browser_html.c 151 KB

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