browser_html.c 176 KB

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