browser_html.c 179 KB

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