browser_html.c 154 KB

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