| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949 |
- /* browser_html: see browser_html.h. A deliberately tolerant parser: bad
- * nesting is repaired, unknown tags are transparent, and the CSS engine only
- * implements the properties in the browser's documented subset. */
- #include "browser_html.h"
- #include <stdlib.h>
- #include <string.h>
- static int is_space(char c);
- static int is_ident(char c);
- /* ---- storage (one document per process) ----
- *
- * The node/text/attribute arenas are allocated from the ring-3 heap, not
- * static BSS: a large article needs several MB and the process image lives
- * in the 4 MB user window. heap_reset() on navigation releases them with
- * the rest of the page heap. */
- static struct br_rule rule_fallback[BR_RULE_FALLBACK];
- static char css_store[BR_CSS_SIZE];
- static int root_font_px = 16;
- #define BR_BG_URLS 16
- static char bg_urls[BR_BG_URLS][192];
- static unsigned int bg_url_count;
- /* @font-face definitions, one document at a time. The URL is resolved by
- * the browser against `base` (the sheet URL, or the page URL for <style>). */
- struct br_font_def {
- char family[BR_FACE_FAMILY];
- char url[BR_FACE_URL];
- char base[192];
- unsigned char used;
- };
- static struct br_font_def font_defs[BR_MAX_FACES];
- static int font_face_count;
- static char font_base[192];
- static void font_reset(void)
- {
- memset(font_defs, 0, sizeof(font_defs));
- font_face_count = 0;
- font_base[0] = '\0';
- }
- void br_set_font_base(const char *base)
- {
- unsigned int i = 0;
- if (!base)
- base = "";
- while (base[i] && i + 1 < sizeof(font_base)) {
- font_base[i] = base[i];
- i++;
- }
- font_base[i] = '\0';
- }
- int br_font_face_count(void)
- {
- return font_face_count;
- }
- const char *br_font_face_url(int id)
- {
- if (id < 1 || id > font_face_count)
- return "";
- return font_defs[id - 1].url;
- }
- const char *br_font_face_base(int id)
- {
- if (id < 1 || id > font_face_count)
- return "";
- return font_defs[id - 1].base[0] ? font_defs[id - 1].base :
- font_base;
- }
- const char *br_font_face_family(int id)
- {
- if (id < 1 || id > font_face_count)
- return "";
- return font_defs[id - 1].family;
- }
- const char *br_bg_image_url(unsigned int idx)
- {
- if (idx >= bg_url_count)
- return "";
- return bg_urls[idx];
- }
- unsigned int br_root_font_px(void)
- {
- return (unsigned int)root_font_px;
- }
- /* ---- tag table ---- */
- struct tag_name {
- const char *name;
- unsigned char tag;
- };
- static const struct tag_name tag_names[] = {
- { "html", BR_TAG_HTML }, { "head", BR_TAG_HEAD },
- { "body", BR_TAG_BODY }, { "title", BR_TAG_TITLE },
- { "style", BR_TAG_STYLE }, { "script", BR_TAG_SCRIPT },
- { "meta", BR_TAG_META }, { "link", BR_TAG_LINK },
- { "base", BR_TAG_BASE }, { "noscript", BR_TAG_NOSCRIPT },
- { "p", BR_TAG_P }, { "div", BR_TAG_DIV }, { "span", BR_TAG_SPAN },
- { "a", BR_TAG_A }, { "b", BR_TAG_B }, { "strong", BR_TAG_STRONG },
- { "i", BR_TAG_I }, { "em", BR_TAG_EM }, { "u", BR_TAG_U },
- { "code", BR_TAG_CODE }, { "font", BR_TAG_FONT },
- { "small", BR_TAG_SMALL }, { "big", BR_TAG_BIG },
- { "br", BR_TAG_BR }, { "hr", BR_TAG_HR }, { "img", BR_TAG_IMG },
- { "picture", BR_TAG_PICTURE },
- { "h1", BR_TAG_H1 }, { "h2", BR_TAG_H2 }, { "h3", BR_TAG_H3 },
- { "h4", BR_TAG_H4 }, { "h5", BR_TAG_H5 }, { "h6", BR_TAG_H6 },
- { "ul", BR_TAG_UL }, { "ol", BR_TAG_OL }, { "li", BR_TAG_LI },
- { "blockquote", BR_TAG_BLOCKQUOTE }, { "pre", BR_TAG_PRE },
- { "center", BR_TAG_CENTER }, { "table", BR_TAG_TABLE },
- { "thead", BR_TAG_THEAD }, { "tbody", BR_TAG_TBODY },
- { "tfoot", BR_TAG_TFOOT }, { "tr", BR_TAG_TR },
- { "td", BR_TAG_TD }, { "th", BR_TAG_TH },
- { "caption", BR_TAG_CAPTION }, { "form", BR_TAG_FORM },
- { "input", BR_TAG_INPUT }, { "button", BR_TAG_BUTTON },
- { "label", BR_TAG_LABEL }, { "select", BR_TAG_SELECT },
- { "option", BR_TAG_OPTION }, { "textarea", BR_TAG_TEXTAREA },
- { "iframe", BR_TAG_IFRAME }, { "svg", BR_TAG_SVG },
- { "video", BR_TAG_VIDEO }, { "audio", BR_TAG_AUDIO },
- { "canvas", BR_TAG_CANVAS }, { "object", BR_TAG_OBJECT },
- { "main", BR_TAG_MAIN }, { "header", BR_TAG_HEADER },
- { "footer", BR_TAG_FOOTER }, { "nav", BR_TAG_NAV },
- { "section", BR_TAG_SECTION }, { "article", BR_TAG_ARTICLE },
- { "aside", BR_TAG_ASIDE }, { "figure", BR_TAG_FIGURE },
- { "figcaption", BR_TAG_FIGCAPTION }, { "details", BR_TAG_DETAILS },
- { "summary", BR_TAG_SUMMARY }, { "dl", BR_TAG_DL },
- { "dt", BR_TAG_DT }, { "dd", BR_TAG_DD },
- { "address", BR_TAG_ADDRESS }, { "mark", BR_TAG_MARK },
- { "time", BR_TAG_TIME }, { "abbr", BR_TAG_ABBR },
- { "cite", BR_TAG_CITE }, { "q", BR_TAG_Q }, { "s", BR_TAG_S },
- { "strike", BR_TAG_STRIKE }, { "sub", BR_TAG_SUB },
- { "sup", BR_TAG_SUP }, { "bgsound", BR_TAG_BGSOUND },
- { "embed", BR_TAG_EMBED }, { "param", BR_TAG_PARAM },
- { "source", BR_TAG_SOURCE }, { "track", BR_TAG_TRACK },
- { "wbr", BR_TAG_WBR }, { "area", BR_TAG_AREA },
- { "col", BR_TAG_COL }, { "colgroup", BR_TAG_COLGROUP },
- { 0, 0 }
- };
- const char *br_tag_name(unsigned int tag)
- {
- unsigned int i;
- for (i = 0; tag_names[i].name; i++)
- if (tag_names[i].tag == tag)
- return tag_names[i].name;
- return "?";
- }
- static unsigned int s_len(const char *s);
- #define TAG_HASH_SIZE 128
- static unsigned char tag_hash_idx[TAG_HASH_SIZE];
- static int tag_hash_ready;
- static unsigned int tag_hash_key(const char *name, unsigned int len)
- {
- unsigned int i, h = 2166136261u;
- for (i = 0; i < len; i++) {
- h ^= (unsigned char)name[i];
- h *= 16777619u;
- }
- return h & (TAG_HASH_SIZE - 1);
- }
- static void tag_hash_build(void)
- {
- unsigned int i;
- for (i = 0; i < TAG_HASH_SIZE; i++)
- tag_hash_idx[i] = 0xFF;
- for (i = 0; tag_names[i].name; i++) {
- unsigned int h = tag_hash_key(tag_names[i].name,
- s_len(tag_names[i].name));
- while (tag_hash_idx[h] != 0xFF)
- h = (h + 1) & (TAG_HASH_SIZE - 1);
- tag_hash_idx[h] = (unsigned char)i;
- }
- tag_hash_ready = 1;
- }
- unsigned int br_tag_from_name(const char *name)
- {
- char lower[16];
- unsigned int i, len, h;
- unsigned char idx;
- for (i = 0; i < 15 && name[i]; i++) {
- char c = name[i];
- if (c >= 'A' && c <= 'Z')
- c = (char)(c - 'A' + 'a');
- lower[i] = c;
- }
- lower[i] = '\0';
- len = i;
- if (!tag_hash_ready)
- tag_hash_build();
- h = tag_hash_key(lower, len);
- for (;;) {
- idx = tag_hash_idx[h];
- if (idx == 0xFF)
- return BR_TAG_UNKNOWN;
- {
- const char *n = tag_names[idx].name;
- unsigned int j;
- for (j = 0; lower[j] && n[j] == lower[j]; j++)
- ;
- if (!lower[j] && !n[j])
- return tag_names[idx].tag;
- }
- h = (h + 1) & (TAG_HASH_SIZE - 1);
- }
- }
- int br_tag_is_void(unsigned int tag)
- {
- switch (tag) {
- case BR_TAG_BR: case BR_TAG_HR: case BR_TAG_IMG:
- case BR_TAG_META: case BR_TAG_LINK: case BR_TAG_BASE:
- case BR_TAG_INPUT: case BR_TAG_EMBED: case BR_TAG_PARAM:
- case BR_TAG_SOURCE: case BR_TAG_TRACK: case BR_TAG_WBR:
- case BR_TAG_AREA: case BR_TAG_COL:
- return 1;
- default:
- return 0;
- }
- }
- int br_tag_is_block(unsigned int tag)
- {
- switch (tag) {
- case BR_TAG_HTML: case BR_TAG_BODY: case BR_TAG_P:
- case BR_TAG_DIV: case BR_TAG_H1: case BR_TAG_H2: case BR_TAG_H3:
- case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
- case BR_TAG_UL: case BR_TAG_OL: case BR_TAG_LI:
- case BR_TAG_BLOCKQUOTE: case BR_TAG_PRE: case BR_TAG_CENTER:
- case BR_TAG_TABLE: case BR_TAG_THEAD: case BR_TAG_TBODY:
- case BR_TAG_TFOOT: case BR_TAG_TR: case BR_TAG_CAPTION:
- case BR_TAG_FORM: case BR_TAG_MAIN: case BR_TAG_HEADER:
- case BR_TAG_FOOTER: case BR_TAG_NAV: case BR_TAG_SECTION:
- case BR_TAG_ARTICLE: case BR_TAG_ASIDE: case BR_TAG_FIGURE:
- case BR_TAG_FIGCAPTION: case BR_TAG_DETAILS: case BR_TAG_SUMMARY:
- case BR_TAG_PICTURE:
- case BR_TAG_DL: case BR_TAG_DT: case BR_TAG_DD:
- case BR_TAG_ADDRESS: case BR_TAG_HR:
- return 1;
- default:
- return 0;
- }
- }
- /* ---- string / entity helpers ---- */
- static unsigned int s_len(const char *s)
- {
- unsigned int n = 0;
- while (s && s[n])
- n++;
- return n;
- }
- static void s_copy(char *d, const char *s, unsigned int cap)
- {
- unsigned int i = 0;
- if (!cap)
- return;
- while (s && s[i] && i + 1 < cap) {
- d[i] = s[i];
- i++;
- }
- d[i] = '\0';
- }
- static int ci_eq_n(const char *a, const char *b, unsigned int n)
- {
- unsigned int i;
- for (i = 0; i < n; i++) {
- char ca = a[i], cb = b[i];
- if (ca >= 'A' && ca <= 'Z')
- ca = (char)(ca - 'A' + 'a');
- if (cb >= 'A' && cb <= 'Z')
- cb = (char)(cb - 'A' + 'a');
- if (ca != cb)
- return 0;
- }
- return 1;
- }
- static int find_ci(const char *hay, unsigned int len, const char *needle,
- unsigned int nlen, unsigned int from)
- {
- unsigned int i;
- if (nlen == 0 || from >= len)
- return -1;
- for (i = from; i + nlen <= len; i++)
- if (ci_eq_n(hay + i, needle, nlen))
- return (int)i;
- return -1;
- }
- static const struct {
- const char *name;
- const char *text;
- } entities[] = {
- { "amp", "&" }, { "lt", "<" }, { "gt", ">" }, { "quot", "\"" },
- { "apos", "'" }, { "nbsp", " " }, { "copy", "(c)" },
- { "reg", "(r)" }, { "trade", "(tm)" }, { "hellip", "..." },
- { "mdash", "-" }, { "ndash", "-" }, { "laquo", "<<" },
- { "raquo", ">>" }, { "bull", "*" }, { "middot", "." },
- { "times", "x" }, { "divide", "/" }, { "deg", "o" },
- { "plusmn", "+-" }, { "euro", "EUR" }, { "pound", "GBP" },
- { "yen", "JPY" }, { "cent", "c" }, { "sect", "S" },
- { "para", "P" }, { "frac12", "1/2" }, { "frac14", "1/4" },
- { "frac34", "3/4" }, { "sup2", "2" }, { "sup3", "3" },
- { "micro", "u" }, { "alpha", "a" }, { "beta", "b" },
- { "pi", "p" }, { "larr", "<-" }, { "rarr", "->" },
- { 0, 0 }
- };
- static void emit(char *out, unsigned int cap, unsigned int *n, char c)
- {
- if (*n + 1 < cap)
- out[(*n)++] = c;
- }
- static void decode_entity(const char *s, unsigned int len, unsigned int *pos,
- char *out, unsigned int cap, unsigned int *n)
- {
- unsigned int start = *pos + 1, i = start;
- unsigned int code = 0;
- int ok = 0;
- while (i < len && s[i] != ';' && i - start < 12)
- i++;
- if (i >= len || s[i] != ';') {
- /* Not an entity: emit the '&' and advance past it. Leaving
- * *pos unchanged here used to spin forever on bare '&'. */
- emit(out, cap, n, '&');
- *pos = start;
- return;
- }
- if (s[start] == '#') {
- unsigned int j = start + 1;
- if (j < i && (s[j] == 'x' || s[j] == 'X')) {
- j++;
- for (; j < i; j++) {
- char c = s[j];
- int d = -1;
- if (c >= '0' && c <= '9') d = c - '0';
- else if (c >= 'a' && c <= 'f') d = c - 'a' + 10;
- else if (c >= 'A' && c <= 'F') d = c - 'A' + 10;
- if (d < 0) break;
- code = code * 16 + (unsigned int)d;
- ok = 1;
- }
- } else {
- for (; j < i; j++) {
- if (s[j] < '0' || s[j] > '9')
- break;
- code = code * 10 + (unsigned int)(s[j] - '0');
- ok = 1;
- }
- }
- } else {
- unsigned int k;
- for (k = 0; entities[k].name; k++) {
- unsigned int el = s_len(entities[k].name);
- unsigned int j;
- if (el != i - start)
- continue;
- for (j = 0; j < el; j++)
- if (s[start + j] != entities[k].name[j])
- break;
- if (j == el) {
- const char *t = entities[k].text;
- while (*t)
- emit(out, cap, n, *t++);
- *pos = i + 1;
- return;
- }
- }
- }
- if (ok && code) {
- if (code == 0x00A0)
- emit(out, cap, n, ' ');
- else if (code == 0x2013 || code == 0x2014)
- emit(out, cap, n, '-');
- else if (code == 0x2018 || code == 0x2019)
- emit(out, cap, n, '\'');
- else if (code == 0x201C || code == 0x201D)
- emit(out, cap, n, '"');
- else if (code == 0x2026) {
- emit(out, cap, n, '.'); emit(out, cap, n, '.');
- emit(out, cap, n, '.');
- } else if (code <= 0xFF) {
- emit(out, cap, n, (char)code);
- } else {
- emit(out, cap, n, '?');
- }
- } else {
- emit(out, cap, n, '?');
- }
- *pos = i + 1;
- }
- /* Decode UTF-8 and entities into out. collapse folds whitespace runs to one
- * space and drops leading spaces. Returns the output length. */
- static unsigned int decode_text(const char *s, unsigned int len, char *out,
- unsigned int cap, int collapse)
- {
- unsigned int i = 0, n = 0;
- while (i < len) {
- unsigned char c = (unsigned char)s[i];
- if (c == '&') {
- decode_entity(s, len, &i, out, cap, &n);
- continue;
- }
- if (c < 0x80) {
- i++;
- if (collapse && (c == ' ' || c == '\t' || c == '\n' ||
- c == '\r' || c == '\f')) {
- if (n > 0 && out[n - 1] == ' ')
- continue;
- emit(out, cap, &n, ' ');
- continue;
- }
- if (!collapse && c == '\t') {
- emit(out, cap, &n, ' ');
- emit(out, cap, &n, ' ');
- emit(out, cap, &n, ' ');
- emit(out, cap, &n, ' ');
- continue;
- }
- emit(out, cap, &n, (char)c);
- continue;
- }
- /* UTF-8 -> Latin-1 where possible (the 8x16 font has those). */
- if ((c & 0xE0) == 0xC0 && i + 1 < len) {
- unsigned int cp = ((c & 0x1F) << 6) |
- ((unsigned char)s[i + 1] & 0x3F);
- i += 2;
- if (cp == 0xA0)
- cp = ' ';
- else if (cp == 0xB7)
- cp = '-';
- else if (cp == 0xD7)
- cp = 'x';
- else if (cp == 0xB0)
- cp = 'o';
- if (cp <= 0xFF)
- emit(out, cap, &n, (char)cp);
- else
- emit(out, cap, &n, '?');
- } else if ((c & 0xF0) == 0xE0 && i + 2 < len) {
- unsigned int cp = ((c & 0x0F) << 12) |
- (((unsigned char)s[i + 1] & 0x3F) << 6) |
- ((unsigned char)s[i + 2] & 0x3F);
- i += 3;
- if (cp == 0x2013 || cp == 0x2014) {
- emit(out, cap, &n, '-');
- } else if (cp == 0x2018 || cp == 0x2019) {
- emit(out, cap, &n, '\'');
- } else if (cp == 0x201C || cp == 0x201D) {
- emit(out, cap, &n, '"');
- } else if (cp == 0x2026) {
- emit(out, cap, &n, '.'); emit(out, cap, &n, '.');
- emit(out, cap, &n, '.');
- } else if (cp == 0x2192) {
- emit(out, cap, &n, '-'); emit(out, cap, &n, '>');
- } else if (cp == 0x2264) {
- emit(out, cap, &n, '<'); emit(out, cap, &n, '=');
- } else {
- emit(out, cap, &n, '?');
- }
- } else {
- i++;
- emit(out, cap, &n, '?');
- }
- }
- out[n] = '\0';
- return n;
- }
- /* Store an attribute string (entity-decoded) in the attr arena. 0 = no
- * space / empty. */
- static unsigned int store_attr(struct br_doc *d, const char *s,
- unsigned int len, unsigned int limit)
- {
- unsigned int n;
- if (d->attrs_used + 2 >= BR_ATTR_SIZE)
- return 0;
- if (len > limit)
- len = limit;
- n = decode_text(s, len, d->attrs + d->attrs_used,
- BR_ATTR_SIZE - d->attrs_used, 0);
- if (n == 0)
- return 0;
- {
- unsigned int off = d->attrs_used;
- d->attrs_used += n + 1;
- return off;
- }
- }
- static const char *attr_ptr(struct br_doc *d, unsigned int off)
- {
- return off ? d->attrs + off : "";
- }
- /* Copy a raw CSS string into the attr arena (no entity decoding). */
- static unsigned int store_raw(struct br_doc *d, const char *s,
- unsigned int len, unsigned int limit)
- {
- unsigned int off;
- if (len > limit)
- len = limit;
- if (!len || d->attrs_used + len + 1 > BR_ATTR_SIZE)
- return 0;
- off = d->attrs_used;
- {
- unsigned int i;
- for (i = 0; i < len; i++)
- d->attrs[off + i] = s[i];
- }
- d->attrs[off + len] = '\0';
- d->attrs_used += len + 1;
- return off;
- }
- #define VAR_HASH_SIZE 1024
- static unsigned int var_hash(const char *name, unsigned int len)
- {
- unsigned int i, h = 2166136261u;
- for (i = 0; i < len; i++) {
- h ^= (unsigned char)name[i];
- h *= 16777619u;
- }
- return h & (VAR_HASH_SIZE - 1);
- }
- static unsigned int css_var_find(struct br_doc *d, const char *name,
- unsigned int nlen)
- {
- unsigned int h = var_hash(name, nlen);
- for (;;) {
- unsigned int i = d->var_slot[h];
- if (!i)
- return BR_MAX_VARS;
- i--;
- if (s_len(attr_ptr(d, d->var_name[i])) == nlen) {
- const char *n = attr_ptr(d, d->var_name[i]);
- unsigned int j;
- for (j = 0; j < nlen && n[j] == name[j]; j++)
- ;
- if (j == nlen)
- return i;
- }
- h = (h + 1) & (VAR_HASH_SIZE - 1);
- }
- }
- static void css_var_set(struct br_doc *d, const char *name,
- unsigned int nlen, const char *val, unsigned int vlen)
- {
- unsigned int i, noff, voff;
- if (nlen < 3 || name[0] != '-' || name[1] != '-' || !vlen)
- return;
- noff = store_raw(d, name, nlen, 63);
- voff = store_raw(d, val, vlen, 199);
- if (!noff || !voff)
- return;
- i = css_var_find(d, name, nlen);
- if (i >= BR_MAX_VARS) {
- unsigned int h;
- if (d->nvars >= BR_MAX_VARS)
- return;
- i = d->nvars++;
- d->var_name[i] = noff;
- h = var_hash(name, nlen);
- while (d->var_slot[h])
- h = (h + 1) & (VAR_HASH_SIZE - 1);
- d->var_slot[h] = (unsigned short)(i + 1);
- }
- d->var_val[i] = voff;
- }
- /* Find the end of a declaration value: the next ';' outside quotes and
- * parentheses. The caller passes the position just after the ':'. */
- static unsigned int css_value_end(const char *s, unsigned int len,
- unsigned int i)
- {
- int depth = 0;
- char quote = 0;
- while (i < len) {
- char c = s[i];
- if (quote) {
- if (c == quote)
- quote = 0;
- else if (c == '\\' && i + 1 < len)
- i++;
- } else if (c == '"' || c == '\'') {
- quote = c;
- } else if (c == '(') {
- depth++;
- } else if (c == ')' && depth > 0) {
- depth--;
- } else if (c == ';' && depth == 0) {
- break;
- }
- i++;
- }
- return i;
- }
- /* Theme-variant selectors (html.dark, .skin-theme-clientpref-night) carry a
- * second palette. We render the light page, so skip their custom-property
- * definitions; otherwise their last-wins values leak into the light rules. */
- static int css_dark_selector(const char *s, unsigned int len)
- {
- unsigned int i;
- for (i = 0; i + 4 <= len; i++) {
- if ((s[i] == 'n' || s[i] == 'N') &&
- (s[i + 1] == 'i' || s[i + 1] == 'I') &&
- (s[i + 2] == 'g' || s[i + 2] == 'G') &&
- (s[i + 3] == 'h' || s[i + 3] == 'H') &&
- (s[i + 4] == 't' || s[i + 4] == 'T'))
- return 1;
- if ((s[i] == 'd' || s[i] == 'D') &&
- (s[i + 1] == 'a' || s[i + 1] == 'A') &&
- (s[i + 2] == 'r' || s[i + 2] == 'R') &&
- (s[i + 3] == 'k' || s[i + 3] == 'K'))
- return 1;
- if (i + 12 <= len &&
- (s[i] == 'c' || s[i] == 'C') &&
- (s[i + 1] == 'l' || s[i + 1] == 'L') &&
- (s[i + 2] == 'i' || s[i + 2] == 'I') &&
- (s[i + 3] == 'e' || s[i + 3] == 'E') &&
- (s[i + 4] == 'n' || s[i + 4] == 'N') &&
- (s[i + 5] == 't' || s[i + 5] == 'T') &&
- (s[i + 6] == 'p' || s[i + 6] == 'P') &&
- (s[i + 7] == 'r' || s[i + 7] == 'R') &&
- (s[i + 8] == 'e' || s[i + 8] == 'E') &&
- (s[i + 9] == 'f' || s[i + 9] == 'F') &&
- s[i + 10] == '-' &&
- (s[i + 11] == 'o' || s[i + 11] == 'O') &&
- (s[i + 12] == 's' || s[i + 12] == 'S'))
- return 1;
- }
- return 0;
- }
- /* Collect --custom-property definitions from one declaration block. */
- static void css_collect_block(struct br_doc *d, const char *s,
- unsigned int len)
- {
- unsigned int i = 0;
- while (i < len) {
- unsigned int pstart, pend, vstart, vend;
- while (i < len && (is_space(s[i]) || s[i] == ';'))
- i++;
- if (i >= len)
- break;
- pstart = i;
- while (i < len && s[i] != ':' && s[i] != ';')
- i++;
- pend = i;
- if (i >= len || s[i] != ':') {
- if (i < len)
- i++;
- continue;
- }
- i++;
- vstart = i;
- i = css_value_end(s, len, i);
- vend = i;
- if (i < len)
- i++;
- while (pstart < pend && is_space(s[pstart]))
- pstart++;
- while (pend > pstart && is_space(s[pend - 1]))
- pend--;
- while (vstart < vend && is_space(s[vstart]))
- vstart++;
- while (vend > vstart && is_space(s[vend - 1]))
- vend--;
- if (vend - vstart >= 10 &&
- ci_eq_n(s + vend - 10, "!important", 10)) {
- vend -= 10;
- while (vend > vstart && is_space(s[vend - 1]))
- vend--;
- }
- if (pend - pstart >= 3 && s[pstart] == '-' &&
- s[pstart + 1] == '-')
- css_var_set(d, s + pstart, pend - pstart,
- s + vstart, vend - vstart);
- }
- }
- const char *br_href(struct br_doc *d, unsigned int node)
- {
- return attr_ptr(d, d->nodes[node].href_off);
- }
- const char *br_src(struct br_doc *d, unsigned int node)
- {
- return attr_ptr(d, d->nodes[node].src_off);
- }
- const char *br_alt(struct br_doc *d, unsigned int node)
- {
- return attr_ptr(d, d->nodes[node].alt_off);
- }
- const char *br_rel(struct br_doc *d, unsigned int node)
- {
- return attr_ptr(d, d->nodes[node].rel_off);
- }
- const char *br_value(struct br_doc *d, unsigned int node)
- {
- return attr_ptr(d, d->nodes[node].src_off);
- }
- const char *br_placeholder(struct br_doc *d, unsigned int node)
- {
- return attr_ptr(d, d->nodes[node].alt_off);
- }
- const char *br_type(struct br_doc *d, unsigned int node)
- {
- return attr_ptr(d, d->nodes[node].rel_off);
- }
- /* ---- document setup / node append ---- */
- static void rule_index_reset(struct br_doc *d)
- {
- unsigned int i;
- for (i = 0; i < BR_MAX_RULES; i++)
- d->rule_next[i] = 0xFFFF;
- for (i = 0; i < BR_RULE_HASH; i++)
- d->rule_bucket[i] = 0xFFFF;
- for (i = 0; i < BR_TAG_MAX; i++)
- d->rule_tag[i] = 0xFFFF;
- d->rule_any = 0xFFFF;
- }
- void br_doc_init(struct br_doc *d)
- {
- unsigned int i;
- for (i = 0; i < sizeof(*d); i++)
- ((unsigned char *)d)[i] = 0;
- rule_index_reset(d);
- d->nodes = malloc(BR_MAX_NODES * sizeof(*d->nodes));
- d->text = malloc(BR_TEXT_SIZE);
- d->attrs = malloc(BR_ATTR_SIZE);
- if (!d->nodes || !d->text || !d->attrs) {
- free(d->nodes);
- free(d->text);
- free(d->attrs);
- d->nodes = 0;
- d->text = 0;
- d->attrs = 0;
- return;
- }
- d->rules = rule_fallback;
- d->rule_cap = BR_RULE_FALLBACK;
- d->attrs_used = 1; /* offset 0 = "no attribute" */
- d->attrs[0] = '\0';
- d->nvars = 0;
- d->root = 0;
- for (unsigned int vi = 0; vi < VAR_HASH_SIZE; vi++)
- d->var_slot[vi] = 0;
- root_font_px = 16;
- bg_url_count = 0;
- font_reset();
- }
- void br_set_rule_store(struct br_doc *d, struct br_rule *buf,
- unsigned int cap)
- {
- if (!buf || !cap) {
- d->rules = rule_fallback;
- d->rule_cap = BR_RULE_FALLBACK;
- d->nrules = 0;
- return;
- }
- if (cap > BR_MAX_RULES)
- cap = BR_MAX_RULES;
- d->rules = buf;
- d->rule_cap = cap;
- d->nrules = 0;
- }
- static void doc_reset(struct br_doc *d)
- {
- unsigned int i;
- d->nnode = 0;
- d->text_used = 0;
- d->attrs_used = 1;
- d->attrs[0] = '\0';
- d->nrules = 0;
- rule_index_reset(d);
- d->title[0] = '\0';
- d->inline_css_len = 0;
- d->nvars = 0;
- d->truncated = 0;
- d->plain = 0;
- d->script_src = 0;
- d->nscripts = 0;
- d->scripts_truncated = 0;
- font_reset();
- for (i = 0; i < BR_MAX_NODES; i++) {
- d->nodes[i].type = BR_NODE_ELEM;
- d->nodes[i].tag = BR_TAG_UNKNOWN;
- d->nodes[i].flags = 0;
- d->nodes[i].parent = 0;
- d->nodes[i].first = d->nodes[i].last = 0;
- d->nodes[i].next = 0;
- d->nodes[i].text_off = d->nodes[i].text_len = 0;
- d->nodes[i].href_off = d->nodes[i].class_off = 0;
- d->nodes[i].id_off = d->nodes[i].style_off = 0;
- d->nodes[i].src_off = d->nodes[i].alt_off = 0;
- d->nodes[i].color_off = d->nodes[i].rel_off = 0;
- d->nodes[i].width_attr = d->nodes[i].height_attr = 0;
- d->nodes[i].colspan = d->nodes[i].rowspan = 0;
- d->nodes[i].name_off = 0;
- d->nodes[i].extra_off = d->nodes[i].extra_len = 0;
- d->nodes[i].width_pct = d->nodes[i].height_pct = 0;
- d->nodes[i].svg_x = d->nodes[i].svg_y = 0;
- d->nodes[i].svg_rx = 0;
- d->nodes[i].img_index = -1;
- d->nodes[i].bg_img_index = -1;
- d->nodes[i].style_valid = 0;
- }
- }
- static unsigned int node_append(struct br_doc *d, unsigned int parent,
- unsigned char type, unsigned char tag)
- {
- unsigned int idx;
- struct br_node *n;
- if (d->nnode >= BR_MAX_NODES) {
- d->truncated = 1;
- return 0;
- }
- idx = d->nnode++;
- n = &d->nodes[idx];
- n->type = type;
- n->tag = tag;
- n->parent = (unsigned short)parent;
- n->img_index = -1;
- n->bg_img_index = -1;
- if (d->nodes[parent].last)
- d->nodes[d->nodes[parent].last].next = (unsigned short)idx;
- else
- d->nodes[parent].first = (unsigned short)idx;
- d->nodes[parent].last = (unsigned short)idx;
- return idx;
- }
- static unsigned int append_text(struct br_doc *d, unsigned int parent,
- const char *s, unsigned int len, int pre)
- {
- char *dst;
- unsigned int n;
- unsigned int idx;
- if (len == 0)
- return 0;
- if (d->text_used + 2 >= BR_TEXT_SIZE) {
- d->truncated = 1;
- return 0;
- }
- dst = d->text + d->text_used;
- n = decode_text(s, len, dst, BR_TEXT_SIZE - d->text_used, !pre);
- if (n == 0) {
- d->text[d->text_used] = '\0';
- return 0;
- }
- idx = node_append(d, parent, BR_NODE_TEXT, 0);
- if (!idx)
- return 0;
- d->nodes[idx].text_off = (unsigned short)d->text_used;
- d->nodes[idx].text_len = (unsigned short)n;
- d->nodes[idx].flags = pre ? BR_NODE_PREFORMAT : 0;
- d->text_used += n + 1;
- return idx;
- }
- /* ---- runtime DOM helpers (JavaScript bindings) ---- */
- static int same_string(const char *a, const char *b)
- {
- while (*a && *a == *b) {
- a++;
- b++;
- }
- return *a == *b;
- }
- static int same_string_n(const char *a, const char *b, unsigned int n)
- {
- unsigned int i;
- for (i = 0; i < n; i++)
- if (a[i] != b[i])
- return 0;
- return 1;
- }
- static int node_detached(struct br_doc *d, unsigned int node)
- {
- unsigned int guard = 0;
- while (node && node < d->nnode && guard++ <= d->nnode) {
- if (d->nodes[node].flags & BR_NODE_DETACHED)
- return 1;
- node = d->nodes[node].parent;
- }
- return 0;
- }
- unsigned int br_find_id(struct br_doc *d, const char *id)
- {
- unsigned int i;
- if (!d || !id)
- return 0;
- for (i = 0; i < d->nnode; i++) {
- struct br_node *n = &d->nodes[i];
- if (n->type == BR_NODE_ELEM && n->id_off &&
- same_string(d->attrs + n->id_off, id) &&
- !node_detached(d, i))
- return i + 1;
- }
- return 0;
- }
- static void text_walk(struct br_doc *d, unsigned int node, char *out,
- unsigned int cap, unsigned int *used)
- {
- unsigned int c;
- if (node >= d->nnode)
- return;
- if (d->nodes[node].type == BR_NODE_TEXT) {
- unsigned int len = d->nodes[node].text_len;
- unsigned int off = d->nodes[node].text_off;
- if (*used + len >= cap)
- len = *used + 1 < cap ? cap - *used - 1 : 0;
- for (c = 0; c < len; c++)
- out[*used + c] = d->text[off + c];
- *used += len;
- return;
- }
- for (c = d->nodes[node].first; c; c = d->nodes[c].next)
- text_walk(d, c, out, cap, used);
- }
- unsigned int br_text_content(struct br_doc *d, unsigned int node,
- char *out, unsigned int cap)
- {
- unsigned int used = 0;
- if (!d || !out || cap == 0 || node >= d->nnode)
- return 0;
- text_walk(d, node, out, cap, &used);
- out[used] = '\0';
- return used;
- }
- int br_set_text_content(struct br_doc *d, unsigned int node,
- const char *text, unsigned int len)
- {
- struct br_node *n;
- unsigned int idx;
- unsigned int i;
- if (!d || node >= d->nnode)
- return -1;
- /* Detach the old subtree; its arena storage is reclaimed on the next
- * navigation. */
- {
- unsigned int c;
- for (c = d->nodes[node].first; c; c = d->nodes[c].next)
- d->nodes[c].flags |= BR_NODE_DETACHED;
- }
- d->nodes[node].first = d->nodes[node].last = 0;
- d->nodes[node].style_valid = 0;
- if (len == 0)
- return 0;
- if (d->text_used + len + 1 > BR_TEXT_SIZE) {
- d->truncated = 1;
- return -1;
- }
- idx = node_append(d, node, BR_NODE_TEXT, 0);
- if (!idx)
- return -1;
- for (i = 0; i < len; i++)
- d->text[d->text_used + i] = text[i];
- d->text[d->text_used + len] = '\0';
- n = &d->nodes[idx];
- n->text_off = (unsigned short)d->text_used;
- n->text_len = (unsigned short)len;
- n->flags = 0;
- d->text_used += len + 1;
- return 0;
- }
- /* Replace the value of an existing text or comment node in place, keeping the
- * node handle (and therefore the JS wrapper) valid. */
- int br_set_text_node(struct br_doc *d, unsigned int node,
- const char *text, unsigned int len)
- {
- struct br_node *n;
- unsigned int i;
- if (!d || node >= d->nnode)
- return -1;
- n = &d->nodes[node];
- if (n->type != BR_NODE_TEXT && n->type != BR_NODE_COMMENT)
- return -1;
- if (d->text_used + len + 1 > BR_TEXT_SIZE) {
- d->truncated = 1;
- return -1;
- }
- for (i = 0; i < len; i++)
- d->text[d->text_used + i] = text[i];
- d->text[d->text_used + len] = '\0';
- n->text_off = (unsigned short)d->text_used;
- n->text_len = (unsigned short)len;
- d->text_used += len + 1;
- return 0;
- }
- /* ---- runtime DOM construction and mutation ---- */
- static unsigned int text_len(const char *s)
- {
- unsigned int n = 0;
- while (s && s[n])
- n++;
- return n;
- }
- static void node_zero(struct br_node *n)
- {
- unsigned int i;
- for (i = 0; i < sizeof(*n); i++)
- ((unsigned char *)n)[i] = 0;
- n->img_index = -1;
- n->bg_img_index = -1;
- }
- static void node_unlink(struct br_doc *d, unsigned int handle)
- {
- unsigned int node, parent, c, prev = 0;
- if (!d || handle == 0 || handle > d->nnode)
- return;
- node = handle - 1;
- parent = d->nodes[node].parent;
- if (parent >= d->nnode) {
- d->nodes[node].parent = 0;
- d->nodes[node].next = 0;
- return;
- }
- c = d->nodes[parent].first;
- while (c && c != node) {
- prev = c;
- c = d->nodes[c].next;
- }
- if (c == node) {
- if (prev)
- d->nodes[prev].next = d->nodes[node].next;
- else
- d->nodes[parent].first = d->nodes[node].next;
- if (d->nodes[parent].last == node)
- d->nodes[parent].last = (unsigned short)prev;
- }
- d->nodes[node].parent = 0;
- d->nodes[node].next = 0;
- d->nodes[node].flags |= BR_NODE_DETACHED;
- }
- static void parse_attrs(struct br_doc *d, struct br_node *n,
- const char *s, unsigned int len);
- static unsigned int find_tag_end(const char *s, unsigned int i,
- unsigned int len)
- {
- char quote = 0;
- while (i < len) {
- char c = s[i];
- if (quote) {
- if (c == quote)
- quote = 0;
- } else if (c == '"' || c == '\'') {
- quote = c;
- } else if (c == '>') {
- return i;
- }
- i++;
- }
- return len;
- }
- int br_set_inner_html(struct br_doc *d, unsigned int node,
- const char *html, unsigned int len)
- {
- unsigned int stack[64];
- unsigned int sp = 1, i = 0;
- if (!d || node >= d->nnode)
- return -1;
- stack[0] = node;
- {
- unsigned int c;
- for (c = d->nodes[node].first; c; c = d->nodes[c].next)
- d->nodes[c].flags |= BR_NODE_DETACHED;
- }
- d->nodes[node].first = d->nodes[node].last = 0;
- d->nodes[node].style_valid = 0;
- while (i < len) {
- if (html[i] != '<') {
- unsigned int start = i, end;
- while (i < len && html[i] != '<')
- i++;
- end = i;
- if (end > start) {
- unsigned int h = br_create_text(d, html + start,
- end - start);
- if (h)
- br_append_child(d, stack[sp - 1], h);
- }
- continue;
- }
- if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
- html[i + 3] == '-') {
- int p = find_ci(html, len, "-->", 3, i + 4);
- unsigned int end = p >= 0 ? (unsigned int)p : len;
- unsigned int h = br_create_comment(d, html + i + 4,
- end > i + 4 ?
- end - i - 4 : 0);
- if (h)
- br_append_child(d, stack[sp - 1], h);
- i = p >= 0 ? (unsigned int)p + 3 : len;
- continue;
- }
- if (html[i + 1] == '/') {
- unsigned int p = find_tag_end(html, i + 2, len);
- i = p < len ? p + 1 : len;
- if (sp > 1)
- sp--;
- continue;
- }
- if (html[i + 1] == '!' || html[i + 1] == '?') {
- unsigned int p = find_tag_end(html, i + 2, len);
- i = p < len ? p + 1 : len;
- continue;
- }
- {
- unsigned int p = find_tag_end(html, i + 1, len);
- unsigned int ns = i + 1, ne, as, ae;
- unsigned int h, k;
- char name[32];
- int selfclose = 0;
- while (ns < p && is_space(html[ns]))
- ns++;
- ne = ns;
- while (ne < p && !is_space(html[ne]) && html[ne] != '/')
- ne++;
- as = ne;
- ae = p;
- if (ae > as && html[ae - 1] == '/') {
- selfclose = 1;
- ae--;
- }
- if (ne == ns) {
- i = p < len ? p + 1 : len;
- continue;
- }
- if (ne - ns >= sizeof(name)) {
- i = p < len ? p + 1 : len;
- continue;
- }
- for (k = 0; k < ne - ns; k++)
- name[k] = html[ns + k];
- name[ne - ns] = '\0';
- h = br_create_element(d, name, ne - ns);
- if (!h) {
- i = p < len ? p + 1 : len;
- continue;
- }
- if (ae > as)
- parse_attrs(d, &d->nodes[h - 1], html + as,
- ae - as);
- br_append_child(d, stack[sp - 1], h);
- {
- unsigned int tag = d->nodes[h - 1].tag;
- if (!selfclose && !br_tag_is_void(tag) &&
- sp < sizeof(stack) / sizeof(stack[0]))
- stack[sp++] = h - 1;
- }
- i = p < len ? p + 1 : len;
- }
- }
- return 0;
- }
- unsigned int br_find_tag(struct br_doc *d, unsigned int tag)
- {
- unsigned int i;
- if (!d)
- return 0;
- for (i = 0; i < d->nnode; i++)
- if (d->nodes[i].type == BR_NODE_ELEM &&
- d->nodes[i].tag == (unsigned char)tag &&
- !node_detached(d, i))
- return i + 1;
- return 0;
- }
- unsigned int br_create_element(struct br_doc *d, const char *name,
- unsigned int len)
- {
- struct br_node *n;
- unsigned int idx;
- if (!d || !name)
- return 0;
- if (d->nnode >= BR_MAX_NODES) {
- d->truncated = 1;
- return 0;
- }
- idx = d->nnode++;
- n = &d->nodes[idx];
- node_zero(n);
- n->type = BR_NODE_ELEM;
- n->flags = BR_NODE_DETACHED;
- n->tag = (unsigned char)br_tag_from_name(name);
- if (n->tag == BR_TAG_UNKNOWN && len)
- n->name_off = store_attr(d, name, len, 31);
- return idx + 1;
- }
- unsigned int br_create_text(struct br_doc *d, const char *text,
- unsigned int len)
- {
- struct br_node *n;
- unsigned int idx, i;
- if (!d)
- return 0;
- if (d->nnode >= BR_MAX_NODES || d->text_used + len + 1 > BR_TEXT_SIZE) {
- d->truncated = 1;
- return 0;
- }
- idx = d->nnode++;
- n = &d->nodes[idx];
- node_zero(n);
- n->type = BR_NODE_TEXT;
- n->flags = BR_NODE_DETACHED;
- n->text_off = (unsigned short)d->text_used;
- n->text_len = (unsigned short)len;
- for (i = 0; i < len; i++)
- d->text[d->text_used + i] = text[i];
- d->text[d->text_used + len] = '\0';
- d->text_used += len + 1;
- return idx + 1;
- }
- unsigned int br_create_comment(struct br_doc *d, const char *text,
- unsigned int len)
- {
- unsigned int h = br_create_text(d, text, len);
- if (h)
- d->nodes[h - 1].type = BR_NODE_COMMENT;
- return h;
- }
- unsigned int br_create_fragment(struct br_doc *d)
- {
- struct br_node *n;
- unsigned int idx;
- if (!d)
- return 0;
- if (d->nnode >= BR_MAX_NODES) {
- d->truncated = 1;
- return 0;
- }
- idx = d->nnode++;
- n = &d->nodes[idx];
- node_zero(n);
- n->type = BR_NODE_FRAGMENT;
- n->flags = BR_NODE_DETACHED;
- return idx + 1;
- }
- int br_append_child(struct br_doc *d, unsigned int parent,
- unsigned int child)
- {
- unsigned int p;
- if (!d || parent >= d->nnode || child == 0 || child > d->nnode ||
- parent == child - 1)
- return -1;
- if (d->nodes[child - 1].type == BR_NODE_FRAGMENT) {
- /* Appending a fragment inserts its children, not the fragment. */
- unsigned int c = d->nodes[child - 1].first;
- while (c) {
- unsigned int next = d->nodes[c].next;
- br_append_child(d, parent, c + 1);
- c = next;
- }
- return 0;
- }
- for (p = parent; p && p < d->nnode; p = d->nodes[p].parent)
- if (p == child - 1)
- return -1;
- node_unlink(d, child);
- d->nodes[child - 1].flags &= (unsigned char)~BR_NODE_DETACHED;
- d->nodes[child - 1].parent = (unsigned short)parent;
- d->nodes[child - 1].next = 0;
- if (d->nodes[parent].last)
- d->nodes[d->nodes[parent].last].next = (unsigned short)(child - 1);
- else
- d->nodes[parent].first = (unsigned short)(child - 1);
- d->nodes[parent].last = (unsigned short)(child - 1);
- return 0;
- }
- int br_insert_before(struct br_doc *d, unsigned int parent,
- unsigned int child, unsigned int before)
- {
- unsigned int prev = 0, c;
- if (before == 0)
- return br_append_child(d, parent, child);
- if (!d || parent >= d->nnode || child == 0 || child > d->nnode ||
- before > d->nnode || parent == child - 1)
- return -1;
- if (d->nodes[before - 1].parent != parent)
- return -1;
- if (d->nodes[child - 1].type == BR_NODE_FRAGMENT) {
- unsigned int c = d->nodes[child - 1].first;
- while (c) {
- unsigned int next = d->nodes[c].next;
- br_insert_before(d, parent, c + 1, before);
- c = next;
- }
- return 0;
- }
- {
- unsigned int p;
- for (p = parent; p && p < d->nnode; p = d->nodes[p].parent)
- if (p == child - 1)
- return -1;
- }
- node_unlink(d, child);
- d->nodes[child - 1].flags &= (unsigned char)~BR_NODE_DETACHED;
- c = d->nodes[parent].first;
- while (c && c != before - 1) {
- prev = c;
- c = d->nodes[c].next;
- }
- if (c != before - 1)
- return -1;
- d->nodes[child - 1].parent = (unsigned short)parent;
- d->nodes[child - 1].next = (unsigned short)(before - 1);
- if (prev)
- d->nodes[prev].next = (unsigned short)(child - 1);
- else
- d->nodes[parent].first = (unsigned short)(child - 1);
- return 0;
- }
- int br_remove_child(struct br_doc *d, unsigned int parent,
- unsigned int child)
- {
- if (!d || child == 0 || child > d->nnode ||
- d->nodes[child - 1].parent != parent)
- return -1;
- node_unlink(d, child);
- return 0;
- }
- unsigned int br_find_child(struct br_doc *d, unsigned int parent,
- unsigned int child)
- {
- unsigned int c;
- if (!d || parent >= d->nnode || child == 0 || child > d->nnode)
- return 0;
- for (c = d->nodes[parent].first; c; c = d->nodes[c].next)
- if (c == child - 1)
- return child;
- return 0;
- }
- const char *br_find_attr(struct br_doc *d, unsigned int node,
- const char *name)
- {
- struct br_node *n;
- const char *p, *end, *found = 0;
- if (!d || node >= d->nnode || !name)
- return 0;
- n = &d->nodes[node];
- p = d->attrs + n->extra_off;
- end = p + n->extra_len;
- while (p + 1 < end) {
- const char *an = p;
- const char *av;
- p += text_len(p) + 1;
- if (p >= end)
- break;
- av = p;
- p += text_len(p) + 1;
- if (same_string(an, name))
- found = av;
- }
- if (found)
- return found;
- if (same_string(name, "id") && n->id_off)
- return d->attrs + n->id_off;
- if (same_string(name, "class") && n->class_off)
- return d->attrs + n->class_off;
- if (same_string(name, "style") && n->style_off)
- return d->attrs + n->style_off;
- if (same_string(name, "href") && n->href_off)
- return d->attrs + n->href_off;
- if (same_string(name, "src") && n->src_off)
- return d->attrs + n->src_off;
- if (same_string(name, "alt") && n->alt_off)
- return d->attrs + n->alt_off;
- if (same_string(name, "rel") && n->rel_off)
- return d->attrs + n->rel_off;
- return 0;
- }
- static void attr_note_known(struct br_doc *d, struct br_node *n,
- const char *name, const char *value,
- unsigned int len)
- {
- if (same_string(name, "id"))
- n->id_off = len ? store_attr(d, value, len, 64) : 0;
- else if (same_string(name, "class"))
- n->class_off = len ? store_attr(d, value, len, 120) : 0;
- else if (same_string(name, "style"))
- n->style_off = len ? store_attr(d, value, len, 240) : 0;
- else if (same_string(name, "href"))
- n->href_off = len ? store_attr(d, value, len, 480) : 0;
- else if (same_string(name, "src"))
- n->src_off = len ? store_attr(d, value, len, 400) : 0;
- else if (same_string(name, "alt"))
- n->alt_off = len ? store_attr(d, value, len, 120) : 0;
- else if (same_string(name, "rel"))
- n->rel_off = len ? store_attr(d, value, len, 64) : 0;
- }
- int br_set_attr(struct br_doc *d, unsigned int node, const char *name,
- const char *value, unsigned int len)
- {
- struct br_node *n;
- unsigned int start, nl, need;
- const char *p, *old, *oldend;
- if (!d || node >= d->nnode || !name)
- return -1;
- n = &d->nodes[node];
- nl = text_len(name);
- need = nl + 1 + len + 1;
- start = d->attrs_used;
- old = d->attrs + n->extra_off;
- oldend = old + n->extra_len;
- if (start + n->extra_len + need > BR_ATTR_SIZE) {
- d->truncated = 1;
- return -1;
- }
- p = old;
- while (p + 1 < oldend) {
- const char *an = p;
- const char *av;
- unsigned int anl, avl, k;
- anl = text_len(an);
- p += anl + 1;
- if (p >= oldend)
- break;
- av = p;
- avl = text_len(av);
- p += avl + 1;
- if (same_string(an, name))
- continue;
- for (k = 0; k < anl; k++)
- d->attrs[d->attrs_used++] = an[k];
- d->attrs[d->attrs_used++] = '\0';
- for (k = 0; k < avl; k++)
- d->attrs[d->attrs_used++] = av[k];
- d->attrs[d->attrs_used++] = '\0';
- }
- {
- unsigned int k;
- for (k = 0; k < nl; k++)
- d->attrs[d->attrs_used++] = name[k];
- d->attrs[d->attrs_used++] = '\0';
- for (k = 0; k < len; k++)
- d->attrs[d->attrs_used++] = value[k];
- d->attrs[d->attrs_used++] = '\0';
- }
- n = &d->nodes[node];
- n->extra_off = start;
- n->extra_len = (unsigned short)(d->attrs_used - start);
- n->style_valid = 0;
- attr_note_known(d, n, name, value, len);
- return 0;
- }
- int br_remove_attr(struct br_doc *d, unsigned int node, const char *name)
- {
- struct br_node *n;
- unsigned int start;
- const char *p, *oldend;
- int found = 0;
- if (!d || node >= d->nnode || !name)
- return -1;
- n = &d->nodes[node];
- start = d->attrs_used;
- p = d->attrs + n->extra_off;
- oldend = p + n->extra_len;
- if (start + n->extra_len > BR_ATTR_SIZE)
- return -1;
- while (p + 1 < oldend) {
- const char *an = p;
- const char *av;
- unsigned int anl, avl, k;
- anl = text_len(an);
- p += anl + 1;
- if (p >= oldend)
- break;
- av = p;
- avl = text_len(av);
- p += avl + 1;
- if (same_string(an, name)) {
- found = 1;
- continue;
- }
- for (k = 0; k < anl; k++)
- d->attrs[d->attrs_used++] = an[k];
- d->attrs[d->attrs_used++] = '\0';
- for (k = 0; k < avl; k++)
- d->attrs[d->attrs_used++] = av[k];
- d->attrs[d->attrs_used++] = '\0';
- }
- n = &d->nodes[node];
- n->extra_off = start;
- n->extra_len = (unsigned short)(d->attrs_used - start);
- n->style_valid = 0;
- attr_note_known(d, n, name, "", 0);
- return found ? 0 : -1;
- }
- unsigned int br_clone_node(struct br_doc *d, unsigned int node, int deep)
- {
- struct br_node *src;
- unsigned int h;
- const char *name;
- if (!d || node == 0 || node > d->nnode)
- return 0;
- src = &d->nodes[node - 1];
- if (src->type == BR_NODE_TEXT)
- return br_create_text(d, d->text + src->text_off, src->text_len);
- if (src->type == BR_NODE_COMMENT)
- return br_create_comment(d, d->text + src->text_off,
- src->text_len);
- if (src->type == BR_NODE_FRAGMENT)
- h = br_create_fragment(d);
- else {
- if (src->tag == BR_TAG_UNKNOWN && src->name_off)
- name = d->attrs + src->name_off;
- else
- name = br_tag_name(src->tag);
- h = br_create_element(d, name, text_len(name));
- }
- if (!h)
- return 0;
- {
- const char *p = d->attrs + src->extra_off;
- const char *end = p + src->extra_len;
- while (p + 1 < end) {
- const char *an = p;
- const char *av;
- unsigned int anl, avl;
- anl = text_len(an);
- p += anl + 1;
- if (p >= end)
- break;
- av = p;
- avl = text_len(av);
- p += avl + 1;
- br_set_attr(d, h, an, av, avl);
- }
- }
- if (deep) {
- unsigned int c;
- for (c = src->first; c; c = d->nodes[c].next) {
- unsigned int ch = br_clone_node(d, c + 1, 1);
- if (ch)
- br_append_child(d, h - 1, ch);
- }
- }
- return h;
- }
- static int attr_value_match(const char *have, const char *want,
- unsigned int want_len, int op)
- {
- unsigned int n = text_len(have);
- unsigned int i;
- if (!op)
- return have != 0;
- if (!have)
- return 0;
- if (op == '=')
- return n == want_len && same_string_n(have, want, want_len);
- if (op == '^')
- return n >= want_len && same_string_n(have, want, want_len);
- if (op == '$') {
- if (n < want_len)
- return 0;
- have += n - want_len;
- for (i = 0; i < want_len; i++)
- if (have[i] != want[i])
- return 0;
- return 1;
- }
- if (op == '*') {
- for (i = 0; i + want_len <= n; i++)
- if (same_string_n(have + i, want, want_len))
- return 1;
- return 0;
- }
- return 0;
- }
- static int class_contains(const char *classes, const char *want,
- unsigned int want_len)
- {
- unsigned int i = 0;
- if (!classes)
- return 0;
- while (classes[i]) {
- unsigned int start;
- while (classes[i] == ' ' || classes[i] == '\t' ||
- classes[i] == '\r' || classes[i] == '\n')
- i++;
- start = i;
- while (classes[i] && classes[i] != ' ' && classes[i] != '\t' &&
- classes[i] != '\r' && classes[i] != '\n')
- i++;
- if (i - start == want_len &&
- same_string_n(classes + start, want, want_len))
- return 1;
- }
- return 0;
- }
- static int match_compound(struct br_doc *d, unsigned int node,
- const char *sel, unsigned int len)
- {
- struct br_node *n = &d->nodes[node];
- unsigned int i = 0;
- while (i < len && sel[i] != '.' && sel[i] != '#' &&
- sel[i] != '[' && sel[i] != ':')
- i++;
- if (i > 0 && !(i == 1 && sel[0] == '*')) {
- char tag[32];
- unsigned int tl = i < sizeof(tag) - 1 ? i : sizeof(tag) - 1;
- unsigned int k;
- for (k = 0; k < tl; k++)
- tag[k] = sel[k];
- tag[tl] = '\0';
- if (n->tag == BR_TAG_UNKNOWN) {
- if (!n->name_off ||
- !same_string(d->attrs + n->name_off, tag))
- return 0;
- } else if (n->tag != br_tag_from_name(tag)) {
- return 0;
- }
- }
- while (i < len) {
- if (sel[i] == '.') {
- unsigned int start;
- i++;
- start = i;
- while (i < len && sel[i] != '.' && sel[i] != '#' &&
- sel[i] != '[' && sel[i] != ':' && sel[i] != ' ')
- i++;
- if (!class_contains(br_find_attr(d, node, "class"),
- sel + start, i - start))
- return 0;
- } else if (sel[i] == '#') {
- unsigned int start;
- i++;
- start = i;
- while (i < len && sel[i] != '.' && sel[i] != '#' &&
- sel[i] != '[' && sel[i] != ':' && sel[i] != ' ')
- i++;
- {
- const char *id = br_find_attr(d, node, "id");
- if (!id || !same_string_n(id, sel + start,
- i - start) ||
- text_len(id) != i - start)
- return 0;
- }
- } else if (sel[i] == '[') {
- unsigned int start, nl, op = 0, vs, ve;
- char quote = 0;
- i++;
- start = i;
- while (i < len && sel[i] != '=' && sel[i] != ']' &&
- sel[i] != '^' && sel[i] != '$' && sel[i] != '*')
- i++;
- nl = i - start;
- if (i < len && sel[i] != ']') {
- op = sel[i++];
- if (i < len && sel[i] == '=')
- i++;
- }
- while (i < len && sel[i] == ' ')
- i++;
- if (i < len && (sel[i] == '"' || sel[i] == '\'')) {
- quote = sel[i++];
- vs = i;
- while (i < len && sel[i] != quote)
- i++;
- ve = i;
- if (i < len)
- i++;
- } else {
- vs = i;
- while (i < len && sel[i] != ']')
- i++;
- ve = i;
- }
- if (i < len && sel[i] == ']')
- i++;
- {
- char name[32];
- unsigned int k;
- if (nl >= sizeof(name))
- return 0;
- for (k = 0; k < nl; k++)
- name[k] = sel[start + k];
- name[nl] = '\0';
- if (!attr_value_match(br_find_attr(d, node, name),
- sel + vs, ve - vs, op))
- return 0;
- }
- } else if (sel[i] == ':') {
- unsigned int start;
- i++;
- start = i;
- while (i < len && sel[i] != '.' && sel[i] != '#' &&
- sel[i] != '[' && sel[i] != ':' && sel[i] != ' ')
- i++;
- /* Every link is treated as unvisited: there is no
- * history state, so a:link matches and a:visited does
- * not. Other pseudo-classes (hover/focus/active) are
- * deliberately unmatched instead of applying their
- * styles permanently. */
- if (i - start != 4 ||
- !ci_eq_n(sel + start, "link", 4) ||
- n->tag != BR_TAG_A || !n->href_off)
- return 0;
- } else {
- return 0;
- }
- }
- return 1;
- }
- static int match_selector(struct br_doc *d, unsigned int node,
- const char *sel, unsigned int len)
- {
- unsigned int i, split = 0, depth = 0;
- char quote = 0;
- while (len > 0 && sel[0] == ' ') {
- sel++;
- len--;
- }
- while (len > 0 && sel[len - 1] == ' ')
- len--;
- if (len == 0)
- return 0;
- for (i = 0; i < len; i++) {
- char c = sel[i];
- if (quote) {
- if (c == quote)
- quote = 0;
- } else if (c == '"' || c == '\'') {
- quote = c;
- } else if (c == '[') {
- depth++;
- } else if (c == ']' && depth) {
- depth--;
- } else if (!depth && c == ' ') {
- split = i;
- }
- }
- if (split) {
- unsigned int p = d->nodes[node].parent;
- if (!match_compound(d, node, sel + split + 1,
- len - split - 1))
- return 0;
- while (p) {
- unsigned int next = d->nodes[p].parent;
- if (match_selector(d, p, sel, split))
- return 1;
- p = next;
- }
- return 0;
- }
- return match_compound(d, node, sel, len);
- }
- static int query_add(unsigned int *out, unsigned int max,
- unsigned int *count, unsigned int node)
- {
- unsigned int i;
- for (i = 0; i < *count; i++)
- if (out[i] == node + 1)
- return 0;
- if (*count < max)
- out[(*count)++] = node + 1;
- return 0;
- }
- static void query_walk(struct br_doc *d, unsigned int node,
- const char *sel, unsigned int len, unsigned int *out,
- unsigned int max, unsigned int *count)
- {
- unsigned int c;
- if (d->nodes[node].type == BR_NODE_ELEM &&
- match_selector(d, node, sel, len))
- query_add(out, max, count, node);
- for (c = d->nodes[node].first; c; c = d->nodes[c].next)
- query_walk(d, c, sel, len, out, max, count);
- }
- int br_matches(struct br_doc *d, unsigned int node, const char *selector)
- {
- unsigned int len;
- if (!d || !selector || node == 0 || node > d->nnode)
- return 0;
- len = text_len(selector);
- while (len > 0 && selector[0] == ' ') {
- selector++;
- len--;
- }
- while (len > 0 && selector[len - 1] == ' ')
- len--;
- return match_selector(d, node - 1, selector, len);
- }
- unsigned int br_query_all(struct br_doc *d, unsigned int root,
- const char *selector, unsigned int *out,
- unsigned int max)
- {
- unsigned int count = 0, i = 0;
- if (!d || !selector || !out || max == 0)
- return 0;
- while (selector[i]) {
- unsigned int start = i, depth = 0;
- char quote = 0;
- while (selector[i]) {
- char c = selector[i];
- if (quote) {
- if (c == quote)
- quote = 0;
- } else if (c == '"' || c == '\'') {
- quote = c;
- } else if (c == '[') {
- depth++;
- } else if (c == ']' && depth) {
- depth--;
- } else if (!depth && c == ',') {
- break;
- }
- i++;
- }
- if (i > start) {
- if (root == 0) {
- unsigned int n;
- for (n = 1; n < d->nnode; n++)
- if (d->nodes[n].type == BR_NODE_ELEM &&
- !node_detached(d, n) &&
- match_selector(d, n, selector + start,
- i - start))
- query_add(out, max, &count, n);
- } else if (root <= d->nnode) {
- unsigned int c;
- for (c = d->nodes[root - 1].first; c;
- c = d->nodes[c].next)
- query_walk(d, c, selector + start,
- i - start, out, max, &count);
- }
- }
- if (selector[i] == ',')
- i++;
- }
- return count;
- }
- /* ---- attribute parsing ---- */
- static int is_space(char c)
- {
- return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f';
- }
- static int str_eq(const char *a, const char *b);
- static unsigned int parse_uint(const char *s, unsigned int len)
- {
- unsigned int i = 0, v = 0;
- while (i < len && is_space(s[i]))
- i++;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- v = v * 10 + (unsigned int)(s[i] - '0');
- if (v > 4096)
- v = 4096;
- i++;
- }
- return v;
- }
- static int contains_ci_len(const char *s, unsigned int len,
- const char *needle);
- static int parse_svg_viewbox(const char *s, unsigned int len,
- unsigned short *w, unsigned short *h)
- {
- unsigned int i = 0, v[4], n = 0;
- while (i < len && n < 4) {
- unsigned int x = 0;
- int any = 0;
- while (i < len && (is_space(s[i]) || s[i] == ','))
- i++;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- x = x * 10 + (unsigned int)(s[i] - '0');
- i++;
- any = 1;
- }
- /* viewBox values are commonly fractional ("0 0 340.16
- * 340.16"). Consume the fraction so it is not mistaken for
- * the next number; the integer part is enough for the aspect
- * ratio used by the renderer. */
- if (i < len && s[i] == '.') {
- i++;
- while (i < len && s[i] >= '0' && s[i] <= '9')
- i++;
- }
- if (!any) {
- if (i < len)
- i++;
- continue;
- }
- v[n++] = x;
- }
- if (n < 4)
- return 0;
- *w = (unsigned short)(v[2] > 4096 ? 4096 : v[2]);
- *h = (unsigned short)(v[3] > 4096 ? 4096 : v[3]);
- return *w && *h;
- }
- static void note_attr_flag(struct br_node *n, const char *name)
- {
- if (str_eq(name, "data-collapse-target"))
- n->attr_flags |= BR_ATTR_COLLAPSE_TARGET;
- else if (str_eq(name, "data-collapse-trigger"))
- n->attr_flags |= BR_ATTR_COLLAPSE_TRIGGER;
- else if (str_eq(name, "is-open"))
- n->attr_flags |= BR_ATTR_IS_OPEN;
- else if (str_eq(name, "disabled"))
- n->attr_flags |= BR_ATTR_DISABLED;
- else if (str_eq(name, "open"))
- n->attr_flags |= BR_ATTR_OPEN;
- else if (str_eq(name, "inert"))
- n->attr_flags |= BR_ATTR_INERT;
- }
- /* Store a packed name/value list for getAttribute()/selectors. This is a
- * second pass so the block stays contiguous even though the known attributes
- * above also use the shared attr arena. */
- static void parse_attrs_generic(struct br_doc *d, struct br_node *n,
- const char *s, unsigned int len)
- {
- unsigned int i = 0;
- unsigned int start = d->attrs_used;
- while (i < len) {
- char name[32];
- unsigned int nl = 0, vstart, vlen;
- char quote = 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i >= len || s[i] == '/')
- break;
- while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>') {
- char c = s[i++];
- if (c >= 'A' && c <= 'Z')
- c = (char)(c - 'A' + 'a');
- if (nl + 1 < sizeof(name))
- name[nl++] = c;
- }
- name[nl] = '\0';
- while (i < len && is_space(s[i]))
- i++;
- if (i < len && s[i] == '=') {
- i++;
- while (i < len && is_space(s[i]))
- i++;
- if (i < len && (s[i] == '"' || s[i] == '\'')) {
- quote = s[i++];
- vstart = i;
- while (i < len && s[i] != quote)
- i++;
- vlen = i - vstart;
- if (i < len)
- i++;
- } else {
- vstart = i;
- while (i < len && !is_space(s[i]) && s[i] != '>')
- i++;
- vlen = i - vstart;
- }
- } else {
- /* Boolean attribute: expose it with an empty value. */
- vstart = 0;
- vlen = 0;
- }
- if (!nl)
- continue;
- if (d->attrs_used + nl + 1 + vlen + 1 > BR_ATTR_SIZE) {
- d->truncated = 1;
- break;
- }
- {
- unsigned int k;
- for (k = 0; k < nl; k++)
- d->attrs[d->attrs_used++] = name[k];
- d->attrs[d->attrs_used++] = '\0';
- for (k = 0; k < vlen; k++)
- d->attrs[d->attrs_used++] = s[vstart + k];
- d->attrs[d->attrs_used++] = '\0';
- }
- }
- if (d->attrs_used > start) {
- n->extra_off = start;
- n->extra_len = (unsigned short)(d->attrs_used - start);
- }
- }
- /* Parse attributes from `s` (between the tag name and '>'). */
- static void parse_attrs(struct br_doc *d, struct br_node *n,
- const char *s, unsigned int len)
- {
- unsigned int i = 0;
- while (i < len) {
- char name[32];
- unsigned int nl = 0, vstart, vlen;
- char quote = 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i >= len || s[i] == '/')
- break;
- while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>') {
- char c = s[i++];
- if (c >= 'A' && c <= 'Z')
- c = (char)(c - 'A' + 'a');
- if (nl + 1 < sizeof(name))
- name[nl++] = c;
- }
- name[nl] = '\0';
- while (i < len && is_space(s[i]))
- i++;
- if (i >= len || s[i] != '=') {
- if (str_eq(name, "hidden"))
- n->flags |= BR_NODE_HIDDEN;
- note_attr_flag(n, name);
- continue;
- }
- i++;
- while (i < len && is_space(s[i]))
- i++;
- if (i < len && (s[i] == '"' || s[i] == '\'')) {
- quote = s[i++];
- vstart = i;
- while (i < len && s[i] != quote)
- i++;
- vlen = i - vstart;
- if (i < len)
- i++;
- } else {
- vstart = i;
- while (i < len && !is_space(s[i]) && s[i] != '>')
- i++;
- vlen = i - vstart;
- }
- if (!nl)
- continue;
- if (str_eq(name, "hidden")) {
- n->flags |= BR_NODE_HIDDEN;
- continue;
- }
- note_attr_flag(n, name);
- if (str_eq(name, "href"))
- n->href_off = store_attr(d, s + vstart, vlen, 480);
- else if (str_eq(name, "class"))
- n->class_off = store_attr(d, s + vstart, vlen, 120);
- else if (str_eq(name, "id"))
- n->id_off = store_attr(d, s + vstart, vlen, 64);
- else if (str_eq(name, "style"))
- n->style_off = store_attr(d, s + vstart, vlen, 240);
- else if (str_eq(name, "src"))
- n->src_off = store_attr(d, s + vstart, vlen, 400);
- else if (str_eq(name, "alt"))
- n->alt_off = store_attr(d, s + vstart, vlen, 120);
- else if (str_eq(name, "color"))
- n->color_off = store_attr(d, s + vstart, vlen, 32);
- else if (str_eq(name, "rel"))
- n->rel_off = store_attr(d, s + vstart, vlen, 64);
- else if (str_eq(name, "value"))
- n->src_off = store_attr(d, s + vstart, vlen, 220);
- else if (str_eq(name, "placeholder") &&
- (n->tag == BR_TAG_INPUT || n->tag == BR_TAG_TEXTAREA))
- n->alt_off = store_attr(d, s + vstart, vlen, 120);
- else if (str_eq(name, "type") &&
- (n->tag == BR_TAG_INPUT || n->tag == BR_TAG_BUTTON ||
- n->tag == BR_TAG_SELECT || n->tag == BR_TAG_TEXTAREA))
- n->rel_off = store_attr(d, s + vstart, vlen, 40);
- else if (str_eq(name, "width")) {
- n->width_attr = (unsigned short)parse_uint(s + vstart, vlen);
- if (vlen && s[vstart + vlen - 1] == '%')
- n->width_pct = 1;
- } else if (str_eq(name, "height")) {
- n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
- if (vlen && s[vstart + vlen - 1] == '%')
- n->height_pct = 1;
- } else if (str_eq(name, "colspan")) {
- unsigned int v = parse_uint(s + vstart, vlen);
- if (v < 1)
- v = 1;
- if (v > 16)
- v = 16;
- n->colspan = (unsigned char)v;
- } else if (str_eq(name, "rowspan")) {
- unsigned int v = parse_uint(s + vstart, vlen);
- if (v < 1)
- v = 1;
- if (v > 16)
- v = 16;
- n->rowspan = (unsigned char)v;
- } else if (str_eq(name, "x") || str_eq(name, "x1")) {
- n->svg_x = (signed short)parse_uint(s + vstart, vlen);
- } else if (str_eq(name, "y") || str_eq(name, "y1")) {
- n->svg_y = (signed short)parse_uint(s + vstart, vlen);
- } else if (str_eq(name, "x2")) {
- n->width_attr = (unsigned short)parse_uint(s + vstart, vlen);
- } else if (str_eq(name, "y2")) {
- n->height_attr = (unsigned short)parse_uint(s + vstart, vlen);
- } else if (str_eq(name, "rx")) {
- n->svg_rx = (unsigned char)parse_uint(s + vstart, vlen);
- } else if (str_eq(name, "d")) {
- n->src_off = store_attr(d, s + vstart, vlen, 300);
- } else if (str_eq(name, "viewbox")) {
- unsigned short vw = 0, vh = 0;
- if (parse_svg_viewbox(s + vstart, vlen, &vw, &vh)) {
- n->width_attr = vw;
- n->height_attr = vh;
- n->flags |= BR_NODE_VIEWBOX;
- }
- } else if (str_eq(name, "text-anchor")) {
- if (contains_ci_len(s + vstart, vlen, "middle"))
- n->attr_flags |= BR_ATTR_ANCHOR_MIDDLE;
- else if (contains_ci_len(s + vstart, vlen, "end"))
- n->attr_flags |= BR_ATTR_ANCHOR_END;
- }
- }
- parse_attrs_generic(d, n, s, len);
- }
- static int str_eq(const char *a, const char *b)
- {
- unsigned int i = 0;
- while (a[i] && b[i] && a[i] == b[i])
- i++;
- return a[i] == '\0' && b[i] == '\0';
- }
- /* ---- CSS ---- */
- static int parse_color(const char *s, unsigned int len, unsigned short *out);
- int br_parse_color(const char *s, unsigned int len, unsigned short *out)
- {
- return parse_color(s, len, out);
- }
- /* SVG paint value -> BR_PAINT_* (0 when unrecognised). */
- static int paint_mode(const char *s, unsigned int len, unsigned short *c)
- {
- while (len && is_space(*s)) {
- s++;
- len--;
- }
- while (len && is_space(s[len - 1]))
- len--;
- if (!len)
- return 0;
- if (len == 4 && ci_eq_n(s, "none", 4))
- return BR_PAINT_NONE;
- if (len == 12 && ci_eq_n(s, "currentcolor", 12))
- return BR_PAINT_CURRENT;
- if (len > 4 && ci_eq_n(s, "url(", 4))
- return BR_PAINT_URL;
- if (len == 11 && ci_eq_n(s, "transparent", 11))
- return BR_PAINT_NONE;
- if (parse_color(s, len, c))
- return BR_PAINT_COLOR;
- return 0;
- }
- /* A plain SVG length ("1.5", "2px") in 1/16 units; -1 when not a number. */
- static int svg_len16(const char *s, unsigned int len)
- {
- unsigned int i = 0;
- int v = 0, frac = 0, digits = 0;
- while (i < len && is_space(s[i]))
- i++;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- if (v < 4000)
- v = v * 10 + (s[i] - '0');
- i++;
- digits++;
- }
- if (i < len && s[i] == '.') {
- int scale = 1;
- i++;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- if (scale < 10000) {
- frac = frac * 10 + (s[i] - '0');
- scale *= 10;
- }
- i++;
- digits++;
- }
- frac = frac * 16 / scale;
- }
- if (!digits)
- return -1;
- return v * 16 + frac;
- }
- /* Inside an <svg> subtree (including the <svg> element itself). */
- static int in_svg(const struct br_doc *d, unsigned int node)
- {
- unsigned int k;
- for (k = 0; node && node < d->nnode && k < 64; k++) {
- if (d->nodes[node].tag == BR_TAG_SVG)
- return 1;
- node = d->nodes[node].parent;
- }
- return 0;
- }
- enum {
- PROP_COLOR, PROP_BG, PROP_BG_IMAGE, PROP_BG_REPEAT,
- PROP_BG_POS_X_KIND, PROP_BG_POS_X, PROP_BG_POS_Y_KIND, PROP_BG_POS_Y,
- PROP_BG_SIZE_KIND, PROP_BG_SIZE_W, PROP_BG_SIZE_H,
- PROP_FONT_PX, PROP_FONT_UNIT,
- PROP_BOLD, PROP_ITALIC, PROP_MONO, PROP_FONT_FACE,
- PROP_ALIGN, PROP_UNDERLINE, PROP_STRIKE, PROP_OVERLINE,
- PROP_DISPLAY, PROP_MT, PROP_MR, PROP_MB, PROP_ML,
- PROP_PT, PROP_PR, PROP_PB, PROP_PL,
- PROP_MT_U, PROP_MR_U, PROP_MB_U, PROP_ML_U,
- PROP_PT_U, PROP_PR_U, PROP_PB_U, PROP_PL_U,
- PROP_BORDER_W, PROP_BORDER_COLOR,
- PROP_BW_T, PROP_BW_R, PROP_BW_B, PROP_BW_L,
- PROP_BC_T, PROP_BC_R, PROP_BC_B, PROP_BC_L,
- PROP_BS_T, PROP_BS_R, PROP_BS_B, PROP_BS_L,
- PROP_BT_T, PROP_BT_R, PROP_BT_B, PROP_BT_L,
- PROP_RADIUS_TL, PROP_RADIUS_TR, PROP_RADIUS_BR, PROP_RADIUS_BL,
- PROP_LIST_NONE, PROP_LIST_STYLE, PROP_NOWRAP,
- PROP_WIDTH, PROP_WIDTH_KIND, PROP_MAXW, PROP_MAXW_KIND,
- PROP_WIDTH_CALC_PCT, PROP_WIDTH_CALC_PX,
- PROP_MINW, PROP_MINW_KIND, PROP_BOX_SIZING, PROP_ML_AUTO, PROP_MR_AUTO,
- PROP_HEIGHT, PROP_HEIGHT_KIND, PROP_MINH, PROP_MINH_KIND,
- PROP_MAXH, PROP_MAXH_KIND, PROP_LH_PCT, PROP_LH_PX, PROP_UPPER,
- PROP_HEIGHT_CALC_PCT, PROP_HEIGHT_CALC_PX,
- PROP_MINH_CALC_PCT, PROP_MINH_CALC_PX,
- PROP_MAXH_CALC_PCT, PROP_MAXH_CALC_PX,
- PROP_MINW_CALC_PCT, PROP_MINW_CALC_PX,
- PROP_MAXW_CALC_PCT, PROP_MAXW_CALC_PX,
- PROP_MT_CPX, PROP_MR_CPX, PROP_MB_CPX, PROP_ML_CPX,
- PROP_PT_CPX, PROP_PR_CPX, PROP_PB_CPX, PROP_PL_CPX,
- PROP_FLOAT, PROP_CLEAR, PROP_FLEX_GROW, PROP_FLEX_COL, PROP_FLEX_WRAP,
- PROP_JUSTIFY, PROP_ALIGN_ITEMS,
- PROP_GRID_NCOLS, PROP_GRID_GAP, PROP_GRID_SLOT, PROP_GRID_AUTO,
- PROP_POS, PROP_LEFT, PROP_TOP, PROP_LEFT_SET, PROP_TOP_SET,
- PROP_RIGHT, PROP_BOTTOM, PROP_RIGHT_SET, PROP_BOTTOM_SET,
- PROP_ZINDEX, PROP_ZSET,
- PROP_TRANSFORM_X, PROP_TRANSFORM_Y, PROP_TRANSFORM_SET,
- PROP_TRANSFORM_PAINT, PROP_SCALE_X, PROP_SCALE_Y, PROP_ROTATE,
- PROP_ORIGIN_X_KIND, PROP_ORIGIN_X, PROP_ORIGIN_Y_KIND, PROP_ORIGIN_Y,
- PROP_VIS_HIDDEN, PROP_OPACITY_HIDDEN, PROP_OVERFLOW_HIDDEN,
- PROP_FILL_MODE, PROP_FILL, PROP_STROKE_MODE, PROP_STROKE,
- PROP_STROKE_W, PROP_FILL_RULE,
- PROP_COUNT
- };
- struct color_name {
- const char *name;
- unsigned short rgb;
- };
- static const struct color_name color_names[] = {
- { "black", 0x0000 }, { "white", 0xFFFF }, { "red", 0xF800 },
- { "lime", 0x07E0 }, { "green", 0x0400 }, { "blue", 0x001F },
- { "yellow", 0xFFE0 }, { "cyan", 0x07FF }, { "aqua", 0x07FF },
- { "magenta", 0xF81F }, { "fuchsia", 0xF81F }, { "gray", 0x8410 },
- { "grey", 0x8410 }, { "silver", 0xC618 }, { "maroon", 0x8000 },
- { "olive", 0x8400 }, { "navy", 0x0010 }, { "purple", 0x8010 },
- { "teal", 0x0410 }, { "orange", 0xFD20 }, { "brown", 0xA145 },
- { "pink", 0xFE19 }, { "gold", 0xFEA0 }, { "lightgray", 0xD69A },
- { "lightgrey", 0xD69A }, { "darkgray", 0x528A },
- { "darkgrey", 0x528A }, { 0, 0 }
- };
- static int hexval(char c)
- {
- if (c >= '0' && c <= '9')
- return c - '0';
- if (c >= 'a' && c <= 'f')
- return c - 'a' + 10;
- if (c >= 'A' && c <= 'F')
- return c - 'A' + 10;
- return -1;
- }
- static int named_color(const char *s, unsigned int len, unsigned short *out)
- {
- unsigned int i;
- for (i = 0; color_names[i].name; i++) {
- unsigned int j, n = s_len(color_names[i].name);
- if (n != len)
- continue;
- for (j = 0; j < n; j++) {
- char c = s[j];
- if (c >= 'A' && c <= 'Z')
- c = (char)(c - 'A' + 'a');
- if (c != color_names[i].name[j])
- break;
- }
- if (j == n) {
- *out = color_names[i].rgb;
- return 1;
- }
- }
- return 0;
- }
- /* Parse #rgb/#rrggbb/rgb()/rgba()/a named colour. Alpha is composited
- * over white; the render target is a white page. */
- static int parse_color(const char *s, unsigned int len, unsigned short *out)
- {
- unsigned int i = 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i < len && s[i] == '#') {
- int d[6], nd = 0;
- i++;
- while (i < len && nd < 6 && hexval(s[i]) >= 0)
- d[nd++] = hexval(s[i++]);
- if (nd == 3) {
- unsigned int r = (unsigned int)d[0], g = (unsigned int)d[1];
- unsigned int b = (unsigned int)d[2];
- *out = (unsigned short)(((r * 17 >> 3) << 11) |
- ((g * 17 >> 2) << 5) |
- (b * 17 >> 3));
- return 1;
- }
- if (nd >= 6) {
- unsigned int r = (unsigned int)(d[0] * 16 + d[1]);
- unsigned int g = (unsigned int)(d[2] * 16 + d[3]);
- unsigned int b = (unsigned int)(d[4] * 16 + d[5]);
- *out = (unsigned short)(((r >> 3) << 11) |
- ((g >> 2) << 5) | (b >> 3));
- return 1;
- }
- return 0;
- }
- if (i + 3 <= len && ci_eq_n(s + i, "rgb", 3)) {
- unsigned int v[3], k = 0, a = 255;
- while (i < len && s[i] != '(')
- i++;
- if (i >= len)
- return 0;
- i++;
- while (k < 3 && i < len) {
- unsigned int x = 0;
- int percent = 0;
- while (i < len && (is_space(s[i]) || s[i] == ','))
- i++;
- if (i >= len || s[i] < '0' || s[i] > '9')
- return 0;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- x = x * 10 + (unsigned int)(s[i] - '0');
- i++;
- }
- if (i < len && s[i] == '%') {
- percent = 1;
- i++;
- }
- if (percent)
- x = x * 255 / 100;
- if (x > 255)
- x = 255;
- v[k++] = x;
- }
- /* Optional alpha in both syntaxes:
- * rgba(1, 2, 3, 0.5), rgb(1 2 3 / 50%) */
- {
- unsigned int whole = 0, frac = 0, scale = 1;
- int percent = 0;
- while (i < len && (is_space(s[i]) || s[i] == ','))
- i++;
- if (i < len && s[i] == '/') {
- i++;
- while (i < len && is_space(s[i]))
- i++;
- }
- if (i < len && s[i] >= '0' && s[i] <= '9') {
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- whole = whole * 10 +
- (unsigned int)(s[i] - '0');
- i++;
- }
- if (i < len && s[i] == '.') {
- i++;
- while (i < len && s[i] >= '0' &&
- s[i] <= '9') {
- if (scale < 1000) {
- frac = frac * 10 +
- (unsigned int)(s[i] - '0');
- scale *= 10;
- }
- i++;
- }
- }
- while (i < len && is_space(s[i]))
- i++;
- if (i < len && s[i] == '%')
- percent = 1;
- if (percent)
- a = whole > 100 ? 255 :
- whole * 255 / 100;
- else if (whole >= 1)
- a = 255;
- else
- a = frac * 255 / scale;
- }
- }
- if (k == 3) {
- unsigned int r = v[0], g = v[1], b = v[2];
- if (a < 255) {
- r = (r * a + 255u * (255u - a)) / 255u;
- g = (g * a + 255u * (255u - a)) / 255u;
- b = (b * a + 255u * (255u - a)) / 255u;
- }
- *out = (unsigned short)(((r >> 3) << 11) |
- ((g >> 2) << 5) | (b >> 3));
- return 1;
- }
- return 0;
- }
- {
- unsigned int j = i;
- while (i < len && !is_space(s[i]))
- i++;
- return named_color(s + j, i - j, out);
- }
- }
- /* Fully transparent colours have no RGB565 representation. They are common
- * as pre-allocated borders ("border:.1rem solid #0000") that become visible
- * only on hover; painting them black would draw an outline around every
- * pill and card. */
- static int color_is_transparent(const char *s, unsigned int len)
- {
- unsigned int i = 0, n = 0;
- int last = 0, prev = 0, d;
- while (i < len && is_space(s[i]))
- i++;
- if (i + 11 <= len && ci_eq_n(s + i, "transparent", 11))
- return 1;
- if (i >= len || s[i] != '#')
- return 0;
- i++;
- while (i < len && hexval(s[i]) >= 0) {
- d = hexval(s[i]);
- prev = last;
- last = d;
- n++;
- i++;
- }
- while (i < len && is_space(s[i]))
- i++;
- if (i != len)
- return 0;
- if (n == 4)
- return last == 0;
- if (n == 8)
- return prev == 0 && last == 0;
- return 0;
- }
- static int register_bg_url(const char *s, unsigned int len)
- {
- unsigned int i, j, n;
- while (len && is_space(*s)) {
- s++;
- len--;
- }
- while (len && is_space(s[len - 1]))
- len--;
- if (len >= 2 && (s[0] == '"' || s[0] == '\'') &&
- s[len - 1] == s[0]) {
- s++;
- len -= 2;
- }
- if (len > 191)
- len = 191;
- for (i = 0; i < bg_url_count; i++) {
- for (j = 0; j < len && bg_urls[i][j] == s[j]; j++)
- ;
- if (j == len && bg_urls[i][j] == '\0')
- return (int)i + 1;
- }
- if (bg_url_count >= BR_BG_URLS || len == 0)
- return 0;
- n = len < 191 ? len : 191;
- for (i = 0; i < n; i++)
- bg_urls[bg_url_count][i] = s[i];
- bg_urls[bg_url_count][n] = '\0';
- return (int)(++bg_url_count);
- }
- /* Length values for margins/padding: px, ignore percentages/units. */
- static unsigned int viewport_w;
- static int parse_len(const char *s, unsigned int len, unsigned int *pos)
- {
- unsigned int i = *pos;
- int sign = 1;
- long v = 0; /* hundredths of a unit before conversion */
- int digits = 0, frac = 0, unit = 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i < len && (s[i] == '-' || s[i] == '+')) {
- if (s[i] == '-')
- sign = -1;
- i++;
- }
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- v = v * 10 + (s[i] - '0');
- digits = 1;
- i++;
- }
- if (i < len && s[i] == '.') {
- i++;
- /* Minified CSS writes ".5rem" without a leading zero. */
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- if (frac < 2) {
- v = v * 10 + (s[i] - '0');
- frac++;
- digits = 1;
- }
- i++;
- }
- }
- while (frac < 2) {
- v *= 10;
- frac++;
- }
- while (frac > 2) {
- v /= 10;
- frac--;
- }
- if (i < len && s[i] == '%')
- unit = 2;
- else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
- s[i + 2] == 'm')
- unit = 1;
- else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
- unit = 3;
- while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
- s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
- s[i] == 'a' || s[i] == 'd'))
- i++;
- *pos = i;
- if (!digits)
- return 0;
- if (unit == 1)
- v *= root_font_px;
- else if (unit == 3)
- v *= root_font_px;
- else if (unit == 2)
- v = v * (long)viewport_w / 100;
- v /= 100;
- v *= sign;
- if (v < -2048)
- v = -2048;
- if (v > 2048)
- v = 2048;
- return (int)v;
- }
- /* Margin values may be the CSS keyword "auto". parse_len() deliberately
- * treats it as a zero-length token; margins need to keep it so block layout
- * can centre a box with a definite width. */
- #define CSS_LEN_AUTO (-32768)
- /* A box length with its unit preserved for layout-time resolution:
- * 0 px, 1 %, 2 em, 3 rem, 4 auto. The raw value is hundredths for %/em/
- * rem and whole pixels for px. */
- static int parse_css_len(const char *s, unsigned int len,
- unsigned int *pos, int *unit)
- {
- unsigned int i = *pos;
- int sign = 1;
- long v = 0;
- int digits = 0, frac = 0;
- *unit = 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i < len && (s[i] == '-' || s[i] == '+')) {
- if (s[i] == '-')
- sign = -1;
- i++;
- }
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- v = v * 10 + (s[i] - '0');
- digits = 1;
- i++;
- }
- if (i < len && s[i] == '.') {
- i++;
- /* Leading-dot fractions are common in minified CSS
- * (".5rem", ".3rem"). */
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- if (frac < 2) {
- v = v * 10 + (s[i] - '0');
- frac++;
- digits = 1;
- }
- i++;
- }
- }
- while (frac < 2) {
- v *= 10;
- frac++;
- }
- if (!digits) {
- *pos = i;
- return 0;
- }
- if (i < len && s[i] == '%') {
- *unit = 1;
- } else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
- s[i + 2] == 'm') {
- *unit = 3;
- i += 3;
- } else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm') {
- *unit = 2;
- i += 2;
- } else {
- v /= 100;
- }
- while (i < len && (s[i] == 'p' || s[i] == 'x' || s[i] == '%' ||
- s[i] == 'e' || s[i] == 'm' || s[i] == 'r' ||
- s[i] == 'a' || s[i] == 'd'))
- i++;
- *pos = i;
- v *= sign;
- if (v < -32768)
- v = -32768;
- if (v > 32767)
- v = 32767;
- return (int)v;
- }
- static int parse_margin_len(const char *s, unsigned int len,
- unsigned int *pos, int *unit)
- {
- unsigned int i = *pos;
- while (i < len && is_space(s[i]))
- i++;
- if (i + 4 <= len && ci_eq_n(s + i, "auto", 4) &&
- (i + 4 == len || is_space(s[i + 4]) || s[i + 4] == ';')) {
- *pos = i + 4;
- *unit = 4;
- return CSS_LEN_AUTO;
- }
- return parse_css_len(s, len, pos, unit);
- }
- static int parse_calc(const char *s, unsigned int len, int *pct, int *px);
- /* calc(P% +/- Npx) for a margin/padding token; *pos advances past ')'. */
- static int parse_margin_calc(const char *s, unsigned int len,
- unsigned int *pos, int *pct, int *px)
- {
- unsigned int i = *pos, j;
- if (i + 5 > len || !ci_eq_n(s + i, "calc(", 5))
- return 0;
- i += 5;
- j = i;
- while (j < len && s[j] != ')')
- j++;
- if (j >= len)
- return 0;
- if (!parse_calc(s + i, j - i, pct, px))
- return 0;
- *pos = j + 1;
- return 1;
- }
- /* calc(P% +/- Npx) for the common width/margin forms. */
- static int parse_calc(const char *s, unsigned int len, int *pct, int *px)
- {
- unsigned int i = 0;
- int sign = 1;
- int have = 0;
- *pct = 0;
- *px = 0;
- while (i < len) {
- unsigned int j;
- long whole = 0, frac = 0, scale = 1;
- int digits = 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i >= len)
- break;
- if (s[i] == '+' || s[i] == '-') {
- sign = s[i] == '-' ? -1 : 1;
- i++;
- continue;
- }
- if (s[i] < '0' || s[i] > '9') {
- if (s[i] == '.') {
- /* fall through to the number parser */
- } else {
- i++;
- continue;
- }
- }
- j = i;
- while (j < len && s[j] >= '0' && s[j] <= '9') {
- whole = whole * 10 + (s[j] - '0');
- digits = 1;
- j++;
- }
- if (j < len && s[j] == '.') {
- j++;
- while (j < len && s[j] >= '0' && s[j] <= '9') {
- frac = frac * 10 + (s[j] - '0');
- scale *= 10;
- j++;
- }
- }
- if (!digits) {
- i = j + 1;
- continue;
- }
- while (j < len && is_space(s[j]))
- j++;
- if (j < len && s[j] == '%') {
- *pct = sign * (int)(whole + (scale > 1 ?
- frac * 100 / scale : 0));
- have = 1;
- j++;
- } else if (j + 1 < len && s[j] == 'p' && s[j + 1] == 'x') {
- *px = sign * (int)whole;
- have = 1;
- j += 2;
- }
- i = j;
- while (i < len && !is_space(s[i]))
- i++;
- sign = 1;
- }
- return have;
- }
- /* width/height: kind 0 = auto/none, 1 = px, 2 = percent. */
- static int parse_size(const char *s, unsigned int len, unsigned short *val,
- unsigned char *kind)
- {
- unsigned int i = 0;
- unsigned long v = 0;
- int digits = 0, frac = 0, pct = 0, rem = 0, vw = 0, bad_unit = 0;
- *val = 0;
- *kind = 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i + 4 <= len && ci_eq_n(s + i, "auto", 4))
- return 0;
- if (i + 4 <= len && ci_eq_n(s + i, "none", 4))
- return 0;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- v = v * 10 + (unsigned int)(s[i] - '0');
- digits = 1;
- i++;
- }
- if (i < len && s[i] == '.') {
- i++;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- if (frac < 2) {
- v = v * 10 + (unsigned int)(s[i] - '0');
- frac++;
- digits = 1;
- }
- i++;
- }
- }
- while (frac < 2) {
- v *= 10;
- frac++;
- }
- while (frac > 2) {
- v /= 10;
- frac--;
- }
- if (!digits)
- return 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i < len && s[i] == '%')
- pct = 1;
- else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
- s[i + 2] == 'm')
- rem = 1;
- else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm')
- rem = 2;
- else if (i + 1 < len && s[i] == 'v' && s[i + 1] == 'w')
- vw = 1;
- else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x')
- ; /* px: already the default scale */
- else if (i < len && (is_ident(s[i]) || s[i] == '#'))
- bad_unit = 1;
- if (bad_unit)
- return 0;
- if (rem == 1 || rem == 2) {
- if (v > 65535)
- v = 65535;
- *val = (unsigned short)v;
- *kind = rem == 1 ? 5 : 4;
- return 1;
- }
- v /= 100;
- if (vw)
- v = v * viewport_w / 100;
- if (pct) {
- if (v > 100)
- v = 100;
- *kind = 2;
- } else {
- if (v > 2048)
- v = 2048;
- *kind = 1;
- }
- *val = (unsigned short)v;
- return 1;
- }
- static int parse_font_px(const char *s, unsigned int len)
- {
- unsigned int i = 0;
- int whole = 0, frac = 0, fscale = 1, digits = 0;
- int v1000, px;
- int is_px = 1, is_rem = 0, is_pct = 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i + 5 <= len && ci_eq_n(s + i, "large", 5))
- return 24;
- if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7))
- return 32;
- if (i + 5 <= len && ci_eq_n(s + i, "small", 5))
- return 14;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- whole = whole * 10 + (s[i] - '0');
- digits = 1;
- i++;
- }
- if (i < len && s[i] == '.') {
- i++;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- if (fscale < 1000) {
- frac = frac * 10 + (s[i] - '0');
- fscale *= 10;
- }
- digits = 1;
- i++;
- }
- }
- if (!digits)
- return 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
- is_rem = 1;
- is_px = 0;
- } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
- is_rem = 1;
- is_px = 0;
- } else if (i < len && s[i] == '%') {
- is_pct = 1;
- is_px = 0;
- }
- v1000 = whole * 1000 + frac * 1000 / fscale;
- if (is_rem)
- px = v1000 * root_font_px / 1000;
- else if (is_pct)
- px = v1000 * root_font_px / 100000;
- else if (is_px)
- px = v1000 / 1000;
- else
- px = v1000 / 1000;
- if (px < 8)
- px = 8;
- if (px > 72)
- px = 72;
- return px;
- }
- /* font-size keeping the unit so em/% resolve against the parent face. */
- static int parse_font_size(const char *s, unsigned int len,
- unsigned short *raw, unsigned char *unit)
- {
- unsigned int i = 0;
- int whole = 0, frac = 0, scale = 1, digits = 0;
- int v100;
- *raw = 0;
- *unit = 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i + 5 <= len && ci_eq_n(s + i, "large", 5)) {
- *raw = 24;
- *unit = 1;
- return 1;
- }
- if (i + 7 <= len && ci_eq_n(s + i, "x-large", 7)) {
- *raw = 32;
- *unit = 1;
- return 1;
- }
- if (i + 5 <= len && ci_eq_n(s + i, "small", 5)) {
- *raw = 14;
- *unit = 1;
- return 1;
- }
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- whole = whole * 10 + (s[i] - '0');
- digits = 1;
- i++;
- }
- if (i < len && s[i] == '.') {
- i++;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- frac = frac * 10 + (s[i] - '0');
- scale *= 10;
- i++;
- }
- }
- if (!digits && !frac)
- return 0;
- v100 = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
- while (i < len && is_space(s[i]))
- i++;
- if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
- *raw = (unsigned short)v100;
- *unit = 3;
- } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
- *raw = (unsigned short)v100;
- *unit = 2;
- } else if (i < len && s[i] == '%') {
- /* Keep hundredths: 62.5% is a common root size and truncating
- * it to 62% makes every rem 1px too small. */
- *raw = (unsigned short)v100;
- *unit = 4;
- } else {
- *raw = (unsigned short)(v100 / 100);
- *unit = 1;
- }
- return 1;
- }
- static int parse_line_height(const char *s, unsigned int len,
- unsigned short *px, unsigned char *pct)
- {
- unsigned int i = 0;
- int whole = 0, frac = 0, fscale = 1, digits = 0;
- int v1000;
- int is_px = 0, is_pct = 0;
- *px = 0;
- *pct = 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i + 6 <= len && ci_eq_n(s + i, "normal", 6))
- return 0;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- whole = whole * 10 + (s[i] - '0');
- digits = 1;
- i++;
- }
- if (i < len && s[i] == '.') {
- i++;
- while (i < len && s[i] >= '0' && s[i] <= '9' && fscale < 1000) {
- frac = frac * 10 + (s[i] - '0');
- fscale *= 10;
- digits = 1;
- i++;
- }
- }
- if (!digits)
- return 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
- is_px = 1;
- else if (i < len && s[i] == '%')
- is_pct = 1;
- v1000 = whole * 1000 + frac * 1000 / fscale;
- /* rem is an absolute length (the root font is known by the time
- * rules are parsed, as for font-size); em behaves like a multiplier. */
- if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
- v1000 = v1000 * root_font_px;
- is_px = 1;
- }
- if (is_px) {
- int v = v1000 / 1000;
- if (v < 8)
- v = 8;
- if (v > 160)
- v = 160;
- *px = (unsigned short)v;
- } else {
- int v = v1000 / 10; /* unitless multiplier -> percent */
- if (is_pct)
- v = v1000 / 1000;
- if (v < 50)
- v = 50;
- if (v > 255) /* stored in an unsigned char */
- v = 255;
- *pct = (unsigned char)v;
- }
- return 1;
- }
- static int contains_ci_len(const char *s, unsigned int len, const char *needle)
- {
- unsigned int i, j, n = s_len(needle);
- for (i = 0; i + n <= len; i++) {
- for (j = 0; j < n; j++) {
- char a = s[i + j];
- if (a >= 'A' && a <= 'Z')
- a = (char)(a - 'A' + 'a');
- if (a != needle[j])
- break;
- }
- if (j == n)
- return 1;
- }
- return 0;
- }
- /* Pull a colour out of a border/background shorthand. */
- /* Background shorthands can hold several layers before the final solid
- * colour. The colour is conventionally the last token (e.g. a stack of
- * gradients followed by var(--bg)), so take the rightmost colour rather
- * than the first gradient stop. */
- static int find_color_last(const char *s, unsigned int len, unsigned short *c)
- {
- int last = -1;
- unsigned int i;
- for (i = 0; i < len; i++) {
- if (s[i] == '#')
- last = (int)i;
- else if ((s[i] == 'r' || s[i] == 'R') &&
- i + 3 <= len && ci_eq_n(s + i, "rgb", 3))
- last = (int)i;
- }
- if (last >= 0)
- return parse_color(s + last, len - (unsigned int)last, c);
- while (i > 0) {
- unsigned int st, e;
- while (i > 0 && (is_space(s[i - 1]) || s[i - 1] == ',' ||
- s[i - 1] == ')' || s[i - 1] == '('))
- i--;
- e = i;
- while (i > 0 && !is_space(s[i - 1]) && s[i - 1] != ',' &&
- s[i - 1] != ')' && s[i - 1] != '(')
- i--;
- st = i;
- if (e > st && parse_color(s + st, e - st, c))
- return 1;
- }
- return 0;
- }
- static int parse_display(const char *s, unsigned int len)
- {
- unsigned int i;
- while (len && is_space(s[0])) { s++; len--; }
- for (i = 0; i < len && !is_space(s[i]) && s[i] != ';'; i++)
- ;
- if (i == 4 && ci_eq_n(s, "none", 4))
- return BR_DISPLAY_NONE;
- if (i == 5 && ci_eq_n(s, "block", 5))
- return BR_DISPLAY_BLOCK;
- if (i == 6 && ci_eq_n(s, "inline", 6))
- return BR_DISPLAY_INLINE;
- if (i == 12 && ci_eq_n(s, "inline-block", 12))
- return BR_DISPLAY_INLINE_BLOCK;
- if (i == 10 && ci_eq_n(s, "list-item", 10))
- return BR_DISPLAY_BLOCK;
- if (i == 4 && ci_eq_n(s, "flex", 4))
- return BR_DISPLAY_FLEX;
- if (i == 11 && ci_eq_n(s, "inline-flex", 11))
- return BR_DISPLAY_FLEX | BR_DISPLAY_INLINE_FLAG;
- /* Legacy -webkit-box is the line-clamp idiom; render it as a normal
- * block instead of an inline box. */
- if (i == 11 && ci_eq_n(s, "-webkit-box", 11))
- return BR_DISPLAY_BLOCK;
- if (i == 8 && ci_eq_n(s, "-moz-box", 8))
- return BR_DISPLAY_BLOCK;
- if (i == 11 && ci_eq_n(s, "-ms-flexbox", 11))
- return BR_DISPLAY_FLEX;
- if (i == 5 && ci_eq_n(s, "table", 5))
- return BR_DISPLAY_BLOCK;
- if (i == 10 && ci_eq_n(s, "table-cell", 10))
- return BR_DISPLAY_BLOCK;
- /* Grid shells are common on news/wiki pages, but the two-column
- * heuristic is not reliable for their nested area templates yet.
- * Collapse them to block flow so the content stays readable. */
- if (i == 4 && ci_eq_n(s, "grid", 4))
- return BR_DISPLAY_BLOCK;
- if (i == 11 && ci_eq_n(s, "inline-grid", 11))
- return BR_DISPLAY_BLOCK;
- return BR_DISPLAY_INLINE;
- }
- static void decl_add(struct br_decl *out, unsigned int *nout,
- unsigned char prop, int val, int important)
- {
- if (*nout >= BR_DECLS)
- return;
- out[*nout].prop = prop;
- out[*nout].val = (short)val;
- out[*nout].important = (unsigned char)(important != 0);
- (*nout)++;
- }
- /* One whitespace-separated token. Returns 0 at the end of the value. */
- static int next_token(const char *s, unsigned int len, unsigned int *pos,
- unsigned int *start, unsigned int *end)
- {
- unsigned int i = *pos;
- while (i < len && is_space(s[i]))
- i++;
- if (i >= len)
- return 0;
- *start = i;
- while (i < len && !is_space(s[i]))
- i++;
- *end = i;
- *pos = i;
- return 1;
- }
- static int bg_number(const char *s, unsigned int len, signed short *val,
- unsigned char *kind)
- {
- unsigned int i = 0;
- long v = 0;
- int neg = 0, digits = 0, pct = 0;
- if (i < len && (s[i] == '-' || s[i] == '+')) {
- neg = s[i] == '-';
- i++;
- }
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- v = v * 10 + (s[i] - '0');
- digits = 1;
- i++;
- }
- if (i < len && s[i] == '.') {
- i++;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- digits = 1;
- i++;
- }
- }
- if (!digits)
- return 0;
- if (i < len && s[i] == '%') {
- pct = 1;
- i++;
- } else if (i + 2 <= len && ci_eq_n(s + i, "px", 2)) {
- i += 2;
- }
- if (i != len)
- return 0;
- if (neg)
- v = -v;
- if (v < -4096)
- v = -4096;
- if (v > 4096)
- v = 4096;
- *val = (signed short)v;
- *kind = (unsigned char)(pct ? 1 : 0);
- return 1;
- }
- static int bg_keyword(const char *s, unsigned int len, signed short *val,
- int *vertical)
- {
- *vertical = 0;
- if (len == 4 && ci_eq_n(s, "left", 4)) {
- *val = 0;
- return 1;
- }
- if (len == 5 && ci_eq_n(s, "right", 5)) {
- *val = 100;
- return 1;
- }
- if (len == 3 && ci_eq_n(s, "top", 3)) {
- *val = 0;
- *vertical = 1;
- return 1;
- }
- if (len == 6 && ci_eq_n(s, "bottom", 6)) {
- *val = 100;
- *vertical = 1;
- return 1;
- }
- if (len == 6 && ci_eq_n(s, "center", 6)) {
- *val = 50;
- return 1;
- }
- return 0;
- }
- static void add_bg_position(struct br_decl *out, unsigned int *nout,
- const char *s, unsigned int len, int important)
- {
- unsigned int pos = 0, ts, te;
- signed short val[2] = { 50, 50 };
- unsigned char kind[2] = { 1, 1 };
- int vert[2] = { 0, 0 }, n = 0;
- while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
- signed short v;
- int is_vert;
- if (bg_keyword(s + ts, te - ts, &v, &is_vert)) {
- val[n] = v;
- kind[n] = 1;
- vert[n] = is_vert;
- } else if (bg_number(s + ts, te - ts, &v, &kind[n])) {
- val[n] = v;
- vert[n] = 0;
- } else {
- n++;
- continue;
- }
- n++;
- }
- if (n == 1) {
- if (vert[0]) {
- decl_add(out, nout, PROP_BG_POS_X_KIND, 1, important);
- decl_add(out, nout, PROP_BG_POS_X, 50, important);
- decl_add(out, nout, PROP_BG_POS_Y_KIND, kind[0],
- important);
- decl_add(out, nout, PROP_BG_POS_Y, val[0], important);
- } else {
- decl_add(out, nout, PROP_BG_POS_X_KIND, kind[0],
- important);
- decl_add(out, nout, PROP_BG_POS_X, val[0], important);
- decl_add(out, nout, PROP_BG_POS_Y_KIND, 1, important);
- decl_add(out, nout, PROP_BG_POS_Y, 50, important);
- }
- return;
- }
- if (n >= 2) {
- int x = vert[0] && !vert[1] ? 1 : 0;
- int y = 1 - x;
- decl_add(out, nout, PROP_BG_POS_X_KIND, kind[x], important);
- decl_add(out, nout, PROP_BG_POS_X, val[x], important);
- decl_add(out, nout, PROP_BG_POS_Y_KIND, kind[y], important);
- decl_add(out, nout, PROP_BG_POS_Y, val[y], important);
- }
- }
- static void add_bg_size(struct br_decl *out, unsigned int *nout,
- const char *s, unsigned int len, int important)
- {
- unsigned int pos = 0, ts, te;
- signed short w = 0, h = 0;
- unsigned char kw = 0, kh = 0;
- int n = 0;
- while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
- if (n == 0 && te - ts == 5 && ci_eq_n(s + ts, "cover", 5)) {
- decl_add(out, nout, PROP_BG_SIZE_KIND, 3, important);
- return;
- }
- if (n == 0 && te - ts == 7 &&
- ci_eq_n(s + ts, "contain", 7)) {
- decl_add(out, nout, PROP_BG_SIZE_KIND, 4, important);
- return;
- }
- if (te - ts == 4 && ci_eq_n(s + ts, "auto", 4)) {
- n++;
- continue;
- }
- {
- signed short v;
- unsigned char k;
- if (bg_number(s + ts, te - ts, &v, &k)) {
- if (n == 0) {
- w = v;
- kw = k;
- } else {
- h = v;
- kh = k;
- }
- }
- }
- n++;
- }
- decl_add(out, nout, PROP_BG_SIZE_KIND, kw ? 2 : (n ? 1 : 0), important);
- decl_add(out, nout, PROP_BG_SIZE_W, w, important);
- decl_add(out, nout, PROP_BG_SIZE_H, h, important);
- (void)kh;
- }
- /* Parse the `position / size` part of the background shorthand. The image
- * URL may contain slashes, so only a slash outside url(...)/quotes counts. */
- static void add_bg_shorthand_extra(struct br_decl *out, unsigned int *nout,
- const char *v, unsigned int len,
- int important)
- {
- unsigned int i, slash = 0, depth = 0, p, first, count;
- char q = 0;
- signed short nv;
- int vert;
- unsigned char kind;
- for (i = 0; i < len; i++) {
- char c = v[i];
- if (q) {
- if (c == q)
- q = 0;
- continue;
- }
- if (c == '\'' || c == '"') {
- q = c;
- continue;
- }
- if (c == '(') {
- depth++;
- continue;
- }
- if (c == ')') {
- if (depth)
- depth--;
- continue;
- }
- if (c == '/' && !depth) {
- slash = i;
- break;
- }
- }
- if (!slash)
- return;
- /* Up to two position tokens immediately before the slash. */
- p = slash;
- first = slash;
- count = 0;
- while (count < 2) {
- unsigned int e;
- while (p > 0 && is_space(v[p - 1]))
- p--;
- e = p;
- while (p > 0 && !is_space(v[p - 1]))
- p--;
- if (e == p)
- break;
- if (bg_keyword(v + p, e - p, &nv, &vert) ||
- bg_number(v + p, e - p, &nv, &kind)) {
- first = p;
- count++;
- } else {
- break;
- }
- }
- if (count)
- add_bg_position(out, nout, v + first, slash - first,
- important);
- /* Up to two size tokens after the slash. */
- p = slash + 1;
- while (p < len && is_space(v[p]))
- p++;
- first = p;
- count = 0;
- while (count < 2) {
- unsigned int ts = p;
- while (p < len && !is_space(v[p]))
- p++;
- if (p == ts)
- break;
- if ((ts + 5 <= p && ci_eq_n(v + ts, "cover", 5)) ||
- (ts + 7 <= p && ci_eq_n(v + ts, "contain", 7)) ||
- (ts + 4 <= p && ci_eq_n(v + ts, "auto", 4)) ||
- bg_number(v + ts, p - ts, &nv, &kind)) {
- count++;
- while (p < len && is_space(v[p]))
- p++;
- } else {
- break;
- }
- }
- if (count)
- add_bg_size(out, nout, v + first, p - first, important);
- }
- static int parse_fixed_100(const char *s, unsigned int len,
- unsigned int *pos, signed short *out)
- {
- unsigned int i = *pos;
- long whole = 0, frac = 0;
- int neg = 0, digits = 0, fd = 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i < len && (s[i] == '-' || s[i] == '+')) {
- neg = s[i] == '-';
- i++;
- }
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- whole = whole * 10 + (s[i] - '0');
- if (whole > 327)
- whole = 327;
- digits = 1;
- i++;
- }
- if (i < len && s[i] == '.') {
- i++;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- if (fd < 2) {
- frac = frac * 10 + (s[i] - '0');
- fd++;
- digits = 1;
- }
- i++;
- }
- }
- if (!digits)
- return 0;
- while (fd++ < 2)
- frac *= 10;
- *out = (signed short)((whole * 100 + frac) * (neg ? -1 : 1));
- *pos = i;
- return 1;
- }
- static int find_fn(const char *v, unsigned int len, const char *name,
- unsigned int *arg)
- {
- unsigned int n = s_len(name), i;
- for (i = 0; i + n <= len; i++) {
- unsigned int j;
- if (!ci_eq_n(v + i, name, n))
- continue;
- j = i + n;
- while (j < len && is_space(v[j]))
- j++;
- if (j < len && v[j] == '(') {
- *arg = j + 1;
- return 1;
- }
- }
- return 0;
- }
- static void add_transform_origin(struct br_decl *out, unsigned int *nout,
- const char *s, unsigned int len,
- int important)
- {
- unsigned int pos = 0, ts, te;
- signed short val[2] = { 50, 50 };
- unsigned char kind[2] = { 1, 1 };
- int vert[2] = { 0, 0 }, n = 0;
- while (n < 2 && next_token(s, len, &pos, &ts, &te)) {
- signed short v;
- int is_vert;
- if (bg_keyword(s + ts, te - ts, &v, &is_vert)) {
- val[n] = v;
- kind[n] = 1;
- vert[n] = is_vert;
- } else if (bg_number(s + ts, te - ts, &v, &kind[n])) {
- val[n] = v;
- vert[n] = 0;
- } else {
- n++;
- continue;
- }
- n++;
- }
- if (n == 1) {
- if (vert[0]) {
- decl_add(out, nout, PROP_ORIGIN_X_KIND, 1, important);
- decl_add(out, nout, PROP_ORIGIN_X, 50, important);
- decl_add(out, nout, PROP_ORIGIN_Y_KIND, kind[0],
- important);
- decl_add(out, nout, PROP_ORIGIN_Y, val[0], important);
- } else {
- decl_add(out, nout, PROP_ORIGIN_X_KIND, kind[0],
- important);
- decl_add(out, nout, PROP_ORIGIN_X, val[0], important);
- decl_add(out, nout, PROP_ORIGIN_Y_KIND, 1, important);
- decl_add(out, nout, PROP_ORIGIN_Y, 50, important);
- }
- return;
- }
- if (n >= 2) {
- int x = vert[0] && !vert[1] ? 1 : 0;
- int y = 1 - x;
- decl_add(out, nout, PROP_ORIGIN_X_KIND, kind[x], important);
- decl_add(out, nout, PROP_ORIGIN_X, val[x], important);
- decl_add(out, nout, PROP_ORIGIN_Y_KIND, kind[y], important);
- decl_add(out, nout, PROP_ORIGIN_Y, val[y], important);
- }
- }
- static void add_bg_image_decl(struct br_decl *out, unsigned int *nout,
- const char *v, unsigned int vl, int important)
- {
- unsigned int i;
- for (i = 0; i + 4 <= vl; i++) {
- if (ci_eq_n(v + i, "url(", 4)) {
- unsigned int st = i + 4, en = st;
- int idx;
- while (en < vl && v[en] != ')')
- en++;
- idx = register_bg_url(v + st, en - st);
- if (idx)
- decl_add(out, nout, PROP_BG_IMAGE, idx,
- important);
- return;
- }
- }
- }
- static int parse_len_token(const char *s, unsigned int len, int *out)
- {
- unsigned int pos = 0;
- int v;
- if (!len)
- return 0;
- if (!(s[0] >= '0' && s[0] <= '9') && s[0] != '.' && s[0] != '-')
- return 0;
- v = parse_len(s, len, &pos);
- if (pos == 0)
- return 0;
- *out = v;
- return 1;
- }
- static void expand_four(int v[4], int n)
- {
- if (n == 1) {
- v[1] = v[2] = v[3] = v[0];
- } else if (n == 2) {
- v[2] = v[0];
- v[3] = v[1];
- } else if (n == 3) {
- v[3] = v[1];
- }
- }
- /* Expand var(--name[, fallback]) references. Returns the expanded length;
- * the input is copied verbatim when it contains no var(). */
- static unsigned int css_expand_vars(struct br_doc *d, const char *s,
- unsigned int len, char *out,
- unsigned int cap, int depth)
- {
- unsigned int i = 0, n = 0;
- if (depth > 8)
- return 0;
- while (i < len && n + 1 < cap) {
- unsigned int p, inner, close, comma, nlen, fstart;
- char quote;
- int parens;
- if (!(i + 4 <= len && ci_eq_n(s + i, "var(", 4))) {
- out[n++] = s[i++];
- continue;
- }
- p = i + 4;
- quote = 0;
- parens = 1;
- while (p < len && parens) {
- char c = s[p];
- if (quote) {
- if (c == quote)
- quote = 0;
- else if (c == '\\' && p + 1 < len)
- p++;
- } else if (c == '"' || c == '\'') {
- quote = c;
- } else if (c == '(') {
- parens++;
- } else if (c == ')') {
- parens--;
- }
- p++;
- }
- if (parens) {
- out[n++] = s[i++];
- continue;
- }
- close = p - 1;
- inner = i + 4;
- while (inner < close && is_space(s[inner]))
- inner++;
- comma = inner;
- parens = 0;
- quote = 0;
- while (comma < close) {
- char c = s[comma];
- if (quote) {
- if (c == quote)
- quote = 0;
- } else if (c == '"' || c == '\'') {
- quote = c;
- } else if (c == '(') {
- parens++;
- } else if (c == ')' && parens > 0) {
- parens--;
- } else if (c == ',' && parens == 0) {
- break;
- }
- comma++;
- }
- nlen = comma - inner;
- while (nlen && is_space(s[inner + nlen - 1]))
- nlen--;
- fstart = comma < close ? comma + 1 : close;
- while (fstart < close && is_space(s[fstart]))
- fstart++;
- if (nlen >= 3 && s[inner] == '-' && s[inner + 1] == '-') {
- unsigned int vi = css_var_find(d, s + inner, nlen);
- if (vi < BR_MAX_VARS) {
- const char *v = attr_ptr(d, d->var_val[vi]);
- unsigned int vn = css_expand_vars(d, v, s_len(v),
- out + n,
- cap - n, depth + 1);
- n += vn;
- } else if (fstart < close) {
- unsigned int vn = css_expand_vars(d, s + fstart,
- close - fstart,
- out + n, cap - n,
- depth + 1);
- n += vn;
- }
- } else if (fstart < close) {
- unsigned int vn = css_expand_vars(d, s + fstart,
- close - fstart,
- out + n, cap - n,
- depth + 1);
- n += vn;
- }
- i = p;
- }
- out[n] = '\0';
- return n;
- }
- static unsigned int grid_skip_sep(const char *s, unsigned int len,
- unsigned int i)
- {
- while (i < len && (is_space(s[i]) || s[i] == ','))
- i++;
- return i;
- }
- /* Parse one grid track (px, %, rem/em, fr, auto, or the second minmax()
- * argument). Returns 1 on success. */
- static int parse_grid_track(const char *s, unsigned int len,
- unsigned int *pp, unsigned short *val,
- unsigned char *kind)
- {
- unsigned int i = grid_skip_sep(s, len, *pp);
- unsigned int whole = 0, frac = 0, scale = 1;
- int any = 0;
- if (i + 7 <= len && ci_eq_n(s + i, "minmax(", 7)) {
- unsigned int j = i + 7;
- while (j < len && s[j] != ',')
- j++;
- if (j >= len)
- return 0;
- i = j + 1;
- while (i < len && is_space(s[i]))
- i++;
- }
- if (i + 7 <= len && ci_eq_n(s + i, "repeat(", 7)) {
- int depth = 1;
- i += 7;
- while (i < len && depth) {
- if (s[i] == '(')
- depth++;
- else if (s[i] == ')')
- depth--;
- i++;
- }
- *pp = i;
- return 0;
- }
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- whole = whole * 10 + (unsigned int)(s[i] - '0');
- i++;
- any = 1;
- }
- if (i < len && s[i] == '.') {
- i++;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- if (scale < 1000) {
- frac = frac * 10 + (unsigned int)(s[i] - '0');
- scale *= 10;
- }
- i++;
- any = 1;
- }
- }
- if (!any) {
- if (i + 4 <= len && ci_eq_n(s + i, "auto", 4)) {
- *val = 1;
- *kind = 3;
- *pp = i + 4;
- return 1;
- }
- return 0;
- }
- if (i + 2 <= len && ci_eq_n(s + i, "fr", 2)) {
- *val = (unsigned short)(whole ? whole : 1);
- *kind = 3;
- i += 2;
- } else if (i < len && s[i] == '%') {
- *val = (unsigned short)(whole > 1000 ? 1000 : whole);
- *kind = 2;
- i++;
- } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
- *val = (unsigned short)(whole * 16 + frac * 16 / scale);
- *kind = 1;
- i += 3;
- } else if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
- *val = (unsigned short)(whole * 16 + frac * 16 / scale);
- *kind = 1;
- i += 2;
- } else {
- *val = (unsigned short)(whole > 1000 ? 1000 : whole);
- *kind = 1;
- if (i + 2 <= len && ci_eq_n(s + i, "px", 2))
- i += 2;
- }
- while (i < len && s[i] != ')' && !is_space(s[i]) && s[i] != ',')
- i++;
- *pp = i;
- return 1;
- }
- static int parse_grid_cols(const char *s, unsigned int len,
- unsigned short val[2], unsigned char kind[2])
- {
- unsigned int pos = 0;
- int n = 0;
- while (n < 2) {
- unsigned int before = pos;
- if (!parse_grid_track(s, len, &pos, &val[n], &kind[n]))
- break;
- n++;
- if (pos <= before)
- break;
- while (pos < len && is_space(s[pos]))
- pos++;
- if (pos >= len)
- break;
- }
- return n;
- }
- static int parse_grid_slot(const char *s, unsigned int len)
- {
- if (contains_ci_len(s, len, "footer") ||
- contains_ci_len(s, len, "header") ||
- contains_ci_len(s, len, "sitenotice") ||
- contains_ci_len(s, len, "navbar"))
- return 1;
- if (contains_ci_len(s, len, "columnstart") ||
- contains_ci_len(s, len, "sidebar") ||
- contains_ci_len(s, len, "left") ||
- contains_ci_len(s, len, "start"))
- return 2;
- if (contains_ci_len(s, len, "pagecontent") ||
- contains_ci_len(s, len, "content") ||
- contains_ci_len(s, len, "main") ||
- contains_ci_len(s, len, "right") ||
- contains_ci_len(s, len, "end"))
- return 3;
- return 0;
- }
- static int css_has_var(const char *s, unsigned int len)
- {
- unsigned int i;
- for (i = 0; i + 4 <= len; i++)
- if ((s[i] == 'v' || s[i] == 'V') &&
- ci_eq_n(s + i, "var(", 4))
- return 1;
- return 0;
- }
- static unsigned int css_skip_comment(const char *s, unsigned int len,
- unsigned int i)
- {
- i += 2;
- while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
- i++;
- if (i + 1 < len)
- i += 2;
- else
- i = len;
- return i;
- }
- static unsigned int css_skip_string(const char *s, unsigned int len,
- unsigned int i)
- {
- char q = s[i++];
- while (i < len && s[i] != q) {
- if (s[i] == '\\' && i + 1 < len)
- i++;
- i++;
- }
- if (i < len)
- i++;
- return i;
- }
- /* Find the '}' that closes a declaration block. i points just past the
- * opening '{'. Comments and quoted strings are skipped: a brace inside
- * one must not unbalance the scan (nd-aktuell's CSS comments contain
- * selectors such as "Highlight-Stories {" without a closing brace). */
- static unsigned int css_block_end(const char *s, unsigned int len,
- unsigned int i)
- {
- int depth = 0;
- while (i < len) {
- char c = s[i];
- if (c == '/' && i + 1 < len && s[i + 1] == '*') {
- i = css_skip_comment(s, len, i);
- continue;
- }
- if (c == '"' || c == '\'') {
- i = css_skip_string(s, len, i);
- continue;
- }
- if (c == '{') {
- depth++;
- } else if (c == '}') {
- if (depth == 0)
- return i;
- depth--;
- }
- i++;
- }
- return len;
- }
- /* Skip a block whose opening '{' has already been consumed. */
- static unsigned int css_skip_nested(const char *s, unsigned int len,
- unsigned int i)
- {
- int depth = 1;
- while (i < len && depth) {
- char c = s[i];
- if (c == '/' && i + 1 < len && s[i + 1] == '*') {
- i = css_skip_comment(s, len, i);
- continue;
- }
- if (c == '"' || c == '\'') {
- i = css_skip_string(s, len, i);
- continue;
- }
- if (c == '{')
- depth++;
- else if (c == '}')
- depth--;
- i++;
- }
- return i;
- }
- /* ---- @font-face ------------------------------------------------------- */
- static void font_trim(const char **sp, unsigned int *len)
- {
- const char *s = *sp;
- unsigned int n = *len;
- while (n && is_space(*s)) {
- s++;
- n--;
- }
- while (n && is_space(s[n - 1]))
- n--;
- if (n >= 2 && (s[0] == '"' || s[0] == '\'') && s[n - 1] == s[0]) {
- s++;
- n -= 2;
- }
- *sp = s;
- *len = n;
- }
- static void font_add(const char *family, unsigned int flen,
- const char *url, unsigned int ulen)
- {
- unsigned int i, n = 0, k;
- font_trim(&family, &flen);
- font_trim(&url, &ulen);
- if (!flen || !ulen || flen >= BR_FACE_FAMILY || ulen >= BR_FACE_URL)
- return;
- for (i = 0; i < (unsigned int)font_face_count; i++) {
- struct br_font_def *f = &font_defs[i];
- if (s_len(f->family) != flen)
- continue;
- for (k = 0; k < flen; k++) {
- char a = f->family[k], b = family[k];
- if (a >= 'A' && a <= 'Z')
- a = (char)(a - 'A' + 'a');
- if (b >= 'A' && b <= 'Z')
- b = (char)(b - 'A' + 'a');
- if (a != b)
- break;
- }
- if (k != flen)
- continue;
- n = 0;
- while (url[n] && n + 1 < sizeof(f->url)) {
- f->url[n] = url[n];
- n++;
- }
- f->url[n] = '\0';
- n = 0;
- while (font_base[n] && n + 1 < sizeof(f->base)) {
- f->base[n] = font_base[n];
- n++;
- }
- f->base[n] = '\0';
- return;
- }
- if (font_face_count >= BR_MAX_FACES)
- return;
- {
- struct br_font_def *f = &font_defs[font_face_count];
- for (n = 0; n < flen; n++)
- f->family[n] = family[n];
- f->family[n] = '\0';
- for (n = 0; n < ulen; n++)
- f->url[n] = url[n];
- f->url[n] = '\0';
- for (n = 0; font_base[n] && n + 1 < sizeof(f->base); n++)
- f->base[n] = font_base[n];
- f->base[n] = '\0';
- f->used = 1;
- font_face_count++;
- }
- }
- /* Look up the first name in a font-family list that has an @font-face. */
- static int font_family_id(const char *s, unsigned int len)
- {
- unsigned int i = 0;
- while (i < len) {
- unsigned int start, end, q = 0;
- while (i < len && (is_space(s[i]) || s[i] == ','))
- i++;
- if (i >= len)
- break;
- if (s[i] == '"' || s[i] == '\'') {
- char quote = s[i++];
- start = i;
- while (i < len && s[i] != quote)
- i++;
- end = i;
- if (i < len)
- i++;
- } else {
- start = i;
- while (i < len && s[i] != ',')
- i++;
- end = i;
- while (end > start && is_space(s[end - 1]))
- end--;
- }
- if (end > start) {
- for (q = 0; q < (unsigned int)font_face_count; q++) {
- const char *f = font_defs[q].family;
- unsigned int fl = s_len(f), k;
- if (fl != end - start)
- continue;
- for (k = 0; k < fl; k++) {
- char a = f[k], b = s[start + k];
- if (a >= 'A' && a <= 'Z')
- a = (char)(a - 'A' + 'a');
- if (b >= 'A' && b <= 'Z')
- b = (char)(b - 'A' + 'a');
- if (a != b)
- break;
- }
- if (k == fl)
- return (int)q + 1;
- }
- }
- }
- return 0;
- }
- /* Pre-scan a stylesheet for @font-face blocks. The main rule parser skips
- * at-rules, so faces must be registered before any font-family declaration
- * in the same sheet is resolved. */
- static void scan_font_faces(const char *s, unsigned int len)
- {
- unsigned int i = 0;
- while (i + 10 <= len) {
- unsigned int j, end;
- char family[BR_FACE_FAMILY], url[BR_FACE_URL];
- unsigned int flen = 0, ulen = 0, p;
- int have_family = 0, have_url = 0;
- if (s[i] != '@' || !ci_eq_n(s + i, "@font-face", 10)) {
- i++;
- continue;
- }
- j = i + 10;
- while (j < len && s[j] != '{' && s[j] != ';')
- j++;
- if (j >= len || s[j] == ';') {
- i = j + 1;
- continue;
- }
- end = css_skip_nested(s, len, j + 1);
- p = j + 1;
- while (p < end) {
- unsigned int ds, de, vs, ve;
- while (p < end && (is_space(s[p]) || s[p] == ';'))
- p++;
- if (p >= end)
- break;
- ds = p;
- while (p < end && s[p] != ':' && s[p] != ';')
- p++;
- de = p;
- if (p >= end || s[p] != ':') {
- while (p < end && s[p] != ';')
- p++;
- continue;
- }
- p++;
- vs = p;
- while (p < end && s[p] != ';')
- p++;
- ve = p;
- while (ds < de && is_space(s[ds]))
- ds++;
- while (de > ds && is_space(s[de - 1]))
- de--;
- while (vs < ve && is_space(s[vs]))
- vs++;
- while (ve > vs && is_space(s[ve - 1]))
- ve--;
- if (de - ds == 11 &&
- ci_eq_n(s + ds, "font-family", 11)) {
- const char *fp = s + vs;
- unsigned int fn = ve - vs;
- font_trim(&fp, &fn);
- if (fn && fn < sizeof(family)) {
- unsigned int k;
- for (k = 0; k < fn; k++)
- family[k] = fp[k];
- family[fn] = '\0';
- flen = fn;
- have_family = 1;
- }
- } else if (de - ds == 3 &&
- ci_eq_n(s + ds, "src", 3)) {
- unsigned int q = vs;
- while (q + 4 <= ve) {
- unsigned int us, ue;
- if (!ci_eq_n(s + q, "url(", 4)) {
- q++;
- continue;
- }
- q += 4;
- us = q;
- while (q < ve && s[q] != ')')
- q++;
- ue = q;
- {
- const char *up = s + us;
- unsigned int un = ue - us;
- font_trim(&up, &un);
- if (un && un < sizeof(url) &&
- !contains_ci_len(up, un,
- "woff")) {
- unsigned int k;
- for (k = 0; k < un; k++)
- url[k] = up[k];
- url[un] = '\0';
- ulen = un;
- have_url = 1;
- break;
- }
- }
- }
- }
- }
- if (have_family && have_url)
- font_add(family, flen, url, ulen);
- i = end;
- }
- }
- /* One border-radius length. Unlike parse_size(), percentages keep their
- * kind: the drawer scales them to the box, not the viewport. */
- static int parse_radius_value(const char *s, unsigned int len,
- unsigned int *pos, unsigned short *val,
- unsigned char *kind)
- {
- unsigned int i = *pos;
- unsigned long v = 0;
- int digits = 0, fdigits = 0, frac = 0, pct = 0, rem = 0;
- while (i < len && is_space(s[i]))
- i++;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- v = v * 10 + (unsigned int)(s[i] - '0');
- digits = 1;
- i++;
- }
- if (i < len && s[i] == '.') {
- i++;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- fdigits = 1;
- if (frac < 2) {
- v = v * 10 + (unsigned int)(s[i] - '0');
- frac++;
- }
- i++;
- }
- }
- while (frac < 2) {
- v *= 10;
- frac++;
- }
- while (i < len && is_space(s[i]))
- i++;
- if (i < len && s[i] == '%') {
- pct = 1;
- i++;
- } else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
- s[i + 2] == 'm') {
- rem = 1;
- i += 3;
- } else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm') {
- rem = 2;
- i += 2;
- } else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x') {
- i += 2;
- } else if (i < len && is_ident(s[i])) {
- return 0;
- }
- if (!digits && !fdigits)
- return 0;
- if (rem == 1)
- v *= root_font_px;
- else if (rem == 2)
- v *= root_font_px;
- v /= 100;
- if (v > 2048)
- v = 2048;
- *val = (unsigned short)v;
- *kind = pct ? 2 : 1;
- *pos = i;
- return 1;
- }
- /* font: [style] [variant] [weight] size[/line-height] family */
- static void add_font_shorthand(struct br_decl *out, unsigned int *nout,
- const char *v, unsigned int len, int important)
- {
- unsigned int pos = 0, ts, te;
- unsigned short size_raw = 0;
- unsigned char size_unit = 0;
- int have_size = 0;
- int bold = 0, italic = 0;
- int lh_px = 0, lh_pct = 0, mono = 0;
- while (next_token(v, len, &pos, &ts, &te)) {
- unsigned int tl = te - ts;
- unsigned int slash = 0;
- if (tl == 4 && ci_eq_n(v + ts, "bold", 4)) {
- bold = 1;
- continue;
- }
- if (tl == 6 && ci_eq_n(v + ts, "normal", 6))
- continue;
- if (tl == 6 && ci_eq_n(v + ts, "italic", 6)) {
- italic = 1;
- continue;
- }
- if (tl == 7 && ci_eq_n(v + ts, "oblique", 7)) {
- italic = 1;
- continue;
- }
- if (tl == 10 && ci_eq_n(v + ts, "small-caps", 10))
- continue;
- for (slash = 0; slash < tl; slash++)
- if (v[ts + slash] == '/')
- break;
- if (slash == tl) {
- unsigned int q;
- int weight = 0;
- for (q = 0; q < tl; q++) {
- if (v[ts + q] < '0' || v[ts + q] > '9')
- break;
- weight = weight * 10 + (v[ts + q] - '0');
- }
- if (q == tl && weight >= 100 && weight <= 900) {
- bold = weight >= 500;
- continue;
- }
- }
- {
- unsigned short raw;
- unsigned char unit;
- if (!parse_font_size(v + ts, slash < tl ? slash : tl,
- &raw, &unit))
- continue;
- size_raw = raw;
- size_unit = unit;
- }
- have_size = 1;
- if (slash < tl) {
- unsigned int lstart = ts + slash + 1;
- unsigned short lhp = 0;
- unsigned char lhpct = 0;
- if (!parse_line_height(v + lstart, te - lstart,
- &lhp, &lhpct)) {
- lh_px = 0;
- lh_pct = 0;
- } else {
- lh_px = lhp;
- lh_pct = lhpct;
- }
- }
- }
- if (!have_size)
- return;
- mono = contains_ci_len(v, len, "mono");
- decl_add(out, nout, PROP_FONT_PX, size_raw, important);
- decl_add(out, nout, PROP_FONT_UNIT, size_unit, important);
- decl_add(out, nout, PROP_BOLD, bold, important);
- decl_add(out, nout, PROP_ITALIC, italic, important);
- decl_add(out, nout, PROP_MONO, mono, important);
- decl_add(out, nout, PROP_FONT_FACE, 0, important);
- decl_add(out, nout, PROP_LH_PX, lh_px, important);
- decl_add(out, nout, PROP_LH_PCT, lh_pct, important);
- }
- static void parse_decls(struct br_doc *d, const char *s,
- unsigned int len, struct br_decl *out,
- unsigned int *nout)
- {
- static char expanded[4096];
- if (css_has_var(s, len)) {
- unsigned int elen = css_expand_vars(d, s, len, expanded,
- sizeof(expanded), 0);
- if (elen) {
- s = expanded;
- len = elen;
- }
- }
- unsigned int i = 0;
- *nout = 0;
- while (i < len && *nout < BR_DECLS) {
- unsigned int pstart, pend, vstart, vend, k;
- char prop[24];
- int important = 0;
- while (i < len && (is_space(s[i]) || s[i] == ';'))
- i++;
- if (i >= len)
- break;
- /* A nested at-rule (@media/@supports) inside the declaration
- * block carries its own selector context; skip it rather than
- * reading its prelude as a declaration. */
- if (s[i] == '@') {
- while (i < len && s[i] != '{' && s[i] != ';')
- i++;
- if (i < len && s[i] == '{')
- i = css_skip_nested(s, len, i + 1);
- else if (i < len)
- i++;
- continue;
- }
- pstart = i;
- while (i < len && s[i] != ':' && s[i] != ';' && s[i] != '{')
- i++;
- if (i < len && s[i] == '{') {
- /* Nested style rule: skip its block. */
- i = css_skip_nested(s, len, i + 1);
- continue;
- }
- pend = i;
- if (i >= len || s[i] != ':')
- continue;
- i++;
- vstart = i;
- while (i < len && s[i] != ';')
- i++;
- vend = i;
- while (pstart < pend && is_space(s[pstart]))
- pstart++;
- while (pend > pstart && is_space(s[pend - 1]))
- pend--;
- while (vstart < vend && is_space(s[vstart]))
- vstart++;
- while (vend > vstart && is_space(s[vend - 1]))
- vend--;
- if (vend - vstart >= 10 &&
- ci_eq_n(s + vend - 10, "!important", 10)) {
- important = 1;
- vend -= 10;
- while (vend > vstart && is_space(s[vend - 1]))
- vend--;
- }
- for (k = 0; k < sizeof(prop) - 1 && pstart + k < pend; k++) {
- char c = s[pstart + k];
- if (c >= 'A' && c <= 'Z')
- c = (char)(c - 'A' + 'a');
- prop[k] = c;
- }
- prop[k] = '\0';
- if (str_eq(prop, "color")) {
- unsigned short c;
- if (parse_color(s + vstart, vend - vstart, &c))
- decl_add(out, nout, PROP_COLOR, c, important);
- } else if (str_eq(prop, "fill") || str_eq(prop, "stroke")) {
- /* SVG paint only: an HTML element's fill never paints
- * its background (tagesschau sets fill on buttons). */
- int is_fill = prop[0] == 'f';
- unsigned short c;
- int mode = paint_mode(s + vstart, vend - vstart, &c);
- if (mode) {
- decl_add(out, nout, is_fill ? PROP_FILL_MODE :
- PROP_STROKE_MODE, mode, important);
- if (mode == BR_PAINT_COLOR)
- decl_add(out, nout, is_fill ? PROP_FILL :
- PROP_STROKE, (short)c,
- important);
- }
- } else if (str_eq(prop, "stroke-width")) {
- int sw = svg_len16(s + vstart, vend - vstart);
- if (sw >= 0)
- decl_add(out, nout, PROP_STROKE_W, sw,
- important);
- } else if (str_eq(prop, "fill-rule")) {
- decl_add(out, nout, PROP_FILL_RULE,
- contains_ci_len(s + vstart, vend - vstart,
- "evenodd") ? 1 : 0,
- important);
- } else if (str_eq(prop, "text-anchor")) {
- int a = BR_ALIGN_LEFT;
- if (contains_ci_len(s + vstart, vend - vstart, "middle"))
- a = BR_ALIGN_CENTER;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "end"))
- a = BR_ALIGN_RIGHT;
- decl_add(out, nout, PROP_ALIGN, a, important);
- } else if (str_eq(prop, "background-image")) {
- add_bg_image_decl(out, nout, s + vstart,
- vend - vstart, important);
- } else if (str_eq(prop, "background-repeat")) {
- int r = 0;
- if (contains_ci_len(s + vstart, vend - vstart,
- "no-repeat"))
- r = 1;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "repeat-x"))
- r = 2;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "repeat-y"))
- r = 3;
- decl_add(out, nout, PROP_BG_REPEAT, r, important);
- } else if (str_eq(prop, "background-position")) {
- add_bg_position(out, nout, s + vstart, vend - vstart,
- important);
- } else if (str_eq(prop, "background-size")) {
- add_bg_size(out, nout, s + vstart, vend - vstart,
- important);
- } else if (str_eq(prop, "background-color") ||
- str_eq(prop, "background")) {
- unsigned short c;
- int is_bg = str_eq(prop, "background");
- if (is_bg) {
- add_bg_image_decl(out, nout, s + vstart,
- vend - vstart, important);
- add_bg_shorthand_extra(out, nout,
- s + vstart,
- vend - vstart,
- important);
- if (contains_ci_len(s + vstart, vend - vstart,
- "no-repeat"))
- decl_add(out, nout, PROP_BG_REPEAT, 1,
- important);
- else if (contains_ci_len(s + vstart,
- vend - vstart,
- "repeat-x"))
- decl_add(out, nout, PROP_BG_REPEAT, 2,
- important);
- else if (contains_ci_len(s + vstart,
- vend - vstart,
- "repeat-y"))
- decl_add(out, nout, PROP_BG_REPEAT, 3,
- important);
- }
- if (parse_color(s + vstart, vend - vstart, &c))
- decl_add(out, nout, PROP_BG, c, important);
- else if (is_bg &&
- find_color_last(s + vstart, vend - vstart, &c))
- decl_add(out, nout, PROP_BG, c, important);
- } else if (str_eq(prop, "font-size")) {
- unsigned short raw;
- unsigned char unit;
- if (parse_font_size(s + vstart, vend - vstart,
- &raw, &unit)) {
- decl_add(out, nout, PROP_FONT_PX, raw,
- important);
- decl_add(out, nout, PROP_FONT_UNIT, unit,
- important);
- }
- } else if (str_eq(prop, "font-weight")) {
- int bold = 0;
- unsigned int j;
- if (vend - vstart >= 4 &&
- ci_eq_n(s + vstart, "bold", 4))
- bold = 1;
- else {
- int w = 0;
- for (j = vstart; j < vend; j++)
- if (s[j] >= '0' && s[j] <= '9')
- w = w * 10 + (s[j] - '0');
- if (w >= 500)
- bold = 1;
- }
- decl_add(out, nout, PROP_BOLD, bold, important);
- } else if (str_eq(prop, "font-style")) {
- int italic = (vend - vstart >= 6 &&
- ci_eq_n(s + vstart, "italic", 6));
- decl_add(out, nout, PROP_ITALIC, italic, important);
- } else if (str_eq(prop, "font-family")) {
- unsigned int j;
- int mono = 0;
- int face = font_family_id(s + vstart, vend - vstart);
- for (j = vstart; j + 4 <= vend; j++)
- if (ci_eq_n(s + j, "mono", 4))
- mono = 1;
- decl_add(out, nout, PROP_FONT_FACE, face, important);
- decl_add(out, nout, PROP_MONO, mono, important);
- } else if (str_eq(prop, "font")) {
- add_font_shorthand(out, nout, s + vstart,
- vend - vstart, important);
- } else if (str_eq(prop, "text-align")) {
- int a = BR_ALIGN_LEFT;
- if (vend - vstart >= 6 &&
- ci_eq_n(s + vstart, "center", 6))
- a = BR_ALIGN_CENTER;
- else if (vend - vstart >= 5 &&
- ci_eq_n(s + vstart, "right", 5))
- a = BR_ALIGN_RIGHT;
- decl_add(out, nout, PROP_ALIGN, a, important);
- } else if (str_eq(prop, "text-decoration")) {
- int u = 0, stv = 0, ov = 0;
- if (contains_ci_len(s + vstart, vend - vstart,
- "underline"))
- u = 1;
- if (contains_ci_len(s + vstart, vend - vstart,
- "line-through"))
- stv = 1;
- if (contains_ci_len(s + vstart, vend - vstart,
- "overline"))
- ov = 1;
- decl_add(out, nout, PROP_UNDERLINE, u, important);
- decl_add(out, nout, PROP_STRIKE, stv, important);
- decl_add(out, nout, PROP_OVERLINE, ov, important);
- } else if (str_eq(prop, "text-transform")) {
- int u = (vend - vstart >= 9 &&
- ci_eq_n(s + vstart, "uppercase", 9));
- decl_add(out, nout, PROP_UPPER, u, important);
- } else if (str_eq(prop, "white-space")) {
- int nw = contains_ci_len(s + vstart, vend - vstart,
- "nowrap") ||
- contains_ci_len(s + vstart, vend - vstart,
- "pre");
- decl_add(out, nout, PROP_NOWRAP, nw, important);
- } else if (str_eq(prop, "display")) {
- decl_add(out, nout, PROP_DISPLAY,
- parse_display(s + vstart, vend - vstart),
- important);
- } else if (str_eq(prop, "visibility")) {
- int h = contains_ci_len(s + vstart, vend - vstart,
- "hidden") ||
- contains_ci_len(s + vstart, vend - vstart,
- "collapse");
- decl_add(out, nout, PROP_VIS_HIDDEN, h, important);
- } else if (str_eq(prop, "opacity")) {
- unsigned int q;
- int h = 1, any = 0;
- for (q = vstart; q < vend; q++) {
- if (s[q] >= '1' && s[q] <= '9') {
- h = 0;
- break;
- }
- if (s[q] >= '0' && s[q] <= '9')
- any = 1;
- }
- if (!any || contains_ci_len(s + vstart, vend - vstart,
- "inherit"))
- h = 0;
- decl_add(out, nout, PROP_OPACITY_HIDDEN, h, important);
- } else if (str_eq(prop, "overflow") ||
- str_eq(prop, "overflow-x") ||
- str_eq(prop, "overflow-y")) {
- int h = contains_ci_len(s + vstart, vend - vstart,
- "hidden") ||
- contains_ci_len(s + vstart, vend - vstart,
- "clip");
- decl_add(out, nout, PROP_OVERFLOW_HIDDEN, h, important);
- } else if (str_eq(prop, "float")) {
- int f = 0;
- if (vend - vstart >= 4 &&
- ci_eq_n(s + vstart, "left", 4))
- f = 1;
- else if (vend - vstart >= 5 &&
- ci_eq_n(s + vstart, "right", 5))
- f = 2;
- decl_add(out, nout, PROP_FLOAT, f, important);
- } else if (str_eq(prop, "clear")) {
- int c = 0;
- if (contains_ci_len(s + vstart, vend - vstart, "both"))
- c = 3;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "left"))
- c = 1;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "right"))
- c = 2;
- decl_add(out, nout, PROP_CLEAR, c, important);
- } else if (str_eq(prop, "flex-grow") ||
- str_eq(prop, "flex")) {
- unsigned int q = vstart;
- int g = 0;
- while (q < vend && (s[q] == ' ' || s[q] == '\t'))
- q++;
- while (q < vend && s[q] >= '0' && s[q] <= '9') {
- g = g * 10 + (s[q] - '0');
- q++;
- }
- if (g > 100)
- g = 100;
- decl_add(out, nout, PROP_FLEX_GROW, g, important);
- } else if (str_eq(prop, "flex-direction")) {
- decl_add(out, nout, PROP_FLEX_COL,
- contains_ci_len(s + vstart, vend - vstart,
- "column"), important);
- } else if (str_eq(prop, "flex-wrap")) {
- int wrap = 0;
- if (contains_ci_len(s + vstart, vend - vstart, "nowrap"))
- wrap = 2;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "wrap"))
- wrap = 1;
- decl_add(out, nout, PROP_FLEX_WRAP, wrap, important);
- } else if (str_eq(prop, "justify-content")) {
- int j = 0;
- if (contains_ci_len(s + vstart, vend - vstart,
- "space-between"))
- j = 3;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "space-around"))
- j = 4;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "space-evenly"))
- j = 5;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "center"))
- j = 1;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "flex-end") ||
- contains_ci_len(s + vstart, vend - vstart,
- "end"))
- j = 2;
- decl_add(out, nout, PROP_JUSTIFY, j, important);
- } else if (str_eq(prop, "align-items")) {
- int a = 0;
- if (contains_ci_len(s + vstart, vend - vstart, "center"))
- a = 1;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "flex-end") ||
- contains_ci_len(s + vstart, vend - vstart,
- "end"))
- a = 2;
- decl_add(out, nout, PROP_ALIGN_ITEMS, a, important);
- } else if (str_eq(prop, "position")) {
- int p = 0;
- if (contains_ci_len(s + vstart, vend - vstart,
- "absolute"))
- p = 1;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "fixed"))
- p = 2;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "relative"))
- p = 3;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "sticky"))
- p = 4;
- decl_add(out, nout, PROP_POS, p, important);
- } else if (str_eq(prop, "left") || str_eq(prop, "top") ||
- str_eq(prop, "right") || str_eq(prop, "bottom")) {
- int which = str_eq(prop, "left") ? 0 :
- str_eq(prop, "top") ? 1 :
- str_eq(prop, "right") ? 2 : 3;
- unsigned int pos = 0;
- if (!(vend - vstart >= 4 &&
- ci_eq_n(s + vstart, "auto", 4))) {
- int v = parse_len(s + vstart, vend - vstart, &pos);
- if (v > 32000)
- v = 32000;
- else if (v < -32000)
- v = -32000;
- decl_add(out, nout,
- (unsigned char)(which == 0 ? PROP_LEFT :
- which == 1 ? PROP_TOP :
- which == 2 ? PROP_RIGHT :
- PROP_BOTTOM),
- v, important);
- decl_add(out, nout,
- (unsigned char)(which == 0 ? PROP_LEFT_SET :
- which == 1 ? PROP_TOP_SET :
- which == 2 ? PROP_RIGHT_SET :
- PROP_BOTTOM_SET),
- 1, important);
- }
- } else if (str_eq(prop, "z-index")) {
- unsigned int q = vstart;
- int sign = 1;
- int v = 0;
- int any = 0;
- if (q < vend && s[q] == '-') {
- sign = -1;
- q++;
- }
- while (q < vend && s[q] >= '0' && s[q] <= '9') {
- v = v * 10 + (s[q] - '0');
- any = 1;
- q++;
- }
- if (any) {
- v *= sign;
- if (v > 32000)
- v = 32000;
- else if (v < -32000)
- v = -32000;
- decl_add(out, nout, PROP_ZINDEX, v, important);
- decl_add(out, nout, PROP_ZSET, 1, important);
- } else if (vend - vstart >= 4 &&
- ci_eq_n(s + vstart, "auto", 4)) {
- decl_add(out, nout, PROP_ZSET, 0, important);
- }
- } else if (str_eq(prop, "transform")) {
- const char *v = s + vstart;
- unsigned int vl = vend - vstart;
- int tx = 0, ty = 0;
- int is_x = contains_ci_len(v, vl, "translatex");
- int is_y = contains_ci_len(v, vl, "translatey");
- unsigned int off = 0, arg;
- if (contains_ci_len(v, vl, "translate")) {
- while (off < vl && v[off] != '(')
- off++;
- if (off < vl)
- off++;
- if (!is_y)
- tx = parse_len(v, vl, &off);
- if (!is_x) {
- while (off < vl &&
- (is_space(v[off]) || v[off] == ','))
- off++;
- ty = parse_len(v, vl, &off);
- }
- decl_add(out, nout, PROP_TRANSFORM_X, tx,
- important);
- decl_add(out, nout, PROP_TRANSFORM_Y, ty,
- important);
- decl_add(out, nout, PROP_TRANSFORM_SET, 1,
- important);
- }
- if (find_fn(v, vl, "scale", &arg)) {
- unsigned int q = arg;
- signed short sx = 100, sy = 100;
- if (parse_fixed_100(v, vl, &q, &sx)) {
- unsigned int r = q;
- while (r < vl &&
- (is_space(v[r]) || v[r] == ','))
- r++;
- if (!parse_fixed_100(v, vl, &r, &sy))
- sy = sx;
- decl_add(out, nout, PROP_SCALE_X, sx,
- important);
- decl_add(out, nout, PROP_SCALE_Y, sy,
- important);
- decl_add(out, nout,
- PROP_TRANSFORM_PAINT, 1,
- important);
- decl_add(out, nout,
- PROP_TRANSFORM_SET, 1,
- important);
- }
- }
- if (find_fn(v, vl, "rotate", &arg)) {
- unsigned int q = arg;
- signed short deg;
- if (parse_fixed_100(v, vl, &q, °)) {
- decl_add(out, nout, PROP_ROTATE, deg,
- important);
- decl_add(out, nout,
- PROP_TRANSFORM_PAINT, 1,
- important);
- decl_add(out, nout,
- PROP_TRANSFORM_SET, 1,
- important);
- }
- }
- } else if (str_eq(prop, "transform-origin")) {
- add_transform_origin(out, nout, s + vstart,
- vend - vstart, important);
- } else if (str_eq(prop, "grid-template-columns") ||
- str_eq(prop, "grid-template")) {
- const char *v = s + vstart;
- unsigned int vl = vend - vstart;
- unsigned short val[2];
- unsigned char kind[2];
- int n, q;
- if (str_eq(prop, "grid-template")) {
- for (q = 0; q < (int)vl; q++)
- if (v[q] == '/') {
- q++;
- while (q < (int)vl && is_space(v[q]))
- q++;
- v += q;
- vl -= (unsigned int)q;
- break;
- }
- }
- if (contains_ci_len(v, vl, "auto-fit") ||
- contains_ci_len(v, vl, "auto-fill")) {
- unsigned int mp = 0;
- int found = 0;
- unsigned short mw = 200;
- unsigned char mk = 1;
- for (q = 0; q + 7 <= (int)vl; q++)
- if (ci_eq_n(v + q, "minmax(", 7)) {
- mp = (unsigned int)q + 7;
- found = 1;
- break;
- }
- if (found)
- parse_grid_track(v, vl, &mp, &mw, &mk);
- if (mk != 1 || mw < 80)
- mw = 200;
- if (mw > 480)
- mw = 480;
- decl_add(out, nout, PROP_GRID_AUTO, 1, important);
- decl_add(out, nout, PROP_WIDTH, mw, important);
- decl_add(out, nout, PROP_WIDTH_KIND, 1, important);
- } else {
- n = parse_grid_cols(v, vl, val, kind);
- if (n > 0) {
- decl_add(out, nout, PROP_WIDTH, val[0],
- important);
- decl_add(out, nout, PROP_WIDTH_KIND,
- kind[0], important);
- decl_add(out, nout, PROP_GRID_NCOLS, n,
- important);
- }
- if (n > 1) {
- decl_add(out, nout, PROP_MAXW, val[1],
- important);
- decl_add(out, nout, PROP_MAXW_KIND,
- kind[1], important);
- }
- }
- } else if (str_eq(prop, "gap") || str_eq(prop, "column-gap")) {
- unsigned int gp = 0;
- unsigned short gv = 0;
- unsigned char gk = 1;
- if (parse_grid_track(s, vend, &gp, &gv, &gk) && gk == 1) {
- if (gv > 120)
- gv = 120;
- decl_add(out, nout, PROP_GRID_GAP, gv, important);
- }
- } else if (str_eq(prop, "grid-area")) {
- int slot = parse_grid_slot(s + vstart, vend - vstart);
- if (slot)
- decl_add(out, nout, PROP_GRID_SLOT, slot, important);
- } else if (str_eq(prop, "width") ||
- str_eq(prop, "max-width") ||
- str_eq(prop, "min-width") ||
- str_eq(prop, "height") ||
- str_eq(prop, "max-height") ||
- str_eq(prop, "min-height")) {
- unsigned short v;
- unsigned char kind;
- if (vend - vstart >= 5 &&
- ci_eq_n(s + vstart, "calc(", 5)) {
- int pct, px;
- if (parse_calc(s + vstart + 5,
- vend - vstart - 5, &pct, &px)) {
- if (str_eq(prop, "width")) {
- decl_add(out, nout, PROP_WIDTH,
- 0, important);
- decl_add(out, nout,
- PROP_WIDTH_KIND, 3,
- important);
- decl_add(out, nout,
- PROP_WIDTH_CALC_PCT,
- pct, important);
- decl_add(out, nout,
- PROP_WIDTH_CALC_PX,
- px, important);
- } else if (str_eq(prop, "max-width")) {
- decl_add(out, nout, PROP_MAXW,
- 0, important);
- decl_add(out, nout,
- PROP_MAXW_KIND, 3,
- important);
- decl_add(out, nout,
- PROP_MAXW_CALC_PCT,
- pct, important);
- decl_add(out, nout,
- PROP_MAXW_CALC_PX,
- px, important);
- } else if (str_eq(prop, "min-width")) {
- decl_add(out, nout, PROP_MINW,
- 0, important);
- decl_add(out, nout,
- PROP_MINW_KIND, 3,
- important);
- decl_add(out, nout,
- PROP_MINW_CALC_PCT,
- pct, important);
- decl_add(out, nout,
- PROP_MINW_CALC_PX,
- px, important);
- } else if (str_eq(prop, "height")) {
- decl_add(out, nout, PROP_HEIGHT,
- 0, important);
- decl_add(out, nout,
- PROP_HEIGHT_KIND, 3,
- important);
- decl_add(out, nout,
- PROP_HEIGHT_CALC_PCT,
- pct, important);
- decl_add(out, nout,
- PROP_HEIGHT_CALC_PX,
- px, important);
- } else if (str_eq(prop, "max-height")) {
- decl_add(out, nout, PROP_MAXH,
- 0, important);
- decl_add(out, nout,
- PROP_MAXH_KIND, 3,
- important);
- decl_add(out, nout,
- PROP_MAXH_CALC_PCT,
- pct, important);
- decl_add(out, nout,
- PROP_MAXH_CALC_PX,
- px, important);
- } else {
- decl_add(out, nout, PROP_MINH,
- 0, important);
- decl_add(out, nout,
- PROP_MINH_KIND, 3,
- important);
- decl_add(out, nout,
- PROP_MINH_CALC_PCT,
- pct, important);
- decl_add(out, nout,
- PROP_MINH_CALC_PX,
- px, important);
- }
- }
- continue;
- }
- if (parse_size(s + vstart, vend - vstart, &v, &kind)) {
- if (str_eq(prop, "width")) {
- decl_add(out, nout, PROP_WIDTH, v, important);
- decl_add(out, nout, PROP_WIDTH_KIND, kind,
- important);
- } else if (str_eq(prop, "max-width")) {
- decl_add(out, nout, PROP_MAXW, v, important);
- decl_add(out, nout, PROP_MAXW_KIND, kind,
- important);
- } else if (str_eq(prop, "min-width")) {
- decl_add(out, nout, PROP_MINW, v, important);
- decl_add(out, nout, PROP_MINW_KIND, kind,
- important);
- } else if (str_eq(prop, "max-height")) {
- decl_add(out, nout, PROP_MAXH, v, important);
- decl_add(out, nout, PROP_MAXH_KIND, kind,
- important);
- } else if (str_eq(prop, "min-height")) {
- decl_add(out, nout, PROP_MINH, v, important);
- decl_add(out, nout, PROP_MINH_KIND, kind,
- important);
- } else {
- decl_add(out, nout, PROP_HEIGHT, v, important);
- decl_add(out, nout, PROP_HEIGHT_KIND, kind,
- important);
- }
- }
- } else if (str_eq(prop, "box-sizing")) {
- int bb = (vend - vstart >= 10 &&
- ci_eq_n(s + vstart, "border-box", 10));
- decl_add(out, nout, PROP_BOX_SIZING, bb, important);
- } else if (str_eq(prop, "line-height")) {
- unsigned short px;
- unsigned char pct;
- if (parse_line_height(s + vstart, vend - vstart, &px, &pct)) {
- if (px) {
- decl_add(out, nout, PROP_LH_PX, px, important);
- decl_add(out, nout, PROP_LH_PCT, 0, important);
- } else {
- decl_add(out, nout, PROP_LH_PX, 0, important);
- decl_add(out, nout, PROP_LH_PCT, pct, important);
- }
- }
- } else if (str_eq(prop, "list-style") ||
- str_eq(prop, "list-style-type")) {
- int ls = BR_LIST_AUTO;
- if (contains_ci_len(s + vstart, vend - vstart,
- "none"))
- ls = BR_LIST_NONE;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "circle"))
- ls = BR_LIST_CIRCLE;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "square"))
- ls = BR_LIST_SQUARE;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "lower-alpha") ||
- contains_ci_len(s + vstart, vend - vstart,
- "lower-latin"))
- ls = BR_LIST_LOWER_ALPHA;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "upper-alpha") ||
- contains_ci_len(s + vstart, vend - vstart,
- "upper-latin"))
- ls = BR_LIST_UPPER_ALPHA;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "lower-roman"))
- ls = BR_LIST_LOWER_ROMAN;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "upper-roman"))
- ls = BR_LIST_UPPER_ROMAN;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "decimal"))
- ls = BR_LIST_DECIMAL;
- else if (contains_ci_len(s + vstart, vend - vstart,
- "disc"))
- ls = BR_LIST_DISC;
- decl_add(out, nout, PROP_LIST_NONE,
- ls == BR_LIST_NONE, important);
- decl_add(out, nout, PROP_LIST_STYLE, ls, important);
- } else if (str_eq(prop, "border-radius") ||
- str_eq(prop, "border-top-left-radius") ||
- str_eq(prop, "border-top-right-radius") ||
- str_eq(prop, "border-bottom-right-radius") ||
- str_eq(prop, "border-bottom-left-radius")) {
- unsigned short v[4];
- unsigned char k[4];
- unsigned int pos = 0;
- int n = 0;
- while (pos < vend - vstart && n < 4) {
- while (pos < vend - vstart &&
- is_space(s[vstart + pos]))
- pos++;
- if (pos >= vend - vstart ||
- s[vstart + pos] == '/')
- break;
- if (!parse_radius_value(s + vstart,
- vend - vstart, &pos,
- &v[n], &k[n]))
- break;
- n++;
- }
- if (n) {
- unsigned short r[4];
- unsigned char rk[4];
- int i;
- if (n == 1) {
- for (i = 0; i < 4; i++) {
- r[i] = v[0];
- rk[i] = k[0];
- }
- } else if (n == 2) {
- r[0] = r[2] = v[0];
- r[1] = r[3] = v[1];
- rk[0] = rk[2] = k[0];
- rk[1] = rk[3] = k[1];
- } else if (n == 3) {
- r[0] = v[0];
- r[1] = r[3] = v[1];
- r[2] = v[2];
- rk[0] = k[0];
- rk[1] = rk[3] = k[1];
- rk[2] = k[2];
- } else {
- for (i = 0; i < 4; i++) {
- r[i] = v[i];
- rk[i] = k[i];
- }
- }
- for (i = 0; i < 4; i++)
- r[i] = rk[i] == 2 ? 255 :
- (r[i] > 255 ? 255 : r[i]);
- if (str_eq(prop, "border-top-left-radius")) {
- decl_add(out, nout, PROP_RADIUS_TL,
- (short)r[0], important);
- } else if (str_eq(prop,
- "border-top-right-radius")) {
- decl_add(out, nout, PROP_RADIUS_TR,
- (short)r[0], important);
- } else if (str_eq(prop,
- "border-bottom-right-radius")) {
- decl_add(out, nout, PROP_RADIUS_BR,
- (short)r[0], important);
- } else if (str_eq(prop,
- "border-bottom-left-radius")) {
- decl_add(out, nout, PROP_RADIUS_BL,
- (short)r[0], important);
- } else {
- decl_add(out, nout, PROP_RADIUS_TL,
- (short)r[0], important);
- decl_add(out, nout, PROP_RADIUS_TR,
- (short)r[1], important);
- decl_add(out, nout, PROP_RADIUS_BR,
- (short)r[2], important);
- decl_add(out, nout, PROP_RADIUS_BL,
- (short)r[3], important);
- }
- }
- } else if (str_eq(prop, "border") ||
- str_eq(prop, "border-top") ||
- str_eq(prop, "border-right") ||
- str_eq(prop, "border-bottom") ||
- str_eq(prop, "border-left") ||
- str_eq(prop, "border-color") ||
- str_eq(prop, "border-width") ||
- str_eq(prop, "border-style") ||
- str_eq(prop, "border-top-width") ||
- str_eq(prop, "border-right-width") ||
- str_eq(prop, "border-bottom-width") ||
- str_eq(prop, "border-left-width") ||
- str_eq(prop, "border-top-color") ||
- str_eq(prop, "border-right-color") ||
- str_eq(prop, "border-bottom-color") ||
- str_eq(prop, "border-left-color") ||
- str_eq(prop, "border-top-style") ||
- str_eq(prop, "border-right-style") ||
- str_eq(prop, "border-bottom-style") ||
- str_eq(prop, "border-left-style")) {
- const char *v = s + vstart;
- unsigned int vl = vend - vstart;
- unsigned int pos = 0, ts, te;
- int widths[4], colors[4], styles[4], trans[4];
- int nw = 0, nc = 0, ns = 0;
- int side = -1;
- int is_wl = str_eq(prop, "border-width");
- int is_cl = str_eq(prop, "border-color");
- int is_sl = str_eq(prop, "border-style");
- int i;
- if (str_eq(prop, "border-top")) side = 0;
- else if (str_eq(prop, "border-right")) side = 1;
- else if (str_eq(prop, "border-bottom")) side = 2;
- else if (str_eq(prop, "border-left")) side = 3;
- else if (str_eq(prop, "border-top-width")) {
- side = 0; is_wl = 2;
- } else if (str_eq(prop, "border-right-width")) {
- side = 1; is_wl = 2;
- } else if (str_eq(prop, "border-bottom-width")) {
- side = 2; is_wl = 2;
- } else if (str_eq(prop, "border-left-width")) {
- side = 3; is_wl = 2;
- } else if (str_eq(prop, "border-top-color")) {
- side = 0; is_cl = 2;
- } else if (str_eq(prop, "border-right-color")) {
- side = 1; is_cl = 2;
- } else if (str_eq(prop, "border-bottom-color")) {
- side = 2; is_cl = 2;
- } else if (str_eq(prop, "border-left-color")) {
- side = 3; is_cl = 2;
- } else if (str_eq(prop, "border-top-style")) {
- side = 0; is_sl = 2;
- } else if (str_eq(prop, "border-right-style")) {
- side = 1; is_sl = 2;
- } else if (str_eq(prop, "border-bottom-style")) {
- side = 2; is_sl = 2;
- } else if (str_eq(prop, "border-left-style")) {
- side = 3; is_sl = 2;
- }
- if (is_wl) {
- while (nw < 4 && next_token(v, vl, &pos,
- &ts, &te)) {
- int x;
- if (!parse_len_token(v + ts, te - ts,
- &x))
- break;
- widths[nw++] = x;
- }
- if (nw) {
- if (is_wl == 1)
- expand_four(widths, nw);
- if (side >= 0 && is_wl == 2) {
- decl_add(out, nout,
- PROP_BW_T + side,
- widths[0], important);
- } else {
- for (i = 0; i < 4; i++)
- decl_add(out, nout,
- PROP_BW_T + i,
- widths[i],
- important);
- }
- }
- } else if (is_cl) {
- unsigned short c;
- int t;
- while (nc < 4 && next_token(v, vl, &pos,
- &ts, &te)) {
- t = color_is_transparent(v + ts,
- te - ts);
- if (!t && !parse_color(v + ts,
- te - ts, &c))
- break;
- colors[nc] = t ? 0 : c;
- trans[nc] = t;
- nc++;
- }
- if (nc) {
- if (is_cl == 1) {
- expand_four(colors, nc);
- expand_four(trans, nc);
- }
- if (side >= 0 && is_cl == 2) {
- decl_add(out, nout,
- PROP_BC_T + side,
- colors[0], important);
- decl_add(out, nout,
- PROP_BT_T + side,
- trans[0], important);
- } else {
- for (i = 0; i < 4; i++) {
- decl_add(out, nout,
- PROP_BC_T + i,
- colors[i],
- important);
- decl_add(out, nout,
- PROP_BT_T + i,
- trans[i],
- important);
- }
- }
- }
- } else if (is_sl) {
- while (ns < 4 && next_token(v, vl, &pos,
- &ts, &te)) {
- int stv;
- if ((te - ts == 4 &&
- ci_eq_n(v + ts, "none", 4)) ||
- (te - ts == 6 &&
- ci_eq_n(v + ts, "hidden", 6)))
- stv = 0;
- else if (te - ts == 6 &&
- ci_eq_n(v + ts,
- "dashed", 6))
- stv = 2;
- else if (te - ts == 6 &&
- ci_eq_n(v + ts,
- "dotted", 6))
- stv = 3;
- else
- stv = 1;
- styles[ns++] = stv;
- }
- if (ns) {
- if (is_sl == 1)
- expand_four(styles, ns);
- if (is_sl == 2 && side >= 0) {
- if (!styles[0])
- decl_add(out, nout,
- PROP_BW_T + side,
- 0, important);
- else
- decl_add(out, nout,
- PROP_BS_T + side,
- styles[0] - 1,
- important);
- } else {
- for (i = 0; i < 4; i++) {
- if (!styles[i])
- decl_add(out, nout,
- PROP_BW_T + i,
- 0,
- important);
- else
- decl_add(out, nout,
- PROP_BS_T + i,
- styles[i] - 1,
- important);
- }
- }
- }
- } else {
- int bw = -1, bc = -1, style_none = 0;
- int bstyle = -1, btrans = 0;
- while (next_token(v, vl, &pos, &ts, &te)) {
- unsigned short c;
- int x;
- if ((te - ts == 4 &&
- ci_eq_n(v + ts, "none", 4)) ||
- (te - ts == 6 &&
- ci_eq_n(v + ts, "hidden", 6))) {
- style_none = 1;
- } else if (te - ts == 6 &&
- ci_eq_n(v + ts, "dashed", 6)) {
- bstyle = BR_BORDER_DASHED;
- } else if (te - ts == 6 &&
- ci_eq_n(v + ts, "dotted", 6)) {
- bstyle = BR_BORDER_DOTTED;
- } else if (te - ts == 5 &&
- ci_eq_n(v + ts, "solid", 5)) {
- bstyle = BR_BORDER_SOLID;
- } else if (parse_len_token(v + ts,
- te - ts, &x)) {
- bw = x;
- } else if (color_is_transparent(v + ts,
- te - ts)) {
- bc = 0;
- btrans = 1;
- } else if (parse_color(v + ts,
- te - ts, &c)) {
- bc = c;
- btrans = 0;
- }
- }
- if (style_none)
- bw = 0;
- if (bc >= 0 && bw < 0)
- bw = 1;
- if (side < 0) {
- if (bw >= 0)
- decl_add(out, nout,
- PROP_BORDER_W, bw,
- important);
- if (bc >= 0)
- decl_add(out, nout,
- PROP_BORDER_COLOR, bc,
- important);
- if (bc >= 0)
- for (i = 0; i < 4; i++)
- decl_add(out, nout,
- PROP_BT_T + i,
- btrans,
- important);
- if (bstyle >= 0)
- for (i = 0; i < 4; i++)
- decl_add(out, nout,
- PROP_BS_T + i,
- bstyle,
- important);
- } else {
- if (bw >= 0)
- decl_add(out, nout,
- PROP_BW_T + side, bw,
- important);
- if (bc >= 0)
- decl_add(out, nout,
- PROP_BC_T + side, bc,
- important);
- if (bc >= 0)
- decl_add(out, nout,
- PROP_BT_T + side,
- btrans, important);
- if (bstyle >= 0)
- decl_add(out, nout,
- PROP_BS_T + side,
- bstyle, important);
- }
- }
- } else if (str_eq(prop, "margin") || str_eq(prop, "padding")) {
- int vals[4], units[4], cpx[4] = { 0, 0, 0, 0 }, nv = 0;
- unsigned int pos = 0;
- int is_pad = prop[0] == 'p';
- unsigned char base = is_pad ? PROP_PT : PROP_MT;
- while (pos < vend - vstart && nv < 4) {
- units[nv] = 0;
- if (parse_margin_calc(s + vstart,
- vend - vstart, &pos,
- &vals[nv], &cpx[nv])) {
- units[nv] = 5;
- } else {
- vals[nv] = parse_margin_len(
- s + vstart, vend - vstart,
- &pos, &units[nv]);
- }
- nv++;
- while (pos < vend - vstart &&
- is_space(s[vstart + pos]))
- pos++;
- }
- if (nv == 1) {
- vals[1] = vals[2] = vals[3] = vals[0];
- units[1] = units[2] = units[3] = units[0];
- cpx[1] = cpx[2] = cpx[3] = cpx[0];
- } else if (nv == 2) {
- vals[2] = vals[0];
- vals[3] = vals[1];
- units[2] = units[0];
- units[3] = units[1];
- cpx[2] = cpx[0];
- cpx[3] = cpx[1];
- } else if (nv == 3) {
- vals[3] = vals[1];
- units[3] = units[1];
- cpx[3] = cpx[1];
- }
- if (nv > 0) {
- int k;
- for (k = 0; k < 4; k++) {
- int v = vals[k];
- int u = units[k];
- int au = u == 4;
- if (au)
- v = 0;
- if (is_pad && au)
- u = 0;
- decl_add(out, nout,
- (unsigned char)(base + k), v,
- important);
- decl_add(out, nout,
- (unsigned char)
- ((is_pad ? PROP_PT_U :
- PROP_MT_U) + k),
- u, important);
- if (u == 5)
- decl_add(out, nout,
- (unsigned char)
- ((is_pad ? PROP_PT_CPX :
- PROP_MT_CPX) + k),
- cpx[k], important);
- if (!is_pad && (k == 1 || k == 3))
- decl_add(out, nout,
- k == 1 ? PROP_MR_AUTO :
- PROP_ML_AUTO,
- au, important);
- }
- }
- } else if (str_eq(prop, "margin-top") ||
- str_eq(prop, "margin-right") ||
- str_eq(prop, "margin-bottom") ||
- str_eq(prop, "margin-left") ||
- str_eq(prop, "padding-top") ||
- str_eq(prop, "padding-right") ||
- str_eq(prop, "padding-bottom") ||
- str_eq(prop, "padding-left")) {
- unsigned int pos = 0;
- int unit = 0;
- int v = 0, cpx = 0;
- unsigned char p = PROP_MT;
- const char *side;
- int au;
- if (parse_margin_calc(s + vstart, vend - vstart,
- &pos, &v, &cpx))
- unit = 5;
- else
- v = parse_margin_len(s + vstart,
- vend - vstart, &pos,
- &unit);
- au = unit == 4;
- if (au)
- v = 0;
- if (prop[0] == 'p') {
- p = PROP_PT;
- if (au)
- unit = 0;
- }
- side = prop + (prop[0] == 'p' ? 8 : 7);
- if (str_eq(side, "right"))
- p = (unsigned char)(p + 1);
- else if (str_eq(side, "bottom"))
- p = (unsigned char)(p + 2);
- else if (str_eq(side, "left"))
- p = (unsigned char)(p + 3);
- decl_add(out, nout, p, v, important);
- decl_add(out, nout,
- (unsigned char)((prop[0] == 'p' ?
- PROP_PT_U : PROP_MT_U) +
- (p - (prop[0] == 'p' ?
- PROP_PT : PROP_MT))),
- unit, important);
- if (unit == 5)
- decl_add(out, nout,
- (unsigned char)((prop[0] == 'p' ?
- PROP_PT_CPX : PROP_MT_CPX) +
- (p - (prop[0] == 'p' ?
- PROP_PT : PROP_MT))),
- cpx, important);
- if (str_eq(side, "right"))
- decl_add(out, nout, PROP_MR_AUTO, au, important);
- else if (str_eq(side, "left"))
- decl_add(out, nout, PROP_ML_AUTO, au, important);
- }
- }
- }
- /* Skip @media/@font-face/@keyframes/@import without descending into their
- * nested blocks. This is the mobile/base rules path: media enhancements are
- * deliberately ignored for now. */
- static unsigned int skip_at_rule(const char *s, unsigned int len,
- unsigned int i)
- {
- unsigned int j = i;
- while (j < len && s[j] != '{' && s[j] != ';')
- j++;
- if (j >= len)
- return len;
- if (s[j] == ';')
- return j + 1;
- j++;
- return css_skip_nested(s, len, j);
- }
- static unsigned int viewport_w = 1024;
- static unsigned short theme_text = 0x0000;
- static unsigned short theme_bg = 0xFFFF;
- static unsigned short theme_panel = 0xEF5D;
- static unsigned short theme_link = 0x001B;
- void br_set_theme(unsigned short text, unsigned short bg,
- unsigned short panel, unsigned short link)
- {
- theme_text = text;
- theme_bg = bg;
- theme_panel = panel;
- theme_link = link;
- }
- void br_set_viewport(unsigned int width)
- {
- viewport_w = width ? width : 1024;
- }
- static unsigned int media_len_px(const char *s, unsigned int len)
- {
- unsigned int i = 0, whole = 0, frac = 0, scale = 1, v;
- int em = 0;
- while (i < len && is_space(s[i]))
- i++;
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- whole = whole * 10 + (unsigned int)(s[i] - '0');
- i++;
- }
- if (i < len && s[i] == '.') {
- i++;
- /* Consume every fractional digit even when there are more
- * than the three we keep: leaving the tail behind made the
- * unit check miss "em" (74.9375em became 74px). */
- while (i < len && s[i] >= '0' && s[i] <= '9') {
- if (scale < 1000) {
- frac = frac * 10 + (unsigned int)(s[i] - '0');
- scale *= 10;
- }
- i++;
- }
- }
- while (i < len && is_space(s[i]))
- i++;
- if (i + 2 <= len && ci_eq_n(s + i, "em", 2)) {
- em = 1;
- i += 2;
- } else if (i + 3 <= len && ci_eq_n(s + i, "rem", 3)) {
- em = 1;
- i += 3;
- } else if (i + 2 <= len && ci_eq_n(s + i, "px", 2)) {
- i += 2;
- }
- v = whole * 100 + (scale > 1 ? frac * 100 / scale : 0);
- if (em)
- return v * 16 / 100;
- return v / 100;
- }
- /* One parenthesised media feature: min-width/max-width and the modern
- * (width <= 600px) form. Unknown features are treated as true. */
- static int media_feature_matches(const char *s, unsigned int len)
- {
- unsigned int i;
- while (len && is_space(*s)) { s++; len--; }
- while (len && is_space(s[len - 1])) len--;
- if (contains_ci_len(s, len, "prefers-color-scheme")) {
- /* There is no user-selectable colour scheme. Ignore both
- * variants so a page's base palette is used; sites whose base
- * is dark (like the project docs) then stay dark instead of
- * flipping to their light media override. */
- return 0;
- }
- if (len >= 9 && ci_eq_n(s, "min-width", 9)) {
- for (i = 9; i < len && s[i] != ':'; i++)
- ;
- if (i < len)
- return viewport_w >= media_len_px(s + i + 1,
- len - i - 1);
- return 1;
- }
- if (len >= 9 && ci_eq_n(s, "max-width", 9)) {
- for (i = 9; i < len && s[i] != ':'; i++)
- ;
- if (i < len)
- return viewport_w <= media_len_px(s + i + 1,
- len - i - 1);
- return 1;
- }
- for (i = 0; i < len; i++) {
- if (s[i] == '<' || s[i] == '>') {
- int max = s[i] == '<';
- unsigned int v = media_len_px(s + i + 1, len - i - 1);
- if (i + 1 < len && s[i + 1] == '=')
- v = media_len_px(s + i + 2, len - i - 2);
- return max ? viewport_w <= v : viewport_w >= v;
- }
- }
- return 1;
- }
- static int media_query_matches(const char *s, unsigned int len)
- {
- unsigned int i;
- if (contains_ci_len(s, len, "print") &&
- !contains_ci_len(s, len, "screen"))
- return 0;
- if (contains_ci_len(s, len, "not "))
- return 0;
- for (i = 0; i < len; i++) {
- if (s[i] == '(') {
- unsigned int st = ++i;
- while (i < len && s[i] != ')')
- i++;
- if (!media_feature_matches(s + st, i - st))
- return 0;
- }
- }
- return 1;
- }
- static int media_list_matches(const char *s, unsigned int len)
- {
- unsigned int i = 0;
- while (i < len) {
- unsigned int st = i;
- while (i < len && s[i] != ',')
- i++;
- if (media_query_matches(s + st, i - st))
- return 1;
- i++;
- }
- return 0;
- }
- static unsigned int rule_hash(const char *s, unsigned int len)
- {
- unsigned int h = 2166136261u, i;
- for (i = 0; i < len; i++) {
- char c = s[i];
- if (c >= 'A' && c <= 'Z')
- c = (char)(c - 'A' + 'a');
- h ^= (unsigned char)c;
- h *= 16777619u;
- }
- return h % BR_RULE_HASH;
- }
- /* Index a parsed rule by the most selective component of its rightmost
- * compound selector. br_style_for() then only visits rules that can
- * possibly match a node instead of scanning every rule. */
- static void rule_index_add(struct br_doc *d, unsigned int idx,
- const char *key, unsigned int len)
- {
- unsigned int i, cpos = 0, clen = 0, ipos = 0, ilen = 0;
- unsigned short *head;
- for (i = 0; i < len; i++) {
- if (key[i] == '.' || key[i] == '#') {
- unsigned int st = i + 1, n = 0;
- while (st + n < len && is_ident(key[st + n]))
- n++;
- if (key[i] == '#') {
- ipos = st;
- ilen = n;
- } else if (n) {
- cpos = st;
- clen = n;
- }
- i = st + n - 1;
- }
- }
- if (ilen)
- head = &d->rule_bucket[rule_hash(key + ipos, ilen)];
- else if (clen)
- head = &d->rule_bucket[rule_hash(key + cpos, clen)];
- else if (len == 5 && ci_eq_n(key, ":root", 5))
- head = &d->rule_tag[BR_TAG_HTML];
- else if (len && is_ident(key[0])) {
- char name[16];
- unsigned int n = 0, tag;
- while (n < len && n + 1 < sizeof(name) && is_ident(key[n])) {
- name[n] = key[n];
- n++;
- }
- name[n] = '\0';
- tag = br_tag_from_name(name);
- if (tag != BR_TAG_UNKNOWN && tag < BR_TAG_MAX)
- head = &d->rule_tag[tag];
- else
- head = &d->rule_any;
- } else {
- head = &d->rule_any;
- }
- d->rule_next[idx] = *head;
- *head = (unsigned short)idx;
- }
- static unsigned int selector_specificity(const char *s, unsigned int len);
- static int css_collect_only;
- /* Simple structural pseudo-classes that can be answered from the node tree.
- * Returns the token length or 0. Kept in sync with compound_match(). */
- static unsigned int structural_pseudo_len(const char *s, unsigned int i,
- unsigned int end)
- {
- static const struct {
- const char *name;
- unsigned char len;
- } p[] = {
- { ":first-child", 12 }, { ":last-child", 11 },
- { ":first-of-type", 14 }, { ":last-of-type", 13 },
- { ":only-child", 11 }, { ":only-of-type", 13 },
- };
- unsigned int k;
- for (k = 0; k < sizeof(p) / sizeof(p[0]); k++) {
- unsigned int l = p[k].len;
- if (i + l <= end && ci_eq_n(s + i, p[k].name, l) &&
- (i + l == end || !is_ident(s[i + l])))
- return l;
- }
- return 0;
- }
- static int structural_pseudo_match(struct br_doc *d, unsigned int node,
- const char *name, unsigned int len)
- {
- unsigned int parent = d->nodes[node].parent;
- unsigned int c;
- int before = 0, after = 0;
- int of_type = contains_ci_len(name, len, "of-type");
- unsigned int tag = d->nodes[node].tag;
- if (!parent)
- /* The root element is the only element child of the
- * document node. */
- return 1;
- for (c = d->nodes[parent].first; c && c != node;
- c = d->nodes[c].next) {
- if (d->nodes[c].type != BR_NODE_ELEM)
- continue;
- if (of_type && d->nodes[c].tag != tag)
- continue;
- before = 1;
- break;
- }
- for (c = d->nodes[node].next; c; c = d->nodes[c].next) {
- if (d->nodes[c].type != BR_NODE_ELEM)
- continue;
- if (of_type && d->nodes[c].tag != tag)
- continue;
- after = 1;
- break;
- }
- if (contains_ci_len(name, len, "first"))
- return !before;
- if (contains_ci_len(name, len, "last"))
- return !after;
- if (contains_ci_len(name, len, "only"))
- return !before && !after;
- return 0;
- }
- static void parse_css(struct br_doc *d, const char *s, unsigned int len)
- {
- unsigned int i = 0;
- while (i < len && d->nrules < d->rule_cap) {
- unsigned int sstart, send, bstart, bend, j;
- struct br_decl decls[BR_DECLS];
- unsigned int nd;
- while (i < len && is_space(s[i]))
- i++;
- if (i + 1 < len && s[i] == '/' && s[i + 1] == '*') {
- i += 2;
- while (i + 1 < len && !(s[i] == '*' && s[i + 1] == '/'))
- i++;
- i += 2;
- continue;
- }
- if (i >= len || s[i] == '}')
- break;
- if (s[i] == '@') {
- if (i + 6 <= len && ci_eq_n(s + i, "@media", 6)) {
- unsigned int q = i + 6;
- while (q < len && s[q] != '{')
- q++;
- if (q < len &&
- media_list_matches(s + i + 6,
- q - (i + 6))) {
- unsigned int b = q + 1;
- unsigned int e = css_skip_nested(s, len, b);
- if (e > b)
- parse_css(d, s + b,
- (e - 1) - b);
- i = e;
- } else {
- i = skip_at_rule(s, len, i);
- }
- continue;
- }
- i = skip_at_rule(s, len, i);
- continue;
- }
- sstart = i;
- while (i < len && s[i] != '{')
- i++;
- if (i >= len)
- break;
- send = i;
- i++;
- bstart = i;
- /* CSS nesting puts @media (and &-rules) inside a rule. Scan
- * to the matching close brace; stopping at the first '}' left
- * the parser out of sync and dropped the rest of the sheet
- * (nd-aktuell lost its desktop rules that way). */
- bend = css_block_end(s, len, i);
- i = bend < len ? bend + 1 : len;
- while (sstart < send && is_space(s[sstart]))
- sstart++;
- while (send > sstart && is_space(s[send - 1]))
- send--;
- if (sstart == send)
- continue;
- if (!css_dark_selector(s + sstart, send - sstart))
- css_collect_block(d, s + bstart, bend - bstart);
- if (css_collect_only)
- continue;
- parse_decls(d, s + bstart, bend - bstart, decls, &nd);
- if (!nd)
- continue;
- /* Split a comma-separated selector list into separate rules. */
- j = sstart;
- while (j <= send && d->nrules < d->rule_cap) {
- unsigned int q = j, e, k;
- char sel[160];
- unsigned int off;
- while (q < send && s[q] != ',')
- q++;
- e = q;
- while (j < e && is_space(s[j]))
- j++;
- while (e > j && is_space(s[e - 1]))
- e--;
- if (e > j) {
- /* Ignore single selectors with sibling combinators,
- * pseudo-classes or attribute tests: they need
- * state/attributes we do not model. The other
- * selectors in the list still apply. */
- for (k = j; k < e; ) {
- if (s[k] == '+' || s[k] == '~')
- break;
- if (s[k] == ':') {
- if (k + 5 <= e &&
- ci_eq_n(s + k, ":root", 5)) {
- k += 5;
- continue;
- }
- if (k + 5 <= e &&
- ci_eq_n(s + k, ":link", 5) &&
- (k + 5 == e ||
- !is_ident(s[k + 5]))) {
- k += 5;
- continue;
- }
- if (k + 5 <= e &&
- ci_eq_n(s + k, ":not(", 5)) {
- unsigned int n = k + 5;
- int nd = 1;
- while (n < e && nd) {
- if (s[n] == '(')
- nd++;
- else if (s[n] == ')')
- nd--;
- n++;
- }
- if (nd)
- break;
- k = n;
- continue;
- }
- {
- unsigned int sl =
- structural_pseudo_len(
- s, k, e);
- if (sl) {
- k += sl;
- continue;
- }
- }
- break;
- }
- k++;
- }
- if (k < e) {
- j = q + 1;
- if (q >= send)
- break;
- continue;
- }
- for (k = 0; k < sizeof(sel) - 1 && j + k < e; k++)
- sel[k] = s[j + k];
- sel[k] = '\0';
- off = store_attr(d, sel, k, 159);
- if (off) {
- struct br_rule *r = &d->rules[d->nrules++];
- unsigned int ks = 0, q;
- for (q = 0; q < k; q++)
- if (sel[q] == ' ' || sel[q] == '>')
- ks = q + 1;
- r->sel_off = off;
- r->key_off = store_attr(d, sel + ks,
- k - ks, 159);
- r->spec = (unsigned short)selector_specificity(sel, k);
- r->single = 1;
- {
- unsigned int z;
- for (z = 0; z < k; z++)
- if (is_space(sel[z]) || sel[z] == '>') {
- r->single = 0;
- break;
- }
- }
- r->nd = (unsigned char)nd;
- for (k = 0; k < nd; k++)
- r->d[k] = decls[k];
- rule_index_add(d, d->nrules - 1,
- attr_ptr(d, r->key_off),
- s_len(attr_ptr(d, r->key_off)));
- }
- }
- j = q + 1;
- if (q >= send)
- break;
- }
- }
- }
- /* Sites often set html { font-size: 62.5% } and then size everything in
- * rem. Detect that root size before parsing the sheet so a rem resolves to
- * the same pixels a real browser would use. */
- static void detect_root_font(const char *s, unsigned int len)
- {
- unsigned int i;
- for (i = 0; i + 4 < len; i++) {
- unsigned int j, end, body, sel, q, h;
- int v = 0, digits = 0, pct = 0, px = 0, frac = 0;
- int exact;
- if (!(s[i] == 'h' && s[i + 1] == 't' && s[i + 2] == 'm' &&
- s[i + 3] == 'l'))
- continue;
- h = i + 4;
- while (h < len && is_space(s[h]))
- h++;
- if (h < len && s[h] != '{' && s[h] != ',' &&
- s[h] != '.' && s[h] != ':' && s[h] != '[')
- continue;
- sel = i;
- while (sel && s[sel - 1] != '}' && s[sel - 1] != '{' &&
- s[sel - 1] != ';' && s[sel - 1] != ',')
- sel--;
- q = sel;
- while (q < i && is_space(s[q]))
- q++;
- exact = (q == i);
- if (!exact && h < len && s[h] != ',' && s[h] != '.')
- continue;
- j = h;
- if (j >= len || s[j] != '{')
- continue;
- while (j < len && s[j] != '{')
- j++;
- if (j >= len)
- break;
- end = j + 1;
- body = end;
- while (end < len && s[end] != '}')
- end++;
- for (; j + 9 < end; j++) {
- if (!(s[j] == 'f' && s[j + 1] == 'o' && s[j + 2] == 'n' &&
- s[j + 3] == 't' && s[j + 4] == '-' &&
- s[j + 5] == 's' && s[j + 6] == 'i' &&
- s[j + 7] == 'z' && s[j + 8] == 'e'))
- continue;
- j += 9;
- while (j < end && s[j] != ':')
- j++;
- if (j >= end)
- break;
- j++;
- while (j < end && is_space(s[j]))
- j++;
- while (j < end && s[j] >= '0' && s[j] <= '9') {
- v = v * 10 + (s[j] - '0');
- digits = 1;
- j++;
- }
- if (j < end && s[j] == '.') {
- j++;
- while (j < end && s[j] >= '0' && s[j] <= '9') {
- v = v * 10 + (s[j] - '0');
- frac++;
- j++;
- }
- }
- while (frac < 2) {
- v *= 10;
- frac++;
- }
- while (frac > 2) {
- v /= 10;
- frac--;
- }
- if (!digits)
- break;
- if (j < end && s[j] == '%')
- pct = 1;
- else if (j + 1 < end && s[j] == 'p' && s[j + 1] == 'x')
- px = 1;
- if (pct)
- v = (v * 16 + 5000) / 10000;
- else if (px)
- v /= 100;
- else
- v = v * 16 / 100;
- if (exact && v >= 8 && v <= 24)
- root_font_px = v;
- if (exact)
- return;
- }
- /* html { font: 10px/1 Verdana } also sets the root size. */
- for (j = body; j + 5 <= end; j++) {
- unsigned int p, ts, te;
- if (!ci_eq_n(s + j, "font:", 5))
- continue;
- p = j + 5;
- while (next_token(s, end, &p, &ts, &te)) {
- int fv = parse_font_px(s + ts, te - ts);
- if (fv >= 8 && fv <= 24) {
- if (exact)
- root_font_px = fv;
- break;
- }
- }
- if (exact)
- return;
- break;
- }
- }
- }
- void br_parse_css_text(struct br_doc *d, const char *css, unsigned int len)
- {
- scan_font_faces(css, len);
- detect_root_font(css, len);
- css_collect_only = 1;
- parse_css(d, css, len);
- css_collect_only = 0;
- parse_css(d, css, len);
- }
- void br_parse_inline_css(struct br_doc *d)
- {
- if (d->inline_css_len) {
- scan_font_faces(css_store, d->inline_css_len);
- detect_root_font(css_store, d->inline_css_len);
- css_collect_only = 1;
- parse_css(d, css_store, d->inline_css_len);
- css_collect_only = 0;
- parse_css(d, css_store, d->inline_css_len);
- }
- d->inline_css_len = 0;
- }
- void br_reset_styles(struct br_doc *d)
- {
- unsigned int i;
- for (i = 0; i < d->nnode; i++)
- d->nodes[i].style_valid = 0;
- }
- /* ---- selector matching ---- */
- static int class_has(struct br_doc *d, unsigned int node, const char *cls,
- unsigned int clen)
- {
- const char *s = attr_ptr(d, d->nodes[node].class_off);
- unsigned int i = 0;
- while (s[i]) {
- unsigned int st, n;
- while (s[i] && is_space(s[i]))
- i++;
- st = i;
- while (s[i] && !is_space(s[i]))
- i++;
- n = i - st;
- if (n == clen && ci_eq_n(s + st, cls, n))
- return 1;
- }
- return 0;
- }
- static int id_eq(struct br_doc *d, unsigned int node, const char *id,
- unsigned int len)
- {
- const char *s = attr_ptr(d, d->nodes[node].id_off);
- unsigned int i;
- for (i = 0; i < len; i++)
- if (s[i] != id[i])
- return 0;
- return s[len] == '\0';
- }
- static int is_ident(char c)
- {
- return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
- (c >= '0' && c <= '9') || c == '-' || c == '_';
- }
- static int attr_is(const char *name, unsigned int nlen, const char *lit)
- {
- unsigned int n = 0;
- while (lit[n])
- n++;
- return n == nlen && ci_eq_n(name, lit, nlen);
- }
- static int attr_present(struct br_doc *d, unsigned int node,
- const char *name, unsigned int nlen)
- {
- struct br_node *n = &d->nodes[node];
- if (attr_is(name, nlen, "data-collapse-target"))
- return (n->attr_flags & BR_ATTR_COLLAPSE_TARGET) != 0;
- if (attr_is(name, nlen, "data-collapse-trigger"))
- return (n->attr_flags & BR_ATTR_COLLAPSE_TRIGGER) != 0;
- if (attr_is(name, nlen, "is-open"))
- return (n->attr_flags & BR_ATTR_IS_OPEN) != 0;
- if (attr_is(name, nlen, "disabled"))
- return (n->attr_flags & BR_ATTR_DISABLED) != 0;
- if (attr_is(name, nlen, "open"))
- return (n->attr_flags & BR_ATTR_OPEN) != 0;
- if (attr_is(name, nlen, "inert"))
- return (n->attr_flags & BR_ATTR_INERT) != 0;
- if (attr_is(name, nlen, "hidden"))
- return (n->flags & BR_NODE_HIDDEN) != 0;
- if (attr_is(name, nlen, "href"))
- return attr_ptr(d, n->href_off)[0] != 0;
- if (attr_is(name, nlen, "class"))
- return attr_ptr(d, n->class_off)[0] != 0;
- if (attr_is(name, nlen, "id"))
- return attr_ptr(d, n->id_off)[0] != 0;
- if (attr_is(name, nlen, "src"))
- return attr_ptr(d, n->src_off)[0] != 0;
- if (attr_is(name, nlen, "alt"))
- return attr_ptr(d, n->alt_off)[0] != 0;
- if (attr_is(name, nlen, "rel"))
- return attr_ptr(d, n->rel_off)[0] != 0;
- return 0;
- }
- static int compound_match(struct br_doc *d, unsigned int node, const char *s,
- unsigned int len)
- {
- unsigned int i = 0;
- int got = 0;
- if (i < len && s[i] == '*') {
- i++;
- got = 1;
- } else if (i < len && is_ident(s[i])) {
- char name[16];
- unsigned int n = 0, tag;
- while (i < len && is_ident(s[i]) && n + 1 < sizeof(name))
- name[n++] = s[i++];
- name[n] = '\0';
- tag = br_tag_from_name(name);
- if (tag == BR_TAG_UNKNOWN) {
- const char *raw = attr_ptr(d,
- d->nodes[node].name_off);
- if (d->nodes[node].tag != BR_TAG_UNKNOWN ||
- !ci_eq_n(raw, name, n) || raw[n] != '\0')
- return 0;
- } else if (tag != d->nodes[node].tag) {
- return 0;
- }
- got = 1;
- }
- while (i < len) {
- if (s[i] == '.') {
- unsigned int st;
- i++;
- st = i;
- while (i < len && is_ident(s[i]))
- i++;
- if (!class_has(d, node, s + st, i - st))
- return 0;
- got = 1;
- } else if (s[i] == '#') {
- unsigned int st;
- i++;
- st = i;
- while (i < len && is_ident(s[i]))
- i++;
- if (!id_eq(d, node, s + st, i - st))
- return 0;
- got = 1;
- } else if (s[i] == '[') {
- unsigned int st, e;
- i++;
- st = e = i;
- while (e < len && s[e] != ']' && s[e] != '=' &&
- !is_space(s[e]))
- e++;
- if (e == st || !attr_present(d, node, s + st, e - st))
- return 0;
- while (i < len && s[i] != ']')
- i++;
- if (i < len)
- i++;
- got = 1;
- } else if (s[i] == ':') {
- if (i + 5 <= len && ci_eq_n(s + i, ":root", 5) &&
- (i + 5 == len || !is_ident(s[i + 5]))) {
- if (d->nodes[node].tag != BR_TAG_HTML)
- return 0;
- i += 5;
- got = 1;
- } else if (i + 5 <= len && ci_eq_n(s + i, ":link", 5) &&
- (i + 5 == len || !is_ident(s[i + 5]))) {
- /* No history state: every link is unvisited. */
- if (d->nodes[node].tag != BR_TAG_A ||
- !d->nodes[node].href_off)
- return 0;
- i += 5;
- got = 1;
- } else if (i + 5 <= len && ci_eq_n(s + i, ":not(", 5)) {
- unsigned int st = i + 5, e = st;
- int depth = 1;
- while (e < len && depth) {
- if (s[e] == '(')
- depth++;
- else if (s[e] == ')')
- depth--;
- if (depth)
- e++;
- }
- if (e >= len)
- return 0;
- for (unsigned int z = st; z < e; z++)
- if (s[z] == ':')
- return 0;
- if (compound_match(d, node, s + st, e - st))
- return 0;
- i = e + 1;
- got = 1;
- } else {
- unsigned int sl = structural_pseudo_len(s, i,
- len);
- if (!sl)
- return 0; /* other pseudo-classes are
- * not modelled */
- if (!structural_pseudo_match(d, node, s + i, sl))
- return 0;
- i += sl;
- got = 1;
- }
- } else {
- i++;
- }
- }
- return got;
- }
- static unsigned int selector_specificity(const char *s, unsigned int len)
- {
- unsigned int i, spec = 0;
- for (i = 0; i < len; i++) {
- if (s[i] == '#')
- spec += 100;
- else if (s[i] == '.' || s[i] == '[' ||
- (s[i] == ':' && !(i + 5 <= len &&
- ci_eq_n(s + i, ":root", 5))))
- spec += 10;
- else if (i == 0 || is_space(s[i - 1]) || s[i - 1] == '>') {
- if (is_ident(s[i]))
- spec += 1;
- }
- }
- return spec;
- }
- /* Match one complex selector; returns specificity or -1. */
- static int selector_match(struct br_doc *d, unsigned int node, const char *s,
- unsigned int len, int right_done)
- {
- struct {
- unsigned int start, end;
- int child;
- } parts[8];
- int np = 0;
- unsigned int i = 0;
- int right;
- while (i < len) {
- int child = 0;
- unsigned int st, e;
- while (i < len && is_space(s[i]))
- i++;
- if (i < len && s[i] == '>') {
- child = 1;
- i++;
- while (i < len && is_space(s[i]))
- i++;
- }
- if (i >= len)
- break;
- st = i;
- while (i < len && !is_space(s[i]) && s[i] != '>')
- i++;
- e = i;
- if (np < 8) {
- parts[np].start = st;
- parts[np].end = e;
- parts[np].child = child;
- np++;
- }
- }
- if (!np)
- return -1;
- if (!right_done &&
- !compound_match(d, node, s + parts[np - 1].start,
- parts[np - 1].end - parts[np - 1].start))
- return -1;
- right = np - 1;
- while (right > 0) {
- unsigned int p = d->nodes[node].parent;
- if (!p)
- return -1;
- if (parts[right].child) {
- if (!compound_match(d, p, s + parts[right - 1].start,
- parts[right - 1].end -
- parts[right - 1].start))
- return -1;
- node = p;
- } else {
- while (p && !compound_match(d, p,
- s + parts[right - 1].start,
- parts[right - 1].end -
- parts[right - 1].start))
- p = d->nodes[p].parent;
- if (!p)
- return -1;
- node = p;
- }
- right--;
- }
- return (int)selector_specificity(s, len);
- }
- #define RULE_MATCH_MAX 512
- struct rule_match {
- unsigned short idx;
- short spec;
- };
- static struct rule_match rule_matches[RULE_MATCH_MAX];
- static unsigned int nrule_matches;
- static void rule_try(struct br_doc *d, unsigned int node, unsigned int idx)
- {
- struct br_rule *r = &d->rules[idx];
- const char *key = attr_ptr(d, r->key_off);
- int spec, key_ok = 0;
- if (key[0]) {
- if (!compound_match(d, node, key, s_len(key)))
- return;
- key_ok = 1;
- }
- spec = (r->single && key_ok) ? (int)r->spec :
- selector_match(d, node, attr_ptr(d, r->sel_off),
- s_len(attr_ptr(d, r->sel_off)), key_ok);
- if (spec < 0)
- return;
- if (nrule_matches < RULE_MATCH_MAX) {
- rule_matches[nrule_matches].idx = (unsigned short)idx;
- rule_matches[nrule_matches].spec = (short)spec;
- nrule_matches++;
- }
- }
- static void rule_scan_list(struct br_doc *d, unsigned int node,
- unsigned short head)
- {
- while (head != 0xFFFF) {
- rule_try(d, node, head);
- head = d->rule_next[head];
- }
- }
- static void rule_scan_class(struct br_doc *d, unsigned int node,
- const char *cls, unsigned int len)
- {
- if (len)
- rule_scan_list(d, node,
- d->rule_bucket[rule_hash(cls, len)]);
- }
- static void rule_collect(struct br_doc *d, unsigned int node)
- {
- const char *cls = attr_ptr(d, d->nodes[node].class_off);
- const char *id = attr_ptr(d, d->nodes[node].id_off);
- unsigned int tag = d->nodes[node].tag;
- unsigned int i;
- nrule_matches = 0;
- rule_scan_list(d, node, d->rule_any);
- if (tag < BR_TAG_MAX)
- rule_scan_list(d, node, d->rule_tag[tag]);
- if (id[0])
- rule_scan_list(d, node,
- d->rule_bucket[rule_hash(id, s_len(id))]);
- i = 0;
- while (cls[i]) {
- unsigned int st, n;
- while (cls[i] && is_space(cls[i]))
- i++;
- st = i;
- while (cls[i] && !is_space(cls[i]))
- i++;
- n = i - st;
- if (n)
- rule_scan_class(d, node, cls + st, n);
- }
- /* Rules must cascade in document order even though the index visits
- * them in bucket order. */
- for (i = 1; i < nrule_matches; i++) {
- struct rule_match m = rule_matches[i];
- unsigned int j = i;
- while (j > 0 && rule_matches[j - 1].idx > m.idx) {
- rule_matches[j] = rule_matches[j - 1];
- j--;
- }
- rule_matches[j] = m;
- }
- }
- static void resolve_size_field(unsigned short *val, unsigned char *kind,
- int font_px)
- {
- if (*kind == 4) {
- *val = (unsigned short)(*val * font_px / 100);
- *kind = 1;
- } else if (*kind == 5) {
- *val = (unsigned short)(*val * root_font_px / 100);
- *kind = 1;
- }
- }
- static void resolve_size_units(struct br_style *st)
- {
- resolve_size_field(&st->width, &st->width_kind, st->font_px);
- resolve_size_field(&st->max_width, &st->max_width_kind, st->font_px);
- resolve_size_field(&st->min_width, &st->min_width_kind, st->font_px);
- resolve_size_field(&st->height, &st->height_kind, st->font_px);
- resolve_size_field(&st->min_height, &st->min_height_kind, st->font_px);
- resolve_size_field(&st->max_height, &st->max_height_kind, st->font_px);
- }
- static void set_border_side_w(struct br_style *st, int side, int v)
- {
- if (v < 0)
- v = 0;
- if (v > 32)
- v = 32;
- st->border_w[side] = (unsigned char)v;
- if (st->border_w[side] && !st->border_c[side])
- st->border_c[side] = st->color;
- }
- static void set_border_side_c(struct br_style *st, int side, int v)
- {
- st->border_c[side] = (unsigned short)v;
- }
- static void apply_decl(struct br_style *st, const struct br_decl *dcl)
- {
- switch (dcl->prop) {
- case PROP_COLOR:
- st->color = (unsigned short)dcl->val;
- break;
- case PROP_BG:
- st->bg = (unsigned short)dcl->val;
- st->has_bg = 1;
- break;
- case PROP_BG_IMAGE:
- st->bg_image = (unsigned short)dcl->val;
- break;
- case PROP_BG_REPEAT:
- st->bg_repeat = (unsigned char)dcl->val;
- break;
- case PROP_BG_POS_X_KIND:
- st->bg_pos_x_kind = (unsigned char)dcl->val;
- break;
- case PROP_BG_POS_X:
- st->bg_pos_x = (signed short)dcl->val;
- break;
- case PROP_BG_POS_Y_KIND:
- st->bg_pos_y_kind = (unsigned char)dcl->val;
- break;
- case PROP_BG_POS_Y:
- st->bg_pos_y = (signed short)dcl->val;
- break;
- case PROP_BG_SIZE_KIND:
- st->bg_size_kind = (unsigned char)dcl->val;
- break;
- case PROP_BG_SIZE_W:
- st->bg_size_w = (unsigned short)dcl->val;
- break;
- case PROP_BG_SIZE_H:
- st->bg_size_h = (unsigned short)dcl->val;
- break;
- case PROP_FONT_PX:
- st->font_raw = (unsigned short)dcl->val;
- break;
- case PROP_FONT_UNIT:
- st->font_unit = (unsigned char)dcl->val;
- break;
- case PROP_BOLD:
- st->bold = (unsigned char)(dcl->val != 0);
- break;
- case PROP_ITALIC:
- st->italic = (unsigned char)(dcl->val != 0);
- break;
- case PROP_MONO:
- st->mono = (unsigned char)(dcl->val != 0);
- break;
- case PROP_FONT_FACE:
- st->font_face = (unsigned char)dcl->val;
- break;
- case PROP_ALIGN:
- st->align = (unsigned char)dcl->val;
- break;
- case PROP_UNDERLINE:
- st->underline = (unsigned char)(dcl->val != 0);
- break;
- case PROP_STRIKE:
- st->strike = (unsigned char)(dcl->val != 0);
- break;
- case PROP_OVERLINE:
- st->overline = (unsigned char)(dcl->val != 0);
- break;
- case PROP_DISPLAY:
- st->display = (unsigned char)(dcl->val &
- ~BR_DISPLAY_INLINE_FLAG);
- st->inline_flex = (dcl->val & BR_DISPLAY_INLINE_FLAG) != 0;
- st->list_item = 0;
- break;
- case PROP_VIS_HIDDEN:
- st->vis_hidden = (unsigned char)(dcl->val != 0);
- break;
- case PROP_OPACITY_HIDDEN:
- st->opacity_hidden = (unsigned char)(dcl->val != 0);
- break;
- case PROP_FILL_MODE:
- st->fill_mode = (unsigned char)dcl->val;
- break;
- case PROP_FILL:
- st->fill = (unsigned short)dcl->val;
- break;
- case PROP_STROKE_MODE:
- st->stroke_mode = (unsigned char)dcl->val;
- break;
- case PROP_STROKE:
- st->stroke = (unsigned short)dcl->val;
- break;
- case PROP_STROKE_W:
- st->stroke_w = (unsigned short)dcl->val;
- break;
- case PROP_FILL_RULE:
- st->fill_evenodd = (unsigned char)(dcl->val != 0);
- break;
- case PROP_OVERFLOW_HIDDEN:
- st->overflow_hidden = (unsigned char)(dcl->val != 0);
- break;
- case PROP_MT: st->mt = dcl->val; break;
- case PROP_MT_CPX: st->mp_calc_px[0] = (signed short)dcl->val; break;
- case PROP_MR_CPX: st->mp_calc_px[1] = (signed short)dcl->val; break;
- case PROP_MB_CPX: st->mp_calc_px[2] = (signed short)dcl->val; break;
- case PROP_ML_CPX: st->mp_calc_px[3] = (signed short)dcl->val; break;
- case PROP_PT_CPX: st->mp_calc_px[4] = (signed short)dcl->val; break;
- case PROP_PR_CPX: st->mp_calc_px[5] = (signed short)dcl->val; break;
- case PROP_PB_CPX: st->mp_calc_px[6] = (signed short)dcl->val; break;
- case PROP_PL_CPX: st->mp_calc_px[7] = (signed short)dcl->val; break;
- case PROP_MR: st->mr = dcl->val; break;
- case PROP_MB: st->mb = dcl->val; break;
- case PROP_ML: st->ml = dcl->val; break;
- case PROP_ML_AUTO: st->ml_auto = (unsigned char)(dcl->val != 0); break;
- case PROP_MR_AUTO: st->mr_auto = (unsigned char)(dcl->val != 0); break;
- case PROP_BOX_SIZING: st->box_sizing = (unsigned char)(dcl->val != 0);
- break;
- case PROP_PT: st->pt = dcl->val; break;
- case PROP_PR: st->pr = dcl->val; break;
- case PROP_PB: st->pb = dcl->val; break;
- case PROP_PL: st->pl = dcl->val; break;
- case PROP_MT_U:
- case PROP_MR_U:
- case PROP_MB_U:
- case PROP_ML_U:
- case PROP_PT_U:
- case PROP_PR_U:
- case PROP_PB_U:
- case PROP_PL_U:
- st->mp_unit[dcl->prop - PROP_MT_U] =
- (unsigned char)dcl->val;
- break;
- case PROP_BORDER_W:
- set_border_side_w(st, 0, dcl->val);
- set_border_side_w(st, 1, dcl->val);
- set_border_side_w(st, 2, dcl->val);
- set_border_side_w(st, 3, dcl->val);
- break;
- case PROP_BORDER_COLOR:
- set_border_side_c(st, 0, dcl->val);
- set_border_side_c(st, 1, dcl->val);
- set_border_side_c(st, 2, dcl->val);
- set_border_side_c(st, 3, dcl->val);
- break;
- case PROP_BW_T: set_border_side_w(st, 0, dcl->val); break;
- case PROP_BW_R: set_border_side_w(st, 1, dcl->val); break;
- case PROP_BW_B: set_border_side_w(st, 2, dcl->val); break;
- case PROP_BW_L: set_border_side_w(st, 3, dcl->val); break;
- case PROP_BC_T: set_border_side_c(st, 0, dcl->val); break;
- case PROP_BC_R: set_border_side_c(st, 1, dcl->val); break;
- case PROP_BC_B: set_border_side_c(st, 2, dcl->val); break;
- case PROP_BC_L: set_border_side_c(st, 3, dcl->val); break;
- case PROP_BS_T: st->border_s[0] = (unsigned char)dcl->val; break;
- case PROP_BS_R: st->border_s[1] = (unsigned char)dcl->val; break;
- case PROP_BS_B: st->border_s[2] = (unsigned char)dcl->val; break;
- case PROP_BS_L: st->border_s[3] = (unsigned char)dcl->val; break;
- case PROP_BT_T: st->border_t[0] = (unsigned char)dcl->val; break;
- case PROP_BT_R: st->border_t[1] = (unsigned char)dcl->val; break;
- case PROP_BT_B: st->border_t[2] = (unsigned char)dcl->val; break;
- case PROP_BT_L: st->border_t[3] = (unsigned char)dcl->val; break;
- case PROP_RADIUS_TL:
- case PROP_RADIUS_TR:
- case PROP_RADIUS_BR:
- case PROP_RADIUS_BL:
- st->radius[dcl->prop - PROP_RADIUS_TL] =
- (unsigned char)dcl->val;
- break;
- case PROP_LIST_NONE:
- st->no_bullet = (unsigned char)(dcl->val != 0);
- break;
- case PROP_LIST_STYLE:
- st->list_style = (unsigned char)dcl->val;
- break;
- case PROP_NOWRAP:
- st->nowrap = (unsigned char)(dcl->val != 0);
- break;
- case PROP_WIDTH:
- st->width = (unsigned short)dcl->val;
- break;
- case PROP_WIDTH_KIND:
- st->width_kind = (unsigned char)dcl->val;
- break;
- case PROP_WIDTH_CALC_PCT:
- st->width_calc_pct = dcl->val;
- break;
- case PROP_WIDTH_CALC_PX:
- st->width_calc_px = dcl->val;
- break;
- case PROP_MAXW:
- st->max_width = (unsigned short)dcl->val;
- break;
- case PROP_MAXW_KIND:
- st->max_width_kind = (unsigned char)dcl->val;
- break;
- case PROP_MAXW_CALC_PCT:
- st->max_width_calc_pct = (signed short)dcl->val;
- break;
- case PROP_MAXW_CALC_PX:
- st->max_width_calc_px = (signed short)dcl->val;
- break;
- case PROP_MINW:
- st->min_width = (unsigned short)dcl->val;
- break;
- case PROP_MINW_KIND:
- st->min_width_kind = (unsigned char)dcl->val;
- break;
- case PROP_MINW_CALC_PCT:
- st->min_width_calc_pct = (signed short)dcl->val;
- break;
- case PROP_MINW_CALC_PX:
- st->min_width_calc_px = (signed short)dcl->val;
- break;
- case PROP_HEIGHT:
- st->height = (unsigned short)dcl->val;
- break;
- case PROP_HEIGHT_KIND:
- st->height_kind = (unsigned char)dcl->val;
- break;
- case PROP_HEIGHT_CALC_PCT:
- st->height_calc_pct = (signed short)dcl->val;
- break;
- case PROP_HEIGHT_CALC_PX:
- st->height_calc_px = (signed short)dcl->val;
- break;
- case PROP_MINH:
- st->min_height = (unsigned short)dcl->val;
- break;
- case PROP_MINH_KIND:
- st->min_height_kind = (unsigned char)dcl->val;
- break;
- case PROP_MINH_CALC_PCT:
- st->min_height_calc_pct = (signed short)dcl->val;
- break;
- case PROP_MINH_CALC_PX:
- st->min_height_calc_px = (signed short)dcl->val;
- break;
- case PROP_MAXH:
- st->max_height = (unsigned short)dcl->val;
- break;
- case PROP_MAXH_KIND:
- st->max_height_kind = (unsigned char)dcl->val;
- break;
- case PROP_MAXH_CALC_PCT:
- st->max_height_calc_pct = (signed short)dcl->val;
- break;
- case PROP_MAXH_CALC_PX:
- st->max_height_calc_px = (signed short)dcl->val;
- break;
- case PROP_LH_PCT:
- st->line_h_pct = (unsigned char)dcl->val;
- break;
- case PROP_LH_PX:
- st->line_h_px = (unsigned short)dcl->val;
- break;
- case PROP_UPPER:
- st->uppercase = (unsigned char)(dcl->val != 0);
- break;
- case PROP_FLOAT:
- st->float_left = (unsigned char)dcl->val;
- break;
- case PROP_CLEAR:
- st->clear = (unsigned char)dcl->val;
- break;
- case PROP_FLEX_GROW:
- st->flex_grow = (unsigned char)dcl->val;
- break;
- case PROP_FLEX_COL:
- st->flex_col = (unsigned char)(dcl->val != 0);
- break;
- case PROP_FLEX_WRAP:
- st->flex_wrap = (unsigned char)(dcl->val != 0);
- break;
- case PROP_JUSTIFY:
- st->justify = (unsigned char)dcl->val;
- break;
- case PROP_ALIGN_ITEMS:
- st->align_items = (unsigned char)dcl->val;
- break;
- case PROP_GRID_NCOLS:
- st->grid_ncols = (unsigned char)dcl->val;
- break;
- case PROP_GRID_GAP:
- st->grid_gap = (unsigned char)dcl->val;
- break;
- case PROP_GRID_SLOT:
- st->grid_slot = (unsigned char)dcl->val;
- break;
- case PROP_GRID_AUTO:
- st->grid_auto = (unsigned char)(dcl->val != 0);
- break;
- case PROP_POS:
- st->pos = (unsigned char)dcl->val;
- break;
- case PROP_LEFT:
- st->left = dcl->val;
- break;
- case PROP_TOP:
- st->top = dcl->val;
- break;
- case PROP_RIGHT:
- st->right = dcl->val;
- break;
- case PROP_BOTTOM:
- st->bottom = dcl->val;
- break;
- case PROP_LEFT_SET:
- st->left_set = (unsigned char)(dcl->val != 0);
- break;
- case PROP_TOP_SET:
- st->top_set = (unsigned char)(dcl->val != 0);
- break;
- case PROP_RIGHT_SET:
- st->right_set = (unsigned char)(dcl->val != 0);
- break;
- case PROP_BOTTOM_SET:
- st->bottom_set = (unsigned char)(dcl->val != 0);
- break;
- case PROP_ZINDEX:
- st->z_index = dcl->val;
- break;
- case PROP_ZSET:
- st->z_set = (unsigned char)(dcl->val != 0);
- break;
- case PROP_TRANSFORM_X:
- st->tx = dcl->val;
- break;
- case PROP_TRANSFORM_Y:
- st->ty = dcl->val;
- break;
- case PROP_TRANSFORM_SET:
- st->transform_set = (unsigned char)(dcl->val != 0);
- break;
- case PROP_TRANSFORM_PAINT:
- st->transform_scale = (unsigned char)(dcl->val != 0);
- break;
- case PROP_SCALE_X:
- st->scale_x = (signed short)dcl->val;
- break;
- case PROP_SCALE_Y:
- st->scale_y = (signed short)dcl->val;
- break;
- case PROP_ROTATE:
- st->rotate_x100 = (signed short)dcl->val;
- break;
- case PROP_ORIGIN_X_KIND:
- st->origin_x_kind = (unsigned char)dcl->val;
- break;
- case PROP_ORIGIN_X:
- st->origin_x = (signed short)dcl->val;
- break;
- case PROP_ORIGIN_Y_KIND:
- st->origin_y_kind = (unsigned char)dcl->val;
- break;
- case PROP_ORIGIN_Y:
- st->origin_y = (signed short)dcl->val;
- break;
- default:
- break;
- }
- }
- static void cascade_decl(struct br_style *st, const struct br_decl *dcl,
- int spec, short *best, short *best_imp)
- {
- unsigned char p = dcl->prop;
- if (p >= PROP_COUNT)
- return;
- if (dcl->important) {
- if (spec >= best_imp[p]) {
- apply_decl(st, dcl);
- best_imp[p] = (short)spec;
- }
- } else if (best_imp[p] < 0 && spec >= best[p]) {
- apply_decl(st, dcl);
- best[p] = (short)spec;
- }
- }
- void br_style_for(struct br_doc *d, unsigned int node,
- const struct br_style *parent, struct br_style *out)
- {
- unsigned int tag;
- short best[PROP_COUNT], best_imp[PROP_COUNT];
- unsigned int i;
- if (d->nodes[node].style_valid) {
- *out = d->nodes[node].style;
- return;
- }
- *out = *parent;
- out->has_bg = 0;
- out->bg_image = 0;
- out->bg_repeat = 0;
- out->bg_pos_x_kind = out->bg_pos_y_kind = 0;
- out->bg_pos_x = out->bg_pos_y = 0;
- out->bg_size_kind = out->bg_size_w = out->bg_size_h = 0;
- out->float_left = 0;
- out->clear = 0;
- out->list_item = 0;
- out->font_unit = 0;
- for (i = 0; i < 4; i++) {
- out->border_w[i] = 0;
- out->border_c[i] = 0;
- out->border_s[i] = BR_BORDER_SOLID;
- out->radius[i] = 0;
- }
- out->width = out->max_width = out->min_width = out->height = 0;
- out->min_height = out->max_height = 0;
- out->width_calc_pct = out->width_calc_px = 0;
- out->height_calc_pct = out->height_calc_px = 0;
- out->min_height_calc_pct = out->min_height_calc_px = 0;
- out->max_height_calc_pct = out->max_height_calc_px = 0;
- out->min_width_calc_pct = out->min_width_calc_px = 0;
- out->max_width_calc_pct = out->max_width_calc_px = 0;
- out->width_kind = out->max_width_kind = out->min_width_kind = 0;
- out->height_kind = out->min_height_kind = out->max_height_kind = 0;
- out->box_sizing = 0;
- out->ml_auto = out->mr_auto = 0;
- out->mt = out->mr = out->mb = out->ml = 0;
- out->pt = out->pr = out->pb = out->pl = 0;
- for (i = 0; i < 8; i++) {
- out->mp_unit[i] = 0;
- out->mp_calc_px[i] = 0;
- }
- out->flex_grow = out->flex_col = out->flex_wrap = 0;
- out->justify = out->align_items = 0;
- out->grid_ncols = out->grid_gap = out->grid_slot = 0;
- out->grid_auto = 0;
- out->pos = out->left_set = out->top_set = 0;
- out->right_set = out->bottom_set = 0;
- out->left = out->top = out->right = out->bottom = 0;
- out->z_index = 0;
- out->z_set = 0;
- out->tx = out->ty = 0;
- out->transform_set = 0;
- out->transform_scale = 0;
- out->scale_x = out->scale_y = 100;
- out->rotate_x100 = 0;
- out->origin_x = out->origin_y = 50;
- out->origin_x_kind = out->origin_y_kind = 1;
- out->vis_hidden = out->opacity_hidden = 0;
- out->inline_flex = 0;
- out->overflow_hidden = 0;
- tag = d->nodes[node].tag;
- out->display = br_tag_is_block(tag) ? BR_DISPLAY_BLOCK :
- BR_DISPLAY_INLINE;
- for (i = 0; i < PROP_COUNT; i++) {
- best[i] = -1;
- best_imp[i] = -1;
- }
- switch (tag) {
- case BR_TAG_HEAD:
- case BR_TAG_TITLE:
- case BR_TAG_META:
- case BR_TAG_LINK:
- case BR_TAG_BASE:
- case BR_TAG_SCRIPT:
- case BR_TAG_STYLE:
- out->display = BR_DISPLAY_NONE;
- break;
- case BR_TAG_BODY:
- out->mt = out->mb = out->ml = out->mr = 4;
- break;
- case BR_TAG_P:
- out->mt = out->mb = 8;
- break;
- case BR_TAG_H1:
- out->font_px = 32; out->bold = 1;
- out->mt = 10; out->mb = 6;
- break;
- case BR_TAG_H2:
- out->font_px = 24; out->bold = 1;
- out->mt = 8; out->mb = 4;
- break;
- case BR_TAG_H3: case BR_TAG_H4: case BR_TAG_H5: case BR_TAG_H6:
- out->bold = 1; out->mt = 6; out->mb = 3;
- if (tag == BR_TAG_H3)
- out->font_px = 19;
- else if (tag == BR_TAG_H4)
- out->font_px = 17;
- else if (tag == BR_TAG_H5)
- out->font_px = 15;
- else
- out->font_px = 13;
- break;
- case BR_TAG_UL: case BR_TAG_OL:
- out->mt = out->mb = 6; out->pl = 20;
- break;
- case BR_TAG_LI:
- out->mt = out->mb = 2;
- out->list_item = 1;
- break;
- case BR_TAG_BLOCKQUOTE:
- out->mt = out->mb = 6; out->ml = out->mr = 16;
- break;
- case BR_TAG_PRE:
- out->mt = out->mb = 8; out->pl = 8;
- out->has_bg = 1; out->bg = theme_panel;
- out->mono = 1;
- break;
- case BR_TAG_CENTER:
- out->align = BR_ALIGN_CENTER;
- break;
- case BR_TAG_A:
- out->color = theme_link; out->underline = 1;
- break;
- case BR_TAG_B: case BR_TAG_STRONG:
- out->bold = 1;
- break;
- case BR_TAG_I: case BR_TAG_EM:
- out->italic = 1;
- break;
- case BR_TAG_U:
- out->underline = 1;
- break;
- case BR_TAG_S: case BR_TAG_STRIKE:
- out->strike = 1;
- break;
- case BR_TAG_CODE:
- out->color = 0x8010;
- out->mono = 1;
- break;
- case BR_TAG_SMALL:
- out->font_px = 13;
- break;
- case BR_TAG_BIG:
- out->font_px = 20;
- break;
- case BR_TAG_TD: case BR_TAG_TH:
- out->pr = 12;
- /* A cell establishes left-aligned content even inside a legacy
- * <center> (HN's outer page wrapper); author CSS/align below
- * can still override this. */
- out->align = BR_ALIGN_LEFT;
- break;
- case BR_TAG_VIDEO:
- out->display = BR_DISPLAY_BLOCK;
- break;
- case BR_TAG_SVG:
- /* The renderer treats an SVG as one replaced box; keeping it
- * block-level also gives it a usable intrinsic width inside
- * flex items such as a header logo. */
- out->display = BR_DISPLAY_BLOCK;
- break;
- default:
- break;
- }
- /* Legacy bgcolor/align attributes (tables/cells/body) are
- * presentational hints; author CSS still wins because it is applied
- * below. */
- {
- unsigned short bgc;
- const char *bga = br_find_attr(d, node, "bgcolor");
- const char *ala = br_find_attr(d, node, "align");
- if (bga && parse_color(bga, s_len(bga), &bgc)) {
- out->bg = bgc;
- out->has_bg = 1;
- }
- if (ala) {
- if (same_string(ala, "right"))
- out->align = BR_ALIGN_RIGHT;
- else if (same_string(ala, "center"))
- out->align = BR_ALIGN_CENTER;
- else if (same_string(ala, "left"))
- out->align = BR_ALIGN_LEFT;
- }
- }
- /* presentational <font color>: author CSS below still wins */
- if (d->nodes[node].color_off) {
- unsigned short c;
- const char *v = attr_ptr(d, d->nodes[node].color_off);
- if (parse_color(v, s_len(v), &c))
- out->color = c;
- }
- /* SVG presentation attributes; CSS declarations still win below. */
- if (in_svg(d, node)) {
- const char *fa = br_find_attr(d, node, "fill");
- const char *sa = br_find_attr(d, node, "stroke");
- const char *wa = br_find_attr(d, node, "stroke-width");
- const char *ra = br_find_attr(d, node, "fill-rule");
- const char *ca = br_find_attr(d, node, "color");
- unsigned short c;
- int mode;
- if (ca && parse_color(ca, s_len(ca), &c))
- out->color = c;
- if (fa && (mode = paint_mode(fa, s_len(fa), &c)) != 0) {
- out->fill_mode = (unsigned char)mode;
- if (mode == BR_PAINT_COLOR)
- out->fill = c;
- }
- if (sa && (mode = paint_mode(sa, s_len(sa), &c)) != 0) {
- out->stroke_mode = (unsigned char)mode;
- if (mode == BR_PAINT_COLOR)
- out->stroke = c;
- }
- if (wa) {
- int sw = svg_len16(wa, s_len(wa));
- if (sw >= 0)
- out->stroke_w = (unsigned short)sw;
- }
- if (ra)
- out->fill_evenodd = same_string(ra, "evenodd");
- }
- /* UA behaviour for a closed <details>: only its <summary> is shown.
- * Author CSS below can still override this. */
- if (tag != BR_TAG_SUMMARY && d->nodes[node].parent) {
- unsigned int p = d->nodes[node].parent;
- if (p < d->nnode && d->nodes[p].tag == BR_TAG_DETAILS &&
- !br_find_attr(d, p, "open"))
- out->display = BR_DISPLAY_NONE;
- }
- rule_collect(d, node);
- for (i = 0; i < nrule_matches; i++) {
- struct br_rule *r = &d->rules[rule_matches[i].idx];
- int spec = rule_matches[i].spec;
- unsigned int k;
- for (k = 0; k < r->nd; k++)
- cascade_decl(out, &r->d[k], spec, best, best_imp);
- }
- if (d->nodes[node].style_off) {
- struct br_decl decls[BR_DECLS];
- unsigned int nd, k;
- const char *v = attr_ptr(d, d->nodes[node].style_off);
- parse_decls(d, v, s_len(v), decls, &nd);
- for (k = 0; k < nd; k++)
- cascade_decl(out, &decls[k], 1000, best, best_imp);
- }
- if (out->font_unit) {
- int px = out->font_raw;
- if (out->font_unit == 2)
- px = parent->font_px * out->font_raw / 100;
- else if (out->font_unit == 3)
- px = root_font_px * out->font_raw / 100;
- else if (out->font_unit == 4)
- px = parent->font_px * out->font_raw / 10000;
- if (px < 8)
- px = 8;
- if (px > 72)
- px = 72;
- out->font_px = (unsigned char)px;
- }
- resolve_size_units(out);
- if (d->nodes[node].flags & BR_NODE_HIDDEN)
- out->display = BR_DISPLAY_NONE;
- if (out->vis_hidden || out->opacity_hidden ||
- (out->overflow_hidden && out->height_kind == 1 &&
- out->height == 0))
- out->display = BR_DISPLAY_NONE;
- d->nodes[node].style = *out;
- d->nodes[node].style_valid = 1;
- }
- /* ---- HTML parser ---- */
- static void stack_recount_pre(struct br_doc *d, unsigned short *stack,
- unsigned int sp, int *pre)
- {
- unsigned int i;
- *pre = 0;
- for (i = 1; i <= sp; i++)
- if (d->nodes[stack[i]].tag == BR_TAG_PRE)
- (*pre)++;
- }
- static void implicit_close(struct br_doc *d, unsigned short *stack,
- unsigned int *sp, unsigned int tag)
- {
- while (*sp > 0) {
- unsigned int top = d->nodes[stack[*sp]].tag;
- if (top == BR_TAG_P && br_tag_is_block(tag)) {
- (*sp)--;
- continue;
- }
- if (top == BR_TAG_LI && tag == BR_TAG_LI) {
- (*sp)--;
- continue;
- }
- if ((top == BR_TAG_TD || top == BR_TAG_TH) &&
- (tag == BR_TAG_TD || tag == BR_TAG_TH || tag == BR_TAG_TR)) {
- (*sp)--;
- continue;
- }
- if (top == BR_TAG_TR && tag == BR_TAG_TR) {
- (*sp)--;
- continue;
- }
- break;
- }
- }
- static int has_attr_token(const char *s, unsigned int len, const char *name)
- {
- unsigned int i, n = s_len(name);
- for (i = 0; i + n <= len; i++) {
- if (i && !is_space(s[i - 1]) && s[i - 1] != '/')
- continue;
- if (!ci_eq_n(s + i, name, n))
- continue;
- if (i + n == len || is_space(s[i + n]) || s[i + n] == '=')
- return 1;
- }
- return 0;
- }
- static int stack_has_svg(const struct br_doc *d,
- const unsigned short *stack, unsigned int sp)
- {
- unsigned int k;
- for (k = 0; k <= sp && k < 64; k++)
- if (d->nodes[stack[k]].tag == BR_TAG_SVG)
- return 1;
- return 0;
- }
- /* Skip an element subtree by finding its closing tag. Used for SVG
- * <title>/<desc>/<metadata> metadata, which must not enter the flow or
- * overwrite the document title. */
- static unsigned int skip_element(const char *html, unsigned int len,
- unsigned int i, const char *name,
- unsigned int n)
- {
- char close[40];
- unsigned int k = 0, p;
- close[k++] = '<';
- close[k++] = '/';
- while (k < sizeof(close) - 1 && n--)
- close[k++] = *name++;
- p = (unsigned int)find_ci(html, len, close, k, i);
- if ((int)p < 0)
- return len;
- p += k;
- while (p < len && html[p] != '>')
- p++;
- if (p < len)
- p++;
- return p;
- }
- /* Store a <script src=...> value in the attribute arena. Kept separate from
- * parse_attrs() so script-only attributes cannot be confused with the
- * value/d attributes that also use src_off on other elements. */
- static unsigned int script_src_attr(struct br_doc *d, const char *s,
- unsigned int len)
- {
- unsigned int i = 0;
- while (i < len) {
- unsigned int ns, ne, vs, ve;
- char quote = 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i >= len || s[i] == '/')
- break;
- ns = i;
- while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '>')
- i++;
- ne = i;
- while (i < len && is_space(s[i]))
- i++;
- if (i >= len || s[i] != '=')
- continue;
- i++;
- while (i < len && is_space(s[i]))
- i++;
- if (i < len && (s[i] == '"' || s[i] == '\'')) {
- quote = s[i++];
- vs = i;
- while (i < len && s[i] != quote)
- i++;
- ve = i;
- if (i < len)
- i++;
- } else {
- vs = i;
- while (i < len && !is_space(s[i]) && s[i] != '>')
- i++;
- ve = i;
- }
- if (ne > ns && ci_eq_n(s + ns, "src", ne - ns))
- return store_attr(d, s + vs, ve - vs, 400);
- }
- return 0;
- }
- /* JavaScript only: skip structured-data/template script blocks (JSON-LD,
- * application/json, text/template, ...) and non-JS type values. External
- * src scripts are allowed; the browser fetches them separately. */
- static int script_is_javascript(const char *s, unsigned int len)
- {
- unsigned int i = 0;
- while (i < len) {
- unsigned int ns, ne, vs = 0, ve = 0;
- char quote = 0;
- int has_val = 0;
- while (i < len && is_space(s[i]))
- i++;
- if (i >= len)
- break;
- ns = i;
- while (i < len && !is_space(s[i]) && s[i] != '=' && s[i] != '/')
- i++;
- ne = i;
- while (i < len && is_space(s[i]))
- i++;
- if (i < len && s[i] == '=') {
- i++;
- while (i < len && is_space(s[i]))
- i++;
- if (i < len && (s[i] == '"' || s[i] == '\'')) {
- quote = s[i++];
- vs = i;
- while (i < len && s[i] != quote)
- i++;
- ve = i;
- if (i < len)
- i++;
- } else {
- vs = i;
- while (i < len && !is_space(s[i]) && s[i] != '/')
- i++;
- ve = i;
- }
- has_val = 1;
- } else if (i == ne) {
- i++;
- }
- if (ne > ns && ci_eq_n(s + ns, "type", ne - ns)) {
- unsigned int vn = has_val ? ve - vs : 0;
- if (vn == 0)
- return 1;
- if ((vn == 15 && ci_eq_n(s + vs, "text/javascript", 15)) ||
- (vn == 22 &&
- ci_eq_n(s + vs, "application/javascript", 22)) ||
- (vn == 15 && ci_eq_n(s + vs, "text/ecmascript", 15)) ||
- (vn == 22 &&
- ci_eq_n(s + vs, "application/ecmascript", 22)))
- return 1;
- return 0;
- }
- }
- return 1;
- }
- int br_parse(struct br_doc *d, const char *html, unsigned int len)
- {
- unsigned short stack[64];
- unsigned int sp = 0;
- unsigned int i = 0;
- int pre = 0;
- unsigned int css_len = 0;
- if (!d)
- return -1;
- if (!d->nodes)
- br_doc_init(d);
- if (!d->nodes || !d->text || !d->attrs)
- return -1;
- doc_reset(d);
- d->script_src = html;
- stack[0] = 0;
- d->nodes[0].type = BR_NODE_ELEM;
- d->nodes[0].tag = BR_TAG_UNKNOWN;
- while (i < len) {
- if (html[i] != '<') {
- unsigned int st = i;
- while (i < len && html[i] != '<')
- i++;
- if (sp <= 63)
- append_text(d, stack[sp], html + st, i - st, pre);
- continue;
- }
- if (i + 3 < len && html[i + 1] == '!' && html[i + 2] == '-' &&
- html[i + 3] == '-') {
- int p = find_ci(html, len, "-->", 3, i + 4);
- i = p >= 0 ? (unsigned int)p + 3 : len;
- continue;
- }
- if (i + 1 < len && (html[i + 1] == '!' || html[i + 1] == '?')) {
- while (i < len && html[i] != '>')
- i++;
- if (i < len)
- i++;
- continue;
- }
- if (i + 1 < len && html[i + 1] == '/') {
- unsigned int ns = i + 2, ne, tag, k, n;
- char name[32];
- while (ns < len && is_space(html[ns]))
- ns++;
- ne = ns;
- while (ne < len && is_ident(html[ne]))
- ne++;
- if (ne == ns) {
- i++;
- continue;
- }
- n = ne - ns;
- if (n >= sizeof(name))
- n = sizeof(name) - 1;
- for (k = 0; k < n; k++)
- name[k] = html[ns + k];
- name[n] = '\0';
- tag = br_tag_from_name(name);
- i = ne;
- while (i < len && html[i] != '>')
- i++;
- if (i < len)
- i++;
- for (k = sp; k > 0; k--) {
- struct br_node *nd = &d->nodes[stack[k]];
- if (tag == BR_TAG_UNKNOWN) {
- const char *raw = attr_ptr(d, nd->name_off);
- if (nd->tag == BR_TAG_UNKNOWN &&
- ci_eq_n(raw, name, n) &&
- raw[n] == '\0') {
- sp = k - 1;
- break;
- }
- } else if (nd->tag == tag) {
- sp = k - 1;
- break;
- }
- }
- stack_recount_pre(d, stack, sp, &pre);
- continue;
- }
- /* start tag */
- {
- unsigned int ns = i + 1, ne, tag, k, n;
- char name[16];
- unsigned int attr_start, attr_end;
- int selfclose = 0;
- while (ns < len && is_space(html[ns]))
- ns++;
- ne = ns;
- while (ne < len && (is_ident(html[ne]) || html[ne] == ':'))
- ne++;
- if (ne == ns) {
- /* stray '<' */
- append_text(d, stack[sp], html + i, 1, pre);
- i++;
- continue;
- }
- n = ne - ns;
- if (n >= sizeof(name))
- n = sizeof(name) - 1;
- for (k = 0; k < n; k++)
- name[k] = html[ns + k];
- name[n] = '\0';
- tag = br_tag_from_name(name);
- attr_start = ne;
- {
- unsigned int q = ne;
- char quote = 0;
- while (q < len) {
- char c = html[q];
- if (quote) {
- if (c == quote)
- quote = 0;
- } else if (c == '"' || c == '\'') {
- quote = c;
- } else if (c == '>') {
- break;
- }
- q++;
- }
- attr_end = q;
- if (q > attr_start && html[q - 1] == '/')
- selfclose = 1;
- i = q < len ? q + 1 : len;
- }
- if (tag == BR_TAG_SCRIPT) {
- int p = find_ci(html, len, "</script", 8, i);
- unsigned int end = p >= 0 ? (unsigned int)p : len;
- unsigned int idx;
- if (script_is_javascript(html + attr_start,
- attr_end - attr_start)) {
- if (d->nscripts < BR_MAX_SCRIPTS) {
- struct br_script *script =
- &d->scripts[d->nscripts++];
- script->off = i;
- script->len = end > i ? end - i : 0;
- script->src_off = script_src_attr(
- d, html + attr_start,
- attr_end - attr_start);
- } else {
- d->scripts_truncated = 1;
- }
- }
- /* Keep the element in the DOM even though its body is
- * executed separately: scripts (Piwik, analytics)
- * insert a sibling after getElementsByTagName('script')
- * [0], and the element must exist for that to work. */
- idx = node_append(d, stack[sp], BR_NODE_ELEM,
- (unsigned char)tag);
- if (idx)
- parse_attrs(d, &d->nodes[idx],
- html + attr_start,
- attr_end - attr_start);
- i = p >= 0 ? (unsigned int)p + 8 : len;
- while (i < len && html[i] != '>')
- i++;
- if (i < len)
- i++;
- continue;
- }
- if (stack_has_svg(d, stack, sp) &&
- (tag == BR_TAG_TITLE ||
- (tag == BR_TAG_UNKNOWN &&
- (ci_eq_n(name, "desc", 4) ||
- ci_eq_n(name, "metadata", 8))))) {
- i = skip_element(html, len, i, name, n);
- continue;
- }
- /* A closed <details> is collapsed at style time, not
- * parse time: the <summary> (the hamburger button on
- * tagesschau and many mobile menus) must stay in the
- * DOM. */
- if (tag == BR_TAG_STYLE) {
- int p = find_ci(html, len, "</style", 7, i);
- unsigned int end = p >= 0 ? (unsigned int)p : len;
- unsigned int cend = end;
- unsigned int idx;
- while (cend > i && is_space(html[cend - 1]))
- cend--;
- if (css_len + (cend - i) + 2 < BR_CSS_SIZE) {
- for (k = i; k < cend; k++)
- css_store[css_len++] = html[k];
- css_store[css_len++] = '\n';
- }
- /* The CSS is collected above, but keep a display:none
- * element so getElementsByTagName('style')/parentNode
- * behave like a real document. */
- idx = node_append(d, stack[sp], BR_NODE_ELEM,
- (unsigned char)tag);
- if (idx)
- parse_attrs(d, &d->nodes[idx],
- html + attr_start,
- attr_end - attr_start);
- i = p >= 0 ? (unsigned int)p + 7 : len;
- while (i < len && html[i] != '>')
- i++;
- if (i < len)
- i++;
- continue;
- }
- if (tag == BR_TAG_TITLE) {
- int p = find_ci(html, len, "</title", 7, i);
- unsigned int end = p >= 0 ? (unsigned int)p : len;
- unsigned int tn = 0;
- char tmp[BR_TITLE_MAX];
- tn = decode_text(html + i, end - i, tmp,
- sizeof(tmp), 1);
- (void)tn;
- s_copy(d->title, tmp, sizeof(d->title));
- i = p >= 0 ? (unsigned int)p + 7 : len;
- while (i < len && html[i] != '>')
- i++;
- if (i < len)
- i++;
- continue;
- }
- if (tag == BR_TAG_BR || tag == BR_TAG_HR ||
- tag == BR_TAG_IMG || br_tag_is_void(tag)) {
- unsigned int idx;
- implicit_close(d, stack, &sp, tag);
- idx = node_append(d, stack[sp], BR_NODE_ELEM,
- (unsigned char)tag);
- if (idx)
- parse_attrs(d, &d->nodes[idx],
- html + attr_start,
- attr_end - attr_start);
- continue;
- }
- if (sp >= 63) {
- d->truncated = 1;
- continue;
- }
- implicit_close(d, stack, &sp, tag);
- {
- unsigned int idx = node_append(d, stack[sp],
- BR_NODE_ELEM,
- (unsigned char)tag);
- if (!idx)
- continue;
- parse_attrs(d, &d->nodes[idx],
- html + attr_start,
- attr_end - attr_start);
- if (tag == BR_TAG_UNKNOWN)
- d->nodes[idx].name_off =
- store_attr(d, name, n, 31);
- if (!selfclose) {
- sp++;
- stack[sp] = (unsigned short)idx;
- if (tag == BR_TAG_PRE)
- pre++;
- }
- }
- }
- }
- d->inline_css_len = css_len;
- return 0;
- }
- int br_parse_plain(struct br_doc *d, const char *text, unsigned int len)
- {
- if (!d->nodes)
- br_doc_init(d);
- if (!d->nodes || !d->text || !d->attrs)
- return -1;
- doc_reset(d);
- d->plain = 1;
- d->nodes[0].type = BR_NODE_ELEM;
- d->nodes[0].tag = BR_TAG_UNKNOWN;
- append_text(d, 0, text, len, 1);
- return 0;
- }
- /* A standalone image response: a synthetic document with one <img>. The
- * caller decodes the already-fetched body into image slot 0 and sets the
- * returned node's img_index. */
- unsigned int br_parse_image_doc(struct br_doc *d, const char *src)
- {
- unsigned int idx;
- if (!d->nodes)
- br_doc_init(d);
- if (!d->nodes || !d->text || !d->attrs)
- return 0;
- doc_reset(d);
- d->nodes[0].type = BR_NODE_ELEM;
- d->nodes[0].tag = BR_TAG_UNKNOWN;
- idx = node_append(d, 0, BR_NODE_ELEM, BR_TAG_IMG);
- if (!idx)
- return 0;
- d->nodes[idx].src_off = store_attr(d, src, s_len(src), 220);
- return idx;
- }
|