compiler.mjs 1.3 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527
  1. /**
  2. * @license Angular v19.2.4
  3. * (c) 2010-2025 Google LLC. https://angular.io/
  4. * License: MIT
  5. */
  6. const _SELECTOR_REGEXP = new RegExp('(\\:not\\()|' + // 1: ":not("
  7. '(([\\.\\#]?)[-\\w]+)|' + // 2: "tag"; 3: "."/"#";
  8. // "-" should appear first in the regexp below as FF31 parses "[.-\w]" as a range
  9. // 4: attribute; 5: attribute_string; 6: attribute_value
  10. '(?:\\[([-.\\w*\\\\$]+)(?:=(["\']?)([^\\]"\']*)\\5)?\\])|' + // "[name]", "[name=value]",
  11. // "[name="value"]",
  12. // "[name='value']"
  13. '(\\))|' + // 7: ")"
  14. '(\\s*,\\s*)', // 8: ","
  15. 'g');
  16. /**
  17. * A css selector contains an element name,
  18. * css classes and attribute/value pairs with the purpose
  19. * of selecting subsets out of them.
  20. */
  21. class CssSelector {
  22. element = null;
  23. classNames = [];
  24. /**
  25. * The selectors are encoded in pairs where:
  26. * - even locations are attribute names
  27. * - odd locations are attribute values.
  28. *
  29. * Example:
  30. * Selector: `[key1=value1][key2]` would parse to:
  31. * ```
  32. * ['key1', 'value1', 'key2', '']
  33. * ```
  34. */
  35. attrs = [];
  36. notSelectors = [];
  37. static parse(selector) {
  38. const results = [];
  39. const _addResult = (res, cssSel) => {
  40. if (cssSel.notSelectors.length > 0 &&
  41. !cssSel.element &&
  42. cssSel.classNames.length == 0 &&
  43. cssSel.attrs.length == 0) {
  44. cssSel.element = '*';
  45. }
  46. res.push(cssSel);
  47. };
  48. let cssSelector = new CssSelector();
  49. let match;
  50. let current = cssSelector;
  51. let inNot = false;
  52. _SELECTOR_REGEXP.lastIndex = 0;
  53. while ((match = _SELECTOR_REGEXP.exec(selector))) {
  54. if (match[1 /* SelectorRegexp.NOT */]) {
  55. if (inNot) {
  56. throw new Error('Nesting :not in a selector is not allowed');
  57. }
  58. inNot = true;
  59. current = new CssSelector();
  60. cssSelector.notSelectors.push(current);
  61. }
  62. const tag = match[2 /* SelectorRegexp.TAG */];
  63. if (tag) {
  64. const prefix = match[3 /* SelectorRegexp.PREFIX */];
  65. if (prefix === '#') {
  66. // #hash
  67. current.addAttribute('id', tag.slice(1));
  68. }
  69. else if (prefix === '.') {
  70. // Class
  71. current.addClassName(tag.slice(1));
  72. }
  73. else {
  74. // Element
  75. current.setElement(tag);
  76. }
  77. }
  78. const attribute = match[4 /* SelectorRegexp.ATTRIBUTE */];
  79. if (attribute) {
  80. current.addAttribute(current.unescapeAttribute(attribute), match[6 /* SelectorRegexp.ATTRIBUTE_VALUE */]);
  81. }
  82. if (match[7 /* SelectorRegexp.NOT_END */]) {
  83. inNot = false;
  84. current = cssSelector;
  85. }
  86. if (match[8 /* SelectorRegexp.SEPARATOR */]) {
  87. if (inNot) {
  88. throw new Error('Multiple selectors in :not are not supported');
  89. }
  90. _addResult(results, cssSelector);
  91. cssSelector = current = new CssSelector();
  92. }
  93. }
  94. _addResult(results, cssSelector);
  95. return results;
  96. }
  97. /**
  98. * Unescape `\$` sequences from the CSS attribute selector.
  99. *
  100. * This is needed because `$` can have a special meaning in CSS selectors,
  101. * but we might want to match an attribute that contains `$`.
  102. * [MDN web link for more
  103. * info](https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors).
  104. * @param attr the attribute to unescape.
  105. * @returns the unescaped string.
  106. */
  107. unescapeAttribute(attr) {
  108. let result = '';
  109. let escaping = false;
  110. for (let i = 0; i < attr.length; i++) {
  111. const char = attr.charAt(i);
  112. if (char === '\\') {
  113. escaping = true;
  114. continue;
  115. }
  116. if (char === '$' && !escaping) {
  117. throw new Error(`Error in attribute selector "${attr}". ` +
  118. `Unescaped "$" is not supported. Please escape with "\\$".`);
  119. }
  120. escaping = false;
  121. result += char;
  122. }
  123. return result;
  124. }
  125. /**
  126. * Escape `$` sequences from the CSS attribute selector.
  127. *
  128. * This is needed because `$` can have a special meaning in CSS selectors,
  129. * with this method we are escaping `$` with `\$'.
  130. * [MDN web link for more
  131. * info](https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors).
  132. * @param attr the attribute to escape.
  133. * @returns the escaped string.
  134. */
  135. escapeAttribute(attr) {
  136. return attr.replace(/\\/g, '\\\\').replace(/\$/g, '\\$');
  137. }
  138. isElementSelector() {
  139. return (this.hasElementSelector() &&
  140. this.classNames.length == 0 &&
  141. this.attrs.length == 0 &&
  142. this.notSelectors.length === 0);
  143. }
  144. hasElementSelector() {
  145. return !!this.element;
  146. }
  147. setElement(element = null) {
  148. this.element = element;
  149. }
  150. getAttrs() {
  151. const result = [];
  152. if (this.classNames.length > 0) {
  153. result.push('class', this.classNames.join(' '));
  154. }
  155. return result.concat(this.attrs);
  156. }
  157. addAttribute(name, value = '') {
  158. this.attrs.push(name, (value && value.toLowerCase()) || '');
  159. }
  160. addClassName(name) {
  161. this.classNames.push(name.toLowerCase());
  162. }
  163. toString() {
  164. let res = this.element || '';
  165. if (this.classNames) {
  166. this.classNames.forEach((klass) => (res += `.${klass}`));
  167. }
  168. if (this.attrs) {
  169. for (let i = 0; i < this.attrs.length; i += 2) {
  170. const name = this.escapeAttribute(this.attrs[i]);
  171. const value = this.attrs[i + 1];
  172. res += `[${name}${value ? '=' + value : ''}]`;
  173. }
  174. }
  175. this.notSelectors.forEach((notSelector) => (res += `:not(${notSelector})`));
  176. return res;
  177. }
  178. }
  179. /**
  180. * Reads a list of CssSelectors and allows to calculate which ones
  181. * are contained in a given CssSelector.
  182. */
  183. class SelectorMatcher {
  184. static createNotMatcher(notSelectors) {
  185. const notMatcher = new SelectorMatcher();
  186. notMatcher.addSelectables(notSelectors, null);
  187. return notMatcher;
  188. }
  189. _elementMap = new Map();
  190. _elementPartialMap = new Map();
  191. _classMap = new Map();
  192. _classPartialMap = new Map();
  193. _attrValueMap = new Map();
  194. _attrValuePartialMap = new Map();
  195. _listContexts = [];
  196. addSelectables(cssSelectors, callbackCtxt) {
  197. let listContext = null;
  198. if (cssSelectors.length > 1) {
  199. listContext = new SelectorListContext(cssSelectors);
  200. this._listContexts.push(listContext);
  201. }
  202. for (let i = 0; i < cssSelectors.length; i++) {
  203. this._addSelectable(cssSelectors[i], callbackCtxt, listContext);
  204. }
  205. }
  206. /**
  207. * Add an object that can be found later on by calling `match`.
  208. * @param cssSelector A css selector
  209. * @param callbackCtxt An opaque object that will be given to the callback of the `match` function
  210. */
  211. _addSelectable(cssSelector, callbackCtxt, listContext) {
  212. let matcher = this;
  213. const element = cssSelector.element;
  214. const classNames = cssSelector.classNames;
  215. const attrs = cssSelector.attrs;
  216. const selectable = new SelectorContext(cssSelector, callbackCtxt, listContext);
  217. if (element) {
  218. const isTerminal = attrs.length === 0 && classNames.length === 0;
  219. if (isTerminal) {
  220. this._addTerminal(matcher._elementMap, element, selectable);
  221. }
  222. else {
  223. matcher = this._addPartial(matcher._elementPartialMap, element);
  224. }
  225. }
  226. if (classNames) {
  227. for (let i = 0; i < classNames.length; i++) {
  228. const isTerminal = attrs.length === 0 && i === classNames.length - 1;
  229. const className = classNames[i];
  230. if (isTerminal) {
  231. this._addTerminal(matcher._classMap, className, selectable);
  232. }
  233. else {
  234. matcher = this._addPartial(matcher._classPartialMap, className);
  235. }
  236. }
  237. }
  238. if (attrs) {
  239. for (let i = 0; i < attrs.length; i += 2) {
  240. const isTerminal = i === attrs.length - 2;
  241. const name = attrs[i];
  242. const value = attrs[i + 1];
  243. if (isTerminal) {
  244. const terminalMap = matcher._attrValueMap;
  245. let terminalValuesMap = terminalMap.get(name);
  246. if (!terminalValuesMap) {
  247. terminalValuesMap = new Map();
  248. terminalMap.set(name, terminalValuesMap);
  249. }
  250. this._addTerminal(terminalValuesMap, value, selectable);
  251. }
  252. else {
  253. const partialMap = matcher._attrValuePartialMap;
  254. let partialValuesMap = partialMap.get(name);
  255. if (!partialValuesMap) {
  256. partialValuesMap = new Map();
  257. partialMap.set(name, partialValuesMap);
  258. }
  259. matcher = this._addPartial(partialValuesMap, value);
  260. }
  261. }
  262. }
  263. }
  264. _addTerminal(map, name, selectable) {
  265. let terminalList = map.get(name);
  266. if (!terminalList) {
  267. terminalList = [];
  268. map.set(name, terminalList);
  269. }
  270. terminalList.push(selectable);
  271. }
  272. _addPartial(map, name) {
  273. let matcher = map.get(name);
  274. if (!matcher) {
  275. matcher = new SelectorMatcher();
  276. map.set(name, matcher);
  277. }
  278. return matcher;
  279. }
  280. /**
  281. * Find the objects that have been added via `addSelectable`
  282. * whose css selector is contained in the given css selector.
  283. * @param cssSelector A css selector
  284. * @param matchedCallback This callback will be called with the object handed into `addSelectable`
  285. * @return boolean true if a match was found
  286. */
  287. match(cssSelector, matchedCallback) {
  288. let result = false;
  289. const element = cssSelector.element;
  290. const classNames = cssSelector.classNames;
  291. const attrs = cssSelector.attrs;
  292. for (let i = 0; i < this._listContexts.length; i++) {
  293. this._listContexts[i].alreadyMatched = false;
  294. }
  295. result = this._matchTerminal(this._elementMap, element, cssSelector, matchedCallback) || result;
  296. result =
  297. this._matchPartial(this._elementPartialMap, element, cssSelector, matchedCallback) || result;
  298. if (classNames) {
  299. for (let i = 0; i < classNames.length; i++) {
  300. const className = classNames[i];
  301. result =
  302. this._matchTerminal(this._classMap, className, cssSelector, matchedCallback) || result;
  303. result =
  304. this._matchPartial(this._classPartialMap, className, cssSelector, matchedCallback) ||
  305. result;
  306. }
  307. }
  308. if (attrs) {
  309. for (let i = 0; i < attrs.length; i += 2) {
  310. const name = attrs[i];
  311. const value = attrs[i + 1];
  312. const terminalValuesMap = this._attrValueMap.get(name);
  313. if (value) {
  314. result =
  315. this._matchTerminal(terminalValuesMap, '', cssSelector, matchedCallback) || result;
  316. }
  317. result =
  318. this._matchTerminal(terminalValuesMap, value, cssSelector, matchedCallback) || result;
  319. const partialValuesMap = this._attrValuePartialMap.get(name);
  320. if (value) {
  321. result = this._matchPartial(partialValuesMap, '', cssSelector, matchedCallback) || result;
  322. }
  323. result =
  324. this._matchPartial(partialValuesMap, value, cssSelector, matchedCallback) || result;
  325. }
  326. }
  327. return result;
  328. }
  329. /** @internal */
  330. _matchTerminal(map, name, cssSelector, matchedCallback) {
  331. if (!map || typeof name !== 'string') {
  332. return false;
  333. }
  334. let selectables = map.get(name) || [];
  335. const starSelectables = map.get('*');
  336. if (starSelectables) {
  337. selectables = selectables.concat(starSelectables);
  338. }
  339. if (selectables.length === 0) {
  340. return false;
  341. }
  342. let selectable;
  343. let result = false;
  344. for (let i = 0; i < selectables.length; i++) {
  345. selectable = selectables[i];
  346. result = selectable.finalize(cssSelector, matchedCallback) || result;
  347. }
  348. return result;
  349. }
  350. /** @internal */
  351. _matchPartial(map, name, cssSelector, matchedCallback) {
  352. if (!map || typeof name !== 'string') {
  353. return false;
  354. }
  355. const nestedSelector = map.get(name);
  356. if (!nestedSelector) {
  357. return false;
  358. }
  359. // TODO(perf): get rid of recursion and measure again
  360. // TODO(perf): don't pass the whole selector into the recursion,
  361. // but only the not processed parts
  362. return nestedSelector.match(cssSelector, matchedCallback);
  363. }
  364. }
  365. class SelectorListContext {
  366. selectors;
  367. alreadyMatched = false;
  368. constructor(selectors) {
  369. this.selectors = selectors;
  370. }
  371. }
  372. // Store context to pass back selector and context when a selector is matched
  373. class SelectorContext {
  374. selector;
  375. cbContext;
  376. listContext;
  377. notSelectors;
  378. constructor(selector, cbContext, listContext) {
  379. this.selector = selector;
  380. this.cbContext = cbContext;
  381. this.listContext = listContext;
  382. this.notSelectors = selector.notSelectors;
  383. }
  384. finalize(cssSelector, callback) {
  385. let result = true;
  386. if (this.notSelectors.length > 0 && (!this.listContext || !this.listContext.alreadyMatched)) {
  387. const notMatcher = SelectorMatcher.createNotMatcher(this.notSelectors);
  388. result = !notMatcher.match(cssSelector, null);
  389. }
  390. if (result && callback && (!this.listContext || !this.listContext.alreadyMatched)) {
  391. if (this.listContext) {
  392. this.listContext.alreadyMatched = true;
  393. }
  394. callback(this.selector, this.cbContext);
  395. }
  396. return result;
  397. }
  398. }
  399. // Attention:
  400. // This file duplicates types and values from @angular/core
  401. // so that we are able to make @angular/compiler independent of @angular/core.
  402. // This is important to prevent a build cycle, as @angular/core needs to
  403. // be compiled with the compiler.
  404. // Stores the default value of `emitDistinctChangesOnly` when the `emitDistinctChangesOnly` is not
  405. // explicitly set.
  406. const emitDistinctChangesOnlyDefaultValue = true;
  407. var ViewEncapsulation;
  408. (function (ViewEncapsulation) {
  409. ViewEncapsulation[ViewEncapsulation["Emulated"] = 0] = "Emulated";
  410. // Historically the 1 value was for `Native` encapsulation which has been removed as of v11.
  411. ViewEncapsulation[ViewEncapsulation["None"] = 2] = "None";
  412. ViewEncapsulation[ViewEncapsulation["ShadowDom"] = 3] = "ShadowDom";
  413. })(ViewEncapsulation || (ViewEncapsulation = {}));
  414. var ChangeDetectionStrategy;
  415. (function (ChangeDetectionStrategy) {
  416. ChangeDetectionStrategy[ChangeDetectionStrategy["OnPush"] = 0] = "OnPush";
  417. ChangeDetectionStrategy[ChangeDetectionStrategy["Default"] = 1] = "Default";
  418. })(ChangeDetectionStrategy || (ChangeDetectionStrategy = {}));
  419. /** Flags describing an input for a directive. */
  420. var InputFlags;
  421. (function (InputFlags) {
  422. InputFlags[InputFlags["None"] = 0] = "None";
  423. InputFlags[InputFlags["SignalBased"] = 1] = "SignalBased";
  424. InputFlags[InputFlags["HasDecoratorInputTransform"] = 2] = "HasDecoratorInputTransform";
  425. })(InputFlags || (InputFlags = {}));
  426. const CUSTOM_ELEMENTS_SCHEMA = {
  427. name: 'custom-elements',
  428. };
  429. const NO_ERRORS_SCHEMA = {
  430. name: 'no-errors-schema',
  431. };
  432. const Type$1 = Function;
  433. var SecurityContext;
  434. (function (SecurityContext) {
  435. SecurityContext[SecurityContext["NONE"] = 0] = "NONE";
  436. SecurityContext[SecurityContext["HTML"] = 1] = "HTML";
  437. SecurityContext[SecurityContext["STYLE"] = 2] = "STYLE";
  438. SecurityContext[SecurityContext["SCRIPT"] = 3] = "SCRIPT";
  439. SecurityContext[SecurityContext["URL"] = 4] = "URL";
  440. SecurityContext[SecurityContext["RESOURCE_URL"] = 5] = "RESOURCE_URL";
  441. })(SecurityContext || (SecurityContext = {}));
  442. var MissingTranslationStrategy;
  443. (function (MissingTranslationStrategy) {
  444. MissingTranslationStrategy[MissingTranslationStrategy["Error"] = 0] = "Error";
  445. MissingTranslationStrategy[MissingTranslationStrategy["Warning"] = 1] = "Warning";
  446. MissingTranslationStrategy[MissingTranslationStrategy["Ignore"] = 2] = "Ignore";
  447. })(MissingTranslationStrategy || (MissingTranslationStrategy = {}));
  448. function parserSelectorToSimpleSelector(selector) {
  449. const classes = selector.classNames && selector.classNames.length
  450. ? [8 /* SelectorFlags.CLASS */, ...selector.classNames]
  451. : [];
  452. const elementName = selector.element && selector.element !== '*' ? selector.element : '';
  453. return [elementName, ...selector.attrs, ...classes];
  454. }
  455. function parserSelectorToNegativeSelector(selector) {
  456. const classes = selector.classNames && selector.classNames.length
  457. ? [8 /* SelectorFlags.CLASS */, ...selector.classNames]
  458. : [];
  459. if (selector.element) {
  460. return [
  461. 1 /* SelectorFlags.NOT */ | 4 /* SelectorFlags.ELEMENT */,
  462. selector.element,
  463. ...selector.attrs,
  464. ...classes,
  465. ];
  466. }
  467. else if (selector.attrs.length) {
  468. return [1 /* SelectorFlags.NOT */ | 2 /* SelectorFlags.ATTRIBUTE */, ...selector.attrs, ...classes];
  469. }
  470. else {
  471. return selector.classNames && selector.classNames.length
  472. ? [1 /* SelectorFlags.NOT */ | 8 /* SelectorFlags.CLASS */, ...selector.classNames]
  473. : [];
  474. }
  475. }
  476. function parserSelectorToR3Selector(selector) {
  477. const positive = parserSelectorToSimpleSelector(selector);
  478. const negative = selector.notSelectors && selector.notSelectors.length
  479. ? selector.notSelectors.map((notSelector) => parserSelectorToNegativeSelector(notSelector))
  480. : [];
  481. return positive.concat(...negative);
  482. }
  483. function parseSelectorToR3Selector(selector) {
  484. return selector ? CssSelector.parse(selector).map(parserSelectorToR3Selector) : [];
  485. }
  486. var core = /*#__PURE__*/Object.freeze({
  487. __proto__: null,
  488. CUSTOM_ELEMENTS_SCHEMA: CUSTOM_ELEMENTS_SCHEMA,
  489. get ChangeDetectionStrategy () { return ChangeDetectionStrategy; },
  490. get InputFlags () { return InputFlags; },
  491. get MissingTranslationStrategy () { return MissingTranslationStrategy; },
  492. NO_ERRORS_SCHEMA: NO_ERRORS_SCHEMA,
  493. get SecurityContext () { return SecurityContext; },
  494. Type: Type$1,
  495. get ViewEncapsulation () { return ViewEncapsulation; },
  496. emitDistinctChangesOnlyDefaultValue: emitDistinctChangesOnlyDefaultValue,
  497. parseSelectorToR3Selector: parseSelectorToR3Selector
  498. });
  499. /**
  500. * A lazily created TextEncoder instance for converting strings into UTF-8 bytes
  501. */
  502. let textEncoder;
  503. /**
  504. * Return the message id or compute it using the XLIFF1 digest.
  505. */
  506. function digest$1(message) {
  507. return message.id || computeDigest(message);
  508. }
  509. /**
  510. * Compute the message id using the XLIFF1 digest.
  511. */
  512. function computeDigest(message) {
  513. return sha1(serializeNodes(message.nodes).join('') + `[${message.meaning}]`);
  514. }
  515. /**
  516. * Return the message id or compute it using the XLIFF2/XMB/$localize digest.
  517. */
  518. function decimalDigest(message) {
  519. return message.id || computeDecimalDigest(message);
  520. }
  521. /**
  522. * Compute the message id using the XLIFF2/XMB/$localize digest.
  523. */
  524. function computeDecimalDigest(message) {
  525. const visitor = new _SerializerIgnoreIcuExpVisitor();
  526. const parts = message.nodes.map((a) => a.visit(visitor, null));
  527. return computeMsgId(parts.join(''), message.meaning);
  528. }
  529. /**
  530. * Serialize the i18n ast to something xml-like in order to generate an UID.
  531. *
  532. * The visitor is also used in the i18n parser tests
  533. *
  534. * @internal
  535. */
  536. class _SerializerVisitor {
  537. visitText(text, context) {
  538. return text.value;
  539. }
  540. visitContainer(container, context) {
  541. return `[${container.children.map((child) => child.visit(this)).join(', ')}]`;
  542. }
  543. visitIcu(icu, context) {
  544. const strCases = Object.keys(icu.cases).map((k) => `${k} {${icu.cases[k].visit(this)}}`);
  545. return `{${icu.expression}, ${icu.type}, ${strCases.join(', ')}}`;
  546. }
  547. visitTagPlaceholder(ph, context) {
  548. return ph.isVoid
  549. ? `<ph tag name="${ph.startName}"/>`
  550. : `<ph tag name="${ph.startName}">${ph.children
  551. .map((child) => child.visit(this))
  552. .join(', ')}</ph name="${ph.closeName}">`;
  553. }
  554. visitPlaceholder(ph, context) {
  555. return ph.value ? `<ph name="${ph.name}">${ph.value}</ph>` : `<ph name="${ph.name}"/>`;
  556. }
  557. visitIcuPlaceholder(ph, context) {
  558. return `<ph icu name="${ph.name}">${ph.value.visit(this)}</ph>`;
  559. }
  560. visitBlockPlaceholder(ph, context) {
  561. return `<ph block name="${ph.startName}">${ph.children
  562. .map((child) => child.visit(this))
  563. .join(', ')}</ph name="${ph.closeName}">`;
  564. }
  565. }
  566. const serializerVisitor$1 = new _SerializerVisitor();
  567. function serializeNodes(nodes) {
  568. return nodes.map((a) => a.visit(serializerVisitor$1, null));
  569. }
  570. /**
  571. * Serialize the i18n ast to something xml-like in order to generate an UID.
  572. *
  573. * Ignore the ICU expressions so that message IDs stays identical if only the expression changes.
  574. *
  575. * @internal
  576. */
  577. class _SerializerIgnoreIcuExpVisitor extends _SerializerVisitor {
  578. visitIcu(icu) {
  579. let strCases = Object.keys(icu.cases).map((k) => `${k} {${icu.cases[k].visit(this)}}`);
  580. // Do not take the expression into account
  581. return `{${icu.type}, ${strCases.join(', ')}}`;
  582. }
  583. }
  584. /**
  585. * Compute the SHA1 of the given string
  586. *
  587. * see https://csrc.nist.gov/publications/fips/fips180-4/fips-180-4.pdf
  588. *
  589. * WARNING: this function has not been designed not tested with security in mind.
  590. * DO NOT USE IT IN A SECURITY SENSITIVE CONTEXT.
  591. */
  592. function sha1(str) {
  593. textEncoder ??= new TextEncoder();
  594. const utf8 = [...textEncoder.encode(str)];
  595. const words32 = bytesToWords32(utf8, Endian.Big);
  596. const len = utf8.length * 8;
  597. const w = new Uint32Array(80);
  598. let a = 0x67452301, b = 0xefcdab89, c = 0x98badcfe, d = 0x10325476, e = 0xc3d2e1f0;
  599. words32[len >> 5] |= 0x80 << (24 - (len % 32));
  600. words32[(((len + 64) >> 9) << 4) + 15] = len;
  601. for (let i = 0; i < words32.length; i += 16) {
  602. const h0 = a, h1 = b, h2 = c, h3 = d, h4 = e;
  603. for (let j = 0; j < 80; j++) {
  604. if (j < 16) {
  605. w[j] = words32[i + j];
  606. }
  607. else {
  608. w[j] = rol32(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
  609. }
  610. const fkVal = fk(j, b, c, d);
  611. const f = fkVal[0];
  612. const k = fkVal[1];
  613. const temp = [rol32(a, 5), f, e, k, w[j]].reduce(add32);
  614. e = d;
  615. d = c;
  616. c = rol32(b, 30);
  617. b = a;
  618. a = temp;
  619. }
  620. a = add32(a, h0);
  621. b = add32(b, h1);
  622. c = add32(c, h2);
  623. d = add32(d, h3);
  624. e = add32(e, h4);
  625. }
  626. // Convert the output parts to a 160-bit hexadecimal string
  627. return toHexU32(a) + toHexU32(b) + toHexU32(c) + toHexU32(d) + toHexU32(e);
  628. }
  629. /**
  630. * Convert and format a number as a string representing a 32-bit unsigned hexadecimal number.
  631. * @param value The value to format as a string.
  632. * @returns A hexadecimal string representing the value.
  633. */
  634. function toHexU32(value) {
  635. // unsigned right shift of zero ensures an unsigned 32-bit number
  636. return (value >>> 0).toString(16).padStart(8, '0');
  637. }
  638. function fk(index, b, c, d) {
  639. if (index < 20) {
  640. return [(b & c) | (~b & d), 0x5a827999];
  641. }
  642. if (index < 40) {
  643. return [b ^ c ^ d, 0x6ed9eba1];
  644. }
  645. if (index < 60) {
  646. return [(b & c) | (b & d) | (c & d), 0x8f1bbcdc];
  647. }
  648. return [b ^ c ^ d, 0xca62c1d6];
  649. }
  650. /**
  651. * Compute the fingerprint of the given string
  652. *
  653. * The output is 64 bit number encoded as a decimal string
  654. *
  655. * based on:
  656. * https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/GoogleJsMessageIdGenerator.java
  657. */
  658. function fingerprint(str) {
  659. textEncoder ??= new TextEncoder();
  660. const utf8 = textEncoder.encode(str);
  661. const view = new DataView(utf8.buffer, utf8.byteOffset, utf8.byteLength);
  662. let hi = hash32(view, utf8.length, 0);
  663. let lo = hash32(view, utf8.length, 102072);
  664. if (hi == 0 && (lo == 0 || lo == 1)) {
  665. hi = hi ^ 0x130f9bef;
  666. lo = lo ^ -1801410264;
  667. }
  668. return (BigInt.asUintN(32, BigInt(hi)) << BigInt(32)) | BigInt.asUintN(32, BigInt(lo));
  669. }
  670. function computeMsgId(msg, meaning = '') {
  671. let msgFingerprint = fingerprint(msg);
  672. if (meaning) {
  673. // Rotate the 64-bit message fingerprint one bit to the left and then add the meaning
  674. // fingerprint.
  675. msgFingerprint =
  676. BigInt.asUintN(64, msgFingerprint << BigInt(1)) |
  677. ((msgFingerprint >> BigInt(63)) & BigInt(1));
  678. msgFingerprint += fingerprint(meaning);
  679. }
  680. return BigInt.asUintN(63, msgFingerprint).toString();
  681. }
  682. function hash32(view, length, c) {
  683. let a = 0x9e3779b9, b = 0x9e3779b9;
  684. let index = 0;
  685. const end = length - 12;
  686. for (; index <= end; index += 12) {
  687. a += view.getUint32(index, true);
  688. b += view.getUint32(index + 4, true);
  689. c += view.getUint32(index + 8, true);
  690. const res = mix(a, b, c);
  691. (a = res[0]), (b = res[1]), (c = res[2]);
  692. }
  693. const remainder = length - index;
  694. // the first byte of c is reserved for the length
  695. c += length;
  696. if (remainder >= 4) {
  697. a += view.getUint32(index, true);
  698. index += 4;
  699. if (remainder >= 8) {
  700. b += view.getUint32(index, true);
  701. index += 4;
  702. // Partial 32-bit word for c
  703. if (remainder >= 9) {
  704. c += view.getUint8(index++) << 8;
  705. }
  706. if (remainder >= 10) {
  707. c += view.getUint8(index++) << 16;
  708. }
  709. if (remainder === 11) {
  710. c += view.getUint8(index++) << 24;
  711. }
  712. }
  713. else {
  714. // Partial 32-bit word for b
  715. if (remainder >= 5) {
  716. b += view.getUint8(index++);
  717. }
  718. if (remainder >= 6) {
  719. b += view.getUint8(index++) << 8;
  720. }
  721. if (remainder === 7) {
  722. b += view.getUint8(index++) << 16;
  723. }
  724. }
  725. }
  726. else {
  727. // Partial 32-bit word for a
  728. if (remainder >= 1) {
  729. a += view.getUint8(index++);
  730. }
  731. if (remainder >= 2) {
  732. a += view.getUint8(index++) << 8;
  733. }
  734. if (remainder === 3) {
  735. a += view.getUint8(index++) << 16;
  736. }
  737. }
  738. return mix(a, b, c)[2];
  739. }
  740. function mix(a, b, c) {
  741. a -= b;
  742. a -= c;
  743. a ^= c >>> 13;
  744. b -= c;
  745. b -= a;
  746. b ^= a << 8;
  747. c -= a;
  748. c -= b;
  749. c ^= b >>> 13;
  750. a -= b;
  751. a -= c;
  752. a ^= c >>> 12;
  753. b -= c;
  754. b -= a;
  755. b ^= a << 16;
  756. c -= a;
  757. c -= b;
  758. c ^= b >>> 5;
  759. a -= b;
  760. a -= c;
  761. a ^= c >>> 3;
  762. b -= c;
  763. b -= a;
  764. b ^= a << 10;
  765. c -= a;
  766. c -= b;
  767. c ^= b >>> 15;
  768. return [a, b, c];
  769. }
  770. // Utils
  771. var Endian;
  772. (function (Endian) {
  773. Endian[Endian["Little"] = 0] = "Little";
  774. Endian[Endian["Big"] = 1] = "Big";
  775. })(Endian || (Endian = {}));
  776. function add32(a, b) {
  777. return add32to64(a, b)[1];
  778. }
  779. function add32to64(a, b) {
  780. const low = (a & 0xffff) + (b & 0xffff);
  781. const high = (a >>> 16) + (b >>> 16) + (low >>> 16);
  782. return [high >>> 16, (high << 16) | (low & 0xffff)];
  783. }
  784. // Rotate a 32b number left `count` position
  785. function rol32(a, count) {
  786. return (a << count) | (a >>> (32 - count));
  787. }
  788. function bytesToWords32(bytes, endian) {
  789. const size = (bytes.length + 3) >>> 2;
  790. const words32 = [];
  791. for (let i = 0; i < size; i++) {
  792. words32[i] = wordAt(bytes, i * 4, endian);
  793. }
  794. return words32;
  795. }
  796. function byteAt(bytes, index) {
  797. return index >= bytes.length ? 0 : bytes[index];
  798. }
  799. function wordAt(bytes, index, endian) {
  800. let word = 0;
  801. if (endian === Endian.Big) {
  802. for (let i = 0; i < 4; i++) {
  803. word += byteAt(bytes, index + i) << (24 - 8 * i);
  804. }
  805. }
  806. else {
  807. for (let i = 0; i < 4; i++) {
  808. word += byteAt(bytes, index + i) << (8 * i);
  809. }
  810. }
  811. return word;
  812. }
  813. //// Types
  814. var TypeModifier;
  815. (function (TypeModifier) {
  816. TypeModifier[TypeModifier["None"] = 0] = "None";
  817. TypeModifier[TypeModifier["Const"] = 1] = "Const";
  818. })(TypeModifier || (TypeModifier = {}));
  819. class Type {
  820. modifiers;
  821. constructor(modifiers = TypeModifier.None) {
  822. this.modifiers = modifiers;
  823. }
  824. hasModifier(modifier) {
  825. return (this.modifiers & modifier) !== 0;
  826. }
  827. }
  828. var BuiltinTypeName;
  829. (function (BuiltinTypeName) {
  830. BuiltinTypeName[BuiltinTypeName["Dynamic"] = 0] = "Dynamic";
  831. BuiltinTypeName[BuiltinTypeName["Bool"] = 1] = "Bool";
  832. BuiltinTypeName[BuiltinTypeName["String"] = 2] = "String";
  833. BuiltinTypeName[BuiltinTypeName["Int"] = 3] = "Int";
  834. BuiltinTypeName[BuiltinTypeName["Number"] = 4] = "Number";
  835. BuiltinTypeName[BuiltinTypeName["Function"] = 5] = "Function";
  836. BuiltinTypeName[BuiltinTypeName["Inferred"] = 6] = "Inferred";
  837. BuiltinTypeName[BuiltinTypeName["None"] = 7] = "None";
  838. })(BuiltinTypeName || (BuiltinTypeName = {}));
  839. class BuiltinType extends Type {
  840. name;
  841. constructor(name, modifiers) {
  842. super(modifiers);
  843. this.name = name;
  844. }
  845. visitType(visitor, context) {
  846. return visitor.visitBuiltinType(this, context);
  847. }
  848. }
  849. class ExpressionType extends Type {
  850. value;
  851. typeParams;
  852. constructor(value, modifiers, typeParams = null) {
  853. super(modifiers);
  854. this.value = value;
  855. this.typeParams = typeParams;
  856. }
  857. visitType(visitor, context) {
  858. return visitor.visitExpressionType(this, context);
  859. }
  860. }
  861. class ArrayType extends Type {
  862. of;
  863. constructor(of, modifiers) {
  864. super(modifiers);
  865. this.of = of;
  866. }
  867. visitType(visitor, context) {
  868. return visitor.visitArrayType(this, context);
  869. }
  870. }
  871. class MapType extends Type {
  872. valueType;
  873. constructor(valueType, modifiers) {
  874. super(modifiers);
  875. this.valueType = valueType || null;
  876. }
  877. visitType(visitor, context) {
  878. return visitor.visitMapType(this, context);
  879. }
  880. }
  881. class TransplantedType extends Type {
  882. type;
  883. constructor(type, modifiers) {
  884. super(modifiers);
  885. this.type = type;
  886. }
  887. visitType(visitor, context) {
  888. return visitor.visitTransplantedType(this, context);
  889. }
  890. }
  891. const DYNAMIC_TYPE = new BuiltinType(BuiltinTypeName.Dynamic);
  892. const INFERRED_TYPE = new BuiltinType(BuiltinTypeName.Inferred);
  893. const BOOL_TYPE = new BuiltinType(BuiltinTypeName.Bool);
  894. const INT_TYPE = new BuiltinType(BuiltinTypeName.Int);
  895. const NUMBER_TYPE = new BuiltinType(BuiltinTypeName.Number);
  896. const STRING_TYPE = new BuiltinType(BuiltinTypeName.String);
  897. const FUNCTION_TYPE = new BuiltinType(BuiltinTypeName.Function);
  898. const NONE_TYPE = new BuiltinType(BuiltinTypeName.None);
  899. ///// Expressions
  900. var UnaryOperator;
  901. (function (UnaryOperator) {
  902. UnaryOperator[UnaryOperator["Minus"] = 0] = "Minus";
  903. UnaryOperator[UnaryOperator["Plus"] = 1] = "Plus";
  904. })(UnaryOperator || (UnaryOperator = {}));
  905. var BinaryOperator;
  906. (function (BinaryOperator) {
  907. BinaryOperator[BinaryOperator["Equals"] = 0] = "Equals";
  908. BinaryOperator[BinaryOperator["NotEquals"] = 1] = "NotEquals";
  909. BinaryOperator[BinaryOperator["Identical"] = 2] = "Identical";
  910. BinaryOperator[BinaryOperator["NotIdentical"] = 3] = "NotIdentical";
  911. BinaryOperator[BinaryOperator["Minus"] = 4] = "Minus";
  912. BinaryOperator[BinaryOperator["Plus"] = 5] = "Plus";
  913. BinaryOperator[BinaryOperator["Divide"] = 6] = "Divide";
  914. BinaryOperator[BinaryOperator["Multiply"] = 7] = "Multiply";
  915. BinaryOperator[BinaryOperator["Modulo"] = 8] = "Modulo";
  916. BinaryOperator[BinaryOperator["And"] = 9] = "And";
  917. BinaryOperator[BinaryOperator["Or"] = 10] = "Or";
  918. BinaryOperator[BinaryOperator["BitwiseOr"] = 11] = "BitwiseOr";
  919. BinaryOperator[BinaryOperator["BitwiseAnd"] = 12] = "BitwiseAnd";
  920. BinaryOperator[BinaryOperator["Lower"] = 13] = "Lower";
  921. BinaryOperator[BinaryOperator["LowerEquals"] = 14] = "LowerEquals";
  922. BinaryOperator[BinaryOperator["Bigger"] = 15] = "Bigger";
  923. BinaryOperator[BinaryOperator["BiggerEquals"] = 16] = "BiggerEquals";
  924. BinaryOperator[BinaryOperator["NullishCoalesce"] = 17] = "NullishCoalesce";
  925. })(BinaryOperator || (BinaryOperator = {}));
  926. function nullSafeIsEquivalent(base, other) {
  927. if (base == null || other == null) {
  928. return base == other;
  929. }
  930. return base.isEquivalent(other);
  931. }
  932. function areAllEquivalentPredicate(base, other, equivalentPredicate) {
  933. const len = base.length;
  934. if (len !== other.length) {
  935. return false;
  936. }
  937. for (let i = 0; i < len; i++) {
  938. if (!equivalentPredicate(base[i], other[i])) {
  939. return false;
  940. }
  941. }
  942. return true;
  943. }
  944. function areAllEquivalent(base, other) {
  945. return areAllEquivalentPredicate(base, other, (baseElement, otherElement) => baseElement.isEquivalent(otherElement));
  946. }
  947. class Expression {
  948. type;
  949. sourceSpan;
  950. constructor(type, sourceSpan) {
  951. this.type = type || null;
  952. this.sourceSpan = sourceSpan || null;
  953. }
  954. prop(name, sourceSpan) {
  955. return new ReadPropExpr(this, name, null, sourceSpan);
  956. }
  957. key(index, type, sourceSpan) {
  958. return new ReadKeyExpr(this, index, type, sourceSpan);
  959. }
  960. callFn(params, sourceSpan, pure) {
  961. return new InvokeFunctionExpr(this, params, null, sourceSpan, pure);
  962. }
  963. instantiate(params, type, sourceSpan) {
  964. return new InstantiateExpr(this, params, type, sourceSpan);
  965. }
  966. conditional(trueCase, falseCase = null, sourceSpan) {
  967. return new ConditionalExpr(this, trueCase, falseCase, null, sourceSpan);
  968. }
  969. equals(rhs, sourceSpan) {
  970. return new BinaryOperatorExpr(BinaryOperator.Equals, this, rhs, null, sourceSpan);
  971. }
  972. notEquals(rhs, sourceSpan) {
  973. return new BinaryOperatorExpr(BinaryOperator.NotEquals, this, rhs, null, sourceSpan);
  974. }
  975. identical(rhs, sourceSpan) {
  976. return new BinaryOperatorExpr(BinaryOperator.Identical, this, rhs, null, sourceSpan);
  977. }
  978. notIdentical(rhs, sourceSpan) {
  979. return new BinaryOperatorExpr(BinaryOperator.NotIdentical, this, rhs, null, sourceSpan);
  980. }
  981. minus(rhs, sourceSpan) {
  982. return new BinaryOperatorExpr(BinaryOperator.Minus, this, rhs, null, sourceSpan);
  983. }
  984. plus(rhs, sourceSpan) {
  985. return new BinaryOperatorExpr(BinaryOperator.Plus, this, rhs, null, sourceSpan);
  986. }
  987. divide(rhs, sourceSpan) {
  988. return new BinaryOperatorExpr(BinaryOperator.Divide, this, rhs, null, sourceSpan);
  989. }
  990. multiply(rhs, sourceSpan) {
  991. return new BinaryOperatorExpr(BinaryOperator.Multiply, this, rhs, null, sourceSpan);
  992. }
  993. modulo(rhs, sourceSpan) {
  994. return new BinaryOperatorExpr(BinaryOperator.Modulo, this, rhs, null, sourceSpan);
  995. }
  996. and(rhs, sourceSpan) {
  997. return new BinaryOperatorExpr(BinaryOperator.And, this, rhs, null, sourceSpan);
  998. }
  999. bitwiseOr(rhs, sourceSpan, parens = true) {
  1000. return new BinaryOperatorExpr(BinaryOperator.BitwiseOr, this, rhs, null, sourceSpan, parens);
  1001. }
  1002. bitwiseAnd(rhs, sourceSpan, parens = true) {
  1003. return new BinaryOperatorExpr(BinaryOperator.BitwiseAnd, this, rhs, null, sourceSpan, parens);
  1004. }
  1005. or(rhs, sourceSpan) {
  1006. return new BinaryOperatorExpr(BinaryOperator.Or, this, rhs, null, sourceSpan);
  1007. }
  1008. lower(rhs, sourceSpan) {
  1009. return new BinaryOperatorExpr(BinaryOperator.Lower, this, rhs, null, sourceSpan);
  1010. }
  1011. lowerEquals(rhs, sourceSpan) {
  1012. return new BinaryOperatorExpr(BinaryOperator.LowerEquals, this, rhs, null, sourceSpan);
  1013. }
  1014. bigger(rhs, sourceSpan) {
  1015. return new BinaryOperatorExpr(BinaryOperator.Bigger, this, rhs, null, sourceSpan);
  1016. }
  1017. biggerEquals(rhs, sourceSpan) {
  1018. return new BinaryOperatorExpr(BinaryOperator.BiggerEquals, this, rhs, null, sourceSpan);
  1019. }
  1020. isBlank(sourceSpan) {
  1021. // Note: We use equals by purpose here to compare to null and undefined in JS.
  1022. // We use the typed null to allow strictNullChecks to narrow types.
  1023. return this.equals(TYPED_NULL_EXPR, sourceSpan);
  1024. }
  1025. nullishCoalesce(rhs, sourceSpan) {
  1026. return new BinaryOperatorExpr(BinaryOperator.NullishCoalesce, this, rhs, null, sourceSpan);
  1027. }
  1028. toStmt() {
  1029. return new ExpressionStatement(this, null);
  1030. }
  1031. }
  1032. class ReadVarExpr extends Expression {
  1033. name;
  1034. constructor(name, type, sourceSpan) {
  1035. super(type, sourceSpan);
  1036. this.name = name;
  1037. }
  1038. isEquivalent(e) {
  1039. return e instanceof ReadVarExpr && this.name === e.name;
  1040. }
  1041. isConstant() {
  1042. return false;
  1043. }
  1044. visitExpression(visitor, context) {
  1045. return visitor.visitReadVarExpr(this, context);
  1046. }
  1047. clone() {
  1048. return new ReadVarExpr(this.name, this.type, this.sourceSpan);
  1049. }
  1050. set(value) {
  1051. return new WriteVarExpr(this.name, value, null, this.sourceSpan);
  1052. }
  1053. }
  1054. class TypeofExpr extends Expression {
  1055. expr;
  1056. constructor(expr, type, sourceSpan) {
  1057. super(type, sourceSpan);
  1058. this.expr = expr;
  1059. }
  1060. visitExpression(visitor, context) {
  1061. return visitor.visitTypeofExpr(this, context);
  1062. }
  1063. isEquivalent(e) {
  1064. return e instanceof TypeofExpr && e.expr.isEquivalent(this.expr);
  1065. }
  1066. isConstant() {
  1067. return this.expr.isConstant();
  1068. }
  1069. clone() {
  1070. return new TypeofExpr(this.expr.clone());
  1071. }
  1072. }
  1073. class WrappedNodeExpr extends Expression {
  1074. node;
  1075. constructor(node, type, sourceSpan) {
  1076. super(type, sourceSpan);
  1077. this.node = node;
  1078. }
  1079. isEquivalent(e) {
  1080. return e instanceof WrappedNodeExpr && this.node === e.node;
  1081. }
  1082. isConstant() {
  1083. return false;
  1084. }
  1085. visitExpression(visitor, context) {
  1086. return visitor.visitWrappedNodeExpr(this, context);
  1087. }
  1088. clone() {
  1089. return new WrappedNodeExpr(this.node, this.type, this.sourceSpan);
  1090. }
  1091. }
  1092. class WriteVarExpr extends Expression {
  1093. name;
  1094. value;
  1095. constructor(name, value, type, sourceSpan) {
  1096. super(type || value.type, sourceSpan);
  1097. this.name = name;
  1098. this.value = value;
  1099. }
  1100. isEquivalent(e) {
  1101. return e instanceof WriteVarExpr && this.name === e.name && this.value.isEquivalent(e.value);
  1102. }
  1103. isConstant() {
  1104. return false;
  1105. }
  1106. visitExpression(visitor, context) {
  1107. return visitor.visitWriteVarExpr(this, context);
  1108. }
  1109. clone() {
  1110. return new WriteVarExpr(this.name, this.value.clone(), this.type, this.sourceSpan);
  1111. }
  1112. toDeclStmt(type, modifiers) {
  1113. return new DeclareVarStmt(this.name, this.value, type, modifiers, this.sourceSpan);
  1114. }
  1115. toConstDecl() {
  1116. return this.toDeclStmt(INFERRED_TYPE, StmtModifier.Final);
  1117. }
  1118. }
  1119. class WriteKeyExpr extends Expression {
  1120. receiver;
  1121. index;
  1122. value;
  1123. constructor(receiver, index, value, type, sourceSpan) {
  1124. super(type || value.type, sourceSpan);
  1125. this.receiver = receiver;
  1126. this.index = index;
  1127. this.value = value;
  1128. }
  1129. isEquivalent(e) {
  1130. return (e instanceof WriteKeyExpr &&
  1131. this.receiver.isEquivalent(e.receiver) &&
  1132. this.index.isEquivalent(e.index) &&
  1133. this.value.isEquivalent(e.value));
  1134. }
  1135. isConstant() {
  1136. return false;
  1137. }
  1138. visitExpression(visitor, context) {
  1139. return visitor.visitWriteKeyExpr(this, context);
  1140. }
  1141. clone() {
  1142. return new WriteKeyExpr(this.receiver.clone(), this.index.clone(), this.value.clone(), this.type, this.sourceSpan);
  1143. }
  1144. }
  1145. class WritePropExpr extends Expression {
  1146. receiver;
  1147. name;
  1148. value;
  1149. constructor(receiver, name, value, type, sourceSpan) {
  1150. super(type || value.type, sourceSpan);
  1151. this.receiver = receiver;
  1152. this.name = name;
  1153. this.value = value;
  1154. }
  1155. isEquivalent(e) {
  1156. return (e instanceof WritePropExpr &&
  1157. this.receiver.isEquivalent(e.receiver) &&
  1158. this.name === e.name &&
  1159. this.value.isEquivalent(e.value));
  1160. }
  1161. isConstant() {
  1162. return false;
  1163. }
  1164. visitExpression(visitor, context) {
  1165. return visitor.visitWritePropExpr(this, context);
  1166. }
  1167. clone() {
  1168. return new WritePropExpr(this.receiver.clone(), this.name, this.value.clone(), this.type, this.sourceSpan);
  1169. }
  1170. }
  1171. class InvokeFunctionExpr extends Expression {
  1172. fn;
  1173. args;
  1174. pure;
  1175. constructor(fn, args, type, sourceSpan, pure = false) {
  1176. super(type, sourceSpan);
  1177. this.fn = fn;
  1178. this.args = args;
  1179. this.pure = pure;
  1180. }
  1181. // An alias for fn, which allows other logic to handle calls and property reads together.
  1182. get receiver() {
  1183. return this.fn;
  1184. }
  1185. isEquivalent(e) {
  1186. return (e instanceof InvokeFunctionExpr &&
  1187. this.fn.isEquivalent(e.fn) &&
  1188. areAllEquivalent(this.args, e.args) &&
  1189. this.pure === e.pure);
  1190. }
  1191. isConstant() {
  1192. return false;
  1193. }
  1194. visitExpression(visitor, context) {
  1195. return visitor.visitInvokeFunctionExpr(this, context);
  1196. }
  1197. clone() {
  1198. return new InvokeFunctionExpr(this.fn.clone(), this.args.map((arg) => arg.clone()), this.type, this.sourceSpan, this.pure);
  1199. }
  1200. }
  1201. class TaggedTemplateLiteralExpr extends Expression {
  1202. tag;
  1203. template;
  1204. constructor(tag, template, type, sourceSpan) {
  1205. super(type, sourceSpan);
  1206. this.tag = tag;
  1207. this.template = template;
  1208. }
  1209. isEquivalent(e) {
  1210. return (e instanceof TaggedTemplateLiteralExpr &&
  1211. this.tag.isEquivalent(e.tag) &&
  1212. this.template.isEquivalent(e.template));
  1213. }
  1214. isConstant() {
  1215. return false;
  1216. }
  1217. visitExpression(visitor, context) {
  1218. return visitor.visitTaggedTemplateLiteralExpr(this, context);
  1219. }
  1220. clone() {
  1221. return new TaggedTemplateLiteralExpr(this.tag.clone(), this.template.clone(), this.type, this.sourceSpan);
  1222. }
  1223. }
  1224. class InstantiateExpr extends Expression {
  1225. classExpr;
  1226. args;
  1227. constructor(classExpr, args, type, sourceSpan) {
  1228. super(type, sourceSpan);
  1229. this.classExpr = classExpr;
  1230. this.args = args;
  1231. }
  1232. isEquivalent(e) {
  1233. return (e instanceof InstantiateExpr &&
  1234. this.classExpr.isEquivalent(e.classExpr) &&
  1235. areAllEquivalent(this.args, e.args));
  1236. }
  1237. isConstant() {
  1238. return false;
  1239. }
  1240. visitExpression(visitor, context) {
  1241. return visitor.visitInstantiateExpr(this, context);
  1242. }
  1243. clone() {
  1244. return new InstantiateExpr(this.classExpr.clone(), this.args.map((arg) => arg.clone()), this.type, this.sourceSpan);
  1245. }
  1246. }
  1247. class LiteralExpr extends Expression {
  1248. value;
  1249. constructor(value, type, sourceSpan) {
  1250. super(type, sourceSpan);
  1251. this.value = value;
  1252. }
  1253. isEquivalent(e) {
  1254. return e instanceof LiteralExpr && this.value === e.value;
  1255. }
  1256. isConstant() {
  1257. return true;
  1258. }
  1259. visitExpression(visitor, context) {
  1260. return visitor.visitLiteralExpr(this, context);
  1261. }
  1262. clone() {
  1263. return new LiteralExpr(this.value, this.type, this.sourceSpan);
  1264. }
  1265. }
  1266. class TemplateLiteralExpr extends Expression {
  1267. elements;
  1268. expressions;
  1269. constructor(elements, expressions, sourceSpan) {
  1270. super(null, sourceSpan);
  1271. this.elements = elements;
  1272. this.expressions = expressions;
  1273. }
  1274. isEquivalent(e) {
  1275. return (e instanceof TemplateLiteralExpr &&
  1276. areAllEquivalentPredicate(this.elements, e.elements, (a, b) => a.text === b.text) &&
  1277. areAllEquivalent(this.expressions, e.expressions));
  1278. }
  1279. isConstant() {
  1280. return false;
  1281. }
  1282. visitExpression(visitor, context) {
  1283. return visitor.visitTemplateLiteralExpr(this, context);
  1284. }
  1285. clone() {
  1286. return new TemplateLiteralExpr(this.elements.map((el) => el.clone()), this.expressions.map((expr) => expr.clone()));
  1287. }
  1288. }
  1289. class TemplateLiteralElementExpr extends Expression {
  1290. text;
  1291. rawText;
  1292. constructor(text, sourceSpan, rawText) {
  1293. super(STRING_TYPE, sourceSpan);
  1294. this.text = text;
  1295. // If `rawText` is not provided, try to extract the raw string from its
  1296. // associated `sourceSpan`. If that is also not available, "fake" the raw
  1297. // string instead by escaping the following control sequences:
  1298. // - "\" would otherwise indicate that the next character is a control character.
  1299. // - "`" and "${" are template string control sequences that would otherwise prematurely
  1300. // indicate the end of the template literal element.
  1301. this.rawText =
  1302. rawText ?? sourceSpan?.toString() ?? escapeForTemplateLiteral(escapeSlashes(text));
  1303. }
  1304. visitExpression(visitor, context) {
  1305. return visitor.visitTemplateLiteralElementExpr(this, context);
  1306. }
  1307. isEquivalent(e) {
  1308. return (e instanceof TemplateLiteralElementExpr && e.text === this.text && e.rawText === this.rawText);
  1309. }
  1310. isConstant() {
  1311. return true;
  1312. }
  1313. clone() {
  1314. return new TemplateLiteralElementExpr(this.text, this.sourceSpan, this.rawText);
  1315. }
  1316. }
  1317. class LiteralPiece {
  1318. text;
  1319. sourceSpan;
  1320. constructor(text, sourceSpan) {
  1321. this.text = text;
  1322. this.sourceSpan = sourceSpan;
  1323. }
  1324. }
  1325. class PlaceholderPiece {
  1326. text;
  1327. sourceSpan;
  1328. associatedMessage;
  1329. /**
  1330. * Create a new instance of a `PlaceholderPiece`.
  1331. *
  1332. * @param text the name of this placeholder (e.g. `PH_1`).
  1333. * @param sourceSpan the location of this placeholder in its localized message the source code.
  1334. * @param associatedMessage reference to another message that this placeholder is associated with.
  1335. * The `associatedMessage` is mainly used to provide a relationship to an ICU message that has
  1336. * been extracted out from the message containing the placeholder.
  1337. */
  1338. constructor(text, sourceSpan, associatedMessage) {
  1339. this.text = text;
  1340. this.sourceSpan = sourceSpan;
  1341. this.associatedMessage = associatedMessage;
  1342. }
  1343. }
  1344. const MEANING_SEPARATOR$1 = '|';
  1345. const ID_SEPARATOR$1 = '@@';
  1346. const LEGACY_ID_INDICATOR = '␟';
  1347. class LocalizedString extends Expression {
  1348. metaBlock;
  1349. messageParts;
  1350. placeHolderNames;
  1351. expressions;
  1352. constructor(metaBlock, messageParts, placeHolderNames, expressions, sourceSpan) {
  1353. super(STRING_TYPE, sourceSpan);
  1354. this.metaBlock = metaBlock;
  1355. this.messageParts = messageParts;
  1356. this.placeHolderNames = placeHolderNames;
  1357. this.expressions = expressions;
  1358. }
  1359. isEquivalent(e) {
  1360. // return e instanceof LocalizedString && this.message === e.message;
  1361. return false;
  1362. }
  1363. isConstant() {
  1364. return false;
  1365. }
  1366. visitExpression(visitor, context) {
  1367. return visitor.visitLocalizedString(this, context);
  1368. }
  1369. clone() {
  1370. return new LocalizedString(this.metaBlock, this.messageParts, this.placeHolderNames, this.expressions.map((expr) => expr.clone()), this.sourceSpan);
  1371. }
  1372. /**
  1373. * Serialize the given `meta` and `messagePart` into "cooked" and "raw" strings that can be used
  1374. * in a `$localize` tagged string. The format of the metadata is the same as that parsed by
  1375. * `parseI18nMeta()`.
  1376. *
  1377. * @param meta The metadata to serialize
  1378. * @param messagePart The first part of the tagged string
  1379. */
  1380. serializeI18nHead() {
  1381. let metaBlock = this.metaBlock.description || '';
  1382. if (this.metaBlock.meaning) {
  1383. metaBlock = `${this.metaBlock.meaning}${MEANING_SEPARATOR$1}${metaBlock}`;
  1384. }
  1385. if (this.metaBlock.customId) {
  1386. metaBlock = `${metaBlock}${ID_SEPARATOR$1}${this.metaBlock.customId}`;
  1387. }
  1388. if (this.metaBlock.legacyIds) {
  1389. this.metaBlock.legacyIds.forEach((legacyId) => {
  1390. metaBlock = `${metaBlock}${LEGACY_ID_INDICATOR}${legacyId}`;
  1391. });
  1392. }
  1393. return createCookedRawString(metaBlock, this.messageParts[0].text, this.getMessagePartSourceSpan(0));
  1394. }
  1395. getMessagePartSourceSpan(i) {
  1396. return this.messageParts[i]?.sourceSpan ?? this.sourceSpan;
  1397. }
  1398. getPlaceholderSourceSpan(i) {
  1399. return (this.placeHolderNames[i]?.sourceSpan ?? this.expressions[i]?.sourceSpan ?? this.sourceSpan);
  1400. }
  1401. /**
  1402. * Serialize the given `placeholderName` and `messagePart` into "cooked" and "raw" strings that
  1403. * can be used in a `$localize` tagged string.
  1404. *
  1405. * The format is `:<placeholder-name>[@@<associated-id>]:`.
  1406. *
  1407. * The `associated-id` is the message id of the (usually an ICU) message to which this placeholder
  1408. * refers.
  1409. *
  1410. * @param partIndex The index of the message part to serialize.
  1411. */
  1412. serializeI18nTemplatePart(partIndex) {
  1413. const placeholder = this.placeHolderNames[partIndex - 1];
  1414. const messagePart = this.messageParts[partIndex];
  1415. let metaBlock = placeholder.text;
  1416. if (placeholder.associatedMessage?.legacyIds.length === 0) {
  1417. metaBlock += `${ID_SEPARATOR$1}${computeMsgId(placeholder.associatedMessage.messageString, placeholder.associatedMessage.meaning)}`;
  1418. }
  1419. return createCookedRawString(metaBlock, messagePart.text, this.getMessagePartSourceSpan(partIndex));
  1420. }
  1421. }
  1422. const escapeSlashes = (str) => str.replace(/\\/g, '\\\\');
  1423. const escapeStartingColon = (str) => str.replace(/^:/, '\\:');
  1424. const escapeColons = (str) => str.replace(/:/g, '\\:');
  1425. const escapeForTemplateLiteral = (str) => str.replace(/`/g, '\\`').replace(/\${/g, '$\\{');
  1426. /**
  1427. * Creates a `{cooked, raw}` object from the `metaBlock` and `messagePart`.
  1428. *
  1429. * The `raw` text must have various character sequences escaped:
  1430. * * "\" would otherwise indicate that the next character is a control character.
  1431. * * "`" and "${" are template string control sequences that would otherwise prematurely indicate
  1432. * the end of a message part.
  1433. * * ":" inside a metablock would prematurely indicate the end of the metablock.
  1434. * * ":" at the start of a messagePart with no metablock would erroneously indicate the start of a
  1435. * metablock.
  1436. *
  1437. * @param metaBlock Any metadata that should be prepended to the string
  1438. * @param messagePart The message part of the string
  1439. */
  1440. function createCookedRawString(metaBlock, messagePart, range) {
  1441. if (metaBlock === '') {
  1442. return {
  1443. cooked: messagePart,
  1444. raw: escapeForTemplateLiteral(escapeStartingColon(escapeSlashes(messagePart))),
  1445. range,
  1446. };
  1447. }
  1448. else {
  1449. return {
  1450. cooked: `:${metaBlock}:${messagePart}`,
  1451. raw: escapeForTemplateLiteral(`:${escapeColons(escapeSlashes(metaBlock))}:${escapeSlashes(messagePart)}`),
  1452. range,
  1453. };
  1454. }
  1455. }
  1456. class ExternalExpr extends Expression {
  1457. value;
  1458. typeParams;
  1459. constructor(value, type, typeParams = null, sourceSpan) {
  1460. super(type, sourceSpan);
  1461. this.value = value;
  1462. this.typeParams = typeParams;
  1463. }
  1464. isEquivalent(e) {
  1465. return (e instanceof ExternalExpr &&
  1466. this.value.name === e.value.name &&
  1467. this.value.moduleName === e.value.moduleName);
  1468. }
  1469. isConstant() {
  1470. return false;
  1471. }
  1472. visitExpression(visitor, context) {
  1473. return visitor.visitExternalExpr(this, context);
  1474. }
  1475. clone() {
  1476. return new ExternalExpr(this.value, this.type, this.typeParams, this.sourceSpan);
  1477. }
  1478. }
  1479. class ExternalReference {
  1480. moduleName;
  1481. name;
  1482. constructor(moduleName, name) {
  1483. this.moduleName = moduleName;
  1484. this.name = name;
  1485. }
  1486. }
  1487. class ConditionalExpr extends Expression {
  1488. condition;
  1489. falseCase;
  1490. trueCase;
  1491. constructor(condition, trueCase, falseCase = null, type, sourceSpan) {
  1492. super(type || trueCase.type, sourceSpan);
  1493. this.condition = condition;
  1494. this.falseCase = falseCase;
  1495. this.trueCase = trueCase;
  1496. }
  1497. isEquivalent(e) {
  1498. return (e instanceof ConditionalExpr &&
  1499. this.condition.isEquivalent(e.condition) &&
  1500. this.trueCase.isEquivalent(e.trueCase) &&
  1501. nullSafeIsEquivalent(this.falseCase, e.falseCase));
  1502. }
  1503. isConstant() {
  1504. return false;
  1505. }
  1506. visitExpression(visitor, context) {
  1507. return visitor.visitConditionalExpr(this, context);
  1508. }
  1509. clone() {
  1510. return new ConditionalExpr(this.condition.clone(), this.trueCase.clone(), this.falseCase?.clone(), this.type, this.sourceSpan);
  1511. }
  1512. }
  1513. class DynamicImportExpr extends Expression {
  1514. url;
  1515. urlComment;
  1516. constructor(url, sourceSpan, urlComment) {
  1517. super(null, sourceSpan);
  1518. this.url = url;
  1519. this.urlComment = urlComment;
  1520. }
  1521. isEquivalent(e) {
  1522. return e instanceof DynamicImportExpr && this.url === e.url && this.urlComment === e.urlComment;
  1523. }
  1524. isConstant() {
  1525. return false;
  1526. }
  1527. visitExpression(visitor, context) {
  1528. return visitor.visitDynamicImportExpr(this, context);
  1529. }
  1530. clone() {
  1531. return new DynamicImportExpr(typeof this.url === 'string' ? this.url : this.url.clone(), this.sourceSpan, this.urlComment);
  1532. }
  1533. }
  1534. class NotExpr extends Expression {
  1535. condition;
  1536. constructor(condition, sourceSpan) {
  1537. super(BOOL_TYPE, sourceSpan);
  1538. this.condition = condition;
  1539. }
  1540. isEquivalent(e) {
  1541. return e instanceof NotExpr && this.condition.isEquivalent(e.condition);
  1542. }
  1543. isConstant() {
  1544. return false;
  1545. }
  1546. visitExpression(visitor, context) {
  1547. return visitor.visitNotExpr(this, context);
  1548. }
  1549. clone() {
  1550. return new NotExpr(this.condition.clone(), this.sourceSpan);
  1551. }
  1552. }
  1553. class FnParam {
  1554. name;
  1555. type;
  1556. constructor(name, type = null) {
  1557. this.name = name;
  1558. this.type = type;
  1559. }
  1560. isEquivalent(param) {
  1561. return this.name === param.name;
  1562. }
  1563. clone() {
  1564. return new FnParam(this.name, this.type);
  1565. }
  1566. }
  1567. class FunctionExpr extends Expression {
  1568. params;
  1569. statements;
  1570. name;
  1571. constructor(params, statements, type, sourceSpan, name) {
  1572. super(type, sourceSpan);
  1573. this.params = params;
  1574. this.statements = statements;
  1575. this.name = name;
  1576. }
  1577. isEquivalent(e) {
  1578. return ((e instanceof FunctionExpr || e instanceof DeclareFunctionStmt) &&
  1579. areAllEquivalent(this.params, e.params) &&
  1580. areAllEquivalent(this.statements, e.statements));
  1581. }
  1582. isConstant() {
  1583. return false;
  1584. }
  1585. visitExpression(visitor, context) {
  1586. return visitor.visitFunctionExpr(this, context);
  1587. }
  1588. toDeclStmt(name, modifiers) {
  1589. return new DeclareFunctionStmt(name, this.params, this.statements, this.type, modifiers, this.sourceSpan);
  1590. }
  1591. clone() {
  1592. // TODO: Should we deep clone statements?
  1593. return new FunctionExpr(this.params.map((p) => p.clone()), this.statements, this.type, this.sourceSpan, this.name);
  1594. }
  1595. }
  1596. class ArrowFunctionExpr extends Expression {
  1597. params;
  1598. body;
  1599. // Note that `body: Expression` represents `() => expr` whereas
  1600. // `body: Statement[]` represents `() => { expr }`.
  1601. constructor(params, body, type, sourceSpan) {
  1602. super(type, sourceSpan);
  1603. this.params = params;
  1604. this.body = body;
  1605. }
  1606. isEquivalent(e) {
  1607. if (!(e instanceof ArrowFunctionExpr) || !areAllEquivalent(this.params, e.params)) {
  1608. return false;
  1609. }
  1610. if (this.body instanceof Expression && e.body instanceof Expression) {
  1611. return this.body.isEquivalent(e.body);
  1612. }
  1613. if (Array.isArray(this.body) && Array.isArray(e.body)) {
  1614. return areAllEquivalent(this.body, e.body);
  1615. }
  1616. return false;
  1617. }
  1618. isConstant() {
  1619. return false;
  1620. }
  1621. visitExpression(visitor, context) {
  1622. return visitor.visitArrowFunctionExpr(this, context);
  1623. }
  1624. clone() {
  1625. // TODO: Should we deep clone statements?
  1626. return new ArrowFunctionExpr(this.params.map((p) => p.clone()), Array.isArray(this.body) ? this.body : this.body.clone(), this.type, this.sourceSpan);
  1627. }
  1628. toDeclStmt(name, modifiers) {
  1629. return new DeclareVarStmt(name, this, INFERRED_TYPE, modifiers, this.sourceSpan);
  1630. }
  1631. }
  1632. class UnaryOperatorExpr extends Expression {
  1633. operator;
  1634. expr;
  1635. parens;
  1636. constructor(operator, expr, type, sourceSpan, parens = true) {
  1637. super(type || NUMBER_TYPE, sourceSpan);
  1638. this.operator = operator;
  1639. this.expr = expr;
  1640. this.parens = parens;
  1641. }
  1642. isEquivalent(e) {
  1643. return (e instanceof UnaryOperatorExpr &&
  1644. this.operator === e.operator &&
  1645. this.expr.isEquivalent(e.expr));
  1646. }
  1647. isConstant() {
  1648. return false;
  1649. }
  1650. visitExpression(visitor, context) {
  1651. return visitor.visitUnaryOperatorExpr(this, context);
  1652. }
  1653. clone() {
  1654. return new UnaryOperatorExpr(this.operator, this.expr.clone(), this.type, this.sourceSpan, this.parens);
  1655. }
  1656. }
  1657. class BinaryOperatorExpr extends Expression {
  1658. operator;
  1659. rhs;
  1660. parens;
  1661. lhs;
  1662. constructor(operator, lhs, rhs, type, sourceSpan, parens = true) {
  1663. super(type || lhs.type, sourceSpan);
  1664. this.operator = operator;
  1665. this.rhs = rhs;
  1666. this.parens = parens;
  1667. this.lhs = lhs;
  1668. }
  1669. isEquivalent(e) {
  1670. return (e instanceof BinaryOperatorExpr &&
  1671. this.operator === e.operator &&
  1672. this.lhs.isEquivalent(e.lhs) &&
  1673. this.rhs.isEquivalent(e.rhs));
  1674. }
  1675. isConstant() {
  1676. return false;
  1677. }
  1678. visitExpression(visitor, context) {
  1679. return visitor.visitBinaryOperatorExpr(this, context);
  1680. }
  1681. clone() {
  1682. return new BinaryOperatorExpr(this.operator, this.lhs.clone(), this.rhs.clone(), this.type, this.sourceSpan, this.parens);
  1683. }
  1684. }
  1685. class ReadPropExpr extends Expression {
  1686. receiver;
  1687. name;
  1688. constructor(receiver, name, type, sourceSpan) {
  1689. super(type, sourceSpan);
  1690. this.receiver = receiver;
  1691. this.name = name;
  1692. }
  1693. // An alias for name, which allows other logic to handle property reads and keyed reads together.
  1694. get index() {
  1695. return this.name;
  1696. }
  1697. isEquivalent(e) {
  1698. return (e instanceof ReadPropExpr && this.receiver.isEquivalent(e.receiver) && this.name === e.name);
  1699. }
  1700. isConstant() {
  1701. return false;
  1702. }
  1703. visitExpression(visitor, context) {
  1704. return visitor.visitReadPropExpr(this, context);
  1705. }
  1706. set(value) {
  1707. return new WritePropExpr(this.receiver, this.name, value, null, this.sourceSpan);
  1708. }
  1709. clone() {
  1710. return new ReadPropExpr(this.receiver.clone(), this.name, this.type, this.sourceSpan);
  1711. }
  1712. }
  1713. class ReadKeyExpr extends Expression {
  1714. receiver;
  1715. index;
  1716. constructor(receiver, index, type, sourceSpan) {
  1717. super(type, sourceSpan);
  1718. this.receiver = receiver;
  1719. this.index = index;
  1720. }
  1721. isEquivalent(e) {
  1722. return (e instanceof ReadKeyExpr &&
  1723. this.receiver.isEquivalent(e.receiver) &&
  1724. this.index.isEquivalent(e.index));
  1725. }
  1726. isConstant() {
  1727. return false;
  1728. }
  1729. visitExpression(visitor, context) {
  1730. return visitor.visitReadKeyExpr(this, context);
  1731. }
  1732. set(value) {
  1733. return new WriteKeyExpr(this.receiver, this.index, value, null, this.sourceSpan);
  1734. }
  1735. clone() {
  1736. return new ReadKeyExpr(this.receiver.clone(), this.index.clone(), this.type, this.sourceSpan);
  1737. }
  1738. }
  1739. class LiteralArrayExpr extends Expression {
  1740. entries;
  1741. constructor(entries, type, sourceSpan) {
  1742. super(type, sourceSpan);
  1743. this.entries = entries;
  1744. }
  1745. isConstant() {
  1746. return this.entries.every((e) => e.isConstant());
  1747. }
  1748. isEquivalent(e) {
  1749. return e instanceof LiteralArrayExpr && areAllEquivalent(this.entries, e.entries);
  1750. }
  1751. visitExpression(visitor, context) {
  1752. return visitor.visitLiteralArrayExpr(this, context);
  1753. }
  1754. clone() {
  1755. return new LiteralArrayExpr(this.entries.map((e) => e.clone()), this.type, this.sourceSpan);
  1756. }
  1757. }
  1758. class LiteralMapEntry {
  1759. key;
  1760. value;
  1761. quoted;
  1762. constructor(key, value, quoted) {
  1763. this.key = key;
  1764. this.value = value;
  1765. this.quoted = quoted;
  1766. }
  1767. isEquivalent(e) {
  1768. return this.key === e.key && this.value.isEquivalent(e.value);
  1769. }
  1770. clone() {
  1771. return new LiteralMapEntry(this.key, this.value.clone(), this.quoted);
  1772. }
  1773. }
  1774. class LiteralMapExpr extends Expression {
  1775. entries;
  1776. valueType = null;
  1777. constructor(entries, type, sourceSpan) {
  1778. super(type, sourceSpan);
  1779. this.entries = entries;
  1780. if (type) {
  1781. this.valueType = type.valueType;
  1782. }
  1783. }
  1784. isEquivalent(e) {
  1785. return e instanceof LiteralMapExpr && areAllEquivalent(this.entries, e.entries);
  1786. }
  1787. isConstant() {
  1788. return this.entries.every((e) => e.value.isConstant());
  1789. }
  1790. visitExpression(visitor, context) {
  1791. return visitor.visitLiteralMapExpr(this, context);
  1792. }
  1793. clone() {
  1794. const entriesClone = this.entries.map((entry) => entry.clone());
  1795. return new LiteralMapExpr(entriesClone, this.type, this.sourceSpan);
  1796. }
  1797. }
  1798. class CommaExpr extends Expression {
  1799. parts;
  1800. constructor(parts, sourceSpan) {
  1801. super(parts[parts.length - 1].type, sourceSpan);
  1802. this.parts = parts;
  1803. }
  1804. isEquivalent(e) {
  1805. return e instanceof CommaExpr && areAllEquivalent(this.parts, e.parts);
  1806. }
  1807. isConstant() {
  1808. return false;
  1809. }
  1810. visitExpression(visitor, context) {
  1811. return visitor.visitCommaExpr(this, context);
  1812. }
  1813. clone() {
  1814. return new CommaExpr(this.parts.map((p) => p.clone()));
  1815. }
  1816. }
  1817. const NULL_EXPR = new LiteralExpr(null, null, null);
  1818. const TYPED_NULL_EXPR = new LiteralExpr(null, INFERRED_TYPE, null);
  1819. //// Statements
  1820. var StmtModifier;
  1821. (function (StmtModifier) {
  1822. StmtModifier[StmtModifier["None"] = 0] = "None";
  1823. StmtModifier[StmtModifier["Final"] = 1] = "Final";
  1824. StmtModifier[StmtModifier["Private"] = 2] = "Private";
  1825. StmtModifier[StmtModifier["Exported"] = 4] = "Exported";
  1826. StmtModifier[StmtModifier["Static"] = 8] = "Static";
  1827. })(StmtModifier || (StmtModifier = {}));
  1828. class LeadingComment {
  1829. text;
  1830. multiline;
  1831. trailingNewline;
  1832. constructor(text, multiline, trailingNewline) {
  1833. this.text = text;
  1834. this.multiline = multiline;
  1835. this.trailingNewline = trailingNewline;
  1836. }
  1837. toString() {
  1838. return this.multiline ? ` ${this.text} ` : this.text;
  1839. }
  1840. }
  1841. class JSDocComment extends LeadingComment {
  1842. tags;
  1843. constructor(tags) {
  1844. super('', /* multiline */ true, /* trailingNewline */ true);
  1845. this.tags = tags;
  1846. }
  1847. toString() {
  1848. return serializeTags(this.tags);
  1849. }
  1850. }
  1851. class Statement {
  1852. modifiers;
  1853. sourceSpan;
  1854. leadingComments;
  1855. constructor(modifiers = StmtModifier.None, sourceSpan = null, leadingComments) {
  1856. this.modifiers = modifiers;
  1857. this.sourceSpan = sourceSpan;
  1858. this.leadingComments = leadingComments;
  1859. }
  1860. hasModifier(modifier) {
  1861. return (this.modifiers & modifier) !== 0;
  1862. }
  1863. addLeadingComment(leadingComment) {
  1864. this.leadingComments = this.leadingComments ?? [];
  1865. this.leadingComments.push(leadingComment);
  1866. }
  1867. }
  1868. class DeclareVarStmt extends Statement {
  1869. name;
  1870. value;
  1871. type;
  1872. constructor(name, value, type, modifiers, sourceSpan, leadingComments) {
  1873. super(modifiers, sourceSpan, leadingComments);
  1874. this.name = name;
  1875. this.value = value;
  1876. this.type = type || (value && value.type) || null;
  1877. }
  1878. isEquivalent(stmt) {
  1879. return (stmt instanceof DeclareVarStmt &&
  1880. this.name === stmt.name &&
  1881. (this.value ? !!stmt.value && this.value.isEquivalent(stmt.value) : !stmt.value));
  1882. }
  1883. visitStatement(visitor, context) {
  1884. return visitor.visitDeclareVarStmt(this, context);
  1885. }
  1886. }
  1887. class DeclareFunctionStmt extends Statement {
  1888. name;
  1889. params;
  1890. statements;
  1891. type;
  1892. constructor(name, params, statements, type, modifiers, sourceSpan, leadingComments) {
  1893. super(modifiers, sourceSpan, leadingComments);
  1894. this.name = name;
  1895. this.params = params;
  1896. this.statements = statements;
  1897. this.type = type || null;
  1898. }
  1899. isEquivalent(stmt) {
  1900. return (stmt instanceof DeclareFunctionStmt &&
  1901. areAllEquivalent(this.params, stmt.params) &&
  1902. areAllEquivalent(this.statements, stmt.statements));
  1903. }
  1904. visitStatement(visitor, context) {
  1905. return visitor.visitDeclareFunctionStmt(this, context);
  1906. }
  1907. }
  1908. class ExpressionStatement extends Statement {
  1909. expr;
  1910. constructor(expr, sourceSpan, leadingComments) {
  1911. super(StmtModifier.None, sourceSpan, leadingComments);
  1912. this.expr = expr;
  1913. }
  1914. isEquivalent(stmt) {
  1915. return stmt instanceof ExpressionStatement && this.expr.isEquivalent(stmt.expr);
  1916. }
  1917. visitStatement(visitor, context) {
  1918. return visitor.visitExpressionStmt(this, context);
  1919. }
  1920. }
  1921. class ReturnStatement extends Statement {
  1922. value;
  1923. constructor(value, sourceSpan = null, leadingComments) {
  1924. super(StmtModifier.None, sourceSpan, leadingComments);
  1925. this.value = value;
  1926. }
  1927. isEquivalent(stmt) {
  1928. return stmt instanceof ReturnStatement && this.value.isEquivalent(stmt.value);
  1929. }
  1930. visitStatement(visitor, context) {
  1931. return visitor.visitReturnStmt(this, context);
  1932. }
  1933. }
  1934. class IfStmt extends Statement {
  1935. condition;
  1936. trueCase;
  1937. falseCase;
  1938. constructor(condition, trueCase, falseCase = [], sourceSpan, leadingComments) {
  1939. super(StmtModifier.None, sourceSpan, leadingComments);
  1940. this.condition = condition;
  1941. this.trueCase = trueCase;
  1942. this.falseCase = falseCase;
  1943. }
  1944. isEquivalent(stmt) {
  1945. return (stmt instanceof IfStmt &&
  1946. this.condition.isEquivalent(stmt.condition) &&
  1947. areAllEquivalent(this.trueCase, stmt.trueCase) &&
  1948. areAllEquivalent(this.falseCase, stmt.falseCase));
  1949. }
  1950. visitStatement(visitor, context) {
  1951. return visitor.visitIfStmt(this, context);
  1952. }
  1953. }
  1954. let RecursiveAstVisitor$1 = class RecursiveAstVisitor {
  1955. visitType(ast, context) {
  1956. return ast;
  1957. }
  1958. visitExpression(ast, context) {
  1959. if (ast.type) {
  1960. ast.type.visitType(this, context);
  1961. }
  1962. return ast;
  1963. }
  1964. visitBuiltinType(type, context) {
  1965. return this.visitType(type, context);
  1966. }
  1967. visitExpressionType(type, context) {
  1968. type.value.visitExpression(this, context);
  1969. if (type.typeParams !== null) {
  1970. type.typeParams.forEach((param) => this.visitType(param, context));
  1971. }
  1972. return this.visitType(type, context);
  1973. }
  1974. visitArrayType(type, context) {
  1975. return this.visitType(type, context);
  1976. }
  1977. visitMapType(type, context) {
  1978. return this.visitType(type, context);
  1979. }
  1980. visitTransplantedType(type, context) {
  1981. return type;
  1982. }
  1983. visitWrappedNodeExpr(ast, context) {
  1984. return ast;
  1985. }
  1986. visitTypeofExpr(ast, context) {
  1987. return this.visitExpression(ast, context);
  1988. }
  1989. visitReadVarExpr(ast, context) {
  1990. return this.visitExpression(ast, context);
  1991. }
  1992. visitWriteVarExpr(ast, context) {
  1993. ast.value.visitExpression(this, context);
  1994. return this.visitExpression(ast, context);
  1995. }
  1996. visitWriteKeyExpr(ast, context) {
  1997. ast.receiver.visitExpression(this, context);
  1998. ast.index.visitExpression(this, context);
  1999. ast.value.visitExpression(this, context);
  2000. return this.visitExpression(ast, context);
  2001. }
  2002. visitWritePropExpr(ast, context) {
  2003. ast.receiver.visitExpression(this, context);
  2004. ast.value.visitExpression(this, context);
  2005. return this.visitExpression(ast, context);
  2006. }
  2007. visitDynamicImportExpr(ast, context) {
  2008. return this.visitExpression(ast, context);
  2009. }
  2010. visitInvokeFunctionExpr(ast, context) {
  2011. ast.fn.visitExpression(this, context);
  2012. this.visitAllExpressions(ast.args, context);
  2013. return this.visitExpression(ast, context);
  2014. }
  2015. visitTaggedTemplateLiteralExpr(ast, context) {
  2016. ast.tag.visitExpression(this, context);
  2017. ast.template.visitExpression(this, context);
  2018. return this.visitExpression(ast, context);
  2019. }
  2020. visitInstantiateExpr(ast, context) {
  2021. ast.classExpr.visitExpression(this, context);
  2022. this.visitAllExpressions(ast.args, context);
  2023. return this.visitExpression(ast, context);
  2024. }
  2025. visitLiteralExpr(ast, context) {
  2026. return this.visitExpression(ast, context);
  2027. }
  2028. visitLocalizedString(ast, context) {
  2029. return this.visitExpression(ast, context);
  2030. }
  2031. visitExternalExpr(ast, context) {
  2032. if (ast.typeParams) {
  2033. ast.typeParams.forEach((type) => type.visitType(this, context));
  2034. }
  2035. return this.visitExpression(ast, context);
  2036. }
  2037. visitConditionalExpr(ast, context) {
  2038. ast.condition.visitExpression(this, context);
  2039. ast.trueCase.visitExpression(this, context);
  2040. ast.falseCase.visitExpression(this, context);
  2041. return this.visitExpression(ast, context);
  2042. }
  2043. visitNotExpr(ast, context) {
  2044. ast.condition.visitExpression(this, context);
  2045. return this.visitExpression(ast, context);
  2046. }
  2047. visitFunctionExpr(ast, context) {
  2048. this.visitAllStatements(ast.statements, context);
  2049. return this.visitExpression(ast, context);
  2050. }
  2051. visitArrowFunctionExpr(ast, context) {
  2052. if (Array.isArray(ast.body)) {
  2053. this.visitAllStatements(ast.body, context);
  2054. }
  2055. else {
  2056. // Note: `body.visitExpression`, rather than `this.visitExpressiont(body)`,
  2057. // because the latter won't recurse into the sub-expressions.
  2058. ast.body.visitExpression(this, context);
  2059. }
  2060. return this.visitExpression(ast, context);
  2061. }
  2062. visitUnaryOperatorExpr(ast, context) {
  2063. ast.expr.visitExpression(this, context);
  2064. return this.visitExpression(ast, context);
  2065. }
  2066. visitBinaryOperatorExpr(ast, context) {
  2067. ast.lhs.visitExpression(this, context);
  2068. ast.rhs.visitExpression(this, context);
  2069. return this.visitExpression(ast, context);
  2070. }
  2071. visitReadPropExpr(ast, context) {
  2072. ast.receiver.visitExpression(this, context);
  2073. return this.visitExpression(ast, context);
  2074. }
  2075. visitReadKeyExpr(ast, context) {
  2076. ast.receiver.visitExpression(this, context);
  2077. ast.index.visitExpression(this, context);
  2078. return this.visitExpression(ast, context);
  2079. }
  2080. visitLiteralArrayExpr(ast, context) {
  2081. this.visitAllExpressions(ast.entries, context);
  2082. return this.visitExpression(ast, context);
  2083. }
  2084. visitLiteralMapExpr(ast, context) {
  2085. ast.entries.forEach((entry) => entry.value.visitExpression(this, context));
  2086. return this.visitExpression(ast, context);
  2087. }
  2088. visitCommaExpr(ast, context) {
  2089. this.visitAllExpressions(ast.parts, context);
  2090. return this.visitExpression(ast, context);
  2091. }
  2092. visitTemplateLiteralExpr(ast, context) {
  2093. this.visitAllExpressions(ast.elements, context);
  2094. this.visitAllExpressions(ast.expressions, context);
  2095. return this.visitExpression(ast, context);
  2096. }
  2097. visitTemplateLiteralElementExpr(ast, context) {
  2098. return this.visitExpression(ast, context);
  2099. }
  2100. visitAllExpressions(exprs, context) {
  2101. exprs.forEach((expr) => expr.visitExpression(this, context));
  2102. }
  2103. visitDeclareVarStmt(stmt, context) {
  2104. if (stmt.value) {
  2105. stmt.value.visitExpression(this, context);
  2106. }
  2107. if (stmt.type) {
  2108. stmt.type.visitType(this, context);
  2109. }
  2110. return stmt;
  2111. }
  2112. visitDeclareFunctionStmt(stmt, context) {
  2113. this.visitAllStatements(stmt.statements, context);
  2114. if (stmt.type) {
  2115. stmt.type.visitType(this, context);
  2116. }
  2117. return stmt;
  2118. }
  2119. visitExpressionStmt(stmt, context) {
  2120. stmt.expr.visitExpression(this, context);
  2121. return stmt;
  2122. }
  2123. visitReturnStmt(stmt, context) {
  2124. stmt.value.visitExpression(this, context);
  2125. return stmt;
  2126. }
  2127. visitIfStmt(stmt, context) {
  2128. stmt.condition.visitExpression(this, context);
  2129. this.visitAllStatements(stmt.trueCase, context);
  2130. this.visitAllStatements(stmt.falseCase, context);
  2131. return stmt;
  2132. }
  2133. visitAllStatements(stmts, context) {
  2134. stmts.forEach((stmt) => stmt.visitStatement(this, context));
  2135. }
  2136. };
  2137. function leadingComment(text, multiline = false, trailingNewline = true) {
  2138. return new LeadingComment(text, multiline, trailingNewline);
  2139. }
  2140. function jsDocComment(tags = []) {
  2141. return new JSDocComment(tags);
  2142. }
  2143. function variable(name, type, sourceSpan) {
  2144. return new ReadVarExpr(name, type, sourceSpan);
  2145. }
  2146. function importExpr(id, typeParams = null, sourceSpan) {
  2147. return new ExternalExpr(id, null, typeParams, sourceSpan);
  2148. }
  2149. function importType(id, typeParams, typeModifiers) {
  2150. return id != null ? expressionType(importExpr(id, typeParams, null), typeModifiers) : null;
  2151. }
  2152. function expressionType(expr, typeModifiers, typeParams) {
  2153. return new ExpressionType(expr, typeModifiers, typeParams);
  2154. }
  2155. function transplantedType(type, typeModifiers) {
  2156. return new TransplantedType(type, typeModifiers);
  2157. }
  2158. function typeofExpr(expr) {
  2159. return new TypeofExpr(expr);
  2160. }
  2161. function literalArr(values, type, sourceSpan) {
  2162. return new LiteralArrayExpr(values, type, sourceSpan);
  2163. }
  2164. function literalMap(values, type = null) {
  2165. return new LiteralMapExpr(values.map((e) => new LiteralMapEntry(e.key, e.value, e.quoted)), type, null);
  2166. }
  2167. function unary(operator, expr, type, sourceSpan) {
  2168. return new UnaryOperatorExpr(operator, expr, type, sourceSpan);
  2169. }
  2170. function not(expr, sourceSpan) {
  2171. return new NotExpr(expr, sourceSpan);
  2172. }
  2173. function fn(params, body, type, sourceSpan, name) {
  2174. return new FunctionExpr(params, body, type, sourceSpan, name);
  2175. }
  2176. function arrowFn(params, body, type, sourceSpan) {
  2177. return new ArrowFunctionExpr(params, body, type, sourceSpan);
  2178. }
  2179. function ifStmt(condition, thenClause, elseClause, sourceSpan, leadingComments) {
  2180. return new IfStmt(condition, thenClause, elseClause, sourceSpan, leadingComments);
  2181. }
  2182. function taggedTemplate(tag, template, type, sourceSpan) {
  2183. return new TaggedTemplateLiteralExpr(tag, template, type, sourceSpan);
  2184. }
  2185. function literal(value, type, sourceSpan) {
  2186. return new LiteralExpr(value, type, sourceSpan);
  2187. }
  2188. function localizedString(metaBlock, messageParts, placeholderNames, expressions, sourceSpan) {
  2189. return new LocalizedString(metaBlock, messageParts, placeholderNames, expressions, sourceSpan);
  2190. }
  2191. function isNull(exp) {
  2192. return exp instanceof LiteralExpr && exp.value === null;
  2193. }
  2194. /*
  2195. * Serializes a `Tag` into a string.
  2196. * Returns a string like " @foo {bar} baz" (note the leading whitespace before `@foo`).
  2197. */
  2198. function tagToString(tag) {
  2199. let out = '';
  2200. if (tag.tagName) {
  2201. out += ` @${tag.tagName}`;
  2202. }
  2203. if (tag.text) {
  2204. if (tag.text.match(/\/\*|\*\//)) {
  2205. throw new Error('JSDoc text cannot contain "/*" and "*/"');
  2206. }
  2207. out += ' ' + tag.text.replace(/@/g, '\\@');
  2208. }
  2209. return out;
  2210. }
  2211. function serializeTags(tags) {
  2212. if (tags.length === 0)
  2213. return '';
  2214. if (tags.length === 1 && tags[0].tagName && !tags[0].text) {
  2215. // The JSDOC comment is a single simple tag: e.g `/** @tagname */`.
  2216. return `*${tagToString(tags[0])} `;
  2217. }
  2218. let out = '*\n';
  2219. for (const tag of tags) {
  2220. out += ' *';
  2221. // If the tagToString is multi-line, insert " * " prefixes on lines.
  2222. out += tagToString(tag).replace(/\n/g, '\n * ');
  2223. out += '\n';
  2224. }
  2225. out += ' ';
  2226. return out;
  2227. }
  2228. var output_ast = /*#__PURE__*/Object.freeze({
  2229. __proto__: null,
  2230. ArrayType: ArrayType,
  2231. ArrowFunctionExpr: ArrowFunctionExpr,
  2232. BOOL_TYPE: BOOL_TYPE,
  2233. get BinaryOperator () { return BinaryOperator; },
  2234. BinaryOperatorExpr: BinaryOperatorExpr,
  2235. BuiltinType: BuiltinType,
  2236. get BuiltinTypeName () { return BuiltinTypeName; },
  2237. CommaExpr: CommaExpr,
  2238. ConditionalExpr: ConditionalExpr,
  2239. DYNAMIC_TYPE: DYNAMIC_TYPE,
  2240. DeclareFunctionStmt: DeclareFunctionStmt,
  2241. DeclareVarStmt: DeclareVarStmt,
  2242. DynamicImportExpr: DynamicImportExpr,
  2243. Expression: Expression,
  2244. ExpressionStatement: ExpressionStatement,
  2245. ExpressionType: ExpressionType,
  2246. ExternalExpr: ExternalExpr,
  2247. ExternalReference: ExternalReference,
  2248. FUNCTION_TYPE: FUNCTION_TYPE,
  2249. FnParam: FnParam,
  2250. FunctionExpr: FunctionExpr,
  2251. INFERRED_TYPE: INFERRED_TYPE,
  2252. INT_TYPE: INT_TYPE,
  2253. IfStmt: IfStmt,
  2254. InstantiateExpr: InstantiateExpr,
  2255. InvokeFunctionExpr: InvokeFunctionExpr,
  2256. JSDocComment: JSDocComment,
  2257. LeadingComment: LeadingComment,
  2258. LiteralArrayExpr: LiteralArrayExpr,
  2259. LiteralExpr: LiteralExpr,
  2260. LiteralMapEntry: LiteralMapEntry,
  2261. LiteralMapExpr: LiteralMapExpr,
  2262. LiteralPiece: LiteralPiece,
  2263. LocalizedString: LocalizedString,
  2264. MapType: MapType,
  2265. NONE_TYPE: NONE_TYPE,
  2266. NULL_EXPR: NULL_EXPR,
  2267. NUMBER_TYPE: NUMBER_TYPE,
  2268. NotExpr: NotExpr,
  2269. PlaceholderPiece: PlaceholderPiece,
  2270. ReadKeyExpr: ReadKeyExpr,
  2271. ReadPropExpr: ReadPropExpr,
  2272. ReadVarExpr: ReadVarExpr,
  2273. RecursiveAstVisitor: RecursiveAstVisitor$1,
  2274. ReturnStatement: ReturnStatement,
  2275. STRING_TYPE: STRING_TYPE,
  2276. Statement: Statement,
  2277. get StmtModifier () { return StmtModifier; },
  2278. TYPED_NULL_EXPR: TYPED_NULL_EXPR,
  2279. TaggedTemplateLiteralExpr: TaggedTemplateLiteralExpr,
  2280. TemplateLiteralElementExpr: TemplateLiteralElementExpr,
  2281. TemplateLiteralExpr: TemplateLiteralExpr,
  2282. TransplantedType: TransplantedType,
  2283. Type: Type,
  2284. get TypeModifier () { return TypeModifier; },
  2285. TypeofExpr: TypeofExpr,
  2286. get UnaryOperator () { return UnaryOperator; },
  2287. UnaryOperatorExpr: UnaryOperatorExpr,
  2288. WrappedNodeExpr: WrappedNodeExpr,
  2289. WriteKeyExpr: WriteKeyExpr,
  2290. WritePropExpr: WritePropExpr,
  2291. WriteVarExpr: WriteVarExpr,
  2292. areAllEquivalent: areAllEquivalent,
  2293. arrowFn: arrowFn,
  2294. expressionType: expressionType,
  2295. fn: fn,
  2296. ifStmt: ifStmt,
  2297. importExpr: importExpr,
  2298. importType: importType,
  2299. isNull: isNull,
  2300. jsDocComment: jsDocComment,
  2301. leadingComment: leadingComment,
  2302. literal: literal,
  2303. literalArr: literalArr,
  2304. literalMap: literalMap,
  2305. localizedString: localizedString,
  2306. not: not,
  2307. nullSafeIsEquivalent: nullSafeIsEquivalent,
  2308. taggedTemplate: taggedTemplate,
  2309. transplantedType: transplantedType,
  2310. typeofExpr: typeofExpr,
  2311. unary: unary,
  2312. variable: variable
  2313. });
  2314. const CONSTANT_PREFIX = '_c';
  2315. /**
  2316. * `ConstantPool` tries to reuse literal factories when two or more literals are identical.
  2317. * We determine whether literals are identical by creating a key out of their AST using the
  2318. * `KeyVisitor`. This constant is used to replace dynamic expressions which can't be safely
  2319. * converted into a key. E.g. given an expression `{foo: bar()}`, since we don't know what
  2320. * the result of `bar` will be, we create a key that looks like `{foo: <unknown>}`. Note
  2321. * that we use a variable, rather than something like `null` in order to avoid collisions.
  2322. */
  2323. const UNKNOWN_VALUE_KEY = variable('<unknown>');
  2324. /**
  2325. * Context to use when producing a key.
  2326. *
  2327. * This ensures we see the constant not the reference variable when producing
  2328. * a key.
  2329. */
  2330. const KEY_CONTEXT = {};
  2331. /**
  2332. * Generally all primitive values are excluded from the `ConstantPool`, but there is an exclusion
  2333. * for strings that reach a certain length threshold. This constant defines the length threshold for
  2334. * strings.
  2335. */
  2336. const POOL_INCLUSION_LENGTH_THRESHOLD_FOR_STRINGS = 50;
  2337. /**
  2338. * A node that is a place-holder that allows the node to be replaced when the actual
  2339. * node is known.
  2340. *
  2341. * This allows the constant pool to change an expression from a direct reference to
  2342. * a constant to a shared constant. It returns a fix-up node that is later allowed to
  2343. * change the referenced expression.
  2344. */
  2345. class FixupExpression extends Expression {
  2346. resolved;
  2347. original;
  2348. shared = false;
  2349. constructor(resolved) {
  2350. super(resolved.type);
  2351. this.resolved = resolved;
  2352. this.original = resolved;
  2353. }
  2354. visitExpression(visitor, context) {
  2355. if (context === KEY_CONTEXT) {
  2356. // When producing a key we want to traverse the constant not the
  2357. // variable used to refer to it.
  2358. return this.original.visitExpression(visitor, context);
  2359. }
  2360. else {
  2361. return this.resolved.visitExpression(visitor, context);
  2362. }
  2363. }
  2364. isEquivalent(e) {
  2365. return e instanceof FixupExpression && this.resolved.isEquivalent(e.resolved);
  2366. }
  2367. isConstant() {
  2368. return true;
  2369. }
  2370. clone() {
  2371. throw new Error(`Not supported.`);
  2372. }
  2373. fixup(expression) {
  2374. this.resolved = expression;
  2375. this.shared = true;
  2376. }
  2377. }
  2378. /**
  2379. * A constant pool allows a code emitter to share constant in an output context.
  2380. *
  2381. * The constant pool also supports sharing access to ivy definitions references.
  2382. */
  2383. class ConstantPool {
  2384. isClosureCompilerEnabled;
  2385. statements = [];
  2386. literals = new Map();
  2387. literalFactories = new Map();
  2388. sharedConstants = new Map();
  2389. /**
  2390. * Constant pool also tracks claimed names from {@link uniqueName}.
  2391. * This is useful to avoid collisions if variables are intended to be
  2392. * named a certain way- but may conflict. We wouldn't want to always suffix
  2393. * them with unique numbers.
  2394. */
  2395. _claimedNames = new Map();
  2396. nextNameIndex = 0;
  2397. constructor(isClosureCompilerEnabled = false) {
  2398. this.isClosureCompilerEnabled = isClosureCompilerEnabled;
  2399. }
  2400. getConstLiteral(literal, forceShared) {
  2401. if ((literal instanceof LiteralExpr && !isLongStringLiteral(literal)) ||
  2402. literal instanceof FixupExpression) {
  2403. // Do no put simple literals into the constant pool or try to produce a constant for a
  2404. // reference to a constant.
  2405. return literal;
  2406. }
  2407. const key = GenericKeyFn.INSTANCE.keyOf(literal);
  2408. let fixup = this.literals.get(key);
  2409. let newValue = false;
  2410. if (!fixup) {
  2411. fixup = new FixupExpression(literal);
  2412. this.literals.set(key, fixup);
  2413. newValue = true;
  2414. }
  2415. if ((!newValue && !fixup.shared) || (newValue && forceShared)) {
  2416. // Replace the expression with a variable
  2417. const name = this.freshName();
  2418. let definition;
  2419. let usage;
  2420. if (this.isClosureCompilerEnabled && isLongStringLiteral(literal)) {
  2421. // For string literals, Closure will **always** inline the string at
  2422. // **all** usages, duplicating it each time. For large strings, this
  2423. // unnecessarily bloats bundle size. To work around this restriction, we
  2424. // wrap the string in a function, and call that function for each usage.
  2425. // This tricks Closure into using inline logic for functions instead of
  2426. // string literals. Function calls are only inlined if the body is small
  2427. // enough to be worth it. By doing this, very large strings will be
  2428. // shared across multiple usages, rather than duplicating the string at
  2429. // each usage site.
  2430. //
  2431. // const myStr = function() { return "very very very long string"; };
  2432. // const usage1 = myStr();
  2433. // const usage2 = myStr();
  2434. definition = variable(name).set(new FunctionExpr([], // Params.
  2435. [
  2436. // Statements.
  2437. new ReturnStatement(literal),
  2438. ]));
  2439. usage = variable(name).callFn([]);
  2440. }
  2441. else {
  2442. // Just declare and use the variable directly, without a function call
  2443. // indirection. This saves a few bytes and avoids an unnecessary call.
  2444. definition = variable(name).set(literal);
  2445. usage = variable(name);
  2446. }
  2447. this.statements.push(definition.toDeclStmt(INFERRED_TYPE, StmtModifier.Final));
  2448. fixup.fixup(usage);
  2449. }
  2450. return fixup;
  2451. }
  2452. getSharedConstant(def, expr) {
  2453. const key = def.keyOf(expr);
  2454. if (!this.sharedConstants.has(key)) {
  2455. const id = this.freshName();
  2456. this.sharedConstants.set(key, variable(id));
  2457. this.statements.push(def.toSharedConstantDeclaration(id, expr));
  2458. }
  2459. return this.sharedConstants.get(key);
  2460. }
  2461. getLiteralFactory(literal) {
  2462. // Create a pure function that builds an array of a mix of constant and variable expressions
  2463. if (literal instanceof LiteralArrayExpr) {
  2464. const argumentsForKey = literal.entries.map((e) => (e.isConstant() ? e : UNKNOWN_VALUE_KEY));
  2465. const key = GenericKeyFn.INSTANCE.keyOf(literalArr(argumentsForKey));
  2466. return this._getLiteralFactory(key, literal.entries, (entries) => literalArr(entries));
  2467. }
  2468. else {
  2469. const expressionForKey = literalMap(literal.entries.map((e) => ({
  2470. key: e.key,
  2471. value: e.value.isConstant() ? e.value : UNKNOWN_VALUE_KEY,
  2472. quoted: e.quoted,
  2473. })));
  2474. const key = GenericKeyFn.INSTANCE.keyOf(expressionForKey);
  2475. return this._getLiteralFactory(key, literal.entries.map((e) => e.value), (entries) => literalMap(entries.map((value, index) => ({
  2476. key: literal.entries[index].key,
  2477. value,
  2478. quoted: literal.entries[index].quoted,
  2479. }))));
  2480. }
  2481. }
  2482. // TODO: useUniqueName(false) is necessary for naming compatibility with
  2483. // TemplateDefinitionBuilder, but should be removed once Template Pipeline is the default.
  2484. getSharedFunctionReference(fn, prefix, useUniqueName = true) {
  2485. const isArrow = fn instanceof ArrowFunctionExpr;
  2486. for (const current of this.statements) {
  2487. // Arrow functions are saved as variables so we check if the
  2488. // value of the variable is the same as the arrow function.
  2489. if (isArrow && current instanceof DeclareVarStmt && current.value?.isEquivalent(fn)) {
  2490. return variable(current.name);
  2491. }
  2492. // Function declarations are saved as function statements
  2493. // so we compare them directly to the passed-in function.
  2494. if (!isArrow &&
  2495. current instanceof DeclareFunctionStmt &&
  2496. fn instanceof FunctionExpr &&
  2497. fn.isEquivalent(current)) {
  2498. return variable(current.name);
  2499. }
  2500. }
  2501. // Otherwise declare the function.
  2502. const name = useUniqueName ? this.uniqueName(prefix) : prefix;
  2503. this.statements.push(fn instanceof FunctionExpr
  2504. ? fn.toDeclStmt(name, StmtModifier.Final)
  2505. : new DeclareVarStmt(name, fn, INFERRED_TYPE, StmtModifier.Final, fn.sourceSpan));
  2506. return variable(name);
  2507. }
  2508. _getLiteralFactory(key, values, resultMap) {
  2509. let literalFactory = this.literalFactories.get(key);
  2510. const literalFactoryArguments = values.filter((e) => !e.isConstant());
  2511. if (!literalFactory) {
  2512. const resultExpressions = values.map((e, index) => e.isConstant() ? this.getConstLiteral(e, true) : variable(`a${index}`));
  2513. const parameters = resultExpressions
  2514. .filter(isVariable)
  2515. .map((e) => new FnParam(e.name, DYNAMIC_TYPE));
  2516. const pureFunctionDeclaration = arrowFn(parameters, resultMap(resultExpressions), INFERRED_TYPE);
  2517. const name = this.freshName();
  2518. this.statements.push(variable(name)
  2519. .set(pureFunctionDeclaration)
  2520. .toDeclStmt(INFERRED_TYPE, StmtModifier.Final));
  2521. literalFactory = variable(name);
  2522. this.literalFactories.set(key, literalFactory);
  2523. }
  2524. return { literalFactory, literalFactoryArguments };
  2525. }
  2526. /**
  2527. * Produce a unique name in the context of this pool.
  2528. *
  2529. * The name might be unique among different prefixes if any of the prefixes end in
  2530. * a digit so the prefix should be a constant string (not based on user input) and
  2531. * must not end in a digit.
  2532. */
  2533. uniqueName(name, alwaysIncludeSuffix = true) {
  2534. const count = this._claimedNames.get(name) ?? 0;
  2535. const result = count === 0 && !alwaysIncludeSuffix ? `${name}` : `${name}${count}`;
  2536. this._claimedNames.set(name, count + 1);
  2537. return result;
  2538. }
  2539. freshName() {
  2540. return this.uniqueName(CONSTANT_PREFIX);
  2541. }
  2542. }
  2543. class GenericKeyFn {
  2544. static INSTANCE = new GenericKeyFn();
  2545. keyOf(expr) {
  2546. if (expr instanceof LiteralExpr && typeof expr.value === 'string') {
  2547. return `"${expr.value}"`;
  2548. }
  2549. else if (expr instanceof LiteralExpr) {
  2550. return String(expr.value);
  2551. }
  2552. else if (expr instanceof LiteralArrayExpr) {
  2553. const entries = [];
  2554. for (const entry of expr.entries) {
  2555. entries.push(this.keyOf(entry));
  2556. }
  2557. return `[${entries.join(',')}]`;
  2558. }
  2559. else if (expr instanceof LiteralMapExpr) {
  2560. const entries = [];
  2561. for (const entry of expr.entries) {
  2562. let key = entry.key;
  2563. if (entry.quoted) {
  2564. key = `"${key}"`;
  2565. }
  2566. entries.push(key + ':' + this.keyOf(entry.value));
  2567. }
  2568. return `{${entries.join(',')}}`;
  2569. }
  2570. else if (expr instanceof ExternalExpr) {
  2571. return `import("${expr.value.moduleName}", ${expr.value.name})`;
  2572. }
  2573. else if (expr instanceof ReadVarExpr) {
  2574. return `read(${expr.name})`;
  2575. }
  2576. else if (expr instanceof TypeofExpr) {
  2577. return `typeof(${this.keyOf(expr.expr)})`;
  2578. }
  2579. else {
  2580. throw new Error(`${this.constructor.name} does not handle expressions of type ${expr.constructor.name}`);
  2581. }
  2582. }
  2583. }
  2584. function isVariable(e) {
  2585. return e instanceof ReadVarExpr;
  2586. }
  2587. function isLongStringLiteral(expr) {
  2588. return (expr instanceof LiteralExpr &&
  2589. typeof expr.value === 'string' &&
  2590. expr.value.length >= POOL_INCLUSION_LENGTH_THRESHOLD_FOR_STRINGS);
  2591. }
  2592. const CORE = '@angular/core';
  2593. class Identifiers {
  2594. /* Methods */
  2595. static NEW_METHOD = 'factory';
  2596. static TRANSFORM_METHOD = 'transform';
  2597. static PATCH_DEPS = 'patchedDeps';
  2598. static core = { name: null, moduleName: CORE };
  2599. /* Instructions */
  2600. static namespaceHTML = { name: 'ɵɵnamespaceHTML', moduleName: CORE };
  2601. static namespaceMathML = { name: 'ɵɵnamespaceMathML', moduleName: CORE };
  2602. static namespaceSVG = { name: 'ɵɵnamespaceSVG', moduleName: CORE };
  2603. static element = { name: 'ɵɵelement', moduleName: CORE };
  2604. static elementStart = { name: 'ɵɵelementStart', moduleName: CORE };
  2605. static elementEnd = { name: 'ɵɵelementEnd', moduleName: CORE };
  2606. static advance = { name: 'ɵɵadvance', moduleName: CORE };
  2607. static syntheticHostProperty = {
  2608. name: 'ɵɵsyntheticHostProperty',
  2609. moduleName: CORE,
  2610. };
  2611. static syntheticHostListener = {
  2612. name: 'ɵɵsyntheticHostListener',
  2613. moduleName: CORE,
  2614. };
  2615. static attribute = { name: 'ɵɵattribute', moduleName: CORE };
  2616. static attributeInterpolate1 = {
  2617. name: 'ɵɵattributeInterpolate1',
  2618. moduleName: CORE,
  2619. };
  2620. static attributeInterpolate2 = {
  2621. name: 'ɵɵattributeInterpolate2',
  2622. moduleName: CORE,
  2623. };
  2624. static attributeInterpolate3 = {
  2625. name: 'ɵɵattributeInterpolate3',
  2626. moduleName: CORE,
  2627. };
  2628. static attributeInterpolate4 = {
  2629. name: 'ɵɵattributeInterpolate4',
  2630. moduleName: CORE,
  2631. };
  2632. static attributeInterpolate5 = {
  2633. name: 'ɵɵattributeInterpolate5',
  2634. moduleName: CORE,
  2635. };
  2636. static attributeInterpolate6 = {
  2637. name: 'ɵɵattributeInterpolate6',
  2638. moduleName: CORE,
  2639. };
  2640. static attributeInterpolate7 = {
  2641. name: 'ɵɵattributeInterpolate7',
  2642. moduleName: CORE,
  2643. };
  2644. static attributeInterpolate8 = {
  2645. name: 'ɵɵattributeInterpolate8',
  2646. moduleName: CORE,
  2647. };
  2648. static attributeInterpolateV = {
  2649. name: 'ɵɵattributeInterpolateV',
  2650. moduleName: CORE,
  2651. };
  2652. static classProp = { name: 'ɵɵclassProp', moduleName: CORE };
  2653. static elementContainerStart = {
  2654. name: 'ɵɵelementContainerStart',
  2655. moduleName: CORE,
  2656. };
  2657. static elementContainerEnd = {
  2658. name: 'ɵɵelementContainerEnd',
  2659. moduleName: CORE,
  2660. };
  2661. static elementContainer = { name: 'ɵɵelementContainer', moduleName: CORE };
  2662. static styleMap = { name: 'ɵɵstyleMap', moduleName: CORE };
  2663. static styleMapInterpolate1 = {
  2664. name: 'ɵɵstyleMapInterpolate1',
  2665. moduleName: CORE,
  2666. };
  2667. static styleMapInterpolate2 = {
  2668. name: 'ɵɵstyleMapInterpolate2',
  2669. moduleName: CORE,
  2670. };
  2671. static styleMapInterpolate3 = {
  2672. name: 'ɵɵstyleMapInterpolate3',
  2673. moduleName: CORE,
  2674. };
  2675. static styleMapInterpolate4 = {
  2676. name: 'ɵɵstyleMapInterpolate4',
  2677. moduleName: CORE,
  2678. };
  2679. static styleMapInterpolate5 = {
  2680. name: 'ɵɵstyleMapInterpolate5',
  2681. moduleName: CORE,
  2682. };
  2683. static styleMapInterpolate6 = {
  2684. name: 'ɵɵstyleMapInterpolate6',
  2685. moduleName: CORE,
  2686. };
  2687. static styleMapInterpolate7 = {
  2688. name: 'ɵɵstyleMapInterpolate7',
  2689. moduleName: CORE,
  2690. };
  2691. static styleMapInterpolate8 = {
  2692. name: 'ɵɵstyleMapInterpolate8',
  2693. moduleName: CORE,
  2694. };
  2695. static styleMapInterpolateV = {
  2696. name: 'ɵɵstyleMapInterpolateV',
  2697. moduleName: CORE,
  2698. };
  2699. static classMap = { name: 'ɵɵclassMap', moduleName: CORE };
  2700. static classMapInterpolate1 = {
  2701. name: 'ɵɵclassMapInterpolate1',
  2702. moduleName: CORE,
  2703. };
  2704. static classMapInterpolate2 = {
  2705. name: 'ɵɵclassMapInterpolate2',
  2706. moduleName: CORE,
  2707. };
  2708. static classMapInterpolate3 = {
  2709. name: 'ɵɵclassMapInterpolate3',
  2710. moduleName: CORE,
  2711. };
  2712. static classMapInterpolate4 = {
  2713. name: 'ɵɵclassMapInterpolate4',
  2714. moduleName: CORE,
  2715. };
  2716. static classMapInterpolate5 = {
  2717. name: 'ɵɵclassMapInterpolate5',
  2718. moduleName: CORE,
  2719. };
  2720. static classMapInterpolate6 = {
  2721. name: 'ɵɵclassMapInterpolate6',
  2722. moduleName: CORE,
  2723. };
  2724. static classMapInterpolate7 = {
  2725. name: 'ɵɵclassMapInterpolate7',
  2726. moduleName: CORE,
  2727. };
  2728. static classMapInterpolate8 = {
  2729. name: 'ɵɵclassMapInterpolate8',
  2730. moduleName: CORE,
  2731. };
  2732. static classMapInterpolateV = {
  2733. name: 'ɵɵclassMapInterpolateV',
  2734. moduleName: CORE,
  2735. };
  2736. static styleProp = { name: 'ɵɵstyleProp', moduleName: CORE };
  2737. static stylePropInterpolate1 = {
  2738. name: 'ɵɵstylePropInterpolate1',
  2739. moduleName: CORE,
  2740. };
  2741. static stylePropInterpolate2 = {
  2742. name: 'ɵɵstylePropInterpolate2',
  2743. moduleName: CORE,
  2744. };
  2745. static stylePropInterpolate3 = {
  2746. name: 'ɵɵstylePropInterpolate3',
  2747. moduleName: CORE,
  2748. };
  2749. static stylePropInterpolate4 = {
  2750. name: 'ɵɵstylePropInterpolate4',
  2751. moduleName: CORE,
  2752. };
  2753. static stylePropInterpolate5 = {
  2754. name: 'ɵɵstylePropInterpolate5',
  2755. moduleName: CORE,
  2756. };
  2757. static stylePropInterpolate6 = {
  2758. name: 'ɵɵstylePropInterpolate6',
  2759. moduleName: CORE,
  2760. };
  2761. static stylePropInterpolate7 = {
  2762. name: 'ɵɵstylePropInterpolate7',
  2763. moduleName: CORE,
  2764. };
  2765. static stylePropInterpolate8 = {
  2766. name: 'ɵɵstylePropInterpolate8',
  2767. moduleName: CORE,
  2768. };
  2769. static stylePropInterpolateV = {
  2770. name: 'ɵɵstylePropInterpolateV',
  2771. moduleName: CORE,
  2772. };
  2773. static nextContext = { name: 'ɵɵnextContext', moduleName: CORE };
  2774. static resetView = { name: 'ɵɵresetView', moduleName: CORE };
  2775. static templateCreate = { name: 'ɵɵtemplate', moduleName: CORE };
  2776. static defer = { name: 'ɵɵdefer', moduleName: CORE };
  2777. static deferWhen = { name: 'ɵɵdeferWhen', moduleName: CORE };
  2778. static deferOnIdle = { name: 'ɵɵdeferOnIdle', moduleName: CORE };
  2779. static deferOnImmediate = { name: 'ɵɵdeferOnImmediate', moduleName: CORE };
  2780. static deferOnTimer = { name: 'ɵɵdeferOnTimer', moduleName: CORE };
  2781. static deferOnHover = { name: 'ɵɵdeferOnHover', moduleName: CORE };
  2782. static deferOnInteraction = { name: 'ɵɵdeferOnInteraction', moduleName: CORE };
  2783. static deferOnViewport = { name: 'ɵɵdeferOnViewport', moduleName: CORE };
  2784. static deferPrefetchWhen = { name: 'ɵɵdeferPrefetchWhen', moduleName: CORE };
  2785. static deferPrefetchOnIdle = {
  2786. name: 'ɵɵdeferPrefetchOnIdle',
  2787. moduleName: CORE,
  2788. };
  2789. static deferPrefetchOnImmediate = {
  2790. name: 'ɵɵdeferPrefetchOnImmediate',
  2791. moduleName: CORE,
  2792. };
  2793. static deferPrefetchOnTimer = {
  2794. name: 'ɵɵdeferPrefetchOnTimer',
  2795. moduleName: CORE,
  2796. };
  2797. static deferPrefetchOnHover = {
  2798. name: 'ɵɵdeferPrefetchOnHover',
  2799. moduleName: CORE,
  2800. };
  2801. static deferPrefetchOnInteraction = {
  2802. name: 'ɵɵdeferPrefetchOnInteraction',
  2803. moduleName: CORE,
  2804. };
  2805. static deferPrefetchOnViewport = {
  2806. name: 'ɵɵdeferPrefetchOnViewport',
  2807. moduleName: CORE,
  2808. };
  2809. static deferHydrateWhen = { name: 'ɵɵdeferHydrateWhen', moduleName: CORE };
  2810. static deferHydrateNever = { name: 'ɵɵdeferHydrateNever', moduleName: CORE };
  2811. static deferHydrateOnIdle = {
  2812. name: 'ɵɵdeferHydrateOnIdle',
  2813. moduleName: CORE,
  2814. };
  2815. static deferHydrateOnImmediate = {
  2816. name: 'ɵɵdeferHydrateOnImmediate',
  2817. moduleName: CORE,
  2818. };
  2819. static deferHydrateOnTimer = {
  2820. name: 'ɵɵdeferHydrateOnTimer',
  2821. moduleName: CORE,
  2822. };
  2823. static deferHydrateOnHover = {
  2824. name: 'ɵɵdeferHydrateOnHover',
  2825. moduleName: CORE,
  2826. };
  2827. static deferHydrateOnInteraction = {
  2828. name: 'ɵɵdeferHydrateOnInteraction',
  2829. moduleName: CORE,
  2830. };
  2831. static deferHydrateOnViewport = {
  2832. name: 'ɵɵdeferHydrateOnViewport',
  2833. moduleName: CORE,
  2834. };
  2835. static deferEnableTimerScheduling = {
  2836. name: 'ɵɵdeferEnableTimerScheduling',
  2837. moduleName: CORE,
  2838. };
  2839. static conditional = { name: 'ɵɵconditional', moduleName: CORE };
  2840. static repeater = { name: 'ɵɵrepeater', moduleName: CORE };
  2841. static repeaterCreate = { name: 'ɵɵrepeaterCreate', moduleName: CORE };
  2842. static repeaterTrackByIndex = {
  2843. name: 'ɵɵrepeaterTrackByIndex',
  2844. moduleName: CORE,
  2845. };
  2846. static repeaterTrackByIdentity = {
  2847. name: 'ɵɵrepeaterTrackByIdentity',
  2848. moduleName: CORE,
  2849. };
  2850. static componentInstance = { name: 'ɵɵcomponentInstance', moduleName: CORE };
  2851. static text = { name: 'ɵɵtext', moduleName: CORE };
  2852. static enableBindings = { name: 'ɵɵenableBindings', moduleName: CORE };
  2853. static disableBindings = { name: 'ɵɵdisableBindings', moduleName: CORE };
  2854. static getCurrentView = { name: 'ɵɵgetCurrentView', moduleName: CORE };
  2855. static textInterpolate = { name: 'ɵɵtextInterpolate', moduleName: CORE };
  2856. static textInterpolate1 = { name: 'ɵɵtextInterpolate1', moduleName: CORE };
  2857. static textInterpolate2 = { name: 'ɵɵtextInterpolate2', moduleName: CORE };
  2858. static textInterpolate3 = { name: 'ɵɵtextInterpolate3', moduleName: CORE };
  2859. static textInterpolate4 = { name: 'ɵɵtextInterpolate4', moduleName: CORE };
  2860. static textInterpolate5 = { name: 'ɵɵtextInterpolate5', moduleName: CORE };
  2861. static textInterpolate6 = { name: 'ɵɵtextInterpolate6', moduleName: CORE };
  2862. static textInterpolate7 = { name: 'ɵɵtextInterpolate7', moduleName: CORE };
  2863. static textInterpolate8 = { name: 'ɵɵtextInterpolate8', moduleName: CORE };
  2864. static textInterpolateV = { name: 'ɵɵtextInterpolateV', moduleName: CORE };
  2865. static restoreView = { name: 'ɵɵrestoreView', moduleName: CORE };
  2866. static pureFunction0 = { name: 'ɵɵpureFunction0', moduleName: CORE };
  2867. static pureFunction1 = { name: 'ɵɵpureFunction1', moduleName: CORE };
  2868. static pureFunction2 = { name: 'ɵɵpureFunction2', moduleName: CORE };
  2869. static pureFunction3 = { name: 'ɵɵpureFunction3', moduleName: CORE };
  2870. static pureFunction4 = { name: 'ɵɵpureFunction4', moduleName: CORE };
  2871. static pureFunction5 = { name: 'ɵɵpureFunction5', moduleName: CORE };
  2872. static pureFunction6 = { name: 'ɵɵpureFunction6', moduleName: CORE };
  2873. static pureFunction7 = { name: 'ɵɵpureFunction7', moduleName: CORE };
  2874. static pureFunction8 = { name: 'ɵɵpureFunction8', moduleName: CORE };
  2875. static pureFunctionV = { name: 'ɵɵpureFunctionV', moduleName: CORE };
  2876. static pipeBind1 = { name: 'ɵɵpipeBind1', moduleName: CORE };
  2877. static pipeBind2 = { name: 'ɵɵpipeBind2', moduleName: CORE };
  2878. static pipeBind3 = { name: 'ɵɵpipeBind3', moduleName: CORE };
  2879. static pipeBind4 = { name: 'ɵɵpipeBind4', moduleName: CORE };
  2880. static pipeBindV = { name: 'ɵɵpipeBindV', moduleName: CORE };
  2881. static hostProperty = { name: 'ɵɵhostProperty', moduleName: CORE };
  2882. static property = { name: 'ɵɵproperty', moduleName: CORE };
  2883. static propertyInterpolate = {
  2884. name: 'ɵɵpropertyInterpolate',
  2885. moduleName: CORE,
  2886. };
  2887. static propertyInterpolate1 = {
  2888. name: 'ɵɵpropertyInterpolate1',
  2889. moduleName: CORE,
  2890. };
  2891. static propertyInterpolate2 = {
  2892. name: 'ɵɵpropertyInterpolate2',
  2893. moduleName: CORE,
  2894. };
  2895. static propertyInterpolate3 = {
  2896. name: 'ɵɵpropertyInterpolate3',
  2897. moduleName: CORE,
  2898. };
  2899. static propertyInterpolate4 = {
  2900. name: 'ɵɵpropertyInterpolate4',
  2901. moduleName: CORE,
  2902. };
  2903. static propertyInterpolate5 = {
  2904. name: 'ɵɵpropertyInterpolate5',
  2905. moduleName: CORE,
  2906. };
  2907. static propertyInterpolate6 = {
  2908. name: 'ɵɵpropertyInterpolate6',
  2909. moduleName: CORE,
  2910. };
  2911. static propertyInterpolate7 = {
  2912. name: 'ɵɵpropertyInterpolate7',
  2913. moduleName: CORE,
  2914. };
  2915. static propertyInterpolate8 = {
  2916. name: 'ɵɵpropertyInterpolate8',
  2917. moduleName: CORE,
  2918. };
  2919. static propertyInterpolateV = {
  2920. name: 'ɵɵpropertyInterpolateV',
  2921. moduleName: CORE,
  2922. };
  2923. static i18n = { name: 'ɵɵi18n', moduleName: CORE };
  2924. static i18nAttributes = { name: 'ɵɵi18nAttributes', moduleName: CORE };
  2925. static i18nExp = { name: 'ɵɵi18nExp', moduleName: CORE };
  2926. static i18nStart = { name: 'ɵɵi18nStart', moduleName: CORE };
  2927. static i18nEnd = { name: 'ɵɵi18nEnd', moduleName: CORE };
  2928. static i18nApply = { name: 'ɵɵi18nApply', moduleName: CORE };
  2929. static i18nPostprocess = { name: 'ɵɵi18nPostprocess', moduleName: CORE };
  2930. static pipe = { name: 'ɵɵpipe', moduleName: CORE };
  2931. static projection = { name: 'ɵɵprojection', moduleName: CORE };
  2932. static projectionDef = { name: 'ɵɵprojectionDef', moduleName: CORE };
  2933. static reference = { name: 'ɵɵreference', moduleName: CORE };
  2934. static inject = { name: 'ɵɵinject', moduleName: CORE };
  2935. static injectAttribute = { name: 'ɵɵinjectAttribute', moduleName: CORE };
  2936. static directiveInject = { name: 'ɵɵdirectiveInject', moduleName: CORE };
  2937. static invalidFactory = { name: 'ɵɵinvalidFactory', moduleName: CORE };
  2938. static invalidFactoryDep = { name: 'ɵɵinvalidFactoryDep', moduleName: CORE };
  2939. static templateRefExtractor = {
  2940. name: 'ɵɵtemplateRefExtractor',
  2941. moduleName: CORE,
  2942. };
  2943. static forwardRef = { name: 'forwardRef', moduleName: CORE };
  2944. static resolveForwardRef = { name: 'resolveForwardRef', moduleName: CORE };
  2945. static replaceMetadata = { name: 'ɵɵreplaceMetadata', moduleName: CORE };
  2946. static ɵɵdefineInjectable = { name: 'ɵɵdefineInjectable', moduleName: CORE };
  2947. static declareInjectable = { name: 'ɵɵngDeclareInjectable', moduleName: CORE };
  2948. static InjectableDeclaration = {
  2949. name: 'ɵɵInjectableDeclaration',
  2950. moduleName: CORE,
  2951. };
  2952. static resolveWindow = { name: 'ɵɵresolveWindow', moduleName: CORE };
  2953. static resolveDocument = { name: 'ɵɵresolveDocument', moduleName: CORE };
  2954. static resolveBody = { name: 'ɵɵresolveBody', moduleName: CORE };
  2955. static getComponentDepsFactory = {
  2956. name: 'ɵɵgetComponentDepsFactory',
  2957. moduleName: CORE,
  2958. };
  2959. static defineComponent = { name: 'ɵɵdefineComponent', moduleName: CORE };
  2960. static declareComponent = { name: 'ɵɵngDeclareComponent', moduleName: CORE };
  2961. static setComponentScope = { name: 'ɵɵsetComponentScope', moduleName: CORE };
  2962. static ChangeDetectionStrategy = {
  2963. name: 'ChangeDetectionStrategy',
  2964. moduleName: CORE,
  2965. };
  2966. static ViewEncapsulation = {
  2967. name: 'ViewEncapsulation',
  2968. moduleName: CORE,
  2969. };
  2970. static ComponentDeclaration = {
  2971. name: 'ɵɵComponentDeclaration',
  2972. moduleName: CORE,
  2973. };
  2974. static FactoryDeclaration = {
  2975. name: 'ɵɵFactoryDeclaration',
  2976. moduleName: CORE,
  2977. };
  2978. static declareFactory = { name: 'ɵɵngDeclareFactory', moduleName: CORE };
  2979. static FactoryTarget = { name: 'ɵɵFactoryTarget', moduleName: CORE };
  2980. static defineDirective = { name: 'ɵɵdefineDirective', moduleName: CORE };
  2981. static declareDirective = { name: 'ɵɵngDeclareDirective', moduleName: CORE };
  2982. static DirectiveDeclaration = {
  2983. name: 'ɵɵDirectiveDeclaration',
  2984. moduleName: CORE,
  2985. };
  2986. static InjectorDef = { name: 'ɵɵInjectorDef', moduleName: CORE };
  2987. static InjectorDeclaration = {
  2988. name: 'ɵɵInjectorDeclaration',
  2989. moduleName: CORE,
  2990. };
  2991. static defineInjector = { name: 'ɵɵdefineInjector', moduleName: CORE };
  2992. static declareInjector = { name: 'ɵɵngDeclareInjector', moduleName: CORE };
  2993. static NgModuleDeclaration = {
  2994. name: 'ɵɵNgModuleDeclaration',
  2995. moduleName: CORE,
  2996. };
  2997. static ModuleWithProviders = {
  2998. name: 'ModuleWithProviders',
  2999. moduleName: CORE,
  3000. };
  3001. static defineNgModule = { name: 'ɵɵdefineNgModule', moduleName: CORE };
  3002. static declareNgModule = { name: 'ɵɵngDeclareNgModule', moduleName: CORE };
  3003. static setNgModuleScope = { name: 'ɵɵsetNgModuleScope', moduleName: CORE };
  3004. static registerNgModuleType = {
  3005. name: 'ɵɵregisterNgModuleType',
  3006. moduleName: CORE,
  3007. };
  3008. static PipeDeclaration = { name: 'ɵɵPipeDeclaration', moduleName: CORE };
  3009. static definePipe = { name: 'ɵɵdefinePipe', moduleName: CORE };
  3010. static declarePipe = { name: 'ɵɵngDeclarePipe', moduleName: CORE };
  3011. static declareClassMetadata = {
  3012. name: 'ɵɵngDeclareClassMetadata',
  3013. moduleName: CORE,
  3014. };
  3015. static declareClassMetadataAsync = {
  3016. name: 'ɵɵngDeclareClassMetadataAsync',
  3017. moduleName: CORE,
  3018. };
  3019. static setClassMetadata = { name: 'ɵsetClassMetadata', moduleName: CORE };
  3020. static setClassMetadataAsync = {
  3021. name: 'ɵsetClassMetadataAsync',
  3022. moduleName: CORE,
  3023. };
  3024. static setClassDebugInfo = { name: 'ɵsetClassDebugInfo', moduleName: CORE };
  3025. static queryRefresh = { name: 'ɵɵqueryRefresh', moduleName: CORE };
  3026. static viewQuery = { name: 'ɵɵviewQuery', moduleName: CORE };
  3027. static loadQuery = { name: 'ɵɵloadQuery', moduleName: CORE };
  3028. static contentQuery = { name: 'ɵɵcontentQuery', moduleName: CORE };
  3029. // Signal queries
  3030. static viewQuerySignal = { name: 'ɵɵviewQuerySignal', moduleName: CORE };
  3031. static contentQuerySignal = { name: 'ɵɵcontentQuerySignal', moduleName: CORE };
  3032. static queryAdvance = { name: 'ɵɵqueryAdvance', moduleName: CORE };
  3033. // Two-way bindings
  3034. static twoWayProperty = { name: 'ɵɵtwoWayProperty', moduleName: CORE };
  3035. static twoWayBindingSet = { name: 'ɵɵtwoWayBindingSet', moduleName: CORE };
  3036. static twoWayListener = { name: 'ɵɵtwoWayListener', moduleName: CORE };
  3037. static declareLet = { name: 'ɵɵdeclareLet', moduleName: CORE };
  3038. static storeLet = { name: 'ɵɵstoreLet', moduleName: CORE };
  3039. static readContextLet = { name: 'ɵɵreadContextLet', moduleName: CORE };
  3040. static attachSourceLocations = {
  3041. name: 'ɵɵattachSourceLocations',
  3042. moduleName: CORE,
  3043. };
  3044. static NgOnChangesFeature = { name: 'ɵɵNgOnChangesFeature', moduleName: CORE };
  3045. static InheritDefinitionFeature = {
  3046. name: 'ɵɵInheritDefinitionFeature',
  3047. moduleName: CORE,
  3048. };
  3049. static CopyDefinitionFeature = {
  3050. name: 'ɵɵCopyDefinitionFeature',
  3051. moduleName: CORE,
  3052. };
  3053. static ProvidersFeature = { name: 'ɵɵProvidersFeature', moduleName: CORE };
  3054. static HostDirectivesFeature = {
  3055. name: 'ɵɵHostDirectivesFeature',
  3056. moduleName: CORE,
  3057. };
  3058. static ExternalStylesFeature = {
  3059. name: 'ɵɵExternalStylesFeature',
  3060. moduleName: CORE,
  3061. };
  3062. static listener = { name: 'ɵɵlistener', moduleName: CORE };
  3063. static getInheritedFactory = {
  3064. name: 'ɵɵgetInheritedFactory',
  3065. moduleName: CORE,
  3066. };
  3067. // sanitization-related functions
  3068. static sanitizeHtml = { name: 'ɵɵsanitizeHtml', moduleName: CORE };
  3069. static sanitizeStyle = { name: 'ɵɵsanitizeStyle', moduleName: CORE };
  3070. static sanitizeResourceUrl = {
  3071. name: 'ɵɵsanitizeResourceUrl',
  3072. moduleName: CORE,
  3073. };
  3074. static sanitizeScript = { name: 'ɵɵsanitizeScript', moduleName: CORE };
  3075. static sanitizeUrl = { name: 'ɵɵsanitizeUrl', moduleName: CORE };
  3076. static sanitizeUrlOrResourceUrl = {
  3077. name: 'ɵɵsanitizeUrlOrResourceUrl',
  3078. moduleName: CORE,
  3079. };
  3080. static trustConstantHtml = { name: 'ɵɵtrustConstantHtml', moduleName: CORE };
  3081. static trustConstantResourceUrl = {
  3082. name: 'ɵɵtrustConstantResourceUrl',
  3083. moduleName: CORE,
  3084. };
  3085. static validateIframeAttribute = {
  3086. name: 'ɵɵvalidateIframeAttribute',
  3087. moduleName: CORE,
  3088. };
  3089. // type-checking
  3090. static InputSignalBrandWriteType = { name: 'ɵINPUT_SIGNAL_BRAND_WRITE_TYPE', moduleName: CORE };
  3091. static UnwrapDirectiveSignalInputs = { name: 'ɵUnwrapDirectiveSignalInputs', moduleName: CORE };
  3092. static unwrapWritableSignal = { name: 'ɵunwrapWritableSignal', moduleName: CORE };
  3093. }
  3094. const DASH_CASE_REGEXP = /-+([a-z0-9])/g;
  3095. function dashCaseToCamelCase(input) {
  3096. return input.replace(DASH_CASE_REGEXP, (...m) => m[1].toUpperCase());
  3097. }
  3098. function splitAtColon(input, defaultValues) {
  3099. return _splitAt(input, ':', defaultValues);
  3100. }
  3101. function splitAtPeriod(input, defaultValues) {
  3102. return _splitAt(input, '.', defaultValues);
  3103. }
  3104. function _splitAt(input, character, defaultValues) {
  3105. const characterIndex = input.indexOf(character);
  3106. if (characterIndex == -1)
  3107. return defaultValues;
  3108. return [input.slice(0, characterIndex).trim(), input.slice(characterIndex + 1).trim()];
  3109. }
  3110. function noUndefined(val) {
  3111. return val === undefined ? null : val;
  3112. }
  3113. function utf8Encode(str) {
  3114. let encoded = [];
  3115. for (let index = 0; index < str.length; index++) {
  3116. let codePoint = str.charCodeAt(index);
  3117. // decode surrogate
  3118. // see https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
  3119. if (codePoint >= 0xd800 && codePoint <= 0xdbff && str.length > index + 1) {
  3120. const low = str.charCodeAt(index + 1);
  3121. if (low >= 0xdc00 && low <= 0xdfff) {
  3122. index++;
  3123. codePoint = ((codePoint - 0xd800) << 10) + low - 0xdc00 + 0x10000;
  3124. }
  3125. }
  3126. if (codePoint <= 0x7f) {
  3127. encoded.push(codePoint);
  3128. }
  3129. else if (codePoint <= 0x7ff) {
  3130. encoded.push(((codePoint >> 6) & 0x1f) | 0xc0, (codePoint & 0x3f) | 0x80);
  3131. }
  3132. else if (codePoint <= 0xffff) {
  3133. encoded.push((codePoint >> 12) | 0xe0, ((codePoint >> 6) & 0x3f) | 0x80, (codePoint & 0x3f) | 0x80);
  3134. }
  3135. else if (codePoint <= 0x1fffff) {
  3136. encoded.push(((codePoint >> 18) & 0x07) | 0xf0, ((codePoint >> 12) & 0x3f) | 0x80, ((codePoint >> 6) & 0x3f) | 0x80, (codePoint & 0x3f) | 0x80);
  3137. }
  3138. }
  3139. return encoded;
  3140. }
  3141. function stringify(token) {
  3142. if (typeof token === 'string') {
  3143. return token;
  3144. }
  3145. if (Array.isArray(token)) {
  3146. return '[' + token.map(stringify).join(', ') + ']';
  3147. }
  3148. if (token == null) {
  3149. return '' + token;
  3150. }
  3151. if (token.overriddenName) {
  3152. return `${token.overriddenName}`;
  3153. }
  3154. if (token.name) {
  3155. return `${token.name}`;
  3156. }
  3157. if (!token.toString) {
  3158. return 'object';
  3159. }
  3160. // WARNING: do not try to `JSON.stringify(token)` here
  3161. // see https://github.com/angular/angular/issues/23440
  3162. const res = token.toString();
  3163. if (res == null) {
  3164. return '' + res;
  3165. }
  3166. const newLineIndex = res.indexOf('\n');
  3167. return newLineIndex === -1 ? res : res.substring(0, newLineIndex);
  3168. }
  3169. class Version {
  3170. full;
  3171. major;
  3172. minor;
  3173. patch;
  3174. constructor(full) {
  3175. this.full = full;
  3176. const splits = full.split('.');
  3177. this.major = splits[0];
  3178. this.minor = splits[1];
  3179. this.patch = splits.slice(2).join('.');
  3180. }
  3181. }
  3182. const _global = globalThis;
  3183. const V1_TO_18 = /^([1-9]|1[0-8])\./;
  3184. function getJitStandaloneDefaultForVersion(version) {
  3185. if (version.startsWith('0.')) {
  3186. // 0.0.0 is always "latest", default is true.
  3187. return true;
  3188. }
  3189. if (V1_TO_18.test(version)) {
  3190. // Angular v2 - v18 default is false.
  3191. return false;
  3192. }
  3193. // All other Angular versions (v19+) default to true.
  3194. return true;
  3195. }
  3196. // https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit
  3197. const VERSION$1 = 3;
  3198. const JS_B64_PREFIX = '# sourceMappingURL=data:application/json;base64,';
  3199. class SourceMapGenerator {
  3200. file;
  3201. sourcesContent = new Map();
  3202. lines = [];
  3203. lastCol0 = 0;
  3204. hasMappings = false;
  3205. constructor(file = null) {
  3206. this.file = file;
  3207. }
  3208. // The content is `null` when the content is expected to be loaded using the URL
  3209. addSource(url, content = null) {
  3210. if (!this.sourcesContent.has(url)) {
  3211. this.sourcesContent.set(url, content);
  3212. }
  3213. return this;
  3214. }
  3215. addLine() {
  3216. this.lines.push([]);
  3217. this.lastCol0 = 0;
  3218. return this;
  3219. }
  3220. addMapping(col0, sourceUrl, sourceLine0, sourceCol0) {
  3221. if (!this.currentLine) {
  3222. throw new Error(`A line must be added before mappings can be added`);
  3223. }
  3224. if (sourceUrl != null && !this.sourcesContent.has(sourceUrl)) {
  3225. throw new Error(`Unknown source file "${sourceUrl}"`);
  3226. }
  3227. if (col0 == null) {
  3228. throw new Error(`The column in the generated code must be provided`);
  3229. }
  3230. if (col0 < this.lastCol0) {
  3231. throw new Error(`Mapping should be added in output order`);
  3232. }
  3233. if (sourceUrl && (sourceLine0 == null || sourceCol0 == null)) {
  3234. throw new Error(`The source location must be provided when a source url is provided`);
  3235. }
  3236. this.hasMappings = true;
  3237. this.lastCol0 = col0;
  3238. this.currentLine.push({ col0, sourceUrl, sourceLine0, sourceCol0 });
  3239. return this;
  3240. }
  3241. /**
  3242. * @internal strip this from published d.ts files due to
  3243. * https://github.com/microsoft/TypeScript/issues/36216
  3244. */
  3245. get currentLine() {
  3246. return this.lines.slice(-1)[0];
  3247. }
  3248. toJSON() {
  3249. if (!this.hasMappings) {
  3250. return null;
  3251. }
  3252. const sourcesIndex = new Map();
  3253. const sources = [];
  3254. const sourcesContent = [];
  3255. Array.from(this.sourcesContent.keys()).forEach((url, i) => {
  3256. sourcesIndex.set(url, i);
  3257. sources.push(url);
  3258. sourcesContent.push(this.sourcesContent.get(url) || null);
  3259. });
  3260. let mappings = '';
  3261. let lastCol0 = 0;
  3262. let lastSourceIndex = 0;
  3263. let lastSourceLine0 = 0;
  3264. let lastSourceCol0 = 0;
  3265. this.lines.forEach((segments) => {
  3266. lastCol0 = 0;
  3267. mappings += segments
  3268. .map((segment) => {
  3269. // zero-based starting column of the line in the generated code
  3270. let segAsStr = toBase64VLQ(segment.col0 - lastCol0);
  3271. lastCol0 = segment.col0;
  3272. if (segment.sourceUrl != null) {
  3273. // zero-based index into the “sources” list
  3274. segAsStr += toBase64VLQ(sourcesIndex.get(segment.sourceUrl) - lastSourceIndex);
  3275. lastSourceIndex = sourcesIndex.get(segment.sourceUrl);
  3276. // the zero-based starting line in the original source
  3277. segAsStr += toBase64VLQ(segment.sourceLine0 - lastSourceLine0);
  3278. lastSourceLine0 = segment.sourceLine0;
  3279. // the zero-based starting column in the original source
  3280. segAsStr += toBase64VLQ(segment.sourceCol0 - lastSourceCol0);
  3281. lastSourceCol0 = segment.sourceCol0;
  3282. }
  3283. return segAsStr;
  3284. })
  3285. .join(',');
  3286. mappings += ';';
  3287. });
  3288. mappings = mappings.slice(0, -1);
  3289. return {
  3290. 'file': this.file || '',
  3291. 'version': VERSION$1,
  3292. 'sourceRoot': '',
  3293. 'sources': sources,
  3294. 'sourcesContent': sourcesContent,
  3295. 'mappings': mappings,
  3296. };
  3297. }
  3298. toJsComment() {
  3299. return this.hasMappings
  3300. ? '//' + JS_B64_PREFIX + toBase64String(JSON.stringify(this, null, 0))
  3301. : '';
  3302. }
  3303. }
  3304. function toBase64String(value) {
  3305. let b64 = '';
  3306. const encoded = utf8Encode(value);
  3307. for (let i = 0; i < encoded.length;) {
  3308. const i1 = encoded[i++];
  3309. const i2 = i < encoded.length ? encoded[i++] : null;
  3310. const i3 = i < encoded.length ? encoded[i++] : null;
  3311. b64 += toBase64Digit(i1 >> 2);
  3312. b64 += toBase64Digit(((i1 & 3) << 4) | (i2 === null ? 0 : i2 >> 4));
  3313. b64 += i2 === null ? '=' : toBase64Digit(((i2 & 15) << 2) | (i3 === null ? 0 : i3 >> 6));
  3314. b64 += i2 === null || i3 === null ? '=' : toBase64Digit(i3 & 63);
  3315. }
  3316. return b64;
  3317. }
  3318. function toBase64VLQ(value) {
  3319. value = value < 0 ? (-value << 1) + 1 : value << 1;
  3320. let out = '';
  3321. do {
  3322. let digit = value & 31;
  3323. value = value >> 5;
  3324. if (value > 0) {
  3325. digit = digit | 32;
  3326. }
  3327. out += toBase64Digit(digit);
  3328. } while (value > 0);
  3329. return out;
  3330. }
  3331. const B64_DIGITS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  3332. function toBase64Digit(value) {
  3333. if (value < 0 || value >= 64) {
  3334. throw new Error(`Can only encode value in the range [0, 63]`);
  3335. }
  3336. return B64_DIGITS[value];
  3337. }
  3338. const _SINGLE_QUOTE_ESCAPE_STRING_RE = /'|\\|\n|\r|\$/g;
  3339. const _LEGAL_IDENTIFIER_RE = /^[$A-Z_][0-9A-Z_$]*$/i;
  3340. const _INDENT_WITH = ' ';
  3341. class _EmittedLine {
  3342. indent;
  3343. partsLength = 0;
  3344. parts = [];
  3345. srcSpans = [];
  3346. constructor(indent) {
  3347. this.indent = indent;
  3348. }
  3349. }
  3350. class EmitterVisitorContext {
  3351. _indent;
  3352. static createRoot() {
  3353. return new EmitterVisitorContext(0);
  3354. }
  3355. _lines;
  3356. constructor(_indent) {
  3357. this._indent = _indent;
  3358. this._lines = [new _EmittedLine(_indent)];
  3359. }
  3360. /**
  3361. * @internal strip this from published d.ts files due to
  3362. * https://github.com/microsoft/TypeScript/issues/36216
  3363. */
  3364. get _currentLine() {
  3365. return this._lines[this._lines.length - 1];
  3366. }
  3367. println(from, lastPart = '') {
  3368. this.print(from || null, lastPart, true);
  3369. }
  3370. lineIsEmpty() {
  3371. return this._currentLine.parts.length === 0;
  3372. }
  3373. lineLength() {
  3374. return this._currentLine.indent * _INDENT_WITH.length + this._currentLine.partsLength;
  3375. }
  3376. print(from, part, newLine = false) {
  3377. if (part.length > 0) {
  3378. this._currentLine.parts.push(part);
  3379. this._currentLine.partsLength += part.length;
  3380. this._currentLine.srcSpans.push((from && from.sourceSpan) || null);
  3381. }
  3382. if (newLine) {
  3383. this._lines.push(new _EmittedLine(this._indent));
  3384. }
  3385. }
  3386. removeEmptyLastLine() {
  3387. if (this.lineIsEmpty()) {
  3388. this._lines.pop();
  3389. }
  3390. }
  3391. incIndent() {
  3392. this._indent++;
  3393. if (this.lineIsEmpty()) {
  3394. this._currentLine.indent = this._indent;
  3395. }
  3396. }
  3397. decIndent() {
  3398. this._indent--;
  3399. if (this.lineIsEmpty()) {
  3400. this._currentLine.indent = this._indent;
  3401. }
  3402. }
  3403. toSource() {
  3404. return this.sourceLines
  3405. .map((l) => (l.parts.length > 0 ? _createIndent(l.indent) + l.parts.join('') : ''))
  3406. .join('\n');
  3407. }
  3408. toSourceMapGenerator(genFilePath, startsAtLine = 0) {
  3409. const map = new SourceMapGenerator(genFilePath);
  3410. let firstOffsetMapped = false;
  3411. const mapFirstOffsetIfNeeded = () => {
  3412. if (!firstOffsetMapped) {
  3413. // Add a single space so that tools won't try to load the file from disk.
  3414. // Note: We are using virtual urls like `ng:///`, so we have to
  3415. // provide a content here.
  3416. map.addSource(genFilePath, ' ').addMapping(0, genFilePath, 0, 0);
  3417. firstOffsetMapped = true;
  3418. }
  3419. };
  3420. for (let i = 0; i < startsAtLine; i++) {
  3421. map.addLine();
  3422. mapFirstOffsetIfNeeded();
  3423. }
  3424. this.sourceLines.forEach((line, lineIdx) => {
  3425. map.addLine();
  3426. const spans = line.srcSpans;
  3427. const parts = line.parts;
  3428. let col0 = line.indent * _INDENT_WITH.length;
  3429. let spanIdx = 0;
  3430. // skip leading parts without source spans
  3431. while (spanIdx < spans.length && !spans[spanIdx]) {
  3432. col0 += parts[spanIdx].length;
  3433. spanIdx++;
  3434. }
  3435. if (spanIdx < spans.length && lineIdx === 0 && col0 === 0) {
  3436. firstOffsetMapped = true;
  3437. }
  3438. else {
  3439. mapFirstOffsetIfNeeded();
  3440. }
  3441. while (spanIdx < spans.length) {
  3442. const span = spans[spanIdx];
  3443. const source = span.start.file;
  3444. const sourceLine = span.start.line;
  3445. const sourceCol = span.start.col;
  3446. map
  3447. .addSource(source.url, source.content)
  3448. .addMapping(col0, source.url, sourceLine, sourceCol);
  3449. col0 += parts[spanIdx].length;
  3450. spanIdx++;
  3451. // assign parts without span or the same span to the previous segment
  3452. while (spanIdx < spans.length && (span === spans[spanIdx] || !spans[spanIdx])) {
  3453. col0 += parts[spanIdx].length;
  3454. spanIdx++;
  3455. }
  3456. }
  3457. });
  3458. return map;
  3459. }
  3460. spanOf(line, column) {
  3461. const emittedLine = this._lines[line];
  3462. if (emittedLine) {
  3463. let columnsLeft = column - _createIndent(emittedLine.indent).length;
  3464. for (let partIndex = 0; partIndex < emittedLine.parts.length; partIndex++) {
  3465. const part = emittedLine.parts[partIndex];
  3466. if (part.length > columnsLeft) {
  3467. return emittedLine.srcSpans[partIndex];
  3468. }
  3469. columnsLeft -= part.length;
  3470. }
  3471. }
  3472. return null;
  3473. }
  3474. /**
  3475. * @internal strip this from published d.ts files due to
  3476. * https://github.com/microsoft/TypeScript/issues/36216
  3477. */
  3478. get sourceLines() {
  3479. if (this._lines.length && this._lines[this._lines.length - 1].parts.length === 0) {
  3480. return this._lines.slice(0, -1);
  3481. }
  3482. return this._lines;
  3483. }
  3484. }
  3485. class AbstractEmitterVisitor {
  3486. _escapeDollarInStrings;
  3487. constructor(_escapeDollarInStrings) {
  3488. this._escapeDollarInStrings = _escapeDollarInStrings;
  3489. }
  3490. printLeadingComments(stmt, ctx) {
  3491. if (stmt.leadingComments === undefined) {
  3492. return;
  3493. }
  3494. for (const comment of stmt.leadingComments) {
  3495. if (comment instanceof JSDocComment) {
  3496. ctx.print(stmt, `/*${comment.toString()}*/`, comment.trailingNewline);
  3497. }
  3498. else {
  3499. if (comment.multiline) {
  3500. ctx.print(stmt, `/* ${comment.text} */`, comment.trailingNewline);
  3501. }
  3502. else {
  3503. comment.text.split('\n').forEach((line) => {
  3504. ctx.println(stmt, `// ${line}`);
  3505. });
  3506. }
  3507. }
  3508. }
  3509. }
  3510. visitExpressionStmt(stmt, ctx) {
  3511. this.printLeadingComments(stmt, ctx);
  3512. stmt.expr.visitExpression(this, ctx);
  3513. ctx.println(stmt, ';');
  3514. return null;
  3515. }
  3516. visitReturnStmt(stmt, ctx) {
  3517. this.printLeadingComments(stmt, ctx);
  3518. ctx.print(stmt, `return `);
  3519. stmt.value.visitExpression(this, ctx);
  3520. ctx.println(stmt, ';');
  3521. return null;
  3522. }
  3523. visitIfStmt(stmt, ctx) {
  3524. this.printLeadingComments(stmt, ctx);
  3525. ctx.print(stmt, `if (`);
  3526. stmt.condition.visitExpression(this, ctx);
  3527. ctx.print(stmt, `) {`);
  3528. const hasElseCase = stmt.falseCase != null && stmt.falseCase.length > 0;
  3529. if (stmt.trueCase.length <= 1 && !hasElseCase) {
  3530. ctx.print(stmt, ` `);
  3531. this.visitAllStatements(stmt.trueCase, ctx);
  3532. ctx.removeEmptyLastLine();
  3533. ctx.print(stmt, ` `);
  3534. }
  3535. else {
  3536. ctx.println();
  3537. ctx.incIndent();
  3538. this.visitAllStatements(stmt.trueCase, ctx);
  3539. ctx.decIndent();
  3540. if (hasElseCase) {
  3541. ctx.println(stmt, `} else {`);
  3542. ctx.incIndent();
  3543. this.visitAllStatements(stmt.falseCase, ctx);
  3544. ctx.decIndent();
  3545. }
  3546. }
  3547. ctx.println(stmt, `}`);
  3548. return null;
  3549. }
  3550. visitWriteVarExpr(expr, ctx) {
  3551. const lineWasEmpty = ctx.lineIsEmpty();
  3552. if (!lineWasEmpty) {
  3553. ctx.print(expr, '(');
  3554. }
  3555. ctx.print(expr, `${expr.name} = `);
  3556. expr.value.visitExpression(this, ctx);
  3557. if (!lineWasEmpty) {
  3558. ctx.print(expr, ')');
  3559. }
  3560. return null;
  3561. }
  3562. visitWriteKeyExpr(expr, ctx) {
  3563. const lineWasEmpty = ctx.lineIsEmpty();
  3564. if (!lineWasEmpty) {
  3565. ctx.print(expr, '(');
  3566. }
  3567. expr.receiver.visitExpression(this, ctx);
  3568. ctx.print(expr, `[`);
  3569. expr.index.visitExpression(this, ctx);
  3570. ctx.print(expr, `] = `);
  3571. expr.value.visitExpression(this, ctx);
  3572. if (!lineWasEmpty) {
  3573. ctx.print(expr, ')');
  3574. }
  3575. return null;
  3576. }
  3577. visitWritePropExpr(expr, ctx) {
  3578. const lineWasEmpty = ctx.lineIsEmpty();
  3579. if (!lineWasEmpty) {
  3580. ctx.print(expr, '(');
  3581. }
  3582. expr.receiver.visitExpression(this, ctx);
  3583. ctx.print(expr, `.${expr.name} = `);
  3584. expr.value.visitExpression(this, ctx);
  3585. if (!lineWasEmpty) {
  3586. ctx.print(expr, ')');
  3587. }
  3588. return null;
  3589. }
  3590. visitInvokeFunctionExpr(expr, ctx) {
  3591. const shouldParenthesize = expr.fn instanceof ArrowFunctionExpr;
  3592. if (shouldParenthesize) {
  3593. ctx.print(expr.fn, '(');
  3594. }
  3595. expr.fn.visitExpression(this, ctx);
  3596. if (shouldParenthesize) {
  3597. ctx.print(expr.fn, ')');
  3598. }
  3599. ctx.print(expr, `(`);
  3600. this.visitAllExpressions(expr.args, ctx, ',');
  3601. ctx.print(expr, `)`);
  3602. return null;
  3603. }
  3604. visitTaggedTemplateLiteralExpr(expr, ctx) {
  3605. expr.tag.visitExpression(this, ctx);
  3606. expr.template.visitExpression(this, ctx);
  3607. return null;
  3608. }
  3609. visitTemplateLiteralExpr(expr, ctx) {
  3610. ctx.print(expr, '`');
  3611. for (let i = 0; i < expr.elements.length; i++) {
  3612. expr.elements[i].visitExpression(this, ctx);
  3613. const expression = i < expr.expressions.length ? expr.expressions[i] : null;
  3614. if (expression !== null) {
  3615. ctx.print(expression, '${');
  3616. expression.visitExpression(this, ctx);
  3617. ctx.print(expression, '}');
  3618. }
  3619. }
  3620. ctx.print(expr, '`');
  3621. }
  3622. visitTemplateLiteralElementExpr(expr, ctx) {
  3623. ctx.print(expr, expr.rawText);
  3624. }
  3625. visitWrappedNodeExpr(ast, ctx) {
  3626. throw new Error('Abstract emitter cannot visit WrappedNodeExpr.');
  3627. }
  3628. visitTypeofExpr(expr, ctx) {
  3629. ctx.print(expr, 'typeof ');
  3630. expr.expr.visitExpression(this, ctx);
  3631. }
  3632. visitReadVarExpr(ast, ctx) {
  3633. ctx.print(ast, ast.name);
  3634. return null;
  3635. }
  3636. visitInstantiateExpr(ast, ctx) {
  3637. ctx.print(ast, `new `);
  3638. ast.classExpr.visitExpression(this, ctx);
  3639. ctx.print(ast, `(`);
  3640. this.visitAllExpressions(ast.args, ctx, ',');
  3641. ctx.print(ast, `)`);
  3642. return null;
  3643. }
  3644. visitLiteralExpr(ast, ctx) {
  3645. const value = ast.value;
  3646. if (typeof value === 'string') {
  3647. ctx.print(ast, escapeIdentifier(value, this._escapeDollarInStrings));
  3648. }
  3649. else {
  3650. ctx.print(ast, `${value}`);
  3651. }
  3652. return null;
  3653. }
  3654. visitLocalizedString(ast, ctx) {
  3655. const head = ast.serializeI18nHead();
  3656. ctx.print(ast, '$localize `' + head.raw);
  3657. for (let i = 1; i < ast.messageParts.length; i++) {
  3658. ctx.print(ast, '${');
  3659. ast.expressions[i - 1].visitExpression(this, ctx);
  3660. ctx.print(ast, `}${ast.serializeI18nTemplatePart(i).raw}`);
  3661. }
  3662. ctx.print(ast, '`');
  3663. return null;
  3664. }
  3665. visitConditionalExpr(ast, ctx) {
  3666. ctx.print(ast, `(`);
  3667. ast.condition.visitExpression(this, ctx);
  3668. ctx.print(ast, '? ');
  3669. ast.trueCase.visitExpression(this, ctx);
  3670. ctx.print(ast, ': ');
  3671. ast.falseCase.visitExpression(this, ctx);
  3672. ctx.print(ast, `)`);
  3673. return null;
  3674. }
  3675. visitDynamicImportExpr(ast, ctx) {
  3676. ctx.print(ast, `import(${ast.url})`);
  3677. }
  3678. visitNotExpr(ast, ctx) {
  3679. ctx.print(ast, '!');
  3680. ast.condition.visitExpression(this, ctx);
  3681. return null;
  3682. }
  3683. visitUnaryOperatorExpr(ast, ctx) {
  3684. let opStr;
  3685. switch (ast.operator) {
  3686. case UnaryOperator.Plus:
  3687. opStr = '+';
  3688. break;
  3689. case UnaryOperator.Minus:
  3690. opStr = '-';
  3691. break;
  3692. default:
  3693. throw new Error(`Unknown operator ${ast.operator}`);
  3694. }
  3695. if (ast.parens)
  3696. ctx.print(ast, `(`);
  3697. ctx.print(ast, opStr);
  3698. ast.expr.visitExpression(this, ctx);
  3699. if (ast.parens)
  3700. ctx.print(ast, `)`);
  3701. return null;
  3702. }
  3703. visitBinaryOperatorExpr(ast, ctx) {
  3704. let opStr;
  3705. switch (ast.operator) {
  3706. case BinaryOperator.Equals:
  3707. opStr = '==';
  3708. break;
  3709. case BinaryOperator.Identical:
  3710. opStr = '===';
  3711. break;
  3712. case BinaryOperator.NotEquals:
  3713. opStr = '!=';
  3714. break;
  3715. case BinaryOperator.NotIdentical:
  3716. opStr = '!==';
  3717. break;
  3718. case BinaryOperator.And:
  3719. opStr = '&&';
  3720. break;
  3721. case BinaryOperator.BitwiseOr:
  3722. opStr = '|';
  3723. break;
  3724. case BinaryOperator.BitwiseAnd:
  3725. opStr = '&';
  3726. break;
  3727. case BinaryOperator.Or:
  3728. opStr = '||';
  3729. break;
  3730. case BinaryOperator.Plus:
  3731. opStr = '+';
  3732. break;
  3733. case BinaryOperator.Minus:
  3734. opStr = '-';
  3735. break;
  3736. case BinaryOperator.Divide:
  3737. opStr = '/';
  3738. break;
  3739. case BinaryOperator.Multiply:
  3740. opStr = '*';
  3741. break;
  3742. case BinaryOperator.Modulo:
  3743. opStr = '%';
  3744. break;
  3745. case BinaryOperator.Lower:
  3746. opStr = '<';
  3747. break;
  3748. case BinaryOperator.LowerEquals:
  3749. opStr = '<=';
  3750. break;
  3751. case BinaryOperator.Bigger:
  3752. opStr = '>';
  3753. break;
  3754. case BinaryOperator.BiggerEquals:
  3755. opStr = '>=';
  3756. break;
  3757. case BinaryOperator.NullishCoalesce:
  3758. opStr = '??';
  3759. break;
  3760. default:
  3761. throw new Error(`Unknown operator ${ast.operator}`);
  3762. }
  3763. if (ast.parens)
  3764. ctx.print(ast, `(`);
  3765. ast.lhs.visitExpression(this, ctx);
  3766. ctx.print(ast, ` ${opStr} `);
  3767. ast.rhs.visitExpression(this, ctx);
  3768. if (ast.parens)
  3769. ctx.print(ast, `)`);
  3770. return null;
  3771. }
  3772. visitReadPropExpr(ast, ctx) {
  3773. ast.receiver.visitExpression(this, ctx);
  3774. ctx.print(ast, `.`);
  3775. ctx.print(ast, ast.name);
  3776. return null;
  3777. }
  3778. visitReadKeyExpr(ast, ctx) {
  3779. ast.receiver.visitExpression(this, ctx);
  3780. ctx.print(ast, `[`);
  3781. ast.index.visitExpression(this, ctx);
  3782. ctx.print(ast, `]`);
  3783. return null;
  3784. }
  3785. visitLiteralArrayExpr(ast, ctx) {
  3786. ctx.print(ast, `[`);
  3787. this.visitAllExpressions(ast.entries, ctx, ',');
  3788. ctx.print(ast, `]`);
  3789. return null;
  3790. }
  3791. visitLiteralMapExpr(ast, ctx) {
  3792. ctx.print(ast, `{`);
  3793. this.visitAllObjects((entry) => {
  3794. ctx.print(ast, `${escapeIdentifier(entry.key, this._escapeDollarInStrings, entry.quoted)}:`);
  3795. entry.value.visitExpression(this, ctx);
  3796. }, ast.entries, ctx, ',');
  3797. ctx.print(ast, `}`);
  3798. return null;
  3799. }
  3800. visitCommaExpr(ast, ctx) {
  3801. ctx.print(ast, '(');
  3802. this.visitAllExpressions(ast.parts, ctx, ',');
  3803. ctx.print(ast, ')');
  3804. return null;
  3805. }
  3806. visitAllExpressions(expressions, ctx, separator) {
  3807. this.visitAllObjects((expr) => expr.visitExpression(this, ctx), expressions, ctx, separator);
  3808. }
  3809. visitAllObjects(handler, expressions, ctx, separator) {
  3810. let incrementedIndent = false;
  3811. for (let i = 0; i < expressions.length; i++) {
  3812. if (i > 0) {
  3813. if (ctx.lineLength() > 80) {
  3814. ctx.print(null, separator, true);
  3815. if (!incrementedIndent) {
  3816. // continuation are marked with double indent.
  3817. ctx.incIndent();
  3818. ctx.incIndent();
  3819. incrementedIndent = true;
  3820. }
  3821. }
  3822. else {
  3823. ctx.print(null, separator, false);
  3824. }
  3825. }
  3826. handler(expressions[i]);
  3827. }
  3828. if (incrementedIndent) {
  3829. // continuation are marked with double indent.
  3830. ctx.decIndent();
  3831. ctx.decIndent();
  3832. }
  3833. }
  3834. visitAllStatements(statements, ctx) {
  3835. statements.forEach((stmt) => stmt.visitStatement(this, ctx));
  3836. }
  3837. }
  3838. function escapeIdentifier(input, escapeDollar, alwaysQuote = true) {
  3839. if (input == null) {
  3840. return null;
  3841. }
  3842. const body = input.replace(_SINGLE_QUOTE_ESCAPE_STRING_RE, (...match) => {
  3843. if (match[0] == '$') {
  3844. return escapeDollar ? '\\$' : '$';
  3845. }
  3846. else if (match[0] == '\n') {
  3847. return '\\n';
  3848. }
  3849. else if (match[0] == '\r') {
  3850. return '\\r';
  3851. }
  3852. else {
  3853. return `\\${match[0]}`;
  3854. }
  3855. });
  3856. const requiresQuotes = alwaysQuote || !_LEGAL_IDENTIFIER_RE.test(body);
  3857. return requiresQuotes ? `'${body}'` : body;
  3858. }
  3859. function _createIndent(count) {
  3860. let res = '';
  3861. for (let i = 0; i < count; i++) {
  3862. res += _INDENT_WITH;
  3863. }
  3864. return res;
  3865. }
  3866. function typeWithParameters(type, numParams) {
  3867. if (numParams === 0) {
  3868. return expressionType(type);
  3869. }
  3870. const params = [];
  3871. for (let i = 0; i < numParams; i++) {
  3872. params.push(DYNAMIC_TYPE);
  3873. }
  3874. return expressionType(type, undefined, params);
  3875. }
  3876. function getSafePropertyAccessString(accessor, name) {
  3877. const escapedName = escapeIdentifier(name, false, false);
  3878. return escapedName !== name ? `${accessor}[${escapedName}]` : `${accessor}.${name}`;
  3879. }
  3880. function jitOnlyGuardedExpression(expr) {
  3881. return guardedExpression('ngJitMode', expr);
  3882. }
  3883. function devOnlyGuardedExpression(expr) {
  3884. return guardedExpression('ngDevMode', expr);
  3885. }
  3886. function guardedExpression(guard, expr) {
  3887. const guardExpr = new ExternalExpr({ name: guard, moduleName: null });
  3888. const guardNotDefined = new BinaryOperatorExpr(BinaryOperator.Identical, new TypeofExpr(guardExpr), literal('undefined'));
  3889. const guardUndefinedOrTrue = new BinaryOperatorExpr(BinaryOperator.Or, guardNotDefined, guardExpr,
  3890. /* type */ undefined,
  3891. /* sourceSpan */ undefined, true);
  3892. return new BinaryOperatorExpr(BinaryOperator.And, guardUndefinedOrTrue, expr);
  3893. }
  3894. function wrapReference(value) {
  3895. const wrapped = new WrappedNodeExpr(value);
  3896. return { value: wrapped, type: wrapped };
  3897. }
  3898. function refsToArray(refs, shouldForwardDeclare) {
  3899. const values = literalArr(refs.map((ref) => ref.value));
  3900. return shouldForwardDeclare ? arrowFn([], values) : values;
  3901. }
  3902. function createMayBeForwardRefExpression(expression, forwardRef) {
  3903. return { expression, forwardRef };
  3904. }
  3905. /**
  3906. * Convert a `MaybeForwardRefExpression` to an `Expression`, possibly wrapping its expression in a
  3907. * `forwardRef()` call.
  3908. *
  3909. * If `MaybeForwardRefExpression.forwardRef` is `ForwardRefHandling.Unwrapped` then the expression
  3910. * was originally wrapped in a `forwardRef()` call to prevent the value from being eagerly evaluated
  3911. * in the code.
  3912. *
  3913. * See `packages/compiler-cli/src/ngtsc/annotations/src/injectable.ts` and
  3914. * `packages/compiler/src/jit_compiler_facade.ts` for more information.
  3915. */
  3916. function convertFromMaybeForwardRefExpression({ expression, forwardRef, }) {
  3917. switch (forwardRef) {
  3918. case 0 /* ForwardRefHandling.None */:
  3919. case 1 /* ForwardRefHandling.Wrapped */:
  3920. return expression;
  3921. case 2 /* ForwardRefHandling.Unwrapped */:
  3922. return generateForwardRef(expression);
  3923. }
  3924. }
  3925. /**
  3926. * Generate an expression that has the given `expr` wrapped in the following form:
  3927. *
  3928. * ```ts
  3929. * forwardRef(() => expr)
  3930. * ```
  3931. */
  3932. function generateForwardRef(expr) {
  3933. return importExpr(Identifiers.forwardRef).callFn([arrowFn([], expr)]);
  3934. }
  3935. var R3FactoryDelegateType;
  3936. (function (R3FactoryDelegateType) {
  3937. R3FactoryDelegateType[R3FactoryDelegateType["Class"] = 0] = "Class";
  3938. R3FactoryDelegateType[R3FactoryDelegateType["Function"] = 1] = "Function";
  3939. })(R3FactoryDelegateType || (R3FactoryDelegateType = {}));
  3940. var FactoryTarget$1;
  3941. (function (FactoryTarget) {
  3942. FactoryTarget[FactoryTarget["Directive"] = 0] = "Directive";
  3943. FactoryTarget[FactoryTarget["Component"] = 1] = "Component";
  3944. FactoryTarget[FactoryTarget["Injectable"] = 2] = "Injectable";
  3945. FactoryTarget[FactoryTarget["Pipe"] = 3] = "Pipe";
  3946. FactoryTarget[FactoryTarget["NgModule"] = 4] = "NgModule";
  3947. })(FactoryTarget$1 || (FactoryTarget$1 = {}));
  3948. /**
  3949. * Construct a factory function expression for the given `R3FactoryMetadata`.
  3950. */
  3951. function compileFactoryFunction(meta) {
  3952. const t = variable('__ngFactoryType__');
  3953. let baseFactoryVar = null;
  3954. // The type to instantiate via constructor invocation. If there is no delegated factory, meaning
  3955. // this type is always created by constructor invocation, then this is the type-to-create
  3956. // parameter provided by the user (t) if specified, or the current type if not. If there is a
  3957. // delegated factory (which is used to create the current type) then this is only the type-to-
  3958. // create parameter (t).
  3959. const typeForCtor = !isDelegatedFactoryMetadata(meta)
  3960. ? new BinaryOperatorExpr(BinaryOperator.Or, t, meta.type.value)
  3961. : t;
  3962. let ctorExpr = null;
  3963. if (meta.deps !== null) {
  3964. // There is a constructor (either explicitly or implicitly defined).
  3965. if (meta.deps !== 'invalid') {
  3966. ctorExpr = new InstantiateExpr(typeForCtor, injectDependencies(meta.deps, meta.target));
  3967. }
  3968. }
  3969. else {
  3970. // There is no constructor, use the base class' factory to construct typeForCtor.
  3971. baseFactoryVar = variable(`ɵ${meta.name}_BaseFactory`);
  3972. ctorExpr = baseFactoryVar.callFn([typeForCtor]);
  3973. }
  3974. const body = [];
  3975. let retExpr = null;
  3976. function makeConditionalFactory(nonCtorExpr) {
  3977. const r = variable('__ngConditionalFactory__');
  3978. body.push(r.set(NULL_EXPR).toDeclStmt());
  3979. const ctorStmt = ctorExpr !== null
  3980. ? r.set(ctorExpr).toStmt()
  3981. : importExpr(Identifiers.invalidFactory).callFn([]).toStmt();
  3982. body.push(ifStmt(t, [ctorStmt], [r.set(nonCtorExpr).toStmt()]));
  3983. return r;
  3984. }
  3985. if (isDelegatedFactoryMetadata(meta)) {
  3986. // This type is created with a delegated factory. If a type parameter is not specified, call
  3987. // the factory instead.
  3988. const delegateArgs = injectDependencies(meta.delegateDeps, meta.target);
  3989. // Either call `new delegate(...)` or `delegate(...)` depending on meta.delegateType.
  3990. const factoryExpr = new (meta.delegateType === R3FactoryDelegateType.Class ? InstantiateExpr : InvokeFunctionExpr)(meta.delegate, delegateArgs);
  3991. retExpr = makeConditionalFactory(factoryExpr);
  3992. }
  3993. else if (isExpressionFactoryMetadata(meta)) {
  3994. // TODO(alxhub): decide whether to lower the value here or in the caller
  3995. retExpr = makeConditionalFactory(meta.expression);
  3996. }
  3997. else {
  3998. retExpr = ctorExpr;
  3999. }
  4000. if (retExpr === null) {
  4001. // The expression cannot be formed so render an `ɵɵinvalidFactory()` call.
  4002. body.push(importExpr(Identifiers.invalidFactory).callFn([]).toStmt());
  4003. }
  4004. else if (baseFactoryVar !== null) {
  4005. // This factory uses a base factory, so call `ɵɵgetInheritedFactory()` to compute it.
  4006. const getInheritedFactoryCall = importExpr(Identifiers.getInheritedFactory).callFn([meta.type.value]);
  4007. // Memoize the base factoryFn: `baseFactory || (baseFactory = ɵɵgetInheritedFactory(...))`
  4008. const baseFactory = new BinaryOperatorExpr(BinaryOperator.Or, baseFactoryVar, baseFactoryVar.set(getInheritedFactoryCall));
  4009. body.push(new ReturnStatement(baseFactory.callFn([typeForCtor])));
  4010. }
  4011. else {
  4012. // This is straightforward factory, just return it.
  4013. body.push(new ReturnStatement(retExpr));
  4014. }
  4015. let factoryFn = fn([new FnParam(t.name, DYNAMIC_TYPE)], body, INFERRED_TYPE, undefined, `${meta.name}_Factory`);
  4016. if (baseFactoryVar !== null) {
  4017. // There is a base factory variable so wrap its declaration along with the factory function into
  4018. // an IIFE.
  4019. factoryFn = arrowFn([], [new DeclareVarStmt(baseFactoryVar.name), new ReturnStatement(factoryFn)])
  4020. .callFn([], /* sourceSpan */ undefined, /* pure */ true);
  4021. }
  4022. return {
  4023. expression: factoryFn,
  4024. statements: [],
  4025. type: createFactoryType(meta),
  4026. };
  4027. }
  4028. function createFactoryType(meta) {
  4029. const ctorDepsType = meta.deps !== null && meta.deps !== 'invalid' ? createCtorDepsType(meta.deps) : NONE_TYPE;
  4030. return expressionType(importExpr(Identifiers.FactoryDeclaration, [
  4031. typeWithParameters(meta.type.type, meta.typeArgumentCount),
  4032. ctorDepsType,
  4033. ]));
  4034. }
  4035. function injectDependencies(deps, target) {
  4036. return deps.map((dep, index) => compileInjectDependency(dep, target, index));
  4037. }
  4038. function compileInjectDependency(dep, target, index) {
  4039. // Interpret the dependency according to its resolved type.
  4040. if (dep.token === null) {
  4041. return importExpr(Identifiers.invalidFactoryDep).callFn([literal(index)]);
  4042. }
  4043. else if (dep.attributeNameType === null) {
  4044. // Build up the injection flags according to the metadata.
  4045. const flags = 0 /* InjectFlags.Default */ |
  4046. (dep.self ? 2 /* InjectFlags.Self */ : 0) |
  4047. (dep.skipSelf ? 4 /* InjectFlags.SkipSelf */ : 0) |
  4048. (dep.host ? 1 /* InjectFlags.Host */ : 0) |
  4049. (dep.optional ? 8 /* InjectFlags.Optional */ : 0) |
  4050. (target === FactoryTarget$1.Pipe ? 16 /* InjectFlags.ForPipe */ : 0);
  4051. // If this dependency is optional or otherwise has non-default flags, then additional
  4052. // parameters describing how to inject the dependency must be passed to the inject function
  4053. // that's being used.
  4054. let flagsParam = flags !== 0 /* InjectFlags.Default */ || dep.optional ? literal(flags) : null;
  4055. // Build up the arguments to the injectFn call.
  4056. const injectArgs = [dep.token];
  4057. if (flagsParam) {
  4058. injectArgs.push(flagsParam);
  4059. }
  4060. const injectFn = getInjectFn(target);
  4061. return importExpr(injectFn).callFn(injectArgs);
  4062. }
  4063. else {
  4064. // The `dep.attributeTypeName` value is defined, which indicates that this is an `@Attribute()`
  4065. // type dependency. For the generated JS we still want to use the `dep.token` value in case the
  4066. // name given for the attribute is not a string literal. For example given `@Attribute(foo())`,
  4067. // we want to generate `ɵɵinjectAttribute(foo())`.
  4068. //
  4069. // The `dep.attributeTypeName` is only actually used (in `createCtorDepType()`) to generate
  4070. // typings.
  4071. return importExpr(Identifiers.injectAttribute).callFn([dep.token]);
  4072. }
  4073. }
  4074. function createCtorDepsType(deps) {
  4075. let hasTypes = false;
  4076. const attributeTypes = deps.map((dep) => {
  4077. const type = createCtorDepType(dep);
  4078. if (type !== null) {
  4079. hasTypes = true;
  4080. return type;
  4081. }
  4082. else {
  4083. return literal(null);
  4084. }
  4085. });
  4086. if (hasTypes) {
  4087. return expressionType(literalArr(attributeTypes));
  4088. }
  4089. else {
  4090. return NONE_TYPE;
  4091. }
  4092. }
  4093. function createCtorDepType(dep) {
  4094. const entries = [];
  4095. if (dep.attributeNameType !== null) {
  4096. entries.push({ key: 'attribute', value: dep.attributeNameType, quoted: false });
  4097. }
  4098. if (dep.optional) {
  4099. entries.push({ key: 'optional', value: literal(true), quoted: false });
  4100. }
  4101. if (dep.host) {
  4102. entries.push({ key: 'host', value: literal(true), quoted: false });
  4103. }
  4104. if (dep.self) {
  4105. entries.push({ key: 'self', value: literal(true), quoted: false });
  4106. }
  4107. if (dep.skipSelf) {
  4108. entries.push({ key: 'skipSelf', value: literal(true), quoted: false });
  4109. }
  4110. return entries.length > 0 ? literalMap(entries) : null;
  4111. }
  4112. function isDelegatedFactoryMetadata(meta) {
  4113. return meta.delegateType !== undefined;
  4114. }
  4115. function isExpressionFactoryMetadata(meta) {
  4116. return meta.expression !== undefined;
  4117. }
  4118. function getInjectFn(target) {
  4119. switch (target) {
  4120. case FactoryTarget$1.Component:
  4121. case FactoryTarget$1.Directive:
  4122. case FactoryTarget$1.Pipe:
  4123. return Identifiers.directiveInject;
  4124. case FactoryTarget$1.NgModule:
  4125. case FactoryTarget$1.Injectable:
  4126. default:
  4127. return Identifiers.inject;
  4128. }
  4129. }
  4130. class ParserError {
  4131. input;
  4132. errLocation;
  4133. ctxLocation;
  4134. message;
  4135. constructor(message, input, errLocation, ctxLocation) {
  4136. this.input = input;
  4137. this.errLocation = errLocation;
  4138. this.ctxLocation = ctxLocation;
  4139. this.message = `Parser Error: ${message} ${errLocation} [${input}] in ${ctxLocation}`;
  4140. }
  4141. }
  4142. class ParseSpan {
  4143. start;
  4144. end;
  4145. constructor(start, end) {
  4146. this.start = start;
  4147. this.end = end;
  4148. }
  4149. toAbsolute(absoluteOffset) {
  4150. return new AbsoluteSourceSpan(absoluteOffset + this.start, absoluteOffset + this.end);
  4151. }
  4152. }
  4153. class AST {
  4154. span;
  4155. sourceSpan;
  4156. constructor(span,
  4157. /**
  4158. * Absolute location of the expression AST in a source code file.
  4159. */
  4160. sourceSpan) {
  4161. this.span = span;
  4162. this.sourceSpan = sourceSpan;
  4163. }
  4164. toString() {
  4165. return 'AST';
  4166. }
  4167. }
  4168. class ASTWithName extends AST {
  4169. nameSpan;
  4170. constructor(span, sourceSpan, nameSpan) {
  4171. super(span, sourceSpan);
  4172. this.nameSpan = nameSpan;
  4173. }
  4174. }
  4175. let EmptyExpr$1 = class EmptyExpr extends AST {
  4176. visit(visitor, context = null) {
  4177. // do nothing
  4178. }
  4179. };
  4180. class ImplicitReceiver extends AST {
  4181. visit(visitor, context = null) {
  4182. return visitor.visitImplicitReceiver(this, context);
  4183. }
  4184. }
  4185. /**
  4186. * Receiver when something is accessed through `this` (e.g. `this.foo`). Note that this class
  4187. * inherits from `ImplicitReceiver`, because accessing something through `this` is treated the
  4188. * same as accessing it implicitly inside of an Angular template (e.g. `[attr.title]="this.title"`
  4189. * is the same as `[attr.title]="title"`.). Inheriting allows for the `this` accesses to be treated
  4190. * the same as implicit ones, except for a couple of exceptions like `$event` and `$any`.
  4191. * TODO: we should find a way for this class not to extend from `ImplicitReceiver` in the future.
  4192. */
  4193. class ThisReceiver extends ImplicitReceiver {
  4194. visit(visitor, context = null) {
  4195. return visitor.visitThisReceiver?.(this, context);
  4196. }
  4197. }
  4198. /**
  4199. * Multiple expressions separated by a semicolon.
  4200. */
  4201. class Chain extends AST {
  4202. expressions;
  4203. constructor(span, sourceSpan, expressions) {
  4204. super(span, sourceSpan);
  4205. this.expressions = expressions;
  4206. }
  4207. visit(visitor, context = null) {
  4208. return visitor.visitChain(this, context);
  4209. }
  4210. }
  4211. class Conditional extends AST {
  4212. condition;
  4213. trueExp;
  4214. falseExp;
  4215. constructor(span, sourceSpan, condition, trueExp, falseExp) {
  4216. super(span, sourceSpan);
  4217. this.condition = condition;
  4218. this.trueExp = trueExp;
  4219. this.falseExp = falseExp;
  4220. }
  4221. visit(visitor, context = null) {
  4222. return visitor.visitConditional(this, context);
  4223. }
  4224. }
  4225. class PropertyRead extends ASTWithName {
  4226. receiver;
  4227. name;
  4228. constructor(span, sourceSpan, nameSpan, receiver, name) {
  4229. super(span, sourceSpan, nameSpan);
  4230. this.receiver = receiver;
  4231. this.name = name;
  4232. }
  4233. visit(visitor, context = null) {
  4234. return visitor.visitPropertyRead(this, context);
  4235. }
  4236. }
  4237. class PropertyWrite extends ASTWithName {
  4238. receiver;
  4239. name;
  4240. value;
  4241. constructor(span, sourceSpan, nameSpan, receiver, name, value) {
  4242. super(span, sourceSpan, nameSpan);
  4243. this.receiver = receiver;
  4244. this.name = name;
  4245. this.value = value;
  4246. }
  4247. visit(visitor, context = null) {
  4248. return visitor.visitPropertyWrite(this, context);
  4249. }
  4250. }
  4251. class SafePropertyRead extends ASTWithName {
  4252. receiver;
  4253. name;
  4254. constructor(span, sourceSpan, nameSpan, receiver, name) {
  4255. super(span, sourceSpan, nameSpan);
  4256. this.receiver = receiver;
  4257. this.name = name;
  4258. }
  4259. visit(visitor, context = null) {
  4260. return visitor.visitSafePropertyRead(this, context);
  4261. }
  4262. }
  4263. class KeyedRead extends AST {
  4264. receiver;
  4265. key;
  4266. constructor(span, sourceSpan, receiver, key) {
  4267. super(span, sourceSpan);
  4268. this.receiver = receiver;
  4269. this.key = key;
  4270. }
  4271. visit(visitor, context = null) {
  4272. return visitor.visitKeyedRead(this, context);
  4273. }
  4274. }
  4275. class SafeKeyedRead extends AST {
  4276. receiver;
  4277. key;
  4278. constructor(span, sourceSpan, receiver, key) {
  4279. super(span, sourceSpan);
  4280. this.receiver = receiver;
  4281. this.key = key;
  4282. }
  4283. visit(visitor, context = null) {
  4284. return visitor.visitSafeKeyedRead(this, context);
  4285. }
  4286. }
  4287. class KeyedWrite extends AST {
  4288. receiver;
  4289. key;
  4290. value;
  4291. constructor(span, sourceSpan, receiver, key, value) {
  4292. super(span, sourceSpan);
  4293. this.receiver = receiver;
  4294. this.key = key;
  4295. this.value = value;
  4296. }
  4297. visit(visitor, context = null) {
  4298. return visitor.visitKeyedWrite(this, context);
  4299. }
  4300. }
  4301. class BindingPipe extends ASTWithName {
  4302. exp;
  4303. name;
  4304. args;
  4305. constructor(span, sourceSpan, exp, name, args, nameSpan) {
  4306. super(span, sourceSpan, nameSpan);
  4307. this.exp = exp;
  4308. this.name = name;
  4309. this.args = args;
  4310. }
  4311. visit(visitor, context = null) {
  4312. return visitor.visitPipe(this, context);
  4313. }
  4314. }
  4315. class LiteralPrimitive extends AST {
  4316. value;
  4317. constructor(span, sourceSpan, value) {
  4318. super(span, sourceSpan);
  4319. this.value = value;
  4320. }
  4321. visit(visitor, context = null) {
  4322. return visitor.visitLiteralPrimitive(this, context);
  4323. }
  4324. }
  4325. class LiteralArray extends AST {
  4326. expressions;
  4327. constructor(span, sourceSpan, expressions) {
  4328. super(span, sourceSpan);
  4329. this.expressions = expressions;
  4330. }
  4331. visit(visitor, context = null) {
  4332. return visitor.visitLiteralArray(this, context);
  4333. }
  4334. }
  4335. class LiteralMap extends AST {
  4336. keys;
  4337. values;
  4338. constructor(span, sourceSpan, keys, values) {
  4339. super(span, sourceSpan);
  4340. this.keys = keys;
  4341. this.values = values;
  4342. }
  4343. visit(visitor, context = null) {
  4344. return visitor.visitLiteralMap(this, context);
  4345. }
  4346. }
  4347. let Interpolation$1 = class Interpolation extends AST {
  4348. strings;
  4349. expressions;
  4350. constructor(span, sourceSpan, strings, expressions) {
  4351. super(span, sourceSpan);
  4352. this.strings = strings;
  4353. this.expressions = expressions;
  4354. }
  4355. visit(visitor, context = null) {
  4356. return visitor.visitInterpolation(this, context);
  4357. }
  4358. };
  4359. class Binary extends AST {
  4360. operation;
  4361. left;
  4362. right;
  4363. constructor(span, sourceSpan, operation, left, right) {
  4364. super(span, sourceSpan);
  4365. this.operation = operation;
  4366. this.left = left;
  4367. this.right = right;
  4368. }
  4369. visit(visitor, context = null) {
  4370. return visitor.visitBinary(this, context);
  4371. }
  4372. }
  4373. /**
  4374. * For backwards compatibility reasons, `Unary` inherits from `Binary` and mimics the binary AST
  4375. * node that was originally used. This inheritance relation can be deleted in some future major,
  4376. * after consumers have been given a chance to fully support Unary.
  4377. */
  4378. class Unary extends Binary {
  4379. operator;
  4380. expr;
  4381. // Redeclare the properties that are inherited from `Binary` as `never`, as consumers should not
  4382. // depend on these fields when operating on `Unary`.
  4383. left = null;
  4384. right = null;
  4385. operation = null;
  4386. /**
  4387. * Creates a unary minus expression "-x", represented as `Binary` using "0 - x".
  4388. */
  4389. static createMinus(span, sourceSpan, expr) {
  4390. return new Unary(span, sourceSpan, '-', expr, '-', new LiteralPrimitive(span, sourceSpan, 0), expr);
  4391. }
  4392. /**
  4393. * Creates a unary plus expression "+x", represented as `Binary` using "x - 0".
  4394. */
  4395. static createPlus(span, sourceSpan, expr) {
  4396. return new Unary(span, sourceSpan, '+', expr, '-', expr, new LiteralPrimitive(span, sourceSpan, 0));
  4397. }
  4398. /**
  4399. * During the deprecation period this constructor is private, to avoid consumers from creating
  4400. * a `Unary` with the fallback properties for `Binary`.
  4401. */
  4402. constructor(span, sourceSpan, operator, expr, binaryOp, binaryLeft, binaryRight) {
  4403. super(span, sourceSpan, binaryOp, binaryLeft, binaryRight);
  4404. this.operator = operator;
  4405. this.expr = expr;
  4406. }
  4407. visit(visitor, context = null) {
  4408. if (visitor.visitUnary !== undefined) {
  4409. return visitor.visitUnary(this, context);
  4410. }
  4411. return visitor.visitBinary(this, context);
  4412. }
  4413. }
  4414. class PrefixNot extends AST {
  4415. expression;
  4416. constructor(span, sourceSpan, expression) {
  4417. super(span, sourceSpan);
  4418. this.expression = expression;
  4419. }
  4420. visit(visitor, context = null) {
  4421. return visitor.visitPrefixNot(this, context);
  4422. }
  4423. }
  4424. class TypeofExpression extends AST {
  4425. expression;
  4426. constructor(span, sourceSpan, expression) {
  4427. super(span, sourceSpan);
  4428. this.expression = expression;
  4429. }
  4430. visit(visitor, context = null) {
  4431. return visitor.visitTypeofExpression(this, context);
  4432. }
  4433. }
  4434. class NonNullAssert extends AST {
  4435. expression;
  4436. constructor(span, sourceSpan, expression) {
  4437. super(span, sourceSpan);
  4438. this.expression = expression;
  4439. }
  4440. visit(visitor, context = null) {
  4441. return visitor.visitNonNullAssert(this, context);
  4442. }
  4443. }
  4444. class Call extends AST {
  4445. receiver;
  4446. args;
  4447. argumentSpan;
  4448. constructor(span, sourceSpan, receiver, args, argumentSpan) {
  4449. super(span, sourceSpan);
  4450. this.receiver = receiver;
  4451. this.args = args;
  4452. this.argumentSpan = argumentSpan;
  4453. }
  4454. visit(visitor, context = null) {
  4455. return visitor.visitCall(this, context);
  4456. }
  4457. }
  4458. class SafeCall extends AST {
  4459. receiver;
  4460. args;
  4461. argumentSpan;
  4462. constructor(span, sourceSpan, receiver, args, argumentSpan) {
  4463. super(span, sourceSpan);
  4464. this.receiver = receiver;
  4465. this.args = args;
  4466. this.argumentSpan = argumentSpan;
  4467. }
  4468. visit(visitor, context = null) {
  4469. return visitor.visitSafeCall(this, context);
  4470. }
  4471. }
  4472. class TemplateLiteral extends AST {
  4473. elements;
  4474. expressions;
  4475. constructor(span, sourceSpan, elements, expressions) {
  4476. super(span, sourceSpan);
  4477. this.elements = elements;
  4478. this.expressions = expressions;
  4479. }
  4480. visit(visitor, context) {
  4481. return visitor.visitTemplateLiteral(this, context);
  4482. }
  4483. }
  4484. class TemplateLiteralElement extends AST {
  4485. text;
  4486. constructor(span, sourceSpan, text) {
  4487. super(span, sourceSpan);
  4488. this.text = text;
  4489. }
  4490. visit(visitor, context) {
  4491. return visitor.visitTemplateLiteralElement(this, context);
  4492. }
  4493. }
  4494. /**
  4495. * Records the absolute position of a text span in a source file, where `start` and `end` are the
  4496. * starting and ending byte offsets, respectively, of the text span in a source file.
  4497. */
  4498. class AbsoluteSourceSpan {
  4499. start;
  4500. end;
  4501. constructor(start, end) {
  4502. this.start = start;
  4503. this.end = end;
  4504. }
  4505. }
  4506. class ASTWithSource extends AST {
  4507. ast;
  4508. source;
  4509. location;
  4510. errors;
  4511. constructor(ast, source, location, absoluteOffset, errors) {
  4512. super(new ParseSpan(0, source === null ? 0 : source.length), new AbsoluteSourceSpan(absoluteOffset, source === null ? absoluteOffset : absoluteOffset + source.length));
  4513. this.ast = ast;
  4514. this.source = source;
  4515. this.location = location;
  4516. this.errors = errors;
  4517. }
  4518. visit(visitor, context = null) {
  4519. if (visitor.visitASTWithSource) {
  4520. return visitor.visitASTWithSource(this, context);
  4521. }
  4522. return this.ast.visit(visitor, context);
  4523. }
  4524. toString() {
  4525. return `${this.source} in ${this.location}`;
  4526. }
  4527. }
  4528. class VariableBinding {
  4529. sourceSpan;
  4530. key;
  4531. value;
  4532. /**
  4533. * @param sourceSpan entire span of the binding.
  4534. * @param key name of the LHS along with its span.
  4535. * @param value optional value for the RHS along with its span.
  4536. */
  4537. constructor(sourceSpan, key, value) {
  4538. this.sourceSpan = sourceSpan;
  4539. this.key = key;
  4540. this.value = value;
  4541. }
  4542. }
  4543. class ExpressionBinding {
  4544. sourceSpan;
  4545. key;
  4546. value;
  4547. /**
  4548. * @param sourceSpan entire span of the binding.
  4549. * @param key binding name, like ngForOf, ngForTrackBy, ngIf, along with its
  4550. * span. Note that the length of the span may not be the same as
  4551. * `key.source.length`. For example,
  4552. * 1. key.source = ngFor, key.span is for "ngFor"
  4553. * 2. key.source = ngForOf, key.span is for "of"
  4554. * 3. key.source = ngForTrackBy, key.span is for "trackBy"
  4555. * @param value optional expression for the RHS.
  4556. */
  4557. constructor(sourceSpan, key, value) {
  4558. this.sourceSpan = sourceSpan;
  4559. this.key = key;
  4560. this.value = value;
  4561. }
  4562. }
  4563. class RecursiveAstVisitor {
  4564. visit(ast, context) {
  4565. // The default implementation just visits every node.
  4566. // Classes that extend RecursiveAstVisitor should override this function
  4567. // to selectively visit the specified node.
  4568. ast.visit(this, context);
  4569. }
  4570. visitUnary(ast, context) {
  4571. this.visit(ast.expr, context);
  4572. }
  4573. visitBinary(ast, context) {
  4574. this.visit(ast.left, context);
  4575. this.visit(ast.right, context);
  4576. }
  4577. visitChain(ast, context) {
  4578. this.visitAll(ast.expressions, context);
  4579. }
  4580. visitConditional(ast, context) {
  4581. this.visit(ast.condition, context);
  4582. this.visit(ast.trueExp, context);
  4583. this.visit(ast.falseExp, context);
  4584. }
  4585. visitPipe(ast, context) {
  4586. this.visit(ast.exp, context);
  4587. this.visitAll(ast.args, context);
  4588. }
  4589. visitImplicitReceiver(ast, context) { }
  4590. visitThisReceiver(ast, context) { }
  4591. visitInterpolation(ast, context) {
  4592. this.visitAll(ast.expressions, context);
  4593. }
  4594. visitKeyedRead(ast, context) {
  4595. this.visit(ast.receiver, context);
  4596. this.visit(ast.key, context);
  4597. }
  4598. visitKeyedWrite(ast, context) {
  4599. this.visit(ast.receiver, context);
  4600. this.visit(ast.key, context);
  4601. this.visit(ast.value, context);
  4602. }
  4603. visitLiteralArray(ast, context) {
  4604. this.visitAll(ast.expressions, context);
  4605. }
  4606. visitLiteralMap(ast, context) {
  4607. this.visitAll(ast.values, context);
  4608. }
  4609. visitLiteralPrimitive(ast, context) { }
  4610. visitPrefixNot(ast, context) {
  4611. this.visit(ast.expression, context);
  4612. }
  4613. visitTypeofExpression(ast, context) {
  4614. this.visit(ast.expression, context);
  4615. }
  4616. visitNonNullAssert(ast, context) {
  4617. this.visit(ast.expression, context);
  4618. }
  4619. visitPropertyRead(ast, context) {
  4620. this.visit(ast.receiver, context);
  4621. }
  4622. visitPropertyWrite(ast, context) {
  4623. this.visit(ast.receiver, context);
  4624. this.visit(ast.value, context);
  4625. }
  4626. visitSafePropertyRead(ast, context) {
  4627. this.visit(ast.receiver, context);
  4628. }
  4629. visitSafeKeyedRead(ast, context) {
  4630. this.visit(ast.receiver, context);
  4631. this.visit(ast.key, context);
  4632. }
  4633. visitCall(ast, context) {
  4634. this.visit(ast.receiver, context);
  4635. this.visitAll(ast.args, context);
  4636. }
  4637. visitSafeCall(ast, context) {
  4638. this.visit(ast.receiver, context);
  4639. this.visitAll(ast.args, context);
  4640. }
  4641. visitTemplateLiteral(ast, context) {
  4642. // Iterate in the declaration order. Note that there will
  4643. // always be one expression less than the number of elements.
  4644. for (let i = 0; i < ast.elements.length; i++) {
  4645. this.visit(ast.elements[i], context);
  4646. const expression = i < ast.expressions.length ? ast.expressions[i] : null;
  4647. if (expression !== null) {
  4648. this.visit(expression, context);
  4649. }
  4650. }
  4651. }
  4652. visitTemplateLiteralElement(ast, context) { }
  4653. // This is not part of the AstVisitor interface, just a helper method
  4654. visitAll(asts, context) {
  4655. for (const ast of asts) {
  4656. this.visit(ast, context);
  4657. }
  4658. }
  4659. }
  4660. // Bindings
  4661. class ParsedProperty {
  4662. name;
  4663. expression;
  4664. type;
  4665. sourceSpan;
  4666. keySpan;
  4667. valueSpan;
  4668. isLiteral;
  4669. isAnimation;
  4670. constructor(name, expression, type, sourceSpan, keySpan, valueSpan) {
  4671. this.name = name;
  4672. this.expression = expression;
  4673. this.type = type;
  4674. this.sourceSpan = sourceSpan;
  4675. this.keySpan = keySpan;
  4676. this.valueSpan = valueSpan;
  4677. this.isLiteral = this.type === ParsedPropertyType.LITERAL_ATTR;
  4678. this.isAnimation = this.type === ParsedPropertyType.ANIMATION;
  4679. }
  4680. }
  4681. var ParsedPropertyType;
  4682. (function (ParsedPropertyType) {
  4683. ParsedPropertyType[ParsedPropertyType["DEFAULT"] = 0] = "DEFAULT";
  4684. ParsedPropertyType[ParsedPropertyType["LITERAL_ATTR"] = 1] = "LITERAL_ATTR";
  4685. ParsedPropertyType[ParsedPropertyType["ANIMATION"] = 2] = "ANIMATION";
  4686. ParsedPropertyType[ParsedPropertyType["TWO_WAY"] = 3] = "TWO_WAY";
  4687. })(ParsedPropertyType || (ParsedPropertyType = {}));
  4688. var ParsedEventType;
  4689. (function (ParsedEventType) {
  4690. // DOM or Directive event
  4691. ParsedEventType[ParsedEventType["Regular"] = 0] = "Regular";
  4692. // Animation specific event
  4693. ParsedEventType[ParsedEventType["Animation"] = 1] = "Animation";
  4694. // Event side of a two-way binding (e.g. `[(property)]="expression"`).
  4695. ParsedEventType[ParsedEventType["TwoWay"] = 2] = "TwoWay";
  4696. })(ParsedEventType || (ParsedEventType = {}));
  4697. class ParsedEvent {
  4698. name;
  4699. targetOrPhase;
  4700. type;
  4701. handler;
  4702. sourceSpan;
  4703. handlerSpan;
  4704. keySpan;
  4705. constructor(name, targetOrPhase, type, handler, sourceSpan, handlerSpan, keySpan) {
  4706. this.name = name;
  4707. this.targetOrPhase = targetOrPhase;
  4708. this.type = type;
  4709. this.handler = handler;
  4710. this.sourceSpan = sourceSpan;
  4711. this.handlerSpan = handlerSpan;
  4712. this.keySpan = keySpan;
  4713. }
  4714. }
  4715. /**
  4716. * ParsedVariable represents a variable declaration in a microsyntax expression.
  4717. */
  4718. class ParsedVariable {
  4719. name;
  4720. value;
  4721. sourceSpan;
  4722. keySpan;
  4723. valueSpan;
  4724. constructor(name, value, sourceSpan, keySpan, valueSpan) {
  4725. this.name = name;
  4726. this.value = value;
  4727. this.sourceSpan = sourceSpan;
  4728. this.keySpan = keySpan;
  4729. this.valueSpan = valueSpan;
  4730. }
  4731. }
  4732. var BindingType;
  4733. (function (BindingType) {
  4734. // A regular binding to a property (e.g. `[property]="expression"`).
  4735. BindingType[BindingType["Property"] = 0] = "Property";
  4736. // A binding to an element attribute (e.g. `[attr.name]="expression"`).
  4737. BindingType[BindingType["Attribute"] = 1] = "Attribute";
  4738. // A binding to a CSS class (e.g. `[class.name]="condition"`).
  4739. BindingType[BindingType["Class"] = 2] = "Class";
  4740. // A binding to a style rule (e.g. `[style.rule]="expression"`).
  4741. BindingType[BindingType["Style"] = 3] = "Style";
  4742. // A binding to an animation reference (e.g. `[animate.key]="expression"`).
  4743. BindingType[BindingType["Animation"] = 4] = "Animation";
  4744. // Property side of a two-way binding (e.g. `[(property)]="expression"`).
  4745. BindingType[BindingType["TwoWay"] = 5] = "TwoWay";
  4746. })(BindingType || (BindingType = {}));
  4747. class BoundElementProperty {
  4748. name;
  4749. type;
  4750. securityContext;
  4751. value;
  4752. unit;
  4753. sourceSpan;
  4754. keySpan;
  4755. valueSpan;
  4756. constructor(name, type, securityContext, value, unit, sourceSpan, keySpan, valueSpan) {
  4757. this.name = name;
  4758. this.type = type;
  4759. this.securityContext = securityContext;
  4760. this.value = value;
  4761. this.unit = unit;
  4762. this.sourceSpan = sourceSpan;
  4763. this.keySpan = keySpan;
  4764. this.valueSpan = valueSpan;
  4765. }
  4766. }
  4767. var TagContentType;
  4768. (function (TagContentType) {
  4769. TagContentType[TagContentType["RAW_TEXT"] = 0] = "RAW_TEXT";
  4770. TagContentType[TagContentType["ESCAPABLE_RAW_TEXT"] = 1] = "ESCAPABLE_RAW_TEXT";
  4771. TagContentType[TagContentType["PARSABLE_DATA"] = 2] = "PARSABLE_DATA";
  4772. })(TagContentType || (TagContentType = {}));
  4773. function splitNsName(elementName, fatal = true) {
  4774. if (elementName[0] != ':') {
  4775. return [null, elementName];
  4776. }
  4777. const colonIndex = elementName.indexOf(':', 1);
  4778. if (colonIndex === -1) {
  4779. if (fatal) {
  4780. throw new Error(`Unsupported format "${elementName}" expecting ":namespace:name"`);
  4781. }
  4782. else {
  4783. return [null, elementName];
  4784. }
  4785. }
  4786. return [elementName.slice(1, colonIndex), elementName.slice(colonIndex + 1)];
  4787. }
  4788. // `<ng-container>` tags work the same regardless the namespace
  4789. function isNgContainer(tagName) {
  4790. return splitNsName(tagName)[1] === 'ng-container';
  4791. }
  4792. // `<ng-content>` tags work the same regardless the namespace
  4793. function isNgContent(tagName) {
  4794. return splitNsName(tagName)[1] === 'ng-content';
  4795. }
  4796. // `<ng-template>` tags work the same regardless the namespace
  4797. function isNgTemplate(tagName) {
  4798. return splitNsName(tagName)[1] === 'ng-template';
  4799. }
  4800. function getNsPrefix(fullName) {
  4801. return fullName === null ? null : splitNsName(fullName)[0];
  4802. }
  4803. function mergeNsAndName(prefix, localName) {
  4804. return prefix ? `:${prefix}:${localName}` : localName;
  4805. }
  4806. /**
  4807. * This is an R3 `Node`-like wrapper for a raw `html.Comment` node. We do not currently
  4808. * require the implementation of a visitor for Comments as they are only collected at
  4809. * the top-level of the R3 AST, and only if `Render3ParseOptions['collectCommentNodes']`
  4810. * is true.
  4811. */
  4812. let Comment$1 = class Comment {
  4813. value;
  4814. sourceSpan;
  4815. constructor(value, sourceSpan) {
  4816. this.value = value;
  4817. this.sourceSpan = sourceSpan;
  4818. }
  4819. visit(_visitor) {
  4820. throw new Error('visit() not implemented for Comment');
  4821. }
  4822. };
  4823. let Text$3 = class Text {
  4824. value;
  4825. sourceSpan;
  4826. constructor(value, sourceSpan) {
  4827. this.value = value;
  4828. this.sourceSpan = sourceSpan;
  4829. }
  4830. visit(visitor) {
  4831. return visitor.visitText(this);
  4832. }
  4833. };
  4834. class BoundText {
  4835. value;
  4836. sourceSpan;
  4837. i18n;
  4838. constructor(value, sourceSpan, i18n) {
  4839. this.value = value;
  4840. this.sourceSpan = sourceSpan;
  4841. this.i18n = i18n;
  4842. }
  4843. visit(visitor) {
  4844. return visitor.visitBoundText(this);
  4845. }
  4846. }
  4847. /**
  4848. * Represents a text attribute in the template.
  4849. *
  4850. * `valueSpan` may not be present in cases where there is no value `<div a></div>`.
  4851. * `keySpan` may also not be present for synthetic attributes from ICU expansions.
  4852. */
  4853. class TextAttribute {
  4854. name;
  4855. value;
  4856. sourceSpan;
  4857. keySpan;
  4858. valueSpan;
  4859. i18n;
  4860. constructor(name, value, sourceSpan, keySpan, valueSpan, i18n) {
  4861. this.name = name;
  4862. this.value = value;
  4863. this.sourceSpan = sourceSpan;
  4864. this.keySpan = keySpan;
  4865. this.valueSpan = valueSpan;
  4866. this.i18n = i18n;
  4867. }
  4868. visit(visitor) {
  4869. return visitor.visitTextAttribute(this);
  4870. }
  4871. }
  4872. class BoundAttribute {
  4873. name;
  4874. type;
  4875. securityContext;
  4876. value;
  4877. unit;
  4878. sourceSpan;
  4879. keySpan;
  4880. valueSpan;
  4881. i18n;
  4882. constructor(name, type, securityContext, value, unit, sourceSpan, keySpan, valueSpan, i18n) {
  4883. this.name = name;
  4884. this.type = type;
  4885. this.securityContext = securityContext;
  4886. this.value = value;
  4887. this.unit = unit;
  4888. this.sourceSpan = sourceSpan;
  4889. this.keySpan = keySpan;
  4890. this.valueSpan = valueSpan;
  4891. this.i18n = i18n;
  4892. }
  4893. static fromBoundElementProperty(prop, i18n) {
  4894. if (prop.keySpan === undefined) {
  4895. throw new Error(`Unexpected state: keySpan must be defined for bound attributes but was not for ${prop.name}: ${prop.sourceSpan}`);
  4896. }
  4897. return new BoundAttribute(prop.name, prop.type, prop.securityContext, prop.value, prop.unit, prop.sourceSpan, prop.keySpan, prop.valueSpan, i18n);
  4898. }
  4899. visit(visitor) {
  4900. return visitor.visitBoundAttribute(this);
  4901. }
  4902. }
  4903. class BoundEvent {
  4904. name;
  4905. type;
  4906. handler;
  4907. target;
  4908. phase;
  4909. sourceSpan;
  4910. handlerSpan;
  4911. keySpan;
  4912. constructor(name, type, handler, target, phase, sourceSpan, handlerSpan, keySpan) {
  4913. this.name = name;
  4914. this.type = type;
  4915. this.handler = handler;
  4916. this.target = target;
  4917. this.phase = phase;
  4918. this.sourceSpan = sourceSpan;
  4919. this.handlerSpan = handlerSpan;
  4920. this.keySpan = keySpan;
  4921. }
  4922. static fromParsedEvent(event) {
  4923. const target = event.type === ParsedEventType.Regular ? event.targetOrPhase : null;
  4924. const phase = event.type === ParsedEventType.Animation ? event.targetOrPhase : null;
  4925. if (event.keySpan === undefined) {
  4926. throw new Error(`Unexpected state: keySpan must be defined for bound event but was not for ${event.name}: ${event.sourceSpan}`);
  4927. }
  4928. return new BoundEvent(event.name, event.type, event.handler, target, phase, event.sourceSpan, event.handlerSpan, event.keySpan);
  4929. }
  4930. visit(visitor) {
  4931. return visitor.visitBoundEvent(this);
  4932. }
  4933. }
  4934. let Element$1 = class Element {
  4935. name;
  4936. attributes;
  4937. inputs;
  4938. outputs;
  4939. children;
  4940. references;
  4941. sourceSpan;
  4942. startSourceSpan;
  4943. endSourceSpan;
  4944. i18n;
  4945. constructor(name, attributes, inputs, outputs, children, references, sourceSpan, startSourceSpan, endSourceSpan, i18n) {
  4946. this.name = name;
  4947. this.attributes = attributes;
  4948. this.inputs = inputs;
  4949. this.outputs = outputs;
  4950. this.children = children;
  4951. this.references = references;
  4952. this.sourceSpan = sourceSpan;
  4953. this.startSourceSpan = startSourceSpan;
  4954. this.endSourceSpan = endSourceSpan;
  4955. this.i18n = i18n;
  4956. }
  4957. visit(visitor) {
  4958. return visitor.visitElement(this);
  4959. }
  4960. };
  4961. class DeferredTrigger {
  4962. nameSpan;
  4963. sourceSpan;
  4964. prefetchSpan;
  4965. whenOrOnSourceSpan;
  4966. hydrateSpan;
  4967. constructor(nameSpan, sourceSpan, prefetchSpan, whenOrOnSourceSpan, hydrateSpan) {
  4968. this.nameSpan = nameSpan;
  4969. this.sourceSpan = sourceSpan;
  4970. this.prefetchSpan = prefetchSpan;
  4971. this.whenOrOnSourceSpan = whenOrOnSourceSpan;
  4972. this.hydrateSpan = hydrateSpan;
  4973. }
  4974. visit(visitor) {
  4975. return visitor.visitDeferredTrigger(this);
  4976. }
  4977. }
  4978. class BoundDeferredTrigger extends DeferredTrigger {
  4979. value;
  4980. constructor(value, sourceSpan, prefetchSpan, whenSourceSpan, hydrateSpan) {
  4981. // BoundDeferredTrigger is for 'when' triggers. These aren't really "triggers" and don't have a
  4982. // nameSpan. Trigger names are the built in event triggers like hover, interaction, etc.
  4983. super(/** nameSpan */ null, sourceSpan, prefetchSpan, whenSourceSpan, hydrateSpan);
  4984. this.value = value;
  4985. }
  4986. }
  4987. class NeverDeferredTrigger extends DeferredTrigger {
  4988. }
  4989. class IdleDeferredTrigger extends DeferredTrigger {
  4990. }
  4991. class ImmediateDeferredTrigger extends DeferredTrigger {
  4992. }
  4993. class HoverDeferredTrigger extends DeferredTrigger {
  4994. reference;
  4995. constructor(reference, nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan) {
  4996. super(nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan);
  4997. this.reference = reference;
  4998. }
  4999. }
  5000. class TimerDeferredTrigger extends DeferredTrigger {
  5001. delay;
  5002. constructor(delay, nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan) {
  5003. super(nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan);
  5004. this.delay = delay;
  5005. }
  5006. }
  5007. class InteractionDeferredTrigger extends DeferredTrigger {
  5008. reference;
  5009. constructor(reference, nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan) {
  5010. super(nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan);
  5011. this.reference = reference;
  5012. }
  5013. }
  5014. class ViewportDeferredTrigger extends DeferredTrigger {
  5015. reference;
  5016. constructor(reference, nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan) {
  5017. super(nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan);
  5018. this.reference = reference;
  5019. }
  5020. }
  5021. class BlockNode {
  5022. nameSpan;
  5023. sourceSpan;
  5024. startSourceSpan;
  5025. endSourceSpan;
  5026. constructor(nameSpan, sourceSpan, startSourceSpan, endSourceSpan) {
  5027. this.nameSpan = nameSpan;
  5028. this.sourceSpan = sourceSpan;
  5029. this.startSourceSpan = startSourceSpan;
  5030. this.endSourceSpan = endSourceSpan;
  5031. }
  5032. }
  5033. class DeferredBlockPlaceholder extends BlockNode {
  5034. children;
  5035. minimumTime;
  5036. i18n;
  5037. constructor(children, minimumTime, nameSpan, sourceSpan, startSourceSpan, endSourceSpan, i18n) {
  5038. super(nameSpan, sourceSpan, startSourceSpan, endSourceSpan);
  5039. this.children = children;
  5040. this.minimumTime = minimumTime;
  5041. this.i18n = i18n;
  5042. }
  5043. visit(visitor) {
  5044. return visitor.visitDeferredBlockPlaceholder(this);
  5045. }
  5046. }
  5047. class DeferredBlockLoading extends BlockNode {
  5048. children;
  5049. afterTime;
  5050. minimumTime;
  5051. i18n;
  5052. constructor(children, afterTime, minimumTime, nameSpan, sourceSpan, startSourceSpan, endSourceSpan, i18n) {
  5053. super(nameSpan, sourceSpan, startSourceSpan, endSourceSpan);
  5054. this.children = children;
  5055. this.afterTime = afterTime;
  5056. this.minimumTime = minimumTime;
  5057. this.i18n = i18n;
  5058. }
  5059. visit(visitor) {
  5060. return visitor.visitDeferredBlockLoading(this);
  5061. }
  5062. }
  5063. class DeferredBlockError extends BlockNode {
  5064. children;
  5065. i18n;
  5066. constructor(children, nameSpan, sourceSpan, startSourceSpan, endSourceSpan, i18n) {
  5067. super(nameSpan, sourceSpan, startSourceSpan, endSourceSpan);
  5068. this.children = children;
  5069. this.i18n = i18n;
  5070. }
  5071. visit(visitor) {
  5072. return visitor.visitDeferredBlockError(this);
  5073. }
  5074. }
  5075. class DeferredBlock extends BlockNode {
  5076. children;
  5077. placeholder;
  5078. loading;
  5079. error;
  5080. mainBlockSpan;
  5081. i18n;
  5082. triggers;
  5083. prefetchTriggers;
  5084. hydrateTriggers;
  5085. definedTriggers;
  5086. definedPrefetchTriggers;
  5087. definedHydrateTriggers;
  5088. constructor(children, triggers, prefetchTriggers, hydrateTriggers, placeholder, loading, error, nameSpan, sourceSpan, mainBlockSpan, startSourceSpan, endSourceSpan, i18n) {
  5089. super(nameSpan, sourceSpan, startSourceSpan, endSourceSpan);
  5090. this.children = children;
  5091. this.placeholder = placeholder;
  5092. this.loading = loading;
  5093. this.error = error;
  5094. this.mainBlockSpan = mainBlockSpan;
  5095. this.i18n = i18n;
  5096. this.triggers = triggers;
  5097. this.prefetchTriggers = prefetchTriggers;
  5098. this.hydrateTriggers = hydrateTriggers;
  5099. // We cache the keys since we know that they won't change and we
  5100. // don't want to enumarate them every time we're traversing the AST.
  5101. this.definedTriggers = Object.keys(triggers);
  5102. this.definedPrefetchTriggers = Object.keys(prefetchTriggers);
  5103. this.definedHydrateTriggers = Object.keys(hydrateTriggers);
  5104. }
  5105. visit(visitor) {
  5106. return visitor.visitDeferredBlock(this);
  5107. }
  5108. visitAll(visitor) {
  5109. // Visit the hydrate triggers first to match their insertion order.
  5110. this.visitTriggers(this.definedHydrateTriggers, this.hydrateTriggers, visitor);
  5111. this.visitTriggers(this.definedTriggers, this.triggers, visitor);
  5112. this.visitTriggers(this.definedPrefetchTriggers, this.prefetchTriggers, visitor);
  5113. visitAll$1(visitor, this.children);
  5114. const remainingBlocks = [this.placeholder, this.loading, this.error].filter((x) => x !== null);
  5115. visitAll$1(visitor, remainingBlocks);
  5116. }
  5117. visitTriggers(keys, triggers, visitor) {
  5118. visitAll$1(visitor, keys.map((k) => triggers[k]));
  5119. }
  5120. }
  5121. class SwitchBlock extends BlockNode {
  5122. expression;
  5123. cases;
  5124. unknownBlocks;
  5125. constructor(expression, cases,
  5126. /**
  5127. * These blocks are only captured to allow for autocompletion in the language service. They
  5128. * aren't meant to be processed in any other way.
  5129. */
  5130. unknownBlocks, sourceSpan, startSourceSpan, endSourceSpan, nameSpan) {
  5131. super(nameSpan, sourceSpan, startSourceSpan, endSourceSpan);
  5132. this.expression = expression;
  5133. this.cases = cases;
  5134. this.unknownBlocks = unknownBlocks;
  5135. }
  5136. visit(visitor) {
  5137. return visitor.visitSwitchBlock(this);
  5138. }
  5139. }
  5140. class SwitchBlockCase extends BlockNode {
  5141. expression;
  5142. children;
  5143. i18n;
  5144. constructor(expression, children, sourceSpan, startSourceSpan, endSourceSpan, nameSpan, i18n) {
  5145. super(nameSpan, sourceSpan, startSourceSpan, endSourceSpan);
  5146. this.expression = expression;
  5147. this.children = children;
  5148. this.i18n = i18n;
  5149. }
  5150. visit(visitor) {
  5151. return visitor.visitSwitchBlockCase(this);
  5152. }
  5153. }
  5154. class ForLoopBlock extends BlockNode {
  5155. item;
  5156. expression;
  5157. trackBy;
  5158. trackKeywordSpan;
  5159. contextVariables;
  5160. children;
  5161. empty;
  5162. mainBlockSpan;
  5163. i18n;
  5164. constructor(item, expression, trackBy, trackKeywordSpan, contextVariables, children, empty, sourceSpan, mainBlockSpan, startSourceSpan, endSourceSpan, nameSpan, i18n) {
  5165. super(nameSpan, sourceSpan, startSourceSpan, endSourceSpan);
  5166. this.item = item;
  5167. this.expression = expression;
  5168. this.trackBy = trackBy;
  5169. this.trackKeywordSpan = trackKeywordSpan;
  5170. this.contextVariables = contextVariables;
  5171. this.children = children;
  5172. this.empty = empty;
  5173. this.mainBlockSpan = mainBlockSpan;
  5174. this.i18n = i18n;
  5175. }
  5176. visit(visitor) {
  5177. return visitor.visitForLoopBlock(this);
  5178. }
  5179. }
  5180. class ForLoopBlockEmpty extends BlockNode {
  5181. children;
  5182. i18n;
  5183. constructor(children, sourceSpan, startSourceSpan, endSourceSpan, nameSpan, i18n) {
  5184. super(nameSpan, sourceSpan, startSourceSpan, endSourceSpan);
  5185. this.children = children;
  5186. this.i18n = i18n;
  5187. }
  5188. visit(visitor) {
  5189. return visitor.visitForLoopBlockEmpty(this);
  5190. }
  5191. }
  5192. class IfBlock extends BlockNode {
  5193. branches;
  5194. constructor(branches, sourceSpan, startSourceSpan, endSourceSpan, nameSpan) {
  5195. super(nameSpan, sourceSpan, startSourceSpan, endSourceSpan);
  5196. this.branches = branches;
  5197. }
  5198. visit(visitor) {
  5199. return visitor.visitIfBlock(this);
  5200. }
  5201. }
  5202. class IfBlockBranch extends BlockNode {
  5203. expression;
  5204. children;
  5205. expressionAlias;
  5206. i18n;
  5207. constructor(expression, children, expressionAlias, sourceSpan, startSourceSpan, endSourceSpan, nameSpan, i18n) {
  5208. super(nameSpan, sourceSpan, startSourceSpan, endSourceSpan);
  5209. this.expression = expression;
  5210. this.children = children;
  5211. this.expressionAlias = expressionAlias;
  5212. this.i18n = i18n;
  5213. }
  5214. visit(visitor) {
  5215. return visitor.visitIfBlockBranch(this);
  5216. }
  5217. }
  5218. class UnknownBlock {
  5219. name;
  5220. sourceSpan;
  5221. nameSpan;
  5222. constructor(name, sourceSpan, nameSpan) {
  5223. this.name = name;
  5224. this.sourceSpan = sourceSpan;
  5225. this.nameSpan = nameSpan;
  5226. }
  5227. visit(visitor) {
  5228. return visitor.visitUnknownBlock(this);
  5229. }
  5230. }
  5231. let LetDeclaration$1 = class LetDeclaration {
  5232. name;
  5233. value;
  5234. sourceSpan;
  5235. nameSpan;
  5236. valueSpan;
  5237. constructor(name, value, sourceSpan, nameSpan, valueSpan) {
  5238. this.name = name;
  5239. this.value = value;
  5240. this.sourceSpan = sourceSpan;
  5241. this.nameSpan = nameSpan;
  5242. this.valueSpan = valueSpan;
  5243. }
  5244. visit(visitor) {
  5245. return visitor.visitLetDeclaration(this);
  5246. }
  5247. };
  5248. class Template {
  5249. tagName;
  5250. attributes;
  5251. inputs;
  5252. outputs;
  5253. templateAttrs;
  5254. children;
  5255. references;
  5256. variables;
  5257. sourceSpan;
  5258. startSourceSpan;
  5259. endSourceSpan;
  5260. i18n;
  5261. constructor(
  5262. // tagName is the name of the container element, if applicable.
  5263. // `null` is a special case for when there is a structural directive on an `ng-template` so
  5264. // the renderer can differentiate between the synthetic template and the one written in the
  5265. // file.
  5266. tagName, attributes, inputs, outputs, templateAttrs, children, references, variables, sourceSpan, startSourceSpan, endSourceSpan, i18n) {
  5267. this.tagName = tagName;
  5268. this.attributes = attributes;
  5269. this.inputs = inputs;
  5270. this.outputs = outputs;
  5271. this.templateAttrs = templateAttrs;
  5272. this.children = children;
  5273. this.references = references;
  5274. this.variables = variables;
  5275. this.sourceSpan = sourceSpan;
  5276. this.startSourceSpan = startSourceSpan;
  5277. this.endSourceSpan = endSourceSpan;
  5278. this.i18n = i18n;
  5279. }
  5280. visit(visitor) {
  5281. return visitor.visitTemplate(this);
  5282. }
  5283. }
  5284. class Content {
  5285. selector;
  5286. attributes;
  5287. children;
  5288. sourceSpan;
  5289. i18n;
  5290. name = 'ng-content';
  5291. constructor(selector, attributes, children, sourceSpan, i18n) {
  5292. this.selector = selector;
  5293. this.attributes = attributes;
  5294. this.children = children;
  5295. this.sourceSpan = sourceSpan;
  5296. this.i18n = i18n;
  5297. }
  5298. visit(visitor) {
  5299. return visitor.visitContent(this);
  5300. }
  5301. }
  5302. class Variable {
  5303. name;
  5304. value;
  5305. sourceSpan;
  5306. keySpan;
  5307. valueSpan;
  5308. constructor(name, value, sourceSpan, keySpan, valueSpan) {
  5309. this.name = name;
  5310. this.value = value;
  5311. this.sourceSpan = sourceSpan;
  5312. this.keySpan = keySpan;
  5313. this.valueSpan = valueSpan;
  5314. }
  5315. visit(visitor) {
  5316. return visitor.visitVariable(this);
  5317. }
  5318. }
  5319. class Reference {
  5320. name;
  5321. value;
  5322. sourceSpan;
  5323. keySpan;
  5324. valueSpan;
  5325. constructor(name, value, sourceSpan, keySpan, valueSpan) {
  5326. this.name = name;
  5327. this.value = value;
  5328. this.sourceSpan = sourceSpan;
  5329. this.keySpan = keySpan;
  5330. this.valueSpan = valueSpan;
  5331. }
  5332. visit(visitor) {
  5333. return visitor.visitReference(this);
  5334. }
  5335. }
  5336. let Icu$1 = class Icu {
  5337. vars;
  5338. placeholders;
  5339. sourceSpan;
  5340. i18n;
  5341. constructor(vars, placeholders, sourceSpan, i18n) {
  5342. this.vars = vars;
  5343. this.placeholders = placeholders;
  5344. this.sourceSpan = sourceSpan;
  5345. this.i18n = i18n;
  5346. }
  5347. visit(visitor) {
  5348. return visitor.visitIcu(this);
  5349. }
  5350. };
  5351. let RecursiveVisitor$1 = class RecursiveVisitor {
  5352. visitElement(element) {
  5353. visitAll$1(this, element.attributes);
  5354. visitAll$1(this, element.inputs);
  5355. visitAll$1(this, element.outputs);
  5356. visitAll$1(this, element.children);
  5357. visitAll$1(this, element.references);
  5358. }
  5359. visitTemplate(template) {
  5360. visitAll$1(this, template.attributes);
  5361. visitAll$1(this, template.inputs);
  5362. visitAll$1(this, template.outputs);
  5363. visitAll$1(this, template.children);
  5364. visitAll$1(this, template.references);
  5365. visitAll$1(this, template.variables);
  5366. }
  5367. visitDeferredBlock(deferred) {
  5368. deferred.visitAll(this);
  5369. }
  5370. visitDeferredBlockPlaceholder(block) {
  5371. visitAll$1(this, block.children);
  5372. }
  5373. visitDeferredBlockError(block) {
  5374. visitAll$1(this, block.children);
  5375. }
  5376. visitDeferredBlockLoading(block) {
  5377. visitAll$1(this, block.children);
  5378. }
  5379. visitSwitchBlock(block) {
  5380. visitAll$1(this, block.cases);
  5381. }
  5382. visitSwitchBlockCase(block) {
  5383. visitAll$1(this, block.children);
  5384. }
  5385. visitForLoopBlock(block) {
  5386. const blockItems = [block.item, ...block.contextVariables, ...block.children];
  5387. block.empty && blockItems.push(block.empty);
  5388. visitAll$1(this, blockItems);
  5389. }
  5390. visitForLoopBlockEmpty(block) {
  5391. visitAll$1(this, block.children);
  5392. }
  5393. visitIfBlock(block) {
  5394. visitAll$1(this, block.branches);
  5395. }
  5396. visitIfBlockBranch(block) {
  5397. const blockItems = block.children;
  5398. block.expressionAlias && blockItems.push(block.expressionAlias);
  5399. visitAll$1(this, blockItems);
  5400. }
  5401. visitContent(content) {
  5402. visitAll$1(this, content.children);
  5403. }
  5404. visitVariable(variable) { }
  5405. visitReference(reference) { }
  5406. visitTextAttribute(attribute) { }
  5407. visitBoundAttribute(attribute) { }
  5408. visitBoundEvent(attribute) { }
  5409. visitText(text) { }
  5410. visitBoundText(text) { }
  5411. visitIcu(icu) { }
  5412. visitDeferredTrigger(trigger) { }
  5413. visitUnknownBlock(block) { }
  5414. visitLetDeclaration(decl) { }
  5415. };
  5416. function visitAll$1(visitor, nodes) {
  5417. const result = [];
  5418. if (visitor.visit) {
  5419. for (const node of nodes) {
  5420. visitor.visit(node) || node.visit(visitor);
  5421. }
  5422. }
  5423. else {
  5424. for (const node of nodes) {
  5425. const newNode = node.visit(visitor);
  5426. if (newNode) {
  5427. result.push(newNode);
  5428. }
  5429. }
  5430. }
  5431. return result;
  5432. }
  5433. class Message {
  5434. nodes;
  5435. placeholders;
  5436. placeholderToMessage;
  5437. meaning;
  5438. description;
  5439. customId;
  5440. sources;
  5441. id;
  5442. /** The ids to use if there are no custom id and if `i18nLegacyMessageIdFormat` is not empty */
  5443. legacyIds = [];
  5444. messageString;
  5445. /**
  5446. * @param nodes message AST
  5447. * @param placeholders maps placeholder names to static content and their source spans
  5448. * @param placeholderToMessage maps placeholder names to messages (used for nested ICU messages)
  5449. * @param meaning
  5450. * @param description
  5451. * @param customId
  5452. */
  5453. constructor(nodes, placeholders, placeholderToMessage, meaning, description, customId) {
  5454. this.nodes = nodes;
  5455. this.placeholders = placeholders;
  5456. this.placeholderToMessage = placeholderToMessage;
  5457. this.meaning = meaning;
  5458. this.description = description;
  5459. this.customId = customId;
  5460. this.id = this.customId;
  5461. this.messageString = serializeMessage(this.nodes);
  5462. if (nodes.length) {
  5463. this.sources = [
  5464. {
  5465. filePath: nodes[0].sourceSpan.start.file.url,
  5466. startLine: nodes[0].sourceSpan.start.line + 1,
  5467. startCol: nodes[0].sourceSpan.start.col + 1,
  5468. endLine: nodes[nodes.length - 1].sourceSpan.end.line + 1,
  5469. endCol: nodes[0].sourceSpan.start.col + 1,
  5470. },
  5471. ];
  5472. }
  5473. else {
  5474. this.sources = [];
  5475. }
  5476. }
  5477. }
  5478. let Text$2 = class Text {
  5479. value;
  5480. sourceSpan;
  5481. constructor(value, sourceSpan) {
  5482. this.value = value;
  5483. this.sourceSpan = sourceSpan;
  5484. }
  5485. visit(visitor, context) {
  5486. return visitor.visitText(this, context);
  5487. }
  5488. };
  5489. // TODO(vicb): do we really need this node (vs an array) ?
  5490. class Container {
  5491. children;
  5492. sourceSpan;
  5493. constructor(children, sourceSpan) {
  5494. this.children = children;
  5495. this.sourceSpan = sourceSpan;
  5496. }
  5497. visit(visitor, context) {
  5498. return visitor.visitContainer(this, context);
  5499. }
  5500. }
  5501. class Icu {
  5502. expression;
  5503. type;
  5504. cases;
  5505. sourceSpan;
  5506. expressionPlaceholder;
  5507. constructor(expression, type, cases, sourceSpan, expressionPlaceholder) {
  5508. this.expression = expression;
  5509. this.type = type;
  5510. this.cases = cases;
  5511. this.sourceSpan = sourceSpan;
  5512. this.expressionPlaceholder = expressionPlaceholder;
  5513. }
  5514. visit(visitor, context) {
  5515. return visitor.visitIcu(this, context);
  5516. }
  5517. }
  5518. class TagPlaceholder {
  5519. tag;
  5520. attrs;
  5521. startName;
  5522. closeName;
  5523. children;
  5524. isVoid;
  5525. sourceSpan;
  5526. startSourceSpan;
  5527. endSourceSpan;
  5528. constructor(tag, attrs, startName, closeName, children, isVoid,
  5529. // TODO sourceSpan should cover all (we need a startSourceSpan and endSourceSpan)
  5530. sourceSpan, startSourceSpan, endSourceSpan) {
  5531. this.tag = tag;
  5532. this.attrs = attrs;
  5533. this.startName = startName;
  5534. this.closeName = closeName;
  5535. this.children = children;
  5536. this.isVoid = isVoid;
  5537. this.sourceSpan = sourceSpan;
  5538. this.startSourceSpan = startSourceSpan;
  5539. this.endSourceSpan = endSourceSpan;
  5540. }
  5541. visit(visitor, context) {
  5542. return visitor.visitTagPlaceholder(this, context);
  5543. }
  5544. }
  5545. class Placeholder {
  5546. value;
  5547. name;
  5548. sourceSpan;
  5549. constructor(value, name, sourceSpan) {
  5550. this.value = value;
  5551. this.name = name;
  5552. this.sourceSpan = sourceSpan;
  5553. }
  5554. visit(visitor, context) {
  5555. return visitor.visitPlaceholder(this, context);
  5556. }
  5557. }
  5558. class IcuPlaceholder {
  5559. value;
  5560. name;
  5561. sourceSpan;
  5562. /** Used to capture a message computed from a previous processing pass (see `setI18nRefs()`). */
  5563. previousMessage;
  5564. constructor(value, name, sourceSpan) {
  5565. this.value = value;
  5566. this.name = name;
  5567. this.sourceSpan = sourceSpan;
  5568. }
  5569. visit(visitor, context) {
  5570. return visitor.visitIcuPlaceholder(this, context);
  5571. }
  5572. }
  5573. class BlockPlaceholder {
  5574. name;
  5575. parameters;
  5576. startName;
  5577. closeName;
  5578. children;
  5579. sourceSpan;
  5580. startSourceSpan;
  5581. endSourceSpan;
  5582. constructor(name, parameters, startName, closeName, children, sourceSpan, startSourceSpan, endSourceSpan) {
  5583. this.name = name;
  5584. this.parameters = parameters;
  5585. this.startName = startName;
  5586. this.closeName = closeName;
  5587. this.children = children;
  5588. this.sourceSpan = sourceSpan;
  5589. this.startSourceSpan = startSourceSpan;
  5590. this.endSourceSpan = endSourceSpan;
  5591. }
  5592. visit(visitor, context) {
  5593. return visitor.visitBlockPlaceholder(this, context);
  5594. }
  5595. }
  5596. // Clone the AST
  5597. class CloneVisitor {
  5598. visitText(text, context) {
  5599. return new Text$2(text.value, text.sourceSpan);
  5600. }
  5601. visitContainer(container, context) {
  5602. const children = container.children.map((n) => n.visit(this, context));
  5603. return new Container(children, container.sourceSpan);
  5604. }
  5605. visitIcu(icu, context) {
  5606. const cases = {};
  5607. Object.keys(icu.cases).forEach((key) => (cases[key] = icu.cases[key].visit(this, context)));
  5608. const msg = new Icu(icu.expression, icu.type, cases, icu.sourceSpan, icu.expressionPlaceholder);
  5609. return msg;
  5610. }
  5611. visitTagPlaceholder(ph, context) {
  5612. const children = ph.children.map((n) => n.visit(this, context));
  5613. return new TagPlaceholder(ph.tag, ph.attrs, ph.startName, ph.closeName, children, ph.isVoid, ph.sourceSpan, ph.startSourceSpan, ph.endSourceSpan);
  5614. }
  5615. visitPlaceholder(ph, context) {
  5616. return new Placeholder(ph.value, ph.name, ph.sourceSpan);
  5617. }
  5618. visitIcuPlaceholder(ph, context) {
  5619. return new IcuPlaceholder(ph.value, ph.name, ph.sourceSpan);
  5620. }
  5621. visitBlockPlaceholder(ph, context) {
  5622. const children = ph.children.map((n) => n.visit(this, context));
  5623. return new BlockPlaceholder(ph.name, ph.parameters, ph.startName, ph.closeName, children, ph.sourceSpan, ph.startSourceSpan, ph.endSourceSpan);
  5624. }
  5625. }
  5626. // Visit all the nodes recursively
  5627. class RecurseVisitor {
  5628. visitText(text, context) { }
  5629. visitContainer(container, context) {
  5630. container.children.forEach((child) => child.visit(this));
  5631. }
  5632. visitIcu(icu, context) {
  5633. Object.keys(icu.cases).forEach((k) => {
  5634. icu.cases[k].visit(this);
  5635. });
  5636. }
  5637. visitTagPlaceholder(ph, context) {
  5638. ph.children.forEach((child) => child.visit(this));
  5639. }
  5640. visitPlaceholder(ph, context) { }
  5641. visitIcuPlaceholder(ph, context) { }
  5642. visitBlockPlaceholder(ph, context) {
  5643. ph.children.forEach((child) => child.visit(this));
  5644. }
  5645. }
  5646. /**
  5647. * Serialize the message to the Localize backtick string format that would appear in compiled code.
  5648. */
  5649. function serializeMessage(messageNodes) {
  5650. const visitor = new LocalizeMessageStringVisitor();
  5651. const str = messageNodes.map((n) => n.visit(visitor)).join('');
  5652. return str;
  5653. }
  5654. class LocalizeMessageStringVisitor {
  5655. visitText(text) {
  5656. return text.value;
  5657. }
  5658. visitContainer(container) {
  5659. return container.children.map((child) => child.visit(this)).join('');
  5660. }
  5661. visitIcu(icu) {
  5662. const strCases = Object.keys(icu.cases).map((k) => `${k} {${icu.cases[k].visit(this)}}`);
  5663. return `{${icu.expressionPlaceholder}, ${icu.type}, ${strCases.join(' ')}}`;
  5664. }
  5665. visitTagPlaceholder(ph) {
  5666. const children = ph.children.map((child) => child.visit(this)).join('');
  5667. return `{$${ph.startName}}${children}{$${ph.closeName}}`;
  5668. }
  5669. visitPlaceholder(ph) {
  5670. return `{$${ph.name}}`;
  5671. }
  5672. visitIcuPlaceholder(ph) {
  5673. return `{$${ph.name}}`;
  5674. }
  5675. visitBlockPlaceholder(ph) {
  5676. const children = ph.children.map((child) => child.visit(this)).join('');
  5677. return `{$${ph.startName}}${children}{$${ph.closeName}}`;
  5678. }
  5679. }
  5680. class Serializer {
  5681. // Creates a name mapper, see `PlaceholderMapper`
  5682. // Returning `null` means that no name mapping is used.
  5683. createNameMapper(message) {
  5684. return null;
  5685. }
  5686. }
  5687. /**
  5688. * A simple mapper that take a function to transform an internal name to a public name
  5689. */
  5690. class SimplePlaceholderMapper extends RecurseVisitor {
  5691. mapName;
  5692. internalToPublic = {};
  5693. publicToNextId = {};
  5694. publicToInternal = {};
  5695. // create a mapping from the message
  5696. constructor(message, mapName) {
  5697. super();
  5698. this.mapName = mapName;
  5699. message.nodes.forEach((node) => node.visit(this));
  5700. }
  5701. toPublicName(internalName) {
  5702. return this.internalToPublic.hasOwnProperty(internalName)
  5703. ? this.internalToPublic[internalName]
  5704. : null;
  5705. }
  5706. toInternalName(publicName) {
  5707. return this.publicToInternal.hasOwnProperty(publicName)
  5708. ? this.publicToInternal[publicName]
  5709. : null;
  5710. }
  5711. visitText(text, context) {
  5712. return null;
  5713. }
  5714. visitTagPlaceholder(ph, context) {
  5715. this.visitPlaceholderName(ph.startName);
  5716. super.visitTagPlaceholder(ph, context);
  5717. this.visitPlaceholderName(ph.closeName);
  5718. }
  5719. visitPlaceholder(ph, context) {
  5720. this.visitPlaceholderName(ph.name);
  5721. }
  5722. visitBlockPlaceholder(ph, context) {
  5723. this.visitPlaceholderName(ph.startName);
  5724. super.visitBlockPlaceholder(ph, context);
  5725. this.visitPlaceholderName(ph.closeName);
  5726. }
  5727. visitIcuPlaceholder(ph, context) {
  5728. this.visitPlaceholderName(ph.name);
  5729. }
  5730. // XMB placeholders could only contains A-Z, 0-9 and _
  5731. visitPlaceholderName(internalName) {
  5732. if (!internalName || this.internalToPublic.hasOwnProperty(internalName)) {
  5733. return;
  5734. }
  5735. let publicName = this.mapName(internalName);
  5736. if (this.publicToInternal.hasOwnProperty(publicName)) {
  5737. // Create a new XMB when it has already been used
  5738. const nextId = this.publicToNextId[publicName];
  5739. this.publicToNextId[publicName] = nextId + 1;
  5740. publicName = `${publicName}_${nextId}`;
  5741. }
  5742. else {
  5743. this.publicToNextId[publicName] = 1;
  5744. }
  5745. this.internalToPublic[internalName] = publicName;
  5746. this.publicToInternal[publicName] = internalName;
  5747. }
  5748. }
  5749. let _Visitor$2 = class _Visitor {
  5750. visitTag(tag) {
  5751. const strAttrs = this._serializeAttributes(tag.attrs);
  5752. if (tag.children.length == 0) {
  5753. return `<${tag.name}${strAttrs}/>`;
  5754. }
  5755. const strChildren = tag.children.map((node) => node.visit(this));
  5756. return `<${tag.name}${strAttrs}>${strChildren.join('')}</${tag.name}>`;
  5757. }
  5758. visitText(text) {
  5759. return text.value;
  5760. }
  5761. visitDeclaration(decl) {
  5762. return `<?xml${this._serializeAttributes(decl.attrs)} ?>`;
  5763. }
  5764. _serializeAttributes(attrs) {
  5765. const strAttrs = Object.keys(attrs)
  5766. .map((name) => `${name}="${attrs[name]}"`)
  5767. .join(' ');
  5768. return strAttrs.length > 0 ? ' ' + strAttrs : '';
  5769. }
  5770. visitDoctype(doctype) {
  5771. return `<!DOCTYPE ${doctype.rootTag} [\n${doctype.dtd}\n]>`;
  5772. }
  5773. };
  5774. const _visitor = new _Visitor$2();
  5775. function serialize$1(nodes) {
  5776. return nodes.map((node) => node.visit(_visitor)).join('');
  5777. }
  5778. class Declaration {
  5779. attrs = {};
  5780. constructor(unescapedAttrs) {
  5781. Object.keys(unescapedAttrs).forEach((k) => {
  5782. this.attrs[k] = escapeXml(unescapedAttrs[k]);
  5783. });
  5784. }
  5785. visit(visitor) {
  5786. return visitor.visitDeclaration(this);
  5787. }
  5788. }
  5789. class Doctype {
  5790. rootTag;
  5791. dtd;
  5792. constructor(rootTag, dtd) {
  5793. this.rootTag = rootTag;
  5794. this.dtd = dtd;
  5795. }
  5796. visit(visitor) {
  5797. return visitor.visitDoctype(this);
  5798. }
  5799. }
  5800. class Tag {
  5801. name;
  5802. children;
  5803. attrs = {};
  5804. constructor(name, unescapedAttrs = {}, children = []) {
  5805. this.name = name;
  5806. this.children = children;
  5807. Object.keys(unescapedAttrs).forEach((k) => {
  5808. this.attrs[k] = escapeXml(unescapedAttrs[k]);
  5809. });
  5810. }
  5811. visit(visitor) {
  5812. return visitor.visitTag(this);
  5813. }
  5814. }
  5815. let Text$1 = class Text {
  5816. value;
  5817. constructor(unescapedValue) {
  5818. this.value = escapeXml(unescapedValue);
  5819. }
  5820. visit(visitor) {
  5821. return visitor.visitText(this);
  5822. }
  5823. };
  5824. class CR extends Text$1 {
  5825. constructor(ws = 0) {
  5826. super(`\n${new Array(ws + 1).join(' ')}`);
  5827. }
  5828. }
  5829. const _ESCAPED_CHARS = [
  5830. [/&/g, '&amp;'],
  5831. [/"/g, '&quot;'],
  5832. [/'/g, '&apos;'],
  5833. [/</g, '&lt;'],
  5834. [/>/g, '&gt;'],
  5835. ];
  5836. // Escape `_ESCAPED_CHARS` characters in the given text with encoded entities
  5837. function escapeXml(text) {
  5838. return _ESCAPED_CHARS.reduce((text, entry) => text.replace(entry[0], entry[1]), text);
  5839. }
  5840. /**
  5841. * Defines the `handler` value on the serialized XMB, indicating that Angular
  5842. * generated the bundle. This is useful for analytics in Translation Console.
  5843. *
  5844. * NOTE: Keep in sync with
  5845. * packages/localize/tools/src/extract/translation_files/xmb_translation_serializer.ts.
  5846. */
  5847. const _XMB_HANDLER = 'angular';
  5848. const _MESSAGES_TAG = 'messagebundle';
  5849. const _MESSAGE_TAG = 'msg';
  5850. const _PLACEHOLDER_TAG$3 = 'ph';
  5851. const _EXAMPLE_TAG = 'ex';
  5852. const _SOURCE_TAG$2 = 'source';
  5853. const _DOCTYPE = `<!ELEMENT messagebundle (msg)*>
  5854. <!ATTLIST messagebundle class CDATA #IMPLIED>
  5855. <!ELEMENT msg (#PCDATA|ph|source)*>
  5856. <!ATTLIST msg id CDATA #IMPLIED>
  5857. <!ATTLIST msg seq CDATA #IMPLIED>
  5858. <!ATTLIST msg name CDATA #IMPLIED>
  5859. <!ATTLIST msg desc CDATA #IMPLIED>
  5860. <!ATTLIST msg meaning CDATA #IMPLIED>
  5861. <!ATTLIST msg obsolete (obsolete) #IMPLIED>
  5862. <!ATTLIST msg xml:space (default|preserve) "default">
  5863. <!ATTLIST msg is_hidden CDATA #IMPLIED>
  5864. <!ELEMENT source (#PCDATA)>
  5865. <!ELEMENT ph (#PCDATA|ex)*>
  5866. <!ATTLIST ph name CDATA #REQUIRED>
  5867. <!ELEMENT ex (#PCDATA)>`;
  5868. class Xmb extends Serializer {
  5869. write(messages, locale) {
  5870. const exampleVisitor = new ExampleVisitor();
  5871. const visitor = new _Visitor$1();
  5872. const rootNode = new Tag(_MESSAGES_TAG);
  5873. rootNode.attrs['handler'] = _XMB_HANDLER;
  5874. messages.forEach((message) => {
  5875. const attrs = { id: message.id };
  5876. if (message.description) {
  5877. attrs['desc'] = message.description;
  5878. }
  5879. if (message.meaning) {
  5880. attrs['meaning'] = message.meaning;
  5881. }
  5882. let sourceTags = [];
  5883. message.sources.forEach((source) => {
  5884. sourceTags.push(new Tag(_SOURCE_TAG$2, {}, [
  5885. new Text$1(`${source.filePath}:${source.startLine}${source.endLine !== source.startLine ? ',' + source.endLine : ''}`),
  5886. ]));
  5887. });
  5888. rootNode.children.push(new CR(2), new Tag(_MESSAGE_TAG, attrs, [...sourceTags, ...visitor.serialize(message.nodes)]));
  5889. });
  5890. rootNode.children.push(new CR());
  5891. return serialize$1([
  5892. new Declaration({ version: '1.0', encoding: 'UTF-8' }),
  5893. new CR(),
  5894. new Doctype(_MESSAGES_TAG, _DOCTYPE),
  5895. new CR(),
  5896. exampleVisitor.addDefaultExamples(rootNode),
  5897. new CR(),
  5898. ]);
  5899. }
  5900. load(content, url) {
  5901. throw new Error('Unsupported');
  5902. }
  5903. digest(message) {
  5904. return digest(message);
  5905. }
  5906. createNameMapper(message) {
  5907. return new SimplePlaceholderMapper(message, toPublicName);
  5908. }
  5909. }
  5910. let _Visitor$1 = class _Visitor {
  5911. visitText(text, context) {
  5912. return [new Text$1(text.value)];
  5913. }
  5914. visitContainer(container, context) {
  5915. const nodes = [];
  5916. container.children.forEach((node) => nodes.push(...node.visit(this)));
  5917. return nodes;
  5918. }
  5919. visitIcu(icu, context) {
  5920. const nodes = [new Text$1(`{${icu.expressionPlaceholder}, ${icu.type}, `)];
  5921. Object.keys(icu.cases).forEach((c) => {
  5922. nodes.push(new Text$1(`${c} {`), ...icu.cases[c].visit(this), new Text$1(`} `));
  5923. });
  5924. nodes.push(new Text$1(`}`));
  5925. return nodes;
  5926. }
  5927. visitTagPlaceholder(ph, context) {
  5928. const startTagAsText = new Text$1(`<${ph.tag}>`);
  5929. const startEx = new Tag(_EXAMPLE_TAG, {}, [startTagAsText]);
  5930. // TC requires PH to have a non empty EX, and uses the text node to show the "original" value.
  5931. const startTagPh = new Tag(_PLACEHOLDER_TAG$3, { name: ph.startName }, [
  5932. startEx,
  5933. startTagAsText,
  5934. ]);
  5935. if (ph.isVoid) {
  5936. // void tags have no children nor closing tags
  5937. return [startTagPh];
  5938. }
  5939. const closeTagAsText = new Text$1(`</${ph.tag}>`);
  5940. const closeEx = new Tag(_EXAMPLE_TAG, {}, [closeTagAsText]);
  5941. // TC requires PH to have a non empty EX, and uses the text node to show the "original" value.
  5942. const closeTagPh = new Tag(_PLACEHOLDER_TAG$3, { name: ph.closeName }, [
  5943. closeEx,
  5944. closeTagAsText,
  5945. ]);
  5946. return [startTagPh, ...this.serialize(ph.children), closeTagPh];
  5947. }
  5948. visitPlaceholder(ph, context) {
  5949. const interpolationAsText = new Text$1(`{{${ph.value}}}`);
  5950. // Example tag needs to be not-empty for TC.
  5951. const exTag = new Tag(_EXAMPLE_TAG, {}, [interpolationAsText]);
  5952. return [
  5953. // TC requires PH to have a non empty EX, and uses the text node to show the "original" value.
  5954. new Tag(_PLACEHOLDER_TAG$3, { name: ph.name }, [exTag, interpolationAsText]),
  5955. ];
  5956. }
  5957. visitBlockPlaceholder(ph, context) {
  5958. const startAsText = new Text$1(`@${ph.name}`);
  5959. const startEx = new Tag(_EXAMPLE_TAG, {}, [startAsText]);
  5960. // TC requires PH to have a non empty EX, and uses the text node to show the "original" value.
  5961. const startTagPh = new Tag(_PLACEHOLDER_TAG$3, { name: ph.startName }, [startEx, startAsText]);
  5962. const closeAsText = new Text$1(`}`);
  5963. const closeEx = new Tag(_EXAMPLE_TAG, {}, [closeAsText]);
  5964. // TC requires PH to have a non empty EX, and uses the text node to show the "original" value.
  5965. const closeTagPh = new Tag(_PLACEHOLDER_TAG$3, { name: ph.closeName }, [closeEx, closeAsText]);
  5966. return [startTagPh, ...this.serialize(ph.children), closeTagPh];
  5967. }
  5968. visitIcuPlaceholder(ph, context) {
  5969. const icuExpression = ph.value.expression;
  5970. const icuType = ph.value.type;
  5971. const icuCases = Object.keys(ph.value.cases)
  5972. .map((value) => value + ' {...}')
  5973. .join(' ');
  5974. const icuAsText = new Text$1(`{${icuExpression}, ${icuType}, ${icuCases}}`);
  5975. const exTag = new Tag(_EXAMPLE_TAG, {}, [icuAsText]);
  5976. return [
  5977. // TC requires PH to have a non empty EX, and uses the text node to show the "original" value.
  5978. new Tag(_PLACEHOLDER_TAG$3, { name: ph.name }, [exTag, icuAsText]),
  5979. ];
  5980. }
  5981. serialize(nodes) {
  5982. return [].concat(...nodes.map((node) => node.visit(this)));
  5983. }
  5984. };
  5985. function digest(message) {
  5986. return decimalDigest(message);
  5987. }
  5988. // TC requires at least one non-empty example on placeholders
  5989. class ExampleVisitor {
  5990. addDefaultExamples(node) {
  5991. node.visit(this);
  5992. return node;
  5993. }
  5994. visitTag(tag) {
  5995. if (tag.name === _PLACEHOLDER_TAG$3) {
  5996. if (!tag.children || tag.children.length == 0) {
  5997. const exText = new Text$1(tag.attrs['name'] || '...');
  5998. tag.children = [new Tag(_EXAMPLE_TAG, {}, [exText])];
  5999. }
  6000. }
  6001. else if (tag.children) {
  6002. tag.children.forEach((node) => node.visit(this));
  6003. }
  6004. }
  6005. visitText(text) { }
  6006. visitDeclaration(decl) { }
  6007. visitDoctype(doctype) { }
  6008. }
  6009. // XMB/XTB placeholders can only contain A-Z, 0-9 and _
  6010. function toPublicName(internalName) {
  6011. return internalName.toUpperCase().replace(/[^A-Z0-9_]/g, '_');
  6012. }
  6013. /** Name of the i18n attributes **/
  6014. const I18N_ATTR = 'i18n';
  6015. const I18N_ATTR_PREFIX = 'i18n-';
  6016. /** Prefix of var expressions used in ICUs */
  6017. const I18N_ICU_VAR_PREFIX = 'VAR_';
  6018. function isI18nAttribute(name) {
  6019. return name === I18N_ATTR || name.startsWith(I18N_ATTR_PREFIX);
  6020. }
  6021. function hasI18nAttrs(element) {
  6022. return element.attrs.some((attr) => isI18nAttribute(attr.name));
  6023. }
  6024. function icuFromI18nMessage(message) {
  6025. return message.nodes[0];
  6026. }
  6027. /**
  6028. * Format the placeholder names in a map of placeholders to expressions.
  6029. *
  6030. * The placeholder names are converted from "internal" format (e.g. `START_TAG_DIV_1`) to "external"
  6031. * format (e.g. `startTagDiv_1`).
  6032. *
  6033. * @param params A map of placeholder names to expressions.
  6034. * @param useCamelCase whether to camelCase the placeholder name when formatting.
  6035. * @returns A new map of formatted placeholder names to expressions.
  6036. */
  6037. function formatI18nPlaceholderNamesInMap(params = {}, useCamelCase) {
  6038. const _params = {};
  6039. if (params && Object.keys(params).length) {
  6040. Object.keys(params).forEach((key) => (_params[formatI18nPlaceholderName(key, useCamelCase)] = params[key]));
  6041. }
  6042. return _params;
  6043. }
  6044. /**
  6045. * Converts internal placeholder names to public-facing format
  6046. * (for example to use in goog.getMsg call).
  6047. * Example: `START_TAG_DIV_1` is converted to `startTagDiv_1`.
  6048. *
  6049. * @param name The placeholder name that should be formatted
  6050. * @returns Formatted placeholder name
  6051. */
  6052. function formatI18nPlaceholderName(name, useCamelCase = true) {
  6053. const publicName = toPublicName(name);
  6054. if (!useCamelCase) {
  6055. return publicName;
  6056. }
  6057. const chunks = publicName.split('_');
  6058. if (chunks.length === 1) {
  6059. // if no "_" found - just lowercase the value
  6060. return name.toLowerCase();
  6061. }
  6062. let postfix;
  6063. // eject last element if it's a number
  6064. if (/^\d+$/.test(chunks[chunks.length - 1])) {
  6065. postfix = chunks.pop();
  6066. }
  6067. let raw = chunks.shift().toLowerCase();
  6068. if (chunks.length) {
  6069. raw += chunks.map((c) => c.charAt(0).toUpperCase() + c.slice(1).toLowerCase()).join('');
  6070. }
  6071. return postfix ? `${raw}_${postfix}` : raw;
  6072. }
  6073. /**
  6074. * Checks whether an object key contains potentially unsafe chars, thus the key should be wrapped in
  6075. * quotes. Note: we do not wrap all keys into quotes, as it may have impact on minification and may
  6076. * not work in some cases when object keys are mangled by a minifier.
  6077. *
  6078. * TODO(FW-1136): this is a temporary solution, we need to come up with a better way of working with
  6079. * inputs that contain potentially unsafe chars.
  6080. */
  6081. const UNSAFE_OBJECT_KEY_NAME_REGEXP = /[-.]/;
  6082. /** Name of the temporary to use during data binding */
  6083. const TEMPORARY_NAME = '_t';
  6084. /** Name of the context parameter passed into a template function */
  6085. const CONTEXT_NAME = 'ctx';
  6086. /** Name of the RenderFlag passed into a template function */
  6087. const RENDER_FLAGS = 'rf';
  6088. /**
  6089. * Creates an allocator for a temporary variable.
  6090. *
  6091. * A variable declaration is added to the statements the first time the allocator is invoked.
  6092. */
  6093. function temporaryAllocator(pushStatement, name) {
  6094. let temp = null;
  6095. return () => {
  6096. if (!temp) {
  6097. pushStatement(new DeclareVarStmt(TEMPORARY_NAME, undefined, DYNAMIC_TYPE));
  6098. temp = variable(name);
  6099. }
  6100. return temp;
  6101. };
  6102. }
  6103. function asLiteral(value) {
  6104. if (Array.isArray(value)) {
  6105. return literalArr(value.map(asLiteral));
  6106. }
  6107. return literal(value, INFERRED_TYPE);
  6108. }
  6109. /**
  6110. * Serializes inputs and outputs for `defineDirective` and `defineComponent`.
  6111. *
  6112. * This will attempt to generate optimized data structures to minimize memory or
  6113. * file size of fully compiled applications.
  6114. */
  6115. function conditionallyCreateDirectiveBindingLiteral(map, forInputs) {
  6116. const keys = Object.getOwnPropertyNames(map);
  6117. if (keys.length === 0) {
  6118. return null;
  6119. }
  6120. return literalMap(keys.map((key) => {
  6121. const value = map[key];
  6122. let declaredName;
  6123. let publicName;
  6124. let minifiedName;
  6125. let expressionValue;
  6126. if (typeof value === 'string') {
  6127. // canonical syntax: `dirProp: publicProp`
  6128. declaredName = key;
  6129. minifiedName = key;
  6130. publicName = value;
  6131. expressionValue = asLiteral(publicName);
  6132. }
  6133. else {
  6134. minifiedName = key;
  6135. declaredName = value.classPropertyName;
  6136. publicName = value.bindingPropertyName;
  6137. const differentDeclaringName = publicName !== declaredName;
  6138. const hasDecoratorInputTransform = value.transformFunction !== null;
  6139. let flags = InputFlags.None;
  6140. // Build up input flags
  6141. if (value.isSignal) {
  6142. flags |= InputFlags.SignalBased;
  6143. }
  6144. if (hasDecoratorInputTransform) {
  6145. flags |= InputFlags.HasDecoratorInputTransform;
  6146. }
  6147. // Inputs, compared to outputs, will track their declared name (for `ngOnChanges`), support
  6148. // decorator input transform functions, or store flag information if there is any.
  6149. if (forInputs &&
  6150. (differentDeclaringName || hasDecoratorInputTransform || flags !== InputFlags.None)) {
  6151. const result = [literal(flags), asLiteral(publicName)];
  6152. if (differentDeclaringName || hasDecoratorInputTransform) {
  6153. result.push(asLiteral(declaredName));
  6154. if (hasDecoratorInputTransform) {
  6155. result.push(value.transformFunction);
  6156. }
  6157. }
  6158. expressionValue = literalArr(result);
  6159. }
  6160. else {
  6161. expressionValue = asLiteral(publicName);
  6162. }
  6163. }
  6164. return {
  6165. key: minifiedName,
  6166. // put quotes around keys that contain potentially unsafe characters
  6167. quoted: UNSAFE_OBJECT_KEY_NAME_REGEXP.test(minifiedName),
  6168. value: expressionValue,
  6169. };
  6170. }));
  6171. }
  6172. /**
  6173. * A representation for an object literal used during codegen of definition objects. The generic
  6174. * type `T` allows to reference a documented type of the generated structure, such that the
  6175. * property names that are set can be resolved to their documented declaration.
  6176. */
  6177. class DefinitionMap {
  6178. values = [];
  6179. set(key, value) {
  6180. if (value) {
  6181. const existing = this.values.find((value) => value.key === key);
  6182. if (existing) {
  6183. existing.value = value;
  6184. }
  6185. else {
  6186. this.values.push({ key: key, value, quoted: false });
  6187. }
  6188. }
  6189. }
  6190. toLiteralMap() {
  6191. return literalMap(this.values);
  6192. }
  6193. }
  6194. /**
  6195. * Creates a `CssSelector` from an AST node.
  6196. */
  6197. function createCssSelectorFromNode(node) {
  6198. const elementName = node instanceof Element$1 ? node.name : 'ng-template';
  6199. const attributes = getAttrsForDirectiveMatching(node);
  6200. const cssSelector = new CssSelector();
  6201. const elementNameNoNs = splitNsName(elementName)[1];
  6202. cssSelector.setElement(elementNameNoNs);
  6203. Object.getOwnPropertyNames(attributes).forEach((name) => {
  6204. const nameNoNs = splitNsName(name)[1];
  6205. const value = attributes[name];
  6206. cssSelector.addAttribute(nameNoNs, value);
  6207. if (name.toLowerCase() === 'class') {
  6208. const classes = value.trim().split(/\s+/);
  6209. classes.forEach((className) => cssSelector.addClassName(className));
  6210. }
  6211. });
  6212. return cssSelector;
  6213. }
  6214. /**
  6215. * Extract a map of properties to values for a given element or template node, which can be used
  6216. * by the directive matching machinery.
  6217. *
  6218. * @param elOrTpl the element or template in question
  6219. * @return an object set up for directive matching. For attributes on the element/template, this
  6220. * object maps a property name to its (static) value. For any bindings, this map simply maps the
  6221. * property name to an empty string.
  6222. */
  6223. function getAttrsForDirectiveMatching(elOrTpl) {
  6224. const attributesMap = {};
  6225. if (elOrTpl instanceof Template && elOrTpl.tagName !== 'ng-template') {
  6226. elOrTpl.templateAttrs.forEach((a) => (attributesMap[a.name] = ''));
  6227. }
  6228. else {
  6229. elOrTpl.attributes.forEach((a) => {
  6230. if (!isI18nAttribute(a.name)) {
  6231. attributesMap[a.name] = a.value;
  6232. }
  6233. });
  6234. elOrTpl.inputs.forEach((i) => {
  6235. if (i.type === BindingType.Property || i.type === BindingType.TwoWay) {
  6236. attributesMap[i.name] = '';
  6237. }
  6238. });
  6239. elOrTpl.outputs.forEach((o) => {
  6240. attributesMap[o.name] = '';
  6241. });
  6242. }
  6243. return attributesMap;
  6244. }
  6245. function compileInjectable(meta, resolveForwardRefs) {
  6246. let result = null;
  6247. const factoryMeta = {
  6248. name: meta.name,
  6249. type: meta.type,
  6250. typeArgumentCount: meta.typeArgumentCount,
  6251. deps: [],
  6252. target: FactoryTarget$1.Injectable,
  6253. };
  6254. if (meta.useClass !== undefined) {
  6255. // meta.useClass has two modes of operation. Either deps are specified, in which case `new` is
  6256. // used to instantiate the class with dependencies injected, or deps are not specified and
  6257. // the factory of the class is used to instantiate it.
  6258. //
  6259. // A special case exists for useClass: Type where Type is the injectable type itself and no
  6260. // deps are specified, in which case 'useClass' is effectively ignored.
  6261. const useClassOnSelf = meta.useClass.expression.isEquivalent(meta.type.value);
  6262. let deps = undefined;
  6263. if (meta.deps !== undefined) {
  6264. deps = meta.deps;
  6265. }
  6266. if (deps !== undefined) {
  6267. // factory: () => new meta.useClass(...deps)
  6268. result = compileFactoryFunction({
  6269. ...factoryMeta,
  6270. delegate: meta.useClass.expression,
  6271. delegateDeps: deps,
  6272. delegateType: R3FactoryDelegateType.Class,
  6273. });
  6274. }
  6275. else if (useClassOnSelf) {
  6276. result = compileFactoryFunction(factoryMeta);
  6277. }
  6278. else {
  6279. result = {
  6280. statements: [],
  6281. expression: delegateToFactory(meta.type.value, meta.useClass.expression, resolveForwardRefs),
  6282. };
  6283. }
  6284. }
  6285. else if (meta.useFactory !== undefined) {
  6286. if (meta.deps !== undefined) {
  6287. result = compileFactoryFunction({
  6288. ...factoryMeta,
  6289. delegate: meta.useFactory,
  6290. delegateDeps: meta.deps || [],
  6291. delegateType: R3FactoryDelegateType.Function,
  6292. });
  6293. }
  6294. else {
  6295. result = { statements: [], expression: arrowFn([], meta.useFactory.callFn([])) };
  6296. }
  6297. }
  6298. else if (meta.useValue !== undefined) {
  6299. // Note: it's safe to use `meta.useValue` instead of the `USE_VALUE in meta` check used for
  6300. // client code because meta.useValue is an Expression which will be defined even if the actual
  6301. // value is undefined.
  6302. result = compileFactoryFunction({
  6303. ...factoryMeta,
  6304. expression: meta.useValue.expression,
  6305. });
  6306. }
  6307. else if (meta.useExisting !== undefined) {
  6308. // useExisting is an `inject` call on the existing token.
  6309. result = compileFactoryFunction({
  6310. ...factoryMeta,
  6311. expression: importExpr(Identifiers.inject).callFn([meta.useExisting.expression]),
  6312. });
  6313. }
  6314. else {
  6315. result = {
  6316. statements: [],
  6317. expression: delegateToFactory(meta.type.value, meta.type.value, resolveForwardRefs),
  6318. };
  6319. }
  6320. const token = meta.type.value;
  6321. const injectableProps = new DefinitionMap();
  6322. injectableProps.set('token', token);
  6323. injectableProps.set('factory', result.expression);
  6324. // Only generate providedIn property if it has a non-null value
  6325. if (meta.providedIn.expression.value !== null) {
  6326. injectableProps.set('providedIn', convertFromMaybeForwardRefExpression(meta.providedIn));
  6327. }
  6328. const expression = importExpr(Identifiers.ɵɵdefineInjectable)
  6329. .callFn([injectableProps.toLiteralMap()], undefined, true);
  6330. return {
  6331. expression,
  6332. type: createInjectableType(meta),
  6333. statements: result.statements,
  6334. };
  6335. }
  6336. function createInjectableType(meta) {
  6337. return new ExpressionType(importExpr(Identifiers.InjectableDeclaration, [
  6338. typeWithParameters(meta.type.type, meta.typeArgumentCount),
  6339. ]));
  6340. }
  6341. function delegateToFactory(type, useType, unwrapForwardRefs) {
  6342. if (type.node === useType.node) {
  6343. // The types are the same, so we can simply delegate directly to the type's factory.
  6344. // ```
  6345. // factory: type.ɵfac
  6346. // ```
  6347. return useType.prop('ɵfac');
  6348. }
  6349. if (!unwrapForwardRefs) {
  6350. // The type is not wrapped in a `forwardRef()`, so we create a simple factory function that
  6351. // accepts a sub-type as an argument.
  6352. // ```
  6353. // factory: function(t) { return useType.ɵfac(t); }
  6354. // ```
  6355. return createFactoryFunction(useType);
  6356. }
  6357. // The useType is actually wrapped in a `forwardRef()` so we need to resolve that before
  6358. // calling its factory.
  6359. // ```
  6360. // factory: function(t) { return core.resolveForwardRef(type).ɵfac(t); }
  6361. // ```
  6362. const unwrappedType = importExpr(Identifiers.resolveForwardRef).callFn([useType]);
  6363. return createFactoryFunction(unwrappedType);
  6364. }
  6365. function createFactoryFunction(type) {
  6366. const t = new FnParam('__ngFactoryType__', DYNAMIC_TYPE);
  6367. return arrowFn([t], type.prop('ɵfac').callFn([variable(t.name)]));
  6368. }
  6369. const UNUSABLE_INTERPOLATION_REGEXPS = [
  6370. /@/, // control flow reserved symbol
  6371. /^\s*$/, // empty
  6372. /[<>]/, // html tag
  6373. /^[{}]$/, // i18n expansion
  6374. /&(#|[a-z])/i, // character reference,
  6375. /^\/\//, // comment
  6376. ];
  6377. function assertInterpolationSymbols(identifier, value) {
  6378. if (value != null && !(Array.isArray(value) && value.length == 2)) {
  6379. throw new Error(`Expected '${identifier}' to be an array, [start, end].`);
  6380. }
  6381. else if (value != null) {
  6382. const start = value[0];
  6383. const end = value[1];
  6384. // Check for unusable interpolation symbols
  6385. UNUSABLE_INTERPOLATION_REGEXPS.forEach((regexp) => {
  6386. if (regexp.test(start) || regexp.test(end)) {
  6387. throw new Error(`['${start}', '${end}'] contains unusable interpolation symbol.`);
  6388. }
  6389. });
  6390. }
  6391. }
  6392. class InterpolationConfig {
  6393. start;
  6394. end;
  6395. static fromArray(markers) {
  6396. if (!markers) {
  6397. return DEFAULT_INTERPOLATION_CONFIG;
  6398. }
  6399. assertInterpolationSymbols('interpolation', markers);
  6400. return new InterpolationConfig(markers[0], markers[1]);
  6401. }
  6402. constructor(start, end) {
  6403. this.start = start;
  6404. this.end = end;
  6405. }
  6406. }
  6407. const DEFAULT_INTERPOLATION_CONFIG = new InterpolationConfig('{{', '}}');
  6408. const DEFAULT_CONTAINER_BLOCKS = new Set(['switch']);
  6409. const $EOF = 0;
  6410. const $BSPACE = 8;
  6411. const $TAB = 9;
  6412. const $LF = 10;
  6413. const $VTAB = 11;
  6414. const $FF = 12;
  6415. const $CR = 13;
  6416. const $SPACE = 32;
  6417. const $BANG = 33;
  6418. const $DQ = 34;
  6419. const $HASH = 35;
  6420. const $$ = 36;
  6421. const $PERCENT = 37;
  6422. const $AMPERSAND = 38;
  6423. const $SQ = 39;
  6424. const $LPAREN = 40;
  6425. const $RPAREN = 41;
  6426. const $STAR = 42;
  6427. const $PLUS = 43;
  6428. const $COMMA = 44;
  6429. const $MINUS = 45;
  6430. const $PERIOD = 46;
  6431. const $SLASH = 47;
  6432. const $COLON = 58;
  6433. const $SEMICOLON = 59;
  6434. const $LT = 60;
  6435. const $EQ = 61;
  6436. const $GT = 62;
  6437. const $QUESTION = 63;
  6438. const $0 = 48;
  6439. const $7 = 55;
  6440. const $9 = 57;
  6441. const $A = 65;
  6442. const $E = 69;
  6443. const $F = 70;
  6444. const $X = 88;
  6445. const $Z = 90;
  6446. const $LBRACKET = 91;
  6447. const $BACKSLASH = 92;
  6448. const $RBRACKET = 93;
  6449. const $CARET = 94;
  6450. const $_ = 95;
  6451. const $a = 97;
  6452. const $b = 98;
  6453. const $e = 101;
  6454. const $f = 102;
  6455. const $n = 110;
  6456. const $r = 114;
  6457. const $t = 116;
  6458. const $u = 117;
  6459. const $v = 118;
  6460. const $x = 120;
  6461. const $z = 122;
  6462. const $LBRACE = 123;
  6463. const $BAR = 124;
  6464. const $RBRACE = 125;
  6465. const $NBSP = 160;
  6466. const $AT = 64;
  6467. const $BT = 96;
  6468. function isWhitespace(code) {
  6469. return (code >= $TAB && code <= $SPACE) || code == $NBSP;
  6470. }
  6471. function isDigit(code) {
  6472. return $0 <= code && code <= $9;
  6473. }
  6474. function isAsciiLetter(code) {
  6475. return (code >= $a && code <= $z) || (code >= $A && code <= $Z);
  6476. }
  6477. function isAsciiHexDigit(code) {
  6478. return (code >= $a && code <= $f) || (code >= $A && code <= $F) || isDigit(code);
  6479. }
  6480. function isNewLine(code) {
  6481. return code === $LF || code === $CR;
  6482. }
  6483. function isOctalDigit(code) {
  6484. return $0 <= code && code <= $7;
  6485. }
  6486. function isQuote(code) {
  6487. return code === $SQ || code === $DQ || code === $BT;
  6488. }
  6489. class ParseLocation {
  6490. file;
  6491. offset;
  6492. line;
  6493. col;
  6494. constructor(file, offset, line, col) {
  6495. this.file = file;
  6496. this.offset = offset;
  6497. this.line = line;
  6498. this.col = col;
  6499. }
  6500. toString() {
  6501. return this.offset != null ? `${this.file.url}@${this.line}:${this.col}` : this.file.url;
  6502. }
  6503. moveBy(delta) {
  6504. const source = this.file.content;
  6505. const len = source.length;
  6506. let offset = this.offset;
  6507. let line = this.line;
  6508. let col = this.col;
  6509. while (offset > 0 && delta < 0) {
  6510. offset--;
  6511. delta++;
  6512. const ch = source.charCodeAt(offset);
  6513. if (ch == $LF) {
  6514. line--;
  6515. const priorLine = source
  6516. .substring(0, offset - 1)
  6517. .lastIndexOf(String.fromCharCode($LF));
  6518. col = priorLine > 0 ? offset - priorLine : offset;
  6519. }
  6520. else {
  6521. col--;
  6522. }
  6523. }
  6524. while (offset < len && delta > 0) {
  6525. const ch = source.charCodeAt(offset);
  6526. offset++;
  6527. delta--;
  6528. if (ch == $LF) {
  6529. line++;
  6530. col = 0;
  6531. }
  6532. else {
  6533. col++;
  6534. }
  6535. }
  6536. return new ParseLocation(this.file, offset, line, col);
  6537. }
  6538. // Return the source around the location
  6539. // Up to `maxChars` or `maxLines` on each side of the location
  6540. getContext(maxChars, maxLines) {
  6541. const content = this.file.content;
  6542. let startOffset = this.offset;
  6543. if (startOffset != null) {
  6544. if (startOffset > content.length - 1) {
  6545. startOffset = content.length - 1;
  6546. }
  6547. let endOffset = startOffset;
  6548. let ctxChars = 0;
  6549. let ctxLines = 0;
  6550. while (ctxChars < maxChars && startOffset > 0) {
  6551. startOffset--;
  6552. ctxChars++;
  6553. if (content[startOffset] == '\n') {
  6554. if (++ctxLines == maxLines) {
  6555. break;
  6556. }
  6557. }
  6558. }
  6559. ctxChars = 0;
  6560. ctxLines = 0;
  6561. while (ctxChars < maxChars && endOffset < content.length - 1) {
  6562. endOffset++;
  6563. ctxChars++;
  6564. if (content[endOffset] == '\n') {
  6565. if (++ctxLines == maxLines) {
  6566. break;
  6567. }
  6568. }
  6569. }
  6570. return {
  6571. before: content.substring(startOffset, this.offset),
  6572. after: content.substring(this.offset, endOffset + 1),
  6573. };
  6574. }
  6575. return null;
  6576. }
  6577. }
  6578. class ParseSourceFile {
  6579. content;
  6580. url;
  6581. constructor(content, url) {
  6582. this.content = content;
  6583. this.url = url;
  6584. }
  6585. }
  6586. class ParseSourceSpan {
  6587. start;
  6588. end;
  6589. fullStart;
  6590. details;
  6591. /**
  6592. * Create an object that holds information about spans of tokens/nodes captured during
  6593. * lexing/parsing of text.
  6594. *
  6595. * @param start
  6596. * The location of the start of the span (having skipped leading trivia).
  6597. * Skipping leading trivia makes source-spans more "user friendly", since things like HTML
  6598. * elements will appear to begin at the start of the opening tag, rather than at the start of any
  6599. * leading trivia, which could include newlines.
  6600. *
  6601. * @param end
  6602. * The location of the end of the span.
  6603. *
  6604. * @param fullStart
  6605. * The start of the token without skipping the leading trivia.
  6606. * This is used by tooling that splits tokens further, such as extracting Angular interpolations
  6607. * from text tokens. Such tooling creates new source-spans relative to the original token's
  6608. * source-span. If leading trivia characters have been skipped then the new source-spans may be
  6609. * incorrectly offset.
  6610. *
  6611. * @param details
  6612. * Additional information (such as identifier names) that should be associated with the span.
  6613. */
  6614. constructor(start, end, fullStart = start, details = null) {
  6615. this.start = start;
  6616. this.end = end;
  6617. this.fullStart = fullStart;
  6618. this.details = details;
  6619. }
  6620. toString() {
  6621. return this.start.file.content.substring(this.start.offset, this.end.offset);
  6622. }
  6623. }
  6624. var ParseErrorLevel;
  6625. (function (ParseErrorLevel) {
  6626. ParseErrorLevel[ParseErrorLevel["WARNING"] = 0] = "WARNING";
  6627. ParseErrorLevel[ParseErrorLevel["ERROR"] = 1] = "ERROR";
  6628. })(ParseErrorLevel || (ParseErrorLevel = {}));
  6629. class ParseError {
  6630. span;
  6631. msg;
  6632. level;
  6633. relatedError;
  6634. constructor(
  6635. /** Location of the error. */
  6636. span,
  6637. /** Error message. */
  6638. msg,
  6639. /** Severity level of the error. */
  6640. level = ParseErrorLevel.ERROR,
  6641. /**
  6642. * Error that caused the error to be surfaced. For example, an error in a sub-expression that
  6643. * couldn't be parsed. Not guaranteed to be defined, but can be used to provide more context.
  6644. */
  6645. relatedError) {
  6646. this.span = span;
  6647. this.msg = msg;
  6648. this.level = level;
  6649. this.relatedError = relatedError;
  6650. }
  6651. contextualMessage() {
  6652. const ctx = this.span.start.getContext(100, 3);
  6653. return ctx
  6654. ? `${this.msg} ("${ctx.before}[${ParseErrorLevel[this.level]} ->]${ctx.after}")`
  6655. : this.msg;
  6656. }
  6657. toString() {
  6658. const details = this.span.details ? `, ${this.span.details}` : '';
  6659. return `${this.contextualMessage()}: ${this.span.start}${details}`;
  6660. }
  6661. }
  6662. /**
  6663. * Generates Source Span object for a given R3 Type for JIT mode.
  6664. *
  6665. * @param kind Component or Directive.
  6666. * @param typeName name of the Component or Directive.
  6667. * @param sourceUrl reference to Component or Directive source.
  6668. * @returns instance of ParseSourceSpan that represent a given Component or Directive.
  6669. */
  6670. function r3JitTypeSourceSpan(kind, typeName, sourceUrl) {
  6671. const sourceFileName = `in ${kind} ${typeName} in ${sourceUrl}`;
  6672. const sourceFile = new ParseSourceFile('', sourceFileName);
  6673. return new ParseSourceSpan(new ParseLocation(sourceFile, -1, -1, -1), new ParseLocation(sourceFile, -1, -1, -1));
  6674. }
  6675. let _anonymousTypeIndex = 0;
  6676. function identifierName(compileIdentifier) {
  6677. if (!compileIdentifier || !compileIdentifier.reference) {
  6678. return null;
  6679. }
  6680. const ref = compileIdentifier.reference;
  6681. if (ref['__anonymousType']) {
  6682. return ref['__anonymousType'];
  6683. }
  6684. if (ref['__forward_ref__']) {
  6685. // We do not want to try to stringify a `forwardRef()` function because that would cause the
  6686. // inner function to be evaluated too early, defeating the whole point of the `forwardRef`.
  6687. return '__forward_ref__';
  6688. }
  6689. let identifier = stringify(ref);
  6690. if (identifier.indexOf('(') >= 0) {
  6691. // case: anonymous functions!
  6692. identifier = `anonymous_${_anonymousTypeIndex++}`;
  6693. ref['__anonymousType'] = identifier;
  6694. }
  6695. else {
  6696. identifier = sanitizeIdentifier(identifier);
  6697. }
  6698. return identifier;
  6699. }
  6700. function sanitizeIdentifier(name) {
  6701. return name.replace(/\W/g, '_');
  6702. }
  6703. /**
  6704. * In TypeScript, tagged template functions expect a "template object", which is an array of
  6705. * "cooked" strings plus a `raw` property that contains an array of "raw" strings. This is
  6706. * typically constructed with a function called `__makeTemplateObject(cooked, raw)`, but it may not
  6707. * be available in all environments.
  6708. *
  6709. * This is a JavaScript polyfill that uses __makeTemplateObject when it's available, but otherwise
  6710. * creates an inline helper with the same functionality.
  6711. *
  6712. * In the inline function, if `Object.defineProperty` is available we use that to attach the `raw`
  6713. * array.
  6714. */
  6715. const makeTemplateObjectPolyfill = '(this&&this.__makeTemplateObject||function(e,t){return Object.defineProperty?Object.defineProperty(e,"raw",{value:t}):e.raw=t,e})';
  6716. class AbstractJsEmitterVisitor extends AbstractEmitterVisitor {
  6717. constructor() {
  6718. super(false);
  6719. }
  6720. visitWrappedNodeExpr(ast, ctx) {
  6721. throw new Error('Cannot emit a WrappedNodeExpr in Javascript.');
  6722. }
  6723. visitDeclareVarStmt(stmt, ctx) {
  6724. ctx.print(stmt, `var ${stmt.name}`);
  6725. if (stmt.value) {
  6726. ctx.print(stmt, ' = ');
  6727. stmt.value.visitExpression(this, ctx);
  6728. }
  6729. ctx.println(stmt, `;`);
  6730. return null;
  6731. }
  6732. visitTaggedTemplateLiteralExpr(ast, ctx) {
  6733. // The following convoluted piece of code is effectively the downlevelled equivalent of
  6734. // ```
  6735. // tag`...`
  6736. // ```
  6737. // which is effectively like:
  6738. // ```
  6739. // tag(__makeTemplateObject(cooked, raw), expression1, expression2, ...);
  6740. // ```
  6741. const elements = ast.template.elements;
  6742. ast.tag.visitExpression(this, ctx);
  6743. ctx.print(ast, `(${makeTemplateObjectPolyfill}(`);
  6744. ctx.print(ast, `[${elements.map((part) => escapeIdentifier(part.text, false)).join(', ')}], `);
  6745. ctx.print(ast, `[${elements.map((part) => escapeIdentifier(part.rawText, false)).join(', ')}])`);
  6746. ast.template.expressions.forEach((expression) => {
  6747. ctx.print(ast, ', ');
  6748. expression.visitExpression(this, ctx);
  6749. });
  6750. ctx.print(ast, ')');
  6751. return null;
  6752. }
  6753. visitTemplateLiteralExpr(expr, ctx) {
  6754. ctx.print(expr, '`');
  6755. for (let i = 0; i < expr.elements.length; i++) {
  6756. expr.elements[i].visitExpression(this, ctx);
  6757. const expression = i < expr.expressions.length ? expr.expressions[i] : null;
  6758. if (expression !== null) {
  6759. ctx.print(expression, '${');
  6760. expression.visitExpression(this, ctx);
  6761. ctx.print(expression, '}');
  6762. }
  6763. }
  6764. ctx.print(expr, '`');
  6765. }
  6766. visitTemplateLiteralElementExpr(expr, ctx) {
  6767. ctx.print(expr, expr.rawText);
  6768. return null;
  6769. }
  6770. visitFunctionExpr(ast, ctx) {
  6771. ctx.print(ast, `function${ast.name ? ' ' + ast.name : ''}(`);
  6772. this._visitParams(ast.params, ctx);
  6773. ctx.println(ast, `) {`);
  6774. ctx.incIndent();
  6775. this.visitAllStatements(ast.statements, ctx);
  6776. ctx.decIndent();
  6777. ctx.print(ast, `}`);
  6778. return null;
  6779. }
  6780. visitArrowFunctionExpr(ast, ctx) {
  6781. ctx.print(ast, '(');
  6782. this._visitParams(ast.params, ctx);
  6783. ctx.print(ast, ') =>');
  6784. if (Array.isArray(ast.body)) {
  6785. ctx.println(ast, `{`);
  6786. ctx.incIndent();
  6787. this.visitAllStatements(ast.body, ctx);
  6788. ctx.decIndent();
  6789. ctx.print(ast, `}`);
  6790. }
  6791. else {
  6792. const isObjectLiteral = ast.body instanceof LiteralMapExpr;
  6793. if (isObjectLiteral) {
  6794. ctx.print(ast, '(');
  6795. }
  6796. ast.body.visitExpression(this, ctx);
  6797. if (isObjectLiteral) {
  6798. ctx.print(ast, ')');
  6799. }
  6800. }
  6801. return null;
  6802. }
  6803. visitDeclareFunctionStmt(stmt, ctx) {
  6804. ctx.print(stmt, `function ${stmt.name}(`);
  6805. this._visitParams(stmt.params, ctx);
  6806. ctx.println(stmt, `) {`);
  6807. ctx.incIndent();
  6808. this.visitAllStatements(stmt.statements, ctx);
  6809. ctx.decIndent();
  6810. ctx.println(stmt, `}`);
  6811. return null;
  6812. }
  6813. visitLocalizedString(ast, ctx) {
  6814. // The following convoluted piece of code is effectively the downlevelled equivalent of
  6815. // ```
  6816. // $localize `...`
  6817. // ```
  6818. // which is effectively like:
  6819. // ```
  6820. // $localize(__makeTemplateObject(cooked, raw), expression1, expression2, ...);
  6821. // ```
  6822. ctx.print(ast, `$localize(${makeTemplateObjectPolyfill}(`);
  6823. const parts = [ast.serializeI18nHead()];
  6824. for (let i = 1; i < ast.messageParts.length; i++) {
  6825. parts.push(ast.serializeI18nTemplatePart(i));
  6826. }
  6827. ctx.print(ast, `[${parts.map((part) => escapeIdentifier(part.cooked, false)).join(', ')}], `);
  6828. ctx.print(ast, `[${parts.map((part) => escapeIdentifier(part.raw, false)).join(', ')}])`);
  6829. ast.expressions.forEach((expression) => {
  6830. ctx.print(ast, ', ');
  6831. expression.visitExpression(this, ctx);
  6832. });
  6833. ctx.print(ast, ')');
  6834. return null;
  6835. }
  6836. _visitParams(params, ctx) {
  6837. this.visitAllObjects((param) => ctx.print(null, param.name), params, ctx, ',');
  6838. }
  6839. }
  6840. /**
  6841. * @fileoverview
  6842. * A module to facilitate use of a Trusted Types policy within the JIT
  6843. * compiler. It lazily constructs the Trusted Types policy, providing helper
  6844. * utilities for promoting strings to Trusted Types. When Trusted Types are not
  6845. * available, strings are used as a fallback.
  6846. * @security All use of this module is security-sensitive and should go through
  6847. * security review.
  6848. */
  6849. /**
  6850. * The Trusted Types policy, or null if Trusted Types are not
  6851. * enabled/supported, or undefined if the policy has not been created yet.
  6852. */
  6853. let policy;
  6854. /**
  6855. * Returns the Trusted Types policy, or null if Trusted Types are not
  6856. * enabled/supported. The first call to this function will create the policy.
  6857. */
  6858. function getPolicy() {
  6859. if (policy === undefined) {
  6860. const trustedTypes = _global['trustedTypes'];
  6861. policy = null;
  6862. if (trustedTypes) {
  6863. try {
  6864. policy = trustedTypes.createPolicy('angular#unsafe-jit', {
  6865. createScript: (s) => s,
  6866. });
  6867. }
  6868. catch {
  6869. // trustedTypes.createPolicy throws if called with a name that is
  6870. // already registered, even in report-only mode. Until the API changes,
  6871. // catch the error not to break the applications functionally. In such
  6872. // cases, the code will fall back to using strings.
  6873. }
  6874. }
  6875. }
  6876. return policy;
  6877. }
  6878. /**
  6879. * Unsafely promote a string to a TrustedScript, falling back to strings when
  6880. * Trusted Types are not available.
  6881. * @security In particular, it must be assured that the provided string will
  6882. * never cause an XSS vulnerability if used in a context that will be
  6883. * interpreted and executed as a script by a browser, e.g. when calling eval.
  6884. */
  6885. function trustedScriptFromString(script) {
  6886. return getPolicy()?.createScript(script) || script;
  6887. }
  6888. /**
  6889. * Unsafely call the Function constructor with the given string arguments.
  6890. * @security This is a security-sensitive function; any use of this function
  6891. * must go through security review. In particular, it must be assured that it
  6892. * is only called from the JIT compiler, as use in other code can lead to XSS
  6893. * vulnerabilities.
  6894. */
  6895. function newTrustedFunctionForJIT(...args) {
  6896. if (!_global['trustedTypes']) {
  6897. // In environments that don't support Trusted Types, fall back to the most
  6898. // straightforward implementation:
  6899. return new Function(...args);
  6900. }
  6901. // Chrome currently does not support passing TrustedScript to the Function
  6902. // constructor. The following implements the workaround proposed on the page
  6903. // below, where the Chromium bug is also referenced:
  6904. // https://github.com/w3c/webappsec-trusted-types/wiki/Trusted-Types-for-function-constructor
  6905. const fnArgs = args.slice(0, -1).join(',');
  6906. const fnBody = args[args.length - 1];
  6907. const body = `(function anonymous(${fnArgs}
  6908. ) { ${fnBody}
  6909. })`;
  6910. // Using eval directly confuses the compiler and prevents this module from
  6911. // being stripped out of JS binaries even if not used. The global['eval']
  6912. // indirection fixes that.
  6913. const fn = _global['eval'](trustedScriptFromString(body));
  6914. if (fn.bind === undefined) {
  6915. // Workaround for a browser bug that only exists in Chrome 83, where passing
  6916. // a TrustedScript to eval just returns the TrustedScript back without
  6917. // evaluating it. In that case, fall back to the most straightforward
  6918. // implementation:
  6919. return new Function(...args);
  6920. }
  6921. // To completely mimic the behavior of calling "new Function", two more
  6922. // things need to happen:
  6923. // 1. Stringifying the resulting function should return its source code
  6924. fn.toString = () => body;
  6925. // 2. When calling the resulting function, `this` should refer to `global`
  6926. return fn.bind(_global);
  6927. // When Trusted Types support in Function constructors is widely available,
  6928. // the implementation of this function can be simplified to:
  6929. // return new Function(...args.map(a => trustedScriptFromString(a)));
  6930. }
  6931. /**
  6932. * A helper class to manage the evaluation of JIT generated code.
  6933. */
  6934. class JitEvaluator {
  6935. /**
  6936. *
  6937. * @param sourceUrl The URL of the generated code.
  6938. * @param statements An array of Angular statement AST nodes to be evaluated.
  6939. * @param refResolver Resolves `o.ExternalReference`s into values.
  6940. * @param createSourceMaps If true then create a source-map for the generated code and include it
  6941. * inline as a source-map comment.
  6942. * @returns A map of all the variables in the generated code.
  6943. */
  6944. evaluateStatements(sourceUrl, statements, refResolver, createSourceMaps) {
  6945. const converter = new JitEmitterVisitor(refResolver);
  6946. const ctx = EmitterVisitorContext.createRoot();
  6947. // Ensure generated code is in strict mode
  6948. if (statements.length > 0 && !isUseStrictStatement(statements[0])) {
  6949. statements = [literal('use strict').toStmt(), ...statements];
  6950. }
  6951. converter.visitAllStatements(statements, ctx);
  6952. converter.createReturnStmt(ctx);
  6953. return this.evaluateCode(sourceUrl, ctx, converter.getArgs(), createSourceMaps);
  6954. }
  6955. /**
  6956. * Evaluate a piece of JIT generated code.
  6957. * @param sourceUrl The URL of this generated code.
  6958. * @param ctx A context object that contains an AST of the code to be evaluated.
  6959. * @param vars A map containing the names and values of variables that the evaluated code might
  6960. * reference.
  6961. * @param createSourceMap If true then create a source-map for the generated code and include it
  6962. * inline as a source-map comment.
  6963. * @returns The result of evaluating the code.
  6964. */
  6965. evaluateCode(sourceUrl, ctx, vars, createSourceMap) {
  6966. let fnBody = `"use strict";${ctx.toSource()}\n//# sourceURL=${sourceUrl}`;
  6967. const fnArgNames = [];
  6968. const fnArgValues = [];
  6969. for (const argName in vars) {
  6970. fnArgValues.push(vars[argName]);
  6971. fnArgNames.push(argName);
  6972. }
  6973. if (createSourceMap) {
  6974. // using `new Function(...)` generates a header, 1 line of no arguments, 2 lines otherwise
  6975. // E.g. ```
  6976. // function anonymous(a,b,c
  6977. // /**/) { ... }```
  6978. // We don't want to hard code this fact, so we auto detect it via an empty function first.
  6979. const emptyFn = newTrustedFunctionForJIT(...fnArgNames.concat('return null;')).toString();
  6980. const headerLines = emptyFn.slice(0, emptyFn.indexOf('return null;')).split('\n').length - 1;
  6981. fnBody += `\n${ctx.toSourceMapGenerator(sourceUrl, headerLines).toJsComment()}`;
  6982. }
  6983. const fn = newTrustedFunctionForJIT(...fnArgNames.concat(fnBody));
  6984. return this.executeFunction(fn, fnArgValues);
  6985. }
  6986. /**
  6987. * Execute a JIT generated function by calling it.
  6988. *
  6989. * This method can be overridden in tests to capture the functions that are generated
  6990. * by this `JitEvaluator` class.
  6991. *
  6992. * @param fn A function to execute.
  6993. * @param args The arguments to pass to the function being executed.
  6994. * @returns The return value of the executed function.
  6995. */
  6996. executeFunction(fn, args) {
  6997. return fn(...args);
  6998. }
  6999. }
  7000. /**
  7001. * An Angular AST visitor that converts AST nodes into executable JavaScript code.
  7002. */
  7003. class JitEmitterVisitor extends AbstractJsEmitterVisitor {
  7004. refResolver;
  7005. _evalArgNames = [];
  7006. _evalArgValues = [];
  7007. _evalExportedVars = [];
  7008. constructor(refResolver) {
  7009. super();
  7010. this.refResolver = refResolver;
  7011. }
  7012. createReturnStmt(ctx) {
  7013. const stmt = new ReturnStatement(new LiteralMapExpr(this._evalExportedVars.map((resultVar) => new LiteralMapEntry(resultVar, variable(resultVar), false))));
  7014. stmt.visitStatement(this, ctx);
  7015. }
  7016. getArgs() {
  7017. const result = {};
  7018. for (let i = 0; i < this._evalArgNames.length; i++) {
  7019. result[this._evalArgNames[i]] = this._evalArgValues[i];
  7020. }
  7021. return result;
  7022. }
  7023. visitExternalExpr(ast, ctx) {
  7024. this._emitReferenceToExternal(ast, this.refResolver.resolveExternalReference(ast.value), ctx);
  7025. return null;
  7026. }
  7027. visitWrappedNodeExpr(ast, ctx) {
  7028. this._emitReferenceToExternal(ast, ast.node, ctx);
  7029. return null;
  7030. }
  7031. visitDeclareVarStmt(stmt, ctx) {
  7032. if (stmt.hasModifier(StmtModifier.Exported)) {
  7033. this._evalExportedVars.push(stmt.name);
  7034. }
  7035. return super.visitDeclareVarStmt(stmt, ctx);
  7036. }
  7037. visitDeclareFunctionStmt(stmt, ctx) {
  7038. if (stmt.hasModifier(StmtModifier.Exported)) {
  7039. this._evalExportedVars.push(stmt.name);
  7040. }
  7041. return super.visitDeclareFunctionStmt(stmt, ctx);
  7042. }
  7043. _emitReferenceToExternal(ast, value, ctx) {
  7044. let id = this._evalArgValues.indexOf(value);
  7045. if (id === -1) {
  7046. id = this._evalArgValues.length;
  7047. this._evalArgValues.push(value);
  7048. const name = identifierName({ reference: value }) || 'val';
  7049. this._evalArgNames.push(`jit_${name}_${id}`);
  7050. }
  7051. ctx.print(ast, this._evalArgNames[id]);
  7052. }
  7053. }
  7054. function isUseStrictStatement(statement) {
  7055. return statement.isEquivalent(literal('use strict').toStmt());
  7056. }
  7057. function compileInjector(meta) {
  7058. const definitionMap = new DefinitionMap();
  7059. if (meta.providers !== null) {
  7060. definitionMap.set('providers', meta.providers);
  7061. }
  7062. if (meta.imports.length > 0) {
  7063. definitionMap.set('imports', literalArr(meta.imports));
  7064. }
  7065. const expression = importExpr(Identifiers.defineInjector)
  7066. .callFn([definitionMap.toLiteralMap()], undefined, true);
  7067. const type = createInjectorType(meta);
  7068. return { expression, type, statements: [] };
  7069. }
  7070. function createInjectorType(meta) {
  7071. return new ExpressionType(importExpr(Identifiers.InjectorDeclaration, [new ExpressionType(meta.type.type)]));
  7072. }
  7073. /**
  7074. * Implementation of `CompileReflector` which resolves references to @angular/core
  7075. * symbols at runtime, according to a consumer-provided mapping.
  7076. *
  7077. * Only supports `resolveExternalReference`, all other methods throw.
  7078. */
  7079. class R3JitReflector {
  7080. context;
  7081. constructor(context) {
  7082. this.context = context;
  7083. }
  7084. resolveExternalReference(ref) {
  7085. // This reflector only handles @angular/core imports.
  7086. if (ref.moduleName !== '@angular/core') {
  7087. throw new Error(`Cannot resolve external reference to ${ref.moduleName}, only references to @angular/core are supported.`);
  7088. }
  7089. if (!this.context.hasOwnProperty(ref.name)) {
  7090. throw new Error(`No value provided for @angular/core symbol '${ref.name}'.`);
  7091. }
  7092. return this.context[ref.name];
  7093. }
  7094. }
  7095. /**
  7096. * How the selector scope of an NgModule (its declarations, imports, and exports) should be emitted
  7097. * as a part of the NgModule definition.
  7098. */
  7099. var R3SelectorScopeMode;
  7100. (function (R3SelectorScopeMode) {
  7101. /**
  7102. * Emit the declarations inline into the module definition.
  7103. *
  7104. * This option is useful in certain contexts where it's known that JIT support is required. The
  7105. * tradeoff here is that this emit style prevents directives and pipes from being tree-shaken if
  7106. * they are unused, but the NgModule is used.
  7107. */
  7108. R3SelectorScopeMode[R3SelectorScopeMode["Inline"] = 0] = "Inline";
  7109. /**
  7110. * Emit the declarations using a side effectful function call, `ɵɵsetNgModuleScope`, that is
  7111. * guarded with the `ngJitMode` flag.
  7112. *
  7113. * This form of emit supports JIT and can be optimized away if the `ngJitMode` flag is set to
  7114. * false, which allows unused directives and pipes to be tree-shaken.
  7115. */
  7116. R3SelectorScopeMode[R3SelectorScopeMode["SideEffect"] = 1] = "SideEffect";
  7117. /**
  7118. * Don't generate selector scopes at all.
  7119. *
  7120. * This is useful for contexts where JIT support is known to be unnecessary.
  7121. */
  7122. R3SelectorScopeMode[R3SelectorScopeMode["Omit"] = 2] = "Omit";
  7123. })(R3SelectorScopeMode || (R3SelectorScopeMode = {}));
  7124. /**
  7125. * The type of the NgModule meta data.
  7126. * - Global: Used for full and partial compilation modes which mainly includes R3References.
  7127. * - Local: Used for the local compilation mode which mainly includes the raw expressions as appears
  7128. * in the NgModule decorator.
  7129. */
  7130. var R3NgModuleMetadataKind;
  7131. (function (R3NgModuleMetadataKind) {
  7132. R3NgModuleMetadataKind[R3NgModuleMetadataKind["Global"] = 0] = "Global";
  7133. R3NgModuleMetadataKind[R3NgModuleMetadataKind["Local"] = 1] = "Local";
  7134. })(R3NgModuleMetadataKind || (R3NgModuleMetadataKind = {}));
  7135. /**
  7136. * Construct an `R3NgModuleDef` for the given `R3NgModuleMetadata`.
  7137. */
  7138. function compileNgModule(meta) {
  7139. const statements = [];
  7140. const definitionMap = new DefinitionMap();
  7141. definitionMap.set('type', meta.type.value);
  7142. // Assign bootstrap definition. In local compilation mode (i.e., for
  7143. // `R3NgModuleMetadataKind.LOCAL`) we assign the bootstrap field using the runtime
  7144. // `ɵɵsetNgModuleScope`.
  7145. if (meta.kind === R3NgModuleMetadataKind.Global && meta.bootstrap.length > 0) {
  7146. definitionMap.set('bootstrap', refsToArray(meta.bootstrap, meta.containsForwardDecls));
  7147. }
  7148. if (meta.selectorScopeMode === R3SelectorScopeMode.Inline) {
  7149. // If requested to emit scope information inline, pass the `declarations`, `imports` and
  7150. // `exports` to the `ɵɵdefineNgModule()` call directly.
  7151. if (meta.declarations.length > 0) {
  7152. definitionMap.set('declarations', refsToArray(meta.declarations, meta.containsForwardDecls));
  7153. }
  7154. if (meta.imports.length > 0) {
  7155. definitionMap.set('imports', refsToArray(meta.imports, meta.containsForwardDecls));
  7156. }
  7157. if (meta.exports.length > 0) {
  7158. definitionMap.set('exports', refsToArray(meta.exports, meta.containsForwardDecls));
  7159. }
  7160. }
  7161. else if (meta.selectorScopeMode === R3SelectorScopeMode.SideEffect) {
  7162. // In this mode, scope information is not passed into `ɵɵdefineNgModule` as it
  7163. // would prevent tree-shaking of the declarations, imports and exports references. Instead, it's
  7164. // patched onto the NgModule definition with a `ɵɵsetNgModuleScope` call that's guarded by the
  7165. // `ngJitMode` flag.
  7166. const setNgModuleScopeCall = generateSetNgModuleScopeCall(meta);
  7167. if (setNgModuleScopeCall !== null) {
  7168. statements.push(setNgModuleScopeCall);
  7169. }
  7170. }
  7171. else ;
  7172. if (meta.schemas !== null && meta.schemas.length > 0) {
  7173. definitionMap.set('schemas', literalArr(meta.schemas.map((ref) => ref.value)));
  7174. }
  7175. if (meta.id !== null) {
  7176. definitionMap.set('id', meta.id);
  7177. // Generate a side-effectful call to register this NgModule by its id, as per the semantics of
  7178. // NgModule ids.
  7179. statements.push(importExpr(Identifiers.registerNgModuleType).callFn([meta.type.value, meta.id]).toStmt());
  7180. }
  7181. const expression = importExpr(Identifiers.defineNgModule)
  7182. .callFn([definitionMap.toLiteralMap()], undefined, true);
  7183. const type = createNgModuleType(meta);
  7184. return { expression, type, statements };
  7185. }
  7186. /**
  7187. * This function is used in JIT mode to generate the call to `ɵɵdefineNgModule()` from a call to
  7188. * `ɵɵngDeclareNgModule()`.
  7189. */
  7190. function compileNgModuleDeclarationExpression(meta) {
  7191. const definitionMap = new DefinitionMap();
  7192. definitionMap.set('type', new WrappedNodeExpr(meta.type));
  7193. if (meta.bootstrap !== undefined) {
  7194. definitionMap.set('bootstrap', new WrappedNodeExpr(meta.bootstrap));
  7195. }
  7196. if (meta.declarations !== undefined) {
  7197. definitionMap.set('declarations', new WrappedNodeExpr(meta.declarations));
  7198. }
  7199. if (meta.imports !== undefined) {
  7200. definitionMap.set('imports', new WrappedNodeExpr(meta.imports));
  7201. }
  7202. if (meta.exports !== undefined) {
  7203. definitionMap.set('exports', new WrappedNodeExpr(meta.exports));
  7204. }
  7205. if (meta.schemas !== undefined) {
  7206. definitionMap.set('schemas', new WrappedNodeExpr(meta.schemas));
  7207. }
  7208. if (meta.id !== undefined) {
  7209. definitionMap.set('id', new WrappedNodeExpr(meta.id));
  7210. }
  7211. return importExpr(Identifiers.defineNgModule).callFn([definitionMap.toLiteralMap()]);
  7212. }
  7213. function createNgModuleType(meta) {
  7214. if (meta.kind === R3NgModuleMetadataKind.Local) {
  7215. return new ExpressionType(meta.type.value);
  7216. }
  7217. const { type: moduleType, declarations, exports, imports, includeImportTypes, publicDeclarationTypes, } = meta;
  7218. return new ExpressionType(importExpr(Identifiers.NgModuleDeclaration, [
  7219. new ExpressionType(moduleType.type),
  7220. publicDeclarationTypes === null
  7221. ? tupleTypeOf(declarations)
  7222. : tupleOfTypes(publicDeclarationTypes),
  7223. includeImportTypes ? tupleTypeOf(imports) : NONE_TYPE,
  7224. tupleTypeOf(exports),
  7225. ]));
  7226. }
  7227. /**
  7228. * Generates a function call to `ɵɵsetNgModuleScope` with all necessary information so that the
  7229. * transitive module scope can be computed during runtime in JIT mode. This call is marked pure
  7230. * such that the references to declarations, imports and exports may be elided causing these
  7231. * symbols to become tree-shakeable.
  7232. */
  7233. function generateSetNgModuleScopeCall(meta) {
  7234. const scopeMap = new DefinitionMap();
  7235. if (meta.kind === R3NgModuleMetadataKind.Global) {
  7236. if (meta.declarations.length > 0) {
  7237. scopeMap.set('declarations', refsToArray(meta.declarations, meta.containsForwardDecls));
  7238. }
  7239. }
  7240. else {
  7241. if (meta.declarationsExpression) {
  7242. scopeMap.set('declarations', meta.declarationsExpression);
  7243. }
  7244. }
  7245. if (meta.kind === R3NgModuleMetadataKind.Global) {
  7246. if (meta.imports.length > 0) {
  7247. scopeMap.set('imports', refsToArray(meta.imports, meta.containsForwardDecls));
  7248. }
  7249. }
  7250. else {
  7251. if (meta.importsExpression) {
  7252. scopeMap.set('imports', meta.importsExpression);
  7253. }
  7254. }
  7255. if (meta.kind === R3NgModuleMetadataKind.Global) {
  7256. if (meta.exports.length > 0) {
  7257. scopeMap.set('exports', refsToArray(meta.exports, meta.containsForwardDecls));
  7258. }
  7259. }
  7260. else {
  7261. if (meta.exportsExpression) {
  7262. scopeMap.set('exports', meta.exportsExpression);
  7263. }
  7264. }
  7265. if (meta.kind === R3NgModuleMetadataKind.Local && meta.bootstrapExpression) {
  7266. scopeMap.set('bootstrap', meta.bootstrapExpression);
  7267. }
  7268. if (Object.keys(scopeMap.values).length === 0) {
  7269. return null;
  7270. }
  7271. // setNgModuleScope(...)
  7272. const fnCall = new InvokeFunctionExpr(
  7273. /* fn */ importExpr(Identifiers.setNgModuleScope),
  7274. /* args */ [meta.type.value, scopeMap.toLiteralMap()]);
  7275. // (ngJitMode guard) && setNgModuleScope(...)
  7276. const guardedCall = jitOnlyGuardedExpression(fnCall);
  7277. // function() { (ngJitMode guard) && setNgModuleScope(...); }
  7278. const iife = new FunctionExpr(/* params */ [], /* statements */ [guardedCall.toStmt()]);
  7279. // (function() { (ngJitMode guard) && setNgModuleScope(...); })()
  7280. const iifeCall = new InvokeFunctionExpr(/* fn */ iife, /* args */ []);
  7281. return iifeCall.toStmt();
  7282. }
  7283. function tupleTypeOf(exp) {
  7284. const types = exp.map((ref) => typeofExpr(ref.type));
  7285. return exp.length > 0 ? expressionType(literalArr(types)) : NONE_TYPE;
  7286. }
  7287. function tupleOfTypes(types) {
  7288. const typeofTypes = types.map((type) => typeofExpr(type));
  7289. return types.length > 0 ? expressionType(literalArr(typeofTypes)) : NONE_TYPE;
  7290. }
  7291. function compilePipeFromMetadata(metadata) {
  7292. const definitionMapValues = [];
  7293. // e.g. `name: 'myPipe'`
  7294. definitionMapValues.push({ key: 'name', value: literal(metadata.pipeName), quoted: false });
  7295. // e.g. `type: MyPipe`
  7296. definitionMapValues.push({ key: 'type', value: metadata.type.value, quoted: false });
  7297. // e.g. `pure: true`
  7298. definitionMapValues.push({ key: 'pure', value: literal(metadata.pure), quoted: false });
  7299. if (metadata.isStandalone === false) {
  7300. definitionMapValues.push({ key: 'standalone', value: literal(false), quoted: false });
  7301. }
  7302. const expression = importExpr(Identifiers.definePipe)
  7303. .callFn([literalMap(definitionMapValues)], undefined, true);
  7304. const type = createPipeType(metadata);
  7305. return { expression, type, statements: [] };
  7306. }
  7307. function createPipeType(metadata) {
  7308. return new ExpressionType(importExpr(Identifiers.PipeDeclaration, [
  7309. typeWithParameters(metadata.type.type, metadata.typeArgumentCount),
  7310. new ExpressionType(new LiteralExpr(metadata.pipeName)),
  7311. new ExpressionType(new LiteralExpr(metadata.isStandalone)),
  7312. ]));
  7313. }
  7314. var R3TemplateDependencyKind;
  7315. (function (R3TemplateDependencyKind) {
  7316. R3TemplateDependencyKind[R3TemplateDependencyKind["Directive"] = 0] = "Directive";
  7317. R3TemplateDependencyKind[R3TemplateDependencyKind["Pipe"] = 1] = "Pipe";
  7318. R3TemplateDependencyKind[R3TemplateDependencyKind["NgModule"] = 2] = "NgModule";
  7319. })(R3TemplateDependencyKind || (R3TemplateDependencyKind = {}));
  7320. /**
  7321. * The following set contains all keywords that can be used in the animation css shorthand
  7322. * property and is used during the scoping of keyframes to make sure such keywords
  7323. * are not modified.
  7324. */
  7325. const animationKeywords = new Set([
  7326. // global values
  7327. 'inherit',
  7328. 'initial',
  7329. 'revert',
  7330. 'unset',
  7331. // animation-direction
  7332. 'alternate',
  7333. 'alternate-reverse',
  7334. 'normal',
  7335. 'reverse',
  7336. // animation-fill-mode
  7337. 'backwards',
  7338. 'both',
  7339. 'forwards',
  7340. 'none',
  7341. // animation-play-state
  7342. 'paused',
  7343. 'running',
  7344. // animation-timing-function
  7345. 'ease',
  7346. 'ease-in',
  7347. 'ease-in-out',
  7348. 'ease-out',
  7349. 'linear',
  7350. 'step-start',
  7351. 'step-end',
  7352. // `steps()` function
  7353. 'end',
  7354. 'jump-both',
  7355. 'jump-end',
  7356. 'jump-none',
  7357. 'jump-start',
  7358. 'start',
  7359. ]);
  7360. /**
  7361. * The following array contains all of the CSS at-rule identifiers which are scoped.
  7362. */
  7363. const scopedAtRuleIdentifiers = [
  7364. '@media',
  7365. '@supports',
  7366. '@document',
  7367. '@layer',
  7368. '@container',
  7369. '@scope',
  7370. '@starting-style',
  7371. ];
  7372. /**
  7373. * The following class has its origin from a port of shadowCSS from webcomponents.js to TypeScript.
  7374. * It has since diverge in many ways to tailor Angular's needs.
  7375. *
  7376. * Source:
  7377. * https://github.com/webcomponents/webcomponentsjs/blob/4efecd7e0e/src/ShadowCSS/ShadowCSS.js
  7378. *
  7379. * The original file level comment is reproduced below
  7380. */
  7381. /*
  7382. This is a limited shim for ShadowDOM css styling.
  7383. https://dvcs.w3.org/hg/webcomponents/raw-file/tip/spec/shadow/index.html#styles
  7384. The intention here is to support only the styling features which can be
  7385. relatively simply implemented. The goal is to allow users to avoid the
  7386. most obvious pitfalls and do so without compromising performance significantly.
  7387. For ShadowDOM styling that's not covered here, a set of best practices
  7388. can be provided that should allow users to accomplish more complex styling.
  7389. The following is a list of specific ShadowDOM styling features and a brief
  7390. discussion of the approach used to shim.
  7391. Shimmed features:
  7392. * :host, :host-context: ShadowDOM allows styling of the shadowRoot's host
  7393. element using the :host rule. To shim this feature, the :host styles are
  7394. reformatted and prefixed with a given scope name and promoted to a
  7395. document level stylesheet.
  7396. For example, given a scope name of .foo, a rule like this:
  7397. :host {
  7398. background: red;
  7399. }
  7400. }
  7401. becomes:
  7402. .foo {
  7403. background: red;
  7404. }
  7405. * encapsulation: Styles defined within ShadowDOM, apply only to
  7406. dom inside the ShadowDOM.
  7407. The selectors are scoped by adding an attribute selector suffix to each
  7408. simple selector that contains the host element tag name. Each element
  7409. in the element's ShadowDOM template is also given the scope attribute.
  7410. Thus, these rules match only elements that have the scope attribute.
  7411. For example, given a scope name of x-foo, a rule like this:
  7412. div {
  7413. font-weight: bold;
  7414. }
  7415. becomes:
  7416. div[x-foo] {
  7417. font-weight: bold;
  7418. }
  7419. Note that elements that are dynamically added to a scope must have the scope
  7420. selector added to them manually.
  7421. * upper/lower bound encapsulation: Styles which are defined outside a
  7422. shadowRoot should not cross the ShadowDOM boundary and should not apply
  7423. inside a shadowRoot.
  7424. This styling behavior is not emulated. Some possible ways to do this that
  7425. were rejected due to complexity and/or performance concerns include: (1) reset
  7426. every possible property for every possible selector for a given scope name;
  7427. (2) re-implement css in javascript.
  7428. As an alternative, users should make sure to use selectors
  7429. specific to the scope in which they are working.
  7430. * ::distributed: This behavior is not emulated. It's often not necessary
  7431. to style the contents of a specific insertion point and instead, descendants
  7432. of the host element can be styled selectively. Users can also create an
  7433. extra node around an insertion point and style that node's contents
  7434. via descendent selectors. For example, with a shadowRoot like this:
  7435. <style>
  7436. ::content(div) {
  7437. background: red;
  7438. }
  7439. </style>
  7440. <content></content>
  7441. could become:
  7442. <style>
  7443. / *@polyfill .content-container div * /
  7444. ::content(div) {
  7445. background: red;
  7446. }
  7447. </style>
  7448. <div class="content-container">
  7449. <content></content>
  7450. </div>
  7451. Note the use of @polyfill in the comment above a ShadowDOM specific style
  7452. declaration. This is a directive to the styling shim to use the selector
  7453. in comments in lieu of the next selector when running under polyfill.
  7454. */
  7455. class ShadowCss {
  7456. /*
  7457. * Shim some cssText with the given selector. Returns cssText that can be included in the document
  7458. *
  7459. * The selector is the attribute added to all elements inside the host,
  7460. * The hostSelector is the attribute added to the host itself.
  7461. */
  7462. shimCssText(cssText, selector, hostSelector = '') {
  7463. // **NOTE**: Do not strip comments as this will cause component sourcemaps to break
  7464. // due to shift in lines.
  7465. // Collect comments and replace them with a placeholder, this is done to avoid complicating
  7466. // the rule parsing RegExp and keep it safer.
  7467. const comments = [];
  7468. cssText = cssText.replace(_commentRe, (m) => {
  7469. if (m.match(_commentWithHashRe)) {
  7470. comments.push(m);
  7471. }
  7472. else {
  7473. // Replace non hash comments with empty lines.
  7474. // This is done so that we do not leak any sensitive data in comments.
  7475. const newLinesMatches = m.match(_newLinesRe);
  7476. comments.push((newLinesMatches?.join('') ?? '') + '\n');
  7477. }
  7478. return COMMENT_PLACEHOLDER;
  7479. });
  7480. cssText = this._insertDirectives(cssText);
  7481. const scopedCssText = this._scopeCssText(cssText, selector, hostSelector);
  7482. // Add back comments at the original position.
  7483. let commentIdx = 0;
  7484. return scopedCssText.replace(_commentWithHashPlaceHolderRe, () => comments[commentIdx++]);
  7485. }
  7486. _insertDirectives(cssText) {
  7487. cssText = this._insertPolyfillDirectivesInCssText(cssText);
  7488. return this._insertPolyfillRulesInCssText(cssText);
  7489. }
  7490. /**
  7491. * Process styles to add scope to keyframes.
  7492. *
  7493. * Modify both the names of the keyframes defined in the component styles and also the css
  7494. * animation rules using them.
  7495. *
  7496. * Animation rules using keyframes defined elsewhere are not modified to allow for globally
  7497. * defined keyframes.
  7498. *
  7499. * For example, we convert this css:
  7500. *
  7501. * ```scss
  7502. * .box {
  7503. * animation: box-animation 1s forwards;
  7504. * }
  7505. *
  7506. * @keyframes box-animation {
  7507. * to {
  7508. * background-color: green;
  7509. * }
  7510. * }
  7511. * ```
  7512. *
  7513. * to this:
  7514. *
  7515. * ```scss
  7516. * .box {
  7517. * animation: scopeName_box-animation 1s forwards;
  7518. * }
  7519. *
  7520. * @keyframes scopeName_box-animation {
  7521. * to {
  7522. * background-color: green;
  7523. * }
  7524. * }
  7525. * ```
  7526. *
  7527. * @param cssText the component's css text that needs to be scoped.
  7528. * @param scopeSelector the component's scope selector.
  7529. *
  7530. * @returns the scoped css text.
  7531. */
  7532. _scopeKeyframesRelatedCss(cssText, scopeSelector) {
  7533. const unscopedKeyframesSet = new Set();
  7534. const scopedKeyframesCssText = processRules(cssText, (rule) => this._scopeLocalKeyframeDeclarations(rule, scopeSelector, unscopedKeyframesSet));
  7535. return processRules(scopedKeyframesCssText, (rule) => this._scopeAnimationRule(rule, scopeSelector, unscopedKeyframesSet));
  7536. }
  7537. /**
  7538. * Scopes local keyframes names, returning the updated css rule and it also
  7539. * adds the original keyframe name to a provided set to collect all keyframes names
  7540. * so that it can later be used to scope the animation rules.
  7541. *
  7542. * For example, it takes a rule such as:
  7543. *
  7544. * ```scss
  7545. * @keyframes box-animation {
  7546. * to {
  7547. * background-color: green;
  7548. * }
  7549. * }
  7550. * ```
  7551. *
  7552. * and returns:
  7553. *
  7554. * ```scss
  7555. * @keyframes scopeName_box-animation {
  7556. * to {
  7557. * background-color: green;
  7558. * }
  7559. * }
  7560. * ```
  7561. * and as a side effect it adds "box-animation" to the `unscopedKeyframesSet` set
  7562. *
  7563. * @param cssRule the css rule to process.
  7564. * @param scopeSelector the component's scope selector.
  7565. * @param unscopedKeyframesSet the set of unscoped keyframes names (which can be
  7566. * modified as a side effect)
  7567. *
  7568. * @returns the css rule modified with the scoped keyframes name.
  7569. */
  7570. _scopeLocalKeyframeDeclarations(rule, scopeSelector, unscopedKeyframesSet) {
  7571. return {
  7572. ...rule,
  7573. selector: rule.selector.replace(/(^@(?:-webkit-)?keyframes(?:\s+))(['"]?)(.+)\2(\s*)$/, (_, start, quote, keyframeName, endSpaces) => {
  7574. unscopedKeyframesSet.add(unescapeQuotes(keyframeName, quote));
  7575. return `${start}${quote}${scopeSelector}_${keyframeName}${quote}${endSpaces}`;
  7576. }),
  7577. };
  7578. }
  7579. /**
  7580. * Function used to scope a keyframes name (obtained from an animation declaration)
  7581. * using an existing set of unscopedKeyframes names to discern if the scoping needs to be
  7582. * performed (keyframes names of keyframes not defined in the component's css need not to be
  7583. * scoped).
  7584. *
  7585. * @param keyframe the keyframes name to check.
  7586. * @param scopeSelector the component's scope selector.
  7587. * @param unscopedKeyframesSet the set of unscoped keyframes names.
  7588. *
  7589. * @returns the scoped name of the keyframe, or the original name is the name need not to be
  7590. * scoped.
  7591. */
  7592. _scopeAnimationKeyframe(keyframe, scopeSelector, unscopedKeyframesSet) {
  7593. return keyframe.replace(/^(\s*)(['"]?)(.+?)\2(\s*)$/, (_, spaces1, quote, name, spaces2) => {
  7594. name = `${unscopedKeyframesSet.has(unescapeQuotes(name, quote)) ? scopeSelector + '_' : ''}${name}`;
  7595. return `${spaces1}${quote}${name}${quote}${spaces2}`;
  7596. });
  7597. }
  7598. /**
  7599. * Regular expression used to extrapolate the possible keyframes from an
  7600. * animation declaration (with possibly multiple animation definitions)
  7601. *
  7602. * The regular expression can be divided in three parts
  7603. * - (^|\s+|,)
  7604. * captures how many (if any) leading whitespaces are present or a comma
  7605. * - (?:(?:(['"])((?:\\\\|\\\2|(?!\2).)+)\2)|(-?[A-Za-z][\w\-]*))
  7606. * captures two different possible keyframes, ones which are quoted or ones which are valid css
  7607. * indents (custom properties excluded)
  7608. * - (?=[,\s;]|$)
  7609. * simply matches the end of the possible keyframe, valid endings are: a comma, a space, a
  7610. * semicolon or the end of the string
  7611. */
  7612. _animationDeclarationKeyframesRe = /(^|\s+|,)(?:(?:(['"])((?:\\\\|\\\2|(?!\2).)+)\2)|(-?[A-Za-z][\w\-]*))(?=[,\s]|$)/g;
  7613. /**
  7614. * Scope an animation rule so that the keyframes mentioned in such rule
  7615. * are scoped if defined in the component's css and left untouched otherwise.
  7616. *
  7617. * It can scope values of both the 'animation' and 'animation-name' properties.
  7618. *
  7619. * @param rule css rule to scope.
  7620. * @param scopeSelector the component's scope selector.
  7621. * @param unscopedKeyframesSet the set of unscoped keyframes names.
  7622. *
  7623. * @returns the updated css rule.
  7624. **/
  7625. _scopeAnimationRule(rule, scopeSelector, unscopedKeyframesSet) {
  7626. let content = rule.content.replace(/((?:^|\s+|;)(?:-webkit-)?animation\s*:\s*),*([^;]+)/g, (_, start, animationDeclarations) => start +
  7627. animationDeclarations.replace(this._animationDeclarationKeyframesRe, (original, leadingSpaces, quote = '', quotedName, nonQuotedName) => {
  7628. if (quotedName) {
  7629. return `${leadingSpaces}${this._scopeAnimationKeyframe(`${quote}${quotedName}${quote}`, scopeSelector, unscopedKeyframesSet)}`;
  7630. }
  7631. else {
  7632. return animationKeywords.has(nonQuotedName)
  7633. ? original
  7634. : `${leadingSpaces}${this._scopeAnimationKeyframe(nonQuotedName, scopeSelector, unscopedKeyframesSet)}`;
  7635. }
  7636. }));
  7637. content = content.replace(/((?:^|\s+|;)(?:-webkit-)?animation-name(?:\s*):(?:\s*))([^;]+)/g, (_match, start, commaSeparatedKeyframes) => `${start}${commaSeparatedKeyframes
  7638. .split(',')
  7639. .map((keyframe) => this._scopeAnimationKeyframe(keyframe, scopeSelector, unscopedKeyframesSet))
  7640. .join(',')}`);
  7641. return { ...rule, content };
  7642. }
  7643. /*
  7644. * Process styles to convert native ShadowDOM rules that will trip
  7645. * up the css parser; we rely on decorating the stylesheet with inert rules.
  7646. *
  7647. * For example, we convert this rule:
  7648. *
  7649. * polyfill-next-selector { content: ':host menu-item'; }
  7650. * ::content menu-item {
  7651. *
  7652. * to this:
  7653. *
  7654. * scopeName menu-item {
  7655. *
  7656. **/
  7657. _insertPolyfillDirectivesInCssText(cssText) {
  7658. return cssText.replace(_cssContentNextSelectorRe, function (...m) {
  7659. return m[2] + '{';
  7660. });
  7661. }
  7662. /*
  7663. * Process styles to add rules which will only apply under the polyfill
  7664. *
  7665. * For example, we convert this rule:
  7666. *
  7667. * polyfill-rule {
  7668. * content: ':host menu-item';
  7669. * ...
  7670. * }
  7671. *
  7672. * to this:
  7673. *
  7674. * scopeName menu-item {...}
  7675. *
  7676. **/
  7677. _insertPolyfillRulesInCssText(cssText) {
  7678. return cssText.replace(_cssContentRuleRe, (...m) => {
  7679. const rule = m[0].replace(m[1], '').replace(m[2], '');
  7680. return m[4] + rule;
  7681. });
  7682. }
  7683. /* Ensure styles are scoped. Pseudo-scoping takes a rule like:
  7684. *
  7685. * .foo {... }
  7686. *
  7687. * and converts this to
  7688. *
  7689. * scopeName .foo { ... }
  7690. */
  7691. _scopeCssText(cssText, scopeSelector, hostSelector) {
  7692. const unscopedRules = this._extractUnscopedRulesFromCssText(cssText);
  7693. // replace :host and :host-context with -shadowcsshost and -shadowcsshostcontext respectively
  7694. cssText = this._insertPolyfillHostInCssText(cssText);
  7695. cssText = this._convertColonHost(cssText);
  7696. cssText = this._convertColonHostContext(cssText);
  7697. cssText = this._convertShadowDOMSelectors(cssText);
  7698. if (scopeSelector) {
  7699. cssText = this._scopeKeyframesRelatedCss(cssText, scopeSelector);
  7700. cssText = this._scopeSelectors(cssText, scopeSelector, hostSelector);
  7701. }
  7702. cssText = cssText + '\n' + unscopedRules;
  7703. return cssText.trim();
  7704. }
  7705. /*
  7706. * Process styles to add rules which will only apply under the polyfill
  7707. * and do not process via CSSOM. (CSSOM is destructive to rules on rare
  7708. * occasions, e.g. -webkit-calc on Safari.)
  7709. * For example, we convert this rule:
  7710. *
  7711. * @polyfill-unscoped-rule {
  7712. * content: 'menu-item';
  7713. * ... }
  7714. *
  7715. * to this:
  7716. *
  7717. * menu-item {...}
  7718. *
  7719. **/
  7720. _extractUnscopedRulesFromCssText(cssText) {
  7721. let r = '';
  7722. let m;
  7723. _cssContentUnscopedRuleRe.lastIndex = 0;
  7724. while ((m = _cssContentUnscopedRuleRe.exec(cssText)) !== null) {
  7725. const rule = m[0].replace(m[2], '').replace(m[1], m[4]);
  7726. r += rule + '\n\n';
  7727. }
  7728. return r;
  7729. }
  7730. /*
  7731. * convert a rule like :host(.foo) > .bar { }
  7732. *
  7733. * to
  7734. *
  7735. * .foo<scopeName> > .bar
  7736. */
  7737. _convertColonHost(cssText) {
  7738. return cssText.replace(_cssColonHostRe, (_, hostSelectors, otherSelectors) => {
  7739. if (hostSelectors) {
  7740. const convertedSelectors = [];
  7741. const hostSelectorArray = hostSelectors.split(',').map((p) => p.trim());
  7742. for (const hostSelector of hostSelectorArray) {
  7743. if (!hostSelector)
  7744. break;
  7745. const convertedSelector = _polyfillHostNoCombinator + hostSelector.replace(_polyfillHost, '') + otherSelectors;
  7746. convertedSelectors.push(convertedSelector);
  7747. }
  7748. return convertedSelectors.join(',');
  7749. }
  7750. else {
  7751. return _polyfillHostNoCombinator + otherSelectors;
  7752. }
  7753. });
  7754. }
  7755. /*
  7756. * convert a rule like :host-context(.foo) > .bar { }
  7757. *
  7758. * to
  7759. *
  7760. * .foo<scopeName> > .bar, .foo <scopeName> > .bar { }
  7761. *
  7762. * and
  7763. *
  7764. * :host-context(.foo:host) .bar { ... }
  7765. *
  7766. * to
  7767. *
  7768. * .foo<scopeName> .bar { ... }
  7769. */
  7770. _convertColonHostContext(cssText) {
  7771. const length = cssText.length;
  7772. let parens = 0;
  7773. let prev = 0;
  7774. let result = '';
  7775. // Splits up the selectors on their top-level commas, processes the :host-context in them
  7776. // individually and stitches them back together. This ensures that individual selectors don't
  7777. // affect each other.
  7778. for (let i = 0; i < length; i++) {
  7779. const char = cssText[i];
  7780. // If we hit a comma and there are no open parentheses, take the current chunk and process it.
  7781. if (char === ',' && parens === 0) {
  7782. result += this._convertColonHostContextInSelectorPart(cssText.slice(prev, i)) + ',';
  7783. prev = i + 1;
  7784. continue;
  7785. }
  7786. // We've hit the end. Take everything since the last comma.
  7787. if (i === length - 1) {
  7788. result += this._convertColonHostContextInSelectorPart(cssText.slice(prev));
  7789. break;
  7790. }
  7791. if (char === '(') {
  7792. parens++;
  7793. }
  7794. else if (char === ')') {
  7795. parens--;
  7796. }
  7797. }
  7798. return result;
  7799. }
  7800. _convertColonHostContextInSelectorPart(cssText) {
  7801. return cssText.replace(_cssColonHostContextReGlobal, (selectorText, pseudoPrefix) => {
  7802. // We have captured a selector that contains a `:host-context` rule.
  7803. // For backward compatibility `:host-context` may contain a comma separated list of selectors.
  7804. // Each context selector group will contain a list of host-context selectors that must match
  7805. // an ancestor of the host.
  7806. // (Normally `contextSelectorGroups` will only contain a single array of context selectors.)
  7807. const contextSelectorGroups = [[]];
  7808. // There may be more than `:host-context` in this selector so `selectorText` could look like:
  7809. // `:host-context(.one):host-context(.two)`.
  7810. // Execute `_cssColonHostContextRe` over and over until we have extracted all the
  7811. // `:host-context` selectors from this selector.
  7812. let match;
  7813. while ((match = _cssColonHostContextRe.exec(selectorText))) {
  7814. // `match` = [':host-context(<selectors>)<rest>', <selectors>, <rest>]
  7815. // The `<selectors>` could actually be a comma separated list: `:host-context(.one, .two)`.
  7816. const newContextSelectors = (match[1] ?? '')
  7817. .trim()
  7818. .split(',')
  7819. .map((m) => m.trim())
  7820. .filter((m) => m !== '');
  7821. // We must duplicate the current selector group for each of these new selectors.
  7822. // For example if the current groups are:
  7823. // ```
  7824. // [
  7825. // ['a', 'b', 'c'],
  7826. // ['x', 'y', 'z'],
  7827. // ]
  7828. // ```
  7829. // And we have a new set of comma separated selectors: `:host-context(m,n)` then the new
  7830. // groups are:
  7831. // ```
  7832. // [
  7833. // ['a', 'b', 'c', 'm'],
  7834. // ['x', 'y', 'z', 'm'],
  7835. // ['a', 'b', 'c', 'n'],
  7836. // ['x', 'y', 'z', 'n'],
  7837. // ]
  7838. // ```
  7839. const contextSelectorGroupsLength = contextSelectorGroups.length;
  7840. repeatGroups(contextSelectorGroups, newContextSelectors.length);
  7841. for (let i = 0; i < newContextSelectors.length; i++) {
  7842. for (let j = 0; j < contextSelectorGroupsLength; j++) {
  7843. contextSelectorGroups[j + i * contextSelectorGroupsLength].push(newContextSelectors[i]);
  7844. }
  7845. }
  7846. // Update the `selectorText` and see repeat to see if there are more `:host-context`s.
  7847. selectorText = match[2];
  7848. }
  7849. // The context selectors now must be combined with each other to capture all the possible
  7850. // selectors that `:host-context` can match. See `_combineHostContextSelectors()` for more
  7851. // info about how this is done.
  7852. return contextSelectorGroups
  7853. .map((contextSelectors) => _combineHostContextSelectors(contextSelectors, selectorText, pseudoPrefix))
  7854. .join(', ');
  7855. });
  7856. }
  7857. /*
  7858. * Convert combinators like ::shadow and pseudo-elements like ::content
  7859. * by replacing with space.
  7860. */
  7861. _convertShadowDOMSelectors(cssText) {
  7862. return _shadowDOMSelectorsRe.reduce((result, pattern) => result.replace(pattern, ' '), cssText);
  7863. }
  7864. // change a selector like 'div' to 'name div'
  7865. _scopeSelectors(cssText, scopeSelector, hostSelector) {
  7866. return processRules(cssText, (rule) => {
  7867. let selector = rule.selector;
  7868. let content = rule.content;
  7869. if (rule.selector[0] !== '@') {
  7870. selector = this._scopeSelector({
  7871. selector,
  7872. scopeSelector,
  7873. hostSelector,
  7874. isParentSelector: true,
  7875. });
  7876. }
  7877. else if (scopedAtRuleIdentifiers.some((atRule) => rule.selector.startsWith(atRule))) {
  7878. content = this._scopeSelectors(rule.content, scopeSelector, hostSelector);
  7879. }
  7880. else if (rule.selector.startsWith('@font-face') || rule.selector.startsWith('@page')) {
  7881. content = this._stripScopingSelectors(rule.content);
  7882. }
  7883. return new CssRule(selector, content);
  7884. });
  7885. }
  7886. /**
  7887. * Handle a css text that is within a rule that should not contain scope selectors by simply
  7888. * removing them! An example of such a rule is `@font-face`.
  7889. *
  7890. * `@font-face` rules cannot contain nested selectors. Nor can they be nested under a selector.
  7891. * Normally this would be a syntax error by the author of the styles. But in some rare cases, such
  7892. * as importing styles from a library, and applying `:host ::ng-deep` to the imported styles, we
  7893. * can end up with broken css if the imported styles happen to contain @font-face rules.
  7894. *
  7895. * For example:
  7896. *
  7897. * ```
  7898. * :host ::ng-deep {
  7899. * import 'some/lib/containing/font-face';
  7900. * }
  7901. *
  7902. * Similar logic applies to `@page` rules which can contain a particular set of properties,
  7903. * as well as some specific at-rules. Since they can't be encapsulated, we have to strip
  7904. * any scoping selectors from them. For more information: https://www.w3.org/TR/css-page-3
  7905. * ```
  7906. */
  7907. _stripScopingSelectors(cssText) {
  7908. return processRules(cssText, (rule) => {
  7909. const selector = rule.selector
  7910. .replace(_shadowDeepSelectors, ' ')
  7911. .replace(_polyfillHostNoCombinatorRe, ' ');
  7912. return new CssRule(selector, rule.content);
  7913. });
  7914. }
  7915. _safeSelector;
  7916. _shouldScopeIndicator;
  7917. // `isParentSelector` is used to distinguish the selectors which are coming from
  7918. // the initial selector string and any nested selectors, parsed recursively,
  7919. // for example `selector = 'a:where(.one)'` could be the parent, while recursive call
  7920. // would have `selector = '.one'`.
  7921. _scopeSelector({ selector, scopeSelector, hostSelector, isParentSelector = false, }) {
  7922. // Split the selector into independent parts by `,` (comma) unless
  7923. // comma is within parenthesis, for example `:is(.one, two)`.
  7924. // Negative lookup after comma allows not splitting inside nested parenthesis,
  7925. // up to three levels (((,))).
  7926. const selectorSplitRe = / ?,(?!(?:[^)(]*(?:\([^)(]*(?:\([^)(]*(?:\([^)(]*\)[^)(]*)*\)[^)(]*)*\)[^)(]*)*\))) ?/;
  7927. return selector
  7928. .split(selectorSplitRe)
  7929. .map((part) => part.split(_shadowDeepSelectors))
  7930. .map((deepParts) => {
  7931. const [shallowPart, ...otherParts] = deepParts;
  7932. const applyScope = (shallowPart) => {
  7933. if (this._selectorNeedsScoping(shallowPart, scopeSelector)) {
  7934. return this._applySelectorScope({
  7935. selector: shallowPart,
  7936. scopeSelector,
  7937. hostSelector,
  7938. isParentSelector,
  7939. });
  7940. }
  7941. else {
  7942. return shallowPart;
  7943. }
  7944. };
  7945. return [applyScope(shallowPart), ...otherParts].join(' ');
  7946. })
  7947. .join(', ');
  7948. }
  7949. _selectorNeedsScoping(selector, scopeSelector) {
  7950. const re = this._makeScopeMatcher(scopeSelector);
  7951. return !re.test(selector);
  7952. }
  7953. _makeScopeMatcher(scopeSelector) {
  7954. const lre = /\[/g;
  7955. const rre = /\]/g;
  7956. scopeSelector = scopeSelector.replace(lre, '\\[').replace(rre, '\\]');
  7957. return new RegExp('^(' + scopeSelector + ')' + _selectorReSuffix, 'm');
  7958. }
  7959. // scope via name and [is=name]
  7960. _applySimpleSelectorScope(selector, scopeSelector, hostSelector) {
  7961. // In Android browser, the lastIndex is not reset when the regex is used in String.replace()
  7962. _polyfillHostRe.lastIndex = 0;
  7963. if (_polyfillHostRe.test(selector)) {
  7964. const replaceBy = `[${hostSelector}]`;
  7965. let result = selector;
  7966. while (result.match(_polyfillHostNoCombinatorRe)) {
  7967. result = result.replace(_polyfillHostNoCombinatorRe, (_hnc, selector) => {
  7968. return selector.replace(/([^:\)]*)(:*)(.*)/, (_, before, colon, after) => {
  7969. return before + replaceBy + colon + after;
  7970. });
  7971. });
  7972. }
  7973. return result.replace(_polyfillHostRe, replaceBy);
  7974. }
  7975. return scopeSelector + ' ' + selector;
  7976. }
  7977. // return a selector with [name] suffix on each simple selector
  7978. // e.g. .foo.bar > .zot becomes .foo[name].bar[name] > .zot[name] /** @internal */
  7979. _applySelectorScope({ selector, scopeSelector, hostSelector, isParentSelector, }) {
  7980. const isRe = /\[is=([^\]]*)\]/g;
  7981. scopeSelector = scopeSelector.replace(isRe, (_, ...parts) => parts[0]);
  7982. const attrName = `[${scopeSelector}]`;
  7983. const _scopeSelectorPart = (p) => {
  7984. let scopedP = p.trim();
  7985. if (!scopedP) {
  7986. return p;
  7987. }
  7988. if (p.includes(_polyfillHostNoCombinator)) {
  7989. scopedP = this._applySimpleSelectorScope(p, scopeSelector, hostSelector);
  7990. if (!p.match(_polyfillHostNoCombinatorOutsidePseudoFunction)) {
  7991. const [_, before, colon, after] = scopedP.match(/([^:]*)(:*)([\s\S]*)/);
  7992. scopedP = before + attrName + colon + after;
  7993. }
  7994. }
  7995. else {
  7996. // remove :host since it should be unnecessary
  7997. const t = p.replace(_polyfillHostRe, '');
  7998. if (t.length > 0) {
  7999. const matches = t.match(/([^:]*)(:*)([\s\S]*)/);
  8000. if (matches) {
  8001. scopedP = matches[1] + attrName + matches[2] + matches[3];
  8002. }
  8003. }
  8004. }
  8005. return scopedP;
  8006. };
  8007. // Wraps `_scopeSelectorPart()` to not use it directly on selectors with
  8008. // pseudo selector functions like `:where()`. Selectors within pseudo selector
  8009. // functions are recursively sent to `_scopeSelector()`.
  8010. const _pseudoFunctionAwareScopeSelectorPart = (selectorPart) => {
  8011. let scopedPart = '';
  8012. // Collect all outer `:where()` and `:is()` selectors,
  8013. // counting parenthesis to keep nested selectors intact.
  8014. const pseudoSelectorParts = [];
  8015. let pseudoSelectorMatch;
  8016. while ((pseudoSelectorMatch = _cssPrefixWithPseudoSelectorFunction.exec(selectorPart)) !== null) {
  8017. let openedBrackets = 1;
  8018. let index = _cssPrefixWithPseudoSelectorFunction.lastIndex;
  8019. while (index < selectorPart.length) {
  8020. const currentSymbol = selectorPart[index];
  8021. index++;
  8022. if (currentSymbol === '(') {
  8023. openedBrackets++;
  8024. continue;
  8025. }
  8026. if (currentSymbol === ')') {
  8027. openedBrackets--;
  8028. if (openedBrackets === 0) {
  8029. break;
  8030. }
  8031. continue;
  8032. }
  8033. }
  8034. pseudoSelectorParts.push(`${pseudoSelectorMatch[0]}${selectorPart.slice(_cssPrefixWithPseudoSelectorFunction.lastIndex, index)}`);
  8035. _cssPrefixWithPseudoSelectorFunction.lastIndex = index;
  8036. }
  8037. // If selector consists of only `:where()` and `:is()` on the outer level
  8038. // scope those pseudo-selectors individually, otherwise scope the whole
  8039. // selector.
  8040. if (pseudoSelectorParts.join('') === selectorPart) {
  8041. scopedPart = pseudoSelectorParts
  8042. .map((selectorPart) => {
  8043. const [cssPseudoSelectorFunction] = selectorPart.match(_cssPrefixWithPseudoSelectorFunction) ?? [];
  8044. // Unwrap the pseudo selector to scope its contents.
  8045. // For example,
  8046. // - `:where(selectorToScope)` -> `selectorToScope`;
  8047. // - `:is(.foo, .bar)` -> `.foo, .bar`.
  8048. const selectorToScope = selectorPart.slice(cssPseudoSelectorFunction?.length, -1);
  8049. if (selectorToScope.includes(_polyfillHostNoCombinator)) {
  8050. this._shouldScopeIndicator = true;
  8051. }
  8052. const scopedInnerPart = this._scopeSelector({
  8053. selector: selectorToScope,
  8054. scopeSelector,
  8055. hostSelector,
  8056. });
  8057. // Put the result back into the pseudo selector function.
  8058. return `${cssPseudoSelectorFunction}${scopedInnerPart})`;
  8059. })
  8060. .join('');
  8061. }
  8062. else {
  8063. this._shouldScopeIndicator =
  8064. this._shouldScopeIndicator || selectorPart.includes(_polyfillHostNoCombinator);
  8065. scopedPart = this._shouldScopeIndicator ? _scopeSelectorPart(selectorPart) : selectorPart;
  8066. }
  8067. return scopedPart;
  8068. };
  8069. if (isParentSelector) {
  8070. this._safeSelector = new SafeSelector(selector);
  8071. selector = this._safeSelector.content();
  8072. }
  8073. let scopedSelector = '';
  8074. let startIndex = 0;
  8075. let res;
  8076. // Combinators aren't used as a delimiter if they are within parenthesis,
  8077. // for example `:where(.one .two)` stays intact.
  8078. // Similarly to selector separation by comma initially, negative lookahead
  8079. // is used here to not break selectors within nested parenthesis up to three
  8080. // nested layers.
  8081. const sep = /( |>|\+|~(?!=))(?!([^)(]*(?:\([^)(]*(?:\([^)(]*(?:\([^)(]*\)[^)(]*)*\)[^)(]*)*\)[^)(]*)*\)))\s*/g;
  8082. // If a selector appears before :host it should not be shimmed as it
  8083. // matches on ancestor elements and not on elements in the host's shadow
  8084. // `:host-context(div)` is transformed to
  8085. // `-shadowcsshost-no-combinatordiv, div -shadowcsshost-no-combinator`
  8086. // the `div` is not part of the component in the 2nd selectors and should not be scoped.
  8087. // Historically `component-tag:host` was matching the component so we also want to preserve
  8088. // this behavior to avoid breaking legacy apps (it should not match).
  8089. // The behavior should be:
  8090. // - `tag:host` -> `tag[h]` (this is to avoid breaking legacy apps, should not match anything)
  8091. // - `tag :host` -> `tag [h]` (`tag` is not scoped because it's considered part of a
  8092. // `:host-context(tag)`)
  8093. const hasHost = selector.includes(_polyfillHostNoCombinator);
  8094. // Only scope parts after or on the same level as the first `-shadowcsshost-no-combinator`
  8095. // when it is present. The selector has the same level when it is a part of a pseudo
  8096. // selector, like `:where()`, for example `:where(:host, .foo)` would result in `.foo`
  8097. // being scoped.
  8098. if (isParentSelector || this._shouldScopeIndicator) {
  8099. this._shouldScopeIndicator = !hasHost;
  8100. }
  8101. while ((res = sep.exec(selector)) !== null) {
  8102. const separator = res[1];
  8103. // Do not trim the selector, as otherwise this will break sourcemaps
  8104. // when they are defined on multiple lines
  8105. // Example:
  8106. // div,
  8107. // p { color: red}
  8108. const part = selector.slice(startIndex, res.index);
  8109. // A space following an escaped hex value and followed by another hex character
  8110. // (ie: ".\fc ber" for ".über") is not a separator between 2 selectors
  8111. // also keep in mind that backslashes are replaced by a placeholder by SafeSelector
  8112. // These escaped selectors happen for example when esbuild runs with optimization.minify.
  8113. if (part.match(/__esc-ph-(\d+)__/) && selector[res.index + 1]?.match(/[a-fA-F\d]/)) {
  8114. continue;
  8115. }
  8116. const scopedPart = _pseudoFunctionAwareScopeSelectorPart(part);
  8117. scopedSelector += `${scopedPart} ${separator} `;
  8118. startIndex = sep.lastIndex;
  8119. }
  8120. const part = selector.substring(startIndex);
  8121. scopedSelector += _pseudoFunctionAwareScopeSelectorPart(part);
  8122. // replace the placeholders with their original values
  8123. // using values stored inside the `safeSelector` instance.
  8124. return this._safeSelector.restore(scopedSelector);
  8125. }
  8126. _insertPolyfillHostInCssText(selector) {
  8127. return selector
  8128. .replace(_colonHostContextRe, _polyfillHostContext)
  8129. .replace(_colonHostRe, _polyfillHost);
  8130. }
  8131. }
  8132. class SafeSelector {
  8133. placeholders = [];
  8134. index = 0;
  8135. _content;
  8136. constructor(selector) {
  8137. // Replaces attribute selectors with placeholders.
  8138. // The WS in [attr="va lue"] would otherwise be interpreted as a selector separator.
  8139. selector = this._escapeRegexMatches(selector, /(\[[^\]]*\])/g);
  8140. // CSS allows for certain special characters to be used in selectors if they're escaped.
  8141. // E.g. `.foo:blue` won't match a class called `foo:blue`, because the colon denotes a
  8142. // pseudo-class, but writing `.foo\:blue` will match, because the colon was escaped.
  8143. // Replace all escape sequences (`\` followed by a character) with a placeholder so
  8144. // that our handling of pseudo-selectors doesn't mess with them.
  8145. // Escaped characters have a specific placeholder so they can be detected separately.
  8146. selector = selector.replace(/(\\.)/g, (_, keep) => {
  8147. const replaceBy = `__esc-ph-${this.index}__`;
  8148. this.placeholders.push(keep);
  8149. this.index++;
  8150. return replaceBy;
  8151. });
  8152. // Replaces the expression in `:nth-child(2n + 1)` with a placeholder.
  8153. // WS and "+" would otherwise be interpreted as selector separators.
  8154. this._content = selector.replace(/(:nth-[-\w]+)(\([^)]+\))/g, (_, pseudo, exp) => {
  8155. const replaceBy = `__ph-${this.index}__`;
  8156. this.placeholders.push(exp);
  8157. this.index++;
  8158. return pseudo + replaceBy;
  8159. });
  8160. }
  8161. restore(content) {
  8162. return content.replace(/__(?:ph|esc-ph)-(\d+)__/g, (_ph, index) => this.placeholders[+index]);
  8163. }
  8164. content() {
  8165. return this._content;
  8166. }
  8167. /**
  8168. * Replaces all of the substrings that match a regex within a
  8169. * special string (e.g. `__ph-0__`, `__ph-1__`, etc).
  8170. */
  8171. _escapeRegexMatches(content, pattern) {
  8172. return content.replace(pattern, (_, keep) => {
  8173. const replaceBy = `__ph-${this.index}__`;
  8174. this.placeholders.push(keep);
  8175. this.index++;
  8176. return replaceBy;
  8177. });
  8178. }
  8179. }
  8180. const _cssScopedPseudoFunctionPrefix = '(:(where|is)\\()?';
  8181. const _cssPrefixWithPseudoSelectorFunction = /:(where|is)\(/gi;
  8182. const _cssContentNextSelectorRe = /polyfill-next-selector[^}]*content:[\s]*?(['"])(.*?)\1[;\s]*}([^{]*?){/gim;
  8183. const _cssContentRuleRe = /(polyfill-rule)[^}]*(content:[\s]*(['"])(.*?)\3)[;\s]*[^}]*}/gim;
  8184. const _cssContentUnscopedRuleRe = /(polyfill-unscoped-rule)[^}]*(content:[\s]*(['"])(.*?)\3)[;\s]*[^}]*}/gim;
  8185. const _polyfillHost = '-shadowcsshost';
  8186. // note: :host-context pre-processed to -shadowcsshostcontext.
  8187. const _polyfillHostContext = '-shadowcsscontext';
  8188. const _parenSuffix = '(?:\\((' + '(?:\\([^)(]*\\)|[^)(]*)+?' + ')\\))';
  8189. const _cssColonHostRe = new RegExp(_polyfillHost + _parenSuffix + '?([^,{]*)', 'gim');
  8190. // note: :host-context patterns are terminated with `{`, as opposed to :host which
  8191. // is both `{` and `,` because :host-context handles top-level commas differently.
  8192. const _hostContextPattern = _polyfillHostContext + _parenSuffix + '?([^{]*)';
  8193. const _cssColonHostContextReGlobal = new RegExp(`${_cssScopedPseudoFunctionPrefix}(${_hostContextPattern})`, 'gim');
  8194. const _cssColonHostContextRe = new RegExp(_hostContextPattern, 'im');
  8195. const _polyfillHostNoCombinator = _polyfillHost + '-no-combinator';
  8196. const _polyfillHostNoCombinatorOutsidePseudoFunction = new RegExp(`${_polyfillHostNoCombinator}(?![^(]*\\))`, 'g');
  8197. const _polyfillHostNoCombinatorRe = /-shadowcsshost-no-combinator([^\s,]*)/;
  8198. const _shadowDOMSelectorsRe = [
  8199. /::shadow/g,
  8200. /::content/g,
  8201. // Deprecated selectors
  8202. /\/shadow-deep\//g,
  8203. /\/shadow\//g,
  8204. ];
  8205. // The deep combinator is deprecated in the CSS spec
  8206. // Support for `>>>`, `deep`, `::ng-deep` is then also deprecated and will be removed in the future.
  8207. // see https://github.com/angular/angular/pull/17677
  8208. const _shadowDeepSelectors = /(?:>>>)|(?:\/deep\/)|(?:::ng-deep)/g;
  8209. const _selectorReSuffix = '([>\\s~+[.,{:][\\s\\S]*)?$';
  8210. const _polyfillHostRe = /-shadowcsshost/gim;
  8211. const _colonHostRe = /:host/gim;
  8212. const _colonHostContextRe = /:host-context/gim;
  8213. const _newLinesRe = /\r?\n/g;
  8214. const _commentRe = /\/\*[\s\S]*?\*\//g;
  8215. const _commentWithHashRe = /\/\*\s*#\s*source(Mapping)?URL=/g;
  8216. const COMMENT_PLACEHOLDER = '%COMMENT%';
  8217. const _commentWithHashPlaceHolderRe = new RegExp(COMMENT_PLACEHOLDER, 'g');
  8218. const BLOCK_PLACEHOLDER = '%BLOCK%';
  8219. const _ruleRe = new RegExp(`(\\s*(?:${COMMENT_PLACEHOLDER}\\s*)*)([^;\\{\\}]+?)(\\s*)((?:{%BLOCK%}?\\s*;?)|(?:\\s*;))`, 'g');
  8220. const CONTENT_PAIRS = new Map([['{', '}']]);
  8221. const COMMA_IN_PLACEHOLDER = '%COMMA_IN_PLACEHOLDER%';
  8222. const SEMI_IN_PLACEHOLDER = '%SEMI_IN_PLACEHOLDER%';
  8223. const COLON_IN_PLACEHOLDER = '%COLON_IN_PLACEHOLDER%';
  8224. const _cssCommaInPlaceholderReGlobal = new RegExp(COMMA_IN_PLACEHOLDER, 'g');
  8225. const _cssSemiInPlaceholderReGlobal = new RegExp(SEMI_IN_PLACEHOLDER, 'g');
  8226. const _cssColonInPlaceholderReGlobal = new RegExp(COLON_IN_PLACEHOLDER, 'g');
  8227. class CssRule {
  8228. selector;
  8229. content;
  8230. constructor(selector, content) {
  8231. this.selector = selector;
  8232. this.content = content;
  8233. }
  8234. }
  8235. function processRules(input, ruleCallback) {
  8236. const escaped = escapeInStrings(input);
  8237. const inputWithEscapedBlocks = escapeBlocks(escaped, CONTENT_PAIRS, BLOCK_PLACEHOLDER);
  8238. let nextBlockIndex = 0;
  8239. const escapedResult = inputWithEscapedBlocks.escapedString.replace(_ruleRe, (...m) => {
  8240. const selector = m[2];
  8241. let content = '';
  8242. let suffix = m[4];
  8243. let contentPrefix = '';
  8244. if (suffix && suffix.startsWith('{' + BLOCK_PLACEHOLDER)) {
  8245. content = inputWithEscapedBlocks.blocks[nextBlockIndex++];
  8246. suffix = suffix.substring(BLOCK_PLACEHOLDER.length + 1);
  8247. contentPrefix = '{';
  8248. }
  8249. const rule = ruleCallback(new CssRule(selector, content));
  8250. return `${m[1]}${rule.selector}${m[3]}${contentPrefix}${rule.content}${suffix}`;
  8251. });
  8252. return unescapeInStrings(escapedResult);
  8253. }
  8254. class StringWithEscapedBlocks {
  8255. escapedString;
  8256. blocks;
  8257. constructor(escapedString, blocks) {
  8258. this.escapedString = escapedString;
  8259. this.blocks = blocks;
  8260. }
  8261. }
  8262. function escapeBlocks(input, charPairs, placeholder) {
  8263. const resultParts = [];
  8264. const escapedBlocks = [];
  8265. let openCharCount = 0;
  8266. let nonBlockStartIndex = 0;
  8267. let blockStartIndex = -1;
  8268. let openChar;
  8269. let closeChar;
  8270. for (let i = 0; i < input.length; i++) {
  8271. const char = input[i];
  8272. if (char === '\\') {
  8273. i++;
  8274. }
  8275. else if (char === closeChar) {
  8276. openCharCount--;
  8277. if (openCharCount === 0) {
  8278. escapedBlocks.push(input.substring(blockStartIndex, i));
  8279. resultParts.push(placeholder);
  8280. nonBlockStartIndex = i;
  8281. blockStartIndex = -1;
  8282. openChar = closeChar = undefined;
  8283. }
  8284. }
  8285. else if (char === openChar) {
  8286. openCharCount++;
  8287. }
  8288. else if (openCharCount === 0 && charPairs.has(char)) {
  8289. openChar = char;
  8290. closeChar = charPairs.get(char);
  8291. openCharCount = 1;
  8292. blockStartIndex = i + 1;
  8293. resultParts.push(input.substring(nonBlockStartIndex, blockStartIndex));
  8294. }
  8295. }
  8296. if (blockStartIndex !== -1) {
  8297. escapedBlocks.push(input.substring(blockStartIndex));
  8298. resultParts.push(placeholder);
  8299. }
  8300. else {
  8301. resultParts.push(input.substring(nonBlockStartIndex));
  8302. }
  8303. return new StringWithEscapedBlocks(resultParts.join(''), escapedBlocks);
  8304. }
  8305. /**
  8306. * Object containing as keys characters that should be substituted by placeholders
  8307. * when found in strings during the css text parsing, and as values the respective
  8308. * placeholders
  8309. */
  8310. const ESCAPE_IN_STRING_MAP = {
  8311. ';': SEMI_IN_PLACEHOLDER,
  8312. ',': COMMA_IN_PLACEHOLDER,
  8313. ':': COLON_IN_PLACEHOLDER,
  8314. };
  8315. /**
  8316. * Parse the provided css text and inside strings (meaning, inside pairs of unescaped single or
  8317. * double quotes) replace specific characters with their respective placeholders as indicated
  8318. * by the `ESCAPE_IN_STRING_MAP` map.
  8319. *
  8320. * For example convert the text
  8321. * `animation: "my-anim:at\"ion" 1s;`
  8322. * to
  8323. * `animation: "my-anim%COLON_IN_PLACEHOLDER%at\"ion" 1s;`
  8324. *
  8325. * This is necessary in order to remove the meaning of some characters when found inside strings
  8326. * (for example `;` indicates the end of a css declaration, `,` the sequence of values and `:` the
  8327. * division between property and value during a declaration, none of these meanings apply when such
  8328. * characters are within strings and so in order to prevent parsing issues they need to be replaced
  8329. * with placeholder text for the duration of the css manipulation process).
  8330. *
  8331. * @param input the original css text.
  8332. *
  8333. * @returns the css text with specific characters in strings replaced by placeholders.
  8334. **/
  8335. function escapeInStrings(input) {
  8336. let result = input;
  8337. let currentQuoteChar = null;
  8338. for (let i = 0; i < result.length; i++) {
  8339. const char = result[i];
  8340. if (char === '\\') {
  8341. i++;
  8342. }
  8343. else {
  8344. if (currentQuoteChar !== null) {
  8345. // index i is inside a quoted sub-string
  8346. if (char === currentQuoteChar) {
  8347. currentQuoteChar = null;
  8348. }
  8349. else {
  8350. const placeholder = ESCAPE_IN_STRING_MAP[char];
  8351. if (placeholder) {
  8352. result = `${result.substr(0, i)}${placeholder}${result.substr(i + 1)}`;
  8353. i += placeholder.length - 1;
  8354. }
  8355. }
  8356. }
  8357. else if (char === "'" || char === '"') {
  8358. currentQuoteChar = char;
  8359. }
  8360. }
  8361. }
  8362. return result;
  8363. }
  8364. /**
  8365. * Replace in a string all occurrences of keys in the `ESCAPE_IN_STRING_MAP` map with their
  8366. * original representation, this is simply used to revert the changes applied by the
  8367. * escapeInStrings function.
  8368. *
  8369. * For example it reverts the text:
  8370. * `animation: "my-anim%COLON_IN_PLACEHOLDER%at\"ion" 1s;`
  8371. * to it's original form of:
  8372. * `animation: "my-anim:at\"ion" 1s;`
  8373. *
  8374. * Note: For the sake of simplicity this function does not check that the placeholders are
  8375. * actually inside strings as it would anyway be extremely unlikely to find them outside of strings.
  8376. *
  8377. * @param input the css text containing the placeholders.
  8378. *
  8379. * @returns the css text without the placeholders.
  8380. */
  8381. function unescapeInStrings(input) {
  8382. let result = input.replace(_cssCommaInPlaceholderReGlobal, ',');
  8383. result = result.replace(_cssSemiInPlaceholderReGlobal, ';');
  8384. result = result.replace(_cssColonInPlaceholderReGlobal, ':');
  8385. return result;
  8386. }
  8387. /**
  8388. * Unescape all quotes present in a string, but only if the string was actually already
  8389. * quoted.
  8390. *
  8391. * This generates a "canonical" representation of strings which can be used to match strings
  8392. * which would otherwise only differ because of differently escaped quotes.
  8393. *
  8394. * For example it converts the string (assumed to be quoted):
  8395. * `this \\"is\\" a \\'\\\\'test`
  8396. * to:
  8397. * `this "is" a '\\\\'test`
  8398. * (note that the latter backslashes are not removed as they are not actually escaping the single
  8399. * quote)
  8400. *
  8401. *
  8402. * @param input the string possibly containing escaped quotes.
  8403. * @param isQuoted boolean indicating whether the string was quoted inside a bigger string (if not
  8404. * then it means that it doesn't represent an inner string and thus no unescaping is required)
  8405. *
  8406. * @returns the string in the "canonical" representation without escaped quotes.
  8407. */
  8408. function unescapeQuotes(str, isQuoted) {
  8409. return !isQuoted ? str : str.replace(/((?:^|[^\\])(?:\\\\)*)\\(?=['"])/g, '$1');
  8410. }
  8411. /**
  8412. * Combine the `contextSelectors` with the `hostMarker` and the `otherSelectors`
  8413. * to create a selector that matches the same as `:host-context()`.
  8414. *
  8415. * Given a single context selector `A` we need to output selectors that match on the host and as an
  8416. * ancestor of the host:
  8417. *
  8418. * ```
  8419. * A <hostMarker>, A<hostMarker> {}
  8420. * ```
  8421. *
  8422. * When there is more than one context selector we also have to create combinations of those
  8423. * selectors with each other. For example if there are `A` and `B` selectors the output is:
  8424. *
  8425. * ```
  8426. * AB<hostMarker>, AB <hostMarker>, A B<hostMarker>,
  8427. * B A<hostMarker>, A B <hostMarker>, B A <hostMarker> {}
  8428. * ```
  8429. *
  8430. * And so on...
  8431. *
  8432. * @param contextSelectors an array of context selectors that will be combined.
  8433. * @param otherSelectors the rest of the selectors that are not context selectors.
  8434. */
  8435. function _combineHostContextSelectors(contextSelectors, otherSelectors, pseudoPrefix = '') {
  8436. const hostMarker = _polyfillHostNoCombinator;
  8437. _polyfillHostRe.lastIndex = 0; // reset the regex to ensure we get an accurate test
  8438. const otherSelectorsHasHost = _polyfillHostRe.test(otherSelectors);
  8439. // If there are no context selectors then just output a host marker
  8440. if (contextSelectors.length === 0) {
  8441. return hostMarker + otherSelectors;
  8442. }
  8443. const combined = [contextSelectors.pop() || ''];
  8444. while (contextSelectors.length > 0) {
  8445. const length = combined.length;
  8446. const contextSelector = contextSelectors.pop();
  8447. for (let i = 0; i < length; i++) {
  8448. const previousSelectors = combined[i];
  8449. // Add the new selector as a descendant of the previous selectors
  8450. combined[length * 2 + i] = previousSelectors + ' ' + contextSelector;
  8451. // Add the new selector as an ancestor of the previous selectors
  8452. combined[length + i] = contextSelector + ' ' + previousSelectors;
  8453. // Add the new selector to act on the same element as the previous selectors
  8454. combined[i] = contextSelector + previousSelectors;
  8455. }
  8456. }
  8457. // Finally connect the selector to the `hostMarker`s: either acting directly on the host
  8458. // (A<hostMarker>) or as an ancestor (A <hostMarker>).
  8459. return combined
  8460. .map((s) => otherSelectorsHasHost
  8461. ? `${pseudoPrefix}${s}${otherSelectors}`
  8462. : `${pseudoPrefix}${s}${hostMarker}${otherSelectors}, ${pseudoPrefix}${s} ${hostMarker}${otherSelectors}`)
  8463. .join(',');
  8464. }
  8465. /**
  8466. * Mutate the given `groups` array so that there are `multiples` clones of the original array
  8467. * stored.
  8468. *
  8469. * For example `repeatGroups([a, b], 3)` will result in `[a, b, a, b, a, b]` - but importantly the
  8470. * newly added groups will be clones of the original.
  8471. *
  8472. * @param groups An array of groups of strings that will be repeated. This array is mutated
  8473. * in-place.
  8474. * @param multiples The number of times the current groups should appear.
  8475. */
  8476. function repeatGroups(groups, multiples) {
  8477. const length = groups.length;
  8478. for (let i = 1; i < multiples; i++) {
  8479. for (let j = 0; j < length; j++) {
  8480. groups[j + i * length] = groups[j].slice(0);
  8481. }
  8482. }
  8483. }
  8484. /**
  8485. * Distinguishes different kinds of IR operations.
  8486. *
  8487. * Includes both creation and update operations.
  8488. */
  8489. var OpKind;
  8490. (function (OpKind) {
  8491. /**
  8492. * A special operation type which is used to represent the beginning and end nodes of a linked
  8493. * list of operations.
  8494. */
  8495. OpKind[OpKind["ListEnd"] = 0] = "ListEnd";
  8496. /**
  8497. * An operation which wraps an output AST statement.
  8498. */
  8499. OpKind[OpKind["Statement"] = 1] = "Statement";
  8500. /**
  8501. * An operation which declares and initializes a `SemanticVariable`.
  8502. */
  8503. OpKind[OpKind["Variable"] = 2] = "Variable";
  8504. /**
  8505. * An operation to begin rendering of an element.
  8506. */
  8507. OpKind[OpKind["ElementStart"] = 3] = "ElementStart";
  8508. /**
  8509. * An operation to render an element with no children.
  8510. */
  8511. OpKind[OpKind["Element"] = 4] = "Element";
  8512. /**
  8513. * An operation which declares an embedded view.
  8514. */
  8515. OpKind[OpKind["Template"] = 5] = "Template";
  8516. /**
  8517. * An operation to end rendering of an element previously started with `ElementStart`.
  8518. */
  8519. OpKind[OpKind["ElementEnd"] = 6] = "ElementEnd";
  8520. /**
  8521. * An operation to begin an `ng-container`.
  8522. */
  8523. OpKind[OpKind["ContainerStart"] = 7] = "ContainerStart";
  8524. /**
  8525. * An operation for an `ng-container` with no children.
  8526. */
  8527. OpKind[OpKind["Container"] = 8] = "Container";
  8528. /**
  8529. * An operation to end an `ng-container`.
  8530. */
  8531. OpKind[OpKind["ContainerEnd"] = 9] = "ContainerEnd";
  8532. /**
  8533. * An operation disable binding for subsequent elements, which are descendants of a non-bindable
  8534. * node.
  8535. */
  8536. OpKind[OpKind["DisableBindings"] = 10] = "DisableBindings";
  8537. /**
  8538. * An op to conditionally render a template.
  8539. */
  8540. OpKind[OpKind["Conditional"] = 11] = "Conditional";
  8541. /**
  8542. * An operation to re-enable binding, after it was previously disabled.
  8543. */
  8544. OpKind[OpKind["EnableBindings"] = 12] = "EnableBindings";
  8545. /**
  8546. * An operation to render a text node.
  8547. */
  8548. OpKind[OpKind["Text"] = 13] = "Text";
  8549. /**
  8550. * An operation declaring an event listener for an element.
  8551. */
  8552. OpKind[OpKind["Listener"] = 14] = "Listener";
  8553. /**
  8554. * An operation to interpolate text into a text node.
  8555. */
  8556. OpKind[OpKind["InterpolateText"] = 15] = "InterpolateText";
  8557. /**
  8558. * An intermediate binding op, that has not yet been processed into an individual property,
  8559. * attribute, style, etc.
  8560. */
  8561. OpKind[OpKind["Binding"] = 16] = "Binding";
  8562. /**
  8563. * An operation to bind an expression to a property of an element.
  8564. */
  8565. OpKind[OpKind["Property"] = 17] = "Property";
  8566. /**
  8567. * An operation to bind an expression to a style property of an element.
  8568. */
  8569. OpKind[OpKind["StyleProp"] = 18] = "StyleProp";
  8570. /**
  8571. * An operation to bind an expression to a class property of an element.
  8572. */
  8573. OpKind[OpKind["ClassProp"] = 19] = "ClassProp";
  8574. /**
  8575. * An operation to bind an expression to the styles of an element.
  8576. */
  8577. OpKind[OpKind["StyleMap"] = 20] = "StyleMap";
  8578. /**
  8579. * An operation to bind an expression to the classes of an element.
  8580. */
  8581. OpKind[OpKind["ClassMap"] = 21] = "ClassMap";
  8582. /**
  8583. * An operation to advance the runtime's implicit slot context during the update phase of a view.
  8584. */
  8585. OpKind[OpKind["Advance"] = 22] = "Advance";
  8586. /**
  8587. * An operation to instantiate a pipe.
  8588. */
  8589. OpKind[OpKind["Pipe"] = 23] = "Pipe";
  8590. /**
  8591. * An operation to associate an attribute with an element.
  8592. */
  8593. OpKind[OpKind["Attribute"] = 24] = "Attribute";
  8594. /**
  8595. * An attribute that has been extracted for inclusion in the consts array.
  8596. */
  8597. OpKind[OpKind["ExtractedAttribute"] = 25] = "ExtractedAttribute";
  8598. /**
  8599. * An operation that configures a `@defer` block.
  8600. */
  8601. OpKind[OpKind["Defer"] = 26] = "Defer";
  8602. /**
  8603. * An operation that controls when a `@defer` loads.
  8604. */
  8605. OpKind[OpKind["DeferOn"] = 27] = "DeferOn";
  8606. /**
  8607. * An operation that controls when a `@defer` loads, using a custom expression as the condition.
  8608. */
  8609. OpKind[OpKind["DeferWhen"] = 28] = "DeferWhen";
  8610. /**
  8611. * An i18n message that has been extracted for inclusion in the consts array.
  8612. */
  8613. OpKind[OpKind["I18nMessage"] = 29] = "I18nMessage";
  8614. /**
  8615. * A host binding property.
  8616. */
  8617. OpKind[OpKind["HostProperty"] = 30] = "HostProperty";
  8618. /**
  8619. * A namespace change, which causes the subsequent elements to be processed as either HTML or SVG.
  8620. */
  8621. OpKind[OpKind["Namespace"] = 31] = "Namespace";
  8622. /**
  8623. * Configure a content projeciton definition for the view.
  8624. */
  8625. OpKind[OpKind["ProjectionDef"] = 32] = "ProjectionDef";
  8626. /**
  8627. * Create a content projection slot.
  8628. */
  8629. OpKind[OpKind["Projection"] = 33] = "Projection";
  8630. /**
  8631. * Create a repeater creation instruction op.
  8632. */
  8633. OpKind[OpKind["RepeaterCreate"] = 34] = "RepeaterCreate";
  8634. /**
  8635. * An update up for a repeater.
  8636. */
  8637. OpKind[OpKind["Repeater"] = 35] = "Repeater";
  8638. /**
  8639. * An operation to bind an expression to the property side of a two-way binding.
  8640. */
  8641. OpKind[OpKind["TwoWayProperty"] = 36] = "TwoWayProperty";
  8642. /**
  8643. * An operation declaring the event side of a two-way binding.
  8644. */
  8645. OpKind[OpKind["TwoWayListener"] = 37] = "TwoWayListener";
  8646. /**
  8647. * A creation-time operation that initializes the slot for a `@let` declaration.
  8648. */
  8649. OpKind[OpKind["DeclareLet"] = 38] = "DeclareLet";
  8650. /**
  8651. * An update-time operation that stores the current value of a `@let` declaration.
  8652. */
  8653. OpKind[OpKind["StoreLet"] = 39] = "StoreLet";
  8654. /**
  8655. * The start of an i18n block.
  8656. */
  8657. OpKind[OpKind["I18nStart"] = 40] = "I18nStart";
  8658. /**
  8659. * A self-closing i18n on a single element.
  8660. */
  8661. OpKind[OpKind["I18n"] = 41] = "I18n";
  8662. /**
  8663. * The end of an i18n block.
  8664. */
  8665. OpKind[OpKind["I18nEnd"] = 42] = "I18nEnd";
  8666. /**
  8667. * An expression in an i18n message.
  8668. */
  8669. OpKind[OpKind["I18nExpression"] = 43] = "I18nExpression";
  8670. /**
  8671. * An instruction that applies a set of i18n expressions.
  8672. */
  8673. OpKind[OpKind["I18nApply"] = 44] = "I18nApply";
  8674. /**
  8675. * An instruction to create an ICU expression.
  8676. */
  8677. OpKind[OpKind["IcuStart"] = 45] = "IcuStart";
  8678. /**
  8679. * An instruction to update an ICU expression.
  8680. */
  8681. OpKind[OpKind["IcuEnd"] = 46] = "IcuEnd";
  8682. /**
  8683. * An instruction representing a placeholder in an ICU expression.
  8684. */
  8685. OpKind[OpKind["IcuPlaceholder"] = 47] = "IcuPlaceholder";
  8686. /**
  8687. * An i18n context containing information needed to generate an i18n message.
  8688. */
  8689. OpKind[OpKind["I18nContext"] = 48] = "I18nContext";
  8690. /**
  8691. * A creation op that corresponds to i18n attributes on an element.
  8692. */
  8693. OpKind[OpKind["I18nAttributes"] = 49] = "I18nAttributes";
  8694. /**
  8695. * Creation op that attaches the location at which an element was defined in a template to it.
  8696. */
  8697. OpKind[OpKind["SourceLocation"] = 50] = "SourceLocation";
  8698. })(OpKind || (OpKind = {}));
  8699. /**
  8700. * Distinguishes different kinds of IR expressions.
  8701. */
  8702. var ExpressionKind;
  8703. (function (ExpressionKind) {
  8704. /**
  8705. * Read of a variable in a lexical scope.
  8706. */
  8707. ExpressionKind[ExpressionKind["LexicalRead"] = 0] = "LexicalRead";
  8708. /**
  8709. * A reference to the current view context.
  8710. */
  8711. ExpressionKind[ExpressionKind["Context"] = 1] = "Context";
  8712. /**
  8713. * A reference to the view context, for use inside a track function.
  8714. */
  8715. ExpressionKind[ExpressionKind["TrackContext"] = 2] = "TrackContext";
  8716. /**
  8717. * Read of a variable declared in a `VariableOp`.
  8718. */
  8719. ExpressionKind[ExpressionKind["ReadVariable"] = 3] = "ReadVariable";
  8720. /**
  8721. * Runtime operation to navigate to the next view context in the view hierarchy.
  8722. */
  8723. ExpressionKind[ExpressionKind["NextContext"] = 4] = "NextContext";
  8724. /**
  8725. * Runtime operation to retrieve the value of a local reference.
  8726. */
  8727. ExpressionKind[ExpressionKind["Reference"] = 5] = "Reference";
  8728. /**
  8729. * A call storing the value of a `@let` declaration.
  8730. */
  8731. ExpressionKind[ExpressionKind["StoreLet"] = 6] = "StoreLet";
  8732. /**
  8733. * A reference to a `@let` declaration read from the context view.
  8734. */
  8735. ExpressionKind[ExpressionKind["ContextLetReference"] = 7] = "ContextLetReference";
  8736. /**
  8737. * Runtime operation to snapshot the current view context.
  8738. */
  8739. ExpressionKind[ExpressionKind["GetCurrentView"] = 8] = "GetCurrentView";
  8740. /**
  8741. * Runtime operation to restore a snapshotted view.
  8742. */
  8743. ExpressionKind[ExpressionKind["RestoreView"] = 9] = "RestoreView";
  8744. /**
  8745. * Runtime operation to reset the current view context after `RestoreView`.
  8746. */
  8747. ExpressionKind[ExpressionKind["ResetView"] = 10] = "ResetView";
  8748. /**
  8749. * Defines and calls a function with change-detected arguments.
  8750. */
  8751. ExpressionKind[ExpressionKind["PureFunctionExpr"] = 11] = "PureFunctionExpr";
  8752. /**
  8753. * Indicates a positional parameter to a pure function definition.
  8754. */
  8755. ExpressionKind[ExpressionKind["PureFunctionParameterExpr"] = 12] = "PureFunctionParameterExpr";
  8756. /**
  8757. * Binding to a pipe transformation.
  8758. */
  8759. ExpressionKind[ExpressionKind["PipeBinding"] = 13] = "PipeBinding";
  8760. /**
  8761. * Binding to a pipe transformation with a variable number of arguments.
  8762. */
  8763. ExpressionKind[ExpressionKind["PipeBindingVariadic"] = 14] = "PipeBindingVariadic";
  8764. /*
  8765. * A safe property read requiring expansion into a null check.
  8766. */
  8767. ExpressionKind[ExpressionKind["SafePropertyRead"] = 15] = "SafePropertyRead";
  8768. /**
  8769. * A safe keyed read requiring expansion into a null check.
  8770. */
  8771. ExpressionKind[ExpressionKind["SafeKeyedRead"] = 16] = "SafeKeyedRead";
  8772. /**
  8773. * A safe function call requiring expansion into a null check.
  8774. */
  8775. ExpressionKind[ExpressionKind["SafeInvokeFunction"] = 17] = "SafeInvokeFunction";
  8776. /**
  8777. * An intermediate expression that will be expanded from a safe read into an explicit ternary.
  8778. */
  8779. ExpressionKind[ExpressionKind["SafeTernaryExpr"] = 18] = "SafeTernaryExpr";
  8780. /**
  8781. * An empty expression that will be stipped before generating the final output.
  8782. */
  8783. ExpressionKind[ExpressionKind["EmptyExpr"] = 19] = "EmptyExpr";
  8784. /*
  8785. * An assignment to a temporary variable.
  8786. */
  8787. ExpressionKind[ExpressionKind["AssignTemporaryExpr"] = 20] = "AssignTemporaryExpr";
  8788. /**
  8789. * A reference to a temporary variable.
  8790. */
  8791. ExpressionKind[ExpressionKind["ReadTemporaryExpr"] = 21] = "ReadTemporaryExpr";
  8792. /**
  8793. * An expression that will cause a literal slot index to be emitted.
  8794. */
  8795. ExpressionKind[ExpressionKind["SlotLiteralExpr"] = 22] = "SlotLiteralExpr";
  8796. /**
  8797. * A test expression for a conditional op.
  8798. */
  8799. ExpressionKind[ExpressionKind["ConditionalCase"] = 23] = "ConditionalCase";
  8800. /**
  8801. * An expression that will be automatically extracted to the component const array.
  8802. */
  8803. ExpressionKind[ExpressionKind["ConstCollected"] = 24] = "ConstCollected";
  8804. /**
  8805. * Operation that sets the value of a two-way binding.
  8806. */
  8807. ExpressionKind[ExpressionKind["TwoWayBindingSet"] = 25] = "TwoWayBindingSet";
  8808. })(ExpressionKind || (ExpressionKind = {}));
  8809. var VariableFlags;
  8810. (function (VariableFlags) {
  8811. VariableFlags[VariableFlags["None"] = 0] = "None";
  8812. /**
  8813. * Always inline this variable, regardless of the number of times it's used.
  8814. * An `AlwaysInline` variable may not depend on context, because doing so may cause side effects
  8815. * that are illegal when multi-inlined. (The optimizer will enforce this constraint.)
  8816. */
  8817. VariableFlags[VariableFlags["AlwaysInline"] = 1] = "AlwaysInline";
  8818. })(VariableFlags || (VariableFlags = {}));
  8819. /**
  8820. * Distinguishes between different kinds of `SemanticVariable`s.
  8821. */
  8822. var SemanticVariableKind;
  8823. (function (SemanticVariableKind) {
  8824. /**
  8825. * Represents the context of a particular view.
  8826. */
  8827. SemanticVariableKind[SemanticVariableKind["Context"] = 0] = "Context";
  8828. /**
  8829. * Represents an identifier declared in the lexical scope of a view.
  8830. */
  8831. SemanticVariableKind[SemanticVariableKind["Identifier"] = 1] = "Identifier";
  8832. /**
  8833. * Represents a saved state that can be used to restore a view in a listener handler function.
  8834. */
  8835. SemanticVariableKind[SemanticVariableKind["SavedView"] = 2] = "SavedView";
  8836. /**
  8837. * An alias generated by a special embedded view type (e.g. a `@for` block).
  8838. */
  8839. SemanticVariableKind[SemanticVariableKind["Alias"] = 3] = "Alias";
  8840. })(SemanticVariableKind || (SemanticVariableKind = {}));
  8841. /**
  8842. * Whether to compile in compatibilty mode. In compatibility mode, the template pipeline will
  8843. * attempt to match the output of `TemplateDefinitionBuilder` as exactly as possible, at the cost
  8844. * of producing quirky or larger code in some cases.
  8845. */
  8846. var CompatibilityMode;
  8847. (function (CompatibilityMode) {
  8848. CompatibilityMode[CompatibilityMode["Normal"] = 0] = "Normal";
  8849. CompatibilityMode[CompatibilityMode["TemplateDefinitionBuilder"] = 1] = "TemplateDefinitionBuilder";
  8850. })(CompatibilityMode || (CompatibilityMode = {}));
  8851. /**
  8852. * Enumeration of the types of attributes which can be applied to an element.
  8853. */
  8854. var BindingKind;
  8855. (function (BindingKind) {
  8856. /**
  8857. * Static attributes.
  8858. */
  8859. BindingKind[BindingKind["Attribute"] = 0] = "Attribute";
  8860. /**
  8861. * Class bindings.
  8862. */
  8863. BindingKind[BindingKind["ClassName"] = 1] = "ClassName";
  8864. /**
  8865. * Style bindings.
  8866. */
  8867. BindingKind[BindingKind["StyleProperty"] = 2] = "StyleProperty";
  8868. /**
  8869. * Dynamic property bindings.
  8870. */
  8871. BindingKind[BindingKind["Property"] = 3] = "Property";
  8872. /**
  8873. * Property or attribute bindings on a template.
  8874. */
  8875. BindingKind[BindingKind["Template"] = 4] = "Template";
  8876. /**
  8877. * Internationalized attributes.
  8878. */
  8879. BindingKind[BindingKind["I18n"] = 5] = "I18n";
  8880. /**
  8881. * Animation property bindings.
  8882. */
  8883. BindingKind[BindingKind["Animation"] = 6] = "Animation";
  8884. /**
  8885. * Property side of a two-way binding.
  8886. */
  8887. BindingKind[BindingKind["TwoWayProperty"] = 7] = "TwoWayProperty";
  8888. })(BindingKind || (BindingKind = {}));
  8889. /**
  8890. * Enumeration of possible times i18n params can be resolved.
  8891. */
  8892. var I18nParamResolutionTime;
  8893. (function (I18nParamResolutionTime) {
  8894. /**
  8895. * Param is resolved at message creation time. Most params should be resolved at message creation
  8896. * time. However, ICU params need to be handled in post-processing.
  8897. */
  8898. I18nParamResolutionTime[I18nParamResolutionTime["Creation"] = 0] = "Creation";
  8899. /**
  8900. * Param is resolved during post-processing. This should be used for params whose value comes from
  8901. * an ICU.
  8902. */
  8903. I18nParamResolutionTime[I18nParamResolutionTime["Postproccessing"] = 1] = "Postproccessing";
  8904. })(I18nParamResolutionTime || (I18nParamResolutionTime = {}));
  8905. /**
  8906. * The contexts in which an i18n expression can be used.
  8907. */
  8908. var I18nExpressionFor;
  8909. (function (I18nExpressionFor) {
  8910. /**
  8911. * This expression is used as a value (i.e. inside an i18n block).
  8912. */
  8913. I18nExpressionFor[I18nExpressionFor["I18nText"] = 0] = "I18nText";
  8914. /**
  8915. * This expression is used in a binding.
  8916. */
  8917. I18nExpressionFor[I18nExpressionFor["I18nAttribute"] = 1] = "I18nAttribute";
  8918. })(I18nExpressionFor || (I18nExpressionFor = {}));
  8919. /**
  8920. * Flags that describe what an i18n param value. These determine how the value is serialized into
  8921. * the final map.
  8922. */
  8923. var I18nParamValueFlags;
  8924. (function (I18nParamValueFlags) {
  8925. I18nParamValueFlags[I18nParamValueFlags["None"] = 0] = "None";
  8926. /**
  8927. * This value represents an element tag.
  8928. */
  8929. I18nParamValueFlags[I18nParamValueFlags["ElementTag"] = 1] = "ElementTag";
  8930. /**
  8931. * This value represents a template tag.
  8932. */
  8933. I18nParamValueFlags[I18nParamValueFlags["TemplateTag"] = 2] = "TemplateTag";
  8934. /**
  8935. * This value represents the opening of a tag.
  8936. */
  8937. I18nParamValueFlags[I18nParamValueFlags["OpenTag"] = 4] = "OpenTag";
  8938. /**
  8939. * This value represents the closing of a tag.
  8940. */
  8941. I18nParamValueFlags[I18nParamValueFlags["CloseTag"] = 8] = "CloseTag";
  8942. /**
  8943. * This value represents an i18n expression index.
  8944. */
  8945. I18nParamValueFlags[I18nParamValueFlags["ExpressionIndex"] = 16] = "ExpressionIndex";
  8946. })(I18nParamValueFlags || (I18nParamValueFlags = {}));
  8947. /**
  8948. * Whether the active namespace is HTML, MathML, or SVG mode.
  8949. */
  8950. var Namespace;
  8951. (function (Namespace) {
  8952. Namespace[Namespace["HTML"] = 0] = "HTML";
  8953. Namespace[Namespace["SVG"] = 1] = "SVG";
  8954. Namespace[Namespace["Math"] = 2] = "Math";
  8955. })(Namespace || (Namespace = {}));
  8956. /**
  8957. * The type of a `@defer` trigger, for use in the ir.
  8958. */
  8959. var DeferTriggerKind;
  8960. (function (DeferTriggerKind) {
  8961. DeferTriggerKind[DeferTriggerKind["Idle"] = 0] = "Idle";
  8962. DeferTriggerKind[DeferTriggerKind["Immediate"] = 1] = "Immediate";
  8963. DeferTriggerKind[DeferTriggerKind["Timer"] = 2] = "Timer";
  8964. DeferTriggerKind[DeferTriggerKind["Hover"] = 3] = "Hover";
  8965. DeferTriggerKind[DeferTriggerKind["Interaction"] = 4] = "Interaction";
  8966. DeferTriggerKind[DeferTriggerKind["Viewport"] = 5] = "Viewport";
  8967. DeferTriggerKind[DeferTriggerKind["Never"] = 6] = "Never";
  8968. })(DeferTriggerKind || (DeferTriggerKind = {}));
  8969. /**
  8970. * Kinds of i18n contexts. They can be created because of root i18n blocks, or ICUs.
  8971. */
  8972. var I18nContextKind;
  8973. (function (I18nContextKind) {
  8974. I18nContextKind[I18nContextKind["RootI18n"] = 0] = "RootI18n";
  8975. I18nContextKind[I18nContextKind["Icu"] = 1] = "Icu";
  8976. I18nContextKind[I18nContextKind["Attr"] = 2] = "Attr";
  8977. })(I18nContextKind || (I18nContextKind = {}));
  8978. var TemplateKind;
  8979. (function (TemplateKind) {
  8980. TemplateKind[TemplateKind["NgTemplate"] = 0] = "NgTemplate";
  8981. TemplateKind[TemplateKind["Structural"] = 1] = "Structural";
  8982. TemplateKind[TemplateKind["Block"] = 2] = "Block";
  8983. })(TemplateKind || (TemplateKind = {}));
  8984. /**
  8985. * Marker symbol for `ConsumesSlotOpTrait`.
  8986. */
  8987. const ConsumesSlot = Symbol('ConsumesSlot');
  8988. /**
  8989. * Marker symbol for `DependsOnSlotContextOpTrait`.
  8990. */
  8991. const DependsOnSlotContext = Symbol('DependsOnSlotContext');
  8992. /**
  8993. * Marker symbol for `ConsumesVars` trait.
  8994. */
  8995. const ConsumesVarsTrait = Symbol('ConsumesVars');
  8996. /**
  8997. * Marker symbol for `UsesVarOffset` trait.
  8998. */
  8999. const UsesVarOffset = Symbol('UsesVarOffset');
  9000. /**
  9001. * Default values for most `ConsumesSlotOpTrait` fields (used with the spread operator to initialize
  9002. * implementors of the trait).
  9003. */
  9004. const TRAIT_CONSUMES_SLOT = {
  9005. [ConsumesSlot]: true,
  9006. numSlotsUsed: 1,
  9007. };
  9008. /**
  9009. * Default values for most `DependsOnSlotContextOpTrait` fields (used with the spread operator to
  9010. * initialize implementors of the trait).
  9011. */
  9012. const TRAIT_DEPENDS_ON_SLOT_CONTEXT = {
  9013. [DependsOnSlotContext]: true,
  9014. };
  9015. /**
  9016. * Default values for `UsesVars` fields (used with the spread operator to initialize
  9017. * implementors of the trait).
  9018. */
  9019. const TRAIT_CONSUMES_VARS = {
  9020. [ConsumesVarsTrait]: true,
  9021. };
  9022. /**
  9023. * Test whether an operation implements `ConsumesSlotOpTrait`.
  9024. */
  9025. function hasConsumesSlotTrait(op) {
  9026. return op[ConsumesSlot] === true;
  9027. }
  9028. function hasDependsOnSlotContextTrait(value) {
  9029. return value[DependsOnSlotContext] === true;
  9030. }
  9031. function hasConsumesVarsTrait(value) {
  9032. return value[ConsumesVarsTrait] === true;
  9033. }
  9034. /**
  9035. * Test whether an expression implements `UsesVarOffsetTrait`.
  9036. */
  9037. function hasUsesVarOffsetTrait(expr) {
  9038. return expr[UsesVarOffset] === true;
  9039. }
  9040. /**
  9041. * Create a `StatementOp`.
  9042. */
  9043. function createStatementOp(statement) {
  9044. return {
  9045. kind: OpKind.Statement,
  9046. statement,
  9047. ...NEW_OP,
  9048. };
  9049. }
  9050. /**
  9051. * Create a `VariableOp`.
  9052. */
  9053. function createVariableOp(xref, variable, initializer, flags) {
  9054. return {
  9055. kind: OpKind.Variable,
  9056. xref,
  9057. variable,
  9058. initializer,
  9059. flags,
  9060. ...NEW_OP,
  9061. };
  9062. }
  9063. /**
  9064. * Static structure shared by all operations.
  9065. *
  9066. * Used as a convenience via the spread operator (`...NEW_OP`) when creating new operations, and
  9067. * ensures the fields are always in the same order.
  9068. */
  9069. const NEW_OP = {
  9070. debugListId: null,
  9071. prev: null,
  9072. next: null,
  9073. };
  9074. /**
  9075. * Create an `InterpolationTextOp`.
  9076. */
  9077. function createInterpolateTextOp(xref, interpolation, sourceSpan) {
  9078. return {
  9079. kind: OpKind.InterpolateText,
  9080. target: xref,
  9081. interpolation,
  9082. sourceSpan,
  9083. ...TRAIT_DEPENDS_ON_SLOT_CONTEXT,
  9084. ...TRAIT_CONSUMES_VARS,
  9085. ...NEW_OP,
  9086. };
  9087. }
  9088. class Interpolation {
  9089. strings;
  9090. expressions;
  9091. i18nPlaceholders;
  9092. constructor(strings, expressions, i18nPlaceholders) {
  9093. this.strings = strings;
  9094. this.expressions = expressions;
  9095. this.i18nPlaceholders = i18nPlaceholders;
  9096. if (i18nPlaceholders.length !== 0 && i18nPlaceholders.length !== expressions.length) {
  9097. throw new Error(`Expected ${expressions.length} placeholders to match interpolation expression count, but got ${i18nPlaceholders.length}`);
  9098. }
  9099. }
  9100. }
  9101. /**
  9102. * Create a `BindingOp`, not yet transformed into a particular type of binding.
  9103. */
  9104. function createBindingOp(target, kind, name, expression, unit, securityContext, isTextAttribute, isStructuralTemplateAttribute, templateKind, i18nMessage, sourceSpan) {
  9105. return {
  9106. kind: OpKind.Binding,
  9107. bindingKind: kind,
  9108. target,
  9109. name,
  9110. expression,
  9111. unit,
  9112. securityContext,
  9113. isTextAttribute,
  9114. isStructuralTemplateAttribute,
  9115. templateKind,
  9116. i18nContext: null,
  9117. i18nMessage,
  9118. sourceSpan,
  9119. ...NEW_OP,
  9120. };
  9121. }
  9122. /**
  9123. * Create a `PropertyOp`.
  9124. */
  9125. function createPropertyOp(target, name, expression, isAnimationTrigger, securityContext, isStructuralTemplateAttribute, templateKind, i18nContext, i18nMessage, sourceSpan) {
  9126. return {
  9127. kind: OpKind.Property,
  9128. target,
  9129. name,
  9130. expression,
  9131. isAnimationTrigger,
  9132. securityContext,
  9133. sanitizer: null,
  9134. isStructuralTemplateAttribute,
  9135. templateKind,
  9136. i18nContext,
  9137. i18nMessage,
  9138. sourceSpan,
  9139. ...TRAIT_DEPENDS_ON_SLOT_CONTEXT,
  9140. ...TRAIT_CONSUMES_VARS,
  9141. ...NEW_OP,
  9142. };
  9143. }
  9144. /**
  9145. * Create a `TwoWayPropertyOp`.
  9146. */
  9147. function createTwoWayPropertyOp(target, name, expression, securityContext, isStructuralTemplateAttribute, templateKind, i18nContext, i18nMessage, sourceSpan) {
  9148. return {
  9149. kind: OpKind.TwoWayProperty,
  9150. target,
  9151. name,
  9152. expression,
  9153. securityContext,
  9154. sanitizer: null,
  9155. isStructuralTemplateAttribute,
  9156. templateKind,
  9157. i18nContext,
  9158. i18nMessage,
  9159. sourceSpan,
  9160. ...TRAIT_DEPENDS_ON_SLOT_CONTEXT,
  9161. ...TRAIT_CONSUMES_VARS,
  9162. ...NEW_OP,
  9163. };
  9164. }
  9165. /** Create a `StylePropOp`. */
  9166. function createStylePropOp(xref, name, expression, unit, sourceSpan) {
  9167. return {
  9168. kind: OpKind.StyleProp,
  9169. target: xref,
  9170. name,
  9171. expression,
  9172. unit,
  9173. sourceSpan,
  9174. ...TRAIT_DEPENDS_ON_SLOT_CONTEXT,
  9175. ...TRAIT_CONSUMES_VARS,
  9176. ...NEW_OP,
  9177. };
  9178. }
  9179. /**
  9180. * Create a `ClassPropOp`.
  9181. */
  9182. function createClassPropOp(xref, name, expression, sourceSpan) {
  9183. return {
  9184. kind: OpKind.ClassProp,
  9185. target: xref,
  9186. name,
  9187. expression,
  9188. sourceSpan,
  9189. ...TRAIT_DEPENDS_ON_SLOT_CONTEXT,
  9190. ...TRAIT_CONSUMES_VARS,
  9191. ...NEW_OP,
  9192. };
  9193. }
  9194. /** Create a `StyleMapOp`. */
  9195. function createStyleMapOp(xref, expression, sourceSpan) {
  9196. return {
  9197. kind: OpKind.StyleMap,
  9198. target: xref,
  9199. expression,
  9200. sourceSpan,
  9201. ...TRAIT_DEPENDS_ON_SLOT_CONTEXT,
  9202. ...TRAIT_CONSUMES_VARS,
  9203. ...NEW_OP,
  9204. };
  9205. }
  9206. /**
  9207. * Create a `ClassMapOp`.
  9208. */
  9209. function createClassMapOp(xref, expression, sourceSpan) {
  9210. return {
  9211. kind: OpKind.ClassMap,
  9212. target: xref,
  9213. expression,
  9214. sourceSpan,
  9215. ...TRAIT_DEPENDS_ON_SLOT_CONTEXT,
  9216. ...TRAIT_CONSUMES_VARS,
  9217. ...NEW_OP,
  9218. };
  9219. }
  9220. /**
  9221. * Create an `AttributeOp`.
  9222. */
  9223. function createAttributeOp(target, namespace, name, expression, securityContext, isTextAttribute, isStructuralTemplateAttribute, templateKind, i18nMessage, sourceSpan) {
  9224. return {
  9225. kind: OpKind.Attribute,
  9226. target,
  9227. namespace,
  9228. name,
  9229. expression,
  9230. securityContext,
  9231. sanitizer: null,
  9232. isTextAttribute,
  9233. isStructuralTemplateAttribute,
  9234. templateKind,
  9235. i18nContext: null,
  9236. i18nMessage,
  9237. sourceSpan,
  9238. ...TRAIT_DEPENDS_ON_SLOT_CONTEXT,
  9239. ...TRAIT_CONSUMES_VARS,
  9240. ...NEW_OP,
  9241. };
  9242. }
  9243. /**
  9244. * Create an `AdvanceOp`.
  9245. */
  9246. function createAdvanceOp(delta, sourceSpan) {
  9247. return {
  9248. kind: OpKind.Advance,
  9249. delta,
  9250. sourceSpan,
  9251. ...NEW_OP,
  9252. };
  9253. }
  9254. /**
  9255. * Create a conditional op, which will display an embedded view according to a condtion.
  9256. */
  9257. function createConditionalOp(target, test, conditions, sourceSpan) {
  9258. return {
  9259. kind: OpKind.Conditional,
  9260. target,
  9261. test,
  9262. conditions,
  9263. processed: null,
  9264. sourceSpan,
  9265. contextValue: null,
  9266. ...NEW_OP,
  9267. ...TRAIT_DEPENDS_ON_SLOT_CONTEXT,
  9268. ...TRAIT_CONSUMES_VARS,
  9269. };
  9270. }
  9271. function createRepeaterOp(repeaterCreate, targetSlot, collection, sourceSpan) {
  9272. return {
  9273. kind: OpKind.Repeater,
  9274. target: repeaterCreate,
  9275. targetSlot,
  9276. collection,
  9277. sourceSpan,
  9278. ...NEW_OP,
  9279. ...TRAIT_DEPENDS_ON_SLOT_CONTEXT,
  9280. };
  9281. }
  9282. function createDeferWhenOp(target, expr, modifier, sourceSpan) {
  9283. return {
  9284. kind: OpKind.DeferWhen,
  9285. target,
  9286. expr,
  9287. modifier,
  9288. sourceSpan,
  9289. ...NEW_OP,
  9290. ...TRAIT_DEPENDS_ON_SLOT_CONTEXT,
  9291. ...TRAIT_CONSUMES_VARS,
  9292. };
  9293. }
  9294. /**
  9295. * Create an i18n expression op.
  9296. */
  9297. function createI18nExpressionOp(context, target, i18nOwner, handle, expression, icuPlaceholder, i18nPlaceholder, resolutionTime, usage, name, sourceSpan) {
  9298. return {
  9299. kind: OpKind.I18nExpression,
  9300. context,
  9301. target,
  9302. i18nOwner,
  9303. handle,
  9304. expression,
  9305. icuPlaceholder,
  9306. i18nPlaceholder,
  9307. resolutionTime,
  9308. usage,
  9309. name,
  9310. sourceSpan,
  9311. ...NEW_OP,
  9312. ...TRAIT_CONSUMES_VARS,
  9313. ...TRAIT_DEPENDS_ON_SLOT_CONTEXT,
  9314. };
  9315. }
  9316. /**
  9317. * Creates an op to apply i18n expression ops.
  9318. */
  9319. function createI18nApplyOp(owner, handle, sourceSpan) {
  9320. return {
  9321. kind: OpKind.I18nApply,
  9322. owner,
  9323. handle,
  9324. sourceSpan,
  9325. ...NEW_OP,
  9326. };
  9327. }
  9328. /**
  9329. * Creates a `StoreLetOp`.
  9330. */
  9331. function createStoreLetOp(target, declaredName, value, sourceSpan) {
  9332. return {
  9333. kind: OpKind.StoreLet,
  9334. target,
  9335. declaredName,
  9336. value,
  9337. sourceSpan,
  9338. ...TRAIT_DEPENDS_ON_SLOT_CONTEXT,
  9339. ...TRAIT_CONSUMES_VARS,
  9340. ...NEW_OP,
  9341. };
  9342. }
  9343. /**
  9344. * Check whether a given `o.Expression` is a logical IR expression type.
  9345. */
  9346. function isIrExpression(expr) {
  9347. return expr instanceof ExpressionBase;
  9348. }
  9349. /**
  9350. * Base type used for all logical IR expressions.
  9351. */
  9352. class ExpressionBase extends Expression {
  9353. constructor(sourceSpan = null) {
  9354. super(null, sourceSpan);
  9355. }
  9356. }
  9357. /**
  9358. * Logical expression representing a lexical read of a variable name.
  9359. */
  9360. class LexicalReadExpr extends ExpressionBase {
  9361. name;
  9362. kind = ExpressionKind.LexicalRead;
  9363. constructor(name) {
  9364. super();
  9365. this.name = name;
  9366. }
  9367. visitExpression(visitor, context) { }
  9368. isEquivalent(other) {
  9369. // We assume that the lexical reads are in the same context, which must be true for parent
  9370. // expressions to be equivalent.
  9371. // TODO: is this generally safe?
  9372. return this.name === other.name;
  9373. }
  9374. isConstant() {
  9375. return false;
  9376. }
  9377. transformInternalExpressions() { }
  9378. clone() {
  9379. return new LexicalReadExpr(this.name);
  9380. }
  9381. }
  9382. /**
  9383. * Runtime operation to retrieve the value of a local reference.
  9384. */
  9385. class ReferenceExpr extends ExpressionBase {
  9386. target;
  9387. targetSlot;
  9388. offset;
  9389. kind = ExpressionKind.Reference;
  9390. constructor(target, targetSlot, offset) {
  9391. super();
  9392. this.target = target;
  9393. this.targetSlot = targetSlot;
  9394. this.offset = offset;
  9395. }
  9396. visitExpression() { }
  9397. isEquivalent(e) {
  9398. return e instanceof ReferenceExpr && e.target === this.target;
  9399. }
  9400. isConstant() {
  9401. return false;
  9402. }
  9403. transformInternalExpressions() { }
  9404. clone() {
  9405. return new ReferenceExpr(this.target, this.targetSlot, this.offset);
  9406. }
  9407. }
  9408. class StoreLetExpr extends ExpressionBase {
  9409. target;
  9410. value;
  9411. sourceSpan;
  9412. kind = ExpressionKind.StoreLet;
  9413. [ConsumesVarsTrait] = true;
  9414. [DependsOnSlotContext] = true;
  9415. constructor(target, value, sourceSpan) {
  9416. super();
  9417. this.target = target;
  9418. this.value = value;
  9419. this.sourceSpan = sourceSpan;
  9420. }
  9421. visitExpression() { }
  9422. isEquivalent(e) {
  9423. return (e instanceof StoreLetExpr && e.target === this.target && e.value.isEquivalent(this.value));
  9424. }
  9425. isConstant() {
  9426. return false;
  9427. }
  9428. transformInternalExpressions(transform, flags) {
  9429. this.value = transformExpressionsInExpression(this.value, transform, flags);
  9430. }
  9431. clone() {
  9432. return new StoreLetExpr(this.target, this.value, this.sourceSpan);
  9433. }
  9434. }
  9435. class ContextLetReferenceExpr extends ExpressionBase {
  9436. target;
  9437. targetSlot;
  9438. kind = ExpressionKind.ContextLetReference;
  9439. constructor(target, targetSlot) {
  9440. super();
  9441. this.target = target;
  9442. this.targetSlot = targetSlot;
  9443. }
  9444. visitExpression() { }
  9445. isEquivalent(e) {
  9446. return e instanceof ContextLetReferenceExpr && e.target === this.target;
  9447. }
  9448. isConstant() {
  9449. return false;
  9450. }
  9451. transformInternalExpressions() { }
  9452. clone() {
  9453. return new ContextLetReferenceExpr(this.target, this.targetSlot);
  9454. }
  9455. }
  9456. /**
  9457. * A reference to the current view context (usually the `ctx` variable in a template function).
  9458. */
  9459. class ContextExpr extends ExpressionBase {
  9460. view;
  9461. kind = ExpressionKind.Context;
  9462. constructor(view) {
  9463. super();
  9464. this.view = view;
  9465. }
  9466. visitExpression() { }
  9467. isEquivalent(e) {
  9468. return e instanceof ContextExpr && e.view === this.view;
  9469. }
  9470. isConstant() {
  9471. return false;
  9472. }
  9473. transformInternalExpressions() { }
  9474. clone() {
  9475. return new ContextExpr(this.view);
  9476. }
  9477. }
  9478. /**
  9479. * A reference to the current view context inside a track function.
  9480. */
  9481. class TrackContextExpr extends ExpressionBase {
  9482. view;
  9483. kind = ExpressionKind.TrackContext;
  9484. constructor(view) {
  9485. super();
  9486. this.view = view;
  9487. }
  9488. visitExpression() { }
  9489. isEquivalent(e) {
  9490. return e instanceof TrackContextExpr && e.view === this.view;
  9491. }
  9492. isConstant() {
  9493. return false;
  9494. }
  9495. transformInternalExpressions() { }
  9496. clone() {
  9497. return new TrackContextExpr(this.view);
  9498. }
  9499. }
  9500. /**
  9501. * Runtime operation to navigate to the next view context in the view hierarchy.
  9502. */
  9503. class NextContextExpr extends ExpressionBase {
  9504. kind = ExpressionKind.NextContext;
  9505. steps = 1;
  9506. constructor() {
  9507. super();
  9508. }
  9509. visitExpression() { }
  9510. isEquivalent(e) {
  9511. return e instanceof NextContextExpr && e.steps === this.steps;
  9512. }
  9513. isConstant() {
  9514. return false;
  9515. }
  9516. transformInternalExpressions() { }
  9517. clone() {
  9518. const expr = new NextContextExpr();
  9519. expr.steps = this.steps;
  9520. return expr;
  9521. }
  9522. }
  9523. /**
  9524. * Runtime operation to snapshot the current view context.
  9525. *
  9526. * The result of this operation can be stored in a variable and later used with the `RestoreView`
  9527. * operation.
  9528. */
  9529. class GetCurrentViewExpr extends ExpressionBase {
  9530. kind = ExpressionKind.GetCurrentView;
  9531. constructor() {
  9532. super();
  9533. }
  9534. visitExpression() { }
  9535. isEquivalent(e) {
  9536. return e instanceof GetCurrentViewExpr;
  9537. }
  9538. isConstant() {
  9539. return false;
  9540. }
  9541. transformInternalExpressions() { }
  9542. clone() {
  9543. return new GetCurrentViewExpr();
  9544. }
  9545. }
  9546. /**
  9547. * Runtime operation to restore a snapshotted view.
  9548. */
  9549. class RestoreViewExpr extends ExpressionBase {
  9550. view;
  9551. kind = ExpressionKind.RestoreView;
  9552. constructor(view) {
  9553. super();
  9554. this.view = view;
  9555. }
  9556. visitExpression(visitor, context) {
  9557. if (typeof this.view !== 'number') {
  9558. this.view.visitExpression(visitor, context);
  9559. }
  9560. }
  9561. isEquivalent(e) {
  9562. if (!(e instanceof RestoreViewExpr) || typeof e.view !== typeof this.view) {
  9563. return false;
  9564. }
  9565. if (typeof this.view === 'number') {
  9566. return this.view === e.view;
  9567. }
  9568. else {
  9569. return this.view.isEquivalent(e.view);
  9570. }
  9571. }
  9572. isConstant() {
  9573. return false;
  9574. }
  9575. transformInternalExpressions(transform, flags) {
  9576. if (typeof this.view !== 'number') {
  9577. this.view = transformExpressionsInExpression(this.view, transform, flags);
  9578. }
  9579. }
  9580. clone() {
  9581. return new RestoreViewExpr(this.view instanceof Expression ? this.view.clone() : this.view);
  9582. }
  9583. }
  9584. /**
  9585. * Runtime operation to reset the current view context after `RestoreView`.
  9586. */
  9587. class ResetViewExpr extends ExpressionBase {
  9588. expr;
  9589. kind = ExpressionKind.ResetView;
  9590. constructor(expr) {
  9591. super();
  9592. this.expr = expr;
  9593. }
  9594. visitExpression(visitor, context) {
  9595. this.expr.visitExpression(visitor, context);
  9596. }
  9597. isEquivalent(e) {
  9598. return e instanceof ResetViewExpr && this.expr.isEquivalent(e.expr);
  9599. }
  9600. isConstant() {
  9601. return false;
  9602. }
  9603. transformInternalExpressions(transform, flags) {
  9604. this.expr = transformExpressionsInExpression(this.expr, transform, flags);
  9605. }
  9606. clone() {
  9607. return new ResetViewExpr(this.expr.clone());
  9608. }
  9609. }
  9610. class TwoWayBindingSetExpr extends ExpressionBase {
  9611. target;
  9612. value;
  9613. kind = ExpressionKind.TwoWayBindingSet;
  9614. constructor(target, value) {
  9615. super();
  9616. this.target = target;
  9617. this.value = value;
  9618. }
  9619. visitExpression(visitor, context) {
  9620. this.target.visitExpression(visitor, context);
  9621. this.value.visitExpression(visitor, context);
  9622. }
  9623. isEquivalent(other) {
  9624. return this.target.isEquivalent(other.target) && this.value.isEquivalent(other.value);
  9625. }
  9626. isConstant() {
  9627. return false;
  9628. }
  9629. transformInternalExpressions(transform, flags) {
  9630. this.target = transformExpressionsInExpression(this.target, transform, flags);
  9631. this.value = transformExpressionsInExpression(this.value, transform, flags);
  9632. }
  9633. clone() {
  9634. return new TwoWayBindingSetExpr(this.target, this.value);
  9635. }
  9636. }
  9637. /**
  9638. * Read of a variable declared as an `ir.VariableOp` and referenced through its `ir.XrefId`.
  9639. */
  9640. class ReadVariableExpr extends ExpressionBase {
  9641. xref;
  9642. kind = ExpressionKind.ReadVariable;
  9643. name = null;
  9644. constructor(xref) {
  9645. super();
  9646. this.xref = xref;
  9647. }
  9648. visitExpression() { }
  9649. isEquivalent(other) {
  9650. return other instanceof ReadVariableExpr && other.xref === this.xref;
  9651. }
  9652. isConstant() {
  9653. return false;
  9654. }
  9655. transformInternalExpressions() { }
  9656. clone() {
  9657. const expr = new ReadVariableExpr(this.xref);
  9658. expr.name = this.name;
  9659. return expr;
  9660. }
  9661. }
  9662. class PureFunctionExpr extends ExpressionBase {
  9663. kind = ExpressionKind.PureFunctionExpr;
  9664. [ConsumesVarsTrait] = true;
  9665. [UsesVarOffset] = true;
  9666. varOffset = null;
  9667. /**
  9668. * The expression which should be memoized as a pure computation.
  9669. *
  9670. * This expression contains internal `PureFunctionParameterExpr`s, which are placeholders for the
  9671. * positional argument expressions in `args.
  9672. */
  9673. body;
  9674. /**
  9675. * Positional arguments to the pure function which will memoize the `body` expression, which act
  9676. * as memoization keys.
  9677. */
  9678. args;
  9679. /**
  9680. * Once extracted to the `ConstantPool`, a reference to the function which defines the computation
  9681. * of `body`.
  9682. */
  9683. fn = null;
  9684. constructor(expression, args) {
  9685. super();
  9686. this.body = expression;
  9687. this.args = args;
  9688. }
  9689. visitExpression(visitor, context) {
  9690. this.body?.visitExpression(visitor, context);
  9691. for (const arg of this.args) {
  9692. arg.visitExpression(visitor, context);
  9693. }
  9694. }
  9695. isEquivalent(other) {
  9696. if (!(other instanceof PureFunctionExpr) || other.args.length !== this.args.length) {
  9697. return false;
  9698. }
  9699. return (other.body !== null &&
  9700. this.body !== null &&
  9701. other.body.isEquivalent(this.body) &&
  9702. other.args.every((arg, idx) => arg.isEquivalent(this.args[idx])));
  9703. }
  9704. isConstant() {
  9705. return false;
  9706. }
  9707. transformInternalExpressions(transform, flags) {
  9708. if (this.body !== null) {
  9709. // TODO: figure out if this is the right flag to pass here.
  9710. this.body = transformExpressionsInExpression(this.body, transform, flags | VisitorContextFlag.InChildOperation);
  9711. }
  9712. else if (this.fn !== null) {
  9713. this.fn = transformExpressionsInExpression(this.fn, transform, flags);
  9714. }
  9715. for (let i = 0; i < this.args.length; i++) {
  9716. this.args[i] = transformExpressionsInExpression(this.args[i], transform, flags);
  9717. }
  9718. }
  9719. clone() {
  9720. const expr = new PureFunctionExpr(this.body?.clone() ?? null, this.args.map((arg) => arg.clone()));
  9721. expr.fn = this.fn?.clone() ?? null;
  9722. expr.varOffset = this.varOffset;
  9723. return expr;
  9724. }
  9725. }
  9726. class PureFunctionParameterExpr extends ExpressionBase {
  9727. index;
  9728. kind = ExpressionKind.PureFunctionParameterExpr;
  9729. constructor(index) {
  9730. super();
  9731. this.index = index;
  9732. }
  9733. visitExpression() { }
  9734. isEquivalent(other) {
  9735. return other instanceof PureFunctionParameterExpr && other.index === this.index;
  9736. }
  9737. isConstant() {
  9738. return true;
  9739. }
  9740. transformInternalExpressions() { }
  9741. clone() {
  9742. return new PureFunctionParameterExpr(this.index);
  9743. }
  9744. }
  9745. class PipeBindingExpr extends ExpressionBase {
  9746. target;
  9747. targetSlot;
  9748. name;
  9749. args;
  9750. kind = ExpressionKind.PipeBinding;
  9751. [ConsumesVarsTrait] = true;
  9752. [UsesVarOffset] = true;
  9753. varOffset = null;
  9754. constructor(target, targetSlot, name, args) {
  9755. super();
  9756. this.target = target;
  9757. this.targetSlot = targetSlot;
  9758. this.name = name;
  9759. this.args = args;
  9760. }
  9761. visitExpression(visitor, context) {
  9762. for (const arg of this.args) {
  9763. arg.visitExpression(visitor, context);
  9764. }
  9765. }
  9766. isEquivalent() {
  9767. return false;
  9768. }
  9769. isConstant() {
  9770. return false;
  9771. }
  9772. transformInternalExpressions(transform, flags) {
  9773. for (let idx = 0; idx < this.args.length; idx++) {
  9774. this.args[idx] = transformExpressionsInExpression(this.args[idx], transform, flags);
  9775. }
  9776. }
  9777. clone() {
  9778. const r = new PipeBindingExpr(this.target, this.targetSlot, this.name, this.args.map((a) => a.clone()));
  9779. r.varOffset = this.varOffset;
  9780. return r;
  9781. }
  9782. }
  9783. class PipeBindingVariadicExpr extends ExpressionBase {
  9784. target;
  9785. targetSlot;
  9786. name;
  9787. args;
  9788. numArgs;
  9789. kind = ExpressionKind.PipeBindingVariadic;
  9790. [ConsumesVarsTrait] = true;
  9791. [UsesVarOffset] = true;
  9792. varOffset = null;
  9793. constructor(target, targetSlot, name, args, numArgs) {
  9794. super();
  9795. this.target = target;
  9796. this.targetSlot = targetSlot;
  9797. this.name = name;
  9798. this.args = args;
  9799. this.numArgs = numArgs;
  9800. }
  9801. visitExpression(visitor, context) {
  9802. this.args.visitExpression(visitor, context);
  9803. }
  9804. isEquivalent() {
  9805. return false;
  9806. }
  9807. isConstant() {
  9808. return false;
  9809. }
  9810. transformInternalExpressions(transform, flags) {
  9811. this.args = transformExpressionsInExpression(this.args, transform, flags);
  9812. }
  9813. clone() {
  9814. const r = new PipeBindingVariadicExpr(this.target, this.targetSlot, this.name, this.args.clone(), this.numArgs);
  9815. r.varOffset = this.varOffset;
  9816. return r;
  9817. }
  9818. }
  9819. class SafePropertyReadExpr extends ExpressionBase {
  9820. receiver;
  9821. name;
  9822. kind = ExpressionKind.SafePropertyRead;
  9823. constructor(receiver, name) {
  9824. super();
  9825. this.receiver = receiver;
  9826. this.name = name;
  9827. }
  9828. // An alias for name, which allows other logic to handle property reads and keyed reads together.
  9829. get index() {
  9830. return this.name;
  9831. }
  9832. visitExpression(visitor, context) {
  9833. this.receiver.visitExpression(visitor, context);
  9834. }
  9835. isEquivalent() {
  9836. return false;
  9837. }
  9838. isConstant() {
  9839. return false;
  9840. }
  9841. transformInternalExpressions(transform, flags) {
  9842. this.receiver = transformExpressionsInExpression(this.receiver, transform, flags);
  9843. }
  9844. clone() {
  9845. return new SafePropertyReadExpr(this.receiver.clone(), this.name);
  9846. }
  9847. }
  9848. class SafeKeyedReadExpr extends ExpressionBase {
  9849. receiver;
  9850. index;
  9851. kind = ExpressionKind.SafeKeyedRead;
  9852. constructor(receiver, index, sourceSpan) {
  9853. super(sourceSpan);
  9854. this.receiver = receiver;
  9855. this.index = index;
  9856. }
  9857. visitExpression(visitor, context) {
  9858. this.receiver.visitExpression(visitor, context);
  9859. this.index.visitExpression(visitor, context);
  9860. }
  9861. isEquivalent() {
  9862. return false;
  9863. }
  9864. isConstant() {
  9865. return false;
  9866. }
  9867. transformInternalExpressions(transform, flags) {
  9868. this.receiver = transformExpressionsInExpression(this.receiver, transform, flags);
  9869. this.index = transformExpressionsInExpression(this.index, transform, flags);
  9870. }
  9871. clone() {
  9872. return new SafeKeyedReadExpr(this.receiver.clone(), this.index.clone(), this.sourceSpan);
  9873. }
  9874. }
  9875. class SafeInvokeFunctionExpr extends ExpressionBase {
  9876. receiver;
  9877. args;
  9878. kind = ExpressionKind.SafeInvokeFunction;
  9879. constructor(receiver, args) {
  9880. super();
  9881. this.receiver = receiver;
  9882. this.args = args;
  9883. }
  9884. visitExpression(visitor, context) {
  9885. this.receiver.visitExpression(visitor, context);
  9886. for (const a of this.args) {
  9887. a.visitExpression(visitor, context);
  9888. }
  9889. }
  9890. isEquivalent() {
  9891. return false;
  9892. }
  9893. isConstant() {
  9894. return false;
  9895. }
  9896. transformInternalExpressions(transform, flags) {
  9897. this.receiver = transformExpressionsInExpression(this.receiver, transform, flags);
  9898. for (let i = 0; i < this.args.length; i++) {
  9899. this.args[i] = transformExpressionsInExpression(this.args[i], transform, flags);
  9900. }
  9901. }
  9902. clone() {
  9903. return new SafeInvokeFunctionExpr(this.receiver.clone(), this.args.map((a) => a.clone()));
  9904. }
  9905. }
  9906. class SafeTernaryExpr extends ExpressionBase {
  9907. guard;
  9908. expr;
  9909. kind = ExpressionKind.SafeTernaryExpr;
  9910. constructor(guard, expr) {
  9911. super();
  9912. this.guard = guard;
  9913. this.expr = expr;
  9914. }
  9915. visitExpression(visitor, context) {
  9916. this.guard.visitExpression(visitor, context);
  9917. this.expr.visitExpression(visitor, context);
  9918. }
  9919. isEquivalent() {
  9920. return false;
  9921. }
  9922. isConstant() {
  9923. return false;
  9924. }
  9925. transformInternalExpressions(transform, flags) {
  9926. this.guard = transformExpressionsInExpression(this.guard, transform, flags);
  9927. this.expr = transformExpressionsInExpression(this.expr, transform, flags);
  9928. }
  9929. clone() {
  9930. return new SafeTernaryExpr(this.guard.clone(), this.expr.clone());
  9931. }
  9932. }
  9933. class EmptyExpr extends ExpressionBase {
  9934. kind = ExpressionKind.EmptyExpr;
  9935. visitExpression(visitor, context) { }
  9936. isEquivalent(e) {
  9937. return e instanceof EmptyExpr;
  9938. }
  9939. isConstant() {
  9940. return true;
  9941. }
  9942. clone() {
  9943. return new EmptyExpr();
  9944. }
  9945. transformInternalExpressions() { }
  9946. }
  9947. class AssignTemporaryExpr extends ExpressionBase {
  9948. expr;
  9949. xref;
  9950. kind = ExpressionKind.AssignTemporaryExpr;
  9951. name = null;
  9952. constructor(expr, xref) {
  9953. super();
  9954. this.expr = expr;
  9955. this.xref = xref;
  9956. }
  9957. visitExpression(visitor, context) {
  9958. this.expr.visitExpression(visitor, context);
  9959. }
  9960. isEquivalent() {
  9961. return false;
  9962. }
  9963. isConstant() {
  9964. return false;
  9965. }
  9966. transformInternalExpressions(transform, flags) {
  9967. this.expr = transformExpressionsInExpression(this.expr, transform, flags);
  9968. }
  9969. clone() {
  9970. const a = new AssignTemporaryExpr(this.expr.clone(), this.xref);
  9971. a.name = this.name;
  9972. return a;
  9973. }
  9974. }
  9975. class ReadTemporaryExpr extends ExpressionBase {
  9976. xref;
  9977. kind = ExpressionKind.ReadTemporaryExpr;
  9978. name = null;
  9979. constructor(xref) {
  9980. super();
  9981. this.xref = xref;
  9982. }
  9983. visitExpression(visitor, context) { }
  9984. isEquivalent() {
  9985. return this.xref === this.xref;
  9986. }
  9987. isConstant() {
  9988. return false;
  9989. }
  9990. transformInternalExpressions(transform, flags) { }
  9991. clone() {
  9992. const r = new ReadTemporaryExpr(this.xref);
  9993. r.name = this.name;
  9994. return r;
  9995. }
  9996. }
  9997. class SlotLiteralExpr extends ExpressionBase {
  9998. slot;
  9999. kind = ExpressionKind.SlotLiteralExpr;
  10000. constructor(slot) {
  10001. super();
  10002. this.slot = slot;
  10003. }
  10004. visitExpression(visitor, context) { }
  10005. isEquivalent(e) {
  10006. return e instanceof SlotLiteralExpr && e.slot === this.slot;
  10007. }
  10008. isConstant() {
  10009. return true;
  10010. }
  10011. clone() {
  10012. return new SlotLiteralExpr(this.slot);
  10013. }
  10014. transformInternalExpressions() { }
  10015. }
  10016. class ConditionalCaseExpr extends ExpressionBase {
  10017. expr;
  10018. target;
  10019. targetSlot;
  10020. alias;
  10021. kind = ExpressionKind.ConditionalCase;
  10022. /**
  10023. * Create an expression for one branch of a conditional.
  10024. * @param expr The expression to be tested for this case. Might be null, as in an `else` case.
  10025. * @param target The Xref of the view to be displayed if this condition is true.
  10026. */
  10027. constructor(expr, target, targetSlot, alias = null) {
  10028. super();
  10029. this.expr = expr;
  10030. this.target = target;
  10031. this.targetSlot = targetSlot;
  10032. this.alias = alias;
  10033. }
  10034. visitExpression(visitor, context) {
  10035. if (this.expr !== null) {
  10036. this.expr.visitExpression(visitor, context);
  10037. }
  10038. }
  10039. isEquivalent(e) {
  10040. return e instanceof ConditionalCaseExpr && e.expr === this.expr;
  10041. }
  10042. isConstant() {
  10043. return true;
  10044. }
  10045. clone() {
  10046. return new ConditionalCaseExpr(this.expr, this.target, this.targetSlot);
  10047. }
  10048. transformInternalExpressions(transform, flags) {
  10049. if (this.expr !== null) {
  10050. this.expr = transformExpressionsInExpression(this.expr, transform, flags);
  10051. }
  10052. }
  10053. }
  10054. class ConstCollectedExpr extends ExpressionBase {
  10055. expr;
  10056. kind = ExpressionKind.ConstCollected;
  10057. constructor(expr) {
  10058. super();
  10059. this.expr = expr;
  10060. }
  10061. transformInternalExpressions(transform, flags) {
  10062. this.expr = transform(this.expr, flags);
  10063. }
  10064. visitExpression(visitor, context) {
  10065. this.expr.visitExpression(visitor, context);
  10066. }
  10067. isEquivalent(e) {
  10068. if (!(e instanceof ConstCollectedExpr)) {
  10069. return false;
  10070. }
  10071. return this.expr.isEquivalent(e.expr);
  10072. }
  10073. isConstant() {
  10074. return this.expr.isConstant();
  10075. }
  10076. clone() {
  10077. return new ConstCollectedExpr(this.expr);
  10078. }
  10079. }
  10080. /**
  10081. * Visits all `Expression`s in the AST of `op` with the `visitor` function.
  10082. */
  10083. function visitExpressionsInOp(op, visitor) {
  10084. transformExpressionsInOp(op, (expr, flags) => {
  10085. visitor(expr, flags);
  10086. return expr;
  10087. }, VisitorContextFlag.None);
  10088. }
  10089. var VisitorContextFlag;
  10090. (function (VisitorContextFlag) {
  10091. VisitorContextFlag[VisitorContextFlag["None"] = 0] = "None";
  10092. VisitorContextFlag[VisitorContextFlag["InChildOperation"] = 1] = "InChildOperation";
  10093. })(VisitorContextFlag || (VisitorContextFlag = {}));
  10094. function transformExpressionsInInterpolation(interpolation, transform, flags) {
  10095. for (let i = 0; i < interpolation.expressions.length; i++) {
  10096. interpolation.expressions[i] = transformExpressionsInExpression(interpolation.expressions[i], transform, flags);
  10097. }
  10098. }
  10099. /**
  10100. * Transform all `Expression`s in the AST of `op` with the `transform` function.
  10101. *
  10102. * All such operations will be replaced with the result of applying `transform`, which may be an
  10103. * identity transformation.
  10104. */
  10105. function transformExpressionsInOp(op, transform, flags) {
  10106. switch (op.kind) {
  10107. case OpKind.StyleProp:
  10108. case OpKind.StyleMap:
  10109. case OpKind.ClassProp:
  10110. case OpKind.ClassMap:
  10111. case OpKind.Binding:
  10112. if (op.expression instanceof Interpolation) {
  10113. transformExpressionsInInterpolation(op.expression, transform, flags);
  10114. }
  10115. else {
  10116. op.expression = transformExpressionsInExpression(op.expression, transform, flags);
  10117. }
  10118. break;
  10119. case OpKind.Property:
  10120. case OpKind.HostProperty:
  10121. case OpKind.Attribute:
  10122. if (op.expression instanceof Interpolation) {
  10123. transformExpressionsInInterpolation(op.expression, transform, flags);
  10124. }
  10125. else {
  10126. op.expression = transformExpressionsInExpression(op.expression, transform, flags);
  10127. }
  10128. op.sanitizer =
  10129. op.sanitizer && transformExpressionsInExpression(op.sanitizer, transform, flags);
  10130. break;
  10131. case OpKind.TwoWayProperty:
  10132. op.expression = transformExpressionsInExpression(op.expression, transform, flags);
  10133. op.sanitizer =
  10134. op.sanitizer && transformExpressionsInExpression(op.sanitizer, transform, flags);
  10135. break;
  10136. case OpKind.I18nExpression:
  10137. op.expression = transformExpressionsInExpression(op.expression, transform, flags);
  10138. break;
  10139. case OpKind.InterpolateText:
  10140. transformExpressionsInInterpolation(op.interpolation, transform, flags);
  10141. break;
  10142. case OpKind.Statement:
  10143. transformExpressionsInStatement(op.statement, transform, flags);
  10144. break;
  10145. case OpKind.Variable:
  10146. op.initializer = transformExpressionsInExpression(op.initializer, transform, flags);
  10147. break;
  10148. case OpKind.Conditional:
  10149. for (const condition of op.conditions) {
  10150. if (condition.expr === null) {
  10151. // This is a default case.
  10152. continue;
  10153. }
  10154. condition.expr = transformExpressionsInExpression(condition.expr, transform, flags);
  10155. }
  10156. if (op.processed !== null) {
  10157. op.processed = transformExpressionsInExpression(op.processed, transform, flags);
  10158. }
  10159. if (op.contextValue !== null) {
  10160. op.contextValue = transformExpressionsInExpression(op.contextValue, transform, flags);
  10161. }
  10162. break;
  10163. case OpKind.Listener:
  10164. case OpKind.TwoWayListener:
  10165. for (const innerOp of op.handlerOps) {
  10166. transformExpressionsInOp(innerOp, transform, flags | VisitorContextFlag.InChildOperation);
  10167. }
  10168. break;
  10169. case OpKind.ExtractedAttribute:
  10170. op.expression =
  10171. op.expression && transformExpressionsInExpression(op.expression, transform, flags);
  10172. op.trustedValueFn =
  10173. op.trustedValueFn && transformExpressionsInExpression(op.trustedValueFn, transform, flags);
  10174. break;
  10175. case OpKind.RepeaterCreate:
  10176. if (op.trackByOps === null) {
  10177. op.track = transformExpressionsInExpression(op.track, transform, flags);
  10178. }
  10179. else {
  10180. for (const innerOp of op.trackByOps) {
  10181. transformExpressionsInOp(innerOp, transform, flags | VisitorContextFlag.InChildOperation);
  10182. }
  10183. }
  10184. if (op.trackByFn !== null) {
  10185. op.trackByFn = transformExpressionsInExpression(op.trackByFn, transform, flags);
  10186. }
  10187. break;
  10188. case OpKind.Repeater:
  10189. op.collection = transformExpressionsInExpression(op.collection, transform, flags);
  10190. break;
  10191. case OpKind.Defer:
  10192. if (op.loadingConfig !== null) {
  10193. op.loadingConfig = transformExpressionsInExpression(op.loadingConfig, transform, flags);
  10194. }
  10195. if (op.placeholderConfig !== null) {
  10196. op.placeholderConfig = transformExpressionsInExpression(op.placeholderConfig, transform, flags);
  10197. }
  10198. if (op.resolverFn !== null) {
  10199. op.resolverFn = transformExpressionsInExpression(op.resolverFn, transform, flags);
  10200. }
  10201. break;
  10202. case OpKind.I18nMessage:
  10203. for (const [placeholder, expr] of op.params) {
  10204. op.params.set(placeholder, transformExpressionsInExpression(expr, transform, flags));
  10205. }
  10206. for (const [placeholder, expr] of op.postprocessingParams) {
  10207. op.postprocessingParams.set(placeholder, transformExpressionsInExpression(expr, transform, flags));
  10208. }
  10209. break;
  10210. case OpKind.DeferWhen:
  10211. op.expr = transformExpressionsInExpression(op.expr, transform, flags);
  10212. break;
  10213. case OpKind.StoreLet:
  10214. op.value = transformExpressionsInExpression(op.value, transform, flags);
  10215. break;
  10216. case OpKind.Advance:
  10217. case OpKind.Container:
  10218. case OpKind.ContainerEnd:
  10219. case OpKind.ContainerStart:
  10220. case OpKind.DeferOn:
  10221. case OpKind.DisableBindings:
  10222. case OpKind.Element:
  10223. case OpKind.ElementEnd:
  10224. case OpKind.ElementStart:
  10225. case OpKind.EnableBindings:
  10226. case OpKind.I18n:
  10227. case OpKind.I18nApply:
  10228. case OpKind.I18nContext:
  10229. case OpKind.I18nEnd:
  10230. case OpKind.I18nStart:
  10231. case OpKind.IcuEnd:
  10232. case OpKind.IcuStart:
  10233. case OpKind.Namespace:
  10234. case OpKind.Pipe:
  10235. case OpKind.Projection:
  10236. case OpKind.ProjectionDef:
  10237. case OpKind.Template:
  10238. case OpKind.Text:
  10239. case OpKind.I18nAttributes:
  10240. case OpKind.IcuPlaceholder:
  10241. case OpKind.DeclareLet:
  10242. case OpKind.SourceLocation:
  10243. // These operations contain no expressions.
  10244. break;
  10245. default:
  10246. throw new Error(`AssertionError: transformExpressionsInOp doesn't handle ${OpKind[op.kind]}`);
  10247. }
  10248. }
  10249. /**
  10250. * Transform all `Expression`s in the AST of `expr` with the `transform` function.
  10251. *
  10252. * All such operations will be replaced with the result of applying `transform`, which may be an
  10253. * identity transformation.
  10254. */
  10255. function transformExpressionsInExpression(expr, transform, flags) {
  10256. if (expr instanceof ExpressionBase) {
  10257. expr.transformInternalExpressions(transform, flags);
  10258. }
  10259. else if (expr instanceof BinaryOperatorExpr) {
  10260. expr.lhs = transformExpressionsInExpression(expr.lhs, transform, flags);
  10261. expr.rhs = transformExpressionsInExpression(expr.rhs, transform, flags);
  10262. }
  10263. else if (expr instanceof UnaryOperatorExpr) {
  10264. expr.expr = transformExpressionsInExpression(expr.expr, transform, flags);
  10265. }
  10266. else if (expr instanceof ReadPropExpr) {
  10267. expr.receiver = transformExpressionsInExpression(expr.receiver, transform, flags);
  10268. }
  10269. else if (expr instanceof ReadKeyExpr) {
  10270. expr.receiver = transformExpressionsInExpression(expr.receiver, transform, flags);
  10271. expr.index = transformExpressionsInExpression(expr.index, transform, flags);
  10272. }
  10273. else if (expr instanceof WritePropExpr) {
  10274. expr.receiver = transformExpressionsInExpression(expr.receiver, transform, flags);
  10275. expr.value = transformExpressionsInExpression(expr.value, transform, flags);
  10276. }
  10277. else if (expr instanceof WriteKeyExpr) {
  10278. expr.receiver = transformExpressionsInExpression(expr.receiver, transform, flags);
  10279. expr.index = transformExpressionsInExpression(expr.index, transform, flags);
  10280. expr.value = transformExpressionsInExpression(expr.value, transform, flags);
  10281. }
  10282. else if (expr instanceof InvokeFunctionExpr) {
  10283. expr.fn = transformExpressionsInExpression(expr.fn, transform, flags);
  10284. for (let i = 0; i < expr.args.length; i++) {
  10285. expr.args[i] = transformExpressionsInExpression(expr.args[i], transform, flags);
  10286. }
  10287. }
  10288. else if (expr instanceof LiteralArrayExpr) {
  10289. for (let i = 0; i < expr.entries.length; i++) {
  10290. expr.entries[i] = transformExpressionsInExpression(expr.entries[i], transform, flags);
  10291. }
  10292. }
  10293. else if (expr instanceof LiteralMapExpr) {
  10294. for (let i = 0; i < expr.entries.length; i++) {
  10295. expr.entries[i].value = transformExpressionsInExpression(expr.entries[i].value, transform, flags);
  10296. }
  10297. }
  10298. else if (expr instanceof ConditionalExpr) {
  10299. expr.condition = transformExpressionsInExpression(expr.condition, transform, flags);
  10300. expr.trueCase = transformExpressionsInExpression(expr.trueCase, transform, flags);
  10301. if (expr.falseCase !== null) {
  10302. expr.falseCase = transformExpressionsInExpression(expr.falseCase, transform, flags);
  10303. }
  10304. }
  10305. else if (expr instanceof TypeofExpr) {
  10306. expr.expr = transformExpressionsInExpression(expr.expr, transform, flags);
  10307. }
  10308. else if (expr instanceof WriteVarExpr) {
  10309. expr.value = transformExpressionsInExpression(expr.value, transform, flags);
  10310. }
  10311. else if (expr instanceof LocalizedString) {
  10312. for (let i = 0; i < expr.expressions.length; i++) {
  10313. expr.expressions[i] = transformExpressionsInExpression(expr.expressions[i], transform, flags);
  10314. }
  10315. }
  10316. else if (expr instanceof NotExpr) {
  10317. expr.condition = transformExpressionsInExpression(expr.condition, transform, flags);
  10318. }
  10319. else if (expr instanceof TaggedTemplateLiteralExpr) {
  10320. expr.tag = transformExpressionsInExpression(expr.tag, transform, flags);
  10321. expr.template.expressions = expr.template.expressions.map((e) => transformExpressionsInExpression(e, transform, flags));
  10322. }
  10323. else if (expr instanceof ArrowFunctionExpr) {
  10324. if (Array.isArray(expr.body)) {
  10325. for (let i = 0; i < expr.body.length; i++) {
  10326. transformExpressionsInStatement(expr.body[i], transform, flags);
  10327. }
  10328. }
  10329. else {
  10330. expr.body = transformExpressionsInExpression(expr.body, transform, flags);
  10331. }
  10332. }
  10333. else if (expr instanceof WrappedNodeExpr) ;
  10334. else if (expr instanceof TemplateLiteralExpr) {
  10335. for (let i = 0; i < expr.expressions.length; i++) {
  10336. expr.expressions[i] = transformExpressionsInExpression(expr.expressions[i], transform, flags);
  10337. }
  10338. }
  10339. else if (expr instanceof ReadVarExpr ||
  10340. expr instanceof ExternalExpr ||
  10341. expr instanceof LiteralExpr) ;
  10342. else {
  10343. throw new Error(`Unhandled expression kind: ${expr.constructor.name}`);
  10344. }
  10345. return transform(expr, flags);
  10346. }
  10347. /**
  10348. * Transform all `Expression`s in the AST of `stmt` with the `transform` function.
  10349. *
  10350. * All such operations will be replaced with the result of applying `transform`, which may be an
  10351. * identity transformation.
  10352. */
  10353. function transformExpressionsInStatement(stmt, transform, flags) {
  10354. if (stmt instanceof ExpressionStatement) {
  10355. stmt.expr = transformExpressionsInExpression(stmt.expr, transform, flags);
  10356. }
  10357. else if (stmt instanceof ReturnStatement) {
  10358. stmt.value = transformExpressionsInExpression(stmt.value, transform, flags);
  10359. }
  10360. else if (stmt instanceof DeclareVarStmt) {
  10361. if (stmt.value !== undefined) {
  10362. stmt.value = transformExpressionsInExpression(stmt.value, transform, flags);
  10363. }
  10364. }
  10365. else if (stmt instanceof IfStmt) {
  10366. stmt.condition = transformExpressionsInExpression(stmt.condition, transform, flags);
  10367. for (const caseStatement of stmt.trueCase) {
  10368. transformExpressionsInStatement(caseStatement, transform, flags);
  10369. }
  10370. for (const caseStatement of stmt.falseCase) {
  10371. transformExpressionsInStatement(caseStatement, transform, flags);
  10372. }
  10373. }
  10374. else {
  10375. throw new Error(`Unhandled statement kind: ${stmt.constructor.name}`);
  10376. }
  10377. }
  10378. /**
  10379. * Checks whether the given expression is a string literal.
  10380. */
  10381. function isStringLiteral(expr) {
  10382. return expr instanceof LiteralExpr && typeof expr.value === 'string';
  10383. }
  10384. /**
  10385. * A linked list of `Op` nodes of a given subtype.
  10386. *
  10387. * @param OpT specific subtype of `Op` nodes which this list contains.
  10388. */
  10389. class OpList {
  10390. static nextListId = 0;
  10391. /**
  10392. * Debug ID of this `OpList` instance.
  10393. */
  10394. debugListId = OpList.nextListId++;
  10395. // OpList uses static head/tail nodes of a special `ListEnd` type.
  10396. // This avoids the need for special casing of the first and last list
  10397. // elements in all list operations.
  10398. head = {
  10399. kind: OpKind.ListEnd,
  10400. next: null,
  10401. prev: null,
  10402. debugListId: this.debugListId,
  10403. };
  10404. tail = {
  10405. kind: OpKind.ListEnd,
  10406. next: null,
  10407. prev: null,
  10408. debugListId: this.debugListId,
  10409. };
  10410. constructor() {
  10411. // Link `head` and `tail` together at the start (list is empty).
  10412. this.head.next = this.tail;
  10413. this.tail.prev = this.head;
  10414. }
  10415. /**
  10416. * Push a new operation to the tail of the list.
  10417. */
  10418. push(op) {
  10419. if (Array.isArray(op)) {
  10420. for (const o of op) {
  10421. this.push(o);
  10422. }
  10423. return;
  10424. }
  10425. OpList.assertIsNotEnd(op);
  10426. OpList.assertIsUnowned(op);
  10427. op.debugListId = this.debugListId;
  10428. // The old "previous" node (which might be the head, if the list is empty).
  10429. const oldLast = this.tail.prev;
  10430. // Insert `op` following the old last node.
  10431. op.prev = oldLast;
  10432. oldLast.next = op;
  10433. // Connect `op` with the list tail.
  10434. op.next = this.tail;
  10435. this.tail.prev = op;
  10436. }
  10437. /**
  10438. * Prepend one or more nodes to the start of the list.
  10439. */
  10440. prepend(ops) {
  10441. if (ops.length === 0) {
  10442. return;
  10443. }
  10444. for (const op of ops) {
  10445. OpList.assertIsNotEnd(op);
  10446. OpList.assertIsUnowned(op);
  10447. op.debugListId = this.debugListId;
  10448. }
  10449. const first = this.head.next;
  10450. let prev = this.head;
  10451. for (const op of ops) {
  10452. prev.next = op;
  10453. op.prev = prev;
  10454. prev = op;
  10455. }
  10456. prev.next = first;
  10457. first.prev = prev;
  10458. }
  10459. /**
  10460. * `OpList` is iterable via the iteration protocol.
  10461. *
  10462. * It's safe to mutate the part of the list that has already been returned by the iterator, up to
  10463. * and including the last operation returned. Mutations beyond that point _may_ be safe, but may
  10464. * also corrupt the iteration position and should be avoided.
  10465. */
  10466. *[Symbol.iterator]() {
  10467. let current = this.head.next;
  10468. while (current !== this.tail) {
  10469. // Guards against corruption of the iterator state by mutations to the tail of the list during
  10470. // iteration.
  10471. OpList.assertIsOwned(current, this.debugListId);
  10472. const next = current.next;
  10473. yield current;
  10474. current = next;
  10475. }
  10476. }
  10477. *reversed() {
  10478. let current = this.tail.prev;
  10479. while (current !== this.head) {
  10480. OpList.assertIsOwned(current, this.debugListId);
  10481. const prev = current.prev;
  10482. yield current;
  10483. current = prev;
  10484. }
  10485. }
  10486. /**
  10487. * Replace `oldOp` with `newOp` in the list.
  10488. */
  10489. static replace(oldOp, newOp) {
  10490. OpList.assertIsNotEnd(oldOp);
  10491. OpList.assertIsNotEnd(newOp);
  10492. OpList.assertIsOwned(oldOp);
  10493. OpList.assertIsUnowned(newOp);
  10494. newOp.debugListId = oldOp.debugListId;
  10495. if (oldOp.prev !== null) {
  10496. oldOp.prev.next = newOp;
  10497. newOp.prev = oldOp.prev;
  10498. }
  10499. if (oldOp.next !== null) {
  10500. oldOp.next.prev = newOp;
  10501. newOp.next = oldOp.next;
  10502. }
  10503. oldOp.debugListId = null;
  10504. oldOp.prev = null;
  10505. oldOp.next = null;
  10506. }
  10507. /**
  10508. * Replace `oldOp` with some number of new operations in the list (which may include `oldOp`).
  10509. */
  10510. static replaceWithMany(oldOp, newOps) {
  10511. if (newOps.length === 0) {
  10512. // Replacing with an empty list -> pure removal.
  10513. OpList.remove(oldOp);
  10514. return;
  10515. }
  10516. OpList.assertIsNotEnd(oldOp);
  10517. OpList.assertIsOwned(oldOp);
  10518. const listId = oldOp.debugListId;
  10519. oldOp.debugListId = null;
  10520. for (const newOp of newOps) {
  10521. OpList.assertIsNotEnd(newOp);
  10522. // `newOp` might be `oldOp`, but at this point it's been marked as unowned.
  10523. OpList.assertIsUnowned(newOp);
  10524. }
  10525. // It should be safe to reuse `oldOp` in the `newOps` list - maybe you want to sandwich an
  10526. // operation between two new ops.
  10527. const { prev: oldPrev, next: oldNext } = oldOp;
  10528. oldOp.prev = null;
  10529. oldOp.next = null;
  10530. let prev = oldPrev;
  10531. for (const newOp of newOps) {
  10532. this.assertIsUnowned(newOp);
  10533. newOp.debugListId = listId;
  10534. prev.next = newOp;
  10535. newOp.prev = prev;
  10536. // This _should_ be the case, but set it just in case.
  10537. newOp.next = null;
  10538. prev = newOp;
  10539. }
  10540. // At the end of iteration, `prev` holds the last node in the list.
  10541. const first = newOps[0];
  10542. const last = prev;
  10543. // Replace `oldOp` with the chain `first` -> `last`.
  10544. if (oldPrev !== null) {
  10545. oldPrev.next = first;
  10546. first.prev = oldPrev;
  10547. }
  10548. if (oldNext !== null) {
  10549. oldNext.prev = last;
  10550. last.next = oldNext;
  10551. }
  10552. }
  10553. /**
  10554. * Remove the given node from the list which contains it.
  10555. */
  10556. static remove(op) {
  10557. OpList.assertIsNotEnd(op);
  10558. OpList.assertIsOwned(op);
  10559. op.prev.next = op.next;
  10560. op.next.prev = op.prev;
  10561. // Break any link between the node and this list to safeguard against its usage in future
  10562. // operations.
  10563. op.debugListId = null;
  10564. op.prev = null;
  10565. op.next = null;
  10566. }
  10567. /**
  10568. * Insert `op` before `target`.
  10569. */
  10570. static insertBefore(op, target) {
  10571. if (Array.isArray(op)) {
  10572. for (const o of op) {
  10573. this.insertBefore(o, target);
  10574. }
  10575. return;
  10576. }
  10577. OpList.assertIsOwned(target);
  10578. if (target.prev === null) {
  10579. throw new Error(`AssertionError: illegal operation on list start`);
  10580. }
  10581. OpList.assertIsNotEnd(op);
  10582. OpList.assertIsUnowned(op);
  10583. op.debugListId = target.debugListId;
  10584. // Just in case.
  10585. op.prev = null;
  10586. target.prev.next = op;
  10587. op.prev = target.prev;
  10588. op.next = target;
  10589. target.prev = op;
  10590. }
  10591. /**
  10592. * Insert `op` after `target`.
  10593. */
  10594. static insertAfter(op, target) {
  10595. OpList.assertIsOwned(target);
  10596. if (target.next === null) {
  10597. throw new Error(`AssertionError: illegal operation on list end`);
  10598. }
  10599. OpList.assertIsNotEnd(op);
  10600. OpList.assertIsUnowned(op);
  10601. op.debugListId = target.debugListId;
  10602. target.next.prev = op;
  10603. op.next = target.next;
  10604. op.prev = target;
  10605. target.next = op;
  10606. }
  10607. /**
  10608. * Asserts that `op` does not currently belong to a list.
  10609. */
  10610. static assertIsUnowned(op) {
  10611. if (op.debugListId !== null) {
  10612. throw new Error(`AssertionError: illegal operation on owned node: ${OpKind[op.kind]}`);
  10613. }
  10614. }
  10615. /**
  10616. * Asserts that `op` currently belongs to a list. If `byList` is passed, `op` is asserted to
  10617. * specifically belong to that list.
  10618. */
  10619. static assertIsOwned(op, byList) {
  10620. if (op.debugListId === null) {
  10621. throw new Error(`AssertionError: illegal operation on unowned node: ${OpKind[op.kind]}`);
  10622. }
  10623. else if (byList !== undefined && op.debugListId !== byList) {
  10624. throw new Error(`AssertionError: node belongs to the wrong list (expected ${byList}, actual ${op.debugListId})`);
  10625. }
  10626. }
  10627. /**
  10628. * Asserts that `op` is not a special `ListEnd` node.
  10629. */
  10630. static assertIsNotEnd(op) {
  10631. if (op.kind === OpKind.ListEnd) {
  10632. throw new Error(`AssertionError: illegal operation on list head or tail`);
  10633. }
  10634. }
  10635. }
  10636. class SlotHandle {
  10637. slot = null;
  10638. }
  10639. /**
  10640. * The set of OpKinds that represent the creation of an element or container
  10641. */
  10642. const elementContainerOpKinds = new Set([
  10643. OpKind.Element,
  10644. OpKind.ElementStart,
  10645. OpKind.Container,
  10646. OpKind.ContainerStart,
  10647. OpKind.Template,
  10648. OpKind.RepeaterCreate,
  10649. ]);
  10650. /**
  10651. * Checks whether the given operation represents the creation of an element or container.
  10652. */
  10653. function isElementOrContainerOp(op) {
  10654. return elementContainerOpKinds.has(op.kind);
  10655. }
  10656. /**
  10657. * Create an `ElementStartOp`.
  10658. */
  10659. function createElementStartOp(tag, xref, namespace, i18nPlaceholder, startSourceSpan, wholeSourceSpan) {
  10660. return {
  10661. kind: OpKind.ElementStart,
  10662. xref,
  10663. tag,
  10664. handle: new SlotHandle(),
  10665. attributes: null,
  10666. localRefs: [],
  10667. nonBindable: false,
  10668. namespace,
  10669. i18nPlaceholder,
  10670. startSourceSpan,
  10671. wholeSourceSpan,
  10672. ...TRAIT_CONSUMES_SLOT,
  10673. ...NEW_OP,
  10674. };
  10675. }
  10676. /**
  10677. * Create a `TemplateOp`.
  10678. */
  10679. function createTemplateOp(xref, templateKind, tag, functionNameSuffix, namespace, i18nPlaceholder, startSourceSpan, wholeSourceSpan) {
  10680. return {
  10681. kind: OpKind.Template,
  10682. xref,
  10683. templateKind,
  10684. attributes: null,
  10685. tag,
  10686. handle: new SlotHandle(),
  10687. functionNameSuffix,
  10688. decls: null,
  10689. vars: null,
  10690. localRefs: [],
  10691. nonBindable: false,
  10692. namespace,
  10693. i18nPlaceholder,
  10694. startSourceSpan,
  10695. wholeSourceSpan,
  10696. ...TRAIT_CONSUMES_SLOT,
  10697. ...NEW_OP,
  10698. };
  10699. }
  10700. function createRepeaterCreateOp(primaryView, emptyView, tag, track, varNames, emptyTag, i18nPlaceholder, emptyI18nPlaceholder, startSourceSpan, wholeSourceSpan) {
  10701. return {
  10702. kind: OpKind.RepeaterCreate,
  10703. attributes: null,
  10704. xref: primaryView,
  10705. handle: new SlotHandle(),
  10706. emptyView,
  10707. track,
  10708. trackByFn: null,
  10709. trackByOps: null,
  10710. tag,
  10711. emptyTag,
  10712. emptyAttributes: null,
  10713. functionNameSuffix: 'For',
  10714. namespace: Namespace.HTML,
  10715. nonBindable: false,
  10716. localRefs: [],
  10717. decls: null,
  10718. vars: null,
  10719. varNames,
  10720. usesComponentInstance: false,
  10721. i18nPlaceholder,
  10722. emptyI18nPlaceholder,
  10723. startSourceSpan,
  10724. wholeSourceSpan,
  10725. ...TRAIT_CONSUMES_SLOT,
  10726. ...NEW_OP,
  10727. ...TRAIT_CONSUMES_VARS,
  10728. numSlotsUsed: emptyView === null ? 2 : 3,
  10729. };
  10730. }
  10731. /**
  10732. * Create an `ElementEndOp`.
  10733. */
  10734. function createElementEndOp(xref, sourceSpan) {
  10735. return {
  10736. kind: OpKind.ElementEnd,
  10737. xref,
  10738. sourceSpan,
  10739. ...NEW_OP,
  10740. };
  10741. }
  10742. function createDisableBindingsOp(xref) {
  10743. return {
  10744. kind: OpKind.DisableBindings,
  10745. xref,
  10746. ...NEW_OP,
  10747. };
  10748. }
  10749. function createEnableBindingsOp(xref) {
  10750. return {
  10751. kind: OpKind.EnableBindings,
  10752. xref,
  10753. ...NEW_OP,
  10754. };
  10755. }
  10756. /**
  10757. * Create a `TextOp`.
  10758. */
  10759. function createTextOp(xref, initialValue, icuPlaceholder, sourceSpan) {
  10760. return {
  10761. kind: OpKind.Text,
  10762. xref,
  10763. handle: new SlotHandle(),
  10764. initialValue,
  10765. icuPlaceholder,
  10766. sourceSpan,
  10767. ...TRAIT_CONSUMES_SLOT,
  10768. ...NEW_OP,
  10769. };
  10770. }
  10771. /**
  10772. * Create a `ListenerOp`. Host bindings reuse all the listener logic.
  10773. */
  10774. function createListenerOp(target, targetSlot, name, tag, handlerOps, animationPhase, eventTarget, hostListener, sourceSpan) {
  10775. const handlerList = new OpList();
  10776. handlerList.push(handlerOps);
  10777. return {
  10778. kind: OpKind.Listener,
  10779. target,
  10780. targetSlot,
  10781. tag,
  10782. hostListener,
  10783. name,
  10784. handlerOps: handlerList,
  10785. handlerFnName: null,
  10786. consumesDollarEvent: false,
  10787. isAnimationListener: animationPhase !== null,
  10788. animationPhase,
  10789. eventTarget,
  10790. sourceSpan,
  10791. ...NEW_OP,
  10792. };
  10793. }
  10794. /**
  10795. * Create a `TwoWayListenerOp`.
  10796. */
  10797. function createTwoWayListenerOp(target, targetSlot, name, tag, handlerOps, sourceSpan) {
  10798. const handlerList = new OpList();
  10799. handlerList.push(handlerOps);
  10800. return {
  10801. kind: OpKind.TwoWayListener,
  10802. target,
  10803. targetSlot,
  10804. tag,
  10805. name,
  10806. handlerOps: handlerList,
  10807. handlerFnName: null,
  10808. sourceSpan,
  10809. ...NEW_OP,
  10810. };
  10811. }
  10812. function createPipeOp(xref, slot, name) {
  10813. return {
  10814. kind: OpKind.Pipe,
  10815. xref,
  10816. handle: slot,
  10817. name,
  10818. ...NEW_OP,
  10819. ...TRAIT_CONSUMES_SLOT,
  10820. };
  10821. }
  10822. function createNamespaceOp(namespace) {
  10823. return {
  10824. kind: OpKind.Namespace,
  10825. active: namespace,
  10826. ...NEW_OP,
  10827. };
  10828. }
  10829. function createProjectionDefOp(def) {
  10830. return {
  10831. kind: OpKind.ProjectionDef,
  10832. def,
  10833. ...NEW_OP,
  10834. };
  10835. }
  10836. function createProjectionOp(xref, selector, i18nPlaceholder, fallbackView, sourceSpan) {
  10837. return {
  10838. kind: OpKind.Projection,
  10839. xref,
  10840. handle: new SlotHandle(),
  10841. selector,
  10842. i18nPlaceholder,
  10843. fallbackView,
  10844. projectionSlotIndex: 0,
  10845. attributes: null,
  10846. localRefs: [],
  10847. sourceSpan,
  10848. ...NEW_OP,
  10849. ...TRAIT_CONSUMES_SLOT,
  10850. numSlotsUsed: fallbackView === null ? 1 : 2,
  10851. };
  10852. }
  10853. /**
  10854. * Create an `ExtractedAttributeOp`.
  10855. */
  10856. function createExtractedAttributeOp(target, bindingKind, namespace, name, expression, i18nContext, i18nMessage, securityContext) {
  10857. return {
  10858. kind: OpKind.ExtractedAttribute,
  10859. target,
  10860. bindingKind,
  10861. namespace,
  10862. name,
  10863. expression,
  10864. i18nContext,
  10865. i18nMessage,
  10866. securityContext,
  10867. trustedValueFn: null,
  10868. ...NEW_OP,
  10869. };
  10870. }
  10871. function createDeferOp(xref, main, mainSlot, ownResolverFn, resolverFn, sourceSpan) {
  10872. return {
  10873. kind: OpKind.Defer,
  10874. xref,
  10875. handle: new SlotHandle(),
  10876. mainView: main,
  10877. mainSlot,
  10878. loadingView: null,
  10879. loadingSlot: null,
  10880. loadingConfig: null,
  10881. loadingMinimumTime: null,
  10882. loadingAfterTime: null,
  10883. placeholderView: null,
  10884. placeholderSlot: null,
  10885. placeholderConfig: null,
  10886. placeholderMinimumTime: null,
  10887. errorView: null,
  10888. errorSlot: null,
  10889. ownResolverFn,
  10890. resolverFn,
  10891. flags: null,
  10892. sourceSpan,
  10893. ...NEW_OP,
  10894. ...TRAIT_CONSUMES_SLOT,
  10895. numSlotsUsed: 2,
  10896. };
  10897. }
  10898. function createDeferOnOp(defer, trigger, modifier, sourceSpan) {
  10899. return {
  10900. kind: OpKind.DeferOn,
  10901. defer,
  10902. trigger,
  10903. modifier,
  10904. sourceSpan,
  10905. ...NEW_OP,
  10906. };
  10907. }
  10908. /**
  10909. * Creates a `DeclareLetOp`.
  10910. */
  10911. function createDeclareLetOp(xref, declaredName, sourceSpan) {
  10912. return {
  10913. kind: OpKind.DeclareLet,
  10914. xref,
  10915. declaredName,
  10916. sourceSpan,
  10917. handle: new SlotHandle(),
  10918. ...TRAIT_CONSUMES_SLOT,
  10919. ...NEW_OP,
  10920. };
  10921. }
  10922. /**
  10923. * Create an `ExtractedMessageOp`.
  10924. */
  10925. function createI18nMessageOp(xref, i18nContext, i18nBlock, message, messagePlaceholder, params, postprocessingParams, needsPostprocessing) {
  10926. return {
  10927. kind: OpKind.I18nMessage,
  10928. xref,
  10929. i18nContext,
  10930. i18nBlock,
  10931. message,
  10932. messagePlaceholder,
  10933. params,
  10934. postprocessingParams,
  10935. needsPostprocessing,
  10936. subMessages: [],
  10937. ...NEW_OP,
  10938. };
  10939. }
  10940. /**
  10941. * Create an `I18nStartOp`.
  10942. */
  10943. function createI18nStartOp(xref, message, root, sourceSpan) {
  10944. return {
  10945. kind: OpKind.I18nStart,
  10946. xref,
  10947. handle: new SlotHandle(),
  10948. root: root ?? xref,
  10949. message,
  10950. messageIndex: null,
  10951. subTemplateIndex: null,
  10952. context: null,
  10953. sourceSpan,
  10954. ...NEW_OP,
  10955. ...TRAIT_CONSUMES_SLOT,
  10956. };
  10957. }
  10958. /**
  10959. * Create an `I18nEndOp`.
  10960. */
  10961. function createI18nEndOp(xref, sourceSpan) {
  10962. return {
  10963. kind: OpKind.I18nEnd,
  10964. xref,
  10965. sourceSpan,
  10966. ...NEW_OP,
  10967. };
  10968. }
  10969. /**
  10970. * Creates an ICU start op.
  10971. */
  10972. function createIcuStartOp(xref, message, messagePlaceholder, sourceSpan) {
  10973. return {
  10974. kind: OpKind.IcuStart,
  10975. xref,
  10976. message,
  10977. messagePlaceholder,
  10978. context: null,
  10979. sourceSpan,
  10980. ...NEW_OP,
  10981. };
  10982. }
  10983. /**
  10984. * Creates an ICU end op.
  10985. */
  10986. function createIcuEndOp(xref) {
  10987. return {
  10988. kind: OpKind.IcuEnd,
  10989. xref,
  10990. ...NEW_OP,
  10991. };
  10992. }
  10993. /**
  10994. * Creates an ICU placeholder op.
  10995. */
  10996. function createIcuPlaceholderOp(xref, name, strings) {
  10997. return {
  10998. kind: OpKind.IcuPlaceholder,
  10999. xref,
  11000. name,
  11001. strings,
  11002. expressionPlaceholders: [],
  11003. ...NEW_OP,
  11004. };
  11005. }
  11006. function createI18nContextOp(contextKind, xref, i18nBlock, message, sourceSpan) {
  11007. if (i18nBlock === null && contextKind !== I18nContextKind.Attr) {
  11008. throw new Error('AssertionError: i18nBlock must be provided for non-attribute contexts.');
  11009. }
  11010. return {
  11011. kind: OpKind.I18nContext,
  11012. contextKind,
  11013. xref,
  11014. i18nBlock,
  11015. message,
  11016. sourceSpan,
  11017. params: new Map(),
  11018. postprocessingParams: new Map(),
  11019. ...NEW_OP,
  11020. };
  11021. }
  11022. function createI18nAttributesOp(xref, handle, target) {
  11023. return {
  11024. kind: OpKind.I18nAttributes,
  11025. xref,
  11026. handle,
  11027. target,
  11028. i18nAttributesConfig: null,
  11029. ...NEW_OP,
  11030. ...TRAIT_CONSUMES_SLOT,
  11031. };
  11032. }
  11033. /** Create a `SourceLocationOp`. */
  11034. function createSourceLocationOp(templatePath, locations) {
  11035. return {
  11036. kind: OpKind.SourceLocation,
  11037. templatePath,
  11038. locations,
  11039. ...NEW_OP,
  11040. };
  11041. }
  11042. function createHostPropertyOp(name, expression, isAnimationTrigger, i18nContext, securityContext, sourceSpan) {
  11043. return {
  11044. kind: OpKind.HostProperty,
  11045. name,
  11046. expression,
  11047. isAnimationTrigger,
  11048. i18nContext,
  11049. securityContext,
  11050. sanitizer: null,
  11051. sourceSpan,
  11052. ...TRAIT_CONSUMES_VARS,
  11053. ...NEW_OP,
  11054. };
  11055. }
  11056. /**
  11057. * When referenced in the template's context parameters, this indicates a reference to the entire
  11058. * context object, rather than a specific parameter.
  11059. */
  11060. const CTX_REF = 'CTX_REF_MARKER';
  11061. var CompilationJobKind;
  11062. (function (CompilationJobKind) {
  11063. CompilationJobKind[CompilationJobKind["Tmpl"] = 0] = "Tmpl";
  11064. CompilationJobKind[CompilationJobKind["Host"] = 1] = "Host";
  11065. CompilationJobKind[CompilationJobKind["Both"] = 2] = "Both";
  11066. })(CompilationJobKind || (CompilationJobKind = {}));
  11067. /**
  11068. * An entire ongoing compilation, which will result in one or more template functions when complete.
  11069. * Contains one or more corresponding compilation units.
  11070. */
  11071. class CompilationJob {
  11072. componentName;
  11073. pool;
  11074. compatibility;
  11075. constructor(componentName, pool, compatibility) {
  11076. this.componentName = componentName;
  11077. this.pool = pool;
  11078. this.compatibility = compatibility;
  11079. }
  11080. kind = CompilationJobKind.Both;
  11081. /**
  11082. * Generate a new unique `ir.XrefId` in this job.
  11083. */
  11084. allocateXrefId() {
  11085. return this.nextXrefId++;
  11086. }
  11087. /**
  11088. * Tracks the next `ir.XrefId` which can be assigned as template structures are ingested.
  11089. */
  11090. nextXrefId = 0;
  11091. }
  11092. /**
  11093. * Compilation-in-progress of a whole component's template, including the main template and any
  11094. * embedded views or host bindings.
  11095. */
  11096. class ComponentCompilationJob extends CompilationJob {
  11097. relativeContextFilePath;
  11098. i18nUseExternalIds;
  11099. deferMeta;
  11100. allDeferrableDepsFn;
  11101. relativeTemplatePath;
  11102. enableDebugLocations;
  11103. constructor(componentName, pool, compatibility, relativeContextFilePath, i18nUseExternalIds, deferMeta, allDeferrableDepsFn, relativeTemplatePath, enableDebugLocations) {
  11104. super(componentName, pool, compatibility);
  11105. this.relativeContextFilePath = relativeContextFilePath;
  11106. this.i18nUseExternalIds = i18nUseExternalIds;
  11107. this.deferMeta = deferMeta;
  11108. this.allDeferrableDepsFn = allDeferrableDepsFn;
  11109. this.relativeTemplatePath = relativeTemplatePath;
  11110. this.enableDebugLocations = enableDebugLocations;
  11111. this.root = new ViewCompilationUnit(this, this.allocateXrefId(), null);
  11112. this.views.set(this.root.xref, this.root);
  11113. }
  11114. kind = CompilationJobKind.Tmpl;
  11115. fnSuffix = 'Template';
  11116. /**
  11117. * The root view, representing the component's template.
  11118. */
  11119. root;
  11120. views = new Map();
  11121. /**
  11122. * Causes ngContentSelectors to be emitted, for content projection slots in the view. Possibly a
  11123. * reference into the constant pool.
  11124. */
  11125. contentSelectors = null;
  11126. /**
  11127. * Add a `ViewCompilation` for a new embedded view to this compilation.
  11128. */
  11129. allocateView(parent) {
  11130. const view = new ViewCompilationUnit(this, this.allocateXrefId(), parent);
  11131. this.views.set(view.xref, view);
  11132. return view;
  11133. }
  11134. get units() {
  11135. return this.views.values();
  11136. }
  11137. /**
  11138. * Add a constant `o.Expression` to the compilation and return its index in the `consts` array.
  11139. */
  11140. addConst(newConst, initializers) {
  11141. for (let idx = 0; idx < this.consts.length; idx++) {
  11142. if (this.consts[idx].isEquivalent(newConst)) {
  11143. return idx;
  11144. }
  11145. }
  11146. const idx = this.consts.length;
  11147. this.consts.push(newConst);
  11148. if (initializers) {
  11149. this.constsInitializers.push(...initializers);
  11150. }
  11151. return idx;
  11152. }
  11153. /**
  11154. * Constant expressions used by operations within this component's compilation.
  11155. *
  11156. * This will eventually become the `consts` array in the component definition.
  11157. */
  11158. consts = [];
  11159. /**
  11160. * Initialization statements needed to set up the consts.
  11161. */
  11162. constsInitializers = [];
  11163. }
  11164. /**
  11165. * A compilation unit is compiled into a template function. Some example units are views and host
  11166. * bindings.
  11167. */
  11168. class CompilationUnit {
  11169. xref;
  11170. constructor(xref) {
  11171. this.xref = xref;
  11172. }
  11173. /**
  11174. * List of creation operations for this view.
  11175. *
  11176. * Creation operations may internally contain other operations, including update operations.
  11177. */
  11178. create = new OpList();
  11179. /**
  11180. * List of update operations for this view.
  11181. */
  11182. update = new OpList();
  11183. /**
  11184. * Name of the function which will be generated for this unit.
  11185. *
  11186. * May be `null` if not yet determined.
  11187. */
  11188. fnName = null;
  11189. /**
  11190. * Number of variable slots used within this view, or `null` if variables have not yet been
  11191. * counted.
  11192. */
  11193. vars = null;
  11194. /**
  11195. * Iterate over all `ir.Op`s within this view.
  11196. *
  11197. * Some operations may have child operations, which this iterator will visit.
  11198. */
  11199. *ops() {
  11200. for (const op of this.create) {
  11201. yield op;
  11202. if (op.kind === OpKind.Listener || op.kind === OpKind.TwoWayListener) {
  11203. for (const listenerOp of op.handlerOps) {
  11204. yield listenerOp;
  11205. }
  11206. }
  11207. else if (op.kind === OpKind.RepeaterCreate && op.trackByOps !== null) {
  11208. for (const trackOp of op.trackByOps) {
  11209. yield trackOp;
  11210. }
  11211. }
  11212. }
  11213. for (const op of this.update) {
  11214. yield op;
  11215. }
  11216. }
  11217. }
  11218. /**
  11219. * Compilation-in-progress of an individual view within a template.
  11220. */
  11221. class ViewCompilationUnit extends CompilationUnit {
  11222. job;
  11223. parent;
  11224. constructor(job, xref, parent) {
  11225. super(xref);
  11226. this.job = job;
  11227. this.parent = parent;
  11228. }
  11229. /**
  11230. * Map of declared variables available within this view to the property on the context object
  11231. * which they alias.
  11232. */
  11233. contextVariables = new Map();
  11234. /**
  11235. * Set of aliases available within this view. An alias is a variable whose provided expression is
  11236. * inlined at every location it is used. It may also depend on context variables, by name.
  11237. */
  11238. aliases = new Set();
  11239. /**
  11240. * Number of declaration slots used within this view, or `null` if slots have not yet been
  11241. * allocated.
  11242. */
  11243. decls = null;
  11244. }
  11245. /**
  11246. * Compilation-in-progress of a host binding, which contains a single unit for that host binding.
  11247. */
  11248. class HostBindingCompilationJob extends CompilationJob {
  11249. constructor(componentName, pool, compatibility) {
  11250. super(componentName, pool, compatibility);
  11251. this.root = new HostBindingCompilationUnit(this);
  11252. }
  11253. kind = CompilationJobKind.Host;
  11254. fnSuffix = 'HostBindings';
  11255. root;
  11256. get units() {
  11257. return [this.root];
  11258. }
  11259. }
  11260. class HostBindingCompilationUnit extends CompilationUnit {
  11261. job;
  11262. constructor(job) {
  11263. super(0);
  11264. this.job = job;
  11265. }
  11266. /**
  11267. * Much like an element can have attributes, so can a host binding function.
  11268. */
  11269. attributes = null;
  11270. }
  11271. /**
  11272. * Find any function calls to `$any`, excluding `this.$any`, and delete them, since they have no
  11273. * runtime effects.
  11274. */
  11275. function deleteAnyCasts(job) {
  11276. for (const unit of job.units) {
  11277. for (const op of unit.ops()) {
  11278. transformExpressionsInOp(op, removeAnys, VisitorContextFlag.None);
  11279. }
  11280. }
  11281. }
  11282. function removeAnys(e) {
  11283. if (e instanceof InvokeFunctionExpr &&
  11284. e.fn instanceof LexicalReadExpr &&
  11285. e.fn.name === '$any') {
  11286. if (e.args.length !== 1) {
  11287. throw new Error('The $any builtin function expects exactly one argument.');
  11288. }
  11289. return e.args[0];
  11290. }
  11291. return e;
  11292. }
  11293. /**
  11294. * Adds apply operations after i18n expressions.
  11295. */
  11296. function applyI18nExpressions(job) {
  11297. const i18nContexts = new Map();
  11298. for (const unit of job.units) {
  11299. for (const op of unit.create) {
  11300. if (op.kind === OpKind.I18nContext) {
  11301. i18nContexts.set(op.xref, op);
  11302. }
  11303. }
  11304. }
  11305. for (const unit of job.units) {
  11306. for (const op of unit.update) {
  11307. // Only add apply after expressions that are not followed by more expressions.
  11308. if (op.kind === OpKind.I18nExpression && needsApplication(i18nContexts, op)) {
  11309. // TODO: what should be the source span for the apply op?
  11310. OpList.insertAfter(createI18nApplyOp(op.i18nOwner, op.handle, null), op);
  11311. }
  11312. }
  11313. }
  11314. }
  11315. /**
  11316. * Checks whether the given expression op needs to be followed with an apply op.
  11317. */
  11318. function needsApplication(i18nContexts, op) {
  11319. // If the next op is not another expression, we need to apply.
  11320. if (op.next?.kind !== OpKind.I18nExpression) {
  11321. return true;
  11322. }
  11323. const context = i18nContexts.get(op.context);
  11324. const nextContext = i18nContexts.get(op.next.context);
  11325. if (context === undefined) {
  11326. throw new Error("AssertionError: expected an I18nContextOp to exist for the I18nExpressionOp's context");
  11327. }
  11328. if (nextContext === undefined) {
  11329. throw new Error("AssertionError: expected an I18nContextOp to exist for the next I18nExpressionOp's context");
  11330. }
  11331. // If the next op is an expression targeting a different i18n block (or different element, in the
  11332. // case of i18n attributes), we need to apply.
  11333. // First, handle the case of i18n blocks.
  11334. if (context.i18nBlock !== null) {
  11335. // This is a block context. Compare the blocks.
  11336. if (context.i18nBlock !== nextContext.i18nBlock) {
  11337. return true;
  11338. }
  11339. return false;
  11340. }
  11341. // Second, handle the case of i18n attributes.
  11342. if (op.i18nOwner !== op.next.i18nOwner) {
  11343. return true;
  11344. }
  11345. return false;
  11346. }
  11347. /**
  11348. * Updates i18n expression ops to target the last slot in their owning i18n block, and moves them
  11349. * after the last update instruction that depends on that slot.
  11350. */
  11351. function assignI18nSlotDependencies(job) {
  11352. for (const unit of job.units) {
  11353. // The first update op.
  11354. let updateOp = unit.update.head;
  11355. // I18n expressions currently being moved during the iteration.
  11356. let i18nExpressionsInProgress = [];
  11357. // Non-null while we are iterating through an i18nStart/i18nEnd pair
  11358. let state = null;
  11359. for (const createOp of unit.create) {
  11360. if (createOp.kind === OpKind.I18nStart) {
  11361. state = {
  11362. blockXref: createOp.xref,
  11363. lastSlotConsumer: createOp.xref,
  11364. };
  11365. }
  11366. else if (createOp.kind === OpKind.I18nEnd) {
  11367. for (const op of i18nExpressionsInProgress) {
  11368. op.target = state.lastSlotConsumer;
  11369. OpList.insertBefore(op, updateOp);
  11370. }
  11371. i18nExpressionsInProgress.length = 0;
  11372. state = null;
  11373. }
  11374. if (hasConsumesSlotTrait(createOp)) {
  11375. if (state !== null) {
  11376. state.lastSlotConsumer = createOp.xref;
  11377. }
  11378. while (true) {
  11379. if (updateOp.next === null) {
  11380. break;
  11381. }
  11382. if (state !== null &&
  11383. updateOp.kind === OpKind.I18nExpression &&
  11384. updateOp.usage === I18nExpressionFor.I18nText &&
  11385. updateOp.i18nOwner === state.blockXref) {
  11386. const opToRemove = updateOp;
  11387. updateOp = updateOp.next;
  11388. OpList.remove(opToRemove);
  11389. i18nExpressionsInProgress.push(opToRemove);
  11390. continue;
  11391. }
  11392. if (hasDependsOnSlotContextTrait(updateOp) && updateOp.target !== createOp.xref) {
  11393. break;
  11394. }
  11395. updateOp = updateOp.next;
  11396. }
  11397. }
  11398. }
  11399. }
  11400. }
  11401. /**
  11402. * Gets a map of all elements in the given view by their xref id.
  11403. */
  11404. function createOpXrefMap(unit) {
  11405. const map = new Map();
  11406. for (const op of unit.create) {
  11407. if (!hasConsumesSlotTrait(op)) {
  11408. continue;
  11409. }
  11410. map.set(op.xref, op);
  11411. // TODO(dylhunn): `@for` loops with `@empty` blocks need to be special-cased here,
  11412. // because the slot consumer trait currently only supports one slot per consumer and we
  11413. // need two. This should be revisited when making the refactors mentioned in:
  11414. // https://github.com/angular/angular/pull/53620#discussion_r1430918822
  11415. if (op.kind === OpKind.RepeaterCreate && op.emptyView !== null) {
  11416. map.set(op.emptyView, op);
  11417. }
  11418. }
  11419. return map;
  11420. }
  11421. /**
  11422. * Find all extractable attribute and binding ops, and create ExtractedAttributeOps for them.
  11423. * In cases where no instruction needs to be generated for the attribute or binding, it is removed.
  11424. */
  11425. function extractAttributes(job) {
  11426. for (const unit of job.units) {
  11427. const elements = createOpXrefMap(unit);
  11428. for (const op of unit.ops()) {
  11429. switch (op.kind) {
  11430. case OpKind.Attribute:
  11431. extractAttributeOp(unit, op, elements);
  11432. break;
  11433. case OpKind.Property:
  11434. if (!op.isAnimationTrigger) {
  11435. let bindingKind;
  11436. if (op.i18nMessage !== null && op.templateKind === null) {
  11437. // If the binding has an i18n context, it is an i18n attribute, and should have that
  11438. // kind in the consts array.
  11439. bindingKind = BindingKind.I18n;
  11440. }
  11441. else if (op.isStructuralTemplateAttribute) {
  11442. bindingKind = BindingKind.Template;
  11443. }
  11444. else {
  11445. bindingKind = BindingKind.Property;
  11446. }
  11447. OpList.insertBefore(
  11448. // Deliberately null i18nMessage value
  11449. createExtractedAttributeOp(op.target, bindingKind, null, op.name,
  11450. /* expression */ null,
  11451. /* i18nContext */ null,
  11452. /* i18nMessage */ null, op.securityContext), lookupElement$2(elements, op.target));
  11453. }
  11454. break;
  11455. case OpKind.TwoWayProperty:
  11456. OpList.insertBefore(createExtractedAttributeOp(op.target, BindingKind.TwoWayProperty, null, op.name,
  11457. /* expression */ null,
  11458. /* i18nContext */ null,
  11459. /* i18nMessage */ null, op.securityContext), lookupElement$2(elements, op.target));
  11460. break;
  11461. case OpKind.StyleProp:
  11462. case OpKind.ClassProp:
  11463. // TODO: Can style or class bindings be i18n attributes?
  11464. // The old compiler treated empty style bindings as regular bindings for the purpose of
  11465. // directive matching. That behavior is incorrect, but we emulate it in compatibility
  11466. // mode.
  11467. if (unit.job.compatibility === CompatibilityMode.TemplateDefinitionBuilder &&
  11468. op.expression instanceof EmptyExpr) {
  11469. OpList.insertBefore(createExtractedAttributeOp(op.target, BindingKind.Property, null, op.name,
  11470. /* expression */ null,
  11471. /* i18nContext */ null,
  11472. /* i18nMessage */ null, SecurityContext.STYLE), lookupElement$2(elements, op.target));
  11473. }
  11474. break;
  11475. case OpKind.Listener:
  11476. if (!op.isAnimationListener) {
  11477. const extractedAttributeOp = createExtractedAttributeOp(op.target, BindingKind.Property, null, op.name,
  11478. /* expression */ null,
  11479. /* i18nContext */ null,
  11480. /* i18nMessage */ null, SecurityContext.NONE);
  11481. if (job.kind === CompilationJobKind.Host) {
  11482. if (job.compatibility) {
  11483. // TemplateDefinitionBuilder does not extract listener bindings to the const array
  11484. // (which is honestly pretty inconsistent).
  11485. break;
  11486. }
  11487. // This attribute will apply to the enclosing host binding compilation unit, so order
  11488. // doesn't matter.
  11489. unit.create.push(extractedAttributeOp);
  11490. }
  11491. else {
  11492. OpList.insertBefore(extractedAttributeOp, lookupElement$2(elements, op.target));
  11493. }
  11494. }
  11495. break;
  11496. case OpKind.TwoWayListener:
  11497. // Two-way listeners aren't supported in host bindings.
  11498. if (job.kind !== CompilationJobKind.Host) {
  11499. const extractedAttributeOp = createExtractedAttributeOp(op.target, BindingKind.Property, null, op.name,
  11500. /* expression */ null,
  11501. /* i18nContext */ null,
  11502. /* i18nMessage */ null, SecurityContext.NONE);
  11503. OpList.insertBefore(extractedAttributeOp, lookupElement$2(elements, op.target));
  11504. }
  11505. break;
  11506. }
  11507. }
  11508. }
  11509. }
  11510. /**
  11511. * Looks up an element in the given map by xref ID.
  11512. */
  11513. function lookupElement$2(elements, xref) {
  11514. const el = elements.get(xref);
  11515. if (el === undefined) {
  11516. throw new Error('All attributes should have an element-like target.');
  11517. }
  11518. return el;
  11519. }
  11520. /**
  11521. * Extracts an attribute binding.
  11522. */
  11523. function extractAttributeOp(unit, op, elements) {
  11524. if (op.expression instanceof Interpolation) {
  11525. return;
  11526. }
  11527. let extractable = op.isTextAttribute || op.expression.isConstant();
  11528. if (unit.job.compatibility === CompatibilityMode.TemplateDefinitionBuilder) {
  11529. // TemplateDefinitionBuilder only extracts text attributes. It does not extract attriibute
  11530. // bindings, even if they are constants.
  11531. extractable &&= op.isTextAttribute;
  11532. }
  11533. if (extractable) {
  11534. const extractedAttributeOp = createExtractedAttributeOp(op.target, op.isStructuralTemplateAttribute ? BindingKind.Template : BindingKind.Attribute, op.namespace, op.name, op.expression, op.i18nContext, op.i18nMessage, op.securityContext);
  11535. if (unit.job.kind === CompilationJobKind.Host) {
  11536. // This attribute will apply to the enclosing host binding compilation unit, so order doesn't
  11537. // matter.
  11538. unit.create.push(extractedAttributeOp);
  11539. }
  11540. else {
  11541. const ownerOp = lookupElement$2(elements, op.target);
  11542. OpList.insertBefore(extractedAttributeOp, ownerOp);
  11543. }
  11544. OpList.remove(op);
  11545. }
  11546. }
  11547. /**
  11548. * Looks up an element in the given map by xref ID.
  11549. */
  11550. function lookupElement$1(elements, xref) {
  11551. const el = elements.get(xref);
  11552. if (el === undefined) {
  11553. throw new Error('All attributes should have an element-like target.');
  11554. }
  11555. return el;
  11556. }
  11557. function specializeBindings(job) {
  11558. const elements = new Map();
  11559. for (const unit of job.units) {
  11560. for (const op of unit.create) {
  11561. if (!isElementOrContainerOp(op)) {
  11562. continue;
  11563. }
  11564. elements.set(op.xref, op);
  11565. }
  11566. }
  11567. for (const unit of job.units) {
  11568. for (const op of unit.ops()) {
  11569. if (op.kind !== OpKind.Binding) {
  11570. continue;
  11571. }
  11572. switch (op.bindingKind) {
  11573. case BindingKind.Attribute:
  11574. if (op.name === 'ngNonBindable') {
  11575. OpList.remove(op);
  11576. const target = lookupElement$1(elements, op.target);
  11577. target.nonBindable = true;
  11578. }
  11579. else {
  11580. const [namespace, name] = splitNsName(op.name);
  11581. OpList.replace(op, createAttributeOp(op.target, namespace, name, op.expression, op.securityContext, op.isTextAttribute, op.isStructuralTemplateAttribute, op.templateKind, op.i18nMessage, op.sourceSpan));
  11582. }
  11583. break;
  11584. case BindingKind.Property:
  11585. case BindingKind.Animation:
  11586. if (job.kind === CompilationJobKind.Host) {
  11587. OpList.replace(op, createHostPropertyOp(op.name, op.expression, op.bindingKind === BindingKind.Animation, op.i18nContext, op.securityContext, op.sourceSpan));
  11588. }
  11589. else {
  11590. OpList.replace(op, createPropertyOp(op.target, op.name, op.expression, op.bindingKind === BindingKind.Animation, op.securityContext, op.isStructuralTemplateAttribute, op.templateKind, op.i18nContext, op.i18nMessage, op.sourceSpan));
  11591. }
  11592. break;
  11593. case BindingKind.TwoWayProperty:
  11594. if (!(op.expression instanceof Expression)) {
  11595. // We shouldn't be able to hit this code path since interpolations in two-way bindings
  11596. // result in a parser error. We assert here so that downstream we can assume that
  11597. // the value is always an expression.
  11598. throw new Error(`Expected value of two-way property binding "${op.name}" to be an expression`);
  11599. }
  11600. OpList.replace(op, createTwoWayPropertyOp(op.target, op.name, op.expression, op.securityContext, op.isStructuralTemplateAttribute, op.templateKind, op.i18nContext, op.i18nMessage, op.sourceSpan));
  11601. break;
  11602. case BindingKind.I18n:
  11603. case BindingKind.ClassName:
  11604. case BindingKind.StyleProperty:
  11605. throw new Error(`Unhandled binding of kind ${BindingKind[op.bindingKind]}`);
  11606. }
  11607. }
  11608. }
  11609. }
  11610. const CHAINABLE = new Set([
  11611. Identifiers.attribute,
  11612. Identifiers.classProp,
  11613. Identifiers.element,
  11614. Identifiers.elementContainer,
  11615. Identifiers.elementContainerEnd,
  11616. Identifiers.elementContainerStart,
  11617. Identifiers.elementEnd,
  11618. Identifiers.elementStart,
  11619. Identifiers.hostProperty,
  11620. Identifiers.i18nExp,
  11621. Identifiers.listener,
  11622. Identifiers.listener,
  11623. Identifiers.property,
  11624. Identifiers.styleProp,
  11625. Identifiers.stylePropInterpolate1,
  11626. Identifiers.stylePropInterpolate2,
  11627. Identifiers.stylePropInterpolate3,
  11628. Identifiers.stylePropInterpolate4,
  11629. Identifiers.stylePropInterpolate5,
  11630. Identifiers.stylePropInterpolate6,
  11631. Identifiers.stylePropInterpolate7,
  11632. Identifiers.stylePropInterpolate8,
  11633. Identifiers.stylePropInterpolateV,
  11634. Identifiers.syntheticHostListener,
  11635. Identifiers.syntheticHostProperty,
  11636. Identifiers.templateCreate,
  11637. Identifiers.twoWayProperty,
  11638. Identifiers.twoWayListener,
  11639. Identifiers.declareLet,
  11640. ]);
  11641. /**
  11642. * Chaining results in repeated call expressions, causing a deep AST of receiver expressions. To prevent running out of
  11643. * stack depth the maximum number of chained instructions is limited to this threshold, which has been selected
  11644. * arbitrarily.
  11645. */
  11646. const MAX_CHAIN_LENGTH = 256;
  11647. /**
  11648. * Post-process a reified view compilation and convert sequential calls to chainable instructions
  11649. * into chain calls.
  11650. *
  11651. * For example, two `elementStart` operations in sequence:
  11652. *
  11653. * ```ts
  11654. * elementStart(0, 'div');
  11655. * elementStart(1, 'span');
  11656. * ```
  11657. *
  11658. * Can be called as a chain instead:
  11659. *
  11660. * ```ts
  11661. * elementStart(0, 'div')(1, 'span');
  11662. * ```
  11663. */
  11664. function chain(job) {
  11665. for (const unit of job.units) {
  11666. chainOperationsInList(unit.create);
  11667. chainOperationsInList(unit.update);
  11668. }
  11669. }
  11670. function chainOperationsInList(opList) {
  11671. let chain = null;
  11672. for (const op of opList) {
  11673. if (op.kind !== OpKind.Statement || !(op.statement instanceof ExpressionStatement)) {
  11674. // This type of statement isn't chainable.
  11675. chain = null;
  11676. continue;
  11677. }
  11678. if (!(op.statement.expr instanceof InvokeFunctionExpr) ||
  11679. !(op.statement.expr.fn instanceof ExternalExpr)) {
  11680. // This is a statement, but not an instruction-type call, so not chainable.
  11681. chain = null;
  11682. continue;
  11683. }
  11684. const instruction = op.statement.expr.fn.value;
  11685. if (!CHAINABLE.has(instruction)) {
  11686. // This instruction isn't chainable.
  11687. chain = null;
  11688. continue;
  11689. }
  11690. // This instruction can be chained. It can either be added on to the previous chain (if
  11691. // compatible) or it can be the start of a new chain.
  11692. if (chain !== null && chain.instruction === instruction && chain.length < MAX_CHAIN_LENGTH) {
  11693. // This instruction can be added onto the previous chain.
  11694. const expression = chain.expression.callFn(op.statement.expr.args, op.statement.expr.sourceSpan, op.statement.expr.pure);
  11695. chain.expression = expression;
  11696. chain.op.statement = expression.toStmt();
  11697. chain.length++;
  11698. OpList.remove(op);
  11699. }
  11700. else {
  11701. // Leave this instruction alone for now, but consider it the start of a new chain.
  11702. chain = {
  11703. op,
  11704. instruction,
  11705. expression: op.statement.expr,
  11706. length: 1,
  11707. };
  11708. }
  11709. }
  11710. }
  11711. /**
  11712. * Attribute interpolations of the form `[attr.foo]="{{foo}}""` should be "collapsed" into a plain
  11713. * attribute instruction, instead of an `attributeInterpolate` instruction.
  11714. *
  11715. * (We cannot do this for singleton property interpolations, because `propertyInterpolate`
  11716. * stringifies its expression.)
  11717. *
  11718. * The reification step is also capable of performing this transformation, but doing it early in the
  11719. * pipeline allows other phases to accurately know what instruction will be emitted.
  11720. */
  11721. function collapseSingletonInterpolations(job) {
  11722. for (const unit of job.units) {
  11723. for (const op of unit.update) {
  11724. const eligibleOpKind = op.kind === OpKind.Attribute;
  11725. if (eligibleOpKind &&
  11726. op.expression instanceof Interpolation &&
  11727. op.expression.strings.length === 2 &&
  11728. op.expression.strings.every((s) => s === '')) {
  11729. op.expression = op.expression.expressions[0];
  11730. }
  11731. }
  11732. }
  11733. }
  11734. /**
  11735. * Collapse the various conditions of conditional ops (if, switch) into a single test expression.
  11736. */
  11737. function generateConditionalExpressions(job) {
  11738. for (const unit of job.units) {
  11739. for (const op of unit.ops()) {
  11740. if (op.kind !== OpKind.Conditional) {
  11741. continue;
  11742. }
  11743. let test;
  11744. // Any case with a `null` condition is `default`. If one exists, default to it instead.
  11745. const defaultCase = op.conditions.findIndex((cond) => cond.expr === null);
  11746. if (defaultCase >= 0) {
  11747. const slot = op.conditions.splice(defaultCase, 1)[0].targetSlot;
  11748. test = new SlotLiteralExpr(slot);
  11749. }
  11750. else {
  11751. // By default, a switch evaluates to `-1`, causing no template to be displayed.
  11752. test = literal(-1);
  11753. }
  11754. // Switch expressions assign their main test to a temporary, to avoid re-executing it.
  11755. let tmp = op.test == null ? null : new AssignTemporaryExpr(op.test, job.allocateXrefId());
  11756. // For each remaining condition, test whether the temporary satifies the check. (If no temp is
  11757. // present, just check each expression directly.)
  11758. for (let i = op.conditions.length - 1; i >= 0; i--) {
  11759. let conditionalCase = op.conditions[i];
  11760. if (conditionalCase.expr === null) {
  11761. continue;
  11762. }
  11763. if (tmp !== null) {
  11764. const useTmp = i === 0 ? tmp : new ReadTemporaryExpr(tmp.xref);
  11765. conditionalCase.expr = new BinaryOperatorExpr(BinaryOperator.Identical, useTmp, conditionalCase.expr);
  11766. }
  11767. else if (conditionalCase.alias !== null) {
  11768. const caseExpressionTemporaryXref = job.allocateXrefId();
  11769. conditionalCase.expr = new AssignTemporaryExpr(conditionalCase.expr, caseExpressionTemporaryXref);
  11770. op.contextValue = new ReadTemporaryExpr(caseExpressionTemporaryXref);
  11771. }
  11772. test = new ConditionalExpr(conditionalCase.expr, new SlotLiteralExpr(conditionalCase.targetSlot), test);
  11773. }
  11774. // Save the resulting aggregate Joost-expression.
  11775. op.processed = test;
  11776. // Clear the original conditions array, since we no longer need it, and don't want it to
  11777. // affect subsequent phases (e.g. pipe creation).
  11778. op.conditions = [];
  11779. }
  11780. }
  11781. }
  11782. const BINARY_OPERATORS = new Map([
  11783. ['&&', BinaryOperator.And],
  11784. ['>', BinaryOperator.Bigger],
  11785. ['>=', BinaryOperator.BiggerEquals],
  11786. ['|', BinaryOperator.BitwiseOr],
  11787. ['&', BinaryOperator.BitwiseAnd],
  11788. ['/', BinaryOperator.Divide],
  11789. ['==', BinaryOperator.Equals],
  11790. ['===', BinaryOperator.Identical],
  11791. ['<', BinaryOperator.Lower],
  11792. ['<=', BinaryOperator.LowerEquals],
  11793. ['-', BinaryOperator.Minus],
  11794. ['%', BinaryOperator.Modulo],
  11795. ['*', BinaryOperator.Multiply],
  11796. ['!=', BinaryOperator.NotEquals],
  11797. ['!==', BinaryOperator.NotIdentical],
  11798. ['??', BinaryOperator.NullishCoalesce],
  11799. ['||', BinaryOperator.Or],
  11800. ['+', BinaryOperator.Plus],
  11801. ]);
  11802. function namespaceForKey(namespacePrefixKey) {
  11803. const NAMESPACES = new Map([
  11804. ['svg', Namespace.SVG],
  11805. ['math', Namespace.Math],
  11806. ]);
  11807. if (namespacePrefixKey === null) {
  11808. return Namespace.HTML;
  11809. }
  11810. return NAMESPACES.get(namespacePrefixKey) ?? Namespace.HTML;
  11811. }
  11812. function keyForNamespace(namespace) {
  11813. const NAMESPACES = new Map([
  11814. ['svg', Namespace.SVG],
  11815. ['math', Namespace.Math],
  11816. ]);
  11817. for (const [k, n] of NAMESPACES.entries()) {
  11818. if (n === namespace) {
  11819. return k;
  11820. }
  11821. }
  11822. return null; // No namespace prefix for HTML
  11823. }
  11824. function prefixWithNamespace(strippedTag, namespace) {
  11825. if (namespace === Namespace.HTML) {
  11826. return strippedTag;
  11827. }
  11828. return `:${keyForNamespace(namespace)}:${strippedTag}`;
  11829. }
  11830. function literalOrArrayLiteral(value) {
  11831. if (Array.isArray(value)) {
  11832. return literalArr(value.map(literalOrArrayLiteral));
  11833. }
  11834. return literal(value);
  11835. }
  11836. /**
  11837. * Converts the semantic attributes of element-like operations (elements, templates) into constant
  11838. * array expressions, and lifts them into the overall component `consts`.
  11839. */
  11840. function collectElementConsts(job) {
  11841. // Collect all extracted attributes.
  11842. const allElementAttributes = new Map();
  11843. for (const unit of job.units) {
  11844. for (const op of unit.create) {
  11845. if (op.kind === OpKind.ExtractedAttribute) {
  11846. const attributes = allElementAttributes.get(op.target) || new ElementAttributes(job.compatibility);
  11847. allElementAttributes.set(op.target, attributes);
  11848. attributes.add(op.bindingKind, op.name, op.expression, op.namespace, op.trustedValueFn);
  11849. OpList.remove(op);
  11850. }
  11851. }
  11852. }
  11853. // Serialize the extracted attributes into the const array.
  11854. if (job instanceof ComponentCompilationJob) {
  11855. for (const unit of job.units) {
  11856. for (const op of unit.create) {
  11857. // TODO: Simplify and combine these cases.
  11858. if (op.kind == OpKind.Projection) {
  11859. const attributes = allElementAttributes.get(op.xref);
  11860. if (attributes !== undefined) {
  11861. const attrArray = serializeAttributes(attributes);
  11862. if (attrArray.entries.length > 0) {
  11863. op.attributes = attrArray;
  11864. }
  11865. }
  11866. }
  11867. else if (isElementOrContainerOp(op)) {
  11868. op.attributes = getConstIndex(job, allElementAttributes, op.xref);
  11869. // TODO(dylhunn): `@for` loops with `@empty` blocks need to be special-cased here,
  11870. // because the slot consumer trait currently only supports one slot per consumer and we
  11871. // need two. This should be revisited when making the refactors mentioned in:
  11872. // https://github.com/angular/angular/pull/53620#discussion_r1430918822
  11873. if (op.kind === OpKind.RepeaterCreate && op.emptyView !== null) {
  11874. op.emptyAttributes = getConstIndex(job, allElementAttributes, op.emptyView);
  11875. }
  11876. }
  11877. }
  11878. }
  11879. }
  11880. else if (job instanceof HostBindingCompilationJob) {
  11881. // TODO: If the host binding case further diverges, we may want to split it into its own
  11882. // phase.
  11883. for (const [xref, attributes] of allElementAttributes.entries()) {
  11884. if (xref !== job.root.xref) {
  11885. throw new Error(`An attribute would be const collected into the host binding's template function, but is not associated with the root xref.`);
  11886. }
  11887. const attrArray = serializeAttributes(attributes);
  11888. if (attrArray.entries.length > 0) {
  11889. job.root.attributes = attrArray;
  11890. }
  11891. }
  11892. }
  11893. }
  11894. function getConstIndex(job, allElementAttributes, xref) {
  11895. const attributes = allElementAttributes.get(xref);
  11896. if (attributes !== undefined) {
  11897. const attrArray = serializeAttributes(attributes);
  11898. if (attrArray.entries.length > 0) {
  11899. return job.addConst(attrArray);
  11900. }
  11901. }
  11902. return null;
  11903. }
  11904. /**
  11905. * Shared instance of an empty array to avoid unnecessary array allocations.
  11906. */
  11907. const FLYWEIGHT_ARRAY = Object.freeze([]);
  11908. /**
  11909. * Container for all of the various kinds of attributes which are applied on an element.
  11910. */
  11911. class ElementAttributes {
  11912. compatibility;
  11913. known = new Map();
  11914. byKind = new Map();
  11915. propertyBindings = null;
  11916. projectAs = null;
  11917. get attributes() {
  11918. return this.byKind.get(BindingKind.Attribute) ?? FLYWEIGHT_ARRAY;
  11919. }
  11920. get classes() {
  11921. return this.byKind.get(BindingKind.ClassName) ?? FLYWEIGHT_ARRAY;
  11922. }
  11923. get styles() {
  11924. return this.byKind.get(BindingKind.StyleProperty) ?? FLYWEIGHT_ARRAY;
  11925. }
  11926. get bindings() {
  11927. return this.propertyBindings ?? FLYWEIGHT_ARRAY;
  11928. }
  11929. get template() {
  11930. return this.byKind.get(BindingKind.Template) ?? FLYWEIGHT_ARRAY;
  11931. }
  11932. get i18n() {
  11933. return this.byKind.get(BindingKind.I18n) ?? FLYWEIGHT_ARRAY;
  11934. }
  11935. constructor(compatibility) {
  11936. this.compatibility = compatibility;
  11937. }
  11938. isKnown(kind, name) {
  11939. const nameToValue = this.known.get(kind) ?? new Set();
  11940. this.known.set(kind, nameToValue);
  11941. if (nameToValue.has(name)) {
  11942. return true;
  11943. }
  11944. nameToValue.add(name);
  11945. return false;
  11946. }
  11947. add(kind, name, value, namespace, trustedValueFn) {
  11948. // TemplateDefinitionBuilder puts duplicate attribute, class, and style values into the consts
  11949. // array. This seems inefficient, we can probably keep just the first one or the last value
  11950. // (whichever actually gets applied when multiple values are listed for the same attribute).
  11951. const allowDuplicates = this.compatibility === CompatibilityMode.TemplateDefinitionBuilder &&
  11952. (kind === BindingKind.Attribute ||
  11953. kind === BindingKind.ClassName ||
  11954. kind === BindingKind.StyleProperty);
  11955. if (!allowDuplicates && this.isKnown(kind, name)) {
  11956. return;
  11957. }
  11958. // TODO: Can this be its own phase
  11959. if (name === 'ngProjectAs') {
  11960. if (value === null ||
  11961. !(value instanceof LiteralExpr) ||
  11962. value.value == null ||
  11963. typeof value.value?.toString() !== 'string') {
  11964. throw Error('ngProjectAs must have a string literal value');
  11965. }
  11966. this.projectAs = value.value.toString();
  11967. // TODO: TemplateDefinitionBuilder allows `ngProjectAs` to also be assigned as a literal
  11968. // attribute. Is this sane?
  11969. }
  11970. const array = this.arrayFor(kind);
  11971. array.push(...getAttributeNameLiterals(namespace, name));
  11972. if (kind === BindingKind.Attribute || kind === BindingKind.StyleProperty) {
  11973. if (value === null) {
  11974. throw Error('Attribute, i18n attribute, & style element attributes must have a value');
  11975. }
  11976. if (trustedValueFn !== null) {
  11977. if (!isStringLiteral(value)) {
  11978. throw Error('AssertionError: extracted attribute value should be string literal');
  11979. }
  11980. array.push(taggedTemplate(trustedValueFn, new TemplateLiteralExpr([new TemplateLiteralElementExpr(value.value)], []), undefined, value.sourceSpan));
  11981. }
  11982. else {
  11983. array.push(value);
  11984. }
  11985. }
  11986. }
  11987. arrayFor(kind) {
  11988. if (kind === BindingKind.Property || kind === BindingKind.TwoWayProperty) {
  11989. this.propertyBindings ??= [];
  11990. return this.propertyBindings;
  11991. }
  11992. else {
  11993. if (!this.byKind.has(kind)) {
  11994. this.byKind.set(kind, []);
  11995. }
  11996. return this.byKind.get(kind);
  11997. }
  11998. }
  11999. }
  12000. /**
  12001. * Gets an array of literal expressions representing the attribute's namespaced name.
  12002. */
  12003. function getAttributeNameLiterals(namespace, name) {
  12004. const nameLiteral = literal(name);
  12005. if (namespace) {
  12006. return [literal(0 /* core.AttributeMarker.NamespaceURI */), literal(namespace), nameLiteral];
  12007. }
  12008. return [nameLiteral];
  12009. }
  12010. /**
  12011. * Serializes an ElementAttributes object into an array expression.
  12012. */
  12013. function serializeAttributes({ attributes, bindings, classes, i18n, projectAs, styles, template, }) {
  12014. const attrArray = [...attributes];
  12015. if (projectAs !== null) {
  12016. // Parse the attribute value into a CssSelectorList. Note that we only take the
  12017. // first selector, because we don't support multiple selectors in ngProjectAs.
  12018. const parsedR3Selector = parseSelectorToR3Selector(projectAs)[0];
  12019. attrArray.push(literal(5 /* core.AttributeMarker.ProjectAs */), literalOrArrayLiteral(parsedR3Selector));
  12020. }
  12021. if (classes.length > 0) {
  12022. attrArray.push(literal(1 /* core.AttributeMarker.Classes */), ...classes);
  12023. }
  12024. if (styles.length > 0) {
  12025. attrArray.push(literal(2 /* core.AttributeMarker.Styles */), ...styles);
  12026. }
  12027. if (bindings.length > 0) {
  12028. attrArray.push(literal(3 /* core.AttributeMarker.Bindings */), ...bindings);
  12029. }
  12030. if (template.length > 0) {
  12031. attrArray.push(literal(4 /* core.AttributeMarker.Template */), ...template);
  12032. }
  12033. if (i18n.length > 0) {
  12034. attrArray.push(literal(6 /* core.AttributeMarker.I18n */), ...i18n);
  12035. }
  12036. return literalArr(attrArray);
  12037. }
  12038. /**
  12039. * Some binding instructions in the update block may actually correspond to i18n bindings. In that
  12040. * case, they should be replaced with i18nExp instructions for the dynamic portions.
  12041. */
  12042. function convertI18nBindings(job) {
  12043. const i18nAttributesByElem = new Map();
  12044. for (const unit of job.units) {
  12045. for (const op of unit.create) {
  12046. if (op.kind === OpKind.I18nAttributes) {
  12047. i18nAttributesByElem.set(op.target, op);
  12048. }
  12049. }
  12050. for (const op of unit.update) {
  12051. switch (op.kind) {
  12052. case OpKind.Property:
  12053. case OpKind.Attribute:
  12054. if (op.i18nContext === null) {
  12055. continue;
  12056. }
  12057. if (!(op.expression instanceof Interpolation)) {
  12058. continue;
  12059. }
  12060. const i18nAttributesForElem = i18nAttributesByElem.get(op.target);
  12061. if (i18nAttributesForElem === undefined) {
  12062. throw new Error('AssertionError: An i18n attribute binding instruction requires the owning element to have an I18nAttributes create instruction');
  12063. }
  12064. if (i18nAttributesForElem.target !== op.target) {
  12065. throw new Error('AssertionError: Expected i18nAttributes target element to match binding target element');
  12066. }
  12067. const ops = [];
  12068. for (let i = 0; i < op.expression.expressions.length; i++) {
  12069. const expr = op.expression.expressions[i];
  12070. if (op.expression.i18nPlaceholders.length !== op.expression.expressions.length) {
  12071. throw new Error(`AssertionError: An i18n attribute binding instruction requires the same number of expressions and placeholders, but found ${op.expression.i18nPlaceholders.length} placeholders and ${op.expression.expressions.length} expressions`);
  12072. }
  12073. ops.push(createI18nExpressionOp(op.i18nContext, i18nAttributesForElem.target, i18nAttributesForElem.xref, i18nAttributesForElem.handle, expr, null, op.expression.i18nPlaceholders[i], I18nParamResolutionTime.Creation, I18nExpressionFor.I18nAttribute, op.name, op.sourceSpan));
  12074. }
  12075. OpList.replaceWithMany(op, ops);
  12076. break;
  12077. }
  12078. }
  12079. }
  12080. }
  12081. /**
  12082. * Resolve the dependency function of a deferred block.
  12083. */
  12084. function resolveDeferDepsFns(job) {
  12085. for (const unit of job.units) {
  12086. for (const op of unit.create) {
  12087. if (op.kind === OpKind.Defer) {
  12088. if (op.resolverFn !== null) {
  12089. continue;
  12090. }
  12091. if (op.ownResolverFn !== null) {
  12092. if (op.handle.slot === null) {
  12093. throw new Error('AssertionError: slot must be assigned before extracting defer deps functions');
  12094. }
  12095. const fullPathName = unit.fnName?.replace('_Template', '');
  12096. op.resolverFn = job.pool.getSharedFunctionReference(op.ownResolverFn, `${fullPathName}_Defer_${op.handle.slot}_DepsFn`,
  12097. /* Don't use unique names for TDB compatibility */ false);
  12098. }
  12099. }
  12100. }
  12101. }
  12102. }
  12103. /**
  12104. * Create one helper context op per i18n block (including generate descending blocks).
  12105. *
  12106. * Also, if an ICU exists inside an i18n block that also contains other localizable content (such as
  12107. * string), create an additional helper context op for the ICU.
  12108. *
  12109. * These context ops are later used for generating i18n messages. (Although we generate at least one
  12110. * context op per nested view, we will collect them up the tree later, to generate a top-level
  12111. * message.)
  12112. */
  12113. function createI18nContexts(job) {
  12114. // Create i18n context ops for i18n attrs.
  12115. const attrContextByMessage = new Map();
  12116. for (const unit of job.units) {
  12117. for (const op of unit.ops()) {
  12118. switch (op.kind) {
  12119. case OpKind.Binding:
  12120. case OpKind.Property:
  12121. case OpKind.Attribute:
  12122. case OpKind.ExtractedAttribute:
  12123. if (op.i18nMessage === null) {
  12124. continue;
  12125. }
  12126. if (!attrContextByMessage.has(op.i18nMessage)) {
  12127. const i18nContext = createI18nContextOp(I18nContextKind.Attr, job.allocateXrefId(), null, op.i18nMessage, null);
  12128. unit.create.push(i18nContext);
  12129. attrContextByMessage.set(op.i18nMessage, i18nContext.xref);
  12130. }
  12131. op.i18nContext = attrContextByMessage.get(op.i18nMessage);
  12132. break;
  12133. }
  12134. }
  12135. }
  12136. // Create i18n context ops for root i18n blocks.
  12137. const blockContextByI18nBlock = new Map();
  12138. for (const unit of job.units) {
  12139. for (const op of unit.create) {
  12140. switch (op.kind) {
  12141. case OpKind.I18nStart:
  12142. if (op.xref === op.root) {
  12143. const contextOp = createI18nContextOp(I18nContextKind.RootI18n, job.allocateXrefId(), op.xref, op.message, null);
  12144. unit.create.push(contextOp);
  12145. op.context = contextOp.xref;
  12146. blockContextByI18nBlock.set(op.xref, contextOp);
  12147. }
  12148. break;
  12149. }
  12150. }
  12151. }
  12152. // Assign i18n contexts for child i18n blocks. These don't need their own conext, instead they
  12153. // should inherit from their root i18n block.
  12154. for (const unit of job.units) {
  12155. for (const op of unit.create) {
  12156. if (op.kind === OpKind.I18nStart && op.xref !== op.root) {
  12157. const rootContext = blockContextByI18nBlock.get(op.root);
  12158. if (rootContext === undefined) {
  12159. throw Error('AssertionError: Root i18n block i18n context should have been created.');
  12160. }
  12161. op.context = rootContext.xref;
  12162. blockContextByI18nBlock.set(op.xref, rootContext);
  12163. }
  12164. }
  12165. }
  12166. // Create or assign i18n contexts for ICUs.
  12167. let currentI18nOp = null;
  12168. for (const unit of job.units) {
  12169. for (const op of unit.create) {
  12170. switch (op.kind) {
  12171. case OpKind.I18nStart:
  12172. currentI18nOp = op;
  12173. break;
  12174. case OpKind.I18nEnd:
  12175. currentI18nOp = null;
  12176. break;
  12177. case OpKind.IcuStart:
  12178. if (currentI18nOp === null) {
  12179. throw Error('AssertionError: Unexpected ICU outside of an i18n block.');
  12180. }
  12181. if (op.message.id !== currentI18nOp.message.id) {
  12182. // This ICU is a sub-message inside its parent i18n block message. We need to give it
  12183. // its own context.
  12184. const contextOp = createI18nContextOp(I18nContextKind.Icu, job.allocateXrefId(), currentI18nOp.root, op.message, null);
  12185. unit.create.push(contextOp);
  12186. op.context = contextOp.xref;
  12187. }
  12188. else {
  12189. // This ICU is the only translatable content in its parent i18n block. We need to
  12190. // convert the parent's context into an ICU context.
  12191. op.context = currentI18nOp.context;
  12192. blockContextByI18nBlock.get(currentI18nOp.xref).contextKind = I18nContextKind.Icu;
  12193. }
  12194. break;
  12195. }
  12196. }
  12197. }
  12198. }
  12199. /**
  12200. * Deduplicate text bindings, e.g. <div class="cls1" class="cls2">
  12201. */
  12202. function deduplicateTextBindings(job) {
  12203. const seen = new Map();
  12204. for (const unit of job.units) {
  12205. for (const op of unit.update.reversed()) {
  12206. if (op.kind === OpKind.Binding && op.isTextAttribute) {
  12207. const seenForElement = seen.get(op.target) || new Set();
  12208. if (seenForElement.has(op.name)) {
  12209. if (job.compatibility === CompatibilityMode.TemplateDefinitionBuilder) {
  12210. // For most duplicated attributes, TemplateDefinitionBuilder lists all of the values in
  12211. // the consts array. However, for style and class attributes it only keeps the last one.
  12212. // We replicate that behavior here since it has actual consequences for apps with
  12213. // duplicate class or style attrs.
  12214. if (op.name === 'style' || op.name === 'class') {
  12215. OpList.remove(op);
  12216. }
  12217. }
  12218. }
  12219. seenForElement.add(op.name);
  12220. seen.set(op.target, seenForElement);
  12221. }
  12222. }
  12223. }
  12224. }
  12225. /**
  12226. * Defer instructions take a configuration array, which should be collected into the component
  12227. * consts. This phase finds the config options, and creates the corresponding const array.
  12228. */
  12229. function configureDeferInstructions(job) {
  12230. for (const unit of job.units) {
  12231. for (const op of unit.create) {
  12232. if (op.kind !== OpKind.Defer) {
  12233. continue;
  12234. }
  12235. if (op.placeholderMinimumTime !== null) {
  12236. op.placeholderConfig = new ConstCollectedExpr(literalOrArrayLiteral([op.placeholderMinimumTime]));
  12237. }
  12238. if (op.loadingMinimumTime !== null || op.loadingAfterTime !== null) {
  12239. op.loadingConfig = new ConstCollectedExpr(literalOrArrayLiteral([op.loadingMinimumTime, op.loadingAfterTime]));
  12240. }
  12241. }
  12242. }
  12243. }
  12244. /**
  12245. * Some `defer` conditions can reference other elements in the template, using their local reference
  12246. * names. However, the semantics are quite different from the normal local reference system: in
  12247. * particular, we need to look at local reference names in enclosing views. This phase resolves
  12248. * all such references to actual xrefs.
  12249. */
  12250. function resolveDeferTargetNames(job) {
  12251. const scopes = new Map();
  12252. function getScopeForView(view) {
  12253. if (scopes.has(view.xref)) {
  12254. return scopes.get(view.xref);
  12255. }
  12256. const scope = new Scope$1();
  12257. for (const op of view.create) {
  12258. // add everything that can be referenced.
  12259. if (!isElementOrContainerOp(op) || op.localRefs === null) {
  12260. continue;
  12261. }
  12262. if (!Array.isArray(op.localRefs)) {
  12263. throw new Error('LocalRefs were already processed, but were needed to resolve defer targets.');
  12264. }
  12265. for (const ref of op.localRefs) {
  12266. if (ref.target !== '') {
  12267. continue;
  12268. }
  12269. scope.targets.set(ref.name, { xref: op.xref, slot: op.handle });
  12270. }
  12271. }
  12272. scopes.set(view.xref, scope);
  12273. return scope;
  12274. }
  12275. function resolveTrigger(deferOwnerView, op, placeholderView) {
  12276. switch (op.trigger.kind) {
  12277. case DeferTriggerKind.Idle:
  12278. case DeferTriggerKind.Never:
  12279. case DeferTriggerKind.Immediate:
  12280. case DeferTriggerKind.Timer:
  12281. return;
  12282. case DeferTriggerKind.Hover:
  12283. case DeferTriggerKind.Interaction:
  12284. case DeferTriggerKind.Viewport:
  12285. if (op.trigger.targetName === null) {
  12286. // A `null` target name indicates we should default to the first element in the
  12287. // placeholder block.
  12288. if (placeholderView === null) {
  12289. throw new Error('defer on trigger with no target name must have a placeholder block');
  12290. }
  12291. const placeholder = job.views.get(placeholderView);
  12292. if (placeholder == undefined) {
  12293. throw new Error('AssertionError: could not find placeholder view for defer on trigger');
  12294. }
  12295. for (const placeholderOp of placeholder.create) {
  12296. if (hasConsumesSlotTrait(placeholderOp) &&
  12297. (isElementOrContainerOp(placeholderOp) ||
  12298. placeholderOp.kind === OpKind.Projection)) {
  12299. op.trigger.targetXref = placeholderOp.xref;
  12300. op.trigger.targetView = placeholderView;
  12301. op.trigger.targetSlotViewSteps = -1;
  12302. op.trigger.targetSlot = placeholderOp.handle;
  12303. return;
  12304. }
  12305. }
  12306. return;
  12307. }
  12308. let view = placeholderView !== null ? job.views.get(placeholderView) : deferOwnerView;
  12309. let step = placeholderView !== null ? -1 : 0;
  12310. while (view !== null) {
  12311. const scope = getScopeForView(view);
  12312. if (scope.targets.has(op.trigger.targetName)) {
  12313. const { xref, slot } = scope.targets.get(op.trigger.targetName);
  12314. op.trigger.targetXref = xref;
  12315. op.trigger.targetView = view.xref;
  12316. op.trigger.targetSlotViewSteps = step;
  12317. op.trigger.targetSlot = slot;
  12318. return;
  12319. }
  12320. view = view.parent !== null ? job.views.get(view.parent) : null;
  12321. step++;
  12322. }
  12323. break;
  12324. default:
  12325. throw new Error(`Trigger kind ${op.trigger.kind} not handled`);
  12326. }
  12327. }
  12328. // Find the defer ops, and assign the data about their targets.
  12329. for (const unit of job.units) {
  12330. const defers = new Map();
  12331. for (const op of unit.create) {
  12332. switch (op.kind) {
  12333. case OpKind.Defer:
  12334. defers.set(op.xref, op);
  12335. break;
  12336. case OpKind.DeferOn:
  12337. const deferOp = defers.get(op.defer);
  12338. resolveTrigger(unit, op, op.modifier === "hydrate" /* ir.DeferOpModifierKind.HYDRATE */
  12339. ? deferOp.mainView
  12340. : deferOp.placeholderView);
  12341. break;
  12342. }
  12343. }
  12344. }
  12345. }
  12346. let Scope$1 = class Scope {
  12347. targets = new Map();
  12348. };
  12349. const REPLACEMENTS = new Map([
  12350. [OpKind.ElementEnd, [OpKind.ElementStart, OpKind.Element]],
  12351. [OpKind.ContainerEnd, [OpKind.ContainerStart, OpKind.Container]],
  12352. [OpKind.I18nEnd, [OpKind.I18nStart, OpKind.I18n]],
  12353. ]);
  12354. /**
  12355. * Op kinds that should not prevent merging of start/end ops.
  12356. */
  12357. const IGNORED_OP_KINDS = new Set([OpKind.Pipe]);
  12358. /**
  12359. * Replace sequences of mergable instructions (e.g. `ElementStart` and `ElementEnd`) with a
  12360. * consolidated instruction (e.g. `Element`).
  12361. */
  12362. function collapseEmptyInstructions(job) {
  12363. for (const unit of job.units) {
  12364. for (const op of unit.create) {
  12365. // Find end ops that may be able to be merged.
  12366. const opReplacements = REPLACEMENTS.get(op.kind);
  12367. if (opReplacements === undefined) {
  12368. continue;
  12369. }
  12370. const [startKind, mergedKind] = opReplacements;
  12371. // Locate the previous (non-ignored) op.
  12372. let prevOp = op.prev;
  12373. while (prevOp !== null && IGNORED_OP_KINDS.has(prevOp.kind)) {
  12374. prevOp = prevOp.prev;
  12375. }
  12376. // If the previous op is the corresponding start op, we can megre.
  12377. if (prevOp !== null && prevOp.kind === startKind) {
  12378. // Transmute the start instruction to the merged version. This is safe as they're designed
  12379. // to be identical apart from the `kind`.
  12380. prevOp.kind = mergedKind;
  12381. // Remove the end instruction.
  12382. OpList.remove(op);
  12383. }
  12384. }
  12385. }
  12386. }
  12387. /**
  12388. * Safe read expressions such as `a?.b` have different semantics in Angular templates as
  12389. * compared to JavaScript. In particular, they default to `null` instead of `undefined`. This phase
  12390. * finds all unresolved safe read expressions, and converts them into the appropriate output AST
  12391. * reads, guarded by null checks. We generate temporaries as needed, to avoid re-evaluating the same
  12392. * sub-expression multiple times.
  12393. */
  12394. function expandSafeReads(job) {
  12395. for (const unit of job.units) {
  12396. for (const op of unit.ops()) {
  12397. transformExpressionsInOp(op, (e) => safeTransform(e, { job }), VisitorContextFlag.None);
  12398. transformExpressionsInOp(op, ternaryTransform, VisitorContextFlag.None);
  12399. }
  12400. }
  12401. }
  12402. function needsTemporaryInSafeAccess(e) {
  12403. // TODO: We probably want to use an expression visitor to recursively visit all descendents.
  12404. // However, that would potentially do a lot of extra work (because it cannot short circuit), so we
  12405. // implement the logic ourselves for now.
  12406. if (e instanceof UnaryOperatorExpr) {
  12407. return needsTemporaryInSafeAccess(e.expr);
  12408. }
  12409. else if (e instanceof BinaryOperatorExpr) {
  12410. return needsTemporaryInSafeAccess(e.lhs) || needsTemporaryInSafeAccess(e.rhs);
  12411. }
  12412. else if (e instanceof ConditionalExpr) {
  12413. if (e.falseCase && needsTemporaryInSafeAccess(e.falseCase))
  12414. return true;
  12415. return needsTemporaryInSafeAccess(e.condition) || needsTemporaryInSafeAccess(e.trueCase);
  12416. }
  12417. else if (e instanceof NotExpr) {
  12418. return needsTemporaryInSafeAccess(e.condition);
  12419. }
  12420. else if (e instanceof AssignTemporaryExpr) {
  12421. return needsTemporaryInSafeAccess(e.expr);
  12422. }
  12423. else if (e instanceof ReadPropExpr) {
  12424. return needsTemporaryInSafeAccess(e.receiver);
  12425. }
  12426. else if (e instanceof ReadKeyExpr) {
  12427. return needsTemporaryInSafeAccess(e.receiver) || needsTemporaryInSafeAccess(e.index);
  12428. }
  12429. // TODO: Switch to a method which is exhaustive of newly added expression subtypes.
  12430. return (e instanceof InvokeFunctionExpr ||
  12431. e instanceof LiteralArrayExpr ||
  12432. e instanceof LiteralMapExpr ||
  12433. e instanceof SafeInvokeFunctionExpr ||
  12434. e instanceof PipeBindingExpr);
  12435. }
  12436. function temporariesIn(e) {
  12437. const temporaries = new Set();
  12438. // TODO: Although it's not currently supported by the transform helper, we should be able to
  12439. // short-circuit exploring the tree to do less work. In particular, we don't have to penetrate
  12440. // into the subexpressions of temporary assignments.
  12441. transformExpressionsInExpression(e, (e) => {
  12442. if (e instanceof AssignTemporaryExpr) {
  12443. temporaries.add(e.xref);
  12444. }
  12445. return e;
  12446. }, VisitorContextFlag.None);
  12447. return temporaries;
  12448. }
  12449. function eliminateTemporaryAssignments(e, tmps, ctx) {
  12450. // TODO: We can be more efficient than the transform helper here. We don't need to visit any
  12451. // descendents of temporary assignments.
  12452. transformExpressionsInExpression(e, (e) => {
  12453. if (e instanceof AssignTemporaryExpr && tmps.has(e.xref)) {
  12454. const read = new ReadTemporaryExpr(e.xref);
  12455. // `TemplateDefinitionBuilder` has the (accidental?) behavior of generating assignments of
  12456. // temporary variables to themselves. This happens because some subexpression that the
  12457. // temporary refers to, possibly through nested temporaries, has a function call. We copy that
  12458. // behavior here.
  12459. return ctx.job.compatibility === CompatibilityMode.TemplateDefinitionBuilder
  12460. ? new AssignTemporaryExpr(read, read.xref)
  12461. : read;
  12462. }
  12463. return e;
  12464. }, VisitorContextFlag.None);
  12465. return e;
  12466. }
  12467. /**
  12468. * Creates a safe ternary guarded by the input expression, and with a body generated by the provided
  12469. * callback on the input expression. Generates a temporary variable assignment if needed, and
  12470. * deduplicates nested temporary assignments if needed.
  12471. */
  12472. function safeTernaryWithTemporary(guard, body, ctx) {
  12473. let result;
  12474. if (needsTemporaryInSafeAccess(guard)) {
  12475. const xref = ctx.job.allocateXrefId();
  12476. result = [new AssignTemporaryExpr(guard, xref), new ReadTemporaryExpr(xref)];
  12477. }
  12478. else {
  12479. result = [guard, guard.clone()];
  12480. // Consider an expression like `a?.[b?.c()]?.d`. The `b?.c()` will be transformed first,
  12481. // introducing a temporary assignment into the key. Then, as part of expanding the `?.d`. That
  12482. // assignment will be duplicated into both the guard and expression sides. We de-duplicate it,
  12483. // by transforming it from an assignment into a read on the expression side.
  12484. eliminateTemporaryAssignments(result[1], temporariesIn(result[0]), ctx);
  12485. }
  12486. return new SafeTernaryExpr(result[0], body(result[1]));
  12487. }
  12488. function isSafeAccessExpression(e) {
  12489. return (e instanceof SafePropertyReadExpr ||
  12490. e instanceof SafeKeyedReadExpr ||
  12491. e instanceof SafeInvokeFunctionExpr);
  12492. }
  12493. function isUnsafeAccessExpression(e) {
  12494. return (e instanceof ReadPropExpr || e instanceof ReadKeyExpr || e instanceof InvokeFunctionExpr);
  12495. }
  12496. function isAccessExpression(e) {
  12497. return isSafeAccessExpression(e) || isUnsafeAccessExpression(e);
  12498. }
  12499. function deepestSafeTernary(e) {
  12500. if (isAccessExpression(e) && e.receiver instanceof SafeTernaryExpr) {
  12501. let st = e.receiver;
  12502. while (st.expr instanceof SafeTernaryExpr) {
  12503. st = st.expr;
  12504. }
  12505. return st;
  12506. }
  12507. return null;
  12508. }
  12509. // TODO: When strict compatibility with TemplateDefinitionBuilder is not required, we can use `&&`
  12510. // instead to save some code size.
  12511. function safeTransform(e, ctx) {
  12512. if (!isAccessExpression(e)) {
  12513. return e;
  12514. }
  12515. const dst = deepestSafeTernary(e);
  12516. if (dst) {
  12517. if (e instanceof InvokeFunctionExpr) {
  12518. dst.expr = dst.expr.callFn(e.args);
  12519. return e.receiver;
  12520. }
  12521. if (e instanceof ReadPropExpr) {
  12522. dst.expr = dst.expr.prop(e.name);
  12523. return e.receiver;
  12524. }
  12525. if (e instanceof ReadKeyExpr) {
  12526. dst.expr = dst.expr.key(e.index);
  12527. return e.receiver;
  12528. }
  12529. if (e instanceof SafeInvokeFunctionExpr) {
  12530. dst.expr = safeTernaryWithTemporary(dst.expr, (r) => r.callFn(e.args), ctx);
  12531. return e.receiver;
  12532. }
  12533. if (e instanceof SafePropertyReadExpr) {
  12534. dst.expr = safeTernaryWithTemporary(dst.expr, (r) => r.prop(e.name), ctx);
  12535. return e.receiver;
  12536. }
  12537. if (e instanceof SafeKeyedReadExpr) {
  12538. dst.expr = safeTernaryWithTemporary(dst.expr, (r) => r.key(e.index), ctx);
  12539. return e.receiver;
  12540. }
  12541. }
  12542. else {
  12543. if (e instanceof SafeInvokeFunctionExpr) {
  12544. return safeTernaryWithTemporary(e.receiver, (r) => r.callFn(e.args), ctx);
  12545. }
  12546. if (e instanceof SafePropertyReadExpr) {
  12547. return safeTernaryWithTemporary(e.receiver, (r) => r.prop(e.name), ctx);
  12548. }
  12549. if (e instanceof SafeKeyedReadExpr) {
  12550. return safeTernaryWithTemporary(e.receiver, (r) => r.key(e.index), ctx);
  12551. }
  12552. }
  12553. return e;
  12554. }
  12555. function ternaryTransform(e) {
  12556. if (!(e instanceof SafeTernaryExpr)) {
  12557. return e;
  12558. }
  12559. return new ConditionalExpr(new BinaryOperatorExpr(BinaryOperator.Equals, e.guard, NULL_EXPR), NULL_EXPR, e.expr);
  12560. }
  12561. /**
  12562. * The escape sequence used indicate message param values.
  12563. */
  12564. const ESCAPE$1 = '\uFFFD';
  12565. /**
  12566. * Marker used to indicate an element tag.
  12567. */
  12568. const ELEMENT_MARKER = '#';
  12569. /**
  12570. * Marker used to indicate a template tag.
  12571. */
  12572. const TEMPLATE_MARKER = '*';
  12573. /**
  12574. * Marker used to indicate closing of an element or template tag.
  12575. */
  12576. const TAG_CLOSE_MARKER = '/';
  12577. /**
  12578. * Marker used to indicate the sub-template context.
  12579. */
  12580. const CONTEXT_MARKER = ':';
  12581. /**
  12582. * Marker used to indicate the start of a list of values.
  12583. */
  12584. const LIST_START_MARKER = '[';
  12585. /**
  12586. * Marker used to indicate the end of a list of values.
  12587. */
  12588. const LIST_END_MARKER = ']';
  12589. /**
  12590. * Delimiter used to separate multiple values in a list.
  12591. */
  12592. const LIST_DELIMITER = '|';
  12593. /**
  12594. * Formats the param maps on extracted message ops into a maps of `Expression` objects that can be
  12595. * used in the final output.
  12596. */
  12597. function extractI18nMessages(job) {
  12598. // Create an i18n message for each context.
  12599. // TODO: Merge the context op with the message op since they're 1:1 anyways.
  12600. const i18nMessagesByContext = new Map();
  12601. const i18nBlocks = new Map();
  12602. const i18nContexts = new Map();
  12603. for (const unit of job.units) {
  12604. for (const op of unit.create) {
  12605. switch (op.kind) {
  12606. case OpKind.I18nContext:
  12607. const i18nMessageOp = createI18nMessage(job, op);
  12608. unit.create.push(i18nMessageOp);
  12609. i18nMessagesByContext.set(op.xref, i18nMessageOp);
  12610. i18nContexts.set(op.xref, op);
  12611. break;
  12612. case OpKind.I18nStart:
  12613. i18nBlocks.set(op.xref, op);
  12614. break;
  12615. }
  12616. }
  12617. }
  12618. // Associate sub-messages for ICUs with their root message. At this point we can also remove the
  12619. // ICU start/end ops, as they are no longer needed.
  12620. let currentIcu = null;
  12621. for (const unit of job.units) {
  12622. for (const op of unit.create) {
  12623. switch (op.kind) {
  12624. case OpKind.IcuStart:
  12625. currentIcu = op;
  12626. OpList.remove(op);
  12627. // Skip any contexts not associated with an ICU.
  12628. const icuContext = i18nContexts.get(op.context);
  12629. if (icuContext.contextKind !== I18nContextKind.Icu) {
  12630. continue;
  12631. }
  12632. // Skip ICUs that share a context with their i18n message. These represent root-level
  12633. // ICUs, not sub-messages.
  12634. const i18nBlock = i18nBlocks.get(icuContext.i18nBlock);
  12635. if (i18nBlock.context === icuContext.xref) {
  12636. continue;
  12637. }
  12638. // Find the root message and push this ICUs message as a sub-message.
  12639. const rootI18nBlock = i18nBlocks.get(i18nBlock.root);
  12640. const rootMessage = i18nMessagesByContext.get(rootI18nBlock.context);
  12641. if (rootMessage === undefined) {
  12642. throw Error('AssertionError: ICU sub-message should belong to a root message.');
  12643. }
  12644. const subMessage = i18nMessagesByContext.get(icuContext.xref);
  12645. subMessage.messagePlaceholder = op.messagePlaceholder;
  12646. rootMessage.subMessages.push(subMessage.xref);
  12647. break;
  12648. case OpKind.IcuEnd:
  12649. currentIcu = null;
  12650. OpList.remove(op);
  12651. break;
  12652. case OpKind.IcuPlaceholder:
  12653. // Add ICU placeholders to the message, then remove the ICU placeholder ops.
  12654. if (currentIcu === null || currentIcu.context == null) {
  12655. throw Error('AssertionError: Unexpected ICU placeholder outside of i18n context');
  12656. }
  12657. const msg = i18nMessagesByContext.get(currentIcu.context);
  12658. msg.postprocessingParams.set(op.name, literal(formatIcuPlaceholder(op)));
  12659. OpList.remove(op);
  12660. break;
  12661. }
  12662. }
  12663. }
  12664. }
  12665. /**
  12666. * Create an i18n message op from an i18n context op.
  12667. */
  12668. function createI18nMessage(job, context, messagePlaceholder) {
  12669. let formattedParams = formatParams(context.params);
  12670. const formattedPostprocessingParams = formatParams(context.postprocessingParams);
  12671. let needsPostprocessing = [...context.params.values()].some((v) => v.length > 1);
  12672. return createI18nMessageOp(job.allocateXrefId(), context.xref, context.i18nBlock, context.message, null, formattedParams, formattedPostprocessingParams, needsPostprocessing);
  12673. }
  12674. /**
  12675. * Formats an ICU placeholder into a single string with expression placeholders.
  12676. */
  12677. function formatIcuPlaceholder(op) {
  12678. if (op.strings.length !== op.expressionPlaceholders.length + 1) {
  12679. throw Error(`AssertionError: Invalid ICU placeholder with ${op.strings.length} strings and ${op.expressionPlaceholders.length} expressions`);
  12680. }
  12681. const values = op.expressionPlaceholders.map(formatValue);
  12682. return op.strings.flatMap((str, i) => [str, values[i] || '']).join('');
  12683. }
  12684. /**
  12685. * Formats a map of `I18nParamValue[]` values into a map of `Expression` values.
  12686. */
  12687. function formatParams(params) {
  12688. const formattedParams = new Map();
  12689. for (const [placeholder, placeholderValues] of params) {
  12690. const serializedValues = formatParamValues(placeholderValues);
  12691. if (serializedValues !== null) {
  12692. formattedParams.set(placeholder, literal(serializedValues));
  12693. }
  12694. }
  12695. return formattedParams;
  12696. }
  12697. /**
  12698. * Formats an `I18nParamValue[]` into a string (or null for empty array).
  12699. */
  12700. function formatParamValues(values) {
  12701. if (values.length === 0) {
  12702. return null;
  12703. }
  12704. const serializedValues = values.map((value) => formatValue(value));
  12705. return serializedValues.length === 1
  12706. ? serializedValues[0]
  12707. : `${LIST_START_MARKER}${serializedValues.join(LIST_DELIMITER)}${LIST_END_MARKER}`;
  12708. }
  12709. /**
  12710. * Formats a single `I18nParamValue` into a string
  12711. */
  12712. function formatValue(value) {
  12713. // Element tags with a structural directive use a special form that concatenates the element and
  12714. // template values.
  12715. if (value.flags & I18nParamValueFlags.ElementTag &&
  12716. value.flags & I18nParamValueFlags.TemplateTag) {
  12717. if (typeof value.value !== 'object') {
  12718. throw Error('AssertionError: Expected i18n param value to have an element and template slot');
  12719. }
  12720. const elementValue = formatValue({
  12721. ...value,
  12722. value: value.value.element,
  12723. flags: value.flags & ~I18nParamValueFlags.TemplateTag,
  12724. });
  12725. const templateValue = formatValue({
  12726. ...value,
  12727. value: value.value.template,
  12728. flags: value.flags & ~I18nParamValueFlags.ElementTag,
  12729. });
  12730. // TODO(mmalerba): This is likely a bug in TemplateDefinitionBuilder, we should not need to
  12731. // record the template value twice. For now I'm re-implementing the behavior here to keep the
  12732. // output consistent with TemplateDefinitionBuilder.
  12733. if (value.flags & I18nParamValueFlags.OpenTag &&
  12734. value.flags & I18nParamValueFlags.CloseTag) {
  12735. return `${templateValue}${elementValue}${templateValue}`;
  12736. }
  12737. // To match the TemplateDefinitionBuilder output, flip the order depending on whether the
  12738. // values represent a closing or opening tag (or both).
  12739. // TODO(mmalerba): Figure out if this makes a difference in terms of either functionality,
  12740. // or the resulting message ID. If not, we can remove the special-casing in the future.
  12741. return value.flags & I18nParamValueFlags.CloseTag
  12742. ? `${elementValue}${templateValue}`
  12743. : `${templateValue}${elementValue}`;
  12744. }
  12745. // Self-closing tags use a special form that concatenates the start and close tag values.
  12746. if (value.flags & I18nParamValueFlags.OpenTag &&
  12747. value.flags & I18nParamValueFlags.CloseTag) {
  12748. return `${formatValue({
  12749. ...value,
  12750. flags: value.flags & ~I18nParamValueFlags.CloseTag,
  12751. })}${formatValue({ ...value, flags: value.flags & ~I18nParamValueFlags.OpenTag })}`;
  12752. }
  12753. // If there are no special flags, just return the raw value.
  12754. if (value.flags === I18nParamValueFlags.None) {
  12755. return `${value.value}`;
  12756. }
  12757. // Encode the remaining flags as part of the value.
  12758. let tagMarker = '';
  12759. let closeMarker = '';
  12760. if (value.flags & I18nParamValueFlags.ElementTag) {
  12761. tagMarker = ELEMENT_MARKER;
  12762. }
  12763. else if (value.flags & I18nParamValueFlags.TemplateTag) {
  12764. tagMarker = TEMPLATE_MARKER;
  12765. }
  12766. if (tagMarker !== '') {
  12767. closeMarker = value.flags & I18nParamValueFlags.CloseTag ? TAG_CLOSE_MARKER : '';
  12768. }
  12769. const context = value.subTemplateIndex === null ? '' : `${CONTEXT_MARKER}${value.subTemplateIndex}`;
  12770. return `${ESCAPE$1}${closeMarker}${tagMarker}${value.value}${context}${ESCAPE$1}`;
  12771. }
  12772. /**
  12773. * Generate `ir.AdvanceOp`s in between `ir.UpdateOp`s that ensure the runtime's implicit slot
  12774. * context will be advanced correctly.
  12775. */
  12776. function generateAdvance(job) {
  12777. for (const unit of job.units) {
  12778. // First build a map of all of the declarations in the view that have assigned slots.
  12779. const slotMap = new Map();
  12780. for (const op of unit.create) {
  12781. if (!hasConsumesSlotTrait(op)) {
  12782. continue;
  12783. }
  12784. else if (op.handle.slot === null) {
  12785. throw new Error(`AssertionError: expected slots to have been allocated before generating advance() calls`);
  12786. }
  12787. slotMap.set(op.xref, op.handle.slot);
  12788. }
  12789. // Next, step through the update operations and generate `ir.AdvanceOp`s as required to ensure
  12790. // the runtime's implicit slot counter will be set to the correct slot before executing each
  12791. // update operation which depends on it.
  12792. //
  12793. // To do that, we track what the runtime's slot counter will be through the update operations.
  12794. let slotContext = 0;
  12795. for (const op of unit.update) {
  12796. let consumer = null;
  12797. if (hasDependsOnSlotContextTrait(op)) {
  12798. consumer = op;
  12799. }
  12800. else {
  12801. visitExpressionsInOp(op, (expr) => {
  12802. if (consumer === null && hasDependsOnSlotContextTrait(expr)) {
  12803. consumer = expr;
  12804. }
  12805. });
  12806. }
  12807. if (consumer === null) {
  12808. continue;
  12809. }
  12810. if (!slotMap.has(consumer.target)) {
  12811. // We expect ops that _do_ depend on the slot counter to point at declarations that exist in
  12812. // the `slotMap`.
  12813. throw new Error(`AssertionError: reference to unknown slot for target ${consumer.target}`);
  12814. }
  12815. const slot = slotMap.get(consumer.target);
  12816. // Does the slot counter need to be adjusted?
  12817. if (slotContext !== slot) {
  12818. // If so, generate an `ir.AdvanceOp` to advance the counter.
  12819. const delta = slot - slotContext;
  12820. if (delta < 0) {
  12821. throw new Error(`AssertionError: slot counter should never need to move backwards`);
  12822. }
  12823. OpList.insertBefore(createAdvanceOp(delta, consumer.sourceSpan), op);
  12824. slotContext = slot;
  12825. }
  12826. }
  12827. }
  12828. }
  12829. /**
  12830. * Locate projection slots, populate the each component's `ngContentSelectors` literal field,
  12831. * populate `project` arguments, and generate the required `projectionDef` instruction for the job's
  12832. * root view.
  12833. */
  12834. function generateProjectionDefs(job) {
  12835. // TODO: Why does TemplateDefinitionBuilder force a shared constant?
  12836. const share = job.compatibility === CompatibilityMode.TemplateDefinitionBuilder;
  12837. // Collect all selectors from this component, and its nested views. Also, assign each projection a
  12838. // unique ascending projection slot index.
  12839. const selectors = [];
  12840. let projectionSlotIndex = 0;
  12841. for (const unit of job.units) {
  12842. for (const op of unit.create) {
  12843. if (op.kind === OpKind.Projection) {
  12844. selectors.push(op.selector);
  12845. op.projectionSlotIndex = projectionSlotIndex++;
  12846. }
  12847. }
  12848. }
  12849. if (selectors.length > 0) {
  12850. // Create the projectionDef array. If we only found a single wildcard selector, then we use the
  12851. // default behavior with no arguments instead.
  12852. let defExpr = null;
  12853. if (selectors.length > 1 || selectors[0] !== '*') {
  12854. const def = selectors.map((s) => (s === '*' ? s : parseSelectorToR3Selector(s)));
  12855. defExpr = job.pool.getConstLiteral(literalOrArrayLiteral(def), share);
  12856. }
  12857. // Create the ngContentSelectors constant.
  12858. job.contentSelectors = job.pool.getConstLiteral(literalOrArrayLiteral(selectors), share);
  12859. // The projection def instruction goes at the beginning of the root view, before any
  12860. // `projection` instructions.
  12861. job.root.create.prepend([createProjectionDefOp(defExpr)]);
  12862. }
  12863. }
  12864. /**
  12865. * Generate a preamble sequence for each view creation block and listener function which declares
  12866. * any variables that be referenced in other operations in the block.
  12867. *
  12868. * Variables generated include:
  12869. * * a saved view context to be used to restore the current view in event listeners.
  12870. * * the context of the restored view within event listener handlers.
  12871. * * context variables from the current view as well as all parent views (including the root
  12872. * context if needed).
  12873. * * local references from elements within the current view and any lexical parents.
  12874. *
  12875. * Variables are generated here unconditionally, and may optimized away in future operations if it
  12876. * turns out their values (and any side effects) are unused.
  12877. */
  12878. function generateVariables(job) {
  12879. recursivelyProcessView(job.root, /* there is no parent scope for the root view */ null);
  12880. }
  12881. /**
  12882. * Process the given `ViewCompilation` and generate preambles for it and any listeners that it
  12883. * declares.
  12884. *
  12885. * @param `parentScope` a scope extracted from the parent view which captures any variables which
  12886. * should be inherited by this view. `null` if the current view is the root view.
  12887. */
  12888. function recursivelyProcessView(view, parentScope) {
  12889. // Extract a `Scope` from this view.
  12890. const scope = getScopeForView(view, parentScope);
  12891. for (const op of view.create) {
  12892. switch (op.kind) {
  12893. case OpKind.Template:
  12894. // Descend into child embedded views.
  12895. recursivelyProcessView(view.job.views.get(op.xref), scope);
  12896. break;
  12897. case OpKind.Projection:
  12898. if (op.fallbackView !== null) {
  12899. recursivelyProcessView(view.job.views.get(op.fallbackView), scope);
  12900. }
  12901. break;
  12902. case OpKind.RepeaterCreate:
  12903. // Descend into child embedded views.
  12904. recursivelyProcessView(view.job.views.get(op.xref), scope);
  12905. if (op.emptyView) {
  12906. recursivelyProcessView(view.job.views.get(op.emptyView), scope);
  12907. }
  12908. if (op.trackByOps !== null) {
  12909. op.trackByOps.prepend(generateVariablesInScopeForView(view, scope, false));
  12910. }
  12911. break;
  12912. case OpKind.Listener:
  12913. case OpKind.TwoWayListener:
  12914. // Prepend variables to listener handler functions.
  12915. op.handlerOps.prepend(generateVariablesInScopeForView(view, scope, true));
  12916. break;
  12917. }
  12918. }
  12919. view.update.prepend(generateVariablesInScopeForView(view, scope, false));
  12920. }
  12921. /**
  12922. * Process a view and generate a `Scope` representing the variables available for reference within
  12923. * that view.
  12924. */
  12925. function getScopeForView(view, parent) {
  12926. const scope = {
  12927. view: view.xref,
  12928. viewContextVariable: {
  12929. kind: SemanticVariableKind.Context,
  12930. name: null,
  12931. view: view.xref,
  12932. },
  12933. contextVariables: new Map(),
  12934. aliases: view.aliases,
  12935. references: [],
  12936. letDeclarations: [],
  12937. parent,
  12938. };
  12939. for (const identifier of view.contextVariables.keys()) {
  12940. scope.contextVariables.set(identifier, {
  12941. kind: SemanticVariableKind.Identifier,
  12942. name: null,
  12943. identifier,
  12944. local: false,
  12945. });
  12946. }
  12947. for (const op of view.create) {
  12948. switch (op.kind) {
  12949. case OpKind.ElementStart:
  12950. case OpKind.Template:
  12951. if (!Array.isArray(op.localRefs)) {
  12952. throw new Error(`AssertionError: expected localRefs to be an array`);
  12953. }
  12954. // Record available local references from this element.
  12955. for (let offset = 0; offset < op.localRefs.length; offset++) {
  12956. scope.references.push({
  12957. name: op.localRefs[offset].name,
  12958. targetId: op.xref,
  12959. targetSlot: op.handle,
  12960. offset,
  12961. variable: {
  12962. kind: SemanticVariableKind.Identifier,
  12963. name: null,
  12964. identifier: op.localRefs[offset].name,
  12965. local: false,
  12966. },
  12967. });
  12968. }
  12969. break;
  12970. case OpKind.DeclareLet:
  12971. scope.letDeclarations.push({
  12972. targetId: op.xref,
  12973. targetSlot: op.handle,
  12974. variable: {
  12975. kind: SemanticVariableKind.Identifier,
  12976. name: null,
  12977. identifier: op.declaredName,
  12978. local: false,
  12979. },
  12980. });
  12981. break;
  12982. }
  12983. }
  12984. return scope;
  12985. }
  12986. /**
  12987. * Generate declarations for all variables that are in scope for a given view.
  12988. *
  12989. * This is a recursive process, as views inherit variables available from their parent view, which
  12990. * itself may have inherited variables, etc.
  12991. */
  12992. function generateVariablesInScopeForView(view, scope, isListener) {
  12993. const newOps = [];
  12994. if (scope.view !== view.xref) {
  12995. // Before generating variables for a parent view, we need to switch to the context of the parent
  12996. // view with a `nextContext` expression. This context switching operation itself declares a
  12997. // variable, because the context of the view may be referenced directly.
  12998. newOps.push(createVariableOp(view.job.allocateXrefId(), scope.viewContextVariable, new NextContextExpr(), VariableFlags.None));
  12999. }
  13000. // Add variables for all context variables available in this scope's view.
  13001. const scopeView = view.job.views.get(scope.view);
  13002. for (const [name, value] of scopeView.contextVariables) {
  13003. const context = new ContextExpr(scope.view);
  13004. // We either read the context, or, if the variable is CTX_REF, use the context directly.
  13005. const variable = value === CTX_REF ? context : new ReadPropExpr(context, value);
  13006. // Add the variable declaration.
  13007. newOps.push(createVariableOp(view.job.allocateXrefId(), scope.contextVariables.get(name), variable, VariableFlags.None));
  13008. }
  13009. for (const alias of scopeView.aliases) {
  13010. newOps.push(createVariableOp(view.job.allocateXrefId(), alias, alias.expression.clone(), VariableFlags.AlwaysInline));
  13011. }
  13012. // Add variables for all local references declared for elements in this scope.
  13013. for (const ref of scope.references) {
  13014. newOps.push(createVariableOp(view.job.allocateXrefId(), ref.variable, new ReferenceExpr(ref.targetId, ref.targetSlot, ref.offset), VariableFlags.None));
  13015. }
  13016. if (scope.view !== view.xref || isListener) {
  13017. for (const decl of scope.letDeclarations) {
  13018. newOps.push(createVariableOp(view.job.allocateXrefId(), decl.variable, new ContextLetReferenceExpr(decl.targetId, decl.targetSlot), VariableFlags.None));
  13019. }
  13020. }
  13021. if (scope.parent !== null) {
  13022. // Recursively add variables from the parent scope.
  13023. newOps.push(...generateVariablesInScopeForView(view, scope.parent, false));
  13024. }
  13025. return newOps;
  13026. }
  13027. /**
  13028. * `ir.ConstCollectedExpr` may be present in any IR expression. This means that expression needs to
  13029. * be lifted into the component const array, and replaced with a reference to the const array at its
  13030. *
  13031. * usage site. This phase walks the IR and performs this transformation.
  13032. */
  13033. function collectConstExpressions(job) {
  13034. for (const unit of job.units) {
  13035. for (const op of unit.ops()) {
  13036. transformExpressionsInOp(op, (expr) => {
  13037. if (!(expr instanceof ConstCollectedExpr)) {
  13038. return expr;
  13039. }
  13040. return literal(job.addConst(expr.expr));
  13041. }, VisitorContextFlag.None);
  13042. }
  13043. }
  13044. }
  13045. const STYLE_DOT = 'style.';
  13046. const CLASS_DOT = 'class.';
  13047. const STYLE_BANG = 'style!';
  13048. const CLASS_BANG = 'class!';
  13049. const BANG_IMPORTANT = '!important';
  13050. /**
  13051. * Host bindings are compiled using a different parser entrypoint, and are parsed quite differently
  13052. * as a result. Therefore, we need to do some extra parsing for host style properties, as compared
  13053. * to non-host style properties.
  13054. * TODO: Unify host bindings and non-host bindings in the parser.
  13055. */
  13056. function parseHostStyleProperties(job) {
  13057. for (const op of job.root.update) {
  13058. if (!(op.kind === OpKind.Binding && op.bindingKind === BindingKind.Property)) {
  13059. continue;
  13060. }
  13061. if (op.name.endsWith(BANG_IMPORTANT)) {
  13062. // Delete any `!important` suffixes from the binding name.
  13063. op.name = op.name.substring(0, op.name.length - BANG_IMPORTANT.length);
  13064. }
  13065. if (op.name.startsWith(STYLE_DOT)) {
  13066. op.bindingKind = BindingKind.StyleProperty;
  13067. op.name = op.name.substring(STYLE_DOT.length);
  13068. if (!isCssCustomProperty(op.name)) {
  13069. op.name = hyphenate$1(op.name);
  13070. }
  13071. const { property, suffix } = parseProperty(op.name);
  13072. op.name = property;
  13073. op.unit = suffix;
  13074. }
  13075. else if (op.name.startsWith(STYLE_BANG)) {
  13076. op.bindingKind = BindingKind.StyleProperty;
  13077. op.name = 'style';
  13078. }
  13079. else if (op.name.startsWith(CLASS_DOT)) {
  13080. op.bindingKind = BindingKind.ClassName;
  13081. op.name = parseProperty(op.name.substring(CLASS_DOT.length)).property;
  13082. }
  13083. else if (op.name.startsWith(CLASS_BANG)) {
  13084. op.bindingKind = BindingKind.ClassName;
  13085. op.name = parseProperty(op.name.substring(CLASS_BANG.length)).property;
  13086. }
  13087. }
  13088. }
  13089. /**
  13090. * Checks whether property name is a custom CSS property.
  13091. * See: https://www.w3.org/TR/css-variables-1
  13092. */
  13093. function isCssCustomProperty(name) {
  13094. return name.startsWith('--');
  13095. }
  13096. function hyphenate$1(value) {
  13097. return value
  13098. .replace(/[a-z][A-Z]/g, (v) => {
  13099. return v.charAt(0) + '-' + v.charAt(1);
  13100. })
  13101. .toLowerCase();
  13102. }
  13103. function parseProperty(name) {
  13104. const overrideIndex = name.indexOf('!important');
  13105. if (overrideIndex !== -1) {
  13106. name = overrideIndex > 0 ? name.substring(0, overrideIndex) : '';
  13107. }
  13108. let suffix = null;
  13109. let property = name;
  13110. const unitIndex = name.lastIndexOf('.');
  13111. if (unitIndex > 0) {
  13112. suffix = name.slice(unitIndex + 1);
  13113. property = name.substring(0, unitIndex);
  13114. }
  13115. return { property, suffix };
  13116. }
  13117. function mapLiteral(obj, quoted = false) {
  13118. return literalMap(Object.keys(obj).map((key) => ({
  13119. key,
  13120. quoted,
  13121. value: obj[key],
  13122. })));
  13123. }
  13124. class IcuSerializerVisitor {
  13125. visitText(text) {
  13126. return text.value;
  13127. }
  13128. visitContainer(container) {
  13129. return container.children.map((child) => child.visit(this)).join('');
  13130. }
  13131. visitIcu(icu) {
  13132. const strCases = Object.keys(icu.cases).map((k) => `${k} {${icu.cases[k].visit(this)}}`);
  13133. const result = `{${icu.expressionPlaceholder}, ${icu.type}, ${strCases.join(' ')}}`;
  13134. return result;
  13135. }
  13136. visitTagPlaceholder(ph) {
  13137. return ph.isVoid
  13138. ? this.formatPh(ph.startName)
  13139. : `${this.formatPh(ph.startName)}${ph.children
  13140. .map((child) => child.visit(this))
  13141. .join('')}${this.formatPh(ph.closeName)}`;
  13142. }
  13143. visitPlaceholder(ph) {
  13144. return this.formatPh(ph.name);
  13145. }
  13146. visitBlockPlaceholder(ph) {
  13147. return `${this.formatPh(ph.startName)}${ph.children
  13148. .map((child) => child.visit(this))
  13149. .join('')}${this.formatPh(ph.closeName)}`;
  13150. }
  13151. visitIcuPlaceholder(ph, context) {
  13152. return this.formatPh(ph.name);
  13153. }
  13154. formatPh(value) {
  13155. return `{${formatI18nPlaceholderName(value, /* useCamelCase */ false)}}`;
  13156. }
  13157. }
  13158. const serializer = new IcuSerializerVisitor();
  13159. function serializeIcuNode(icu) {
  13160. return icu.visit(serializer);
  13161. }
  13162. class NodeWithI18n {
  13163. sourceSpan;
  13164. i18n;
  13165. constructor(sourceSpan, i18n) {
  13166. this.sourceSpan = sourceSpan;
  13167. this.i18n = i18n;
  13168. }
  13169. }
  13170. class Text extends NodeWithI18n {
  13171. value;
  13172. tokens;
  13173. constructor(value, sourceSpan, tokens, i18n) {
  13174. super(sourceSpan, i18n);
  13175. this.value = value;
  13176. this.tokens = tokens;
  13177. }
  13178. visit(visitor, context) {
  13179. return visitor.visitText(this, context);
  13180. }
  13181. }
  13182. class Expansion extends NodeWithI18n {
  13183. switchValue;
  13184. type;
  13185. cases;
  13186. switchValueSourceSpan;
  13187. constructor(switchValue, type, cases, sourceSpan, switchValueSourceSpan, i18n) {
  13188. super(sourceSpan, i18n);
  13189. this.switchValue = switchValue;
  13190. this.type = type;
  13191. this.cases = cases;
  13192. this.switchValueSourceSpan = switchValueSourceSpan;
  13193. }
  13194. visit(visitor, context) {
  13195. return visitor.visitExpansion(this, context);
  13196. }
  13197. }
  13198. class ExpansionCase {
  13199. value;
  13200. expression;
  13201. sourceSpan;
  13202. valueSourceSpan;
  13203. expSourceSpan;
  13204. constructor(value, expression, sourceSpan, valueSourceSpan, expSourceSpan) {
  13205. this.value = value;
  13206. this.expression = expression;
  13207. this.sourceSpan = sourceSpan;
  13208. this.valueSourceSpan = valueSourceSpan;
  13209. this.expSourceSpan = expSourceSpan;
  13210. }
  13211. visit(visitor, context) {
  13212. return visitor.visitExpansionCase(this, context);
  13213. }
  13214. }
  13215. class Attribute extends NodeWithI18n {
  13216. name;
  13217. value;
  13218. keySpan;
  13219. valueSpan;
  13220. valueTokens;
  13221. constructor(name, value, sourceSpan, keySpan, valueSpan, valueTokens, i18n) {
  13222. super(sourceSpan, i18n);
  13223. this.name = name;
  13224. this.value = value;
  13225. this.keySpan = keySpan;
  13226. this.valueSpan = valueSpan;
  13227. this.valueTokens = valueTokens;
  13228. }
  13229. visit(visitor, context) {
  13230. return visitor.visitAttribute(this, context);
  13231. }
  13232. }
  13233. class Element extends NodeWithI18n {
  13234. name;
  13235. attrs;
  13236. children;
  13237. startSourceSpan;
  13238. endSourceSpan;
  13239. constructor(name, attrs, children, sourceSpan, startSourceSpan, endSourceSpan = null, i18n) {
  13240. super(sourceSpan, i18n);
  13241. this.name = name;
  13242. this.attrs = attrs;
  13243. this.children = children;
  13244. this.startSourceSpan = startSourceSpan;
  13245. this.endSourceSpan = endSourceSpan;
  13246. }
  13247. visit(visitor, context) {
  13248. return visitor.visitElement(this, context);
  13249. }
  13250. }
  13251. class Comment {
  13252. value;
  13253. sourceSpan;
  13254. constructor(value, sourceSpan) {
  13255. this.value = value;
  13256. this.sourceSpan = sourceSpan;
  13257. }
  13258. visit(visitor, context) {
  13259. return visitor.visitComment(this, context);
  13260. }
  13261. }
  13262. class Block extends NodeWithI18n {
  13263. name;
  13264. parameters;
  13265. children;
  13266. nameSpan;
  13267. startSourceSpan;
  13268. endSourceSpan;
  13269. constructor(name, parameters, children, sourceSpan, nameSpan, startSourceSpan, endSourceSpan = null, i18n) {
  13270. super(sourceSpan, i18n);
  13271. this.name = name;
  13272. this.parameters = parameters;
  13273. this.children = children;
  13274. this.nameSpan = nameSpan;
  13275. this.startSourceSpan = startSourceSpan;
  13276. this.endSourceSpan = endSourceSpan;
  13277. }
  13278. visit(visitor, context) {
  13279. return visitor.visitBlock(this, context);
  13280. }
  13281. }
  13282. class BlockParameter {
  13283. expression;
  13284. sourceSpan;
  13285. constructor(expression, sourceSpan) {
  13286. this.expression = expression;
  13287. this.sourceSpan = sourceSpan;
  13288. }
  13289. visit(visitor, context) {
  13290. return visitor.visitBlockParameter(this, context);
  13291. }
  13292. }
  13293. class LetDeclaration {
  13294. name;
  13295. value;
  13296. sourceSpan;
  13297. nameSpan;
  13298. valueSpan;
  13299. constructor(name, value, sourceSpan, nameSpan, valueSpan) {
  13300. this.name = name;
  13301. this.value = value;
  13302. this.sourceSpan = sourceSpan;
  13303. this.nameSpan = nameSpan;
  13304. this.valueSpan = valueSpan;
  13305. }
  13306. visit(visitor, context) {
  13307. return visitor.visitLetDeclaration(this, context);
  13308. }
  13309. }
  13310. function visitAll(visitor, nodes, context = null) {
  13311. const result = [];
  13312. const visit = visitor.visit
  13313. ? (ast) => visitor.visit(ast, context) || ast.visit(visitor, context)
  13314. : (ast) => ast.visit(visitor, context);
  13315. nodes.forEach((ast) => {
  13316. const astResult = visit(ast);
  13317. if (astResult) {
  13318. result.push(astResult);
  13319. }
  13320. });
  13321. return result;
  13322. }
  13323. class RecursiveVisitor {
  13324. constructor() { }
  13325. visitElement(ast, context) {
  13326. this.visitChildren(context, (visit) => {
  13327. visit(ast.attrs);
  13328. visit(ast.children);
  13329. });
  13330. }
  13331. visitAttribute(ast, context) { }
  13332. visitText(ast, context) { }
  13333. visitComment(ast, context) { }
  13334. visitExpansion(ast, context) {
  13335. return this.visitChildren(context, (visit) => {
  13336. visit(ast.cases);
  13337. });
  13338. }
  13339. visitExpansionCase(ast, context) { }
  13340. visitBlock(block, context) {
  13341. this.visitChildren(context, (visit) => {
  13342. visit(block.parameters);
  13343. visit(block.children);
  13344. });
  13345. }
  13346. visitBlockParameter(ast, context) { }
  13347. visitLetDeclaration(decl, context) { }
  13348. visitChildren(context, cb) {
  13349. let results = [];
  13350. let t = this;
  13351. function visit(children) {
  13352. if (children)
  13353. results.push(visitAll(t, children, context));
  13354. }
  13355. cb(visit);
  13356. return Array.prototype.concat.apply([], results);
  13357. }
  13358. }
  13359. // Mapping between all HTML entity names and their unicode representation.
  13360. // Generated from https://html.spec.whatwg.org/multipage/entities.json by stripping
  13361. // the `&` and `;` from the keys and removing the duplicates.
  13362. // see https://www.w3.org/TR/html51/syntax.html#named-character-references
  13363. const NAMED_ENTITIES = {
  13364. 'AElig': '\u00C6',
  13365. 'AMP': '\u0026',
  13366. 'amp': '\u0026',
  13367. 'Aacute': '\u00C1',
  13368. 'Abreve': '\u0102',
  13369. 'Acirc': '\u00C2',
  13370. 'Acy': '\u0410',
  13371. 'Afr': '\uD835\uDD04',
  13372. 'Agrave': '\u00C0',
  13373. 'Alpha': '\u0391',
  13374. 'Amacr': '\u0100',
  13375. 'And': '\u2A53',
  13376. 'Aogon': '\u0104',
  13377. 'Aopf': '\uD835\uDD38',
  13378. 'ApplyFunction': '\u2061',
  13379. 'af': '\u2061',
  13380. 'Aring': '\u00C5',
  13381. 'angst': '\u00C5',
  13382. 'Ascr': '\uD835\uDC9C',
  13383. 'Assign': '\u2254',
  13384. 'colone': '\u2254',
  13385. 'coloneq': '\u2254',
  13386. 'Atilde': '\u00C3',
  13387. 'Auml': '\u00C4',
  13388. 'Backslash': '\u2216',
  13389. 'setminus': '\u2216',
  13390. 'setmn': '\u2216',
  13391. 'smallsetminus': '\u2216',
  13392. 'ssetmn': '\u2216',
  13393. 'Barv': '\u2AE7',
  13394. 'Barwed': '\u2306',
  13395. 'doublebarwedge': '\u2306',
  13396. 'Bcy': '\u0411',
  13397. 'Because': '\u2235',
  13398. 'becaus': '\u2235',
  13399. 'because': '\u2235',
  13400. 'Bernoullis': '\u212C',
  13401. 'Bscr': '\u212C',
  13402. 'bernou': '\u212C',
  13403. 'Beta': '\u0392',
  13404. 'Bfr': '\uD835\uDD05',
  13405. 'Bopf': '\uD835\uDD39',
  13406. 'Breve': '\u02D8',
  13407. 'breve': '\u02D8',
  13408. 'Bumpeq': '\u224E',
  13409. 'HumpDownHump': '\u224E',
  13410. 'bump': '\u224E',
  13411. 'CHcy': '\u0427',
  13412. 'COPY': '\u00A9',
  13413. 'copy': '\u00A9',
  13414. 'Cacute': '\u0106',
  13415. 'Cap': '\u22D2',
  13416. 'CapitalDifferentialD': '\u2145',
  13417. 'DD': '\u2145',
  13418. 'Cayleys': '\u212D',
  13419. 'Cfr': '\u212D',
  13420. 'Ccaron': '\u010C',
  13421. 'Ccedil': '\u00C7',
  13422. 'Ccirc': '\u0108',
  13423. 'Cconint': '\u2230',
  13424. 'Cdot': '\u010A',
  13425. 'Cedilla': '\u00B8',
  13426. 'cedil': '\u00B8',
  13427. 'CenterDot': '\u00B7',
  13428. 'centerdot': '\u00B7',
  13429. 'middot': '\u00B7',
  13430. 'Chi': '\u03A7',
  13431. 'CircleDot': '\u2299',
  13432. 'odot': '\u2299',
  13433. 'CircleMinus': '\u2296',
  13434. 'ominus': '\u2296',
  13435. 'CirclePlus': '\u2295',
  13436. 'oplus': '\u2295',
  13437. 'CircleTimes': '\u2297',
  13438. 'otimes': '\u2297',
  13439. 'ClockwiseContourIntegral': '\u2232',
  13440. 'cwconint': '\u2232',
  13441. 'CloseCurlyDoubleQuote': '\u201D',
  13442. 'rdquo': '\u201D',
  13443. 'rdquor': '\u201D',
  13444. 'CloseCurlyQuote': '\u2019',
  13445. 'rsquo': '\u2019',
  13446. 'rsquor': '\u2019',
  13447. 'Colon': '\u2237',
  13448. 'Proportion': '\u2237',
  13449. 'Colone': '\u2A74',
  13450. 'Congruent': '\u2261',
  13451. 'equiv': '\u2261',
  13452. 'Conint': '\u222F',
  13453. 'DoubleContourIntegral': '\u222F',
  13454. 'ContourIntegral': '\u222E',
  13455. 'conint': '\u222E',
  13456. 'oint': '\u222E',
  13457. 'Copf': '\u2102',
  13458. 'complexes': '\u2102',
  13459. 'Coproduct': '\u2210',
  13460. 'coprod': '\u2210',
  13461. 'CounterClockwiseContourIntegral': '\u2233',
  13462. 'awconint': '\u2233',
  13463. 'Cross': '\u2A2F',
  13464. 'Cscr': '\uD835\uDC9E',
  13465. 'Cup': '\u22D3',
  13466. 'CupCap': '\u224D',
  13467. 'asympeq': '\u224D',
  13468. 'DDotrahd': '\u2911',
  13469. 'DJcy': '\u0402',
  13470. 'DScy': '\u0405',
  13471. 'DZcy': '\u040F',
  13472. 'Dagger': '\u2021',
  13473. 'ddagger': '\u2021',
  13474. 'Darr': '\u21A1',
  13475. 'Dashv': '\u2AE4',
  13476. 'DoubleLeftTee': '\u2AE4',
  13477. 'Dcaron': '\u010E',
  13478. 'Dcy': '\u0414',
  13479. 'Del': '\u2207',
  13480. 'nabla': '\u2207',
  13481. 'Delta': '\u0394',
  13482. 'Dfr': '\uD835\uDD07',
  13483. 'DiacriticalAcute': '\u00B4',
  13484. 'acute': '\u00B4',
  13485. 'DiacriticalDot': '\u02D9',
  13486. 'dot': '\u02D9',
  13487. 'DiacriticalDoubleAcute': '\u02DD',
  13488. 'dblac': '\u02DD',
  13489. 'DiacriticalGrave': '\u0060',
  13490. 'grave': '\u0060',
  13491. 'DiacriticalTilde': '\u02DC',
  13492. 'tilde': '\u02DC',
  13493. 'Diamond': '\u22C4',
  13494. 'diam': '\u22C4',
  13495. 'diamond': '\u22C4',
  13496. 'DifferentialD': '\u2146',
  13497. 'dd': '\u2146',
  13498. 'Dopf': '\uD835\uDD3B',
  13499. 'Dot': '\u00A8',
  13500. 'DoubleDot': '\u00A8',
  13501. 'die': '\u00A8',
  13502. 'uml': '\u00A8',
  13503. 'DotDot': '\u20DC',
  13504. 'DotEqual': '\u2250',
  13505. 'doteq': '\u2250',
  13506. 'esdot': '\u2250',
  13507. 'DoubleDownArrow': '\u21D3',
  13508. 'Downarrow': '\u21D3',
  13509. 'dArr': '\u21D3',
  13510. 'DoubleLeftArrow': '\u21D0',
  13511. 'Leftarrow': '\u21D0',
  13512. 'lArr': '\u21D0',
  13513. 'DoubleLeftRightArrow': '\u21D4',
  13514. 'Leftrightarrow': '\u21D4',
  13515. 'hArr': '\u21D4',
  13516. 'iff': '\u21D4',
  13517. 'DoubleLongLeftArrow': '\u27F8',
  13518. 'Longleftarrow': '\u27F8',
  13519. 'xlArr': '\u27F8',
  13520. 'DoubleLongLeftRightArrow': '\u27FA',
  13521. 'Longleftrightarrow': '\u27FA',
  13522. 'xhArr': '\u27FA',
  13523. 'DoubleLongRightArrow': '\u27F9',
  13524. 'Longrightarrow': '\u27F9',
  13525. 'xrArr': '\u27F9',
  13526. 'DoubleRightArrow': '\u21D2',
  13527. 'Implies': '\u21D2',
  13528. 'Rightarrow': '\u21D2',
  13529. 'rArr': '\u21D2',
  13530. 'DoubleRightTee': '\u22A8',
  13531. 'vDash': '\u22A8',
  13532. 'DoubleUpArrow': '\u21D1',
  13533. 'Uparrow': '\u21D1',
  13534. 'uArr': '\u21D1',
  13535. 'DoubleUpDownArrow': '\u21D5',
  13536. 'Updownarrow': '\u21D5',
  13537. 'vArr': '\u21D5',
  13538. 'DoubleVerticalBar': '\u2225',
  13539. 'par': '\u2225',
  13540. 'parallel': '\u2225',
  13541. 'shortparallel': '\u2225',
  13542. 'spar': '\u2225',
  13543. 'DownArrow': '\u2193',
  13544. 'ShortDownArrow': '\u2193',
  13545. 'darr': '\u2193',
  13546. 'downarrow': '\u2193',
  13547. 'DownArrowBar': '\u2913',
  13548. 'DownArrowUpArrow': '\u21F5',
  13549. 'duarr': '\u21F5',
  13550. 'DownBreve': '\u0311',
  13551. 'DownLeftRightVector': '\u2950',
  13552. 'DownLeftTeeVector': '\u295E',
  13553. 'DownLeftVector': '\u21BD',
  13554. 'leftharpoondown': '\u21BD',
  13555. 'lhard': '\u21BD',
  13556. 'DownLeftVectorBar': '\u2956',
  13557. 'DownRightTeeVector': '\u295F',
  13558. 'DownRightVector': '\u21C1',
  13559. 'rhard': '\u21C1',
  13560. 'rightharpoondown': '\u21C1',
  13561. 'DownRightVectorBar': '\u2957',
  13562. 'DownTee': '\u22A4',
  13563. 'top': '\u22A4',
  13564. 'DownTeeArrow': '\u21A7',
  13565. 'mapstodown': '\u21A7',
  13566. 'Dscr': '\uD835\uDC9F',
  13567. 'Dstrok': '\u0110',
  13568. 'ENG': '\u014A',
  13569. 'ETH': '\u00D0',
  13570. 'Eacute': '\u00C9',
  13571. 'Ecaron': '\u011A',
  13572. 'Ecirc': '\u00CA',
  13573. 'Ecy': '\u042D',
  13574. 'Edot': '\u0116',
  13575. 'Efr': '\uD835\uDD08',
  13576. 'Egrave': '\u00C8',
  13577. 'Element': '\u2208',
  13578. 'in': '\u2208',
  13579. 'isin': '\u2208',
  13580. 'isinv': '\u2208',
  13581. 'Emacr': '\u0112',
  13582. 'EmptySmallSquare': '\u25FB',
  13583. 'EmptyVerySmallSquare': '\u25AB',
  13584. 'Eogon': '\u0118',
  13585. 'Eopf': '\uD835\uDD3C',
  13586. 'Epsilon': '\u0395',
  13587. 'Equal': '\u2A75',
  13588. 'EqualTilde': '\u2242',
  13589. 'eqsim': '\u2242',
  13590. 'esim': '\u2242',
  13591. 'Equilibrium': '\u21CC',
  13592. 'rightleftharpoons': '\u21CC',
  13593. 'rlhar': '\u21CC',
  13594. 'Escr': '\u2130',
  13595. 'expectation': '\u2130',
  13596. 'Esim': '\u2A73',
  13597. 'Eta': '\u0397',
  13598. 'Euml': '\u00CB',
  13599. 'Exists': '\u2203',
  13600. 'exist': '\u2203',
  13601. 'ExponentialE': '\u2147',
  13602. 'ee': '\u2147',
  13603. 'exponentiale': '\u2147',
  13604. 'Fcy': '\u0424',
  13605. 'Ffr': '\uD835\uDD09',
  13606. 'FilledSmallSquare': '\u25FC',
  13607. 'FilledVerySmallSquare': '\u25AA',
  13608. 'blacksquare': '\u25AA',
  13609. 'squarf': '\u25AA',
  13610. 'squf': '\u25AA',
  13611. 'Fopf': '\uD835\uDD3D',
  13612. 'ForAll': '\u2200',
  13613. 'forall': '\u2200',
  13614. 'Fouriertrf': '\u2131',
  13615. 'Fscr': '\u2131',
  13616. 'GJcy': '\u0403',
  13617. 'GT': '\u003E',
  13618. 'gt': '\u003E',
  13619. 'Gamma': '\u0393',
  13620. 'Gammad': '\u03DC',
  13621. 'Gbreve': '\u011E',
  13622. 'Gcedil': '\u0122',
  13623. 'Gcirc': '\u011C',
  13624. 'Gcy': '\u0413',
  13625. 'Gdot': '\u0120',
  13626. 'Gfr': '\uD835\uDD0A',
  13627. 'Gg': '\u22D9',
  13628. 'ggg': '\u22D9',
  13629. 'Gopf': '\uD835\uDD3E',
  13630. 'GreaterEqual': '\u2265',
  13631. 'ge': '\u2265',
  13632. 'geq': '\u2265',
  13633. 'GreaterEqualLess': '\u22DB',
  13634. 'gel': '\u22DB',
  13635. 'gtreqless': '\u22DB',
  13636. 'GreaterFullEqual': '\u2267',
  13637. 'gE': '\u2267',
  13638. 'geqq': '\u2267',
  13639. 'GreaterGreater': '\u2AA2',
  13640. 'GreaterLess': '\u2277',
  13641. 'gl': '\u2277',
  13642. 'gtrless': '\u2277',
  13643. 'GreaterSlantEqual': '\u2A7E',
  13644. 'geqslant': '\u2A7E',
  13645. 'ges': '\u2A7E',
  13646. 'GreaterTilde': '\u2273',
  13647. 'gsim': '\u2273',
  13648. 'gtrsim': '\u2273',
  13649. 'Gscr': '\uD835\uDCA2',
  13650. 'Gt': '\u226B',
  13651. 'NestedGreaterGreater': '\u226B',
  13652. 'gg': '\u226B',
  13653. 'HARDcy': '\u042A',
  13654. 'Hacek': '\u02C7',
  13655. 'caron': '\u02C7',
  13656. 'Hat': '\u005E',
  13657. 'Hcirc': '\u0124',
  13658. 'Hfr': '\u210C',
  13659. 'Poincareplane': '\u210C',
  13660. 'HilbertSpace': '\u210B',
  13661. 'Hscr': '\u210B',
  13662. 'hamilt': '\u210B',
  13663. 'Hopf': '\u210D',
  13664. 'quaternions': '\u210D',
  13665. 'HorizontalLine': '\u2500',
  13666. 'boxh': '\u2500',
  13667. 'Hstrok': '\u0126',
  13668. 'HumpEqual': '\u224F',
  13669. 'bumpe': '\u224F',
  13670. 'bumpeq': '\u224F',
  13671. 'IEcy': '\u0415',
  13672. 'IJlig': '\u0132',
  13673. 'IOcy': '\u0401',
  13674. 'Iacute': '\u00CD',
  13675. 'Icirc': '\u00CE',
  13676. 'Icy': '\u0418',
  13677. 'Idot': '\u0130',
  13678. 'Ifr': '\u2111',
  13679. 'Im': '\u2111',
  13680. 'image': '\u2111',
  13681. 'imagpart': '\u2111',
  13682. 'Igrave': '\u00CC',
  13683. 'Imacr': '\u012A',
  13684. 'ImaginaryI': '\u2148',
  13685. 'ii': '\u2148',
  13686. 'Int': '\u222C',
  13687. 'Integral': '\u222B',
  13688. 'int': '\u222B',
  13689. 'Intersection': '\u22C2',
  13690. 'bigcap': '\u22C2',
  13691. 'xcap': '\u22C2',
  13692. 'InvisibleComma': '\u2063',
  13693. 'ic': '\u2063',
  13694. 'InvisibleTimes': '\u2062',
  13695. 'it': '\u2062',
  13696. 'Iogon': '\u012E',
  13697. 'Iopf': '\uD835\uDD40',
  13698. 'Iota': '\u0399',
  13699. 'Iscr': '\u2110',
  13700. 'imagline': '\u2110',
  13701. 'Itilde': '\u0128',
  13702. 'Iukcy': '\u0406',
  13703. 'Iuml': '\u00CF',
  13704. 'Jcirc': '\u0134',
  13705. 'Jcy': '\u0419',
  13706. 'Jfr': '\uD835\uDD0D',
  13707. 'Jopf': '\uD835\uDD41',
  13708. 'Jscr': '\uD835\uDCA5',
  13709. 'Jsercy': '\u0408',
  13710. 'Jukcy': '\u0404',
  13711. 'KHcy': '\u0425',
  13712. 'KJcy': '\u040C',
  13713. 'Kappa': '\u039A',
  13714. 'Kcedil': '\u0136',
  13715. 'Kcy': '\u041A',
  13716. 'Kfr': '\uD835\uDD0E',
  13717. 'Kopf': '\uD835\uDD42',
  13718. 'Kscr': '\uD835\uDCA6',
  13719. 'LJcy': '\u0409',
  13720. 'LT': '\u003C',
  13721. 'lt': '\u003C',
  13722. 'Lacute': '\u0139',
  13723. 'Lambda': '\u039B',
  13724. 'Lang': '\u27EA',
  13725. 'Laplacetrf': '\u2112',
  13726. 'Lscr': '\u2112',
  13727. 'lagran': '\u2112',
  13728. 'Larr': '\u219E',
  13729. 'twoheadleftarrow': '\u219E',
  13730. 'Lcaron': '\u013D',
  13731. 'Lcedil': '\u013B',
  13732. 'Lcy': '\u041B',
  13733. 'LeftAngleBracket': '\u27E8',
  13734. 'lang': '\u27E8',
  13735. 'langle': '\u27E8',
  13736. 'LeftArrow': '\u2190',
  13737. 'ShortLeftArrow': '\u2190',
  13738. 'larr': '\u2190',
  13739. 'leftarrow': '\u2190',
  13740. 'slarr': '\u2190',
  13741. 'LeftArrowBar': '\u21E4',
  13742. 'larrb': '\u21E4',
  13743. 'LeftArrowRightArrow': '\u21C6',
  13744. 'leftrightarrows': '\u21C6',
  13745. 'lrarr': '\u21C6',
  13746. 'LeftCeiling': '\u2308',
  13747. 'lceil': '\u2308',
  13748. 'LeftDoubleBracket': '\u27E6',
  13749. 'lobrk': '\u27E6',
  13750. 'LeftDownTeeVector': '\u2961',
  13751. 'LeftDownVector': '\u21C3',
  13752. 'dharl': '\u21C3',
  13753. 'downharpoonleft': '\u21C3',
  13754. 'LeftDownVectorBar': '\u2959',
  13755. 'LeftFloor': '\u230A',
  13756. 'lfloor': '\u230A',
  13757. 'LeftRightArrow': '\u2194',
  13758. 'harr': '\u2194',
  13759. 'leftrightarrow': '\u2194',
  13760. 'LeftRightVector': '\u294E',
  13761. 'LeftTee': '\u22A3',
  13762. 'dashv': '\u22A3',
  13763. 'LeftTeeArrow': '\u21A4',
  13764. 'mapstoleft': '\u21A4',
  13765. 'LeftTeeVector': '\u295A',
  13766. 'LeftTriangle': '\u22B2',
  13767. 'vartriangleleft': '\u22B2',
  13768. 'vltri': '\u22B2',
  13769. 'LeftTriangleBar': '\u29CF',
  13770. 'LeftTriangleEqual': '\u22B4',
  13771. 'ltrie': '\u22B4',
  13772. 'trianglelefteq': '\u22B4',
  13773. 'LeftUpDownVector': '\u2951',
  13774. 'LeftUpTeeVector': '\u2960',
  13775. 'LeftUpVector': '\u21BF',
  13776. 'uharl': '\u21BF',
  13777. 'upharpoonleft': '\u21BF',
  13778. 'LeftUpVectorBar': '\u2958',
  13779. 'LeftVector': '\u21BC',
  13780. 'leftharpoonup': '\u21BC',
  13781. 'lharu': '\u21BC',
  13782. 'LeftVectorBar': '\u2952',
  13783. 'LessEqualGreater': '\u22DA',
  13784. 'leg': '\u22DA',
  13785. 'lesseqgtr': '\u22DA',
  13786. 'LessFullEqual': '\u2266',
  13787. 'lE': '\u2266',
  13788. 'leqq': '\u2266',
  13789. 'LessGreater': '\u2276',
  13790. 'lessgtr': '\u2276',
  13791. 'lg': '\u2276',
  13792. 'LessLess': '\u2AA1',
  13793. 'LessSlantEqual': '\u2A7D',
  13794. 'leqslant': '\u2A7D',
  13795. 'les': '\u2A7D',
  13796. 'LessTilde': '\u2272',
  13797. 'lesssim': '\u2272',
  13798. 'lsim': '\u2272',
  13799. 'Lfr': '\uD835\uDD0F',
  13800. 'Ll': '\u22D8',
  13801. 'Lleftarrow': '\u21DA',
  13802. 'lAarr': '\u21DA',
  13803. 'Lmidot': '\u013F',
  13804. 'LongLeftArrow': '\u27F5',
  13805. 'longleftarrow': '\u27F5',
  13806. 'xlarr': '\u27F5',
  13807. 'LongLeftRightArrow': '\u27F7',
  13808. 'longleftrightarrow': '\u27F7',
  13809. 'xharr': '\u27F7',
  13810. 'LongRightArrow': '\u27F6',
  13811. 'longrightarrow': '\u27F6',
  13812. 'xrarr': '\u27F6',
  13813. 'Lopf': '\uD835\uDD43',
  13814. 'LowerLeftArrow': '\u2199',
  13815. 'swarr': '\u2199',
  13816. 'swarrow': '\u2199',
  13817. 'LowerRightArrow': '\u2198',
  13818. 'searr': '\u2198',
  13819. 'searrow': '\u2198',
  13820. 'Lsh': '\u21B0',
  13821. 'lsh': '\u21B0',
  13822. 'Lstrok': '\u0141',
  13823. 'Lt': '\u226A',
  13824. 'NestedLessLess': '\u226A',
  13825. 'll': '\u226A',
  13826. 'Map': '\u2905',
  13827. 'Mcy': '\u041C',
  13828. 'MediumSpace': '\u205F',
  13829. 'Mellintrf': '\u2133',
  13830. 'Mscr': '\u2133',
  13831. 'phmmat': '\u2133',
  13832. 'Mfr': '\uD835\uDD10',
  13833. 'MinusPlus': '\u2213',
  13834. 'mnplus': '\u2213',
  13835. 'mp': '\u2213',
  13836. 'Mopf': '\uD835\uDD44',
  13837. 'Mu': '\u039C',
  13838. 'NJcy': '\u040A',
  13839. 'Nacute': '\u0143',
  13840. 'Ncaron': '\u0147',
  13841. 'Ncedil': '\u0145',
  13842. 'Ncy': '\u041D',
  13843. 'NegativeMediumSpace': '\u200B',
  13844. 'NegativeThickSpace': '\u200B',
  13845. 'NegativeThinSpace': '\u200B',
  13846. 'NegativeVeryThinSpace': '\u200B',
  13847. 'ZeroWidthSpace': '\u200B',
  13848. 'NewLine': '\u000A',
  13849. 'Nfr': '\uD835\uDD11',
  13850. 'NoBreak': '\u2060',
  13851. 'NonBreakingSpace': '\u00A0',
  13852. 'nbsp': '\u00A0',
  13853. 'Nopf': '\u2115',
  13854. 'naturals': '\u2115',
  13855. 'Not': '\u2AEC',
  13856. 'NotCongruent': '\u2262',
  13857. 'nequiv': '\u2262',
  13858. 'NotCupCap': '\u226D',
  13859. 'NotDoubleVerticalBar': '\u2226',
  13860. 'npar': '\u2226',
  13861. 'nparallel': '\u2226',
  13862. 'nshortparallel': '\u2226',
  13863. 'nspar': '\u2226',
  13864. 'NotElement': '\u2209',
  13865. 'notin': '\u2209',
  13866. 'notinva': '\u2209',
  13867. 'NotEqual': '\u2260',
  13868. 'ne': '\u2260',
  13869. 'NotEqualTilde': '\u2242\u0338',
  13870. 'nesim': '\u2242\u0338',
  13871. 'NotExists': '\u2204',
  13872. 'nexist': '\u2204',
  13873. 'nexists': '\u2204',
  13874. 'NotGreater': '\u226F',
  13875. 'ngt': '\u226F',
  13876. 'ngtr': '\u226F',
  13877. 'NotGreaterEqual': '\u2271',
  13878. 'nge': '\u2271',
  13879. 'ngeq': '\u2271',
  13880. 'NotGreaterFullEqual': '\u2267\u0338',
  13881. 'ngE': '\u2267\u0338',
  13882. 'ngeqq': '\u2267\u0338',
  13883. 'NotGreaterGreater': '\u226B\u0338',
  13884. 'nGtv': '\u226B\u0338',
  13885. 'NotGreaterLess': '\u2279',
  13886. 'ntgl': '\u2279',
  13887. 'NotGreaterSlantEqual': '\u2A7E\u0338',
  13888. 'ngeqslant': '\u2A7E\u0338',
  13889. 'nges': '\u2A7E\u0338',
  13890. 'NotGreaterTilde': '\u2275',
  13891. 'ngsim': '\u2275',
  13892. 'NotHumpDownHump': '\u224E\u0338',
  13893. 'nbump': '\u224E\u0338',
  13894. 'NotHumpEqual': '\u224F\u0338',
  13895. 'nbumpe': '\u224F\u0338',
  13896. 'NotLeftTriangle': '\u22EA',
  13897. 'nltri': '\u22EA',
  13898. 'ntriangleleft': '\u22EA',
  13899. 'NotLeftTriangleBar': '\u29CF\u0338',
  13900. 'NotLeftTriangleEqual': '\u22EC',
  13901. 'nltrie': '\u22EC',
  13902. 'ntrianglelefteq': '\u22EC',
  13903. 'NotLess': '\u226E',
  13904. 'nless': '\u226E',
  13905. 'nlt': '\u226E',
  13906. 'NotLessEqual': '\u2270',
  13907. 'nle': '\u2270',
  13908. 'nleq': '\u2270',
  13909. 'NotLessGreater': '\u2278',
  13910. 'ntlg': '\u2278',
  13911. 'NotLessLess': '\u226A\u0338',
  13912. 'nLtv': '\u226A\u0338',
  13913. 'NotLessSlantEqual': '\u2A7D\u0338',
  13914. 'nleqslant': '\u2A7D\u0338',
  13915. 'nles': '\u2A7D\u0338',
  13916. 'NotLessTilde': '\u2274',
  13917. 'nlsim': '\u2274',
  13918. 'NotNestedGreaterGreater': '\u2AA2\u0338',
  13919. 'NotNestedLessLess': '\u2AA1\u0338',
  13920. 'NotPrecedes': '\u2280',
  13921. 'npr': '\u2280',
  13922. 'nprec': '\u2280',
  13923. 'NotPrecedesEqual': '\u2AAF\u0338',
  13924. 'npre': '\u2AAF\u0338',
  13925. 'npreceq': '\u2AAF\u0338',
  13926. 'NotPrecedesSlantEqual': '\u22E0',
  13927. 'nprcue': '\u22E0',
  13928. 'NotReverseElement': '\u220C',
  13929. 'notni': '\u220C',
  13930. 'notniva': '\u220C',
  13931. 'NotRightTriangle': '\u22EB',
  13932. 'nrtri': '\u22EB',
  13933. 'ntriangleright': '\u22EB',
  13934. 'NotRightTriangleBar': '\u29D0\u0338',
  13935. 'NotRightTriangleEqual': '\u22ED',
  13936. 'nrtrie': '\u22ED',
  13937. 'ntrianglerighteq': '\u22ED',
  13938. 'NotSquareSubset': '\u228F\u0338',
  13939. 'NotSquareSubsetEqual': '\u22E2',
  13940. 'nsqsube': '\u22E2',
  13941. 'NotSquareSuperset': '\u2290\u0338',
  13942. 'NotSquareSupersetEqual': '\u22E3',
  13943. 'nsqsupe': '\u22E3',
  13944. 'NotSubset': '\u2282\u20D2',
  13945. 'nsubset': '\u2282\u20D2',
  13946. 'vnsub': '\u2282\u20D2',
  13947. 'NotSubsetEqual': '\u2288',
  13948. 'nsube': '\u2288',
  13949. 'nsubseteq': '\u2288',
  13950. 'NotSucceeds': '\u2281',
  13951. 'nsc': '\u2281',
  13952. 'nsucc': '\u2281',
  13953. 'NotSucceedsEqual': '\u2AB0\u0338',
  13954. 'nsce': '\u2AB0\u0338',
  13955. 'nsucceq': '\u2AB0\u0338',
  13956. 'NotSucceedsSlantEqual': '\u22E1',
  13957. 'nsccue': '\u22E1',
  13958. 'NotSucceedsTilde': '\u227F\u0338',
  13959. 'NotSuperset': '\u2283\u20D2',
  13960. 'nsupset': '\u2283\u20D2',
  13961. 'vnsup': '\u2283\u20D2',
  13962. 'NotSupersetEqual': '\u2289',
  13963. 'nsupe': '\u2289',
  13964. 'nsupseteq': '\u2289',
  13965. 'NotTilde': '\u2241',
  13966. 'nsim': '\u2241',
  13967. 'NotTildeEqual': '\u2244',
  13968. 'nsime': '\u2244',
  13969. 'nsimeq': '\u2244',
  13970. 'NotTildeFullEqual': '\u2247',
  13971. 'ncong': '\u2247',
  13972. 'NotTildeTilde': '\u2249',
  13973. 'nap': '\u2249',
  13974. 'napprox': '\u2249',
  13975. 'NotVerticalBar': '\u2224',
  13976. 'nmid': '\u2224',
  13977. 'nshortmid': '\u2224',
  13978. 'nsmid': '\u2224',
  13979. 'Nscr': '\uD835\uDCA9',
  13980. 'Ntilde': '\u00D1',
  13981. 'Nu': '\u039D',
  13982. 'OElig': '\u0152',
  13983. 'Oacute': '\u00D3',
  13984. 'Ocirc': '\u00D4',
  13985. 'Ocy': '\u041E',
  13986. 'Odblac': '\u0150',
  13987. 'Ofr': '\uD835\uDD12',
  13988. 'Ograve': '\u00D2',
  13989. 'Omacr': '\u014C',
  13990. 'Omega': '\u03A9',
  13991. 'ohm': '\u03A9',
  13992. 'Omicron': '\u039F',
  13993. 'Oopf': '\uD835\uDD46',
  13994. 'OpenCurlyDoubleQuote': '\u201C',
  13995. 'ldquo': '\u201C',
  13996. 'OpenCurlyQuote': '\u2018',
  13997. 'lsquo': '\u2018',
  13998. 'Or': '\u2A54',
  13999. 'Oscr': '\uD835\uDCAA',
  14000. 'Oslash': '\u00D8',
  14001. 'Otilde': '\u00D5',
  14002. 'Otimes': '\u2A37',
  14003. 'Ouml': '\u00D6',
  14004. 'OverBar': '\u203E',
  14005. 'oline': '\u203E',
  14006. 'OverBrace': '\u23DE',
  14007. 'OverBracket': '\u23B4',
  14008. 'tbrk': '\u23B4',
  14009. 'OverParenthesis': '\u23DC',
  14010. 'PartialD': '\u2202',
  14011. 'part': '\u2202',
  14012. 'Pcy': '\u041F',
  14013. 'Pfr': '\uD835\uDD13',
  14014. 'Phi': '\u03A6',
  14015. 'Pi': '\u03A0',
  14016. 'PlusMinus': '\u00B1',
  14017. 'plusmn': '\u00B1',
  14018. 'pm': '\u00B1',
  14019. 'Popf': '\u2119',
  14020. 'primes': '\u2119',
  14021. 'Pr': '\u2ABB',
  14022. 'Precedes': '\u227A',
  14023. 'pr': '\u227A',
  14024. 'prec': '\u227A',
  14025. 'PrecedesEqual': '\u2AAF',
  14026. 'pre': '\u2AAF',
  14027. 'preceq': '\u2AAF',
  14028. 'PrecedesSlantEqual': '\u227C',
  14029. 'prcue': '\u227C',
  14030. 'preccurlyeq': '\u227C',
  14031. 'PrecedesTilde': '\u227E',
  14032. 'precsim': '\u227E',
  14033. 'prsim': '\u227E',
  14034. 'Prime': '\u2033',
  14035. 'Product': '\u220F',
  14036. 'prod': '\u220F',
  14037. 'Proportional': '\u221D',
  14038. 'prop': '\u221D',
  14039. 'propto': '\u221D',
  14040. 'varpropto': '\u221D',
  14041. 'vprop': '\u221D',
  14042. 'Pscr': '\uD835\uDCAB',
  14043. 'Psi': '\u03A8',
  14044. 'QUOT': '\u0022',
  14045. 'quot': '\u0022',
  14046. 'Qfr': '\uD835\uDD14',
  14047. 'Qopf': '\u211A',
  14048. 'rationals': '\u211A',
  14049. 'Qscr': '\uD835\uDCAC',
  14050. 'RBarr': '\u2910',
  14051. 'drbkarow': '\u2910',
  14052. 'REG': '\u00AE',
  14053. 'circledR': '\u00AE',
  14054. 'reg': '\u00AE',
  14055. 'Racute': '\u0154',
  14056. 'Rang': '\u27EB',
  14057. 'Rarr': '\u21A0',
  14058. 'twoheadrightarrow': '\u21A0',
  14059. 'Rarrtl': '\u2916',
  14060. 'Rcaron': '\u0158',
  14061. 'Rcedil': '\u0156',
  14062. 'Rcy': '\u0420',
  14063. 'Re': '\u211C',
  14064. 'Rfr': '\u211C',
  14065. 'real': '\u211C',
  14066. 'realpart': '\u211C',
  14067. 'ReverseElement': '\u220B',
  14068. 'SuchThat': '\u220B',
  14069. 'ni': '\u220B',
  14070. 'niv': '\u220B',
  14071. 'ReverseEquilibrium': '\u21CB',
  14072. 'leftrightharpoons': '\u21CB',
  14073. 'lrhar': '\u21CB',
  14074. 'ReverseUpEquilibrium': '\u296F',
  14075. 'duhar': '\u296F',
  14076. 'Rho': '\u03A1',
  14077. 'RightAngleBracket': '\u27E9',
  14078. 'rang': '\u27E9',
  14079. 'rangle': '\u27E9',
  14080. 'RightArrow': '\u2192',
  14081. 'ShortRightArrow': '\u2192',
  14082. 'rarr': '\u2192',
  14083. 'rightarrow': '\u2192',
  14084. 'srarr': '\u2192',
  14085. 'RightArrowBar': '\u21E5',
  14086. 'rarrb': '\u21E5',
  14087. 'RightArrowLeftArrow': '\u21C4',
  14088. 'rightleftarrows': '\u21C4',
  14089. 'rlarr': '\u21C4',
  14090. 'RightCeiling': '\u2309',
  14091. 'rceil': '\u2309',
  14092. 'RightDoubleBracket': '\u27E7',
  14093. 'robrk': '\u27E7',
  14094. 'RightDownTeeVector': '\u295D',
  14095. 'RightDownVector': '\u21C2',
  14096. 'dharr': '\u21C2',
  14097. 'downharpoonright': '\u21C2',
  14098. 'RightDownVectorBar': '\u2955',
  14099. 'RightFloor': '\u230B',
  14100. 'rfloor': '\u230B',
  14101. 'RightTee': '\u22A2',
  14102. 'vdash': '\u22A2',
  14103. 'RightTeeArrow': '\u21A6',
  14104. 'map': '\u21A6',
  14105. 'mapsto': '\u21A6',
  14106. 'RightTeeVector': '\u295B',
  14107. 'RightTriangle': '\u22B3',
  14108. 'vartriangleright': '\u22B3',
  14109. 'vrtri': '\u22B3',
  14110. 'RightTriangleBar': '\u29D0',
  14111. 'RightTriangleEqual': '\u22B5',
  14112. 'rtrie': '\u22B5',
  14113. 'trianglerighteq': '\u22B5',
  14114. 'RightUpDownVector': '\u294F',
  14115. 'RightUpTeeVector': '\u295C',
  14116. 'RightUpVector': '\u21BE',
  14117. 'uharr': '\u21BE',
  14118. 'upharpoonright': '\u21BE',
  14119. 'RightUpVectorBar': '\u2954',
  14120. 'RightVector': '\u21C0',
  14121. 'rharu': '\u21C0',
  14122. 'rightharpoonup': '\u21C0',
  14123. 'RightVectorBar': '\u2953',
  14124. 'Ropf': '\u211D',
  14125. 'reals': '\u211D',
  14126. 'RoundImplies': '\u2970',
  14127. 'Rrightarrow': '\u21DB',
  14128. 'rAarr': '\u21DB',
  14129. 'Rscr': '\u211B',
  14130. 'realine': '\u211B',
  14131. 'Rsh': '\u21B1',
  14132. 'rsh': '\u21B1',
  14133. 'RuleDelayed': '\u29F4',
  14134. 'SHCHcy': '\u0429',
  14135. 'SHcy': '\u0428',
  14136. 'SOFTcy': '\u042C',
  14137. 'Sacute': '\u015A',
  14138. 'Sc': '\u2ABC',
  14139. 'Scaron': '\u0160',
  14140. 'Scedil': '\u015E',
  14141. 'Scirc': '\u015C',
  14142. 'Scy': '\u0421',
  14143. 'Sfr': '\uD835\uDD16',
  14144. 'ShortUpArrow': '\u2191',
  14145. 'UpArrow': '\u2191',
  14146. 'uarr': '\u2191',
  14147. 'uparrow': '\u2191',
  14148. 'Sigma': '\u03A3',
  14149. 'SmallCircle': '\u2218',
  14150. 'compfn': '\u2218',
  14151. 'Sopf': '\uD835\uDD4A',
  14152. 'Sqrt': '\u221A',
  14153. 'radic': '\u221A',
  14154. 'Square': '\u25A1',
  14155. 'squ': '\u25A1',
  14156. 'square': '\u25A1',
  14157. 'SquareIntersection': '\u2293',
  14158. 'sqcap': '\u2293',
  14159. 'SquareSubset': '\u228F',
  14160. 'sqsub': '\u228F',
  14161. 'sqsubset': '\u228F',
  14162. 'SquareSubsetEqual': '\u2291',
  14163. 'sqsube': '\u2291',
  14164. 'sqsubseteq': '\u2291',
  14165. 'SquareSuperset': '\u2290',
  14166. 'sqsup': '\u2290',
  14167. 'sqsupset': '\u2290',
  14168. 'SquareSupersetEqual': '\u2292',
  14169. 'sqsupe': '\u2292',
  14170. 'sqsupseteq': '\u2292',
  14171. 'SquareUnion': '\u2294',
  14172. 'sqcup': '\u2294',
  14173. 'Sscr': '\uD835\uDCAE',
  14174. 'Star': '\u22C6',
  14175. 'sstarf': '\u22C6',
  14176. 'Sub': '\u22D0',
  14177. 'Subset': '\u22D0',
  14178. 'SubsetEqual': '\u2286',
  14179. 'sube': '\u2286',
  14180. 'subseteq': '\u2286',
  14181. 'Succeeds': '\u227B',
  14182. 'sc': '\u227B',
  14183. 'succ': '\u227B',
  14184. 'SucceedsEqual': '\u2AB0',
  14185. 'sce': '\u2AB0',
  14186. 'succeq': '\u2AB0',
  14187. 'SucceedsSlantEqual': '\u227D',
  14188. 'sccue': '\u227D',
  14189. 'succcurlyeq': '\u227D',
  14190. 'SucceedsTilde': '\u227F',
  14191. 'scsim': '\u227F',
  14192. 'succsim': '\u227F',
  14193. 'Sum': '\u2211',
  14194. 'sum': '\u2211',
  14195. 'Sup': '\u22D1',
  14196. 'Supset': '\u22D1',
  14197. 'Superset': '\u2283',
  14198. 'sup': '\u2283',
  14199. 'supset': '\u2283',
  14200. 'SupersetEqual': '\u2287',
  14201. 'supe': '\u2287',
  14202. 'supseteq': '\u2287',
  14203. 'THORN': '\u00DE',
  14204. 'TRADE': '\u2122',
  14205. 'trade': '\u2122',
  14206. 'TSHcy': '\u040B',
  14207. 'TScy': '\u0426',
  14208. 'Tab': '\u0009',
  14209. 'Tau': '\u03A4',
  14210. 'Tcaron': '\u0164',
  14211. 'Tcedil': '\u0162',
  14212. 'Tcy': '\u0422',
  14213. 'Tfr': '\uD835\uDD17',
  14214. 'Therefore': '\u2234',
  14215. 'there4': '\u2234',
  14216. 'therefore': '\u2234',
  14217. 'Theta': '\u0398',
  14218. 'ThickSpace': '\u205F\u200A',
  14219. 'ThinSpace': '\u2009',
  14220. 'thinsp': '\u2009',
  14221. 'Tilde': '\u223C',
  14222. 'sim': '\u223C',
  14223. 'thicksim': '\u223C',
  14224. 'thksim': '\u223C',
  14225. 'TildeEqual': '\u2243',
  14226. 'sime': '\u2243',
  14227. 'simeq': '\u2243',
  14228. 'TildeFullEqual': '\u2245',
  14229. 'cong': '\u2245',
  14230. 'TildeTilde': '\u2248',
  14231. 'ap': '\u2248',
  14232. 'approx': '\u2248',
  14233. 'asymp': '\u2248',
  14234. 'thickapprox': '\u2248',
  14235. 'thkap': '\u2248',
  14236. 'Topf': '\uD835\uDD4B',
  14237. 'TripleDot': '\u20DB',
  14238. 'tdot': '\u20DB',
  14239. 'Tscr': '\uD835\uDCAF',
  14240. 'Tstrok': '\u0166',
  14241. 'Uacute': '\u00DA',
  14242. 'Uarr': '\u219F',
  14243. 'Uarrocir': '\u2949',
  14244. 'Ubrcy': '\u040E',
  14245. 'Ubreve': '\u016C',
  14246. 'Ucirc': '\u00DB',
  14247. 'Ucy': '\u0423',
  14248. 'Udblac': '\u0170',
  14249. 'Ufr': '\uD835\uDD18',
  14250. 'Ugrave': '\u00D9',
  14251. 'Umacr': '\u016A',
  14252. 'UnderBar': '\u005F',
  14253. 'lowbar': '\u005F',
  14254. 'UnderBrace': '\u23DF',
  14255. 'UnderBracket': '\u23B5',
  14256. 'bbrk': '\u23B5',
  14257. 'UnderParenthesis': '\u23DD',
  14258. 'Union': '\u22C3',
  14259. 'bigcup': '\u22C3',
  14260. 'xcup': '\u22C3',
  14261. 'UnionPlus': '\u228E',
  14262. 'uplus': '\u228E',
  14263. 'Uogon': '\u0172',
  14264. 'Uopf': '\uD835\uDD4C',
  14265. 'UpArrowBar': '\u2912',
  14266. 'UpArrowDownArrow': '\u21C5',
  14267. 'udarr': '\u21C5',
  14268. 'UpDownArrow': '\u2195',
  14269. 'updownarrow': '\u2195',
  14270. 'varr': '\u2195',
  14271. 'UpEquilibrium': '\u296E',
  14272. 'udhar': '\u296E',
  14273. 'UpTee': '\u22A5',
  14274. 'bot': '\u22A5',
  14275. 'bottom': '\u22A5',
  14276. 'perp': '\u22A5',
  14277. 'UpTeeArrow': '\u21A5',
  14278. 'mapstoup': '\u21A5',
  14279. 'UpperLeftArrow': '\u2196',
  14280. 'nwarr': '\u2196',
  14281. 'nwarrow': '\u2196',
  14282. 'UpperRightArrow': '\u2197',
  14283. 'nearr': '\u2197',
  14284. 'nearrow': '\u2197',
  14285. 'Upsi': '\u03D2',
  14286. 'upsih': '\u03D2',
  14287. 'Upsilon': '\u03A5',
  14288. 'Uring': '\u016E',
  14289. 'Uscr': '\uD835\uDCB0',
  14290. 'Utilde': '\u0168',
  14291. 'Uuml': '\u00DC',
  14292. 'VDash': '\u22AB',
  14293. 'Vbar': '\u2AEB',
  14294. 'Vcy': '\u0412',
  14295. 'Vdash': '\u22A9',
  14296. 'Vdashl': '\u2AE6',
  14297. 'Vee': '\u22C1',
  14298. 'bigvee': '\u22C1',
  14299. 'xvee': '\u22C1',
  14300. 'Verbar': '\u2016',
  14301. 'Vert': '\u2016',
  14302. 'VerticalBar': '\u2223',
  14303. 'mid': '\u2223',
  14304. 'shortmid': '\u2223',
  14305. 'smid': '\u2223',
  14306. 'VerticalLine': '\u007C',
  14307. 'verbar': '\u007C',
  14308. 'vert': '\u007C',
  14309. 'VerticalSeparator': '\u2758',
  14310. 'VerticalTilde': '\u2240',
  14311. 'wr': '\u2240',
  14312. 'wreath': '\u2240',
  14313. 'VeryThinSpace': '\u200A',
  14314. 'hairsp': '\u200A',
  14315. 'Vfr': '\uD835\uDD19',
  14316. 'Vopf': '\uD835\uDD4D',
  14317. 'Vscr': '\uD835\uDCB1',
  14318. 'Vvdash': '\u22AA',
  14319. 'Wcirc': '\u0174',
  14320. 'Wedge': '\u22C0',
  14321. 'bigwedge': '\u22C0',
  14322. 'xwedge': '\u22C0',
  14323. 'Wfr': '\uD835\uDD1A',
  14324. 'Wopf': '\uD835\uDD4E',
  14325. 'Wscr': '\uD835\uDCB2',
  14326. 'Xfr': '\uD835\uDD1B',
  14327. 'Xi': '\u039E',
  14328. 'Xopf': '\uD835\uDD4F',
  14329. 'Xscr': '\uD835\uDCB3',
  14330. 'YAcy': '\u042F',
  14331. 'YIcy': '\u0407',
  14332. 'YUcy': '\u042E',
  14333. 'Yacute': '\u00DD',
  14334. 'Ycirc': '\u0176',
  14335. 'Ycy': '\u042B',
  14336. 'Yfr': '\uD835\uDD1C',
  14337. 'Yopf': '\uD835\uDD50',
  14338. 'Yscr': '\uD835\uDCB4',
  14339. 'Yuml': '\u0178',
  14340. 'ZHcy': '\u0416',
  14341. 'Zacute': '\u0179',
  14342. 'Zcaron': '\u017D',
  14343. 'Zcy': '\u0417',
  14344. 'Zdot': '\u017B',
  14345. 'Zeta': '\u0396',
  14346. 'Zfr': '\u2128',
  14347. 'zeetrf': '\u2128',
  14348. 'Zopf': '\u2124',
  14349. 'integers': '\u2124',
  14350. 'Zscr': '\uD835\uDCB5',
  14351. 'aacute': '\u00E1',
  14352. 'abreve': '\u0103',
  14353. 'ac': '\u223E',
  14354. 'mstpos': '\u223E',
  14355. 'acE': '\u223E\u0333',
  14356. 'acd': '\u223F',
  14357. 'acirc': '\u00E2',
  14358. 'acy': '\u0430',
  14359. 'aelig': '\u00E6',
  14360. 'afr': '\uD835\uDD1E',
  14361. 'agrave': '\u00E0',
  14362. 'alefsym': '\u2135',
  14363. 'aleph': '\u2135',
  14364. 'alpha': '\u03B1',
  14365. 'amacr': '\u0101',
  14366. 'amalg': '\u2A3F',
  14367. 'and': '\u2227',
  14368. 'wedge': '\u2227',
  14369. 'andand': '\u2A55',
  14370. 'andd': '\u2A5C',
  14371. 'andslope': '\u2A58',
  14372. 'andv': '\u2A5A',
  14373. 'ang': '\u2220',
  14374. 'angle': '\u2220',
  14375. 'ange': '\u29A4',
  14376. 'angmsd': '\u2221',
  14377. 'measuredangle': '\u2221',
  14378. 'angmsdaa': '\u29A8',
  14379. 'angmsdab': '\u29A9',
  14380. 'angmsdac': '\u29AA',
  14381. 'angmsdad': '\u29AB',
  14382. 'angmsdae': '\u29AC',
  14383. 'angmsdaf': '\u29AD',
  14384. 'angmsdag': '\u29AE',
  14385. 'angmsdah': '\u29AF',
  14386. 'angrt': '\u221F',
  14387. 'angrtvb': '\u22BE',
  14388. 'angrtvbd': '\u299D',
  14389. 'angsph': '\u2222',
  14390. 'angzarr': '\u237C',
  14391. 'aogon': '\u0105',
  14392. 'aopf': '\uD835\uDD52',
  14393. 'apE': '\u2A70',
  14394. 'apacir': '\u2A6F',
  14395. 'ape': '\u224A',
  14396. 'approxeq': '\u224A',
  14397. 'apid': '\u224B',
  14398. 'apos': '\u0027',
  14399. 'aring': '\u00E5',
  14400. 'ascr': '\uD835\uDCB6',
  14401. 'ast': '\u002A',
  14402. 'midast': '\u002A',
  14403. 'atilde': '\u00E3',
  14404. 'auml': '\u00E4',
  14405. 'awint': '\u2A11',
  14406. 'bNot': '\u2AED',
  14407. 'backcong': '\u224C',
  14408. 'bcong': '\u224C',
  14409. 'backepsilon': '\u03F6',
  14410. 'bepsi': '\u03F6',
  14411. 'backprime': '\u2035',
  14412. 'bprime': '\u2035',
  14413. 'backsim': '\u223D',
  14414. 'bsim': '\u223D',
  14415. 'backsimeq': '\u22CD',
  14416. 'bsime': '\u22CD',
  14417. 'barvee': '\u22BD',
  14418. 'barwed': '\u2305',
  14419. 'barwedge': '\u2305',
  14420. 'bbrktbrk': '\u23B6',
  14421. 'bcy': '\u0431',
  14422. 'bdquo': '\u201E',
  14423. 'ldquor': '\u201E',
  14424. 'bemptyv': '\u29B0',
  14425. 'beta': '\u03B2',
  14426. 'beth': '\u2136',
  14427. 'between': '\u226C',
  14428. 'twixt': '\u226C',
  14429. 'bfr': '\uD835\uDD1F',
  14430. 'bigcirc': '\u25EF',
  14431. 'xcirc': '\u25EF',
  14432. 'bigodot': '\u2A00',
  14433. 'xodot': '\u2A00',
  14434. 'bigoplus': '\u2A01',
  14435. 'xoplus': '\u2A01',
  14436. 'bigotimes': '\u2A02',
  14437. 'xotime': '\u2A02',
  14438. 'bigsqcup': '\u2A06',
  14439. 'xsqcup': '\u2A06',
  14440. 'bigstar': '\u2605',
  14441. 'starf': '\u2605',
  14442. 'bigtriangledown': '\u25BD',
  14443. 'xdtri': '\u25BD',
  14444. 'bigtriangleup': '\u25B3',
  14445. 'xutri': '\u25B3',
  14446. 'biguplus': '\u2A04',
  14447. 'xuplus': '\u2A04',
  14448. 'bkarow': '\u290D',
  14449. 'rbarr': '\u290D',
  14450. 'blacklozenge': '\u29EB',
  14451. 'lozf': '\u29EB',
  14452. 'blacktriangle': '\u25B4',
  14453. 'utrif': '\u25B4',
  14454. 'blacktriangledown': '\u25BE',
  14455. 'dtrif': '\u25BE',
  14456. 'blacktriangleleft': '\u25C2',
  14457. 'ltrif': '\u25C2',
  14458. 'blacktriangleright': '\u25B8',
  14459. 'rtrif': '\u25B8',
  14460. 'blank': '\u2423',
  14461. 'blk12': '\u2592',
  14462. 'blk14': '\u2591',
  14463. 'blk34': '\u2593',
  14464. 'block': '\u2588',
  14465. 'bne': '\u003D\u20E5',
  14466. 'bnequiv': '\u2261\u20E5',
  14467. 'bnot': '\u2310',
  14468. 'bopf': '\uD835\uDD53',
  14469. 'bowtie': '\u22C8',
  14470. 'boxDL': '\u2557',
  14471. 'boxDR': '\u2554',
  14472. 'boxDl': '\u2556',
  14473. 'boxDr': '\u2553',
  14474. 'boxH': '\u2550',
  14475. 'boxHD': '\u2566',
  14476. 'boxHU': '\u2569',
  14477. 'boxHd': '\u2564',
  14478. 'boxHu': '\u2567',
  14479. 'boxUL': '\u255D',
  14480. 'boxUR': '\u255A',
  14481. 'boxUl': '\u255C',
  14482. 'boxUr': '\u2559',
  14483. 'boxV': '\u2551',
  14484. 'boxVH': '\u256C',
  14485. 'boxVL': '\u2563',
  14486. 'boxVR': '\u2560',
  14487. 'boxVh': '\u256B',
  14488. 'boxVl': '\u2562',
  14489. 'boxVr': '\u255F',
  14490. 'boxbox': '\u29C9',
  14491. 'boxdL': '\u2555',
  14492. 'boxdR': '\u2552',
  14493. 'boxdl': '\u2510',
  14494. 'boxdr': '\u250C',
  14495. 'boxhD': '\u2565',
  14496. 'boxhU': '\u2568',
  14497. 'boxhd': '\u252C',
  14498. 'boxhu': '\u2534',
  14499. 'boxminus': '\u229F',
  14500. 'minusb': '\u229F',
  14501. 'boxplus': '\u229E',
  14502. 'plusb': '\u229E',
  14503. 'boxtimes': '\u22A0',
  14504. 'timesb': '\u22A0',
  14505. 'boxuL': '\u255B',
  14506. 'boxuR': '\u2558',
  14507. 'boxul': '\u2518',
  14508. 'boxur': '\u2514',
  14509. 'boxv': '\u2502',
  14510. 'boxvH': '\u256A',
  14511. 'boxvL': '\u2561',
  14512. 'boxvR': '\u255E',
  14513. 'boxvh': '\u253C',
  14514. 'boxvl': '\u2524',
  14515. 'boxvr': '\u251C',
  14516. 'brvbar': '\u00A6',
  14517. 'bscr': '\uD835\uDCB7',
  14518. 'bsemi': '\u204F',
  14519. 'bsol': '\u005C',
  14520. 'bsolb': '\u29C5',
  14521. 'bsolhsub': '\u27C8',
  14522. 'bull': '\u2022',
  14523. 'bullet': '\u2022',
  14524. 'bumpE': '\u2AAE',
  14525. 'cacute': '\u0107',
  14526. 'cap': '\u2229',
  14527. 'capand': '\u2A44',
  14528. 'capbrcup': '\u2A49',
  14529. 'capcap': '\u2A4B',
  14530. 'capcup': '\u2A47',
  14531. 'capdot': '\u2A40',
  14532. 'caps': '\u2229\uFE00',
  14533. 'caret': '\u2041',
  14534. 'ccaps': '\u2A4D',
  14535. 'ccaron': '\u010D',
  14536. 'ccedil': '\u00E7',
  14537. 'ccirc': '\u0109',
  14538. 'ccups': '\u2A4C',
  14539. 'ccupssm': '\u2A50',
  14540. 'cdot': '\u010B',
  14541. 'cemptyv': '\u29B2',
  14542. 'cent': '\u00A2',
  14543. 'cfr': '\uD835\uDD20',
  14544. 'chcy': '\u0447',
  14545. 'check': '\u2713',
  14546. 'checkmark': '\u2713',
  14547. 'chi': '\u03C7',
  14548. 'cir': '\u25CB',
  14549. 'cirE': '\u29C3',
  14550. 'circ': '\u02C6',
  14551. 'circeq': '\u2257',
  14552. 'cire': '\u2257',
  14553. 'circlearrowleft': '\u21BA',
  14554. 'olarr': '\u21BA',
  14555. 'circlearrowright': '\u21BB',
  14556. 'orarr': '\u21BB',
  14557. 'circledS': '\u24C8',
  14558. 'oS': '\u24C8',
  14559. 'circledast': '\u229B',
  14560. 'oast': '\u229B',
  14561. 'circledcirc': '\u229A',
  14562. 'ocir': '\u229A',
  14563. 'circleddash': '\u229D',
  14564. 'odash': '\u229D',
  14565. 'cirfnint': '\u2A10',
  14566. 'cirmid': '\u2AEF',
  14567. 'cirscir': '\u29C2',
  14568. 'clubs': '\u2663',
  14569. 'clubsuit': '\u2663',
  14570. 'colon': '\u003A',
  14571. 'comma': '\u002C',
  14572. 'commat': '\u0040',
  14573. 'comp': '\u2201',
  14574. 'complement': '\u2201',
  14575. 'congdot': '\u2A6D',
  14576. 'copf': '\uD835\uDD54',
  14577. 'copysr': '\u2117',
  14578. 'crarr': '\u21B5',
  14579. 'cross': '\u2717',
  14580. 'cscr': '\uD835\uDCB8',
  14581. 'csub': '\u2ACF',
  14582. 'csube': '\u2AD1',
  14583. 'csup': '\u2AD0',
  14584. 'csupe': '\u2AD2',
  14585. 'ctdot': '\u22EF',
  14586. 'cudarrl': '\u2938',
  14587. 'cudarrr': '\u2935',
  14588. 'cuepr': '\u22DE',
  14589. 'curlyeqprec': '\u22DE',
  14590. 'cuesc': '\u22DF',
  14591. 'curlyeqsucc': '\u22DF',
  14592. 'cularr': '\u21B6',
  14593. 'curvearrowleft': '\u21B6',
  14594. 'cularrp': '\u293D',
  14595. 'cup': '\u222A',
  14596. 'cupbrcap': '\u2A48',
  14597. 'cupcap': '\u2A46',
  14598. 'cupcup': '\u2A4A',
  14599. 'cupdot': '\u228D',
  14600. 'cupor': '\u2A45',
  14601. 'cups': '\u222A\uFE00',
  14602. 'curarr': '\u21B7',
  14603. 'curvearrowright': '\u21B7',
  14604. 'curarrm': '\u293C',
  14605. 'curlyvee': '\u22CE',
  14606. 'cuvee': '\u22CE',
  14607. 'curlywedge': '\u22CF',
  14608. 'cuwed': '\u22CF',
  14609. 'curren': '\u00A4',
  14610. 'cwint': '\u2231',
  14611. 'cylcty': '\u232D',
  14612. 'dHar': '\u2965',
  14613. 'dagger': '\u2020',
  14614. 'daleth': '\u2138',
  14615. 'dash': '\u2010',
  14616. 'hyphen': '\u2010',
  14617. 'dbkarow': '\u290F',
  14618. 'rBarr': '\u290F',
  14619. 'dcaron': '\u010F',
  14620. 'dcy': '\u0434',
  14621. 'ddarr': '\u21CA',
  14622. 'downdownarrows': '\u21CA',
  14623. 'ddotseq': '\u2A77',
  14624. 'eDDot': '\u2A77',
  14625. 'deg': '\u00B0',
  14626. 'delta': '\u03B4',
  14627. 'demptyv': '\u29B1',
  14628. 'dfisht': '\u297F',
  14629. 'dfr': '\uD835\uDD21',
  14630. 'diamondsuit': '\u2666',
  14631. 'diams': '\u2666',
  14632. 'digamma': '\u03DD',
  14633. 'gammad': '\u03DD',
  14634. 'disin': '\u22F2',
  14635. 'div': '\u00F7',
  14636. 'divide': '\u00F7',
  14637. 'divideontimes': '\u22C7',
  14638. 'divonx': '\u22C7',
  14639. 'djcy': '\u0452',
  14640. 'dlcorn': '\u231E',
  14641. 'llcorner': '\u231E',
  14642. 'dlcrop': '\u230D',
  14643. 'dollar': '\u0024',
  14644. 'dopf': '\uD835\uDD55',
  14645. 'doteqdot': '\u2251',
  14646. 'eDot': '\u2251',
  14647. 'dotminus': '\u2238',
  14648. 'minusd': '\u2238',
  14649. 'dotplus': '\u2214',
  14650. 'plusdo': '\u2214',
  14651. 'dotsquare': '\u22A1',
  14652. 'sdotb': '\u22A1',
  14653. 'drcorn': '\u231F',
  14654. 'lrcorner': '\u231F',
  14655. 'drcrop': '\u230C',
  14656. 'dscr': '\uD835\uDCB9',
  14657. 'dscy': '\u0455',
  14658. 'dsol': '\u29F6',
  14659. 'dstrok': '\u0111',
  14660. 'dtdot': '\u22F1',
  14661. 'dtri': '\u25BF',
  14662. 'triangledown': '\u25BF',
  14663. 'dwangle': '\u29A6',
  14664. 'dzcy': '\u045F',
  14665. 'dzigrarr': '\u27FF',
  14666. 'eacute': '\u00E9',
  14667. 'easter': '\u2A6E',
  14668. 'ecaron': '\u011B',
  14669. 'ecir': '\u2256',
  14670. 'eqcirc': '\u2256',
  14671. 'ecirc': '\u00EA',
  14672. 'ecolon': '\u2255',
  14673. 'eqcolon': '\u2255',
  14674. 'ecy': '\u044D',
  14675. 'edot': '\u0117',
  14676. 'efDot': '\u2252',
  14677. 'fallingdotseq': '\u2252',
  14678. 'efr': '\uD835\uDD22',
  14679. 'eg': '\u2A9A',
  14680. 'egrave': '\u00E8',
  14681. 'egs': '\u2A96',
  14682. 'eqslantgtr': '\u2A96',
  14683. 'egsdot': '\u2A98',
  14684. 'el': '\u2A99',
  14685. 'elinters': '\u23E7',
  14686. 'ell': '\u2113',
  14687. 'els': '\u2A95',
  14688. 'eqslantless': '\u2A95',
  14689. 'elsdot': '\u2A97',
  14690. 'emacr': '\u0113',
  14691. 'empty': '\u2205',
  14692. 'emptyset': '\u2205',
  14693. 'emptyv': '\u2205',
  14694. 'varnothing': '\u2205',
  14695. 'emsp13': '\u2004',
  14696. 'emsp14': '\u2005',
  14697. 'emsp': '\u2003',
  14698. 'eng': '\u014B',
  14699. 'ensp': '\u2002',
  14700. 'eogon': '\u0119',
  14701. 'eopf': '\uD835\uDD56',
  14702. 'epar': '\u22D5',
  14703. 'eparsl': '\u29E3',
  14704. 'eplus': '\u2A71',
  14705. 'epsi': '\u03B5',
  14706. 'epsilon': '\u03B5',
  14707. 'epsiv': '\u03F5',
  14708. 'straightepsilon': '\u03F5',
  14709. 'varepsilon': '\u03F5',
  14710. 'equals': '\u003D',
  14711. 'equest': '\u225F',
  14712. 'questeq': '\u225F',
  14713. 'equivDD': '\u2A78',
  14714. 'eqvparsl': '\u29E5',
  14715. 'erDot': '\u2253',
  14716. 'risingdotseq': '\u2253',
  14717. 'erarr': '\u2971',
  14718. 'escr': '\u212F',
  14719. 'eta': '\u03B7',
  14720. 'eth': '\u00F0',
  14721. 'euml': '\u00EB',
  14722. 'euro': '\u20AC',
  14723. 'excl': '\u0021',
  14724. 'fcy': '\u0444',
  14725. 'female': '\u2640',
  14726. 'ffilig': '\uFB03',
  14727. 'fflig': '\uFB00',
  14728. 'ffllig': '\uFB04',
  14729. 'ffr': '\uD835\uDD23',
  14730. 'filig': '\uFB01',
  14731. 'fjlig': '\u0066\u006A',
  14732. 'flat': '\u266D',
  14733. 'fllig': '\uFB02',
  14734. 'fltns': '\u25B1',
  14735. 'fnof': '\u0192',
  14736. 'fopf': '\uD835\uDD57',
  14737. 'fork': '\u22D4',
  14738. 'pitchfork': '\u22D4',
  14739. 'forkv': '\u2AD9',
  14740. 'fpartint': '\u2A0D',
  14741. 'frac12': '\u00BD',
  14742. 'half': '\u00BD',
  14743. 'frac13': '\u2153',
  14744. 'frac14': '\u00BC',
  14745. 'frac15': '\u2155',
  14746. 'frac16': '\u2159',
  14747. 'frac18': '\u215B',
  14748. 'frac23': '\u2154',
  14749. 'frac25': '\u2156',
  14750. 'frac34': '\u00BE',
  14751. 'frac35': '\u2157',
  14752. 'frac38': '\u215C',
  14753. 'frac45': '\u2158',
  14754. 'frac56': '\u215A',
  14755. 'frac58': '\u215D',
  14756. 'frac78': '\u215E',
  14757. 'frasl': '\u2044',
  14758. 'frown': '\u2322',
  14759. 'sfrown': '\u2322',
  14760. 'fscr': '\uD835\uDCBB',
  14761. 'gEl': '\u2A8C',
  14762. 'gtreqqless': '\u2A8C',
  14763. 'gacute': '\u01F5',
  14764. 'gamma': '\u03B3',
  14765. 'gap': '\u2A86',
  14766. 'gtrapprox': '\u2A86',
  14767. 'gbreve': '\u011F',
  14768. 'gcirc': '\u011D',
  14769. 'gcy': '\u0433',
  14770. 'gdot': '\u0121',
  14771. 'gescc': '\u2AA9',
  14772. 'gesdot': '\u2A80',
  14773. 'gesdoto': '\u2A82',
  14774. 'gesdotol': '\u2A84',
  14775. 'gesl': '\u22DB\uFE00',
  14776. 'gesles': '\u2A94',
  14777. 'gfr': '\uD835\uDD24',
  14778. 'gimel': '\u2137',
  14779. 'gjcy': '\u0453',
  14780. 'glE': '\u2A92',
  14781. 'gla': '\u2AA5',
  14782. 'glj': '\u2AA4',
  14783. 'gnE': '\u2269',
  14784. 'gneqq': '\u2269',
  14785. 'gnap': '\u2A8A',
  14786. 'gnapprox': '\u2A8A',
  14787. 'gne': '\u2A88',
  14788. 'gneq': '\u2A88',
  14789. 'gnsim': '\u22E7',
  14790. 'gopf': '\uD835\uDD58',
  14791. 'gscr': '\u210A',
  14792. 'gsime': '\u2A8E',
  14793. 'gsiml': '\u2A90',
  14794. 'gtcc': '\u2AA7',
  14795. 'gtcir': '\u2A7A',
  14796. 'gtdot': '\u22D7',
  14797. 'gtrdot': '\u22D7',
  14798. 'gtlPar': '\u2995',
  14799. 'gtquest': '\u2A7C',
  14800. 'gtrarr': '\u2978',
  14801. 'gvertneqq': '\u2269\uFE00',
  14802. 'gvnE': '\u2269\uFE00',
  14803. 'hardcy': '\u044A',
  14804. 'harrcir': '\u2948',
  14805. 'harrw': '\u21AD',
  14806. 'leftrightsquigarrow': '\u21AD',
  14807. 'hbar': '\u210F',
  14808. 'hslash': '\u210F',
  14809. 'planck': '\u210F',
  14810. 'plankv': '\u210F',
  14811. 'hcirc': '\u0125',
  14812. 'hearts': '\u2665',
  14813. 'heartsuit': '\u2665',
  14814. 'hellip': '\u2026',
  14815. 'mldr': '\u2026',
  14816. 'hercon': '\u22B9',
  14817. 'hfr': '\uD835\uDD25',
  14818. 'hksearow': '\u2925',
  14819. 'searhk': '\u2925',
  14820. 'hkswarow': '\u2926',
  14821. 'swarhk': '\u2926',
  14822. 'hoarr': '\u21FF',
  14823. 'homtht': '\u223B',
  14824. 'hookleftarrow': '\u21A9',
  14825. 'larrhk': '\u21A9',
  14826. 'hookrightarrow': '\u21AA',
  14827. 'rarrhk': '\u21AA',
  14828. 'hopf': '\uD835\uDD59',
  14829. 'horbar': '\u2015',
  14830. 'hscr': '\uD835\uDCBD',
  14831. 'hstrok': '\u0127',
  14832. 'hybull': '\u2043',
  14833. 'iacute': '\u00ED',
  14834. 'icirc': '\u00EE',
  14835. 'icy': '\u0438',
  14836. 'iecy': '\u0435',
  14837. 'iexcl': '\u00A1',
  14838. 'ifr': '\uD835\uDD26',
  14839. 'igrave': '\u00EC',
  14840. 'iiiint': '\u2A0C',
  14841. 'qint': '\u2A0C',
  14842. 'iiint': '\u222D',
  14843. 'tint': '\u222D',
  14844. 'iinfin': '\u29DC',
  14845. 'iiota': '\u2129',
  14846. 'ijlig': '\u0133',
  14847. 'imacr': '\u012B',
  14848. 'imath': '\u0131',
  14849. 'inodot': '\u0131',
  14850. 'imof': '\u22B7',
  14851. 'imped': '\u01B5',
  14852. 'incare': '\u2105',
  14853. 'infin': '\u221E',
  14854. 'infintie': '\u29DD',
  14855. 'intcal': '\u22BA',
  14856. 'intercal': '\u22BA',
  14857. 'intlarhk': '\u2A17',
  14858. 'intprod': '\u2A3C',
  14859. 'iprod': '\u2A3C',
  14860. 'iocy': '\u0451',
  14861. 'iogon': '\u012F',
  14862. 'iopf': '\uD835\uDD5A',
  14863. 'iota': '\u03B9',
  14864. 'iquest': '\u00BF',
  14865. 'iscr': '\uD835\uDCBE',
  14866. 'isinE': '\u22F9',
  14867. 'isindot': '\u22F5',
  14868. 'isins': '\u22F4',
  14869. 'isinsv': '\u22F3',
  14870. 'itilde': '\u0129',
  14871. 'iukcy': '\u0456',
  14872. 'iuml': '\u00EF',
  14873. 'jcirc': '\u0135',
  14874. 'jcy': '\u0439',
  14875. 'jfr': '\uD835\uDD27',
  14876. 'jmath': '\u0237',
  14877. 'jopf': '\uD835\uDD5B',
  14878. 'jscr': '\uD835\uDCBF',
  14879. 'jsercy': '\u0458',
  14880. 'jukcy': '\u0454',
  14881. 'kappa': '\u03BA',
  14882. 'kappav': '\u03F0',
  14883. 'varkappa': '\u03F0',
  14884. 'kcedil': '\u0137',
  14885. 'kcy': '\u043A',
  14886. 'kfr': '\uD835\uDD28',
  14887. 'kgreen': '\u0138',
  14888. 'khcy': '\u0445',
  14889. 'kjcy': '\u045C',
  14890. 'kopf': '\uD835\uDD5C',
  14891. 'kscr': '\uD835\uDCC0',
  14892. 'lAtail': '\u291B',
  14893. 'lBarr': '\u290E',
  14894. 'lEg': '\u2A8B',
  14895. 'lesseqqgtr': '\u2A8B',
  14896. 'lHar': '\u2962',
  14897. 'lacute': '\u013A',
  14898. 'laemptyv': '\u29B4',
  14899. 'lambda': '\u03BB',
  14900. 'langd': '\u2991',
  14901. 'lap': '\u2A85',
  14902. 'lessapprox': '\u2A85',
  14903. 'laquo': '\u00AB',
  14904. 'larrbfs': '\u291F',
  14905. 'larrfs': '\u291D',
  14906. 'larrlp': '\u21AB',
  14907. 'looparrowleft': '\u21AB',
  14908. 'larrpl': '\u2939',
  14909. 'larrsim': '\u2973',
  14910. 'larrtl': '\u21A2',
  14911. 'leftarrowtail': '\u21A2',
  14912. 'lat': '\u2AAB',
  14913. 'latail': '\u2919',
  14914. 'late': '\u2AAD',
  14915. 'lates': '\u2AAD\uFE00',
  14916. 'lbarr': '\u290C',
  14917. 'lbbrk': '\u2772',
  14918. 'lbrace': '\u007B',
  14919. 'lcub': '\u007B',
  14920. 'lbrack': '\u005B',
  14921. 'lsqb': '\u005B',
  14922. 'lbrke': '\u298B',
  14923. 'lbrksld': '\u298F',
  14924. 'lbrkslu': '\u298D',
  14925. 'lcaron': '\u013E',
  14926. 'lcedil': '\u013C',
  14927. 'lcy': '\u043B',
  14928. 'ldca': '\u2936',
  14929. 'ldrdhar': '\u2967',
  14930. 'ldrushar': '\u294B',
  14931. 'ldsh': '\u21B2',
  14932. 'le': '\u2264',
  14933. 'leq': '\u2264',
  14934. 'leftleftarrows': '\u21C7',
  14935. 'llarr': '\u21C7',
  14936. 'leftthreetimes': '\u22CB',
  14937. 'lthree': '\u22CB',
  14938. 'lescc': '\u2AA8',
  14939. 'lesdot': '\u2A7F',
  14940. 'lesdoto': '\u2A81',
  14941. 'lesdotor': '\u2A83',
  14942. 'lesg': '\u22DA\uFE00',
  14943. 'lesges': '\u2A93',
  14944. 'lessdot': '\u22D6',
  14945. 'ltdot': '\u22D6',
  14946. 'lfisht': '\u297C',
  14947. 'lfr': '\uD835\uDD29',
  14948. 'lgE': '\u2A91',
  14949. 'lharul': '\u296A',
  14950. 'lhblk': '\u2584',
  14951. 'ljcy': '\u0459',
  14952. 'llhard': '\u296B',
  14953. 'lltri': '\u25FA',
  14954. 'lmidot': '\u0140',
  14955. 'lmoust': '\u23B0',
  14956. 'lmoustache': '\u23B0',
  14957. 'lnE': '\u2268',
  14958. 'lneqq': '\u2268',
  14959. 'lnap': '\u2A89',
  14960. 'lnapprox': '\u2A89',
  14961. 'lne': '\u2A87',
  14962. 'lneq': '\u2A87',
  14963. 'lnsim': '\u22E6',
  14964. 'loang': '\u27EC',
  14965. 'loarr': '\u21FD',
  14966. 'longmapsto': '\u27FC',
  14967. 'xmap': '\u27FC',
  14968. 'looparrowright': '\u21AC',
  14969. 'rarrlp': '\u21AC',
  14970. 'lopar': '\u2985',
  14971. 'lopf': '\uD835\uDD5D',
  14972. 'loplus': '\u2A2D',
  14973. 'lotimes': '\u2A34',
  14974. 'lowast': '\u2217',
  14975. 'loz': '\u25CA',
  14976. 'lozenge': '\u25CA',
  14977. 'lpar': '\u0028',
  14978. 'lparlt': '\u2993',
  14979. 'lrhard': '\u296D',
  14980. 'lrm': '\u200E',
  14981. 'lrtri': '\u22BF',
  14982. 'lsaquo': '\u2039',
  14983. 'lscr': '\uD835\uDCC1',
  14984. 'lsime': '\u2A8D',
  14985. 'lsimg': '\u2A8F',
  14986. 'lsquor': '\u201A',
  14987. 'sbquo': '\u201A',
  14988. 'lstrok': '\u0142',
  14989. 'ltcc': '\u2AA6',
  14990. 'ltcir': '\u2A79',
  14991. 'ltimes': '\u22C9',
  14992. 'ltlarr': '\u2976',
  14993. 'ltquest': '\u2A7B',
  14994. 'ltrPar': '\u2996',
  14995. 'ltri': '\u25C3',
  14996. 'triangleleft': '\u25C3',
  14997. 'lurdshar': '\u294A',
  14998. 'luruhar': '\u2966',
  14999. 'lvertneqq': '\u2268\uFE00',
  15000. 'lvnE': '\u2268\uFE00',
  15001. 'mDDot': '\u223A',
  15002. 'macr': '\u00AF',
  15003. 'strns': '\u00AF',
  15004. 'male': '\u2642',
  15005. 'malt': '\u2720',
  15006. 'maltese': '\u2720',
  15007. 'marker': '\u25AE',
  15008. 'mcomma': '\u2A29',
  15009. 'mcy': '\u043C',
  15010. 'mdash': '\u2014',
  15011. 'mfr': '\uD835\uDD2A',
  15012. 'mho': '\u2127',
  15013. 'micro': '\u00B5',
  15014. 'midcir': '\u2AF0',
  15015. 'minus': '\u2212',
  15016. 'minusdu': '\u2A2A',
  15017. 'mlcp': '\u2ADB',
  15018. 'models': '\u22A7',
  15019. 'mopf': '\uD835\uDD5E',
  15020. 'mscr': '\uD835\uDCC2',
  15021. 'mu': '\u03BC',
  15022. 'multimap': '\u22B8',
  15023. 'mumap': '\u22B8',
  15024. 'nGg': '\u22D9\u0338',
  15025. 'nGt': '\u226B\u20D2',
  15026. 'nLeftarrow': '\u21CD',
  15027. 'nlArr': '\u21CD',
  15028. 'nLeftrightarrow': '\u21CE',
  15029. 'nhArr': '\u21CE',
  15030. 'nLl': '\u22D8\u0338',
  15031. 'nLt': '\u226A\u20D2',
  15032. 'nRightarrow': '\u21CF',
  15033. 'nrArr': '\u21CF',
  15034. 'nVDash': '\u22AF',
  15035. 'nVdash': '\u22AE',
  15036. 'nacute': '\u0144',
  15037. 'nang': '\u2220\u20D2',
  15038. 'napE': '\u2A70\u0338',
  15039. 'napid': '\u224B\u0338',
  15040. 'napos': '\u0149',
  15041. 'natur': '\u266E',
  15042. 'natural': '\u266E',
  15043. 'ncap': '\u2A43',
  15044. 'ncaron': '\u0148',
  15045. 'ncedil': '\u0146',
  15046. 'ncongdot': '\u2A6D\u0338',
  15047. 'ncup': '\u2A42',
  15048. 'ncy': '\u043D',
  15049. 'ndash': '\u2013',
  15050. 'neArr': '\u21D7',
  15051. 'nearhk': '\u2924',
  15052. 'nedot': '\u2250\u0338',
  15053. 'nesear': '\u2928',
  15054. 'toea': '\u2928',
  15055. 'nfr': '\uD835\uDD2B',
  15056. 'nharr': '\u21AE',
  15057. 'nleftrightarrow': '\u21AE',
  15058. 'nhpar': '\u2AF2',
  15059. 'nis': '\u22FC',
  15060. 'nisd': '\u22FA',
  15061. 'njcy': '\u045A',
  15062. 'nlE': '\u2266\u0338',
  15063. 'nleqq': '\u2266\u0338',
  15064. 'nlarr': '\u219A',
  15065. 'nleftarrow': '\u219A',
  15066. 'nldr': '\u2025',
  15067. 'nopf': '\uD835\uDD5F',
  15068. 'not': '\u00AC',
  15069. 'notinE': '\u22F9\u0338',
  15070. 'notindot': '\u22F5\u0338',
  15071. 'notinvb': '\u22F7',
  15072. 'notinvc': '\u22F6',
  15073. 'notnivb': '\u22FE',
  15074. 'notnivc': '\u22FD',
  15075. 'nparsl': '\u2AFD\u20E5',
  15076. 'npart': '\u2202\u0338',
  15077. 'npolint': '\u2A14',
  15078. 'nrarr': '\u219B',
  15079. 'nrightarrow': '\u219B',
  15080. 'nrarrc': '\u2933\u0338',
  15081. 'nrarrw': '\u219D\u0338',
  15082. 'nscr': '\uD835\uDCC3',
  15083. 'nsub': '\u2284',
  15084. 'nsubE': '\u2AC5\u0338',
  15085. 'nsubseteqq': '\u2AC5\u0338',
  15086. 'nsup': '\u2285',
  15087. 'nsupE': '\u2AC6\u0338',
  15088. 'nsupseteqq': '\u2AC6\u0338',
  15089. 'ntilde': '\u00F1',
  15090. 'nu': '\u03BD',
  15091. 'num': '\u0023',
  15092. 'numero': '\u2116',
  15093. 'numsp': '\u2007',
  15094. 'nvDash': '\u22AD',
  15095. 'nvHarr': '\u2904',
  15096. 'nvap': '\u224D\u20D2',
  15097. 'nvdash': '\u22AC',
  15098. 'nvge': '\u2265\u20D2',
  15099. 'nvgt': '\u003E\u20D2',
  15100. 'nvinfin': '\u29DE',
  15101. 'nvlArr': '\u2902',
  15102. 'nvle': '\u2264\u20D2',
  15103. 'nvlt': '\u003C\u20D2',
  15104. 'nvltrie': '\u22B4\u20D2',
  15105. 'nvrArr': '\u2903',
  15106. 'nvrtrie': '\u22B5\u20D2',
  15107. 'nvsim': '\u223C\u20D2',
  15108. 'nwArr': '\u21D6',
  15109. 'nwarhk': '\u2923',
  15110. 'nwnear': '\u2927',
  15111. 'oacute': '\u00F3',
  15112. 'ocirc': '\u00F4',
  15113. 'ocy': '\u043E',
  15114. 'odblac': '\u0151',
  15115. 'odiv': '\u2A38',
  15116. 'odsold': '\u29BC',
  15117. 'oelig': '\u0153',
  15118. 'ofcir': '\u29BF',
  15119. 'ofr': '\uD835\uDD2C',
  15120. 'ogon': '\u02DB',
  15121. 'ograve': '\u00F2',
  15122. 'ogt': '\u29C1',
  15123. 'ohbar': '\u29B5',
  15124. 'olcir': '\u29BE',
  15125. 'olcross': '\u29BB',
  15126. 'olt': '\u29C0',
  15127. 'omacr': '\u014D',
  15128. 'omega': '\u03C9',
  15129. 'omicron': '\u03BF',
  15130. 'omid': '\u29B6',
  15131. 'oopf': '\uD835\uDD60',
  15132. 'opar': '\u29B7',
  15133. 'operp': '\u29B9',
  15134. 'or': '\u2228',
  15135. 'vee': '\u2228',
  15136. 'ord': '\u2A5D',
  15137. 'order': '\u2134',
  15138. 'orderof': '\u2134',
  15139. 'oscr': '\u2134',
  15140. 'ordf': '\u00AA',
  15141. 'ordm': '\u00BA',
  15142. 'origof': '\u22B6',
  15143. 'oror': '\u2A56',
  15144. 'orslope': '\u2A57',
  15145. 'orv': '\u2A5B',
  15146. 'oslash': '\u00F8',
  15147. 'osol': '\u2298',
  15148. 'otilde': '\u00F5',
  15149. 'otimesas': '\u2A36',
  15150. 'ouml': '\u00F6',
  15151. 'ovbar': '\u233D',
  15152. 'para': '\u00B6',
  15153. 'parsim': '\u2AF3',
  15154. 'parsl': '\u2AFD',
  15155. 'pcy': '\u043F',
  15156. 'percnt': '\u0025',
  15157. 'period': '\u002E',
  15158. 'permil': '\u2030',
  15159. 'pertenk': '\u2031',
  15160. 'pfr': '\uD835\uDD2D',
  15161. 'phi': '\u03C6',
  15162. 'phiv': '\u03D5',
  15163. 'straightphi': '\u03D5',
  15164. 'varphi': '\u03D5',
  15165. 'phone': '\u260E',
  15166. 'pi': '\u03C0',
  15167. 'piv': '\u03D6',
  15168. 'varpi': '\u03D6',
  15169. 'planckh': '\u210E',
  15170. 'plus': '\u002B',
  15171. 'plusacir': '\u2A23',
  15172. 'pluscir': '\u2A22',
  15173. 'plusdu': '\u2A25',
  15174. 'pluse': '\u2A72',
  15175. 'plussim': '\u2A26',
  15176. 'plustwo': '\u2A27',
  15177. 'pointint': '\u2A15',
  15178. 'popf': '\uD835\uDD61',
  15179. 'pound': '\u00A3',
  15180. 'prE': '\u2AB3',
  15181. 'prap': '\u2AB7',
  15182. 'precapprox': '\u2AB7',
  15183. 'precnapprox': '\u2AB9',
  15184. 'prnap': '\u2AB9',
  15185. 'precneqq': '\u2AB5',
  15186. 'prnE': '\u2AB5',
  15187. 'precnsim': '\u22E8',
  15188. 'prnsim': '\u22E8',
  15189. 'prime': '\u2032',
  15190. 'profalar': '\u232E',
  15191. 'profline': '\u2312',
  15192. 'profsurf': '\u2313',
  15193. 'prurel': '\u22B0',
  15194. 'pscr': '\uD835\uDCC5',
  15195. 'psi': '\u03C8',
  15196. 'puncsp': '\u2008',
  15197. 'qfr': '\uD835\uDD2E',
  15198. 'qopf': '\uD835\uDD62',
  15199. 'qprime': '\u2057',
  15200. 'qscr': '\uD835\uDCC6',
  15201. 'quatint': '\u2A16',
  15202. 'quest': '\u003F',
  15203. 'rAtail': '\u291C',
  15204. 'rHar': '\u2964',
  15205. 'race': '\u223D\u0331',
  15206. 'racute': '\u0155',
  15207. 'raemptyv': '\u29B3',
  15208. 'rangd': '\u2992',
  15209. 'range': '\u29A5',
  15210. 'raquo': '\u00BB',
  15211. 'rarrap': '\u2975',
  15212. 'rarrbfs': '\u2920',
  15213. 'rarrc': '\u2933',
  15214. 'rarrfs': '\u291E',
  15215. 'rarrpl': '\u2945',
  15216. 'rarrsim': '\u2974',
  15217. 'rarrtl': '\u21A3',
  15218. 'rightarrowtail': '\u21A3',
  15219. 'rarrw': '\u219D',
  15220. 'rightsquigarrow': '\u219D',
  15221. 'ratail': '\u291A',
  15222. 'ratio': '\u2236',
  15223. 'rbbrk': '\u2773',
  15224. 'rbrace': '\u007D',
  15225. 'rcub': '\u007D',
  15226. 'rbrack': '\u005D',
  15227. 'rsqb': '\u005D',
  15228. 'rbrke': '\u298C',
  15229. 'rbrksld': '\u298E',
  15230. 'rbrkslu': '\u2990',
  15231. 'rcaron': '\u0159',
  15232. 'rcedil': '\u0157',
  15233. 'rcy': '\u0440',
  15234. 'rdca': '\u2937',
  15235. 'rdldhar': '\u2969',
  15236. 'rdsh': '\u21B3',
  15237. 'rect': '\u25AD',
  15238. 'rfisht': '\u297D',
  15239. 'rfr': '\uD835\uDD2F',
  15240. 'rharul': '\u296C',
  15241. 'rho': '\u03C1',
  15242. 'rhov': '\u03F1',
  15243. 'varrho': '\u03F1',
  15244. 'rightrightarrows': '\u21C9',
  15245. 'rrarr': '\u21C9',
  15246. 'rightthreetimes': '\u22CC',
  15247. 'rthree': '\u22CC',
  15248. 'ring': '\u02DA',
  15249. 'rlm': '\u200F',
  15250. 'rmoust': '\u23B1',
  15251. 'rmoustache': '\u23B1',
  15252. 'rnmid': '\u2AEE',
  15253. 'roang': '\u27ED',
  15254. 'roarr': '\u21FE',
  15255. 'ropar': '\u2986',
  15256. 'ropf': '\uD835\uDD63',
  15257. 'roplus': '\u2A2E',
  15258. 'rotimes': '\u2A35',
  15259. 'rpar': '\u0029',
  15260. 'rpargt': '\u2994',
  15261. 'rppolint': '\u2A12',
  15262. 'rsaquo': '\u203A',
  15263. 'rscr': '\uD835\uDCC7',
  15264. 'rtimes': '\u22CA',
  15265. 'rtri': '\u25B9',
  15266. 'triangleright': '\u25B9',
  15267. 'rtriltri': '\u29CE',
  15268. 'ruluhar': '\u2968',
  15269. 'rx': '\u211E',
  15270. 'sacute': '\u015B',
  15271. 'scE': '\u2AB4',
  15272. 'scap': '\u2AB8',
  15273. 'succapprox': '\u2AB8',
  15274. 'scaron': '\u0161',
  15275. 'scedil': '\u015F',
  15276. 'scirc': '\u015D',
  15277. 'scnE': '\u2AB6',
  15278. 'succneqq': '\u2AB6',
  15279. 'scnap': '\u2ABA',
  15280. 'succnapprox': '\u2ABA',
  15281. 'scnsim': '\u22E9',
  15282. 'succnsim': '\u22E9',
  15283. 'scpolint': '\u2A13',
  15284. 'scy': '\u0441',
  15285. 'sdot': '\u22C5',
  15286. 'sdote': '\u2A66',
  15287. 'seArr': '\u21D8',
  15288. 'sect': '\u00A7',
  15289. 'semi': '\u003B',
  15290. 'seswar': '\u2929',
  15291. 'tosa': '\u2929',
  15292. 'sext': '\u2736',
  15293. 'sfr': '\uD835\uDD30',
  15294. 'sharp': '\u266F',
  15295. 'shchcy': '\u0449',
  15296. 'shcy': '\u0448',
  15297. 'shy': '\u00AD',
  15298. 'sigma': '\u03C3',
  15299. 'sigmaf': '\u03C2',
  15300. 'sigmav': '\u03C2',
  15301. 'varsigma': '\u03C2',
  15302. 'simdot': '\u2A6A',
  15303. 'simg': '\u2A9E',
  15304. 'simgE': '\u2AA0',
  15305. 'siml': '\u2A9D',
  15306. 'simlE': '\u2A9F',
  15307. 'simne': '\u2246',
  15308. 'simplus': '\u2A24',
  15309. 'simrarr': '\u2972',
  15310. 'smashp': '\u2A33',
  15311. 'smeparsl': '\u29E4',
  15312. 'smile': '\u2323',
  15313. 'ssmile': '\u2323',
  15314. 'smt': '\u2AAA',
  15315. 'smte': '\u2AAC',
  15316. 'smtes': '\u2AAC\uFE00',
  15317. 'softcy': '\u044C',
  15318. 'sol': '\u002F',
  15319. 'solb': '\u29C4',
  15320. 'solbar': '\u233F',
  15321. 'sopf': '\uD835\uDD64',
  15322. 'spades': '\u2660',
  15323. 'spadesuit': '\u2660',
  15324. 'sqcaps': '\u2293\uFE00',
  15325. 'sqcups': '\u2294\uFE00',
  15326. 'sscr': '\uD835\uDCC8',
  15327. 'star': '\u2606',
  15328. 'sub': '\u2282',
  15329. 'subset': '\u2282',
  15330. 'subE': '\u2AC5',
  15331. 'subseteqq': '\u2AC5',
  15332. 'subdot': '\u2ABD',
  15333. 'subedot': '\u2AC3',
  15334. 'submult': '\u2AC1',
  15335. 'subnE': '\u2ACB',
  15336. 'subsetneqq': '\u2ACB',
  15337. 'subne': '\u228A',
  15338. 'subsetneq': '\u228A',
  15339. 'subplus': '\u2ABF',
  15340. 'subrarr': '\u2979',
  15341. 'subsim': '\u2AC7',
  15342. 'subsub': '\u2AD5',
  15343. 'subsup': '\u2AD3',
  15344. 'sung': '\u266A',
  15345. 'sup1': '\u00B9',
  15346. 'sup2': '\u00B2',
  15347. 'sup3': '\u00B3',
  15348. 'supE': '\u2AC6',
  15349. 'supseteqq': '\u2AC6',
  15350. 'supdot': '\u2ABE',
  15351. 'supdsub': '\u2AD8',
  15352. 'supedot': '\u2AC4',
  15353. 'suphsol': '\u27C9',
  15354. 'suphsub': '\u2AD7',
  15355. 'suplarr': '\u297B',
  15356. 'supmult': '\u2AC2',
  15357. 'supnE': '\u2ACC',
  15358. 'supsetneqq': '\u2ACC',
  15359. 'supne': '\u228B',
  15360. 'supsetneq': '\u228B',
  15361. 'supplus': '\u2AC0',
  15362. 'supsim': '\u2AC8',
  15363. 'supsub': '\u2AD4',
  15364. 'supsup': '\u2AD6',
  15365. 'swArr': '\u21D9',
  15366. 'swnwar': '\u292A',
  15367. 'szlig': '\u00DF',
  15368. 'target': '\u2316',
  15369. 'tau': '\u03C4',
  15370. 'tcaron': '\u0165',
  15371. 'tcedil': '\u0163',
  15372. 'tcy': '\u0442',
  15373. 'telrec': '\u2315',
  15374. 'tfr': '\uD835\uDD31',
  15375. 'theta': '\u03B8',
  15376. 'thetasym': '\u03D1',
  15377. 'thetav': '\u03D1',
  15378. 'vartheta': '\u03D1',
  15379. 'thorn': '\u00FE',
  15380. 'times': '\u00D7',
  15381. 'timesbar': '\u2A31',
  15382. 'timesd': '\u2A30',
  15383. 'topbot': '\u2336',
  15384. 'topcir': '\u2AF1',
  15385. 'topf': '\uD835\uDD65',
  15386. 'topfork': '\u2ADA',
  15387. 'tprime': '\u2034',
  15388. 'triangle': '\u25B5',
  15389. 'utri': '\u25B5',
  15390. 'triangleq': '\u225C',
  15391. 'trie': '\u225C',
  15392. 'tridot': '\u25EC',
  15393. 'triminus': '\u2A3A',
  15394. 'triplus': '\u2A39',
  15395. 'trisb': '\u29CD',
  15396. 'tritime': '\u2A3B',
  15397. 'trpezium': '\u23E2',
  15398. 'tscr': '\uD835\uDCC9',
  15399. 'tscy': '\u0446',
  15400. 'tshcy': '\u045B',
  15401. 'tstrok': '\u0167',
  15402. 'uHar': '\u2963',
  15403. 'uacute': '\u00FA',
  15404. 'ubrcy': '\u045E',
  15405. 'ubreve': '\u016D',
  15406. 'ucirc': '\u00FB',
  15407. 'ucy': '\u0443',
  15408. 'udblac': '\u0171',
  15409. 'ufisht': '\u297E',
  15410. 'ufr': '\uD835\uDD32',
  15411. 'ugrave': '\u00F9',
  15412. 'uhblk': '\u2580',
  15413. 'ulcorn': '\u231C',
  15414. 'ulcorner': '\u231C',
  15415. 'ulcrop': '\u230F',
  15416. 'ultri': '\u25F8',
  15417. 'umacr': '\u016B',
  15418. 'uogon': '\u0173',
  15419. 'uopf': '\uD835\uDD66',
  15420. 'upsi': '\u03C5',
  15421. 'upsilon': '\u03C5',
  15422. 'upuparrows': '\u21C8',
  15423. 'uuarr': '\u21C8',
  15424. 'urcorn': '\u231D',
  15425. 'urcorner': '\u231D',
  15426. 'urcrop': '\u230E',
  15427. 'uring': '\u016F',
  15428. 'urtri': '\u25F9',
  15429. 'uscr': '\uD835\uDCCA',
  15430. 'utdot': '\u22F0',
  15431. 'utilde': '\u0169',
  15432. 'uuml': '\u00FC',
  15433. 'uwangle': '\u29A7',
  15434. 'vBar': '\u2AE8',
  15435. 'vBarv': '\u2AE9',
  15436. 'vangrt': '\u299C',
  15437. 'varsubsetneq': '\u228A\uFE00',
  15438. 'vsubne': '\u228A\uFE00',
  15439. 'varsubsetneqq': '\u2ACB\uFE00',
  15440. 'vsubnE': '\u2ACB\uFE00',
  15441. 'varsupsetneq': '\u228B\uFE00',
  15442. 'vsupne': '\u228B\uFE00',
  15443. 'varsupsetneqq': '\u2ACC\uFE00',
  15444. 'vsupnE': '\u2ACC\uFE00',
  15445. 'vcy': '\u0432',
  15446. 'veebar': '\u22BB',
  15447. 'veeeq': '\u225A',
  15448. 'vellip': '\u22EE',
  15449. 'vfr': '\uD835\uDD33',
  15450. 'vopf': '\uD835\uDD67',
  15451. 'vscr': '\uD835\uDCCB',
  15452. 'vzigzag': '\u299A',
  15453. 'wcirc': '\u0175',
  15454. 'wedbar': '\u2A5F',
  15455. 'wedgeq': '\u2259',
  15456. 'weierp': '\u2118',
  15457. 'wp': '\u2118',
  15458. 'wfr': '\uD835\uDD34',
  15459. 'wopf': '\uD835\uDD68',
  15460. 'wscr': '\uD835\uDCCC',
  15461. 'xfr': '\uD835\uDD35',
  15462. 'xi': '\u03BE',
  15463. 'xnis': '\u22FB',
  15464. 'xopf': '\uD835\uDD69',
  15465. 'xscr': '\uD835\uDCCD',
  15466. 'yacute': '\u00FD',
  15467. 'yacy': '\u044F',
  15468. 'ycirc': '\u0177',
  15469. 'ycy': '\u044B',
  15470. 'yen': '\u00A5',
  15471. 'yfr': '\uD835\uDD36',
  15472. 'yicy': '\u0457',
  15473. 'yopf': '\uD835\uDD6A',
  15474. 'yscr': '\uD835\uDCCE',
  15475. 'yucy': '\u044E',
  15476. 'yuml': '\u00FF',
  15477. 'zacute': '\u017A',
  15478. 'zcaron': '\u017E',
  15479. 'zcy': '\u0437',
  15480. 'zdot': '\u017C',
  15481. 'zeta': '\u03B6',
  15482. 'zfr': '\uD835\uDD37',
  15483. 'zhcy': '\u0436',
  15484. 'zigrarr': '\u21DD',
  15485. 'zopf': '\uD835\uDD6B',
  15486. 'zscr': '\uD835\uDCCF',
  15487. 'zwj': '\u200D',
  15488. 'zwnj': '\u200C',
  15489. };
  15490. // The &ngsp; pseudo-entity is denoting a space.
  15491. // 0xE500 is a PUA (Private Use Areas) unicode character
  15492. // This is inspired by the Angular Dart implementation.
  15493. const NGSP_UNICODE = '\uE500';
  15494. NAMED_ENTITIES['ngsp'] = NGSP_UNICODE;
  15495. class TokenError extends ParseError {
  15496. tokenType;
  15497. constructor(errorMsg, tokenType, span) {
  15498. super(span, errorMsg);
  15499. this.tokenType = tokenType;
  15500. }
  15501. }
  15502. class TokenizeResult {
  15503. tokens;
  15504. errors;
  15505. nonNormalizedIcuExpressions;
  15506. constructor(tokens, errors, nonNormalizedIcuExpressions) {
  15507. this.tokens = tokens;
  15508. this.errors = errors;
  15509. this.nonNormalizedIcuExpressions = nonNormalizedIcuExpressions;
  15510. }
  15511. }
  15512. function tokenize(source, url, getTagDefinition, options = {}) {
  15513. const tokenizer = new _Tokenizer(new ParseSourceFile(source, url), getTagDefinition, options);
  15514. tokenizer.tokenize();
  15515. return new TokenizeResult(mergeTextTokens(tokenizer.tokens), tokenizer.errors, tokenizer.nonNormalizedIcuExpressions);
  15516. }
  15517. const _CR_OR_CRLF_REGEXP = /\r\n?/g;
  15518. function _unexpectedCharacterErrorMsg(charCode) {
  15519. const char = charCode === $EOF ? 'EOF' : String.fromCharCode(charCode);
  15520. return `Unexpected character "${char}"`;
  15521. }
  15522. function _unknownEntityErrorMsg(entitySrc) {
  15523. return `Unknown entity "${entitySrc}" - use the "&#<decimal>;" or "&#x<hex>;" syntax`;
  15524. }
  15525. function _unparsableEntityErrorMsg(type, entityStr) {
  15526. return `Unable to parse entity "${entityStr}" - ${type} character reference entities must end with ";"`;
  15527. }
  15528. var CharacterReferenceType;
  15529. (function (CharacterReferenceType) {
  15530. CharacterReferenceType["HEX"] = "hexadecimal";
  15531. CharacterReferenceType["DEC"] = "decimal";
  15532. })(CharacterReferenceType || (CharacterReferenceType = {}));
  15533. class _ControlFlowError {
  15534. error;
  15535. constructor(error) {
  15536. this.error = error;
  15537. }
  15538. }
  15539. // See https://www.w3.org/TR/html51/syntax.html#writing-html-documents
  15540. class _Tokenizer {
  15541. _getTagDefinition;
  15542. _cursor;
  15543. _tokenizeIcu;
  15544. _interpolationConfig;
  15545. _leadingTriviaCodePoints;
  15546. _currentTokenStart = null;
  15547. _currentTokenType = null;
  15548. _expansionCaseStack = [];
  15549. _inInterpolation = false;
  15550. _preserveLineEndings;
  15551. _i18nNormalizeLineEndingsInICUs;
  15552. _tokenizeBlocks;
  15553. _tokenizeLet;
  15554. tokens = [];
  15555. errors = [];
  15556. nonNormalizedIcuExpressions = [];
  15557. /**
  15558. * @param _file The html source file being tokenized.
  15559. * @param _getTagDefinition A function that will retrieve a tag definition for a given tag name.
  15560. * @param options Configuration of the tokenization.
  15561. */
  15562. constructor(_file, _getTagDefinition, options) {
  15563. this._getTagDefinition = _getTagDefinition;
  15564. this._tokenizeIcu = options.tokenizeExpansionForms || false;
  15565. this._interpolationConfig = options.interpolationConfig || DEFAULT_INTERPOLATION_CONFIG;
  15566. this._leadingTriviaCodePoints =
  15567. options.leadingTriviaChars && options.leadingTriviaChars.map((c) => c.codePointAt(0) || 0);
  15568. const range = options.range || {
  15569. endPos: _file.content.length,
  15570. startPos: 0,
  15571. startLine: 0,
  15572. startCol: 0,
  15573. };
  15574. this._cursor = options.escapedString
  15575. ? new EscapedCharacterCursor(_file, range)
  15576. : new PlainCharacterCursor(_file, range);
  15577. this._preserveLineEndings = options.preserveLineEndings || false;
  15578. this._i18nNormalizeLineEndingsInICUs = options.i18nNormalizeLineEndingsInICUs || false;
  15579. this._tokenizeBlocks = options.tokenizeBlocks ?? true;
  15580. this._tokenizeLet = options.tokenizeLet ?? true;
  15581. try {
  15582. this._cursor.init();
  15583. }
  15584. catch (e) {
  15585. this.handleError(e);
  15586. }
  15587. }
  15588. _processCarriageReturns(content) {
  15589. if (this._preserveLineEndings) {
  15590. return content;
  15591. }
  15592. // https://www.w3.org/TR/html51/syntax.html#preprocessing-the-input-stream
  15593. // In order to keep the original position in the source, we can not
  15594. // pre-process it.
  15595. // Instead CRs are processed right before instantiating the tokens.
  15596. return content.replace(_CR_OR_CRLF_REGEXP, '\n');
  15597. }
  15598. tokenize() {
  15599. while (this._cursor.peek() !== $EOF) {
  15600. const start = this._cursor.clone();
  15601. try {
  15602. if (this._attemptCharCode($LT)) {
  15603. if (this._attemptCharCode($BANG)) {
  15604. if (this._attemptCharCode($LBRACKET)) {
  15605. this._consumeCdata(start);
  15606. }
  15607. else if (this._attemptCharCode($MINUS)) {
  15608. this._consumeComment(start);
  15609. }
  15610. else {
  15611. this._consumeDocType(start);
  15612. }
  15613. }
  15614. else if (this._attemptCharCode($SLASH)) {
  15615. this._consumeTagClose(start);
  15616. }
  15617. else {
  15618. this._consumeTagOpen(start);
  15619. }
  15620. }
  15621. else if (this._tokenizeLet &&
  15622. // Use `peek` instead of `attempCharCode` since we
  15623. // don't want to advance in case it's not `@let`.
  15624. this._cursor.peek() === $AT &&
  15625. !this._inInterpolation &&
  15626. this._attemptStr('@let')) {
  15627. this._consumeLetDeclaration(start);
  15628. }
  15629. else if (this._tokenizeBlocks && this._attemptCharCode($AT)) {
  15630. this._consumeBlockStart(start);
  15631. }
  15632. else if (this._tokenizeBlocks &&
  15633. !this._inInterpolation &&
  15634. !this._isInExpansionCase() &&
  15635. !this._isInExpansionForm() &&
  15636. this._attemptCharCode($RBRACE)) {
  15637. this._consumeBlockEnd(start);
  15638. }
  15639. else if (!(this._tokenizeIcu && this._tokenizeExpansionForm())) {
  15640. // In (possibly interpolated) text the end of the text is given by `isTextEnd()`, while
  15641. // the premature end of an interpolation is given by the start of a new HTML element.
  15642. this._consumeWithInterpolation(5 /* TokenType.TEXT */, 8 /* TokenType.INTERPOLATION */, () => this._isTextEnd(), () => this._isTagStart());
  15643. }
  15644. }
  15645. catch (e) {
  15646. this.handleError(e);
  15647. }
  15648. }
  15649. this._beginToken(33 /* TokenType.EOF */);
  15650. this._endToken([]);
  15651. }
  15652. _getBlockName() {
  15653. // This allows us to capture up something like `@else if`, but not `@ if`.
  15654. let spacesInNameAllowed = false;
  15655. const nameCursor = this._cursor.clone();
  15656. this._attemptCharCodeUntilFn((code) => {
  15657. if (isWhitespace(code)) {
  15658. return !spacesInNameAllowed;
  15659. }
  15660. if (isBlockNameChar(code)) {
  15661. spacesInNameAllowed = true;
  15662. return false;
  15663. }
  15664. return true;
  15665. });
  15666. return this._cursor.getChars(nameCursor).trim();
  15667. }
  15668. _consumeBlockStart(start) {
  15669. this._beginToken(24 /* TokenType.BLOCK_OPEN_START */, start);
  15670. const startToken = this._endToken([this._getBlockName()]);
  15671. if (this._cursor.peek() === $LPAREN) {
  15672. // Advance past the opening paren.
  15673. this._cursor.advance();
  15674. // Capture the parameters.
  15675. this._consumeBlockParameters();
  15676. // Allow spaces before the closing paren.
  15677. this._attemptCharCodeUntilFn(isNotWhitespace);
  15678. if (this._attemptCharCode($RPAREN)) {
  15679. // Allow spaces after the paren.
  15680. this._attemptCharCodeUntilFn(isNotWhitespace);
  15681. }
  15682. else {
  15683. startToken.type = 28 /* TokenType.INCOMPLETE_BLOCK_OPEN */;
  15684. return;
  15685. }
  15686. }
  15687. if (this._attemptCharCode($LBRACE)) {
  15688. this._beginToken(25 /* TokenType.BLOCK_OPEN_END */);
  15689. this._endToken([]);
  15690. }
  15691. else {
  15692. startToken.type = 28 /* TokenType.INCOMPLETE_BLOCK_OPEN */;
  15693. }
  15694. }
  15695. _consumeBlockEnd(start) {
  15696. this._beginToken(26 /* TokenType.BLOCK_CLOSE */, start);
  15697. this._endToken([]);
  15698. }
  15699. _consumeBlockParameters() {
  15700. // Trim the whitespace until the first parameter.
  15701. this._attemptCharCodeUntilFn(isBlockParameterChar);
  15702. while (this._cursor.peek() !== $RPAREN && this._cursor.peek() !== $EOF) {
  15703. this._beginToken(27 /* TokenType.BLOCK_PARAMETER */);
  15704. const start = this._cursor.clone();
  15705. let inQuote = null;
  15706. let openParens = 0;
  15707. // Consume the parameter until the next semicolon or brace.
  15708. // Note that we skip over semicolons/braces inside of strings.
  15709. while ((this._cursor.peek() !== $SEMICOLON && this._cursor.peek() !== $EOF) ||
  15710. inQuote !== null) {
  15711. const char = this._cursor.peek();
  15712. // Skip to the next character if it was escaped.
  15713. if (char === $BACKSLASH) {
  15714. this._cursor.advance();
  15715. }
  15716. else if (char === inQuote) {
  15717. inQuote = null;
  15718. }
  15719. else if (inQuote === null && isQuote(char)) {
  15720. inQuote = char;
  15721. }
  15722. else if (char === $LPAREN && inQuote === null) {
  15723. openParens++;
  15724. }
  15725. else if (char === $RPAREN && inQuote === null) {
  15726. if (openParens === 0) {
  15727. break;
  15728. }
  15729. else if (openParens > 0) {
  15730. openParens--;
  15731. }
  15732. }
  15733. this._cursor.advance();
  15734. }
  15735. this._endToken([this._cursor.getChars(start)]);
  15736. // Skip to the next parameter.
  15737. this._attemptCharCodeUntilFn(isBlockParameterChar);
  15738. }
  15739. }
  15740. _consumeLetDeclaration(start) {
  15741. this._beginToken(29 /* TokenType.LET_START */, start);
  15742. // Require at least one white space after the `@let`.
  15743. if (isWhitespace(this._cursor.peek())) {
  15744. this._attemptCharCodeUntilFn(isNotWhitespace);
  15745. }
  15746. else {
  15747. const token = this._endToken([this._cursor.getChars(start)]);
  15748. token.type = 32 /* TokenType.INCOMPLETE_LET */;
  15749. return;
  15750. }
  15751. const startToken = this._endToken([this._getLetDeclarationName()]);
  15752. // Skip over white space before the equals character.
  15753. this._attemptCharCodeUntilFn(isNotWhitespace);
  15754. // Expect an equals sign.
  15755. if (!this._attemptCharCode($EQ)) {
  15756. startToken.type = 32 /* TokenType.INCOMPLETE_LET */;
  15757. return;
  15758. }
  15759. // Skip spaces after the equals.
  15760. this._attemptCharCodeUntilFn((code) => isNotWhitespace(code) && !isNewLine(code));
  15761. this._consumeLetDeclarationValue();
  15762. // Terminate the `@let` with a semicolon.
  15763. const endChar = this._cursor.peek();
  15764. if (endChar === $SEMICOLON) {
  15765. this._beginToken(31 /* TokenType.LET_END */);
  15766. this._endToken([]);
  15767. this._cursor.advance();
  15768. }
  15769. else {
  15770. startToken.type = 32 /* TokenType.INCOMPLETE_LET */;
  15771. startToken.sourceSpan = this._cursor.getSpan(start);
  15772. }
  15773. }
  15774. _getLetDeclarationName() {
  15775. const nameCursor = this._cursor.clone();
  15776. let allowDigit = false;
  15777. this._attemptCharCodeUntilFn((code) => {
  15778. if (isAsciiLetter(code) ||
  15779. code === $$ ||
  15780. code === $_ ||
  15781. // `@let` names can't start with a digit, but digits are valid anywhere else in the name.
  15782. (allowDigit && isDigit(code))) {
  15783. allowDigit = true;
  15784. return false;
  15785. }
  15786. return true;
  15787. });
  15788. return this._cursor.getChars(nameCursor).trim();
  15789. }
  15790. _consumeLetDeclarationValue() {
  15791. const start = this._cursor.clone();
  15792. this._beginToken(30 /* TokenType.LET_VALUE */, start);
  15793. while (this._cursor.peek() !== $EOF) {
  15794. const char = this._cursor.peek();
  15795. // `@let` declarations terminate with a semicolon.
  15796. if (char === $SEMICOLON) {
  15797. break;
  15798. }
  15799. // If we hit a quote, skip over its content since we don't care what's inside.
  15800. if (isQuote(char)) {
  15801. this._cursor.advance();
  15802. this._attemptCharCodeUntilFn((inner) => {
  15803. if (inner === $BACKSLASH) {
  15804. this._cursor.advance();
  15805. return false;
  15806. }
  15807. return inner === char;
  15808. });
  15809. }
  15810. this._cursor.advance();
  15811. }
  15812. this._endToken([this._cursor.getChars(start)]);
  15813. }
  15814. /**
  15815. * @returns whether an ICU token has been created
  15816. * @internal
  15817. */
  15818. _tokenizeExpansionForm() {
  15819. if (this.isExpansionFormStart()) {
  15820. this._consumeExpansionFormStart();
  15821. return true;
  15822. }
  15823. if (isExpansionCaseStart(this._cursor.peek()) && this._isInExpansionForm()) {
  15824. this._consumeExpansionCaseStart();
  15825. return true;
  15826. }
  15827. if (this._cursor.peek() === $RBRACE) {
  15828. if (this._isInExpansionCase()) {
  15829. this._consumeExpansionCaseEnd();
  15830. return true;
  15831. }
  15832. if (this._isInExpansionForm()) {
  15833. this._consumeExpansionFormEnd();
  15834. return true;
  15835. }
  15836. }
  15837. return false;
  15838. }
  15839. _beginToken(type, start = this._cursor.clone()) {
  15840. this._currentTokenStart = start;
  15841. this._currentTokenType = type;
  15842. }
  15843. _endToken(parts, end) {
  15844. if (this._currentTokenStart === null) {
  15845. throw new TokenError('Programming error - attempted to end a token when there was no start to the token', this._currentTokenType, this._cursor.getSpan(end));
  15846. }
  15847. if (this._currentTokenType === null) {
  15848. throw new TokenError('Programming error - attempted to end a token which has no token type', null, this._cursor.getSpan(this._currentTokenStart));
  15849. }
  15850. const token = {
  15851. type: this._currentTokenType,
  15852. parts,
  15853. sourceSpan: (end ?? this._cursor).getSpan(this._currentTokenStart, this._leadingTriviaCodePoints),
  15854. };
  15855. this.tokens.push(token);
  15856. this._currentTokenStart = null;
  15857. this._currentTokenType = null;
  15858. return token;
  15859. }
  15860. _createError(msg, span) {
  15861. if (this._isInExpansionForm()) {
  15862. msg += ` (Do you have an unescaped "{" in your template? Use "{{ '{' }}") to escape it.)`;
  15863. }
  15864. const error = new TokenError(msg, this._currentTokenType, span);
  15865. this._currentTokenStart = null;
  15866. this._currentTokenType = null;
  15867. return new _ControlFlowError(error);
  15868. }
  15869. handleError(e) {
  15870. if (e instanceof CursorError) {
  15871. e = this._createError(e.msg, this._cursor.getSpan(e.cursor));
  15872. }
  15873. if (e instanceof _ControlFlowError) {
  15874. this.errors.push(e.error);
  15875. }
  15876. else {
  15877. throw e;
  15878. }
  15879. }
  15880. _attemptCharCode(charCode) {
  15881. if (this._cursor.peek() === charCode) {
  15882. this._cursor.advance();
  15883. return true;
  15884. }
  15885. return false;
  15886. }
  15887. _attemptCharCodeCaseInsensitive(charCode) {
  15888. if (compareCharCodeCaseInsensitive(this._cursor.peek(), charCode)) {
  15889. this._cursor.advance();
  15890. return true;
  15891. }
  15892. return false;
  15893. }
  15894. _requireCharCode(charCode) {
  15895. const location = this._cursor.clone();
  15896. if (!this._attemptCharCode(charCode)) {
  15897. throw this._createError(_unexpectedCharacterErrorMsg(this._cursor.peek()), this._cursor.getSpan(location));
  15898. }
  15899. }
  15900. _attemptStr(chars) {
  15901. const len = chars.length;
  15902. if (this._cursor.charsLeft() < len) {
  15903. return false;
  15904. }
  15905. const initialPosition = this._cursor.clone();
  15906. for (let i = 0; i < len; i++) {
  15907. if (!this._attemptCharCode(chars.charCodeAt(i))) {
  15908. // If attempting to parse the string fails, we want to reset the parser
  15909. // to where it was before the attempt
  15910. this._cursor = initialPosition;
  15911. return false;
  15912. }
  15913. }
  15914. return true;
  15915. }
  15916. _attemptStrCaseInsensitive(chars) {
  15917. for (let i = 0; i < chars.length; i++) {
  15918. if (!this._attemptCharCodeCaseInsensitive(chars.charCodeAt(i))) {
  15919. return false;
  15920. }
  15921. }
  15922. return true;
  15923. }
  15924. _requireStr(chars) {
  15925. const location = this._cursor.clone();
  15926. if (!this._attemptStr(chars)) {
  15927. throw this._createError(_unexpectedCharacterErrorMsg(this._cursor.peek()), this._cursor.getSpan(location));
  15928. }
  15929. }
  15930. _attemptCharCodeUntilFn(predicate) {
  15931. while (!predicate(this._cursor.peek())) {
  15932. this._cursor.advance();
  15933. }
  15934. }
  15935. _requireCharCodeUntilFn(predicate, len) {
  15936. const start = this._cursor.clone();
  15937. this._attemptCharCodeUntilFn(predicate);
  15938. if (this._cursor.diff(start) < len) {
  15939. throw this._createError(_unexpectedCharacterErrorMsg(this._cursor.peek()), this._cursor.getSpan(start));
  15940. }
  15941. }
  15942. _attemptUntilChar(char) {
  15943. while (this._cursor.peek() !== char) {
  15944. this._cursor.advance();
  15945. }
  15946. }
  15947. _readChar() {
  15948. // Don't rely upon reading directly from `_input` as the actual char value
  15949. // may have been generated from an escape sequence.
  15950. const char = String.fromCodePoint(this._cursor.peek());
  15951. this._cursor.advance();
  15952. return char;
  15953. }
  15954. _consumeEntity(textTokenType) {
  15955. this._beginToken(9 /* TokenType.ENCODED_ENTITY */);
  15956. const start = this._cursor.clone();
  15957. this._cursor.advance();
  15958. if (this._attemptCharCode($HASH)) {
  15959. const isHex = this._attemptCharCode($x) || this._attemptCharCode($X);
  15960. const codeStart = this._cursor.clone();
  15961. this._attemptCharCodeUntilFn(isDigitEntityEnd);
  15962. if (this._cursor.peek() != $SEMICOLON) {
  15963. // Advance cursor to include the peeked character in the string provided to the error
  15964. // message.
  15965. this._cursor.advance();
  15966. const entityType = isHex ? CharacterReferenceType.HEX : CharacterReferenceType.DEC;
  15967. throw this._createError(_unparsableEntityErrorMsg(entityType, this._cursor.getChars(start)), this._cursor.getSpan());
  15968. }
  15969. const strNum = this._cursor.getChars(codeStart);
  15970. this._cursor.advance();
  15971. try {
  15972. const charCode = parseInt(strNum, isHex ? 16 : 10);
  15973. this._endToken([String.fromCharCode(charCode), this._cursor.getChars(start)]);
  15974. }
  15975. catch {
  15976. throw this._createError(_unknownEntityErrorMsg(this._cursor.getChars(start)), this._cursor.getSpan());
  15977. }
  15978. }
  15979. else {
  15980. const nameStart = this._cursor.clone();
  15981. this._attemptCharCodeUntilFn(isNamedEntityEnd);
  15982. if (this._cursor.peek() != $SEMICOLON) {
  15983. // No semicolon was found so abort the encoded entity token that was in progress, and treat
  15984. // this as a text token
  15985. this._beginToken(textTokenType, start);
  15986. this._cursor = nameStart;
  15987. this._endToken(['&']);
  15988. }
  15989. else {
  15990. const name = this._cursor.getChars(nameStart);
  15991. this._cursor.advance();
  15992. const char = NAMED_ENTITIES[name];
  15993. if (!char) {
  15994. throw this._createError(_unknownEntityErrorMsg(name), this._cursor.getSpan(start));
  15995. }
  15996. this._endToken([char, `&${name};`]);
  15997. }
  15998. }
  15999. }
  16000. _consumeRawText(consumeEntities, endMarkerPredicate) {
  16001. this._beginToken(consumeEntities ? 6 /* TokenType.ESCAPABLE_RAW_TEXT */ : 7 /* TokenType.RAW_TEXT */);
  16002. const parts = [];
  16003. while (true) {
  16004. const tagCloseStart = this._cursor.clone();
  16005. const foundEndMarker = endMarkerPredicate();
  16006. this._cursor = tagCloseStart;
  16007. if (foundEndMarker) {
  16008. break;
  16009. }
  16010. if (consumeEntities && this._cursor.peek() === $AMPERSAND) {
  16011. this._endToken([this._processCarriageReturns(parts.join(''))]);
  16012. parts.length = 0;
  16013. this._consumeEntity(6 /* TokenType.ESCAPABLE_RAW_TEXT */);
  16014. this._beginToken(6 /* TokenType.ESCAPABLE_RAW_TEXT */);
  16015. }
  16016. else {
  16017. parts.push(this._readChar());
  16018. }
  16019. }
  16020. this._endToken([this._processCarriageReturns(parts.join(''))]);
  16021. }
  16022. _consumeComment(start) {
  16023. this._beginToken(10 /* TokenType.COMMENT_START */, start);
  16024. this._requireCharCode($MINUS);
  16025. this._endToken([]);
  16026. this._consumeRawText(false, () => this._attemptStr('-->'));
  16027. this._beginToken(11 /* TokenType.COMMENT_END */);
  16028. this._requireStr('-->');
  16029. this._endToken([]);
  16030. }
  16031. _consumeCdata(start) {
  16032. this._beginToken(12 /* TokenType.CDATA_START */, start);
  16033. this._requireStr('CDATA[');
  16034. this._endToken([]);
  16035. this._consumeRawText(false, () => this._attemptStr(']]>'));
  16036. this._beginToken(13 /* TokenType.CDATA_END */);
  16037. this._requireStr(']]>');
  16038. this._endToken([]);
  16039. }
  16040. _consumeDocType(start) {
  16041. this._beginToken(18 /* TokenType.DOC_TYPE */, start);
  16042. const contentStart = this._cursor.clone();
  16043. this._attemptUntilChar($GT);
  16044. const content = this._cursor.getChars(contentStart);
  16045. this._cursor.advance();
  16046. this._endToken([content]);
  16047. }
  16048. _consumePrefixAndName() {
  16049. const nameOrPrefixStart = this._cursor.clone();
  16050. let prefix = '';
  16051. while (this._cursor.peek() !== $COLON && !isPrefixEnd(this._cursor.peek())) {
  16052. this._cursor.advance();
  16053. }
  16054. let nameStart;
  16055. if (this._cursor.peek() === $COLON) {
  16056. prefix = this._cursor.getChars(nameOrPrefixStart);
  16057. this._cursor.advance();
  16058. nameStart = this._cursor.clone();
  16059. }
  16060. else {
  16061. nameStart = nameOrPrefixStart;
  16062. }
  16063. this._requireCharCodeUntilFn(isNameEnd, prefix === '' ? 0 : 1);
  16064. const name = this._cursor.getChars(nameStart);
  16065. return [prefix, name];
  16066. }
  16067. _consumeTagOpen(start) {
  16068. let tagName;
  16069. let prefix;
  16070. let openTagToken;
  16071. try {
  16072. if (!isAsciiLetter(this._cursor.peek())) {
  16073. throw this._createError(_unexpectedCharacterErrorMsg(this._cursor.peek()), this._cursor.getSpan(start));
  16074. }
  16075. openTagToken = this._consumeTagOpenStart(start);
  16076. prefix = openTagToken.parts[0];
  16077. tagName = openTagToken.parts[1];
  16078. this._attemptCharCodeUntilFn(isNotWhitespace);
  16079. while (this._cursor.peek() !== $SLASH &&
  16080. this._cursor.peek() !== $GT &&
  16081. this._cursor.peek() !== $LT &&
  16082. this._cursor.peek() !== $EOF) {
  16083. this._consumeAttributeName();
  16084. this._attemptCharCodeUntilFn(isNotWhitespace);
  16085. if (this._attemptCharCode($EQ)) {
  16086. this._attemptCharCodeUntilFn(isNotWhitespace);
  16087. this._consumeAttributeValue();
  16088. }
  16089. this._attemptCharCodeUntilFn(isNotWhitespace);
  16090. }
  16091. this._consumeTagOpenEnd();
  16092. }
  16093. catch (e) {
  16094. if (e instanceof _ControlFlowError) {
  16095. if (openTagToken) {
  16096. // We errored before we could close the opening tag, so it is incomplete.
  16097. openTagToken.type = 4 /* TokenType.INCOMPLETE_TAG_OPEN */;
  16098. }
  16099. else {
  16100. // When the start tag is invalid, assume we want a "<" as text.
  16101. // Back to back text tokens are merged at the end.
  16102. this._beginToken(5 /* TokenType.TEXT */, start);
  16103. this._endToken(['<']);
  16104. }
  16105. return;
  16106. }
  16107. throw e;
  16108. }
  16109. const contentTokenType = this._getTagDefinition(tagName).getContentType(prefix);
  16110. if (contentTokenType === TagContentType.RAW_TEXT) {
  16111. this._consumeRawTextWithTagClose(prefix, tagName, false);
  16112. }
  16113. else if (contentTokenType === TagContentType.ESCAPABLE_RAW_TEXT) {
  16114. this._consumeRawTextWithTagClose(prefix, tagName, true);
  16115. }
  16116. }
  16117. _consumeRawTextWithTagClose(prefix, tagName, consumeEntities) {
  16118. this._consumeRawText(consumeEntities, () => {
  16119. if (!this._attemptCharCode($LT))
  16120. return false;
  16121. if (!this._attemptCharCode($SLASH))
  16122. return false;
  16123. this._attemptCharCodeUntilFn(isNotWhitespace);
  16124. if (!this._attemptStrCaseInsensitive(tagName))
  16125. return false;
  16126. this._attemptCharCodeUntilFn(isNotWhitespace);
  16127. return this._attemptCharCode($GT);
  16128. });
  16129. this._beginToken(3 /* TokenType.TAG_CLOSE */);
  16130. this._requireCharCodeUntilFn((code) => code === $GT, 3);
  16131. this._cursor.advance(); // Consume the `>`
  16132. this._endToken([prefix, tagName]);
  16133. }
  16134. _consumeTagOpenStart(start) {
  16135. this._beginToken(0 /* TokenType.TAG_OPEN_START */, start);
  16136. const parts = this._consumePrefixAndName();
  16137. return this._endToken(parts);
  16138. }
  16139. _consumeAttributeName() {
  16140. const attrNameStart = this._cursor.peek();
  16141. if (attrNameStart === $SQ || attrNameStart === $DQ) {
  16142. throw this._createError(_unexpectedCharacterErrorMsg(attrNameStart), this._cursor.getSpan());
  16143. }
  16144. this._beginToken(14 /* TokenType.ATTR_NAME */);
  16145. const prefixAndName = this._consumePrefixAndName();
  16146. this._endToken(prefixAndName);
  16147. }
  16148. _consumeAttributeValue() {
  16149. if (this._cursor.peek() === $SQ || this._cursor.peek() === $DQ) {
  16150. const quoteChar = this._cursor.peek();
  16151. this._consumeQuote(quoteChar);
  16152. // In an attribute then end of the attribute value and the premature end to an interpolation
  16153. // are both triggered by the `quoteChar`.
  16154. const endPredicate = () => this._cursor.peek() === quoteChar;
  16155. this._consumeWithInterpolation(16 /* TokenType.ATTR_VALUE_TEXT */, 17 /* TokenType.ATTR_VALUE_INTERPOLATION */, endPredicate, endPredicate);
  16156. this._consumeQuote(quoteChar);
  16157. }
  16158. else {
  16159. const endPredicate = () => isNameEnd(this._cursor.peek());
  16160. this._consumeWithInterpolation(16 /* TokenType.ATTR_VALUE_TEXT */, 17 /* TokenType.ATTR_VALUE_INTERPOLATION */, endPredicate, endPredicate);
  16161. }
  16162. }
  16163. _consumeQuote(quoteChar) {
  16164. this._beginToken(15 /* TokenType.ATTR_QUOTE */);
  16165. this._requireCharCode(quoteChar);
  16166. this._endToken([String.fromCodePoint(quoteChar)]);
  16167. }
  16168. _consumeTagOpenEnd() {
  16169. const tokenType = this._attemptCharCode($SLASH)
  16170. ? 2 /* TokenType.TAG_OPEN_END_VOID */
  16171. : 1 /* TokenType.TAG_OPEN_END */;
  16172. this._beginToken(tokenType);
  16173. this._requireCharCode($GT);
  16174. this._endToken([]);
  16175. }
  16176. _consumeTagClose(start) {
  16177. this._beginToken(3 /* TokenType.TAG_CLOSE */, start);
  16178. this._attemptCharCodeUntilFn(isNotWhitespace);
  16179. const prefixAndName = this._consumePrefixAndName();
  16180. this._attemptCharCodeUntilFn(isNotWhitespace);
  16181. this._requireCharCode($GT);
  16182. this._endToken(prefixAndName);
  16183. }
  16184. _consumeExpansionFormStart() {
  16185. this._beginToken(19 /* TokenType.EXPANSION_FORM_START */);
  16186. this._requireCharCode($LBRACE);
  16187. this._endToken([]);
  16188. this._expansionCaseStack.push(19 /* TokenType.EXPANSION_FORM_START */);
  16189. this._beginToken(7 /* TokenType.RAW_TEXT */);
  16190. const condition = this._readUntil($COMMA);
  16191. const normalizedCondition = this._processCarriageReturns(condition);
  16192. if (this._i18nNormalizeLineEndingsInICUs) {
  16193. // We explicitly want to normalize line endings for this text.
  16194. this._endToken([normalizedCondition]);
  16195. }
  16196. else {
  16197. // We are not normalizing line endings.
  16198. const conditionToken = this._endToken([condition]);
  16199. if (normalizedCondition !== condition) {
  16200. this.nonNormalizedIcuExpressions.push(conditionToken);
  16201. }
  16202. }
  16203. this._requireCharCode($COMMA);
  16204. this._attemptCharCodeUntilFn(isNotWhitespace);
  16205. this._beginToken(7 /* TokenType.RAW_TEXT */);
  16206. const type = this._readUntil($COMMA);
  16207. this._endToken([type]);
  16208. this._requireCharCode($COMMA);
  16209. this._attemptCharCodeUntilFn(isNotWhitespace);
  16210. }
  16211. _consumeExpansionCaseStart() {
  16212. this._beginToken(20 /* TokenType.EXPANSION_CASE_VALUE */);
  16213. const value = this._readUntil($LBRACE).trim();
  16214. this._endToken([value]);
  16215. this._attemptCharCodeUntilFn(isNotWhitespace);
  16216. this._beginToken(21 /* TokenType.EXPANSION_CASE_EXP_START */);
  16217. this._requireCharCode($LBRACE);
  16218. this._endToken([]);
  16219. this._attemptCharCodeUntilFn(isNotWhitespace);
  16220. this._expansionCaseStack.push(21 /* TokenType.EXPANSION_CASE_EXP_START */);
  16221. }
  16222. _consumeExpansionCaseEnd() {
  16223. this._beginToken(22 /* TokenType.EXPANSION_CASE_EXP_END */);
  16224. this._requireCharCode($RBRACE);
  16225. this._endToken([]);
  16226. this._attemptCharCodeUntilFn(isNotWhitespace);
  16227. this._expansionCaseStack.pop();
  16228. }
  16229. _consumeExpansionFormEnd() {
  16230. this._beginToken(23 /* TokenType.EXPANSION_FORM_END */);
  16231. this._requireCharCode($RBRACE);
  16232. this._endToken([]);
  16233. this._expansionCaseStack.pop();
  16234. }
  16235. /**
  16236. * Consume a string that may contain interpolation expressions.
  16237. *
  16238. * The first token consumed will be of `tokenType` and then there will be alternating
  16239. * `interpolationTokenType` and `tokenType` tokens until the `endPredicate()` returns true.
  16240. *
  16241. * If an interpolation token ends prematurely it will have no end marker in its `parts` array.
  16242. *
  16243. * @param textTokenType the kind of tokens to interleave around interpolation tokens.
  16244. * @param interpolationTokenType the kind of tokens that contain interpolation.
  16245. * @param endPredicate a function that should return true when we should stop consuming.
  16246. * @param endInterpolation a function that should return true if there is a premature end to an
  16247. * interpolation expression - i.e. before we get to the normal interpolation closing marker.
  16248. */
  16249. _consumeWithInterpolation(textTokenType, interpolationTokenType, endPredicate, endInterpolation) {
  16250. this._beginToken(textTokenType);
  16251. const parts = [];
  16252. while (!endPredicate()) {
  16253. const current = this._cursor.clone();
  16254. if (this._interpolationConfig && this._attemptStr(this._interpolationConfig.start)) {
  16255. this._endToken([this._processCarriageReturns(parts.join(''))], current);
  16256. parts.length = 0;
  16257. this._consumeInterpolation(interpolationTokenType, current, endInterpolation);
  16258. this._beginToken(textTokenType);
  16259. }
  16260. else if (this._cursor.peek() === $AMPERSAND) {
  16261. this._endToken([this._processCarriageReturns(parts.join(''))]);
  16262. parts.length = 0;
  16263. this._consumeEntity(textTokenType);
  16264. this._beginToken(textTokenType);
  16265. }
  16266. else {
  16267. parts.push(this._readChar());
  16268. }
  16269. }
  16270. // It is possible that an interpolation was started but not ended inside this text token.
  16271. // Make sure that we reset the state of the lexer correctly.
  16272. this._inInterpolation = false;
  16273. this._endToken([this._processCarriageReturns(parts.join(''))]);
  16274. }
  16275. /**
  16276. * Consume a block of text that has been interpreted as an Angular interpolation.
  16277. *
  16278. * @param interpolationTokenType the type of the interpolation token to generate.
  16279. * @param interpolationStart a cursor that points to the start of this interpolation.
  16280. * @param prematureEndPredicate a function that should return true if the next characters indicate
  16281. * an end to the interpolation before its normal closing marker.
  16282. */
  16283. _consumeInterpolation(interpolationTokenType, interpolationStart, prematureEndPredicate) {
  16284. const parts = [];
  16285. this._beginToken(interpolationTokenType, interpolationStart);
  16286. parts.push(this._interpolationConfig.start);
  16287. // Find the end of the interpolation, ignoring content inside quotes.
  16288. const expressionStart = this._cursor.clone();
  16289. let inQuote = null;
  16290. let inComment = false;
  16291. while (this._cursor.peek() !== $EOF &&
  16292. (prematureEndPredicate === null || !prematureEndPredicate())) {
  16293. const current = this._cursor.clone();
  16294. if (this._isTagStart()) {
  16295. // We are starting what looks like an HTML element in the middle of this interpolation.
  16296. // Reset the cursor to before the `<` character and end the interpolation token.
  16297. // (This is actually wrong but here for backward compatibility).
  16298. this._cursor = current;
  16299. parts.push(this._getProcessedChars(expressionStart, current));
  16300. this._endToken(parts);
  16301. return;
  16302. }
  16303. if (inQuote === null) {
  16304. if (this._attemptStr(this._interpolationConfig.end)) {
  16305. // We are not in a string, and we hit the end interpolation marker
  16306. parts.push(this._getProcessedChars(expressionStart, current));
  16307. parts.push(this._interpolationConfig.end);
  16308. this._endToken(parts);
  16309. return;
  16310. }
  16311. else if (this._attemptStr('//')) {
  16312. // Once we are in a comment we ignore any quotes
  16313. inComment = true;
  16314. }
  16315. }
  16316. const char = this._cursor.peek();
  16317. this._cursor.advance();
  16318. if (char === $BACKSLASH) {
  16319. // Skip the next character because it was escaped.
  16320. this._cursor.advance();
  16321. }
  16322. else if (char === inQuote) {
  16323. // Exiting the current quoted string
  16324. inQuote = null;
  16325. }
  16326. else if (!inComment && inQuote === null && isQuote(char)) {
  16327. // Entering a new quoted string
  16328. inQuote = char;
  16329. }
  16330. }
  16331. // We hit EOF without finding a closing interpolation marker
  16332. parts.push(this._getProcessedChars(expressionStart, this._cursor));
  16333. this._endToken(parts);
  16334. }
  16335. _getProcessedChars(start, end) {
  16336. return this._processCarriageReturns(end.getChars(start));
  16337. }
  16338. _isTextEnd() {
  16339. if (this._isTagStart() || this._cursor.peek() === $EOF) {
  16340. return true;
  16341. }
  16342. if (this._tokenizeIcu && !this._inInterpolation) {
  16343. if (this.isExpansionFormStart()) {
  16344. // start of an expansion form
  16345. return true;
  16346. }
  16347. if (this._cursor.peek() === $RBRACE && this._isInExpansionCase()) {
  16348. // end of and expansion case
  16349. return true;
  16350. }
  16351. }
  16352. if (this._tokenizeBlocks &&
  16353. !this._inInterpolation &&
  16354. !this._isInExpansion() &&
  16355. (this._cursor.peek() === $AT || this._cursor.peek() === $RBRACE)) {
  16356. return true;
  16357. }
  16358. return false;
  16359. }
  16360. /**
  16361. * Returns true if the current cursor is pointing to the start of a tag
  16362. * (opening/closing/comments/cdata/etc).
  16363. */
  16364. _isTagStart() {
  16365. if (this._cursor.peek() === $LT) {
  16366. // We assume that `<` followed by whitespace is not the start of an HTML element.
  16367. const tmp = this._cursor.clone();
  16368. tmp.advance();
  16369. // If the next character is alphabetic, ! nor / then it is a tag start
  16370. const code = tmp.peek();
  16371. if (($a <= code && code <= $z) ||
  16372. ($A <= code && code <= $Z) ||
  16373. code === $SLASH ||
  16374. code === $BANG) {
  16375. return true;
  16376. }
  16377. }
  16378. return false;
  16379. }
  16380. _readUntil(char) {
  16381. const start = this._cursor.clone();
  16382. this._attemptUntilChar(char);
  16383. return this._cursor.getChars(start);
  16384. }
  16385. _isInExpansion() {
  16386. return this._isInExpansionCase() || this._isInExpansionForm();
  16387. }
  16388. _isInExpansionCase() {
  16389. return (this._expansionCaseStack.length > 0 &&
  16390. this._expansionCaseStack[this._expansionCaseStack.length - 1] ===
  16391. 21 /* TokenType.EXPANSION_CASE_EXP_START */);
  16392. }
  16393. _isInExpansionForm() {
  16394. return (this._expansionCaseStack.length > 0 &&
  16395. this._expansionCaseStack[this._expansionCaseStack.length - 1] ===
  16396. 19 /* TokenType.EXPANSION_FORM_START */);
  16397. }
  16398. isExpansionFormStart() {
  16399. if (this._cursor.peek() !== $LBRACE) {
  16400. return false;
  16401. }
  16402. if (this._interpolationConfig) {
  16403. const start = this._cursor.clone();
  16404. const isInterpolation = this._attemptStr(this._interpolationConfig.start);
  16405. this._cursor = start;
  16406. return !isInterpolation;
  16407. }
  16408. return true;
  16409. }
  16410. }
  16411. function isNotWhitespace(code) {
  16412. return !isWhitespace(code) || code === $EOF;
  16413. }
  16414. function isNameEnd(code) {
  16415. return (isWhitespace(code) ||
  16416. code === $GT ||
  16417. code === $LT ||
  16418. code === $SLASH ||
  16419. code === $SQ ||
  16420. code === $DQ ||
  16421. code === $EQ ||
  16422. code === $EOF);
  16423. }
  16424. function isPrefixEnd(code) {
  16425. return ((code < $a || $z < code) &&
  16426. (code < $A || $Z < code) &&
  16427. (code < $0 || code > $9));
  16428. }
  16429. function isDigitEntityEnd(code) {
  16430. return code === $SEMICOLON || code === $EOF || !isAsciiHexDigit(code);
  16431. }
  16432. function isNamedEntityEnd(code) {
  16433. return code === $SEMICOLON || code === $EOF || !isAsciiLetter(code);
  16434. }
  16435. function isExpansionCaseStart(peek) {
  16436. return peek !== $RBRACE;
  16437. }
  16438. function compareCharCodeCaseInsensitive(code1, code2) {
  16439. return toUpperCaseCharCode(code1) === toUpperCaseCharCode(code2);
  16440. }
  16441. function toUpperCaseCharCode(code) {
  16442. return code >= $a && code <= $z ? code - $a + $A : code;
  16443. }
  16444. function isBlockNameChar(code) {
  16445. return isAsciiLetter(code) || isDigit(code) || code === $_;
  16446. }
  16447. function isBlockParameterChar(code) {
  16448. return code !== $SEMICOLON && isNotWhitespace(code);
  16449. }
  16450. function mergeTextTokens(srcTokens) {
  16451. const dstTokens = [];
  16452. let lastDstToken = undefined;
  16453. for (let i = 0; i < srcTokens.length; i++) {
  16454. const token = srcTokens[i];
  16455. if ((lastDstToken && lastDstToken.type === 5 /* TokenType.TEXT */ && token.type === 5 /* TokenType.TEXT */) ||
  16456. (lastDstToken &&
  16457. lastDstToken.type === 16 /* TokenType.ATTR_VALUE_TEXT */ &&
  16458. token.type === 16 /* TokenType.ATTR_VALUE_TEXT */)) {
  16459. lastDstToken.parts[0] += token.parts[0];
  16460. lastDstToken.sourceSpan.end = token.sourceSpan.end;
  16461. }
  16462. else {
  16463. lastDstToken = token;
  16464. dstTokens.push(lastDstToken);
  16465. }
  16466. }
  16467. return dstTokens;
  16468. }
  16469. class PlainCharacterCursor {
  16470. state;
  16471. file;
  16472. input;
  16473. end;
  16474. constructor(fileOrCursor, range) {
  16475. if (fileOrCursor instanceof PlainCharacterCursor) {
  16476. this.file = fileOrCursor.file;
  16477. this.input = fileOrCursor.input;
  16478. this.end = fileOrCursor.end;
  16479. const state = fileOrCursor.state;
  16480. // Note: avoid using `{...fileOrCursor.state}` here as that has a severe performance penalty.
  16481. // In ES5 bundles the object spread operator is translated into the `__assign` helper, which
  16482. // is not optimized by VMs as efficiently as a raw object literal. Since this constructor is
  16483. // called in tight loops, this difference matters.
  16484. this.state = {
  16485. peek: state.peek,
  16486. offset: state.offset,
  16487. line: state.line,
  16488. column: state.column,
  16489. };
  16490. }
  16491. else {
  16492. if (!range) {
  16493. throw new Error('Programming error: the range argument must be provided with a file argument.');
  16494. }
  16495. this.file = fileOrCursor;
  16496. this.input = fileOrCursor.content;
  16497. this.end = range.endPos;
  16498. this.state = {
  16499. peek: -1,
  16500. offset: range.startPos,
  16501. line: range.startLine,
  16502. column: range.startCol,
  16503. };
  16504. }
  16505. }
  16506. clone() {
  16507. return new PlainCharacterCursor(this);
  16508. }
  16509. peek() {
  16510. return this.state.peek;
  16511. }
  16512. charsLeft() {
  16513. return this.end - this.state.offset;
  16514. }
  16515. diff(other) {
  16516. return this.state.offset - other.state.offset;
  16517. }
  16518. advance() {
  16519. this.advanceState(this.state);
  16520. }
  16521. init() {
  16522. this.updatePeek(this.state);
  16523. }
  16524. getSpan(start, leadingTriviaCodePoints) {
  16525. start = start || this;
  16526. let fullStart = start;
  16527. if (leadingTriviaCodePoints) {
  16528. while (this.diff(start) > 0 && leadingTriviaCodePoints.indexOf(start.peek()) !== -1) {
  16529. if (fullStart === start) {
  16530. start = start.clone();
  16531. }
  16532. start.advance();
  16533. }
  16534. }
  16535. const startLocation = this.locationFromCursor(start);
  16536. const endLocation = this.locationFromCursor(this);
  16537. const fullStartLocation = fullStart !== start ? this.locationFromCursor(fullStart) : startLocation;
  16538. return new ParseSourceSpan(startLocation, endLocation, fullStartLocation);
  16539. }
  16540. getChars(start) {
  16541. return this.input.substring(start.state.offset, this.state.offset);
  16542. }
  16543. charAt(pos) {
  16544. return this.input.charCodeAt(pos);
  16545. }
  16546. advanceState(state) {
  16547. if (state.offset >= this.end) {
  16548. this.state = state;
  16549. throw new CursorError('Unexpected character "EOF"', this);
  16550. }
  16551. const currentChar = this.charAt(state.offset);
  16552. if (currentChar === $LF) {
  16553. state.line++;
  16554. state.column = 0;
  16555. }
  16556. else if (!isNewLine(currentChar)) {
  16557. state.column++;
  16558. }
  16559. state.offset++;
  16560. this.updatePeek(state);
  16561. }
  16562. updatePeek(state) {
  16563. state.peek = state.offset >= this.end ? $EOF : this.charAt(state.offset);
  16564. }
  16565. locationFromCursor(cursor) {
  16566. return new ParseLocation(cursor.file, cursor.state.offset, cursor.state.line, cursor.state.column);
  16567. }
  16568. }
  16569. class EscapedCharacterCursor extends PlainCharacterCursor {
  16570. internalState;
  16571. constructor(fileOrCursor, range) {
  16572. if (fileOrCursor instanceof EscapedCharacterCursor) {
  16573. super(fileOrCursor);
  16574. this.internalState = { ...fileOrCursor.internalState };
  16575. }
  16576. else {
  16577. super(fileOrCursor, range);
  16578. this.internalState = this.state;
  16579. }
  16580. }
  16581. advance() {
  16582. this.state = this.internalState;
  16583. super.advance();
  16584. this.processEscapeSequence();
  16585. }
  16586. init() {
  16587. super.init();
  16588. this.processEscapeSequence();
  16589. }
  16590. clone() {
  16591. return new EscapedCharacterCursor(this);
  16592. }
  16593. getChars(start) {
  16594. const cursor = start.clone();
  16595. let chars = '';
  16596. while (cursor.internalState.offset < this.internalState.offset) {
  16597. chars += String.fromCodePoint(cursor.peek());
  16598. cursor.advance();
  16599. }
  16600. return chars;
  16601. }
  16602. /**
  16603. * Process the escape sequence that starts at the current position in the text.
  16604. *
  16605. * This method is called to ensure that `peek` has the unescaped value of escape sequences.
  16606. */
  16607. processEscapeSequence() {
  16608. const peek = () => this.internalState.peek;
  16609. if (peek() === $BACKSLASH) {
  16610. // We have hit an escape sequence so we need the internal state to become independent
  16611. // of the external state.
  16612. this.internalState = { ...this.state };
  16613. // Move past the backslash
  16614. this.advanceState(this.internalState);
  16615. // First check for standard control char sequences
  16616. if (peek() === $n) {
  16617. this.state.peek = $LF;
  16618. }
  16619. else if (peek() === $r) {
  16620. this.state.peek = $CR;
  16621. }
  16622. else if (peek() === $v) {
  16623. this.state.peek = $VTAB;
  16624. }
  16625. else if (peek() === $t) {
  16626. this.state.peek = $TAB;
  16627. }
  16628. else if (peek() === $b) {
  16629. this.state.peek = $BSPACE;
  16630. }
  16631. else if (peek() === $f) {
  16632. this.state.peek = $FF;
  16633. }
  16634. // Now consider more complex sequences
  16635. else if (peek() === $u) {
  16636. // Unicode code-point sequence
  16637. this.advanceState(this.internalState); // advance past the `u` char
  16638. if (peek() === $LBRACE) {
  16639. // Variable length Unicode, e.g. `\x{123}`
  16640. this.advanceState(this.internalState); // advance past the `{` char
  16641. // Advance past the variable number of hex digits until we hit a `}` char
  16642. const digitStart = this.clone();
  16643. let length = 0;
  16644. while (peek() !== $RBRACE) {
  16645. this.advanceState(this.internalState);
  16646. length++;
  16647. }
  16648. this.state.peek = this.decodeHexDigits(digitStart, length);
  16649. }
  16650. else {
  16651. // Fixed length Unicode, e.g. `\u1234`
  16652. const digitStart = this.clone();
  16653. this.advanceState(this.internalState);
  16654. this.advanceState(this.internalState);
  16655. this.advanceState(this.internalState);
  16656. this.state.peek = this.decodeHexDigits(digitStart, 4);
  16657. }
  16658. }
  16659. else if (peek() === $x) {
  16660. // Hex char code, e.g. `\x2F`
  16661. this.advanceState(this.internalState); // advance past the `x` char
  16662. const digitStart = this.clone();
  16663. this.advanceState(this.internalState);
  16664. this.state.peek = this.decodeHexDigits(digitStart, 2);
  16665. }
  16666. else if (isOctalDigit(peek())) {
  16667. // Octal char code, e.g. `\012`,
  16668. let octal = '';
  16669. let length = 0;
  16670. let previous = this.clone();
  16671. while (isOctalDigit(peek()) && length < 3) {
  16672. previous = this.clone();
  16673. octal += String.fromCodePoint(peek());
  16674. this.advanceState(this.internalState);
  16675. length++;
  16676. }
  16677. this.state.peek = parseInt(octal, 8);
  16678. // Backup one char
  16679. this.internalState = previous.internalState;
  16680. }
  16681. else if (isNewLine(this.internalState.peek)) {
  16682. // Line continuation `\` followed by a new line
  16683. this.advanceState(this.internalState); // advance over the newline
  16684. this.state = this.internalState;
  16685. }
  16686. else {
  16687. // If none of the `if` blocks were executed then we just have an escaped normal character.
  16688. // In that case we just, effectively, skip the backslash from the character.
  16689. this.state.peek = this.internalState.peek;
  16690. }
  16691. }
  16692. }
  16693. decodeHexDigits(start, length) {
  16694. const hex = this.input.slice(start.internalState.offset, start.internalState.offset + length);
  16695. const charCode = parseInt(hex, 16);
  16696. if (!isNaN(charCode)) {
  16697. return charCode;
  16698. }
  16699. else {
  16700. start.state = start.internalState;
  16701. throw new CursorError('Invalid hexadecimal escape sequence', start);
  16702. }
  16703. }
  16704. }
  16705. class CursorError {
  16706. msg;
  16707. cursor;
  16708. constructor(msg, cursor) {
  16709. this.msg = msg;
  16710. this.cursor = cursor;
  16711. }
  16712. }
  16713. class TreeError extends ParseError {
  16714. elementName;
  16715. static create(elementName, span, msg) {
  16716. return new TreeError(elementName, span, msg);
  16717. }
  16718. constructor(elementName, span, msg) {
  16719. super(span, msg);
  16720. this.elementName = elementName;
  16721. }
  16722. }
  16723. class ParseTreeResult {
  16724. rootNodes;
  16725. errors;
  16726. constructor(rootNodes, errors) {
  16727. this.rootNodes = rootNodes;
  16728. this.errors = errors;
  16729. }
  16730. }
  16731. let Parser$1 = class Parser {
  16732. getTagDefinition;
  16733. constructor(getTagDefinition) {
  16734. this.getTagDefinition = getTagDefinition;
  16735. }
  16736. parse(source, url, options) {
  16737. const tokenizeResult = tokenize(source, url, this.getTagDefinition, options);
  16738. const parser = new _TreeBuilder(tokenizeResult.tokens, this.getTagDefinition);
  16739. parser.build();
  16740. return new ParseTreeResult(parser.rootNodes, tokenizeResult.errors.concat(parser.errors));
  16741. }
  16742. };
  16743. class _TreeBuilder {
  16744. tokens;
  16745. getTagDefinition;
  16746. _index = -1;
  16747. // `_peek` will be initialized by the call to `_advance()` in the constructor.
  16748. _peek;
  16749. _containerStack = [];
  16750. rootNodes = [];
  16751. errors = [];
  16752. constructor(tokens, getTagDefinition) {
  16753. this.tokens = tokens;
  16754. this.getTagDefinition = getTagDefinition;
  16755. this._advance();
  16756. }
  16757. build() {
  16758. while (this._peek.type !== 33 /* TokenType.EOF */) {
  16759. if (this._peek.type === 0 /* TokenType.TAG_OPEN_START */ ||
  16760. this._peek.type === 4 /* TokenType.INCOMPLETE_TAG_OPEN */) {
  16761. this._consumeStartTag(this._advance());
  16762. }
  16763. else if (this._peek.type === 3 /* TokenType.TAG_CLOSE */) {
  16764. this._consumeEndTag(this._advance());
  16765. }
  16766. else if (this._peek.type === 12 /* TokenType.CDATA_START */) {
  16767. this._closeVoidElement();
  16768. this._consumeCdata(this._advance());
  16769. }
  16770. else if (this._peek.type === 10 /* TokenType.COMMENT_START */) {
  16771. this._closeVoidElement();
  16772. this._consumeComment(this._advance());
  16773. }
  16774. else if (this._peek.type === 5 /* TokenType.TEXT */ ||
  16775. this._peek.type === 7 /* TokenType.RAW_TEXT */ ||
  16776. this._peek.type === 6 /* TokenType.ESCAPABLE_RAW_TEXT */) {
  16777. this._closeVoidElement();
  16778. this._consumeText(this._advance());
  16779. }
  16780. else if (this._peek.type === 19 /* TokenType.EXPANSION_FORM_START */) {
  16781. this._consumeExpansion(this._advance());
  16782. }
  16783. else if (this._peek.type === 24 /* TokenType.BLOCK_OPEN_START */) {
  16784. this._closeVoidElement();
  16785. this._consumeBlockOpen(this._advance());
  16786. }
  16787. else if (this._peek.type === 26 /* TokenType.BLOCK_CLOSE */) {
  16788. this._closeVoidElement();
  16789. this._consumeBlockClose(this._advance());
  16790. }
  16791. else if (this._peek.type === 28 /* TokenType.INCOMPLETE_BLOCK_OPEN */) {
  16792. this._closeVoidElement();
  16793. this._consumeIncompleteBlock(this._advance());
  16794. }
  16795. else if (this._peek.type === 29 /* TokenType.LET_START */) {
  16796. this._closeVoidElement();
  16797. this._consumeLet(this._advance());
  16798. }
  16799. else if (this._peek.type === 32 /* TokenType.INCOMPLETE_LET */) {
  16800. this._closeVoidElement();
  16801. this._consumeIncompleteLet(this._advance());
  16802. }
  16803. else {
  16804. // Skip all other tokens...
  16805. this._advance();
  16806. }
  16807. }
  16808. for (const leftoverContainer of this._containerStack) {
  16809. // Unlike HTML elements, blocks aren't closed implicitly by the end of the file.
  16810. if (leftoverContainer instanceof Block) {
  16811. this.errors.push(TreeError.create(leftoverContainer.name, leftoverContainer.sourceSpan, `Unclosed block "${leftoverContainer.name}"`));
  16812. }
  16813. }
  16814. }
  16815. _advance() {
  16816. const prev = this._peek;
  16817. if (this._index < this.tokens.length - 1) {
  16818. // Note: there is always an EOF token at the end
  16819. this._index++;
  16820. }
  16821. this._peek = this.tokens[this._index];
  16822. return prev;
  16823. }
  16824. _advanceIf(type) {
  16825. if (this._peek.type === type) {
  16826. return this._advance();
  16827. }
  16828. return null;
  16829. }
  16830. _consumeCdata(_startToken) {
  16831. this._consumeText(this._advance());
  16832. this._advanceIf(13 /* TokenType.CDATA_END */);
  16833. }
  16834. _consumeComment(token) {
  16835. const text = this._advanceIf(7 /* TokenType.RAW_TEXT */);
  16836. const endToken = this._advanceIf(11 /* TokenType.COMMENT_END */);
  16837. const value = text != null ? text.parts[0].trim() : null;
  16838. const sourceSpan = endToken == null
  16839. ? token.sourceSpan
  16840. : new ParseSourceSpan(token.sourceSpan.start, endToken.sourceSpan.end, token.sourceSpan.fullStart);
  16841. this._addToParent(new Comment(value, sourceSpan));
  16842. }
  16843. _consumeExpansion(token) {
  16844. const switchValue = this._advance();
  16845. const type = this._advance();
  16846. const cases = [];
  16847. // read =
  16848. while (this._peek.type === 20 /* TokenType.EXPANSION_CASE_VALUE */) {
  16849. const expCase = this._parseExpansionCase();
  16850. if (!expCase)
  16851. return; // error
  16852. cases.push(expCase);
  16853. }
  16854. // read the final }
  16855. if (this._peek.type !== 23 /* TokenType.EXPANSION_FORM_END */) {
  16856. this.errors.push(TreeError.create(null, this._peek.sourceSpan, `Invalid ICU message. Missing '}'.`));
  16857. return;
  16858. }
  16859. const sourceSpan = new ParseSourceSpan(token.sourceSpan.start, this._peek.sourceSpan.end, token.sourceSpan.fullStart);
  16860. this._addToParent(new Expansion(switchValue.parts[0], type.parts[0], cases, sourceSpan, switchValue.sourceSpan));
  16861. this._advance();
  16862. }
  16863. _parseExpansionCase() {
  16864. const value = this._advance();
  16865. // read {
  16866. if (this._peek.type !== 21 /* TokenType.EXPANSION_CASE_EXP_START */) {
  16867. this.errors.push(TreeError.create(null, this._peek.sourceSpan, `Invalid ICU message. Missing '{'.`));
  16868. return null;
  16869. }
  16870. // read until }
  16871. const start = this._advance();
  16872. const exp = this._collectExpansionExpTokens(start);
  16873. if (!exp)
  16874. return null;
  16875. const end = this._advance();
  16876. exp.push({ type: 33 /* TokenType.EOF */, parts: [], sourceSpan: end.sourceSpan });
  16877. // parse everything in between { and }
  16878. const expansionCaseParser = new _TreeBuilder(exp, this.getTagDefinition);
  16879. expansionCaseParser.build();
  16880. if (expansionCaseParser.errors.length > 0) {
  16881. this.errors = this.errors.concat(expansionCaseParser.errors);
  16882. return null;
  16883. }
  16884. const sourceSpan = new ParseSourceSpan(value.sourceSpan.start, end.sourceSpan.end, value.sourceSpan.fullStart);
  16885. const expSourceSpan = new ParseSourceSpan(start.sourceSpan.start, end.sourceSpan.end, start.sourceSpan.fullStart);
  16886. return new ExpansionCase(value.parts[0], expansionCaseParser.rootNodes, sourceSpan, value.sourceSpan, expSourceSpan);
  16887. }
  16888. _collectExpansionExpTokens(start) {
  16889. const exp = [];
  16890. const expansionFormStack = [21 /* TokenType.EXPANSION_CASE_EXP_START */];
  16891. while (true) {
  16892. if (this._peek.type === 19 /* TokenType.EXPANSION_FORM_START */ ||
  16893. this._peek.type === 21 /* TokenType.EXPANSION_CASE_EXP_START */) {
  16894. expansionFormStack.push(this._peek.type);
  16895. }
  16896. if (this._peek.type === 22 /* TokenType.EXPANSION_CASE_EXP_END */) {
  16897. if (lastOnStack(expansionFormStack, 21 /* TokenType.EXPANSION_CASE_EXP_START */)) {
  16898. expansionFormStack.pop();
  16899. if (expansionFormStack.length === 0)
  16900. return exp;
  16901. }
  16902. else {
  16903. this.errors.push(TreeError.create(null, start.sourceSpan, `Invalid ICU message. Missing '}'.`));
  16904. return null;
  16905. }
  16906. }
  16907. if (this._peek.type === 23 /* TokenType.EXPANSION_FORM_END */) {
  16908. if (lastOnStack(expansionFormStack, 19 /* TokenType.EXPANSION_FORM_START */)) {
  16909. expansionFormStack.pop();
  16910. }
  16911. else {
  16912. this.errors.push(TreeError.create(null, start.sourceSpan, `Invalid ICU message. Missing '}'.`));
  16913. return null;
  16914. }
  16915. }
  16916. if (this._peek.type === 33 /* TokenType.EOF */) {
  16917. this.errors.push(TreeError.create(null, start.sourceSpan, `Invalid ICU message. Missing '}'.`));
  16918. return null;
  16919. }
  16920. exp.push(this._advance());
  16921. }
  16922. }
  16923. _consumeText(token) {
  16924. const tokens = [token];
  16925. const startSpan = token.sourceSpan;
  16926. let text = token.parts[0];
  16927. if (text.length > 0 && text[0] === '\n') {
  16928. const parent = this._getContainer();
  16929. if (parent != null &&
  16930. parent.children.length === 0 &&
  16931. this.getTagDefinition(parent.name).ignoreFirstLf) {
  16932. text = text.substring(1);
  16933. tokens[0] = { type: token.type, sourceSpan: token.sourceSpan, parts: [text] };
  16934. }
  16935. }
  16936. while (this._peek.type === 8 /* TokenType.INTERPOLATION */ ||
  16937. this._peek.type === 5 /* TokenType.TEXT */ ||
  16938. this._peek.type === 9 /* TokenType.ENCODED_ENTITY */) {
  16939. token = this._advance();
  16940. tokens.push(token);
  16941. if (token.type === 8 /* TokenType.INTERPOLATION */) {
  16942. // For backward compatibility we decode HTML entities that appear in interpolation
  16943. // expressions. This is arguably a bug, but it could be a considerable breaking change to
  16944. // fix it. It should be addressed in a larger project to refactor the entire parser/lexer
  16945. // chain after View Engine has been removed.
  16946. text += token.parts.join('').replace(/&([^;]+);/g, decodeEntity);
  16947. }
  16948. else if (token.type === 9 /* TokenType.ENCODED_ENTITY */) {
  16949. text += token.parts[0];
  16950. }
  16951. else {
  16952. text += token.parts.join('');
  16953. }
  16954. }
  16955. if (text.length > 0) {
  16956. const endSpan = token.sourceSpan;
  16957. this._addToParent(new Text(text, new ParseSourceSpan(startSpan.start, endSpan.end, startSpan.fullStart, startSpan.details), tokens));
  16958. }
  16959. }
  16960. _closeVoidElement() {
  16961. const el = this._getContainer();
  16962. if (el instanceof Element && this.getTagDefinition(el.name).isVoid) {
  16963. this._containerStack.pop();
  16964. }
  16965. }
  16966. _consumeStartTag(startTagToken) {
  16967. const [prefix, name] = startTagToken.parts;
  16968. const attrs = [];
  16969. while (this._peek.type === 14 /* TokenType.ATTR_NAME */) {
  16970. attrs.push(this._consumeAttr(this._advance()));
  16971. }
  16972. const fullName = this._getElementFullName(prefix, name, this._getClosestParentElement());
  16973. let selfClosing = false;
  16974. // Note: There could have been a tokenizer error
  16975. // so that we don't get a token for the end tag...
  16976. if (this._peek.type === 2 /* TokenType.TAG_OPEN_END_VOID */) {
  16977. this._advance();
  16978. selfClosing = true;
  16979. const tagDef = this.getTagDefinition(fullName);
  16980. if (!(tagDef.canSelfClose || getNsPrefix(fullName) !== null || tagDef.isVoid)) {
  16981. this.errors.push(TreeError.create(fullName, startTagToken.sourceSpan, `Only void, custom and foreign elements can be self closed "${startTagToken.parts[1]}"`));
  16982. }
  16983. }
  16984. else if (this._peek.type === 1 /* TokenType.TAG_OPEN_END */) {
  16985. this._advance();
  16986. selfClosing = false;
  16987. }
  16988. const end = this._peek.sourceSpan.fullStart;
  16989. const span = new ParseSourceSpan(startTagToken.sourceSpan.start, end, startTagToken.sourceSpan.fullStart);
  16990. // Create a separate `startSpan` because `span` will be modified when there is an `end` span.
  16991. const startSpan = new ParseSourceSpan(startTagToken.sourceSpan.start, end, startTagToken.sourceSpan.fullStart);
  16992. const el = new Element(fullName, attrs, [], span, startSpan, undefined);
  16993. const parentEl = this._getContainer();
  16994. this._pushContainer(el, parentEl instanceof Element &&
  16995. this.getTagDefinition(parentEl.name).isClosedByChild(el.name));
  16996. if (selfClosing) {
  16997. // Elements that are self-closed have their `endSourceSpan` set to the full span, as the
  16998. // element start tag also represents the end tag.
  16999. this._popContainer(fullName, Element, span);
  17000. }
  17001. else if (startTagToken.type === 4 /* TokenType.INCOMPLETE_TAG_OPEN */) {
  17002. // We already know the opening tag is not complete, so it is unlikely it has a corresponding
  17003. // close tag. Let's optimistically parse it as a full element and emit an error.
  17004. this._popContainer(fullName, Element, null);
  17005. this.errors.push(TreeError.create(fullName, span, `Opening tag "${fullName}" not terminated.`));
  17006. }
  17007. }
  17008. _pushContainer(node, isClosedByChild) {
  17009. if (isClosedByChild) {
  17010. this._containerStack.pop();
  17011. }
  17012. this._addToParent(node);
  17013. this._containerStack.push(node);
  17014. }
  17015. _consumeEndTag(endTagToken) {
  17016. const fullName = this._getElementFullName(endTagToken.parts[0], endTagToken.parts[1], this._getClosestParentElement());
  17017. if (this.getTagDefinition(fullName).isVoid) {
  17018. this.errors.push(TreeError.create(fullName, endTagToken.sourceSpan, `Void elements do not have end tags "${endTagToken.parts[1]}"`));
  17019. }
  17020. else if (!this._popContainer(fullName, Element, endTagToken.sourceSpan)) {
  17021. const errMsg = `Unexpected closing tag "${fullName}". It may happen when the tag has already been closed by another tag. For more info see https://www.w3.org/TR/html5/syntax.html#closing-elements-that-have-implied-end-tags`;
  17022. this.errors.push(TreeError.create(fullName, endTagToken.sourceSpan, errMsg));
  17023. }
  17024. }
  17025. /**
  17026. * Closes the nearest element with the tag name `fullName` in the parse tree.
  17027. * `endSourceSpan` is the span of the closing tag, or null if the element does
  17028. * not have a closing tag (for example, this happens when an incomplete
  17029. * opening tag is recovered).
  17030. */
  17031. _popContainer(expectedName, expectedType, endSourceSpan) {
  17032. let unexpectedCloseTagDetected = false;
  17033. for (let stackIndex = this._containerStack.length - 1; stackIndex >= 0; stackIndex--) {
  17034. const node = this._containerStack[stackIndex];
  17035. if ((node.name === expectedName || expectedName === null) && node instanceof expectedType) {
  17036. // Record the parse span with the element that is being closed. Any elements that are
  17037. // removed from the element stack at this point are closed implicitly, so they won't get
  17038. // an end source span (as there is no explicit closing element).
  17039. node.endSourceSpan = endSourceSpan;
  17040. node.sourceSpan.end = endSourceSpan !== null ? endSourceSpan.end : node.sourceSpan.end;
  17041. this._containerStack.splice(stackIndex, this._containerStack.length - stackIndex);
  17042. return !unexpectedCloseTagDetected;
  17043. }
  17044. // Blocks and most elements are not self closing.
  17045. if (node instanceof Block ||
  17046. (node instanceof Element && !this.getTagDefinition(node.name).closedByParent)) {
  17047. // Note that we encountered an unexpected close tag but continue processing the element
  17048. // stack so we can assign an `endSourceSpan` if there is a corresponding start tag for this
  17049. // end tag in the stack.
  17050. unexpectedCloseTagDetected = true;
  17051. }
  17052. }
  17053. return false;
  17054. }
  17055. _consumeAttr(attrName) {
  17056. const fullName = mergeNsAndName(attrName.parts[0], attrName.parts[1]);
  17057. let attrEnd = attrName.sourceSpan.end;
  17058. // Consume any quote
  17059. if (this._peek.type === 15 /* TokenType.ATTR_QUOTE */) {
  17060. this._advance();
  17061. }
  17062. // Consume the attribute value
  17063. let value = '';
  17064. const valueTokens = [];
  17065. let valueStartSpan = undefined;
  17066. let valueEnd = undefined;
  17067. // NOTE: We need to use a new variable `nextTokenType` here to hide the actual type of
  17068. // `_peek.type` from TS. Otherwise TS will narrow the type of `_peek.type` preventing it from
  17069. // being able to consider `ATTR_VALUE_INTERPOLATION` as an option. This is because TS is not
  17070. // able to see that `_advance()` will actually mutate `_peek`.
  17071. const nextTokenType = this._peek.type;
  17072. if (nextTokenType === 16 /* TokenType.ATTR_VALUE_TEXT */) {
  17073. valueStartSpan = this._peek.sourceSpan;
  17074. valueEnd = this._peek.sourceSpan.end;
  17075. while (this._peek.type === 16 /* TokenType.ATTR_VALUE_TEXT */ ||
  17076. this._peek.type === 17 /* TokenType.ATTR_VALUE_INTERPOLATION */ ||
  17077. this._peek.type === 9 /* TokenType.ENCODED_ENTITY */) {
  17078. const valueToken = this._advance();
  17079. valueTokens.push(valueToken);
  17080. if (valueToken.type === 17 /* TokenType.ATTR_VALUE_INTERPOLATION */) {
  17081. // For backward compatibility we decode HTML entities that appear in interpolation
  17082. // expressions. This is arguably a bug, but it could be a considerable breaking change to
  17083. // fix it. It should be addressed in a larger project to refactor the entire parser/lexer
  17084. // chain after View Engine has been removed.
  17085. value += valueToken.parts.join('').replace(/&([^;]+);/g, decodeEntity);
  17086. }
  17087. else if (valueToken.type === 9 /* TokenType.ENCODED_ENTITY */) {
  17088. value += valueToken.parts[0];
  17089. }
  17090. else {
  17091. value += valueToken.parts.join('');
  17092. }
  17093. valueEnd = attrEnd = valueToken.sourceSpan.end;
  17094. }
  17095. }
  17096. // Consume any quote
  17097. if (this._peek.type === 15 /* TokenType.ATTR_QUOTE */) {
  17098. const quoteToken = this._advance();
  17099. attrEnd = quoteToken.sourceSpan.end;
  17100. }
  17101. const valueSpan = valueStartSpan &&
  17102. valueEnd &&
  17103. new ParseSourceSpan(valueStartSpan.start, valueEnd, valueStartSpan.fullStart);
  17104. return new Attribute(fullName, value, new ParseSourceSpan(attrName.sourceSpan.start, attrEnd, attrName.sourceSpan.fullStart), attrName.sourceSpan, valueSpan, valueTokens.length > 0 ? valueTokens : undefined, undefined);
  17105. }
  17106. _consumeBlockOpen(token) {
  17107. const parameters = [];
  17108. while (this._peek.type === 27 /* TokenType.BLOCK_PARAMETER */) {
  17109. const paramToken = this._advance();
  17110. parameters.push(new BlockParameter(paramToken.parts[0], paramToken.sourceSpan));
  17111. }
  17112. if (this._peek.type === 25 /* TokenType.BLOCK_OPEN_END */) {
  17113. this._advance();
  17114. }
  17115. const end = this._peek.sourceSpan.fullStart;
  17116. const span = new ParseSourceSpan(token.sourceSpan.start, end, token.sourceSpan.fullStart);
  17117. // Create a separate `startSpan` because `span` will be modified when there is an `end` span.
  17118. const startSpan = new ParseSourceSpan(token.sourceSpan.start, end, token.sourceSpan.fullStart);
  17119. const block = new Block(token.parts[0], parameters, [], span, token.sourceSpan, startSpan);
  17120. this._pushContainer(block, false);
  17121. }
  17122. _consumeBlockClose(token) {
  17123. if (!this._popContainer(null, Block, token.sourceSpan)) {
  17124. this.errors.push(TreeError.create(null, token.sourceSpan, `Unexpected closing block. The block may have been closed earlier. ` +
  17125. `If you meant to write the } character, you should use the "&#125;" ` +
  17126. `HTML entity instead.`));
  17127. }
  17128. }
  17129. _consumeIncompleteBlock(token) {
  17130. const parameters = [];
  17131. while (this._peek.type === 27 /* TokenType.BLOCK_PARAMETER */) {
  17132. const paramToken = this._advance();
  17133. parameters.push(new BlockParameter(paramToken.parts[0], paramToken.sourceSpan));
  17134. }
  17135. const end = this._peek.sourceSpan.fullStart;
  17136. const span = new ParseSourceSpan(token.sourceSpan.start, end, token.sourceSpan.fullStart);
  17137. // Create a separate `startSpan` because `span` will be modified when there is an `end` span.
  17138. const startSpan = new ParseSourceSpan(token.sourceSpan.start, end, token.sourceSpan.fullStart);
  17139. const block = new Block(token.parts[0], parameters, [], span, token.sourceSpan, startSpan);
  17140. this._pushContainer(block, false);
  17141. // Incomplete blocks don't have children so we close them immediately and report an error.
  17142. this._popContainer(null, Block, null);
  17143. this.errors.push(TreeError.create(token.parts[0], span, `Incomplete block "${token.parts[0]}". If you meant to write the @ character, ` +
  17144. `you should use the "&#64;" HTML entity instead.`));
  17145. }
  17146. _consumeLet(startToken) {
  17147. const name = startToken.parts[0];
  17148. let valueToken;
  17149. let endToken;
  17150. if (this._peek.type !== 30 /* TokenType.LET_VALUE */) {
  17151. this.errors.push(TreeError.create(startToken.parts[0], startToken.sourceSpan, `Invalid @let declaration "${name}". Declaration must have a value.`));
  17152. return;
  17153. }
  17154. else {
  17155. valueToken = this._advance();
  17156. }
  17157. // Type cast is necessary here since TS narrowed the type of `peek` above.
  17158. if (this._peek.type !== 31 /* TokenType.LET_END */) {
  17159. this.errors.push(TreeError.create(startToken.parts[0], startToken.sourceSpan, `Unterminated @let declaration "${name}". Declaration must be terminated with a semicolon.`));
  17160. return;
  17161. }
  17162. else {
  17163. endToken = this._advance();
  17164. }
  17165. const end = endToken.sourceSpan.fullStart;
  17166. const span = new ParseSourceSpan(startToken.sourceSpan.start, end, startToken.sourceSpan.fullStart);
  17167. // The start token usually captures the `@let`. Construct a name span by
  17168. // offsetting the start by the length of any text before the name.
  17169. const startOffset = startToken.sourceSpan.toString().lastIndexOf(name);
  17170. const nameStart = startToken.sourceSpan.start.moveBy(startOffset);
  17171. const nameSpan = new ParseSourceSpan(nameStart, startToken.sourceSpan.end);
  17172. const node = new LetDeclaration(name, valueToken.parts[0], span, nameSpan, valueToken.sourceSpan);
  17173. this._addToParent(node);
  17174. }
  17175. _consumeIncompleteLet(token) {
  17176. // Incomplete `@let` declaration may end up with an empty name.
  17177. const name = token.parts[0] ?? '';
  17178. const nameString = name ? ` "${name}"` : '';
  17179. // If there's at least a name, we can salvage an AST node that can be used for completions.
  17180. if (name.length > 0) {
  17181. const startOffset = token.sourceSpan.toString().lastIndexOf(name);
  17182. const nameStart = token.sourceSpan.start.moveBy(startOffset);
  17183. const nameSpan = new ParseSourceSpan(nameStart, token.sourceSpan.end);
  17184. const valueSpan = new ParseSourceSpan(token.sourceSpan.start, token.sourceSpan.start.moveBy(0));
  17185. const node = new LetDeclaration(name, '', token.sourceSpan, nameSpan, valueSpan);
  17186. this._addToParent(node);
  17187. }
  17188. this.errors.push(TreeError.create(token.parts[0], token.sourceSpan, `Incomplete @let declaration${nameString}. ` +
  17189. `@let declarations must be written as \`@let <name> = <value>;\``));
  17190. }
  17191. _getContainer() {
  17192. return this._containerStack.length > 0
  17193. ? this._containerStack[this._containerStack.length - 1]
  17194. : null;
  17195. }
  17196. _getClosestParentElement() {
  17197. for (let i = this._containerStack.length - 1; i > -1; i--) {
  17198. if (this._containerStack[i] instanceof Element) {
  17199. return this._containerStack[i];
  17200. }
  17201. }
  17202. return null;
  17203. }
  17204. _addToParent(node) {
  17205. const parent = this._getContainer();
  17206. if (parent === null) {
  17207. this.rootNodes.push(node);
  17208. }
  17209. else {
  17210. parent.children.push(node);
  17211. }
  17212. }
  17213. _getElementFullName(prefix, localName, parentElement) {
  17214. if (prefix === '') {
  17215. prefix = this.getTagDefinition(localName).implicitNamespacePrefix || '';
  17216. if (prefix === '' && parentElement != null) {
  17217. const parentTagName = splitNsName(parentElement.name)[1];
  17218. const parentTagDefinition = this.getTagDefinition(parentTagName);
  17219. if (!parentTagDefinition.preventNamespaceInheritance) {
  17220. prefix = getNsPrefix(parentElement.name);
  17221. }
  17222. }
  17223. }
  17224. return mergeNsAndName(prefix, localName);
  17225. }
  17226. }
  17227. function lastOnStack(stack, element) {
  17228. return stack.length > 0 && stack[stack.length - 1] === element;
  17229. }
  17230. /**
  17231. * Decode the `entity` string, which we believe is the contents of an HTML entity.
  17232. *
  17233. * If the string is not actually a valid/known entity then just return the original `match` string.
  17234. */
  17235. function decodeEntity(match, entity) {
  17236. if (NAMED_ENTITIES[entity] !== undefined) {
  17237. return NAMED_ENTITIES[entity] || match;
  17238. }
  17239. if (/^#x[a-f0-9]+$/i.test(entity)) {
  17240. return String.fromCodePoint(parseInt(entity.slice(2), 16));
  17241. }
  17242. if (/^#\d+$/.test(entity)) {
  17243. return String.fromCodePoint(parseInt(entity.slice(1), 10));
  17244. }
  17245. return match;
  17246. }
  17247. const PRESERVE_WS_ATTR_NAME = 'ngPreserveWhitespaces';
  17248. const SKIP_WS_TRIM_TAGS = new Set(['pre', 'template', 'textarea', 'script', 'style']);
  17249. // Equivalent to \s with \u00a0 (non-breaking space) excluded.
  17250. // Based on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
  17251. const WS_CHARS = ' \f\n\r\t\v\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff';
  17252. const NO_WS_REGEXP = new RegExp(`[^${WS_CHARS}]`);
  17253. const WS_REPLACE_REGEXP = new RegExp(`[${WS_CHARS}]{2,}`, 'g');
  17254. function hasPreserveWhitespacesAttr(attrs) {
  17255. return attrs.some((attr) => attr.name === PRESERVE_WS_ATTR_NAME);
  17256. }
  17257. /**
  17258. * &ngsp; is a placeholder for non-removable space
  17259. * &ngsp; is converted to the 0xE500 PUA (Private Use Areas) unicode character
  17260. * and later on replaced by a space.
  17261. */
  17262. function replaceNgsp(value) {
  17263. // lexer is replacing the &ngsp; pseudo-entity with NGSP_UNICODE
  17264. return value.replace(new RegExp(NGSP_UNICODE, 'g'), ' ');
  17265. }
  17266. /**
  17267. * This visitor can walk HTML parse tree and remove / trim text nodes using the following rules:
  17268. * - consider spaces, tabs and new lines as whitespace characters;
  17269. * - drop text nodes consisting of whitespace characters only;
  17270. * - for all other text nodes replace consecutive whitespace characters with one space;
  17271. * - convert &ngsp; pseudo-entity to a single space;
  17272. *
  17273. * Removal and trimming of whitespaces have positive performance impact (less code to generate
  17274. * while compiling templates, faster view creation). At the same time it can be "destructive"
  17275. * in some cases (whitespaces can influence layout). Because of the potential of breaking layout
  17276. * this visitor is not activated by default in Angular 5 and people need to explicitly opt-in for
  17277. * whitespace removal. The default option for whitespace removal will be revisited in Angular 6
  17278. * and might be changed to "on" by default.
  17279. *
  17280. * If `originalNodeMap` is provided, the transformed nodes will be mapped back to their original
  17281. * inputs. Any output nodes not in the map were not transformed. This supports correlating and
  17282. * porting information between the trimmed nodes and original nodes (such as `i18n` properties)
  17283. * such that trimming whitespace does not does not drop required information from the node.
  17284. */
  17285. class WhitespaceVisitor {
  17286. preserveSignificantWhitespace;
  17287. originalNodeMap;
  17288. requireContext;
  17289. // How many ICU expansions which are currently being visited. ICUs can be nested, so this
  17290. // tracks the current depth of nesting. If this depth is greater than 0, then this visitor is
  17291. // currently processing content inside an ICU expansion.
  17292. icuExpansionDepth = 0;
  17293. constructor(preserveSignificantWhitespace, originalNodeMap, requireContext = true) {
  17294. this.preserveSignificantWhitespace = preserveSignificantWhitespace;
  17295. this.originalNodeMap = originalNodeMap;
  17296. this.requireContext = requireContext;
  17297. }
  17298. visitElement(element, context) {
  17299. if (SKIP_WS_TRIM_TAGS.has(element.name) || hasPreserveWhitespacesAttr(element.attrs)) {
  17300. // don't descent into elements where we need to preserve whitespaces
  17301. // but still visit all attributes to eliminate one used as a market to preserve WS
  17302. const newElement = new Element(element.name, visitAllWithSiblings(this, element.attrs), element.children, element.sourceSpan, element.startSourceSpan, element.endSourceSpan, element.i18n);
  17303. this.originalNodeMap?.set(newElement, element);
  17304. return newElement;
  17305. }
  17306. const newElement = new Element(element.name, element.attrs, visitAllWithSiblings(this, element.children), element.sourceSpan, element.startSourceSpan, element.endSourceSpan, element.i18n);
  17307. this.originalNodeMap?.set(newElement, element);
  17308. return newElement;
  17309. }
  17310. visitAttribute(attribute, context) {
  17311. return attribute.name !== PRESERVE_WS_ATTR_NAME ? attribute : null;
  17312. }
  17313. visitText(text, context) {
  17314. const isNotBlank = text.value.match(NO_WS_REGEXP);
  17315. const hasExpansionSibling = context && (context.prev instanceof Expansion || context.next instanceof Expansion);
  17316. // Do not trim whitespace within ICU expansions when preserving significant whitespace.
  17317. // Historically, ICU whitespace was never trimmed and this is really a bug. However fixing it
  17318. // would change message IDs which we can't easily do. Instead we only trim ICU whitespace within
  17319. // ICU expansions when not preserving significant whitespace, which is the new behavior where it
  17320. // most matters.
  17321. const inIcuExpansion = this.icuExpansionDepth > 0;
  17322. if (inIcuExpansion && this.preserveSignificantWhitespace)
  17323. return text;
  17324. if (isNotBlank || hasExpansionSibling) {
  17325. // Process the whitespace in the tokens of this Text node
  17326. const tokens = text.tokens.map((token) => token.type === 5 /* TokenType.TEXT */ ? createWhitespaceProcessedTextToken(token) : token);
  17327. // Fully trim message when significant whitespace is not preserved.
  17328. if (!this.preserveSignificantWhitespace && tokens.length > 0) {
  17329. // The first token should only call `.trimStart()` and the last token
  17330. // should only call `.trimEnd()`, but there might be only one token which
  17331. // needs to call both.
  17332. const firstToken = tokens[0];
  17333. tokens.splice(0, 1, trimLeadingWhitespace(firstToken, context));
  17334. const lastToken = tokens[tokens.length - 1]; // Could be the same as the first token.
  17335. tokens.splice(tokens.length - 1, 1, trimTrailingWhitespace(lastToken, context));
  17336. }
  17337. // Process the whitespace of the value of this Text node. Also trim the leading/trailing
  17338. // whitespace when we don't need to preserve significant whitespace.
  17339. const processed = processWhitespace(text.value);
  17340. const value = this.preserveSignificantWhitespace
  17341. ? processed
  17342. : trimLeadingAndTrailingWhitespace(processed, context);
  17343. const result = new Text(value, text.sourceSpan, tokens, text.i18n);
  17344. this.originalNodeMap?.set(result, text);
  17345. return result;
  17346. }
  17347. return null;
  17348. }
  17349. visitComment(comment, context) {
  17350. return comment;
  17351. }
  17352. visitExpansion(expansion, context) {
  17353. this.icuExpansionDepth++;
  17354. let newExpansion;
  17355. try {
  17356. newExpansion = new Expansion(expansion.switchValue, expansion.type, visitAllWithSiblings(this, expansion.cases), expansion.sourceSpan, expansion.switchValueSourceSpan, expansion.i18n);
  17357. }
  17358. finally {
  17359. this.icuExpansionDepth--;
  17360. }
  17361. this.originalNodeMap?.set(newExpansion, expansion);
  17362. return newExpansion;
  17363. }
  17364. visitExpansionCase(expansionCase, context) {
  17365. const newExpansionCase = new ExpansionCase(expansionCase.value, visitAllWithSiblings(this, expansionCase.expression), expansionCase.sourceSpan, expansionCase.valueSourceSpan, expansionCase.expSourceSpan);
  17366. this.originalNodeMap?.set(newExpansionCase, expansionCase);
  17367. return newExpansionCase;
  17368. }
  17369. visitBlock(block, context) {
  17370. const newBlock = new Block(block.name, block.parameters, visitAllWithSiblings(this, block.children), block.sourceSpan, block.nameSpan, block.startSourceSpan, block.endSourceSpan);
  17371. this.originalNodeMap?.set(newBlock, block);
  17372. return newBlock;
  17373. }
  17374. visitBlockParameter(parameter, context) {
  17375. return parameter;
  17376. }
  17377. visitLetDeclaration(decl, context) {
  17378. return decl;
  17379. }
  17380. visit(_node, context) {
  17381. // `visitAllWithSiblings` provides context necessary for ICU messages to be handled correctly.
  17382. // Prefer that over calling `html.visitAll` directly on this visitor.
  17383. if (this.requireContext && !context) {
  17384. throw new Error(`WhitespaceVisitor requires context. Visit via \`visitAllWithSiblings\` to get this context.`);
  17385. }
  17386. return false;
  17387. }
  17388. }
  17389. function trimLeadingWhitespace(token, context) {
  17390. if (token.type !== 5 /* TokenType.TEXT */)
  17391. return token;
  17392. const isFirstTokenInTag = !context?.prev;
  17393. if (!isFirstTokenInTag)
  17394. return token;
  17395. return transformTextToken(token, (text) => text.trimStart());
  17396. }
  17397. function trimTrailingWhitespace(token, context) {
  17398. if (token.type !== 5 /* TokenType.TEXT */)
  17399. return token;
  17400. const isLastTokenInTag = !context?.next;
  17401. if (!isLastTokenInTag)
  17402. return token;
  17403. return transformTextToken(token, (text) => text.trimEnd());
  17404. }
  17405. function trimLeadingAndTrailingWhitespace(text, context) {
  17406. const isFirstTokenInTag = !context?.prev;
  17407. const isLastTokenInTag = !context?.next;
  17408. const maybeTrimmedStart = isFirstTokenInTag ? text.trimStart() : text;
  17409. const maybeTrimmed = isLastTokenInTag ? maybeTrimmedStart.trimEnd() : maybeTrimmedStart;
  17410. return maybeTrimmed;
  17411. }
  17412. function createWhitespaceProcessedTextToken({ type, parts, sourceSpan }) {
  17413. return { type, parts: [processWhitespace(parts[0])], sourceSpan };
  17414. }
  17415. function transformTextToken({ type, parts, sourceSpan }, transform) {
  17416. // `TextToken` only ever has one part as defined in its type, so we just transform the first element.
  17417. return { type, parts: [transform(parts[0])], sourceSpan };
  17418. }
  17419. function processWhitespace(text) {
  17420. return replaceNgsp(text).replace(WS_REPLACE_REGEXP, ' ');
  17421. }
  17422. function visitAllWithSiblings(visitor, nodes) {
  17423. const result = [];
  17424. nodes.forEach((ast, i) => {
  17425. const context = { prev: nodes[i - 1], next: nodes[i + 1] };
  17426. const astResult = ast.visit(visitor, context);
  17427. if (astResult) {
  17428. result.push(astResult);
  17429. }
  17430. });
  17431. return result;
  17432. }
  17433. var TokenType;
  17434. (function (TokenType) {
  17435. TokenType[TokenType["Character"] = 0] = "Character";
  17436. TokenType[TokenType["Identifier"] = 1] = "Identifier";
  17437. TokenType[TokenType["PrivateIdentifier"] = 2] = "PrivateIdentifier";
  17438. TokenType[TokenType["Keyword"] = 3] = "Keyword";
  17439. TokenType[TokenType["String"] = 4] = "String";
  17440. TokenType[TokenType["Operator"] = 5] = "Operator";
  17441. TokenType[TokenType["Number"] = 6] = "Number";
  17442. TokenType[TokenType["Error"] = 7] = "Error";
  17443. })(TokenType || (TokenType = {}));
  17444. var StringTokenKind;
  17445. (function (StringTokenKind) {
  17446. StringTokenKind[StringTokenKind["Plain"] = 0] = "Plain";
  17447. StringTokenKind[StringTokenKind["TemplateLiteralPart"] = 1] = "TemplateLiteralPart";
  17448. StringTokenKind[StringTokenKind["TemplateLiteralEnd"] = 2] = "TemplateLiteralEnd";
  17449. })(StringTokenKind || (StringTokenKind = {}));
  17450. const KEYWORDS = [
  17451. 'var',
  17452. 'let',
  17453. 'as',
  17454. 'null',
  17455. 'undefined',
  17456. 'true',
  17457. 'false',
  17458. 'if',
  17459. 'else',
  17460. 'this',
  17461. 'typeof',
  17462. ];
  17463. class Lexer {
  17464. tokenize(text) {
  17465. return new _Scanner(text).scan();
  17466. }
  17467. }
  17468. class Token {
  17469. index;
  17470. end;
  17471. type;
  17472. numValue;
  17473. strValue;
  17474. constructor(index, end, type, numValue, strValue) {
  17475. this.index = index;
  17476. this.end = end;
  17477. this.type = type;
  17478. this.numValue = numValue;
  17479. this.strValue = strValue;
  17480. }
  17481. isCharacter(code) {
  17482. return this.type === TokenType.Character && this.numValue === code;
  17483. }
  17484. isNumber() {
  17485. return this.type === TokenType.Number;
  17486. }
  17487. isString() {
  17488. return this.type === TokenType.String;
  17489. }
  17490. isOperator(operator) {
  17491. return this.type === TokenType.Operator && this.strValue === operator;
  17492. }
  17493. isIdentifier() {
  17494. return this.type === TokenType.Identifier;
  17495. }
  17496. isPrivateIdentifier() {
  17497. return this.type === TokenType.PrivateIdentifier;
  17498. }
  17499. isKeyword() {
  17500. return this.type === TokenType.Keyword;
  17501. }
  17502. isKeywordLet() {
  17503. return this.type === TokenType.Keyword && this.strValue === 'let';
  17504. }
  17505. isKeywordAs() {
  17506. return this.type === TokenType.Keyword && this.strValue === 'as';
  17507. }
  17508. isKeywordNull() {
  17509. return this.type === TokenType.Keyword && this.strValue === 'null';
  17510. }
  17511. isKeywordUndefined() {
  17512. return this.type === TokenType.Keyword && this.strValue === 'undefined';
  17513. }
  17514. isKeywordTrue() {
  17515. return this.type === TokenType.Keyword && this.strValue === 'true';
  17516. }
  17517. isKeywordFalse() {
  17518. return this.type === TokenType.Keyword && this.strValue === 'false';
  17519. }
  17520. isKeywordThis() {
  17521. return this.type === TokenType.Keyword && this.strValue === 'this';
  17522. }
  17523. isKeywordTypeof() {
  17524. return this.type === TokenType.Keyword && this.strValue === 'typeof';
  17525. }
  17526. isError() {
  17527. return this.type === TokenType.Error;
  17528. }
  17529. toNumber() {
  17530. return this.type === TokenType.Number ? this.numValue : -1;
  17531. }
  17532. isTemplateLiteralPart() {
  17533. return this.isString() && this.kind === StringTokenKind.TemplateLiteralPart;
  17534. }
  17535. isTemplateLiteralEnd() {
  17536. return this.isString() && this.kind === StringTokenKind.TemplateLiteralEnd;
  17537. }
  17538. isTemplateLiteralInterpolationStart() {
  17539. return this.isOperator('${');
  17540. }
  17541. isTemplateLiteralInterpolationEnd() {
  17542. return this.isOperator('}');
  17543. }
  17544. toString() {
  17545. switch (this.type) {
  17546. case TokenType.Character:
  17547. case TokenType.Identifier:
  17548. case TokenType.Keyword:
  17549. case TokenType.Operator:
  17550. case TokenType.PrivateIdentifier:
  17551. case TokenType.String:
  17552. case TokenType.Error:
  17553. return this.strValue;
  17554. case TokenType.Number:
  17555. return this.numValue.toString();
  17556. default:
  17557. return null;
  17558. }
  17559. }
  17560. }
  17561. class StringToken extends Token {
  17562. kind;
  17563. constructor(index, end, strValue, kind) {
  17564. super(index, end, TokenType.String, 0, strValue);
  17565. this.kind = kind;
  17566. }
  17567. }
  17568. function newCharacterToken(index, end, code) {
  17569. return new Token(index, end, TokenType.Character, code, String.fromCharCode(code));
  17570. }
  17571. function newIdentifierToken(index, end, text) {
  17572. return new Token(index, end, TokenType.Identifier, 0, text);
  17573. }
  17574. function newPrivateIdentifierToken(index, end, text) {
  17575. return new Token(index, end, TokenType.PrivateIdentifier, 0, text);
  17576. }
  17577. function newKeywordToken(index, end, text) {
  17578. return new Token(index, end, TokenType.Keyword, 0, text);
  17579. }
  17580. function newOperatorToken(index, end, text) {
  17581. return new Token(index, end, TokenType.Operator, 0, text);
  17582. }
  17583. function newNumberToken(index, end, n) {
  17584. return new Token(index, end, TokenType.Number, n, '');
  17585. }
  17586. function newErrorToken(index, end, message) {
  17587. return new Token(index, end, TokenType.Error, 0, message);
  17588. }
  17589. const EOF = new Token(-1, -1, TokenType.Character, 0, '');
  17590. class _Scanner {
  17591. input;
  17592. tokens = [];
  17593. length;
  17594. peek = 0;
  17595. index = -1;
  17596. literalInterpolationDepth = 0;
  17597. braceDepth = 0;
  17598. constructor(input) {
  17599. this.input = input;
  17600. this.length = input.length;
  17601. this.advance();
  17602. }
  17603. scan() {
  17604. let token = this.scanToken();
  17605. while (token !== null) {
  17606. this.tokens.push(token);
  17607. token = this.scanToken();
  17608. }
  17609. return this.tokens;
  17610. }
  17611. advance() {
  17612. this.peek = ++this.index >= this.length ? $EOF : this.input.charCodeAt(this.index);
  17613. }
  17614. scanToken() {
  17615. const input = this.input;
  17616. const length = this.length;
  17617. let peek = this.peek;
  17618. let index = this.index;
  17619. // Skip whitespace.
  17620. while (peek <= $SPACE) {
  17621. if (++index >= length) {
  17622. peek = $EOF;
  17623. break;
  17624. }
  17625. else {
  17626. peek = input.charCodeAt(index);
  17627. }
  17628. }
  17629. this.peek = peek;
  17630. this.index = index;
  17631. if (index >= length) {
  17632. return null;
  17633. }
  17634. // Handle identifiers and numbers.
  17635. if (isIdentifierStart(peek)) {
  17636. return this.scanIdentifier();
  17637. }
  17638. if (isDigit(peek)) {
  17639. return this.scanNumber(index);
  17640. }
  17641. const start = index;
  17642. switch (peek) {
  17643. case $PERIOD:
  17644. this.advance();
  17645. return isDigit(this.peek)
  17646. ? this.scanNumber(start)
  17647. : newCharacterToken(start, this.index, $PERIOD);
  17648. case $LPAREN:
  17649. case $RPAREN:
  17650. case $LBRACKET:
  17651. case $RBRACKET:
  17652. case $COMMA:
  17653. case $COLON:
  17654. case $SEMICOLON:
  17655. return this.scanCharacter(start, peek);
  17656. case $LBRACE:
  17657. return this.scanOpenBrace(start, peek);
  17658. case $RBRACE:
  17659. return this.scanCloseBrace(start, peek);
  17660. case $SQ:
  17661. case $DQ:
  17662. return this.scanString();
  17663. case $BT:
  17664. this.advance();
  17665. return this.scanTemplateLiteralPart(start);
  17666. case $HASH:
  17667. return this.scanPrivateIdentifier();
  17668. case $PLUS:
  17669. case $MINUS:
  17670. case $STAR:
  17671. case $SLASH:
  17672. case $PERCENT:
  17673. case $CARET:
  17674. return this.scanOperator(start, String.fromCharCode(peek));
  17675. case $QUESTION:
  17676. return this.scanQuestion(start);
  17677. case $LT:
  17678. case $GT:
  17679. return this.scanComplexOperator(start, String.fromCharCode(peek), $EQ, '=');
  17680. case $BANG:
  17681. case $EQ:
  17682. return this.scanComplexOperator(start, String.fromCharCode(peek), $EQ, '=', $EQ, '=');
  17683. case $AMPERSAND:
  17684. return this.scanComplexOperator(start, '&', $AMPERSAND, '&');
  17685. case $BAR:
  17686. return this.scanComplexOperator(start, '|', $BAR, '|');
  17687. case $NBSP:
  17688. while (isWhitespace(this.peek))
  17689. this.advance();
  17690. return this.scanToken();
  17691. }
  17692. this.advance();
  17693. return this.error(`Unexpected character [${String.fromCharCode(peek)}]`, 0);
  17694. }
  17695. scanCharacter(start, code) {
  17696. this.advance();
  17697. return newCharacterToken(start, this.index, code);
  17698. }
  17699. scanOperator(start, str) {
  17700. this.advance();
  17701. return newOperatorToken(start, this.index, str);
  17702. }
  17703. scanOpenBrace(start, code) {
  17704. this.braceDepth++;
  17705. this.advance();
  17706. return newCharacterToken(start, this.index, code);
  17707. }
  17708. scanCloseBrace(start, code) {
  17709. this.advance();
  17710. if (this.braceDepth === 0 && this.literalInterpolationDepth > 0) {
  17711. this.literalInterpolationDepth--;
  17712. this.tokens.push(newOperatorToken(start, this.index, '}'));
  17713. return this.scanTemplateLiteralPart(this.index);
  17714. }
  17715. this.braceDepth--;
  17716. return newCharacterToken(start, this.index, code);
  17717. }
  17718. /**
  17719. * Tokenize a 2/3 char long operator
  17720. *
  17721. * @param start start index in the expression
  17722. * @param one first symbol (always part of the operator)
  17723. * @param twoCode code point for the second symbol
  17724. * @param two second symbol (part of the operator when the second code point matches)
  17725. * @param threeCode code point for the third symbol
  17726. * @param three third symbol (part of the operator when provided and matches source expression)
  17727. */
  17728. scanComplexOperator(start, one, twoCode, two, threeCode, three) {
  17729. this.advance();
  17730. let str = one;
  17731. if (this.peek == twoCode) {
  17732. this.advance();
  17733. str += two;
  17734. }
  17735. if (threeCode != null && this.peek == threeCode) {
  17736. this.advance();
  17737. str += three;
  17738. }
  17739. return newOperatorToken(start, this.index, str);
  17740. }
  17741. scanIdentifier() {
  17742. const start = this.index;
  17743. this.advance();
  17744. while (isIdentifierPart(this.peek))
  17745. this.advance();
  17746. const str = this.input.substring(start, this.index);
  17747. return KEYWORDS.indexOf(str) > -1
  17748. ? newKeywordToken(start, this.index, str)
  17749. : newIdentifierToken(start, this.index, str);
  17750. }
  17751. /** Scans an ECMAScript private identifier. */
  17752. scanPrivateIdentifier() {
  17753. const start = this.index;
  17754. this.advance();
  17755. if (!isIdentifierStart(this.peek)) {
  17756. return this.error('Invalid character [#]', -1);
  17757. }
  17758. while (isIdentifierPart(this.peek))
  17759. this.advance();
  17760. const identifierName = this.input.substring(start, this.index);
  17761. return newPrivateIdentifierToken(start, this.index, identifierName);
  17762. }
  17763. scanNumber(start) {
  17764. let simple = this.index === start;
  17765. let hasSeparators = false;
  17766. this.advance(); // Skip initial digit.
  17767. while (true) {
  17768. if (isDigit(this.peek)) ;
  17769. else if (this.peek === $_) {
  17770. // Separators are only valid when they're surrounded by digits. E.g. `1_0_1` is
  17771. // valid while `_101` and `101_` are not. The separator can't be next to the decimal
  17772. // point or another separator either. Note that it's unlikely that we'll hit a case where
  17773. // the underscore is at the start, because that's a valid identifier and it will be picked
  17774. // up earlier in the parsing. We validate for it anyway just in case.
  17775. if (!isDigit(this.input.charCodeAt(this.index - 1)) ||
  17776. !isDigit(this.input.charCodeAt(this.index + 1))) {
  17777. return this.error('Invalid numeric separator', 0);
  17778. }
  17779. hasSeparators = true;
  17780. }
  17781. else if (this.peek === $PERIOD) {
  17782. simple = false;
  17783. }
  17784. else if (isExponentStart(this.peek)) {
  17785. this.advance();
  17786. if (isExponentSign(this.peek))
  17787. this.advance();
  17788. if (!isDigit(this.peek))
  17789. return this.error('Invalid exponent', -1);
  17790. simple = false;
  17791. }
  17792. else {
  17793. break;
  17794. }
  17795. this.advance();
  17796. }
  17797. let str = this.input.substring(start, this.index);
  17798. if (hasSeparators) {
  17799. str = str.replace(/_/g, '');
  17800. }
  17801. const value = simple ? parseIntAutoRadix(str) : parseFloat(str);
  17802. return newNumberToken(start, this.index, value);
  17803. }
  17804. scanString() {
  17805. const start = this.index;
  17806. const quote = this.peek;
  17807. this.advance(); // Skip initial quote.
  17808. let buffer = '';
  17809. let marker = this.index;
  17810. const input = this.input;
  17811. while (this.peek != quote) {
  17812. if (this.peek == $BACKSLASH) {
  17813. const result = this.scanStringBackslash(buffer, marker);
  17814. if (typeof result !== 'string') {
  17815. return result; // Error
  17816. }
  17817. buffer = result;
  17818. marker = this.index;
  17819. }
  17820. else if (this.peek == $EOF) {
  17821. return this.error('Unterminated quote', 0);
  17822. }
  17823. else {
  17824. this.advance();
  17825. }
  17826. }
  17827. const last = input.substring(marker, this.index);
  17828. this.advance(); // Skip terminating quote.
  17829. return new StringToken(start, this.index, buffer + last, StringTokenKind.Plain);
  17830. }
  17831. scanQuestion(start) {
  17832. this.advance();
  17833. let str = '?';
  17834. // Either `a ?? b` or 'a?.b'.
  17835. if (this.peek === $QUESTION || this.peek === $PERIOD) {
  17836. str += this.peek === $PERIOD ? '.' : '?';
  17837. this.advance();
  17838. }
  17839. return newOperatorToken(start, this.index, str);
  17840. }
  17841. scanTemplateLiteralPart(start) {
  17842. let buffer = '';
  17843. let marker = this.index;
  17844. while (this.peek !== $BT) {
  17845. if (this.peek === $BACKSLASH) {
  17846. const result = this.scanStringBackslash(buffer, marker);
  17847. if (typeof result !== 'string') {
  17848. return result; // Error
  17849. }
  17850. buffer = result;
  17851. marker = this.index;
  17852. }
  17853. else if (this.peek === $$) {
  17854. const dollar = this.index;
  17855. this.advance();
  17856. // @ts-expect-error
  17857. if (this.peek === $LBRACE) {
  17858. this.literalInterpolationDepth++;
  17859. this.tokens.push(new StringToken(start, dollar, buffer + this.input.substring(marker, dollar), StringTokenKind.TemplateLiteralPart));
  17860. this.advance();
  17861. return newOperatorToken(dollar, this.index, this.input.substring(dollar, this.index));
  17862. }
  17863. }
  17864. else if (this.peek === $EOF) {
  17865. return this.error('Unterminated template literal', 0);
  17866. }
  17867. else {
  17868. this.advance();
  17869. }
  17870. }
  17871. const last = this.input.substring(marker, this.index);
  17872. this.advance();
  17873. return new StringToken(start, this.index, buffer + last, StringTokenKind.TemplateLiteralEnd);
  17874. }
  17875. error(message, offset) {
  17876. const position = this.index + offset;
  17877. return newErrorToken(position, this.index, `Lexer Error: ${message} at column ${position} in expression [${this.input}]`);
  17878. }
  17879. scanStringBackslash(buffer, marker) {
  17880. buffer += this.input.substring(marker, this.index);
  17881. let unescapedCode;
  17882. this.advance();
  17883. if (this.peek === $u) {
  17884. // 4 character hex code for unicode character.
  17885. const hex = this.input.substring(this.index + 1, this.index + 5);
  17886. if (/^[0-9a-f]+$/i.test(hex)) {
  17887. unescapedCode = parseInt(hex, 16);
  17888. }
  17889. else {
  17890. return this.error(`Invalid unicode escape [\\u${hex}]`, 0);
  17891. }
  17892. for (let i = 0; i < 5; i++) {
  17893. this.advance();
  17894. }
  17895. }
  17896. else {
  17897. unescapedCode = unescape(this.peek);
  17898. this.advance();
  17899. }
  17900. buffer += String.fromCharCode(unescapedCode);
  17901. return buffer;
  17902. }
  17903. }
  17904. function isIdentifierStart(code) {
  17905. return (($a <= code && code <= $z) ||
  17906. ($A <= code && code <= $Z) ||
  17907. code == $_ ||
  17908. code == $$);
  17909. }
  17910. function isIdentifierPart(code) {
  17911. return isAsciiLetter(code) || isDigit(code) || code == $_ || code == $$;
  17912. }
  17913. function isExponentStart(code) {
  17914. return code == $e || code == $E;
  17915. }
  17916. function isExponentSign(code) {
  17917. return code == $MINUS || code == $PLUS;
  17918. }
  17919. function unescape(code) {
  17920. switch (code) {
  17921. case $n:
  17922. return $LF;
  17923. case $f:
  17924. return $FF;
  17925. case $r:
  17926. return $CR;
  17927. case $t:
  17928. return $TAB;
  17929. case $v:
  17930. return $VTAB;
  17931. default:
  17932. return code;
  17933. }
  17934. }
  17935. function parseIntAutoRadix(text) {
  17936. const result = parseInt(text);
  17937. if (isNaN(result)) {
  17938. throw new Error('Invalid integer literal when parsing ' + text);
  17939. }
  17940. return result;
  17941. }
  17942. class SplitInterpolation {
  17943. strings;
  17944. expressions;
  17945. offsets;
  17946. constructor(strings, expressions, offsets) {
  17947. this.strings = strings;
  17948. this.expressions = expressions;
  17949. this.offsets = offsets;
  17950. }
  17951. }
  17952. class TemplateBindingParseResult {
  17953. templateBindings;
  17954. warnings;
  17955. errors;
  17956. constructor(templateBindings, warnings, errors) {
  17957. this.templateBindings = templateBindings;
  17958. this.warnings = warnings;
  17959. this.errors = errors;
  17960. }
  17961. }
  17962. class Parser {
  17963. _lexer;
  17964. errors = [];
  17965. constructor(_lexer) {
  17966. this._lexer = _lexer;
  17967. }
  17968. parseAction(input, location, absoluteOffset, interpolationConfig = DEFAULT_INTERPOLATION_CONFIG) {
  17969. this._checkNoInterpolation(input, location, interpolationConfig);
  17970. const sourceToLex = this._stripComments(input);
  17971. const tokens = this._lexer.tokenize(sourceToLex);
  17972. const ast = new _ParseAST(input, location, absoluteOffset, tokens, 1 /* ParseFlags.Action */, this.errors, 0).parseChain();
  17973. return new ASTWithSource(ast, input, location, absoluteOffset, this.errors);
  17974. }
  17975. parseBinding(input, location, absoluteOffset, interpolationConfig = DEFAULT_INTERPOLATION_CONFIG) {
  17976. const ast = this._parseBindingAst(input, location, absoluteOffset, interpolationConfig);
  17977. return new ASTWithSource(ast, input, location, absoluteOffset, this.errors);
  17978. }
  17979. checkSimpleExpression(ast) {
  17980. const checker = new SimpleExpressionChecker();
  17981. ast.visit(checker);
  17982. return checker.errors;
  17983. }
  17984. // Host bindings parsed here
  17985. parseSimpleBinding(input, location, absoluteOffset, interpolationConfig = DEFAULT_INTERPOLATION_CONFIG) {
  17986. const ast = this._parseBindingAst(input, location, absoluteOffset, interpolationConfig);
  17987. const errors = this.checkSimpleExpression(ast);
  17988. if (errors.length > 0) {
  17989. this._reportError(`Host binding expression cannot contain ${errors.join(' ')}`, input, location);
  17990. }
  17991. return new ASTWithSource(ast, input, location, absoluteOffset, this.errors);
  17992. }
  17993. _reportError(message, input, errLocation, ctxLocation) {
  17994. this.errors.push(new ParserError(message, input, errLocation, ctxLocation));
  17995. }
  17996. _parseBindingAst(input, location, absoluteOffset, interpolationConfig) {
  17997. this._checkNoInterpolation(input, location, interpolationConfig);
  17998. const sourceToLex = this._stripComments(input);
  17999. const tokens = this._lexer.tokenize(sourceToLex);
  18000. return new _ParseAST(input, location, absoluteOffset, tokens, 0 /* ParseFlags.None */, this.errors, 0).parseChain();
  18001. }
  18002. /**
  18003. * Parse microsyntax template expression and return a list of bindings or
  18004. * parsing errors in case the given expression is invalid.
  18005. *
  18006. * For example,
  18007. * ```html
  18008. * <div *ngFor="let item of items">
  18009. * ^ ^ absoluteValueOffset for `templateValue`
  18010. * absoluteKeyOffset for `templateKey`
  18011. * ```
  18012. * contains three bindings:
  18013. * 1. ngFor -> null
  18014. * 2. item -> NgForOfContext.$implicit
  18015. * 3. ngForOf -> items
  18016. *
  18017. * This is apparent from the de-sugared template:
  18018. * ```html
  18019. * <ng-template ngFor let-item [ngForOf]="items">
  18020. * ```
  18021. *
  18022. * @param templateKey name of directive, without the * prefix. For example: ngIf, ngFor
  18023. * @param templateValue RHS of the microsyntax attribute
  18024. * @param templateUrl template filename if it's external, component filename if it's inline
  18025. * @param absoluteKeyOffset start of the `templateKey`
  18026. * @param absoluteValueOffset start of the `templateValue`
  18027. */
  18028. parseTemplateBindings(templateKey, templateValue, templateUrl, absoluteKeyOffset, absoluteValueOffset) {
  18029. const tokens = this._lexer.tokenize(templateValue);
  18030. const parser = new _ParseAST(templateValue, templateUrl, absoluteValueOffset, tokens, 0 /* ParseFlags.None */, this.errors, 0 /* relative offset */);
  18031. return parser.parseTemplateBindings({
  18032. source: templateKey,
  18033. span: new AbsoluteSourceSpan(absoluteKeyOffset, absoluteKeyOffset + templateKey.length),
  18034. });
  18035. }
  18036. parseInterpolation(input, location, absoluteOffset, interpolatedTokens, interpolationConfig = DEFAULT_INTERPOLATION_CONFIG) {
  18037. const { strings, expressions, offsets } = this.splitInterpolation(input, location, interpolatedTokens, interpolationConfig);
  18038. if (expressions.length === 0)
  18039. return null;
  18040. const expressionNodes = [];
  18041. for (let i = 0; i < expressions.length; ++i) {
  18042. const expressionText = expressions[i].text;
  18043. const sourceToLex = this._stripComments(expressionText);
  18044. const tokens = this._lexer.tokenize(sourceToLex);
  18045. const ast = new _ParseAST(input, location, absoluteOffset, tokens, 0 /* ParseFlags.None */, this.errors, offsets[i]).parseChain();
  18046. expressionNodes.push(ast);
  18047. }
  18048. return this.createInterpolationAst(strings.map((s) => s.text), expressionNodes, input, location, absoluteOffset);
  18049. }
  18050. /**
  18051. * Similar to `parseInterpolation`, but treats the provided string as a single expression
  18052. * element that would normally appear within the interpolation prefix and suffix (`{{` and `}}`).
  18053. * This is used for parsing the switch expression in ICUs.
  18054. */
  18055. parseInterpolationExpression(expression, location, absoluteOffset) {
  18056. const sourceToLex = this._stripComments(expression);
  18057. const tokens = this._lexer.tokenize(sourceToLex);
  18058. const ast = new _ParseAST(expression, location, absoluteOffset, tokens, 0 /* ParseFlags.None */, this.errors, 0).parseChain();
  18059. const strings = ['', '']; // The prefix and suffix strings are both empty
  18060. return this.createInterpolationAst(strings, [ast], expression, location, absoluteOffset);
  18061. }
  18062. createInterpolationAst(strings, expressions, input, location, absoluteOffset) {
  18063. const span = new ParseSpan(0, input.length);
  18064. const interpolation = new Interpolation$1(span, span.toAbsolute(absoluteOffset), strings, expressions);
  18065. return new ASTWithSource(interpolation, input, location, absoluteOffset, this.errors);
  18066. }
  18067. /**
  18068. * Splits a string of text into "raw" text segments and expressions present in interpolations in
  18069. * the string.
  18070. * Returns `null` if there are no interpolations, otherwise a
  18071. * `SplitInterpolation` with splits that look like
  18072. * <raw text> <expression> <raw text> ... <raw text> <expression> <raw text>
  18073. */
  18074. splitInterpolation(input, location, interpolatedTokens, interpolationConfig = DEFAULT_INTERPOLATION_CONFIG) {
  18075. const strings = [];
  18076. const expressions = [];
  18077. const offsets = [];
  18078. const inputToTemplateIndexMap = interpolatedTokens
  18079. ? getIndexMapForOriginalTemplate(interpolatedTokens)
  18080. : null;
  18081. let i = 0;
  18082. let atInterpolation = false;
  18083. let extendLastString = false;
  18084. let { start: interpStart, end: interpEnd } = interpolationConfig;
  18085. while (i < input.length) {
  18086. if (!atInterpolation) {
  18087. // parse until starting {{
  18088. const start = i;
  18089. i = input.indexOf(interpStart, i);
  18090. if (i === -1) {
  18091. i = input.length;
  18092. }
  18093. const text = input.substring(start, i);
  18094. strings.push({ text, start, end: i });
  18095. atInterpolation = true;
  18096. }
  18097. else {
  18098. // parse from starting {{ to ending }} while ignoring content inside quotes.
  18099. const fullStart = i;
  18100. const exprStart = fullStart + interpStart.length;
  18101. const exprEnd = this._getInterpolationEndIndex(input, interpEnd, exprStart);
  18102. if (exprEnd === -1) {
  18103. // Could not find the end of the interpolation; do not parse an expression.
  18104. // Instead we should extend the content on the last raw string.
  18105. atInterpolation = false;
  18106. extendLastString = true;
  18107. break;
  18108. }
  18109. const fullEnd = exprEnd + interpEnd.length;
  18110. const text = input.substring(exprStart, exprEnd);
  18111. if (text.trim().length === 0) {
  18112. this._reportError('Blank expressions are not allowed in interpolated strings', input, `at column ${i} in`, location);
  18113. }
  18114. expressions.push({ text, start: fullStart, end: fullEnd });
  18115. const startInOriginalTemplate = inputToTemplateIndexMap?.get(fullStart) ?? fullStart;
  18116. const offset = startInOriginalTemplate + interpStart.length;
  18117. offsets.push(offset);
  18118. i = fullEnd;
  18119. atInterpolation = false;
  18120. }
  18121. }
  18122. if (!atInterpolation) {
  18123. // If we are now at a text section, add the remaining content as a raw string.
  18124. if (extendLastString) {
  18125. const piece = strings[strings.length - 1];
  18126. piece.text += input.substring(i);
  18127. piece.end = input.length;
  18128. }
  18129. else {
  18130. strings.push({ text: input.substring(i), start: i, end: input.length });
  18131. }
  18132. }
  18133. return new SplitInterpolation(strings, expressions, offsets);
  18134. }
  18135. wrapLiteralPrimitive(input, location, absoluteOffset) {
  18136. const span = new ParseSpan(0, input == null ? 0 : input.length);
  18137. return new ASTWithSource(new LiteralPrimitive(span, span.toAbsolute(absoluteOffset), input), input, location, absoluteOffset, this.errors);
  18138. }
  18139. _stripComments(input) {
  18140. const i = this._commentStart(input);
  18141. return i != null ? input.substring(0, i) : input;
  18142. }
  18143. _commentStart(input) {
  18144. let outerQuote = null;
  18145. for (let i = 0; i < input.length - 1; i++) {
  18146. const char = input.charCodeAt(i);
  18147. const nextChar = input.charCodeAt(i + 1);
  18148. if (char === $SLASH && nextChar == $SLASH && outerQuote == null)
  18149. return i;
  18150. if (outerQuote === char) {
  18151. outerQuote = null;
  18152. }
  18153. else if (outerQuote == null && isQuote(char)) {
  18154. outerQuote = char;
  18155. }
  18156. }
  18157. return null;
  18158. }
  18159. _checkNoInterpolation(input, location, { start, end }) {
  18160. let startIndex = -1;
  18161. let endIndex = -1;
  18162. for (const charIndex of this._forEachUnquotedChar(input, 0)) {
  18163. if (startIndex === -1) {
  18164. if (input.startsWith(start)) {
  18165. startIndex = charIndex;
  18166. }
  18167. }
  18168. else {
  18169. endIndex = this._getInterpolationEndIndex(input, end, charIndex);
  18170. if (endIndex > -1) {
  18171. break;
  18172. }
  18173. }
  18174. }
  18175. if (startIndex > -1 && endIndex > -1) {
  18176. this._reportError(`Got interpolation (${start}${end}) where expression was expected`, input, `at column ${startIndex} in`, location);
  18177. }
  18178. }
  18179. /**
  18180. * Finds the index of the end of an interpolation expression
  18181. * while ignoring comments and quoted content.
  18182. */
  18183. _getInterpolationEndIndex(input, expressionEnd, start) {
  18184. for (const charIndex of this._forEachUnquotedChar(input, start)) {
  18185. if (input.startsWith(expressionEnd, charIndex)) {
  18186. return charIndex;
  18187. }
  18188. // Nothing else in the expression matters after we've
  18189. // hit a comment so look directly for the end token.
  18190. if (input.startsWith('//', charIndex)) {
  18191. return input.indexOf(expressionEnd, charIndex);
  18192. }
  18193. }
  18194. return -1;
  18195. }
  18196. /**
  18197. * Generator used to iterate over the character indexes of a string that are outside of quotes.
  18198. * @param input String to loop through.
  18199. * @param start Index within the string at which to start.
  18200. */
  18201. *_forEachUnquotedChar(input, start) {
  18202. let currentQuote = null;
  18203. let escapeCount = 0;
  18204. for (let i = start; i < input.length; i++) {
  18205. const char = input[i];
  18206. // Skip the characters inside quotes. Note that we only care about the outer-most
  18207. // quotes matching up and we need to account for escape characters.
  18208. if (isQuote(input.charCodeAt(i)) &&
  18209. (currentQuote === null || currentQuote === char) &&
  18210. escapeCount % 2 === 0) {
  18211. currentQuote = currentQuote === null ? char : null;
  18212. }
  18213. else if (currentQuote === null) {
  18214. yield i;
  18215. }
  18216. escapeCount = char === '\\' ? escapeCount + 1 : 0;
  18217. }
  18218. }
  18219. }
  18220. /** Describes a stateful context an expression parser is in. */
  18221. var ParseContextFlags;
  18222. (function (ParseContextFlags) {
  18223. ParseContextFlags[ParseContextFlags["None"] = 0] = "None";
  18224. /**
  18225. * A Writable context is one in which a value may be written to an lvalue.
  18226. * For example, after we see a property access, we may expect a write to the
  18227. * property via the "=" operator.
  18228. * prop
  18229. * ^ possible "=" after
  18230. */
  18231. ParseContextFlags[ParseContextFlags["Writable"] = 1] = "Writable";
  18232. })(ParseContextFlags || (ParseContextFlags = {}));
  18233. class _ParseAST {
  18234. input;
  18235. location;
  18236. absoluteOffset;
  18237. tokens;
  18238. parseFlags;
  18239. errors;
  18240. offset;
  18241. rparensExpected = 0;
  18242. rbracketsExpected = 0;
  18243. rbracesExpected = 0;
  18244. context = ParseContextFlags.None;
  18245. // Cache of expression start and input indeces to the absolute source span they map to, used to
  18246. // prevent creating superfluous source spans in `sourceSpan`.
  18247. // A serial of the expression start and input index is used for mapping because both are stateful
  18248. // and may change for subsequent expressions visited by the parser.
  18249. sourceSpanCache = new Map();
  18250. index = 0;
  18251. constructor(input, location, absoluteOffset, tokens, parseFlags, errors, offset) {
  18252. this.input = input;
  18253. this.location = location;
  18254. this.absoluteOffset = absoluteOffset;
  18255. this.tokens = tokens;
  18256. this.parseFlags = parseFlags;
  18257. this.errors = errors;
  18258. this.offset = offset;
  18259. }
  18260. peek(offset) {
  18261. const i = this.index + offset;
  18262. return i < this.tokens.length ? this.tokens[i] : EOF;
  18263. }
  18264. get next() {
  18265. return this.peek(0);
  18266. }
  18267. /** Whether all the parser input has been processed. */
  18268. get atEOF() {
  18269. return this.index >= this.tokens.length;
  18270. }
  18271. /**
  18272. * Index of the next token to be processed, or the end of the last token if all have been
  18273. * processed.
  18274. */
  18275. get inputIndex() {
  18276. return this.atEOF ? this.currentEndIndex : this.next.index + this.offset;
  18277. }
  18278. /**
  18279. * End index of the last processed token, or the start of the first token if none have been
  18280. * processed.
  18281. */
  18282. get currentEndIndex() {
  18283. if (this.index > 0) {
  18284. const curToken = this.peek(-1);
  18285. return curToken.end + this.offset;
  18286. }
  18287. // No tokens have been processed yet; return the next token's start or the length of the input
  18288. // if there is no token.
  18289. if (this.tokens.length === 0) {
  18290. return this.input.length + this.offset;
  18291. }
  18292. return this.next.index + this.offset;
  18293. }
  18294. /**
  18295. * Returns the absolute offset of the start of the current token.
  18296. */
  18297. get currentAbsoluteOffset() {
  18298. return this.absoluteOffset + this.inputIndex;
  18299. }
  18300. /**
  18301. * Retrieve a `ParseSpan` from `start` to the current position (or to `artificialEndIndex` if
  18302. * provided).
  18303. *
  18304. * @param start Position from which the `ParseSpan` will start.
  18305. * @param artificialEndIndex Optional ending index to be used if provided (and if greater than the
  18306. * natural ending index)
  18307. */
  18308. span(start, artificialEndIndex) {
  18309. let endIndex = this.currentEndIndex;
  18310. if (artificialEndIndex !== undefined && artificialEndIndex > this.currentEndIndex) {
  18311. endIndex = artificialEndIndex;
  18312. }
  18313. // In some unusual parsing scenarios (like when certain tokens are missing and an `EmptyExpr` is
  18314. // being created), the current token may already be advanced beyond the `currentEndIndex`. This
  18315. // appears to be a deep-seated parser bug.
  18316. //
  18317. // As a workaround for now, swap the start and end indices to ensure a valid `ParseSpan`.
  18318. // TODO(alxhub): fix the bug upstream in the parser state, and remove this workaround.
  18319. if (start > endIndex) {
  18320. const tmp = endIndex;
  18321. endIndex = start;
  18322. start = tmp;
  18323. }
  18324. return new ParseSpan(start, endIndex);
  18325. }
  18326. sourceSpan(start, artificialEndIndex) {
  18327. const serial = `${start}@${this.inputIndex}:${artificialEndIndex}`;
  18328. if (!this.sourceSpanCache.has(serial)) {
  18329. this.sourceSpanCache.set(serial, this.span(start, artificialEndIndex).toAbsolute(this.absoluteOffset));
  18330. }
  18331. return this.sourceSpanCache.get(serial);
  18332. }
  18333. advance() {
  18334. this.index++;
  18335. }
  18336. /**
  18337. * Executes a callback in the provided context.
  18338. */
  18339. withContext(context, cb) {
  18340. this.context |= context;
  18341. const ret = cb();
  18342. this.context ^= context;
  18343. return ret;
  18344. }
  18345. consumeOptionalCharacter(code) {
  18346. if (this.next.isCharacter(code)) {
  18347. this.advance();
  18348. return true;
  18349. }
  18350. else {
  18351. return false;
  18352. }
  18353. }
  18354. peekKeywordLet() {
  18355. return this.next.isKeywordLet();
  18356. }
  18357. peekKeywordAs() {
  18358. return this.next.isKeywordAs();
  18359. }
  18360. /**
  18361. * Consumes an expected character, otherwise emits an error about the missing expected character
  18362. * and skips over the token stream until reaching a recoverable point.
  18363. *
  18364. * See `this.error` and `this.skip` for more details.
  18365. */
  18366. expectCharacter(code) {
  18367. if (this.consumeOptionalCharacter(code))
  18368. return;
  18369. this.error(`Missing expected ${String.fromCharCode(code)}`);
  18370. }
  18371. consumeOptionalOperator(op) {
  18372. if (this.next.isOperator(op)) {
  18373. this.advance();
  18374. return true;
  18375. }
  18376. else {
  18377. return false;
  18378. }
  18379. }
  18380. expectOperator(operator) {
  18381. if (this.consumeOptionalOperator(operator))
  18382. return;
  18383. this.error(`Missing expected operator ${operator}`);
  18384. }
  18385. prettyPrintToken(tok) {
  18386. return tok === EOF ? 'end of input' : `token ${tok}`;
  18387. }
  18388. expectIdentifierOrKeyword() {
  18389. const n = this.next;
  18390. if (!n.isIdentifier() && !n.isKeyword()) {
  18391. if (n.isPrivateIdentifier()) {
  18392. this._reportErrorForPrivateIdentifier(n, 'expected identifier or keyword');
  18393. }
  18394. else {
  18395. this.error(`Unexpected ${this.prettyPrintToken(n)}, expected identifier or keyword`);
  18396. }
  18397. return null;
  18398. }
  18399. this.advance();
  18400. return n.toString();
  18401. }
  18402. expectIdentifierOrKeywordOrString() {
  18403. const n = this.next;
  18404. if (!n.isIdentifier() && !n.isKeyword() && !n.isString()) {
  18405. if (n.isPrivateIdentifier()) {
  18406. this._reportErrorForPrivateIdentifier(n, 'expected identifier, keyword or string');
  18407. }
  18408. else {
  18409. this.error(`Unexpected ${this.prettyPrintToken(n)}, expected identifier, keyword, or string`);
  18410. }
  18411. return '';
  18412. }
  18413. this.advance();
  18414. return n.toString();
  18415. }
  18416. parseChain() {
  18417. const exprs = [];
  18418. const start = this.inputIndex;
  18419. while (this.index < this.tokens.length) {
  18420. const expr = this.parsePipe();
  18421. exprs.push(expr);
  18422. if (this.consumeOptionalCharacter($SEMICOLON)) {
  18423. if (!(this.parseFlags & 1 /* ParseFlags.Action */)) {
  18424. this.error('Binding expression cannot contain chained expression');
  18425. }
  18426. while (this.consumeOptionalCharacter($SEMICOLON)) { } // read all semicolons
  18427. }
  18428. else if (this.index < this.tokens.length) {
  18429. const errorIndex = this.index;
  18430. this.error(`Unexpected token '${this.next}'`);
  18431. // The `error` call above will skip ahead to the next recovery point in an attempt to
  18432. // recover part of the expression, but that might be the token we started from which will
  18433. // lead to an infinite loop. If that's the case, break the loop assuming that we can't
  18434. // parse further.
  18435. if (this.index === errorIndex) {
  18436. break;
  18437. }
  18438. }
  18439. }
  18440. if (exprs.length === 0) {
  18441. // We have no expressions so create an empty expression that spans the entire input length
  18442. const artificialStart = this.offset;
  18443. const artificialEnd = this.offset + this.input.length;
  18444. return new EmptyExpr$1(this.span(artificialStart, artificialEnd), this.sourceSpan(artificialStart, artificialEnd));
  18445. }
  18446. if (exprs.length == 1)
  18447. return exprs[0];
  18448. return new Chain(this.span(start), this.sourceSpan(start), exprs);
  18449. }
  18450. parsePipe() {
  18451. const start = this.inputIndex;
  18452. let result = this.parseExpression();
  18453. if (this.consumeOptionalOperator('|')) {
  18454. if (this.parseFlags & 1 /* ParseFlags.Action */) {
  18455. this.error(`Cannot have a pipe in an action expression`);
  18456. }
  18457. do {
  18458. const nameStart = this.inputIndex;
  18459. let nameId = this.expectIdentifierOrKeyword();
  18460. let nameSpan;
  18461. let fullSpanEnd = undefined;
  18462. if (nameId !== null) {
  18463. nameSpan = this.sourceSpan(nameStart);
  18464. }
  18465. else {
  18466. // No valid identifier was found, so we'll assume an empty pipe name ('').
  18467. nameId = '';
  18468. // However, there may have been whitespace present between the pipe character and the next
  18469. // token in the sequence (or the end of input). We want to track this whitespace so that
  18470. // the `BindingPipe` we produce covers not just the pipe character, but any trailing
  18471. // whitespace beyond it. Another way of thinking about this is that the zero-length name
  18472. // is assumed to be at the end of any whitespace beyond the pipe character.
  18473. //
  18474. // Therefore, we push the end of the `ParseSpan` for this pipe all the way up to the
  18475. // beginning of the next token, or until the end of input if the next token is EOF.
  18476. fullSpanEnd = this.next.index !== -1 ? this.next.index : this.input.length + this.offset;
  18477. // The `nameSpan` for an empty pipe name is zero-length at the end of any whitespace
  18478. // beyond the pipe character.
  18479. nameSpan = new ParseSpan(fullSpanEnd, fullSpanEnd).toAbsolute(this.absoluteOffset);
  18480. }
  18481. const args = [];
  18482. while (this.consumeOptionalCharacter($COLON)) {
  18483. args.push(this.parseExpression());
  18484. // If there are additional expressions beyond the name, then the artificial end for the
  18485. // name is no longer relevant.
  18486. }
  18487. result = new BindingPipe(this.span(start), this.sourceSpan(start, fullSpanEnd), result, nameId, args, nameSpan);
  18488. } while (this.consumeOptionalOperator('|'));
  18489. }
  18490. return result;
  18491. }
  18492. parseExpression() {
  18493. return this.parseConditional();
  18494. }
  18495. parseConditional() {
  18496. const start = this.inputIndex;
  18497. const result = this.parseLogicalOr();
  18498. if (this.consumeOptionalOperator('?')) {
  18499. const yes = this.parsePipe();
  18500. let no;
  18501. if (!this.consumeOptionalCharacter($COLON)) {
  18502. const end = this.inputIndex;
  18503. const expression = this.input.substring(start, end);
  18504. this.error(`Conditional expression ${expression} requires all 3 expressions`);
  18505. no = new EmptyExpr$1(this.span(start), this.sourceSpan(start));
  18506. }
  18507. else {
  18508. no = this.parsePipe();
  18509. }
  18510. return new Conditional(this.span(start), this.sourceSpan(start), result, yes, no);
  18511. }
  18512. else {
  18513. return result;
  18514. }
  18515. }
  18516. parseLogicalOr() {
  18517. // '||'
  18518. const start = this.inputIndex;
  18519. let result = this.parseLogicalAnd();
  18520. while (this.consumeOptionalOperator('||')) {
  18521. const right = this.parseLogicalAnd();
  18522. result = new Binary(this.span(start), this.sourceSpan(start), '||', result, right);
  18523. }
  18524. return result;
  18525. }
  18526. parseLogicalAnd() {
  18527. // '&&'
  18528. const start = this.inputIndex;
  18529. let result = this.parseNullishCoalescing();
  18530. while (this.consumeOptionalOperator('&&')) {
  18531. const right = this.parseNullishCoalescing();
  18532. result = new Binary(this.span(start), this.sourceSpan(start), '&&', result, right);
  18533. }
  18534. return result;
  18535. }
  18536. parseNullishCoalescing() {
  18537. // '??'
  18538. const start = this.inputIndex;
  18539. let result = this.parseEquality();
  18540. while (this.consumeOptionalOperator('??')) {
  18541. const right = this.parseEquality();
  18542. result = new Binary(this.span(start), this.sourceSpan(start), '??', result, right);
  18543. }
  18544. return result;
  18545. }
  18546. parseEquality() {
  18547. // '==','!=','===','!=='
  18548. const start = this.inputIndex;
  18549. let result = this.parseRelational();
  18550. while (this.next.type == TokenType.Operator) {
  18551. const operator = this.next.strValue;
  18552. switch (operator) {
  18553. case '==':
  18554. case '===':
  18555. case '!=':
  18556. case '!==':
  18557. this.advance();
  18558. const right = this.parseRelational();
  18559. result = new Binary(this.span(start), this.sourceSpan(start), operator, result, right);
  18560. continue;
  18561. }
  18562. break;
  18563. }
  18564. return result;
  18565. }
  18566. parseRelational() {
  18567. // '<', '>', '<=', '>='
  18568. const start = this.inputIndex;
  18569. let result = this.parseAdditive();
  18570. while (this.next.type == TokenType.Operator) {
  18571. const operator = this.next.strValue;
  18572. switch (operator) {
  18573. case '<':
  18574. case '>':
  18575. case '<=':
  18576. case '>=':
  18577. this.advance();
  18578. const right = this.parseAdditive();
  18579. result = new Binary(this.span(start), this.sourceSpan(start), operator, result, right);
  18580. continue;
  18581. }
  18582. break;
  18583. }
  18584. return result;
  18585. }
  18586. parseAdditive() {
  18587. // '+', '-'
  18588. const start = this.inputIndex;
  18589. let result = this.parseMultiplicative();
  18590. while (this.next.type == TokenType.Operator) {
  18591. const operator = this.next.strValue;
  18592. switch (operator) {
  18593. case '+':
  18594. case '-':
  18595. this.advance();
  18596. let right = this.parseMultiplicative();
  18597. result = new Binary(this.span(start), this.sourceSpan(start), operator, result, right);
  18598. continue;
  18599. }
  18600. break;
  18601. }
  18602. return result;
  18603. }
  18604. parseMultiplicative() {
  18605. // '*', '%', '/'
  18606. const start = this.inputIndex;
  18607. let result = this.parsePrefix();
  18608. while (this.next.type == TokenType.Operator) {
  18609. const operator = this.next.strValue;
  18610. switch (operator) {
  18611. case '*':
  18612. case '%':
  18613. case '/':
  18614. this.advance();
  18615. let right = this.parsePrefix();
  18616. result = new Binary(this.span(start), this.sourceSpan(start), operator, result, right);
  18617. continue;
  18618. }
  18619. break;
  18620. }
  18621. return result;
  18622. }
  18623. parsePrefix() {
  18624. if (this.next.type == TokenType.Operator) {
  18625. const start = this.inputIndex;
  18626. const operator = this.next.strValue;
  18627. let result;
  18628. switch (operator) {
  18629. case '+':
  18630. this.advance();
  18631. result = this.parsePrefix();
  18632. return Unary.createPlus(this.span(start), this.sourceSpan(start), result);
  18633. case '-':
  18634. this.advance();
  18635. result = this.parsePrefix();
  18636. return Unary.createMinus(this.span(start), this.sourceSpan(start), result);
  18637. case '!':
  18638. this.advance();
  18639. result = this.parsePrefix();
  18640. return new PrefixNot(this.span(start), this.sourceSpan(start), result);
  18641. }
  18642. }
  18643. else if (this.next.isKeywordTypeof()) {
  18644. this.advance();
  18645. const start = this.inputIndex;
  18646. let result = this.parsePrefix();
  18647. return new TypeofExpression(this.span(start), this.sourceSpan(start), result);
  18648. }
  18649. return this.parseCallChain();
  18650. }
  18651. parseCallChain() {
  18652. const start = this.inputIndex;
  18653. let result = this.parsePrimary();
  18654. while (true) {
  18655. if (this.consumeOptionalCharacter($PERIOD)) {
  18656. result = this.parseAccessMember(result, start, false);
  18657. }
  18658. else if (this.consumeOptionalOperator('?.')) {
  18659. if (this.consumeOptionalCharacter($LPAREN)) {
  18660. result = this.parseCall(result, start, true);
  18661. }
  18662. else {
  18663. result = this.consumeOptionalCharacter($LBRACKET)
  18664. ? this.parseKeyedReadOrWrite(result, start, true)
  18665. : this.parseAccessMember(result, start, true);
  18666. }
  18667. }
  18668. else if (this.consumeOptionalCharacter($LBRACKET)) {
  18669. result = this.parseKeyedReadOrWrite(result, start, false);
  18670. }
  18671. else if (this.consumeOptionalCharacter($LPAREN)) {
  18672. result = this.parseCall(result, start, false);
  18673. }
  18674. else if (this.consumeOptionalOperator('!')) {
  18675. result = new NonNullAssert(this.span(start), this.sourceSpan(start), result);
  18676. }
  18677. else {
  18678. return result;
  18679. }
  18680. }
  18681. }
  18682. parsePrimary() {
  18683. const start = this.inputIndex;
  18684. if (this.consumeOptionalCharacter($LPAREN)) {
  18685. this.rparensExpected++;
  18686. const result = this.parsePipe();
  18687. this.rparensExpected--;
  18688. this.expectCharacter($RPAREN);
  18689. return result;
  18690. }
  18691. else if (this.next.isKeywordNull()) {
  18692. this.advance();
  18693. return new LiteralPrimitive(this.span(start), this.sourceSpan(start), null);
  18694. }
  18695. else if (this.next.isKeywordUndefined()) {
  18696. this.advance();
  18697. return new LiteralPrimitive(this.span(start), this.sourceSpan(start), void 0);
  18698. }
  18699. else if (this.next.isKeywordTrue()) {
  18700. this.advance();
  18701. return new LiteralPrimitive(this.span(start), this.sourceSpan(start), true);
  18702. }
  18703. else if (this.next.isKeywordFalse()) {
  18704. this.advance();
  18705. return new LiteralPrimitive(this.span(start), this.sourceSpan(start), false);
  18706. }
  18707. else if (this.next.isKeywordThis()) {
  18708. this.advance();
  18709. return new ThisReceiver(this.span(start), this.sourceSpan(start));
  18710. }
  18711. else if (this.consumeOptionalCharacter($LBRACKET)) {
  18712. this.rbracketsExpected++;
  18713. const elements = this.parseExpressionList($RBRACKET);
  18714. this.rbracketsExpected--;
  18715. this.expectCharacter($RBRACKET);
  18716. return new LiteralArray(this.span(start), this.sourceSpan(start), elements);
  18717. }
  18718. else if (this.next.isCharacter($LBRACE)) {
  18719. return this.parseLiteralMap();
  18720. }
  18721. else if (this.next.isIdentifier()) {
  18722. return this.parseAccessMember(new ImplicitReceiver(this.span(start), this.sourceSpan(start)), start, false);
  18723. }
  18724. else if (this.next.isNumber()) {
  18725. const value = this.next.toNumber();
  18726. this.advance();
  18727. return new LiteralPrimitive(this.span(start), this.sourceSpan(start), value);
  18728. }
  18729. else if (this.next.isTemplateLiteralEnd()) {
  18730. return this.parseNoInterpolationTemplateLiteral();
  18731. }
  18732. else if (this.next.isTemplateLiteralPart()) {
  18733. return this.parseTemplateLiteral();
  18734. }
  18735. else if (this.next.isString() && this.next.kind === StringTokenKind.Plain) {
  18736. const literalValue = this.next.toString();
  18737. this.advance();
  18738. return new LiteralPrimitive(this.span(start), this.sourceSpan(start), literalValue);
  18739. }
  18740. else if (this.next.isPrivateIdentifier()) {
  18741. this._reportErrorForPrivateIdentifier(this.next, null);
  18742. return new EmptyExpr$1(this.span(start), this.sourceSpan(start));
  18743. }
  18744. else if (this.index >= this.tokens.length) {
  18745. this.error(`Unexpected end of expression: ${this.input}`);
  18746. return new EmptyExpr$1(this.span(start), this.sourceSpan(start));
  18747. }
  18748. else {
  18749. this.error(`Unexpected token ${this.next}`);
  18750. return new EmptyExpr$1(this.span(start), this.sourceSpan(start));
  18751. }
  18752. }
  18753. parseExpressionList(terminator) {
  18754. const result = [];
  18755. do {
  18756. if (!this.next.isCharacter(terminator)) {
  18757. result.push(this.parsePipe());
  18758. }
  18759. else {
  18760. break;
  18761. }
  18762. } while (this.consumeOptionalCharacter($COMMA));
  18763. return result;
  18764. }
  18765. parseLiteralMap() {
  18766. const keys = [];
  18767. const values = [];
  18768. const start = this.inputIndex;
  18769. this.expectCharacter($LBRACE);
  18770. if (!this.consumeOptionalCharacter($RBRACE)) {
  18771. this.rbracesExpected++;
  18772. do {
  18773. const keyStart = this.inputIndex;
  18774. const quoted = this.next.isString();
  18775. const key = this.expectIdentifierOrKeywordOrString();
  18776. const literalMapKey = { key, quoted };
  18777. keys.push(literalMapKey);
  18778. // Properties with quoted keys can't use the shorthand syntax.
  18779. if (quoted) {
  18780. this.expectCharacter($COLON);
  18781. values.push(this.parsePipe());
  18782. }
  18783. else if (this.consumeOptionalCharacter($COLON)) {
  18784. values.push(this.parsePipe());
  18785. }
  18786. else {
  18787. literalMapKey.isShorthandInitialized = true;
  18788. const span = this.span(keyStart);
  18789. const sourceSpan = this.sourceSpan(keyStart);
  18790. values.push(new PropertyRead(span, sourceSpan, sourceSpan, new ImplicitReceiver(span, sourceSpan), key));
  18791. }
  18792. } while (this.consumeOptionalCharacter($COMMA) &&
  18793. !this.next.isCharacter($RBRACE));
  18794. this.rbracesExpected--;
  18795. this.expectCharacter($RBRACE);
  18796. }
  18797. return new LiteralMap(this.span(start), this.sourceSpan(start), keys, values);
  18798. }
  18799. parseAccessMember(readReceiver, start, isSafe) {
  18800. const nameStart = this.inputIndex;
  18801. const id = this.withContext(ParseContextFlags.Writable, () => {
  18802. const id = this.expectIdentifierOrKeyword() ?? '';
  18803. if (id.length === 0) {
  18804. this.error(`Expected identifier for property access`, readReceiver.span.end);
  18805. }
  18806. return id;
  18807. });
  18808. const nameSpan = this.sourceSpan(nameStart);
  18809. let receiver;
  18810. if (isSafe) {
  18811. if (this.consumeOptionalOperator('=')) {
  18812. this.error("The '?.' operator cannot be used in the assignment");
  18813. receiver = new EmptyExpr$1(this.span(start), this.sourceSpan(start));
  18814. }
  18815. else {
  18816. receiver = new SafePropertyRead(this.span(start), this.sourceSpan(start), nameSpan, readReceiver, id);
  18817. }
  18818. }
  18819. else {
  18820. if (this.consumeOptionalOperator('=')) {
  18821. if (!(this.parseFlags & 1 /* ParseFlags.Action */)) {
  18822. this.error('Bindings cannot contain assignments');
  18823. return new EmptyExpr$1(this.span(start), this.sourceSpan(start));
  18824. }
  18825. const value = this.parseConditional();
  18826. receiver = new PropertyWrite(this.span(start), this.sourceSpan(start), nameSpan, readReceiver, id, value);
  18827. }
  18828. else {
  18829. receiver = new PropertyRead(this.span(start), this.sourceSpan(start), nameSpan, readReceiver, id);
  18830. }
  18831. }
  18832. return receiver;
  18833. }
  18834. parseCall(receiver, start, isSafe) {
  18835. const argumentStart = this.inputIndex;
  18836. this.rparensExpected++;
  18837. const args = this.parseCallArguments();
  18838. const argumentSpan = this.span(argumentStart, this.inputIndex).toAbsolute(this.absoluteOffset);
  18839. this.expectCharacter($RPAREN);
  18840. this.rparensExpected--;
  18841. const span = this.span(start);
  18842. const sourceSpan = this.sourceSpan(start);
  18843. return isSafe
  18844. ? new SafeCall(span, sourceSpan, receiver, args, argumentSpan)
  18845. : new Call(span, sourceSpan, receiver, args, argumentSpan);
  18846. }
  18847. parseCallArguments() {
  18848. if (this.next.isCharacter($RPAREN))
  18849. return [];
  18850. const positionals = [];
  18851. do {
  18852. positionals.push(this.parsePipe());
  18853. } while (this.consumeOptionalCharacter($COMMA));
  18854. return positionals;
  18855. }
  18856. /**
  18857. * Parses an identifier, a keyword, a string with an optional `-` in between,
  18858. * and returns the string along with its absolute source span.
  18859. */
  18860. expectTemplateBindingKey() {
  18861. let result = '';
  18862. let operatorFound = false;
  18863. const start = this.currentAbsoluteOffset;
  18864. do {
  18865. result += this.expectIdentifierOrKeywordOrString();
  18866. operatorFound = this.consumeOptionalOperator('-');
  18867. if (operatorFound) {
  18868. result += '-';
  18869. }
  18870. } while (operatorFound);
  18871. return {
  18872. source: result,
  18873. span: new AbsoluteSourceSpan(start, start + result.length),
  18874. };
  18875. }
  18876. /**
  18877. * Parse microsyntax template expression and return a list of bindings or
  18878. * parsing errors in case the given expression is invalid.
  18879. *
  18880. * For example,
  18881. * ```html
  18882. * <div *ngFor="let item of items; index as i; trackBy: func">
  18883. * ```
  18884. * contains five bindings:
  18885. * 1. ngFor -> null
  18886. * 2. item -> NgForOfContext.$implicit
  18887. * 3. ngForOf -> items
  18888. * 4. i -> NgForOfContext.index
  18889. * 5. ngForTrackBy -> func
  18890. *
  18891. * For a full description of the microsyntax grammar, see
  18892. * https://gist.github.com/mhevery/d3530294cff2e4a1b3fe15ff75d08855
  18893. *
  18894. * @param templateKey name of the microsyntax directive, like ngIf, ngFor,
  18895. * without the *, along with its absolute span.
  18896. */
  18897. parseTemplateBindings(templateKey) {
  18898. const bindings = [];
  18899. // The first binding is for the template key itself
  18900. // In *ngFor="let item of items", key = "ngFor", value = null
  18901. // In *ngIf="cond | pipe", key = "ngIf", value = "cond | pipe"
  18902. bindings.push(...this.parseDirectiveKeywordBindings(templateKey));
  18903. while (this.index < this.tokens.length) {
  18904. // If it starts with 'let', then this must be variable declaration
  18905. const letBinding = this.parseLetBinding();
  18906. if (letBinding) {
  18907. bindings.push(letBinding);
  18908. }
  18909. else {
  18910. // Two possible cases here, either `value "as" key` or
  18911. // "directive-keyword expression". We don't know which case, but both
  18912. // "value" and "directive-keyword" are template binding key, so consume
  18913. // the key first.
  18914. const key = this.expectTemplateBindingKey();
  18915. // Peek at the next token, if it is "as" then this must be variable
  18916. // declaration.
  18917. const binding = this.parseAsBinding(key);
  18918. if (binding) {
  18919. bindings.push(binding);
  18920. }
  18921. else {
  18922. // Otherwise the key must be a directive keyword, like "of". Transform
  18923. // the key to actual key. Eg. of -> ngForOf, trackBy -> ngForTrackBy
  18924. key.source =
  18925. templateKey.source + key.source.charAt(0).toUpperCase() + key.source.substring(1);
  18926. bindings.push(...this.parseDirectiveKeywordBindings(key));
  18927. }
  18928. }
  18929. this.consumeStatementTerminator();
  18930. }
  18931. return new TemplateBindingParseResult(bindings, [] /* warnings */, this.errors);
  18932. }
  18933. parseKeyedReadOrWrite(receiver, start, isSafe) {
  18934. return this.withContext(ParseContextFlags.Writable, () => {
  18935. this.rbracketsExpected++;
  18936. const key = this.parsePipe();
  18937. if (key instanceof EmptyExpr$1) {
  18938. this.error(`Key access cannot be empty`);
  18939. }
  18940. this.rbracketsExpected--;
  18941. this.expectCharacter($RBRACKET);
  18942. if (this.consumeOptionalOperator('=')) {
  18943. if (isSafe) {
  18944. this.error("The '?.' operator cannot be used in the assignment");
  18945. }
  18946. else {
  18947. const value = this.parseConditional();
  18948. return new KeyedWrite(this.span(start), this.sourceSpan(start), receiver, key, value);
  18949. }
  18950. }
  18951. else {
  18952. return isSafe
  18953. ? new SafeKeyedRead(this.span(start), this.sourceSpan(start), receiver, key)
  18954. : new KeyedRead(this.span(start), this.sourceSpan(start), receiver, key);
  18955. }
  18956. return new EmptyExpr$1(this.span(start), this.sourceSpan(start));
  18957. });
  18958. }
  18959. /**
  18960. * Parse a directive keyword, followed by a mandatory expression.
  18961. * For example, "of items", "trackBy: func".
  18962. * The bindings are: ngForOf -> items, ngForTrackBy -> func
  18963. * There could be an optional "as" binding that follows the expression.
  18964. * For example,
  18965. * ```
  18966. * *ngFor="let item of items | slice:0:1 as collection".
  18967. * ^^ ^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^
  18968. * keyword bound target optional 'as' binding
  18969. * ```
  18970. *
  18971. * @param key binding key, for example, ngFor, ngIf, ngForOf, along with its
  18972. * absolute span.
  18973. */
  18974. parseDirectiveKeywordBindings(key) {
  18975. const bindings = [];
  18976. this.consumeOptionalCharacter($COLON); // trackBy: trackByFunction
  18977. const value = this.getDirectiveBoundTarget();
  18978. let spanEnd = this.currentAbsoluteOffset;
  18979. // The binding could optionally be followed by "as". For example,
  18980. // *ngIf="cond | pipe as x". In this case, the key in the "as" binding
  18981. // is "x" and the value is the template key itself ("ngIf"). Note that the
  18982. // 'key' in the current context now becomes the "value" in the next binding.
  18983. const asBinding = this.parseAsBinding(key);
  18984. if (!asBinding) {
  18985. this.consumeStatementTerminator();
  18986. spanEnd = this.currentAbsoluteOffset;
  18987. }
  18988. const sourceSpan = new AbsoluteSourceSpan(key.span.start, spanEnd);
  18989. bindings.push(new ExpressionBinding(sourceSpan, key, value));
  18990. if (asBinding) {
  18991. bindings.push(asBinding);
  18992. }
  18993. return bindings;
  18994. }
  18995. /**
  18996. * Return the expression AST for the bound target of a directive keyword
  18997. * binding. For example,
  18998. * ```
  18999. * *ngIf="condition | pipe"
  19000. * ^^^^^^^^^^^^^^^^ bound target for "ngIf"
  19001. * *ngFor="let item of items"
  19002. * ^^^^^ bound target for "ngForOf"
  19003. * ```
  19004. */
  19005. getDirectiveBoundTarget() {
  19006. if (this.next === EOF || this.peekKeywordAs() || this.peekKeywordLet()) {
  19007. return null;
  19008. }
  19009. const ast = this.parsePipe(); // example: "condition | async"
  19010. const { start, end } = ast.span;
  19011. const value = this.input.substring(start, end);
  19012. return new ASTWithSource(ast, value, this.location, this.absoluteOffset + start, this.errors);
  19013. }
  19014. /**
  19015. * Return the binding for a variable declared using `as`. Note that the order
  19016. * of the key-value pair in this declaration is reversed. For example,
  19017. * ```
  19018. * *ngFor="let item of items; index as i"
  19019. * ^^^^^ ^
  19020. * value key
  19021. * ```
  19022. *
  19023. * @param value name of the value in the declaration, "ngIf" in the example
  19024. * above, along with its absolute span.
  19025. */
  19026. parseAsBinding(value) {
  19027. if (!this.peekKeywordAs()) {
  19028. return null;
  19029. }
  19030. this.advance(); // consume the 'as' keyword
  19031. const key = this.expectTemplateBindingKey();
  19032. this.consumeStatementTerminator();
  19033. const sourceSpan = new AbsoluteSourceSpan(value.span.start, this.currentAbsoluteOffset);
  19034. return new VariableBinding(sourceSpan, key, value);
  19035. }
  19036. /**
  19037. * Return the binding for a variable declared using `let`. For example,
  19038. * ```
  19039. * *ngFor="let item of items; let i=index;"
  19040. * ^^^^^^^^ ^^^^^^^^^^^
  19041. * ```
  19042. * In the first binding, `item` is bound to `NgForOfContext.$implicit`.
  19043. * In the second binding, `i` is bound to `NgForOfContext.index`.
  19044. */
  19045. parseLetBinding() {
  19046. if (!this.peekKeywordLet()) {
  19047. return null;
  19048. }
  19049. const spanStart = this.currentAbsoluteOffset;
  19050. this.advance(); // consume the 'let' keyword
  19051. const key = this.expectTemplateBindingKey();
  19052. let value = null;
  19053. if (this.consumeOptionalOperator('=')) {
  19054. value = this.expectTemplateBindingKey();
  19055. }
  19056. this.consumeStatementTerminator();
  19057. const sourceSpan = new AbsoluteSourceSpan(spanStart, this.currentAbsoluteOffset);
  19058. return new VariableBinding(sourceSpan, key, value);
  19059. }
  19060. parseNoInterpolationTemplateLiteral() {
  19061. const text = this.next.strValue;
  19062. const start = this.inputIndex;
  19063. this.advance();
  19064. const span = this.span(start);
  19065. const sourceSpan = this.sourceSpan(start);
  19066. return new TemplateLiteral(span, sourceSpan, [new TemplateLiteralElement(span, sourceSpan, text)], []);
  19067. }
  19068. parseTemplateLiteral() {
  19069. const start = this.inputIndex;
  19070. const elements = [];
  19071. const expressions = [];
  19072. while (this.next !== EOF) {
  19073. const token = this.next;
  19074. if (token.isTemplateLiteralPart() || token.isTemplateLiteralEnd()) {
  19075. const partStart = this.inputIndex;
  19076. this.advance();
  19077. elements.push(new TemplateLiteralElement(this.span(partStart), this.sourceSpan(partStart), token.strValue));
  19078. if (token.isTemplateLiteralEnd()) {
  19079. break;
  19080. }
  19081. }
  19082. else if (token.isTemplateLiteralInterpolationStart()) {
  19083. this.advance();
  19084. const expression = this.parsePipe();
  19085. if (expression instanceof EmptyExpr$1) {
  19086. this.error('Template literal interpolation cannot be empty');
  19087. }
  19088. else {
  19089. expressions.push(expression);
  19090. }
  19091. }
  19092. else {
  19093. this.advance();
  19094. }
  19095. }
  19096. return new TemplateLiteral(this.span(start), this.sourceSpan(start), elements, expressions);
  19097. }
  19098. /**
  19099. * Consume the optional statement terminator: semicolon or comma.
  19100. */
  19101. consumeStatementTerminator() {
  19102. this.consumeOptionalCharacter($SEMICOLON) || this.consumeOptionalCharacter($COMMA);
  19103. }
  19104. /**
  19105. * Records an error and skips over the token stream until reaching a recoverable point. See
  19106. * `this.skip` for more details on token skipping.
  19107. */
  19108. error(message, index = null) {
  19109. this.errors.push(new ParserError(message, this.input, this.locationText(index), this.location));
  19110. this.skip();
  19111. }
  19112. locationText(index = null) {
  19113. if (index == null)
  19114. index = this.index;
  19115. return index < this.tokens.length
  19116. ? `at column ${this.tokens[index].index + 1} in`
  19117. : `at the end of the expression`;
  19118. }
  19119. /**
  19120. * Records an error for an unexpected private identifier being discovered.
  19121. * @param token Token representing a private identifier.
  19122. * @param extraMessage Optional additional message being appended to the error.
  19123. */
  19124. _reportErrorForPrivateIdentifier(token, extraMessage) {
  19125. let errorMessage = `Private identifiers are not supported. Unexpected private identifier: ${token}`;
  19126. if (extraMessage !== null) {
  19127. errorMessage += `, ${extraMessage}`;
  19128. }
  19129. this.error(errorMessage);
  19130. }
  19131. /**
  19132. * Error recovery should skip tokens until it encounters a recovery point.
  19133. *
  19134. * The following are treated as unconditional recovery points:
  19135. * - end of input
  19136. * - ';' (parseChain() is always the root production, and it expects a ';')
  19137. * - '|' (since pipes may be chained and each pipe expression may be treated independently)
  19138. *
  19139. * The following are conditional recovery points:
  19140. * - ')', '}', ']' if one of calling productions is expecting one of these symbols
  19141. * - This allows skip() to recover from errors such as '(a.) + 1' allowing more of the AST to
  19142. * be retained (it doesn't skip any tokens as the ')' is retained because of the '(' begins
  19143. * an '(' <expr> ')' production).
  19144. * The recovery points of grouping symbols must be conditional as they must be skipped if
  19145. * none of the calling productions are not expecting the closing token else we will never
  19146. * make progress in the case of an extraneous group closing symbol (such as a stray ')').
  19147. * That is, we skip a closing symbol if we are not in a grouping production.
  19148. * - '=' in a `Writable` context
  19149. * - In this context, we are able to recover after seeing the `=` operator, which
  19150. * signals the presence of an independent rvalue expression following the `=` operator.
  19151. *
  19152. * If a production expects one of these token it increments the corresponding nesting count,
  19153. * and then decrements it just prior to checking if the token is in the input.
  19154. */
  19155. skip() {
  19156. let n = this.next;
  19157. while (this.index < this.tokens.length &&
  19158. !n.isCharacter($SEMICOLON) &&
  19159. !n.isOperator('|') &&
  19160. (this.rparensExpected <= 0 || !n.isCharacter($RPAREN)) &&
  19161. (this.rbracesExpected <= 0 || !n.isCharacter($RBRACE)) &&
  19162. (this.rbracketsExpected <= 0 || !n.isCharacter($RBRACKET)) &&
  19163. (!(this.context & ParseContextFlags.Writable) || !n.isOperator('='))) {
  19164. if (this.next.isError()) {
  19165. this.errors.push(new ParserError(this.next.toString(), this.input, this.locationText(), this.location));
  19166. }
  19167. this.advance();
  19168. n = this.next;
  19169. }
  19170. }
  19171. }
  19172. class SimpleExpressionChecker extends RecursiveAstVisitor {
  19173. errors = [];
  19174. visitPipe() {
  19175. this.errors.push('pipes');
  19176. }
  19177. }
  19178. /**
  19179. * Computes the real offset in the original template for indexes in an interpolation.
  19180. *
  19181. * Because templates can have encoded HTML entities and the input passed to the parser at this stage
  19182. * of the compiler is the _decoded_ value, we need to compute the real offset using the original
  19183. * encoded values in the interpolated tokens. Note that this is only a special case handling for
  19184. * `MlParserTokenType.ENCODED_ENTITY` token types. All other interpolated tokens are expected to
  19185. * have parts which exactly match the input string for parsing the interpolation.
  19186. *
  19187. * @param interpolatedTokens The tokens for the interpolated value.
  19188. *
  19189. * @returns A map of index locations in the decoded template to indexes in the original template
  19190. */
  19191. function getIndexMapForOriginalTemplate(interpolatedTokens) {
  19192. let offsetMap = new Map();
  19193. let consumedInOriginalTemplate = 0;
  19194. let consumedInInput = 0;
  19195. let tokenIndex = 0;
  19196. while (tokenIndex < interpolatedTokens.length) {
  19197. const currentToken = interpolatedTokens[tokenIndex];
  19198. if (currentToken.type === 9 /* MlParserTokenType.ENCODED_ENTITY */) {
  19199. const [decoded, encoded] = currentToken.parts;
  19200. consumedInOriginalTemplate += encoded.length;
  19201. consumedInInput += decoded.length;
  19202. }
  19203. else {
  19204. const lengthOfParts = currentToken.parts.reduce((sum, current) => sum + current.length, 0);
  19205. consumedInInput += lengthOfParts;
  19206. consumedInOriginalTemplate += lengthOfParts;
  19207. }
  19208. offsetMap.set(consumedInInput, consumedInOriginalTemplate);
  19209. tokenIndex++;
  19210. }
  19211. return offsetMap;
  19212. }
  19213. /** Serializes the given AST into a normalized string format. */
  19214. function serialize(expression) {
  19215. return expression.visit(new SerializeExpressionVisitor());
  19216. }
  19217. class SerializeExpressionVisitor {
  19218. visitUnary(ast, context) {
  19219. return `${ast.operator}${ast.expr.visit(this, context)}`;
  19220. }
  19221. visitBinary(ast, context) {
  19222. return `${ast.left.visit(this, context)} ${ast.operation} ${ast.right.visit(this, context)}`;
  19223. }
  19224. visitChain(ast, context) {
  19225. return ast.expressions.map((e) => e.visit(this, context)).join('; ');
  19226. }
  19227. visitConditional(ast, context) {
  19228. return `${ast.condition.visit(this, context)} ? ${ast.trueExp.visit(this, context)} : ${ast.falseExp.visit(this, context)}`;
  19229. }
  19230. visitThisReceiver() {
  19231. return 'this';
  19232. }
  19233. visitImplicitReceiver() {
  19234. return '';
  19235. }
  19236. visitInterpolation(ast, context) {
  19237. return interleave(ast.strings, ast.expressions.map((e) => e.visit(this, context))).join('');
  19238. }
  19239. visitKeyedRead(ast, context) {
  19240. return `${ast.receiver.visit(this, context)}[${ast.key.visit(this, context)}]`;
  19241. }
  19242. visitKeyedWrite(ast, context) {
  19243. return `${ast.receiver.visit(this, context)}[${ast.key.visit(this, context)}] = ${ast.value.visit(this, context)}`;
  19244. }
  19245. visitLiteralArray(ast, context) {
  19246. return `[${ast.expressions.map((e) => e.visit(this, context)).join(', ')}]`;
  19247. }
  19248. visitLiteralMap(ast, context) {
  19249. return `{${zip(ast.keys.map((literal) => (literal.quoted ? `'${literal.key}'` : literal.key)), ast.values.map((value) => value.visit(this, context)))
  19250. .map(([key, value]) => `${key}: ${value}`)
  19251. .join(', ')}}`;
  19252. }
  19253. visitLiteralPrimitive(ast) {
  19254. if (ast.value === null)
  19255. return 'null';
  19256. switch (typeof ast.value) {
  19257. case 'number':
  19258. case 'boolean':
  19259. return ast.value.toString();
  19260. case 'undefined':
  19261. return 'undefined';
  19262. case 'string':
  19263. return `'${ast.value.replace(/'/g, `\\'`)}'`;
  19264. default:
  19265. throw new Error(`Unsupported primitive type: ${ast.value}`);
  19266. }
  19267. }
  19268. visitPipe(ast, context) {
  19269. return `${ast.exp.visit(this, context)} | ${ast.name}`;
  19270. }
  19271. visitPrefixNot(ast, context) {
  19272. return `!${ast.expression.visit(this, context)}`;
  19273. }
  19274. visitNonNullAssert(ast, context) {
  19275. return `${ast.expression.visit(this, context)}!`;
  19276. }
  19277. visitPropertyRead(ast, context) {
  19278. if (ast.receiver instanceof ImplicitReceiver) {
  19279. return ast.name;
  19280. }
  19281. else {
  19282. return `${ast.receiver.visit(this, context)}.${ast.name}`;
  19283. }
  19284. }
  19285. visitPropertyWrite(ast, context) {
  19286. if (ast.receiver instanceof ImplicitReceiver) {
  19287. return `${ast.name} = ${ast.value.visit(this, context)}`;
  19288. }
  19289. else {
  19290. return `${ast.receiver.visit(this, context)}.${ast.name} = ${ast.value.visit(this, context)}`;
  19291. }
  19292. }
  19293. visitSafePropertyRead(ast, context) {
  19294. return `${ast.receiver.visit(this, context)}?.${ast.name}`;
  19295. }
  19296. visitSafeKeyedRead(ast, context) {
  19297. return `${ast.receiver.visit(this, context)}?.[${ast.key.visit(this, context)}]`;
  19298. }
  19299. visitCall(ast, context) {
  19300. return `${ast.receiver.visit(this, context)}(${ast.args
  19301. .map((e) => e.visit(this, context))
  19302. .join(', ')})`;
  19303. }
  19304. visitSafeCall(ast, context) {
  19305. return `${ast.receiver.visit(this, context)}?.(${ast.args
  19306. .map((e) => e.visit(this, context))
  19307. .join(', ')})`;
  19308. }
  19309. visitTypeofExpression(ast, context) {
  19310. return `typeof ${ast.expression.visit(this, context)}`;
  19311. }
  19312. visitASTWithSource(ast, context) {
  19313. return ast.ast.visit(this, context);
  19314. }
  19315. visitTemplateLiteral(ast, context) {
  19316. let result = '';
  19317. for (let i = 0; i < ast.elements.length; i++) {
  19318. result += ast.elements[i].visit(this, context);
  19319. const expression = i < ast.expressions.length ? ast.expressions[i] : null;
  19320. if (expression !== null) {
  19321. result += '${' + expression.visit(this, context) + '}';
  19322. }
  19323. }
  19324. return '`' + result + '`';
  19325. }
  19326. visitTemplateLiteralElement(ast, context) {
  19327. return ast.text;
  19328. }
  19329. }
  19330. /** Zips the two input arrays into a single array of pairs of elements at the same index. */
  19331. function zip(left, right) {
  19332. if (left.length !== right.length)
  19333. throw new Error('Array lengths must match');
  19334. return left.map((l, i) => [l, right[i]]);
  19335. }
  19336. /**
  19337. * Interleaves the two arrays, starting with the first item on the left, then the first item
  19338. * on the right, second item from the left, and so on. When the first array's items are exhausted,
  19339. * the remaining items from the other array are included with no interleaving.
  19340. */
  19341. function interleave(left, right) {
  19342. const result = [];
  19343. for (let index = 0; index < Math.max(left.length, right.length); index++) {
  19344. if (index < left.length)
  19345. result.push(left[index]);
  19346. if (index < right.length)
  19347. result.push(right[index]);
  19348. }
  19349. return result;
  19350. }
  19351. // =================================================================================================
  19352. // =================================================================================================
  19353. // =========== S T O P - S T O P - S T O P - S T O P - S T O P - S T O P ===========
  19354. // =================================================================================================
  19355. // =================================================================================================
  19356. //
  19357. // DO NOT EDIT THIS LIST OF SECURITY SENSITIVE PROPERTIES WITHOUT A SECURITY REVIEW!
  19358. // Reach out to mprobst for details.
  19359. //
  19360. // =================================================================================================
  19361. /** Map from tagName|propertyName to SecurityContext. Properties applying to all tags use '*'. */
  19362. let _SECURITY_SCHEMA;
  19363. function SECURITY_SCHEMA() {
  19364. if (!_SECURITY_SCHEMA) {
  19365. _SECURITY_SCHEMA = {};
  19366. // Case is insignificant below, all element and attribute names are lower-cased for lookup.
  19367. registerContext(SecurityContext.HTML, ['iframe|srcdoc', '*|innerHTML', '*|outerHTML']);
  19368. registerContext(SecurityContext.STYLE, ['*|style']);
  19369. // NB: no SCRIPT contexts here, they are never allowed due to the parser stripping them.
  19370. registerContext(SecurityContext.URL, [
  19371. '*|formAction',
  19372. 'area|href',
  19373. 'area|ping',
  19374. 'audio|src',
  19375. 'a|href',
  19376. 'a|ping',
  19377. 'blockquote|cite',
  19378. 'body|background',
  19379. 'del|cite',
  19380. 'form|action',
  19381. 'img|src',
  19382. 'input|src',
  19383. 'ins|cite',
  19384. 'q|cite',
  19385. 'source|src',
  19386. 'track|src',
  19387. 'video|poster',
  19388. 'video|src',
  19389. ]);
  19390. registerContext(SecurityContext.RESOURCE_URL, [
  19391. 'applet|code',
  19392. 'applet|codebase',
  19393. 'base|href',
  19394. 'embed|src',
  19395. 'frame|src',
  19396. 'head|profile',
  19397. 'html|manifest',
  19398. 'iframe|src',
  19399. 'link|href',
  19400. 'media|src',
  19401. 'object|codebase',
  19402. 'object|data',
  19403. 'script|src',
  19404. ]);
  19405. }
  19406. return _SECURITY_SCHEMA;
  19407. }
  19408. function registerContext(ctx, specs) {
  19409. for (const spec of specs)
  19410. _SECURITY_SCHEMA[spec.toLowerCase()] = ctx;
  19411. }
  19412. /**
  19413. * The set of security-sensitive attributes of an `<iframe>` that *must* be
  19414. * applied as a static attribute only. This ensures that all security-sensitive
  19415. * attributes are taken into account while creating an instance of an `<iframe>`
  19416. * at runtime.
  19417. *
  19418. * Note: avoid using this set directly, use the `isIframeSecuritySensitiveAttr` function
  19419. * in the code instead.
  19420. */
  19421. const IFRAME_SECURITY_SENSITIVE_ATTRS = new Set([
  19422. 'sandbox',
  19423. 'allow',
  19424. 'allowfullscreen',
  19425. 'referrerpolicy',
  19426. 'csp',
  19427. 'fetchpriority',
  19428. ]);
  19429. /**
  19430. * Checks whether a given attribute name might represent a security-sensitive
  19431. * attribute of an <iframe>.
  19432. */
  19433. function isIframeSecuritySensitiveAttr(attrName) {
  19434. // The `setAttribute` DOM API is case-insensitive, so we lowercase the value
  19435. // before checking it against a known security-sensitive attributes.
  19436. return IFRAME_SECURITY_SENSITIVE_ATTRS.has(attrName.toLowerCase());
  19437. }
  19438. class ElementSchemaRegistry {
  19439. }
  19440. const BOOLEAN = 'boolean';
  19441. const NUMBER = 'number';
  19442. const STRING = 'string';
  19443. const OBJECT = 'object';
  19444. /**
  19445. * This array represents the DOM schema. It encodes inheritance, properties, and events.
  19446. *
  19447. * ## Overview
  19448. *
  19449. * Each line represents one kind of element. The `element_inheritance` and properties are joined
  19450. * using `element_inheritance|properties` syntax.
  19451. *
  19452. * ## Element Inheritance
  19453. *
  19454. * The `element_inheritance` can be further subdivided as `element1,element2,...^parentElement`.
  19455. * Here the individual elements are separated by `,` (commas). Every element in the list
  19456. * has identical properties.
  19457. *
  19458. * An `element` may inherit additional properties from `parentElement` If no `^parentElement` is
  19459. * specified then `""` (blank) element is assumed.
  19460. *
  19461. * NOTE: The blank element inherits from root `[Element]` element, the super element of all
  19462. * elements.
  19463. *
  19464. * NOTE an element prefix such as `:svg:` has no special meaning to the schema.
  19465. *
  19466. * ## Properties
  19467. *
  19468. * Each element has a set of properties separated by `,` (commas). Each property can be prefixed
  19469. * by a special character designating its type:
  19470. *
  19471. * - (no prefix): property is a string.
  19472. * - `*`: property represents an event.
  19473. * - `!`: property is a boolean.
  19474. * - `#`: property is a number.
  19475. * - `%`: property is an object.
  19476. *
  19477. * ## Query
  19478. *
  19479. * The class creates an internal squas representation which allows to easily answer the query of
  19480. * if a given property exist on a given element.
  19481. *
  19482. * NOTE: We don't yet support querying for types or events.
  19483. * NOTE: This schema is auto extracted from `schema_extractor.ts` located in the test folder,
  19484. * see dom_element_schema_registry_spec.ts
  19485. */
  19486. // =================================================================================================
  19487. // =================================================================================================
  19488. // =========== S T O P - S T O P - S T O P - S T O P - S T O P - S T O P ===========
  19489. // =================================================================================================
  19490. // =================================================================================================
  19491. //
  19492. // DO NOT EDIT THIS DOM SCHEMA WITHOUT A SECURITY REVIEW!
  19493. //
  19494. // Newly added properties must be security reviewed and assigned an appropriate SecurityContext in
  19495. // dom_security_schema.ts. Reach out to mprobst & rjamet for details.
  19496. //
  19497. // =================================================================================================
  19498. const SCHEMA = [
  19499. '[Element]|textContent,%ariaAtomic,%ariaAutoComplete,%ariaBusy,%ariaChecked,%ariaColCount,%ariaColIndex,%ariaColSpan,%ariaCurrent,%ariaDescription,%ariaDisabled,%ariaExpanded,%ariaHasPopup,%ariaHidden,%ariaKeyShortcuts,%ariaLabel,%ariaLevel,%ariaLive,%ariaModal,%ariaMultiLine,%ariaMultiSelectable,%ariaOrientation,%ariaPlaceholder,%ariaPosInSet,%ariaPressed,%ariaReadOnly,%ariaRelevant,%ariaRequired,%ariaRoleDescription,%ariaRowCount,%ariaRowIndex,%ariaRowSpan,%ariaSelected,%ariaSetSize,%ariaSort,%ariaValueMax,%ariaValueMin,%ariaValueNow,%ariaValueText,%classList,className,elementTiming,id,innerHTML,*beforecopy,*beforecut,*beforepaste,*fullscreenchange,*fullscreenerror,*search,*webkitfullscreenchange,*webkitfullscreenerror,outerHTML,%part,#scrollLeft,#scrollTop,slot' +
  19500. /* added manually to avoid breaking changes */
  19501. ',*message,*mozfullscreenchange,*mozfullscreenerror,*mozpointerlockchange,*mozpointerlockerror,*webglcontextcreationerror,*webglcontextlost,*webglcontextrestored',
  19502. '[HTMLElement]^[Element]|accessKey,autocapitalize,!autofocus,contentEditable,dir,!draggable,enterKeyHint,!hidden,!inert,innerText,inputMode,lang,nonce,*abort,*animationend,*animationiteration,*animationstart,*auxclick,*beforexrselect,*blur,*cancel,*canplay,*canplaythrough,*change,*click,*close,*contextmenu,*copy,*cuechange,*cut,*dblclick,*drag,*dragend,*dragenter,*dragleave,*dragover,*dragstart,*drop,*durationchange,*emptied,*ended,*error,*focus,*formdata,*gotpointercapture,*input,*invalid,*keydown,*keypress,*keyup,*load,*loadeddata,*loadedmetadata,*loadstart,*lostpointercapture,*mousedown,*mouseenter,*mouseleave,*mousemove,*mouseout,*mouseover,*mouseup,*mousewheel,*paste,*pause,*play,*playing,*pointercancel,*pointerdown,*pointerenter,*pointerleave,*pointermove,*pointerout,*pointerover,*pointerrawupdate,*pointerup,*progress,*ratechange,*reset,*resize,*scroll,*securitypolicyviolation,*seeked,*seeking,*select,*selectionchange,*selectstart,*slotchange,*stalled,*submit,*suspend,*timeupdate,*toggle,*transitioncancel,*transitionend,*transitionrun,*transitionstart,*volumechange,*waiting,*webkitanimationend,*webkitanimationiteration,*webkitanimationstart,*webkittransitionend,*wheel,outerText,!spellcheck,%style,#tabIndex,title,!translate,virtualKeyboardPolicy',
  19503. 'abbr,address,article,aside,b,bdi,bdo,cite,content,code,dd,dfn,dt,em,figcaption,figure,footer,header,hgroup,i,kbd,main,mark,nav,noscript,rb,rp,rt,rtc,ruby,s,samp,section,small,strong,sub,sup,u,var,wbr^[HTMLElement]|accessKey,autocapitalize,!autofocus,contentEditable,dir,!draggable,enterKeyHint,!hidden,innerText,inputMode,lang,nonce,*abort,*animationend,*animationiteration,*animationstart,*auxclick,*beforexrselect,*blur,*cancel,*canplay,*canplaythrough,*change,*click,*close,*contextmenu,*copy,*cuechange,*cut,*dblclick,*drag,*dragend,*dragenter,*dragleave,*dragover,*dragstart,*drop,*durationchange,*emptied,*ended,*error,*focus,*formdata,*gotpointercapture,*input,*invalid,*keydown,*keypress,*keyup,*load,*loadeddata,*loadedmetadata,*loadstart,*lostpointercapture,*mousedown,*mouseenter,*mouseleave,*mousemove,*mouseout,*mouseover,*mouseup,*mousewheel,*paste,*pause,*play,*playing,*pointercancel,*pointerdown,*pointerenter,*pointerleave,*pointermove,*pointerout,*pointerover,*pointerrawupdate,*pointerup,*progress,*ratechange,*reset,*resize,*scroll,*securitypolicyviolation,*seeked,*seeking,*select,*selectionchange,*selectstart,*slotchange,*stalled,*submit,*suspend,*timeupdate,*toggle,*transitioncancel,*transitionend,*transitionrun,*transitionstart,*volumechange,*waiting,*webkitanimationend,*webkitanimationiteration,*webkitanimationstart,*webkittransitionend,*wheel,outerText,!spellcheck,%style,#tabIndex,title,!translate,virtualKeyboardPolicy',
  19504. 'media^[HTMLElement]|!autoplay,!controls,%controlsList,%crossOrigin,#currentTime,!defaultMuted,#defaultPlaybackRate,!disableRemotePlayback,!loop,!muted,*encrypted,*waitingforkey,#playbackRate,preload,!preservesPitch,src,%srcObject,#volume',
  19505. ':svg:^[HTMLElement]|!autofocus,nonce,*abort,*animationend,*animationiteration,*animationstart,*auxclick,*beforexrselect,*blur,*cancel,*canplay,*canplaythrough,*change,*click,*close,*contextmenu,*copy,*cuechange,*cut,*dblclick,*drag,*dragend,*dragenter,*dragleave,*dragover,*dragstart,*drop,*durationchange,*emptied,*ended,*error,*focus,*formdata,*gotpointercapture,*input,*invalid,*keydown,*keypress,*keyup,*load,*loadeddata,*loadedmetadata,*loadstart,*lostpointercapture,*mousedown,*mouseenter,*mouseleave,*mousemove,*mouseout,*mouseover,*mouseup,*mousewheel,*paste,*pause,*play,*playing,*pointercancel,*pointerdown,*pointerenter,*pointerleave,*pointermove,*pointerout,*pointerover,*pointerrawupdate,*pointerup,*progress,*ratechange,*reset,*resize,*scroll,*securitypolicyviolation,*seeked,*seeking,*select,*selectionchange,*selectstart,*slotchange,*stalled,*submit,*suspend,*timeupdate,*toggle,*transitioncancel,*transitionend,*transitionrun,*transitionstart,*volumechange,*waiting,*webkitanimationend,*webkitanimationiteration,*webkitanimationstart,*webkittransitionend,*wheel,%style,#tabIndex',
  19506. ':svg:graphics^:svg:|',
  19507. ':svg:animation^:svg:|*begin,*end,*repeat',
  19508. ':svg:geometry^:svg:|',
  19509. ':svg:componentTransferFunction^:svg:|',
  19510. ':svg:gradient^:svg:|',
  19511. ':svg:textContent^:svg:graphics|',
  19512. ':svg:textPositioning^:svg:textContent|',
  19513. 'a^[HTMLElement]|charset,coords,download,hash,host,hostname,href,hreflang,name,password,pathname,ping,port,protocol,referrerPolicy,rel,%relList,rev,search,shape,target,text,type,username',
  19514. 'area^[HTMLElement]|alt,coords,download,hash,host,hostname,href,!noHref,password,pathname,ping,port,protocol,referrerPolicy,rel,%relList,search,shape,target,username',
  19515. 'audio^media|',
  19516. 'br^[HTMLElement]|clear',
  19517. 'base^[HTMLElement]|href,target',
  19518. 'body^[HTMLElement]|aLink,background,bgColor,link,*afterprint,*beforeprint,*beforeunload,*blur,*error,*focus,*hashchange,*languagechange,*load,*message,*messageerror,*offline,*online,*pagehide,*pageshow,*popstate,*rejectionhandled,*resize,*scroll,*storage,*unhandledrejection,*unload,text,vLink',
  19519. 'button^[HTMLElement]|!disabled,formAction,formEnctype,formMethod,!formNoValidate,formTarget,name,type,value',
  19520. 'canvas^[HTMLElement]|#height,#width',
  19521. 'content^[HTMLElement]|select',
  19522. 'dl^[HTMLElement]|!compact',
  19523. 'data^[HTMLElement]|value',
  19524. 'datalist^[HTMLElement]|',
  19525. 'details^[HTMLElement]|!open',
  19526. 'dialog^[HTMLElement]|!open,returnValue',
  19527. 'dir^[HTMLElement]|!compact',
  19528. 'div^[HTMLElement]|align',
  19529. 'embed^[HTMLElement]|align,height,name,src,type,width',
  19530. 'fieldset^[HTMLElement]|!disabled,name',
  19531. 'font^[HTMLElement]|color,face,size',
  19532. 'form^[HTMLElement]|acceptCharset,action,autocomplete,encoding,enctype,method,name,!noValidate,target',
  19533. 'frame^[HTMLElement]|frameBorder,longDesc,marginHeight,marginWidth,name,!noResize,scrolling,src',
  19534. 'frameset^[HTMLElement]|cols,*afterprint,*beforeprint,*beforeunload,*blur,*error,*focus,*hashchange,*languagechange,*load,*message,*messageerror,*offline,*online,*pagehide,*pageshow,*popstate,*rejectionhandled,*resize,*scroll,*storage,*unhandledrejection,*unload,rows',
  19535. 'hr^[HTMLElement]|align,color,!noShade,size,width',
  19536. 'head^[HTMLElement]|',
  19537. 'h1,h2,h3,h4,h5,h6^[HTMLElement]|align',
  19538. 'html^[HTMLElement]|version',
  19539. 'iframe^[HTMLElement]|align,allow,!allowFullscreen,!allowPaymentRequest,csp,frameBorder,height,loading,longDesc,marginHeight,marginWidth,name,referrerPolicy,%sandbox,scrolling,src,srcdoc,width',
  19540. 'img^[HTMLElement]|align,alt,border,%crossOrigin,decoding,#height,#hspace,!isMap,loading,longDesc,lowsrc,name,referrerPolicy,sizes,src,srcset,useMap,#vspace,#width',
  19541. 'input^[HTMLElement]|accept,align,alt,autocomplete,!checked,!defaultChecked,defaultValue,dirName,!disabled,%files,formAction,formEnctype,formMethod,!formNoValidate,formTarget,#height,!incremental,!indeterminate,max,#maxLength,min,#minLength,!multiple,name,pattern,placeholder,!readOnly,!required,selectionDirection,#selectionEnd,#selectionStart,#size,src,step,type,useMap,value,%valueAsDate,#valueAsNumber,#width',
  19542. 'li^[HTMLElement]|type,#value',
  19543. 'label^[HTMLElement]|htmlFor',
  19544. 'legend^[HTMLElement]|align',
  19545. 'link^[HTMLElement]|as,charset,%crossOrigin,!disabled,href,hreflang,imageSizes,imageSrcset,integrity,media,referrerPolicy,rel,%relList,rev,%sizes,target,type',
  19546. 'map^[HTMLElement]|name',
  19547. 'marquee^[HTMLElement]|behavior,bgColor,direction,height,#hspace,#loop,#scrollAmount,#scrollDelay,!trueSpeed,#vspace,width',
  19548. 'menu^[HTMLElement]|!compact',
  19549. 'meta^[HTMLElement]|content,httpEquiv,media,name,scheme',
  19550. 'meter^[HTMLElement]|#high,#low,#max,#min,#optimum,#value',
  19551. 'ins,del^[HTMLElement]|cite,dateTime',
  19552. 'ol^[HTMLElement]|!compact,!reversed,#start,type',
  19553. 'object^[HTMLElement]|align,archive,border,code,codeBase,codeType,data,!declare,height,#hspace,name,standby,type,useMap,#vspace,width',
  19554. 'optgroup^[HTMLElement]|!disabled,label',
  19555. 'option^[HTMLElement]|!defaultSelected,!disabled,label,!selected,text,value',
  19556. 'output^[HTMLElement]|defaultValue,%htmlFor,name,value',
  19557. 'p^[HTMLElement]|align',
  19558. 'param^[HTMLElement]|name,type,value,valueType',
  19559. 'picture^[HTMLElement]|',
  19560. 'pre^[HTMLElement]|#width',
  19561. 'progress^[HTMLElement]|#max,#value',
  19562. 'q,blockquote,cite^[HTMLElement]|',
  19563. 'script^[HTMLElement]|!async,charset,%crossOrigin,!defer,event,htmlFor,integrity,!noModule,%referrerPolicy,src,text,type',
  19564. 'select^[HTMLElement]|autocomplete,!disabled,#length,!multiple,name,!required,#selectedIndex,#size,value',
  19565. 'slot^[HTMLElement]|name',
  19566. 'source^[HTMLElement]|#height,media,sizes,src,srcset,type,#width',
  19567. 'span^[HTMLElement]|',
  19568. 'style^[HTMLElement]|!disabled,media,type',
  19569. 'caption^[HTMLElement]|align',
  19570. 'th,td^[HTMLElement]|abbr,align,axis,bgColor,ch,chOff,#colSpan,headers,height,!noWrap,#rowSpan,scope,vAlign,width',
  19571. 'col,colgroup^[HTMLElement]|align,ch,chOff,#span,vAlign,width',
  19572. 'table^[HTMLElement]|align,bgColor,border,%caption,cellPadding,cellSpacing,frame,rules,summary,%tFoot,%tHead,width',
  19573. 'tr^[HTMLElement]|align,bgColor,ch,chOff,vAlign',
  19574. 'tfoot,thead,tbody^[HTMLElement]|align,ch,chOff,vAlign',
  19575. 'template^[HTMLElement]|',
  19576. 'textarea^[HTMLElement]|autocomplete,#cols,defaultValue,dirName,!disabled,#maxLength,#minLength,name,placeholder,!readOnly,!required,#rows,selectionDirection,#selectionEnd,#selectionStart,value,wrap',
  19577. 'time^[HTMLElement]|dateTime',
  19578. 'title^[HTMLElement]|text',
  19579. 'track^[HTMLElement]|!default,kind,label,src,srclang',
  19580. 'ul^[HTMLElement]|!compact,type',
  19581. 'unknown^[HTMLElement]|',
  19582. 'video^media|!disablePictureInPicture,#height,*enterpictureinpicture,*leavepictureinpicture,!playsInline,poster,#width',
  19583. ':svg:a^:svg:graphics|',
  19584. ':svg:animate^:svg:animation|',
  19585. ':svg:animateMotion^:svg:animation|',
  19586. ':svg:animateTransform^:svg:animation|',
  19587. ':svg:circle^:svg:geometry|',
  19588. ':svg:clipPath^:svg:graphics|',
  19589. ':svg:defs^:svg:graphics|',
  19590. ':svg:desc^:svg:|',
  19591. ':svg:discard^:svg:|',
  19592. ':svg:ellipse^:svg:geometry|',
  19593. ':svg:feBlend^:svg:|',
  19594. ':svg:feColorMatrix^:svg:|',
  19595. ':svg:feComponentTransfer^:svg:|',
  19596. ':svg:feComposite^:svg:|',
  19597. ':svg:feConvolveMatrix^:svg:|',
  19598. ':svg:feDiffuseLighting^:svg:|',
  19599. ':svg:feDisplacementMap^:svg:|',
  19600. ':svg:feDistantLight^:svg:|',
  19601. ':svg:feDropShadow^:svg:|',
  19602. ':svg:feFlood^:svg:|',
  19603. ':svg:feFuncA^:svg:componentTransferFunction|',
  19604. ':svg:feFuncB^:svg:componentTransferFunction|',
  19605. ':svg:feFuncG^:svg:componentTransferFunction|',
  19606. ':svg:feFuncR^:svg:componentTransferFunction|',
  19607. ':svg:feGaussianBlur^:svg:|',
  19608. ':svg:feImage^:svg:|',
  19609. ':svg:feMerge^:svg:|',
  19610. ':svg:feMergeNode^:svg:|',
  19611. ':svg:feMorphology^:svg:|',
  19612. ':svg:feOffset^:svg:|',
  19613. ':svg:fePointLight^:svg:|',
  19614. ':svg:feSpecularLighting^:svg:|',
  19615. ':svg:feSpotLight^:svg:|',
  19616. ':svg:feTile^:svg:|',
  19617. ':svg:feTurbulence^:svg:|',
  19618. ':svg:filter^:svg:|',
  19619. ':svg:foreignObject^:svg:graphics|',
  19620. ':svg:g^:svg:graphics|',
  19621. ':svg:image^:svg:graphics|decoding',
  19622. ':svg:line^:svg:geometry|',
  19623. ':svg:linearGradient^:svg:gradient|',
  19624. ':svg:mpath^:svg:|',
  19625. ':svg:marker^:svg:|',
  19626. ':svg:mask^:svg:|',
  19627. ':svg:metadata^:svg:|',
  19628. ':svg:path^:svg:geometry|',
  19629. ':svg:pattern^:svg:|',
  19630. ':svg:polygon^:svg:geometry|',
  19631. ':svg:polyline^:svg:geometry|',
  19632. ':svg:radialGradient^:svg:gradient|',
  19633. ':svg:rect^:svg:geometry|',
  19634. ':svg:svg^:svg:graphics|#currentScale,#zoomAndPan',
  19635. ':svg:script^:svg:|type',
  19636. ':svg:set^:svg:animation|',
  19637. ':svg:stop^:svg:|',
  19638. ':svg:style^:svg:|!disabled,media,title,type',
  19639. ':svg:switch^:svg:graphics|',
  19640. ':svg:symbol^:svg:|',
  19641. ':svg:tspan^:svg:textPositioning|',
  19642. ':svg:text^:svg:textPositioning|',
  19643. ':svg:textPath^:svg:textContent|',
  19644. ':svg:title^:svg:|',
  19645. ':svg:use^:svg:graphics|',
  19646. ':svg:view^:svg:|#zoomAndPan',
  19647. 'data^[HTMLElement]|value',
  19648. 'keygen^[HTMLElement]|!autofocus,challenge,!disabled,form,keytype,name',
  19649. 'menuitem^[HTMLElement]|type,label,icon,!disabled,!checked,radiogroup,!default',
  19650. 'summary^[HTMLElement]|',
  19651. 'time^[HTMLElement]|dateTime',
  19652. ':svg:cursor^:svg:|',
  19653. ':math:^[HTMLElement]|!autofocus,nonce,*abort,*animationend,*animationiteration,*animationstart,*auxclick,*beforeinput,*beforematch,*beforetoggle,*beforexrselect,*blur,*cancel,*canplay,*canplaythrough,*change,*click,*close,*contentvisibilityautostatechange,*contextlost,*contextmenu,*contextrestored,*copy,*cuechange,*cut,*dblclick,*drag,*dragend,*dragenter,*dragleave,*dragover,*dragstart,*drop,*durationchange,*emptied,*ended,*error,*focus,*formdata,*gotpointercapture,*input,*invalid,*keydown,*keypress,*keyup,*load,*loadeddata,*loadedmetadata,*loadstart,*lostpointercapture,*mousedown,*mouseenter,*mouseleave,*mousemove,*mouseout,*mouseover,*mouseup,*mousewheel,*paste,*pause,*play,*playing,*pointercancel,*pointerdown,*pointerenter,*pointerleave,*pointermove,*pointerout,*pointerover,*pointerrawupdate,*pointerup,*progress,*ratechange,*reset,*resize,*scroll,*scrollend,*securitypolicyviolation,*seeked,*seeking,*select,*selectionchange,*selectstart,*slotchange,*stalled,*submit,*suspend,*timeupdate,*toggle,*transitioncancel,*transitionend,*transitionrun,*transitionstart,*volumechange,*waiting,*webkitanimationend,*webkitanimationiteration,*webkitanimationstart,*webkittransitionend,*wheel,%style,#tabIndex',
  19654. ':math:math^:math:|',
  19655. ':math:maction^:math:|',
  19656. ':math:menclose^:math:|',
  19657. ':math:merror^:math:|',
  19658. ':math:mfenced^:math:|',
  19659. ':math:mfrac^:math:|',
  19660. ':math:mi^:math:|',
  19661. ':math:mmultiscripts^:math:|',
  19662. ':math:mn^:math:|',
  19663. ':math:mo^:math:|',
  19664. ':math:mover^:math:|',
  19665. ':math:mpadded^:math:|',
  19666. ':math:mphantom^:math:|',
  19667. ':math:mroot^:math:|',
  19668. ':math:mrow^:math:|',
  19669. ':math:ms^:math:|',
  19670. ':math:mspace^:math:|',
  19671. ':math:msqrt^:math:|',
  19672. ':math:mstyle^:math:|',
  19673. ':math:msub^:math:|',
  19674. ':math:msubsup^:math:|',
  19675. ':math:msup^:math:|',
  19676. ':math:mtable^:math:|',
  19677. ':math:mtd^:math:|',
  19678. ':math:mtext^:math:|',
  19679. ':math:mtr^:math:|',
  19680. ':math:munder^:math:|',
  19681. ':math:munderover^:math:|',
  19682. ':math:semantics^:math:|',
  19683. ];
  19684. const _ATTR_TO_PROP = new Map(Object.entries({
  19685. 'class': 'className',
  19686. 'for': 'htmlFor',
  19687. 'formaction': 'formAction',
  19688. 'innerHtml': 'innerHTML',
  19689. 'readonly': 'readOnly',
  19690. 'tabindex': 'tabIndex',
  19691. }));
  19692. // Invert _ATTR_TO_PROP.
  19693. const _PROP_TO_ATTR = Array.from(_ATTR_TO_PROP).reduce((inverted, [propertyName, attributeName]) => {
  19694. inverted.set(propertyName, attributeName);
  19695. return inverted;
  19696. }, new Map());
  19697. class DomElementSchemaRegistry extends ElementSchemaRegistry {
  19698. _schema = new Map();
  19699. // We don't allow binding to events for security reasons. Allowing event bindings would almost
  19700. // certainly introduce bad XSS vulnerabilities. Instead, we store events in a separate schema.
  19701. _eventSchema = new Map();
  19702. constructor() {
  19703. super();
  19704. SCHEMA.forEach((encodedType) => {
  19705. const type = new Map();
  19706. const events = new Set();
  19707. const [strType, strProperties] = encodedType.split('|');
  19708. const properties = strProperties.split(',');
  19709. const [typeNames, superName] = strType.split('^');
  19710. typeNames.split(',').forEach((tag) => {
  19711. this._schema.set(tag.toLowerCase(), type);
  19712. this._eventSchema.set(tag.toLowerCase(), events);
  19713. });
  19714. const superType = superName && this._schema.get(superName.toLowerCase());
  19715. if (superType) {
  19716. for (const [prop, value] of superType) {
  19717. type.set(prop, value);
  19718. }
  19719. for (const superEvent of this._eventSchema.get(superName.toLowerCase())) {
  19720. events.add(superEvent);
  19721. }
  19722. }
  19723. properties.forEach((property) => {
  19724. if (property.length > 0) {
  19725. switch (property[0]) {
  19726. case '*':
  19727. events.add(property.substring(1));
  19728. break;
  19729. case '!':
  19730. type.set(property.substring(1), BOOLEAN);
  19731. break;
  19732. case '#':
  19733. type.set(property.substring(1), NUMBER);
  19734. break;
  19735. case '%':
  19736. type.set(property.substring(1), OBJECT);
  19737. break;
  19738. default:
  19739. type.set(property, STRING);
  19740. }
  19741. }
  19742. });
  19743. });
  19744. }
  19745. hasProperty(tagName, propName, schemaMetas) {
  19746. if (schemaMetas.some((schema) => schema.name === NO_ERRORS_SCHEMA.name)) {
  19747. return true;
  19748. }
  19749. if (tagName.indexOf('-') > -1) {
  19750. if (isNgContainer(tagName) || isNgContent(tagName)) {
  19751. return false;
  19752. }
  19753. if (schemaMetas.some((schema) => schema.name === CUSTOM_ELEMENTS_SCHEMA.name)) {
  19754. // Can't tell now as we don't know which properties a custom element will get
  19755. // once it is instantiated
  19756. return true;
  19757. }
  19758. }
  19759. const elementProperties = this._schema.get(tagName.toLowerCase()) || this._schema.get('unknown');
  19760. return elementProperties.has(propName);
  19761. }
  19762. hasElement(tagName, schemaMetas) {
  19763. if (schemaMetas.some((schema) => schema.name === NO_ERRORS_SCHEMA.name)) {
  19764. return true;
  19765. }
  19766. if (tagName.indexOf('-') > -1) {
  19767. if (isNgContainer(tagName) || isNgContent(tagName)) {
  19768. return true;
  19769. }
  19770. if (schemaMetas.some((schema) => schema.name === CUSTOM_ELEMENTS_SCHEMA.name)) {
  19771. // Allow any custom elements
  19772. return true;
  19773. }
  19774. }
  19775. return this._schema.has(tagName.toLowerCase());
  19776. }
  19777. /**
  19778. * securityContext returns the security context for the given property on the given DOM tag.
  19779. *
  19780. * Tag and property name are statically known and cannot change at runtime, i.e. it is not
  19781. * possible to bind a value into a changing attribute or tag name.
  19782. *
  19783. * The filtering is based on a list of allowed tags|attributes. All attributes in the schema
  19784. * above are assumed to have the 'NONE' security context, i.e. that they are safe inert
  19785. * string values. Only specific well known attack vectors are assigned their appropriate context.
  19786. */
  19787. securityContext(tagName, propName, isAttribute) {
  19788. if (isAttribute) {
  19789. // NB: For security purposes, use the mapped property name, not the attribute name.
  19790. propName = this.getMappedPropName(propName);
  19791. }
  19792. // Make sure comparisons are case insensitive, so that case differences between attribute and
  19793. // property names do not have a security impact.
  19794. tagName = tagName.toLowerCase();
  19795. propName = propName.toLowerCase();
  19796. let ctx = SECURITY_SCHEMA()[tagName + '|' + propName];
  19797. if (ctx) {
  19798. return ctx;
  19799. }
  19800. ctx = SECURITY_SCHEMA()['*|' + propName];
  19801. return ctx ? ctx : SecurityContext.NONE;
  19802. }
  19803. getMappedPropName(propName) {
  19804. return _ATTR_TO_PROP.get(propName) ?? propName;
  19805. }
  19806. getDefaultComponentElementName() {
  19807. return 'ng-component';
  19808. }
  19809. validateProperty(name) {
  19810. if (name.toLowerCase().startsWith('on')) {
  19811. const msg = `Binding to event property '${name}' is disallowed for security reasons, ` +
  19812. `please use (${name.slice(2)})=...` +
  19813. `\nIf '${name}' is a directive input, make sure the directive is imported by the` +
  19814. ` current module.`;
  19815. return { error: true, msg: msg };
  19816. }
  19817. else {
  19818. return { error: false };
  19819. }
  19820. }
  19821. validateAttribute(name) {
  19822. if (name.toLowerCase().startsWith('on')) {
  19823. const msg = `Binding to event attribute '${name}' is disallowed for security reasons, ` +
  19824. `please use (${name.slice(2)})=...`;
  19825. return { error: true, msg: msg };
  19826. }
  19827. else {
  19828. return { error: false };
  19829. }
  19830. }
  19831. allKnownElementNames() {
  19832. return Array.from(this._schema.keys());
  19833. }
  19834. allKnownAttributesOfElement(tagName) {
  19835. const elementProperties = this._schema.get(tagName.toLowerCase()) || this._schema.get('unknown');
  19836. // Convert properties to attributes.
  19837. return Array.from(elementProperties.keys()).map((prop) => _PROP_TO_ATTR.get(prop) ?? prop);
  19838. }
  19839. allKnownEventsOfElement(tagName) {
  19840. return Array.from(this._eventSchema.get(tagName.toLowerCase()) ?? []);
  19841. }
  19842. normalizeAnimationStyleProperty(propName) {
  19843. return dashCaseToCamelCase(propName);
  19844. }
  19845. normalizeAnimationStyleValue(camelCaseProp, userProvidedProp, val) {
  19846. let unit = '';
  19847. const strVal = val.toString().trim();
  19848. let errorMsg = null;
  19849. if (_isPixelDimensionStyle(camelCaseProp) && val !== 0 && val !== '0') {
  19850. if (typeof val === 'number') {
  19851. unit = 'px';
  19852. }
  19853. else {
  19854. const valAndSuffixMatch = val.match(/^[+-]?[\d\.]+([a-z]*)$/);
  19855. if (valAndSuffixMatch && valAndSuffixMatch[1].length == 0) {
  19856. errorMsg = `Please provide a CSS unit value for ${userProvidedProp}:${val}`;
  19857. }
  19858. }
  19859. }
  19860. return { error: errorMsg, value: strVal + unit };
  19861. }
  19862. }
  19863. function _isPixelDimensionStyle(prop) {
  19864. switch (prop) {
  19865. case 'width':
  19866. case 'height':
  19867. case 'minWidth':
  19868. case 'minHeight':
  19869. case 'maxWidth':
  19870. case 'maxHeight':
  19871. case 'left':
  19872. case 'top':
  19873. case 'bottom':
  19874. case 'right':
  19875. case 'fontSize':
  19876. case 'outlineWidth':
  19877. case 'outlineOffset':
  19878. case 'paddingTop':
  19879. case 'paddingLeft':
  19880. case 'paddingBottom':
  19881. case 'paddingRight':
  19882. case 'marginTop':
  19883. case 'marginLeft':
  19884. case 'marginBottom':
  19885. case 'marginRight':
  19886. case 'borderRadius':
  19887. case 'borderWidth':
  19888. case 'borderTopWidth':
  19889. case 'borderLeftWidth':
  19890. case 'borderRightWidth':
  19891. case 'borderBottomWidth':
  19892. case 'textIndent':
  19893. return true;
  19894. default:
  19895. return false;
  19896. }
  19897. }
  19898. class HtmlTagDefinition {
  19899. closedByChildren = {};
  19900. contentType;
  19901. closedByParent = false;
  19902. implicitNamespacePrefix;
  19903. isVoid;
  19904. ignoreFirstLf;
  19905. canSelfClose;
  19906. preventNamespaceInheritance;
  19907. constructor({ closedByChildren, implicitNamespacePrefix, contentType = TagContentType.PARSABLE_DATA, closedByParent = false, isVoid = false, ignoreFirstLf = false, preventNamespaceInheritance = false, canSelfClose = false, } = {}) {
  19908. if (closedByChildren && closedByChildren.length > 0) {
  19909. closedByChildren.forEach((tagName) => (this.closedByChildren[tagName] = true));
  19910. }
  19911. this.isVoid = isVoid;
  19912. this.closedByParent = closedByParent || isVoid;
  19913. this.implicitNamespacePrefix = implicitNamespacePrefix || null;
  19914. this.contentType = contentType;
  19915. this.ignoreFirstLf = ignoreFirstLf;
  19916. this.preventNamespaceInheritance = preventNamespaceInheritance;
  19917. this.canSelfClose = canSelfClose ?? isVoid;
  19918. }
  19919. isClosedByChild(name) {
  19920. return this.isVoid || name.toLowerCase() in this.closedByChildren;
  19921. }
  19922. getContentType(prefix) {
  19923. if (typeof this.contentType === 'object') {
  19924. const overrideType = prefix === undefined ? undefined : this.contentType[prefix];
  19925. return overrideType ?? this.contentType.default;
  19926. }
  19927. return this.contentType;
  19928. }
  19929. }
  19930. let DEFAULT_TAG_DEFINITION;
  19931. // see https://www.w3.org/TR/html51/syntax.html#optional-tags
  19932. // This implementation does not fully conform to the HTML5 spec.
  19933. let TAG_DEFINITIONS;
  19934. function getHtmlTagDefinition(tagName) {
  19935. if (!TAG_DEFINITIONS) {
  19936. DEFAULT_TAG_DEFINITION = new HtmlTagDefinition({ canSelfClose: true });
  19937. TAG_DEFINITIONS = Object.assign(Object.create(null), {
  19938. 'base': new HtmlTagDefinition({ isVoid: true }),
  19939. 'meta': new HtmlTagDefinition({ isVoid: true }),
  19940. 'area': new HtmlTagDefinition({ isVoid: true }),
  19941. 'embed': new HtmlTagDefinition({ isVoid: true }),
  19942. 'link': new HtmlTagDefinition({ isVoid: true }),
  19943. 'img': new HtmlTagDefinition({ isVoid: true }),
  19944. 'input': new HtmlTagDefinition({ isVoid: true }),
  19945. 'param': new HtmlTagDefinition({ isVoid: true }),
  19946. 'hr': new HtmlTagDefinition({ isVoid: true }),
  19947. 'br': new HtmlTagDefinition({ isVoid: true }),
  19948. 'source': new HtmlTagDefinition({ isVoid: true }),
  19949. 'track': new HtmlTagDefinition({ isVoid: true }),
  19950. 'wbr': new HtmlTagDefinition({ isVoid: true }),
  19951. 'p': new HtmlTagDefinition({
  19952. closedByChildren: [
  19953. 'address',
  19954. 'article',
  19955. 'aside',
  19956. 'blockquote',
  19957. 'div',
  19958. 'dl',
  19959. 'fieldset',
  19960. 'footer',
  19961. 'form',
  19962. 'h1',
  19963. 'h2',
  19964. 'h3',
  19965. 'h4',
  19966. 'h5',
  19967. 'h6',
  19968. 'header',
  19969. 'hgroup',
  19970. 'hr',
  19971. 'main',
  19972. 'nav',
  19973. 'ol',
  19974. 'p',
  19975. 'pre',
  19976. 'section',
  19977. 'table',
  19978. 'ul',
  19979. ],
  19980. closedByParent: true,
  19981. }),
  19982. 'thead': new HtmlTagDefinition({ closedByChildren: ['tbody', 'tfoot'] }),
  19983. 'tbody': new HtmlTagDefinition({ closedByChildren: ['tbody', 'tfoot'], closedByParent: true }),
  19984. 'tfoot': new HtmlTagDefinition({ closedByChildren: ['tbody'], closedByParent: true }),
  19985. 'tr': new HtmlTagDefinition({ closedByChildren: ['tr'], closedByParent: true }),
  19986. 'td': new HtmlTagDefinition({ closedByChildren: ['td', 'th'], closedByParent: true }),
  19987. 'th': new HtmlTagDefinition({ closedByChildren: ['td', 'th'], closedByParent: true }),
  19988. 'col': new HtmlTagDefinition({ isVoid: true }),
  19989. 'svg': new HtmlTagDefinition({ implicitNamespacePrefix: 'svg' }),
  19990. 'foreignObject': new HtmlTagDefinition({
  19991. // Usually the implicit namespace here would be redundant since it will be inherited from
  19992. // the parent `svg`, but we have to do it for `foreignObject`, because the way the parser
  19993. // works is that the parent node of an end tag is its own start tag which means that
  19994. // the `preventNamespaceInheritance` on `foreignObject` would have it default to the
  19995. // implicit namespace which is `html`, unless specified otherwise.
  19996. implicitNamespacePrefix: 'svg',
  19997. // We want to prevent children of foreignObject from inheriting its namespace, because
  19998. // the point of the element is to allow nodes from other namespaces to be inserted.
  19999. preventNamespaceInheritance: true,
  20000. }),
  20001. 'math': new HtmlTagDefinition({ implicitNamespacePrefix: 'math' }),
  20002. 'li': new HtmlTagDefinition({ closedByChildren: ['li'], closedByParent: true }),
  20003. 'dt': new HtmlTagDefinition({ closedByChildren: ['dt', 'dd'] }),
  20004. 'dd': new HtmlTagDefinition({ closedByChildren: ['dt', 'dd'], closedByParent: true }),
  20005. 'rb': new HtmlTagDefinition({
  20006. closedByChildren: ['rb', 'rt', 'rtc', 'rp'],
  20007. closedByParent: true,
  20008. }),
  20009. 'rt': new HtmlTagDefinition({
  20010. closedByChildren: ['rb', 'rt', 'rtc', 'rp'],
  20011. closedByParent: true,
  20012. }),
  20013. 'rtc': new HtmlTagDefinition({ closedByChildren: ['rb', 'rtc', 'rp'], closedByParent: true }),
  20014. 'rp': new HtmlTagDefinition({
  20015. closedByChildren: ['rb', 'rt', 'rtc', 'rp'],
  20016. closedByParent: true,
  20017. }),
  20018. 'optgroup': new HtmlTagDefinition({ closedByChildren: ['optgroup'], closedByParent: true }),
  20019. 'option': new HtmlTagDefinition({
  20020. closedByChildren: ['option', 'optgroup'],
  20021. closedByParent: true,
  20022. }),
  20023. 'pre': new HtmlTagDefinition({ ignoreFirstLf: true }),
  20024. 'listing': new HtmlTagDefinition({ ignoreFirstLf: true }),
  20025. 'style': new HtmlTagDefinition({ contentType: TagContentType.RAW_TEXT }),
  20026. 'script': new HtmlTagDefinition({ contentType: TagContentType.RAW_TEXT }),
  20027. 'title': new HtmlTagDefinition({
  20028. // The browser supports two separate `title` tags which have to use
  20029. // a different content type: `HTMLTitleElement` and `SVGTitleElement`
  20030. contentType: {
  20031. default: TagContentType.ESCAPABLE_RAW_TEXT,
  20032. svg: TagContentType.PARSABLE_DATA,
  20033. },
  20034. }),
  20035. 'textarea': new HtmlTagDefinition({
  20036. contentType: TagContentType.ESCAPABLE_RAW_TEXT,
  20037. ignoreFirstLf: true,
  20038. }),
  20039. });
  20040. new DomElementSchemaRegistry().allKnownElementNames().forEach((knownTagName) => {
  20041. if (!TAG_DEFINITIONS[knownTagName] && getNsPrefix(knownTagName) === null) {
  20042. TAG_DEFINITIONS[knownTagName] = new HtmlTagDefinition({ canSelfClose: false });
  20043. }
  20044. });
  20045. }
  20046. // We have to make both a case-sensitive and a case-insensitive lookup, because
  20047. // HTML tag names are case insensitive, whereas some SVG tags are case sensitive.
  20048. return (TAG_DEFINITIONS[tagName] ?? TAG_DEFINITIONS[tagName.toLowerCase()] ?? DEFAULT_TAG_DEFINITION);
  20049. }
  20050. const TAG_TO_PLACEHOLDER_NAMES = {
  20051. 'A': 'LINK',
  20052. 'B': 'BOLD_TEXT',
  20053. 'BR': 'LINE_BREAK',
  20054. 'EM': 'EMPHASISED_TEXT',
  20055. 'H1': 'HEADING_LEVEL1',
  20056. 'H2': 'HEADING_LEVEL2',
  20057. 'H3': 'HEADING_LEVEL3',
  20058. 'H4': 'HEADING_LEVEL4',
  20059. 'H5': 'HEADING_LEVEL5',
  20060. 'H6': 'HEADING_LEVEL6',
  20061. 'HR': 'HORIZONTAL_RULE',
  20062. 'I': 'ITALIC_TEXT',
  20063. 'LI': 'LIST_ITEM',
  20064. 'LINK': 'MEDIA_LINK',
  20065. 'OL': 'ORDERED_LIST',
  20066. 'P': 'PARAGRAPH',
  20067. 'Q': 'QUOTATION',
  20068. 'S': 'STRIKETHROUGH_TEXT',
  20069. 'SMALL': 'SMALL_TEXT',
  20070. 'SUB': 'SUBSTRIPT',
  20071. 'SUP': 'SUPERSCRIPT',
  20072. 'TBODY': 'TABLE_BODY',
  20073. 'TD': 'TABLE_CELL',
  20074. 'TFOOT': 'TABLE_FOOTER',
  20075. 'TH': 'TABLE_HEADER_CELL',
  20076. 'THEAD': 'TABLE_HEADER',
  20077. 'TR': 'TABLE_ROW',
  20078. 'TT': 'MONOSPACED_TEXT',
  20079. 'U': 'UNDERLINED_TEXT',
  20080. 'UL': 'UNORDERED_LIST',
  20081. };
  20082. /**
  20083. * Creates unique names for placeholder with different content.
  20084. *
  20085. * Returns the same placeholder name when the content is identical.
  20086. */
  20087. class PlaceholderRegistry {
  20088. // Count the occurrence of the base name top generate a unique name
  20089. _placeHolderNameCounts = {};
  20090. // Maps signature to placeholder names
  20091. _signatureToName = {};
  20092. getStartTagPlaceholderName(tag, attrs, isVoid) {
  20093. const signature = this._hashTag(tag, attrs, isVoid);
  20094. if (this._signatureToName[signature]) {
  20095. return this._signatureToName[signature];
  20096. }
  20097. const upperTag = tag.toUpperCase();
  20098. const baseName = TAG_TO_PLACEHOLDER_NAMES[upperTag] || `TAG_${upperTag}`;
  20099. const name = this._generateUniqueName(isVoid ? baseName : `START_${baseName}`);
  20100. this._signatureToName[signature] = name;
  20101. return name;
  20102. }
  20103. getCloseTagPlaceholderName(tag) {
  20104. const signature = this._hashClosingTag(tag);
  20105. if (this._signatureToName[signature]) {
  20106. return this._signatureToName[signature];
  20107. }
  20108. const upperTag = tag.toUpperCase();
  20109. const baseName = TAG_TO_PLACEHOLDER_NAMES[upperTag] || `TAG_${upperTag}`;
  20110. const name = this._generateUniqueName(`CLOSE_${baseName}`);
  20111. this._signatureToName[signature] = name;
  20112. return name;
  20113. }
  20114. getPlaceholderName(name, content) {
  20115. const upperName = name.toUpperCase();
  20116. const signature = `PH: ${upperName}=${content}`;
  20117. if (this._signatureToName[signature]) {
  20118. return this._signatureToName[signature];
  20119. }
  20120. const uniqueName = this._generateUniqueName(upperName);
  20121. this._signatureToName[signature] = uniqueName;
  20122. return uniqueName;
  20123. }
  20124. getUniquePlaceholder(name) {
  20125. return this._generateUniqueName(name.toUpperCase());
  20126. }
  20127. getStartBlockPlaceholderName(name, parameters) {
  20128. const signature = this._hashBlock(name, parameters);
  20129. if (this._signatureToName[signature]) {
  20130. return this._signatureToName[signature];
  20131. }
  20132. const placeholder = this._generateUniqueName(`START_BLOCK_${this._toSnakeCase(name)}`);
  20133. this._signatureToName[signature] = placeholder;
  20134. return placeholder;
  20135. }
  20136. getCloseBlockPlaceholderName(name) {
  20137. const signature = this._hashClosingBlock(name);
  20138. if (this._signatureToName[signature]) {
  20139. return this._signatureToName[signature];
  20140. }
  20141. const placeholder = this._generateUniqueName(`CLOSE_BLOCK_${this._toSnakeCase(name)}`);
  20142. this._signatureToName[signature] = placeholder;
  20143. return placeholder;
  20144. }
  20145. // Generate a hash for a tag - does not take attribute order into account
  20146. _hashTag(tag, attrs, isVoid) {
  20147. const start = `<${tag}`;
  20148. const strAttrs = Object.keys(attrs)
  20149. .sort()
  20150. .map((name) => ` ${name}=${attrs[name]}`)
  20151. .join('');
  20152. const end = isVoid ? '/>' : `></${tag}>`;
  20153. return start + strAttrs + end;
  20154. }
  20155. _hashClosingTag(tag) {
  20156. return this._hashTag(`/${tag}`, {}, false);
  20157. }
  20158. _hashBlock(name, parameters) {
  20159. const params = parameters.length === 0 ? '' : ` (${parameters.sort().join('; ')})`;
  20160. return `@${name}${params} {}`;
  20161. }
  20162. _hashClosingBlock(name) {
  20163. return this._hashBlock(`close_${name}`, []);
  20164. }
  20165. _toSnakeCase(name) {
  20166. return name.toUpperCase().replace(/[^A-Z0-9]/g, '_');
  20167. }
  20168. _generateUniqueName(base) {
  20169. const seen = this._placeHolderNameCounts.hasOwnProperty(base);
  20170. if (!seen) {
  20171. this._placeHolderNameCounts[base] = 1;
  20172. return base;
  20173. }
  20174. const id = this._placeHolderNameCounts[base];
  20175. this._placeHolderNameCounts[base] = id + 1;
  20176. return `${base}_${id}`;
  20177. }
  20178. }
  20179. const _expParser = new Parser(new Lexer());
  20180. /**
  20181. * Returns a function converting html nodes to an i18n Message given an interpolationConfig
  20182. */
  20183. function createI18nMessageFactory(interpolationConfig, containerBlocks, retainEmptyTokens, preserveExpressionWhitespace) {
  20184. const visitor = new _I18nVisitor(_expParser, interpolationConfig, containerBlocks, retainEmptyTokens, preserveExpressionWhitespace);
  20185. return (nodes, meaning, description, customId, visitNodeFn) => visitor.toI18nMessage(nodes, meaning, description, customId, visitNodeFn);
  20186. }
  20187. function noopVisitNodeFn(_html, i18n) {
  20188. return i18n;
  20189. }
  20190. class _I18nVisitor {
  20191. _expressionParser;
  20192. _interpolationConfig;
  20193. _containerBlocks;
  20194. _retainEmptyTokens;
  20195. _preserveExpressionWhitespace;
  20196. constructor(_expressionParser, _interpolationConfig, _containerBlocks, _retainEmptyTokens, _preserveExpressionWhitespace) {
  20197. this._expressionParser = _expressionParser;
  20198. this._interpolationConfig = _interpolationConfig;
  20199. this._containerBlocks = _containerBlocks;
  20200. this._retainEmptyTokens = _retainEmptyTokens;
  20201. this._preserveExpressionWhitespace = _preserveExpressionWhitespace;
  20202. }
  20203. toI18nMessage(nodes, meaning = '', description = '', customId = '', visitNodeFn) {
  20204. const context = {
  20205. isIcu: nodes.length == 1 && nodes[0] instanceof Expansion,
  20206. icuDepth: 0,
  20207. placeholderRegistry: new PlaceholderRegistry(),
  20208. placeholderToContent: {},
  20209. placeholderToMessage: {},
  20210. visitNodeFn: visitNodeFn || noopVisitNodeFn,
  20211. };
  20212. const i18nodes = visitAll(this, nodes, context);
  20213. return new Message(i18nodes, context.placeholderToContent, context.placeholderToMessage, meaning, description, customId);
  20214. }
  20215. visitElement(el, context) {
  20216. const children = visitAll(this, el.children, context);
  20217. const attrs = {};
  20218. el.attrs.forEach((attr) => {
  20219. // Do not visit the attributes, translatable ones are top-level ASTs
  20220. attrs[attr.name] = attr.value;
  20221. });
  20222. const isVoid = getHtmlTagDefinition(el.name).isVoid;
  20223. const startPhName = context.placeholderRegistry.getStartTagPlaceholderName(el.name, attrs, isVoid);
  20224. context.placeholderToContent[startPhName] = {
  20225. text: el.startSourceSpan.toString(),
  20226. sourceSpan: el.startSourceSpan,
  20227. };
  20228. let closePhName = '';
  20229. if (!isVoid) {
  20230. closePhName = context.placeholderRegistry.getCloseTagPlaceholderName(el.name);
  20231. context.placeholderToContent[closePhName] = {
  20232. text: `</${el.name}>`,
  20233. sourceSpan: el.endSourceSpan ?? el.sourceSpan,
  20234. };
  20235. }
  20236. const node = new TagPlaceholder(el.name, attrs, startPhName, closePhName, children, isVoid, el.sourceSpan, el.startSourceSpan, el.endSourceSpan);
  20237. return context.visitNodeFn(el, node);
  20238. }
  20239. visitAttribute(attribute, context) {
  20240. const node = attribute.valueTokens === undefined || attribute.valueTokens.length === 1
  20241. ? new Text$2(attribute.value, attribute.valueSpan || attribute.sourceSpan)
  20242. : this._visitTextWithInterpolation(attribute.valueTokens, attribute.valueSpan || attribute.sourceSpan, context, attribute.i18n);
  20243. return context.visitNodeFn(attribute, node);
  20244. }
  20245. visitText(text, context) {
  20246. const node = text.tokens.length === 1
  20247. ? new Text$2(text.value, text.sourceSpan)
  20248. : this._visitTextWithInterpolation(text.tokens, text.sourceSpan, context, text.i18n);
  20249. return context.visitNodeFn(text, node);
  20250. }
  20251. visitComment(comment, context) {
  20252. return null;
  20253. }
  20254. visitExpansion(icu, context) {
  20255. context.icuDepth++;
  20256. const i18nIcuCases = {};
  20257. const i18nIcu = new Icu(icu.switchValue, icu.type, i18nIcuCases, icu.sourceSpan);
  20258. icu.cases.forEach((caze) => {
  20259. i18nIcuCases[caze.value] = new Container(caze.expression.map((node) => node.visit(this, context)), caze.expSourceSpan);
  20260. });
  20261. context.icuDepth--;
  20262. if (context.isIcu || context.icuDepth > 0) {
  20263. // Returns an ICU node when:
  20264. // - the message (vs a part of the message) is an ICU message, or
  20265. // - the ICU message is nested.
  20266. const expPh = context.placeholderRegistry.getUniquePlaceholder(`VAR_${icu.type}`);
  20267. i18nIcu.expressionPlaceholder = expPh;
  20268. context.placeholderToContent[expPh] = {
  20269. text: icu.switchValue,
  20270. sourceSpan: icu.switchValueSourceSpan,
  20271. };
  20272. return context.visitNodeFn(icu, i18nIcu);
  20273. }
  20274. // Else returns a placeholder
  20275. // ICU placeholders should not be replaced with their original content but with the their
  20276. // translations.
  20277. // TODO(vicb): add a html.Node -> i18n.Message cache to avoid having to re-create the msg
  20278. const phName = context.placeholderRegistry.getPlaceholderName('ICU', icu.sourceSpan.toString());
  20279. context.placeholderToMessage[phName] = this.toI18nMessage([icu], '', '', '', undefined);
  20280. const node = new IcuPlaceholder(i18nIcu, phName, icu.sourceSpan);
  20281. return context.visitNodeFn(icu, node);
  20282. }
  20283. visitExpansionCase(_icuCase, _context) {
  20284. throw new Error('Unreachable code');
  20285. }
  20286. visitBlock(block, context) {
  20287. const children = visitAll(this, block.children, context);
  20288. if (this._containerBlocks.has(block.name)) {
  20289. return new Container(children, block.sourceSpan);
  20290. }
  20291. const parameters = block.parameters.map((param) => param.expression);
  20292. const startPhName = context.placeholderRegistry.getStartBlockPlaceholderName(block.name, parameters);
  20293. const closePhName = context.placeholderRegistry.getCloseBlockPlaceholderName(block.name);
  20294. context.placeholderToContent[startPhName] = {
  20295. text: block.startSourceSpan.toString(),
  20296. sourceSpan: block.startSourceSpan,
  20297. };
  20298. context.placeholderToContent[closePhName] = {
  20299. text: block.endSourceSpan ? block.endSourceSpan.toString() : '}',
  20300. sourceSpan: block.endSourceSpan ?? block.sourceSpan,
  20301. };
  20302. const node = new BlockPlaceholder(block.name, parameters, startPhName, closePhName, children, block.sourceSpan, block.startSourceSpan, block.endSourceSpan);
  20303. return context.visitNodeFn(block, node);
  20304. }
  20305. visitBlockParameter(_parameter, _context) {
  20306. throw new Error('Unreachable code');
  20307. }
  20308. visitLetDeclaration(decl, context) {
  20309. return null;
  20310. }
  20311. /**
  20312. * Convert, text and interpolated tokens up into text and placeholder pieces.
  20313. *
  20314. * @param tokens The text and interpolated tokens.
  20315. * @param sourceSpan The span of the whole of the `text` string.
  20316. * @param context The current context of the visitor, used to compute and store placeholders.
  20317. * @param previousI18n Any i18n metadata associated with this `text` from a previous pass.
  20318. */
  20319. _visitTextWithInterpolation(tokens, sourceSpan, context, previousI18n) {
  20320. // Return a sequence of `Text` and `Placeholder` nodes grouped in a `Container`.
  20321. const nodes = [];
  20322. // We will only create a container if there are actually interpolations,
  20323. // so this flag tracks that.
  20324. let hasInterpolation = false;
  20325. for (const token of tokens) {
  20326. switch (token.type) {
  20327. case 8 /* TokenType.INTERPOLATION */:
  20328. case 17 /* TokenType.ATTR_VALUE_INTERPOLATION */:
  20329. hasInterpolation = true;
  20330. const [startMarker, expression, endMarker] = token.parts;
  20331. const baseName = extractPlaceholderName(expression) || 'INTERPOLATION';
  20332. const phName = context.placeholderRegistry.getPlaceholderName(baseName, expression);
  20333. if (this._preserveExpressionWhitespace) {
  20334. context.placeholderToContent[phName] = {
  20335. text: token.parts.join(''),
  20336. sourceSpan: token.sourceSpan,
  20337. };
  20338. nodes.push(new Placeholder(expression, phName, token.sourceSpan));
  20339. }
  20340. else {
  20341. const normalized = this.normalizeExpression(token);
  20342. context.placeholderToContent[phName] = {
  20343. text: `${startMarker}${normalized}${endMarker}`,
  20344. sourceSpan: token.sourceSpan,
  20345. };
  20346. nodes.push(new Placeholder(normalized, phName, token.sourceSpan));
  20347. }
  20348. break;
  20349. default:
  20350. // Try to merge text tokens with previous tokens. We do this even for all tokens
  20351. // when `retainEmptyTokens == true` because whitespace tokens may have non-zero
  20352. // length, but will be trimmed by `WhitespaceVisitor` in one extraction pass and
  20353. // be considered "empty" there. Therefore a whitespace token with
  20354. // `retainEmptyTokens === true` should be treated like an empty token and either
  20355. // retained or merged into the previous node. Since extraction does two passes with
  20356. // different trimming behavior, the second pass needs to have identical node count
  20357. // to reuse source spans, so we need this check to get the same answer when both
  20358. // trimming and not trimming.
  20359. if (token.parts[0].length > 0 || this._retainEmptyTokens) {
  20360. // This token is text or an encoded entity.
  20361. // If it is following on from a previous text node then merge it into that node
  20362. // Otherwise, if it is following an interpolation, then add a new node.
  20363. const previous = nodes[nodes.length - 1];
  20364. if (previous instanceof Text$2) {
  20365. previous.value += token.parts[0];
  20366. previous.sourceSpan = new ParseSourceSpan(previous.sourceSpan.start, token.sourceSpan.end, previous.sourceSpan.fullStart, previous.sourceSpan.details);
  20367. }
  20368. else {
  20369. nodes.push(new Text$2(token.parts[0], token.sourceSpan));
  20370. }
  20371. }
  20372. else {
  20373. // Retain empty tokens to avoid breaking dropping entire nodes such that source
  20374. // spans should not be reusable across multiple parses of a template. We *should*
  20375. // do this all the time, however we need to maintain backwards compatibility
  20376. // with existing message IDs so we can't do it by default and should only enable
  20377. // this when removing significant whitespace.
  20378. if (this._retainEmptyTokens) {
  20379. nodes.push(new Text$2(token.parts[0], token.sourceSpan));
  20380. }
  20381. }
  20382. break;
  20383. }
  20384. }
  20385. if (hasInterpolation) {
  20386. // Whitespace removal may have invalidated the interpolation source-spans.
  20387. reusePreviousSourceSpans(nodes, previousI18n);
  20388. return new Container(nodes, sourceSpan);
  20389. }
  20390. else {
  20391. return nodes[0];
  20392. }
  20393. }
  20394. // Normalize expression whitespace by parsing and re-serializing it. This makes
  20395. // message IDs more durable to insignificant whitespace changes.
  20396. normalizeExpression(token) {
  20397. const expression = token.parts[1];
  20398. const expr = this._expressionParser.parseBinding(expression,
  20399. /* location */ token.sourceSpan.start.toString(),
  20400. /* absoluteOffset */ token.sourceSpan.start.offset, this._interpolationConfig);
  20401. return serialize(expr);
  20402. }
  20403. }
  20404. /**
  20405. * Re-use the source-spans from `previousI18n` metadata for the `nodes`.
  20406. *
  20407. * Whitespace removal can invalidate the source-spans of interpolation nodes, so we
  20408. * reuse the source-span stored from a previous pass before the whitespace was removed.
  20409. *
  20410. * @param nodes The `Text` and `Placeholder` nodes to be processed.
  20411. * @param previousI18n Any i18n metadata for these `nodes` stored from a previous pass.
  20412. */
  20413. function reusePreviousSourceSpans(nodes, previousI18n) {
  20414. if (previousI18n instanceof Message) {
  20415. // The `previousI18n` is an i18n `Message`, so we are processing an `Attribute` with i18n
  20416. // metadata. The `Message` should consist only of a single `Container` that contains the
  20417. // parts (`Text` and `Placeholder`) to process.
  20418. assertSingleContainerMessage(previousI18n);
  20419. previousI18n = previousI18n.nodes[0];
  20420. }
  20421. if (previousI18n instanceof Container) {
  20422. // The `previousI18n` is a `Container`, which means that this is a second i18n extraction pass
  20423. // after whitespace has been removed from the AST nodes.
  20424. assertEquivalentNodes(previousI18n.children, nodes);
  20425. // Reuse the source-spans from the first pass.
  20426. for (let i = 0; i < nodes.length; i++) {
  20427. nodes[i].sourceSpan = previousI18n.children[i].sourceSpan;
  20428. }
  20429. }
  20430. }
  20431. /**
  20432. * Asserts that the `message` contains exactly one `Container` node.
  20433. */
  20434. function assertSingleContainerMessage(message) {
  20435. const nodes = message.nodes;
  20436. if (nodes.length !== 1 || !(nodes[0] instanceof Container)) {
  20437. throw new Error('Unexpected previous i18n message - expected it to consist of only a single `Container` node.');
  20438. }
  20439. }
  20440. /**
  20441. * Asserts that the `previousNodes` and `node` collections have the same number of elements and
  20442. * corresponding elements have the same node type.
  20443. */
  20444. function assertEquivalentNodes(previousNodes, nodes) {
  20445. if (previousNodes.length !== nodes.length) {
  20446. throw new Error(`
  20447. The number of i18n message children changed between first and second pass.
  20448. First pass (${previousNodes.length} tokens):
  20449. ${previousNodes.map((node) => `"${node.sourceSpan.toString()}"`).join('\n')}
  20450. Second pass (${nodes.length} tokens):
  20451. ${nodes.map((node) => `"${node.sourceSpan.toString()}"`).join('\n')}
  20452. `.trim());
  20453. }
  20454. if (previousNodes.some((node, i) => nodes[i].constructor !== node.constructor)) {
  20455. throw new Error('The types of the i18n message children changed between first and second pass.');
  20456. }
  20457. }
  20458. const _CUSTOM_PH_EXP = /\/\/[\s\S]*i18n[\s\S]*\([\s\S]*ph[\s\S]*=[\s\S]*("|')([\s\S]*?)\1[\s\S]*\)/g;
  20459. function extractPlaceholderName(input) {
  20460. return input.split(_CUSTOM_PH_EXP)[2];
  20461. }
  20462. /**
  20463. * An i18n error.
  20464. */
  20465. class I18nError extends ParseError {
  20466. constructor(span, msg) {
  20467. super(span, msg);
  20468. }
  20469. }
  20470. /**
  20471. * Set of tagName|propertyName corresponding to Trusted Types sinks. Properties applying to all
  20472. * tags use '*'.
  20473. *
  20474. * Extracted from, and should be kept in sync with
  20475. * https://w3c.github.io/webappsec-trusted-types/dist/spec/#integrations
  20476. */
  20477. const TRUSTED_TYPES_SINKS = new Set([
  20478. // NOTE: All strings in this set *must* be lowercase!
  20479. // TrustedHTML
  20480. 'iframe|srcdoc',
  20481. '*|innerhtml',
  20482. '*|outerhtml',
  20483. // NB: no TrustedScript here, as the corresponding tags are stripped by the compiler.
  20484. // TrustedScriptURL
  20485. 'embed|src',
  20486. 'object|codebase',
  20487. 'object|data',
  20488. ]);
  20489. /**
  20490. * isTrustedTypesSink returns true if the given property on the given DOM tag is a Trusted Types
  20491. * sink. In that case, use `ElementSchemaRegistry.securityContext` to determine which particular
  20492. * Trusted Type is required for values passed to the sink:
  20493. * - SecurityContext.HTML corresponds to TrustedHTML
  20494. * - SecurityContext.RESOURCE_URL corresponds to TrustedScriptURL
  20495. */
  20496. function isTrustedTypesSink(tagName, propName) {
  20497. // Make sure comparisons are case insensitive, so that case differences between attribute and
  20498. // property names do not have a security impact.
  20499. tagName = tagName.toLowerCase();
  20500. propName = propName.toLowerCase();
  20501. return (TRUSTED_TYPES_SINKS.has(tagName + '|' + propName) || TRUSTED_TYPES_SINKS.has('*|' + propName));
  20502. }
  20503. const setI18nRefs = (originalNodeMap) => {
  20504. return (trimmedNode, i18nNode) => {
  20505. // We need to set i18n properties on the original, untrimmed AST nodes. The i18n nodes needs to
  20506. // use the trimmed content for message IDs to make messages more stable to whitespace changes.
  20507. // But we don't want to actually trim the content, so we can't use the trimmed HTML AST for
  20508. // general code gen. Instead we map the trimmed HTML AST back to the original AST and then
  20509. // attach the i18n nodes so we get trimmed i18n nodes on the original (untrimmed) HTML AST.
  20510. const originalNode = originalNodeMap.get(trimmedNode) ?? trimmedNode;
  20511. if (originalNode instanceof NodeWithI18n) {
  20512. if (i18nNode instanceof IcuPlaceholder && originalNode.i18n instanceof Message) {
  20513. // This html node represents an ICU but this is a second processing pass, and the legacy id
  20514. // was computed in the previous pass and stored in the `i18n` property as a message.
  20515. // We are about to wipe out that property so capture the previous message to be reused when
  20516. // generating the message for this ICU later. See `_generateI18nMessage()`.
  20517. i18nNode.previousMessage = originalNode.i18n;
  20518. }
  20519. originalNode.i18n = i18nNode;
  20520. }
  20521. return i18nNode;
  20522. };
  20523. };
  20524. /**
  20525. * This visitor walks over HTML parse tree and converts information stored in
  20526. * i18n-related attributes ("i18n" and "i18n-*") into i18n meta object that is
  20527. * stored with other element's and attribute's information.
  20528. */
  20529. class I18nMetaVisitor {
  20530. interpolationConfig;
  20531. keepI18nAttrs;
  20532. enableI18nLegacyMessageIdFormat;
  20533. containerBlocks;
  20534. preserveSignificantWhitespace;
  20535. retainEmptyTokens;
  20536. // whether visited nodes contain i18n information
  20537. hasI18nMeta = false;
  20538. _errors = [];
  20539. constructor(interpolationConfig = DEFAULT_INTERPOLATION_CONFIG, keepI18nAttrs = false, enableI18nLegacyMessageIdFormat = false, containerBlocks = DEFAULT_CONTAINER_BLOCKS, preserveSignificantWhitespace = true,
  20540. // When dropping significant whitespace we need to retain empty tokens or
  20541. // else we won't be able to reuse source spans because empty tokens would be
  20542. // removed and cause a mismatch. Unfortunately this still needs to be
  20543. // configurable and sometimes needs to be set independently in order to make
  20544. // sure the number of nodes don't change between parses, even when
  20545. // `preserveSignificantWhitespace` changes.
  20546. retainEmptyTokens = !preserveSignificantWhitespace) {
  20547. this.interpolationConfig = interpolationConfig;
  20548. this.keepI18nAttrs = keepI18nAttrs;
  20549. this.enableI18nLegacyMessageIdFormat = enableI18nLegacyMessageIdFormat;
  20550. this.containerBlocks = containerBlocks;
  20551. this.preserveSignificantWhitespace = preserveSignificantWhitespace;
  20552. this.retainEmptyTokens = retainEmptyTokens;
  20553. }
  20554. _generateI18nMessage(nodes, meta = '', visitNodeFn) {
  20555. const { meaning, description, customId } = this._parseMetadata(meta);
  20556. const createI18nMessage = createI18nMessageFactory(this.interpolationConfig, this.containerBlocks, this.retainEmptyTokens,
  20557. /* preserveExpressionWhitespace */ this.preserveSignificantWhitespace);
  20558. const message = createI18nMessage(nodes, meaning, description, customId, visitNodeFn);
  20559. this._setMessageId(message, meta);
  20560. this._setLegacyIds(message, meta);
  20561. return message;
  20562. }
  20563. visitAllWithErrors(nodes) {
  20564. const result = nodes.map((node) => node.visit(this, null));
  20565. return new ParseTreeResult(result, this._errors);
  20566. }
  20567. visitElement(element) {
  20568. let message = undefined;
  20569. if (hasI18nAttrs(element)) {
  20570. this.hasI18nMeta = true;
  20571. const attrs = [];
  20572. const attrsMeta = {};
  20573. for (const attr of element.attrs) {
  20574. if (attr.name === I18N_ATTR) {
  20575. // root 'i18n' node attribute
  20576. const i18n = element.i18n || attr.value;
  20577. // Generate a new AST with whitespace trimmed, but also generate a map
  20578. // to correlate each new node to its original so we can apply i18n
  20579. // information to the original node based on the trimmed content.
  20580. //
  20581. // `WhitespaceVisitor` removes *insignificant* whitespace as well as
  20582. // significant whitespace. Enabling this visitor should be conditional
  20583. // on `preserveWhitespace` rather than `preserveSignificantWhitespace`,
  20584. // however this would be a breaking change for existing behavior where
  20585. // `preserveWhitespace` was not respected correctly when generating
  20586. // message IDs. This is really a bug but one we need to keep to maintain
  20587. // backwards compatibility.
  20588. const originalNodeMap = new Map();
  20589. const trimmedNodes = this.preserveSignificantWhitespace
  20590. ? element.children
  20591. : visitAllWithSiblings(new WhitespaceVisitor(false /* preserveSignificantWhitespace */, originalNodeMap), element.children);
  20592. message = this._generateI18nMessage(trimmedNodes, i18n, setI18nRefs(originalNodeMap));
  20593. if (message.nodes.length === 0) {
  20594. // Ignore the message if it is empty.
  20595. message = undefined;
  20596. }
  20597. // Store the message on the element
  20598. element.i18n = message;
  20599. }
  20600. else if (attr.name.startsWith(I18N_ATTR_PREFIX)) {
  20601. // 'i18n-*' attributes
  20602. const name = attr.name.slice(I18N_ATTR_PREFIX.length);
  20603. if (isTrustedTypesSink(element.name, name)) {
  20604. this._reportError(attr, `Translating attribute '${name}' is disallowed for security reasons.`);
  20605. }
  20606. else {
  20607. attrsMeta[name] = attr.value;
  20608. }
  20609. }
  20610. else {
  20611. // non-i18n attributes
  20612. attrs.push(attr);
  20613. }
  20614. }
  20615. // set i18n meta for attributes
  20616. if (Object.keys(attrsMeta).length) {
  20617. for (const attr of attrs) {
  20618. const meta = attrsMeta[attr.name];
  20619. // do not create translation for empty attributes
  20620. if (meta !== undefined && attr.value) {
  20621. attr.i18n = this._generateI18nMessage([attr], attr.i18n || meta);
  20622. }
  20623. }
  20624. }
  20625. if (!this.keepI18nAttrs) {
  20626. // update element's attributes,
  20627. // keeping only non-i18n related ones
  20628. element.attrs = attrs;
  20629. }
  20630. }
  20631. visitAll(this, element.children, message);
  20632. return element;
  20633. }
  20634. visitExpansion(expansion, currentMessage) {
  20635. let message;
  20636. const meta = expansion.i18n;
  20637. this.hasI18nMeta = true;
  20638. if (meta instanceof IcuPlaceholder) {
  20639. // set ICU placeholder name (e.g. "ICU_1"),
  20640. // generated while processing root element contents,
  20641. // so we can reference it when we output translation
  20642. const name = meta.name;
  20643. message = this._generateI18nMessage([expansion], meta);
  20644. const icu = icuFromI18nMessage(message);
  20645. icu.name = name;
  20646. if (currentMessage !== null) {
  20647. // Also update the placeholderToMessage map with this new message
  20648. currentMessage.placeholderToMessage[name] = message;
  20649. }
  20650. }
  20651. else {
  20652. // ICU is a top level message, try to use metadata from container element if provided via
  20653. // `context` argument. Note: context may not be available for standalone ICUs (without
  20654. // wrapping element), so fallback to ICU metadata in this case.
  20655. message = this._generateI18nMessage([expansion], currentMessage || meta);
  20656. }
  20657. expansion.i18n = message;
  20658. return expansion;
  20659. }
  20660. visitText(text) {
  20661. return text;
  20662. }
  20663. visitAttribute(attribute) {
  20664. return attribute;
  20665. }
  20666. visitComment(comment) {
  20667. return comment;
  20668. }
  20669. visitExpansionCase(expansionCase) {
  20670. return expansionCase;
  20671. }
  20672. visitBlock(block, context) {
  20673. visitAll(this, block.children, context);
  20674. return block;
  20675. }
  20676. visitBlockParameter(parameter, context) {
  20677. return parameter;
  20678. }
  20679. visitLetDeclaration(decl, context) {
  20680. return decl;
  20681. }
  20682. /**
  20683. * Parse the general form `meta` passed into extract the explicit metadata needed to create a
  20684. * `Message`.
  20685. *
  20686. * There are three possibilities for the `meta` variable
  20687. * 1) a string from an `i18n` template attribute: parse it to extract the metadata values.
  20688. * 2) a `Message` from a previous processing pass: reuse the metadata values in the message.
  20689. * 4) other: ignore this and just process the message metadata as normal
  20690. *
  20691. * @param meta the bucket that holds information about the message
  20692. * @returns the parsed metadata.
  20693. */
  20694. _parseMetadata(meta) {
  20695. return typeof meta === 'string'
  20696. ? parseI18nMeta(meta)
  20697. : meta instanceof Message
  20698. ? meta
  20699. : {};
  20700. }
  20701. /**
  20702. * Generate (or restore) message id if not specified already.
  20703. */
  20704. _setMessageId(message, meta) {
  20705. if (!message.id) {
  20706. message.id = (meta instanceof Message && meta.id) || decimalDigest(message);
  20707. }
  20708. }
  20709. /**
  20710. * Update the `message` with a `legacyId` if necessary.
  20711. *
  20712. * @param message the message whose legacy id should be set
  20713. * @param meta information about the message being processed
  20714. */
  20715. _setLegacyIds(message, meta) {
  20716. if (this.enableI18nLegacyMessageIdFormat) {
  20717. message.legacyIds = [computeDigest(message), computeDecimalDigest(message)];
  20718. }
  20719. else if (typeof meta !== 'string') {
  20720. // This occurs if we are doing the 2nd pass after whitespace removal (see `parseTemplate()` in
  20721. // `packages/compiler/src/render3/view/template.ts`).
  20722. // In that case we want to reuse the legacy message generated in the 1st pass (see
  20723. // `setI18nRefs()`).
  20724. const previousMessage = meta instanceof Message
  20725. ? meta
  20726. : meta instanceof IcuPlaceholder
  20727. ? meta.previousMessage
  20728. : undefined;
  20729. message.legacyIds = previousMessage ? previousMessage.legacyIds : [];
  20730. }
  20731. }
  20732. _reportError(node, msg) {
  20733. this._errors.push(new I18nError(node.sourceSpan, msg));
  20734. }
  20735. }
  20736. /** I18n separators for metadata **/
  20737. const I18N_MEANING_SEPARATOR = '|';
  20738. const I18N_ID_SEPARATOR = '@@';
  20739. /**
  20740. * Parses i18n metas like:
  20741. * - "@@id",
  20742. * - "description[@@id]",
  20743. * - "meaning|description[@@id]"
  20744. * and returns an object with parsed output.
  20745. *
  20746. * @param meta String that represents i18n meta
  20747. * @returns Object with id, meaning and description fields
  20748. */
  20749. function parseI18nMeta(meta = '') {
  20750. let customId;
  20751. let meaning;
  20752. let description;
  20753. meta = meta.trim();
  20754. if (meta) {
  20755. const idIndex = meta.indexOf(I18N_ID_SEPARATOR);
  20756. const descIndex = meta.indexOf(I18N_MEANING_SEPARATOR);
  20757. let meaningAndDesc;
  20758. [meaningAndDesc, customId] =
  20759. idIndex > -1 ? [meta.slice(0, idIndex), meta.slice(idIndex + 2)] : [meta, ''];
  20760. [meaning, description] =
  20761. descIndex > -1
  20762. ? [meaningAndDesc.slice(0, descIndex), meaningAndDesc.slice(descIndex + 1)]
  20763. : ['', meaningAndDesc];
  20764. }
  20765. return { customId, meaning, description };
  20766. }
  20767. // Converts i18n meta information for a message (id, description, meaning)
  20768. // to a JsDoc statement formatted as expected by the Closure compiler.
  20769. function i18nMetaToJSDoc(meta) {
  20770. const tags = [];
  20771. if (meta.description) {
  20772. tags.push({ tagName: "desc" /* o.JSDocTagName.Desc */, text: meta.description });
  20773. }
  20774. else {
  20775. // Suppress the JSCompiler warning that a `@desc` was not given for this message.
  20776. tags.push({ tagName: "suppress" /* o.JSDocTagName.Suppress */, text: '{msgDescriptions}' });
  20777. }
  20778. if (meta.meaning) {
  20779. tags.push({ tagName: "meaning" /* o.JSDocTagName.Meaning */, text: meta.meaning });
  20780. }
  20781. return jsDocComment(tags);
  20782. }
  20783. /** Closure uses `goog.getMsg(message)` to lookup translations */
  20784. const GOOG_GET_MSG = 'goog.getMsg';
  20785. /**
  20786. * Generates a `goog.getMsg()` statement and reassignment. The template:
  20787. *
  20788. * ```html
  20789. * <div i18n>Sent from {{ sender }} to <span class="receiver">{{ receiver }}</span></div>
  20790. * ```
  20791. *
  20792. * Generates:
  20793. *
  20794. * ```ts
  20795. * const MSG_FOO = goog.getMsg(
  20796. * // Message template.
  20797. * 'Sent from {$interpolation} to {$startTagSpan}{$interpolation_1}{$closeTagSpan}.',
  20798. * // Placeholder values, set to magic strings which get replaced by the Angular runtime.
  20799. * {
  20800. * 'interpolation': '\uFFFD0\uFFFD',
  20801. * 'startTagSpan': '\uFFFD1\uFFFD',
  20802. * 'interpolation_1': '\uFFFD2\uFFFD',
  20803. * 'closeTagSpan': '\uFFFD3\uFFFD',
  20804. * },
  20805. * // Options bag.
  20806. * {
  20807. * // Maps each placeholder to the original Angular source code which generates it's value.
  20808. * original_code: {
  20809. * 'interpolation': '{{ sender }}',
  20810. * 'startTagSpan': '<span class="receiver">',
  20811. * 'interpolation_1': '{{ receiver }}',
  20812. * 'closeTagSpan': '</span>',
  20813. * },
  20814. * },
  20815. * );
  20816. * const I18N_0 = MSG_FOO;
  20817. * ```
  20818. */
  20819. function createGoogleGetMsgStatements(variable$1, message, closureVar, placeholderValues) {
  20820. const messageString = serializeI18nMessageForGetMsg(message);
  20821. const args = [literal(messageString)];
  20822. if (Object.keys(placeholderValues).length) {
  20823. // Message template parameters containing the magic strings replaced by the Angular runtime with
  20824. // real data, e.g. `{'interpolation': '\uFFFD0\uFFFD'}`.
  20825. args.push(mapLiteral(formatI18nPlaceholderNamesInMap(placeholderValues, true /* useCamelCase */), true /* quoted */));
  20826. // Message options object, which contains original source code for placeholders (as they are
  20827. // present in a template, e.g.
  20828. // `{original_code: {'interpolation': '{{ name }}', 'startTagSpan': '<span>'}}`.
  20829. args.push(mapLiteral({
  20830. original_code: literalMap(Object.keys(placeholderValues).map((param) => ({
  20831. key: formatI18nPlaceholderName(param),
  20832. quoted: true,
  20833. value: message.placeholders[param]
  20834. ? // Get source span for typical placeholder if it exists.
  20835. literal(message.placeholders[param].sourceSpan.toString())
  20836. : // Otherwise must be an ICU expression, get it's source span.
  20837. literal(message.placeholderToMessage[param].nodes
  20838. .map((node) => node.sourceSpan.toString())
  20839. .join('')),
  20840. }))),
  20841. }));
  20842. }
  20843. // /**
  20844. // * @desc description of message
  20845. // * @meaning meaning of message
  20846. // */
  20847. // const MSG_... = goog.getMsg(..);
  20848. // I18N_X = MSG_...;
  20849. const googGetMsgStmt = closureVar.set(variable(GOOG_GET_MSG).callFn(args)).toConstDecl();
  20850. googGetMsgStmt.addLeadingComment(i18nMetaToJSDoc(message));
  20851. const i18nAssignmentStmt = new ExpressionStatement(variable$1.set(closureVar));
  20852. return [googGetMsgStmt, i18nAssignmentStmt];
  20853. }
  20854. /**
  20855. * This visitor walks over i18n tree and generates its string representation, including ICUs and
  20856. * placeholders in `{$placeholder}` (for plain messages) or `{PLACEHOLDER}` (inside ICUs) format.
  20857. */
  20858. class GetMsgSerializerVisitor {
  20859. formatPh(value) {
  20860. return `{$${formatI18nPlaceholderName(value)}}`;
  20861. }
  20862. visitText(text) {
  20863. return text.value;
  20864. }
  20865. visitContainer(container) {
  20866. return container.children.map((child) => child.visit(this)).join('');
  20867. }
  20868. visitIcu(icu) {
  20869. return serializeIcuNode(icu);
  20870. }
  20871. visitTagPlaceholder(ph) {
  20872. return ph.isVoid
  20873. ? this.formatPh(ph.startName)
  20874. : `${this.formatPh(ph.startName)}${ph.children
  20875. .map((child) => child.visit(this))
  20876. .join('')}${this.formatPh(ph.closeName)}`;
  20877. }
  20878. visitPlaceholder(ph) {
  20879. return this.formatPh(ph.name);
  20880. }
  20881. visitBlockPlaceholder(ph) {
  20882. return `${this.formatPh(ph.startName)}${ph.children
  20883. .map((child) => child.visit(this))
  20884. .join('')}${this.formatPh(ph.closeName)}`;
  20885. }
  20886. visitIcuPlaceholder(ph, context) {
  20887. return this.formatPh(ph.name);
  20888. }
  20889. }
  20890. const serializerVisitor = new GetMsgSerializerVisitor();
  20891. function serializeI18nMessageForGetMsg(message) {
  20892. return message.nodes.map((node) => node.visit(serializerVisitor, null)).join('');
  20893. }
  20894. function createLocalizeStatements(variable, message, params) {
  20895. const { messageParts, placeHolders } = serializeI18nMessageForLocalize(message);
  20896. const sourceSpan = getSourceSpan(message);
  20897. const expressions = placeHolders.map((ph) => params[ph.text]);
  20898. const localizedString$1 = localizedString(message, messageParts, placeHolders, expressions, sourceSpan);
  20899. const variableInitialization = variable.set(localizedString$1);
  20900. return [new ExpressionStatement(variableInitialization)];
  20901. }
  20902. /**
  20903. * This visitor walks over an i18n tree, capturing literal strings and placeholders.
  20904. *
  20905. * The result can be used for generating the `$localize` tagged template literals.
  20906. */
  20907. class LocalizeSerializerVisitor {
  20908. placeholderToMessage;
  20909. pieces;
  20910. constructor(placeholderToMessage, pieces) {
  20911. this.placeholderToMessage = placeholderToMessage;
  20912. this.pieces = pieces;
  20913. }
  20914. visitText(text) {
  20915. if (this.pieces[this.pieces.length - 1] instanceof LiteralPiece) {
  20916. // Two literal pieces in a row means that there was some comment node in-between.
  20917. this.pieces[this.pieces.length - 1].text += text.value;
  20918. }
  20919. else {
  20920. const sourceSpan = new ParseSourceSpan(text.sourceSpan.fullStart, text.sourceSpan.end, text.sourceSpan.fullStart, text.sourceSpan.details);
  20921. this.pieces.push(new LiteralPiece(text.value, sourceSpan));
  20922. }
  20923. }
  20924. visitContainer(container) {
  20925. container.children.forEach((child) => child.visit(this));
  20926. }
  20927. visitIcu(icu) {
  20928. this.pieces.push(new LiteralPiece(serializeIcuNode(icu), icu.sourceSpan));
  20929. }
  20930. visitTagPlaceholder(ph) {
  20931. this.pieces.push(this.createPlaceholderPiece(ph.startName, ph.startSourceSpan ?? ph.sourceSpan));
  20932. if (!ph.isVoid) {
  20933. ph.children.forEach((child) => child.visit(this));
  20934. this.pieces.push(this.createPlaceholderPiece(ph.closeName, ph.endSourceSpan ?? ph.sourceSpan));
  20935. }
  20936. }
  20937. visitPlaceholder(ph) {
  20938. this.pieces.push(this.createPlaceholderPiece(ph.name, ph.sourceSpan));
  20939. }
  20940. visitBlockPlaceholder(ph) {
  20941. this.pieces.push(this.createPlaceholderPiece(ph.startName, ph.startSourceSpan ?? ph.sourceSpan));
  20942. ph.children.forEach((child) => child.visit(this));
  20943. this.pieces.push(this.createPlaceholderPiece(ph.closeName, ph.endSourceSpan ?? ph.sourceSpan));
  20944. }
  20945. visitIcuPlaceholder(ph) {
  20946. this.pieces.push(this.createPlaceholderPiece(ph.name, ph.sourceSpan, this.placeholderToMessage[ph.name]));
  20947. }
  20948. createPlaceholderPiece(name, sourceSpan, associatedMessage) {
  20949. return new PlaceholderPiece(formatI18nPlaceholderName(name, /* useCamelCase */ false), sourceSpan, associatedMessage);
  20950. }
  20951. }
  20952. /**
  20953. * Serialize an i18n message into two arrays: messageParts and placeholders.
  20954. *
  20955. * These arrays will be used to generate `$localize` tagged template literals.
  20956. *
  20957. * @param message The message to be serialized.
  20958. * @returns an object containing the messageParts and placeholders.
  20959. */
  20960. function serializeI18nMessageForLocalize(message) {
  20961. const pieces = [];
  20962. const serializerVisitor = new LocalizeSerializerVisitor(message.placeholderToMessage, pieces);
  20963. message.nodes.forEach((node) => node.visit(serializerVisitor));
  20964. return processMessagePieces(pieces);
  20965. }
  20966. function getSourceSpan(message) {
  20967. const startNode = message.nodes[0];
  20968. const endNode = message.nodes[message.nodes.length - 1];
  20969. return new ParseSourceSpan(startNode.sourceSpan.fullStart, endNode.sourceSpan.end, startNode.sourceSpan.fullStart, startNode.sourceSpan.details);
  20970. }
  20971. /**
  20972. * Convert the list of serialized MessagePieces into two arrays.
  20973. *
  20974. * One contains the literal string pieces and the other the placeholders that will be replaced by
  20975. * expressions when rendering `$localize` tagged template literals.
  20976. *
  20977. * @param pieces The pieces to process.
  20978. * @returns an object containing the messageParts and placeholders.
  20979. */
  20980. function processMessagePieces(pieces) {
  20981. const messageParts = [];
  20982. const placeHolders = [];
  20983. if (pieces[0] instanceof PlaceholderPiece) {
  20984. // The first piece was a placeholder so we need to add an initial empty message part.
  20985. messageParts.push(createEmptyMessagePart(pieces[0].sourceSpan.start));
  20986. }
  20987. for (let i = 0; i < pieces.length; i++) {
  20988. const part = pieces[i];
  20989. if (part instanceof LiteralPiece) {
  20990. messageParts.push(part);
  20991. }
  20992. else {
  20993. placeHolders.push(part);
  20994. if (pieces[i - 1] instanceof PlaceholderPiece) {
  20995. // There were two placeholders in a row, so we need to add an empty message part.
  20996. messageParts.push(createEmptyMessagePart(pieces[i - 1].sourceSpan.end));
  20997. }
  20998. }
  20999. }
  21000. if (pieces[pieces.length - 1] instanceof PlaceholderPiece) {
  21001. // The last piece was a placeholder so we need to add a final empty message part.
  21002. messageParts.push(createEmptyMessagePart(pieces[pieces.length - 1].sourceSpan.end));
  21003. }
  21004. return { messageParts, placeHolders };
  21005. }
  21006. function createEmptyMessagePart(location) {
  21007. return new LiteralPiece('', new ParseSourceSpan(location, location));
  21008. }
  21009. /** Name of the global variable that is used to determine if we use Closure translations or not */
  21010. const NG_I18N_CLOSURE_MODE = 'ngI18nClosureMode';
  21011. /**
  21012. * Prefix for non-`goog.getMsg` i18n-related vars.
  21013. * Note: the prefix uses lowercase characters intentionally due to a Closure behavior that
  21014. * considers variables like `I18N_0` as constants and throws an error when their value changes.
  21015. */
  21016. const TRANSLATION_VAR_PREFIX = 'i18n_';
  21017. /** Prefix of ICU expressions for post processing */
  21018. const I18N_ICU_MAPPING_PREFIX = 'I18N_EXP_';
  21019. /**
  21020. * The escape sequence used for message param values.
  21021. */
  21022. const ESCAPE = '\uFFFD';
  21023. /* Closure variables holding messages must be named `MSG_[A-Z0-9]+` */
  21024. const CLOSURE_TRANSLATION_VAR_PREFIX = 'MSG_';
  21025. /**
  21026. * Generates a prefix for translation const name.
  21027. *
  21028. * @param extra Additional local prefix that should be injected into translation var name
  21029. * @returns Complete translation const prefix
  21030. */
  21031. function getTranslationConstPrefix(extra) {
  21032. return `${CLOSURE_TRANSLATION_VAR_PREFIX}${extra}`.toUpperCase();
  21033. }
  21034. /**
  21035. * Generate AST to declare a variable. E.g. `var I18N_1;`.
  21036. * @param variable the name of the variable to declare.
  21037. */
  21038. function declareI18nVariable(variable) {
  21039. return new DeclareVarStmt(variable.name, undefined, INFERRED_TYPE, undefined, variable.sourceSpan);
  21040. }
  21041. /**
  21042. * Lifts i18n properties into the consts array.
  21043. * TODO: Can we use `ConstCollectedExpr`?
  21044. * TODO: The way the various attributes are linked together is very complex. Perhaps we could
  21045. * simplify the process, maybe by combining the context and message ops?
  21046. */
  21047. function collectI18nConsts(job) {
  21048. const fileBasedI18nSuffix = job.relativeContextFilePath.replace(/[^A-Za-z0-9]/g, '_').toUpperCase() + '_';
  21049. // Step One: Build up various lookup maps we need to collect all the consts.
  21050. // Context Xref -> Extracted Attribute Ops
  21051. const extractedAttributesByI18nContext = new Map();
  21052. // Element/ElementStart Xref -> I18n Attributes config op
  21053. const i18nAttributesByElement = new Map();
  21054. // Element/ElementStart Xref -> All I18n Expression ops for attrs on that target
  21055. const i18nExpressionsByElement = new Map();
  21056. // I18n Message Xref -> I18n Message Op (TODO: use a central op map)
  21057. const messages = new Map();
  21058. for (const unit of job.units) {
  21059. for (const op of unit.ops()) {
  21060. if (op.kind === OpKind.ExtractedAttribute && op.i18nContext !== null) {
  21061. const attributes = extractedAttributesByI18nContext.get(op.i18nContext) ?? [];
  21062. attributes.push(op);
  21063. extractedAttributesByI18nContext.set(op.i18nContext, attributes);
  21064. }
  21065. else if (op.kind === OpKind.I18nAttributes) {
  21066. i18nAttributesByElement.set(op.target, op);
  21067. }
  21068. else if (op.kind === OpKind.I18nExpression &&
  21069. op.usage === I18nExpressionFor.I18nAttribute) {
  21070. const expressions = i18nExpressionsByElement.get(op.target) ?? [];
  21071. expressions.push(op);
  21072. i18nExpressionsByElement.set(op.target, expressions);
  21073. }
  21074. else if (op.kind === OpKind.I18nMessage) {
  21075. messages.set(op.xref, op);
  21076. }
  21077. }
  21078. }
  21079. // Step Two: Serialize the extracted i18n messages for root i18n blocks and i18n attributes into
  21080. // the const array.
  21081. //
  21082. // Also, each i18n message will have a variable expression that can refer to its
  21083. // value. Store these expressions in the appropriate place:
  21084. // 1. For normal i18n content, it also goes in the const array. We save the const index to use
  21085. // later.
  21086. // 2. For extracted attributes, it becomes the value of the extracted attribute instruction.
  21087. // 3. For i18n bindings, it will go in a separate const array instruction below; for now, we just
  21088. // save it.
  21089. const i18nValuesByContext = new Map();
  21090. const messageConstIndices = new Map();
  21091. for (const unit of job.units) {
  21092. for (const op of unit.create) {
  21093. if (op.kind === OpKind.I18nMessage) {
  21094. if (op.messagePlaceholder === null) {
  21095. const { mainVar, statements } = collectMessage(job, fileBasedI18nSuffix, messages, op);
  21096. if (op.i18nBlock !== null) {
  21097. // This is a regular i18n message with a corresponding i18n block. Collect it into the
  21098. // const array.
  21099. const i18nConst = job.addConst(mainVar, statements);
  21100. messageConstIndices.set(op.i18nBlock, i18nConst);
  21101. }
  21102. else {
  21103. // This is an i18n attribute. Extract the initializers into the const pool.
  21104. job.constsInitializers.push(...statements);
  21105. // Save the i18n variable value for later.
  21106. i18nValuesByContext.set(op.i18nContext, mainVar);
  21107. // This i18n message may correspond to an individual extracted attribute. If so, The
  21108. // value of that attribute is updated to read the extracted i18n variable.
  21109. const attributesForMessage = extractedAttributesByI18nContext.get(op.i18nContext);
  21110. if (attributesForMessage !== undefined) {
  21111. for (const attr of attributesForMessage) {
  21112. attr.expression = mainVar.clone();
  21113. }
  21114. }
  21115. }
  21116. }
  21117. OpList.remove(op);
  21118. }
  21119. }
  21120. }
  21121. // Step Three: Serialize I18nAttributes configurations into the const array. Each I18nAttributes
  21122. // instruction has a config array, which contains k-v pairs describing each binding name, and the
  21123. // i18n variable that provides the value.
  21124. for (const unit of job.units) {
  21125. for (const elem of unit.create) {
  21126. if (isElementOrContainerOp(elem)) {
  21127. const i18nAttributes = i18nAttributesByElement.get(elem.xref);
  21128. if (i18nAttributes === undefined) {
  21129. // This element is not associated with an i18n attributes configuration instruction.
  21130. continue;
  21131. }
  21132. let i18nExpressions = i18nExpressionsByElement.get(elem.xref);
  21133. if (i18nExpressions === undefined) {
  21134. // Unused i18nAttributes should have already been removed.
  21135. // TODO: Should the removal of those dead instructions be merged with this phase?
  21136. throw new Error('AssertionError: Could not find any i18n expressions associated with an I18nAttributes instruction');
  21137. }
  21138. // Find expressions for all the unique property names, removing duplicates.
  21139. const seenPropertyNames = new Set();
  21140. i18nExpressions = i18nExpressions.filter((i18nExpr) => {
  21141. const seen = seenPropertyNames.has(i18nExpr.name);
  21142. seenPropertyNames.add(i18nExpr.name);
  21143. return !seen;
  21144. });
  21145. const i18nAttributeConfig = i18nExpressions.flatMap((i18nExpr) => {
  21146. const i18nExprValue = i18nValuesByContext.get(i18nExpr.context);
  21147. if (i18nExprValue === undefined) {
  21148. throw new Error("AssertionError: Could not find i18n expression's value");
  21149. }
  21150. return [literal(i18nExpr.name), i18nExprValue];
  21151. });
  21152. i18nAttributes.i18nAttributesConfig = job.addConst(new LiteralArrayExpr(i18nAttributeConfig));
  21153. }
  21154. }
  21155. }
  21156. // Step Four: Propagate the extracted const index into i18n ops that messages were extracted from.
  21157. for (const unit of job.units) {
  21158. for (const op of unit.create) {
  21159. if (op.kind === OpKind.I18nStart) {
  21160. const msgIndex = messageConstIndices.get(op.root);
  21161. if (msgIndex === undefined) {
  21162. throw new Error('AssertionError: Could not find corresponding i18n block index for an i18n message op; was an i18n message incorrectly assumed to correspond to an attribute?');
  21163. }
  21164. op.messageIndex = msgIndex;
  21165. }
  21166. }
  21167. }
  21168. }
  21169. /**
  21170. * Collects the given message into a set of statements that can be added to the const array.
  21171. * This will recursively collect any sub-messages referenced from the parent message as well.
  21172. */
  21173. function collectMessage(job, fileBasedI18nSuffix, messages, messageOp) {
  21174. // Recursively collect any sub-messages, record each sub-message's main variable under its
  21175. // placeholder so that we can add them to the params for the parent message. It is possible
  21176. // that multiple sub-messages will share the same placeholder, so we need to track an array of
  21177. // variables for each placeholder.
  21178. const statements = [];
  21179. const subMessagePlaceholders = new Map();
  21180. for (const subMessageId of messageOp.subMessages) {
  21181. const subMessage = messages.get(subMessageId);
  21182. const { mainVar: subMessageVar, statements: subMessageStatements } = collectMessage(job, fileBasedI18nSuffix, messages, subMessage);
  21183. statements.push(...subMessageStatements);
  21184. const subMessages = subMessagePlaceholders.get(subMessage.messagePlaceholder) ?? [];
  21185. subMessages.push(subMessageVar);
  21186. subMessagePlaceholders.set(subMessage.messagePlaceholder, subMessages);
  21187. }
  21188. addSubMessageParams(messageOp, subMessagePlaceholders);
  21189. // Sort the params for consistency with TemaplateDefinitionBuilder output.
  21190. messageOp.params = new Map([...messageOp.params.entries()].sort());
  21191. const mainVar = variable(job.pool.uniqueName(TRANSLATION_VAR_PREFIX));
  21192. // Closure Compiler requires const names to start with `MSG_` but disallows any other
  21193. // const to start with `MSG_`. We define a variable starting with `MSG_` just for the
  21194. // `goog.getMsg` call
  21195. const closureVar = i18nGenerateClosureVar(job.pool, messageOp.message.id, fileBasedI18nSuffix, job.i18nUseExternalIds);
  21196. let transformFn = undefined;
  21197. // If nescessary, add a post-processing step and resolve any placeholder params that are
  21198. // set in post-processing.
  21199. if (messageOp.needsPostprocessing || messageOp.postprocessingParams.size > 0) {
  21200. // Sort the post-processing params for consistency with TemaplateDefinitionBuilder output.
  21201. const postprocessingParams = Object.fromEntries([...messageOp.postprocessingParams.entries()].sort());
  21202. const formattedPostprocessingParams = formatI18nPlaceholderNamesInMap(postprocessingParams,
  21203. /* useCamelCase */ false);
  21204. const extraTransformFnParams = [];
  21205. if (messageOp.postprocessingParams.size > 0) {
  21206. extraTransformFnParams.push(mapLiteral(formattedPostprocessingParams, /* quoted */ true));
  21207. }
  21208. transformFn = (expr) => importExpr(Identifiers.i18nPostprocess).callFn([expr, ...extraTransformFnParams]);
  21209. }
  21210. // Add the message's statements
  21211. statements.push(...getTranslationDeclStmts(messageOp.message, mainVar, closureVar, messageOp.params, transformFn));
  21212. return { mainVar, statements };
  21213. }
  21214. /**
  21215. * Adds the given subMessage placeholders to the given message op.
  21216. *
  21217. * If a placeholder only corresponds to a single sub-message variable, we just set that variable
  21218. * as the param value. However, if the placeholder corresponds to multiple sub-message
  21219. * variables, we need to add a special placeholder value that is handled by the post-processing
  21220. * step. We then add the array of variables as a post-processing param.
  21221. */
  21222. function addSubMessageParams(messageOp, subMessagePlaceholders) {
  21223. for (const [placeholder, subMessages] of subMessagePlaceholders) {
  21224. if (subMessages.length === 1) {
  21225. messageOp.params.set(placeholder, subMessages[0]);
  21226. }
  21227. else {
  21228. messageOp.params.set(placeholder, literal(`${ESCAPE}${I18N_ICU_MAPPING_PREFIX}${placeholder}${ESCAPE}`));
  21229. messageOp.postprocessingParams.set(placeholder, literalArr(subMessages));
  21230. }
  21231. }
  21232. }
  21233. /**
  21234. * Generate statements that define a given translation message.
  21235. *
  21236. * ```ts
  21237. * var I18N_1;
  21238. * if (typeof ngI18nClosureMode !== undefined && ngI18nClosureMode) {
  21239. * var MSG_EXTERNAL_XXX = goog.getMsg(
  21240. * "Some message with {$interpolation}!",
  21241. * { "interpolation": "\uFFFD0\uFFFD" }
  21242. * );
  21243. * I18N_1 = MSG_EXTERNAL_XXX;
  21244. * }
  21245. * else {
  21246. * I18N_1 = $localize`Some message with ${'\uFFFD0\uFFFD'}!`;
  21247. * }
  21248. * ```
  21249. *
  21250. * @param message The original i18n AST message node
  21251. * @param variable The variable that will be assigned the translation, e.g. `I18N_1`.
  21252. * @param closureVar The variable for Closure `goog.getMsg` calls, e.g. `MSG_EXTERNAL_XXX`.
  21253. * @param params Object mapping placeholder names to their values (e.g.
  21254. * `{ "interpolation": "\uFFFD0\uFFFD" }`).
  21255. * @param transformFn Optional transformation function that will be applied to the translation
  21256. * (e.g.
  21257. * post-processing).
  21258. * @returns An array of statements that defined a given translation.
  21259. */
  21260. function getTranslationDeclStmts(message, variable, closureVar, params, transformFn) {
  21261. const paramsObject = Object.fromEntries(params);
  21262. const statements = [
  21263. declareI18nVariable(variable),
  21264. ifStmt(createClosureModeGuard(), createGoogleGetMsgStatements(variable, message, closureVar, paramsObject), createLocalizeStatements(variable, message, formatI18nPlaceholderNamesInMap(paramsObject, /* useCamelCase */ false))),
  21265. ];
  21266. if (transformFn) {
  21267. statements.push(new ExpressionStatement(variable.set(transformFn(variable))));
  21268. }
  21269. return statements;
  21270. }
  21271. /**
  21272. * Create the expression that will be used to guard the closure mode block
  21273. * It is equivalent to:
  21274. *
  21275. * ```ts
  21276. * typeof ngI18nClosureMode !== undefined && ngI18nClosureMode
  21277. * ```
  21278. */
  21279. function createClosureModeGuard() {
  21280. return typeofExpr(variable(NG_I18N_CLOSURE_MODE))
  21281. .notIdentical(literal('undefined', STRING_TYPE))
  21282. .and(variable(NG_I18N_CLOSURE_MODE));
  21283. }
  21284. /**
  21285. * Generates vars with Closure-specific names for i18n blocks (i.e. `MSG_XXX`).
  21286. */
  21287. function i18nGenerateClosureVar(pool, messageId, fileBasedI18nSuffix, useExternalIds) {
  21288. let name;
  21289. const suffix = fileBasedI18nSuffix;
  21290. if (useExternalIds) {
  21291. const prefix = getTranslationConstPrefix(`EXTERNAL_`);
  21292. const uniqueSuffix = pool.uniqueName(suffix);
  21293. name = `${prefix}${sanitizeIdentifier(messageId)}$$${uniqueSuffix}`;
  21294. }
  21295. else {
  21296. const prefix = getTranslationConstPrefix(suffix);
  21297. name = pool.uniqueName(prefix);
  21298. }
  21299. return variable(name);
  21300. }
  21301. /**
  21302. * Removes text nodes within i18n blocks since they are already hardcoded into the i18n message.
  21303. * Also, replaces interpolations on these text nodes with i18n expressions of the non-text portions,
  21304. * which will be applied later.
  21305. */
  21306. function convertI18nText(job) {
  21307. for (const unit of job.units) {
  21308. // Remove all text nodes within i18n blocks, their content is already captured in the i18n
  21309. // message.
  21310. let currentI18n = null;
  21311. let currentIcu = null;
  21312. const textNodeI18nBlocks = new Map();
  21313. const textNodeIcus = new Map();
  21314. const icuPlaceholderByText = new Map();
  21315. for (const op of unit.create) {
  21316. switch (op.kind) {
  21317. case OpKind.I18nStart:
  21318. if (op.context === null) {
  21319. throw Error('I18n op should have its context set.');
  21320. }
  21321. currentI18n = op;
  21322. break;
  21323. case OpKind.I18nEnd:
  21324. currentI18n = null;
  21325. break;
  21326. case OpKind.IcuStart:
  21327. if (op.context === null) {
  21328. throw Error('Icu op should have its context set.');
  21329. }
  21330. currentIcu = op;
  21331. break;
  21332. case OpKind.IcuEnd:
  21333. currentIcu = null;
  21334. break;
  21335. case OpKind.Text:
  21336. if (currentI18n !== null) {
  21337. textNodeI18nBlocks.set(op.xref, currentI18n);
  21338. textNodeIcus.set(op.xref, currentIcu);
  21339. if (op.icuPlaceholder !== null) {
  21340. // Create an op to represent the ICU placeholder. Initially set its static text to the
  21341. // value of the text op, though this may be overwritten later if this text op is a
  21342. // placeholder for an interpolation.
  21343. const icuPlaceholderOp = createIcuPlaceholderOp(job.allocateXrefId(), op.icuPlaceholder, [op.initialValue]);
  21344. OpList.replace(op, icuPlaceholderOp);
  21345. icuPlaceholderByText.set(op.xref, icuPlaceholderOp);
  21346. }
  21347. else {
  21348. // Otherwise just remove the text op, since its value is already accounted for in the
  21349. // translated message.
  21350. OpList.remove(op);
  21351. }
  21352. }
  21353. break;
  21354. }
  21355. }
  21356. // Update any interpolations to the removed text, and instead represent them as a series of i18n
  21357. // expressions that we then apply.
  21358. for (const op of unit.update) {
  21359. switch (op.kind) {
  21360. case OpKind.InterpolateText:
  21361. if (!textNodeI18nBlocks.has(op.target)) {
  21362. continue;
  21363. }
  21364. const i18nOp = textNodeI18nBlocks.get(op.target);
  21365. const icuOp = textNodeIcus.get(op.target);
  21366. const icuPlaceholder = icuPlaceholderByText.get(op.target);
  21367. const contextId = icuOp ? icuOp.context : i18nOp.context;
  21368. const resolutionTime = icuOp
  21369. ? I18nParamResolutionTime.Postproccessing
  21370. : I18nParamResolutionTime.Creation;
  21371. const ops = [];
  21372. for (let i = 0; i < op.interpolation.expressions.length; i++) {
  21373. const expr = op.interpolation.expressions[i];
  21374. // For now, this i18nExpression depends on the slot context of the enclosing i18n block.
  21375. // Later, we will modify this, and advance to a different point.
  21376. ops.push(createI18nExpressionOp(contextId, i18nOp.xref, i18nOp.xref, i18nOp.handle, expr, icuPlaceholder?.xref ?? null, op.interpolation.i18nPlaceholders[i] ?? null, resolutionTime, I18nExpressionFor.I18nText, '', expr.sourceSpan ?? op.sourceSpan));
  21377. }
  21378. OpList.replaceWithMany(op, ops);
  21379. // If this interpolation is part of an ICU placeholder, add the strings and expressions to
  21380. // the placeholder.
  21381. if (icuPlaceholder !== undefined) {
  21382. icuPlaceholder.strings = op.interpolation.strings;
  21383. }
  21384. break;
  21385. }
  21386. }
  21387. }
  21388. }
  21389. /**
  21390. * Lifts local reference declarations on element-like structures within each view into an entry in
  21391. * the `consts` array for the whole component.
  21392. */
  21393. function liftLocalRefs(job) {
  21394. for (const unit of job.units) {
  21395. for (const op of unit.create) {
  21396. switch (op.kind) {
  21397. case OpKind.ElementStart:
  21398. case OpKind.Template:
  21399. if (!Array.isArray(op.localRefs)) {
  21400. throw new Error(`AssertionError: expected localRefs to be an array still`);
  21401. }
  21402. op.numSlotsUsed += op.localRefs.length;
  21403. if (op.localRefs.length > 0) {
  21404. const localRefs = serializeLocalRefs(op.localRefs);
  21405. op.localRefs = job.addConst(localRefs);
  21406. }
  21407. else {
  21408. op.localRefs = null;
  21409. }
  21410. break;
  21411. }
  21412. }
  21413. }
  21414. }
  21415. function serializeLocalRefs(refs) {
  21416. const constRefs = [];
  21417. for (const ref of refs) {
  21418. constRefs.push(literal(ref.name), literal(ref.target));
  21419. }
  21420. return literalArr(constRefs);
  21421. }
  21422. /**
  21423. * Change namespaces between HTML, SVG and MathML, depending on the next element.
  21424. */
  21425. function emitNamespaceChanges(job) {
  21426. for (const unit of job.units) {
  21427. let activeNamespace = Namespace.HTML;
  21428. for (const op of unit.create) {
  21429. if (op.kind !== OpKind.ElementStart) {
  21430. continue;
  21431. }
  21432. if (op.namespace !== activeNamespace) {
  21433. OpList.insertBefore(createNamespaceOp(op.namespace), op);
  21434. activeNamespace = op.namespace;
  21435. }
  21436. }
  21437. }
  21438. }
  21439. /**
  21440. * Parses string representation of a style and converts it into object literal.
  21441. *
  21442. * @param value string representation of style as used in the `style` attribute in HTML.
  21443. * Example: `color: red; height: auto`.
  21444. * @returns An array of style property name and value pairs, e.g. `['color', 'red', 'height',
  21445. * 'auto']`
  21446. */
  21447. function parse(value) {
  21448. // we use a string array here instead of a string map
  21449. // because a string-map is not guaranteed to retain the
  21450. // order of the entries whereas a string array can be
  21451. // constructed in a [key, value, key, value] format.
  21452. const styles = [];
  21453. let i = 0;
  21454. let parenDepth = 0;
  21455. let quote = 0 /* Char.QuoteNone */;
  21456. let valueStart = 0;
  21457. let propStart = 0;
  21458. let currentProp = null;
  21459. while (i < value.length) {
  21460. const token = value.charCodeAt(i++);
  21461. switch (token) {
  21462. case 40 /* Char.OpenParen */:
  21463. parenDepth++;
  21464. break;
  21465. case 41 /* Char.CloseParen */:
  21466. parenDepth--;
  21467. break;
  21468. case 39 /* Char.QuoteSingle */:
  21469. // valueStart needs to be there since prop values don't
  21470. // have quotes in CSS
  21471. if (quote === 0 /* Char.QuoteNone */) {
  21472. quote = 39 /* Char.QuoteSingle */;
  21473. }
  21474. else if (quote === 39 /* Char.QuoteSingle */ && value.charCodeAt(i - 1) !== 92 /* Char.BackSlash */) {
  21475. quote = 0 /* Char.QuoteNone */;
  21476. }
  21477. break;
  21478. case 34 /* Char.QuoteDouble */:
  21479. // same logic as above
  21480. if (quote === 0 /* Char.QuoteNone */) {
  21481. quote = 34 /* Char.QuoteDouble */;
  21482. }
  21483. else if (quote === 34 /* Char.QuoteDouble */ && value.charCodeAt(i - 1) !== 92 /* Char.BackSlash */) {
  21484. quote = 0 /* Char.QuoteNone */;
  21485. }
  21486. break;
  21487. case 58 /* Char.Colon */:
  21488. if (!currentProp && parenDepth === 0 && quote === 0 /* Char.QuoteNone */) {
  21489. // TODO: Do not hyphenate CSS custom property names like: `--intentionallyCamelCase`
  21490. currentProp = hyphenate(value.substring(propStart, i - 1).trim());
  21491. valueStart = i;
  21492. }
  21493. break;
  21494. case 59 /* Char.Semicolon */:
  21495. if (currentProp && valueStart > 0 && parenDepth === 0 && quote === 0 /* Char.QuoteNone */) {
  21496. const styleVal = value.substring(valueStart, i - 1).trim();
  21497. styles.push(currentProp, styleVal);
  21498. propStart = i;
  21499. valueStart = 0;
  21500. currentProp = null;
  21501. }
  21502. break;
  21503. }
  21504. }
  21505. if (currentProp && valueStart) {
  21506. const styleVal = value.slice(valueStart).trim();
  21507. styles.push(currentProp, styleVal);
  21508. }
  21509. return styles;
  21510. }
  21511. function hyphenate(value) {
  21512. return value
  21513. .replace(/[a-z][A-Z]/g, (v) => {
  21514. return v.charAt(0) + '-' + v.charAt(1);
  21515. })
  21516. .toLowerCase();
  21517. }
  21518. /**
  21519. * Parses extracted style and class attributes into separate ExtractedAttributeOps per style or
  21520. * class property.
  21521. */
  21522. function parseExtractedStyles(job) {
  21523. const elements = new Map();
  21524. for (const unit of job.units) {
  21525. for (const op of unit.create) {
  21526. if (isElementOrContainerOp(op)) {
  21527. elements.set(op.xref, op);
  21528. }
  21529. }
  21530. }
  21531. for (const unit of job.units) {
  21532. for (const op of unit.create) {
  21533. if (op.kind === OpKind.ExtractedAttribute &&
  21534. op.bindingKind === BindingKind.Attribute &&
  21535. isStringLiteral(op.expression)) {
  21536. const target = elements.get(op.target);
  21537. if (target !== undefined &&
  21538. target.kind === OpKind.Template &&
  21539. target.templateKind === TemplateKind.Structural) {
  21540. // TemplateDefinitionBuilder will not apply class and style bindings to structural
  21541. // directives; instead, it will leave them as attributes.
  21542. // (It's not clear what that would mean, anyway -- classes and styles on a structural
  21543. // element should probably be a parse error.)
  21544. // TODO: We may be able to remove this once Template Pipeline is the default.
  21545. continue;
  21546. }
  21547. if (op.name === 'style') {
  21548. const parsedStyles = parse(op.expression.value);
  21549. for (let i = 0; i < parsedStyles.length - 1; i += 2) {
  21550. OpList.insertBefore(createExtractedAttributeOp(op.target, BindingKind.StyleProperty, null, parsedStyles[i], literal(parsedStyles[i + 1]), null, null, SecurityContext.STYLE), op);
  21551. }
  21552. OpList.remove(op);
  21553. }
  21554. else if (op.name === 'class') {
  21555. const parsedClasses = op.expression.value.trim().split(/\s+/g);
  21556. for (const parsedClass of parsedClasses) {
  21557. OpList.insertBefore(createExtractedAttributeOp(op.target, BindingKind.ClassName, null, parsedClass, null, null, null, SecurityContext.NONE), op);
  21558. }
  21559. OpList.remove(op);
  21560. }
  21561. }
  21562. }
  21563. }
  21564. }
  21565. /**
  21566. * Generate names for functions and variables across all views.
  21567. *
  21568. * This includes propagating those names into any `ir.ReadVariableExpr`s of those variables, so that
  21569. * the reads can be emitted correctly.
  21570. */
  21571. function nameFunctionsAndVariables(job) {
  21572. addNamesToView(job.root, job.componentName, { index: 0 }, job.compatibility === CompatibilityMode.TemplateDefinitionBuilder);
  21573. }
  21574. function addNamesToView(unit, baseName, state, compatibility) {
  21575. if (unit.fnName === null) {
  21576. // Ensure unique names for view units. This is necessary because there might be multiple
  21577. // components with same names in the context of the same pool. Only add the suffix
  21578. // if really needed.
  21579. unit.fnName = unit.job.pool.uniqueName(sanitizeIdentifier(`${baseName}_${unit.job.fnSuffix}`),
  21580. /* alwaysIncludeSuffix */ false);
  21581. }
  21582. // Keep track of the names we assign to variables in the view. We'll need to propagate these
  21583. // into reads of those variables afterwards.
  21584. const varNames = new Map();
  21585. for (const op of unit.ops()) {
  21586. switch (op.kind) {
  21587. case OpKind.Property:
  21588. case OpKind.HostProperty:
  21589. if (op.isAnimationTrigger) {
  21590. op.name = '@' + op.name;
  21591. }
  21592. break;
  21593. case OpKind.Listener:
  21594. if (op.handlerFnName !== null) {
  21595. break;
  21596. }
  21597. if (!op.hostListener && op.targetSlot.slot === null) {
  21598. throw new Error(`Expected a slot to be assigned`);
  21599. }
  21600. let animation = '';
  21601. if (op.isAnimationListener) {
  21602. op.name = `@${op.name}.${op.animationPhase}`;
  21603. animation = 'animation';
  21604. }
  21605. if (op.hostListener) {
  21606. op.handlerFnName = `${baseName}_${animation}${op.name}_HostBindingHandler`;
  21607. }
  21608. else {
  21609. op.handlerFnName = `${unit.fnName}_${op.tag.replace('-', '_')}_${animation}${op.name}_${op.targetSlot.slot}_listener`;
  21610. }
  21611. op.handlerFnName = sanitizeIdentifier(op.handlerFnName);
  21612. break;
  21613. case OpKind.TwoWayListener:
  21614. if (op.handlerFnName !== null) {
  21615. break;
  21616. }
  21617. if (op.targetSlot.slot === null) {
  21618. throw new Error(`Expected a slot to be assigned`);
  21619. }
  21620. op.handlerFnName = sanitizeIdentifier(`${unit.fnName}_${op.tag.replace('-', '_')}_${op.name}_${op.targetSlot.slot}_listener`);
  21621. break;
  21622. case OpKind.Variable:
  21623. varNames.set(op.xref, getVariableName(unit, op.variable, state));
  21624. break;
  21625. case OpKind.RepeaterCreate:
  21626. if (!(unit instanceof ViewCompilationUnit)) {
  21627. throw new Error(`AssertionError: must be compiling a component`);
  21628. }
  21629. if (op.handle.slot === null) {
  21630. throw new Error(`Expected slot to be assigned`);
  21631. }
  21632. if (op.emptyView !== null) {
  21633. const emptyView = unit.job.views.get(op.emptyView);
  21634. // Repeater empty view function is at slot +2 (metadata is in the first slot).
  21635. addNamesToView(emptyView, `${baseName}_${op.functionNameSuffix}Empty_${op.handle.slot + 2}`, state, compatibility);
  21636. }
  21637. // Repeater primary view function is at slot +1 (metadata is in the first slot).
  21638. addNamesToView(unit.job.views.get(op.xref), `${baseName}_${op.functionNameSuffix}_${op.handle.slot + 1}`, state, compatibility);
  21639. break;
  21640. case OpKind.Projection:
  21641. if (!(unit instanceof ViewCompilationUnit)) {
  21642. throw new Error(`AssertionError: must be compiling a component`);
  21643. }
  21644. if (op.handle.slot === null) {
  21645. throw new Error(`Expected slot to be assigned`);
  21646. }
  21647. if (op.fallbackView !== null) {
  21648. const fallbackView = unit.job.views.get(op.fallbackView);
  21649. addNamesToView(fallbackView, `${baseName}_ProjectionFallback_${op.handle.slot}`, state, compatibility);
  21650. }
  21651. break;
  21652. case OpKind.Template:
  21653. if (!(unit instanceof ViewCompilationUnit)) {
  21654. throw new Error(`AssertionError: must be compiling a component`);
  21655. }
  21656. const childView = unit.job.views.get(op.xref);
  21657. if (op.handle.slot === null) {
  21658. throw new Error(`Expected slot to be assigned`);
  21659. }
  21660. const suffix = op.functionNameSuffix.length === 0 ? '' : `_${op.functionNameSuffix}`;
  21661. addNamesToView(childView, `${baseName}${suffix}_${op.handle.slot}`, state, compatibility);
  21662. break;
  21663. case OpKind.StyleProp:
  21664. op.name = normalizeStylePropName(op.name);
  21665. if (compatibility) {
  21666. op.name = stripImportant(op.name);
  21667. }
  21668. break;
  21669. case OpKind.ClassProp:
  21670. if (compatibility) {
  21671. op.name = stripImportant(op.name);
  21672. }
  21673. break;
  21674. }
  21675. }
  21676. // Having named all variables declared in the view, now we can push those names into the
  21677. // `ir.ReadVariableExpr` expressions which represent reads of those variables.
  21678. for (const op of unit.ops()) {
  21679. visitExpressionsInOp(op, (expr) => {
  21680. if (!(expr instanceof ReadVariableExpr) || expr.name !== null) {
  21681. return;
  21682. }
  21683. if (!varNames.has(expr.xref)) {
  21684. throw new Error(`Variable ${expr.xref} not yet named`);
  21685. }
  21686. expr.name = varNames.get(expr.xref);
  21687. });
  21688. }
  21689. }
  21690. function getVariableName(unit, variable, state) {
  21691. if (variable.name === null) {
  21692. switch (variable.kind) {
  21693. case SemanticVariableKind.Context:
  21694. variable.name = `ctx_r${state.index++}`;
  21695. break;
  21696. case SemanticVariableKind.Identifier:
  21697. if (unit.job.compatibility === CompatibilityMode.TemplateDefinitionBuilder) {
  21698. // TODO: Prefix increment and `_r` are for compatibility with the old naming scheme.
  21699. // This has the potential to cause collisions when `ctx` is the identifier, so we need a
  21700. // special check for that as well.
  21701. const compatPrefix = variable.identifier === 'ctx' ? 'i' : '';
  21702. variable.name = `${variable.identifier}_${compatPrefix}r${++state.index}`;
  21703. }
  21704. else {
  21705. variable.name = `${variable.identifier}_i${state.index++}`;
  21706. }
  21707. break;
  21708. default:
  21709. // TODO: Prefix increment for compatibility only.
  21710. variable.name = `_r${++state.index}`;
  21711. break;
  21712. }
  21713. }
  21714. return variable.name;
  21715. }
  21716. /**
  21717. * Normalizes a style prop name by hyphenating it (unless its a CSS variable).
  21718. */
  21719. function normalizeStylePropName(name) {
  21720. return name.startsWith('--') ? name : hyphenate(name);
  21721. }
  21722. /**
  21723. * Strips `!important` out of the given style or class name.
  21724. */
  21725. function stripImportant(name) {
  21726. const importantIndex = name.indexOf('!important');
  21727. if (importantIndex > -1) {
  21728. return name.substring(0, importantIndex);
  21729. }
  21730. return name;
  21731. }
  21732. /**
  21733. * Merges logically sequential `NextContextExpr` operations.
  21734. *
  21735. * `NextContextExpr` can be referenced repeatedly, "popping" the runtime's context stack each time.
  21736. * When two such expressions appear back-to-back, it's possible to merge them together into a single
  21737. * `NextContextExpr` that steps multiple contexts. This merging is possible if all conditions are
  21738. * met:
  21739. *
  21740. * * The result of the `NextContextExpr` that's folded into the subsequent one is not stored (that
  21741. * is, the call is purely side-effectful).
  21742. * * No operations in between them uses the implicit context.
  21743. */
  21744. function mergeNextContextExpressions(job) {
  21745. for (const unit of job.units) {
  21746. for (const op of unit.create) {
  21747. if (op.kind === OpKind.Listener || op.kind === OpKind.TwoWayListener) {
  21748. mergeNextContextsInOps(op.handlerOps);
  21749. }
  21750. }
  21751. mergeNextContextsInOps(unit.update);
  21752. }
  21753. }
  21754. function mergeNextContextsInOps(ops) {
  21755. for (const op of ops) {
  21756. // Look for a candidate operation to maybe merge.
  21757. if (op.kind !== OpKind.Statement ||
  21758. !(op.statement instanceof ExpressionStatement) ||
  21759. !(op.statement.expr instanceof NextContextExpr)) {
  21760. continue;
  21761. }
  21762. const mergeSteps = op.statement.expr.steps;
  21763. // Try to merge this `ir.NextContextExpr`.
  21764. let tryToMerge = true;
  21765. for (let candidate = op.next; candidate.kind !== OpKind.ListEnd && tryToMerge; candidate = candidate.next) {
  21766. visitExpressionsInOp(candidate, (expr, flags) => {
  21767. if (!isIrExpression(expr)) {
  21768. return expr;
  21769. }
  21770. if (!tryToMerge) {
  21771. // Either we've already merged, or failed to merge.
  21772. return;
  21773. }
  21774. if (flags & VisitorContextFlag.InChildOperation) {
  21775. // We cannot merge into child operations.
  21776. return;
  21777. }
  21778. switch (expr.kind) {
  21779. case ExpressionKind.NextContext:
  21780. // Merge the previous `ir.NextContextExpr` into this one.
  21781. expr.steps += mergeSteps;
  21782. OpList.remove(op);
  21783. tryToMerge = false;
  21784. break;
  21785. case ExpressionKind.GetCurrentView:
  21786. case ExpressionKind.Reference:
  21787. case ExpressionKind.ContextLetReference:
  21788. // Can't merge past a dependency on the context.
  21789. tryToMerge = false;
  21790. break;
  21791. }
  21792. return;
  21793. });
  21794. }
  21795. }
  21796. }
  21797. const CONTAINER_TAG = 'ng-container';
  21798. /**
  21799. * Replace an `Element` or `ElementStart` whose tag is `ng-container` with a specific op.
  21800. */
  21801. function generateNgContainerOps(job) {
  21802. for (const unit of job.units) {
  21803. const updatedElementXrefs = new Set();
  21804. for (const op of unit.create) {
  21805. if (op.kind === OpKind.ElementStart && op.tag === CONTAINER_TAG) {
  21806. // Transmute the `ElementStart` instruction to `ContainerStart`.
  21807. op.kind = OpKind.ContainerStart;
  21808. updatedElementXrefs.add(op.xref);
  21809. }
  21810. if (op.kind === OpKind.ElementEnd && updatedElementXrefs.has(op.xref)) {
  21811. // This `ElementEnd` is associated with an `ElementStart` we already transmuted.
  21812. op.kind = OpKind.ContainerEnd;
  21813. }
  21814. }
  21815. }
  21816. }
  21817. /**
  21818. * Looks up an element in the given map by xref ID.
  21819. */
  21820. function lookupElement(elements, xref) {
  21821. const el = elements.get(xref);
  21822. if (el === undefined) {
  21823. throw new Error('All attributes should have an element-like target.');
  21824. }
  21825. return el;
  21826. }
  21827. /**
  21828. * When a container is marked with `ngNonBindable`, the non-bindable characteristic also applies to
  21829. * all descendants of that container. Therefore, we must emit `disableBindings` and `enableBindings`
  21830. * instructions for every such container.
  21831. */
  21832. function disableBindings$1(job) {
  21833. const elements = new Map();
  21834. for (const view of job.units) {
  21835. for (const op of view.create) {
  21836. if (!isElementOrContainerOp(op)) {
  21837. continue;
  21838. }
  21839. elements.set(op.xref, op);
  21840. }
  21841. }
  21842. for (const unit of job.units) {
  21843. for (const op of unit.create) {
  21844. if ((op.kind === OpKind.ElementStart || op.kind === OpKind.ContainerStart) &&
  21845. op.nonBindable) {
  21846. OpList.insertAfter(createDisableBindingsOp(op.xref), op);
  21847. }
  21848. if ((op.kind === OpKind.ElementEnd || op.kind === OpKind.ContainerEnd) &&
  21849. lookupElement(elements, op.xref).nonBindable) {
  21850. OpList.insertBefore(createEnableBindingsOp(op.xref), op);
  21851. }
  21852. }
  21853. }
  21854. }
  21855. /**
  21856. * Nullish coalescing expressions such as `a ?? b` have different semantics in Angular templates as
  21857. * compared to JavaScript. In particular, they default to `null` instead of `undefined`. Therefore,
  21858. * we replace them with ternary expressions, assigning temporaries as needed to avoid re-evaluating
  21859. * the same sub-expression multiple times.
  21860. */
  21861. function generateNullishCoalesceExpressions(job) {
  21862. for (const unit of job.units) {
  21863. for (const op of unit.ops()) {
  21864. transformExpressionsInOp(op, (expr) => {
  21865. if (!(expr instanceof BinaryOperatorExpr) ||
  21866. expr.operator !== BinaryOperator.NullishCoalesce) {
  21867. return expr;
  21868. }
  21869. const assignment = new AssignTemporaryExpr(expr.lhs.clone(), job.allocateXrefId());
  21870. const read = new ReadTemporaryExpr(assignment.xref);
  21871. // TODO: When not in compatibility mode for TemplateDefinitionBuilder, we can just emit
  21872. // `t != null` instead of including an undefined check as well.
  21873. return new ConditionalExpr(new BinaryOperatorExpr(BinaryOperator.And, new BinaryOperatorExpr(BinaryOperator.NotIdentical, assignment, NULL_EXPR), new BinaryOperatorExpr(BinaryOperator.NotIdentical, read, new LiteralExpr(undefined))), read.clone(), expr.rhs);
  21874. }, VisitorContextFlag.None);
  21875. }
  21876. }
  21877. }
  21878. function kindTest(kind) {
  21879. return (op) => op.kind === kind;
  21880. }
  21881. function kindWithInterpolationTest(kind, interpolation) {
  21882. return (op) => {
  21883. return op.kind === kind && interpolation === op.expression instanceof Interpolation;
  21884. };
  21885. }
  21886. function basicListenerKindTest(op) {
  21887. return ((op.kind === OpKind.Listener && !(op.hostListener && op.isAnimationListener)) ||
  21888. op.kind === OpKind.TwoWayListener);
  21889. }
  21890. function nonInterpolationPropertyKindTest(op) {
  21891. return ((op.kind === OpKind.Property || op.kind === OpKind.TwoWayProperty) &&
  21892. !(op.expression instanceof Interpolation));
  21893. }
  21894. /**
  21895. * Defines the groups based on `OpKind` that ops will be divided into, for the various create
  21896. * op kinds. Ops will be collected into groups, then optionally transformed, before recombining
  21897. * the groups in the order defined here.
  21898. */
  21899. const CREATE_ORDERING = [
  21900. { test: (op) => op.kind === OpKind.Listener && op.hostListener && op.isAnimationListener },
  21901. { test: basicListenerKindTest },
  21902. ];
  21903. /**
  21904. * Defines the groups based on `OpKind` that ops will be divided into, for the various update
  21905. * op kinds.
  21906. */
  21907. const UPDATE_ORDERING = [
  21908. { test: kindTest(OpKind.StyleMap), transform: keepLast },
  21909. { test: kindTest(OpKind.ClassMap), transform: keepLast },
  21910. { test: kindTest(OpKind.StyleProp) },
  21911. { test: kindTest(OpKind.ClassProp) },
  21912. { test: kindWithInterpolationTest(OpKind.Attribute, true) },
  21913. { test: kindWithInterpolationTest(OpKind.Property, true) },
  21914. { test: nonInterpolationPropertyKindTest },
  21915. { test: kindWithInterpolationTest(OpKind.Attribute, false) },
  21916. ];
  21917. /**
  21918. * Host bindings have their own update ordering.
  21919. */
  21920. const UPDATE_HOST_ORDERING = [
  21921. { test: kindWithInterpolationTest(OpKind.HostProperty, true) },
  21922. { test: kindWithInterpolationTest(OpKind.HostProperty, false) },
  21923. { test: kindTest(OpKind.Attribute) },
  21924. { test: kindTest(OpKind.StyleMap), transform: keepLast },
  21925. { test: kindTest(OpKind.ClassMap), transform: keepLast },
  21926. { test: kindTest(OpKind.StyleProp) },
  21927. { test: kindTest(OpKind.ClassProp) },
  21928. ];
  21929. /**
  21930. * The set of all op kinds we handle in the reordering phase.
  21931. */
  21932. const handledOpKinds = new Set([
  21933. OpKind.Listener,
  21934. OpKind.TwoWayListener,
  21935. OpKind.StyleMap,
  21936. OpKind.ClassMap,
  21937. OpKind.StyleProp,
  21938. OpKind.ClassProp,
  21939. OpKind.Property,
  21940. OpKind.TwoWayProperty,
  21941. OpKind.HostProperty,
  21942. OpKind.Attribute,
  21943. ]);
  21944. /**
  21945. * Many type of operations have ordering constraints that must be respected. For example, a
  21946. * `ClassMap` instruction must be ordered after a `StyleMap` instruction, in order to have
  21947. * predictable semantics that match TemplateDefinitionBuilder and don't break applications.
  21948. */
  21949. function orderOps(job) {
  21950. for (const unit of job.units) {
  21951. // First, we pull out ops that need to be ordered. Then, when we encounter an op that shouldn't
  21952. // be reordered, put the ones we've pulled so far back in the correct order. Finally, if we
  21953. // still have ops pulled at the end, put them back in the correct order.
  21954. // Create mode:
  21955. orderWithin(unit.create, CREATE_ORDERING);
  21956. // Update mode:
  21957. const ordering = unit.job.kind === CompilationJobKind.Host ? UPDATE_HOST_ORDERING : UPDATE_ORDERING;
  21958. orderWithin(unit.update, ordering);
  21959. }
  21960. }
  21961. /**
  21962. * Order all the ops within the specified group.
  21963. */
  21964. function orderWithin(opList, ordering) {
  21965. let opsToOrder = [];
  21966. // Only reorder ops that target the same xref; do not mix ops that target different xrefs.
  21967. let firstTargetInGroup = null;
  21968. for (const op of opList) {
  21969. const currentTarget = hasDependsOnSlotContextTrait(op) ? op.target : null;
  21970. if (!handledOpKinds.has(op.kind) ||
  21971. (currentTarget !== firstTargetInGroup &&
  21972. firstTargetInGroup !== null &&
  21973. currentTarget !== null)) {
  21974. OpList.insertBefore(reorder(opsToOrder, ordering), op);
  21975. opsToOrder = [];
  21976. firstTargetInGroup = null;
  21977. }
  21978. if (handledOpKinds.has(op.kind)) {
  21979. opsToOrder.push(op);
  21980. OpList.remove(op);
  21981. firstTargetInGroup = currentTarget ?? firstTargetInGroup;
  21982. }
  21983. }
  21984. opList.push(reorder(opsToOrder, ordering));
  21985. }
  21986. /**
  21987. * Reorders the given list of ops according to the ordering defined by `ORDERING`.
  21988. */
  21989. function reorder(ops, ordering) {
  21990. // Break the ops list into groups based on OpKind.
  21991. const groups = Array.from(ordering, () => new Array());
  21992. for (const op of ops) {
  21993. const groupIndex = ordering.findIndex((o) => o.test(op));
  21994. groups[groupIndex].push(op);
  21995. }
  21996. // Reassemble the groups into a single list, in the correct order.
  21997. return groups.flatMap((group, i) => {
  21998. const transform = ordering[i].transform;
  21999. return transform ? transform(group) : group;
  22000. });
  22001. }
  22002. /**
  22003. * Keeps only the last op in a list of ops.
  22004. */
  22005. function keepLast(ops) {
  22006. return ops.slice(ops.length - 1);
  22007. }
  22008. /**
  22009. * Attributes of `ng-content` named 'select' are specifically removed, because they control which
  22010. * content matches as a property of the `projection`, and are not a plain attribute.
  22011. */
  22012. function removeContentSelectors(job) {
  22013. for (const unit of job.units) {
  22014. const elements = createOpXrefMap(unit);
  22015. for (const op of unit.ops()) {
  22016. switch (op.kind) {
  22017. case OpKind.Binding:
  22018. const target = lookupInXrefMap(elements, op.target);
  22019. if (isSelectAttribute(op.name) && target.kind === OpKind.Projection) {
  22020. OpList.remove(op);
  22021. }
  22022. break;
  22023. }
  22024. }
  22025. }
  22026. }
  22027. function isSelectAttribute(name) {
  22028. return name.toLowerCase() === 'select';
  22029. }
  22030. /**
  22031. * Looks up an element in the given map by xref ID.
  22032. */
  22033. function lookupInXrefMap(map, xref) {
  22034. const el = map.get(xref);
  22035. if (el === undefined) {
  22036. throw new Error('All attributes should have an slottable target.');
  22037. }
  22038. return el;
  22039. }
  22040. /**
  22041. * This phase generates pipe creation instructions. We do this based on the pipe bindings found in
  22042. * the update block, in the order we see them.
  22043. *
  22044. * When not in compatibility mode, we can simply group all these creation instructions together, to
  22045. * maximize chaining opportunities.
  22046. */
  22047. function createPipes(job) {
  22048. for (const unit of job.units) {
  22049. processPipeBindingsInView(unit);
  22050. }
  22051. }
  22052. function processPipeBindingsInView(unit) {
  22053. for (const updateOp of unit.update) {
  22054. visitExpressionsInOp(updateOp, (expr, flags) => {
  22055. if (!isIrExpression(expr)) {
  22056. return;
  22057. }
  22058. if (expr.kind !== ExpressionKind.PipeBinding) {
  22059. return;
  22060. }
  22061. if (flags & VisitorContextFlag.InChildOperation) {
  22062. throw new Error(`AssertionError: pipe bindings should not appear in child expressions`);
  22063. }
  22064. if (unit.job.compatibility) {
  22065. // TODO: We can delete this cast and check once compatibility mode is removed.
  22066. const slotHandle = updateOp.target;
  22067. if (slotHandle == undefined) {
  22068. throw new Error(`AssertionError: expected slot handle to be assigned for pipe creation`);
  22069. }
  22070. addPipeToCreationBlock(unit, updateOp.target, expr);
  22071. }
  22072. else {
  22073. // When not in compatibility mode, we just add the pipe to the end of the create block. This
  22074. // is not only simpler and faster, but allows more chaining opportunities for other
  22075. // instructions.
  22076. unit.create.push(createPipeOp(expr.target, expr.targetSlot, expr.name));
  22077. }
  22078. });
  22079. }
  22080. }
  22081. function addPipeToCreationBlock(unit, afterTargetXref, binding) {
  22082. // Find the appropriate point to insert the Pipe creation operation.
  22083. // We're looking for `afterTargetXref` (and also want to insert after any other pipe operations
  22084. // which might be beyond it).
  22085. for (let op = unit.create.head.next; op.kind !== OpKind.ListEnd; op = op.next) {
  22086. if (!hasConsumesSlotTrait(op)) {
  22087. continue;
  22088. }
  22089. if (op.xref !== afterTargetXref) {
  22090. continue;
  22091. }
  22092. // We've found a tentative insertion point; however, we also want to skip past any _other_ pipe
  22093. // operations present.
  22094. while (op.next.kind === OpKind.Pipe) {
  22095. op = op.next;
  22096. }
  22097. const pipe = createPipeOp(binding.target, binding.targetSlot, binding.name);
  22098. OpList.insertBefore(pipe, op.next);
  22099. // This completes adding the pipe to the creation block.
  22100. return;
  22101. }
  22102. // At this point, we've failed to add the pipe to the creation block.
  22103. throw new Error(`AssertionError: unable to find insertion point for pipe ${binding.name}`);
  22104. }
  22105. /**
  22106. * Pipes that accept more than 4 arguments are variadic, and are handled with a different runtime
  22107. * instruction.
  22108. */
  22109. function createVariadicPipes(job) {
  22110. for (const unit of job.units) {
  22111. for (const op of unit.update) {
  22112. transformExpressionsInOp(op, (expr) => {
  22113. if (!(expr instanceof PipeBindingExpr)) {
  22114. return expr;
  22115. }
  22116. // Pipes are variadic if they have more than 4 arguments.
  22117. if (expr.args.length <= 4) {
  22118. return expr;
  22119. }
  22120. return new PipeBindingVariadicExpr(expr.target, expr.targetSlot, expr.name, literalArr(expr.args), expr.args.length);
  22121. }, VisitorContextFlag.None);
  22122. }
  22123. }
  22124. }
  22125. /**
  22126. * Propagate i18n blocks down through child templates that act as placeholders in the root i18n
  22127. * message. Specifically, perform an in-order traversal of all the views, and add i18nStart/i18nEnd
  22128. * op pairs into descending views. Also, assign an increasing sub-template index to each
  22129. * descending view.
  22130. */
  22131. function propagateI18nBlocks(job) {
  22132. propagateI18nBlocksToTemplates(job.root, 0);
  22133. }
  22134. /**
  22135. * Propagates i18n ops in the given view through to any child views recursively.
  22136. */
  22137. function propagateI18nBlocksToTemplates(unit, subTemplateIndex) {
  22138. let i18nBlock = null;
  22139. for (const op of unit.create) {
  22140. switch (op.kind) {
  22141. case OpKind.I18nStart:
  22142. op.subTemplateIndex = subTemplateIndex === 0 ? null : subTemplateIndex;
  22143. i18nBlock = op;
  22144. break;
  22145. case OpKind.I18nEnd:
  22146. // When we exit a root-level i18n block, reset the sub-template index counter.
  22147. if (i18nBlock.subTemplateIndex === null) {
  22148. subTemplateIndex = 0;
  22149. }
  22150. i18nBlock = null;
  22151. break;
  22152. case OpKind.Template:
  22153. subTemplateIndex = propagateI18nBlocksForView(unit.job.views.get(op.xref), i18nBlock, op.i18nPlaceholder, subTemplateIndex);
  22154. break;
  22155. case OpKind.RepeaterCreate:
  22156. // Propagate i18n blocks to the @for template.
  22157. const forView = unit.job.views.get(op.xref);
  22158. subTemplateIndex = propagateI18nBlocksForView(forView, i18nBlock, op.i18nPlaceholder, subTemplateIndex);
  22159. // Then if there's an @empty template, propagate the i18n blocks for it as well.
  22160. if (op.emptyView !== null) {
  22161. subTemplateIndex = propagateI18nBlocksForView(unit.job.views.get(op.emptyView), i18nBlock, op.emptyI18nPlaceholder, subTemplateIndex);
  22162. }
  22163. break;
  22164. }
  22165. }
  22166. return subTemplateIndex;
  22167. }
  22168. /**
  22169. * Propagate i18n blocks for a view.
  22170. */
  22171. function propagateI18nBlocksForView(view, i18nBlock, i18nPlaceholder, subTemplateIndex) {
  22172. // We found an <ng-template> inside an i18n block; increment the sub-template counter and
  22173. // wrap the template's view in a child i18n block.
  22174. if (i18nPlaceholder !== undefined) {
  22175. if (i18nBlock === null) {
  22176. throw Error('Expected template with i18n placeholder to be in an i18n block.');
  22177. }
  22178. subTemplateIndex++;
  22179. wrapTemplateWithI18n(view, i18nBlock);
  22180. }
  22181. // Continue traversing inside the template's view.
  22182. return propagateI18nBlocksToTemplates(view, subTemplateIndex);
  22183. }
  22184. /**
  22185. * Wraps a template view with i18n start and end ops.
  22186. */
  22187. function wrapTemplateWithI18n(unit, parentI18n) {
  22188. // Only add i18n ops if they have not already been propagated to this template.
  22189. if (unit.create.head.next?.kind !== OpKind.I18nStart) {
  22190. const id = unit.job.allocateXrefId();
  22191. OpList.insertAfter(
  22192. // Nested ng-template i18n start/end ops should not receive source spans.
  22193. createI18nStartOp(id, parentI18n.message, parentI18n.root, null), unit.create.head);
  22194. OpList.insertBefore(createI18nEndOp(id, null), unit.create.tail);
  22195. }
  22196. }
  22197. function extractPureFunctions(job) {
  22198. for (const view of job.units) {
  22199. for (const op of view.ops()) {
  22200. visitExpressionsInOp(op, (expr) => {
  22201. if (!(expr instanceof PureFunctionExpr) || expr.body === null) {
  22202. return;
  22203. }
  22204. const constantDef = new PureFunctionConstant(expr.args.length);
  22205. expr.fn = job.pool.getSharedConstant(constantDef, expr.body);
  22206. expr.body = null;
  22207. });
  22208. }
  22209. }
  22210. }
  22211. class PureFunctionConstant extends GenericKeyFn {
  22212. numArgs;
  22213. constructor(numArgs) {
  22214. super();
  22215. this.numArgs = numArgs;
  22216. }
  22217. keyOf(expr) {
  22218. if (expr instanceof PureFunctionParameterExpr) {
  22219. return `param(${expr.index})`;
  22220. }
  22221. else {
  22222. return super.keyOf(expr);
  22223. }
  22224. }
  22225. // TODO: Use the new pool method `getSharedFunctionReference`
  22226. toSharedConstantDeclaration(declName, keyExpr) {
  22227. const fnParams = [];
  22228. for (let idx = 0; idx < this.numArgs; idx++) {
  22229. fnParams.push(new FnParam('a' + idx));
  22230. }
  22231. // We will never visit `ir.PureFunctionParameterExpr`s that don't belong to us, because this
  22232. // transform runs inside another visitor which will visit nested pure functions before this one.
  22233. const returnExpr = transformExpressionsInExpression(keyExpr, (expr) => {
  22234. if (!(expr instanceof PureFunctionParameterExpr)) {
  22235. return expr;
  22236. }
  22237. return variable('a' + expr.index);
  22238. }, VisitorContextFlag.None);
  22239. return new DeclareVarStmt(declName, new ArrowFunctionExpr(fnParams, returnExpr), undefined, StmtModifier.Final);
  22240. }
  22241. }
  22242. function generatePureLiteralStructures(job) {
  22243. for (const unit of job.units) {
  22244. for (const op of unit.update) {
  22245. transformExpressionsInOp(op, (expr, flags) => {
  22246. if (flags & VisitorContextFlag.InChildOperation) {
  22247. return expr;
  22248. }
  22249. if (expr instanceof LiteralArrayExpr) {
  22250. return transformLiteralArray(expr);
  22251. }
  22252. else if (expr instanceof LiteralMapExpr) {
  22253. return transformLiteralMap(expr);
  22254. }
  22255. return expr;
  22256. }, VisitorContextFlag.None);
  22257. }
  22258. }
  22259. }
  22260. function transformLiteralArray(expr) {
  22261. const derivedEntries = [];
  22262. const nonConstantArgs = [];
  22263. for (const entry of expr.entries) {
  22264. if (entry.isConstant()) {
  22265. derivedEntries.push(entry);
  22266. }
  22267. else {
  22268. const idx = nonConstantArgs.length;
  22269. nonConstantArgs.push(entry);
  22270. derivedEntries.push(new PureFunctionParameterExpr(idx));
  22271. }
  22272. }
  22273. return new PureFunctionExpr(literalArr(derivedEntries), nonConstantArgs);
  22274. }
  22275. function transformLiteralMap(expr) {
  22276. let derivedEntries = [];
  22277. const nonConstantArgs = [];
  22278. for (const entry of expr.entries) {
  22279. if (entry.value.isConstant()) {
  22280. derivedEntries.push(entry);
  22281. }
  22282. else {
  22283. const idx = nonConstantArgs.length;
  22284. nonConstantArgs.push(entry.value);
  22285. derivedEntries.push(new LiteralMapEntry(entry.key, new PureFunctionParameterExpr(idx), entry.quoted));
  22286. }
  22287. }
  22288. return new PureFunctionExpr(literalMap(derivedEntries), nonConstantArgs);
  22289. }
  22290. // This file contains helpers for generating calls to Ivy instructions. In particular, each
  22291. // instruction type is represented as a function, which may select a specific instruction variant
  22292. // depending on the exact arguments.
  22293. function element(slot, tag, constIndex, localRefIndex, sourceSpan) {
  22294. return elementOrContainerBase(Identifiers.element, slot, tag, constIndex, localRefIndex, sourceSpan);
  22295. }
  22296. function elementStart(slot, tag, constIndex, localRefIndex, sourceSpan) {
  22297. return elementOrContainerBase(Identifiers.elementStart, slot, tag, constIndex, localRefIndex, sourceSpan);
  22298. }
  22299. function elementOrContainerBase(instruction, slot, tag, constIndex, localRefIndex, sourceSpan) {
  22300. const args = [literal(slot)];
  22301. if (tag !== null) {
  22302. args.push(literal(tag));
  22303. }
  22304. if (localRefIndex !== null) {
  22305. args.push(literal(constIndex), // might be null, but that's okay.
  22306. literal(localRefIndex));
  22307. }
  22308. else if (constIndex !== null) {
  22309. args.push(literal(constIndex));
  22310. }
  22311. return call(instruction, args, sourceSpan);
  22312. }
  22313. function elementEnd(sourceSpan) {
  22314. return call(Identifiers.elementEnd, [], sourceSpan);
  22315. }
  22316. function elementContainerStart(slot, constIndex, localRefIndex, sourceSpan) {
  22317. return elementOrContainerBase(Identifiers.elementContainerStart, slot,
  22318. /* tag */ null, constIndex, localRefIndex, sourceSpan);
  22319. }
  22320. function elementContainer(slot, constIndex, localRefIndex, sourceSpan) {
  22321. return elementOrContainerBase(Identifiers.elementContainer, slot,
  22322. /* tag */ null, constIndex, localRefIndex, sourceSpan);
  22323. }
  22324. function elementContainerEnd() {
  22325. return call(Identifiers.elementContainerEnd, [], null);
  22326. }
  22327. function template(slot, templateFnRef, decls, vars, tag, constIndex, localRefs, sourceSpan) {
  22328. const args = [
  22329. literal(slot),
  22330. templateFnRef,
  22331. literal(decls),
  22332. literal(vars),
  22333. literal(tag),
  22334. literal(constIndex),
  22335. ];
  22336. if (localRefs !== null) {
  22337. args.push(literal(localRefs));
  22338. args.push(importExpr(Identifiers.templateRefExtractor));
  22339. }
  22340. while (args[args.length - 1].isEquivalent(NULL_EXPR)) {
  22341. args.pop();
  22342. }
  22343. return call(Identifiers.templateCreate, args, sourceSpan);
  22344. }
  22345. function disableBindings() {
  22346. return call(Identifiers.disableBindings, [], null);
  22347. }
  22348. function enableBindings() {
  22349. return call(Identifiers.enableBindings, [], null);
  22350. }
  22351. function listener(name, handlerFn, eventTargetResolver, syntheticHost, sourceSpan) {
  22352. const args = [literal(name), handlerFn];
  22353. if (eventTargetResolver !== null) {
  22354. args.push(literal(false)); // `useCapture` flag, defaults to `false`
  22355. args.push(importExpr(eventTargetResolver));
  22356. }
  22357. return call(syntheticHost ? Identifiers.syntheticHostListener : Identifiers.listener, args, sourceSpan);
  22358. }
  22359. function twoWayBindingSet(target, value) {
  22360. return importExpr(Identifiers.twoWayBindingSet).callFn([target, value]);
  22361. }
  22362. function twoWayListener(name, handlerFn, sourceSpan) {
  22363. return call(Identifiers.twoWayListener, [literal(name), handlerFn], sourceSpan);
  22364. }
  22365. function pipe(slot, name) {
  22366. return call(Identifiers.pipe, [literal(slot), literal(name)], null);
  22367. }
  22368. function namespaceHTML() {
  22369. return call(Identifiers.namespaceHTML, [], null);
  22370. }
  22371. function namespaceSVG() {
  22372. return call(Identifiers.namespaceSVG, [], null);
  22373. }
  22374. function namespaceMath() {
  22375. return call(Identifiers.namespaceMathML, [], null);
  22376. }
  22377. function advance(delta, sourceSpan) {
  22378. return call(Identifiers.advance, delta > 1 ? [literal(delta)] : [], sourceSpan);
  22379. }
  22380. function reference(slot) {
  22381. return importExpr(Identifiers.reference).callFn([literal(slot)]);
  22382. }
  22383. function nextContext(steps) {
  22384. return importExpr(Identifiers.nextContext).callFn(steps === 1 ? [] : [literal(steps)]);
  22385. }
  22386. function getCurrentView() {
  22387. return importExpr(Identifiers.getCurrentView).callFn([]);
  22388. }
  22389. function restoreView(savedView) {
  22390. return importExpr(Identifiers.restoreView).callFn([savedView]);
  22391. }
  22392. function resetView(returnValue) {
  22393. return importExpr(Identifiers.resetView).callFn([returnValue]);
  22394. }
  22395. function text(slot, initialValue, sourceSpan) {
  22396. const args = [literal(slot, null)];
  22397. if (initialValue !== '') {
  22398. args.push(literal(initialValue));
  22399. }
  22400. return call(Identifiers.text, args, sourceSpan);
  22401. }
  22402. function defer(selfSlot, primarySlot, dependencyResolverFn, loadingSlot, placeholderSlot, errorSlot, loadingConfig, placeholderConfig, enableTimerScheduling, sourceSpan, flags) {
  22403. const args = [
  22404. literal(selfSlot),
  22405. literal(primarySlot),
  22406. dependencyResolverFn ?? literal(null),
  22407. literal(loadingSlot),
  22408. literal(placeholderSlot),
  22409. literal(errorSlot),
  22410. loadingConfig ?? literal(null),
  22411. placeholderConfig ?? literal(null),
  22412. enableTimerScheduling ? importExpr(Identifiers.deferEnableTimerScheduling) : literal(null),
  22413. literal(flags),
  22414. ];
  22415. let expr;
  22416. while ((expr = args[args.length - 1]) !== null &&
  22417. expr instanceof LiteralExpr &&
  22418. expr.value === null) {
  22419. args.pop();
  22420. }
  22421. return call(Identifiers.defer, args, sourceSpan);
  22422. }
  22423. const deferTriggerToR3TriggerInstructionsMap = new Map([
  22424. [
  22425. DeferTriggerKind.Idle,
  22426. {
  22427. ["none" /* ir.DeferOpModifierKind.NONE */]: Identifiers.deferOnIdle,
  22428. ["prefetch" /* ir.DeferOpModifierKind.PREFETCH */]: Identifiers.deferPrefetchOnIdle,
  22429. ["hydrate" /* ir.DeferOpModifierKind.HYDRATE */]: Identifiers.deferHydrateOnIdle,
  22430. },
  22431. ],
  22432. [
  22433. DeferTriggerKind.Immediate,
  22434. {
  22435. ["none" /* ir.DeferOpModifierKind.NONE */]: Identifiers.deferOnImmediate,
  22436. ["prefetch" /* ir.DeferOpModifierKind.PREFETCH */]: Identifiers.deferPrefetchOnImmediate,
  22437. ["hydrate" /* ir.DeferOpModifierKind.HYDRATE */]: Identifiers.deferHydrateOnImmediate,
  22438. },
  22439. ],
  22440. [
  22441. DeferTriggerKind.Timer,
  22442. {
  22443. ["none" /* ir.DeferOpModifierKind.NONE */]: Identifiers.deferOnTimer,
  22444. ["prefetch" /* ir.DeferOpModifierKind.PREFETCH */]: Identifiers.deferPrefetchOnTimer,
  22445. ["hydrate" /* ir.DeferOpModifierKind.HYDRATE */]: Identifiers.deferHydrateOnTimer,
  22446. },
  22447. ],
  22448. [
  22449. DeferTriggerKind.Hover,
  22450. {
  22451. ["none" /* ir.DeferOpModifierKind.NONE */]: Identifiers.deferOnHover,
  22452. ["prefetch" /* ir.DeferOpModifierKind.PREFETCH */]: Identifiers.deferPrefetchOnHover,
  22453. ["hydrate" /* ir.DeferOpModifierKind.HYDRATE */]: Identifiers.deferHydrateOnHover,
  22454. },
  22455. ],
  22456. [
  22457. DeferTriggerKind.Interaction,
  22458. {
  22459. ["none" /* ir.DeferOpModifierKind.NONE */]: Identifiers.deferOnInteraction,
  22460. ["prefetch" /* ir.DeferOpModifierKind.PREFETCH */]: Identifiers.deferPrefetchOnInteraction,
  22461. ["hydrate" /* ir.DeferOpModifierKind.HYDRATE */]: Identifiers.deferHydrateOnInteraction,
  22462. },
  22463. ],
  22464. [
  22465. DeferTriggerKind.Viewport,
  22466. {
  22467. ["none" /* ir.DeferOpModifierKind.NONE */]: Identifiers.deferOnViewport,
  22468. ["prefetch" /* ir.DeferOpModifierKind.PREFETCH */]: Identifiers.deferPrefetchOnViewport,
  22469. ["hydrate" /* ir.DeferOpModifierKind.HYDRATE */]: Identifiers.deferHydrateOnViewport,
  22470. },
  22471. ],
  22472. [
  22473. DeferTriggerKind.Never,
  22474. {
  22475. ["none" /* ir.DeferOpModifierKind.NONE */]: Identifiers.deferHydrateNever,
  22476. ["prefetch" /* ir.DeferOpModifierKind.PREFETCH */]: Identifiers.deferHydrateNever,
  22477. ["hydrate" /* ir.DeferOpModifierKind.HYDRATE */]: Identifiers.deferHydrateNever,
  22478. },
  22479. ],
  22480. ]);
  22481. function deferOn(trigger, args, modifier, sourceSpan) {
  22482. const instructionToCall = deferTriggerToR3TriggerInstructionsMap.get(trigger)?.[modifier];
  22483. if (instructionToCall === undefined) {
  22484. throw new Error(`Unable to determine instruction for trigger ${trigger}`);
  22485. }
  22486. return call(instructionToCall, args.map((a) => literal(a)), sourceSpan);
  22487. }
  22488. function projectionDef(def) {
  22489. return call(Identifiers.projectionDef, def ? [def] : [], null);
  22490. }
  22491. function projection(slot, projectionSlotIndex, attributes, fallbackFnName, fallbackDecls, fallbackVars, sourceSpan) {
  22492. const args = [literal(slot)];
  22493. if (projectionSlotIndex !== 0 || attributes !== null || fallbackFnName !== null) {
  22494. args.push(literal(projectionSlotIndex));
  22495. if (attributes !== null) {
  22496. args.push(attributes);
  22497. }
  22498. if (fallbackFnName !== null) {
  22499. if (attributes === null) {
  22500. args.push(literal(null));
  22501. }
  22502. args.push(variable(fallbackFnName), literal(fallbackDecls), literal(fallbackVars));
  22503. }
  22504. }
  22505. return call(Identifiers.projection, args, sourceSpan);
  22506. }
  22507. function i18nStart(slot, constIndex, subTemplateIndex, sourceSpan) {
  22508. const args = [literal(slot), literal(constIndex)];
  22509. if (subTemplateIndex !== null) {
  22510. args.push(literal(subTemplateIndex));
  22511. }
  22512. return call(Identifiers.i18nStart, args, sourceSpan);
  22513. }
  22514. function repeaterCreate(slot, viewFnName, decls, vars, tag, constIndex, trackByFn, trackByUsesComponentInstance, emptyViewFnName, emptyDecls, emptyVars, emptyTag, emptyConstIndex, sourceSpan) {
  22515. const args = [
  22516. literal(slot),
  22517. variable(viewFnName),
  22518. literal(decls),
  22519. literal(vars),
  22520. literal(tag),
  22521. literal(constIndex),
  22522. trackByFn,
  22523. ];
  22524. if (trackByUsesComponentInstance || emptyViewFnName !== null) {
  22525. args.push(literal(trackByUsesComponentInstance));
  22526. if (emptyViewFnName !== null) {
  22527. args.push(variable(emptyViewFnName), literal(emptyDecls), literal(emptyVars));
  22528. if (emptyTag !== null || emptyConstIndex !== null) {
  22529. args.push(literal(emptyTag));
  22530. }
  22531. if (emptyConstIndex !== null) {
  22532. args.push(literal(emptyConstIndex));
  22533. }
  22534. }
  22535. }
  22536. return call(Identifiers.repeaterCreate, args, sourceSpan);
  22537. }
  22538. function repeater(collection, sourceSpan) {
  22539. return call(Identifiers.repeater, [collection], sourceSpan);
  22540. }
  22541. function deferWhen(modifier, expr, sourceSpan) {
  22542. if (modifier === "prefetch" /* ir.DeferOpModifierKind.PREFETCH */) {
  22543. return call(Identifiers.deferPrefetchWhen, [expr], sourceSpan);
  22544. }
  22545. else if (modifier === "hydrate" /* ir.DeferOpModifierKind.HYDRATE */) {
  22546. return call(Identifiers.deferHydrateWhen, [expr], sourceSpan);
  22547. }
  22548. return call(Identifiers.deferWhen, [expr], sourceSpan);
  22549. }
  22550. function declareLet(slot, sourceSpan) {
  22551. return call(Identifiers.declareLet, [literal(slot)], sourceSpan);
  22552. }
  22553. function storeLet(value, sourceSpan) {
  22554. return importExpr(Identifiers.storeLet).callFn([value], sourceSpan);
  22555. }
  22556. function readContextLet(slot) {
  22557. return importExpr(Identifiers.readContextLet).callFn([literal(slot)]);
  22558. }
  22559. function i18n(slot, constIndex, subTemplateIndex, sourceSpan) {
  22560. const args = [literal(slot), literal(constIndex)];
  22561. if (subTemplateIndex) {
  22562. args.push(literal(subTemplateIndex));
  22563. }
  22564. return call(Identifiers.i18n, args, sourceSpan);
  22565. }
  22566. function i18nEnd(endSourceSpan) {
  22567. return call(Identifiers.i18nEnd, [], endSourceSpan);
  22568. }
  22569. function i18nAttributes(slot, i18nAttributesConfig) {
  22570. const args = [literal(slot), literal(i18nAttributesConfig)];
  22571. return call(Identifiers.i18nAttributes, args, null);
  22572. }
  22573. function property(name, expression, sanitizer, sourceSpan) {
  22574. const args = [literal(name), expression];
  22575. if (sanitizer !== null) {
  22576. args.push(sanitizer);
  22577. }
  22578. return call(Identifiers.property, args, sourceSpan);
  22579. }
  22580. function twoWayProperty(name, expression, sanitizer, sourceSpan) {
  22581. const args = [literal(name), expression];
  22582. if (sanitizer !== null) {
  22583. args.push(sanitizer);
  22584. }
  22585. return call(Identifiers.twoWayProperty, args, sourceSpan);
  22586. }
  22587. function attribute(name, expression, sanitizer, namespace) {
  22588. const args = [literal(name), expression];
  22589. if (sanitizer !== null || namespace !== null) {
  22590. args.push(sanitizer ?? literal(null));
  22591. }
  22592. if (namespace !== null) {
  22593. args.push(literal(namespace));
  22594. }
  22595. return call(Identifiers.attribute, args, null);
  22596. }
  22597. function styleProp(name, expression, unit, sourceSpan) {
  22598. const args = [literal(name), expression];
  22599. if (unit !== null) {
  22600. args.push(literal(unit));
  22601. }
  22602. return call(Identifiers.styleProp, args, sourceSpan);
  22603. }
  22604. function classProp(name, expression, sourceSpan) {
  22605. return call(Identifiers.classProp, [literal(name), expression], sourceSpan);
  22606. }
  22607. function styleMap(expression, sourceSpan) {
  22608. return call(Identifiers.styleMap, [expression], sourceSpan);
  22609. }
  22610. function classMap(expression, sourceSpan) {
  22611. return call(Identifiers.classMap, [expression], sourceSpan);
  22612. }
  22613. const PIPE_BINDINGS = [
  22614. Identifiers.pipeBind1,
  22615. Identifiers.pipeBind2,
  22616. Identifiers.pipeBind3,
  22617. Identifiers.pipeBind4,
  22618. ];
  22619. function pipeBind(slot, varOffset, args) {
  22620. if (args.length < 1 || args.length > PIPE_BINDINGS.length) {
  22621. throw new Error(`pipeBind() argument count out of bounds`);
  22622. }
  22623. const instruction = PIPE_BINDINGS[args.length - 1];
  22624. return importExpr(instruction).callFn([literal(slot), literal(varOffset), ...args]);
  22625. }
  22626. function pipeBindV(slot, varOffset, args) {
  22627. return importExpr(Identifiers.pipeBindV).callFn([literal(slot), literal(varOffset), args]);
  22628. }
  22629. function textInterpolate(strings, expressions, sourceSpan) {
  22630. const interpolationArgs = collateInterpolationArgs(strings, expressions);
  22631. return callVariadicInstruction(TEXT_INTERPOLATE_CONFIG, [], interpolationArgs, [], sourceSpan);
  22632. }
  22633. function i18nExp(expr, sourceSpan) {
  22634. return call(Identifiers.i18nExp, [expr], sourceSpan);
  22635. }
  22636. function i18nApply(slot, sourceSpan) {
  22637. return call(Identifiers.i18nApply, [literal(slot)], sourceSpan);
  22638. }
  22639. function propertyInterpolate(name, strings, expressions, sanitizer, sourceSpan) {
  22640. const interpolationArgs = collateInterpolationArgs(strings, expressions);
  22641. const extraArgs = [];
  22642. if (sanitizer !== null) {
  22643. extraArgs.push(sanitizer);
  22644. }
  22645. return callVariadicInstruction(PROPERTY_INTERPOLATE_CONFIG, [literal(name)], interpolationArgs, extraArgs, sourceSpan);
  22646. }
  22647. function attributeInterpolate(name, strings, expressions, sanitizer, sourceSpan) {
  22648. const interpolationArgs = collateInterpolationArgs(strings, expressions);
  22649. const extraArgs = [];
  22650. if (sanitizer !== null) {
  22651. extraArgs.push(sanitizer);
  22652. }
  22653. return callVariadicInstruction(ATTRIBUTE_INTERPOLATE_CONFIG, [literal(name)], interpolationArgs, extraArgs, sourceSpan);
  22654. }
  22655. function stylePropInterpolate(name, strings, expressions, unit, sourceSpan) {
  22656. const interpolationArgs = collateInterpolationArgs(strings, expressions);
  22657. const extraArgs = [];
  22658. if (unit !== null) {
  22659. extraArgs.push(literal(unit));
  22660. }
  22661. return callVariadicInstruction(STYLE_PROP_INTERPOLATE_CONFIG, [literal(name)], interpolationArgs, extraArgs, sourceSpan);
  22662. }
  22663. function styleMapInterpolate(strings, expressions, sourceSpan) {
  22664. const interpolationArgs = collateInterpolationArgs(strings, expressions);
  22665. return callVariadicInstruction(STYLE_MAP_INTERPOLATE_CONFIG, [], interpolationArgs, [], sourceSpan);
  22666. }
  22667. function classMapInterpolate(strings, expressions, sourceSpan) {
  22668. const interpolationArgs = collateInterpolationArgs(strings, expressions);
  22669. return callVariadicInstruction(CLASS_MAP_INTERPOLATE_CONFIG, [], interpolationArgs, [], sourceSpan);
  22670. }
  22671. function hostProperty(name, expression, sanitizer, sourceSpan) {
  22672. const args = [literal(name), expression];
  22673. if (sanitizer !== null) {
  22674. args.push(sanitizer);
  22675. }
  22676. return call(Identifiers.hostProperty, args, sourceSpan);
  22677. }
  22678. function syntheticHostProperty(name, expression, sourceSpan) {
  22679. return call(Identifiers.syntheticHostProperty, [literal(name), expression], sourceSpan);
  22680. }
  22681. function pureFunction(varOffset, fn, args) {
  22682. return callVariadicInstructionExpr(PURE_FUNCTION_CONFIG, [literal(varOffset), fn], args, [], null);
  22683. }
  22684. function attachSourceLocation(templatePath, locations) {
  22685. return call(Identifiers.attachSourceLocations, [literal(templatePath), locations], null);
  22686. }
  22687. /**
  22688. * Collates the string an expression arguments for an interpolation instruction.
  22689. */
  22690. function collateInterpolationArgs(strings, expressions) {
  22691. if (strings.length < 1 || expressions.length !== strings.length - 1) {
  22692. throw new Error(`AssertionError: expected specific shape of args for strings/expressions in interpolation`);
  22693. }
  22694. const interpolationArgs = [];
  22695. if (expressions.length === 1 && strings[0] === '' && strings[1] === '') {
  22696. interpolationArgs.push(expressions[0]);
  22697. }
  22698. else {
  22699. let idx;
  22700. for (idx = 0; idx < expressions.length; idx++) {
  22701. interpolationArgs.push(literal(strings[idx]), expressions[idx]);
  22702. }
  22703. // idx points at the last string.
  22704. interpolationArgs.push(literal(strings[idx]));
  22705. }
  22706. return interpolationArgs;
  22707. }
  22708. function call(instruction, args, sourceSpan) {
  22709. const expr = importExpr(instruction).callFn(args, sourceSpan);
  22710. return createStatementOp(new ExpressionStatement(expr, sourceSpan));
  22711. }
  22712. function conditional(condition, contextValue, sourceSpan) {
  22713. const args = [condition];
  22714. if (contextValue !== null) {
  22715. args.push(contextValue);
  22716. }
  22717. return call(Identifiers.conditional, args, sourceSpan);
  22718. }
  22719. /**
  22720. * `InterpolationConfig` for the `textInterpolate` instruction.
  22721. */
  22722. const TEXT_INTERPOLATE_CONFIG = {
  22723. constant: [
  22724. Identifiers.textInterpolate,
  22725. Identifiers.textInterpolate1,
  22726. Identifiers.textInterpolate2,
  22727. Identifiers.textInterpolate3,
  22728. Identifiers.textInterpolate4,
  22729. Identifiers.textInterpolate5,
  22730. Identifiers.textInterpolate6,
  22731. Identifiers.textInterpolate7,
  22732. Identifiers.textInterpolate8,
  22733. ],
  22734. variable: Identifiers.textInterpolateV,
  22735. mapping: (n) => {
  22736. if (n % 2 === 0) {
  22737. throw new Error(`Expected odd number of arguments`);
  22738. }
  22739. return (n - 1) / 2;
  22740. },
  22741. };
  22742. /**
  22743. * `InterpolationConfig` for the `propertyInterpolate` instruction.
  22744. */
  22745. const PROPERTY_INTERPOLATE_CONFIG = {
  22746. constant: [
  22747. Identifiers.propertyInterpolate,
  22748. Identifiers.propertyInterpolate1,
  22749. Identifiers.propertyInterpolate2,
  22750. Identifiers.propertyInterpolate3,
  22751. Identifiers.propertyInterpolate4,
  22752. Identifiers.propertyInterpolate5,
  22753. Identifiers.propertyInterpolate6,
  22754. Identifiers.propertyInterpolate7,
  22755. Identifiers.propertyInterpolate8,
  22756. ],
  22757. variable: Identifiers.propertyInterpolateV,
  22758. mapping: (n) => {
  22759. if (n % 2 === 0) {
  22760. throw new Error(`Expected odd number of arguments`);
  22761. }
  22762. return (n - 1) / 2;
  22763. },
  22764. };
  22765. /**
  22766. * `InterpolationConfig` for the `stylePropInterpolate` instruction.
  22767. */
  22768. const STYLE_PROP_INTERPOLATE_CONFIG = {
  22769. constant: [
  22770. Identifiers.styleProp,
  22771. Identifiers.stylePropInterpolate1,
  22772. Identifiers.stylePropInterpolate2,
  22773. Identifiers.stylePropInterpolate3,
  22774. Identifiers.stylePropInterpolate4,
  22775. Identifiers.stylePropInterpolate5,
  22776. Identifiers.stylePropInterpolate6,
  22777. Identifiers.stylePropInterpolate7,
  22778. Identifiers.stylePropInterpolate8,
  22779. ],
  22780. variable: Identifiers.stylePropInterpolateV,
  22781. mapping: (n) => {
  22782. if (n % 2 === 0) {
  22783. throw new Error(`Expected odd number of arguments`);
  22784. }
  22785. return (n - 1) / 2;
  22786. },
  22787. };
  22788. /**
  22789. * `InterpolationConfig` for the `attributeInterpolate` instruction.
  22790. */
  22791. const ATTRIBUTE_INTERPOLATE_CONFIG = {
  22792. constant: [
  22793. Identifiers.attribute,
  22794. Identifiers.attributeInterpolate1,
  22795. Identifiers.attributeInterpolate2,
  22796. Identifiers.attributeInterpolate3,
  22797. Identifiers.attributeInterpolate4,
  22798. Identifiers.attributeInterpolate5,
  22799. Identifiers.attributeInterpolate6,
  22800. Identifiers.attributeInterpolate7,
  22801. Identifiers.attributeInterpolate8,
  22802. ],
  22803. variable: Identifiers.attributeInterpolateV,
  22804. mapping: (n) => {
  22805. if (n % 2 === 0) {
  22806. throw new Error(`Expected odd number of arguments`);
  22807. }
  22808. return (n - 1) / 2;
  22809. },
  22810. };
  22811. /**
  22812. * `InterpolationConfig` for the `styleMapInterpolate` instruction.
  22813. */
  22814. const STYLE_MAP_INTERPOLATE_CONFIG = {
  22815. constant: [
  22816. Identifiers.styleMap,
  22817. Identifiers.styleMapInterpolate1,
  22818. Identifiers.styleMapInterpolate2,
  22819. Identifiers.styleMapInterpolate3,
  22820. Identifiers.styleMapInterpolate4,
  22821. Identifiers.styleMapInterpolate5,
  22822. Identifiers.styleMapInterpolate6,
  22823. Identifiers.styleMapInterpolate7,
  22824. Identifiers.styleMapInterpolate8,
  22825. ],
  22826. variable: Identifiers.styleMapInterpolateV,
  22827. mapping: (n) => {
  22828. if (n % 2 === 0) {
  22829. throw new Error(`Expected odd number of arguments`);
  22830. }
  22831. return (n - 1) / 2;
  22832. },
  22833. };
  22834. /**
  22835. * `InterpolationConfig` for the `classMapInterpolate` instruction.
  22836. */
  22837. const CLASS_MAP_INTERPOLATE_CONFIG = {
  22838. constant: [
  22839. Identifiers.classMap,
  22840. Identifiers.classMapInterpolate1,
  22841. Identifiers.classMapInterpolate2,
  22842. Identifiers.classMapInterpolate3,
  22843. Identifiers.classMapInterpolate4,
  22844. Identifiers.classMapInterpolate5,
  22845. Identifiers.classMapInterpolate6,
  22846. Identifiers.classMapInterpolate7,
  22847. Identifiers.classMapInterpolate8,
  22848. ],
  22849. variable: Identifiers.classMapInterpolateV,
  22850. mapping: (n) => {
  22851. if (n % 2 === 0) {
  22852. throw new Error(`Expected odd number of arguments`);
  22853. }
  22854. return (n - 1) / 2;
  22855. },
  22856. };
  22857. const PURE_FUNCTION_CONFIG = {
  22858. constant: [
  22859. Identifiers.pureFunction0,
  22860. Identifiers.pureFunction1,
  22861. Identifiers.pureFunction2,
  22862. Identifiers.pureFunction3,
  22863. Identifiers.pureFunction4,
  22864. Identifiers.pureFunction5,
  22865. Identifiers.pureFunction6,
  22866. Identifiers.pureFunction7,
  22867. Identifiers.pureFunction8,
  22868. ],
  22869. variable: Identifiers.pureFunctionV,
  22870. mapping: (n) => n,
  22871. };
  22872. function callVariadicInstructionExpr(config, baseArgs, interpolationArgs, extraArgs, sourceSpan) {
  22873. const n = config.mapping(interpolationArgs.length);
  22874. if (n < config.constant.length) {
  22875. // Constant calling pattern.
  22876. return importExpr(config.constant[n])
  22877. .callFn([...baseArgs, ...interpolationArgs, ...extraArgs], sourceSpan);
  22878. }
  22879. else if (config.variable !== null) {
  22880. // Variable calling pattern.
  22881. return importExpr(config.variable)
  22882. .callFn([...baseArgs, literalArr(interpolationArgs), ...extraArgs], sourceSpan);
  22883. }
  22884. else {
  22885. throw new Error(`AssertionError: unable to call variadic function`);
  22886. }
  22887. }
  22888. function callVariadicInstruction(config, baseArgs, interpolationArgs, extraArgs, sourceSpan) {
  22889. return createStatementOp(callVariadicInstructionExpr(config, baseArgs, interpolationArgs, extraArgs, sourceSpan).toStmt());
  22890. }
  22891. /**
  22892. * Map of target resolvers for event listeners.
  22893. */
  22894. const GLOBAL_TARGET_RESOLVERS = new Map([
  22895. ['window', Identifiers.resolveWindow],
  22896. ['document', Identifiers.resolveDocument],
  22897. ['body', Identifiers.resolveBody],
  22898. ]);
  22899. /**
  22900. * Compiles semantic operations across all views and generates output `o.Statement`s with actual
  22901. * runtime calls in their place.
  22902. *
  22903. * Reification replaces semantic operations with selected Ivy instructions and other generated code
  22904. * structures. After reification, the create/update operation lists of all views should only contain
  22905. * `ir.StatementOp`s (which wrap generated `o.Statement`s).
  22906. */
  22907. function reify(job) {
  22908. for (const unit of job.units) {
  22909. reifyCreateOperations(unit, unit.create);
  22910. reifyUpdateOperations(unit, unit.update);
  22911. }
  22912. }
  22913. function reifyCreateOperations(unit, ops) {
  22914. for (const op of ops) {
  22915. transformExpressionsInOp(op, reifyIrExpression, VisitorContextFlag.None);
  22916. switch (op.kind) {
  22917. case OpKind.Text:
  22918. OpList.replace(op, text(op.handle.slot, op.initialValue, op.sourceSpan));
  22919. break;
  22920. case OpKind.ElementStart:
  22921. OpList.replace(op, elementStart(op.handle.slot, op.tag, op.attributes, op.localRefs, op.startSourceSpan));
  22922. break;
  22923. case OpKind.Element:
  22924. OpList.replace(op, element(op.handle.slot, op.tag, op.attributes, op.localRefs, op.wholeSourceSpan));
  22925. break;
  22926. case OpKind.ElementEnd:
  22927. OpList.replace(op, elementEnd(op.sourceSpan));
  22928. break;
  22929. case OpKind.ContainerStart:
  22930. OpList.replace(op, elementContainerStart(op.handle.slot, op.attributes, op.localRefs, op.startSourceSpan));
  22931. break;
  22932. case OpKind.Container:
  22933. OpList.replace(op, elementContainer(op.handle.slot, op.attributes, op.localRefs, op.wholeSourceSpan));
  22934. break;
  22935. case OpKind.ContainerEnd:
  22936. OpList.replace(op, elementContainerEnd());
  22937. break;
  22938. case OpKind.I18nStart:
  22939. OpList.replace(op, i18nStart(op.handle.slot, op.messageIndex, op.subTemplateIndex, op.sourceSpan));
  22940. break;
  22941. case OpKind.I18nEnd:
  22942. OpList.replace(op, i18nEnd(op.sourceSpan));
  22943. break;
  22944. case OpKind.I18n:
  22945. OpList.replace(op, i18n(op.handle.slot, op.messageIndex, op.subTemplateIndex, op.sourceSpan));
  22946. break;
  22947. case OpKind.I18nAttributes:
  22948. if (op.i18nAttributesConfig === null) {
  22949. throw new Error(`AssertionError: i18nAttributesConfig was not set`);
  22950. }
  22951. OpList.replace(op, i18nAttributes(op.handle.slot, op.i18nAttributesConfig));
  22952. break;
  22953. case OpKind.Template:
  22954. if (!(unit instanceof ViewCompilationUnit)) {
  22955. throw new Error(`AssertionError: must be compiling a component`);
  22956. }
  22957. if (Array.isArray(op.localRefs)) {
  22958. throw new Error(`AssertionError: local refs array should have been extracted into a constant`);
  22959. }
  22960. const childView = unit.job.views.get(op.xref);
  22961. OpList.replace(op, template(op.handle.slot, variable(childView.fnName), childView.decls, childView.vars, op.tag, op.attributes, op.localRefs, op.startSourceSpan));
  22962. break;
  22963. case OpKind.DisableBindings:
  22964. OpList.replace(op, disableBindings());
  22965. break;
  22966. case OpKind.EnableBindings:
  22967. OpList.replace(op, enableBindings());
  22968. break;
  22969. case OpKind.Pipe:
  22970. OpList.replace(op, pipe(op.handle.slot, op.name));
  22971. break;
  22972. case OpKind.DeclareLet:
  22973. OpList.replace(op, declareLet(op.handle.slot, op.sourceSpan));
  22974. break;
  22975. case OpKind.Listener:
  22976. const listenerFn = reifyListenerHandler(unit, op.handlerFnName, op.handlerOps, op.consumesDollarEvent);
  22977. const eventTargetResolver = op.eventTarget
  22978. ? GLOBAL_TARGET_RESOLVERS.get(op.eventTarget)
  22979. : null;
  22980. if (eventTargetResolver === undefined) {
  22981. throw new Error(`Unexpected global target '${op.eventTarget}' defined for '${op.name}' event. Supported list of global targets: window,document,body.`);
  22982. }
  22983. OpList.replace(op, listener(op.name, listenerFn, eventTargetResolver, op.hostListener && op.isAnimationListener, op.sourceSpan));
  22984. break;
  22985. case OpKind.TwoWayListener:
  22986. OpList.replace(op, twoWayListener(op.name, reifyListenerHandler(unit, op.handlerFnName, op.handlerOps, true), op.sourceSpan));
  22987. break;
  22988. case OpKind.Variable:
  22989. if (op.variable.name === null) {
  22990. throw new Error(`AssertionError: unnamed variable ${op.xref}`);
  22991. }
  22992. OpList.replace(op, createStatementOp(new DeclareVarStmt(op.variable.name, op.initializer, undefined, StmtModifier.Final)));
  22993. break;
  22994. case OpKind.Namespace:
  22995. switch (op.active) {
  22996. case Namespace.HTML:
  22997. OpList.replace(op, namespaceHTML());
  22998. break;
  22999. case Namespace.SVG:
  23000. OpList.replace(op, namespaceSVG());
  23001. break;
  23002. case Namespace.Math:
  23003. OpList.replace(op, namespaceMath());
  23004. break;
  23005. }
  23006. break;
  23007. case OpKind.Defer:
  23008. const timerScheduling = !!op.loadingMinimumTime || !!op.loadingAfterTime || !!op.placeholderMinimumTime;
  23009. OpList.replace(op, defer(op.handle.slot, op.mainSlot.slot, op.resolverFn, op.loadingSlot?.slot ?? null, op.placeholderSlot?.slot ?? null, op.errorSlot?.slot ?? null, op.loadingConfig, op.placeholderConfig, timerScheduling, op.sourceSpan, op.flags));
  23010. break;
  23011. case OpKind.DeferOn:
  23012. let args = [];
  23013. switch (op.trigger.kind) {
  23014. case DeferTriggerKind.Never:
  23015. case DeferTriggerKind.Idle:
  23016. case DeferTriggerKind.Immediate:
  23017. break;
  23018. case DeferTriggerKind.Timer:
  23019. args = [op.trigger.delay];
  23020. break;
  23021. case DeferTriggerKind.Interaction:
  23022. case DeferTriggerKind.Hover:
  23023. case DeferTriggerKind.Viewport:
  23024. // `hydrate` triggers don't support targets.
  23025. if (op.modifier === "hydrate" /* ir.DeferOpModifierKind.HYDRATE */) {
  23026. args = [];
  23027. }
  23028. else {
  23029. if (op.trigger.targetSlot?.slot == null || op.trigger.targetSlotViewSteps === null) {
  23030. throw new Error(`Slot or view steps not set in trigger reification for trigger kind ${op.trigger.kind}`);
  23031. }
  23032. args = [op.trigger.targetSlot.slot];
  23033. if (op.trigger.targetSlotViewSteps !== 0) {
  23034. args.push(op.trigger.targetSlotViewSteps);
  23035. }
  23036. }
  23037. break;
  23038. default:
  23039. throw new Error(`AssertionError: Unsupported reification of defer trigger kind ${op.trigger.kind}`);
  23040. }
  23041. OpList.replace(op, deferOn(op.trigger.kind, args, op.modifier, op.sourceSpan));
  23042. break;
  23043. case OpKind.ProjectionDef:
  23044. OpList.replace(op, projectionDef(op.def));
  23045. break;
  23046. case OpKind.Projection:
  23047. if (op.handle.slot === null) {
  23048. throw new Error('No slot was assigned for project instruction');
  23049. }
  23050. let fallbackViewFnName = null;
  23051. let fallbackDecls = null;
  23052. let fallbackVars = null;
  23053. if (op.fallbackView !== null) {
  23054. if (!(unit instanceof ViewCompilationUnit)) {
  23055. throw new Error(`AssertionError: must be compiling a component`);
  23056. }
  23057. const fallbackView = unit.job.views.get(op.fallbackView);
  23058. if (fallbackView === undefined) {
  23059. throw new Error('AssertionError: projection had fallback view xref, but fallback view was not found');
  23060. }
  23061. if (fallbackView.fnName === null ||
  23062. fallbackView.decls === null ||
  23063. fallbackView.vars === null) {
  23064. throw new Error(`AssertionError: expected projection fallback view to have been named and counted`);
  23065. }
  23066. fallbackViewFnName = fallbackView.fnName;
  23067. fallbackDecls = fallbackView.decls;
  23068. fallbackVars = fallbackView.vars;
  23069. }
  23070. OpList.replace(op, projection(op.handle.slot, op.projectionSlotIndex, op.attributes, fallbackViewFnName, fallbackDecls, fallbackVars, op.sourceSpan));
  23071. break;
  23072. case OpKind.RepeaterCreate:
  23073. if (op.handle.slot === null) {
  23074. throw new Error('No slot was assigned for repeater instruction');
  23075. }
  23076. if (!(unit instanceof ViewCompilationUnit)) {
  23077. throw new Error(`AssertionError: must be compiling a component`);
  23078. }
  23079. const repeaterView = unit.job.views.get(op.xref);
  23080. if (repeaterView.fnName === null) {
  23081. throw new Error(`AssertionError: expected repeater primary view to have been named`);
  23082. }
  23083. let emptyViewFnName = null;
  23084. let emptyDecls = null;
  23085. let emptyVars = null;
  23086. if (op.emptyView !== null) {
  23087. const emptyView = unit.job.views.get(op.emptyView);
  23088. if (emptyView === undefined) {
  23089. throw new Error('AssertionError: repeater had empty view xref, but empty view was not found');
  23090. }
  23091. if (emptyView.fnName === null || emptyView.decls === null || emptyView.vars === null) {
  23092. throw new Error(`AssertionError: expected repeater empty view to have been named and counted`);
  23093. }
  23094. emptyViewFnName = emptyView.fnName;
  23095. emptyDecls = emptyView.decls;
  23096. emptyVars = emptyView.vars;
  23097. }
  23098. OpList.replace(op, repeaterCreate(op.handle.slot, repeaterView.fnName, op.decls, op.vars, op.tag, op.attributes, reifyTrackBy(unit, op), op.usesComponentInstance, emptyViewFnName, emptyDecls, emptyVars, op.emptyTag, op.emptyAttributes, op.wholeSourceSpan));
  23099. break;
  23100. case OpKind.SourceLocation:
  23101. const locationsLiteral = literalArr(op.locations.map(({ targetSlot, offset, line, column }) => {
  23102. if (targetSlot.slot === null) {
  23103. throw new Error('No slot was assigned for source location');
  23104. }
  23105. return literalArr([
  23106. literal(targetSlot.slot),
  23107. literal(offset),
  23108. literal(line),
  23109. literal(column),
  23110. ]);
  23111. }));
  23112. OpList.replace(op, attachSourceLocation(op.templatePath, locationsLiteral));
  23113. break;
  23114. case OpKind.Statement:
  23115. // Pass statement operations directly through.
  23116. break;
  23117. default:
  23118. throw new Error(`AssertionError: Unsupported reification of create op ${OpKind[op.kind]}`);
  23119. }
  23120. }
  23121. }
  23122. function reifyUpdateOperations(_unit, ops) {
  23123. for (const op of ops) {
  23124. transformExpressionsInOp(op, reifyIrExpression, VisitorContextFlag.None);
  23125. switch (op.kind) {
  23126. case OpKind.Advance:
  23127. OpList.replace(op, advance(op.delta, op.sourceSpan));
  23128. break;
  23129. case OpKind.Property:
  23130. if (op.expression instanceof Interpolation) {
  23131. OpList.replace(op, propertyInterpolate(op.name, op.expression.strings, op.expression.expressions, op.sanitizer, op.sourceSpan));
  23132. }
  23133. else {
  23134. OpList.replace(op, property(op.name, op.expression, op.sanitizer, op.sourceSpan));
  23135. }
  23136. break;
  23137. case OpKind.TwoWayProperty:
  23138. OpList.replace(op, twoWayProperty(op.name, op.expression, op.sanitizer, op.sourceSpan));
  23139. break;
  23140. case OpKind.StyleProp:
  23141. if (op.expression instanceof Interpolation) {
  23142. OpList.replace(op, stylePropInterpolate(op.name, op.expression.strings, op.expression.expressions, op.unit, op.sourceSpan));
  23143. }
  23144. else {
  23145. OpList.replace(op, styleProp(op.name, op.expression, op.unit, op.sourceSpan));
  23146. }
  23147. break;
  23148. case OpKind.ClassProp:
  23149. OpList.replace(op, classProp(op.name, op.expression, op.sourceSpan));
  23150. break;
  23151. case OpKind.StyleMap:
  23152. if (op.expression instanceof Interpolation) {
  23153. OpList.replace(op, styleMapInterpolate(op.expression.strings, op.expression.expressions, op.sourceSpan));
  23154. }
  23155. else {
  23156. OpList.replace(op, styleMap(op.expression, op.sourceSpan));
  23157. }
  23158. break;
  23159. case OpKind.ClassMap:
  23160. if (op.expression instanceof Interpolation) {
  23161. OpList.replace(op, classMapInterpolate(op.expression.strings, op.expression.expressions, op.sourceSpan));
  23162. }
  23163. else {
  23164. OpList.replace(op, classMap(op.expression, op.sourceSpan));
  23165. }
  23166. break;
  23167. case OpKind.I18nExpression:
  23168. OpList.replace(op, i18nExp(op.expression, op.sourceSpan));
  23169. break;
  23170. case OpKind.I18nApply:
  23171. OpList.replace(op, i18nApply(op.handle.slot, op.sourceSpan));
  23172. break;
  23173. case OpKind.InterpolateText:
  23174. OpList.replace(op, textInterpolate(op.interpolation.strings, op.interpolation.expressions, op.sourceSpan));
  23175. break;
  23176. case OpKind.Attribute:
  23177. if (op.expression instanceof Interpolation) {
  23178. OpList.replace(op, attributeInterpolate(op.name, op.expression.strings, op.expression.expressions, op.sanitizer, op.sourceSpan));
  23179. }
  23180. else {
  23181. OpList.replace(op, attribute(op.name, op.expression, op.sanitizer, op.namespace));
  23182. }
  23183. break;
  23184. case OpKind.HostProperty:
  23185. if (op.expression instanceof Interpolation) {
  23186. throw new Error('not yet handled');
  23187. }
  23188. else {
  23189. if (op.isAnimationTrigger) {
  23190. OpList.replace(op, syntheticHostProperty(op.name, op.expression, op.sourceSpan));
  23191. }
  23192. else {
  23193. OpList.replace(op, hostProperty(op.name, op.expression, op.sanitizer, op.sourceSpan));
  23194. }
  23195. }
  23196. break;
  23197. case OpKind.Variable:
  23198. if (op.variable.name === null) {
  23199. throw new Error(`AssertionError: unnamed variable ${op.xref}`);
  23200. }
  23201. OpList.replace(op, createStatementOp(new DeclareVarStmt(op.variable.name, op.initializer, undefined, StmtModifier.Final)));
  23202. break;
  23203. case OpKind.Conditional:
  23204. if (op.processed === null) {
  23205. throw new Error(`Conditional test was not set.`);
  23206. }
  23207. OpList.replace(op, conditional(op.processed, op.contextValue, op.sourceSpan));
  23208. break;
  23209. case OpKind.Repeater:
  23210. OpList.replace(op, repeater(op.collection, op.sourceSpan));
  23211. break;
  23212. case OpKind.DeferWhen:
  23213. OpList.replace(op, deferWhen(op.modifier, op.expr, op.sourceSpan));
  23214. break;
  23215. case OpKind.StoreLet:
  23216. throw new Error(`AssertionError: unexpected storeLet ${op.declaredName}`);
  23217. case OpKind.Statement:
  23218. // Pass statement operations directly through.
  23219. break;
  23220. default:
  23221. throw new Error(`AssertionError: Unsupported reification of update op ${OpKind[op.kind]}`);
  23222. }
  23223. }
  23224. }
  23225. function reifyIrExpression(expr) {
  23226. if (!isIrExpression(expr)) {
  23227. return expr;
  23228. }
  23229. switch (expr.kind) {
  23230. case ExpressionKind.NextContext:
  23231. return nextContext(expr.steps);
  23232. case ExpressionKind.Reference:
  23233. return reference(expr.targetSlot.slot + 1 + expr.offset);
  23234. case ExpressionKind.LexicalRead:
  23235. throw new Error(`AssertionError: unresolved LexicalRead of ${expr.name}`);
  23236. case ExpressionKind.TwoWayBindingSet:
  23237. throw new Error(`AssertionError: unresolved TwoWayBindingSet`);
  23238. case ExpressionKind.RestoreView:
  23239. if (typeof expr.view === 'number') {
  23240. throw new Error(`AssertionError: unresolved RestoreView`);
  23241. }
  23242. return restoreView(expr.view);
  23243. case ExpressionKind.ResetView:
  23244. return resetView(expr.expr);
  23245. case ExpressionKind.GetCurrentView:
  23246. return getCurrentView();
  23247. case ExpressionKind.ReadVariable:
  23248. if (expr.name === null) {
  23249. throw new Error(`Read of unnamed variable ${expr.xref}`);
  23250. }
  23251. return variable(expr.name);
  23252. case ExpressionKind.ReadTemporaryExpr:
  23253. if (expr.name === null) {
  23254. throw new Error(`Read of unnamed temporary ${expr.xref}`);
  23255. }
  23256. return variable(expr.name);
  23257. case ExpressionKind.AssignTemporaryExpr:
  23258. if (expr.name === null) {
  23259. throw new Error(`Assign of unnamed temporary ${expr.xref}`);
  23260. }
  23261. return variable(expr.name).set(expr.expr);
  23262. case ExpressionKind.PureFunctionExpr:
  23263. if (expr.fn === null) {
  23264. throw new Error(`AssertionError: expected PureFunctions to have been extracted`);
  23265. }
  23266. return pureFunction(expr.varOffset, expr.fn, expr.args);
  23267. case ExpressionKind.PureFunctionParameterExpr:
  23268. throw new Error(`AssertionError: expected PureFunctionParameterExpr to have been extracted`);
  23269. case ExpressionKind.PipeBinding:
  23270. return pipeBind(expr.targetSlot.slot, expr.varOffset, expr.args);
  23271. case ExpressionKind.PipeBindingVariadic:
  23272. return pipeBindV(expr.targetSlot.slot, expr.varOffset, expr.args);
  23273. case ExpressionKind.SlotLiteralExpr:
  23274. return literal(expr.slot.slot);
  23275. case ExpressionKind.ContextLetReference:
  23276. return readContextLet(expr.targetSlot.slot);
  23277. case ExpressionKind.StoreLet:
  23278. return storeLet(expr.value, expr.sourceSpan);
  23279. case ExpressionKind.TrackContext:
  23280. return variable('this');
  23281. default:
  23282. throw new Error(`AssertionError: Unsupported reification of ir.Expression kind: ${ExpressionKind[expr.kind]}`);
  23283. }
  23284. }
  23285. /**
  23286. * Listeners get turned into a function expression, which may or may not have the `$event`
  23287. * parameter defined.
  23288. */
  23289. function reifyListenerHandler(unit, name, handlerOps, consumesDollarEvent) {
  23290. // First, reify all instruction calls within `handlerOps`.
  23291. reifyUpdateOperations(unit, handlerOps);
  23292. // Next, extract all the `o.Statement`s from the reified operations. We can expect that at this
  23293. // point, all operations have been converted to statements.
  23294. const handlerStmts = [];
  23295. for (const op of handlerOps) {
  23296. if (op.kind !== OpKind.Statement) {
  23297. throw new Error(`AssertionError: expected reified statements, but found op ${OpKind[op.kind]}`);
  23298. }
  23299. handlerStmts.push(op.statement);
  23300. }
  23301. // If `$event` is referenced, we need to generate it as a parameter.
  23302. const params = [];
  23303. if (consumesDollarEvent) {
  23304. // We need the `$event` parameter.
  23305. params.push(new FnParam('$event'));
  23306. }
  23307. return fn(params, handlerStmts, undefined, undefined, name);
  23308. }
  23309. /** Reifies the tracking expression of a `RepeaterCreateOp`. */
  23310. function reifyTrackBy(unit, op) {
  23311. // If the tracking function was created already, there's nothing left to do.
  23312. if (op.trackByFn !== null) {
  23313. return op.trackByFn;
  23314. }
  23315. const params = [new FnParam('$index'), new FnParam('$item')];
  23316. let fn$1;
  23317. if (op.trackByOps === null) {
  23318. // If there are no additional ops related to the tracking function, we just need
  23319. // to turn it into a function that returns the result of the expression.
  23320. fn$1 = op.usesComponentInstance
  23321. ? fn(params, [new ReturnStatement(op.track)])
  23322. : arrowFn(params, op.track);
  23323. }
  23324. else {
  23325. // Otherwise first we need to reify the track-related ops.
  23326. reifyUpdateOperations(unit, op.trackByOps);
  23327. const statements = [];
  23328. for (const trackOp of op.trackByOps) {
  23329. if (trackOp.kind !== OpKind.Statement) {
  23330. throw new Error(`AssertionError: expected reified statements, but found op ${OpKind[trackOp.kind]}`);
  23331. }
  23332. statements.push(trackOp.statement);
  23333. }
  23334. // Afterwards we can create the function from those ops.
  23335. fn$1 =
  23336. op.usesComponentInstance ||
  23337. statements.length !== 1 ||
  23338. !(statements[0] instanceof ReturnStatement)
  23339. ? fn(params, statements)
  23340. : arrowFn(params, statements[0].value);
  23341. }
  23342. op.trackByFn = unit.job.pool.getSharedFunctionReference(fn$1, '_forTrack');
  23343. return op.trackByFn;
  23344. }
  23345. /**
  23346. * Binding with no content can be safely deleted.
  23347. */
  23348. function removeEmptyBindings(job) {
  23349. for (const unit of job.units) {
  23350. for (const op of unit.update) {
  23351. switch (op.kind) {
  23352. case OpKind.Attribute:
  23353. case OpKind.Binding:
  23354. case OpKind.ClassProp:
  23355. case OpKind.ClassMap:
  23356. case OpKind.Property:
  23357. case OpKind.StyleProp:
  23358. case OpKind.StyleMap:
  23359. if (op.expression instanceof EmptyExpr) {
  23360. OpList.remove(op);
  23361. }
  23362. break;
  23363. }
  23364. }
  23365. }
  23366. }
  23367. /**
  23368. * Remove the i18n context ops after they are no longer needed, and null out references to them to
  23369. * be safe.
  23370. */
  23371. function removeI18nContexts(job) {
  23372. for (const unit of job.units) {
  23373. for (const op of unit.create) {
  23374. switch (op.kind) {
  23375. case OpKind.I18nContext:
  23376. OpList.remove(op);
  23377. break;
  23378. case OpKind.I18nStart:
  23379. op.context = null;
  23380. break;
  23381. }
  23382. }
  23383. }
  23384. }
  23385. /**
  23386. * i18nAttributes ops will be generated for each i18n attribute. However, not all i18n attribues
  23387. * will contain dynamic content, and so some of these i18nAttributes ops may be unnecessary.
  23388. */
  23389. function removeUnusedI18nAttributesOps(job) {
  23390. for (const unit of job.units) {
  23391. const ownersWithI18nExpressions = new Set();
  23392. for (const op of unit.update) {
  23393. switch (op.kind) {
  23394. case OpKind.I18nExpression:
  23395. ownersWithI18nExpressions.add(op.i18nOwner);
  23396. }
  23397. }
  23398. for (const op of unit.create) {
  23399. switch (op.kind) {
  23400. case OpKind.I18nAttributes:
  23401. if (ownersWithI18nExpressions.has(op.xref)) {
  23402. continue;
  23403. }
  23404. OpList.remove(op);
  23405. }
  23406. }
  23407. }
  23408. }
  23409. /**
  23410. * Resolves `ir.ContextExpr` expressions (which represent embedded view or component contexts) to
  23411. * either the `ctx` parameter to component functions (for the current view context) or to variables
  23412. * that store those contexts (for contexts accessed via the `nextContext()` instruction).
  23413. */
  23414. function resolveContexts(job) {
  23415. for (const unit of job.units) {
  23416. processLexicalScope$1(unit, unit.create);
  23417. processLexicalScope$1(unit, unit.update);
  23418. }
  23419. }
  23420. function processLexicalScope$1(view, ops) {
  23421. // Track the expressions used to access all available contexts within the current view, by the
  23422. // view `ir.XrefId`.
  23423. const scope = new Map();
  23424. // The current view's context is accessible via the `ctx` parameter.
  23425. scope.set(view.xref, variable('ctx'));
  23426. for (const op of ops) {
  23427. switch (op.kind) {
  23428. case OpKind.Variable:
  23429. switch (op.variable.kind) {
  23430. case SemanticVariableKind.Context:
  23431. scope.set(op.variable.view, new ReadVariableExpr(op.xref));
  23432. break;
  23433. }
  23434. break;
  23435. case OpKind.Listener:
  23436. case OpKind.TwoWayListener:
  23437. processLexicalScope$1(view, op.handlerOps);
  23438. break;
  23439. case OpKind.RepeaterCreate:
  23440. if (op.trackByOps !== null) {
  23441. processLexicalScope$1(view, op.trackByOps);
  23442. }
  23443. break;
  23444. }
  23445. }
  23446. if (view === view.job.root) {
  23447. // Prefer `ctx` of the root view to any variables which happen to contain the root context.
  23448. scope.set(view.xref, variable('ctx'));
  23449. }
  23450. for (const op of ops) {
  23451. transformExpressionsInOp(op, (expr) => {
  23452. if (expr instanceof ContextExpr) {
  23453. if (!scope.has(expr.view)) {
  23454. throw new Error(`No context found for reference to view ${expr.view} from view ${view.xref}`);
  23455. }
  23456. return scope.get(expr.view);
  23457. }
  23458. else {
  23459. return expr;
  23460. }
  23461. }, VisitorContextFlag.None);
  23462. }
  23463. }
  23464. /**
  23465. * Any variable inside a listener with the name `$event` will be transformed into a output lexical
  23466. * read immediately, and does not participate in any of the normal logic for handling variables.
  23467. */
  23468. function resolveDollarEvent(job) {
  23469. for (const unit of job.units) {
  23470. transformDollarEvent(unit.create);
  23471. transformDollarEvent(unit.update);
  23472. }
  23473. }
  23474. function transformDollarEvent(ops) {
  23475. for (const op of ops) {
  23476. if (op.kind === OpKind.Listener || op.kind === OpKind.TwoWayListener) {
  23477. transformExpressionsInOp(op, (expr) => {
  23478. if (expr instanceof LexicalReadExpr && expr.name === '$event') {
  23479. // Two-way listeners always consume `$event` so they omit this field.
  23480. if (op.kind === OpKind.Listener) {
  23481. op.consumesDollarEvent = true;
  23482. }
  23483. return new ReadVarExpr(expr.name);
  23484. }
  23485. return expr;
  23486. }, VisitorContextFlag.InChildOperation);
  23487. }
  23488. }
  23489. }
  23490. /**
  23491. * Resolve the element placeholders in i18n messages.
  23492. */
  23493. function resolveI18nElementPlaceholders(job) {
  23494. // Record all of the element and i18n context ops for use later.
  23495. const i18nContexts = new Map();
  23496. const elements = new Map();
  23497. for (const unit of job.units) {
  23498. for (const op of unit.create) {
  23499. switch (op.kind) {
  23500. case OpKind.I18nContext:
  23501. i18nContexts.set(op.xref, op);
  23502. break;
  23503. case OpKind.ElementStart:
  23504. elements.set(op.xref, op);
  23505. break;
  23506. }
  23507. }
  23508. }
  23509. resolvePlaceholdersForView(job, job.root, i18nContexts, elements);
  23510. }
  23511. /**
  23512. * Recursively resolves element and template tag placeholders in the given view.
  23513. */
  23514. function resolvePlaceholdersForView(job, unit, i18nContexts, elements, pendingStructuralDirective) {
  23515. // Track the current i18n op and corresponding i18n context op as we step through the creation
  23516. // IR.
  23517. let currentOps = null;
  23518. let pendingStructuralDirectiveCloses = new Map();
  23519. for (const op of unit.create) {
  23520. switch (op.kind) {
  23521. case OpKind.I18nStart:
  23522. if (!op.context) {
  23523. throw Error('Could not find i18n context for i18n op');
  23524. }
  23525. currentOps = { i18nBlock: op, i18nContext: i18nContexts.get(op.context) };
  23526. break;
  23527. case OpKind.I18nEnd:
  23528. currentOps = null;
  23529. break;
  23530. case OpKind.ElementStart:
  23531. // For elements with i18n placeholders, record its slot value in the params map under the
  23532. // corresponding tag start placeholder.
  23533. if (op.i18nPlaceholder !== undefined) {
  23534. if (currentOps === null) {
  23535. throw Error('i18n tag placeholder should only occur inside an i18n block');
  23536. }
  23537. recordElementStart(op, currentOps.i18nContext, currentOps.i18nBlock, pendingStructuralDirective);
  23538. // If there is a separate close tag placeholder for this element, save the pending
  23539. // structural directive so we can pass it to the closing tag as well.
  23540. if (pendingStructuralDirective && op.i18nPlaceholder.closeName) {
  23541. pendingStructuralDirectiveCloses.set(op.xref, pendingStructuralDirective);
  23542. }
  23543. // Clear out the pending structural directive now that its been accounted for.
  23544. pendingStructuralDirective = undefined;
  23545. }
  23546. break;
  23547. case OpKind.ElementEnd:
  23548. // For elements with i18n placeholders, record its slot value in the params map under the
  23549. // corresponding tag close placeholder.
  23550. const startOp = elements.get(op.xref);
  23551. if (startOp && startOp.i18nPlaceholder !== undefined) {
  23552. if (currentOps === null) {
  23553. throw Error('AssertionError: i18n tag placeholder should only occur inside an i18n block');
  23554. }
  23555. recordElementClose(startOp, currentOps.i18nContext, currentOps.i18nBlock, pendingStructuralDirectiveCloses.get(op.xref));
  23556. // Clear out the pending structural directive close that was accounted for.
  23557. pendingStructuralDirectiveCloses.delete(op.xref);
  23558. }
  23559. break;
  23560. case OpKind.Projection:
  23561. // For content projections with i18n placeholders, record its slot value in the params map
  23562. // under the corresponding tag start and close placeholders.
  23563. if (op.i18nPlaceholder !== undefined) {
  23564. if (currentOps === null) {
  23565. throw Error('i18n tag placeholder should only occur inside an i18n block');
  23566. }
  23567. recordElementStart(op, currentOps.i18nContext, currentOps.i18nBlock, pendingStructuralDirective);
  23568. recordElementClose(op, currentOps.i18nContext, currentOps.i18nBlock, pendingStructuralDirective);
  23569. // Clear out the pending structural directive now that its been accounted for.
  23570. pendingStructuralDirective = undefined;
  23571. }
  23572. break;
  23573. case OpKind.Template:
  23574. const view = job.views.get(op.xref);
  23575. if (op.i18nPlaceholder === undefined) {
  23576. // If there is no i18n placeholder, just recurse into the view in case it contains i18n
  23577. // blocks.
  23578. resolvePlaceholdersForView(job, view, i18nContexts, elements);
  23579. }
  23580. else {
  23581. if (currentOps === null) {
  23582. throw Error('i18n tag placeholder should only occur inside an i18n block');
  23583. }
  23584. if (op.templateKind === TemplateKind.Structural) {
  23585. // If this is a structural directive template, don't record anything yet. Instead pass
  23586. // the current template as a pending structural directive to be recorded when we find
  23587. // the element, content, or template it belongs to. This allows us to create combined
  23588. // values that represent, e.g. the start of a template and element at the same time.
  23589. resolvePlaceholdersForView(job, view, i18nContexts, elements, op);
  23590. }
  23591. else {
  23592. // If this is some other kind of template, we can record its start, recurse into its
  23593. // view, and then record its end.
  23594. recordTemplateStart(job, view, op.handle.slot, op.i18nPlaceholder, currentOps.i18nContext, currentOps.i18nBlock, pendingStructuralDirective);
  23595. resolvePlaceholdersForView(job, view, i18nContexts, elements);
  23596. recordTemplateClose(job, view, op.handle.slot, op.i18nPlaceholder, currentOps.i18nContext, currentOps.i18nBlock, pendingStructuralDirective);
  23597. pendingStructuralDirective = undefined;
  23598. }
  23599. }
  23600. break;
  23601. case OpKind.RepeaterCreate:
  23602. if (pendingStructuralDirective !== undefined) {
  23603. throw Error('AssertionError: Unexpected structural directive associated with @for block');
  23604. }
  23605. // RepeaterCreate has 3 slots: the first is for the op itself, the second is for the @for
  23606. // template and the (optional) third is for the @empty template.
  23607. const forSlot = op.handle.slot + 1;
  23608. const forView = job.views.get(op.xref);
  23609. // First record all of the placeholders for the @for template.
  23610. if (op.i18nPlaceholder === undefined) {
  23611. // If there is no i18n placeholder, just recurse into the view in case it contains i18n
  23612. // blocks.
  23613. resolvePlaceholdersForView(job, forView, i18nContexts, elements);
  23614. }
  23615. else {
  23616. if (currentOps === null) {
  23617. throw Error('i18n tag placeholder should only occur inside an i18n block');
  23618. }
  23619. recordTemplateStart(job, forView, forSlot, op.i18nPlaceholder, currentOps.i18nContext, currentOps.i18nBlock, pendingStructuralDirective);
  23620. resolvePlaceholdersForView(job, forView, i18nContexts, elements);
  23621. recordTemplateClose(job, forView, forSlot, op.i18nPlaceholder, currentOps.i18nContext, currentOps.i18nBlock, pendingStructuralDirective);
  23622. pendingStructuralDirective = undefined;
  23623. }
  23624. // Then if there's an @empty template, add its placeholders as well.
  23625. if (op.emptyView !== null) {
  23626. // RepeaterCreate has 3 slots: the first is for the op itself, the second is for the @for
  23627. // template and the (optional) third is for the @empty template.
  23628. const emptySlot = op.handle.slot + 2;
  23629. const emptyView = job.views.get(op.emptyView);
  23630. if (op.emptyI18nPlaceholder === undefined) {
  23631. // If there is no i18n placeholder, just recurse into the view in case it contains i18n
  23632. // blocks.
  23633. resolvePlaceholdersForView(job, emptyView, i18nContexts, elements);
  23634. }
  23635. else {
  23636. if (currentOps === null) {
  23637. throw Error('i18n tag placeholder should only occur inside an i18n block');
  23638. }
  23639. recordTemplateStart(job, emptyView, emptySlot, op.emptyI18nPlaceholder, currentOps.i18nContext, currentOps.i18nBlock, pendingStructuralDirective);
  23640. resolvePlaceholdersForView(job, emptyView, i18nContexts, elements);
  23641. recordTemplateClose(job, emptyView, emptySlot, op.emptyI18nPlaceholder, currentOps.i18nContext, currentOps.i18nBlock, pendingStructuralDirective);
  23642. pendingStructuralDirective = undefined;
  23643. }
  23644. }
  23645. break;
  23646. }
  23647. }
  23648. }
  23649. /**
  23650. * Records an i18n param value for the start of an element.
  23651. */
  23652. function recordElementStart(op, i18nContext, i18nBlock, structuralDirective) {
  23653. const { startName, closeName } = op.i18nPlaceholder;
  23654. let flags = I18nParamValueFlags.ElementTag | I18nParamValueFlags.OpenTag;
  23655. let value = op.handle.slot;
  23656. // If the element is associated with a structural directive, start it as well.
  23657. if (structuralDirective !== undefined) {
  23658. flags |= I18nParamValueFlags.TemplateTag;
  23659. value = { element: value, template: structuralDirective.handle.slot };
  23660. }
  23661. // For self-closing tags, there is no close tag placeholder. Instead, the start tag
  23662. // placeholder accounts for the start and close of the element.
  23663. if (!closeName) {
  23664. flags |= I18nParamValueFlags.CloseTag;
  23665. }
  23666. addParam(i18nContext.params, startName, value, i18nBlock.subTemplateIndex, flags);
  23667. }
  23668. /**
  23669. * Records an i18n param value for the closing of an element.
  23670. */
  23671. function recordElementClose(op, i18nContext, i18nBlock, structuralDirective) {
  23672. const { closeName } = op.i18nPlaceholder;
  23673. // Self-closing tags don't have a closing tag placeholder, instead the element closing is
  23674. // recorded via an additional flag on the element start value.
  23675. if (closeName) {
  23676. let flags = I18nParamValueFlags.ElementTag | I18nParamValueFlags.CloseTag;
  23677. let value = op.handle.slot;
  23678. // If the element is associated with a structural directive, close it as well.
  23679. if (structuralDirective !== undefined) {
  23680. flags |= I18nParamValueFlags.TemplateTag;
  23681. value = { element: value, template: structuralDirective.handle.slot };
  23682. }
  23683. addParam(i18nContext.params, closeName, value, i18nBlock.subTemplateIndex, flags);
  23684. }
  23685. }
  23686. /**
  23687. * Records an i18n param value for the start of a template.
  23688. */
  23689. function recordTemplateStart(job, view, slot, i18nPlaceholder, i18nContext, i18nBlock, structuralDirective) {
  23690. let { startName, closeName } = i18nPlaceholder;
  23691. let flags = I18nParamValueFlags.TemplateTag | I18nParamValueFlags.OpenTag;
  23692. // For self-closing tags, there is no close tag placeholder. Instead, the start tag
  23693. // placeholder accounts for the start and close of the element.
  23694. if (!closeName) {
  23695. flags |= I18nParamValueFlags.CloseTag;
  23696. }
  23697. // If the template is associated with a structural directive, record the structural directive's
  23698. // start first. Since this template must be in the structural directive's view, we can just
  23699. // directly use the current i18n block's sub-template index.
  23700. if (structuralDirective !== undefined) {
  23701. addParam(i18nContext.params, startName, structuralDirective.handle.slot, i18nBlock.subTemplateIndex, flags);
  23702. }
  23703. // Record the start of the template. For the sub-template index, pass the index for the template's
  23704. // view, rather than the current i18n block's index.
  23705. addParam(i18nContext.params, startName, slot, getSubTemplateIndexForTemplateTag(job, i18nBlock, view), flags);
  23706. }
  23707. /**
  23708. * Records an i18n param value for the closing of a template.
  23709. */
  23710. function recordTemplateClose(job, view, slot, i18nPlaceholder, i18nContext, i18nBlock, structuralDirective) {
  23711. const { closeName } = i18nPlaceholder;
  23712. const flags = I18nParamValueFlags.TemplateTag | I18nParamValueFlags.CloseTag;
  23713. // Self-closing tags don't have a closing tag placeholder, instead the template's closing is
  23714. // recorded via an additional flag on the template start value.
  23715. if (closeName) {
  23716. // Record the closing of the template. For the sub-template index, pass the index for the
  23717. // template's view, rather than the current i18n block's index.
  23718. addParam(i18nContext.params, closeName, slot, getSubTemplateIndexForTemplateTag(job, i18nBlock, view), flags);
  23719. // If the template is associated with a structural directive, record the structural directive's
  23720. // closing after. Since this template must be in the structural directive's view, we can just
  23721. // directly use the current i18n block's sub-template index.
  23722. if (structuralDirective !== undefined) {
  23723. addParam(i18nContext.params, closeName, structuralDirective.handle.slot, i18nBlock.subTemplateIndex, flags);
  23724. }
  23725. }
  23726. }
  23727. /**
  23728. * Get the subTemplateIndex for the given template op. For template ops, use the subTemplateIndex of
  23729. * the child i18n block inside the template.
  23730. */
  23731. function getSubTemplateIndexForTemplateTag(job, i18nOp, view) {
  23732. for (const childOp of view.create) {
  23733. if (childOp.kind === OpKind.I18nStart) {
  23734. return childOp.subTemplateIndex;
  23735. }
  23736. }
  23737. return i18nOp.subTemplateIndex;
  23738. }
  23739. /**
  23740. * Add a param value to the given params map.
  23741. */
  23742. function addParam(params, placeholder, value, subTemplateIndex, flags) {
  23743. const values = params.get(placeholder) ?? [];
  23744. values.push({ value, subTemplateIndex, flags });
  23745. params.set(placeholder, values);
  23746. }
  23747. /**
  23748. * Resolve the i18n expression placeholders in i18n messages.
  23749. */
  23750. function resolveI18nExpressionPlaceholders(job) {
  23751. // Record all of the i18n context ops, and the sub-template index for each i18n op.
  23752. const subTemplateIndices = new Map();
  23753. const i18nContexts = new Map();
  23754. const icuPlaceholders = new Map();
  23755. for (const unit of job.units) {
  23756. for (const op of unit.create) {
  23757. switch (op.kind) {
  23758. case OpKind.I18nStart:
  23759. subTemplateIndices.set(op.xref, op.subTemplateIndex);
  23760. break;
  23761. case OpKind.I18nContext:
  23762. i18nContexts.set(op.xref, op);
  23763. break;
  23764. case OpKind.IcuPlaceholder:
  23765. icuPlaceholders.set(op.xref, op);
  23766. break;
  23767. }
  23768. }
  23769. }
  23770. // Keep track of the next available expression index for each i18n message.
  23771. const expressionIndices = new Map();
  23772. // Keep track of a reference index for each expression.
  23773. // We use different references for normal i18n expressio and attribute i18n expressions. This is
  23774. // because child i18n blocks in templates don't get their own context, since they're rolled into
  23775. // the translated message of the parent, but they may target a different slot.
  23776. const referenceIndex = (op) => op.usage === I18nExpressionFor.I18nText ? op.i18nOwner : op.context;
  23777. for (const unit of job.units) {
  23778. for (const op of unit.update) {
  23779. if (op.kind === OpKind.I18nExpression) {
  23780. const index = expressionIndices.get(referenceIndex(op)) || 0;
  23781. const subTemplateIndex = subTemplateIndices.get(op.i18nOwner) ?? null;
  23782. const value = {
  23783. value: index,
  23784. subTemplateIndex: subTemplateIndex,
  23785. flags: I18nParamValueFlags.ExpressionIndex,
  23786. };
  23787. updatePlaceholder(op, value, i18nContexts, icuPlaceholders);
  23788. expressionIndices.set(referenceIndex(op), index + 1);
  23789. }
  23790. }
  23791. }
  23792. }
  23793. function updatePlaceholder(op, value, i18nContexts, icuPlaceholders) {
  23794. if (op.i18nPlaceholder !== null) {
  23795. const i18nContext = i18nContexts.get(op.context);
  23796. const params = op.resolutionTime === I18nParamResolutionTime.Creation
  23797. ? i18nContext.params
  23798. : i18nContext.postprocessingParams;
  23799. const values = params.get(op.i18nPlaceholder) || [];
  23800. values.push(value);
  23801. params.set(op.i18nPlaceholder, values);
  23802. }
  23803. if (op.icuPlaceholder !== null) {
  23804. const icuPlaceholderOp = icuPlaceholders.get(op.icuPlaceholder);
  23805. icuPlaceholderOp?.expressionPlaceholders.push(value);
  23806. }
  23807. }
  23808. /**
  23809. * Resolves lexical references in views (`ir.LexicalReadExpr`) to either a target variable or to
  23810. * property reads on the top-level component context.
  23811. *
  23812. * Also matches `ir.RestoreViewExpr` expressions with the variables of their corresponding saved
  23813. * views.
  23814. */
  23815. function resolveNames(job) {
  23816. for (const unit of job.units) {
  23817. processLexicalScope(unit, unit.create, null);
  23818. processLexicalScope(unit, unit.update, null);
  23819. }
  23820. }
  23821. function processLexicalScope(unit, ops, savedView) {
  23822. // Maps names defined in the lexical scope of this template to the `ir.XrefId`s of the variable
  23823. // declarations which represent those values.
  23824. //
  23825. // Since variables are generated in each view for the entire lexical scope (including any
  23826. // identifiers from parent templates) only local variables need be considered here.
  23827. const scope = new Map();
  23828. // Symbols defined within the current scope. They take precedence over ones defined outside.
  23829. const localDefinitions = new Map();
  23830. // First, step through the operations list and:
  23831. // 1) build up the `scope` mapping
  23832. // 2) recurse into any listener functions
  23833. for (const op of ops) {
  23834. switch (op.kind) {
  23835. case OpKind.Variable:
  23836. switch (op.variable.kind) {
  23837. case SemanticVariableKind.Identifier:
  23838. if (op.variable.local) {
  23839. if (localDefinitions.has(op.variable.identifier)) {
  23840. continue;
  23841. }
  23842. localDefinitions.set(op.variable.identifier, op.xref);
  23843. }
  23844. else if (scope.has(op.variable.identifier)) {
  23845. continue;
  23846. }
  23847. scope.set(op.variable.identifier, op.xref);
  23848. break;
  23849. case SemanticVariableKind.Alias:
  23850. // This variable represents some kind of identifier which can be used in the template.
  23851. if (scope.has(op.variable.identifier)) {
  23852. continue;
  23853. }
  23854. scope.set(op.variable.identifier, op.xref);
  23855. break;
  23856. case SemanticVariableKind.SavedView:
  23857. // This variable represents a snapshot of the current view context, and can be used to
  23858. // restore that context within listener functions.
  23859. savedView = {
  23860. view: op.variable.view,
  23861. variable: op.xref,
  23862. };
  23863. break;
  23864. }
  23865. break;
  23866. case OpKind.Listener:
  23867. case OpKind.TwoWayListener:
  23868. // Listener functions have separate variable declarations, so process them as a separate
  23869. // lexical scope.
  23870. processLexicalScope(unit, op.handlerOps, savedView);
  23871. break;
  23872. case OpKind.RepeaterCreate:
  23873. if (op.trackByOps !== null) {
  23874. processLexicalScope(unit, op.trackByOps, savedView);
  23875. }
  23876. break;
  23877. }
  23878. }
  23879. // Next, use the `scope` mapping to match `ir.LexicalReadExpr` with defined names in the lexical
  23880. // scope. Also, look for `ir.RestoreViewExpr`s and match them with the snapshotted view context
  23881. // variable.
  23882. for (const op of ops) {
  23883. if (op.kind == OpKind.Listener || op.kind === OpKind.TwoWayListener) {
  23884. // Listeners were already processed above with their own scopes.
  23885. continue;
  23886. }
  23887. transformExpressionsInOp(op, (expr) => {
  23888. if (expr instanceof LexicalReadExpr) {
  23889. // `expr` is a read of a name within the lexical scope of this view.
  23890. // Either that name is defined within the current view, or it represents a property from the
  23891. // main component context.
  23892. if (localDefinitions.has(expr.name)) {
  23893. return new ReadVariableExpr(localDefinitions.get(expr.name));
  23894. }
  23895. else if (scope.has(expr.name)) {
  23896. // This was a defined variable in the current scope.
  23897. return new ReadVariableExpr(scope.get(expr.name));
  23898. }
  23899. else {
  23900. // Reading from the component context.
  23901. return new ReadPropExpr(new ContextExpr(unit.job.root.xref), expr.name);
  23902. }
  23903. }
  23904. else if (expr instanceof RestoreViewExpr && typeof expr.view === 'number') {
  23905. // `ir.RestoreViewExpr` happens in listener functions and restores a saved view from the
  23906. // parent creation list. We expect to find that we captured the `savedView` previously, and
  23907. // that it matches the expected view to be restored.
  23908. if (savedView === null || savedView.view !== expr.view) {
  23909. throw new Error(`AssertionError: no saved view ${expr.view} from view ${unit.xref}`);
  23910. }
  23911. expr.view = new ReadVariableExpr(savedView.variable);
  23912. return expr;
  23913. }
  23914. else {
  23915. return expr;
  23916. }
  23917. }, VisitorContextFlag.None);
  23918. }
  23919. for (const op of ops) {
  23920. visitExpressionsInOp(op, (expr) => {
  23921. if (expr instanceof LexicalReadExpr) {
  23922. throw new Error(`AssertionError: no lexical reads should remain, but found read of ${expr.name}`);
  23923. }
  23924. });
  23925. }
  23926. }
  23927. /**
  23928. * Map of security contexts to their sanitizer function.
  23929. */
  23930. const sanitizerFns = new Map([
  23931. [SecurityContext.HTML, Identifiers.sanitizeHtml],
  23932. [SecurityContext.RESOURCE_URL, Identifiers.sanitizeResourceUrl],
  23933. [SecurityContext.SCRIPT, Identifiers.sanitizeScript],
  23934. [SecurityContext.STYLE, Identifiers.sanitizeStyle],
  23935. [SecurityContext.URL, Identifiers.sanitizeUrl],
  23936. ]);
  23937. /**
  23938. * Map of security contexts to their trusted value function.
  23939. */
  23940. const trustedValueFns = new Map([
  23941. [SecurityContext.HTML, Identifiers.trustConstantHtml],
  23942. [SecurityContext.RESOURCE_URL, Identifiers.trustConstantResourceUrl],
  23943. ]);
  23944. /**
  23945. * Resolves sanitization functions for ops that need them.
  23946. */
  23947. function resolveSanitizers(job) {
  23948. for (const unit of job.units) {
  23949. const elements = createOpXrefMap(unit);
  23950. // For normal element bindings we create trusted values for security sensitive constant
  23951. // attributes. However, for host bindings we skip this step (this matches what
  23952. // TemplateDefinitionBuilder does).
  23953. // TODO: Is the TDB behavior correct here?
  23954. if (job.kind !== CompilationJobKind.Host) {
  23955. for (const op of unit.create) {
  23956. if (op.kind === OpKind.ExtractedAttribute) {
  23957. const trustedValueFn = trustedValueFns.get(getOnlySecurityContext(op.securityContext)) ?? null;
  23958. op.trustedValueFn = trustedValueFn !== null ? importExpr(trustedValueFn) : null;
  23959. }
  23960. }
  23961. }
  23962. for (const op of unit.update) {
  23963. switch (op.kind) {
  23964. case OpKind.Property:
  23965. case OpKind.Attribute:
  23966. case OpKind.HostProperty:
  23967. let sanitizerFn = null;
  23968. if (Array.isArray(op.securityContext) &&
  23969. op.securityContext.length === 2 &&
  23970. op.securityContext.indexOf(SecurityContext.URL) > -1 &&
  23971. op.securityContext.indexOf(SecurityContext.RESOURCE_URL) > -1) {
  23972. // When the host element isn't known, some URL attributes (such as "src" and "href") may
  23973. // be part of multiple different security contexts. In this case we use special
  23974. // sanitization function and select the actual sanitizer at runtime based on a tag name
  23975. // that is provided while invoking sanitization function.
  23976. sanitizerFn = Identifiers.sanitizeUrlOrResourceUrl;
  23977. }
  23978. else {
  23979. sanitizerFn = sanitizerFns.get(getOnlySecurityContext(op.securityContext)) ?? null;
  23980. }
  23981. op.sanitizer = sanitizerFn !== null ? importExpr(sanitizerFn) : null;
  23982. // If there was no sanitization function found based on the security context of an
  23983. // attribute/property, check whether this attribute/property is one of the
  23984. // security-sensitive <iframe> attributes (and that the current element is actually an
  23985. // <iframe>).
  23986. if (op.sanitizer === null) {
  23987. let isIframe = false;
  23988. if (job.kind === CompilationJobKind.Host || op.kind === OpKind.HostProperty) {
  23989. // Note: for host bindings defined on a directive, we do not try to find all
  23990. // possible places where it can be matched, so we can not determine whether
  23991. // the host element is an <iframe>. In this case, we just assume it is and append a
  23992. // validation function, which is invoked at runtime and would have access to the
  23993. // underlying DOM element to check if it's an <iframe> and if so - run extra checks.
  23994. isIframe = true;
  23995. }
  23996. else {
  23997. // For a normal binding we can just check if the element its on is an iframe.
  23998. const ownerOp = elements.get(op.target);
  23999. if (ownerOp === undefined || !isElementOrContainerOp(ownerOp)) {
  24000. throw Error('Property should have an element-like owner');
  24001. }
  24002. isIframe = isIframeElement(ownerOp);
  24003. }
  24004. if (isIframe && isIframeSecuritySensitiveAttr(op.name)) {
  24005. op.sanitizer = importExpr(Identifiers.validateIframeAttribute);
  24006. }
  24007. }
  24008. break;
  24009. }
  24010. }
  24011. }
  24012. }
  24013. /**
  24014. * Checks whether the given op represents an iframe element.
  24015. */
  24016. function isIframeElement(op) {
  24017. return op.kind === OpKind.ElementStart && op.tag?.toLowerCase() === 'iframe';
  24018. }
  24019. /**
  24020. * Asserts that there is only a single security context and returns it.
  24021. */
  24022. function getOnlySecurityContext(securityContext) {
  24023. if (Array.isArray(securityContext)) {
  24024. if (securityContext.length > 1) {
  24025. // TODO: What should we do here? TDB just took the first one, but this feels like something we
  24026. // would want to know about and create a special case for like we did for Url/ResourceUrl. My
  24027. // guess is that, outside of the Url/ResourceUrl case, this never actually happens. If there
  24028. // do turn out to be other cases, throwing an error until we can address it feels safer.
  24029. throw Error(`AssertionError: Ambiguous security context`);
  24030. }
  24031. return securityContext[0] || SecurityContext.NONE;
  24032. }
  24033. return securityContext;
  24034. }
  24035. /**
  24036. * Transforms a `TwoWayBindingSet` expression into an expression that either
  24037. * sets a value through the `twoWayBindingSet` instruction or falls back to setting
  24038. * the value directly. E.g. the expression `TwoWayBindingSet(target, value)` becomes:
  24039. * `ng.twoWayBindingSet(target, value) || (target = value)`.
  24040. */
  24041. function transformTwoWayBindingSet(job) {
  24042. for (const unit of job.units) {
  24043. for (const op of unit.create) {
  24044. if (op.kind === OpKind.TwoWayListener) {
  24045. transformExpressionsInOp(op, (expr) => {
  24046. if (!(expr instanceof TwoWayBindingSetExpr)) {
  24047. return expr;
  24048. }
  24049. const { target, value } = expr;
  24050. if (target instanceof ReadPropExpr || target instanceof ReadKeyExpr) {
  24051. return twoWayBindingSet(target, value).or(target.set(value));
  24052. }
  24053. // ASSUMPTION: here we're assuming that `ReadVariableExpr` will be a reference
  24054. // to a local template variable. This appears to be the case at the time of writing.
  24055. // If the expression is targeting a variable read, we only emit the `twoWayBindingSet`
  24056. // since the fallback would be attempting to write into a constant. Invalid usages will be
  24057. // flagged during template type checking.
  24058. if (target instanceof ReadVariableExpr) {
  24059. return twoWayBindingSet(target, value);
  24060. }
  24061. throw new Error(`Unsupported expression in two-way action binding.`);
  24062. }, VisitorContextFlag.InChildOperation);
  24063. }
  24064. }
  24065. }
  24066. }
  24067. /**
  24068. * When inside of a listener, we may need access to one or more enclosing views. Therefore, each
  24069. * view should save the current view, and each listener must have the ability to restore the
  24070. * appropriate view. We eagerly generate all save view variables; they will be optimized away later.
  24071. */
  24072. function saveAndRestoreView(job) {
  24073. for (const unit of job.units) {
  24074. unit.create.prepend([
  24075. createVariableOp(unit.job.allocateXrefId(), {
  24076. kind: SemanticVariableKind.SavedView,
  24077. name: null,
  24078. view: unit.xref,
  24079. }, new GetCurrentViewExpr(), VariableFlags.None),
  24080. ]);
  24081. for (const op of unit.create) {
  24082. if (op.kind !== OpKind.Listener && op.kind !== OpKind.TwoWayListener) {
  24083. continue;
  24084. }
  24085. // Embedded views always need the save/restore view operation.
  24086. let needsRestoreView = unit !== job.root;
  24087. if (!needsRestoreView) {
  24088. for (const handlerOp of op.handlerOps) {
  24089. visitExpressionsInOp(handlerOp, (expr) => {
  24090. if (expr instanceof ReferenceExpr || expr instanceof ContextLetReferenceExpr) {
  24091. // Listeners that reference() a local ref need the save/restore view operation.
  24092. needsRestoreView = true;
  24093. }
  24094. });
  24095. }
  24096. }
  24097. if (needsRestoreView) {
  24098. addSaveRestoreViewOperationToListener(unit, op);
  24099. }
  24100. }
  24101. }
  24102. }
  24103. function addSaveRestoreViewOperationToListener(unit, op) {
  24104. op.handlerOps.prepend([
  24105. createVariableOp(unit.job.allocateXrefId(), {
  24106. kind: SemanticVariableKind.Context,
  24107. name: null,
  24108. view: unit.xref,
  24109. }, new RestoreViewExpr(unit.xref), VariableFlags.None),
  24110. ]);
  24111. // The "restore view" operation in listeners requires a call to `resetView` to reset the
  24112. // context prior to returning from the listener operation. Find any `return` statements in
  24113. // the listener body and wrap them in a call to reset the view.
  24114. for (const handlerOp of op.handlerOps) {
  24115. if (handlerOp.kind === OpKind.Statement &&
  24116. handlerOp.statement instanceof ReturnStatement) {
  24117. handlerOp.statement.value = new ResetViewExpr(handlerOp.statement.value);
  24118. }
  24119. }
  24120. }
  24121. /**
  24122. * Assign data slots for all operations which implement `ConsumesSlotOpTrait`, and propagate the
  24123. * assigned data slots of those operations to any expressions which reference them via
  24124. * `UsesSlotIndexTrait`.
  24125. *
  24126. * This phase is also responsible for counting the number of slots used for each view (its `decls`)
  24127. * and propagating that number into the `Template` operations which declare embedded views.
  24128. */
  24129. function allocateSlots(job) {
  24130. // Map of all declarations in all views within the component which require an assigned slot index.
  24131. // This map needs to be global (across all views within the component) since it's possible to
  24132. // reference a slot from one view from an expression within another (e.g. local references work
  24133. // this way).
  24134. const slotMap = new Map();
  24135. // Process all views in the component and assign slot indexes.
  24136. for (const unit of job.units) {
  24137. // Slot indices start at 0 for each view (and are not unique between views).
  24138. let slotCount = 0;
  24139. for (const op of unit.create) {
  24140. // Only consider declarations which consume data slots.
  24141. if (!hasConsumesSlotTrait(op)) {
  24142. continue;
  24143. }
  24144. // Assign slots to this declaration starting at the current `slotCount`.
  24145. op.handle.slot = slotCount;
  24146. // And track its assigned slot in the `slotMap`.
  24147. slotMap.set(op.xref, op.handle.slot);
  24148. // Each declaration may use more than 1 slot, so increment `slotCount` to reserve the number
  24149. // of slots required.
  24150. slotCount += op.numSlotsUsed;
  24151. }
  24152. // Record the total number of slots used on the view itself. This will later be propagated into
  24153. // `ir.TemplateOp`s which declare those views (except for the root view).
  24154. unit.decls = slotCount;
  24155. }
  24156. // After slot assignment, `slotMap` now contains slot assignments for every declaration in the
  24157. // whole template, across all views. Next, look for expressions which implement
  24158. // `UsesSlotIndexExprTrait` and propagate the assigned slot indexes into them.
  24159. // Additionally, this second scan allows us to find `ir.TemplateOp`s which declare views and
  24160. // propagate the number of slots used for each view into the operation which declares it.
  24161. for (const unit of job.units) {
  24162. for (const op of unit.ops()) {
  24163. if (op.kind === OpKind.Template || op.kind === OpKind.RepeaterCreate) {
  24164. // Record the number of slots used by the view this `ir.TemplateOp` declares in the
  24165. // operation itself, so it can be emitted later.
  24166. const childView = job.views.get(op.xref);
  24167. op.decls = childView.decls;
  24168. // TODO: currently we handle the decls for the RepeaterCreate empty template in the reify
  24169. // phase. We should handle that here instead.
  24170. }
  24171. }
  24172. }
  24173. }
  24174. /**
  24175. * Transforms special-case bindings with 'style' or 'class' in their names. Must run before the
  24176. * main binding specialization pass.
  24177. */
  24178. function specializeStyleBindings(job) {
  24179. for (const unit of job.units) {
  24180. for (const op of unit.update) {
  24181. if (op.kind !== OpKind.Binding) {
  24182. continue;
  24183. }
  24184. switch (op.bindingKind) {
  24185. case BindingKind.ClassName:
  24186. if (op.expression instanceof Interpolation) {
  24187. throw new Error(`Unexpected interpolation in ClassName binding`);
  24188. }
  24189. OpList.replace(op, createClassPropOp(op.target, op.name, op.expression, op.sourceSpan));
  24190. break;
  24191. case BindingKind.StyleProperty:
  24192. OpList.replace(op, createStylePropOp(op.target, op.name, op.expression, op.unit, op.sourceSpan));
  24193. break;
  24194. case BindingKind.Property:
  24195. case BindingKind.Template:
  24196. if (op.name === 'style') {
  24197. OpList.replace(op, createStyleMapOp(op.target, op.expression, op.sourceSpan));
  24198. }
  24199. else if (op.name === 'class') {
  24200. OpList.replace(op, createClassMapOp(op.target, op.expression, op.sourceSpan));
  24201. }
  24202. break;
  24203. }
  24204. }
  24205. }
  24206. }
  24207. /**
  24208. * Find all assignments and usages of temporary variables, which are linked to each other with cross
  24209. * references. Generate names for each cross-reference, and add a `DeclareVarStmt` to initialize
  24210. * them at the beginning of the update block.
  24211. *
  24212. * TODO: Sometimes, it will be possible to reuse names across different subexpressions. For example,
  24213. * in the double keyed read `a?.[f()]?.[f()]`, the two function calls have non-overlapping scopes.
  24214. * Implement an algorithm for reuse.
  24215. */
  24216. function generateTemporaryVariables(job) {
  24217. for (const unit of job.units) {
  24218. unit.create.prepend(generateTemporaries(unit.create));
  24219. unit.update.prepend(generateTemporaries(unit.update));
  24220. }
  24221. }
  24222. function generateTemporaries(ops) {
  24223. let opCount = 0;
  24224. let generatedStatements = [];
  24225. // For each op, search for any variables that are assigned or read. For each variable, generate a
  24226. // name and produce a `DeclareVarStmt` to the beginning of the block.
  24227. for (const op of ops) {
  24228. // Identify the final time each temp var is read.
  24229. const finalReads = new Map();
  24230. visitExpressionsInOp(op, (expr, flag) => {
  24231. if (flag & VisitorContextFlag.InChildOperation) {
  24232. return;
  24233. }
  24234. if (expr instanceof ReadTemporaryExpr) {
  24235. finalReads.set(expr.xref, expr);
  24236. }
  24237. });
  24238. // Name the temp vars, accounting for the fact that a name can be reused after it has been
  24239. // read for the final time.
  24240. let count = 0;
  24241. const assigned = new Set();
  24242. const released = new Set();
  24243. const defs = new Map();
  24244. visitExpressionsInOp(op, (expr, flag) => {
  24245. if (flag & VisitorContextFlag.InChildOperation) {
  24246. return;
  24247. }
  24248. if (expr instanceof AssignTemporaryExpr) {
  24249. if (!assigned.has(expr.xref)) {
  24250. assigned.add(expr.xref);
  24251. // TODO: Exactly replicate the naming scheme used by `TemplateDefinitionBuilder`.
  24252. // It seems to rely on an expression index instead of an op index.
  24253. defs.set(expr.xref, `tmp_${opCount}_${count++}`);
  24254. }
  24255. assignName(defs, expr);
  24256. }
  24257. else if (expr instanceof ReadTemporaryExpr) {
  24258. if (finalReads.get(expr.xref) === expr) {
  24259. released.add(expr.xref);
  24260. count--;
  24261. }
  24262. assignName(defs, expr);
  24263. }
  24264. });
  24265. // Add declarations for the temp vars.
  24266. generatedStatements.push(...Array.from(new Set(defs.values())).map((name) => createStatementOp(new DeclareVarStmt(name))));
  24267. opCount++;
  24268. if (op.kind === OpKind.Listener || op.kind === OpKind.TwoWayListener) {
  24269. op.handlerOps.prepend(generateTemporaries(op.handlerOps));
  24270. }
  24271. else if (op.kind === OpKind.RepeaterCreate && op.trackByOps !== null) {
  24272. op.trackByOps.prepend(generateTemporaries(op.trackByOps));
  24273. }
  24274. }
  24275. return generatedStatements;
  24276. }
  24277. /**
  24278. * Assigns a name to the temporary variable in the given temporary variable expression.
  24279. */
  24280. function assignName(names, expr) {
  24281. const name = names.get(expr.xref);
  24282. if (name === undefined) {
  24283. throw new Error(`Found xref with unassigned name: ${expr.xref}`);
  24284. }
  24285. expr.name = name;
  24286. }
  24287. /**
  24288. * `track` functions in `for` repeaters can sometimes be "optimized," i.e. transformed into inline
  24289. * expressions, in lieu of an external function call. For example, tracking by `$index` can be be
  24290. * optimized into an inline `trackByIndex` reference. This phase checks track expressions for
  24291. * optimizable cases.
  24292. */
  24293. function optimizeTrackFns(job) {
  24294. for (const unit of job.units) {
  24295. for (const op of unit.create) {
  24296. if (op.kind !== OpKind.RepeaterCreate) {
  24297. continue;
  24298. }
  24299. if (op.track instanceof ReadVarExpr && op.track.name === '$index') {
  24300. // Top-level access of `$index` uses the built in `repeaterTrackByIndex`.
  24301. op.trackByFn = importExpr(Identifiers.repeaterTrackByIndex);
  24302. }
  24303. else if (op.track instanceof ReadVarExpr && op.track.name === '$item') {
  24304. // Top-level access of the item uses the built in `repeaterTrackByIdentity`.
  24305. op.trackByFn = importExpr(Identifiers.repeaterTrackByIdentity);
  24306. }
  24307. else if (isTrackByFunctionCall(job.root.xref, op.track)) {
  24308. // Mark the function as using the component instance to play it safe
  24309. // since the method might be using `this` internally (see #53628).
  24310. op.usesComponentInstance = true;
  24311. // Top-level method calls in the form of `fn($index, item)` can be passed in directly.
  24312. if (op.track.receiver.receiver.view === unit.xref) {
  24313. // TODO: this may be wrong
  24314. op.trackByFn = op.track.receiver;
  24315. }
  24316. else {
  24317. // This is a plain method call, but not in the component's root view.
  24318. // We need to get the component instance, and then call the method on it.
  24319. op.trackByFn = importExpr(Identifiers.componentInstance)
  24320. .callFn([])
  24321. .prop(op.track.receiver.name);
  24322. // Because the context is not avaiable (without a special function), we don't want to
  24323. // try to resolve it later. Let's get rid of it by overwriting the original track
  24324. // expression (which won't be used anyway).
  24325. op.track = op.trackByFn;
  24326. }
  24327. }
  24328. else {
  24329. // The track function could not be optimized.
  24330. // Replace context reads with a special IR expression, since context reads in a track
  24331. // function are emitted specially.
  24332. op.track = transformExpressionsInExpression(op.track, (expr) => {
  24333. if (expr instanceof PipeBindingExpr || expr instanceof PipeBindingVariadicExpr) {
  24334. throw new Error(`Illegal State: Pipes are not allowed in this context`);
  24335. }
  24336. else if (expr instanceof ContextExpr) {
  24337. op.usesComponentInstance = true;
  24338. return new TrackContextExpr(expr.view);
  24339. }
  24340. return expr;
  24341. }, VisitorContextFlag.None);
  24342. // Also create an OpList for the tracking expression since it may need
  24343. // additional ops when generating the final code (e.g. temporary variables).
  24344. const trackOpList = new OpList();
  24345. trackOpList.push(createStatementOp(new ReturnStatement(op.track, op.track.sourceSpan)));
  24346. op.trackByOps = trackOpList;
  24347. }
  24348. }
  24349. }
  24350. }
  24351. function isTrackByFunctionCall(rootView, expr) {
  24352. if (!(expr instanceof InvokeFunctionExpr) || expr.args.length === 0 || expr.args.length > 2) {
  24353. return false;
  24354. }
  24355. if (!(expr.receiver instanceof ReadPropExpr && expr.receiver.receiver instanceof ContextExpr) ||
  24356. expr.receiver.receiver.view !== rootView) {
  24357. return false;
  24358. }
  24359. const [arg0, arg1] = expr.args;
  24360. if (!(arg0 instanceof ReadVarExpr) || arg0.name !== '$index') {
  24361. return false;
  24362. }
  24363. else if (expr.args.length === 1) {
  24364. return true;
  24365. }
  24366. if (!(arg1 instanceof ReadVarExpr) || arg1.name !== '$item') {
  24367. return false;
  24368. }
  24369. return true;
  24370. }
  24371. /**
  24372. * Inside the `track` expression on a `for` repeater, the `$index` and `$item` variables are
  24373. * ambiently available. In this phase, we find those variable usages, and replace them with the
  24374. * appropriate output read.
  24375. */
  24376. function generateTrackVariables(job) {
  24377. for (const unit of job.units) {
  24378. for (const op of unit.create) {
  24379. if (op.kind !== OpKind.RepeaterCreate) {
  24380. continue;
  24381. }
  24382. op.track = transformExpressionsInExpression(op.track, (expr) => {
  24383. if (expr instanceof LexicalReadExpr) {
  24384. if (op.varNames.$index.has(expr.name)) {
  24385. return variable('$index');
  24386. }
  24387. else if (expr.name === op.varNames.$implicit) {
  24388. return variable('$item');
  24389. }
  24390. // TODO: handle prohibited context variables (emit as globals?)
  24391. }
  24392. return expr;
  24393. }, VisitorContextFlag.None);
  24394. }
  24395. }
  24396. }
  24397. /**
  24398. * Counts the number of variable slots used within each view, and stores that on the view itself, as
  24399. * well as propagates it to the `ir.TemplateOp` for embedded views.
  24400. */
  24401. function countVariables(job) {
  24402. // First, count the vars used in each view, and update the view-level counter.
  24403. for (const unit of job.units) {
  24404. let varCount = 0;
  24405. // Count variables on top-level ops first. Don't explore nested expressions just yet.
  24406. for (const op of unit.ops()) {
  24407. if (hasConsumesVarsTrait(op)) {
  24408. varCount += varsUsedByOp(op);
  24409. }
  24410. }
  24411. // Count variables on expressions inside ops. We do this later because some of these expressions
  24412. // might be conditional (e.g. `pipeBinding` inside of a ternary), and we don't want to interfere
  24413. // with indices for top-level binding slots (e.g. `property`).
  24414. for (const op of unit.ops()) {
  24415. visitExpressionsInOp(op, (expr) => {
  24416. if (!isIrExpression(expr)) {
  24417. return;
  24418. }
  24419. // TemplateDefinitionBuilder assigns variable offsets for everything but pure functions
  24420. // first, and then assigns offsets to pure functions lazily. We emulate that behavior by
  24421. // assigning offsets in two passes instead of one, only in compatibility mode.
  24422. if (job.compatibility === CompatibilityMode.TemplateDefinitionBuilder &&
  24423. expr instanceof PureFunctionExpr) {
  24424. return;
  24425. }
  24426. // Some expressions require knowledge of the number of variable slots consumed.
  24427. if (hasUsesVarOffsetTrait(expr)) {
  24428. expr.varOffset = varCount;
  24429. }
  24430. if (hasConsumesVarsTrait(expr)) {
  24431. varCount += varsUsedByIrExpression(expr);
  24432. }
  24433. });
  24434. }
  24435. // Compatibility mode pass for pure function offsets (as explained above).
  24436. if (job.compatibility === CompatibilityMode.TemplateDefinitionBuilder) {
  24437. for (const op of unit.ops()) {
  24438. visitExpressionsInOp(op, (expr) => {
  24439. if (!isIrExpression(expr) || !(expr instanceof PureFunctionExpr)) {
  24440. return;
  24441. }
  24442. // Some expressions require knowledge of the number of variable slots consumed.
  24443. if (hasUsesVarOffsetTrait(expr)) {
  24444. expr.varOffset = varCount;
  24445. }
  24446. if (hasConsumesVarsTrait(expr)) {
  24447. varCount += varsUsedByIrExpression(expr);
  24448. }
  24449. });
  24450. }
  24451. }
  24452. unit.vars = varCount;
  24453. }
  24454. if (job instanceof ComponentCompilationJob) {
  24455. // Add var counts for each view to the `ir.TemplateOp` which declares that view (if the view is
  24456. // an embedded view).
  24457. for (const unit of job.units) {
  24458. for (const op of unit.create) {
  24459. if (op.kind !== OpKind.Template && op.kind !== OpKind.RepeaterCreate) {
  24460. continue;
  24461. }
  24462. const childView = job.views.get(op.xref);
  24463. op.vars = childView.vars;
  24464. // TODO: currently we handle the vars for the RepeaterCreate empty template in the reify
  24465. // phase. We should handle that here instead.
  24466. }
  24467. }
  24468. }
  24469. }
  24470. /**
  24471. * Different operations that implement `ir.UsesVarsTrait` use different numbers of variables, so
  24472. * count the variables used by any particular `op`.
  24473. */
  24474. function varsUsedByOp(op) {
  24475. let slots;
  24476. switch (op.kind) {
  24477. case OpKind.Property:
  24478. case OpKind.HostProperty:
  24479. case OpKind.Attribute:
  24480. // All of these bindings use 1 variable slot, plus 1 slot for every interpolated expression,
  24481. // if any.
  24482. slots = 1;
  24483. if (op.expression instanceof Interpolation && !isSingletonInterpolation(op.expression)) {
  24484. slots += op.expression.expressions.length;
  24485. }
  24486. return slots;
  24487. case OpKind.TwoWayProperty:
  24488. // Two-way properties can only have expressions so they only need one variable slot.
  24489. return 1;
  24490. case OpKind.StyleProp:
  24491. case OpKind.ClassProp:
  24492. case OpKind.StyleMap:
  24493. case OpKind.ClassMap:
  24494. // Style & class bindings use 2 variable slots, plus 1 slot for every interpolated expression,
  24495. // if any.
  24496. slots = 2;
  24497. if (op.expression instanceof Interpolation) {
  24498. slots += op.expression.expressions.length;
  24499. }
  24500. return slots;
  24501. case OpKind.InterpolateText:
  24502. // `ir.InterpolateTextOp`s use a variable slot for each dynamic expression.
  24503. return op.interpolation.expressions.length;
  24504. case OpKind.I18nExpression:
  24505. case OpKind.Conditional:
  24506. case OpKind.DeferWhen:
  24507. case OpKind.StoreLet:
  24508. return 1;
  24509. case OpKind.RepeaterCreate:
  24510. // Repeaters may require an extra variable binding slot, if they have an empty view, for the
  24511. // empty block tracking.
  24512. // TODO: It's a bit odd to have a create mode instruction consume variable slots. Maybe we can
  24513. // find a way to use the Repeater update op instead.
  24514. return op.emptyView ? 1 : 0;
  24515. default:
  24516. throw new Error(`Unhandled op: ${OpKind[op.kind]}`);
  24517. }
  24518. }
  24519. function varsUsedByIrExpression(expr) {
  24520. switch (expr.kind) {
  24521. case ExpressionKind.PureFunctionExpr:
  24522. return 1 + expr.args.length;
  24523. case ExpressionKind.PipeBinding:
  24524. return 1 + expr.args.length;
  24525. case ExpressionKind.PipeBindingVariadic:
  24526. return 1 + expr.numArgs;
  24527. case ExpressionKind.StoreLet:
  24528. return 1;
  24529. default:
  24530. throw new Error(`AssertionError: unhandled ConsumesVarsTrait expression ${expr.constructor.name}`);
  24531. }
  24532. }
  24533. function isSingletonInterpolation(expr) {
  24534. if (expr.expressions.length !== 1 || expr.strings.length !== 2) {
  24535. return false;
  24536. }
  24537. if (expr.strings[0] !== '' || expr.strings[1] !== '') {
  24538. return false;
  24539. }
  24540. return true;
  24541. }
  24542. /**
  24543. * Optimize variables declared and used in the IR.
  24544. *
  24545. * Variables are eagerly generated by pipeline stages for all possible values that could be
  24546. * referenced. This stage processes the list of declared variables and all variable usages,
  24547. * and optimizes where possible. It performs 3 main optimizations:
  24548. *
  24549. * * It transforms variable declarations to side effectful expressions when the
  24550. * variable is not used, but its initializer has global effects which other
  24551. * operations rely upon.
  24552. * * It removes variable declarations if those variables are not referenced and
  24553. * either they do not have global effects, or nothing relies on them.
  24554. * * It inlines variable declarations when those variables are only used once
  24555. * and the inlining is semantically safe.
  24556. *
  24557. * To guarantee correctness, analysis of "fences" in the instruction lists is used to determine
  24558. * which optimizations are safe to perform.
  24559. */
  24560. function optimizeVariables(job) {
  24561. for (const unit of job.units) {
  24562. inlineAlwaysInlineVariables(unit.create);
  24563. inlineAlwaysInlineVariables(unit.update);
  24564. for (const op of unit.create) {
  24565. if (op.kind === OpKind.Listener || op.kind === OpKind.TwoWayListener) {
  24566. inlineAlwaysInlineVariables(op.handlerOps);
  24567. }
  24568. else if (op.kind === OpKind.RepeaterCreate && op.trackByOps !== null) {
  24569. inlineAlwaysInlineVariables(op.trackByOps);
  24570. }
  24571. }
  24572. optimizeVariablesInOpList(unit.create, job.compatibility);
  24573. optimizeVariablesInOpList(unit.update, job.compatibility);
  24574. for (const op of unit.create) {
  24575. if (op.kind === OpKind.Listener || op.kind === OpKind.TwoWayListener) {
  24576. optimizeVariablesInOpList(op.handlerOps, job.compatibility);
  24577. }
  24578. else if (op.kind === OpKind.RepeaterCreate && op.trackByOps !== null) {
  24579. optimizeVariablesInOpList(op.trackByOps, job.compatibility);
  24580. }
  24581. }
  24582. }
  24583. }
  24584. /**
  24585. * A [fence](https://en.wikipedia.org/wiki/Memory_barrier) flag for an expression which indicates
  24586. * how that expression can be optimized in relation to other expressions or instructions.
  24587. *
  24588. * `Fence`s are a bitfield, so multiple flags may be set on a single expression.
  24589. */
  24590. var Fence;
  24591. (function (Fence) {
  24592. /**
  24593. * Empty flag (no fence exists).
  24594. */
  24595. Fence[Fence["None"] = 0] = "None";
  24596. /**
  24597. * A context read fence, meaning that the expression in question reads from the "current view"
  24598. * context of the runtime.
  24599. */
  24600. Fence[Fence["ViewContextRead"] = 1] = "ViewContextRead";
  24601. /**
  24602. * A context write fence, meaning that the expression in question writes to the "current view"
  24603. * context of the runtime.
  24604. *
  24605. * Note that all `ContextWrite` fences are implicitly `ContextRead` fences as operations which
  24606. * change the view context do so based on the current one.
  24607. */
  24608. Fence[Fence["ViewContextWrite"] = 2] = "ViewContextWrite";
  24609. /**
  24610. * Indicates that a call is required for its side-effects, even if nothing reads its result.
  24611. *
  24612. * This is also true of `ViewContextWrite` operations **if** they are followed by a
  24613. * `ViewContextRead`.
  24614. */
  24615. Fence[Fence["SideEffectful"] = 4] = "SideEffectful";
  24616. })(Fence || (Fence = {}));
  24617. function inlineAlwaysInlineVariables(ops) {
  24618. const vars = new Map();
  24619. for (const op of ops) {
  24620. if (op.kind === OpKind.Variable && op.flags & VariableFlags.AlwaysInline) {
  24621. visitExpressionsInOp(op, (expr) => {
  24622. if (isIrExpression(expr) && fencesForIrExpression(expr) !== Fence.None) {
  24623. throw new Error(`AssertionError: A context-sensitive variable was marked AlwaysInline`);
  24624. }
  24625. });
  24626. vars.set(op.xref, op);
  24627. }
  24628. transformExpressionsInOp(op, (expr) => {
  24629. if (expr instanceof ReadVariableExpr && vars.has(expr.xref)) {
  24630. const varOp = vars.get(expr.xref);
  24631. // Inline by cloning, because we might inline into multiple places.
  24632. return varOp.initializer.clone();
  24633. }
  24634. return expr;
  24635. }, VisitorContextFlag.None);
  24636. }
  24637. for (const op of vars.values()) {
  24638. OpList.remove(op);
  24639. }
  24640. }
  24641. /**
  24642. * Process a list of operations and optimize variables within that list.
  24643. */
  24644. function optimizeVariablesInOpList(ops, compatibility) {
  24645. const varDecls = new Map();
  24646. const varUsages = new Map();
  24647. // Track variables that are used outside of the immediate operation list. For example, within
  24648. // `ListenerOp` handler operations of listeners in the current operation list.
  24649. const varRemoteUsages = new Set();
  24650. const opMap = new Map();
  24651. // First, extract information about variables declared or used within the whole list.
  24652. for (const op of ops) {
  24653. if (op.kind === OpKind.Variable) {
  24654. if (varDecls.has(op.xref) || varUsages.has(op.xref)) {
  24655. throw new Error(`Should not see two declarations of the same variable: ${op.xref}`);
  24656. }
  24657. varDecls.set(op.xref, op);
  24658. varUsages.set(op.xref, 0);
  24659. }
  24660. opMap.set(op, collectOpInfo(op));
  24661. countVariableUsages(op, varUsages, varRemoteUsages);
  24662. }
  24663. // The next step is to remove any variable declarations for variables that aren't used. The
  24664. // variable initializer expressions may be side-effectful, so they may need to be retained as
  24665. // expression statements.
  24666. // Track whether we've seen an operation which reads from the view context yet. This is used to
  24667. // determine whether a write to the view context in a variable initializer can be observed.
  24668. let contextIsUsed = false;
  24669. // Note that iteration through the list happens in reverse, which guarantees that we'll process
  24670. // all reads of a variable prior to processing its declaration.
  24671. for (const op of ops.reversed()) {
  24672. const opInfo = opMap.get(op);
  24673. if (op.kind === OpKind.Variable && varUsages.get(op.xref) === 0) {
  24674. // This variable is unused and can be removed. We might need to keep the initializer around,
  24675. // though, if something depends on it running.
  24676. if ((contextIsUsed && opInfo.fences & Fence.ViewContextWrite) ||
  24677. opInfo.fences & Fence.SideEffectful) {
  24678. // This variable initializer has a side effect which must be retained. Either:
  24679. // * it writes to the view context, and we know there is a future operation which depends
  24680. // on that write, or
  24681. // * it's an operation which is inherently side-effectful.
  24682. // We can't remove the initializer, but we can remove the variable declaration itself and
  24683. // replace it with a side-effectful statement.
  24684. const stmtOp = createStatementOp(op.initializer.toStmt());
  24685. opMap.set(stmtOp, opInfo);
  24686. OpList.replace(op, stmtOp);
  24687. }
  24688. else {
  24689. // It's safe to delete this entire variable declaration as nothing depends on it, even
  24690. // side-effectfully. Note that doing this might make other variables unused. Since we're
  24691. // iterating in reverse order, we should always be processing usages before declarations
  24692. // and therefore by the time we get to a declaration, all removable usages will have been
  24693. // removed.
  24694. uncountVariableUsages(op, varUsages);
  24695. OpList.remove(op);
  24696. }
  24697. opMap.delete(op);
  24698. varDecls.delete(op.xref);
  24699. varUsages.delete(op.xref);
  24700. continue;
  24701. }
  24702. // Does this operation depend on the view context?
  24703. if (opInfo.fences & Fence.ViewContextRead) {
  24704. contextIsUsed = true;
  24705. }
  24706. }
  24707. // Next, inline any remaining variables with exactly one usage.
  24708. const toInline = [];
  24709. for (const [id, count] of varUsages) {
  24710. const decl = varDecls.get(id);
  24711. // We can inline variables that:
  24712. // - are used exactly once, and
  24713. // - are not used remotely
  24714. // OR
  24715. // - are marked for always inlining
  24716. const isAlwaysInline = !!(decl.flags & VariableFlags.AlwaysInline);
  24717. if (count !== 1 || isAlwaysInline) {
  24718. // We can't inline this variable as it's used more than once.
  24719. continue;
  24720. }
  24721. if (varRemoteUsages.has(id)) {
  24722. // This variable is used once, but across an operation boundary, so it can't be inlined.
  24723. continue;
  24724. }
  24725. toInline.push(id);
  24726. }
  24727. let candidate;
  24728. while ((candidate = toInline.pop())) {
  24729. // We will attempt to inline this variable. If inlining fails (due to fences for example),
  24730. // no future operation will make inlining legal.
  24731. const decl = varDecls.get(candidate);
  24732. const varInfo = opMap.get(decl);
  24733. const isAlwaysInline = !!(decl.flags & VariableFlags.AlwaysInline);
  24734. if (isAlwaysInline) {
  24735. throw new Error(`AssertionError: Found an 'AlwaysInline' variable after the always inlining pass.`);
  24736. }
  24737. // Scan operations following the variable declaration and look for the point where that variable
  24738. // is used. There should only be one usage given the precondition above.
  24739. for (let targetOp = decl.next; targetOp.kind !== OpKind.ListEnd; targetOp = targetOp.next) {
  24740. const opInfo = opMap.get(targetOp);
  24741. // Is the variable used in this operation?
  24742. if (opInfo.variablesUsed.has(candidate)) {
  24743. if (compatibility === CompatibilityMode.TemplateDefinitionBuilder &&
  24744. !allowConservativeInlining(decl, targetOp)) {
  24745. // We're in conservative mode, and this variable is not eligible for inlining into the
  24746. // target operation in this mode.
  24747. break;
  24748. }
  24749. // Yes, try to inline it. Inlining may not be successful if fences in this operation before
  24750. // the variable's usage cannot be safely crossed.
  24751. if (tryInlineVariableInitializer(candidate, decl.initializer, targetOp, varInfo.fences)) {
  24752. // Inlining was successful! Update the tracking structures to reflect the inlined
  24753. // variable.
  24754. opInfo.variablesUsed.delete(candidate);
  24755. // Add all variables used in the variable's initializer to its new usage site.
  24756. for (const id of varInfo.variablesUsed) {
  24757. opInfo.variablesUsed.add(id);
  24758. }
  24759. // Merge fences in the variable's initializer into its new usage site.
  24760. opInfo.fences |= varInfo.fences;
  24761. // Delete tracking info related to the declaration.
  24762. varDecls.delete(candidate);
  24763. varUsages.delete(candidate);
  24764. opMap.delete(decl);
  24765. // And finally, delete the original declaration from the operation list.
  24766. OpList.remove(decl);
  24767. }
  24768. // Whether inlining succeeded or failed, we're done processing this variable.
  24769. break;
  24770. }
  24771. // If the variable is not used in this operation, then we'd need to inline across it. Check if
  24772. // that's safe to do.
  24773. if (!safeToInlinePastFences(opInfo.fences, varInfo.fences)) {
  24774. // We can't safely inline this variable beyond this operation, so don't proceed with
  24775. // inlining this variable.
  24776. break;
  24777. }
  24778. }
  24779. }
  24780. }
  24781. /**
  24782. * Given an `ir.Expression`, returns the `Fence` flags for that expression type.
  24783. */
  24784. function fencesForIrExpression(expr) {
  24785. switch (expr.kind) {
  24786. case ExpressionKind.NextContext:
  24787. return Fence.ViewContextRead | Fence.ViewContextWrite;
  24788. case ExpressionKind.RestoreView:
  24789. return Fence.ViewContextRead | Fence.ViewContextWrite | Fence.SideEffectful;
  24790. case ExpressionKind.StoreLet:
  24791. return Fence.SideEffectful;
  24792. case ExpressionKind.Reference:
  24793. case ExpressionKind.ContextLetReference:
  24794. return Fence.ViewContextRead;
  24795. default:
  24796. return Fence.None;
  24797. }
  24798. }
  24799. /**
  24800. * Build the `OpInfo` structure for the given `op`. This performs two operations:
  24801. *
  24802. * * It tracks which variables are used in the operation's expressions.
  24803. * * It rolls up fence flags for expressions within the operation.
  24804. */
  24805. function collectOpInfo(op) {
  24806. let fences = Fence.None;
  24807. const variablesUsed = new Set();
  24808. visitExpressionsInOp(op, (expr) => {
  24809. if (!isIrExpression(expr)) {
  24810. return;
  24811. }
  24812. switch (expr.kind) {
  24813. case ExpressionKind.ReadVariable:
  24814. variablesUsed.add(expr.xref);
  24815. break;
  24816. default:
  24817. fences |= fencesForIrExpression(expr);
  24818. }
  24819. });
  24820. return { fences, variablesUsed };
  24821. }
  24822. /**
  24823. * Count the number of usages of each variable, being careful to track whether those usages are
  24824. * local or remote.
  24825. */
  24826. function countVariableUsages(op, varUsages, varRemoteUsage) {
  24827. visitExpressionsInOp(op, (expr, flags) => {
  24828. if (!isIrExpression(expr)) {
  24829. return;
  24830. }
  24831. if (expr.kind !== ExpressionKind.ReadVariable) {
  24832. return;
  24833. }
  24834. const count = varUsages.get(expr.xref);
  24835. if (count === undefined) {
  24836. // This variable is declared outside the current scope of optimization.
  24837. return;
  24838. }
  24839. varUsages.set(expr.xref, count + 1);
  24840. if (flags & VisitorContextFlag.InChildOperation) {
  24841. varRemoteUsage.add(expr.xref);
  24842. }
  24843. });
  24844. }
  24845. /**
  24846. * Remove usages of a variable in `op` from the `varUsages` tracking.
  24847. */
  24848. function uncountVariableUsages(op, varUsages) {
  24849. visitExpressionsInOp(op, (expr) => {
  24850. if (!isIrExpression(expr)) {
  24851. return;
  24852. }
  24853. if (expr.kind !== ExpressionKind.ReadVariable) {
  24854. return;
  24855. }
  24856. const count = varUsages.get(expr.xref);
  24857. if (count === undefined) {
  24858. // This variable is declared outside the current scope of optimization.
  24859. return;
  24860. }
  24861. else if (count === 0) {
  24862. throw new Error(`Inaccurate variable count: ${expr.xref} - found another read but count is already 0`);
  24863. }
  24864. varUsages.set(expr.xref, count - 1);
  24865. });
  24866. }
  24867. /**
  24868. * Checks whether it's safe to inline a variable across a particular operation.
  24869. *
  24870. * @param fences the fences of the operation which the inlining will cross
  24871. * @param declFences the fences of the variable being inlined.
  24872. */
  24873. function safeToInlinePastFences(fences, declFences) {
  24874. if (fences & Fence.ViewContextWrite) {
  24875. // It's not safe to inline context reads across context writes.
  24876. if (declFences & Fence.ViewContextRead) {
  24877. return false;
  24878. }
  24879. }
  24880. else if (fences & Fence.ViewContextRead) {
  24881. // It's not safe to inline context writes across context reads.
  24882. if (declFences & Fence.ViewContextWrite) {
  24883. return false;
  24884. }
  24885. }
  24886. return true;
  24887. }
  24888. /**
  24889. * Attempt to inline the initializer of a variable into a target operation's expressions.
  24890. *
  24891. * This may or may not be safe to do. For example, the variable could be read following the
  24892. * execution of an expression with fences that don't permit the variable to be inlined across them.
  24893. */
  24894. function tryInlineVariableInitializer(id, initializer, target, declFences) {
  24895. // We use `ir.transformExpressionsInOp` to walk the expressions and inline the variable if
  24896. // possible. Since this operation is callback-based, once inlining succeeds or fails we can't
  24897. // "stop" the expression processing, and have to keep track of whether inlining has succeeded or
  24898. // is no longer allowed.
  24899. let inlined = false;
  24900. let inliningAllowed = true;
  24901. transformExpressionsInOp(target, (expr, flags) => {
  24902. if (!isIrExpression(expr)) {
  24903. return expr;
  24904. }
  24905. if (inlined || !inliningAllowed) {
  24906. // Either the inlining has already succeeded, or we've passed a fence that disallows inlining
  24907. // at this point, so don't try.
  24908. return expr;
  24909. }
  24910. else if (flags & VisitorContextFlag.InChildOperation &&
  24911. declFences & Fence.ViewContextRead) {
  24912. // We cannot inline variables that are sensitive to the current context across operation
  24913. // boundaries.
  24914. return expr;
  24915. }
  24916. switch (expr.kind) {
  24917. case ExpressionKind.ReadVariable:
  24918. if (expr.xref === id) {
  24919. // This is the usage site of the variable. Since nothing has disallowed inlining, it's
  24920. // safe to inline the initializer here.
  24921. inlined = true;
  24922. return initializer;
  24923. }
  24924. break;
  24925. default:
  24926. // For other types of `ir.Expression`s, whether inlining is allowed depends on their fences.
  24927. const exprFences = fencesForIrExpression(expr);
  24928. inliningAllowed = inliningAllowed && safeToInlinePastFences(exprFences, declFences);
  24929. break;
  24930. }
  24931. return expr;
  24932. }, VisitorContextFlag.None);
  24933. return inlined;
  24934. }
  24935. /**
  24936. * Determines whether inlining of `decl` should be allowed in "conservative" mode.
  24937. *
  24938. * In conservative mode, inlining behavior is limited to those operations which the
  24939. * `TemplateDefinitionBuilder` supported, with the goal of producing equivalent output.
  24940. */
  24941. function allowConservativeInlining(decl, target) {
  24942. // TODO(alxhub): understand exactly how TemplateDefinitionBuilder approaches inlining, and record
  24943. // that behavior here.
  24944. switch (decl.variable.kind) {
  24945. case SemanticVariableKind.Identifier:
  24946. if (decl.initializer instanceof ReadVarExpr && decl.initializer.name === 'ctx') {
  24947. // Although TemplateDefinitionBuilder is cautious about inlining, we still want to do so
  24948. // when the variable is the context, to imitate its behavior with aliases in control flow
  24949. // blocks. This quirky behavior will become dead code once compatibility mode is no longer
  24950. // supported.
  24951. return true;
  24952. }
  24953. return false;
  24954. case SemanticVariableKind.Context:
  24955. // Context can only be inlined into other variables.
  24956. return target.kind === OpKind.Variable;
  24957. default:
  24958. return true;
  24959. }
  24960. }
  24961. /**
  24962. * Wraps ICUs that do not already belong to an i18n block in a new i18n block.
  24963. */
  24964. function wrapI18nIcus(job) {
  24965. for (const unit of job.units) {
  24966. let currentI18nOp = null;
  24967. let addedI18nId = null;
  24968. for (const op of unit.create) {
  24969. switch (op.kind) {
  24970. case OpKind.I18nStart:
  24971. currentI18nOp = op;
  24972. break;
  24973. case OpKind.I18nEnd:
  24974. currentI18nOp = null;
  24975. break;
  24976. case OpKind.IcuStart:
  24977. if (currentI18nOp === null) {
  24978. addedI18nId = job.allocateXrefId();
  24979. // ICU i18n start/end ops should not receive source spans.
  24980. OpList.insertBefore(createI18nStartOp(addedI18nId, op.message, undefined, null), op);
  24981. }
  24982. break;
  24983. case OpKind.IcuEnd:
  24984. if (addedI18nId !== null) {
  24985. OpList.insertAfter(createI18nEndOp(addedI18nId, null), op);
  24986. addedI18nId = null;
  24987. }
  24988. break;
  24989. }
  24990. }
  24991. }
  24992. }
  24993. /*!
  24994. * @license
  24995. * Copyright Google LLC All Rights Reserved.
  24996. *
  24997. * Use of this source code is governed by an MIT-style license that can be
  24998. * found in the LICENSE file at https://angular.dev/license
  24999. */
  25000. /**
  25001. * Removes any `storeLet` calls that aren't referenced outside of the current view.
  25002. */
  25003. function optimizeStoreLet(job) {
  25004. const letUsedExternally = new Set();
  25005. // Since `@let` declarations can be referenced in child views, both in
  25006. // the creation block (via listeners) and in the update block, we have
  25007. // to look through all the ops to find the references.
  25008. for (const unit of job.units) {
  25009. for (const op of unit.ops()) {
  25010. visitExpressionsInOp(op, (expr) => {
  25011. if (expr instanceof ContextLetReferenceExpr) {
  25012. letUsedExternally.add(expr.target);
  25013. }
  25014. });
  25015. }
  25016. }
  25017. // TODO(crisbeto): potentially remove the unused calls completely, pending discussion.
  25018. for (const unit of job.units) {
  25019. for (const op of unit.update) {
  25020. transformExpressionsInOp(op, (expression) => expression instanceof StoreLetExpr && !letUsedExternally.has(expression.target)
  25021. ? expression.value
  25022. : expression, VisitorContextFlag.None);
  25023. }
  25024. }
  25025. }
  25026. /**
  25027. * It's not allowed to access a `@let` declaration before it has been defined. This is enforced
  25028. * already via template type checking, however it can trip some of the assertions in the pipeline.
  25029. * E.g. the naming phase can fail because we resolved the variable here, but the variable doesn't
  25030. * exist anymore because the optimization phase removed it since it's invalid. To avoid surfacing
  25031. * confusing errors to users in the case where template type checking isn't running (e.g. in JIT
  25032. * mode) this phase detects illegal forward references and replaces them with `undefined`.
  25033. * Eventually users will see the proper error from the template type checker.
  25034. */
  25035. function removeIllegalLetReferences(job) {
  25036. for (const unit of job.units) {
  25037. for (const op of unit.update) {
  25038. if (op.kind !== OpKind.Variable ||
  25039. op.variable.kind !== SemanticVariableKind.Identifier ||
  25040. !(op.initializer instanceof StoreLetExpr)) {
  25041. continue;
  25042. }
  25043. const name = op.variable.identifier;
  25044. let current = op;
  25045. while (current && current.kind !== OpKind.ListEnd) {
  25046. transformExpressionsInOp(current, (expr) => expr instanceof LexicalReadExpr && expr.name === name ? literal(undefined) : expr, VisitorContextFlag.None);
  25047. current = current.prev;
  25048. }
  25049. }
  25050. }
  25051. }
  25052. /**
  25053. * Replaces the `storeLet` ops with variables that can be
  25054. * used to reference the value within the same view.
  25055. */
  25056. function generateLocalLetReferences(job) {
  25057. for (const unit of job.units) {
  25058. for (const op of unit.update) {
  25059. if (op.kind !== OpKind.StoreLet) {
  25060. continue;
  25061. }
  25062. const variable = {
  25063. kind: SemanticVariableKind.Identifier,
  25064. name: null,
  25065. identifier: op.declaredName,
  25066. local: true,
  25067. };
  25068. OpList.replace(op, createVariableOp(job.allocateXrefId(), variable, new StoreLetExpr(op.target, op.value, op.sourceSpan), VariableFlags.None));
  25069. }
  25070. }
  25071. }
  25072. /**
  25073. * Locates all of the elements defined in a creation block and outputs an op
  25074. * that will expose their definition location in the DOM.
  25075. */
  25076. function attachSourceLocations(job) {
  25077. if (!job.enableDebugLocations || job.relativeTemplatePath === null) {
  25078. return;
  25079. }
  25080. for (const unit of job.units) {
  25081. const locations = [];
  25082. for (const op of unit.create) {
  25083. if (op.kind === OpKind.ElementStart || op.kind === OpKind.Element) {
  25084. const start = op.startSourceSpan.start;
  25085. locations.push({
  25086. targetSlot: op.handle,
  25087. offset: start.offset,
  25088. line: start.line,
  25089. column: start.col,
  25090. });
  25091. }
  25092. }
  25093. if (locations.length > 0) {
  25094. unit.create.push(createSourceLocationOp(job.relativeTemplatePath, locations));
  25095. }
  25096. }
  25097. }
  25098. /**
  25099. *
  25100. * @license
  25101. * Copyright Google LLC All Rights Reserved.
  25102. *
  25103. * Use of this source code is governed by an MIT-style license that can be
  25104. * found in the LICENSE file at https://angular.dev/license
  25105. */
  25106. const phases = [
  25107. { kind: CompilationJobKind.Tmpl, fn: removeContentSelectors },
  25108. { kind: CompilationJobKind.Host, fn: parseHostStyleProperties },
  25109. { kind: CompilationJobKind.Tmpl, fn: emitNamespaceChanges },
  25110. { kind: CompilationJobKind.Tmpl, fn: propagateI18nBlocks },
  25111. { kind: CompilationJobKind.Tmpl, fn: wrapI18nIcus },
  25112. { kind: CompilationJobKind.Both, fn: deduplicateTextBindings },
  25113. { kind: CompilationJobKind.Both, fn: specializeStyleBindings },
  25114. { kind: CompilationJobKind.Both, fn: specializeBindings },
  25115. { kind: CompilationJobKind.Both, fn: extractAttributes },
  25116. { kind: CompilationJobKind.Tmpl, fn: createI18nContexts },
  25117. { kind: CompilationJobKind.Both, fn: parseExtractedStyles },
  25118. { kind: CompilationJobKind.Tmpl, fn: removeEmptyBindings },
  25119. { kind: CompilationJobKind.Both, fn: collapseSingletonInterpolations },
  25120. { kind: CompilationJobKind.Both, fn: orderOps },
  25121. { kind: CompilationJobKind.Tmpl, fn: generateConditionalExpressions },
  25122. { kind: CompilationJobKind.Tmpl, fn: createPipes },
  25123. { kind: CompilationJobKind.Tmpl, fn: configureDeferInstructions },
  25124. { kind: CompilationJobKind.Tmpl, fn: convertI18nText },
  25125. { kind: CompilationJobKind.Tmpl, fn: convertI18nBindings },
  25126. { kind: CompilationJobKind.Tmpl, fn: removeUnusedI18nAttributesOps },
  25127. { kind: CompilationJobKind.Tmpl, fn: assignI18nSlotDependencies },
  25128. { kind: CompilationJobKind.Tmpl, fn: applyI18nExpressions },
  25129. { kind: CompilationJobKind.Tmpl, fn: createVariadicPipes },
  25130. { kind: CompilationJobKind.Both, fn: generatePureLiteralStructures },
  25131. { kind: CompilationJobKind.Tmpl, fn: generateProjectionDefs },
  25132. { kind: CompilationJobKind.Tmpl, fn: generateLocalLetReferences },
  25133. { kind: CompilationJobKind.Tmpl, fn: generateVariables },
  25134. { kind: CompilationJobKind.Tmpl, fn: saveAndRestoreView },
  25135. { kind: CompilationJobKind.Both, fn: deleteAnyCasts },
  25136. { kind: CompilationJobKind.Both, fn: resolveDollarEvent },
  25137. { kind: CompilationJobKind.Tmpl, fn: generateTrackVariables },
  25138. { kind: CompilationJobKind.Tmpl, fn: removeIllegalLetReferences },
  25139. { kind: CompilationJobKind.Both, fn: resolveNames },
  25140. { kind: CompilationJobKind.Tmpl, fn: resolveDeferTargetNames },
  25141. { kind: CompilationJobKind.Tmpl, fn: transformTwoWayBindingSet },
  25142. { kind: CompilationJobKind.Tmpl, fn: optimizeTrackFns },
  25143. { kind: CompilationJobKind.Both, fn: resolveContexts },
  25144. { kind: CompilationJobKind.Both, fn: resolveSanitizers },
  25145. { kind: CompilationJobKind.Tmpl, fn: liftLocalRefs },
  25146. { kind: CompilationJobKind.Both, fn: generateNullishCoalesceExpressions },
  25147. { kind: CompilationJobKind.Both, fn: expandSafeReads },
  25148. { kind: CompilationJobKind.Both, fn: generateTemporaryVariables },
  25149. { kind: CompilationJobKind.Both, fn: optimizeVariables },
  25150. { kind: CompilationJobKind.Both, fn: optimizeStoreLet },
  25151. { kind: CompilationJobKind.Tmpl, fn: allocateSlots },
  25152. { kind: CompilationJobKind.Tmpl, fn: resolveI18nElementPlaceholders },
  25153. { kind: CompilationJobKind.Tmpl, fn: resolveI18nExpressionPlaceholders },
  25154. { kind: CompilationJobKind.Tmpl, fn: extractI18nMessages },
  25155. { kind: CompilationJobKind.Tmpl, fn: collectI18nConsts },
  25156. { kind: CompilationJobKind.Tmpl, fn: collectConstExpressions },
  25157. { kind: CompilationJobKind.Both, fn: collectElementConsts },
  25158. { kind: CompilationJobKind.Tmpl, fn: removeI18nContexts },
  25159. { kind: CompilationJobKind.Both, fn: countVariables },
  25160. { kind: CompilationJobKind.Tmpl, fn: generateAdvance },
  25161. { kind: CompilationJobKind.Both, fn: nameFunctionsAndVariables },
  25162. { kind: CompilationJobKind.Tmpl, fn: resolveDeferDepsFns },
  25163. { kind: CompilationJobKind.Tmpl, fn: mergeNextContextExpressions },
  25164. { kind: CompilationJobKind.Tmpl, fn: generateNgContainerOps },
  25165. { kind: CompilationJobKind.Tmpl, fn: collapseEmptyInstructions },
  25166. { kind: CompilationJobKind.Tmpl, fn: attachSourceLocations },
  25167. { kind: CompilationJobKind.Tmpl, fn: disableBindings$1 },
  25168. { kind: CompilationJobKind.Both, fn: extractPureFunctions },
  25169. { kind: CompilationJobKind.Both, fn: reify },
  25170. { kind: CompilationJobKind.Both, fn: chain },
  25171. ];
  25172. /**
  25173. * Run all transformation phases in the correct order against a compilation job. After this
  25174. * processing, the compilation should be in a state where it can be emitted.
  25175. */
  25176. function transform(job, kind) {
  25177. for (const phase of phases) {
  25178. if (phase.kind === kind || phase.kind === CompilationJobKind.Both) {
  25179. // The type of `Phase` above ensures it is impossible to call a phase that doesn't support the
  25180. // job kind.
  25181. phase.fn(job);
  25182. }
  25183. }
  25184. }
  25185. /**
  25186. * Compile all views in the given `ComponentCompilation` into the final template function, which may
  25187. * reference constants defined in a `ConstantPool`.
  25188. */
  25189. function emitTemplateFn(tpl, pool) {
  25190. const rootFn = emitView(tpl.root);
  25191. emitChildViews(tpl.root, pool);
  25192. return rootFn;
  25193. }
  25194. function emitChildViews(parent, pool) {
  25195. for (const unit of parent.job.units) {
  25196. if (unit.parent !== parent.xref) {
  25197. continue;
  25198. }
  25199. // Child views are emitted depth-first.
  25200. emitChildViews(unit, pool);
  25201. const viewFn = emitView(unit);
  25202. pool.statements.push(viewFn.toDeclStmt(viewFn.name));
  25203. }
  25204. }
  25205. /**
  25206. * Emit a template function for an individual `ViewCompilation` (which may be either the root view
  25207. * or an embedded view).
  25208. */
  25209. function emitView(view) {
  25210. if (view.fnName === null) {
  25211. throw new Error(`AssertionError: view ${view.xref} is unnamed`);
  25212. }
  25213. const createStatements = [];
  25214. for (const op of view.create) {
  25215. if (op.kind !== OpKind.Statement) {
  25216. throw new Error(`AssertionError: expected all create ops to have been compiled, but got ${OpKind[op.kind]}`);
  25217. }
  25218. createStatements.push(op.statement);
  25219. }
  25220. const updateStatements = [];
  25221. for (const op of view.update) {
  25222. if (op.kind !== OpKind.Statement) {
  25223. throw new Error(`AssertionError: expected all update ops to have been compiled, but got ${OpKind[op.kind]}`);
  25224. }
  25225. updateStatements.push(op.statement);
  25226. }
  25227. const createCond = maybeGenerateRfBlock(1, createStatements);
  25228. const updateCond = maybeGenerateRfBlock(2, updateStatements);
  25229. return fn([new FnParam('rf'), new FnParam('ctx')], [...createCond, ...updateCond],
  25230. /* type */ undefined,
  25231. /* sourceSpan */ undefined, view.fnName);
  25232. }
  25233. function maybeGenerateRfBlock(flag, statements) {
  25234. if (statements.length === 0) {
  25235. return [];
  25236. }
  25237. return [
  25238. ifStmt(new BinaryOperatorExpr(BinaryOperator.BitwiseAnd, variable('rf'), literal(flag)), statements),
  25239. ];
  25240. }
  25241. function emitHostBindingFunction(job) {
  25242. if (job.root.fnName === null) {
  25243. throw new Error(`AssertionError: host binding function is unnamed`);
  25244. }
  25245. const createStatements = [];
  25246. for (const op of job.root.create) {
  25247. if (op.kind !== OpKind.Statement) {
  25248. throw new Error(`AssertionError: expected all create ops to have been compiled, but got ${OpKind[op.kind]}`);
  25249. }
  25250. createStatements.push(op.statement);
  25251. }
  25252. const updateStatements = [];
  25253. for (const op of job.root.update) {
  25254. if (op.kind !== OpKind.Statement) {
  25255. throw new Error(`AssertionError: expected all update ops to have been compiled, but got ${OpKind[op.kind]}`);
  25256. }
  25257. updateStatements.push(op.statement);
  25258. }
  25259. if (createStatements.length === 0 && updateStatements.length === 0) {
  25260. return null;
  25261. }
  25262. const createCond = maybeGenerateRfBlock(1, createStatements);
  25263. const updateCond = maybeGenerateRfBlock(2, updateStatements);
  25264. return fn([new FnParam('rf'), new FnParam('ctx')], [...createCond, ...updateCond],
  25265. /* type */ undefined,
  25266. /* sourceSpan */ undefined, job.root.fnName);
  25267. }
  25268. const compatibilityMode = CompatibilityMode.TemplateDefinitionBuilder;
  25269. // Schema containing DOM elements and their properties.
  25270. const domSchema = new DomElementSchemaRegistry();
  25271. // Tag name of the `ng-template` element.
  25272. const NG_TEMPLATE_TAG_NAME = 'ng-template';
  25273. function isI18nRootNode(meta) {
  25274. return meta instanceof Message;
  25275. }
  25276. function isSingleI18nIcu(meta) {
  25277. return isI18nRootNode(meta) && meta.nodes.length === 1 && meta.nodes[0] instanceof Icu;
  25278. }
  25279. /**
  25280. * Process a template AST and convert it into a `ComponentCompilation` in the intermediate
  25281. * representation.
  25282. * TODO: Refactor more of the ingestion code into phases.
  25283. */
  25284. function ingestComponent(componentName, template, constantPool, relativeContextFilePath, i18nUseExternalIds, deferMeta, allDeferrableDepsFn, relativeTemplatePath, enableDebugLocations) {
  25285. const job = new ComponentCompilationJob(componentName, constantPool, compatibilityMode, relativeContextFilePath, i18nUseExternalIds, deferMeta, allDeferrableDepsFn, relativeTemplatePath, enableDebugLocations);
  25286. ingestNodes(job.root, template);
  25287. return job;
  25288. }
  25289. /**
  25290. * Process a host binding AST and convert it into a `HostBindingCompilationJob` in the intermediate
  25291. * representation.
  25292. */
  25293. function ingestHostBinding(input, bindingParser, constantPool) {
  25294. const job = new HostBindingCompilationJob(input.componentName, constantPool, compatibilityMode);
  25295. for (const property of input.properties ?? []) {
  25296. let bindingKind = BindingKind.Property;
  25297. // TODO: this should really be handled in the parser.
  25298. if (property.name.startsWith('attr.')) {
  25299. property.name = property.name.substring('attr.'.length);
  25300. bindingKind = BindingKind.Attribute;
  25301. }
  25302. if (property.isAnimation) {
  25303. bindingKind = BindingKind.Animation;
  25304. }
  25305. const securityContexts = bindingParser
  25306. .calcPossibleSecurityContexts(input.componentSelector, property.name, bindingKind === BindingKind.Attribute)
  25307. .filter((context) => context !== SecurityContext.NONE);
  25308. ingestHostProperty(job, property, bindingKind, securityContexts);
  25309. }
  25310. for (const [name, expr] of Object.entries(input.attributes) ?? []) {
  25311. const securityContexts = bindingParser
  25312. .calcPossibleSecurityContexts(input.componentSelector, name, true)
  25313. .filter((context) => context !== SecurityContext.NONE);
  25314. ingestHostAttribute(job, name, expr, securityContexts);
  25315. }
  25316. for (const event of input.events ?? []) {
  25317. ingestHostEvent(job, event);
  25318. }
  25319. return job;
  25320. }
  25321. // TODO: We should refactor the parser to use the same types and structures for host bindings as
  25322. // with ordinary components. This would allow us to share a lot more ingestion code.
  25323. function ingestHostProperty(job, property, bindingKind, securityContexts) {
  25324. let expression;
  25325. const ast = property.expression.ast;
  25326. if (ast instanceof Interpolation$1) {
  25327. expression = new Interpolation(ast.strings, ast.expressions.map((expr) => convertAst(expr, job, property.sourceSpan)), []);
  25328. }
  25329. else {
  25330. expression = convertAst(ast, job, property.sourceSpan);
  25331. }
  25332. job.root.update.push(createBindingOp(job.root.xref, bindingKind, property.name, expression, null, securityContexts, false, false, null,
  25333. /* TODO: How do Host bindings handle i18n attrs? */ null, property.sourceSpan));
  25334. }
  25335. function ingestHostAttribute(job, name, value, securityContexts) {
  25336. const attrBinding = createBindingOp(job.root.xref, BindingKind.Attribute, name, value, null, securityContexts,
  25337. /* Host attributes should always be extracted to const hostAttrs, even if they are not
  25338. *strictly* text literals */
  25339. true, false, null,
  25340. /* TODO */ null,
  25341. /** TODO: May be null? */ value.sourceSpan);
  25342. job.root.update.push(attrBinding);
  25343. }
  25344. function ingestHostEvent(job, event) {
  25345. const [phase, target] = event.type !== ParsedEventType.Animation
  25346. ? [null, event.targetOrPhase]
  25347. : [event.targetOrPhase, null];
  25348. const eventBinding = createListenerOp(job.root.xref, new SlotHandle(), event.name, null, makeListenerHandlerOps(job.root, event.handler, event.handlerSpan), phase, target, true, event.sourceSpan);
  25349. job.root.create.push(eventBinding);
  25350. }
  25351. /**
  25352. * Ingest the nodes of a template AST into the given `ViewCompilation`.
  25353. */
  25354. function ingestNodes(unit, template) {
  25355. for (const node of template) {
  25356. if (node instanceof Element$1) {
  25357. ingestElement(unit, node);
  25358. }
  25359. else if (node instanceof Template) {
  25360. ingestTemplate(unit, node);
  25361. }
  25362. else if (node instanceof Content) {
  25363. ingestContent(unit, node);
  25364. }
  25365. else if (node instanceof Text$3) {
  25366. ingestText(unit, node, null);
  25367. }
  25368. else if (node instanceof BoundText) {
  25369. ingestBoundText(unit, node, null);
  25370. }
  25371. else if (node instanceof IfBlock) {
  25372. ingestIfBlock(unit, node);
  25373. }
  25374. else if (node instanceof SwitchBlock) {
  25375. ingestSwitchBlock(unit, node);
  25376. }
  25377. else if (node instanceof DeferredBlock) {
  25378. ingestDeferBlock(unit, node);
  25379. }
  25380. else if (node instanceof Icu$1) {
  25381. ingestIcu(unit, node);
  25382. }
  25383. else if (node instanceof ForLoopBlock) {
  25384. ingestForBlock(unit, node);
  25385. }
  25386. else if (node instanceof LetDeclaration$1) {
  25387. ingestLetDeclaration(unit, node);
  25388. }
  25389. else {
  25390. throw new Error(`Unsupported template node: ${node.constructor.name}`);
  25391. }
  25392. }
  25393. }
  25394. /**
  25395. * Ingest an element AST from the template into the given `ViewCompilation`.
  25396. */
  25397. function ingestElement(unit, element) {
  25398. if (element.i18n !== undefined &&
  25399. !(element.i18n instanceof Message || element.i18n instanceof TagPlaceholder)) {
  25400. throw Error(`Unhandled i18n metadata type for element: ${element.i18n.constructor.name}`);
  25401. }
  25402. const id = unit.job.allocateXrefId();
  25403. const [namespaceKey, elementName] = splitNsName(element.name);
  25404. const startOp = createElementStartOp(elementName, id, namespaceForKey(namespaceKey), element.i18n instanceof TagPlaceholder ? element.i18n : undefined, element.startSourceSpan, element.sourceSpan);
  25405. unit.create.push(startOp);
  25406. ingestElementBindings(unit, startOp, element);
  25407. ingestReferences(startOp, element);
  25408. // Start i18n, if needed, goes after the element create and bindings, but before the nodes
  25409. let i18nBlockId = null;
  25410. if (element.i18n instanceof Message) {
  25411. i18nBlockId = unit.job.allocateXrefId();
  25412. unit.create.push(createI18nStartOp(i18nBlockId, element.i18n, undefined, element.startSourceSpan));
  25413. }
  25414. ingestNodes(unit, element.children);
  25415. // The source span for the end op is typically the element closing tag. However, if no closing tag
  25416. // exists, such as in `<input>`, we use the start source span instead. Usually the start and end
  25417. // instructions will be collapsed into one `element` instruction, negating the purpose of this
  25418. // fallback, but in cases when it is not collapsed (such as an input with a binding), we still
  25419. // want to map the end instruction to the main element.
  25420. const endOp = createElementEndOp(id, element.endSourceSpan ?? element.startSourceSpan);
  25421. unit.create.push(endOp);
  25422. // If there is an i18n message associated with this element, insert i18n start and end ops.
  25423. if (i18nBlockId !== null) {
  25424. OpList.insertBefore(createI18nEndOp(i18nBlockId, element.endSourceSpan ?? element.startSourceSpan), endOp);
  25425. }
  25426. }
  25427. /**
  25428. * Ingest an `ng-template` node from the AST into the given `ViewCompilation`.
  25429. */
  25430. function ingestTemplate(unit, tmpl) {
  25431. if (tmpl.i18n !== undefined &&
  25432. !(tmpl.i18n instanceof Message || tmpl.i18n instanceof TagPlaceholder)) {
  25433. throw Error(`Unhandled i18n metadata type for template: ${tmpl.i18n.constructor.name}`);
  25434. }
  25435. const childView = unit.job.allocateView(unit.xref);
  25436. let tagNameWithoutNamespace = tmpl.tagName;
  25437. let namespacePrefix = '';
  25438. if (tmpl.tagName) {
  25439. [namespacePrefix, tagNameWithoutNamespace] = splitNsName(tmpl.tagName);
  25440. }
  25441. const i18nPlaceholder = tmpl.i18n instanceof TagPlaceholder ? tmpl.i18n : undefined;
  25442. const namespace = namespaceForKey(namespacePrefix);
  25443. const functionNameSuffix = tagNameWithoutNamespace === null ? '' : prefixWithNamespace(tagNameWithoutNamespace, namespace);
  25444. const templateKind = isPlainTemplate(tmpl)
  25445. ? TemplateKind.NgTemplate
  25446. : TemplateKind.Structural;
  25447. const templateOp = createTemplateOp(childView.xref, templateKind, tagNameWithoutNamespace, functionNameSuffix, namespace, i18nPlaceholder, tmpl.startSourceSpan, tmpl.sourceSpan);
  25448. unit.create.push(templateOp);
  25449. ingestTemplateBindings(unit, templateOp, tmpl, templateKind);
  25450. ingestReferences(templateOp, tmpl);
  25451. ingestNodes(childView, tmpl.children);
  25452. for (const { name, value } of tmpl.variables) {
  25453. childView.contextVariables.set(name, value !== '' ? value : '$implicit');
  25454. }
  25455. // If this is a plain template and there is an i18n message associated with it, insert i18n start
  25456. // and end ops. For structural directive templates, the i18n ops will be added when ingesting the
  25457. // element/template the directive is placed on.
  25458. if (templateKind === TemplateKind.NgTemplate && tmpl.i18n instanceof Message) {
  25459. const id = unit.job.allocateXrefId();
  25460. OpList.insertAfter(createI18nStartOp(id, tmpl.i18n, undefined, tmpl.startSourceSpan), childView.create.head);
  25461. OpList.insertBefore(createI18nEndOp(id, tmpl.endSourceSpan ?? tmpl.startSourceSpan), childView.create.tail);
  25462. }
  25463. }
  25464. /**
  25465. * Ingest a content node from the AST into the given `ViewCompilation`.
  25466. */
  25467. function ingestContent(unit, content) {
  25468. if (content.i18n !== undefined && !(content.i18n instanceof TagPlaceholder)) {
  25469. throw Error(`Unhandled i18n metadata type for element: ${content.i18n.constructor.name}`);
  25470. }
  25471. let fallbackView = null;
  25472. // Don't capture default content that's only made up of empty text nodes and comments.
  25473. // Note that we process the default content before the projection in order to match the
  25474. // insertion order at runtime.
  25475. if (content.children.some((child) => !(child instanceof Comment$1) &&
  25476. (!(child instanceof Text$3) || child.value.trim().length > 0))) {
  25477. fallbackView = unit.job.allocateView(unit.xref);
  25478. ingestNodes(fallbackView, content.children);
  25479. }
  25480. const id = unit.job.allocateXrefId();
  25481. const op = createProjectionOp(id, content.selector, content.i18n, fallbackView?.xref ?? null, content.sourceSpan);
  25482. for (const attr of content.attributes) {
  25483. const securityContext = domSchema.securityContext(content.name, attr.name, true);
  25484. unit.update.push(createBindingOp(op.xref, BindingKind.Attribute, attr.name, literal(attr.value), null, securityContext, true, false, null, asMessage(attr.i18n), attr.sourceSpan));
  25485. }
  25486. unit.create.push(op);
  25487. }
  25488. /**
  25489. * Ingest a literal text node from the AST into the given `ViewCompilation`.
  25490. */
  25491. function ingestText(unit, text, icuPlaceholder) {
  25492. unit.create.push(createTextOp(unit.job.allocateXrefId(), text.value, icuPlaceholder, text.sourceSpan));
  25493. }
  25494. /**
  25495. * Ingest an interpolated text node from the AST into the given `ViewCompilation`.
  25496. */
  25497. function ingestBoundText(unit, text, icuPlaceholder) {
  25498. let value = text.value;
  25499. if (value instanceof ASTWithSource) {
  25500. value = value.ast;
  25501. }
  25502. if (!(value instanceof Interpolation$1)) {
  25503. throw new Error(`AssertionError: expected Interpolation for BoundText node, got ${value.constructor.name}`);
  25504. }
  25505. if (text.i18n !== undefined && !(text.i18n instanceof Container)) {
  25506. throw Error(`Unhandled i18n metadata type for text interpolation: ${text.i18n?.constructor.name}`);
  25507. }
  25508. const i18nPlaceholders = text.i18n instanceof Container
  25509. ? text.i18n.children
  25510. .filter((node) => node instanceof Placeholder)
  25511. .map((placeholder) => placeholder.name)
  25512. : [];
  25513. if (i18nPlaceholders.length > 0 && i18nPlaceholders.length !== value.expressions.length) {
  25514. throw Error(`Unexpected number of i18n placeholders (${value.expressions.length}) for BoundText with ${value.expressions.length} expressions`);
  25515. }
  25516. const textXref = unit.job.allocateXrefId();
  25517. unit.create.push(createTextOp(textXref, '', icuPlaceholder, text.sourceSpan));
  25518. // TemplateDefinitionBuilder does not generate source maps for sub-expressions inside an
  25519. // interpolation. We copy that behavior in compatibility mode.
  25520. // TODO: is it actually correct to generate these extra maps in modern mode?
  25521. const baseSourceSpan = unit.job.compatibility ? null : text.sourceSpan;
  25522. unit.update.push(createInterpolateTextOp(textXref, new Interpolation(value.strings, value.expressions.map((expr) => convertAst(expr, unit.job, baseSourceSpan)), i18nPlaceholders), text.sourceSpan));
  25523. }
  25524. /**
  25525. * Ingest an `@if` block into the given `ViewCompilation`.
  25526. */
  25527. function ingestIfBlock(unit, ifBlock) {
  25528. let firstXref = null;
  25529. let conditions = [];
  25530. for (let i = 0; i < ifBlock.branches.length; i++) {
  25531. const ifCase = ifBlock.branches[i];
  25532. const cView = unit.job.allocateView(unit.xref);
  25533. const tagName = ingestControlFlowInsertionPoint(unit, cView.xref, ifCase);
  25534. if (ifCase.expressionAlias !== null) {
  25535. cView.contextVariables.set(ifCase.expressionAlias.name, CTX_REF);
  25536. }
  25537. let ifCaseI18nMeta = undefined;
  25538. if (ifCase.i18n !== undefined) {
  25539. if (!(ifCase.i18n instanceof BlockPlaceholder)) {
  25540. throw Error(`Unhandled i18n metadata type for if block: ${ifCase.i18n?.constructor.name}`);
  25541. }
  25542. ifCaseI18nMeta = ifCase.i18n;
  25543. }
  25544. const templateOp = createTemplateOp(cView.xref, TemplateKind.Block, tagName, 'Conditional', Namespace.HTML, ifCaseI18nMeta, ifCase.startSourceSpan, ifCase.sourceSpan);
  25545. unit.create.push(templateOp);
  25546. if (firstXref === null) {
  25547. firstXref = cView.xref;
  25548. }
  25549. const caseExpr = ifCase.expression ? convertAst(ifCase.expression, unit.job, null) : null;
  25550. const conditionalCaseExpr = new ConditionalCaseExpr(caseExpr, templateOp.xref, templateOp.handle, ifCase.expressionAlias);
  25551. conditions.push(conditionalCaseExpr);
  25552. ingestNodes(cView, ifCase.children);
  25553. }
  25554. unit.update.push(createConditionalOp(firstXref, null, conditions, ifBlock.sourceSpan));
  25555. }
  25556. /**
  25557. * Ingest an `@switch` block into the given `ViewCompilation`.
  25558. */
  25559. function ingestSwitchBlock(unit, switchBlock) {
  25560. // Don't ingest empty switches since they won't render anything.
  25561. if (switchBlock.cases.length === 0) {
  25562. return;
  25563. }
  25564. let firstXref = null;
  25565. let conditions = [];
  25566. for (const switchCase of switchBlock.cases) {
  25567. const cView = unit.job.allocateView(unit.xref);
  25568. const tagName = ingestControlFlowInsertionPoint(unit, cView.xref, switchCase);
  25569. let switchCaseI18nMeta = undefined;
  25570. if (switchCase.i18n !== undefined) {
  25571. if (!(switchCase.i18n instanceof BlockPlaceholder)) {
  25572. throw Error(`Unhandled i18n metadata type for switch block: ${switchCase.i18n?.constructor.name}`);
  25573. }
  25574. switchCaseI18nMeta = switchCase.i18n;
  25575. }
  25576. const templateOp = createTemplateOp(cView.xref, TemplateKind.Block, tagName, 'Case', Namespace.HTML, switchCaseI18nMeta, switchCase.startSourceSpan, switchCase.sourceSpan);
  25577. unit.create.push(templateOp);
  25578. if (firstXref === null) {
  25579. firstXref = cView.xref;
  25580. }
  25581. const caseExpr = switchCase.expression
  25582. ? convertAst(switchCase.expression, unit.job, switchBlock.startSourceSpan)
  25583. : null;
  25584. const conditionalCaseExpr = new ConditionalCaseExpr(caseExpr, templateOp.xref, templateOp.handle);
  25585. conditions.push(conditionalCaseExpr);
  25586. ingestNodes(cView, switchCase.children);
  25587. }
  25588. unit.update.push(createConditionalOp(firstXref, convertAst(switchBlock.expression, unit.job, null), conditions, switchBlock.sourceSpan));
  25589. }
  25590. function ingestDeferView(unit, suffix, i18nMeta, children, sourceSpan) {
  25591. if (i18nMeta !== undefined && !(i18nMeta instanceof BlockPlaceholder)) {
  25592. throw Error('Unhandled i18n metadata type for defer block');
  25593. }
  25594. if (children === undefined) {
  25595. return null;
  25596. }
  25597. const secondaryView = unit.job.allocateView(unit.xref);
  25598. ingestNodes(secondaryView, children);
  25599. const templateOp = createTemplateOp(secondaryView.xref, TemplateKind.Block, null, `Defer${suffix}`, Namespace.HTML, i18nMeta, sourceSpan, sourceSpan);
  25600. unit.create.push(templateOp);
  25601. return templateOp;
  25602. }
  25603. function ingestDeferBlock(unit, deferBlock) {
  25604. let ownResolverFn = null;
  25605. if (unit.job.deferMeta.mode === 0 /* DeferBlockDepsEmitMode.PerBlock */) {
  25606. if (!unit.job.deferMeta.blocks.has(deferBlock)) {
  25607. throw new Error(`AssertionError: unable to find a dependency function for this deferred block`);
  25608. }
  25609. ownResolverFn = unit.job.deferMeta.blocks.get(deferBlock) ?? null;
  25610. }
  25611. // Generate the defer main view and all secondary views.
  25612. const main = ingestDeferView(unit, '', deferBlock.i18n, deferBlock.children, deferBlock.sourceSpan);
  25613. const loading = ingestDeferView(unit, 'Loading', deferBlock.loading?.i18n, deferBlock.loading?.children, deferBlock.loading?.sourceSpan);
  25614. const placeholder = ingestDeferView(unit, 'Placeholder', deferBlock.placeholder?.i18n, deferBlock.placeholder?.children, deferBlock.placeholder?.sourceSpan);
  25615. const error = ingestDeferView(unit, 'Error', deferBlock.error?.i18n, deferBlock.error?.children, deferBlock.error?.sourceSpan);
  25616. // Create the main defer op, and ops for all secondary views.
  25617. const deferXref = unit.job.allocateXrefId();
  25618. const deferOp = createDeferOp(deferXref, main.xref, main.handle, ownResolverFn, unit.job.allDeferrableDepsFn, deferBlock.sourceSpan);
  25619. deferOp.placeholderView = placeholder?.xref ?? null;
  25620. deferOp.placeholderSlot = placeholder?.handle ?? null;
  25621. deferOp.loadingSlot = loading?.handle ?? null;
  25622. deferOp.errorSlot = error?.handle ?? null;
  25623. deferOp.placeholderMinimumTime = deferBlock.placeholder?.minimumTime ?? null;
  25624. deferOp.loadingMinimumTime = deferBlock.loading?.minimumTime ?? null;
  25625. deferOp.loadingAfterTime = deferBlock.loading?.afterTime ?? null;
  25626. deferOp.flags = calcDeferBlockFlags(deferBlock);
  25627. unit.create.push(deferOp);
  25628. // Configure all defer `on` conditions.
  25629. // TODO: refactor prefetch triggers to use a separate op type, with a shared superclass. This will
  25630. // make it easier to refactor prefetch behavior in the future.
  25631. const deferOnOps = [];
  25632. const deferWhenOps = [];
  25633. // Ingest the hydrate triggers first since they set up all the other triggers during SSR.
  25634. ingestDeferTriggers("hydrate" /* ir.DeferOpModifierKind.HYDRATE */, deferBlock.hydrateTriggers, deferOnOps, deferWhenOps, unit, deferXref);
  25635. ingestDeferTriggers("none" /* ir.DeferOpModifierKind.NONE */, deferBlock.triggers, deferOnOps, deferWhenOps, unit, deferXref);
  25636. ingestDeferTriggers("prefetch" /* ir.DeferOpModifierKind.PREFETCH */, deferBlock.prefetchTriggers, deferOnOps, deferWhenOps, unit, deferXref);
  25637. // If no (non-prefetching or hydrating) defer triggers were provided, default to `idle`.
  25638. const hasConcreteTrigger = deferOnOps.some((op) => op.modifier === "none" /* ir.DeferOpModifierKind.NONE */) ||
  25639. deferWhenOps.some((op) => op.modifier === "none" /* ir.DeferOpModifierKind.NONE */);
  25640. if (!hasConcreteTrigger) {
  25641. deferOnOps.push(createDeferOnOp(deferXref, { kind: DeferTriggerKind.Idle }, "none" /* ir.DeferOpModifierKind.NONE */, null));
  25642. }
  25643. unit.create.push(deferOnOps);
  25644. unit.update.push(deferWhenOps);
  25645. }
  25646. function calcDeferBlockFlags(deferBlockDetails) {
  25647. if (Object.keys(deferBlockDetails.hydrateTriggers).length > 0) {
  25648. return 1 /* ir.TDeferDetailsFlags.HasHydrateTriggers */;
  25649. }
  25650. return null;
  25651. }
  25652. function ingestDeferTriggers(modifier, triggers, onOps, whenOps, unit, deferXref) {
  25653. if (triggers.idle !== undefined) {
  25654. const deferOnOp = createDeferOnOp(deferXref, { kind: DeferTriggerKind.Idle }, modifier, triggers.idle.sourceSpan);
  25655. onOps.push(deferOnOp);
  25656. }
  25657. if (triggers.immediate !== undefined) {
  25658. const deferOnOp = createDeferOnOp(deferXref, { kind: DeferTriggerKind.Immediate }, modifier, triggers.immediate.sourceSpan);
  25659. onOps.push(deferOnOp);
  25660. }
  25661. if (triggers.timer !== undefined) {
  25662. const deferOnOp = createDeferOnOp(deferXref, { kind: DeferTriggerKind.Timer, delay: triggers.timer.delay }, modifier, triggers.timer.sourceSpan);
  25663. onOps.push(deferOnOp);
  25664. }
  25665. if (triggers.hover !== undefined) {
  25666. const deferOnOp = createDeferOnOp(deferXref, {
  25667. kind: DeferTriggerKind.Hover,
  25668. targetName: triggers.hover.reference,
  25669. targetXref: null,
  25670. targetSlot: null,
  25671. targetView: null,
  25672. targetSlotViewSteps: null,
  25673. }, modifier, triggers.hover.sourceSpan);
  25674. onOps.push(deferOnOp);
  25675. }
  25676. if (triggers.interaction !== undefined) {
  25677. const deferOnOp = createDeferOnOp(deferXref, {
  25678. kind: DeferTriggerKind.Interaction,
  25679. targetName: triggers.interaction.reference,
  25680. targetXref: null,
  25681. targetSlot: null,
  25682. targetView: null,
  25683. targetSlotViewSteps: null,
  25684. }, modifier, triggers.interaction.sourceSpan);
  25685. onOps.push(deferOnOp);
  25686. }
  25687. if (triggers.viewport !== undefined) {
  25688. const deferOnOp = createDeferOnOp(deferXref, {
  25689. kind: DeferTriggerKind.Viewport,
  25690. targetName: triggers.viewport.reference,
  25691. targetXref: null,
  25692. targetSlot: null,
  25693. targetView: null,
  25694. targetSlotViewSteps: null,
  25695. }, modifier, triggers.viewport.sourceSpan);
  25696. onOps.push(deferOnOp);
  25697. }
  25698. if (triggers.never !== undefined) {
  25699. const deferOnOp = createDeferOnOp(deferXref, { kind: DeferTriggerKind.Never }, modifier, triggers.never.sourceSpan);
  25700. onOps.push(deferOnOp);
  25701. }
  25702. if (triggers.when !== undefined) {
  25703. if (triggers.when.value instanceof Interpolation$1) {
  25704. // TemplateDefinitionBuilder supports this case, but it's very strange to me. What would it
  25705. // even mean?
  25706. throw new Error(`Unexpected interpolation in defer block when trigger`);
  25707. }
  25708. const deferOnOp = createDeferWhenOp(deferXref, convertAst(triggers.when.value, unit.job, triggers.when.sourceSpan), modifier, triggers.when.sourceSpan);
  25709. whenOps.push(deferOnOp);
  25710. }
  25711. }
  25712. function ingestIcu(unit, icu) {
  25713. if (icu.i18n instanceof Message && isSingleI18nIcu(icu.i18n)) {
  25714. const xref = unit.job.allocateXrefId();
  25715. unit.create.push(createIcuStartOp(xref, icu.i18n, icuFromI18nMessage(icu.i18n).name, null));
  25716. for (const [placeholder, text] of Object.entries({ ...icu.vars, ...icu.placeholders })) {
  25717. if (text instanceof BoundText) {
  25718. ingestBoundText(unit, text, placeholder);
  25719. }
  25720. else {
  25721. ingestText(unit, text, placeholder);
  25722. }
  25723. }
  25724. unit.create.push(createIcuEndOp(xref));
  25725. }
  25726. else {
  25727. throw Error(`Unhandled i18n metadata type for ICU: ${icu.i18n?.constructor.name}`);
  25728. }
  25729. }
  25730. /**
  25731. * Ingest an `@for` block into the given `ViewCompilation`.
  25732. */
  25733. function ingestForBlock(unit, forBlock) {
  25734. const repeaterView = unit.job.allocateView(unit.xref);
  25735. // We copy TemplateDefinitionBuilder's scheme of creating names for `$count` and `$index`
  25736. // that are suffixed with special information, to disambiguate which level of nested loop
  25737. // the below aliases refer to.
  25738. // TODO: We should refactor Template Pipeline's variable phases to gracefully handle
  25739. // shadowing, and arbitrarily many levels of variables depending on each other.
  25740. const indexName = `ɵ$index_${repeaterView.xref}`;
  25741. const countName = `ɵ$count_${repeaterView.xref}`;
  25742. const indexVarNames = new Set();
  25743. // Set all the context variables and aliases available in the repeater.
  25744. repeaterView.contextVariables.set(forBlock.item.name, forBlock.item.value);
  25745. for (const variable of forBlock.contextVariables) {
  25746. if (variable.value === '$index') {
  25747. indexVarNames.add(variable.name);
  25748. }
  25749. if (variable.name === '$index') {
  25750. repeaterView.contextVariables.set('$index', variable.value).set(indexName, variable.value);
  25751. }
  25752. else if (variable.name === '$count') {
  25753. repeaterView.contextVariables.set('$count', variable.value).set(countName, variable.value);
  25754. }
  25755. else {
  25756. repeaterView.aliases.add({
  25757. kind: SemanticVariableKind.Alias,
  25758. name: null,
  25759. identifier: variable.name,
  25760. expression: getComputedForLoopVariableExpression(variable, indexName, countName),
  25761. });
  25762. }
  25763. }
  25764. const sourceSpan = convertSourceSpan(forBlock.trackBy.span, forBlock.sourceSpan);
  25765. const track = convertAst(forBlock.trackBy, unit.job, sourceSpan);
  25766. ingestNodes(repeaterView, forBlock.children);
  25767. let emptyView = null;
  25768. let emptyTagName = null;
  25769. if (forBlock.empty !== null) {
  25770. emptyView = unit.job.allocateView(unit.xref);
  25771. ingestNodes(emptyView, forBlock.empty.children);
  25772. emptyTagName = ingestControlFlowInsertionPoint(unit, emptyView.xref, forBlock.empty);
  25773. }
  25774. const varNames = {
  25775. $index: indexVarNames,
  25776. $implicit: forBlock.item.name,
  25777. };
  25778. if (forBlock.i18n !== undefined && !(forBlock.i18n instanceof BlockPlaceholder)) {
  25779. throw Error('AssertionError: Unhandled i18n metadata type or @for');
  25780. }
  25781. if (forBlock.empty?.i18n !== undefined &&
  25782. !(forBlock.empty.i18n instanceof BlockPlaceholder)) {
  25783. throw Error('AssertionError: Unhandled i18n metadata type or @empty');
  25784. }
  25785. const i18nPlaceholder = forBlock.i18n;
  25786. const emptyI18nPlaceholder = forBlock.empty?.i18n;
  25787. const tagName = ingestControlFlowInsertionPoint(unit, repeaterView.xref, forBlock);
  25788. const repeaterCreate = createRepeaterCreateOp(repeaterView.xref, emptyView?.xref ?? null, tagName, track, varNames, emptyTagName, i18nPlaceholder, emptyI18nPlaceholder, forBlock.startSourceSpan, forBlock.sourceSpan);
  25789. unit.create.push(repeaterCreate);
  25790. const expression = convertAst(forBlock.expression, unit.job, convertSourceSpan(forBlock.expression.span, forBlock.sourceSpan));
  25791. const repeater = createRepeaterOp(repeaterCreate.xref, repeaterCreate.handle, expression, forBlock.sourceSpan);
  25792. unit.update.push(repeater);
  25793. }
  25794. /**
  25795. * Gets an expression that represents a variable in an `@for` loop.
  25796. * @param variable AST representing the variable.
  25797. * @param indexName Loop-specific name for `$index`.
  25798. * @param countName Loop-specific name for `$count`.
  25799. */
  25800. function getComputedForLoopVariableExpression(variable, indexName, countName) {
  25801. switch (variable.value) {
  25802. case '$index':
  25803. return new LexicalReadExpr(indexName);
  25804. case '$count':
  25805. return new LexicalReadExpr(countName);
  25806. case '$first':
  25807. return new LexicalReadExpr(indexName).identical(literal(0));
  25808. case '$last':
  25809. return new LexicalReadExpr(indexName).identical(new LexicalReadExpr(countName).minus(literal(1)));
  25810. case '$even':
  25811. return new LexicalReadExpr(indexName).modulo(literal(2)).identical(literal(0));
  25812. case '$odd':
  25813. return new LexicalReadExpr(indexName).modulo(literal(2)).notIdentical(literal(0));
  25814. default:
  25815. throw new Error(`AssertionError: unknown @for loop variable ${variable.value}`);
  25816. }
  25817. }
  25818. function ingestLetDeclaration(unit, node) {
  25819. const target = unit.job.allocateXrefId();
  25820. unit.create.push(createDeclareLetOp(target, node.name, node.sourceSpan));
  25821. unit.update.push(createStoreLetOp(target, node.name, convertAst(node.value, unit.job, node.valueSpan), node.sourceSpan));
  25822. }
  25823. /**
  25824. * Convert a template AST expression into an output AST expression.
  25825. */
  25826. function convertAst(ast, job, baseSourceSpan) {
  25827. if (ast instanceof ASTWithSource) {
  25828. return convertAst(ast.ast, job, baseSourceSpan);
  25829. }
  25830. else if (ast instanceof PropertyRead) {
  25831. // Whether this is an implicit receiver, *excluding* explicit reads of `this`.
  25832. const isImplicitReceiver = ast.receiver instanceof ImplicitReceiver && !(ast.receiver instanceof ThisReceiver);
  25833. if (isImplicitReceiver) {
  25834. return new LexicalReadExpr(ast.name);
  25835. }
  25836. else {
  25837. return new ReadPropExpr(convertAst(ast.receiver, job, baseSourceSpan), ast.name, null, convertSourceSpan(ast.span, baseSourceSpan));
  25838. }
  25839. }
  25840. else if (ast instanceof PropertyWrite) {
  25841. if (ast.receiver instanceof ImplicitReceiver) {
  25842. return new WritePropExpr(
  25843. // TODO: Is it correct to always use the root context in place of the implicit receiver?
  25844. new ContextExpr(job.root.xref), ast.name, convertAst(ast.value, job, baseSourceSpan), null, convertSourceSpan(ast.span, baseSourceSpan));
  25845. }
  25846. return new WritePropExpr(convertAst(ast.receiver, job, baseSourceSpan), ast.name, convertAst(ast.value, job, baseSourceSpan), undefined, convertSourceSpan(ast.span, baseSourceSpan));
  25847. }
  25848. else if (ast instanceof KeyedWrite) {
  25849. return new WriteKeyExpr(convertAst(ast.receiver, job, baseSourceSpan), convertAst(ast.key, job, baseSourceSpan), convertAst(ast.value, job, baseSourceSpan), undefined, convertSourceSpan(ast.span, baseSourceSpan));
  25850. }
  25851. else if (ast instanceof Call) {
  25852. if (ast.receiver instanceof ImplicitReceiver) {
  25853. throw new Error(`Unexpected ImplicitReceiver`);
  25854. }
  25855. else {
  25856. return new InvokeFunctionExpr(convertAst(ast.receiver, job, baseSourceSpan), ast.args.map((arg) => convertAst(arg, job, baseSourceSpan)), undefined, convertSourceSpan(ast.span, baseSourceSpan));
  25857. }
  25858. }
  25859. else if (ast instanceof LiteralPrimitive) {
  25860. return literal(ast.value, undefined, convertSourceSpan(ast.span, baseSourceSpan));
  25861. }
  25862. else if (ast instanceof Unary) {
  25863. switch (ast.operator) {
  25864. case '+':
  25865. return new UnaryOperatorExpr(UnaryOperator.Plus, convertAst(ast.expr, job, baseSourceSpan), undefined, convertSourceSpan(ast.span, baseSourceSpan));
  25866. case '-':
  25867. return new UnaryOperatorExpr(UnaryOperator.Minus, convertAst(ast.expr, job, baseSourceSpan), undefined, convertSourceSpan(ast.span, baseSourceSpan));
  25868. default:
  25869. throw new Error(`AssertionError: unknown unary operator ${ast.operator}`);
  25870. }
  25871. }
  25872. else if (ast instanceof Binary) {
  25873. const operator = BINARY_OPERATORS.get(ast.operation);
  25874. if (operator === undefined) {
  25875. throw new Error(`AssertionError: unknown binary operator ${ast.operation}`);
  25876. }
  25877. return new BinaryOperatorExpr(operator, convertAst(ast.left, job, baseSourceSpan), convertAst(ast.right, job, baseSourceSpan), undefined, convertSourceSpan(ast.span, baseSourceSpan));
  25878. }
  25879. else if (ast instanceof ThisReceiver) {
  25880. // TODO: should context expressions have source maps?
  25881. return new ContextExpr(job.root.xref);
  25882. }
  25883. else if (ast instanceof KeyedRead) {
  25884. return new ReadKeyExpr(convertAst(ast.receiver, job, baseSourceSpan), convertAst(ast.key, job, baseSourceSpan), undefined, convertSourceSpan(ast.span, baseSourceSpan));
  25885. }
  25886. else if (ast instanceof Chain) {
  25887. throw new Error(`AssertionError: Chain in unknown context`);
  25888. }
  25889. else if (ast instanceof LiteralMap) {
  25890. const entries = ast.keys.map((key, idx) => {
  25891. const value = ast.values[idx];
  25892. // TODO: should literals have source maps, or do we just map the whole surrounding
  25893. // expression?
  25894. return new LiteralMapEntry(key.key, convertAst(value, job, baseSourceSpan), key.quoted);
  25895. });
  25896. return new LiteralMapExpr(entries, undefined, convertSourceSpan(ast.span, baseSourceSpan));
  25897. }
  25898. else if (ast instanceof LiteralArray) {
  25899. // TODO: should literals have source maps, or do we just map the whole surrounding expression?
  25900. return new LiteralArrayExpr(ast.expressions.map((expr) => convertAst(expr, job, baseSourceSpan)));
  25901. }
  25902. else if (ast instanceof Conditional) {
  25903. return new ConditionalExpr(convertAst(ast.condition, job, baseSourceSpan), convertAst(ast.trueExp, job, baseSourceSpan), convertAst(ast.falseExp, job, baseSourceSpan), undefined, convertSourceSpan(ast.span, baseSourceSpan));
  25904. }
  25905. else if (ast instanceof NonNullAssert) {
  25906. // A non-null assertion shouldn't impact generated instructions, so we can just drop it.
  25907. return convertAst(ast.expression, job, baseSourceSpan);
  25908. }
  25909. else if (ast instanceof BindingPipe) {
  25910. // TODO: pipes should probably have source maps; figure out details.
  25911. return new PipeBindingExpr(job.allocateXrefId(), new SlotHandle(), ast.name, [
  25912. convertAst(ast.exp, job, baseSourceSpan),
  25913. ...ast.args.map((arg) => convertAst(arg, job, baseSourceSpan)),
  25914. ]);
  25915. }
  25916. else if (ast instanceof SafeKeyedRead) {
  25917. return new SafeKeyedReadExpr(convertAst(ast.receiver, job, baseSourceSpan), convertAst(ast.key, job, baseSourceSpan), convertSourceSpan(ast.span, baseSourceSpan));
  25918. }
  25919. else if (ast instanceof SafePropertyRead) {
  25920. // TODO: source span
  25921. return new SafePropertyReadExpr(convertAst(ast.receiver, job, baseSourceSpan), ast.name);
  25922. }
  25923. else if (ast instanceof SafeCall) {
  25924. // TODO: source span
  25925. return new SafeInvokeFunctionExpr(convertAst(ast.receiver, job, baseSourceSpan), ast.args.map((a) => convertAst(a, job, baseSourceSpan)));
  25926. }
  25927. else if (ast instanceof EmptyExpr$1) {
  25928. return new EmptyExpr(convertSourceSpan(ast.span, baseSourceSpan));
  25929. }
  25930. else if (ast instanceof PrefixNot) {
  25931. return not(convertAst(ast.expression, job, baseSourceSpan), convertSourceSpan(ast.span, baseSourceSpan));
  25932. }
  25933. else if (ast instanceof TypeofExpression) {
  25934. return typeofExpr(convertAst(ast.expression, job, baseSourceSpan));
  25935. }
  25936. else if (ast instanceof TemplateLiteral) {
  25937. return new TemplateLiteralExpr(ast.elements.map((el) => {
  25938. return new TemplateLiteralElementExpr(el.text, convertSourceSpan(el.span, baseSourceSpan));
  25939. }), ast.expressions.map((expr) => convertAst(expr, job, baseSourceSpan)), convertSourceSpan(ast.span, baseSourceSpan));
  25940. }
  25941. else {
  25942. throw new Error(`Unhandled expression type "${ast.constructor.name}" in file "${baseSourceSpan?.start.file.url}"`);
  25943. }
  25944. }
  25945. function convertAstWithInterpolation(job, value, i18nMeta, sourceSpan) {
  25946. let expression;
  25947. if (value instanceof Interpolation$1) {
  25948. expression = new Interpolation(value.strings, value.expressions.map((e) => convertAst(e, job, null)), Object.keys(asMessage(i18nMeta)?.placeholders ?? {}));
  25949. }
  25950. else if (value instanceof AST) {
  25951. expression = convertAst(value, job, null);
  25952. }
  25953. else {
  25954. expression = literal(value);
  25955. }
  25956. return expression;
  25957. }
  25958. // TODO: Can we populate Template binding kinds in ingest?
  25959. const BINDING_KINDS = new Map([
  25960. [BindingType.Property, BindingKind.Property],
  25961. [BindingType.TwoWay, BindingKind.TwoWayProperty],
  25962. [BindingType.Attribute, BindingKind.Attribute],
  25963. [BindingType.Class, BindingKind.ClassName],
  25964. [BindingType.Style, BindingKind.StyleProperty],
  25965. [BindingType.Animation, BindingKind.Animation],
  25966. ]);
  25967. /**
  25968. * Checks whether the given template is a plain ng-template (as opposed to another kind of template
  25969. * such as a structural directive template or control flow template). This is checked based on the
  25970. * tagName. We can expect that only plain ng-templates will come through with a tagName of
  25971. * 'ng-template'.
  25972. *
  25973. * Here are some of the cases we expect:
  25974. *
  25975. * | Angular HTML | Template tagName |
  25976. * | ---------------------------------- | ------------------ |
  25977. * | `<ng-template>` | 'ng-template' |
  25978. * | `<div *ngIf="true">` | 'div' |
  25979. * | `<svg><ng-template>` | 'svg:ng-template' |
  25980. * | `@if (true) {` | 'Conditional' |
  25981. * | `<ng-template *ngIf>` (plain) | 'ng-template' |
  25982. * | `<ng-template *ngIf>` (structural) | null |
  25983. */
  25984. function isPlainTemplate(tmpl) {
  25985. return splitNsName(tmpl.tagName ?? '')[1] === NG_TEMPLATE_TAG_NAME;
  25986. }
  25987. /**
  25988. * Ensures that the i18nMeta, if provided, is an i18n.Message.
  25989. */
  25990. function asMessage(i18nMeta) {
  25991. if (i18nMeta == null) {
  25992. return null;
  25993. }
  25994. if (!(i18nMeta instanceof Message)) {
  25995. throw Error(`Expected i18n meta to be a Message, but got: ${i18nMeta.constructor.name}`);
  25996. }
  25997. return i18nMeta;
  25998. }
  25999. /**
  26000. * Process all of the bindings on an element in the template AST and convert them to their IR
  26001. * representation.
  26002. */
  26003. function ingestElementBindings(unit, op, element) {
  26004. let bindings = new Array();
  26005. let i18nAttributeBindingNames = new Set();
  26006. for (const attr of element.attributes) {
  26007. // Attribute literal bindings, such as `attr.foo="bar"`.
  26008. const securityContext = domSchema.securityContext(element.name, attr.name, true);
  26009. bindings.push(createBindingOp(op.xref, BindingKind.Attribute, attr.name, convertAstWithInterpolation(unit.job, attr.value, attr.i18n), null, securityContext, true, false, null, asMessage(attr.i18n), attr.sourceSpan));
  26010. if (attr.i18n) {
  26011. i18nAttributeBindingNames.add(attr.name);
  26012. }
  26013. }
  26014. for (const input of element.inputs) {
  26015. if (i18nAttributeBindingNames.has(input.name)) {
  26016. console.error(`On component ${unit.job.componentName}, the binding ${input.name} is both an i18n attribute and a property. You may want to remove the property binding. This will become a compilation error in future versions of Angular.`);
  26017. }
  26018. // All dynamic bindings (both attribute and property bindings).
  26019. bindings.push(createBindingOp(op.xref, BINDING_KINDS.get(input.type), input.name, convertAstWithInterpolation(unit.job, astOf(input.value), input.i18n), input.unit, input.securityContext, false, false, null, asMessage(input.i18n) ?? null, input.sourceSpan));
  26020. }
  26021. unit.create.push(bindings.filter((b) => b?.kind === OpKind.ExtractedAttribute));
  26022. unit.update.push(bindings.filter((b) => b?.kind === OpKind.Binding));
  26023. for (const output of element.outputs) {
  26024. if (output.type === ParsedEventType.Animation && output.phase === null) {
  26025. throw Error('Animation listener should have a phase');
  26026. }
  26027. if (output.type === ParsedEventType.TwoWay) {
  26028. unit.create.push(createTwoWayListenerOp(op.xref, op.handle, output.name, op.tag, makeTwoWayListenerHandlerOps(unit, output.handler, output.handlerSpan), output.sourceSpan));
  26029. }
  26030. else {
  26031. unit.create.push(createListenerOp(op.xref, op.handle, output.name, op.tag, makeListenerHandlerOps(unit, output.handler, output.handlerSpan), output.phase, output.target, false, output.sourceSpan));
  26032. }
  26033. }
  26034. // If any of the bindings on this element have an i18n message, then an i18n attrs configuration
  26035. // op is also required.
  26036. if (bindings.some((b) => b?.i18nMessage) !== null) {
  26037. unit.create.push(createI18nAttributesOp(unit.job.allocateXrefId(), new SlotHandle(), op.xref));
  26038. }
  26039. }
  26040. /**
  26041. * Process all of the bindings on a template in the template AST and convert them to their IR
  26042. * representation.
  26043. */
  26044. function ingestTemplateBindings(unit, op, template, templateKind) {
  26045. let bindings = new Array();
  26046. for (const attr of template.templateAttrs) {
  26047. if (attr instanceof TextAttribute) {
  26048. const securityContext = domSchema.securityContext(NG_TEMPLATE_TAG_NAME, attr.name, true);
  26049. bindings.push(createTemplateBinding(unit, op.xref, BindingType.Attribute, attr.name, attr.value, null, securityContext, true, templateKind, asMessage(attr.i18n), attr.sourceSpan));
  26050. }
  26051. else {
  26052. bindings.push(createTemplateBinding(unit, op.xref, attr.type, attr.name, astOf(attr.value), attr.unit, attr.securityContext, true, templateKind, asMessage(attr.i18n), attr.sourceSpan));
  26053. }
  26054. }
  26055. for (const attr of template.attributes) {
  26056. // Attribute literal bindings, such as `attr.foo="bar"`.
  26057. const securityContext = domSchema.securityContext(NG_TEMPLATE_TAG_NAME, attr.name, true);
  26058. bindings.push(createTemplateBinding(unit, op.xref, BindingType.Attribute, attr.name, attr.value, null, securityContext, false, templateKind, asMessage(attr.i18n), attr.sourceSpan));
  26059. }
  26060. for (const input of template.inputs) {
  26061. // Dynamic bindings (both attribute and property bindings).
  26062. bindings.push(createTemplateBinding(unit, op.xref, input.type, input.name, astOf(input.value), input.unit, input.securityContext, false, templateKind, asMessage(input.i18n), input.sourceSpan));
  26063. }
  26064. unit.create.push(bindings.filter((b) => b?.kind === OpKind.ExtractedAttribute));
  26065. unit.update.push(bindings.filter((b) => b?.kind === OpKind.Binding));
  26066. for (const output of template.outputs) {
  26067. if (output.type === ParsedEventType.Animation && output.phase === null) {
  26068. throw Error('Animation listener should have a phase');
  26069. }
  26070. if (templateKind === TemplateKind.NgTemplate) {
  26071. if (output.type === ParsedEventType.TwoWay) {
  26072. unit.create.push(createTwoWayListenerOp(op.xref, op.handle, output.name, op.tag, makeTwoWayListenerHandlerOps(unit, output.handler, output.handlerSpan), output.sourceSpan));
  26073. }
  26074. else {
  26075. unit.create.push(createListenerOp(op.xref, op.handle, output.name, op.tag, makeListenerHandlerOps(unit, output.handler, output.handlerSpan), output.phase, output.target, false, output.sourceSpan));
  26076. }
  26077. }
  26078. if (templateKind === TemplateKind.Structural &&
  26079. output.type !== ParsedEventType.Animation) {
  26080. // Animation bindings are excluded from the structural template's const array.
  26081. const securityContext = domSchema.securityContext(NG_TEMPLATE_TAG_NAME, output.name, false);
  26082. unit.create.push(createExtractedAttributeOp(op.xref, BindingKind.Property, null, output.name, null, null, null, securityContext));
  26083. }
  26084. }
  26085. // TODO: Perhaps we could do this in a phase? (It likely wouldn't change the slot indices.)
  26086. if (bindings.some((b) => b?.i18nMessage) !== null) {
  26087. unit.create.push(createI18nAttributesOp(unit.job.allocateXrefId(), new SlotHandle(), op.xref));
  26088. }
  26089. }
  26090. /**
  26091. * Helper to ingest an individual binding on a template, either an explicit `ng-template`, or an
  26092. * implicit template created via structural directive.
  26093. *
  26094. * Bindings on templates are *extremely* tricky. I have tried to isolate all of the confusing edge
  26095. * cases into this function, and to comment it well to document the behavior.
  26096. *
  26097. * Some of this behavior is intuitively incorrect, and we should consider changing it in the future.
  26098. *
  26099. * @param view The compilation unit for the view containing the template.
  26100. * @param xref The xref of the template op.
  26101. * @param type The binding type, according to the parser. This is fairly reasonable, e.g. both
  26102. * dynamic and static attributes have e.BindingType.Attribute.
  26103. * @param name The binding's name.
  26104. * @param value The bindings's value, which will either be an input AST expression, or a string
  26105. * literal. Note that the input AST expression may or may not be const -- it will only be a
  26106. * string literal if the parser considered it a text binding.
  26107. * @param unit If the binding has a unit (e.g. `px` for style bindings), then this is the unit.
  26108. * @param securityContext The security context of the binding.
  26109. * @param isStructuralTemplateAttribute Whether this binding actually applies to the structural
  26110. * ng-template. For example, an `ngFor` would actually apply to the structural template. (Most
  26111. * bindings on structural elements target the inner element, not the template.)
  26112. * @param templateKind Whether this is an explicit `ng-template` or an implicit template created by
  26113. * a structural directive. This should never be a block template.
  26114. * @param i18nMessage The i18n metadata for the binding, if any.
  26115. * @param sourceSpan The source span of the binding.
  26116. * @returns An IR binding op, or null if the binding should be skipped.
  26117. */
  26118. function createTemplateBinding(view, xref, type, name, value, unit, securityContext, isStructuralTemplateAttribute, templateKind, i18nMessage, sourceSpan) {
  26119. const isTextBinding = typeof value === 'string';
  26120. // If this is a structural template, then several kinds of bindings should not result in an
  26121. // update instruction.
  26122. if (templateKind === TemplateKind.Structural) {
  26123. if (!isStructuralTemplateAttribute) {
  26124. switch (type) {
  26125. case BindingType.Property:
  26126. case BindingType.Class:
  26127. case BindingType.Style:
  26128. // Because this binding doesn't really target the ng-template, it must be a binding on an
  26129. // inner node of a structural template. We can't skip it entirely, because we still need
  26130. // it on the ng-template's consts (e.g. for the purposes of directive matching). However,
  26131. // we should not generate an update instruction for it.
  26132. return createExtractedAttributeOp(xref, BindingKind.Property, null, name, null, null, i18nMessage, securityContext);
  26133. case BindingType.TwoWay:
  26134. return createExtractedAttributeOp(xref, BindingKind.TwoWayProperty, null, name, null, null, i18nMessage, securityContext);
  26135. }
  26136. }
  26137. if (!isTextBinding && (type === BindingType.Attribute || type === BindingType.Animation)) {
  26138. // Again, this binding doesn't really target the ng-template; it actually targets the element
  26139. // inside the structural template. In the case of non-text attribute or animation bindings,
  26140. // the binding doesn't even show up on the ng-template const array, so we just skip it
  26141. // entirely.
  26142. return null;
  26143. }
  26144. }
  26145. let bindingType = BINDING_KINDS.get(type);
  26146. if (templateKind === TemplateKind.NgTemplate) {
  26147. // We know we are dealing with bindings directly on an explicit ng-template.
  26148. // Static attribute bindings should be collected into the const array as k/v pairs. Property
  26149. // bindings should result in a `property` instruction, and `AttributeMarker.Bindings` const
  26150. // entries.
  26151. //
  26152. // The difficulty is with dynamic attribute, style, and class bindings. These don't really make
  26153. // sense on an `ng-template` and should probably be parser errors. However,
  26154. // TemplateDefinitionBuilder generates `property` instructions for them, and so we do that as
  26155. // well.
  26156. //
  26157. // Note that we do have a slight behavior difference with TemplateDefinitionBuilder: although
  26158. // TDB emits `property` instructions for dynamic attributes, styles, and classes, only styles
  26159. // and classes also get const collected into the `AttributeMarker.Bindings` field. Dynamic
  26160. // attribute bindings are missing from the consts entirely. We choose to emit them into the
  26161. // consts field anyway, to avoid creating special cases for something so arcane and nonsensical.
  26162. if (type === BindingType.Class ||
  26163. type === BindingType.Style ||
  26164. (type === BindingType.Attribute && !isTextBinding)) {
  26165. // TODO: These cases should be parse errors.
  26166. bindingType = BindingKind.Property;
  26167. }
  26168. }
  26169. return createBindingOp(xref, bindingType, name, convertAstWithInterpolation(view.job, value, i18nMessage), unit, securityContext, isTextBinding, isStructuralTemplateAttribute, templateKind, i18nMessage, sourceSpan);
  26170. }
  26171. function makeListenerHandlerOps(unit, handler, handlerSpan) {
  26172. handler = astOf(handler);
  26173. const handlerOps = new Array();
  26174. let handlerExprs = handler instanceof Chain ? handler.expressions : [handler];
  26175. if (handlerExprs.length === 0) {
  26176. throw new Error('Expected listener to have non-empty expression list.');
  26177. }
  26178. const expressions = handlerExprs.map((expr) => convertAst(expr, unit.job, handlerSpan));
  26179. const returnExpr = expressions.pop();
  26180. handlerOps.push(...expressions.map((e) => createStatementOp(new ExpressionStatement(e, e.sourceSpan))));
  26181. handlerOps.push(createStatementOp(new ReturnStatement(returnExpr, returnExpr.sourceSpan)));
  26182. return handlerOps;
  26183. }
  26184. function makeTwoWayListenerHandlerOps(unit, handler, handlerSpan) {
  26185. handler = astOf(handler);
  26186. const handlerOps = new Array();
  26187. if (handler instanceof Chain) {
  26188. if (handler.expressions.length === 1) {
  26189. handler = handler.expressions[0];
  26190. }
  26191. else {
  26192. // This is validated during parsing already, but we do it here just in case.
  26193. throw new Error('Expected two-way listener to have a single expression.');
  26194. }
  26195. }
  26196. const handlerExpr = convertAst(handler, unit.job, handlerSpan);
  26197. const eventReference = new LexicalReadExpr('$event');
  26198. const twoWaySetExpr = new TwoWayBindingSetExpr(handlerExpr, eventReference);
  26199. handlerOps.push(createStatementOp(new ExpressionStatement(twoWaySetExpr)));
  26200. handlerOps.push(createStatementOp(new ReturnStatement(eventReference)));
  26201. return handlerOps;
  26202. }
  26203. function astOf(ast) {
  26204. return ast instanceof ASTWithSource ? ast.ast : ast;
  26205. }
  26206. /**
  26207. * Process all of the local references on an element-like structure in the template AST and
  26208. * convert them to their IR representation.
  26209. */
  26210. function ingestReferences(op, element) {
  26211. assertIsArray(op.localRefs);
  26212. for (const { name, value } of element.references) {
  26213. op.localRefs.push({
  26214. name,
  26215. target: value,
  26216. });
  26217. }
  26218. }
  26219. /**
  26220. * Assert that the given value is an array.
  26221. */
  26222. function assertIsArray(value) {
  26223. if (!Array.isArray(value)) {
  26224. throw new Error(`AssertionError: expected an array`);
  26225. }
  26226. }
  26227. /**
  26228. * Creates an absolute `ParseSourceSpan` from the relative `ParseSpan`.
  26229. *
  26230. * `ParseSpan` objects are relative to the start of the expression.
  26231. * This method converts these to full `ParseSourceSpan` objects that
  26232. * show where the span is within the overall source file.
  26233. *
  26234. * @param span the relative span to convert.
  26235. * @param baseSourceSpan a span corresponding to the base of the expression tree.
  26236. * @returns a `ParseSourceSpan` for the given span or null if no `baseSourceSpan` was provided.
  26237. */
  26238. function convertSourceSpan(span, baseSourceSpan) {
  26239. if (baseSourceSpan === null) {
  26240. return null;
  26241. }
  26242. const start = baseSourceSpan.start.moveBy(span.start);
  26243. const end = baseSourceSpan.start.moveBy(span.end);
  26244. const fullStart = baseSourceSpan.fullStart.moveBy(span.start);
  26245. return new ParseSourceSpan(start, end, fullStart);
  26246. }
  26247. /**
  26248. * With the directive-based control flow users were able to conditionally project content using
  26249. * the `*` syntax. E.g. `<div *ngIf="expr" projectMe></div>` will be projected into
  26250. * `<ng-content select="[projectMe]"/>`, because the attributes and tag name from the `div` are
  26251. * copied to the template via the template creation instruction. With `@if` and `@for` that is
  26252. * not the case, because the conditional is placed *around* elements, rather than *on* them.
  26253. * The result is that content projection won't work in the same way if a user converts from
  26254. * `*ngIf` to `@if`.
  26255. *
  26256. * This function aims to cover the most common case by doing the same copying when a control flow
  26257. * node has *one and only one* root element or template node.
  26258. *
  26259. * This approach comes with some caveats:
  26260. * 1. As soon as any other node is added to the root, the copying behavior won't work anymore.
  26261. * A diagnostic will be added to flag cases like this and to explain how to work around it.
  26262. * 2. If `preserveWhitespaces` is enabled, it's very likely that indentation will break this
  26263. * workaround, because it'll include an additional text node as the first child. We can work
  26264. * around it here, but in a discussion it was decided not to, because the user explicitly opted
  26265. * into preserving the whitespace and we would have to drop it from the generated code.
  26266. * The diagnostic mentioned point in #1 will flag such cases to users.
  26267. *
  26268. * @returns Tag name to be used for the control flow template.
  26269. */
  26270. function ingestControlFlowInsertionPoint(unit, xref, node) {
  26271. let root = null;
  26272. for (const child of node.children) {
  26273. // Skip over comment nodes and @let declarations since
  26274. // it doesn't matter where they end up in the DOM.
  26275. if (child instanceof Comment$1 || child instanceof LetDeclaration$1) {
  26276. continue;
  26277. }
  26278. // We can only infer the tag name/attributes if there's a single root node.
  26279. if (root !== null) {
  26280. return null;
  26281. }
  26282. // Root nodes can only elements or templates with a tag name (e.g. `<div *foo></div>`).
  26283. if (child instanceof Element$1 || (child instanceof Template && child.tagName !== null)) {
  26284. root = child;
  26285. }
  26286. else {
  26287. return null;
  26288. }
  26289. }
  26290. // If we've found a single root node, its tag name and attributes can be
  26291. // copied to the surrounding template to be used for content projection.
  26292. if (root !== null) {
  26293. // Collect the static attributes for content projection purposes.
  26294. for (const attr of root.attributes) {
  26295. const securityContext = domSchema.securityContext(NG_TEMPLATE_TAG_NAME, attr.name, true);
  26296. unit.update.push(createBindingOp(xref, BindingKind.Attribute, attr.name, literal(attr.value), null, securityContext, true, false, null, asMessage(attr.i18n), attr.sourceSpan));
  26297. }
  26298. // Also collect the inputs since they participate in content projection as well.
  26299. // Note that TDB used to collect the outputs as well, but it wasn't passing them into
  26300. // the template instruction. Here we just don't collect them.
  26301. for (const attr of root.inputs) {
  26302. if (attr.type !== BindingType.Animation && attr.type !== BindingType.Attribute) {
  26303. const securityContext = domSchema.securityContext(NG_TEMPLATE_TAG_NAME, attr.name, true);
  26304. unit.create.push(createExtractedAttributeOp(xref, BindingKind.Property, null, attr.name, null, null, null, securityContext));
  26305. }
  26306. }
  26307. const tagName = root instanceof Element$1 ? root.name : root.tagName;
  26308. // Don't pass along `ng-template` tag name since it enables directive matching.
  26309. return tagName === NG_TEMPLATE_TAG_NAME ? null : tagName;
  26310. }
  26311. return null;
  26312. }
  26313. /*!
  26314. * @license
  26315. * Copyright Google LLC All Rights Reserved.
  26316. *
  26317. * Use of this source code is governed by an MIT-style license that can be
  26318. * found in the LICENSE file at https://angular.dev/license
  26319. */
  26320. /**
  26321. * Whether to produce instructions that will attach the source location to each DOM node.
  26322. *
  26323. * !!!Important!!! at the time of writing this flag isn't exposed externally, but internal debug
  26324. * tools enable it via a local change. Any modifications to this flag need to update the
  26325. * internal tooling as well.
  26326. */
  26327. let ENABLE_TEMPLATE_SOURCE_LOCATIONS = false;
  26328. /** Gets whether template source locations are enabled. */
  26329. function getTemplateSourceLocationsEnabled() {
  26330. return ENABLE_TEMPLATE_SOURCE_LOCATIONS;
  26331. }
  26332. // if (rf & flags) { .. }
  26333. function renderFlagCheckIfStmt(flags, statements) {
  26334. return ifStmt(variable(RENDER_FLAGS).bitwiseAnd(literal(flags), null, false), statements);
  26335. }
  26336. /**
  26337. * Translates query flags into `TQueryFlags` type in
  26338. * packages/core/src/render3/interfaces/query.ts
  26339. * @param query
  26340. */
  26341. function toQueryFlags(query) {
  26342. return ((query.descendants ? 1 /* QueryFlags.descendants */ : 0 /* QueryFlags.none */) |
  26343. (query.static ? 2 /* QueryFlags.isStatic */ : 0 /* QueryFlags.none */) |
  26344. (query.emitDistinctChangesOnly ? 4 /* QueryFlags.emitDistinctChangesOnly */ : 0 /* QueryFlags.none */));
  26345. }
  26346. function getQueryPredicate(query, constantPool) {
  26347. if (Array.isArray(query.predicate)) {
  26348. let predicate = [];
  26349. query.predicate.forEach((selector) => {
  26350. // Each item in predicates array may contain strings with comma-separated refs
  26351. // (for ex. 'ref, ref1, ..., refN'), thus we extract individual refs and store them
  26352. // as separate array entities
  26353. const selectors = selector.split(',').map((token) => literal(token.trim()));
  26354. predicate.push(...selectors);
  26355. });
  26356. return constantPool.getConstLiteral(literalArr(predicate), true);
  26357. }
  26358. else {
  26359. // The original predicate may have been wrapped in a `forwardRef()` call.
  26360. switch (query.predicate.forwardRef) {
  26361. case 0 /* ForwardRefHandling.None */:
  26362. case 2 /* ForwardRefHandling.Unwrapped */:
  26363. return query.predicate.expression;
  26364. case 1 /* ForwardRefHandling.Wrapped */:
  26365. return importExpr(Identifiers.resolveForwardRef).callFn([query.predicate.expression]);
  26366. }
  26367. }
  26368. }
  26369. function createQueryCreateCall(query, constantPool, queryTypeFns, prependParams) {
  26370. const parameters = [];
  26371. if (prependParams !== undefined) {
  26372. parameters.push(...prependParams);
  26373. }
  26374. if (query.isSignal) {
  26375. parameters.push(new ReadPropExpr(variable(CONTEXT_NAME), query.propertyName));
  26376. }
  26377. parameters.push(getQueryPredicate(query, constantPool), literal(toQueryFlags(query)));
  26378. if (query.read) {
  26379. parameters.push(query.read);
  26380. }
  26381. const queryCreateFn = query.isSignal ? queryTypeFns.signalBased : queryTypeFns.nonSignal;
  26382. return importExpr(queryCreateFn).callFn(parameters);
  26383. }
  26384. const queryAdvancePlaceholder = Symbol('queryAdvancePlaceholder');
  26385. /**
  26386. * Collapses query advance placeholders in a list of statements.
  26387. *
  26388. * This allows for less generated code because multiple sibling query advance
  26389. * statements can be collapsed into a single call with the count as argument.
  26390. *
  26391. * e.g.
  26392. *
  26393. * ```ts
  26394. * bla();
  26395. * queryAdvance();
  26396. * queryAdvance();
  26397. * bla();
  26398. * ```
  26399. *
  26400. * --> will turn into
  26401. *
  26402. * ```ts
  26403. * bla();
  26404. * queryAdvance(2);
  26405. * bla();
  26406. * ```
  26407. */
  26408. function collapseAdvanceStatements(statements) {
  26409. const result = [];
  26410. let advanceCollapseCount = 0;
  26411. const flushAdvanceCount = () => {
  26412. if (advanceCollapseCount > 0) {
  26413. result.unshift(importExpr(Identifiers.queryAdvance)
  26414. .callFn(advanceCollapseCount === 1 ? [] : [literal(advanceCollapseCount)])
  26415. .toStmt());
  26416. advanceCollapseCount = 0;
  26417. }
  26418. };
  26419. // Iterate through statements in reverse and collapse advance placeholders.
  26420. for (let i = statements.length - 1; i >= 0; i--) {
  26421. const st = statements[i];
  26422. if (st === queryAdvancePlaceholder) {
  26423. advanceCollapseCount++;
  26424. }
  26425. else {
  26426. flushAdvanceCount();
  26427. result.unshift(st);
  26428. }
  26429. }
  26430. flushAdvanceCount();
  26431. return result;
  26432. }
  26433. // Define and update any view queries
  26434. function createViewQueriesFunction(viewQueries, constantPool, name) {
  26435. const createStatements = [];
  26436. const updateStatements = [];
  26437. const tempAllocator = temporaryAllocator((st) => updateStatements.push(st), TEMPORARY_NAME);
  26438. viewQueries.forEach((query) => {
  26439. // creation call, e.g. r3.viewQuery(somePredicate, true) or
  26440. // r3.viewQuerySignal(ctx.prop, somePredicate, true);
  26441. const queryDefinitionCall = createQueryCreateCall(query, constantPool, {
  26442. signalBased: Identifiers.viewQuerySignal,
  26443. nonSignal: Identifiers.viewQuery,
  26444. });
  26445. createStatements.push(queryDefinitionCall.toStmt());
  26446. // Signal queries update lazily and we just advance the index.
  26447. if (query.isSignal) {
  26448. updateStatements.push(queryAdvancePlaceholder);
  26449. return;
  26450. }
  26451. // update, e.g. (r3.queryRefresh(tmp = r3.loadQuery()) && (ctx.someDir = tmp));
  26452. const temporary = tempAllocator();
  26453. const getQueryList = importExpr(Identifiers.loadQuery).callFn([]);
  26454. const refresh = importExpr(Identifiers.queryRefresh).callFn([temporary.set(getQueryList)]);
  26455. const updateDirective = variable(CONTEXT_NAME)
  26456. .prop(query.propertyName)
  26457. .set(query.first ? temporary.prop('first') : temporary);
  26458. updateStatements.push(refresh.and(updateDirective).toStmt());
  26459. });
  26460. const viewQueryFnName = name ? `${name}_Query` : null;
  26461. return fn([new FnParam(RENDER_FLAGS, NUMBER_TYPE), new FnParam(CONTEXT_NAME, null)], [
  26462. renderFlagCheckIfStmt(1 /* core.RenderFlags.Create */, createStatements),
  26463. renderFlagCheckIfStmt(2 /* core.RenderFlags.Update */, collapseAdvanceStatements(updateStatements)),
  26464. ], INFERRED_TYPE, null, viewQueryFnName);
  26465. }
  26466. // Define and update any content queries
  26467. function createContentQueriesFunction(queries, constantPool, name) {
  26468. const createStatements = [];
  26469. const updateStatements = [];
  26470. const tempAllocator = temporaryAllocator((st) => updateStatements.push(st), TEMPORARY_NAME);
  26471. for (const query of queries) {
  26472. // creation, e.g. r3.contentQuery(dirIndex, somePredicate, true, null) or
  26473. // r3.contentQuerySignal(dirIndex, propName, somePredicate, <flags>, <read>).
  26474. createStatements.push(createQueryCreateCall(query, constantPool, { nonSignal: Identifiers.contentQuery, signalBased: Identifiers.contentQuerySignal },
  26475. /* prependParams */ [variable('dirIndex')]).toStmt());
  26476. // Signal queries update lazily and we just advance the index.
  26477. if (query.isSignal) {
  26478. updateStatements.push(queryAdvancePlaceholder);
  26479. continue;
  26480. }
  26481. // update, e.g. (r3.queryRefresh(tmp = r3.loadQuery()) && (ctx.someDir = tmp));
  26482. const temporary = tempAllocator();
  26483. const getQueryList = importExpr(Identifiers.loadQuery).callFn([]);
  26484. const refresh = importExpr(Identifiers.queryRefresh).callFn([temporary.set(getQueryList)]);
  26485. const updateDirective = variable(CONTEXT_NAME)
  26486. .prop(query.propertyName)
  26487. .set(query.first ? temporary.prop('first') : temporary);
  26488. updateStatements.push(refresh.and(updateDirective).toStmt());
  26489. }
  26490. const contentQueriesFnName = name ? `${name}_ContentQueries` : null;
  26491. return fn([
  26492. new FnParam(RENDER_FLAGS, NUMBER_TYPE),
  26493. new FnParam(CONTEXT_NAME, null),
  26494. new FnParam('dirIndex', null),
  26495. ], [
  26496. renderFlagCheckIfStmt(1 /* core.RenderFlags.Create */, createStatements),
  26497. renderFlagCheckIfStmt(2 /* core.RenderFlags.Update */, collapseAdvanceStatements(updateStatements)),
  26498. ], INFERRED_TYPE, null, contentQueriesFnName);
  26499. }
  26500. class HtmlParser extends Parser$1 {
  26501. constructor() {
  26502. super(getHtmlTagDefinition);
  26503. }
  26504. parse(source, url, options) {
  26505. return super.parse(source, url, options);
  26506. }
  26507. }
  26508. const PROPERTY_PARTS_SEPARATOR = '.';
  26509. const ATTRIBUTE_PREFIX = 'attr';
  26510. const CLASS_PREFIX = 'class';
  26511. const STYLE_PREFIX = 'style';
  26512. const TEMPLATE_ATTR_PREFIX$1 = '*';
  26513. const ANIMATE_PROP_PREFIX = 'animate-';
  26514. /**
  26515. * Parses bindings in templates and in the directive host area.
  26516. */
  26517. class BindingParser {
  26518. _exprParser;
  26519. _interpolationConfig;
  26520. _schemaRegistry;
  26521. errors;
  26522. constructor(_exprParser, _interpolationConfig, _schemaRegistry, errors) {
  26523. this._exprParser = _exprParser;
  26524. this._interpolationConfig = _interpolationConfig;
  26525. this._schemaRegistry = _schemaRegistry;
  26526. this.errors = errors;
  26527. }
  26528. get interpolationConfig() {
  26529. return this._interpolationConfig;
  26530. }
  26531. createBoundHostProperties(properties, sourceSpan) {
  26532. const boundProps = [];
  26533. for (const propName of Object.keys(properties)) {
  26534. const expression = properties[propName];
  26535. if (typeof expression === 'string') {
  26536. this.parsePropertyBinding(propName, expression, true, false, sourceSpan, sourceSpan.start.offset, undefined, [],
  26537. // Use the `sourceSpan` for `keySpan`. This isn't really accurate, but neither is the
  26538. // sourceSpan, as it represents the sourceSpan of the host itself rather than the
  26539. // source of the host binding (which doesn't exist in the template). Regardless,
  26540. // neither of these values are used in Ivy but are only here to satisfy the function
  26541. // signature. This should likely be refactored in the future so that `sourceSpan`
  26542. // isn't being used inaccurately.
  26543. boundProps, sourceSpan);
  26544. }
  26545. else {
  26546. this._reportError(`Value of the host property binding "${propName}" needs to be a string representing an expression but got "${expression}" (${typeof expression})`, sourceSpan);
  26547. }
  26548. }
  26549. return boundProps;
  26550. }
  26551. createDirectiveHostEventAsts(hostListeners, sourceSpan) {
  26552. const targetEvents = [];
  26553. for (const propName of Object.keys(hostListeners)) {
  26554. const expression = hostListeners[propName];
  26555. if (typeof expression === 'string') {
  26556. // Use the `sourceSpan` for `keySpan` and `handlerSpan`. This isn't really accurate, but
  26557. // neither is the `sourceSpan`, as it represents the `sourceSpan` of the host itself
  26558. // rather than the source of the host binding (which doesn't exist in the template).
  26559. // Regardless, neither of these values are used in Ivy but are only here to satisfy the
  26560. // function signature. This should likely be refactored in the future so that `sourceSpan`
  26561. // isn't being used inaccurately.
  26562. this.parseEvent(propName, expression,
  26563. /* isAssignmentEvent */ false, sourceSpan, sourceSpan, [], targetEvents, sourceSpan);
  26564. }
  26565. else {
  26566. this._reportError(`Value of the host listener "${propName}" needs to be a string representing an expression but got "${expression}" (${typeof expression})`, sourceSpan);
  26567. }
  26568. }
  26569. return targetEvents;
  26570. }
  26571. parseInterpolation(value, sourceSpan, interpolatedTokens) {
  26572. const sourceInfo = sourceSpan.start.toString();
  26573. const absoluteOffset = sourceSpan.fullStart.offset;
  26574. try {
  26575. const ast = this._exprParser.parseInterpolation(value, sourceInfo, absoluteOffset, interpolatedTokens, this._interpolationConfig);
  26576. if (ast)
  26577. this._reportExpressionParserErrors(ast.errors, sourceSpan);
  26578. return ast;
  26579. }
  26580. catch (e) {
  26581. this._reportError(`${e}`, sourceSpan);
  26582. return this._exprParser.wrapLiteralPrimitive('ERROR', sourceInfo, absoluteOffset);
  26583. }
  26584. }
  26585. /**
  26586. * Similar to `parseInterpolation`, but treats the provided string as a single expression
  26587. * element that would normally appear within the interpolation prefix and suffix (`{{` and `}}`).
  26588. * This is used for parsing the switch expression in ICUs.
  26589. */
  26590. parseInterpolationExpression(expression, sourceSpan) {
  26591. const sourceInfo = sourceSpan.start.toString();
  26592. const absoluteOffset = sourceSpan.start.offset;
  26593. try {
  26594. const ast = this._exprParser.parseInterpolationExpression(expression, sourceInfo, absoluteOffset);
  26595. if (ast)
  26596. this._reportExpressionParserErrors(ast.errors, sourceSpan);
  26597. return ast;
  26598. }
  26599. catch (e) {
  26600. this._reportError(`${e}`, sourceSpan);
  26601. return this._exprParser.wrapLiteralPrimitive('ERROR', sourceInfo, absoluteOffset);
  26602. }
  26603. }
  26604. /**
  26605. * Parses the bindings in a microsyntax expression, and converts them to
  26606. * `ParsedProperty` or `ParsedVariable`.
  26607. *
  26608. * @param tplKey template binding name
  26609. * @param tplValue template binding value
  26610. * @param sourceSpan span of template binding relative to entire the template
  26611. * @param absoluteValueOffset start of the tplValue relative to the entire template
  26612. * @param targetMatchableAttrs potential attributes to match in the template
  26613. * @param targetProps target property bindings in the template
  26614. * @param targetVars target variables in the template
  26615. */
  26616. parseInlineTemplateBinding(tplKey, tplValue, sourceSpan, absoluteValueOffset, targetMatchableAttrs, targetProps, targetVars, isIvyAst) {
  26617. const absoluteKeyOffset = sourceSpan.start.offset + TEMPLATE_ATTR_PREFIX$1.length;
  26618. const bindings = this._parseTemplateBindings(tplKey, tplValue, sourceSpan, absoluteKeyOffset, absoluteValueOffset);
  26619. for (const binding of bindings) {
  26620. // sourceSpan is for the entire HTML attribute. bindingSpan is for a particular
  26621. // binding within the microsyntax expression so it's more narrow than sourceSpan.
  26622. const bindingSpan = moveParseSourceSpan(sourceSpan, binding.sourceSpan);
  26623. const key = binding.key.source;
  26624. const keySpan = moveParseSourceSpan(sourceSpan, binding.key.span);
  26625. if (binding instanceof VariableBinding) {
  26626. const value = binding.value ? binding.value.source : '$implicit';
  26627. const valueSpan = binding.value
  26628. ? moveParseSourceSpan(sourceSpan, binding.value.span)
  26629. : undefined;
  26630. targetVars.push(new ParsedVariable(key, value, bindingSpan, keySpan, valueSpan));
  26631. }
  26632. else if (binding.value) {
  26633. const srcSpan = isIvyAst ? bindingSpan : sourceSpan;
  26634. const valueSpan = moveParseSourceSpan(sourceSpan, binding.value.ast.sourceSpan);
  26635. this._parsePropertyAst(key, binding.value, false, srcSpan, keySpan, valueSpan, targetMatchableAttrs, targetProps);
  26636. }
  26637. else {
  26638. targetMatchableAttrs.push([key, '' /* value */]);
  26639. // Since this is a literal attribute with no RHS, source span should be
  26640. // just the key span.
  26641. this.parseLiteralAttr(key, null /* value */, keySpan, absoluteValueOffset, undefined /* valueSpan */, targetMatchableAttrs, targetProps, keySpan);
  26642. }
  26643. }
  26644. }
  26645. /**
  26646. * Parses the bindings in a microsyntax expression, e.g.
  26647. * ```html
  26648. * <tag *tplKey="let value1 = prop; let value2 = localVar">
  26649. * ```
  26650. *
  26651. * @param tplKey template binding name
  26652. * @param tplValue template binding value
  26653. * @param sourceSpan span of template binding relative to entire the template
  26654. * @param absoluteKeyOffset start of the `tplKey`
  26655. * @param absoluteValueOffset start of the `tplValue`
  26656. */
  26657. _parseTemplateBindings(tplKey, tplValue, sourceSpan, absoluteKeyOffset, absoluteValueOffset) {
  26658. const sourceInfo = sourceSpan.start.toString();
  26659. try {
  26660. const bindingsResult = this._exprParser.parseTemplateBindings(tplKey, tplValue, sourceInfo, absoluteKeyOffset, absoluteValueOffset);
  26661. this._reportExpressionParserErrors(bindingsResult.errors, sourceSpan);
  26662. bindingsResult.warnings.forEach((warning) => {
  26663. this._reportError(warning, sourceSpan, ParseErrorLevel.WARNING);
  26664. });
  26665. return bindingsResult.templateBindings;
  26666. }
  26667. catch (e) {
  26668. this._reportError(`${e}`, sourceSpan);
  26669. return [];
  26670. }
  26671. }
  26672. parseLiteralAttr(name, value, sourceSpan, absoluteOffset, valueSpan, targetMatchableAttrs, targetProps, keySpan) {
  26673. if (isAnimationLabel(name)) {
  26674. name = name.substring(1);
  26675. if (keySpan !== undefined) {
  26676. keySpan = moveParseSourceSpan(keySpan, new AbsoluteSourceSpan(keySpan.start.offset + 1, keySpan.end.offset));
  26677. }
  26678. if (value) {
  26679. this._reportError(`Assigning animation triggers via @prop="exp" attributes with an expression is invalid.` +
  26680. ` Use property bindings (e.g. [@prop]="exp") or use an attribute without a value (e.g. @prop) instead.`, sourceSpan, ParseErrorLevel.ERROR);
  26681. }
  26682. this._parseAnimation(name, value, sourceSpan, absoluteOffset, keySpan, valueSpan, targetMatchableAttrs, targetProps);
  26683. }
  26684. else {
  26685. targetProps.push(new ParsedProperty(name, this._exprParser.wrapLiteralPrimitive(value, '', absoluteOffset), ParsedPropertyType.LITERAL_ATTR, sourceSpan, keySpan, valueSpan));
  26686. }
  26687. }
  26688. parsePropertyBinding(name, expression, isHost, isPartOfAssignmentBinding, sourceSpan, absoluteOffset, valueSpan, targetMatchableAttrs, targetProps, keySpan) {
  26689. if (name.length === 0) {
  26690. this._reportError(`Property name is missing in binding`, sourceSpan);
  26691. }
  26692. let isAnimationProp = false;
  26693. if (name.startsWith(ANIMATE_PROP_PREFIX)) {
  26694. isAnimationProp = true;
  26695. name = name.substring(ANIMATE_PROP_PREFIX.length);
  26696. if (keySpan !== undefined) {
  26697. keySpan = moveParseSourceSpan(keySpan, new AbsoluteSourceSpan(keySpan.start.offset + ANIMATE_PROP_PREFIX.length, keySpan.end.offset));
  26698. }
  26699. }
  26700. else if (isAnimationLabel(name)) {
  26701. isAnimationProp = true;
  26702. name = name.substring(1);
  26703. if (keySpan !== undefined) {
  26704. keySpan = moveParseSourceSpan(keySpan, new AbsoluteSourceSpan(keySpan.start.offset + 1, keySpan.end.offset));
  26705. }
  26706. }
  26707. if (isAnimationProp) {
  26708. this._parseAnimation(name, expression, sourceSpan, absoluteOffset, keySpan, valueSpan, targetMatchableAttrs, targetProps);
  26709. }
  26710. else {
  26711. this._parsePropertyAst(name, this.parseBinding(expression, isHost, valueSpan || sourceSpan, absoluteOffset), isPartOfAssignmentBinding, sourceSpan, keySpan, valueSpan, targetMatchableAttrs, targetProps);
  26712. }
  26713. }
  26714. parsePropertyInterpolation(name, value, sourceSpan, valueSpan, targetMatchableAttrs, targetProps, keySpan, interpolatedTokens) {
  26715. const expr = this.parseInterpolation(value, valueSpan || sourceSpan, interpolatedTokens);
  26716. if (expr) {
  26717. this._parsePropertyAst(name, expr, false, sourceSpan, keySpan, valueSpan, targetMatchableAttrs, targetProps);
  26718. return true;
  26719. }
  26720. return false;
  26721. }
  26722. _parsePropertyAst(name, ast, isPartOfAssignmentBinding, sourceSpan, keySpan, valueSpan, targetMatchableAttrs, targetProps) {
  26723. targetMatchableAttrs.push([name, ast.source]);
  26724. targetProps.push(new ParsedProperty(name, ast, isPartOfAssignmentBinding ? ParsedPropertyType.TWO_WAY : ParsedPropertyType.DEFAULT, sourceSpan, keySpan, valueSpan));
  26725. }
  26726. _parseAnimation(name, expression, sourceSpan, absoluteOffset, keySpan, valueSpan, targetMatchableAttrs, targetProps) {
  26727. if (name.length === 0) {
  26728. this._reportError('Animation trigger is missing', sourceSpan);
  26729. }
  26730. // This will occur when a @trigger is not paired with an expression.
  26731. // For animations it is valid to not have an expression since */void
  26732. // states will be applied by angular when the element is attached/detached
  26733. const ast = this.parseBinding(expression || 'undefined', false, valueSpan || sourceSpan, absoluteOffset);
  26734. targetMatchableAttrs.push([name, ast.source]);
  26735. targetProps.push(new ParsedProperty(name, ast, ParsedPropertyType.ANIMATION, sourceSpan, keySpan, valueSpan));
  26736. }
  26737. parseBinding(value, isHostBinding, sourceSpan, absoluteOffset) {
  26738. const sourceInfo = ((sourceSpan && sourceSpan.start) || '(unknown)').toString();
  26739. try {
  26740. const ast = isHostBinding
  26741. ? this._exprParser.parseSimpleBinding(value, sourceInfo, absoluteOffset, this._interpolationConfig)
  26742. : this._exprParser.parseBinding(value, sourceInfo, absoluteOffset, this._interpolationConfig);
  26743. if (ast)
  26744. this._reportExpressionParserErrors(ast.errors, sourceSpan);
  26745. return ast;
  26746. }
  26747. catch (e) {
  26748. this._reportError(`${e}`, sourceSpan);
  26749. return this._exprParser.wrapLiteralPrimitive('ERROR', sourceInfo, absoluteOffset);
  26750. }
  26751. }
  26752. createBoundElementProperty(elementSelector, boundProp, skipValidation = false, mapPropertyName = true) {
  26753. if (boundProp.isAnimation) {
  26754. return new BoundElementProperty(boundProp.name, BindingType.Animation, SecurityContext.NONE, boundProp.expression, null, boundProp.sourceSpan, boundProp.keySpan, boundProp.valueSpan);
  26755. }
  26756. let unit = null;
  26757. let bindingType = undefined;
  26758. let boundPropertyName = null;
  26759. const parts = boundProp.name.split(PROPERTY_PARTS_SEPARATOR);
  26760. let securityContexts = undefined;
  26761. // Check for special cases (prefix style, attr, class)
  26762. if (parts.length > 1) {
  26763. if (parts[0] == ATTRIBUTE_PREFIX) {
  26764. boundPropertyName = parts.slice(1).join(PROPERTY_PARTS_SEPARATOR);
  26765. if (!skipValidation) {
  26766. this._validatePropertyOrAttributeName(boundPropertyName, boundProp.sourceSpan, true);
  26767. }
  26768. securityContexts = calcPossibleSecurityContexts(this._schemaRegistry, elementSelector, boundPropertyName, true);
  26769. const nsSeparatorIdx = boundPropertyName.indexOf(':');
  26770. if (nsSeparatorIdx > -1) {
  26771. const ns = boundPropertyName.substring(0, nsSeparatorIdx);
  26772. const name = boundPropertyName.substring(nsSeparatorIdx + 1);
  26773. boundPropertyName = mergeNsAndName(ns, name);
  26774. }
  26775. bindingType = BindingType.Attribute;
  26776. }
  26777. else if (parts[0] == CLASS_PREFIX) {
  26778. boundPropertyName = parts[1];
  26779. bindingType = BindingType.Class;
  26780. securityContexts = [SecurityContext.NONE];
  26781. }
  26782. else if (parts[0] == STYLE_PREFIX) {
  26783. unit = parts.length > 2 ? parts[2] : null;
  26784. boundPropertyName = parts[1];
  26785. bindingType = BindingType.Style;
  26786. securityContexts = [SecurityContext.STYLE];
  26787. }
  26788. }
  26789. // If not a special case, use the full property name
  26790. if (boundPropertyName === null) {
  26791. const mappedPropName = this._schemaRegistry.getMappedPropName(boundProp.name);
  26792. boundPropertyName = mapPropertyName ? mappedPropName : boundProp.name;
  26793. securityContexts = calcPossibleSecurityContexts(this._schemaRegistry, elementSelector, mappedPropName, false);
  26794. bindingType =
  26795. boundProp.type === ParsedPropertyType.TWO_WAY ? BindingType.TwoWay : BindingType.Property;
  26796. if (!skipValidation) {
  26797. this._validatePropertyOrAttributeName(mappedPropName, boundProp.sourceSpan, false);
  26798. }
  26799. }
  26800. return new BoundElementProperty(boundPropertyName, bindingType, securityContexts[0], boundProp.expression, unit, boundProp.sourceSpan, boundProp.keySpan, boundProp.valueSpan);
  26801. }
  26802. // TODO: keySpan should be required but was made optional to avoid changing VE parser.
  26803. parseEvent(name, expression, isAssignmentEvent, sourceSpan, handlerSpan, targetMatchableAttrs, targetEvents, keySpan) {
  26804. if (name.length === 0) {
  26805. this._reportError(`Event name is missing in binding`, sourceSpan);
  26806. }
  26807. if (isAnimationLabel(name)) {
  26808. name = name.slice(1);
  26809. if (keySpan !== undefined) {
  26810. keySpan = moveParseSourceSpan(keySpan, new AbsoluteSourceSpan(keySpan.start.offset + 1, keySpan.end.offset));
  26811. }
  26812. this._parseAnimationEvent(name, expression, sourceSpan, handlerSpan, targetEvents, keySpan);
  26813. }
  26814. else {
  26815. this._parseRegularEvent(name, expression, isAssignmentEvent, sourceSpan, handlerSpan, targetMatchableAttrs, targetEvents, keySpan);
  26816. }
  26817. }
  26818. calcPossibleSecurityContexts(selector, propName, isAttribute) {
  26819. const prop = this._schemaRegistry.getMappedPropName(propName);
  26820. return calcPossibleSecurityContexts(this._schemaRegistry, selector, prop, isAttribute);
  26821. }
  26822. _parseAnimationEvent(name, expression, sourceSpan, handlerSpan, targetEvents, keySpan) {
  26823. const matches = splitAtPeriod(name, [name, '']);
  26824. const eventName = matches[0];
  26825. const phase = matches[1].toLowerCase();
  26826. const ast = this._parseAction(expression, handlerSpan);
  26827. targetEvents.push(new ParsedEvent(eventName, phase, ParsedEventType.Animation, ast, sourceSpan, handlerSpan, keySpan));
  26828. if (eventName.length === 0) {
  26829. this._reportError(`Animation event name is missing in binding`, sourceSpan);
  26830. }
  26831. if (phase) {
  26832. if (phase !== 'start' && phase !== 'done') {
  26833. this._reportError(`The provided animation output phase value "${phase}" for "@${eventName}" is not supported (use start or done)`, sourceSpan);
  26834. }
  26835. }
  26836. else {
  26837. this._reportError(`The animation trigger output event (@${eventName}) is missing its phase value name (start or done are currently supported)`, sourceSpan);
  26838. }
  26839. }
  26840. _parseRegularEvent(name, expression, isAssignmentEvent, sourceSpan, handlerSpan, targetMatchableAttrs, targetEvents, keySpan) {
  26841. // long format: 'target: eventName'
  26842. const [target, eventName] = splitAtColon(name, [null, name]);
  26843. const prevErrorCount = this.errors.length;
  26844. const ast = this._parseAction(expression, handlerSpan);
  26845. const isValid = this.errors.length === prevErrorCount;
  26846. targetMatchableAttrs.push([name, ast.source]);
  26847. // Don't try to validate assignment events if there were other
  26848. // parsing errors to avoid adding more noise to the error logs.
  26849. if (isAssignmentEvent && isValid && !this._isAllowedAssignmentEvent(ast)) {
  26850. this._reportError('Unsupported expression in a two-way binding', sourceSpan);
  26851. }
  26852. targetEvents.push(new ParsedEvent(eventName, target, isAssignmentEvent ? ParsedEventType.TwoWay : ParsedEventType.Regular, ast, sourceSpan, handlerSpan, keySpan));
  26853. // Don't detect directives for event names for now,
  26854. // so don't add the event name to the matchableAttrs
  26855. }
  26856. _parseAction(value, sourceSpan) {
  26857. const sourceInfo = ((sourceSpan && sourceSpan.start) || '(unknown').toString();
  26858. const absoluteOffset = sourceSpan && sourceSpan.start ? sourceSpan.start.offset : 0;
  26859. try {
  26860. const ast = this._exprParser.parseAction(value, sourceInfo, absoluteOffset, this._interpolationConfig);
  26861. if (ast) {
  26862. this._reportExpressionParserErrors(ast.errors, sourceSpan);
  26863. }
  26864. if (!ast || ast.ast instanceof EmptyExpr$1) {
  26865. this._reportError(`Empty expressions are not allowed`, sourceSpan);
  26866. return this._exprParser.wrapLiteralPrimitive('ERROR', sourceInfo, absoluteOffset);
  26867. }
  26868. return ast;
  26869. }
  26870. catch (e) {
  26871. this._reportError(`${e}`, sourceSpan);
  26872. return this._exprParser.wrapLiteralPrimitive('ERROR', sourceInfo, absoluteOffset);
  26873. }
  26874. }
  26875. _reportError(message, sourceSpan, level = ParseErrorLevel.ERROR, relatedError) {
  26876. this.errors.push(new ParseError(sourceSpan, message, level, relatedError));
  26877. }
  26878. _reportExpressionParserErrors(errors, sourceSpan) {
  26879. for (const error of errors) {
  26880. this._reportError(error.message, sourceSpan, undefined, error);
  26881. }
  26882. }
  26883. /**
  26884. * @param propName the name of the property / attribute
  26885. * @param sourceSpan
  26886. * @param isAttr true when binding to an attribute
  26887. */
  26888. _validatePropertyOrAttributeName(propName, sourceSpan, isAttr) {
  26889. const report = isAttr
  26890. ? this._schemaRegistry.validateAttribute(propName)
  26891. : this._schemaRegistry.validateProperty(propName);
  26892. if (report.error) {
  26893. this._reportError(report.msg, sourceSpan, ParseErrorLevel.ERROR);
  26894. }
  26895. }
  26896. /**
  26897. * Returns whether a parsed AST is allowed to be used within the event side of a two-way binding.
  26898. * @param ast Parsed AST to be checked.
  26899. */
  26900. _isAllowedAssignmentEvent(ast) {
  26901. if (ast instanceof ASTWithSource) {
  26902. return this._isAllowedAssignmentEvent(ast.ast);
  26903. }
  26904. if (ast instanceof NonNullAssert) {
  26905. return this._isAllowedAssignmentEvent(ast.expression);
  26906. }
  26907. if (ast instanceof Call &&
  26908. ast.args.length === 1 &&
  26909. ast.receiver instanceof PropertyRead &&
  26910. ast.receiver.name === '$any' &&
  26911. ast.receiver.receiver instanceof ImplicitReceiver &&
  26912. !(ast.receiver.receiver instanceof ThisReceiver)) {
  26913. return this._isAllowedAssignmentEvent(ast.args[0]);
  26914. }
  26915. if (ast instanceof PropertyRead || ast instanceof KeyedRead) {
  26916. return true;
  26917. }
  26918. return false;
  26919. }
  26920. }
  26921. function isAnimationLabel(name) {
  26922. return name[0] == '@';
  26923. }
  26924. function calcPossibleSecurityContexts(registry, selector, propName, isAttribute) {
  26925. const ctxs = [];
  26926. CssSelector.parse(selector).forEach((selector) => {
  26927. const elementNames = selector.element ? [selector.element] : registry.allKnownElementNames();
  26928. const notElementNames = new Set(selector.notSelectors
  26929. .filter((selector) => selector.isElementSelector())
  26930. .map((selector) => selector.element));
  26931. const possibleElementNames = elementNames.filter((elementName) => !notElementNames.has(elementName));
  26932. ctxs.push(...possibleElementNames.map((elementName) => registry.securityContext(elementName, propName, isAttribute)));
  26933. });
  26934. return ctxs.length === 0 ? [SecurityContext.NONE] : Array.from(new Set(ctxs)).sort();
  26935. }
  26936. /**
  26937. * Compute a new ParseSourceSpan based off an original `sourceSpan` by using
  26938. * absolute offsets from the specified `absoluteSpan`.
  26939. *
  26940. * @param sourceSpan original source span
  26941. * @param absoluteSpan absolute source span to move to
  26942. */
  26943. function moveParseSourceSpan(sourceSpan, absoluteSpan) {
  26944. // The difference of two absolute offsets provide the relative offset
  26945. const startDiff = absoluteSpan.start - sourceSpan.start.offset;
  26946. const endDiff = absoluteSpan.end - sourceSpan.end.offset;
  26947. return new ParseSourceSpan(sourceSpan.start.moveBy(startDiff), sourceSpan.end.moveBy(endDiff), sourceSpan.fullStart.moveBy(startDiff), sourceSpan.details);
  26948. }
  26949. // Some of the code comes from WebComponents.JS
  26950. // https://github.com/webcomponents/webcomponentsjs/blob/master/src/HTMLImports/path.js
  26951. function isStyleUrlResolvable(url) {
  26952. if (url == null || url.length === 0 || url[0] == '/')
  26953. return false;
  26954. const schemeMatch = url.match(URL_WITH_SCHEMA_REGEXP);
  26955. return schemeMatch === null || schemeMatch[1] == 'package' || schemeMatch[1] == 'asset';
  26956. }
  26957. const URL_WITH_SCHEMA_REGEXP = /^([^:/?#]+):/;
  26958. const NG_CONTENT_SELECT_ATTR = 'select';
  26959. const LINK_ELEMENT = 'link';
  26960. const LINK_STYLE_REL_ATTR = 'rel';
  26961. const LINK_STYLE_HREF_ATTR = 'href';
  26962. const LINK_STYLE_REL_VALUE = 'stylesheet';
  26963. const STYLE_ELEMENT = 'style';
  26964. const SCRIPT_ELEMENT = 'script';
  26965. const NG_NON_BINDABLE_ATTR = 'ngNonBindable';
  26966. const NG_PROJECT_AS = 'ngProjectAs';
  26967. function preparseElement(ast) {
  26968. let selectAttr = null;
  26969. let hrefAttr = null;
  26970. let relAttr = null;
  26971. let nonBindable = false;
  26972. let projectAs = '';
  26973. ast.attrs.forEach((attr) => {
  26974. const lcAttrName = attr.name.toLowerCase();
  26975. if (lcAttrName == NG_CONTENT_SELECT_ATTR) {
  26976. selectAttr = attr.value;
  26977. }
  26978. else if (lcAttrName == LINK_STYLE_HREF_ATTR) {
  26979. hrefAttr = attr.value;
  26980. }
  26981. else if (lcAttrName == LINK_STYLE_REL_ATTR) {
  26982. relAttr = attr.value;
  26983. }
  26984. else if (attr.name == NG_NON_BINDABLE_ATTR) {
  26985. nonBindable = true;
  26986. }
  26987. else if (attr.name == NG_PROJECT_AS) {
  26988. if (attr.value.length > 0) {
  26989. projectAs = attr.value;
  26990. }
  26991. }
  26992. });
  26993. selectAttr = normalizeNgContentSelect(selectAttr);
  26994. const nodeName = ast.name.toLowerCase();
  26995. let type = PreparsedElementType.OTHER;
  26996. if (isNgContent(nodeName)) {
  26997. type = PreparsedElementType.NG_CONTENT;
  26998. }
  26999. else if (nodeName == STYLE_ELEMENT) {
  27000. type = PreparsedElementType.STYLE;
  27001. }
  27002. else if (nodeName == SCRIPT_ELEMENT) {
  27003. type = PreparsedElementType.SCRIPT;
  27004. }
  27005. else if (nodeName == LINK_ELEMENT && relAttr == LINK_STYLE_REL_VALUE) {
  27006. type = PreparsedElementType.STYLESHEET;
  27007. }
  27008. return new PreparsedElement(type, selectAttr, hrefAttr, nonBindable, projectAs);
  27009. }
  27010. var PreparsedElementType;
  27011. (function (PreparsedElementType) {
  27012. PreparsedElementType[PreparsedElementType["NG_CONTENT"] = 0] = "NG_CONTENT";
  27013. PreparsedElementType[PreparsedElementType["STYLE"] = 1] = "STYLE";
  27014. PreparsedElementType[PreparsedElementType["STYLESHEET"] = 2] = "STYLESHEET";
  27015. PreparsedElementType[PreparsedElementType["SCRIPT"] = 3] = "SCRIPT";
  27016. PreparsedElementType[PreparsedElementType["OTHER"] = 4] = "OTHER";
  27017. })(PreparsedElementType || (PreparsedElementType = {}));
  27018. class PreparsedElement {
  27019. type;
  27020. selectAttr;
  27021. hrefAttr;
  27022. nonBindable;
  27023. projectAs;
  27024. constructor(type, selectAttr, hrefAttr, nonBindable, projectAs) {
  27025. this.type = type;
  27026. this.selectAttr = selectAttr;
  27027. this.hrefAttr = hrefAttr;
  27028. this.nonBindable = nonBindable;
  27029. this.projectAs = projectAs;
  27030. }
  27031. }
  27032. function normalizeNgContentSelect(selectAttr) {
  27033. if (selectAttr === null || selectAttr.length === 0) {
  27034. return '*';
  27035. }
  27036. return selectAttr;
  27037. }
  27038. /** Pattern for the expression in a for loop block. */
  27039. const FOR_LOOP_EXPRESSION_PATTERN = /^\s*([0-9A-Za-z_$]*)\s+of\s+([\S\s]*)/;
  27040. /** Pattern for the tracking expression in a for loop block. */
  27041. const FOR_LOOP_TRACK_PATTERN = /^track\s+([\S\s]*)/;
  27042. /** Pattern for the `as` expression in a conditional block. */
  27043. const CONDITIONAL_ALIAS_PATTERN = /^(as\s)+(.*)/;
  27044. /** Pattern used to identify an `else if` block. */
  27045. const ELSE_IF_PATTERN = /^else[^\S\r\n]+if/;
  27046. /** Pattern used to identify a `let` parameter. */
  27047. const FOR_LOOP_LET_PATTERN = /^let\s+([\S\s]*)/;
  27048. /**
  27049. * Pattern to group a string into leading whitespace, non whitespace, and trailing whitespace.
  27050. * Useful for getting the variable name span when a span can contain leading and trailing space.
  27051. */
  27052. const CHARACTERS_IN_SURROUNDING_WHITESPACE_PATTERN = /(\s*)(\S+)(\s*)/;
  27053. /** Names of variables that are allowed to be used in the `let` expression of a `for` loop. */
  27054. const ALLOWED_FOR_LOOP_LET_VARIABLES = new Set([
  27055. '$index',
  27056. '$first',
  27057. '$last',
  27058. '$even',
  27059. '$odd',
  27060. '$count',
  27061. ]);
  27062. /**
  27063. * Predicate function that determines if a block with
  27064. * a specific name cam be connected to a `for` block.
  27065. */
  27066. function isConnectedForLoopBlock(name) {
  27067. return name === 'empty';
  27068. }
  27069. /**
  27070. * Predicate function that determines if a block with
  27071. * a specific name cam be connected to an `if` block.
  27072. */
  27073. function isConnectedIfLoopBlock(name) {
  27074. return name === 'else' || ELSE_IF_PATTERN.test(name);
  27075. }
  27076. /** Creates an `if` loop block from an HTML AST node. */
  27077. function createIfBlock(ast, connectedBlocks, visitor, bindingParser) {
  27078. const errors = validateIfConnectedBlocks(connectedBlocks);
  27079. const branches = [];
  27080. const mainBlockParams = parseConditionalBlockParameters(ast, errors, bindingParser);
  27081. if (mainBlockParams !== null) {
  27082. branches.push(new IfBlockBranch(mainBlockParams.expression, visitAll(visitor, ast.children, ast.children), mainBlockParams.expressionAlias, ast.sourceSpan, ast.startSourceSpan, ast.endSourceSpan, ast.nameSpan, ast.i18n));
  27083. }
  27084. for (const block of connectedBlocks) {
  27085. if (ELSE_IF_PATTERN.test(block.name)) {
  27086. const params = parseConditionalBlockParameters(block, errors, bindingParser);
  27087. if (params !== null) {
  27088. const children = visitAll(visitor, block.children, block.children);
  27089. branches.push(new IfBlockBranch(params.expression, children, params.expressionAlias, block.sourceSpan, block.startSourceSpan, block.endSourceSpan, block.nameSpan, block.i18n));
  27090. }
  27091. }
  27092. else if (block.name === 'else') {
  27093. const children = visitAll(visitor, block.children, block.children);
  27094. branches.push(new IfBlockBranch(null, children, null, block.sourceSpan, block.startSourceSpan, block.endSourceSpan, block.nameSpan, block.i18n));
  27095. }
  27096. }
  27097. // The outer IfBlock should have a span that encapsulates all branches.
  27098. const ifBlockStartSourceSpan = branches.length > 0 ? branches[0].startSourceSpan : ast.startSourceSpan;
  27099. const ifBlockEndSourceSpan = branches.length > 0 ? branches[branches.length - 1].endSourceSpan : ast.endSourceSpan;
  27100. let wholeSourceSpan = ast.sourceSpan;
  27101. const lastBranch = branches[branches.length - 1];
  27102. if (lastBranch !== undefined) {
  27103. wholeSourceSpan = new ParseSourceSpan(ifBlockStartSourceSpan.start, lastBranch.sourceSpan.end);
  27104. }
  27105. return {
  27106. node: new IfBlock(branches, wholeSourceSpan, ast.startSourceSpan, ifBlockEndSourceSpan, ast.nameSpan),
  27107. errors,
  27108. };
  27109. }
  27110. /** Creates a `for` loop block from an HTML AST node. */
  27111. function createForLoop(ast, connectedBlocks, visitor, bindingParser) {
  27112. const errors = [];
  27113. const params = parseForLoopParameters(ast, errors, bindingParser);
  27114. let node = null;
  27115. let empty = null;
  27116. for (const block of connectedBlocks) {
  27117. if (block.name === 'empty') {
  27118. if (empty !== null) {
  27119. errors.push(new ParseError(block.sourceSpan, '@for loop can only have one @empty block'));
  27120. }
  27121. else if (block.parameters.length > 0) {
  27122. errors.push(new ParseError(block.sourceSpan, '@empty block cannot have parameters'));
  27123. }
  27124. else {
  27125. empty = new ForLoopBlockEmpty(visitAll(visitor, block.children, block.children), block.sourceSpan, block.startSourceSpan, block.endSourceSpan, block.nameSpan, block.i18n);
  27126. }
  27127. }
  27128. else {
  27129. errors.push(new ParseError(block.sourceSpan, `Unrecognized @for loop block "${block.name}"`));
  27130. }
  27131. }
  27132. if (params !== null) {
  27133. if (params.trackBy === null) {
  27134. // TODO: We should not fail here, and instead try to produce some AST for the language
  27135. // service.
  27136. errors.push(new ParseError(ast.startSourceSpan, '@for loop must have a "track" expression'));
  27137. }
  27138. else {
  27139. // The `for` block has a main span that includes the `empty` branch. For only the span of the
  27140. // main `for` body, use `mainSourceSpan`.
  27141. const endSpan = empty?.endSourceSpan ?? ast.endSourceSpan;
  27142. const sourceSpan = new ParseSourceSpan(ast.sourceSpan.start, endSpan?.end ?? ast.sourceSpan.end);
  27143. node = new ForLoopBlock(params.itemName, params.expression, params.trackBy.expression, params.trackBy.keywordSpan, params.context, visitAll(visitor, ast.children, ast.children), empty, sourceSpan, ast.sourceSpan, ast.startSourceSpan, endSpan, ast.nameSpan, ast.i18n);
  27144. }
  27145. }
  27146. return { node, errors };
  27147. }
  27148. /** Creates a switch block from an HTML AST node. */
  27149. function createSwitchBlock(ast, visitor, bindingParser) {
  27150. const errors = validateSwitchBlock(ast);
  27151. const primaryExpression = ast.parameters.length > 0
  27152. ? parseBlockParameterToBinding(ast.parameters[0], bindingParser)
  27153. : bindingParser.parseBinding('', false, ast.sourceSpan, 0);
  27154. const cases = [];
  27155. const unknownBlocks = [];
  27156. let defaultCase = null;
  27157. // Here we assume that all the blocks are valid given that we validated them above.
  27158. for (const node of ast.children) {
  27159. if (!(node instanceof Block)) {
  27160. continue;
  27161. }
  27162. if ((node.name !== 'case' || node.parameters.length === 0) && node.name !== 'default') {
  27163. unknownBlocks.push(new UnknownBlock(node.name, node.sourceSpan, node.nameSpan));
  27164. continue;
  27165. }
  27166. const expression = node.name === 'case' ? parseBlockParameterToBinding(node.parameters[0], bindingParser) : null;
  27167. const ast = new SwitchBlockCase(expression, visitAll(visitor, node.children, node.children), node.sourceSpan, node.startSourceSpan, node.endSourceSpan, node.nameSpan, node.i18n);
  27168. if (expression === null) {
  27169. defaultCase = ast;
  27170. }
  27171. else {
  27172. cases.push(ast);
  27173. }
  27174. }
  27175. // Ensure that the default case is last in the array.
  27176. if (defaultCase !== null) {
  27177. cases.push(defaultCase);
  27178. }
  27179. return {
  27180. node: new SwitchBlock(primaryExpression, cases, unknownBlocks, ast.sourceSpan, ast.startSourceSpan, ast.endSourceSpan, ast.nameSpan),
  27181. errors,
  27182. };
  27183. }
  27184. /** Parses the parameters of a `for` loop block. */
  27185. function parseForLoopParameters(block, errors, bindingParser) {
  27186. if (block.parameters.length === 0) {
  27187. errors.push(new ParseError(block.startSourceSpan, '@for loop does not have an expression'));
  27188. return null;
  27189. }
  27190. const [expressionParam, ...secondaryParams] = block.parameters;
  27191. const match = stripOptionalParentheses(expressionParam, errors)?.match(FOR_LOOP_EXPRESSION_PATTERN);
  27192. if (!match || match[2].trim().length === 0) {
  27193. errors.push(new ParseError(expressionParam.sourceSpan, 'Cannot parse expression. @for loop expression must match the pattern "<identifier> of <expression>"'));
  27194. return null;
  27195. }
  27196. const [, itemName, rawExpression] = match;
  27197. if (ALLOWED_FOR_LOOP_LET_VARIABLES.has(itemName)) {
  27198. errors.push(new ParseError(expressionParam.sourceSpan, `@for loop item name cannot be one of ${Array.from(ALLOWED_FOR_LOOP_LET_VARIABLES).join(', ')}.`));
  27199. }
  27200. // `expressionParam.expression` contains the variable declaration and the expression of the
  27201. // for...of statement, i.e. 'user of users' The variable of a ForOfStatement is _only_ the "const
  27202. // user" part and does not include "of x".
  27203. const variableName = expressionParam.expression.split(' ')[0];
  27204. const variableSpan = new ParseSourceSpan(expressionParam.sourceSpan.start, expressionParam.sourceSpan.start.moveBy(variableName.length));
  27205. const result = {
  27206. itemName: new Variable(itemName, '$implicit', variableSpan, variableSpan),
  27207. trackBy: null,
  27208. expression: parseBlockParameterToBinding(expressionParam, bindingParser, rawExpression),
  27209. context: Array.from(ALLOWED_FOR_LOOP_LET_VARIABLES, (variableName) => {
  27210. // Give ambiently-available context variables empty spans at the end of
  27211. // the start of the `for` block, since they are not explicitly defined.
  27212. const emptySpanAfterForBlockStart = new ParseSourceSpan(block.startSourceSpan.end, block.startSourceSpan.end);
  27213. return new Variable(variableName, variableName, emptySpanAfterForBlockStart, emptySpanAfterForBlockStart);
  27214. }),
  27215. };
  27216. for (const param of secondaryParams) {
  27217. const letMatch = param.expression.match(FOR_LOOP_LET_PATTERN);
  27218. if (letMatch !== null) {
  27219. const variablesSpan = new ParseSourceSpan(param.sourceSpan.start.moveBy(letMatch[0].length - letMatch[1].length), param.sourceSpan.end);
  27220. parseLetParameter(param.sourceSpan, letMatch[1], variablesSpan, itemName, result.context, errors);
  27221. continue;
  27222. }
  27223. const trackMatch = param.expression.match(FOR_LOOP_TRACK_PATTERN);
  27224. if (trackMatch !== null) {
  27225. if (result.trackBy !== null) {
  27226. errors.push(new ParseError(param.sourceSpan, '@for loop can only have one "track" expression'));
  27227. }
  27228. else {
  27229. const expression = parseBlockParameterToBinding(param, bindingParser, trackMatch[1]);
  27230. if (expression.ast instanceof EmptyExpr$1) {
  27231. errors.push(new ParseError(block.startSourceSpan, '@for loop must have a "track" expression'));
  27232. }
  27233. const keywordSpan = new ParseSourceSpan(param.sourceSpan.start, param.sourceSpan.start.moveBy('track'.length));
  27234. result.trackBy = { expression, keywordSpan };
  27235. }
  27236. continue;
  27237. }
  27238. errors.push(new ParseError(param.sourceSpan, `Unrecognized @for loop parameter "${param.expression}"`));
  27239. }
  27240. return result;
  27241. }
  27242. /** Parses the `let` parameter of a `for` loop block. */
  27243. function parseLetParameter(sourceSpan, expression, span, loopItemName, context, errors) {
  27244. const parts = expression.split(',');
  27245. let startSpan = span.start;
  27246. for (const part of parts) {
  27247. const expressionParts = part.split('=');
  27248. const name = expressionParts.length === 2 ? expressionParts[0].trim() : '';
  27249. const variableName = expressionParts.length === 2 ? expressionParts[1].trim() : '';
  27250. if (name.length === 0 || variableName.length === 0) {
  27251. errors.push(new ParseError(sourceSpan, `Invalid @for loop "let" parameter. Parameter should match the pattern "<name> = <variable name>"`));
  27252. }
  27253. else if (!ALLOWED_FOR_LOOP_LET_VARIABLES.has(variableName)) {
  27254. errors.push(new ParseError(sourceSpan, `Unknown "let" parameter variable "${variableName}". The allowed variables are: ${Array.from(ALLOWED_FOR_LOOP_LET_VARIABLES).join(', ')}`));
  27255. }
  27256. else if (name === loopItemName) {
  27257. errors.push(new ParseError(sourceSpan, `Invalid @for loop "let" parameter. Variable cannot be called "${loopItemName}"`));
  27258. }
  27259. else if (context.some((v) => v.name === name)) {
  27260. errors.push(new ParseError(sourceSpan, `Duplicate "let" parameter variable "${variableName}"`));
  27261. }
  27262. else {
  27263. const [, keyLeadingWhitespace, keyName] = expressionParts[0].match(CHARACTERS_IN_SURROUNDING_WHITESPACE_PATTERN) ?? [];
  27264. const keySpan = keyLeadingWhitespace !== undefined && expressionParts.length === 2
  27265. ? new ParseSourceSpan(
  27266. /* strip leading spaces */
  27267. startSpan.moveBy(keyLeadingWhitespace.length),
  27268. /* advance to end of the variable name */
  27269. startSpan.moveBy(keyLeadingWhitespace.length + keyName.length))
  27270. : span;
  27271. let valueSpan = undefined;
  27272. if (expressionParts.length === 2) {
  27273. const [, valueLeadingWhitespace, implicit] = expressionParts[1].match(CHARACTERS_IN_SURROUNDING_WHITESPACE_PATTERN) ?? [];
  27274. valueSpan =
  27275. valueLeadingWhitespace !== undefined
  27276. ? new ParseSourceSpan(startSpan.moveBy(expressionParts[0].length + 1 + valueLeadingWhitespace.length), startSpan.moveBy(expressionParts[0].length + 1 + valueLeadingWhitespace.length + implicit.length))
  27277. : undefined;
  27278. }
  27279. const sourceSpan = new ParseSourceSpan(keySpan.start, valueSpan?.end ?? keySpan.end);
  27280. context.push(new Variable(name, variableName, sourceSpan, keySpan, valueSpan));
  27281. }
  27282. startSpan = startSpan.moveBy(part.length + 1 /* add 1 to move past the comma */);
  27283. }
  27284. }
  27285. /**
  27286. * Checks that the shape of the blocks connected to an
  27287. * `@if` block is correct. Returns an array of errors.
  27288. */
  27289. function validateIfConnectedBlocks(connectedBlocks) {
  27290. const errors = [];
  27291. let hasElse = false;
  27292. for (let i = 0; i < connectedBlocks.length; i++) {
  27293. const block = connectedBlocks[i];
  27294. if (block.name === 'else') {
  27295. if (hasElse) {
  27296. errors.push(new ParseError(block.startSourceSpan, 'Conditional can only have one @else block'));
  27297. }
  27298. else if (connectedBlocks.length > 1 && i < connectedBlocks.length - 1) {
  27299. errors.push(new ParseError(block.startSourceSpan, '@else block must be last inside the conditional'));
  27300. }
  27301. else if (block.parameters.length > 0) {
  27302. errors.push(new ParseError(block.startSourceSpan, '@else block cannot have parameters'));
  27303. }
  27304. hasElse = true;
  27305. }
  27306. else if (!ELSE_IF_PATTERN.test(block.name)) {
  27307. errors.push(new ParseError(block.startSourceSpan, `Unrecognized conditional block @${block.name}`));
  27308. }
  27309. }
  27310. return errors;
  27311. }
  27312. /** Checks that the shape of a `switch` block is valid. Returns an array of errors. */
  27313. function validateSwitchBlock(ast) {
  27314. const errors = [];
  27315. let hasDefault = false;
  27316. if (ast.parameters.length !== 1) {
  27317. errors.push(new ParseError(ast.startSourceSpan, '@switch block must have exactly one parameter'));
  27318. return errors;
  27319. }
  27320. for (const node of ast.children) {
  27321. // Skip over comments and empty text nodes inside the switch block.
  27322. // Empty text nodes can be used for formatting while comments don't affect the runtime.
  27323. if (node instanceof Comment ||
  27324. (node instanceof Text && node.value.trim().length === 0)) {
  27325. continue;
  27326. }
  27327. if (!(node instanceof Block) || (node.name !== 'case' && node.name !== 'default')) {
  27328. errors.push(new ParseError(node.sourceSpan, '@switch block can only contain @case and @default blocks'));
  27329. continue;
  27330. }
  27331. if (node.name === 'default') {
  27332. if (hasDefault) {
  27333. errors.push(new ParseError(node.startSourceSpan, '@switch block can only have one @default block'));
  27334. }
  27335. else if (node.parameters.length > 0) {
  27336. errors.push(new ParseError(node.startSourceSpan, '@default block cannot have parameters'));
  27337. }
  27338. hasDefault = true;
  27339. }
  27340. else if (node.name === 'case' && node.parameters.length !== 1) {
  27341. errors.push(new ParseError(node.startSourceSpan, '@case block must have exactly one parameter'));
  27342. }
  27343. }
  27344. return errors;
  27345. }
  27346. /**
  27347. * Parses a block parameter into a binding AST.
  27348. * @param ast Block parameter that should be parsed.
  27349. * @param bindingParser Parser that the expression should be parsed with.
  27350. * @param part Specific part of the expression that should be parsed.
  27351. */
  27352. function parseBlockParameterToBinding(ast, bindingParser, part) {
  27353. let start;
  27354. let end;
  27355. if (typeof part === 'string') {
  27356. // Note: `lastIndexOf` here should be enough to know the start index of the expression,
  27357. // because we know that it'll be at the end of the param. Ideally we could use the `d`
  27358. // flag when matching via regex and get the index from `match.indices`, but it's unclear
  27359. // if we can use it yet since it's a relatively new feature. See:
  27360. // https://github.com/tc39/proposal-regexp-match-indices
  27361. start = Math.max(0, ast.expression.lastIndexOf(part));
  27362. end = start + part.length;
  27363. }
  27364. else {
  27365. start = 0;
  27366. end = ast.expression.length;
  27367. }
  27368. return bindingParser.parseBinding(ast.expression.slice(start, end), false, ast.sourceSpan, ast.sourceSpan.start.offset + start);
  27369. }
  27370. /** Parses the parameter of a conditional block (`if` or `else if`). */
  27371. function parseConditionalBlockParameters(block, errors, bindingParser) {
  27372. if (block.parameters.length === 0) {
  27373. errors.push(new ParseError(block.startSourceSpan, 'Conditional block does not have an expression'));
  27374. return null;
  27375. }
  27376. const expression = parseBlockParameterToBinding(block.parameters[0], bindingParser);
  27377. let expressionAlias = null;
  27378. // Start from 1 since we processed the first parameter already.
  27379. for (let i = 1; i < block.parameters.length; i++) {
  27380. const param = block.parameters[i];
  27381. const aliasMatch = param.expression.match(CONDITIONAL_ALIAS_PATTERN);
  27382. // For now conditionals can only have an `as` parameter.
  27383. // We may want to rework this later if we add more.
  27384. if (aliasMatch === null) {
  27385. errors.push(new ParseError(param.sourceSpan, `Unrecognized conditional parameter "${param.expression}"`));
  27386. }
  27387. else if (block.name !== 'if') {
  27388. errors.push(new ParseError(param.sourceSpan, '"as" expression is only allowed on the primary @if block'));
  27389. }
  27390. else if (expressionAlias !== null) {
  27391. errors.push(new ParseError(param.sourceSpan, 'Conditional can only have one "as" expression'));
  27392. }
  27393. else {
  27394. const name = aliasMatch[2].trim();
  27395. const variableStart = param.sourceSpan.start.moveBy(aliasMatch[1].length);
  27396. const variableSpan = new ParseSourceSpan(variableStart, variableStart.moveBy(name.length));
  27397. expressionAlias = new Variable(name, name, variableSpan, variableSpan);
  27398. }
  27399. }
  27400. return { expression, expressionAlias };
  27401. }
  27402. /** Strips optional parentheses around from a control from expression parameter. */
  27403. function stripOptionalParentheses(param, errors) {
  27404. const expression = param.expression;
  27405. const spaceRegex = /^\s$/;
  27406. let openParens = 0;
  27407. let start = 0;
  27408. let end = expression.length - 1;
  27409. for (let i = 0; i < expression.length; i++) {
  27410. const char = expression[i];
  27411. if (char === '(') {
  27412. start = i + 1;
  27413. openParens++;
  27414. }
  27415. else if (spaceRegex.test(char)) {
  27416. continue;
  27417. }
  27418. else {
  27419. break;
  27420. }
  27421. }
  27422. if (openParens === 0) {
  27423. return expression;
  27424. }
  27425. for (let i = expression.length - 1; i > -1; i--) {
  27426. const char = expression[i];
  27427. if (char === ')') {
  27428. end = i;
  27429. openParens--;
  27430. if (openParens === 0) {
  27431. break;
  27432. }
  27433. }
  27434. else if (spaceRegex.test(char)) {
  27435. continue;
  27436. }
  27437. else {
  27438. break;
  27439. }
  27440. }
  27441. if (openParens !== 0) {
  27442. errors.push(new ParseError(param.sourceSpan, 'Unclosed parentheses in expression'));
  27443. return null;
  27444. }
  27445. return expression.slice(start, end);
  27446. }
  27447. /** Pattern for a timing value in a trigger. */
  27448. const TIME_PATTERN = /^\d+\.?\d*(ms|s)?$/;
  27449. /** Pattern for a separator between keywords in a trigger expression. */
  27450. const SEPARATOR_PATTERN = /^\s$/;
  27451. /** Pairs of characters that form syntax that is comma-delimited. */
  27452. const COMMA_DELIMITED_SYNTAX = new Map([
  27453. [$LBRACE, $RBRACE], // Object literals
  27454. [$LBRACKET, $RBRACKET], // Array literals
  27455. [$LPAREN, $RPAREN], // Function calls
  27456. ]);
  27457. /** Possible types of `on` triggers. */
  27458. var OnTriggerType;
  27459. (function (OnTriggerType) {
  27460. OnTriggerType["IDLE"] = "idle";
  27461. OnTriggerType["TIMER"] = "timer";
  27462. OnTriggerType["INTERACTION"] = "interaction";
  27463. OnTriggerType["IMMEDIATE"] = "immediate";
  27464. OnTriggerType["HOVER"] = "hover";
  27465. OnTriggerType["VIEWPORT"] = "viewport";
  27466. OnTriggerType["NEVER"] = "never";
  27467. })(OnTriggerType || (OnTriggerType = {}));
  27468. /** Parses a `when` deferred trigger. */
  27469. function parseNeverTrigger({ expression, sourceSpan }, triggers, errors) {
  27470. const neverIndex = expression.indexOf('never');
  27471. const neverSourceSpan = new ParseSourceSpan(sourceSpan.start.moveBy(neverIndex), sourceSpan.start.moveBy(neverIndex + 'never'.length));
  27472. const prefetchSpan = getPrefetchSpan(expression, sourceSpan);
  27473. const hydrateSpan = getHydrateSpan(expression, sourceSpan);
  27474. // This is here just to be safe, we shouldn't enter this function
  27475. // in the first place if a block doesn't have the "on" keyword.
  27476. if (neverIndex === -1) {
  27477. errors.push(new ParseError(sourceSpan, `Could not find "never" keyword in expression`));
  27478. }
  27479. else {
  27480. trackTrigger('never', triggers, errors, new NeverDeferredTrigger(neverSourceSpan, sourceSpan, prefetchSpan, null, hydrateSpan));
  27481. }
  27482. }
  27483. /** Parses a `when` deferred trigger. */
  27484. function parseWhenTrigger({ expression, sourceSpan }, bindingParser, triggers, errors) {
  27485. const whenIndex = expression.indexOf('when');
  27486. const whenSourceSpan = new ParseSourceSpan(sourceSpan.start.moveBy(whenIndex), sourceSpan.start.moveBy(whenIndex + 'when'.length));
  27487. const prefetchSpan = getPrefetchSpan(expression, sourceSpan);
  27488. const hydrateSpan = getHydrateSpan(expression, sourceSpan);
  27489. // This is here just to be safe, we shouldn't enter this function
  27490. // in the first place if a block doesn't have the "when" keyword.
  27491. if (whenIndex === -1) {
  27492. errors.push(new ParseError(sourceSpan, `Could not find "when" keyword in expression`));
  27493. }
  27494. else {
  27495. const start = getTriggerParametersStart(expression, whenIndex + 1);
  27496. const parsed = bindingParser.parseBinding(expression.slice(start), false, sourceSpan, sourceSpan.start.offset + start);
  27497. trackTrigger('when', triggers, errors, new BoundDeferredTrigger(parsed, sourceSpan, prefetchSpan, whenSourceSpan, hydrateSpan));
  27498. }
  27499. }
  27500. /** Parses an `on` trigger */
  27501. function parseOnTrigger({ expression, sourceSpan }, triggers, errors, placeholder) {
  27502. const onIndex = expression.indexOf('on');
  27503. const onSourceSpan = new ParseSourceSpan(sourceSpan.start.moveBy(onIndex), sourceSpan.start.moveBy(onIndex + 'on'.length));
  27504. const prefetchSpan = getPrefetchSpan(expression, sourceSpan);
  27505. const hydrateSpan = getHydrateSpan(expression, sourceSpan);
  27506. // This is here just to be safe, we shouldn't enter this function
  27507. // in the first place if a block doesn't have the "on" keyword.
  27508. if (onIndex === -1) {
  27509. errors.push(new ParseError(sourceSpan, `Could not find "on" keyword in expression`));
  27510. }
  27511. else {
  27512. const start = getTriggerParametersStart(expression, onIndex + 1);
  27513. const parser = new OnTriggerParser(expression, start, sourceSpan, triggers, errors, expression.startsWith('hydrate')
  27514. ? validateHydrateReferenceBasedTrigger
  27515. : validatePlainReferenceBasedTrigger, placeholder, prefetchSpan, onSourceSpan, hydrateSpan);
  27516. parser.parse();
  27517. }
  27518. }
  27519. function getPrefetchSpan(expression, sourceSpan) {
  27520. if (!expression.startsWith('prefetch')) {
  27521. return null;
  27522. }
  27523. return new ParseSourceSpan(sourceSpan.start, sourceSpan.start.moveBy('prefetch'.length));
  27524. }
  27525. function getHydrateSpan(expression, sourceSpan) {
  27526. if (!expression.startsWith('hydrate')) {
  27527. return null;
  27528. }
  27529. return new ParseSourceSpan(sourceSpan.start, sourceSpan.start.moveBy('hydrate'.length));
  27530. }
  27531. class OnTriggerParser {
  27532. expression;
  27533. start;
  27534. span;
  27535. triggers;
  27536. errors;
  27537. validator;
  27538. placeholder;
  27539. prefetchSpan;
  27540. onSourceSpan;
  27541. hydrateSpan;
  27542. index = 0;
  27543. tokens;
  27544. constructor(expression, start, span, triggers, errors, validator, placeholder, prefetchSpan, onSourceSpan, hydrateSpan) {
  27545. this.expression = expression;
  27546. this.start = start;
  27547. this.span = span;
  27548. this.triggers = triggers;
  27549. this.errors = errors;
  27550. this.validator = validator;
  27551. this.placeholder = placeholder;
  27552. this.prefetchSpan = prefetchSpan;
  27553. this.onSourceSpan = onSourceSpan;
  27554. this.hydrateSpan = hydrateSpan;
  27555. this.tokens = new Lexer().tokenize(expression.slice(start));
  27556. }
  27557. parse() {
  27558. while (this.tokens.length > 0 && this.index < this.tokens.length) {
  27559. const token = this.token();
  27560. if (!token.isIdentifier()) {
  27561. this.unexpectedToken(token);
  27562. break;
  27563. }
  27564. // An identifier immediately followed by a comma or the end of
  27565. // the expression cannot have parameters so we can exit early.
  27566. if (this.isFollowedByOrLast($COMMA)) {
  27567. this.consumeTrigger(token, []);
  27568. this.advance();
  27569. }
  27570. else if (this.isFollowedByOrLast($LPAREN)) {
  27571. this.advance(); // Advance to the opening paren.
  27572. const prevErrors = this.errors.length;
  27573. const parameters = this.consumeParameters();
  27574. if (this.errors.length !== prevErrors) {
  27575. break;
  27576. }
  27577. this.consumeTrigger(token, parameters);
  27578. this.advance(); // Advance past the closing paren.
  27579. }
  27580. else if (this.index < this.tokens.length - 1) {
  27581. this.unexpectedToken(this.tokens[this.index + 1]);
  27582. }
  27583. this.advance();
  27584. }
  27585. }
  27586. advance() {
  27587. this.index++;
  27588. }
  27589. isFollowedByOrLast(char) {
  27590. if (this.index === this.tokens.length - 1) {
  27591. return true;
  27592. }
  27593. return this.tokens[this.index + 1].isCharacter(char);
  27594. }
  27595. token() {
  27596. return this.tokens[Math.min(this.index, this.tokens.length - 1)];
  27597. }
  27598. consumeTrigger(identifier, parameters) {
  27599. const triggerNameStartSpan = this.span.start.moveBy(this.start + identifier.index - this.tokens[0].index);
  27600. const nameSpan = new ParseSourceSpan(triggerNameStartSpan, triggerNameStartSpan.moveBy(identifier.strValue.length));
  27601. const endSpan = triggerNameStartSpan.moveBy(this.token().end - identifier.index);
  27602. // Put the prefetch and on spans with the first trigger
  27603. // This should maybe be refactored to have something like an outer OnGroup AST
  27604. // Since triggers can be grouped with commas "on hover(x), interaction(y)"
  27605. const isFirstTrigger = identifier.index === 0;
  27606. const onSourceSpan = isFirstTrigger ? this.onSourceSpan : null;
  27607. const prefetchSourceSpan = isFirstTrigger ? this.prefetchSpan : null;
  27608. const hydrateSourceSpan = isFirstTrigger ? this.hydrateSpan : null;
  27609. const sourceSpan = new ParseSourceSpan(isFirstTrigger ? this.span.start : triggerNameStartSpan, endSpan);
  27610. try {
  27611. switch (identifier.toString()) {
  27612. case OnTriggerType.IDLE:
  27613. this.trackTrigger('idle', createIdleTrigger(parameters, nameSpan, sourceSpan, prefetchSourceSpan, onSourceSpan, hydrateSourceSpan));
  27614. break;
  27615. case OnTriggerType.TIMER:
  27616. this.trackTrigger('timer', createTimerTrigger(parameters, nameSpan, sourceSpan, this.prefetchSpan, this.onSourceSpan, this.hydrateSpan));
  27617. break;
  27618. case OnTriggerType.INTERACTION:
  27619. this.trackTrigger('interaction', createInteractionTrigger(parameters, nameSpan, sourceSpan, this.prefetchSpan, this.onSourceSpan, this.hydrateSpan, this.placeholder, this.validator));
  27620. break;
  27621. case OnTriggerType.IMMEDIATE:
  27622. this.trackTrigger('immediate', createImmediateTrigger(parameters, nameSpan, sourceSpan, this.prefetchSpan, this.onSourceSpan, this.hydrateSpan));
  27623. break;
  27624. case OnTriggerType.HOVER:
  27625. this.trackTrigger('hover', createHoverTrigger(parameters, nameSpan, sourceSpan, this.prefetchSpan, this.onSourceSpan, this.hydrateSpan, this.placeholder, this.validator));
  27626. break;
  27627. case OnTriggerType.VIEWPORT:
  27628. this.trackTrigger('viewport', createViewportTrigger(parameters, nameSpan, sourceSpan, this.prefetchSpan, this.onSourceSpan, this.hydrateSpan, this.placeholder, this.validator));
  27629. break;
  27630. default:
  27631. throw new Error(`Unrecognized trigger type "${identifier}"`);
  27632. }
  27633. }
  27634. catch (e) {
  27635. this.error(identifier, e.message);
  27636. }
  27637. }
  27638. consumeParameters() {
  27639. const parameters = [];
  27640. if (!this.token().isCharacter($LPAREN)) {
  27641. this.unexpectedToken(this.token());
  27642. return parameters;
  27643. }
  27644. this.advance();
  27645. const commaDelimStack = [];
  27646. let current = '';
  27647. while (this.index < this.tokens.length) {
  27648. const token = this.token();
  27649. // Stop parsing if we've hit the end character and we're outside of a comma-delimited syntax.
  27650. // Note that we don't need to account for strings here since the lexer already parsed them
  27651. // into string tokens.
  27652. if (token.isCharacter($RPAREN) && commaDelimStack.length === 0) {
  27653. if (current.length) {
  27654. parameters.push(current);
  27655. }
  27656. break;
  27657. }
  27658. // In the `on` microsyntax "top-level" commas (e.g. ones outside of an parameters) separate
  27659. // the different triggers (e.g. `on idle,timer(500)`). This is problematic, because the
  27660. // function-like syntax also implies that multiple parameters can be passed into the
  27661. // individual trigger (e.g. `on foo(a, b)`). To avoid tripping up the parser with commas that
  27662. // are part of other sorts of syntax (object literals, arrays), we treat anything inside
  27663. // a comma-delimited syntax block as plain text.
  27664. if (token.type === TokenType.Character && COMMA_DELIMITED_SYNTAX.has(token.numValue)) {
  27665. commaDelimStack.push(COMMA_DELIMITED_SYNTAX.get(token.numValue));
  27666. }
  27667. if (commaDelimStack.length > 0 &&
  27668. token.isCharacter(commaDelimStack[commaDelimStack.length - 1])) {
  27669. commaDelimStack.pop();
  27670. }
  27671. // If we hit a comma outside of a comma-delimited syntax, it means
  27672. // that we're at the top level and we're starting a new parameter.
  27673. if (commaDelimStack.length === 0 && token.isCharacter($COMMA) && current.length > 0) {
  27674. parameters.push(current);
  27675. current = '';
  27676. this.advance();
  27677. continue;
  27678. }
  27679. // Otherwise treat the token as a plain text character in the current parameter.
  27680. current += this.tokenText();
  27681. this.advance();
  27682. }
  27683. if (!this.token().isCharacter($RPAREN) || commaDelimStack.length > 0) {
  27684. this.error(this.token(), 'Unexpected end of expression');
  27685. }
  27686. if (this.index < this.tokens.length - 1 &&
  27687. !this.tokens[this.index + 1].isCharacter($COMMA)) {
  27688. this.unexpectedToken(this.tokens[this.index + 1]);
  27689. }
  27690. return parameters;
  27691. }
  27692. tokenText() {
  27693. // Tokens have a toString already which we could use, but for string tokens it omits the quotes.
  27694. // Eventually we could expose this information on the token directly.
  27695. return this.expression.slice(this.start + this.token().index, this.start + this.token().end);
  27696. }
  27697. trackTrigger(name, trigger) {
  27698. trackTrigger(name, this.triggers, this.errors, trigger);
  27699. }
  27700. error(token, message) {
  27701. const newStart = this.span.start.moveBy(this.start + token.index);
  27702. const newEnd = newStart.moveBy(token.end - token.index);
  27703. this.errors.push(new ParseError(new ParseSourceSpan(newStart, newEnd), message));
  27704. }
  27705. unexpectedToken(token) {
  27706. this.error(token, `Unexpected token "${token}"`);
  27707. }
  27708. }
  27709. /** Adds a trigger to a map of triggers. */
  27710. function trackTrigger(name, allTriggers, errors, trigger) {
  27711. if (allTriggers[name]) {
  27712. errors.push(new ParseError(trigger.sourceSpan, `Duplicate "${name}" trigger is not allowed`));
  27713. }
  27714. else {
  27715. allTriggers[name] = trigger;
  27716. }
  27717. }
  27718. function createIdleTrigger(parameters, nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan) {
  27719. if (parameters.length > 0) {
  27720. throw new Error(`"${OnTriggerType.IDLE}" trigger cannot have parameters`);
  27721. }
  27722. return new IdleDeferredTrigger(nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan);
  27723. }
  27724. function createTimerTrigger(parameters, nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan) {
  27725. if (parameters.length !== 1) {
  27726. throw new Error(`"${OnTriggerType.TIMER}" trigger must have exactly one parameter`);
  27727. }
  27728. const delay = parseDeferredTime(parameters[0]);
  27729. if (delay === null) {
  27730. throw new Error(`Could not parse time value of trigger "${OnTriggerType.TIMER}"`);
  27731. }
  27732. return new TimerDeferredTrigger(delay, nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan);
  27733. }
  27734. function createImmediateTrigger(parameters, nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan) {
  27735. if (parameters.length > 0) {
  27736. throw new Error(`"${OnTriggerType.IMMEDIATE}" trigger cannot have parameters`);
  27737. }
  27738. return new ImmediateDeferredTrigger(nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan);
  27739. }
  27740. function createHoverTrigger(parameters, nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan, placeholder, validator) {
  27741. validator(OnTriggerType.HOVER, parameters, placeholder);
  27742. return new HoverDeferredTrigger(parameters[0] ?? null, nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan);
  27743. }
  27744. function createInteractionTrigger(parameters, nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan, placeholder, validator) {
  27745. validator(OnTriggerType.INTERACTION, parameters, placeholder);
  27746. return new InteractionDeferredTrigger(parameters[0] ?? null, nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan);
  27747. }
  27748. function createViewportTrigger(parameters, nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan, placeholder, validator) {
  27749. validator(OnTriggerType.VIEWPORT, parameters, placeholder);
  27750. return new ViewportDeferredTrigger(parameters[0] ?? null, nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan);
  27751. }
  27752. /**
  27753. * Checks whether the structure of a non-hydrate reference-based trigger is valid.
  27754. * @param type Type of the trigger being validated.
  27755. * @param parameters Parameters of the trigger.
  27756. * @param placeholder Placeholder of the defer block.
  27757. */
  27758. function validatePlainReferenceBasedTrigger(type, parameters, placeholder) {
  27759. if (parameters.length > 1) {
  27760. throw new Error(`"${type}" trigger can only have zero or one parameters`);
  27761. }
  27762. if (parameters.length === 0) {
  27763. if (placeholder === null) {
  27764. throw new Error(`"${type}" trigger with no parameters can only be placed on an @defer that has a @placeholder block`);
  27765. }
  27766. if (placeholder.children.length !== 1 || !(placeholder.children[0] instanceof Element$1)) {
  27767. throw new Error(`"${type}" trigger with no parameters can only be placed on an @defer that has a ` +
  27768. `@placeholder block with exactly one root element node`);
  27769. }
  27770. }
  27771. }
  27772. /**
  27773. * Checks whether the structure of a hydrate trigger is valid.
  27774. * @param type Type of the trigger being validated.
  27775. * @param parameters Parameters of the trigger.
  27776. */
  27777. function validateHydrateReferenceBasedTrigger(type, parameters) {
  27778. if (parameters.length > 0) {
  27779. throw new Error(`Hydration trigger "${type}" cannot have parameters`);
  27780. }
  27781. }
  27782. /** Gets the index within an expression at which the trigger parameters start. */
  27783. function getTriggerParametersStart(value, startPosition = 0) {
  27784. let hasFoundSeparator = false;
  27785. for (let i = startPosition; i < value.length; i++) {
  27786. if (SEPARATOR_PATTERN.test(value[i])) {
  27787. hasFoundSeparator = true;
  27788. }
  27789. else if (hasFoundSeparator) {
  27790. return i;
  27791. }
  27792. }
  27793. return -1;
  27794. }
  27795. /**
  27796. * Parses a time expression from a deferred trigger to
  27797. * milliseconds. Returns null if it cannot be parsed.
  27798. */
  27799. function parseDeferredTime(value) {
  27800. const match = value.match(TIME_PATTERN);
  27801. if (!match) {
  27802. return null;
  27803. }
  27804. const [time, units] = match;
  27805. return parseFloat(time) * (units === 's' ? 1000 : 1);
  27806. }
  27807. /** Pattern to identify a `prefetch when` trigger. */
  27808. const PREFETCH_WHEN_PATTERN = /^prefetch\s+when\s/;
  27809. /** Pattern to identify a `prefetch on` trigger. */
  27810. const PREFETCH_ON_PATTERN = /^prefetch\s+on\s/;
  27811. /** Pattern to identify a `hydrate when` trigger. */
  27812. const HYDRATE_WHEN_PATTERN = /^hydrate\s+when\s/;
  27813. /** Pattern to identify a `hydrate on` trigger. */
  27814. const HYDRATE_ON_PATTERN = /^hydrate\s+on\s/;
  27815. /** Pattern to identify a `hydrate never` trigger. */
  27816. const HYDRATE_NEVER_PATTERN = /^hydrate\s+never(\s*)$/;
  27817. /** Pattern to identify a `minimum` parameter in a block. */
  27818. const MINIMUM_PARAMETER_PATTERN = /^minimum\s/;
  27819. /** Pattern to identify a `after` parameter in a block. */
  27820. const AFTER_PARAMETER_PATTERN = /^after\s/;
  27821. /** Pattern to identify a `when` parameter in a block. */
  27822. const WHEN_PARAMETER_PATTERN = /^when\s/;
  27823. /** Pattern to identify a `on` parameter in a block. */
  27824. const ON_PARAMETER_PATTERN = /^on\s/;
  27825. /**
  27826. * Predicate function that determines if a block with
  27827. * a specific name cam be connected to a `defer` block.
  27828. */
  27829. function isConnectedDeferLoopBlock(name) {
  27830. return name === 'placeholder' || name === 'loading' || name === 'error';
  27831. }
  27832. /** Creates a deferred block from an HTML AST node. */
  27833. function createDeferredBlock(ast, connectedBlocks, visitor, bindingParser) {
  27834. const errors = [];
  27835. const { placeholder, loading, error } = parseConnectedBlocks(connectedBlocks, errors, visitor);
  27836. const { triggers, prefetchTriggers, hydrateTriggers } = parsePrimaryTriggers(ast, bindingParser, errors, placeholder);
  27837. // The `defer` block has a main span encompassing all of the connected branches as well.
  27838. let lastEndSourceSpan = ast.endSourceSpan;
  27839. let endOfLastSourceSpan = ast.sourceSpan.end;
  27840. if (connectedBlocks.length > 0) {
  27841. const lastConnectedBlock = connectedBlocks[connectedBlocks.length - 1];
  27842. lastEndSourceSpan = lastConnectedBlock.endSourceSpan;
  27843. endOfLastSourceSpan = lastConnectedBlock.sourceSpan.end;
  27844. }
  27845. const sourceSpanWithConnectedBlocks = new ParseSourceSpan(ast.sourceSpan.start, endOfLastSourceSpan);
  27846. const node = new DeferredBlock(visitAll(visitor, ast.children, ast.children), triggers, prefetchTriggers, hydrateTriggers, placeholder, loading, error, ast.nameSpan, sourceSpanWithConnectedBlocks, ast.sourceSpan, ast.startSourceSpan, lastEndSourceSpan, ast.i18n);
  27847. return { node, errors };
  27848. }
  27849. function parseConnectedBlocks(connectedBlocks, errors, visitor) {
  27850. let placeholder = null;
  27851. let loading = null;
  27852. let error = null;
  27853. for (const block of connectedBlocks) {
  27854. try {
  27855. if (!isConnectedDeferLoopBlock(block.name)) {
  27856. errors.push(new ParseError(block.startSourceSpan, `Unrecognized block "@${block.name}"`));
  27857. break;
  27858. }
  27859. switch (block.name) {
  27860. case 'placeholder':
  27861. if (placeholder !== null) {
  27862. errors.push(new ParseError(block.startSourceSpan, `@defer block can only have one @placeholder block`));
  27863. }
  27864. else {
  27865. placeholder = parsePlaceholderBlock(block, visitor);
  27866. }
  27867. break;
  27868. case 'loading':
  27869. if (loading !== null) {
  27870. errors.push(new ParseError(block.startSourceSpan, `@defer block can only have one @loading block`));
  27871. }
  27872. else {
  27873. loading = parseLoadingBlock(block, visitor);
  27874. }
  27875. break;
  27876. case 'error':
  27877. if (error !== null) {
  27878. errors.push(new ParseError(block.startSourceSpan, `@defer block can only have one @error block`));
  27879. }
  27880. else {
  27881. error = parseErrorBlock(block, visitor);
  27882. }
  27883. break;
  27884. }
  27885. }
  27886. catch (e) {
  27887. errors.push(new ParseError(block.startSourceSpan, e.message));
  27888. }
  27889. }
  27890. return { placeholder, loading, error };
  27891. }
  27892. function parsePlaceholderBlock(ast, visitor) {
  27893. let minimumTime = null;
  27894. for (const param of ast.parameters) {
  27895. if (MINIMUM_PARAMETER_PATTERN.test(param.expression)) {
  27896. if (minimumTime != null) {
  27897. throw new Error(`@placeholder block can only have one "minimum" parameter`);
  27898. }
  27899. const parsedTime = parseDeferredTime(param.expression.slice(getTriggerParametersStart(param.expression)));
  27900. if (parsedTime === null) {
  27901. throw new Error(`Could not parse time value of parameter "minimum"`);
  27902. }
  27903. minimumTime = parsedTime;
  27904. }
  27905. else {
  27906. throw new Error(`Unrecognized parameter in @placeholder block: "${param.expression}"`);
  27907. }
  27908. }
  27909. return new DeferredBlockPlaceholder(visitAll(visitor, ast.children, ast.children), minimumTime, ast.nameSpan, ast.sourceSpan, ast.startSourceSpan, ast.endSourceSpan, ast.i18n);
  27910. }
  27911. function parseLoadingBlock(ast, visitor) {
  27912. let afterTime = null;
  27913. let minimumTime = null;
  27914. for (const param of ast.parameters) {
  27915. if (AFTER_PARAMETER_PATTERN.test(param.expression)) {
  27916. if (afterTime != null) {
  27917. throw new Error(`@loading block can only have one "after" parameter`);
  27918. }
  27919. const parsedTime = parseDeferredTime(param.expression.slice(getTriggerParametersStart(param.expression)));
  27920. if (parsedTime === null) {
  27921. throw new Error(`Could not parse time value of parameter "after"`);
  27922. }
  27923. afterTime = parsedTime;
  27924. }
  27925. else if (MINIMUM_PARAMETER_PATTERN.test(param.expression)) {
  27926. if (minimumTime != null) {
  27927. throw new Error(`@loading block can only have one "minimum" parameter`);
  27928. }
  27929. const parsedTime = parseDeferredTime(param.expression.slice(getTriggerParametersStart(param.expression)));
  27930. if (parsedTime === null) {
  27931. throw new Error(`Could not parse time value of parameter "minimum"`);
  27932. }
  27933. minimumTime = parsedTime;
  27934. }
  27935. else {
  27936. throw new Error(`Unrecognized parameter in @loading block: "${param.expression}"`);
  27937. }
  27938. }
  27939. return new DeferredBlockLoading(visitAll(visitor, ast.children, ast.children), afterTime, minimumTime, ast.nameSpan, ast.sourceSpan, ast.startSourceSpan, ast.endSourceSpan, ast.i18n);
  27940. }
  27941. function parseErrorBlock(ast, visitor) {
  27942. if (ast.parameters.length > 0) {
  27943. throw new Error(`@error block cannot have parameters`);
  27944. }
  27945. return new DeferredBlockError(visitAll(visitor, ast.children, ast.children), ast.nameSpan, ast.sourceSpan, ast.startSourceSpan, ast.endSourceSpan, ast.i18n);
  27946. }
  27947. function parsePrimaryTriggers(ast, bindingParser, errors, placeholder) {
  27948. const triggers = {};
  27949. const prefetchTriggers = {};
  27950. const hydrateTriggers = {};
  27951. for (const param of ast.parameters) {
  27952. // The lexer ignores the leading spaces so we can assume
  27953. // that the expression starts with a keyword.
  27954. if (WHEN_PARAMETER_PATTERN.test(param.expression)) {
  27955. parseWhenTrigger(param, bindingParser, triggers, errors);
  27956. }
  27957. else if (ON_PARAMETER_PATTERN.test(param.expression)) {
  27958. parseOnTrigger(param, triggers, errors, placeholder);
  27959. }
  27960. else if (PREFETCH_WHEN_PATTERN.test(param.expression)) {
  27961. parseWhenTrigger(param, bindingParser, prefetchTriggers, errors);
  27962. }
  27963. else if (PREFETCH_ON_PATTERN.test(param.expression)) {
  27964. parseOnTrigger(param, prefetchTriggers, errors, placeholder);
  27965. }
  27966. else if (HYDRATE_WHEN_PATTERN.test(param.expression)) {
  27967. parseWhenTrigger(param, bindingParser, hydrateTriggers, errors);
  27968. }
  27969. else if (HYDRATE_ON_PATTERN.test(param.expression)) {
  27970. parseOnTrigger(param, hydrateTriggers, errors, placeholder);
  27971. }
  27972. else if (HYDRATE_NEVER_PATTERN.test(param.expression)) {
  27973. parseNeverTrigger(param, hydrateTriggers, errors);
  27974. }
  27975. else {
  27976. errors.push(new ParseError(param.sourceSpan, 'Unrecognized trigger'));
  27977. }
  27978. }
  27979. if (hydrateTriggers.never && Object.keys(hydrateTriggers).length > 1) {
  27980. errors.push(new ParseError(ast.startSourceSpan, 'Cannot specify additional `hydrate` triggers if `hydrate never` is present'));
  27981. }
  27982. return { triggers, prefetchTriggers, hydrateTriggers };
  27983. }
  27984. const BIND_NAME_REGEXP = /^(?:(bind-)|(let-)|(ref-|#)|(on-)|(bindon-)|(@))(.*)$/;
  27985. // Group 1 = "bind-"
  27986. const KW_BIND_IDX = 1;
  27987. // Group 2 = "let-"
  27988. const KW_LET_IDX = 2;
  27989. // Group 3 = "ref-/#"
  27990. const KW_REF_IDX = 3;
  27991. // Group 4 = "on-"
  27992. const KW_ON_IDX = 4;
  27993. // Group 5 = "bindon-"
  27994. const KW_BINDON_IDX = 5;
  27995. // Group 6 = "@"
  27996. const KW_AT_IDX = 6;
  27997. // Group 7 = the identifier after "bind-", "let-", "ref-/#", "on-", "bindon-" or "@"
  27998. const IDENT_KW_IDX = 7;
  27999. const BINDING_DELIMS = {
  28000. BANANA_BOX: { start: '[(', end: ')]' },
  28001. PROPERTY: { start: '[', end: ']' },
  28002. EVENT: { start: '(', end: ')' },
  28003. };
  28004. const TEMPLATE_ATTR_PREFIX = '*';
  28005. function htmlAstToRender3Ast(htmlNodes, bindingParser, options) {
  28006. const transformer = new HtmlAstToIvyAst(bindingParser, options);
  28007. const ivyNodes = visitAll(transformer, htmlNodes, htmlNodes);
  28008. // Errors might originate in either the binding parser or the html to ivy transformer
  28009. const allErrors = bindingParser.errors.concat(transformer.errors);
  28010. const result = {
  28011. nodes: ivyNodes,
  28012. errors: allErrors,
  28013. styleUrls: transformer.styleUrls,
  28014. styles: transformer.styles,
  28015. ngContentSelectors: transformer.ngContentSelectors,
  28016. };
  28017. if (options.collectCommentNodes) {
  28018. result.commentNodes = transformer.commentNodes;
  28019. }
  28020. return result;
  28021. }
  28022. class HtmlAstToIvyAst {
  28023. bindingParser;
  28024. options;
  28025. errors = [];
  28026. styles = [];
  28027. styleUrls = [];
  28028. ngContentSelectors = [];
  28029. // This array will be populated if `Render3ParseOptions['collectCommentNodes']` is true
  28030. commentNodes = [];
  28031. inI18nBlock = false;
  28032. /**
  28033. * Keeps track of the nodes that have been processed already when previous nodes were visited.
  28034. * These are typically blocks connected to other blocks or text nodes between connected blocks.
  28035. */
  28036. processedNodes = new Set();
  28037. constructor(bindingParser, options) {
  28038. this.bindingParser = bindingParser;
  28039. this.options = options;
  28040. }
  28041. // HTML visitor
  28042. visitElement(element) {
  28043. const isI18nRootElement = isI18nRootNode(element.i18n);
  28044. if (isI18nRootElement) {
  28045. if (this.inI18nBlock) {
  28046. this.reportError('Cannot mark an element as translatable inside of a translatable section. Please remove the nested i18n marker.', element.sourceSpan);
  28047. }
  28048. this.inI18nBlock = true;
  28049. }
  28050. const preparsedElement = preparseElement(element);
  28051. if (preparsedElement.type === PreparsedElementType.SCRIPT) {
  28052. return null;
  28053. }
  28054. else if (preparsedElement.type === PreparsedElementType.STYLE) {
  28055. const contents = textContents(element);
  28056. if (contents !== null) {
  28057. this.styles.push(contents);
  28058. }
  28059. return null;
  28060. }
  28061. else if (preparsedElement.type === PreparsedElementType.STYLESHEET &&
  28062. isStyleUrlResolvable(preparsedElement.hrefAttr)) {
  28063. this.styleUrls.push(preparsedElement.hrefAttr);
  28064. return null;
  28065. }
  28066. // Whether the element is a `<ng-template>`
  28067. const isTemplateElement = isNgTemplate(element.name);
  28068. const parsedProperties = [];
  28069. const boundEvents = [];
  28070. const variables = [];
  28071. const references = [];
  28072. const attributes = [];
  28073. const i18nAttrsMeta = {};
  28074. const templateParsedProperties = [];
  28075. const templateVariables = [];
  28076. // Whether the element has any *-attribute
  28077. let elementHasInlineTemplate = false;
  28078. for (const attribute of element.attrs) {
  28079. let hasBinding = false;
  28080. const normalizedName = normalizeAttributeName(attribute.name);
  28081. // `*attr` defines template bindings
  28082. let isTemplateBinding = false;
  28083. if (attribute.i18n) {
  28084. i18nAttrsMeta[attribute.name] = attribute.i18n;
  28085. }
  28086. if (normalizedName.startsWith(TEMPLATE_ATTR_PREFIX)) {
  28087. // *-attributes
  28088. if (elementHasInlineTemplate) {
  28089. this.reportError(`Can't have multiple template bindings on one element. Use only one attribute prefixed with *`, attribute.sourceSpan);
  28090. }
  28091. isTemplateBinding = true;
  28092. elementHasInlineTemplate = true;
  28093. const templateValue = attribute.value;
  28094. const templateKey = normalizedName.substring(TEMPLATE_ATTR_PREFIX.length);
  28095. const parsedVariables = [];
  28096. const absoluteValueOffset = attribute.valueSpan
  28097. ? attribute.valueSpan.start.offset
  28098. : // If there is no value span the attribute does not have a value, like `attr` in
  28099. //`<div attr></div>`. In this case, point to one character beyond the last character of
  28100. // the attribute name.
  28101. attribute.sourceSpan.start.offset + attribute.name.length;
  28102. this.bindingParser.parseInlineTemplateBinding(templateKey, templateValue, attribute.sourceSpan, absoluteValueOffset, [], templateParsedProperties, parsedVariables, true /* isIvyAst */);
  28103. templateVariables.push(...parsedVariables.map((v) => new Variable(v.name, v.value, v.sourceSpan, v.keySpan, v.valueSpan)));
  28104. }
  28105. else {
  28106. // Check for variables, events, property bindings, interpolation
  28107. hasBinding = this.parseAttribute(isTemplateElement, attribute, [], parsedProperties, boundEvents, variables, references);
  28108. }
  28109. if (!hasBinding && !isTemplateBinding) {
  28110. // don't include the bindings as attributes as well in the AST
  28111. attributes.push(this.visitAttribute(attribute));
  28112. }
  28113. }
  28114. let children;
  28115. if (preparsedElement.nonBindable) {
  28116. // The `NonBindableVisitor` may need to return an array of nodes for blocks so we need
  28117. // to flatten the array here. Avoid doing this for the `HtmlAstToIvyAst` since `flat` creates
  28118. // a new array.
  28119. children = visitAll(NON_BINDABLE_VISITOR, element.children).flat(Infinity);
  28120. }
  28121. else {
  28122. children = visitAll(this, element.children, element.children);
  28123. }
  28124. let parsedElement;
  28125. if (preparsedElement.type === PreparsedElementType.NG_CONTENT) {
  28126. const selector = preparsedElement.selectAttr;
  28127. const attrs = element.attrs.map((attr) => this.visitAttribute(attr));
  28128. parsedElement = new Content(selector, attrs, children, element.sourceSpan, element.i18n);
  28129. this.ngContentSelectors.push(selector);
  28130. }
  28131. else if (isTemplateElement) {
  28132. // `<ng-template>`
  28133. const attrs = this.extractAttributes(element.name, parsedProperties, i18nAttrsMeta);
  28134. parsedElement = new Template(element.name, attributes, attrs.bound, boundEvents, [
  28135. /* no template attributes */
  28136. ], children, references, variables, element.sourceSpan, element.startSourceSpan, element.endSourceSpan, element.i18n);
  28137. }
  28138. else {
  28139. const attrs = this.extractAttributes(element.name, parsedProperties, i18nAttrsMeta);
  28140. parsedElement = new Element$1(element.name, attributes, attrs.bound, boundEvents, children, references, element.sourceSpan, element.startSourceSpan, element.endSourceSpan, element.i18n);
  28141. }
  28142. if (elementHasInlineTemplate) {
  28143. // If this node is an inline-template (e.g. has *ngFor) then we need to create a template
  28144. // node that contains this node.
  28145. // Moreover, if the node is an element, then we need to hoist its attributes to the template
  28146. // node for matching against content projection selectors.
  28147. const attrs = this.extractAttributes('ng-template', templateParsedProperties, i18nAttrsMeta);
  28148. const templateAttrs = [];
  28149. attrs.literal.forEach((attr) => templateAttrs.push(attr));
  28150. attrs.bound.forEach((attr) => templateAttrs.push(attr));
  28151. const hoistedAttrs = parsedElement instanceof Element$1
  28152. ? {
  28153. attributes: parsedElement.attributes,
  28154. inputs: parsedElement.inputs,
  28155. outputs: parsedElement.outputs,
  28156. }
  28157. : { attributes: [], inputs: [], outputs: [] };
  28158. // For <ng-template>s with structural directives on them, avoid passing i18n information to
  28159. // the wrapping template to prevent unnecessary i18n instructions from being generated. The
  28160. // necessary i18n meta information will be extracted from child elements.
  28161. const i18n = isTemplateElement && isI18nRootElement ? undefined : element.i18n;
  28162. const name = parsedElement instanceof Template ? null : parsedElement.name;
  28163. parsedElement = new Template(name, hoistedAttrs.attributes, hoistedAttrs.inputs, hoistedAttrs.outputs, templateAttrs, [parsedElement], [
  28164. /* no references */
  28165. ], templateVariables, element.sourceSpan, element.startSourceSpan, element.endSourceSpan, i18n);
  28166. }
  28167. if (isI18nRootElement) {
  28168. this.inI18nBlock = false;
  28169. }
  28170. return parsedElement;
  28171. }
  28172. visitAttribute(attribute) {
  28173. return new TextAttribute(attribute.name, attribute.value, attribute.sourceSpan, attribute.keySpan, attribute.valueSpan, attribute.i18n);
  28174. }
  28175. visitText(text) {
  28176. return this.processedNodes.has(text)
  28177. ? null
  28178. : this._visitTextWithInterpolation(text.value, text.sourceSpan, text.tokens, text.i18n);
  28179. }
  28180. visitExpansion(expansion) {
  28181. if (!expansion.i18n) {
  28182. // do not generate Icu in case it was created
  28183. // outside of i18n block in a template
  28184. return null;
  28185. }
  28186. if (!isI18nRootNode(expansion.i18n)) {
  28187. throw new Error(`Invalid type "${expansion.i18n.constructor}" for "i18n" property of ${expansion.sourceSpan.toString()}. Expected a "Message"`);
  28188. }
  28189. const message = expansion.i18n;
  28190. const vars = {};
  28191. const placeholders = {};
  28192. // extract VARs from ICUs - we process them separately while
  28193. // assembling resulting message via goog.getMsg function, since
  28194. // we need to pass them to top-level goog.getMsg call
  28195. Object.keys(message.placeholders).forEach((key) => {
  28196. const value = message.placeholders[key];
  28197. if (key.startsWith(I18N_ICU_VAR_PREFIX)) {
  28198. // Currently when the `plural` or `select` keywords in an ICU contain trailing spaces (e.g.
  28199. // `{count, select , ...}`), these spaces are also included into the key names in ICU vars
  28200. // (e.g. "VAR_SELECT "). These trailing spaces are not desirable, since they will later be
  28201. // converted into `_` symbols while normalizing placeholder names, which might lead to
  28202. // mismatches at runtime (i.e. placeholder will not be replaced with the correct value).
  28203. const formattedKey = key.trim();
  28204. const ast = this.bindingParser.parseInterpolationExpression(value.text, value.sourceSpan);
  28205. vars[formattedKey] = new BoundText(ast, value.sourceSpan);
  28206. }
  28207. else {
  28208. placeholders[key] = this._visitTextWithInterpolation(value.text, value.sourceSpan, null);
  28209. }
  28210. });
  28211. return new Icu$1(vars, placeholders, expansion.sourceSpan, message);
  28212. }
  28213. visitExpansionCase(expansionCase) {
  28214. return null;
  28215. }
  28216. visitComment(comment) {
  28217. if (this.options.collectCommentNodes) {
  28218. this.commentNodes.push(new Comment$1(comment.value || '', comment.sourceSpan));
  28219. }
  28220. return null;
  28221. }
  28222. visitLetDeclaration(decl, context) {
  28223. const value = this.bindingParser.parseBinding(decl.value, false, decl.valueSpan, decl.valueSpan.start.offset);
  28224. if (value.errors.length === 0 && value.ast instanceof EmptyExpr$1) {
  28225. this.reportError('@let declaration value cannot be empty', decl.valueSpan);
  28226. }
  28227. return new LetDeclaration$1(decl.name, value, decl.sourceSpan, decl.nameSpan, decl.valueSpan);
  28228. }
  28229. visitBlockParameter() {
  28230. return null;
  28231. }
  28232. visitBlock(block, context) {
  28233. const index = Array.isArray(context) ? context.indexOf(block) : -1;
  28234. if (index === -1) {
  28235. throw new Error('Visitor invoked incorrectly. Expecting visitBlock to be invoked siblings array as its context');
  28236. }
  28237. // Connected blocks may have been processed as a part of the previous block.
  28238. if (this.processedNodes.has(block)) {
  28239. return null;
  28240. }
  28241. let result = null;
  28242. switch (block.name) {
  28243. case 'defer':
  28244. result = createDeferredBlock(block, this.findConnectedBlocks(index, context, isConnectedDeferLoopBlock), this, this.bindingParser);
  28245. break;
  28246. case 'switch':
  28247. result = createSwitchBlock(block, this, this.bindingParser);
  28248. break;
  28249. case 'for':
  28250. result = createForLoop(block, this.findConnectedBlocks(index, context, isConnectedForLoopBlock), this, this.bindingParser);
  28251. break;
  28252. case 'if':
  28253. result = createIfBlock(block, this.findConnectedBlocks(index, context, isConnectedIfLoopBlock), this, this.bindingParser);
  28254. break;
  28255. default:
  28256. let errorMessage;
  28257. if (isConnectedDeferLoopBlock(block.name)) {
  28258. errorMessage = `@${block.name} block can only be used after an @defer block.`;
  28259. this.processedNodes.add(block);
  28260. }
  28261. else if (isConnectedForLoopBlock(block.name)) {
  28262. errorMessage = `@${block.name} block can only be used after an @for block.`;
  28263. this.processedNodes.add(block);
  28264. }
  28265. else if (isConnectedIfLoopBlock(block.name)) {
  28266. errorMessage = `@${block.name} block can only be used after an @if or @else if block.`;
  28267. this.processedNodes.add(block);
  28268. }
  28269. else {
  28270. errorMessage = `Unrecognized block @${block.name}.`;
  28271. }
  28272. result = {
  28273. node: new UnknownBlock(block.name, block.sourceSpan, block.nameSpan),
  28274. errors: [new ParseError(block.sourceSpan, errorMessage)],
  28275. };
  28276. break;
  28277. }
  28278. this.errors.push(...result.errors);
  28279. return result.node;
  28280. }
  28281. findConnectedBlocks(primaryBlockIndex, siblings, predicate) {
  28282. const relatedBlocks = [];
  28283. for (let i = primaryBlockIndex + 1; i < siblings.length; i++) {
  28284. const node = siblings[i];
  28285. // Skip over comments.
  28286. if (node instanceof Comment) {
  28287. continue;
  28288. }
  28289. // Ignore empty text nodes between blocks.
  28290. if (node instanceof Text && node.value.trim().length === 0) {
  28291. // Add the text node to the processed nodes since we don't want
  28292. // it to be generated between the connected nodes.
  28293. this.processedNodes.add(node);
  28294. continue;
  28295. }
  28296. // Stop searching as soon as we hit a non-block node or a block that is unrelated.
  28297. if (!(node instanceof Block) || !predicate(node.name)) {
  28298. break;
  28299. }
  28300. relatedBlocks.push(node);
  28301. this.processedNodes.add(node);
  28302. }
  28303. return relatedBlocks;
  28304. }
  28305. // convert view engine `ParsedProperty` to a format suitable for IVY
  28306. extractAttributes(elementName, properties, i18nPropsMeta) {
  28307. const bound = [];
  28308. const literal = [];
  28309. properties.forEach((prop) => {
  28310. const i18n = i18nPropsMeta[prop.name];
  28311. if (prop.isLiteral) {
  28312. literal.push(new TextAttribute(prop.name, prop.expression.source || '', prop.sourceSpan, prop.keySpan, prop.valueSpan, i18n));
  28313. }
  28314. else {
  28315. // Note that validation is skipped and property mapping is disabled
  28316. // due to the fact that we need to make sure a given prop is not an
  28317. // input of a directive and directive matching happens at runtime.
  28318. const bep = this.bindingParser.createBoundElementProperty(elementName, prop,
  28319. /* skipValidation */ true,
  28320. /* mapPropertyName */ false);
  28321. bound.push(BoundAttribute.fromBoundElementProperty(bep, i18n));
  28322. }
  28323. });
  28324. return { bound, literal };
  28325. }
  28326. parseAttribute(isTemplateElement, attribute, matchableAttributes, parsedProperties, boundEvents, variables, references) {
  28327. const name = normalizeAttributeName(attribute.name);
  28328. const value = attribute.value;
  28329. const srcSpan = attribute.sourceSpan;
  28330. const absoluteOffset = attribute.valueSpan
  28331. ? attribute.valueSpan.start.offset
  28332. : srcSpan.start.offset;
  28333. function createKeySpan(srcSpan, prefix, identifier) {
  28334. // We need to adjust the start location for the keySpan to account for the removed 'data-'
  28335. // prefix from `normalizeAttributeName`.
  28336. const normalizationAdjustment = attribute.name.length - name.length;
  28337. const keySpanStart = srcSpan.start.moveBy(prefix.length + normalizationAdjustment);
  28338. const keySpanEnd = keySpanStart.moveBy(identifier.length);
  28339. return new ParseSourceSpan(keySpanStart, keySpanEnd, keySpanStart, identifier);
  28340. }
  28341. const bindParts = name.match(BIND_NAME_REGEXP);
  28342. if (bindParts) {
  28343. if (bindParts[KW_BIND_IDX] != null) {
  28344. const identifier = bindParts[IDENT_KW_IDX];
  28345. const keySpan = createKeySpan(srcSpan, bindParts[KW_BIND_IDX], identifier);
  28346. this.bindingParser.parsePropertyBinding(identifier, value, false, false, srcSpan, absoluteOffset, attribute.valueSpan, matchableAttributes, parsedProperties, keySpan);
  28347. }
  28348. else if (bindParts[KW_LET_IDX]) {
  28349. if (isTemplateElement) {
  28350. const identifier = bindParts[IDENT_KW_IDX];
  28351. const keySpan = createKeySpan(srcSpan, bindParts[KW_LET_IDX], identifier);
  28352. this.parseVariable(identifier, value, srcSpan, keySpan, attribute.valueSpan, variables);
  28353. }
  28354. else {
  28355. this.reportError(`"let-" is only supported on ng-template elements.`, srcSpan);
  28356. }
  28357. }
  28358. else if (bindParts[KW_REF_IDX]) {
  28359. const identifier = bindParts[IDENT_KW_IDX];
  28360. const keySpan = createKeySpan(srcSpan, bindParts[KW_REF_IDX], identifier);
  28361. this.parseReference(identifier, value, srcSpan, keySpan, attribute.valueSpan, references);
  28362. }
  28363. else if (bindParts[KW_ON_IDX]) {
  28364. const events = [];
  28365. const identifier = bindParts[IDENT_KW_IDX];
  28366. const keySpan = createKeySpan(srcSpan, bindParts[KW_ON_IDX], identifier);
  28367. this.bindingParser.parseEvent(identifier, value,
  28368. /* isAssignmentEvent */ false, srcSpan, attribute.valueSpan || srcSpan, matchableAttributes, events, keySpan);
  28369. addEvents(events, boundEvents);
  28370. }
  28371. else if (bindParts[KW_BINDON_IDX]) {
  28372. const identifier = bindParts[IDENT_KW_IDX];
  28373. const keySpan = createKeySpan(srcSpan, bindParts[KW_BINDON_IDX], identifier);
  28374. this.bindingParser.parsePropertyBinding(identifier, value, false, true, srcSpan, absoluteOffset, attribute.valueSpan, matchableAttributes, parsedProperties, keySpan);
  28375. this.parseAssignmentEvent(identifier, value, srcSpan, attribute.valueSpan, matchableAttributes, boundEvents, keySpan);
  28376. }
  28377. else if (bindParts[KW_AT_IDX]) {
  28378. const keySpan = createKeySpan(srcSpan, '', name);
  28379. this.bindingParser.parseLiteralAttr(name, value, srcSpan, absoluteOffset, attribute.valueSpan, matchableAttributes, parsedProperties, keySpan);
  28380. }
  28381. return true;
  28382. }
  28383. // We didn't see a kw-prefixed property binding, but we have not yet checked
  28384. // for the []/()/[()] syntax.
  28385. let delims = null;
  28386. if (name.startsWith(BINDING_DELIMS.BANANA_BOX.start)) {
  28387. delims = BINDING_DELIMS.BANANA_BOX;
  28388. }
  28389. else if (name.startsWith(BINDING_DELIMS.PROPERTY.start)) {
  28390. delims = BINDING_DELIMS.PROPERTY;
  28391. }
  28392. else if (name.startsWith(BINDING_DELIMS.EVENT.start)) {
  28393. delims = BINDING_DELIMS.EVENT;
  28394. }
  28395. if (delims !== null &&
  28396. // NOTE: older versions of the parser would match a start/end delimited
  28397. // binding iff the property name was terminated by the ending delimiter
  28398. // and the identifier in the binding was non-empty.
  28399. // TODO(ayazhafiz): update this to handle malformed bindings.
  28400. name.endsWith(delims.end) &&
  28401. name.length > delims.start.length + delims.end.length) {
  28402. const identifier = name.substring(delims.start.length, name.length - delims.end.length);
  28403. const keySpan = createKeySpan(srcSpan, delims.start, identifier);
  28404. if (delims.start === BINDING_DELIMS.BANANA_BOX.start) {
  28405. this.bindingParser.parsePropertyBinding(identifier, value, false, true, srcSpan, absoluteOffset, attribute.valueSpan, matchableAttributes, parsedProperties, keySpan);
  28406. this.parseAssignmentEvent(identifier, value, srcSpan, attribute.valueSpan, matchableAttributes, boundEvents, keySpan);
  28407. }
  28408. else if (delims.start === BINDING_DELIMS.PROPERTY.start) {
  28409. this.bindingParser.parsePropertyBinding(identifier, value, false, false, srcSpan, absoluteOffset, attribute.valueSpan, matchableAttributes, parsedProperties, keySpan);
  28410. }
  28411. else {
  28412. const events = [];
  28413. this.bindingParser.parseEvent(identifier, value,
  28414. /* isAssignmentEvent */ false, srcSpan, attribute.valueSpan || srcSpan, matchableAttributes, events, keySpan);
  28415. addEvents(events, boundEvents);
  28416. }
  28417. return true;
  28418. }
  28419. // No explicit binding found.
  28420. const keySpan = createKeySpan(srcSpan, '' /* prefix */, name);
  28421. const hasBinding = this.bindingParser.parsePropertyInterpolation(name, value, srcSpan, attribute.valueSpan, matchableAttributes, parsedProperties, keySpan, attribute.valueTokens ?? null);
  28422. return hasBinding;
  28423. }
  28424. _visitTextWithInterpolation(value, sourceSpan, interpolatedTokens, i18n) {
  28425. const valueNoNgsp = replaceNgsp(value);
  28426. const expr = this.bindingParser.parseInterpolation(valueNoNgsp, sourceSpan, interpolatedTokens);
  28427. return expr ? new BoundText(expr, sourceSpan, i18n) : new Text$3(valueNoNgsp, sourceSpan);
  28428. }
  28429. parseVariable(identifier, value, sourceSpan, keySpan, valueSpan, variables) {
  28430. if (identifier.indexOf('-') > -1) {
  28431. this.reportError(`"-" is not allowed in variable names`, sourceSpan);
  28432. }
  28433. else if (identifier.length === 0) {
  28434. this.reportError(`Variable does not have a name`, sourceSpan);
  28435. }
  28436. variables.push(new Variable(identifier, value, sourceSpan, keySpan, valueSpan));
  28437. }
  28438. parseReference(identifier, value, sourceSpan, keySpan, valueSpan, references) {
  28439. if (identifier.indexOf('-') > -1) {
  28440. this.reportError(`"-" is not allowed in reference names`, sourceSpan);
  28441. }
  28442. else if (identifier.length === 0) {
  28443. this.reportError(`Reference does not have a name`, sourceSpan);
  28444. }
  28445. else if (references.some((reference) => reference.name === identifier)) {
  28446. this.reportError(`Reference "#${identifier}" is defined more than once`, sourceSpan);
  28447. }
  28448. references.push(new Reference(identifier, value, sourceSpan, keySpan, valueSpan));
  28449. }
  28450. parseAssignmentEvent(name, expression, sourceSpan, valueSpan, targetMatchableAttrs, boundEvents, keySpan) {
  28451. const events = [];
  28452. this.bindingParser.parseEvent(`${name}Change`, expression,
  28453. /* isAssignmentEvent */ true, sourceSpan, valueSpan || sourceSpan, targetMatchableAttrs, events, keySpan);
  28454. addEvents(events, boundEvents);
  28455. }
  28456. reportError(message, sourceSpan, level = ParseErrorLevel.ERROR) {
  28457. this.errors.push(new ParseError(sourceSpan, message, level));
  28458. }
  28459. }
  28460. class NonBindableVisitor {
  28461. visitElement(ast) {
  28462. const preparsedElement = preparseElement(ast);
  28463. if (preparsedElement.type === PreparsedElementType.SCRIPT ||
  28464. preparsedElement.type === PreparsedElementType.STYLE ||
  28465. preparsedElement.type === PreparsedElementType.STYLESHEET) {
  28466. // Skipping <script> for security reasons
  28467. // Skipping <style> and stylesheets as we already processed them
  28468. // in the StyleCompiler
  28469. return null;
  28470. }
  28471. const children = visitAll(this, ast.children, null);
  28472. return new Element$1(ast.name, visitAll(this, ast.attrs),
  28473. /* inputs */ [],
  28474. /* outputs */ [], children,
  28475. /* references */ [], ast.sourceSpan, ast.startSourceSpan, ast.endSourceSpan);
  28476. }
  28477. visitComment(comment) {
  28478. return null;
  28479. }
  28480. visitAttribute(attribute) {
  28481. return new TextAttribute(attribute.name, attribute.value, attribute.sourceSpan, attribute.keySpan, attribute.valueSpan, attribute.i18n);
  28482. }
  28483. visitText(text) {
  28484. return new Text$3(text.value, text.sourceSpan);
  28485. }
  28486. visitExpansion(expansion) {
  28487. return null;
  28488. }
  28489. visitExpansionCase(expansionCase) {
  28490. return null;
  28491. }
  28492. visitBlock(block, context) {
  28493. const nodes = [
  28494. // In an ngNonBindable context we treat the opening/closing tags of block as plain text.
  28495. // This is the as if the `tokenizeBlocks` option was disabled.
  28496. new Text$3(block.startSourceSpan.toString(), block.startSourceSpan),
  28497. ...visitAll(this, block.children),
  28498. ];
  28499. if (block.endSourceSpan !== null) {
  28500. nodes.push(new Text$3(block.endSourceSpan.toString(), block.endSourceSpan));
  28501. }
  28502. return nodes;
  28503. }
  28504. visitBlockParameter(parameter, context) {
  28505. return null;
  28506. }
  28507. visitLetDeclaration(decl, context) {
  28508. return new Text$3(`@let ${decl.name} = ${decl.value};`, decl.sourceSpan);
  28509. }
  28510. }
  28511. const NON_BINDABLE_VISITOR = new NonBindableVisitor();
  28512. function normalizeAttributeName(attrName) {
  28513. return /^data-/i.test(attrName) ? attrName.substring(5) : attrName;
  28514. }
  28515. function addEvents(events, boundEvents) {
  28516. boundEvents.push(...events.map((e) => BoundEvent.fromParsedEvent(e)));
  28517. }
  28518. function textContents(node) {
  28519. if (node.children.length !== 1 || !(node.children[0] instanceof Text)) {
  28520. return null;
  28521. }
  28522. else {
  28523. return node.children[0].value;
  28524. }
  28525. }
  28526. const LEADING_TRIVIA_CHARS = [' ', '\n', '\r', '\t'];
  28527. /**
  28528. * Parse a template into render3 `Node`s and additional metadata, with no other dependencies.
  28529. *
  28530. * @param template text of the template to parse
  28531. * @param templateUrl URL to use for source mapping of the parsed template
  28532. * @param options options to modify how the template is parsed
  28533. */
  28534. function parseTemplate(template, templateUrl, options = {}) {
  28535. const { interpolationConfig, preserveWhitespaces, enableI18nLegacyMessageIdFormat } = options;
  28536. const bindingParser = makeBindingParser(interpolationConfig);
  28537. const htmlParser = new HtmlParser();
  28538. const parseResult = htmlParser.parse(template, templateUrl, {
  28539. leadingTriviaChars: LEADING_TRIVIA_CHARS,
  28540. ...options,
  28541. tokenizeExpansionForms: true,
  28542. tokenizeBlocks: options.enableBlockSyntax ?? true,
  28543. tokenizeLet: options.enableLetSyntax ?? true,
  28544. });
  28545. if (!options.alwaysAttemptHtmlToR3AstConversion &&
  28546. parseResult.errors &&
  28547. parseResult.errors.length > 0) {
  28548. const parsedTemplate = {
  28549. interpolationConfig,
  28550. preserveWhitespaces,
  28551. errors: parseResult.errors,
  28552. nodes: [],
  28553. styleUrls: [],
  28554. styles: [],
  28555. ngContentSelectors: [],
  28556. };
  28557. if (options.collectCommentNodes) {
  28558. parsedTemplate.commentNodes = [];
  28559. }
  28560. return parsedTemplate;
  28561. }
  28562. let rootNodes = parseResult.rootNodes;
  28563. // We need to use the same `retainEmptyTokens` value for both parses to avoid
  28564. // causing a mismatch when reusing source spans, even if the
  28565. // `preserveSignificantWhitespace` behavior is different between the two
  28566. // parses.
  28567. const retainEmptyTokens = !(options.preserveSignificantWhitespace ?? true);
  28568. // process i18n meta information (scan attributes, generate ids)
  28569. // before we run whitespace removal process, because existing i18n
  28570. // extraction process (ng extract-i18n) relies on a raw content to generate
  28571. // message ids
  28572. const i18nMetaVisitor = new I18nMetaVisitor(interpolationConfig,
  28573. /* keepI18nAttrs */ !preserveWhitespaces, enableI18nLegacyMessageIdFormat,
  28574. /* containerBlocks */ undefined, options.preserveSignificantWhitespace, retainEmptyTokens);
  28575. const i18nMetaResult = i18nMetaVisitor.visitAllWithErrors(rootNodes);
  28576. if (!options.alwaysAttemptHtmlToR3AstConversion &&
  28577. i18nMetaResult.errors &&
  28578. i18nMetaResult.errors.length > 0) {
  28579. const parsedTemplate = {
  28580. interpolationConfig,
  28581. preserveWhitespaces,
  28582. errors: i18nMetaResult.errors,
  28583. nodes: [],
  28584. styleUrls: [],
  28585. styles: [],
  28586. ngContentSelectors: [],
  28587. };
  28588. if (options.collectCommentNodes) {
  28589. parsedTemplate.commentNodes = [];
  28590. }
  28591. return parsedTemplate;
  28592. }
  28593. rootNodes = i18nMetaResult.rootNodes;
  28594. if (!preserveWhitespaces) {
  28595. // Always preserve significant whitespace here because this is used to generate the `goog.getMsg`
  28596. // and `$localize` calls which should retain significant whitespace in order to render the
  28597. // correct output. We let this diverge from the message IDs generated earlier which might not
  28598. // have preserved significant whitespace.
  28599. //
  28600. // This should use `visitAllWithSiblings` to set `WhitespaceVisitor` context correctly, however
  28601. // there is an existing bug where significant whitespace is not properly retained in the JS
  28602. // output of leading/trailing whitespace for ICU messages due to the existing lack of context\
  28603. // in `WhitespaceVisitor`. Using `visitAllWithSiblings` here would fix that bug and retain the
  28604. // whitespace, however it would also change the runtime representation which we don't want to do
  28605. // right now.
  28606. rootNodes = visitAll(new WhitespaceVisitor(
  28607. /* preserveSignificantWhitespace */ true,
  28608. /* originalNodeMap */ undefined,
  28609. /* requireContext */ false), rootNodes);
  28610. // run i18n meta visitor again in case whitespaces are removed (because that might affect
  28611. // generated i18n message content) and first pass indicated that i18n content is present in a
  28612. // template. During this pass i18n IDs generated at the first pass will be preserved, so we can
  28613. // mimic existing extraction process (ng extract-i18n)
  28614. if (i18nMetaVisitor.hasI18nMeta) {
  28615. rootNodes = visitAll(new I18nMetaVisitor(interpolationConfig,
  28616. /* keepI18nAttrs */ false,
  28617. /* enableI18nLegacyMessageIdFormat */ undefined,
  28618. /* containerBlocks */ undefined,
  28619. /* preserveSignificantWhitespace */ true, retainEmptyTokens), rootNodes);
  28620. }
  28621. }
  28622. const { nodes, errors, styleUrls, styles, ngContentSelectors, commentNodes } = htmlAstToRender3Ast(rootNodes, bindingParser, { collectCommentNodes: !!options.collectCommentNodes });
  28623. errors.push(...parseResult.errors, ...i18nMetaResult.errors);
  28624. const parsedTemplate = {
  28625. interpolationConfig,
  28626. preserveWhitespaces,
  28627. errors: errors.length > 0 ? errors : null,
  28628. nodes,
  28629. styleUrls,
  28630. styles,
  28631. ngContentSelectors,
  28632. };
  28633. if (options.collectCommentNodes) {
  28634. parsedTemplate.commentNodes = commentNodes;
  28635. }
  28636. return parsedTemplate;
  28637. }
  28638. const elementRegistry = new DomElementSchemaRegistry();
  28639. /**
  28640. * Construct a `BindingParser` with a default configuration.
  28641. */
  28642. function makeBindingParser(interpolationConfig = DEFAULT_INTERPOLATION_CONFIG) {
  28643. return new BindingParser(new Parser(new Lexer()), interpolationConfig, elementRegistry, []);
  28644. }
  28645. const COMPONENT_VARIABLE = '%COMP%';
  28646. const HOST_ATTR = `_nghost-${COMPONENT_VARIABLE}`;
  28647. const CONTENT_ATTR = `_ngcontent-${COMPONENT_VARIABLE}`;
  28648. function baseDirectiveFields(meta, constantPool, bindingParser) {
  28649. const definitionMap = new DefinitionMap();
  28650. const selectors = parseSelectorToR3Selector(meta.selector);
  28651. // e.g. `type: MyDirective`
  28652. definitionMap.set('type', meta.type.value);
  28653. // e.g. `selectors: [['', 'someDir', '']]`
  28654. if (selectors.length > 0) {
  28655. definitionMap.set('selectors', asLiteral(selectors));
  28656. }
  28657. if (meta.queries.length > 0) {
  28658. // e.g. `contentQueries: (rf, ctx, dirIndex) => { ... }
  28659. definitionMap.set('contentQueries', createContentQueriesFunction(meta.queries, constantPool, meta.name));
  28660. }
  28661. if (meta.viewQueries.length) {
  28662. definitionMap.set('viewQuery', createViewQueriesFunction(meta.viewQueries, constantPool, meta.name));
  28663. }
  28664. // e.g. `hostBindings: (rf, ctx) => { ... }
  28665. definitionMap.set('hostBindings', createHostBindingsFunction(meta.host, meta.typeSourceSpan, bindingParser, constantPool, meta.selector || '', meta.name, definitionMap));
  28666. // e.g 'inputs: {a: 'a'}`
  28667. definitionMap.set('inputs', conditionallyCreateDirectiveBindingLiteral(meta.inputs, true));
  28668. // e.g 'outputs: {a: 'a'}`
  28669. definitionMap.set('outputs', conditionallyCreateDirectiveBindingLiteral(meta.outputs));
  28670. if (meta.exportAs !== null) {
  28671. definitionMap.set('exportAs', literalArr(meta.exportAs.map((e) => literal(e))));
  28672. }
  28673. if (meta.isStandalone === false) {
  28674. definitionMap.set('standalone', literal(false));
  28675. }
  28676. if (meta.isSignal) {
  28677. definitionMap.set('signals', literal(true));
  28678. }
  28679. return definitionMap;
  28680. }
  28681. /**
  28682. * Add features to the definition map.
  28683. */
  28684. function addFeatures(definitionMap, meta) {
  28685. // e.g. `features: [NgOnChangesFeature]`
  28686. const features = [];
  28687. const providers = meta.providers;
  28688. const viewProviders = meta.viewProviders;
  28689. if (providers || viewProviders) {
  28690. const args = [providers || new LiteralArrayExpr([])];
  28691. if (viewProviders) {
  28692. args.push(viewProviders);
  28693. }
  28694. features.push(importExpr(Identifiers.ProvidersFeature).callFn(args));
  28695. }
  28696. // Note: host directives feature needs to be inserted before the
  28697. // inheritance feature to ensure the correct execution order.
  28698. if (meta.hostDirectives?.length) {
  28699. features.push(importExpr(Identifiers.HostDirectivesFeature)
  28700. .callFn([createHostDirectivesFeatureArg(meta.hostDirectives)]));
  28701. }
  28702. if (meta.usesInheritance) {
  28703. features.push(importExpr(Identifiers.InheritDefinitionFeature));
  28704. }
  28705. if (meta.fullInheritance) {
  28706. features.push(importExpr(Identifiers.CopyDefinitionFeature));
  28707. }
  28708. if (meta.lifecycle.usesOnChanges) {
  28709. features.push(importExpr(Identifiers.NgOnChangesFeature));
  28710. }
  28711. if ('externalStyles' in meta && meta.externalStyles?.length) {
  28712. const externalStyleNodes = meta.externalStyles.map((externalStyle) => literal(externalStyle));
  28713. features.push(importExpr(Identifiers.ExternalStylesFeature).callFn([literalArr(externalStyleNodes)]));
  28714. }
  28715. if (features.length) {
  28716. definitionMap.set('features', literalArr(features));
  28717. }
  28718. }
  28719. /**
  28720. * Compile a directive for the render3 runtime as defined by the `R3DirectiveMetadata`.
  28721. */
  28722. function compileDirectiveFromMetadata(meta, constantPool, bindingParser) {
  28723. const definitionMap = baseDirectiveFields(meta, constantPool, bindingParser);
  28724. addFeatures(definitionMap, meta);
  28725. const expression = importExpr(Identifiers.defineDirective)
  28726. .callFn([definitionMap.toLiteralMap()], undefined, true);
  28727. const type = createDirectiveType(meta);
  28728. return { expression, type, statements: [] };
  28729. }
  28730. /**
  28731. * Compile a component for the render3 runtime as defined by the `R3ComponentMetadata`.
  28732. */
  28733. function compileComponentFromMetadata(meta, constantPool, bindingParser) {
  28734. const definitionMap = baseDirectiveFields(meta, constantPool, bindingParser);
  28735. addFeatures(definitionMap, meta);
  28736. const selector = meta.selector && CssSelector.parse(meta.selector);
  28737. const firstSelector = selector && selector[0];
  28738. // e.g. `attr: ["class", ".my.app"]`
  28739. // This is optional an only included if the first selector of a component specifies attributes.
  28740. if (firstSelector) {
  28741. const selectorAttributes = firstSelector.getAttrs();
  28742. if (selectorAttributes.length) {
  28743. definitionMap.set('attrs', constantPool.getConstLiteral(literalArr(selectorAttributes.map((value) => value != null ? literal(value) : literal(undefined))),
  28744. /* forceShared */ true));
  28745. }
  28746. }
  28747. // e.g. `template: function MyComponent_Template(_ctx, _cm) {...}`
  28748. const templateTypeName = meta.name;
  28749. let allDeferrableDepsFn = null;
  28750. if (meta.defer.mode === 1 /* DeferBlockDepsEmitMode.PerComponent */ &&
  28751. meta.defer.dependenciesFn !== null) {
  28752. const fnName = `${templateTypeName}_DeferFn`;
  28753. constantPool.statements.push(new DeclareVarStmt(fnName, meta.defer.dependenciesFn, undefined, StmtModifier.Final));
  28754. allDeferrableDepsFn = variable(fnName);
  28755. }
  28756. // First the template is ingested into IR:
  28757. const tpl = ingestComponent(meta.name, meta.template.nodes, constantPool, meta.relativeContextFilePath, meta.i18nUseExternalIds, meta.defer, allDeferrableDepsFn, meta.relativeTemplatePath, getTemplateSourceLocationsEnabled());
  28758. // Then the IR is transformed to prepare it for cod egeneration.
  28759. transform(tpl, CompilationJobKind.Tmpl);
  28760. // Finally we emit the template function:
  28761. const templateFn = emitTemplateFn(tpl, constantPool);
  28762. if (tpl.contentSelectors !== null) {
  28763. definitionMap.set('ngContentSelectors', tpl.contentSelectors);
  28764. }
  28765. definitionMap.set('decls', literal(tpl.root.decls));
  28766. definitionMap.set('vars', literal(tpl.root.vars));
  28767. if (tpl.consts.length > 0) {
  28768. if (tpl.constsInitializers.length > 0) {
  28769. definitionMap.set('consts', arrowFn([], [...tpl.constsInitializers, new ReturnStatement(literalArr(tpl.consts))]));
  28770. }
  28771. else {
  28772. definitionMap.set('consts', literalArr(tpl.consts));
  28773. }
  28774. }
  28775. definitionMap.set('template', templateFn);
  28776. if (meta.declarationListEmitMode !== 3 /* DeclarationListEmitMode.RuntimeResolved */ &&
  28777. meta.declarations.length > 0) {
  28778. definitionMap.set('dependencies', compileDeclarationList(literalArr(meta.declarations.map((decl) => decl.type)), meta.declarationListEmitMode));
  28779. }
  28780. else if (meta.declarationListEmitMode === 3 /* DeclarationListEmitMode.RuntimeResolved */) {
  28781. const args = [meta.type.value];
  28782. if (meta.rawImports) {
  28783. args.push(meta.rawImports);
  28784. }
  28785. definitionMap.set('dependencies', importExpr(Identifiers.getComponentDepsFactory).callFn(args));
  28786. }
  28787. if (meta.encapsulation === null) {
  28788. meta.encapsulation = ViewEncapsulation.Emulated;
  28789. }
  28790. let hasStyles = !!meta.externalStyles?.length;
  28791. // e.g. `styles: [str1, str2]`
  28792. if (meta.styles && meta.styles.length) {
  28793. const styleValues = meta.encapsulation == ViewEncapsulation.Emulated
  28794. ? compileStyles(meta.styles, CONTENT_ATTR, HOST_ATTR)
  28795. : meta.styles;
  28796. const styleNodes = styleValues.reduce((result, style) => {
  28797. if (style.trim().length > 0) {
  28798. result.push(constantPool.getConstLiteral(literal(style)));
  28799. }
  28800. return result;
  28801. }, []);
  28802. if (styleNodes.length > 0) {
  28803. hasStyles = true;
  28804. definitionMap.set('styles', literalArr(styleNodes));
  28805. }
  28806. }
  28807. if (!hasStyles && meta.encapsulation === ViewEncapsulation.Emulated) {
  28808. // If there is no style, don't generate css selectors on elements
  28809. meta.encapsulation = ViewEncapsulation.None;
  28810. }
  28811. // Only set view encapsulation if it's not the default value
  28812. if (meta.encapsulation !== ViewEncapsulation.Emulated) {
  28813. definitionMap.set('encapsulation', literal(meta.encapsulation));
  28814. }
  28815. // e.g. `animation: [trigger('123', [])]`
  28816. if (meta.animations !== null) {
  28817. definitionMap.set('data', literalMap([{ key: 'animation', value: meta.animations, quoted: false }]));
  28818. }
  28819. // Setting change detection flag
  28820. if (meta.changeDetection !== null) {
  28821. if (typeof meta.changeDetection === 'number' &&
  28822. meta.changeDetection !== ChangeDetectionStrategy.Default) {
  28823. // changeDetection is resolved during analysis. Only set it if not the default.
  28824. definitionMap.set('changeDetection', literal(meta.changeDetection));
  28825. }
  28826. else if (typeof meta.changeDetection === 'object') {
  28827. // changeDetection is not resolved during analysis (e.g., we are in local compilation mode).
  28828. // So place it as is.
  28829. definitionMap.set('changeDetection', meta.changeDetection);
  28830. }
  28831. }
  28832. const expression = importExpr(Identifiers.defineComponent)
  28833. .callFn([definitionMap.toLiteralMap()], undefined, true);
  28834. const type = createComponentType(meta);
  28835. return { expression, type, statements: [] };
  28836. }
  28837. /**
  28838. * Creates the type specification from the component meta. This type is inserted into .d.ts files
  28839. * to be consumed by upstream compilations.
  28840. */
  28841. function createComponentType(meta) {
  28842. const typeParams = createBaseDirectiveTypeParams(meta);
  28843. typeParams.push(stringArrayAsType(meta.template.ngContentSelectors));
  28844. typeParams.push(expressionType(literal(meta.isStandalone)));
  28845. typeParams.push(createHostDirectivesType(meta));
  28846. // TODO(signals): Always include this metadata starting with v17. Right
  28847. // now Angular v16.0.x does not support this field and library distributions
  28848. // would then be incompatible with v16.0.x framework users.
  28849. if (meta.isSignal) {
  28850. typeParams.push(expressionType(literal(meta.isSignal)));
  28851. }
  28852. return expressionType(importExpr(Identifiers.ComponentDeclaration, typeParams));
  28853. }
  28854. /**
  28855. * Compiles the array literal of declarations into an expression according to the provided emit
  28856. * mode.
  28857. */
  28858. function compileDeclarationList(list, mode) {
  28859. switch (mode) {
  28860. case 0 /* DeclarationListEmitMode.Direct */:
  28861. // directives: [MyDir],
  28862. return list;
  28863. case 1 /* DeclarationListEmitMode.Closure */:
  28864. // directives: function () { return [MyDir]; }
  28865. return arrowFn([], list);
  28866. case 2 /* DeclarationListEmitMode.ClosureResolved */:
  28867. // directives: function () { return [MyDir].map(ng.resolveForwardRef); }
  28868. const resolvedList = list.prop('map').callFn([importExpr(Identifiers.resolveForwardRef)]);
  28869. return arrowFn([], resolvedList);
  28870. case 3 /* DeclarationListEmitMode.RuntimeResolved */:
  28871. throw new Error(`Unsupported with an array of pre-resolved dependencies`);
  28872. }
  28873. }
  28874. function stringAsType(str) {
  28875. return expressionType(literal(str));
  28876. }
  28877. function stringMapAsLiteralExpression(map) {
  28878. const mapValues = Object.keys(map).map((key) => {
  28879. const value = Array.isArray(map[key]) ? map[key][0] : map[key];
  28880. return {
  28881. key,
  28882. value: literal(value),
  28883. quoted: true,
  28884. };
  28885. });
  28886. return literalMap(mapValues);
  28887. }
  28888. function stringArrayAsType(arr) {
  28889. return arr.length > 0
  28890. ? expressionType(literalArr(arr.map((value) => literal(value))))
  28891. : NONE_TYPE;
  28892. }
  28893. function createBaseDirectiveTypeParams(meta) {
  28894. // On the type side, remove newlines from the selector as it will need to fit into a TypeScript
  28895. // string literal, which must be on one line.
  28896. const selectorForType = meta.selector !== null ? meta.selector.replace(/\n/g, '') : null;
  28897. return [
  28898. typeWithParameters(meta.type.type, meta.typeArgumentCount),
  28899. selectorForType !== null ? stringAsType(selectorForType) : NONE_TYPE,
  28900. meta.exportAs !== null ? stringArrayAsType(meta.exportAs) : NONE_TYPE,
  28901. expressionType(getInputsTypeExpression(meta)),
  28902. expressionType(stringMapAsLiteralExpression(meta.outputs)),
  28903. stringArrayAsType(meta.queries.map((q) => q.propertyName)),
  28904. ];
  28905. }
  28906. function getInputsTypeExpression(meta) {
  28907. return literalMap(Object.keys(meta.inputs).map((key) => {
  28908. const value = meta.inputs[key];
  28909. const values = [
  28910. { key: 'alias', value: literal(value.bindingPropertyName), quoted: true },
  28911. { key: 'required', value: literal(value.required), quoted: true },
  28912. ];
  28913. // TODO(legacy-partial-output-inputs): Consider always emitting this information,
  28914. // or leaving it as is.
  28915. if (value.isSignal) {
  28916. values.push({ key: 'isSignal', value: literal(value.isSignal), quoted: true });
  28917. }
  28918. return { key, value: literalMap(values), quoted: true };
  28919. }));
  28920. }
  28921. /**
  28922. * Creates the type specification from the directive meta. This type is inserted into .d.ts files
  28923. * to be consumed by upstream compilations.
  28924. */
  28925. function createDirectiveType(meta) {
  28926. const typeParams = createBaseDirectiveTypeParams(meta);
  28927. // Directives have no NgContentSelectors slot, but instead express a `never` type
  28928. // so that future fields align.
  28929. typeParams.push(NONE_TYPE);
  28930. typeParams.push(expressionType(literal(meta.isStandalone)));
  28931. typeParams.push(createHostDirectivesType(meta));
  28932. // TODO(signals): Always include this metadata starting with v17. Right
  28933. // now Angular v16.0.x does not support this field and library distributions
  28934. // would then be incompatible with v16.0.x framework users.
  28935. if (meta.isSignal) {
  28936. typeParams.push(expressionType(literal(meta.isSignal)));
  28937. }
  28938. return expressionType(importExpr(Identifiers.DirectiveDeclaration, typeParams));
  28939. }
  28940. // Return a host binding function or null if one is not necessary.
  28941. function createHostBindingsFunction(hostBindingsMetadata, typeSourceSpan, bindingParser, constantPool, selector, name, definitionMap) {
  28942. const bindings = bindingParser.createBoundHostProperties(hostBindingsMetadata.properties, typeSourceSpan);
  28943. // Calculate host event bindings
  28944. const eventBindings = bindingParser.createDirectiveHostEventAsts(hostBindingsMetadata.listeners, typeSourceSpan);
  28945. // The parser for host bindings treats class and style attributes specially -- they are
  28946. // extracted into these separate fields. This is not the case for templates, so the compiler can
  28947. // actually already handle these special attributes internally. Therefore, we just drop them
  28948. // into the attributes map.
  28949. if (hostBindingsMetadata.specialAttributes.styleAttr) {
  28950. hostBindingsMetadata.attributes['style'] = literal(hostBindingsMetadata.specialAttributes.styleAttr);
  28951. }
  28952. if (hostBindingsMetadata.specialAttributes.classAttr) {
  28953. hostBindingsMetadata.attributes['class'] = literal(hostBindingsMetadata.specialAttributes.classAttr);
  28954. }
  28955. const hostJob = ingestHostBinding({
  28956. componentName: name,
  28957. componentSelector: selector,
  28958. properties: bindings,
  28959. events: eventBindings,
  28960. attributes: hostBindingsMetadata.attributes,
  28961. }, bindingParser, constantPool);
  28962. transform(hostJob, CompilationJobKind.Host);
  28963. definitionMap.set('hostAttrs', hostJob.root.attributes);
  28964. const varCount = hostJob.root.vars;
  28965. if (varCount !== null && varCount > 0) {
  28966. definitionMap.set('hostVars', literal(varCount));
  28967. }
  28968. return emitHostBindingFunction(hostJob);
  28969. }
  28970. const HOST_REG_EXP = /^(?:\[([^\]]+)\])|(?:\(([^\)]+)\))$/;
  28971. function parseHostBindings(host) {
  28972. const attributes = {};
  28973. const listeners = {};
  28974. const properties = {};
  28975. const specialAttributes = {};
  28976. for (const key of Object.keys(host)) {
  28977. const value = host[key];
  28978. const matches = key.match(HOST_REG_EXP);
  28979. if (matches === null) {
  28980. switch (key) {
  28981. case 'class':
  28982. if (typeof value !== 'string') {
  28983. // TODO(alxhub): make this a diagnostic.
  28984. throw new Error(`Class binding must be string`);
  28985. }
  28986. specialAttributes.classAttr = value;
  28987. break;
  28988. case 'style':
  28989. if (typeof value !== 'string') {
  28990. // TODO(alxhub): make this a diagnostic.
  28991. throw new Error(`Style binding must be string`);
  28992. }
  28993. specialAttributes.styleAttr = value;
  28994. break;
  28995. default:
  28996. if (typeof value === 'string') {
  28997. attributes[key] = literal(value);
  28998. }
  28999. else {
  29000. attributes[key] = value;
  29001. }
  29002. }
  29003. }
  29004. else if (matches[1 /* HostBindingGroup.Binding */] != null) {
  29005. if (typeof value !== 'string') {
  29006. // TODO(alxhub): make this a diagnostic.
  29007. throw new Error(`Property binding must be string`);
  29008. }
  29009. // synthetic properties (the ones that have a `@` as a prefix)
  29010. // are still treated the same as regular properties. Therefore
  29011. // there is no point in storing them in a separate map.
  29012. properties[matches[1 /* HostBindingGroup.Binding */]] = value;
  29013. }
  29014. else if (matches[2 /* HostBindingGroup.Event */] != null) {
  29015. if (typeof value !== 'string') {
  29016. // TODO(alxhub): make this a diagnostic.
  29017. throw new Error(`Event binding must be string`);
  29018. }
  29019. listeners[matches[2 /* HostBindingGroup.Event */]] = value;
  29020. }
  29021. }
  29022. return { attributes, listeners, properties, specialAttributes };
  29023. }
  29024. /**
  29025. * Verifies host bindings and returns the list of errors (if any). Empty array indicates that a
  29026. * given set of host bindings has no errors.
  29027. *
  29028. * @param bindings set of host bindings to verify.
  29029. * @param sourceSpan source span where host bindings were defined.
  29030. * @returns array of errors associated with a given set of host bindings.
  29031. */
  29032. function verifyHostBindings(bindings, sourceSpan) {
  29033. // TODO: abstract out host bindings verification logic and use it instead of
  29034. // creating events and properties ASTs to detect errors (FW-996)
  29035. const bindingParser = makeBindingParser();
  29036. bindingParser.createDirectiveHostEventAsts(bindings.listeners, sourceSpan);
  29037. bindingParser.createBoundHostProperties(bindings.properties, sourceSpan);
  29038. return bindingParser.errors;
  29039. }
  29040. function compileStyles(styles, selector, hostSelector) {
  29041. const shadowCss = new ShadowCss();
  29042. return styles.map((style) => {
  29043. return shadowCss.shimCssText(style, selector, hostSelector);
  29044. });
  29045. }
  29046. /**
  29047. * Encapsulates a CSS stylesheet with emulated view encapsulation.
  29048. * This allows a stylesheet to be used with an Angular component that
  29049. * is using the `ViewEncapsulation.Emulated` mode.
  29050. *
  29051. * @param style The content of a CSS stylesheet.
  29052. * @param componentIdentifier The identifier to use within the CSS rules.
  29053. * @returns The encapsulated content for the style.
  29054. */
  29055. function encapsulateStyle(style, componentIdentifier) {
  29056. const shadowCss = new ShadowCss();
  29057. const selector = componentIdentifier
  29058. ? CONTENT_ATTR.replace(COMPONENT_VARIABLE, componentIdentifier)
  29059. : CONTENT_ATTR;
  29060. const hostSelector = componentIdentifier
  29061. ? HOST_ATTR.replace(COMPONENT_VARIABLE, componentIdentifier)
  29062. : HOST_ATTR;
  29063. return shadowCss.shimCssText(style, selector, hostSelector);
  29064. }
  29065. function createHostDirectivesType(meta) {
  29066. if (!meta.hostDirectives?.length) {
  29067. return NONE_TYPE;
  29068. }
  29069. return expressionType(literalArr(meta.hostDirectives.map((hostMeta) => literalMap([
  29070. { key: 'directive', value: typeofExpr(hostMeta.directive.type), quoted: false },
  29071. {
  29072. key: 'inputs',
  29073. value: stringMapAsLiteralExpression(hostMeta.inputs || {}),
  29074. quoted: false,
  29075. },
  29076. {
  29077. key: 'outputs',
  29078. value: stringMapAsLiteralExpression(hostMeta.outputs || {}),
  29079. quoted: false,
  29080. },
  29081. ]))));
  29082. }
  29083. function createHostDirectivesFeatureArg(hostDirectives) {
  29084. const expressions = [];
  29085. let hasForwardRef = false;
  29086. for (const current of hostDirectives) {
  29087. // Use a shorthand if there are no inputs or outputs.
  29088. if (!current.inputs && !current.outputs) {
  29089. expressions.push(current.directive.type);
  29090. }
  29091. else {
  29092. const keys = [{ key: 'directive', value: current.directive.type, quoted: false }];
  29093. if (current.inputs) {
  29094. const inputsLiteral = createHostDirectivesMappingArray(current.inputs);
  29095. if (inputsLiteral) {
  29096. keys.push({ key: 'inputs', value: inputsLiteral, quoted: false });
  29097. }
  29098. }
  29099. if (current.outputs) {
  29100. const outputsLiteral = createHostDirectivesMappingArray(current.outputs);
  29101. if (outputsLiteral) {
  29102. keys.push({ key: 'outputs', value: outputsLiteral, quoted: false });
  29103. }
  29104. }
  29105. expressions.push(literalMap(keys));
  29106. }
  29107. if (current.isForwardReference) {
  29108. hasForwardRef = true;
  29109. }
  29110. }
  29111. // If there's a forward reference, we generate a `function() { return [HostDir] }`,
  29112. // otherwise we can save some bytes by using a plain array, e.g. `[HostDir]`.
  29113. return hasForwardRef
  29114. ? new FunctionExpr([], [new ReturnStatement(literalArr(expressions))])
  29115. : literalArr(expressions);
  29116. }
  29117. /**
  29118. * Converts an input/output mapping object literal into an array where the even keys are the
  29119. * public name of the binding and the odd ones are the name it was aliased to. E.g.
  29120. * `{inputOne: 'aliasOne', inputTwo: 'aliasTwo'}` will become
  29121. * `['inputOne', 'aliasOne', 'inputTwo', 'aliasTwo']`.
  29122. *
  29123. * This conversion is necessary, because hosts bind to the public name of the host directive and
  29124. * keeping the mapping in an object literal will break for apps using property renaming.
  29125. */
  29126. function createHostDirectivesMappingArray(mapping) {
  29127. const elements = [];
  29128. for (const publicName in mapping) {
  29129. if (mapping.hasOwnProperty(publicName)) {
  29130. elements.push(literal(publicName), literal(mapping[publicName]));
  29131. }
  29132. }
  29133. return elements.length > 0 ? literalArr(elements) : null;
  29134. }
  29135. /**
  29136. * Compiles the dependency resolver function for a defer block.
  29137. */
  29138. function compileDeferResolverFunction(meta) {
  29139. const depExpressions = [];
  29140. if (meta.mode === 0 /* DeferBlockDepsEmitMode.PerBlock */) {
  29141. for (const dep of meta.dependencies) {
  29142. if (dep.isDeferrable) {
  29143. // Callback function, e.g. `m () => m.MyCmp;`.
  29144. const innerFn = arrowFn(
  29145. // Default imports are always accessed through the `default` property.
  29146. [new FnParam('m', DYNAMIC_TYPE)], variable('m').prop(dep.isDefaultImport ? 'default' : dep.symbolName));
  29147. // Dynamic import, e.g. `import('./a').then(...)`.
  29148. const importExpr = new DynamicImportExpr(dep.importPath).prop('then').callFn([innerFn]);
  29149. depExpressions.push(importExpr);
  29150. }
  29151. else {
  29152. // Non-deferrable symbol, just use a reference to the type. Note that it's important to
  29153. // go through `typeReference`, rather than `symbolName` in order to preserve the
  29154. // original reference within the source file.
  29155. depExpressions.push(dep.typeReference);
  29156. }
  29157. }
  29158. }
  29159. else {
  29160. for (const { symbolName, importPath, isDefaultImport } of meta.dependencies) {
  29161. // Callback function, e.g. `m () => m.MyCmp;`.
  29162. const innerFn = arrowFn([new FnParam('m', DYNAMIC_TYPE)], variable('m').prop(isDefaultImport ? 'default' : symbolName));
  29163. // Dynamic import, e.g. `import('./a').then(...)`.
  29164. const importExpr = new DynamicImportExpr(importPath).prop('then').callFn([innerFn]);
  29165. depExpressions.push(importExpr);
  29166. }
  29167. }
  29168. return arrowFn([], literalArr(depExpressions));
  29169. }
  29170. /**
  29171. * Computes a difference between full list (first argument) and
  29172. * list of items that should be excluded from the full list (second
  29173. * argument).
  29174. */
  29175. function diff(fullList, itemsToExclude) {
  29176. const exclude = new Set(itemsToExclude);
  29177. return fullList.filter((item) => !exclude.has(item));
  29178. }
  29179. /**
  29180. * Given a template string and a set of available directive selectors,
  29181. * computes a list of matching selectors and splits them into 2 buckets:
  29182. * (1) eagerly used in a template and (2) directives used only in defer
  29183. * blocks. Similarly, returns 2 lists of pipes (eager and deferrable).
  29184. *
  29185. * Note: deferrable directives selectors and pipes names used in `@defer`
  29186. * blocks are **candidates** and API caller should make sure that:
  29187. *
  29188. * * A Component where a given template is defined is standalone
  29189. * * Underlying dependency classes are also standalone
  29190. * * Dependency class symbols are not eagerly used in a TS file
  29191. * where a host component (that owns the template) is located
  29192. */
  29193. function findMatchingDirectivesAndPipes(template, directiveSelectors) {
  29194. const matcher = new SelectorMatcher();
  29195. for (const selector of directiveSelectors) {
  29196. // Create a fake directive instance to account for the logic inside
  29197. // of the `R3TargetBinder` class (which invokes the `hasBindingPropertyName`
  29198. // function internally).
  29199. const fakeDirective = {
  29200. selector,
  29201. exportAs: null,
  29202. inputs: {
  29203. hasBindingPropertyName() {
  29204. return false;
  29205. },
  29206. },
  29207. outputs: {
  29208. hasBindingPropertyName() {
  29209. return false;
  29210. },
  29211. },
  29212. };
  29213. matcher.addSelectables(CssSelector.parse(selector), [fakeDirective]);
  29214. }
  29215. const parsedTemplate = parseTemplate(template, '' /* templateUrl */);
  29216. const binder = new R3TargetBinder(matcher);
  29217. const bound = binder.bind({ template: parsedTemplate.nodes });
  29218. const eagerDirectiveSelectors = bound.getEagerlyUsedDirectives().map((dir) => dir.selector);
  29219. const allMatchedDirectiveSelectors = bound.getUsedDirectives().map((dir) => dir.selector);
  29220. const eagerPipes = bound.getEagerlyUsedPipes();
  29221. return {
  29222. directives: {
  29223. regular: eagerDirectiveSelectors,
  29224. deferCandidates: diff(allMatchedDirectiveSelectors, eagerDirectiveSelectors),
  29225. },
  29226. pipes: {
  29227. regular: eagerPipes,
  29228. deferCandidates: diff(bound.getUsedPipes(), eagerPipes),
  29229. },
  29230. };
  29231. }
  29232. /**
  29233. * Processes `Target`s with a given set of directives and performs a binding operation, which
  29234. * returns an object similar to TypeScript's `ts.TypeChecker` that contains knowledge about the
  29235. * target.
  29236. */
  29237. class R3TargetBinder {
  29238. directiveMatcher;
  29239. constructor(directiveMatcher) {
  29240. this.directiveMatcher = directiveMatcher;
  29241. }
  29242. /**
  29243. * Perform a binding operation on the given `Target` and return a `BoundTarget` which contains
  29244. * metadata about the types referenced in the template.
  29245. */
  29246. bind(target) {
  29247. if (!target.template) {
  29248. throw new Error('Empty bound targets are not supported');
  29249. }
  29250. const directives = new Map();
  29251. const eagerDirectives = [];
  29252. const bindings = new Map();
  29253. const references = new Map();
  29254. const scopedNodeEntities = new Map();
  29255. const expressions = new Map();
  29256. const symbols = new Map();
  29257. const nestingLevel = new Map();
  29258. const usedPipes = new Set();
  29259. const eagerPipes = new Set();
  29260. const deferBlocks = [];
  29261. if (target.template) {
  29262. // First, parse the template into a `Scope` structure. This operation captures the syntactic
  29263. // scopes in the template and makes them available for later use.
  29264. const scope = Scope.apply(target.template);
  29265. // Use the `Scope` to extract the entities present at every level of the template.
  29266. extractScopedNodeEntities(scope, scopedNodeEntities);
  29267. // Next, perform directive matching on the template using the `DirectiveBinder`. This returns:
  29268. // - directives: Map of nodes (elements & ng-templates) to the directives on them.
  29269. // - bindings: Map of inputs, outputs, and attributes to the directive/element that claims
  29270. // them. TODO(alxhub): handle multiple directives claiming an input/output/etc.
  29271. // - references: Map of #references to their targets.
  29272. DirectiveBinder.apply(target.template, this.directiveMatcher, directives, eagerDirectives, bindings, references);
  29273. // Finally, run the TemplateBinder to bind references, variables, and other entities within the
  29274. // template. This extracts all the metadata that doesn't depend on directive matching.
  29275. TemplateBinder.applyWithScope(target.template, scope, expressions, symbols, nestingLevel, usedPipes, eagerPipes, deferBlocks);
  29276. }
  29277. return new R3BoundTarget(target, directives, eagerDirectives, bindings, references, expressions, symbols, nestingLevel, scopedNodeEntities, usedPipes, eagerPipes, deferBlocks);
  29278. }
  29279. }
  29280. /**
  29281. * Represents a binding scope within a template.
  29282. *
  29283. * Any variables, references, or other named entities declared within the template will
  29284. * be captured and available by name in `namedEntities`. Additionally, child templates will
  29285. * be analyzed and have their child `Scope`s available in `childScopes`.
  29286. */
  29287. class Scope {
  29288. parentScope;
  29289. rootNode;
  29290. /**
  29291. * Named members of the `Scope`, such as `Reference`s or `Variable`s.
  29292. */
  29293. namedEntities = new Map();
  29294. /**
  29295. * Set of elements that belong to this scope.
  29296. */
  29297. elementsInScope = new Set();
  29298. /**
  29299. * Child `Scope`s for immediately nested `ScopedNode`s.
  29300. */
  29301. childScopes = new Map();
  29302. /** Whether this scope is deferred or if any of its ancestors are deferred. */
  29303. isDeferred;
  29304. constructor(parentScope, rootNode) {
  29305. this.parentScope = parentScope;
  29306. this.rootNode = rootNode;
  29307. this.isDeferred =
  29308. parentScope !== null && parentScope.isDeferred ? true : rootNode instanceof DeferredBlock;
  29309. }
  29310. static newRootScope() {
  29311. return new Scope(null, null);
  29312. }
  29313. /**
  29314. * Process a template (either as a `Template` sub-template with variables, or a plain array of
  29315. * template `Node`s) and construct its `Scope`.
  29316. */
  29317. static apply(template) {
  29318. const scope = Scope.newRootScope();
  29319. scope.ingest(template);
  29320. return scope;
  29321. }
  29322. /**
  29323. * Internal method to process the scoped node and populate the `Scope`.
  29324. */
  29325. ingest(nodeOrNodes) {
  29326. if (nodeOrNodes instanceof Template) {
  29327. // Variables on an <ng-template> are defined in the inner scope.
  29328. nodeOrNodes.variables.forEach((node) => this.visitVariable(node));
  29329. // Process the nodes of the template.
  29330. nodeOrNodes.children.forEach((node) => node.visit(this));
  29331. }
  29332. else if (nodeOrNodes instanceof IfBlockBranch) {
  29333. if (nodeOrNodes.expressionAlias !== null) {
  29334. this.visitVariable(nodeOrNodes.expressionAlias);
  29335. }
  29336. nodeOrNodes.children.forEach((node) => node.visit(this));
  29337. }
  29338. else if (nodeOrNodes instanceof ForLoopBlock) {
  29339. this.visitVariable(nodeOrNodes.item);
  29340. nodeOrNodes.contextVariables.forEach((v) => this.visitVariable(v));
  29341. nodeOrNodes.children.forEach((node) => node.visit(this));
  29342. }
  29343. else if (nodeOrNodes instanceof SwitchBlockCase ||
  29344. nodeOrNodes instanceof ForLoopBlockEmpty ||
  29345. nodeOrNodes instanceof DeferredBlock ||
  29346. nodeOrNodes instanceof DeferredBlockError ||
  29347. nodeOrNodes instanceof DeferredBlockPlaceholder ||
  29348. nodeOrNodes instanceof DeferredBlockLoading ||
  29349. nodeOrNodes instanceof Content) {
  29350. nodeOrNodes.children.forEach((node) => node.visit(this));
  29351. }
  29352. else {
  29353. // No overarching `Template` instance, so process the nodes directly.
  29354. nodeOrNodes.forEach((node) => node.visit(this));
  29355. }
  29356. }
  29357. visitElement(element) {
  29358. // `Element`s in the template may have `Reference`s which are captured in the scope.
  29359. element.references.forEach((node) => this.visitReference(node));
  29360. // Recurse into the `Element`'s children.
  29361. element.children.forEach((node) => node.visit(this));
  29362. this.elementsInScope.add(element);
  29363. }
  29364. visitTemplate(template) {
  29365. // References on a <ng-template> are defined in the outer scope, so capture them before
  29366. // processing the template's child scope.
  29367. template.references.forEach((node) => this.visitReference(node));
  29368. // Next, create an inner scope and process the template within it.
  29369. this.ingestScopedNode(template);
  29370. }
  29371. visitVariable(variable) {
  29372. // Declare the variable if it's not already.
  29373. this.maybeDeclare(variable);
  29374. }
  29375. visitReference(reference) {
  29376. // Declare the variable if it's not already.
  29377. this.maybeDeclare(reference);
  29378. }
  29379. visitDeferredBlock(deferred) {
  29380. this.ingestScopedNode(deferred);
  29381. deferred.placeholder?.visit(this);
  29382. deferred.loading?.visit(this);
  29383. deferred.error?.visit(this);
  29384. }
  29385. visitDeferredBlockPlaceholder(block) {
  29386. this.ingestScopedNode(block);
  29387. }
  29388. visitDeferredBlockError(block) {
  29389. this.ingestScopedNode(block);
  29390. }
  29391. visitDeferredBlockLoading(block) {
  29392. this.ingestScopedNode(block);
  29393. }
  29394. visitSwitchBlock(block) {
  29395. block.cases.forEach((node) => node.visit(this));
  29396. }
  29397. visitSwitchBlockCase(block) {
  29398. this.ingestScopedNode(block);
  29399. }
  29400. visitForLoopBlock(block) {
  29401. this.ingestScopedNode(block);
  29402. block.empty?.visit(this);
  29403. }
  29404. visitForLoopBlockEmpty(block) {
  29405. this.ingestScopedNode(block);
  29406. }
  29407. visitIfBlock(block) {
  29408. block.branches.forEach((node) => node.visit(this));
  29409. }
  29410. visitIfBlockBranch(block) {
  29411. this.ingestScopedNode(block);
  29412. }
  29413. visitContent(content) {
  29414. this.ingestScopedNode(content);
  29415. }
  29416. visitLetDeclaration(decl) {
  29417. this.maybeDeclare(decl);
  29418. }
  29419. // Unused visitors.
  29420. visitBoundAttribute(attr) { }
  29421. visitBoundEvent(event) { }
  29422. visitBoundText(text) { }
  29423. visitText(text) { }
  29424. visitTextAttribute(attr) { }
  29425. visitIcu(icu) { }
  29426. visitDeferredTrigger(trigger) { }
  29427. visitUnknownBlock(block) { }
  29428. maybeDeclare(thing) {
  29429. // Declare something with a name, as long as that name isn't taken.
  29430. if (!this.namedEntities.has(thing.name)) {
  29431. this.namedEntities.set(thing.name, thing);
  29432. }
  29433. }
  29434. /**
  29435. * Look up a variable within this `Scope`.
  29436. *
  29437. * This can recurse into a parent `Scope` if it's available.
  29438. */
  29439. lookup(name) {
  29440. if (this.namedEntities.has(name)) {
  29441. // Found in the local scope.
  29442. return this.namedEntities.get(name);
  29443. }
  29444. else if (this.parentScope !== null) {
  29445. // Not in the local scope, but there's a parent scope so check there.
  29446. return this.parentScope.lookup(name);
  29447. }
  29448. else {
  29449. // At the top level and it wasn't found.
  29450. return null;
  29451. }
  29452. }
  29453. /**
  29454. * Get the child scope for a `ScopedNode`.
  29455. *
  29456. * This should always be defined.
  29457. */
  29458. getChildScope(node) {
  29459. const res = this.childScopes.get(node);
  29460. if (res === undefined) {
  29461. throw new Error(`Assertion error: child scope for ${node} not found`);
  29462. }
  29463. return res;
  29464. }
  29465. ingestScopedNode(node) {
  29466. const scope = new Scope(this, node);
  29467. scope.ingest(node);
  29468. this.childScopes.set(node, scope);
  29469. }
  29470. }
  29471. /**
  29472. * Processes a template and matches directives on nodes (elements and templates).
  29473. *
  29474. * Usually used via the static `apply()` method.
  29475. */
  29476. class DirectiveBinder {
  29477. matcher;
  29478. directives;
  29479. eagerDirectives;
  29480. bindings;
  29481. references;
  29482. // Indicates whether we are visiting elements within a `defer` block
  29483. isInDeferBlock = false;
  29484. constructor(matcher, directives, eagerDirectives, bindings, references) {
  29485. this.matcher = matcher;
  29486. this.directives = directives;
  29487. this.eagerDirectives = eagerDirectives;
  29488. this.bindings = bindings;
  29489. this.references = references;
  29490. }
  29491. /**
  29492. * Process a template (list of `Node`s) and perform directive matching against each node.
  29493. *
  29494. * @param template the list of template `Node`s to match (recursively).
  29495. * @param selectorMatcher a `SelectorMatcher` containing the directives that are in scope for
  29496. * this template.
  29497. * @returns three maps which contain information about directives in the template: the
  29498. * `directives` map which lists directives matched on each node, the `bindings` map which
  29499. * indicates which directives claimed which bindings (inputs, outputs, etc), and the `references`
  29500. * map which resolves #references (`Reference`s) within the template to the named directive or
  29501. * template node.
  29502. */
  29503. static apply(template, selectorMatcher, directives, eagerDirectives, bindings, references) {
  29504. const matcher = new DirectiveBinder(selectorMatcher, directives, eagerDirectives, bindings, references);
  29505. matcher.ingest(template);
  29506. }
  29507. ingest(template) {
  29508. template.forEach((node) => node.visit(this));
  29509. }
  29510. visitElement(element) {
  29511. this.visitElementOrTemplate(element);
  29512. }
  29513. visitTemplate(template) {
  29514. this.visitElementOrTemplate(template);
  29515. }
  29516. visitElementOrTemplate(node) {
  29517. // First, determine the HTML shape of the node for the purpose of directive matching.
  29518. // Do this by building up a `CssSelector` for the node.
  29519. const cssSelector = createCssSelectorFromNode(node);
  29520. // Next, use the `SelectorMatcher` to get the list of directives on the node.
  29521. const directives = [];
  29522. this.matcher.match(cssSelector, (_selector, results) => directives.push(...results));
  29523. if (directives.length > 0) {
  29524. this.directives.set(node, directives);
  29525. if (!this.isInDeferBlock) {
  29526. this.eagerDirectives.push(...directives);
  29527. }
  29528. }
  29529. // Resolve any references that are created on this node.
  29530. node.references.forEach((ref) => {
  29531. let dirTarget = null;
  29532. // If the reference expression is empty, then it matches the "primary" directive on the node
  29533. // (if there is one). Otherwise it matches the host node itself (either an element or
  29534. // <ng-template> node).
  29535. if (ref.value.trim() === '') {
  29536. // This could be a reference to a component if there is one.
  29537. dirTarget = directives.find((dir) => dir.isComponent) || null;
  29538. }
  29539. else {
  29540. // This should be a reference to a directive exported via exportAs.
  29541. dirTarget =
  29542. directives.find((dir) => dir.exportAs !== null && dir.exportAs.some((value) => value === ref.value)) || null;
  29543. // Check if a matching directive was found.
  29544. if (dirTarget === null) {
  29545. // No matching directive was found - this reference points to an unknown target. Leave it
  29546. // unmapped.
  29547. return;
  29548. }
  29549. }
  29550. if (dirTarget !== null) {
  29551. // This reference points to a directive.
  29552. this.references.set(ref, { directive: dirTarget, node });
  29553. }
  29554. else {
  29555. // This reference points to the node itself.
  29556. this.references.set(ref, node);
  29557. }
  29558. });
  29559. const setAttributeBinding = (attribute, ioType) => {
  29560. const dir = directives.find((dir) => dir[ioType].hasBindingPropertyName(attribute.name));
  29561. const binding = dir !== undefined ? dir : node;
  29562. this.bindings.set(attribute, binding);
  29563. };
  29564. // Node inputs (bound attributes) and text attributes can be bound to an
  29565. // input on a directive.
  29566. node.inputs.forEach((input) => setAttributeBinding(input, 'inputs'));
  29567. node.attributes.forEach((attr) => setAttributeBinding(attr, 'inputs'));
  29568. if (node instanceof Template) {
  29569. node.templateAttrs.forEach((attr) => setAttributeBinding(attr, 'inputs'));
  29570. }
  29571. // Node outputs (bound events) can be bound to an output on a directive.
  29572. node.outputs.forEach((output) => setAttributeBinding(output, 'outputs'));
  29573. // Recurse into the node's children.
  29574. node.children.forEach((child) => child.visit(this));
  29575. }
  29576. visitDeferredBlock(deferred) {
  29577. const wasInDeferBlock = this.isInDeferBlock;
  29578. this.isInDeferBlock = true;
  29579. deferred.children.forEach((child) => child.visit(this));
  29580. this.isInDeferBlock = wasInDeferBlock;
  29581. deferred.placeholder?.visit(this);
  29582. deferred.loading?.visit(this);
  29583. deferred.error?.visit(this);
  29584. }
  29585. visitDeferredBlockPlaceholder(block) {
  29586. block.children.forEach((child) => child.visit(this));
  29587. }
  29588. visitDeferredBlockError(block) {
  29589. block.children.forEach((child) => child.visit(this));
  29590. }
  29591. visitDeferredBlockLoading(block) {
  29592. block.children.forEach((child) => child.visit(this));
  29593. }
  29594. visitSwitchBlock(block) {
  29595. block.cases.forEach((node) => node.visit(this));
  29596. }
  29597. visitSwitchBlockCase(block) {
  29598. block.children.forEach((node) => node.visit(this));
  29599. }
  29600. visitForLoopBlock(block) {
  29601. block.item.visit(this);
  29602. block.contextVariables.forEach((v) => v.visit(this));
  29603. block.children.forEach((node) => node.visit(this));
  29604. block.empty?.visit(this);
  29605. }
  29606. visitForLoopBlockEmpty(block) {
  29607. block.children.forEach((node) => node.visit(this));
  29608. }
  29609. visitIfBlock(block) {
  29610. block.branches.forEach((node) => node.visit(this));
  29611. }
  29612. visitIfBlockBranch(block) {
  29613. block.expressionAlias?.visit(this);
  29614. block.children.forEach((node) => node.visit(this));
  29615. }
  29616. visitContent(content) {
  29617. content.children.forEach((child) => child.visit(this));
  29618. }
  29619. // Unused visitors.
  29620. visitVariable(variable) { }
  29621. visitReference(reference) { }
  29622. visitTextAttribute(attribute) { }
  29623. visitBoundAttribute(attribute) { }
  29624. visitBoundEvent(attribute) { }
  29625. visitBoundAttributeOrEvent(node) { }
  29626. visitText(text) { }
  29627. visitBoundText(text) { }
  29628. visitIcu(icu) { }
  29629. visitDeferredTrigger(trigger) { }
  29630. visitUnknownBlock(block) { }
  29631. visitLetDeclaration(decl) { }
  29632. }
  29633. /**
  29634. * Processes a template and extract metadata about expressions and symbols within.
  29635. *
  29636. * This is a companion to the `DirectiveBinder` that doesn't require knowledge of directives matched
  29637. * within the template in order to operate.
  29638. *
  29639. * Expressions are visited by the superclass `RecursiveAstVisitor`, with custom logic provided
  29640. * by overridden methods from that visitor.
  29641. */
  29642. class TemplateBinder extends RecursiveAstVisitor {
  29643. bindings;
  29644. symbols;
  29645. usedPipes;
  29646. eagerPipes;
  29647. deferBlocks;
  29648. nestingLevel;
  29649. scope;
  29650. rootNode;
  29651. level;
  29652. visitNode;
  29653. constructor(bindings, symbols, usedPipes, eagerPipes, deferBlocks, nestingLevel, scope, rootNode, level) {
  29654. super();
  29655. this.bindings = bindings;
  29656. this.symbols = symbols;
  29657. this.usedPipes = usedPipes;
  29658. this.eagerPipes = eagerPipes;
  29659. this.deferBlocks = deferBlocks;
  29660. this.nestingLevel = nestingLevel;
  29661. this.scope = scope;
  29662. this.rootNode = rootNode;
  29663. this.level = level;
  29664. // Save a bit of processing time by constructing this closure in advance.
  29665. this.visitNode = (node) => node.visit(this);
  29666. }
  29667. // This method is defined to reconcile the type of TemplateBinder since both
  29668. // RecursiveAstVisitor and Visitor define the visit() method in their
  29669. // interfaces.
  29670. visit(node, context) {
  29671. if (node instanceof AST) {
  29672. node.visit(this, context);
  29673. }
  29674. else {
  29675. node.visit(this);
  29676. }
  29677. }
  29678. /**
  29679. * Process a template and extract metadata about expressions and symbols within.
  29680. *
  29681. * @param nodes the nodes of the template to process
  29682. * @param scope the `Scope` of the template being processed.
  29683. * @returns three maps which contain metadata about the template: `expressions` which interprets
  29684. * special `AST` nodes in expressions as pointing to references or variables declared within the
  29685. * template, `symbols` which maps those variables and references to the nested `Template` which
  29686. * declares them, if any, and `nestingLevel` which associates each `Template` with a integer
  29687. * nesting level (how many levels deep within the template structure the `Template` is), starting
  29688. * at 1.
  29689. */
  29690. static applyWithScope(nodes, scope, expressions, symbols, nestingLevel, usedPipes, eagerPipes, deferBlocks) {
  29691. const template = nodes instanceof Template ? nodes : null;
  29692. // The top-level template has nesting level 0.
  29693. const binder = new TemplateBinder(expressions, symbols, usedPipes, eagerPipes, deferBlocks, nestingLevel, scope, template, 0);
  29694. binder.ingest(nodes);
  29695. }
  29696. ingest(nodeOrNodes) {
  29697. if (nodeOrNodes instanceof Template) {
  29698. // For <ng-template>s, process only variables and child nodes. Inputs, outputs, templateAttrs,
  29699. // and references were all processed in the scope of the containing template.
  29700. nodeOrNodes.variables.forEach(this.visitNode);
  29701. nodeOrNodes.children.forEach(this.visitNode);
  29702. // Set the nesting level.
  29703. this.nestingLevel.set(nodeOrNodes, this.level);
  29704. }
  29705. else if (nodeOrNodes instanceof IfBlockBranch) {
  29706. if (nodeOrNodes.expressionAlias !== null) {
  29707. this.visitNode(nodeOrNodes.expressionAlias);
  29708. }
  29709. nodeOrNodes.children.forEach(this.visitNode);
  29710. this.nestingLevel.set(nodeOrNodes, this.level);
  29711. }
  29712. else if (nodeOrNodes instanceof ForLoopBlock) {
  29713. this.visitNode(nodeOrNodes.item);
  29714. nodeOrNodes.contextVariables.forEach((v) => this.visitNode(v));
  29715. nodeOrNodes.trackBy.visit(this);
  29716. nodeOrNodes.children.forEach(this.visitNode);
  29717. this.nestingLevel.set(nodeOrNodes, this.level);
  29718. }
  29719. else if (nodeOrNodes instanceof DeferredBlock) {
  29720. if (this.scope.rootNode !== nodeOrNodes) {
  29721. throw new Error(`Assertion error: resolved incorrect scope for deferred block ${nodeOrNodes}`);
  29722. }
  29723. this.deferBlocks.push([nodeOrNodes, this.scope]);
  29724. nodeOrNodes.children.forEach((node) => node.visit(this));
  29725. this.nestingLevel.set(nodeOrNodes, this.level);
  29726. }
  29727. else if (nodeOrNodes instanceof SwitchBlockCase ||
  29728. nodeOrNodes instanceof ForLoopBlockEmpty ||
  29729. nodeOrNodes instanceof DeferredBlockError ||
  29730. nodeOrNodes instanceof DeferredBlockPlaceholder ||
  29731. nodeOrNodes instanceof DeferredBlockLoading ||
  29732. nodeOrNodes instanceof Content) {
  29733. nodeOrNodes.children.forEach((node) => node.visit(this));
  29734. this.nestingLevel.set(nodeOrNodes, this.level);
  29735. }
  29736. else {
  29737. // Visit each node from the top-level template.
  29738. nodeOrNodes.forEach(this.visitNode);
  29739. }
  29740. }
  29741. visitElement(element) {
  29742. // Visit the inputs, outputs, and children of the element.
  29743. element.inputs.forEach(this.visitNode);
  29744. element.outputs.forEach(this.visitNode);
  29745. element.children.forEach(this.visitNode);
  29746. element.references.forEach(this.visitNode);
  29747. }
  29748. visitTemplate(template) {
  29749. // First, visit inputs, outputs and template attributes of the template node.
  29750. template.inputs.forEach(this.visitNode);
  29751. template.outputs.forEach(this.visitNode);
  29752. template.templateAttrs.forEach(this.visitNode);
  29753. template.references.forEach(this.visitNode);
  29754. // Next, recurse into the template.
  29755. this.ingestScopedNode(template);
  29756. }
  29757. visitVariable(variable) {
  29758. // Register the `Variable` as a symbol in the current `Template`.
  29759. if (this.rootNode !== null) {
  29760. this.symbols.set(variable, this.rootNode);
  29761. }
  29762. }
  29763. visitReference(reference) {
  29764. // Register the `Reference` as a symbol in the current `Template`.
  29765. if (this.rootNode !== null) {
  29766. this.symbols.set(reference, this.rootNode);
  29767. }
  29768. }
  29769. // Unused template visitors
  29770. visitText(text) { }
  29771. visitTextAttribute(attribute) { }
  29772. visitUnknownBlock(block) { }
  29773. visitDeferredTrigger() { }
  29774. visitIcu(icu) {
  29775. Object.keys(icu.vars).forEach((key) => icu.vars[key].visit(this));
  29776. Object.keys(icu.placeholders).forEach((key) => icu.placeholders[key].visit(this));
  29777. }
  29778. // The remaining visitors are concerned with processing AST expressions within template bindings
  29779. visitBoundAttribute(attribute) {
  29780. attribute.value.visit(this);
  29781. }
  29782. visitBoundEvent(event) {
  29783. event.handler.visit(this);
  29784. }
  29785. visitDeferredBlock(deferred) {
  29786. this.ingestScopedNode(deferred);
  29787. deferred.triggers.when?.value.visit(this);
  29788. deferred.prefetchTriggers.when?.value.visit(this);
  29789. deferred.hydrateTriggers.when?.value.visit(this);
  29790. deferred.hydrateTriggers.never?.visit(this);
  29791. deferred.placeholder && this.visitNode(deferred.placeholder);
  29792. deferred.loading && this.visitNode(deferred.loading);
  29793. deferred.error && this.visitNode(deferred.error);
  29794. }
  29795. visitDeferredBlockPlaceholder(block) {
  29796. this.ingestScopedNode(block);
  29797. }
  29798. visitDeferredBlockError(block) {
  29799. this.ingestScopedNode(block);
  29800. }
  29801. visitDeferredBlockLoading(block) {
  29802. this.ingestScopedNode(block);
  29803. }
  29804. visitSwitchBlock(block) {
  29805. block.expression.visit(this);
  29806. block.cases.forEach(this.visitNode);
  29807. }
  29808. visitSwitchBlockCase(block) {
  29809. block.expression?.visit(this);
  29810. this.ingestScopedNode(block);
  29811. }
  29812. visitForLoopBlock(block) {
  29813. block.expression.visit(this);
  29814. this.ingestScopedNode(block);
  29815. block.empty?.visit(this);
  29816. }
  29817. visitForLoopBlockEmpty(block) {
  29818. this.ingestScopedNode(block);
  29819. }
  29820. visitIfBlock(block) {
  29821. block.branches.forEach((node) => node.visit(this));
  29822. }
  29823. visitIfBlockBranch(block) {
  29824. block.expression?.visit(this);
  29825. this.ingestScopedNode(block);
  29826. }
  29827. visitContent(content) {
  29828. this.ingestScopedNode(content);
  29829. }
  29830. visitBoundText(text) {
  29831. text.value.visit(this);
  29832. }
  29833. visitLetDeclaration(decl) {
  29834. decl.value.visit(this);
  29835. if (this.rootNode !== null) {
  29836. this.symbols.set(decl, this.rootNode);
  29837. }
  29838. }
  29839. visitPipe(ast, context) {
  29840. this.usedPipes.add(ast.name);
  29841. if (!this.scope.isDeferred) {
  29842. this.eagerPipes.add(ast.name);
  29843. }
  29844. return super.visitPipe(ast, context);
  29845. }
  29846. // These five types of AST expressions can refer to expression roots, which could be variables
  29847. // or references in the current scope.
  29848. visitPropertyRead(ast, context) {
  29849. this.maybeMap(ast, ast.name);
  29850. return super.visitPropertyRead(ast, context);
  29851. }
  29852. visitSafePropertyRead(ast, context) {
  29853. this.maybeMap(ast, ast.name);
  29854. return super.visitSafePropertyRead(ast, context);
  29855. }
  29856. visitPropertyWrite(ast, context) {
  29857. this.maybeMap(ast, ast.name);
  29858. return super.visitPropertyWrite(ast, context);
  29859. }
  29860. ingestScopedNode(node) {
  29861. const childScope = this.scope.getChildScope(node);
  29862. const binder = new TemplateBinder(this.bindings, this.symbols, this.usedPipes, this.eagerPipes, this.deferBlocks, this.nestingLevel, childScope, node, this.level + 1);
  29863. binder.ingest(node);
  29864. }
  29865. maybeMap(ast, name) {
  29866. // If the receiver of the expression isn't the `ImplicitReceiver`, this isn't the root of an
  29867. // `AST` expression that maps to a `Variable` or `Reference`.
  29868. if (!(ast.receiver instanceof ImplicitReceiver) || ast.receiver instanceof ThisReceiver) {
  29869. return;
  29870. }
  29871. // Check whether the name exists in the current scope. If so, map it. Otherwise, the name is
  29872. // probably a property on the top-level component context.
  29873. const target = this.scope.lookup(name);
  29874. if (target !== null) {
  29875. this.bindings.set(ast, target);
  29876. }
  29877. }
  29878. }
  29879. /**
  29880. * Metadata container for a `Target` that allows queries for specific bits of metadata.
  29881. *
  29882. * See `BoundTarget` for documentation on the individual methods.
  29883. */
  29884. class R3BoundTarget {
  29885. target;
  29886. directives;
  29887. eagerDirectives;
  29888. bindings;
  29889. references;
  29890. exprTargets;
  29891. symbols;
  29892. nestingLevel;
  29893. scopedNodeEntities;
  29894. usedPipes;
  29895. eagerPipes;
  29896. /** Deferred blocks, ordered as they appear in the template. */
  29897. deferredBlocks;
  29898. /** Map of deferred blocks to their scope. */
  29899. deferredScopes;
  29900. constructor(target, directives, eagerDirectives, bindings, references, exprTargets, symbols, nestingLevel, scopedNodeEntities, usedPipes, eagerPipes, rawDeferred) {
  29901. this.target = target;
  29902. this.directives = directives;
  29903. this.eagerDirectives = eagerDirectives;
  29904. this.bindings = bindings;
  29905. this.references = references;
  29906. this.exprTargets = exprTargets;
  29907. this.symbols = symbols;
  29908. this.nestingLevel = nestingLevel;
  29909. this.scopedNodeEntities = scopedNodeEntities;
  29910. this.usedPipes = usedPipes;
  29911. this.eagerPipes = eagerPipes;
  29912. this.deferredBlocks = rawDeferred.map((current) => current[0]);
  29913. this.deferredScopes = new Map(rawDeferred);
  29914. }
  29915. getEntitiesInScope(node) {
  29916. return this.scopedNodeEntities.get(node) ?? new Set();
  29917. }
  29918. getDirectivesOfNode(node) {
  29919. return this.directives.get(node) || null;
  29920. }
  29921. getReferenceTarget(ref) {
  29922. return this.references.get(ref) || null;
  29923. }
  29924. getConsumerOfBinding(binding) {
  29925. return this.bindings.get(binding) || null;
  29926. }
  29927. getExpressionTarget(expr) {
  29928. return this.exprTargets.get(expr) || null;
  29929. }
  29930. getDefinitionNodeOfSymbol(symbol) {
  29931. return this.symbols.get(symbol) || null;
  29932. }
  29933. getNestingLevel(node) {
  29934. return this.nestingLevel.get(node) || 0;
  29935. }
  29936. getUsedDirectives() {
  29937. const set = new Set();
  29938. this.directives.forEach((dirs) => dirs.forEach((dir) => set.add(dir)));
  29939. return Array.from(set.values());
  29940. }
  29941. getEagerlyUsedDirectives() {
  29942. const set = new Set(this.eagerDirectives);
  29943. return Array.from(set.values());
  29944. }
  29945. getUsedPipes() {
  29946. return Array.from(this.usedPipes);
  29947. }
  29948. getEagerlyUsedPipes() {
  29949. return Array.from(this.eagerPipes);
  29950. }
  29951. getDeferBlocks() {
  29952. return this.deferredBlocks;
  29953. }
  29954. getDeferredTriggerTarget(block, trigger) {
  29955. // Only triggers that refer to DOM nodes can be resolved.
  29956. if (!(trigger instanceof InteractionDeferredTrigger) &&
  29957. !(trigger instanceof ViewportDeferredTrigger) &&
  29958. !(trigger instanceof HoverDeferredTrigger)) {
  29959. return null;
  29960. }
  29961. const name = trigger.reference;
  29962. if (name === null) {
  29963. let trigger = null;
  29964. if (block.placeholder !== null) {
  29965. for (const child of block.placeholder.children) {
  29966. // Skip over comment nodes. Currently by default the template parser doesn't capture
  29967. // comments, but we have a safeguard here just in case since it can be enabled.
  29968. if (child instanceof Comment$1) {
  29969. continue;
  29970. }
  29971. // We can only infer the trigger if there's one root element node. Any other
  29972. // nodes at the root make it so that we can't infer the trigger anymore.
  29973. if (trigger !== null) {
  29974. return null;
  29975. }
  29976. if (child instanceof Element$1) {
  29977. trigger = child;
  29978. }
  29979. }
  29980. }
  29981. return trigger;
  29982. }
  29983. const outsideRef = this.findEntityInScope(block, name);
  29984. // First try to resolve the target in the scope of the main deferred block. Note that we
  29985. // skip triggers defined inside the main block itself, because they might not exist yet.
  29986. if (outsideRef instanceof Reference && this.getDefinitionNodeOfSymbol(outsideRef) !== block) {
  29987. const target = this.getReferenceTarget(outsideRef);
  29988. if (target !== null) {
  29989. return this.referenceTargetToElement(target);
  29990. }
  29991. }
  29992. // If the trigger couldn't be found in the main block, check the
  29993. // placeholder block which is shown before the main block has loaded.
  29994. if (block.placeholder !== null) {
  29995. const refInPlaceholder = this.findEntityInScope(block.placeholder, name);
  29996. const targetInPlaceholder = refInPlaceholder instanceof Reference ? this.getReferenceTarget(refInPlaceholder) : null;
  29997. if (targetInPlaceholder !== null) {
  29998. return this.referenceTargetToElement(targetInPlaceholder);
  29999. }
  30000. }
  30001. return null;
  30002. }
  30003. isDeferred(element) {
  30004. for (const block of this.deferredBlocks) {
  30005. if (!this.deferredScopes.has(block)) {
  30006. continue;
  30007. }
  30008. const stack = [this.deferredScopes.get(block)];
  30009. while (stack.length > 0) {
  30010. const current = stack.pop();
  30011. if (current.elementsInScope.has(element)) {
  30012. return true;
  30013. }
  30014. stack.push(...current.childScopes.values());
  30015. }
  30016. }
  30017. return false;
  30018. }
  30019. /**
  30020. * Finds an entity with a specific name in a scope.
  30021. * @param rootNode Root node of the scope.
  30022. * @param name Name of the entity.
  30023. */
  30024. findEntityInScope(rootNode, name) {
  30025. const entities = this.getEntitiesInScope(rootNode);
  30026. for (const entity of entities) {
  30027. if (entity.name === name) {
  30028. return entity;
  30029. }
  30030. }
  30031. return null;
  30032. }
  30033. /** Coerces a `ReferenceTarget` to an `Element`, if possible. */
  30034. referenceTargetToElement(target) {
  30035. if (target instanceof Element$1) {
  30036. return target;
  30037. }
  30038. if (target instanceof Template) {
  30039. return null;
  30040. }
  30041. return this.referenceTargetToElement(target.node);
  30042. }
  30043. }
  30044. function extractScopedNodeEntities(rootScope, templateEntities) {
  30045. const entityMap = new Map();
  30046. function extractScopeEntities(scope) {
  30047. if (entityMap.has(scope.rootNode)) {
  30048. return entityMap.get(scope.rootNode);
  30049. }
  30050. const currentEntities = scope.namedEntities;
  30051. let entities;
  30052. if (scope.parentScope !== null) {
  30053. entities = new Map([...extractScopeEntities(scope.parentScope), ...currentEntities]);
  30054. }
  30055. else {
  30056. entities = new Map(currentEntities);
  30057. }
  30058. entityMap.set(scope.rootNode, entities);
  30059. return entities;
  30060. }
  30061. const scopesToProcess = [rootScope];
  30062. while (scopesToProcess.length > 0) {
  30063. const scope = scopesToProcess.pop();
  30064. for (const childScope of scope.childScopes.values()) {
  30065. scopesToProcess.push(childScope);
  30066. }
  30067. extractScopeEntities(scope);
  30068. }
  30069. for (const [template, entities] of entityMap) {
  30070. templateEntities.set(template, new Set(entities.values()));
  30071. }
  30072. }
  30073. /**
  30074. * An interface for retrieving documents by URL that the compiler uses to
  30075. * load templates.
  30076. *
  30077. * This is an abstract class, rather than an interface, so that it can be used
  30078. * as injection token.
  30079. */
  30080. class ResourceLoader {
  30081. }
  30082. class CompilerFacadeImpl {
  30083. jitEvaluator;
  30084. FactoryTarget = FactoryTarget$1;
  30085. ResourceLoader = ResourceLoader;
  30086. elementSchemaRegistry = new DomElementSchemaRegistry();
  30087. constructor(jitEvaluator = new JitEvaluator()) {
  30088. this.jitEvaluator = jitEvaluator;
  30089. }
  30090. compilePipe(angularCoreEnv, sourceMapUrl, facade) {
  30091. const metadata = {
  30092. type: wrapReference(facade.type),
  30093. typeArgumentCount: 0,
  30094. pipeName: facade.pipeName,
  30095. pure: facade.pure,
  30096. isStandalone: facade.isStandalone,
  30097. };
  30098. const res = compilePipeFromMetadata(metadata);
  30099. return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, []);
  30100. }
  30101. compilePipeDeclaration(angularCoreEnv, sourceMapUrl, declaration) {
  30102. const meta = convertDeclarePipeFacadeToMetadata(declaration);
  30103. const res = compilePipeFromMetadata(meta);
  30104. return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, []);
  30105. }
  30106. compileInjectable(angularCoreEnv, sourceMapUrl, facade) {
  30107. const { expression, statements } = compileInjectable({
  30108. name: facade.name,
  30109. type: wrapReference(facade.type),
  30110. typeArgumentCount: facade.typeArgumentCount,
  30111. providedIn: computeProvidedIn(facade.providedIn),
  30112. useClass: convertToProviderExpression(facade, 'useClass'),
  30113. useFactory: wrapExpression(facade, 'useFactory'),
  30114. useValue: convertToProviderExpression(facade, 'useValue'),
  30115. useExisting: convertToProviderExpression(facade, 'useExisting'),
  30116. deps: facade.deps?.map(convertR3DependencyMetadata),
  30117. },
  30118. /* resolveForwardRefs */ true);
  30119. return this.jitExpression(expression, angularCoreEnv, sourceMapUrl, statements);
  30120. }
  30121. compileInjectableDeclaration(angularCoreEnv, sourceMapUrl, facade) {
  30122. const { expression, statements } = compileInjectable({
  30123. name: facade.type.name,
  30124. type: wrapReference(facade.type),
  30125. typeArgumentCount: 0,
  30126. providedIn: computeProvidedIn(facade.providedIn),
  30127. useClass: convertToProviderExpression(facade, 'useClass'),
  30128. useFactory: wrapExpression(facade, 'useFactory'),
  30129. useValue: convertToProviderExpression(facade, 'useValue'),
  30130. useExisting: convertToProviderExpression(facade, 'useExisting'),
  30131. deps: facade.deps?.map(convertR3DeclareDependencyMetadata),
  30132. },
  30133. /* resolveForwardRefs */ true);
  30134. return this.jitExpression(expression, angularCoreEnv, sourceMapUrl, statements);
  30135. }
  30136. compileInjector(angularCoreEnv, sourceMapUrl, facade) {
  30137. const meta = {
  30138. type: wrapReference(facade.type),
  30139. providers: facade.providers && facade.providers.length > 0
  30140. ? new WrappedNodeExpr(facade.providers)
  30141. : null,
  30142. imports: facade.imports.map((i) => new WrappedNodeExpr(i)),
  30143. };
  30144. const res = compileInjector(meta);
  30145. return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, []);
  30146. }
  30147. compileInjectorDeclaration(angularCoreEnv, sourceMapUrl, declaration) {
  30148. const meta = convertDeclareInjectorFacadeToMetadata(declaration);
  30149. const res = compileInjector(meta);
  30150. return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, []);
  30151. }
  30152. compileNgModule(angularCoreEnv, sourceMapUrl, facade) {
  30153. const meta = {
  30154. kind: R3NgModuleMetadataKind.Global,
  30155. type: wrapReference(facade.type),
  30156. bootstrap: facade.bootstrap.map(wrapReference),
  30157. declarations: facade.declarations.map(wrapReference),
  30158. publicDeclarationTypes: null, // only needed for types in AOT
  30159. imports: facade.imports.map(wrapReference),
  30160. includeImportTypes: true,
  30161. exports: facade.exports.map(wrapReference),
  30162. selectorScopeMode: R3SelectorScopeMode.Inline,
  30163. containsForwardDecls: false,
  30164. schemas: facade.schemas ? facade.schemas.map(wrapReference) : null,
  30165. id: facade.id ? new WrappedNodeExpr(facade.id) : null,
  30166. };
  30167. const res = compileNgModule(meta);
  30168. return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, []);
  30169. }
  30170. compileNgModuleDeclaration(angularCoreEnv, sourceMapUrl, declaration) {
  30171. const expression = compileNgModuleDeclarationExpression(declaration);
  30172. return this.jitExpression(expression, angularCoreEnv, sourceMapUrl, []);
  30173. }
  30174. compileDirective(angularCoreEnv, sourceMapUrl, facade) {
  30175. const meta = convertDirectiveFacadeToMetadata(facade);
  30176. return this.compileDirectiveFromMeta(angularCoreEnv, sourceMapUrl, meta);
  30177. }
  30178. compileDirectiveDeclaration(angularCoreEnv, sourceMapUrl, declaration) {
  30179. const typeSourceSpan = this.createParseSourceSpan('Directive', declaration.type.name, sourceMapUrl);
  30180. const meta = convertDeclareDirectiveFacadeToMetadata(declaration, typeSourceSpan);
  30181. return this.compileDirectiveFromMeta(angularCoreEnv, sourceMapUrl, meta);
  30182. }
  30183. compileDirectiveFromMeta(angularCoreEnv, sourceMapUrl, meta) {
  30184. const constantPool = new ConstantPool();
  30185. const bindingParser = makeBindingParser();
  30186. const res = compileDirectiveFromMetadata(meta, constantPool, bindingParser);
  30187. return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, constantPool.statements);
  30188. }
  30189. compileComponent(angularCoreEnv, sourceMapUrl, facade) {
  30190. // Parse the template and check for errors.
  30191. const { template, interpolation, defer } = parseJitTemplate(facade.template, facade.name, sourceMapUrl, facade.preserveWhitespaces, facade.interpolation, undefined);
  30192. // Compile the component metadata, including template, into an expression.
  30193. const meta = {
  30194. ...facade,
  30195. ...convertDirectiveFacadeToMetadata(facade),
  30196. selector: facade.selector || this.elementSchemaRegistry.getDefaultComponentElementName(),
  30197. template,
  30198. declarations: facade.declarations.map(convertDeclarationFacadeToMetadata),
  30199. declarationListEmitMode: 0 /* DeclarationListEmitMode.Direct */,
  30200. defer,
  30201. styles: [...facade.styles, ...template.styles],
  30202. encapsulation: facade.encapsulation,
  30203. interpolation,
  30204. changeDetection: facade.changeDetection ?? null,
  30205. animations: facade.animations != null ? new WrappedNodeExpr(facade.animations) : null,
  30206. viewProviders: facade.viewProviders != null ? new WrappedNodeExpr(facade.viewProviders) : null,
  30207. relativeContextFilePath: '',
  30208. i18nUseExternalIds: true,
  30209. relativeTemplatePath: null,
  30210. };
  30211. const jitExpressionSourceMap = `ng:///${facade.name}.js`;
  30212. return this.compileComponentFromMeta(angularCoreEnv, jitExpressionSourceMap, meta);
  30213. }
  30214. compileComponentDeclaration(angularCoreEnv, sourceMapUrl, declaration) {
  30215. const typeSourceSpan = this.createParseSourceSpan('Component', declaration.type.name, sourceMapUrl);
  30216. const meta = convertDeclareComponentFacadeToMetadata(declaration, typeSourceSpan, sourceMapUrl);
  30217. return this.compileComponentFromMeta(angularCoreEnv, sourceMapUrl, meta);
  30218. }
  30219. compileComponentFromMeta(angularCoreEnv, sourceMapUrl, meta) {
  30220. const constantPool = new ConstantPool();
  30221. const bindingParser = makeBindingParser(meta.interpolation);
  30222. const res = compileComponentFromMetadata(meta, constantPool, bindingParser);
  30223. return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, constantPool.statements);
  30224. }
  30225. compileFactory(angularCoreEnv, sourceMapUrl, meta) {
  30226. const factoryRes = compileFactoryFunction({
  30227. name: meta.name,
  30228. type: wrapReference(meta.type),
  30229. typeArgumentCount: meta.typeArgumentCount,
  30230. deps: convertR3DependencyMetadataArray(meta.deps),
  30231. target: meta.target,
  30232. });
  30233. return this.jitExpression(factoryRes.expression, angularCoreEnv, sourceMapUrl, factoryRes.statements);
  30234. }
  30235. compileFactoryDeclaration(angularCoreEnv, sourceMapUrl, meta) {
  30236. const factoryRes = compileFactoryFunction({
  30237. name: meta.type.name,
  30238. type: wrapReference(meta.type),
  30239. typeArgumentCount: 0,
  30240. deps: Array.isArray(meta.deps)
  30241. ? meta.deps.map(convertR3DeclareDependencyMetadata)
  30242. : meta.deps,
  30243. target: meta.target,
  30244. });
  30245. return this.jitExpression(factoryRes.expression, angularCoreEnv, sourceMapUrl, factoryRes.statements);
  30246. }
  30247. createParseSourceSpan(kind, typeName, sourceUrl) {
  30248. return r3JitTypeSourceSpan(kind, typeName, sourceUrl);
  30249. }
  30250. /**
  30251. * JIT compiles an expression and returns the result of executing that expression.
  30252. *
  30253. * @param def the definition which will be compiled and executed to get the value to patch
  30254. * @param context an object map of @angular/core symbol names to symbols which will be available
  30255. * in the context of the compiled expression
  30256. * @param sourceUrl a URL to use for the source map of the compiled expression
  30257. * @param preStatements a collection of statements that should be evaluated before the expression.
  30258. */
  30259. jitExpression(def, context, sourceUrl, preStatements) {
  30260. // The ConstantPool may contain Statements which declare variables used in the final expression.
  30261. // Therefore, its statements need to precede the actual JIT operation. The final statement is a
  30262. // declaration of $def which is set to the expression being compiled.
  30263. const statements = [
  30264. ...preStatements,
  30265. new DeclareVarStmt('$def', def, undefined, StmtModifier.Exported),
  30266. ];
  30267. const res = this.jitEvaluator.evaluateStatements(sourceUrl, statements, new R3JitReflector(context),
  30268. /* enableSourceMaps */ true);
  30269. return res['$def'];
  30270. }
  30271. }
  30272. function convertToR3QueryMetadata(facade) {
  30273. return {
  30274. ...facade,
  30275. isSignal: facade.isSignal,
  30276. predicate: convertQueryPredicate(facade.predicate),
  30277. read: facade.read ? new WrappedNodeExpr(facade.read) : null,
  30278. static: facade.static,
  30279. emitDistinctChangesOnly: facade.emitDistinctChangesOnly,
  30280. };
  30281. }
  30282. function convertQueryDeclarationToMetadata(declaration) {
  30283. return {
  30284. propertyName: declaration.propertyName,
  30285. first: declaration.first ?? false,
  30286. predicate: convertQueryPredicate(declaration.predicate),
  30287. descendants: declaration.descendants ?? false,
  30288. read: declaration.read ? new WrappedNodeExpr(declaration.read) : null,
  30289. static: declaration.static ?? false,
  30290. emitDistinctChangesOnly: declaration.emitDistinctChangesOnly ?? true,
  30291. isSignal: !!declaration.isSignal,
  30292. };
  30293. }
  30294. function convertQueryPredicate(predicate) {
  30295. return Array.isArray(predicate)
  30296. ? // The predicate is an array of strings so pass it through.
  30297. predicate
  30298. : // The predicate is a type - assume that we will need to unwrap any `forwardRef()` calls.
  30299. createMayBeForwardRefExpression(new WrappedNodeExpr(predicate), 1 /* ForwardRefHandling.Wrapped */);
  30300. }
  30301. function convertDirectiveFacadeToMetadata(facade) {
  30302. const inputsFromMetadata = parseInputsArray(facade.inputs || []);
  30303. const outputsFromMetadata = parseMappingStringArray(facade.outputs || []);
  30304. const propMetadata = facade.propMetadata;
  30305. const inputsFromType = {};
  30306. const outputsFromType = {};
  30307. for (const field in propMetadata) {
  30308. if (propMetadata.hasOwnProperty(field)) {
  30309. propMetadata[field].forEach((ann) => {
  30310. if (isInput(ann)) {
  30311. inputsFromType[field] = {
  30312. bindingPropertyName: ann.alias || field,
  30313. classPropertyName: field,
  30314. required: ann.required || false,
  30315. // For JIT, decorators are used to declare signal inputs. That is because of
  30316. // a technical limitation where it's not possible to statically reflect class
  30317. // members of a directive/component at runtime before instantiating the class.
  30318. isSignal: !!ann.isSignal,
  30319. transformFunction: ann.transform != null ? new WrappedNodeExpr(ann.transform) : null,
  30320. };
  30321. }
  30322. else if (isOutput(ann)) {
  30323. outputsFromType[field] = ann.alias || field;
  30324. }
  30325. });
  30326. }
  30327. }
  30328. const hostDirectives = facade.hostDirectives?.length
  30329. ? facade.hostDirectives.map((hostDirective) => {
  30330. return typeof hostDirective === 'function'
  30331. ? {
  30332. directive: wrapReference(hostDirective),
  30333. inputs: null,
  30334. outputs: null,
  30335. isForwardReference: false,
  30336. }
  30337. : {
  30338. directive: wrapReference(hostDirective.directive),
  30339. isForwardReference: false,
  30340. inputs: hostDirective.inputs ? parseMappingStringArray(hostDirective.inputs) : null,
  30341. outputs: hostDirective.outputs
  30342. ? parseMappingStringArray(hostDirective.outputs)
  30343. : null,
  30344. };
  30345. })
  30346. : null;
  30347. return {
  30348. ...facade,
  30349. typeArgumentCount: 0,
  30350. typeSourceSpan: facade.typeSourceSpan,
  30351. type: wrapReference(facade.type),
  30352. deps: null,
  30353. host: {
  30354. ...extractHostBindings(facade.propMetadata, facade.typeSourceSpan, facade.host),
  30355. },
  30356. inputs: { ...inputsFromMetadata, ...inputsFromType },
  30357. outputs: { ...outputsFromMetadata, ...outputsFromType },
  30358. queries: facade.queries.map(convertToR3QueryMetadata),
  30359. providers: facade.providers != null ? new WrappedNodeExpr(facade.providers) : null,
  30360. viewQueries: facade.viewQueries.map(convertToR3QueryMetadata),
  30361. fullInheritance: false,
  30362. hostDirectives,
  30363. };
  30364. }
  30365. function convertDeclareDirectiveFacadeToMetadata(declaration, typeSourceSpan) {
  30366. const hostDirectives = declaration.hostDirectives?.length
  30367. ? declaration.hostDirectives.map((dir) => ({
  30368. directive: wrapReference(dir.directive),
  30369. isForwardReference: false,
  30370. inputs: dir.inputs ? getHostDirectiveBindingMapping(dir.inputs) : null,
  30371. outputs: dir.outputs ? getHostDirectiveBindingMapping(dir.outputs) : null,
  30372. }))
  30373. : null;
  30374. return {
  30375. name: declaration.type.name,
  30376. type: wrapReference(declaration.type),
  30377. typeSourceSpan,
  30378. selector: declaration.selector ?? null,
  30379. inputs: declaration.inputs ? inputsPartialMetadataToInputMetadata(declaration.inputs) : {},
  30380. outputs: declaration.outputs ?? {},
  30381. host: convertHostDeclarationToMetadata(declaration.host),
  30382. queries: (declaration.queries ?? []).map(convertQueryDeclarationToMetadata),
  30383. viewQueries: (declaration.viewQueries ?? []).map(convertQueryDeclarationToMetadata),
  30384. providers: declaration.providers !== undefined ? new WrappedNodeExpr(declaration.providers) : null,
  30385. exportAs: declaration.exportAs ?? null,
  30386. usesInheritance: declaration.usesInheritance ?? false,
  30387. lifecycle: { usesOnChanges: declaration.usesOnChanges ?? false },
  30388. deps: null,
  30389. typeArgumentCount: 0,
  30390. fullInheritance: false,
  30391. isStandalone: declaration.isStandalone ?? getJitStandaloneDefaultForVersion(declaration.version),
  30392. isSignal: declaration.isSignal ?? false,
  30393. hostDirectives,
  30394. };
  30395. }
  30396. function convertHostDeclarationToMetadata(host = {}) {
  30397. return {
  30398. attributes: convertOpaqueValuesToExpressions(host.attributes ?? {}),
  30399. listeners: host.listeners ?? {},
  30400. properties: host.properties ?? {},
  30401. specialAttributes: {
  30402. classAttr: host.classAttribute,
  30403. styleAttr: host.styleAttribute,
  30404. },
  30405. };
  30406. }
  30407. /**
  30408. * Parses a host directive mapping where each odd array key is the name of an input/output
  30409. * and each even key is its public name, e.g. `['one', 'oneAlias', 'two', 'two']`.
  30410. */
  30411. function getHostDirectiveBindingMapping(array) {
  30412. let result = null;
  30413. for (let i = 1; i < array.length; i += 2) {
  30414. result = result || {};
  30415. result[array[i - 1]] = array[i];
  30416. }
  30417. return result;
  30418. }
  30419. function convertOpaqueValuesToExpressions(obj) {
  30420. const result = {};
  30421. for (const key of Object.keys(obj)) {
  30422. result[key] = new WrappedNodeExpr(obj[key]);
  30423. }
  30424. return result;
  30425. }
  30426. function convertDeclareComponentFacadeToMetadata(decl, typeSourceSpan, sourceMapUrl) {
  30427. const { template, interpolation, defer } = parseJitTemplate(decl.template, decl.type.name, sourceMapUrl, decl.preserveWhitespaces ?? false, decl.interpolation, decl.deferBlockDependencies);
  30428. const declarations = [];
  30429. if (decl.dependencies) {
  30430. for (const innerDep of decl.dependencies) {
  30431. switch (innerDep.kind) {
  30432. case 'directive':
  30433. case 'component':
  30434. declarations.push(convertDirectiveDeclarationToMetadata(innerDep));
  30435. break;
  30436. case 'pipe':
  30437. declarations.push(convertPipeDeclarationToMetadata(innerDep));
  30438. break;
  30439. }
  30440. }
  30441. }
  30442. else if (decl.components || decl.directives || decl.pipes) {
  30443. // Existing declarations on NPM may not be using the new `dependencies` merged field, and may
  30444. // have separate fields for dependencies instead. Unify them for JIT compilation.
  30445. decl.components &&
  30446. declarations.push(...decl.components.map((dir) => convertDirectiveDeclarationToMetadata(dir, /* isComponent */ true)));
  30447. decl.directives &&
  30448. declarations.push(...decl.directives.map((dir) => convertDirectiveDeclarationToMetadata(dir)));
  30449. decl.pipes && declarations.push(...convertPipeMapToMetadata(decl.pipes));
  30450. }
  30451. return {
  30452. ...convertDeclareDirectiveFacadeToMetadata(decl, typeSourceSpan),
  30453. template,
  30454. styles: decl.styles ?? [],
  30455. declarations,
  30456. viewProviders: decl.viewProviders !== undefined ? new WrappedNodeExpr(decl.viewProviders) : null,
  30457. animations: decl.animations !== undefined ? new WrappedNodeExpr(decl.animations) : null,
  30458. defer,
  30459. changeDetection: decl.changeDetection ?? ChangeDetectionStrategy.Default,
  30460. encapsulation: decl.encapsulation ?? ViewEncapsulation.Emulated,
  30461. interpolation,
  30462. declarationListEmitMode: 2 /* DeclarationListEmitMode.ClosureResolved */,
  30463. relativeContextFilePath: '',
  30464. i18nUseExternalIds: true,
  30465. relativeTemplatePath: null,
  30466. };
  30467. }
  30468. function convertDeclarationFacadeToMetadata(declaration) {
  30469. return {
  30470. ...declaration,
  30471. type: new WrappedNodeExpr(declaration.type),
  30472. };
  30473. }
  30474. function convertDirectiveDeclarationToMetadata(declaration, isComponent = null) {
  30475. return {
  30476. kind: R3TemplateDependencyKind.Directive,
  30477. isComponent: isComponent || declaration.kind === 'component',
  30478. selector: declaration.selector,
  30479. type: new WrappedNodeExpr(declaration.type),
  30480. inputs: declaration.inputs ?? [],
  30481. outputs: declaration.outputs ?? [],
  30482. exportAs: declaration.exportAs ?? null,
  30483. };
  30484. }
  30485. function convertPipeMapToMetadata(pipes) {
  30486. if (!pipes) {
  30487. return [];
  30488. }
  30489. return Object.keys(pipes).map((name) => {
  30490. return {
  30491. kind: R3TemplateDependencyKind.Pipe,
  30492. name,
  30493. type: new WrappedNodeExpr(pipes[name]),
  30494. };
  30495. });
  30496. }
  30497. function convertPipeDeclarationToMetadata(pipe) {
  30498. return {
  30499. kind: R3TemplateDependencyKind.Pipe,
  30500. name: pipe.name,
  30501. type: new WrappedNodeExpr(pipe.type),
  30502. };
  30503. }
  30504. function parseJitTemplate(template, typeName, sourceMapUrl, preserveWhitespaces, interpolation, deferBlockDependencies) {
  30505. const interpolationConfig = interpolation
  30506. ? InterpolationConfig.fromArray(interpolation)
  30507. : DEFAULT_INTERPOLATION_CONFIG;
  30508. // Parse the template and check for errors.
  30509. const parsed = parseTemplate(template, sourceMapUrl, {
  30510. preserveWhitespaces,
  30511. interpolationConfig,
  30512. });
  30513. if (parsed.errors !== null) {
  30514. const errors = parsed.errors.map((err) => err.toString()).join(', ');
  30515. throw new Error(`Errors during JIT compilation of template for ${typeName}: ${errors}`);
  30516. }
  30517. const binder = new R3TargetBinder(new SelectorMatcher());
  30518. const boundTarget = binder.bind({ template: parsed.nodes });
  30519. return {
  30520. template: parsed,
  30521. interpolation: interpolationConfig,
  30522. defer: createR3ComponentDeferMetadata(boundTarget, deferBlockDependencies),
  30523. };
  30524. }
  30525. /**
  30526. * Convert the expression, if present to an `R3ProviderExpression`.
  30527. *
  30528. * In JIT mode we do not want the compiler to wrap the expression in a `forwardRef()` call because,
  30529. * if it is referencing a type that has not yet been defined, it will have already been wrapped in
  30530. * a `forwardRef()` - either by the application developer or during partial-compilation. Thus we can
  30531. * use `ForwardRefHandling.None`.
  30532. */
  30533. function convertToProviderExpression(obj, property) {
  30534. if (obj.hasOwnProperty(property)) {
  30535. return createMayBeForwardRefExpression(new WrappedNodeExpr(obj[property]), 0 /* ForwardRefHandling.None */);
  30536. }
  30537. else {
  30538. return undefined;
  30539. }
  30540. }
  30541. function wrapExpression(obj, property) {
  30542. if (obj.hasOwnProperty(property)) {
  30543. return new WrappedNodeExpr(obj[property]);
  30544. }
  30545. else {
  30546. return undefined;
  30547. }
  30548. }
  30549. function computeProvidedIn(providedIn) {
  30550. const expression = typeof providedIn === 'function'
  30551. ? new WrappedNodeExpr(providedIn)
  30552. : new LiteralExpr(providedIn ?? null);
  30553. // See `convertToProviderExpression()` for why this uses `ForwardRefHandling.None`.
  30554. return createMayBeForwardRefExpression(expression, 0 /* ForwardRefHandling.None */);
  30555. }
  30556. function convertR3DependencyMetadataArray(facades) {
  30557. return facades == null ? null : facades.map(convertR3DependencyMetadata);
  30558. }
  30559. function convertR3DependencyMetadata(facade) {
  30560. const isAttributeDep = facade.attribute != null; // both `null` and `undefined`
  30561. const rawToken = facade.token === null ? null : new WrappedNodeExpr(facade.token);
  30562. // In JIT mode, if the dep is an `@Attribute()` then we use the attribute name given in
  30563. // `attribute` rather than the `token`.
  30564. const token = isAttributeDep ? new WrappedNodeExpr(facade.attribute) : rawToken;
  30565. return createR3DependencyMetadata(token, isAttributeDep, facade.host, facade.optional, facade.self, facade.skipSelf);
  30566. }
  30567. function convertR3DeclareDependencyMetadata(facade) {
  30568. const isAttributeDep = facade.attribute ?? false;
  30569. const token = facade.token === null ? null : new WrappedNodeExpr(facade.token);
  30570. return createR3DependencyMetadata(token, isAttributeDep, facade.host ?? false, facade.optional ?? false, facade.self ?? false, facade.skipSelf ?? false);
  30571. }
  30572. function createR3DependencyMetadata(token, isAttributeDep, host, optional, self, skipSelf) {
  30573. // If the dep is an `@Attribute()` the `attributeNameType` ought to be the `unknown` type.
  30574. // But types are not available at runtime so we just use a literal `"<unknown>"` string as a dummy
  30575. // marker.
  30576. const attributeNameType = isAttributeDep ? literal('unknown') : null;
  30577. return { token, attributeNameType, host, optional, self, skipSelf };
  30578. }
  30579. function createR3ComponentDeferMetadata(boundTarget, deferBlockDependencies) {
  30580. const deferredBlocks = boundTarget.getDeferBlocks();
  30581. const blocks = new Map();
  30582. for (let i = 0; i < deferredBlocks.length; i++) {
  30583. const dependencyFn = deferBlockDependencies?.[i];
  30584. blocks.set(deferredBlocks[i], dependencyFn ? new WrappedNodeExpr(dependencyFn) : null);
  30585. }
  30586. return { mode: 0 /* DeferBlockDepsEmitMode.PerBlock */, blocks };
  30587. }
  30588. function extractHostBindings(propMetadata, sourceSpan, host) {
  30589. // First parse the declarations from the metadata.
  30590. const bindings = parseHostBindings(host || {});
  30591. // After that check host bindings for errors
  30592. const errors = verifyHostBindings(bindings, sourceSpan);
  30593. if (errors.length) {
  30594. throw new Error(errors.map((error) => error.msg).join('\n'));
  30595. }
  30596. // Next, loop over the properties of the object, looking for @HostBinding and @HostListener.
  30597. for (const field in propMetadata) {
  30598. if (propMetadata.hasOwnProperty(field)) {
  30599. propMetadata[field].forEach((ann) => {
  30600. if (isHostBinding(ann)) {
  30601. // Since this is a decorator, we know that the value is a class member. Always access it
  30602. // through `this` so that further down the line it can't be confused for a literal value
  30603. // (e.g. if there's a property called `true`).
  30604. bindings.properties[ann.hostPropertyName || field] = getSafePropertyAccessString('this', field);
  30605. }
  30606. else if (isHostListener(ann)) {
  30607. bindings.listeners[ann.eventName || field] = `${field}(${(ann.args || []).join(',')})`;
  30608. }
  30609. });
  30610. }
  30611. }
  30612. return bindings;
  30613. }
  30614. function isHostBinding(value) {
  30615. return value.ngMetadataName === 'HostBinding';
  30616. }
  30617. function isHostListener(value) {
  30618. return value.ngMetadataName === 'HostListener';
  30619. }
  30620. function isInput(value) {
  30621. return value.ngMetadataName === 'Input';
  30622. }
  30623. function isOutput(value) {
  30624. return value.ngMetadataName === 'Output';
  30625. }
  30626. function inputsPartialMetadataToInputMetadata(inputs) {
  30627. return Object.keys(inputs).reduce((result, minifiedClassName) => {
  30628. const value = inputs[minifiedClassName];
  30629. // Handle legacy partial input output.
  30630. if (typeof value === 'string' || Array.isArray(value)) {
  30631. result[minifiedClassName] = parseLegacyInputPartialOutput(value);
  30632. }
  30633. else {
  30634. result[minifiedClassName] = {
  30635. bindingPropertyName: value.publicName,
  30636. classPropertyName: minifiedClassName,
  30637. transformFunction: value.transformFunction !== null ? new WrappedNodeExpr(value.transformFunction) : null,
  30638. required: value.isRequired,
  30639. isSignal: value.isSignal,
  30640. };
  30641. }
  30642. return result;
  30643. }, {});
  30644. }
  30645. /**
  30646. * Parses the legacy input partial output. For more details see `partial/directive.ts`.
  30647. * TODO(legacy-partial-output-inputs): Remove in v18.
  30648. */
  30649. function parseLegacyInputPartialOutput(value) {
  30650. if (typeof value === 'string') {
  30651. return {
  30652. bindingPropertyName: value,
  30653. classPropertyName: value,
  30654. transformFunction: null,
  30655. required: false,
  30656. // legacy partial output does not capture signal inputs.
  30657. isSignal: false,
  30658. };
  30659. }
  30660. return {
  30661. bindingPropertyName: value[0],
  30662. classPropertyName: value[1],
  30663. transformFunction: value[2] ? new WrappedNodeExpr(value[2]) : null,
  30664. required: false,
  30665. // legacy partial output does not capture signal inputs.
  30666. isSignal: false,
  30667. };
  30668. }
  30669. function parseInputsArray(values) {
  30670. return values.reduce((results, value) => {
  30671. if (typeof value === 'string') {
  30672. const [bindingPropertyName, classPropertyName] = parseMappingString(value);
  30673. results[classPropertyName] = {
  30674. bindingPropertyName,
  30675. classPropertyName,
  30676. required: false,
  30677. // Signal inputs not supported for the inputs array.
  30678. isSignal: false,
  30679. transformFunction: null,
  30680. };
  30681. }
  30682. else {
  30683. results[value.name] = {
  30684. bindingPropertyName: value.alias || value.name,
  30685. classPropertyName: value.name,
  30686. required: value.required || false,
  30687. // Signal inputs not supported for the inputs array.
  30688. isSignal: false,
  30689. transformFunction: value.transform != null ? new WrappedNodeExpr(value.transform) : null,
  30690. };
  30691. }
  30692. return results;
  30693. }, {});
  30694. }
  30695. function parseMappingStringArray(values) {
  30696. return values.reduce((results, value) => {
  30697. const [alias, fieldName] = parseMappingString(value);
  30698. results[fieldName] = alias;
  30699. return results;
  30700. }, {});
  30701. }
  30702. function parseMappingString(value) {
  30703. // Either the value is 'field' or 'field: property'. In the first case, `property` will
  30704. // be undefined, in which case the field name should also be used as the property name.
  30705. const [fieldName, bindingPropertyName] = value.split(':', 2).map((str) => str.trim());
  30706. return [bindingPropertyName ?? fieldName, fieldName];
  30707. }
  30708. function convertDeclarePipeFacadeToMetadata(declaration) {
  30709. return {
  30710. name: declaration.type.name,
  30711. type: wrapReference(declaration.type),
  30712. typeArgumentCount: 0,
  30713. pipeName: declaration.name,
  30714. deps: null,
  30715. pure: declaration.pure ?? true,
  30716. isStandalone: declaration.isStandalone ?? getJitStandaloneDefaultForVersion(declaration.version),
  30717. };
  30718. }
  30719. function convertDeclareInjectorFacadeToMetadata(declaration) {
  30720. return {
  30721. name: declaration.type.name,
  30722. type: wrapReference(declaration.type),
  30723. providers: declaration.providers !== undefined && declaration.providers.length > 0
  30724. ? new WrappedNodeExpr(declaration.providers)
  30725. : null,
  30726. imports: declaration.imports !== undefined
  30727. ? declaration.imports.map((i) => new WrappedNodeExpr(i))
  30728. : [],
  30729. };
  30730. }
  30731. function publishFacade(global) {
  30732. const ng = global.ng || (global.ng = {});
  30733. ng.ɵcompilerFacade = new CompilerFacadeImpl();
  30734. }
  30735. /**
  30736. * @module
  30737. * @description
  30738. * Entry point for all public APIs of the compiler package.
  30739. */
  30740. const VERSION = new Version('19.2.4');
  30741. class CompilerConfig {
  30742. defaultEncapsulation;
  30743. preserveWhitespaces;
  30744. strictInjectionParameters;
  30745. constructor({ defaultEncapsulation = ViewEncapsulation.Emulated, preserveWhitespaces, strictInjectionParameters, } = {}) {
  30746. this.defaultEncapsulation = defaultEncapsulation;
  30747. this.preserveWhitespaces = preserveWhitespacesDefault(noUndefined(preserveWhitespaces));
  30748. this.strictInjectionParameters = strictInjectionParameters === true;
  30749. }
  30750. }
  30751. function preserveWhitespacesDefault(preserveWhitespacesOption, defaultSetting = false) {
  30752. return preserveWhitespacesOption === null ? defaultSetting : preserveWhitespacesOption;
  30753. }
  30754. const _I18N_ATTR = 'i18n';
  30755. const _I18N_ATTR_PREFIX = 'i18n-';
  30756. const _I18N_COMMENT_PREFIX_REGEXP = /^i18n:?/;
  30757. const MEANING_SEPARATOR = '|';
  30758. const ID_SEPARATOR = '@@';
  30759. let i18nCommentsWarned = false;
  30760. /**
  30761. * Extract translatable messages from an html AST
  30762. */
  30763. function extractMessages(nodes, interpolationConfig, implicitTags, implicitAttrs, preserveSignificantWhitespace) {
  30764. const visitor = new _Visitor(implicitTags, implicitAttrs, preserveSignificantWhitespace);
  30765. return visitor.extract(nodes, interpolationConfig);
  30766. }
  30767. function mergeTranslations(nodes, translations, interpolationConfig, implicitTags, implicitAttrs) {
  30768. const visitor = new _Visitor(implicitTags, implicitAttrs);
  30769. return visitor.merge(nodes, translations, interpolationConfig);
  30770. }
  30771. class ExtractionResult {
  30772. messages;
  30773. errors;
  30774. constructor(messages, errors) {
  30775. this.messages = messages;
  30776. this.errors = errors;
  30777. }
  30778. }
  30779. var _VisitorMode;
  30780. (function (_VisitorMode) {
  30781. _VisitorMode[_VisitorMode["Extract"] = 0] = "Extract";
  30782. _VisitorMode[_VisitorMode["Merge"] = 1] = "Merge";
  30783. })(_VisitorMode || (_VisitorMode = {}));
  30784. /**
  30785. * This Visitor is used:
  30786. * 1. to extract all the translatable strings from an html AST (see `extract()`),
  30787. * 2. to replace the translatable strings with the actual translations (see `merge()`)
  30788. *
  30789. * @internal
  30790. */
  30791. class _Visitor {
  30792. _implicitTags;
  30793. _implicitAttrs;
  30794. _preserveSignificantWhitespace;
  30795. // Using non-null assertions because all variables are (re)set in init()
  30796. _depth;
  30797. // <el i18n>...</el>
  30798. _inI18nNode;
  30799. _inImplicitNode;
  30800. // <!--i18n-->...<!--/i18n-->
  30801. _inI18nBlock;
  30802. _blockMeaningAndDesc;
  30803. _blockChildren;
  30804. _blockStartDepth;
  30805. // {<icu message>}
  30806. _inIcu;
  30807. // set to void 0 when not in a section
  30808. _msgCountAtSectionStart;
  30809. _errors;
  30810. _mode;
  30811. // _VisitorMode.Extract only
  30812. _messages;
  30813. // _VisitorMode.Merge only
  30814. _translations;
  30815. _createI18nMessage;
  30816. constructor(_implicitTags, _implicitAttrs, _preserveSignificantWhitespace = true) {
  30817. this._implicitTags = _implicitTags;
  30818. this._implicitAttrs = _implicitAttrs;
  30819. this._preserveSignificantWhitespace = _preserveSignificantWhitespace;
  30820. }
  30821. /**
  30822. * Extracts the messages from the tree
  30823. */
  30824. extract(nodes, interpolationConfig) {
  30825. this._init(_VisitorMode.Extract, interpolationConfig);
  30826. nodes.forEach((node) => node.visit(this, null));
  30827. if (this._inI18nBlock) {
  30828. this._reportError(nodes[nodes.length - 1], 'Unclosed block');
  30829. }
  30830. return new ExtractionResult(this._messages, this._errors);
  30831. }
  30832. /**
  30833. * Returns a tree where all translatable nodes are translated
  30834. */
  30835. merge(nodes, translations, interpolationConfig) {
  30836. this._init(_VisitorMode.Merge, interpolationConfig);
  30837. this._translations = translations;
  30838. // Construct a single fake root element
  30839. const wrapper = new Element('wrapper', [], nodes, undefined, undefined, undefined);
  30840. const translatedNode = wrapper.visit(this, null);
  30841. if (this._inI18nBlock) {
  30842. this._reportError(nodes[nodes.length - 1], 'Unclosed block');
  30843. }
  30844. return new ParseTreeResult(translatedNode.children, this._errors);
  30845. }
  30846. visitExpansionCase(icuCase, context) {
  30847. // Parse cases for translatable html attributes
  30848. const expression = visitAll(this, icuCase.expression, context);
  30849. if (this._mode === _VisitorMode.Merge) {
  30850. return new ExpansionCase(icuCase.value, expression, icuCase.sourceSpan, icuCase.valueSourceSpan, icuCase.expSourceSpan);
  30851. }
  30852. }
  30853. visitExpansion(icu, context) {
  30854. this._mayBeAddBlockChildren(icu);
  30855. const wasInIcu = this._inIcu;
  30856. if (!this._inIcu) {
  30857. // nested ICU messages should not be extracted but top-level translated as a whole
  30858. if (this._isInTranslatableSection) {
  30859. this._addMessage([icu]);
  30860. }
  30861. this._inIcu = true;
  30862. }
  30863. const cases = visitAll(this, icu.cases, context);
  30864. if (this._mode === _VisitorMode.Merge) {
  30865. icu = new Expansion(icu.switchValue, icu.type, cases, icu.sourceSpan, icu.switchValueSourceSpan);
  30866. }
  30867. this._inIcu = wasInIcu;
  30868. return icu;
  30869. }
  30870. visitComment(comment, context) {
  30871. const isOpening = _isOpeningComment(comment);
  30872. if (isOpening && this._isInTranslatableSection) {
  30873. this._reportError(comment, 'Could not start a block inside a translatable section');
  30874. return;
  30875. }
  30876. const isClosing = _isClosingComment(comment);
  30877. if (isClosing && !this._inI18nBlock) {
  30878. this._reportError(comment, 'Trying to close an unopened block');
  30879. return;
  30880. }
  30881. if (!this._inI18nNode && !this._inIcu) {
  30882. if (!this._inI18nBlock) {
  30883. if (isOpening) {
  30884. // deprecated from v5 you should use <ng-container i18n> instead of i18n comments
  30885. if (!i18nCommentsWarned && console && console.warn) {
  30886. i18nCommentsWarned = true;
  30887. const details = comment.sourceSpan.details ? `, ${comment.sourceSpan.details}` : '';
  30888. // TODO(ocombe): use a log service once there is a public one available
  30889. console.warn(`I18n comments are deprecated, use an <ng-container> element instead (${comment.sourceSpan.start}${details})`);
  30890. }
  30891. this._inI18nBlock = true;
  30892. this._blockStartDepth = this._depth;
  30893. this._blockChildren = [];
  30894. this._blockMeaningAndDesc = comment
  30895. .value.replace(_I18N_COMMENT_PREFIX_REGEXP, '')
  30896. .trim();
  30897. this._openTranslatableSection(comment);
  30898. }
  30899. }
  30900. else {
  30901. if (isClosing) {
  30902. if (this._depth == this._blockStartDepth) {
  30903. this._closeTranslatableSection(comment, this._blockChildren);
  30904. this._inI18nBlock = false;
  30905. const message = this._addMessage(this._blockChildren, this._blockMeaningAndDesc);
  30906. // merge attributes in sections
  30907. const nodes = this._translateMessage(comment, message);
  30908. return visitAll(this, nodes);
  30909. }
  30910. else {
  30911. this._reportError(comment, 'I18N blocks should not cross element boundaries');
  30912. return;
  30913. }
  30914. }
  30915. }
  30916. }
  30917. }
  30918. visitText(text, context) {
  30919. if (this._isInTranslatableSection) {
  30920. this._mayBeAddBlockChildren(text);
  30921. }
  30922. return text;
  30923. }
  30924. visitElement(el, context) {
  30925. this._mayBeAddBlockChildren(el);
  30926. this._depth++;
  30927. const wasInI18nNode = this._inI18nNode;
  30928. const wasInImplicitNode = this._inImplicitNode;
  30929. let childNodes = [];
  30930. let translatedChildNodes = undefined;
  30931. // Extract:
  30932. // - top level nodes with the (implicit) "i18n" attribute if not already in a section
  30933. // - ICU messages
  30934. const i18nAttr = _getI18nAttr(el);
  30935. const i18nMeta = i18nAttr ? i18nAttr.value : '';
  30936. const isImplicit = this._implicitTags.some((tag) => el.name === tag) &&
  30937. !this._inIcu &&
  30938. !this._isInTranslatableSection;
  30939. const isTopLevelImplicit = !wasInImplicitNode && isImplicit;
  30940. this._inImplicitNode = wasInImplicitNode || isImplicit;
  30941. if (!this._isInTranslatableSection && !this._inIcu) {
  30942. if (i18nAttr || isTopLevelImplicit) {
  30943. this._inI18nNode = true;
  30944. const message = this._addMessage(el.children, i18nMeta);
  30945. translatedChildNodes = this._translateMessage(el, message);
  30946. }
  30947. if (this._mode == _VisitorMode.Extract) {
  30948. const isTranslatable = i18nAttr || isTopLevelImplicit;
  30949. if (isTranslatable)
  30950. this._openTranslatableSection(el);
  30951. visitAll(this, el.children);
  30952. if (isTranslatable)
  30953. this._closeTranslatableSection(el, el.children);
  30954. }
  30955. }
  30956. else {
  30957. if (i18nAttr || isTopLevelImplicit) {
  30958. this._reportError(el, 'Could not mark an element as translatable inside a translatable section');
  30959. }
  30960. if (this._mode == _VisitorMode.Extract) {
  30961. // Descend into child nodes for extraction
  30962. visitAll(this, el.children);
  30963. }
  30964. }
  30965. if (this._mode === _VisitorMode.Merge) {
  30966. const visitNodes = translatedChildNodes || el.children;
  30967. visitNodes.forEach((child) => {
  30968. const visited = child.visit(this, context);
  30969. if (visited && !this._isInTranslatableSection) {
  30970. // Do not add the children from translatable sections (= i18n blocks here)
  30971. // They will be added later in this loop when the block closes (i.e. on `<!-- /i18n -->`)
  30972. childNodes = childNodes.concat(visited);
  30973. }
  30974. });
  30975. }
  30976. this._visitAttributesOf(el);
  30977. this._depth--;
  30978. this._inI18nNode = wasInI18nNode;
  30979. this._inImplicitNode = wasInImplicitNode;
  30980. if (this._mode === _VisitorMode.Merge) {
  30981. const translatedAttrs = this._translateAttributes(el);
  30982. return new Element(el.name, translatedAttrs, childNodes, el.sourceSpan, el.startSourceSpan, el.endSourceSpan);
  30983. }
  30984. return null;
  30985. }
  30986. visitAttribute(attribute, context) {
  30987. throw new Error('unreachable code');
  30988. }
  30989. visitBlock(block, context) {
  30990. visitAll(this, block.children, context);
  30991. }
  30992. visitBlockParameter(parameter, context) { }
  30993. visitLetDeclaration(decl, context) { }
  30994. _init(mode, interpolationConfig) {
  30995. this._mode = mode;
  30996. this._inI18nBlock = false;
  30997. this._inI18nNode = false;
  30998. this._depth = 0;
  30999. this._inIcu = false;
  31000. this._msgCountAtSectionStart = undefined;
  31001. this._errors = [];
  31002. this._messages = [];
  31003. this._inImplicitNode = false;
  31004. this._createI18nMessage = createI18nMessageFactory(interpolationConfig, DEFAULT_CONTAINER_BLOCKS,
  31005. // When dropping significant whitespace we need to retain whitespace tokens or
  31006. // else we won't be able to reuse source spans because empty tokens would be
  31007. // removed and cause a mismatch.
  31008. /* retainEmptyTokens */ !this._preserveSignificantWhitespace,
  31009. /* preserveExpressionWhitespace */ this._preserveSignificantWhitespace);
  31010. }
  31011. // looks for translatable attributes
  31012. _visitAttributesOf(el) {
  31013. const explicitAttrNameToValue = {};
  31014. const implicitAttrNames = this._implicitAttrs[el.name] || [];
  31015. el.attrs
  31016. .filter((attr) => attr.name.startsWith(_I18N_ATTR_PREFIX))
  31017. .forEach((attr) => (explicitAttrNameToValue[attr.name.slice(_I18N_ATTR_PREFIX.length)] = attr.value));
  31018. el.attrs.forEach((attr) => {
  31019. if (attr.name in explicitAttrNameToValue) {
  31020. this._addMessage([attr], explicitAttrNameToValue[attr.name]);
  31021. }
  31022. else if (implicitAttrNames.some((name) => attr.name === name)) {
  31023. this._addMessage([attr]);
  31024. }
  31025. });
  31026. }
  31027. // add a translatable message
  31028. _addMessage(ast, msgMeta) {
  31029. if (ast.length == 0 ||
  31030. this._isEmptyAttributeValue(ast) ||
  31031. this._isPlaceholderOnlyAttributeValue(ast) ||
  31032. this._isPlaceholderOnlyMessage(ast)) {
  31033. // Do not create empty messages
  31034. return null;
  31035. }
  31036. const { meaning, description, id } = _parseMessageMeta(msgMeta);
  31037. const message = this._createI18nMessage(ast, meaning, description, id);
  31038. this._messages.push(message);
  31039. return message;
  31040. }
  31041. // Check for cases like `<div i18n-title title="">`.
  31042. _isEmptyAttributeValue(ast) {
  31043. if (!isAttrNode(ast))
  31044. return false;
  31045. const node = ast[0];
  31046. return node.value.trim() === '';
  31047. }
  31048. // Check for cases like `<div i18n-title title="{{ name }}">`.
  31049. _isPlaceholderOnlyAttributeValue(ast) {
  31050. if (!isAttrNode(ast))
  31051. return false;
  31052. const tokens = ast[0].valueTokens ?? [];
  31053. const interpolations = tokens.filter((token) => token.type === 17 /* TokenType.ATTR_VALUE_INTERPOLATION */);
  31054. const plainText = tokens
  31055. .filter((token) => token.type === 16 /* TokenType.ATTR_VALUE_TEXT */)
  31056. // `AttributeValueTextToken` always has exactly one part per its type.
  31057. .map((token) => token.parts[0].trim())
  31058. .join('');
  31059. // Check if there is a single interpolation and all text around it is empty.
  31060. return interpolations.length === 1 && plainText === '';
  31061. }
  31062. // Check for cases like `<div i18n>{{ name }}</div>`.
  31063. _isPlaceholderOnlyMessage(ast) {
  31064. if (!isTextNode(ast))
  31065. return false;
  31066. const tokens = ast[0].tokens;
  31067. const interpolations = tokens.filter((token) => token.type === 8 /* TokenType.INTERPOLATION */);
  31068. const plainText = tokens
  31069. .filter((token) => token.type === 5 /* TokenType.TEXT */)
  31070. // `TextToken` always has exactly one part per its type.
  31071. .map((token) => token.parts[0].trim())
  31072. .join('');
  31073. // Check if there is a single interpolation and all text around it is empty.
  31074. return interpolations.length === 1 && plainText === '';
  31075. }
  31076. // Translates the given message given the `TranslationBundle`
  31077. // This is used for translating elements / blocks - see `_translateAttributes` for attributes
  31078. // no-op when called in extraction mode (returns [])
  31079. _translateMessage(el, message) {
  31080. if (message && this._mode === _VisitorMode.Merge) {
  31081. const nodes = this._translations.get(message);
  31082. if (nodes) {
  31083. return nodes;
  31084. }
  31085. this._reportError(el, `Translation unavailable for message id="${this._translations.digest(message)}"`);
  31086. }
  31087. return [];
  31088. }
  31089. // translate the attributes of an element and remove i18n specific attributes
  31090. _translateAttributes(el) {
  31091. const attributes = el.attrs;
  31092. const i18nParsedMessageMeta = {};
  31093. attributes.forEach((attr) => {
  31094. if (attr.name.startsWith(_I18N_ATTR_PREFIX)) {
  31095. i18nParsedMessageMeta[attr.name.slice(_I18N_ATTR_PREFIX.length)] = _parseMessageMeta(attr.value);
  31096. }
  31097. });
  31098. const translatedAttributes = [];
  31099. attributes.forEach((attr) => {
  31100. if (attr.name === _I18N_ATTR || attr.name.startsWith(_I18N_ATTR_PREFIX)) {
  31101. // strip i18n specific attributes
  31102. return;
  31103. }
  31104. if (attr.value && attr.value != '' && i18nParsedMessageMeta.hasOwnProperty(attr.name)) {
  31105. const { meaning, description, id } = i18nParsedMessageMeta[attr.name];
  31106. const message = this._createI18nMessage([attr], meaning, description, id);
  31107. const nodes = this._translations.get(message);
  31108. if (nodes) {
  31109. if (nodes.length == 0) {
  31110. translatedAttributes.push(new Attribute(attr.name, '', attr.sourceSpan, undefined /* keySpan */, undefined /* valueSpan */, undefined /* valueTokens */, undefined /* i18n */));
  31111. }
  31112. else if (nodes[0] instanceof Text) {
  31113. const value = nodes[0].value;
  31114. translatedAttributes.push(new Attribute(attr.name, value, attr.sourceSpan, undefined /* keySpan */, undefined /* valueSpan */, undefined /* valueTokens */, undefined /* i18n */));
  31115. }
  31116. else {
  31117. this._reportError(el, `Unexpected translation for attribute "${attr.name}" (id="${id || this._translations.digest(message)}")`);
  31118. }
  31119. }
  31120. else {
  31121. this._reportError(el, `Translation unavailable for attribute "${attr.name}" (id="${id || this._translations.digest(message)}")`);
  31122. }
  31123. }
  31124. else {
  31125. translatedAttributes.push(attr);
  31126. }
  31127. });
  31128. return translatedAttributes;
  31129. }
  31130. /**
  31131. * Add the node as a child of the block when:
  31132. * - we are in a block,
  31133. * - we are not inside a ICU message (those are handled separately),
  31134. * - the node is a "direct child" of the block
  31135. */
  31136. _mayBeAddBlockChildren(node) {
  31137. if (this._inI18nBlock && !this._inIcu && this._depth == this._blockStartDepth) {
  31138. this._blockChildren.push(node);
  31139. }
  31140. }
  31141. /**
  31142. * Marks the start of a section, see `_closeTranslatableSection`
  31143. */
  31144. _openTranslatableSection(node) {
  31145. if (this._isInTranslatableSection) {
  31146. this._reportError(node, 'Unexpected section start');
  31147. }
  31148. else {
  31149. this._msgCountAtSectionStart = this._messages.length;
  31150. }
  31151. }
  31152. /**
  31153. * A translatable section could be:
  31154. * - the content of translatable element,
  31155. * - nodes between `<!-- i18n -->` and `<!-- /i18n -->` comments
  31156. */
  31157. get _isInTranslatableSection() {
  31158. return this._msgCountAtSectionStart !== void 0;
  31159. }
  31160. /**
  31161. * Terminates a section.
  31162. *
  31163. * If a section has only one significant children (comments not significant) then we should not
  31164. * keep the message from this children:
  31165. *
  31166. * `<p i18n="meaning|description">{ICU message}</p>` would produce two messages:
  31167. * - one for the <p> content with meaning and description,
  31168. * - another one for the ICU message.
  31169. *
  31170. * In this case the last message is discarded as it contains less information (the AST is
  31171. * otherwise identical).
  31172. *
  31173. * Note that we should still keep messages extracted from attributes inside the section (ie in the
  31174. * ICU message here)
  31175. */
  31176. _closeTranslatableSection(node, directChildren) {
  31177. if (!this._isInTranslatableSection) {
  31178. this._reportError(node, 'Unexpected section end');
  31179. return;
  31180. }
  31181. const startIndex = this._msgCountAtSectionStart;
  31182. const significantChildren = directChildren.reduce((count, node) => count + (node instanceof Comment ? 0 : 1), 0);
  31183. if (significantChildren == 1) {
  31184. for (let i = this._messages.length - 1; i >= startIndex; i--) {
  31185. const ast = this._messages[i].nodes;
  31186. if (!(ast.length == 1 && ast[0] instanceof Text$2)) {
  31187. this._messages.splice(i, 1);
  31188. break;
  31189. }
  31190. }
  31191. }
  31192. this._msgCountAtSectionStart = undefined;
  31193. }
  31194. _reportError(node, msg) {
  31195. this._errors.push(new I18nError(node.sourceSpan, msg));
  31196. }
  31197. }
  31198. function _isOpeningComment(n) {
  31199. return !!(n instanceof Comment && n.value && n.value.startsWith('i18n'));
  31200. }
  31201. function _isClosingComment(n) {
  31202. return !!(n instanceof Comment && n.value && n.value === '/i18n');
  31203. }
  31204. function _getI18nAttr(p) {
  31205. return p.attrs.find((attr) => attr.name === _I18N_ATTR) || null;
  31206. }
  31207. function _parseMessageMeta(i18n) {
  31208. if (!i18n)
  31209. return { meaning: '', description: '', id: '' };
  31210. const idIndex = i18n.indexOf(ID_SEPARATOR);
  31211. const descIndex = i18n.indexOf(MEANING_SEPARATOR);
  31212. const [meaningAndDesc, id] = idIndex > -1 ? [i18n.slice(0, idIndex), i18n.slice(idIndex + 2)] : [i18n, ''];
  31213. const [meaning, description] = descIndex > -1
  31214. ? [meaningAndDesc.slice(0, descIndex), meaningAndDesc.slice(descIndex + 1)]
  31215. : ['', meaningAndDesc];
  31216. return { meaning, description, id: id.trim() };
  31217. }
  31218. function isTextNode(ast) {
  31219. return ast.length === 1 && ast[0] instanceof Text;
  31220. }
  31221. function isAttrNode(ast) {
  31222. return ast.length === 1 && ast[0] instanceof Attribute;
  31223. }
  31224. class XmlTagDefinition {
  31225. closedByParent = false;
  31226. implicitNamespacePrefix = null;
  31227. isVoid = false;
  31228. ignoreFirstLf = false;
  31229. canSelfClose = true;
  31230. preventNamespaceInheritance = false;
  31231. requireExtraParent(currentParent) {
  31232. return false;
  31233. }
  31234. isClosedByChild(name) {
  31235. return false;
  31236. }
  31237. getContentType() {
  31238. return TagContentType.PARSABLE_DATA;
  31239. }
  31240. }
  31241. const _TAG_DEFINITION = new XmlTagDefinition();
  31242. function getXmlTagDefinition(tagName) {
  31243. return _TAG_DEFINITION;
  31244. }
  31245. class XmlParser extends Parser$1 {
  31246. constructor() {
  31247. super(getXmlTagDefinition);
  31248. }
  31249. parse(source, url, options = {}) {
  31250. // Blocks and let declarations aren't supported in an XML context.
  31251. return super.parse(source, url, { ...options, tokenizeBlocks: false, tokenizeLet: false });
  31252. }
  31253. }
  31254. const _VERSION$1 = '1.2';
  31255. const _XMLNS$1 = 'urn:oasis:names:tc:xliff:document:1.2';
  31256. // TODO(vicb): make this a param (s/_/-/)
  31257. const _DEFAULT_SOURCE_LANG$1 = 'en';
  31258. const _PLACEHOLDER_TAG$2 = 'x';
  31259. const _MARKER_TAG$1 = 'mrk';
  31260. const _FILE_TAG = 'file';
  31261. const _SOURCE_TAG$1 = 'source';
  31262. const _SEGMENT_SOURCE_TAG = 'seg-source';
  31263. const _ALT_TRANS_TAG = 'alt-trans';
  31264. const _TARGET_TAG$1 = 'target';
  31265. const _UNIT_TAG$1 = 'trans-unit';
  31266. const _CONTEXT_GROUP_TAG = 'context-group';
  31267. const _CONTEXT_TAG = 'context';
  31268. // https://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html
  31269. // https://docs.oasis-open.org/xliff/v1.2/xliff-profile-html/xliff-profile-html-1.2.html
  31270. class Xliff extends Serializer {
  31271. write(messages, locale) {
  31272. const visitor = new _WriteVisitor$1();
  31273. const transUnits = [];
  31274. messages.forEach((message) => {
  31275. let contextTags = [];
  31276. message.sources.forEach((source) => {
  31277. let contextGroupTag = new Tag(_CONTEXT_GROUP_TAG, { purpose: 'location' });
  31278. contextGroupTag.children.push(new CR(10), new Tag(_CONTEXT_TAG, { 'context-type': 'sourcefile' }, [
  31279. new Text$1(source.filePath),
  31280. ]), new CR(10), new Tag(_CONTEXT_TAG, { 'context-type': 'linenumber' }, [
  31281. new Text$1(`${source.startLine}`),
  31282. ]), new CR(8));
  31283. contextTags.push(new CR(8), contextGroupTag);
  31284. });
  31285. const transUnit = new Tag(_UNIT_TAG$1, { id: message.id, datatype: 'html' });
  31286. transUnit.children.push(new CR(8), new Tag(_SOURCE_TAG$1, {}, visitor.serialize(message.nodes)), ...contextTags);
  31287. if (message.description) {
  31288. transUnit.children.push(new CR(8), new Tag('note', { priority: '1', from: 'description' }, [
  31289. new Text$1(message.description),
  31290. ]));
  31291. }
  31292. if (message.meaning) {
  31293. transUnit.children.push(new CR(8), new Tag('note', { priority: '1', from: 'meaning' }, [new Text$1(message.meaning)]));
  31294. }
  31295. transUnit.children.push(new CR(6));
  31296. transUnits.push(new CR(6), transUnit);
  31297. });
  31298. const body = new Tag('body', {}, [...transUnits, new CR(4)]);
  31299. const file = new Tag('file', {
  31300. 'source-language': locale || _DEFAULT_SOURCE_LANG$1,
  31301. datatype: 'plaintext',
  31302. original: 'ng2.template',
  31303. }, [new CR(4), body, new CR(2)]);
  31304. const xliff = new Tag('xliff', { version: _VERSION$1, xmlns: _XMLNS$1 }, [
  31305. new CR(2),
  31306. file,
  31307. new CR(),
  31308. ]);
  31309. return serialize$1([
  31310. new Declaration({ version: '1.0', encoding: 'UTF-8' }),
  31311. new CR(),
  31312. xliff,
  31313. new CR(),
  31314. ]);
  31315. }
  31316. load(content, url) {
  31317. // xliff to xml nodes
  31318. const xliffParser = new XliffParser();
  31319. const { locale, msgIdToHtml, errors } = xliffParser.parse(content, url);
  31320. // xml nodes to i18n nodes
  31321. const i18nNodesByMsgId = {};
  31322. const converter = new XmlToI18n$2();
  31323. Object.keys(msgIdToHtml).forEach((msgId) => {
  31324. const { i18nNodes, errors: e } = converter.convert(msgIdToHtml[msgId], url);
  31325. errors.push(...e);
  31326. i18nNodesByMsgId[msgId] = i18nNodes;
  31327. });
  31328. if (errors.length) {
  31329. throw new Error(`xliff parse errors:\n${errors.join('\n')}`);
  31330. }
  31331. return { locale: locale, i18nNodesByMsgId };
  31332. }
  31333. digest(message) {
  31334. return digest$1(message);
  31335. }
  31336. }
  31337. let _WriteVisitor$1 = class _WriteVisitor {
  31338. visitText(text, context) {
  31339. return [new Text$1(text.value)];
  31340. }
  31341. visitContainer(container, context) {
  31342. const nodes = [];
  31343. container.children.forEach((node) => nodes.push(...node.visit(this)));
  31344. return nodes;
  31345. }
  31346. visitIcu(icu, context) {
  31347. const nodes = [new Text$1(`{${icu.expressionPlaceholder}, ${icu.type}, `)];
  31348. Object.keys(icu.cases).forEach((c) => {
  31349. nodes.push(new Text$1(`${c} {`), ...icu.cases[c].visit(this), new Text$1(`} `));
  31350. });
  31351. nodes.push(new Text$1(`}`));
  31352. return nodes;
  31353. }
  31354. visitTagPlaceholder(ph, context) {
  31355. const ctype = getCtypeForTag(ph.tag);
  31356. if (ph.isVoid) {
  31357. // void tags have no children nor closing tags
  31358. return [
  31359. new Tag(_PLACEHOLDER_TAG$2, { id: ph.startName, ctype, 'equiv-text': `<${ph.tag}/>` }),
  31360. ];
  31361. }
  31362. const startTagPh = new Tag(_PLACEHOLDER_TAG$2, {
  31363. id: ph.startName,
  31364. ctype,
  31365. 'equiv-text': `<${ph.tag}>`,
  31366. });
  31367. const closeTagPh = new Tag(_PLACEHOLDER_TAG$2, {
  31368. id: ph.closeName,
  31369. ctype,
  31370. 'equiv-text': `</${ph.tag}>`,
  31371. });
  31372. return [startTagPh, ...this.serialize(ph.children), closeTagPh];
  31373. }
  31374. visitPlaceholder(ph, context) {
  31375. return [new Tag(_PLACEHOLDER_TAG$2, { id: ph.name, 'equiv-text': `{{${ph.value}}}` })];
  31376. }
  31377. visitBlockPlaceholder(ph, context) {
  31378. const ctype = `x-${ph.name.toLowerCase().replace(/[^a-z0-9]/g, '-')}`;
  31379. const startTagPh = new Tag(_PLACEHOLDER_TAG$2, {
  31380. id: ph.startName,
  31381. ctype,
  31382. 'equiv-text': `@${ph.name}`,
  31383. });
  31384. const closeTagPh = new Tag(_PLACEHOLDER_TAG$2, { id: ph.closeName, ctype, 'equiv-text': `}` });
  31385. return [startTagPh, ...this.serialize(ph.children), closeTagPh];
  31386. }
  31387. visitIcuPlaceholder(ph, context) {
  31388. const equivText = `{${ph.value.expression}, ${ph.value.type}, ${Object.keys(ph.value.cases)
  31389. .map((value) => value + ' {...}')
  31390. .join(' ')}}`;
  31391. return [new Tag(_PLACEHOLDER_TAG$2, { id: ph.name, 'equiv-text': equivText })];
  31392. }
  31393. serialize(nodes) {
  31394. return [].concat(...nodes.map((node) => node.visit(this)));
  31395. }
  31396. };
  31397. // TODO(vicb): add error management (structure)
  31398. // Extract messages as xml nodes from the xliff file
  31399. class XliffParser {
  31400. // using non-null assertions because they're re(set) by parse()
  31401. _unitMlString;
  31402. _errors;
  31403. _msgIdToHtml;
  31404. _locale = null;
  31405. parse(xliff, url) {
  31406. this._unitMlString = null;
  31407. this._msgIdToHtml = {};
  31408. const xml = new XmlParser().parse(xliff, url);
  31409. this._errors = xml.errors;
  31410. visitAll(this, xml.rootNodes, null);
  31411. return {
  31412. msgIdToHtml: this._msgIdToHtml,
  31413. errors: this._errors,
  31414. locale: this._locale,
  31415. };
  31416. }
  31417. visitElement(element, context) {
  31418. switch (element.name) {
  31419. case _UNIT_TAG$1:
  31420. this._unitMlString = null;
  31421. const idAttr = element.attrs.find((attr) => attr.name === 'id');
  31422. if (!idAttr) {
  31423. this._addError(element, `<${_UNIT_TAG$1}> misses the "id" attribute`);
  31424. }
  31425. else {
  31426. const id = idAttr.value;
  31427. if (this._msgIdToHtml.hasOwnProperty(id)) {
  31428. this._addError(element, `Duplicated translations for msg ${id}`);
  31429. }
  31430. else {
  31431. visitAll(this, element.children, null);
  31432. if (typeof this._unitMlString === 'string') {
  31433. this._msgIdToHtml[id] = this._unitMlString;
  31434. }
  31435. else {
  31436. this._addError(element, `Message ${id} misses a translation`);
  31437. }
  31438. }
  31439. }
  31440. break;
  31441. // ignore those tags
  31442. case _SOURCE_TAG$1:
  31443. case _SEGMENT_SOURCE_TAG:
  31444. case _ALT_TRANS_TAG:
  31445. break;
  31446. case _TARGET_TAG$1:
  31447. const innerTextStart = element.startSourceSpan.end.offset;
  31448. const innerTextEnd = element.endSourceSpan.start.offset;
  31449. const content = element.startSourceSpan.start.file.content;
  31450. const innerText = content.slice(innerTextStart, innerTextEnd);
  31451. this._unitMlString = innerText;
  31452. break;
  31453. case _FILE_TAG:
  31454. const localeAttr = element.attrs.find((attr) => attr.name === 'target-language');
  31455. if (localeAttr) {
  31456. this._locale = localeAttr.value;
  31457. }
  31458. visitAll(this, element.children, null);
  31459. break;
  31460. default:
  31461. // TODO(vicb): assert file structure, xliff version
  31462. // For now only recurse on unhandled nodes
  31463. visitAll(this, element.children, null);
  31464. }
  31465. }
  31466. visitAttribute(attribute, context) { }
  31467. visitText(text, context) { }
  31468. visitComment(comment, context) { }
  31469. visitExpansion(expansion, context) { }
  31470. visitExpansionCase(expansionCase, context) { }
  31471. visitBlock(block, context) { }
  31472. visitBlockParameter(parameter, context) { }
  31473. visitLetDeclaration(decl, context) { }
  31474. _addError(node, message) {
  31475. this._errors.push(new I18nError(node.sourceSpan, message));
  31476. }
  31477. }
  31478. // Convert ml nodes (xliff syntax) to i18n nodes
  31479. let XmlToI18n$2 = class XmlToI18n {
  31480. // using non-null assertion because it's re(set) by convert()
  31481. _errors;
  31482. convert(message, url) {
  31483. const xmlIcu = new XmlParser().parse(message, url, { tokenizeExpansionForms: true });
  31484. this._errors = xmlIcu.errors;
  31485. const i18nNodes = this._errors.length > 0 || xmlIcu.rootNodes.length == 0
  31486. ? []
  31487. : [].concat(...visitAll(this, xmlIcu.rootNodes));
  31488. return {
  31489. i18nNodes: i18nNodes,
  31490. errors: this._errors,
  31491. };
  31492. }
  31493. visitText(text, context) {
  31494. return new Text$2(text.value, text.sourceSpan);
  31495. }
  31496. visitElement(el, context) {
  31497. if (el.name === _PLACEHOLDER_TAG$2) {
  31498. const nameAttr = el.attrs.find((attr) => attr.name === 'id');
  31499. if (nameAttr) {
  31500. return new Placeholder('', nameAttr.value, el.sourceSpan);
  31501. }
  31502. this._addError(el, `<${_PLACEHOLDER_TAG$2}> misses the "id" attribute`);
  31503. return null;
  31504. }
  31505. if (el.name === _MARKER_TAG$1) {
  31506. return [].concat(...visitAll(this, el.children));
  31507. }
  31508. this._addError(el, `Unexpected tag`);
  31509. return null;
  31510. }
  31511. visitExpansion(icu, context) {
  31512. const caseMap = {};
  31513. visitAll(this, icu.cases).forEach((c) => {
  31514. caseMap[c.value] = new Container(c.nodes, icu.sourceSpan);
  31515. });
  31516. return new Icu(icu.switchValue, icu.type, caseMap, icu.sourceSpan);
  31517. }
  31518. visitExpansionCase(icuCase, context) {
  31519. return {
  31520. value: icuCase.value,
  31521. nodes: visitAll(this, icuCase.expression),
  31522. };
  31523. }
  31524. visitComment(comment, context) { }
  31525. visitAttribute(attribute, context) { }
  31526. visitBlock(block, context) { }
  31527. visitBlockParameter(parameter, context) { }
  31528. visitLetDeclaration(decl, context) { }
  31529. _addError(node, message) {
  31530. this._errors.push(new I18nError(node.sourceSpan, message));
  31531. }
  31532. };
  31533. function getCtypeForTag(tag) {
  31534. switch (tag.toLowerCase()) {
  31535. case 'br':
  31536. return 'lb';
  31537. case 'img':
  31538. return 'image';
  31539. default:
  31540. return `x-${tag}`;
  31541. }
  31542. }
  31543. const _VERSION = '2.0';
  31544. const _XMLNS = 'urn:oasis:names:tc:xliff:document:2.0';
  31545. // TODO(vicb): make this a param (s/_/-/)
  31546. const _DEFAULT_SOURCE_LANG = 'en';
  31547. const _PLACEHOLDER_TAG$1 = 'ph';
  31548. const _PLACEHOLDER_SPANNING_TAG = 'pc';
  31549. const _MARKER_TAG = 'mrk';
  31550. const _XLIFF_TAG = 'xliff';
  31551. const _SOURCE_TAG = 'source';
  31552. const _TARGET_TAG = 'target';
  31553. const _UNIT_TAG = 'unit';
  31554. // https://docs.oasis-open.org/xliff/xliff-core/v2.0/os/xliff-core-v2.0-os.html
  31555. class Xliff2 extends Serializer {
  31556. write(messages, locale) {
  31557. const visitor = new _WriteVisitor();
  31558. const units = [];
  31559. messages.forEach((message) => {
  31560. const unit = new Tag(_UNIT_TAG, { id: message.id });
  31561. const notes = new Tag('notes');
  31562. if (message.description || message.meaning) {
  31563. if (message.description) {
  31564. notes.children.push(new CR(8), new Tag('note', { category: 'description' }, [new Text$1(message.description)]));
  31565. }
  31566. if (message.meaning) {
  31567. notes.children.push(new CR(8), new Tag('note', { category: 'meaning' }, [new Text$1(message.meaning)]));
  31568. }
  31569. }
  31570. message.sources.forEach((source) => {
  31571. notes.children.push(new CR(8), new Tag('note', { category: 'location' }, [
  31572. new Text$1(`${source.filePath}:${source.startLine}${source.endLine !== source.startLine ? ',' + source.endLine : ''}`),
  31573. ]));
  31574. });
  31575. notes.children.push(new CR(6));
  31576. unit.children.push(new CR(6), notes);
  31577. const segment = new Tag('segment');
  31578. segment.children.push(new CR(8), new Tag(_SOURCE_TAG, {}, visitor.serialize(message.nodes)), new CR(6));
  31579. unit.children.push(new CR(6), segment, new CR(4));
  31580. units.push(new CR(4), unit);
  31581. });
  31582. const file = new Tag('file', { 'original': 'ng.template', id: 'ngi18n' }, [
  31583. ...units,
  31584. new CR(2),
  31585. ]);
  31586. const xliff = new Tag(_XLIFF_TAG, { version: _VERSION, xmlns: _XMLNS, srcLang: locale || _DEFAULT_SOURCE_LANG }, [new CR(2), file, new CR()]);
  31587. return serialize$1([
  31588. new Declaration({ version: '1.0', encoding: 'UTF-8' }),
  31589. new CR(),
  31590. xliff,
  31591. new CR(),
  31592. ]);
  31593. }
  31594. load(content, url) {
  31595. // xliff to xml nodes
  31596. const xliff2Parser = new Xliff2Parser();
  31597. const { locale, msgIdToHtml, errors } = xliff2Parser.parse(content, url);
  31598. // xml nodes to i18n nodes
  31599. const i18nNodesByMsgId = {};
  31600. const converter = new XmlToI18n$1();
  31601. Object.keys(msgIdToHtml).forEach((msgId) => {
  31602. const { i18nNodes, errors: e } = converter.convert(msgIdToHtml[msgId], url);
  31603. errors.push(...e);
  31604. i18nNodesByMsgId[msgId] = i18nNodes;
  31605. });
  31606. if (errors.length) {
  31607. throw new Error(`xliff2 parse errors:\n${errors.join('\n')}`);
  31608. }
  31609. return { locale: locale, i18nNodesByMsgId };
  31610. }
  31611. digest(message) {
  31612. return decimalDigest(message);
  31613. }
  31614. }
  31615. class _WriteVisitor {
  31616. _nextPlaceholderId = 0;
  31617. visitText(text, context) {
  31618. return [new Text$1(text.value)];
  31619. }
  31620. visitContainer(container, context) {
  31621. const nodes = [];
  31622. container.children.forEach((node) => nodes.push(...node.visit(this)));
  31623. return nodes;
  31624. }
  31625. visitIcu(icu, context) {
  31626. const nodes = [new Text$1(`{${icu.expressionPlaceholder}, ${icu.type}, `)];
  31627. Object.keys(icu.cases).forEach((c) => {
  31628. nodes.push(new Text$1(`${c} {`), ...icu.cases[c].visit(this), new Text$1(`} `));
  31629. });
  31630. nodes.push(new Text$1(`}`));
  31631. return nodes;
  31632. }
  31633. visitTagPlaceholder(ph, context) {
  31634. const type = getTypeForTag(ph.tag);
  31635. if (ph.isVoid) {
  31636. const tagPh = new Tag(_PLACEHOLDER_TAG$1, {
  31637. id: (this._nextPlaceholderId++).toString(),
  31638. equiv: ph.startName,
  31639. type: type,
  31640. disp: `<${ph.tag}/>`,
  31641. });
  31642. return [tagPh];
  31643. }
  31644. const tagPc = new Tag(_PLACEHOLDER_SPANNING_TAG, {
  31645. id: (this._nextPlaceholderId++).toString(),
  31646. equivStart: ph.startName,
  31647. equivEnd: ph.closeName,
  31648. type: type,
  31649. dispStart: `<${ph.tag}>`,
  31650. dispEnd: `</${ph.tag}>`,
  31651. });
  31652. const nodes = [].concat(...ph.children.map((node) => node.visit(this)));
  31653. if (nodes.length) {
  31654. nodes.forEach((node) => tagPc.children.push(node));
  31655. }
  31656. else {
  31657. tagPc.children.push(new Text$1(''));
  31658. }
  31659. return [tagPc];
  31660. }
  31661. visitPlaceholder(ph, context) {
  31662. const idStr = (this._nextPlaceholderId++).toString();
  31663. return [
  31664. new Tag(_PLACEHOLDER_TAG$1, {
  31665. id: idStr,
  31666. equiv: ph.name,
  31667. disp: `{{${ph.value}}}`,
  31668. }),
  31669. ];
  31670. }
  31671. visitBlockPlaceholder(ph, context) {
  31672. const tagPc = new Tag(_PLACEHOLDER_SPANNING_TAG, {
  31673. id: (this._nextPlaceholderId++).toString(),
  31674. equivStart: ph.startName,
  31675. equivEnd: ph.closeName,
  31676. type: 'other',
  31677. dispStart: `@${ph.name}`,
  31678. dispEnd: `}`,
  31679. });
  31680. const nodes = [].concat(...ph.children.map((node) => node.visit(this)));
  31681. if (nodes.length) {
  31682. nodes.forEach((node) => tagPc.children.push(node));
  31683. }
  31684. else {
  31685. tagPc.children.push(new Text$1(''));
  31686. }
  31687. return [tagPc];
  31688. }
  31689. visitIcuPlaceholder(ph, context) {
  31690. const cases = Object.keys(ph.value.cases)
  31691. .map((value) => value + ' {...}')
  31692. .join(' ');
  31693. const idStr = (this._nextPlaceholderId++).toString();
  31694. return [
  31695. new Tag(_PLACEHOLDER_TAG$1, {
  31696. id: idStr,
  31697. equiv: ph.name,
  31698. disp: `{${ph.value.expression}, ${ph.value.type}, ${cases}}`,
  31699. }),
  31700. ];
  31701. }
  31702. serialize(nodes) {
  31703. this._nextPlaceholderId = 0;
  31704. return [].concat(...nodes.map((node) => node.visit(this)));
  31705. }
  31706. }
  31707. // Extract messages as xml nodes from the xliff file
  31708. class Xliff2Parser {
  31709. // using non-null assertions because they're all (re)set by parse()
  31710. _unitMlString;
  31711. _errors;
  31712. _msgIdToHtml;
  31713. _locale = null;
  31714. parse(xliff, url) {
  31715. this._unitMlString = null;
  31716. this._msgIdToHtml = {};
  31717. const xml = new XmlParser().parse(xliff, url);
  31718. this._errors = xml.errors;
  31719. visitAll(this, xml.rootNodes, null);
  31720. return {
  31721. msgIdToHtml: this._msgIdToHtml,
  31722. errors: this._errors,
  31723. locale: this._locale,
  31724. };
  31725. }
  31726. visitElement(element, context) {
  31727. switch (element.name) {
  31728. case _UNIT_TAG:
  31729. this._unitMlString = null;
  31730. const idAttr = element.attrs.find((attr) => attr.name === 'id');
  31731. if (!idAttr) {
  31732. this._addError(element, `<${_UNIT_TAG}> misses the "id" attribute`);
  31733. }
  31734. else {
  31735. const id = idAttr.value;
  31736. if (this._msgIdToHtml.hasOwnProperty(id)) {
  31737. this._addError(element, `Duplicated translations for msg ${id}`);
  31738. }
  31739. else {
  31740. visitAll(this, element.children, null);
  31741. if (typeof this._unitMlString === 'string') {
  31742. this._msgIdToHtml[id] = this._unitMlString;
  31743. }
  31744. else {
  31745. this._addError(element, `Message ${id} misses a translation`);
  31746. }
  31747. }
  31748. }
  31749. break;
  31750. case _SOURCE_TAG:
  31751. // ignore source message
  31752. break;
  31753. case _TARGET_TAG:
  31754. const innerTextStart = element.startSourceSpan.end.offset;
  31755. const innerTextEnd = element.endSourceSpan.start.offset;
  31756. const content = element.startSourceSpan.start.file.content;
  31757. const innerText = content.slice(innerTextStart, innerTextEnd);
  31758. this._unitMlString = innerText;
  31759. break;
  31760. case _XLIFF_TAG:
  31761. const localeAttr = element.attrs.find((attr) => attr.name === 'trgLang');
  31762. if (localeAttr) {
  31763. this._locale = localeAttr.value;
  31764. }
  31765. const versionAttr = element.attrs.find((attr) => attr.name === 'version');
  31766. if (versionAttr) {
  31767. const version = versionAttr.value;
  31768. if (version !== '2.0') {
  31769. this._addError(element, `The XLIFF file version ${version} is not compatible with XLIFF 2.0 serializer`);
  31770. }
  31771. else {
  31772. visitAll(this, element.children, null);
  31773. }
  31774. }
  31775. break;
  31776. default:
  31777. visitAll(this, element.children, null);
  31778. }
  31779. }
  31780. visitAttribute(attribute, context) { }
  31781. visitText(text, context) { }
  31782. visitComment(comment, context) { }
  31783. visitExpansion(expansion, context) { }
  31784. visitExpansionCase(expansionCase, context) { }
  31785. visitBlock(block, context) { }
  31786. visitBlockParameter(parameter, context) { }
  31787. visitLetDeclaration(decl, context) { }
  31788. _addError(node, message) {
  31789. this._errors.push(new I18nError(node.sourceSpan, message));
  31790. }
  31791. }
  31792. // Convert ml nodes (xliff syntax) to i18n nodes
  31793. let XmlToI18n$1 = class XmlToI18n {
  31794. // using non-null assertion because re(set) by convert()
  31795. _errors;
  31796. convert(message, url) {
  31797. const xmlIcu = new XmlParser().parse(message, url, { tokenizeExpansionForms: true });
  31798. this._errors = xmlIcu.errors;
  31799. const i18nNodes = this._errors.length > 0 || xmlIcu.rootNodes.length == 0
  31800. ? []
  31801. : [].concat(...visitAll(this, xmlIcu.rootNodes));
  31802. return {
  31803. i18nNodes,
  31804. errors: this._errors,
  31805. };
  31806. }
  31807. visitText(text, context) {
  31808. return new Text$2(text.value, text.sourceSpan);
  31809. }
  31810. visitElement(el, context) {
  31811. switch (el.name) {
  31812. case _PLACEHOLDER_TAG$1:
  31813. const nameAttr = el.attrs.find((attr) => attr.name === 'equiv');
  31814. if (nameAttr) {
  31815. return [new Placeholder('', nameAttr.value, el.sourceSpan)];
  31816. }
  31817. this._addError(el, `<${_PLACEHOLDER_TAG$1}> misses the "equiv" attribute`);
  31818. break;
  31819. case _PLACEHOLDER_SPANNING_TAG:
  31820. const startAttr = el.attrs.find((attr) => attr.name === 'equivStart');
  31821. const endAttr = el.attrs.find((attr) => attr.name === 'equivEnd');
  31822. if (!startAttr) {
  31823. this._addError(el, `<${_PLACEHOLDER_TAG$1}> misses the "equivStart" attribute`);
  31824. }
  31825. else if (!endAttr) {
  31826. this._addError(el, `<${_PLACEHOLDER_TAG$1}> misses the "equivEnd" attribute`);
  31827. }
  31828. else {
  31829. const startId = startAttr.value;
  31830. const endId = endAttr.value;
  31831. const nodes = [];
  31832. return nodes.concat(new Placeholder('', startId, el.sourceSpan), ...el.children.map((node) => node.visit(this, null)), new Placeholder('', endId, el.sourceSpan));
  31833. }
  31834. break;
  31835. case _MARKER_TAG:
  31836. return [].concat(...visitAll(this, el.children));
  31837. default:
  31838. this._addError(el, `Unexpected tag`);
  31839. }
  31840. return null;
  31841. }
  31842. visitExpansion(icu, context) {
  31843. const caseMap = {};
  31844. visitAll(this, icu.cases).forEach((c) => {
  31845. caseMap[c.value] = new Container(c.nodes, icu.sourceSpan);
  31846. });
  31847. return new Icu(icu.switchValue, icu.type, caseMap, icu.sourceSpan);
  31848. }
  31849. visitExpansionCase(icuCase, context) {
  31850. return {
  31851. value: icuCase.value,
  31852. nodes: [].concat(...visitAll(this, icuCase.expression)),
  31853. };
  31854. }
  31855. visitComment(comment, context) { }
  31856. visitAttribute(attribute, context) { }
  31857. visitBlock(block, context) { }
  31858. visitBlockParameter(parameter, context) { }
  31859. visitLetDeclaration(decl, context) { }
  31860. _addError(node, message) {
  31861. this._errors.push(new I18nError(node.sourceSpan, message));
  31862. }
  31863. };
  31864. function getTypeForTag(tag) {
  31865. switch (tag.toLowerCase()) {
  31866. case 'br':
  31867. case 'b':
  31868. case 'i':
  31869. case 'u':
  31870. return 'fmt';
  31871. case 'img':
  31872. return 'image';
  31873. case 'a':
  31874. return 'link';
  31875. default:
  31876. return 'other';
  31877. }
  31878. }
  31879. const _TRANSLATIONS_TAG = 'translationbundle';
  31880. const _TRANSLATION_TAG = 'translation';
  31881. const _PLACEHOLDER_TAG = 'ph';
  31882. class Xtb extends Serializer {
  31883. write(messages, locale) {
  31884. throw new Error('Unsupported');
  31885. }
  31886. load(content, url) {
  31887. // xtb to xml nodes
  31888. const xtbParser = new XtbParser();
  31889. const { locale, msgIdToHtml, errors } = xtbParser.parse(content, url);
  31890. // xml nodes to i18n nodes
  31891. const i18nNodesByMsgId = {};
  31892. const converter = new XmlToI18n();
  31893. // Because we should be able to load xtb files that rely on features not supported by angular,
  31894. // we need to delay the conversion of html to i18n nodes so that non angular messages are not
  31895. // converted
  31896. Object.keys(msgIdToHtml).forEach((msgId) => {
  31897. const valueFn = function () {
  31898. const { i18nNodes, errors } = converter.convert(msgIdToHtml[msgId], url);
  31899. if (errors.length) {
  31900. throw new Error(`xtb parse errors:\n${errors.join('\n')}`);
  31901. }
  31902. return i18nNodes;
  31903. };
  31904. createLazyProperty(i18nNodesByMsgId, msgId, valueFn);
  31905. });
  31906. if (errors.length) {
  31907. throw new Error(`xtb parse errors:\n${errors.join('\n')}`);
  31908. }
  31909. return { locale: locale, i18nNodesByMsgId };
  31910. }
  31911. digest(message) {
  31912. return digest(message);
  31913. }
  31914. createNameMapper(message) {
  31915. return new SimplePlaceholderMapper(message, toPublicName);
  31916. }
  31917. }
  31918. function createLazyProperty(messages, id, valueFn) {
  31919. Object.defineProperty(messages, id, {
  31920. configurable: true,
  31921. enumerable: true,
  31922. get: function () {
  31923. const value = valueFn();
  31924. Object.defineProperty(messages, id, { enumerable: true, value });
  31925. return value;
  31926. },
  31927. set: (_) => {
  31928. throw new Error('Could not overwrite an XTB translation');
  31929. },
  31930. });
  31931. }
  31932. // Extract messages as xml nodes from the xtb file
  31933. class XtbParser {
  31934. // using non-null assertions because they're (re)set by parse()
  31935. _bundleDepth;
  31936. _errors;
  31937. _msgIdToHtml;
  31938. _locale = null;
  31939. parse(xtb, url) {
  31940. this._bundleDepth = 0;
  31941. this._msgIdToHtml = {};
  31942. // We can not parse the ICU messages at this point as some messages might not originate
  31943. // from Angular that could not be lex'd.
  31944. const xml = new XmlParser().parse(xtb, url);
  31945. this._errors = xml.errors;
  31946. visitAll(this, xml.rootNodes);
  31947. return {
  31948. msgIdToHtml: this._msgIdToHtml,
  31949. errors: this._errors,
  31950. locale: this._locale,
  31951. };
  31952. }
  31953. visitElement(element, context) {
  31954. switch (element.name) {
  31955. case _TRANSLATIONS_TAG:
  31956. this._bundleDepth++;
  31957. if (this._bundleDepth > 1) {
  31958. this._addError(element, `<${_TRANSLATIONS_TAG}> elements can not be nested`);
  31959. }
  31960. const langAttr = element.attrs.find((attr) => attr.name === 'lang');
  31961. if (langAttr) {
  31962. this._locale = langAttr.value;
  31963. }
  31964. visitAll(this, element.children, null);
  31965. this._bundleDepth--;
  31966. break;
  31967. case _TRANSLATION_TAG:
  31968. const idAttr = element.attrs.find((attr) => attr.name === 'id');
  31969. if (!idAttr) {
  31970. this._addError(element, `<${_TRANSLATION_TAG}> misses the "id" attribute`);
  31971. }
  31972. else {
  31973. const id = idAttr.value;
  31974. if (this._msgIdToHtml.hasOwnProperty(id)) {
  31975. this._addError(element, `Duplicated translations for msg ${id}`);
  31976. }
  31977. else {
  31978. const innerTextStart = element.startSourceSpan.end.offset;
  31979. const innerTextEnd = element.endSourceSpan.start.offset;
  31980. const content = element.startSourceSpan.start.file.content;
  31981. const innerText = content.slice(innerTextStart, innerTextEnd);
  31982. this._msgIdToHtml[id] = innerText;
  31983. }
  31984. }
  31985. break;
  31986. default:
  31987. this._addError(element, 'Unexpected tag');
  31988. }
  31989. }
  31990. visitAttribute(attribute, context) { }
  31991. visitText(text, context) { }
  31992. visitComment(comment, context) { }
  31993. visitExpansion(expansion, context) { }
  31994. visitExpansionCase(expansionCase, context) { }
  31995. visitBlock(block, context) { }
  31996. visitBlockParameter(block, context) { }
  31997. visitLetDeclaration(decl, context) { }
  31998. _addError(node, message) {
  31999. this._errors.push(new I18nError(node.sourceSpan, message));
  32000. }
  32001. }
  32002. // Convert ml nodes (xtb syntax) to i18n nodes
  32003. class XmlToI18n {
  32004. // using non-null assertion because it's (re)set by convert()
  32005. _errors;
  32006. convert(message, url) {
  32007. const xmlIcu = new XmlParser().parse(message, url, { tokenizeExpansionForms: true });
  32008. this._errors = xmlIcu.errors;
  32009. const i18nNodes = this._errors.length > 0 || xmlIcu.rootNodes.length == 0
  32010. ? []
  32011. : visitAll(this, xmlIcu.rootNodes);
  32012. return {
  32013. i18nNodes,
  32014. errors: this._errors,
  32015. };
  32016. }
  32017. visitText(text, context) {
  32018. return new Text$2(text.value, text.sourceSpan);
  32019. }
  32020. visitExpansion(icu, context) {
  32021. const caseMap = {};
  32022. visitAll(this, icu.cases).forEach((c) => {
  32023. caseMap[c.value] = new Container(c.nodes, icu.sourceSpan);
  32024. });
  32025. return new Icu(icu.switchValue, icu.type, caseMap, icu.sourceSpan);
  32026. }
  32027. visitExpansionCase(icuCase, context) {
  32028. return {
  32029. value: icuCase.value,
  32030. nodes: visitAll(this, icuCase.expression),
  32031. };
  32032. }
  32033. visitElement(el, context) {
  32034. if (el.name === _PLACEHOLDER_TAG) {
  32035. const nameAttr = el.attrs.find((attr) => attr.name === 'name');
  32036. if (nameAttr) {
  32037. return new Placeholder('', nameAttr.value, el.sourceSpan);
  32038. }
  32039. this._addError(el, `<${_PLACEHOLDER_TAG}> misses the "name" attribute`);
  32040. }
  32041. else {
  32042. this._addError(el, `Unexpected tag`);
  32043. }
  32044. return null;
  32045. }
  32046. visitComment(comment, context) { }
  32047. visitAttribute(attribute, context) { }
  32048. visitBlock(block, context) { }
  32049. visitBlockParameter(block, context) { }
  32050. visitLetDeclaration(decl, context) { }
  32051. _addError(node, message) {
  32052. this._errors.push(new I18nError(node.sourceSpan, message));
  32053. }
  32054. }
  32055. /**
  32056. * A container for translated messages
  32057. */
  32058. class TranslationBundle {
  32059. _i18nNodesByMsgId;
  32060. digest;
  32061. mapperFactory;
  32062. _i18nToHtml;
  32063. constructor(_i18nNodesByMsgId = {}, locale, digest, mapperFactory, missingTranslationStrategy = MissingTranslationStrategy.Warning, console) {
  32064. this._i18nNodesByMsgId = _i18nNodesByMsgId;
  32065. this.digest = digest;
  32066. this.mapperFactory = mapperFactory;
  32067. this._i18nToHtml = new I18nToHtmlVisitor(_i18nNodesByMsgId, locale, digest, mapperFactory, missingTranslationStrategy, console);
  32068. }
  32069. // Creates a `TranslationBundle` by parsing the given `content` with the `serializer`.
  32070. static load(content, url, serializer, missingTranslationStrategy, console) {
  32071. const { locale, i18nNodesByMsgId } = serializer.load(content, url);
  32072. const digestFn = (m) => serializer.digest(m);
  32073. const mapperFactory = (m) => serializer.createNameMapper(m);
  32074. return new TranslationBundle(i18nNodesByMsgId, locale, digestFn, mapperFactory, missingTranslationStrategy, console);
  32075. }
  32076. // Returns the translation as HTML nodes from the given source message.
  32077. get(srcMsg) {
  32078. const html = this._i18nToHtml.convert(srcMsg);
  32079. if (html.errors.length) {
  32080. throw new Error(html.errors.join('\n'));
  32081. }
  32082. return html.nodes;
  32083. }
  32084. has(srcMsg) {
  32085. return this.digest(srcMsg) in this._i18nNodesByMsgId;
  32086. }
  32087. }
  32088. class I18nToHtmlVisitor {
  32089. _i18nNodesByMsgId;
  32090. _locale;
  32091. _digest;
  32092. _mapperFactory;
  32093. _missingTranslationStrategy;
  32094. _console;
  32095. // using non-null assertions because they're (re)set by convert()
  32096. _srcMsg;
  32097. _errors = [];
  32098. _contextStack = [];
  32099. _mapper;
  32100. constructor(_i18nNodesByMsgId = {}, _locale, _digest, _mapperFactory, _missingTranslationStrategy, _console) {
  32101. this._i18nNodesByMsgId = _i18nNodesByMsgId;
  32102. this._locale = _locale;
  32103. this._digest = _digest;
  32104. this._mapperFactory = _mapperFactory;
  32105. this._missingTranslationStrategy = _missingTranslationStrategy;
  32106. this._console = _console;
  32107. }
  32108. convert(srcMsg) {
  32109. this._contextStack.length = 0;
  32110. this._errors.length = 0;
  32111. // i18n to text
  32112. const text = this._convertToText(srcMsg);
  32113. // text to html
  32114. const url = srcMsg.nodes[0].sourceSpan.start.file.url;
  32115. const html = new HtmlParser().parse(text, url, { tokenizeExpansionForms: true });
  32116. return {
  32117. nodes: html.rootNodes,
  32118. errors: [...this._errors, ...html.errors],
  32119. };
  32120. }
  32121. visitText(text, context) {
  32122. // `convert()` uses an `HtmlParser` to return `html.Node`s
  32123. // we should then make sure that any special characters are escaped
  32124. return escapeXml(text.value);
  32125. }
  32126. visitContainer(container, context) {
  32127. return container.children.map((n) => n.visit(this)).join('');
  32128. }
  32129. visitIcu(icu, context) {
  32130. const cases = Object.keys(icu.cases).map((k) => `${k} {${icu.cases[k].visit(this)}}`);
  32131. // TODO(vicb): Once all format switch to using expression placeholders
  32132. // we should throw when the placeholder is not in the source message
  32133. const exp = this._srcMsg.placeholders.hasOwnProperty(icu.expression)
  32134. ? this._srcMsg.placeholders[icu.expression].text
  32135. : icu.expression;
  32136. return `{${exp}, ${icu.type}, ${cases.join(' ')}}`;
  32137. }
  32138. visitPlaceholder(ph, context) {
  32139. const phName = this._mapper(ph.name);
  32140. if (this._srcMsg.placeholders.hasOwnProperty(phName)) {
  32141. return this._srcMsg.placeholders[phName].text;
  32142. }
  32143. if (this._srcMsg.placeholderToMessage.hasOwnProperty(phName)) {
  32144. return this._convertToText(this._srcMsg.placeholderToMessage[phName]);
  32145. }
  32146. this._addError(ph, `Unknown placeholder "${ph.name}"`);
  32147. return '';
  32148. }
  32149. // Loaded message contains only placeholders (vs tag and icu placeholders).
  32150. // However when a translation can not be found, we need to serialize the source message
  32151. // which can contain tag placeholders
  32152. visitTagPlaceholder(ph, context) {
  32153. const tag = `${ph.tag}`;
  32154. const attrs = Object.keys(ph.attrs)
  32155. .map((name) => `${name}="${ph.attrs[name]}"`)
  32156. .join(' ');
  32157. if (ph.isVoid) {
  32158. return `<${tag} ${attrs}/>`;
  32159. }
  32160. const children = ph.children.map((c) => c.visit(this)).join('');
  32161. return `<${tag} ${attrs}>${children}</${tag}>`;
  32162. }
  32163. // Loaded message contains only placeholders (vs tag and icu placeholders).
  32164. // However when a translation can not be found, we need to serialize the source message
  32165. // which can contain tag placeholders
  32166. visitIcuPlaceholder(ph, context) {
  32167. // An ICU placeholder references the source message to be serialized
  32168. return this._convertToText(this._srcMsg.placeholderToMessage[ph.name]);
  32169. }
  32170. visitBlockPlaceholder(ph, context) {
  32171. const params = ph.parameters.length === 0 ? '' : ` (${ph.parameters.join('; ')})`;
  32172. const children = ph.children.map((c) => c.visit(this)).join('');
  32173. return `@${ph.name}${params} {${children}}`;
  32174. }
  32175. /**
  32176. * Convert a source message to a translated text string:
  32177. * - text nodes are replaced with their translation,
  32178. * - placeholders are replaced with their content,
  32179. * - ICU nodes are converted to ICU expressions.
  32180. */
  32181. _convertToText(srcMsg) {
  32182. const id = this._digest(srcMsg);
  32183. const mapper = this._mapperFactory ? this._mapperFactory(srcMsg) : null;
  32184. let nodes;
  32185. this._contextStack.push({ msg: this._srcMsg, mapper: this._mapper });
  32186. this._srcMsg = srcMsg;
  32187. if (this._i18nNodesByMsgId.hasOwnProperty(id)) {
  32188. // When there is a translation use its nodes as the source
  32189. // And create a mapper to convert serialized placeholder names to internal names
  32190. nodes = this._i18nNodesByMsgId[id];
  32191. this._mapper = (name) => (mapper ? mapper.toInternalName(name) : name);
  32192. }
  32193. else {
  32194. // When no translation has been found
  32195. // - report an error / a warning / nothing,
  32196. // - use the nodes from the original message
  32197. // - placeholders are already internal and need no mapper
  32198. if (this._missingTranslationStrategy === MissingTranslationStrategy.Error) {
  32199. const ctx = this._locale ? ` for locale "${this._locale}"` : '';
  32200. this._addError(srcMsg.nodes[0], `Missing translation for message "${id}"${ctx}`);
  32201. }
  32202. else if (this._console &&
  32203. this._missingTranslationStrategy === MissingTranslationStrategy.Warning) {
  32204. const ctx = this._locale ? ` for locale "${this._locale}"` : '';
  32205. this._console.warn(`Missing translation for message "${id}"${ctx}`);
  32206. }
  32207. nodes = srcMsg.nodes;
  32208. this._mapper = (name) => name;
  32209. }
  32210. const text = nodes.map((node) => node.visit(this)).join('');
  32211. const context = this._contextStack.pop();
  32212. this._srcMsg = context.msg;
  32213. this._mapper = context.mapper;
  32214. return text;
  32215. }
  32216. _addError(el, msg) {
  32217. this._errors.push(new I18nError(el.sourceSpan, msg));
  32218. }
  32219. }
  32220. class I18NHtmlParser {
  32221. _htmlParser;
  32222. // @override
  32223. getTagDefinition;
  32224. _translationBundle;
  32225. constructor(_htmlParser, translations, translationsFormat, missingTranslation = MissingTranslationStrategy.Warning, console) {
  32226. this._htmlParser = _htmlParser;
  32227. if (translations) {
  32228. const serializer = createSerializer(translationsFormat);
  32229. this._translationBundle = TranslationBundle.load(translations, 'i18n', serializer, missingTranslation, console);
  32230. }
  32231. else {
  32232. this._translationBundle = new TranslationBundle({}, null, digest$1, undefined, missingTranslation, console);
  32233. }
  32234. }
  32235. parse(source, url, options = {}) {
  32236. const interpolationConfig = options.interpolationConfig || DEFAULT_INTERPOLATION_CONFIG;
  32237. const parseResult = this._htmlParser.parse(source, url, { interpolationConfig, ...options });
  32238. if (parseResult.errors.length) {
  32239. return new ParseTreeResult(parseResult.rootNodes, parseResult.errors);
  32240. }
  32241. return mergeTranslations(parseResult.rootNodes, this._translationBundle, interpolationConfig, [], {});
  32242. }
  32243. }
  32244. function createSerializer(format) {
  32245. format = (format || 'xlf').toLowerCase();
  32246. switch (format) {
  32247. case 'xmb':
  32248. return new Xmb();
  32249. case 'xtb':
  32250. return new Xtb();
  32251. case 'xliff2':
  32252. case 'xlf2':
  32253. return new Xliff2();
  32254. case 'xliff':
  32255. case 'xlf':
  32256. default:
  32257. return new Xliff();
  32258. }
  32259. }
  32260. /**
  32261. * A container for message extracted from the templates.
  32262. */
  32263. class MessageBundle {
  32264. _htmlParser;
  32265. _implicitTags;
  32266. _implicitAttrs;
  32267. _locale;
  32268. _preserveWhitespace;
  32269. _messages = [];
  32270. constructor(_htmlParser, _implicitTags, _implicitAttrs, _locale = null, _preserveWhitespace = true) {
  32271. this._htmlParser = _htmlParser;
  32272. this._implicitTags = _implicitTags;
  32273. this._implicitAttrs = _implicitAttrs;
  32274. this._locale = _locale;
  32275. this._preserveWhitespace = _preserveWhitespace;
  32276. }
  32277. updateFromTemplate(source, url, interpolationConfig) {
  32278. const htmlParserResult = this._htmlParser.parse(source, url, {
  32279. tokenizeExpansionForms: true,
  32280. interpolationConfig,
  32281. });
  32282. if (htmlParserResult.errors.length) {
  32283. return htmlParserResult.errors;
  32284. }
  32285. // Trim unnecessary whitespace from extracted messages if requested. This
  32286. // makes the messages more durable to trivial whitespace changes without
  32287. // affected message IDs.
  32288. const rootNodes = this._preserveWhitespace
  32289. ? htmlParserResult.rootNodes
  32290. : visitAllWithSiblings(new WhitespaceVisitor(/* preserveSignificantWhitespace */ false), htmlParserResult.rootNodes);
  32291. const i18nParserResult = extractMessages(rootNodes, interpolationConfig, this._implicitTags, this._implicitAttrs,
  32292. /* preserveSignificantWhitespace */ this._preserveWhitespace);
  32293. if (i18nParserResult.errors.length) {
  32294. return i18nParserResult.errors;
  32295. }
  32296. this._messages.push(...i18nParserResult.messages);
  32297. return [];
  32298. }
  32299. // Return the message in the internal format
  32300. // The public (serialized) format might be different, see the `write` method.
  32301. getMessages() {
  32302. return this._messages;
  32303. }
  32304. write(serializer, filterSources) {
  32305. const messages = {};
  32306. const mapperVisitor = new MapPlaceholderNames();
  32307. // Deduplicate messages based on their ID
  32308. this._messages.forEach((message) => {
  32309. const id = serializer.digest(message);
  32310. if (!messages.hasOwnProperty(id)) {
  32311. messages[id] = message;
  32312. }
  32313. else {
  32314. messages[id].sources.push(...message.sources);
  32315. }
  32316. });
  32317. // Transform placeholder names using the serializer mapping
  32318. const msgList = Object.keys(messages).map((id) => {
  32319. const mapper = serializer.createNameMapper(messages[id]);
  32320. const src = messages[id];
  32321. const nodes = mapper ? mapperVisitor.convert(src.nodes, mapper) : src.nodes;
  32322. let transformedMessage = new Message(nodes, {}, {}, src.meaning, src.description, id);
  32323. transformedMessage.sources = src.sources;
  32324. if (filterSources) {
  32325. transformedMessage.sources.forEach((source) => (source.filePath = filterSources(source.filePath)));
  32326. }
  32327. return transformedMessage;
  32328. });
  32329. return serializer.write(msgList, this._locale);
  32330. }
  32331. }
  32332. // Transform an i18n AST by renaming the placeholder nodes with the given mapper
  32333. class MapPlaceholderNames extends CloneVisitor {
  32334. convert(nodes, mapper) {
  32335. return mapper ? nodes.map((n) => n.visit(this, mapper)) : nodes;
  32336. }
  32337. visitTagPlaceholder(ph, mapper) {
  32338. const startName = mapper.toPublicName(ph.startName);
  32339. const closeName = ph.closeName ? mapper.toPublicName(ph.closeName) : ph.closeName;
  32340. const children = ph.children.map((n) => n.visit(this, mapper));
  32341. return new TagPlaceholder(ph.tag, ph.attrs, startName, closeName, children, ph.isVoid, ph.sourceSpan, ph.startSourceSpan, ph.endSourceSpan);
  32342. }
  32343. visitBlockPlaceholder(ph, mapper) {
  32344. const startName = mapper.toPublicName(ph.startName);
  32345. const closeName = ph.closeName ? mapper.toPublicName(ph.closeName) : ph.closeName;
  32346. const children = ph.children.map((n) => n.visit(this, mapper));
  32347. return new BlockPlaceholder(ph.name, ph.parameters, startName, closeName, children, ph.sourceSpan, ph.startSourceSpan, ph.endSourceSpan);
  32348. }
  32349. visitPlaceholder(ph, mapper) {
  32350. return new Placeholder(ph.value, mapper.toPublicName(ph.name), ph.sourceSpan);
  32351. }
  32352. visitIcuPlaceholder(ph, mapper) {
  32353. return new IcuPlaceholder(ph.value, mapper.toPublicName(ph.name), ph.sourceSpan);
  32354. }
  32355. }
  32356. var FactoryTarget;
  32357. (function (FactoryTarget) {
  32358. FactoryTarget[FactoryTarget["Directive"] = 0] = "Directive";
  32359. FactoryTarget[FactoryTarget["Component"] = 1] = "Component";
  32360. FactoryTarget[FactoryTarget["Injectable"] = 2] = "Injectable";
  32361. FactoryTarget[FactoryTarget["Pipe"] = 3] = "Pipe";
  32362. FactoryTarget[FactoryTarget["NgModule"] = 4] = "NgModule";
  32363. })(FactoryTarget || (FactoryTarget = {}));
  32364. function compileClassMetadata(metadata) {
  32365. const fnCall = internalCompileClassMetadata(metadata);
  32366. return arrowFn([], [devOnlyGuardedExpression(fnCall).toStmt()]).callFn([]);
  32367. }
  32368. /** Compiles only the `setClassMetadata` call without any additional wrappers. */
  32369. function internalCompileClassMetadata(metadata) {
  32370. return importExpr(Identifiers.setClassMetadata)
  32371. .callFn([
  32372. metadata.type,
  32373. metadata.decorators,
  32374. metadata.ctorParameters ?? literal(null),
  32375. metadata.propDecorators ?? literal(null),
  32376. ]);
  32377. }
  32378. /**
  32379. * Wraps the `setClassMetadata` function with extra logic that dynamically
  32380. * loads dependencies from `@defer` blocks.
  32381. *
  32382. * Generates a call like this:
  32383. * ```ts
  32384. * setClassMetadataAsync(type, () => [
  32385. * import('./cmp-a').then(m => m.CmpA);
  32386. * import('./cmp-b').then(m => m.CmpB);
  32387. * ], (CmpA, CmpB) => {
  32388. * setClassMetadata(type, decorators, ctorParameters, propParameters);
  32389. * });
  32390. * ```
  32391. *
  32392. * Similar to the `setClassMetadata` call, it's wrapped into the `ngDevMode`
  32393. * check to tree-shake away this code in production mode.
  32394. */
  32395. function compileComponentClassMetadata(metadata, dependencies) {
  32396. if (dependencies === null || dependencies.length === 0) {
  32397. // If there are no deferrable symbols - just generate a regular `setClassMetadata` call.
  32398. return compileClassMetadata(metadata);
  32399. }
  32400. return internalCompileSetClassMetadataAsync(metadata, dependencies.map((dep) => new FnParam(dep.symbolName, DYNAMIC_TYPE)), compileComponentMetadataAsyncResolver(dependencies));
  32401. }
  32402. /**
  32403. * Identical to `compileComponentClassMetadata`. Used for the cases where we're unable to
  32404. * analyze the deferred block dependencies, but we have a reference to the compiled
  32405. * dependency resolver function that we can use as is.
  32406. * @param metadata Class metadata for the internal `setClassMetadata` call.
  32407. * @param deferResolver Expression representing the deferred dependency loading function.
  32408. * @param deferredDependencyNames Names of the dependencies that are being loaded asynchronously.
  32409. */
  32410. function compileOpaqueAsyncClassMetadata(metadata, deferResolver, deferredDependencyNames) {
  32411. return internalCompileSetClassMetadataAsync(metadata, deferredDependencyNames.map((name) => new FnParam(name, DYNAMIC_TYPE)), deferResolver);
  32412. }
  32413. /**
  32414. * Internal logic used to compile a `setClassMetadataAsync` call.
  32415. * @param metadata Class metadata for the internal `setClassMetadata` call.
  32416. * @param wrapperParams Parameters to be set on the callback that wraps `setClassMetata`.
  32417. * @param dependencyResolverFn Function to resolve the deferred dependencies.
  32418. */
  32419. function internalCompileSetClassMetadataAsync(metadata, wrapperParams, dependencyResolverFn) {
  32420. // Omit the wrapper since it'll be added around `setClassMetadataAsync` instead.
  32421. const setClassMetadataCall = internalCompileClassMetadata(metadata);
  32422. const setClassMetaWrapper = arrowFn(wrapperParams, [setClassMetadataCall.toStmt()]);
  32423. const setClassMetaAsync = importExpr(Identifiers.setClassMetadataAsync)
  32424. .callFn([metadata.type, dependencyResolverFn, setClassMetaWrapper]);
  32425. return arrowFn([], [devOnlyGuardedExpression(setClassMetaAsync).toStmt()]).callFn([]);
  32426. }
  32427. /**
  32428. * Compiles the function that loads the dependencies for the
  32429. * entire component in `setClassMetadataAsync`.
  32430. */
  32431. function compileComponentMetadataAsyncResolver(dependencies) {
  32432. const dynamicImports = dependencies.map(({ symbolName, importPath, isDefaultImport }) => {
  32433. // e.g. `(m) => m.CmpA`
  32434. const innerFn =
  32435. // Default imports are always accessed through the `default` property.
  32436. arrowFn([new FnParam('m', DYNAMIC_TYPE)], variable('m').prop(isDefaultImport ? 'default' : symbolName));
  32437. // e.g. `import('./cmp-a').then(...)`
  32438. return new DynamicImportExpr(importPath).prop('then').callFn([innerFn]);
  32439. });
  32440. // e.g. `() => [ ... ];`
  32441. return arrowFn([], literalArr(dynamicImports));
  32442. }
  32443. /**
  32444. * Generate an ngDevMode guarded call to setClassDebugInfo with the debug info about the class
  32445. * (e.g., the file name in which the class is defined)
  32446. */
  32447. function compileClassDebugInfo(debugInfo) {
  32448. const debugInfoObject = {
  32449. className: debugInfo.className,
  32450. };
  32451. // Include file path and line number only if the file relative path is calculated successfully.
  32452. if (debugInfo.filePath) {
  32453. debugInfoObject.filePath = debugInfo.filePath;
  32454. debugInfoObject.lineNumber = debugInfo.lineNumber;
  32455. }
  32456. // Include forbidOrphanRendering only if it's set to true (to reduce generated code)
  32457. if (debugInfo.forbidOrphanRendering) {
  32458. debugInfoObject.forbidOrphanRendering = literal(true);
  32459. }
  32460. const fnCall = importExpr(Identifiers.setClassDebugInfo)
  32461. .callFn([debugInfo.type, mapLiteral(debugInfoObject)]);
  32462. const iife = arrowFn([], [devOnlyGuardedExpression(fnCall).toStmt()]);
  32463. return iife.callFn([]);
  32464. }
  32465. /*!
  32466. * @license
  32467. * Copyright Google LLC All Rights Reserved.
  32468. *
  32469. * Use of this source code is governed by an MIT-style license that can be
  32470. * found in the LICENSE file at https://angular.dev/license
  32471. */
  32472. /**
  32473. * Compiles the expression that initializes HMR for a class.
  32474. * @param meta HMR metadata extracted from the class.
  32475. */
  32476. function compileHmrInitializer(meta) {
  32477. const moduleName = 'm';
  32478. const dataName = 'd';
  32479. const timestampName = 't';
  32480. const idName = 'id';
  32481. const importCallbackName = `${meta.className}_HmrLoad`;
  32482. const namespaces = meta.namespaceDependencies.map((dep) => {
  32483. return new ExternalExpr({ moduleName: dep.moduleName, name: null });
  32484. });
  32485. // m.default
  32486. const defaultRead = variable(moduleName).prop('default');
  32487. // ɵɵreplaceMetadata(Comp, m.default, [...namespaces], [...locals], import.meta, id);
  32488. const replaceCall = importExpr(Identifiers.replaceMetadata)
  32489. .callFn([
  32490. meta.type,
  32491. defaultRead,
  32492. literalArr(namespaces),
  32493. literalArr(meta.localDependencies.map((l) => l.runtimeRepresentation)),
  32494. variable('import').prop('meta'),
  32495. variable(idName),
  32496. ]);
  32497. // (m) => m.default && ɵɵreplaceMetadata(...)
  32498. const replaceCallback = arrowFn([new FnParam(moduleName)], defaultRead.and(replaceCall));
  32499. // '<url>?c=' + id + '&t=' + encodeURIComponent(t)
  32500. const urlValue = literal(`./@ng/component?c=`)
  32501. .plus(variable(idName))
  32502. .plus(literal('&t='))
  32503. .plus(variable('encodeURIComponent').callFn([variable(timestampName)]));
  32504. // import.meta.url
  32505. const urlBase = variable('import').prop('meta').prop('url');
  32506. // new URL(urlValue, urlBase).href
  32507. const urlHref = new InstantiateExpr(variable('URL'), [urlValue, urlBase]).prop('href');
  32508. // function Cmp_HmrLoad(t) {
  32509. // import(/* @vite-ignore */ urlHref).then((m) => m.default && replaceMetadata(...));
  32510. // }
  32511. const importCallback = new DeclareFunctionStmt(importCallbackName, [new FnParam(timestampName)], [
  32512. // The vite-ignore special comment is required to prevent Vite from generating a superfluous
  32513. // warning for each usage within the development code. If Vite provides a method to
  32514. // programmatically avoid this warning in the future, this added comment can be removed here.
  32515. new DynamicImportExpr(urlHref, null, '@vite-ignore')
  32516. .prop('then')
  32517. .callFn([replaceCallback])
  32518. .toStmt(),
  32519. ], null, StmtModifier.Final);
  32520. // (d) => d.id === id && Cmp_HmrLoad(d.timestamp)
  32521. const updateCallback = arrowFn([new FnParam(dataName)], variable(dataName)
  32522. .prop('id')
  32523. .identical(variable(idName))
  32524. .and(variable(importCallbackName).callFn([variable(dataName).prop('timestamp')])));
  32525. // Cmp_HmrLoad(Date.now());
  32526. // Initial call to kick off the loading in order to avoid edge cases with components
  32527. // coming from lazy chunks that change before the chunk has loaded.
  32528. const initialCall = variable(importCallbackName)
  32529. .callFn([variable('Date').prop('now').callFn([])]);
  32530. // import.meta.hot
  32531. const hotRead = variable('import').prop('meta').prop('hot');
  32532. // import.meta.hot.on('angular:component-update', () => ...);
  32533. const hotListener = hotRead
  32534. .clone()
  32535. .prop('on')
  32536. .callFn([literal('angular:component-update'), updateCallback]);
  32537. return arrowFn([], [
  32538. // const id = <id>;
  32539. new DeclareVarStmt(idName, literal(encodeURIComponent(`${meta.filePath}@${meta.className}`)), null, StmtModifier.Final),
  32540. // function Cmp_HmrLoad() {...}.
  32541. importCallback,
  32542. // ngDevMode && Cmp_HmrLoad(Date.now());
  32543. devOnlyGuardedExpression(initialCall).toStmt(),
  32544. // ngDevMode && import.meta.hot && import.meta.hot.on(...)
  32545. devOnlyGuardedExpression(hotRead.and(hotListener)).toStmt(),
  32546. ])
  32547. .callFn([]);
  32548. }
  32549. /**
  32550. * Compiles the HMR update callback for a class.
  32551. * @param definitions Compiled definitions for the class (e.g. `defineComponent` calls).
  32552. * @param constantStatements Supporting constants statements that were generated alongside
  32553. * the definition.
  32554. * @param meta HMR metadata extracted from the class.
  32555. */
  32556. function compileHmrUpdateCallback(definitions, constantStatements, meta) {
  32557. const namespaces = 'ɵɵnamespaces';
  32558. const params = [meta.className, namespaces].map((name) => new FnParam(name, DYNAMIC_TYPE));
  32559. const body = [];
  32560. for (const local of meta.localDependencies) {
  32561. params.push(new FnParam(local.name));
  32562. }
  32563. // Declare variables that read out the individual namespaces.
  32564. for (let i = 0; i < meta.namespaceDependencies.length; i++) {
  32565. body.push(new DeclareVarStmt(meta.namespaceDependencies[i].assignedName, variable(namespaces).key(literal(i)), DYNAMIC_TYPE, StmtModifier.Final));
  32566. }
  32567. body.push(...constantStatements);
  32568. for (const field of definitions) {
  32569. if (field.initializer !== null) {
  32570. body.push(variable(meta.className).prop(field.name).set(field.initializer).toStmt());
  32571. for (const stmt of field.statements) {
  32572. body.push(stmt);
  32573. }
  32574. }
  32575. }
  32576. return new DeclareFunctionStmt(`${meta.className}_UpdateMetadata`, params, body, null, StmtModifier.Final);
  32577. }
  32578. /**
  32579. * Every time we make a breaking change to the declaration interface or partial-linker behavior, we
  32580. * must update this constant to prevent old partial-linkers from incorrectly processing the
  32581. * declaration.
  32582. *
  32583. * Do not include any prerelease in these versions as they are ignored.
  32584. */
  32585. const MINIMUM_PARTIAL_LINKER_VERSION$5 = '12.0.0';
  32586. /**
  32587. * Minimum version at which deferred blocks are supported in the linker.
  32588. */
  32589. const MINIMUM_PARTIAL_LINKER_DEFER_SUPPORT_VERSION = '18.0.0';
  32590. function compileDeclareClassMetadata(metadata) {
  32591. const definitionMap = new DefinitionMap();
  32592. definitionMap.set('minVersion', literal(MINIMUM_PARTIAL_LINKER_VERSION$5));
  32593. definitionMap.set('version', literal('19.2.4'));
  32594. definitionMap.set('ngImport', importExpr(Identifiers.core));
  32595. definitionMap.set('type', metadata.type);
  32596. definitionMap.set('decorators', metadata.decorators);
  32597. definitionMap.set('ctorParameters', metadata.ctorParameters);
  32598. definitionMap.set('propDecorators', metadata.propDecorators);
  32599. return importExpr(Identifiers.declareClassMetadata).callFn([definitionMap.toLiteralMap()]);
  32600. }
  32601. function compileComponentDeclareClassMetadata(metadata, dependencies) {
  32602. if (dependencies === null || dependencies.length === 0) {
  32603. return compileDeclareClassMetadata(metadata);
  32604. }
  32605. const definitionMap = new DefinitionMap();
  32606. const callbackReturnDefinitionMap = new DefinitionMap();
  32607. callbackReturnDefinitionMap.set('decorators', metadata.decorators);
  32608. callbackReturnDefinitionMap.set('ctorParameters', metadata.ctorParameters ?? literal(null));
  32609. callbackReturnDefinitionMap.set('propDecorators', metadata.propDecorators ?? literal(null));
  32610. definitionMap.set('minVersion', literal(MINIMUM_PARTIAL_LINKER_DEFER_SUPPORT_VERSION));
  32611. definitionMap.set('version', literal('19.2.4'));
  32612. definitionMap.set('ngImport', importExpr(Identifiers.core));
  32613. definitionMap.set('type', metadata.type);
  32614. definitionMap.set('resolveDeferredDeps', compileComponentMetadataAsyncResolver(dependencies));
  32615. definitionMap.set('resolveMetadata', arrowFn(dependencies.map((dep) => new FnParam(dep.symbolName, DYNAMIC_TYPE)), callbackReturnDefinitionMap.toLiteralMap()));
  32616. return importExpr(Identifiers.declareClassMetadataAsync).callFn([definitionMap.toLiteralMap()]);
  32617. }
  32618. /**
  32619. * Creates an array literal expression from the given array, mapping all values to an expression
  32620. * using the provided mapping function. If the array is empty or null, then null is returned.
  32621. *
  32622. * @param values The array to transfer into literal array expression.
  32623. * @param mapper The logic to use for creating an expression for the array's values.
  32624. * @returns An array literal expression representing `values`, or null if `values` is empty or
  32625. * is itself null.
  32626. */
  32627. function toOptionalLiteralArray(values, mapper) {
  32628. if (values === null || values.length === 0) {
  32629. return null;
  32630. }
  32631. return literalArr(values.map((value) => mapper(value)));
  32632. }
  32633. /**
  32634. * Creates an object literal expression from the given object, mapping all values to an expression
  32635. * using the provided mapping function. If the object has no keys, then null is returned.
  32636. *
  32637. * @param object The object to transfer into an object literal expression.
  32638. * @param mapper The logic to use for creating an expression for the object's values.
  32639. * @returns An object literal expression representing `object`, or null if `object` does not have
  32640. * any keys.
  32641. */
  32642. function toOptionalLiteralMap(object, mapper) {
  32643. const entries = Object.keys(object).map((key) => {
  32644. const value = object[key];
  32645. return { key, value: mapper(value), quoted: true };
  32646. });
  32647. if (entries.length > 0) {
  32648. return literalMap(entries);
  32649. }
  32650. else {
  32651. return null;
  32652. }
  32653. }
  32654. function compileDependencies(deps) {
  32655. if (deps === 'invalid') {
  32656. // The `deps` can be set to the string "invalid" by the `unwrapConstructorDependencies()`
  32657. // function, which tries to convert `ConstructorDeps` into `R3DependencyMetadata[]`.
  32658. return literal('invalid');
  32659. }
  32660. else if (deps === null) {
  32661. return literal(null);
  32662. }
  32663. else {
  32664. return literalArr(deps.map(compileDependency));
  32665. }
  32666. }
  32667. function compileDependency(dep) {
  32668. const depMeta = new DefinitionMap();
  32669. depMeta.set('token', dep.token);
  32670. if (dep.attributeNameType !== null) {
  32671. depMeta.set('attribute', literal(true));
  32672. }
  32673. if (dep.host) {
  32674. depMeta.set('host', literal(true));
  32675. }
  32676. if (dep.optional) {
  32677. depMeta.set('optional', literal(true));
  32678. }
  32679. if (dep.self) {
  32680. depMeta.set('self', literal(true));
  32681. }
  32682. if (dep.skipSelf) {
  32683. depMeta.set('skipSelf', literal(true));
  32684. }
  32685. return depMeta.toLiteralMap();
  32686. }
  32687. /**
  32688. * Compile a directive declaration defined by the `R3DirectiveMetadata`.
  32689. */
  32690. function compileDeclareDirectiveFromMetadata(meta) {
  32691. const definitionMap = createDirectiveDefinitionMap(meta);
  32692. const expression = importExpr(Identifiers.declareDirective).callFn([definitionMap.toLiteralMap()]);
  32693. const type = createDirectiveType(meta);
  32694. return { expression, type, statements: [] };
  32695. }
  32696. /**
  32697. * Gathers the declaration fields for a directive into a `DefinitionMap`. This allows for reusing
  32698. * this logic for components, as they extend the directive metadata.
  32699. */
  32700. function createDirectiveDefinitionMap(meta) {
  32701. const definitionMap = new DefinitionMap();
  32702. const minVersion = getMinimumVersionForPartialOutput(meta);
  32703. definitionMap.set('minVersion', literal(minVersion));
  32704. definitionMap.set('version', literal('19.2.4'));
  32705. // e.g. `type: MyDirective`
  32706. definitionMap.set('type', meta.type.value);
  32707. if (meta.isStandalone !== undefined) {
  32708. definitionMap.set('isStandalone', literal(meta.isStandalone));
  32709. }
  32710. if (meta.isSignal) {
  32711. definitionMap.set('isSignal', literal(meta.isSignal));
  32712. }
  32713. // e.g. `selector: 'some-dir'`
  32714. if (meta.selector !== null) {
  32715. definitionMap.set('selector', literal(meta.selector));
  32716. }
  32717. definitionMap.set('inputs', needsNewInputPartialOutput(meta)
  32718. ? createInputsPartialMetadata(meta.inputs)
  32719. : legacyInputsPartialMetadata(meta.inputs));
  32720. definitionMap.set('outputs', conditionallyCreateDirectiveBindingLiteral(meta.outputs));
  32721. definitionMap.set('host', compileHostMetadata(meta.host));
  32722. definitionMap.set('providers', meta.providers);
  32723. if (meta.queries.length > 0) {
  32724. definitionMap.set('queries', literalArr(meta.queries.map(compileQuery)));
  32725. }
  32726. if (meta.viewQueries.length > 0) {
  32727. definitionMap.set('viewQueries', literalArr(meta.viewQueries.map(compileQuery)));
  32728. }
  32729. if (meta.exportAs !== null) {
  32730. definitionMap.set('exportAs', asLiteral(meta.exportAs));
  32731. }
  32732. if (meta.usesInheritance) {
  32733. definitionMap.set('usesInheritance', literal(true));
  32734. }
  32735. if (meta.lifecycle.usesOnChanges) {
  32736. definitionMap.set('usesOnChanges', literal(true));
  32737. }
  32738. if (meta.hostDirectives?.length) {
  32739. definitionMap.set('hostDirectives', createHostDirectives(meta.hostDirectives));
  32740. }
  32741. definitionMap.set('ngImport', importExpr(Identifiers.core));
  32742. return definitionMap;
  32743. }
  32744. /**
  32745. * Determines the minimum linker version for the partial output
  32746. * generated for this directive.
  32747. *
  32748. * Every time we make a breaking change to the declaration interface or partial-linker
  32749. * behavior, we must update the minimum versions to prevent old partial-linkers from
  32750. * incorrectly processing the declaration.
  32751. *
  32752. * NOTE: Do not include any prerelease in these versions as they are ignored.
  32753. */
  32754. function getMinimumVersionForPartialOutput(meta) {
  32755. // We are starting with the oldest minimum version that can work for common
  32756. // directive partial compilation output. As we discover usages of new features
  32757. // that require a newer partial output emit, we bump the `minVersion`. Our goal
  32758. // is to keep libraries as much compatible with older linker versions as possible.
  32759. let minVersion = '14.0.0';
  32760. // Note: in order to allow consuming Angular libraries that have been compiled with 16.1+ in
  32761. // Angular 16.0, we only force a minimum version of 16.1 if input transform feature as introduced
  32762. // in 16.1 is actually used.
  32763. const hasDecoratorTransformFunctions = Object.values(meta.inputs).some((input) => input.transformFunction !== null);
  32764. if (hasDecoratorTransformFunctions) {
  32765. minVersion = '16.1.0';
  32766. }
  32767. // If there are input flags and we need the new emit, use the actual minimum version,
  32768. // where this was introduced. i.e. in 17.1.0
  32769. // TODO(legacy-partial-output-inputs): Remove in v18.
  32770. if (needsNewInputPartialOutput(meta)) {
  32771. minVersion = '17.1.0';
  32772. }
  32773. // If there are signal-based queries, partial output generates an extra field
  32774. // that should be parsed by linkers. Ensure a proper minimum linker version.
  32775. if (meta.queries.some((q) => q.isSignal) || meta.viewQueries.some((q) => q.isSignal)) {
  32776. minVersion = '17.2.0';
  32777. }
  32778. return minVersion;
  32779. }
  32780. /**
  32781. * Gets whether the given directive needs the new input partial output structure
  32782. * that can hold additional metadata like `isRequired`, `isSignal` etc.
  32783. */
  32784. function needsNewInputPartialOutput(meta) {
  32785. return Object.values(meta.inputs).some((input) => input.isSignal);
  32786. }
  32787. /**
  32788. * Compiles the metadata of a single query into its partial declaration form as declared
  32789. * by `R3DeclareQueryMetadata`.
  32790. */
  32791. function compileQuery(query) {
  32792. const meta = new DefinitionMap();
  32793. meta.set('propertyName', literal(query.propertyName));
  32794. if (query.first) {
  32795. meta.set('first', literal(true));
  32796. }
  32797. meta.set('predicate', Array.isArray(query.predicate)
  32798. ? asLiteral(query.predicate)
  32799. : convertFromMaybeForwardRefExpression(query.predicate));
  32800. if (!query.emitDistinctChangesOnly) {
  32801. // `emitDistinctChangesOnly` is special because we expect it to be `true`.
  32802. // Therefore we explicitly emit the field, and explicitly place it only when it's `false`.
  32803. meta.set('emitDistinctChangesOnly', literal(false));
  32804. }
  32805. if (query.descendants) {
  32806. meta.set('descendants', literal(true));
  32807. }
  32808. meta.set('read', query.read);
  32809. if (query.static) {
  32810. meta.set('static', literal(true));
  32811. }
  32812. if (query.isSignal) {
  32813. meta.set('isSignal', literal(true));
  32814. }
  32815. return meta.toLiteralMap();
  32816. }
  32817. /**
  32818. * Compiles the host metadata into its partial declaration form as declared
  32819. * in `R3DeclareDirectiveMetadata['host']`
  32820. */
  32821. function compileHostMetadata(meta) {
  32822. const hostMetadata = new DefinitionMap();
  32823. hostMetadata.set('attributes', toOptionalLiteralMap(meta.attributes, (expression) => expression));
  32824. hostMetadata.set('listeners', toOptionalLiteralMap(meta.listeners, literal));
  32825. hostMetadata.set('properties', toOptionalLiteralMap(meta.properties, literal));
  32826. if (meta.specialAttributes.styleAttr) {
  32827. hostMetadata.set('styleAttribute', literal(meta.specialAttributes.styleAttr));
  32828. }
  32829. if (meta.specialAttributes.classAttr) {
  32830. hostMetadata.set('classAttribute', literal(meta.specialAttributes.classAttr));
  32831. }
  32832. if (hostMetadata.values.length > 0) {
  32833. return hostMetadata.toLiteralMap();
  32834. }
  32835. else {
  32836. return null;
  32837. }
  32838. }
  32839. function createHostDirectives(hostDirectives) {
  32840. const expressions = hostDirectives.map((current) => {
  32841. const keys = [
  32842. {
  32843. key: 'directive',
  32844. value: current.isForwardReference
  32845. ? generateForwardRef(current.directive.type)
  32846. : current.directive.type,
  32847. quoted: false,
  32848. },
  32849. ];
  32850. const inputsLiteral = current.inputs ? createHostDirectivesMappingArray(current.inputs) : null;
  32851. const outputsLiteral = current.outputs
  32852. ? createHostDirectivesMappingArray(current.outputs)
  32853. : null;
  32854. if (inputsLiteral) {
  32855. keys.push({ key: 'inputs', value: inputsLiteral, quoted: false });
  32856. }
  32857. if (outputsLiteral) {
  32858. keys.push({ key: 'outputs', value: outputsLiteral, quoted: false });
  32859. }
  32860. return literalMap(keys);
  32861. });
  32862. // If there's a forward reference, we generate a `function() { return [{directive: HostDir}] }`,
  32863. // otherwise we can save some bytes by using a plain array, e.g. `[{directive: HostDir}]`.
  32864. return literalArr(expressions);
  32865. }
  32866. /**
  32867. * Generates partial output metadata for inputs of a directive.
  32868. *
  32869. * The generated structure is expected to match `R3DeclareDirectiveFacade['inputs']`.
  32870. */
  32871. function createInputsPartialMetadata(inputs) {
  32872. const keys = Object.getOwnPropertyNames(inputs);
  32873. if (keys.length === 0) {
  32874. return null;
  32875. }
  32876. return literalMap(keys.map((declaredName) => {
  32877. const value = inputs[declaredName];
  32878. return {
  32879. key: declaredName,
  32880. // put quotes around keys that contain potentially unsafe characters
  32881. quoted: UNSAFE_OBJECT_KEY_NAME_REGEXP.test(declaredName),
  32882. value: literalMap([
  32883. { key: 'classPropertyName', quoted: false, value: asLiteral(value.classPropertyName) },
  32884. { key: 'publicName', quoted: false, value: asLiteral(value.bindingPropertyName) },
  32885. { key: 'isSignal', quoted: false, value: asLiteral(value.isSignal) },
  32886. { key: 'isRequired', quoted: false, value: asLiteral(value.required) },
  32887. { key: 'transformFunction', quoted: false, value: value.transformFunction ?? NULL_EXPR },
  32888. ]),
  32889. };
  32890. }));
  32891. }
  32892. /**
  32893. * Pre v18 legacy partial output for inputs.
  32894. *
  32895. * Previously, inputs did not capture metadata like `isSignal` in the partial compilation output.
  32896. * To enable capturing such metadata, we restructured how input metadata is communicated in the
  32897. * partial output. This would make libraries incompatible with older Angular FW versions where the
  32898. * linker would not know how to handle this new "format". For this reason, if we know this metadata
  32899. * does not need to be captured- we fall back to the old format. This is what this function
  32900. * generates.
  32901. *
  32902. * See:
  32903. * https://github.com/angular/angular/blob/d4b423690210872b5c32a322a6090beda30b05a3/packages/core/src/compiler/compiler_facade_interface.ts#L197-L199
  32904. */
  32905. function legacyInputsPartialMetadata(inputs) {
  32906. // TODO(legacy-partial-output-inputs): Remove function in v18.
  32907. const keys = Object.getOwnPropertyNames(inputs);
  32908. if (keys.length === 0) {
  32909. return null;
  32910. }
  32911. return literalMap(keys.map((declaredName) => {
  32912. const value = inputs[declaredName];
  32913. const publicName = value.bindingPropertyName;
  32914. const differentDeclaringName = publicName !== declaredName;
  32915. let result;
  32916. if (differentDeclaringName || value.transformFunction !== null) {
  32917. const values = [asLiteral(publicName), asLiteral(declaredName)];
  32918. if (value.transformFunction !== null) {
  32919. values.push(value.transformFunction);
  32920. }
  32921. result = literalArr(values);
  32922. }
  32923. else {
  32924. result = asLiteral(publicName);
  32925. }
  32926. return {
  32927. key: declaredName,
  32928. // put quotes around keys that contain potentially unsafe characters
  32929. quoted: UNSAFE_OBJECT_KEY_NAME_REGEXP.test(declaredName),
  32930. value: result,
  32931. };
  32932. }));
  32933. }
  32934. /**
  32935. * Compile a component declaration defined by the `R3ComponentMetadata`.
  32936. */
  32937. function compileDeclareComponentFromMetadata(meta, template, additionalTemplateInfo) {
  32938. const definitionMap = createComponentDefinitionMap(meta, template, additionalTemplateInfo);
  32939. const expression = importExpr(Identifiers.declareComponent).callFn([definitionMap.toLiteralMap()]);
  32940. const type = createComponentType(meta);
  32941. return { expression, type, statements: [] };
  32942. }
  32943. /**
  32944. * Gathers the declaration fields for a component into a `DefinitionMap`.
  32945. */
  32946. function createComponentDefinitionMap(meta, template, templateInfo) {
  32947. const definitionMap = createDirectiveDefinitionMap(meta);
  32948. const blockVisitor = new BlockPresenceVisitor();
  32949. visitAll$1(blockVisitor, template.nodes);
  32950. definitionMap.set('template', getTemplateExpression(template, templateInfo));
  32951. if (templateInfo.isInline) {
  32952. definitionMap.set('isInline', literal(true));
  32953. }
  32954. // Set the minVersion to 17.0.0 if the component is using at least one block in its template.
  32955. // We don't do this for templates without blocks, in order to preserve backwards compatibility.
  32956. if (blockVisitor.hasBlocks) {
  32957. definitionMap.set('minVersion', literal('17.0.0'));
  32958. }
  32959. definitionMap.set('styles', toOptionalLiteralArray(meta.styles, literal));
  32960. definitionMap.set('dependencies', compileUsedDependenciesMetadata(meta));
  32961. definitionMap.set('viewProviders', meta.viewProviders);
  32962. definitionMap.set('animations', meta.animations);
  32963. if (meta.changeDetection !== null) {
  32964. if (typeof meta.changeDetection === 'object') {
  32965. throw new Error('Impossible state! Change detection flag is not resolved!');
  32966. }
  32967. definitionMap.set('changeDetection', importExpr(Identifiers.ChangeDetectionStrategy)
  32968. .prop(ChangeDetectionStrategy[meta.changeDetection]));
  32969. }
  32970. if (meta.encapsulation !== ViewEncapsulation.Emulated) {
  32971. definitionMap.set('encapsulation', importExpr(Identifiers.ViewEncapsulation).prop(ViewEncapsulation[meta.encapsulation]));
  32972. }
  32973. if (meta.interpolation !== DEFAULT_INTERPOLATION_CONFIG) {
  32974. definitionMap.set('interpolation', literalArr([literal(meta.interpolation.start), literal(meta.interpolation.end)]));
  32975. }
  32976. if (template.preserveWhitespaces === true) {
  32977. definitionMap.set('preserveWhitespaces', literal(true));
  32978. }
  32979. if (meta.defer.mode === 0 /* DeferBlockDepsEmitMode.PerBlock */) {
  32980. const resolvers = [];
  32981. let hasResolvers = false;
  32982. for (const deps of meta.defer.blocks.values()) {
  32983. // Note: we need to push a `null` even if there are no dependencies, because matching of
  32984. // defer resolver functions to defer blocks happens by index and not adding an array
  32985. // entry for a block can throw off the blocks coming after it.
  32986. if (deps === null) {
  32987. resolvers.push(literal(null));
  32988. }
  32989. else {
  32990. resolvers.push(deps);
  32991. hasResolvers = true;
  32992. }
  32993. }
  32994. // If *all* the resolvers are null, we can skip the field.
  32995. if (hasResolvers) {
  32996. definitionMap.set('deferBlockDependencies', literalArr(resolvers));
  32997. }
  32998. }
  32999. else {
  33000. throw new Error('Unsupported defer function emit mode in partial compilation');
  33001. }
  33002. return definitionMap;
  33003. }
  33004. function getTemplateExpression(template, templateInfo) {
  33005. // If the template has been defined using a direct literal, we use that expression directly
  33006. // without any modifications. This is ensures proper source mapping from the partially
  33007. // compiled code to the source file declaring the template. Note that this does not capture
  33008. // template literals referenced indirectly through an identifier.
  33009. if (templateInfo.inlineTemplateLiteralExpression !== null) {
  33010. return templateInfo.inlineTemplateLiteralExpression;
  33011. }
  33012. // If the template is defined inline but not through a literal, the template has been resolved
  33013. // through static interpretation. We create a literal but cannot provide any source span. Note
  33014. // that we cannot use the expression defining the template because the linker expects the template
  33015. // to be defined as a literal in the declaration.
  33016. if (templateInfo.isInline) {
  33017. return literal(templateInfo.content, null, null);
  33018. }
  33019. // The template is external so we must synthesize an expression node with
  33020. // the appropriate source-span.
  33021. const contents = templateInfo.content;
  33022. const file = new ParseSourceFile(contents, templateInfo.sourceUrl);
  33023. const start = new ParseLocation(file, 0, 0, 0);
  33024. const end = computeEndLocation(file, contents);
  33025. const span = new ParseSourceSpan(start, end);
  33026. return literal(contents, null, span);
  33027. }
  33028. function computeEndLocation(file, contents) {
  33029. const length = contents.length;
  33030. let lineStart = 0;
  33031. let lastLineStart = 0;
  33032. let line = 0;
  33033. do {
  33034. lineStart = contents.indexOf('\n', lastLineStart);
  33035. if (lineStart !== -1) {
  33036. lastLineStart = lineStart + 1;
  33037. line++;
  33038. }
  33039. } while (lineStart !== -1);
  33040. return new ParseLocation(file, length, line, length - lastLineStart);
  33041. }
  33042. function compileUsedDependenciesMetadata(meta) {
  33043. const wrapType = meta.declarationListEmitMode !== 0 /* DeclarationListEmitMode.Direct */
  33044. ? generateForwardRef
  33045. : (expr) => expr;
  33046. if (meta.declarationListEmitMode === 3 /* DeclarationListEmitMode.RuntimeResolved */) {
  33047. throw new Error(`Unsupported emit mode`);
  33048. }
  33049. return toOptionalLiteralArray(meta.declarations, (decl) => {
  33050. switch (decl.kind) {
  33051. case R3TemplateDependencyKind.Directive:
  33052. const dirMeta = new DefinitionMap();
  33053. dirMeta.set('kind', literal(decl.isComponent ? 'component' : 'directive'));
  33054. dirMeta.set('type', wrapType(decl.type));
  33055. dirMeta.set('selector', literal(decl.selector));
  33056. dirMeta.set('inputs', toOptionalLiteralArray(decl.inputs, literal));
  33057. dirMeta.set('outputs', toOptionalLiteralArray(decl.outputs, literal));
  33058. dirMeta.set('exportAs', toOptionalLiteralArray(decl.exportAs, literal));
  33059. return dirMeta.toLiteralMap();
  33060. case R3TemplateDependencyKind.Pipe:
  33061. const pipeMeta = new DefinitionMap();
  33062. pipeMeta.set('kind', literal('pipe'));
  33063. pipeMeta.set('type', wrapType(decl.type));
  33064. pipeMeta.set('name', literal(decl.name));
  33065. return pipeMeta.toLiteralMap();
  33066. case R3TemplateDependencyKind.NgModule:
  33067. const ngModuleMeta = new DefinitionMap();
  33068. ngModuleMeta.set('kind', literal('ngmodule'));
  33069. ngModuleMeta.set('type', wrapType(decl.type));
  33070. return ngModuleMeta.toLiteralMap();
  33071. }
  33072. });
  33073. }
  33074. class BlockPresenceVisitor extends RecursiveVisitor$1 {
  33075. hasBlocks = false;
  33076. visitDeferredBlock() {
  33077. this.hasBlocks = true;
  33078. }
  33079. visitDeferredBlockPlaceholder() {
  33080. this.hasBlocks = true;
  33081. }
  33082. visitDeferredBlockLoading() {
  33083. this.hasBlocks = true;
  33084. }
  33085. visitDeferredBlockError() {
  33086. this.hasBlocks = true;
  33087. }
  33088. visitIfBlock() {
  33089. this.hasBlocks = true;
  33090. }
  33091. visitIfBlockBranch() {
  33092. this.hasBlocks = true;
  33093. }
  33094. visitForLoopBlock() {
  33095. this.hasBlocks = true;
  33096. }
  33097. visitForLoopBlockEmpty() {
  33098. this.hasBlocks = true;
  33099. }
  33100. visitSwitchBlock() {
  33101. this.hasBlocks = true;
  33102. }
  33103. visitSwitchBlockCase() {
  33104. this.hasBlocks = true;
  33105. }
  33106. }
  33107. /**
  33108. * Every time we make a breaking change to the declaration interface or partial-linker behavior, we
  33109. * must update this constant to prevent old partial-linkers from incorrectly processing the
  33110. * declaration.
  33111. *
  33112. * Do not include any prerelease in these versions as they are ignored.
  33113. */
  33114. const MINIMUM_PARTIAL_LINKER_VERSION$4 = '12.0.0';
  33115. function compileDeclareFactoryFunction(meta) {
  33116. const definitionMap = new DefinitionMap();
  33117. definitionMap.set('minVersion', literal(MINIMUM_PARTIAL_LINKER_VERSION$4));
  33118. definitionMap.set('version', literal('19.2.4'));
  33119. definitionMap.set('ngImport', importExpr(Identifiers.core));
  33120. definitionMap.set('type', meta.type.value);
  33121. definitionMap.set('deps', compileDependencies(meta.deps));
  33122. definitionMap.set('target', importExpr(Identifiers.FactoryTarget).prop(FactoryTarget$1[meta.target]));
  33123. return {
  33124. expression: importExpr(Identifiers.declareFactory).callFn([definitionMap.toLiteralMap()]),
  33125. statements: [],
  33126. type: createFactoryType(meta),
  33127. };
  33128. }
  33129. /**
  33130. * Every time we make a breaking change to the declaration interface or partial-linker behavior, we
  33131. * must update this constant to prevent old partial-linkers from incorrectly processing the
  33132. * declaration.
  33133. *
  33134. * Do not include any prerelease in these versions as they are ignored.
  33135. */
  33136. const MINIMUM_PARTIAL_LINKER_VERSION$3 = '12.0.0';
  33137. /**
  33138. * Compile a Injectable declaration defined by the `R3InjectableMetadata`.
  33139. */
  33140. function compileDeclareInjectableFromMetadata(meta) {
  33141. const definitionMap = createInjectableDefinitionMap(meta);
  33142. const expression = importExpr(Identifiers.declareInjectable).callFn([definitionMap.toLiteralMap()]);
  33143. const type = createInjectableType(meta);
  33144. return { expression, type, statements: [] };
  33145. }
  33146. /**
  33147. * Gathers the declaration fields for a Injectable into a `DefinitionMap`.
  33148. */
  33149. function createInjectableDefinitionMap(meta) {
  33150. const definitionMap = new DefinitionMap();
  33151. definitionMap.set('minVersion', literal(MINIMUM_PARTIAL_LINKER_VERSION$3));
  33152. definitionMap.set('version', literal('19.2.4'));
  33153. definitionMap.set('ngImport', importExpr(Identifiers.core));
  33154. definitionMap.set('type', meta.type.value);
  33155. // Only generate providedIn property if it has a non-null value
  33156. if (meta.providedIn !== undefined) {
  33157. const providedIn = convertFromMaybeForwardRefExpression(meta.providedIn);
  33158. if (providedIn.value !== null) {
  33159. definitionMap.set('providedIn', providedIn);
  33160. }
  33161. }
  33162. if (meta.useClass !== undefined) {
  33163. definitionMap.set('useClass', convertFromMaybeForwardRefExpression(meta.useClass));
  33164. }
  33165. if (meta.useExisting !== undefined) {
  33166. definitionMap.set('useExisting', convertFromMaybeForwardRefExpression(meta.useExisting));
  33167. }
  33168. if (meta.useValue !== undefined) {
  33169. definitionMap.set('useValue', convertFromMaybeForwardRefExpression(meta.useValue));
  33170. }
  33171. // Factories do not contain `ForwardRef`s since any types are already wrapped in a function call
  33172. // so the types will not be eagerly evaluated. Therefore we do not need to process this expression
  33173. // with `convertFromProviderExpression()`.
  33174. if (meta.useFactory !== undefined) {
  33175. definitionMap.set('useFactory', meta.useFactory);
  33176. }
  33177. if (meta.deps !== undefined) {
  33178. definitionMap.set('deps', literalArr(meta.deps.map(compileDependency)));
  33179. }
  33180. return definitionMap;
  33181. }
  33182. /**
  33183. * Every time we make a breaking change to the declaration interface or partial-linker behavior, we
  33184. * must update this constant to prevent old partial-linkers from incorrectly processing the
  33185. * declaration.
  33186. *
  33187. * Do not include any prerelease in these versions as they are ignored.
  33188. */
  33189. const MINIMUM_PARTIAL_LINKER_VERSION$2 = '12.0.0';
  33190. function compileDeclareInjectorFromMetadata(meta) {
  33191. const definitionMap = createInjectorDefinitionMap(meta);
  33192. const expression = importExpr(Identifiers.declareInjector).callFn([definitionMap.toLiteralMap()]);
  33193. const type = createInjectorType(meta);
  33194. return { expression, type, statements: [] };
  33195. }
  33196. /**
  33197. * Gathers the declaration fields for an Injector into a `DefinitionMap`.
  33198. */
  33199. function createInjectorDefinitionMap(meta) {
  33200. const definitionMap = new DefinitionMap();
  33201. definitionMap.set('minVersion', literal(MINIMUM_PARTIAL_LINKER_VERSION$2));
  33202. definitionMap.set('version', literal('19.2.4'));
  33203. definitionMap.set('ngImport', importExpr(Identifiers.core));
  33204. definitionMap.set('type', meta.type.value);
  33205. definitionMap.set('providers', meta.providers);
  33206. if (meta.imports.length > 0) {
  33207. definitionMap.set('imports', literalArr(meta.imports));
  33208. }
  33209. return definitionMap;
  33210. }
  33211. /**
  33212. * Every time we make a breaking change to the declaration interface or partial-linker behavior, we
  33213. * must update this constant to prevent old partial-linkers from incorrectly processing the
  33214. * declaration.
  33215. *
  33216. * Do not include any prerelease in these versions as they are ignored.
  33217. */
  33218. const MINIMUM_PARTIAL_LINKER_VERSION$1 = '14.0.0';
  33219. function compileDeclareNgModuleFromMetadata(meta) {
  33220. const definitionMap = createNgModuleDefinitionMap(meta);
  33221. const expression = importExpr(Identifiers.declareNgModule).callFn([definitionMap.toLiteralMap()]);
  33222. const type = createNgModuleType(meta);
  33223. return { expression, type, statements: [] };
  33224. }
  33225. /**
  33226. * Gathers the declaration fields for an NgModule into a `DefinitionMap`.
  33227. */
  33228. function createNgModuleDefinitionMap(meta) {
  33229. const definitionMap = new DefinitionMap();
  33230. if (meta.kind === R3NgModuleMetadataKind.Local) {
  33231. throw new Error('Invalid path! Local compilation mode should not get into the partial compilation path');
  33232. }
  33233. definitionMap.set('minVersion', literal(MINIMUM_PARTIAL_LINKER_VERSION$1));
  33234. definitionMap.set('version', literal('19.2.4'));
  33235. definitionMap.set('ngImport', importExpr(Identifiers.core));
  33236. definitionMap.set('type', meta.type.value);
  33237. // We only generate the keys in the metadata if the arrays contain values.
  33238. // We must wrap the arrays inside a function if any of the values are a forward reference to a
  33239. // not-yet-declared class. This is to support JIT execution of the `ɵɵngDeclareNgModule()` call.
  33240. // In the linker these wrappers are stripped and then reapplied for the `ɵɵdefineNgModule()` call.
  33241. if (meta.bootstrap.length > 0) {
  33242. definitionMap.set('bootstrap', refsToArray(meta.bootstrap, meta.containsForwardDecls));
  33243. }
  33244. if (meta.declarations.length > 0) {
  33245. definitionMap.set('declarations', refsToArray(meta.declarations, meta.containsForwardDecls));
  33246. }
  33247. if (meta.imports.length > 0) {
  33248. definitionMap.set('imports', refsToArray(meta.imports, meta.containsForwardDecls));
  33249. }
  33250. if (meta.exports.length > 0) {
  33251. definitionMap.set('exports', refsToArray(meta.exports, meta.containsForwardDecls));
  33252. }
  33253. if (meta.schemas !== null && meta.schemas.length > 0) {
  33254. definitionMap.set('schemas', literalArr(meta.schemas.map((ref) => ref.value)));
  33255. }
  33256. if (meta.id !== null) {
  33257. definitionMap.set('id', meta.id);
  33258. }
  33259. return definitionMap;
  33260. }
  33261. /**
  33262. * Every time we make a breaking change to the declaration interface or partial-linker behavior, we
  33263. * must update this constant to prevent old partial-linkers from incorrectly processing the
  33264. * declaration.
  33265. *
  33266. * Do not include any prerelease in these versions as they are ignored.
  33267. */
  33268. const MINIMUM_PARTIAL_LINKER_VERSION = '14.0.0';
  33269. /**
  33270. * Compile a Pipe declaration defined by the `R3PipeMetadata`.
  33271. */
  33272. function compileDeclarePipeFromMetadata(meta) {
  33273. const definitionMap = createPipeDefinitionMap(meta);
  33274. const expression = importExpr(Identifiers.declarePipe).callFn([definitionMap.toLiteralMap()]);
  33275. const type = createPipeType(meta);
  33276. return { expression, type, statements: [] };
  33277. }
  33278. /**
  33279. * Gathers the declaration fields for a Pipe into a `DefinitionMap`.
  33280. */
  33281. function createPipeDefinitionMap(meta) {
  33282. const definitionMap = new DefinitionMap();
  33283. definitionMap.set('minVersion', literal(MINIMUM_PARTIAL_LINKER_VERSION));
  33284. definitionMap.set('version', literal('19.2.4'));
  33285. definitionMap.set('ngImport', importExpr(Identifiers.core));
  33286. // e.g. `type: MyPipe`
  33287. definitionMap.set('type', meta.type.value);
  33288. if (meta.isStandalone !== undefined) {
  33289. definitionMap.set('isStandalone', literal(meta.isStandalone));
  33290. }
  33291. // e.g. `name: "myPipe"`
  33292. definitionMap.set('name', literal(meta.pipeName));
  33293. if (meta.pure === false) {
  33294. // e.g. `pure: false`
  33295. definitionMap.set('pure', literal(meta.pure));
  33296. }
  33297. return definitionMap;
  33298. }
  33299. //////////////////////////////////////
  33300. // THIS FILE HAS GLOBAL SIDE EFFECT //
  33301. // (see bottom of file) //
  33302. //////////////////////////////////////
  33303. /**
  33304. * @module
  33305. * @description
  33306. * Entry point for all APIs of the compiler package.
  33307. *
  33308. * <div class="callout is-critical">
  33309. * <header>Unstable APIs</header>
  33310. * <p>
  33311. * All compiler apis are currently considered experimental and private!
  33312. * </p>
  33313. * <p>
  33314. * We expect the APIs in this package to keep on changing. Do not rely on them.
  33315. * </p>
  33316. * </div>
  33317. */
  33318. // This file only reexports content of the `src` folder. Keep it that way.
  33319. // This function call has a global side effects and publishes the compiler into global namespace for
  33320. // the late binding of the Compiler to the @angular/core for jit compilation.
  33321. publishFacade(_global);
  33322. export { AST, ASTWithName, ASTWithSource, AbsoluteSourceSpan, ArrayType, ArrowFunctionExpr, Attribute, Binary, BinaryOperator, BinaryOperatorExpr, BindingPipe, BindingType, Block, BlockParameter, BoundElementProperty, BuiltinType, BuiltinTypeName, CUSTOM_ELEMENTS_SCHEMA, Call, Chain, ChangeDetectionStrategy, CommaExpr, Comment, CompilerConfig, Conditional, ConditionalExpr, ConstantPool, CssSelector, DEFAULT_INTERPOLATION_CONFIG, DYNAMIC_TYPE, DeclareFunctionStmt, DeclareVarStmt, DomElementSchemaRegistry, DynamicImportExpr, EOF, Element, ElementSchemaRegistry, EmitterVisitorContext, EmptyExpr$1 as EmptyExpr, Expansion, ExpansionCase, Expression, ExpressionBinding, ExpressionStatement, ExpressionType, ExternalExpr, ExternalReference, FactoryTarget$1 as FactoryTarget, FunctionExpr, HtmlParser, HtmlTagDefinition, I18NHtmlParser, IfStmt, ImplicitReceiver, InstantiateExpr, Interpolation$1 as Interpolation, InterpolationConfig, InvokeFunctionExpr, JSDocComment, JitEvaluator, KeyedRead, KeyedWrite, LeadingComment, LetDeclaration, Lexer, LiteralArray, LiteralArrayExpr, LiteralExpr, LiteralMap, LiteralMapExpr, LiteralPrimitive, LocalizedString, MapType, MessageBundle, NONE_TYPE, NO_ERRORS_SCHEMA, NodeWithI18n, NonNullAssert, NotExpr, ParseError, ParseErrorLevel, ParseLocation, ParseSourceFile, ParseSourceSpan, ParseSpan, ParseTreeResult, ParsedEvent, ParsedEventType, ParsedProperty, ParsedPropertyType, ParsedVariable, Parser, ParserError, PrefixNot, PropertyRead, PropertyWrite, R3BoundTarget, Identifiers as R3Identifiers, R3NgModuleMetadataKind, R3SelectorScopeMode, R3TargetBinder, R3TemplateDependencyKind, ReadKeyExpr, ReadPropExpr, ReadVarExpr, RecursiveAstVisitor, RecursiveVisitor, ResourceLoader, ReturnStatement, STRING_TYPE, SafeCall, SafeKeyedRead, SafePropertyRead, SelectorContext, SelectorListContext, SelectorMatcher, Serializer, SplitInterpolation, Statement, StmtModifier, StringToken, StringTokenKind, TagContentType, TaggedTemplateLiteralExpr, TemplateBindingParseResult, TemplateLiteral, TemplateLiteralElement, TemplateLiteralElementExpr, TemplateLiteralExpr, Text, ThisReceiver, BlockNode as TmplAstBlockNode, BoundAttribute as TmplAstBoundAttribute, BoundDeferredTrigger as TmplAstBoundDeferredTrigger, BoundEvent as TmplAstBoundEvent, BoundText as TmplAstBoundText, Content as TmplAstContent, DeferredBlock as TmplAstDeferredBlock, DeferredBlockError as TmplAstDeferredBlockError, DeferredBlockLoading as TmplAstDeferredBlockLoading, DeferredBlockPlaceholder as TmplAstDeferredBlockPlaceholder, DeferredTrigger as TmplAstDeferredTrigger, Element$1 as TmplAstElement, ForLoopBlock as TmplAstForLoopBlock, ForLoopBlockEmpty as TmplAstForLoopBlockEmpty, HoverDeferredTrigger as TmplAstHoverDeferredTrigger, Icu$1 as TmplAstIcu, IdleDeferredTrigger as TmplAstIdleDeferredTrigger, IfBlock as TmplAstIfBlock, IfBlockBranch as TmplAstIfBlockBranch, ImmediateDeferredTrigger as TmplAstImmediateDeferredTrigger, InteractionDeferredTrigger as TmplAstInteractionDeferredTrigger, LetDeclaration$1 as TmplAstLetDeclaration, NeverDeferredTrigger as TmplAstNeverDeferredTrigger, RecursiveVisitor$1 as TmplAstRecursiveVisitor, Reference as TmplAstReference, SwitchBlock as TmplAstSwitchBlock, SwitchBlockCase as TmplAstSwitchBlockCase, Template as TmplAstTemplate, Text$3 as TmplAstText, TextAttribute as TmplAstTextAttribute, TimerDeferredTrigger as TmplAstTimerDeferredTrigger, UnknownBlock as TmplAstUnknownBlock, Variable as TmplAstVariable, ViewportDeferredTrigger as TmplAstViewportDeferredTrigger, Token, TokenType, TransplantedType, TreeError, Type, TypeModifier, TypeofExpr, TypeofExpression, Unary, UnaryOperator, UnaryOperatorExpr, VERSION, VariableBinding, Version, ViewEncapsulation, WrappedNodeExpr, WriteKeyExpr, WritePropExpr, WriteVarExpr, Xliff, Xliff2, Xmb, XmlParser, Xtb, compileClassDebugInfo, compileClassMetadata, compileComponentClassMetadata, compileComponentDeclareClassMetadata, compileComponentFromMetadata, compileDeclareClassMetadata, compileDeclareComponentFromMetadata, compileDeclareDirectiveFromMetadata, compileDeclareFactoryFunction, compileDeclareInjectableFromMetadata, compileDeclareInjectorFromMetadata, compileDeclareNgModuleFromMetadata, compileDeclarePipeFromMetadata, compileDeferResolverFunction, compileDirectiveFromMetadata, compileFactoryFunction, compileHmrInitializer, compileHmrUpdateCallback, compileInjectable, compileInjector, compileNgModule, compileOpaqueAsyncClassMetadata, compilePipeFromMetadata, computeMsgId, core, createCssSelectorFromNode, createInjectableType, createMayBeForwardRefExpression, devOnlyGuardedExpression, emitDistinctChangesOnlyDefaultValue, encapsulateStyle, findMatchingDirectivesAndPipes, getHtmlTagDefinition, getNsPrefix, getSafePropertyAccessString, identifierName, isNgContainer, isNgContent, isNgTemplate, jsDocComment, leadingComment, literal, literalMap, makeBindingParser, mergeNsAndName, output_ast as outputAst, parseHostBindings, parseTemplate, preserveWhitespacesDefault, publishFacade, r3JitTypeSourceSpan, sanitizeIdentifier, splitNsName, visitAll$1 as tmplAstVisitAll, verifyHostBindings, visitAll };
  33323. //# sourceMappingURL=compiler.mjs.map