browser_html.c 178 KB

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