browser_html.c 209 KB

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