1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226 |
- /*
- * Copyright (c) 2018-2024, Andreas Kling <andreas@ladybird.org>
- * Copyright (c) 2020-2021, the SerenityOS developers.
- * Copyright (c) 2021-2024, Sam Atkins <sam@ladybird.org>
- * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
- * Copyright (c) 2022, MacDue <macdue@dueutil.tech>
- * Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
- * Copyright (c) 2024, Tommy van der Vorst <tommy@pixelspark.nl>
- * Copyright (c) 2024, Matthew Olsson <mattco@serenityos.org>
- * Copyright (c) 2024, Glenn Skrzypczak <glenn.skrzypczak@gmail.com>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
- #include <AK/CharacterTypes.h>
- #include <AK/Debug.h>
- #include <AK/GenericLexer.h>
- #include <AK/NonnullRawPtr.h>
- #include <AK/SourceLocation.h>
- #include <AK/TemporaryChange.h>
- #include <LibWeb/CSS/CSSStyleDeclaration.h>
- #include <LibWeb/CSS/CSSStyleSheet.h>
- #include <LibWeb/CSS/CSSStyleValue.h>
- #include <LibWeb/CSS/CalculatedOr.h>
- #include <LibWeb/CSS/EdgeRect.h>
- #include <LibWeb/CSS/MediaList.h>
- #include <LibWeb/CSS/Parser/Parser.h>
- #include <LibWeb/CSS/PropertyName.h>
- #include <LibWeb/CSS/Selector.h>
- #include <LibWeb/CSS/Sizing.h>
- #include <LibWeb/CSS/StyleComputer.h>
- #include <LibWeb/CSS/StyleValues/AngleStyleValue.h>
- #include <LibWeb/CSS/StyleValues/BackgroundRepeatStyleValue.h>
- #include <LibWeb/CSS/StyleValues/BackgroundSizeStyleValue.h>
- #include <LibWeb/CSS/StyleValues/BasicShapeStyleValue.h>
- #include <LibWeb/CSS/StyleValues/BorderRadiusStyleValue.h>
- #include <LibWeb/CSS/StyleValues/CSSColor.h>
- #include <LibWeb/CSS/StyleValues/CSSColorValue.h>
- #include <LibWeb/CSS/StyleValues/CSSHSL.h>
- #include <LibWeb/CSS/StyleValues/CSSHWB.h>
- #include <LibWeb/CSS/StyleValues/CSSKeywordValue.h>
- #include <LibWeb/CSS/StyleValues/CSSLCHLike.h>
- #include <LibWeb/CSS/StyleValues/CSSLabLike.h>
- #include <LibWeb/CSS/StyleValues/CSSRGB.h>
- #include <LibWeb/CSS/StyleValues/ContentStyleValue.h>
- #include <LibWeb/CSS/StyleValues/CounterDefinitionsStyleValue.h>
- #include <LibWeb/CSS/StyleValues/CounterStyleValue.h>
- #include <LibWeb/CSS/StyleValues/CustomIdentStyleValue.h>
- #include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>
- #include <LibWeb/CSS/StyleValues/EasingStyleValue.h>
- #include <LibWeb/CSS/StyleValues/EdgeStyleValue.h>
- #include <LibWeb/CSS/StyleValues/FilterValueListStyleValue.h>
- #include <LibWeb/CSS/StyleValues/FlexStyleValue.h>
- #include <LibWeb/CSS/StyleValues/FrequencyStyleValue.h>
- #include <LibWeb/CSS/StyleValues/GridAutoFlowStyleValue.h>
- #include <LibWeb/CSS/StyleValues/GridTemplateAreaStyleValue.h>
- #include <LibWeb/CSS/StyleValues/GridTrackPlacementStyleValue.h>
- #include <LibWeb/CSS/StyleValues/GridTrackSizeListStyleValue.h>
- #include <LibWeb/CSS/StyleValues/ImageStyleValue.h>
- #include <LibWeb/CSS/StyleValues/IntegerStyleValue.h>
- #include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
- #include <LibWeb/CSS/StyleValues/MathDepthStyleValue.h>
- #include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
- #include <LibWeb/CSS/StyleValues/OpenTypeTaggedStyleValue.h>
- #include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
- #include <LibWeb/CSS/StyleValues/PositionStyleValue.h>
- #include <LibWeb/CSS/StyleValues/RatioStyleValue.h>
- #include <LibWeb/CSS/StyleValues/RectStyleValue.h>
- #include <LibWeb/CSS/StyleValues/ResolutionStyleValue.h>
- #include <LibWeb/CSS/StyleValues/RotationStyleValue.h>
- #include <LibWeb/CSS/StyleValues/ScaleStyleValue.h>
- #include <LibWeb/CSS/StyleValues/ScrollbarGutterStyleValue.h>
- #include <LibWeb/CSS/StyleValues/ShadowStyleValue.h>
- #include <LibWeb/CSS/StyleValues/ShorthandStyleValue.h>
- #include <LibWeb/CSS/StyleValues/StringStyleValue.h>
- #include <LibWeb/CSS/StyleValues/StyleValueList.h>
- #include <LibWeb/CSS/StyleValues/TimeStyleValue.h>
- #include <LibWeb/CSS/StyleValues/TransformationStyleValue.h>
- #include <LibWeb/CSS/StyleValues/TransitionStyleValue.h>
- #include <LibWeb/CSS/StyleValues/TranslationStyleValue.h>
- #include <LibWeb/CSS/StyleValues/URLStyleValue.h>
- #include <LibWeb/CSS/StyleValues/UnresolvedStyleValue.h>
- #include <LibWeb/Dump.h>
- #include <LibWeb/HTML/HTMLImageElement.h>
- #include <LibWeb/Infra/CharacterTypes.h>
- #include <LibWeb/Infra/Strings.h>
- static void log_parse_error(SourceLocation const& location = SourceLocation::current())
- {
- dbgln_if(CSS_PARSER_DEBUG, "Parse error (CSS) {}", location);
- }
- namespace Web::CSS::Parser {
- Parser Parser::create(ParsingContext const& context, StringView input, StringView encoding)
- {
- auto tokens = Tokenizer::tokenize(input, encoding);
- return Parser { context, move(tokens) };
- }
- Parser::Parser(ParsingContext const& context, Vector<Token> tokens)
- : m_context(context)
- , m_tokens(move(tokens))
- , m_token_stream(m_tokens)
- {
- }
- Parser::Parser(Parser&& other)
- : m_context(other.m_context)
- , m_tokens(move(other.m_tokens))
- , m_token_stream(m_tokens)
- {
- // Moving the TokenStream directly from `other` would break it, because TokenStream holds
- // a reference to the Vector<Token>, so it would be pointing at the old Parser's tokens.
- // So instead, we create a new TokenStream from this Parser's tokens, and then tell it to
- // copy the other TokenStream's state. This is quite hacky.
- m_token_stream.copy_state({}, other.m_token_stream);
- }
- // https://drafts.csswg.org/css-syntax/#parse-stylesheet
- template<typename T>
- Parser::ParsedStyleSheet Parser::parse_a_stylesheet(TokenStream<T>& input, Optional<URL::URL> location)
- {
- // To parse a stylesheet from an input given an optional url location:
- // 1. If input is a byte stream for a stylesheet, decode bytes from input, and set input to the result.
- // 2. Normalize input, and set input to the result.
- // NOTE: These are done automatically when creating the Parser.
- // 3. Create a new stylesheet, with its location set to location (or null, if location was not passed).
- ParsedStyleSheet style_sheet;
- style_sheet.location = move(location);
- // 4. Consume a stylesheet’s contents from input, and set the stylesheet’s rules to the result.
- style_sheet.rules = consume_a_stylesheets_contents(input);
- // 5. Return the stylesheet.
- return style_sheet;
- }
- // https://drafts.csswg.org/css-syntax/#parse-a-stylesheets-contents
- template<typename T>
- Vector<Rule> Parser::parse_a_stylesheets_contents(TokenStream<T>& input)
- {
- // To parse a stylesheet’s contents from input:
- // 1. Normalize input, and set input to the result.
- // NOTE: This is done automatically when creating the Parser.
- // 2. Consume a stylesheet’s contents from input, and return the result.
- return consume_a_stylesheets_contents(input);
- }
- // https://drafts.csswg.org/css-syntax/#parse-a-css-stylesheet
- CSSStyleSheet* Parser::parse_as_css_stylesheet(Optional<URL::URL> location)
- {
- // To parse a CSS stylesheet, first parse a stylesheet.
- auto const& style_sheet = parse_a_stylesheet(m_token_stream, {});
- // Interpret all of the resulting top-level qualified rules as style rules, defined below.
- GC::MarkedVector<CSSRule*> rules(m_context.realm().heap());
- for (auto const& raw_rule : style_sheet.rules) {
- auto rule = convert_to_rule(raw_rule, Nested::No);
- // If any style rule is invalid, or any at-rule is not recognized or is invalid according to its grammar or context, it’s a parse error.
- // Discard that rule.
- if (!rule) {
- log_parse_error();
- continue;
- }
- rules.append(rule);
- }
- auto rule_list = CSSRuleList::create(m_context.realm(), rules);
- auto media_list = MediaList::create(m_context.realm(), {});
- return CSSStyleSheet::create(m_context.realm(), rule_list, media_list, move(location));
- }
- RefPtr<Supports> Parser::parse_as_supports()
- {
- return parse_a_supports(m_token_stream);
- }
- template<typename T>
- RefPtr<Supports> Parser::parse_a_supports(TokenStream<T>& tokens)
- {
- auto component_values = parse_a_list_of_component_values(tokens);
- TokenStream<ComponentValue> token_stream { component_values };
- m_rule_context.append(ContextType::SupportsCondition);
- auto maybe_condition = parse_supports_condition(token_stream);
- m_rule_context.take_last();
- token_stream.discard_whitespace();
- if (maybe_condition && !token_stream.has_next_token())
- return Supports::create(m_context.realm(), maybe_condition.release_nonnull());
- return {};
- }
- template RefPtr<Supports> Parser::parse_a_supports(TokenStream<ComponentValue>&);
- template RefPtr<Supports> Parser::parse_a_supports(TokenStream<Token>&);
- OwnPtr<Supports::Condition> Parser::parse_supports_condition(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- tokens.discard_whitespace();
- auto const& peeked_token = tokens.next_token();
- // `not <supports-in-parens>`
- if (peeked_token.is_ident("not"sv)) {
- tokens.discard_a_token();
- tokens.discard_whitespace();
- auto child = parse_supports_in_parens(tokens);
- if (!child.has_value())
- return {};
- transaction.commit();
- auto condition = make<Supports::Condition>();
- condition->type = Supports::Condition::Type::Not;
- condition->children.append(child.release_value());
- return condition;
- }
- // ` <supports-in-parens> [ and <supports-in-parens> ]*
- // | <supports-in-parens> [ or <supports-in-parens> ]*`
- Vector<Supports::InParens> children;
- Optional<Supports::Condition::Type> condition_type {};
- auto as_condition_type = [](auto& token) -> Optional<Supports::Condition::Type> {
- if (!token.is(Token::Type::Ident))
- return {};
- auto ident = token.token().ident();
- if (ident.equals_ignoring_ascii_case("and"sv))
- return Supports::Condition::Type::And;
- if (ident.equals_ignoring_ascii_case("or"sv))
- return Supports::Condition::Type::Or;
- return {};
- };
- while (tokens.has_next_token()) {
- if (!children.is_empty()) {
- // Expect `and` or `or` here
- auto maybe_combination = as_condition_type(tokens.consume_a_token());
- if (!maybe_combination.has_value())
- return {};
- if (!condition_type.has_value()) {
- condition_type = maybe_combination.value();
- } else if (maybe_combination != condition_type) {
- return {};
- }
- }
- tokens.discard_whitespace();
- if (auto in_parens = parse_supports_in_parens(tokens); in_parens.has_value()) {
- children.append(in_parens.release_value());
- } else {
- return {};
- }
- tokens.discard_whitespace();
- }
- if (children.is_empty())
- return {};
- transaction.commit();
- auto condition = make<Supports::Condition>();
- condition->type = condition_type.value_or(Supports::Condition::Type::Or);
- condition->children = move(children);
- return condition;
- }
- Optional<Supports::InParens> Parser::parse_supports_in_parens(TokenStream<ComponentValue>& tokens)
- {
- // `( <supports-condition> )`
- auto const& first_token = tokens.next_token();
- if (first_token.is_block() && first_token.block().is_paren()) {
- auto transaction = tokens.begin_transaction();
- tokens.discard_a_token();
- tokens.discard_whitespace();
- TokenStream child_tokens { first_token.block().value };
- if (auto condition = parse_supports_condition(child_tokens)) {
- if (child_tokens.has_next_token())
- return {};
- transaction.commit();
- return Supports::InParens {
- .value = { condition.release_nonnull() }
- };
- }
- }
- // `<supports-feature>`
- if (auto feature = parse_supports_feature(tokens); feature.has_value()) {
- return Supports::InParens {
- .value = { feature.release_value() }
- };
- }
- // `<general-enclosed>`
- if (auto general_enclosed = parse_general_enclosed(tokens); general_enclosed.has_value()) {
- return Supports::InParens {
- .value = general_enclosed.release_value()
- };
- }
- return {};
- }
- Optional<Supports::Feature> Parser::parse_supports_feature(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- tokens.discard_whitespace();
- auto const& first_token = tokens.consume_a_token();
- // `<supports-decl>`
- if (first_token.is_block() && first_token.block().is_paren()) {
- TokenStream block_tokens { first_token.block().value };
- // FIXME: Parsing and then converting back to a string is weird.
- if (auto declaration = consume_a_declaration(block_tokens); declaration.has_value()) {
- transaction.commit();
- return Supports::Feature {
- Supports::Declaration { declaration->to_string() }
- };
- }
- }
- // `<supports-selector-fn>`
- if (first_token.is_function("selector"sv)) {
- // FIXME: Parsing and then converting back to a string is weird.
- StringBuilder builder;
- for (auto const& item : first_token.function().value)
- builder.append(item.to_string());
- transaction.commit();
- return Supports::Feature {
- Supports::Selector { builder.to_string().release_value_but_fixme_should_propagate_errors() }
- };
- }
- return {};
- }
- // https://www.w3.org/TR/mediaqueries-4/#typedef-general-enclosed
- Optional<GeneralEnclosed> Parser::parse_general_enclosed(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- tokens.discard_whitespace();
- auto const& first_token = tokens.consume_a_token();
- // `[ <function-token> <any-value>? ) ]`
- if (first_token.is_function()) {
- transaction.commit();
- return GeneralEnclosed { first_token.to_string() };
- }
- // `( <any-value>? )`
- if (first_token.is_block() && first_token.block().is_paren()) {
- transaction.commit();
- return GeneralEnclosed { first_token.to_string() };
- }
- return {};
- }
- // https://drafts.csswg.org/css-syntax/#consume-stylesheet-contents
- template<typename T>
- Vector<Rule> Parser::consume_a_stylesheets_contents(TokenStream<T>& input)
- {
- // To consume a stylesheet’s contents from a token stream input:
- // Let rules be an initially empty list of rules.
- Vector<Rule> rules;
- // Process input:
- for (;;) {
- auto& token = input.next_token();
- // <whitespace-token>
- if (token.is(Token::Type::Whitespace)) {
- // Discard a token from input.
- input.discard_a_token();
- continue;
- }
- // <EOF-token>
- if (token.is(Token::Type::EndOfFile)) {
- // Return rules.
- return rules;
- }
- // <CDO-token>
- // <CDC-token>
- if (token.is(Token::Type::CDO) || token.is(Token::Type::CDC)) {
- // Discard a token from input.
- input.discard_a_token();
- continue;
- }
- // <at-keyword-token>
- if (token.is(Token::Type::AtKeyword)) {
- // Consume an at-rule from input. If anything is returned, append it to rules.
- if (auto maybe_at_rule = consume_an_at_rule(input); maybe_at_rule.has_value())
- rules.append(*maybe_at_rule);
- continue;
- }
- // anything else
- {
- // Consume a qualified rule from input. If a rule is returned, append it to rules.
- consume_a_qualified_rule(input).visit(
- [&](QualifiedRule qualified_rule) { rules.append(move(qualified_rule)); },
- [](auto&) {});
- }
- }
- }
- // https://drafts.csswg.org/css-syntax/#consume-at-rule
- template<typename T>
- Optional<AtRule> Parser::consume_an_at_rule(TokenStream<T>& input, Nested nested)
- {
- // To consume an at-rule from a token stream input, given an optional bool nested (default false):
- // Assert: The next token is an <at-keyword-token>.
- VERIFY(input.next_token().is(Token::Type::AtKeyword));
- // Consume a token from input, and let rule be a new at-rule with its name set to the returned token’s value,
- // its prelude initially set to an empty list, and no declarations or child rules.
- AtRule rule {
- .name = ((Token)input.consume_a_token()).at_keyword(),
- .prelude = {},
- .child_rules_and_lists_of_declarations = {},
- };
- // Process input:
- for (;;) {
- auto& token = input.next_token();
- // <semicolon-token>
- // <EOF-token>
- if (token.is(Token::Type::Semicolon) || token.is(Token::Type::EndOfFile)) {
- // Discard a token from input. If rule is valid in the current context, return it; otherwise return nothing.
- input.discard_a_token();
- if (is_valid_in_the_current_context(rule))
- return rule;
- return {};
- }
- // <}-token>
- if (token.is(Token::Type::CloseCurly)) {
- // If nested is true:
- if (nested == Nested::Yes) {
- // If rule is valid in the current context, return it.
- if (is_valid_in_the_current_context(rule))
- return rule;
- // Otherwise, return nothing.
- return {};
- }
- // Otherwise, consume a token and append the result to rule’s prelude.
- else {
- rule.prelude.append(input.consume_a_token());
- }
- continue;
- }
- // <{-token>
- if (token.is(Token::Type::OpenCurly)) {
- // Consume a block from input, and assign the result to rule’s child rules.
- m_rule_context.append(context_type_for_at_rule(rule.name));
- rule.child_rules_and_lists_of_declarations = consume_a_block(input);
- m_rule_context.take_last();
- // If rule is valid in the current context, return it. Otherwise, return nothing.
- if (is_valid_in_the_current_context(rule))
- return rule;
- return {};
- }
- // anything else
- {
- // Consume a component value from input and append the returned value to rule’s prelude.
- rule.prelude.append(consume_a_component_value(input));
- }
- }
- }
- // https://drafts.csswg.org/css-syntax/#consume-qualified-rule
- template<typename T>
- Variant<Empty, QualifiedRule, Parser::InvalidRuleError> Parser::consume_a_qualified_rule(TokenStream<T>& input, Optional<Token::Type> stop_token, Nested nested)
- {
- // To consume a qualified rule, from a token stream input, given an optional token stop token and an optional bool nested (default false):
- // Let rule be a new qualified rule with its prelude, declarations, and child rules all initially set to empty lists.
- QualifiedRule rule {
- .prelude = {},
- .declarations = {},
- .child_rules = {},
- };
- // NOTE: Qualified rules inside @keyframes are a keyframe rule.
- // We'll assume all others are style rules.
- auto type_of_qualified_rule = (!m_rule_context.is_empty() && m_rule_context.last() == ContextType::AtKeyframes)
- ? ContextType::Keyframe
- : ContextType::Style;
- // Process input:
- for (;;) {
- auto& token = input.next_token();
- // <EOF-token>
- // stop token (if passed)
- if (token.is(Token::Type::EndOfFile) || (stop_token.has_value() && token.is(*stop_token))) {
- // This is a parse error. Return nothing.
- log_parse_error();
- return {};
- }
- // <}-token>
- if (token.is(Token::Type::CloseCurly)) {
- // This is a parse error. If nested is true, return nothing. Otherwise, consume a token and append the result to rule’s prelude.
- log_parse_error();
- if (nested == Nested::Yes)
- return {};
- rule.prelude.append(input.consume_a_token());
- continue;
- }
- // <{-token>
- if (token.is(Token::Type::OpenCurly)) {
- // If the first two non-<whitespace-token> values of rule’s prelude are an <ident-token> whose value starts with "--"
- // followed by a <colon-token>, then:
- TokenStream prelude_tokens { rule.prelude };
- prelude_tokens.discard_whitespace();
- auto& first_non_whitespace = prelude_tokens.consume_a_token();
- prelude_tokens.discard_whitespace();
- auto& second_non_whitespace = prelude_tokens.consume_a_token();
- if (first_non_whitespace.is(Token::Type::Ident) && first_non_whitespace.token().ident().starts_with_bytes("--"sv)
- && second_non_whitespace.is(Token::Type::Colon)) {
- // If nested is true, consume the remnants of a bad declaration from input, with nested set to true, and return nothing.
- if (nested == Nested::Yes) {
- consume_the_remnants_of_a_bad_declaration(input, Nested::Yes);
- return {};
- }
- // If nested is false, consume a block from input, and return nothing.
- (void)consume_a_block(input);
- return {};
- }
- // Otherwise, consume a block from input, and let child rules be the result.
- m_rule_context.append(type_of_qualified_rule);
- rule.child_rules = consume_a_block(input);
- m_rule_context.take_last();
- // If the first item of child rules is a list of declarations, remove it from child rules and assign it to rule’s declarations.
- if (!rule.child_rules.is_empty() && rule.child_rules.first().has<Vector<Declaration>>()) {
- auto first = rule.child_rules.take_first();
- rule.declarations = move(first.get<Vector<Declaration>>());
- }
- // If any remaining items of child rules are lists of declarations, replace them with nested declarations rules
- // containing the list as its sole child. Assign child rules to rule’s child rules.
- // NOTE: We do this later, when converting the QualifiedRule to a CSSRule type.
- // If rule is valid in the current context, return it; otherwise return an invalid rule error.
- if (is_valid_in_the_current_context(rule))
- return rule;
- return InvalidRuleError {};
- }
- // anything else
- {
- // Consume a component value from input and append the result to rule’s prelude.
- rule.prelude.append(consume_a_component_value(input));
- }
- }
- }
- // https://drafts.csswg.org/css-syntax/#consume-block
- template<typename T>
- Vector<RuleOrListOfDeclarations> Parser::consume_a_block(TokenStream<T>& input)
- {
- // To consume a block, from a token stream input:
- // Assert: The next token is a <{-token>.
- VERIFY(input.next_token().is(Token::Type::OpenCurly));
- // Discard a token from input.
- input.discard_a_token();
- // Consume a block’s contents from input and let rules be the result.
- auto rules = consume_a_blocks_contents(input);
- // Discard a token from input.
- input.discard_a_token();
- // Return rules.
- return rules;
- }
- // https://drafts.csswg.org/css-syntax/#consume-block-contents
- template<typename T>
- Vector<RuleOrListOfDeclarations> Parser::consume_a_blocks_contents(TokenStream<T>& input)
- {
- // To consume a block’s contents from a token stream input:
- // Let rules be an empty list, containing either rules or lists of declarations.
- Vector<RuleOrListOfDeclarations> rules;
- // Let decls be an empty list of declarations.
- Vector<Declaration> declarations;
- // Process input:
- for (;;) {
- auto& token = input.next_token();
- // <whitespace-token>
- // <semicolon-token>
- if (token.is(Token::Type::Whitespace) || token.is(Token::Type::Semicolon)) {
- // Discard a token from input.
- input.discard_a_token();
- continue;
- }
- // <EOF-token>
- // <}-token>
- if (token.is(Token::Type::EndOfFile) || token.is(Token::Type::CloseCurly)) {
- // AD-HOC: If decls is not empty, append it to rules.
- // Spec issue: https://github.com/w3c/csswg-drafts/issues/11017
- if (!declarations.is_empty())
- rules.append(move(declarations));
- // Return rules.
- return rules;
- }
- // <at-keyword-token>
- if (token.is(Token::Type::AtKeyword)) {
- // If decls is not empty, append it to rules, and set decls to a fresh empty list of declarations.
- if (!declarations.is_empty()) {
- rules.append(move(declarations));
- declarations = {};
- }
- // Consume an at-rule from input, with nested set to true.
- // If a rule was returned, append it to rules.
- if (auto at_rule = consume_an_at_rule(input, Nested::Yes); at_rule.has_value())
- rules.append({ at_rule.release_value() });
- continue;
- }
- // anything else
- {
- // Mark input.
- input.mark();
- // Consume a declaration from input, with nested set to true.
- // If a declaration was returned, append it to decls, and discard a mark from input.
- if (auto declaration = consume_a_declaration(input, Nested::Yes); declaration.has_value()) {
- declarations.append(declaration.release_value());
- input.discard_a_mark();
- }
- // Otherwise, restore a mark from input, then consume a qualified rule from input,
- // with nested set to true, and <semicolon-token> as the stop token.
- else {
- input.restore_a_mark();
- consume_a_qualified_rule(input, Token::Type::Semicolon, Nested::Yes).visit(
- // -> If nothing was returned
- [](Empty&) {
- // Do nothing
- },
- // -> If an invalid rule error was returned
- [&](InvalidRuleError&) {
- // If decls is not empty, append decls to rules, and set decls to a fresh empty list of declarations. (Otherwise, do nothing.)
- if (!declarations.is_empty()) {
- rules.append(move(declarations));
- declarations = {};
- }
- },
- // -> If a rule was returned
- [&](QualifiedRule rule) {
- // If decls is not empty, append decls to rules, and set decls to a fresh empty list of declarations.
- if (!declarations.is_empty()) {
- rules.append(move(declarations));
- declarations = {};
- }
- // Append the rule to rules.
- rules.append({ move(rule) });
- });
- }
- }
- }
- }
- template<>
- ComponentValue Parser::consume_a_component_value<ComponentValue>(TokenStream<ComponentValue>& tokens)
- {
- // Note: This overload is called once tokens have already been converted into component values,
- // so we do not need to do the work in the more general overload.
- return tokens.consume_a_token();
- }
- // 5.4.7. Consume a component value
- // https://drafts.csswg.org/css-syntax/#consume-component-value
- template<typename T>
- ComponentValue Parser::consume_a_component_value(TokenStream<T>& input)
- {
- // To consume a component value from a token stream input:
- // Process input:
- for (;;) {
- auto& token = input.next_token();
- // <{-token>
- // <[-token>
- // <(-token>
- if (token.is(Token::Type::OpenCurly) || token.is(Token::Type::OpenSquare) || token.is(Token::Type::OpenParen)) {
- // Consume a simple block from input and return the result.
- return ComponentValue { consume_a_simple_block(input) };
- }
- // <function-token>
- if (token.is(Token::Type::Function)) {
- // Consume a function from input and return the result.
- return ComponentValue { consume_a_function(input) };
- }
- // anything else
- {
- // Consume a token from input and return the result.
- return ComponentValue { input.consume_a_token() };
- }
- }
- }
- template<typename T>
- Vector<ComponentValue> Parser::consume_a_list_of_component_values(TokenStream<T>& input, Optional<Token::Type> stop_token, Nested nested)
- {
- // To consume a list of component values from a token stream input, given an optional token stop token
- // and an optional boolean nested (default false):
- // Let values be an empty list of component values.
- Vector<ComponentValue> values;
- // Process input:
- for (;;) {
- auto& token = input.next_token();
- // <eof-token>
- // stop token (if passed)
- if (token.is(Token::Type::EndOfFile) || (stop_token.has_value() && token.is(*stop_token))) {
- // Return values.
- return values;
- }
- // <}-token>
- if (token.is(Token::Type::CloseCurly)) {
- // If nested is true, return values.
- if (nested == Nested::Yes) {
- return values;
- }
- // Otherwise, this is a parse error. Consume a token from input and append the result to values.
- else {
- log_parse_error();
- values.append(input.consume_a_token());
- }
- }
- // anything else
- {
- // Consume a component value from input, and append the result to values.
- values.append(consume_a_component_value(input));
- }
- }
- }
- template Vector<ComponentValue> Parser::consume_a_list_of_component_values(TokenStream<ComponentValue>& input, Optional<Token::Type> stop_token, Nested nested);
- template Vector<ComponentValue> Parser::consume_a_list_of_component_values(TokenStream<Token>& input, Optional<Token::Type> stop_token, Nested nested);
- // https://drafts.csswg.org/css-syntax/#consume-simple-block
- template<typename T>
- SimpleBlock Parser::consume_a_simple_block(TokenStream<T>& input)
- {
- // To consume a simple block from a token stream input:
- // Assert: the next token of input is <{-token>, <[-token>, or <(-token>.
- auto& next = input.next_token();
- VERIFY(next.is(Token::Type::OpenCurly) || next.is(Token::Type::OpenSquare) || next.is(Token::Type::OpenParen));
- // Let ending token be the mirror variant of the next token. (E.g. if it was called with <[-token>, the ending token is <]-token>.)
- auto ending_token = ((Token)input.next_token()).mirror_variant();
- // Let block be a new simple block with its associated token set to the next token and with its value initially set to an empty list.
- SimpleBlock block {
- .token = input.next_token(),
- .value = {},
- };
- // Discard a token from input.
- input.discard_a_token();
- // Process input:
- for (;;) {
- auto& token = input.next_token();
- // <eof-token>
- // ending token
- if (token.is(Token::Type::EndOfFile) || token.is(ending_token)) {
- // Discard a token from input. Return block.
- // AD-HOC: Store the token instead as the "end token"
- block.end_token = input.consume_a_token();
- return block;
- }
- // anything else
- {
- // Consume a component value from input and append the result to block’s value.
- block.value.empend(move(consume_a_component_value(input)));
- }
- }
- }
- // https://drafts.csswg.org/css-syntax/#consume-function
- template<typename T>
- Function Parser::consume_a_function(TokenStream<T>& input)
- {
- // To consume a function from a token stream input:
- // Assert: The next token is a <function-token>.
- VERIFY(input.next_token().is(Token::Type::Function));
- // Consume a token from input, and let function be a new function with its name equal the returned token’s value,
- // and a value set to an empty list.
- auto name_token = ((Token)input.consume_a_token());
- Function function {
- .name = name_token.function(),
- .value = {},
- .name_token = name_token,
- };
- // Process input:
- for (;;) {
- auto& token = input.next_token();
- // <eof-token>
- // <)-token>
- if (token.is(Token::Type::EndOfFile) || token.is(Token::Type::CloseParen)) {
- // Discard a token from input. Return function.
- // AD-HOC: Store the token instead as the "end token"
- function.end_token = input.consume_a_token();
- return function;
- }
- // anything else
- {
- // Consume a component value from input and append the result to function’s value.
- function.value.append(consume_a_component_value(input));
- }
- }
- }
- // https://drafts.csswg.org/css-syntax/#consume-declaration
- template<typename T>
- Optional<Declaration> Parser::consume_a_declaration(TokenStream<T>& input, Nested nested)
- {
- // To consume a declaration from a token stream input, given an optional bool nested (default false):
- // TODO: As noted in the "Implementation note" below https://drafts.csswg.org/css-syntax/#consume-block-contents
- // there are ways we can optimise this by early-exiting.
- // Let decl be a new declaration, with an initially empty name and a value set to an empty list.
- Declaration declaration {
- .name {},
- .value {},
- };
- // 1. If the next token is an <ident-token>, consume a token from input and set decl’s name to the token’s value.
- if (input.next_token().is(Token::Type::Ident)) {
- declaration.name = ((Token)input.consume_a_token()).ident();
- }
- // Otherwise, consume the remnants of a bad declaration from input, with nested, and return nothing.
- else {
- consume_the_remnants_of_a_bad_declaration(input, nested);
- return {};
- }
- // 2. Discard whitespace from input.
- input.discard_whitespace();
- // 3. If the next token is a <colon-token>, discard a token from input.
- if (input.next_token().is(Token::Type::Colon)) {
- input.discard_a_token();
- }
- // Otherwise, consume the remnants of a bad declaration from input, with nested, and return nothing.
- else {
- consume_the_remnants_of_a_bad_declaration(input, nested);
- return {};
- }
- // 4. Discard whitespace from input.
- input.discard_whitespace();
- // 5. Consume a list of component values from input, with nested, and with <semicolon-token> as the stop token,
- // and set decl’s value to the result.
- declaration.value = consume_a_list_of_component_values(input, Token::Type::Semicolon, nested);
- // 6. If the last two non-<whitespace-token>s in decl’s value are a <delim-token> with the value "!"
- // followed by an <ident-token> with a value that is an ASCII case-insensitive match for "important",
- // remove them from decl’s value and set decl’s important flag.
- if (declaration.value.size() >= 2) {
- // NOTE: Walk backwards from the end until we find "important"
- Optional<size_t> important_index;
- for (size_t i = declaration.value.size() - 1; i > 0; i--) {
- auto const& value = declaration.value[i];
- if (value.is_ident("important"sv)) {
- important_index = i;
- break;
- }
- if (!value.is(Token::Type::Whitespace))
- break;
- }
- // NOTE: Walk backwards from important until we find "!"
- if (important_index.has_value()) {
- Optional<size_t> bang_index;
- for (size_t i = important_index.value() - 1; i > 0; i--) {
- auto const& value = declaration.value[i];
- if (value.is_delim('!')) {
- bang_index = i;
- break;
- }
- if (value.is(Token::Type::Whitespace))
- continue;
- break;
- }
- if (bang_index.has_value()) {
- declaration.value.remove(important_index.value());
- declaration.value.remove(bang_index.value());
- declaration.important = Important::Yes;
- }
- }
- }
- // 7. While the last item in decl’s value is a <whitespace-token>, remove that token.
- while (!declaration.value.is_empty() && declaration.value.last().is(Token::Type::Whitespace)) {
- declaration.value.take_last();
- }
- // See second clause of step 8.
- auto contains_a_curly_block_and_non_whitespace = [](Vector<ComponentValue> const& declaration_value) {
- bool contains_curly_block = false;
- bool contains_non_whitespace = false;
- for (auto const& value : declaration_value) {
- if (value.is_block() && value.block().is_curly()) {
- if (contains_non_whitespace)
- return true;
- contains_curly_block = true;
- continue;
- }
- if (!value.is(Token::Type::Whitespace)) {
- if (contains_curly_block)
- return true;
- contains_non_whitespace = true;
- continue;
- }
- }
- return false;
- };
- // 8. If decl’s name is a custom property name string, then set decl’s original text to the segment
- // of the original source text string corresponding to the tokens of decl’s value.
- if (is_a_custom_property_name_string(declaration.name)) {
- // TODO: If we could reach inside the source string that the TokenStream uses, we could grab this as
- // a single substring instead of having to reconstruct it.
- StringBuilder original_text;
- for (auto const& value : declaration.value) {
- original_text.append(value.original_source_text());
- }
- declaration.original_text = original_text.to_string_without_validation();
- }
- // Otherwise, if decl’s value contains a top-level simple block with an associated token of <{-token>,
- // and also contains any other non-<whitespace-token> value, return nothing.
- // (That is, a top-level {}-block is only allowed as the entire value of a non-custom property.)
- else if (contains_a_curly_block_and_non_whitespace(declaration.value)) {
- return {};
- }
- // Otherwise, if decl’s name is an ASCII case-insensitive match for "unicode-range", consume the value of
- // a unicode-range descriptor from the segment of the original source text string corresponding to the
- // tokens returned by the consume a list of component values call, and replace decl’s value with the result.
- else if (declaration.name.equals_ignoring_ascii_case("unicode-range"sv)) {
- // FIXME: Special unicode-range handling
- }
- // 9. If decl is valid in the current context, return it; otherwise return nothing.
- if (is_valid_in_the_current_context(declaration))
- return declaration;
- return {};
- }
- // https://drafts.csswg.org/css-syntax/#consume-the-remnants-of-a-bad-declaration
- template<typename T>
- void Parser::consume_the_remnants_of_a_bad_declaration(TokenStream<T>& input, Nested nested)
- {
- // To consume the remnants of a bad declaration from a token stream input, given a bool nested:
- // Process input:
- for (;;) {
- auto& token = input.next_token();
- // <eof-token>
- // <semicolon-token>
- if (token.is(Token::Type::EndOfFile) || token.is(Token::Type::Semicolon)) {
- // Discard a token from input, and return nothing.
- input.discard_a_token();
- return;
- }
- // <}-token>
- if (token.is(Token::Type::CloseCurly)) {
- // If nested is true, return nothing. Otherwise, discard a token.
- if (nested == Nested::Yes)
- return;
- input.discard_a_token();
- continue;
- }
- // anything else
- {
- // Consume a component value from input, and do nothing.
- (void)consume_a_component_value(input);
- continue;
- }
- }
- }
- CSSRule* Parser::parse_as_css_rule()
- {
- if (auto maybe_rule = parse_a_rule(m_token_stream); maybe_rule.has_value())
- return convert_to_rule(maybe_rule.value(), Nested::No);
- return {};
- }
- // https://drafts.csswg.org/css-syntax/#parse-rule
- template<typename T>
- Optional<Rule> Parser::parse_a_rule(TokenStream<T>& input)
- {
- // To parse a rule from input:
- Optional<Rule> rule;
- // 1. Normalize input, and set input to the result.
- // NOTE: This is done when initializing the Parser.
- // 2. Discard whitespace from input.
- input.discard_whitespace();
- // 3. If the next token from input is an <EOF-token>, return a syntax error.
- if (input.next_token().is(Token::Type::EndOfFile)) {
- return {};
- }
- // Otherwise, if the next token from input is an <at-keyword-token>,
- // consume an at-rule from input, and let rule be the return value.
- else if (input.next_token().is(Token::Type::AtKeyword)) {
- rule = consume_an_at_rule(m_token_stream).map([](auto& it) { return Rule { it }; });
- }
- // Otherwise, consume a qualified rule from input and let rule be the return value.
- // If nothing or an invalid rule error was returned, return a syntax error.
- else {
- consume_a_qualified_rule(input).visit(
- [&](QualifiedRule qualified_rule) { rule = move(qualified_rule); },
- [](auto&) {});
- if (!rule.has_value())
- return {};
- }
- // 4. Discard whitespace from input.
- input.discard_whitespace();
- // 5. If the next token from input is an <EOF-token>, return rule. Otherwise, return a syntax error.
- if (input.next_token().is(Token::Type::EndOfFile))
- return rule;
- return {};
- }
- // https://drafts.csswg.org/css-syntax/#parse-block-contents
- template<typename T>
- Vector<RuleOrListOfDeclarations> Parser::parse_a_blocks_contents(TokenStream<T>& input)
- {
- // To parse a block’s contents from input:
- // 1. Normalize input, and set input to the result.
- // NOTE: Done by constructing the Parser.
- // 2. Consume a block’s contents from input, and return the result.
- return consume_a_blocks_contents(input);
- }
- Optional<StyleProperty> Parser::parse_as_supports_condition()
- {
- m_rule_context.append(ContextType::SupportsCondition);
- auto maybe_declaration = parse_a_declaration(m_token_stream);
- m_rule_context.take_last();
- if (maybe_declaration.has_value())
- return convert_to_style_property(maybe_declaration.release_value());
- return {};
- }
- // https://drafts.csswg.org/css-syntax/#parse-declaration
- template<typename T>
- Optional<Declaration> Parser::parse_a_declaration(TokenStream<T>& input)
- {
- // To parse a declaration from input:
- // 1. Normalize input, and set input to the result.
- // Note: This is done when initializing the Parser.
- // 2. Discard whitespace from input.
- input.discard_whitespace();
- // 3. Consume a declaration from input. If anything was returned, return it. Otherwise, return a syntax error.
- if (auto declaration = consume_a_declaration(input); declaration.has_value())
- return declaration.release_value();
- // FIXME: Syntax error
- return {};
- }
- Optional<ComponentValue> Parser::parse_as_component_value()
- {
- return parse_a_component_value(m_token_stream);
- }
- // https://drafts.csswg.org/css-syntax/#parse-component-value
- template<typename T>
- Optional<ComponentValue> Parser::parse_a_component_value(TokenStream<T>& input)
- {
- // To parse a component value from input:
- // 1. Normalize input, and set input to the result.
- // Note: This is done when initializing the Parser.
- // 2. Discard whitespace from input.
- input.discard_whitespace();
- // 3. If input is empty, return a syntax error.
- // FIXME: Syntax error
- if (input.is_empty())
- return {};
- // 4. Consume a component value from input and let value be the return value.
- auto value = consume_a_component_value(input);
- // 5. Discard whitespace from input.
- input.discard_whitespace();
- // 6. If input is empty, return value. Otherwise, return a syntax error.
- if (input.is_empty())
- return move(value);
- // FIXME: Syntax error
- return {};
- }
- // https://drafts.csswg.org/css-syntax/#parse-list-of-component-values
- template<typename T>
- Vector<ComponentValue> Parser::parse_a_list_of_component_values(TokenStream<T>& input)
- {
- // To parse a list of component values from input:
- // 1. Normalize input, and set input to the result.
- // Note: This is done when initializing the Parser.
- // 2. Consume a list of component values from input, and return the result.
- return consume_a_list_of_component_values(input);
- }
- // https://drafts.csswg.org/css-syntax/#parse-comma-separated-list-of-component-values
- template<typename T>
- Vector<Vector<ComponentValue>> Parser::parse_a_comma_separated_list_of_component_values(TokenStream<T>& input)
- {
- // To parse a comma-separated list of component values from input:
- // 1. Normalize input, and set input to the result.
- // Note: This is done when initializing the Parser.
- // 2. Let groups be an empty list.
- Vector<Vector<ComponentValue>> groups;
- // 3. While input is not empty:
- while (!input.is_empty()) {
- // 1. Consume a list of component values from input, with <comma-token> as the stop token, and append the result to groups.
- groups.append(consume_a_list_of_component_values(input, Token::Type::Comma));
- // 2. Discard a token from input.
- input.discard_a_token();
- }
- // 4. Return groups.
- return groups;
- }
- template Vector<Vector<ComponentValue>> Parser::parse_a_comma_separated_list_of_component_values(TokenStream<ComponentValue>&);
- template Vector<Vector<ComponentValue>> Parser::parse_a_comma_separated_list_of_component_values(TokenStream<Token>&);
- ElementInlineCSSStyleDeclaration* Parser::parse_as_style_attribute(DOM::Element& element)
- {
- auto expand_shorthands = [&](Vector<StyleProperty>& properties) -> Vector<StyleProperty> {
- Vector<StyleProperty> expanded_properties;
- for (auto& property : properties) {
- if (property_is_shorthand(property.property_id)) {
- StyleComputer::for_each_property_expanding_shorthands(property.property_id, *property.value, StyleComputer::AllowUnresolved::Yes, [&](PropertyID longhand_property_id, CSSStyleValue const& longhand_value) {
- expanded_properties.append(CSS::StyleProperty {
- .important = property.important,
- .property_id = longhand_property_id,
- .value = longhand_value,
- });
- });
- } else {
- expanded_properties.append(property);
- }
- }
- return expanded_properties;
- };
- m_rule_context.append(ContextType::Style);
- auto declarations_and_at_rules = parse_a_blocks_contents(m_token_stream);
- m_rule_context.take_last();
- auto [properties, custom_properties] = extract_properties(declarations_and_at_rules);
- auto expanded_properties = expand_shorthands(properties);
- return ElementInlineCSSStyleDeclaration::create(element, move(expanded_properties), move(custom_properties));
- }
- Optional<URL::URL> Parser::parse_url_function(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- auto& component_value = tokens.consume_a_token();
- auto convert_string_to_url = [&](StringView url_string) -> Optional<URL::URL> {
- auto url = m_context.complete_url(url_string);
- if (url.is_valid()) {
- transaction.commit();
- return url;
- }
- return {};
- };
- if (component_value.is(Token::Type::Url)) {
- auto url_string = component_value.token().url();
- return convert_string_to_url(url_string);
- }
- if (component_value.is_function("url"sv)) {
- auto const& function_values = component_value.function().value;
- // FIXME: Handle url-modifiers. https://www.w3.org/TR/css-values-4/#url-modifiers
- for (size_t i = 0; i < function_values.size(); ++i) {
- auto const& value = function_values[i];
- if (value.is(Token::Type::Whitespace))
- continue;
- if (value.is(Token::Type::String)) {
- auto url_string = value.token().string();
- return convert_string_to_url(url_string);
- }
- break;
- }
- }
- return {};
- }
- RefPtr<CSSStyleValue> Parser::parse_url_value(TokenStream<ComponentValue>& tokens)
- {
- auto url = parse_url_function(tokens);
- if (!url.has_value())
- return nullptr;
- return URLStyleValue::create(*url);
- }
- // https://www.w3.org/TR/css-shapes-1/#typedef-shape-radius
- Optional<ShapeRadius> Parser::parse_shape_radius(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- tokens.discard_whitespace();
- auto maybe_radius = parse_length_percentage(tokens);
- if (maybe_radius.has_value()) {
- // Negative radius is invalid.
- auto radius = maybe_radius.value();
- if ((radius.is_length() && radius.length().raw_value() < 0) || (radius.is_percentage() && radius.percentage().value() < 0))
- return {};
- transaction.commit();
- return radius;
- }
- if (tokens.next_token().is_ident("closest-side"sv)) {
- tokens.discard_a_token();
- transaction.commit();
- return FitSide::ClosestSide;
- }
- if (tokens.next_token().is_ident("farthest-side"sv)) {
- tokens.discard_a_token();
- transaction.commit();
- return FitSide::FarthestSide;
- }
- return {};
- }
- RefPtr<CSSStyleValue> Parser::parse_basic_shape_value(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- auto& component_value = tokens.consume_a_token();
- if (!component_value.is_function())
- return nullptr;
- auto function_name = component_value.function().name.bytes_as_string_view();
- // FIXME: Implement path(). See: https://www.w3.org/TR/css-shapes-1/#basic-shape-functions
- if (function_name.equals_ignoring_ascii_case("inset"sv)) {
- // inset() = inset( <length-percentage>{1,4} [ round <'border-radius'> ]? )
- // FIXME: Parse the border-radius.
- auto arguments_tokens = TokenStream { component_value.function().value };
- // If less than four <length-percentage> values are provided,
- // the omitted values default in the same way as the margin shorthand:
- // an omitted second or third value defaults to the first, and an omitted fourth value defaults to the second.
- // The four <length-percentage>s define the position of the top, right, bottom, and left edges of a rectangle.
- arguments_tokens.discard_whitespace();
- auto top = parse_length_percentage(arguments_tokens);
- if (!top.has_value())
- return nullptr;
- arguments_tokens.discard_whitespace();
- auto right = parse_length_percentage(arguments_tokens);
- if (!right.has_value())
- right = top;
- arguments_tokens.discard_whitespace();
- auto bottom = parse_length_percentage(arguments_tokens);
- if (!bottom.has_value())
- bottom = top;
- arguments_tokens.discard_whitespace();
- auto left = parse_length_percentage(arguments_tokens);
- if (!left.has_value())
- left = right;
- arguments_tokens.discard_whitespace();
- if (arguments_tokens.has_next_token())
- return nullptr;
- transaction.commit();
- return BasicShapeStyleValue::create(Inset { LengthBox(top.value(), right.value(), bottom.value(), left.value()) });
- }
- if (function_name.equals_ignoring_ascii_case("xywh"sv)) {
- // xywh() = xywh( <length-percentage>{2} <length-percentage [0,∞]>{2} [ round <'border-radius'> ]? )
- // FIXME: Parse the border-radius.
- auto arguments_tokens = TokenStream { component_value.function().value };
- arguments_tokens.discard_whitespace();
- auto x = parse_length_percentage(arguments_tokens);
- if (!x.has_value())
- return nullptr;
- arguments_tokens.discard_whitespace();
- auto y = parse_length_percentage(arguments_tokens);
- if (!y.has_value())
- return nullptr;
- arguments_tokens.discard_whitespace();
- auto width = parse_length_percentage(arguments_tokens);
- if (!width.has_value())
- return nullptr;
- arguments_tokens.discard_whitespace();
- auto height = parse_length_percentage(arguments_tokens);
- if (!height.has_value())
- return nullptr;
- arguments_tokens.discard_whitespace();
- if (arguments_tokens.has_next_token())
- return nullptr;
- // Negative width or height is invalid.
- if ((width->is_length() && width->length().raw_value() < 0)
- || (width->is_percentage() && width->percentage().value() < 0)
- || (height->is_length() && height->length().raw_value() < 0)
- || (height->is_percentage() && height->percentage().value() < 0))
- return nullptr;
- transaction.commit();
- return BasicShapeStyleValue::create(Xywh { x.value(), y.value(), width.value(), height.value() });
- }
- if (function_name.equals_ignoring_ascii_case("rect"sv)) {
- // rect() = rect( [ <length-percentage> | auto ]{4} [ round <'border-radius'> ]? )
- // FIXME: Parse the border-radius.
- auto arguments_tokens = TokenStream { component_value.function().value };
- auto parse_length_percentage_or_auto = [this](TokenStream<ComponentValue>& tokens) -> Optional<LengthPercentage> {
- tokens.discard_whitespace();
- auto value = parse_length_percentage(tokens);
- if (!value.has_value()) {
- if (tokens.consume_a_token().is_ident("auto"sv)) {
- value = Length::make_auto();
- }
- }
- return value;
- };
- auto top = parse_length_percentage_or_auto(arguments_tokens);
- auto right = parse_length_percentage_or_auto(arguments_tokens);
- auto bottom = parse_length_percentage_or_auto(arguments_tokens);
- auto left = parse_length_percentage_or_auto(arguments_tokens);
- if (!top.has_value() || !right.has_value() || !bottom.has_value() || !left.has_value())
- return nullptr;
- arguments_tokens.discard_whitespace();
- if (arguments_tokens.has_next_token())
- return nullptr;
- transaction.commit();
- return BasicShapeStyleValue::create(Rect { LengthBox(top.value(), right.value(), bottom.value(), left.value()) });
- }
- if (function_name.equals_ignoring_ascii_case("circle"sv)) {
- // circle() = circle( <shape-radius>? [ at <position> ]? )
- auto arguments_tokens = TokenStream { component_value.function().value };
- auto radius = parse_shape_radius(arguments_tokens).value_or(FitSide::ClosestSide);
- auto position = PositionStyleValue::create_center();
- arguments_tokens.discard_whitespace();
- if (arguments_tokens.next_token().is_ident("at"sv)) {
- arguments_tokens.discard_a_token();
- arguments_tokens.discard_whitespace();
- auto maybe_position = parse_position_value(arguments_tokens);
- if (maybe_position.is_null())
- return nullptr;
- position = maybe_position.release_nonnull();
- }
- arguments_tokens.discard_whitespace();
- if (arguments_tokens.has_next_token())
- return nullptr;
- transaction.commit();
- return BasicShapeStyleValue::create(Circle { radius, position });
- }
- if (function_name.equals_ignoring_ascii_case("ellipse"sv)) {
- // ellipse() = ellipse( [ <shape-radius>{2} ]? [ at <position> ]? )
- auto arguments_tokens = TokenStream { component_value.function().value };
- Optional<ShapeRadius> radius_x = parse_shape_radius(arguments_tokens);
- Optional<ShapeRadius> radius_y = parse_shape_radius(arguments_tokens);
- if (radius_x.has_value() && !radius_y.has_value())
- return nullptr;
- if (!radius_x.has_value()) {
- radius_x = FitSide::ClosestSide;
- radius_y = FitSide::ClosestSide;
- }
- auto position = PositionStyleValue::create_center();
- arguments_tokens.discard_whitespace();
- if (arguments_tokens.next_token().is_ident("at"sv)) {
- arguments_tokens.discard_a_token();
- arguments_tokens.discard_whitespace();
- auto maybe_position = parse_position_value(arguments_tokens);
- if (maybe_position.is_null())
- return nullptr;
- position = maybe_position.release_nonnull();
- }
- arguments_tokens.discard_whitespace();
- if (arguments_tokens.has_next_token())
- return nullptr;
- transaction.commit();
- return BasicShapeStyleValue::create(Ellipse { radius_x.value(), radius_y.value(), position });
- }
- if (function_name.equals_ignoring_ascii_case("polygon"sv)) {
- // polygon() = polygon( <'fill-rule'>? , [<length-percentage> <length-percentage>]# )
- auto arguments_tokens = TokenStream { component_value.function().value };
- auto arguments = parse_a_comma_separated_list_of_component_values(arguments_tokens);
- if (arguments.size() < 1)
- return nullptr;
- Optional<Gfx::WindingRule> fill_rule;
- auto const& first_argument = arguments[0];
- TokenStream first_argument_tokens { first_argument };
- first_argument_tokens.discard_whitespace();
- if (first_argument_tokens.next_token().is_ident("nonzero"sv)) {
- fill_rule = Gfx::WindingRule::Nonzero;
- } else if (first_argument_tokens.next_token().is_ident("evenodd"sv)) {
- fill_rule = Gfx::WindingRule::EvenOdd;
- }
- if (fill_rule.has_value()) {
- first_argument_tokens.discard_a_token();
- if (first_argument_tokens.has_next_token())
- return nullptr;
- arguments.remove(0);
- } else {
- fill_rule = Gfx::WindingRule::Nonzero;
- }
- if (arguments.size() < 1)
- return nullptr;
- Vector<Polygon::Point> points;
- for (auto& argument : arguments) {
- TokenStream argument_tokens { argument };
- argument_tokens.discard_whitespace();
- auto x_pos = parse_length_percentage(argument_tokens);
- if (!x_pos.has_value())
- return nullptr;
- argument_tokens.discard_whitespace();
- auto y_pos = parse_length_percentage(argument_tokens);
- if (!y_pos.has_value())
- return nullptr;
- argument_tokens.discard_whitespace();
- if (argument_tokens.has_next_token())
- return nullptr;
- points.append(Polygon::Point { *x_pos, *y_pos });
- }
- transaction.commit();
- return BasicShapeStyleValue::create(Polygon { fill_rule.value(), move(points) });
- }
- return nullptr;
- }
- bool Parser::is_valid_in_the_current_context(Declaration const&) const
- {
- // TODO: Determine if this *particular* declaration is valid here, not just declarations in general.
- // Declarations can't appear at the top level
- if (m_rule_context.is_empty())
- return false;
- switch (m_rule_context.last()) {
- case ContextType::Unknown:
- // If the context is an unknown type, we don't accept anything.
- return false;
- case ContextType::Style:
- case ContextType::Keyframe:
- // Style and keyframe rules contain property declarations
- return true;
- case ContextType::AtLayer:
- case ContextType::AtMedia:
- case ContextType::AtSupports:
- // Grouping rules can contain declarations if they are themselves inside a style rule
- return m_rule_context.contains_slow(ContextType::Style);
- case ContextType::AtFontFace:
- case ContextType::AtProperty:
- // @font-face and @property have descriptor declarations
- return true;
- case ContextType::AtKeyframes:
- // @keyframes can only contain keyframe rules
- return false;
- case ContextType::SupportsCondition:
- // @supports conditions accept all declarations
- return true;
- }
- VERIFY_NOT_REACHED();
- }
- bool Parser::is_valid_in_the_current_context(AtRule const& at_rule) const
- {
- // All at-rules can appear at the top level
- if (m_rule_context.is_empty())
- return true;
- switch (m_rule_context.last()) {
- case ContextType::Unknown:
- // If the context is an unknown type, we don't accept anything.
- return false;
- case ContextType::Style:
- // Style rules can contain grouping rules
- return first_is_one_of(at_rule.name, "layer", "media", "supports");
- case ContextType::AtLayer:
- case ContextType::AtMedia:
- case ContextType::AtSupports:
- // Grouping rules can contain anything except @import or @namespace
- return !first_is_one_of(at_rule.name, "import", "namespace");
- case ContextType::SupportsCondition:
- // @supports cannot check for at-rules
- return false;
- case ContextType::AtFontFace:
- case ContextType::AtKeyframes:
- case ContextType::Keyframe:
- case ContextType::AtProperty:
- // These can't contain any at-rules
- return false;
- }
- VERIFY_NOT_REACHED();
- }
- bool Parser::is_valid_in_the_current_context(QualifiedRule const&) const
- {
- // TODO: Different places accept different kinds of qualified rules. How do we tell them apart? Can we?
- // Top level can contain style rules
- if (m_rule_context.is_empty())
- return true;
- switch (m_rule_context.last()) {
- case ContextType::Unknown:
- // If the context is an unknown type, we don't accept anything.
- return false;
- case ContextType::Style:
- // Style rules can contain style rules
- return true;
- case ContextType::AtLayer:
- case ContextType::AtMedia:
- case ContextType::AtSupports:
- // Grouping rules can contain style rules
- return true;
- case ContextType::AtKeyframes:
- // @keyframes can contain keyframe rules
- return true;
- case ContextType::SupportsCondition:
- // @supports cannot check qualified rules
- return false;
- case ContextType::AtFontFace:
- case ContextType::AtProperty:
- case ContextType::Keyframe:
- // These can't contain qualified rules
- return false;
- }
- VERIFY_NOT_REACHED();
- }
- Parser::PropertiesAndCustomProperties Parser::extract_properties(Vector<RuleOrListOfDeclarations> const& rules_and_lists_of_declarations)
- {
- PropertiesAndCustomProperties result;
- for (auto const& rule_or_list : rules_and_lists_of_declarations) {
- if (rule_or_list.has<Rule>())
- continue;
- auto& declarations = rule_or_list.get<Vector<Declaration>>();
- PropertiesAndCustomProperties& dest = result;
- for (auto const& declaration : declarations) {
- extract_property(declaration, dest);
- }
- }
- return result;
- }
- void Parser::extract_property(Declaration const& declaration, PropertiesAndCustomProperties& dest)
- {
- if (auto maybe_property = convert_to_style_property(declaration); maybe_property.has_value()) {
- auto property = maybe_property.release_value();
- if (property.property_id == PropertyID::Custom) {
- dest.custom_properties.set(property.custom_name, property);
- } else {
- dest.properties.append(move(property));
- }
- }
- }
- PropertyOwningCSSStyleDeclaration* Parser::convert_to_style_declaration(Vector<Declaration> const& declarations)
- {
- PropertiesAndCustomProperties properties;
- PropertiesAndCustomProperties& dest = properties;
- for (auto const& declaration : declarations) {
- extract_property(declaration, dest);
- }
- return PropertyOwningCSSStyleDeclaration::create(m_context.realm(), move(properties.properties), move(properties.custom_properties));
- }
- Optional<StyleProperty> Parser::convert_to_style_property(Declaration const& declaration)
- {
- auto const& property_name = declaration.name;
- auto property_id = property_id_from_string(property_name);
- if (!property_id.has_value()) {
- if (property_name.bytes_as_string_view().starts_with("--"sv)) {
- property_id = PropertyID::Custom;
- } else if (has_ignored_vendor_prefix(property_name)) {
- return {};
- } else if (!property_name.bytes_as_string_view().starts_with('-')) {
- dbgln_if(CSS_PARSER_DEBUG, "Unrecognized CSS property '{}'", property_name);
- return {};
- }
- }
- auto value_token_stream = TokenStream(declaration.value);
- auto value = parse_css_value(property_id.value(), value_token_stream, declaration.original_text);
- if (value.is_error()) {
- if (value.error() == ParseError::SyntaxError) {
- dbgln_if(CSS_PARSER_DEBUG, "Unable to parse value for CSS property '{}'.", property_name);
- if constexpr (CSS_PARSER_DEBUG) {
- value_token_stream.dump_all_tokens();
- }
- }
- return {};
- }
- if (property_id.value() == PropertyID::Custom)
- return StyleProperty { declaration.important, property_id.value(), value.release_value(), declaration.name };
- return StyleProperty { declaration.important, property_id.value(), value.release_value(), {} };
- }
- RefPtr<CSSStyleValue> Parser::parse_builtin_value(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- auto& component_value = tokens.consume_a_token();
- if (component_value.is(Token::Type::Ident)) {
- auto ident = component_value.token().ident();
- if (ident.equals_ignoring_ascii_case("inherit"sv)) {
- transaction.commit();
- return CSSKeywordValue::create(Keyword::Inherit);
- }
- if (ident.equals_ignoring_ascii_case("initial"sv)) {
- transaction.commit();
- return CSSKeywordValue::create(Keyword::Initial);
- }
- if (ident.equals_ignoring_ascii_case("unset"sv)) {
- transaction.commit();
- return CSSKeywordValue::create(Keyword::Unset);
- }
- if (ident.equals_ignoring_ascii_case("revert"sv)) {
- transaction.commit();
- return CSSKeywordValue::create(Keyword::Revert);
- }
- if (ident.equals_ignoring_ascii_case("revert-layer"sv)) {
- transaction.commit();
- return CSSKeywordValue::create(Keyword::RevertLayer);
- }
- }
- return nullptr;
- }
- // https://www.w3.org/TR/css-values-4/#custom-idents
- RefPtr<CustomIdentStyleValue> Parser::parse_custom_ident_value(TokenStream<ComponentValue>& tokens, std::initializer_list<StringView> blacklist)
- {
- auto transaction = tokens.begin_transaction();
- tokens.discard_whitespace();
- auto const& token = tokens.consume_a_token();
- if (!token.is(Token::Type::Ident))
- return nullptr;
- auto custom_ident = token.token().ident();
- // The CSS-wide keywords are not valid <custom-ident>s.
- if (is_css_wide_keyword(custom_ident))
- return nullptr;
- // The default keyword is reserved and is also not a valid <custom-ident>.
- if (custom_ident.equals_ignoring_ascii_case("default"sv))
- return nullptr;
- // Specifications using <custom-ident> must specify clearly what other keywords are excluded from <custom-ident>,
- // if any—for example by saying that any pre-defined keywords in that property’s value definition are excluded.
- // Excluded keywords are excluded in all ASCII case permutations.
- for (auto& value : blacklist) {
- if (custom_ident.equals_ignoring_ascii_case(value))
- return nullptr;
- }
- transaction.commit();
- return CustomIdentStyleValue::create(custom_ident);
- }
- RefPtr<CSSMathValue> Parser::parse_calculated_value(ComponentValue const& component_value)
- {
- if (!component_value.is_function())
- return nullptr;
- auto const& function = component_value.function();
- auto function_node = parse_a_calc_function_node(function);
- if (!function_node)
- return nullptr;
- auto function_type = function_node->determine_type(m_context.current_property_id());
- if (!function_type.has_value())
- return nullptr;
- return CSSMathValue::create(function_node.release_nonnull(), function_type.release_value());
- }
- OwnPtr<CalculationNode> Parser::parse_a_calc_function_node(Function const& function)
- {
- if (function.name.equals_ignoring_ascii_case("calc"sv))
- return parse_a_calculation(function.value);
- if (auto maybe_function = parse_math_function(m_context.current_property_id(), function))
- return maybe_function;
- return nullptr;
- }
- Optional<Dimension> Parser::parse_dimension(ComponentValue const& component_value)
- {
- if (component_value.is(Token::Type::Dimension)) {
- auto numeric_value = component_value.token().dimension_value();
- auto unit_string = component_value.token().dimension_unit();
- if (auto length_type = Length::unit_from_name(unit_string); length_type.has_value())
- return Length { numeric_value, length_type.release_value() };
- if (auto angle_type = Angle::unit_from_name(unit_string); angle_type.has_value())
- return Angle { numeric_value, angle_type.release_value() };
- if (auto flex_type = Flex::unit_from_name(unit_string); flex_type.has_value())
- return Flex { numeric_value, flex_type.release_value() };
- if (auto frequency_type = Frequency::unit_from_name(unit_string); frequency_type.has_value())
- return Frequency { numeric_value, frequency_type.release_value() };
- if (auto resolution_type = Resolution::unit_from_name(unit_string); resolution_type.has_value())
- return Resolution { numeric_value, resolution_type.release_value() };
- if (auto time_type = Time::unit_from_name(unit_string); time_type.has_value())
- return Time { numeric_value, time_type.release_value() };
- }
- if (component_value.is(Token::Type::Percentage))
- return Percentage { component_value.token().percentage() };
- if (component_value.is(Token::Type::Number)) {
- auto numeric_value = component_value.token().number_value();
- if (numeric_value == 0)
- return Length::make_px(0);
- if (m_context.in_quirks_mode() && property_has_quirk(m_context.current_property_id(), Quirk::UnitlessLength)) {
- // https://quirks.spec.whatwg.org/#quirky-length-value
- // FIXME: Disallow quirk when inside a CSS sub-expression (like `calc()`)
- // "The <quirky-length> value must not be supported in arguments to CSS expressions other than the rect()
- // expression, and must not be supported in the supports() static method of the CSS interface."
- return Length::make_px(CSSPixels::nearest_value_for(numeric_value));
- }
- }
- return {};
- }
- Optional<AngleOrCalculated> Parser::parse_angle(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- auto& token = tokens.consume_a_token();
- if (auto dimension = parse_dimension(token); dimension.has_value()) {
- if (dimension->is_angle()) {
- transaction.commit();
- return dimension->angle();
- }
- return {};
- }
- if (auto calc = parse_calculated_value(token); calc && calc->resolves_to_angle()) {
- transaction.commit();
- return calc.release_nonnull();
- }
- return {};
- }
- Optional<AnglePercentage> Parser::parse_angle_percentage(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- auto& token = tokens.consume_a_token();
- if (auto dimension = parse_dimension(token); dimension.has_value()) {
- if (dimension->is_angle_percentage()) {
- transaction.commit();
- return dimension->angle_percentage();
- }
- return {};
- }
- if (auto calc = parse_calculated_value(token); calc && calc->resolves_to_angle_percentage()) {
- transaction.commit();
- return calc.release_nonnull();
- }
- return {};
- }
- Optional<FlexOrCalculated> Parser::parse_flex(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- auto& token = tokens.consume_a_token();
- if (auto dimension = parse_dimension(token); dimension.has_value()) {
- if (dimension->is_flex()) {
- transaction.commit();
- return dimension->flex();
- }
- return {};
- }
- if (auto calc = parse_calculated_value(token); calc && calc->resolves_to_flex()) {
- transaction.commit();
- return calc.release_nonnull();
- }
- return {};
- }
- Optional<FrequencyOrCalculated> Parser::parse_frequency(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- auto& token = tokens.consume_a_token();
- if (auto dimension = parse_dimension(token); dimension.has_value()) {
- if (dimension->is_frequency()) {
- transaction.commit();
- return dimension->frequency();
- }
- return {};
- }
- if (auto calc = parse_calculated_value(token); calc && calc->resolves_to_frequency()) {
- transaction.commit();
- return calc.release_nonnull();
- }
- return {};
- }
- Optional<FrequencyPercentage> Parser::parse_frequency_percentage(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- auto& token = tokens.consume_a_token();
- if (auto dimension = parse_dimension(token); dimension.has_value()) {
- if (dimension->is_frequency_percentage()) {
- transaction.commit();
- return dimension->frequency_percentage();
- }
- return {};
- }
- if (auto calc = parse_calculated_value(token); calc && calc->resolves_to_frequency_percentage()) {
- transaction.commit();
- return calc.release_nonnull();
- }
- return {};
- }
- Optional<IntegerOrCalculated> Parser::parse_integer(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- auto& token = tokens.consume_a_token();
- if (token.is(Token::Type::Number) && token.token().number().is_integer()) {
- transaction.commit();
- return token.token().to_integer();
- }
- if (auto calc = parse_calculated_value(token); calc && calc->resolves_to_number()) {
- transaction.commit();
- return calc.release_nonnull();
- }
- return {};
- }
- Optional<LengthOrCalculated> Parser::parse_length(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- auto& token = tokens.consume_a_token();
- if (auto dimension = parse_dimension(token); dimension.has_value()) {
- if (dimension->is_length()) {
- transaction.commit();
- return dimension->length();
- }
- return {};
- }
- if (auto calc = parse_calculated_value(token); calc && calc->resolves_to_length()) {
- transaction.commit();
- return calc.release_nonnull();
- }
- return {};
- }
- Optional<LengthPercentage> Parser::parse_length_percentage(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- auto& token = tokens.consume_a_token();
- if (auto dimension = parse_dimension(token); dimension.has_value()) {
- if (dimension->is_length_percentage()) {
- transaction.commit();
- return dimension->length_percentage();
- }
- return {};
- }
- if (auto calc = parse_calculated_value(token); calc && calc->resolves_to_length_percentage()) {
- transaction.commit();
- return calc.release_nonnull();
- }
- return {};
- }
- Optional<NumberOrCalculated> Parser::parse_number(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- auto& token = tokens.consume_a_token();
- if (token.is(Token::Type::Number)) {
- transaction.commit();
- return token.token().number_value();
- }
- if (auto calc = parse_calculated_value(token); calc && calc->resolves_to_number()) {
- transaction.commit();
- return calc.release_nonnull();
- }
- return {};
- }
- Optional<NumberPercentage> Parser::parse_number_percentage(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- auto& token = tokens.consume_a_token();
- if (token.is(Token::Type::Number)) {
- transaction.commit();
- return token.token().number();
- }
- if (token.is(Token::Type::Percentage)) {
- transaction.commit();
- return Percentage(token.token().percentage());
- }
- if (auto calc = parse_calculated_value(token); calc && calc->resolves_to_number_percentage()) {
- transaction.commit();
- return calc.release_nonnull();
- }
- return {};
- }
- Optional<ResolutionOrCalculated> Parser::parse_resolution(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- auto& token = tokens.consume_a_token();
- if (auto dimension = parse_dimension(token); dimension.has_value()) {
- if (dimension->is_resolution()) {
- transaction.commit();
- return dimension->resolution();
- }
- return {};
- }
- if (auto calc = parse_calculated_value(token); calc && calc->resolves_to_resolution()) {
- transaction.commit();
- return calc.release_nonnull();
- }
- return {};
- }
- Optional<TimeOrCalculated> Parser::parse_time(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- auto& token = tokens.consume_a_token();
- if (auto dimension = parse_dimension(token); dimension.has_value()) {
- if (dimension->is_time()) {
- transaction.commit();
- return dimension->time();
- }
- return {};
- }
- if (auto calc = parse_calculated_value(token); calc && calc->resolves_to_time()) {
- transaction.commit();
- return calc.release_nonnull();
- }
- return {};
- }
- Optional<TimePercentage> Parser::parse_time_percentage(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- auto& token = tokens.consume_a_token();
- if (auto dimension = parse_dimension(token); dimension.has_value()) {
- if (dimension->is_time_percentage()) {
- transaction.commit();
- return dimension->time_percentage();
- }
- return {};
- }
- if (auto calc = parse_calculated_value(token); calc && calc->resolves_to_time_percentage()) {
- transaction.commit();
- return calc.release_nonnull();
- }
- return {};
- }
- Optional<LengthOrCalculated> Parser::parse_source_size_value(TokenStream<ComponentValue>& tokens)
- {
- if (tokens.next_token().is_ident("auto"sv)) {
- tokens.discard_a_token(); // auto
- return LengthOrCalculated { Length::make_auto() };
- }
- return parse_length(tokens);
- }
- Optional<Ratio> Parser::parse_ratio(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- tokens.discard_whitespace();
- auto read_number_value = [this](ComponentValue const& component_value) -> Optional<double> {
- if (component_value.is(Token::Type::Number)) {
- return component_value.token().number_value();
- } else if (component_value.is_function()) {
- auto maybe_calc = parse_calculated_value(component_value);
- if (!maybe_calc || !maybe_calc->resolves_to_number())
- return {};
- if (auto resolved_number = maybe_calc->resolve_number(); resolved_number.has_value() && resolved_number.value() >= 0) {
- return resolved_number.value();
- }
- }
- return {};
- };
- // `<ratio> = <number [0,∞]> [ / <number [0,∞]> ]?`
- auto maybe_numerator = read_number_value(tokens.consume_a_token());
- if (!maybe_numerator.has_value() || maybe_numerator.value() < 0)
- return {};
- auto numerator = maybe_numerator.value();
- {
- auto two_value_transaction = tokens.begin_transaction();
- tokens.discard_whitespace();
- auto const& solidus = tokens.consume_a_token();
- tokens.discard_whitespace();
- auto maybe_denominator = read_number_value(tokens.consume_a_token());
- if (solidus.is_delim('/') && maybe_denominator.has_value() && maybe_denominator.value() >= 0) {
- auto denominator = maybe_denominator.value();
- // Two-value ratio
- two_value_transaction.commit();
- transaction.commit();
- return Ratio { numerator, denominator };
- }
- }
- // Single-value ratio
- transaction.commit();
- return Ratio { numerator };
- }
- // https://www.w3.org/TR/css-syntax-3/#urange-syntax
- Optional<Gfx::UnicodeRange> Parser::parse_unicode_range(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- tokens.discard_whitespace();
- // <urange> =
- // u '+' <ident-token> '?'* |
- // u <dimension-token> '?'* |
- // u <number-token> '?'* |
- // u <number-token> <dimension-token> |
- // u <number-token> <number-token> |
- // u '+' '?'+
- // (All with no whitespace in between tokens.)
- // NOTE: Parsing this is different from usual. We take these steps:
- // 1. Match the grammar above against the tokens, concatenating them into a string using their original representation.
- // 2. Then, parse that string according to the spec algorithm.
- // Step 2 is performed by calling the other parse_unicode_range() overload.
- auto is_ending_token = [](ComponentValue const& component_value) {
- return component_value.is(Token::Type::EndOfFile)
- || component_value.is(Token::Type::Comma)
- || component_value.is(Token::Type::Semicolon)
- || component_value.is(Token::Type::Whitespace);
- };
- auto create_unicode_range = [&](StringView text, auto& local_transaction) -> Optional<Gfx::UnicodeRange> {
- auto maybe_unicode_range = parse_unicode_range(text);
- if (maybe_unicode_range.has_value()) {
- local_transaction.commit();
- transaction.commit();
- }
- return maybe_unicode_range;
- };
- // All options start with 'u'/'U'.
- auto const& u = tokens.consume_a_token();
- if (!u.is_ident("u"sv)) {
- dbgln_if(CSS_PARSER_DEBUG, "CSSParser: <urange> does not start with 'u'");
- return {};
- }
- auto const& second_token = tokens.consume_a_token();
- // u '+' <ident-token> '?'* |
- // u '+' '?'+
- if (second_token.is_delim('+')) {
- auto local_transaction = tokens.begin_transaction();
- StringBuilder string_builder;
- string_builder.append(second_token.token().original_source_text());
- auto const& third_token = tokens.consume_a_token();
- if (third_token.is(Token::Type::Ident) || third_token.is_delim('?')) {
- string_builder.append(third_token.token().original_source_text());
- while (tokens.next_token().is_delim('?'))
- string_builder.append(tokens.consume_a_token().token().original_source_text());
- if (is_ending_token(tokens.next_token()))
- return create_unicode_range(string_builder.string_view(), local_transaction);
- }
- }
- // u <dimension-token> '?'*
- if (second_token.is(Token::Type::Dimension)) {
- auto local_transaction = tokens.begin_transaction();
- StringBuilder string_builder;
- string_builder.append(second_token.token().original_source_text());
- while (tokens.next_token().is_delim('?'))
- string_builder.append(tokens.consume_a_token().token().original_source_text());
- if (is_ending_token(tokens.next_token()))
- return create_unicode_range(string_builder.string_view(), local_transaction);
- }
- // u <number-token> '?'* |
- // u <number-token> <dimension-token> |
- // u <number-token> <number-token>
- if (second_token.is(Token::Type::Number)) {
- auto local_transaction = tokens.begin_transaction();
- StringBuilder string_builder;
- string_builder.append(second_token.token().original_source_text());
- if (is_ending_token(tokens.next_token()))
- return create_unicode_range(string_builder.string_view(), local_transaction);
- auto const& third_token = tokens.consume_a_token();
- if (third_token.is_delim('?')) {
- string_builder.append(third_token.token().original_source_text());
- while (tokens.next_token().is_delim('?'))
- string_builder.append(tokens.consume_a_token().token().original_source_text());
- if (is_ending_token(tokens.next_token()))
- return create_unicode_range(string_builder.string_view(), local_transaction);
- } else if (third_token.is(Token::Type::Dimension)) {
- string_builder.append(third_token.token().original_source_text());
- if (is_ending_token(tokens.next_token()))
- return create_unicode_range(string_builder.string_view(), local_transaction);
- } else if (third_token.is(Token::Type::Number)) {
- string_builder.append(third_token.token().original_source_text());
- if (is_ending_token(tokens.next_token()))
- return create_unicode_range(string_builder.string_view(), local_transaction);
- }
- }
- if constexpr (CSS_PARSER_DEBUG) {
- dbgln("CSSParser: Tokens did not match <urange> grammar.");
- tokens.dump_all_tokens();
- }
- return {};
- }
- Optional<Gfx::UnicodeRange> Parser::parse_unicode_range(StringView text)
- {
- auto make_valid_unicode_range = [&](u32 start_value, u32 end_value) -> Optional<Gfx::UnicodeRange> {
- // https://www.w3.org/TR/css-syntax-3/#maximum-allowed-code-point
- constexpr u32 maximum_allowed_code_point = 0x10FFFF;
- // To determine what codepoints the <urange> represents:
- // 1. If end value is greater than the maximum allowed code point,
- // the <urange> is invalid and a syntax error.
- if (end_value > maximum_allowed_code_point) {
- dbgln_if(CSS_PARSER_DEBUG, "CSSParser: Invalid <urange>: end_value ({}) > maximum ({})", end_value, maximum_allowed_code_point);
- return {};
- }
- // 2. If start value is greater than end value, the <urange> is invalid and a syntax error.
- if (start_value > end_value) {
- dbgln_if(CSS_PARSER_DEBUG, "CSSParser: Invalid <urange>: start_value ({}) > end_value ({})", start_value, end_value);
- return {};
- }
- // 3. Otherwise, the <urange> represents a contiguous range of codepoints from start value to end value, inclusive.
- return Gfx::UnicodeRange { start_value, end_value };
- };
- // 1. Skipping the first u token, concatenate the representations of all the tokens in the production together.
- // Let this be text.
- // NOTE: The concatenation is already done by the caller.
- GenericLexer lexer { text };
- // 2. If the first character of text is U+002B PLUS SIGN, consume it.
- // Otherwise, this is an invalid <urange>, and this algorithm must exit.
- if (lexer.next_is('+')) {
- lexer.consume();
- } else {
- dbgln_if(CSS_PARSER_DEBUG, "CSSParser: Second character of <urange> was not '+'; got: '{}'", lexer.consume());
- return {};
- }
- // 3. Consume as many hex digits from text as possible.
- // then consume as many U+003F QUESTION MARK (?) code points as possible.
- auto start_position = lexer.tell();
- auto hex_digits = lexer.consume_while(is_ascii_hex_digit);
- auto question_marks = lexer.consume_while([](auto it) { return it == '?'; });
- // If zero code points were consumed, or more than six code points were consumed,
- // this is an invalid <urange>, and this algorithm must exit.
- size_t consumed_code_points = hex_digits.length() + question_marks.length();
- if (consumed_code_points == 0 || consumed_code_points > 6) {
- dbgln_if(CSS_PARSER_DEBUG, "CSSParser: <urange> start value had {} digits/?s, expected between 1 and 6.", consumed_code_points);
- return {};
- }
- StringView start_value_code_points = text.substring_view(start_position, consumed_code_points);
- // If any U+003F QUESTION MARK (?) code points were consumed, then:
- if (question_marks.length() > 0) {
- // 1. If there are any code points left in text, this is an invalid <urange>,
- // and this algorithm must exit.
- if (lexer.tell_remaining() != 0) {
- dbgln_if(CSS_PARSER_DEBUG, "CSSParser: <urange> invalid; had {} code points left over.", lexer.tell_remaining());
- return {};
- }
- // 2. Interpret the consumed code points as a hexadecimal number,
- // with the U+003F QUESTION MARK (?) code points replaced by U+0030 DIGIT ZERO (0) code points.
- // This is the start value.
- auto start_value_string = start_value_code_points.replace("?"sv, "0"sv, ReplaceMode::All);
- auto maybe_start_value = AK::StringUtils::convert_to_uint_from_hex<u32>(start_value_string);
- if (!maybe_start_value.has_value()) {
- dbgln_if(CSS_PARSER_DEBUG, "CSSParser: <urange> ?-converted start value did not parse as hex number.");
- return {};
- }
- u32 start_value = maybe_start_value.release_value();
- // 3. Interpret the consumed code points as a hexadecimal number again,
- // with the U+003F QUESTION MARK (?) code points replaced by U+0046 LATIN CAPITAL LETTER F (F) code points.
- // This is the end value.
- auto end_value_string = start_value_code_points.replace("?"sv, "F"sv, ReplaceMode::All);
- auto maybe_end_value = AK::StringUtils::convert_to_uint_from_hex<u32>(end_value_string);
- if (!maybe_end_value.has_value()) {
- dbgln_if(CSS_PARSER_DEBUG, "CSSParser: <urange> ?-converted end value did not parse as hex number.");
- return {};
- }
- u32 end_value = maybe_end_value.release_value();
- // 4. Exit this algorithm.
- return make_valid_unicode_range(start_value, end_value);
- }
- // Otherwise, interpret the consumed code points as a hexadecimal number. This is the start value.
- auto maybe_start_value = AK::StringUtils::convert_to_uint_from_hex<u32>(start_value_code_points);
- if (!maybe_start_value.has_value()) {
- dbgln_if(CSS_PARSER_DEBUG, "CSSParser: <urange> start value did not parse as hex number.");
- return {};
- }
- u32 start_value = maybe_start_value.release_value();
- // 4. If there are no code points left in text, The end value is the same as the start value.
- // Exit this algorithm.
- if (lexer.tell_remaining() == 0)
- return make_valid_unicode_range(start_value, start_value);
- // 5. If the next code point in text is U+002D HYPHEN-MINUS (-), consume it.
- if (lexer.next_is('-')) {
- lexer.consume();
- }
- // Otherwise, this is an invalid <urange>, and this algorithm must exit.
- else {
- dbgln_if(CSS_PARSER_DEBUG, "CSSParser: <urange> start and end values not separated by '-'.");
- return {};
- }
- // 6. Consume as many hex digits as possible from text.
- auto end_hex_digits = lexer.consume_while(is_ascii_hex_digit);
- // If zero hex digits were consumed, or more than 6 hex digits were consumed,
- // this is an invalid <urange>, and this algorithm must exit.
- if (end_hex_digits.length() == 0 || end_hex_digits.length() > 6) {
- dbgln_if(CSS_PARSER_DEBUG, "CSSParser: <urange> end value had {} digits, expected between 1 and 6.", end_hex_digits.length());
- return {};
- }
- // If there are any code points left in text, this is an invalid <urange>, and this algorithm must exit.
- if (lexer.tell_remaining() != 0) {
- dbgln_if(CSS_PARSER_DEBUG, "CSSParser: <urange> invalid; had {} code points left over.", lexer.tell_remaining());
- return {};
- }
- // 7. Interpret the consumed code points as a hexadecimal number. This is the end value.
- auto maybe_end_value = AK::StringUtils::convert_to_uint_from_hex<u32>(end_hex_digits);
- if (!maybe_end_value.has_value()) {
- dbgln_if(CSS_PARSER_DEBUG, "CSSParser: <urange> end value did not parse as hex number.");
- return {};
- }
- u32 end_value = maybe_end_value.release_value();
- return make_valid_unicode_range(start_value, end_value);
- }
- Vector<Gfx::UnicodeRange> Parser::parse_unicode_ranges(TokenStream<ComponentValue>& tokens)
- {
- Vector<Gfx::UnicodeRange> unicode_ranges;
- auto range_token_lists = parse_a_comma_separated_list_of_component_values(tokens);
- for (auto& range_tokens : range_token_lists) {
- TokenStream range_token_stream { range_tokens };
- auto maybe_unicode_range = parse_unicode_range(range_token_stream);
- if (!maybe_unicode_range.has_value()) {
- dbgln_if(CSS_PARSER_DEBUG, "CSSParser: unicode-range format invalid; discarding.");
- return {};
- }
- unicode_ranges.append(maybe_unicode_range.release_value());
- }
- return unicode_ranges;
- }
- RefPtr<CSSStyleValue> Parser::parse_dimension_value(TokenStream<ComponentValue>& tokens)
- {
- if (auto dimension = parse_dimension(tokens.next_token()); dimension.has_value()) {
- tokens.discard_a_token(); // dimension
- if (dimension->is_angle())
- return AngleStyleValue::create(dimension->angle());
- if (dimension->is_frequency())
- return FrequencyStyleValue::create(dimension->frequency());
- if (dimension->is_length())
- return LengthStyleValue::create(dimension->length());
- if (dimension->is_percentage())
- return PercentageStyleValue::create(dimension->percentage());
- if (dimension->is_resolution())
- return ResolutionStyleValue::create(dimension->resolution());
- if (dimension->is_time())
- return TimeStyleValue::create(dimension->time());
- VERIFY_NOT_REACHED();
- }
- if (auto calc = parse_calculated_value(tokens.next_token()); calc && calc->resolves_to_dimension()) {
- tokens.discard_a_token(); // calc
- return calc;
- }
- return nullptr;
- }
- RefPtr<CSSStyleValue> Parser::parse_integer_value(TokenStream<ComponentValue>& tokens)
- {
- auto const& peek_token = tokens.next_token();
- if (peek_token.is(Token::Type::Number) && peek_token.token().number().is_integer()) {
- tokens.discard_a_token(); // integer
- return IntegerStyleValue::create(peek_token.token().number().integer_value());
- }
- if (auto calc = parse_calculated_value(peek_token); calc && calc->resolves_to_number()) {
- tokens.discard_a_token(); // calc
- return calc;
- }
- return nullptr;
- }
- RefPtr<CSSStyleValue> Parser::parse_number_value(TokenStream<ComponentValue>& tokens)
- {
- auto const& peek_token = tokens.next_token();
- if (peek_token.is(Token::Type::Number)) {
- tokens.discard_a_token(); // number
- return NumberStyleValue::create(peek_token.token().number().value());
- }
- if (auto calc = parse_calculated_value(peek_token); calc && calc->resolves_to_number()) {
- tokens.discard_a_token(); // calc
- return calc;
- }
- return nullptr;
- }
- RefPtr<CSSStyleValue> Parser::parse_number_percentage_value(TokenStream<ComponentValue>& tokens)
- {
- // Parses [<percentage> | <number>] (which is equivalent to [<alpha-value>])
- auto const& peek_token = tokens.next_token();
- if (peek_token.is(Token::Type::Number)) {
- tokens.discard_a_token(); // number
- return NumberStyleValue::create(peek_token.token().number().value());
- }
- if (peek_token.is(Token::Type::Percentage)) {
- tokens.discard_a_token(); // percentage
- return PercentageStyleValue::create(Percentage(peek_token.token().percentage()));
- }
- if (auto calc = parse_calculated_value(peek_token); calc && calc->resolves_to_number_percentage()) {
- tokens.discard_a_token(); // calc
- return calc;
- }
- return nullptr;
- }
- RefPtr<CSSStyleValue> Parser::parse_number_percentage_none_value(TokenStream<ComponentValue>& tokens)
- {
- // Parses [<percentage> | <number> | none] (which is equivalent to [<alpha-value> | none])
- auto peek_token = tokens.next_token();
- if (peek_token.is(Token::Type::Number)) {
- tokens.discard_a_token(); // number
- return NumberStyleValue::create(peek_token.token().number().value());
- }
- if (peek_token.is(Token::Type::Percentage)) {
- tokens.discard_a_token(); // percentage
- return PercentageStyleValue::create(Percentage(peek_token.token().percentage()));
- }
- if (auto calc = parse_calculated_value(peek_token); calc && calc->resolves_to_number_percentage()) {
- tokens.discard_a_token(); // calc
- return calc;
- }
- if (peek_token.is(Token::Type::Ident)) {
- auto keyword = keyword_from_string(peek_token.token().ident());
- if (keyword.has_value() && keyword.value() == Keyword::None) {
- tokens.discard_a_token(); // keyword none
- return CSSKeywordValue::create(keyword.value());
- }
- }
- return nullptr;
- }
- RefPtr<CSSStyleValue> Parser::parse_percentage_value(TokenStream<ComponentValue>& tokens)
- {
- auto const& peek_token = tokens.next_token();
- if (peek_token.is(Token::Type::Percentage)) {
- tokens.discard_a_token(); // percentage
- return PercentageStyleValue::create(Percentage(peek_token.token().percentage()));
- }
- if (auto calc = parse_calculated_value(peek_token); calc && calc->resolves_to_percentage()) {
- tokens.discard_a_token(); // calc
- return calc;
- }
- return nullptr;
- }
- RefPtr<CSSStyleValue> Parser::parse_angle_value(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- if (auto dimension_value = parse_dimension_value(tokens)) {
- if (dimension_value->is_angle()
- || (dimension_value->is_math() && dimension_value->as_math().resolves_to_angle())) {
- transaction.commit();
- return dimension_value;
- }
- }
- return nullptr;
- }
- RefPtr<CSSStyleValue> Parser::parse_angle_percentage_value(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- if (auto dimension_value = parse_dimension_value(tokens)) {
- if (dimension_value->is_angle() || dimension_value->is_percentage()
- || (dimension_value->is_math() && dimension_value->as_math().resolves_to_angle_percentage())) {
- transaction.commit();
- return dimension_value;
- }
- }
- return nullptr;
- }
- RefPtr<CSSStyleValue> Parser::parse_flex_value(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- if (auto dimension_value = parse_dimension_value(tokens)) {
- if (dimension_value->is_flex()
- || (dimension_value->is_math() && dimension_value->as_math().resolves_to_flex())) {
- transaction.commit();
- return dimension_value;
- }
- }
- return nullptr;
- }
- RefPtr<CSSStyleValue> Parser::parse_frequency_value(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- if (auto dimension_value = parse_dimension_value(tokens)) {
- if (dimension_value->is_frequency()
- || (dimension_value->is_math() && dimension_value->as_math().resolves_to_frequency())) {
- transaction.commit();
- return dimension_value;
- }
- }
- return nullptr;
- }
- RefPtr<CSSStyleValue> Parser::parse_frequency_percentage_value(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- if (auto dimension_value = parse_dimension_value(tokens)) {
- if (dimension_value->is_frequency() || dimension_value->is_percentage()
- || (dimension_value->is_math() && dimension_value->as_math().resolves_to_frequency_percentage())) {
- transaction.commit();
- return dimension_value;
- }
- }
- return nullptr;
- }
- RefPtr<CSSStyleValue> Parser::parse_length_value(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- if (auto dimension_value = parse_dimension_value(tokens)) {
- if (dimension_value->is_length()
- || (dimension_value->is_math() && dimension_value->as_math().resolves_to_length())) {
- transaction.commit();
- return dimension_value;
- }
- }
- return nullptr;
- }
- RefPtr<CSSStyleValue> Parser::parse_length_percentage_value(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- if (auto dimension_value = parse_dimension_value(tokens)) {
- if (dimension_value->is_length() || dimension_value->is_percentage()
- || (dimension_value->is_math() && dimension_value->as_math().resolves_to_length_percentage())) {
- transaction.commit();
- return dimension_value;
- }
- }
- return nullptr;
- }
- RefPtr<CSSStyleValue> Parser::parse_resolution_value(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- if (auto dimension_value = parse_dimension_value(tokens)) {
- if (dimension_value->is_resolution()
- || (dimension_value->is_math() && dimension_value->as_math().resolves_to_resolution())) {
- transaction.commit();
- return dimension_value;
- }
- }
- return nullptr;
- }
- RefPtr<CSSStyleValue> Parser::parse_time_value(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- if (auto dimension_value = parse_dimension_value(tokens)) {
- if (dimension_value->is_time()
- || (dimension_value->is_math() && dimension_value->as_math().resolves_to_time())) {
- transaction.commit();
- return dimension_value;
- }
- }
- return nullptr;
- }
- RefPtr<CSSStyleValue> Parser::parse_time_percentage_value(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- if (auto dimension_value = parse_dimension_value(tokens)) {
- if (dimension_value->is_time() || dimension_value->is_percentage()
- || (dimension_value->is_math() && dimension_value->as_math().resolves_to_time_percentage())) {
- transaction.commit();
- return dimension_value;
- }
- }
- return nullptr;
- }
- RefPtr<CSSStyleValue> Parser::parse_keyword_value(TokenStream<ComponentValue>& tokens)
- {
- auto const& peek_token = tokens.next_token();
- if (peek_token.is(Token::Type::Ident)) {
- auto keyword = keyword_from_string(peek_token.token().ident());
- if (keyword.has_value()) {
- tokens.discard_a_token(); // ident
- return CSSKeywordValue::create(keyword.value());
- }
- }
- return nullptr;
- }
- // https://www.w3.org/TR/CSS2/visufx.html#value-def-shape
- RefPtr<CSSStyleValue> Parser::parse_rect_value(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- auto const& function_token = tokens.consume_a_token();
- if (!function_token.is_function("rect"sv))
- return nullptr;
- Vector<Length, 4> params;
- auto argument_tokens = TokenStream { function_token.function().value };
- enum class CommaRequirement {
- Unknown,
- RequiresCommas,
- RequiresNoCommas
- };
- enum class Side {
- Top = 0,
- Right = 1,
- Bottom = 2,
- Left = 3
- };
- auto comma_requirement = CommaRequirement::Unknown;
- // In CSS 2.1, the only valid <shape> value is: rect(<top>, <right>, <bottom>, <left>) where
- // <top> and <bottom> specify offsets from the top border edge of the box, and <right>, and
- // <left> specify offsets from the left border edge of the box.
- for (size_t side = 0; side < 4; side++) {
- argument_tokens.discard_whitespace();
- // <top>, <right>, <bottom>, and <left> may either have a <length> value or 'auto'.
- // Negative lengths are permitted.
- if (argument_tokens.next_token().is_ident("auto"sv)) {
- (void)argument_tokens.consume_a_token(); // `auto`
- params.append(Length::make_auto());
- } else {
- auto maybe_length = parse_length(argument_tokens);
- if (!maybe_length.has_value())
- return nullptr;
- if (maybe_length.value().is_calculated()) {
- dbgln("FIXME: Support calculated lengths in rect(): {}", maybe_length.value().calculated()->to_string());
- return nullptr;
- }
- params.append(maybe_length.value().value());
- }
- argument_tokens.discard_whitespace();
- // The last side, should be no more tokens following it.
- if (static_cast<Side>(side) == Side::Left) {
- if (argument_tokens.has_next_token())
- return nullptr;
- break;
- }
- bool next_is_comma = argument_tokens.next_token().is(Token::Type::Comma);
- // Authors should separate offset values with commas. User agents must support separation
- // with commas, but may also support separation without commas (but not a combination),
- // because a previous revision of this specification was ambiguous in this respect.
- if (comma_requirement == CommaRequirement::Unknown)
- comma_requirement = next_is_comma ? CommaRequirement::RequiresCommas : CommaRequirement::RequiresNoCommas;
- if (comma_requirement == CommaRequirement::RequiresCommas) {
- if (next_is_comma)
- argument_tokens.discard_a_token();
- else
- return nullptr;
- } else if (comma_requirement == CommaRequirement::RequiresNoCommas) {
- if (next_is_comma)
- return nullptr;
- } else {
- VERIFY_NOT_REACHED();
- }
- }
- transaction.commit();
- return RectStyleValue::create(EdgeRect { params[0], params[1], params[2], params[3] });
- }
- // https://www.w3.org/TR/css-color-4/#typedef-hue
- RefPtr<CSSStyleValue> Parser::parse_hue_none_value(TokenStream<ComponentValue>& tokens)
- {
- // Parses [<hue> | none]
- // <hue> = <number> | <angle>
- auto peek_token = tokens.next_token();
- if (peek_token.is(Token::Type::Number)) {
- tokens.discard_a_token(); // number
- return NumberStyleValue::create(peek_token.token().number().value());
- }
- if (auto calc = parse_calculated_value(peek_token); calc && (calc->resolves_to_number() || calc->resolves_to_angle())) {
- tokens.discard_a_token(); // calc number/angle
- return calc;
- }
- if (auto dimension = parse_dimension(peek_token); dimension.has_value() && dimension->is_angle()) {
- tokens.discard_a_token(); // angle
- return AngleStyleValue::create(dimension->angle());
- }
- if (peek_token.is(Token::Type::Ident)) {
- auto keyword = keyword_from_string(peek_token.token().ident());
- if (keyword.has_value() && keyword.value() == Keyword::None) {
- tokens.discard_a_token(); // keyword none
- return CSSKeywordValue::create(keyword.value());
- }
- }
- return nullptr;
- }
- // https://www.w3.org/TR/css-color-4/#typedef-color-alpha-value
- RefPtr<CSSStyleValue> Parser::parse_solidus_and_alpha_value(TokenStream<ComponentValue>& tokens)
- {
- // [ / [<alpha-value> | none] ]?
- // <alpha-value> = <number> | <percentage>
- // Common to the modern-syntax color functions.
- auto transaction = tokens.begin_transaction();
- tokens.discard_whitespace();
- if (!tokens.consume_a_token().is_delim('/'))
- return {};
- tokens.discard_whitespace();
- auto alpha = parse_number_percentage_none_value(tokens);
- if (!alpha)
- return {};
- tokens.discard_whitespace();
- transaction.commit();
- return alpha;
- }
- // https://www.w3.org/TR/css-color-4/#funcdef-rgb
- RefPtr<CSSStyleValue> Parser::parse_rgb_color_value(TokenStream<ComponentValue>& outer_tokens)
- {
- // rgb() = [ <legacy-rgb-syntax> | <modern-rgb-syntax> ]
- // rgba() = [ <legacy-rgba-syntax> | <modern-rgba-syntax> ]
- // <legacy-rgb-syntax> = rgb( <percentage>#{3} , <alpha-value>? ) |
- // rgb( <number>#{3} , <alpha-value>? )
- // <legacy-rgba-syntax> = rgba( <percentage>#{3} , <alpha-value>? ) |
- // rgba( <number>#{3} , <alpha-value>? )
- // <modern-rgb-syntax> = rgb(
- // [ <number> | <percentage> | none]{3}
- // [ / [<alpha-value> | none] ]? )
- // <modern-rgba-syntax> = rgba(
- // [ <number> | <percentage> | none]{3}
- // [ / [<alpha-value> | none] ]? )
- auto transaction = outer_tokens.begin_transaction();
- outer_tokens.discard_whitespace();
- auto& function_token = outer_tokens.consume_a_token();
- if (!function_token.is_function("rgb"sv) && !function_token.is_function("rgba"sv))
- return {};
- RefPtr<CSSStyleValue> red;
- RefPtr<CSSStyleValue> green;
- RefPtr<CSSStyleValue> blue;
- RefPtr<CSSStyleValue> alpha;
- auto inner_tokens = TokenStream { function_token.function().value };
- inner_tokens.discard_whitespace();
- red = parse_number_percentage_none_value(inner_tokens);
- if (!red)
- return {};
- inner_tokens.discard_whitespace();
- bool legacy_syntax = inner_tokens.next_token().is(Token::Type::Comma);
- if (legacy_syntax) {
- // Legacy syntax
- // <percentage>#{3} , <alpha-value>?
- // | <number>#{3} , <alpha-value>?
- // So, r/g/b can be numbers or percentages, as long as they're all the same type.
- // We accepted the 'none' keyword when parsing the red value, but it's not allowed in the legacy syntax.
- if (red->is_keyword())
- return {};
- inner_tokens.discard_a_token(); // comma
- inner_tokens.discard_whitespace();
- green = parse_number_percentage_value(inner_tokens);
- if (!green)
- return {};
- inner_tokens.discard_whitespace();
- if (!inner_tokens.consume_a_token().is(Token::Type::Comma))
- return {};
- inner_tokens.discard_whitespace();
- blue = parse_number_percentage_value(inner_tokens);
- if (!blue)
- return {};
- inner_tokens.discard_whitespace();
- if (inner_tokens.has_next_token()) {
- // Try and read comma and alpha
- if (!inner_tokens.consume_a_token().is(Token::Type::Comma))
- return {};
- inner_tokens.discard_whitespace();
- alpha = parse_number_percentage_value(inner_tokens);
- if (!alpha)
- return {};
- inner_tokens.discard_whitespace();
- if (inner_tokens.has_next_token())
- return {};
- }
- // Verify we're all percentages or all numbers
- auto is_percentage = [](CSSStyleValue const& style_value) {
- return style_value.is_percentage()
- || (style_value.is_math() && style_value.as_math().resolves_to_percentage());
- };
- bool red_is_percentage = is_percentage(*red);
- bool green_is_percentage = is_percentage(*green);
- bool blue_is_percentage = is_percentage(*blue);
- if (red_is_percentage != green_is_percentage || red_is_percentage != blue_is_percentage)
- return {};
- } else {
- // Modern syntax
- // [ <number> | <percentage> | none]{3} [ / [<alpha-value> | none] ]?
- green = parse_number_percentage_none_value(inner_tokens);
- if (!green)
- return {};
- inner_tokens.discard_whitespace();
- blue = parse_number_percentage_none_value(inner_tokens);
- if (!blue)
- return {};
- inner_tokens.discard_whitespace();
- if (inner_tokens.has_next_token()) {
- alpha = parse_solidus_and_alpha_value(inner_tokens);
- if (!alpha || inner_tokens.has_next_token())
- return {};
- }
- }
- if (!alpha)
- alpha = NumberStyleValue::create(1);
- transaction.commit();
- return CSSRGB::create(red.release_nonnull(), green.release_nonnull(), blue.release_nonnull(), alpha.release_nonnull());
- }
- // https://www.w3.org/TR/css-color-4/#funcdef-hsl
- RefPtr<CSSStyleValue> Parser::parse_hsl_color_value(TokenStream<ComponentValue>& outer_tokens)
- {
- // hsl() = [ <legacy-hsl-syntax> | <modern-hsl-syntax> ]
- // hsla() = [ <legacy-hsla-syntax> | <modern-hsla-syntax> ]
- // <modern-hsl-syntax> = hsl(
- // [<hue> | none]
- // [<percentage> | <number> | none]
- // [<percentage> | <number> | none]
- // [ / [<alpha-value> | none] ]? )
- // <modern-hsla-syntax> = hsla(
- // [<hue> | none]
- // [<percentage> | <number> | none]
- // [<percentage> | <number> | none]
- // [ / [<alpha-value> | none] ]? )
- // <legacy-hsl-syntax> = hsl( <hue>, <percentage>, <percentage>, <alpha-value>? )
- // <legacy-hsla-syntax> = hsla( <hue>, <percentage>, <percentage>, <alpha-value>? )
- auto transaction = outer_tokens.begin_transaction();
- outer_tokens.discard_whitespace();
- auto& function_token = outer_tokens.consume_a_token();
- if (!function_token.is_function("hsl"sv) && !function_token.is_function("hsla"sv))
- return {};
- RefPtr<CSSStyleValue> h;
- RefPtr<CSSStyleValue> s;
- RefPtr<CSSStyleValue> l;
- RefPtr<CSSStyleValue> alpha;
- auto inner_tokens = TokenStream { function_token.function().value };
- inner_tokens.discard_whitespace();
- h = parse_hue_none_value(inner_tokens);
- if (!h)
- return {};
- inner_tokens.discard_whitespace();
- bool legacy_syntax = inner_tokens.next_token().is(Token::Type::Comma);
- if (legacy_syntax) {
- // Legacy syntax
- // <hue>, <percentage>, <percentage>, <alpha-value>?
- // We accepted the 'none' keyword when parsing the h value, but it's not allowed in the legacy syntax.
- if (h->is_keyword())
- return {};
- (void)inner_tokens.consume_a_token(); // comma
- inner_tokens.discard_whitespace();
- s = parse_percentage_value(inner_tokens);
- if (!s)
- return {};
- inner_tokens.discard_whitespace();
- if (!inner_tokens.consume_a_token().is(Token::Type::Comma))
- return {};
- inner_tokens.discard_whitespace();
- l = parse_percentage_value(inner_tokens);
- if (!l)
- return {};
- inner_tokens.discard_whitespace();
- if (inner_tokens.has_next_token()) {
- // Try and read comma and alpha
- if (!inner_tokens.consume_a_token().is(Token::Type::Comma))
- return {};
- inner_tokens.discard_whitespace();
- alpha = parse_number_percentage_value(inner_tokens);
- // The parser has consumed a comma, so the alpha value is now required
- if (!alpha)
- return {};
- inner_tokens.discard_whitespace();
- if (inner_tokens.has_next_token())
- return {};
- }
- } else {
- // Modern syntax
- // [<hue> | none]
- // [<percentage> | <number> | none]
- // [<percentage> | <number> | none]
- // [ / [<alpha-value> | none] ]?
- s = parse_number_percentage_none_value(inner_tokens);
- if (!s)
- return {};
- inner_tokens.discard_whitespace();
- l = parse_number_percentage_none_value(inner_tokens);
- if (!l)
- return {};
- inner_tokens.discard_whitespace();
- if (inner_tokens.has_next_token()) {
- alpha = parse_solidus_and_alpha_value(inner_tokens);
- if (!alpha || inner_tokens.has_next_token())
- return {};
- }
- }
- if (!alpha)
- alpha = NumberStyleValue::create(1);
- transaction.commit();
- return CSSHSL::create(h.release_nonnull(), s.release_nonnull(), l.release_nonnull(), alpha.release_nonnull());
- }
- // https://www.w3.org/TR/css-color-4/#funcdef-hwb
- RefPtr<CSSStyleValue> Parser::parse_hwb_color_value(TokenStream<ComponentValue>& outer_tokens)
- {
- // hwb() = hwb(
- // [<hue> | none]
- // [<percentage> | <number> | none]
- // [<percentage> | <number> | none]
- // [ / [<alpha-value> | none] ]? )
- auto transaction = outer_tokens.begin_transaction();
- outer_tokens.discard_whitespace();
- auto& function_token = outer_tokens.consume_a_token();
- if (!function_token.is_function("hwb"sv))
- return {};
- RefPtr<CSSStyleValue> h;
- RefPtr<CSSStyleValue> w;
- RefPtr<CSSStyleValue> b;
- RefPtr<CSSStyleValue> alpha;
- auto inner_tokens = TokenStream { function_token.function().value };
- inner_tokens.discard_whitespace();
- h = parse_hue_none_value(inner_tokens);
- if (!h)
- return {};
- inner_tokens.discard_whitespace();
- w = parse_number_percentage_none_value(inner_tokens);
- if (!w)
- return {};
- inner_tokens.discard_whitespace();
- b = parse_number_percentage_none_value(inner_tokens);
- if (!b)
- return {};
- inner_tokens.discard_whitespace();
- if (inner_tokens.has_next_token()) {
- alpha = parse_solidus_and_alpha_value(inner_tokens);
- if (!alpha || inner_tokens.has_next_token())
- return {};
- }
- if (!alpha)
- alpha = NumberStyleValue::create(1);
- transaction.commit();
- return CSSHWB::create(h.release_nonnull(), w.release_nonnull(), b.release_nonnull(), alpha.release_nonnull());
- }
- Optional<Array<RefPtr<CSSStyleValue>, 4>> Parser::parse_lab_like_color_value(TokenStream<ComponentValue>& outer_tokens, StringView function_name)
- {
- // This helper is designed to be compatible with lab and oklab and parses a function with a form like:
- // f() = f( [ <percentage> | <number> | none]
- // [ <percentage> | <number> | none]
- // [ <percentage> | <number> | none]
- // [ / [<alpha-value> | none] ]? )
- auto transaction = outer_tokens.begin_transaction();
- outer_tokens.discard_whitespace();
- auto& function_token = outer_tokens.consume_a_token();
- if (!function_token.is_function(function_name))
- return OptionalNone {};
- RefPtr<CSSStyleValue> l;
- RefPtr<CSSStyleValue> a;
- RefPtr<CSSStyleValue> b;
- RefPtr<CSSStyleValue> alpha;
- auto inner_tokens = TokenStream { function_token.function().value };
- inner_tokens.discard_whitespace();
- l = parse_number_percentage_none_value(inner_tokens);
- if (!l)
- return OptionalNone {};
- inner_tokens.discard_whitespace();
- a = parse_number_percentage_none_value(inner_tokens);
- if (!a)
- return OptionalNone {};
- inner_tokens.discard_whitespace();
- b = parse_number_percentage_none_value(inner_tokens);
- if (!b)
- return OptionalNone {};
- inner_tokens.discard_whitespace();
- if (inner_tokens.has_next_token()) {
- alpha = parse_solidus_and_alpha_value(inner_tokens);
- if (!alpha || inner_tokens.has_next_token())
- return OptionalNone {};
- }
- if (!alpha)
- alpha = NumberStyleValue::create(1);
- transaction.commit();
- return Array { move(l), move(a), move(b), move(alpha) };
- }
- // https://www.w3.org/TR/css-color-4/#funcdef-lab
- RefPtr<CSSStyleValue> Parser::parse_lab_color_value(TokenStream<ComponentValue>& outer_tokens)
- {
- // lab() = lab( [<percentage> | <number> | none]
- // [ <percentage> | <number> | none]
- // [ <percentage> | <number> | none]
- // [ / [<alpha-value> | none] ]? )
- auto maybe_color_values = parse_lab_like_color_value(outer_tokens, "lab"sv);
- if (!maybe_color_values.has_value())
- return {};
- auto& color_values = *maybe_color_values;
- return CSSLabLike::create<CSSLab>(color_values[0].release_nonnull(),
- color_values[1].release_nonnull(),
- color_values[2].release_nonnull(),
- color_values[3].release_nonnull());
- }
- // https://www.w3.org/TR/css-color-4/#funcdef-oklab
- RefPtr<CSSStyleValue> Parser::parse_oklab_color_value(TokenStream<ComponentValue>& outer_tokens)
- {
- // oklab() = oklab( [ <percentage> | <number> | none]
- // [ <percentage> | <number> | none]
- // [ <percentage> | <number> | none]
- // [ / [<alpha-value> | none] ]? )
- auto maybe_color_values = parse_lab_like_color_value(outer_tokens, "oklab"sv);
- if (!maybe_color_values.has_value())
- return {};
- auto& color_values = *maybe_color_values;
- return CSSLabLike::create<CSSOKLab>(color_values[0].release_nonnull(),
- color_values[1].release_nonnull(),
- color_values[2].release_nonnull(),
- color_values[3].release_nonnull());
- }
- Optional<Array<RefPtr<CSSStyleValue>, 4>> Parser::parse_lch_like_color_value(TokenStream<ComponentValue>& outer_tokens, StringView function_name)
- {
- // This helper is designed to be compatible with lch and oklch and parses a function with a form like:
- // f() = f( [<percentage> | <number> | none]
- // [ <percentage> | <number> | none]
- // [ <hue> | none]
- // [ / [<alpha-value> | none] ]? )
- auto transaction = outer_tokens.begin_transaction();
- outer_tokens.discard_whitespace();
- auto const& function_token = outer_tokens.consume_a_token();
- if (!function_token.is_function(function_name))
- return OptionalNone {};
- auto inner_tokens = TokenStream { function_token.function().value };
- inner_tokens.discard_whitespace();
- auto l = parse_number_percentage_none_value(inner_tokens);
- if (!l)
- return OptionalNone {};
- inner_tokens.discard_whitespace();
- auto c = parse_number_percentage_none_value(inner_tokens);
- if (!c)
- return OptionalNone {};
- inner_tokens.discard_whitespace();
- auto h = parse_hue_none_value(inner_tokens);
- if (!h)
- return OptionalNone {};
- inner_tokens.discard_whitespace();
- RefPtr<CSSStyleValue> alpha;
- if (inner_tokens.has_next_token()) {
- alpha = parse_solidus_and_alpha_value(inner_tokens);
- if (!alpha || inner_tokens.has_next_token())
- return OptionalNone {};
- }
- if (!alpha)
- alpha = NumberStyleValue::create(1);
- transaction.commit();
- return Array { move(l), move(c), move(h), move(alpha) };
- }
- // https://www.w3.org/TR/css-color-4/#funcdef-lch
- RefPtr<CSSStyleValue> Parser::parse_lch_color_value(TokenStream<ComponentValue>& outer_tokens)
- {
- // lch() = lch( [<percentage> | <number> | none]
- // [ <percentage> | <number> | none]
- // [ <hue> | none]
- // [ / [<alpha-value> | none] ]? )
- auto maybe_color_values = parse_lch_like_color_value(outer_tokens, "lch"sv);
- if (!maybe_color_values.has_value())
- return {};
- auto& color_values = *maybe_color_values;
- return CSSLCHLike::create<CSSLCH>(color_values[0].release_nonnull(),
- color_values[1].release_nonnull(),
- color_values[2].release_nonnull(),
- color_values[3].release_nonnull());
- }
- // https://www.w3.org/TR/css-color-4/#funcdef-oklch
- RefPtr<CSSStyleValue> Parser::parse_oklch_color_value(TokenStream<ComponentValue>& outer_tokens)
- {
- // oklch() = oklch( [ <percentage> | <number> | none]
- // [ <percentage> | <number> | none]
- // [ <hue> | none]
- // [ / [<alpha-value> | none] ]? )
- auto maybe_color_values = parse_lch_like_color_value(outer_tokens, "oklch"sv);
- if (!maybe_color_values.has_value())
- return {};
- auto& color_values = *maybe_color_values;
- return CSSLCHLike::create<CSSOKLCH>(color_values[0].release_nonnull(),
- color_values[1].release_nonnull(),
- color_values[2].release_nonnull(),
- color_values[3].release_nonnull());
- }
- // https://www.w3.org/TR/css-color-4/#funcdef-color
- RefPtr<CSSStyleValue> Parser::parse_color_function(TokenStream<ComponentValue>& outer_tokens)
- {
- // color() = color( <colorspace-params> [ / [ <alpha-value> | none ] ]? )
- // <colorspace-params> = [ <predefined-rgb-params> | <xyz-params>]
- // <predefined-rgb-params> = <predefined-rgb> [ <number> | <percentage> | none ]{3}
- // <predefined-rgb> = srgb | srgb-linear | display-p3 | a98-rgb | prophoto-rgb | rec2020
- // <xyz-params> = <xyz-space> [ <number> | <percentage> | none ]{3}
- // <xyz-space> = xyz | xyz-d50 | xyz-d65
- auto transaction = outer_tokens.begin_transaction();
- outer_tokens.discard_whitespace();
- auto const& function_token = outer_tokens.consume_a_token();
- if (!function_token.is_function("color"sv))
- return {};
- auto inner_tokens = TokenStream { function_token.function().value };
- inner_tokens.discard_whitespace();
- auto const& maybe_color_space = inner_tokens.consume_a_token();
- inner_tokens.discard_whitespace();
- if (!any_of(CSSColor::s_supported_color_space, [&](auto supported) { return maybe_color_space.is_ident(supported); }))
- return {};
- auto const& color_space = maybe_color_space.token().ident();
- auto c1 = parse_number_percentage_value(inner_tokens);
- if (!c1)
- return {};
- inner_tokens.discard_whitespace();
- auto c2 = parse_number_percentage_value(inner_tokens);
- if (!c2)
- return {};
- inner_tokens.discard_whitespace();
- auto c3 = parse_number_percentage_value(inner_tokens);
- if (!c3)
- return {};
- inner_tokens.discard_whitespace();
- RefPtr<CSSStyleValue> alpha;
- if (inner_tokens.has_next_token()) {
- alpha = parse_solidus_and_alpha_value(inner_tokens);
- if (!alpha || inner_tokens.has_next_token())
- return {};
- }
- if (!alpha)
- alpha = NumberStyleValue::create(1);
- transaction.commit();
- return CSSColor::create(color_space.to_ascii_lowercase(),
- c1.release_nonnull(),
- c2.release_nonnull(),
- c3.release_nonnull(),
- alpha.release_nonnull());
- }
- // https://www.w3.org/TR/css-color-4/#color-syntax
- RefPtr<CSSStyleValue> Parser::parse_color_value(TokenStream<ComponentValue>& tokens)
- {
- // Keywords: <system-color> | <deprecated-color> | currentColor
- {
- auto transaction = tokens.begin_transaction();
- if (auto keyword = parse_keyword_value(tokens); keyword && keyword->has_color()) {
- transaction.commit();
- return keyword;
- }
- }
- // Functions
- if (auto color = parse_color_function(tokens))
- return color;
- if (auto rgb = parse_rgb_color_value(tokens))
- return rgb;
- if (auto hsl = parse_hsl_color_value(tokens))
- return hsl;
- if (auto hwb = parse_hwb_color_value(tokens))
- return hwb;
- if (auto lab = parse_lab_color_value(tokens))
- return lab;
- if (auto lch = parse_lch_color_value(tokens))
- return lch;
- if (auto oklab = parse_oklab_color_value(tokens))
- return oklab;
- if (auto oklch = parse_oklch_color_value(tokens))
- return oklch;
- auto transaction = tokens.begin_transaction();
- tokens.discard_whitespace();
- auto const& component_value = tokens.consume_a_token();
- if (component_value.is(Token::Type::Ident)) {
- auto ident = component_value.token().ident();
- auto color = Color::from_string(ident);
- if (color.has_value()) {
- transaction.commit();
- return CSSColorValue::create_from_color(color.release_value());
- }
- // Otherwise, fall through to the hashless-hex-color case
- }
- if (component_value.is(Token::Type::Hash)) {
- auto color = Color::from_string(MUST(String::formatted("#{}", component_value.token().hash_value())));
- if (color.has_value()) {
- transaction.commit();
- return CSSColorValue::create_from_color(color.release_value());
- }
- return {};
- }
- // https://quirks.spec.whatwg.org/#the-hashless-hex-color-quirk
- if (m_context.in_quirks_mode() && property_has_quirk(m_context.current_property_id(), Quirk::HashlessHexColor)) {
- // The value of a quirky color is obtained from the possible component values using the following algorithm,
- // aborting on the first step that returns a value:
- // 1. Let cv be the component value.
- auto const& cv = component_value;
- String serialization;
- // 2. If cv is a <number-token> or a <dimension-token>, follow these substeps:
- if (cv.is(Token::Type::Number) || cv.is(Token::Type::Dimension)) {
- // 1. If cv’s type flag is not "integer", return an error.
- // This means that values that happen to use scientific notation, e.g., 5e5e5e, will fail to parse.
- if (!cv.token().number().is_integer())
- return {};
- // 2. If cv’s value is less than zero, return an error.
- auto value = cv.is(Token::Type::Number) ? cv.token().to_integer() : cv.token().dimension_value_int();
- if (value < 0)
- return {};
- // 3. Let serialization be the serialization of cv’s value, as a base-ten integer using digits 0-9 (U+0030 to U+0039) in the shortest form possible.
- StringBuilder serialization_builder;
- serialization_builder.appendff("{}", value);
- // 4. If cv is a <dimension-token>, append the unit to serialization.
- if (cv.is(Token::Type::Dimension))
- serialization_builder.append(cv.token().dimension_unit());
- // 5. If serialization consists of fewer than six characters, prepend zeros (U+0030) so that it becomes six characters.
- serialization = MUST(serialization_builder.to_string());
- if (serialization_builder.length() < 6) {
- StringBuilder builder;
- for (size_t i = 0; i < (6 - serialization_builder.length()); i++)
- builder.append('0');
- builder.append(serialization_builder.string_view());
- serialization = MUST(builder.to_string());
- }
- }
- // 3. Otherwise, cv is an <ident-token>; let serialization be cv’s value.
- else {
- if (!cv.is(Token::Type::Ident))
- return {};
- serialization = cv.token().ident().to_string();
- }
- // 4. If serialization does not consist of three or six characters, return an error.
- if (serialization.bytes().size() != 3 && serialization.bytes().size() != 6)
- return {};
- // 5. If serialization contains any characters not in the range [0-9A-Fa-f] (U+0030 to U+0039, U+0041 to U+0046, U+0061 to U+0066), return an error.
- for (auto c : serialization.bytes_as_string_view()) {
- if (!((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f')))
- return {};
- }
- // 6. Return the concatenation of "#" (U+0023) and serialization.
- auto color = Color::from_string(MUST(String::formatted("#{}", serialization)));
- if (color.has_value()) {
- transaction.commit();
- return CSSColorValue::create_from_color(color.release_value());
- }
- }
- return {};
- }
- // https://drafts.csswg.org/css-lists-3/#counter-functions
- RefPtr<CSSStyleValue> Parser::parse_counter_value(TokenStream<ComponentValue>& tokens)
- {
- auto parse_counter_name = [this](TokenStream<ComponentValue>& tokens) -> Optional<FlyString> {
- // https://drafts.csswg.org/css-lists-3/#typedef-counter-name
- // Counters are referred to in CSS syntax using the <counter-name> type, which represents
- // their name as a <custom-ident>. A <counter-name> name cannot match the keyword none;
- // such an identifier is invalid as a <counter-name>.
- auto transaction = tokens.begin_transaction();
- tokens.discard_whitespace();
- auto counter_name = parse_custom_ident_value(tokens, { "none"sv });
- if (!counter_name)
- return {};
- tokens.discard_whitespace();
- if (tokens.has_next_token())
- return {};
- transaction.commit();
- return counter_name->custom_ident();
- };
- auto parse_counter_style = [this](TokenStream<ComponentValue>& tokens) -> RefPtr<CSSStyleValue> {
- // https://drafts.csswg.org/css-counter-styles-3/#typedef-counter-style
- // <counter-style> = <counter-style-name> | <symbols()>
- // For now we just support <counter-style-name>, found here:
- // https://drafts.csswg.org/css-counter-styles-3/#typedef-counter-style-name
- // <counter-style-name> is a <custom-ident> that is not an ASCII case-insensitive match for none.
- auto transaction = tokens.begin_transaction();
- tokens.discard_whitespace();
- auto counter_style_name = parse_custom_ident_value(tokens, { "none"sv });
- if (!counter_style_name)
- return {};
- tokens.discard_whitespace();
- if (tokens.has_next_token())
- return {};
- transaction.commit();
- return counter_style_name.release_nonnull();
- };
- auto transaction = tokens.begin_transaction();
- auto const& token = tokens.consume_a_token();
- if (token.is_function("counter"sv)) {
- // counter() = counter( <counter-name>, <counter-style>? )
- auto& function = token.function();
- TokenStream function_tokens { function.value };
- auto function_values = parse_a_comma_separated_list_of_component_values(function_tokens);
- if (function_values.is_empty() || function_values.size() > 2)
- return nullptr;
- TokenStream name_tokens { function_values[0] };
- auto counter_name = parse_counter_name(name_tokens);
- if (!counter_name.has_value())
- return nullptr;
- RefPtr<CSSStyleValue> counter_style;
- if (function_values.size() > 1) {
- TokenStream counter_style_tokens { function_values[1] };
- counter_style = parse_counter_style(counter_style_tokens);
- if (!counter_style)
- return nullptr;
- } else {
- // In both cases, if the <counter-style> argument is omitted it defaults to `decimal`.
- counter_style = CustomIdentStyleValue::create("decimal"_fly_string);
- }
- transaction.commit();
- return CounterStyleValue::create_counter(counter_name.release_value(), counter_style.release_nonnull());
- }
- if (token.is_function("counters"sv)) {
- // counters() = counters( <counter-name>, <string>, <counter-style>? )
- auto& function = token.function();
- TokenStream function_tokens { function.value };
- auto function_values = parse_a_comma_separated_list_of_component_values(function_tokens);
- if (function_values.size() < 2 || function_values.size() > 3)
- return nullptr;
- TokenStream name_tokens { function_values[0] };
- auto counter_name = parse_counter_name(name_tokens);
- if (!counter_name.has_value())
- return nullptr;
- TokenStream string_tokens { function_values[1] };
- string_tokens.discard_whitespace();
- auto join_string = parse_string_value(string_tokens);
- string_tokens.discard_whitespace();
- if (!join_string || string_tokens.has_next_token())
- return nullptr;
- RefPtr<CSSStyleValue> counter_style;
- if (function_values.size() > 2) {
- TokenStream counter_style_tokens { function_values[2] };
- counter_style = parse_counter_style(counter_style_tokens);
- if (!counter_style)
- return nullptr;
- } else {
- // In both cases, if the <counter-style> argument is omitted it defaults to `decimal`.
- counter_style = CustomIdentStyleValue::create("decimal"_fly_string);
- }
- transaction.commit();
- return CounterStyleValue::create_counters(counter_name.release_value(), join_string->string_value(), counter_style.release_nonnull());
- }
- return nullptr;
- }
- RefPtr<CSSStyleValue> Parser::parse_counter_definitions_value(TokenStream<ComponentValue>& tokens, AllowReversed allow_reversed, i32 default_value_if_not_reversed)
- {
- // If AllowReversed is Yes, parses:
- // [ <counter-name> <integer>? | <reversed-counter-name> <integer>? ]+
- // Otherwise parses:
- // [ <counter-name> <integer>? ]+
- // FIXME: This disabled parsing of `reversed()` counters. Remove this line once they're supported.
- allow_reversed = AllowReversed::No;
- auto transaction = tokens.begin_transaction();
- tokens.discard_whitespace();
- Vector<CounterDefinition> counter_definitions;
- while (tokens.has_next_token()) {
- auto per_item_transaction = tokens.begin_transaction();
- CounterDefinition definition {};
- // <counter-name> | <reversed-counter-name>
- auto& token = tokens.consume_a_token();
- if (token.is(Token::Type::Ident)) {
- definition.name = token.token().ident();
- definition.is_reversed = false;
- } else if (allow_reversed == AllowReversed::Yes && token.is_function("reversed"sv)) {
- TokenStream function_tokens { token.function().value };
- function_tokens.discard_whitespace();
- auto& name_token = function_tokens.consume_a_token();
- if (!name_token.is(Token::Type::Ident))
- break;
- function_tokens.discard_whitespace();
- if (function_tokens.has_next_token())
- break;
- definition.name = name_token.token().ident();
- definition.is_reversed = true;
- } else {
- break;
- }
- tokens.discard_whitespace();
- // <integer>?
- definition.value = parse_integer_value(tokens);
- if (!definition.value && !definition.is_reversed)
- definition.value = IntegerStyleValue::create(default_value_if_not_reversed);
- counter_definitions.append(move(definition));
- tokens.discard_whitespace();
- per_item_transaction.commit();
- }
- if (counter_definitions.is_empty())
- return {};
- transaction.commit();
- return CounterDefinitionsStyleValue::create(move(counter_definitions));
- }
- RefPtr<CSSStyleValue> Parser::parse_ratio_value(TokenStream<ComponentValue>& tokens)
- {
- if (auto ratio = parse_ratio(tokens); ratio.has_value())
- return RatioStyleValue::create(ratio.release_value());
- return nullptr;
- }
- RefPtr<StringStyleValue> Parser::parse_string_value(TokenStream<ComponentValue>& tokens)
- {
- auto const& peek = tokens.next_token();
- if (peek.is(Token::Type::String)) {
- tokens.discard_a_token();
- return StringStyleValue::create(peek.token().string());
- }
- return nullptr;
- }
- RefPtr<CSSStyleValue> Parser::parse_image_value(TokenStream<ComponentValue>& tokens)
- {
- if (auto url = parse_url_function(tokens); url.has_value())
- return ImageStyleValue::create(url.value());
- if (auto linear_gradient = parse_linear_gradient_function(tokens))
- return linear_gradient;
- if (auto conic_gradient = parse_conic_gradient_function(tokens))
- return conic_gradient;
- if (auto radial_gradient = parse_radial_gradient_function(tokens))
- return radial_gradient;
- return nullptr;
- }
- // https://svgwg.org/svg2-draft/painting.html#SpecifyingPaint
- RefPtr<CSSStyleValue> Parser::parse_paint_value(TokenStream<ComponentValue>& tokens)
- {
- // `<paint> = none | <color> | <url> [none | <color>]? | context-fill | context-stroke`
- auto parse_color_or_none = [&]() -> Optional<RefPtr<CSSStyleValue>> {
- if (auto color = parse_color_value(tokens))
- return color;
- // NOTE: <color> also accepts identifiers, so we do this identifier check last.
- if (tokens.next_token().is(Token::Type::Ident)) {
- auto maybe_keyword = keyword_from_string(tokens.next_token().token().ident());
- if (maybe_keyword.has_value()) {
- // FIXME: Accept `context-fill` and `context-stroke`
- switch (*maybe_keyword) {
- case Keyword::None:
- tokens.discard_a_token();
- return CSSKeywordValue::create(*maybe_keyword);
- default:
- return nullptr;
- }
- }
- }
- return OptionalNone {};
- };
- // FIMXE: Allow context-fill/context-stroke here
- if (auto color_or_none = parse_color_or_none(); color_or_none.has_value())
- return *color_or_none;
- if (auto url = parse_url_value(tokens)) {
- tokens.discard_whitespace();
- if (auto color_or_none = parse_color_or_none(); color_or_none == nullptr) {
- // Fail to parse if the fallback is invalid, but otherwise ignore it.
- // FIXME: Use fallback color
- return nullptr;
- }
- return url;
- }
- return nullptr;
- }
- // https://www.w3.org/TR/css-values-4/#position
- RefPtr<PositionStyleValue> Parser::parse_position_value(TokenStream<ComponentValue>& tokens, PositionParsingMode position_parsing_mode)
- {
- auto parse_position_edge = [](ComponentValue const& token) -> Optional<PositionEdge> {
- if (!token.is(Token::Type::Ident))
- return {};
- auto keyword = keyword_from_string(token.token().ident());
- if (!keyword.has_value())
- return {};
- return keyword_to_position_edge(*keyword);
- };
- auto parse_length_percentage = [&](ComponentValue const& token) -> Optional<LengthPercentage> {
- if (token.is(Token::Type::EndOfFile))
- return {};
- if (auto dimension = parse_dimension(token); dimension.has_value()) {
- if (dimension->is_length_percentage())
- return dimension->length_percentage();
- return {};
- }
- if (auto calc = parse_calculated_value(token); calc && calc->resolves_to_length_percentage())
- return LengthPercentage { calc.release_nonnull() };
- return {};
- };
- auto is_horizontal = [](PositionEdge edge, bool accept_center) -> bool {
- switch (edge) {
- case PositionEdge::Left:
- case PositionEdge::Right:
- return true;
- case PositionEdge::Center:
- return accept_center;
- default:
- return false;
- }
- };
- auto is_vertical = [](PositionEdge edge, bool accept_center) -> bool {
- switch (edge) {
- case PositionEdge::Top:
- case PositionEdge::Bottom:
- return true;
- case PositionEdge::Center:
- return accept_center;
- default:
- return false;
- }
- };
- auto make_edge_style_value = [](PositionEdge position_edge, bool is_horizontal) -> NonnullRefPtr<EdgeStyleValue> {
- if (position_edge == PositionEdge::Center)
- return EdgeStyleValue::create(is_horizontal ? PositionEdge::Left : PositionEdge::Top, Percentage { 50 });
- return EdgeStyleValue::create(position_edge, Length::make_px(0));
- };
- // <position> = [
- // [ left | center | right | top | bottom | <length-percentage> ]
- // |
- // [ left | center | right ] && [ top | center | bottom ]
- // |
- // [ left | center | right | <length-percentage> ]
- // [ top | center | bottom | <length-percentage> ]
- // |
- // [ [ left | right ] <length-percentage> ] &&
- // [ [ top | bottom ] <length-percentage> ]
- // ]
- // [ left | center | right | top | bottom | <length-percentage> ]
- auto alternative_1 = [&]() -> RefPtr<PositionStyleValue> {
- auto transaction = tokens.begin_transaction();
- tokens.discard_whitespace();
- auto const& token = tokens.consume_a_token();
- // [ left | center | right | top | bottom ]
- if (auto maybe_edge = parse_position_edge(token); maybe_edge.has_value()) {
- auto edge = maybe_edge.release_value();
- transaction.commit();
- // [ left | right ]
- if (is_horizontal(edge, false))
- return PositionStyleValue::create(make_edge_style_value(edge, true), make_edge_style_value(PositionEdge::Center, false));
- // [ top | bottom ]
- if (is_vertical(edge, false))
- return PositionStyleValue::create(make_edge_style_value(PositionEdge::Center, true), make_edge_style_value(edge, false));
- // [ center ]
- VERIFY(edge == PositionEdge::Center);
- return PositionStyleValue::create(make_edge_style_value(PositionEdge::Center, true), make_edge_style_value(PositionEdge::Center, false));
- }
- // [ <length-percentage> ]
- if (auto maybe_percentage = parse_length_percentage(token); maybe_percentage.has_value()) {
- transaction.commit();
- return PositionStyleValue::create(EdgeStyleValue::create(PositionEdge::Left, *maybe_percentage), make_edge_style_value(PositionEdge::Center, false));
- }
- return nullptr;
- };
- // [ left | center | right ] && [ top | center | bottom ]
- auto alternative_2 = [&]() -> RefPtr<PositionStyleValue> {
- auto transaction = tokens.begin_transaction();
- tokens.discard_whitespace();
- // Parse out two position edges
- auto maybe_first_edge = parse_position_edge(tokens.consume_a_token());
- if (!maybe_first_edge.has_value())
- return nullptr;
- auto first_edge = maybe_first_edge.release_value();
- tokens.discard_whitespace();
- auto maybe_second_edge = parse_position_edge(tokens.consume_a_token());
- if (!maybe_second_edge.has_value())
- return nullptr;
- auto second_edge = maybe_second_edge.release_value();
- // If 'left' or 'right' is given, that position is X and the other is Y.
- // Conversely -
- // If 'top' or 'bottom' is given, that position is Y and the other is X.
- if (is_vertical(first_edge, false) || is_horizontal(second_edge, false))
- swap(first_edge, second_edge);
- // [ left | center | right ] [ top | bottom | center ]
- if (is_horizontal(first_edge, true) && is_vertical(second_edge, true)) {
- transaction.commit();
- return PositionStyleValue::create(make_edge_style_value(first_edge, true), make_edge_style_value(second_edge, false));
- }
- return nullptr;
- };
- // [ left | center | right | <length-percentage> ]
- // [ top | center | bottom | <length-percentage> ]
- auto alternative_3 = [&]() -> RefPtr<PositionStyleValue> {
- auto transaction = tokens.begin_transaction();
- auto parse_position_or_length = [&](bool as_horizontal) -> RefPtr<EdgeStyleValue> {
- tokens.discard_whitespace();
- auto const& token = tokens.consume_a_token();
- if (auto maybe_position = parse_position_edge(token); maybe_position.has_value()) {
- auto position = maybe_position.release_value();
- bool valid = as_horizontal ? is_horizontal(position, true) : is_vertical(position, true);
- if (!valid)
- return nullptr;
- return make_edge_style_value(position, as_horizontal);
- }
- auto maybe_length = parse_length_percentage(token);
- if (!maybe_length.has_value())
- return nullptr;
- return EdgeStyleValue::create(as_horizontal ? PositionEdge::Left : PositionEdge::Top, maybe_length.release_value());
- };
- // [ left | center | right | <length-percentage> ]
- auto horizontal_edge = parse_position_or_length(true);
- if (!horizontal_edge)
- return nullptr;
- // [ top | center | bottom | <length-percentage> ]
- auto vertical_edge = parse_position_or_length(false);
- if (!vertical_edge)
- return nullptr;
- transaction.commit();
- return PositionStyleValue::create(horizontal_edge.release_nonnull(), vertical_edge.release_nonnull());
- };
- // [ [ left | right ] <length-percentage> ] &&
- // [ [ top | bottom ] <length-percentage> ]
- auto alternative_4 = [&]() -> RefPtr<PositionStyleValue> {
- struct PositionAndLength {
- PositionEdge position;
- LengthPercentage length;
- };
- auto parse_position_and_length = [&]() -> Optional<PositionAndLength> {
- tokens.discard_whitespace();
- auto maybe_position = parse_position_edge(tokens.consume_a_token());
- if (!maybe_position.has_value())
- return {};
- tokens.discard_whitespace();
- auto maybe_length = parse_length_percentage(tokens.consume_a_token());
- if (!maybe_length.has_value())
- return {};
- return PositionAndLength {
- .position = maybe_position.release_value(),
- .length = maybe_length.release_value(),
- };
- };
- auto transaction = tokens.begin_transaction();
- auto maybe_group1 = parse_position_and_length();
- if (!maybe_group1.has_value())
- return nullptr;
- auto maybe_group2 = parse_position_and_length();
- if (!maybe_group2.has_value())
- return nullptr;
- auto group1 = maybe_group1.release_value();
- auto group2 = maybe_group2.release_value();
- // [ [ left | right ] <length-percentage> ] [ [ top | bottom ] <length-percentage> ]
- if (is_horizontal(group1.position, false) && is_vertical(group2.position, false)) {
- transaction.commit();
- return PositionStyleValue::create(EdgeStyleValue::create(group1.position, group1.length), EdgeStyleValue::create(group2.position, group2.length));
- }
- // [ [ top | bottom ] <length-percentage> ] [ [ left | right ] <length-percentage> ]
- if (is_vertical(group1.position, false) && is_horizontal(group2.position, false)) {
- transaction.commit();
- return PositionStyleValue::create(EdgeStyleValue::create(group2.position, group2.length), EdgeStyleValue::create(group1.position, group1.length));
- }
- return nullptr;
- };
- // The extra 3-value syntax that's allowed for background-position:
- // [ center | [ left | right ] <length-percentage>? ] &&
- // [ center | [ top | bottom ] <length-percentage>? ]
- auto alternative_5_for_background_position = [&]() -> RefPtr<PositionStyleValue> {
- auto transaction = tokens.begin_transaction();
- struct PositionAndMaybeLength {
- PositionEdge position;
- Optional<LengthPercentage> length;
- };
- // [ <position> <length-percentage>? ]
- auto parse_position_and_maybe_length = [&]() -> Optional<PositionAndMaybeLength> {
- tokens.discard_whitespace();
- auto maybe_position = parse_position_edge(tokens.consume_a_token());
- if (!maybe_position.has_value())
- return {};
- tokens.discard_whitespace();
- auto maybe_length = parse_length_percentage(tokens.next_token());
- if (maybe_length.has_value()) {
- // 'center' cannot be followed by a <length-percentage>
- if (maybe_position.value() == PositionEdge::Center && maybe_length.has_value())
- return {};
- tokens.discard_a_token();
- }
- return PositionAndMaybeLength {
- .position = maybe_position.release_value(),
- .length = move(maybe_length),
- };
- };
- auto maybe_group1 = parse_position_and_maybe_length();
- if (!maybe_group1.has_value())
- return nullptr;
- auto maybe_group2 = parse_position_and_maybe_length();
- if (!maybe_group2.has_value())
- return nullptr;
- auto group1 = maybe_group1.release_value();
- auto group2 = maybe_group2.release_value();
- // 2-value or 4-value if both <length-percentage>s are present or missing.
- if (group1.length.has_value() == group2.length.has_value())
- return nullptr;
- // If 'left' or 'right' is given, that position is X and the other is Y.
- // Conversely -
- // If 'top' or 'bottom' is given, that position is Y and the other is X.
- if (is_vertical(group1.position, false) || is_horizontal(group2.position, false))
- swap(group1, group2);
- // [ center | [ left | right ] ]
- if (!is_horizontal(group1.position, true))
- return nullptr;
- // [ center | [ top | bottom ] ]
- if (!is_vertical(group2.position, true))
- return nullptr;
- auto to_style_value = [&](PositionAndMaybeLength const& group, bool is_horizontal) -> NonnullRefPtr<EdgeStyleValue> {
- if (group.position == PositionEdge::Center)
- return EdgeStyleValue::create(is_horizontal ? PositionEdge::Left : PositionEdge::Top, Percentage { 50 });
- return EdgeStyleValue::create(group.position, group.length.value_or(Length::make_px(0)));
- };
- transaction.commit();
- return PositionStyleValue::create(to_style_value(group1, true), to_style_value(group2, false));
- };
- // Note: The alternatives must be attempted in this order since shorter alternatives can match a prefix of longer ones.
- if (auto position = alternative_4())
- return position;
- if (position_parsing_mode == PositionParsingMode::BackgroundPosition) {
- if (auto position = alternative_5_for_background_position())
- return position;
- }
- if (auto position = alternative_3())
- return position;
- if (auto position = alternative_2())
- return position;
- if (auto position = alternative_1())
- return position;
- return nullptr;
- }
- template<typename ParseFunction>
- RefPtr<CSSStyleValue> Parser::parse_comma_separated_value_list(TokenStream<ComponentValue>& tokens, ParseFunction parse_one_value)
- {
- auto first = parse_one_value(tokens);
- if (!first || !tokens.has_next_token())
- return first;
- StyleValueVector values;
- values.append(first.release_nonnull());
- while (tokens.has_next_token()) {
- if (!tokens.consume_a_token().is(Token::Type::Comma))
- return nullptr;
- if (auto maybe_value = parse_one_value(tokens)) {
- values.append(maybe_value.release_nonnull());
- continue;
- }
- return nullptr;
- }
- return StyleValueList::create(move(values), StyleValueList::Separator::Comma);
- }
- RefPtr<CSSStyleValue> Parser::parse_simple_comma_separated_value_list(PropertyID property_id, TokenStream<ComponentValue>& tokens)
- {
- return parse_comma_separated_value_list(tokens, [this, property_id](auto& tokens) -> RefPtr<CSSStyleValue> {
- if (auto value = parse_css_value_for_property(property_id, tokens))
- return value;
- tokens.reconsume_current_input_token();
- return nullptr;
- });
- }
- RefPtr<CSSStyleValue> Parser::parse_all_as_single_keyword_value(TokenStream<ComponentValue>& tokens, Keyword keyword)
- {
- auto transaction = tokens.begin_transaction();
- tokens.discard_whitespace();
- auto keyword_value = parse_keyword_value(tokens);
- tokens.discard_whitespace();
- if (tokens.has_next_token() || !keyword_value || keyword_value->to_keyword() != keyword)
- return {};
- transaction.commit();
- return keyword_value;
- }
- static void remove_property(Vector<PropertyID>& properties, PropertyID property_to_remove)
- {
- properties.remove_first_matching([&](auto it) { return it == property_to_remove; });
- }
- // https://www.w3.org/TR/css-sizing-4/#aspect-ratio
- RefPtr<CSSStyleValue> Parser::parse_aspect_ratio_value(TokenStream<ComponentValue>& tokens)
- {
- // `auto || <ratio>`
- RefPtr<CSSStyleValue> auto_value;
- RefPtr<CSSStyleValue> ratio_value;
- auto transaction = tokens.begin_transaction();
- while (tokens.has_next_token()) {
- auto maybe_value = parse_css_value_for_property(PropertyID::AspectRatio, tokens);
- if (!maybe_value)
- return nullptr;
- if (maybe_value->is_ratio()) {
- if (ratio_value)
- return nullptr;
- ratio_value = maybe_value.release_nonnull();
- continue;
- }
- if (maybe_value->is_keyword() && maybe_value->as_keyword().keyword() == Keyword::Auto) {
- if (auto_value)
- return nullptr;
- auto_value = maybe_value.release_nonnull();
- continue;
- }
- return nullptr;
- }
- if (auto_value && ratio_value) {
- transaction.commit();
- return StyleValueList::create(
- StyleValueVector { auto_value.release_nonnull(), ratio_value.release_nonnull() },
- StyleValueList::Separator::Space);
- }
- if (ratio_value) {
- transaction.commit();
- return ratio_value.release_nonnull();
- }
- if (auto_value) {
- transaction.commit();
- return auto_value.release_nonnull();
- }
- return nullptr;
- }
- RefPtr<CSSStyleValue> Parser::parse_background_value(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- auto make_background_shorthand = [&](auto background_color, auto background_image, auto background_position, auto background_size, auto background_repeat, auto background_attachment, auto background_origin, auto background_clip) {
- return ShorthandStyleValue::create(PropertyID::Background,
- { PropertyID::BackgroundColor, PropertyID::BackgroundImage, PropertyID::BackgroundPosition, PropertyID::BackgroundSize, PropertyID::BackgroundRepeat, PropertyID::BackgroundAttachment, PropertyID::BackgroundOrigin, PropertyID::BackgroundClip },
- { move(background_color), move(background_image), move(background_position), move(background_size), move(background_repeat), move(background_attachment), move(background_origin), move(background_clip) });
- };
- StyleValueVector background_images;
- StyleValueVector background_positions;
- StyleValueVector background_sizes;
- StyleValueVector background_repeats;
- StyleValueVector background_attachments;
- StyleValueVector background_clips;
- StyleValueVector background_origins;
- RefPtr<CSSStyleValue> background_color;
- auto initial_background_image = property_initial_value(m_context.realm(), PropertyID::BackgroundImage);
- auto initial_background_position = property_initial_value(m_context.realm(), PropertyID::BackgroundPosition);
- auto initial_background_size = property_initial_value(m_context.realm(), PropertyID::BackgroundSize);
- auto initial_background_repeat = property_initial_value(m_context.realm(), PropertyID::BackgroundRepeat);
- auto initial_background_attachment = property_initial_value(m_context.realm(), PropertyID::BackgroundAttachment);
- auto initial_background_clip = property_initial_value(m_context.realm(), PropertyID::BackgroundClip);
- auto initial_background_origin = property_initial_value(m_context.realm(), PropertyID::BackgroundOrigin);
- auto initial_background_color = property_initial_value(m_context.realm(), PropertyID::BackgroundColor);
- // Per-layer values
- RefPtr<CSSStyleValue> background_image;
- RefPtr<CSSStyleValue> background_position;
- RefPtr<CSSStyleValue> background_size;
- RefPtr<CSSStyleValue> background_repeat;
- RefPtr<CSSStyleValue> background_attachment;
- RefPtr<CSSStyleValue> background_clip;
- RefPtr<CSSStyleValue> background_origin;
- bool has_multiple_layers = false;
- // BackgroundSize is always parsed as part of BackgroundPosition, so we don't include it here.
- Vector<PropertyID> remaining_layer_properties {
- PropertyID::BackgroundAttachment,
- PropertyID::BackgroundClip,
- PropertyID::BackgroundColor,
- PropertyID::BackgroundImage,
- PropertyID::BackgroundOrigin,
- PropertyID::BackgroundPosition,
- PropertyID::BackgroundRepeat,
- };
- auto background_layer_is_valid = [&](bool allow_background_color) -> bool {
- if (allow_background_color) {
- if (background_color)
- return true;
- } else {
- if (background_color)
- return false;
- }
- return background_image || background_position || background_size || background_repeat || background_attachment || background_clip || background_origin;
- };
- auto complete_background_layer = [&]() {
- background_images.append(background_image ? background_image.release_nonnull() : initial_background_image);
- background_positions.append(background_position ? background_position.release_nonnull() : initial_background_position);
- background_sizes.append(background_size ? background_size.release_nonnull() : initial_background_size);
- background_repeats.append(background_repeat ? background_repeat.release_nonnull() : initial_background_repeat);
- background_attachments.append(background_attachment ? background_attachment.release_nonnull() : initial_background_attachment);
- if (!background_origin && !background_clip) {
- background_origin = initial_background_origin;
- background_clip = initial_background_clip;
- } else if (!background_clip) {
- background_clip = background_origin;
- }
- background_origins.append(background_origin.release_nonnull());
- background_clips.append(background_clip.release_nonnull());
- background_image = nullptr;
- background_position = nullptr;
- background_size = nullptr;
- background_repeat = nullptr;
- background_attachment = nullptr;
- background_clip = nullptr;
- background_origin = nullptr;
- remaining_layer_properties.clear_with_capacity();
- remaining_layer_properties.unchecked_append(PropertyID::BackgroundAttachment);
- remaining_layer_properties.unchecked_append(PropertyID::BackgroundClip);
- remaining_layer_properties.unchecked_append(PropertyID::BackgroundColor);
- remaining_layer_properties.unchecked_append(PropertyID::BackgroundImage);
- remaining_layer_properties.unchecked_append(PropertyID::BackgroundOrigin);
- remaining_layer_properties.unchecked_append(PropertyID::BackgroundPosition);
- remaining_layer_properties.unchecked_append(PropertyID::BackgroundRepeat);
- };
- while (tokens.has_next_token()) {
- if (tokens.next_token().is(Token::Type::Comma)) {
- has_multiple_layers = true;
- if (!background_layer_is_valid(false))
- return nullptr;
- complete_background_layer();
- tokens.discard_a_token();
- continue;
- }
- auto value_and_property = parse_css_value_for_properties(remaining_layer_properties, tokens);
- if (!value_and_property.has_value())
- return nullptr;
- auto& value = value_and_property->style_value;
- remove_property(remaining_layer_properties, value_and_property->property);
- switch (value_and_property->property) {
- case PropertyID::BackgroundAttachment:
- VERIFY(!background_attachment);
- background_attachment = value.release_nonnull();
- continue;
- case PropertyID::BackgroundColor:
- VERIFY(!background_color);
- background_color = value.release_nonnull();
- continue;
- case PropertyID::BackgroundImage:
- VERIFY(!background_image);
- background_image = value.release_nonnull();
- continue;
- case PropertyID::BackgroundClip:
- case PropertyID::BackgroundOrigin: {
- // background-origin and background-clip accept the same values. From the spec:
- // "If one <box> value is present then it sets both background-origin and background-clip to that value.
- // If two values are present, then the first sets background-origin and the second background-clip."
- // - https://www.w3.org/TR/css-backgrounds-3/#background
- // So, we put the first one in background-origin, then if we get a second, we put it in background-clip.
- // If we only get one, we copy the value before creating the ShorthandStyleValue.
- if (!background_origin) {
- background_origin = value.release_nonnull();
- } else if (!background_clip) {
- background_clip = value.release_nonnull();
- } else {
- VERIFY_NOT_REACHED();
- }
- continue;
- }
- case PropertyID::BackgroundPosition: {
- VERIFY(!background_position);
- background_position = value.release_nonnull();
- // Attempt to parse `/ <background-size>`
- auto background_size_transaction = tokens.begin_transaction();
- auto& maybe_slash = tokens.consume_a_token();
- if (maybe_slash.is_delim('/')) {
- if (auto maybe_background_size = parse_single_background_size_value(tokens)) {
- background_size_transaction.commit();
- background_size = maybe_background_size.release_nonnull();
- continue;
- }
- return nullptr;
- }
- continue;
- }
- case PropertyID::BackgroundRepeat: {
- VERIFY(!background_repeat);
- tokens.reconsume_current_input_token();
- if (auto maybe_repeat = parse_single_background_repeat_value(tokens)) {
- background_repeat = maybe_repeat.release_nonnull();
- continue;
- }
- return nullptr;
- }
- default:
- VERIFY_NOT_REACHED();
- }
- return nullptr;
- }
- if (!background_layer_is_valid(true))
- return nullptr;
- // We only need to create StyleValueLists if there are multiple layers.
- // Otherwise, we can pass the single StyleValues directly.
- if (has_multiple_layers) {
- complete_background_layer();
- if (!background_color)
- background_color = initial_background_color;
- transaction.commit();
- return make_background_shorthand(
- background_color.release_nonnull(),
- StyleValueList::create(move(background_images), StyleValueList::Separator::Comma),
- StyleValueList::create(move(background_positions), StyleValueList::Separator::Comma),
- StyleValueList::create(move(background_sizes), StyleValueList::Separator::Comma),
- StyleValueList::create(move(background_repeats), StyleValueList::Separator::Comma),
- StyleValueList::create(move(background_attachments), StyleValueList::Separator::Comma),
- StyleValueList::create(move(background_origins), StyleValueList::Separator::Comma),
- StyleValueList::create(move(background_clips), StyleValueList::Separator::Comma));
- }
- if (!background_color)
- background_color = initial_background_color;
- if (!background_image)
- background_image = initial_background_image;
- if (!background_position)
- background_position = initial_background_position;
- if (!background_size)
- background_size = initial_background_size;
- if (!background_repeat)
- background_repeat = initial_background_repeat;
- if (!background_attachment)
- background_attachment = initial_background_attachment;
- if (!background_origin && !background_clip) {
- background_origin = initial_background_origin;
- background_clip = initial_background_clip;
- } else if (!background_clip) {
- background_clip = background_origin;
- }
- transaction.commit();
- return make_background_shorthand(
- background_color.release_nonnull(),
- background_image.release_nonnull(),
- background_position.release_nonnull(),
- background_size.release_nonnull(),
- background_repeat.release_nonnull(),
- background_attachment.release_nonnull(),
- background_origin.release_nonnull(),
- background_clip.release_nonnull());
- }
- static Optional<LengthPercentage> style_value_to_length_percentage(auto value)
- {
- if (value->is_percentage())
- return LengthPercentage { value->as_percentage().percentage() };
- if (value->is_length())
- return LengthPercentage { value->as_length().length() };
- if (value->is_math())
- return LengthPercentage { value->as_math() };
- return {};
- }
- RefPtr<CSSStyleValue> Parser::parse_single_background_position_x_or_y_value(TokenStream<ComponentValue>& tokens, PropertyID property)
- {
- PositionEdge relative_edge {};
- if (property == PropertyID::BackgroundPositionX) {
- // [ center | [ [ left | right | x-start | x-end ]? <length-percentage>? ]! ]#
- relative_edge = PositionEdge::Left;
- } else if (property == PropertyID::BackgroundPositionY) {
- // [ center | [ [ top | bottom | y-start | y-end ]? <length-percentage>? ]! ]#
- relative_edge = PositionEdge::Top;
- } else {
- VERIFY_NOT_REACHED();
- }
- auto transaction = tokens.begin_transaction();
- if (!tokens.has_next_token())
- return nullptr;
- auto value = parse_css_value_for_property(property, tokens);
- if (!value)
- return nullptr;
- if (value->is_keyword()) {
- auto keyword = value->to_keyword();
- if (keyword == Keyword::Center) {
- transaction.commit();
- return EdgeStyleValue::create(relative_edge, Percentage { 50 });
- }
- if (auto edge = keyword_to_position_edge(keyword); edge.has_value()) {
- relative_edge = *edge;
- } else {
- return nullptr;
- }
- if (tokens.has_next_token()) {
- value = parse_css_value_for_property(property, tokens);
- if (!value) {
- transaction.commit();
- return EdgeStyleValue::create(relative_edge, Length::make_px(0));
- }
- }
- }
- auto offset = style_value_to_length_percentage(value);
- if (offset.has_value()) {
- transaction.commit();
- return EdgeStyleValue::create(relative_edge, *offset);
- }
- // If no offset is provided create this element but with an offset of default value of zero
- transaction.commit();
- return EdgeStyleValue::create(relative_edge, Length::make_px(0));
- }
- RefPtr<CSSStyleValue> Parser::parse_single_background_repeat_value(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- auto is_directional_repeat = [](CSSStyleValue const& value) -> bool {
- auto keyword = value.to_keyword();
- return keyword == Keyword::RepeatX || keyword == Keyword::RepeatY;
- };
- auto as_repeat = [](Keyword keyword) -> Optional<Repeat> {
- switch (keyword) {
- case Keyword::NoRepeat:
- return Repeat::NoRepeat;
- case Keyword::Repeat:
- return Repeat::Repeat;
- case Keyword::Round:
- return Repeat::Round;
- case Keyword::Space:
- return Repeat::Space;
- default:
- return {};
- }
- };
- auto maybe_x_value = parse_css_value_for_property(PropertyID::BackgroundRepeat, tokens);
- if (!maybe_x_value)
- return nullptr;
- auto x_value = maybe_x_value.release_nonnull();
- if (is_directional_repeat(*x_value)) {
- auto keyword = x_value->to_keyword();
- transaction.commit();
- return BackgroundRepeatStyleValue::create(
- keyword == Keyword::RepeatX ? Repeat::Repeat : Repeat::NoRepeat,
- keyword == Keyword::RepeatX ? Repeat::NoRepeat : Repeat::Repeat);
- }
- auto x_repeat = as_repeat(x_value->to_keyword());
- if (!x_repeat.has_value())
- return nullptr;
- // See if we have a second value for Y
- auto maybe_y_value = parse_css_value_for_property(PropertyID::BackgroundRepeat, tokens);
- if (!maybe_y_value) {
- // We don't have a second value, so use x for both
- transaction.commit();
- return BackgroundRepeatStyleValue::create(x_repeat.value(), x_repeat.value());
- }
- auto y_value = maybe_y_value.release_nonnull();
- if (is_directional_repeat(*y_value))
- return nullptr;
- auto y_repeat = as_repeat(y_value->to_keyword());
- if (!y_repeat.has_value())
- return nullptr;
- transaction.commit();
- return BackgroundRepeatStyleValue::create(x_repeat.value(), y_repeat.value());
- }
- RefPtr<CSSStyleValue> Parser::parse_single_background_size_value(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- auto get_length_percentage = [](CSSStyleValue& style_value) -> Optional<LengthPercentage> {
- if (style_value.has_auto())
- return LengthPercentage { Length::make_auto() };
- if (style_value.is_percentage())
- return LengthPercentage { style_value.as_percentage().percentage() };
- if (style_value.is_length())
- return LengthPercentage { style_value.as_length().length() };
- if (style_value.is_math())
- return LengthPercentage { style_value.as_math() };
- return {};
- };
- auto maybe_x_value = parse_css_value_for_property(PropertyID::BackgroundSize, tokens);
- if (!maybe_x_value)
- return nullptr;
- auto x_value = maybe_x_value.release_nonnull();
- if (x_value->to_keyword() == Keyword::Cover || x_value->to_keyword() == Keyword::Contain) {
- transaction.commit();
- return x_value;
- }
- auto maybe_y_value = parse_css_value_for_property(PropertyID::BackgroundSize, tokens);
- if (!maybe_y_value) {
- auto y_value = LengthPercentage { Length::make_auto() };
- auto x_size = get_length_percentage(*x_value);
- if (!x_size.has_value())
- return nullptr;
- transaction.commit();
- return BackgroundSizeStyleValue::create(x_size.value(), y_value);
- }
- auto y_value = maybe_y_value.release_nonnull();
- auto x_size = get_length_percentage(*x_value);
- auto y_size = get_length_percentage(*y_value);
- if (!x_size.has_value() || !y_size.has_value())
- return nullptr;
- transaction.commit();
- return BackgroundSizeStyleValue::create(x_size.release_value(), y_size.release_value());
- }
- RefPtr<CSSStyleValue> Parser::parse_border_value(PropertyID property_id, TokenStream<ComponentValue>& tokens)
- {
- RefPtr<CSSStyleValue> border_width;
- RefPtr<CSSStyleValue> border_color;
- RefPtr<CSSStyleValue> border_style;
- auto color_property = PropertyID::Invalid;
- auto style_property = PropertyID::Invalid;
- auto width_property = PropertyID::Invalid;
- switch (property_id) {
- case PropertyID::Border:
- color_property = PropertyID::BorderColor;
- style_property = PropertyID::BorderStyle;
- width_property = PropertyID::BorderWidth;
- break;
- case PropertyID::BorderBottom:
- color_property = PropertyID::BorderBottomColor;
- style_property = PropertyID::BorderBottomStyle;
- width_property = PropertyID::BorderBottomWidth;
- break;
- case PropertyID::BorderLeft:
- color_property = PropertyID::BorderLeftColor;
- style_property = PropertyID::BorderLeftStyle;
- width_property = PropertyID::BorderLeftWidth;
- break;
- case PropertyID::BorderRight:
- color_property = PropertyID::BorderRightColor;
- style_property = PropertyID::BorderRightStyle;
- width_property = PropertyID::BorderRightWidth;
- break;
- case PropertyID::BorderTop:
- color_property = PropertyID::BorderTopColor;
- style_property = PropertyID::BorderTopStyle;
- width_property = PropertyID::BorderTopWidth;
- break;
- default:
- VERIFY_NOT_REACHED();
- }
- auto remaining_longhands = Vector { width_property, color_property, style_property };
- auto transaction = tokens.begin_transaction();
- while (tokens.has_next_token()) {
- auto property_and_value = parse_css_value_for_properties(remaining_longhands, tokens);
- if (!property_and_value.has_value())
- return nullptr;
- auto& value = property_and_value->style_value;
- remove_property(remaining_longhands, property_and_value->property);
- if (property_and_value->property == width_property) {
- VERIFY(!border_width);
- border_width = value.release_nonnull();
- } else if (property_and_value->property == color_property) {
- VERIFY(!border_color);
- border_color = value.release_nonnull();
- } else if (property_and_value->property == style_property) {
- VERIFY(!border_style);
- border_style = value.release_nonnull();
- } else {
- VERIFY_NOT_REACHED();
- }
- }
- if (!border_width)
- border_width = property_initial_value(m_context.realm(), width_property);
- if (!border_style)
- border_style = property_initial_value(m_context.realm(), style_property);
- if (!border_color)
- border_color = property_initial_value(m_context.realm(), color_property);
- transaction.commit();
- return ShorthandStyleValue::create(property_id,
- { width_property, style_property, color_property },
- { border_width.release_nonnull(), border_style.release_nonnull(), border_color.release_nonnull() });
- }
- RefPtr<CSSStyleValue> Parser::parse_border_radius_value(TokenStream<ComponentValue>& tokens)
- {
- if (tokens.remaining_token_count() == 2) {
- auto transaction = tokens.begin_transaction();
- auto horizontal = parse_length_percentage(tokens);
- auto vertical = parse_length_percentage(tokens);
- if (horizontal.has_value() && vertical.has_value()) {
- transaction.commit();
- return BorderRadiusStyleValue::create(horizontal.release_value(), vertical.release_value());
- }
- }
- if (tokens.remaining_token_count() == 1) {
- auto transaction = tokens.begin_transaction();
- auto radius = parse_length_percentage(tokens);
- if (radius.has_value()) {
- transaction.commit();
- return BorderRadiusStyleValue::create(radius.value(), radius.value());
- }
- }
- return nullptr;
- }
- RefPtr<CSSStyleValue> Parser::parse_border_radius_shorthand_value(TokenStream<ComponentValue>& tokens)
- {
- auto top_left = [&](Vector<LengthPercentage>& radii) { return radii[0]; };
- auto top_right = [&](Vector<LengthPercentage>& radii) {
- switch (radii.size()) {
- case 4:
- case 3:
- case 2:
- return radii[1];
- case 1:
- return radii[0];
- default:
- VERIFY_NOT_REACHED();
- }
- };
- auto bottom_right = [&](Vector<LengthPercentage>& radii) {
- switch (radii.size()) {
- case 4:
- case 3:
- return radii[2];
- case 2:
- case 1:
- return radii[0];
- default:
- VERIFY_NOT_REACHED();
- }
- };
- auto bottom_left = [&](Vector<LengthPercentage>& radii) {
- switch (radii.size()) {
- case 4:
- return radii[3];
- case 3:
- case 2:
- return radii[1];
- case 1:
- return radii[0];
- default:
- VERIFY_NOT_REACHED();
- }
- };
- Vector<LengthPercentage> horizontal_radii;
- Vector<LengthPercentage> vertical_radii;
- bool reading_vertical = false;
- auto transaction = tokens.begin_transaction();
- while (tokens.has_next_token()) {
- if (tokens.next_token().is_delim('/')) {
- if (reading_vertical || horizontal_radii.is_empty())
- return nullptr;
- reading_vertical = true;
- tokens.discard_a_token(); // `/`
- continue;
- }
- auto maybe_dimension = parse_length_percentage(tokens);
- if (!maybe_dimension.has_value())
- return nullptr;
- if (reading_vertical) {
- vertical_radii.append(maybe_dimension.release_value());
- } else {
- horizontal_radii.append(maybe_dimension.release_value());
- }
- }
- if (horizontal_radii.size() > 4 || vertical_radii.size() > 4
- || horizontal_radii.is_empty()
- || (reading_vertical && vertical_radii.is_empty()))
- return nullptr;
- auto top_left_radius = BorderRadiusStyleValue::create(top_left(horizontal_radii),
- vertical_radii.is_empty() ? top_left(horizontal_radii) : top_left(vertical_radii));
- auto top_right_radius = BorderRadiusStyleValue::create(top_right(horizontal_radii),
- vertical_radii.is_empty() ? top_right(horizontal_radii) : top_right(vertical_radii));
- auto bottom_right_radius = BorderRadiusStyleValue::create(bottom_right(horizontal_radii),
- vertical_radii.is_empty() ? bottom_right(horizontal_radii) : bottom_right(vertical_radii));
- auto bottom_left_radius = BorderRadiusStyleValue::create(bottom_left(horizontal_radii),
- vertical_radii.is_empty() ? bottom_left(horizontal_radii) : bottom_left(vertical_radii));
- transaction.commit();
- return ShorthandStyleValue::create(PropertyID::BorderRadius,
- { PropertyID::BorderTopLeftRadius, PropertyID::BorderTopRightRadius, PropertyID::BorderBottomRightRadius, PropertyID::BorderBottomLeftRadius },
- { move(top_left_radius), move(top_right_radius), move(bottom_right_radius), move(bottom_left_radius) });
- }
- RefPtr<CSSStyleValue> Parser::parse_columns_value(TokenStream<ComponentValue>& tokens)
- {
- if (tokens.remaining_token_count() > 2)
- return nullptr;
- RefPtr<CSSStyleValue> column_count;
- RefPtr<CSSStyleValue> column_width;
- Vector<PropertyID> remaining_longhands { PropertyID::ColumnCount, PropertyID::ColumnWidth };
- int found_autos = 0;
- auto transaction = tokens.begin_transaction();
- while (tokens.has_next_token()) {
- auto property_and_value = parse_css_value_for_properties(remaining_longhands, tokens);
- if (!property_and_value.has_value())
- return nullptr;
- auto& value = property_and_value->style_value;
- // since the values can be in either order, we want to skip over autos
- if (value->has_auto()) {
- found_autos++;
- continue;
- }
- remove_property(remaining_longhands, property_and_value->property);
- switch (property_and_value->property) {
- case PropertyID::ColumnCount: {
- VERIFY(!column_count);
- column_count = value.release_nonnull();
- continue;
- }
- case PropertyID::ColumnWidth: {
- VERIFY(!column_width);
- column_width = value.release_nonnull();
- continue;
- }
- default:
- VERIFY_NOT_REACHED();
- }
- }
- if (found_autos > 2)
- return nullptr;
- if (found_autos == 2) {
- column_count = CSSKeywordValue::create(Keyword::Auto);
- column_width = CSSKeywordValue::create(Keyword::Auto);
- }
- if (found_autos == 1) {
- if (!column_count)
- column_count = CSSKeywordValue::create(Keyword::Auto);
- if (!column_width)
- column_width = CSSKeywordValue::create(Keyword::Auto);
- }
- if (!column_count)
- column_count = property_initial_value(m_context.realm(), PropertyID::ColumnCount);
- if (!column_width)
- column_width = property_initial_value(m_context.realm(), PropertyID::ColumnWidth);
- transaction.commit();
- return ShorthandStyleValue::create(PropertyID::Columns,
- { PropertyID::ColumnCount, PropertyID::ColumnWidth },
- { column_count.release_nonnull(), column_width.release_nonnull() });
- }
- RefPtr<CSSStyleValue> Parser::parse_shadow_value(TokenStream<ComponentValue>& tokens, AllowInsetKeyword allow_inset_keyword)
- {
- // "none"
- if (auto none = parse_all_as_single_keyword_value(tokens, Keyword::None))
- return none;
- return parse_comma_separated_value_list(tokens, [this, allow_inset_keyword](auto& tokens) {
- return parse_single_shadow_value(tokens, allow_inset_keyword);
- });
- }
- RefPtr<CSSStyleValue> Parser::parse_single_shadow_value(TokenStream<ComponentValue>& tokens, AllowInsetKeyword allow_inset_keyword)
- {
- auto transaction = tokens.begin_transaction();
- RefPtr<CSSStyleValue> color;
- RefPtr<CSSStyleValue> offset_x;
- RefPtr<CSSStyleValue> offset_y;
- RefPtr<CSSStyleValue> blur_radius;
- RefPtr<CSSStyleValue> spread_distance;
- Optional<ShadowPlacement> placement;
- auto possibly_dynamic_length = [&](ComponentValue const& token) -> RefPtr<CSSStyleValue> {
- auto tokens = TokenStream<ComponentValue>::of_single_token(token);
- auto maybe_length = parse_length(tokens);
- if (!maybe_length.has_value())
- return nullptr;
- return maybe_length->as_style_value();
- };
- while (tokens.has_next_token()) {
- if (auto maybe_color = parse_color_value(tokens); maybe_color) {
- if (color)
- return nullptr;
- color = maybe_color.release_nonnull();
- continue;
- }
- auto const& token = tokens.next_token();
- if (auto maybe_offset_x = possibly_dynamic_length(token); maybe_offset_x) {
- // horizontal offset
- if (offset_x)
- return nullptr;
- offset_x = maybe_offset_x;
- tokens.discard_a_token();
- // vertical offset
- if (!tokens.has_next_token())
- return nullptr;
- auto maybe_offset_y = possibly_dynamic_length(tokens.next_token());
- if (!maybe_offset_y)
- return nullptr;
- offset_y = maybe_offset_y;
- tokens.discard_a_token();
- // blur radius (optional)
- if (!tokens.has_next_token())
- break;
- auto maybe_blur_radius = possibly_dynamic_length(tokens.next_token());
- if (!maybe_blur_radius)
- continue;
- blur_radius = maybe_blur_radius;
- tokens.discard_a_token();
- // spread distance (optional)
- if (!tokens.has_next_token())
- break;
- auto maybe_spread_distance = possibly_dynamic_length(tokens.next_token());
- if (!maybe_spread_distance)
- continue;
- spread_distance = maybe_spread_distance;
- tokens.discard_a_token();
- continue;
- }
- if (allow_inset_keyword == AllowInsetKeyword::Yes && token.is_ident("inset"sv)) {
- if (placement.has_value())
- return nullptr;
- placement = ShadowPlacement::Inner;
- tokens.discard_a_token();
- continue;
- }
- if (token.is(Token::Type::Comma))
- break;
- return nullptr;
- }
- // If color is absent, default to `currentColor`
- if (!color)
- color = CSSKeywordValue::create(Keyword::Currentcolor);
- // x/y offsets are required
- if (!offset_x || !offset_y)
- return nullptr;
- // Other lengths default to 0
- if (!blur_radius)
- blur_radius = LengthStyleValue::create(Length::make_px(0));
- if (!spread_distance)
- spread_distance = LengthStyleValue::create(Length::make_px(0));
- // Placement is outer by default
- if (!placement.has_value())
- placement = ShadowPlacement::Outer;
- transaction.commit();
- return ShadowStyleValue::create(color.release_nonnull(), offset_x.release_nonnull(), offset_y.release_nonnull(), blur_radius.release_nonnull(), spread_distance.release_nonnull(), placement.release_value());
- }
- RefPtr<CSSStyleValue> Parser::parse_rotate_value(TokenStream<ComponentValue>& tokens)
- {
- // Value: none | <angle> | [ x | y | z | <number>{3} ] && <angle>
- if (tokens.remaining_token_count() == 1) {
- // "none"
- if (auto none = parse_all_as_single_keyword_value(tokens, Keyword::None))
- return none;
- // <angle>
- if (auto angle = parse_angle_value(tokens))
- return RotationStyleValue::create(angle.release_nonnull(), NumberStyleValue::create(0), NumberStyleValue::create(0), NumberStyleValue::create(1));
- }
- auto parse_one_of_xyz = [&]() -> Optional<ComponentValue const&> {
- auto transaction = tokens.begin_transaction();
- auto const& axis = tokens.consume_a_token();
- if (axis.is_ident("x"sv) || axis.is_ident("y"sv) || axis.is_ident("z"sv)) {
- transaction.commit();
- return axis;
- }
- return {};
- };
- // [ x | y | z ] && <angle>
- if (tokens.remaining_token_count() == 2) {
- // Try parsing `x <angle>`
- if (auto axis = parse_one_of_xyz(); axis.has_value()) {
- if (auto angle = parse_angle_value(tokens); angle) {
- if (axis->is_ident("x"sv))
- return RotationStyleValue::create(angle.release_nonnull(), NumberStyleValue::create(1), NumberStyleValue::create(0), NumberStyleValue::create(0));
- if (axis->is_ident("y"sv))
- return RotationStyleValue::create(angle.release_nonnull(), NumberStyleValue::create(0), NumberStyleValue::create(1), NumberStyleValue::create(0));
- if (axis->is_ident("z"sv))
- return RotationStyleValue::create(angle.release_nonnull(), NumberStyleValue::create(0), NumberStyleValue::create(0), NumberStyleValue::create(1));
- }
- }
- // Try parsing `<angle> x`
- if (auto angle = parse_angle_value(tokens); angle) {
- if (auto axis = parse_one_of_xyz(); axis.has_value()) {
- if (axis->is_ident("x"sv))
- return RotationStyleValue::create(angle.release_nonnull(), NumberStyleValue::create(1), NumberStyleValue::create(0), NumberStyleValue::create(0));
- if (axis->is_ident("y"sv))
- return RotationStyleValue::create(angle.release_nonnull(), NumberStyleValue::create(0), NumberStyleValue::create(1), NumberStyleValue::create(0));
- if (axis->is_ident("z"sv))
- return RotationStyleValue::create(angle.release_nonnull(), NumberStyleValue::create(0), NumberStyleValue::create(0), NumberStyleValue::create(1));
- }
- }
- }
- auto parse_three_numbers = [&]() -> Optional<StyleValueVector> {
- auto transaction = tokens.begin_transaction();
- StyleValueVector numbers;
- for (size_t i = 0; i < 3; ++i) {
- if (auto number = parse_number_value(tokens); number) {
- numbers.append(number.release_nonnull());
- } else {
- return {};
- }
- }
- transaction.commit();
- return numbers;
- };
- // <number>{3} && <angle>
- if (tokens.remaining_token_count() == 4) {
- // Try parsing <number>{3} <angle>
- if (auto maybe_numbers = parse_three_numbers(); maybe_numbers.has_value()) {
- if (auto angle = parse_angle_value(tokens); angle) {
- auto numbers = maybe_numbers.release_value();
- return RotationStyleValue::create(angle.release_nonnull(), numbers[0], numbers[1], numbers[2]);
- }
- }
- // Try parsing <angle> <number>{3}
- if (auto angle = parse_angle_value(tokens); angle) {
- if (auto maybe_numbers = parse_three_numbers(); maybe_numbers.has_value()) {
- auto numbers = maybe_numbers.release_value();
- return RotationStyleValue::create(angle.release_nonnull(), numbers[0], numbers[1], numbers[2]);
- }
- }
- }
- return nullptr;
- }
- RefPtr<CSSStyleValue> Parser::parse_stroke_dasharray_value(TokenStream<ComponentValue>& tokens)
- {
- // https://svgwg.org/svg2-draft/painting.html#StrokeDashing
- // Value: none | <dasharray>
- if (auto none = parse_all_as_single_keyword_value(tokens, Keyword::None))
- return none;
- // https://svgwg.org/svg2-draft/painting.html#DataTypeDasharray
- // <dasharray> = [ [ <length-percentage> | <number> ]+ ]#
- Vector<ValueComparingNonnullRefPtr<CSSStyleValue const>> dashes;
- while (tokens.has_next_token()) {
- tokens.discard_whitespace();
- // A <dasharray> is a list of comma and/or white space separated <number> or <length-percentage> values. A <number> value represents a value in user units.
- auto value = parse_number_value(tokens);
- if (value) {
- dashes.append(value.release_nonnull());
- } else {
- auto value = parse_length_percentage_value(tokens);
- if (!value)
- return {};
- dashes.append(value.release_nonnull());
- }
- tokens.discard_whitespace();
- if (tokens.has_next_token() && tokens.next_token().is(Token::Type::Comma))
- tokens.discard_a_token();
- }
- return StyleValueList::create(move(dashes), StyleValueList::Separator::Comma);
- }
- RefPtr<CSSStyleValue> Parser::parse_content_value(TokenStream<ComponentValue>& tokens)
- {
- // FIXME: `content` accepts several kinds of function() type, which we don't handle in property_accepts_value() yet.
- auto is_single_value_keyword = [](Keyword keyword) -> bool {
- switch (keyword) {
- case Keyword::None:
- case Keyword::Normal:
- return true;
- default:
- return false;
- }
- };
- if (tokens.remaining_token_count() == 1) {
- auto transaction = tokens.begin_transaction();
- if (auto keyword = parse_keyword_value(tokens)) {
- if (is_single_value_keyword(keyword->to_keyword())) {
- transaction.commit();
- return keyword;
- }
- }
- }
- auto transaction = tokens.begin_transaction();
- StyleValueVector content_values;
- StyleValueVector alt_text_values;
- bool in_alt_text = false;
- while (tokens.has_next_token()) {
- auto& next = tokens.next_token();
- if (next.is_delim('/')) {
- if (in_alt_text || content_values.is_empty())
- return nullptr;
- in_alt_text = true;
- tokens.discard_a_token();
- continue;
- }
- if (auto style_value = parse_css_value_for_property(PropertyID::Content, tokens)) {
- if (is_single_value_keyword(style_value->to_keyword()))
- return nullptr;
- if (in_alt_text) {
- alt_text_values.append(style_value.release_nonnull());
- } else {
- content_values.append(style_value.release_nonnull());
- }
- continue;
- }
- return nullptr;
- }
- if (content_values.is_empty())
- return nullptr;
- if (in_alt_text && alt_text_values.is_empty())
- return nullptr;
- RefPtr<StyleValueList> alt_text;
- if (!alt_text_values.is_empty())
- alt_text = StyleValueList::create(move(alt_text_values), StyleValueList::Separator::Space);
- transaction.commit();
- return ContentStyleValue::create(StyleValueList::create(move(content_values), StyleValueList::Separator::Space), move(alt_text));
- }
- // https://drafts.csswg.org/css-lists-3/#propdef-counter-increment
- RefPtr<CSSStyleValue> Parser::parse_counter_increment_value(TokenStream<ComponentValue>& tokens)
- {
- // [ <counter-name> <integer>? ]+ | none
- if (auto none = parse_all_as_single_keyword_value(tokens, Keyword::None))
- return none;
- return parse_counter_definitions_value(tokens, AllowReversed::No, 1);
- }
- // https://drafts.csswg.org/css-lists-3/#propdef-counter-reset
- RefPtr<CSSStyleValue> Parser::parse_counter_reset_value(TokenStream<ComponentValue>& tokens)
- {
- // [ <counter-name> <integer>? | <reversed-counter-name> <integer>? ]+ | none
- if (auto none = parse_all_as_single_keyword_value(tokens, Keyword::None))
- return none;
- return parse_counter_definitions_value(tokens, AllowReversed::Yes, 0);
- }
- // https://drafts.csswg.org/css-lists-3/#propdef-counter-set
- RefPtr<CSSStyleValue> Parser::parse_counter_set_value(TokenStream<ComponentValue>& tokens)
- {
- // [ <counter-name> <integer>? ]+ | none
- if (auto none = parse_all_as_single_keyword_value(tokens, Keyword::None))
- return none;
- return parse_counter_definitions_value(tokens, AllowReversed::No, 0);
- }
- // https://www.w3.org/TR/css-display-3/#the-display-properties
- RefPtr<CSSStyleValue> Parser::parse_display_value(TokenStream<ComponentValue>& tokens)
- {
- auto parse_single_component_display = [this](TokenStream<ComponentValue>& tokens) -> Optional<Display> {
- auto transaction = tokens.begin_transaction();
- if (auto keyword_value = parse_keyword_value(tokens)) {
- auto keyword = keyword_value->to_keyword();
- if (keyword == Keyword::ListItem) {
- transaction.commit();
- return Display::from_short(Display::Short::ListItem);
- }
- if (auto display_outside = keyword_to_display_outside(keyword); display_outside.has_value()) {
- transaction.commit();
- switch (display_outside.value()) {
- case DisplayOutside::Block:
- return Display::from_short(Display::Short::Block);
- case DisplayOutside::Inline:
- return Display::from_short(Display::Short::Inline);
- case DisplayOutside::RunIn:
- return Display::from_short(Display::Short::RunIn);
- }
- }
- if (auto display_inside = keyword_to_display_inside(keyword); display_inside.has_value()) {
- transaction.commit();
- switch (display_inside.value()) {
- case DisplayInside::Flow:
- return Display::from_short(Display::Short::Flow);
- case DisplayInside::FlowRoot:
- return Display::from_short(Display::Short::FlowRoot);
- case DisplayInside::Table:
- return Display::from_short(Display::Short::Table);
- case DisplayInside::Flex:
- return Display::from_short(Display::Short::Flex);
- case DisplayInside::Grid:
- return Display::from_short(Display::Short::Grid);
- case DisplayInside::Ruby:
- return Display::from_short(Display::Short::Ruby);
- case DisplayInside::Math:
- return Display::from_short(Display::Short::Math);
- }
- }
- if (auto display_internal = keyword_to_display_internal(keyword); display_internal.has_value()) {
- transaction.commit();
- return Display { display_internal.value() };
- }
- if (auto display_box = keyword_to_display_box(keyword); display_box.has_value()) {
- transaction.commit();
- switch (display_box.value()) {
- case DisplayBox::Contents:
- return Display::from_short(Display::Short::Contents);
- case DisplayBox::None:
- return Display::from_short(Display::Short::None);
- }
- }
- if (auto display_legacy = keyword_to_display_legacy(keyword); display_legacy.has_value()) {
- transaction.commit();
- switch (display_legacy.value()) {
- case DisplayLegacy::InlineBlock:
- return Display::from_short(Display::Short::InlineBlock);
- case DisplayLegacy::InlineTable:
- return Display::from_short(Display::Short::InlineTable);
- case DisplayLegacy::InlineFlex:
- return Display::from_short(Display::Short::InlineFlex);
- case DisplayLegacy::InlineGrid:
- return Display::from_short(Display::Short::InlineGrid);
- }
- }
- }
- return OptionalNone {};
- };
- auto parse_multi_component_display = [this](TokenStream<ComponentValue>& tokens) -> Optional<Display> {
- auto list_item = Display::ListItem::No;
- Optional<DisplayInside> inside;
- Optional<DisplayOutside> outside;
- auto transaction = tokens.begin_transaction();
- while (tokens.has_next_token()) {
- if (auto value = parse_keyword_value(tokens)) {
- auto keyword = value->to_keyword();
- if (keyword == Keyword::ListItem) {
- if (list_item == Display::ListItem::Yes)
- return {};
- list_item = Display::ListItem::Yes;
- continue;
- }
- if (auto inside_value = keyword_to_display_inside(keyword); inside_value.has_value()) {
- if (inside.has_value())
- return {};
- inside = inside_value.value();
- continue;
- }
- if (auto outside_value = keyword_to_display_outside(keyword); outside_value.has_value()) {
- if (outside.has_value())
- return {};
- outside = outside_value.value();
- continue;
- }
- }
- // Not a display value, abort.
- dbgln_if(CSS_PARSER_DEBUG, "Unrecognized display value: `{}`", tokens.next_token().to_string());
- return {};
- }
- // The spec does not allow any other inside values to be combined with list-item
- // <display-outside>? && [ flow | flow-root ]? && list-item
- if (list_item == Display::ListItem::Yes && inside.has_value() && inside != DisplayInside::Flow && inside != DisplayInside::FlowRoot)
- return {};
- transaction.commit();
- return Display { outside.value_or(DisplayOutside::Block), inside.value_or(DisplayInside::Flow), list_item };
- };
- Optional<Display> display;
- if (tokens.remaining_token_count() == 1)
- display = parse_single_component_display(tokens);
- else
- display = parse_multi_component_display(tokens);
- if (display.has_value())
- return DisplayStyleValue::create(display.value());
- return nullptr;
- }
- RefPtr<CSSStyleValue> Parser::parse_filter_value_list_value(TokenStream<ComponentValue>& tokens)
- {
- if (auto none = parse_all_as_single_keyword_value(tokens, Keyword::None))
- return none;
- auto transaction = tokens.begin_transaction();
- // FIXME: <url>s are ignored for now
- // <filter-value-list> = [ <filter-function> | <url> ]+
- enum class FilterToken {
- // Color filters:
- Brightness,
- Contrast,
- Grayscale,
- Invert,
- Opacity,
- Saturate,
- Sepia,
- // Special filters:
- Blur,
- DropShadow,
- HueRotate
- };
- auto filter_token_to_operation = [&](auto filter) {
- VERIFY(to_underlying(filter) < to_underlying(FilterToken::Blur));
- return static_cast<FilterOperation::Color::Type>(filter);
- };
- auto parse_filter_function_name = [&](auto name) -> Optional<FilterToken> {
- if (name.equals_ignoring_ascii_case("blur"sv))
- return FilterToken::Blur;
- if (name.equals_ignoring_ascii_case("brightness"sv))
- return FilterToken::Brightness;
- if (name.equals_ignoring_ascii_case("contrast"sv))
- return FilterToken::Contrast;
- if (name.equals_ignoring_ascii_case("drop-shadow"sv))
- return FilterToken::DropShadow;
- if (name.equals_ignoring_ascii_case("grayscale"sv))
- return FilterToken::Grayscale;
- if (name.equals_ignoring_ascii_case("hue-rotate"sv))
- return FilterToken::HueRotate;
- if (name.equals_ignoring_ascii_case("invert"sv))
- return FilterToken::Invert;
- if (name.equals_ignoring_ascii_case("opacity"sv))
- return FilterToken::Opacity;
- if (name.equals_ignoring_ascii_case("saturate"sv))
- return FilterToken::Saturate;
- if (name.equals_ignoring_ascii_case("sepia"sv))
- return FilterToken::Sepia;
- return {};
- };
- auto parse_filter_function = [&](auto filter_token, auto const& function_values) -> Optional<FilterFunction> {
- TokenStream tokens { function_values };
- tokens.discard_whitespace();
- auto if_no_more_tokens_return = [&](auto filter) -> Optional<FilterFunction> {
- tokens.discard_whitespace();
- if (tokens.has_next_token())
- return {};
- return filter;
- };
- if (filter_token == FilterToken::Blur) {
- // blur( <length>? )
- if (!tokens.has_next_token())
- return FilterOperation::Blur {};
- auto blur_radius = parse_length(tokens);
- tokens.discard_whitespace();
- if (!blur_radius.has_value())
- return {};
- return if_no_more_tokens_return(FilterOperation::Blur { blur_radius.value() });
- } else if (filter_token == FilterToken::DropShadow) {
- if (!tokens.has_next_token())
- return {};
- // drop-shadow( [ <color>? && <length>{2,3} ] )
- // Note: The following code is a little awkward to allow the color to be before or after the lengths.
- Optional<LengthOrCalculated> maybe_radius = {};
- auto maybe_color = parse_color_value(tokens);
- tokens.discard_whitespace();
- auto x_offset = parse_length(tokens);
- tokens.discard_whitespace();
- if (!x_offset.has_value() || !tokens.has_next_token())
- return {};
- auto y_offset = parse_length(tokens);
- tokens.discard_whitespace();
- if (!y_offset.has_value())
- return {};
- if (tokens.has_next_token()) {
- maybe_radius = parse_length(tokens);
- tokens.discard_whitespace();
- if (!maybe_color && (!maybe_radius.has_value() || tokens.has_next_token())) {
- maybe_color = parse_color_value(tokens);
- if (!maybe_color)
- return {};
- } else if (!maybe_radius.has_value()) {
- return {};
- }
- }
- Optional<Color> color = {};
- if (maybe_color)
- color = maybe_color->to_color({});
- return if_no_more_tokens_return(FilterOperation::DropShadow { x_offset.value(), y_offset.value(), maybe_radius, color });
- } else if (filter_token == FilterToken::HueRotate) {
- // hue-rotate( [ <angle> | <zero> ]? )
- if (!tokens.has_next_token())
- return FilterOperation::HueRotate {};
- if (tokens.next_token().is(Token::Type::Number)) {
- // hue-rotate(0)
- auto number = tokens.consume_a_token().token().number();
- if (number.is_integer() && number.integer_value() == 0)
- return if_no_more_tokens_return(FilterOperation::HueRotate { FilterOperation::HueRotate::Zero {} });
- return {};
- }
- if (auto angle = parse_angle(tokens); angle.has_value())
- return if_no_more_tokens_return(FilterOperation::HueRotate { angle.value() });
- return {};
- } else {
- // Simple filters:
- // brightness( <number-percentage>? )
- // contrast( <number-percentage>? )
- // grayscale( <number-percentage>? )
- // invert( <number-percentage>? )
- // opacity( <number-percentage>? )
- // sepia( <number-percentage>? )
- // saturate( <number-percentage>? )
- if (!tokens.has_next_token())
- return FilterOperation::Color { filter_token_to_operation(filter_token) };
- auto amount = parse_number_percentage(tokens);
- return if_no_more_tokens_return(FilterOperation::Color { filter_token_to_operation(filter_token), amount });
- }
- };
- Vector<FilterFunction> filter_value_list {};
- while (tokens.has_next_token()) {
- tokens.discard_whitespace();
- if (!tokens.has_next_token())
- break;
- auto& token = tokens.consume_a_token();
- if (!token.is_function())
- return nullptr;
- auto filter_token = parse_filter_function_name(token.function().name);
- if (!filter_token.has_value())
- return nullptr;
- auto filter_function = parse_filter_function(*filter_token, token.function().value);
- if (!filter_function.has_value())
- return nullptr;
- filter_value_list.append(*filter_function);
- }
- if (filter_value_list.is_empty())
- return nullptr;
- transaction.commit();
- return FilterValueListStyleValue::create(move(filter_value_list));
- }
- RefPtr<CSSStyleValue> Parser::parse_flex_shorthand_value(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- auto make_flex_shorthand = [&](NonnullRefPtr<CSSStyleValue> flex_grow, NonnullRefPtr<CSSStyleValue> flex_shrink, NonnullRefPtr<CSSStyleValue> flex_basis) {
- transaction.commit();
- return ShorthandStyleValue::create(PropertyID::Flex,
- { PropertyID::FlexGrow, PropertyID::FlexShrink, PropertyID::FlexBasis },
- { move(flex_grow), move(flex_shrink), move(flex_basis) });
- };
- if (tokens.remaining_token_count() == 1) {
- // One-value syntax: <flex-grow> | <flex-basis> | none
- auto properties = Array { PropertyID::FlexGrow, PropertyID::FlexBasis, PropertyID::Flex };
- auto property_and_value = parse_css_value_for_properties(properties, tokens);
- if (!property_and_value.has_value())
- return nullptr;
- auto& value = property_and_value->style_value;
- switch (property_and_value->property) {
- case PropertyID::FlexGrow: {
- // NOTE: The spec says that flex-basis should be 0 here, but other engines currently use 0%.
- // https://github.com/w3c/csswg-drafts/issues/5742
- auto flex_basis = PercentageStyleValue::create(Percentage(0));
- auto one = NumberStyleValue::create(1);
- return make_flex_shorthand(*value, one, flex_basis);
- }
- case PropertyID::FlexBasis: {
- auto one = NumberStyleValue::create(1);
- return make_flex_shorthand(one, one, *value);
- }
- case PropertyID::Flex: {
- if (value->is_keyword() && value->to_keyword() == Keyword::None) {
- auto zero = NumberStyleValue::create(0);
- return make_flex_shorthand(zero, zero, CSSKeywordValue::create(Keyword::Auto));
- }
- break;
- }
- default:
- VERIFY_NOT_REACHED();
- }
- return nullptr;
- }
- RefPtr<CSSStyleValue> flex_grow;
- RefPtr<CSSStyleValue> flex_shrink;
- RefPtr<CSSStyleValue> flex_basis;
- // NOTE: FlexGrow has to be before FlexBasis. `0` is a valid FlexBasis, but only
- // if FlexGrow (along with optional FlexShrink) have already been specified.
- auto remaining_longhands = Vector { PropertyID::FlexGrow, PropertyID::FlexBasis };
- while (tokens.has_next_token()) {
- auto property_and_value = parse_css_value_for_properties(remaining_longhands, tokens);
- if (!property_and_value.has_value())
- return nullptr;
- auto& value = property_and_value->style_value;
- remove_property(remaining_longhands, property_and_value->property);
- switch (property_and_value->property) {
- case PropertyID::FlexGrow: {
- VERIFY(!flex_grow);
- flex_grow = value.release_nonnull();
- // Flex-shrink may optionally follow directly after.
- auto maybe_flex_shrink = parse_css_value_for_property(PropertyID::FlexShrink, tokens);
- if (maybe_flex_shrink)
- flex_shrink = maybe_flex_shrink.release_nonnull();
- continue;
- }
- case PropertyID::FlexBasis: {
- VERIFY(!flex_basis);
- flex_basis = value.release_nonnull();
- continue;
- }
- default:
- VERIFY_NOT_REACHED();
- }
- }
- if (!flex_grow)
- flex_grow = property_initial_value(m_context.realm(), PropertyID::FlexGrow);
- if (!flex_shrink)
- flex_shrink = property_initial_value(m_context.realm(), PropertyID::FlexShrink);
- if (!flex_basis) {
- // NOTE: The spec says that flex-basis should be 0 here, but other engines currently use 0%.
- // https://github.com/w3c/csswg-drafts/issues/5742
- flex_basis = PercentageStyleValue::create(Percentage(0));
- }
- return make_flex_shorthand(flex_grow.release_nonnull(), flex_shrink.release_nonnull(), flex_basis.release_nonnull());
- }
- RefPtr<CSSStyleValue> Parser::parse_flex_flow_value(TokenStream<ComponentValue>& tokens)
- {
- RefPtr<CSSStyleValue> flex_direction;
- RefPtr<CSSStyleValue> flex_wrap;
- auto remaining_longhands = Vector { PropertyID::FlexDirection, PropertyID::FlexWrap };
- auto transaction = tokens.begin_transaction();
- while (tokens.has_next_token()) {
- auto property_and_value = parse_css_value_for_properties(remaining_longhands, tokens);
- if (!property_and_value.has_value())
- return nullptr;
- auto& value = property_and_value->style_value;
- remove_property(remaining_longhands, property_and_value->property);
- switch (property_and_value->property) {
- case PropertyID::FlexDirection:
- VERIFY(!flex_direction);
- flex_direction = value.release_nonnull();
- continue;
- case PropertyID::FlexWrap:
- VERIFY(!flex_wrap);
- flex_wrap = value.release_nonnull();
- continue;
- default:
- VERIFY_NOT_REACHED();
- }
- }
- if (!flex_direction)
- flex_direction = property_initial_value(m_context.realm(), PropertyID::FlexDirection);
- if (!flex_wrap)
- flex_wrap = property_initial_value(m_context.realm(), PropertyID::FlexWrap);
- transaction.commit();
- return ShorthandStyleValue::create(PropertyID::FlexFlow,
- { PropertyID::FlexDirection, PropertyID::FlexWrap },
- { flex_direction.release_nonnull(), flex_wrap.release_nonnull() });
- }
- bool Parser::is_generic_font_family(Keyword keyword)
- {
- switch (keyword) {
- case Keyword::Cursive:
- case Keyword::Fantasy:
- case Keyword::Monospace:
- case Keyword::Serif:
- case Keyword::SansSerif:
- case Keyword::UiMonospace:
- case Keyword::UiRounded:
- case Keyword::UiSerif:
- case Keyword::UiSansSerif:
- return true;
- default:
- return false;
- }
- }
- RefPtr<CSSStyleValue> Parser::parse_font_value(TokenStream<ComponentValue>& tokens)
- {
- RefPtr<CSSStyleValue> font_width;
- RefPtr<CSSStyleValue> font_style;
- RefPtr<CSSStyleValue> font_weight;
- RefPtr<CSSStyleValue> font_size;
- RefPtr<CSSStyleValue> line_height;
- RefPtr<CSSStyleValue> font_families;
- RefPtr<CSSStyleValue> font_variant;
- // FIXME: Handle system fonts. (caption, icon, menu, message-box, small-caption, status-bar)
- // Several sub-properties can be "normal", and appear in any order: style, variant, weight, stretch
- // So, we have to handle that separately.
- int normal_count = 0;
- // FIXME: `font-variant` allows a lot of different values which aren't allowed in the `font` shorthand.
- // FIXME: `font-width` allows <percentage> values, which aren't allowed in the `font` shorthand.
- auto remaining_longhands = Vector { PropertyID::FontSize, PropertyID::FontStyle, PropertyID::FontVariant, PropertyID::FontWeight, PropertyID::FontWidth };
- auto transaction = tokens.begin_transaction();
- while (tokens.has_next_token()) {
- auto& peek_token = tokens.next_token();
- if (peek_token.is_ident("normal"sv)) {
- normal_count++;
- tokens.discard_a_token();
- continue;
- }
- auto property_and_value = parse_css_value_for_properties(remaining_longhands, tokens);
- if (!property_and_value.has_value())
- return nullptr;
- auto& value = property_and_value->style_value;
- remove_property(remaining_longhands, property_and_value->property);
- switch (property_and_value->property) {
- case PropertyID::FontSize: {
- VERIFY(!font_size);
- font_size = value.release_nonnull();
- // Consume `/ line-height` if present
- if (tokens.next_token().is_delim('/')) {
- tokens.discard_a_token();
- auto maybe_line_height = parse_css_value_for_property(PropertyID::LineHeight, tokens);
- if (!maybe_line_height)
- return nullptr;
- line_height = maybe_line_height.release_nonnull();
- }
- // Consume font-families
- auto maybe_font_families = parse_font_family_value(tokens);
- // font-family comes last, so we must not have any tokens left over.
- if (!maybe_font_families || tokens.has_next_token())
- return nullptr;
- font_families = maybe_font_families.release_nonnull();
- continue;
- }
- case PropertyID::FontWidth: {
- VERIFY(!font_width);
- font_width = value.release_nonnull();
- continue;
- }
- case PropertyID::FontStyle: {
- VERIFY(!font_style);
- font_style = value.release_nonnull();
- continue;
- }
- case PropertyID::FontVariant: {
- VERIFY(!font_variant);
- font_variant = value.release_nonnull();
- continue;
- }
- case PropertyID::FontWeight: {
- VERIFY(!font_weight);
- font_weight = value.release_nonnull();
- continue;
- }
- default:
- VERIFY_NOT_REACHED();
- }
- return nullptr;
- }
- // Since normal is the default value for all the properties that can have it, we don't have to actually
- // set anything to normal here. It'll be set when we create the ShorthandStyleValue below.
- // We just need to make sure we were not given more normals than will fit.
- int unset_value_count = (font_style ? 0 : 1) + (font_weight ? 0 : 1) + (font_variant ? 0 : 1) + (font_width ? 0 : 1);
- if (unset_value_count < normal_count)
- return nullptr;
- if (!font_size || !font_families)
- return nullptr;
- if (!font_style)
- font_style = property_initial_value(m_context.realm(), PropertyID::FontStyle);
- if (!font_variant)
- font_variant = property_initial_value(m_context.realm(), PropertyID::FontVariant);
- if (!font_weight)
- font_weight = property_initial_value(m_context.realm(), PropertyID::FontWeight);
- if (!font_width)
- font_width = property_initial_value(m_context.realm(), PropertyID::FontWidth);
- if (!line_height)
- line_height = property_initial_value(m_context.realm(), PropertyID::LineHeight);
- transaction.commit();
- return ShorthandStyleValue::create(PropertyID::Font,
- { PropertyID::FontStyle, PropertyID::FontVariant, PropertyID::FontWeight, PropertyID::FontWidth, PropertyID::FontSize, PropertyID::LineHeight, PropertyID::FontFamily },
- { font_style.release_nonnull(), font_variant.release_nonnull(), font_weight.release_nonnull(), font_width.release_nonnull(), font_size.release_nonnull(), line_height.release_nonnull(), font_families.release_nonnull() });
- }
- RefPtr<CSSStyleValue> Parser::parse_font_family_value(TokenStream<ComponentValue>& tokens)
- {
- auto next_is_comma_or_eof = [&]() -> bool {
- return !tokens.has_next_token() || tokens.next_token().is(Token::Type::Comma);
- };
- // Note: Font-family names can either be a quoted string, or a keyword, or a series of custom-idents.
- // eg, these are equivalent:
- // font-family: my cool font\!, serif;
- // font-family: "my cool font!", serif;
- StyleValueVector font_families;
- Vector<String> current_name_parts;
- while (tokens.has_next_token()) {
- auto const& peek = tokens.next_token();
- if (peek.is(Token::Type::String)) {
- // `font-family: my cool "font";` is invalid.
- if (!current_name_parts.is_empty())
- return nullptr;
- tokens.discard_a_token(); // String
- if (!next_is_comma_or_eof())
- return nullptr;
- font_families.append(StringStyleValue::create(peek.token().string()));
- tokens.discard_a_token(); // Comma
- continue;
- }
- if (peek.is(Token::Type::Ident)) {
- // If this is a valid identifier, it's NOT a custom-ident and can't be part of a larger name.
- // CSS-wide keywords are not allowed
- if (auto builtin = parse_builtin_value(tokens))
- return nullptr;
- auto maybe_keyword = keyword_from_string(peek.token().ident());
- // Can't have a generic-font-name as a token in an unquoted font name.
- if (maybe_keyword.has_value() && is_generic_font_family(maybe_keyword.value())) {
- if (!current_name_parts.is_empty())
- return nullptr;
- tokens.discard_a_token(); // Ident
- if (!next_is_comma_or_eof())
- return nullptr;
- font_families.append(CSSKeywordValue::create(maybe_keyword.value()));
- tokens.discard_a_token(); // Comma
- continue;
- }
- current_name_parts.append(tokens.consume_a_token().token().ident().to_string());
- continue;
- }
- if (peek.is(Token::Type::Comma)) {
- if (current_name_parts.is_empty())
- return nullptr;
- tokens.discard_a_token(); // Comma
- // This is really a series of custom-idents, not just one. But for the sake of simplicity we'll make it one.
- font_families.append(CustomIdentStyleValue::create(MUST(String::join(' ', current_name_parts))));
- current_name_parts.clear();
- // Can't have a trailing comma
- if (!tokens.has_next_token())
- return nullptr;
- continue;
- }
- return nullptr;
- }
- if (!current_name_parts.is_empty()) {
- // This is really a series of custom-idents, not just one. But for the sake of simplicity we'll make it one.
- font_families.append(CustomIdentStyleValue::create(MUST(String::join(' ', current_name_parts))));
- current_name_parts.clear();
- }
- if (font_families.is_empty())
- return nullptr;
- return StyleValueList::create(move(font_families), StyleValueList::Separator::Comma);
- }
- RefPtr<CSSStyleValue> Parser::parse_font_language_override_value(TokenStream<ComponentValue>& tokens)
- {
- // https://drafts.csswg.org/css-fonts/#propdef-font-language-override
- // This is `normal | <string>` but with the constraint that the string has to be 4 characters long:
- // Shorter strings are right-padded with spaces, and longer strings are invalid.
- if (auto normal = parse_all_as_single_keyword_value(tokens, Keyword::Normal))
- return normal;
- auto transaction = tokens.begin_transaction();
- tokens.discard_whitespace();
- if (auto string = parse_string_value(tokens)) {
- auto string_value = string->string_value();
- tokens.discard_whitespace();
- if (tokens.has_next_token()) {
- dbgln_if(CSS_PARSER_DEBUG, "CSSParser: Failed to parse font-language-override: unexpected trailing tokens");
- return nullptr;
- }
- auto length = string_value.code_points().length();
- if (length > 4) {
- dbgln_if(CSS_PARSER_DEBUG, "CSSParser: Failed to parse font-language-override: <string> value \"{}\" is too long", string_value);
- return nullptr;
- }
- transaction.commit();
- if (length < 4)
- return StringStyleValue::create(MUST(String::formatted("{:<4}", string_value)));
- return string;
- }
- return nullptr;
- }
- RefPtr<CSSStyleValue> Parser::parse_font_feature_settings_value(TokenStream<ComponentValue>& tokens)
- {
- // https://drafts.csswg.org/css-fonts/#propdef-font-feature-settings
- // normal | <feature-tag-value>#
- // normal
- if (auto normal = parse_all_as_single_keyword_value(tokens, Keyword::Normal))
- return normal;
- // <feature-tag-value>#
- auto transaction = tokens.begin_transaction();
- auto tag_values = parse_a_comma_separated_list_of_component_values(tokens);
- // "The computed value of font-feature-settings is a map, so any duplicates in the specified value must not be preserved.
- // If the same feature tag appears more than once, the value associated with the last appearance supersedes any previous
- // value for that axis."
- // So, we deduplicate them here using a HashSet.
- OrderedHashMap<FlyString, NonnullRefPtr<OpenTypeTaggedStyleValue>> feature_tags_map;
- for (auto const& values : tag_values) {
- // <feature-tag-value> = <opentype-tag> [ <integer [0,∞]> | on | off ]?
- TokenStream tag_tokens { values };
- tag_tokens.discard_whitespace();
- auto opentype_tag = parse_opentype_tag_value(tag_tokens);
- tag_tokens.discard_whitespace();
- RefPtr<CSSStyleValue> value;
- if (tag_tokens.has_next_token()) {
- if (auto integer = parse_integer_value(tag_tokens)) {
- if (integer->is_integer() && integer->as_integer().value() < 0)
- return nullptr;
- value = integer;
- } else {
- // A value of on is synonymous with 1 and off is synonymous with 0.
- auto keyword = parse_keyword_value(tag_tokens);
- if (!keyword)
- return nullptr;
- switch (keyword->to_keyword()) {
- case Keyword::On:
- value = IntegerStyleValue::create(1);
- break;
- case Keyword::Off:
- value = IntegerStyleValue::create(0);
- break;
- default:
- return nullptr;
- }
- }
- tag_tokens.discard_whitespace();
- } else {
- // "If the value is omitted, a value of 1 is assumed."
- value = IntegerStyleValue::create(1);
- }
- if (!opentype_tag || !value || tag_tokens.has_next_token())
- return nullptr;
- feature_tags_map.set(opentype_tag->string_value(), OpenTypeTaggedStyleValue::create(opentype_tag->string_value(), value.release_nonnull()));
- }
- // "The computed value contains the de-duplicated feature tags, sorted in ascending order by code unit."
- StyleValueVector feature_tags;
- feature_tags.ensure_capacity(feature_tags_map.size());
- for (auto const& [key, feature_tag] : feature_tags_map)
- feature_tags.append(feature_tag);
- quick_sort(feature_tags, [](auto& a, auto& b) {
- return a->as_open_type_tagged().tag() < b->as_open_type_tagged().tag();
- });
- transaction.commit();
- return StyleValueList::create(move(feature_tags), StyleValueList::Separator::Comma);
- }
- RefPtr<CSSStyleValue> Parser::parse_font_variation_settings_value(TokenStream<ComponentValue>& tokens)
- {
- // https://drafts.csswg.org/css-fonts/#propdef-font-variation-settings
- // normal | [ <opentype-tag> <number>]#
- // normal
- if (auto normal = parse_all_as_single_keyword_value(tokens, Keyword::Normal))
- return normal;
- // [ <opentype-tag> <number>]#
- auto transaction = tokens.begin_transaction();
- auto tag_values = parse_a_comma_separated_list_of_component_values(tokens);
- // "If the same axis name appears more than once, the value associated with the last appearance supersedes any
- // previous value for that axis. This deduplication is observable by accessing the computed value of this property."
- // So, we deduplicate them here using a HashSet.
- OrderedHashMap<FlyString, NonnullRefPtr<OpenTypeTaggedStyleValue>> axis_tags_map;
- for (auto const& values : tag_values) {
- TokenStream tag_tokens { values };
- tag_tokens.discard_whitespace();
- auto opentype_tag = parse_opentype_tag_value(tag_tokens);
- tag_tokens.discard_whitespace();
- auto number = parse_number_value(tag_tokens);
- tag_tokens.discard_whitespace();
- if (!opentype_tag || !number || tag_tokens.has_next_token())
- return nullptr;
- axis_tags_map.set(opentype_tag->string_value(), OpenTypeTaggedStyleValue::create(opentype_tag->string_value(), number.release_nonnull()));
- }
- // "The computed value contains the de-duplicated axis names, sorted in ascending order by code unit."
- StyleValueVector axis_tags;
- axis_tags.ensure_capacity(axis_tags_map.size());
- for (auto const& [key, axis_tag] : axis_tags_map)
- axis_tags.append(axis_tag);
- quick_sort(axis_tags, [](auto& a, auto& b) {
- return a->as_open_type_tagged().tag() < b->as_open_type_tagged().tag();
- });
- transaction.commit();
- return StyleValueList::create(move(axis_tags), StyleValueList::Separator::Comma);
- }
- Vector<ParsedFontFace::Source> Parser::parse_as_font_face_src()
- {
- return parse_font_face_src(m_token_stream);
- }
- template<typename T>
- Vector<ParsedFontFace::Source> Parser::parse_font_face_src(TokenStream<T>& component_values)
- {
- // FIXME: Get this information from the system somehow?
- // Format-name table: https://www.w3.org/TR/css-fonts-4/#font-format-definitions
- auto font_format_is_supported = [](StringView name) {
- // The spec requires us to treat opentype and truetype as synonymous.
- if (name.is_one_of_ignoring_ascii_case("opentype"sv, "truetype"sv, "woff"sv, "woff2"sv))
- return true;
- return false;
- };
- Vector<ParsedFontFace::Source> supported_sources;
- auto list_of_source_token_lists = parse_a_comma_separated_list_of_component_values(component_values);
- for (auto const& source_token_list : list_of_source_token_lists) {
- TokenStream source_tokens { source_token_list };
- source_tokens.discard_whitespace();
- // <url> [ format(<font-format>)]?
- // FIXME: Implement optional tech() function from CSS-Fonts-4.
- if (auto maybe_url = parse_url_function(source_tokens); maybe_url.has_value()) {
- auto url = maybe_url.release_value();
- if (!url.is_valid()) {
- continue;
- }
- Optional<FlyString> format;
- source_tokens.discard_whitespace();
- if (!source_tokens.has_next_token()) {
- supported_sources.empend(move(url), format);
- continue;
- }
- auto const& maybe_function = source_tokens.consume_a_token();
- if (!maybe_function.is_function()) {
- dbgln_if(CSS_PARSER_DEBUG, "CSSParser: @font-face src invalid (token after `url()` that isn't a function: {}); discarding.", maybe_function.to_debug_string());
- return {};
- }
- auto const& function = maybe_function.function();
- if (function.name.equals_ignoring_ascii_case("format"sv)) {
- TokenStream format_tokens { function.value };
- format_tokens.discard_whitespace();
- auto const& format_name_token = format_tokens.consume_a_token();
- StringView format_name;
- if (format_name_token.is(Token::Type::Ident)) {
- format_name = format_name_token.token().ident();
- } else if (format_name_token.is(Token::Type::String)) {
- format_name = format_name_token.token().string();
- } else {
- dbgln_if(CSS_PARSER_DEBUG, "CSSParser: @font-face src invalid (`format()` parameter not an ident or string; is: {}); discarding.", format_name_token.to_debug_string());
- return {};
- }
- if (!font_format_is_supported(format_name)) {
- dbgln_if(CSS_PARSER_DEBUG, "CSSParser: @font-face src format({}) not supported; skipping.", format_name);
- continue;
- }
- format = FlyString::from_utf8(format_name).release_value_but_fixme_should_propagate_errors();
- } else {
- dbgln_if(CSS_PARSER_DEBUG, "CSSParser: @font-face src invalid (unrecognized function token `{}`); discarding.", function.name);
- return {};
- }
- source_tokens.discard_whitespace();
- if (source_tokens.has_next_token()) {
- dbgln_if(CSS_PARSER_DEBUG, "CSSParser: @font-face src invalid (extra token `{}`); discarding.", source_tokens.next_token().to_debug_string());
- return {};
- }
- supported_sources.empend(move(url), format);
- continue;
- }
- auto const& first = source_tokens.consume_a_token();
- if (first.is_function("local"sv)) {
- if (first.function().value.is_empty()) {
- continue;
- }
- supported_sources.empend(first.function().value.first().to_string(), Optional<FlyString> {});
- continue;
- }
- dbgln_if(CSS_PARSER_DEBUG, "CSSParser: @font-face src invalid (failed to parse url from: {}); discarding.", first.to_debug_string());
- return {};
- }
- return supported_sources;
- }
- template Vector<ParsedFontFace::Source> Parser::parse_font_face_src(TokenStream<Token>& component_values);
- template Vector<ParsedFontFace::Source> Parser::parse_font_face_src(TokenStream<ComponentValue>& component_values);
- RefPtr<CSSStyleValue> Parser::parse_list_style_value(TokenStream<ComponentValue>& tokens)
- {
- RefPtr<CSSStyleValue> list_position;
- RefPtr<CSSStyleValue> list_image;
- RefPtr<CSSStyleValue> list_type;
- int found_nones = 0;
- Vector<PropertyID> remaining_longhands { PropertyID::ListStyleImage, PropertyID::ListStylePosition, PropertyID::ListStyleType };
- auto transaction = tokens.begin_transaction();
- while (tokens.has_next_token()) {
- if (auto const& peek = tokens.next_token(); peek.is_ident("none"sv)) {
- tokens.discard_a_token();
- found_nones++;
- continue;
- }
- auto property_and_value = parse_css_value_for_properties(remaining_longhands, tokens);
- if (!property_and_value.has_value())
- return nullptr;
- auto& value = property_and_value->style_value;
- remove_property(remaining_longhands, property_and_value->property);
- switch (property_and_value->property) {
- case PropertyID::ListStylePosition: {
- VERIFY(!list_position);
- list_position = value.release_nonnull();
- continue;
- }
- case PropertyID::ListStyleImage: {
- VERIFY(!list_image);
- list_image = value.release_nonnull();
- continue;
- }
- case PropertyID::ListStyleType: {
- VERIFY(!list_type);
- list_type = value.release_nonnull();
- continue;
- }
- default:
- VERIFY_NOT_REACHED();
- }
- }
- if (found_nones > 2)
- return nullptr;
- if (found_nones == 2) {
- if (list_image || list_type)
- return nullptr;
- auto none = CSSKeywordValue::create(Keyword::None);
- list_image = none;
- list_type = none;
- } else if (found_nones == 1) {
- if (list_image && list_type)
- return nullptr;
- auto none = CSSKeywordValue::create(Keyword::None);
- if (!list_image)
- list_image = none;
- if (!list_type)
- list_type = none;
- }
- if (!list_position)
- list_position = property_initial_value(m_context.realm(), PropertyID::ListStylePosition);
- if (!list_image)
- list_image = property_initial_value(m_context.realm(), PropertyID::ListStyleImage);
- if (!list_type)
- list_type = property_initial_value(m_context.realm(), PropertyID::ListStyleType);
- transaction.commit();
- return ShorthandStyleValue::create(PropertyID::ListStyle,
- { PropertyID::ListStylePosition, PropertyID::ListStyleImage, PropertyID::ListStyleType },
- { list_position.release_nonnull(), list_image.release_nonnull(), list_type.release_nonnull() });
- }
- RefPtr<CSSStyleValue> Parser::parse_math_depth_value(TokenStream<ComponentValue>& tokens)
- {
- // https://w3c.github.io/mathml-core/#propdef-math-depth
- // auto-add | add(<integer>) | <integer>
- auto transaction = tokens.begin_transaction();
- auto const& token = tokens.consume_a_token();
- if (tokens.has_next_token())
- return nullptr;
- // auto-add
- if (token.is_ident("auto-add"sv)) {
- transaction.commit();
- return MathDepthStyleValue::create_auto_add();
- }
- // FIXME: Make it easier to parse "thing that might be <bar> or literally anything that resolves to it" and get rid of this
- auto parse_something_that_resolves_to_integer = [this](ComponentValue const& token) -> RefPtr<CSSStyleValue> {
- if (token.is(Token::Type::Number) && token.token().number().is_integer())
- return IntegerStyleValue::create(token.token().to_integer());
- if (auto value = parse_calculated_value(token); value && value->resolves_to_number())
- return value;
- return nullptr;
- };
- // add(<integer>)
- if (token.is_function("add"sv)) {
- auto add_tokens = TokenStream { token.function().value };
- add_tokens.discard_whitespace();
- auto const& integer_token = add_tokens.consume_a_token();
- add_tokens.discard_whitespace();
- if (add_tokens.has_next_token())
- return nullptr;
- if (auto integer_value = parse_something_that_resolves_to_integer(integer_token)) {
- transaction.commit();
- return MathDepthStyleValue::create_add(integer_value.release_nonnull());
- }
- return nullptr;
- }
- // <integer>
- if (auto integer_value = parse_something_that_resolves_to_integer(token)) {
- transaction.commit();
- return MathDepthStyleValue::create_integer(integer_value.release_nonnull());
- }
- return nullptr;
- }
- RefPtr<CSSStyleValue> Parser::parse_overflow_value(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- auto maybe_x_value = parse_css_value_for_property(PropertyID::OverflowX, tokens);
- if (!maybe_x_value)
- return nullptr;
- auto maybe_y_value = parse_css_value_for_property(PropertyID::OverflowY, tokens);
- transaction.commit();
- if (maybe_y_value) {
- return ShorthandStyleValue::create(PropertyID::Overflow,
- { PropertyID::OverflowX, PropertyID::OverflowY },
- { maybe_x_value.release_nonnull(), maybe_y_value.release_nonnull() });
- }
- return ShorthandStyleValue::create(PropertyID::Overflow,
- { PropertyID::OverflowX, PropertyID::OverflowY },
- { *maybe_x_value, *maybe_x_value });
- }
- RefPtr<CSSStyleValue> Parser::parse_place_content_value(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- auto maybe_align_content_value = parse_css_value_for_property(PropertyID::AlignContent, tokens);
- if (!maybe_align_content_value)
- return nullptr;
- if (!tokens.has_next_token()) {
- if (!property_accepts_keyword(PropertyID::JustifyContent, maybe_align_content_value->to_keyword()))
- return nullptr;
- transaction.commit();
- return ShorthandStyleValue::create(PropertyID::PlaceContent,
- { PropertyID::AlignContent, PropertyID::JustifyContent },
- { *maybe_align_content_value, *maybe_align_content_value });
- }
- auto maybe_justify_content_value = parse_css_value_for_property(PropertyID::JustifyContent, tokens);
- if (!maybe_justify_content_value)
- return nullptr;
- transaction.commit();
- return ShorthandStyleValue::create(PropertyID::PlaceContent,
- { PropertyID::AlignContent, PropertyID::JustifyContent },
- { maybe_align_content_value.release_nonnull(), maybe_justify_content_value.release_nonnull() });
- }
- RefPtr<CSSStyleValue> Parser::parse_place_items_value(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- auto maybe_align_items_value = parse_css_value_for_property(PropertyID::AlignItems, tokens);
- if (!maybe_align_items_value)
- return nullptr;
- if (!tokens.has_next_token()) {
- if (!property_accepts_keyword(PropertyID::JustifyItems, maybe_align_items_value->to_keyword()))
- return nullptr;
- transaction.commit();
- return ShorthandStyleValue::create(PropertyID::PlaceItems,
- { PropertyID::AlignItems, PropertyID::JustifyItems },
- { *maybe_align_items_value, *maybe_align_items_value });
- }
- auto maybe_justify_items_value = parse_css_value_for_property(PropertyID::JustifyItems, tokens);
- if (!maybe_justify_items_value)
- return nullptr;
- transaction.commit();
- return ShorthandStyleValue::create(PropertyID::PlaceItems,
- { PropertyID::AlignItems, PropertyID::JustifyItems },
- { *maybe_align_items_value, *maybe_justify_items_value });
- }
- RefPtr<CSSStyleValue> Parser::parse_place_self_value(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- auto maybe_align_self_value = parse_css_value_for_property(PropertyID::AlignSelf, tokens);
- if (!maybe_align_self_value)
- return nullptr;
- if (!tokens.has_next_token()) {
- if (!property_accepts_keyword(PropertyID::JustifySelf, maybe_align_self_value->to_keyword()))
- return nullptr;
- transaction.commit();
- return ShorthandStyleValue::create(PropertyID::PlaceSelf,
- { PropertyID::AlignSelf, PropertyID::JustifySelf },
- { *maybe_align_self_value, *maybe_align_self_value });
- }
- auto maybe_justify_self_value = parse_css_value_for_property(PropertyID::JustifySelf, tokens);
- if (!maybe_justify_self_value)
- return nullptr;
- transaction.commit();
- return ShorthandStyleValue::create(PropertyID::PlaceSelf,
- { PropertyID::AlignSelf, PropertyID::JustifySelf },
- { *maybe_align_self_value, *maybe_justify_self_value });
- }
- RefPtr<CSSStyleValue> Parser::parse_quotes_value(TokenStream<ComponentValue>& tokens)
- {
- // https://www.w3.org/TR/css-content-3/#quotes-property
- // auto | none | [ <string> <string> ]+
- auto transaction = tokens.begin_transaction();
- if (tokens.remaining_token_count() == 1) {
- auto keyword = parse_keyword_value(tokens);
- if (keyword && property_accepts_keyword(PropertyID::Quotes, keyword->to_keyword())) {
- transaction.commit();
- return keyword;
- }
- return nullptr;
- }
- // Parse an even number of <string> values.
- if (tokens.remaining_token_count() % 2 != 0)
- return nullptr;
- StyleValueVector string_values;
- while (tokens.has_next_token()) {
- auto maybe_string = parse_string_value(tokens);
- if (!maybe_string)
- return nullptr;
- string_values.append(maybe_string.release_nonnull());
- }
- transaction.commit();
- return StyleValueList::create(move(string_values), StyleValueList::Separator::Space);
- }
- RefPtr<CSSStyleValue> Parser::parse_text_decoration_value(TokenStream<ComponentValue>& tokens)
- {
- RefPtr<CSSStyleValue> decoration_line;
- RefPtr<CSSStyleValue> decoration_thickness;
- RefPtr<CSSStyleValue> decoration_style;
- RefPtr<CSSStyleValue> decoration_color;
- auto remaining_longhands = Vector { PropertyID::TextDecorationColor, PropertyID::TextDecorationLine, PropertyID::TextDecorationStyle, PropertyID::TextDecorationThickness };
- auto transaction = tokens.begin_transaction();
- while (tokens.has_next_token()) {
- auto property_and_value = parse_css_value_for_properties(remaining_longhands, tokens);
- if (!property_and_value.has_value())
- return nullptr;
- auto& value = property_and_value->style_value;
- remove_property(remaining_longhands, property_and_value->property);
- switch (property_and_value->property) {
- case PropertyID::TextDecorationColor: {
- VERIFY(!decoration_color);
- decoration_color = value.release_nonnull();
- continue;
- }
- case PropertyID::TextDecorationLine: {
- VERIFY(!decoration_line);
- tokens.reconsume_current_input_token();
- auto parsed_decoration_line = parse_text_decoration_line_value(tokens);
- if (!parsed_decoration_line)
- return nullptr;
- decoration_line = parsed_decoration_line.release_nonnull();
- continue;
- }
- case PropertyID::TextDecorationThickness: {
- VERIFY(!decoration_thickness);
- decoration_thickness = value.release_nonnull();
- continue;
- }
- case PropertyID::TextDecorationStyle: {
- VERIFY(!decoration_style);
- decoration_style = value.release_nonnull();
- continue;
- }
- default:
- VERIFY_NOT_REACHED();
- }
- }
- if (!decoration_line)
- decoration_line = property_initial_value(m_context.realm(), PropertyID::TextDecorationLine);
- if (!decoration_thickness)
- decoration_thickness = property_initial_value(m_context.realm(), PropertyID::TextDecorationThickness);
- if (!decoration_style)
- decoration_style = property_initial_value(m_context.realm(), PropertyID::TextDecorationStyle);
- if (!decoration_color)
- decoration_color = property_initial_value(m_context.realm(), PropertyID::TextDecorationColor);
- transaction.commit();
- return ShorthandStyleValue::create(PropertyID::TextDecoration,
- { PropertyID::TextDecorationLine, PropertyID::TextDecorationThickness, PropertyID::TextDecorationStyle, PropertyID::TextDecorationColor },
- { decoration_line.release_nonnull(), decoration_thickness.release_nonnull(), decoration_style.release_nonnull(), decoration_color.release_nonnull() });
- }
- RefPtr<CSSStyleValue> Parser::parse_text_decoration_line_value(TokenStream<ComponentValue>& tokens)
- {
- StyleValueVector style_values;
- while (tokens.has_next_token()) {
- auto maybe_value = parse_css_value_for_property(PropertyID::TextDecorationLine, tokens);
- if (!maybe_value)
- break;
- auto value = maybe_value.release_nonnull();
- if (auto maybe_line = keyword_to_text_decoration_line(value->to_keyword()); maybe_line.has_value()) {
- if (maybe_line == TextDecorationLine::None) {
- if (!style_values.is_empty())
- break;
- return value;
- }
- if (style_values.contains_slow(value))
- break;
- style_values.append(move(value));
- continue;
- }
- break;
- }
- if (style_values.is_empty())
- return nullptr;
- return StyleValueList::create(move(style_values), StyleValueList::Separator::Space);
- }
- RefPtr<CSSStyleValue> Parser::parse_easing_value(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- tokens.discard_whitespace();
- auto const& part = tokens.consume_a_token();
- if (part.is(Token::Type::Ident)) {
- auto name = part.token().ident();
- auto maybe_simple_easing = [&] -> RefPtr<EasingStyleValue> {
- if (name.equals_ignoring_ascii_case("linear"sv))
- return EasingStyleValue::create(EasingStyleValue::Linear::identity());
- if (name.equals_ignoring_ascii_case("ease"sv))
- return EasingStyleValue::create(EasingStyleValue::CubicBezier::ease());
- if (name.equals_ignoring_ascii_case("ease-in"sv))
- return EasingStyleValue::create(EasingStyleValue::CubicBezier::ease_in());
- if (name.equals_ignoring_ascii_case("ease-out"sv))
- return EasingStyleValue::create(EasingStyleValue::CubicBezier::ease_out());
- if (name.equals_ignoring_ascii_case("ease-in-out"sv))
- return EasingStyleValue::create(EasingStyleValue::CubicBezier::ease_in_out());
- if (name.equals_ignoring_ascii_case("step-start"sv))
- return EasingStyleValue::create(EasingStyleValue::Steps::step_start());
- if (name.equals_ignoring_ascii_case("step-end"sv))
- return EasingStyleValue::create(EasingStyleValue::Steps::step_end());
- return {};
- }();
- if (!maybe_simple_easing)
- return nullptr;
- transaction.commit();
- return maybe_simple_easing;
- }
- if (!part.is_function())
- return nullptr;
- TokenStream argument_tokens { part.function().value };
- auto comma_separated_arguments = parse_a_comma_separated_list_of_component_values(argument_tokens);
- // Remove whitespace
- for (auto& argument : comma_separated_arguments)
- argument.remove_all_matching([](auto& value) { return value.is(Token::Type::Whitespace); });
- auto name = part.function().name;
- if (name.equals_ignoring_ascii_case("linear"sv)) {
- // linear() = linear( [ <number> && <percentage>{0,2} ]# )
- Vector<EasingStyleValue::Linear::Stop> stops;
- for (auto const& argument : comma_separated_arguments) {
- TokenStream argument_tokens { argument };
- Optional<double> output;
- Optional<double> first_input;
- Optional<double> second_input;
- if (argument_tokens.next_token().is(Token::Type::Number))
- output = argument_tokens.consume_a_token().token().number_value();
- if (argument_tokens.next_token().is(Token::Type::Percentage)) {
- first_input = argument_tokens.consume_a_token().token().percentage() / 100;
- if (argument_tokens.next_token().is(Token::Type::Percentage)) {
- second_input = argument_tokens.consume_a_token().token().percentage() / 100;
- }
- }
- if (argument_tokens.next_token().is(Token::Type::Number)) {
- if (output.has_value())
- return nullptr;
- output = argument_tokens.consume_a_token().token().number_value();
- }
- if (argument_tokens.has_next_token() || !output.has_value())
- return nullptr;
- stops.append({ output.value(), first_input, first_input.has_value() });
- if (second_input.has_value())
- stops.append({ output.value(), second_input, true });
- }
- if (stops.is_empty())
- return nullptr;
- transaction.commit();
- return EasingStyleValue::create(EasingStyleValue::Linear { move(stops) });
- }
- if (name.equals_ignoring_ascii_case("cubic-bezier"sv)) {
- if (comma_separated_arguments.size() != 4)
- return nullptr;
- for (auto const& argument : comma_separated_arguments) {
- if (argument.size() != 1)
- return nullptr;
- if (!argument[0].is(Token::Type::Number))
- return nullptr;
- }
- EasingStyleValue::CubicBezier bezier {
- comma_separated_arguments[0][0].token().number_value(),
- comma_separated_arguments[1][0].token().number_value(),
- comma_separated_arguments[2][0].token().number_value(),
- comma_separated_arguments[3][0].token().number_value(),
- };
- if (bezier.x1 < 0.0 || bezier.x1 > 1.0 || bezier.x2 < 0.0 || bezier.x2 > 1.0)
- return nullptr;
- transaction.commit();
- return EasingStyleValue::create(bezier);
- }
- if (name.equals_ignoring_ascii_case("steps"sv)) {
- if (comma_separated_arguments.is_empty() || comma_separated_arguments.size() > 2)
- return nullptr;
- for (auto const& argument : comma_separated_arguments) {
- if (argument.size() != 1)
- return nullptr;
- }
- EasingStyleValue::Steps steps;
- auto const& intervals_argument = comma_separated_arguments[0][0];
- if (!intervals_argument.is(Token::Type::Number))
- return nullptr;
- if (!intervals_argument.token().number().is_integer())
- return nullptr;
- auto intervals = intervals_argument.token().to_integer();
- if (comma_separated_arguments.size() == 2) {
- TokenStream identifier_stream { comma_separated_arguments[1] };
- auto keyword_value = parse_keyword_value(identifier_stream);
- if (!keyword_value)
- return nullptr;
- switch (keyword_value->to_keyword()) {
- case Keyword::JumpStart:
- steps.position = EasingStyleValue::Steps::Position::JumpStart;
- break;
- case Keyword::JumpEnd:
- steps.position = EasingStyleValue::Steps::Position::JumpEnd;
- break;
- case Keyword::JumpBoth:
- steps.position = EasingStyleValue::Steps::Position::JumpBoth;
- break;
- case Keyword::JumpNone:
- steps.position = EasingStyleValue::Steps::Position::JumpNone;
- break;
- case Keyword::Start:
- steps.position = EasingStyleValue::Steps::Position::Start;
- break;
- case Keyword::End:
- steps.position = EasingStyleValue::Steps::Position::End;
- break;
- default:
- return nullptr;
- }
- }
- // Perform extra validation
- // https://drafts.csswg.org/css-easing/#step-easing-functions
- // If the <step-position> is jump-none, the <integer> must be at least 2, or the function is invalid.
- // Otherwise, the <integer> must be at least 1, or the function is invalid.
- if (steps.position == EasingStyleValue::Steps::Position::JumpNone) {
- if (intervals <= 1)
- return nullptr;
- } else if (intervals <= 0) {
- return nullptr;
- }
- steps.number_of_intervals = intervals;
- transaction.commit();
- return EasingStyleValue::create(steps);
- }
- return nullptr;
- }
- // https://www.w3.org/TR/css-transforms-1/#transform-property
- RefPtr<CSSStyleValue> Parser::parse_transform_value(TokenStream<ComponentValue>& tokens)
- {
- // <transform> = none | <transform-list>
- // <transform-list> = <transform-function>+
- if (auto none = parse_all_as_single_keyword_value(tokens, Keyword::None))
- return none;
- StyleValueVector transformations;
- auto transaction = tokens.begin_transaction();
- while (tokens.has_next_token()) {
- auto const& part = tokens.consume_a_token();
- if (!part.is_function())
- return nullptr;
- auto maybe_function = transform_function_from_string(part.function().name);
- if (!maybe_function.has_value())
- return nullptr;
- auto function = maybe_function.release_value();
- auto function_metadata = transform_function_metadata(function);
- auto function_tokens = TokenStream { part.function().value };
- auto arguments = parse_a_comma_separated_list_of_component_values(function_tokens);
- if (arguments.size() > function_metadata.parameters.size()) {
- dbgln_if(CSS_PARSER_DEBUG, "Too many arguments to {}. max: {}", part.function().name, function_metadata.parameters.size());
- return nullptr;
- }
- if (arguments.size() < function_metadata.parameters.size() && function_metadata.parameters[arguments.size()].required) {
- dbgln_if(CSS_PARSER_DEBUG, "Required parameter at position {} is missing", arguments.size());
- return nullptr;
- }
- StyleValueVector values;
- for (auto argument_index = 0u; argument_index < arguments.size(); ++argument_index) {
- TokenStream argument_tokens { arguments[argument_index] };
- argument_tokens.discard_whitespace();
- auto const& value = argument_tokens.consume_a_token();
- RefPtr<CSSMathValue> maybe_calc_value = parse_calculated_value(value);
- switch (function_metadata.parameters[argument_index].type) {
- case TransformFunctionParameterType::Angle: {
- // These are `<angle> | <zero>` in the spec, so we have to check for both kinds.
- if (maybe_calc_value && maybe_calc_value->resolves_to_angle()) {
- values.append(maybe_calc_value.release_nonnull());
- } else if (value.is(Token::Type::Number) && value.token().number_value() == 0) {
- values.append(AngleStyleValue::create(Angle::make_degrees(0)));
- } else {
- // FIXME: Remove this reconsume once all parsing functions are TokenStream-based.
- argument_tokens.reconsume_current_input_token();
- auto dimension_value = parse_dimension_value(argument_tokens);
- if (!dimension_value || !dimension_value->is_angle())
- return nullptr;
- values.append(dimension_value.release_nonnull());
- }
- break;
- }
- case TransformFunctionParameterType::Length:
- case TransformFunctionParameterType::LengthNone: {
- if (maybe_calc_value && maybe_calc_value->resolves_to_length()) {
- argument_tokens.discard_a_token(); // calc()
- values.append(maybe_calc_value.release_nonnull());
- } else {
- // FIXME: Remove this reconsume once all parsing functions are TokenStream-based.
- argument_tokens.reconsume_current_input_token();
- if (function_metadata.parameters[argument_index].type == TransformFunctionParameterType::LengthNone) {
- auto keyword_transaction = argument_tokens.begin_transaction();
- auto keyword_value = parse_keyword_value(argument_tokens);
- if (keyword_value && keyword_value->to_keyword() == Keyword::None) {
- values.append(keyword_value.release_nonnull());
- keyword_transaction.commit();
- break;
- }
- }
- auto dimension_value = parse_dimension_value(argument_tokens);
- if (!dimension_value || !dimension_value->is_length())
- return nullptr;
- values.append(dimension_value.release_nonnull());
- }
- break;
- }
- case TransformFunctionParameterType::LengthPercentage: {
- if (maybe_calc_value && maybe_calc_value->resolves_to_length_percentage()) {
- values.append(maybe_calc_value.release_nonnull());
- } else {
- // FIXME: Remove this reconsume once all parsing functions are TokenStream-based.
- argument_tokens.reconsume_current_input_token();
- auto dimension_value = parse_dimension_value(argument_tokens);
- if (!dimension_value)
- return nullptr;
- if (dimension_value->is_percentage() || dimension_value->is_length())
- values.append(dimension_value.release_nonnull());
- else
- return nullptr;
- }
- break;
- }
- case TransformFunctionParameterType::Number: {
- if (maybe_calc_value && maybe_calc_value->resolves_to_number()) {
- values.append(maybe_calc_value.release_nonnull());
- } else {
- // FIXME: Remove this reconsume once all parsing functions are TokenStream-based.
- argument_tokens.reconsume_current_input_token();
- auto number = parse_number_value(argument_tokens);
- if (!number)
- return nullptr;
- values.append(number.release_nonnull());
- }
- break;
- }
- case TransformFunctionParameterType::NumberPercentage: {
- if (maybe_calc_value && maybe_calc_value->resolves_to_number()) {
- values.append(maybe_calc_value.release_nonnull());
- } else {
- // FIXME: Remove this reconsume once all parsing functions are TokenStream-based.
- argument_tokens.reconsume_current_input_token();
- auto number_or_percentage = parse_number_percentage_value(argument_tokens);
- if (!number_or_percentage)
- return nullptr;
- values.append(number_or_percentage.release_nonnull());
- }
- break;
- }
- }
- argument_tokens.discard_whitespace();
- if (argument_tokens.has_next_token())
- return nullptr;
- }
- transformations.append(TransformationStyleValue::create(function, move(values)));
- }
- transaction.commit();
- return StyleValueList::create(move(transformations), StyleValueList::Separator::Space);
- }
- // https://www.w3.org/TR/css-transforms-1/#propdef-transform-origin
- // FIXME: This only supports a 2D position
- RefPtr<CSSStyleValue> Parser::parse_transform_origin_value(TokenStream<ComponentValue>& tokens)
- {
- enum class Axis {
- None,
- X,
- Y,
- };
- struct AxisOffset {
- Axis axis;
- NonnullRefPtr<CSSStyleValue> offset;
- };
- auto to_axis_offset = [](RefPtr<CSSStyleValue> value) -> Optional<AxisOffset> {
- if (!value)
- return OptionalNone {};
- if (value->is_percentage())
- return AxisOffset { Axis::None, value->as_percentage() };
- if (value->is_length())
- return AxisOffset { Axis::None, value->as_length() };
- if (value->is_keyword()) {
- switch (value->to_keyword()) {
- case Keyword::Top:
- return AxisOffset { Axis::Y, PercentageStyleValue::create(Percentage(0)) };
- case Keyword::Left:
- return AxisOffset { Axis::X, PercentageStyleValue::create(Percentage(0)) };
- case Keyword::Center:
- return AxisOffset { Axis::None, PercentageStyleValue::create(Percentage(50)) };
- case Keyword::Bottom:
- return AxisOffset { Axis::Y, PercentageStyleValue::create(Percentage(100)) };
- case Keyword::Right:
- return AxisOffset { Axis::X, PercentageStyleValue::create(Percentage(100)) };
- default:
- return OptionalNone {};
- }
- }
- if (value->is_math()) {
- return AxisOffset { Axis::None, value->as_math() };
- }
- return OptionalNone {};
- };
- auto transaction = tokens.begin_transaction();
- auto make_list = [&transaction](NonnullRefPtr<CSSStyleValue> const& x_value, NonnullRefPtr<CSSStyleValue> const& y_value) -> NonnullRefPtr<StyleValueList> {
- transaction.commit();
- return StyleValueList::create(StyleValueVector { x_value, y_value }, StyleValueList::Separator::Space);
- };
- switch (tokens.remaining_token_count()) {
- case 1: {
- auto single_value = to_axis_offset(parse_css_value_for_property(PropertyID::TransformOrigin, tokens));
- if (!single_value.has_value())
- return nullptr;
- // If only one value is specified, the second value is assumed to be center.
- // FIXME: If one or two values are specified, the third value is assumed to be 0px.
- switch (single_value->axis) {
- case Axis::None:
- case Axis::X:
- return make_list(single_value->offset, PercentageStyleValue::create(Percentage(50)));
- case Axis::Y:
- return make_list(PercentageStyleValue::create(Percentage(50)), single_value->offset);
- }
- VERIFY_NOT_REACHED();
- }
- case 2: {
- auto first_value = to_axis_offset(parse_css_value_for_property(PropertyID::TransformOrigin, tokens));
- auto second_value = to_axis_offset(parse_css_value_for_property(PropertyID::TransformOrigin, tokens));
- if (!first_value.has_value() || !second_value.has_value())
- return nullptr;
- RefPtr<CSSStyleValue> x_value;
- RefPtr<CSSStyleValue> y_value;
- if (first_value->axis == Axis::X) {
- x_value = first_value->offset;
- } else if (first_value->axis == Axis::Y) {
- y_value = first_value->offset;
- }
- if (second_value->axis == Axis::X) {
- if (x_value)
- return nullptr;
- x_value = second_value->offset;
- // Put the other in Y since its axis can't have been X
- y_value = first_value->offset;
- } else if (second_value->axis == Axis::Y) {
- if (y_value)
- return nullptr;
- y_value = second_value->offset;
- // Put the other in X since its axis can't have been Y
- x_value = first_value->offset;
- } else {
- if (x_value) {
- VERIFY(!y_value);
- y_value = second_value->offset;
- } else {
- VERIFY(!x_value);
- x_value = second_value->offset;
- }
- }
- // If two or more values are defined and either no value is a keyword, or the only used keyword is center,
- // then the first value represents the horizontal position (or offset) and the second represents the vertical position (or offset).
- // FIXME: A third value always represents the Z position (or offset) and must be of type <length>.
- if (first_value->axis == Axis::None && second_value->axis == Axis::None) {
- x_value = first_value->offset;
- y_value = second_value->offset;
- }
- return make_list(x_value.release_nonnull(), y_value.release_nonnull());
- }
- }
- return nullptr;
- }
- RefPtr<CSSStyleValue> Parser::parse_transition_value(TokenStream<ComponentValue>& tokens)
- {
- if (auto none = parse_all_as_single_keyword_value(tokens, Keyword::None))
- return none;
- Vector<TransitionStyleValue::Transition> transitions;
- auto transaction = tokens.begin_transaction();
- while (tokens.has_next_token()) {
- TransitionStyleValue::Transition transition;
- auto time_value_count = 0;
- while (tokens.has_next_token() && !tokens.next_token().is(Token::Type::Comma)) {
- if (auto time = parse_time(tokens); time.has_value()) {
- switch (time_value_count) {
- case 0:
- transition.duration = time.release_value();
- break;
- case 1:
- transition.delay = time.release_value();
- break;
- default:
- dbgln_if(CSS_PARSER_DEBUG, "Transition property has more than two time values");
- return {};
- }
- time_value_count++;
- continue;
- }
- if (auto easing = parse_easing_value(tokens)) {
- if (transition.easing) {
- dbgln_if(CSS_PARSER_DEBUG, "Transition property has multiple easing values");
- return {};
- }
- transition.easing = easing->as_easing();
- continue;
- }
- if (tokens.next_token().is(Token::Type::Ident)) {
- if (transition.property_name) {
- dbgln_if(CSS_PARSER_DEBUG, "Transition property has multiple property identifiers");
- return {};
- }
- auto ident = tokens.consume_a_token().token().ident();
- if (auto property = property_id_from_string(ident); property.has_value())
- transition.property_name = CustomIdentStyleValue::create(ident);
- continue;
- }
- dbgln_if(CSS_PARSER_DEBUG, "Transition property has unexpected token \"{}\"", tokens.next_token().to_string());
- return {};
- }
- if (!transition.property_name)
- transition.property_name = CustomIdentStyleValue::create("all"_fly_string);
- if (!transition.easing)
- transition.easing = EasingStyleValue::create(EasingStyleValue::CubicBezier::ease());
- transitions.append(move(transition));
- if (!tokens.next_token().is(Token::Type::Comma))
- break;
- tokens.discard_a_token();
- }
- transaction.commit();
- return TransitionStyleValue::create(move(transitions));
- }
- RefPtr<CSSStyleValue> Parser::parse_as_css_value(PropertyID property_id)
- {
- auto component_values = parse_a_list_of_component_values(m_token_stream);
- auto tokens = TokenStream(component_values);
- auto parsed_value = parse_css_value(property_id, tokens);
- if (parsed_value.is_error())
- return nullptr;
- return parsed_value.release_value();
- }
- Optional<CSS::GridSize> Parser::parse_grid_size(ComponentValue const& component_value)
- {
- if (component_value.is_function()) {
- if (auto maybe_calculated = parse_calculated_value(component_value)) {
- if (maybe_calculated->resolves_to_length_percentage())
- return GridSize(LengthPercentage(maybe_calculated.release_nonnull()));
- // FIXME: Support calculated <flex>
- }
- return {};
- }
- if (component_value.is_ident("auto"sv))
- return GridSize::make_auto();
- if (component_value.is_ident("max-content"sv))
- return GridSize(GridSize::Type::MaxContent);
- if (component_value.is_ident("min-content"sv))
- return GridSize(GridSize::Type::MinContent);
- auto dimension = parse_dimension(component_value);
- if (!dimension.has_value())
- return {};
- if (dimension->is_length())
- return GridSize(dimension->length());
- else if (dimension->is_percentage())
- return GridSize(dimension->percentage());
- else if (dimension->is_flex())
- return GridSize(dimension->flex());
- return {};
- }
- RefPtr<CSSStyleValue> Parser::parse_translate_value(TokenStream<ComponentValue>& tokens)
- {
- if (tokens.remaining_token_count() == 1) {
- // "none"
- if (auto none = parse_all_as_single_keyword_value(tokens, Keyword::None))
- return none;
- }
- auto transaction = tokens.begin_transaction();
- auto maybe_x = parse_length_percentage(tokens);
- if (!maybe_x.has_value())
- return nullptr;
- if (!tokens.has_next_token()) {
- transaction.commit();
- return TranslationStyleValue::create(maybe_x.release_value(), LengthPercentage(Length::make_px(0)));
- }
- auto maybe_y = parse_length_percentage(tokens);
- if (!maybe_y.has_value())
- return nullptr;
- transaction.commit();
- return TranslationStyleValue::create(maybe_x.release_value(), maybe_y.release_value());
- }
- RefPtr<CSSStyleValue> Parser::parse_scale_value(TokenStream<ComponentValue>& tokens)
- {
- if (tokens.remaining_token_count() == 1) {
- // "none"
- if (auto none = parse_all_as_single_keyword_value(tokens, Keyword::None))
- return none;
- }
- auto transaction = tokens.begin_transaction();
- auto maybe_x = parse_number_percentage(tokens);
- if (!maybe_x.has_value())
- return nullptr;
- if (!tokens.has_next_token()) {
- transaction.commit();
- return ScaleStyleValue::create(maybe_x.value(), maybe_x.value());
- }
- auto maybe_y = parse_number_percentage(tokens);
- if (!maybe_y.has_value())
- return nullptr;
- transaction.commit();
- return ScaleStyleValue::create(maybe_x.release_value(), maybe_y.release_value());
- }
- Optional<CSS::GridFitContent> Parser::parse_fit_content(Vector<ComponentValue> const& component_values)
- {
- // https://www.w3.org/TR/css-grid-2/#valdef-grid-template-columns-fit-content
- // 'fit-content( <length-percentage> )'
- // Represents the formula max(minimum, min(limit, max-content)), where minimum represents an auto minimum (which is often, but not always,
- // equal to a min-content minimum), and limit is the track sizing function passed as an argument to fit-content().
- // This is essentially calculated as the smaller of minmax(auto, max-content) and minmax(auto, limit).
- auto function_tokens = TokenStream(component_values);
- function_tokens.discard_whitespace();
- auto maybe_length_percentage = parse_length_percentage(function_tokens);
- if (maybe_length_percentage.has_value())
- return CSS::GridFitContent(CSS::GridSize(CSS::GridSize::Type::FitContent, maybe_length_percentage.value()));
- return {};
- }
- Optional<CSS::GridMinMax> Parser::parse_min_max(Vector<ComponentValue> const& component_values)
- {
- // https://www.w3.org/TR/css-grid-2/#valdef-grid-template-columns-minmax
- // 'minmax(min, max)'
- // Defines a size range greater than or equal to min and less than or equal to max. If the max is
- // less than the min, then the max will be floored by the min (essentially yielding minmax(min,
- // min)). As a maximum, a <flex> value sets the track’s flex factor; it is invalid as a minimum.
- auto function_tokens = TokenStream(component_values);
- auto comma_separated_list = parse_a_comma_separated_list_of_component_values(function_tokens);
- if (comma_separated_list.size() != 2)
- return {};
- TokenStream part_one_tokens { comma_separated_list[0] };
- part_one_tokens.discard_whitespace();
- if (!part_one_tokens.has_next_token())
- return {};
- NonnullRawPtr<ComponentValue const> current_token = part_one_tokens.consume_a_token();
- auto min_grid_size = parse_grid_size(current_token);
- TokenStream part_two_tokens { comma_separated_list[1] };
- part_two_tokens.discard_whitespace();
- if (!part_two_tokens.has_next_token())
- return {};
- current_token = part_two_tokens.consume_a_token();
- auto max_grid_size = parse_grid_size(current_token);
- if (min_grid_size.has_value() && max_grid_size.has_value()) {
- // https://www.w3.org/TR/css-grid-2/#valdef-grid-template-columns-minmax
- // As a maximum, a <flex> value sets the track’s flex factor; it is invalid as a minimum.
- if (min_grid_size.value().is_flexible_length())
- return {};
- return CSS::GridMinMax(min_grid_size.value(), max_grid_size.value());
- }
- return {};
- }
- Optional<CSS::GridRepeat> Parser::parse_repeat(Vector<ComponentValue> const& component_values)
- {
- // https://www.w3.org/TR/css-grid-2/#repeat-syntax
- // 7.2.3.1. Syntax of repeat()
- // The generic form of the repeat() syntax is, approximately,
- // repeat( [ <integer [1,∞]> | auto-fill | auto-fit ] , <track-list> )
- auto is_auto_fill = false;
- auto is_auto_fit = false;
- auto function_tokens = TokenStream(component_values);
- auto comma_separated_list = parse_a_comma_separated_list_of_component_values(function_tokens);
- if (comma_separated_list.size() != 2)
- return {};
- // The first argument specifies the number of repetitions.
- TokenStream part_one_tokens { comma_separated_list[0] };
- part_one_tokens.discard_whitespace();
- if (!part_one_tokens.has_next_token())
- return {};
- auto& current_token = part_one_tokens.consume_a_token();
- auto repeat_count = 0;
- if (current_token.is(Token::Type::Number) && current_token.token().number().is_integer() && current_token.token().number_value() > 0)
- repeat_count = current_token.token().number_value();
- else if (current_token.is_ident("auto-fill"sv))
- is_auto_fill = true;
- else if (current_token.is_ident("auto-fit"sv))
- is_auto_fit = true;
- // The second argument is a track list, which is repeated that number of times.
- TokenStream part_two_tokens { comma_separated_list[1] };
- part_two_tokens.discard_whitespace();
- if (!part_two_tokens.has_next_token())
- return {};
- Vector<Variant<ExplicitGridTrack, GridLineNames>> repeat_params;
- auto last_object_was_line_names = false;
- while (part_two_tokens.has_next_token()) {
- auto const& token = part_two_tokens.consume_a_token();
- Vector<String> line_names;
- if (token.is_block()) {
- if (last_object_was_line_names)
- return {};
- last_object_was_line_names = true;
- if (!token.block().is_square())
- return {};
- TokenStream block_tokens { token.block().value };
- while (block_tokens.has_next_token()) {
- auto const& current_block_token = block_tokens.consume_a_token();
- line_names.append(current_block_token.token().ident().to_string());
- block_tokens.discard_whitespace();
- }
- repeat_params.append(GridLineNames { move(line_names) });
- part_two_tokens.discard_whitespace();
- } else {
- last_object_was_line_names = false;
- auto track_sizing_function = parse_track_sizing_function(token);
- if (!track_sizing_function.has_value())
- return {};
- // However, there are some restrictions:
- // The repeat() notation can’t be nested.
- if (track_sizing_function.value().is_repeat())
- return {};
- // Automatic repetitions (auto-fill or auto-fit) cannot be combined with intrinsic or flexible sizes.
- // Note that 'auto' is also an intrinsic size (and thus not permitted) but we can't use
- // track_sizing_function.is_auto(..) to check for it, as it requires AvailableSize, which is why there is
- // a separate check for it below.
- // https://www.w3.org/TR/css-grid-2/#repeat-syntax
- // https://www.w3.org/TR/css-grid-2/#intrinsic-sizing-function
- if (track_sizing_function.value().is_default()
- && (track_sizing_function.value().grid_size().is_flexible_length() || token.is_ident("auto"sv))
- && (is_auto_fill || is_auto_fit))
- return {};
- repeat_params.append(track_sizing_function.value());
- part_two_tokens.discard_whitespace();
- }
- }
- // Thus the precise syntax of the repeat() notation has several forms:
- // <track-repeat> = repeat( [ <integer [1,∞]> ] , [ <line-names>? <track-size> ]+ <line-names>? )
- // <auto-repeat> = repeat( [ auto-fill | auto-fit ] , [ <line-names>? <fixed-size> ]+ <line-names>? )
- // <fixed-repeat> = repeat( [ <integer [1,∞]> ] , [ <line-names>? <fixed-size> ]+ <line-names>? )
- // <name-repeat> = repeat( [ <integer [1,∞]> | auto-fill ], <line-names>+)
- // The <track-repeat> variant can represent the repetition of any <track-size>, but is limited to a
- // fixed number of repetitions.
- // The <auto-repeat> variant can repeat automatically to fill a space, but requires definite track
- // sizes so that the number of repetitions can be calculated. It can only appear once in the track
- // list, but the same track list can also contain <fixed-repeat>s.
- // The <name-repeat> variant is for adding line names to subgrids. It can only be used with the
- // subgrid keyword and cannot specify track sizes, only line names.
- // If a repeat() function that is not a <name-repeat> ends up placing two <line-names> adjacent to
- // each other, the name lists are merged. For example, repeat(2, [a] 1fr [b]) is equivalent to [a]
- // 1fr [b a] 1fr [b].
- if (is_auto_fill)
- return GridRepeat(GridTrackSizeList(move(repeat_params)), GridRepeat::Type::AutoFill);
- else if (is_auto_fit)
- return GridRepeat(GridTrackSizeList(move(repeat_params)), GridRepeat::Type::AutoFit);
- else
- return GridRepeat(GridTrackSizeList(move(repeat_params)), repeat_count);
- }
- Optional<CSS::ExplicitGridTrack> Parser::parse_track_sizing_function(ComponentValue const& token)
- {
- if (token.is_function()) {
- auto const& function_token = token.function();
- if (function_token.name.equals_ignoring_ascii_case("repeat"sv)) {
- auto maybe_repeat = parse_repeat(function_token.value);
- if (maybe_repeat.has_value())
- return CSS::ExplicitGridTrack(maybe_repeat.value());
- else
- return {};
- } else if (function_token.name.equals_ignoring_ascii_case("minmax"sv)) {
- auto maybe_min_max_value = parse_min_max(function_token.value);
- if (maybe_min_max_value.has_value())
- return CSS::ExplicitGridTrack(maybe_min_max_value.value());
- else
- return {};
- } else if (function_token.name.equals_ignoring_ascii_case("fit-content"sv)) {
- auto maybe_fit_content_value = parse_fit_content(function_token.value);
- if (maybe_fit_content_value.has_value())
- return CSS::ExplicitGridTrack(maybe_fit_content_value.value());
- return {};
- } else if (auto maybe_calculated = parse_calculated_value(token)) {
- return CSS::ExplicitGridTrack(GridSize(LengthPercentage(maybe_calculated.release_nonnull())));
- }
- return {};
- } else if (token.is_ident("auto"sv)) {
- return CSS::ExplicitGridTrack(GridSize(Length::make_auto()));
- } else if (token.is_block()) {
- return {};
- } else {
- auto grid_size = parse_grid_size(token);
- if (!grid_size.has_value())
- return {};
- return CSS::ExplicitGridTrack(grid_size.value());
- }
- }
- RefPtr<CSSStyleValue> Parser::parse_grid_track_size_list(TokenStream<ComponentValue>& tokens, bool allow_separate_line_name_blocks)
- {
- if (auto none = parse_all_as_single_keyword_value(tokens, Keyword::None))
- return GridTrackSizeListStyleValue::make_none();
- auto transaction = tokens.begin_transaction();
- Vector<Variant<ExplicitGridTrack, GridLineNames>> track_list;
- auto last_object_was_line_names = false;
- while (tokens.has_next_token()) {
- auto const& token = tokens.consume_a_token();
- if (token.is_block()) {
- if (last_object_was_line_names && !allow_separate_line_name_blocks) {
- transaction.commit();
- return GridTrackSizeListStyleValue::make_auto();
- }
- last_object_was_line_names = true;
- Vector<String> line_names;
- if (!token.block().is_square()) {
- transaction.commit();
- return GridTrackSizeListStyleValue::make_auto();
- }
- TokenStream block_tokens { token.block().value };
- block_tokens.discard_whitespace();
- while (block_tokens.has_next_token()) {
- auto const& current_block_token = block_tokens.consume_a_token();
- line_names.append(current_block_token.token().ident().to_string());
- block_tokens.discard_whitespace();
- }
- track_list.append(GridLineNames { move(line_names) });
- } else {
- last_object_was_line_names = false;
- auto track_sizing_function = parse_track_sizing_function(token);
- if (!track_sizing_function.has_value()) {
- transaction.commit();
- return GridTrackSizeListStyleValue::make_auto();
- }
- // FIXME: Handle multiple repeat values (should combine them here, or remove
- // any other ones if the first one is auto-fill, etc.)
- track_list.append(track_sizing_function.value());
- }
- }
- transaction.commit();
- return GridTrackSizeListStyleValue::create(GridTrackSizeList(move(track_list)));
- }
- // https://www.w3.org/TR/css-grid-1/#grid-auto-flow-property
- RefPtr<GridAutoFlowStyleValue> Parser::parse_grid_auto_flow_value(TokenStream<ComponentValue>& tokens)
- {
- // [ row | column ] || dense
- if (!tokens.has_next_token())
- return nullptr;
- auto transaction = tokens.begin_transaction();
- auto parse_axis = [&]() -> Optional<GridAutoFlowStyleValue::Axis> {
- auto transaction = tokens.begin_transaction();
- auto const& token = tokens.consume_a_token();
- if (!token.is(Token::Type::Ident))
- return {};
- auto const& ident = token.token().ident();
- if (ident.equals_ignoring_ascii_case("row"sv)) {
- transaction.commit();
- return GridAutoFlowStyleValue::Axis::Row;
- } else if (ident.equals_ignoring_ascii_case("column"sv)) {
- transaction.commit();
- return GridAutoFlowStyleValue::Axis::Column;
- }
- return {};
- };
- auto parse_dense = [&]() -> Optional<GridAutoFlowStyleValue::Dense> {
- auto transaction = tokens.begin_transaction();
- auto const& token = tokens.consume_a_token();
- if (!token.is(Token::Type::Ident))
- return {};
- auto const& ident = token.token().ident();
- if (ident.equals_ignoring_ascii_case("dense"sv)) {
- transaction.commit();
- return GridAutoFlowStyleValue::Dense::Yes;
- }
- return {};
- };
- Optional<GridAutoFlowStyleValue::Axis> axis;
- Optional<GridAutoFlowStyleValue::Dense> dense;
- if (axis = parse_axis(); axis.has_value()) {
- dense = parse_dense();
- } else if (dense = parse_dense(); dense.has_value()) {
- axis = parse_axis();
- }
- if (tokens.has_next_token())
- return nullptr;
- transaction.commit();
- return GridAutoFlowStyleValue::create(axis.value_or(GridAutoFlowStyleValue::Axis::Row), dense.value_or(GridAutoFlowStyleValue::Dense::No));
- }
- // https://drafts.csswg.org/css-overflow/#propdef-scrollbar-gutter
- RefPtr<CSSStyleValue> Parser::parse_scrollbar_gutter_value(TokenStream<ComponentValue>& tokens)
- {
- // auto | stable && both-edges?
- if (!tokens.has_next_token())
- return nullptr;
- auto transaction = tokens.begin_transaction();
- auto parse_stable = [&]() -> Optional<bool> {
- auto transaction = tokens.begin_transaction();
- auto const& token = tokens.consume_a_token();
- if (!token.is(Token::Type::Ident))
- return {};
- auto const& ident = token.token().ident();
- if (ident.equals_ignoring_ascii_case("auto"sv)) {
- transaction.commit();
- return false;
- } else if (ident.equals_ignoring_ascii_case("stable"sv)) {
- transaction.commit();
- return true;
- }
- return {};
- };
- auto parse_both_edges = [&]() -> Optional<bool> {
- auto transaction = tokens.begin_transaction();
- auto const& token = tokens.consume_a_token();
- if (!token.is(Token::Type::Ident))
- return {};
- auto const& ident = token.token().ident();
- if (ident.equals_ignoring_ascii_case("both-edges"sv)) {
- transaction.commit();
- return true;
- }
- return {};
- };
- Optional<bool> stable;
- Optional<bool> both_edges;
- if (stable = parse_stable(); stable.has_value()) {
- if (stable.value())
- both_edges = parse_both_edges();
- } else if (both_edges = parse_both_edges(); both_edges.has_value()) {
- stable = parse_stable();
- if (!stable.has_value() || !stable.value())
- return nullptr;
- }
- if (tokens.has_next_token())
- return nullptr;
- transaction.commit();
- ScrollbarGutter gutter_value;
- if (both_edges.has_value())
- gutter_value = ScrollbarGutter::BothEdges;
- else if (stable.has_value() && stable.value())
- gutter_value = ScrollbarGutter::Stable;
- else
- gutter_value = ScrollbarGutter::Auto;
- return ScrollbarGutterStyleValue::create(gutter_value);
- }
- RefPtr<CSSStyleValue> Parser::parse_grid_auto_track_sizes(TokenStream<ComponentValue>& tokens)
- {
- // https://www.w3.org/TR/css-grid-2/#auto-tracks
- // <track-size>+
- Vector<Variant<ExplicitGridTrack, GridLineNames>> track_list;
- auto transaction = tokens.begin_transaction();
- while (tokens.has_next_token()) {
- auto const& token = tokens.consume_a_token();
- auto track_sizing_function = parse_track_sizing_function(token);
- if (!track_sizing_function.has_value()) {
- transaction.commit();
- return GridTrackSizeListStyleValue::make_auto();
- }
- // FIXME: Handle multiple repeat values (should combine them here, or remove
- // any other ones if the first one is auto-fill, etc.)
- track_list.append(track_sizing_function.value());
- }
- transaction.commit();
- return GridTrackSizeListStyleValue::create(GridTrackSizeList(move(track_list)));
- }
- RefPtr<GridTrackPlacementStyleValue> Parser::parse_grid_track_placement(TokenStream<ComponentValue>& tokens)
- {
- // FIXME: This shouldn't be needed. Right now, the below code returns a CSSStyleValue even if no tokens are consumed!
- if (!tokens.has_next_token())
- return nullptr;
- // https://www.w3.org/TR/css-grid-2/#line-placement
- // Line-based Placement: the grid-row-start, grid-column-start, grid-row-end, and grid-column-end properties
- // <grid-line> =
- // auto |
- // <custom-ident> |
- // [ <integer> && <custom-ident>? ] |
- // [ span && [ <integer> || <custom-ident> ] ]
- auto is_valid_integer = [](auto& token) -> bool {
- // An <integer> value of zero makes the declaration invalid.
- if (token.is(Token::Type::Number) && token.token().number().is_integer() && token.token().number_value() != 0)
- return true;
- return false;
- };
- auto parse_custom_ident = [this](auto& tokens) {
- // The <custom-ident> additionally excludes the keywords span and auto.
- return parse_custom_ident_value(tokens, { "span"sv, "auto"sv });
- };
- auto transaction = tokens.begin_transaction();
- // FIXME: Handle the single-token case inside the loop instead, so that we can more easily call this from
- // `parse_grid_area_shorthand_value()` using a single TokenStream.
- if (tokens.remaining_token_count() == 1) {
- if (auto custom_ident = parse_custom_ident(tokens)) {
- transaction.commit();
- return GridTrackPlacementStyleValue::create(GridTrackPlacement::make_line({}, custom_ident->custom_ident().to_string()));
- }
- auto const& token = tokens.consume_a_token();
- if (auto maybe_calculated = parse_calculated_value(token); maybe_calculated && maybe_calculated->resolves_to_number()) {
- transaction.commit();
- return GridTrackPlacementStyleValue::create(GridTrackPlacement::make_line(static_cast<int>(maybe_calculated->resolve_integer().value()), {}));
- }
- if (token.is_ident("auto"sv)) {
- transaction.commit();
- return GridTrackPlacementStyleValue::create(GridTrackPlacement::make_auto());
- }
- if (token.is_ident("span"sv)) {
- transaction.commit();
- return GridTrackPlacementStyleValue::create(GridTrackPlacement::make_span(1));
- }
- if (is_valid_integer(token)) {
- transaction.commit();
- return GridTrackPlacementStyleValue::create(GridTrackPlacement::make_line(static_cast<int>(token.token().number_value()), {}));
- }
- return nullptr;
- }
- auto span_value = false;
- auto span_or_position_value = 0;
- String identifier_value;
- while (tokens.has_next_token()) {
- auto const& token = tokens.next_token();
- if (token.is_ident("auto"sv))
- return nullptr;
- if (token.is_ident("span"sv)) {
- if (span_value)
- return nullptr;
- tokens.discard_a_token(); // span
- span_value = true;
- continue;
- }
- if (is_valid_integer(token)) {
- if (span_or_position_value != 0)
- return nullptr;
- span_or_position_value = static_cast<int>(tokens.consume_a_token().token().to_integer());
- continue;
- }
- if (auto custom_ident = parse_custom_ident(tokens)) {
- if (!identifier_value.is_empty())
- return nullptr;
- identifier_value = custom_ident->custom_ident().to_string();
- continue;
- }
- break;
- }
- // Negative integers or zero are invalid.
- if (span_value && span_or_position_value < 1)
- return nullptr;
- // If the <integer> is omitted, it defaults to 1.
- if (span_or_position_value == 0)
- span_or_position_value = 1;
- transaction.commit();
- if (!identifier_value.is_empty())
- return GridTrackPlacementStyleValue::create(GridTrackPlacement::make_line(span_or_position_value, identifier_value));
- return GridTrackPlacementStyleValue::create(GridTrackPlacement::make_span(span_or_position_value));
- }
- RefPtr<CSSStyleValue> Parser::parse_grid_track_placement_shorthand_value(PropertyID property_id, TokenStream<ComponentValue>& tokens)
- {
- auto start_property = (property_id == PropertyID::GridColumn) ? PropertyID::GridColumnStart : PropertyID::GridRowStart;
- auto end_property = (property_id == PropertyID::GridColumn) ? PropertyID::GridColumnEnd : PropertyID::GridRowEnd;
- auto transaction = tokens.begin_transaction();
- NonnullRawPtr<ComponentValue const> current_token = tokens.consume_a_token();
- Vector<ComponentValue> track_start_placement_tokens;
- while (true) {
- if (current_token->is_delim('/'))
- break;
- track_start_placement_tokens.append(current_token);
- if (!tokens.has_next_token())
- break;
- current_token = tokens.consume_a_token();
- }
- Vector<ComponentValue> track_end_placement_tokens;
- if (tokens.has_next_token()) {
- current_token = tokens.consume_a_token();
- while (true) {
- track_end_placement_tokens.append(current_token);
- if (!tokens.has_next_token())
- break;
- current_token = tokens.consume_a_token();
- }
- }
- TokenStream track_start_placement_token_stream { track_start_placement_tokens };
- auto parsed_start_value = parse_grid_track_placement(track_start_placement_token_stream);
- if (parsed_start_value && track_end_placement_tokens.is_empty()) {
- transaction.commit();
- if (parsed_start_value->grid_track_placement().has_identifier()) {
- auto custom_ident = parsed_start_value.release_nonnull();
- return ShorthandStyleValue::create(property_id, { start_property, end_property }, { custom_ident, custom_ident });
- }
- return ShorthandStyleValue::create(property_id,
- { start_property, end_property },
- { parsed_start_value.release_nonnull(), GridTrackPlacementStyleValue::create(GridTrackPlacement::make_auto()) });
- }
- TokenStream track_end_placement_token_stream { track_end_placement_tokens };
- auto parsed_end_value = parse_grid_track_placement(track_end_placement_token_stream);
- if (parsed_start_value && parsed_end_value) {
- transaction.commit();
- return ShorthandStyleValue::create(property_id,
- { start_property, end_property },
- { parsed_start_value.release_nonnull(), parsed_end_value.release_nonnull() });
- }
- return nullptr;
- }
- // https://www.w3.org/TR/css-grid-2/#explicit-grid-shorthand
- // 7.4. Explicit Grid Shorthand: the grid-template property
- RefPtr<CSSStyleValue> Parser::parse_grid_track_size_list_shorthand_value(PropertyID property_id, TokenStream<ComponentValue>& tokens)
- {
- // The grid-template property is a shorthand for setting grid-template-columns, grid-template-rows,
- // and grid-template-areas in a single declaration. It has several distinct syntax forms:
- // none
- // - Sets all three properties to their initial values (none).
- // <'grid-template-rows'> / <'grid-template-columns'>
- // - Sets grid-template-rows and grid-template-columns to the specified values, respectively, and sets grid-template-areas to none.
- // [ <line-names>? <string> <track-size>? <line-names>? ]+ [ / <explicit-track-list> ]?
- // - Sets grid-template-areas to the strings listed.
- // - Sets grid-template-rows to the <track-size>s following each string (filling in auto for any missing sizes),
- // and splicing in the named lines defined before/after each size.
- // - Sets grid-template-columns to the track listing specified after the slash (or none, if not specified).
- auto transaction = tokens.begin_transaction();
- // FIXME: Read the parts in place if possible, instead of constructing separate vectors and streams.
- Vector<ComponentValue> template_rows_tokens;
- Vector<ComponentValue> template_columns_tokens;
- Vector<ComponentValue> template_area_tokens;
- bool found_forward_slash = false;
- while (tokens.has_next_token()) {
- auto& token = tokens.consume_a_token();
- if (token.is_delim('/')) {
- if (found_forward_slash)
- return nullptr;
- found_forward_slash = true;
- continue;
- }
- if (found_forward_slash) {
- template_columns_tokens.append(token);
- continue;
- }
- if (token.is(Token::Type::String))
- template_area_tokens.append(token);
- else
- template_rows_tokens.append(token);
- }
- TokenStream template_area_token_stream { template_area_tokens };
- TokenStream template_rows_token_stream { template_rows_tokens };
- TokenStream template_columns_token_stream { template_columns_tokens };
- auto parsed_template_areas_values = parse_grid_template_areas_value(template_area_token_stream);
- auto parsed_template_rows_values = parse_grid_track_size_list(template_rows_token_stream, true);
- auto parsed_template_columns_values = parse_grid_track_size_list(template_columns_token_stream);
- if (template_area_token_stream.has_next_token()
- || template_rows_token_stream.has_next_token()
- || template_columns_token_stream.has_next_token())
- return nullptr;
- transaction.commit();
- return ShorthandStyleValue::create(property_id,
- { PropertyID::GridTemplateAreas, PropertyID::GridTemplateRows, PropertyID::GridTemplateColumns },
- { parsed_template_areas_values.release_nonnull(), parsed_template_rows_values.release_nonnull(), parsed_template_columns_values.release_nonnull() });
- }
- RefPtr<CSSStyleValue> Parser::parse_grid_area_shorthand_value(TokenStream<ComponentValue>& tokens)
- {
- auto transaction = tokens.begin_transaction();
- auto parse_placement_tokens = [&](Vector<ComponentValue>& placement_tokens, bool check_for_delimiter = true) -> void {
- while (tokens.has_next_token()) {
- auto& current_token = tokens.consume_a_token();
- if (check_for_delimiter && current_token.is_delim('/'))
- break;
- placement_tokens.append(current_token);
- }
- };
- Vector<ComponentValue> row_start_placement_tokens;
- parse_placement_tokens(row_start_placement_tokens);
- Vector<ComponentValue> column_start_placement_tokens;
- if (tokens.has_next_token())
- parse_placement_tokens(column_start_placement_tokens);
- Vector<ComponentValue> row_end_placement_tokens;
- if (tokens.has_next_token())
- parse_placement_tokens(row_end_placement_tokens);
- Vector<ComponentValue> column_end_placement_tokens;
- if (tokens.has_next_token())
- parse_placement_tokens(column_end_placement_tokens, false);
- // https://www.w3.org/TR/css-grid-2/#placement-shorthands
- // The grid-area property is a shorthand for grid-row-start, grid-column-start, grid-row-end and
- // grid-column-end.
- TokenStream row_start_placement_token_stream { row_start_placement_tokens };
- auto row_start_style_value = parse_grid_track_placement(row_start_placement_token_stream);
- if (row_start_placement_token_stream.has_next_token())
- return nullptr;
- TokenStream column_start_placement_token_stream { column_start_placement_tokens };
- auto column_start_style_value = parse_grid_track_placement(column_start_placement_token_stream);
- if (column_start_placement_token_stream.has_next_token())
- return nullptr;
- TokenStream row_end_placement_token_stream { row_end_placement_tokens };
- auto row_end_style_value = parse_grid_track_placement(row_end_placement_token_stream);
- if (row_end_placement_token_stream.has_next_token())
- return nullptr;
- TokenStream column_end_placement_token_stream { column_end_placement_tokens };
- auto column_end_style_value = parse_grid_track_placement(column_end_placement_token_stream);
- if (column_end_placement_token_stream.has_next_token())
- return nullptr;
- // If four <grid-line> values are specified, grid-row-start is set to the first value, grid-column-start
- // is set to the second value, grid-row-end is set to the third value, and grid-column-end is set to the
- // fourth value.
- auto row_start = GridTrackPlacement::make_auto();
- auto column_start = GridTrackPlacement::make_auto();
- auto row_end = GridTrackPlacement::make_auto();
- auto column_end = GridTrackPlacement::make_auto();
- if (row_start_style_value)
- row_start = row_start_style_value.release_nonnull()->as_grid_track_placement().grid_track_placement();
- // When grid-column-start is omitted, if grid-row-start is a <custom-ident>, all four longhands are set to
- // that value. Otherwise, it is set to auto.
- if (column_start_style_value)
- column_start = column_start_style_value.release_nonnull()->as_grid_track_placement().grid_track_placement();
- else
- column_start = row_start;
- // When grid-row-end is omitted, if grid-row-start is a <custom-ident>, grid-row-end is set to that
- // <custom-ident>; otherwise, it is set to auto.
- if (row_end_style_value)
- row_end = row_end_style_value.release_nonnull()->as_grid_track_placement().grid_track_placement();
- else
- row_end = column_start;
- // When grid-column-end is omitted, if grid-column-start is a <custom-ident>, grid-column-end is set to
- // that <custom-ident>; otherwise, it is set to auto.
- if (column_end_style_value)
- column_end = column_end_style_value.release_nonnull()->as_grid_track_placement().grid_track_placement();
- else
- column_end = row_end;
- transaction.commit();
- return ShorthandStyleValue::create(PropertyID::GridArea,
- { PropertyID::GridRowStart, PropertyID::GridColumnStart, PropertyID::GridRowEnd, PropertyID::GridColumnEnd },
- { GridTrackPlacementStyleValue::create(row_start), GridTrackPlacementStyleValue::create(column_start), GridTrackPlacementStyleValue::create(row_end), GridTrackPlacementStyleValue::create(column_end) });
- }
- RefPtr<CSSStyleValue> Parser::parse_grid_shorthand_value(TokenStream<ComponentValue>& tokens)
- {
- // <'grid-template'> |
- // FIXME: <'grid-template-rows'> / [ auto-flow && dense? ] <'grid-auto-columns'>? |
- // FIXME: [ auto-flow && dense? ] <'grid-auto-rows'>? / <'grid-template-columns'>
- return parse_grid_track_size_list_shorthand_value(PropertyID::Grid, tokens);
- }
- // https://www.w3.org/TR/css-grid-1/#grid-template-areas-property
- RefPtr<CSSStyleValue> Parser::parse_grid_template_areas_value(TokenStream<ComponentValue>& tokens)
- {
- // none | <string>+
- Vector<Vector<String>> grid_area_rows;
- if (auto none = parse_all_as_single_keyword_value(tokens, Keyword::None))
- return GridTemplateAreaStyleValue::create(move(grid_area_rows));
- auto transaction = tokens.begin_transaction();
- while (tokens.has_next_token() && tokens.next_token().is(Token::Type::String)) {
- Vector<String> grid_area_columns;
- auto const parts = MUST(tokens.consume_a_token().token().string().to_string().split(' '));
- for (auto& part : parts) {
- grid_area_columns.append(part);
- }
- grid_area_rows.append(move(grid_area_columns));
- }
- transaction.commit();
- return GridTemplateAreaStyleValue::create(grid_area_rows);
- }
- static bool block_contains_var_or_attr(SimpleBlock const& block);
- static bool function_contains_var_or_attr(Function const& function)
- {
- if (function.name.equals_ignoring_ascii_case("var"sv) || function.name.equals_ignoring_ascii_case("attr"sv))
- return true;
- for (auto const& token : function.value) {
- if (token.is_function() && function_contains_var_or_attr(token.function()))
- return true;
- if (token.is_block() && block_contains_var_or_attr(token.block()))
- return true;
- }
- return false;
- }
- bool block_contains_var_or_attr(SimpleBlock const& block)
- {
- for (auto const& token : block.value) {
- if (token.is_function() && function_contains_var_or_attr(token.function()))
- return true;
- if (token.is_block() && block_contains_var_or_attr(token.block()))
- return true;
- }
- return false;
- }
- Parser::ParseErrorOr<NonnullRefPtr<CSSStyleValue>> Parser::parse_css_value(PropertyID property_id, TokenStream<ComponentValue>& unprocessed_tokens, Optional<String> original_source_text)
- {
- m_context.set_current_property_id(property_id);
- Vector<ComponentValue> component_values;
- bool contains_var_or_attr = false;
- bool const property_accepts_custom_ident = property_accepts_type(property_id, ValueType::CustomIdent);
- while (unprocessed_tokens.has_next_token()) {
- auto const& token = unprocessed_tokens.consume_a_token();
- if (token.is(Token::Type::Semicolon)) {
- unprocessed_tokens.reconsume_current_input_token();
- break;
- }
- if (property_id != PropertyID::Custom) {
- if (token.is(Token::Type::Whitespace))
- continue;
- if (!property_accepts_custom_ident && token.is(Token::Type::Ident) && has_ignored_vendor_prefix(token.token().ident()))
- return ParseError::IncludesIgnoredVendorPrefix;
- }
- if (!contains_var_or_attr) {
- if (token.is_function() && function_contains_var_or_attr(token.function()))
- contains_var_or_attr = true;
- else if (token.is_block() && block_contains_var_or_attr(token.block()))
- contains_var_or_attr = true;
- }
- component_values.append(token);
- }
- if (property_id == PropertyID::Custom || contains_var_or_attr)
- return UnresolvedStyleValue::create(move(component_values), contains_var_or_attr, original_source_text);
- if (component_values.is_empty())
- return ParseError::SyntaxError;
- auto tokens = TokenStream { component_values };
- if (component_values.size() == 1) {
- if (auto parsed_value = parse_builtin_value(tokens))
- return parsed_value.release_nonnull();
- }
- // Special-case property handling
- switch (property_id) {
- case PropertyID::AspectRatio:
- if (auto parsed_value = parse_aspect_ratio_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::BackdropFilter:
- case PropertyID::Filter:
- if (auto parsed_value = parse_filter_value_list_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::Background:
- if (auto parsed_value = parse_background_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::BackgroundAttachment:
- case PropertyID::BackgroundClip:
- case PropertyID::BackgroundImage:
- case PropertyID::BackgroundOrigin:
- if (auto parsed_value = parse_simple_comma_separated_value_list(property_id, tokens))
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::BackgroundPosition:
- if (auto parsed_value = parse_comma_separated_value_list(tokens, [this](auto& tokens) { return parse_position_value(tokens, PositionParsingMode::BackgroundPosition); }))
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::BackgroundPositionX:
- case PropertyID::BackgroundPositionY:
- if (auto parsed_value = parse_comma_separated_value_list(tokens, [this, property_id](auto& tokens) { return parse_single_background_position_x_or_y_value(tokens, property_id); }))
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::BackgroundRepeat:
- if (auto parsed_value = parse_comma_separated_value_list(tokens, [this](auto& tokens) { return parse_single_background_repeat_value(tokens); }))
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::BackgroundSize:
- if (auto parsed_value = parse_comma_separated_value_list(tokens, [this](auto& tokens) { return parse_single_background_size_value(tokens); }))
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::Border:
- case PropertyID::BorderBottom:
- case PropertyID::BorderLeft:
- case PropertyID::BorderRight:
- case PropertyID::BorderTop:
- if (auto parsed_value = parse_border_value(property_id, tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::BorderTopLeftRadius:
- case PropertyID::BorderTopRightRadius:
- case PropertyID::BorderBottomRightRadius:
- case PropertyID::BorderBottomLeftRadius:
- if (auto parsed_value = parse_border_radius_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::BorderRadius:
- if (auto parsed_value = parse_border_radius_shorthand_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::BoxShadow:
- if (auto parsed_value = parse_shadow_value(tokens, AllowInsetKeyword::Yes); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::Columns:
- if (auto parsed_value = parse_columns_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::Content:
- if (auto parsed_value = parse_content_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::CounterIncrement:
- if (auto parsed_value = parse_counter_increment_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::CounterReset:
- if (auto parsed_value = parse_counter_reset_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::CounterSet:
- if (auto parsed_value = parse_counter_set_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::Display:
- if (auto parsed_value = parse_display_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::Flex:
- if (auto parsed_value = parse_flex_shorthand_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::FlexFlow:
- if (auto parsed_value = parse_flex_flow_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::Font:
- if (auto parsed_value = parse_font_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::FontFamily:
- if (auto parsed_value = parse_font_family_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::FontFeatureSettings:
- if (auto parsed_value = parse_font_feature_settings_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::FontLanguageOverride:
- if (auto parsed_value = parse_font_language_override_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::FontVariationSettings:
- if (auto parsed_value = parse_font_variation_settings_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::GridArea:
- if (auto parsed_value = parse_grid_area_shorthand_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::GridAutoFlow:
- if (auto parsed_value = parse_grid_auto_flow_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::GridColumn:
- if (auto parsed_value = parse_grid_track_placement_shorthand_value(property_id, tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::GridColumnEnd:
- if (auto parsed_value = parse_grid_track_placement(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::GridColumnStart:
- if (auto parsed_value = parse_grid_track_placement(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::GridRow:
- if (auto parsed_value = parse_grid_track_placement_shorthand_value(property_id, tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::GridRowEnd:
- if (auto parsed_value = parse_grid_track_placement(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::GridRowStart:
- if (auto parsed_value = parse_grid_track_placement(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::Grid:
- if (auto parsed_value = parse_grid_shorthand_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::GridTemplate:
- if (auto parsed_value = parse_grid_track_size_list_shorthand_value(property_id, tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::GridTemplateAreas:
- if (auto parsed_value = parse_grid_template_areas_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::GridTemplateColumns:
- if (auto parsed_value = parse_grid_track_size_list(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::GridTemplateRows:
- if (auto parsed_value = parse_grid_track_size_list(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::GridAutoColumns:
- if (auto parsed_value = parse_grid_auto_track_sizes(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::GridAutoRows:
- if (auto parsed_value = parse_grid_auto_track_sizes(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::ListStyle:
- if (auto parsed_value = parse_list_style_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::MathDepth:
- if (auto parsed_value = parse_math_depth_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::Overflow:
- if (auto parsed_value = parse_overflow_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::PlaceContent:
- if (auto parsed_value = parse_place_content_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::PlaceItems:
- if (auto parsed_value = parse_place_items_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::PlaceSelf:
- if (auto parsed_value = parse_place_self_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::Quotes:
- if (auto parsed_value = parse_quotes_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::Rotate:
- if (auto parsed_value = parse_rotate_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::ScrollbarGutter:
- if (auto parsed_value = parse_scrollbar_gutter_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::StrokeDasharray:
- if (auto parsed_value = parse_stroke_dasharray_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::TextDecoration:
- if (auto parsed_value = parse_text_decoration_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::TextDecorationLine:
- if (auto parsed_value = parse_text_decoration_line_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::TextShadow:
- if (auto parsed_value = parse_shadow_value(tokens, AllowInsetKeyword::No); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::Transform:
- if (auto parsed_value = parse_transform_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::TransformOrigin:
- if (auto parsed_value = parse_transform_origin_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::Transition:
- if (auto parsed_value = parse_transition_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::Translate:
- if (auto parsed_value = parse_translate_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- case PropertyID::Scale:
- if (auto parsed_value = parse_scale_value(tokens); parsed_value && !tokens.has_next_token())
- return parsed_value.release_nonnull();
- return ParseError::SyntaxError;
- default:
- break;
- }
- // If there's only 1 ComponentValue, we can only produce a single CSSStyleValue.
- if (component_values.size() == 1) {
- auto stream = TokenStream { component_values };
- if (auto parsed_value = parse_css_value_for_property(property_id, stream))
- return parsed_value.release_nonnull();
- } else {
- StyleValueVector parsed_values;
- auto stream = TokenStream { component_values };
- while (auto parsed_value = parse_css_value_for_property(property_id, stream)) {
- parsed_values.append(parsed_value.release_nonnull());
- if (!stream.has_next_token())
- break;
- }
- // Some types (such as <ratio>) can be made from multiple ComponentValues, so if we only made 1 CSSStyleValue, return it directly.
- if (parsed_values.size() == 1)
- return *parsed_values.take_first();
- if (!parsed_values.is_empty() && parsed_values.size() <= property_maximum_value_count(property_id))
- return StyleValueList::create(move(parsed_values), StyleValueList::Separator::Space);
- }
- // We have multiple values, but the property claims to accept only a single one, check if it's a shorthand property.
- auto unassigned_properties = longhands_for_shorthand(property_id);
- if (unassigned_properties.is_empty())
- return ParseError::SyntaxError;
- auto stream = TokenStream { component_values };
- HashMap<UnderlyingType<PropertyID>, Vector<ValueComparingNonnullRefPtr<CSSStyleValue const>>> assigned_values;
- while (stream.has_next_token() && !unassigned_properties.is_empty()) {
- auto property_and_value = parse_css_value_for_properties(unassigned_properties, stream);
- if (property_and_value.has_value()) {
- auto property = property_and_value->property;
- auto value = property_and_value->style_value;
- auto& values = assigned_values.ensure(to_underlying(property));
- if (values.size() + 1 == property_maximum_value_count(property)) {
- // We're done with this property, move on to the next one.
- unassigned_properties.remove_first_matching([&](auto& unassigned_property) { return unassigned_property == property; });
- }
- values.append(value.release_nonnull());
- continue;
- }
- // No property matched, so we're done.
- dbgln("No property (from {} properties) matched {}", unassigned_properties.size(), stream.next_token().to_debug_string());
- for (auto id : unassigned_properties)
- dbgln(" {}", string_from_property_id(id));
- break;
- }
- for (auto& property : unassigned_properties)
- assigned_values.ensure(to_underlying(property)).append(property_initial_value(m_context.realm(), property));
- stream.discard_whitespace();
- if (stream.has_next_token())
- return ParseError::SyntaxError;
- Vector<PropertyID> longhand_properties;
- longhand_properties.ensure_capacity(assigned_values.size());
- for (auto& it : assigned_values)
- longhand_properties.unchecked_append(static_cast<PropertyID>(it.key));
- StyleValueVector longhand_values;
- longhand_values.ensure_capacity(assigned_values.size());
- for (auto& it : assigned_values) {
- if (it.value.size() == 1)
- longhand_values.unchecked_append(it.value.take_first());
- else
- longhand_values.unchecked_append(StyleValueList::create(move(it.value), StyleValueList::Separator::Space));
- }
- return { ShorthandStyleValue::create(property_id, move(longhand_properties), move(longhand_values)) };
- }
- RefPtr<CSSStyleValue> Parser::parse_css_value_for_property(PropertyID property_id, TokenStream<ComponentValue>& tokens)
- {
- return parse_css_value_for_properties({ &property_id, 1 }, tokens)
- .map([](auto& it) { return it.style_value; })
- .value_or(nullptr);
- }
- Optional<Parser::PropertyAndValue> Parser::parse_css_value_for_properties(ReadonlySpan<PropertyID> property_ids, TokenStream<ComponentValue>& tokens)
- {
- auto any_property_accepts_type = [](ReadonlySpan<PropertyID> property_ids, ValueType value_type) -> Optional<PropertyID> {
- for (auto const& property : property_ids) {
- if (property_accepts_type(property, value_type))
- return property;
- }
- return {};
- };
- auto any_property_accepts_type_percentage = [](ReadonlySpan<PropertyID> property_ids, ValueType value_type) -> Optional<PropertyID> {
- for (auto const& property : property_ids) {
- if (property_accepts_type(property, value_type) && property_accepts_type(property, ValueType::Percentage))
- return property;
- }
- return {};
- };
- auto any_property_accepts_keyword = [](ReadonlySpan<PropertyID> property_ids, Keyword keyword) -> Optional<PropertyID> {
- for (auto const& property : property_ids) {
- if (property_accepts_keyword(property, keyword))
- return property;
- }
- return {};
- };
- auto& peek_token = tokens.next_token();
- if (auto property = any_property_accepts_type(property_ids, ValueType::EasingFunction); property.has_value()) {
- if (auto maybe_easing_function = parse_easing_value(tokens))
- return PropertyAndValue { *property, maybe_easing_function };
- }
- if (peek_token.is(Token::Type::Ident)) {
- // NOTE: We do not try to parse "CSS-wide keywords" here. https://www.w3.org/TR/css-values-4/#common-keywords
- // These are only valid on their own, and so should be parsed directly in `parse_css_value()`.
- auto keyword = keyword_from_string(peek_token.token().ident());
- if (keyword.has_value()) {
- if (auto property = any_property_accepts_keyword(property_ids, keyword.value()); property.has_value()) {
- tokens.discard_a_token();
- return PropertyAndValue { *property, CSSKeywordValue::create(keyword.value()) };
- }
- }
- // Custom idents
- if (auto property = any_property_accepts_type(property_ids, ValueType::CustomIdent); property.has_value()) {
- if (auto custom_ident = parse_custom_ident_value(tokens, {}))
- return PropertyAndValue { *property, custom_ident };
- }
- }
- if (auto property = any_property_accepts_type(property_ids, ValueType::Color); property.has_value()) {
- if (auto maybe_color = parse_color_value(tokens))
- return PropertyAndValue { *property, maybe_color };
- }
- if (auto property = any_property_accepts_type(property_ids, ValueType::Counter); property.has_value()) {
- if (auto maybe_counter = parse_counter_value(tokens))
- return PropertyAndValue { *property, maybe_counter };
- }
- if (auto property = any_property_accepts_type(property_ids, ValueType::Image); property.has_value()) {
- if (auto maybe_image = parse_image_value(tokens))
- return PropertyAndValue { *property, maybe_image };
- }
- if (auto property = any_property_accepts_type(property_ids, ValueType::Position); property.has_value()) {
- if (auto maybe_position = parse_position_value(tokens))
- return PropertyAndValue { *property, maybe_position };
- }
- if (auto property = any_property_accepts_type(property_ids, ValueType::BackgroundPosition); property.has_value()) {
- if (auto maybe_position = parse_position_value(tokens, PositionParsingMode::BackgroundPosition))
- return PropertyAndValue { *property, maybe_position };
- }
- if (auto property = any_property_accepts_type(property_ids, ValueType::BasicShape); property.has_value()) {
- if (auto maybe_basic_shape = parse_basic_shape_value(tokens))
- return PropertyAndValue { *property, maybe_basic_shape };
- }
- if (auto property = any_property_accepts_type(property_ids, ValueType::Ratio); property.has_value()) {
- if (auto maybe_ratio = parse_ratio_value(tokens))
- return PropertyAndValue { *property, maybe_ratio };
- }
- auto property_accepting_integer = any_property_accepts_type(property_ids, ValueType::Integer);
- auto property_accepting_number = any_property_accepts_type(property_ids, ValueType::Number);
- bool property_accepts_numeric = property_accepting_integer.has_value() || property_accepting_number.has_value();
- if (peek_token.is(Token::Type::Number) && property_accepts_numeric) {
- if (peek_token.token().number().is_integer() && property_accepting_integer.has_value()) {
- auto integer = IntegerStyleValue::create(peek_token.token().number().integer_value());
- if (property_accepts_integer(*property_accepting_integer, integer->as_integer().integer())) {
- tokens.discard_a_token(); // integer
- return PropertyAndValue { *property_accepting_integer, integer };
- }
- }
- if (property_accepting_number.has_value()) {
- auto number = NumberStyleValue::create(peek_token.token().number().value());
- if (property_accepts_number(*property_accepting_number, number->as_number().number())) {
- tokens.discard_a_token(); // number
- return PropertyAndValue { *property_accepting_number, number };
- }
- }
- }
- if (auto property = any_property_accepts_type(property_ids, ValueType::OpenTypeTag); property.has_value()) {
- if (auto maybe_rect = parse_opentype_tag_value(tokens))
- return PropertyAndValue { *property, maybe_rect };
- }
- if (peek_token.is(Token::Type::Percentage)) {
- auto percentage = Percentage(peek_token.token().percentage());
- if (auto property = any_property_accepts_type(property_ids, ValueType::Percentage); property.has_value() && property_accepts_percentage(*property, percentage)) {
- tokens.discard_a_token();
- return PropertyAndValue { *property, PercentageStyleValue::create(percentage) };
- }
- }
- if (auto property = any_property_accepts_type(property_ids, ValueType::Rect); property.has_value()) {
- if (auto maybe_rect = parse_rect_value(tokens))
- return PropertyAndValue { *property, maybe_rect };
- }
- if (peek_token.is(Token::Type::String)) {
- if (auto property = any_property_accepts_type(property_ids, ValueType::String); property.has_value())
- return PropertyAndValue { *property, StringStyleValue::create(tokens.consume_a_token().token().string()) };
- }
- if (auto property = any_property_accepts_type(property_ids, ValueType::Url); property.has_value()) {
- if (auto url = parse_url_value(tokens))
- return PropertyAndValue { *property, url };
- }
- bool property_accepts_dimension = any_property_accepts_type(property_ids, ValueType::Angle).has_value()
- || any_property_accepts_type(property_ids, ValueType::Flex).has_value()
- || any_property_accepts_type(property_ids, ValueType::Frequency).has_value()
- || any_property_accepts_type(property_ids, ValueType::Length).has_value()
- || any_property_accepts_type(property_ids, ValueType::Percentage).has_value()
- || any_property_accepts_type(property_ids, ValueType::Resolution).has_value()
- || any_property_accepts_type(property_ids, ValueType::Time).has_value();
- if (property_accepts_dimension) {
- if (peek_token.is(Token::Type::Number) && m_context.is_parsing_svg_presentation_attribute()) {
- auto transaction = tokens.begin_transaction();
- auto const& token = tokens.consume_a_token();
- // https://svgwg.org/svg2-draft/types.html#presentation-attribute-css-value
- // We need to allow <number> in any place that expects a <length> or <angle>.
- // FIXME: How should these numbers be interpreted? https://github.com/w3c/svgwg/issues/792
- // For now: Convert them to px lengths, or deg angles.
- auto angle = Angle::make_degrees(token.token().number_value());
- if (auto property = any_property_accepts_type(property_ids, ValueType::Angle); property.has_value() && property_accepts_angle(*property, angle)) {
- transaction.commit();
- return PropertyAndValue { *property, AngleStyleValue::create(angle) };
- }
- auto length = Length::make_px(CSSPixels::nearest_value_for(token.token().number_value()));
- if (auto property = any_property_accepts_type(property_ids, ValueType::Length); property.has_value() && property_accepts_length(*property, length)) {
- transaction.commit();
- return PropertyAndValue { *property, LengthStyleValue::create(length) };
- }
- }
- auto transaction = tokens.begin_transaction();
- if (auto maybe_dimension = parse_dimension(peek_token); maybe_dimension.has_value()) {
- tokens.discard_a_token();
- auto dimension = maybe_dimension.release_value();
- if (dimension.is_angle()) {
- auto angle = dimension.angle();
- if (auto property = any_property_accepts_type(property_ids, ValueType::Angle); property.has_value() && property_accepts_angle(*property, angle)) {
- transaction.commit();
- return PropertyAndValue { *property, AngleStyleValue::create(angle) };
- }
- }
- if (dimension.is_flex()) {
- auto flex = dimension.flex();
- if (auto property = any_property_accepts_type(property_ids, ValueType::Flex); property.has_value() && property_accepts_flex(*property, flex)) {
- transaction.commit();
- return PropertyAndValue { *property, FlexStyleValue::create(flex) };
- }
- }
- if (dimension.is_frequency()) {
- auto frequency = dimension.frequency();
- if (auto property = any_property_accepts_type(property_ids, ValueType::Frequency); property.has_value() && property_accepts_frequency(*property, frequency)) {
- transaction.commit();
- return PropertyAndValue { *property, FrequencyStyleValue::create(frequency) };
- }
- }
- if (dimension.is_length()) {
- auto length = dimension.length();
- if (auto property = any_property_accepts_type(property_ids, ValueType::Length); property.has_value() && property_accepts_length(*property, length)) {
- transaction.commit();
- return PropertyAndValue { *property, LengthStyleValue::create(length) };
- }
- }
- if (dimension.is_resolution()) {
- auto resolution = dimension.resolution();
- if (auto property = any_property_accepts_type(property_ids, ValueType::Resolution); property.has_value() && property_accepts_resolution(*property, resolution)) {
- transaction.commit();
- return PropertyAndValue { *property, ResolutionStyleValue::create(resolution) };
- }
- }
- if (dimension.is_time()) {
- auto time = dimension.time();
- if (auto property = any_property_accepts_type(property_ids, ValueType::Time); property.has_value() && property_accepts_time(*property, time)) {
- transaction.commit();
- return PropertyAndValue { *property, TimeStyleValue::create(time) };
- }
- }
- }
- }
- // In order to not end up parsing `calc()` and other math expressions multiple times,
- // we parse it once, and then see if its resolved type matches what the property accepts.
- if (peek_token.is_function() && (property_accepts_dimension || property_accepts_numeric)) {
- if (auto maybe_calculated = parse_calculated_value(peek_token); maybe_calculated) {
- tokens.discard_a_token();
- auto& calculated = *maybe_calculated;
- // This is a bit sensitive to ordering: `<foo>` and `<percentage>` have to be checked before `<foo-percentage>`.
- // FIXME: When parsing SVG presentation attributes, <number> is permitted wherever <length>, <length-percentage>, or <angle> are.
- // The specifics are unclear, so I'm ignoring this for calculated values for now.
- // See https://github.com/w3c/svgwg/issues/792
- if (calculated.resolves_to_percentage()) {
- if (auto property = any_property_accepts_type(property_ids, ValueType::Percentage); property.has_value())
- return PropertyAndValue { *property, calculated };
- } else if (calculated.resolves_to_angle()) {
- if (auto property = any_property_accepts_type(property_ids, ValueType::Angle); property.has_value())
- return PropertyAndValue { *property, calculated };
- } else if (calculated.resolves_to_angle_percentage()) {
- if (auto property = any_property_accepts_type_percentage(property_ids, ValueType::Angle); property.has_value())
- return PropertyAndValue { *property, calculated };
- } else if (calculated.resolves_to_flex()) {
- if (auto property = any_property_accepts_type(property_ids, ValueType::Flex); property.has_value())
- return PropertyAndValue { *property, calculated };
- } else if (calculated.resolves_to_frequency()) {
- if (auto property = any_property_accepts_type(property_ids, ValueType::Frequency); property.has_value())
- return PropertyAndValue { *property, calculated };
- } else if (calculated.resolves_to_frequency_percentage()) {
- if (auto property = any_property_accepts_type_percentage(property_ids, ValueType::Frequency); property.has_value())
- return PropertyAndValue { *property, calculated };
- } else if (calculated.resolves_to_number()) {
- if (property_accepts_numeric) {
- auto property_or_resolved = property_accepting_integer.value_or_lazy_evaluated([property_accepting_number]() { return property_accepting_number.value(); });
- return PropertyAndValue { property_or_resolved, calculated };
- }
- } else if (calculated.resolves_to_number_percentage()) {
- if (auto property = any_property_accepts_type_percentage(property_ids, ValueType::Number); property.has_value())
- return PropertyAndValue { *property, calculated };
- } else if (calculated.resolves_to_length()) {
- if (auto property = any_property_accepts_type(property_ids, ValueType::Length); property.has_value())
- return PropertyAndValue { *property, calculated };
- } else if (calculated.resolves_to_length_percentage()) {
- if (auto property = any_property_accepts_type_percentage(property_ids, ValueType::Length); property.has_value())
- return PropertyAndValue { *property, calculated };
- } else if (calculated.resolves_to_time()) {
- if (auto property = any_property_accepts_type(property_ids, ValueType::Time); property.has_value())
- return PropertyAndValue { *property, calculated };
- } else if (calculated.resolves_to_time_percentage()) {
- if (auto property = any_property_accepts_type_percentage(property_ids, ValueType::Time); property.has_value())
- return PropertyAndValue { *property, calculated };
- }
- }
- }
- if (auto property = any_property_accepts_type(property_ids, ValueType::Paint); property.has_value()) {
- if (auto value = parse_paint_value(tokens))
- return PropertyAndValue { *property, value.release_nonnull() };
- }
- return OptionalNone {};
- }
- class UnparsedCalculationNode final : public CalculationNode {
- public:
- static NonnullOwnPtr<UnparsedCalculationNode> create(ComponentValue component_value)
- {
- return adopt_own(*new (nothrow) UnparsedCalculationNode(move(component_value)));
- }
- virtual ~UnparsedCalculationNode() = default;
- ComponentValue& component_value() { return m_component_value; }
- virtual String to_string() const override { VERIFY_NOT_REACHED(); }
- virtual Optional<CSSMathValue::ResolvedType> resolved_type() const override { VERIFY_NOT_REACHED(); }
- virtual Optional<CSSNumericType> determine_type(Web::CSS::PropertyID) const override { VERIFY_NOT_REACHED(); }
- virtual bool contains_percentage() const override { VERIFY_NOT_REACHED(); }
- virtual CSSMathValue::CalculationResult resolve(Optional<Length::ResolutionContext const&>, CSSMathValue::PercentageBasis const&) const override { VERIFY_NOT_REACHED(); }
- virtual void for_each_child_node(AK::Function<void(NonnullOwnPtr<CalculationNode>&)> const&) override { }
- virtual void dump(StringBuilder& builder, int indent) const override
- {
- builder.appendff("{: >{}}UNPARSED({})\n", "", indent, m_component_value.to_debug_string());
- }
- virtual bool equals(CalculationNode const&) const override { return false; }
- private:
- UnparsedCalculationNode(ComponentValue component_value)
- : CalculationNode(Type::Unparsed)
- , m_component_value(move(component_value))
- {
- }
- ComponentValue m_component_value;
- };
- // https://html.spec.whatwg.org/multipage/images.html#parsing-a-sizes-attribute
- LengthOrCalculated Parser::Parser::parse_as_sizes_attribute(DOM::Element const& element, HTML::HTMLImageElement const* img)
- {
- // When asked to parse a sizes attribute from an element element, with an img element or null img:
- // AD-HOC: If element has no sizes attribute, this algorithm always logs a parse error and then returns 100vw.
- // The attribute is optional, so avoid spamming the debug log with false positives by just returning early.
- if (!element.has_attribute(HTML::AttributeNames::sizes))
- return Length(100, Length::Type::Vw);
- // 1. Let unparsed sizes list be the result of parsing a comma-separated list of component values
- // from the value of element's sizes attribute (or the empty string, if the attribute is absent).
- // NOTE: The sizes attribute has already been tokenized into m_token_stream by this point.
- auto unparsed_sizes_list = parse_a_comma_separated_list_of_component_values(m_token_stream);
- // 2. Let size be null.
- Optional<LengthOrCalculated> size;
- auto size_is_auto = [&size]() {
- return !size->is_calculated() && size->value().is_auto();
- };
- auto remove_all_consecutive_whitespace_tokens_from_the_end_of = [](auto& tokens) {
- while (!tokens.is_empty() && tokens.last().is_token() && tokens.last().token().is(Token::Type::Whitespace))
- tokens.take_last();
- };
- // 3. For each unparsed size in unparsed sizes list:
- for (auto i = 0u; i < unparsed_sizes_list.size(); i++) {
- auto& unparsed_size = unparsed_sizes_list[i];
- // 1. Remove all consecutive <whitespace-token>s from the end of unparsed size.
- // If unparsed size is now empty, that is a parse error; continue.
- remove_all_consecutive_whitespace_tokens_from_the_end_of(unparsed_size);
- if (unparsed_size.is_empty()) {
- log_parse_error();
- dbgln_if(CSS_PARSER_DEBUG, "-> Failed in step 3.1; all whitespace");
- continue;
- }
- // 2. If the last component value in unparsed size is a valid non-negative <source-size-value>,
- // then set size to its value and remove the component value from unparsed size.
- // Any CSS function other than the math functions is invalid.
- // Otherwise, there is a parse error; continue.
- auto last_value_stream = TokenStream<ComponentValue>::of_single_token(unparsed_size.last());
- if (auto source_size_value = parse_source_size_value(last_value_stream); source_size_value.has_value()) {
- size = source_size_value.value();
- unparsed_size.take_last();
- } else {
- log_parse_error();
- dbgln_if(CSS_PARSER_DEBUG, "-> Failed in step 3.2; couldn't parse {} as a <source-size-value>", unparsed_size.last().to_debug_string());
- continue;
- }
- // 3. If size is auto, and img is not null, and img is being rendered, and img allows auto-sizes,
- // then set size to the concrete object size width of img, in CSS pixels.
- // FIXME: "img is being rendered" - we just see if it has a bitmap for now
- if (size_is_auto() && img && img->immutable_bitmap() && img->allows_auto_sizes()) {
- // FIXME: The spec doesn't seem to tell us how to determine the concrete size of an <img>, so use the default sizing algorithm.
- // Should this use some of the methods from FormattingContext?
- auto concrete_size = run_default_sizing_algorithm(
- img->width(), img->height(),
- img->natural_width(), img->natural_height(), img->intrinsic_aspect_ratio(),
- // NOTE: https://html.spec.whatwg.org/multipage/rendering.html#img-contain-size
- CSSPixelSize { 300, 150 });
- size = Length::make_px(concrete_size.width());
- }
- // 4. Remove all consecutive <whitespace-token>s from the end of unparsed size.
- // If unparsed size is now empty:
- remove_all_consecutive_whitespace_tokens_from_the_end_of(unparsed_size);
- if (unparsed_size.is_empty()) {
- // 1. If this was not the last item in unparsed sizes list, that is a parse error.
- if (i != unparsed_sizes_list.size() - 1) {
- log_parse_error();
- dbgln_if(CSS_PARSER_DEBUG, "-> Failed in step 3.4.1; is unparsed size #{}, count {}", i, unparsed_sizes_list.size());
- }
- // 2. If size is not auto, then return size. Otherwise, continue.
- if (!size_is_auto())
- return size.release_value();
- continue;
- }
- // 5. Parse the remaining component values in unparsed size as a <media-condition>.
- // If it does not parse correctly, or it does parse correctly but the <media-condition> evaluates to false, continue.
- TokenStream<ComponentValue> token_stream { unparsed_size };
- auto media_condition = parse_media_condition(token_stream, MediaCondition::AllowOr::Yes);
- auto const* context_window = m_context.window();
- if (!media_condition || (context_window && media_condition->evaluate(*context_window) == MatchResult::False)) {
- continue;
- }
- // 5. If size is not auto, then return size. Otherwise, continue.
- if (!size_is_auto())
- return size.value();
- }
- // 4. Return 100vw.
- return Length(100, Length::Type::Vw);
- }
- // https://www.w3.org/TR/css-values-4/#parse-a-calculation
- OwnPtr<CalculationNode> Parser::parse_a_calculation(Vector<ComponentValue> const& original_values)
- {
- // 1. Discard any <whitespace-token>s from values.
- // 2. An item in values is an “operator” if it’s a <delim-token> with the value "+", "-", "*", or "/". Otherwise, it’s a “value”.
- struct Operator {
- char delim;
- };
- using Value = Variant<NonnullOwnPtr<CalculationNode>, Operator>;
- Vector<Value> values;
- for (auto const& value : original_values) {
- if (value.is(Token::Type::Whitespace))
- continue;
- if (value.is(Token::Type::Delim)) {
- if (first_is_one_of(value.token().delim(), static_cast<u32>('+'), static_cast<u32>('-'), static_cast<u32>('*'), static_cast<u32>('/'))) {
- // NOTE: Sequential operators are invalid syntax.
- if (!values.is_empty() && values.last().has<Operator>())
- return nullptr;
- values.append(Operator { static_cast<char>(value.token().delim()) });
- continue;
- }
- }
- if (value.is(Token::Type::Ident)) {
- auto maybe_constant = CalculationNode::constant_type_from_string(value.token().ident());
- if (maybe_constant.has_value()) {
- values.append({ ConstantCalculationNode::create(maybe_constant.value()) });
- continue;
- }
- }
- if (value.is(Token::Type::Number)) {
- values.append({ NumericCalculationNode::create(value.token().number()) });
- continue;
- }
- if (auto dimension = parse_dimension(value); dimension.has_value()) {
- if (dimension->is_angle())
- values.append({ NumericCalculationNode::create(dimension->angle()) });
- else if (dimension->is_frequency())
- values.append({ NumericCalculationNode::create(dimension->frequency()) });
- else if (dimension->is_length())
- values.append({ NumericCalculationNode::create(dimension->length()) });
- else if (dimension->is_percentage())
- values.append({ NumericCalculationNode::create(dimension->percentage()) });
- else if (dimension->is_resolution())
- values.append({ NumericCalculationNode::create(dimension->resolution()) });
- else if (dimension->is_time())
- values.append({ NumericCalculationNode::create(dimension->time()) });
- else if (dimension->is_flex()) {
- // https://www.w3.org/TR/css3-grid-layout/#fr-unit
- // NOTE: <flex> values are not <length>s (nor are they compatible with <length>s, like some <percentage> values),
- // so they cannot be represented in or combined with other unit types in calc() expressions.
- return nullptr;
- } else {
- VERIFY_NOT_REACHED();
- }
- continue;
- }
- values.append({ UnparsedCalculationNode::create(value) });
- }
- // If we have no values, the syntax is invalid.
- if (values.is_empty())
- return nullptr;
- // NOTE: If the first or last value is an operator, the syntax is invalid.
- if (values.first().has<Operator>() || values.last().has<Operator>())
- return nullptr;
- // 3. Collect children into Product and Invert nodes.
- // For every consecutive run of value items in values separated by "*" or "/" operators:
- while (true) {
- Optional<size_t> first_product_operator = values.find_first_index_if([](auto const& item) {
- return item.template has<Operator>()
- && first_is_one_of(item.template get<Operator>().delim, '*', '/');
- });
- if (!first_product_operator.has_value())
- break;
- auto start_of_run = first_product_operator.value() - 1;
- auto end_of_run = first_product_operator.value() + 1;
- for (auto i = start_of_run + 1; i < values.size(); i += 2) {
- auto& item = values[i];
- if (!item.has<Operator>()) {
- end_of_run = i - 1;
- break;
- }
- auto delim = item.get<Operator>().delim;
- if (!first_is_one_of(delim, '*', '/')) {
- end_of_run = i - 1;
- break;
- }
- }
- // 1. For each "/" operator in the run, replace its right-hand value item rhs with an Invert node containing rhs as its child.
- Vector<NonnullOwnPtr<CalculationNode>> run_values;
- run_values.append(move(values[start_of_run].get<NonnullOwnPtr<CalculationNode>>()));
- for (auto i = start_of_run + 1; i <= end_of_run; i += 2) {
- auto& operator_ = values[i].get<Operator>().delim;
- auto& rhs = values[i + 1];
- if (operator_ == '/') {
- run_values.append(InvertCalculationNode::create(move(rhs.get<NonnullOwnPtr<CalculationNode>>())));
- continue;
- }
- VERIFY(operator_ == '*');
- run_values.append(move(rhs.get<NonnullOwnPtr<CalculationNode>>()));
- }
- // 2. Replace the entire run with a Product node containing the value items of the run as its children.
- auto product_node = ProductCalculationNode::create(move(run_values));
- values.remove(start_of_run, end_of_run - start_of_run + 1);
- values.insert(start_of_run, { move(product_node) });
- }
- // 4. Collect children into Sum and Negate nodes.
- Optional<NonnullOwnPtr<CalculationNode>> single_value;
- {
- // 1. For each "-" operator item in values, replace its right-hand value item rhs with a Negate node containing rhs as its child.
- for (auto i = 0u; i < values.size(); ++i) {
- auto& maybe_minus_operator = values[i];
- if (!maybe_minus_operator.has<Operator>() || maybe_minus_operator.get<Operator>().delim != '-')
- continue;
- auto rhs_index = ++i;
- auto& rhs = values[rhs_index];
- NonnullOwnPtr<CalculationNode> negate_node = NegateCalculationNode::create(move(rhs.get<NonnullOwnPtr<CalculationNode>>()));
- values.remove(rhs_index);
- values.insert(rhs_index, move(negate_node));
- }
- // 2. If values has only one item, and it is a Product node or a parenthesized simple block, replace values with that item.
- if (values.size() == 1) {
- values.first().visit(
- [&](ComponentValue& component_value) {
- if (component_value.is_block() && component_value.block().is_paren())
- single_value = UnparsedCalculationNode::create(move(component_value));
- },
- [&](NonnullOwnPtr<CalculationNode>& node) {
- if (node->type() == CalculationNode::Type::Product)
- single_value = move(node);
- },
- [](auto&) {});
- }
- // Otherwise, replace values with a Sum node containing the value items of values as its children.
- if (!single_value.has_value()) {
- values.remove_all_matching([](Value& value) { return value.has<Operator>(); });
- Vector<NonnullOwnPtr<CalculationNode>> value_items;
- value_items.ensure_capacity(values.size());
- for (auto& value : values) {
- if (value.has<Operator>())
- continue;
- value_items.unchecked_append(move(value.get<NonnullOwnPtr<CalculationNode>>()));
- }
- single_value = SumCalculationNode::create(move(value_items));
- }
- }
- // 5. At this point values is a tree of Sum, Product, Negate, and Invert nodes, with other types of values at the leaf nodes. Process the leaf nodes.
- // For every leaf node leaf in values:
- bool parsing_failed_for_child_node = false;
- single_value.value()->for_each_child_node([&](NonnullOwnPtr<CalculationNode>& node) {
- if (node->type() != CalculationNode::Type::Unparsed)
- return;
- auto& unparsed_node = static_cast<UnparsedCalculationNode&>(*node);
- auto& component_value = unparsed_node.component_value();
- // 1. If leaf is a parenthesized simple block, replace leaf with the result of parsing a calculation from leaf’s contents.
- if (component_value.is_block() && component_value.block().is_paren()) {
- auto leaf_calculation = parse_a_calculation(component_value.block().value);
- if (!leaf_calculation) {
- parsing_failed_for_child_node = true;
- return;
- }
- node = leaf_calculation.release_nonnull();
- return;
- }
- // 2. If leaf is a math function, replace leaf with the internal representation of that math function.
- // NOTE: All function tokens at this point should be math functions.
- else if (component_value.is_function()) {
- auto& function = component_value.function();
- auto leaf_calculation = parse_a_calc_function_node(function);
- if (!leaf_calculation) {
- parsing_failed_for_child_node = true;
- return;
- }
- node = leaf_calculation.release_nonnull();
- return;
- }
- // NOTE: If we get here, then we have an UnparsedCalculationNode that didn't get replaced with something else.
- // So, the calc() is invalid.
- dbgln_if(CSS_PARSER_DEBUG, "Leftover UnparsedCalculationNode in calc tree! That probably means the syntax is invalid, but maybe we just didn't implement `{}` yet.", component_value.to_debug_string());
- parsing_failed_for_child_node = true;
- return;
- });
- if (parsing_failed_for_child_node)
- return nullptr;
- // FIXME: 6. Return the result of simplifying a calculation tree from values.
- return single_value.release_value();
- }
- bool Parser::has_ignored_vendor_prefix(StringView string)
- {
- if (!string.starts_with('-'))
- return false;
- if (string.starts_with("--"sv))
- return false;
- if (string.starts_with("-libweb-"sv))
- return false;
- return true;
- }
- NonnullRefPtr<CSSStyleValue> Parser::resolve_unresolved_style_value(ParsingContext const& context, DOM::Element& element, Optional<Selector::PseudoElement::Type> pseudo_element, PropertyID property_id, UnresolvedStyleValue const& unresolved)
- {
- // Unresolved always contains a var() or attr(), unless it is a custom property's value, in which case we shouldn't be trying
- // to produce a different CSSStyleValue from it.
- VERIFY(unresolved.contains_var_or_attr());
- // If the value is invalid, we fall back to `unset`: https://www.w3.org/TR/css-variables-1/#invalid-at-computed-value-time
- auto parser = Parser::create(context, ""sv);
- return parser.resolve_unresolved_style_value(element, pseudo_element, property_id, unresolved);
- }
- class PropertyDependencyNode : public RefCounted<PropertyDependencyNode> {
- public:
- static NonnullRefPtr<PropertyDependencyNode> create(FlyString name)
- {
- return adopt_ref(*new PropertyDependencyNode(move(name)));
- }
- void add_child(NonnullRefPtr<PropertyDependencyNode> new_child)
- {
- for (auto const& child : m_children) {
- if (child->m_name == new_child->m_name)
- return;
- }
- // We detect self-reference already.
- VERIFY(new_child->m_name != m_name);
- m_children.append(move(new_child));
- }
- bool has_cycles()
- {
- if (m_marked)
- return true;
- TemporaryChange change { m_marked, true };
- for (auto& child : m_children) {
- if (child->has_cycles())
- return true;
- }
- return false;
- }
- private:
- explicit PropertyDependencyNode(FlyString name)
- : m_name(move(name))
- {
- }
- FlyString m_name;
- Vector<NonnullRefPtr<PropertyDependencyNode>> m_children;
- bool m_marked { false };
- };
- NonnullRefPtr<CSSStyleValue> Parser::resolve_unresolved_style_value(DOM::Element& element, Optional<Selector::PseudoElement::Type> pseudo_element, PropertyID property_id, UnresolvedStyleValue const& unresolved)
- {
- TokenStream unresolved_values_without_variables_expanded { unresolved.values() };
- Vector<ComponentValue> values_with_variables_expanded;
- HashMap<FlyString, NonnullRefPtr<PropertyDependencyNode>> dependencies;
- if (!expand_variables(element, pseudo_element, string_from_property_id(property_id), dependencies, unresolved_values_without_variables_expanded, values_with_variables_expanded))
- return CSSKeywordValue::create(Keyword::Unset);
- TokenStream unresolved_values_with_variables_expanded { values_with_variables_expanded };
- Vector<ComponentValue> expanded_values;
- if (!expand_unresolved_values(element, string_from_property_id(property_id), unresolved_values_with_variables_expanded, expanded_values))
- return CSSKeywordValue::create(Keyword::Unset);
- auto expanded_value_tokens = TokenStream { expanded_values };
- if (auto parsed_value = parse_css_value(property_id, expanded_value_tokens); !parsed_value.is_error())
- return parsed_value.release_value();
- return CSSKeywordValue::create(Keyword::Unset);
- }
- static RefPtr<CSSStyleValue const> get_custom_property(DOM::Element const& element, Optional<CSS::Selector::PseudoElement::Type> pseudo_element, FlyString const& custom_property_name)
- {
- if (pseudo_element.has_value()) {
- if (auto it = element.custom_properties(pseudo_element).find(custom_property_name); it != element.custom_properties(pseudo_element).end())
- return it->value.value;
- }
- for (auto const* current_element = &element; current_element; current_element = current_element->parent_or_shadow_host_element()) {
- if (auto it = current_element->custom_properties({}).find(custom_property_name); it != current_element->custom_properties({}).end())
- return it->value.value;
- }
- return nullptr;
- }
- bool Parser::expand_variables(DOM::Element& element, Optional<Selector::PseudoElement::Type> pseudo_element, FlyString const& property_name, HashMap<FlyString, NonnullRefPtr<PropertyDependencyNode>>& dependencies, TokenStream<ComponentValue>& source, Vector<ComponentValue>& dest)
- {
- // Arbitrary large value chosen to avoid the billion-laughs attack.
- // https://www.w3.org/TR/css-variables-1/#long-variables
- size_t const MAX_VALUE_COUNT = 16384;
- if (source.remaining_token_count() + dest.size() > MAX_VALUE_COUNT) {
- dbgln("Stopped expanding CSS variables: maximum length reached.");
- return false;
- }
- auto get_dependency_node = [&](FlyString const& name) -> NonnullRefPtr<PropertyDependencyNode> {
- if (auto existing = dependencies.get(name); existing.has_value())
- return *existing.value();
- auto new_node = PropertyDependencyNode::create(name);
- dependencies.set(name, new_node);
- return new_node;
- };
- while (source.has_next_token()) {
- auto const& value = source.consume_a_token();
- if (value.is_block()) {
- auto const& source_block = value.block();
- Vector<ComponentValue> block_values;
- TokenStream source_block_contents { source_block.value };
- if (!expand_variables(element, pseudo_element, property_name, dependencies, source_block_contents, block_values))
- return false;
- dest.empend(SimpleBlock { source_block.token, move(block_values) });
- continue;
- }
- if (!value.is_function()) {
- dest.empend(value.token());
- continue;
- }
- if (!value.function().name.equals_ignoring_ascii_case("var"sv)) {
- auto const& source_function = value.function();
- Vector<ComponentValue> function_values;
- TokenStream source_function_contents { source_function.value };
- if (!expand_variables(element, pseudo_element, property_name, dependencies, source_function_contents, function_values))
- return false;
- dest.empend(Function { source_function.name, move(function_values) });
- continue;
- }
- TokenStream var_contents { value.function().value };
- var_contents.discard_whitespace();
- if (!var_contents.has_next_token())
- return false;
- auto const& custom_property_name_token = var_contents.consume_a_token();
- if (!custom_property_name_token.is(Token::Type::Ident))
- return false;
- auto custom_property_name = custom_property_name_token.token().ident();
- if (!custom_property_name.bytes_as_string_view().starts_with("--"sv))
- return false;
- // Detect dependency cycles. https://www.w3.org/TR/css-variables-1/#cycles
- // We do not do this by the spec, since we are not keeping a graph of var dependencies around,
- // but rebuilding it every time.
- if (custom_property_name == property_name)
- return false;
- auto parent = get_dependency_node(property_name);
- auto child = get_dependency_node(custom_property_name);
- parent->add_child(child);
- if (parent->has_cycles())
- return false;
- if (auto custom_property_value = get_custom_property(element, pseudo_element, custom_property_name)) {
- VERIFY(custom_property_value->is_unresolved());
- TokenStream custom_property_tokens { custom_property_value->as_unresolved().values() };
- if (!expand_variables(element, pseudo_element, custom_property_name, dependencies, custom_property_tokens, dest))
- return false;
- continue;
- }
- // Use the provided fallback value, if any.
- var_contents.discard_whitespace();
- if (var_contents.has_next_token()) {
- auto const& comma_token = var_contents.consume_a_token();
- if (!comma_token.is(Token::Type::Comma))
- return false;
- var_contents.discard_whitespace();
- if (!expand_variables(element, pseudo_element, property_name, dependencies, var_contents, dest))
- return false;
- }
- }
- return true;
- }
- bool Parser::expand_unresolved_values(DOM::Element& element, FlyString const& property_name, TokenStream<ComponentValue>& source, Vector<ComponentValue>& dest)
- {
- auto property = property_id_from_string(property_name);
- while (source.has_next_token()) {
- auto const& value = source.consume_a_token();
- if (value.is_function()) {
- if (value.function().name.equals_ignoring_ascii_case("attr"sv)) {
- if (!substitute_attr_function(element, property_name, value.function(), dest))
- return false;
- continue;
- }
- if (property.has_value()) {
- if (auto maybe_calc_value = parse_calculated_value(value); maybe_calc_value && maybe_calc_value->is_math()) {
- // FIXME: Run the actual simplification algorithm
- auto& calc_value = maybe_calc_value->as_math();
- if (property_accepts_type(*property, ValueType::Angle) && calc_value.resolves_to_angle()) {
- auto resolved_value = calc_value.resolve_angle();
- dest.empend(Token::create_dimension(resolved_value->to_degrees(), "deg"_fly_string));
- continue;
- }
- if (property_accepts_type(*property, ValueType::Frequency) && calc_value.resolves_to_frequency()) {
- auto resolved_value = calc_value.resolve_frequency();
- dest.empend(Token::create_dimension(resolved_value->to_hertz(), "hz"_fly_string));
- continue;
- }
- if (property_accepts_type(*property, ValueType::Length) && calc_value.resolves_to_length()) {
- // FIXME: In order to resolve lengths, we need to know the font metrics in case a font-relative unit
- // is used. So... we can't do that until style is computed?
- // This might be easier once we have calc-simplification implemented.
- }
- if (property_accepts_type(*property, ValueType::Percentage) && calc_value.resolves_to_percentage()) {
- auto resolved_value = calc_value.resolve_percentage();
- dest.empend(Token::create_percentage(resolved_value.value().value()));
- continue;
- }
- if (property_accepts_type(*property, ValueType::Time) && calc_value.resolves_to_time()) {
- auto resolved_value = calc_value.resolve_time();
- dest.empend(Token::create_dimension(resolved_value->to_seconds(), "s"_fly_string));
- continue;
- }
- if (property_accepts_type(*property, ValueType::Number) && calc_value.resolves_to_number()) {
- auto resolved_value = calc_value.resolve_number();
- dest.empend(Token::create_number(resolved_value.value(), Number::Type::Number));
- continue;
- }
- if (property_accepts_type(*property, ValueType::Integer) && calc_value.resolves_to_number()) {
- auto resolved_value = calc_value.resolve_integer();
- dest.empend(Token::create_number(resolved_value.value(), Number::Type::Integer));
- continue;
- }
- }
- }
- auto const& source_function = value.function();
- Vector<ComponentValue> function_values;
- TokenStream source_function_contents { source_function.value };
- if (!expand_unresolved_values(element, property_name, source_function_contents, function_values))
- return false;
- dest.empend(Function { source_function.name, move(function_values) });
- continue;
- }
- if (value.is_block()) {
- auto const& source_block = value.block();
- TokenStream source_block_values { source_block.value };
- Vector<ComponentValue> block_values;
- if (!expand_unresolved_values(element, property_name, source_block_values, block_values))
- return false;
- dest.empend(SimpleBlock { source_block.token, move(block_values) });
- continue;
- }
- dest.empend(value.token());
- }
- return true;
- }
- // https://drafts.csswg.org/css-values-5/#attr-substitution
- bool Parser::substitute_attr_function(DOM::Element& element, FlyString const& property_name, Function const& attr_function, Vector<ComponentValue>& dest)
- {
- // First, parse the arguments to attr():
- // attr() = attr( <q-name> <attr-type>? , <declaration-value>?)
- // <attr-type> = string | url | ident | color | number | percentage | length | angle | time | frequency | flex | <dimension-unit>
- TokenStream attr_contents { attr_function.value };
- attr_contents.discard_whitespace();
- if (!attr_contents.has_next_token())
- return false;
- // - Attribute name
- // FIXME: Support optional attribute namespace
- if (!attr_contents.next_token().is(Token::Type::Ident))
- return false;
- auto attribute_name = attr_contents.consume_a_token().token().ident();
- attr_contents.discard_whitespace();
- // - Attribute type (optional)
- auto attribute_type = "string"_fly_string;
- if (attr_contents.next_token().is(Token::Type::Ident)) {
- attribute_type = attr_contents.consume_a_token().token().ident();
- attr_contents.discard_whitespace();
- }
- // - Comma, then fallback values (optional)
- bool has_fallback_values = false;
- if (attr_contents.has_next_token()) {
- if (!attr_contents.next_token().is(Token::Type::Comma))
- return false;
- (void)attr_contents.consume_a_token(); // Comma
- has_fallback_values = true;
- }
- // Then, run the substitution algorithm:
- // 1. If the attr() function has a substitution value, replace the attr() function by the substitution value.
- // https://drafts.csswg.org/css-values-5/#attr-types
- if (element.has_attribute(attribute_name)) {
- auto attribute_value = element.get_attribute_value(attribute_name);
- if (attribute_type.equals_ignoring_ascii_case("angle"_fly_string)) {
- // Parse a component value from the attribute’s value.
- auto component_value = Parser::Parser::create(m_context, attribute_value).parse_as_component_value();
- // If the result is a <dimension-token> whose unit matches the given type, the result is the substitution value.
- // Otherwise, there is no substitution value.
- if (component_value.has_value() && component_value->is(Token::Type::Dimension)) {
- if (Angle::unit_from_name(component_value->token().dimension_unit()).has_value()) {
- dest.append(component_value.release_value());
- return true;
- }
- }
- } else if (attribute_type.equals_ignoring_ascii_case("color"_fly_string)) {
- // Parse a component value from the attribute’s value.
- // If the result is a <hex-color> or a named color ident, the substitution value is that result as a <color>.
- // Otherwise there is no substitution value.
- auto component_value = Parser::Parser::create(m_context, attribute_value).parse_as_component_value();
- if (component_value.has_value()) {
- if ((component_value->is(Token::Type::Hash)
- && Color::from_string(MUST(String::formatted("#{}", component_value->token().hash_value()))).has_value())
- || (component_value->is(Token::Type::Ident)
- && Color::from_string(component_value->token().ident()).has_value())) {
- dest.append(component_value.release_value());
- return true;
- }
- }
- } else if (attribute_type.equals_ignoring_ascii_case("flex"_fly_string)) {
- // Parse a component value from the attribute’s value.
- auto component_value = Parser::Parser::create(m_context, attribute_value).parse_as_component_value();
- // If the result is a <dimension-token> whose unit matches the given type, the result is the substitution value.
- // Otherwise, there is no substitution value.
- if (component_value.has_value() && component_value->is(Token::Type::Dimension)) {
- if (Flex::unit_from_name(component_value->token().dimension_unit()).has_value()) {
- dest.append(component_value.release_value());
- return true;
- }
- }
- } else if (attribute_type.equals_ignoring_ascii_case("frequency"_fly_string)) {
- // Parse a component value from the attribute’s value.
- auto component_value = Parser::Parser::create(m_context, attribute_value).parse_as_component_value();
- // If the result is a <dimension-token> whose unit matches the given type, the result is the substitution value.
- // Otherwise, there is no substitution value.
- if (component_value.has_value() && component_value->is(Token::Type::Dimension)) {
- if (Frequency::unit_from_name(component_value->token().dimension_unit()).has_value()) {
- dest.append(component_value.release_value());
- return true;
- }
- }
- } else if (attribute_type.equals_ignoring_ascii_case("ident"_fly_string)) {
- // The substitution value is a CSS <custom-ident>, whose value is the literal value of the attribute,
- // with leading and trailing ASCII whitespace stripped. (No CSS parsing of the value is performed.)
- // If the attribute value, after trimming, is the empty string, there is instead no substitution value.
- // If the <custom-ident>’s value is a CSS-wide keyword or `default`, there is instead no substitution value.
- auto substitution_value = MUST(attribute_value.trim(Infra::ASCII_WHITESPACE));
- if (!substitution_value.is_empty()
- && !substitution_value.equals_ignoring_ascii_case("default"sv)
- && !is_css_wide_keyword(substitution_value)) {
- dest.empend(Token::create_ident(substitution_value));
- return true;
- }
- } else if (attribute_type.equals_ignoring_ascii_case("length"_fly_string)) {
- // Parse a component value from the attribute’s value.
- auto component_value = Parser::Parser::create(m_context, attribute_value).parse_as_component_value();
- // If the result is a <dimension-token> whose unit matches the given type, the result is the substitution value.
- // Otherwise, there is no substitution value.
- if (component_value.has_value() && component_value->is(Token::Type::Dimension)) {
- if (Length::unit_from_name(component_value->token().dimension_unit()).has_value()) {
- dest.append(component_value.release_value());
- return true;
- }
- }
- } else if (attribute_type.equals_ignoring_ascii_case("number"_fly_string)) {
- // Parse a component value from the attribute’s value.
- // If the result is a <number-token>, the result is the substitution value.
- // Otherwise, there is no substitution value.
- auto component_value = Parser::Parser::create(m_context, attribute_value).parse_as_component_value();
- if (component_value.has_value() && component_value->is(Token::Type::Number)) {
- dest.append(component_value.release_value());
- return true;
- }
- } else if (attribute_type.equals_ignoring_ascii_case("percentage"_fly_string)) {
- // Parse a component value from the attribute’s value.
- auto component_value = Parser::Parser::create(m_context, attribute_value).parse_as_component_value();
- // If the result is a <percentage-token>, the result is the substitution value.
- // Otherwise, there is no substitution value.
- if (component_value.has_value() && component_value->is(Token::Type::Percentage)) {
- dest.append(component_value.release_value());
- return true;
- }
- } else if (attribute_type.equals_ignoring_ascii_case("string"_fly_string)) {
- // The substitution value is a CSS string, whose value is the literal value of the attribute.
- // (No CSS parsing or "cleanup" of the value is performed.)
- // No value triggers fallback.
- dest.empend(Token::create_string(attribute_value));
- return true;
- } else if (attribute_type.equals_ignoring_ascii_case("time"_fly_string)) {
- // Parse a component value from the attribute’s value.
- auto component_value = Parser::Parser::create(m_context, attribute_value).parse_as_component_value();
- // If the result is a <dimension-token> whose unit matches the given type, the result is the substitution value.
- // Otherwise, there is no substitution value.
- if (component_value.has_value() && component_value->is(Token::Type::Dimension)) {
- if (Time::unit_from_name(component_value->token().dimension_unit()).has_value()) {
- dest.append(component_value.release_value());
- return true;
- }
- }
- } else if (attribute_type.equals_ignoring_ascii_case("url"_fly_string)) {
- // The substitution value is a CSS <url> value, whose url is the literal value of the attribute.
- // (No CSS parsing or "cleanup" of the value is performed.)
- // No value triggers fallback.
- dest.empend(Token::create_url(attribute_value));
- return true;
- } else {
- // Dimension units
- // Parse a component value from the attribute’s value.
- // If the result is a <number-token>, the substitution value is a dimension with the result’s value, and the given unit.
- // Otherwise, there is no substitution value.
- auto component_value = Parser::Parser::create(m_context, attribute_value).parse_as_component_value();
- if (component_value.has_value() && component_value->is(Token::Type::Number)) {
- if (attribute_value == "%"sv) {
- dest.empend(Token::create_dimension(component_value->token().number_value(), attribute_type));
- return true;
- } else if (auto angle_unit = Angle::unit_from_name(attribute_type); angle_unit.has_value()) {
- dest.empend(Token::create_dimension(component_value->token().number_value(), attribute_type));
- return true;
- } else if (auto flex_unit = Flex::unit_from_name(attribute_type); flex_unit.has_value()) {
- dest.empend(Token::create_dimension(component_value->token().number_value(), attribute_type));
- return true;
- } else if (auto frequency_unit = Frequency::unit_from_name(attribute_type); frequency_unit.has_value()) {
- dest.empend(Token::create_dimension(component_value->token().number_value(), attribute_type));
- return true;
- } else if (auto length_unit = Length::unit_from_name(attribute_type); length_unit.has_value()) {
- dest.empend(Token::create_dimension(component_value->token().number_value(), attribute_type));
- return true;
- } else if (auto time_unit = Time::unit_from_name(attribute_type); time_unit.has_value()) {
- dest.empend(Token::create_dimension(component_value->token().number_value(), attribute_type));
- return true;
- } else {
- // Not a dimension unit.
- return false;
- }
- }
- }
- }
- // 2. Otherwise, if the attr() function has a fallback value as its last argument, replace the attr() function by the fallback value.
- // If there are any var() or attr() references in the fallback, substitute them as well.
- if (has_fallback_values)
- return expand_unresolved_values(element, property_name, attr_contents, dest);
- if (attribute_type.equals_ignoring_ascii_case("string"_fly_string)) {
- // If the <attr-type> argument is string, defaults to the empty string if omitted
- dest.empend(Token::create_string({}));
- return true;
- }
- // 3. Otherwise, the property containing the attr() function is invalid at computed-value time.
- return false;
- }
- // https://drafts.csswg.org/css-fonts/#typedef-opentype-tag
- RefPtr<StringStyleValue> Parser::parse_opentype_tag_value(TokenStream<ComponentValue>& tokens)
- {
- // <opentype-tag> = <string>
- // The <opentype-tag> is a case-sensitive OpenType feature tag.
- // As specified in the OpenType specification [OPENTYPE], feature tags contain four ASCII characters.
- // Tag strings longer or shorter than four characters, or containing characters outside the U+20–7E codepoint range are invalid.
- auto transaction = tokens.begin_transaction();
- auto string_value = parse_string_value(tokens);
- if (string_value == nullptr)
- return nullptr;
- auto string = string_value->string_value().bytes_as_string_view();
- if (string.length() != 4)
- return nullptr;
- for (char c : string) {
- if (c < 0x20 || c > 0x7E)
- return nullptr;
- }
- transaction.commit();
- return string_value;
- }
- Parser::ContextType Parser::context_type_for_at_rule(FlyString const& name)
- {
- if (name == "media")
- return ContextType::AtMedia;
- if (name == "font-face")
- return ContextType::AtFontFace;
- if (name == "keyframes")
- return ContextType::AtKeyframes;
- if (name == "supports")
- return ContextType::AtSupports;
- if (name == "layer")
- return ContextType::AtLayer;
- if (name == "property")
- return ContextType::AtProperty;
- return ContextType::Unknown;
- }
- }
|