browser_html.c 185 KB

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