compiler.mjs 985 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019
  1. /**
  2. * @license Angular v16.0.4
  3. * (c) 2010-2022 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. constructor() {
  23. this.element = null;
  24. this.classNames = [];
  25. /**
  26. * The selectors are encoded in pairs where:
  27. * - even locations are attribute names
  28. * - odd locations are attribute values.
  29. *
  30. * Example:
  31. * Selector: `[key1=value1][key2]` would parse to:
  32. * ```
  33. * ['key1', 'value1', 'key2', '']
  34. * ```
  35. */
  36. this.attrs = [];
  37. this.notSelectors = [];
  38. }
  39. static parse(selector) {
  40. const results = [];
  41. const _addResult = (res, cssSel) => {
  42. if (cssSel.notSelectors.length > 0 && !cssSel.element && 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() && this.classNames.length == 0 && this.attrs.length == 0 &&
  140. this.notSelectors.length === 0;
  141. }
  142. hasElementSelector() {
  143. return !!this.element;
  144. }
  145. setElement(element = null) {
  146. this.element = element;
  147. }
  148. getAttrs() {
  149. const result = [];
  150. if (this.classNames.length > 0) {
  151. result.push('class', this.classNames.join(' '));
  152. }
  153. return result.concat(this.attrs);
  154. }
  155. addAttribute(name, value = '') {
  156. this.attrs.push(name, value && value.toLowerCase() || '');
  157. }
  158. addClassName(name) {
  159. this.classNames.push(name.toLowerCase());
  160. }
  161. toString() {
  162. let res = this.element || '';
  163. if (this.classNames) {
  164. this.classNames.forEach(klass => res += `.${klass}`);
  165. }
  166. if (this.attrs) {
  167. for (let i = 0; i < this.attrs.length; i += 2) {
  168. const name = this.escapeAttribute(this.attrs[i]);
  169. const value = this.attrs[i + 1];
  170. res += `[${name}${value ? '=' + value : ''}]`;
  171. }
  172. }
  173. this.notSelectors.forEach(notSelector => res += `:not(${notSelector})`);
  174. return res;
  175. }
  176. }
  177. /**
  178. * Reads a list of CssSelectors and allows to calculate which ones
  179. * are contained in a given CssSelector.
  180. */
  181. class SelectorMatcher {
  182. constructor() {
  183. this._elementMap = new Map();
  184. this._elementPartialMap = new Map();
  185. this._classMap = new Map();
  186. this._classPartialMap = new Map();
  187. this._attrValueMap = new Map();
  188. this._attrValuePartialMap = new Map();
  189. this._listContexts = [];
  190. }
  191. static createNotMatcher(notSelectors) {
  192. const notMatcher = new SelectorMatcher();
  193. notMatcher.addSelectables(notSelectors, null);
  194. return notMatcher;
  195. }
  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 = this._matchPartial(this._elementPartialMap, element, cssSelector, matchedCallback) ||
  297. 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. constructor(selectors) {
  367. this.selectors = selectors;
  368. this.alreadyMatched = false;
  369. }
  370. }
  371. // Store context to pass back selector and context when a selector is matched
  372. class SelectorContext {
  373. constructor(selector, cbContext, listContext) {
  374. this.selector = selector;
  375. this.cbContext = cbContext;
  376. this.listContext = listContext;
  377. this.notSelectors = selector.notSelectors;
  378. }
  379. finalize(cssSelector, callback) {
  380. let result = true;
  381. if (this.notSelectors.length > 0 && (!this.listContext || !this.listContext.alreadyMatched)) {
  382. const notMatcher = SelectorMatcher.createNotMatcher(this.notSelectors);
  383. result = !notMatcher.match(cssSelector, null);
  384. }
  385. if (result && callback && (!this.listContext || !this.listContext.alreadyMatched)) {
  386. if (this.listContext) {
  387. this.listContext.alreadyMatched = true;
  388. }
  389. callback(this.selector, this.cbContext);
  390. }
  391. return result;
  392. }
  393. }
  394. // Attention:
  395. // Stores the default value of `emitDistinctChangesOnly` when the `emitDistinctChangesOnly` is not
  396. // explicitly set.
  397. const emitDistinctChangesOnlyDefaultValue = true;
  398. var ViewEncapsulation;
  399. (function (ViewEncapsulation) {
  400. ViewEncapsulation[ViewEncapsulation["Emulated"] = 0] = "Emulated";
  401. // Historically the 1 value was for `Native` encapsulation which has been removed as of v11.
  402. ViewEncapsulation[ViewEncapsulation["None"] = 2] = "None";
  403. ViewEncapsulation[ViewEncapsulation["ShadowDom"] = 3] = "ShadowDom";
  404. })(ViewEncapsulation || (ViewEncapsulation = {}));
  405. var ChangeDetectionStrategy;
  406. (function (ChangeDetectionStrategy) {
  407. ChangeDetectionStrategy[ChangeDetectionStrategy["OnPush"] = 0] = "OnPush";
  408. ChangeDetectionStrategy[ChangeDetectionStrategy["Default"] = 1] = "Default";
  409. })(ChangeDetectionStrategy || (ChangeDetectionStrategy = {}));
  410. const CUSTOM_ELEMENTS_SCHEMA = {
  411. name: 'custom-elements'
  412. };
  413. const NO_ERRORS_SCHEMA = {
  414. name: 'no-errors-schema'
  415. };
  416. const Type$1 = Function;
  417. var SecurityContext;
  418. (function (SecurityContext) {
  419. SecurityContext[SecurityContext["NONE"] = 0] = "NONE";
  420. SecurityContext[SecurityContext["HTML"] = 1] = "HTML";
  421. SecurityContext[SecurityContext["STYLE"] = 2] = "STYLE";
  422. SecurityContext[SecurityContext["SCRIPT"] = 3] = "SCRIPT";
  423. SecurityContext[SecurityContext["URL"] = 4] = "URL";
  424. SecurityContext[SecurityContext["RESOURCE_URL"] = 5] = "RESOURCE_URL";
  425. })(SecurityContext || (SecurityContext = {}));
  426. var MissingTranslationStrategy;
  427. (function (MissingTranslationStrategy) {
  428. MissingTranslationStrategy[MissingTranslationStrategy["Error"] = 0] = "Error";
  429. MissingTranslationStrategy[MissingTranslationStrategy["Warning"] = 1] = "Warning";
  430. MissingTranslationStrategy[MissingTranslationStrategy["Ignore"] = 2] = "Ignore";
  431. })(MissingTranslationStrategy || (MissingTranslationStrategy = {}));
  432. function parserSelectorToSimpleSelector(selector) {
  433. const classes = selector.classNames && selector.classNames.length ?
  434. [8 /* SelectorFlags.CLASS */, ...selector.classNames] :
  435. [];
  436. const elementName = selector.element && selector.element !== '*' ? selector.element : '';
  437. return [elementName, ...selector.attrs, ...classes];
  438. }
  439. function parserSelectorToNegativeSelector(selector) {
  440. const classes = selector.classNames && selector.classNames.length ?
  441. [8 /* SelectorFlags.CLASS */, ...selector.classNames] :
  442. [];
  443. if (selector.element) {
  444. return [
  445. 1 /* SelectorFlags.NOT */ | 4 /* SelectorFlags.ELEMENT */, selector.element, ...selector.attrs, ...classes
  446. ];
  447. }
  448. else if (selector.attrs.length) {
  449. return [1 /* SelectorFlags.NOT */ | 2 /* SelectorFlags.ATTRIBUTE */, ...selector.attrs, ...classes];
  450. }
  451. else {
  452. return selector.classNames && selector.classNames.length ?
  453. [1 /* SelectorFlags.NOT */ | 8 /* SelectorFlags.CLASS */, ...selector.classNames] :
  454. [];
  455. }
  456. }
  457. function parserSelectorToR3Selector(selector) {
  458. const positive = parserSelectorToSimpleSelector(selector);
  459. const negative = selector.notSelectors && selector.notSelectors.length ?
  460. selector.notSelectors.map(notSelector => parserSelectorToNegativeSelector(notSelector)) :
  461. [];
  462. return positive.concat(...negative);
  463. }
  464. function parseSelectorToR3Selector(selector) {
  465. return selector ? CssSelector.parse(selector).map(parserSelectorToR3Selector) : [];
  466. }
  467. var core = /*#__PURE__*/Object.freeze({
  468. __proto__: null,
  469. emitDistinctChangesOnlyDefaultValue: emitDistinctChangesOnlyDefaultValue,
  470. get ViewEncapsulation () { return ViewEncapsulation; },
  471. get ChangeDetectionStrategy () { return ChangeDetectionStrategy; },
  472. CUSTOM_ELEMENTS_SCHEMA: CUSTOM_ELEMENTS_SCHEMA,
  473. NO_ERRORS_SCHEMA: NO_ERRORS_SCHEMA,
  474. Type: Type$1,
  475. get SecurityContext () { return SecurityContext; },
  476. get MissingTranslationStrategy () { return MissingTranslationStrategy; },
  477. parseSelectorToR3Selector: parseSelectorToR3Selector
  478. });
  479. /**
  480. * Represents a big integer using a buffer of its individual digits, with the least significant
  481. * digit stored at the beginning of the array (little endian).
  482. *
  483. * For performance reasons, each instance is mutable. The addition operation can be done in-place
  484. * to reduce memory pressure of allocation for the digits array.
  485. */
  486. class BigInteger {
  487. static zero() {
  488. return new BigInteger([0]);
  489. }
  490. static one() {
  491. return new BigInteger([1]);
  492. }
  493. /**
  494. * Creates a big integer using its individual digits in little endian storage.
  495. */
  496. constructor(digits) {
  497. this.digits = digits;
  498. }
  499. /**
  500. * Creates a clone of this instance.
  501. */
  502. clone() {
  503. return new BigInteger(this.digits.slice());
  504. }
  505. /**
  506. * Returns a new big integer with the sum of `this` and `other` as its value. This does not mutate
  507. * `this` but instead returns a new instance, unlike `addToSelf`.
  508. */
  509. add(other) {
  510. const result = this.clone();
  511. result.addToSelf(other);
  512. return result;
  513. }
  514. /**
  515. * Adds `other` to the instance itself, thereby mutating its value.
  516. */
  517. addToSelf(other) {
  518. const maxNrOfDigits = Math.max(this.digits.length, other.digits.length);
  519. let carry = 0;
  520. for (let i = 0; i < maxNrOfDigits; i++) {
  521. let digitSum = carry;
  522. if (i < this.digits.length) {
  523. digitSum += this.digits[i];
  524. }
  525. if (i < other.digits.length) {
  526. digitSum += other.digits[i];
  527. }
  528. if (digitSum >= 10) {
  529. this.digits[i] = digitSum - 10;
  530. carry = 1;
  531. }
  532. else {
  533. this.digits[i] = digitSum;
  534. carry = 0;
  535. }
  536. }
  537. // Apply a remaining carry if needed.
  538. if (carry > 0) {
  539. this.digits[maxNrOfDigits] = 1;
  540. }
  541. }
  542. /**
  543. * Builds the decimal string representation of the big integer. As this is stored in
  544. * little endian, the digits are concatenated in reverse order.
  545. */
  546. toString() {
  547. let res = '';
  548. for (let i = this.digits.length - 1; i >= 0; i--) {
  549. res += this.digits[i];
  550. }
  551. return res;
  552. }
  553. }
  554. /**
  555. * Represents a big integer which is optimized for multiplication operations, as its power-of-twos
  556. * are memoized. See `multiplyBy()` for details on the multiplication algorithm.
  557. */
  558. class BigIntForMultiplication {
  559. constructor(value) {
  560. this.powerOfTwos = [value];
  561. }
  562. /**
  563. * Returns the big integer itself.
  564. */
  565. getValue() {
  566. return this.powerOfTwos[0];
  567. }
  568. /**
  569. * Computes the value for `num * b`, where `num` is a JS number and `b` is a big integer. The
  570. * value for `b` is represented by a storage model that is optimized for this computation.
  571. *
  572. * This operation is implemented in N(log2(num)) by continuous halving of the number, where the
  573. * least-significant bit (LSB) is tested in each iteration. If the bit is set, the bit's index is
  574. * used as exponent into the power-of-two multiplication of `b`.
  575. *
  576. * As an example, consider the multiplication num=42, b=1337. In binary 42 is 0b00101010 and the
  577. * algorithm unrolls into the following iterations:
  578. *
  579. * Iteration | num | LSB | b * 2^iter | Add? | product
  580. * -----------|------------|------|------------|------|--------
  581. * 0 | 0b00101010 | 0 | 1337 | No | 0
  582. * 1 | 0b00010101 | 1 | 2674 | Yes | 2674
  583. * 2 | 0b00001010 | 0 | 5348 | No | 2674
  584. * 3 | 0b00000101 | 1 | 10696 | Yes | 13370
  585. * 4 | 0b00000010 | 0 | 21392 | No | 13370
  586. * 5 | 0b00000001 | 1 | 42784 | Yes | 56154
  587. * 6 | 0b00000000 | 0 | 85568 | No | 56154
  588. *
  589. * The computed product of 56154 is indeed the correct result.
  590. *
  591. * The `BigIntForMultiplication` representation for a big integer provides memoized access to the
  592. * power-of-two values to reduce the workload in computing those values.
  593. */
  594. multiplyBy(num) {
  595. const product = BigInteger.zero();
  596. this.multiplyByAndAddTo(num, product);
  597. return product;
  598. }
  599. /**
  600. * See `multiplyBy()` for details. This function allows for the computed product to be added
  601. * directly to the provided result big integer.
  602. */
  603. multiplyByAndAddTo(num, result) {
  604. for (let exponent = 0; num !== 0; num = num >>> 1, exponent++) {
  605. if (num & 1) {
  606. const value = this.getMultipliedByPowerOfTwo(exponent);
  607. result.addToSelf(value);
  608. }
  609. }
  610. }
  611. /**
  612. * Computes and memoizes the big integer value for `this.number * 2^exponent`.
  613. */
  614. getMultipliedByPowerOfTwo(exponent) {
  615. // Compute the powers up until the requested exponent, where each value is computed from its
  616. // predecessor. This is simple as `this.number * 2^(exponent - 1)` only has to be doubled (i.e.
  617. // added to itself) to reach `this.number * 2^exponent`.
  618. for (let i = this.powerOfTwos.length; i <= exponent; i++) {
  619. const previousPower = this.powerOfTwos[i - 1];
  620. this.powerOfTwos[i] = previousPower.add(previousPower);
  621. }
  622. return this.powerOfTwos[exponent];
  623. }
  624. }
  625. /**
  626. * Represents an exponentiation operation for the provided base, of which exponents are computed and
  627. * memoized. The results are represented by a `BigIntForMultiplication` which is tailored for
  628. * multiplication operations by memoizing the power-of-twos. This effectively results in a matrix
  629. * representation that is lazily computed upon request.
  630. */
  631. class BigIntExponentiation {
  632. constructor(base) {
  633. this.base = base;
  634. this.exponents = [new BigIntForMultiplication(BigInteger.one())];
  635. }
  636. /**
  637. * Compute the value for `this.base^exponent`, resulting in a big integer that is optimized for
  638. * further multiplication operations.
  639. */
  640. toThePowerOf(exponent) {
  641. // Compute the results up until the requested exponent, where every value is computed from its
  642. // predecessor. This is because `this.base^(exponent - 1)` only has to be multiplied by `base`
  643. // to reach `this.base^exponent`.
  644. for (let i = this.exponents.length; i <= exponent; i++) {
  645. const value = this.exponents[i - 1].multiplyBy(this.base);
  646. this.exponents[i] = new BigIntForMultiplication(value);
  647. }
  648. return this.exponents[exponent];
  649. }
  650. }
  651. /**
  652. * A lazily created TextEncoder instance for converting strings into UTF-8 bytes
  653. */
  654. let textEncoder;
  655. /**
  656. * Return the message id or compute it using the XLIFF1 digest.
  657. */
  658. function digest$1(message) {
  659. return message.id || computeDigest(message);
  660. }
  661. /**
  662. * Compute the message id using the XLIFF1 digest.
  663. */
  664. function computeDigest(message) {
  665. return sha1(serializeNodes(message.nodes).join('') + `[${message.meaning}]`);
  666. }
  667. /**
  668. * Return the message id or compute it using the XLIFF2/XMB/$localize digest.
  669. */
  670. function decimalDigest(message) {
  671. return message.id || computeDecimalDigest(message);
  672. }
  673. /**
  674. * Compute the message id using the XLIFF2/XMB/$localize digest.
  675. */
  676. function computeDecimalDigest(message) {
  677. const visitor = new _SerializerIgnoreIcuExpVisitor();
  678. const parts = message.nodes.map(a => a.visit(visitor, null));
  679. return computeMsgId(parts.join(''), message.meaning);
  680. }
  681. /**
  682. * Serialize the i18n ast to something xml-like in order to generate an UID.
  683. *
  684. * The visitor is also used in the i18n parser tests
  685. *
  686. * @internal
  687. */
  688. class _SerializerVisitor {
  689. visitText(text, context) {
  690. return text.value;
  691. }
  692. visitContainer(container, context) {
  693. return `[${container.children.map(child => child.visit(this)).join(', ')}]`;
  694. }
  695. visitIcu(icu, context) {
  696. const strCases = Object.keys(icu.cases).map((k) => `${k} {${icu.cases[k].visit(this)}}`);
  697. return `{${icu.expression}, ${icu.type}, ${strCases.join(', ')}}`;
  698. }
  699. visitTagPlaceholder(ph, context) {
  700. return ph.isVoid ?
  701. `<ph tag name="${ph.startName}"/>` :
  702. `<ph tag name="${ph.startName}">${ph.children.map(child => child.visit(this)).join(', ')}</ph name="${ph.closeName}">`;
  703. }
  704. visitPlaceholder(ph, context) {
  705. return ph.value ? `<ph name="${ph.name}">${ph.value}</ph>` : `<ph name="${ph.name}"/>`;
  706. }
  707. visitIcuPlaceholder(ph, context) {
  708. return `<ph icu name="${ph.name}">${ph.value.visit(this)}</ph>`;
  709. }
  710. }
  711. const serializerVisitor$1 = new _SerializerVisitor();
  712. function serializeNodes(nodes) {
  713. return nodes.map(a => a.visit(serializerVisitor$1, null));
  714. }
  715. /**
  716. * Serialize the i18n ast to something xml-like in order to generate an UID.
  717. *
  718. * Ignore the ICU expressions so that message IDs stays identical if only the expression changes.
  719. *
  720. * @internal
  721. */
  722. class _SerializerIgnoreIcuExpVisitor extends _SerializerVisitor {
  723. visitIcu(icu, context) {
  724. let strCases = Object.keys(icu.cases).map((k) => `${k} {${icu.cases[k].visit(this)}}`);
  725. // Do not take the expression into account
  726. return `{${icu.type}, ${strCases.join(', ')}}`;
  727. }
  728. }
  729. /**
  730. * Compute the SHA1 of the given string
  731. *
  732. * see https://csrc.nist.gov/publications/fips/fips180-4/fips-180-4.pdf
  733. *
  734. * WARNING: this function has not been designed not tested with security in mind.
  735. * DO NOT USE IT IN A SECURITY SENSITIVE CONTEXT.
  736. */
  737. function sha1(str) {
  738. textEncoder ??= new TextEncoder();
  739. const utf8 = [...textEncoder.encode(str)];
  740. const words32 = bytesToWords32(utf8, Endian.Big);
  741. const len = utf8.length * 8;
  742. const w = new Uint32Array(80);
  743. let a = 0x67452301, b = 0xefcdab89, c = 0x98badcfe, d = 0x10325476, e = 0xc3d2e1f0;
  744. words32[len >> 5] |= 0x80 << (24 - len % 32);
  745. words32[((len + 64 >> 9) << 4) + 15] = len;
  746. for (let i = 0; i < words32.length; i += 16) {
  747. const h0 = a, h1 = b, h2 = c, h3 = d, h4 = e;
  748. for (let j = 0; j < 80; j++) {
  749. if (j < 16) {
  750. w[j] = words32[i + j];
  751. }
  752. else {
  753. w[j] = rol32(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
  754. }
  755. const fkVal = fk(j, b, c, d);
  756. const f = fkVal[0];
  757. const k = fkVal[1];
  758. const temp = [rol32(a, 5), f, e, k, w[j]].reduce(add32);
  759. e = d;
  760. d = c;
  761. c = rol32(b, 30);
  762. b = a;
  763. a = temp;
  764. }
  765. a = add32(a, h0);
  766. b = add32(b, h1);
  767. c = add32(c, h2);
  768. d = add32(d, h3);
  769. e = add32(e, h4);
  770. }
  771. // Convert the output parts to a 160-bit hexadecimal string
  772. return toHexU32(a) + toHexU32(b) + toHexU32(c) + toHexU32(d) + toHexU32(e);
  773. }
  774. /**
  775. * Convert and format a number as a string representing a 32-bit unsigned hexadecimal number.
  776. * @param value The value to format as a string.
  777. * @returns A hexadecimal string representing the value.
  778. */
  779. function toHexU32(value) {
  780. // unsigned right shift of zero ensures an unsigned 32-bit number
  781. return (value >>> 0).toString(16).padStart(8, '0');
  782. }
  783. function fk(index, b, c, d) {
  784. if (index < 20) {
  785. return [(b & c) | (~b & d), 0x5a827999];
  786. }
  787. if (index < 40) {
  788. return [b ^ c ^ d, 0x6ed9eba1];
  789. }
  790. if (index < 60) {
  791. return [(b & c) | (b & d) | (c & d), 0x8f1bbcdc];
  792. }
  793. return [b ^ c ^ d, 0xca62c1d6];
  794. }
  795. /**
  796. * Compute the fingerprint of the given string
  797. *
  798. * The output is 64 bit number encoded as a decimal string
  799. *
  800. * based on:
  801. * https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/GoogleJsMessageIdGenerator.java
  802. */
  803. function fingerprint(str) {
  804. textEncoder ??= new TextEncoder();
  805. const utf8 = textEncoder.encode(str);
  806. const view = new DataView(utf8.buffer, utf8.byteOffset, utf8.byteLength);
  807. let hi = hash32(view, utf8.length, 0);
  808. let lo = hash32(view, utf8.length, 102072);
  809. if (hi == 0 && (lo == 0 || lo == 1)) {
  810. hi = hi ^ 0x130f9bef;
  811. lo = lo ^ -0x6b5f56d8;
  812. }
  813. return [hi, lo];
  814. }
  815. function computeMsgId(msg, meaning = '') {
  816. let msgFingerprint = fingerprint(msg);
  817. if (meaning) {
  818. const meaningFingerprint = fingerprint(meaning);
  819. msgFingerprint = add64(rol64(msgFingerprint, 1), meaningFingerprint);
  820. }
  821. const hi = msgFingerprint[0];
  822. const lo = msgFingerprint[1];
  823. return wordsToDecimalString(hi & 0x7fffffff, lo);
  824. }
  825. function hash32(view, length, c) {
  826. let a = 0x9e3779b9, b = 0x9e3779b9;
  827. let index = 0;
  828. const end = length - 12;
  829. for (; index <= end; index += 12) {
  830. a += view.getUint32(index, true);
  831. b += view.getUint32(index + 4, true);
  832. c += view.getUint32(index + 8, true);
  833. const res = mix(a, b, c);
  834. a = res[0], b = res[1], c = res[2];
  835. }
  836. const remainder = length - index;
  837. // the first byte of c is reserved for the length
  838. c += length;
  839. if (remainder >= 4) {
  840. a += view.getUint32(index, true);
  841. index += 4;
  842. if (remainder >= 8) {
  843. b += view.getUint32(index, true);
  844. index += 4;
  845. // Partial 32-bit word for c
  846. if (remainder >= 9) {
  847. c += view.getUint8(index++) << 8;
  848. }
  849. if (remainder >= 10) {
  850. c += view.getUint8(index++) << 16;
  851. }
  852. if (remainder === 11) {
  853. c += view.getUint8(index++) << 24;
  854. }
  855. }
  856. else {
  857. // Partial 32-bit word for b
  858. if (remainder >= 5) {
  859. b += view.getUint8(index++);
  860. }
  861. if (remainder >= 6) {
  862. b += view.getUint8(index++) << 8;
  863. }
  864. if (remainder === 7) {
  865. b += view.getUint8(index++) << 16;
  866. }
  867. }
  868. }
  869. else {
  870. // Partial 32-bit word for a
  871. if (remainder >= 1) {
  872. a += view.getUint8(index++);
  873. }
  874. if (remainder >= 2) {
  875. a += view.getUint8(index++) << 8;
  876. }
  877. if (remainder === 3) {
  878. a += view.getUint8(index++) << 16;
  879. }
  880. }
  881. return mix(a, b, c)[2];
  882. }
  883. // clang-format off
  884. function mix(a, b, c) {
  885. a -= b;
  886. a -= c;
  887. a ^= c >>> 13;
  888. b -= c;
  889. b -= a;
  890. b ^= a << 8;
  891. c -= a;
  892. c -= b;
  893. c ^= b >>> 13;
  894. a -= b;
  895. a -= c;
  896. a ^= c >>> 12;
  897. b -= c;
  898. b -= a;
  899. b ^= a << 16;
  900. c -= a;
  901. c -= b;
  902. c ^= b >>> 5;
  903. a -= b;
  904. a -= c;
  905. a ^= c >>> 3;
  906. b -= c;
  907. b -= a;
  908. b ^= a << 10;
  909. c -= a;
  910. c -= b;
  911. c ^= b >>> 15;
  912. return [a, b, c];
  913. }
  914. // clang-format on
  915. // Utils
  916. var Endian;
  917. (function (Endian) {
  918. Endian[Endian["Little"] = 0] = "Little";
  919. Endian[Endian["Big"] = 1] = "Big";
  920. })(Endian || (Endian = {}));
  921. function add32(a, b) {
  922. return add32to64(a, b)[1];
  923. }
  924. function add32to64(a, b) {
  925. const low = (a & 0xffff) + (b & 0xffff);
  926. const high = (a >>> 16) + (b >>> 16) + (low >>> 16);
  927. return [high >>> 16, (high << 16) | (low & 0xffff)];
  928. }
  929. function add64(a, b) {
  930. const ah = a[0], al = a[1];
  931. const bh = b[0], bl = b[1];
  932. const result = add32to64(al, bl);
  933. const carry = result[0];
  934. const l = result[1];
  935. const h = add32(add32(ah, bh), carry);
  936. return [h, l];
  937. }
  938. // Rotate a 32b number left `count` position
  939. function rol32(a, count) {
  940. return (a << count) | (a >>> (32 - count));
  941. }
  942. // Rotate a 64b number left `count` position
  943. function rol64(num, count) {
  944. const hi = num[0], lo = num[1];
  945. const h = (hi << count) | (lo >>> (32 - count));
  946. const l = (lo << count) | (hi >>> (32 - count));
  947. return [h, l];
  948. }
  949. function bytesToWords32(bytes, endian) {
  950. const size = (bytes.length + 3) >>> 2;
  951. const words32 = [];
  952. for (let i = 0; i < size; i++) {
  953. words32[i] = wordAt(bytes, i * 4, endian);
  954. }
  955. return words32;
  956. }
  957. function byteAt(bytes, index) {
  958. return index >= bytes.length ? 0 : bytes[index];
  959. }
  960. function wordAt(bytes, index, endian) {
  961. let word = 0;
  962. if (endian === Endian.Big) {
  963. for (let i = 0; i < 4; i++) {
  964. word += byteAt(bytes, index + i) << (24 - 8 * i);
  965. }
  966. }
  967. else {
  968. for (let i = 0; i < 4; i++) {
  969. word += byteAt(bytes, index + i) << 8 * i;
  970. }
  971. }
  972. return word;
  973. }
  974. /**
  975. * Create a shared exponentiation pool for base-256 computations. This shared pool provides memoized
  976. * power-of-256 results with memoized power-of-two computations for efficient multiplication.
  977. *
  978. * For our purposes, this can be safely stored as a global without memory concerns. The reason is
  979. * that we encode two words, so only need the 0th (for the low word) and 4th (for the high word)
  980. * exponent.
  981. */
  982. const base256 = new BigIntExponentiation(256);
  983. /**
  984. * Represents two 32-bit words as a single decimal number. This requires a big integer storage
  985. * model as JS numbers are not accurate enough to represent the 64-bit number.
  986. *
  987. * Based on https://www.danvk.org/hex2dec.html
  988. */
  989. function wordsToDecimalString(hi, lo) {
  990. // Encode the four bytes in lo in the lower digits of the decimal number.
  991. // Note: the multiplication results in lo itself but represented by a big integer using its
  992. // decimal digits.
  993. const decimal = base256.toThePowerOf(0).multiplyBy(lo);
  994. // Encode the four bytes in hi above the four lo bytes. lo is a maximum of (2^8)^4, which is why
  995. // this multiplication factor is applied.
  996. base256.toThePowerOf(4).multiplyByAndAddTo(hi, decimal);
  997. return decimal.toString();
  998. }
  999. //// Types
  1000. var TypeModifier;
  1001. (function (TypeModifier) {
  1002. TypeModifier[TypeModifier["None"] = 0] = "None";
  1003. TypeModifier[TypeModifier["Const"] = 1] = "Const";
  1004. })(TypeModifier || (TypeModifier = {}));
  1005. class Type {
  1006. constructor(modifiers = TypeModifier.None) {
  1007. this.modifiers = modifiers;
  1008. }
  1009. hasModifier(modifier) {
  1010. return (this.modifiers & modifier) !== 0;
  1011. }
  1012. }
  1013. var BuiltinTypeName;
  1014. (function (BuiltinTypeName) {
  1015. BuiltinTypeName[BuiltinTypeName["Dynamic"] = 0] = "Dynamic";
  1016. BuiltinTypeName[BuiltinTypeName["Bool"] = 1] = "Bool";
  1017. BuiltinTypeName[BuiltinTypeName["String"] = 2] = "String";
  1018. BuiltinTypeName[BuiltinTypeName["Int"] = 3] = "Int";
  1019. BuiltinTypeName[BuiltinTypeName["Number"] = 4] = "Number";
  1020. BuiltinTypeName[BuiltinTypeName["Function"] = 5] = "Function";
  1021. BuiltinTypeName[BuiltinTypeName["Inferred"] = 6] = "Inferred";
  1022. BuiltinTypeName[BuiltinTypeName["None"] = 7] = "None";
  1023. })(BuiltinTypeName || (BuiltinTypeName = {}));
  1024. class BuiltinType extends Type {
  1025. constructor(name, modifiers) {
  1026. super(modifiers);
  1027. this.name = name;
  1028. }
  1029. visitType(visitor, context) {
  1030. return visitor.visitBuiltinType(this, context);
  1031. }
  1032. }
  1033. class ExpressionType extends Type {
  1034. constructor(value, modifiers, typeParams = null) {
  1035. super(modifiers);
  1036. this.value = value;
  1037. this.typeParams = typeParams;
  1038. }
  1039. visitType(visitor, context) {
  1040. return visitor.visitExpressionType(this, context);
  1041. }
  1042. }
  1043. class ArrayType extends Type {
  1044. constructor(of, modifiers) {
  1045. super(modifiers);
  1046. this.of = of;
  1047. }
  1048. visitType(visitor, context) {
  1049. return visitor.visitArrayType(this, context);
  1050. }
  1051. }
  1052. class MapType extends Type {
  1053. constructor(valueType, modifiers) {
  1054. super(modifiers);
  1055. this.valueType = valueType || null;
  1056. }
  1057. visitType(visitor, context) {
  1058. return visitor.visitMapType(this, context);
  1059. }
  1060. }
  1061. class TransplantedType extends Type {
  1062. constructor(type, modifiers) {
  1063. super(modifiers);
  1064. this.type = type;
  1065. }
  1066. visitType(visitor, context) {
  1067. return visitor.visitTransplantedType(this, context);
  1068. }
  1069. }
  1070. const DYNAMIC_TYPE = new BuiltinType(BuiltinTypeName.Dynamic);
  1071. const INFERRED_TYPE = new BuiltinType(BuiltinTypeName.Inferred);
  1072. const BOOL_TYPE = new BuiltinType(BuiltinTypeName.Bool);
  1073. const INT_TYPE = new BuiltinType(BuiltinTypeName.Int);
  1074. const NUMBER_TYPE = new BuiltinType(BuiltinTypeName.Number);
  1075. const STRING_TYPE = new BuiltinType(BuiltinTypeName.String);
  1076. const FUNCTION_TYPE = new BuiltinType(BuiltinTypeName.Function);
  1077. const NONE_TYPE = new BuiltinType(BuiltinTypeName.None);
  1078. ///// Expressions
  1079. var UnaryOperator;
  1080. (function (UnaryOperator) {
  1081. UnaryOperator[UnaryOperator["Minus"] = 0] = "Minus";
  1082. UnaryOperator[UnaryOperator["Plus"] = 1] = "Plus";
  1083. })(UnaryOperator || (UnaryOperator = {}));
  1084. var BinaryOperator;
  1085. (function (BinaryOperator) {
  1086. BinaryOperator[BinaryOperator["Equals"] = 0] = "Equals";
  1087. BinaryOperator[BinaryOperator["NotEquals"] = 1] = "NotEquals";
  1088. BinaryOperator[BinaryOperator["Identical"] = 2] = "Identical";
  1089. BinaryOperator[BinaryOperator["NotIdentical"] = 3] = "NotIdentical";
  1090. BinaryOperator[BinaryOperator["Minus"] = 4] = "Minus";
  1091. BinaryOperator[BinaryOperator["Plus"] = 5] = "Plus";
  1092. BinaryOperator[BinaryOperator["Divide"] = 6] = "Divide";
  1093. BinaryOperator[BinaryOperator["Multiply"] = 7] = "Multiply";
  1094. BinaryOperator[BinaryOperator["Modulo"] = 8] = "Modulo";
  1095. BinaryOperator[BinaryOperator["And"] = 9] = "And";
  1096. BinaryOperator[BinaryOperator["Or"] = 10] = "Or";
  1097. BinaryOperator[BinaryOperator["BitwiseAnd"] = 11] = "BitwiseAnd";
  1098. BinaryOperator[BinaryOperator["Lower"] = 12] = "Lower";
  1099. BinaryOperator[BinaryOperator["LowerEquals"] = 13] = "LowerEquals";
  1100. BinaryOperator[BinaryOperator["Bigger"] = 14] = "Bigger";
  1101. BinaryOperator[BinaryOperator["BiggerEquals"] = 15] = "BiggerEquals";
  1102. BinaryOperator[BinaryOperator["NullishCoalesce"] = 16] = "NullishCoalesce";
  1103. })(BinaryOperator || (BinaryOperator = {}));
  1104. function nullSafeIsEquivalent(base, other) {
  1105. if (base == null || other == null) {
  1106. return base == other;
  1107. }
  1108. return base.isEquivalent(other);
  1109. }
  1110. function areAllEquivalentPredicate(base, other, equivalentPredicate) {
  1111. const len = base.length;
  1112. if (len !== other.length) {
  1113. return false;
  1114. }
  1115. for (let i = 0; i < len; i++) {
  1116. if (!equivalentPredicate(base[i], other[i])) {
  1117. return false;
  1118. }
  1119. }
  1120. return true;
  1121. }
  1122. function areAllEquivalent(base, other) {
  1123. return areAllEquivalentPredicate(base, other, (baseElement, otherElement) => baseElement.isEquivalent(otherElement));
  1124. }
  1125. class Expression {
  1126. constructor(type, sourceSpan) {
  1127. this.type = type || null;
  1128. this.sourceSpan = sourceSpan || null;
  1129. }
  1130. prop(name, sourceSpan) {
  1131. return new ReadPropExpr(this, name, null, sourceSpan);
  1132. }
  1133. key(index, type, sourceSpan) {
  1134. return new ReadKeyExpr(this, index, type, sourceSpan);
  1135. }
  1136. callFn(params, sourceSpan, pure) {
  1137. return new InvokeFunctionExpr(this, params, null, sourceSpan, pure);
  1138. }
  1139. instantiate(params, type, sourceSpan) {
  1140. return new InstantiateExpr(this, params, type, sourceSpan);
  1141. }
  1142. conditional(trueCase, falseCase = null, sourceSpan) {
  1143. return new ConditionalExpr(this, trueCase, falseCase, null, sourceSpan);
  1144. }
  1145. equals(rhs, sourceSpan) {
  1146. return new BinaryOperatorExpr(BinaryOperator.Equals, this, rhs, null, sourceSpan);
  1147. }
  1148. notEquals(rhs, sourceSpan) {
  1149. return new BinaryOperatorExpr(BinaryOperator.NotEquals, this, rhs, null, sourceSpan);
  1150. }
  1151. identical(rhs, sourceSpan) {
  1152. return new BinaryOperatorExpr(BinaryOperator.Identical, this, rhs, null, sourceSpan);
  1153. }
  1154. notIdentical(rhs, sourceSpan) {
  1155. return new BinaryOperatorExpr(BinaryOperator.NotIdentical, this, rhs, null, sourceSpan);
  1156. }
  1157. minus(rhs, sourceSpan) {
  1158. return new BinaryOperatorExpr(BinaryOperator.Minus, this, rhs, null, sourceSpan);
  1159. }
  1160. plus(rhs, sourceSpan) {
  1161. return new BinaryOperatorExpr(BinaryOperator.Plus, this, rhs, null, sourceSpan);
  1162. }
  1163. divide(rhs, sourceSpan) {
  1164. return new BinaryOperatorExpr(BinaryOperator.Divide, this, rhs, null, sourceSpan);
  1165. }
  1166. multiply(rhs, sourceSpan) {
  1167. return new BinaryOperatorExpr(BinaryOperator.Multiply, this, rhs, null, sourceSpan);
  1168. }
  1169. modulo(rhs, sourceSpan) {
  1170. return new BinaryOperatorExpr(BinaryOperator.Modulo, this, rhs, null, sourceSpan);
  1171. }
  1172. and(rhs, sourceSpan) {
  1173. return new BinaryOperatorExpr(BinaryOperator.And, this, rhs, null, sourceSpan);
  1174. }
  1175. bitwiseAnd(rhs, sourceSpan, parens = true) {
  1176. return new BinaryOperatorExpr(BinaryOperator.BitwiseAnd, this, rhs, null, sourceSpan, parens);
  1177. }
  1178. or(rhs, sourceSpan) {
  1179. return new BinaryOperatorExpr(BinaryOperator.Or, this, rhs, null, sourceSpan);
  1180. }
  1181. lower(rhs, sourceSpan) {
  1182. return new BinaryOperatorExpr(BinaryOperator.Lower, this, rhs, null, sourceSpan);
  1183. }
  1184. lowerEquals(rhs, sourceSpan) {
  1185. return new BinaryOperatorExpr(BinaryOperator.LowerEquals, this, rhs, null, sourceSpan);
  1186. }
  1187. bigger(rhs, sourceSpan) {
  1188. return new BinaryOperatorExpr(BinaryOperator.Bigger, this, rhs, null, sourceSpan);
  1189. }
  1190. biggerEquals(rhs, sourceSpan) {
  1191. return new BinaryOperatorExpr(BinaryOperator.BiggerEquals, this, rhs, null, sourceSpan);
  1192. }
  1193. isBlank(sourceSpan) {
  1194. // Note: We use equals by purpose here to compare to null and undefined in JS.
  1195. // We use the typed null to allow strictNullChecks to narrow types.
  1196. return this.equals(TYPED_NULL_EXPR, sourceSpan);
  1197. }
  1198. nullishCoalesce(rhs, sourceSpan) {
  1199. return new BinaryOperatorExpr(BinaryOperator.NullishCoalesce, this, rhs, null, sourceSpan);
  1200. }
  1201. toStmt() {
  1202. return new ExpressionStatement(this, null);
  1203. }
  1204. }
  1205. class ReadVarExpr extends Expression {
  1206. constructor(name, type, sourceSpan) {
  1207. super(type, sourceSpan);
  1208. this.name = name;
  1209. }
  1210. isEquivalent(e) {
  1211. return e instanceof ReadVarExpr && this.name === e.name;
  1212. }
  1213. isConstant() {
  1214. return false;
  1215. }
  1216. visitExpression(visitor, context) {
  1217. return visitor.visitReadVarExpr(this, context);
  1218. }
  1219. set(value) {
  1220. return new WriteVarExpr(this.name, value, null, this.sourceSpan);
  1221. }
  1222. }
  1223. class TypeofExpr extends Expression {
  1224. constructor(expr, type, sourceSpan) {
  1225. super(type, sourceSpan);
  1226. this.expr = expr;
  1227. }
  1228. visitExpression(visitor, context) {
  1229. return visitor.visitTypeofExpr(this, context);
  1230. }
  1231. isEquivalent(e) {
  1232. return e instanceof TypeofExpr && e.expr.isEquivalent(this.expr);
  1233. }
  1234. isConstant() {
  1235. return this.expr.isConstant();
  1236. }
  1237. }
  1238. class WrappedNodeExpr extends Expression {
  1239. constructor(node, type, sourceSpan) {
  1240. super(type, sourceSpan);
  1241. this.node = node;
  1242. }
  1243. isEquivalent(e) {
  1244. return e instanceof WrappedNodeExpr && this.node === e.node;
  1245. }
  1246. isConstant() {
  1247. return false;
  1248. }
  1249. visitExpression(visitor, context) {
  1250. return visitor.visitWrappedNodeExpr(this, context);
  1251. }
  1252. }
  1253. class WriteVarExpr extends Expression {
  1254. constructor(name, value, type, sourceSpan) {
  1255. super(type || value.type, sourceSpan);
  1256. this.name = name;
  1257. this.value = value;
  1258. }
  1259. isEquivalent(e) {
  1260. return e instanceof WriteVarExpr && this.name === e.name && this.value.isEquivalent(e.value);
  1261. }
  1262. isConstant() {
  1263. return false;
  1264. }
  1265. visitExpression(visitor, context) {
  1266. return visitor.visitWriteVarExpr(this, context);
  1267. }
  1268. toDeclStmt(type, modifiers) {
  1269. return new DeclareVarStmt(this.name, this.value, type, modifiers, this.sourceSpan);
  1270. }
  1271. toConstDecl() {
  1272. return this.toDeclStmt(INFERRED_TYPE, StmtModifier.Final);
  1273. }
  1274. }
  1275. class WriteKeyExpr extends Expression {
  1276. constructor(receiver, index, value, type, sourceSpan) {
  1277. super(type || value.type, sourceSpan);
  1278. this.receiver = receiver;
  1279. this.index = index;
  1280. this.value = value;
  1281. }
  1282. isEquivalent(e) {
  1283. return e instanceof WriteKeyExpr && this.receiver.isEquivalent(e.receiver) &&
  1284. this.index.isEquivalent(e.index) && this.value.isEquivalent(e.value);
  1285. }
  1286. isConstant() {
  1287. return false;
  1288. }
  1289. visitExpression(visitor, context) {
  1290. return visitor.visitWriteKeyExpr(this, context);
  1291. }
  1292. }
  1293. class WritePropExpr extends Expression {
  1294. constructor(receiver, name, value, type, sourceSpan) {
  1295. super(type || value.type, sourceSpan);
  1296. this.receiver = receiver;
  1297. this.name = name;
  1298. this.value = value;
  1299. }
  1300. isEquivalent(e) {
  1301. return e instanceof WritePropExpr && this.receiver.isEquivalent(e.receiver) &&
  1302. this.name === e.name && this.value.isEquivalent(e.value);
  1303. }
  1304. isConstant() {
  1305. return false;
  1306. }
  1307. visitExpression(visitor, context) {
  1308. return visitor.visitWritePropExpr(this, context);
  1309. }
  1310. }
  1311. class InvokeFunctionExpr extends Expression {
  1312. constructor(fn, args, type, sourceSpan, pure = false) {
  1313. super(type, sourceSpan);
  1314. this.fn = fn;
  1315. this.args = args;
  1316. this.pure = pure;
  1317. }
  1318. isEquivalent(e) {
  1319. return e instanceof InvokeFunctionExpr && this.fn.isEquivalent(e.fn) &&
  1320. areAllEquivalent(this.args, e.args) && this.pure === e.pure;
  1321. }
  1322. isConstant() {
  1323. return false;
  1324. }
  1325. visitExpression(visitor, context) {
  1326. return visitor.visitInvokeFunctionExpr(this, context);
  1327. }
  1328. }
  1329. class TaggedTemplateExpr extends Expression {
  1330. constructor(tag, template, type, sourceSpan) {
  1331. super(type, sourceSpan);
  1332. this.tag = tag;
  1333. this.template = template;
  1334. }
  1335. isEquivalent(e) {
  1336. return e instanceof TaggedTemplateExpr && this.tag.isEquivalent(e.tag) &&
  1337. areAllEquivalentPredicate(this.template.elements, e.template.elements, (a, b) => a.text === b.text) &&
  1338. areAllEquivalent(this.template.expressions, e.template.expressions);
  1339. }
  1340. isConstant() {
  1341. return false;
  1342. }
  1343. visitExpression(visitor, context) {
  1344. return visitor.visitTaggedTemplateExpr(this, context);
  1345. }
  1346. }
  1347. class InstantiateExpr extends Expression {
  1348. constructor(classExpr, args, type, sourceSpan) {
  1349. super(type, sourceSpan);
  1350. this.classExpr = classExpr;
  1351. this.args = args;
  1352. }
  1353. isEquivalent(e) {
  1354. return e instanceof InstantiateExpr && this.classExpr.isEquivalent(e.classExpr) &&
  1355. areAllEquivalent(this.args, e.args);
  1356. }
  1357. isConstant() {
  1358. return false;
  1359. }
  1360. visitExpression(visitor, context) {
  1361. return visitor.visitInstantiateExpr(this, context);
  1362. }
  1363. }
  1364. class LiteralExpr extends Expression {
  1365. constructor(value, type, sourceSpan) {
  1366. super(type, sourceSpan);
  1367. this.value = value;
  1368. }
  1369. isEquivalent(e) {
  1370. return e instanceof LiteralExpr && this.value === e.value;
  1371. }
  1372. isConstant() {
  1373. return true;
  1374. }
  1375. visitExpression(visitor, context) {
  1376. return visitor.visitLiteralExpr(this, context);
  1377. }
  1378. }
  1379. class TemplateLiteral {
  1380. constructor(elements, expressions) {
  1381. this.elements = elements;
  1382. this.expressions = expressions;
  1383. }
  1384. }
  1385. class TemplateLiteralElement {
  1386. constructor(text, sourceSpan, rawText) {
  1387. this.text = text;
  1388. this.sourceSpan = sourceSpan;
  1389. // If `rawText` is not provided, try to extract the raw string from its
  1390. // associated `sourceSpan`. If that is also not available, "fake" the raw
  1391. // string instead by escaping the following control sequences:
  1392. // - "\" would otherwise indicate that the next character is a control character.
  1393. // - "`" and "${" are template string control sequences that would otherwise prematurely
  1394. // indicate the end of the template literal element.
  1395. this.rawText =
  1396. rawText ?? sourceSpan?.toString() ?? escapeForTemplateLiteral(escapeSlashes(text));
  1397. }
  1398. }
  1399. class LiteralPiece {
  1400. constructor(text, sourceSpan) {
  1401. this.text = text;
  1402. this.sourceSpan = sourceSpan;
  1403. }
  1404. }
  1405. class PlaceholderPiece {
  1406. /**
  1407. * Create a new instance of a `PlaceholderPiece`.
  1408. *
  1409. * @param text the name of this placeholder (e.g. `PH_1`).
  1410. * @param sourceSpan the location of this placeholder in its localized message the source code.
  1411. * @param associatedMessage reference to another message that this placeholder is associated with.
  1412. * The `associatedMessage` is mainly used to provide a relationship to an ICU message that has
  1413. * been extracted out from the message containing the placeholder.
  1414. */
  1415. constructor(text, sourceSpan, associatedMessage) {
  1416. this.text = text;
  1417. this.sourceSpan = sourceSpan;
  1418. this.associatedMessage = associatedMessage;
  1419. }
  1420. }
  1421. const MEANING_SEPARATOR$1 = '|';
  1422. const ID_SEPARATOR$1 = '@@';
  1423. const LEGACY_ID_INDICATOR = '␟';
  1424. class LocalizedString extends Expression {
  1425. constructor(metaBlock, messageParts, placeHolderNames, expressions, sourceSpan) {
  1426. super(STRING_TYPE, sourceSpan);
  1427. this.metaBlock = metaBlock;
  1428. this.messageParts = messageParts;
  1429. this.placeHolderNames = placeHolderNames;
  1430. this.expressions = expressions;
  1431. }
  1432. isEquivalent(e) {
  1433. // return e instanceof LocalizedString && this.message === e.message;
  1434. return false;
  1435. }
  1436. isConstant() {
  1437. return false;
  1438. }
  1439. visitExpression(visitor, context) {
  1440. return visitor.visitLocalizedString(this, context);
  1441. }
  1442. /**
  1443. * Serialize the given `meta` and `messagePart` into "cooked" and "raw" strings that can be used
  1444. * in a `$localize` tagged string. The format of the metadata is the same as that parsed by
  1445. * `parseI18nMeta()`.
  1446. *
  1447. * @param meta The metadata to serialize
  1448. * @param messagePart The first part of the tagged string
  1449. */
  1450. serializeI18nHead() {
  1451. let metaBlock = this.metaBlock.description || '';
  1452. if (this.metaBlock.meaning) {
  1453. metaBlock = `${this.metaBlock.meaning}${MEANING_SEPARATOR$1}${metaBlock}`;
  1454. }
  1455. if (this.metaBlock.customId) {
  1456. metaBlock = `${metaBlock}${ID_SEPARATOR$1}${this.metaBlock.customId}`;
  1457. }
  1458. if (this.metaBlock.legacyIds) {
  1459. this.metaBlock.legacyIds.forEach(legacyId => {
  1460. metaBlock = `${metaBlock}${LEGACY_ID_INDICATOR}${legacyId}`;
  1461. });
  1462. }
  1463. return createCookedRawString(metaBlock, this.messageParts[0].text, this.getMessagePartSourceSpan(0));
  1464. }
  1465. getMessagePartSourceSpan(i) {
  1466. return this.messageParts[i]?.sourceSpan ?? this.sourceSpan;
  1467. }
  1468. getPlaceholderSourceSpan(i) {
  1469. return this.placeHolderNames[i]?.sourceSpan ?? this.expressions[i]?.sourceSpan ??
  1470. this.sourceSpan;
  1471. }
  1472. /**
  1473. * Serialize the given `placeholderName` and `messagePart` into "cooked" and "raw" strings that
  1474. * can be used in a `$localize` tagged string.
  1475. *
  1476. * The format is `:<placeholder-name>[@@<associated-id>]:`.
  1477. *
  1478. * The `associated-id` is the message id of the (usually an ICU) message to which this placeholder
  1479. * refers.
  1480. *
  1481. * @param partIndex The index of the message part to serialize.
  1482. */
  1483. serializeI18nTemplatePart(partIndex) {
  1484. const placeholder = this.placeHolderNames[partIndex - 1];
  1485. const messagePart = this.messageParts[partIndex];
  1486. let metaBlock = placeholder.text;
  1487. if (placeholder.associatedMessage?.legacyIds.length === 0) {
  1488. metaBlock += `${ID_SEPARATOR$1}${computeMsgId(placeholder.associatedMessage.messageString, placeholder.associatedMessage.meaning)}`;
  1489. }
  1490. return createCookedRawString(metaBlock, messagePart.text, this.getMessagePartSourceSpan(partIndex));
  1491. }
  1492. }
  1493. const escapeSlashes = (str) => str.replace(/\\/g, '\\\\');
  1494. const escapeStartingColon = (str) => str.replace(/^:/, '\\:');
  1495. const escapeColons = (str) => str.replace(/:/g, '\\:');
  1496. const escapeForTemplateLiteral = (str) => str.replace(/`/g, '\\`').replace(/\${/g, '$\\{');
  1497. /**
  1498. * Creates a `{cooked, raw}` object from the `metaBlock` and `messagePart`.
  1499. *
  1500. * The `raw` text must have various character sequences escaped:
  1501. * * "\" would otherwise indicate that the next character is a control character.
  1502. * * "`" and "${" are template string control sequences that would otherwise prematurely indicate
  1503. * the end of a message part.
  1504. * * ":" inside a metablock would prematurely indicate the end of the metablock.
  1505. * * ":" at the start of a messagePart with no metablock would erroneously indicate the start of a
  1506. * metablock.
  1507. *
  1508. * @param metaBlock Any metadata that should be prepended to the string
  1509. * @param messagePart The message part of the string
  1510. */
  1511. function createCookedRawString(metaBlock, messagePart, range) {
  1512. if (metaBlock === '') {
  1513. return {
  1514. cooked: messagePart,
  1515. raw: escapeForTemplateLiteral(escapeStartingColon(escapeSlashes(messagePart))),
  1516. range,
  1517. };
  1518. }
  1519. else {
  1520. return {
  1521. cooked: `:${metaBlock}:${messagePart}`,
  1522. raw: escapeForTemplateLiteral(`:${escapeColons(escapeSlashes(metaBlock))}:${escapeSlashes(messagePart)}`),
  1523. range,
  1524. };
  1525. }
  1526. }
  1527. class ExternalExpr extends Expression {
  1528. constructor(value, type, typeParams = null, sourceSpan) {
  1529. super(type, sourceSpan);
  1530. this.value = value;
  1531. this.typeParams = typeParams;
  1532. }
  1533. isEquivalent(e) {
  1534. return e instanceof ExternalExpr && this.value.name === e.value.name &&
  1535. this.value.moduleName === e.value.moduleName && this.value.runtime === e.value.runtime;
  1536. }
  1537. isConstant() {
  1538. return false;
  1539. }
  1540. visitExpression(visitor, context) {
  1541. return visitor.visitExternalExpr(this, context);
  1542. }
  1543. }
  1544. class ExternalReference {
  1545. constructor(moduleName, name, runtime) {
  1546. this.moduleName = moduleName;
  1547. this.name = name;
  1548. this.runtime = runtime;
  1549. }
  1550. }
  1551. class ConditionalExpr extends Expression {
  1552. constructor(condition, trueCase, falseCase = null, type, sourceSpan) {
  1553. super(type || trueCase.type, sourceSpan);
  1554. this.condition = condition;
  1555. this.falseCase = falseCase;
  1556. this.trueCase = trueCase;
  1557. }
  1558. isEquivalent(e) {
  1559. return e instanceof ConditionalExpr && this.condition.isEquivalent(e.condition) &&
  1560. this.trueCase.isEquivalent(e.trueCase) && nullSafeIsEquivalent(this.falseCase, e.falseCase);
  1561. }
  1562. isConstant() {
  1563. return false;
  1564. }
  1565. visitExpression(visitor, context) {
  1566. return visitor.visitConditionalExpr(this, context);
  1567. }
  1568. }
  1569. class NotExpr extends Expression {
  1570. constructor(condition, sourceSpan) {
  1571. super(BOOL_TYPE, sourceSpan);
  1572. this.condition = condition;
  1573. }
  1574. isEquivalent(e) {
  1575. return e instanceof NotExpr && this.condition.isEquivalent(e.condition);
  1576. }
  1577. isConstant() {
  1578. return false;
  1579. }
  1580. visitExpression(visitor, context) {
  1581. return visitor.visitNotExpr(this, context);
  1582. }
  1583. }
  1584. class FnParam {
  1585. constructor(name, type = null) {
  1586. this.name = name;
  1587. this.type = type;
  1588. }
  1589. isEquivalent(param) {
  1590. return this.name === param.name;
  1591. }
  1592. }
  1593. class FunctionExpr extends Expression {
  1594. constructor(params, statements, type, sourceSpan, name) {
  1595. super(type, sourceSpan);
  1596. this.params = params;
  1597. this.statements = statements;
  1598. this.name = name;
  1599. }
  1600. isEquivalent(e) {
  1601. return e instanceof FunctionExpr && areAllEquivalent(this.params, e.params) &&
  1602. areAllEquivalent(this.statements, e.statements);
  1603. }
  1604. isConstant() {
  1605. return false;
  1606. }
  1607. visitExpression(visitor, context) {
  1608. return visitor.visitFunctionExpr(this, context);
  1609. }
  1610. toDeclStmt(name, modifiers) {
  1611. return new DeclareFunctionStmt(name, this.params, this.statements, this.type, modifiers, this.sourceSpan);
  1612. }
  1613. }
  1614. class UnaryOperatorExpr extends Expression {
  1615. constructor(operator, expr, type, sourceSpan, parens = true) {
  1616. super(type || NUMBER_TYPE, sourceSpan);
  1617. this.operator = operator;
  1618. this.expr = expr;
  1619. this.parens = parens;
  1620. }
  1621. isEquivalent(e) {
  1622. return e instanceof UnaryOperatorExpr && this.operator === e.operator &&
  1623. this.expr.isEquivalent(e.expr);
  1624. }
  1625. isConstant() {
  1626. return false;
  1627. }
  1628. visitExpression(visitor, context) {
  1629. return visitor.visitUnaryOperatorExpr(this, context);
  1630. }
  1631. }
  1632. class BinaryOperatorExpr extends Expression {
  1633. constructor(operator, lhs, rhs, type, sourceSpan, parens = true) {
  1634. super(type || lhs.type, sourceSpan);
  1635. this.operator = operator;
  1636. this.rhs = rhs;
  1637. this.parens = parens;
  1638. this.lhs = lhs;
  1639. }
  1640. isEquivalent(e) {
  1641. return e instanceof BinaryOperatorExpr && this.operator === e.operator &&
  1642. this.lhs.isEquivalent(e.lhs) && this.rhs.isEquivalent(e.rhs);
  1643. }
  1644. isConstant() {
  1645. return false;
  1646. }
  1647. visitExpression(visitor, context) {
  1648. return visitor.visitBinaryOperatorExpr(this, context);
  1649. }
  1650. }
  1651. class ReadPropExpr extends Expression {
  1652. constructor(receiver, name, type, sourceSpan) {
  1653. super(type, sourceSpan);
  1654. this.receiver = receiver;
  1655. this.name = name;
  1656. }
  1657. isEquivalent(e) {
  1658. return e instanceof ReadPropExpr && this.receiver.isEquivalent(e.receiver) &&
  1659. this.name === e.name;
  1660. }
  1661. isConstant() {
  1662. return false;
  1663. }
  1664. visitExpression(visitor, context) {
  1665. return visitor.visitReadPropExpr(this, context);
  1666. }
  1667. set(value) {
  1668. return new WritePropExpr(this.receiver, this.name, value, null, this.sourceSpan);
  1669. }
  1670. }
  1671. class ReadKeyExpr extends Expression {
  1672. constructor(receiver, index, type, sourceSpan) {
  1673. super(type, sourceSpan);
  1674. this.receiver = receiver;
  1675. this.index = index;
  1676. }
  1677. isEquivalent(e) {
  1678. return e instanceof ReadKeyExpr && this.receiver.isEquivalent(e.receiver) &&
  1679. this.index.isEquivalent(e.index);
  1680. }
  1681. isConstant() {
  1682. return false;
  1683. }
  1684. visitExpression(visitor, context) {
  1685. return visitor.visitReadKeyExpr(this, context);
  1686. }
  1687. set(value) {
  1688. return new WriteKeyExpr(this.receiver, this.index, value, null, this.sourceSpan);
  1689. }
  1690. }
  1691. class LiteralArrayExpr extends Expression {
  1692. constructor(entries, type, sourceSpan) {
  1693. super(type, sourceSpan);
  1694. this.entries = entries;
  1695. }
  1696. isConstant() {
  1697. return this.entries.every(e => e.isConstant());
  1698. }
  1699. isEquivalent(e) {
  1700. return e instanceof LiteralArrayExpr && areAllEquivalent(this.entries, e.entries);
  1701. }
  1702. visitExpression(visitor, context) {
  1703. return visitor.visitLiteralArrayExpr(this, context);
  1704. }
  1705. }
  1706. class LiteralMapEntry {
  1707. constructor(key, value, quoted) {
  1708. this.key = key;
  1709. this.value = value;
  1710. this.quoted = quoted;
  1711. }
  1712. isEquivalent(e) {
  1713. return this.key === e.key && this.value.isEquivalent(e.value);
  1714. }
  1715. }
  1716. class LiteralMapExpr extends Expression {
  1717. constructor(entries, type, sourceSpan) {
  1718. super(type, sourceSpan);
  1719. this.entries = entries;
  1720. this.valueType = null;
  1721. if (type) {
  1722. this.valueType = type.valueType;
  1723. }
  1724. }
  1725. isEquivalent(e) {
  1726. return e instanceof LiteralMapExpr && areAllEquivalent(this.entries, e.entries);
  1727. }
  1728. isConstant() {
  1729. return this.entries.every(e => e.value.isConstant());
  1730. }
  1731. visitExpression(visitor, context) {
  1732. return visitor.visitLiteralMapExpr(this, context);
  1733. }
  1734. }
  1735. class CommaExpr extends Expression {
  1736. constructor(parts, sourceSpan) {
  1737. super(parts[parts.length - 1].type, sourceSpan);
  1738. this.parts = parts;
  1739. }
  1740. isEquivalent(e) {
  1741. return e instanceof CommaExpr && areAllEquivalent(this.parts, e.parts);
  1742. }
  1743. isConstant() {
  1744. return false;
  1745. }
  1746. visitExpression(visitor, context) {
  1747. return visitor.visitCommaExpr(this, context);
  1748. }
  1749. }
  1750. const NULL_EXPR = new LiteralExpr(null, null, null);
  1751. const TYPED_NULL_EXPR = new LiteralExpr(null, INFERRED_TYPE, null);
  1752. //// Statements
  1753. var StmtModifier;
  1754. (function (StmtModifier) {
  1755. StmtModifier[StmtModifier["None"] = 0] = "None";
  1756. StmtModifier[StmtModifier["Final"] = 1] = "Final";
  1757. StmtModifier[StmtModifier["Private"] = 2] = "Private";
  1758. StmtModifier[StmtModifier["Exported"] = 4] = "Exported";
  1759. StmtModifier[StmtModifier["Static"] = 8] = "Static";
  1760. })(StmtModifier || (StmtModifier = {}));
  1761. class LeadingComment {
  1762. constructor(text, multiline, trailingNewline) {
  1763. this.text = text;
  1764. this.multiline = multiline;
  1765. this.trailingNewline = trailingNewline;
  1766. }
  1767. toString() {
  1768. return this.multiline ? ` ${this.text} ` : this.text;
  1769. }
  1770. }
  1771. class JSDocComment extends LeadingComment {
  1772. constructor(tags) {
  1773. super('', /* multiline */ true, /* trailingNewline */ true);
  1774. this.tags = tags;
  1775. }
  1776. toString() {
  1777. return serializeTags(this.tags);
  1778. }
  1779. }
  1780. class Statement {
  1781. constructor(modifiers = StmtModifier.None, sourceSpan = null, leadingComments) {
  1782. this.modifiers = modifiers;
  1783. this.sourceSpan = sourceSpan;
  1784. this.leadingComments = leadingComments;
  1785. }
  1786. hasModifier(modifier) {
  1787. return (this.modifiers & modifier) !== 0;
  1788. }
  1789. addLeadingComment(leadingComment) {
  1790. this.leadingComments = this.leadingComments ?? [];
  1791. this.leadingComments.push(leadingComment);
  1792. }
  1793. }
  1794. class DeclareVarStmt extends Statement {
  1795. constructor(name, value, type, modifiers, sourceSpan, leadingComments) {
  1796. super(modifiers, sourceSpan, leadingComments);
  1797. this.name = name;
  1798. this.value = value;
  1799. this.type = type || (value && value.type) || null;
  1800. }
  1801. isEquivalent(stmt) {
  1802. return stmt instanceof DeclareVarStmt && this.name === stmt.name &&
  1803. (this.value ? !!stmt.value && this.value.isEquivalent(stmt.value) : !stmt.value);
  1804. }
  1805. visitStatement(visitor, context) {
  1806. return visitor.visitDeclareVarStmt(this, context);
  1807. }
  1808. }
  1809. class DeclareFunctionStmt extends Statement {
  1810. constructor(name, params, statements, type, modifiers, sourceSpan, leadingComments) {
  1811. super(modifiers, sourceSpan, leadingComments);
  1812. this.name = name;
  1813. this.params = params;
  1814. this.statements = statements;
  1815. this.type = type || null;
  1816. }
  1817. isEquivalent(stmt) {
  1818. return stmt instanceof DeclareFunctionStmt && areAllEquivalent(this.params, stmt.params) &&
  1819. areAllEquivalent(this.statements, stmt.statements);
  1820. }
  1821. visitStatement(visitor, context) {
  1822. return visitor.visitDeclareFunctionStmt(this, context);
  1823. }
  1824. }
  1825. class ExpressionStatement extends Statement {
  1826. constructor(expr, sourceSpan, leadingComments) {
  1827. super(StmtModifier.None, sourceSpan, leadingComments);
  1828. this.expr = expr;
  1829. }
  1830. isEquivalent(stmt) {
  1831. return stmt instanceof ExpressionStatement && this.expr.isEquivalent(stmt.expr);
  1832. }
  1833. visitStatement(visitor, context) {
  1834. return visitor.visitExpressionStmt(this, context);
  1835. }
  1836. }
  1837. class ReturnStatement extends Statement {
  1838. constructor(value, sourceSpan = null, leadingComments) {
  1839. super(StmtModifier.None, sourceSpan, leadingComments);
  1840. this.value = value;
  1841. }
  1842. isEquivalent(stmt) {
  1843. return stmt instanceof ReturnStatement && this.value.isEquivalent(stmt.value);
  1844. }
  1845. visitStatement(visitor, context) {
  1846. return visitor.visitReturnStmt(this, context);
  1847. }
  1848. }
  1849. class IfStmt extends Statement {
  1850. constructor(condition, trueCase, falseCase = [], sourceSpan, leadingComments) {
  1851. super(StmtModifier.None, sourceSpan, leadingComments);
  1852. this.condition = condition;
  1853. this.trueCase = trueCase;
  1854. this.falseCase = falseCase;
  1855. }
  1856. isEquivalent(stmt) {
  1857. return stmt instanceof IfStmt && this.condition.isEquivalent(stmt.condition) &&
  1858. areAllEquivalent(this.trueCase, stmt.trueCase) &&
  1859. areAllEquivalent(this.falseCase, stmt.falseCase);
  1860. }
  1861. visitStatement(visitor, context) {
  1862. return visitor.visitIfStmt(this, context);
  1863. }
  1864. }
  1865. class RecursiveAstVisitor$1 {
  1866. visitType(ast, context) {
  1867. return ast;
  1868. }
  1869. visitExpression(ast, context) {
  1870. if (ast.type) {
  1871. ast.type.visitType(this, context);
  1872. }
  1873. return ast;
  1874. }
  1875. visitBuiltinType(type, context) {
  1876. return this.visitType(type, context);
  1877. }
  1878. visitExpressionType(type, context) {
  1879. type.value.visitExpression(this, context);
  1880. if (type.typeParams !== null) {
  1881. type.typeParams.forEach(param => this.visitType(param, context));
  1882. }
  1883. return this.visitType(type, context);
  1884. }
  1885. visitArrayType(type, context) {
  1886. return this.visitType(type, context);
  1887. }
  1888. visitMapType(type, context) {
  1889. return this.visitType(type, context);
  1890. }
  1891. visitTransplantedType(type, context) {
  1892. return type;
  1893. }
  1894. visitWrappedNodeExpr(ast, context) {
  1895. return ast;
  1896. }
  1897. visitTypeofExpr(ast, context) {
  1898. return this.visitExpression(ast, context);
  1899. }
  1900. visitReadVarExpr(ast, context) {
  1901. return this.visitExpression(ast, context);
  1902. }
  1903. visitWriteVarExpr(ast, context) {
  1904. ast.value.visitExpression(this, context);
  1905. return this.visitExpression(ast, context);
  1906. }
  1907. visitWriteKeyExpr(ast, context) {
  1908. ast.receiver.visitExpression(this, context);
  1909. ast.index.visitExpression(this, context);
  1910. ast.value.visitExpression(this, context);
  1911. return this.visitExpression(ast, context);
  1912. }
  1913. visitWritePropExpr(ast, context) {
  1914. ast.receiver.visitExpression(this, context);
  1915. ast.value.visitExpression(this, context);
  1916. return this.visitExpression(ast, context);
  1917. }
  1918. visitInvokeFunctionExpr(ast, context) {
  1919. ast.fn.visitExpression(this, context);
  1920. this.visitAllExpressions(ast.args, context);
  1921. return this.visitExpression(ast, context);
  1922. }
  1923. visitTaggedTemplateExpr(ast, context) {
  1924. ast.tag.visitExpression(this, context);
  1925. this.visitAllExpressions(ast.template.expressions, context);
  1926. return this.visitExpression(ast, context);
  1927. }
  1928. visitInstantiateExpr(ast, context) {
  1929. ast.classExpr.visitExpression(this, context);
  1930. this.visitAllExpressions(ast.args, context);
  1931. return this.visitExpression(ast, context);
  1932. }
  1933. visitLiteralExpr(ast, context) {
  1934. return this.visitExpression(ast, context);
  1935. }
  1936. visitLocalizedString(ast, context) {
  1937. return this.visitExpression(ast, context);
  1938. }
  1939. visitExternalExpr(ast, context) {
  1940. if (ast.typeParams) {
  1941. ast.typeParams.forEach(type => type.visitType(this, context));
  1942. }
  1943. return this.visitExpression(ast, context);
  1944. }
  1945. visitConditionalExpr(ast, context) {
  1946. ast.condition.visitExpression(this, context);
  1947. ast.trueCase.visitExpression(this, context);
  1948. ast.falseCase.visitExpression(this, context);
  1949. return this.visitExpression(ast, context);
  1950. }
  1951. visitNotExpr(ast, context) {
  1952. ast.condition.visitExpression(this, context);
  1953. return this.visitExpression(ast, context);
  1954. }
  1955. visitFunctionExpr(ast, context) {
  1956. this.visitAllStatements(ast.statements, context);
  1957. return this.visitExpression(ast, context);
  1958. }
  1959. visitUnaryOperatorExpr(ast, context) {
  1960. ast.expr.visitExpression(this, context);
  1961. return this.visitExpression(ast, context);
  1962. }
  1963. visitBinaryOperatorExpr(ast, context) {
  1964. ast.lhs.visitExpression(this, context);
  1965. ast.rhs.visitExpression(this, context);
  1966. return this.visitExpression(ast, context);
  1967. }
  1968. visitReadPropExpr(ast, context) {
  1969. ast.receiver.visitExpression(this, context);
  1970. return this.visitExpression(ast, context);
  1971. }
  1972. visitReadKeyExpr(ast, context) {
  1973. ast.receiver.visitExpression(this, context);
  1974. ast.index.visitExpression(this, context);
  1975. return this.visitExpression(ast, context);
  1976. }
  1977. visitLiteralArrayExpr(ast, context) {
  1978. this.visitAllExpressions(ast.entries, context);
  1979. return this.visitExpression(ast, context);
  1980. }
  1981. visitLiteralMapExpr(ast, context) {
  1982. ast.entries.forEach((entry) => entry.value.visitExpression(this, context));
  1983. return this.visitExpression(ast, context);
  1984. }
  1985. visitCommaExpr(ast, context) {
  1986. this.visitAllExpressions(ast.parts, context);
  1987. return this.visitExpression(ast, context);
  1988. }
  1989. visitAllExpressions(exprs, context) {
  1990. exprs.forEach(expr => expr.visitExpression(this, context));
  1991. }
  1992. visitDeclareVarStmt(stmt, context) {
  1993. if (stmt.value) {
  1994. stmt.value.visitExpression(this, context);
  1995. }
  1996. if (stmt.type) {
  1997. stmt.type.visitType(this, context);
  1998. }
  1999. return stmt;
  2000. }
  2001. visitDeclareFunctionStmt(stmt, context) {
  2002. this.visitAllStatements(stmt.statements, context);
  2003. if (stmt.type) {
  2004. stmt.type.visitType(this, context);
  2005. }
  2006. return stmt;
  2007. }
  2008. visitExpressionStmt(stmt, context) {
  2009. stmt.expr.visitExpression(this, context);
  2010. return stmt;
  2011. }
  2012. visitReturnStmt(stmt, context) {
  2013. stmt.value.visitExpression(this, context);
  2014. return stmt;
  2015. }
  2016. visitIfStmt(stmt, context) {
  2017. stmt.condition.visitExpression(this, context);
  2018. this.visitAllStatements(stmt.trueCase, context);
  2019. this.visitAllStatements(stmt.falseCase, context);
  2020. return stmt;
  2021. }
  2022. visitAllStatements(stmts, context) {
  2023. stmts.forEach(stmt => stmt.visitStatement(this, context));
  2024. }
  2025. }
  2026. function leadingComment(text, multiline = false, trailingNewline = true) {
  2027. return new LeadingComment(text, multiline, trailingNewline);
  2028. }
  2029. function jsDocComment(tags = []) {
  2030. return new JSDocComment(tags);
  2031. }
  2032. function variable(name, type, sourceSpan) {
  2033. return new ReadVarExpr(name, type, sourceSpan);
  2034. }
  2035. function importExpr(id, typeParams = null, sourceSpan) {
  2036. return new ExternalExpr(id, null, typeParams, sourceSpan);
  2037. }
  2038. function importType(id, typeParams, typeModifiers) {
  2039. return id != null ? expressionType(importExpr(id, typeParams, null), typeModifiers) : null;
  2040. }
  2041. function expressionType(expr, typeModifiers, typeParams) {
  2042. return new ExpressionType(expr, typeModifiers, typeParams);
  2043. }
  2044. function transplantedType(type, typeModifiers) {
  2045. return new TransplantedType(type, typeModifiers);
  2046. }
  2047. function typeofExpr(expr) {
  2048. return new TypeofExpr(expr);
  2049. }
  2050. function literalArr(values, type, sourceSpan) {
  2051. return new LiteralArrayExpr(values, type, sourceSpan);
  2052. }
  2053. function literalMap(values, type = null) {
  2054. return new LiteralMapExpr(values.map(e => new LiteralMapEntry(e.key, e.value, e.quoted)), type, null);
  2055. }
  2056. function unary(operator, expr, type, sourceSpan) {
  2057. return new UnaryOperatorExpr(operator, expr, type, sourceSpan);
  2058. }
  2059. function not(expr, sourceSpan) {
  2060. return new NotExpr(expr, sourceSpan);
  2061. }
  2062. function fn(params, body, type, sourceSpan, name) {
  2063. return new FunctionExpr(params, body, type, sourceSpan, name);
  2064. }
  2065. function ifStmt(condition, thenClause, elseClause, sourceSpan, leadingComments) {
  2066. return new IfStmt(condition, thenClause, elseClause, sourceSpan, leadingComments);
  2067. }
  2068. function taggedTemplate(tag, template, type, sourceSpan) {
  2069. return new TaggedTemplateExpr(tag, template, type, sourceSpan);
  2070. }
  2071. function literal(value, type, sourceSpan) {
  2072. return new LiteralExpr(value, type, sourceSpan);
  2073. }
  2074. function localizedString(metaBlock, messageParts, placeholderNames, expressions, sourceSpan) {
  2075. return new LocalizedString(metaBlock, messageParts, placeholderNames, expressions, sourceSpan);
  2076. }
  2077. function isNull(exp) {
  2078. return exp instanceof LiteralExpr && exp.value === null;
  2079. }
  2080. /*
  2081. * Serializes a `Tag` into a string.
  2082. * Returns a string like " @foo {bar} baz" (note the leading whitespace before `@foo`).
  2083. */
  2084. function tagToString(tag) {
  2085. let out = '';
  2086. if (tag.tagName) {
  2087. out += ` @${tag.tagName}`;
  2088. }
  2089. if (tag.text) {
  2090. if (tag.text.match(/\/\*|\*\//)) {
  2091. throw new Error('JSDoc text cannot contain "/*" and "*/"');
  2092. }
  2093. out += ' ' + tag.text.replace(/@/g, '\\@');
  2094. }
  2095. return out;
  2096. }
  2097. function serializeTags(tags) {
  2098. if (tags.length === 0)
  2099. return '';
  2100. if (tags.length === 1 && tags[0].tagName && !tags[0].text) {
  2101. // The JSDOC comment is a single simple tag: e.g `/** @tagname */`.
  2102. return `*${tagToString(tags[0])} `;
  2103. }
  2104. let out = '*\n';
  2105. for (const tag of tags) {
  2106. out += ' *';
  2107. // If the tagToString is multi-line, insert " * " prefixes on lines.
  2108. out += tagToString(tag).replace(/\n/g, '\n * ');
  2109. out += '\n';
  2110. }
  2111. out += ' ';
  2112. return out;
  2113. }
  2114. var output_ast = /*#__PURE__*/Object.freeze({
  2115. __proto__: null,
  2116. get TypeModifier () { return TypeModifier; },
  2117. Type: Type,
  2118. get BuiltinTypeName () { return BuiltinTypeName; },
  2119. BuiltinType: BuiltinType,
  2120. ExpressionType: ExpressionType,
  2121. ArrayType: ArrayType,
  2122. MapType: MapType,
  2123. TransplantedType: TransplantedType,
  2124. DYNAMIC_TYPE: DYNAMIC_TYPE,
  2125. INFERRED_TYPE: INFERRED_TYPE,
  2126. BOOL_TYPE: BOOL_TYPE,
  2127. INT_TYPE: INT_TYPE,
  2128. NUMBER_TYPE: NUMBER_TYPE,
  2129. STRING_TYPE: STRING_TYPE,
  2130. FUNCTION_TYPE: FUNCTION_TYPE,
  2131. NONE_TYPE: NONE_TYPE,
  2132. get UnaryOperator () { return UnaryOperator; },
  2133. get BinaryOperator () { return BinaryOperator; },
  2134. nullSafeIsEquivalent: nullSafeIsEquivalent,
  2135. areAllEquivalent: areAllEquivalent,
  2136. Expression: Expression,
  2137. ReadVarExpr: ReadVarExpr,
  2138. TypeofExpr: TypeofExpr,
  2139. WrappedNodeExpr: WrappedNodeExpr,
  2140. WriteVarExpr: WriteVarExpr,
  2141. WriteKeyExpr: WriteKeyExpr,
  2142. WritePropExpr: WritePropExpr,
  2143. InvokeFunctionExpr: InvokeFunctionExpr,
  2144. TaggedTemplateExpr: TaggedTemplateExpr,
  2145. InstantiateExpr: InstantiateExpr,
  2146. LiteralExpr: LiteralExpr,
  2147. TemplateLiteral: TemplateLiteral,
  2148. TemplateLiteralElement: TemplateLiteralElement,
  2149. LiteralPiece: LiteralPiece,
  2150. PlaceholderPiece: PlaceholderPiece,
  2151. LocalizedString: LocalizedString,
  2152. ExternalExpr: ExternalExpr,
  2153. ExternalReference: ExternalReference,
  2154. ConditionalExpr: ConditionalExpr,
  2155. NotExpr: NotExpr,
  2156. FnParam: FnParam,
  2157. FunctionExpr: FunctionExpr,
  2158. UnaryOperatorExpr: UnaryOperatorExpr,
  2159. BinaryOperatorExpr: BinaryOperatorExpr,
  2160. ReadPropExpr: ReadPropExpr,
  2161. ReadKeyExpr: ReadKeyExpr,
  2162. LiteralArrayExpr: LiteralArrayExpr,
  2163. LiteralMapEntry: LiteralMapEntry,
  2164. LiteralMapExpr: LiteralMapExpr,
  2165. CommaExpr: CommaExpr,
  2166. NULL_EXPR: NULL_EXPR,
  2167. TYPED_NULL_EXPR: TYPED_NULL_EXPR,
  2168. get StmtModifier () { return StmtModifier; },
  2169. LeadingComment: LeadingComment,
  2170. JSDocComment: JSDocComment,
  2171. Statement: Statement,
  2172. DeclareVarStmt: DeclareVarStmt,
  2173. DeclareFunctionStmt: DeclareFunctionStmt,
  2174. ExpressionStatement: ExpressionStatement,
  2175. ReturnStatement: ReturnStatement,
  2176. IfStmt: IfStmt,
  2177. RecursiveAstVisitor: RecursiveAstVisitor$1,
  2178. leadingComment: leadingComment,
  2179. jsDocComment: jsDocComment,
  2180. variable: variable,
  2181. importExpr: importExpr,
  2182. importType: importType,
  2183. expressionType: expressionType,
  2184. transplantedType: transplantedType,
  2185. typeofExpr: typeofExpr,
  2186. literalArr: literalArr,
  2187. literalMap: literalMap,
  2188. unary: unary,
  2189. not: not,
  2190. fn: fn,
  2191. ifStmt: ifStmt,
  2192. taggedTemplate: taggedTemplate,
  2193. literal: literal,
  2194. localizedString: localizedString,
  2195. isNull: isNull
  2196. });
  2197. const CONSTANT_PREFIX = '_c';
  2198. /**
  2199. * `ConstantPool` tries to reuse literal factories when two or more literals are identical.
  2200. * We determine whether literals are identical by creating a key out of their AST using the
  2201. * `KeyVisitor`. This constant is used to replace dynamic expressions which can't be safely
  2202. * converted into a key. E.g. given an expression `{foo: bar()}`, since we don't know what
  2203. * the result of `bar` will be, we create a key that looks like `{foo: <unknown>}`. Note
  2204. * that we use a variable, rather than something like `null` in order to avoid collisions.
  2205. */
  2206. const UNKNOWN_VALUE_KEY = variable('<unknown>');
  2207. /**
  2208. * Context to use when producing a key.
  2209. *
  2210. * This ensures we see the constant not the reference variable when producing
  2211. * a key.
  2212. */
  2213. const KEY_CONTEXT = {};
  2214. /**
  2215. * Generally all primitive values are excluded from the `ConstantPool`, but there is an exclusion
  2216. * for strings that reach a certain length threshold. This constant defines the length threshold for
  2217. * strings.
  2218. */
  2219. const POOL_INCLUSION_LENGTH_THRESHOLD_FOR_STRINGS = 50;
  2220. /**
  2221. * A node that is a place-holder that allows the node to be replaced when the actual
  2222. * node is known.
  2223. *
  2224. * This allows the constant pool to change an expression from a direct reference to
  2225. * a constant to a shared constant. It returns a fix-up node that is later allowed to
  2226. * change the referenced expression.
  2227. */
  2228. class FixupExpression extends Expression {
  2229. constructor(resolved) {
  2230. super(resolved.type);
  2231. this.resolved = resolved;
  2232. this.shared = false;
  2233. this.original = resolved;
  2234. }
  2235. visitExpression(visitor, context) {
  2236. if (context === KEY_CONTEXT) {
  2237. // When producing a key we want to traverse the constant not the
  2238. // variable used to refer to it.
  2239. return this.original.visitExpression(visitor, context);
  2240. }
  2241. else {
  2242. return this.resolved.visitExpression(visitor, context);
  2243. }
  2244. }
  2245. isEquivalent(e) {
  2246. return e instanceof FixupExpression && this.resolved.isEquivalent(e.resolved);
  2247. }
  2248. isConstant() {
  2249. return true;
  2250. }
  2251. fixup(expression) {
  2252. this.resolved = expression;
  2253. this.shared = true;
  2254. }
  2255. }
  2256. /**
  2257. * A constant pool allows a code emitter to share constant in an output context.
  2258. *
  2259. * The constant pool also supports sharing access to ivy definitions references.
  2260. */
  2261. class ConstantPool {
  2262. constructor(isClosureCompilerEnabled = false) {
  2263. this.isClosureCompilerEnabled = isClosureCompilerEnabled;
  2264. this.statements = [];
  2265. this.literals = new Map();
  2266. this.literalFactories = new Map();
  2267. this.nextNameIndex = 0;
  2268. }
  2269. getConstLiteral(literal, forceShared) {
  2270. if ((literal instanceof LiteralExpr && !isLongStringLiteral(literal)) ||
  2271. literal instanceof FixupExpression) {
  2272. // Do no put simple literals into the constant pool or try to produce a constant for a
  2273. // reference to a constant.
  2274. return literal;
  2275. }
  2276. const key = this.keyOf(literal);
  2277. let fixup = this.literals.get(key);
  2278. let newValue = false;
  2279. if (!fixup) {
  2280. fixup = new FixupExpression(literal);
  2281. this.literals.set(key, fixup);
  2282. newValue = true;
  2283. }
  2284. if ((!newValue && !fixup.shared) || (newValue && forceShared)) {
  2285. // Replace the expression with a variable
  2286. const name = this.freshName();
  2287. let definition;
  2288. let usage;
  2289. if (this.isClosureCompilerEnabled && isLongStringLiteral(literal)) {
  2290. // For string literals, Closure will **always** inline the string at
  2291. // **all** usages, duplicating it each time. For large strings, this
  2292. // unnecessarily bloats bundle size. To work around this restriction, we
  2293. // wrap the string in a function, and call that function for each usage.
  2294. // This tricks Closure into using inline logic for functions instead of
  2295. // string literals. Function calls are only inlined if the body is small
  2296. // enough to be worth it. By doing this, very large strings will be
  2297. // shared across multiple usages, rather than duplicating the string at
  2298. // each usage site.
  2299. //
  2300. // const myStr = function() { return "very very very long string"; };
  2301. // const usage1 = myStr();
  2302. // const usage2 = myStr();
  2303. definition = variable(name).set(new FunctionExpr([], // Params.
  2304. [
  2305. // Statements.
  2306. new ReturnStatement(literal),
  2307. ]));
  2308. usage = variable(name).callFn([]);
  2309. }
  2310. else {
  2311. // Just declare and use the variable directly, without a function call
  2312. // indirection. This saves a few bytes and avoids an unnecessary call.
  2313. definition = variable(name).set(literal);
  2314. usage = variable(name);
  2315. }
  2316. this.statements.push(definition.toDeclStmt(INFERRED_TYPE, StmtModifier.Final));
  2317. fixup.fixup(usage);
  2318. }
  2319. return fixup;
  2320. }
  2321. getLiteralFactory(literal) {
  2322. // Create a pure function that builds an array of a mix of constant and variable expressions
  2323. if (literal instanceof LiteralArrayExpr) {
  2324. const argumentsForKey = literal.entries.map(e => e.isConstant() ? e : UNKNOWN_VALUE_KEY);
  2325. const key = this.keyOf(literalArr(argumentsForKey));
  2326. return this._getLiteralFactory(key, literal.entries, entries => literalArr(entries));
  2327. }
  2328. else {
  2329. const expressionForKey = literalMap(literal.entries.map(e => ({
  2330. key: e.key,
  2331. value: e.value.isConstant() ? e.value : UNKNOWN_VALUE_KEY,
  2332. quoted: e.quoted
  2333. })));
  2334. const key = this.keyOf(expressionForKey);
  2335. return this._getLiteralFactory(key, literal.entries.map(e => e.value), entries => literalMap(entries.map((value, index) => ({
  2336. key: literal.entries[index].key,
  2337. value,
  2338. quoted: literal.entries[index].quoted
  2339. }))));
  2340. }
  2341. }
  2342. _getLiteralFactory(key, values, resultMap) {
  2343. let literalFactory = this.literalFactories.get(key);
  2344. const literalFactoryArguments = values.filter((e => !e.isConstant()));
  2345. if (!literalFactory) {
  2346. const resultExpressions = values.map((e, index) => e.isConstant() ? this.getConstLiteral(e, true) : variable(`a${index}`));
  2347. const parameters = resultExpressions.filter(isVariable).map(e => new FnParam(e.name, DYNAMIC_TYPE));
  2348. const pureFunctionDeclaration = fn(parameters, [new ReturnStatement(resultMap(resultExpressions))], INFERRED_TYPE);
  2349. const name = this.freshName();
  2350. this.statements.push(variable(name)
  2351. .set(pureFunctionDeclaration)
  2352. .toDeclStmt(INFERRED_TYPE, StmtModifier.Final));
  2353. literalFactory = variable(name);
  2354. this.literalFactories.set(key, literalFactory);
  2355. }
  2356. return { literalFactory, literalFactoryArguments };
  2357. }
  2358. /**
  2359. * Produce a unique name.
  2360. *
  2361. * The name might be unique among different prefixes if any of the prefixes end in
  2362. * a digit so the prefix should be a constant string (not based on user input) and
  2363. * must not end in a digit.
  2364. */
  2365. uniqueName(prefix) {
  2366. return `${prefix}${this.nextNameIndex++}`;
  2367. }
  2368. freshName() {
  2369. return this.uniqueName(CONSTANT_PREFIX);
  2370. }
  2371. keyOf(expression) {
  2372. return expression.visitExpression(new KeyVisitor(), KEY_CONTEXT);
  2373. }
  2374. }
  2375. /**
  2376. * Visitor used to determine if 2 expressions are equivalent and can be shared in the
  2377. * `ConstantPool`.
  2378. *
  2379. * When the id (string) generated by the visitor is equal, expressions are considered equivalent.
  2380. */
  2381. class KeyVisitor {
  2382. constructor() {
  2383. this.visitWrappedNodeExpr = invalid$1;
  2384. this.visitWriteVarExpr = invalid$1;
  2385. this.visitWriteKeyExpr = invalid$1;
  2386. this.visitWritePropExpr = invalid$1;
  2387. this.visitInvokeFunctionExpr = invalid$1;
  2388. this.visitTaggedTemplateExpr = invalid$1;
  2389. this.visitInstantiateExpr = invalid$1;
  2390. this.visitConditionalExpr = invalid$1;
  2391. this.visitNotExpr = invalid$1;
  2392. this.visitAssertNotNullExpr = invalid$1;
  2393. this.visitCastExpr = invalid$1;
  2394. this.visitFunctionExpr = invalid$1;
  2395. this.visitUnaryOperatorExpr = invalid$1;
  2396. this.visitBinaryOperatorExpr = invalid$1;
  2397. this.visitReadPropExpr = invalid$1;
  2398. this.visitReadKeyExpr = invalid$1;
  2399. this.visitCommaExpr = invalid$1;
  2400. this.visitLocalizedString = invalid$1;
  2401. }
  2402. visitLiteralExpr(ast) {
  2403. return `${typeof ast.value === 'string' ? '"' + ast.value + '"' : ast.value}`;
  2404. }
  2405. visitLiteralArrayExpr(ast, context) {
  2406. return `[${ast.entries.map(entry => entry.visitExpression(this, context)).join(',')}]`;
  2407. }
  2408. visitLiteralMapExpr(ast, context) {
  2409. const mapKey = (entry) => {
  2410. const quote = entry.quoted ? '"' : '';
  2411. return `${quote}${entry.key}${quote}`;
  2412. };
  2413. const mapEntry = (entry) => `${mapKey(entry)}:${entry.value.visitExpression(this, context)}`;
  2414. return `{${ast.entries.map(mapEntry).join(',')}`;
  2415. }
  2416. visitExternalExpr(ast) {
  2417. return ast.value.moduleName ? `EX:${ast.value.moduleName}:${ast.value.name}` :
  2418. `EX:${ast.value.runtime.name}`;
  2419. }
  2420. visitReadVarExpr(node) {
  2421. return `VAR:${node.name}`;
  2422. }
  2423. visitTypeofExpr(node, context) {
  2424. return `TYPEOF:${node.expr.visitExpression(this, context)}`;
  2425. }
  2426. }
  2427. function invalid$1(arg) {
  2428. throw new Error(`Invalid state: Visitor ${this.constructor.name} doesn't handle ${arg.constructor.name}`);
  2429. }
  2430. function isVariable(e) {
  2431. return e instanceof ReadVarExpr;
  2432. }
  2433. function isLongStringLiteral(expr) {
  2434. return expr instanceof LiteralExpr && typeof expr.value === 'string' &&
  2435. expr.value.length >= POOL_INCLUSION_LENGTH_THRESHOLD_FOR_STRINGS;
  2436. }
  2437. const CORE = '@angular/core';
  2438. class Identifiers {
  2439. /* Methods */
  2440. static { this.NEW_METHOD = 'factory'; }
  2441. static { this.TRANSFORM_METHOD = 'transform'; }
  2442. static { this.PATCH_DEPS = 'patchedDeps'; }
  2443. static { this.core = { name: null, moduleName: CORE }; }
  2444. /* Instructions */
  2445. static { this.namespaceHTML = { name: 'ɵɵnamespaceHTML', moduleName: CORE }; }
  2446. static { this.namespaceMathML = { name: 'ɵɵnamespaceMathML', moduleName: CORE }; }
  2447. static { this.namespaceSVG = { name: 'ɵɵnamespaceSVG', moduleName: CORE }; }
  2448. static { this.element = { name: 'ɵɵelement', moduleName: CORE }; }
  2449. static { this.elementStart = { name: 'ɵɵelementStart', moduleName: CORE }; }
  2450. static { this.elementEnd = { name: 'ɵɵelementEnd', moduleName: CORE }; }
  2451. static { this.advance = { name: 'ɵɵadvance', moduleName: CORE }; }
  2452. static { this.syntheticHostProperty = { name: 'ɵɵsyntheticHostProperty', moduleName: CORE }; }
  2453. static { this.syntheticHostListener = { name: 'ɵɵsyntheticHostListener', moduleName: CORE }; }
  2454. static { this.attribute = { name: 'ɵɵattribute', moduleName: CORE }; }
  2455. static { this.attributeInterpolate1 = { name: 'ɵɵattributeInterpolate1', moduleName: CORE }; }
  2456. static { this.attributeInterpolate2 = { name: 'ɵɵattributeInterpolate2', moduleName: CORE }; }
  2457. static { this.attributeInterpolate3 = { name: 'ɵɵattributeInterpolate3', moduleName: CORE }; }
  2458. static { this.attributeInterpolate4 = { name: 'ɵɵattributeInterpolate4', moduleName: CORE }; }
  2459. static { this.attributeInterpolate5 = { name: 'ɵɵattributeInterpolate5', moduleName: CORE }; }
  2460. static { this.attributeInterpolate6 = { name: 'ɵɵattributeInterpolate6', moduleName: CORE }; }
  2461. static { this.attributeInterpolate7 = { name: 'ɵɵattributeInterpolate7', moduleName: CORE }; }
  2462. static { this.attributeInterpolate8 = { name: 'ɵɵattributeInterpolate8', moduleName: CORE }; }
  2463. static { this.attributeInterpolateV = { name: 'ɵɵattributeInterpolateV', moduleName: CORE }; }
  2464. static { this.classProp = { name: 'ɵɵclassProp', moduleName: CORE }; }
  2465. static { this.elementContainerStart = { name: 'ɵɵelementContainerStart', moduleName: CORE }; }
  2466. static { this.elementContainerEnd = { name: 'ɵɵelementContainerEnd', moduleName: CORE }; }
  2467. static { this.elementContainer = { name: 'ɵɵelementContainer', moduleName: CORE }; }
  2468. static { this.styleMap = { name: 'ɵɵstyleMap', moduleName: CORE }; }
  2469. static { this.styleMapInterpolate1 = { name: 'ɵɵstyleMapInterpolate1', moduleName: CORE }; }
  2470. static { this.styleMapInterpolate2 = { name: 'ɵɵstyleMapInterpolate2', moduleName: CORE }; }
  2471. static { this.styleMapInterpolate3 = { name: 'ɵɵstyleMapInterpolate3', moduleName: CORE }; }
  2472. static { this.styleMapInterpolate4 = { name: 'ɵɵstyleMapInterpolate4', moduleName: CORE }; }
  2473. static { this.styleMapInterpolate5 = { name: 'ɵɵstyleMapInterpolate5', moduleName: CORE }; }
  2474. static { this.styleMapInterpolate6 = { name: 'ɵɵstyleMapInterpolate6', moduleName: CORE }; }
  2475. static { this.styleMapInterpolate7 = { name: 'ɵɵstyleMapInterpolate7', moduleName: CORE }; }
  2476. static { this.styleMapInterpolate8 = { name: 'ɵɵstyleMapInterpolate8', moduleName: CORE }; }
  2477. static { this.styleMapInterpolateV = { name: 'ɵɵstyleMapInterpolateV', moduleName: CORE }; }
  2478. static { this.classMap = { name: 'ɵɵclassMap', moduleName: CORE }; }
  2479. static { this.classMapInterpolate1 = { name: 'ɵɵclassMapInterpolate1', moduleName: CORE }; }
  2480. static { this.classMapInterpolate2 = { name: 'ɵɵclassMapInterpolate2', moduleName: CORE }; }
  2481. static { this.classMapInterpolate3 = { name: 'ɵɵclassMapInterpolate3', moduleName: CORE }; }
  2482. static { this.classMapInterpolate4 = { name: 'ɵɵclassMapInterpolate4', moduleName: CORE }; }
  2483. static { this.classMapInterpolate5 = { name: 'ɵɵclassMapInterpolate5', moduleName: CORE }; }
  2484. static { this.classMapInterpolate6 = { name: 'ɵɵclassMapInterpolate6', moduleName: CORE }; }
  2485. static { this.classMapInterpolate7 = { name: 'ɵɵclassMapInterpolate7', moduleName: CORE }; }
  2486. static { this.classMapInterpolate8 = { name: 'ɵɵclassMapInterpolate8', moduleName: CORE }; }
  2487. static { this.classMapInterpolateV = { name: 'ɵɵclassMapInterpolateV', moduleName: CORE }; }
  2488. static { this.styleProp = { name: 'ɵɵstyleProp', moduleName: CORE }; }
  2489. static { this.stylePropInterpolate1 = { name: 'ɵɵstylePropInterpolate1', moduleName: CORE }; }
  2490. static { this.stylePropInterpolate2 = { name: 'ɵɵstylePropInterpolate2', moduleName: CORE }; }
  2491. static { this.stylePropInterpolate3 = { name: 'ɵɵstylePropInterpolate3', moduleName: CORE }; }
  2492. static { this.stylePropInterpolate4 = { name: 'ɵɵstylePropInterpolate4', moduleName: CORE }; }
  2493. static { this.stylePropInterpolate5 = { name: 'ɵɵstylePropInterpolate5', moduleName: CORE }; }
  2494. static { this.stylePropInterpolate6 = { name: 'ɵɵstylePropInterpolate6', moduleName: CORE }; }
  2495. static { this.stylePropInterpolate7 = { name: 'ɵɵstylePropInterpolate7', moduleName: CORE }; }
  2496. static { this.stylePropInterpolate8 = { name: 'ɵɵstylePropInterpolate8', moduleName: CORE }; }
  2497. static { this.stylePropInterpolateV = { name: 'ɵɵstylePropInterpolateV', moduleName: CORE }; }
  2498. static { this.nextContext = { name: 'ɵɵnextContext', moduleName: CORE }; }
  2499. static { this.resetView = { name: 'ɵɵresetView', moduleName: CORE }; }
  2500. static { this.templateCreate = { name: 'ɵɵtemplate', moduleName: CORE }; }
  2501. static { this.text = { name: 'ɵɵtext', moduleName: CORE }; }
  2502. static { this.enableBindings = { name: 'ɵɵenableBindings', moduleName: CORE }; }
  2503. static { this.disableBindings = { name: 'ɵɵdisableBindings', moduleName: CORE }; }
  2504. static { this.getCurrentView = { name: 'ɵɵgetCurrentView', moduleName: CORE }; }
  2505. static { this.textInterpolate = { name: 'ɵɵtextInterpolate', moduleName: CORE }; }
  2506. static { this.textInterpolate1 = { name: 'ɵɵtextInterpolate1', moduleName: CORE }; }
  2507. static { this.textInterpolate2 = { name: 'ɵɵtextInterpolate2', moduleName: CORE }; }
  2508. static { this.textInterpolate3 = { name: 'ɵɵtextInterpolate3', moduleName: CORE }; }
  2509. static { this.textInterpolate4 = { name: 'ɵɵtextInterpolate4', moduleName: CORE }; }
  2510. static { this.textInterpolate5 = { name: 'ɵɵtextInterpolate5', moduleName: CORE }; }
  2511. static { this.textInterpolate6 = { name: 'ɵɵtextInterpolate6', moduleName: CORE }; }
  2512. static { this.textInterpolate7 = { name: 'ɵɵtextInterpolate7', moduleName: CORE }; }
  2513. static { this.textInterpolate8 = { name: 'ɵɵtextInterpolate8', moduleName: CORE }; }
  2514. static { this.textInterpolateV = { name: 'ɵɵtextInterpolateV', moduleName: CORE }; }
  2515. static { this.restoreView = { name: 'ɵɵrestoreView', moduleName: CORE }; }
  2516. static { this.pureFunction0 = { name: 'ɵɵpureFunction0', moduleName: CORE }; }
  2517. static { this.pureFunction1 = { name: 'ɵɵpureFunction1', moduleName: CORE }; }
  2518. static { this.pureFunction2 = { name: 'ɵɵpureFunction2', moduleName: CORE }; }
  2519. static { this.pureFunction3 = { name: 'ɵɵpureFunction3', moduleName: CORE }; }
  2520. static { this.pureFunction4 = { name: 'ɵɵpureFunction4', moduleName: CORE }; }
  2521. static { this.pureFunction5 = { name: 'ɵɵpureFunction5', moduleName: CORE }; }
  2522. static { this.pureFunction6 = { name: 'ɵɵpureFunction6', moduleName: CORE }; }
  2523. static { this.pureFunction7 = { name: 'ɵɵpureFunction7', moduleName: CORE }; }
  2524. static { this.pureFunction8 = { name: 'ɵɵpureFunction8', moduleName: CORE }; }
  2525. static { this.pureFunctionV = { name: 'ɵɵpureFunctionV', moduleName: CORE }; }
  2526. static { this.pipeBind1 = { name: 'ɵɵpipeBind1', moduleName: CORE }; }
  2527. static { this.pipeBind2 = { name: 'ɵɵpipeBind2', moduleName: CORE }; }
  2528. static { this.pipeBind3 = { name: 'ɵɵpipeBind3', moduleName: CORE }; }
  2529. static { this.pipeBind4 = { name: 'ɵɵpipeBind4', moduleName: CORE }; }
  2530. static { this.pipeBindV = { name: 'ɵɵpipeBindV', moduleName: CORE }; }
  2531. static { this.hostProperty = { name: 'ɵɵhostProperty', moduleName: CORE }; }
  2532. static { this.property = { name: 'ɵɵproperty', moduleName: CORE }; }
  2533. static { this.propertyInterpolate = { name: 'ɵɵpropertyInterpolate', moduleName: CORE }; }
  2534. static { this.propertyInterpolate1 = { name: 'ɵɵpropertyInterpolate1', moduleName: CORE }; }
  2535. static { this.propertyInterpolate2 = { name: 'ɵɵpropertyInterpolate2', moduleName: CORE }; }
  2536. static { this.propertyInterpolate3 = { name: 'ɵɵpropertyInterpolate3', moduleName: CORE }; }
  2537. static { this.propertyInterpolate4 = { name: 'ɵɵpropertyInterpolate4', moduleName: CORE }; }
  2538. static { this.propertyInterpolate5 = { name: 'ɵɵpropertyInterpolate5', moduleName: CORE }; }
  2539. static { this.propertyInterpolate6 = { name: 'ɵɵpropertyInterpolate6', moduleName: CORE }; }
  2540. static { this.propertyInterpolate7 = { name: 'ɵɵpropertyInterpolate7', moduleName: CORE }; }
  2541. static { this.propertyInterpolate8 = { name: 'ɵɵpropertyInterpolate8', moduleName: CORE }; }
  2542. static { this.propertyInterpolateV = { name: 'ɵɵpropertyInterpolateV', moduleName: CORE }; }
  2543. static { this.i18n = { name: 'ɵɵi18n', moduleName: CORE }; }
  2544. static { this.i18nAttributes = { name: 'ɵɵi18nAttributes', moduleName: CORE }; }
  2545. static { this.i18nExp = { name: 'ɵɵi18nExp', moduleName: CORE }; }
  2546. static { this.i18nStart = { name: 'ɵɵi18nStart', moduleName: CORE }; }
  2547. static { this.i18nEnd = { name: 'ɵɵi18nEnd', moduleName: CORE }; }
  2548. static { this.i18nApply = { name: 'ɵɵi18nApply', moduleName: CORE }; }
  2549. static { this.i18nPostprocess = { name: 'ɵɵi18nPostprocess', moduleName: CORE }; }
  2550. static { this.pipe = { name: 'ɵɵpipe', moduleName: CORE }; }
  2551. static { this.projection = { name: 'ɵɵprojection', moduleName: CORE }; }
  2552. static { this.projectionDef = { name: 'ɵɵprojectionDef', moduleName: CORE }; }
  2553. static { this.reference = { name: 'ɵɵreference', moduleName: CORE }; }
  2554. static { this.inject = { name: 'ɵɵinject', moduleName: CORE }; }
  2555. static { this.injectAttribute = { name: 'ɵɵinjectAttribute', moduleName: CORE }; }
  2556. static { this.directiveInject = { name: 'ɵɵdirectiveInject', moduleName: CORE }; }
  2557. static { this.invalidFactory = { name: 'ɵɵinvalidFactory', moduleName: CORE }; }
  2558. static { this.invalidFactoryDep = { name: 'ɵɵinvalidFactoryDep', moduleName: CORE }; }
  2559. static { this.templateRefExtractor = { name: 'ɵɵtemplateRefExtractor', moduleName: CORE }; }
  2560. static { this.forwardRef = { name: 'forwardRef', moduleName: CORE }; }
  2561. static { this.resolveForwardRef = { name: 'resolveForwardRef', moduleName: CORE }; }
  2562. static { this.ɵɵdefineInjectable = { name: 'ɵɵdefineInjectable', moduleName: CORE }; }
  2563. static { this.declareInjectable = { name: 'ɵɵngDeclareInjectable', moduleName: CORE }; }
  2564. static { this.InjectableDeclaration = { name: 'ɵɵInjectableDeclaration', moduleName: CORE }; }
  2565. static { this.resolveWindow = { name: 'ɵɵresolveWindow', moduleName: CORE }; }
  2566. static { this.resolveDocument = { name: 'ɵɵresolveDocument', moduleName: CORE }; }
  2567. static { this.resolveBody = { name: 'ɵɵresolveBody', moduleName: CORE }; }
  2568. static { this.defineComponent = { name: 'ɵɵdefineComponent', moduleName: CORE }; }
  2569. static { this.declareComponent = { name: 'ɵɵngDeclareComponent', moduleName: CORE }; }
  2570. static { this.setComponentScope = { name: 'ɵɵsetComponentScope', moduleName: CORE }; }
  2571. static { this.ChangeDetectionStrategy = {
  2572. name: 'ChangeDetectionStrategy',
  2573. moduleName: CORE,
  2574. }; }
  2575. static { this.ViewEncapsulation = {
  2576. name: 'ViewEncapsulation',
  2577. moduleName: CORE,
  2578. }; }
  2579. static { this.ComponentDeclaration = {
  2580. name: 'ɵɵComponentDeclaration',
  2581. moduleName: CORE,
  2582. }; }
  2583. static { this.FactoryDeclaration = {
  2584. name: 'ɵɵFactoryDeclaration',
  2585. moduleName: CORE,
  2586. }; }
  2587. static { this.declareFactory = { name: 'ɵɵngDeclareFactory', moduleName: CORE }; }
  2588. static { this.FactoryTarget = { name: 'ɵɵFactoryTarget', moduleName: CORE }; }
  2589. static { this.defineDirective = { name: 'ɵɵdefineDirective', moduleName: CORE }; }
  2590. static { this.declareDirective = { name: 'ɵɵngDeclareDirective', moduleName: CORE }; }
  2591. static { this.DirectiveDeclaration = {
  2592. name: 'ɵɵDirectiveDeclaration',
  2593. moduleName: CORE,
  2594. }; }
  2595. static { this.InjectorDef = { name: 'ɵɵInjectorDef', moduleName: CORE }; }
  2596. static { this.InjectorDeclaration = { name: 'ɵɵInjectorDeclaration', moduleName: CORE }; }
  2597. static { this.defineInjector = { name: 'ɵɵdefineInjector', moduleName: CORE }; }
  2598. static { this.declareInjector = { name: 'ɵɵngDeclareInjector', moduleName: CORE }; }
  2599. static { this.NgModuleDeclaration = {
  2600. name: 'ɵɵNgModuleDeclaration',
  2601. moduleName: CORE,
  2602. }; }
  2603. static { this.ModuleWithProviders = {
  2604. name: 'ModuleWithProviders',
  2605. moduleName: CORE,
  2606. }; }
  2607. static { this.defineNgModule = { name: 'ɵɵdefineNgModule', moduleName: CORE }; }
  2608. static { this.declareNgModule = { name: 'ɵɵngDeclareNgModule', moduleName: CORE }; }
  2609. static { this.setNgModuleScope = { name: 'ɵɵsetNgModuleScope', moduleName: CORE }; }
  2610. static { this.registerNgModuleType = { name: 'ɵɵregisterNgModuleType', moduleName: CORE }; }
  2611. static { this.PipeDeclaration = { name: 'ɵɵPipeDeclaration', moduleName: CORE }; }
  2612. static { this.definePipe = { name: 'ɵɵdefinePipe', moduleName: CORE }; }
  2613. static { this.declarePipe = { name: 'ɵɵngDeclarePipe', moduleName: CORE }; }
  2614. static { this.declareClassMetadata = { name: 'ɵɵngDeclareClassMetadata', moduleName: CORE }; }
  2615. static { this.setClassMetadata = { name: 'ɵsetClassMetadata', moduleName: CORE }; }
  2616. static { this.queryRefresh = { name: 'ɵɵqueryRefresh', moduleName: CORE }; }
  2617. static { this.viewQuery = { name: 'ɵɵviewQuery', moduleName: CORE }; }
  2618. static { this.loadQuery = { name: 'ɵɵloadQuery', moduleName: CORE }; }
  2619. static { this.contentQuery = { name: 'ɵɵcontentQuery', moduleName: CORE }; }
  2620. static { this.NgOnChangesFeature = { name: 'ɵɵNgOnChangesFeature', moduleName: CORE }; }
  2621. static { this.InheritDefinitionFeature = { name: 'ɵɵInheritDefinitionFeature', moduleName: CORE }; }
  2622. static { this.CopyDefinitionFeature = { name: 'ɵɵCopyDefinitionFeature', moduleName: CORE }; }
  2623. static { this.StandaloneFeature = { name: 'ɵɵStandaloneFeature', moduleName: CORE }; }
  2624. static { this.ProvidersFeature = { name: 'ɵɵProvidersFeature', moduleName: CORE }; }
  2625. static { this.HostDirectivesFeature = { name: 'ɵɵHostDirectivesFeature', moduleName: CORE }; }
  2626. static { this.listener = { name: 'ɵɵlistener', moduleName: CORE }; }
  2627. static { this.getInheritedFactory = {
  2628. name: 'ɵɵgetInheritedFactory',
  2629. moduleName: CORE,
  2630. }; }
  2631. // sanitization-related functions
  2632. static { this.sanitizeHtml = { name: 'ɵɵsanitizeHtml', moduleName: CORE }; }
  2633. static { this.sanitizeStyle = { name: 'ɵɵsanitizeStyle', moduleName: CORE }; }
  2634. static { this.sanitizeResourceUrl = { name: 'ɵɵsanitizeResourceUrl', moduleName: CORE }; }
  2635. static { this.sanitizeScript = { name: 'ɵɵsanitizeScript', moduleName: CORE }; }
  2636. static { this.sanitizeUrl = { name: 'ɵɵsanitizeUrl', moduleName: CORE }; }
  2637. static { this.sanitizeUrlOrResourceUrl = { name: 'ɵɵsanitizeUrlOrResourceUrl', moduleName: CORE }; }
  2638. static { this.trustConstantHtml = { name: 'ɵɵtrustConstantHtml', moduleName: CORE }; }
  2639. static { this.trustConstantResourceUrl = { name: 'ɵɵtrustConstantResourceUrl', moduleName: CORE }; }
  2640. static { this.validateIframeAttribute = { name: 'ɵɵvalidateIframeAttribute', moduleName: CORE }; }
  2641. }
  2642. const DASH_CASE_REGEXP = /-+([a-z0-9])/g;
  2643. function dashCaseToCamelCase(input) {
  2644. return input.replace(DASH_CASE_REGEXP, (...m) => m[1].toUpperCase());
  2645. }
  2646. function splitAtColon(input, defaultValues) {
  2647. return _splitAt(input, ':', defaultValues);
  2648. }
  2649. function splitAtPeriod(input, defaultValues) {
  2650. return _splitAt(input, '.', defaultValues);
  2651. }
  2652. function _splitAt(input, character, defaultValues) {
  2653. const characterIndex = input.indexOf(character);
  2654. if (characterIndex == -1)
  2655. return defaultValues;
  2656. return [input.slice(0, characterIndex).trim(), input.slice(characterIndex + 1).trim()];
  2657. }
  2658. function noUndefined(val) {
  2659. return val === undefined ? null : val;
  2660. }
  2661. function error(msg) {
  2662. throw new Error(`Internal Error: ${msg}`);
  2663. }
  2664. // Escape characters that have a special meaning in Regular Expressions
  2665. function escapeRegExp(s) {
  2666. return s.replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1');
  2667. }
  2668. function utf8Encode(str) {
  2669. let encoded = [];
  2670. for (let index = 0; index < str.length; index++) {
  2671. let codePoint = str.charCodeAt(index);
  2672. // decode surrogate
  2673. // see https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
  2674. if (codePoint >= 0xd800 && codePoint <= 0xdbff && str.length > (index + 1)) {
  2675. const low = str.charCodeAt(index + 1);
  2676. if (low >= 0xdc00 && low <= 0xdfff) {
  2677. index++;
  2678. codePoint = ((codePoint - 0xd800) << 10) + low - 0xdc00 + 0x10000;
  2679. }
  2680. }
  2681. if (codePoint <= 0x7f) {
  2682. encoded.push(codePoint);
  2683. }
  2684. else if (codePoint <= 0x7ff) {
  2685. encoded.push(((codePoint >> 6) & 0x1F) | 0xc0, (codePoint & 0x3f) | 0x80);
  2686. }
  2687. else if (codePoint <= 0xffff) {
  2688. encoded.push((codePoint >> 12) | 0xe0, ((codePoint >> 6) & 0x3f) | 0x80, (codePoint & 0x3f) | 0x80);
  2689. }
  2690. else if (codePoint <= 0x1fffff) {
  2691. encoded.push(((codePoint >> 18) & 0x07) | 0xf0, ((codePoint >> 12) & 0x3f) | 0x80, ((codePoint >> 6) & 0x3f) | 0x80, (codePoint & 0x3f) | 0x80);
  2692. }
  2693. }
  2694. return encoded;
  2695. }
  2696. function stringify(token) {
  2697. if (typeof token === 'string') {
  2698. return token;
  2699. }
  2700. if (Array.isArray(token)) {
  2701. return '[' + token.map(stringify).join(', ') + ']';
  2702. }
  2703. if (token == null) {
  2704. return '' + token;
  2705. }
  2706. if (token.overriddenName) {
  2707. return `${token.overriddenName}`;
  2708. }
  2709. if (token.name) {
  2710. return `${token.name}`;
  2711. }
  2712. if (!token.toString) {
  2713. return 'object';
  2714. }
  2715. // WARNING: do not try to `JSON.stringify(token)` here
  2716. // see https://github.com/angular/angular/issues/23440
  2717. const res = token.toString();
  2718. if (res == null) {
  2719. return '' + res;
  2720. }
  2721. const newLineIndex = res.indexOf('\n');
  2722. return newLineIndex === -1 ? res : res.substring(0, newLineIndex);
  2723. }
  2724. class Version {
  2725. constructor(full) {
  2726. this.full = full;
  2727. const splits = full.split('.');
  2728. this.major = splits[0];
  2729. this.minor = splits[1];
  2730. this.patch = splits.slice(2).join('.');
  2731. }
  2732. }
  2733. // Check `global` first, because in Node tests both `global` and `window` may be defined and our
  2734. // `_global` variable should point to the NodeJS `global` in that case. Note: Typeof/Instanceof
  2735. // checks are considered side-effects in Terser. We explicitly mark this as side-effect free:
  2736. // https://github.com/terser/terser/issues/250.
  2737. const _global = ( /* @__PURE__ */(() => (typeof global !== 'undefined' && global) || (typeof window !== 'undefined' && window) ||
  2738. (typeof self !== 'undefined' && typeof WorkerGlobalScope !== 'undefined' &&
  2739. self instanceof WorkerGlobalScope && self))());
  2740. function newArray(size, value) {
  2741. const list = [];
  2742. for (let i = 0; i < size; i++) {
  2743. list.push(value);
  2744. }
  2745. return list;
  2746. }
  2747. /**
  2748. * Partitions a given array into 2 arrays, based on a boolean value returned by the condition
  2749. * function.
  2750. *
  2751. * @param arr Input array that should be partitioned
  2752. * @param conditionFn Condition function that is called for each item in a given array and returns a
  2753. * boolean value.
  2754. */
  2755. function partitionArray(arr, conditionFn) {
  2756. const truthy = [];
  2757. const falsy = [];
  2758. for (const item of arr) {
  2759. (conditionFn(item) ? truthy : falsy).push(item);
  2760. }
  2761. return [truthy, falsy];
  2762. }
  2763. // https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit
  2764. const VERSION$1 = 3;
  2765. const JS_B64_PREFIX = '# sourceMappingURL=data:application/json;base64,';
  2766. class SourceMapGenerator {
  2767. constructor(file = null) {
  2768. this.file = file;
  2769. this.sourcesContent = new Map();
  2770. this.lines = [];
  2771. this.lastCol0 = 0;
  2772. this.hasMappings = false;
  2773. }
  2774. // The content is `null` when the content is expected to be loaded using the URL
  2775. addSource(url, content = null) {
  2776. if (!this.sourcesContent.has(url)) {
  2777. this.sourcesContent.set(url, content);
  2778. }
  2779. return this;
  2780. }
  2781. addLine() {
  2782. this.lines.push([]);
  2783. this.lastCol0 = 0;
  2784. return this;
  2785. }
  2786. addMapping(col0, sourceUrl, sourceLine0, sourceCol0) {
  2787. if (!this.currentLine) {
  2788. throw new Error(`A line must be added before mappings can be added`);
  2789. }
  2790. if (sourceUrl != null && !this.sourcesContent.has(sourceUrl)) {
  2791. throw new Error(`Unknown source file "${sourceUrl}"`);
  2792. }
  2793. if (col0 == null) {
  2794. throw new Error(`The column in the generated code must be provided`);
  2795. }
  2796. if (col0 < this.lastCol0) {
  2797. throw new Error(`Mapping should be added in output order`);
  2798. }
  2799. if (sourceUrl && (sourceLine0 == null || sourceCol0 == null)) {
  2800. throw new Error(`The source location must be provided when a source url is provided`);
  2801. }
  2802. this.hasMappings = true;
  2803. this.lastCol0 = col0;
  2804. this.currentLine.push({ col0, sourceUrl, sourceLine0, sourceCol0 });
  2805. return this;
  2806. }
  2807. /**
  2808. * @internal strip this from published d.ts files due to
  2809. * https://github.com/microsoft/TypeScript/issues/36216
  2810. */
  2811. get currentLine() {
  2812. return this.lines.slice(-1)[0];
  2813. }
  2814. toJSON() {
  2815. if (!this.hasMappings) {
  2816. return null;
  2817. }
  2818. const sourcesIndex = new Map();
  2819. const sources = [];
  2820. const sourcesContent = [];
  2821. Array.from(this.sourcesContent.keys()).forEach((url, i) => {
  2822. sourcesIndex.set(url, i);
  2823. sources.push(url);
  2824. sourcesContent.push(this.sourcesContent.get(url) || null);
  2825. });
  2826. let mappings = '';
  2827. let lastCol0 = 0;
  2828. let lastSourceIndex = 0;
  2829. let lastSourceLine0 = 0;
  2830. let lastSourceCol0 = 0;
  2831. this.lines.forEach(segments => {
  2832. lastCol0 = 0;
  2833. mappings += segments
  2834. .map(segment => {
  2835. // zero-based starting column of the line in the generated code
  2836. let segAsStr = toBase64VLQ(segment.col0 - lastCol0);
  2837. lastCol0 = segment.col0;
  2838. if (segment.sourceUrl != null) {
  2839. // zero-based index into the “sources” list
  2840. segAsStr +=
  2841. toBase64VLQ(sourcesIndex.get(segment.sourceUrl) - lastSourceIndex);
  2842. lastSourceIndex = sourcesIndex.get(segment.sourceUrl);
  2843. // the zero-based starting line in the original source
  2844. segAsStr += toBase64VLQ(segment.sourceLine0 - lastSourceLine0);
  2845. lastSourceLine0 = segment.sourceLine0;
  2846. // the zero-based starting column in the original source
  2847. segAsStr += toBase64VLQ(segment.sourceCol0 - lastSourceCol0);
  2848. lastSourceCol0 = segment.sourceCol0;
  2849. }
  2850. return segAsStr;
  2851. })
  2852. .join(',');
  2853. mappings += ';';
  2854. });
  2855. mappings = mappings.slice(0, -1);
  2856. return {
  2857. 'file': this.file || '',
  2858. 'version': VERSION$1,
  2859. 'sourceRoot': '',
  2860. 'sources': sources,
  2861. 'sourcesContent': sourcesContent,
  2862. 'mappings': mappings,
  2863. };
  2864. }
  2865. toJsComment() {
  2866. return this.hasMappings ? '//' + JS_B64_PREFIX + toBase64String(JSON.stringify(this, null, 0)) :
  2867. '';
  2868. }
  2869. }
  2870. function toBase64String(value) {
  2871. let b64 = '';
  2872. const encoded = utf8Encode(value);
  2873. for (let i = 0; i < encoded.length;) {
  2874. const i1 = encoded[i++];
  2875. const i2 = i < encoded.length ? encoded[i++] : null;
  2876. const i3 = i < encoded.length ? encoded[i++] : null;
  2877. b64 += toBase64Digit(i1 >> 2);
  2878. b64 += toBase64Digit(((i1 & 3) << 4) | (i2 === null ? 0 : i2 >> 4));
  2879. b64 += i2 === null ? '=' : toBase64Digit(((i2 & 15) << 2) | (i3 === null ? 0 : i3 >> 6));
  2880. b64 += i2 === null || i3 === null ? '=' : toBase64Digit(i3 & 63);
  2881. }
  2882. return b64;
  2883. }
  2884. function toBase64VLQ(value) {
  2885. value = value < 0 ? ((-value) << 1) + 1 : value << 1;
  2886. let out = '';
  2887. do {
  2888. let digit = value & 31;
  2889. value = value >> 5;
  2890. if (value > 0) {
  2891. digit = digit | 32;
  2892. }
  2893. out += toBase64Digit(digit);
  2894. } while (value > 0);
  2895. return out;
  2896. }
  2897. const B64_DIGITS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  2898. function toBase64Digit(value) {
  2899. if (value < 0 || value >= 64) {
  2900. throw new Error(`Can only encode value in the range [0, 63]`);
  2901. }
  2902. return B64_DIGITS[value];
  2903. }
  2904. const _SINGLE_QUOTE_ESCAPE_STRING_RE = /'|\\|\n|\r|\$/g;
  2905. const _LEGAL_IDENTIFIER_RE = /^[$A-Z_][0-9A-Z_$]*$/i;
  2906. const _INDENT_WITH = ' ';
  2907. class _EmittedLine {
  2908. constructor(indent) {
  2909. this.indent = indent;
  2910. this.partsLength = 0;
  2911. this.parts = [];
  2912. this.srcSpans = [];
  2913. }
  2914. }
  2915. class EmitterVisitorContext {
  2916. static createRoot() {
  2917. return new EmitterVisitorContext(0);
  2918. }
  2919. constructor(_indent) {
  2920. this._indent = _indent;
  2921. this._lines = [new _EmittedLine(_indent)];
  2922. }
  2923. /**
  2924. * @internal strip this from published d.ts files due to
  2925. * https://github.com/microsoft/TypeScript/issues/36216
  2926. */
  2927. get _currentLine() {
  2928. return this._lines[this._lines.length - 1];
  2929. }
  2930. println(from, lastPart = '') {
  2931. this.print(from || null, lastPart, true);
  2932. }
  2933. lineIsEmpty() {
  2934. return this._currentLine.parts.length === 0;
  2935. }
  2936. lineLength() {
  2937. return this._currentLine.indent * _INDENT_WITH.length + this._currentLine.partsLength;
  2938. }
  2939. print(from, part, newLine = false) {
  2940. if (part.length > 0) {
  2941. this._currentLine.parts.push(part);
  2942. this._currentLine.partsLength += part.length;
  2943. this._currentLine.srcSpans.push(from && from.sourceSpan || null);
  2944. }
  2945. if (newLine) {
  2946. this._lines.push(new _EmittedLine(this._indent));
  2947. }
  2948. }
  2949. removeEmptyLastLine() {
  2950. if (this.lineIsEmpty()) {
  2951. this._lines.pop();
  2952. }
  2953. }
  2954. incIndent() {
  2955. this._indent++;
  2956. if (this.lineIsEmpty()) {
  2957. this._currentLine.indent = this._indent;
  2958. }
  2959. }
  2960. decIndent() {
  2961. this._indent--;
  2962. if (this.lineIsEmpty()) {
  2963. this._currentLine.indent = this._indent;
  2964. }
  2965. }
  2966. toSource() {
  2967. return this.sourceLines
  2968. .map(l => l.parts.length > 0 ? _createIndent(l.indent) + l.parts.join('') : '')
  2969. .join('\n');
  2970. }
  2971. toSourceMapGenerator(genFilePath, startsAtLine = 0) {
  2972. const map = new SourceMapGenerator(genFilePath);
  2973. let firstOffsetMapped = false;
  2974. const mapFirstOffsetIfNeeded = () => {
  2975. if (!firstOffsetMapped) {
  2976. // Add a single space so that tools won't try to load the file from disk.
  2977. // Note: We are using virtual urls like `ng:///`, so we have to
  2978. // provide a content here.
  2979. map.addSource(genFilePath, ' ').addMapping(0, genFilePath, 0, 0);
  2980. firstOffsetMapped = true;
  2981. }
  2982. };
  2983. for (let i = 0; i < startsAtLine; i++) {
  2984. map.addLine();
  2985. mapFirstOffsetIfNeeded();
  2986. }
  2987. this.sourceLines.forEach((line, lineIdx) => {
  2988. map.addLine();
  2989. const spans = line.srcSpans;
  2990. const parts = line.parts;
  2991. let col0 = line.indent * _INDENT_WITH.length;
  2992. let spanIdx = 0;
  2993. // skip leading parts without source spans
  2994. while (spanIdx < spans.length && !spans[spanIdx]) {
  2995. col0 += parts[spanIdx].length;
  2996. spanIdx++;
  2997. }
  2998. if (spanIdx < spans.length && lineIdx === 0 && col0 === 0) {
  2999. firstOffsetMapped = true;
  3000. }
  3001. else {
  3002. mapFirstOffsetIfNeeded();
  3003. }
  3004. while (spanIdx < spans.length) {
  3005. const span = spans[spanIdx];
  3006. const source = span.start.file;
  3007. const sourceLine = span.start.line;
  3008. const sourceCol = span.start.col;
  3009. map.addSource(source.url, source.content)
  3010. .addMapping(col0, source.url, sourceLine, sourceCol);
  3011. col0 += parts[spanIdx].length;
  3012. spanIdx++;
  3013. // assign parts without span or the same span to the previous segment
  3014. while (spanIdx < spans.length && (span === spans[spanIdx] || !spans[spanIdx])) {
  3015. col0 += parts[spanIdx].length;
  3016. spanIdx++;
  3017. }
  3018. }
  3019. });
  3020. return map;
  3021. }
  3022. spanOf(line, column) {
  3023. const emittedLine = this._lines[line];
  3024. if (emittedLine) {
  3025. let columnsLeft = column - _createIndent(emittedLine.indent).length;
  3026. for (let partIndex = 0; partIndex < emittedLine.parts.length; partIndex++) {
  3027. const part = emittedLine.parts[partIndex];
  3028. if (part.length > columnsLeft) {
  3029. return emittedLine.srcSpans[partIndex];
  3030. }
  3031. columnsLeft -= part.length;
  3032. }
  3033. }
  3034. return null;
  3035. }
  3036. /**
  3037. * @internal strip this from published d.ts files due to
  3038. * https://github.com/microsoft/TypeScript/issues/36216
  3039. */
  3040. get sourceLines() {
  3041. if (this._lines.length && this._lines[this._lines.length - 1].parts.length === 0) {
  3042. return this._lines.slice(0, -1);
  3043. }
  3044. return this._lines;
  3045. }
  3046. }
  3047. class AbstractEmitterVisitor {
  3048. constructor(_escapeDollarInStrings) {
  3049. this._escapeDollarInStrings = _escapeDollarInStrings;
  3050. }
  3051. printLeadingComments(stmt, ctx) {
  3052. if (stmt.leadingComments === undefined) {
  3053. return;
  3054. }
  3055. for (const comment of stmt.leadingComments) {
  3056. if (comment instanceof JSDocComment) {
  3057. ctx.print(stmt, `/*${comment.toString()}*/`, comment.trailingNewline);
  3058. }
  3059. else {
  3060. if (comment.multiline) {
  3061. ctx.print(stmt, `/* ${comment.text} */`, comment.trailingNewline);
  3062. }
  3063. else {
  3064. comment.text.split('\n').forEach((line) => {
  3065. ctx.println(stmt, `// ${line}`);
  3066. });
  3067. }
  3068. }
  3069. }
  3070. }
  3071. visitExpressionStmt(stmt, ctx) {
  3072. this.printLeadingComments(stmt, ctx);
  3073. stmt.expr.visitExpression(this, ctx);
  3074. ctx.println(stmt, ';');
  3075. return null;
  3076. }
  3077. visitReturnStmt(stmt, ctx) {
  3078. this.printLeadingComments(stmt, ctx);
  3079. ctx.print(stmt, `return `);
  3080. stmt.value.visitExpression(this, ctx);
  3081. ctx.println(stmt, ';');
  3082. return null;
  3083. }
  3084. visitIfStmt(stmt, ctx) {
  3085. this.printLeadingComments(stmt, ctx);
  3086. ctx.print(stmt, `if (`);
  3087. stmt.condition.visitExpression(this, ctx);
  3088. ctx.print(stmt, `) {`);
  3089. const hasElseCase = stmt.falseCase != null && stmt.falseCase.length > 0;
  3090. if (stmt.trueCase.length <= 1 && !hasElseCase) {
  3091. ctx.print(stmt, ` `);
  3092. this.visitAllStatements(stmt.trueCase, ctx);
  3093. ctx.removeEmptyLastLine();
  3094. ctx.print(stmt, ` `);
  3095. }
  3096. else {
  3097. ctx.println();
  3098. ctx.incIndent();
  3099. this.visitAllStatements(stmt.trueCase, ctx);
  3100. ctx.decIndent();
  3101. if (hasElseCase) {
  3102. ctx.println(stmt, `} else {`);
  3103. ctx.incIndent();
  3104. this.visitAllStatements(stmt.falseCase, ctx);
  3105. ctx.decIndent();
  3106. }
  3107. }
  3108. ctx.println(stmt, `}`);
  3109. return null;
  3110. }
  3111. visitWriteVarExpr(expr, ctx) {
  3112. const lineWasEmpty = ctx.lineIsEmpty();
  3113. if (!lineWasEmpty) {
  3114. ctx.print(expr, '(');
  3115. }
  3116. ctx.print(expr, `${expr.name} = `);
  3117. expr.value.visitExpression(this, ctx);
  3118. if (!lineWasEmpty) {
  3119. ctx.print(expr, ')');
  3120. }
  3121. return null;
  3122. }
  3123. visitWriteKeyExpr(expr, ctx) {
  3124. const lineWasEmpty = ctx.lineIsEmpty();
  3125. if (!lineWasEmpty) {
  3126. ctx.print(expr, '(');
  3127. }
  3128. expr.receiver.visitExpression(this, ctx);
  3129. ctx.print(expr, `[`);
  3130. expr.index.visitExpression(this, ctx);
  3131. ctx.print(expr, `] = `);
  3132. expr.value.visitExpression(this, ctx);
  3133. if (!lineWasEmpty) {
  3134. ctx.print(expr, ')');
  3135. }
  3136. return null;
  3137. }
  3138. visitWritePropExpr(expr, ctx) {
  3139. const lineWasEmpty = ctx.lineIsEmpty();
  3140. if (!lineWasEmpty) {
  3141. ctx.print(expr, '(');
  3142. }
  3143. expr.receiver.visitExpression(this, ctx);
  3144. ctx.print(expr, `.${expr.name} = `);
  3145. expr.value.visitExpression(this, ctx);
  3146. if (!lineWasEmpty) {
  3147. ctx.print(expr, ')');
  3148. }
  3149. return null;
  3150. }
  3151. visitInvokeFunctionExpr(expr, ctx) {
  3152. expr.fn.visitExpression(this, ctx);
  3153. ctx.print(expr, `(`);
  3154. this.visitAllExpressions(expr.args, ctx, ',');
  3155. ctx.print(expr, `)`);
  3156. return null;
  3157. }
  3158. visitTaggedTemplateExpr(expr, ctx) {
  3159. expr.tag.visitExpression(this, ctx);
  3160. ctx.print(expr, '`' + expr.template.elements[0].rawText);
  3161. for (let i = 1; i < expr.template.elements.length; i++) {
  3162. ctx.print(expr, '${');
  3163. expr.template.expressions[i - 1].visitExpression(this, ctx);
  3164. ctx.print(expr, `}${expr.template.elements[i].rawText}`);
  3165. }
  3166. ctx.print(expr, '`');
  3167. return null;
  3168. }
  3169. visitWrappedNodeExpr(ast, ctx) {
  3170. throw new Error('Abstract emitter cannot visit WrappedNodeExpr.');
  3171. }
  3172. visitTypeofExpr(expr, ctx) {
  3173. ctx.print(expr, 'typeof ');
  3174. expr.expr.visitExpression(this, ctx);
  3175. }
  3176. visitReadVarExpr(ast, ctx) {
  3177. ctx.print(ast, ast.name);
  3178. return null;
  3179. }
  3180. visitInstantiateExpr(ast, ctx) {
  3181. ctx.print(ast, `new `);
  3182. ast.classExpr.visitExpression(this, ctx);
  3183. ctx.print(ast, `(`);
  3184. this.visitAllExpressions(ast.args, ctx, ',');
  3185. ctx.print(ast, `)`);
  3186. return null;
  3187. }
  3188. visitLiteralExpr(ast, ctx) {
  3189. const value = ast.value;
  3190. if (typeof value === 'string') {
  3191. ctx.print(ast, escapeIdentifier(value, this._escapeDollarInStrings));
  3192. }
  3193. else {
  3194. ctx.print(ast, `${value}`);
  3195. }
  3196. return null;
  3197. }
  3198. visitLocalizedString(ast, ctx) {
  3199. const head = ast.serializeI18nHead();
  3200. ctx.print(ast, '$localize `' + head.raw);
  3201. for (let i = 1; i < ast.messageParts.length; i++) {
  3202. ctx.print(ast, '${');
  3203. ast.expressions[i - 1].visitExpression(this, ctx);
  3204. ctx.print(ast, `}${ast.serializeI18nTemplatePart(i).raw}`);
  3205. }
  3206. ctx.print(ast, '`');
  3207. return null;
  3208. }
  3209. visitConditionalExpr(ast, ctx) {
  3210. ctx.print(ast, `(`);
  3211. ast.condition.visitExpression(this, ctx);
  3212. ctx.print(ast, '? ');
  3213. ast.trueCase.visitExpression(this, ctx);
  3214. ctx.print(ast, ': ');
  3215. ast.falseCase.visitExpression(this, ctx);
  3216. ctx.print(ast, `)`);
  3217. return null;
  3218. }
  3219. visitNotExpr(ast, ctx) {
  3220. ctx.print(ast, '!');
  3221. ast.condition.visitExpression(this, ctx);
  3222. return null;
  3223. }
  3224. visitUnaryOperatorExpr(ast, ctx) {
  3225. let opStr;
  3226. switch (ast.operator) {
  3227. case UnaryOperator.Plus:
  3228. opStr = '+';
  3229. break;
  3230. case UnaryOperator.Minus:
  3231. opStr = '-';
  3232. break;
  3233. default:
  3234. throw new Error(`Unknown operator ${ast.operator}`);
  3235. }
  3236. if (ast.parens)
  3237. ctx.print(ast, `(`);
  3238. ctx.print(ast, opStr);
  3239. ast.expr.visitExpression(this, ctx);
  3240. if (ast.parens)
  3241. ctx.print(ast, `)`);
  3242. return null;
  3243. }
  3244. visitBinaryOperatorExpr(ast, ctx) {
  3245. let opStr;
  3246. switch (ast.operator) {
  3247. case BinaryOperator.Equals:
  3248. opStr = '==';
  3249. break;
  3250. case BinaryOperator.Identical:
  3251. opStr = '===';
  3252. break;
  3253. case BinaryOperator.NotEquals:
  3254. opStr = '!=';
  3255. break;
  3256. case BinaryOperator.NotIdentical:
  3257. opStr = '!==';
  3258. break;
  3259. case BinaryOperator.And:
  3260. opStr = '&&';
  3261. break;
  3262. case BinaryOperator.BitwiseAnd:
  3263. opStr = '&';
  3264. break;
  3265. case BinaryOperator.Or:
  3266. opStr = '||';
  3267. break;
  3268. case BinaryOperator.Plus:
  3269. opStr = '+';
  3270. break;
  3271. case BinaryOperator.Minus:
  3272. opStr = '-';
  3273. break;
  3274. case BinaryOperator.Divide:
  3275. opStr = '/';
  3276. break;
  3277. case BinaryOperator.Multiply:
  3278. opStr = '*';
  3279. break;
  3280. case BinaryOperator.Modulo:
  3281. opStr = '%';
  3282. break;
  3283. case BinaryOperator.Lower:
  3284. opStr = '<';
  3285. break;
  3286. case BinaryOperator.LowerEquals:
  3287. opStr = '<=';
  3288. break;
  3289. case BinaryOperator.Bigger:
  3290. opStr = '>';
  3291. break;
  3292. case BinaryOperator.BiggerEquals:
  3293. opStr = '>=';
  3294. break;
  3295. case BinaryOperator.NullishCoalesce:
  3296. opStr = '??';
  3297. break;
  3298. default:
  3299. throw new Error(`Unknown operator ${ast.operator}`);
  3300. }
  3301. if (ast.parens)
  3302. ctx.print(ast, `(`);
  3303. ast.lhs.visitExpression(this, ctx);
  3304. ctx.print(ast, ` ${opStr} `);
  3305. ast.rhs.visitExpression(this, ctx);
  3306. if (ast.parens)
  3307. ctx.print(ast, `)`);
  3308. return null;
  3309. }
  3310. visitReadPropExpr(ast, ctx) {
  3311. ast.receiver.visitExpression(this, ctx);
  3312. ctx.print(ast, `.`);
  3313. ctx.print(ast, ast.name);
  3314. return null;
  3315. }
  3316. visitReadKeyExpr(ast, ctx) {
  3317. ast.receiver.visitExpression(this, ctx);
  3318. ctx.print(ast, `[`);
  3319. ast.index.visitExpression(this, ctx);
  3320. ctx.print(ast, `]`);
  3321. return null;
  3322. }
  3323. visitLiteralArrayExpr(ast, ctx) {
  3324. ctx.print(ast, `[`);
  3325. this.visitAllExpressions(ast.entries, ctx, ',');
  3326. ctx.print(ast, `]`);
  3327. return null;
  3328. }
  3329. visitLiteralMapExpr(ast, ctx) {
  3330. ctx.print(ast, `{`);
  3331. this.visitAllObjects(entry => {
  3332. ctx.print(ast, `${escapeIdentifier(entry.key, this._escapeDollarInStrings, entry.quoted)}:`);
  3333. entry.value.visitExpression(this, ctx);
  3334. }, ast.entries, ctx, ',');
  3335. ctx.print(ast, `}`);
  3336. return null;
  3337. }
  3338. visitCommaExpr(ast, ctx) {
  3339. ctx.print(ast, '(');
  3340. this.visitAllExpressions(ast.parts, ctx, ',');
  3341. ctx.print(ast, ')');
  3342. return null;
  3343. }
  3344. visitAllExpressions(expressions, ctx, separator) {
  3345. this.visitAllObjects(expr => expr.visitExpression(this, ctx), expressions, ctx, separator);
  3346. }
  3347. visitAllObjects(handler, expressions, ctx, separator) {
  3348. let incrementedIndent = false;
  3349. for (let i = 0; i < expressions.length; i++) {
  3350. if (i > 0) {
  3351. if (ctx.lineLength() > 80) {
  3352. ctx.print(null, separator, true);
  3353. if (!incrementedIndent) {
  3354. // continuation are marked with double indent.
  3355. ctx.incIndent();
  3356. ctx.incIndent();
  3357. incrementedIndent = true;
  3358. }
  3359. }
  3360. else {
  3361. ctx.print(null, separator, false);
  3362. }
  3363. }
  3364. handler(expressions[i]);
  3365. }
  3366. if (incrementedIndent) {
  3367. // continuation are marked with double indent.
  3368. ctx.decIndent();
  3369. ctx.decIndent();
  3370. }
  3371. }
  3372. visitAllStatements(statements, ctx) {
  3373. statements.forEach((stmt) => stmt.visitStatement(this, ctx));
  3374. }
  3375. }
  3376. function escapeIdentifier(input, escapeDollar, alwaysQuote = true) {
  3377. if (input == null) {
  3378. return null;
  3379. }
  3380. const body = input.replace(_SINGLE_QUOTE_ESCAPE_STRING_RE, (...match) => {
  3381. if (match[0] == '$') {
  3382. return escapeDollar ? '\\$' : '$';
  3383. }
  3384. else if (match[0] == '\n') {
  3385. return '\\n';
  3386. }
  3387. else if (match[0] == '\r') {
  3388. return '\\r';
  3389. }
  3390. else {
  3391. return `\\${match[0]}`;
  3392. }
  3393. });
  3394. const requiresQuotes = alwaysQuote || !_LEGAL_IDENTIFIER_RE.test(body);
  3395. return requiresQuotes ? `'${body}'` : body;
  3396. }
  3397. function _createIndent(count) {
  3398. let res = '';
  3399. for (let i = 0; i < count; i++) {
  3400. res += _INDENT_WITH;
  3401. }
  3402. return res;
  3403. }
  3404. function typeWithParameters(type, numParams) {
  3405. if (numParams === 0) {
  3406. return expressionType(type);
  3407. }
  3408. const params = [];
  3409. for (let i = 0; i < numParams; i++) {
  3410. params.push(DYNAMIC_TYPE);
  3411. }
  3412. return expressionType(type, undefined, params);
  3413. }
  3414. const ANIMATE_SYMBOL_PREFIX = '@';
  3415. function prepareSyntheticPropertyName(name) {
  3416. return `${ANIMATE_SYMBOL_PREFIX}${name}`;
  3417. }
  3418. function prepareSyntheticListenerName(name, phase) {
  3419. return `${ANIMATE_SYMBOL_PREFIX}${name}.${phase}`;
  3420. }
  3421. function getSafePropertyAccessString(accessor, name) {
  3422. const escapedName = escapeIdentifier(name, false, false);
  3423. return escapedName !== name ? `${accessor}[${escapedName}]` : `${accessor}.${name}`;
  3424. }
  3425. function prepareSyntheticListenerFunctionName(name, phase) {
  3426. return `animation_${name}_${phase}`;
  3427. }
  3428. function jitOnlyGuardedExpression(expr) {
  3429. return guardedExpression('ngJitMode', expr);
  3430. }
  3431. function devOnlyGuardedExpression(expr) {
  3432. return guardedExpression('ngDevMode', expr);
  3433. }
  3434. function guardedExpression(guard, expr) {
  3435. const guardExpr = new ExternalExpr({ name: guard, moduleName: null });
  3436. const guardNotDefined = new BinaryOperatorExpr(BinaryOperator.Identical, new TypeofExpr(guardExpr), literal('undefined'));
  3437. const guardUndefinedOrTrue = new BinaryOperatorExpr(BinaryOperator.Or, guardNotDefined, guardExpr, /* type */ undefined,
  3438. /* sourceSpan */ undefined, true);
  3439. return new BinaryOperatorExpr(BinaryOperator.And, guardUndefinedOrTrue, expr);
  3440. }
  3441. function wrapReference(value) {
  3442. const wrapped = new WrappedNodeExpr(value);
  3443. return { value: wrapped, type: wrapped };
  3444. }
  3445. function refsToArray(refs, shouldForwardDeclare) {
  3446. const values = literalArr(refs.map(ref => ref.value));
  3447. return shouldForwardDeclare ? fn([], [new ReturnStatement(values)]) : values;
  3448. }
  3449. function createMayBeForwardRefExpression(expression, forwardRef) {
  3450. return { expression, forwardRef };
  3451. }
  3452. /**
  3453. * Convert a `MaybeForwardRefExpression` to an `Expression`, possibly wrapping its expression in a
  3454. * `forwardRef()` call.
  3455. *
  3456. * If `MaybeForwardRefExpression.forwardRef` is `ForwardRefHandling.Unwrapped` then the expression
  3457. * was originally wrapped in a `forwardRef()` call to prevent the value from being eagerly evaluated
  3458. * in the code.
  3459. *
  3460. * See `packages/compiler-cli/src/ngtsc/annotations/src/injectable.ts` and
  3461. * `packages/compiler/src/jit_compiler_facade.ts` for more information.
  3462. */
  3463. function convertFromMaybeForwardRefExpression({ expression, forwardRef }) {
  3464. switch (forwardRef) {
  3465. case 0 /* ForwardRefHandling.None */:
  3466. case 1 /* ForwardRefHandling.Wrapped */:
  3467. return expression;
  3468. case 2 /* ForwardRefHandling.Unwrapped */:
  3469. return generateForwardRef(expression);
  3470. }
  3471. }
  3472. /**
  3473. * Generate an expression that has the given `expr` wrapped in the following form:
  3474. *
  3475. * ```
  3476. * forwardRef(() => expr)
  3477. * ```
  3478. */
  3479. function generateForwardRef(expr) {
  3480. return importExpr(Identifiers.forwardRef).callFn([fn([], [new ReturnStatement(expr)])]);
  3481. }
  3482. var R3FactoryDelegateType;
  3483. (function (R3FactoryDelegateType) {
  3484. R3FactoryDelegateType[R3FactoryDelegateType["Class"] = 0] = "Class";
  3485. R3FactoryDelegateType[R3FactoryDelegateType["Function"] = 1] = "Function";
  3486. })(R3FactoryDelegateType || (R3FactoryDelegateType = {}));
  3487. var FactoryTarget$1;
  3488. (function (FactoryTarget) {
  3489. FactoryTarget[FactoryTarget["Directive"] = 0] = "Directive";
  3490. FactoryTarget[FactoryTarget["Component"] = 1] = "Component";
  3491. FactoryTarget[FactoryTarget["Injectable"] = 2] = "Injectable";
  3492. FactoryTarget[FactoryTarget["Pipe"] = 3] = "Pipe";
  3493. FactoryTarget[FactoryTarget["NgModule"] = 4] = "NgModule";
  3494. })(FactoryTarget$1 || (FactoryTarget$1 = {}));
  3495. /**
  3496. * Construct a factory function expression for the given `R3FactoryMetadata`.
  3497. */
  3498. function compileFactoryFunction(meta) {
  3499. const t = variable('t');
  3500. let baseFactoryVar = null;
  3501. // The type to instantiate via constructor invocation. If there is no delegated factory, meaning
  3502. // this type is always created by constructor invocation, then this is the type-to-create
  3503. // parameter provided by the user (t) if specified, or the current type if not. If there is a
  3504. // delegated factory (which is used to create the current type) then this is only the type-to-
  3505. // create parameter (t).
  3506. const typeForCtor = !isDelegatedFactoryMetadata(meta) ?
  3507. new BinaryOperatorExpr(BinaryOperator.Or, t, meta.type.value) :
  3508. t;
  3509. let ctorExpr = null;
  3510. if (meta.deps !== null) {
  3511. // There is a constructor (either explicitly or implicitly defined).
  3512. if (meta.deps !== 'invalid') {
  3513. ctorExpr = new InstantiateExpr(typeForCtor, injectDependencies(meta.deps, meta.target));
  3514. }
  3515. }
  3516. else {
  3517. // There is no constructor, use the base class' factory to construct typeForCtor.
  3518. baseFactoryVar = variable(`ɵ${meta.name}_BaseFactory`);
  3519. ctorExpr = baseFactoryVar.callFn([typeForCtor]);
  3520. }
  3521. const body = [];
  3522. let retExpr = null;
  3523. function makeConditionalFactory(nonCtorExpr) {
  3524. const r = variable('r');
  3525. body.push(r.set(NULL_EXPR).toDeclStmt());
  3526. const ctorStmt = ctorExpr !== null ? r.set(ctorExpr).toStmt() :
  3527. importExpr(Identifiers.invalidFactory).callFn([]).toStmt();
  3528. body.push(ifStmt(t, [ctorStmt], [r.set(nonCtorExpr).toStmt()]));
  3529. return r;
  3530. }
  3531. if (isDelegatedFactoryMetadata(meta)) {
  3532. // This type is created with a delegated factory. If a type parameter is not specified, call
  3533. // the factory instead.
  3534. const delegateArgs = injectDependencies(meta.delegateDeps, meta.target);
  3535. // Either call `new delegate(...)` or `delegate(...)` depending on meta.delegateType.
  3536. const factoryExpr = new (meta.delegateType === R3FactoryDelegateType.Class ?
  3537. InstantiateExpr :
  3538. InvokeFunctionExpr)(meta.delegate, delegateArgs);
  3539. retExpr = makeConditionalFactory(factoryExpr);
  3540. }
  3541. else if (isExpressionFactoryMetadata(meta)) {
  3542. // TODO(alxhub): decide whether to lower the value here or in the caller
  3543. retExpr = makeConditionalFactory(meta.expression);
  3544. }
  3545. else {
  3546. retExpr = ctorExpr;
  3547. }
  3548. if (retExpr === null) {
  3549. // The expression cannot be formed so render an `ɵɵinvalidFactory()` call.
  3550. body.push(importExpr(Identifiers.invalidFactory).callFn([]).toStmt());
  3551. }
  3552. else if (baseFactoryVar !== null) {
  3553. // This factory uses a base factory, so call `ɵɵgetInheritedFactory()` to compute it.
  3554. const getInheritedFactoryCall = importExpr(Identifiers.getInheritedFactory).callFn([meta.type.value]);
  3555. // Memoize the base factoryFn: `baseFactory || (baseFactory = ɵɵgetInheritedFactory(...))`
  3556. const baseFactory = new BinaryOperatorExpr(BinaryOperator.Or, baseFactoryVar, baseFactoryVar.set(getInheritedFactoryCall));
  3557. body.push(new ReturnStatement(baseFactory.callFn([typeForCtor])));
  3558. }
  3559. else {
  3560. // This is straightforward factory, just return it.
  3561. body.push(new ReturnStatement(retExpr));
  3562. }
  3563. let factoryFn = fn([new FnParam('t', DYNAMIC_TYPE)], body, INFERRED_TYPE, undefined, `${meta.name}_Factory`);
  3564. if (baseFactoryVar !== null) {
  3565. // There is a base factory variable so wrap its declaration along with the factory function into
  3566. // an IIFE.
  3567. factoryFn = fn([], [
  3568. new DeclareVarStmt(baseFactoryVar.name), new ReturnStatement(factoryFn)
  3569. ]).callFn([], /* sourceSpan */ undefined, /* pure */ true);
  3570. }
  3571. return {
  3572. expression: factoryFn,
  3573. statements: [],
  3574. type: createFactoryType(meta),
  3575. };
  3576. }
  3577. function createFactoryType(meta) {
  3578. const ctorDepsType = meta.deps !== null && meta.deps !== 'invalid' ? createCtorDepsType(meta.deps) : NONE_TYPE;
  3579. return expressionType(importExpr(Identifiers.FactoryDeclaration, [typeWithParameters(meta.type.type, meta.typeArgumentCount), ctorDepsType]));
  3580. }
  3581. function injectDependencies(deps, target) {
  3582. return deps.map((dep, index) => compileInjectDependency(dep, target, index));
  3583. }
  3584. function compileInjectDependency(dep, target, index) {
  3585. // Interpret the dependency according to its resolved type.
  3586. if (dep.token === null) {
  3587. return importExpr(Identifiers.invalidFactoryDep).callFn([literal(index)]);
  3588. }
  3589. else if (dep.attributeNameType === null) {
  3590. // Build up the injection flags according to the metadata.
  3591. const flags = 0 /* InjectFlags.Default */ | (dep.self ? 2 /* InjectFlags.Self */ : 0) |
  3592. (dep.skipSelf ? 4 /* InjectFlags.SkipSelf */ : 0) | (dep.host ? 1 /* InjectFlags.Host */ : 0) |
  3593. (dep.optional ? 8 /* InjectFlags.Optional */ : 0) |
  3594. (target === FactoryTarget$1.Pipe ? 16 /* InjectFlags.ForPipe */ : 0);
  3595. // If this dependency is optional or otherwise has non-default flags, then additional
  3596. // parameters describing how to inject the dependency must be passed to the inject function
  3597. // that's being used.
  3598. let flagsParam = (flags !== 0 /* InjectFlags.Default */ || dep.optional) ? literal(flags) : null;
  3599. // Build up the arguments to the injectFn call.
  3600. const injectArgs = [dep.token];
  3601. if (flagsParam) {
  3602. injectArgs.push(flagsParam);
  3603. }
  3604. const injectFn = getInjectFn(target);
  3605. return importExpr(injectFn).callFn(injectArgs);
  3606. }
  3607. else {
  3608. // The `dep.attributeTypeName` value is defined, which indicates that this is an `@Attribute()`
  3609. // type dependency. For the generated JS we still want to use the `dep.token` value in case the
  3610. // name given for the attribute is not a string literal. For example given `@Attribute(foo())`,
  3611. // we want to generate `ɵɵinjectAttribute(foo())`.
  3612. //
  3613. // The `dep.attributeTypeName` is only actually used (in `createCtorDepType()`) to generate
  3614. // typings.
  3615. return importExpr(Identifiers.injectAttribute).callFn([dep.token]);
  3616. }
  3617. }
  3618. function createCtorDepsType(deps) {
  3619. let hasTypes = false;
  3620. const attributeTypes = deps.map(dep => {
  3621. const type = createCtorDepType(dep);
  3622. if (type !== null) {
  3623. hasTypes = true;
  3624. return type;
  3625. }
  3626. else {
  3627. return literal(null);
  3628. }
  3629. });
  3630. if (hasTypes) {
  3631. return expressionType(literalArr(attributeTypes));
  3632. }
  3633. else {
  3634. return NONE_TYPE;
  3635. }
  3636. }
  3637. function createCtorDepType(dep) {
  3638. const entries = [];
  3639. if (dep.attributeNameType !== null) {
  3640. entries.push({ key: 'attribute', value: dep.attributeNameType, quoted: false });
  3641. }
  3642. if (dep.optional) {
  3643. entries.push({ key: 'optional', value: literal(true), quoted: false });
  3644. }
  3645. if (dep.host) {
  3646. entries.push({ key: 'host', value: literal(true), quoted: false });
  3647. }
  3648. if (dep.self) {
  3649. entries.push({ key: 'self', value: literal(true), quoted: false });
  3650. }
  3651. if (dep.skipSelf) {
  3652. entries.push({ key: 'skipSelf', value: literal(true), quoted: false });
  3653. }
  3654. return entries.length > 0 ? literalMap(entries) : null;
  3655. }
  3656. function isDelegatedFactoryMetadata(meta) {
  3657. return meta.delegateType !== undefined;
  3658. }
  3659. function isExpressionFactoryMetadata(meta) {
  3660. return meta.expression !== undefined;
  3661. }
  3662. function getInjectFn(target) {
  3663. switch (target) {
  3664. case FactoryTarget$1.Component:
  3665. case FactoryTarget$1.Directive:
  3666. case FactoryTarget$1.Pipe:
  3667. return Identifiers.directiveInject;
  3668. case FactoryTarget$1.NgModule:
  3669. case FactoryTarget$1.Injectable:
  3670. default:
  3671. return Identifiers.inject;
  3672. }
  3673. }
  3674. /**
  3675. * This is an R3 `Node`-like wrapper for a raw `html.Comment` node. We do not currently
  3676. * require the implementation of a visitor for Comments as they are only collected at
  3677. * the top-level of the R3 AST, and only if `Render3ParseOptions['collectCommentNodes']`
  3678. * is true.
  3679. */
  3680. class Comment$1 {
  3681. constructor(value, sourceSpan) {
  3682. this.value = value;
  3683. this.sourceSpan = sourceSpan;
  3684. }
  3685. visit(_visitor) {
  3686. throw new Error('visit() not implemented for Comment');
  3687. }
  3688. }
  3689. class Text$3 {
  3690. constructor(value, sourceSpan) {
  3691. this.value = value;
  3692. this.sourceSpan = sourceSpan;
  3693. }
  3694. visit(visitor) {
  3695. return visitor.visitText(this);
  3696. }
  3697. }
  3698. class BoundText {
  3699. constructor(value, sourceSpan, i18n) {
  3700. this.value = value;
  3701. this.sourceSpan = sourceSpan;
  3702. this.i18n = i18n;
  3703. }
  3704. visit(visitor) {
  3705. return visitor.visitBoundText(this);
  3706. }
  3707. }
  3708. /**
  3709. * Represents a text attribute in the template.
  3710. *
  3711. * `valueSpan` may not be present in cases where there is no value `<div a></div>`.
  3712. * `keySpan` may also not be present for synthetic attributes from ICU expansions.
  3713. */
  3714. class TextAttribute {
  3715. constructor(name, value, sourceSpan, keySpan, valueSpan, i18n) {
  3716. this.name = name;
  3717. this.value = value;
  3718. this.sourceSpan = sourceSpan;
  3719. this.keySpan = keySpan;
  3720. this.valueSpan = valueSpan;
  3721. this.i18n = i18n;
  3722. }
  3723. visit(visitor) {
  3724. return visitor.visitTextAttribute(this);
  3725. }
  3726. }
  3727. class BoundAttribute {
  3728. constructor(name, type, securityContext, value, unit, sourceSpan, keySpan, valueSpan, i18n) {
  3729. this.name = name;
  3730. this.type = type;
  3731. this.securityContext = securityContext;
  3732. this.value = value;
  3733. this.unit = unit;
  3734. this.sourceSpan = sourceSpan;
  3735. this.keySpan = keySpan;
  3736. this.valueSpan = valueSpan;
  3737. this.i18n = i18n;
  3738. }
  3739. static fromBoundElementProperty(prop, i18n) {
  3740. if (prop.keySpan === undefined) {
  3741. throw new Error(`Unexpected state: keySpan must be defined for bound attributes but was not for ${prop.name}: ${prop.sourceSpan}`);
  3742. }
  3743. return new BoundAttribute(prop.name, prop.type, prop.securityContext, prop.value, prop.unit, prop.sourceSpan, prop.keySpan, prop.valueSpan, i18n);
  3744. }
  3745. visit(visitor) {
  3746. return visitor.visitBoundAttribute(this);
  3747. }
  3748. }
  3749. class BoundEvent {
  3750. constructor(name, type, handler, target, phase, sourceSpan, handlerSpan, keySpan) {
  3751. this.name = name;
  3752. this.type = type;
  3753. this.handler = handler;
  3754. this.target = target;
  3755. this.phase = phase;
  3756. this.sourceSpan = sourceSpan;
  3757. this.handlerSpan = handlerSpan;
  3758. this.keySpan = keySpan;
  3759. }
  3760. static fromParsedEvent(event) {
  3761. const target = event.type === 0 /* ParsedEventType.Regular */ ? event.targetOrPhase : null;
  3762. const phase = event.type === 1 /* ParsedEventType.Animation */ ? event.targetOrPhase : null;
  3763. if (event.keySpan === undefined) {
  3764. throw new Error(`Unexpected state: keySpan must be defined for bound event but was not for ${event.name}: ${event.sourceSpan}`);
  3765. }
  3766. return new BoundEvent(event.name, event.type, event.handler, target, phase, event.sourceSpan, event.handlerSpan, event.keySpan);
  3767. }
  3768. visit(visitor) {
  3769. return visitor.visitBoundEvent(this);
  3770. }
  3771. }
  3772. class Element$1 {
  3773. constructor(name, attributes, inputs, outputs, children, references, sourceSpan, startSourceSpan, endSourceSpan, i18n) {
  3774. this.name = name;
  3775. this.attributes = attributes;
  3776. this.inputs = inputs;
  3777. this.outputs = outputs;
  3778. this.children = children;
  3779. this.references = references;
  3780. this.sourceSpan = sourceSpan;
  3781. this.startSourceSpan = startSourceSpan;
  3782. this.endSourceSpan = endSourceSpan;
  3783. this.i18n = i18n;
  3784. }
  3785. visit(visitor) {
  3786. return visitor.visitElement(this);
  3787. }
  3788. }
  3789. class Template {
  3790. constructor(
  3791. // tagName is the name of the container element, if applicable.
  3792. // `null` is a special case for when there is a structural directive on an `ng-template` so
  3793. // the renderer can differentiate between the synthetic template and the one written in the
  3794. // file.
  3795. tagName, attributes, inputs, outputs, templateAttrs, children, references, variables, sourceSpan, startSourceSpan, endSourceSpan, i18n) {
  3796. this.tagName = tagName;
  3797. this.attributes = attributes;
  3798. this.inputs = inputs;
  3799. this.outputs = outputs;
  3800. this.templateAttrs = templateAttrs;
  3801. this.children = children;
  3802. this.references = references;
  3803. this.variables = variables;
  3804. this.sourceSpan = sourceSpan;
  3805. this.startSourceSpan = startSourceSpan;
  3806. this.endSourceSpan = endSourceSpan;
  3807. this.i18n = i18n;
  3808. }
  3809. visit(visitor) {
  3810. return visitor.visitTemplate(this);
  3811. }
  3812. }
  3813. class Content {
  3814. constructor(selector, attributes, sourceSpan, i18n) {
  3815. this.selector = selector;
  3816. this.attributes = attributes;
  3817. this.sourceSpan = sourceSpan;
  3818. this.i18n = i18n;
  3819. this.name = 'ng-content';
  3820. }
  3821. visit(visitor) {
  3822. return visitor.visitContent(this);
  3823. }
  3824. }
  3825. class Variable {
  3826. constructor(name, value, sourceSpan, keySpan, valueSpan) {
  3827. this.name = name;
  3828. this.value = value;
  3829. this.sourceSpan = sourceSpan;
  3830. this.keySpan = keySpan;
  3831. this.valueSpan = valueSpan;
  3832. }
  3833. visit(visitor) {
  3834. return visitor.visitVariable(this);
  3835. }
  3836. }
  3837. class Reference {
  3838. constructor(name, value, sourceSpan, keySpan, valueSpan) {
  3839. this.name = name;
  3840. this.value = value;
  3841. this.sourceSpan = sourceSpan;
  3842. this.keySpan = keySpan;
  3843. this.valueSpan = valueSpan;
  3844. }
  3845. visit(visitor) {
  3846. return visitor.visitReference(this);
  3847. }
  3848. }
  3849. class Icu$1 {
  3850. constructor(vars, placeholders, sourceSpan, i18n) {
  3851. this.vars = vars;
  3852. this.placeholders = placeholders;
  3853. this.sourceSpan = sourceSpan;
  3854. this.i18n = i18n;
  3855. }
  3856. visit(visitor) {
  3857. return visitor.visitIcu(this);
  3858. }
  3859. }
  3860. class RecursiveVisitor$1 {
  3861. visitElement(element) {
  3862. visitAll$1(this, element.attributes);
  3863. visitAll$1(this, element.inputs);
  3864. visitAll$1(this, element.outputs);
  3865. visitAll$1(this, element.children);
  3866. visitAll$1(this, element.references);
  3867. }
  3868. visitTemplate(template) {
  3869. visitAll$1(this, template.attributes);
  3870. visitAll$1(this, template.inputs);
  3871. visitAll$1(this, template.outputs);
  3872. visitAll$1(this, template.children);
  3873. visitAll$1(this, template.references);
  3874. visitAll$1(this, template.variables);
  3875. }
  3876. visitContent(content) { }
  3877. visitVariable(variable) { }
  3878. visitReference(reference) { }
  3879. visitTextAttribute(attribute) { }
  3880. visitBoundAttribute(attribute) { }
  3881. visitBoundEvent(attribute) { }
  3882. visitText(text) { }
  3883. visitBoundText(text) { }
  3884. visitIcu(icu) { }
  3885. }
  3886. function visitAll$1(visitor, nodes) {
  3887. const result = [];
  3888. if (visitor.visit) {
  3889. for (const node of nodes) {
  3890. visitor.visit(node) || node.visit(visitor);
  3891. }
  3892. }
  3893. else {
  3894. for (const node of nodes) {
  3895. const newNode = node.visit(visitor);
  3896. if (newNode) {
  3897. result.push(newNode);
  3898. }
  3899. }
  3900. }
  3901. return result;
  3902. }
  3903. class Message {
  3904. /**
  3905. * @param nodes message AST
  3906. * @param placeholders maps placeholder names to static content and their source spans
  3907. * @param placeholderToMessage maps placeholder names to messages (used for nested ICU messages)
  3908. * @param meaning
  3909. * @param description
  3910. * @param customId
  3911. */
  3912. constructor(nodes, placeholders, placeholderToMessage, meaning, description, customId) {
  3913. this.nodes = nodes;
  3914. this.placeholders = placeholders;
  3915. this.placeholderToMessage = placeholderToMessage;
  3916. this.meaning = meaning;
  3917. this.description = description;
  3918. this.customId = customId;
  3919. /** The ids to use if there are no custom id and if `i18nLegacyMessageIdFormat` is not empty */
  3920. this.legacyIds = [];
  3921. this.id = this.customId;
  3922. this.messageString = serializeMessage(this.nodes);
  3923. if (nodes.length) {
  3924. this.sources = [{
  3925. filePath: nodes[0].sourceSpan.start.file.url,
  3926. startLine: nodes[0].sourceSpan.start.line + 1,
  3927. startCol: nodes[0].sourceSpan.start.col + 1,
  3928. endLine: nodes[nodes.length - 1].sourceSpan.end.line + 1,
  3929. endCol: nodes[0].sourceSpan.start.col + 1
  3930. }];
  3931. }
  3932. else {
  3933. this.sources = [];
  3934. }
  3935. }
  3936. }
  3937. class Text$2 {
  3938. constructor(value, sourceSpan) {
  3939. this.value = value;
  3940. this.sourceSpan = sourceSpan;
  3941. }
  3942. visit(visitor, context) {
  3943. return visitor.visitText(this, context);
  3944. }
  3945. }
  3946. // TODO(vicb): do we really need this node (vs an array) ?
  3947. class Container {
  3948. constructor(children, sourceSpan) {
  3949. this.children = children;
  3950. this.sourceSpan = sourceSpan;
  3951. }
  3952. visit(visitor, context) {
  3953. return visitor.visitContainer(this, context);
  3954. }
  3955. }
  3956. class Icu {
  3957. constructor(expression, type, cases, sourceSpan, expressionPlaceholder) {
  3958. this.expression = expression;
  3959. this.type = type;
  3960. this.cases = cases;
  3961. this.sourceSpan = sourceSpan;
  3962. this.expressionPlaceholder = expressionPlaceholder;
  3963. }
  3964. visit(visitor, context) {
  3965. return visitor.visitIcu(this, context);
  3966. }
  3967. }
  3968. class TagPlaceholder {
  3969. constructor(tag, attrs, startName, closeName, children, isVoid,
  3970. // TODO sourceSpan should cover all (we need a startSourceSpan and endSourceSpan)
  3971. sourceSpan, startSourceSpan, endSourceSpan) {
  3972. this.tag = tag;
  3973. this.attrs = attrs;
  3974. this.startName = startName;
  3975. this.closeName = closeName;
  3976. this.children = children;
  3977. this.isVoid = isVoid;
  3978. this.sourceSpan = sourceSpan;
  3979. this.startSourceSpan = startSourceSpan;
  3980. this.endSourceSpan = endSourceSpan;
  3981. }
  3982. visit(visitor, context) {
  3983. return visitor.visitTagPlaceholder(this, context);
  3984. }
  3985. }
  3986. class Placeholder {
  3987. constructor(value, name, sourceSpan) {
  3988. this.value = value;
  3989. this.name = name;
  3990. this.sourceSpan = sourceSpan;
  3991. }
  3992. visit(visitor, context) {
  3993. return visitor.visitPlaceholder(this, context);
  3994. }
  3995. }
  3996. class IcuPlaceholder {
  3997. constructor(value, name, sourceSpan) {
  3998. this.value = value;
  3999. this.name = name;
  4000. this.sourceSpan = sourceSpan;
  4001. }
  4002. visit(visitor, context) {
  4003. return visitor.visitIcuPlaceholder(this, context);
  4004. }
  4005. }
  4006. // Clone the AST
  4007. class CloneVisitor {
  4008. visitText(text, context) {
  4009. return new Text$2(text.value, text.sourceSpan);
  4010. }
  4011. visitContainer(container, context) {
  4012. const children = container.children.map(n => n.visit(this, context));
  4013. return new Container(children, container.sourceSpan);
  4014. }
  4015. visitIcu(icu, context) {
  4016. const cases = {};
  4017. Object.keys(icu.cases).forEach(key => cases[key] = icu.cases[key].visit(this, context));
  4018. const msg = new Icu(icu.expression, icu.type, cases, icu.sourceSpan, icu.expressionPlaceholder);
  4019. return msg;
  4020. }
  4021. visitTagPlaceholder(ph, context) {
  4022. const children = ph.children.map(n => n.visit(this, context));
  4023. return new TagPlaceholder(ph.tag, ph.attrs, ph.startName, ph.closeName, children, ph.isVoid, ph.sourceSpan, ph.startSourceSpan, ph.endSourceSpan);
  4024. }
  4025. visitPlaceholder(ph, context) {
  4026. return new Placeholder(ph.value, ph.name, ph.sourceSpan);
  4027. }
  4028. visitIcuPlaceholder(ph, context) {
  4029. return new IcuPlaceholder(ph.value, ph.name, ph.sourceSpan);
  4030. }
  4031. }
  4032. // Visit all the nodes recursively
  4033. class RecurseVisitor {
  4034. visitText(text, context) { }
  4035. visitContainer(container, context) {
  4036. container.children.forEach(child => child.visit(this));
  4037. }
  4038. visitIcu(icu, context) {
  4039. Object.keys(icu.cases).forEach(k => {
  4040. icu.cases[k].visit(this);
  4041. });
  4042. }
  4043. visitTagPlaceholder(ph, context) {
  4044. ph.children.forEach(child => child.visit(this));
  4045. }
  4046. visitPlaceholder(ph, context) { }
  4047. visitIcuPlaceholder(ph, context) { }
  4048. }
  4049. /**
  4050. * Serialize the message to the Localize backtick string format that would appear in compiled code.
  4051. */
  4052. function serializeMessage(messageNodes) {
  4053. const visitor = new LocalizeMessageStringVisitor();
  4054. const str = messageNodes.map(n => n.visit(visitor)).join('');
  4055. return str;
  4056. }
  4057. class LocalizeMessageStringVisitor {
  4058. visitText(text) {
  4059. return text.value;
  4060. }
  4061. visitContainer(container) {
  4062. return container.children.map(child => child.visit(this)).join('');
  4063. }
  4064. visitIcu(icu) {
  4065. const strCases = Object.keys(icu.cases).map((k) => `${k} {${icu.cases[k].visit(this)}}`);
  4066. return `{${icu.expressionPlaceholder}, ${icu.type}, ${strCases.join(' ')}}`;
  4067. }
  4068. visitTagPlaceholder(ph) {
  4069. const children = ph.children.map(child => child.visit(this)).join('');
  4070. return `{$${ph.startName}}${children}{$${ph.closeName}}`;
  4071. }
  4072. visitPlaceholder(ph) {
  4073. return `{$${ph.name}}`;
  4074. }
  4075. visitIcuPlaceholder(ph) {
  4076. return `{$${ph.name}}`;
  4077. }
  4078. }
  4079. class Serializer {
  4080. // Creates a name mapper, see `PlaceholderMapper`
  4081. // Returning `null` means that no name mapping is used.
  4082. createNameMapper(message) {
  4083. return null;
  4084. }
  4085. }
  4086. /**
  4087. * A simple mapper that take a function to transform an internal name to a public name
  4088. */
  4089. class SimplePlaceholderMapper extends RecurseVisitor {
  4090. // create a mapping from the message
  4091. constructor(message, mapName) {
  4092. super();
  4093. this.mapName = mapName;
  4094. this.internalToPublic = {};
  4095. this.publicToNextId = {};
  4096. this.publicToInternal = {};
  4097. message.nodes.forEach(node => node.visit(this));
  4098. }
  4099. toPublicName(internalName) {
  4100. return this.internalToPublic.hasOwnProperty(internalName) ?
  4101. this.internalToPublic[internalName] :
  4102. null;
  4103. }
  4104. toInternalName(publicName) {
  4105. return this.publicToInternal.hasOwnProperty(publicName) ? this.publicToInternal[publicName] :
  4106. null;
  4107. }
  4108. visitText(text, context) {
  4109. return null;
  4110. }
  4111. visitTagPlaceholder(ph, context) {
  4112. this.visitPlaceholderName(ph.startName);
  4113. super.visitTagPlaceholder(ph, context);
  4114. this.visitPlaceholderName(ph.closeName);
  4115. }
  4116. visitPlaceholder(ph, context) {
  4117. this.visitPlaceholderName(ph.name);
  4118. }
  4119. visitIcuPlaceholder(ph, context) {
  4120. this.visitPlaceholderName(ph.name);
  4121. }
  4122. // XMB placeholders could only contains A-Z, 0-9 and _
  4123. visitPlaceholderName(internalName) {
  4124. if (!internalName || this.internalToPublic.hasOwnProperty(internalName)) {
  4125. return;
  4126. }
  4127. let publicName = this.mapName(internalName);
  4128. if (this.publicToInternal.hasOwnProperty(publicName)) {
  4129. // Create a new XMB when it has already been used
  4130. const nextId = this.publicToNextId[publicName];
  4131. this.publicToNextId[publicName] = nextId + 1;
  4132. publicName = `${publicName}_${nextId}`;
  4133. }
  4134. else {
  4135. this.publicToNextId[publicName] = 1;
  4136. }
  4137. this.internalToPublic[internalName] = publicName;
  4138. this.publicToInternal[publicName] = internalName;
  4139. }
  4140. }
  4141. class _Visitor$2 {
  4142. visitTag(tag) {
  4143. const strAttrs = this._serializeAttributes(tag.attrs);
  4144. if (tag.children.length == 0) {
  4145. return `<${tag.name}${strAttrs}/>`;
  4146. }
  4147. const strChildren = tag.children.map(node => node.visit(this));
  4148. return `<${tag.name}${strAttrs}>${strChildren.join('')}</${tag.name}>`;
  4149. }
  4150. visitText(text) {
  4151. return text.value;
  4152. }
  4153. visitDeclaration(decl) {
  4154. return `<?xml${this._serializeAttributes(decl.attrs)} ?>`;
  4155. }
  4156. _serializeAttributes(attrs) {
  4157. const strAttrs = Object.keys(attrs).map((name) => `${name}="${attrs[name]}"`).join(' ');
  4158. return strAttrs.length > 0 ? ' ' + strAttrs : '';
  4159. }
  4160. visitDoctype(doctype) {
  4161. return `<!DOCTYPE ${doctype.rootTag} [\n${doctype.dtd}\n]>`;
  4162. }
  4163. }
  4164. const _visitor = new _Visitor$2();
  4165. function serialize(nodes) {
  4166. return nodes.map((node) => node.visit(_visitor)).join('');
  4167. }
  4168. class Declaration {
  4169. constructor(unescapedAttrs) {
  4170. this.attrs = {};
  4171. Object.keys(unescapedAttrs).forEach((k) => {
  4172. this.attrs[k] = escapeXml(unescapedAttrs[k]);
  4173. });
  4174. }
  4175. visit(visitor) {
  4176. return visitor.visitDeclaration(this);
  4177. }
  4178. }
  4179. class Doctype {
  4180. constructor(rootTag, dtd) {
  4181. this.rootTag = rootTag;
  4182. this.dtd = dtd;
  4183. }
  4184. visit(visitor) {
  4185. return visitor.visitDoctype(this);
  4186. }
  4187. }
  4188. class Tag {
  4189. constructor(name, unescapedAttrs = {}, children = []) {
  4190. this.name = name;
  4191. this.children = children;
  4192. this.attrs = {};
  4193. Object.keys(unescapedAttrs).forEach((k) => {
  4194. this.attrs[k] = escapeXml(unescapedAttrs[k]);
  4195. });
  4196. }
  4197. visit(visitor) {
  4198. return visitor.visitTag(this);
  4199. }
  4200. }
  4201. class Text$1 {
  4202. constructor(unescapedValue) {
  4203. this.value = escapeXml(unescapedValue);
  4204. }
  4205. visit(visitor) {
  4206. return visitor.visitText(this);
  4207. }
  4208. }
  4209. class CR extends Text$1 {
  4210. constructor(ws = 0) {
  4211. super(`\n${new Array(ws + 1).join(' ')}`);
  4212. }
  4213. }
  4214. const _ESCAPED_CHARS = [
  4215. [/&/g, '&amp;'],
  4216. [/"/g, '&quot;'],
  4217. [/'/g, '&apos;'],
  4218. [/</g, '&lt;'],
  4219. [/>/g, '&gt;'],
  4220. ];
  4221. // Escape `_ESCAPED_CHARS` characters in the given text with encoded entities
  4222. function escapeXml(text) {
  4223. return _ESCAPED_CHARS.reduce((text, entry) => text.replace(entry[0], entry[1]), text);
  4224. }
  4225. const _MESSAGES_TAG = 'messagebundle';
  4226. const _MESSAGE_TAG = 'msg';
  4227. const _PLACEHOLDER_TAG$3 = 'ph';
  4228. const _EXAMPLE_TAG = 'ex';
  4229. const _SOURCE_TAG$2 = 'source';
  4230. const _DOCTYPE = `<!ELEMENT messagebundle (msg)*>
  4231. <!ATTLIST messagebundle class CDATA #IMPLIED>
  4232. <!ELEMENT msg (#PCDATA|ph|source)*>
  4233. <!ATTLIST msg id CDATA #IMPLIED>
  4234. <!ATTLIST msg seq CDATA #IMPLIED>
  4235. <!ATTLIST msg name CDATA #IMPLIED>
  4236. <!ATTLIST msg desc CDATA #IMPLIED>
  4237. <!ATTLIST msg meaning CDATA #IMPLIED>
  4238. <!ATTLIST msg obsolete (obsolete) #IMPLIED>
  4239. <!ATTLIST msg xml:space (default|preserve) "default">
  4240. <!ATTLIST msg is_hidden CDATA #IMPLIED>
  4241. <!ELEMENT source (#PCDATA)>
  4242. <!ELEMENT ph (#PCDATA|ex)*>
  4243. <!ATTLIST ph name CDATA #REQUIRED>
  4244. <!ELEMENT ex (#PCDATA)>`;
  4245. class Xmb extends Serializer {
  4246. write(messages, locale) {
  4247. const exampleVisitor = new ExampleVisitor();
  4248. const visitor = new _Visitor$1();
  4249. let rootNode = new Tag(_MESSAGES_TAG);
  4250. messages.forEach(message => {
  4251. const attrs = { id: message.id };
  4252. if (message.description) {
  4253. attrs['desc'] = message.description;
  4254. }
  4255. if (message.meaning) {
  4256. attrs['meaning'] = message.meaning;
  4257. }
  4258. let sourceTags = [];
  4259. message.sources.forEach((source) => {
  4260. sourceTags.push(new Tag(_SOURCE_TAG$2, {}, [new Text$1(`${source.filePath}:${source.startLine}${source.endLine !== source.startLine ? ',' + source.endLine : ''}`)]));
  4261. });
  4262. rootNode.children.push(new CR(2), new Tag(_MESSAGE_TAG, attrs, [...sourceTags, ...visitor.serialize(message.nodes)]));
  4263. });
  4264. rootNode.children.push(new CR());
  4265. return serialize([
  4266. new Declaration({ version: '1.0', encoding: 'UTF-8' }),
  4267. new CR(),
  4268. new Doctype(_MESSAGES_TAG, _DOCTYPE),
  4269. new CR(),
  4270. exampleVisitor.addDefaultExamples(rootNode),
  4271. new CR(),
  4272. ]);
  4273. }
  4274. load(content, url) {
  4275. throw new Error('Unsupported');
  4276. }
  4277. digest(message) {
  4278. return digest(message);
  4279. }
  4280. createNameMapper(message) {
  4281. return new SimplePlaceholderMapper(message, toPublicName);
  4282. }
  4283. }
  4284. class _Visitor$1 {
  4285. visitText(text, context) {
  4286. return [new Text$1(text.value)];
  4287. }
  4288. visitContainer(container, context) {
  4289. const nodes = [];
  4290. container.children.forEach((node) => nodes.push(...node.visit(this)));
  4291. return nodes;
  4292. }
  4293. visitIcu(icu, context) {
  4294. const nodes = [new Text$1(`{${icu.expressionPlaceholder}, ${icu.type}, `)];
  4295. Object.keys(icu.cases).forEach((c) => {
  4296. nodes.push(new Text$1(`${c} {`), ...icu.cases[c].visit(this), new Text$1(`} `));
  4297. });
  4298. nodes.push(new Text$1(`}`));
  4299. return nodes;
  4300. }
  4301. visitTagPlaceholder(ph, context) {
  4302. const startTagAsText = new Text$1(`<${ph.tag}>`);
  4303. const startEx = new Tag(_EXAMPLE_TAG, {}, [startTagAsText]);
  4304. // TC requires PH to have a non empty EX, and uses the text node to show the "original" value.
  4305. const startTagPh = new Tag(_PLACEHOLDER_TAG$3, { name: ph.startName }, [startEx, startTagAsText]);
  4306. if (ph.isVoid) {
  4307. // void tags have no children nor closing tags
  4308. return [startTagPh];
  4309. }
  4310. const closeTagAsText = new Text$1(`</${ph.tag}>`);
  4311. const closeEx = new Tag(_EXAMPLE_TAG, {}, [closeTagAsText]);
  4312. // TC requires PH to have a non empty EX, and uses the text node to show the "original" value.
  4313. const closeTagPh = new Tag(_PLACEHOLDER_TAG$3, { name: ph.closeName }, [closeEx, closeTagAsText]);
  4314. return [startTagPh, ...this.serialize(ph.children), closeTagPh];
  4315. }
  4316. visitPlaceholder(ph, context) {
  4317. const interpolationAsText = new Text$1(`{{${ph.value}}}`);
  4318. // Example tag needs to be not-empty for TC.
  4319. const exTag = new Tag(_EXAMPLE_TAG, {}, [interpolationAsText]);
  4320. return [
  4321. // TC requires PH to have a non empty EX, and uses the text node to show the "original" value.
  4322. new Tag(_PLACEHOLDER_TAG$3, { name: ph.name }, [exTag, interpolationAsText])
  4323. ];
  4324. }
  4325. visitIcuPlaceholder(ph, context) {
  4326. const icuExpression = ph.value.expression;
  4327. const icuType = ph.value.type;
  4328. const icuCases = Object.keys(ph.value.cases).map((value) => value + ' {...}').join(' ');
  4329. const icuAsText = new Text$1(`{${icuExpression}, ${icuType}, ${icuCases}}`);
  4330. const exTag = new Tag(_EXAMPLE_TAG, {}, [icuAsText]);
  4331. return [
  4332. // TC requires PH to have a non empty EX, and uses the text node to show the "original" value.
  4333. new Tag(_PLACEHOLDER_TAG$3, { name: ph.name }, [exTag, icuAsText])
  4334. ];
  4335. }
  4336. serialize(nodes) {
  4337. return [].concat(...nodes.map(node => node.visit(this)));
  4338. }
  4339. }
  4340. function digest(message) {
  4341. return decimalDigest(message);
  4342. }
  4343. // TC requires at least one non-empty example on placeholders
  4344. class ExampleVisitor {
  4345. addDefaultExamples(node) {
  4346. node.visit(this);
  4347. return node;
  4348. }
  4349. visitTag(tag) {
  4350. if (tag.name === _PLACEHOLDER_TAG$3) {
  4351. if (!tag.children || tag.children.length == 0) {
  4352. const exText = new Text$1(tag.attrs['name'] || '...');
  4353. tag.children = [new Tag(_EXAMPLE_TAG, {}, [exText])];
  4354. }
  4355. }
  4356. else if (tag.children) {
  4357. tag.children.forEach(node => node.visit(this));
  4358. }
  4359. }
  4360. visitText(text) { }
  4361. visitDeclaration(decl) { }
  4362. visitDoctype(doctype) { }
  4363. }
  4364. // XMB/XTB placeholders can only contain A-Z, 0-9 and _
  4365. function toPublicName(internalName) {
  4366. return internalName.toUpperCase().replace(/[^A-Z0-9_]/g, '_');
  4367. }
  4368. /* Closure variables holding messages must be named `MSG_[A-Z0-9]+` */
  4369. const CLOSURE_TRANSLATION_VAR_PREFIX = 'MSG_';
  4370. /**
  4371. * Prefix for non-`goog.getMsg` i18n-related vars.
  4372. * Note: the prefix uses lowercase characters intentionally due to a Closure behavior that
  4373. * considers variables like `I18N_0` as constants and throws an error when their value changes.
  4374. */
  4375. const TRANSLATION_VAR_PREFIX = 'i18n_';
  4376. /** Name of the i18n attributes **/
  4377. const I18N_ATTR = 'i18n';
  4378. const I18N_ATTR_PREFIX = 'i18n-';
  4379. /** Prefix of var expressions used in ICUs */
  4380. const I18N_ICU_VAR_PREFIX = 'VAR_';
  4381. /** Prefix of ICU expressions for post processing */
  4382. const I18N_ICU_MAPPING_PREFIX = 'I18N_EXP_';
  4383. /** Placeholder wrapper for i18n expressions **/
  4384. const I18N_PLACEHOLDER_SYMBOL = '�';
  4385. function isI18nAttribute(name) {
  4386. return name === I18N_ATTR || name.startsWith(I18N_ATTR_PREFIX);
  4387. }
  4388. function isI18nRootNode(meta) {
  4389. return meta instanceof Message;
  4390. }
  4391. function isSingleI18nIcu(meta) {
  4392. return isI18nRootNode(meta) && meta.nodes.length === 1 && meta.nodes[0] instanceof Icu;
  4393. }
  4394. function hasI18nMeta(node) {
  4395. return !!node.i18n;
  4396. }
  4397. function hasI18nAttrs(element) {
  4398. return element.attrs.some((attr) => isI18nAttribute(attr.name));
  4399. }
  4400. function icuFromI18nMessage(message) {
  4401. return message.nodes[0];
  4402. }
  4403. function wrapI18nPlaceholder(content, contextId = 0) {
  4404. const blockId = contextId > 0 ? `:${contextId}` : '';
  4405. return `${I18N_PLACEHOLDER_SYMBOL}${content}${blockId}${I18N_PLACEHOLDER_SYMBOL}`;
  4406. }
  4407. function assembleI18nBoundString(strings, bindingStartIndex = 0, contextId = 0) {
  4408. if (!strings.length)
  4409. return '';
  4410. let acc = '';
  4411. const lastIdx = strings.length - 1;
  4412. for (let i = 0; i < lastIdx; i++) {
  4413. acc += `${strings[i]}${wrapI18nPlaceholder(bindingStartIndex + i, contextId)}`;
  4414. }
  4415. acc += strings[lastIdx];
  4416. return acc;
  4417. }
  4418. function getSeqNumberGenerator(startsAt = 0) {
  4419. let current = startsAt;
  4420. return () => current++;
  4421. }
  4422. function placeholdersToParams(placeholders) {
  4423. const params = {};
  4424. placeholders.forEach((values, key) => {
  4425. params[key] = literal(values.length > 1 ? `[${values.join('|')}]` : values[0]);
  4426. });
  4427. return params;
  4428. }
  4429. function updatePlaceholderMap(map, name, ...values) {
  4430. const current = map.get(name) || [];
  4431. current.push(...values);
  4432. map.set(name, current);
  4433. }
  4434. function assembleBoundTextPlaceholders(meta, bindingStartIndex = 0, contextId = 0) {
  4435. const startIdx = bindingStartIndex;
  4436. const placeholders = new Map();
  4437. const node = meta instanceof Message ? meta.nodes.find(node => node instanceof Container) : meta;
  4438. if (node) {
  4439. node
  4440. .children
  4441. .filter((child) => child instanceof Placeholder)
  4442. .forEach((child, idx) => {
  4443. const content = wrapI18nPlaceholder(startIdx + idx, contextId);
  4444. updatePlaceholderMap(placeholders, child.name, content);
  4445. });
  4446. }
  4447. return placeholders;
  4448. }
  4449. /**
  4450. * Format the placeholder names in a map of placeholders to expressions.
  4451. *
  4452. * The placeholder names are converted from "internal" format (e.g. `START_TAG_DIV_1`) to "external"
  4453. * format (e.g. `startTagDiv_1`).
  4454. *
  4455. * @param params A map of placeholder names to expressions.
  4456. * @param useCamelCase whether to camelCase the placeholder name when formatting.
  4457. * @returns A new map of formatted placeholder names to expressions.
  4458. */
  4459. function formatI18nPlaceholderNamesInMap(params = {}, useCamelCase) {
  4460. const _params = {};
  4461. if (params && Object.keys(params).length) {
  4462. Object.keys(params).forEach(key => _params[formatI18nPlaceholderName(key, useCamelCase)] = params[key]);
  4463. }
  4464. return _params;
  4465. }
  4466. /**
  4467. * Converts internal placeholder names to public-facing format
  4468. * (for example to use in goog.getMsg call).
  4469. * Example: `START_TAG_DIV_1` is converted to `startTagDiv_1`.
  4470. *
  4471. * @param name The placeholder name that should be formatted
  4472. * @returns Formatted placeholder name
  4473. */
  4474. function formatI18nPlaceholderName(name, useCamelCase = true) {
  4475. const publicName = toPublicName(name);
  4476. if (!useCamelCase) {
  4477. return publicName;
  4478. }
  4479. const chunks = publicName.split('_');
  4480. if (chunks.length === 1) {
  4481. // if no "_" found - just lowercase the value
  4482. return name.toLowerCase();
  4483. }
  4484. let postfix;
  4485. // eject last element if it's a number
  4486. if (/^\d+$/.test(chunks[chunks.length - 1])) {
  4487. postfix = chunks.pop();
  4488. }
  4489. let raw = chunks.shift().toLowerCase();
  4490. if (chunks.length) {
  4491. raw += chunks.map(c => c.charAt(0).toUpperCase() + c.slice(1).toLowerCase()).join('');
  4492. }
  4493. return postfix ? `${raw}_${postfix}` : raw;
  4494. }
  4495. /**
  4496. * Generates a prefix for translation const name.
  4497. *
  4498. * @param extra Additional local prefix that should be injected into translation var name
  4499. * @returns Complete translation const prefix
  4500. */
  4501. function getTranslationConstPrefix(extra) {
  4502. return `${CLOSURE_TRANSLATION_VAR_PREFIX}${extra}`.toUpperCase();
  4503. }
  4504. /**
  4505. * Generate AST to declare a variable. E.g. `var I18N_1;`.
  4506. * @param variable the name of the variable to declare.
  4507. */
  4508. function declareI18nVariable(variable) {
  4509. return new DeclareVarStmt(variable.name, undefined, INFERRED_TYPE, undefined, variable.sourceSpan);
  4510. }
  4511. /**
  4512. * Checks whether an object key contains potentially unsafe chars, thus the key should be wrapped in
  4513. * quotes. Note: we do not wrap all keys into quotes, as it may have impact on minification and may
  4514. * bot work in some cases when object keys are mangled by minifier.
  4515. *
  4516. * TODO(FW-1136): this is a temporary solution, we need to come up with a better way of working with
  4517. * inputs that contain potentially unsafe chars.
  4518. */
  4519. const UNSAFE_OBJECT_KEY_NAME_REGEXP = /[-.]/;
  4520. /** Name of the temporary to use during data binding */
  4521. const TEMPORARY_NAME = '_t';
  4522. /** Name of the context parameter passed into a template function */
  4523. const CONTEXT_NAME = 'ctx';
  4524. /** Name of the RenderFlag passed into a template function */
  4525. const RENDER_FLAGS = 'rf';
  4526. /** The prefix reference variables */
  4527. const REFERENCE_PREFIX = '_r';
  4528. /** The name of the implicit context reference */
  4529. const IMPLICIT_REFERENCE = '$implicit';
  4530. /** Non bindable attribute name **/
  4531. const NON_BINDABLE_ATTR = 'ngNonBindable';
  4532. /** Name for the variable keeping track of the context returned by `ɵɵrestoreView`. */
  4533. const RESTORED_VIEW_CONTEXT_NAME = 'restoredCtx';
  4534. /**
  4535. * Maximum length of a single instruction chain. Because our output AST uses recursion, we're
  4536. * limited in how many expressions we can nest before we reach the call stack limit. This
  4537. * length is set very conservatively in order to reduce the chance of problems.
  4538. */
  4539. const MAX_CHAIN_LENGTH = 500;
  4540. /** Instructions that support chaining. */
  4541. const CHAINABLE_INSTRUCTIONS = new Set([
  4542. Identifiers.element,
  4543. Identifiers.elementStart,
  4544. Identifiers.elementEnd,
  4545. Identifiers.elementContainer,
  4546. Identifiers.elementContainerStart,
  4547. Identifiers.elementContainerEnd,
  4548. Identifiers.i18nExp,
  4549. Identifiers.listener,
  4550. Identifiers.classProp,
  4551. Identifiers.syntheticHostListener,
  4552. Identifiers.hostProperty,
  4553. Identifiers.syntheticHostProperty,
  4554. Identifiers.property,
  4555. Identifiers.propertyInterpolate1,
  4556. Identifiers.propertyInterpolate2,
  4557. Identifiers.propertyInterpolate3,
  4558. Identifiers.propertyInterpolate4,
  4559. Identifiers.propertyInterpolate5,
  4560. Identifiers.propertyInterpolate6,
  4561. Identifiers.propertyInterpolate7,
  4562. Identifiers.propertyInterpolate8,
  4563. Identifiers.propertyInterpolateV,
  4564. Identifiers.attribute,
  4565. Identifiers.attributeInterpolate1,
  4566. Identifiers.attributeInterpolate2,
  4567. Identifiers.attributeInterpolate3,
  4568. Identifiers.attributeInterpolate4,
  4569. Identifiers.attributeInterpolate5,
  4570. Identifiers.attributeInterpolate6,
  4571. Identifiers.attributeInterpolate7,
  4572. Identifiers.attributeInterpolate8,
  4573. Identifiers.attributeInterpolateV,
  4574. Identifiers.styleProp,
  4575. Identifiers.stylePropInterpolate1,
  4576. Identifiers.stylePropInterpolate2,
  4577. Identifiers.stylePropInterpolate3,
  4578. Identifiers.stylePropInterpolate4,
  4579. Identifiers.stylePropInterpolate5,
  4580. Identifiers.stylePropInterpolate6,
  4581. Identifiers.stylePropInterpolate7,
  4582. Identifiers.stylePropInterpolate8,
  4583. Identifiers.stylePropInterpolateV,
  4584. Identifiers.textInterpolate,
  4585. Identifiers.textInterpolate1,
  4586. Identifiers.textInterpolate2,
  4587. Identifiers.textInterpolate3,
  4588. Identifiers.textInterpolate4,
  4589. Identifiers.textInterpolate5,
  4590. Identifiers.textInterpolate6,
  4591. Identifiers.textInterpolate7,
  4592. Identifiers.textInterpolate8,
  4593. Identifiers.textInterpolateV,
  4594. ]);
  4595. /** Generates a call to a single instruction. */
  4596. function invokeInstruction(span, reference, params) {
  4597. return importExpr(reference, null, span).callFn(params, span);
  4598. }
  4599. /**
  4600. * Creates an allocator for a temporary variable.
  4601. *
  4602. * A variable declaration is added to the statements the first time the allocator is invoked.
  4603. */
  4604. function temporaryAllocator(statements, name) {
  4605. let temp = null;
  4606. return () => {
  4607. if (!temp) {
  4608. statements.push(new DeclareVarStmt(TEMPORARY_NAME, undefined, DYNAMIC_TYPE));
  4609. temp = variable(name);
  4610. }
  4611. return temp;
  4612. };
  4613. }
  4614. function invalid(arg) {
  4615. throw new Error(`Invalid state: Visitor ${this.constructor.name} doesn't handle ${arg.constructor.name}`);
  4616. }
  4617. function asLiteral(value) {
  4618. if (Array.isArray(value)) {
  4619. return literalArr(value.map(asLiteral));
  4620. }
  4621. return literal(value, INFERRED_TYPE);
  4622. }
  4623. function conditionallyCreateDirectiveBindingLiteral(map, keepDeclared) {
  4624. const keys = Object.getOwnPropertyNames(map);
  4625. if (keys.length === 0) {
  4626. return null;
  4627. }
  4628. return literalMap(keys.map(key => {
  4629. const value = map[key];
  4630. let declaredName;
  4631. let publicName;
  4632. let minifiedName;
  4633. let needsDeclaredName;
  4634. if (typeof value === 'string') {
  4635. // canonical syntax: `dirProp: publicProp`
  4636. declaredName = key;
  4637. minifiedName = key;
  4638. publicName = value;
  4639. needsDeclaredName = false;
  4640. }
  4641. else {
  4642. minifiedName = key;
  4643. declaredName = value.classPropertyName;
  4644. publicName = value.bindingPropertyName;
  4645. needsDeclaredName = publicName !== declaredName;
  4646. }
  4647. return {
  4648. key: minifiedName,
  4649. // put quotes around keys that contain potentially unsafe characters
  4650. quoted: UNSAFE_OBJECT_KEY_NAME_REGEXP.test(minifiedName),
  4651. value: (keepDeclared && needsDeclaredName) ?
  4652. literalArr([asLiteral(publicName), asLiteral(declaredName)]) :
  4653. asLiteral(publicName)
  4654. };
  4655. }));
  4656. }
  4657. /**
  4658. * Remove trailing null nodes as they are implied.
  4659. */
  4660. function trimTrailingNulls(parameters) {
  4661. while (isNull(parameters[parameters.length - 1])) {
  4662. parameters.pop();
  4663. }
  4664. return parameters;
  4665. }
  4666. function getQueryPredicate(query, constantPool) {
  4667. if (Array.isArray(query.predicate)) {
  4668. let predicate = [];
  4669. query.predicate.forEach((selector) => {
  4670. // Each item in predicates array may contain strings with comma-separated refs
  4671. // (for ex. 'ref, ref1, ..., refN'), thus we extract individual refs and store them
  4672. // as separate array entities
  4673. const selectors = selector.split(',').map(token => literal(token.trim()));
  4674. predicate.push(...selectors);
  4675. });
  4676. return constantPool.getConstLiteral(literalArr(predicate), true);
  4677. }
  4678. else {
  4679. // The original predicate may have been wrapped in a `forwardRef()` call.
  4680. switch (query.predicate.forwardRef) {
  4681. case 0 /* ForwardRefHandling.None */:
  4682. case 2 /* ForwardRefHandling.Unwrapped */:
  4683. return query.predicate.expression;
  4684. case 1 /* ForwardRefHandling.Wrapped */:
  4685. return importExpr(Identifiers.resolveForwardRef).callFn([query.predicate.expression]);
  4686. }
  4687. }
  4688. }
  4689. /**
  4690. * A representation for an object literal used during codegen of definition objects. The generic
  4691. * type `T` allows to reference a documented type of the generated structure, such that the
  4692. * property names that are set can be resolved to their documented declaration.
  4693. */
  4694. class DefinitionMap {
  4695. constructor() {
  4696. this.values = [];
  4697. }
  4698. set(key, value) {
  4699. if (value) {
  4700. this.values.push({ key: key, value, quoted: false });
  4701. }
  4702. }
  4703. toLiteralMap() {
  4704. return literalMap(this.values);
  4705. }
  4706. }
  4707. /**
  4708. * Extract a map of properties to values for a given element or template node, which can be used
  4709. * by the directive matching machinery.
  4710. *
  4711. * @param elOrTpl the element or template in question
  4712. * @return an object set up for directive matching. For attributes on the element/template, this
  4713. * object maps a property name to its (static) value. For any bindings, this map simply maps the
  4714. * property name to an empty string.
  4715. */
  4716. function getAttrsForDirectiveMatching(elOrTpl) {
  4717. const attributesMap = {};
  4718. if (elOrTpl instanceof Template && elOrTpl.tagName !== 'ng-template') {
  4719. elOrTpl.templateAttrs.forEach(a => attributesMap[a.name] = '');
  4720. }
  4721. else {
  4722. elOrTpl.attributes.forEach(a => {
  4723. if (!isI18nAttribute(a.name)) {
  4724. attributesMap[a.name] = a.value;
  4725. }
  4726. });
  4727. elOrTpl.inputs.forEach(i => {
  4728. if (i.type === 0 /* BindingType.Property */) {
  4729. attributesMap[i.name] = '';
  4730. }
  4731. });
  4732. elOrTpl.outputs.forEach(o => {
  4733. attributesMap[o.name] = '';
  4734. });
  4735. }
  4736. return attributesMap;
  4737. }
  4738. /**
  4739. * Gets the number of arguments expected to be passed to a generated instruction in the case of
  4740. * interpolation instructions.
  4741. * @param interpolation An interpolation ast
  4742. */
  4743. function getInterpolationArgsLength(interpolation) {
  4744. const { expressions, strings } = interpolation;
  4745. if (expressions.length === 1 && strings.length === 2 && strings[0] === '' && strings[1] === '') {
  4746. // If the interpolation has one interpolated value, but the prefix and suffix are both empty
  4747. // strings, we only pass one argument, to a special instruction like `propertyInterpolate` or
  4748. // `textInterpolate`.
  4749. return 1;
  4750. }
  4751. else {
  4752. return expressions.length + strings.length;
  4753. }
  4754. }
  4755. /**
  4756. * Generates the final instruction call statements based on the passed in configuration.
  4757. * Will try to chain instructions as much as possible, if chaining is supported.
  4758. */
  4759. function getInstructionStatements(instructions) {
  4760. const statements = [];
  4761. let pendingExpression = null;
  4762. let pendingExpressionType = null;
  4763. let chainLength = 0;
  4764. for (const current of instructions) {
  4765. const resolvedParams = (typeof current.paramsOrFn === 'function' ? current.paramsOrFn() : current.paramsOrFn) ??
  4766. [];
  4767. const params = Array.isArray(resolvedParams) ? resolvedParams : [resolvedParams];
  4768. // If the current instruction is the same as the previous one
  4769. // and it can be chained, add another call to the chain.
  4770. if (chainLength < MAX_CHAIN_LENGTH && pendingExpressionType === current.reference &&
  4771. CHAINABLE_INSTRUCTIONS.has(pendingExpressionType)) {
  4772. // We'll always have a pending expression when there's a pending expression type.
  4773. pendingExpression = pendingExpression.callFn(params, pendingExpression.sourceSpan);
  4774. chainLength++;
  4775. }
  4776. else {
  4777. if (pendingExpression !== null) {
  4778. statements.push(pendingExpression.toStmt());
  4779. }
  4780. pendingExpression = invokeInstruction(current.span, current.reference, params);
  4781. pendingExpressionType = current.reference;
  4782. chainLength = 0;
  4783. }
  4784. }
  4785. // Since the current instruction adds the previous one to the statements,
  4786. // we may be left with the final one at the end that is still pending.
  4787. if (pendingExpression !== null) {
  4788. statements.push(pendingExpression.toStmt());
  4789. }
  4790. return statements;
  4791. }
  4792. function compileInjectable(meta, resolveForwardRefs) {
  4793. let result = null;
  4794. const factoryMeta = {
  4795. name: meta.name,
  4796. type: meta.type,
  4797. typeArgumentCount: meta.typeArgumentCount,
  4798. deps: [],
  4799. target: FactoryTarget$1.Injectable,
  4800. };
  4801. if (meta.useClass !== undefined) {
  4802. // meta.useClass has two modes of operation. Either deps are specified, in which case `new` is
  4803. // used to instantiate the class with dependencies injected, or deps are not specified and
  4804. // the factory of the class is used to instantiate it.
  4805. //
  4806. // A special case exists for useClass: Type where Type is the injectable type itself and no
  4807. // deps are specified, in which case 'useClass' is effectively ignored.
  4808. const useClassOnSelf = meta.useClass.expression.isEquivalent(meta.type.value);
  4809. let deps = undefined;
  4810. if (meta.deps !== undefined) {
  4811. deps = meta.deps;
  4812. }
  4813. if (deps !== undefined) {
  4814. // factory: () => new meta.useClass(...deps)
  4815. result = compileFactoryFunction({
  4816. ...factoryMeta,
  4817. delegate: meta.useClass.expression,
  4818. delegateDeps: deps,
  4819. delegateType: R3FactoryDelegateType.Class,
  4820. });
  4821. }
  4822. else if (useClassOnSelf) {
  4823. result = compileFactoryFunction(factoryMeta);
  4824. }
  4825. else {
  4826. result = {
  4827. statements: [],
  4828. expression: delegateToFactory(meta.type.value, meta.useClass.expression, resolveForwardRefs)
  4829. };
  4830. }
  4831. }
  4832. else if (meta.useFactory !== undefined) {
  4833. if (meta.deps !== undefined) {
  4834. result = compileFactoryFunction({
  4835. ...factoryMeta,
  4836. delegate: meta.useFactory,
  4837. delegateDeps: meta.deps || [],
  4838. delegateType: R3FactoryDelegateType.Function,
  4839. });
  4840. }
  4841. else {
  4842. result = {
  4843. statements: [],
  4844. expression: fn([], [new ReturnStatement(meta.useFactory.callFn([]))])
  4845. };
  4846. }
  4847. }
  4848. else if (meta.useValue !== undefined) {
  4849. // Note: it's safe to use `meta.useValue` instead of the `USE_VALUE in meta` check used for
  4850. // client code because meta.useValue is an Expression which will be defined even if the actual
  4851. // value is undefined.
  4852. result = compileFactoryFunction({
  4853. ...factoryMeta,
  4854. expression: meta.useValue.expression,
  4855. });
  4856. }
  4857. else if (meta.useExisting !== undefined) {
  4858. // useExisting is an `inject` call on the existing token.
  4859. result = compileFactoryFunction({
  4860. ...factoryMeta,
  4861. expression: importExpr(Identifiers.inject).callFn([meta.useExisting.expression]),
  4862. });
  4863. }
  4864. else {
  4865. result = {
  4866. statements: [],
  4867. expression: delegateToFactory(meta.type.value, meta.type.value, resolveForwardRefs)
  4868. };
  4869. }
  4870. const token = meta.type.value;
  4871. const injectableProps = new DefinitionMap();
  4872. injectableProps.set('token', token);
  4873. injectableProps.set('factory', result.expression);
  4874. // Only generate providedIn property if it has a non-null value
  4875. if (meta.providedIn.expression.value !== null) {
  4876. injectableProps.set('providedIn', convertFromMaybeForwardRefExpression(meta.providedIn));
  4877. }
  4878. const expression = importExpr(Identifiers.ɵɵdefineInjectable)
  4879. .callFn([injectableProps.toLiteralMap()], undefined, true);
  4880. return {
  4881. expression,
  4882. type: createInjectableType(meta),
  4883. statements: result.statements,
  4884. };
  4885. }
  4886. function createInjectableType(meta) {
  4887. return new ExpressionType(importExpr(Identifiers.InjectableDeclaration, [typeWithParameters(meta.type.type, meta.typeArgumentCount)]));
  4888. }
  4889. function delegateToFactory(type, useType, unwrapForwardRefs) {
  4890. if (type.node === useType.node) {
  4891. // The types are the same, so we can simply delegate directly to the type's factory.
  4892. // ```
  4893. // factory: type.ɵfac
  4894. // ```
  4895. return useType.prop('ɵfac');
  4896. }
  4897. if (!unwrapForwardRefs) {
  4898. // The type is not wrapped in a `forwardRef()`, so we create a simple factory function that
  4899. // accepts a sub-type as an argument.
  4900. // ```
  4901. // factory: function(t) { return useType.ɵfac(t); }
  4902. // ```
  4903. return createFactoryFunction(useType);
  4904. }
  4905. // The useType is actually wrapped in a `forwardRef()` so we need to resolve that before
  4906. // calling its factory.
  4907. // ```
  4908. // factory: function(t) { return core.resolveForwardRef(type).ɵfac(t); }
  4909. // ```
  4910. const unwrappedType = importExpr(Identifiers.resolveForwardRef).callFn([useType]);
  4911. return createFactoryFunction(unwrappedType);
  4912. }
  4913. function createFactoryFunction(type) {
  4914. return fn([new FnParam('t', DYNAMIC_TYPE)], [new ReturnStatement(type.prop('ɵfac').callFn([variable('t')]))]);
  4915. }
  4916. const UNUSABLE_INTERPOLATION_REGEXPS = [
  4917. /^\s*$/,
  4918. /[<>]/,
  4919. /^[{}]$/,
  4920. /&(#|[a-z])/i,
  4921. /^\/\//, // comment
  4922. ];
  4923. function assertInterpolationSymbols(identifier, value) {
  4924. if (value != null && !(Array.isArray(value) && value.length == 2)) {
  4925. throw new Error(`Expected '${identifier}' to be an array, [start, end].`);
  4926. }
  4927. else if (value != null) {
  4928. const start = value[0];
  4929. const end = value[1];
  4930. // Check for unusable interpolation symbols
  4931. UNUSABLE_INTERPOLATION_REGEXPS.forEach(regexp => {
  4932. if (regexp.test(start) || regexp.test(end)) {
  4933. throw new Error(`['${start}', '${end}'] contains unusable interpolation symbol.`);
  4934. }
  4935. });
  4936. }
  4937. }
  4938. class InterpolationConfig {
  4939. static fromArray(markers) {
  4940. if (!markers) {
  4941. return DEFAULT_INTERPOLATION_CONFIG;
  4942. }
  4943. assertInterpolationSymbols('interpolation', markers);
  4944. return new InterpolationConfig(markers[0], markers[1]);
  4945. }
  4946. constructor(start, end) {
  4947. this.start = start;
  4948. this.end = end;
  4949. }
  4950. }
  4951. const DEFAULT_INTERPOLATION_CONFIG = new InterpolationConfig('{{', '}}');
  4952. const $EOF = 0;
  4953. const $BSPACE = 8;
  4954. const $TAB = 9;
  4955. const $LF = 10;
  4956. const $VTAB = 11;
  4957. const $FF = 12;
  4958. const $CR = 13;
  4959. const $SPACE = 32;
  4960. const $BANG = 33;
  4961. const $DQ = 34;
  4962. const $HASH = 35;
  4963. const $$ = 36;
  4964. const $PERCENT = 37;
  4965. const $AMPERSAND = 38;
  4966. const $SQ = 39;
  4967. const $LPAREN = 40;
  4968. const $RPAREN = 41;
  4969. const $STAR = 42;
  4970. const $PLUS = 43;
  4971. const $COMMA = 44;
  4972. const $MINUS = 45;
  4973. const $PERIOD = 46;
  4974. const $SLASH = 47;
  4975. const $COLON = 58;
  4976. const $SEMICOLON = 59;
  4977. const $LT = 60;
  4978. const $EQ = 61;
  4979. const $GT = 62;
  4980. const $QUESTION = 63;
  4981. const $0 = 48;
  4982. const $7 = 55;
  4983. const $9 = 57;
  4984. const $A = 65;
  4985. const $E = 69;
  4986. const $F = 70;
  4987. const $X = 88;
  4988. const $Z = 90;
  4989. const $LBRACKET = 91;
  4990. const $BACKSLASH = 92;
  4991. const $RBRACKET = 93;
  4992. const $CARET = 94;
  4993. const $_ = 95;
  4994. const $a = 97;
  4995. const $b = 98;
  4996. const $e = 101;
  4997. const $f = 102;
  4998. const $n = 110;
  4999. const $r = 114;
  5000. const $t = 116;
  5001. const $u = 117;
  5002. const $v = 118;
  5003. const $x = 120;
  5004. const $z = 122;
  5005. const $LBRACE = 123;
  5006. const $BAR = 124;
  5007. const $RBRACE = 125;
  5008. const $NBSP = 160;
  5009. const $PIPE = 124;
  5010. const $TILDA = 126;
  5011. const $AT = 64;
  5012. const $BT = 96;
  5013. function isWhitespace(code) {
  5014. return (code >= $TAB && code <= $SPACE) || (code == $NBSP);
  5015. }
  5016. function isDigit(code) {
  5017. return $0 <= code && code <= $9;
  5018. }
  5019. function isAsciiLetter(code) {
  5020. return code >= $a && code <= $z || code >= $A && code <= $Z;
  5021. }
  5022. function isAsciiHexDigit(code) {
  5023. return code >= $a && code <= $f || code >= $A && code <= $F || isDigit(code);
  5024. }
  5025. function isNewLine(code) {
  5026. return code === $LF || code === $CR;
  5027. }
  5028. function isOctalDigit(code) {
  5029. return $0 <= code && code <= $7;
  5030. }
  5031. function isQuote(code) {
  5032. return code === $SQ || code === $DQ || code === $BT;
  5033. }
  5034. class ParseLocation {
  5035. constructor(file, offset, line, col) {
  5036. this.file = file;
  5037. this.offset = offset;
  5038. this.line = line;
  5039. this.col = col;
  5040. }
  5041. toString() {
  5042. return this.offset != null ? `${this.file.url}@${this.line}:${this.col}` : this.file.url;
  5043. }
  5044. moveBy(delta) {
  5045. const source = this.file.content;
  5046. const len = source.length;
  5047. let offset = this.offset;
  5048. let line = this.line;
  5049. let col = this.col;
  5050. while (offset > 0 && delta < 0) {
  5051. offset--;
  5052. delta++;
  5053. const ch = source.charCodeAt(offset);
  5054. if (ch == $LF) {
  5055. line--;
  5056. const priorLine = source.substring(0, offset - 1).lastIndexOf(String.fromCharCode($LF));
  5057. col = priorLine > 0 ? offset - priorLine : offset;
  5058. }
  5059. else {
  5060. col--;
  5061. }
  5062. }
  5063. while (offset < len && delta > 0) {
  5064. const ch = source.charCodeAt(offset);
  5065. offset++;
  5066. delta--;
  5067. if (ch == $LF) {
  5068. line++;
  5069. col = 0;
  5070. }
  5071. else {
  5072. col++;
  5073. }
  5074. }
  5075. return new ParseLocation(this.file, offset, line, col);
  5076. }
  5077. // Return the source around the location
  5078. // Up to `maxChars` or `maxLines` on each side of the location
  5079. getContext(maxChars, maxLines) {
  5080. const content = this.file.content;
  5081. let startOffset = this.offset;
  5082. if (startOffset != null) {
  5083. if (startOffset > content.length - 1) {
  5084. startOffset = content.length - 1;
  5085. }
  5086. let endOffset = startOffset;
  5087. let ctxChars = 0;
  5088. let ctxLines = 0;
  5089. while (ctxChars < maxChars && startOffset > 0) {
  5090. startOffset--;
  5091. ctxChars++;
  5092. if (content[startOffset] == '\n') {
  5093. if (++ctxLines == maxLines) {
  5094. break;
  5095. }
  5096. }
  5097. }
  5098. ctxChars = 0;
  5099. ctxLines = 0;
  5100. while (ctxChars < maxChars && endOffset < content.length - 1) {
  5101. endOffset++;
  5102. ctxChars++;
  5103. if (content[endOffset] == '\n') {
  5104. if (++ctxLines == maxLines) {
  5105. break;
  5106. }
  5107. }
  5108. }
  5109. return {
  5110. before: content.substring(startOffset, this.offset),
  5111. after: content.substring(this.offset, endOffset + 1),
  5112. };
  5113. }
  5114. return null;
  5115. }
  5116. }
  5117. class ParseSourceFile {
  5118. constructor(content, url) {
  5119. this.content = content;
  5120. this.url = url;
  5121. }
  5122. }
  5123. class ParseSourceSpan {
  5124. /**
  5125. * Create an object that holds information about spans of tokens/nodes captured during
  5126. * lexing/parsing of text.
  5127. *
  5128. * @param start
  5129. * The location of the start of the span (having skipped leading trivia).
  5130. * Skipping leading trivia makes source-spans more "user friendly", since things like HTML
  5131. * elements will appear to begin at the start of the opening tag, rather than at the start of any
  5132. * leading trivia, which could include newlines.
  5133. *
  5134. * @param end
  5135. * The location of the end of the span.
  5136. *
  5137. * @param fullStart
  5138. * The start of the token without skipping the leading trivia.
  5139. * This is used by tooling that splits tokens further, such as extracting Angular interpolations
  5140. * from text tokens. Such tooling creates new source-spans relative to the original token's
  5141. * source-span. If leading trivia characters have been skipped then the new source-spans may be
  5142. * incorrectly offset.
  5143. *
  5144. * @param details
  5145. * Additional information (such as identifier names) that should be associated with the span.
  5146. */
  5147. constructor(start, end, fullStart = start, details = null) {
  5148. this.start = start;
  5149. this.end = end;
  5150. this.fullStart = fullStart;
  5151. this.details = details;
  5152. }
  5153. toString() {
  5154. return this.start.file.content.substring(this.start.offset, this.end.offset);
  5155. }
  5156. }
  5157. var ParseErrorLevel;
  5158. (function (ParseErrorLevel) {
  5159. ParseErrorLevel[ParseErrorLevel["WARNING"] = 0] = "WARNING";
  5160. ParseErrorLevel[ParseErrorLevel["ERROR"] = 1] = "ERROR";
  5161. })(ParseErrorLevel || (ParseErrorLevel = {}));
  5162. class ParseError {
  5163. constructor(span, msg, level = ParseErrorLevel.ERROR) {
  5164. this.span = span;
  5165. this.msg = msg;
  5166. this.level = level;
  5167. }
  5168. contextualMessage() {
  5169. const ctx = this.span.start.getContext(100, 3);
  5170. return ctx ? `${this.msg} ("${ctx.before}[${ParseErrorLevel[this.level]} ->]${ctx.after}")` :
  5171. this.msg;
  5172. }
  5173. toString() {
  5174. const details = this.span.details ? `, ${this.span.details}` : '';
  5175. return `${this.contextualMessage()}: ${this.span.start}${details}`;
  5176. }
  5177. }
  5178. /**
  5179. * Generates Source Span object for a given R3 Type for JIT mode.
  5180. *
  5181. * @param kind Component or Directive.
  5182. * @param typeName name of the Component or Directive.
  5183. * @param sourceUrl reference to Component or Directive source.
  5184. * @returns instance of ParseSourceSpan that represent a given Component or Directive.
  5185. */
  5186. function r3JitTypeSourceSpan(kind, typeName, sourceUrl) {
  5187. const sourceFileName = `in ${kind} ${typeName} in ${sourceUrl}`;
  5188. const sourceFile = new ParseSourceFile('', sourceFileName);
  5189. return new ParseSourceSpan(new ParseLocation(sourceFile, -1, -1, -1), new ParseLocation(sourceFile, -1, -1, -1));
  5190. }
  5191. let _anonymousTypeIndex = 0;
  5192. function identifierName(compileIdentifier) {
  5193. if (!compileIdentifier || !compileIdentifier.reference) {
  5194. return null;
  5195. }
  5196. const ref = compileIdentifier.reference;
  5197. if (ref['__anonymousType']) {
  5198. return ref['__anonymousType'];
  5199. }
  5200. if (ref['__forward_ref__']) {
  5201. // We do not want to try to stringify a `forwardRef()` function because that would cause the
  5202. // inner function to be evaluated too early, defeating the whole point of the `forwardRef`.
  5203. return '__forward_ref__';
  5204. }
  5205. let identifier = stringify(ref);
  5206. if (identifier.indexOf('(') >= 0) {
  5207. // case: anonymous functions!
  5208. identifier = `anonymous_${_anonymousTypeIndex++}`;
  5209. ref['__anonymousType'] = identifier;
  5210. }
  5211. else {
  5212. identifier = sanitizeIdentifier(identifier);
  5213. }
  5214. return identifier;
  5215. }
  5216. function sanitizeIdentifier(name) {
  5217. return name.replace(/\W/g, '_');
  5218. }
  5219. /**
  5220. * In TypeScript, tagged template functions expect a "template object", which is an array of
  5221. * "cooked" strings plus a `raw` property that contains an array of "raw" strings. This is
  5222. * typically constructed with a function called `__makeTemplateObject(cooked, raw)`, but it may not
  5223. * be available in all environments.
  5224. *
  5225. * This is a JavaScript polyfill that uses __makeTemplateObject when it's available, but otherwise
  5226. * creates an inline helper with the same functionality.
  5227. *
  5228. * In the inline function, if `Object.defineProperty` is available we use that to attach the `raw`
  5229. * array.
  5230. */
  5231. const makeTemplateObjectPolyfill = '(this&&this.__makeTemplateObject||function(e,t){return Object.defineProperty?Object.defineProperty(e,"raw",{value:t}):e.raw=t,e})';
  5232. class AbstractJsEmitterVisitor extends AbstractEmitterVisitor {
  5233. constructor() {
  5234. super(false);
  5235. }
  5236. visitWrappedNodeExpr(ast, ctx) {
  5237. throw new Error('Cannot emit a WrappedNodeExpr in Javascript.');
  5238. }
  5239. visitDeclareVarStmt(stmt, ctx) {
  5240. ctx.print(stmt, `var ${stmt.name}`);
  5241. if (stmt.value) {
  5242. ctx.print(stmt, ' = ');
  5243. stmt.value.visitExpression(this, ctx);
  5244. }
  5245. ctx.println(stmt, `;`);
  5246. return null;
  5247. }
  5248. visitTaggedTemplateExpr(ast, ctx) {
  5249. // The following convoluted piece of code is effectively the downlevelled equivalent of
  5250. // ```
  5251. // tag`...`
  5252. // ```
  5253. // which is effectively like:
  5254. // ```
  5255. // tag(__makeTemplateObject(cooked, raw), expression1, expression2, ...);
  5256. // ```
  5257. const elements = ast.template.elements;
  5258. ast.tag.visitExpression(this, ctx);
  5259. ctx.print(ast, `(${makeTemplateObjectPolyfill}(`);
  5260. ctx.print(ast, `[${elements.map(part => escapeIdentifier(part.text, false)).join(', ')}], `);
  5261. ctx.print(ast, `[${elements.map(part => escapeIdentifier(part.rawText, false)).join(', ')}])`);
  5262. ast.template.expressions.forEach(expression => {
  5263. ctx.print(ast, ', ');
  5264. expression.visitExpression(this, ctx);
  5265. });
  5266. ctx.print(ast, ')');
  5267. return null;
  5268. }
  5269. visitFunctionExpr(ast, ctx) {
  5270. ctx.print(ast, `function${ast.name ? ' ' + ast.name : ''}(`);
  5271. this._visitParams(ast.params, ctx);
  5272. ctx.println(ast, `) {`);
  5273. ctx.incIndent();
  5274. this.visitAllStatements(ast.statements, ctx);
  5275. ctx.decIndent();
  5276. ctx.print(ast, `}`);
  5277. return null;
  5278. }
  5279. visitDeclareFunctionStmt(stmt, ctx) {
  5280. ctx.print(stmt, `function ${stmt.name}(`);
  5281. this._visitParams(stmt.params, ctx);
  5282. ctx.println(stmt, `) {`);
  5283. ctx.incIndent();
  5284. this.visitAllStatements(stmt.statements, ctx);
  5285. ctx.decIndent();
  5286. ctx.println(stmt, `}`);
  5287. return null;
  5288. }
  5289. visitLocalizedString(ast, ctx) {
  5290. // The following convoluted piece of code is effectively the downlevelled equivalent of
  5291. // ```
  5292. // $localize `...`
  5293. // ```
  5294. // which is effectively like:
  5295. // ```
  5296. // $localize(__makeTemplateObject(cooked, raw), expression1, expression2, ...);
  5297. // ```
  5298. ctx.print(ast, `$localize(${makeTemplateObjectPolyfill}(`);
  5299. const parts = [ast.serializeI18nHead()];
  5300. for (let i = 1; i < ast.messageParts.length; i++) {
  5301. parts.push(ast.serializeI18nTemplatePart(i));
  5302. }
  5303. ctx.print(ast, `[${parts.map(part => escapeIdentifier(part.cooked, false)).join(', ')}], `);
  5304. ctx.print(ast, `[${parts.map(part => escapeIdentifier(part.raw, false)).join(', ')}])`);
  5305. ast.expressions.forEach(expression => {
  5306. ctx.print(ast, ', ');
  5307. expression.visitExpression(this, ctx);
  5308. });
  5309. ctx.print(ast, ')');
  5310. return null;
  5311. }
  5312. _visitParams(params, ctx) {
  5313. this.visitAllObjects(param => ctx.print(null, param.name), params, ctx, ',');
  5314. }
  5315. }
  5316. /**
  5317. * @fileoverview
  5318. * A module to facilitate use of a Trusted Types policy within the JIT
  5319. * compiler. It lazily constructs the Trusted Types policy, providing helper
  5320. * utilities for promoting strings to Trusted Types. When Trusted Types are not
  5321. * available, strings are used as a fallback.
  5322. * @security All use of this module is security-sensitive and should go through
  5323. * security review.
  5324. */
  5325. /**
  5326. * The Trusted Types policy, or null if Trusted Types are not
  5327. * enabled/supported, or undefined if the policy has not been created yet.
  5328. */
  5329. let policy;
  5330. /**
  5331. * Returns the Trusted Types policy, or null if Trusted Types are not
  5332. * enabled/supported. The first call to this function will create the policy.
  5333. */
  5334. function getPolicy() {
  5335. if (policy === undefined) {
  5336. policy = null;
  5337. if (_global.trustedTypes) {
  5338. try {
  5339. policy =
  5340. _global.trustedTypes.createPolicy('angular#unsafe-jit', {
  5341. createScript: (s) => s,
  5342. });
  5343. }
  5344. catch {
  5345. // trustedTypes.createPolicy throws if called with a name that is
  5346. // already registered, even in report-only mode. Until the API changes,
  5347. // catch the error not to break the applications functionally. In such
  5348. // cases, the code will fall back to using strings.
  5349. }
  5350. }
  5351. }
  5352. return policy;
  5353. }
  5354. /**
  5355. * Unsafely promote a string to a TrustedScript, falling back to strings when
  5356. * Trusted Types are not available.
  5357. * @security In particular, it must be assured that the provided string will
  5358. * never cause an XSS vulnerability if used in a context that will be
  5359. * interpreted and executed as a script by a browser, e.g. when calling eval.
  5360. */
  5361. function trustedScriptFromString(script) {
  5362. return getPolicy()?.createScript(script) || script;
  5363. }
  5364. /**
  5365. * Unsafely call the Function constructor with the given string arguments.
  5366. * @security This is a security-sensitive function; any use of this function
  5367. * must go through security review. In particular, it must be assured that it
  5368. * is only called from the JIT compiler, as use in other code can lead to XSS
  5369. * vulnerabilities.
  5370. */
  5371. function newTrustedFunctionForJIT(...args) {
  5372. if (!_global.trustedTypes) {
  5373. // In environments that don't support Trusted Types, fall back to the most
  5374. // straightforward implementation:
  5375. return new Function(...args);
  5376. }
  5377. // Chrome currently does not support passing TrustedScript to the Function
  5378. // constructor. The following implements the workaround proposed on the page
  5379. // below, where the Chromium bug is also referenced:
  5380. // https://github.com/w3c/webappsec-trusted-types/wiki/Trusted-Types-for-function-constructor
  5381. const fnArgs = args.slice(0, -1).join(',');
  5382. const fnBody = args[args.length - 1];
  5383. const body = `(function anonymous(${fnArgs}
  5384. ) { ${fnBody}
  5385. })`;
  5386. // Using eval directly confuses the compiler and prevents this module from
  5387. // being stripped out of JS binaries even if not used. The global['eval']
  5388. // indirection fixes that.
  5389. const fn = _global['eval'](trustedScriptFromString(body));
  5390. if (fn.bind === undefined) {
  5391. // Workaround for a browser bug that only exists in Chrome 83, where passing
  5392. // a TrustedScript to eval just returns the TrustedScript back without
  5393. // evaluating it. In that case, fall back to the most straightforward
  5394. // implementation:
  5395. return new Function(...args);
  5396. }
  5397. // To completely mimic the behavior of calling "new Function", two more
  5398. // things need to happen:
  5399. // 1. Stringifying the resulting function should return its source code
  5400. fn.toString = () => body;
  5401. // 2. When calling the resulting function, `this` should refer to `global`
  5402. return fn.bind(_global);
  5403. // When Trusted Types support in Function constructors is widely available,
  5404. // the implementation of this function can be simplified to:
  5405. // return new Function(...args.map(a => trustedScriptFromString(a)));
  5406. }
  5407. /**
  5408. * A helper class to manage the evaluation of JIT generated code.
  5409. */
  5410. class JitEvaluator {
  5411. /**
  5412. *
  5413. * @param sourceUrl The URL of the generated code.
  5414. * @param statements An array of Angular statement AST nodes to be evaluated.
  5415. * @param refResolver Resolves `o.ExternalReference`s into values.
  5416. * @param createSourceMaps If true then create a source-map for the generated code and include it
  5417. * inline as a source-map comment.
  5418. * @returns A map of all the variables in the generated code.
  5419. */
  5420. evaluateStatements(sourceUrl, statements, refResolver, createSourceMaps) {
  5421. const converter = new JitEmitterVisitor(refResolver);
  5422. const ctx = EmitterVisitorContext.createRoot();
  5423. // Ensure generated code is in strict mode
  5424. if (statements.length > 0 && !isUseStrictStatement(statements[0])) {
  5425. statements = [
  5426. literal('use strict').toStmt(),
  5427. ...statements,
  5428. ];
  5429. }
  5430. converter.visitAllStatements(statements, ctx);
  5431. converter.createReturnStmt(ctx);
  5432. return this.evaluateCode(sourceUrl, ctx, converter.getArgs(), createSourceMaps);
  5433. }
  5434. /**
  5435. * Evaluate a piece of JIT generated code.
  5436. * @param sourceUrl The URL of this generated code.
  5437. * @param ctx A context object that contains an AST of the code to be evaluated.
  5438. * @param vars A map containing the names and values of variables that the evaluated code might
  5439. * reference.
  5440. * @param createSourceMap If true then create a source-map for the generated code and include it
  5441. * inline as a source-map comment.
  5442. * @returns The result of evaluating the code.
  5443. */
  5444. evaluateCode(sourceUrl, ctx, vars, createSourceMap) {
  5445. let fnBody = `"use strict";${ctx.toSource()}\n//# sourceURL=${sourceUrl}`;
  5446. const fnArgNames = [];
  5447. const fnArgValues = [];
  5448. for (const argName in vars) {
  5449. fnArgValues.push(vars[argName]);
  5450. fnArgNames.push(argName);
  5451. }
  5452. if (createSourceMap) {
  5453. // using `new Function(...)` generates a header, 1 line of no arguments, 2 lines otherwise
  5454. // E.g. ```
  5455. // function anonymous(a,b,c
  5456. // /**/) { ... }```
  5457. // We don't want to hard code this fact, so we auto detect it via an empty function first.
  5458. const emptyFn = newTrustedFunctionForJIT(...fnArgNames.concat('return null;')).toString();
  5459. const headerLines = emptyFn.slice(0, emptyFn.indexOf('return null;')).split('\n').length - 1;
  5460. fnBody += `\n${ctx.toSourceMapGenerator(sourceUrl, headerLines).toJsComment()}`;
  5461. }
  5462. const fn = newTrustedFunctionForJIT(...fnArgNames.concat(fnBody));
  5463. return this.executeFunction(fn, fnArgValues);
  5464. }
  5465. /**
  5466. * Execute a JIT generated function by calling it.
  5467. *
  5468. * This method can be overridden in tests to capture the functions that are generated
  5469. * by this `JitEvaluator` class.
  5470. *
  5471. * @param fn A function to execute.
  5472. * @param args The arguments to pass to the function being executed.
  5473. * @returns The return value of the executed function.
  5474. */
  5475. executeFunction(fn, args) {
  5476. return fn(...args);
  5477. }
  5478. }
  5479. /**
  5480. * An Angular AST visitor that converts AST nodes into executable JavaScript code.
  5481. */
  5482. class JitEmitterVisitor extends AbstractJsEmitterVisitor {
  5483. constructor(refResolver) {
  5484. super();
  5485. this.refResolver = refResolver;
  5486. this._evalArgNames = [];
  5487. this._evalArgValues = [];
  5488. this._evalExportedVars = [];
  5489. }
  5490. createReturnStmt(ctx) {
  5491. const stmt = new ReturnStatement(new LiteralMapExpr(this._evalExportedVars.map(resultVar => new LiteralMapEntry(resultVar, variable(resultVar), false))));
  5492. stmt.visitStatement(this, ctx);
  5493. }
  5494. getArgs() {
  5495. const result = {};
  5496. for (let i = 0; i < this._evalArgNames.length; i++) {
  5497. result[this._evalArgNames[i]] = this._evalArgValues[i];
  5498. }
  5499. return result;
  5500. }
  5501. visitExternalExpr(ast, ctx) {
  5502. this._emitReferenceToExternal(ast, this.refResolver.resolveExternalReference(ast.value), ctx);
  5503. return null;
  5504. }
  5505. visitWrappedNodeExpr(ast, ctx) {
  5506. this._emitReferenceToExternal(ast, ast.node, ctx);
  5507. return null;
  5508. }
  5509. visitDeclareVarStmt(stmt, ctx) {
  5510. if (stmt.hasModifier(StmtModifier.Exported)) {
  5511. this._evalExportedVars.push(stmt.name);
  5512. }
  5513. return super.visitDeclareVarStmt(stmt, ctx);
  5514. }
  5515. visitDeclareFunctionStmt(stmt, ctx) {
  5516. if (stmt.hasModifier(StmtModifier.Exported)) {
  5517. this._evalExportedVars.push(stmt.name);
  5518. }
  5519. return super.visitDeclareFunctionStmt(stmt, ctx);
  5520. }
  5521. _emitReferenceToExternal(ast, value, ctx) {
  5522. let id = this._evalArgValues.indexOf(value);
  5523. if (id === -1) {
  5524. id = this._evalArgValues.length;
  5525. this._evalArgValues.push(value);
  5526. const name = identifierName({ reference: value }) || 'val';
  5527. this._evalArgNames.push(`jit_${name}_${id}`);
  5528. }
  5529. ctx.print(ast, this._evalArgNames[id]);
  5530. }
  5531. }
  5532. function isUseStrictStatement(statement) {
  5533. return statement.isEquivalent(literal('use strict').toStmt());
  5534. }
  5535. function compileInjector(meta) {
  5536. const definitionMap = new DefinitionMap();
  5537. if (meta.providers !== null) {
  5538. definitionMap.set('providers', meta.providers);
  5539. }
  5540. if (meta.imports.length > 0) {
  5541. definitionMap.set('imports', literalArr(meta.imports));
  5542. }
  5543. const expression = importExpr(Identifiers.defineInjector).callFn([definitionMap.toLiteralMap()], undefined, true);
  5544. const type = createInjectorType(meta);
  5545. return { expression, type, statements: [] };
  5546. }
  5547. function createInjectorType(meta) {
  5548. return new ExpressionType(importExpr(Identifiers.InjectorDeclaration, [new ExpressionType(meta.type.type)]));
  5549. }
  5550. /**
  5551. * Implementation of `CompileReflector` which resolves references to @angular/core
  5552. * symbols at runtime, according to a consumer-provided mapping.
  5553. *
  5554. * Only supports `resolveExternalReference`, all other methods throw.
  5555. */
  5556. class R3JitReflector {
  5557. constructor(context) {
  5558. this.context = context;
  5559. }
  5560. resolveExternalReference(ref) {
  5561. // This reflector only handles @angular/core imports.
  5562. if (ref.moduleName !== '@angular/core') {
  5563. throw new Error(`Cannot resolve external reference to ${ref.moduleName}, only references to @angular/core are supported.`);
  5564. }
  5565. if (!this.context.hasOwnProperty(ref.name)) {
  5566. throw new Error(`No value provided for @angular/core symbol '${ref.name}'.`);
  5567. }
  5568. return this.context[ref.name];
  5569. }
  5570. }
  5571. /**
  5572. * How the selector scope of an NgModule (its declarations, imports, and exports) should be emitted
  5573. * as a part of the NgModule definition.
  5574. */
  5575. var R3SelectorScopeMode;
  5576. (function (R3SelectorScopeMode) {
  5577. /**
  5578. * Emit the declarations inline into the module definition.
  5579. *
  5580. * This option is useful in certain contexts where it's known that JIT support is required. The
  5581. * tradeoff here is that this emit style prevents directives and pipes from being tree-shaken if
  5582. * they are unused, but the NgModule is used.
  5583. */
  5584. R3SelectorScopeMode[R3SelectorScopeMode["Inline"] = 0] = "Inline";
  5585. /**
  5586. * Emit the declarations using a side effectful function call, `ɵɵsetNgModuleScope`, that is
  5587. * guarded with the `ngJitMode` flag.
  5588. *
  5589. * This form of emit supports JIT and can be optimized away if the `ngJitMode` flag is set to
  5590. * false, which allows unused directives and pipes to be tree-shaken.
  5591. */
  5592. R3SelectorScopeMode[R3SelectorScopeMode["SideEffect"] = 1] = "SideEffect";
  5593. /**
  5594. * Don't generate selector scopes at all.
  5595. *
  5596. * This is useful for contexts where JIT support is known to be unnecessary.
  5597. */
  5598. R3SelectorScopeMode[R3SelectorScopeMode["Omit"] = 2] = "Omit";
  5599. })(R3SelectorScopeMode || (R3SelectorScopeMode = {}));
  5600. /**
  5601. * Construct an `R3NgModuleDef` for the given `R3NgModuleMetadata`.
  5602. */
  5603. function compileNgModule(meta) {
  5604. const { type: moduleType, bootstrap, declarations, imports, exports, schemas, containsForwardDecls, selectorScopeMode, id } = meta;
  5605. const statements = [];
  5606. const definitionMap = new DefinitionMap();
  5607. definitionMap.set('type', moduleType.value);
  5608. if (bootstrap.length > 0) {
  5609. definitionMap.set('bootstrap', refsToArray(bootstrap, containsForwardDecls));
  5610. }
  5611. if (selectorScopeMode === R3SelectorScopeMode.Inline) {
  5612. // If requested to emit scope information inline, pass the `declarations`, `imports` and
  5613. // `exports` to the `ɵɵdefineNgModule()` call directly.
  5614. if (declarations.length > 0) {
  5615. definitionMap.set('declarations', refsToArray(declarations, containsForwardDecls));
  5616. }
  5617. if (imports.length > 0) {
  5618. definitionMap.set('imports', refsToArray(imports, containsForwardDecls));
  5619. }
  5620. if (exports.length > 0) {
  5621. definitionMap.set('exports', refsToArray(exports, containsForwardDecls));
  5622. }
  5623. }
  5624. else if (selectorScopeMode === R3SelectorScopeMode.SideEffect) {
  5625. // In this mode, scope information is not passed into `ɵɵdefineNgModule` as it
  5626. // would prevent tree-shaking of the declarations, imports and exports references. Instead, it's
  5627. // patched onto the NgModule definition with a `ɵɵsetNgModuleScope` call that's guarded by the
  5628. // `ngJitMode` flag.
  5629. const setNgModuleScopeCall = generateSetNgModuleScopeCall(meta);
  5630. if (setNgModuleScopeCall !== null) {
  5631. statements.push(setNgModuleScopeCall);
  5632. }
  5633. }
  5634. else {
  5635. // Selector scope emit was not requested, so skip it.
  5636. }
  5637. if (schemas !== null && schemas.length > 0) {
  5638. definitionMap.set('schemas', literalArr(schemas.map(ref => ref.value)));
  5639. }
  5640. if (id !== null) {
  5641. definitionMap.set('id', id);
  5642. // Generate a side-effectful call to register this NgModule by its id, as per the semantics of
  5643. // NgModule ids.
  5644. statements.push(importExpr(Identifiers.registerNgModuleType).callFn([moduleType.value, id]).toStmt());
  5645. }
  5646. const expression = importExpr(Identifiers.defineNgModule).callFn([definitionMap.toLiteralMap()], undefined, true);
  5647. const type = createNgModuleType(meta);
  5648. return { expression, type, statements };
  5649. }
  5650. /**
  5651. * This function is used in JIT mode to generate the call to `ɵɵdefineNgModule()` from a call to
  5652. * `ɵɵngDeclareNgModule()`.
  5653. */
  5654. function compileNgModuleDeclarationExpression(meta) {
  5655. const definitionMap = new DefinitionMap();
  5656. definitionMap.set('type', new WrappedNodeExpr(meta.type));
  5657. if (meta.bootstrap !== undefined) {
  5658. definitionMap.set('bootstrap', new WrappedNodeExpr(meta.bootstrap));
  5659. }
  5660. if (meta.declarations !== undefined) {
  5661. definitionMap.set('declarations', new WrappedNodeExpr(meta.declarations));
  5662. }
  5663. if (meta.imports !== undefined) {
  5664. definitionMap.set('imports', new WrappedNodeExpr(meta.imports));
  5665. }
  5666. if (meta.exports !== undefined) {
  5667. definitionMap.set('exports', new WrappedNodeExpr(meta.exports));
  5668. }
  5669. if (meta.schemas !== undefined) {
  5670. definitionMap.set('schemas', new WrappedNodeExpr(meta.schemas));
  5671. }
  5672. if (meta.id !== undefined) {
  5673. definitionMap.set('id', new WrappedNodeExpr(meta.id));
  5674. }
  5675. return importExpr(Identifiers.defineNgModule).callFn([definitionMap.toLiteralMap()]);
  5676. }
  5677. function createNgModuleType({ type: moduleType, declarations, exports, imports, includeImportTypes, publicDeclarationTypes }) {
  5678. return new ExpressionType(importExpr(Identifiers.NgModuleDeclaration, [
  5679. new ExpressionType(moduleType.type),
  5680. publicDeclarationTypes === null ? tupleTypeOf(declarations) :
  5681. tupleOfTypes(publicDeclarationTypes),
  5682. includeImportTypes ? tupleTypeOf(imports) : NONE_TYPE,
  5683. tupleTypeOf(exports),
  5684. ]));
  5685. }
  5686. /**
  5687. * Generates a function call to `ɵɵsetNgModuleScope` with all necessary information so that the
  5688. * transitive module scope can be computed during runtime in JIT mode. This call is marked pure
  5689. * such that the references to declarations, imports and exports may be elided causing these
  5690. * symbols to become tree-shakeable.
  5691. */
  5692. function generateSetNgModuleScopeCall(meta) {
  5693. const { type: moduleType, declarations, imports, exports, containsForwardDecls } = meta;
  5694. const scopeMap = new DefinitionMap();
  5695. if (declarations.length > 0) {
  5696. scopeMap.set('declarations', refsToArray(declarations, containsForwardDecls));
  5697. }
  5698. if (imports.length > 0) {
  5699. scopeMap.set('imports', refsToArray(imports, containsForwardDecls));
  5700. }
  5701. if (exports.length > 0) {
  5702. scopeMap.set('exports', refsToArray(exports, containsForwardDecls));
  5703. }
  5704. if (Object.keys(scopeMap.values).length === 0) {
  5705. return null;
  5706. }
  5707. // setNgModuleScope(...)
  5708. const fnCall = new InvokeFunctionExpr(
  5709. /* fn */ importExpr(Identifiers.setNgModuleScope),
  5710. /* args */ [moduleType.value, scopeMap.toLiteralMap()]);
  5711. // (ngJitMode guard) && setNgModuleScope(...)
  5712. const guardedCall = jitOnlyGuardedExpression(fnCall);
  5713. // function() { (ngJitMode guard) && setNgModuleScope(...); }
  5714. const iife = new FunctionExpr(
  5715. /* params */ [],
  5716. /* statements */ [guardedCall.toStmt()]);
  5717. // (function() { (ngJitMode guard) && setNgModuleScope(...); })()
  5718. const iifeCall = new InvokeFunctionExpr(
  5719. /* fn */ iife,
  5720. /* args */ []);
  5721. return iifeCall.toStmt();
  5722. }
  5723. function tupleTypeOf(exp) {
  5724. const types = exp.map(ref => typeofExpr(ref.type));
  5725. return exp.length > 0 ? expressionType(literalArr(types)) : NONE_TYPE;
  5726. }
  5727. function tupleOfTypes(types) {
  5728. const typeofTypes = types.map(type => typeofExpr(type));
  5729. return types.length > 0 ? expressionType(literalArr(typeofTypes)) : NONE_TYPE;
  5730. }
  5731. function compilePipeFromMetadata(metadata) {
  5732. const definitionMapValues = [];
  5733. // e.g. `name: 'myPipe'`
  5734. definitionMapValues.push({ key: 'name', value: literal(metadata.pipeName), quoted: false });
  5735. // e.g. `type: MyPipe`
  5736. definitionMapValues.push({ key: 'type', value: metadata.type.value, quoted: false });
  5737. // e.g. `pure: true`
  5738. definitionMapValues.push({ key: 'pure', value: literal(metadata.pure), quoted: false });
  5739. if (metadata.isStandalone) {
  5740. definitionMapValues.push({ key: 'standalone', value: literal(true), quoted: false });
  5741. }
  5742. const expression = importExpr(Identifiers.definePipe).callFn([literalMap(definitionMapValues)], undefined, true);
  5743. const type = createPipeType(metadata);
  5744. return { expression, type, statements: [] };
  5745. }
  5746. function createPipeType(metadata) {
  5747. return new ExpressionType(importExpr(Identifiers.PipeDeclaration, [
  5748. typeWithParameters(metadata.type.type, metadata.typeArgumentCount),
  5749. new ExpressionType(new LiteralExpr(metadata.pipeName)),
  5750. new ExpressionType(new LiteralExpr(metadata.isStandalone)),
  5751. ]));
  5752. }
  5753. var R3TemplateDependencyKind;
  5754. (function (R3TemplateDependencyKind) {
  5755. R3TemplateDependencyKind[R3TemplateDependencyKind["Directive"] = 0] = "Directive";
  5756. R3TemplateDependencyKind[R3TemplateDependencyKind["Pipe"] = 1] = "Pipe";
  5757. R3TemplateDependencyKind[R3TemplateDependencyKind["NgModule"] = 2] = "NgModule";
  5758. })(R3TemplateDependencyKind || (R3TemplateDependencyKind = {}));
  5759. class ParserError {
  5760. constructor(message, input, errLocation, ctxLocation) {
  5761. this.input = input;
  5762. this.errLocation = errLocation;
  5763. this.ctxLocation = ctxLocation;
  5764. this.message = `Parser Error: ${message} ${errLocation} [${input}] in ${ctxLocation}`;
  5765. }
  5766. }
  5767. class ParseSpan {
  5768. constructor(start, end) {
  5769. this.start = start;
  5770. this.end = end;
  5771. }
  5772. toAbsolute(absoluteOffset) {
  5773. return new AbsoluteSourceSpan(absoluteOffset + this.start, absoluteOffset + this.end);
  5774. }
  5775. }
  5776. class AST {
  5777. constructor(span,
  5778. /**
  5779. * Absolute location of the expression AST in a source code file.
  5780. */
  5781. sourceSpan) {
  5782. this.span = span;
  5783. this.sourceSpan = sourceSpan;
  5784. }
  5785. toString() {
  5786. return 'AST';
  5787. }
  5788. }
  5789. class ASTWithName extends AST {
  5790. constructor(span, sourceSpan, nameSpan) {
  5791. super(span, sourceSpan);
  5792. this.nameSpan = nameSpan;
  5793. }
  5794. }
  5795. class EmptyExpr extends AST {
  5796. visit(visitor, context = null) {
  5797. // do nothing
  5798. }
  5799. }
  5800. class ImplicitReceiver extends AST {
  5801. visit(visitor, context = null) {
  5802. return visitor.visitImplicitReceiver(this, context);
  5803. }
  5804. }
  5805. /**
  5806. * Receiver when something is accessed through `this` (e.g. `this.foo`). Note that this class
  5807. * inherits from `ImplicitReceiver`, because accessing something through `this` is treated the
  5808. * same as accessing it implicitly inside of an Angular template (e.g. `[attr.title]="this.title"`
  5809. * is the same as `[attr.title]="title"`.). Inheriting allows for the `this` accesses to be treated
  5810. * the same as implicit ones, except for a couple of exceptions like `$event` and `$any`.
  5811. * TODO: we should find a way for this class not to extend from `ImplicitReceiver` in the future.
  5812. */
  5813. class ThisReceiver extends ImplicitReceiver {
  5814. visit(visitor, context = null) {
  5815. return visitor.visitThisReceiver?.(this, context);
  5816. }
  5817. }
  5818. /**
  5819. * Multiple expressions separated by a semicolon.
  5820. */
  5821. class Chain extends AST {
  5822. constructor(span, sourceSpan, expressions) {
  5823. super(span, sourceSpan);
  5824. this.expressions = expressions;
  5825. }
  5826. visit(visitor, context = null) {
  5827. return visitor.visitChain(this, context);
  5828. }
  5829. }
  5830. class Conditional extends AST {
  5831. constructor(span, sourceSpan, condition, trueExp, falseExp) {
  5832. super(span, sourceSpan);
  5833. this.condition = condition;
  5834. this.trueExp = trueExp;
  5835. this.falseExp = falseExp;
  5836. }
  5837. visit(visitor, context = null) {
  5838. return visitor.visitConditional(this, context);
  5839. }
  5840. }
  5841. class PropertyRead extends ASTWithName {
  5842. constructor(span, sourceSpan, nameSpan, receiver, name) {
  5843. super(span, sourceSpan, nameSpan);
  5844. this.receiver = receiver;
  5845. this.name = name;
  5846. }
  5847. visit(visitor, context = null) {
  5848. return visitor.visitPropertyRead(this, context);
  5849. }
  5850. }
  5851. class PropertyWrite extends ASTWithName {
  5852. constructor(span, sourceSpan, nameSpan, receiver, name, value) {
  5853. super(span, sourceSpan, nameSpan);
  5854. this.receiver = receiver;
  5855. this.name = name;
  5856. this.value = value;
  5857. }
  5858. visit(visitor, context = null) {
  5859. return visitor.visitPropertyWrite(this, context);
  5860. }
  5861. }
  5862. class SafePropertyRead extends ASTWithName {
  5863. constructor(span, sourceSpan, nameSpan, receiver, name) {
  5864. super(span, sourceSpan, nameSpan);
  5865. this.receiver = receiver;
  5866. this.name = name;
  5867. }
  5868. visit(visitor, context = null) {
  5869. return visitor.visitSafePropertyRead(this, context);
  5870. }
  5871. }
  5872. class KeyedRead extends AST {
  5873. constructor(span, sourceSpan, receiver, key) {
  5874. super(span, sourceSpan);
  5875. this.receiver = receiver;
  5876. this.key = key;
  5877. }
  5878. visit(visitor, context = null) {
  5879. return visitor.visitKeyedRead(this, context);
  5880. }
  5881. }
  5882. class SafeKeyedRead extends AST {
  5883. constructor(span, sourceSpan, receiver, key) {
  5884. super(span, sourceSpan);
  5885. this.receiver = receiver;
  5886. this.key = key;
  5887. }
  5888. visit(visitor, context = null) {
  5889. return visitor.visitSafeKeyedRead(this, context);
  5890. }
  5891. }
  5892. class KeyedWrite extends AST {
  5893. constructor(span, sourceSpan, receiver, key, value) {
  5894. super(span, sourceSpan);
  5895. this.receiver = receiver;
  5896. this.key = key;
  5897. this.value = value;
  5898. }
  5899. visit(visitor, context = null) {
  5900. return visitor.visitKeyedWrite(this, context);
  5901. }
  5902. }
  5903. class BindingPipe extends ASTWithName {
  5904. constructor(span, sourceSpan, exp, name, args, nameSpan) {
  5905. super(span, sourceSpan, nameSpan);
  5906. this.exp = exp;
  5907. this.name = name;
  5908. this.args = args;
  5909. }
  5910. visit(visitor, context = null) {
  5911. return visitor.visitPipe(this, context);
  5912. }
  5913. }
  5914. class LiteralPrimitive extends AST {
  5915. constructor(span, sourceSpan, value) {
  5916. super(span, sourceSpan);
  5917. this.value = value;
  5918. }
  5919. visit(visitor, context = null) {
  5920. return visitor.visitLiteralPrimitive(this, context);
  5921. }
  5922. }
  5923. class LiteralArray extends AST {
  5924. constructor(span, sourceSpan, expressions) {
  5925. super(span, sourceSpan);
  5926. this.expressions = expressions;
  5927. }
  5928. visit(visitor, context = null) {
  5929. return visitor.visitLiteralArray(this, context);
  5930. }
  5931. }
  5932. class LiteralMap extends AST {
  5933. constructor(span, sourceSpan, keys, values) {
  5934. super(span, sourceSpan);
  5935. this.keys = keys;
  5936. this.values = values;
  5937. }
  5938. visit(visitor, context = null) {
  5939. return visitor.visitLiteralMap(this, context);
  5940. }
  5941. }
  5942. class Interpolation extends AST {
  5943. constructor(span, sourceSpan, strings, expressions) {
  5944. super(span, sourceSpan);
  5945. this.strings = strings;
  5946. this.expressions = expressions;
  5947. }
  5948. visit(visitor, context = null) {
  5949. return visitor.visitInterpolation(this, context);
  5950. }
  5951. }
  5952. class Binary extends AST {
  5953. constructor(span, sourceSpan, operation, left, right) {
  5954. super(span, sourceSpan);
  5955. this.operation = operation;
  5956. this.left = left;
  5957. this.right = right;
  5958. }
  5959. visit(visitor, context = null) {
  5960. return visitor.visitBinary(this, context);
  5961. }
  5962. }
  5963. /**
  5964. * For backwards compatibility reasons, `Unary` inherits from `Binary` and mimics the binary AST
  5965. * node that was originally used. This inheritance relation can be deleted in some future major,
  5966. * after consumers have been given a chance to fully support Unary.
  5967. */
  5968. class Unary extends Binary {
  5969. /**
  5970. * Creates a unary minus expression "-x", represented as `Binary` using "0 - x".
  5971. */
  5972. static createMinus(span, sourceSpan, expr) {
  5973. return new Unary(span, sourceSpan, '-', expr, '-', new LiteralPrimitive(span, sourceSpan, 0), expr);
  5974. }
  5975. /**
  5976. * Creates a unary plus expression "+x", represented as `Binary` using "x - 0".
  5977. */
  5978. static createPlus(span, sourceSpan, expr) {
  5979. return new Unary(span, sourceSpan, '+', expr, '-', expr, new LiteralPrimitive(span, sourceSpan, 0));
  5980. }
  5981. /**
  5982. * During the deprecation period this constructor is private, to avoid consumers from creating
  5983. * a `Unary` with the fallback properties for `Binary`.
  5984. */
  5985. constructor(span, sourceSpan, operator, expr, binaryOp, binaryLeft, binaryRight) {
  5986. super(span, sourceSpan, binaryOp, binaryLeft, binaryRight);
  5987. this.operator = operator;
  5988. this.expr = expr;
  5989. // Redeclare the properties that are inherited from `Binary` as `never`, as consumers should not
  5990. // depend on these fields when operating on `Unary`.
  5991. this.left = null;
  5992. this.right = null;
  5993. this.operation = null;
  5994. }
  5995. visit(visitor, context = null) {
  5996. if (visitor.visitUnary !== undefined) {
  5997. return visitor.visitUnary(this, context);
  5998. }
  5999. return visitor.visitBinary(this, context);
  6000. }
  6001. }
  6002. class PrefixNot extends AST {
  6003. constructor(span, sourceSpan, expression) {
  6004. super(span, sourceSpan);
  6005. this.expression = expression;
  6006. }
  6007. visit(visitor, context = null) {
  6008. return visitor.visitPrefixNot(this, context);
  6009. }
  6010. }
  6011. class NonNullAssert extends AST {
  6012. constructor(span, sourceSpan, expression) {
  6013. super(span, sourceSpan);
  6014. this.expression = expression;
  6015. }
  6016. visit(visitor, context = null) {
  6017. return visitor.visitNonNullAssert(this, context);
  6018. }
  6019. }
  6020. class Call extends AST {
  6021. constructor(span, sourceSpan, receiver, args, argumentSpan) {
  6022. super(span, sourceSpan);
  6023. this.receiver = receiver;
  6024. this.args = args;
  6025. this.argumentSpan = argumentSpan;
  6026. }
  6027. visit(visitor, context = null) {
  6028. return visitor.visitCall(this, context);
  6029. }
  6030. }
  6031. class SafeCall extends AST {
  6032. constructor(span, sourceSpan, receiver, args, argumentSpan) {
  6033. super(span, sourceSpan);
  6034. this.receiver = receiver;
  6035. this.args = args;
  6036. this.argumentSpan = argumentSpan;
  6037. }
  6038. visit(visitor, context = null) {
  6039. return visitor.visitSafeCall(this, context);
  6040. }
  6041. }
  6042. /**
  6043. * Records the absolute position of a text span in a source file, where `start` and `end` are the
  6044. * starting and ending byte offsets, respectively, of the text span in a source file.
  6045. */
  6046. class AbsoluteSourceSpan {
  6047. constructor(start, end) {
  6048. this.start = start;
  6049. this.end = end;
  6050. }
  6051. }
  6052. class ASTWithSource extends AST {
  6053. constructor(ast, source, location, absoluteOffset, errors) {
  6054. super(new ParseSpan(0, source === null ? 0 : source.length), new AbsoluteSourceSpan(absoluteOffset, source === null ? absoluteOffset : absoluteOffset + source.length));
  6055. this.ast = ast;
  6056. this.source = source;
  6057. this.location = location;
  6058. this.errors = errors;
  6059. }
  6060. visit(visitor, context = null) {
  6061. if (visitor.visitASTWithSource) {
  6062. return visitor.visitASTWithSource(this, context);
  6063. }
  6064. return this.ast.visit(visitor, context);
  6065. }
  6066. toString() {
  6067. return `${this.source} in ${this.location}`;
  6068. }
  6069. }
  6070. class VariableBinding {
  6071. /**
  6072. * @param sourceSpan entire span of the binding.
  6073. * @param key name of the LHS along with its span.
  6074. * @param value optional value for the RHS along with its span.
  6075. */
  6076. constructor(sourceSpan, key, value) {
  6077. this.sourceSpan = sourceSpan;
  6078. this.key = key;
  6079. this.value = value;
  6080. }
  6081. }
  6082. class ExpressionBinding {
  6083. /**
  6084. * @param sourceSpan entire span of the binding.
  6085. * @param key binding name, like ngForOf, ngForTrackBy, ngIf, along with its
  6086. * span. Note that the length of the span may not be the same as
  6087. * `key.source.length`. For example,
  6088. * 1. key.source = ngFor, key.span is for "ngFor"
  6089. * 2. key.source = ngForOf, key.span is for "of"
  6090. * 3. key.source = ngForTrackBy, key.span is for "trackBy"
  6091. * @param value optional expression for the RHS.
  6092. */
  6093. constructor(sourceSpan, key, value) {
  6094. this.sourceSpan = sourceSpan;
  6095. this.key = key;
  6096. this.value = value;
  6097. }
  6098. }
  6099. class RecursiveAstVisitor {
  6100. visit(ast, context) {
  6101. // The default implementation just visits every node.
  6102. // Classes that extend RecursiveAstVisitor should override this function
  6103. // to selectively visit the specified node.
  6104. ast.visit(this, context);
  6105. }
  6106. visitUnary(ast, context) {
  6107. this.visit(ast.expr, context);
  6108. }
  6109. visitBinary(ast, context) {
  6110. this.visit(ast.left, context);
  6111. this.visit(ast.right, context);
  6112. }
  6113. visitChain(ast, context) {
  6114. this.visitAll(ast.expressions, context);
  6115. }
  6116. visitConditional(ast, context) {
  6117. this.visit(ast.condition, context);
  6118. this.visit(ast.trueExp, context);
  6119. this.visit(ast.falseExp, context);
  6120. }
  6121. visitPipe(ast, context) {
  6122. this.visit(ast.exp, context);
  6123. this.visitAll(ast.args, context);
  6124. }
  6125. visitImplicitReceiver(ast, context) { }
  6126. visitThisReceiver(ast, context) { }
  6127. visitInterpolation(ast, context) {
  6128. this.visitAll(ast.expressions, context);
  6129. }
  6130. visitKeyedRead(ast, context) {
  6131. this.visit(ast.receiver, context);
  6132. this.visit(ast.key, context);
  6133. }
  6134. visitKeyedWrite(ast, context) {
  6135. this.visit(ast.receiver, context);
  6136. this.visit(ast.key, context);
  6137. this.visit(ast.value, context);
  6138. }
  6139. visitLiteralArray(ast, context) {
  6140. this.visitAll(ast.expressions, context);
  6141. }
  6142. visitLiteralMap(ast, context) {
  6143. this.visitAll(ast.values, context);
  6144. }
  6145. visitLiteralPrimitive(ast, context) { }
  6146. visitPrefixNot(ast, context) {
  6147. this.visit(ast.expression, context);
  6148. }
  6149. visitNonNullAssert(ast, context) {
  6150. this.visit(ast.expression, context);
  6151. }
  6152. visitPropertyRead(ast, context) {
  6153. this.visit(ast.receiver, context);
  6154. }
  6155. visitPropertyWrite(ast, context) {
  6156. this.visit(ast.receiver, context);
  6157. this.visit(ast.value, context);
  6158. }
  6159. visitSafePropertyRead(ast, context) {
  6160. this.visit(ast.receiver, context);
  6161. }
  6162. visitSafeKeyedRead(ast, context) {
  6163. this.visit(ast.receiver, context);
  6164. this.visit(ast.key, context);
  6165. }
  6166. visitCall(ast, context) {
  6167. this.visit(ast.receiver, context);
  6168. this.visitAll(ast.args, context);
  6169. }
  6170. visitSafeCall(ast, context) {
  6171. this.visit(ast.receiver, context);
  6172. this.visitAll(ast.args, context);
  6173. }
  6174. // This is not part of the AstVisitor interface, just a helper method
  6175. visitAll(asts, context) {
  6176. for (const ast of asts) {
  6177. this.visit(ast, context);
  6178. }
  6179. }
  6180. }
  6181. class AstTransformer {
  6182. visitImplicitReceiver(ast, context) {
  6183. return ast;
  6184. }
  6185. visitThisReceiver(ast, context) {
  6186. return ast;
  6187. }
  6188. visitInterpolation(ast, context) {
  6189. return new Interpolation(ast.span, ast.sourceSpan, ast.strings, this.visitAll(ast.expressions));
  6190. }
  6191. visitLiteralPrimitive(ast, context) {
  6192. return new LiteralPrimitive(ast.span, ast.sourceSpan, ast.value);
  6193. }
  6194. visitPropertyRead(ast, context) {
  6195. return new PropertyRead(ast.span, ast.sourceSpan, ast.nameSpan, ast.receiver.visit(this), ast.name);
  6196. }
  6197. visitPropertyWrite(ast, context) {
  6198. return new PropertyWrite(ast.span, ast.sourceSpan, ast.nameSpan, ast.receiver.visit(this), ast.name, ast.value.visit(this));
  6199. }
  6200. visitSafePropertyRead(ast, context) {
  6201. return new SafePropertyRead(ast.span, ast.sourceSpan, ast.nameSpan, ast.receiver.visit(this), ast.name);
  6202. }
  6203. visitLiteralArray(ast, context) {
  6204. return new LiteralArray(ast.span, ast.sourceSpan, this.visitAll(ast.expressions));
  6205. }
  6206. visitLiteralMap(ast, context) {
  6207. return new LiteralMap(ast.span, ast.sourceSpan, ast.keys, this.visitAll(ast.values));
  6208. }
  6209. visitUnary(ast, context) {
  6210. switch (ast.operator) {
  6211. case '+':
  6212. return Unary.createPlus(ast.span, ast.sourceSpan, ast.expr.visit(this));
  6213. case '-':
  6214. return Unary.createMinus(ast.span, ast.sourceSpan, ast.expr.visit(this));
  6215. default:
  6216. throw new Error(`Unknown unary operator ${ast.operator}`);
  6217. }
  6218. }
  6219. visitBinary(ast, context) {
  6220. return new Binary(ast.span, ast.sourceSpan, ast.operation, ast.left.visit(this), ast.right.visit(this));
  6221. }
  6222. visitPrefixNot(ast, context) {
  6223. return new PrefixNot(ast.span, ast.sourceSpan, ast.expression.visit(this));
  6224. }
  6225. visitNonNullAssert(ast, context) {
  6226. return new NonNullAssert(ast.span, ast.sourceSpan, ast.expression.visit(this));
  6227. }
  6228. visitConditional(ast, context) {
  6229. return new Conditional(ast.span, ast.sourceSpan, ast.condition.visit(this), ast.trueExp.visit(this), ast.falseExp.visit(this));
  6230. }
  6231. visitPipe(ast, context) {
  6232. return new BindingPipe(ast.span, ast.sourceSpan, ast.exp.visit(this), ast.name, this.visitAll(ast.args), ast.nameSpan);
  6233. }
  6234. visitKeyedRead(ast, context) {
  6235. return new KeyedRead(ast.span, ast.sourceSpan, ast.receiver.visit(this), ast.key.visit(this));
  6236. }
  6237. visitKeyedWrite(ast, context) {
  6238. return new KeyedWrite(ast.span, ast.sourceSpan, ast.receiver.visit(this), ast.key.visit(this), ast.value.visit(this));
  6239. }
  6240. visitCall(ast, context) {
  6241. return new Call(ast.span, ast.sourceSpan, ast.receiver.visit(this), this.visitAll(ast.args), ast.argumentSpan);
  6242. }
  6243. visitSafeCall(ast, context) {
  6244. return new SafeCall(ast.span, ast.sourceSpan, ast.receiver.visit(this), this.visitAll(ast.args), ast.argumentSpan);
  6245. }
  6246. visitAll(asts) {
  6247. const res = [];
  6248. for (let i = 0; i < asts.length; ++i) {
  6249. res[i] = asts[i].visit(this);
  6250. }
  6251. return res;
  6252. }
  6253. visitChain(ast, context) {
  6254. return new Chain(ast.span, ast.sourceSpan, this.visitAll(ast.expressions));
  6255. }
  6256. visitSafeKeyedRead(ast, context) {
  6257. return new SafeKeyedRead(ast.span, ast.sourceSpan, ast.receiver.visit(this), ast.key.visit(this));
  6258. }
  6259. }
  6260. // A transformer that only creates new nodes if the transformer makes a change or
  6261. // a change is made a child node.
  6262. class AstMemoryEfficientTransformer {
  6263. visitImplicitReceiver(ast, context) {
  6264. return ast;
  6265. }
  6266. visitThisReceiver(ast, context) {
  6267. return ast;
  6268. }
  6269. visitInterpolation(ast, context) {
  6270. const expressions = this.visitAll(ast.expressions);
  6271. if (expressions !== ast.expressions)
  6272. return new Interpolation(ast.span, ast.sourceSpan, ast.strings, expressions);
  6273. return ast;
  6274. }
  6275. visitLiteralPrimitive(ast, context) {
  6276. return ast;
  6277. }
  6278. visitPropertyRead(ast, context) {
  6279. const receiver = ast.receiver.visit(this);
  6280. if (receiver !== ast.receiver) {
  6281. return new PropertyRead(ast.span, ast.sourceSpan, ast.nameSpan, receiver, ast.name);
  6282. }
  6283. return ast;
  6284. }
  6285. visitPropertyWrite(ast, context) {
  6286. const receiver = ast.receiver.visit(this);
  6287. const value = ast.value.visit(this);
  6288. if (receiver !== ast.receiver || value !== ast.value) {
  6289. return new PropertyWrite(ast.span, ast.sourceSpan, ast.nameSpan, receiver, ast.name, value);
  6290. }
  6291. return ast;
  6292. }
  6293. visitSafePropertyRead(ast, context) {
  6294. const receiver = ast.receiver.visit(this);
  6295. if (receiver !== ast.receiver) {
  6296. return new SafePropertyRead(ast.span, ast.sourceSpan, ast.nameSpan, receiver, ast.name);
  6297. }
  6298. return ast;
  6299. }
  6300. visitLiteralArray(ast, context) {
  6301. const expressions = this.visitAll(ast.expressions);
  6302. if (expressions !== ast.expressions) {
  6303. return new LiteralArray(ast.span, ast.sourceSpan, expressions);
  6304. }
  6305. return ast;
  6306. }
  6307. visitLiteralMap(ast, context) {
  6308. const values = this.visitAll(ast.values);
  6309. if (values !== ast.values) {
  6310. return new LiteralMap(ast.span, ast.sourceSpan, ast.keys, values);
  6311. }
  6312. return ast;
  6313. }
  6314. visitUnary(ast, context) {
  6315. const expr = ast.expr.visit(this);
  6316. if (expr !== ast.expr) {
  6317. switch (ast.operator) {
  6318. case '+':
  6319. return Unary.createPlus(ast.span, ast.sourceSpan, expr);
  6320. case '-':
  6321. return Unary.createMinus(ast.span, ast.sourceSpan, expr);
  6322. default:
  6323. throw new Error(`Unknown unary operator ${ast.operator}`);
  6324. }
  6325. }
  6326. return ast;
  6327. }
  6328. visitBinary(ast, context) {
  6329. const left = ast.left.visit(this);
  6330. const right = ast.right.visit(this);
  6331. if (left !== ast.left || right !== ast.right) {
  6332. return new Binary(ast.span, ast.sourceSpan, ast.operation, left, right);
  6333. }
  6334. return ast;
  6335. }
  6336. visitPrefixNot(ast, context) {
  6337. const expression = ast.expression.visit(this);
  6338. if (expression !== ast.expression) {
  6339. return new PrefixNot(ast.span, ast.sourceSpan, expression);
  6340. }
  6341. return ast;
  6342. }
  6343. visitNonNullAssert(ast, context) {
  6344. const expression = ast.expression.visit(this);
  6345. if (expression !== ast.expression) {
  6346. return new NonNullAssert(ast.span, ast.sourceSpan, expression);
  6347. }
  6348. return ast;
  6349. }
  6350. visitConditional(ast, context) {
  6351. const condition = ast.condition.visit(this);
  6352. const trueExp = ast.trueExp.visit(this);
  6353. const falseExp = ast.falseExp.visit(this);
  6354. if (condition !== ast.condition || trueExp !== ast.trueExp || falseExp !== ast.falseExp) {
  6355. return new Conditional(ast.span, ast.sourceSpan, condition, trueExp, falseExp);
  6356. }
  6357. return ast;
  6358. }
  6359. visitPipe(ast, context) {
  6360. const exp = ast.exp.visit(this);
  6361. const args = this.visitAll(ast.args);
  6362. if (exp !== ast.exp || args !== ast.args) {
  6363. return new BindingPipe(ast.span, ast.sourceSpan, exp, ast.name, args, ast.nameSpan);
  6364. }
  6365. return ast;
  6366. }
  6367. visitKeyedRead(ast, context) {
  6368. const obj = ast.receiver.visit(this);
  6369. const key = ast.key.visit(this);
  6370. if (obj !== ast.receiver || key !== ast.key) {
  6371. return new KeyedRead(ast.span, ast.sourceSpan, obj, key);
  6372. }
  6373. return ast;
  6374. }
  6375. visitKeyedWrite(ast, context) {
  6376. const obj = ast.receiver.visit(this);
  6377. const key = ast.key.visit(this);
  6378. const value = ast.value.visit(this);
  6379. if (obj !== ast.receiver || key !== ast.key || value !== ast.value) {
  6380. return new KeyedWrite(ast.span, ast.sourceSpan, obj, key, value);
  6381. }
  6382. return ast;
  6383. }
  6384. visitAll(asts) {
  6385. const res = [];
  6386. let modified = false;
  6387. for (let i = 0; i < asts.length; ++i) {
  6388. const original = asts[i];
  6389. const value = original.visit(this);
  6390. res[i] = value;
  6391. modified = modified || value !== original;
  6392. }
  6393. return modified ? res : asts;
  6394. }
  6395. visitChain(ast, context) {
  6396. const expressions = this.visitAll(ast.expressions);
  6397. if (expressions !== ast.expressions) {
  6398. return new Chain(ast.span, ast.sourceSpan, expressions);
  6399. }
  6400. return ast;
  6401. }
  6402. visitCall(ast, context) {
  6403. const receiver = ast.receiver.visit(this);
  6404. const args = this.visitAll(ast.args);
  6405. if (receiver !== ast.receiver || args !== ast.args) {
  6406. return new Call(ast.span, ast.sourceSpan, receiver, args, ast.argumentSpan);
  6407. }
  6408. return ast;
  6409. }
  6410. visitSafeCall(ast, context) {
  6411. const receiver = ast.receiver.visit(this);
  6412. const args = this.visitAll(ast.args);
  6413. if (receiver !== ast.receiver || args !== ast.args) {
  6414. return new SafeCall(ast.span, ast.sourceSpan, receiver, args, ast.argumentSpan);
  6415. }
  6416. return ast;
  6417. }
  6418. visitSafeKeyedRead(ast, context) {
  6419. const obj = ast.receiver.visit(this);
  6420. const key = ast.key.visit(this);
  6421. if (obj !== ast.receiver || key !== ast.key) {
  6422. return new SafeKeyedRead(ast.span, ast.sourceSpan, obj, key);
  6423. }
  6424. return ast;
  6425. }
  6426. }
  6427. // Bindings
  6428. class ParsedProperty {
  6429. constructor(name, expression, type, sourceSpan, keySpan, valueSpan) {
  6430. this.name = name;
  6431. this.expression = expression;
  6432. this.type = type;
  6433. this.sourceSpan = sourceSpan;
  6434. this.keySpan = keySpan;
  6435. this.valueSpan = valueSpan;
  6436. this.isLiteral = this.type === ParsedPropertyType.LITERAL_ATTR;
  6437. this.isAnimation = this.type === ParsedPropertyType.ANIMATION;
  6438. }
  6439. }
  6440. var ParsedPropertyType;
  6441. (function (ParsedPropertyType) {
  6442. ParsedPropertyType[ParsedPropertyType["DEFAULT"] = 0] = "DEFAULT";
  6443. ParsedPropertyType[ParsedPropertyType["LITERAL_ATTR"] = 1] = "LITERAL_ATTR";
  6444. ParsedPropertyType[ParsedPropertyType["ANIMATION"] = 2] = "ANIMATION";
  6445. })(ParsedPropertyType || (ParsedPropertyType = {}));
  6446. class ParsedEvent {
  6447. // Regular events have a target
  6448. // Animation events have a phase
  6449. constructor(name, targetOrPhase, type, handler, sourceSpan, handlerSpan, keySpan) {
  6450. this.name = name;
  6451. this.targetOrPhase = targetOrPhase;
  6452. this.type = type;
  6453. this.handler = handler;
  6454. this.sourceSpan = sourceSpan;
  6455. this.handlerSpan = handlerSpan;
  6456. this.keySpan = keySpan;
  6457. }
  6458. }
  6459. /**
  6460. * ParsedVariable represents a variable declaration in a microsyntax expression.
  6461. */
  6462. class ParsedVariable {
  6463. constructor(name, value, sourceSpan, keySpan, valueSpan) {
  6464. this.name = name;
  6465. this.value = value;
  6466. this.sourceSpan = sourceSpan;
  6467. this.keySpan = keySpan;
  6468. this.valueSpan = valueSpan;
  6469. }
  6470. }
  6471. class BoundElementProperty {
  6472. constructor(name, type, securityContext, value, unit, sourceSpan, keySpan, valueSpan) {
  6473. this.name = name;
  6474. this.type = type;
  6475. this.securityContext = securityContext;
  6476. this.value = value;
  6477. this.unit = unit;
  6478. this.sourceSpan = sourceSpan;
  6479. this.keySpan = keySpan;
  6480. this.valueSpan = valueSpan;
  6481. }
  6482. }
  6483. class EventHandlerVars {
  6484. static { this.event = variable('$event'); }
  6485. }
  6486. /**
  6487. * Converts the given expression AST into an executable output AST, assuming the expression is
  6488. * used in an action binding (e.g. an event handler).
  6489. */
  6490. function convertActionBinding(localResolver, implicitReceiver, action, bindingId, baseSourceSpan, implicitReceiverAccesses, globals) {
  6491. if (!localResolver) {
  6492. localResolver = new DefaultLocalResolver(globals);
  6493. }
  6494. const actionWithoutBuiltins = convertPropertyBindingBuiltins({
  6495. createLiteralArrayConverter: (argCount) => {
  6496. // Note: no caching for literal arrays in actions.
  6497. return (args) => literalArr(args);
  6498. },
  6499. createLiteralMapConverter: (keys) => {
  6500. // Note: no caching for literal maps in actions.
  6501. return (values) => {
  6502. const entries = keys.map((k, i) => ({
  6503. key: k.key,
  6504. value: values[i],
  6505. quoted: k.quoted,
  6506. }));
  6507. return literalMap(entries);
  6508. };
  6509. },
  6510. createPipeConverter: (name) => {
  6511. throw new Error(`Illegal State: Actions are not allowed to contain pipes. Pipe: ${name}`);
  6512. }
  6513. }, action);
  6514. const visitor = new _AstToIrVisitor(localResolver, implicitReceiver, bindingId, /* supportsInterpolation */ false, baseSourceSpan, implicitReceiverAccesses);
  6515. const actionStmts = [];
  6516. flattenStatements(actionWithoutBuiltins.visit(visitor, _Mode.Statement), actionStmts);
  6517. prependTemporaryDecls(visitor.temporaryCount, bindingId, actionStmts);
  6518. if (visitor.usesImplicitReceiver) {
  6519. localResolver.notifyImplicitReceiverUse();
  6520. }
  6521. const lastIndex = actionStmts.length - 1;
  6522. if (lastIndex >= 0) {
  6523. const lastStatement = actionStmts[lastIndex];
  6524. // Ensure that the value of the last expression statement is returned
  6525. if (lastStatement instanceof ExpressionStatement) {
  6526. actionStmts[lastIndex] = new ReturnStatement(lastStatement.expr);
  6527. }
  6528. }
  6529. return actionStmts;
  6530. }
  6531. function convertPropertyBindingBuiltins(converterFactory, ast) {
  6532. return convertBuiltins(converterFactory, ast);
  6533. }
  6534. class ConvertPropertyBindingResult {
  6535. constructor(stmts, currValExpr) {
  6536. this.stmts = stmts;
  6537. this.currValExpr = currValExpr;
  6538. }
  6539. }
  6540. /**
  6541. * Converts the given expression AST into an executable output AST, assuming the expression
  6542. * is used in property binding. The expression has to be preprocessed via
  6543. * `convertPropertyBindingBuiltins`.
  6544. */
  6545. function convertPropertyBinding(localResolver, implicitReceiver, expressionWithoutBuiltins, bindingId) {
  6546. if (!localResolver) {
  6547. localResolver = new DefaultLocalResolver();
  6548. }
  6549. const visitor = new _AstToIrVisitor(localResolver, implicitReceiver, bindingId, /* supportsInterpolation */ false);
  6550. const outputExpr = expressionWithoutBuiltins.visit(visitor, _Mode.Expression);
  6551. const stmts = getStatementsFromVisitor(visitor, bindingId);
  6552. if (visitor.usesImplicitReceiver) {
  6553. localResolver.notifyImplicitReceiverUse();
  6554. }
  6555. return new ConvertPropertyBindingResult(stmts, outputExpr);
  6556. }
  6557. /**
  6558. * Given some expression, such as a binding or interpolation expression, and a context expression to
  6559. * look values up on, visit each facet of the given expression resolving values from the context
  6560. * expression such that a list of arguments can be derived from the found values that can be used as
  6561. * arguments to an external update instruction.
  6562. *
  6563. * @param localResolver The resolver to use to look up expressions by name appropriately
  6564. * @param contextVariableExpression The expression representing the context variable used to create
  6565. * the final argument expressions
  6566. * @param expressionWithArgumentsToExtract The expression to visit to figure out what values need to
  6567. * be resolved and what arguments list to build.
  6568. * @param bindingId A name prefix used to create temporary variable names if they're needed for the
  6569. * arguments generated
  6570. * @returns An array of expressions that can be passed as arguments to instruction expressions like
  6571. * `o.importExpr(R3.propertyInterpolate).callFn(result)`
  6572. */
  6573. function convertUpdateArguments(localResolver, contextVariableExpression, expressionWithArgumentsToExtract, bindingId) {
  6574. const visitor = new _AstToIrVisitor(localResolver, contextVariableExpression, bindingId, /* supportsInterpolation */ true);
  6575. const outputExpr = visitor.visitInterpolation(expressionWithArgumentsToExtract, _Mode.Expression);
  6576. if (visitor.usesImplicitReceiver) {
  6577. localResolver.notifyImplicitReceiverUse();
  6578. }
  6579. const stmts = getStatementsFromVisitor(visitor, bindingId);
  6580. const args = outputExpr.args;
  6581. return { stmts, args };
  6582. }
  6583. function getStatementsFromVisitor(visitor, bindingId) {
  6584. const stmts = [];
  6585. for (let i = 0; i < visitor.temporaryCount; i++) {
  6586. stmts.push(temporaryDeclaration(bindingId, i));
  6587. }
  6588. return stmts;
  6589. }
  6590. function convertBuiltins(converterFactory, ast) {
  6591. const visitor = new _BuiltinAstConverter(converterFactory);
  6592. return ast.visit(visitor);
  6593. }
  6594. function temporaryName(bindingId, temporaryNumber) {
  6595. return `tmp_${bindingId}_${temporaryNumber}`;
  6596. }
  6597. function temporaryDeclaration(bindingId, temporaryNumber) {
  6598. return new DeclareVarStmt(temporaryName(bindingId, temporaryNumber));
  6599. }
  6600. function prependTemporaryDecls(temporaryCount, bindingId, statements) {
  6601. for (let i = temporaryCount - 1; i >= 0; i--) {
  6602. statements.unshift(temporaryDeclaration(bindingId, i));
  6603. }
  6604. }
  6605. var _Mode;
  6606. (function (_Mode) {
  6607. _Mode[_Mode["Statement"] = 0] = "Statement";
  6608. _Mode[_Mode["Expression"] = 1] = "Expression";
  6609. })(_Mode || (_Mode = {}));
  6610. function ensureStatementMode(mode, ast) {
  6611. if (mode !== _Mode.Statement) {
  6612. throw new Error(`Expected a statement, but saw ${ast}`);
  6613. }
  6614. }
  6615. function ensureExpressionMode(mode, ast) {
  6616. if (mode !== _Mode.Expression) {
  6617. throw new Error(`Expected an expression, but saw ${ast}`);
  6618. }
  6619. }
  6620. function convertToStatementIfNeeded(mode, expr) {
  6621. if (mode === _Mode.Statement) {
  6622. return expr.toStmt();
  6623. }
  6624. else {
  6625. return expr;
  6626. }
  6627. }
  6628. class _BuiltinAstConverter extends AstTransformer {
  6629. constructor(_converterFactory) {
  6630. super();
  6631. this._converterFactory = _converterFactory;
  6632. }
  6633. visitPipe(ast, context) {
  6634. const args = [ast.exp, ...ast.args].map(ast => ast.visit(this, context));
  6635. return new BuiltinFunctionCall(ast.span, ast.sourceSpan, args, this._converterFactory.createPipeConverter(ast.name, args.length));
  6636. }
  6637. visitLiteralArray(ast, context) {
  6638. const args = ast.expressions.map(ast => ast.visit(this, context));
  6639. return new BuiltinFunctionCall(ast.span, ast.sourceSpan, args, this._converterFactory.createLiteralArrayConverter(ast.expressions.length));
  6640. }
  6641. visitLiteralMap(ast, context) {
  6642. const args = ast.values.map(ast => ast.visit(this, context));
  6643. return new BuiltinFunctionCall(ast.span, ast.sourceSpan, args, this._converterFactory.createLiteralMapConverter(ast.keys));
  6644. }
  6645. }
  6646. class _AstToIrVisitor {
  6647. constructor(_localResolver, _implicitReceiver, bindingId, supportsInterpolation, baseSourceSpan, implicitReceiverAccesses) {
  6648. this._localResolver = _localResolver;
  6649. this._implicitReceiver = _implicitReceiver;
  6650. this.bindingId = bindingId;
  6651. this.supportsInterpolation = supportsInterpolation;
  6652. this.baseSourceSpan = baseSourceSpan;
  6653. this.implicitReceiverAccesses = implicitReceiverAccesses;
  6654. this._nodeMap = new Map();
  6655. this._resultMap = new Map();
  6656. this._currentTemporary = 0;
  6657. this.temporaryCount = 0;
  6658. this.usesImplicitReceiver = false;
  6659. }
  6660. visitUnary(ast, mode) {
  6661. let op;
  6662. switch (ast.operator) {
  6663. case '+':
  6664. op = UnaryOperator.Plus;
  6665. break;
  6666. case '-':
  6667. op = UnaryOperator.Minus;
  6668. break;
  6669. default:
  6670. throw new Error(`Unsupported operator ${ast.operator}`);
  6671. }
  6672. return convertToStatementIfNeeded(mode, new UnaryOperatorExpr(op, this._visit(ast.expr, _Mode.Expression), undefined, this.convertSourceSpan(ast.span)));
  6673. }
  6674. visitBinary(ast, mode) {
  6675. let op;
  6676. switch (ast.operation) {
  6677. case '+':
  6678. op = BinaryOperator.Plus;
  6679. break;
  6680. case '-':
  6681. op = BinaryOperator.Minus;
  6682. break;
  6683. case '*':
  6684. op = BinaryOperator.Multiply;
  6685. break;
  6686. case '/':
  6687. op = BinaryOperator.Divide;
  6688. break;
  6689. case '%':
  6690. op = BinaryOperator.Modulo;
  6691. break;
  6692. case '&&':
  6693. op = BinaryOperator.And;
  6694. break;
  6695. case '||':
  6696. op = BinaryOperator.Or;
  6697. break;
  6698. case '==':
  6699. op = BinaryOperator.Equals;
  6700. break;
  6701. case '!=':
  6702. op = BinaryOperator.NotEquals;
  6703. break;
  6704. case '===':
  6705. op = BinaryOperator.Identical;
  6706. break;
  6707. case '!==':
  6708. op = BinaryOperator.NotIdentical;
  6709. break;
  6710. case '<':
  6711. op = BinaryOperator.Lower;
  6712. break;
  6713. case '>':
  6714. op = BinaryOperator.Bigger;
  6715. break;
  6716. case '<=':
  6717. op = BinaryOperator.LowerEquals;
  6718. break;
  6719. case '>=':
  6720. op = BinaryOperator.BiggerEquals;
  6721. break;
  6722. case '??':
  6723. return this.convertNullishCoalesce(ast, mode);
  6724. default:
  6725. throw new Error(`Unsupported operation ${ast.operation}`);
  6726. }
  6727. return convertToStatementIfNeeded(mode, new BinaryOperatorExpr(op, this._visit(ast.left, _Mode.Expression), this._visit(ast.right, _Mode.Expression), undefined, this.convertSourceSpan(ast.span)));
  6728. }
  6729. visitChain(ast, mode) {
  6730. ensureStatementMode(mode, ast);
  6731. return this.visitAll(ast.expressions, mode);
  6732. }
  6733. visitConditional(ast, mode) {
  6734. const value = this._visit(ast.condition, _Mode.Expression);
  6735. return convertToStatementIfNeeded(mode, value.conditional(this._visit(ast.trueExp, _Mode.Expression), this._visit(ast.falseExp, _Mode.Expression), this.convertSourceSpan(ast.span)));
  6736. }
  6737. visitPipe(ast, mode) {
  6738. throw new Error(`Illegal state: Pipes should have been converted into functions. Pipe: ${ast.name}`);
  6739. }
  6740. visitImplicitReceiver(ast, mode) {
  6741. ensureExpressionMode(mode, ast);
  6742. this.usesImplicitReceiver = true;
  6743. return this._implicitReceiver;
  6744. }
  6745. visitThisReceiver(ast, mode) {
  6746. return this.visitImplicitReceiver(ast, mode);
  6747. }
  6748. visitInterpolation(ast, mode) {
  6749. if (!this.supportsInterpolation) {
  6750. throw new Error('Unexpected interpolation');
  6751. }
  6752. ensureExpressionMode(mode, ast);
  6753. let args = [];
  6754. for (let i = 0; i < ast.strings.length - 1; i++) {
  6755. args.push(literal(ast.strings[i]));
  6756. args.push(this._visit(ast.expressions[i], _Mode.Expression));
  6757. }
  6758. args.push(literal(ast.strings[ast.strings.length - 1]));
  6759. // If we're dealing with an interpolation of 1 value with an empty prefix and suffix, reduce the
  6760. // args returned to just the value, because we're going to pass it to a special instruction.
  6761. const strings = ast.strings;
  6762. if (strings.length === 2 && strings[0] === '' && strings[1] === '') {
  6763. // Single argument interpolate instructions.
  6764. args = [args[1]];
  6765. }
  6766. else if (ast.expressions.length >= 9) {
  6767. // 9 or more arguments must be passed to the `interpolateV`-style instructions, which accept
  6768. // an array of arguments
  6769. args = [literalArr(args)];
  6770. }
  6771. return new InterpolationExpression(args);
  6772. }
  6773. visitKeyedRead(ast, mode) {
  6774. const leftMostSafe = this.leftMostSafeNode(ast);
  6775. if (leftMostSafe) {
  6776. return this.convertSafeAccess(ast, leftMostSafe, mode);
  6777. }
  6778. else {
  6779. return convertToStatementIfNeeded(mode, this._visit(ast.receiver, _Mode.Expression).key(this._visit(ast.key, _Mode.Expression)));
  6780. }
  6781. }
  6782. visitKeyedWrite(ast, mode) {
  6783. const obj = this._visit(ast.receiver, _Mode.Expression);
  6784. const key = this._visit(ast.key, _Mode.Expression);
  6785. const value = this._visit(ast.value, _Mode.Expression);
  6786. if (obj === this._implicitReceiver) {
  6787. this._localResolver.maybeRestoreView();
  6788. }
  6789. return convertToStatementIfNeeded(mode, obj.key(key).set(value));
  6790. }
  6791. visitLiteralArray(ast, mode) {
  6792. throw new Error(`Illegal State: literal arrays should have been converted into functions`);
  6793. }
  6794. visitLiteralMap(ast, mode) {
  6795. throw new Error(`Illegal State: literal maps should have been converted into functions`);
  6796. }
  6797. visitLiteralPrimitive(ast, mode) {
  6798. // For literal values of null, undefined, true, or false allow type interference
  6799. // to infer the type.
  6800. const type = ast.value === null || ast.value === undefined || ast.value === true || ast.value === true ?
  6801. INFERRED_TYPE :
  6802. undefined;
  6803. return convertToStatementIfNeeded(mode, literal(ast.value, type, this.convertSourceSpan(ast.span)));
  6804. }
  6805. _getLocal(name, receiver) {
  6806. if (this._localResolver.globals?.has(name) && receiver instanceof ThisReceiver) {
  6807. return null;
  6808. }
  6809. return this._localResolver.getLocal(name);
  6810. }
  6811. visitPrefixNot(ast, mode) {
  6812. return convertToStatementIfNeeded(mode, not(this._visit(ast.expression, _Mode.Expression)));
  6813. }
  6814. visitNonNullAssert(ast, mode) {
  6815. return convertToStatementIfNeeded(mode, this._visit(ast.expression, _Mode.Expression));
  6816. }
  6817. visitPropertyRead(ast, mode) {
  6818. const leftMostSafe = this.leftMostSafeNode(ast);
  6819. if (leftMostSafe) {
  6820. return this.convertSafeAccess(ast, leftMostSafe, mode);
  6821. }
  6822. else {
  6823. let result = null;
  6824. const prevUsesImplicitReceiver = this.usesImplicitReceiver;
  6825. const receiver = this._visit(ast.receiver, _Mode.Expression);
  6826. if (receiver === this._implicitReceiver) {
  6827. result = this._getLocal(ast.name, ast.receiver);
  6828. if (result) {
  6829. // Restore the previous "usesImplicitReceiver" state since the implicit
  6830. // receiver has been replaced with a resolved local expression.
  6831. this.usesImplicitReceiver = prevUsesImplicitReceiver;
  6832. this.addImplicitReceiverAccess(ast.name);
  6833. }
  6834. }
  6835. if (result == null) {
  6836. result = receiver.prop(ast.name, this.convertSourceSpan(ast.span));
  6837. }
  6838. return convertToStatementIfNeeded(mode, result);
  6839. }
  6840. }
  6841. visitPropertyWrite(ast, mode) {
  6842. const receiver = this._visit(ast.receiver, _Mode.Expression);
  6843. const prevUsesImplicitReceiver = this.usesImplicitReceiver;
  6844. let varExpr = null;
  6845. if (receiver === this._implicitReceiver) {
  6846. const localExpr = this._getLocal(ast.name, ast.receiver);
  6847. if (localExpr) {
  6848. if (localExpr instanceof ReadPropExpr) {
  6849. // If the local variable is a property read expression, it's a reference
  6850. // to a 'context.property' value and will be used as the target of the
  6851. // write expression.
  6852. varExpr = localExpr;
  6853. // Restore the previous "usesImplicitReceiver" state since the implicit
  6854. // receiver has been replaced with a resolved local expression.
  6855. this.usesImplicitReceiver = prevUsesImplicitReceiver;
  6856. this.addImplicitReceiverAccess(ast.name);
  6857. }
  6858. else {
  6859. // Otherwise it's an error.
  6860. const receiver = ast.name;
  6861. const value = (ast.value instanceof PropertyRead) ? ast.value.name : undefined;
  6862. throw new Error(`Cannot assign value "${value}" to template variable "${receiver}". Template variables are read-only.`);
  6863. }
  6864. }
  6865. }
  6866. // If no local expression could be produced, use the original receiver's
  6867. // property as the target.
  6868. if (varExpr === null) {
  6869. varExpr = receiver.prop(ast.name, this.convertSourceSpan(ast.span));
  6870. }
  6871. return convertToStatementIfNeeded(mode, varExpr.set(this._visit(ast.value, _Mode.Expression)));
  6872. }
  6873. visitSafePropertyRead(ast, mode) {
  6874. return this.convertSafeAccess(ast, this.leftMostSafeNode(ast), mode);
  6875. }
  6876. visitSafeKeyedRead(ast, mode) {
  6877. return this.convertSafeAccess(ast, this.leftMostSafeNode(ast), mode);
  6878. }
  6879. visitAll(asts, mode) {
  6880. return asts.map(ast => this._visit(ast, mode));
  6881. }
  6882. visitCall(ast, mode) {
  6883. const leftMostSafe = this.leftMostSafeNode(ast);
  6884. if (leftMostSafe) {
  6885. return this.convertSafeAccess(ast, leftMostSafe, mode);
  6886. }
  6887. const convertedArgs = this.visitAll(ast.args, _Mode.Expression);
  6888. if (ast instanceof BuiltinFunctionCall) {
  6889. return convertToStatementIfNeeded(mode, ast.converter(convertedArgs));
  6890. }
  6891. const receiver = ast.receiver;
  6892. if (receiver instanceof PropertyRead &&
  6893. receiver.receiver instanceof ImplicitReceiver &&
  6894. !(receiver.receiver instanceof ThisReceiver) && receiver.name === '$any') {
  6895. if (convertedArgs.length !== 1) {
  6896. throw new Error(`Invalid call to $any, expected 1 argument but received ${convertedArgs.length || 'none'}`);
  6897. }
  6898. return convertToStatementIfNeeded(mode, convertedArgs[0]);
  6899. }
  6900. const call = this._visit(receiver, _Mode.Expression)
  6901. .callFn(convertedArgs, this.convertSourceSpan(ast.span));
  6902. return convertToStatementIfNeeded(mode, call);
  6903. }
  6904. visitSafeCall(ast, mode) {
  6905. return this.convertSafeAccess(ast, this.leftMostSafeNode(ast), mode);
  6906. }
  6907. _visit(ast, mode) {
  6908. const result = this._resultMap.get(ast);
  6909. if (result)
  6910. return result;
  6911. return (this._nodeMap.get(ast) || ast).visit(this, mode);
  6912. }
  6913. convertSafeAccess(ast, leftMostSafe, mode) {
  6914. // If the expression contains a safe access node on the left it needs to be converted to
  6915. // an expression that guards the access to the member by checking the receiver for blank. As
  6916. // execution proceeds from left to right, the left most part of the expression must be guarded
  6917. // first but, because member access is left associative, the right side of the expression is at
  6918. // the top of the AST. The desired result requires lifting a copy of the left part of the
  6919. // expression up to test it for blank before generating the unguarded version.
  6920. // Consider, for example the following expression: a?.b.c?.d.e
  6921. // This results in the ast:
  6922. // .
  6923. // / \
  6924. // ?. e
  6925. // / \
  6926. // . d
  6927. // / \
  6928. // ?. c
  6929. // / \
  6930. // a b
  6931. // The following tree should be generated:
  6932. //
  6933. // /---- ? ----\
  6934. // / | \
  6935. // a /--- ? ---\ null
  6936. // / | \
  6937. // . . null
  6938. // / \ / \
  6939. // . c . e
  6940. // / \ / \
  6941. // a b . d
  6942. // / \
  6943. // . c
  6944. // / \
  6945. // a b
  6946. //
  6947. // Notice that the first guard condition is the left hand of the left most safe access node
  6948. // which comes in as leftMostSafe to this routine.
  6949. let guardedExpression = this._visit(leftMostSafe.receiver, _Mode.Expression);
  6950. let temporary = undefined;
  6951. if (this.needsTemporaryInSafeAccess(leftMostSafe.receiver)) {
  6952. // If the expression has method calls or pipes then we need to save the result into a
  6953. // temporary variable to avoid calling stateful or impure code more than once.
  6954. temporary = this.allocateTemporary();
  6955. // Preserve the result in the temporary variable
  6956. guardedExpression = temporary.set(guardedExpression);
  6957. // Ensure all further references to the guarded expression refer to the temporary instead.
  6958. this._resultMap.set(leftMostSafe.receiver, temporary);
  6959. }
  6960. const condition = guardedExpression.isBlank();
  6961. // Convert the ast to an unguarded access to the receiver's member. The map will substitute
  6962. // leftMostNode with its unguarded version in the call to `this.visit()`.
  6963. if (leftMostSafe instanceof SafeCall) {
  6964. this._nodeMap.set(leftMostSafe, new Call(leftMostSafe.span, leftMostSafe.sourceSpan, leftMostSafe.receiver, leftMostSafe.args, leftMostSafe.argumentSpan));
  6965. }
  6966. else if (leftMostSafe instanceof SafeKeyedRead) {
  6967. this._nodeMap.set(leftMostSafe, new KeyedRead(leftMostSafe.span, leftMostSafe.sourceSpan, leftMostSafe.receiver, leftMostSafe.key));
  6968. }
  6969. else {
  6970. this._nodeMap.set(leftMostSafe, new PropertyRead(leftMostSafe.span, leftMostSafe.sourceSpan, leftMostSafe.nameSpan, leftMostSafe.receiver, leftMostSafe.name));
  6971. }
  6972. // Recursively convert the node now without the guarded member access.
  6973. const access = this._visit(ast, _Mode.Expression);
  6974. // Remove the mapping. This is not strictly required as the converter only traverses each node
  6975. // once but is safer if the conversion is changed to traverse the nodes more than once.
  6976. this._nodeMap.delete(leftMostSafe);
  6977. // If we allocated a temporary, release it.
  6978. if (temporary) {
  6979. this.releaseTemporary(temporary);
  6980. }
  6981. // Produce the conditional
  6982. return convertToStatementIfNeeded(mode, condition.conditional(NULL_EXPR, access));
  6983. }
  6984. convertNullishCoalesce(ast, mode) {
  6985. const left = this._visit(ast.left, _Mode.Expression);
  6986. const right = this._visit(ast.right, _Mode.Expression);
  6987. const temporary = this.allocateTemporary();
  6988. this.releaseTemporary(temporary);
  6989. // Generate the following expression. It is identical to how TS
  6990. // transpiles binary expressions with a nullish coalescing operator.
  6991. // let temp;
  6992. // (temp = a) !== null && temp !== undefined ? temp : b;
  6993. return convertToStatementIfNeeded(mode, temporary.set(left)
  6994. .notIdentical(NULL_EXPR)
  6995. .and(temporary.notIdentical(literal(undefined)))
  6996. .conditional(temporary, right));
  6997. }
  6998. // Given an expression of the form a?.b.c?.d.e then the left most safe node is
  6999. // the (a?.b). The . and ?. are left associative thus can be rewritten as:
  7000. // ((((a?.c).b).c)?.d).e. This returns the most deeply nested safe read or
  7001. // safe method call as this needs to be transformed initially to:
  7002. // a == null ? null : a.c.b.c?.d.e
  7003. // then to:
  7004. // a == null ? null : a.b.c == null ? null : a.b.c.d.e
  7005. leftMostSafeNode(ast) {
  7006. const visit = (visitor, ast) => {
  7007. return (this._nodeMap.get(ast) || ast).visit(visitor);
  7008. };
  7009. return ast.visit({
  7010. visitUnary(ast) {
  7011. return null;
  7012. },
  7013. visitBinary(ast) {
  7014. return null;
  7015. },
  7016. visitChain(ast) {
  7017. return null;
  7018. },
  7019. visitConditional(ast) {
  7020. return null;
  7021. },
  7022. visitCall(ast) {
  7023. return visit(this, ast.receiver);
  7024. },
  7025. visitSafeCall(ast) {
  7026. return visit(this, ast.receiver) || ast;
  7027. },
  7028. visitImplicitReceiver(ast) {
  7029. return null;
  7030. },
  7031. visitThisReceiver(ast) {
  7032. return null;
  7033. },
  7034. visitInterpolation(ast) {
  7035. return null;
  7036. },
  7037. visitKeyedRead(ast) {
  7038. return visit(this, ast.receiver);
  7039. },
  7040. visitKeyedWrite(ast) {
  7041. return null;
  7042. },
  7043. visitLiteralArray(ast) {
  7044. return null;
  7045. },
  7046. visitLiteralMap(ast) {
  7047. return null;
  7048. },
  7049. visitLiteralPrimitive(ast) {
  7050. return null;
  7051. },
  7052. visitPipe(ast) {
  7053. return null;
  7054. },
  7055. visitPrefixNot(ast) {
  7056. return null;
  7057. },
  7058. visitNonNullAssert(ast) {
  7059. return visit(this, ast.expression);
  7060. },
  7061. visitPropertyRead(ast) {
  7062. return visit(this, ast.receiver);
  7063. },
  7064. visitPropertyWrite(ast) {
  7065. return null;
  7066. },
  7067. visitSafePropertyRead(ast) {
  7068. return visit(this, ast.receiver) || ast;
  7069. },
  7070. visitSafeKeyedRead(ast) {
  7071. return visit(this, ast.receiver) || ast;
  7072. }
  7073. });
  7074. }
  7075. // Returns true of the AST includes a method or a pipe indicating that, if the
  7076. // expression is used as the target of a safe property or method access then
  7077. // the expression should be stored into a temporary variable.
  7078. needsTemporaryInSafeAccess(ast) {
  7079. const visit = (visitor, ast) => {
  7080. return ast && (this._nodeMap.get(ast) || ast).visit(visitor);
  7081. };
  7082. const visitSome = (visitor, ast) => {
  7083. return ast.some(ast => visit(visitor, ast));
  7084. };
  7085. return ast.visit({
  7086. visitUnary(ast) {
  7087. return visit(this, ast.expr);
  7088. },
  7089. visitBinary(ast) {
  7090. return visit(this, ast.left) || visit(this, ast.right);
  7091. },
  7092. visitChain(ast) {
  7093. return false;
  7094. },
  7095. visitConditional(ast) {
  7096. return visit(this, ast.condition) || visit(this, ast.trueExp) || visit(this, ast.falseExp);
  7097. },
  7098. visitCall(ast) {
  7099. return true;
  7100. },
  7101. visitSafeCall(ast) {
  7102. return true;
  7103. },
  7104. visitImplicitReceiver(ast) {
  7105. return false;
  7106. },
  7107. visitThisReceiver(ast) {
  7108. return false;
  7109. },
  7110. visitInterpolation(ast) {
  7111. return visitSome(this, ast.expressions);
  7112. },
  7113. visitKeyedRead(ast) {
  7114. return false;
  7115. },
  7116. visitKeyedWrite(ast) {
  7117. return false;
  7118. },
  7119. visitLiteralArray(ast) {
  7120. return true;
  7121. },
  7122. visitLiteralMap(ast) {
  7123. return true;
  7124. },
  7125. visitLiteralPrimitive(ast) {
  7126. return false;
  7127. },
  7128. visitPipe(ast) {
  7129. return true;
  7130. },
  7131. visitPrefixNot(ast) {
  7132. return visit(this, ast.expression);
  7133. },
  7134. visitNonNullAssert(ast) {
  7135. return visit(this, ast.expression);
  7136. },
  7137. visitPropertyRead(ast) {
  7138. return false;
  7139. },
  7140. visitPropertyWrite(ast) {
  7141. return false;
  7142. },
  7143. visitSafePropertyRead(ast) {
  7144. return false;
  7145. },
  7146. visitSafeKeyedRead(ast) {
  7147. return false;
  7148. }
  7149. });
  7150. }
  7151. allocateTemporary() {
  7152. const tempNumber = this._currentTemporary++;
  7153. this.temporaryCount = Math.max(this._currentTemporary, this.temporaryCount);
  7154. return new ReadVarExpr(temporaryName(this.bindingId, tempNumber));
  7155. }
  7156. releaseTemporary(temporary) {
  7157. this._currentTemporary--;
  7158. if (temporary.name != temporaryName(this.bindingId, this._currentTemporary)) {
  7159. throw new Error(`Temporary ${temporary.name} released out of order`);
  7160. }
  7161. }
  7162. /**
  7163. * Creates an absolute `ParseSourceSpan` from the relative `ParseSpan`.
  7164. *
  7165. * `ParseSpan` objects are relative to the start of the expression.
  7166. * This method converts these to full `ParseSourceSpan` objects that
  7167. * show where the span is within the overall source file.
  7168. *
  7169. * @param span the relative span to convert.
  7170. * @returns a `ParseSourceSpan` for the given span or null if no
  7171. * `baseSourceSpan` was provided to this class.
  7172. */
  7173. convertSourceSpan(span) {
  7174. if (this.baseSourceSpan) {
  7175. const start = this.baseSourceSpan.start.moveBy(span.start);
  7176. const end = this.baseSourceSpan.start.moveBy(span.end);
  7177. const fullStart = this.baseSourceSpan.fullStart.moveBy(span.start);
  7178. return new ParseSourceSpan(start, end, fullStart);
  7179. }
  7180. else {
  7181. return null;
  7182. }
  7183. }
  7184. /** Adds the name of an AST to the list of implicit receiver accesses. */
  7185. addImplicitReceiverAccess(name) {
  7186. if (this.implicitReceiverAccesses) {
  7187. this.implicitReceiverAccesses.add(name);
  7188. }
  7189. }
  7190. }
  7191. function flattenStatements(arg, output) {
  7192. if (Array.isArray(arg)) {
  7193. arg.forEach((entry) => flattenStatements(entry, output));
  7194. }
  7195. else {
  7196. output.push(arg);
  7197. }
  7198. }
  7199. function unsupported() {
  7200. throw new Error('Unsupported operation');
  7201. }
  7202. class InterpolationExpression extends Expression {
  7203. constructor(args) {
  7204. super(null, null);
  7205. this.args = args;
  7206. this.isConstant = unsupported;
  7207. this.isEquivalent = unsupported;
  7208. this.visitExpression = unsupported;
  7209. }
  7210. }
  7211. class DefaultLocalResolver {
  7212. constructor(globals) {
  7213. this.globals = globals;
  7214. }
  7215. notifyImplicitReceiverUse() { }
  7216. maybeRestoreView() { }
  7217. getLocal(name) {
  7218. if (name === EventHandlerVars.event.name) {
  7219. return EventHandlerVars.event;
  7220. }
  7221. return null;
  7222. }
  7223. }
  7224. class BuiltinFunctionCall extends Call {
  7225. constructor(span, sourceSpan, args, converter) {
  7226. super(span, sourceSpan, new EmptyExpr(span, sourceSpan), args, null);
  7227. this.converter = converter;
  7228. }
  7229. }
  7230. // =================================================================================================
  7231. // =================================================================================================
  7232. // =========== S T O P - S T O P - S T O P - S T O P - S T O P - S T O P ===========
  7233. // =================================================================================================
  7234. // =================================================================================================
  7235. //
  7236. // DO NOT EDIT THIS LIST OF SECURITY SENSITIVE PROPERTIES WITHOUT A SECURITY REVIEW!
  7237. // Reach out to mprobst for details.
  7238. //
  7239. // =================================================================================================
  7240. /** Map from tagName|propertyName to SecurityContext. Properties applying to all tags use '*'. */
  7241. let _SECURITY_SCHEMA;
  7242. function SECURITY_SCHEMA() {
  7243. if (!_SECURITY_SCHEMA) {
  7244. _SECURITY_SCHEMA = {};
  7245. // Case is insignificant below, all element and attribute names are lower-cased for lookup.
  7246. registerContext(SecurityContext.HTML, [
  7247. 'iframe|srcdoc',
  7248. '*|innerHTML',
  7249. '*|outerHTML',
  7250. ]);
  7251. registerContext(SecurityContext.STYLE, ['*|style']);
  7252. // NB: no SCRIPT contexts here, they are never allowed due to the parser stripping them.
  7253. registerContext(SecurityContext.URL, [
  7254. '*|formAction',
  7255. 'area|href',
  7256. 'area|ping',
  7257. 'audio|src',
  7258. 'a|href',
  7259. 'a|ping',
  7260. 'blockquote|cite',
  7261. 'body|background',
  7262. 'del|cite',
  7263. 'form|action',
  7264. 'img|src',
  7265. 'input|src',
  7266. 'ins|cite',
  7267. 'q|cite',
  7268. 'source|src',
  7269. 'track|src',
  7270. 'video|poster',
  7271. 'video|src',
  7272. ]);
  7273. registerContext(SecurityContext.RESOURCE_URL, [
  7274. 'applet|code',
  7275. 'applet|codebase',
  7276. 'base|href',
  7277. 'embed|src',
  7278. 'frame|src',
  7279. 'head|profile',
  7280. 'html|manifest',
  7281. 'iframe|src',
  7282. 'link|href',
  7283. 'media|src',
  7284. 'object|codebase',
  7285. 'object|data',
  7286. 'script|src',
  7287. ]);
  7288. }
  7289. return _SECURITY_SCHEMA;
  7290. }
  7291. function registerContext(ctx, specs) {
  7292. for (const spec of specs)
  7293. _SECURITY_SCHEMA[spec.toLowerCase()] = ctx;
  7294. }
  7295. /**
  7296. * The set of security-sensitive attributes of an `<iframe>` that *must* be
  7297. * applied as a static attribute only. This ensures that all security-sensitive
  7298. * attributes are taken into account while creating an instance of an `<iframe>`
  7299. * at runtime.
  7300. *
  7301. * Note: avoid using this set directly, use the `isIframeSecuritySensitiveAttr` function
  7302. * in the code instead.
  7303. */
  7304. const IFRAME_SECURITY_SENSITIVE_ATTRS = new Set(['sandbox', 'allow', 'allowfullscreen', 'referrerpolicy', 'csp', 'fetchpriority']);
  7305. /**
  7306. * Checks whether a given attribute name might represent a security-sensitive
  7307. * attribute of an <iframe>.
  7308. */
  7309. function isIframeSecuritySensitiveAttr(attrName) {
  7310. // The `setAttribute` DOM API is case-insensitive, so we lowercase the value
  7311. // before checking it against a known security-sensitive attributes.
  7312. return IFRAME_SECURITY_SENSITIVE_ATTRS.has(attrName.toLowerCase());
  7313. }
  7314. /**
  7315. * The following set contains all keywords that can be used in the animation css shorthand
  7316. * property and is used during the scoping of keyframes to make sure such keywords
  7317. * are not modified.
  7318. */
  7319. const animationKeywords = new Set([
  7320. // global values
  7321. 'inherit', 'initial', 'revert', 'unset',
  7322. // animation-direction
  7323. 'alternate', 'alternate-reverse', 'normal', 'reverse',
  7324. // animation-fill-mode
  7325. 'backwards', 'both', 'forwards', 'none',
  7326. // animation-play-state
  7327. 'paused', 'running',
  7328. // animation-timing-function
  7329. 'ease', 'ease-in', 'ease-in-out', 'ease-out', 'linear', 'step-start', 'step-end',
  7330. // `steps()` function
  7331. 'end', 'jump-both', 'jump-end', 'jump-none', 'jump-start', 'start'
  7332. ]);
  7333. /**
  7334. * The following class has its origin from a port of shadowCSS from webcomponents.js to TypeScript.
  7335. * It has since diverge in many ways to tailor Angular's needs.
  7336. *
  7337. * Source:
  7338. * https://github.com/webcomponents/webcomponentsjs/blob/4efecd7e0e/src/ShadowCSS/ShadowCSS.js
  7339. *
  7340. * The original file level comment is reproduced below
  7341. */
  7342. /*
  7343. This is a limited shim for ShadowDOM css styling.
  7344. https://dvcs.w3.org/hg/webcomponents/raw-file/tip/spec/shadow/index.html#styles
  7345. The intention here is to support only the styling features which can be
  7346. relatively simply implemented. The goal is to allow users to avoid the
  7347. most obvious pitfalls and do so without compromising performance significantly.
  7348. For ShadowDOM styling that's not covered here, a set of best practices
  7349. can be provided that should allow users to accomplish more complex styling.
  7350. The following is a list of specific ShadowDOM styling features and a brief
  7351. discussion of the approach used to shim.
  7352. Shimmed features:
  7353. * :host, :host-context: ShadowDOM allows styling of the shadowRoot's host
  7354. element using the :host rule. To shim this feature, the :host styles are
  7355. reformatted and prefixed with a given scope name and promoted to a
  7356. document level stylesheet.
  7357. For example, given a scope name of .foo, a rule like this:
  7358. :host {
  7359. background: red;
  7360. }
  7361. }
  7362. becomes:
  7363. .foo {
  7364. background: red;
  7365. }
  7366. * encapsulation: Styles defined within ShadowDOM, apply only to
  7367. dom inside the ShadowDOM.
  7368. The selectors are scoped by adding an attribute selector suffix to each
  7369. simple selector that contains the host element tag name. Each element
  7370. in the element's ShadowDOM template is also given the scope attribute.
  7371. Thus, these rules match only elements that have the scope attribute.
  7372. For example, given a scope name of x-foo, a rule like this:
  7373. div {
  7374. font-weight: bold;
  7375. }
  7376. becomes:
  7377. div[x-foo] {
  7378. font-weight: bold;
  7379. }
  7380. Note that elements that are dynamically added to a scope must have the scope
  7381. selector added to them manually.
  7382. * upper/lower bound encapsulation: Styles which are defined outside a
  7383. shadowRoot should not cross the ShadowDOM boundary and should not apply
  7384. inside a shadowRoot.
  7385. This styling behavior is not emulated. Some possible ways to do this that
  7386. were rejected due to complexity and/or performance concerns include: (1) reset
  7387. every possible property for every possible selector for a given scope name;
  7388. (2) re-implement css in javascript.
  7389. As an alternative, users should make sure to use selectors
  7390. specific to the scope in which they are working.
  7391. * ::distributed: This behavior is not emulated. It's often not necessary
  7392. to style the contents of a specific insertion point and instead, descendants
  7393. of the host element can be styled selectively. Users can also create an
  7394. extra node around an insertion point and style that node's contents
  7395. via descendent selectors. For example, with a shadowRoot like this:
  7396. <style>
  7397. ::content(div) {
  7398. background: red;
  7399. }
  7400. </style>
  7401. <content></content>
  7402. could become:
  7403. <style>
  7404. / *@polyfill .content-container div * /
  7405. ::content(div) {
  7406. background: red;
  7407. }
  7408. </style>
  7409. <div class="content-container">
  7410. <content></content>
  7411. </div>
  7412. Note the use of @polyfill in the comment above a ShadowDOM specific style
  7413. declaration. This is a directive to the styling shim to use the selector
  7414. in comments in lieu of the next selector when running under polyfill.
  7415. */
  7416. class ShadowCss {
  7417. constructor() {
  7418. /**
  7419. * Regular expression used to extrapolate the possible keyframes from an
  7420. * animation declaration (with possibly multiple animation definitions)
  7421. *
  7422. * The regular expression can be divided in three parts
  7423. * - (^|\s+)
  7424. * simply captures how many (if any) leading whitespaces are present
  7425. * - (?:(?:(['"])((?:\\\\|\\\2|(?!\2).)+)\2)|(-?[A-Za-z][\w\-]*))
  7426. * captures two different possible keyframes, ones which are quoted or ones which are valid css
  7427. * idents (custom properties excluded)
  7428. * - (?=[,\s;]|$)
  7429. * simply matches the end of the possible keyframe, valid endings are: a comma, a space, a
  7430. * semicolon or the end of the string
  7431. */
  7432. this._animationDeclarationKeyframesRe = /(^|\s+)(?:(?:(['"])((?:\\\\|\\\2|(?!\2).)+)\2)|(-?[A-Za-z][\w\-]*))(?=[,\s]|$)/g;
  7433. }
  7434. /*
  7435. * Shim some cssText with the given selector. Returns cssText that can be included in the document
  7436. *
  7437. * The selector is the attribute added to all elements inside the host,
  7438. * The hostSelector is the attribute added to the host itself.
  7439. */
  7440. shimCssText(cssText, selector, hostSelector = '') {
  7441. const commentsWithHash = extractCommentsWithHash(cssText);
  7442. cssText = stripComments(cssText);
  7443. cssText = this._insertDirectives(cssText);
  7444. const scopedCssText = this._scopeCssText(cssText, selector, hostSelector);
  7445. return [scopedCssText, ...commentsWithHash].join('\n');
  7446. }
  7447. _insertDirectives(cssText) {
  7448. cssText = this._insertPolyfillDirectivesInCssText(cssText);
  7449. return this._insertPolyfillRulesInCssText(cssText);
  7450. }
  7451. /**
  7452. * Process styles to add scope to keyframes.
  7453. *
  7454. * Modify both the names of the keyframes defined in the component styles and also the css
  7455. * animation rules using them.
  7456. *
  7457. * Animation rules using keyframes defined elsewhere are not modified to allow for globally
  7458. * defined keyframes.
  7459. *
  7460. * For example, we convert this css:
  7461. *
  7462. * ```
  7463. * .box {
  7464. * animation: box-animation 1s forwards;
  7465. * }
  7466. *
  7467. * @keyframes box-animation {
  7468. * to {
  7469. * background-color: green;
  7470. * }
  7471. * }
  7472. * ```
  7473. *
  7474. * to this:
  7475. *
  7476. * ```
  7477. * .box {
  7478. * animation: scopeName_box-animation 1s forwards;
  7479. * }
  7480. *
  7481. * @keyframes scopeName_box-animation {
  7482. * to {
  7483. * background-color: green;
  7484. * }
  7485. * }
  7486. * ```
  7487. *
  7488. * @param cssText the component's css text that needs to be scoped.
  7489. * @param scopeSelector the component's scope selector.
  7490. *
  7491. * @returns the scoped css text.
  7492. */
  7493. _scopeKeyframesRelatedCss(cssText, scopeSelector) {
  7494. const unscopedKeyframesSet = new Set();
  7495. const scopedKeyframesCssText = processRules(cssText, rule => this._scopeLocalKeyframeDeclarations(rule, scopeSelector, unscopedKeyframesSet));
  7496. return processRules(scopedKeyframesCssText, rule => this._scopeAnimationRule(rule, scopeSelector, unscopedKeyframesSet));
  7497. }
  7498. /**
  7499. * Scopes local keyframes names, returning the updated css rule and it also
  7500. * adds the original keyframe name to a provided set to collect all keyframes names
  7501. * so that it can later be used to scope the animation rules.
  7502. *
  7503. * For example, it takes a rule such as:
  7504. *
  7505. * ```
  7506. * @keyframes box-animation {
  7507. * to {
  7508. * background-color: green;
  7509. * }
  7510. * }
  7511. * ```
  7512. *
  7513. * and returns:
  7514. *
  7515. * ```
  7516. * @keyframes scopeName_box-animation {
  7517. * to {
  7518. * background-color: green;
  7519. * }
  7520. * }
  7521. * ```
  7522. * and as a side effect it adds "box-animation" to the `unscopedKeyframesSet` set
  7523. *
  7524. * @param cssRule the css rule to process.
  7525. * @param scopeSelector the component's scope selector.
  7526. * @param unscopedKeyframesSet the set of unscoped keyframes names (which can be
  7527. * modified as a side effect)
  7528. *
  7529. * @returns the css rule modified with the scoped keyframes name.
  7530. */
  7531. _scopeLocalKeyframeDeclarations(rule, scopeSelector, unscopedKeyframesSet) {
  7532. return {
  7533. ...rule,
  7534. selector: rule.selector.replace(/(^@(?:-webkit-)?keyframes(?:\s+))(['"]?)(.+)\2(\s*)$/, (_, start, quote, keyframeName, endSpaces) => {
  7535. unscopedKeyframesSet.add(unescapeQuotes(keyframeName, quote));
  7536. return `${start}${quote}${scopeSelector}_${keyframeName}${quote}${endSpaces}`;
  7537. }),
  7538. };
  7539. }
  7540. /**
  7541. * Function used to scope a keyframes name (obtained from an animation declaration)
  7542. * using an existing set of unscopedKeyframes names to discern if the scoping needs to be
  7543. * performed (keyframes names of keyframes not defined in the component's css need not to be
  7544. * scoped).
  7545. *
  7546. * @param keyframe the keyframes name to check.
  7547. * @param scopeSelector the component's scope selector.
  7548. * @param unscopedKeyframesSet the set of unscoped keyframes names.
  7549. *
  7550. * @returns the scoped name of the keyframe, or the original name is the name need not to be
  7551. * scoped.
  7552. */
  7553. _scopeAnimationKeyframe(keyframe, scopeSelector, unscopedKeyframesSet) {
  7554. return keyframe.replace(/^(\s*)(['"]?)(.+?)\2(\s*)$/, (_, spaces1, quote, name, spaces2) => {
  7555. name = `${unscopedKeyframesSet.has(unescapeQuotes(name, quote)) ? scopeSelector + '_' : ''}${name}`;
  7556. return `${spaces1}${quote}${name}${quote}${spaces2}`;
  7557. });
  7558. }
  7559. /**
  7560. * Scope an animation rule so that the keyframes mentioned in such rule
  7561. * are scoped if defined in the component's css and left untouched otherwise.
  7562. *
  7563. * It can scope values of both the 'animation' and 'animation-name' properties.
  7564. *
  7565. * @param rule css rule to scope.
  7566. * @param scopeSelector the component's scope selector.
  7567. * @param unscopedKeyframesSet the set of unscoped keyframes names.
  7568. *
  7569. * @returns the updated css rule.
  7570. **/
  7571. _scopeAnimationRule(rule, scopeSelector, unscopedKeyframesSet) {
  7572. let content = rule.content.replace(/((?:^|\s+|;)(?:-webkit-)?animation(?:\s*):(?:\s*))([^;]+)/g, (_, start, animationDeclarations) => start +
  7573. animationDeclarations.replace(this._animationDeclarationKeyframesRe, (original, leadingSpaces, quote = '', quotedName, nonQuotedName) => {
  7574. if (quotedName) {
  7575. return `${leadingSpaces}${this._scopeAnimationKeyframe(`${quote}${quotedName}${quote}`, scopeSelector, unscopedKeyframesSet)}`;
  7576. }
  7577. else {
  7578. return animationKeywords.has(nonQuotedName) ?
  7579. original :
  7580. `${leadingSpaces}${this._scopeAnimationKeyframe(nonQuotedName, scopeSelector, unscopedKeyframesSet)}`;
  7581. }
  7582. }));
  7583. content = content.replace(/((?:^|\s+|;)(?:-webkit-)?animation-name(?:\s*):(?:\s*))([^;]+)/g, (_match, start, commaSeparatedKeyframes) => `${start}${commaSeparatedKeyframes.split(',')
  7584. .map((keyframe) => this._scopeAnimationKeyframe(keyframe, scopeSelector, unscopedKeyframesSet))
  7585. .join(',')}`);
  7586. return { ...rule, content };
  7587. }
  7588. /*
  7589. * Process styles to convert native ShadowDOM rules that will trip
  7590. * up the css parser; we rely on decorating the stylesheet with inert rules.
  7591. *
  7592. * For example, we convert this rule:
  7593. *
  7594. * polyfill-next-selector { content: ':host menu-item'; }
  7595. * ::content menu-item {
  7596. *
  7597. * to this:
  7598. *
  7599. * scopeName menu-item {
  7600. *
  7601. **/
  7602. _insertPolyfillDirectivesInCssText(cssText) {
  7603. return cssText.replace(_cssContentNextSelectorRe, function (...m) {
  7604. return m[2] + '{';
  7605. });
  7606. }
  7607. /*
  7608. * Process styles to add rules which will only apply under the polyfill
  7609. *
  7610. * For example, we convert this rule:
  7611. *
  7612. * polyfill-rule {
  7613. * content: ':host menu-item';
  7614. * ...
  7615. * }
  7616. *
  7617. * to this:
  7618. *
  7619. * scopeName menu-item {...}
  7620. *
  7621. **/
  7622. _insertPolyfillRulesInCssText(cssText) {
  7623. return cssText.replace(_cssContentRuleRe, (...m) => {
  7624. const rule = m[0].replace(m[1], '').replace(m[2], '');
  7625. return m[4] + rule;
  7626. });
  7627. }
  7628. /* Ensure styles are scoped. Pseudo-scoping takes a rule like:
  7629. *
  7630. * .foo {... }
  7631. *
  7632. * and converts this to
  7633. *
  7634. * scopeName .foo { ... }
  7635. */
  7636. _scopeCssText(cssText, scopeSelector, hostSelector) {
  7637. const unscopedRules = this._extractUnscopedRulesFromCssText(cssText);
  7638. // replace :host and :host-context -shadowcsshost and -shadowcsshost respectively
  7639. cssText = this._insertPolyfillHostInCssText(cssText);
  7640. cssText = this._convertColonHost(cssText);
  7641. cssText = this._convertColonHostContext(cssText);
  7642. cssText = this._convertShadowDOMSelectors(cssText);
  7643. if (scopeSelector) {
  7644. cssText = this._scopeKeyframesRelatedCss(cssText, scopeSelector);
  7645. cssText = this._scopeSelectors(cssText, scopeSelector, hostSelector);
  7646. }
  7647. cssText = cssText + '\n' + unscopedRules;
  7648. return cssText.trim();
  7649. }
  7650. /*
  7651. * Process styles to add rules which will only apply under the polyfill
  7652. * and do not process via CSSOM. (CSSOM is destructive to rules on rare
  7653. * occasions, e.g. -webkit-calc on Safari.)
  7654. * For example, we convert this rule:
  7655. *
  7656. * @polyfill-unscoped-rule {
  7657. * content: 'menu-item';
  7658. * ... }
  7659. *
  7660. * to this:
  7661. *
  7662. * menu-item {...}
  7663. *
  7664. **/
  7665. _extractUnscopedRulesFromCssText(cssText) {
  7666. let r = '';
  7667. let m;
  7668. _cssContentUnscopedRuleRe.lastIndex = 0;
  7669. while ((m = _cssContentUnscopedRuleRe.exec(cssText)) !== null) {
  7670. const rule = m[0].replace(m[2], '').replace(m[1], m[4]);
  7671. r += rule + '\n\n';
  7672. }
  7673. return r;
  7674. }
  7675. /*
  7676. * convert a rule like :host(.foo) > .bar { }
  7677. *
  7678. * to
  7679. *
  7680. * .foo<scopeName> > .bar
  7681. */
  7682. _convertColonHost(cssText) {
  7683. return cssText.replace(_cssColonHostRe, (_, hostSelectors, otherSelectors) => {
  7684. if (hostSelectors) {
  7685. const convertedSelectors = [];
  7686. const hostSelectorArray = hostSelectors.split(',').map(p => p.trim());
  7687. for (const hostSelector of hostSelectorArray) {
  7688. if (!hostSelector)
  7689. break;
  7690. const convertedSelector = _polyfillHostNoCombinator + hostSelector.replace(_polyfillHost, '') + otherSelectors;
  7691. convertedSelectors.push(convertedSelector);
  7692. }
  7693. return convertedSelectors.join(',');
  7694. }
  7695. else {
  7696. return _polyfillHostNoCombinator + otherSelectors;
  7697. }
  7698. });
  7699. }
  7700. /*
  7701. * convert a rule like :host-context(.foo) > .bar { }
  7702. *
  7703. * to
  7704. *
  7705. * .foo<scopeName> > .bar, .foo <scopeName> > .bar { }
  7706. *
  7707. * and
  7708. *
  7709. * :host-context(.foo:host) .bar { ... }
  7710. *
  7711. * to
  7712. *
  7713. * .foo<scopeName> .bar { ... }
  7714. */
  7715. _convertColonHostContext(cssText) {
  7716. return cssText.replace(_cssColonHostContextReGlobal, selectorText => {
  7717. // We have captured a selector that contains a `:host-context` rule.
  7718. // For backward compatibility `:host-context` may contain a comma separated list of selectors.
  7719. // Each context selector group will contain a list of host-context selectors that must match
  7720. // an ancestor of the host.
  7721. // (Normally `contextSelectorGroups` will only contain a single array of context selectors.)
  7722. const contextSelectorGroups = [[]];
  7723. // There may be more than `:host-context` in this selector so `selectorText` could look like:
  7724. // `:host-context(.one):host-context(.two)`.
  7725. // Execute `_cssColonHostContextRe` over and over until we have extracted all the
  7726. // `:host-context` selectors from this selector.
  7727. let match;
  7728. while (match = _cssColonHostContextRe.exec(selectorText)) {
  7729. // `match` = [':host-context(<selectors>)<rest>', <selectors>, <rest>]
  7730. // The `<selectors>` could actually be a comma separated list: `:host-context(.one, .two)`.
  7731. const newContextSelectors = (match[1] ?? '').trim().split(',').map(m => m.trim()).filter(m => m !== '');
  7732. // We must duplicate the current selector group for each of these new selectors.
  7733. // For example if the current groups are:
  7734. // ```
  7735. // [
  7736. // ['a', 'b', 'c'],
  7737. // ['x', 'y', 'z'],
  7738. // ]
  7739. // ```
  7740. // And we have a new set of comma separated selectors: `:host-context(m,n)` then the new
  7741. // groups are:
  7742. // ```
  7743. // [
  7744. // ['a', 'b', 'c', 'm'],
  7745. // ['x', 'y', 'z', 'm'],
  7746. // ['a', 'b', 'c', 'n'],
  7747. // ['x', 'y', 'z', 'n'],
  7748. // ]
  7749. // ```
  7750. const contextSelectorGroupsLength = contextSelectorGroups.length;
  7751. repeatGroups(contextSelectorGroups, newContextSelectors.length);
  7752. for (let i = 0; i < newContextSelectors.length; i++) {
  7753. for (let j = 0; j < contextSelectorGroupsLength; j++) {
  7754. contextSelectorGroups[j + (i * contextSelectorGroupsLength)].push(newContextSelectors[i]);
  7755. }
  7756. }
  7757. // Update the `selectorText` and see repeat to see if there are more `:host-context`s.
  7758. selectorText = match[2];
  7759. }
  7760. // The context selectors now must be combined with each other to capture all the possible
  7761. // selectors that `:host-context` can match. See `combineHostContextSelectors()` for more
  7762. // info about how this is done.
  7763. return contextSelectorGroups
  7764. .map(contextSelectors => combineHostContextSelectors(contextSelectors, selectorText))
  7765. .join(', ');
  7766. });
  7767. }
  7768. /*
  7769. * Convert combinators like ::shadow and pseudo-elements like ::content
  7770. * by replacing with space.
  7771. */
  7772. _convertShadowDOMSelectors(cssText) {
  7773. return _shadowDOMSelectorsRe.reduce((result, pattern) => result.replace(pattern, ' '), cssText);
  7774. }
  7775. // change a selector like 'div' to 'name div'
  7776. _scopeSelectors(cssText, scopeSelector, hostSelector) {
  7777. return processRules(cssText, (rule) => {
  7778. let selector = rule.selector;
  7779. let content = rule.content;
  7780. if (rule.selector[0] !== '@') {
  7781. selector = this._scopeSelector(rule.selector, scopeSelector, hostSelector);
  7782. }
  7783. else if (rule.selector.startsWith('@media') || rule.selector.startsWith('@supports') ||
  7784. rule.selector.startsWith('@document') || rule.selector.startsWith('@layer') ||
  7785. rule.selector.startsWith('@container')) {
  7786. content = this._scopeSelectors(rule.content, scopeSelector, hostSelector);
  7787. }
  7788. else if (rule.selector.startsWith('@font-face') || rule.selector.startsWith('@page')) {
  7789. content = this._stripScopingSelectors(rule.content);
  7790. }
  7791. return new CssRule(selector, content);
  7792. });
  7793. }
  7794. /**
  7795. * Handle a css text that is within a rule that should not contain scope selectors by simply
  7796. * removing them! An example of such a rule is `@font-face`.
  7797. *
  7798. * `@font-face` rules cannot contain nested selectors. Nor can they be nested under a selector.
  7799. * Normally this would be a syntax error by the author of the styles. But in some rare cases, such
  7800. * as importing styles from a library, and applying `:host ::ng-deep` to the imported styles, we
  7801. * can end up with broken css if the imported styles happen to contain @font-face rules.
  7802. *
  7803. * For example:
  7804. *
  7805. * ```
  7806. * :host ::ng-deep {
  7807. * import 'some/lib/containing/font-face';
  7808. * }
  7809. *
  7810. * Similar logic applies to `@page` rules which can contain a particular set of properties,
  7811. * as well as some specific at-rules. Since they can't be encapsulated, we have to strip
  7812. * any scoping selectors from them. For more information: https://www.w3.org/TR/css-page-3
  7813. * ```
  7814. */
  7815. _stripScopingSelectors(cssText) {
  7816. return processRules(cssText, rule => {
  7817. const selector = rule.selector.replace(_shadowDeepSelectors, ' ')
  7818. .replace(_polyfillHostNoCombinatorRe, ' ');
  7819. return new CssRule(selector, rule.content);
  7820. });
  7821. }
  7822. _scopeSelector(selector, scopeSelector, hostSelector) {
  7823. return selector.split(',')
  7824. .map(part => part.trim().split(_shadowDeepSelectors))
  7825. .map((deepParts) => {
  7826. const [shallowPart, ...otherParts] = deepParts;
  7827. const applyScope = (shallowPart) => {
  7828. if (this._selectorNeedsScoping(shallowPart, scopeSelector)) {
  7829. return this._applySelectorScope(shallowPart, scopeSelector, hostSelector);
  7830. }
  7831. else {
  7832. return shallowPart;
  7833. }
  7834. };
  7835. return [applyScope(shallowPart), ...otherParts].join(' ');
  7836. })
  7837. .join(', ');
  7838. }
  7839. _selectorNeedsScoping(selector, scopeSelector) {
  7840. const re = this._makeScopeMatcher(scopeSelector);
  7841. return !re.test(selector);
  7842. }
  7843. _makeScopeMatcher(scopeSelector) {
  7844. const lre = /\[/g;
  7845. const rre = /\]/g;
  7846. scopeSelector = scopeSelector.replace(lre, '\\[').replace(rre, '\\]');
  7847. return new RegExp('^(' + scopeSelector + ')' + _selectorReSuffix, 'm');
  7848. }
  7849. // scope via name and [is=name]
  7850. _applySimpleSelectorScope(selector, scopeSelector, hostSelector) {
  7851. // In Android browser, the lastIndex is not reset when the regex is used in String.replace()
  7852. _polyfillHostRe.lastIndex = 0;
  7853. if (_polyfillHostRe.test(selector)) {
  7854. const replaceBy = `[${hostSelector}]`;
  7855. return selector
  7856. .replace(_polyfillHostNoCombinatorRe, (hnc, selector) => {
  7857. return selector.replace(/([^:]*)(:*)(.*)/, (_, before, colon, after) => {
  7858. return before + replaceBy + colon + after;
  7859. });
  7860. })
  7861. .replace(_polyfillHostRe, replaceBy + ' ');
  7862. }
  7863. return scopeSelector + ' ' + selector;
  7864. }
  7865. // return a selector with [name] suffix on each simple selector
  7866. // e.g. .foo.bar > .zot becomes .foo[name].bar[name] > .zot[name] /** @internal */
  7867. _applySelectorScope(selector, scopeSelector, hostSelector) {
  7868. const isRe = /\[is=([^\]]*)\]/g;
  7869. scopeSelector = scopeSelector.replace(isRe, (_, ...parts) => parts[0]);
  7870. const attrName = '[' + scopeSelector + ']';
  7871. const _scopeSelectorPart = (p) => {
  7872. let scopedP = p.trim();
  7873. if (!scopedP) {
  7874. return '';
  7875. }
  7876. if (p.indexOf(_polyfillHostNoCombinator) > -1) {
  7877. scopedP = this._applySimpleSelectorScope(p, scopeSelector, hostSelector);
  7878. }
  7879. else {
  7880. // remove :host since it should be unnecessary
  7881. const t = p.replace(_polyfillHostRe, '');
  7882. if (t.length > 0) {
  7883. const matches = t.match(/([^:]*)(:*)(.*)/);
  7884. if (matches) {
  7885. scopedP = matches[1] + attrName + matches[2] + matches[3];
  7886. }
  7887. }
  7888. }
  7889. return scopedP;
  7890. };
  7891. const safeContent = new SafeSelector(selector);
  7892. selector = safeContent.content();
  7893. let scopedSelector = '';
  7894. let startIndex = 0;
  7895. let res;
  7896. const sep = /( |>|\+|~(?!=))\s*/g;
  7897. // If a selector appears before :host it should not be shimmed as it
  7898. // matches on ancestor elements and not on elements in the host's shadow
  7899. // `:host-context(div)` is transformed to
  7900. // `-shadowcsshost-no-combinatordiv, div -shadowcsshost-no-combinator`
  7901. // the `div` is not part of the component in the 2nd selectors and should not be scoped.
  7902. // Historically `component-tag:host` was matching the component so we also want to preserve
  7903. // this behavior to avoid breaking legacy apps (it should not match).
  7904. // The behavior should be:
  7905. // - `tag:host` -> `tag[h]` (this is to avoid breaking legacy apps, should not match anything)
  7906. // - `tag :host` -> `tag [h]` (`tag` is not scoped because it's considered part of a
  7907. // `:host-context(tag)`)
  7908. const hasHost = selector.indexOf(_polyfillHostNoCombinator) > -1;
  7909. // Only scope parts after the first `-shadowcsshost-no-combinator` when it is present
  7910. let shouldScope = !hasHost;
  7911. while ((res = sep.exec(selector)) !== null) {
  7912. const separator = res[1];
  7913. const part = selector.slice(startIndex, res.index).trim();
  7914. // A space following an escaped hex value and followed by another hex character
  7915. // (ie: ".\fc ber" for ".über") is not a separator between 2 selectors
  7916. // also keep in mind that backslashes are replaced by a placeholder by SafeSelector
  7917. // These escaped selectors happen for example when esbuild runs with optimization.minify.
  7918. if (part.match(_placeholderRe) && selector[res.index + 1]?.match(/[a-fA-F\d]/)) {
  7919. continue;
  7920. }
  7921. shouldScope = shouldScope || part.indexOf(_polyfillHostNoCombinator) > -1;
  7922. const scopedPart = shouldScope ? _scopeSelectorPart(part) : part;
  7923. scopedSelector += `${scopedPart} ${separator} `;
  7924. startIndex = sep.lastIndex;
  7925. }
  7926. const part = selector.substring(startIndex);
  7927. shouldScope = shouldScope || part.indexOf(_polyfillHostNoCombinator) > -1;
  7928. scopedSelector += shouldScope ? _scopeSelectorPart(part) : part;
  7929. // replace the placeholders with their original values
  7930. return safeContent.restore(scopedSelector);
  7931. }
  7932. _insertPolyfillHostInCssText(selector) {
  7933. return selector.replace(_colonHostContextRe, _polyfillHostContext)
  7934. .replace(_colonHostRe, _polyfillHost);
  7935. }
  7936. }
  7937. class SafeSelector {
  7938. constructor(selector) {
  7939. this.placeholders = [];
  7940. this.index = 0;
  7941. // Replaces attribute selectors with placeholders.
  7942. // The WS in [attr="va lue"] would otherwise be interpreted as a selector separator.
  7943. selector = this._escapeRegexMatches(selector, /(\[[^\]]*\])/g);
  7944. // CSS allows for certain special characters to be used in selectors if they're escaped.
  7945. // E.g. `.foo:blue` won't match a class called `foo:blue`, because the colon denotes a
  7946. // pseudo-class, but writing `.foo\:blue` will match, because the colon was escaped.
  7947. // Replace all escape sequences (`\` followed by a character) with a placeholder so
  7948. // that our handling of pseudo-selectors doesn't mess with them.
  7949. selector = this._escapeRegexMatches(selector, /(\\.)/g);
  7950. // Replaces the expression in `:nth-child(2n + 1)` with a placeholder.
  7951. // WS and "+" would otherwise be interpreted as selector separators.
  7952. this._content = selector.replace(/(:nth-[-\w]+)(\([^)]+\))/g, (_, pseudo, exp) => {
  7953. const replaceBy = `__ph-${this.index}__`;
  7954. this.placeholders.push(exp);
  7955. this.index++;
  7956. return pseudo + replaceBy;
  7957. });
  7958. }
  7959. restore(content) {
  7960. return content.replace(_placeholderRe, (_ph, index) => this.placeholders[+index]);
  7961. }
  7962. content() {
  7963. return this._content;
  7964. }
  7965. /**
  7966. * Replaces all of the substrings that match a regex within a
  7967. * special string (e.g. `__ph-0__`, `__ph-1__`, etc).
  7968. */
  7969. _escapeRegexMatches(content, pattern) {
  7970. return content.replace(pattern, (_, keep) => {
  7971. const replaceBy = `__ph-${this.index}__`;
  7972. this.placeholders.push(keep);
  7973. this.index++;
  7974. return replaceBy;
  7975. });
  7976. }
  7977. }
  7978. const _cssContentNextSelectorRe = /polyfill-next-selector[^}]*content:[\s]*?(['"])(.*?)\1[;\s]*}([^{]*?){/gim;
  7979. const _cssContentRuleRe = /(polyfill-rule)[^}]*(content:[\s]*(['"])(.*?)\3)[;\s]*[^}]*}/gim;
  7980. const _cssContentUnscopedRuleRe = /(polyfill-unscoped-rule)[^}]*(content:[\s]*(['"])(.*?)\3)[;\s]*[^}]*}/gim;
  7981. const _polyfillHost = '-shadowcsshost';
  7982. // note: :host-context pre-processed to -shadowcsshostcontext.
  7983. const _polyfillHostContext = '-shadowcsscontext';
  7984. const _parenSuffix = '(?:\\((' +
  7985. '(?:\\([^)(]*\\)|[^)(]*)+?' +
  7986. ')\\))?([^,{]*)';
  7987. const _cssColonHostRe = new RegExp(_polyfillHost + _parenSuffix, 'gim');
  7988. const _cssColonHostContextReGlobal = new RegExp(_polyfillHostContext + _parenSuffix, 'gim');
  7989. const _cssColonHostContextRe = new RegExp(_polyfillHostContext + _parenSuffix, 'im');
  7990. const _polyfillHostNoCombinator = _polyfillHost + '-no-combinator';
  7991. const _polyfillHostNoCombinatorRe = /-shadowcsshost-no-combinator([^\s]*)/;
  7992. const _shadowDOMSelectorsRe = [
  7993. /::shadow/g,
  7994. /::content/g,
  7995. // Deprecated selectors
  7996. /\/shadow-deep\//g,
  7997. /\/shadow\//g,
  7998. ];
  7999. // The deep combinator is deprecated in the CSS spec
  8000. // Support for `>>>`, `deep`, `::ng-deep` is then also deprecated and will be removed in the future.
  8001. // see https://github.com/angular/angular/pull/17677
  8002. const _shadowDeepSelectors = /(?:>>>)|(?:\/deep\/)|(?:::ng-deep)/g;
  8003. const _selectorReSuffix = '([>\\s~+[.,{:][\\s\\S]*)?$';
  8004. const _polyfillHostRe = /-shadowcsshost/gim;
  8005. const _colonHostRe = /:host/gim;
  8006. const _colonHostContextRe = /:host-context/gim;
  8007. const _commentRe = /\/\*[\s\S]*?\*\//g;
  8008. const _placeholderRe = /__ph-(\d+)__/g;
  8009. function stripComments(input) {
  8010. return input.replace(_commentRe, '');
  8011. }
  8012. const _commentWithHashRe = /\/\*\s*#\s*source(Mapping)?URL=[\s\S]+?\*\//g;
  8013. function extractCommentsWithHash(input) {
  8014. return input.match(_commentWithHashRe) || [];
  8015. }
  8016. const BLOCK_PLACEHOLDER = '%BLOCK%';
  8017. const _ruleRe = /(\s*)([^;\{\}]+?)(\s*)((?:{%BLOCK%}?\s*;?)|(?:\s*;))/g;
  8018. const CONTENT_PAIRS = new Map([['{', '}']]);
  8019. const COMMA_IN_PLACEHOLDER = '%COMMA_IN_PLACEHOLDER%';
  8020. const SEMI_IN_PLACEHOLDER = '%SEMI_IN_PLACEHOLDER%';
  8021. const COLON_IN_PLACEHOLDER = '%COLON_IN_PLACEHOLDER%';
  8022. const _cssCommaInPlaceholderReGlobal = new RegExp(COMMA_IN_PLACEHOLDER, 'g');
  8023. const _cssSemiInPlaceholderReGlobal = new RegExp(SEMI_IN_PLACEHOLDER, 'g');
  8024. const _cssColonInPlaceholderReGlobal = new RegExp(COLON_IN_PLACEHOLDER, 'g');
  8025. class CssRule {
  8026. constructor(selector, content) {
  8027. this.selector = selector;
  8028. this.content = content;
  8029. }
  8030. }
  8031. function processRules(input, ruleCallback) {
  8032. const escaped = escapeInStrings(input);
  8033. const inputWithEscapedBlocks = escapeBlocks(escaped, CONTENT_PAIRS, BLOCK_PLACEHOLDER);
  8034. let nextBlockIndex = 0;
  8035. const escapedResult = inputWithEscapedBlocks.escapedString.replace(_ruleRe, (...m) => {
  8036. const selector = m[2];
  8037. let content = '';
  8038. let suffix = m[4];
  8039. let contentPrefix = '';
  8040. if (suffix && suffix.startsWith('{' + BLOCK_PLACEHOLDER)) {
  8041. content = inputWithEscapedBlocks.blocks[nextBlockIndex++];
  8042. suffix = suffix.substring(BLOCK_PLACEHOLDER.length + 1);
  8043. contentPrefix = '{';
  8044. }
  8045. const rule = ruleCallback(new CssRule(selector, content));
  8046. return `${m[1]}${rule.selector}${m[3]}${contentPrefix}${rule.content}${suffix}`;
  8047. });
  8048. return unescapeInStrings(escapedResult);
  8049. }
  8050. class StringWithEscapedBlocks {
  8051. constructor(escapedString, blocks) {
  8052. this.escapedString = escapedString;
  8053. this.blocks = blocks;
  8054. }
  8055. }
  8056. function escapeBlocks(input, charPairs, placeholder) {
  8057. const resultParts = [];
  8058. const escapedBlocks = [];
  8059. let openCharCount = 0;
  8060. let nonBlockStartIndex = 0;
  8061. let blockStartIndex = -1;
  8062. let openChar;
  8063. let closeChar;
  8064. for (let i = 0; i < input.length; i++) {
  8065. const char = input[i];
  8066. if (char === '\\') {
  8067. i++;
  8068. }
  8069. else if (char === closeChar) {
  8070. openCharCount--;
  8071. if (openCharCount === 0) {
  8072. escapedBlocks.push(input.substring(blockStartIndex, i));
  8073. resultParts.push(placeholder);
  8074. nonBlockStartIndex = i;
  8075. blockStartIndex = -1;
  8076. openChar = closeChar = undefined;
  8077. }
  8078. }
  8079. else if (char === openChar) {
  8080. openCharCount++;
  8081. }
  8082. else if (openCharCount === 0 && charPairs.has(char)) {
  8083. openChar = char;
  8084. closeChar = charPairs.get(char);
  8085. openCharCount = 1;
  8086. blockStartIndex = i + 1;
  8087. resultParts.push(input.substring(nonBlockStartIndex, blockStartIndex));
  8088. }
  8089. }
  8090. if (blockStartIndex !== -1) {
  8091. escapedBlocks.push(input.substring(blockStartIndex));
  8092. resultParts.push(placeholder);
  8093. }
  8094. else {
  8095. resultParts.push(input.substring(nonBlockStartIndex));
  8096. }
  8097. return new StringWithEscapedBlocks(resultParts.join(''), escapedBlocks);
  8098. }
  8099. /**
  8100. * Object containing as keys characters that should be substituted by placeholders
  8101. * when found in strings during the css text parsing, and as values the respective
  8102. * placeholders
  8103. */
  8104. const ESCAPE_IN_STRING_MAP = {
  8105. ';': SEMI_IN_PLACEHOLDER,
  8106. ',': COMMA_IN_PLACEHOLDER,
  8107. ':': COLON_IN_PLACEHOLDER
  8108. };
  8109. /**
  8110. * Parse the provided css text and inside strings (meaning, inside pairs of unescaped single or
  8111. * double quotes) replace specific characters with their respective placeholders as indicated
  8112. * by the `ESCAPE_IN_STRING_MAP` map.
  8113. *
  8114. * For example convert the text
  8115. * `animation: "my-anim:at\"ion" 1s;`
  8116. * to
  8117. * `animation: "my-anim%COLON_IN_PLACEHOLDER%at\"ion" 1s;`
  8118. *
  8119. * This is necessary in order to remove the meaning of some characters when found inside strings
  8120. * (for example `;` indicates the end of a css declaration, `,` the sequence of values and `:` the
  8121. * division between property and value during a declaration, none of these meanings apply when such
  8122. * characters are within strings and so in order to prevent parsing issues they need to be replaced
  8123. * with placeholder text for the duration of the css manipulation process).
  8124. *
  8125. * @param input the original css text.
  8126. *
  8127. * @returns the css text with specific characters in strings replaced by placeholders.
  8128. **/
  8129. function escapeInStrings(input) {
  8130. let result = input;
  8131. let currentQuoteChar = null;
  8132. for (let i = 0; i < result.length; i++) {
  8133. const char = result[i];
  8134. if (char === '\\') {
  8135. i++;
  8136. }
  8137. else {
  8138. if (currentQuoteChar !== null) {
  8139. // index i is inside a quoted sub-string
  8140. if (char === currentQuoteChar) {
  8141. currentQuoteChar = null;
  8142. }
  8143. else {
  8144. const placeholder = ESCAPE_IN_STRING_MAP[char];
  8145. if (placeholder) {
  8146. result = `${result.substr(0, i)}${placeholder}${result.substr(i + 1)}`;
  8147. i += placeholder.length - 1;
  8148. }
  8149. }
  8150. }
  8151. else if (char === '\'' || char === '"') {
  8152. currentQuoteChar = char;
  8153. }
  8154. }
  8155. }
  8156. return result;
  8157. }
  8158. /**
  8159. * Replace in a string all occurrences of keys in the `ESCAPE_IN_STRING_MAP` map with their
  8160. * original representation, this is simply used to revert the changes applied by the
  8161. * escapeInStrings function.
  8162. *
  8163. * For example it reverts the text:
  8164. * `animation: "my-anim%COLON_IN_PLACEHOLDER%at\"ion" 1s;`
  8165. * to it's original form of:
  8166. * `animation: "my-anim:at\"ion" 1s;`
  8167. *
  8168. * Note: For the sake of simplicity this function does not check that the placeholders are
  8169. * actually inside strings as it would anyway be extremely unlikely to find them outside of strings.
  8170. *
  8171. * @param input the css text containing the placeholders.
  8172. *
  8173. * @returns the css text without the placeholders.
  8174. */
  8175. function unescapeInStrings(input) {
  8176. let result = input.replace(_cssCommaInPlaceholderReGlobal, ',');
  8177. result = result.replace(_cssSemiInPlaceholderReGlobal, ';');
  8178. result = result.replace(_cssColonInPlaceholderReGlobal, ':');
  8179. return result;
  8180. }
  8181. /**
  8182. * Unescape all quotes present in a string, but only if the string was actually already
  8183. * quoted.
  8184. *
  8185. * This generates a "canonical" representation of strings which can be used to match strings
  8186. * which would otherwise only differ because of differently escaped quotes.
  8187. *
  8188. * For example it converts the string (assumed to be quoted):
  8189. * `this \\"is\\" a \\'\\\\'test`
  8190. * to:
  8191. * `this "is" a '\\\\'test`
  8192. * (note that the latter backslashes are not removed as they are not actually escaping the single
  8193. * quote)
  8194. *
  8195. *
  8196. * @param input the string possibly containing escaped quotes.
  8197. * @param isQuoted boolean indicating whether the string was quoted inside a bigger string (if not
  8198. * then it means that it doesn't represent an inner string and thus no unescaping is required)
  8199. *
  8200. * @returns the string in the "canonical" representation without escaped quotes.
  8201. */
  8202. function unescapeQuotes(str, isQuoted) {
  8203. return !isQuoted ? str : str.replace(/((?:^|[^\\])(?:\\\\)*)\\(?=['"])/g, '$1');
  8204. }
  8205. /**
  8206. * Combine the `contextSelectors` with the `hostMarker` and the `otherSelectors`
  8207. * to create a selector that matches the same as `:host-context()`.
  8208. *
  8209. * Given a single context selector `A` we need to output selectors that match on the host and as an
  8210. * ancestor of the host:
  8211. *
  8212. * ```
  8213. * A <hostMarker>, A<hostMarker> {}
  8214. * ```
  8215. *
  8216. * When there is more than one context selector we also have to create combinations of those
  8217. * selectors with each other. For example if there are `A` and `B` selectors the output is:
  8218. *
  8219. * ```
  8220. * AB<hostMarker>, AB <hostMarker>, A B<hostMarker>,
  8221. * B A<hostMarker>, A B <hostMarker>, B A <hostMarker> {}
  8222. * ```
  8223. *
  8224. * And so on...
  8225. *
  8226. * @param hostMarker the string that selects the host element.
  8227. * @param contextSelectors an array of context selectors that will be combined.
  8228. * @param otherSelectors the rest of the selectors that are not context selectors.
  8229. */
  8230. function combineHostContextSelectors(contextSelectors, otherSelectors) {
  8231. const hostMarker = _polyfillHostNoCombinator;
  8232. _polyfillHostRe.lastIndex = 0; // reset the regex to ensure we get an accurate test
  8233. const otherSelectorsHasHost = _polyfillHostRe.test(otherSelectors);
  8234. // If there are no context selectors then just output a host marker
  8235. if (contextSelectors.length === 0) {
  8236. return hostMarker + otherSelectors;
  8237. }
  8238. const combined = [contextSelectors.pop() || ''];
  8239. while (contextSelectors.length > 0) {
  8240. const length = combined.length;
  8241. const contextSelector = contextSelectors.pop();
  8242. for (let i = 0; i < length; i++) {
  8243. const previousSelectors = combined[i];
  8244. // Add the new selector as a descendant of the previous selectors
  8245. combined[length * 2 + i] = previousSelectors + ' ' + contextSelector;
  8246. // Add the new selector as an ancestor of the previous selectors
  8247. combined[length + i] = contextSelector + ' ' + previousSelectors;
  8248. // Add the new selector to act on the same element as the previous selectors
  8249. combined[i] = contextSelector + previousSelectors;
  8250. }
  8251. }
  8252. // Finally connect the selector to the `hostMarker`s: either acting directly on the host
  8253. // (A<hostMarker>) or as an ancestor (A <hostMarker>).
  8254. return combined
  8255. .map(s => otherSelectorsHasHost ?
  8256. `${s}${otherSelectors}` :
  8257. `${s}${hostMarker}${otherSelectors}, ${s} ${hostMarker}${otherSelectors}`)
  8258. .join(',');
  8259. }
  8260. /**
  8261. * Mutate the given `groups` array so that there are `multiples` clones of the original array
  8262. * stored.
  8263. *
  8264. * For example `repeatGroups([a, b], 3)` will result in `[a, b, a, b, a, b]` - but importantly the
  8265. * newly added groups will be clones of the original.
  8266. *
  8267. * @param groups An array of groups of strings that will be repeated. This array is mutated
  8268. * in-place.
  8269. * @param multiples The number of times the current groups should appear.
  8270. */
  8271. function repeatGroups(groups, multiples) {
  8272. const length = groups.length;
  8273. for (let i = 1; i < multiples; i++) {
  8274. for (let j = 0; j < length; j++) {
  8275. groups[j + (i * length)] = groups[j].slice(0);
  8276. }
  8277. }
  8278. }
  8279. var TagContentType;
  8280. (function (TagContentType) {
  8281. TagContentType[TagContentType["RAW_TEXT"] = 0] = "RAW_TEXT";
  8282. TagContentType[TagContentType["ESCAPABLE_RAW_TEXT"] = 1] = "ESCAPABLE_RAW_TEXT";
  8283. TagContentType[TagContentType["PARSABLE_DATA"] = 2] = "PARSABLE_DATA";
  8284. })(TagContentType || (TagContentType = {}));
  8285. function splitNsName(elementName) {
  8286. if (elementName[0] != ':') {
  8287. return [null, elementName];
  8288. }
  8289. const colonIndex = elementName.indexOf(':', 1);
  8290. if (colonIndex === -1) {
  8291. throw new Error(`Unsupported format "${elementName}" expecting ":namespace:name"`);
  8292. }
  8293. return [elementName.slice(1, colonIndex), elementName.slice(colonIndex + 1)];
  8294. }
  8295. // `<ng-container>` tags work the same regardless the namespace
  8296. function isNgContainer(tagName) {
  8297. return splitNsName(tagName)[1] === 'ng-container';
  8298. }
  8299. // `<ng-content>` tags work the same regardless the namespace
  8300. function isNgContent(tagName) {
  8301. return splitNsName(tagName)[1] === 'ng-content';
  8302. }
  8303. // `<ng-template>` tags work the same regardless the namespace
  8304. function isNgTemplate(tagName) {
  8305. return splitNsName(tagName)[1] === 'ng-template';
  8306. }
  8307. function getNsPrefix(fullName) {
  8308. return fullName === null ? null : splitNsName(fullName)[0];
  8309. }
  8310. function mergeNsAndName(prefix, localName) {
  8311. return prefix ? `:${prefix}:${localName}` : localName;
  8312. }
  8313. /**
  8314. * Enumeration of the types of attributes which can be applied to an element.
  8315. */
  8316. var ElementAttributeKind;
  8317. (function (ElementAttributeKind) {
  8318. /**
  8319. * Static attributes.
  8320. */
  8321. ElementAttributeKind[ElementAttributeKind["Attribute"] = 0] = "Attribute";
  8322. /**
  8323. * Class bindings.
  8324. */
  8325. ElementAttributeKind[ElementAttributeKind["Class"] = 1] = "Class";
  8326. /**
  8327. * Style bindings.
  8328. */
  8329. ElementAttributeKind[ElementAttributeKind["Style"] = 2] = "Style";
  8330. /**
  8331. * Dynamic property or attribute bindings.
  8332. */
  8333. ElementAttributeKind[ElementAttributeKind["Binding"] = 3] = "Binding";
  8334. /**
  8335. * Attributes on a template node.
  8336. */
  8337. ElementAttributeKind[ElementAttributeKind["Template"] = 4] = "Template";
  8338. /**
  8339. * Internationalized attributes.
  8340. */
  8341. ElementAttributeKind[ElementAttributeKind["I18n"] = 5] = "I18n";
  8342. })(ElementAttributeKind || (ElementAttributeKind = {}));
  8343. const FLYWEIGHT_ARRAY = Object.freeze([]);
  8344. /**
  8345. * Container for all of the various kinds of attributes which are applied on an element.
  8346. */
  8347. class ElementAttributes {
  8348. constructor() {
  8349. this.known = new Set();
  8350. this.byKind = new Map;
  8351. this.projectAs = null;
  8352. }
  8353. get attributes() {
  8354. return this.byKind.get(ElementAttributeKind.Attribute) ?? FLYWEIGHT_ARRAY;
  8355. }
  8356. get classes() {
  8357. return this.byKind.get(ElementAttributeKind.Class) ?? FLYWEIGHT_ARRAY;
  8358. }
  8359. get styles() {
  8360. return this.byKind.get(ElementAttributeKind.Style) ?? FLYWEIGHT_ARRAY;
  8361. }
  8362. get bindings() {
  8363. return this.byKind.get(ElementAttributeKind.Binding) ?? FLYWEIGHT_ARRAY;
  8364. }
  8365. get template() {
  8366. return this.byKind.get(ElementAttributeKind.Template) ?? FLYWEIGHT_ARRAY;
  8367. }
  8368. get i18n() {
  8369. return this.byKind.get(ElementAttributeKind.I18n) ?? FLYWEIGHT_ARRAY;
  8370. }
  8371. add(kind, name, value) {
  8372. if (this.known.has(name)) {
  8373. return;
  8374. }
  8375. this.known.add(name);
  8376. const array = this.arrayFor(kind);
  8377. array.push(...getAttributeNameLiterals$1(name));
  8378. if (value !== null) {
  8379. array.push(value);
  8380. }
  8381. }
  8382. arrayFor(kind) {
  8383. if (!this.byKind.has(kind)) {
  8384. this.byKind.set(kind, []);
  8385. }
  8386. return this.byKind.get(kind);
  8387. }
  8388. }
  8389. function getAttributeNameLiterals$1(name) {
  8390. const [attributeNamespace, attributeName] = splitNsName(name);
  8391. const nameLiteral = literal(attributeName);
  8392. if (attributeNamespace) {
  8393. return [
  8394. literal(0 /* core.AttributeMarker.NamespaceURI */), literal(attributeNamespace), nameLiteral
  8395. ];
  8396. }
  8397. return [nameLiteral];
  8398. }
  8399. function assertIsElementAttributes(attrs) {
  8400. if (!(attrs instanceof ElementAttributes)) {
  8401. throw new Error(`AssertionError: ElementAttributes has already been coalesced into the view constants`);
  8402. }
  8403. }
  8404. /**
  8405. * Distinguishes different kinds of IR operations.
  8406. *
  8407. * Includes both creation and update operations.
  8408. */
  8409. var OpKind;
  8410. (function (OpKind) {
  8411. /**
  8412. * A special operation type which is used to represent the beginning and end nodes of a linked
  8413. * list of operations.
  8414. */
  8415. OpKind[OpKind["ListEnd"] = 0] = "ListEnd";
  8416. /**
  8417. * An operation which wraps an output AST statement.
  8418. */
  8419. OpKind[OpKind["Statement"] = 1] = "Statement";
  8420. /**
  8421. * An operation which declares and initializes a `SemanticVariable`.
  8422. */
  8423. OpKind[OpKind["Variable"] = 2] = "Variable";
  8424. /**
  8425. * An operation to begin rendering of an element.
  8426. */
  8427. OpKind[OpKind["ElementStart"] = 3] = "ElementStart";
  8428. /**
  8429. * An operation to render an element with no children.
  8430. */
  8431. OpKind[OpKind["Element"] = 4] = "Element";
  8432. /**
  8433. * An operation which declares an embedded view.
  8434. */
  8435. OpKind[OpKind["Template"] = 5] = "Template";
  8436. /**
  8437. * An operation to end rendering of an element previously started with `ElementStart`.
  8438. */
  8439. OpKind[OpKind["ElementEnd"] = 6] = "ElementEnd";
  8440. /**
  8441. * An operation to render a text node.
  8442. */
  8443. OpKind[OpKind["Text"] = 7] = "Text";
  8444. /**
  8445. * An operation declaring an event listener for an element.
  8446. */
  8447. OpKind[OpKind["Listener"] = 8] = "Listener";
  8448. /**
  8449. * An operation to interpolate text into a text node.
  8450. */
  8451. OpKind[OpKind["InterpolateText"] = 9] = "InterpolateText";
  8452. /**
  8453. * An operation to bind an expression to a property of an element.
  8454. */
  8455. OpKind[OpKind["Property"] = 10] = "Property";
  8456. /**
  8457. * An operation to advance the runtime's implicit slot context during the update phase of a view.
  8458. */
  8459. OpKind[OpKind["Advance"] = 11] = "Advance";
  8460. })(OpKind || (OpKind = {}));
  8461. /**
  8462. * Distinguishes different kinds of IR expressions.
  8463. */
  8464. var ExpressionKind;
  8465. (function (ExpressionKind) {
  8466. /**
  8467. * Read of a variable in a lexical scope.
  8468. */
  8469. ExpressionKind[ExpressionKind["LexicalRead"] = 0] = "LexicalRead";
  8470. /**
  8471. * A reference to the current view context.
  8472. */
  8473. ExpressionKind[ExpressionKind["Context"] = 1] = "Context";
  8474. /**
  8475. * Read of a variable declared in a `VariableOp`.
  8476. */
  8477. ExpressionKind[ExpressionKind["ReadVariable"] = 2] = "ReadVariable";
  8478. /**
  8479. * Runtime operation to navigate to the next view context in the view hierarchy.
  8480. */
  8481. ExpressionKind[ExpressionKind["NextContext"] = 3] = "NextContext";
  8482. /**
  8483. * Runtime operation to retrieve the value of a local reference.
  8484. */
  8485. ExpressionKind[ExpressionKind["Reference"] = 4] = "Reference";
  8486. /**
  8487. * Runtime operation to snapshot the current view context.
  8488. */
  8489. ExpressionKind[ExpressionKind["GetCurrentView"] = 5] = "GetCurrentView";
  8490. /**
  8491. * Runtime operation to restore a snapshotted view.
  8492. */
  8493. ExpressionKind[ExpressionKind["RestoreView"] = 6] = "RestoreView";
  8494. /**
  8495. * Runtime operation to reset the current view context after `RestoreView`.
  8496. */
  8497. ExpressionKind[ExpressionKind["ResetView"] = 7] = "ResetView";
  8498. })(ExpressionKind || (ExpressionKind = {}));
  8499. /**
  8500. * Distinguishes between different kinds of `SemanticVariable`s.
  8501. */
  8502. var SemanticVariableKind;
  8503. (function (SemanticVariableKind) {
  8504. /**
  8505. * Represents the context of a particular view.
  8506. */
  8507. SemanticVariableKind[SemanticVariableKind["Context"] = 0] = "Context";
  8508. /**
  8509. * Represents an identifier declared in the lexical scope of a view.
  8510. */
  8511. SemanticVariableKind[SemanticVariableKind["Identifier"] = 1] = "Identifier";
  8512. /**
  8513. * Represents a saved state that can be used to restore a view in a listener handler function.
  8514. */
  8515. SemanticVariableKind[SemanticVariableKind["SavedView"] = 2] = "SavedView";
  8516. })(SemanticVariableKind || (SemanticVariableKind = {}));
  8517. /**
  8518. * Marker symbol for `ConsumesSlotOpTrait`.
  8519. */
  8520. const ConsumesSlot = Symbol('ConsumesSlot');
  8521. /**
  8522. * Marker symbol for `DependsOnSlotContextOpTrait`.
  8523. */
  8524. const DependsOnSlotContext = Symbol('DependsOnSlotContext');
  8525. /**
  8526. * Marker symbol for `UsesSlotIndex` trait.
  8527. */
  8528. const UsesSlotIndex = Symbol('UsesSlotIndex');
  8529. /**
  8530. * Marker symbol for `ConsumesVars` trait.
  8531. */
  8532. const ConsumesVarsTrait = Symbol('UsesVars');
  8533. /**
  8534. * Default values for most `ConsumesSlotOpTrait` fields (used with the spread operator to initialize
  8535. * implementors of the trait).
  8536. */
  8537. const TRAIT_CONSUMES_SLOT = {
  8538. [ConsumesSlot]: true,
  8539. slot: null,
  8540. numSlotsUsed: 1,
  8541. };
  8542. /**
  8543. * Default values for most `UsesSlotIndexTrait` fields (used with the spread operator to initialize
  8544. * implementors of the trait).
  8545. */
  8546. const TRAIT_USES_SLOT_INDEX = {
  8547. [UsesSlotIndex]: true,
  8548. slot: null,
  8549. };
  8550. /**
  8551. * Default values for most `DependsOnSlotContextOpTrait` fields (used with the spread operator to
  8552. * initialize implementors of the trait).
  8553. */
  8554. const TRAIT_DEPENDS_ON_SLOT_CONTEXT = {
  8555. [DependsOnSlotContext]: true,
  8556. };
  8557. /**
  8558. * Default values for `UsesVars` fields (used with the spread operator to initialize
  8559. * implementors of the trait).
  8560. */
  8561. const TRAIT_CONSUMES_VARS = {
  8562. [ConsumesVarsTrait]: true,
  8563. };
  8564. /**
  8565. * Test whether an operation implements `ConsumesSlotOpTrait`.
  8566. */
  8567. function hasConsumesSlotTrait(op) {
  8568. return op[ConsumesSlot] === true;
  8569. }
  8570. /**
  8571. * Test whether an operation implements `DependsOnSlotContextOpTrait`.
  8572. */
  8573. function hasDependsOnSlotContextTrait(op) {
  8574. return op[DependsOnSlotContext] === true;
  8575. }
  8576. function hasConsumesVarsTrait(value) {
  8577. return value[ConsumesVarsTrait] === true;
  8578. }
  8579. function hasUsesSlotIndexTrait(value) {
  8580. return value[UsesSlotIndex] === true;
  8581. }
  8582. var _a;
  8583. /**
  8584. * Check whether a given `o.Expression` is a logical IR expression type.
  8585. */
  8586. function isIrExpression(expr) {
  8587. return expr instanceof ExpressionBase;
  8588. }
  8589. /**
  8590. * Base type used for all logical IR expressions.
  8591. */
  8592. class ExpressionBase extends Expression {
  8593. constructor(sourceSpan = null) {
  8594. super(null, sourceSpan);
  8595. }
  8596. }
  8597. /**
  8598. * Logical expression representing a lexical read of a variable name.
  8599. */
  8600. class LexicalReadExpr extends ExpressionBase {
  8601. constructor(name) {
  8602. super();
  8603. this.name = name;
  8604. this.kind = ExpressionKind.LexicalRead;
  8605. }
  8606. visitExpression(visitor, context) { }
  8607. isEquivalent() {
  8608. return false;
  8609. }
  8610. isConstant() {
  8611. return false;
  8612. }
  8613. transformInternalExpressions() { }
  8614. }
  8615. /**
  8616. * Runtime operation to retrieve the value of a local reference.
  8617. */
  8618. class ReferenceExpr extends ExpressionBase {
  8619. static { _a = UsesSlotIndex; }
  8620. constructor(target, offset) {
  8621. super();
  8622. this.target = target;
  8623. this.offset = offset;
  8624. this.kind = ExpressionKind.Reference;
  8625. this[_a] = true;
  8626. this.slot = null;
  8627. }
  8628. visitExpression() { }
  8629. isEquivalent(e) {
  8630. return e instanceof ReferenceExpr && e.target === this.target;
  8631. }
  8632. isConstant() {
  8633. return false;
  8634. }
  8635. transformInternalExpressions() { }
  8636. }
  8637. /**
  8638. * A reference to the current view context (usually the `ctx` variable in a template function).
  8639. */
  8640. class ContextExpr extends ExpressionBase {
  8641. constructor(view) {
  8642. super();
  8643. this.view = view;
  8644. this.kind = ExpressionKind.Context;
  8645. }
  8646. visitExpression() { }
  8647. isEquivalent(e) {
  8648. return e instanceof ContextExpr && e.view === this.view;
  8649. }
  8650. isConstant() {
  8651. return false;
  8652. }
  8653. transformInternalExpressions() { }
  8654. }
  8655. /**
  8656. * Runtime operation to navigate to the next view context in the view hierarchy.
  8657. */
  8658. class NextContextExpr extends ExpressionBase {
  8659. constructor() {
  8660. super();
  8661. this.kind = ExpressionKind.NextContext;
  8662. this.steps = 1;
  8663. }
  8664. visitExpression() { }
  8665. isEquivalent(e) {
  8666. return e instanceof NextContextExpr && e.steps === this.steps;
  8667. }
  8668. isConstant() {
  8669. return false;
  8670. }
  8671. transformInternalExpressions() { }
  8672. }
  8673. /**
  8674. * Runtime operation to snapshot the current view context.
  8675. *
  8676. * The result of this operation can be stored in a variable and later used with the `RestoreView`
  8677. * operation.
  8678. */
  8679. class GetCurrentViewExpr extends ExpressionBase {
  8680. constructor() {
  8681. super();
  8682. this.kind = ExpressionKind.GetCurrentView;
  8683. }
  8684. visitExpression() { }
  8685. isEquivalent(e) {
  8686. return e instanceof GetCurrentViewExpr;
  8687. }
  8688. isConstant() {
  8689. return false;
  8690. }
  8691. transformInternalExpressions() { }
  8692. }
  8693. /**
  8694. * Runtime operation to restore a snapshotted view.
  8695. */
  8696. class RestoreViewExpr extends ExpressionBase {
  8697. constructor(view) {
  8698. super();
  8699. this.view = view;
  8700. this.kind = ExpressionKind.RestoreView;
  8701. }
  8702. visitExpression(visitor, context) {
  8703. if (typeof this.view !== 'number') {
  8704. this.view.visitExpression(visitor, context);
  8705. }
  8706. }
  8707. isEquivalent(e) {
  8708. if (!(e instanceof RestoreViewExpr) || typeof e.view !== typeof this.view) {
  8709. return false;
  8710. }
  8711. if (typeof this.view === 'number') {
  8712. return this.view === e.view;
  8713. }
  8714. else {
  8715. return this.view.isEquivalent(e.view);
  8716. }
  8717. }
  8718. isConstant() {
  8719. return false;
  8720. }
  8721. transformInternalExpressions(transform, flags) {
  8722. if (typeof this.view !== 'number') {
  8723. this.view = transformExpressionsInExpression(this.view, transform, flags);
  8724. }
  8725. }
  8726. }
  8727. /**
  8728. * Runtime operation to reset the current view context after `RestoreView`.
  8729. */
  8730. class ResetViewExpr extends ExpressionBase {
  8731. constructor(expr) {
  8732. super();
  8733. this.expr = expr;
  8734. this.kind = ExpressionKind.ResetView;
  8735. }
  8736. visitExpression(visitor, context) {
  8737. this.expr.visitExpression(visitor, context);
  8738. }
  8739. isEquivalent(e) {
  8740. return e instanceof ResetViewExpr && this.expr.isEquivalent(e.expr);
  8741. }
  8742. isConstant() {
  8743. return false;
  8744. }
  8745. transformInternalExpressions(transform, flags) {
  8746. this.expr = transformExpressionsInExpression(this.expr, transform, flags);
  8747. }
  8748. }
  8749. /**
  8750. * Read of a variable declared as an `ir.VariableOp` and referenced through its `ir.XrefId`.
  8751. */
  8752. class ReadVariableExpr extends ExpressionBase {
  8753. constructor(xref) {
  8754. super();
  8755. this.xref = xref;
  8756. this.kind = ExpressionKind.ReadVariable;
  8757. this.name = null;
  8758. }
  8759. visitExpression() { }
  8760. isEquivalent(other) {
  8761. return other instanceof ReadVariableExpr && other.xref === this.xref;
  8762. }
  8763. isConstant() {
  8764. return false;
  8765. }
  8766. transformInternalExpressions() { }
  8767. }
  8768. /**
  8769. * Visits all `Expression`s in the AST of `op` with the `visitor` function.
  8770. */
  8771. function visitExpressionsInOp(op, visitor) {
  8772. transformExpressionsInOp(op, (expr, flags) => {
  8773. visitor(expr, flags);
  8774. return expr;
  8775. }, VisitorContextFlag.None);
  8776. }
  8777. var VisitorContextFlag;
  8778. (function (VisitorContextFlag) {
  8779. VisitorContextFlag[VisitorContextFlag["None"] = 0] = "None";
  8780. VisitorContextFlag[VisitorContextFlag["InChildOperation"] = 1] = "InChildOperation";
  8781. })(VisitorContextFlag || (VisitorContextFlag = {}));
  8782. /**
  8783. * Transform all `Expression`s in the AST of `op` with the `transform` function.
  8784. *
  8785. * All such operations will be replaced with the result of applying `transform`, which may be an
  8786. * identity transformation.
  8787. */
  8788. function transformExpressionsInOp(op, transform, flags) {
  8789. switch (op.kind) {
  8790. case OpKind.Property:
  8791. op.expression = transformExpressionsInExpression(op.expression, transform, flags);
  8792. break;
  8793. case OpKind.Statement:
  8794. transformExpressionsInStatement(op.statement, transform, flags);
  8795. break;
  8796. case OpKind.Variable:
  8797. op.initializer = transformExpressionsInExpression(op.initializer, transform, flags);
  8798. break;
  8799. case OpKind.InterpolateText:
  8800. for (let i = 0; i < op.expressions.length; i++) {
  8801. op.expressions[i] = transformExpressionsInExpression(op.expressions[i], transform, flags);
  8802. }
  8803. break;
  8804. case OpKind.Listener:
  8805. for (const innerOp of op.handlerOps) {
  8806. transformExpressionsInOp(innerOp, transform, flags | VisitorContextFlag.InChildOperation);
  8807. }
  8808. break;
  8809. case OpKind.Element:
  8810. case OpKind.ElementStart:
  8811. case OpKind.ElementEnd:
  8812. case OpKind.Template:
  8813. case OpKind.Text:
  8814. case OpKind.Advance:
  8815. // These operations contain no expressions.
  8816. break;
  8817. default:
  8818. throw new Error(`AssertionError: transformExpressionsInOp doesn't handle ${OpKind[op.kind]}`);
  8819. }
  8820. }
  8821. /**
  8822. * Transform all `Expression`s in the AST of `expr` with the `transform` function.
  8823. *
  8824. * All such operations will be replaced with the result of applying `transform`, which may be an
  8825. * identity transformation.
  8826. */
  8827. function transformExpressionsInExpression(expr, transform, flags) {
  8828. if (expr instanceof ExpressionBase) {
  8829. expr.transformInternalExpressions(transform, flags);
  8830. return transform(expr, flags);
  8831. }
  8832. else if (expr instanceof BinaryOperatorExpr) {
  8833. expr.lhs = transformExpressionsInExpression(expr.lhs, transform, flags);
  8834. expr.rhs = transformExpressionsInExpression(expr.rhs, transform, flags);
  8835. }
  8836. else if (expr instanceof ReadPropExpr) {
  8837. expr.receiver = transformExpressionsInExpression(expr.receiver, transform, flags);
  8838. }
  8839. else if (expr instanceof InvokeFunctionExpr) {
  8840. expr.fn = transformExpressionsInExpression(expr.fn, transform, flags);
  8841. for (let i = 0; i < expr.args.length; i++) {
  8842. expr.args[i] = transformExpressionsInExpression(expr.args[i], transform, flags);
  8843. }
  8844. }
  8845. else if (expr instanceof ReadVarExpr || expr instanceof ExternalExpr ||
  8846. expr instanceof LiteralExpr) {
  8847. // No action for these types.
  8848. }
  8849. else {
  8850. throw new Error(`Unhandled expression kind: ${expr.constructor.name}`);
  8851. }
  8852. return expr;
  8853. }
  8854. /**
  8855. * Transform all `Expression`s in the AST of `stmt` with the `transform` function.
  8856. *
  8857. * All such operations will be replaced with the result of applying `transform`, which may be an
  8858. * identity transformation.
  8859. */
  8860. function transformExpressionsInStatement(stmt, transform, flags) {
  8861. if (stmt instanceof ExpressionStatement) {
  8862. stmt.expr = transformExpressionsInExpression(stmt.expr, transform, flags);
  8863. }
  8864. else if (stmt instanceof ReturnStatement) {
  8865. stmt.value = transformExpressionsInExpression(stmt.value, transform, flags);
  8866. }
  8867. else {
  8868. throw new Error(`Unhandled statement kind: ${stmt.constructor.name}`);
  8869. }
  8870. }
  8871. /**
  8872. * A linked list of `Op` nodes of a given subtype.
  8873. *
  8874. * @param OpT specific subtype of `Op` nodes which this list contains.
  8875. */
  8876. class OpList {
  8877. static { this.nextListId = 0; }
  8878. constructor() {
  8879. /**
  8880. * Debug ID of this `OpList` instance.
  8881. */
  8882. this.debugListId = OpList.nextListId++;
  8883. // OpList uses static head/tail nodes of a special `ListEnd` type.
  8884. // This avoids the need for special casing of the first and last list
  8885. // elements in all list operations.
  8886. this.head = {
  8887. kind: OpKind.ListEnd,
  8888. next: null,
  8889. prev: null,
  8890. debugListId: this.debugListId,
  8891. };
  8892. this.tail = {
  8893. kind: OpKind.ListEnd,
  8894. next: null,
  8895. prev: null,
  8896. debugListId: this.debugListId,
  8897. };
  8898. // Link `head` and `tail` together at the start (list is empty).
  8899. this.head.next = this.tail;
  8900. this.tail.prev = this.head;
  8901. }
  8902. /**
  8903. * Push a new operation to the tail of the list.
  8904. */
  8905. push(op) {
  8906. OpList.assertIsNotEnd(op);
  8907. OpList.assertIsUnowned(op);
  8908. op.debugListId = this.debugListId;
  8909. // The old "previous" node (which might be the head, if the list is empty).
  8910. const oldLast = this.tail.prev;
  8911. // Insert `op` following the old last node.
  8912. op.prev = oldLast;
  8913. oldLast.next = op;
  8914. // Connect `op` with the list tail.
  8915. op.next = this.tail;
  8916. this.tail.prev = op;
  8917. }
  8918. /**
  8919. * Prepend one or more nodes to the start of the list.
  8920. */
  8921. prepend(ops) {
  8922. if (ops.length === 0) {
  8923. return;
  8924. }
  8925. for (const op of ops) {
  8926. OpList.assertIsNotEnd(op);
  8927. OpList.assertIsUnowned(op);
  8928. op.debugListId = this.debugListId;
  8929. }
  8930. const first = this.head.next;
  8931. let prev = this.head;
  8932. for (const op of ops) {
  8933. prev.next = op;
  8934. op.prev = prev;
  8935. prev = op;
  8936. }
  8937. prev.next = first;
  8938. first.prev = prev;
  8939. }
  8940. /**
  8941. * `OpList` is iterable via the iteration protocol.
  8942. *
  8943. * It's safe to mutate the part of the list that has already been returned by the iterator, up to
  8944. * and including the last operation returned. Mutations beyond that point _may_ be safe, but may
  8945. * also corrupt the iteration position and should be avoided.
  8946. */
  8947. *[Symbol.iterator]() {
  8948. let current = this.head.next;
  8949. while (current !== this.tail) {
  8950. // Guards against corruption of the iterator state by mutations to the tail of the list during
  8951. // iteration.
  8952. OpList.assertIsOwned(current, this.debugListId);
  8953. const next = current.next;
  8954. yield current;
  8955. current = next;
  8956. }
  8957. }
  8958. *reversed() {
  8959. let current = this.tail.prev;
  8960. while (current !== this.head) {
  8961. OpList.assertIsOwned(current, this.debugListId);
  8962. const prev = current.prev;
  8963. yield current;
  8964. current = prev;
  8965. }
  8966. }
  8967. /**
  8968. * Replace `oldOp` with `newOp` in the list.
  8969. */
  8970. static replace(oldOp, newOp) {
  8971. OpList.assertIsNotEnd(oldOp);
  8972. OpList.assertIsNotEnd(newOp);
  8973. OpList.assertIsOwned(oldOp);
  8974. OpList.assertIsUnowned(newOp);
  8975. newOp.debugListId = oldOp.debugListId;
  8976. if (oldOp.prev !== null) {
  8977. oldOp.prev.next = newOp;
  8978. newOp.prev = oldOp.prev;
  8979. }
  8980. if (oldOp.next !== null) {
  8981. oldOp.next.prev = newOp;
  8982. newOp.next = oldOp.next;
  8983. }
  8984. oldOp.debugListId = null;
  8985. oldOp.prev = null;
  8986. oldOp.next = null;
  8987. }
  8988. /**
  8989. * Replace `oldOp` with some number of new operations in the list (which may include `oldOp`).
  8990. */
  8991. static replaceWithMany(oldOp, newOps) {
  8992. if (newOps.length === 0) {
  8993. // Replacing with an empty list -> pure removal.
  8994. OpList.remove(oldOp);
  8995. return;
  8996. }
  8997. OpList.assertIsNotEnd(oldOp);
  8998. OpList.assertIsOwned(oldOp);
  8999. const listId = oldOp.debugListId;
  9000. oldOp.debugListId = null;
  9001. for (const newOp of newOps) {
  9002. OpList.assertIsNotEnd(newOp);
  9003. // `newOp` might be `oldOp`, but at this point it's been marked as unowned.
  9004. OpList.assertIsUnowned(newOp);
  9005. }
  9006. // It should be safe to reuse `oldOp` in the `newOps` list - maybe you want to sandwich an
  9007. // operation between two new ops.
  9008. const { prev: oldPrev, next: oldNext } = oldOp;
  9009. oldOp.prev = null;
  9010. oldOp.next = null;
  9011. let prev = oldPrev;
  9012. for (const newOp of newOps) {
  9013. this.assertIsUnowned(newOp);
  9014. newOp.debugListId = listId;
  9015. prev.next = newOp;
  9016. newOp.prev = prev;
  9017. // This _should_ be the case, but set it just in case.
  9018. newOp.next = null;
  9019. prev = newOp;
  9020. }
  9021. // At the end of iteration, `prev` holds the last node in the list.
  9022. const first = newOps[0];
  9023. const last = prev;
  9024. // Replace `oldOp` with the chain `first` -> `last`.
  9025. if (oldPrev !== null) {
  9026. oldPrev.next = first;
  9027. first.prev = oldOp.prev;
  9028. }
  9029. if (oldNext !== null) {
  9030. oldNext.prev = last;
  9031. last.next = oldNext;
  9032. }
  9033. }
  9034. /**
  9035. * Remove the given node from the list which contains it.
  9036. */
  9037. static remove(op) {
  9038. OpList.assertIsNotEnd(op);
  9039. OpList.assertIsOwned(op);
  9040. op.prev.next = op.next;
  9041. op.next.prev = op.prev;
  9042. // Break any link between the node and this list to safeguard against its usage in future
  9043. // operations.
  9044. op.debugListId = null;
  9045. op.prev = null;
  9046. op.next = null;
  9047. }
  9048. /**
  9049. * Insert `op` before `before`.
  9050. */
  9051. static insertBefore(op, before) {
  9052. OpList.assertIsNotEnd(before);
  9053. OpList.assertIsNotEnd(op);
  9054. OpList.assertIsUnowned(op);
  9055. OpList.assertIsOwned(before);
  9056. op.debugListId = before.debugListId;
  9057. // Just in case.
  9058. op.prev = null;
  9059. before.prev.next = op;
  9060. op.prev = before.prev;
  9061. op.next = before;
  9062. before.prev = op;
  9063. }
  9064. /**
  9065. * Asserts that `op` does not currently belong to a list.
  9066. */
  9067. static assertIsUnowned(op) {
  9068. if (op.debugListId !== null) {
  9069. throw new Error(`AssertionError: illegal operation on owned node: ${OpKind[op.kind]}`);
  9070. }
  9071. }
  9072. /**
  9073. * Asserts that `op` currently belongs to a list. If `byList` is passed, `op` is asserted to
  9074. * specifically belong to that list.
  9075. */
  9076. static assertIsOwned(op, byList) {
  9077. if (op.debugListId === null) {
  9078. throw new Error(`AssertionError: illegal operation on unowned node: ${OpKind[op.kind]}`);
  9079. }
  9080. else if (byList !== undefined && op.debugListId !== byList) {
  9081. throw new Error(`AssertionError: node belongs to the wrong list (expected ${byList}, actual ${op.debugListId})`);
  9082. }
  9083. }
  9084. /**
  9085. * Asserts that `op` is not a special `ListEnd` node.
  9086. */
  9087. static assertIsNotEnd(op) {
  9088. if (op.kind === OpKind.ListEnd) {
  9089. throw new Error(`AssertionError: illegal operation on list head or tail`);
  9090. }
  9091. }
  9092. }
  9093. /**
  9094. * Create a `StatementOp`.
  9095. */
  9096. function createStatementOp(statement) {
  9097. return {
  9098. kind: OpKind.Statement,
  9099. statement,
  9100. ...NEW_OP,
  9101. };
  9102. }
  9103. /**
  9104. * Create a `VariableOp`.
  9105. */
  9106. function createVariableOp(xref, variable, initializer) {
  9107. return {
  9108. kind: OpKind.Variable,
  9109. xref,
  9110. variable,
  9111. initializer,
  9112. ...NEW_OP,
  9113. };
  9114. }
  9115. /**
  9116. * Static structure shared by all operations.
  9117. *
  9118. * Used as a convenience via the spread operator (`...NEW_OP`) when creating new operations, and
  9119. * ensures the fields are always in the same order.
  9120. */
  9121. const NEW_OP = {
  9122. debugListId: null,
  9123. prev: null,
  9124. next: null,
  9125. };
  9126. /**
  9127. * Create an `ElementStartOp`.
  9128. */
  9129. function createElementStartOp(tag, xref) {
  9130. return {
  9131. kind: OpKind.ElementStart,
  9132. xref,
  9133. tag,
  9134. attributes: new ElementAttributes(),
  9135. localRefs: [],
  9136. ...TRAIT_CONSUMES_SLOT,
  9137. ...NEW_OP,
  9138. };
  9139. }
  9140. /**
  9141. * Create a `TemplateOp`.
  9142. */
  9143. function createTemplateOp(xref, tag) {
  9144. return {
  9145. kind: OpKind.Template,
  9146. xref,
  9147. attributes: new ElementAttributes(),
  9148. tag,
  9149. decls: null,
  9150. vars: null,
  9151. localRefs: [],
  9152. ...TRAIT_CONSUMES_SLOT,
  9153. ...NEW_OP,
  9154. };
  9155. }
  9156. /**
  9157. * Create an `ElementEndOp`.
  9158. */
  9159. function createElementEndOp(xref) {
  9160. return {
  9161. kind: OpKind.ElementEnd,
  9162. xref,
  9163. ...NEW_OP,
  9164. };
  9165. }
  9166. /**
  9167. * Create a `TextOp`.
  9168. */
  9169. function createTextOp(xref, initialValue) {
  9170. return {
  9171. kind: OpKind.Text,
  9172. xref,
  9173. initialValue,
  9174. ...TRAIT_CONSUMES_SLOT,
  9175. ...NEW_OP,
  9176. };
  9177. }
  9178. /**
  9179. * Create a `ListenerOp`.
  9180. */
  9181. function createListenerOp(target, name, tag) {
  9182. return {
  9183. kind: OpKind.Listener,
  9184. target,
  9185. tag,
  9186. name,
  9187. handlerOps: new OpList(),
  9188. handlerFnName: null,
  9189. ...NEW_OP,
  9190. ...TRAIT_USES_SLOT_INDEX,
  9191. };
  9192. }
  9193. /**
  9194. * Create an `InterpolationTextOp`.
  9195. */
  9196. function createInterpolateTextOp(xref, strings, expressions) {
  9197. return {
  9198. kind: OpKind.InterpolateText,
  9199. target: xref,
  9200. strings,
  9201. expressions,
  9202. ...TRAIT_DEPENDS_ON_SLOT_CONTEXT,
  9203. ...TRAIT_CONSUMES_VARS,
  9204. ...NEW_OP,
  9205. };
  9206. }
  9207. /**
  9208. * Create a `PropertyOp`.
  9209. */
  9210. function createPropertyOp(xref, name, expression) {
  9211. return {
  9212. kind: OpKind.Property,
  9213. target: xref,
  9214. name,
  9215. expression,
  9216. ...TRAIT_DEPENDS_ON_SLOT_CONTEXT,
  9217. ...TRAIT_CONSUMES_VARS,
  9218. ...NEW_OP,
  9219. };
  9220. }
  9221. /**
  9222. * Create an `AdvanceOp`.
  9223. */
  9224. function createAdvanceOp(delta) {
  9225. return {
  9226. kind: OpKind.Advance,
  9227. delta,
  9228. ...NEW_OP,
  9229. };
  9230. }
  9231. /**
  9232. * Converts the semantic attributes of element-like operations (elements, templates) into constant
  9233. * array expressions, and lifts them into the overall component `consts`.
  9234. */
  9235. function phaseConstCollection(cpl) {
  9236. for (const [_, view] of cpl.views) {
  9237. for (const op of view.create) {
  9238. if (op.kind !== OpKind.ElementStart && op.kind !== OpKind.Element &&
  9239. op.kind !== OpKind.Template) {
  9240. continue;
  9241. }
  9242. else if (!(op.attributes instanceof ElementAttributes)) {
  9243. continue;
  9244. }
  9245. const attrArray = serializeAttributes(op.attributes);
  9246. if (attrArray.entries.length > 0) {
  9247. op.attributes = cpl.addConst(attrArray);
  9248. }
  9249. else {
  9250. op.attributes = null;
  9251. }
  9252. }
  9253. }
  9254. }
  9255. function serializeAttributes({ attributes, bindings, classes, i18n, projectAs, styles, template }) {
  9256. const attrArray = [...attributes];
  9257. if (projectAs !== null) {
  9258. attrArray.push(literal(5 /* core.AttributeMarker.ProjectAs */), literal(projectAs));
  9259. }
  9260. if (classes.length > 0) {
  9261. attrArray.push(literal(1 /* core.AttributeMarker.Classes */), ...classes);
  9262. }
  9263. if (styles.length > 0) {
  9264. attrArray.push(literal(2 /* core.AttributeMarker.Styles */), ...styles);
  9265. }
  9266. if (bindings.length > 0) {
  9267. attrArray.push(literal(3 /* core.AttributeMarker.Bindings */), ...bindings);
  9268. }
  9269. if (template.length > 0) {
  9270. attrArray.push(literal(4 /* core.AttributeMarker.Template */), ...template);
  9271. }
  9272. if (i18n.length > 0) {
  9273. attrArray.push(literal(6 /* core.AttributeMarker.I18n */), ...i18n);
  9274. }
  9275. return literalArr(attrArray);
  9276. }
  9277. /**
  9278. * Replace sequences of `ElementStart` followed by `ElementEnd` with a condensed `Element`
  9279. * instruction.
  9280. */
  9281. function phaseEmptyElements(cpl) {
  9282. for (const [_, view] of cpl.views) {
  9283. for (const op of view.create) {
  9284. if (op.kind === OpKind.ElementEnd && op.prev !== null &&
  9285. op.prev.kind === OpKind.ElementStart) {
  9286. // Transmute the `ElementStart` instruction to `Element`. This is safe as they're designed
  9287. // to be identical apart from the `kind`.
  9288. op.prev.kind = OpKind.Element;
  9289. // Remove the `ElementEnd` instruction.
  9290. OpList.remove(op);
  9291. }
  9292. }
  9293. }
  9294. }
  9295. /**
  9296. * Generate `ir.AdvanceOp`s in between `ir.UpdateOp`s that ensure the runtime's implicit slot
  9297. * context will be advanced correctly.
  9298. */
  9299. function phaseGenerateAdvance(cpl) {
  9300. for (const [_, view] of cpl.views) {
  9301. // First build a map of all of the declarations in the view that have assigned slots.
  9302. const slotMap = new Map();
  9303. for (const op of view.create) {
  9304. if (!hasConsumesSlotTrait(op)) {
  9305. continue;
  9306. }
  9307. else if (op.slot === null) {
  9308. throw new Error(`AssertionError: expected slots to have been allocated before generating advance() calls`);
  9309. }
  9310. slotMap.set(op.xref, op.slot);
  9311. }
  9312. // Next, step through the update operations and generate `ir.AdvanceOp`s as required to ensure
  9313. // the runtime's implicit slot counter will be set to the correct slot before executing each
  9314. // update operation which depends on it.
  9315. //
  9316. // To do that, we track what the runtime's slot counter will be through the update operations.
  9317. let slotContext = 0;
  9318. for (const op of view.update) {
  9319. if (!hasDependsOnSlotContextTrait(op)) {
  9320. // `op` doesn't depend on the slot counter, so it can be skipped.
  9321. continue;
  9322. }
  9323. else if (!slotMap.has(op.target)) {
  9324. // We expect ops that _do_ depend on the slot counter to point at declarations that exist in
  9325. // the `slotMap`.
  9326. throw new Error(`AssertionError: reference to unknown slot for var ${op.target}`);
  9327. }
  9328. const slot = slotMap.get(op.target);
  9329. // Does the slot counter need to be adjusted?
  9330. if (slotContext !== slot) {
  9331. // If so, generate an `ir.AdvanceOp` to advance the counter.
  9332. const delta = slot - slotContext;
  9333. if (delta < 0) {
  9334. throw new Error(`AssertionError: slot counter should never need to move backwards`);
  9335. }
  9336. OpList.insertBefore(createAdvanceOp(delta), op);
  9337. slotContext = slot;
  9338. }
  9339. }
  9340. }
  9341. }
  9342. // This file contains helpers for generating calls to Ivy instructions. In particular, each
  9343. // instruction type is represented as a function, which may select a specific instruction variant
  9344. // depending on the exact arguments.
  9345. function element(slot, tag, constIndex, localRefIndex) {
  9346. return elementStartBase(Identifiers.element, slot, tag, constIndex, localRefIndex);
  9347. }
  9348. function elementStart(slot, tag, constIndex, localRefIndex) {
  9349. return elementStartBase(Identifiers.elementStart, slot, tag, constIndex, localRefIndex);
  9350. }
  9351. function elementStartBase(instruction, slot, tag, constIndex, localRefIndex) {
  9352. const args = [
  9353. literal(slot),
  9354. literal(tag),
  9355. ];
  9356. if (localRefIndex !== null) {
  9357. args.push(literal(constIndex), // might be null, but that's okay.
  9358. literal(localRefIndex));
  9359. }
  9360. else if (constIndex !== null) {
  9361. args.push(literal(constIndex));
  9362. }
  9363. return call(instruction, args);
  9364. }
  9365. function elementEnd() {
  9366. return call(Identifiers.elementEnd, []);
  9367. }
  9368. function template(slot, templateFnRef, decls, vars, tag, constIndex) {
  9369. return call(Identifiers.templateCreate, [
  9370. literal(slot),
  9371. templateFnRef,
  9372. literal(decls),
  9373. literal(vars),
  9374. literal(tag),
  9375. literal(constIndex),
  9376. ]);
  9377. }
  9378. function listener(name, handlerFn) {
  9379. return call(Identifiers.listener, [
  9380. literal(name),
  9381. handlerFn,
  9382. ]);
  9383. }
  9384. function advance(delta) {
  9385. return call(Identifiers.advance, [
  9386. literal(delta),
  9387. ]);
  9388. }
  9389. function reference(slot) {
  9390. return importExpr(Identifiers.reference).callFn([
  9391. literal(slot),
  9392. ]);
  9393. }
  9394. function nextContext(steps) {
  9395. return importExpr(Identifiers.nextContext).callFn(steps === 1 ? [] : [literal(steps)]);
  9396. }
  9397. function getCurrentView() {
  9398. return importExpr(Identifiers.getCurrentView).callFn([]);
  9399. }
  9400. function restoreView(savedView) {
  9401. return importExpr(Identifiers.restoreView).callFn([
  9402. savedView,
  9403. ]);
  9404. }
  9405. function resetView(returnValue) {
  9406. return importExpr(Identifiers.resetView).callFn([
  9407. returnValue,
  9408. ]);
  9409. }
  9410. function text(slot, initialValue) {
  9411. const args = [literal(slot)];
  9412. if (initialValue !== '') {
  9413. args.push(literal(initialValue));
  9414. }
  9415. return call(Identifiers.text, args);
  9416. }
  9417. function property(name, expression) {
  9418. return call(Identifiers.property, [
  9419. literal(name),
  9420. expression,
  9421. ]);
  9422. }
  9423. function textInterpolate(strings, expressions) {
  9424. if (strings.length < 1 || expressions.length !== strings.length - 1) {
  9425. throw new Error(`AssertionError: expected specific shape of args for strings/expressions in interpolation`);
  9426. }
  9427. const interpolationArgs = [];
  9428. if (expressions.length === 1 && strings[0] === '' && strings[1] === '') {
  9429. interpolationArgs.push(expressions[0]);
  9430. }
  9431. else {
  9432. let idx;
  9433. for (idx = 0; idx < expressions.length; idx++) {
  9434. interpolationArgs.push(literal(strings[idx]), expressions[idx]);
  9435. }
  9436. // idx points at the last string.
  9437. interpolationArgs.push(literal(strings[idx]));
  9438. }
  9439. return callInterpolation(TEXT_INTERPOLATE_CONFIG, [], interpolationArgs);
  9440. }
  9441. function call(instruction, args) {
  9442. return createStatementOp(importExpr(instruction).callFn(args).toStmt());
  9443. }
  9444. /**
  9445. * `InterpolationConfig` for the `textInterpolate` instruction.
  9446. */
  9447. const TEXT_INTERPOLATE_CONFIG = {
  9448. constant: [
  9449. Identifiers.textInterpolate,
  9450. Identifiers.textInterpolate1,
  9451. Identifiers.textInterpolate2,
  9452. Identifiers.textInterpolate3,
  9453. Identifiers.textInterpolate4,
  9454. Identifiers.textInterpolate5,
  9455. Identifiers.textInterpolate6,
  9456. Identifiers.textInterpolate7,
  9457. Identifiers.textInterpolate8,
  9458. ],
  9459. variable: Identifiers.textInterpolateV,
  9460. };
  9461. function callInterpolation(config, baseArgs, interpolationArgs) {
  9462. if (interpolationArgs.length % 2 === 0) {
  9463. throw new Error(`Expected odd number of interpolation arguments`);
  9464. }
  9465. const n = (interpolationArgs.length - 1) / 2;
  9466. if (n < config.constant.length) {
  9467. // Constant calling pattern.
  9468. return call(config.constant[n], [...baseArgs, ...interpolationArgs]);
  9469. }
  9470. else {
  9471. // Variable calling pattern.
  9472. return call(config.variable, [...baseArgs, literalArr(interpolationArgs)]);
  9473. }
  9474. }
  9475. /**
  9476. * Compiles semantic operations across all views and generates output `o.Statement`s with actual
  9477. * runtime calls in their place.
  9478. *
  9479. * Reification replaces semantic operations with selected Ivy instructions and other generated code
  9480. * structures. After reification, the create/update operation lists of all views should only contain
  9481. * `ir.StatementOp`s (which wrap generated `o.Statement`s).
  9482. */
  9483. function phaseReify(cpl) {
  9484. for (const [_, view] of cpl.views) {
  9485. reifyCreateOperations(view, view.create);
  9486. reifyUpdateOperations(view, view.update);
  9487. }
  9488. }
  9489. function reifyCreateOperations(view, ops) {
  9490. for (const op of ops) {
  9491. transformExpressionsInOp(op, reifyIrExpression, VisitorContextFlag.None);
  9492. switch (op.kind) {
  9493. case OpKind.Text:
  9494. OpList.replace(op, text(op.slot, op.initialValue));
  9495. break;
  9496. case OpKind.ElementStart:
  9497. OpList.replace(op, elementStart(op.slot, op.tag, op.attributes, op.localRefs));
  9498. break;
  9499. case OpKind.Element:
  9500. OpList.replace(op, element(op.slot, op.tag, op.attributes, op.localRefs));
  9501. break;
  9502. case OpKind.ElementEnd:
  9503. OpList.replace(op, elementEnd());
  9504. break;
  9505. case OpKind.Template:
  9506. const childView = view.tpl.views.get(op.xref);
  9507. OpList.replace(op, template(op.slot, variable(childView.fnName), childView.decls, childView.vars, op.tag, op.attributes));
  9508. break;
  9509. case OpKind.Listener:
  9510. const listenerFn = reifyListenerHandler(view, op.handlerFnName, op.handlerOps);
  9511. OpList.replace(op, listener(op.name, listenerFn));
  9512. break;
  9513. case OpKind.Variable:
  9514. if (op.variable.name === null) {
  9515. throw new Error(`AssertionError: unnamed variable ${op.xref}`);
  9516. }
  9517. OpList.replace(op, createStatementOp(new DeclareVarStmt(op.variable.name, op.initializer, undefined, StmtModifier.Final)));
  9518. break;
  9519. case OpKind.Statement:
  9520. // Pass statement operations directly through.
  9521. break;
  9522. default:
  9523. throw new Error(`AssertionError: Unsupported reification of create op ${OpKind[op.kind]}`);
  9524. }
  9525. }
  9526. }
  9527. function reifyUpdateOperations(_view, ops) {
  9528. for (const op of ops) {
  9529. transformExpressionsInOp(op, reifyIrExpression, VisitorContextFlag.None);
  9530. switch (op.kind) {
  9531. case OpKind.Advance:
  9532. OpList.replace(op, advance(op.delta));
  9533. break;
  9534. case OpKind.Property:
  9535. OpList.replace(op, property(op.name, op.expression));
  9536. break;
  9537. case OpKind.InterpolateText:
  9538. OpList.replace(op, textInterpolate(op.strings, op.expressions));
  9539. break;
  9540. case OpKind.Variable:
  9541. if (op.variable.name === null) {
  9542. throw new Error(`AssertionError: unnamed variable ${op.xref}`);
  9543. }
  9544. OpList.replace(op, createStatementOp(new DeclareVarStmt(op.variable.name, op.initializer, undefined, StmtModifier.Final)));
  9545. break;
  9546. case OpKind.Statement:
  9547. // Pass statement operations directly through.
  9548. break;
  9549. default:
  9550. throw new Error(`AssertionError: Unsupported reification of update op ${OpKind[op.kind]}`);
  9551. }
  9552. }
  9553. }
  9554. function reifyIrExpression(expr) {
  9555. switch (expr.kind) {
  9556. case ExpressionKind.NextContext:
  9557. return nextContext(expr.steps);
  9558. case ExpressionKind.Reference:
  9559. return reference(expr.slot + 1 + expr.offset);
  9560. case ExpressionKind.LexicalRead:
  9561. throw new Error(`AssertionError: unresolved LexicalRead of ${expr.name}`);
  9562. case ExpressionKind.RestoreView:
  9563. if (typeof expr.view === 'number') {
  9564. throw new Error(`AssertionError: unresolved RestoreView`);
  9565. }
  9566. return restoreView(expr.view);
  9567. case ExpressionKind.ResetView:
  9568. return resetView(expr.expr);
  9569. case ExpressionKind.GetCurrentView:
  9570. return getCurrentView();
  9571. case ExpressionKind.ReadVariable:
  9572. if (expr.name === null) {
  9573. throw new Error(`Read of unnamed variable ${expr.xref}`);
  9574. }
  9575. return variable(expr.name);
  9576. default:
  9577. throw new Error(`AssertionError: Unsupported reification of ir.Expression kind: ${ExpressionKind[expr.kind]}`);
  9578. }
  9579. }
  9580. /**
  9581. * Listeners get turned into a function expression, which may or may not have the `$event`
  9582. * parameter defined.
  9583. */
  9584. function reifyListenerHandler(view, name, handlerOps) {
  9585. const lookForEvent = new LookForEventVisitor();
  9586. // First, reify all instruction calls within `handlerOps`.
  9587. reifyUpdateOperations(view, handlerOps);
  9588. // Next, extract all the `o.Statement`s from the reified operations. We can expect that at this
  9589. // point, all operations have been converted to statements.
  9590. const handlerStmts = [];
  9591. for (const op of handlerOps) {
  9592. if (op.kind !== OpKind.Statement) {
  9593. throw new Error(`AssertionError: expected reified statements, but found op ${OpKind[op.kind]}`);
  9594. }
  9595. handlerStmts.push(op.statement);
  9596. }
  9597. // Scan the statement list for usages of `$event`. If referenced, we need to generate it as a
  9598. // parameter.
  9599. lookForEvent.visitAllStatements(handlerStmts, null);
  9600. const params = [];
  9601. if (lookForEvent.seenEventRead) {
  9602. // We need the `$event` parameter.
  9603. params.push(new FnParam('$event'));
  9604. }
  9605. return fn(params, handlerStmts, undefined, undefined, name);
  9606. }
  9607. /**
  9608. * Visitor which scans for reads of the `$event` special variable.
  9609. */
  9610. class LookForEventVisitor extends RecursiveAstVisitor$1 {
  9611. constructor() {
  9612. super(...arguments);
  9613. this.seenEventRead = false;
  9614. }
  9615. visitReadVarExpr(ast, context) {
  9616. if (ast.name === '$event') {
  9617. this.seenEventRead = true;
  9618. }
  9619. }
  9620. }
  9621. /**
  9622. * Assign data slots for all operations which implement `ConsumesSlotOpTrait`, and propagate the
  9623. * assigned data slots of those operations to any expressions which reference them via
  9624. * `UsesSlotIndexTrait`.
  9625. *
  9626. * This phase is also responsible for counting the number of slots used for each view (its `decls`)
  9627. * and propagating that number into the `Template` operations which declare embedded views.
  9628. */
  9629. function phaseSlotAllocation(cpl) {
  9630. // Map of all declarations in all views within the component which require an assigned slot index.
  9631. // This map needs to be global (across all views within the component) since it's possible to
  9632. // reference a slot from one view from an expression within another (e.g. local references work
  9633. // this way).
  9634. const slotMap = new Map();
  9635. // Process all views in the component and assign slot indexes.
  9636. for (const [_, view] of cpl.views) {
  9637. // Slot indices start at 0 for each view (and are not unique between views).
  9638. let slotCount = 0;
  9639. for (const op of view.create) {
  9640. // Only consider declarations which consume data slots.
  9641. if (!hasConsumesSlotTrait(op)) {
  9642. continue;
  9643. }
  9644. // Assign slots to this declaration starting at the current `slotCount`.
  9645. op.slot = slotCount;
  9646. // And track its assigned slot in the `slotMap`.
  9647. slotMap.set(op.xref, op.slot);
  9648. // Each declaration may use more than 1 slot, so increment `slotCount` to reserve the number
  9649. // of slots required.
  9650. slotCount += op.numSlotsUsed;
  9651. }
  9652. // Record the total number of slots used on the view itself. This will later be propagated into
  9653. // `ir.TemplateOp`s which declare those views (except for the root view).
  9654. view.decls = slotCount;
  9655. }
  9656. // After slot assignment, `slotMap` now contains slot assignments for every declaration in the
  9657. // whole template, across all views. Next, look for expressions which implement
  9658. // `UsesSlotIndexExprTrait` and propagate the assigned slot indexes into them.
  9659. // Additionally, this second scan allows us to find `ir.TemplateOp`s which declare views and
  9660. // propagate the number of slots used for each view into the operation which declares it.
  9661. for (const [_, view] of cpl.views) {
  9662. for (const op of view.ops()) {
  9663. if (op.kind === OpKind.Template) {
  9664. // Record the number of slots used by the view this `ir.TemplateOp` declares in the
  9665. // operation itself, so it can be emitted later.
  9666. const childView = cpl.views.get(op.xref);
  9667. op.decls = childView.decls;
  9668. }
  9669. if (hasUsesSlotIndexTrait(op) && op.slot === null) {
  9670. if (!slotMap.has(op.target)) {
  9671. // We do expect to find a slot allocated for everything which might be referenced.
  9672. throw new Error(`AssertionError: no slot allocated for ${OpKind[op.kind]} target ${op.target}`);
  9673. }
  9674. op.slot = slotMap.get(op.target);
  9675. }
  9676. // Process all `ir.Expression`s within this view, and look for `usesSlotIndexExprTrait`.
  9677. visitExpressionsInOp(op, expr => {
  9678. if (!hasUsesSlotIndexTrait(expr) || expr.slot !== null) {
  9679. return;
  9680. }
  9681. // The `UsesSlotIndexExprTrait` indicates that this expression references something declared
  9682. // in this component template by its slot index. Use the `target` `ir.XrefId` to find the
  9683. // allocated slot for that declaration in `slotMap`.
  9684. if (!slotMap.has(expr.target)) {
  9685. // We do expect to find a slot allocated for everything which might be referenced.
  9686. throw new Error(`AssertionError: no slot allocated for ${expr.constructor.name} target ${expr.target}`);
  9687. }
  9688. // Record the allocated slot on the expression.
  9689. expr.slot = slotMap.get(expr.target);
  9690. });
  9691. }
  9692. }
  9693. }
  9694. /**
  9695. * Counts the number of variable slots used within each view, and stores that on the view itself, as
  9696. * well as propagates it to the `ir.TemplateOp` for embedded views.
  9697. */
  9698. function phaseVarCounting(cpl) {
  9699. // First, count the vars used in each view, and update the view-level counter.
  9700. for (const [_, view] of cpl.views) {
  9701. let varCount = 0;
  9702. for (const op of view.ops()) {
  9703. if (hasConsumesVarsTrait(op)) {
  9704. varCount += varsUsedByOp(op);
  9705. }
  9706. visitExpressionsInOp(op, expr => {
  9707. if (hasConsumesVarsTrait(expr)) {
  9708. varCount += varsUsedByIrExpression(expr);
  9709. }
  9710. });
  9711. }
  9712. view.vars = varCount;
  9713. }
  9714. // Add var counts for each view to the `ir.TemplateOp` which declares that view (if the view is an
  9715. // embedded view).
  9716. for (const [_, view] of cpl.views) {
  9717. for (const op of view.create) {
  9718. if (op.kind !== OpKind.Template) {
  9719. continue;
  9720. }
  9721. const childView = cpl.views.get(op.xref);
  9722. op.vars = childView.vars;
  9723. }
  9724. }
  9725. }
  9726. /**
  9727. * Different operations that implement `ir.UsesVarsTrait` use different numbers of variables, so
  9728. * count the variables used by any particular `op`.
  9729. */
  9730. function varsUsedByOp(op) {
  9731. switch (op.kind) {
  9732. case OpKind.Property:
  9733. // Property bindings use 1 variable slot.
  9734. return 1;
  9735. case OpKind.InterpolateText:
  9736. // `ir.InterpolateTextOp`s use a variable slot for each dynamic expression.
  9737. return op.expressions.length;
  9738. default:
  9739. throw new Error(`Unhandled op: ${OpKind[op.kind]}`);
  9740. }
  9741. }
  9742. function varsUsedByIrExpression(expr) {
  9743. return 0;
  9744. }
  9745. /**
  9746. * Generate names for functions and variables across all views.
  9747. *
  9748. * This includes propagating those names into any `ir.ReadVariableExpr`s of those variables, so that
  9749. * the reads can be emitted correctly.
  9750. */
  9751. function phaseNaming(cpl) {
  9752. addNamesToView(cpl.root, cpl.componentName, { index: 0 });
  9753. }
  9754. function addNamesToView(view, baseName, state) {
  9755. if (view.fnName === null) {
  9756. view.fnName = `${baseName}_Template`;
  9757. }
  9758. // Keep track of the names we assign to variables in the view. We'll need to propagate these
  9759. // into reads of those variables afterwards.
  9760. const varNames = new Map();
  9761. for (const op of view.ops()) {
  9762. switch (op.kind) {
  9763. case OpKind.Listener:
  9764. if (op.handlerFnName === null) {
  9765. // TODO(alxhub): convert this temporary name to match how the
  9766. // `TemplateDefinitionBuilder` names listener functions.
  9767. if (op.slot === null) {
  9768. throw new Error(`Expected a slot to be assigned`);
  9769. }
  9770. op.handlerFnName = `${view.fnName}_${op.tag}_${op.name}_${op.slot}_listener`;
  9771. }
  9772. break;
  9773. case OpKind.Variable:
  9774. varNames.set(op.xref, getVariableName(op.variable, state));
  9775. break;
  9776. case OpKind.Template:
  9777. const childView = view.tpl.views.get(op.xref);
  9778. if (op.slot === null) {
  9779. throw new Error(`Expected slot to be assigned`);
  9780. }
  9781. // TODO: properly escape the tag name.
  9782. const safeTagName = op.tag.replace('-', '_');
  9783. addNamesToView(childView, `${baseName}_${safeTagName}_${op.slot}`, state);
  9784. break;
  9785. }
  9786. }
  9787. // Having named all variables declared in the view, now we can push those names into the
  9788. // `ir.ReadVariableExpr` expressions which represent reads of those variables.
  9789. for (const op of view.ops()) {
  9790. visitExpressionsInOp(op, expr => {
  9791. if (!(expr instanceof ReadVariableExpr) || expr.name !== null) {
  9792. return;
  9793. }
  9794. if (!varNames.has(expr.xref)) {
  9795. throw new Error(`Variable ${expr.xref} not yet named`);
  9796. }
  9797. expr.name = varNames.get(expr.xref);
  9798. });
  9799. }
  9800. }
  9801. function getVariableName(variable, state) {
  9802. if (variable.name === null) {
  9803. switch (variable.kind) {
  9804. case SemanticVariableKind.Identifier:
  9805. variable.name = `${variable.identifier}_${state.index++}`;
  9806. break;
  9807. default:
  9808. variable.name = `_r${state.index++}`;
  9809. break;
  9810. }
  9811. }
  9812. return variable.name;
  9813. }
  9814. /**
  9815. * Lifts local reference declarations on element-like structures within each view into an entry in
  9816. * the `consts` array for the whole component.
  9817. */
  9818. function phaseLocalRefs(cpl) {
  9819. for (const view of cpl.views.values()) {
  9820. for (const op of view.create) {
  9821. switch (op.kind) {
  9822. case OpKind.ElementStart:
  9823. case OpKind.Element:
  9824. case OpKind.Template:
  9825. if (!Array.isArray(op.localRefs)) {
  9826. throw new Error(`AssertionError: expected localRefs to be an array still`);
  9827. }
  9828. op.numSlotsUsed += op.localRefs.length;
  9829. if (op.localRefs.length > 0) {
  9830. const localRefs = serializeLocalRefs(op.localRefs);
  9831. op.localRefs = cpl.addConst(localRefs);
  9832. }
  9833. else {
  9834. op.localRefs = null;
  9835. }
  9836. break;
  9837. }
  9838. }
  9839. }
  9840. }
  9841. function serializeLocalRefs(refs) {
  9842. const constRefs = [];
  9843. for (const ref of refs) {
  9844. constRefs.push(literal(ref.name), literal(ref.target));
  9845. }
  9846. return literalArr(constRefs);
  9847. }
  9848. /**
  9849. * Generate a preamble sequence for each view creation block and listener function which declares
  9850. * any variables that be referenced in other operations in the block.
  9851. *
  9852. * Variables generated include:
  9853. * * a saved view context to be used to restore the current view in event listeners.
  9854. * * the context of the restored view within event listener handlers.
  9855. * * context variables from the current view as well as all parent views (including the root
  9856. * context if needed).
  9857. * * local references from elements within the current view and any lexical parents.
  9858. *
  9859. * Variables are generated here unconditionally, and may optimized away in future operations if it
  9860. * turns out their values (and any side effects) are unused.
  9861. */
  9862. function phaseGenerateVariables(cpl) {
  9863. recursivelyProcessView(cpl.root, /* there is no parent scope for the root view */ null);
  9864. }
  9865. /**
  9866. * Process the given `ViewCompilation` and generate preambles for it and any listeners that it
  9867. * declares.
  9868. *
  9869. * @param `parentScope` a scope extracted from the parent view which captures any variables which
  9870. * should be inherited by this view. `null` if the current view is the root view.
  9871. */
  9872. function recursivelyProcessView(view, parentScope) {
  9873. // Extract a `Scope` from this view.
  9874. const scope = getScopeForView(view, parentScope);
  9875. // Start the view creation block with an operation to save the current view context. This may be
  9876. // used to restore the view context in any listeners that may be present.
  9877. view.create.prepend([
  9878. createVariableOp(view.tpl.allocateXrefId(), scope.savedViewVariable, new GetCurrentViewExpr()),
  9879. ]);
  9880. for (const op of view.create) {
  9881. switch (op.kind) {
  9882. case OpKind.Template:
  9883. // Descend into child embedded views.
  9884. recursivelyProcessView(view.tpl.views.get(op.xref), scope);
  9885. break;
  9886. case OpKind.Listener:
  9887. // Listeners get a preamble which starts with a call to restore the view.
  9888. const preambleOps = [
  9889. createVariableOp(view.tpl.allocateXrefId(), scope.viewContextVariable, new RestoreViewExpr(view.xref)),
  9890. // And includes all variables available to this view.
  9891. ...generateVariablesInScopeForView(view, scope)
  9892. ];
  9893. op.handlerOps.prepend(preambleOps);
  9894. // The "restore view" operation in listeners requires a call to `resetView` to reset the
  9895. // context prior to returning from the listener operation. Find any `return` statements in
  9896. // the listener body and wrap them in a call to reset the view.
  9897. for (const handlerOp of op.handlerOps) {
  9898. if (handlerOp.kind === OpKind.Statement &&
  9899. handlerOp.statement instanceof ReturnStatement) {
  9900. handlerOp.statement.value = new ResetViewExpr(handlerOp.statement.value);
  9901. }
  9902. }
  9903. break;
  9904. }
  9905. }
  9906. // Prepend the declarations for all available variables in scope to the `update` block.
  9907. const preambleOps = generateVariablesInScopeForView(view, scope);
  9908. view.update.prepend(preambleOps);
  9909. }
  9910. /**
  9911. * Process a view and generate a `Scope` representing the variables available for reference within
  9912. * that view.
  9913. */
  9914. function getScopeForView(view, parent) {
  9915. const scope = {
  9916. view: view.xref,
  9917. viewContextVariable: {
  9918. kind: SemanticVariableKind.Context,
  9919. name: null,
  9920. view: view.xref,
  9921. },
  9922. savedViewVariable: {
  9923. kind: SemanticVariableKind.SavedView,
  9924. name: null,
  9925. view: view.xref,
  9926. },
  9927. contextVariables: new Map(),
  9928. references: [],
  9929. parent,
  9930. };
  9931. for (const identifier of view.contextVariables.keys()) {
  9932. scope.contextVariables.set(identifier, {
  9933. kind: SemanticVariableKind.Identifier,
  9934. name: null,
  9935. identifier,
  9936. });
  9937. }
  9938. for (const op of view.create) {
  9939. switch (op.kind) {
  9940. case OpKind.Element:
  9941. case OpKind.ElementStart:
  9942. case OpKind.Template:
  9943. if (!Array.isArray(op.localRefs)) {
  9944. throw new Error(`AssertionError: expected localRefs to be an array`);
  9945. }
  9946. // Record available local references from this element.
  9947. for (let offset = 0; offset < op.localRefs.length; offset++) {
  9948. scope.references.push({
  9949. name: op.localRefs[offset].name,
  9950. targetId: op.xref,
  9951. offset,
  9952. variable: {
  9953. kind: SemanticVariableKind.Identifier,
  9954. name: null,
  9955. identifier: op.localRefs[offset].name,
  9956. },
  9957. });
  9958. }
  9959. break;
  9960. }
  9961. }
  9962. return scope;
  9963. }
  9964. /**
  9965. * Generate declarations for all variables that are in scope for a given view.
  9966. *
  9967. * This is a recursive process, as views inherit variables available from their parent view, which
  9968. * itself may have inherited variables, etc.
  9969. */
  9970. function generateVariablesInScopeForView(view, scope) {
  9971. const newOps = [];
  9972. if (scope.view !== view.xref) {
  9973. // Before generating variables for a parent view, we need to switch to the context of the parent
  9974. // view with a `nextContext` expression. This context switching operation itself declares a
  9975. // variable, because the context of the view may be referenced directly.
  9976. newOps.push(createVariableOp(view.tpl.allocateXrefId(), scope.viewContextVariable, new NextContextExpr()));
  9977. }
  9978. // Add variables for all context variables available in this scope's view.
  9979. for (const [name, value] of view.tpl.views.get(scope.view).contextVariables) {
  9980. newOps.push(createVariableOp(view.tpl.allocateXrefId(), scope.contextVariables.get(name), new ReadPropExpr(new ContextExpr(scope.view), value)));
  9981. }
  9982. // Add variables for all local references declared for elements in this scope.
  9983. for (const ref of scope.references) {
  9984. newOps.push(createVariableOp(view.tpl.allocateXrefId(), ref.variable, new ReferenceExpr(ref.targetId, ref.offset)));
  9985. }
  9986. if (scope.parent !== null) {
  9987. // Recursively add variables from the parent scope.
  9988. newOps.push(...generateVariablesInScopeForView(view, scope.parent));
  9989. }
  9990. return newOps;
  9991. }
  9992. /**
  9993. * Resolves lexical references in views (`ir.LexicalReadExpr`) to either a target variable or to
  9994. * property reads on the top-level component context.
  9995. *
  9996. * Also matches `ir.RestoreViewExpr` expressions with the variables of their corresponding saved
  9997. * views.
  9998. */
  9999. function phaseResolveNames(cpl) {
  10000. for (const [_, view] of cpl.views) {
  10001. processLexicalScope$1(view, view.create, null);
  10002. processLexicalScope$1(view, view.update, null);
  10003. }
  10004. }
  10005. function processLexicalScope$1(view, ops, savedView) {
  10006. // Maps names defined in the lexical scope of this template to the `ir.XrefId`s of the variable
  10007. // declarations which represent those values.
  10008. //
  10009. // Since variables are generated in each view for the entire lexical scope (including any
  10010. // identifiers from parent templates) only local variables need be considered here.
  10011. const scope = new Map();
  10012. // First, step through the operations list and:
  10013. // 1) build up the `scope` mapping
  10014. // 2) recurse into any listener functions
  10015. for (const op of ops) {
  10016. switch (op.kind) {
  10017. case OpKind.Variable:
  10018. switch (op.variable.kind) {
  10019. case SemanticVariableKind.Identifier:
  10020. // This variable represents some kind of identifier which can be used in the template.
  10021. if (scope.has(op.variable.identifier)) {
  10022. continue;
  10023. }
  10024. scope.set(op.variable.identifier, op.xref);
  10025. break;
  10026. case SemanticVariableKind.SavedView:
  10027. // This variable represents a snapshot of the current view context, and can be used to
  10028. // restore that context within listener functions.
  10029. savedView = {
  10030. view: op.variable.view,
  10031. variable: op.xref,
  10032. };
  10033. break;
  10034. }
  10035. break;
  10036. case OpKind.Listener:
  10037. // Listener functions have separate variable declarations, so process them as a separate
  10038. // lexical scope.
  10039. processLexicalScope$1(view, op.handlerOps, savedView);
  10040. break;
  10041. }
  10042. }
  10043. // Next, use the `scope` mapping to match `ir.LexicalReadExpr` with defined names in the lexical
  10044. // scope. Also, look for `ir.RestoreViewExpr`s and match them with the snapshotted view context
  10045. // variable.
  10046. for (const op of ops) {
  10047. transformExpressionsInOp(op, expr => {
  10048. if (expr instanceof LexicalReadExpr) {
  10049. // `expr` is a read of a name within the lexical scope of this view.
  10050. // Either that name is defined within the current view, or it represents a property from the
  10051. // main component context.
  10052. if (scope.has(expr.name)) {
  10053. // This was a defined variable in the current scope.
  10054. return new ReadVariableExpr(scope.get(expr.name));
  10055. }
  10056. else {
  10057. // Reading from the component context.
  10058. return new ReadPropExpr(new ContextExpr(view.tpl.root.xref), expr.name);
  10059. }
  10060. }
  10061. else if (expr instanceof RestoreViewExpr && typeof expr.view === 'number') {
  10062. // `ir.RestoreViewExpr` happens in listener functions and restores a saved view from the
  10063. // parent creation list. We expect to find that we captured the `savedView` previously, and
  10064. // that it matches the expected view to be restored.
  10065. if (savedView === null || savedView.view !== expr.view) {
  10066. throw new Error(`AssertionError: no saved view ${expr.view} from view ${view.xref}`);
  10067. }
  10068. expr.view = new ReadVariableExpr(savedView.variable);
  10069. return expr;
  10070. }
  10071. else {
  10072. return expr;
  10073. }
  10074. }, VisitorContextFlag.None);
  10075. }
  10076. }
  10077. /**
  10078. * Resolves `ir.ContextExpr` expressions (which represent embedded view or component contexts) to
  10079. * either the `ctx` parameter to component functions (for the current view context) or to variables
  10080. * that store those contexts (for contexts accessed via the `nextContext()` instruction).
  10081. */
  10082. function phaseResolveContexts(cpl) {
  10083. for (const view of cpl.views.values()) {
  10084. processLexicalScope(view, view.create);
  10085. processLexicalScope(view, view.update);
  10086. }
  10087. }
  10088. function processLexicalScope(view, ops) {
  10089. // Track the expressions used to access all available contexts within the current view, by the
  10090. // view `ir.XrefId`.
  10091. const scope = new Map();
  10092. // The current view's context is accessible via the `ctx` parameter.
  10093. scope.set(view.xref, variable('ctx'));
  10094. for (const op of ops) {
  10095. switch (op.kind) {
  10096. case OpKind.Variable:
  10097. switch (op.variable.kind) {
  10098. case SemanticVariableKind.Context:
  10099. scope.set(op.variable.view, new ReadVariableExpr(op.xref));
  10100. break;
  10101. }
  10102. break;
  10103. case OpKind.Listener:
  10104. processLexicalScope(view, op.handlerOps);
  10105. break;
  10106. }
  10107. }
  10108. for (const op of ops) {
  10109. transformExpressionsInOp(op, expr => {
  10110. if (expr instanceof ContextExpr) {
  10111. if (!scope.has(expr.view)) {
  10112. throw new Error(`No context found for reference to view ${expr.view} from view ${view.xref}`);
  10113. }
  10114. return scope.get(expr.view);
  10115. }
  10116. else {
  10117. return expr;
  10118. }
  10119. }, VisitorContextFlag.None);
  10120. }
  10121. }
  10122. /**
  10123. * Optimize variables declared and used in the IR.
  10124. *
  10125. * Variables are eagerly generated by pipeline stages for all possible values that could be
  10126. * referenced. This stage processes the list of declared variables and all variable usages,
  10127. * and optimizes where possible. It performs 3 main optimizations:
  10128. *
  10129. * * It transforms variable declarations to side effectful expressions when the
  10130. * variable is not used, but its initializer has global effects which other
  10131. * operations rely upon.
  10132. * * It removes variable declarations if those variables are not referenced and
  10133. * either they do not have global effects, or nothing relies on them.
  10134. * * It inlines variable declarations when those variables are only used once
  10135. * and the inlining is semantically safe.
  10136. *
  10137. * To guarantee correctness, analysis of "fences" in the instruction lists is used to determine
  10138. * which optimizations are safe to perform.
  10139. */
  10140. function phaseVariableOptimization(cpl, options) {
  10141. for (const [_, view] of cpl.views) {
  10142. optimizeVariablesInOpList(view.create, options);
  10143. optimizeVariablesInOpList(view.update, options);
  10144. for (const op of view.create) {
  10145. if (op.kind === OpKind.Listener) {
  10146. optimizeVariablesInOpList(op.handlerOps, options);
  10147. }
  10148. }
  10149. }
  10150. }
  10151. /**
  10152. * A [fence](https://en.wikipedia.org/wiki/Memory_barrier) flag for an expression which indicates
  10153. * how that expression can be optimized in relation to other expressions or instructions.
  10154. *
  10155. * `Fence`s are a bitfield, so multiple flags may be set on a single expression.
  10156. */
  10157. var Fence;
  10158. (function (Fence) {
  10159. /**
  10160. * Empty flag (no fence exists).
  10161. */
  10162. Fence[Fence["None"] = 0] = "None";
  10163. /**
  10164. * A context read fence, meaning that the expression in question reads from the "current view"
  10165. * context of the runtime.
  10166. */
  10167. Fence[Fence["ViewContextRead"] = 1] = "ViewContextRead";
  10168. /**
  10169. * A context write fence, meaning that the expression in question writes to the "current view"
  10170. * context of the runtime.
  10171. *
  10172. * Note that all `ContextWrite` fences are implicitly `ContextRead` fences as operations which
  10173. * change the view context do so based on the current one.
  10174. */
  10175. Fence[Fence["ViewContextWrite"] = 3] = "ViewContextWrite";
  10176. /**
  10177. * Indicates that a call is required for its side-effects, even if nothing reads its result.
  10178. *
  10179. * This is also true of `ViewContextWrite` operations **if** they are followed by a
  10180. * `ViewContextRead`.
  10181. */
  10182. Fence[Fence["SideEffectful"] = 4] = "SideEffectful";
  10183. })(Fence || (Fence = {}));
  10184. /**
  10185. * Process a list of operations and optimize variables within that list.
  10186. */
  10187. function optimizeVariablesInOpList(ops, options) {
  10188. const varDecls = new Map();
  10189. const varUsages = new Map();
  10190. // Track variables that are used outside of the immediate operation list. For example, within
  10191. // `ListenerOp` handler operations of listeners in the current operation list.
  10192. const varRemoteUsages = new Set();
  10193. const opMap = new Map();
  10194. // First, extract information about variables declared or used within the whole list.
  10195. for (const op of ops) {
  10196. if (op.kind === OpKind.Variable) {
  10197. if (varDecls.has(op.xref) || varUsages.has(op.xref)) {
  10198. throw new Error(`Should not see two declarations of the same variable: ${op.xref}`);
  10199. }
  10200. varDecls.set(op.xref, op);
  10201. varUsages.set(op.xref, 0);
  10202. }
  10203. opMap.set(op, collectOpInfo(op));
  10204. countVariableUsages(op, varUsages, varRemoteUsages);
  10205. }
  10206. // The next step is to remove any variable declarations for variables that aren't used. The
  10207. // variable initializer expressions may be side-effectful, so they may need to be retained as
  10208. // expression statements.
  10209. // Track whether we've seen an operation which reads from the view context yet. This is used to
  10210. // determine whether a write to the view context in a variable initializer can be observed.
  10211. let contextIsUsed = false;
  10212. // Note that iteration through the list happens in reverse, which guarantees that we'll process
  10213. // all reads of a variable prior to processing its declaration.
  10214. for (const op of ops.reversed()) {
  10215. const opInfo = opMap.get(op);
  10216. if (op.kind === OpKind.Variable && varUsages.get(op.xref) === 0) {
  10217. // This variable is unused and can be removed. We might need to keep the initializer around,
  10218. // though, if something depends on it running.
  10219. if ((contextIsUsed && opInfo.fences & Fence.ViewContextWrite) ||
  10220. (opInfo.fences & Fence.SideEffectful)) {
  10221. // This variable initializer has a side effect which must be retained. Either:
  10222. // * it writes to the view context, and we know there is a future operation which depends
  10223. // on that write, or
  10224. // * it's an operation which is inherently side-effectful.
  10225. // We can't remove the initializer, but we can remove the variable declaration itself and
  10226. // replace it with a side-effectful statement.
  10227. const stmtOp = createStatementOp(op.initializer.toStmt());
  10228. opMap.set(stmtOp, opInfo);
  10229. OpList.replace(op, stmtOp);
  10230. }
  10231. else {
  10232. // It's safe to delete this entire variable declaration as nothing depends on it, even
  10233. // side-effectfully. Note that doing this might make other variables unused. Since we're
  10234. // iterating in reverse order, we should always be processing usages before declarations
  10235. // and therefore by the time we get to a declaration, all removable usages will have been
  10236. // removed.
  10237. uncountVariableUsages(op, varUsages);
  10238. OpList.remove(op);
  10239. }
  10240. opMap.delete(op);
  10241. varDecls.delete(op.xref);
  10242. varUsages.delete(op.xref);
  10243. continue;
  10244. }
  10245. // Does this operation depend on the view context?
  10246. if (opInfo.fences & Fence.ViewContextRead) {
  10247. contextIsUsed = true;
  10248. }
  10249. }
  10250. // Next, inline any remaining variables with exactly one usage.
  10251. const toInline = [];
  10252. for (const [id, count] of varUsages) {
  10253. // We can inline variables that:
  10254. // - are used once
  10255. // - are not used remotely
  10256. if (count !== 1) {
  10257. // We can't inline this variable as it's used more than once.
  10258. continue;
  10259. }
  10260. if (varRemoteUsages.has(id)) {
  10261. // This variable is used once, but across an operation boundary, so it can't be inlined.
  10262. continue;
  10263. }
  10264. toInline.push(id);
  10265. }
  10266. let candidate;
  10267. while (candidate = toInline.pop()) {
  10268. // We will attempt to inline this variable. If inlining fails (due to fences for example),
  10269. // no future operation will make inlining legal.
  10270. const decl = varDecls.get(candidate);
  10271. const varInfo = opMap.get(decl);
  10272. // Scan operations following the variable declaration and look for the point where that variable
  10273. // is used. There should only be one usage given the precondition above.
  10274. for (let targetOp = decl.next; targetOp.kind !== OpKind.ListEnd; targetOp = targetOp.next) {
  10275. const opInfo = opMap.get(targetOp);
  10276. // Is the variable used in this operation?
  10277. if (opInfo.variablesUsed.has(candidate)) {
  10278. if (options.conservative && !allowConservativeInlining(decl, targetOp)) {
  10279. // We're in conservative mode, and this variable is not eligible for inlining into the
  10280. // target operation in this mode.
  10281. break;
  10282. }
  10283. // Yes, try to inline it. Inlining may not be successful if fences in this operation before
  10284. // the variable's usage cannot be safely crossed.
  10285. if (tryInlineVariableInitializer(candidate, decl.initializer, targetOp, varInfo.fences)) {
  10286. // Inlining was successful! Update the tracking structures to reflect the inlined
  10287. // variable.
  10288. opInfo.variablesUsed.delete(candidate);
  10289. // Add all variables used in the variable's initializer to its new usage site.
  10290. for (const id of varInfo.variablesUsed) {
  10291. opInfo.variablesUsed.add(id);
  10292. }
  10293. // Merge fences in the variable's initializer into its new usage site.
  10294. opInfo.fences |= varInfo.fences;
  10295. // Delete tracking info related to the declaration.
  10296. varDecls.delete(candidate);
  10297. varUsages.delete(candidate);
  10298. opMap.delete(decl);
  10299. // And finally, delete the original declaration from the operation list.
  10300. OpList.remove(decl);
  10301. }
  10302. // Whether inlining succeeded or failed, we're done processing this variable.
  10303. break;
  10304. }
  10305. // If the variable is not used in this operation, then we'd need to inline across it. Check if
  10306. // that's safe to do.
  10307. if (!safeToInlinePastFences(opInfo.fences, varInfo.fences)) {
  10308. // We can't safely inline this variable beyond this operation, so don't proceed with
  10309. // inlining this variable.
  10310. break;
  10311. }
  10312. }
  10313. }
  10314. }
  10315. /**
  10316. * Given an `ir.Expression`, returns the `Fence` flags for that expression type.
  10317. */
  10318. function fencesForIrExpression(expr) {
  10319. switch (expr.kind) {
  10320. case ExpressionKind.NextContext:
  10321. return Fence.ViewContextWrite;
  10322. case ExpressionKind.RestoreView:
  10323. return Fence.ViewContextWrite | Fence.SideEffectful;
  10324. case ExpressionKind.Reference:
  10325. return Fence.ViewContextRead;
  10326. default:
  10327. return Fence.None;
  10328. }
  10329. }
  10330. /**
  10331. * Build the `OpInfo` structure for the given `op`. This performs two operations:
  10332. *
  10333. * * It tracks which variables are used in the operation's expressions.
  10334. * * It rolls up fence flags for expressions within the operation.
  10335. */
  10336. function collectOpInfo(op) {
  10337. let fences = Fence.None;
  10338. const variablesUsed = new Set();
  10339. visitExpressionsInOp(op, expr => {
  10340. switch (expr.kind) {
  10341. case ExpressionKind.ReadVariable:
  10342. variablesUsed.add(expr.xref);
  10343. break;
  10344. default:
  10345. fences |= fencesForIrExpression(expr);
  10346. }
  10347. });
  10348. return { fences, variablesUsed };
  10349. }
  10350. /**
  10351. * Count the number of usages of each variable, being careful to track whether those usages are
  10352. * local or remote.
  10353. */
  10354. function countVariableUsages(op, varUsages, varRemoteUsage) {
  10355. visitExpressionsInOp(op, (expr, flags) => {
  10356. if (expr.kind !== ExpressionKind.ReadVariable) {
  10357. return;
  10358. }
  10359. const count = varUsages.get(expr.xref);
  10360. if (count === undefined) {
  10361. // This variable is declared outside the current scope of optimization.
  10362. return;
  10363. }
  10364. varUsages.set(expr.xref, count + 1);
  10365. if (flags & VisitorContextFlag.InChildOperation) {
  10366. varRemoteUsage.add(expr.xref);
  10367. }
  10368. });
  10369. }
  10370. /**
  10371. * Remove usages of a variable in `op` from the `varUsages` tracking.
  10372. */
  10373. function uncountVariableUsages(op, varUsages) {
  10374. visitExpressionsInOp(op, expr => {
  10375. if (expr.kind !== ExpressionKind.ReadVariable) {
  10376. return;
  10377. }
  10378. const count = varUsages.get(expr.xref);
  10379. if (count === undefined) {
  10380. // This variable is declared outside the current scope of optimization.
  10381. return;
  10382. }
  10383. else if (count === 0) {
  10384. throw new Error(`Inaccurate variable count: ${expr.xref} - found another read but count is already 0`);
  10385. }
  10386. varUsages.set(expr.xref, count - 1);
  10387. });
  10388. }
  10389. /**
  10390. * Checks whether it's safe to inline a variable across a particular operation.
  10391. *
  10392. * @param fences the fences of the operation which the inlining will cross
  10393. * @param declFences the fences of the variable being inlined.
  10394. */
  10395. function safeToInlinePastFences(fences, declFences) {
  10396. if (fences & Fence.ViewContextWrite) {
  10397. // It's not safe to inline context reads across context writes.
  10398. if (declFences & Fence.ViewContextRead) {
  10399. return false;
  10400. }
  10401. }
  10402. else if (fences & Fence.ViewContextRead) {
  10403. // It's not safe to inline context writes across context reads.
  10404. if (declFences & Fence.ViewContextWrite) {
  10405. return false;
  10406. }
  10407. }
  10408. return true;
  10409. }
  10410. /**
  10411. * Attempt to inline the initializer of a variable into a target operation's expressions.
  10412. *
  10413. * This may or may not be safe to do. For example, the variable could be read following the
  10414. * execution of an expression with fences that don't permit the variable to be inlined across them.
  10415. */
  10416. function tryInlineVariableInitializer(id, initializer, target, declFences) {
  10417. // We use `ir.transformExpressionsInOp` to walk the expressions and inline the variable if
  10418. // possible. Since this operation is callback-based, once inlining succeeds or fails we can't
  10419. // "stop" the expression processing, and have to keep track of whether inlining has succeeded or
  10420. // is no longer allowed.
  10421. let inlined = false;
  10422. let inliningAllowed = true;
  10423. transformExpressionsInOp(target, (expr, flags) => {
  10424. if (inlined || !inliningAllowed) {
  10425. // Either the inlining has already succeeded, or we've passed a fence that disallows inlining
  10426. // at this point, so don't try.
  10427. return expr;
  10428. }
  10429. else if ((flags & VisitorContextFlag.InChildOperation) && (declFences & Fence.ViewContextRead)) {
  10430. // We cannot inline variables that are sensitive to the current context across operation
  10431. // boundaries.
  10432. return expr;
  10433. }
  10434. switch (expr.kind) {
  10435. case ExpressionKind.ReadVariable:
  10436. if (expr.xref === id) {
  10437. // This is the usage site of the variable. Since nothing has disallowed inlining, it's
  10438. // safe to inline the initializer here.
  10439. inlined = true;
  10440. return initializer;
  10441. }
  10442. break;
  10443. default:
  10444. // For other types of `ir.Expression`s, whether inlining is allowed depends on their fences.
  10445. const exprFences = fencesForIrExpression(expr);
  10446. inliningAllowed = inliningAllowed && safeToInlinePastFences(exprFences, declFences);
  10447. break;
  10448. }
  10449. return expr;
  10450. }, VisitorContextFlag.None);
  10451. return inlined;
  10452. }
  10453. /**
  10454. * Determines whether inlining of `decl` should be allowed in "conservative" mode.
  10455. *
  10456. * In conservative mode, inlining behavior is limited to those operations which the
  10457. * `TemplateDefinitionBuilder` supported, with the goal of producing equivalent output.
  10458. */
  10459. function allowConservativeInlining(decl, target) {
  10460. // TODO(alxhub): understand exactly how TemplateDefinitionBuilder approaches inlining, and record
  10461. // that behavior here.
  10462. switch (decl.variable.kind) {
  10463. case SemanticVariableKind.Identifier:
  10464. return false;
  10465. case SemanticVariableKind.Context:
  10466. // Context can only be inlined into other variables.
  10467. return target.kind === OpKind.Variable;
  10468. default:
  10469. return true;
  10470. }
  10471. }
  10472. const CHAINABLE = new Set([
  10473. Identifiers.elementStart,
  10474. Identifiers.elementEnd,
  10475. Identifiers.property,
  10476. ]);
  10477. /**
  10478. * Post-process a reified view compilation and convert sequential calls to chainable instructions
  10479. * into chain calls.
  10480. *
  10481. * For example, two `elementStart` operations in sequence:
  10482. *
  10483. * ```typescript
  10484. * elementStart(0, 'div');
  10485. * elementStart(1, 'span');
  10486. * ```
  10487. *
  10488. * Can be called as a chain instead:
  10489. *
  10490. * ```typescript
  10491. * elementStart(0, 'div')(1, 'span');
  10492. * ```
  10493. */
  10494. function phaseChaining(cpl) {
  10495. for (const [_, view] of cpl.views) {
  10496. chainOperationsInList(view.create);
  10497. chainOperationsInList(view.update);
  10498. }
  10499. }
  10500. function chainOperationsInList(opList) {
  10501. let chain = null;
  10502. for (const op of opList) {
  10503. if (op.kind !== OpKind.Statement || !(op.statement instanceof ExpressionStatement)) {
  10504. // This type of statement isn't chainable.
  10505. chain = null;
  10506. continue;
  10507. }
  10508. if (!(op.statement.expr instanceof InvokeFunctionExpr) ||
  10509. !(op.statement.expr.fn instanceof ExternalExpr)) {
  10510. // This is a statement, but not an instruction-type call, so not chainable.
  10511. chain = null;
  10512. continue;
  10513. }
  10514. const instruction = op.statement.expr.fn.value;
  10515. if (!CHAINABLE.has(instruction)) {
  10516. // This instruction isn't chainable.
  10517. chain = null;
  10518. continue;
  10519. }
  10520. // This instruction can be chained. It can either be added on to the previous chain (if
  10521. // compatible) or it can be the start of a new chain.
  10522. if (chain !== null && chain.instruction === instruction) {
  10523. // This instruction can be added onto the previous chain.
  10524. const expression = chain.expression.callFn(op.statement.expr.args, op.statement.expr.sourceSpan, op.statement.expr.pure);
  10525. chain.expression = expression;
  10526. chain.op.statement = expression.toStmt();
  10527. OpList.remove(op);
  10528. }
  10529. else {
  10530. // Leave this instruction alone for now, but consider it the start of a new chain.
  10531. chain = {
  10532. op,
  10533. instruction,
  10534. expression: op.statement.expr,
  10535. };
  10536. }
  10537. }
  10538. }
  10539. /**
  10540. * Merges logically sequential `NextContextExpr` operations.
  10541. *
  10542. * `NextContextExpr` can be referenced repeatedly, "popping" the runtime's context stack each time.
  10543. * When two such expressions appear back-to-back, it's possible to merge them together into a single
  10544. * `NextContextExpr` that steps multiple contexts. This merging is possible if all conditions are
  10545. * met:
  10546. *
  10547. * * The result of the `NextContextExpr` that's folded into the subsequent one is not stored (that
  10548. * is, the call is purely side-effectful).
  10549. * * No operations in between them uses the implicit context.
  10550. */
  10551. function phaseMergeNextContext(cpl) {
  10552. for (const view of cpl.views.values()) {
  10553. for (const op of view.create) {
  10554. if (op.kind === OpKind.Listener) {
  10555. mergeNextContextsInOps(op.handlerOps);
  10556. }
  10557. }
  10558. mergeNextContextsInOps(view.update);
  10559. }
  10560. }
  10561. function mergeNextContextsInOps(ops) {
  10562. for (const op of ops) {
  10563. // Look for a candidate operation to maybe merge.
  10564. if (op.kind !== OpKind.Statement || !(op.statement instanceof ExpressionStatement) ||
  10565. !(op.statement.expr instanceof NextContextExpr)) {
  10566. continue;
  10567. }
  10568. const mergeSteps = op.statement.expr.steps;
  10569. // Try to merge this `ir.NextContextExpr`.
  10570. let tryToMerge = true;
  10571. for (let candidate = op.next; candidate.kind !== OpKind.ListEnd && tryToMerge; candidate = candidate.next) {
  10572. visitExpressionsInOp(candidate, (expr, flags) => {
  10573. if (!tryToMerge) {
  10574. // Either we've already merged, or failed to merge.
  10575. return;
  10576. }
  10577. if (flags & VisitorContextFlag.InChildOperation) {
  10578. // We cannot merge into child operations.
  10579. return;
  10580. }
  10581. switch (expr.kind) {
  10582. case ExpressionKind.NextContext:
  10583. // Merge the previous `ir.NextContextExpr` into this one.
  10584. expr.steps += mergeSteps;
  10585. OpList.remove(op);
  10586. tryToMerge = false;
  10587. break;
  10588. case ExpressionKind.GetCurrentView:
  10589. case ExpressionKind.Reference:
  10590. // Can't merge past a dependency on the context.
  10591. tryToMerge = false;
  10592. break;
  10593. }
  10594. });
  10595. }
  10596. }
  10597. }
  10598. /**
  10599. * Run all transformation phases in the correct order against a `ComponentCompilation`. After this
  10600. * processing, the compilation should be in a state where it can be emitted via `emitTemplateFn`.s
  10601. */
  10602. function transformTemplate(cpl) {
  10603. phaseGenerateVariables(cpl);
  10604. phaseResolveNames(cpl);
  10605. phaseResolveContexts(cpl);
  10606. phaseLocalRefs(cpl);
  10607. phaseEmptyElements(cpl);
  10608. phaseConstCollection(cpl);
  10609. phaseSlotAllocation(cpl);
  10610. phaseVarCounting(cpl);
  10611. phaseGenerateAdvance(cpl);
  10612. phaseNaming(cpl);
  10613. phaseVariableOptimization(cpl, { conservative: true });
  10614. phaseMergeNextContext(cpl);
  10615. phaseReify(cpl);
  10616. phaseChaining(cpl);
  10617. }
  10618. /**
  10619. * Compile all views in the given `ComponentCompilation` into the final template function, which may
  10620. * reference constants defined in a `ConstantPool`.
  10621. */
  10622. function emitTemplateFn(tpl, pool) {
  10623. const rootFn = emitView(tpl.root);
  10624. emitChildViews(tpl.root, pool);
  10625. return rootFn;
  10626. }
  10627. function emitChildViews(parent, pool) {
  10628. for (const view of parent.tpl.views.values()) {
  10629. if (view.parent !== parent.xref) {
  10630. continue;
  10631. }
  10632. // Child views are emitted depth-first.
  10633. emitChildViews(view, pool);
  10634. const viewFn = emitView(view);
  10635. pool.statements.push(viewFn.toDeclStmt(viewFn.name));
  10636. }
  10637. }
  10638. /**
  10639. * Emit a template function for an individual `ViewCompilation` (which may be either the root view
  10640. * or an embedded view).
  10641. */
  10642. function emitView(view) {
  10643. if (view.fnName === null) {
  10644. throw new Error(`AssertionError: view ${view.xref} is unnamed`);
  10645. }
  10646. const createStatements = [];
  10647. for (const op of view.create) {
  10648. if (op.kind !== OpKind.Statement) {
  10649. throw new Error(`AssertionError: expected all create ops to have been compiled, but got ${OpKind[op.kind]}`);
  10650. }
  10651. createStatements.push(op.statement);
  10652. }
  10653. const updateStatements = [];
  10654. for (const op of view.update) {
  10655. if (op.kind !== OpKind.Statement) {
  10656. throw new Error(`AssertionError: expected all update ops to have been compiled, but got ${OpKind[op.kind]}`);
  10657. }
  10658. updateStatements.push(op.statement);
  10659. }
  10660. const createCond = maybeGenerateRfBlock(1, createStatements);
  10661. const updateCond = maybeGenerateRfBlock(2, updateStatements);
  10662. return fn([
  10663. new FnParam('rf'),
  10664. new FnParam('ctx'),
  10665. ], [
  10666. ...createCond,
  10667. ...updateCond,
  10668. ],
  10669. /* type */ undefined, /* sourceSpan */ undefined, view.fnName);
  10670. }
  10671. function maybeGenerateRfBlock(flag, statements) {
  10672. if (statements.length === 0) {
  10673. return [];
  10674. }
  10675. return [
  10676. ifStmt(new BinaryOperatorExpr(BinaryOperator.BitwiseAnd, variable('rf'), literal(flag)), statements),
  10677. ];
  10678. }
  10679. /**
  10680. * Compilation-in-progress of a whole component's template, including the main template and any
  10681. * embedded views or host bindings.
  10682. */
  10683. class ComponentCompilation {
  10684. constructor(componentName) {
  10685. this.componentName = componentName;
  10686. /**
  10687. * Tracks the next `ir.XrefId` which can be assigned as template structures are ingested.
  10688. */
  10689. this.nextXrefId = 0;
  10690. /**
  10691. * Map of view IDs to `ViewCompilation`s.
  10692. */
  10693. this.views = new Map();
  10694. /**
  10695. * Constant expressions used by operations within this component's compilation.
  10696. *
  10697. * This will eventually become the `consts` array in the component definition.
  10698. */
  10699. this.consts = [];
  10700. // Allocate the root view.
  10701. const root = new ViewCompilation(this, this.allocateXrefId(), null);
  10702. this.views.set(root.xref, root);
  10703. this.root = root;
  10704. }
  10705. /**
  10706. * Add a `ViewCompilation` for a new embedded view to this compilation.
  10707. */
  10708. allocateView(parent) {
  10709. const view = new ViewCompilation(this, this.allocateXrefId(), parent);
  10710. this.views.set(view.xref, view);
  10711. return view;
  10712. }
  10713. /**
  10714. * Generate a new unique `ir.XrefId`.
  10715. */
  10716. allocateXrefId() {
  10717. return this.nextXrefId++;
  10718. }
  10719. /**
  10720. * Add a constant `o.Expression` to the compilation and return its index in the `consts` array.
  10721. */
  10722. addConst(newConst) {
  10723. for (let idx = 0; idx < this.consts.length; idx++) {
  10724. if (this.consts[idx].isEquivalent(newConst)) {
  10725. return idx;
  10726. }
  10727. }
  10728. const idx = this.consts.length;
  10729. this.consts.push(newConst);
  10730. return idx;
  10731. }
  10732. }
  10733. /**
  10734. * Compilation-in-progress of an individual view within a template.
  10735. */
  10736. class ViewCompilation {
  10737. constructor(tpl, xref, parent) {
  10738. this.tpl = tpl;
  10739. this.xref = xref;
  10740. this.parent = parent;
  10741. /**
  10742. * Name of the function which will be generated for this view.
  10743. *
  10744. * May be `null` if not yet determined.
  10745. */
  10746. this.fnName = null;
  10747. /**
  10748. * List of creation operations for this view.
  10749. *
  10750. * Creation operations may internally contain other operations, including update operations.
  10751. */
  10752. this.create = new OpList();
  10753. /**
  10754. * List of update operations for this view.
  10755. */
  10756. this.update = new OpList();
  10757. /**
  10758. * Map of declared variables available within this view to the property on the context object
  10759. * which they alias.
  10760. */
  10761. this.contextVariables = new Map();
  10762. /**
  10763. * Number of declaration slots used within this view, or `null` if slots have not yet been
  10764. * allocated.
  10765. */
  10766. this.decls = null;
  10767. /**
  10768. * Number of variable slots used within this view, or `null` if variables have not yet been
  10769. * counted.
  10770. */
  10771. this.vars = null;
  10772. }
  10773. /**
  10774. * Iterate over all `ir.Op`s within this view.
  10775. *
  10776. * Some operations may have child operations, which this iterator will visit.
  10777. */
  10778. *ops() {
  10779. for (const op of this.create) {
  10780. yield op;
  10781. if (op.kind === OpKind.Listener) {
  10782. for (const listenerOp of op.handlerOps) {
  10783. yield listenerOp;
  10784. }
  10785. }
  10786. }
  10787. for (const op of this.update) {
  10788. yield op;
  10789. }
  10790. }
  10791. }
  10792. /**
  10793. * Process a template AST and convert it into a `ComponentCompilation` in the intermediate
  10794. * representation.
  10795. */
  10796. function ingest(componentName, template) {
  10797. const cpl = new ComponentCompilation(componentName);
  10798. ingestNodes(cpl.root, template);
  10799. return cpl;
  10800. }
  10801. /**
  10802. * Ingest the nodes of a template AST into the given `ViewCompilation`.
  10803. */
  10804. function ingestNodes(view, template) {
  10805. for (const node of template) {
  10806. if (node instanceof Element$1) {
  10807. ingestElement(view, node);
  10808. }
  10809. else if (node instanceof Template) {
  10810. ingestTemplate(view, node);
  10811. }
  10812. else if (node instanceof Text$3) {
  10813. ingestText(view, node);
  10814. }
  10815. else if (node instanceof BoundText) {
  10816. ingestBoundText(view, node);
  10817. }
  10818. else {
  10819. throw new Error(`Unsupported template node: ${node.constructor.name}`);
  10820. }
  10821. }
  10822. }
  10823. /**
  10824. * Ingest an element AST from the template into the given `ViewCompilation`.
  10825. */
  10826. function ingestElement(view, element) {
  10827. const staticAttributes = {};
  10828. for (const attr of element.attributes) {
  10829. staticAttributes[attr.name] = attr.value;
  10830. }
  10831. const id = view.tpl.allocateXrefId();
  10832. const startOp = createElementStartOp(element.name, id);
  10833. view.create.push(startOp);
  10834. ingestAttributes(startOp, element);
  10835. ingestBindings(view, startOp, element);
  10836. ingestReferences(startOp, element);
  10837. ingestNodes(view, element.children);
  10838. view.create.push(createElementEndOp(id));
  10839. }
  10840. /**
  10841. * Ingest an `ng-template` node from the AST into the given `ViewCompilation`.
  10842. */
  10843. function ingestTemplate(view, tmpl) {
  10844. const childView = view.tpl.allocateView(view.xref);
  10845. // TODO: validate the fallback tag name here.
  10846. const tplOp = createTemplateOp(childView.xref, tmpl.tagName ?? 'ng-template');
  10847. view.create.push(tplOp);
  10848. ingestAttributes(tplOp, tmpl);
  10849. ingestBindings(view, tplOp, tmpl);
  10850. ingestReferences(tplOp, tmpl);
  10851. ingestNodes(childView, tmpl.children);
  10852. for (const { name, value } of tmpl.variables) {
  10853. childView.contextVariables.set(name, value);
  10854. }
  10855. }
  10856. /**
  10857. * Ingest a literal text node from the AST into the given `ViewCompilation`.
  10858. */
  10859. function ingestText(view, text) {
  10860. view.create.push(createTextOp(view.tpl.allocateXrefId(), text.value));
  10861. }
  10862. /**
  10863. * Ingest an interpolated text node from the AST into the given `ViewCompilation`.
  10864. */
  10865. function ingestBoundText(view, text) {
  10866. let value = text.value;
  10867. if (value instanceof ASTWithSource) {
  10868. value = value.ast;
  10869. }
  10870. if (!(value instanceof Interpolation)) {
  10871. throw new Error(`AssertionError: expected Interpolation for BoundText node, got ${value.constructor.name}`);
  10872. }
  10873. const textXref = view.tpl.allocateXrefId();
  10874. view.create.push(createTextOp(textXref, ''));
  10875. view.update.push(createInterpolateTextOp(textXref, value.strings, value.expressions.map(expr => convertAst(expr, view.tpl))));
  10876. }
  10877. /**
  10878. * Convert a template AST expression into an output AST expression.
  10879. */
  10880. function convertAst(ast, cpl) {
  10881. if (ast instanceof ASTWithSource) {
  10882. return convertAst(ast.ast, cpl);
  10883. }
  10884. else if (ast instanceof PropertyRead) {
  10885. if (ast.receiver instanceof ImplicitReceiver) {
  10886. return new LexicalReadExpr(ast.name);
  10887. }
  10888. else {
  10889. return new ReadPropExpr(convertAst(ast.receiver, cpl), ast.name);
  10890. }
  10891. }
  10892. else if (ast instanceof Call) {
  10893. if (ast.receiver instanceof ImplicitReceiver) {
  10894. throw new Error(`Unexpected ImplicitReceiver`);
  10895. }
  10896. else {
  10897. return new InvokeFunctionExpr(convertAst(ast.receiver, cpl), ast.args.map(arg => convertAst(arg, cpl)));
  10898. }
  10899. }
  10900. else if (ast instanceof LiteralPrimitive) {
  10901. return literal(ast.value);
  10902. }
  10903. else if (ast instanceof ThisReceiver) {
  10904. return new ContextExpr(cpl.root.xref);
  10905. }
  10906. else {
  10907. throw new Error(`Unhandled expression type: ${ast.constructor.name}`);
  10908. }
  10909. }
  10910. /**
  10911. * Process all of the attributes on an element-like structure in the template AST and convert them
  10912. * to their IR representation.
  10913. */
  10914. function ingestAttributes(op, element) {
  10915. assertIsElementAttributes(op.attributes);
  10916. for (const attr of element.attributes) {
  10917. op.attributes.add(ElementAttributeKind.Attribute, attr.name, literal(attr.value));
  10918. }
  10919. for (const input of element.inputs) {
  10920. op.attributes.add(ElementAttributeKind.Binding, input.name, null);
  10921. }
  10922. for (const output of element.outputs) {
  10923. op.attributes.add(ElementAttributeKind.Binding, output.name, null);
  10924. }
  10925. if (element instanceof Template) {
  10926. for (const attr of element.templateAttrs) {
  10927. // TODO: what do we do about the value here?
  10928. op.attributes.add(ElementAttributeKind.Template, attr.name, null);
  10929. }
  10930. }
  10931. }
  10932. /**
  10933. * Process all of the bindings on an element-like structure in the template AST and convert them
  10934. * to their IR representation.
  10935. */
  10936. function ingestBindings(view, op, element) {
  10937. if (element instanceof Template) {
  10938. for (const attr of element.templateAttrs) {
  10939. if (typeof attr.value === 'string') {
  10940. // TODO: do we need to handle static attribute bindings here?
  10941. }
  10942. else {
  10943. view.update.push(createPropertyOp(op.xref, attr.name, convertAst(attr.value, view.tpl)));
  10944. }
  10945. }
  10946. }
  10947. else {
  10948. for (const input of element.inputs) {
  10949. view.update.push(createPropertyOp(op.xref, input.name, convertAst(input.value, view.tpl)));
  10950. }
  10951. for (const output of element.outputs) {
  10952. const listenerOp = createListenerOp(op.xref, output.name, op.tag);
  10953. listenerOp.handlerOps.push(createStatementOp(new ReturnStatement(convertAst(output.handler, view.tpl))));
  10954. view.create.push(listenerOp);
  10955. }
  10956. }
  10957. }
  10958. /**
  10959. * Process all of the local references on an element-like structure in the template AST and convert
  10960. * them to their IR representation.
  10961. */
  10962. function ingestReferences(op, element) {
  10963. assertIsArray(op.localRefs);
  10964. for (const { name, value } of element.references) {
  10965. op.localRefs.push({
  10966. name,
  10967. target: value,
  10968. });
  10969. }
  10970. }
  10971. /**
  10972. * Assert that the given value is an array.
  10973. */
  10974. function assertIsArray(value) {
  10975. if (!Array.isArray(value)) {
  10976. throw new Error(`AssertionError: expected an array`);
  10977. }
  10978. }
  10979. const USE_TEMPLATE_PIPELINE = false;
  10980. /**
  10981. * Parses string representation of a style and converts it into object literal.
  10982. *
  10983. * @param value string representation of style as used in the `style` attribute in HTML.
  10984. * Example: `color: red; height: auto`.
  10985. * @returns An array of style property name and value pairs, e.g. `['color', 'red', 'height',
  10986. * 'auto']`
  10987. */
  10988. function parse(value) {
  10989. // we use a string array here instead of a string map
  10990. // because a string-map is not guaranteed to retain the
  10991. // order of the entries whereas a string array can be
  10992. // constructed in a [key, value, key, value] format.
  10993. const styles = [];
  10994. let i = 0;
  10995. let parenDepth = 0;
  10996. let quote = 0 /* Char.QuoteNone */;
  10997. let valueStart = 0;
  10998. let propStart = 0;
  10999. let currentProp = null;
  11000. while (i < value.length) {
  11001. const token = value.charCodeAt(i++);
  11002. switch (token) {
  11003. case 40 /* Char.OpenParen */:
  11004. parenDepth++;
  11005. break;
  11006. case 41 /* Char.CloseParen */:
  11007. parenDepth--;
  11008. break;
  11009. case 39 /* Char.QuoteSingle */:
  11010. // valueStart needs to be there since prop values don't
  11011. // have quotes in CSS
  11012. if (quote === 0 /* Char.QuoteNone */) {
  11013. quote = 39 /* Char.QuoteSingle */;
  11014. }
  11015. else if (quote === 39 /* Char.QuoteSingle */ && value.charCodeAt(i - 1) !== 92 /* Char.BackSlash */) {
  11016. quote = 0 /* Char.QuoteNone */;
  11017. }
  11018. break;
  11019. case 34 /* Char.QuoteDouble */:
  11020. // same logic as above
  11021. if (quote === 0 /* Char.QuoteNone */) {
  11022. quote = 34 /* Char.QuoteDouble */;
  11023. }
  11024. else if (quote === 34 /* Char.QuoteDouble */ && value.charCodeAt(i - 1) !== 92 /* Char.BackSlash */) {
  11025. quote = 0 /* Char.QuoteNone */;
  11026. }
  11027. break;
  11028. case 58 /* Char.Colon */:
  11029. if (!currentProp && parenDepth === 0 && quote === 0 /* Char.QuoteNone */) {
  11030. currentProp = hyphenate(value.substring(propStart, i - 1).trim());
  11031. valueStart = i;
  11032. }
  11033. break;
  11034. case 59 /* Char.Semicolon */:
  11035. if (currentProp && valueStart > 0 && parenDepth === 0 && quote === 0 /* Char.QuoteNone */) {
  11036. const styleVal = value.substring(valueStart, i - 1).trim();
  11037. styles.push(currentProp, styleVal);
  11038. propStart = i;
  11039. valueStart = 0;
  11040. currentProp = null;
  11041. }
  11042. break;
  11043. }
  11044. }
  11045. if (currentProp && valueStart) {
  11046. const styleVal = value.slice(valueStart).trim();
  11047. styles.push(currentProp, styleVal);
  11048. }
  11049. return styles;
  11050. }
  11051. function hyphenate(value) {
  11052. return value
  11053. .replace(/[a-z][A-Z]/g, v => {
  11054. return v.charAt(0) + '-' + v.charAt(1);
  11055. })
  11056. .toLowerCase();
  11057. }
  11058. const IMPORTANT_FLAG = '!important';
  11059. /**
  11060. * Minimum amount of binding slots required in the runtime for style/class bindings.
  11061. *
  11062. * Styling in Angular uses up two slots in the runtime LView/TData data structures to
  11063. * record binding data, property information and metadata.
  11064. *
  11065. * When a binding is registered it will place the following information in the `LView`:
  11066. *
  11067. * slot 1) binding value
  11068. * slot 2) cached value (all other values collected before it in string form)
  11069. *
  11070. * When a binding is registered it will place the following information in the `TData`:
  11071. *
  11072. * slot 1) prop name
  11073. * slot 2) binding index that points to the previous style/class binding (and some extra config
  11074. * values)
  11075. *
  11076. * Let's imagine we have a binding that looks like so:
  11077. *
  11078. * ```
  11079. * <div [style.width]="x" [style.height]="y">
  11080. * ```
  11081. *
  11082. * Our `LView` and `TData` data-structures look like so:
  11083. *
  11084. * ```typescript
  11085. * LView = [
  11086. * // ...
  11087. * x, // value of x
  11088. * "width: x",
  11089. *
  11090. * y, // value of y
  11091. * "width: x; height: y",
  11092. * // ...
  11093. * ];
  11094. *
  11095. * TData = [
  11096. * // ...
  11097. * "width", // binding slot 20
  11098. * 0,
  11099. *
  11100. * "height",
  11101. * 20,
  11102. * // ...
  11103. * ];
  11104. * ```
  11105. *
  11106. * */
  11107. const MIN_STYLING_BINDING_SLOTS_REQUIRED = 2;
  11108. /**
  11109. * Produces creation/update instructions for all styling bindings (class and style)
  11110. *
  11111. * It also produces the creation instruction to register all initial styling values
  11112. * (which are all the static class="..." and style="..." attribute values that exist
  11113. * on an element within a template).
  11114. *
  11115. * The builder class below handles producing instructions for the following cases:
  11116. *
  11117. * - Static style/class attributes (style="..." and class="...")
  11118. * - Dynamic style/class map bindings ([style]="map" and [class]="map|string")
  11119. * - Dynamic style/class property bindings ([style.prop]="exp" and [class.name]="exp")
  11120. *
  11121. * Due to the complex relationship of all of these cases, the instructions generated
  11122. * for these attributes/properties/bindings must be done so in the correct order. The
  11123. * order which these must be generated is as follows:
  11124. *
  11125. * if (createMode) {
  11126. * styling(...)
  11127. * }
  11128. * if (updateMode) {
  11129. * styleMap(...)
  11130. * classMap(...)
  11131. * styleProp(...)
  11132. * classProp(...)
  11133. * }
  11134. *
  11135. * The creation/update methods within the builder class produce these instructions.
  11136. */
  11137. class StylingBuilder {
  11138. constructor(_directiveExpr) {
  11139. this._directiveExpr = _directiveExpr;
  11140. /** Whether or not there are any static styling values present */
  11141. this._hasInitialValues = false;
  11142. /**
  11143. * Whether or not there are any styling bindings present
  11144. * (i.e. `[style]`, `[class]`, `[style.prop]` or `[class.name]`)
  11145. */
  11146. this.hasBindings = false;
  11147. this.hasBindingsWithPipes = false;
  11148. /** the input for [class] (if it exists) */
  11149. this._classMapInput = null;
  11150. /** the input for [style] (if it exists) */
  11151. this._styleMapInput = null;
  11152. /** an array of each [style.prop] input */
  11153. this._singleStyleInputs = null;
  11154. /** an array of each [class.name] input */
  11155. this._singleClassInputs = null;
  11156. this._lastStylingInput = null;
  11157. this._firstStylingInput = null;
  11158. // maps are used instead of hash maps because a Map will
  11159. // retain the ordering of the keys
  11160. /**
  11161. * Represents the location of each style binding in the template
  11162. * (e.g. `<div [style.width]="w" [style.height]="h">` implies
  11163. * that `width=0` and `height=1`)
  11164. */
  11165. this._stylesIndex = new Map();
  11166. /**
  11167. * Represents the location of each class binding in the template
  11168. * (e.g. `<div [class.big]="b" [class.hidden]="h">` implies
  11169. * that `big=0` and `hidden=1`)
  11170. */
  11171. this._classesIndex = new Map();
  11172. this._initialStyleValues = [];
  11173. this._initialClassValues = [];
  11174. }
  11175. /**
  11176. * Registers a given input to the styling builder to be later used when producing AOT code.
  11177. *
  11178. * The code below will only accept the input if it is somehow tied to styling (whether it be
  11179. * style/class bindings or static style/class attributes).
  11180. */
  11181. registerBoundInput(input) {
  11182. // [attr.style] or [attr.class] are skipped in the code below,
  11183. // they should not be treated as styling-based bindings since
  11184. // they are intended to be written directly to the attr and
  11185. // will therefore skip all style/class resolution that is present
  11186. // with style="", [style]="" and [style.prop]="", class="",
  11187. // [class.prop]="". [class]="" assignments
  11188. let binding = null;
  11189. let name = input.name;
  11190. switch (input.type) {
  11191. case 0 /* BindingType.Property */:
  11192. binding = this.registerInputBasedOnName(name, input.value, input.sourceSpan);
  11193. break;
  11194. case 3 /* BindingType.Style */:
  11195. binding = this.registerStyleInput(name, false, input.value, input.sourceSpan, input.unit);
  11196. break;
  11197. case 2 /* BindingType.Class */:
  11198. binding = this.registerClassInput(name, false, input.value, input.sourceSpan);
  11199. break;
  11200. }
  11201. return binding ? true : false;
  11202. }
  11203. registerInputBasedOnName(name, expression, sourceSpan) {
  11204. let binding = null;
  11205. const prefix = name.substring(0, 6);
  11206. const isStyle = name === 'style' || prefix === 'style.' || prefix === 'style!';
  11207. const isClass = !isStyle && (name === 'class' || prefix === 'class.' || prefix === 'class!');
  11208. if (isStyle || isClass) {
  11209. const isMapBased = name.charAt(5) !== '.'; // style.prop or class.prop makes this a no
  11210. const property = name.slice(isMapBased ? 5 : 6); // the dot explains why there's a +1
  11211. if (isStyle) {
  11212. binding = this.registerStyleInput(property, isMapBased, expression, sourceSpan);
  11213. }
  11214. else {
  11215. binding = this.registerClassInput(property, isMapBased, expression, sourceSpan);
  11216. }
  11217. }
  11218. return binding;
  11219. }
  11220. registerStyleInput(name, isMapBased, value, sourceSpan, suffix) {
  11221. if (isEmptyExpression(value)) {
  11222. return null;
  11223. }
  11224. // CSS custom properties are case-sensitive so we shouldn't normalize them.
  11225. // See: https://www.w3.org/TR/css-variables-1/#defining-variables
  11226. if (!isCssCustomProperty(name)) {
  11227. name = hyphenate(name);
  11228. }
  11229. const { property, hasOverrideFlag, suffix: bindingSuffix } = parseProperty(name);
  11230. suffix = typeof suffix === 'string' && suffix.length !== 0 ? suffix : bindingSuffix;
  11231. const entry = { name: property, suffix: suffix, value, sourceSpan, hasOverrideFlag };
  11232. if (isMapBased) {
  11233. this._styleMapInput = entry;
  11234. }
  11235. else {
  11236. (this._singleStyleInputs = this._singleStyleInputs || []).push(entry);
  11237. registerIntoMap(this._stylesIndex, property);
  11238. }
  11239. this._lastStylingInput = entry;
  11240. this._firstStylingInput = this._firstStylingInput || entry;
  11241. this._checkForPipes(value);
  11242. this.hasBindings = true;
  11243. return entry;
  11244. }
  11245. registerClassInput(name, isMapBased, value, sourceSpan) {
  11246. if (isEmptyExpression(value)) {
  11247. return null;
  11248. }
  11249. const { property, hasOverrideFlag } = parseProperty(name);
  11250. const entry = { name: property, value, sourceSpan, hasOverrideFlag, suffix: null };
  11251. if (isMapBased) {
  11252. this._classMapInput = entry;
  11253. }
  11254. else {
  11255. (this._singleClassInputs = this._singleClassInputs || []).push(entry);
  11256. registerIntoMap(this._classesIndex, property);
  11257. }
  11258. this._lastStylingInput = entry;
  11259. this._firstStylingInput = this._firstStylingInput || entry;
  11260. this._checkForPipes(value);
  11261. this.hasBindings = true;
  11262. return entry;
  11263. }
  11264. _checkForPipes(value) {
  11265. if ((value instanceof ASTWithSource) && (value.ast instanceof BindingPipe)) {
  11266. this.hasBindingsWithPipes = true;
  11267. }
  11268. }
  11269. /**
  11270. * Registers the element's static style string value to the builder.
  11271. *
  11272. * @param value the style string (e.g. `width:100px; height:200px;`)
  11273. */
  11274. registerStyleAttr(value) {
  11275. this._initialStyleValues = parse(value);
  11276. this._hasInitialValues = true;
  11277. }
  11278. /**
  11279. * Registers the element's static class string value to the builder.
  11280. *
  11281. * @param value the className string (e.g. `disabled gold zoom`)
  11282. */
  11283. registerClassAttr(value) {
  11284. this._initialClassValues = value.trim().split(/\s+/g);
  11285. this._hasInitialValues = true;
  11286. }
  11287. /**
  11288. * Appends all styling-related expressions to the provided attrs array.
  11289. *
  11290. * @param attrs an existing array where each of the styling expressions
  11291. * will be inserted into.
  11292. */
  11293. populateInitialStylingAttrs(attrs) {
  11294. // [CLASS_MARKER, 'foo', 'bar', 'baz' ...]
  11295. if (this._initialClassValues.length) {
  11296. attrs.push(literal(1 /* AttributeMarker.Classes */));
  11297. for (let i = 0; i < this._initialClassValues.length; i++) {
  11298. attrs.push(literal(this._initialClassValues[i]));
  11299. }
  11300. }
  11301. // [STYLE_MARKER, 'width', '200px', 'height', '100px', ...]
  11302. if (this._initialStyleValues.length) {
  11303. attrs.push(literal(2 /* AttributeMarker.Styles */));
  11304. for (let i = 0; i < this._initialStyleValues.length; i += 2) {
  11305. attrs.push(literal(this._initialStyleValues[i]), literal(this._initialStyleValues[i + 1]));
  11306. }
  11307. }
  11308. }
  11309. /**
  11310. * Builds an instruction with all the expressions and parameters for `elementHostAttrs`.
  11311. *
  11312. * The instruction generation code below is used for producing the AOT statement code which is
  11313. * responsible for registering initial styles (within a directive hostBindings' creation block),
  11314. * as well as any of the provided attribute values, to the directive host element.
  11315. */
  11316. assignHostAttrs(attrs, definitionMap) {
  11317. if (this._directiveExpr && (attrs.length || this._hasInitialValues)) {
  11318. this.populateInitialStylingAttrs(attrs);
  11319. definitionMap.set('hostAttrs', literalArr(attrs));
  11320. }
  11321. }
  11322. /**
  11323. * Builds an instruction with all the expressions and parameters for `classMap`.
  11324. *
  11325. * The instruction data will contain all expressions for `classMap` to function
  11326. * which includes the `[class]` expression params.
  11327. */
  11328. buildClassMapInstruction(valueConverter) {
  11329. if (this._classMapInput) {
  11330. return this._buildMapBasedInstruction(valueConverter, true, this._classMapInput);
  11331. }
  11332. return null;
  11333. }
  11334. /**
  11335. * Builds an instruction with all the expressions and parameters for `styleMap`.
  11336. *
  11337. * The instruction data will contain all expressions for `styleMap` to function
  11338. * which includes the `[style]` expression params.
  11339. */
  11340. buildStyleMapInstruction(valueConverter) {
  11341. if (this._styleMapInput) {
  11342. return this._buildMapBasedInstruction(valueConverter, false, this._styleMapInput);
  11343. }
  11344. return null;
  11345. }
  11346. _buildMapBasedInstruction(valueConverter, isClassBased, stylingInput) {
  11347. // each styling binding value is stored in the LView
  11348. // map-based bindings allocate two slots: one for the
  11349. // previous binding value and another for the previous
  11350. // className or style attribute value.
  11351. let totalBindingSlotsRequired = MIN_STYLING_BINDING_SLOTS_REQUIRED;
  11352. // these values must be outside of the update block so that they can
  11353. // be evaluated (the AST visit call) during creation time so that any
  11354. // pipes can be picked up in time before the template is built
  11355. const mapValue = stylingInput.value.visit(valueConverter);
  11356. let reference;
  11357. if (mapValue instanceof Interpolation) {
  11358. totalBindingSlotsRequired += mapValue.expressions.length;
  11359. reference = isClassBased ? getClassMapInterpolationExpression(mapValue) :
  11360. getStyleMapInterpolationExpression(mapValue);
  11361. }
  11362. else {
  11363. reference = isClassBased ? Identifiers.classMap : Identifiers.styleMap;
  11364. }
  11365. return {
  11366. reference,
  11367. calls: [{
  11368. supportsInterpolation: true,
  11369. sourceSpan: stylingInput.sourceSpan,
  11370. allocateBindingSlots: totalBindingSlotsRequired,
  11371. params: (convertFn) => {
  11372. const convertResult = convertFn(mapValue);
  11373. const params = Array.isArray(convertResult) ? convertResult : [convertResult];
  11374. return params;
  11375. }
  11376. }]
  11377. };
  11378. }
  11379. _buildSingleInputs(reference, inputs, valueConverter, getInterpolationExpressionFn, isClassBased) {
  11380. const instructions = [];
  11381. inputs.forEach(input => {
  11382. const previousInstruction = instructions[instructions.length - 1];
  11383. const value = input.value.visit(valueConverter);
  11384. let referenceForCall = reference;
  11385. // each styling binding value is stored in the LView
  11386. // but there are two values stored for each binding:
  11387. // 1) the value itself
  11388. // 2) an intermediate value (concatenation of style up to this point).
  11389. // We need to store the intermediate value so that we don't allocate
  11390. // the strings on each CD.
  11391. let totalBindingSlotsRequired = MIN_STYLING_BINDING_SLOTS_REQUIRED;
  11392. if (value instanceof Interpolation) {
  11393. totalBindingSlotsRequired += value.expressions.length;
  11394. if (getInterpolationExpressionFn) {
  11395. referenceForCall = getInterpolationExpressionFn(value);
  11396. }
  11397. }
  11398. const call = {
  11399. sourceSpan: input.sourceSpan,
  11400. allocateBindingSlots: totalBindingSlotsRequired,
  11401. supportsInterpolation: !!getInterpolationExpressionFn,
  11402. params: (convertFn) => {
  11403. // params => stylingProp(propName, value, suffix)
  11404. const params = [];
  11405. params.push(literal(input.name));
  11406. const convertResult = convertFn(value);
  11407. if (Array.isArray(convertResult)) {
  11408. params.push(...convertResult);
  11409. }
  11410. else {
  11411. params.push(convertResult);
  11412. }
  11413. // [style.prop] bindings may use suffix values (e.g. px, em, etc...), therefore,
  11414. // if that is detected then we need to pass that in as an optional param.
  11415. if (!isClassBased && input.suffix !== null) {
  11416. params.push(literal(input.suffix));
  11417. }
  11418. return params;
  11419. }
  11420. };
  11421. // If we ended up generating a call to the same instruction as the previous styling property
  11422. // we can chain the calls together safely to save some bytes, otherwise we have to generate
  11423. // a separate instruction call. This is primarily a concern with interpolation instructions
  11424. // where we may start off with one `reference`, but end up using another based on the
  11425. // number of interpolations.
  11426. if (previousInstruction && previousInstruction.reference === referenceForCall) {
  11427. previousInstruction.calls.push(call);
  11428. }
  11429. else {
  11430. instructions.push({ reference: referenceForCall, calls: [call] });
  11431. }
  11432. });
  11433. return instructions;
  11434. }
  11435. _buildClassInputs(valueConverter) {
  11436. if (this._singleClassInputs) {
  11437. return this._buildSingleInputs(Identifiers.classProp, this._singleClassInputs, valueConverter, null, true);
  11438. }
  11439. return [];
  11440. }
  11441. _buildStyleInputs(valueConverter) {
  11442. if (this._singleStyleInputs) {
  11443. return this._buildSingleInputs(Identifiers.styleProp, this._singleStyleInputs, valueConverter, getStylePropInterpolationExpression, false);
  11444. }
  11445. return [];
  11446. }
  11447. /**
  11448. * Constructs all instructions which contain the expressions that will be placed
  11449. * into the update block of a template function or a directive hostBindings function.
  11450. */
  11451. buildUpdateLevelInstructions(valueConverter) {
  11452. const instructions = [];
  11453. if (this.hasBindings) {
  11454. const styleMapInstruction = this.buildStyleMapInstruction(valueConverter);
  11455. if (styleMapInstruction) {
  11456. instructions.push(styleMapInstruction);
  11457. }
  11458. const classMapInstruction = this.buildClassMapInstruction(valueConverter);
  11459. if (classMapInstruction) {
  11460. instructions.push(classMapInstruction);
  11461. }
  11462. instructions.push(...this._buildStyleInputs(valueConverter));
  11463. instructions.push(...this._buildClassInputs(valueConverter));
  11464. }
  11465. return instructions;
  11466. }
  11467. }
  11468. function registerIntoMap(map, key) {
  11469. if (!map.has(key)) {
  11470. map.set(key, map.size);
  11471. }
  11472. }
  11473. function parseProperty(name) {
  11474. let hasOverrideFlag = false;
  11475. const overrideIndex = name.indexOf(IMPORTANT_FLAG);
  11476. if (overrideIndex !== -1) {
  11477. name = overrideIndex > 0 ? name.substring(0, overrideIndex) : '';
  11478. hasOverrideFlag = true;
  11479. }
  11480. let suffix = null;
  11481. let property = name;
  11482. const unitIndex = name.lastIndexOf('.');
  11483. if (unitIndex > 0) {
  11484. suffix = name.slice(unitIndex + 1);
  11485. property = name.substring(0, unitIndex);
  11486. }
  11487. return { property, suffix, hasOverrideFlag };
  11488. }
  11489. /**
  11490. * Gets the instruction to generate for an interpolated class map.
  11491. * @param interpolation An Interpolation AST
  11492. */
  11493. function getClassMapInterpolationExpression(interpolation) {
  11494. switch (getInterpolationArgsLength(interpolation)) {
  11495. case 1:
  11496. return Identifiers.classMap;
  11497. case 3:
  11498. return Identifiers.classMapInterpolate1;
  11499. case 5:
  11500. return Identifiers.classMapInterpolate2;
  11501. case 7:
  11502. return Identifiers.classMapInterpolate3;
  11503. case 9:
  11504. return Identifiers.classMapInterpolate4;
  11505. case 11:
  11506. return Identifiers.classMapInterpolate5;
  11507. case 13:
  11508. return Identifiers.classMapInterpolate6;
  11509. case 15:
  11510. return Identifiers.classMapInterpolate7;
  11511. case 17:
  11512. return Identifiers.classMapInterpolate8;
  11513. default:
  11514. return Identifiers.classMapInterpolateV;
  11515. }
  11516. }
  11517. /**
  11518. * Gets the instruction to generate for an interpolated style map.
  11519. * @param interpolation An Interpolation AST
  11520. */
  11521. function getStyleMapInterpolationExpression(interpolation) {
  11522. switch (getInterpolationArgsLength(interpolation)) {
  11523. case 1:
  11524. return Identifiers.styleMap;
  11525. case 3:
  11526. return Identifiers.styleMapInterpolate1;
  11527. case 5:
  11528. return Identifiers.styleMapInterpolate2;
  11529. case 7:
  11530. return Identifiers.styleMapInterpolate3;
  11531. case 9:
  11532. return Identifiers.styleMapInterpolate4;
  11533. case 11:
  11534. return Identifiers.styleMapInterpolate5;
  11535. case 13:
  11536. return Identifiers.styleMapInterpolate6;
  11537. case 15:
  11538. return Identifiers.styleMapInterpolate7;
  11539. case 17:
  11540. return Identifiers.styleMapInterpolate8;
  11541. default:
  11542. return Identifiers.styleMapInterpolateV;
  11543. }
  11544. }
  11545. /**
  11546. * Gets the instruction to generate for an interpolated style prop.
  11547. * @param interpolation An Interpolation AST
  11548. */
  11549. function getStylePropInterpolationExpression(interpolation) {
  11550. switch (getInterpolationArgsLength(interpolation)) {
  11551. case 1:
  11552. return Identifiers.styleProp;
  11553. case 3:
  11554. return Identifiers.stylePropInterpolate1;
  11555. case 5:
  11556. return Identifiers.stylePropInterpolate2;
  11557. case 7:
  11558. return Identifiers.stylePropInterpolate3;
  11559. case 9:
  11560. return Identifiers.stylePropInterpolate4;
  11561. case 11:
  11562. return Identifiers.stylePropInterpolate5;
  11563. case 13:
  11564. return Identifiers.stylePropInterpolate6;
  11565. case 15:
  11566. return Identifiers.stylePropInterpolate7;
  11567. case 17:
  11568. return Identifiers.stylePropInterpolate8;
  11569. default:
  11570. return Identifiers.stylePropInterpolateV;
  11571. }
  11572. }
  11573. /**
  11574. * Checks whether property name is a custom CSS property.
  11575. * See: https://www.w3.org/TR/css-variables-1
  11576. */
  11577. function isCssCustomProperty(name) {
  11578. return name.startsWith('--');
  11579. }
  11580. function isEmptyExpression(ast) {
  11581. if (ast instanceof ASTWithSource) {
  11582. ast = ast.ast;
  11583. }
  11584. return ast instanceof EmptyExpr;
  11585. }
  11586. var TokenType;
  11587. (function (TokenType) {
  11588. TokenType[TokenType["Character"] = 0] = "Character";
  11589. TokenType[TokenType["Identifier"] = 1] = "Identifier";
  11590. TokenType[TokenType["PrivateIdentifier"] = 2] = "PrivateIdentifier";
  11591. TokenType[TokenType["Keyword"] = 3] = "Keyword";
  11592. TokenType[TokenType["String"] = 4] = "String";
  11593. TokenType[TokenType["Operator"] = 5] = "Operator";
  11594. TokenType[TokenType["Number"] = 6] = "Number";
  11595. TokenType[TokenType["Error"] = 7] = "Error";
  11596. })(TokenType || (TokenType = {}));
  11597. const KEYWORDS = ['var', 'let', 'as', 'null', 'undefined', 'true', 'false', 'if', 'else', 'this'];
  11598. class Lexer {
  11599. tokenize(text) {
  11600. const scanner = new _Scanner(text);
  11601. const tokens = [];
  11602. let token = scanner.scanToken();
  11603. while (token != null) {
  11604. tokens.push(token);
  11605. token = scanner.scanToken();
  11606. }
  11607. return tokens;
  11608. }
  11609. }
  11610. class Token {
  11611. constructor(index, end, type, numValue, strValue) {
  11612. this.index = index;
  11613. this.end = end;
  11614. this.type = type;
  11615. this.numValue = numValue;
  11616. this.strValue = strValue;
  11617. }
  11618. isCharacter(code) {
  11619. return this.type == TokenType.Character && this.numValue == code;
  11620. }
  11621. isNumber() {
  11622. return this.type == TokenType.Number;
  11623. }
  11624. isString() {
  11625. return this.type == TokenType.String;
  11626. }
  11627. isOperator(operator) {
  11628. return this.type == TokenType.Operator && this.strValue == operator;
  11629. }
  11630. isIdentifier() {
  11631. return this.type == TokenType.Identifier;
  11632. }
  11633. isPrivateIdentifier() {
  11634. return this.type == TokenType.PrivateIdentifier;
  11635. }
  11636. isKeyword() {
  11637. return this.type == TokenType.Keyword;
  11638. }
  11639. isKeywordLet() {
  11640. return this.type == TokenType.Keyword && this.strValue == 'let';
  11641. }
  11642. isKeywordAs() {
  11643. return this.type == TokenType.Keyword && this.strValue == 'as';
  11644. }
  11645. isKeywordNull() {
  11646. return this.type == TokenType.Keyword && this.strValue == 'null';
  11647. }
  11648. isKeywordUndefined() {
  11649. return this.type == TokenType.Keyword && this.strValue == 'undefined';
  11650. }
  11651. isKeywordTrue() {
  11652. return this.type == TokenType.Keyword && this.strValue == 'true';
  11653. }
  11654. isKeywordFalse() {
  11655. return this.type == TokenType.Keyword && this.strValue == 'false';
  11656. }
  11657. isKeywordThis() {
  11658. return this.type == TokenType.Keyword && this.strValue == 'this';
  11659. }
  11660. isError() {
  11661. return this.type == TokenType.Error;
  11662. }
  11663. toNumber() {
  11664. return this.type == TokenType.Number ? this.numValue : -1;
  11665. }
  11666. toString() {
  11667. switch (this.type) {
  11668. case TokenType.Character:
  11669. case TokenType.Identifier:
  11670. case TokenType.Keyword:
  11671. case TokenType.Operator:
  11672. case TokenType.PrivateIdentifier:
  11673. case TokenType.String:
  11674. case TokenType.Error:
  11675. return this.strValue;
  11676. case TokenType.Number:
  11677. return this.numValue.toString();
  11678. default:
  11679. return null;
  11680. }
  11681. }
  11682. }
  11683. function newCharacterToken(index, end, code) {
  11684. return new Token(index, end, TokenType.Character, code, String.fromCharCode(code));
  11685. }
  11686. function newIdentifierToken(index, end, text) {
  11687. return new Token(index, end, TokenType.Identifier, 0, text);
  11688. }
  11689. function newPrivateIdentifierToken(index, end, text) {
  11690. return new Token(index, end, TokenType.PrivateIdentifier, 0, text);
  11691. }
  11692. function newKeywordToken(index, end, text) {
  11693. return new Token(index, end, TokenType.Keyword, 0, text);
  11694. }
  11695. function newOperatorToken(index, end, text) {
  11696. return new Token(index, end, TokenType.Operator, 0, text);
  11697. }
  11698. function newStringToken(index, end, text) {
  11699. return new Token(index, end, TokenType.String, 0, text);
  11700. }
  11701. function newNumberToken(index, end, n) {
  11702. return new Token(index, end, TokenType.Number, n, '');
  11703. }
  11704. function newErrorToken(index, end, message) {
  11705. return new Token(index, end, TokenType.Error, 0, message);
  11706. }
  11707. const EOF = new Token(-1, -1, TokenType.Character, 0, '');
  11708. class _Scanner {
  11709. constructor(input) {
  11710. this.input = input;
  11711. this.peek = 0;
  11712. this.index = -1;
  11713. this.length = input.length;
  11714. this.advance();
  11715. }
  11716. advance() {
  11717. this.peek = ++this.index >= this.length ? $EOF : this.input.charCodeAt(this.index);
  11718. }
  11719. scanToken() {
  11720. const input = this.input, length = this.length;
  11721. let peek = this.peek, index = this.index;
  11722. // Skip whitespace.
  11723. while (peek <= $SPACE) {
  11724. if (++index >= length) {
  11725. peek = $EOF;
  11726. break;
  11727. }
  11728. else {
  11729. peek = input.charCodeAt(index);
  11730. }
  11731. }
  11732. this.peek = peek;
  11733. this.index = index;
  11734. if (index >= length) {
  11735. return null;
  11736. }
  11737. // Handle identifiers and numbers.
  11738. if (isIdentifierStart(peek))
  11739. return this.scanIdentifier();
  11740. if (isDigit(peek))
  11741. return this.scanNumber(index);
  11742. const start = index;
  11743. switch (peek) {
  11744. case $PERIOD:
  11745. this.advance();
  11746. return isDigit(this.peek) ? this.scanNumber(start) :
  11747. newCharacterToken(start, this.index, $PERIOD);
  11748. case $LPAREN:
  11749. case $RPAREN:
  11750. case $LBRACE:
  11751. case $RBRACE:
  11752. case $LBRACKET:
  11753. case $RBRACKET:
  11754. case $COMMA:
  11755. case $COLON:
  11756. case $SEMICOLON:
  11757. return this.scanCharacter(start, peek);
  11758. case $SQ:
  11759. case $DQ:
  11760. return this.scanString();
  11761. case $HASH:
  11762. return this.scanPrivateIdentifier();
  11763. case $PLUS:
  11764. case $MINUS:
  11765. case $STAR:
  11766. case $SLASH:
  11767. case $PERCENT:
  11768. case $CARET:
  11769. return this.scanOperator(start, String.fromCharCode(peek));
  11770. case $QUESTION:
  11771. return this.scanQuestion(start);
  11772. case $LT:
  11773. case $GT:
  11774. return this.scanComplexOperator(start, String.fromCharCode(peek), $EQ, '=');
  11775. case $BANG:
  11776. case $EQ:
  11777. return this.scanComplexOperator(start, String.fromCharCode(peek), $EQ, '=', $EQ, '=');
  11778. case $AMPERSAND:
  11779. return this.scanComplexOperator(start, '&', $AMPERSAND, '&');
  11780. case $BAR:
  11781. return this.scanComplexOperator(start, '|', $BAR, '|');
  11782. case $NBSP:
  11783. while (isWhitespace(this.peek))
  11784. this.advance();
  11785. return this.scanToken();
  11786. }
  11787. this.advance();
  11788. return this.error(`Unexpected character [${String.fromCharCode(peek)}]`, 0);
  11789. }
  11790. scanCharacter(start, code) {
  11791. this.advance();
  11792. return newCharacterToken(start, this.index, code);
  11793. }
  11794. scanOperator(start, str) {
  11795. this.advance();
  11796. return newOperatorToken(start, this.index, str);
  11797. }
  11798. /**
  11799. * Tokenize a 2/3 char long operator
  11800. *
  11801. * @param start start index in the expression
  11802. * @param one first symbol (always part of the operator)
  11803. * @param twoCode code point for the second symbol
  11804. * @param two second symbol (part of the operator when the second code point matches)
  11805. * @param threeCode code point for the third symbol
  11806. * @param three third symbol (part of the operator when provided and matches source expression)
  11807. */
  11808. scanComplexOperator(start, one, twoCode, two, threeCode, three) {
  11809. this.advance();
  11810. let str = one;
  11811. if (this.peek == twoCode) {
  11812. this.advance();
  11813. str += two;
  11814. }
  11815. if (threeCode != null && this.peek == threeCode) {
  11816. this.advance();
  11817. str += three;
  11818. }
  11819. return newOperatorToken(start, this.index, str);
  11820. }
  11821. scanIdentifier() {
  11822. const start = this.index;
  11823. this.advance();
  11824. while (isIdentifierPart(this.peek))
  11825. this.advance();
  11826. const str = this.input.substring(start, this.index);
  11827. return KEYWORDS.indexOf(str) > -1 ? newKeywordToken(start, this.index, str) :
  11828. newIdentifierToken(start, this.index, str);
  11829. }
  11830. /** Scans an ECMAScript private identifier. */
  11831. scanPrivateIdentifier() {
  11832. const start = this.index;
  11833. this.advance();
  11834. if (!isIdentifierStart(this.peek)) {
  11835. return this.error('Invalid character [#]', -1);
  11836. }
  11837. while (isIdentifierPart(this.peek))
  11838. this.advance();
  11839. const identifierName = this.input.substring(start, this.index);
  11840. return newPrivateIdentifierToken(start, this.index, identifierName);
  11841. }
  11842. scanNumber(start) {
  11843. let simple = (this.index === start);
  11844. let hasSeparators = false;
  11845. this.advance(); // Skip initial digit.
  11846. while (true) {
  11847. if (isDigit(this.peek)) {
  11848. // Do nothing.
  11849. }
  11850. else if (this.peek === $_) {
  11851. // Separators are only valid when they're surrounded by digits. E.g. `1_0_1` is
  11852. // valid while `_101` and `101_` are not. The separator can't be next to the decimal
  11853. // point or another separator either. Note that it's unlikely that we'll hit a case where
  11854. // the underscore is at the start, because that's a valid identifier and it will be picked
  11855. // up earlier in the parsing. We validate for it anyway just in case.
  11856. if (!isDigit(this.input.charCodeAt(this.index - 1)) ||
  11857. !isDigit(this.input.charCodeAt(this.index + 1))) {
  11858. return this.error('Invalid numeric separator', 0);
  11859. }
  11860. hasSeparators = true;
  11861. }
  11862. else if (this.peek === $PERIOD) {
  11863. simple = false;
  11864. }
  11865. else if (isExponentStart(this.peek)) {
  11866. this.advance();
  11867. if (isExponentSign(this.peek))
  11868. this.advance();
  11869. if (!isDigit(this.peek))
  11870. return this.error('Invalid exponent', -1);
  11871. simple = false;
  11872. }
  11873. else {
  11874. break;
  11875. }
  11876. this.advance();
  11877. }
  11878. let str = this.input.substring(start, this.index);
  11879. if (hasSeparators) {
  11880. str = str.replace(/_/g, '');
  11881. }
  11882. const value = simple ? parseIntAutoRadix(str) : parseFloat(str);
  11883. return newNumberToken(start, this.index, value);
  11884. }
  11885. scanString() {
  11886. const start = this.index;
  11887. const quote = this.peek;
  11888. this.advance(); // Skip initial quote.
  11889. let buffer = '';
  11890. let marker = this.index;
  11891. const input = this.input;
  11892. while (this.peek != quote) {
  11893. if (this.peek == $BACKSLASH) {
  11894. buffer += input.substring(marker, this.index);
  11895. let unescapedCode;
  11896. this.advance(); // mutates this.peek
  11897. // @ts-expect-error see microsoft/TypeScript#9998
  11898. if (this.peek == $u) {
  11899. // 4 character hex code for unicode character.
  11900. const hex = input.substring(this.index + 1, this.index + 5);
  11901. if (/^[0-9a-f]+$/i.test(hex)) {
  11902. unescapedCode = parseInt(hex, 16);
  11903. }
  11904. else {
  11905. return this.error(`Invalid unicode escape [\\u${hex}]`, 0);
  11906. }
  11907. for (let i = 0; i < 5; i++) {
  11908. this.advance();
  11909. }
  11910. }
  11911. else {
  11912. unescapedCode = unescape(this.peek);
  11913. this.advance();
  11914. }
  11915. buffer += String.fromCharCode(unescapedCode);
  11916. marker = this.index;
  11917. }
  11918. else if (this.peek == $EOF) {
  11919. return this.error('Unterminated quote', 0);
  11920. }
  11921. else {
  11922. this.advance();
  11923. }
  11924. }
  11925. const last = input.substring(marker, this.index);
  11926. this.advance(); // Skip terminating quote.
  11927. return newStringToken(start, this.index, buffer + last);
  11928. }
  11929. scanQuestion(start) {
  11930. this.advance();
  11931. let str = '?';
  11932. // Either `a ?? b` or 'a?.b'.
  11933. if (this.peek === $QUESTION || this.peek === $PERIOD) {
  11934. str += this.peek === $PERIOD ? '.' : '?';
  11935. this.advance();
  11936. }
  11937. return newOperatorToken(start, this.index, str);
  11938. }
  11939. error(message, offset) {
  11940. const position = this.index + offset;
  11941. return newErrorToken(position, this.index, `Lexer Error: ${message} at column ${position} in expression [${this.input}]`);
  11942. }
  11943. }
  11944. function isIdentifierStart(code) {
  11945. return ($a <= code && code <= $z) || ($A <= code && code <= $Z) ||
  11946. (code == $_) || (code == $$);
  11947. }
  11948. function isIdentifier(input) {
  11949. if (input.length == 0)
  11950. return false;
  11951. const scanner = new _Scanner(input);
  11952. if (!isIdentifierStart(scanner.peek))
  11953. return false;
  11954. scanner.advance();
  11955. while (scanner.peek !== $EOF) {
  11956. if (!isIdentifierPart(scanner.peek))
  11957. return false;
  11958. scanner.advance();
  11959. }
  11960. return true;
  11961. }
  11962. function isIdentifierPart(code) {
  11963. return isAsciiLetter(code) || isDigit(code) || (code == $_) ||
  11964. (code == $$);
  11965. }
  11966. function isExponentStart(code) {
  11967. return code == $e || code == $E;
  11968. }
  11969. function isExponentSign(code) {
  11970. return code == $MINUS || code == $PLUS;
  11971. }
  11972. function unescape(code) {
  11973. switch (code) {
  11974. case $n:
  11975. return $LF;
  11976. case $f:
  11977. return $FF;
  11978. case $r:
  11979. return $CR;
  11980. case $t:
  11981. return $TAB;
  11982. case $v:
  11983. return $VTAB;
  11984. default:
  11985. return code;
  11986. }
  11987. }
  11988. function parseIntAutoRadix(text) {
  11989. const result = parseInt(text);
  11990. if (isNaN(result)) {
  11991. throw new Error('Invalid integer literal when parsing ' + text);
  11992. }
  11993. return result;
  11994. }
  11995. class SplitInterpolation {
  11996. constructor(strings, expressions, offsets) {
  11997. this.strings = strings;
  11998. this.expressions = expressions;
  11999. this.offsets = offsets;
  12000. }
  12001. }
  12002. class TemplateBindingParseResult {
  12003. constructor(templateBindings, warnings, errors) {
  12004. this.templateBindings = templateBindings;
  12005. this.warnings = warnings;
  12006. this.errors = errors;
  12007. }
  12008. }
  12009. class Parser$1 {
  12010. constructor(_lexer) {
  12011. this._lexer = _lexer;
  12012. this.errors = [];
  12013. }
  12014. parseAction(input, isAssignmentEvent, location, absoluteOffset, interpolationConfig = DEFAULT_INTERPOLATION_CONFIG) {
  12015. this._checkNoInterpolation(input, location, interpolationConfig);
  12016. const sourceToLex = this._stripComments(input);
  12017. const tokens = this._lexer.tokenize(sourceToLex);
  12018. let flags = 1 /* ParseFlags.Action */;
  12019. if (isAssignmentEvent) {
  12020. flags |= 2 /* ParseFlags.AssignmentEvent */;
  12021. }
  12022. const ast = new _ParseAST(input, location, absoluteOffset, tokens, flags, this.errors, 0).parseChain();
  12023. return new ASTWithSource(ast, input, location, absoluteOffset, this.errors);
  12024. }
  12025. parseBinding(input, location, absoluteOffset, interpolationConfig = DEFAULT_INTERPOLATION_CONFIG) {
  12026. const ast = this._parseBindingAst(input, location, absoluteOffset, interpolationConfig);
  12027. return new ASTWithSource(ast, input, location, absoluteOffset, this.errors);
  12028. }
  12029. checkSimpleExpression(ast) {
  12030. const checker = new SimpleExpressionChecker();
  12031. ast.visit(checker);
  12032. return checker.errors;
  12033. }
  12034. parseSimpleBinding(input, location, absoluteOffset, interpolationConfig = DEFAULT_INTERPOLATION_CONFIG) {
  12035. const ast = this._parseBindingAst(input, location, absoluteOffset, interpolationConfig);
  12036. const errors = this.checkSimpleExpression(ast);
  12037. if (errors.length > 0) {
  12038. this._reportError(`Host binding expression cannot contain ${errors.join(' ')}`, input, location);
  12039. }
  12040. return new ASTWithSource(ast, input, location, absoluteOffset, this.errors);
  12041. }
  12042. _reportError(message, input, errLocation, ctxLocation) {
  12043. this.errors.push(new ParserError(message, input, errLocation, ctxLocation));
  12044. }
  12045. _parseBindingAst(input, location, absoluteOffset, interpolationConfig) {
  12046. this._checkNoInterpolation(input, location, interpolationConfig);
  12047. const sourceToLex = this._stripComments(input);
  12048. const tokens = this._lexer.tokenize(sourceToLex);
  12049. return new _ParseAST(input, location, absoluteOffset, tokens, 0 /* ParseFlags.None */, this.errors, 0)
  12050. .parseChain();
  12051. }
  12052. /**
  12053. * Parse microsyntax template expression and return a list of bindings or
  12054. * parsing errors in case the given expression is invalid.
  12055. *
  12056. * For example,
  12057. * ```
  12058. * <div *ngFor="let item of items">
  12059. * ^ ^ absoluteValueOffset for `templateValue`
  12060. * absoluteKeyOffset for `templateKey`
  12061. * ```
  12062. * contains three bindings:
  12063. * 1. ngFor -> null
  12064. * 2. item -> NgForOfContext.$implicit
  12065. * 3. ngForOf -> items
  12066. *
  12067. * This is apparent from the de-sugared template:
  12068. * ```
  12069. * <ng-template ngFor let-item [ngForOf]="items">
  12070. * ```
  12071. *
  12072. * @param templateKey name of directive, without the * prefix. For example: ngIf, ngFor
  12073. * @param templateValue RHS of the microsyntax attribute
  12074. * @param templateUrl template filename if it's external, component filename if it's inline
  12075. * @param absoluteKeyOffset start of the `templateKey`
  12076. * @param absoluteValueOffset start of the `templateValue`
  12077. */
  12078. parseTemplateBindings(templateKey, templateValue, templateUrl, absoluteKeyOffset, absoluteValueOffset) {
  12079. const tokens = this._lexer.tokenize(templateValue);
  12080. const parser = new _ParseAST(templateValue, templateUrl, absoluteValueOffset, tokens, 0 /* ParseFlags.None */, this.errors, 0 /* relative offset */);
  12081. return parser.parseTemplateBindings({
  12082. source: templateKey,
  12083. span: new AbsoluteSourceSpan(absoluteKeyOffset, absoluteKeyOffset + templateKey.length),
  12084. });
  12085. }
  12086. parseInterpolation(input, location, absoluteOffset, interpolatedTokens, interpolationConfig = DEFAULT_INTERPOLATION_CONFIG) {
  12087. const { strings, expressions, offsets } = this.splitInterpolation(input, location, interpolatedTokens, interpolationConfig);
  12088. if (expressions.length === 0)
  12089. return null;
  12090. const expressionNodes = [];
  12091. for (let i = 0; i < expressions.length; ++i) {
  12092. const expressionText = expressions[i].text;
  12093. const sourceToLex = this._stripComments(expressionText);
  12094. const tokens = this._lexer.tokenize(sourceToLex);
  12095. const ast = new _ParseAST(input, location, absoluteOffset, tokens, 0 /* ParseFlags.None */, this.errors, offsets[i])
  12096. .parseChain();
  12097. expressionNodes.push(ast);
  12098. }
  12099. return this.createInterpolationAst(strings.map(s => s.text), expressionNodes, input, location, absoluteOffset);
  12100. }
  12101. /**
  12102. * Similar to `parseInterpolation`, but treats the provided string as a single expression
  12103. * element that would normally appear within the interpolation prefix and suffix (`{{` and `}}`).
  12104. * This is used for parsing the switch expression in ICUs.
  12105. */
  12106. parseInterpolationExpression(expression, location, absoluteOffset) {
  12107. const sourceToLex = this._stripComments(expression);
  12108. const tokens = this._lexer.tokenize(sourceToLex);
  12109. const ast = new _ParseAST(expression, location, absoluteOffset, tokens, 0 /* ParseFlags.None */, this.errors, 0)
  12110. .parseChain();
  12111. const strings = ['', '']; // The prefix and suffix strings are both empty
  12112. return this.createInterpolationAst(strings, [ast], expression, location, absoluteOffset);
  12113. }
  12114. createInterpolationAst(strings, expressions, input, location, absoluteOffset) {
  12115. const span = new ParseSpan(0, input.length);
  12116. const interpolation = new Interpolation(span, span.toAbsolute(absoluteOffset), strings, expressions);
  12117. return new ASTWithSource(interpolation, input, location, absoluteOffset, this.errors);
  12118. }
  12119. /**
  12120. * Splits a string of text into "raw" text segments and expressions present in interpolations in
  12121. * the string.
  12122. * Returns `null` if there are no interpolations, otherwise a
  12123. * `SplitInterpolation` with splits that look like
  12124. * <raw text> <expression> <raw text> ... <raw text> <expression> <raw text>
  12125. */
  12126. splitInterpolation(input, location, interpolatedTokens, interpolationConfig = DEFAULT_INTERPOLATION_CONFIG) {
  12127. const strings = [];
  12128. const expressions = [];
  12129. const offsets = [];
  12130. const inputToTemplateIndexMap = interpolatedTokens ? getIndexMapForOriginalTemplate(interpolatedTokens) : null;
  12131. let i = 0;
  12132. let atInterpolation = false;
  12133. let extendLastString = false;
  12134. let { start: interpStart, end: interpEnd } = interpolationConfig;
  12135. while (i < input.length) {
  12136. if (!atInterpolation) {
  12137. // parse until starting {{
  12138. const start = i;
  12139. i = input.indexOf(interpStart, i);
  12140. if (i === -1) {
  12141. i = input.length;
  12142. }
  12143. const text = input.substring(start, i);
  12144. strings.push({ text, start, end: i });
  12145. atInterpolation = true;
  12146. }
  12147. else {
  12148. // parse from starting {{ to ending }} while ignoring content inside quotes.
  12149. const fullStart = i;
  12150. const exprStart = fullStart + interpStart.length;
  12151. const exprEnd = this._getInterpolationEndIndex(input, interpEnd, exprStart);
  12152. if (exprEnd === -1) {
  12153. // Could not find the end of the interpolation; do not parse an expression.
  12154. // Instead we should extend the content on the last raw string.
  12155. atInterpolation = false;
  12156. extendLastString = true;
  12157. break;
  12158. }
  12159. const fullEnd = exprEnd + interpEnd.length;
  12160. const text = input.substring(exprStart, exprEnd);
  12161. if (text.trim().length === 0) {
  12162. this._reportError('Blank expressions are not allowed in interpolated strings', input, `at column ${i} in`, location);
  12163. }
  12164. expressions.push({ text, start: fullStart, end: fullEnd });
  12165. const startInOriginalTemplate = inputToTemplateIndexMap?.get(fullStart) ?? fullStart;
  12166. const offset = startInOriginalTemplate + interpStart.length;
  12167. offsets.push(offset);
  12168. i = fullEnd;
  12169. atInterpolation = false;
  12170. }
  12171. }
  12172. if (!atInterpolation) {
  12173. // If we are now at a text section, add the remaining content as a raw string.
  12174. if (extendLastString) {
  12175. const piece = strings[strings.length - 1];
  12176. piece.text += input.substring(i);
  12177. piece.end = input.length;
  12178. }
  12179. else {
  12180. strings.push({ text: input.substring(i), start: i, end: input.length });
  12181. }
  12182. }
  12183. return new SplitInterpolation(strings, expressions, offsets);
  12184. }
  12185. wrapLiteralPrimitive(input, location, absoluteOffset) {
  12186. const span = new ParseSpan(0, input == null ? 0 : input.length);
  12187. return new ASTWithSource(new LiteralPrimitive(span, span.toAbsolute(absoluteOffset), input), input, location, absoluteOffset, this.errors);
  12188. }
  12189. _stripComments(input) {
  12190. const i = this._commentStart(input);
  12191. return i != null ? input.substring(0, i) : input;
  12192. }
  12193. _commentStart(input) {
  12194. let outerQuote = null;
  12195. for (let i = 0; i < input.length - 1; i++) {
  12196. const char = input.charCodeAt(i);
  12197. const nextChar = input.charCodeAt(i + 1);
  12198. if (char === $SLASH && nextChar == $SLASH && outerQuote == null)
  12199. return i;
  12200. if (outerQuote === char) {
  12201. outerQuote = null;
  12202. }
  12203. else if (outerQuote == null && isQuote(char)) {
  12204. outerQuote = char;
  12205. }
  12206. }
  12207. return null;
  12208. }
  12209. _checkNoInterpolation(input, location, { start, end }) {
  12210. let startIndex = -1;
  12211. let endIndex = -1;
  12212. for (const charIndex of this._forEachUnquotedChar(input, 0)) {
  12213. if (startIndex === -1) {
  12214. if (input.startsWith(start)) {
  12215. startIndex = charIndex;
  12216. }
  12217. }
  12218. else {
  12219. endIndex = this._getInterpolationEndIndex(input, end, charIndex);
  12220. if (endIndex > -1) {
  12221. break;
  12222. }
  12223. }
  12224. }
  12225. if (startIndex > -1 && endIndex > -1) {
  12226. this._reportError(`Got interpolation (${start}${end}) where expression was expected`, input, `at column ${startIndex} in`, location);
  12227. }
  12228. }
  12229. /**
  12230. * Finds the index of the end of an interpolation expression
  12231. * while ignoring comments and quoted content.
  12232. */
  12233. _getInterpolationEndIndex(input, expressionEnd, start) {
  12234. for (const charIndex of this._forEachUnquotedChar(input, start)) {
  12235. if (input.startsWith(expressionEnd, charIndex)) {
  12236. return charIndex;
  12237. }
  12238. // Nothing else in the expression matters after we've
  12239. // hit a comment so look directly for the end token.
  12240. if (input.startsWith('//', charIndex)) {
  12241. return input.indexOf(expressionEnd, charIndex);
  12242. }
  12243. }
  12244. return -1;
  12245. }
  12246. /**
  12247. * Generator used to iterate over the character indexes of a string that are outside of quotes.
  12248. * @param input String to loop through.
  12249. * @param start Index within the string at which to start.
  12250. */
  12251. *_forEachUnquotedChar(input, start) {
  12252. let currentQuote = null;
  12253. let escapeCount = 0;
  12254. for (let i = start; i < input.length; i++) {
  12255. const char = input[i];
  12256. // Skip the characters inside quotes. Note that we only care about the outer-most
  12257. // quotes matching up and we need to account for escape characters.
  12258. if (isQuote(input.charCodeAt(i)) && (currentQuote === null || currentQuote === char) &&
  12259. escapeCount % 2 === 0) {
  12260. currentQuote = currentQuote === null ? char : null;
  12261. }
  12262. else if (currentQuote === null) {
  12263. yield i;
  12264. }
  12265. escapeCount = char === '\\' ? escapeCount + 1 : 0;
  12266. }
  12267. }
  12268. }
  12269. /** Describes a stateful context an expression parser is in. */
  12270. var ParseContextFlags;
  12271. (function (ParseContextFlags) {
  12272. ParseContextFlags[ParseContextFlags["None"] = 0] = "None";
  12273. /**
  12274. * A Writable context is one in which a value may be written to an lvalue.
  12275. * For example, after we see a property access, we may expect a write to the
  12276. * property via the "=" operator.
  12277. * prop
  12278. * ^ possible "=" after
  12279. */
  12280. ParseContextFlags[ParseContextFlags["Writable"] = 1] = "Writable";
  12281. })(ParseContextFlags || (ParseContextFlags = {}));
  12282. class _ParseAST {
  12283. constructor(input, location, absoluteOffset, tokens, parseFlags, errors, offset) {
  12284. this.input = input;
  12285. this.location = location;
  12286. this.absoluteOffset = absoluteOffset;
  12287. this.tokens = tokens;
  12288. this.parseFlags = parseFlags;
  12289. this.errors = errors;
  12290. this.offset = offset;
  12291. this.rparensExpected = 0;
  12292. this.rbracketsExpected = 0;
  12293. this.rbracesExpected = 0;
  12294. this.context = ParseContextFlags.None;
  12295. // Cache of expression start and input indeces to the absolute source span they map to, used to
  12296. // prevent creating superfluous source spans in `sourceSpan`.
  12297. // A serial of the expression start and input index is used for mapping because both are stateful
  12298. // and may change for subsequent expressions visited by the parser.
  12299. this.sourceSpanCache = new Map();
  12300. this.index = 0;
  12301. }
  12302. peek(offset) {
  12303. const i = this.index + offset;
  12304. return i < this.tokens.length ? this.tokens[i] : EOF;
  12305. }
  12306. get next() {
  12307. return this.peek(0);
  12308. }
  12309. /** Whether all the parser input has been processed. */
  12310. get atEOF() {
  12311. return this.index >= this.tokens.length;
  12312. }
  12313. /**
  12314. * Index of the next token to be processed, or the end of the last token if all have been
  12315. * processed.
  12316. */
  12317. get inputIndex() {
  12318. return this.atEOF ? this.currentEndIndex : this.next.index + this.offset;
  12319. }
  12320. /**
  12321. * End index of the last processed token, or the start of the first token if none have been
  12322. * processed.
  12323. */
  12324. get currentEndIndex() {
  12325. if (this.index > 0) {
  12326. const curToken = this.peek(-1);
  12327. return curToken.end + this.offset;
  12328. }
  12329. // No tokens have been processed yet; return the next token's start or the length of the input
  12330. // if there is no token.
  12331. if (this.tokens.length === 0) {
  12332. return this.input.length + this.offset;
  12333. }
  12334. return this.next.index + this.offset;
  12335. }
  12336. /**
  12337. * Returns the absolute offset of the start of the current token.
  12338. */
  12339. get currentAbsoluteOffset() {
  12340. return this.absoluteOffset + this.inputIndex;
  12341. }
  12342. /**
  12343. * Retrieve a `ParseSpan` from `start` to the current position (or to `artificialEndIndex` if
  12344. * provided).
  12345. *
  12346. * @param start Position from which the `ParseSpan` will start.
  12347. * @param artificialEndIndex Optional ending index to be used if provided (and if greater than the
  12348. * natural ending index)
  12349. */
  12350. span(start, artificialEndIndex) {
  12351. let endIndex = this.currentEndIndex;
  12352. if (artificialEndIndex !== undefined && artificialEndIndex > this.currentEndIndex) {
  12353. endIndex = artificialEndIndex;
  12354. }
  12355. // In some unusual parsing scenarios (like when certain tokens are missing and an `EmptyExpr` is
  12356. // being created), the current token may already be advanced beyond the `currentEndIndex`. This
  12357. // appears to be a deep-seated parser bug.
  12358. //
  12359. // As a workaround for now, swap the start and end indices to ensure a valid `ParseSpan`.
  12360. // TODO(alxhub): fix the bug upstream in the parser state, and remove this workaround.
  12361. if (start > endIndex) {
  12362. const tmp = endIndex;
  12363. endIndex = start;
  12364. start = tmp;
  12365. }
  12366. return new ParseSpan(start, endIndex);
  12367. }
  12368. sourceSpan(start, artificialEndIndex) {
  12369. const serial = `${start}@${this.inputIndex}:${artificialEndIndex}`;
  12370. if (!this.sourceSpanCache.has(serial)) {
  12371. this.sourceSpanCache.set(serial, this.span(start, artificialEndIndex).toAbsolute(this.absoluteOffset));
  12372. }
  12373. return this.sourceSpanCache.get(serial);
  12374. }
  12375. advance() {
  12376. this.index++;
  12377. }
  12378. /**
  12379. * Executes a callback in the provided context.
  12380. */
  12381. withContext(context, cb) {
  12382. this.context |= context;
  12383. const ret = cb();
  12384. this.context ^= context;
  12385. return ret;
  12386. }
  12387. consumeOptionalCharacter(code) {
  12388. if (this.next.isCharacter(code)) {
  12389. this.advance();
  12390. return true;
  12391. }
  12392. else {
  12393. return false;
  12394. }
  12395. }
  12396. peekKeywordLet() {
  12397. return this.next.isKeywordLet();
  12398. }
  12399. peekKeywordAs() {
  12400. return this.next.isKeywordAs();
  12401. }
  12402. /**
  12403. * Consumes an expected character, otherwise emits an error about the missing expected character
  12404. * and skips over the token stream until reaching a recoverable point.
  12405. *
  12406. * See `this.error` and `this.skip` for more details.
  12407. */
  12408. expectCharacter(code) {
  12409. if (this.consumeOptionalCharacter(code))
  12410. return;
  12411. this.error(`Missing expected ${String.fromCharCode(code)}`);
  12412. }
  12413. consumeOptionalOperator(op) {
  12414. if (this.next.isOperator(op)) {
  12415. this.advance();
  12416. return true;
  12417. }
  12418. else {
  12419. return false;
  12420. }
  12421. }
  12422. expectOperator(operator) {
  12423. if (this.consumeOptionalOperator(operator))
  12424. return;
  12425. this.error(`Missing expected operator ${operator}`);
  12426. }
  12427. prettyPrintToken(tok) {
  12428. return tok === EOF ? 'end of input' : `token ${tok}`;
  12429. }
  12430. expectIdentifierOrKeyword() {
  12431. const n = this.next;
  12432. if (!n.isIdentifier() && !n.isKeyword()) {
  12433. if (n.isPrivateIdentifier()) {
  12434. this._reportErrorForPrivateIdentifier(n, 'expected identifier or keyword');
  12435. }
  12436. else {
  12437. this.error(`Unexpected ${this.prettyPrintToken(n)}, expected identifier or keyword`);
  12438. }
  12439. return null;
  12440. }
  12441. this.advance();
  12442. return n.toString();
  12443. }
  12444. expectIdentifierOrKeywordOrString() {
  12445. const n = this.next;
  12446. if (!n.isIdentifier() && !n.isKeyword() && !n.isString()) {
  12447. if (n.isPrivateIdentifier()) {
  12448. this._reportErrorForPrivateIdentifier(n, 'expected identifier, keyword or string');
  12449. }
  12450. else {
  12451. this.error(`Unexpected ${this.prettyPrintToken(n)}, expected identifier, keyword, or string`);
  12452. }
  12453. return '';
  12454. }
  12455. this.advance();
  12456. return n.toString();
  12457. }
  12458. parseChain() {
  12459. const exprs = [];
  12460. const start = this.inputIndex;
  12461. while (this.index < this.tokens.length) {
  12462. const expr = this.parsePipe();
  12463. exprs.push(expr);
  12464. if (this.consumeOptionalCharacter($SEMICOLON)) {
  12465. if (!(this.parseFlags & 1 /* ParseFlags.Action */)) {
  12466. this.error('Binding expression cannot contain chained expression');
  12467. }
  12468. while (this.consumeOptionalCharacter($SEMICOLON)) {
  12469. } // read all semicolons
  12470. }
  12471. else if (this.index < this.tokens.length) {
  12472. const errorIndex = this.index;
  12473. this.error(`Unexpected token '${this.next}'`);
  12474. // The `error` call above will skip ahead to the next recovery point in an attempt to
  12475. // recover part of the expression, but that might be the token we started from which will
  12476. // lead to an infinite loop. If that's the case, break the loop assuming that we can't
  12477. // parse further.
  12478. if (this.index === errorIndex) {
  12479. break;
  12480. }
  12481. }
  12482. }
  12483. if (exprs.length === 0) {
  12484. // We have no expressions so create an empty expression that spans the entire input length
  12485. const artificialStart = this.offset;
  12486. const artificialEnd = this.offset + this.input.length;
  12487. return new EmptyExpr(this.span(artificialStart, artificialEnd), this.sourceSpan(artificialStart, artificialEnd));
  12488. }
  12489. if (exprs.length == 1)
  12490. return exprs[0];
  12491. return new Chain(this.span(start), this.sourceSpan(start), exprs);
  12492. }
  12493. parsePipe() {
  12494. const start = this.inputIndex;
  12495. let result = this.parseExpression();
  12496. if (this.consumeOptionalOperator('|')) {
  12497. if (this.parseFlags & 1 /* ParseFlags.Action */) {
  12498. this.error('Cannot have a pipe in an action expression');
  12499. }
  12500. do {
  12501. const nameStart = this.inputIndex;
  12502. let nameId = this.expectIdentifierOrKeyword();
  12503. let nameSpan;
  12504. let fullSpanEnd = undefined;
  12505. if (nameId !== null) {
  12506. nameSpan = this.sourceSpan(nameStart);
  12507. }
  12508. else {
  12509. // No valid identifier was found, so we'll assume an empty pipe name ('').
  12510. nameId = '';
  12511. // However, there may have been whitespace present between the pipe character and the next
  12512. // token in the sequence (or the end of input). We want to track this whitespace so that
  12513. // the `BindingPipe` we produce covers not just the pipe character, but any trailing
  12514. // whitespace beyond it. Another way of thinking about this is that the zero-length name
  12515. // is assumed to be at the end of any whitespace beyond the pipe character.
  12516. //
  12517. // Therefore, we push the end of the `ParseSpan` for this pipe all the way up to the
  12518. // beginning of the next token, or until the end of input if the next token is EOF.
  12519. fullSpanEnd = this.next.index !== -1 ? this.next.index : this.input.length + this.offset;
  12520. // The `nameSpan` for an empty pipe name is zero-length at the end of any whitespace
  12521. // beyond the pipe character.
  12522. nameSpan = new ParseSpan(fullSpanEnd, fullSpanEnd).toAbsolute(this.absoluteOffset);
  12523. }
  12524. const args = [];
  12525. while (this.consumeOptionalCharacter($COLON)) {
  12526. args.push(this.parseExpression());
  12527. // If there are additional expressions beyond the name, then the artificial end for the
  12528. // name is no longer relevant.
  12529. }
  12530. result = new BindingPipe(this.span(start), this.sourceSpan(start, fullSpanEnd), result, nameId, args, nameSpan);
  12531. } while (this.consumeOptionalOperator('|'));
  12532. }
  12533. return result;
  12534. }
  12535. parseExpression() {
  12536. return this.parseConditional();
  12537. }
  12538. parseConditional() {
  12539. const start = this.inputIndex;
  12540. const result = this.parseLogicalOr();
  12541. if (this.consumeOptionalOperator('?')) {
  12542. const yes = this.parsePipe();
  12543. let no;
  12544. if (!this.consumeOptionalCharacter($COLON)) {
  12545. const end = this.inputIndex;
  12546. const expression = this.input.substring(start, end);
  12547. this.error(`Conditional expression ${expression} requires all 3 expressions`);
  12548. no = new EmptyExpr(this.span(start), this.sourceSpan(start));
  12549. }
  12550. else {
  12551. no = this.parsePipe();
  12552. }
  12553. return new Conditional(this.span(start), this.sourceSpan(start), result, yes, no);
  12554. }
  12555. else {
  12556. return result;
  12557. }
  12558. }
  12559. parseLogicalOr() {
  12560. // '||'
  12561. const start = this.inputIndex;
  12562. let result = this.parseLogicalAnd();
  12563. while (this.consumeOptionalOperator('||')) {
  12564. const right = this.parseLogicalAnd();
  12565. result = new Binary(this.span(start), this.sourceSpan(start), '||', result, right);
  12566. }
  12567. return result;
  12568. }
  12569. parseLogicalAnd() {
  12570. // '&&'
  12571. const start = this.inputIndex;
  12572. let result = this.parseNullishCoalescing();
  12573. while (this.consumeOptionalOperator('&&')) {
  12574. const right = this.parseNullishCoalescing();
  12575. result = new Binary(this.span(start), this.sourceSpan(start), '&&', result, right);
  12576. }
  12577. return result;
  12578. }
  12579. parseNullishCoalescing() {
  12580. // '??'
  12581. const start = this.inputIndex;
  12582. let result = this.parseEquality();
  12583. while (this.consumeOptionalOperator('??')) {
  12584. const right = this.parseEquality();
  12585. result = new Binary(this.span(start), this.sourceSpan(start), '??', result, right);
  12586. }
  12587. return result;
  12588. }
  12589. parseEquality() {
  12590. // '==','!=','===','!=='
  12591. const start = this.inputIndex;
  12592. let result = this.parseRelational();
  12593. while (this.next.type == TokenType.Operator) {
  12594. const operator = this.next.strValue;
  12595. switch (operator) {
  12596. case '==':
  12597. case '===':
  12598. case '!=':
  12599. case '!==':
  12600. this.advance();
  12601. const right = this.parseRelational();
  12602. result = new Binary(this.span(start), this.sourceSpan(start), operator, result, right);
  12603. continue;
  12604. }
  12605. break;
  12606. }
  12607. return result;
  12608. }
  12609. parseRelational() {
  12610. // '<', '>', '<=', '>='
  12611. const start = this.inputIndex;
  12612. let result = this.parseAdditive();
  12613. while (this.next.type == TokenType.Operator) {
  12614. const operator = this.next.strValue;
  12615. switch (operator) {
  12616. case '<':
  12617. case '>':
  12618. case '<=':
  12619. case '>=':
  12620. this.advance();
  12621. const right = this.parseAdditive();
  12622. result = new Binary(this.span(start), this.sourceSpan(start), operator, result, right);
  12623. continue;
  12624. }
  12625. break;
  12626. }
  12627. return result;
  12628. }
  12629. parseAdditive() {
  12630. // '+', '-'
  12631. const start = this.inputIndex;
  12632. let result = this.parseMultiplicative();
  12633. while (this.next.type == TokenType.Operator) {
  12634. const operator = this.next.strValue;
  12635. switch (operator) {
  12636. case '+':
  12637. case '-':
  12638. this.advance();
  12639. let right = this.parseMultiplicative();
  12640. result = new Binary(this.span(start), this.sourceSpan(start), operator, result, right);
  12641. continue;
  12642. }
  12643. break;
  12644. }
  12645. return result;
  12646. }
  12647. parseMultiplicative() {
  12648. // '*', '%', '/'
  12649. const start = this.inputIndex;
  12650. let result = this.parsePrefix();
  12651. while (this.next.type == TokenType.Operator) {
  12652. const operator = this.next.strValue;
  12653. switch (operator) {
  12654. case '*':
  12655. case '%':
  12656. case '/':
  12657. this.advance();
  12658. let right = this.parsePrefix();
  12659. result = new Binary(this.span(start), this.sourceSpan(start), operator, result, right);
  12660. continue;
  12661. }
  12662. break;
  12663. }
  12664. return result;
  12665. }
  12666. parsePrefix() {
  12667. if (this.next.type == TokenType.Operator) {
  12668. const start = this.inputIndex;
  12669. const operator = this.next.strValue;
  12670. let result;
  12671. switch (operator) {
  12672. case '+':
  12673. this.advance();
  12674. result = this.parsePrefix();
  12675. return Unary.createPlus(this.span(start), this.sourceSpan(start), result);
  12676. case '-':
  12677. this.advance();
  12678. result = this.parsePrefix();
  12679. return Unary.createMinus(this.span(start), this.sourceSpan(start), result);
  12680. case '!':
  12681. this.advance();
  12682. result = this.parsePrefix();
  12683. return new PrefixNot(this.span(start), this.sourceSpan(start), result);
  12684. }
  12685. }
  12686. return this.parseCallChain();
  12687. }
  12688. parseCallChain() {
  12689. const start = this.inputIndex;
  12690. let result = this.parsePrimary();
  12691. while (true) {
  12692. if (this.consumeOptionalCharacter($PERIOD)) {
  12693. result = this.parseAccessMember(result, start, false);
  12694. }
  12695. else if (this.consumeOptionalOperator('?.')) {
  12696. if (this.consumeOptionalCharacter($LPAREN)) {
  12697. result = this.parseCall(result, start, true);
  12698. }
  12699. else {
  12700. result = this.consumeOptionalCharacter($LBRACKET) ?
  12701. this.parseKeyedReadOrWrite(result, start, true) :
  12702. this.parseAccessMember(result, start, true);
  12703. }
  12704. }
  12705. else if (this.consumeOptionalCharacter($LBRACKET)) {
  12706. result = this.parseKeyedReadOrWrite(result, start, false);
  12707. }
  12708. else if (this.consumeOptionalCharacter($LPAREN)) {
  12709. result = this.parseCall(result, start, false);
  12710. }
  12711. else if (this.consumeOptionalOperator('!')) {
  12712. result = new NonNullAssert(this.span(start), this.sourceSpan(start), result);
  12713. }
  12714. else {
  12715. return result;
  12716. }
  12717. }
  12718. }
  12719. parsePrimary() {
  12720. const start = this.inputIndex;
  12721. if (this.consumeOptionalCharacter($LPAREN)) {
  12722. this.rparensExpected++;
  12723. const result = this.parsePipe();
  12724. this.rparensExpected--;
  12725. this.expectCharacter($RPAREN);
  12726. return result;
  12727. }
  12728. else if (this.next.isKeywordNull()) {
  12729. this.advance();
  12730. return new LiteralPrimitive(this.span(start), this.sourceSpan(start), null);
  12731. }
  12732. else if (this.next.isKeywordUndefined()) {
  12733. this.advance();
  12734. return new LiteralPrimitive(this.span(start), this.sourceSpan(start), void 0);
  12735. }
  12736. else if (this.next.isKeywordTrue()) {
  12737. this.advance();
  12738. return new LiteralPrimitive(this.span(start), this.sourceSpan(start), true);
  12739. }
  12740. else if (this.next.isKeywordFalse()) {
  12741. this.advance();
  12742. return new LiteralPrimitive(this.span(start), this.sourceSpan(start), false);
  12743. }
  12744. else if (this.next.isKeywordThis()) {
  12745. this.advance();
  12746. return new ThisReceiver(this.span(start), this.sourceSpan(start));
  12747. }
  12748. else if (this.consumeOptionalCharacter($LBRACKET)) {
  12749. this.rbracketsExpected++;
  12750. const elements = this.parseExpressionList($RBRACKET);
  12751. this.rbracketsExpected--;
  12752. this.expectCharacter($RBRACKET);
  12753. return new LiteralArray(this.span(start), this.sourceSpan(start), elements);
  12754. }
  12755. else if (this.next.isCharacter($LBRACE)) {
  12756. return this.parseLiteralMap();
  12757. }
  12758. else if (this.next.isIdentifier()) {
  12759. return this.parseAccessMember(new ImplicitReceiver(this.span(start), this.sourceSpan(start)), start, false);
  12760. }
  12761. else if (this.next.isNumber()) {
  12762. const value = this.next.toNumber();
  12763. this.advance();
  12764. return new LiteralPrimitive(this.span(start), this.sourceSpan(start), value);
  12765. }
  12766. else if (this.next.isString()) {
  12767. const literalValue = this.next.toString();
  12768. this.advance();
  12769. return new LiteralPrimitive(this.span(start), this.sourceSpan(start), literalValue);
  12770. }
  12771. else if (this.next.isPrivateIdentifier()) {
  12772. this._reportErrorForPrivateIdentifier(this.next, null);
  12773. return new EmptyExpr(this.span(start), this.sourceSpan(start));
  12774. }
  12775. else if (this.index >= this.tokens.length) {
  12776. this.error(`Unexpected end of expression: ${this.input}`);
  12777. return new EmptyExpr(this.span(start), this.sourceSpan(start));
  12778. }
  12779. else {
  12780. this.error(`Unexpected token ${this.next}`);
  12781. return new EmptyExpr(this.span(start), this.sourceSpan(start));
  12782. }
  12783. }
  12784. parseExpressionList(terminator) {
  12785. const result = [];
  12786. do {
  12787. if (!this.next.isCharacter(terminator)) {
  12788. result.push(this.parsePipe());
  12789. }
  12790. else {
  12791. break;
  12792. }
  12793. } while (this.consumeOptionalCharacter($COMMA));
  12794. return result;
  12795. }
  12796. parseLiteralMap() {
  12797. const keys = [];
  12798. const values = [];
  12799. const start = this.inputIndex;
  12800. this.expectCharacter($LBRACE);
  12801. if (!this.consumeOptionalCharacter($RBRACE)) {
  12802. this.rbracesExpected++;
  12803. do {
  12804. const keyStart = this.inputIndex;
  12805. const quoted = this.next.isString();
  12806. const key = this.expectIdentifierOrKeywordOrString();
  12807. keys.push({ key, quoted });
  12808. // Properties with quoted keys can't use the shorthand syntax.
  12809. if (quoted) {
  12810. this.expectCharacter($COLON);
  12811. values.push(this.parsePipe());
  12812. }
  12813. else if (this.consumeOptionalCharacter($COLON)) {
  12814. values.push(this.parsePipe());
  12815. }
  12816. else {
  12817. const span = this.span(keyStart);
  12818. const sourceSpan = this.sourceSpan(keyStart);
  12819. values.push(new PropertyRead(span, sourceSpan, sourceSpan, new ImplicitReceiver(span, sourceSpan), key));
  12820. }
  12821. } while (this.consumeOptionalCharacter($COMMA) &&
  12822. !this.next.isCharacter($RBRACE));
  12823. this.rbracesExpected--;
  12824. this.expectCharacter($RBRACE);
  12825. }
  12826. return new LiteralMap(this.span(start), this.sourceSpan(start), keys, values);
  12827. }
  12828. parseAccessMember(readReceiver, start, isSafe) {
  12829. const nameStart = this.inputIndex;
  12830. const id = this.withContext(ParseContextFlags.Writable, () => {
  12831. const id = this.expectIdentifierOrKeyword() ?? '';
  12832. if (id.length === 0) {
  12833. this.error(`Expected identifier for property access`, readReceiver.span.end);
  12834. }
  12835. return id;
  12836. });
  12837. const nameSpan = this.sourceSpan(nameStart);
  12838. let receiver;
  12839. if (isSafe) {
  12840. if (this.consumeOptionalAssignment()) {
  12841. this.error('The \'?.\' operator cannot be used in the assignment');
  12842. receiver = new EmptyExpr(this.span(start), this.sourceSpan(start));
  12843. }
  12844. else {
  12845. receiver = new SafePropertyRead(this.span(start), this.sourceSpan(start), nameSpan, readReceiver, id);
  12846. }
  12847. }
  12848. else {
  12849. if (this.consumeOptionalAssignment()) {
  12850. if (!(this.parseFlags & 1 /* ParseFlags.Action */)) {
  12851. this.error('Bindings cannot contain assignments');
  12852. return new EmptyExpr(this.span(start), this.sourceSpan(start));
  12853. }
  12854. const value = this.parseConditional();
  12855. receiver = new PropertyWrite(this.span(start), this.sourceSpan(start), nameSpan, readReceiver, id, value);
  12856. }
  12857. else {
  12858. receiver =
  12859. new PropertyRead(this.span(start), this.sourceSpan(start), nameSpan, readReceiver, id);
  12860. }
  12861. }
  12862. return receiver;
  12863. }
  12864. parseCall(receiver, start, isSafe) {
  12865. const argumentStart = this.inputIndex;
  12866. this.rparensExpected++;
  12867. const args = this.parseCallArguments();
  12868. const argumentSpan = this.span(argumentStart, this.inputIndex).toAbsolute(this.absoluteOffset);
  12869. this.expectCharacter($RPAREN);
  12870. this.rparensExpected--;
  12871. const span = this.span(start);
  12872. const sourceSpan = this.sourceSpan(start);
  12873. return isSafe ? new SafeCall(span, sourceSpan, receiver, args, argumentSpan) :
  12874. new Call(span, sourceSpan, receiver, args, argumentSpan);
  12875. }
  12876. consumeOptionalAssignment() {
  12877. // When parsing assignment events (originating from two-way-binding aka banana-in-a-box syntax),
  12878. // it is valid for the primary expression to be terminated by the non-null operator. This
  12879. // primary expression is substituted as LHS of the assignment operator to achieve
  12880. // two-way-binding, such that the LHS could be the non-null operator. The grammar doesn't
  12881. // naturally allow for this syntax, so assignment events are parsed specially.
  12882. if ((this.parseFlags & 2 /* ParseFlags.AssignmentEvent */) && this.next.isOperator('!') &&
  12883. this.peek(1).isOperator('=')) {
  12884. // First skip over the ! operator.
  12885. this.advance();
  12886. // Then skip over the = operator, to fully consume the optional assignment operator.
  12887. this.advance();
  12888. return true;
  12889. }
  12890. return this.consumeOptionalOperator('=');
  12891. }
  12892. parseCallArguments() {
  12893. if (this.next.isCharacter($RPAREN))
  12894. return [];
  12895. const positionals = [];
  12896. do {
  12897. positionals.push(this.parsePipe());
  12898. } while (this.consumeOptionalCharacter($COMMA));
  12899. return positionals;
  12900. }
  12901. /**
  12902. * Parses an identifier, a keyword, a string with an optional `-` in between,
  12903. * and returns the string along with its absolute source span.
  12904. */
  12905. expectTemplateBindingKey() {
  12906. let result = '';
  12907. let operatorFound = false;
  12908. const start = this.currentAbsoluteOffset;
  12909. do {
  12910. result += this.expectIdentifierOrKeywordOrString();
  12911. operatorFound = this.consumeOptionalOperator('-');
  12912. if (operatorFound) {
  12913. result += '-';
  12914. }
  12915. } while (operatorFound);
  12916. return {
  12917. source: result,
  12918. span: new AbsoluteSourceSpan(start, start + result.length),
  12919. };
  12920. }
  12921. /**
  12922. * Parse microsyntax template expression and return a list of bindings or
  12923. * parsing errors in case the given expression is invalid.
  12924. *
  12925. * For example,
  12926. * ```
  12927. * <div *ngFor="let item of items; index as i; trackBy: func">
  12928. * ```
  12929. * contains five bindings:
  12930. * 1. ngFor -> null
  12931. * 2. item -> NgForOfContext.$implicit
  12932. * 3. ngForOf -> items
  12933. * 4. i -> NgForOfContext.index
  12934. * 5. ngForTrackBy -> func
  12935. *
  12936. * For a full description of the microsyntax grammar, see
  12937. * https://gist.github.com/mhevery/d3530294cff2e4a1b3fe15ff75d08855
  12938. *
  12939. * @param templateKey name of the microsyntax directive, like ngIf, ngFor,
  12940. * without the *, along with its absolute span.
  12941. */
  12942. parseTemplateBindings(templateKey) {
  12943. const bindings = [];
  12944. // The first binding is for the template key itself
  12945. // In *ngFor="let item of items", key = "ngFor", value = null
  12946. // In *ngIf="cond | pipe", key = "ngIf", value = "cond | pipe"
  12947. bindings.push(...this.parseDirectiveKeywordBindings(templateKey));
  12948. while (this.index < this.tokens.length) {
  12949. // If it starts with 'let', then this must be variable declaration
  12950. const letBinding = this.parseLetBinding();
  12951. if (letBinding) {
  12952. bindings.push(letBinding);
  12953. }
  12954. else {
  12955. // Two possible cases here, either `value "as" key` or
  12956. // "directive-keyword expression". We don't know which case, but both
  12957. // "value" and "directive-keyword" are template binding key, so consume
  12958. // the key first.
  12959. const key = this.expectTemplateBindingKey();
  12960. // Peek at the next token, if it is "as" then this must be variable
  12961. // declaration.
  12962. const binding = this.parseAsBinding(key);
  12963. if (binding) {
  12964. bindings.push(binding);
  12965. }
  12966. else {
  12967. // Otherwise the key must be a directive keyword, like "of". Transform
  12968. // the key to actual key. Eg. of -> ngForOf, trackBy -> ngForTrackBy
  12969. key.source =
  12970. templateKey.source + key.source.charAt(0).toUpperCase() + key.source.substring(1);
  12971. bindings.push(...this.parseDirectiveKeywordBindings(key));
  12972. }
  12973. }
  12974. this.consumeStatementTerminator();
  12975. }
  12976. return new TemplateBindingParseResult(bindings, [] /* warnings */, this.errors);
  12977. }
  12978. parseKeyedReadOrWrite(receiver, start, isSafe) {
  12979. return this.withContext(ParseContextFlags.Writable, () => {
  12980. this.rbracketsExpected++;
  12981. const key = this.parsePipe();
  12982. if (key instanceof EmptyExpr) {
  12983. this.error(`Key access cannot be empty`);
  12984. }
  12985. this.rbracketsExpected--;
  12986. this.expectCharacter($RBRACKET);
  12987. if (this.consumeOptionalOperator('=')) {
  12988. if (isSafe) {
  12989. this.error('The \'?.\' operator cannot be used in the assignment');
  12990. }
  12991. else {
  12992. const value = this.parseConditional();
  12993. return new KeyedWrite(this.span(start), this.sourceSpan(start), receiver, key, value);
  12994. }
  12995. }
  12996. else {
  12997. return isSafe ? new SafeKeyedRead(this.span(start), this.sourceSpan(start), receiver, key) :
  12998. new KeyedRead(this.span(start), this.sourceSpan(start), receiver, key);
  12999. }
  13000. return new EmptyExpr(this.span(start), this.sourceSpan(start));
  13001. });
  13002. }
  13003. /**
  13004. * Parse a directive keyword, followed by a mandatory expression.
  13005. * For example, "of items", "trackBy: func".
  13006. * The bindings are: ngForOf -> items, ngForTrackBy -> func
  13007. * There could be an optional "as" binding that follows the expression.
  13008. * For example,
  13009. * ```
  13010. * *ngFor="let item of items | slice:0:1 as collection".
  13011. * ^^ ^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^
  13012. * keyword bound target optional 'as' binding
  13013. * ```
  13014. *
  13015. * @param key binding key, for example, ngFor, ngIf, ngForOf, along with its
  13016. * absolute span.
  13017. */
  13018. parseDirectiveKeywordBindings(key) {
  13019. const bindings = [];
  13020. this.consumeOptionalCharacter($COLON); // trackBy: trackByFunction
  13021. const value = this.getDirectiveBoundTarget();
  13022. let spanEnd = this.currentAbsoluteOffset;
  13023. // The binding could optionally be followed by "as". For example,
  13024. // *ngIf="cond | pipe as x". In this case, the key in the "as" binding
  13025. // is "x" and the value is the template key itself ("ngIf"). Note that the
  13026. // 'key' in the current context now becomes the "value" in the next binding.
  13027. const asBinding = this.parseAsBinding(key);
  13028. if (!asBinding) {
  13029. this.consumeStatementTerminator();
  13030. spanEnd = this.currentAbsoluteOffset;
  13031. }
  13032. const sourceSpan = new AbsoluteSourceSpan(key.span.start, spanEnd);
  13033. bindings.push(new ExpressionBinding(sourceSpan, key, value));
  13034. if (asBinding) {
  13035. bindings.push(asBinding);
  13036. }
  13037. return bindings;
  13038. }
  13039. /**
  13040. * Return the expression AST for the bound target of a directive keyword
  13041. * binding. For example,
  13042. * ```
  13043. * *ngIf="condition | pipe"
  13044. * ^^^^^^^^^^^^^^^^ bound target for "ngIf"
  13045. * *ngFor="let item of items"
  13046. * ^^^^^ bound target for "ngForOf"
  13047. * ```
  13048. */
  13049. getDirectiveBoundTarget() {
  13050. if (this.next === EOF || this.peekKeywordAs() || this.peekKeywordLet()) {
  13051. return null;
  13052. }
  13053. const ast = this.parsePipe(); // example: "condition | async"
  13054. const { start, end } = ast.span;
  13055. const value = this.input.substring(start, end);
  13056. return new ASTWithSource(ast, value, this.location, this.absoluteOffset + start, this.errors);
  13057. }
  13058. /**
  13059. * Return the binding for a variable declared using `as`. Note that the order
  13060. * of the key-value pair in this declaration is reversed. For example,
  13061. * ```
  13062. * *ngFor="let item of items; index as i"
  13063. * ^^^^^ ^
  13064. * value key
  13065. * ```
  13066. *
  13067. * @param value name of the value in the declaration, "ngIf" in the example
  13068. * above, along with its absolute span.
  13069. */
  13070. parseAsBinding(value) {
  13071. if (!this.peekKeywordAs()) {
  13072. return null;
  13073. }
  13074. this.advance(); // consume the 'as' keyword
  13075. const key = this.expectTemplateBindingKey();
  13076. this.consumeStatementTerminator();
  13077. const sourceSpan = new AbsoluteSourceSpan(value.span.start, this.currentAbsoluteOffset);
  13078. return new VariableBinding(sourceSpan, key, value);
  13079. }
  13080. /**
  13081. * Return the binding for a variable declared using `let`. For example,
  13082. * ```
  13083. * *ngFor="let item of items; let i=index;"
  13084. * ^^^^^^^^ ^^^^^^^^^^^
  13085. * ```
  13086. * In the first binding, `item` is bound to `NgForOfContext.$implicit`.
  13087. * In the second binding, `i` is bound to `NgForOfContext.index`.
  13088. */
  13089. parseLetBinding() {
  13090. if (!this.peekKeywordLet()) {
  13091. return null;
  13092. }
  13093. const spanStart = this.currentAbsoluteOffset;
  13094. this.advance(); // consume the 'let' keyword
  13095. const key = this.expectTemplateBindingKey();
  13096. let value = null;
  13097. if (this.consumeOptionalOperator('=')) {
  13098. value = this.expectTemplateBindingKey();
  13099. }
  13100. this.consumeStatementTerminator();
  13101. const sourceSpan = new AbsoluteSourceSpan(spanStart, this.currentAbsoluteOffset);
  13102. return new VariableBinding(sourceSpan, key, value);
  13103. }
  13104. /**
  13105. * Consume the optional statement terminator: semicolon or comma.
  13106. */
  13107. consumeStatementTerminator() {
  13108. this.consumeOptionalCharacter($SEMICOLON) || this.consumeOptionalCharacter($COMMA);
  13109. }
  13110. /**
  13111. * Records an error and skips over the token stream until reaching a recoverable point. See
  13112. * `this.skip` for more details on token skipping.
  13113. */
  13114. error(message, index = null) {
  13115. this.errors.push(new ParserError(message, this.input, this.locationText(index), this.location));
  13116. this.skip();
  13117. }
  13118. locationText(index = null) {
  13119. if (index == null)
  13120. index = this.index;
  13121. return (index < this.tokens.length) ? `at column ${this.tokens[index].index + 1} in` :
  13122. `at the end of the expression`;
  13123. }
  13124. /**
  13125. * Records an error for an unexpected private identifier being discovered.
  13126. * @param token Token representing a private identifier.
  13127. * @param extraMessage Optional additional message being appended to the error.
  13128. */
  13129. _reportErrorForPrivateIdentifier(token, extraMessage) {
  13130. let errorMessage = `Private identifiers are not supported. Unexpected private identifier: ${token}`;
  13131. if (extraMessage !== null) {
  13132. errorMessage += `, ${extraMessage}`;
  13133. }
  13134. this.error(errorMessage);
  13135. }
  13136. /**
  13137. * Error recovery should skip tokens until it encounters a recovery point.
  13138. *
  13139. * The following are treated as unconditional recovery points:
  13140. * - end of input
  13141. * - ';' (parseChain() is always the root production, and it expects a ';')
  13142. * - '|' (since pipes may be chained and each pipe expression may be treated independently)
  13143. *
  13144. * The following are conditional recovery points:
  13145. * - ')', '}', ']' if one of calling productions is expecting one of these symbols
  13146. * - This allows skip() to recover from errors such as '(a.) + 1' allowing more of the AST to
  13147. * be retained (it doesn't skip any tokens as the ')' is retained because of the '(' begins
  13148. * an '(' <expr> ')' production).
  13149. * The recovery points of grouping symbols must be conditional as they must be skipped if
  13150. * none of the calling productions are not expecting the closing token else we will never
  13151. * make progress in the case of an extraneous group closing symbol (such as a stray ')').
  13152. * That is, we skip a closing symbol if we are not in a grouping production.
  13153. * - '=' in a `Writable` context
  13154. * - In this context, we are able to recover after seeing the `=` operator, which
  13155. * signals the presence of an independent rvalue expression following the `=` operator.
  13156. *
  13157. * If a production expects one of these token it increments the corresponding nesting count,
  13158. * and then decrements it just prior to checking if the token is in the input.
  13159. */
  13160. skip() {
  13161. let n = this.next;
  13162. while (this.index < this.tokens.length && !n.isCharacter($SEMICOLON) &&
  13163. !n.isOperator('|') && (this.rparensExpected <= 0 || !n.isCharacter($RPAREN)) &&
  13164. (this.rbracesExpected <= 0 || !n.isCharacter($RBRACE)) &&
  13165. (this.rbracketsExpected <= 0 || !n.isCharacter($RBRACKET)) &&
  13166. (!(this.context & ParseContextFlags.Writable) || !n.isOperator('='))) {
  13167. if (this.next.isError()) {
  13168. this.errors.push(new ParserError(this.next.toString(), this.input, this.locationText(), this.location));
  13169. }
  13170. this.advance();
  13171. n = this.next;
  13172. }
  13173. }
  13174. }
  13175. class SimpleExpressionChecker extends RecursiveAstVisitor {
  13176. constructor() {
  13177. super(...arguments);
  13178. this.errors = [];
  13179. }
  13180. visitPipe() {
  13181. this.errors.push('pipes');
  13182. }
  13183. }
  13184. /**
  13185. * Computes the real offset in the original template for indexes in an interpolation.
  13186. *
  13187. * Because templates can have encoded HTML entities and the input passed to the parser at this stage
  13188. * of the compiler is the _decoded_ value, we need to compute the real offset using the original
  13189. * encoded values in the interpolated tokens. Note that this is only a special case handling for
  13190. * `MlParserTokenType.ENCODED_ENTITY` token types. All other interpolated tokens are expected to
  13191. * have parts which exactly match the input string for parsing the interpolation.
  13192. *
  13193. * @param interpolatedTokens The tokens for the interpolated value.
  13194. *
  13195. * @returns A map of index locations in the decoded template to indexes in the original template
  13196. */
  13197. function getIndexMapForOriginalTemplate(interpolatedTokens) {
  13198. let offsetMap = new Map();
  13199. let consumedInOriginalTemplate = 0;
  13200. let consumedInInput = 0;
  13201. let tokenIndex = 0;
  13202. while (tokenIndex < interpolatedTokens.length) {
  13203. const currentToken = interpolatedTokens[tokenIndex];
  13204. if (currentToken.type === 9 /* MlParserTokenType.ENCODED_ENTITY */) {
  13205. const [decoded, encoded] = currentToken.parts;
  13206. consumedInOriginalTemplate += encoded.length;
  13207. consumedInInput += decoded.length;
  13208. }
  13209. else {
  13210. const lengthOfParts = currentToken.parts.reduce((sum, current) => sum + current.length, 0);
  13211. consumedInInput += lengthOfParts;
  13212. consumedInOriginalTemplate += lengthOfParts;
  13213. }
  13214. offsetMap.set(consumedInInput, consumedInOriginalTemplate);
  13215. tokenIndex++;
  13216. }
  13217. return offsetMap;
  13218. }
  13219. class NodeWithI18n {
  13220. constructor(sourceSpan, i18n) {
  13221. this.sourceSpan = sourceSpan;
  13222. this.i18n = i18n;
  13223. }
  13224. }
  13225. class Text extends NodeWithI18n {
  13226. constructor(value, sourceSpan, tokens, i18n) {
  13227. super(sourceSpan, i18n);
  13228. this.value = value;
  13229. this.tokens = tokens;
  13230. }
  13231. visit(visitor, context) {
  13232. return visitor.visitText(this, context);
  13233. }
  13234. }
  13235. class Expansion extends NodeWithI18n {
  13236. constructor(switchValue, type, cases, sourceSpan, switchValueSourceSpan, i18n) {
  13237. super(sourceSpan, i18n);
  13238. this.switchValue = switchValue;
  13239. this.type = type;
  13240. this.cases = cases;
  13241. this.switchValueSourceSpan = switchValueSourceSpan;
  13242. }
  13243. visit(visitor, context) {
  13244. return visitor.visitExpansion(this, context);
  13245. }
  13246. }
  13247. class ExpansionCase {
  13248. constructor(value, expression, sourceSpan, valueSourceSpan, expSourceSpan) {
  13249. this.value = value;
  13250. this.expression = expression;
  13251. this.sourceSpan = sourceSpan;
  13252. this.valueSourceSpan = valueSourceSpan;
  13253. this.expSourceSpan = expSourceSpan;
  13254. }
  13255. visit(visitor, context) {
  13256. return visitor.visitExpansionCase(this, context);
  13257. }
  13258. }
  13259. class Attribute extends NodeWithI18n {
  13260. constructor(name, value, sourceSpan, keySpan, valueSpan, valueTokens, i18n) {
  13261. super(sourceSpan, i18n);
  13262. this.name = name;
  13263. this.value = value;
  13264. this.keySpan = keySpan;
  13265. this.valueSpan = valueSpan;
  13266. this.valueTokens = valueTokens;
  13267. }
  13268. visit(visitor, context) {
  13269. return visitor.visitAttribute(this, context);
  13270. }
  13271. }
  13272. class Element extends NodeWithI18n {
  13273. constructor(name, attrs, children, sourceSpan, startSourceSpan, endSourceSpan = null, i18n) {
  13274. super(sourceSpan, i18n);
  13275. this.name = name;
  13276. this.attrs = attrs;
  13277. this.children = children;
  13278. this.startSourceSpan = startSourceSpan;
  13279. this.endSourceSpan = endSourceSpan;
  13280. }
  13281. visit(visitor, context) {
  13282. return visitor.visitElement(this, context);
  13283. }
  13284. }
  13285. class Comment {
  13286. constructor(value, sourceSpan) {
  13287. this.value = value;
  13288. this.sourceSpan = sourceSpan;
  13289. }
  13290. visit(visitor, context) {
  13291. return visitor.visitComment(this, context);
  13292. }
  13293. }
  13294. function visitAll(visitor, nodes, context = null) {
  13295. const result = [];
  13296. const visit = visitor.visit ?
  13297. (ast) => visitor.visit(ast, context) || ast.visit(visitor, context) :
  13298. (ast) => ast.visit(visitor, context);
  13299. nodes.forEach(ast => {
  13300. const astResult = visit(ast);
  13301. if (astResult) {
  13302. result.push(astResult);
  13303. }
  13304. });
  13305. return result;
  13306. }
  13307. class RecursiveVisitor {
  13308. constructor() { }
  13309. visitElement(ast, context) {
  13310. this.visitChildren(context, visit => {
  13311. visit(ast.attrs);
  13312. visit(ast.children);
  13313. });
  13314. }
  13315. visitAttribute(ast, context) { }
  13316. visitText(ast, context) { }
  13317. visitComment(ast, context) { }
  13318. visitExpansion(ast, context) {
  13319. return this.visitChildren(context, visit => {
  13320. visit(ast.cases);
  13321. });
  13322. }
  13323. visitExpansionCase(ast, context) { }
  13324. visitChildren(context, cb) {
  13325. let results = [];
  13326. let t = this;
  13327. function visit(children) {
  13328. if (children)
  13329. results.push(visitAll(t, children, context));
  13330. }
  13331. cb(visit);
  13332. return Array.prototype.concat.apply([], results);
  13333. }
  13334. }
  13335. class ElementSchemaRegistry {
  13336. }
  13337. const BOOLEAN = 'boolean';
  13338. const NUMBER = 'number';
  13339. const STRING = 'string';
  13340. const OBJECT = 'object';
  13341. /**
  13342. * This array represents the DOM schema. It encodes inheritance, properties, and events.
  13343. *
  13344. * ## Overview
  13345. *
  13346. * Each line represents one kind of element. The `element_inheritance` and properties are joined
  13347. * using `element_inheritance|properties` syntax.
  13348. *
  13349. * ## Element Inheritance
  13350. *
  13351. * The `element_inheritance` can be further subdivided as `element1,element2,...^parentElement`.
  13352. * Here the individual elements are separated by `,` (commas). Every element in the list
  13353. * has identical properties.
  13354. *
  13355. * An `element` may inherit additional properties from `parentElement` If no `^parentElement` is
  13356. * specified then `""` (blank) element is assumed.
  13357. *
  13358. * NOTE: The blank element inherits from root `[Element]` element, the super element of all
  13359. * elements.
  13360. *
  13361. * NOTE an element prefix such as `:svg:` has no special meaning to the schema.
  13362. *
  13363. * ## Properties
  13364. *
  13365. * Each element has a set of properties separated by `,` (commas). Each property can be prefixed
  13366. * by a special character designating its type:
  13367. *
  13368. * - (no prefix): property is a string.
  13369. * - `*`: property represents an event.
  13370. * - `!`: property is a boolean.
  13371. * - `#`: property is a number.
  13372. * - `%`: property is an object.
  13373. *
  13374. * ## Query
  13375. *
  13376. * The class creates an internal squas representation which allows to easily answer the query of
  13377. * if a given property exist on a given element.
  13378. *
  13379. * NOTE: We don't yet support querying for types or events.
  13380. * NOTE: This schema is auto extracted from `schema_extractor.ts` located in the test folder,
  13381. * see dom_element_schema_registry_spec.ts
  13382. */
  13383. // =================================================================================================
  13384. // =================================================================================================
  13385. // =========== S T O P - S T O P - S T O P - S T O P - S T O P - S T O P ===========
  13386. // =================================================================================================
  13387. // =================================================================================================
  13388. //
  13389. // DO NOT EDIT THIS DOM SCHEMA WITHOUT A SECURITY REVIEW!
  13390. //
  13391. // Newly added properties must be security reviewed and assigned an appropriate SecurityContext in
  13392. // dom_security_schema.ts. Reach out to mprobst & rjamet for details.
  13393. //
  13394. // =================================================================================================
  13395. const SCHEMA = [
  13396. '[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' +
  13397. /* added manually to avoid breaking changes */
  13398. ',*message,*mozfullscreenchange,*mozfullscreenerror,*mozpointerlockchange,*mozpointerlockerror,*webglcontextcreationerror,*webglcontextlost,*webglcontextrestored',
  13399. '[HTMLElement]^[Element]|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',
  13400. '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',
  13401. 'media^[HTMLElement]|!autoplay,!controls,%controlsList,%crossOrigin,#currentTime,!defaultMuted,#defaultPlaybackRate,!disableRemotePlayback,!loop,!muted,*encrypted,*waitingforkey,#playbackRate,preload,!preservesPitch,src,%srcObject,#volume',
  13402. ':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',
  13403. ':svg:graphics^:svg:|',
  13404. ':svg:animation^:svg:|*begin,*end,*repeat',
  13405. ':svg:geometry^:svg:|',
  13406. ':svg:componentTransferFunction^:svg:|',
  13407. ':svg:gradient^:svg:|',
  13408. ':svg:textContent^:svg:graphics|',
  13409. ':svg:textPositioning^:svg:textContent|',
  13410. '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',
  13411. 'area^[HTMLElement]|alt,coords,download,hash,host,hostname,href,!noHref,password,pathname,ping,port,protocol,referrerPolicy,rel,%relList,search,shape,target,username',
  13412. 'audio^media|',
  13413. 'br^[HTMLElement]|clear',
  13414. 'base^[HTMLElement]|href,target',
  13415. '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',
  13416. 'button^[HTMLElement]|!disabled,formAction,formEnctype,formMethod,!formNoValidate,formTarget,name,type,value',
  13417. 'canvas^[HTMLElement]|#height,#width',
  13418. 'content^[HTMLElement]|select',
  13419. 'dl^[HTMLElement]|!compact',
  13420. 'data^[HTMLElement]|value',
  13421. 'datalist^[HTMLElement]|',
  13422. 'details^[HTMLElement]|!open',
  13423. 'dialog^[HTMLElement]|!open,returnValue',
  13424. 'dir^[HTMLElement]|!compact',
  13425. 'div^[HTMLElement]|align',
  13426. 'embed^[HTMLElement]|align,height,name,src,type,width',
  13427. 'fieldset^[HTMLElement]|!disabled,name',
  13428. 'font^[HTMLElement]|color,face,size',
  13429. 'form^[HTMLElement]|acceptCharset,action,autocomplete,encoding,enctype,method,name,!noValidate,target',
  13430. 'frame^[HTMLElement]|frameBorder,longDesc,marginHeight,marginWidth,name,!noResize,scrolling,src',
  13431. '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',
  13432. 'hr^[HTMLElement]|align,color,!noShade,size,width',
  13433. 'head^[HTMLElement]|',
  13434. 'h1,h2,h3,h4,h5,h6^[HTMLElement]|align',
  13435. 'html^[HTMLElement]|version',
  13436. 'iframe^[HTMLElement]|align,allow,!allowFullscreen,!allowPaymentRequest,csp,frameBorder,height,loading,longDesc,marginHeight,marginWidth,name,referrerPolicy,%sandbox,scrolling,src,srcdoc,width',
  13437. 'img^[HTMLElement]|align,alt,border,%crossOrigin,decoding,#height,#hspace,!isMap,loading,longDesc,lowsrc,name,referrerPolicy,sizes,src,srcset,useMap,#vspace,#width',
  13438. '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',
  13439. 'li^[HTMLElement]|type,#value',
  13440. 'label^[HTMLElement]|htmlFor',
  13441. 'legend^[HTMLElement]|align',
  13442. 'link^[HTMLElement]|as,charset,%crossOrigin,!disabled,href,hreflang,imageSizes,imageSrcset,integrity,media,referrerPolicy,rel,%relList,rev,%sizes,target,type',
  13443. 'map^[HTMLElement]|name',
  13444. 'marquee^[HTMLElement]|behavior,bgColor,direction,height,#hspace,#loop,#scrollAmount,#scrollDelay,!trueSpeed,#vspace,width',
  13445. 'menu^[HTMLElement]|!compact',
  13446. 'meta^[HTMLElement]|content,httpEquiv,media,name,scheme',
  13447. 'meter^[HTMLElement]|#high,#low,#max,#min,#optimum,#value',
  13448. 'ins,del^[HTMLElement]|cite,dateTime',
  13449. 'ol^[HTMLElement]|!compact,!reversed,#start,type',
  13450. 'object^[HTMLElement]|align,archive,border,code,codeBase,codeType,data,!declare,height,#hspace,name,standby,type,useMap,#vspace,width',
  13451. 'optgroup^[HTMLElement]|!disabled,label',
  13452. 'option^[HTMLElement]|!defaultSelected,!disabled,label,!selected,text,value',
  13453. 'output^[HTMLElement]|defaultValue,%htmlFor,name,value',
  13454. 'p^[HTMLElement]|align',
  13455. 'param^[HTMLElement]|name,type,value,valueType',
  13456. 'picture^[HTMLElement]|',
  13457. 'pre^[HTMLElement]|#width',
  13458. 'progress^[HTMLElement]|#max,#value',
  13459. 'q,blockquote,cite^[HTMLElement]|',
  13460. 'script^[HTMLElement]|!async,charset,%crossOrigin,!defer,event,htmlFor,integrity,!noModule,%referrerPolicy,src,text,type',
  13461. 'select^[HTMLElement]|autocomplete,!disabled,#length,!multiple,name,!required,#selectedIndex,#size,value',
  13462. 'slot^[HTMLElement]|name',
  13463. 'source^[HTMLElement]|#height,media,sizes,src,srcset,type,#width',
  13464. 'span^[HTMLElement]|',
  13465. 'style^[HTMLElement]|!disabled,media,type',
  13466. 'caption^[HTMLElement]|align',
  13467. 'th,td^[HTMLElement]|abbr,align,axis,bgColor,ch,chOff,#colSpan,headers,height,!noWrap,#rowSpan,scope,vAlign,width',
  13468. 'col,colgroup^[HTMLElement]|align,ch,chOff,#span,vAlign,width',
  13469. 'table^[HTMLElement]|align,bgColor,border,%caption,cellPadding,cellSpacing,frame,rules,summary,%tFoot,%tHead,width',
  13470. 'tr^[HTMLElement]|align,bgColor,ch,chOff,vAlign',
  13471. 'tfoot,thead,tbody^[HTMLElement]|align,ch,chOff,vAlign',
  13472. 'template^[HTMLElement]|',
  13473. 'textarea^[HTMLElement]|autocomplete,#cols,defaultValue,dirName,!disabled,#maxLength,#minLength,name,placeholder,!readOnly,!required,#rows,selectionDirection,#selectionEnd,#selectionStart,value,wrap',
  13474. 'time^[HTMLElement]|dateTime',
  13475. 'title^[HTMLElement]|text',
  13476. 'track^[HTMLElement]|!default,kind,label,src,srclang',
  13477. 'ul^[HTMLElement]|!compact,type',
  13478. 'unknown^[HTMLElement]|',
  13479. 'video^media|!disablePictureInPicture,#height,*enterpictureinpicture,*leavepictureinpicture,!playsInline,poster,#width',
  13480. ':svg:a^:svg:graphics|',
  13481. ':svg:animate^:svg:animation|',
  13482. ':svg:animateMotion^:svg:animation|',
  13483. ':svg:animateTransform^:svg:animation|',
  13484. ':svg:circle^:svg:geometry|',
  13485. ':svg:clipPath^:svg:graphics|',
  13486. ':svg:defs^:svg:graphics|',
  13487. ':svg:desc^:svg:|',
  13488. ':svg:discard^:svg:|',
  13489. ':svg:ellipse^:svg:geometry|',
  13490. ':svg:feBlend^:svg:|',
  13491. ':svg:feColorMatrix^:svg:|',
  13492. ':svg:feComponentTransfer^:svg:|',
  13493. ':svg:feComposite^:svg:|',
  13494. ':svg:feConvolveMatrix^:svg:|',
  13495. ':svg:feDiffuseLighting^:svg:|',
  13496. ':svg:feDisplacementMap^:svg:|',
  13497. ':svg:feDistantLight^:svg:|',
  13498. ':svg:feDropShadow^:svg:|',
  13499. ':svg:feFlood^:svg:|',
  13500. ':svg:feFuncA^:svg:componentTransferFunction|',
  13501. ':svg:feFuncB^:svg:componentTransferFunction|',
  13502. ':svg:feFuncG^:svg:componentTransferFunction|',
  13503. ':svg:feFuncR^:svg:componentTransferFunction|',
  13504. ':svg:feGaussianBlur^:svg:|',
  13505. ':svg:feImage^:svg:|',
  13506. ':svg:feMerge^:svg:|',
  13507. ':svg:feMergeNode^:svg:|',
  13508. ':svg:feMorphology^:svg:|',
  13509. ':svg:feOffset^:svg:|',
  13510. ':svg:fePointLight^:svg:|',
  13511. ':svg:feSpecularLighting^:svg:|',
  13512. ':svg:feSpotLight^:svg:|',
  13513. ':svg:feTile^:svg:|',
  13514. ':svg:feTurbulence^:svg:|',
  13515. ':svg:filter^:svg:|',
  13516. ':svg:foreignObject^:svg:graphics|',
  13517. ':svg:g^:svg:graphics|',
  13518. ':svg:image^:svg:graphics|decoding',
  13519. ':svg:line^:svg:geometry|',
  13520. ':svg:linearGradient^:svg:gradient|',
  13521. ':svg:mpath^:svg:|',
  13522. ':svg:marker^:svg:|',
  13523. ':svg:mask^:svg:|',
  13524. ':svg:metadata^:svg:|',
  13525. ':svg:path^:svg:geometry|',
  13526. ':svg:pattern^:svg:|',
  13527. ':svg:polygon^:svg:geometry|',
  13528. ':svg:polyline^:svg:geometry|',
  13529. ':svg:radialGradient^:svg:gradient|',
  13530. ':svg:rect^:svg:geometry|',
  13531. ':svg:svg^:svg:graphics|#currentScale,#zoomAndPan',
  13532. ':svg:script^:svg:|type',
  13533. ':svg:set^:svg:animation|',
  13534. ':svg:stop^:svg:|',
  13535. ':svg:style^:svg:|!disabled,media,title,type',
  13536. ':svg:switch^:svg:graphics|',
  13537. ':svg:symbol^:svg:|',
  13538. ':svg:tspan^:svg:textPositioning|',
  13539. ':svg:text^:svg:textPositioning|',
  13540. ':svg:textPath^:svg:textContent|',
  13541. ':svg:title^:svg:|',
  13542. ':svg:use^:svg:graphics|',
  13543. ':svg:view^:svg:|#zoomAndPan',
  13544. 'data^[HTMLElement]|value',
  13545. 'keygen^[HTMLElement]|!autofocus,challenge,!disabled,form,keytype,name',
  13546. 'menuitem^[HTMLElement]|type,label,icon,!disabled,!checked,radiogroup,!default',
  13547. 'summary^[HTMLElement]|',
  13548. 'time^[HTMLElement]|dateTime',
  13549. ':svg:cursor^:svg:|',
  13550. ];
  13551. const _ATTR_TO_PROP = new Map(Object.entries({
  13552. 'class': 'className',
  13553. 'for': 'htmlFor',
  13554. 'formaction': 'formAction',
  13555. 'innerHtml': 'innerHTML',
  13556. 'readonly': 'readOnly',
  13557. 'tabindex': 'tabIndex',
  13558. }));
  13559. // Invert _ATTR_TO_PROP.
  13560. const _PROP_TO_ATTR = Array.from(_ATTR_TO_PROP).reduce((inverted, [propertyName, attributeName]) => {
  13561. inverted.set(propertyName, attributeName);
  13562. return inverted;
  13563. }, new Map());
  13564. class DomElementSchemaRegistry extends ElementSchemaRegistry {
  13565. constructor() {
  13566. super();
  13567. this._schema = new Map();
  13568. // We don't allow binding to events for security reasons. Allowing event bindings would almost
  13569. // certainly introduce bad XSS vulnerabilities. Instead, we store events in a separate schema.
  13570. this._eventSchema = new Map;
  13571. SCHEMA.forEach(encodedType => {
  13572. const type = new Map();
  13573. const events = new Set();
  13574. const [strType, strProperties] = encodedType.split('|');
  13575. const properties = strProperties.split(',');
  13576. const [typeNames, superName] = strType.split('^');
  13577. typeNames.split(',').forEach(tag => {
  13578. this._schema.set(tag.toLowerCase(), type);
  13579. this._eventSchema.set(tag.toLowerCase(), events);
  13580. });
  13581. const superType = superName && this._schema.get(superName.toLowerCase());
  13582. if (superType) {
  13583. for (const [prop, value] of superType) {
  13584. type.set(prop, value);
  13585. }
  13586. for (const superEvent of this._eventSchema.get(superName.toLowerCase())) {
  13587. events.add(superEvent);
  13588. }
  13589. }
  13590. properties.forEach((property) => {
  13591. if (property.length > 0) {
  13592. switch (property[0]) {
  13593. case '*':
  13594. events.add(property.substring(1));
  13595. break;
  13596. case '!':
  13597. type.set(property.substring(1), BOOLEAN);
  13598. break;
  13599. case '#':
  13600. type.set(property.substring(1), NUMBER);
  13601. break;
  13602. case '%':
  13603. type.set(property.substring(1), OBJECT);
  13604. break;
  13605. default:
  13606. type.set(property, STRING);
  13607. }
  13608. }
  13609. });
  13610. });
  13611. }
  13612. hasProperty(tagName, propName, schemaMetas) {
  13613. if (schemaMetas.some((schema) => schema.name === NO_ERRORS_SCHEMA.name)) {
  13614. return true;
  13615. }
  13616. if (tagName.indexOf('-') > -1) {
  13617. if (isNgContainer(tagName) || isNgContent(tagName)) {
  13618. return false;
  13619. }
  13620. if (schemaMetas.some((schema) => schema.name === CUSTOM_ELEMENTS_SCHEMA.name)) {
  13621. // Can't tell now as we don't know which properties a custom element will get
  13622. // once it is instantiated
  13623. return true;
  13624. }
  13625. }
  13626. const elementProperties = this._schema.get(tagName.toLowerCase()) || this._schema.get('unknown');
  13627. return elementProperties.has(propName);
  13628. }
  13629. hasElement(tagName, schemaMetas) {
  13630. if (schemaMetas.some((schema) => schema.name === NO_ERRORS_SCHEMA.name)) {
  13631. return true;
  13632. }
  13633. if (tagName.indexOf('-') > -1) {
  13634. if (isNgContainer(tagName) || isNgContent(tagName)) {
  13635. return true;
  13636. }
  13637. if (schemaMetas.some((schema) => schema.name === CUSTOM_ELEMENTS_SCHEMA.name)) {
  13638. // Allow any custom elements
  13639. return true;
  13640. }
  13641. }
  13642. return this._schema.has(tagName.toLowerCase());
  13643. }
  13644. /**
  13645. * securityContext returns the security context for the given property on the given DOM tag.
  13646. *
  13647. * Tag and property name are statically known and cannot change at runtime, i.e. it is not
  13648. * possible to bind a value into a changing attribute or tag name.
  13649. *
  13650. * The filtering is based on a list of allowed tags|attributes. All attributes in the schema
  13651. * above are assumed to have the 'NONE' security context, i.e. that they are safe inert
  13652. * string values. Only specific well known attack vectors are assigned their appropriate context.
  13653. */
  13654. securityContext(tagName, propName, isAttribute) {
  13655. if (isAttribute) {
  13656. // NB: For security purposes, use the mapped property name, not the attribute name.
  13657. propName = this.getMappedPropName(propName);
  13658. }
  13659. // Make sure comparisons are case insensitive, so that case differences between attribute and
  13660. // property names do not have a security impact.
  13661. tagName = tagName.toLowerCase();
  13662. propName = propName.toLowerCase();
  13663. let ctx = SECURITY_SCHEMA()[tagName + '|' + propName];
  13664. if (ctx) {
  13665. return ctx;
  13666. }
  13667. ctx = SECURITY_SCHEMA()['*|' + propName];
  13668. return ctx ? ctx : SecurityContext.NONE;
  13669. }
  13670. getMappedPropName(propName) {
  13671. return _ATTR_TO_PROP.get(propName) ?? propName;
  13672. }
  13673. getDefaultComponentElementName() {
  13674. return 'ng-component';
  13675. }
  13676. validateProperty(name) {
  13677. if (name.toLowerCase().startsWith('on')) {
  13678. const msg = `Binding to event property '${name}' is disallowed for security reasons, ` +
  13679. `please use (${name.slice(2)})=...` +
  13680. `\nIf '${name}' is a directive input, make sure the directive is imported by the` +
  13681. ` current module.`;
  13682. return { error: true, msg: msg };
  13683. }
  13684. else {
  13685. return { error: false };
  13686. }
  13687. }
  13688. validateAttribute(name) {
  13689. if (name.toLowerCase().startsWith('on')) {
  13690. const msg = `Binding to event attribute '${name}' is disallowed for security reasons, ` +
  13691. `please use (${name.slice(2)})=...`;
  13692. return { error: true, msg: msg };
  13693. }
  13694. else {
  13695. return { error: false };
  13696. }
  13697. }
  13698. allKnownElementNames() {
  13699. return Array.from(this._schema.keys());
  13700. }
  13701. allKnownAttributesOfElement(tagName) {
  13702. const elementProperties = this._schema.get(tagName.toLowerCase()) || this._schema.get('unknown');
  13703. // Convert properties to attributes.
  13704. return Array.from(elementProperties.keys()).map(prop => _PROP_TO_ATTR.get(prop) ?? prop);
  13705. }
  13706. allKnownEventsOfElement(tagName) {
  13707. return Array.from(this._eventSchema.get(tagName.toLowerCase()) ?? []);
  13708. }
  13709. normalizeAnimationStyleProperty(propName) {
  13710. return dashCaseToCamelCase(propName);
  13711. }
  13712. normalizeAnimationStyleValue(camelCaseProp, userProvidedProp, val) {
  13713. let unit = '';
  13714. const strVal = val.toString().trim();
  13715. let errorMsg = null;
  13716. if (_isPixelDimensionStyle(camelCaseProp) && val !== 0 && val !== '0') {
  13717. if (typeof val === 'number') {
  13718. unit = 'px';
  13719. }
  13720. else {
  13721. const valAndSuffixMatch = val.match(/^[+-]?[\d\.]+([a-z]*)$/);
  13722. if (valAndSuffixMatch && valAndSuffixMatch[1].length == 0) {
  13723. errorMsg = `Please provide a CSS unit value for ${userProvidedProp}:${val}`;
  13724. }
  13725. }
  13726. }
  13727. return { error: errorMsg, value: strVal + unit };
  13728. }
  13729. }
  13730. function _isPixelDimensionStyle(prop) {
  13731. switch (prop) {
  13732. case 'width':
  13733. case 'height':
  13734. case 'minWidth':
  13735. case 'minHeight':
  13736. case 'maxWidth':
  13737. case 'maxHeight':
  13738. case 'left':
  13739. case 'top':
  13740. case 'bottom':
  13741. case 'right':
  13742. case 'fontSize':
  13743. case 'outlineWidth':
  13744. case 'outlineOffset':
  13745. case 'paddingTop':
  13746. case 'paddingLeft':
  13747. case 'paddingBottom':
  13748. case 'paddingRight':
  13749. case 'marginTop':
  13750. case 'marginLeft':
  13751. case 'marginBottom':
  13752. case 'marginRight':
  13753. case 'borderRadius':
  13754. case 'borderWidth':
  13755. case 'borderTopWidth':
  13756. case 'borderLeftWidth':
  13757. case 'borderRightWidth':
  13758. case 'borderBottomWidth':
  13759. case 'textIndent':
  13760. return true;
  13761. default:
  13762. return false;
  13763. }
  13764. }
  13765. class HtmlTagDefinition {
  13766. constructor({ closedByChildren, implicitNamespacePrefix, contentType = TagContentType.PARSABLE_DATA, closedByParent = false, isVoid = false, ignoreFirstLf = false, preventNamespaceInheritance = false, canSelfClose = false, } = {}) {
  13767. this.closedByChildren = {};
  13768. this.closedByParent = false;
  13769. if (closedByChildren && closedByChildren.length > 0) {
  13770. closedByChildren.forEach(tagName => this.closedByChildren[tagName] = true);
  13771. }
  13772. this.isVoid = isVoid;
  13773. this.closedByParent = closedByParent || isVoid;
  13774. this.implicitNamespacePrefix = implicitNamespacePrefix || null;
  13775. this.contentType = contentType;
  13776. this.ignoreFirstLf = ignoreFirstLf;
  13777. this.preventNamespaceInheritance = preventNamespaceInheritance;
  13778. this.canSelfClose = canSelfClose ?? isVoid;
  13779. }
  13780. isClosedByChild(name) {
  13781. return this.isVoid || name.toLowerCase() in this.closedByChildren;
  13782. }
  13783. getContentType(prefix) {
  13784. if (typeof this.contentType === 'object') {
  13785. const overrideType = prefix === undefined ? undefined : this.contentType[prefix];
  13786. return overrideType ?? this.contentType.default;
  13787. }
  13788. return this.contentType;
  13789. }
  13790. }
  13791. let DEFAULT_TAG_DEFINITION;
  13792. // see https://www.w3.org/TR/html51/syntax.html#optional-tags
  13793. // This implementation does not fully conform to the HTML5 spec.
  13794. let TAG_DEFINITIONS;
  13795. function getHtmlTagDefinition(tagName) {
  13796. if (!TAG_DEFINITIONS) {
  13797. DEFAULT_TAG_DEFINITION = new HtmlTagDefinition({ canSelfClose: true });
  13798. TAG_DEFINITIONS = {
  13799. 'base': new HtmlTagDefinition({ isVoid: true }),
  13800. 'meta': new HtmlTagDefinition({ isVoid: true }),
  13801. 'area': new HtmlTagDefinition({ isVoid: true }),
  13802. 'embed': new HtmlTagDefinition({ isVoid: true }),
  13803. 'link': new HtmlTagDefinition({ isVoid: true }),
  13804. 'img': new HtmlTagDefinition({ isVoid: true }),
  13805. 'input': new HtmlTagDefinition({ isVoid: true }),
  13806. 'param': new HtmlTagDefinition({ isVoid: true }),
  13807. 'hr': new HtmlTagDefinition({ isVoid: true }),
  13808. 'br': new HtmlTagDefinition({ isVoid: true }),
  13809. 'source': new HtmlTagDefinition({ isVoid: true }),
  13810. 'track': new HtmlTagDefinition({ isVoid: true }),
  13811. 'wbr': new HtmlTagDefinition({ isVoid: true }),
  13812. 'p': new HtmlTagDefinition({
  13813. closedByChildren: [
  13814. 'address', 'article', 'aside', 'blockquote', 'div', 'dl', 'fieldset',
  13815. 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5',
  13816. 'h6', 'header', 'hgroup', 'hr', 'main', 'nav', 'ol',
  13817. 'p', 'pre', 'section', 'table', 'ul'
  13818. ],
  13819. closedByParent: true
  13820. }),
  13821. 'thead': new HtmlTagDefinition({ closedByChildren: ['tbody', 'tfoot'] }),
  13822. 'tbody': new HtmlTagDefinition({ closedByChildren: ['tbody', 'tfoot'], closedByParent: true }),
  13823. 'tfoot': new HtmlTagDefinition({ closedByChildren: ['tbody'], closedByParent: true }),
  13824. 'tr': new HtmlTagDefinition({ closedByChildren: ['tr'], closedByParent: true }),
  13825. 'td': new HtmlTagDefinition({ closedByChildren: ['td', 'th'], closedByParent: true }),
  13826. 'th': new HtmlTagDefinition({ closedByChildren: ['td', 'th'], closedByParent: true }),
  13827. 'col': new HtmlTagDefinition({ isVoid: true }),
  13828. 'svg': new HtmlTagDefinition({ implicitNamespacePrefix: 'svg' }),
  13829. 'foreignObject': new HtmlTagDefinition({
  13830. // Usually the implicit namespace here would be redundant since it will be inherited from
  13831. // the parent `svg`, but we have to do it for `foreignObject`, because the way the parser
  13832. // works is that the parent node of an end tag is its own start tag which means that
  13833. // the `preventNamespaceInheritance` on `foreignObject` would have it default to the
  13834. // implicit namespace which is `html`, unless specified otherwise.
  13835. implicitNamespacePrefix: 'svg',
  13836. // We want to prevent children of foreignObject from inheriting its namespace, because
  13837. // the point of the element is to allow nodes from other namespaces to be inserted.
  13838. preventNamespaceInheritance: true,
  13839. }),
  13840. 'math': new HtmlTagDefinition({ implicitNamespacePrefix: 'math' }),
  13841. 'li': new HtmlTagDefinition({ closedByChildren: ['li'], closedByParent: true }),
  13842. 'dt': new HtmlTagDefinition({ closedByChildren: ['dt', 'dd'] }),
  13843. 'dd': new HtmlTagDefinition({ closedByChildren: ['dt', 'dd'], closedByParent: true }),
  13844. 'rb': new HtmlTagDefinition({ closedByChildren: ['rb', 'rt', 'rtc', 'rp'], closedByParent: true }),
  13845. 'rt': new HtmlTagDefinition({ closedByChildren: ['rb', 'rt', 'rtc', 'rp'], closedByParent: true }),
  13846. 'rtc': new HtmlTagDefinition({ closedByChildren: ['rb', 'rtc', 'rp'], closedByParent: true }),
  13847. 'rp': new HtmlTagDefinition({ closedByChildren: ['rb', 'rt', 'rtc', 'rp'], closedByParent: true }),
  13848. 'optgroup': new HtmlTagDefinition({ closedByChildren: ['optgroup'], closedByParent: true }),
  13849. 'option': new HtmlTagDefinition({ closedByChildren: ['option', 'optgroup'], closedByParent: true }),
  13850. 'pre': new HtmlTagDefinition({ ignoreFirstLf: true }),
  13851. 'listing': new HtmlTagDefinition({ ignoreFirstLf: true }),
  13852. 'style': new HtmlTagDefinition({ contentType: TagContentType.RAW_TEXT }),
  13853. 'script': new HtmlTagDefinition({ contentType: TagContentType.RAW_TEXT }),
  13854. 'title': new HtmlTagDefinition({
  13855. // The browser supports two separate `title` tags which have to use
  13856. // a different content type: `HTMLTitleElement` and `SVGTitleElement`
  13857. contentType: { default: TagContentType.ESCAPABLE_RAW_TEXT, svg: TagContentType.PARSABLE_DATA }
  13858. }),
  13859. 'textarea': new HtmlTagDefinition({ contentType: TagContentType.ESCAPABLE_RAW_TEXT, ignoreFirstLf: true }),
  13860. };
  13861. new DomElementSchemaRegistry().allKnownElementNames().forEach(knownTagName => {
  13862. if (!TAG_DEFINITIONS.hasOwnProperty(knownTagName) && getNsPrefix(knownTagName) === null) {
  13863. TAG_DEFINITIONS[knownTagName] = new HtmlTagDefinition({ canSelfClose: false });
  13864. }
  13865. });
  13866. }
  13867. // We have to make both a case-sensitive and a case-insensitive lookup, because
  13868. // HTML tag names are case insensitive, whereas some SVG tags are case sensitive.
  13869. return TAG_DEFINITIONS[tagName] ?? TAG_DEFINITIONS[tagName.toLowerCase()] ??
  13870. DEFAULT_TAG_DEFINITION;
  13871. }
  13872. // Mapping between all HTML entity names and their unicode representation.
  13873. // Generated from https://html.spec.whatwg.org/multipage/entities.json by stripping
  13874. // the `&` and `;` from the keys and removing the duplicates.
  13875. // see https://www.w3.org/TR/html51/syntax.html#named-character-references
  13876. const NAMED_ENTITIES = {
  13877. 'AElig': '\u00C6',
  13878. 'AMP': '\u0026',
  13879. 'amp': '\u0026',
  13880. 'Aacute': '\u00C1',
  13881. 'Abreve': '\u0102',
  13882. 'Acirc': '\u00C2',
  13883. 'Acy': '\u0410',
  13884. 'Afr': '\uD835\uDD04',
  13885. 'Agrave': '\u00C0',
  13886. 'Alpha': '\u0391',
  13887. 'Amacr': '\u0100',
  13888. 'And': '\u2A53',
  13889. 'Aogon': '\u0104',
  13890. 'Aopf': '\uD835\uDD38',
  13891. 'ApplyFunction': '\u2061',
  13892. 'af': '\u2061',
  13893. 'Aring': '\u00C5',
  13894. 'angst': '\u00C5',
  13895. 'Ascr': '\uD835\uDC9C',
  13896. 'Assign': '\u2254',
  13897. 'colone': '\u2254',
  13898. 'coloneq': '\u2254',
  13899. 'Atilde': '\u00C3',
  13900. 'Auml': '\u00C4',
  13901. 'Backslash': '\u2216',
  13902. 'setminus': '\u2216',
  13903. 'setmn': '\u2216',
  13904. 'smallsetminus': '\u2216',
  13905. 'ssetmn': '\u2216',
  13906. 'Barv': '\u2AE7',
  13907. 'Barwed': '\u2306',
  13908. 'doublebarwedge': '\u2306',
  13909. 'Bcy': '\u0411',
  13910. 'Because': '\u2235',
  13911. 'becaus': '\u2235',
  13912. 'because': '\u2235',
  13913. 'Bernoullis': '\u212C',
  13914. 'Bscr': '\u212C',
  13915. 'bernou': '\u212C',
  13916. 'Beta': '\u0392',
  13917. 'Bfr': '\uD835\uDD05',
  13918. 'Bopf': '\uD835\uDD39',
  13919. 'Breve': '\u02D8',
  13920. 'breve': '\u02D8',
  13921. 'Bumpeq': '\u224E',
  13922. 'HumpDownHump': '\u224E',
  13923. 'bump': '\u224E',
  13924. 'CHcy': '\u0427',
  13925. 'COPY': '\u00A9',
  13926. 'copy': '\u00A9',
  13927. 'Cacute': '\u0106',
  13928. 'Cap': '\u22D2',
  13929. 'CapitalDifferentialD': '\u2145',
  13930. 'DD': '\u2145',
  13931. 'Cayleys': '\u212D',
  13932. 'Cfr': '\u212D',
  13933. 'Ccaron': '\u010C',
  13934. 'Ccedil': '\u00C7',
  13935. 'Ccirc': '\u0108',
  13936. 'Cconint': '\u2230',
  13937. 'Cdot': '\u010A',
  13938. 'Cedilla': '\u00B8',
  13939. 'cedil': '\u00B8',
  13940. 'CenterDot': '\u00B7',
  13941. 'centerdot': '\u00B7',
  13942. 'middot': '\u00B7',
  13943. 'Chi': '\u03A7',
  13944. 'CircleDot': '\u2299',
  13945. 'odot': '\u2299',
  13946. 'CircleMinus': '\u2296',
  13947. 'ominus': '\u2296',
  13948. 'CirclePlus': '\u2295',
  13949. 'oplus': '\u2295',
  13950. 'CircleTimes': '\u2297',
  13951. 'otimes': '\u2297',
  13952. 'ClockwiseContourIntegral': '\u2232',
  13953. 'cwconint': '\u2232',
  13954. 'CloseCurlyDoubleQuote': '\u201D',
  13955. 'rdquo': '\u201D',
  13956. 'rdquor': '\u201D',
  13957. 'CloseCurlyQuote': '\u2019',
  13958. 'rsquo': '\u2019',
  13959. 'rsquor': '\u2019',
  13960. 'Colon': '\u2237',
  13961. 'Proportion': '\u2237',
  13962. 'Colone': '\u2A74',
  13963. 'Congruent': '\u2261',
  13964. 'equiv': '\u2261',
  13965. 'Conint': '\u222F',
  13966. 'DoubleContourIntegral': '\u222F',
  13967. 'ContourIntegral': '\u222E',
  13968. 'conint': '\u222E',
  13969. 'oint': '\u222E',
  13970. 'Copf': '\u2102',
  13971. 'complexes': '\u2102',
  13972. 'Coproduct': '\u2210',
  13973. 'coprod': '\u2210',
  13974. 'CounterClockwiseContourIntegral': '\u2233',
  13975. 'awconint': '\u2233',
  13976. 'Cross': '\u2A2F',
  13977. 'Cscr': '\uD835\uDC9E',
  13978. 'Cup': '\u22D3',
  13979. 'CupCap': '\u224D',
  13980. 'asympeq': '\u224D',
  13981. 'DDotrahd': '\u2911',
  13982. 'DJcy': '\u0402',
  13983. 'DScy': '\u0405',
  13984. 'DZcy': '\u040F',
  13985. 'Dagger': '\u2021',
  13986. 'ddagger': '\u2021',
  13987. 'Darr': '\u21A1',
  13988. 'Dashv': '\u2AE4',
  13989. 'DoubleLeftTee': '\u2AE4',
  13990. 'Dcaron': '\u010E',
  13991. 'Dcy': '\u0414',
  13992. 'Del': '\u2207',
  13993. 'nabla': '\u2207',
  13994. 'Delta': '\u0394',
  13995. 'Dfr': '\uD835\uDD07',
  13996. 'DiacriticalAcute': '\u00B4',
  13997. 'acute': '\u00B4',
  13998. 'DiacriticalDot': '\u02D9',
  13999. 'dot': '\u02D9',
  14000. 'DiacriticalDoubleAcute': '\u02DD',
  14001. 'dblac': '\u02DD',
  14002. 'DiacriticalGrave': '\u0060',
  14003. 'grave': '\u0060',
  14004. 'DiacriticalTilde': '\u02DC',
  14005. 'tilde': '\u02DC',
  14006. 'Diamond': '\u22C4',
  14007. 'diam': '\u22C4',
  14008. 'diamond': '\u22C4',
  14009. 'DifferentialD': '\u2146',
  14010. 'dd': '\u2146',
  14011. 'Dopf': '\uD835\uDD3B',
  14012. 'Dot': '\u00A8',
  14013. 'DoubleDot': '\u00A8',
  14014. 'die': '\u00A8',
  14015. 'uml': '\u00A8',
  14016. 'DotDot': '\u20DC',
  14017. 'DotEqual': '\u2250',
  14018. 'doteq': '\u2250',
  14019. 'esdot': '\u2250',
  14020. 'DoubleDownArrow': '\u21D3',
  14021. 'Downarrow': '\u21D3',
  14022. 'dArr': '\u21D3',
  14023. 'DoubleLeftArrow': '\u21D0',
  14024. 'Leftarrow': '\u21D0',
  14025. 'lArr': '\u21D0',
  14026. 'DoubleLeftRightArrow': '\u21D4',
  14027. 'Leftrightarrow': '\u21D4',
  14028. 'hArr': '\u21D4',
  14029. 'iff': '\u21D4',
  14030. 'DoubleLongLeftArrow': '\u27F8',
  14031. 'Longleftarrow': '\u27F8',
  14032. 'xlArr': '\u27F8',
  14033. 'DoubleLongLeftRightArrow': '\u27FA',
  14034. 'Longleftrightarrow': '\u27FA',
  14035. 'xhArr': '\u27FA',
  14036. 'DoubleLongRightArrow': '\u27F9',
  14037. 'Longrightarrow': '\u27F9',
  14038. 'xrArr': '\u27F9',
  14039. 'DoubleRightArrow': '\u21D2',
  14040. 'Implies': '\u21D2',
  14041. 'Rightarrow': '\u21D2',
  14042. 'rArr': '\u21D2',
  14043. 'DoubleRightTee': '\u22A8',
  14044. 'vDash': '\u22A8',
  14045. 'DoubleUpArrow': '\u21D1',
  14046. 'Uparrow': '\u21D1',
  14047. 'uArr': '\u21D1',
  14048. 'DoubleUpDownArrow': '\u21D5',
  14049. 'Updownarrow': '\u21D5',
  14050. 'vArr': '\u21D5',
  14051. 'DoubleVerticalBar': '\u2225',
  14052. 'par': '\u2225',
  14053. 'parallel': '\u2225',
  14054. 'shortparallel': '\u2225',
  14055. 'spar': '\u2225',
  14056. 'DownArrow': '\u2193',
  14057. 'ShortDownArrow': '\u2193',
  14058. 'darr': '\u2193',
  14059. 'downarrow': '\u2193',
  14060. 'DownArrowBar': '\u2913',
  14061. 'DownArrowUpArrow': '\u21F5',
  14062. 'duarr': '\u21F5',
  14063. 'DownBreve': '\u0311',
  14064. 'DownLeftRightVector': '\u2950',
  14065. 'DownLeftTeeVector': '\u295E',
  14066. 'DownLeftVector': '\u21BD',
  14067. 'leftharpoondown': '\u21BD',
  14068. 'lhard': '\u21BD',
  14069. 'DownLeftVectorBar': '\u2956',
  14070. 'DownRightTeeVector': '\u295F',
  14071. 'DownRightVector': '\u21C1',
  14072. 'rhard': '\u21C1',
  14073. 'rightharpoondown': '\u21C1',
  14074. 'DownRightVectorBar': '\u2957',
  14075. 'DownTee': '\u22A4',
  14076. 'top': '\u22A4',
  14077. 'DownTeeArrow': '\u21A7',
  14078. 'mapstodown': '\u21A7',
  14079. 'Dscr': '\uD835\uDC9F',
  14080. 'Dstrok': '\u0110',
  14081. 'ENG': '\u014A',
  14082. 'ETH': '\u00D0',
  14083. 'Eacute': '\u00C9',
  14084. 'Ecaron': '\u011A',
  14085. 'Ecirc': '\u00CA',
  14086. 'Ecy': '\u042D',
  14087. 'Edot': '\u0116',
  14088. 'Efr': '\uD835\uDD08',
  14089. 'Egrave': '\u00C8',
  14090. 'Element': '\u2208',
  14091. 'in': '\u2208',
  14092. 'isin': '\u2208',
  14093. 'isinv': '\u2208',
  14094. 'Emacr': '\u0112',
  14095. 'EmptySmallSquare': '\u25FB',
  14096. 'EmptyVerySmallSquare': '\u25AB',
  14097. 'Eogon': '\u0118',
  14098. 'Eopf': '\uD835\uDD3C',
  14099. 'Epsilon': '\u0395',
  14100. 'Equal': '\u2A75',
  14101. 'EqualTilde': '\u2242',
  14102. 'eqsim': '\u2242',
  14103. 'esim': '\u2242',
  14104. 'Equilibrium': '\u21CC',
  14105. 'rightleftharpoons': '\u21CC',
  14106. 'rlhar': '\u21CC',
  14107. 'Escr': '\u2130',
  14108. 'expectation': '\u2130',
  14109. 'Esim': '\u2A73',
  14110. 'Eta': '\u0397',
  14111. 'Euml': '\u00CB',
  14112. 'Exists': '\u2203',
  14113. 'exist': '\u2203',
  14114. 'ExponentialE': '\u2147',
  14115. 'ee': '\u2147',
  14116. 'exponentiale': '\u2147',
  14117. 'Fcy': '\u0424',
  14118. 'Ffr': '\uD835\uDD09',
  14119. 'FilledSmallSquare': '\u25FC',
  14120. 'FilledVerySmallSquare': '\u25AA',
  14121. 'blacksquare': '\u25AA',
  14122. 'squarf': '\u25AA',
  14123. 'squf': '\u25AA',
  14124. 'Fopf': '\uD835\uDD3D',
  14125. 'ForAll': '\u2200',
  14126. 'forall': '\u2200',
  14127. 'Fouriertrf': '\u2131',
  14128. 'Fscr': '\u2131',
  14129. 'GJcy': '\u0403',
  14130. 'GT': '\u003E',
  14131. 'gt': '\u003E',
  14132. 'Gamma': '\u0393',
  14133. 'Gammad': '\u03DC',
  14134. 'Gbreve': '\u011E',
  14135. 'Gcedil': '\u0122',
  14136. 'Gcirc': '\u011C',
  14137. 'Gcy': '\u0413',
  14138. 'Gdot': '\u0120',
  14139. 'Gfr': '\uD835\uDD0A',
  14140. 'Gg': '\u22D9',
  14141. 'ggg': '\u22D9',
  14142. 'Gopf': '\uD835\uDD3E',
  14143. 'GreaterEqual': '\u2265',
  14144. 'ge': '\u2265',
  14145. 'geq': '\u2265',
  14146. 'GreaterEqualLess': '\u22DB',
  14147. 'gel': '\u22DB',
  14148. 'gtreqless': '\u22DB',
  14149. 'GreaterFullEqual': '\u2267',
  14150. 'gE': '\u2267',
  14151. 'geqq': '\u2267',
  14152. 'GreaterGreater': '\u2AA2',
  14153. 'GreaterLess': '\u2277',
  14154. 'gl': '\u2277',
  14155. 'gtrless': '\u2277',
  14156. 'GreaterSlantEqual': '\u2A7E',
  14157. 'geqslant': '\u2A7E',
  14158. 'ges': '\u2A7E',
  14159. 'GreaterTilde': '\u2273',
  14160. 'gsim': '\u2273',
  14161. 'gtrsim': '\u2273',
  14162. 'Gscr': '\uD835\uDCA2',
  14163. 'Gt': '\u226B',
  14164. 'NestedGreaterGreater': '\u226B',
  14165. 'gg': '\u226B',
  14166. 'HARDcy': '\u042A',
  14167. 'Hacek': '\u02C7',
  14168. 'caron': '\u02C7',
  14169. 'Hat': '\u005E',
  14170. 'Hcirc': '\u0124',
  14171. 'Hfr': '\u210C',
  14172. 'Poincareplane': '\u210C',
  14173. 'HilbertSpace': '\u210B',
  14174. 'Hscr': '\u210B',
  14175. 'hamilt': '\u210B',
  14176. 'Hopf': '\u210D',
  14177. 'quaternions': '\u210D',
  14178. 'HorizontalLine': '\u2500',
  14179. 'boxh': '\u2500',
  14180. 'Hstrok': '\u0126',
  14181. 'HumpEqual': '\u224F',
  14182. 'bumpe': '\u224F',
  14183. 'bumpeq': '\u224F',
  14184. 'IEcy': '\u0415',
  14185. 'IJlig': '\u0132',
  14186. 'IOcy': '\u0401',
  14187. 'Iacute': '\u00CD',
  14188. 'Icirc': '\u00CE',
  14189. 'Icy': '\u0418',
  14190. 'Idot': '\u0130',
  14191. 'Ifr': '\u2111',
  14192. 'Im': '\u2111',
  14193. 'image': '\u2111',
  14194. 'imagpart': '\u2111',
  14195. 'Igrave': '\u00CC',
  14196. 'Imacr': '\u012A',
  14197. 'ImaginaryI': '\u2148',
  14198. 'ii': '\u2148',
  14199. 'Int': '\u222C',
  14200. 'Integral': '\u222B',
  14201. 'int': '\u222B',
  14202. 'Intersection': '\u22C2',
  14203. 'bigcap': '\u22C2',
  14204. 'xcap': '\u22C2',
  14205. 'InvisibleComma': '\u2063',
  14206. 'ic': '\u2063',
  14207. 'InvisibleTimes': '\u2062',
  14208. 'it': '\u2062',
  14209. 'Iogon': '\u012E',
  14210. 'Iopf': '\uD835\uDD40',
  14211. 'Iota': '\u0399',
  14212. 'Iscr': '\u2110',
  14213. 'imagline': '\u2110',
  14214. 'Itilde': '\u0128',
  14215. 'Iukcy': '\u0406',
  14216. 'Iuml': '\u00CF',
  14217. 'Jcirc': '\u0134',
  14218. 'Jcy': '\u0419',
  14219. 'Jfr': '\uD835\uDD0D',
  14220. 'Jopf': '\uD835\uDD41',
  14221. 'Jscr': '\uD835\uDCA5',
  14222. 'Jsercy': '\u0408',
  14223. 'Jukcy': '\u0404',
  14224. 'KHcy': '\u0425',
  14225. 'KJcy': '\u040C',
  14226. 'Kappa': '\u039A',
  14227. 'Kcedil': '\u0136',
  14228. 'Kcy': '\u041A',
  14229. 'Kfr': '\uD835\uDD0E',
  14230. 'Kopf': '\uD835\uDD42',
  14231. 'Kscr': '\uD835\uDCA6',
  14232. 'LJcy': '\u0409',
  14233. 'LT': '\u003C',
  14234. 'lt': '\u003C',
  14235. 'Lacute': '\u0139',
  14236. 'Lambda': '\u039B',
  14237. 'Lang': '\u27EA',
  14238. 'Laplacetrf': '\u2112',
  14239. 'Lscr': '\u2112',
  14240. 'lagran': '\u2112',
  14241. 'Larr': '\u219E',
  14242. 'twoheadleftarrow': '\u219E',
  14243. 'Lcaron': '\u013D',
  14244. 'Lcedil': '\u013B',
  14245. 'Lcy': '\u041B',
  14246. 'LeftAngleBracket': '\u27E8',
  14247. 'lang': '\u27E8',
  14248. 'langle': '\u27E8',
  14249. 'LeftArrow': '\u2190',
  14250. 'ShortLeftArrow': '\u2190',
  14251. 'larr': '\u2190',
  14252. 'leftarrow': '\u2190',
  14253. 'slarr': '\u2190',
  14254. 'LeftArrowBar': '\u21E4',
  14255. 'larrb': '\u21E4',
  14256. 'LeftArrowRightArrow': '\u21C6',
  14257. 'leftrightarrows': '\u21C6',
  14258. 'lrarr': '\u21C6',
  14259. 'LeftCeiling': '\u2308',
  14260. 'lceil': '\u2308',
  14261. 'LeftDoubleBracket': '\u27E6',
  14262. 'lobrk': '\u27E6',
  14263. 'LeftDownTeeVector': '\u2961',
  14264. 'LeftDownVector': '\u21C3',
  14265. 'dharl': '\u21C3',
  14266. 'downharpoonleft': '\u21C3',
  14267. 'LeftDownVectorBar': '\u2959',
  14268. 'LeftFloor': '\u230A',
  14269. 'lfloor': '\u230A',
  14270. 'LeftRightArrow': '\u2194',
  14271. 'harr': '\u2194',
  14272. 'leftrightarrow': '\u2194',
  14273. 'LeftRightVector': '\u294E',
  14274. 'LeftTee': '\u22A3',
  14275. 'dashv': '\u22A3',
  14276. 'LeftTeeArrow': '\u21A4',
  14277. 'mapstoleft': '\u21A4',
  14278. 'LeftTeeVector': '\u295A',
  14279. 'LeftTriangle': '\u22B2',
  14280. 'vartriangleleft': '\u22B2',
  14281. 'vltri': '\u22B2',
  14282. 'LeftTriangleBar': '\u29CF',
  14283. 'LeftTriangleEqual': '\u22B4',
  14284. 'ltrie': '\u22B4',
  14285. 'trianglelefteq': '\u22B4',
  14286. 'LeftUpDownVector': '\u2951',
  14287. 'LeftUpTeeVector': '\u2960',
  14288. 'LeftUpVector': '\u21BF',
  14289. 'uharl': '\u21BF',
  14290. 'upharpoonleft': '\u21BF',
  14291. 'LeftUpVectorBar': '\u2958',
  14292. 'LeftVector': '\u21BC',
  14293. 'leftharpoonup': '\u21BC',
  14294. 'lharu': '\u21BC',
  14295. 'LeftVectorBar': '\u2952',
  14296. 'LessEqualGreater': '\u22DA',
  14297. 'leg': '\u22DA',
  14298. 'lesseqgtr': '\u22DA',
  14299. 'LessFullEqual': '\u2266',
  14300. 'lE': '\u2266',
  14301. 'leqq': '\u2266',
  14302. 'LessGreater': '\u2276',
  14303. 'lessgtr': '\u2276',
  14304. 'lg': '\u2276',
  14305. 'LessLess': '\u2AA1',
  14306. 'LessSlantEqual': '\u2A7D',
  14307. 'leqslant': '\u2A7D',
  14308. 'les': '\u2A7D',
  14309. 'LessTilde': '\u2272',
  14310. 'lesssim': '\u2272',
  14311. 'lsim': '\u2272',
  14312. 'Lfr': '\uD835\uDD0F',
  14313. 'Ll': '\u22D8',
  14314. 'Lleftarrow': '\u21DA',
  14315. 'lAarr': '\u21DA',
  14316. 'Lmidot': '\u013F',
  14317. 'LongLeftArrow': '\u27F5',
  14318. 'longleftarrow': '\u27F5',
  14319. 'xlarr': '\u27F5',
  14320. 'LongLeftRightArrow': '\u27F7',
  14321. 'longleftrightarrow': '\u27F7',
  14322. 'xharr': '\u27F7',
  14323. 'LongRightArrow': '\u27F6',
  14324. 'longrightarrow': '\u27F6',
  14325. 'xrarr': '\u27F6',
  14326. 'Lopf': '\uD835\uDD43',
  14327. 'LowerLeftArrow': '\u2199',
  14328. 'swarr': '\u2199',
  14329. 'swarrow': '\u2199',
  14330. 'LowerRightArrow': '\u2198',
  14331. 'searr': '\u2198',
  14332. 'searrow': '\u2198',
  14333. 'Lsh': '\u21B0',
  14334. 'lsh': '\u21B0',
  14335. 'Lstrok': '\u0141',
  14336. 'Lt': '\u226A',
  14337. 'NestedLessLess': '\u226A',
  14338. 'll': '\u226A',
  14339. 'Map': '\u2905',
  14340. 'Mcy': '\u041C',
  14341. 'MediumSpace': '\u205F',
  14342. 'Mellintrf': '\u2133',
  14343. 'Mscr': '\u2133',
  14344. 'phmmat': '\u2133',
  14345. 'Mfr': '\uD835\uDD10',
  14346. 'MinusPlus': '\u2213',
  14347. 'mnplus': '\u2213',
  14348. 'mp': '\u2213',
  14349. 'Mopf': '\uD835\uDD44',
  14350. 'Mu': '\u039C',
  14351. 'NJcy': '\u040A',
  14352. 'Nacute': '\u0143',
  14353. 'Ncaron': '\u0147',
  14354. 'Ncedil': '\u0145',
  14355. 'Ncy': '\u041D',
  14356. 'NegativeMediumSpace': '\u200B',
  14357. 'NegativeThickSpace': '\u200B',
  14358. 'NegativeThinSpace': '\u200B',
  14359. 'NegativeVeryThinSpace': '\u200B',
  14360. 'ZeroWidthSpace': '\u200B',
  14361. 'NewLine': '\u000A',
  14362. 'Nfr': '\uD835\uDD11',
  14363. 'NoBreak': '\u2060',
  14364. 'NonBreakingSpace': '\u00A0',
  14365. 'nbsp': '\u00A0',
  14366. 'Nopf': '\u2115',
  14367. 'naturals': '\u2115',
  14368. 'Not': '\u2AEC',
  14369. 'NotCongruent': '\u2262',
  14370. 'nequiv': '\u2262',
  14371. 'NotCupCap': '\u226D',
  14372. 'NotDoubleVerticalBar': '\u2226',
  14373. 'npar': '\u2226',
  14374. 'nparallel': '\u2226',
  14375. 'nshortparallel': '\u2226',
  14376. 'nspar': '\u2226',
  14377. 'NotElement': '\u2209',
  14378. 'notin': '\u2209',
  14379. 'notinva': '\u2209',
  14380. 'NotEqual': '\u2260',
  14381. 'ne': '\u2260',
  14382. 'NotEqualTilde': '\u2242\u0338',
  14383. 'nesim': '\u2242\u0338',
  14384. 'NotExists': '\u2204',
  14385. 'nexist': '\u2204',
  14386. 'nexists': '\u2204',
  14387. 'NotGreater': '\u226F',
  14388. 'ngt': '\u226F',
  14389. 'ngtr': '\u226F',
  14390. 'NotGreaterEqual': '\u2271',
  14391. 'nge': '\u2271',
  14392. 'ngeq': '\u2271',
  14393. 'NotGreaterFullEqual': '\u2267\u0338',
  14394. 'ngE': '\u2267\u0338',
  14395. 'ngeqq': '\u2267\u0338',
  14396. 'NotGreaterGreater': '\u226B\u0338',
  14397. 'nGtv': '\u226B\u0338',
  14398. 'NotGreaterLess': '\u2279',
  14399. 'ntgl': '\u2279',
  14400. 'NotGreaterSlantEqual': '\u2A7E\u0338',
  14401. 'ngeqslant': '\u2A7E\u0338',
  14402. 'nges': '\u2A7E\u0338',
  14403. 'NotGreaterTilde': '\u2275',
  14404. 'ngsim': '\u2275',
  14405. 'NotHumpDownHump': '\u224E\u0338',
  14406. 'nbump': '\u224E\u0338',
  14407. 'NotHumpEqual': '\u224F\u0338',
  14408. 'nbumpe': '\u224F\u0338',
  14409. 'NotLeftTriangle': '\u22EA',
  14410. 'nltri': '\u22EA',
  14411. 'ntriangleleft': '\u22EA',
  14412. 'NotLeftTriangleBar': '\u29CF\u0338',
  14413. 'NotLeftTriangleEqual': '\u22EC',
  14414. 'nltrie': '\u22EC',
  14415. 'ntrianglelefteq': '\u22EC',
  14416. 'NotLess': '\u226E',
  14417. 'nless': '\u226E',
  14418. 'nlt': '\u226E',
  14419. 'NotLessEqual': '\u2270',
  14420. 'nle': '\u2270',
  14421. 'nleq': '\u2270',
  14422. 'NotLessGreater': '\u2278',
  14423. 'ntlg': '\u2278',
  14424. 'NotLessLess': '\u226A\u0338',
  14425. 'nLtv': '\u226A\u0338',
  14426. 'NotLessSlantEqual': '\u2A7D\u0338',
  14427. 'nleqslant': '\u2A7D\u0338',
  14428. 'nles': '\u2A7D\u0338',
  14429. 'NotLessTilde': '\u2274',
  14430. 'nlsim': '\u2274',
  14431. 'NotNestedGreaterGreater': '\u2AA2\u0338',
  14432. 'NotNestedLessLess': '\u2AA1\u0338',
  14433. 'NotPrecedes': '\u2280',
  14434. 'npr': '\u2280',
  14435. 'nprec': '\u2280',
  14436. 'NotPrecedesEqual': '\u2AAF\u0338',
  14437. 'npre': '\u2AAF\u0338',
  14438. 'npreceq': '\u2AAF\u0338',
  14439. 'NotPrecedesSlantEqual': '\u22E0',
  14440. 'nprcue': '\u22E0',
  14441. 'NotReverseElement': '\u220C',
  14442. 'notni': '\u220C',
  14443. 'notniva': '\u220C',
  14444. 'NotRightTriangle': '\u22EB',
  14445. 'nrtri': '\u22EB',
  14446. 'ntriangleright': '\u22EB',
  14447. 'NotRightTriangleBar': '\u29D0\u0338',
  14448. 'NotRightTriangleEqual': '\u22ED',
  14449. 'nrtrie': '\u22ED',
  14450. 'ntrianglerighteq': '\u22ED',
  14451. 'NotSquareSubset': '\u228F\u0338',
  14452. 'NotSquareSubsetEqual': '\u22E2',
  14453. 'nsqsube': '\u22E2',
  14454. 'NotSquareSuperset': '\u2290\u0338',
  14455. 'NotSquareSupersetEqual': '\u22E3',
  14456. 'nsqsupe': '\u22E3',
  14457. 'NotSubset': '\u2282\u20D2',
  14458. 'nsubset': '\u2282\u20D2',
  14459. 'vnsub': '\u2282\u20D2',
  14460. 'NotSubsetEqual': '\u2288',
  14461. 'nsube': '\u2288',
  14462. 'nsubseteq': '\u2288',
  14463. 'NotSucceeds': '\u2281',
  14464. 'nsc': '\u2281',
  14465. 'nsucc': '\u2281',
  14466. 'NotSucceedsEqual': '\u2AB0\u0338',
  14467. 'nsce': '\u2AB0\u0338',
  14468. 'nsucceq': '\u2AB0\u0338',
  14469. 'NotSucceedsSlantEqual': '\u22E1',
  14470. 'nsccue': '\u22E1',
  14471. 'NotSucceedsTilde': '\u227F\u0338',
  14472. 'NotSuperset': '\u2283\u20D2',
  14473. 'nsupset': '\u2283\u20D2',
  14474. 'vnsup': '\u2283\u20D2',
  14475. 'NotSupersetEqual': '\u2289',
  14476. 'nsupe': '\u2289',
  14477. 'nsupseteq': '\u2289',
  14478. 'NotTilde': '\u2241',
  14479. 'nsim': '\u2241',
  14480. 'NotTildeEqual': '\u2244',
  14481. 'nsime': '\u2244',
  14482. 'nsimeq': '\u2244',
  14483. 'NotTildeFullEqual': '\u2247',
  14484. 'ncong': '\u2247',
  14485. 'NotTildeTilde': '\u2249',
  14486. 'nap': '\u2249',
  14487. 'napprox': '\u2249',
  14488. 'NotVerticalBar': '\u2224',
  14489. 'nmid': '\u2224',
  14490. 'nshortmid': '\u2224',
  14491. 'nsmid': '\u2224',
  14492. 'Nscr': '\uD835\uDCA9',
  14493. 'Ntilde': '\u00D1',
  14494. 'Nu': '\u039D',
  14495. 'OElig': '\u0152',
  14496. 'Oacute': '\u00D3',
  14497. 'Ocirc': '\u00D4',
  14498. 'Ocy': '\u041E',
  14499. 'Odblac': '\u0150',
  14500. 'Ofr': '\uD835\uDD12',
  14501. 'Ograve': '\u00D2',
  14502. 'Omacr': '\u014C',
  14503. 'Omega': '\u03A9',
  14504. 'ohm': '\u03A9',
  14505. 'Omicron': '\u039F',
  14506. 'Oopf': '\uD835\uDD46',
  14507. 'OpenCurlyDoubleQuote': '\u201C',
  14508. 'ldquo': '\u201C',
  14509. 'OpenCurlyQuote': '\u2018',
  14510. 'lsquo': '\u2018',
  14511. 'Or': '\u2A54',
  14512. 'Oscr': '\uD835\uDCAA',
  14513. 'Oslash': '\u00D8',
  14514. 'Otilde': '\u00D5',
  14515. 'Otimes': '\u2A37',
  14516. 'Ouml': '\u00D6',
  14517. 'OverBar': '\u203E',
  14518. 'oline': '\u203E',
  14519. 'OverBrace': '\u23DE',
  14520. 'OverBracket': '\u23B4',
  14521. 'tbrk': '\u23B4',
  14522. 'OverParenthesis': '\u23DC',
  14523. 'PartialD': '\u2202',
  14524. 'part': '\u2202',
  14525. 'Pcy': '\u041F',
  14526. 'Pfr': '\uD835\uDD13',
  14527. 'Phi': '\u03A6',
  14528. 'Pi': '\u03A0',
  14529. 'PlusMinus': '\u00B1',
  14530. 'plusmn': '\u00B1',
  14531. 'pm': '\u00B1',
  14532. 'Popf': '\u2119',
  14533. 'primes': '\u2119',
  14534. 'Pr': '\u2ABB',
  14535. 'Precedes': '\u227A',
  14536. 'pr': '\u227A',
  14537. 'prec': '\u227A',
  14538. 'PrecedesEqual': '\u2AAF',
  14539. 'pre': '\u2AAF',
  14540. 'preceq': '\u2AAF',
  14541. 'PrecedesSlantEqual': '\u227C',
  14542. 'prcue': '\u227C',
  14543. 'preccurlyeq': '\u227C',
  14544. 'PrecedesTilde': '\u227E',
  14545. 'precsim': '\u227E',
  14546. 'prsim': '\u227E',
  14547. 'Prime': '\u2033',
  14548. 'Product': '\u220F',
  14549. 'prod': '\u220F',
  14550. 'Proportional': '\u221D',
  14551. 'prop': '\u221D',
  14552. 'propto': '\u221D',
  14553. 'varpropto': '\u221D',
  14554. 'vprop': '\u221D',
  14555. 'Pscr': '\uD835\uDCAB',
  14556. 'Psi': '\u03A8',
  14557. 'QUOT': '\u0022',
  14558. 'quot': '\u0022',
  14559. 'Qfr': '\uD835\uDD14',
  14560. 'Qopf': '\u211A',
  14561. 'rationals': '\u211A',
  14562. 'Qscr': '\uD835\uDCAC',
  14563. 'RBarr': '\u2910',
  14564. 'drbkarow': '\u2910',
  14565. 'REG': '\u00AE',
  14566. 'circledR': '\u00AE',
  14567. 'reg': '\u00AE',
  14568. 'Racute': '\u0154',
  14569. 'Rang': '\u27EB',
  14570. 'Rarr': '\u21A0',
  14571. 'twoheadrightarrow': '\u21A0',
  14572. 'Rarrtl': '\u2916',
  14573. 'Rcaron': '\u0158',
  14574. 'Rcedil': '\u0156',
  14575. 'Rcy': '\u0420',
  14576. 'Re': '\u211C',
  14577. 'Rfr': '\u211C',
  14578. 'real': '\u211C',
  14579. 'realpart': '\u211C',
  14580. 'ReverseElement': '\u220B',
  14581. 'SuchThat': '\u220B',
  14582. 'ni': '\u220B',
  14583. 'niv': '\u220B',
  14584. 'ReverseEquilibrium': '\u21CB',
  14585. 'leftrightharpoons': '\u21CB',
  14586. 'lrhar': '\u21CB',
  14587. 'ReverseUpEquilibrium': '\u296F',
  14588. 'duhar': '\u296F',
  14589. 'Rho': '\u03A1',
  14590. 'RightAngleBracket': '\u27E9',
  14591. 'rang': '\u27E9',
  14592. 'rangle': '\u27E9',
  14593. 'RightArrow': '\u2192',
  14594. 'ShortRightArrow': '\u2192',
  14595. 'rarr': '\u2192',
  14596. 'rightarrow': '\u2192',
  14597. 'srarr': '\u2192',
  14598. 'RightArrowBar': '\u21E5',
  14599. 'rarrb': '\u21E5',
  14600. 'RightArrowLeftArrow': '\u21C4',
  14601. 'rightleftarrows': '\u21C4',
  14602. 'rlarr': '\u21C4',
  14603. 'RightCeiling': '\u2309',
  14604. 'rceil': '\u2309',
  14605. 'RightDoubleBracket': '\u27E7',
  14606. 'robrk': '\u27E7',
  14607. 'RightDownTeeVector': '\u295D',
  14608. 'RightDownVector': '\u21C2',
  14609. 'dharr': '\u21C2',
  14610. 'downharpoonright': '\u21C2',
  14611. 'RightDownVectorBar': '\u2955',
  14612. 'RightFloor': '\u230B',
  14613. 'rfloor': '\u230B',
  14614. 'RightTee': '\u22A2',
  14615. 'vdash': '\u22A2',
  14616. 'RightTeeArrow': '\u21A6',
  14617. 'map': '\u21A6',
  14618. 'mapsto': '\u21A6',
  14619. 'RightTeeVector': '\u295B',
  14620. 'RightTriangle': '\u22B3',
  14621. 'vartriangleright': '\u22B3',
  14622. 'vrtri': '\u22B3',
  14623. 'RightTriangleBar': '\u29D0',
  14624. 'RightTriangleEqual': '\u22B5',
  14625. 'rtrie': '\u22B5',
  14626. 'trianglerighteq': '\u22B5',
  14627. 'RightUpDownVector': '\u294F',
  14628. 'RightUpTeeVector': '\u295C',
  14629. 'RightUpVector': '\u21BE',
  14630. 'uharr': '\u21BE',
  14631. 'upharpoonright': '\u21BE',
  14632. 'RightUpVectorBar': '\u2954',
  14633. 'RightVector': '\u21C0',
  14634. 'rharu': '\u21C0',
  14635. 'rightharpoonup': '\u21C0',
  14636. 'RightVectorBar': '\u2953',
  14637. 'Ropf': '\u211D',
  14638. 'reals': '\u211D',
  14639. 'RoundImplies': '\u2970',
  14640. 'Rrightarrow': '\u21DB',
  14641. 'rAarr': '\u21DB',
  14642. 'Rscr': '\u211B',
  14643. 'realine': '\u211B',
  14644. 'Rsh': '\u21B1',
  14645. 'rsh': '\u21B1',
  14646. 'RuleDelayed': '\u29F4',
  14647. 'SHCHcy': '\u0429',
  14648. 'SHcy': '\u0428',
  14649. 'SOFTcy': '\u042C',
  14650. 'Sacute': '\u015A',
  14651. 'Sc': '\u2ABC',
  14652. 'Scaron': '\u0160',
  14653. 'Scedil': '\u015E',
  14654. 'Scirc': '\u015C',
  14655. 'Scy': '\u0421',
  14656. 'Sfr': '\uD835\uDD16',
  14657. 'ShortUpArrow': '\u2191',
  14658. 'UpArrow': '\u2191',
  14659. 'uarr': '\u2191',
  14660. 'uparrow': '\u2191',
  14661. 'Sigma': '\u03A3',
  14662. 'SmallCircle': '\u2218',
  14663. 'compfn': '\u2218',
  14664. 'Sopf': '\uD835\uDD4A',
  14665. 'Sqrt': '\u221A',
  14666. 'radic': '\u221A',
  14667. 'Square': '\u25A1',
  14668. 'squ': '\u25A1',
  14669. 'square': '\u25A1',
  14670. 'SquareIntersection': '\u2293',
  14671. 'sqcap': '\u2293',
  14672. 'SquareSubset': '\u228F',
  14673. 'sqsub': '\u228F',
  14674. 'sqsubset': '\u228F',
  14675. 'SquareSubsetEqual': '\u2291',
  14676. 'sqsube': '\u2291',
  14677. 'sqsubseteq': '\u2291',
  14678. 'SquareSuperset': '\u2290',
  14679. 'sqsup': '\u2290',
  14680. 'sqsupset': '\u2290',
  14681. 'SquareSupersetEqual': '\u2292',
  14682. 'sqsupe': '\u2292',
  14683. 'sqsupseteq': '\u2292',
  14684. 'SquareUnion': '\u2294',
  14685. 'sqcup': '\u2294',
  14686. 'Sscr': '\uD835\uDCAE',
  14687. 'Star': '\u22C6',
  14688. 'sstarf': '\u22C6',
  14689. 'Sub': '\u22D0',
  14690. 'Subset': '\u22D0',
  14691. 'SubsetEqual': '\u2286',
  14692. 'sube': '\u2286',
  14693. 'subseteq': '\u2286',
  14694. 'Succeeds': '\u227B',
  14695. 'sc': '\u227B',
  14696. 'succ': '\u227B',
  14697. 'SucceedsEqual': '\u2AB0',
  14698. 'sce': '\u2AB0',
  14699. 'succeq': '\u2AB0',
  14700. 'SucceedsSlantEqual': '\u227D',
  14701. 'sccue': '\u227D',
  14702. 'succcurlyeq': '\u227D',
  14703. 'SucceedsTilde': '\u227F',
  14704. 'scsim': '\u227F',
  14705. 'succsim': '\u227F',
  14706. 'Sum': '\u2211',
  14707. 'sum': '\u2211',
  14708. 'Sup': '\u22D1',
  14709. 'Supset': '\u22D1',
  14710. 'Superset': '\u2283',
  14711. 'sup': '\u2283',
  14712. 'supset': '\u2283',
  14713. 'SupersetEqual': '\u2287',
  14714. 'supe': '\u2287',
  14715. 'supseteq': '\u2287',
  14716. 'THORN': '\u00DE',
  14717. 'TRADE': '\u2122',
  14718. 'trade': '\u2122',
  14719. 'TSHcy': '\u040B',
  14720. 'TScy': '\u0426',
  14721. 'Tab': '\u0009',
  14722. 'Tau': '\u03A4',
  14723. 'Tcaron': '\u0164',
  14724. 'Tcedil': '\u0162',
  14725. 'Tcy': '\u0422',
  14726. 'Tfr': '\uD835\uDD17',
  14727. 'Therefore': '\u2234',
  14728. 'there4': '\u2234',
  14729. 'therefore': '\u2234',
  14730. 'Theta': '\u0398',
  14731. 'ThickSpace': '\u205F\u200A',
  14732. 'ThinSpace': '\u2009',
  14733. 'thinsp': '\u2009',
  14734. 'Tilde': '\u223C',
  14735. 'sim': '\u223C',
  14736. 'thicksim': '\u223C',
  14737. 'thksim': '\u223C',
  14738. 'TildeEqual': '\u2243',
  14739. 'sime': '\u2243',
  14740. 'simeq': '\u2243',
  14741. 'TildeFullEqual': '\u2245',
  14742. 'cong': '\u2245',
  14743. 'TildeTilde': '\u2248',
  14744. 'ap': '\u2248',
  14745. 'approx': '\u2248',
  14746. 'asymp': '\u2248',
  14747. 'thickapprox': '\u2248',
  14748. 'thkap': '\u2248',
  14749. 'Topf': '\uD835\uDD4B',
  14750. 'TripleDot': '\u20DB',
  14751. 'tdot': '\u20DB',
  14752. 'Tscr': '\uD835\uDCAF',
  14753. 'Tstrok': '\u0166',
  14754. 'Uacute': '\u00DA',
  14755. 'Uarr': '\u219F',
  14756. 'Uarrocir': '\u2949',
  14757. 'Ubrcy': '\u040E',
  14758. 'Ubreve': '\u016C',
  14759. 'Ucirc': '\u00DB',
  14760. 'Ucy': '\u0423',
  14761. 'Udblac': '\u0170',
  14762. 'Ufr': '\uD835\uDD18',
  14763. 'Ugrave': '\u00D9',
  14764. 'Umacr': '\u016A',
  14765. 'UnderBar': '\u005F',
  14766. 'lowbar': '\u005F',
  14767. 'UnderBrace': '\u23DF',
  14768. 'UnderBracket': '\u23B5',
  14769. 'bbrk': '\u23B5',
  14770. 'UnderParenthesis': '\u23DD',
  14771. 'Union': '\u22C3',
  14772. 'bigcup': '\u22C3',
  14773. 'xcup': '\u22C3',
  14774. 'UnionPlus': '\u228E',
  14775. 'uplus': '\u228E',
  14776. 'Uogon': '\u0172',
  14777. 'Uopf': '\uD835\uDD4C',
  14778. 'UpArrowBar': '\u2912',
  14779. 'UpArrowDownArrow': '\u21C5',
  14780. 'udarr': '\u21C5',
  14781. 'UpDownArrow': '\u2195',
  14782. 'updownarrow': '\u2195',
  14783. 'varr': '\u2195',
  14784. 'UpEquilibrium': '\u296E',
  14785. 'udhar': '\u296E',
  14786. 'UpTee': '\u22A5',
  14787. 'bot': '\u22A5',
  14788. 'bottom': '\u22A5',
  14789. 'perp': '\u22A5',
  14790. 'UpTeeArrow': '\u21A5',
  14791. 'mapstoup': '\u21A5',
  14792. 'UpperLeftArrow': '\u2196',
  14793. 'nwarr': '\u2196',
  14794. 'nwarrow': '\u2196',
  14795. 'UpperRightArrow': '\u2197',
  14796. 'nearr': '\u2197',
  14797. 'nearrow': '\u2197',
  14798. 'Upsi': '\u03D2',
  14799. 'upsih': '\u03D2',
  14800. 'Upsilon': '\u03A5',
  14801. 'Uring': '\u016E',
  14802. 'Uscr': '\uD835\uDCB0',
  14803. 'Utilde': '\u0168',
  14804. 'Uuml': '\u00DC',
  14805. 'VDash': '\u22AB',
  14806. 'Vbar': '\u2AEB',
  14807. 'Vcy': '\u0412',
  14808. 'Vdash': '\u22A9',
  14809. 'Vdashl': '\u2AE6',
  14810. 'Vee': '\u22C1',
  14811. 'bigvee': '\u22C1',
  14812. 'xvee': '\u22C1',
  14813. 'Verbar': '\u2016',
  14814. 'Vert': '\u2016',
  14815. 'VerticalBar': '\u2223',
  14816. 'mid': '\u2223',
  14817. 'shortmid': '\u2223',
  14818. 'smid': '\u2223',
  14819. 'VerticalLine': '\u007C',
  14820. 'verbar': '\u007C',
  14821. 'vert': '\u007C',
  14822. 'VerticalSeparator': '\u2758',
  14823. 'VerticalTilde': '\u2240',
  14824. 'wr': '\u2240',
  14825. 'wreath': '\u2240',
  14826. 'VeryThinSpace': '\u200A',
  14827. 'hairsp': '\u200A',
  14828. 'Vfr': '\uD835\uDD19',
  14829. 'Vopf': '\uD835\uDD4D',
  14830. 'Vscr': '\uD835\uDCB1',
  14831. 'Vvdash': '\u22AA',
  14832. 'Wcirc': '\u0174',
  14833. 'Wedge': '\u22C0',
  14834. 'bigwedge': '\u22C0',
  14835. 'xwedge': '\u22C0',
  14836. 'Wfr': '\uD835\uDD1A',
  14837. 'Wopf': '\uD835\uDD4E',
  14838. 'Wscr': '\uD835\uDCB2',
  14839. 'Xfr': '\uD835\uDD1B',
  14840. 'Xi': '\u039E',
  14841. 'Xopf': '\uD835\uDD4F',
  14842. 'Xscr': '\uD835\uDCB3',
  14843. 'YAcy': '\u042F',
  14844. 'YIcy': '\u0407',
  14845. 'YUcy': '\u042E',
  14846. 'Yacute': '\u00DD',
  14847. 'Ycirc': '\u0176',
  14848. 'Ycy': '\u042B',
  14849. 'Yfr': '\uD835\uDD1C',
  14850. 'Yopf': '\uD835\uDD50',
  14851. 'Yscr': '\uD835\uDCB4',
  14852. 'Yuml': '\u0178',
  14853. 'ZHcy': '\u0416',
  14854. 'Zacute': '\u0179',
  14855. 'Zcaron': '\u017D',
  14856. 'Zcy': '\u0417',
  14857. 'Zdot': '\u017B',
  14858. 'Zeta': '\u0396',
  14859. 'Zfr': '\u2128',
  14860. 'zeetrf': '\u2128',
  14861. 'Zopf': '\u2124',
  14862. 'integers': '\u2124',
  14863. 'Zscr': '\uD835\uDCB5',
  14864. 'aacute': '\u00E1',
  14865. 'abreve': '\u0103',
  14866. 'ac': '\u223E',
  14867. 'mstpos': '\u223E',
  14868. 'acE': '\u223E\u0333',
  14869. 'acd': '\u223F',
  14870. 'acirc': '\u00E2',
  14871. 'acy': '\u0430',
  14872. 'aelig': '\u00E6',
  14873. 'afr': '\uD835\uDD1E',
  14874. 'agrave': '\u00E0',
  14875. 'alefsym': '\u2135',
  14876. 'aleph': '\u2135',
  14877. 'alpha': '\u03B1',
  14878. 'amacr': '\u0101',
  14879. 'amalg': '\u2A3F',
  14880. 'and': '\u2227',
  14881. 'wedge': '\u2227',
  14882. 'andand': '\u2A55',
  14883. 'andd': '\u2A5C',
  14884. 'andslope': '\u2A58',
  14885. 'andv': '\u2A5A',
  14886. 'ang': '\u2220',
  14887. 'angle': '\u2220',
  14888. 'ange': '\u29A4',
  14889. 'angmsd': '\u2221',
  14890. 'measuredangle': '\u2221',
  14891. 'angmsdaa': '\u29A8',
  14892. 'angmsdab': '\u29A9',
  14893. 'angmsdac': '\u29AA',
  14894. 'angmsdad': '\u29AB',
  14895. 'angmsdae': '\u29AC',
  14896. 'angmsdaf': '\u29AD',
  14897. 'angmsdag': '\u29AE',
  14898. 'angmsdah': '\u29AF',
  14899. 'angrt': '\u221F',
  14900. 'angrtvb': '\u22BE',
  14901. 'angrtvbd': '\u299D',
  14902. 'angsph': '\u2222',
  14903. 'angzarr': '\u237C',
  14904. 'aogon': '\u0105',
  14905. 'aopf': '\uD835\uDD52',
  14906. 'apE': '\u2A70',
  14907. 'apacir': '\u2A6F',
  14908. 'ape': '\u224A',
  14909. 'approxeq': '\u224A',
  14910. 'apid': '\u224B',
  14911. 'apos': '\u0027',
  14912. 'aring': '\u00E5',
  14913. 'ascr': '\uD835\uDCB6',
  14914. 'ast': '\u002A',
  14915. 'midast': '\u002A',
  14916. 'atilde': '\u00E3',
  14917. 'auml': '\u00E4',
  14918. 'awint': '\u2A11',
  14919. 'bNot': '\u2AED',
  14920. 'backcong': '\u224C',
  14921. 'bcong': '\u224C',
  14922. 'backepsilon': '\u03F6',
  14923. 'bepsi': '\u03F6',
  14924. 'backprime': '\u2035',
  14925. 'bprime': '\u2035',
  14926. 'backsim': '\u223D',
  14927. 'bsim': '\u223D',
  14928. 'backsimeq': '\u22CD',
  14929. 'bsime': '\u22CD',
  14930. 'barvee': '\u22BD',
  14931. 'barwed': '\u2305',
  14932. 'barwedge': '\u2305',
  14933. 'bbrktbrk': '\u23B6',
  14934. 'bcy': '\u0431',
  14935. 'bdquo': '\u201E',
  14936. 'ldquor': '\u201E',
  14937. 'bemptyv': '\u29B0',
  14938. 'beta': '\u03B2',
  14939. 'beth': '\u2136',
  14940. 'between': '\u226C',
  14941. 'twixt': '\u226C',
  14942. 'bfr': '\uD835\uDD1F',
  14943. 'bigcirc': '\u25EF',
  14944. 'xcirc': '\u25EF',
  14945. 'bigodot': '\u2A00',
  14946. 'xodot': '\u2A00',
  14947. 'bigoplus': '\u2A01',
  14948. 'xoplus': '\u2A01',
  14949. 'bigotimes': '\u2A02',
  14950. 'xotime': '\u2A02',
  14951. 'bigsqcup': '\u2A06',
  14952. 'xsqcup': '\u2A06',
  14953. 'bigstar': '\u2605',
  14954. 'starf': '\u2605',
  14955. 'bigtriangledown': '\u25BD',
  14956. 'xdtri': '\u25BD',
  14957. 'bigtriangleup': '\u25B3',
  14958. 'xutri': '\u25B3',
  14959. 'biguplus': '\u2A04',
  14960. 'xuplus': '\u2A04',
  14961. 'bkarow': '\u290D',
  14962. 'rbarr': '\u290D',
  14963. 'blacklozenge': '\u29EB',
  14964. 'lozf': '\u29EB',
  14965. 'blacktriangle': '\u25B4',
  14966. 'utrif': '\u25B4',
  14967. 'blacktriangledown': '\u25BE',
  14968. 'dtrif': '\u25BE',
  14969. 'blacktriangleleft': '\u25C2',
  14970. 'ltrif': '\u25C2',
  14971. 'blacktriangleright': '\u25B8',
  14972. 'rtrif': '\u25B8',
  14973. 'blank': '\u2423',
  14974. 'blk12': '\u2592',
  14975. 'blk14': '\u2591',
  14976. 'blk34': '\u2593',
  14977. 'block': '\u2588',
  14978. 'bne': '\u003D\u20E5',
  14979. 'bnequiv': '\u2261\u20E5',
  14980. 'bnot': '\u2310',
  14981. 'bopf': '\uD835\uDD53',
  14982. 'bowtie': '\u22C8',
  14983. 'boxDL': '\u2557',
  14984. 'boxDR': '\u2554',
  14985. 'boxDl': '\u2556',
  14986. 'boxDr': '\u2553',
  14987. 'boxH': '\u2550',
  14988. 'boxHD': '\u2566',
  14989. 'boxHU': '\u2569',
  14990. 'boxHd': '\u2564',
  14991. 'boxHu': '\u2567',
  14992. 'boxUL': '\u255D',
  14993. 'boxUR': '\u255A',
  14994. 'boxUl': '\u255C',
  14995. 'boxUr': '\u2559',
  14996. 'boxV': '\u2551',
  14997. 'boxVH': '\u256C',
  14998. 'boxVL': '\u2563',
  14999. 'boxVR': '\u2560',
  15000. 'boxVh': '\u256B',
  15001. 'boxVl': '\u2562',
  15002. 'boxVr': '\u255F',
  15003. 'boxbox': '\u29C9',
  15004. 'boxdL': '\u2555',
  15005. 'boxdR': '\u2552',
  15006. 'boxdl': '\u2510',
  15007. 'boxdr': '\u250C',
  15008. 'boxhD': '\u2565',
  15009. 'boxhU': '\u2568',
  15010. 'boxhd': '\u252C',
  15011. 'boxhu': '\u2534',
  15012. 'boxminus': '\u229F',
  15013. 'minusb': '\u229F',
  15014. 'boxplus': '\u229E',
  15015. 'plusb': '\u229E',
  15016. 'boxtimes': '\u22A0',
  15017. 'timesb': '\u22A0',
  15018. 'boxuL': '\u255B',
  15019. 'boxuR': '\u2558',
  15020. 'boxul': '\u2518',
  15021. 'boxur': '\u2514',
  15022. 'boxv': '\u2502',
  15023. 'boxvH': '\u256A',
  15024. 'boxvL': '\u2561',
  15025. 'boxvR': '\u255E',
  15026. 'boxvh': '\u253C',
  15027. 'boxvl': '\u2524',
  15028. 'boxvr': '\u251C',
  15029. 'brvbar': '\u00A6',
  15030. 'bscr': '\uD835\uDCB7',
  15031. 'bsemi': '\u204F',
  15032. 'bsol': '\u005C',
  15033. 'bsolb': '\u29C5',
  15034. 'bsolhsub': '\u27C8',
  15035. 'bull': '\u2022',
  15036. 'bullet': '\u2022',
  15037. 'bumpE': '\u2AAE',
  15038. 'cacute': '\u0107',
  15039. 'cap': '\u2229',
  15040. 'capand': '\u2A44',
  15041. 'capbrcup': '\u2A49',
  15042. 'capcap': '\u2A4B',
  15043. 'capcup': '\u2A47',
  15044. 'capdot': '\u2A40',
  15045. 'caps': '\u2229\uFE00',
  15046. 'caret': '\u2041',
  15047. 'ccaps': '\u2A4D',
  15048. 'ccaron': '\u010D',
  15049. 'ccedil': '\u00E7',
  15050. 'ccirc': '\u0109',
  15051. 'ccups': '\u2A4C',
  15052. 'ccupssm': '\u2A50',
  15053. 'cdot': '\u010B',
  15054. 'cemptyv': '\u29B2',
  15055. 'cent': '\u00A2',
  15056. 'cfr': '\uD835\uDD20',
  15057. 'chcy': '\u0447',
  15058. 'check': '\u2713',
  15059. 'checkmark': '\u2713',
  15060. 'chi': '\u03C7',
  15061. 'cir': '\u25CB',
  15062. 'cirE': '\u29C3',
  15063. 'circ': '\u02C6',
  15064. 'circeq': '\u2257',
  15065. 'cire': '\u2257',
  15066. 'circlearrowleft': '\u21BA',
  15067. 'olarr': '\u21BA',
  15068. 'circlearrowright': '\u21BB',
  15069. 'orarr': '\u21BB',
  15070. 'circledS': '\u24C8',
  15071. 'oS': '\u24C8',
  15072. 'circledast': '\u229B',
  15073. 'oast': '\u229B',
  15074. 'circledcirc': '\u229A',
  15075. 'ocir': '\u229A',
  15076. 'circleddash': '\u229D',
  15077. 'odash': '\u229D',
  15078. 'cirfnint': '\u2A10',
  15079. 'cirmid': '\u2AEF',
  15080. 'cirscir': '\u29C2',
  15081. 'clubs': '\u2663',
  15082. 'clubsuit': '\u2663',
  15083. 'colon': '\u003A',
  15084. 'comma': '\u002C',
  15085. 'commat': '\u0040',
  15086. 'comp': '\u2201',
  15087. 'complement': '\u2201',
  15088. 'congdot': '\u2A6D',
  15089. 'copf': '\uD835\uDD54',
  15090. 'copysr': '\u2117',
  15091. 'crarr': '\u21B5',
  15092. 'cross': '\u2717',
  15093. 'cscr': '\uD835\uDCB8',
  15094. 'csub': '\u2ACF',
  15095. 'csube': '\u2AD1',
  15096. 'csup': '\u2AD0',
  15097. 'csupe': '\u2AD2',
  15098. 'ctdot': '\u22EF',
  15099. 'cudarrl': '\u2938',
  15100. 'cudarrr': '\u2935',
  15101. 'cuepr': '\u22DE',
  15102. 'curlyeqprec': '\u22DE',
  15103. 'cuesc': '\u22DF',
  15104. 'curlyeqsucc': '\u22DF',
  15105. 'cularr': '\u21B6',
  15106. 'curvearrowleft': '\u21B6',
  15107. 'cularrp': '\u293D',
  15108. 'cup': '\u222A',
  15109. 'cupbrcap': '\u2A48',
  15110. 'cupcap': '\u2A46',
  15111. 'cupcup': '\u2A4A',
  15112. 'cupdot': '\u228D',
  15113. 'cupor': '\u2A45',
  15114. 'cups': '\u222A\uFE00',
  15115. 'curarr': '\u21B7',
  15116. 'curvearrowright': '\u21B7',
  15117. 'curarrm': '\u293C',
  15118. 'curlyvee': '\u22CE',
  15119. 'cuvee': '\u22CE',
  15120. 'curlywedge': '\u22CF',
  15121. 'cuwed': '\u22CF',
  15122. 'curren': '\u00A4',
  15123. 'cwint': '\u2231',
  15124. 'cylcty': '\u232D',
  15125. 'dHar': '\u2965',
  15126. 'dagger': '\u2020',
  15127. 'daleth': '\u2138',
  15128. 'dash': '\u2010',
  15129. 'hyphen': '\u2010',
  15130. 'dbkarow': '\u290F',
  15131. 'rBarr': '\u290F',
  15132. 'dcaron': '\u010F',
  15133. 'dcy': '\u0434',
  15134. 'ddarr': '\u21CA',
  15135. 'downdownarrows': '\u21CA',
  15136. 'ddotseq': '\u2A77',
  15137. 'eDDot': '\u2A77',
  15138. 'deg': '\u00B0',
  15139. 'delta': '\u03B4',
  15140. 'demptyv': '\u29B1',
  15141. 'dfisht': '\u297F',
  15142. 'dfr': '\uD835\uDD21',
  15143. 'diamondsuit': '\u2666',
  15144. 'diams': '\u2666',
  15145. 'digamma': '\u03DD',
  15146. 'gammad': '\u03DD',
  15147. 'disin': '\u22F2',
  15148. 'div': '\u00F7',
  15149. 'divide': '\u00F7',
  15150. 'divideontimes': '\u22C7',
  15151. 'divonx': '\u22C7',
  15152. 'djcy': '\u0452',
  15153. 'dlcorn': '\u231E',
  15154. 'llcorner': '\u231E',
  15155. 'dlcrop': '\u230D',
  15156. 'dollar': '\u0024',
  15157. 'dopf': '\uD835\uDD55',
  15158. 'doteqdot': '\u2251',
  15159. 'eDot': '\u2251',
  15160. 'dotminus': '\u2238',
  15161. 'minusd': '\u2238',
  15162. 'dotplus': '\u2214',
  15163. 'plusdo': '\u2214',
  15164. 'dotsquare': '\u22A1',
  15165. 'sdotb': '\u22A1',
  15166. 'drcorn': '\u231F',
  15167. 'lrcorner': '\u231F',
  15168. 'drcrop': '\u230C',
  15169. 'dscr': '\uD835\uDCB9',
  15170. 'dscy': '\u0455',
  15171. 'dsol': '\u29F6',
  15172. 'dstrok': '\u0111',
  15173. 'dtdot': '\u22F1',
  15174. 'dtri': '\u25BF',
  15175. 'triangledown': '\u25BF',
  15176. 'dwangle': '\u29A6',
  15177. 'dzcy': '\u045F',
  15178. 'dzigrarr': '\u27FF',
  15179. 'eacute': '\u00E9',
  15180. 'easter': '\u2A6E',
  15181. 'ecaron': '\u011B',
  15182. 'ecir': '\u2256',
  15183. 'eqcirc': '\u2256',
  15184. 'ecirc': '\u00EA',
  15185. 'ecolon': '\u2255',
  15186. 'eqcolon': '\u2255',
  15187. 'ecy': '\u044D',
  15188. 'edot': '\u0117',
  15189. 'efDot': '\u2252',
  15190. 'fallingdotseq': '\u2252',
  15191. 'efr': '\uD835\uDD22',
  15192. 'eg': '\u2A9A',
  15193. 'egrave': '\u00E8',
  15194. 'egs': '\u2A96',
  15195. 'eqslantgtr': '\u2A96',
  15196. 'egsdot': '\u2A98',
  15197. 'el': '\u2A99',
  15198. 'elinters': '\u23E7',
  15199. 'ell': '\u2113',
  15200. 'els': '\u2A95',
  15201. 'eqslantless': '\u2A95',
  15202. 'elsdot': '\u2A97',
  15203. 'emacr': '\u0113',
  15204. 'empty': '\u2205',
  15205. 'emptyset': '\u2205',
  15206. 'emptyv': '\u2205',
  15207. 'varnothing': '\u2205',
  15208. 'emsp13': '\u2004',
  15209. 'emsp14': '\u2005',
  15210. 'emsp': '\u2003',
  15211. 'eng': '\u014B',
  15212. 'ensp': '\u2002',
  15213. 'eogon': '\u0119',
  15214. 'eopf': '\uD835\uDD56',
  15215. 'epar': '\u22D5',
  15216. 'eparsl': '\u29E3',
  15217. 'eplus': '\u2A71',
  15218. 'epsi': '\u03B5',
  15219. 'epsilon': '\u03B5',
  15220. 'epsiv': '\u03F5',
  15221. 'straightepsilon': '\u03F5',
  15222. 'varepsilon': '\u03F5',
  15223. 'equals': '\u003D',
  15224. 'equest': '\u225F',
  15225. 'questeq': '\u225F',
  15226. 'equivDD': '\u2A78',
  15227. 'eqvparsl': '\u29E5',
  15228. 'erDot': '\u2253',
  15229. 'risingdotseq': '\u2253',
  15230. 'erarr': '\u2971',
  15231. 'escr': '\u212F',
  15232. 'eta': '\u03B7',
  15233. 'eth': '\u00F0',
  15234. 'euml': '\u00EB',
  15235. 'euro': '\u20AC',
  15236. 'excl': '\u0021',
  15237. 'fcy': '\u0444',
  15238. 'female': '\u2640',
  15239. 'ffilig': '\uFB03',
  15240. 'fflig': '\uFB00',
  15241. 'ffllig': '\uFB04',
  15242. 'ffr': '\uD835\uDD23',
  15243. 'filig': '\uFB01',
  15244. 'fjlig': '\u0066\u006A',
  15245. 'flat': '\u266D',
  15246. 'fllig': '\uFB02',
  15247. 'fltns': '\u25B1',
  15248. 'fnof': '\u0192',
  15249. 'fopf': '\uD835\uDD57',
  15250. 'fork': '\u22D4',
  15251. 'pitchfork': '\u22D4',
  15252. 'forkv': '\u2AD9',
  15253. 'fpartint': '\u2A0D',
  15254. 'frac12': '\u00BD',
  15255. 'half': '\u00BD',
  15256. 'frac13': '\u2153',
  15257. 'frac14': '\u00BC',
  15258. 'frac15': '\u2155',
  15259. 'frac16': '\u2159',
  15260. 'frac18': '\u215B',
  15261. 'frac23': '\u2154',
  15262. 'frac25': '\u2156',
  15263. 'frac34': '\u00BE',
  15264. 'frac35': '\u2157',
  15265. 'frac38': '\u215C',
  15266. 'frac45': '\u2158',
  15267. 'frac56': '\u215A',
  15268. 'frac58': '\u215D',
  15269. 'frac78': '\u215E',
  15270. 'frasl': '\u2044',
  15271. 'frown': '\u2322',
  15272. 'sfrown': '\u2322',
  15273. 'fscr': '\uD835\uDCBB',
  15274. 'gEl': '\u2A8C',
  15275. 'gtreqqless': '\u2A8C',
  15276. 'gacute': '\u01F5',
  15277. 'gamma': '\u03B3',
  15278. 'gap': '\u2A86',
  15279. 'gtrapprox': '\u2A86',
  15280. 'gbreve': '\u011F',
  15281. 'gcirc': '\u011D',
  15282. 'gcy': '\u0433',
  15283. 'gdot': '\u0121',
  15284. 'gescc': '\u2AA9',
  15285. 'gesdot': '\u2A80',
  15286. 'gesdoto': '\u2A82',
  15287. 'gesdotol': '\u2A84',
  15288. 'gesl': '\u22DB\uFE00',
  15289. 'gesles': '\u2A94',
  15290. 'gfr': '\uD835\uDD24',
  15291. 'gimel': '\u2137',
  15292. 'gjcy': '\u0453',
  15293. 'glE': '\u2A92',
  15294. 'gla': '\u2AA5',
  15295. 'glj': '\u2AA4',
  15296. 'gnE': '\u2269',
  15297. 'gneqq': '\u2269',
  15298. 'gnap': '\u2A8A',
  15299. 'gnapprox': '\u2A8A',
  15300. 'gne': '\u2A88',
  15301. 'gneq': '\u2A88',
  15302. 'gnsim': '\u22E7',
  15303. 'gopf': '\uD835\uDD58',
  15304. 'gscr': '\u210A',
  15305. 'gsime': '\u2A8E',
  15306. 'gsiml': '\u2A90',
  15307. 'gtcc': '\u2AA7',
  15308. 'gtcir': '\u2A7A',
  15309. 'gtdot': '\u22D7',
  15310. 'gtrdot': '\u22D7',
  15311. 'gtlPar': '\u2995',
  15312. 'gtquest': '\u2A7C',
  15313. 'gtrarr': '\u2978',
  15314. 'gvertneqq': '\u2269\uFE00',
  15315. 'gvnE': '\u2269\uFE00',
  15316. 'hardcy': '\u044A',
  15317. 'harrcir': '\u2948',
  15318. 'harrw': '\u21AD',
  15319. 'leftrightsquigarrow': '\u21AD',
  15320. 'hbar': '\u210F',
  15321. 'hslash': '\u210F',
  15322. 'planck': '\u210F',
  15323. 'plankv': '\u210F',
  15324. 'hcirc': '\u0125',
  15325. 'hearts': '\u2665',
  15326. 'heartsuit': '\u2665',
  15327. 'hellip': '\u2026',
  15328. 'mldr': '\u2026',
  15329. 'hercon': '\u22B9',
  15330. 'hfr': '\uD835\uDD25',
  15331. 'hksearow': '\u2925',
  15332. 'searhk': '\u2925',
  15333. 'hkswarow': '\u2926',
  15334. 'swarhk': '\u2926',
  15335. 'hoarr': '\u21FF',
  15336. 'homtht': '\u223B',
  15337. 'hookleftarrow': '\u21A9',
  15338. 'larrhk': '\u21A9',
  15339. 'hookrightarrow': '\u21AA',
  15340. 'rarrhk': '\u21AA',
  15341. 'hopf': '\uD835\uDD59',
  15342. 'horbar': '\u2015',
  15343. 'hscr': '\uD835\uDCBD',
  15344. 'hstrok': '\u0127',
  15345. 'hybull': '\u2043',
  15346. 'iacute': '\u00ED',
  15347. 'icirc': '\u00EE',
  15348. 'icy': '\u0438',
  15349. 'iecy': '\u0435',
  15350. 'iexcl': '\u00A1',
  15351. 'ifr': '\uD835\uDD26',
  15352. 'igrave': '\u00EC',
  15353. 'iiiint': '\u2A0C',
  15354. 'qint': '\u2A0C',
  15355. 'iiint': '\u222D',
  15356. 'tint': '\u222D',
  15357. 'iinfin': '\u29DC',
  15358. 'iiota': '\u2129',
  15359. 'ijlig': '\u0133',
  15360. 'imacr': '\u012B',
  15361. 'imath': '\u0131',
  15362. 'inodot': '\u0131',
  15363. 'imof': '\u22B7',
  15364. 'imped': '\u01B5',
  15365. 'incare': '\u2105',
  15366. 'infin': '\u221E',
  15367. 'infintie': '\u29DD',
  15368. 'intcal': '\u22BA',
  15369. 'intercal': '\u22BA',
  15370. 'intlarhk': '\u2A17',
  15371. 'intprod': '\u2A3C',
  15372. 'iprod': '\u2A3C',
  15373. 'iocy': '\u0451',
  15374. 'iogon': '\u012F',
  15375. 'iopf': '\uD835\uDD5A',
  15376. 'iota': '\u03B9',
  15377. 'iquest': '\u00BF',
  15378. 'iscr': '\uD835\uDCBE',
  15379. 'isinE': '\u22F9',
  15380. 'isindot': '\u22F5',
  15381. 'isins': '\u22F4',
  15382. 'isinsv': '\u22F3',
  15383. 'itilde': '\u0129',
  15384. 'iukcy': '\u0456',
  15385. 'iuml': '\u00EF',
  15386. 'jcirc': '\u0135',
  15387. 'jcy': '\u0439',
  15388. 'jfr': '\uD835\uDD27',
  15389. 'jmath': '\u0237',
  15390. 'jopf': '\uD835\uDD5B',
  15391. 'jscr': '\uD835\uDCBF',
  15392. 'jsercy': '\u0458',
  15393. 'jukcy': '\u0454',
  15394. 'kappa': '\u03BA',
  15395. 'kappav': '\u03F0',
  15396. 'varkappa': '\u03F0',
  15397. 'kcedil': '\u0137',
  15398. 'kcy': '\u043A',
  15399. 'kfr': '\uD835\uDD28',
  15400. 'kgreen': '\u0138',
  15401. 'khcy': '\u0445',
  15402. 'kjcy': '\u045C',
  15403. 'kopf': '\uD835\uDD5C',
  15404. 'kscr': '\uD835\uDCC0',
  15405. 'lAtail': '\u291B',
  15406. 'lBarr': '\u290E',
  15407. 'lEg': '\u2A8B',
  15408. 'lesseqqgtr': '\u2A8B',
  15409. 'lHar': '\u2962',
  15410. 'lacute': '\u013A',
  15411. 'laemptyv': '\u29B4',
  15412. 'lambda': '\u03BB',
  15413. 'langd': '\u2991',
  15414. 'lap': '\u2A85',
  15415. 'lessapprox': '\u2A85',
  15416. 'laquo': '\u00AB',
  15417. 'larrbfs': '\u291F',
  15418. 'larrfs': '\u291D',
  15419. 'larrlp': '\u21AB',
  15420. 'looparrowleft': '\u21AB',
  15421. 'larrpl': '\u2939',
  15422. 'larrsim': '\u2973',
  15423. 'larrtl': '\u21A2',
  15424. 'leftarrowtail': '\u21A2',
  15425. 'lat': '\u2AAB',
  15426. 'latail': '\u2919',
  15427. 'late': '\u2AAD',
  15428. 'lates': '\u2AAD\uFE00',
  15429. 'lbarr': '\u290C',
  15430. 'lbbrk': '\u2772',
  15431. 'lbrace': '\u007B',
  15432. 'lcub': '\u007B',
  15433. 'lbrack': '\u005B',
  15434. 'lsqb': '\u005B',
  15435. 'lbrke': '\u298B',
  15436. 'lbrksld': '\u298F',
  15437. 'lbrkslu': '\u298D',
  15438. 'lcaron': '\u013E',
  15439. 'lcedil': '\u013C',
  15440. 'lcy': '\u043B',
  15441. 'ldca': '\u2936',
  15442. 'ldrdhar': '\u2967',
  15443. 'ldrushar': '\u294B',
  15444. 'ldsh': '\u21B2',
  15445. 'le': '\u2264',
  15446. 'leq': '\u2264',
  15447. 'leftleftarrows': '\u21C7',
  15448. 'llarr': '\u21C7',
  15449. 'leftthreetimes': '\u22CB',
  15450. 'lthree': '\u22CB',
  15451. 'lescc': '\u2AA8',
  15452. 'lesdot': '\u2A7F',
  15453. 'lesdoto': '\u2A81',
  15454. 'lesdotor': '\u2A83',
  15455. 'lesg': '\u22DA\uFE00',
  15456. 'lesges': '\u2A93',
  15457. 'lessdot': '\u22D6',
  15458. 'ltdot': '\u22D6',
  15459. 'lfisht': '\u297C',
  15460. 'lfr': '\uD835\uDD29',
  15461. 'lgE': '\u2A91',
  15462. 'lharul': '\u296A',
  15463. 'lhblk': '\u2584',
  15464. 'ljcy': '\u0459',
  15465. 'llhard': '\u296B',
  15466. 'lltri': '\u25FA',
  15467. 'lmidot': '\u0140',
  15468. 'lmoust': '\u23B0',
  15469. 'lmoustache': '\u23B0',
  15470. 'lnE': '\u2268',
  15471. 'lneqq': '\u2268',
  15472. 'lnap': '\u2A89',
  15473. 'lnapprox': '\u2A89',
  15474. 'lne': '\u2A87',
  15475. 'lneq': '\u2A87',
  15476. 'lnsim': '\u22E6',
  15477. 'loang': '\u27EC',
  15478. 'loarr': '\u21FD',
  15479. 'longmapsto': '\u27FC',
  15480. 'xmap': '\u27FC',
  15481. 'looparrowright': '\u21AC',
  15482. 'rarrlp': '\u21AC',
  15483. 'lopar': '\u2985',
  15484. 'lopf': '\uD835\uDD5D',
  15485. 'loplus': '\u2A2D',
  15486. 'lotimes': '\u2A34',
  15487. 'lowast': '\u2217',
  15488. 'loz': '\u25CA',
  15489. 'lozenge': '\u25CA',
  15490. 'lpar': '\u0028',
  15491. 'lparlt': '\u2993',
  15492. 'lrhard': '\u296D',
  15493. 'lrm': '\u200E',
  15494. 'lrtri': '\u22BF',
  15495. 'lsaquo': '\u2039',
  15496. 'lscr': '\uD835\uDCC1',
  15497. 'lsime': '\u2A8D',
  15498. 'lsimg': '\u2A8F',
  15499. 'lsquor': '\u201A',
  15500. 'sbquo': '\u201A',
  15501. 'lstrok': '\u0142',
  15502. 'ltcc': '\u2AA6',
  15503. 'ltcir': '\u2A79',
  15504. 'ltimes': '\u22C9',
  15505. 'ltlarr': '\u2976',
  15506. 'ltquest': '\u2A7B',
  15507. 'ltrPar': '\u2996',
  15508. 'ltri': '\u25C3',
  15509. 'triangleleft': '\u25C3',
  15510. 'lurdshar': '\u294A',
  15511. 'luruhar': '\u2966',
  15512. 'lvertneqq': '\u2268\uFE00',
  15513. 'lvnE': '\u2268\uFE00',
  15514. 'mDDot': '\u223A',
  15515. 'macr': '\u00AF',
  15516. 'strns': '\u00AF',
  15517. 'male': '\u2642',
  15518. 'malt': '\u2720',
  15519. 'maltese': '\u2720',
  15520. 'marker': '\u25AE',
  15521. 'mcomma': '\u2A29',
  15522. 'mcy': '\u043C',
  15523. 'mdash': '\u2014',
  15524. 'mfr': '\uD835\uDD2A',
  15525. 'mho': '\u2127',
  15526. 'micro': '\u00B5',
  15527. 'midcir': '\u2AF0',
  15528. 'minus': '\u2212',
  15529. 'minusdu': '\u2A2A',
  15530. 'mlcp': '\u2ADB',
  15531. 'models': '\u22A7',
  15532. 'mopf': '\uD835\uDD5E',
  15533. 'mscr': '\uD835\uDCC2',
  15534. 'mu': '\u03BC',
  15535. 'multimap': '\u22B8',
  15536. 'mumap': '\u22B8',
  15537. 'nGg': '\u22D9\u0338',
  15538. 'nGt': '\u226B\u20D2',
  15539. 'nLeftarrow': '\u21CD',
  15540. 'nlArr': '\u21CD',
  15541. 'nLeftrightarrow': '\u21CE',
  15542. 'nhArr': '\u21CE',
  15543. 'nLl': '\u22D8\u0338',
  15544. 'nLt': '\u226A\u20D2',
  15545. 'nRightarrow': '\u21CF',
  15546. 'nrArr': '\u21CF',
  15547. 'nVDash': '\u22AF',
  15548. 'nVdash': '\u22AE',
  15549. 'nacute': '\u0144',
  15550. 'nang': '\u2220\u20D2',
  15551. 'napE': '\u2A70\u0338',
  15552. 'napid': '\u224B\u0338',
  15553. 'napos': '\u0149',
  15554. 'natur': '\u266E',
  15555. 'natural': '\u266E',
  15556. 'ncap': '\u2A43',
  15557. 'ncaron': '\u0148',
  15558. 'ncedil': '\u0146',
  15559. 'ncongdot': '\u2A6D\u0338',
  15560. 'ncup': '\u2A42',
  15561. 'ncy': '\u043D',
  15562. 'ndash': '\u2013',
  15563. 'neArr': '\u21D7',
  15564. 'nearhk': '\u2924',
  15565. 'nedot': '\u2250\u0338',
  15566. 'nesear': '\u2928',
  15567. 'toea': '\u2928',
  15568. 'nfr': '\uD835\uDD2B',
  15569. 'nharr': '\u21AE',
  15570. 'nleftrightarrow': '\u21AE',
  15571. 'nhpar': '\u2AF2',
  15572. 'nis': '\u22FC',
  15573. 'nisd': '\u22FA',
  15574. 'njcy': '\u045A',
  15575. 'nlE': '\u2266\u0338',
  15576. 'nleqq': '\u2266\u0338',
  15577. 'nlarr': '\u219A',
  15578. 'nleftarrow': '\u219A',
  15579. 'nldr': '\u2025',
  15580. 'nopf': '\uD835\uDD5F',
  15581. 'not': '\u00AC',
  15582. 'notinE': '\u22F9\u0338',
  15583. 'notindot': '\u22F5\u0338',
  15584. 'notinvb': '\u22F7',
  15585. 'notinvc': '\u22F6',
  15586. 'notnivb': '\u22FE',
  15587. 'notnivc': '\u22FD',
  15588. 'nparsl': '\u2AFD\u20E5',
  15589. 'npart': '\u2202\u0338',
  15590. 'npolint': '\u2A14',
  15591. 'nrarr': '\u219B',
  15592. 'nrightarrow': '\u219B',
  15593. 'nrarrc': '\u2933\u0338',
  15594. 'nrarrw': '\u219D\u0338',
  15595. 'nscr': '\uD835\uDCC3',
  15596. 'nsub': '\u2284',
  15597. 'nsubE': '\u2AC5\u0338',
  15598. 'nsubseteqq': '\u2AC5\u0338',
  15599. 'nsup': '\u2285',
  15600. 'nsupE': '\u2AC6\u0338',
  15601. 'nsupseteqq': '\u2AC6\u0338',
  15602. 'ntilde': '\u00F1',
  15603. 'nu': '\u03BD',
  15604. 'num': '\u0023',
  15605. 'numero': '\u2116',
  15606. 'numsp': '\u2007',
  15607. 'nvDash': '\u22AD',
  15608. 'nvHarr': '\u2904',
  15609. 'nvap': '\u224D\u20D2',
  15610. 'nvdash': '\u22AC',
  15611. 'nvge': '\u2265\u20D2',
  15612. 'nvgt': '\u003E\u20D2',
  15613. 'nvinfin': '\u29DE',
  15614. 'nvlArr': '\u2902',
  15615. 'nvle': '\u2264\u20D2',
  15616. 'nvlt': '\u003C\u20D2',
  15617. 'nvltrie': '\u22B4\u20D2',
  15618. 'nvrArr': '\u2903',
  15619. 'nvrtrie': '\u22B5\u20D2',
  15620. 'nvsim': '\u223C\u20D2',
  15621. 'nwArr': '\u21D6',
  15622. 'nwarhk': '\u2923',
  15623. 'nwnear': '\u2927',
  15624. 'oacute': '\u00F3',
  15625. 'ocirc': '\u00F4',
  15626. 'ocy': '\u043E',
  15627. 'odblac': '\u0151',
  15628. 'odiv': '\u2A38',
  15629. 'odsold': '\u29BC',
  15630. 'oelig': '\u0153',
  15631. 'ofcir': '\u29BF',
  15632. 'ofr': '\uD835\uDD2C',
  15633. 'ogon': '\u02DB',
  15634. 'ograve': '\u00F2',
  15635. 'ogt': '\u29C1',
  15636. 'ohbar': '\u29B5',
  15637. 'olcir': '\u29BE',
  15638. 'olcross': '\u29BB',
  15639. 'olt': '\u29C0',
  15640. 'omacr': '\u014D',
  15641. 'omega': '\u03C9',
  15642. 'omicron': '\u03BF',
  15643. 'omid': '\u29B6',
  15644. 'oopf': '\uD835\uDD60',
  15645. 'opar': '\u29B7',
  15646. 'operp': '\u29B9',
  15647. 'or': '\u2228',
  15648. 'vee': '\u2228',
  15649. 'ord': '\u2A5D',
  15650. 'order': '\u2134',
  15651. 'orderof': '\u2134',
  15652. 'oscr': '\u2134',
  15653. 'ordf': '\u00AA',
  15654. 'ordm': '\u00BA',
  15655. 'origof': '\u22B6',
  15656. 'oror': '\u2A56',
  15657. 'orslope': '\u2A57',
  15658. 'orv': '\u2A5B',
  15659. 'oslash': '\u00F8',
  15660. 'osol': '\u2298',
  15661. 'otilde': '\u00F5',
  15662. 'otimesas': '\u2A36',
  15663. 'ouml': '\u00F6',
  15664. 'ovbar': '\u233D',
  15665. 'para': '\u00B6',
  15666. 'parsim': '\u2AF3',
  15667. 'parsl': '\u2AFD',
  15668. 'pcy': '\u043F',
  15669. 'percnt': '\u0025',
  15670. 'period': '\u002E',
  15671. 'permil': '\u2030',
  15672. 'pertenk': '\u2031',
  15673. 'pfr': '\uD835\uDD2D',
  15674. 'phi': '\u03C6',
  15675. 'phiv': '\u03D5',
  15676. 'straightphi': '\u03D5',
  15677. 'varphi': '\u03D5',
  15678. 'phone': '\u260E',
  15679. 'pi': '\u03C0',
  15680. 'piv': '\u03D6',
  15681. 'varpi': '\u03D6',
  15682. 'planckh': '\u210E',
  15683. 'plus': '\u002B',
  15684. 'plusacir': '\u2A23',
  15685. 'pluscir': '\u2A22',
  15686. 'plusdu': '\u2A25',
  15687. 'pluse': '\u2A72',
  15688. 'plussim': '\u2A26',
  15689. 'plustwo': '\u2A27',
  15690. 'pointint': '\u2A15',
  15691. 'popf': '\uD835\uDD61',
  15692. 'pound': '\u00A3',
  15693. 'prE': '\u2AB3',
  15694. 'prap': '\u2AB7',
  15695. 'precapprox': '\u2AB7',
  15696. 'precnapprox': '\u2AB9',
  15697. 'prnap': '\u2AB9',
  15698. 'precneqq': '\u2AB5',
  15699. 'prnE': '\u2AB5',
  15700. 'precnsim': '\u22E8',
  15701. 'prnsim': '\u22E8',
  15702. 'prime': '\u2032',
  15703. 'profalar': '\u232E',
  15704. 'profline': '\u2312',
  15705. 'profsurf': '\u2313',
  15706. 'prurel': '\u22B0',
  15707. 'pscr': '\uD835\uDCC5',
  15708. 'psi': '\u03C8',
  15709. 'puncsp': '\u2008',
  15710. 'qfr': '\uD835\uDD2E',
  15711. 'qopf': '\uD835\uDD62',
  15712. 'qprime': '\u2057',
  15713. 'qscr': '\uD835\uDCC6',
  15714. 'quatint': '\u2A16',
  15715. 'quest': '\u003F',
  15716. 'rAtail': '\u291C',
  15717. 'rHar': '\u2964',
  15718. 'race': '\u223D\u0331',
  15719. 'racute': '\u0155',
  15720. 'raemptyv': '\u29B3',
  15721. 'rangd': '\u2992',
  15722. 'range': '\u29A5',
  15723. 'raquo': '\u00BB',
  15724. 'rarrap': '\u2975',
  15725. 'rarrbfs': '\u2920',
  15726. 'rarrc': '\u2933',
  15727. 'rarrfs': '\u291E',
  15728. 'rarrpl': '\u2945',
  15729. 'rarrsim': '\u2974',
  15730. 'rarrtl': '\u21A3',
  15731. 'rightarrowtail': '\u21A3',
  15732. 'rarrw': '\u219D',
  15733. 'rightsquigarrow': '\u219D',
  15734. 'ratail': '\u291A',
  15735. 'ratio': '\u2236',
  15736. 'rbbrk': '\u2773',
  15737. 'rbrace': '\u007D',
  15738. 'rcub': '\u007D',
  15739. 'rbrack': '\u005D',
  15740. 'rsqb': '\u005D',
  15741. 'rbrke': '\u298C',
  15742. 'rbrksld': '\u298E',
  15743. 'rbrkslu': '\u2990',
  15744. 'rcaron': '\u0159',
  15745. 'rcedil': '\u0157',
  15746. 'rcy': '\u0440',
  15747. 'rdca': '\u2937',
  15748. 'rdldhar': '\u2969',
  15749. 'rdsh': '\u21B3',
  15750. 'rect': '\u25AD',
  15751. 'rfisht': '\u297D',
  15752. 'rfr': '\uD835\uDD2F',
  15753. 'rharul': '\u296C',
  15754. 'rho': '\u03C1',
  15755. 'rhov': '\u03F1',
  15756. 'varrho': '\u03F1',
  15757. 'rightrightarrows': '\u21C9',
  15758. 'rrarr': '\u21C9',
  15759. 'rightthreetimes': '\u22CC',
  15760. 'rthree': '\u22CC',
  15761. 'ring': '\u02DA',
  15762. 'rlm': '\u200F',
  15763. 'rmoust': '\u23B1',
  15764. 'rmoustache': '\u23B1',
  15765. 'rnmid': '\u2AEE',
  15766. 'roang': '\u27ED',
  15767. 'roarr': '\u21FE',
  15768. 'ropar': '\u2986',
  15769. 'ropf': '\uD835\uDD63',
  15770. 'roplus': '\u2A2E',
  15771. 'rotimes': '\u2A35',
  15772. 'rpar': '\u0029',
  15773. 'rpargt': '\u2994',
  15774. 'rppolint': '\u2A12',
  15775. 'rsaquo': '\u203A',
  15776. 'rscr': '\uD835\uDCC7',
  15777. 'rtimes': '\u22CA',
  15778. 'rtri': '\u25B9',
  15779. 'triangleright': '\u25B9',
  15780. 'rtriltri': '\u29CE',
  15781. 'ruluhar': '\u2968',
  15782. 'rx': '\u211E',
  15783. 'sacute': '\u015B',
  15784. 'scE': '\u2AB4',
  15785. 'scap': '\u2AB8',
  15786. 'succapprox': '\u2AB8',
  15787. 'scaron': '\u0161',
  15788. 'scedil': '\u015F',
  15789. 'scirc': '\u015D',
  15790. 'scnE': '\u2AB6',
  15791. 'succneqq': '\u2AB6',
  15792. 'scnap': '\u2ABA',
  15793. 'succnapprox': '\u2ABA',
  15794. 'scnsim': '\u22E9',
  15795. 'succnsim': '\u22E9',
  15796. 'scpolint': '\u2A13',
  15797. 'scy': '\u0441',
  15798. 'sdot': '\u22C5',
  15799. 'sdote': '\u2A66',
  15800. 'seArr': '\u21D8',
  15801. 'sect': '\u00A7',
  15802. 'semi': '\u003B',
  15803. 'seswar': '\u2929',
  15804. 'tosa': '\u2929',
  15805. 'sext': '\u2736',
  15806. 'sfr': '\uD835\uDD30',
  15807. 'sharp': '\u266F',
  15808. 'shchcy': '\u0449',
  15809. 'shcy': '\u0448',
  15810. 'shy': '\u00AD',
  15811. 'sigma': '\u03C3',
  15812. 'sigmaf': '\u03C2',
  15813. 'sigmav': '\u03C2',
  15814. 'varsigma': '\u03C2',
  15815. 'simdot': '\u2A6A',
  15816. 'simg': '\u2A9E',
  15817. 'simgE': '\u2AA0',
  15818. 'siml': '\u2A9D',
  15819. 'simlE': '\u2A9F',
  15820. 'simne': '\u2246',
  15821. 'simplus': '\u2A24',
  15822. 'simrarr': '\u2972',
  15823. 'smashp': '\u2A33',
  15824. 'smeparsl': '\u29E4',
  15825. 'smile': '\u2323',
  15826. 'ssmile': '\u2323',
  15827. 'smt': '\u2AAA',
  15828. 'smte': '\u2AAC',
  15829. 'smtes': '\u2AAC\uFE00',
  15830. 'softcy': '\u044C',
  15831. 'sol': '\u002F',
  15832. 'solb': '\u29C4',
  15833. 'solbar': '\u233F',
  15834. 'sopf': '\uD835\uDD64',
  15835. 'spades': '\u2660',
  15836. 'spadesuit': '\u2660',
  15837. 'sqcaps': '\u2293\uFE00',
  15838. 'sqcups': '\u2294\uFE00',
  15839. 'sscr': '\uD835\uDCC8',
  15840. 'star': '\u2606',
  15841. 'sub': '\u2282',
  15842. 'subset': '\u2282',
  15843. 'subE': '\u2AC5',
  15844. 'subseteqq': '\u2AC5',
  15845. 'subdot': '\u2ABD',
  15846. 'subedot': '\u2AC3',
  15847. 'submult': '\u2AC1',
  15848. 'subnE': '\u2ACB',
  15849. 'subsetneqq': '\u2ACB',
  15850. 'subne': '\u228A',
  15851. 'subsetneq': '\u228A',
  15852. 'subplus': '\u2ABF',
  15853. 'subrarr': '\u2979',
  15854. 'subsim': '\u2AC7',
  15855. 'subsub': '\u2AD5',
  15856. 'subsup': '\u2AD3',
  15857. 'sung': '\u266A',
  15858. 'sup1': '\u00B9',
  15859. 'sup2': '\u00B2',
  15860. 'sup3': '\u00B3',
  15861. 'supE': '\u2AC6',
  15862. 'supseteqq': '\u2AC6',
  15863. 'supdot': '\u2ABE',
  15864. 'supdsub': '\u2AD8',
  15865. 'supedot': '\u2AC4',
  15866. 'suphsol': '\u27C9',
  15867. 'suphsub': '\u2AD7',
  15868. 'suplarr': '\u297B',
  15869. 'supmult': '\u2AC2',
  15870. 'supnE': '\u2ACC',
  15871. 'supsetneqq': '\u2ACC',
  15872. 'supne': '\u228B',
  15873. 'supsetneq': '\u228B',
  15874. 'supplus': '\u2AC0',
  15875. 'supsim': '\u2AC8',
  15876. 'supsub': '\u2AD4',
  15877. 'supsup': '\u2AD6',
  15878. 'swArr': '\u21D9',
  15879. 'swnwar': '\u292A',
  15880. 'szlig': '\u00DF',
  15881. 'target': '\u2316',
  15882. 'tau': '\u03C4',
  15883. 'tcaron': '\u0165',
  15884. 'tcedil': '\u0163',
  15885. 'tcy': '\u0442',
  15886. 'telrec': '\u2315',
  15887. 'tfr': '\uD835\uDD31',
  15888. 'theta': '\u03B8',
  15889. 'thetasym': '\u03D1',
  15890. 'thetav': '\u03D1',
  15891. 'vartheta': '\u03D1',
  15892. 'thorn': '\u00FE',
  15893. 'times': '\u00D7',
  15894. 'timesbar': '\u2A31',
  15895. 'timesd': '\u2A30',
  15896. 'topbot': '\u2336',
  15897. 'topcir': '\u2AF1',
  15898. 'topf': '\uD835\uDD65',
  15899. 'topfork': '\u2ADA',
  15900. 'tprime': '\u2034',
  15901. 'triangle': '\u25B5',
  15902. 'utri': '\u25B5',
  15903. 'triangleq': '\u225C',
  15904. 'trie': '\u225C',
  15905. 'tridot': '\u25EC',
  15906. 'triminus': '\u2A3A',
  15907. 'triplus': '\u2A39',
  15908. 'trisb': '\u29CD',
  15909. 'tritime': '\u2A3B',
  15910. 'trpezium': '\u23E2',
  15911. 'tscr': '\uD835\uDCC9',
  15912. 'tscy': '\u0446',
  15913. 'tshcy': '\u045B',
  15914. 'tstrok': '\u0167',
  15915. 'uHar': '\u2963',
  15916. 'uacute': '\u00FA',
  15917. 'ubrcy': '\u045E',
  15918. 'ubreve': '\u016D',
  15919. 'ucirc': '\u00FB',
  15920. 'ucy': '\u0443',
  15921. 'udblac': '\u0171',
  15922. 'ufisht': '\u297E',
  15923. 'ufr': '\uD835\uDD32',
  15924. 'ugrave': '\u00F9',
  15925. 'uhblk': '\u2580',
  15926. 'ulcorn': '\u231C',
  15927. 'ulcorner': '\u231C',
  15928. 'ulcrop': '\u230F',
  15929. 'ultri': '\u25F8',
  15930. 'umacr': '\u016B',
  15931. 'uogon': '\u0173',
  15932. 'uopf': '\uD835\uDD66',
  15933. 'upsi': '\u03C5',
  15934. 'upsilon': '\u03C5',
  15935. 'upuparrows': '\u21C8',
  15936. 'uuarr': '\u21C8',
  15937. 'urcorn': '\u231D',
  15938. 'urcorner': '\u231D',
  15939. 'urcrop': '\u230E',
  15940. 'uring': '\u016F',
  15941. 'urtri': '\u25F9',
  15942. 'uscr': '\uD835\uDCCA',
  15943. 'utdot': '\u22F0',
  15944. 'utilde': '\u0169',
  15945. 'uuml': '\u00FC',
  15946. 'uwangle': '\u29A7',
  15947. 'vBar': '\u2AE8',
  15948. 'vBarv': '\u2AE9',
  15949. 'vangrt': '\u299C',
  15950. 'varsubsetneq': '\u228A\uFE00',
  15951. 'vsubne': '\u228A\uFE00',
  15952. 'varsubsetneqq': '\u2ACB\uFE00',
  15953. 'vsubnE': '\u2ACB\uFE00',
  15954. 'varsupsetneq': '\u228B\uFE00',
  15955. 'vsupne': '\u228B\uFE00',
  15956. 'varsupsetneqq': '\u2ACC\uFE00',
  15957. 'vsupnE': '\u2ACC\uFE00',
  15958. 'vcy': '\u0432',
  15959. 'veebar': '\u22BB',
  15960. 'veeeq': '\u225A',
  15961. 'vellip': '\u22EE',
  15962. 'vfr': '\uD835\uDD33',
  15963. 'vopf': '\uD835\uDD67',
  15964. 'vscr': '\uD835\uDCCB',
  15965. 'vzigzag': '\u299A',
  15966. 'wcirc': '\u0175',
  15967. 'wedbar': '\u2A5F',
  15968. 'wedgeq': '\u2259',
  15969. 'weierp': '\u2118',
  15970. 'wp': '\u2118',
  15971. 'wfr': '\uD835\uDD34',
  15972. 'wopf': '\uD835\uDD68',
  15973. 'wscr': '\uD835\uDCCC',
  15974. 'xfr': '\uD835\uDD35',
  15975. 'xi': '\u03BE',
  15976. 'xnis': '\u22FB',
  15977. 'xopf': '\uD835\uDD69',
  15978. 'xscr': '\uD835\uDCCD',
  15979. 'yacute': '\u00FD',
  15980. 'yacy': '\u044F',
  15981. 'ycirc': '\u0177',
  15982. 'ycy': '\u044B',
  15983. 'yen': '\u00A5',
  15984. 'yfr': '\uD835\uDD36',
  15985. 'yicy': '\u0457',
  15986. 'yopf': '\uD835\uDD6A',
  15987. 'yscr': '\uD835\uDCCE',
  15988. 'yucy': '\u044E',
  15989. 'yuml': '\u00FF',
  15990. 'zacute': '\u017A',
  15991. 'zcaron': '\u017E',
  15992. 'zcy': '\u0437',
  15993. 'zdot': '\u017C',
  15994. 'zeta': '\u03B6',
  15995. 'zfr': '\uD835\uDD37',
  15996. 'zhcy': '\u0436',
  15997. 'zigrarr': '\u21DD',
  15998. 'zopf': '\uD835\uDD6B',
  15999. 'zscr': '\uD835\uDCCF',
  16000. 'zwj': '\u200D',
  16001. 'zwnj': '\u200C'
  16002. };
  16003. // The &ngsp; pseudo-entity is denoting a space.
  16004. // 0xE500 is a PUA (Private Use Areas) unicode character
  16005. // This is inspired by the Angular Dart implementation.
  16006. const NGSP_UNICODE = '\uE500';
  16007. NAMED_ENTITIES['ngsp'] = NGSP_UNICODE;
  16008. class TokenError extends ParseError {
  16009. constructor(errorMsg, tokenType, span) {
  16010. super(span, errorMsg);
  16011. this.tokenType = tokenType;
  16012. }
  16013. }
  16014. class TokenizeResult {
  16015. constructor(tokens, errors, nonNormalizedIcuExpressions) {
  16016. this.tokens = tokens;
  16017. this.errors = errors;
  16018. this.nonNormalizedIcuExpressions = nonNormalizedIcuExpressions;
  16019. }
  16020. }
  16021. function tokenize(source, url, getTagDefinition, options = {}) {
  16022. const tokenizer = new _Tokenizer(new ParseSourceFile(source, url), getTagDefinition, options);
  16023. tokenizer.tokenize();
  16024. return new TokenizeResult(mergeTextTokens(tokenizer.tokens), tokenizer.errors, tokenizer.nonNormalizedIcuExpressions);
  16025. }
  16026. const _CR_OR_CRLF_REGEXP = /\r\n?/g;
  16027. function _unexpectedCharacterErrorMsg(charCode) {
  16028. const char = charCode === $EOF ? 'EOF' : String.fromCharCode(charCode);
  16029. return `Unexpected character "${char}"`;
  16030. }
  16031. function _unknownEntityErrorMsg(entitySrc) {
  16032. return `Unknown entity "${entitySrc}" - use the "&#<decimal>;" or "&#x<hex>;" syntax`;
  16033. }
  16034. function _unparsableEntityErrorMsg(type, entityStr) {
  16035. return `Unable to parse entity "${entityStr}" - ${type} character reference entities must end with ";"`;
  16036. }
  16037. var CharacterReferenceType;
  16038. (function (CharacterReferenceType) {
  16039. CharacterReferenceType["HEX"] = "hexadecimal";
  16040. CharacterReferenceType["DEC"] = "decimal";
  16041. })(CharacterReferenceType || (CharacterReferenceType = {}));
  16042. class _ControlFlowError {
  16043. constructor(error) {
  16044. this.error = error;
  16045. }
  16046. }
  16047. // See https://www.w3.org/TR/html51/syntax.html#writing-html-documents
  16048. class _Tokenizer {
  16049. /**
  16050. * @param _file The html source file being tokenized.
  16051. * @param _getTagDefinition A function that will retrieve a tag definition for a given tag name.
  16052. * @param options Configuration of the tokenization.
  16053. */
  16054. constructor(_file, _getTagDefinition, options) {
  16055. this._getTagDefinition = _getTagDefinition;
  16056. this._currentTokenStart = null;
  16057. this._currentTokenType = null;
  16058. this._expansionCaseStack = [];
  16059. this._inInterpolation = false;
  16060. this.tokens = [];
  16061. this.errors = [];
  16062. this.nonNormalizedIcuExpressions = [];
  16063. this._tokenizeIcu = options.tokenizeExpansionForms || false;
  16064. this._interpolationConfig = options.interpolationConfig || DEFAULT_INTERPOLATION_CONFIG;
  16065. this._leadingTriviaCodePoints =
  16066. options.leadingTriviaChars && options.leadingTriviaChars.map(c => c.codePointAt(0) || 0);
  16067. const range = options.range || { endPos: _file.content.length, startPos: 0, startLine: 0, startCol: 0 };
  16068. this._cursor = options.escapedString ? new EscapedCharacterCursor(_file, range) :
  16069. new PlainCharacterCursor(_file, range);
  16070. this._preserveLineEndings = options.preserveLineEndings || false;
  16071. this._escapedString = options.escapedString || false;
  16072. this._i18nNormalizeLineEndingsInICUs = options.i18nNormalizeLineEndingsInICUs || false;
  16073. try {
  16074. this._cursor.init();
  16075. }
  16076. catch (e) {
  16077. this.handleError(e);
  16078. }
  16079. }
  16080. _processCarriageReturns(content) {
  16081. if (this._preserveLineEndings) {
  16082. return content;
  16083. }
  16084. // https://www.w3.org/TR/html51/syntax.html#preprocessing-the-input-stream
  16085. // In order to keep the original position in the source, we can not
  16086. // pre-process it.
  16087. // Instead CRs are processed right before instantiating the tokens.
  16088. return content.replace(_CR_OR_CRLF_REGEXP, '\n');
  16089. }
  16090. tokenize() {
  16091. while (this._cursor.peek() !== $EOF) {
  16092. const start = this._cursor.clone();
  16093. try {
  16094. if (this._attemptCharCode($LT)) {
  16095. if (this._attemptCharCode($BANG)) {
  16096. if (this._attemptCharCode($LBRACKET)) {
  16097. this._consumeCdata(start);
  16098. }
  16099. else if (this._attemptCharCode($MINUS)) {
  16100. this._consumeComment(start);
  16101. }
  16102. else {
  16103. this._consumeDocType(start);
  16104. }
  16105. }
  16106. else if (this._attemptCharCode($SLASH)) {
  16107. this._consumeTagClose(start);
  16108. }
  16109. else {
  16110. this._consumeTagOpen(start);
  16111. }
  16112. }
  16113. else if (!(this._tokenizeIcu && this._tokenizeExpansionForm())) {
  16114. // In (possibly interpolated) text the end of the text is given by `isTextEnd()`, while
  16115. // the premature end of an interpolation is given by the start of a new HTML element.
  16116. this._consumeWithInterpolation(5 /* TokenType.TEXT */, 8 /* TokenType.INTERPOLATION */, () => this._isTextEnd(), () => this._isTagStart());
  16117. }
  16118. }
  16119. catch (e) {
  16120. this.handleError(e);
  16121. }
  16122. }
  16123. this._beginToken(24 /* TokenType.EOF */);
  16124. this._endToken([]);
  16125. }
  16126. /**
  16127. * @returns whether an ICU token has been created
  16128. * @internal
  16129. */
  16130. _tokenizeExpansionForm() {
  16131. if (this.isExpansionFormStart()) {
  16132. this._consumeExpansionFormStart();
  16133. return true;
  16134. }
  16135. if (isExpansionCaseStart(this._cursor.peek()) && this._isInExpansionForm()) {
  16136. this._consumeExpansionCaseStart();
  16137. return true;
  16138. }
  16139. if (this._cursor.peek() === $RBRACE) {
  16140. if (this._isInExpansionCase()) {
  16141. this._consumeExpansionCaseEnd();
  16142. return true;
  16143. }
  16144. if (this._isInExpansionForm()) {
  16145. this._consumeExpansionFormEnd();
  16146. return true;
  16147. }
  16148. }
  16149. return false;
  16150. }
  16151. _beginToken(type, start = this._cursor.clone()) {
  16152. this._currentTokenStart = start;
  16153. this._currentTokenType = type;
  16154. }
  16155. _endToken(parts, end) {
  16156. if (this._currentTokenStart === null) {
  16157. throw new TokenError('Programming error - attempted to end a token when there was no start to the token', this._currentTokenType, this._cursor.getSpan(end));
  16158. }
  16159. if (this._currentTokenType === null) {
  16160. throw new TokenError('Programming error - attempted to end a token which has no token type', null, this._cursor.getSpan(this._currentTokenStart));
  16161. }
  16162. const token = {
  16163. type: this._currentTokenType,
  16164. parts,
  16165. sourceSpan: (end ?? this._cursor).getSpan(this._currentTokenStart, this._leadingTriviaCodePoints),
  16166. };
  16167. this.tokens.push(token);
  16168. this._currentTokenStart = null;
  16169. this._currentTokenType = null;
  16170. return token;
  16171. }
  16172. _createError(msg, span) {
  16173. if (this._isInExpansionForm()) {
  16174. msg += ` (Do you have an unescaped "{" in your template? Use "{{ '{' }}") to escape it.)`;
  16175. }
  16176. const error = new TokenError(msg, this._currentTokenType, span);
  16177. this._currentTokenStart = null;
  16178. this._currentTokenType = null;
  16179. return new _ControlFlowError(error);
  16180. }
  16181. handleError(e) {
  16182. if (e instanceof CursorError) {
  16183. e = this._createError(e.msg, this._cursor.getSpan(e.cursor));
  16184. }
  16185. if (e instanceof _ControlFlowError) {
  16186. this.errors.push(e.error);
  16187. }
  16188. else {
  16189. throw e;
  16190. }
  16191. }
  16192. _attemptCharCode(charCode) {
  16193. if (this._cursor.peek() === charCode) {
  16194. this._cursor.advance();
  16195. return true;
  16196. }
  16197. return false;
  16198. }
  16199. _attemptCharCodeCaseInsensitive(charCode) {
  16200. if (compareCharCodeCaseInsensitive(this._cursor.peek(), charCode)) {
  16201. this._cursor.advance();
  16202. return true;
  16203. }
  16204. return false;
  16205. }
  16206. _requireCharCode(charCode) {
  16207. const location = this._cursor.clone();
  16208. if (!this._attemptCharCode(charCode)) {
  16209. throw this._createError(_unexpectedCharacterErrorMsg(this._cursor.peek()), this._cursor.getSpan(location));
  16210. }
  16211. }
  16212. _attemptStr(chars) {
  16213. const len = chars.length;
  16214. if (this._cursor.charsLeft() < len) {
  16215. return false;
  16216. }
  16217. const initialPosition = this._cursor.clone();
  16218. for (let i = 0; i < len; i++) {
  16219. if (!this._attemptCharCode(chars.charCodeAt(i))) {
  16220. // If attempting to parse the string fails, we want to reset the parser
  16221. // to where it was before the attempt
  16222. this._cursor = initialPosition;
  16223. return false;
  16224. }
  16225. }
  16226. return true;
  16227. }
  16228. _attemptStrCaseInsensitive(chars) {
  16229. for (let i = 0; i < chars.length; i++) {
  16230. if (!this._attemptCharCodeCaseInsensitive(chars.charCodeAt(i))) {
  16231. return false;
  16232. }
  16233. }
  16234. return true;
  16235. }
  16236. _requireStr(chars) {
  16237. const location = this._cursor.clone();
  16238. if (!this._attemptStr(chars)) {
  16239. throw this._createError(_unexpectedCharacterErrorMsg(this._cursor.peek()), this._cursor.getSpan(location));
  16240. }
  16241. }
  16242. _attemptCharCodeUntilFn(predicate) {
  16243. while (!predicate(this._cursor.peek())) {
  16244. this._cursor.advance();
  16245. }
  16246. }
  16247. _requireCharCodeUntilFn(predicate, len) {
  16248. const start = this._cursor.clone();
  16249. this._attemptCharCodeUntilFn(predicate);
  16250. if (this._cursor.diff(start) < len) {
  16251. throw this._createError(_unexpectedCharacterErrorMsg(this._cursor.peek()), this._cursor.getSpan(start));
  16252. }
  16253. }
  16254. _attemptUntilChar(char) {
  16255. while (this._cursor.peek() !== char) {
  16256. this._cursor.advance();
  16257. }
  16258. }
  16259. _readChar() {
  16260. // Don't rely upon reading directly from `_input` as the actual char value
  16261. // may have been generated from an escape sequence.
  16262. const char = String.fromCodePoint(this._cursor.peek());
  16263. this._cursor.advance();
  16264. return char;
  16265. }
  16266. _consumeEntity(textTokenType) {
  16267. this._beginToken(9 /* TokenType.ENCODED_ENTITY */);
  16268. const start = this._cursor.clone();
  16269. this._cursor.advance();
  16270. if (this._attemptCharCode($HASH)) {
  16271. const isHex = this._attemptCharCode($x) || this._attemptCharCode($X);
  16272. const codeStart = this._cursor.clone();
  16273. this._attemptCharCodeUntilFn(isDigitEntityEnd);
  16274. if (this._cursor.peek() != $SEMICOLON) {
  16275. // Advance cursor to include the peeked character in the string provided to the error
  16276. // message.
  16277. this._cursor.advance();
  16278. const entityType = isHex ? CharacterReferenceType.HEX : CharacterReferenceType.DEC;
  16279. throw this._createError(_unparsableEntityErrorMsg(entityType, this._cursor.getChars(start)), this._cursor.getSpan());
  16280. }
  16281. const strNum = this._cursor.getChars(codeStart);
  16282. this._cursor.advance();
  16283. try {
  16284. const charCode = parseInt(strNum, isHex ? 16 : 10);
  16285. this._endToken([String.fromCharCode(charCode), this._cursor.getChars(start)]);
  16286. }
  16287. catch {
  16288. throw this._createError(_unknownEntityErrorMsg(this._cursor.getChars(start)), this._cursor.getSpan());
  16289. }
  16290. }
  16291. else {
  16292. const nameStart = this._cursor.clone();
  16293. this._attemptCharCodeUntilFn(isNamedEntityEnd);
  16294. if (this._cursor.peek() != $SEMICOLON) {
  16295. // No semicolon was found so abort the encoded entity token that was in progress, and treat
  16296. // this as a text token
  16297. this._beginToken(textTokenType, start);
  16298. this._cursor = nameStart;
  16299. this._endToken(['&']);
  16300. }
  16301. else {
  16302. const name = this._cursor.getChars(nameStart);
  16303. this._cursor.advance();
  16304. const char = NAMED_ENTITIES[name];
  16305. if (!char) {
  16306. throw this._createError(_unknownEntityErrorMsg(name), this._cursor.getSpan(start));
  16307. }
  16308. this._endToken([char, `&${name};`]);
  16309. }
  16310. }
  16311. }
  16312. _consumeRawText(consumeEntities, endMarkerPredicate) {
  16313. this._beginToken(consumeEntities ? 6 /* TokenType.ESCAPABLE_RAW_TEXT */ : 7 /* TokenType.RAW_TEXT */);
  16314. const parts = [];
  16315. while (true) {
  16316. const tagCloseStart = this._cursor.clone();
  16317. const foundEndMarker = endMarkerPredicate();
  16318. this._cursor = tagCloseStart;
  16319. if (foundEndMarker) {
  16320. break;
  16321. }
  16322. if (consumeEntities && this._cursor.peek() === $AMPERSAND) {
  16323. this._endToken([this._processCarriageReturns(parts.join(''))]);
  16324. parts.length = 0;
  16325. this._consumeEntity(6 /* TokenType.ESCAPABLE_RAW_TEXT */);
  16326. this._beginToken(6 /* TokenType.ESCAPABLE_RAW_TEXT */);
  16327. }
  16328. else {
  16329. parts.push(this._readChar());
  16330. }
  16331. }
  16332. this._endToken([this._processCarriageReturns(parts.join(''))]);
  16333. }
  16334. _consumeComment(start) {
  16335. this._beginToken(10 /* TokenType.COMMENT_START */, start);
  16336. this._requireCharCode($MINUS);
  16337. this._endToken([]);
  16338. this._consumeRawText(false, () => this._attemptStr('-->'));
  16339. this._beginToken(11 /* TokenType.COMMENT_END */);
  16340. this._requireStr('-->');
  16341. this._endToken([]);
  16342. }
  16343. _consumeCdata(start) {
  16344. this._beginToken(12 /* TokenType.CDATA_START */, start);
  16345. this._requireStr('CDATA[');
  16346. this._endToken([]);
  16347. this._consumeRawText(false, () => this._attemptStr(']]>'));
  16348. this._beginToken(13 /* TokenType.CDATA_END */);
  16349. this._requireStr(']]>');
  16350. this._endToken([]);
  16351. }
  16352. _consumeDocType(start) {
  16353. this._beginToken(18 /* TokenType.DOC_TYPE */, start);
  16354. const contentStart = this._cursor.clone();
  16355. this._attemptUntilChar($GT);
  16356. const content = this._cursor.getChars(contentStart);
  16357. this._cursor.advance();
  16358. this._endToken([content]);
  16359. }
  16360. _consumePrefixAndName() {
  16361. const nameOrPrefixStart = this._cursor.clone();
  16362. let prefix = '';
  16363. while (this._cursor.peek() !== $COLON && !isPrefixEnd(this._cursor.peek())) {
  16364. this._cursor.advance();
  16365. }
  16366. let nameStart;
  16367. if (this._cursor.peek() === $COLON) {
  16368. prefix = this._cursor.getChars(nameOrPrefixStart);
  16369. this._cursor.advance();
  16370. nameStart = this._cursor.clone();
  16371. }
  16372. else {
  16373. nameStart = nameOrPrefixStart;
  16374. }
  16375. this._requireCharCodeUntilFn(isNameEnd, prefix === '' ? 0 : 1);
  16376. const name = this._cursor.getChars(nameStart);
  16377. return [prefix, name];
  16378. }
  16379. _consumeTagOpen(start) {
  16380. let tagName;
  16381. let prefix;
  16382. let openTagToken;
  16383. try {
  16384. if (!isAsciiLetter(this._cursor.peek())) {
  16385. throw this._createError(_unexpectedCharacterErrorMsg(this._cursor.peek()), this._cursor.getSpan(start));
  16386. }
  16387. openTagToken = this._consumeTagOpenStart(start);
  16388. prefix = openTagToken.parts[0];
  16389. tagName = openTagToken.parts[1];
  16390. this._attemptCharCodeUntilFn(isNotWhitespace);
  16391. while (this._cursor.peek() !== $SLASH && this._cursor.peek() !== $GT &&
  16392. this._cursor.peek() !== $LT && this._cursor.peek() !== $EOF) {
  16393. this._consumeAttributeName();
  16394. this._attemptCharCodeUntilFn(isNotWhitespace);
  16395. if (this._attemptCharCode($EQ)) {
  16396. this._attemptCharCodeUntilFn(isNotWhitespace);
  16397. this._consumeAttributeValue();
  16398. }
  16399. this._attemptCharCodeUntilFn(isNotWhitespace);
  16400. }
  16401. this._consumeTagOpenEnd();
  16402. }
  16403. catch (e) {
  16404. if (e instanceof _ControlFlowError) {
  16405. if (openTagToken) {
  16406. // We errored before we could close the opening tag, so it is incomplete.
  16407. openTagToken.type = 4 /* TokenType.INCOMPLETE_TAG_OPEN */;
  16408. }
  16409. else {
  16410. // When the start tag is invalid, assume we want a "<" as text.
  16411. // Back to back text tokens are merged at the end.
  16412. this._beginToken(5 /* TokenType.TEXT */, start);
  16413. this._endToken(['<']);
  16414. }
  16415. return;
  16416. }
  16417. throw e;
  16418. }
  16419. const contentTokenType = this._getTagDefinition(tagName).getContentType(prefix);
  16420. if (contentTokenType === TagContentType.RAW_TEXT) {
  16421. this._consumeRawTextWithTagClose(prefix, tagName, false);
  16422. }
  16423. else if (contentTokenType === TagContentType.ESCAPABLE_RAW_TEXT) {
  16424. this._consumeRawTextWithTagClose(prefix, tagName, true);
  16425. }
  16426. }
  16427. _consumeRawTextWithTagClose(prefix, tagName, consumeEntities) {
  16428. this._consumeRawText(consumeEntities, () => {
  16429. if (!this._attemptCharCode($LT))
  16430. return false;
  16431. if (!this._attemptCharCode($SLASH))
  16432. return false;
  16433. this._attemptCharCodeUntilFn(isNotWhitespace);
  16434. if (!this._attemptStrCaseInsensitive(tagName))
  16435. return false;
  16436. this._attemptCharCodeUntilFn(isNotWhitespace);
  16437. return this._attemptCharCode($GT);
  16438. });
  16439. this._beginToken(3 /* TokenType.TAG_CLOSE */);
  16440. this._requireCharCodeUntilFn(code => code === $GT, 3);
  16441. this._cursor.advance(); // Consume the `>`
  16442. this._endToken([prefix, tagName]);
  16443. }
  16444. _consumeTagOpenStart(start) {
  16445. this._beginToken(0 /* TokenType.TAG_OPEN_START */, start);
  16446. const parts = this._consumePrefixAndName();
  16447. return this._endToken(parts);
  16448. }
  16449. _consumeAttributeName() {
  16450. const attrNameStart = this._cursor.peek();
  16451. if (attrNameStart === $SQ || attrNameStart === $DQ) {
  16452. throw this._createError(_unexpectedCharacterErrorMsg(attrNameStart), this._cursor.getSpan());
  16453. }
  16454. this._beginToken(14 /* TokenType.ATTR_NAME */);
  16455. const prefixAndName = this._consumePrefixAndName();
  16456. this._endToken(prefixAndName);
  16457. }
  16458. _consumeAttributeValue() {
  16459. let value;
  16460. if (this._cursor.peek() === $SQ || this._cursor.peek() === $DQ) {
  16461. const quoteChar = this._cursor.peek();
  16462. this._consumeQuote(quoteChar);
  16463. // In an attribute then end of the attribute value and the premature end to an interpolation
  16464. // are both triggered by the `quoteChar`.
  16465. const endPredicate = () => this._cursor.peek() === quoteChar;
  16466. this._consumeWithInterpolation(16 /* TokenType.ATTR_VALUE_TEXT */, 17 /* TokenType.ATTR_VALUE_INTERPOLATION */, endPredicate, endPredicate);
  16467. this._consumeQuote(quoteChar);
  16468. }
  16469. else {
  16470. const endPredicate = () => isNameEnd(this._cursor.peek());
  16471. this._consumeWithInterpolation(16 /* TokenType.ATTR_VALUE_TEXT */, 17 /* TokenType.ATTR_VALUE_INTERPOLATION */, endPredicate, endPredicate);
  16472. }
  16473. }
  16474. _consumeQuote(quoteChar) {
  16475. this._beginToken(15 /* TokenType.ATTR_QUOTE */);
  16476. this._requireCharCode(quoteChar);
  16477. this._endToken([String.fromCodePoint(quoteChar)]);
  16478. }
  16479. _consumeTagOpenEnd() {
  16480. const tokenType = this._attemptCharCode($SLASH) ? 2 /* TokenType.TAG_OPEN_END_VOID */ : 1 /* TokenType.TAG_OPEN_END */;
  16481. this._beginToken(tokenType);
  16482. this._requireCharCode($GT);
  16483. this._endToken([]);
  16484. }
  16485. _consumeTagClose(start) {
  16486. this._beginToken(3 /* TokenType.TAG_CLOSE */, start);
  16487. this._attemptCharCodeUntilFn(isNotWhitespace);
  16488. const prefixAndName = this._consumePrefixAndName();
  16489. this._attemptCharCodeUntilFn(isNotWhitespace);
  16490. this._requireCharCode($GT);
  16491. this._endToken(prefixAndName);
  16492. }
  16493. _consumeExpansionFormStart() {
  16494. this._beginToken(19 /* TokenType.EXPANSION_FORM_START */);
  16495. this._requireCharCode($LBRACE);
  16496. this._endToken([]);
  16497. this._expansionCaseStack.push(19 /* TokenType.EXPANSION_FORM_START */);
  16498. this._beginToken(7 /* TokenType.RAW_TEXT */);
  16499. const condition = this._readUntil($COMMA);
  16500. const normalizedCondition = this._processCarriageReturns(condition);
  16501. if (this._i18nNormalizeLineEndingsInICUs) {
  16502. // We explicitly want to normalize line endings for this text.
  16503. this._endToken([normalizedCondition]);
  16504. }
  16505. else {
  16506. // We are not normalizing line endings.
  16507. const conditionToken = this._endToken([condition]);
  16508. if (normalizedCondition !== condition) {
  16509. this.nonNormalizedIcuExpressions.push(conditionToken);
  16510. }
  16511. }
  16512. this._requireCharCode($COMMA);
  16513. this._attemptCharCodeUntilFn(isNotWhitespace);
  16514. this._beginToken(7 /* TokenType.RAW_TEXT */);
  16515. const type = this._readUntil($COMMA);
  16516. this._endToken([type]);
  16517. this._requireCharCode($COMMA);
  16518. this._attemptCharCodeUntilFn(isNotWhitespace);
  16519. }
  16520. _consumeExpansionCaseStart() {
  16521. this._beginToken(20 /* TokenType.EXPANSION_CASE_VALUE */);
  16522. const value = this._readUntil($LBRACE).trim();
  16523. this._endToken([value]);
  16524. this._attemptCharCodeUntilFn(isNotWhitespace);
  16525. this._beginToken(21 /* TokenType.EXPANSION_CASE_EXP_START */);
  16526. this._requireCharCode($LBRACE);
  16527. this._endToken([]);
  16528. this._attemptCharCodeUntilFn(isNotWhitespace);
  16529. this._expansionCaseStack.push(21 /* TokenType.EXPANSION_CASE_EXP_START */);
  16530. }
  16531. _consumeExpansionCaseEnd() {
  16532. this._beginToken(22 /* TokenType.EXPANSION_CASE_EXP_END */);
  16533. this._requireCharCode($RBRACE);
  16534. this._endToken([]);
  16535. this._attemptCharCodeUntilFn(isNotWhitespace);
  16536. this._expansionCaseStack.pop();
  16537. }
  16538. _consumeExpansionFormEnd() {
  16539. this._beginToken(23 /* TokenType.EXPANSION_FORM_END */);
  16540. this._requireCharCode($RBRACE);
  16541. this._endToken([]);
  16542. this._expansionCaseStack.pop();
  16543. }
  16544. /**
  16545. * Consume a string that may contain interpolation expressions.
  16546. *
  16547. * The first token consumed will be of `tokenType` and then there will be alternating
  16548. * `interpolationTokenType` and `tokenType` tokens until the `endPredicate()` returns true.
  16549. *
  16550. * If an interpolation token ends prematurely it will have no end marker in its `parts` array.
  16551. *
  16552. * @param textTokenType the kind of tokens to interleave around interpolation tokens.
  16553. * @param interpolationTokenType the kind of tokens that contain interpolation.
  16554. * @param endPredicate a function that should return true when we should stop consuming.
  16555. * @param endInterpolation a function that should return true if there is a premature end to an
  16556. * interpolation expression - i.e. before we get to the normal interpolation closing marker.
  16557. */
  16558. _consumeWithInterpolation(textTokenType, interpolationTokenType, endPredicate, endInterpolation) {
  16559. this._beginToken(textTokenType);
  16560. const parts = [];
  16561. while (!endPredicate()) {
  16562. const current = this._cursor.clone();
  16563. if (this._interpolationConfig && this._attemptStr(this._interpolationConfig.start)) {
  16564. this._endToken([this._processCarriageReturns(parts.join(''))], current);
  16565. parts.length = 0;
  16566. this._consumeInterpolation(interpolationTokenType, current, endInterpolation);
  16567. this._beginToken(textTokenType);
  16568. }
  16569. else if (this._cursor.peek() === $AMPERSAND) {
  16570. this._endToken([this._processCarriageReturns(parts.join(''))]);
  16571. parts.length = 0;
  16572. this._consumeEntity(textTokenType);
  16573. this._beginToken(textTokenType);
  16574. }
  16575. else {
  16576. parts.push(this._readChar());
  16577. }
  16578. }
  16579. // It is possible that an interpolation was started but not ended inside this text token.
  16580. // Make sure that we reset the state of the lexer correctly.
  16581. this._inInterpolation = false;
  16582. this._endToken([this._processCarriageReturns(parts.join(''))]);
  16583. }
  16584. /**
  16585. * Consume a block of text that has been interpreted as an Angular interpolation.
  16586. *
  16587. * @param interpolationTokenType the type of the interpolation token to generate.
  16588. * @param interpolationStart a cursor that points to the start of this interpolation.
  16589. * @param prematureEndPredicate a function that should return true if the next characters indicate
  16590. * an end to the interpolation before its normal closing marker.
  16591. */
  16592. _consumeInterpolation(interpolationTokenType, interpolationStart, prematureEndPredicate) {
  16593. const parts = [];
  16594. this._beginToken(interpolationTokenType, interpolationStart);
  16595. parts.push(this._interpolationConfig.start);
  16596. // Find the end of the interpolation, ignoring content inside quotes.
  16597. const expressionStart = this._cursor.clone();
  16598. let inQuote = null;
  16599. let inComment = false;
  16600. while (this._cursor.peek() !== $EOF &&
  16601. (prematureEndPredicate === null || !prematureEndPredicate())) {
  16602. const current = this._cursor.clone();
  16603. if (this._isTagStart()) {
  16604. // We are starting what looks like an HTML element in the middle of this interpolation.
  16605. // Reset the cursor to before the `<` character and end the interpolation token.
  16606. // (This is actually wrong but here for backward compatibility).
  16607. this._cursor = current;
  16608. parts.push(this._getProcessedChars(expressionStart, current));
  16609. this._endToken(parts);
  16610. return;
  16611. }
  16612. if (inQuote === null) {
  16613. if (this._attemptStr(this._interpolationConfig.end)) {
  16614. // We are not in a string, and we hit the end interpolation marker
  16615. parts.push(this._getProcessedChars(expressionStart, current));
  16616. parts.push(this._interpolationConfig.end);
  16617. this._endToken(parts);
  16618. return;
  16619. }
  16620. else if (this._attemptStr('//')) {
  16621. // Once we are in a comment we ignore any quotes
  16622. inComment = true;
  16623. }
  16624. }
  16625. const char = this._cursor.peek();
  16626. this._cursor.advance();
  16627. if (char === $BACKSLASH) {
  16628. // Skip the next character because it was escaped.
  16629. this._cursor.advance();
  16630. }
  16631. else if (char === inQuote) {
  16632. // Exiting the current quoted string
  16633. inQuote = null;
  16634. }
  16635. else if (!inComment && inQuote === null && isQuote(char)) {
  16636. // Entering a new quoted string
  16637. inQuote = char;
  16638. }
  16639. }
  16640. // We hit EOF without finding a closing interpolation marker
  16641. parts.push(this._getProcessedChars(expressionStart, this._cursor));
  16642. this._endToken(parts);
  16643. }
  16644. _getProcessedChars(start, end) {
  16645. return this._processCarriageReturns(end.getChars(start));
  16646. }
  16647. _isTextEnd() {
  16648. if (this._isTagStart() || this._cursor.peek() === $EOF) {
  16649. return true;
  16650. }
  16651. if (this._tokenizeIcu && !this._inInterpolation) {
  16652. if (this.isExpansionFormStart()) {
  16653. // start of an expansion form
  16654. return true;
  16655. }
  16656. if (this._cursor.peek() === $RBRACE && this._isInExpansionCase()) {
  16657. // end of and expansion case
  16658. return true;
  16659. }
  16660. }
  16661. return false;
  16662. }
  16663. /**
  16664. * Returns true if the current cursor is pointing to the start of a tag
  16665. * (opening/closing/comments/cdata/etc).
  16666. */
  16667. _isTagStart() {
  16668. if (this._cursor.peek() === $LT) {
  16669. // We assume that `<` followed by whitespace is not the start of an HTML element.
  16670. const tmp = this._cursor.clone();
  16671. tmp.advance();
  16672. // If the next character is alphabetic, ! nor / then it is a tag start
  16673. const code = tmp.peek();
  16674. if (($a <= code && code <= $z) || ($A <= code && code <= $Z) ||
  16675. code === $SLASH || code === $BANG) {
  16676. return true;
  16677. }
  16678. }
  16679. return false;
  16680. }
  16681. _readUntil(char) {
  16682. const start = this._cursor.clone();
  16683. this._attemptUntilChar(char);
  16684. return this._cursor.getChars(start);
  16685. }
  16686. _isInExpansionCase() {
  16687. return this._expansionCaseStack.length > 0 &&
  16688. this._expansionCaseStack[this._expansionCaseStack.length - 1] ===
  16689. 21 /* TokenType.EXPANSION_CASE_EXP_START */;
  16690. }
  16691. _isInExpansionForm() {
  16692. return this._expansionCaseStack.length > 0 &&
  16693. this._expansionCaseStack[this._expansionCaseStack.length - 1] ===
  16694. 19 /* TokenType.EXPANSION_FORM_START */;
  16695. }
  16696. isExpansionFormStart() {
  16697. if (this._cursor.peek() !== $LBRACE) {
  16698. return false;
  16699. }
  16700. if (this._interpolationConfig) {
  16701. const start = this._cursor.clone();
  16702. const isInterpolation = this._attemptStr(this._interpolationConfig.start);
  16703. this._cursor = start;
  16704. return !isInterpolation;
  16705. }
  16706. return true;
  16707. }
  16708. }
  16709. function isNotWhitespace(code) {
  16710. return !isWhitespace(code) || code === $EOF;
  16711. }
  16712. function isNameEnd(code) {
  16713. return isWhitespace(code) || code === $GT || code === $LT ||
  16714. code === $SLASH || code === $SQ || code === $DQ || code === $EQ ||
  16715. code === $EOF;
  16716. }
  16717. function isPrefixEnd(code) {
  16718. return (code < $a || $z < code) && (code < $A || $Z < code) &&
  16719. (code < $0 || code > $9);
  16720. }
  16721. function isDigitEntityEnd(code) {
  16722. return code === $SEMICOLON || code === $EOF || !isAsciiHexDigit(code);
  16723. }
  16724. function isNamedEntityEnd(code) {
  16725. return code === $SEMICOLON || code === $EOF || !isAsciiLetter(code);
  16726. }
  16727. function isExpansionCaseStart(peek) {
  16728. return peek !== $RBRACE;
  16729. }
  16730. function compareCharCodeCaseInsensitive(code1, code2) {
  16731. return toUpperCaseCharCode(code1) === toUpperCaseCharCode(code2);
  16732. }
  16733. function toUpperCaseCharCode(code) {
  16734. return code >= $a && code <= $z ? code - $a + $A : code;
  16735. }
  16736. function mergeTextTokens(srcTokens) {
  16737. const dstTokens = [];
  16738. let lastDstToken = undefined;
  16739. for (let i = 0; i < srcTokens.length; i++) {
  16740. const token = srcTokens[i];
  16741. if ((lastDstToken && lastDstToken.type === 5 /* TokenType.TEXT */ && token.type === 5 /* TokenType.TEXT */) ||
  16742. (lastDstToken && lastDstToken.type === 16 /* TokenType.ATTR_VALUE_TEXT */ &&
  16743. token.type === 16 /* TokenType.ATTR_VALUE_TEXT */)) {
  16744. lastDstToken.parts[0] += token.parts[0];
  16745. lastDstToken.sourceSpan.end = token.sourceSpan.end;
  16746. }
  16747. else {
  16748. lastDstToken = token;
  16749. dstTokens.push(lastDstToken);
  16750. }
  16751. }
  16752. return dstTokens;
  16753. }
  16754. class PlainCharacterCursor {
  16755. constructor(fileOrCursor, range) {
  16756. if (fileOrCursor instanceof PlainCharacterCursor) {
  16757. this.file = fileOrCursor.file;
  16758. this.input = fileOrCursor.input;
  16759. this.end = fileOrCursor.end;
  16760. const state = fileOrCursor.state;
  16761. // Note: avoid using `{...fileOrCursor.state}` here as that has a severe performance penalty.
  16762. // In ES5 bundles the object spread operator is translated into the `__assign` helper, which
  16763. // is not optimized by VMs as efficiently as a raw object literal. Since this constructor is
  16764. // called in tight loops, this difference matters.
  16765. this.state = {
  16766. peek: state.peek,
  16767. offset: state.offset,
  16768. line: state.line,
  16769. column: state.column,
  16770. };
  16771. }
  16772. else {
  16773. if (!range) {
  16774. throw new Error('Programming error: the range argument must be provided with a file argument.');
  16775. }
  16776. this.file = fileOrCursor;
  16777. this.input = fileOrCursor.content;
  16778. this.end = range.endPos;
  16779. this.state = {
  16780. peek: -1,
  16781. offset: range.startPos,
  16782. line: range.startLine,
  16783. column: range.startCol,
  16784. };
  16785. }
  16786. }
  16787. clone() {
  16788. return new PlainCharacterCursor(this);
  16789. }
  16790. peek() {
  16791. return this.state.peek;
  16792. }
  16793. charsLeft() {
  16794. return this.end - this.state.offset;
  16795. }
  16796. diff(other) {
  16797. return this.state.offset - other.state.offset;
  16798. }
  16799. advance() {
  16800. this.advanceState(this.state);
  16801. }
  16802. init() {
  16803. this.updatePeek(this.state);
  16804. }
  16805. getSpan(start, leadingTriviaCodePoints) {
  16806. start = start || this;
  16807. let fullStart = start;
  16808. if (leadingTriviaCodePoints) {
  16809. while (this.diff(start) > 0 && leadingTriviaCodePoints.indexOf(start.peek()) !== -1) {
  16810. if (fullStart === start) {
  16811. start = start.clone();
  16812. }
  16813. start.advance();
  16814. }
  16815. }
  16816. const startLocation = this.locationFromCursor(start);
  16817. const endLocation = this.locationFromCursor(this);
  16818. const fullStartLocation = fullStart !== start ? this.locationFromCursor(fullStart) : startLocation;
  16819. return new ParseSourceSpan(startLocation, endLocation, fullStartLocation);
  16820. }
  16821. getChars(start) {
  16822. return this.input.substring(start.state.offset, this.state.offset);
  16823. }
  16824. charAt(pos) {
  16825. return this.input.charCodeAt(pos);
  16826. }
  16827. advanceState(state) {
  16828. if (state.offset >= this.end) {
  16829. this.state = state;
  16830. throw new CursorError('Unexpected character "EOF"', this);
  16831. }
  16832. const currentChar = this.charAt(state.offset);
  16833. if (currentChar === $LF) {
  16834. state.line++;
  16835. state.column = 0;
  16836. }
  16837. else if (!isNewLine(currentChar)) {
  16838. state.column++;
  16839. }
  16840. state.offset++;
  16841. this.updatePeek(state);
  16842. }
  16843. updatePeek(state) {
  16844. state.peek = state.offset >= this.end ? $EOF : this.charAt(state.offset);
  16845. }
  16846. locationFromCursor(cursor) {
  16847. return new ParseLocation(cursor.file, cursor.state.offset, cursor.state.line, cursor.state.column);
  16848. }
  16849. }
  16850. class EscapedCharacterCursor extends PlainCharacterCursor {
  16851. constructor(fileOrCursor, range) {
  16852. if (fileOrCursor instanceof EscapedCharacterCursor) {
  16853. super(fileOrCursor);
  16854. this.internalState = { ...fileOrCursor.internalState };
  16855. }
  16856. else {
  16857. super(fileOrCursor, range);
  16858. this.internalState = this.state;
  16859. }
  16860. }
  16861. advance() {
  16862. this.state = this.internalState;
  16863. super.advance();
  16864. this.processEscapeSequence();
  16865. }
  16866. init() {
  16867. super.init();
  16868. this.processEscapeSequence();
  16869. }
  16870. clone() {
  16871. return new EscapedCharacterCursor(this);
  16872. }
  16873. getChars(start) {
  16874. const cursor = start.clone();
  16875. let chars = '';
  16876. while (cursor.internalState.offset < this.internalState.offset) {
  16877. chars += String.fromCodePoint(cursor.peek());
  16878. cursor.advance();
  16879. }
  16880. return chars;
  16881. }
  16882. /**
  16883. * Process the escape sequence that starts at the current position in the text.
  16884. *
  16885. * This method is called to ensure that `peek` has the unescaped value of escape sequences.
  16886. */
  16887. processEscapeSequence() {
  16888. const peek = () => this.internalState.peek;
  16889. if (peek() === $BACKSLASH) {
  16890. // We have hit an escape sequence so we need the internal state to become independent
  16891. // of the external state.
  16892. this.internalState = { ...this.state };
  16893. // Move past the backslash
  16894. this.advanceState(this.internalState);
  16895. // First check for standard control char sequences
  16896. if (peek() === $n) {
  16897. this.state.peek = $LF;
  16898. }
  16899. else if (peek() === $r) {
  16900. this.state.peek = $CR;
  16901. }
  16902. else if (peek() === $v) {
  16903. this.state.peek = $VTAB;
  16904. }
  16905. else if (peek() === $t) {
  16906. this.state.peek = $TAB;
  16907. }
  16908. else if (peek() === $b) {
  16909. this.state.peek = $BSPACE;
  16910. }
  16911. else if (peek() === $f) {
  16912. this.state.peek = $FF;
  16913. }
  16914. // Now consider more complex sequences
  16915. else if (peek() === $u) {
  16916. // Unicode code-point sequence
  16917. this.advanceState(this.internalState); // advance past the `u` char
  16918. if (peek() === $LBRACE) {
  16919. // Variable length Unicode, e.g. `\x{123}`
  16920. this.advanceState(this.internalState); // advance past the `{` char
  16921. // Advance past the variable number of hex digits until we hit a `}` char
  16922. const digitStart = this.clone();
  16923. let length = 0;
  16924. while (peek() !== $RBRACE) {
  16925. this.advanceState(this.internalState);
  16926. length++;
  16927. }
  16928. this.state.peek = this.decodeHexDigits(digitStart, length);
  16929. }
  16930. else {
  16931. // Fixed length Unicode, e.g. `\u1234`
  16932. const digitStart = this.clone();
  16933. this.advanceState(this.internalState);
  16934. this.advanceState(this.internalState);
  16935. this.advanceState(this.internalState);
  16936. this.state.peek = this.decodeHexDigits(digitStart, 4);
  16937. }
  16938. }
  16939. else if (peek() === $x) {
  16940. // Hex char code, e.g. `\x2F`
  16941. this.advanceState(this.internalState); // advance past the `x` char
  16942. const digitStart = this.clone();
  16943. this.advanceState(this.internalState);
  16944. this.state.peek = this.decodeHexDigits(digitStart, 2);
  16945. }
  16946. else if (isOctalDigit(peek())) {
  16947. // Octal char code, e.g. `\012`,
  16948. let octal = '';
  16949. let length = 0;
  16950. let previous = this.clone();
  16951. while (isOctalDigit(peek()) && length < 3) {
  16952. previous = this.clone();
  16953. octal += String.fromCodePoint(peek());
  16954. this.advanceState(this.internalState);
  16955. length++;
  16956. }
  16957. this.state.peek = parseInt(octal, 8);
  16958. // Backup one char
  16959. this.internalState = previous.internalState;
  16960. }
  16961. else if (isNewLine(this.internalState.peek)) {
  16962. // Line continuation `\` followed by a new line
  16963. this.advanceState(this.internalState); // advance over the newline
  16964. this.state = this.internalState;
  16965. }
  16966. else {
  16967. // If none of the `if` blocks were executed then we just have an escaped normal character.
  16968. // In that case we just, effectively, skip the backslash from the character.
  16969. this.state.peek = this.internalState.peek;
  16970. }
  16971. }
  16972. }
  16973. decodeHexDigits(start, length) {
  16974. const hex = this.input.slice(start.internalState.offset, start.internalState.offset + length);
  16975. const charCode = parseInt(hex, 16);
  16976. if (!isNaN(charCode)) {
  16977. return charCode;
  16978. }
  16979. else {
  16980. start.state = start.internalState;
  16981. throw new CursorError('Invalid hexadecimal escape sequence', start);
  16982. }
  16983. }
  16984. }
  16985. class CursorError {
  16986. constructor(msg, cursor) {
  16987. this.msg = msg;
  16988. this.cursor = cursor;
  16989. }
  16990. }
  16991. class TreeError extends ParseError {
  16992. static create(elementName, span, msg) {
  16993. return new TreeError(elementName, span, msg);
  16994. }
  16995. constructor(elementName, span, msg) {
  16996. super(span, msg);
  16997. this.elementName = elementName;
  16998. }
  16999. }
  17000. class ParseTreeResult {
  17001. constructor(rootNodes, errors) {
  17002. this.rootNodes = rootNodes;
  17003. this.errors = errors;
  17004. }
  17005. }
  17006. class Parser {
  17007. constructor(getTagDefinition) {
  17008. this.getTagDefinition = getTagDefinition;
  17009. }
  17010. parse(source, url, options) {
  17011. const tokenizeResult = tokenize(source, url, this.getTagDefinition, options);
  17012. const parser = new _TreeBuilder(tokenizeResult.tokens, this.getTagDefinition);
  17013. parser.build();
  17014. return new ParseTreeResult(parser.rootNodes, tokenizeResult.errors.concat(parser.errors));
  17015. }
  17016. }
  17017. class _TreeBuilder {
  17018. constructor(tokens, getTagDefinition) {
  17019. this.tokens = tokens;
  17020. this.getTagDefinition = getTagDefinition;
  17021. this._index = -1;
  17022. this._elementStack = [];
  17023. this.rootNodes = [];
  17024. this.errors = [];
  17025. this._advance();
  17026. }
  17027. build() {
  17028. while (this._peek.type !== 24 /* TokenType.EOF */) {
  17029. if (this._peek.type === 0 /* TokenType.TAG_OPEN_START */ ||
  17030. this._peek.type === 4 /* TokenType.INCOMPLETE_TAG_OPEN */) {
  17031. this._consumeStartTag(this._advance());
  17032. }
  17033. else if (this._peek.type === 3 /* TokenType.TAG_CLOSE */) {
  17034. this._consumeEndTag(this._advance());
  17035. }
  17036. else if (this._peek.type === 12 /* TokenType.CDATA_START */) {
  17037. this._closeVoidElement();
  17038. this._consumeCdata(this._advance());
  17039. }
  17040. else if (this._peek.type === 10 /* TokenType.COMMENT_START */) {
  17041. this._closeVoidElement();
  17042. this._consumeComment(this._advance());
  17043. }
  17044. else if (this._peek.type === 5 /* TokenType.TEXT */ || this._peek.type === 7 /* TokenType.RAW_TEXT */ ||
  17045. this._peek.type === 6 /* TokenType.ESCAPABLE_RAW_TEXT */) {
  17046. this._closeVoidElement();
  17047. this._consumeText(this._advance());
  17048. }
  17049. else if (this._peek.type === 19 /* TokenType.EXPANSION_FORM_START */) {
  17050. this._consumeExpansion(this._advance());
  17051. }
  17052. else {
  17053. // Skip all other tokens...
  17054. this._advance();
  17055. }
  17056. }
  17057. }
  17058. _advance() {
  17059. const prev = this._peek;
  17060. if (this._index < this.tokens.length - 1) {
  17061. // Note: there is always an EOF token at the end
  17062. this._index++;
  17063. }
  17064. this._peek = this.tokens[this._index];
  17065. return prev;
  17066. }
  17067. _advanceIf(type) {
  17068. if (this._peek.type === type) {
  17069. return this._advance();
  17070. }
  17071. return null;
  17072. }
  17073. _consumeCdata(_startToken) {
  17074. this._consumeText(this._advance());
  17075. this._advanceIf(13 /* TokenType.CDATA_END */);
  17076. }
  17077. _consumeComment(token) {
  17078. const text = this._advanceIf(7 /* TokenType.RAW_TEXT */);
  17079. this._advanceIf(11 /* TokenType.COMMENT_END */);
  17080. const value = text != null ? text.parts[0].trim() : null;
  17081. this._addToParent(new Comment(value, token.sourceSpan));
  17082. }
  17083. _consumeExpansion(token) {
  17084. const switchValue = this._advance();
  17085. const type = this._advance();
  17086. const cases = [];
  17087. // read =
  17088. while (this._peek.type === 20 /* TokenType.EXPANSION_CASE_VALUE */) {
  17089. const expCase = this._parseExpansionCase();
  17090. if (!expCase)
  17091. return; // error
  17092. cases.push(expCase);
  17093. }
  17094. // read the final }
  17095. if (this._peek.type !== 23 /* TokenType.EXPANSION_FORM_END */) {
  17096. this.errors.push(TreeError.create(null, this._peek.sourceSpan, `Invalid ICU message. Missing '}'.`));
  17097. return;
  17098. }
  17099. const sourceSpan = new ParseSourceSpan(token.sourceSpan.start, this._peek.sourceSpan.end, token.sourceSpan.fullStart);
  17100. this._addToParent(new Expansion(switchValue.parts[0], type.parts[0], cases, sourceSpan, switchValue.sourceSpan));
  17101. this._advance();
  17102. }
  17103. _parseExpansionCase() {
  17104. const value = this._advance();
  17105. // read {
  17106. if (this._peek.type !== 21 /* TokenType.EXPANSION_CASE_EXP_START */) {
  17107. this.errors.push(TreeError.create(null, this._peek.sourceSpan, `Invalid ICU message. Missing '{'.`));
  17108. return null;
  17109. }
  17110. // read until }
  17111. const start = this._advance();
  17112. const exp = this._collectExpansionExpTokens(start);
  17113. if (!exp)
  17114. return null;
  17115. const end = this._advance();
  17116. exp.push({ type: 24 /* TokenType.EOF */, parts: [], sourceSpan: end.sourceSpan });
  17117. // parse everything in between { and }
  17118. const expansionCaseParser = new _TreeBuilder(exp, this.getTagDefinition);
  17119. expansionCaseParser.build();
  17120. if (expansionCaseParser.errors.length > 0) {
  17121. this.errors = this.errors.concat(expansionCaseParser.errors);
  17122. return null;
  17123. }
  17124. const sourceSpan = new ParseSourceSpan(value.sourceSpan.start, end.sourceSpan.end, value.sourceSpan.fullStart);
  17125. const expSourceSpan = new ParseSourceSpan(start.sourceSpan.start, end.sourceSpan.end, start.sourceSpan.fullStart);
  17126. return new ExpansionCase(value.parts[0], expansionCaseParser.rootNodes, sourceSpan, value.sourceSpan, expSourceSpan);
  17127. }
  17128. _collectExpansionExpTokens(start) {
  17129. const exp = [];
  17130. const expansionFormStack = [21 /* TokenType.EXPANSION_CASE_EXP_START */];
  17131. while (true) {
  17132. if (this._peek.type === 19 /* TokenType.EXPANSION_FORM_START */ ||
  17133. this._peek.type === 21 /* TokenType.EXPANSION_CASE_EXP_START */) {
  17134. expansionFormStack.push(this._peek.type);
  17135. }
  17136. if (this._peek.type === 22 /* TokenType.EXPANSION_CASE_EXP_END */) {
  17137. if (lastOnStack(expansionFormStack, 21 /* TokenType.EXPANSION_CASE_EXP_START */)) {
  17138. expansionFormStack.pop();
  17139. if (expansionFormStack.length === 0)
  17140. return exp;
  17141. }
  17142. else {
  17143. this.errors.push(TreeError.create(null, start.sourceSpan, `Invalid ICU message. Missing '}'.`));
  17144. return null;
  17145. }
  17146. }
  17147. if (this._peek.type === 23 /* TokenType.EXPANSION_FORM_END */) {
  17148. if (lastOnStack(expansionFormStack, 19 /* TokenType.EXPANSION_FORM_START */)) {
  17149. expansionFormStack.pop();
  17150. }
  17151. else {
  17152. this.errors.push(TreeError.create(null, start.sourceSpan, `Invalid ICU message. Missing '}'.`));
  17153. return null;
  17154. }
  17155. }
  17156. if (this._peek.type === 24 /* TokenType.EOF */) {
  17157. this.errors.push(TreeError.create(null, start.sourceSpan, `Invalid ICU message. Missing '}'.`));
  17158. return null;
  17159. }
  17160. exp.push(this._advance());
  17161. }
  17162. }
  17163. _consumeText(token) {
  17164. const tokens = [token];
  17165. const startSpan = token.sourceSpan;
  17166. let text = token.parts[0];
  17167. if (text.length > 0 && text[0] === '\n') {
  17168. const parent = this._getParentElement();
  17169. if (parent != null && parent.children.length === 0 &&
  17170. this.getTagDefinition(parent.name).ignoreFirstLf) {
  17171. text = text.substring(1);
  17172. tokens[0] = { type: token.type, sourceSpan: token.sourceSpan, parts: [text] };
  17173. }
  17174. }
  17175. while (this._peek.type === 8 /* TokenType.INTERPOLATION */ || this._peek.type === 5 /* TokenType.TEXT */ ||
  17176. this._peek.type === 9 /* TokenType.ENCODED_ENTITY */) {
  17177. token = this._advance();
  17178. tokens.push(token);
  17179. if (token.type === 8 /* TokenType.INTERPOLATION */) {
  17180. // For backward compatibility we decode HTML entities that appear in interpolation
  17181. // expressions. This is arguably a bug, but it could be a considerable breaking change to
  17182. // fix it. It should be addressed in a larger project to refactor the entire parser/lexer
  17183. // chain after View Engine has been removed.
  17184. text += token.parts.join('').replace(/&([^;]+);/g, decodeEntity);
  17185. }
  17186. else if (token.type === 9 /* TokenType.ENCODED_ENTITY */) {
  17187. text += token.parts[0];
  17188. }
  17189. else {
  17190. text += token.parts.join('');
  17191. }
  17192. }
  17193. if (text.length > 0) {
  17194. const endSpan = token.sourceSpan;
  17195. this._addToParent(new Text(text, new ParseSourceSpan(startSpan.start, endSpan.end, startSpan.fullStart, startSpan.details), tokens));
  17196. }
  17197. }
  17198. _closeVoidElement() {
  17199. const el = this._getParentElement();
  17200. if (el && this.getTagDefinition(el.name).isVoid) {
  17201. this._elementStack.pop();
  17202. }
  17203. }
  17204. _consumeStartTag(startTagToken) {
  17205. const [prefix, name] = startTagToken.parts;
  17206. const attrs = [];
  17207. while (this._peek.type === 14 /* TokenType.ATTR_NAME */) {
  17208. attrs.push(this._consumeAttr(this._advance()));
  17209. }
  17210. const fullName = this._getElementFullName(prefix, name, this._getParentElement());
  17211. let selfClosing = false;
  17212. // Note: There could have been a tokenizer error
  17213. // so that we don't get a token for the end tag...
  17214. if (this._peek.type === 2 /* TokenType.TAG_OPEN_END_VOID */) {
  17215. this._advance();
  17216. selfClosing = true;
  17217. const tagDef = this.getTagDefinition(fullName);
  17218. if (!(tagDef.canSelfClose || getNsPrefix(fullName) !== null || tagDef.isVoid)) {
  17219. this.errors.push(TreeError.create(fullName, startTagToken.sourceSpan, `Only void, custom and foreign elements can be self closed "${startTagToken.parts[1]}"`));
  17220. }
  17221. }
  17222. else if (this._peek.type === 1 /* TokenType.TAG_OPEN_END */) {
  17223. this._advance();
  17224. selfClosing = false;
  17225. }
  17226. const end = this._peek.sourceSpan.fullStart;
  17227. const span = new ParseSourceSpan(startTagToken.sourceSpan.start, end, startTagToken.sourceSpan.fullStart);
  17228. // Create a separate `startSpan` because `span` will be modified when there is an `end` span.
  17229. const startSpan = new ParseSourceSpan(startTagToken.sourceSpan.start, end, startTagToken.sourceSpan.fullStart);
  17230. const el = new Element(fullName, attrs, [], span, startSpan, undefined);
  17231. this._pushElement(el);
  17232. if (selfClosing) {
  17233. // Elements that are self-closed have their `endSourceSpan` set to the full span, as the
  17234. // element start tag also represents the end tag.
  17235. this._popElement(fullName, span);
  17236. }
  17237. else if (startTagToken.type === 4 /* TokenType.INCOMPLETE_TAG_OPEN */) {
  17238. // We already know the opening tag is not complete, so it is unlikely it has a corresponding
  17239. // close tag. Let's optimistically parse it as a full element and emit an error.
  17240. this._popElement(fullName, null);
  17241. this.errors.push(TreeError.create(fullName, span, `Opening tag "${fullName}" not terminated.`));
  17242. }
  17243. }
  17244. _pushElement(el) {
  17245. const parentEl = this._getParentElement();
  17246. if (parentEl && this.getTagDefinition(parentEl.name).isClosedByChild(el.name)) {
  17247. this._elementStack.pop();
  17248. }
  17249. this._addToParent(el);
  17250. this._elementStack.push(el);
  17251. }
  17252. _consumeEndTag(endTagToken) {
  17253. const fullName = this._getElementFullName(endTagToken.parts[0], endTagToken.parts[1], this._getParentElement());
  17254. if (this.getTagDefinition(fullName).isVoid) {
  17255. this.errors.push(TreeError.create(fullName, endTagToken.sourceSpan, `Void elements do not have end tags "${endTagToken.parts[1]}"`));
  17256. }
  17257. else if (!this._popElement(fullName, endTagToken.sourceSpan)) {
  17258. 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`;
  17259. this.errors.push(TreeError.create(fullName, endTagToken.sourceSpan, errMsg));
  17260. }
  17261. }
  17262. /**
  17263. * Closes the nearest element with the tag name `fullName` in the parse tree.
  17264. * `endSourceSpan` is the span of the closing tag, or null if the element does
  17265. * not have a closing tag (for example, this happens when an incomplete
  17266. * opening tag is recovered).
  17267. */
  17268. _popElement(fullName, endSourceSpan) {
  17269. let unexpectedCloseTagDetected = false;
  17270. for (let stackIndex = this._elementStack.length - 1; stackIndex >= 0; stackIndex--) {
  17271. const el = this._elementStack[stackIndex];
  17272. if (el.name === fullName) {
  17273. // Record the parse span with the element that is being closed. Any elements that are
  17274. // removed from the element stack at this point are closed implicitly, so they won't get
  17275. // an end source span (as there is no explicit closing element).
  17276. el.endSourceSpan = endSourceSpan;
  17277. el.sourceSpan.end = endSourceSpan !== null ? endSourceSpan.end : el.sourceSpan.end;
  17278. this._elementStack.splice(stackIndex, this._elementStack.length - stackIndex);
  17279. return !unexpectedCloseTagDetected;
  17280. }
  17281. if (!this.getTagDefinition(el.name).closedByParent) {
  17282. // Note that we encountered an unexpected close tag but continue processing the element
  17283. // stack so we can assign an `endSourceSpan` if there is a corresponding start tag for this
  17284. // end tag in the stack.
  17285. unexpectedCloseTagDetected = true;
  17286. }
  17287. }
  17288. return false;
  17289. }
  17290. _consumeAttr(attrName) {
  17291. const fullName = mergeNsAndName(attrName.parts[0], attrName.parts[1]);
  17292. let attrEnd = attrName.sourceSpan.end;
  17293. // Consume any quote
  17294. if (this._peek.type === 15 /* TokenType.ATTR_QUOTE */) {
  17295. this._advance();
  17296. }
  17297. // Consume the attribute value
  17298. let value = '';
  17299. const valueTokens = [];
  17300. let valueStartSpan = undefined;
  17301. let valueEnd = undefined;
  17302. // NOTE: We need to use a new variable `nextTokenType` here to hide the actual type of
  17303. // `_peek.type` from TS. Otherwise TS will narrow the type of `_peek.type` preventing it from
  17304. // being able to consider `ATTR_VALUE_INTERPOLATION` as an option. This is because TS is not
  17305. // able to see that `_advance()` will actually mutate `_peek`.
  17306. const nextTokenType = this._peek.type;
  17307. if (nextTokenType === 16 /* TokenType.ATTR_VALUE_TEXT */) {
  17308. valueStartSpan = this._peek.sourceSpan;
  17309. valueEnd = this._peek.sourceSpan.end;
  17310. while (this._peek.type === 16 /* TokenType.ATTR_VALUE_TEXT */ ||
  17311. this._peek.type === 17 /* TokenType.ATTR_VALUE_INTERPOLATION */ ||
  17312. this._peek.type === 9 /* TokenType.ENCODED_ENTITY */) {
  17313. const valueToken = this._advance();
  17314. valueTokens.push(valueToken);
  17315. if (valueToken.type === 17 /* TokenType.ATTR_VALUE_INTERPOLATION */) {
  17316. // For backward compatibility we decode HTML entities that appear in interpolation
  17317. // expressions. This is arguably a bug, but it could be a considerable breaking change to
  17318. // fix it. It should be addressed in a larger project to refactor the entire parser/lexer
  17319. // chain after View Engine has been removed.
  17320. value += valueToken.parts.join('').replace(/&([^;]+);/g, decodeEntity);
  17321. }
  17322. else if (valueToken.type === 9 /* TokenType.ENCODED_ENTITY */) {
  17323. value += valueToken.parts[0];
  17324. }
  17325. else {
  17326. value += valueToken.parts.join('');
  17327. }
  17328. valueEnd = attrEnd = valueToken.sourceSpan.end;
  17329. }
  17330. }
  17331. // Consume any quote
  17332. if (this._peek.type === 15 /* TokenType.ATTR_QUOTE */) {
  17333. const quoteToken = this._advance();
  17334. attrEnd = quoteToken.sourceSpan.end;
  17335. }
  17336. const valueSpan = valueStartSpan && valueEnd &&
  17337. new ParseSourceSpan(valueStartSpan.start, valueEnd, valueStartSpan.fullStart);
  17338. return new Attribute(fullName, value, new ParseSourceSpan(attrName.sourceSpan.start, attrEnd, attrName.sourceSpan.fullStart), attrName.sourceSpan, valueSpan, valueTokens.length > 0 ? valueTokens : undefined, undefined);
  17339. }
  17340. _getParentElement() {
  17341. return this._elementStack.length > 0 ? this._elementStack[this._elementStack.length - 1] : null;
  17342. }
  17343. _addToParent(node) {
  17344. const parent = this._getParentElement();
  17345. if (parent != null) {
  17346. parent.children.push(node);
  17347. }
  17348. else {
  17349. this.rootNodes.push(node);
  17350. }
  17351. }
  17352. _getElementFullName(prefix, localName, parentElement) {
  17353. if (prefix === '') {
  17354. prefix = this.getTagDefinition(localName).implicitNamespacePrefix || '';
  17355. if (prefix === '' && parentElement != null) {
  17356. const parentTagName = splitNsName(parentElement.name)[1];
  17357. const parentTagDefinition = this.getTagDefinition(parentTagName);
  17358. if (!parentTagDefinition.preventNamespaceInheritance) {
  17359. prefix = getNsPrefix(parentElement.name);
  17360. }
  17361. }
  17362. }
  17363. return mergeNsAndName(prefix, localName);
  17364. }
  17365. }
  17366. function lastOnStack(stack, element) {
  17367. return stack.length > 0 && stack[stack.length - 1] === element;
  17368. }
  17369. /**
  17370. * Decode the `entity` string, which we believe is the contents of an HTML entity.
  17371. *
  17372. * If the string is not actually a valid/known entity then just return the original `match` string.
  17373. */
  17374. function decodeEntity(match, entity) {
  17375. if (NAMED_ENTITIES[entity] !== undefined) {
  17376. return NAMED_ENTITIES[entity] || match;
  17377. }
  17378. if (/^#x[a-f0-9]+$/i.test(entity)) {
  17379. return String.fromCodePoint(parseInt(entity.slice(2), 16));
  17380. }
  17381. if (/^#\d+$/.test(entity)) {
  17382. return String.fromCodePoint(parseInt(entity.slice(1), 10));
  17383. }
  17384. return match;
  17385. }
  17386. class HtmlParser extends Parser {
  17387. constructor() {
  17388. super(getHtmlTagDefinition);
  17389. }
  17390. parse(source, url, options) {
  17391. return super.parse(source, url, options);
  17392. }
  17393. }
  17394. const PRESERVE_WS_ATTR_NAME = 'ngPreserveWhitespaces';
  17395. const SKIP_WS_TRIM_TAGS = new Set(['pre', 'template', 'textarea', 'script', 'style']);
  17396. // Equivalent to \s with \u00a0 (non-breaking space) excluded.
  17397. // Based on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
  17398. const WS_CHARS = ' \f\n\r\t\v\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff';
  17399. const NO_WS_REGEXP = new RegExp(`[^${WS_CHARS}]`);
  17400. const WS_REPLACE_REGEXP = new RegExp(`[${WS_CHARS}]{2,}`, 'g');
  17401. function hasPreserveWhitespacesAttr(attrs) {
  17402. return attrs.some((attr) => attr.name === PRESERVE_WS_ATTR_NAME);
  17403. }
  17404. /**
  17405. * &ngsp; is a placeholder for non-removable space
  17406. * &ngsp; is converted to the 0xE500 PUA (Private Use Areas) unicode character
  17407. * and later on replaced by a space.
  17408. */
  17409. function replaceNgsp(value) {
  17410. // lexer is replacing the &ngsp; pseudo-entity with NGSP_UNICODE
  17411. return value.replace(new RegExp(NGSP_UNICODE, 'g'), ' ');
  17412. }
  17413. /**
  17414. * This visitor can walk HTML parse tree and remove / trim text nodes using the following rules:
  17415. * - consider spaces, tabs and new lines as whitespace characters;
  17416. * - drop text nodes consisting of whitespace characters only;
  17417. * - for all other text nodes replace consecutive whitespace characters with one space;
  17418. * - convert &ngsp; pseudo-entity to a single space;
  17419. *
  17420. * Removal and trimming of whitespaces have positive performance impact (less code to generate
  17421. * while compiling templates, faster view creation). At the same time it can be "destructive"
  17422. * in some cases (whitespaces can influence layout). Because of the potential of breaking layout
  17423. * this visitor is not activated by default in Angular 5 and people need to explicitly opt-in for
  17424. * whitespace removal. The default option for whitespace removal will be revisited in Angular 6
  17425. * and might be changed to "on" by default.
  17426. */
  17427. class WhitespaceVisitor {
  17428. visitElement(element, context) {
  17429. if (SKIP_WS_TRIM_TAGS.has(element.name) || hasPreserveWhitespacesAttr(element.attrs)) {
  17430. // don't descent into elements where we need to preserve whitespaces
  17431. // but still visit all attributes to eliminate one used as a market to preserve WS
  17432. return new Element(element.name, visitAll(this, element.attrs), element.children, element.sourceSpan, element.startSourceSpan, element.endSourceSpan, element.i18n);
  17433. }
  17434. return new Element(element.name, element.attrs, visitAllWithSiblings(this, element.children), element.sourceSpan, element.startSourceSpan, element.endSourceSpan, element.i18n);
  17435. }
  17436. visitAttribute(attribute, context) {
  17437. return attribute.name !== PRESERVE_WS_ATTR_NAME ? attribute : null;
  17438. }
  17439. visitText(text, context) {
  17440. const isNotBlank = text.value.match(NO_WS_REGEXP);
  17441. const hasExpansionSibling = context &&
  17442. (context.prev instanceof Expansion || context.next instanceof Expansion);
  17443. if (isNotBlank || hasExpansionSibling) {
  17444. // Process the whitespace in the tokens of this Text node
  17445. const tokens = text.tokens.map(token => token.type === 5 /* TokenType.TEXT */ ? createWhitespaceProcessedTextToken(token) : token);
  17446. // Process the whitespace of the value of this Text node
  17447. const value = processWhitespace(text.value);
  17448. return new Text(value, text.sourceSpan, tokens, text.i18n);
  17449. }
  17450. return null;
  17451. }
  17452. visitComment(comment, context) {
  17453. return comment;
  17454. }
  17455. visitExpansion(expansion, context) {
  17456. return expansion;
  17457. }
  17458. visitExpansionCase(expansionCase, context) {
  17459. return expansionCase;
  17460. }
  17461. }
  17462. function createWhitespaceProcessedTextToken({ type, parts, sourceSpan }) {
  17463. return { type, parts: [processWhitespace(parts[0])], sourceSpan };
  17464. }
  17465. function processWhitespace(text) {
  17466. return replaceNgsp(text).replace(WS_REPLACE_REGEXP, ' ');
  17467. }
  17468. function removeWhitespaces(htmlAstWithErrors) {
  17469. return new ParseTreeResult(visitAll(new WhitespaceVisitor(), htmlAstWithErrors.rootNodes), htmlAstWithErrors.errors);
  17470. }
  17471. function visitAllWithSiblings(visitor, nodes) {
  17472. const result = [];
  17473. nodes.forEach((ast, i) => {
  17474. const context = { prev: nodes[i - 1], next: nodes[i + 1] };
  17475. const astResult = ast.visit(visitor, context);
  17476. if (astResult) {
  17477. result.push(astResult);
  17478. }
  17479. });
  17480. return result;
  17481. }
  17482. function mapEntry(key, value) {
  17483. return { key, value, quoted: false };
  17484. }
  17485. function mapLiteral(obj, quoted = false) {
  17486. return literalMap(Object.keys(obj).map(key => ({
  17487. key,
  17488. quoted,
  17489. value: obj[key],
  17490. })));
  17491. }
  17492. /**
  17493. * Set of tagName|propertyName corresponding to Trusted Types sinks. Properties applying to all
  17494. * tags use '*'.
  17495. *
  17496. * Extracted from, and should be kept in sync with
  17497. * https://w3c.github.io/webappsec-trusted-types/dist/spec/#integrations
  17498. */
  17499. const TRUSTED_TYPES_SINKS = new Set([
  17500. // NOTE: All strings in this set *must* be lowercase!
  17501. // TrustedHTML
  17502. 'iframe|srcdoc',
  17503. '*|innerhtml',
  17504. '*|outerhtml',
  17505. // NB: no TrustedScript here, as the corresponding tags are stripped by the compiler.
  17506. // TrustedScriptURL
  17507. 'embed|src',
  17508. 'object|codebase',
  17509. 'object|data',
  17510. ]);
  17511. /**
  17512. * isTrustedTypesSink returns true if the given property on the given DOM tag is a Trusted Types
  17513. * sink. In that case, use `ElementSchemaRegistry.securityContext` to determine which particular
  17514. * Trusted Type is required for values passed to the sink:
  17515. * - SecurityContext.HTML corresponds to TrustedHTML
  17516. * - SecurityContext.RESOURCE_URL corresponds to TrustedScriptURL
  17517. */
  17518. function isTrustedTypesSink(tagName, propName) {
  17519. // Make sure comparisons are case insensitive, so that case differences between attribute and
  17520. // property names do not have a security impact.
  17521. tagName = tagName.toLowerCase();
  17522. propName = propName.toLowerCase();
  17523. return TRUSTED_TYPES_SINKS.has(tagName + '|' + propName) ||
  17524. TRUSTED_TYPES_SINKS.has('*|' + propName);
  17525. }
  17526. const PROPERTY_PARTS_SEPARATOR = '.';
  17527. const ATTRIBUTE_PREFIX = 'attr';
  17528. const CLASS_PREFIX = 'class';
  17529. const STYLE_PREFIX = 'style';
  17530. const TEMPLATE_ATTR_PREFIX$1 = '*';
  17531. const ANIMATE_PROP_PREFIX = 'animate-';
  17532. /**
  17533. * Parses bindings in templates and in the directive host area.
  17534. */
  17535. class BindingParser {
  17536. constructor(_exprParser, _interpolationConfig, _schemaRegistry, errors) {
  17537. this._exprParser = _exprParser;
  17538. this._interpolationConfig = _interpolationConfig;
  17539. this._schemaRegistry = _schemaRegistry;
  17540. this.errors = errors;
  17541. }
  17542. get interpolationConfig() {
  17543. return this._interpolationConfig;
  17544. }
  17545. createBoundHostProperties(properties, sourceSpan) {
  17546. const boundProps = [];
  17547. for (const propName of Object.keys(properties)) {
  17548. const expression = properties[propName];
  17549. if (typeof expression === 'string') {
  17550. this.parsePropertyBinding(propName, expression, true, sourceSpan, sourceSpan.start.offset, undefined, [],
  17551. // Use the `sourceSpan` for `keySpan`. This isn't really accurate, but neither is the
  17552. // sourceSpan, as it represents the sourceSpan of the host itself rather than the
  17553. // source of the host binding (which doesn't exist in the template). Regardless,
  17554. // neither of these values are used in Ivy but are only here to satisfy the function
  17555. // signature. This should likely be refactored in the future so that `sourceSpan`
  17556. // isn't being used inaccurately.
  17557. boundProps, sourceSpan);
  17558. }
  17559. else {
  17560. this._reportError(`Value of the host property binding "${propName}" needs to be a string representing an expression but got "${expression}" (${typeof expression})`, sourceSpan);
  17561. }
  17562. }
  17563. return boundProps;
  17564. }
  17565. createDirectiveHostEventAsts(hostListeners, sourceSpan) {
  17566. const targetEvents = [];
  17567. for (const propName of Object.keys(hostListeners)) {
  17568. const expression = hostListeners[propName];
  17569. if (typeof expression === 'string') {
  17570. // Use the `sourceSpan` for `keySpan` and `handlerSpan`. This isn't really accurate, but
  17571. // neither is the `sourceSpan`, as it represents the `sourceSpan` of the host itself
  17572. // rather than the source of the host binding (which doesn't exist in the template).
  17573. // Regardless, neither of these values are used in Ivy but are only here to satisfy the
  17574. // function signature. This should likely be refactored in the future so that `sourceSpan`
  17575. // isn't being used inaccurately.
  17576. this.parseEvent(propName, expression, /* isAssignmentEvent */ false, sourceSpan, sourceSpan, [], targetEvents, sourceSpan);
  17577. }
  17578. else {
  17579. this._reportError(`Value of the host listener "${propName}" needs to be a string representing an expression but got "${expression}" (${typeof expression})`, sourceSpan);
  17580. }
  17581. }
  17582. return targetEvents;
  17583. }
  17584. parseInterpolation(value, sourceSpan, interpolatedTokens) {
  17585. const sourceInfo = sourceSpan.start.toString();
  17586. const absoluteOffset = sourceSpan.fullStart.offset;
  17587. try {
  17588. const ast = this._exprParser.parseInterpolation(value, sourceInfo, absoluteOffset, interpolatedTokens, this._interpolationConfig);
  17589. if (ast)
  17590. this._reportExpressionParserErrors(ast.errors, sourceSpan);
  17591. return ast;
  17592. }
  17593. catch (e) {
  17594. this._reportError(`${e}`, sourceSpan);
  17595. return this._exprParser.wrapLiteralPrimitive('ERROR', sourceInfo, absoluteOffset);
  17596. }
  17597. }
  17598. /**
  17599. * Similar to `parseInterpolation`, but treats the provided string as a single expression
  17600. * element that would normally appear within the interpolation prefix and suffix (`{{` and `}}`).
  17601. * This is used for parsing the switch expression in ICUs.
  17602. */
  17603. parseInterpolationExpression(expression, sourceSpan) {
  17604. const sourceInfo = sourceSpan.start.toString();
  17605. const absoluteOffset = sourceSpan.start.offset;
  17606. try {
  17607. const ast = this._exprParser.parseInterpolationExpression(expression, sourceInfo, absoluteOffset);
  17608. if (ast)
  17609. this._reportExpressionParserErrors(ast.errors, sourceSpan);
  17610. return ast;
  17611. }
  17612. catch (e) {
  17613. this._reportError(`${e}`, sourceSpan);
  17614. return this._exprParser.wrapLiteralPrimitive('ERROR', sourceInfo, absoluteOffset);
  17615. }
  17616. }
  17617. /**
  17618. * Parses the bindings in a microsyntax expression, and converts them to
  17619. * `ParsedProperty` or `ParsedVariable`.
  17620. *
  17621. * @param tplKey template binding name
  17622. * @param tplValue template binding value
  17623. * @param sourceSpan span of template binding relative to entire the template
  17624. * @param absoluteValueOffset start of the tplValue relative to the entire template
  17625. * @param targetMatchableAttrs potential attributes to match in the template
  17626. * @param targetProps target property bindings in the template
  17627. * @param targetVars target variables in the template
  17628. */
  17629. parseInlineTemplateBinding(tplKey, tplValue, sourceSpan, absoluteValueOffset, targetMatchableAttrs, targetProps, targetVars, isIvyAst) {
  17630. const absoluteKeyOffset = sourceSpan.start.offset + TEMPLATE_ATTR_PREFIX$1.length;
  17631. const bindings = this._parseTemplateBindings(tplKey, tplValue, sourceSpan, absoluteKeyOffset, absoluteValueOffset);
  17632. for (const binding of bindings) {
  17633. // sourceSpan is for the entire HTML attribute. bindingSpan is for a particular
  17634. // binding within the microsyntax expression so it's more narrow than sourceSpan.
  17635. const bindingSpan = moveParseSourceSpan(sourceSpan, binding.sourceSpan);
  17636. const key = binding.key.source;
  17637. const keySpan = moveParseSourceSpan(sourceSpan, binding.key.span);
  17638. if (binding instanceof VariableBinding) {
  17639. const value = binding.value ? binding.value.source : '$implicit';
  17640. const valueSpan = binding.value ? moveParseSourceSpan(sourceSpan, binding.value.span) : undefined;
  17641. targetVars.push(new ParsedVariable(key, value, bindingSpan, keySpan, valueSpan));
  17642. }
  17643. else if (binding.value) {
  17644. const srcSpan = isIvyAst ? bindingSpan : sourceSpan;
  17645. const valueSpan = moveParseSourceSpan(sourceSpan, binding.value.ast.sourceSpan);
  17646. this._parsePropertyAst(key, binding.value, srcSpan, keySpan, valueSpan, targetMatchableAttrs, targetProps);
  17647. }
  17648. else {
  17649. targetMatchableAttrs.push([key, '' /* value */]);
  17650. // Since this is a literal attribute with no RHS, source span should be
  17651. // just the key span.
  17652. this.parseLiteralAttr(key, null /* value */, keySpan, absoluteValueOffset, undefined /* valueSpan */, targetMatchableAttrs, targetProps, keySpan);
  17653. }
  17654. }
  17655. }
  17656. /**
  17657. * Parses the bindings in a microsyntax expression, e.g.
  17658. * ```
  17659. * <tag *tplKey="let value1 = prop; let value2 = localVar">
  17660. * ```
  17661. *
  17662. * @param tplKey template binding name
  17663. * @param tplValue template binding value
  17664. * @param sourceSpan span of template binding relative to entire the template
  17665. * @param absoluteKeyOffset start of the `tplKey`
  17666. * @param absoluteValueOffset start of the `tplValue`
  17667. */
  17668. _parseTemplateBindings(tplKey, tplValue, sourceSpan, absoluteKeyOffset, absoluteValueOffset) {
  17669. const sourceInfo = sourceSpan.start.toString();
  17670. try {
  17671. const bindingsResult = this._exprParser.parseTemplateBindings(tplKey, tplValue, sourceInfo, absoluteKeyOffset, absoluteValueOffset);
  17672. this._reportExpressionParserErrors(bindingsResult.errors, sourceSpan);
  17673. bindingsResult.warnings.forEach((warning) => {
  17674. this._reportError(warning, sourceSpan, ParseErrorLevel.WARNING);
  17675. });
  17676. return bindingsResult.templateBindings;
  17677. }
  17678. catch (e) {
  17679. this._reportError(`${e}`, sourceSpan);
  17680. return [];
  17681. }
  17682. }
  17683. parseLiteralAttr(name, value, sourceSpan, absoluteOffset, valueSpan, targetMatchableAttrs, targetProps, keySpan) {
  17684. if (isAnimationLabel(name)) {
  17685. name = name.substring(1);
  17686. if (keySpan !== undefined) {
  17687. keySpan = moveParseSourceSpan(keySpan, new AbsoluteSourceSpan(keySpan.start.offset + 1, keySpan.end.offset));
  17688. }
  17689. if (value) {
  17690. this._reportError(`Assigning animation triggers via @prop="exp" attributes with an expression is invalid.` +
  17691. ` Use property bindings (e.g. [@prop]="exp") or use an attribute without a value (e.g. @prop) instead.`, sourceSpan, ParseErrorLevel.ERROR);
  17692. }
  17693. this._parseAnimation(name, value, sourceSpan, absoluteOffset, keySpan, valueSpan, targetMatchableAttrs, targetProps);
  17694. }
  17695. else {
  17696. targetProps.push(new ParsedProperty(name, this._exprParser.wrapLiteralPrimitive(value, '', absoluteOffset), ParsedPropertyType.LITERAL_ATTR, sourceSpan, keySpan, valueSpan));
  17697. }
  17698. }
  17699. parsePropertyBinding(name, expression, isHost, sourceSpan, absoluteOffset, valueSpan, targetMatchableAttrs, targetProps, keySpan) {
  17700. if (name.length === 0) {
  17701. this._reportError(`Property name is missing in binding`, sourceSpan);
  17702. }
  17703. let isAnimationProp = false;
  17704. if (name.startsWith(ANIMATE_PROP_PREFIX)) {
  17705. isAnimationProp = true;
  17706. name = name.substring(ANIMATE_PROP_PREFIX.length);
  17707. if (keySpan !== undefined) {
  17708. keySpan = moveParseSourceSpan(keySpan, new AbsoluteSourceSpan(keySpan.start.offset + ANIMATE_PROP_PREFIX.length, keySpan.end.offset));
  17709. }
  17710. }
  17711. else if (isAnimationLabel(name)) {
  17712. isAnimationProp = true;
  17713. name = name.substring(1);
  17714. if (keySpan !== undefined) {
  17715. keySpan = moveParseSourceSpan(keySpan, new AbsoluteSourceSpan(keySpan.start.offset + 1, keySpan.end.offset));
  17716. }
  17717. }
  17718. if (isAnimationProp) {
  17719. this._parseAnimation(name, expression, sourceSpan, absoluteOffset, keySpan, valueSpan, targetMatchableAttrs, targetProps);
  17720. }
  17721. else {
  17722. this._parsePropertyAst(name, this._parseBinding(expression, isHost, valueSpan || sourceSpan, absoluteOffset), sourceSpan, keySpan, valueSpan, targetMatchableAttrs, targetProps);
  17723. }
  17724. }
  17725. parsePropertyInterpolation(name, value, sourceSpan, valueSpan, targetMatchableAttrs, targetProps, keySpan, interpolatedTokens) {
  17726. const expr = this.parseInterpolation(value, valueSpan || sourceSpan, interpolatedTokens);
  17727. if (expr) {
  17728. this._parsePropertyAst(name, expr, sourceSpan, keySpan, valueSpan, targetMatchableAttrs, targetProps);
  17729. return true;
  17730. }
  17731. return false;
  17732. }
  17733. _parsePropertyAst(name, ast, sourceSpan, keySpan, valueSpan, targetMatchableAttrs, targetProps) {
  17734. targetMatchableAttrs.push([name, ast.source]);
  17735. targetProps.push(new ParsedProperty(name, ast, ParsedPropertyType.DEFAULT, sourceSpan, keySpan, valueSpan));
  17736. }
  17737. _parseAnimation(name, expression, sourceSpan, absoluteOffset, keySpan, valueSpan, targetMatchableAttrs, targetProps) {
  17738. if (name.length === 0) {
  17739. this._reportError('Animation trigger is missing', sourceSpan);
  17740. }
  17741. // This will occur when a @trigger is not paired with an expression.
  17742. // For animations it is valid to not have an expression since */void
  17743. // states will be applied by angular when the element is attached/detached
  17744. const ast = this._parseBinding(expression || 'undefined', false, valueSpan || sourceSpan, absoluteOffset);
  17745. targetMatchableAttrs.push([name, ast.source]);
  17746. targetProps.push(new ParsedProperty(name, ast, ParsedPropertyType.ANIMATION, sourceSpan, keySpan, valueSpan));
  17747. }
  17748. _parseBinding(value, isHostBinding, sourceSpan, absoluteOffset) {
  17749. const sourceInfo = (sourceSpan && sourceSpan.start || '(unknown)').toString();
  17750. try {
  17751. const ast = isHostBinding ?
  17752. this._exprParser.parseSimpleBinding(value, sourceInfo, absoluteOffset, this._interpolationConfig) :
  17753. this._exprParser.parseBinding(value, sourceInfo, absoluteOffset, this._interpolationConfig);
  17754. if (ast)
  17755. this._reportExpressionParserErrors(ast.errors, sourceSpan);
  17756. return ast;
  17757. }
  17758. catch (e) {
  17759. this._reportError(`${e}`, sourceSpan);
  17760. return this._exprParser.wrapLiteralPrimitive('ERROR', sourceInfo, absoluteOffset);
  17761. }
  17762. }
  17763. createBoundElementProperty(elementSelector, boundProp, skipValidation = false, mapPropertyName = true) {
  17764. if (boundProp.isAnimation) {
  17765. return new BoundElementProperty(boundProp.name, 4 /* BindingType.Animation */, SecurityContext.NONE, boundProp.expression, null, boundProp.sourceSpan, boundProp.keySpan, boundProp.valueSpan);
  17766. }
  17767. let unit = null;
  17768. let bindingType = undefined;
  17769. let boundPropertyName = null;
  17770. const parts = boundProp.name.split(PROPERTY_PARTS_SEPARATOR);
  17771. let securityContexts = undefined;
  17772. // Check for special cases (prefix style, attr, class)
  17773. if (parts.length > 1) {
  17774. if (parts[0] == ATTRIBUTE_PREFIX) {
  17775. boundPropertyName = parts.slice(1).join(PROPERTY_PARTS_SEPARATOR);
  17776. if (!skipValidation) {
  17777. this._validatePropertyOrAttributeName(boundPropertyName, boundProp.sourceSpan, true);
  17778. }
  17779. securityContexts = calcPossibleSecurityContexts(this._schemaRegistry, elementSelector, boundPropertyName, true);
  17780. const nsSeparatorIdx = boundPropertyName.indexOf(':');
  17781. if (nsSeparatorIdx > -1) {
  17782. const ns = boundPropertyName.substring(0, nsSeparatorIdx);
  17783. const name = boundPropertyName.substring(nsSeparatorIdx + 1);
  17784. boundPropertyName = mergeNsAndName(ns, name);
  17785. }
  17786. bindingType = 1 /* BindingType.Attribute */;
  17787. }
  17788. else if (parts[0] == CLASS_PREFIX) {
  17789. boundPropertyName = parts[1];
  17790. bindingType = 2 /* BindingType.Class */;
  17791. securityContexts = [SecurityContext.NONE];
  17792. }
  17793. else if (parts[0] == STYLE_PREFIX) {
  17794. unit = parts.length > 2 ? parts[2] : null;
  17795. boundPropertyName = parts[1];
  17796. bindingType = 3 /* BindingType.Style */;
  17797. securityContexts = [SecurityContext.STYLE];
  17798. }
  17799. }
  17800. // If not a special case, use the full property name
  17801. if (boundPropertyName === null) {
  17802. const mappedPropName = this._schemaRegistry.getMappedPropName(boundProp.name);
  17803. boundPropertyName = mapPropertyName ? mappedPropName : boundProp.name;
  17804. securityContexts = calcPossibleSecurityContexts(this._schemaRegistry, elementSelector, mappedPropName, false);
  17805. bindingType = 0 /* BindingType.Property */;
  17806. if (!skipValidation) {
  17807. this._validatePropertyOrAttributeName(mappedPropName, boundProp.sourceSpan, false);
  17808. }
  17809. }
  17810. return new BoundElementProperty(boundPropertyName, bindingType, securityContexts[0], boundProp.expression, unit, boundProp.sourceSpan, boundProp.keySpan, boundProp.valueSpan);
  17811. }
  17812. // TODO: keySpan should be required but was made optional to avoid changing VE parser.
  17813. parseEvent(name, expression, isAssignmentEvent, sourceSpan, handlerSpan, targetMatchableAttrs, targetEvents, keySpan) {
  17814. if (name.length === 0) {
  17815. this._reportError(`Event name is missing in binding`, sourceSpan);
  17816. }
  17817. if (isAnimationLabel(name)) {
  17818. name = name.slice(1);
  17819. if (keySpan !== undefined) {
  17820. keySpan = moveParseSourceSpan(keySpan, new AbsoluteSourceSpan(keySpan.start.offset + 1, keySpan.end.offset));
  17821. }
  17822. this._parseAnimationEvent(name, expression, isAssignmentEvent, sourceSpan, handlerSpan, targetEvents, keySpan);
  17823. }
  17824. else {
  17825. this._parseRegularEvent(name, expression, isAssignmentEvent, sourceSpan, handlerSpan, targetMatchableAttrs, targetEvents, keySpan);
  17826. }
  17827. }
  17828. calcPossibleSecurityContexts(selector, propName, isAttribute) {
  17829. const prop = this._schemaRegistry.getMappedPropName(propName);
  17830. return calcPossibleSecurityContexts(this._schemaRegistry, selector, prop, isAttribute);
  17831. }
  17832. _parseAnimationEvent(name, expression, isAssignmentEvent, sourceSpan, handlerSpan, targetEvents, keySpan) {
  17833. const matches = splitAtPeriod(name, [name, '']);
  17834. const eventName = matches[0];
  17835. const phase = matches[1].toLowerCase();
  17836. const ast = this._parseAction(expression, isAssignmentEvent, handlerSpan);
  17837. targetEvents.push(new ParsedEvent(eventName, phase, 1 /* ParsedEventType.Animation */, ast, sourceSpan, handlerSpan, keySpan));
  17838. if (eventName.length === 0) {
  17839. this._reportError(`Animation event name is missing in binding`, sourceSpan);
  17840. }
  17841. if (phase) {
  17842. if (phase !== 'start' && phase !== 'done') {
  17843. this._reportError(`The provided animation output phase value "${phase}" for "@${eventName}" is not supported (use start or done)`, sourceSpan);
  17844. }
  17845. }
  17846. else {
  17847. this._reportError(`The animation trigger output event (@${eventName}) is missing its phase value name (start or done are currently supported)`, sourceSpan);
  17848. }
  17849. }
  17850. _parseRegularEvent(name, expression, isAssignmentEvent, sourceSpan, handlerSpan, targetMatchableAttrs, targetEvents, keySpan) {
  17851. // long format: 'target: eventName'
  17852. const [target, eventName] = splitAtColon(name, [null, name]);
  17853. const ast = this._parseAction(expression, isAssignmentEvent, handlerSpan);
  17854. targetMatchableAttrs.push([name, ast.source]);
  17855. targetEvents.push(new ParsedEvent(eventName, target, 0 /* ParsedEventType.Regular */, ast, sourceSpan, handlerSpan, keySpan));
  17856. // Don't detect directives for event names for now,
  17857. // so don't add the event name to the matchableAttrs
  17858. }
  17859. _parseAction(value, isAssignmentEvent, sourceSpan) {
  17860. const sourceInfo = (sourceSpan && sourceSpan.start || '(unknown').toString();
  17861. const absoluteOffset = (sourceSpan && sourceSpan.start) ? sourceSpan.start.offset : 0;
  17862. try {
  17863. const ast = this._exprParser.parseAction(value, isAssignmentEvent, sourceInfo, absoluteOffset, this._interpolationConfig);
  17864. if (ast) {
  17865. this._reportExpressionParserErrors(ast.errors, sourceSpan);
  17866. }
  17867. if (!ast || ast.ast instanceof EmptyExpr) {
  17868. this._reportError(`Empty expressions are not allowed`, sourceSpan);
  17869. return this._exprParser.wrapLiteralPrimitive('ERROR', sourceInfo, absoluteOffset);
  17870. }
  17871. return ast;
  17872. }
  17873. catch (e) {
  17874. this._reportError(`${e}`, sourceSpan);
  17875. return this._exprParser.wrapLiteralPrimitive('ERROR', sourceInfo, absoluteOffset);
  17876. }
  17877. }
  17878. _reportError(message, sourceSpan, level = ParseErrorLevel.ERROR) {
  17879. this.errors.push(new ParseError(sourceSpan, message, level));
  17880. }
  17881. _reportExpressionParserErrors(errors, sourceSpan) {
  17882. for (const error of errors) {
  17883. this._reportError(error.message, sourceSpan);
  17884. }
  17885. }
  17886. /**
  17887. * @param propName the name of the property / attribute
  17888. * @param sourceSpan
  17889. * @param isAttr true when binding to an attribute
  17890. */
  17891. _validatePropertyOrAttributeName(propName, sourceSpan, isAttr) {
  17892. const report = isAttr ? this._schemaRegistry.validateAttribute(propName) :
  17893. this._schemaRegistry.validateProperty(propName);
  17894. if (report.error) {
  17895. this._reportError(report.msg, sourceSpan, ParseErrorLevel.ERROR);
  17896. }
  17897. }
  17898. }
  17899. class PipeCollector extends RecursiveAstVisitor {
  17900. constructor() {
  17901. super(...arguments);
  17902. this.pipes = new Map();
  17903. }
  17904. visitPipe(ast, context) {
  17905. this.pipes.set(ast.name, ast);
  17906. ast.exp.visit(this);
  17907. this.visitAll(ast.args, context);
  17908. return null;
  17909. }
  17910. }
  17911. function isAnimationLabel(name) {
  17912. return name[0] == '@';
  17913. }
  17914. function calcPossibleSecurityContexts(registry, selector, propName, isAttribute) {
  17915. const ctxs = [];
  17916. CssSelector.parse(selector).forEach((selector) => {
  17917. const elementNames = selector.element ? [selector.element] : registry.allKnownElementNames();
  17918. const notElementNames = new Set(selector.notSelectors.filter(selector => selector.isElementSelector())
  17919. .map((selector) => selector.element));
  17920. const possibleElementNames = elementNames.filter(elementName => !notElementNames.has(elementName));
  17921. ctxs.push(...possibleElementNames.map(elementName => registry.securityContext(elementName, propName, isAttribute)));
  17922. });
  17923. return ctxs.length === 0 ? [SecurityContext.NONE] : Array.from(new Set(ctxs)).sort();
  17924. }
  17925. /**
  17926. * Compute a new ParseSourceSpan based off an original `sourceSpan` by using
  17927. * absolute offsets from the specified `absoluteSpan`.
  17928. *
  17929. * @param sourceSpan original source span
  17930. * @param absoluteSpan absolute source span to move to
  17931. */
  17932. function moveParseSourceSpan(sourceSpan, absoluteSpan) {
  17933. // The difference of two absolute offsets provide the relative offset
  17934. const startDiff = absoluteSpan.start - sourceSpan.start.offset;
  17935. const endDiff = absoluteSpan.end - sourceSpan.end.offset;
  17936. return new ParseSourceSpan(sourceSpan.start.moveBy(startDiff), sourceSpan.end.moveBy(endDiff), sourceSpan.fullStart.moveBy(startDiff), sourceSpan.details);
  17937. }
  17938. // Some of the code comes from WebComponents.JS
  17939. // https://github.com/webcomponents/webcomponentsjs/blob/master/src/HTMLImports/path.js
  17940. function isStyleUrlResolvable(url) {
  17941. if (url == null || url.length === 0 || url[0] == '/')
  17942. return false;
  17943. const schemeMatch = url.match(URL_WITH_SCHEMA_REGEXP);
  17944. return schemeMatch === null || schemeMatch[1] == 'package' || schemeMatch[1] == 'asset';
  17945. }
  17946. const URL_WITH_SCHEMA_REGEXP = /^([^:/?#]+):/;
  17947. const NG_CONTENT_SELECT_ATTR$1 = 'select';
  17948. const LINK_ELEMENT = 'link';
  17949. const LINK_STYLE_REL_ATTR = 'rel';
  17950. const LINK_STYLE_HREF_ATTR = 'href';
  17951. const LINK_STYLE_REL_VALUE = 'stylesheet';
  17952. const STYLE_ELEMENT = 'style';
  17953. const SCRIPT_ELEMENT = 'script';
  17954. const NG_NON_BINDABLE_ATTR = 'ngNonBindable';
  17955. const NG_PROJECT_AS = 'ngProjectAs';
  17956. function preparseElement(ast) {
  17957. let selectAttr = null;
  17958. let hrefAttr = null;
  17959. let relAttr = null;
  17960. let nonBindable = false;
  17961. let projectAs = '';
  17962. ast.attrs.forEach(attr => {
  17963. const lcAttrName = attr.name.toLowerCase();
  17964. if (lcAttrName == NG_CONTENT_SELECT_ATTR$1) {
  17965. selectAttr = attr.value;
  17966. }
  17967. else if (lcAttrName == LINK_STYLE_HREF_ATTR) {
  17968. hrefAttr = attr.value;
  17969. }
  17970. else if (lcAttrName == LINK_STYLE_REL_ATTR) {
  17971. relAttr = attr.value;
  17972. }
  17973. else if (attr.name == NG_NON_BINDABLE_ATTR) {
  17974. nonBindable = true;
  17975. }
  17976. else if (attr.name == NG_PROJECT_AS) {
  17977. if (attr.value.length > 0) {
  17978. projectAs = attr.value;
  17979. }
  17980. }
  17981. });
  17982. selectAttr = normalizeNgContentSelect(selectAttr);
  17983. const nodeName = ast.name.toLowerCase();
  17984. let type = PreparsedElementType.OTHER;
  17985. if (isNgContent(nodeName)) {
  17986. type = PreparsedElementType.NG_CONTENT;
  17987. }
  17988. else if (nodeName == STYLE_ELEMENT) {
  17989. type = PreparsedElementType.STYLE;
  17990. }
  17991. else if (nodeName == SCRIPT_ELEMENT) {
  17992. type = PreparsedElementType.SCRIPT;
  17993. }
  17994. else if (nodeName == LINK_ELEMENT && relAttr == LINK_STYLE_REL_VALUE) {
  17995. type = PreparsedElementType.STYLESHEET;
  17996. }
  17997. return new PreparsedElement(type, selectAttr, hrefAttr, nonBindable, projectAs);
  17998. }
  17999. var PreparsedElementType;
  18000. (function (PreparsedElementType) {
  18001. PreparsedElementType[PreparsedElementType["NG_CONTENT"] = 0] = "NG_CONTENT";
  18002. PreparsedElementType[PreparsedElementType["STYLE"] = 1] = "STYLE";
  18003. PreparsedElementType[PreparsedElementType["STYLESHEET"] = 2] = "STYLESHEET";
  18004. PreparsedElementType[PreparsedElementType["SCRIPT"] = 3] = "SCRIPT";
  18005. PreparsedElementType[PreparsedElementType["OTHER"] = 4] = "OTHER";
  18006. })(PreparsedElementType || (PreparsedElementType = {}));
  18007. class PreparsedElement {
  18008. constructor(type, selectAttr, hrefAttr, nonBindable, projectAs) {
  18009. this.type = type;
  18010. this.selectAttr = selectAttr;
  18011. this.hrefAttr = hrefAttr;
  18012. this.nonBindable = nonBindable;
  18013. this.projectAs = projectAs;
  18014. }
  18015. }
  18016. function normalizeNgContentSelect(selectAttr) {
  18017. if (selectAttr === null || selectAttr.length === 0) {
  18018. return '*';
  18019. }
  18020. return selectAttr;
  18021. }
  18022. const BIND_NAME_REGEXP = /^(?:(bind-)|(let-)|(ref-|#)|(on-)|(bindon-)|(@))(.*)$/;
  18023. // Group 1 = "bind-"
  18024. const KW_BIND_IDX = 1;
  18025. // Group 2 = "let-"
  18026. const KW_LET_IDX = 2;
  18027. // Group 3 = "ref-/#"
  18028. const KW_REF_IDX = 3;
  18029. // Group 4 = "on-"
  18030. const KW_ON_IDX = 4;
  18031. // Group 5 = "bindon-"
  18032. const KW_BINDON_IDX = 5;
  18033. // Group 6 = "@"
  18034. const KW_AT_IDX = 6;
  18035. // Group 7 = the identifier after "bind-", "let-", "ref-/#", "on-", "bindon-" or "@"
  18036. const IDENT_KW_IDX = 7;
  18037. const BINDING_DELIMS = {
  18038. BANANA_BOX: { start: '[(', end: ')]' },
  18039. PROPERTY: { start: '[', end: ']' },
  18040. EVENT: { start: '(', end: ')' },
  18041. };
  18042. const TEMPLATE_ATTR_PREFIX = '*';
  18043. function htmlAstToRender3Ast(htmlNodes, bindingParser, options) {
  18044. const transformer = new HtmlAstToIvyAst(bindingParser, options);
  18045. const ivyNodes = visitAll(transformer, htmlNodes);
  18046. // Errors might originate in either the binding parser or the html to ivy transformer
  18047. const allErrors = bindingParser.errors.concat(transformer.errors);
  18048. const result = {
  18049. nodes: ivyNodes,
  18050. errors: allErrors,
  18051. styleUrls: transformer.styleUrls,
  18052. styles: transformer.styles,
  18053. ngContentSelectors: transformer.ngContentSelectors
  18054. };
  18055. if (options.collectCommentNodes) {
  18056. result.commentNodes = transformer.commentNodes;
  18057. }
  18058. return result;
  18059. }
  18060. class HtmlAstToIvyAst {
  18061. constructor(bindingParser, options) {
  18062. this.bindingParser = bindingParser;
  18063. this.options = options;
  18064. this.errors = [];
  18065. this.styles = [];
  18066. this.styleUrls = [];
  18067. this.ngContentSelectors = [];
  18068. // This array will be populated if `Render3ParseOptions['collectCommentNodes']` is true
  18069. this.commentNodes = [];
  18070. this.inI18nBlock = false;
  18071. }
  18072. // HTML visitor
  18073. visitElement(element) {
  18074. const isI18nRootElement = isI18nRootNode(element.i18n);
  18075. if (isI18nRootElement) {
  18076. if (this.inI18nBlock) {
  18077. this.reportError('Cannot mark an element as translatable inside of a translatable section. Please remove the nested i18n marker.', element.sourceSpan);
  18078. }
  18079. this.inI18nBlock = true;
  18080. }
  18081. const preparsedElement = preparseElement(element);
  18082. if (preparsedElement.type === PreparsedElementType.SCRIPT) {
  18083. return null;
  18084. }
  18085. else if (preparsedElement.type === PreparsedElementType.STYLE) {
  18086. const contents = textContents(element);
  18087. if (contents !== null) {
  18088. this.styles.push(contents);
  18089. }
  18090. return null;
  18091. }
  18092. else if (preparsedElement.type === PreparsedElementType.STYLESHEET &&
  18093. isStyleUrlResolvable(preparsedElement.hrefAttr)) {
  18094. this.styleUrls.push(preparsedElement.hrefAttr);
  18095. return null;
  18096. }
  18097. // Whether the element is a `<ng-template>`
  18098. const isTemplateElement = isNgTemplate(element.name);
  18099. const parsedProperties = [];
  18100. const boundEvents = [];
  18101. const variables = [];
  18102. const references = [];
  18103. const attributes = [];
  18104. const i18nAttrsMeta = {};
  18105. const templateParsedProperties = [];
  18106. const templateVariables = [];
  18107. // Whether the element has any *-attribute
  18108. let elementHasInlineTemplate = false;
  18109. for (const attribute of element.attrs) {
  18110. let hasBinding = false;
  18111. const normalizedName = normalizeAttributeName(attribute.name);
  18112. // `*attr` defines template bindings
  18113. let isTemplateBinding = false;
  18114. if (attribute.i18n) {
  18115. i18nAttrsMeta[attribute.name] = attribute.i18n;
  18116. }
  18117. if (normalizedName.startsWith(TEMPLATE_ATTR_PREFIX)) {
  18118. // *-attributes
  18119. if (elementHasInlineTemplate) {
  18120. this.reportError(`Can't have multiple template bindings on one element. Use only one attribute prefixed with *`, attribute.sourceSpan);
  18121. }
  18122. isTemplateBinding = true;
  18123. elementHasInlineTemplate = true;
  18124. const templateValue = attribute.value;
  18125. const templateKey = normalizedName.substring(TEMPLATE_ATTR_PREFIX.length);
  18126. const parsedVariables = [];
  18127. const absoluteValueOffset = attribute.valueSpan ?
  18128. attribute.valueSpan.start.offset :
  18129. // If there is no value span the attribute does not have a value, like `attr` in
  18130. //`<div attr></div>`. In this case, point to one character beyond the last character of
  18131. // the attribute name.
  18132. attribute.sourceSpan.start.offset + attribute.name.length;
  18133. this.bindingParser.parseInlineTemplateBinding(templateKey, templateValue, attribute.sourceSpan, absoluteValueOffset, [], templateParsedProperties, parsedVariables, true /* isIvyAst */);
  18134. templateVariables.push(...parsedVariables.map(v => new Variable(v.name, v.value, v.sourceSpan, v.keySpan, v.valueSpan)));
  18135. }
  18136. else {
  18137. // Check for variables, events, property bindings, interpolation
  18138. hasBinding = this.parseAttribute(isTemplateElement, attribute, [], parsedProperties, boundEvents, variables, references);
  18139. }
  18140. if (!hasBinding && !isTemplateBinding) {
  18141. // don't include the bindings as attributes as well in the AST
  18142. attributes.push(this.visitAttribute(attribute));
  18143. }
  18144. }
  18145. const children = visitAll(preparsedElement.nonBindable ? NON_BINDABLE_VISITOR : this, element.children);
  18146. let parsedElement;
  18147. if (preparsedElement.type === PreparsedElementType.NG_CONTENT) {
  18148. // `<ng-content>`
  18149. if (element.children &&
  18150. !element.children.every((node) => isEmptyTextNode(node) || isCommentNode(node))) {
  18151. this.reportError(`<ng-content> element cannot have content.`, element.sourceSpan);
  18152. }
  18153. const selector = preparsedElement.selectAttr;
  18154. const attrs = element.attrs.map(attr => this.visitAttribute(attr));
  18155. parsedElement = new Content(selector, attrs, element.sourceSpan, element.i18n);
  18156. this.ngContentSelectors.push(selector);
  18157. }
  18158. else if (isTemplateElement) {
  18159. // `<ng-template>`
  18160. const attrs = this.extractAttributes(element.name, parsedProperties, i18nAttrsMeta);
  18161. parsedElement = new Template(element.name, attributes, attrs.bound, boundEvents, [ /* no template attributes */], children, references, variables, element.sourceSpan, element.startSourceSpan, element.endSourceSpan, element.i18n);
  18162. }
  18163. else {
  18164. const attrs = this.extractAttributes(element.name, parsedProperties, i18nAttrsMeta);
  18165. parsedElement = new Element$1(element.name, attributes, attrs.bound, boundEvents, children, references, element.sourceSpan, element.startSourceSpan, element.endSourceSpan, element.i18n);
  18166. }
  18167. if (elementHasInlineTemplate) {
  18168. // If this node is an inline-template (e.g. has *ngFor) then we need to create a template
  18169. // node that contains this node.
  18170. // Moreover, if the node is an element, then we need to hoist its attributes to the template
  18171. // node for matching against content projection selectors.
  18172. const attrs = this.extractAttributes('ng-template', templateParsedProperties, i18nAttrsMeta);
  18173. const templateAttrs = [];
  18174. attrs.literal.forEach(attr => templateAttrs.push(attr));
  18175. attrs.bound.forEach(attr => templateAttrs.push(attr));
  18176. const hoistedAttrs = parsedElement instanceof Element$1 ?
  18177. {
  18178. attributes: parsedElement.attributes,
  18179. inputs: parsedElement.inputs,
  18180. outputs: parsedElement.outputs,
  18181. } :
  18182. { attributes: [], inputs: [], outputs: [] };
  18183. // For <ng-template>s with structural directives on them, avoid passing i18n information to
  18184. // the wrapping template to prevent unnecessary i18n instructions from being generated. The
  18185. // necessary i18n meta information will be extracted from child elements.
  18186. const i18n = isTemplateElement && isI18nRootElement ? undefined : element.i18n;
  18187. const name = parsedElement instanceof Template ? null : parsedElement.name;
  18188. parsedElement = new Template(name, hoistedAttrs.attributes, hoistedAttrs.inputs, hoistedAttrs.outputs, templateAttrs, [parsedElement], [ /* no references */], templateVariables, element.sourceSpan, element.startSourceSpan, element.endSourceSpan, i18n);
  18189. }
  18190. if (isI18nRootElement) {
  18191. this.inI18nBlock = false;
  18192. }
  18193. return parsedElement;
  18194. }
  18195. visitAttribute(attribute) {
  18196. return new TextAttribute(attribute.name, attribute.value, attribute.sourceSpan, attribute.keySpan, attribute.valueSpan, attribute.i18n);
  18197. }
  18198. visitText(text) {
  18199. return this._visitTextWithInterpolation(text.value, text.sourceSpan, text.tokens, text.i18n);
  18200. }
  18201. visitExpansion(expansion) {
  18202. if (!expansion.i18n) {
  18203. // do not generate Icu in case it was created
  18204. // outside of i18n block in a template
  18205. return null;
  18206. }
  18207. if (!isI18nRootNode(expansion.i18n)) {
  18208. throw new Error(`Invalid type "${expansion.i18n.constructor}" for "i18n" property of ${expansion.sourceSpan.toString()}. Expected a "Message"`);
  18209. }
  18210. const message = expansion.i18n;
  18211. const vars = {};
  18212. const placeholders = {};
  18213. // extract VARs from ICUs - we process them separately while
  18214. // assembling resulting message via goog.getMsg function, since
  18215. // we need to pass them to top-level goog.getMsg call
  18216. Object.keys(message.placeholders).forEach(key => {
  18217. const value = message.placeholders[key];
  18218. if (key.startsWith(I18N_ICU_VAR_PREFIX)) {
  18219. // Currently when the `plural` or `select` keywords in an ICU contain trailing spaces (e.g.
  18220. // `{count, select , ...}`), these spaces are also included into the key names in ICU vars
  18221. // (e.g. "VAR_SELECT "). These trailing spaces are not desirable, since they will later be
  18222. // converted into `_` symbols while normalizing placeholder names, which might lead to
  18223. // mismatches at runtime (i.e. placeholder will not be replaced with the correct value).
  18224. const formattedKey = key.trim();
  18225. const ast = this.bindingParser.parseInterpolationExpression(value.text, value.sourceSpan);
  18226. vars[formattedKey] = new BoundText(ast, value.sourceSpan);
  18227. }
  18228. else {
  18229. placeholders[key] = this._visitTextWithInterpolation(value.text, value.sourceSpan, null);
  18230. }
  18231. });
  18232. return new Icu$1(vars, placeholders, expansion.sourceSpan, message);
  18233. }
  18234. visitExpansionCase(expansionCase) {
  18235. return null;
  18236. }
  18237. visitComment(comment) {
  18238. if (this.options.collectCommentNodes) {
  18239. this.commentNodes.push(new Comment$1(comment.value || '', comment.sourceSpan));
  18240. }
  18241. return null;
  18242. }
  18243. // convert view engine `ParsedProperty` to a format suitable for IVY
  18244. extractAttributes(elementName, properties, i18nPropsMeta) {
  18245. const bound = [];
  18246. const literal = [];
  18247. properties.forEach(prop => {
  18248. const i18n = i18nPropsMeta[prop.name];
  18249. if (prop.isLiteral) {
  18250. literal.push(new TextAttribute(prop.name, prop.expression.source || '', prop.sourceSpan, prop.keySpan, prop.valueSpan, i18n));
  18251. }
  18252. else {
  18253. // Note that validation is skipped and property mapping is disabled
  18254. // due to the fact that we need to make sure a given prop is not an
  18255. // input of a directive and directive matching happens at runtime.
  18256. const bep = this.bindingParser.createBoundElementProperty(elementName, prop, /* skipValidation */ true, /* mapPropertyName */ false);
  18257. bound.push(BoundAttribute.fromBoundElementProperty(bep, i18n));
  18258. }
  18259. });
  18260. return { bound, literal };
  18261. }
  18262. parseAttribute(isTemplateElement, attribute, matchableAttributes, parsedProperties, boundEvents, variables, references) {
  18263. const name = normalizeAttributeName(attribute.name);
  18264. const value = attribute.value;
  18265. const srcSpan = attribute.sourceSpan;
  18266. const absoluteOffset = attribute.valueSpan ? attribute.valueSpan.start.offset : srcSpan.start.offset;
  18267. function createKeySpan(srcSpan, prefix, identifier) {
  18268. // We need to adjust the start location for the keySpan to account for the removed 'data-'
  18269. // prefix from `normalizeAttributeName`.
  18270. const normalizationAdjustment = attribute.name.length - name.length;
  18271. const keySpanStart = srcSpan.start.moveBy(prefix.length + normalizationAdjustment);
  18272. const keySpanEnd = keySpanStart.moveBy(identifier.length);
  18273. return new ParseSourceSpan(keySpanStart, keySpanEnd, keySpanStart, identifier);
  18274. }
  18275. const bindParts = name.match(BIND_NAME_REGEXP);
  18276. if (bindParts) {
  18277. if (bindParts[KW_BIND_IDX] != null) {
  18278. const identifier = bindParts[IDENT_KW_IDX];
  18279. const keySpan = createKeySpan(srcSpan, bindParts[KW_BIND_IDX], identifier);
  18280. this.bindingParser.parsePropertyBinding(identifier, value, false, srcSpan, absoluteOffset, attribute.valueSpan, matchableAttributes, parsedProperties, keySpan);
  18281. }
  18282. else if (bindParts[KW_LET_IDX]) {
  18283. if (isTemplateElement) {
  18284. const identifier = bindParts[IDENT_KW_IDX];
  18285. const keySpan = createKeySpan(srcSpan, bindParts[KW_LET_IDX], identifier);
  18286. this.parseVariable(identifier, value, srcSpan, keySpan, attribute.valueSpan, variables);
  18287. }
  18288. else {
  18289. this.reportError(`"let-" is only supported on ng-template elements.`, srcSpan);
  18290. }
  18291. }
  18292. else if (bindParts[KW_REF_IDX]) {
  18293. const identifier = bindParts[IDENT_KW_IDX];
  18294. const keySpan = createKeySpan(srcSpan, bindParts[KW_REF_IDX], identifier);
  18295. this.parseReference(identifier, value, srcSpan, keySpan, attribute.valueSpan, references);
  18296. }
  18297. else if (bindParts[KW_ON_IDX]) {
  18298. const events = [];
  18299. const identifier = bindParts[IDENT_KW_IDX];
  18300. const keySpan = createKeySpan(srcSpan, bindParts[KW_ON_IDX], identifier);
  18301. this.bindingParser.parseEvent(identifier, value, /* isAssignmentEvent */ false, srcSpan, attribute.valueSpan || srcSpan, matchableAttributes, events, keySpan);
  18302. addEvents(events, boundEvents);
  18303. }
  18304. else if (bindParts[KW_BINDON_IDX]) {
  18305. const identifier = bindParts[IDENT_KW_IDX];
  18306. const keySpan = createKeySpan(srcSpan, bindParts[KW_BINDON_IDX], identifier);
  18307. this.bindingParser.parsePropertyBinding(identifier, value, false, srcSpan, absoluteOffset, attribute.valueSpan, matchableAttributes, parsedProperties, keySpan);
  18308. this.parseAssignmentEvent(identifier, value, srcSpan, attribute.valueSpan, matchableAttributes, boundEvents, keySpan);
  18309. }
  18310. else if (bindParts[KW_AT_IDX]) {
  18311. const keySpan = createKeySpan(srcSpan, '', name);
  18312. this.bindingParser.parseLiteralAttr(name, value, srcSpan, absoluteOffset, attribute.valueSpan, matchableAttributes, parsedProperties, keySpan);
  18313. }
  18314. return true;
  18315. }
  18316. // We didn't see a kw-prefixed property binding, but we have not yet checked
  18317. // for the []/()/[()] syntax.
  18318. let delims = null;
  18319. if (name.startsWith(BINDING_DELIMS.BANANA_BOX.start)) {
  18320. delims = BINDING_DELIMS.BANANA_BOX;
  18321. }
  18322. else if (name.startsWith(BINDING_DELIMS.PROPERTY.start)) {
  18323. delims = BINDING_DELIMS.PROPERTY;
  18324. }
  18325. else if (name.startsWith(BINDING_DELIMS.EVENT.start)) {
  18326. delims = BINDING_DELIMS.EVENT;
  18327. }
  18328. if (delims !== null &&
  18329. // NOTE: older versions of the parser would match a start/end delimited
  18330. // binding iff the property name was terminated by the ending delimiter
  18331. // and the identifier in the binding was non-empty.
  18332. // TODO(ayazhafiz): update this to handle malformed bindings.
  18333. name.endsWith(delims.end) && name.length > delims.start.length + delims.end.length) {
  18334. const identifier = name.substring(delims.start.length, name.length - delims.end.length);
  18335. const keySpan = createKeySpan(srcSpan, delims.start, identifier);
  18336. if (delims.start === BINDING_DELIMS.BANANA_BOX.start) {
  18337. this.bindingParser.parsePropertyBinding(identifier, value, false, srcSpan, absoluteOffset, attribute.valueSpan, matchableAttributes, parsedProperties, keySpan);
  18338. this.parseAssignmentEvent(identifier, value, srcSpan, attribute.valueSpan, matchableAttributes, boundEvents, keySpan);
  18339. }
  18340. else if (delims.start === BINDING_DELIMS.PROPERTY.start) {
  18341. this.bindingParser.parsePropertyBinding(identifier, value, false, srcSpan, absoluteOffset, attribute.valueSpan, matchableAttributes, parsedProperties, keySpan);
  18342. }
  18343. else {
  18344. const events = [];
  18345. this.bindingParser.parseEvent(identifier, value, /* isAssignmentEvent */ false, srcSpan, attribute.valueSpan || srcSpan, matchableAttributes, events, keySpan);
  18346. addEvents(events, boundEvents);
  18347. }
  18348. return true;
  18349. }
  18350. // No explicit binding found.
  18351. const keySpan = createKeySpan(srcSpan, '' /* prefix */, name);
  18352. const hasBinding = this.bindingParser.parsePropertyInterpolation(name, value, srcSpan, attribute.valueSpan, matchableAttributes, parsedProperties, keySpan, attribute.valueTokens ?? null);
  18353. return hasBinding;
  18354. }
  18355. _visitTextWithInterpolation(value, sourceSpan, interpolatedTokens, i18n) {
  18356. const valueNoNgsp = replaceNgsp(value);
  18357. const expr = this.bindingParser.parseInterpolation(valueNoNgsp, sourceSpan, interpolatedTokens);
  18358. return expr ? new BoundText(expr, sourceSpan, i18n) : new Text$3(valueNoNgsp, sourceSpan);
  18359. }
  18360. parseVariable(identifier, value, sourceSpan, keySpan, valueSpan, variables) {
  18361. if (identifier.indexOf('-') > -1) {
  18362. this.reportError(`"-" is not allowed in variable names`, sourceSpan);
  18363. }
  18364. else if (identifier.length === 0) {
  18365. this.reportError(`Variable does not have a name`, sourceSpan);
  18366. }
  18367. variables.push(new Variable(identifier, value, sourceSpan, keySpan, valueSpan));
  18368. }
  18369. parseReference(identifier, value, sourceSpan, keySpan, valueSpan, references) {
  18370. if (identifier.indexOf('-') > -1) {
  18371. this.reportError(`"-" is not allowed in reference names`, sourceSpan);
  18372. }
  18373. else if (identifier.length === 0) {
  18374. this.reportError(`Reference does not have a name`, sourceSpan);
  18375. }
  18376. else if (references.some(reference => reference.name === identifier)) {
  18377. this.reportError(`Reference "#${identifier}" is defined more than once`, sourceSpan);
  18378. }
  18379. references.push(new Reference(identifier, value, sourceSpan, keySpan, valueSpan));
  18380. }
  18381. parseAssignmentEvent(name, expression, sourceSpan, valueSpan, targetMatchableAttrs, boundEvents, keySpan) {
  18382. const events = [];
  18383. this.bindingParser.parseEvent(`${name}Change`, `${expression} =$event`, /* isAssignmentEvent */ true, sourceSpan, valueSpan || sourceSpan, targetMatchableAttrs, events, keySpan);
  18384. addEvents(events, boundEvents);
  18385. }
  18386. reportError(message, sourceSpan, level = ParseErrorLevel.ERROR) {
  18387. this.errors.push(new ParseError(sourceSpan, message, level));
  18388. }
  18389. }
  18390. class NonBindableVisitor {
  18391. visitElement(ast) {
  18392. const preparsedElement = preparseElement(ast);
  18393. if (preparsedElement.type === PreparsedElementType.SCRIPT ||
  18394. preparsedElement.type === PreparsedElementType.STYLE ||
  18395. preparsedElement.type === PreparsedElementType.STYLESHEET) {
  18396. // Skipping <script> for security reasons
  18397. // Skipping <style> and stylesheets as we already processed them
  18398. // in the StyleCompiler
  18399. return null;
  18400. }
  18401. const children = visitAll(this, ast.children, null);
  18402. return new Element$1(ast.name, visitAll(this, ast.attrs),
  18403. /* inputs */ [], /* outputs */ [], children, /* references */ [], ast.sourceSpan, ast.startSourceSpan, ast.endSourceSpan);
  18404. }
  18405. visitComment(comment) {
  18406. return null;
  18407. }
  18408. visitAttribute(attribute) {
  18409. return new TextAttribute(attribute.name, attribute.value, attribute.sourceSpan, attribute.keySpan, attribute.valueSpan, attribute.i18n);
  18410. }
  18411. visitText(text) {
  18412. return new Text$3(text.value, text.sourceSpan);
  18413. }
  18414. visitExpansion(expansion) {
  18415. return null;
  18416. }
  18417. visitExpansionCase(expansionCase) {
  18418. return null;
  18419. }
  18420. }
  18421. const NON_BINDABLE_VISITOR = new NonBindableVisitor();
  18422. function normalizeAttributeName(attrName) {
  18423. return /^data-/i.test(attrName) ? attrName.substring(5) : attrName;
  18424. }
  18425. function addEvents(events, boundEvents) {
  18426. boundEvents.push(...events.map(e => BoundEvent.fromParsedEvent(e)));
  18427. }
  18428. function isEmptyTextNode(node) {
  18429. return node instanceof Text && node.value.trim().length == 0;
  18430. }
  18431. function isCommentNode(node) {
  18432. return node instanceof Comment;
  18433. }
  18434. function textContents(node) {
  18435. if (node.children.length !== 1 || !(node.children[0] instanceof Text)) {
  18436. return null;
  18437. }
  18438. else {
  18439. return node.children[0].value;
  18440. }
  18441. }
  18442. var TagType;
  18443. (function (TagType) {
  18444. TagType[TagType["ELEMENT"] = 0] = "ELEMENT";
  18445. TagType[TagType["TEMPLATE"] = 1] = "TEMPLATE";
  18446. })(TagType || (TagType = {}));
  18447. /**
  18448. * Generates an object that is used as a shared state between parent and all child contexts.
  18449. */
  18450. function setupRegistry() {
  18451. return { getUniqueId: getSeqNumberGenerator(), icus: new Map() };
  18452. }
  18453. /**
  18454. * I18nContext is a helper class which keeps track of all i18n-related aspects
  18455. * (accumulates placeholders, bindings, etc) between i18nStart and i18nEnd instructions.
  18456. *
  18457. * When we enter a nested template, the top-level context is being passed down
  18458. * to the nested component, which uses this context to generate a child instance
  18459. * of I18nContext class (to handle nested template) and at the end, reconciles it back
  18460. * with the parent context.
  18461. *
  18462. * @param index Instruction index of i18nStart, which initiates this context
  18463. * @param ref Reference to a translation const that represents the content if thus context
  18464. * @param level Nesting level defined for child contexts
  18465. * @param templateIndex Instruction index of a template which this context belongs to
  18466. * @param meta Meta information (id, meaning, description, etc) associated with this context
  18467. */
  18468. class I18nContext {
  18469. constructor(index, ref, level = 0, templateIndex = null, meta, registry) {
  18470. this.index = index;
  18471. this.ref = ref;
  18472. this.level = level;
  18473. this.templateIndex = templateIndex;
  18474. this.meta = meta;
  18475. this.registry = registry;
  18476. this.bindings = new Set();
  18477. this.placeholders = new Map();
  18478. this.isEmitted = false;
  18479. this._unresolvedCtxCount = 0;
  18480. this._registry = registry || setupRegistry();
  18481. this.id = this._registry.getUniqueId();
  18482. }
  18483. appendTag(type, node, index, closed) {
  18484. if (node.isVoid && closed) {
  18485. return; // ignore "close" for void tags
  18486. }
  18487. const ph = node.isVoid || !closed ? node.startName : node.closeName;
  18488. const content = { type, index, ctx: this.id, isVoid: node.isVoid, closed };
  18489. updatePlaceholderMap(this.placeholders, ph, content);
  18490. }
  18491. get icus() {
  18492. return this._registry.icus;
  18493. }
  18494. get isRoot() {
  18495. return this.level === 0;
  18496. }
  18497. get isResolved() {
  18498. return this._unresolvedCtxCount === 0;
  18499. }
  18500. getSerializedPlaceholders() {
  18501. const result = new Map();
  18502. this.placeholders.forEach((values, key) => result.set(key, values.map(serializePlaceholderValue)));
  18503. return result;
  18504. }
  18505. // public API to accumulate i18n-related content
  18506. appendBinding(binding) {
  18507. this.bindings.add(binding);
  18508. }
  18509. appendIcu(name, ref) {
  18510. updatePlaceholderMap(this._registry.icus, name, ref);
  18511. }
  18512. appendBoundText(node) {
  18513. const phs = assembleBoundTextPlaceholders(node, this.bindings.size, this.id);
  18514. phs.forEach((values, key) => updatePlaceholderMap(this.placeholders, key, ...values));
  18515. }
  18516. appendTemplate(node, index) {
  18517. // add open and close tags at the same time,
  18518. // since we process nested templates separately
  18519. this.appendTag(TagType.TEMPLATE, node, index, false);
  18520. this.appendTag(TagType.TEMPLATE, node, index, true);
  18521. this._unresolvedCtxCount++;
  18522. }
  18523. appendElement(node, index, closed) {
  18524. this.appendTag(TagType.ELEMENT, node, index, closed);
  18525. }
  18526. appendProjection(node, index) {
  18527. // Add open and close tags at the same time, since `<ng-content>` has no content,
  18528. // so when we come across `<ng-content>` we can register both open and close tags.
  18529. // Note: runtime i18n logic doesn't distinguish `<ng-content>` tag placeholders and
  18530. // regular element tag placeholders, so we generate element placeholders for both types.
  18531. this.appendTag(TagType.ELEMENT, node, index, false);
  18532. this.appendTag(TagType.ELEMENT, node, index, true);
  18533. }
  18534. /**
  18535. * Generates an instance of a child context based on the root one,
  18536. * when we enter a nested template within I18n section.
  18537. *
  18538. * @param index Instruction index of corresponding i18nStart, which initiates this context
  18539. * @param templateIndex Instruction index of a template which this context belongs to
  18540. * @param meta Meta information (id, meaning, description, etc) associated with this context
  18541. *
  18542. * @returns I18nContext instance
  18543. */
  18544. forkChildContext(index, templateIndex, meta) {
  18545. return new I18nContext(index, this.ref, this.level + 1, templateIndex, meta, this._registry);
  18546. }
  18547. /**
  18548. * Reconciles child context into parent one once the end of the i18n block is reached (i18nEnd).
  18549. *
  18550. * @param context Child I18nContext instance to be reconciled with parent context.
  18551. */
  18552. reconcileChildContext(context) {
  18553. // set the right context id for open and close
  18554. // template tags, so we can use it as sub-block ids
  18555. ['start', 'close'].forEach((op) => {
  18556. const key = context.meta[`${op}Name`];
  18557. const phs = this.placeholders.get(key) || [];
  18558. const tag = phs.find(findTemplateFn(this.id, context.templateIndex));
  18559. if (tag) {
  18560. tag.ctx = context.id;
  18561. }
  18562. });
  18563. // reconcile placeholders
  18564. const childPhs = context.placeholders;
  18565. childPhs.forEach((values, key) => {
  18566. const phs = this.placeholders.get(key);
  18567. if (!phs) {
  18568. this.placeholders.set(key, values);
  18569. return;
  18570. }
  18571. // try to find matching template...
  18572. const tmplIdx = phs.findIndex(findTemplateFn(context.id, context.templateIndex));
  18573. if (tmplIdx >= 0) {
  18574. // ... if found - replace it with nested template content
  18575. const isCloseTag = key.startsWith('CLOSE');
  18576. const isTemplateTag = key.endsWith('NG-TEMPLATE');
  18577. if (isTemplateTag) {
  18578. // current template's content is placed before or after
  18579. // parent template tag, depending on the open/close attribute
  18580. phs.splice(tmplIdx + (isCloseTag ? 0 : 1), 0, ...values);
  18581. }
  18582. else {
  18583. const idx = isCloseTag ? values.length - 1 : 0;
  18584. values[idx].tmpl = phs[tmplIdx];
  18585. phs.splice(tmplIdx, 1, ...values);
  18586. }
  18587. }
  18588. else {
  18589. // ... otherwise just append content to placeholder value
  18590. phs.push(...values);
  18591. }
  18592. this.placeholders.set(key, phs);
  18593. });
  18594. this._unresolvedCtxCount--;
  18595. }
  18596. }
  18597. //
  18598. // Helper methods
  18599. //
  18600. function wrap(symbol, index, contextId, closed) {
  18601. const state = closed ? '/' : '';
  18602. return wrapI18nPlaceholder(`${state}${symbol}${index}`, contextId);
  18603. }
  18604. function wrapTag(symbol, { index, ctx, isVoid }, closed) {
  18605. return isVoid ? wrap(symbol, index, ctx) + wrap(symbol, index, ctx, true) :
  18606. wrap(symbol, index, ctx, closed);
  18607. }
  18608. function findTemplateFn(ctx, templateIndex) {
  18609. return (token) => typeof token === 'object' && token.type === TagType.TEMPLATE &&
  18610. token.index === templateIndex && token.ctx === ctx;
  18611. }
  18612. function serializePlaceholderValue(value) {
  18613. const element = (data, closed) => wrapTag('#', data, closed);
  18614. const template = (data, closed) => wrapTag('*', data, closed);
  18615. switch (value.type) {
  18616. case TagType.ELEMENT:
  18617. // close element tag
  18618. if (value.closed) {
  18619. return element(value, true) + (value.tmpl ? template(value.tmpl, true) : '');
  18620. }
  18621. // open element tag that also initiates a template
  18622. if (value.tmpl) {
  18623. return template(value.tmpl) + element(value) +
  18624. (value.isVoid ? template(value.tmpl, true) : '');
  18625. }
  18626. return element(value);
  18627. case TagType.TEMPLATE:
  18628. return template(value, value.closed);
  18629. default:
  18630. return value;
  18631. }
  18632. }
  18633. class IcuSerializerVisitor {
  18634. visitText(text) {
  18635. return text.value;
  18636. }
  18637. visitContainer(container) {
  18638. return container.children.map(child => child.visit(this)).join('');
  18639. }
  18640. visitIcu(icu) {
  18641. const strCases = Object.keys(icu.cases).map((k) => `${k} {${icu.cases[k].visit(this)}}`);
  18642. const result = `{${icu.expressionPlaceholder}, ${icu.type}, ${strCases.join(' ')}}`;
  18643. return result;
  18644. }
  18645. visitTagPlaceholder(ph) {
  18646. return ph.isVoid ?
  18647. this.formatPh(ph.startName) :
  18648. `${this.formatPh(ph.startName)}${ph.children.map(child => child.visit(this)).join('')}${this.formatPh(ph.closeName)}`;
  18649. }
  18650. visitPlaceholder(ph) {
  18651. return this.formatPh(ph.name);
  18652. }
  18653. visitIcuPlaceholder(ph, context) {
  18654. return this.formatPh(ph.name);
  18655. }
  18656. formatPh(value) {
  18657. return `{${formatI18nPlaceholderName(value, /* useCamelCase */ false)}}`;
  18658. }
  18659. }
  18660. const serializer = new IcuSerializerVisitor();
  18661. function serializeIcuNode(icu) {
  18662. return icu.visit(serializer);
  18663. }
  18664. const TAG_TO_PLACEHOLDER_NAMES = {
  18665. 'A': 'LINK',
  18666. 'B': 'BOLD_TEXT',
  18667. 'BR': 'LINE_BREAK',
  18668. 'EM': 'EMPHASISED_TEXT',
  18669. 'H1': 'HEADING_LEVEL1',
  18670. 'H2': 'HEADING_LEVEL2',
  18671. 'H3': 'HEADING_LEVEL3',
  18672. 'H4': 'HEADING_LEVEL4',
  18673. 'H5': 'HEADING_LEVEL5',
  18674. 'H6': 'HEADING_LEVEL6',
  18675. 'HR': 'HORIZONTAL_RULE',
  18676. 'I': 'ITALIC_TEXT',
  18677. 'LI': 'LIST_ITEM',
  18678. 'LINK': 'MEDIA_LINK',
  18679. 'OL': 'ORDERED_LIST',
  18680. 'P': 'PARAGRAPH',
  18681. 'Q': 'QUOTATION',
  18682. 'S': 'STRIKETHROUGH_TEXT',
  18683. 'SMALL': 'SMALL_TEXT',
  18684. 'SUB': 'SUBSTRIPT',
  18685. 'SUP': 'SUPERSCRIPT',
  18686. 'TBODY': 'TABLE_BODY',
  18687. 'TD': 'TABLE_CELL',
  18688. 'TFOOT': 'TABLE_FOOTER',
  18689. 'TH': 'TABLE_HEADER_CELL',
  18690. 'THEAD': 'TABLE_HEADER',
  18691. 'TR': 'TABLE_ROW',
  18692. 'TT': 'MONOSPACED_TEXT',
  18693. 'U': 'UNDERLINED_TEXT',
  18694. 'UL': 'UNORDERED_LIST',
  18695. };
  18696. /**
  18697. * Creates unique names for placeholder with different content.
  18698. *
  18699. * Returns the same placeholder name when the content is identical.
  18700. */
  18701. class PlaceholderRegistry {
  18702. constructor() {
  18703. // Count the occurrence of the base name top generate a unique name
  18704. this._placeHolderNameCounts = {};
  18705. // Maps signature to placeholder names
  18706. this._signatureToName = {};
  18707. }
  18708. getStartTagPlaceholderName(tag, attrs, isVoid) {
  18709. const signature = this._hashTag(tag, attrs, isVoid);
  18710. if (this._signatureToName[signature]) {
  18711. return this._signatureToName[signature];
  18712. }
  18713. const upperTag = tag.toUpperCase();
  18714. const baseName = TAG_TO_PLACEHOLDER_NAMES[upperTag] || `TAG_${upperTag}`;
  18715. const name = this._generateUniqueName(isVoid ? baseName : `START_${baseName}`);
  18716. this._signatureToName[signature] = name;
  18717. return name;
  18718. }
  18719. getCloseTagPlaceholderName(tag) {
  18720. const signature = this._hashClosingTag(tag);
  18721. if (this._signatureToName[signature]) {
  18722. return this._signatureToName[signature];
  18723. }
  18724. const upperTag = tag.toUpperCase();
  18725. const baseName = TAG_TO_PLACEHOLDER_NAMES[upperTag] || `TAG_${upperTag}`;
  18726. const name = this._generateUniqueName(`CLOSE_${baseName}`);
  18727. this._signatureToName[signature] = name;
  18728. return name;
  18729. }
  18730. getPlaceholderName(name, content) {
  18731. const upperName = name.toUpperCase();
  18732. const signature = `PH: ${upperName}=${content}`;
  18733. if (this._signatureToName[signature]) {
  18734. return this._signatureToName[signature];
  18735. }
  18736. const uniqueName = this._generateUniqueName(upperName);
  18737. this._signatureToName[signature] = uniqueName;
  18738. return uniqueName;
  18739. }
  18740. getUniquePlaceholder(name) {
  18741. return this._generateUniqueName(name.toUpperCase());
  18742. }
  18743. // Generate a hash for a tag - does not take attribute order into account
  18744. _hashTag(tag, attrs, isVoid) {
  18745. const start = `<${tag}`;
  18746. const strAttrs = Object.keys(attrs).sort().map((name) => ` ${name}=${attrs[name]}`).join('');
  18747. const end = isVoid ? '/>' : `></${tag}>`;
  18748. return start + strAttrs + end;
  18749. }
  18750. _hashClosingTag(tag) {
  18751. return this._hashTag(`/${tag}`, {}, false);
  18752. }
  18753. _generateUniqueName(base) {
  18754. const seen = this._placeHolderNameCounts.hasOwnProperty(base);
  18755. if (!seen) {
  18756. this._placeHolderNameCounts[base] = 1;
  18757. return base;
  18758. }
  18759. const id = this._placeHolderNameCounts[base];
  18760. this._placeHolderNameCounts[base] = id + 1;
  18761. return `${base}_${id}`;
  18762. }
  18763. }
  18764. const _expParser = new Parser$1(new Lexer());
  18765. /**
  18766. * Returns a function converting html nodes to an i18n Message given an interpolationConfig
  18767. */
  18768. function createI18nMessageFactory(interpolationConfig) {
  18769. const visitor = new _I18nVisitor(_expParser, interpolationConfig);
  18770. return (nodes, meaning, description, customId, visitNodeFn) => visitor.toI18nMessage(nodes, meaning, description, customId, visitNodeFn);
  18771. }
  18772. function noopVisitNodeFn(_html, i18n) {
  18773. return i18n;
  18774. }
  18775. class _I18nVisitor {
  18776. constructor(_expressionParser, _interpolationConfig) {
  18777. this._expressionParser = _expressionParser;
  18778. this._interpolationConfig = _interpolationConfig;
  18779. }
  18780. toI18nMessage(nodes, meaning = '', description = '', customId = '', visitNodeFn) {
  18781. const context = {
  18782. isIcu: nodes.length == 1 && nodes[0] instanceof Expansion,
  18783. icuDepth: 0,
  18784. placeholderRegistry: new PlaceholderRegistry(),
  18785. placeholderToContent: {},
  18786. placeholderToMessage: {},
  18787. visitNodeFn: visitNodeFn || noopVisitNodeFn,
  18788. };
  18789. const i18nodes = visitAll(this, nodes, context);
  18790. return new Message(i18nodes, context.placeholderToContent, context.placeholderToMessage, meaning, description, customId);
  18791. }
  18792. visitElement(el, context) {
  18793. const children = visitAll(this, el.children, context);
  18794. const attrs = {};
  18795. el.attrs.forEach(attr => {
  18796. // Do not visit the attributes, translatable ones are top-level ASTs
  18797. attrs[attr.name] = attr.value;
  18798. });
  18799. const isVoid = getHtmlTagDefinition(el.name).isVoid;
  18800. const startPhName = context.placeholderRegistry.getStartTagPlaceholderName(el.name, attrs, isVoid);
  18801. context.placeholderToContent[startPhName] = {
  18802. text: el.startSourceSpan.toString(),
  18803. sourceSpan: el.startSourceSpan,
  18804. };
  18805. let closePhName = '';
  18806. if (!isVoid) {
  18807. closePhName = context.placeholderRegistry.getCloseTagPlaceholderName(el.name);
  18808. context.placeholderToContent[closePhName] = {
  18809. text: `</${el.name}>`,
  18810. sourceSpan: el.endSourceSpan ?? el.sourceSpan,
  18811. };
  18812. }
  18813. const node = new TagPlaceholder(el.name, attrs, startPhName, closePhName, children, isVoid, el.sourceSpan, el.startSourceSpan, el.endSourceSpan);
  18814. return context.visitNodeFn(el, node);
  18815. }
  18816. visitAttribute(attribute, context) {
  18817. const node = attribute.valueTokens === undefined || attribute.valueTokens.length === 1 ?
  18818. new Text$2(attribute.value, attribute.valueSpan || attribute.sourceSpan) :
  18819. this._visitTextWithInterpolation(attribute.valueTokens, attribute.valueSpan || attribute.sourceSpan, context, attribute.i18n);
  18820. return context.visitNodeFn(attribute, node);
  18821. }
  18822. visitText(text, context) {
  18823. const node = text.tokens.length === 1 ?
  18824. new Text$2(text.value, text.sourceSpan) :
  18825. this._visitTextWithInterpolation(text.tokens, text.sourceSpan, context, text.i18n);
  18826. return context.visitNodeFn(text, node);
  18827. }
  18828. visitComment(comment, context) {
  18829. return null;
  18830. }
  18831. visitExpansion(icu, context) {
  18832. context.icuDepth++;
  18833. const i18nIcuCases = {};
  18834. const i18nIcu = new Icu(icu.switchValue, icu.type, i18nIcuCases, icu.sourceSpan);
  18835. icu.cases.forEach((caze) => {
  18836. i18nIcuCases[caze.value] = new Container(caze.expression.map((node) => node.visit(this, context)), caze.expSourceSpan);
  18837. });
  18838. context.icuDepth--;
  18839. if (context.isIcu || context.icuDepth > 0) {
  18840. // Returns an ICU node when:
  18841. // - the message (vs a part of the message) is an ICU message, or
  18842. // - the ICU message is nested.
  18843. const expPh = context.placeholderRegistry.getUniquePlaceholder(`VAR_${icu.type}`);
  18844. i18nIcu.expressionPlaceholder = expPh;
  18845. context.placeholderToContent[expPh] = {
  18846. text: icu.switchValue,
  18847. sourceSpan: icu.switchValueSourceSpan,
  18848. };
  18849. return context.visitNodeFn(icu, i18nIcu);
  18850. }
  18851. // Else returns a placeholder
  18852. // ICU placeholders should not be replaced with their original content but with the their
  18853. // translations.
  18854. // TODO(vicb): add a html.Node -> i18n.Message cache to avoid having to re-create the msg
  18855. const phName = context.placeholderRegistry.getPlaceholderName('ICU', icu.sourceSpan.toString());
  18856. context.placeholderToMessage[phName] = this.toI18nMessage([icu], '', '', '', undefined);
  18857. const node = new IcuPlaceholder(i18nIcu, phName, icu.sourceSpan);
  18858. return context.visitNodeFn(icu, node);
  18859. }
  18860. visitExpansionCase(_icuCase, _context) {
  18861. throw new Error('Unreachable code');
  18862. }
  18863. /**
  18864. * Convert, text and interpolated tokens up into text and placeholder pieces.
  18865. *
  18866. * @param tokens The text and interpolated tokens.
  18867. * @param sourceSpan The span of the whole of the `text` string.
  18868. * @param context The current context of the visitor, used to compute and store placeholders.
  18869. * @param previousI18n Any i18n metadata associated with this `text` from a previous pass.
  18870. */
  18871. _visitTextWithInterpolation(tokens, sourceSpan, context, previousI18n) {
  18872. // Return a sequence of `Text` and `Placeholder` nodes grouped in a `Container`.
  18873. const nodes = [];
  18874. // We will only create a container if there are actually interpolations,
  18875. // so this flag tracks that.
  18876. let hasInterpolation = false;
  18877. for (const token of tokens) {
  18878. switch (token.type) {
  18879. case 8 /* TokenType.INTERPOLATION */:
  18880. case 17 /* TokenType.ATTR_VALUE_INTERPOLATION */:
  18881. hasInterpolation = true;
  18882. const expression = token.parts[1];
  18883. const baseName = extractPlaceholderName(expression) || 'INTERPOLATION';
  18884. const phName = context.placeholderRegistry.getPlaceholderName(baseName, expression);
  18885. context.placeholderToContent[phName] = {
  18886. text: token.parts.join(''),
  18887. sourceSpan: token.sourceSpan
  18888. };
  18889. nodes.push(new Placeholder(expression, phName, token.sourceSpan));
  18890. break;
  18891. default:
  18892. if (token.parts[0].length > 0) {
  18893. // This token is text or an encoded entity.
  18894. // If it is following on from a previous text node then merge it into that node
  18895. // Otherwise, if it is following an interpolation, then add a new node.
  18896. const previous = nodes[nodes.length - 1];
  18897. if (previous instanceof Text$2) {
  18898. previous.value += token.parts[0];
  18899. previous.sourceSpan = new ParseSourceSpan(previous.sourceSpan.start, token.sourceSpan.end, previous.sourceSpan.fullStart, previous.sourceSpan.details);
  18900. }
  18901. else {
  18902. nodes.push(new Text$2(token.parts[0], token.sourceSpan));
  18903. }
  18904. }
  18905. break;
  18906. }
  18907. }
  18908. if (hasInterpolation) {
  18909. // Whitespace removal may have invalidated the interpolation source-spans.
  18910. reusePreviousSourceSpans(nodes, previousI18n);
  18911. return new Container(nodes, sourceSpan);
  18912. }
  18913. else {
  18914. return nodes[0];
  18915. }
  18916. }
  18917. }
  18918. /**
  18919. * Re-use the source-spans from `previousI18n` metadata for the `nodes`.
  18920. *
  18921. * Whitespace removal can invalidate the source-spans of interpolation nodes, so we
  18922. * reuse the source-span stored from a previous pass before the whitespace was removed.
  18923. *
  18924. * @param nodes The `Text` and `Placeholder` nodes to be processed.
  18925. * @param previousI18n Any i18n metadata for these `nodes` stored from a previous pass.
  18926. */
  18927. function reusePreviousSourceSpans(nodes, previousI18n) {
  18928. if (previousI18n instanceof Message) {
  18929. // The `previousI18n` is an i18n `Message`, so we are processing an `Attribute` with i18n
  18930. // metadata. The `Message` should consist only of a single `Container` that contains the
  18931. // parts (`Text` and `Placeholder`) to process.
  18932. assertSingleContainerMessage(previousI18n);
  18933. previousI18n = previousI18n.nodes[0];
  18934. }
  18935. if (previousI18n instanceof Container) {
  18936. // The `previousI18n` is a `Container`, which means that this is a second i18n extraction pass
  18937. // after whitespace has been removed from the AST nodes.
  18938. assertEquivalentNodes(previousI18n.children, nodes);
  18939. // Reuse the source-spans from the first pass.
  18940. for (let i = 0; i < nodes.length; i++) {
  18941. nodes[i].sourceSpan = previousI18n.children[i].sourceSpan;
  18942. }
  18943. }
  18944. }
  18945. /**
  18946. * Asserts that the `message` contains exactly one `Container` node.
  18947. */
  18948. function assertSingleContainerMessage(message) {
  18949. const nodes = message.nodes;
  18950. if (nodes.length !== 1 || !(nodes[0] instanceof Container)) {
  18951. throw new Error('Unexpected previous i18n message - expected it to consist of only a single `Container` node.');
  18952. }
  18953. }
  18954. /**
  18955. * Asserts that the `previousNodes` and `node` collections have the same number of elements and
  18956. * corresponding elements have the same node type.
  18957. */
  18958. function assertEquivalentNodes(previousNodes, nodes) {
  18959. if (previousNodes.length !== nodes.length) {
  18960. throw new Error('The number of i18n message children changed between first and second pass.');
  18961. }
  18962. if (previousNodes.some((node, i) => nodes[i].constructor !== node.constructor)) {
  18963. throw new Error('The types of the i18n message children changed between first and second pass.');
  18964. }
  18965. }
  18966. const _CUSTOM_PH_EXP = /\/\/[\s\S]*i18n[\s\S]*\([\s\S]*ph[\s\S]*=[\s\S]*("|')([\s\S]*?)\1[\s\S]*\)/g;
  18967. function extractPlaceholderName(input) {
  18968. return input.split(_CUSTOM_PH_EXP)[2];
  18969. }
  18970. /**
  18971. * An i18n error.
  18972. */
  18973. class I18nError extends ParseError {
  18974. constructor(span, msg) {
  18975. super(span, msg);
  18976. }
  18977. }
  18978. const setI18nRefs = (htmlNode, i18nNode) => {
  18979. if (htmlNode instanceof NodeWithI18n) {
  18980. if (i18nNode instanceof IcuPlaceholder && htmlNode.i18n instanceof Message) {
  18981. // This html node represents an ICU but this is a second processing pass, and the legacy id
  18982. // was computed in the previous pass and stored in the `i18n` property as a message.
  18983. // We are about to wipe out that property so capture the previous message to be reused when
  18984. // generating the message for this ICU later. See `_generateI18nMessage()`.
  18985. i18nNode.previousMessage = htmlNode.i18n;
  18986. }
  18987. htmlNode.i18n = i18nNode;
  18988. }
  18989. return i18nNode;
  18990. };
  18991. /**
  18992. * This visitor walks over HTML parse tree and converts information stored in
  18993. * i18n-related attributes ("i18n" and "i18n-*") into i18n meta object that is
  18994. * stored with other element's and attribute's information.
  18995. */
  18996. class I18nMetaVisitor {
  18997. constructor(interpolationConfig = DEFAULT_INTERPOLATION_CONFIG, keepI18nAttrs = false, enableI18nLegacyMessageIdFormat = false) {
  18998. this.interpolationConfig = interpolationConfig;
  18999. this.keepI18nAttrs = keepI18nAttrs;
  19000. this.enableI18nLegacyMessageIdFormat = enableI18nLegacyMessageIdFormat;
  19001. // whether visited nodes contain i18n information
  19002. this.hasI18nMeta = false;
  19003. this._errors = [];
  19004. }
  19005. _generateI18nMessage(nodes, meta = '', visitNodeFn) {
  19006. const { meaning, description, customId } = this._parseMetadata(meta);
  19007. const createI18nMessage = createI18nMessageFactory(this.interpolationConfig);
  19008. const message = createI18nMessage(nodes, meaning, description, customId, visitNodeFn);
  19009. this._setMessageId(message, meta);
  19010. this._setLegacyIds(message, meta);
  19011. return message;
  19012. }
  19013. visitAllWithErrors(nodes) {
  19014. const result = nodes.map(node => node.visit(this, null));
  19015. return new ParseTreeResult(result, this._errors);
  19016. }
  19017. visitElement(element) {
  19018. let message = undefined;
  19019. if (hasI18nAttrs(element)) {
  19020. this.hasI18nMeta = true;
  19021. const attrs = [];
  19022. const attrsMeta = {};
  19023. for (const attr of element.attrs) {
  19024. if (attr.name === I18N_ATTR) {
  19025. // root 'i18n' node attribute
  19026. const i18n = element.i18n || attr.value;
  19027. message = this._generateI18nMessage(element.children, i18n, setI18nRefs);
  19028. if (message.nodes.length === 0) {
  19029. // Ignore the message if it is empty.
  19030. message = undefined;
  19031. }
  19032. // Store the message on the element
  19033. element.i18n = message;
  19034. }
  19035. else if (attr.name.startsWith(I18N_ATTR_PREFIX)) {
  19036. // 'i18n-*' attributes
  19037. const name = attr.name.slice(I18N_ATTR_PREFIX.length);
  19038. if (isTrustedTypesSink(element.name, name)) {
  19039. this._reportError(attr, `Translating attribute '${name}' is disallowed for security reasons.`);
  19040. }
  19041. else {
  19042. attrsMeta[name] = attr.value;
  19043. }
  19044. }
  19045. else {
  19046. // non-i18n attributes
  19047. attrs.push(attr);
  19048. }
  19049. }
  19050. // set i18n meta for attributes
  19051. if (Object.keys(attrsMeta).length) {
  19052. for (const attr of attrs) {
  19053. const meta = attrsMeta[attr.name];
  19054. // do not create translation for empty attributes
  19055. if (meta !== undefined && attr.value) {
  19056. attr.i18n = this._generateI18nMessage([attr], attr.i18n || meta);
  19057. }
  19058. }
  19059. }
  19060. if (!this.keepI18nAttrs) {
  19061. // update element's attributes,
  19062. // keeping only non-i18n related ones
  19063. element.attrs = attrs;
  19064. }
  19065. }
  19066. visitAll(this, element.children, message);
  19067. return element;
  19068. }
  19069. visitExpansion(expansion, currentMessage) {
  19070. let message;
  19071. const meta = expansion.i18n;
  19072. this.hasI18nMeta = true;
  19073. if (meta instanceof IcuPlaceholder) {
  19074. // set ICU placeholder name (e.g. "ICU_1"),
  19075. // generated while processing root element contents,
  19076. // so we can reference it when we output translation
  19077. const name = meta.name;
  19078. message = this._generateI18nMessage([expansion], meta);
  19079. const icu = icuFromI18nMessage(message);
  19080. icu.name = name;
  19081. if (currentMessage !== null) {
  19082. // Also update the placeholderToMessage map with this new message
  19083. currentMessage.placeholderToMessage[name] = message;
  19084. }
  19085. }
  19086. else {
  19087. // ICU is a top level message, try to use metadata from container element if provided via
  19088. // `context` argument. Note: context may not be available for standalone ICUs (without
  19089. // wrapping element), so fallback to ICU metadata in this case.
  19090. message = this._generateI18nMessage([expansion], currentMessage || meta);
  19091. }
  19092. expansion.i18n = message;
  19093. return expansion;
  19094. }
  19095. visitText(text) {
  19096. return text;
  19097. }
  19098. visitAttribute(attribute) {
  19099. return attribute;
  19100. }
  19101. visitComment(comment) {
  19102. return comment;
  19103. }
  19104. visitExpansionCase(expansionCase) {
  19105. return expansionCase;
  19106. }
  19107. /**
  19108. * Parse the general form `meta` passed into extract the explicit metadata needed to create a
  19109. * `Message`.
  19110. *
  19111. * There are three possibilities for the `meta` variable
  19112. * 1) a string from an `i18n` template attribute: parse it to extract the metadata values.
  19113. * 2) a `Message` from a previous processing pass: reuse the metadata values in the message.
  19114. * 4) other: ignore this and just process the message metadata as normal
  19115. *
  19116. * @param meta the bucket that holds information about the message
  19117. * @returns the parsed metadata.
  19118. */
  19119. _parseMetadata(meta) {
  19120. return typeof meta === 'string' ? parseI18nMeta(meta) :
  19121. meta instanceof Message ? meta :
  19122. {};
  19123. }
  19124. /**
  19125. * Generate (or restore) message id if not specified already.
  19126. */
  19127. _setMessageId(message, meta) {
  19128. if (!message.id) {
  19129. message.id = meta instanceof Message && meta.id || decimalDigest(message);
  19130. }
  19131. }
  19132. /**
  19133. * Update the `message` with a `legacyId` if necessary.
  19134. *
  19135. * @param message the message whose legacy id should be set
  19136. * @param meta information about the message being processed
  19137. */
  19138. _setLegacyIds(message, meta) {
  19139. if (this.enableI18nLegacyMessageIdFormat) {
  19140. message.legacyIds = [computeDigest(message), computeDecimalDigest(message)];
  19141. }
  19142. else if (typeof meta !== 'string') {
  19143. // This occurs if we are doing the 2nd pass after whitespace removal (see `parseTemplate()` in
  19144. // `packages/compiler/src/render3/view/template.ts`).
  19145. // In that case we want to reuse the legacy message generated in the 1st pass (see
  19146. // `setI18nRefs()`).
  19147. const previousMessage = meta instanceof Message ? meta :
  19148. meta instanceof IcuPlaceholder ? meta.previousMessage :
  19149. undefined;
  19150. message.legacyIds = previousMessage ? previousMessage.legacyIds : [];
  19151. }
  19152. }
  19153. _reportError(node, msg) {
  19154. this._errors.push(new I18nError(node.sourceSpan, msg));
  19155. }
  19156. }
  19157. /** I18n separators for metadata **/
  19158. const I18N_MEANING_SEPARATOR = '|';
  19159. const I18N_ID_SEPARATOR = '@@';
  19160. /**
  19161. * Parses i18n metas like:
  19162. * - "@@id",
  19163. * - "description[@@id]",
  19164. * - "meaning|description[@@id]"
  19165. * and returns an object with parsed output.
  19166. *
  19167. * @param meta String that represents i18n meta
  19168. * @returns Object with id, meaning and description fields
  19169. */
  19170. function parseI18nMeta(meta = '') {
  19171. let customId;
  19172. let meaning;
  19173. let description;
  19174. meta = meta.trim();
  19175. if (meta) {
  19176. const idIndex = meta.indexOf(I18N_ID_SEPARATOR);
  19177. const descIndex = meta.indexOf(I18N_MEANING_SEPARATOR);
  19178. let meaningAndDesc;
  19179. [meaningAndDesc, customId] =
  19180. (idIndex > -1) ? [meta.slice(0, idIndex), meta.slice(idIndex + 2)] : [meta, ''];
  19181. [meaning, description] = (descIndex > -1) ?
  19182. [meaningAndDesc.slice(0, descIndex), meaningAndDesc.slice(descIndex + 1)] :
  19183. ['', meaningAndDesc];
  19184. }
  19185. return { customId, meaning, description };
  19186. }
  19187. // Converts i18n meta information for a message (id, description, meaning)
  19188. // to a JsDoc statement formatted as expected by the Closure compiler.
  19189. function i18nMetaToJSDoc(meta) {
  19190. const tags = [];
  19191. if (meta.description) {
  19192. tags.push({ tagName: "desc" /* o.JSDocTagName.Desc */, text: meta.description });
  19193. }
  19194. else {
  19195. // Suppress the JSCompiler warning that a `@desc` was not given for this message.
  19196. tags.push({ tagName: "suppress" /* o.JSDocTagName.Suppress */, text: '{msgDescriptions}' });
  19197. }
  19198. if (meta.meaning) {
  19199. tags.push({ tagName: "meaning" /* o.JSDocTagName.Meaning */, text: meta.meaning });
  19200. }
  19201. return jsDocComment(tags);
  19202. }
  19203. /** Closure uses `goog.getMsg(message)` to lookup translations */
  19204. const GOOG_GET_MSG = 'goog.getMsg';
  19205. /**
  19206. * Generates a `goog.getMsg()` statement and reassignment. The template:
  19207. *
  19208. * ```html
  19209. * <div i18n>Sent from {{ sender }} to <span class="receiver">{{ receiver }}</span></div>
  19210. * ```
  19211. *
  19212. * Generates:
  19213. *
  19214. * ```typescript
  19215. * const MSG_FOO = goog.getMsg(
  19216. * // Message template.
  19217. * 'Sent from {$interpolation} to {$startTagSpan}{$interpolation_1}{$closeTagSpan}.',
  19218. * // Placeholder values, set to magic strings which get replaced by the Angular runtime.
  19219. * {
  19220. * 'interpolation': '\uFFFD0\uFFFD',
  19221. * 'startTagSpan': '\uFFFD1\uFFFD',
  19222. * 'interpolation_1': '\uFFFD2\uFFFD',
  19223. * 'closeTagSpan': '\uFFFD3\uFFFD',
  19224. * },
  19225. * // Options bag.
  19226. * {
  19227. * // Maps each placeholder to the original Angular source code which generates it's value.
  19228. * original_code: {
  19229. * 'interpolation': '{{ sender }}',
  19230. * 'startTagSpan': '<span class="receiver">',
  19231. * 'interpolation_1': '{{ receiver }}',
  19232. * 'closeTagSpan': '</span>',
  19233. * },
  19234. * },
  19235. * );
  19236. * const I18N_0 = MSG_FOO;
  19237. * ```
  19238. */
  19239. function createGoogleGetMsgStatements(variable$1, message, closureVar, placeholderValues) {
  19240. const messageString = serializeI18nMessageForGetMsg(message);
  19241. const args = [literal(messageString)];
  19242. if (Object.keys(placeholderValues).length) {
  19243. // Message template parameters containing the magic strings replaced by the Angular runtime with
  19244. // real data, e.g. `{'interpolation': '\uFFFD0\uFFFD'}`.
  19245. args.push(mapLiteral(formatI18nPlaceholderNamesInMap(placeholderValues, true /* useCamelCase */), true /* quoted */));
  19246. // Message options object, which contains original source code for placeholders (as they are
  19247. // present in a template, e.g.
  19248. // `{original_code: {'interpolation': '{{ name }}', 'startTagSpan': '<span>'}}`.
  19249. args.push(mapLiteral({
  19250. original_code: literalMap(Object.keys(placeholderValues)
  19251. .map((param) => ({
  19252. key: formatI18nPlaceholderName(param),
  19253. quoted: true,
  19254. value: message.placeholders[param] ?
  19255. // Get source span for typical placeholder if it exists.
  19256. literal(message.placeholders[param].sourceSpan.toString()) :
  19257. // Otherwise must be an ICU expression, get it's source span.
  19258. literal(message.placeholderToMessage[param]
  19259. .nodes.map((node) => node.sourceSpan.toString())
  19260. .join('')),
  19261. }))),
  19262. }));
  19263. }
  19264. // /**
  19265. // * @desc description of message
  19266. // * @meaning meaning of message
  19267. // */
  19268. // const MSG_... = goog.getMsg(..);
  19269. // I18N_X = MSG_...;
  19270. const googGetMsgStmt = closureVar.set(variable(GOOG_GET_MSG).callFn(args)).toConstDecl();
  19271. googGetMsgStmt.addLeadingComment(i18nMetaToJSDoc(message));
  19272. const i18nAssignmentStmt = new ExpressionStatement(variable$1.set(closureVar));
  19273. return [googGetMsgStmt, i18nAssignmentStmt];
  19274. }
  19275. /**
  19276. * This visitor walks over i18n tree and generates its string representation, including ICUs and
  19277. * placeholders in `{$placeholder}` (for plain messages) or `{PLACEHOLDER}` (inside ICUs) format.
  19278. */
  19279. class GetMsgSerializerVisitor {
  19280. formatPh(value) {
  19281. return `{$${formatI18nPlaceholderName(value)}}`;
  19282. }
  19283. visitText(text) {
  19284. return text.value;
  19285. }
  19286. visitContainer(container) {
  19287. return container.children.map(child => child.visit(this)).join('');
  19288. }
  19289. visitIcu(icu) {
  19290. return serializeIcuNode(icu);
  19291. }
  19292. visitTagPlaceholder(ph) {
  19293. return ph.isVoid ?
  19294. this.formatPh(ph.startName) :
  19295. `${this.formatPh(ph.startName)}${ph.children.map(child => child.visit(this)).join('')}${this.formatPh(ph.closeName)}`;
  19296. }
  19297. visitPlaceholder(ph) {
  19298. return this.formatPh(ph.name);
  19299. }
  19300. visitIcuPlaceholder(ph, context) {
  19301. return this.formatPh(ph.name);
  19302. }
  19303. }
  19304. const serializerVisitor = new GetMsgSerializerVisitor();
  19305. function serializeI18nMessageForGetMsg(message) {
  19306. return message.nodes.map(node => node.visit(serializerVisitor, null)).join('');
  19307. }
  19308. function createLocalizeStatements(variable, message, params) {
  19309. const { messageParts, placeHolders } = serializeI18nMessageForLocalize(message);
  19310. const sourceSpan = getSourceSpan(message);
  19311. const expressions = placeHolders.map(ph => params[ph.text]);
  19312. const localizedString$1 = localizedString(message, messageParts, placeHolders, expressions, sourceSpan);
  19313. const variableInitialization = variable.set(localizedString$1);
  19314. return [new ExpressionStatement(variableInitialization)];
  19315. }
  19316. /**
  19317. * This visitor walks over an i18n tree, capturing literal strings and placeholders.
  19318. *
  19319. * The result can be used for generating the `$localize` tagged template literals.
  19320. */
  19321. class LocalizeSerializerVisitor {
  19322. constructor(placeholderToMessage, pieces) {
  19323. this.placeholderToMessage = placeholderToMessage;
  19324. this.pieces = pieces;
  19325. }
  19326. visitText(text) {
  19327. if (this.pieces[this.pieces.length - 1] instanceof LiteralPiece) {
  19328. // Two literal pieces in a row means that there was some comment node in-between.
  19329. this.pieces[this.pieces.length - 1].text += text.value;
  19330. }
  19331. else {
  19332. const sourceSpan = new ParseSourceSpan(text.sourceSpan.fullStart, text.sourceSpan.end, text.sourceSpan.fullStart, text.sourceSpan.details);
  19333. this.pieces.push(new LiteralPiece(text.value, sourceSpan));
  19334. }
  19335. }
  19336. visitContainer(container) {
  19337. container.children.forEach(child => child.visit(this));
  19338. }
  19339. visitIcu(icu) {
  19340. this.pieces.push(new LiteralPiece(serializeIcuNode(icu), icu.sourceSpan));
  19341. }
  19342. visitTagPlaceholder(ph) {
  19343. this.pieces.push(this.createPlaceholderPiece(ph.startName, ph.startSourceSpan ?? ph.sourceSpan));
  19344. if (!ph.isVoid) {
  19345. ph.children.forEach(child => child.visit(this));
  19346. this.pieces.push(this.createPlaceholderPiece(ph.closeName, ph.endSourceSpan ?? ph.sourceSpan));
  19347. }
  19348. }
  19349. visitPlaceholder(ph) {
  19350. this.pieces.push(this.createPlaceholderPiece(ph.name, ph.sourceSpan));
  19351. }
  19352. visitIcuPlaceholder(ph) {
  19353. this.pieces.push(this.createPlaceholderPiece(ph.name, ph.sourceSpan, this.placeholderToMessage[ph.name]));
  19354. }
  19355. createPlaceholderPiece(name, sourceSpan, associatedMessage) {
  19356. return new PlaceholderPiece(formatI18nPlaceholderName(name, /* useCamelCase */ false), sourceSpan, associatedMessage);
  19357. }
  19358. }
  19359. /**
  19360. * Serialize an i18n message into two arrays: messageParts and placeholders.
  19361. *
  19362. * These arrays will be used to generate `$localize` tagged template literals.
  19363. *
  19364. * @param message The message to be serialized.
  19365. * @returns an object containing the messageParts and placeholders.
  19366. */
  19367. function serializeI18nMessageForLocalize(message) {
  19368. const pieces = [];
  19369. const serializerVisitor = new LocalizeSerializerVisitor(message.placeholderToMessage, pieces);
  19370. message.nodes.forEach(node => node.visit(serializerVisitor));
  19371. return processMessagePieces(pieces);
  19372. }
  19373. function getSourceSpan(message) {
  19374. const startNode = message.nodes[0];
  19375. const endNode = message.nodes[message.nodes.length - 1];
  19376. return new ParseSourceSpan(startNode.sourceSpan.fullStart, endNode.sourceSpan.end, startNode.sourceSpan.fullStart, startNode.sourceSpan.details);
  19377. }
  19378. /**
  19379. * Convert the list of serialized MessagePieces into two arrays.
  19380. *
  19381. * One contains the literal string pieces and the other the placeholders that will be replaced by
  19382. * expressions when rendering `$localize` tagged template literals.
  19383. *
  19384. * @param pieces The pieces to process.
  19385. * @returns an object containing the messageParts and placeholders.
  19386. */
  19387. function processMessagePieces(pieces) {
  19388. const messageParts = [];
  19389. const placeHolders = [];
  19390. if (pieces[0] instanceof PlaceholderPiece) {
  19391. // The first piece was a placeholder so we need to add an initial empty message part.
  19392. messageParts.push(createEmptyMessagePart(pieces[0].sourceSpan.start));
  19393. }
  19394. for (let i = 0; i < pieces.length; i++) {
  19395. const part = pieces[i];
  19396. if (part instanceof LiteralPiece) {
  19397. messageParts.push(part);
  19398. }
  19399. else {
  19400. placeHolders.push(part);
  19401. if (pieces[i - 1] instanceof PlaceholderPiece) {
  19402. // There were two placeholders in a row, so we need to add an empty message part.
  19403. messageParts.push(createEmptyMessagePart(pieces[i - 1].sourceSpan.end));
  19404. }
  19405. }
  19406. }
  19407. if (pieces[pieces.length - 1] instanceof PlaceholderPiece) {
  19408. // The last piece was a placeholder so we need to add a final empty message part.
  19409. messageParts.push(createEmptyMessagePart(pieces[pieces.length - 1].sourceSpan.end));
  19410. }
  19411. return { messageParts, placeHolders };
  19412. }
  19413. function createEmptyMessagePart(location) {
  19414. return new LiteralPiece('', new ParseSourceSpan(location, location));
  19415. }
  19416. // Selector attribute name of `<ng-content>`
  19417. const NG_CONTENT_SELECT_ATTR = 'select';
  19418. // Attribute name of `ngProjectAs`.
  19419. const NG_PROJECT_AS_ATTR_NAME = 'ngProjectAs';
  19420. // Global symbols available only inside event bindings.
  19421. const EVENT_BINDING_SCOPE_GLOBALS = new Set(['$event']);
  19422. // List of supported global targets for event listeners
  19423. const GLOBAL_TARGET_RESOLVERS = new Map([['window', Identifiers.resolveWindow], ['document', Identifiers.resolveDocument], ['body', Identifiers.resolveBody]]);
  19424. const LEADING_TRIVIA_CHARS = [' ', '\n', '\r', '\t'];
  19425. // if (rf & flags) { .. }
  19426. function renderFlagCheckIfStmt(flags, statements) {
  19427. return ifStmt(variable(RENDER_FLAGS).bitwiseAnd(literal(flags), null, false), statements);
  19428. }
  19429. function prepareEventListenerParameters(eventAst, handlerName = null, scope = null) {
  19430. const { type, name, target, phase, handler } = eventAst;
  19431. if (target && !GLOBAL_TARGET_RESOLVERS.has(target)) {
  19432. throw new Error(`Unexpected global target '${target}' defined for '${name}' event.
  19433. Supported list of global targets: ${Array.from(GLOBAL_TARGET_RESOLVERS.keys())}.`);
  19434. }
  19435. const eventArgumentName = '$event';
  19436. const implicitReceiverAccesses = new Set();
  19437. const implicitReceiverExpr = (scope === null || scope.bindingLevel === 0) ?
  19438. variable(CONTEXT_NAME) :
  19439. scope.getOrCreateSharedContextVar(0);
  19440. const bindingStatements = convertActionBinding(scope, implicitReceiverExpr, handler, 'b', eventAst.handlerSpan, implicitReceiverAccesses, EVENT_BINDING_SCOPE_GLOBALS);
  19441. const statements = [];
  19442. const variableDeclarations = scope?.variableDeclarations();
  19443. const restoreViewStatement = scope?.restoreViewStatement();
  19444. if (variableDeclarations) {
  19445. // `variableDeclarations` needs to run first, because
  19446. // `restoreViewStatement` depends on the result.
  19447. statements.push(...variableDeclarations);
  19448. }
  19449. statements.push(...bindingStatements);
  19450. if (restoreViewStatement) {
  19451. statements.unshift(restoreViewStatement);
  19452. // If there's a `restoreView` call, we need to reset the view at the end of the listener
  19453. // in order to avoid a leak. If there's a `return` statement already, we wrap it in the
  19454. // call, e.g. `return resetView(ctx.foo())`. Otherwise we add the call as the last statement.
  19455. const lastStatement = statements[statements.length - 1];
  19456. if (lastStatement instanceof ReturnStatement) {
  19457. statements[statements.length - 1] = new ReturnStatement(invokeInstruction(lastStatement.value.sourceSpan, Identifiers.resetView, [lastStatement.value]));
  19458. }
  19459. else {
  19460. statements.push(new ExpressionStatement(invokeInstruction(null, Identifiers.resetView, [])));
  19461. }
  19462. }
  19463. const eventName = type === 1 /* ParsedEventType.Animation */ ? prepareSyntheticListenerName(name, phase) : name;
  19464. const fnName = handlerName && sanitizeIdentifier(handlerName);
  19465. const fnArgs = [];
  19466. if (implicitReceiverAccesses.has(eventArgumentName)) {
  19467. fnArgs.push(new FnParam(eventArgumentName, DYNAMIC_TYPE));
  19468. }
  19469. const handlerFn = fn(fnArgs, statements, INFERRED_TYPE, null, fnName);
  19470. const params = [literal(eventName), handlerFn];
  19471. if (target) {
  19472. params.push(literal(false), // `useCapture` flag, defaults to `false`
  19473. importExpr(GLOBAL_TARGET_RESOLVERS.get(target)));
  19474. }
  19475. return params;
  19476. }
  19477. function createComponentDefConsts() {
  19478. return {
  19479. prepareStatements: [],
  19480. constExpressions: [],
  19481. i18nVarRefsCache: new Map(),
  19482. };
  19483. }
  19484. class TemplateDefinitionBuilder {
  19485. constructor(constantPool, parentBindingScope, level = 0, contextName, i18nContext, templateIndex, templateName, _namespace, relativeContextFilePath, i18nUseExternalIds, _constants = createComponentDefConsts()) {
  19486. this.constantPool = constantPool;
  19487. this.level = level;
  19488. this.contextName = contextName;
  19489. this.i18nContext = i18nContext;
  19490. this.templateIndex = templateIndex;
  19491. this.templateName = templateName;
  19492. this._namespace = _namespace;
  19493. this.i18nUseExternalIds = i18nUseExternalIds;
  19494. this._constants = _constants;
  19495. this._dataIndex = 0;
  19496. this._bindingContext = 0;
  19497. this._prefixCode = [];
  19498. /**
  19499. * List of callbacks to generate creation mode instructions. We store them here as we process
  19500. * the template so bindings in listeners are resolved only once all nodes have been visited.
  19501. * This ensures all local refs and context variables are available for matching.
  19502. */
  19503. this._creationCodeFns = [];
  19504. /**
  19505. * List of callbacks to generate update mode instructions. We store them here as we process
  19506. * the template so bindings are resolved only once all nodes have been visited. This ensures
  19507. * all local refs and context variables are available for matching.
  19508. */
  19509. this._updateCodeFns = [];
  19510. /** Index of the currently-selected node. */
  19511. this._currentIndex = 0;
  19512. /** Temporary variable declarations generated from visiting pipes, literals, etc. */
  19513. this._tempVariables = [];
  19514. /**
  19515. * List of callbacks to build nested templates. Nested templates must not be visited until
  19516. * after the parent template has finished visiting all of its nodes. This ensures that all
  19517. * local ref bindings in nested templates are able to find local ref values if the refs
  19518. * are defined after the template declaration.
  19519. */
  19520. this._nestedTemplateFns = [];
  19521. // i18n context local to this template
  19522. this.i18n = null;
  19523. // Number of slots to reserve for pureFunctions
  19524. this._pureFunctionSlots = 0;
  19525. // Number of binding slots
  19526. this._bindingSlots = 0;
  19527. // Projection slots found in the template. Projection slots can distribute projected
  19528. // nodes based on a selector, or can just use the wildcard selector to match
  19529. // all nodes which aren't matching any selector.
  19530. this._ngContentReservedSlots = [];
  19531. // Number of non-default selectors found in all parent templates of this template. We need to
  19532. // track it to properly adjust projection slot index in the `projection` instruction.
  19533. this._ngContentSelectorsOffset = 0;
  19534. // Expression that should be used as implicit receiver when converting template
  19535. // expressions to output AST.
  19536. this._implicitReceiverExpr = null;
  19537. // These should be handled in the template or element directly.
  19538. this.visitReference = invalid;
  19539. this.visitVariable = invalid;
  19540. this.visitTextAttribute = invalid;
  19541. this.visitBoundAttribute = invalid;
  19542. this.visitBoundEvent = invalid;
  19543. this._bindingScope = parentBindingScope.nestedScope(level);
  19544. // Turn the relative context file path into an identifier by replacing non-alphanumeric
  19545. // characters with underscores.
  19546. this.fileBasedI18nSuffix = relativeContextFilePath.replace(/[^A-Za-z0-9]/g, '_') + '_';
  19547. this._valueConverter = new ValueConverter(constantPool, () => this.allocateDataSlot(), (numSlots) => this.allocatePureFunctionSlots(numSlots), (name, localName, slot, value) => {
  19548. this._bindingScope.set(this.level, localName, value);
  19549. this.creationInstruction(null, Identifiers.pipe, [literal(slot), literal(name)]);
  19550. });
  19551. }
  19552. buildTemplateFunction(nodes, variables, ngContentSelectorsOffset = 0, i18n) {
  19553. this._ngContentSelectorsOffset = ngContentSelectorsOffset;
  19554. if (this._namespace !== Identifiers.namespaceHTML) {
  19555. this.creationInstruction(null, this._namespace);
  19556. }
  19557. // Create variable bindings
  19558. variables.forEach(v => this.registerContextVariables(v));
  19559. // Initiate i18n context in case:
  19560. // - this template has parent i18n context
  19561. // - or the template has i18n meta associated with it,
  19562. // but it's not initiated by the Element (e.g. <ng-template i18n>)
  19563. const initI18nContext = this.i18nContext ||
  19564. (isI18nRootNode(i18n) && !isSingleI18nIcu(i18n) &&
  19565. !(isSingleElementTemplate(nodes) && nodes[0].i18n === i18n));
  19566. const selfClosingI18nInstruction = hasTextChildrenOnly(nodes);
  19567. if (initI18nContext) {
  19568. this.i18nStart(null, i18n, selfClosingI18nInstruction);
  19569. }
  19570. // This is the initial pass through the nodes of this template. In this pass, we
  19571. // queue all creation mode and update mode instructions for generation in the second
  19572. // pass. It's necessary to separate the passes to ensure local refs are defined before
  19573. // resolving bindings. We also count bindings in this pass as we walk bound expressions.
  19574. visitAll$1(this, nodes);
  19575. // Add total binding count to pure function count so pure function instructions are
  19576. // generated with the correct slot offset when update instructions are processed.
  19577. this._pureFunctionSlots += this._bindingSlots;
  19578. // Pipes are walked in the first pass (to enqueue `pipe()` creation instructions and
  19579. // `pipeBind` update instructions), so we have to update the slot offsets manually
  19580. // to account for bindings.
  19581. this._valueConverter.updatePipeSlotOffsets(this._bindingSlots);
  19582. // Nested templates must be processed before creation instructions so template()
  19583. // instructions can be generated with the correct internal const count.
  19584. this._nestedTemplateFns.forEach(buildTemplateFn => buildTemplateFn());
  19585. // Output the `projectionDef` instruction when some `<ng-content>` tags are present.
  19586. // The `projectionDef` instruction is only emitted for the component template and
  19587. // is skipped for nested templates (<ng-template> tags).
  19588. if (this.level === 0 && this._ngContentReservedSlots.length) {
  19589. const parameters = [];
  19590. // By default the `projectionDef` instructions creates one slot for the wildcard
  19591. // selector if no parameters are passed. Therefore we only want to allocate a new
  19592. // array for the projection slots if the default projection slot is not sufficient.
  19593. if (this._ngContentReservedSlots.length > 1 || this._ngContentReservedSlots[0] !== '*') {
  19594. const r3ReservedSlots = this._ngContentReservedSlots.map(s => s !== '*' ? parseSelectorToR3Selector(s) : s);
  19595. parameters.push(this.constantPool.getConstLiteral(asLiteral(r3ReservedSlots), true));
  19596. }
  19597. // Since we accumulate ngContent selectors while processing template elements,
  19598. // we *prepend* `projectionDef` to creation instructions block, to put it before
  19599. // any `projection` instructions
  19600. this.creationInstruction(null, Identifiers.projectionDef, parameters, /* prepend */ true);
  19601. }
  19602. if (initI18nContext) {
  19603. this.i18nEnd(null, selfClosingI18nInstruction);
  19604. }
  19605. // Generate all the creation mode instructions (e.g. resolve bindings in listeners)
  19606. const creationStatements = getInstructionStatements(this._creationCodeFns);
  19607. // Generate all the update mode instructions (e.g. resolve property or text bindings)
  19608. const updateStatements = getInstructionStatements(this._updateCodeFns);
  19609. // Variable declaration must occur after binding resolution so we can generate context
  19610. // instructions that build on each other.
  19611. // e.g. const b = nextContext().$implicit(); const b = nextContext();
  19612. const creationVariables = this._bindingScope.viewSnapshotStatements();
  19613. const updateVariables = this._bindingScope.variableDeclarations().concat(this._tempVariables);
  19614. const creationBlock = creationStatements.length > 0 ?
  19615. [renderFlagCheckIfStmt(1 /* core.RenderFlags.Create */, creationVariables.concat(creationStatements))] :
  19616. [];
  19617. const updateBlock = updateStatements.length > 0 ?
  19618. [renderFlagCheckIfStmt(2 /* core.RenderFlags.Update */, updateVariables.concat(updateStatements))] :
  19619. [];
  19620. return fn(
  19621. // i.e. (rf: RenderFlags, ctx: any)
  19622. [new FnParam(RENDER_FLAGS, NUMBER_TYPE), new FnParam(CONTEXT_NAME, null)], [
  19623. // Temporary variable declarations for query refresh (i.e. let _t: any;)
  19624. ...this._prefixCode,
  19625. // Creating mode (i.e. if (rf & RenderFlags.Create) { ... })
  19626. ...creationBlock,
  19627. // Binding and refresh mode (i.e. if (rf & RenderFlags.Update) {...})
  19628. ...updateBlock,
  19629. ], INFERRED_TYPE, null, this.templateName);
  19630. }
  19631. // LocalResolver
  19632. getLocal(name) {
  19633. return this._bindingScope.get(name);
  19634. }
  19635. // LocalResolver
  19636. notifyImplicitReceiverUse() {
  19637. this._bindingScope.notifyImplicitReceiverUse();
  19638. }
  19639. // LocalResolver
  19640. maybeRestoreView() {
  19641. this._bindingScope.maybeRestoreView();
  19642. }
  19643. i18nTranslate(message, params = {}, ref, transformFn) {
  19644. const _ref = ref || this.i18nGenerateMainBlockVar();
  19645. // Closure Compiler requires const names to start with `MSG_` but disallows any other const to
  19646. // start with `MSG_`. We define a variable starting with `MSG_` just for the `goog.getMsg` call
  19647. const closureVar = this.i18nGenerateClosureVar(message.id);
  19648. const statements = getTranslationDeclStmts(message, _ref, closureVar, params, transformFn);
  19649. this._constants.prepareStatements.push(...statements);
  19650. return _ref;
  19651. }
  19652. registerContextVariables(variable$1) {
  19653. const scopedName = this._bindingScope.freshReferenceName();
  19654. const retrievalLevel = this.level;
  19655. const lhs = variable(variable$1.name + scopedName);
  19656. this._bindingScope.set(retrievalLevel, variable$1.name, lhs, 1 /* DeclarationPriority.CONTEXT */, (scope, relativeLevel) => {
  19657. let rhs;
  19658. if (scope.bindingLevel === retrievalLevel) {
  19659. if (scope.isListenerScope() && scope.hasRestoreViewVariable()) {
  19660. // e.g. restoredCtx.
  19661. // We have to get the context from a view reference, if one is available, because
  19662. // the context that was passed in during creation may not be correct anymore.
  19663. // For more information see: https://github.com/angular/angular/pull/40360.
  19664. rhs = variable(RESTORED_VIEW_CONTEXT_NAME);
  19665. scope.notifyRestoredViewContextUse();
  19666. }
  19667. else {
  19668. // e.g. ctx
  19669. rhs = variable(CONTEXT_NAME);
  19670. }
  19671. }
  19672. else {
  19673. const sharedCtxVar = scope.getSharedContextName(retrievalLevel);
  19674. // e.g. ctx_r0 OR x(2);
  19675. rhs = sharedCtxVar ? sharedCtxVar : generateNextContextExpr(relativeLevel);
  19676. }
  19677. // e.g. const $item$ = x(2).$implicit;
  19678. return [lhs.set(rhs.prop(variable$1.value || IMPLICIT_REFERENCE)).toConstDecl()];
  19679. });
  19680. }
  19681. i18nAppendBindings(expressions) {
  19682. if (expressions.length > 0) {
  19683. expressions.forEach(expression => this.i18n.appendBinding(expression));
  19684. }
  19685. }
  19686. i18nBindProps(props) {
  19687. const bound = {};
  19688. Object.keys(props).forEach(key => {
  19689. const prop = props[key];
  19690. if (prop instanceof Text$3) {
  19691. bound[key] = literal(prop.value);
  19692. }
  19693. else {
  19694. const value = prop.value.visit(this._valueConverter);
  19695. this.allocateBindingSlots(value);
  19696. if (value instanceof Interpolation) {
  19697. const { strings, expressions } = value;
  19698. const { id, bindings } = this.i18n;
  19699. const label = assembleI18nBoundString(strings, bindings.size, id);
  19700. this.i18nAppendBindings(expressions);
  19701. bound[key] = literal(label);
  19702. }
  19703. }
  19704. });
  19705. return bound;
  19706. }
  19707. // Generates top level vars for i18n blocks (i.e. `i18n_N`).
  19708. i18nGenerateMainBlockVar() {
  19709. return variable(this.constantPool.uniqueName(TRANSLATION_VAR_PREFIX));
  19710. }
  19711. // Generates vars with Closure-specific names for i18n blocks (i.e. `MSG_XXX`).
  19712. i18nGenerateClosureVar(messageId) {
  19713. let name;
  19714. const suffix = this.fileBasedI18nSuffix.toUpperCase();
  19715. if (this.i18nUseExternalIds) {
  19716. const prefix = getTranslationConstPrefix(`EXTERNAL_`);
  19717. const uniqueSuffix = this.constantPool.uniqueName(suffix);
  19718. name = `${prefix}${sanitizeIdentifier(messageId)}$$${uniqueSuffix}`;
  19719. }
  19720. else {
  19721. const prefix = getTranslationConstPrefix(suffix);
  19722. name = this.constantPool.uniqueName(prefix);
  19723. }
  19724. return variable(name);
  19725. }
  19726. i18nUpdateRef(context) {
  19727. const { icus, meta, isRoot, isResolved, isEmitted } = context;
  19728. if (isRoot && isResolved && !isEmitted && !isSingleI18nIcu(meta)) {
  19729. context.isEmitted = true;
  19730. const placeholders = context.getSerializedPlaceholders();
  19731. let icuMapping = {};
  19732. let params = placeholders.size ? placeholdersToParams(placeholders) : {};
  19733. if (icus.size) {
  19734. icus.forEach((refs, key) => {
  19735. if (refs.length === 1) {
  19736. // if we have one ICU defined for a given
  19737. // placeholder - just output its reference
  19738. params[key] = refs[0];
  19739. }
  19740. else {
  19741. // ... otherwise we need to activate post-processing
  19742. // to replace ICU placeholders with proper values
  19743. const placeholder = wrapI18nPlaceholder(`${I18N_ICU_MAPPING_PREFIX}${key}`);
  19744. params[key] = literal(placeholder);
  19745. icuMapping[key] = literalArr(refs);
  19746. }
  19747. });
  19748. }
  19749. // translation requires post processing in 2 cases:
  19750. // - if we have placeholders with multiple values (ex. `START_DIV`: [�#1�, �#2�, ...])
  19751. // - if we have multiple ICUs that refer to the same placeholder name
  19752. const needsPostprocessing = Array.from(placeholders.values()).some((value) => value.length > 1) ||
  19753. Object.keys(icuMapping).length;
  19754. let transformFn;
  19755. if (needsPostprocessing) {
  19756. transformFn = (raw) => {
  19757. const args = [raw];
  19758. if (Object.keys(icuMapping).length) {
  19759. args.push(mapLiteral(icuMapping, true));
  19760. }
  19761. return invokeInstruction(null, Identifiers.i18nPostprocess, args);
  19762. };
  19763. }
  19764. this.i18nTranslate(meta, params, context.ref, transformFn);
  19765. }
  19766. }
  19767. i18nStart(span = null, meta, selfClosing) {
  19768. const index = this.allocateDataSlot();
  19769. this.i18n = this.i18nContext ?
  19770. this.i18nContext.forkChildContext(index, this.templateIndex, meta) :
  19771. new I18nContext(index, this.i18nGenerateMainBlockVar(), 0, this.templateIndex, meta);
  19772. // generate i18nStart instruction
  19773. const { id, ref } = this.i18n;
  19774. const params = [literal(index), this.addToConsts(ref)];
  19775. if (id > 0) {
  19776. // do not push 3rd argument (sub-block id)
  19777. // into i18nStart call for top level i18n context
  19778. params.push(literal(id));
  19779. }
  19780. this.creationInstruction(span, selfClosing ? Identifiers.i18n : Identifiers.i18nStart, params);
  19781. }
  19782. i18nEnd(span = null, selfClosing) {
  19783. if (!this.i18n) {
  19784. throw new Error('i18nEnd is executed with no i18n context present');
  19785. }
  19786. if (this.i18nContext) {
  19787. this.i18nContext.reconcileChildContext(this.i18n);
  19788. this.i18nUpdateRef(this.i18nContext);
  19789. }
  19790. else {
  19791. this.i18nUpdateRef(this.i18n);
  19792. }
  19793. // setup accumulated bindings
  19794. const { index, bindings } = this.i18n;
  19795. if (bindings.size) {
  19796. for (const binding of bindings) {
  19797. // for i18n block, advance to the most recent element index (by taking the current number of
  19798. // elements and subtracting one) before invoking `i18nExp` instructions, to make sure the
  19799. // necessary lifecycle hooks of components/directives are properly flushed.
  19800. this.updateInstructionWithAdvance(this.getConstCount() - 1, span, Identifiers.i18nExp, () => this.convertPropertyBinding(binding));
  19801. }
  19802. this.updateInstruction(span, Identifiers.i18nApply, [literal(index)]);
  19803. }
  19804. if (!selfClosing) {
  19805. this.creationInstruction(span, Identifiers.i18nEnd);
  19806. }
  19807. this.i18n = null; // reset local i18n context
  19808. }
  19809. i18nAttributesInstruction(nodeIndex, attrs, sourceSpan) {
  19810. let hasBindings = false;
  19811. const i18nAttrArgs = [];
  19812. attrs.forEach(attr => {
  19813. const message = attr.i18n;
  19814. const converted = attr.value.visit(this._valueConverter);
  19815. this.allocateBindingSlots(converted);
  19816. if (converted instanceof Interpolation) {
  19817. const placeholders = assembleBoundTextPlaceholders(message);
  19818. const params = placeholdersToParams(placeholders);
  19819. i18nAttrArgs.push(literal(attr.name), this.i18nTranslate(message, params));
  19820. converted.expressions.forEach(expression => {
  19821. hasBindings = true;
  19822. this.updateInstructionWithAdvance(nodeIndex, sourceSpan, Identifiers.i18nExp, () => this.convertPropertyBinding(expression));
  19823. });
  19824. }
  19825. });
  19826. if (i18nAttrArgs.length > 0) {
  19827. const index = literal(this.allocateDataSlot());
  19828. const constIndex = this.addToConsts(literalArr(i18nAttrArgs));
  19829. this.creationInstruction(sourceSpan, Identifiers.i18nAttributes, [index, constIndex]);
  19830. if (hasBindings) {
  19831. this.updateInstruction(sourceSpan, Identifiers.i18nApply, [index]);
  19832. }
  19833. }
  19834. }
  19835. getNamespaceInstruction(namespaceKey) {
  19836. switch (namespaceKey) {
  19837. case 'math':
  19838. return Identifiers.namespaceMathML;
  19839. case 'svg':
  19840. return Identifiers.namespaceSVG;
  19841. default:
  19842. return Identifiers.namespaceHTML;
  19843. }
  19844. }
  19845. addNamespaceInstruction(nsInstruction, element) {
  19846. this._namespace = nsInstruction;
  19847. this.creationInstruction(element.startSourceSpan, nsInstruction);
  19848. }
  19849. /**
  19850. * Adds an update instruction for an interpolated property or attribute, such as
  19851. * `prop="{{value}}"` or `attr.title="{{value}}"`
  19852. */
  19853. interpolatedUpdateInstruction(instruction, elementIndex, attrName, input, value, params) {
  19854. this.updateInstructionWithAdvance(elementIndex, input.sourceSpan, instruction, () => [literal(attrName), ...this.getUpdateInstructionArguments(value), ...params]);
  19855. }
  19856. visitContent(ngContent) {
  19857. const slot = this.allocateDataSlot();
  19858. const projectionSlotIdx = this._ngContentSelectorsOffset + this._ngContentReservedSlots.length;
  19859. const parameters = [literal(slot)];
  19860. this._ngContentReservedSlots.push(ngContent.selector);
  19861. const nonContentSelectAttributes = ngContent.attributes.filter(attr => attr.name.toLowerCase() !== NG_CONTENT_SELECT_ATTR);
  19862. const attributes = this.getAttributeExpressions(ngContent.name, nonContentSelectAttributes, [], []);
  19863. if (attributes.length > 0) {
  19864. parameters.push(literal(projectionSlotIdx), literalArr(attributes));
  19865. }
  19866. else if (projectionSlotIdx !== 0) {
  19867. parameters.push(literal(projectionSlotIdx));
  19868. }
  19869. this.creationInstruction(ngContent.sourceSpan, Identifiers.projection, parameters);
  19870. if (this.i18n) {
  19871. this.i18n.appendProjection(ngContent.i18n, slot);
  19872. }
  19873. }
  19874. visitElement(element) {
  19875. const elementIndex = this.allocateDataSlot();
  19876. const stylingBuilder = new StylingBuilder(null);
  19877. let isNonBindableMode = false;
  19878. const isI18nRootElement = isI18nRootNode(element.i18n) && !isSingleI18nIcu(element.i18n);
  19879. const outputAttrs = [];
  19880. const [namespaceKey, elementName] = splitNsName(element.name);
  19881. const isNgContainer$1 = isNgContainer(element.name);
  19882. // Handle styling, i18n, ngNonBindable attributes
  19883. for (const attr of element.attributes) {
  19884. const { name, value } = attr;
  19885. if (name === NON_BINDABLE_ATTR) {
  19886. isNonBindableMode = true;
  19887. }
  19888. else if (name === 'style') {
  19889. stylingBuilder.registerStyleAttr(value);
  19890. }
  19891. else if (name === 'class') {
  19892. stylingBuilder.registerClassAttr(value);
  19893. }
  19894. else {
  19895. outputAttrs.push(attr);
  19896. }
  19897. }
  19898. // Regular element or ng-container creation mode
  19899. const parameters = [literal(elementIndex)];
  19900. if (!isNgContainer$1) {
  19901. parameters.push(literal(elementName));
  19902. }
  19903. // Add the attributes
  19904. const allOtherInputs = [];
  19905. const boundI18nAttrs = [];
  19906. element.inputs.forEach(input => {
  19907. const stylingInputWasSet = stylingBuilder.registerBoundInput(input);
  19908. if (!stylingInputWasSet) {
  19909. if (input.type === 0 /* BindingType.Property */ && input.i18n) {
  19910. boundI18nAttrs.push(input);
  19911. }
  19912. else {
  19913. allOtherInputs.push(input);
  19914. }
  19915. }
  19916. });
  19917. // add attributes for directive and projection matching purposes
  19918. const attributes = this.getAttributeExpressions(element.name, outputAttrs, allOtherInputs, element.outputs, stylingBuilder, [], boundI18nAttrs);
  19919. parameters.push(this.addAttrsToConsts(attributes));
  19920. // local refs (ex.: <div #foo #bar="baz">)
  19921. const refs = this.prepareRefsArray(element.references);
  19922. parameters.push(this.addToConsts(refs));
  19923. const wasInNamespace = this._namespace;
  19924. const currentNamespace = this.getNamespaceInstruction(namespaceKey);
  19925. // If the namespace is changing now, include an instruction to change it
  19926. // during element creation.
  19927. if (currentNamespace !== wasInNamespace) {
  19928. this.addNamespaceInstruction(currentNamespace, element);
  19929. }
  19930. if (this.i18n) {
  19931. this.i18n.appendElement(element.i18n, elementIndex);
  19932. }
  19933. // Note that we do not append text node instructions and ICUs inside i18n section,
  19934. // so we exclude them while calculating whether current element has children
  19935. const hasChildren = (!isI18nRootElement && this.i18n) ? !hasTextChildrenOnly(element.children) :
  19936. element.children.length > 0;
  19937. const createSelfClosingInstruction = !stylingBuilder.hasBindingsWithPipes &&
  19938. element.outputs.length === 0 && boundI18nAttrs.length === 0 && !hasChildren;
  19939. const createSelfClosingI18nInstruction = !createSelfClosingInstruction && hasTextChildrenOnly(element.children);
  19940. if (createSelfClosingInstruction) {
  19941. this.creationInstruction(element.sourceSpan, isNgContainer$1 ? Identifiers.elementContainer : Identifiers.element, trimTrailingNulls(parameters));
  19942. }
  19943. else {
  19944. this.creationInstruction(element.startSourceSpan, isNgContainer$1 ? Identifiers.elementContainerStart : Identifiers.elementStart, trimTrailingNulls(parameters));
  19945. if (isNonBindableMode) {
  19946. this.creationInstruction(element.startSourceSpan, Identifiers.disableBindings);
  19947. }
  19948. if (boundI18nAttrs.length > 0) {
  19949. this.i18nAttributesInstruction(elementIndex, boundI18nAttrs, element.startSourceSpan ?? element.sourceSpan);
  19950. }
  19951. // Generate Listeners (outputs)
  19952. if (element.outputs.length > 0) {
  19953. for (const outputAst of element.outputs) {
  19954. this.creationInstruction(outputAst.sourceSpan, Identifiers.listener, this.prepareListenerParameter(element.name, outputAst, elementIndex));
  19955. }
  19956. }
  19957. // Note: it's important to keep i18n/i18nStart instructions after i18nAttributes and
  19958. // listeners, to make sure i18nAttributes instruction targets current element at runtime.
  19959. if (isI18nRootElement) {
  19960. this.i18nStart(element.startSourceSpan, element.i18n, createSelfClosingI18nInstruction);
  19961. }
  19962. }
  19963. // the code here will collect all update-level styling instructions and add them to the
  19964. // update block of the template function AOT code. Instructions like `styleProp`,
  19965. // `styleMap`, `classMap`, `classProp`
  19966. // are all generated and assigned in the code below.
  19967. const stylingInstructions = stylingBuilder.buildUpdateLevelInstructions(this._valueConverter);
  19968. const limit = stylingInstructions.length - 1;
  19969. for (let i = 0; i <= limit; i++) {
  19970. const instruction = stylingInstructions[i];
  19971. this._bindingSlots += this.processStylingUpdateInstruction(elementIndex, instruction);
  19972. }
  19973. // the reason why `undefined` is used is because the renderer understands this as a
  19974. // special value to symbolize that there is no RHS to this binding
  19975. // TODO (matsko): revisit this once FW-959 is approached
  19976. const emptyValueBindInstruction = literal(undefined);
  19977. const propertyBindings = [];
  19978. const attributeBindings = [];
  19979. // Generate element input bindings
  19980. allOtherInputs.forEach(input => {
  19981. const inputType = input.type;
  19982. if (inputType === 4 /* BindingType.Animation */) {
  19983. const value = input.value.visit(this._valueConverter);
  19984. // animation bindings can be presented in the following formats:
  19985. // 1. [@binding]="fooExp"
  19986. // 2. [@binding]="{value:fooExp, params:{...}}"
  19987. // 3. [@binding]
  19988. // 4. @binding
  19989. // All formats will be valid for when a synthetic binding is created.
  19990. // The reasoning for this is because the renderer should get each
  19991. // synthetic binding value in the order of the array that they are
  19992. // defined in...
  19993. const hasValue = value instanceof LiteralPrimitive ? !!value.value : true;
  19994. this.allocateBindingSlots(value);
  19995. propertyBindings.push({
  19996. span: input.sourceSpan,
  19997. paramsOrFn: getBindingFunctionParams(() => hasValue ? this.convertPropertyBinding(value) : emptyValueBindInstruction, prepareSyntheticPropertyName(input.name))
  19998. });
  19999. }
  20000. else {
  20001. // we must skip attributes with associated i18n context, since these attributes are handled
  20002. // separately and corresponding `i18nExp` and `i18nApply` instructions will be generated
  20003. if (input.i18n)
  20004. return;
  20005. const value = input.value.visit(this._valueConverter);
  20006. if (value !== undefined) {
  20007. const params = [];
  20008. const [attrNamespace, attrName] = splitNsName(input.name);
  20009. const isAttributeBinding = inputType === 1 /* BindingType.Attribute */;
  20010. let sanitizationRef = resolveSanitizationFn(input.securityContext, isAttributeBinding);
  20011. if (!sanitizationRef) {
  20012. // If there was no sanitization function found based on the security context
  20013. // of an attribute/property - check whether this attribute/property is
  20014. // one of the security-sensitive <iframe> attributes (and that the current
  20015. // element is actually an <iframe>).
  20016. if (isIframeElement(element.name) && isIframeSecuritySensitiveAttr(input.name)) {
  20017. sanitizationRef = importExpr(Identifiers.validateIframeAttribute);
  20018. }
  20019. }
  20020. if (sanitizationRef) {
  20021. params.push(sanitizationRef);
  20022. }
  20023. if (attrNamespace) {
  20024. const namespaceLiteral = literal(attrNamespace);
  20025. if (sanitizationRef) {
  20026. params.push(namespaceLiteral);
  20027. }
  20028. else {
  20029. // If there wasn't a sanitization ref, we need to add
  20030. // an extra param so that we can pass in the namespace.
  20031. params.push(literal(null), namespaceLiteral);
  20032. }
  20033. }
  20034. this.allocateBindingSlots(value);
  20035. if (inputType === 0 /* BindingType.Property */) {
  20036. if (value instanceof Interpolation) {
  20037. // prop="{{value}}" and friends
  20038. this.interpolatedUpdateInstruction(getPropertyInterpolationExpression(value), elementIndex, attrName, input, value, params);
  20039. }
  20040. else {
  20041. // [prop]="value"
  20042. // Collect all the properties so that we can chain into a single function at the end.
  20043. propertyBindings.push({
  20044. span: input.sourceSpan,
  20045. paramsOrFn: getBindingFunctionParams(() => this.convertPropertyBinding(value), attrName, params)
  20046. });
  20047. }
  20048. }
  20049. else if (inputType === 1 /* BindingType.Attribute */) {
  20050. if (value instanceof Interpolation && getInterpolationArgsLength(value) > 1) {
  20051. // attr.name="text{{value}}" and friends
  20052. this.interpolatedUpdateInstruction(getAttributeInterpolationExpression(value), elementIndex, attrName, input, value, params);
  20053. }
  20054. else {
  20055. const boundValue = value instanceof Interpolation ? value.expressions[0] : value;
  20056. // [attr.name]="value" or attr.name="{{value}}"
  20057. // Collect the attribute bindings so that they can be chained at the end.
  20058. attributeBindings.push({
  20059. span: input.sourceSpan,
  20060. paramsOrFn: getBindingFunctionParams(() => this.convertPropertyBinding(boundValue), attrName, params)
  20061. });
  20062. }
  20063. }
  20064. else {
  20065. // class prop
  20066. this.updateInstructionWithAdvance(elementIndex, input.sourceSpan, Identifiers.classProp, () => {
  20067. return [
  20068. literal(elementIndex), literal(attrName), this.convertPropertyBinding(value),
  20069. ...params
  20070. ];
  20071. });
  20072. }
  20073. }
  20074. }
  20075. });
  20076. for (const propertyBinding of propertyBindings) {
  20077. this.updateInstructionWithAdvance(elementIndex, propertyBinding.span, Identifiers.property, propertyBinding.paramsOrFn);
  20078. }
  20079. for (const attributeBinding of attributeBindings) {
  20080. this.updateInstructionWithAdvance(elementIndex, attributeBinding.span, Identifiers.attribute, attributeBinding.paramsOrFn);
  20081. }
  20082. // Traverse element child nodes
  20083. visitAll$1(this, element.children);
  20084. if (!isI18nRootElement && this.i18n) {
  20085. this.i18n.appendElement(element.i18n, elementIndex, true);
  20086. }
  20087. if (!createSelfClosingInstruction) {
  20088. // Finish element construction mode.
  20089. const span = element.endSourceSpan ?? element.sourceSpan;
  20090. if (isI18nRootElement) {
  20091. this.i18nEnd(span, createSelfClosingI18nInstruction);
  20092. }
  20093. if (isNonBindableMode) {
  20094. this.creationInstruction(span, Identifiers.enableBindings);
  20095. }
  20096. this.creationInstruction(span, isNgContainer$1 ? Identifiers.elementContainerEnd : Identifiers.elementEnd);
  20097. }
  20098. }
  20099. visitTemplate(template) {
  20100. const NG_TEMPLATE_TAG_NAME = 'ng-template';
  20101. const templateIndex = this.allocateDataSlot();
  20102. if (this.i18n) {
  20103. this.i18n.appendTemplate(template.i18n, templateIndex);
  20104. }
  20105. const tagNameWithoutNamespace = template.tagName ? splitNsName(template.tagName)[1] : template.tagName;
  20106. const contextName = `${this.contextName}${template.tagName ? '_' + sanitizeIdentifier(template.tagName) : ''}_${templateIndex}`;
  20107. const templateName = `${contextName}_Template`;
  20108. const parameters = [
  20109. literal(templateIndex),
  20110. variable(templateName),
  20111. // We don't care about the tag's namespace here, because we infer
  20112. // it based on the parent nodes inside the template instruction.
  20113. literal(tagNameWithoutNamespace),
  20114. ];
  20115. // prepare attributes parameter (including attributes used for directive matching)
  20116. const attrsExprs = this.getAttributeExpressions(NG_TEMPLATE_TAG_NAME, template.attributes, template.inputs, template.outputs, undefined /* styles */, template.templateAttrs);
  20117. parameters.push(this.addAttrsToConsts(attrsExprs));
  20118. // local refs (ex.: <ng-template #foo>)
  20119. if (template.references && template.references.length) {
  20120. const refs = this.prepareRefsArray(template.references);
  20121. parameters.push(this.addToConsts(refs));
  20122. parameters.push(importExpr(Identifiers.templateRefExtractor));
  20123. }
  20124. // Create the template function
  20125. const templateVisitor = new TemplateDefinitionBuilder(this.constantPool, this._bindingScope, this.level + 1, contextName, this.i18n, templateIndex, templateName, this._namespace, this.fileBasedI18nSuffix, this.i18nUseExternalIds, this._constants);
  20126. // Nested templates must not be visited until after their parent templates have completed
  20127. // processing, so they are queued here until after the initial pass. Otherwise, we wouldn't
  20128. // be able to support bindings in nested templates to local refs that occur after the
  20129. // template definition. e.g. <div *ngIf="showing">{{ foo }}</div> <div #foo></div>
  20130. this._nestedTemplateFns.push(() => {
  20131. const templateFunctionExpr = templateVisitor.buildTemplateFunction(template.children, template.variables, this._ngContentReservedSlots.length + this._ngContentSelectorsOffset, template.i18n);
  20132. this.constantPool.statements.push(templateFunctionExpr.toDeclStmt(templateName));
  20133. if (templateVisitor._ngContentReservedSlots.length) {
  20134. this._ngContentReservedSlots.push(...templateVisitor._ngContentReservedSlots);
  20135. }
  20136. });
  20137. // e.g. template(1, MyComp_Template_1)
  20138. this.creationInstruction(template.sourceSpan, Identifiers.templateCreate, () => {
  20139. parameters.splice(2, 0, literal(templateVisitor.getConstCount()), literal(templateVisitor.getVarCount()));
  20140. return trimTrailingNulls(parameters);
  20141. });
  20142. // handle property bindings e.g. ɵɵproperty('ngForOf', ctx.items), et al;
  20143. this.templatePropertyBindings(templateIndex, template.templateAttrs);
  20144. // Only add normal input/output binding instructions on explicit <ng-template> elements.
  20145. if (tagNameWithoutNamespace === NG_TEMPLATE_TAG_NAME) {
  20146. const [i18nInputs, inputs] = partitionArray(template.inputs, hasI18nMeta);
  20147. // Add i18n attributes that may act as inputs to directives. If such attributes are present,
  20148. // generate `i18nAttributes` instruction. Note: we generate it only for explicit <ng-template>
  20149. // elements, in case of inline templates, corresponding instructions will be generated in the
  20150. // nested template function.
  20151. if (i18nInputs.length > 0) {
  20152. this.i18nAttributesInstruction(templateIndex, i18nInputs, template.startSourceSpan ?? template.sourceSpan);
  20153. }
  20154. // Add the input bindings
  20155. if (inputs.length > 0) {
  20156. this.templatePropertyBindings(templateIndex, inputs);
  20157. }
  20158. // Generate listeners for directive output
  20159. for (const outputAst of template.outputs) {
  20160. this.creationInstruction(outputAst.sourceSpan, Identifiers.listener, this.prepareListenerParameter('ng_template', outputAst, templateIndex));
  20161. }
  20162. }
  20163. }
  20164. visitBoundText(text) {
  20165. if (this.i18n) {
  20166. const value = text.value.visit(this._valueConverter);
  20167. this.allocateBindingSlots(value);
  20168. if (value instanceof Interpolation) {
  20169. this.i18n.appendBoundText(text.i18n);
  20170. this.i18nAppendBindings(value.expressions);
  20171. }
  20172. return;
  20173. }
  20174. const nodeIndex = this.allocateDataSlot();
  20175. this.creationInstruction(text.sourceSpan, Identifiers.text, [literal(nodeIndex)]);
  20176. const value = text.value.visit(this._valueConverter);
  20177. this.allocateBindingSlots(value);
  20178. if (value instanceof Interpolation) {
  20179. this.updateInstructionWithAdvance(nodeIndex, text.sourceSpan, getTextInterpolationExpression(value), () => this.getUpdateInstructionArguments(value));
  20180. }
  20181. else {
  20182. error('Text nodes should be interpolated and never bound directly.');
  20183. }
  20184. }
  20185. visitText(text) {
  20186. // when a text element is located within a translatable
  20187. // block, we exclude this text element from instructions set,
  20188. // since it will be captured in i18n content and processed at runtime
  20189. if (!this.i18n) {
  20190. this.creationInstruction(text.sourceSpan, Identifiers.text, [literal(this.allocateDataSlot()), literal(text.value)]);
  20191. }
  20192. }
  20193. visitIcu(icu) {
  20194. let initWasInvoked = false;
  20195. // if an ICU was created outside of i18n block, we still treat
  20196. // it as a translatable entity and invoke i18nStart and i18nEnd
  20197. // to generate i18n context and the necessary instructions
  20198. if (!this.i18n) {
  20199. initWasInvoked = true;
  20200. this.i18nStart(null, icu.i18n, true);
  20201. }
  20202. const i18n = this.i18n;
  20203. const vars = this.i18nBindProps(icu.vars);
  20204. const placeholders = this.i18nBindProps(icu.placeholders);
  20205. // output ICU directly and keep ICU reference in context
  20206. const message = icu.i18n;
  20207. // we always need post-processing function for ICUs, to make sure that:
  20208. // - all placeholders in a form of {PLACEHOLDER} are replaced with actual values (note:
  20209. // `goog.getMsg` does not process ICUs and uses the `{PLACEHOLDER}` format for placeholders
  20210. // inside ICUs)
  20211. // - all ICU vars (such as `VAR_SELECT` or `VAR_PLURAL`) are replaced with correct values
  20212. const transformFn = (raw) => {
  20213. const params = { ...vars, ...placeholders };
  20214. const formatted = formatI18nPlaceholderNamesInMap(params, /* useCamelCase */ false);
  20215. return invokeInstruction(null, Identifiers.i18nPostprocess, [raw, mapLiteral(formatted, true)]);
  20216. };
  20217. // in case the whole i18n message is a single ICU - we do not need to
  20218. // create a separate top-level translation, we can use the root ref instead
  20219. // and make this ICU a top-level translation
  20220. // note: ICU placeholders are replaced with actual values in `i18nPostprocess` function
  20221. // separately, so we do not pass placeholders into `i18nTranslate` function.
  20222. if (isSingleI18nIcu(i18n.meta)) {
  20223. this.i18nTranslate(message, /* placeholders */ {}, i18n.ref, transformFn);
  20224. }
  20225. else {
  20226. // output ICU directly and keep ICU reference in context
  20227. const ref = this.i18nTranslate(message, /* placeholders */ {}, /* ref */ undefined, transformFn);
  20228. i18n.appendIcu(icuFromI18nMessage(message).name, ref);
  20229. }
  20230. if (initWasInvoked) {
  20231. this.i18nEnd(null, true);
  20232. }
  20233. return null;
  20234. }
  20235. allocateDataSlot() {
  20236. return this._dataIndex++;
  20237. }
  20238. getConstCount() {
  20239. return this._dataIndex;
  20240. }
  20241. getVarCount() {
  20242. return this._pureFunctionSlots;
  20243. }
  20244. getConsts() {
  20245. return this._constants;
  20246. }
  20247. getNgContentSelectors() {
  20248. return this._ngContentReservedSlots.length ?
  20249. this.constantPool.getConstLiteral(asLiteral(this._ngContentReservedSlots), true) :
  20250. null;
  20251. }
  20252. bindingContext() {
  20253. return `${this._bindingContext++}`;
  20254. }
  20255. templatePropertyBindings(templateIndex, attrs) {
  20256. const propertyBindings = [];
  20257. for (const input of attrs) {
  20258. if (!(input instanceof BoundAttribute)) {
  20259. continue;
  20260. }
  20261. const value = input.value.visit(this._valueConverter);
  20262. if (value === undefined) {
  20263. continue;
  20264. }
  20265. this.allocateBindingSlots(value);
  20266. if (value instanceof Interpolation) {
  20267. // Params typically contain attribute namespace and value sanitizer, which is applicable
  20268. // for regular HTML elements, but not applicable for <ng-template> (since props act as
  20269. // inputs to directives), so keep params array empty.
  20270. const params = [];
  20271. // prop="{{value}}" case
  20272. this.interpolatedUpdateInstruction(getPropertyInterpolationExpression(value), templateIndex, input.name, input, value, params);
  20273. }
  20274. else {
  20275. // [prop]="value" case
  20276. propertyBindings.push({
  20277. span: input.sourceSpan,
  20278. paramsOrFn: getBindingFunctionParams(() => this.convertPropertyBinding(value), input.name)
  20279. });
  20280. }
  20281. }
  20282. for (const propertyBinding of propertyBindings) {
  20283. this.updateInstructionWithAdvance(templateIndex, propertyBinding.span, Identifiers.property, propertyBinding.paramsOrFn);
  20284. }
  20285. }
  20286. // Bindings must only be resolved after all local refs have been visited, so all
  20287. // instructions are queued in callbacks that execute once the initial pass has completed.
  20288. // Otherwise, we wouldn't be able to support local refs that are defined after their
  20289. // bindings. e.g. {{ foo }} <div #foo></div>
  20290. instructionFn(fns, span, reference, paramsOrFn, prepend = false) {
  20291. fns[prepend ? 'unshift' : 'push']({ span, reference, paramsOrFn });
  20292. }
  20293. processStylingUpdateInstruction(elementIndex, instruction) {
  20294. let allocateBindingSlots = 0;
  20295. if (instruction) {
  20296. for (const call of instruction.calls) {
  20297. allocateBindingSlots += call.allocateBindingSlots;
  20298. this.updateInstructionWithAdvance(elementIndex, call.sourceSpan, instruction.reference, () => call.params(value => (call.supportsInterpolation && value instanceof Interpolation) ?
  20299. this.getUpdateInstructionArguments(value) :
  20300. this.convertPropertyBinding(value)));
  20301. }
  20302. }
  20303. return allocateBindingSlots;
  20304. }
  20305. creationInstruction(span, reference, paramsOrFn, prepend) {
  20306. this.instructionFn(this._creationCodeFns, span, reference, paramsOrFn || [], prepend);
  20307. }
  20308. updateInstructionWithAdvance(nodeIndex, span, reference, paramsOrFn) {
  20309. this.addAdvanceInstructionIfNecessary(nodeIndex, span);
  20310. this.updateInstruction(span, reference, paramsOrFn);
  20311. }
  20312. updateInstruction(span, reference, paramsOrFn) {
  20313. this.instructionFn(this._updateCodeFns, span, reference, paramsOrFn || []);
  20314. }
  20315. addAdvanceInstructionIfNecessary(nodeIndex, span) {
  20316. if (nodeIndex !== this._currentIndex) {
  20317. const delta = nodeIndex - this._currentIndex;
  20318. if (delta < 1) {
  20319. throw new Error('advance instruction can only go forwards');
  20320. }
  20321. this.instructionFn(this._updateCodeFns, span, Identifiers.advance, [literal(delta)]);
  20322. this._currentIndex = nodeIndex;
  20323. }
  20324. }
  20325. allocatePureFunctionSlots(numSlots) {
  20326. const originalSlots = this._pureFunctionSlots;
  20327. this._pureFunctionSlots += numSlots;
  20328. return originalSlots;
  20329. }
  20330. allocateBindingSlots(value) {
  20331. this._bindingSlots += value instanceof Interpolation ? value.expressions.length : 1;
  20332. }
  20333. /**
  20334. * Gets an expression that refers to the implicit receiver. The implicit
  20335. * receiver is always the root level context.
  20336. */
  20337. getImplicitReceiverExpr() {
  20338. if (this._implicitReceiverExpr) {
  20339. return this._implicitReceiverExpr;
  20340. }
  20341. return this._implicitReceiverExpr = this.level === 0 ?
  20342. variable(CONTEXT_NAME) :
  20343. this._bindingScope.getOrCreateSharedContextVar(0);
  20344. }
  20345. convertPropertyBinding(value) {
  20346. const convertedPropertyBinding = convertPropertyBinding(this, this.getImplicitReceiverExpr(), value, this.bindingContext());
  20347. const valExpr = convertedPropertyBinding.currValExpr;
  20348. this._tempVariables.push(...convertedPropertyBinding.stmts);
  20349. return valExpr;
  20350. }
  20351. /**
  20352. * Gets a list of argument expressions to pass to an update instruction expression. Also updates
  20353. * the temp variables state with temp variables that were identified as needing to be created
  20354. * while visiting the arguments.
  20355. * @param value The original expression we will be resolving an arguments list from.
  20356. */
  20357. getUpdateInstructionArguments(value) {
  20358. const { args, stmts } = convertUpdateArguments(this, this.getImplicitReceiverExpr(), value, this.bindingContext());
  20359. this._tempVariables.push(...stmts);
  20360. return args;
  20361. }
  20362. /**
  20363. * Prepares all attribute expression values for the `TAttributes` array.
  20364. *
  20365. * The purpose of this function is to properly construct an attributes array that
  20366. * is passed into the `elementStart` (or just `element`) functions. Because there
  20367. * are many different types of attributes, the array needs to be constructed in a
  20368. * special way so that `elementStart` can properly evaluate them.
  20369. *
  20370. * The format looks like this:
  20371. *
  20372. * ```
  20373. * attrs = [prop, value, prop2, value2,
  20374. * PROJECT_AS, selector,
  20375. * CLASSES, class1, class2,
  20376. * STYLES, style1, value1, style2, value2,
  20377. * BINDINGS, name1, name2, name3,
  20378. * TEMPLATE, name4, name5, name6,
  20379. * I18N, name7, name8, ...]
  20380. * ```
  20381. *
  20382. * Note that this function will fully ignore all synthetic (@foo) attribute values
  20383. * because those values are intended to always be generated as property instructions.
  20384. */
  20385. getAttributeExpressions(elementName, renderAttributes, inputs, outputs, styles, templateAttrs = [], boundI18nAttrs = []) {
  20386. const alreadySeen = new Set();
  20387. const attrExprs = [];
  20388. let ngProjectAsAttr;
  20389. for (const attr of renderAttributes) {
  20390. if (attr.name === NG_PROJECT_AS_ATTR_NAME) {
  20391. ngProjectAsAttr = attr;
  20392. }
  20393. // Note that static i18n attributes aren't in the i18n array,
  20394. // because they're treated in the same way as regular attributes.
  20395. if (attr.i18n) {
  20396. // When i18n attributes are present on elements with structural directives
  20397. // (e.g. `<div *ngIf title="Hello" i18n-title>`), we want to avoid generating
  20398. // duplicate i18n translation blocks for `ɵɵtemplate` and `ɵɵelement` instruction
  20399. // attributes. So we do a cache lookup to see if suitable i18n translation block
  20400. // already exists.
  20401. const { i18nVarRefsCache } = this._constants;
  20402. let i18nVarRef;
  20403. if (i18nVarRefsCache.has(attr.i18n)) {
  20404. i18nVarRef = i18nVarRefsCache.get(attr.i18n);
  20405. }
  20406. else {
  20407. i18nVarRef = this.i18nTranslate(attr.i18n);
  20408. i18nVarRefsCache.set(attr.i18n, i18nVarRef);
  20409. }
  20410. attrExprs.push(literal(attr.name), i18nVarRef);
  20411. }
  20412. else {
  20413. attrExprs.push(...getAttributeNameLiterals(attr.name), trustedConstAttribute(elementName, attr));
  20414. }
  20415. }
  20416. // Keep ngProjectAs next to the other name, value pairs so we can verify that we match
  20417. // ngProjectAs marker in the attribute name slot.
  20418. if (ngProjectAsAttr) {
  20419. attrExprs.push(...getNgProjectAsLiteral(ngProjectAsAttr));
  20420. }
  20421. function addAttrExpr(key, value) {
  20422. if (typeof key === 'string') {
  20423. if (!alreadySeen.has(key)) {
  20424. attrExprs.push(...getAttributeNameLiterals(key));
  20425. value !== undefined && attrExprs.push(value);
  20426. alreadySeen.add(key);
  20427. }
  20428. }
  20429. else {
  20430. attrExprs.push(literal(key));
  20431. }
  20432. }
  20433. // it's important that this occurs before BINDINGS and TEMPLATE because once `elementStart`
  20434. // comes across the BINDINGS or TEMPLATE markers then it will continue reading each value as
  20435. // as single property value cell by cell.
  20436. if (styles) {
  20437. styles.populateInitialStylingAttrs(attrExprs);
  20438. }
  20439. if (inputs.length || outputs.length) {
  20440. const attrsLengthBeforeInputs = attrExprs.length;
  20441. for (let i = 0; i < inputs.length; i++) {
  20442. const input = inputs[i];
  20443. // We don't want the animation and attribute bindings in the
  20444. // attributes array since they aren't used for directive matching.
  20445. if (input.type !== 4 /* BindingType.Animation */ && input.type !== 1 /* BindingType.Attribute */) {
  20446. addAttrExpr(input.name);
  20447. }
  20448. }
  20449. for (let i = 0; i < outputs.length; i++) {
  20450. const output = outputs[i];
  20451. if (output.type !== 1 /* ParsedEventType.Animation */) {
  20452. addAttrExpr(output.name);
  20453. }
  20454. }
  20455. // this is a cheap way of adding the marker only after all the input/output
  20456. // values have been filtered (by not including the animation ones) and added
  20457. // to the expressions. The marker is important because it tells the runtime
  20458. // code that this is where attributes without values start...
  20459. if (attrExprs.length !== attrsLengthBeforeInputs) {
  20460. attrExprs.splice(attrsLengthBeforeInputs, 0, literal(3 /* core.AttributeMarker.Bindings */));
  20461. }
  20462. }
  20463. if (templateAttrs.length) {
  20464. attrExprs.push(literal(4 /* core.AttributeMarker.Template */));
  20465. templateAttrs.forEach(attr => addAttrExpr(attr.name));
  20466. }
  20467. if (boundI18nAttrs.length) {
  20468. attrExprs.push(literal(6 /* core.AttributeMarker.I18n */));
  20469. boundI18nAttrs.forEach(attr => addAttrExpr(attr.name));
  20470. }
  20471. return attrExprs;
  20472. }
  20473. addToConsts(expression) {
  20474. if (isNull(expression)) {
  20475. return TYPED_NULL_EXPR;
  20476. }
  20477. const consts = this._constants.constExpressions;
  20478. // Try to reuse a literal that's already in the array, if possible.
  20479. for (let i = 0; i < consts.length; i++) {
  20480. if (consts[i].isEquivalent(expression)) {
  20481. return literal(i);
  20482. }
  20483. }
  20484. return literal(consts.push(expression) - 1);
  20485. }
  20486. addAttrsToConsts(attrs) {
  20487. return attrs.length > 0 ? this.addToConsts(literalArr(attrs)) : TYPED_NULL_EXPR;
  20488. }
  20489. prepareRefsArray(references) {
  20490. if (!references || references.length === 0) {
  20491. return TYPED_NULL_EXPR;
  20492. }
  20493. const refsParam = references.flatMap(reference => {
  20494. const slot = this.allocateDataSlot();
  20495. // Generate the update temporary.
  20496. const variableName = this._bindingScope.freshReferenceName();
  20497. const retrievalLevel = this.level;
  20498. const lhs = variable(variableName);
  20499. this._bindingScope.set(retrievalLevel, reference.name, lhs, 0 /* DeclarationPriority.DEFAULT */, (scope, relativeLevel) => {
  20500. // e.g. nextContext(2);
  20501. const nextContextStmt = relativeLevel > 0 ? [generateNextContextExpr(relativeLevel).toStmt()] : [];
  20502. // e.g. const $foo$ = reference(1);
  20503. const refExpr = lhs.set(importExpr(Identifiers.reference).callFn([literal(slot)]));
  20504. return nextContextStmt.concat(refExpr.toConstDecl());
  20505. }, true);
  20506. return [reference.name, reference.value];
  20507. });
  20508. return asLiteral(refsParam);
  20509. }
  20510. prepareListenerParameter(tagName, outputAst, index) {
  20511. return () => {
  20512. const eventName = outputAst.name;
  20513. const bindingFnName = outputAst.type === 1 /* ParsedEventType.Animation */ ?
  20514. // synthetic @listener.foo values are treated the exact same as are standard listeners
  20515. prepareSyntheticListenerFunctionName(eventName, outputAst.phase) :
  20516. sanitizeIdentifier(eventName);
  20517. const handlerName = `${this.templateName}_${tagName}_${bindingFnName}_${index}_listener`;
  20518. const scope = this._bindingScope.nestedScope(this._bindingScope.bindingLevel, EVENT_BINDING_SCOPE_GLOBALS);
  20519. return prepareEventListenerParameters(outputAst, handlerName, scope);
  20520. };
  20521. }
  20522. }
  20523. class ValueConverter extends AstMemoryEfficientTransformer {
  20524. constructor(constantPool, allocateSlot, allocatePureFunctionSlots, definePipe) {
  20525. super();
  20526. this.constantPool = constantPool;
  20527. this.allocateSlot = allocateSlot;
  20528. this.allocatePureFunctionSlots = allocatePureFunctionSlots;
  20529. this.definePipe = definePipe;
  20530. this._pipeBindExprs = [];
  20531. }
  20532. // AstMemoryEfficientTransformer
  20533. visitPipe(pipe, context) {
  20534. // Allocate a slot to create the pipe
  20535. const slot = this.allocateSlot();
  20536. const slotPseudoLocal = `PIPE:${slot}`;
  20537. // Allocate one slot for the result plus one slot per pipe argument
  20538. const pureFunctionSlot = this.allocatePureFunctionSlots(2 + pipe.args.length);
  20539. const target = new PropertyRead(pipe.span, pipe.sourceSpan, pipe.nameSpan, new ImplicitReceiver(pipe.span, pipe.sourceSpan), slotPseudoLocal);
  20540. const { identifier, isVarLength } = pipeBindingCallInfo(pipe.args);
  20541. this.definePipe(pipe.name, slotPseudoLocal, slot, importExpr(identifier));
  20542. const args = [pipe.exp, ...pipe.args];
  20543. const convertedArgs = isVarLength ?
  20544. this.visitAll([new LiteralArray(pipe.span, pipe.sourceSpan, args)]) :
  20545. this.visitAll(args);
  20546. const pipeBindExpr = new Call(pipe.span, pipe.sourceSpan, target, [
  20547. new LiteralPrimitive(pipe.span, pipe.sourceSpan, slot),
  20548. new LiteralPrimitive(pipe.span, pipe.sourceSpan, pureFunctionSlot),
  20549. ...convertedArgs,
  20550. ], null);
  20551. this._pipeBindExprs.push(pipeBindExpr);
  20552. return pipeBindExpr;
  20553. }
  20554. updatePipeSlotOffsets(bindingSlots) {
  20555. this._pipeBindExprs.forEach((pipe) => {
  20556. // update the slot offset arg (index 1) to account for binding slots
  20557. const slotOffset = pipe.args[1];
  20558. slotOffset.value += bindingSlots;
  20559. });
  20560. }
  20561. visitLiteralArray(array, context) {
  20562. return new BuiltinFunctionCall(array.span, array.sourceSpan, this.visitAll(array.expressions), values => {
  20563. // If the literal has calculated (non-literal) elements transform it into
  20564. // calls to literal factories that compose the literal and will cache intermediate
  20565. // values.
  20566. const literal = literalArr(values);
  20567. return getLiteralFactory(this.constantPool, literal, this.allocatePureFunctionSlots);
  20568. });
  20569. }
  20570. visitLiteralMap(map, context) {
  20571. return new BuiltinFunctionCall(map.span, map.sourceSpan, this.visitAll(map.values), values => {
  20572. // If the literal has calculated (non-literal) elements transform it into
  20573. // calls to literal factories that compose the literal and will cache intermediate
  20574. // values.
  20575. const literal = literalMap(values.map((value, index) => ({ key: map.keys[index].key, value, quoted: map.keys[index].quoted })));
  20576. return getLiteralFactory(this.constantPool, literal, this.allocatePureFunctionSlots);
  20577. });
  20578. }
  20579. }
  20580. // Pipes always have at least one parameter, the value they operate on
  20581. const pipeBindingIdentifiers = [Identifiers.pipeBind1, Identifiers.pipeBind2, Identifiers.pipeBind3, Identifiers.pipeBind4];
  20582. function pipeBindingCallInfo(args) {
  20583. const identifier = pipeBindingIdentifiers[args.length];
  20584. return {
  20585. identifier: identifier || Identifiers.pipeBindV,
  20586. isVarLength: !identifier,
  20587. };
  20588. }
  20589. const pureFunctionIdentifiers = [
  20590. Identifiers.pureFunction0, Identifiers.pureFunction1, Identifiers.pureFunction2, Identifiers.pureFunction3, Identifiers.pureFunction4,
  20591. Identifiers.pureFunction5, Identifiers.pureFunction6, Identifiers.pureFunction7, Identifiers.pureFunction8
  20592. ];
  20593. function pureFunctionCallInfo(args) {
  20594. const identifier = pureFunctionIdentifiers[args.length];
  20595. return {
  20596. identifier: identifier || Identifiers.pureFunctionV,
  20597. isVarLength: !identifier,
  20598. };
  20599. }
  20600. // e.g. x(2);
  20601. function generateNextContextExpr(relativeLevelDiff) {
  20602. return importExpr(Identifiers.nextContext)
  20603. .callFn(relativeLevelDiff > 1 ? [literal(relativeLevelDiff)] : []);
  20604. }
  20605. function getLiteralFactory(constantPool, literal$1, allocateSlots) {
  20606. const { literalFactory, literalFactoryArguments } = constantPool.getLiteralFactory(literal$1);
  20607. // Allocate 1 slot for the result plus 1 per argument
  20608. const startSlot = allocateSlots(1 + literalFactoryArguments.length);
  20609. const { identifier, isVarLength } = pureFunctionCallInfo(literalFactoryArguments);
  20610. // Literal factories are pure functions that only need to be re-invoked when the parameters
  20611. // change.
  20612. const args = [literal(startSlot), literalFactory];
  20613. if (isVarLength) {
  20614. args.push(literalArr(literalFactoryArguments));
  20615. }
  20616. else {
  20617. args.push(...literalFactoryArguments);
  20618. }
  20619. return importExpr(identifier).callFn(args);
  20620. }
  20621. /**
  20622. * Gets an array of literals that can be added to an expression
  20623. * to represent the name and namespace of an attribute. E.g.
  20624. * `:xlink:href` turns into `[AttributeMarker.NamespaceURI, 'xlink', 'href']`.
  20625. *
  20626. * @param name Name of the attribute, including the namespace.
  20627. */
  20628. function getAttributeNameLiterals(name) {
  20629. const [attributeNamespace, attributeName] = splitNsName(name);
  20630. const nameLiteral = literal(attributeName);
  20631. if (attributeNamespace) {
  20632. return [
  20633. literal(0 /* core.AttributeMarker.NamespaceURI */), literal(attributeNamespace), nameLiteral
  20634. ];
  20635. }
  20636. return [nameLiteral];
  20637. }
  20638. /** The prefix used to get a shared context in BindingScope's map. */
  20639. const SHARED_CONTEXT_KEY = '$$shared_ctx$$';
  20640. class BindingScope {
  20641. static createRootScope() {
  20642. return new BindingScope();
  20643. }
  20644. constructor(bindingLevel = 0, parent = null, globals) {
  20645. this.bindingLevel = bindingLevel;
  20646. this.parent = parent;
  20647. this.globals = globals;
  20648. /** Keeps a map from local variables to their BindingData. */
  20649. this.map = new Map();
  20650. this.referenceNameIndex = 0;
  20651. this.restoreViewVariable = null;
  20652. this.usesRestoredViewContext = false;
  20653. if (globals !== undefined) {
  20654. for (const name of globals) {
  20655. this.set(0, name, variable(name));
  20656. }
  20657. }
  20658. }
  20659. get(name) {
  20660. let current = this;
  20661. while (current) {
  20662. let value = current.map.get(name);
  20663. if (value != null) {
  20664. if (current !== this) {
  20665. // make a local copy and reset the `declare` state
  20666. value = {
  20667. retrievalLevel: value.retrievalLevel,
  20668. lhs: value.lhs,
  20669. declareLocalCallback: value.declareLocalCallback,
  20670. declare: false,
  20671. priority: value.priority
  20672. };
  20673. // Cache the value locally.
  20674. this.map.set(name, value);
  20675. // Possibly generate a shared context var
  20676. this.maybeGenerateSharedContextVar(value);
  20677. this.maybeRestoreView();
  20678. }
  20679. if (value.declareLocalCallback && !value.declare) {
  20680. value.declare = true;
  20681. }
  20682. return value.lhs;
  20683. }
  20684. current = current.parent;
  20685. }
  20686. // If we get to this point, we are looking for a property on the top level component
  20687. // - If level === 0, we are on the top and don't need to re-declare `ctx`.
  20688. // - If level > 0, we are in an embedded view. We need to retrieve the name of the
  20689. // local var we used to store the component context, e.g. const $comp$ = x();
  20690. return this.bindingLevel === 0 ? null : this.getComponentProperty(name);
  20691. }
  20692. /**
  20693. * Create a local variable for later reference.
  20694. *
  20695. * @param retrievalLevel The level from which this value can be retrieved
  20696. * @param name Name of the variable.
  20697. * @param lhs AST representing the left hand side of the `let lhs = rhs;`.
  20698. * @param priority The sorting priority of this var
  20699. * @param declareLocalCallback The callback to invoke when declaring this local var
  20700. * @param localRef Whether or not this is a local ref
  20701. */
  20702. set(retrievalLevel, name, lhs, priority = 0 /* DeclarationPriority.DEFAULT */, declareLocalCallback, localRef) {
  20703. if (this.map.has(name)) {
  20704. if (localRef) {
  20705. // Do not throw an error if it's a local ref and do not update existing value,
  20706. // so the first defined ref is always returned.
  20707. return this;
  20708. }
  20709. error(`The name ${name} is already defined in scope to be ${this.map.get(name)}`);
  20710. }
  20711. this.map.set(name, {
  20712. retrievalLevel: retrievalLevel,
  20713. lhs: lhs,
  20714. declare: false,
  20715. declareLocalCallback: declareLocalCallback,
  20716. priority: priority,
  20717. });
  20718. return this;
  20719. }
  20720. // Implemented as part of LocalResolver.
  20721. getLocal(name) {
  20722. return this.get(name);
  20723. }
  20724. // Implemented as part of LocalResolver.
  20725. notifyImplicitReceiverUse() {
  20726. if (this.bindingLevel !== 0) {
  20727. // Since the implicit receiver is accessed in an embedded view, we need to
  20728. // ensure that we declare a shared context variable for the current template
  20729. // in the update variables.
  20730. this.map.get(SHARED_CONTEXT_KEY + 0).declare = true;
  20731. }
  20732. }
  20733. nestedScope(level, globals) {
  20734. const newScope = new BindingScope(level, this, globals);
  20735. if (level > 0)
  20736. newScope.generateSharedContextVar(0);
  20737. return newScope;
  20738. }
  20739. /**
  20740. * Gets or creates a shared context variable and returns its expression. Note that
  20741. * this does not mean that the shared variable will be declared. Variables in the
  20742. * binding scope will be only declared if they are used.
  20743. */
  20744. getOrCreateSharedContextVar(retrievalLevel) {
  20745. const bindingKey = SHARED_CONTEXT_KEY + retrievalLevel;
  20746. if (!this.map.has(bindingKey)) {
  20747. this.generateSharedContextVar(retrievalLevel);
  20748. }
  20749. // Shared context variables are always generated as "ReadVarExpr".
  20750. return this.map.get(bindingKey).lhs;
  20751. }
  20752. getSharedContextName(retrievalLevel) {
  20753. const sharedCtxObj = this.map.get(SHARED_CONTEXT_KEY + retrievalLevel);
  20754. // Shared context variables are always generated as "ReadVarExpr".
  20755. return sharedCtxObj && sharedCtxObj.declare ? sharedCtxObj.lhs : null;
  20756. }
  20757. maybeGenerateSharedContextVar(value) {
  20758. if (value.priority === 1 /* DeclarationPriority.CONTEXT */ &&
  20759. value.retrievalLevel < this.bindingLevel) {
  20760. const sharedCtxObj = this.map.get(SHARED_CONTEXT_KEY + value.retrievalLevel);
  20761. if (sharedCtxObj) {
  20762. sharedCtxObj.declare = true;
  20763. }
  20764. else {
  20765. this.generateSharedContextVar(value.retrievalLevel);
  20766. }
  20767. }
  20768. }
  20769. generateSharedContextVar(retrievalLevel) {
  20770. const lhs = variable(CONTEXT_NAME + this.freshReferenceName());
  20771. this.map.set(SHARED_CONTEXT_KEY + retrievalLevel, {
  20772. retrievalLevel: retrievalLevel,
  20773. lhs: lhs,
  20774. declareLocalCallback: (scope, relativeLevel) => {
  20775. // const ctx_r0 = nextContext(2);
  20776. return [lhs.set(generateNextContextExpr(relativeLevel)).toConstDecl()];
  20777. },
  20778. declare: false,
  20779. priority: 2 /* DeclarationPriority.SHARED_CONTEXT */,
  20780. });
  20781. }
  20782. getComponentProperty(name) {
  20783. const componentValue = this.map.get(SHARED_CONTEXT_KEY + 0);
  20784. componentValue.declare = true;
  20785. this.maybeRestoreView();
  20786. return componentValue.lhs.prop(name);
  20787. }
  20788. maybeRestoreView() {
  20789. // View restoration is required for listener instructions inside embedded views, because
  20790. // they only run in creation mode and they can have references to the context object.
  20791. // If the context object changes in update mode, the reference will be incorrect, because
  20792. // it was established during creation.
  20793. if (this.isListenerScope()) {
  20794. if (!this.parent.restoreViewVariable) {
  20795. // parent saves variable to generate a shared `const $s$ = getCurrentView();` instruction
  20796. this.parent.restoreViewVariable = variable(this.parent.freshReferenceName());
  20797. }
  20798. this.restoreViewVariable = this.parent.restoreViewVariable;
  20799. }
  20800. }
  20801. restoreViewStatement() {
  20802. if (this.restoreViewVariable) {
  20803. const restoreCall = invokeInstruction(null, Identifiers.restoreView, [this.restoreViewVariable]);
  20804. // Either `const restoredCtx = restoreView($state$);` or `restoreView($state$);`
  20805. // depending on whether it is being used.
  20806. return this.usesRestoredViewContext ?
  20807. variable(RESTORED_VIEW_CONTEXT_NAME).set(restoreCall).toConstDecl() :
  20808. restoreCall.toStmt();
  20809. }
  20810. return null;
  20811. }
  20812. viewSnapshotStatements() {
  20813. // const $state$ = getCurrentView();
  20814. return this.restoreViewVariable ?
  20815. [
  20816. this.restoreViewVariable.set(invokeInstruction(null, Identifiers.getCurrentView, [])).toConstDecl()
  20817. ] :
  20818. [];
  20819. }
  20820. isListenerScope() {
  20821. return this.parent && this.parent.bindingLevel === this.bindingLevel;
  20822. }
  20823. variableDeclarations() {
  20824. let currentContextLevel = 0;
  20825. return Array.from(this.map.values())
  20826. .filter(value => value.declare)
  20827. .sort((a, b) => b.retrievalLevel - a.retrievalLevel || b.priority - a.priority)
  20828. .reduce((stmts, value) => {
  20829. const levelDiff = this.bindingLevel - value.retrievalLevel;
  20830. const currStmts = value.declareLocalCallback(this, levelDiff - currentContextLevel);
  20831. currentContextLevel = levelDiff;
  20832. return stmts.concat(currStmts);
  20833. }, []);
  20834. }
  20835. freshReferenceName() {
  20836. let current = this;
  20837. // Find the top scope as it maintains the global reference count
  20838. while (current.parent)
  20839. current = current.parent;
  20840. const ref = `${REFERENCE_PREFIX}${current.referenceNameIndex++}`;
  20841. return ref;
  20842. }
  20843. hasRestoreViewVariable() {
  20844. return !!this.restoreViewVariable;
  20845. }
  20846. notifyRestoredViewContextUse() {
  20847. this.usesRestoredViewContext = true;
  20848. }
  20849. }
  20850. /**
  20851. * Creates a `CssSelector` given a tag name and a map of attributes
  20852. */
  20853. function createCssSelector(elementName, attributes) {
  20854. const cssSelector = new CssSelector();
  20855. const elementNameNoNs = splitNsName(elementName)[1];
  20856. cssSelector.setElement(elementNameNoNs);
  20857. Object.getOwnPropertyNames(attributes).forEach((name) => {
  20858. const nameNoNs = splitNsName(name)[1];
  20859. const value = attributes[name];
  20860. cssSelector.addAttribute(nameNoNs, value);
  20861. if (name.toLowerCase() === 'class') {
  20862. const classes = value.trim().split(/\s+/);
  20863. classes.forEach(className => cssSelector.addClassName(className));
  20864. }
  20865. });
  20866. return cssSelector;
  20867. }
  20868. /**
  20869. * Creates an array of expressions out of an `ngProjectAs` attributes
  20870. * which can be added to the instruction parameters.
  20871. */
  20872. function getNgProjectAsLiteral(attribute) {
  20873. // Parse the attribute value into a CssSelectorList. Note that we only take the
  20874. // first selector, because we don't support multiple selectors in ngProjectAs.
  20875. const parsedR3Selector = parseSelectorToR3Selector(attribute.value)[0];
  20876. return [literal(5 /* core.AttributeMarker.ProjectAs */), asLiteral(parsedR3Selector)];
  20877. }
  20878. /**
  20879. * Gets the instruction to generate for an interpolated property
  20880. * @param interpolation An Interpolation AST
  20881. */
  20882. function getPropertyInterpolationExpression(interpolation) {
  20883. switch (getInterpolationArgsLength(interpolation)) {
  20884. case 1:
  20885. return Identifiers.propertyInterpolate;
  20886. case 3:
  20887. return Identifiers.propertyInterpolate1;
  20888. case 5:
  20889. return Identifiers.propertyInterpolate2;
  20890. case 7:
  20891. return Identifiers.propertyInterpolate3;
  20892. case 9:
  20893. return Identifiers.propertyInterpolate4;
  20894. case 11:
  20895. return Identifiers.propertyInterpolate5;
  20896. case 13:
  20897. return Identifiers.propertyInterpolate6;
  20898. case 15:
  20899. return Identifiers.propertyInterpolate7;
  20900. case 17:
  20901. return Identifiers.propertyInterpolate8;
  20902. default:
  20903. return Identifiers.propertyInterpolateV;
  20904. }
  20905. }
  20906. /**
  20907. * Gets the instruction to generate for an interpolated attribute
  20908. * @param interpolation An Interpolation AST
  20909. */
  20910. function getAttributeInterpolationExpression(interpolation) {
  20911. switch (getInterpolationArgsLength(interpolation)) {
  20912. case 3:
  20913. return Identifiers.attributeInterpolate1;
  20914. case 5:
  20915. return Identifiers.attributeInterpolate2;
  20916. case 7:
  20917. return Identifiers.attributeInterpolate3;
  20918. case 9:
  20919. return Identifiers.attributeInterpolate4;
  20920. case 11:
  20921. return Identifiers.attributeInterpolate5;
  20922. case 13:
  20923. return Identifiers.attributeInterpolate6;
  20924. case 15:
  20925. return Identifiers.attributeInterpolate7;
  20926. case 17:
  20927. return Identifiers.attributeInterpolate8;
  20928. default:
  20929. return Identifiers.attributeInterpolateV;
  20930. }
  20931. }
  20932. /**
  20933. * Gets the instruction to generate for interpolated text.
  20934. * @param interpolation An Interpolation AST
  20935. */
  20936. function getTextInterpolationExpression(interpolation) {
  20937. switch (getInterpolationArgsLength(interpolation)) {
  20938. case 1:
  20939. return Identifiers.textInterpolate;
  20940. case 3:
  20941. return Identifiers.textInterpolate1;
  20942. case 5:
  20943. return Identifiers.textInterpolate2;
  20944. case 7:
  20945. return Identifiers.textInterpolate3;
  20946. case 9:
  20947. return Identifiers.textInterpolate4;
  20948. case 11:
  20949. return Identifiers.textInterpolate5;
  20950. case 13:
  20951. return Identifiers.textInterpolate6;
  20952. case 15:
  20953. return Identifiers.textInterpolate7;
  20954. case 17:
  20955. return Identifiers.textInterpolate8;
  20956. default:
  20957. return Identifiers.textInterpolateV;
  20958. }
  20959. }
  20960. /**
  20961. * Parse a template into render3 `Node`s and additional metadata, with no other dependencies.
  20962. *
  20963. * @param template text of the template to parse
  20964. * @param templateUrl URL to use for source mapping of the parsed template
  20965. * @param options options to modify how the template is parsed
  20966. */
  20967. function parseTemplate(template, templateUrl, options = {}) {
  20968. const { interpolationConfig, preserveWhitespaces, enableI18nLegacyMessageIdFormat } = options;
  20969. const bindingParser = makeBindingParser(interpolationConfig);
  20970. const htmlParser = new HtmlParser();
  20971. const parseResult = htmlParser.parse(template, templateUrl, { leadingTriviaChars: LEADING_TRIVIA_CHARS, ...options, tokenizeExpansionForms: true });
  20972. if (!options.alwaysAttemptHtmlToR3AstConversion && parseResult.errors &&
  20973. parseResult.errors.length > 0) {
  20974. const parsedTemplate = {
  20975. interpolationConfig,
  20976. preserveWhitespaces,
  20977. errors: parseResult.errors,
  20978. nodes: [],
  20979. styleUrls: [],
  20980. styles: [],
  20981. ngContentSelectors: []
  20982. };
  20983. if (options.collectCommentNodes) {
  20984. parsedTemplate.commentNodes = [];
  20985. }
  20986. return parsedTemplate;
  20987. }
  20988. let rootNodes = parseResult.rootNodes;
  20989. // process i18n meta information (scan attributes, generate ids)
  20990. // before we run whitespace removal process, because existing i18n
  20991. // extraction process (ng extract-i18n) relies on a raw content to generate
  20992. // message ids
  20993. const i18nMetaVisitor = new I18nMetaVisitor(interpolationConfig, /* keepI18nAttrs */ !preserveWhitespaces, enableI18nLegacyMessageIdFormat);
  20994. const i18nMetaResult = i18nMetaVisitor.visitAllWithErrors(rootNodes);
  20995. if (!options.alwaysAttemptHtmlToR3AstConversion && i18nMetaResult.errors &&
  20996. i18nMetaResult.errors.length > 0) {
  20997. const parsedTemplate = {
  20998. interpolationConfig,
  20999. preserveWhitespaces,
  21000. errors: i18nMetaResult.errors,
  21001. nodes: [],
  21002. styleUrls: [],
  21003. styles: [],
  21004. ngContentSelectors: []
  21005. };
  21006. if (options.collectCommentNodes) {
  21007. parsedTemplate.commentNodes = [];
  21008. }
  21009. return parsedTemplate;
  21010. }
  21011. rootNodes = i18nMetaResult.rootNodes;
  21012. if (!preserveWhitespaces) {
  21013. rootNodes = visitAll(new WhitespaceVisitor(), rootNodes);
  21014. // run i18n meta visitor again in case whitespaces are removed (because that might affect
  21015. // generated i18n message content) and first pass indicated that i18n content is present in a
  21016. // template. During this pass i18n IDs generated at the first pass will be preserved, so we can
  21017. // mimic existing extraction process (ng extract-i18n)
  21018. if (i18nMetaVisitor.hasI18nMeta) {
  21019. rootNodes = visitAll(new I18nMetaVisitor(interpolationConfig, /* keepI18nAttrs */ false), rootNodes);
  21020. }
  21021. }
  21022. const { nodes, errors, styleUrls, styles, ngContentSelectors, commentNodes } = htmlAstToRender3Ast(rootNodes, bindingParser, { collectCommentNodes: !!options.collectCommentNodes });
  21023. errors.push(...parseResult.errors, ...i18nMetaResult.errors);
  21024. const parsedTemplate = {
  21025. interpolationConfig,
  21026. preserveWhitespaces,
  21027. errors: errors.length > 0 ? errors : null,
  21028. nodes,
  21029. styleUrls,
  21030. styles,
  21031. ngContentSelectors
  21032. };
  21033. if (options.collectCommentNodes) {
  21034. parsedTemplate.commentNodes = commentNodes;
  21035. }
  21036. return parsedTemplate;
  21037. }
  21038. const elementRegistry = new DomElementSchemaRegistry();
  21039. /**
  21040. * Construct a `BindingParser` with a default configuration.
  21041. */
  21042. function makeBindingParser(interpolationConfig = DEFAULT_INTERPOLATION_CONFIG) {
  21043. return new BindingParser(new Parser$1(new Lexer()), interpolationConfig, elementRegistry, []);
  21044. }
  21045. function resolveSanitizationFn(context, isAttribute) {
  21046. switch (context) {
  21047. case SecurityContext.HTML:
  21048. return importExpr(Identifiers.sanitizeHtml);
  21049. case SecurityContext.SCRIPT:
  21050. return importExpr(Identifiers.sanitizeScript);
  21051. case SecurityContext.STYLE:
  21052. // the compiler does not fill in an instruction for [style.prop?] binding
  21053. // values because the style algorithm knows internally what props are subject
  21054. // to sanitization (only [attr.style] values are explicitly sanitized)
  21055. return isAttribute ? importExpr(Identifiers.sanitizeStyle) : null;
  21056. case SecurityContext.URL:
  21057. return importExpr(Identifiers.sanitizeUrl);
  21058. case SecurityContext.RESOURCE_URL:
  21059. return importExpr(Identifiers.sanitizeResourceUrl);
  21060. default:
  21061. return null;
  21062. }
  21063. }
  21064. function trustedConstAttribute(tagName, attr) {
  21065. const value = asLiteral(attr.value);
  21066. if (isTrustedTypesSink(tagName, attr.name)) {
  21067. switch (elementRegistry.securityContext(tagName, attr.name, /* isAttribute */ true)) {
  21068. case SecurityContext.HTML:
  21069. return taggedTemplate(importExpr(Identifiers.trustConstantHtml), new TemplateLiteral([new TemplateLiteralElement(attr.value)], []), undefined, attr.valueSpan);
  21070. // NB: no SecurityContext.SCRIPT here, as the corresponding tags are stripped by the compiler.
  21071. case SecurityContext.RESOURCE_URL:
  21072. return taggedTemplate(importExpr(Identifiers.trustConstantResourceUrl), new TemplateLiteral([new TemplateLiteralElement(attr.value)], []), undefined, attr.valueSpan);
  21073. default:
  21074. return value;
  21075. }
  21076. }
  21077. else {
  21078. return value;
  21079. }
  21080. }
  21081. function isSingleElementTemplate(children) {
  21082. return children.length === 1 && children[0] instanceof Element$1;
  21083. }
  21084. function isTextNode(node) {
  21085. return node instanceof Text$3 || node instanceof BoundText || node instanceof Icu$1;
  21086. }
  21087. function isIframeElement(tagName) {
  21088. return tagName.toLowerCase() === 'iframe';
  21089. }
  21090. function hasTextChildrenOnly(children) {
  21091. return children.every(isTextNode);
  21092. }
  21093. function getBindingFunctionParams(deferredParams, name, eagerParams) {
  21094. return () => {
  21095. const value = deferredParams();
  21096. const fnParams = Array.isArray(value) ? value : [value];
  21097. if (eagerParams) {
  21098. fnParams.push(...eagerParams);
  21099. }
  21100. if (name) {
  21101. // We want the property name to always be the first function parameter.
  21102. fnParams.unshift(literal(name));
  21103. }
  21104. return fnParams;
  21105. };
  21106. }
  21107. /** Name of the global variable that is used to determine if we use Closure translations or not */
  21108. const NG_I18N_CLOSURE_MODE = 'ngI18nClosureMode';
  21109. /**
  21110. * Generate statements that define a given translation message.
  21111. *
  21112. * ```
  21113. * var I18N_1;
  21114. * if (typeof ngI18nClosureMode !== undefined && ngI18nClosureMode) {
  21115. * var MSG_EXTERNAL_XXX = goog.getMsg(
  21116. * "Some message with {$interpolation}!",
  21117. * { "interpolation": "\uFFFD0\uFFFD" }
  21118. * );
  21119. * I18N_1 = MSG_EXTERNAL_XXX;
  21120. * }
  21121. * else {
  21122. * I18N_1 = $localize`Some message with ${'\uFFFD0\uFFFD'}!`;
  21123. * }
  21124. * ```
  21125. *
  21126. * @param message The original i18n AST message node
  21127. * @param variable The variable that will be assigned the translation, e.g. `I18N_1`.
  21128. * @param closureVar The variable for Closure `goog.getMsg` calls, e.g. `MSG_EXTERNAL_XXX`.
  21129. * @param params Object mapping placeholder names to their values (e.g.
  21130. * `{ "interpolation": "\uFFFD0\uFFFD" }`).
  21131. * @param transformFn Optional transformation function that will be applied to the translation (e.g.
  21132. * post-processing).
  21133. * @returns An array of statements that defined a given translation.
  21134. */
  21135. function getTranslationDeclStmts(message, variable, closureVar, params = {}, transformFn) {
  21136. const statements = [
  21137. declareI18nVariable(variable),
  21138. ifStmt(createClosureModeGuard(), createGoogleGetMsgStatements(variable, message, closureVar, params), createLocalizeStatements(variable, message, formatI18nPlaceholderNamesInMap(params, /* useCamelCase */ false))),
  21139. ];
  21140. if (transformFn) {
  21141. statements.push(new ExpressionStatement(variable.set(transformFn(variable))));
  21142. }
  21143. return statements;
  21144. }
  21145. /**
  21146. * Create the expression that will be used to guard the closure mode block
  21147. * It is equivalent to:
  21148. *
  21149. * ```
  21150. * typeof ngI18nClosureMode !== undefined && ngI18nClosureMode
  21151. * ```
  21152. */
  21153. function createClosureModeGuard() {
  21154. return typeofExpr(variable(NG_I18N_CLOSURE_MODE))
  21155. .notIdentical(literal('undefined', STRING_TYPE))
  21156. .and(variable(NG_I18N_CLOSURE_MODE));
  21157. }
  21158. // This regex matches any binding names that contain the "attr." prefix, e.g. "attr.required"
  21159. // If there is a match, the first matching group will contain the attribute name to bind.
  21160. const ATTR_REGEX = /attr\.([^\]]+)/;
  21161. const COMPONENT_VARIABLE = '%COMP%';
  21162. const HOST_ATTR = `_nghost-${COMPONENT_VARIABLE}`;
  21163. const CONTENT_ATTR = `_ngcontent-${COMPONENT_VARIABLE}`;
  21164. function baseDirectiveFields(meta, constantPool, bindingParser) {
  21165. const definitionMap = new DefinitionMap();
  21166. const selectors = parseSelectorToR3Selector(meta.selector);
  21167. // e.g. `type: MyDirective`
  21168. definitionMap.set('type', meta.type.value);
  21169. // e.g. `selectors: [['', 'someDir', '']]`
  21170. if (selectors.length > 0) {
  21171. definitionMap.set('selectors', asLiteral(selectors));
  21172. }
  21173. if (meta.queries.length > 0) {
  21174. // e.g. `contentQueries: (rf, ctx, dirIndex) => { ... }
  21175. definitionMap.set('contentQueries', createContentQueriesFunction(meta.queries, constantPool, meta.name));
  21176. }
  21177. if (meta.viewQueries.length) {
  21178. definitionMap.set('viewQuery', createViewQueriesFunction(meta.viewQueries, constantPool, meta.name));
  21179. }
  21180. // e.g. `hostBindings: (rf, ctx) => { ... }
  21181. definitionMap.set('hostBindings', createHostBindingsFunction(meta.host, meta.typeSourceSpan, bindingParser, constantPool, meta.selector || '', meta.name, definitionMap));
  21182. // e.g 'inputs: {a: 'a'}`
  21183. definitionMap.set('inputs', conditionallyCreateDirectiveBindingLiteral(meta.inputs, true));
  21184. // e.g 'outputs: {a: 'a'}`
  21185. definitionMap.set('outputs', conditionallyCreateDirectiveBindingLiteral(meta.outputs));
  21186. if (meta.exportAs !== null) {
  21187. definitionMap.set('exportAs', literalArr(meta.exportAs.map(e => literal(e))));
  21188. }
  21189. if (meta.isStandalone) {
  21190. definitionMap.set('standalone', literal(true));
  21191. }
  21192. return definitionMap;
  21193. }
  21194. /**
  21195. * Add features to the definition map.
  21196. */
  21197. function addFeatures(definitionMap, meta) {
  21198. // e.g. `features: [NgOnChangesFeature]`
  21199. const features = [];
  21200. const providers = meta.providers;
  21201. const viewProviders = meta.viewProviders;
  21202. if (providers || viewProviders) {
  21203. const args = [providers || new LiteralArrayExpr([])];
  21204. if (viewProviders) {
  21205. args.push(viewProviders);
  21206. }
  21207. features.push(importExpr(Identifiers.ProvidersFeature).callFn(args));
  21208. }
  21209. if (meta.usesInheritance) {
  21210. features.push(importExpr(Identifiers.InheritDefinitionFeature));
  21211. }
  21212. if (meta.fullInheritance) {
  21213. features.push(importExpr(Identifiers.CopyDefinitionFeature));
  21214. }
  21215. if (meta.lifecycle.usesOnChanges) {
  21216. features.push(importExpr(Identifiers.NgOnChangesFeature));
  21217. }
  21218. // TODO: better way of differentiating component vs directive metadata.
  21219. if (meta.hasOwnProperty('template') && meta.isStandalone) {
  21220. features.push(importExpr(Identifiers.StandaloneFeature));
  21221. }
  21222. if (meta.hostDirectives?.length) {
  21223. features.push(importExpr(Identifiers.HostDirectivesFeature).callFn([createHostDirectivesFeatureArg(meta.hostDirectives)]));
  21224. }
  21225. if (features.length) {
  21226. definitionMap.set('features', literalArr(features));
  21227. }
  21228. }
  21229. /**
  21230. * Compile a directive for the render3 runtime as defined by the `R3DirectiveMetadata`.
  21231. */
  21232. function compileDirectiveFromMetadata(meta, constantPool, bindingParser) {
  21233. const definitionMap = baseDirectiveFields(meta, constantPool, bindingParser);
  21234. addFeatures(definitionMap, meta);
  21235. const expression = importExpr(Identifiers.defineDirective).callFn([definitionMap.toLiteralMap()], undefined, true);
  21236. const type = createDirectiveType(meta);
  21237. return { expression, type, statements: [] };
  21238. }
  21239. /**
  21240. * Compile a component for the render3 runtime as defined by the `R3ComponentMetadata`.
  21241. */
  21242. function compileComponentFromMetadata(meta, constantPool, bindingParser) {
  21243. const definitionMap = baseDirectiveFields(meta, constantPool, bindingParser);
  21244. addFeatures(definitionMap, meta);
  21245. const selector = meta.selector && CssSelector.parse(meta.selector);
  21246. const firstSelector = selector && selector[0];
  21247. // e.g. `attr: ["class", ".my.app"]`
  21248. // This is optional an only included if the first selector of a component specifies attributes.
  21249. if (firstSelector) {
  21250. const selectorAttributes = firstSelector.getAttrs();
  21251. if (selectorAttributes.length) {
  21252. definitionMap.set('attrs', constantPool.getConstLiteral(literalArr(selectorAttributes.map(value => value != null ? literal(value) : literal(undefined))),
  21253. /* forceShared */ true));
  21254. }
  21255. }
  21256. // e.g. `template: function MyComponent_Template(_ctx, _cm) {...}`
  21257. const templateTypeName = meta.name;
  21258. const templateName = templateTypeName ? `${templateTypeName}_Template` : null;
  21259. const changeDetection = meta.changeDetection;
  21260. // Template compilation is currently conditional as we're in the process of rewriting it.
  21261. if (!USE_TEMPLATE_PIPELINE) {
  21262. // This is the main path currently used in compilation, which compiles the template with the
  21263. // legacy `TemplateDefinitionBuilder`.
  21264. const template = meta.template;
  21265. const templateBuilder = new TemplateDefinitionBuilder(constantPool, BindingScope.createRootScope(), 0, templateTypeName, null, null, templateName, Identifiers.namespaceHTML, meta.relativeContextFilePath, meta.i18nUseExternalIds);
  21266. const templateFunctionExpression = templateBuilder.buildTemplateFunction(template.nodes, []);
  21267. // We need to provide this so that dynamically generated components know what
  21268. // projected content blocks to pass through to the component when it is
  21269. // instantiated.
  21270. const ngContentSelectors = templateBuilder.getNgContentSelectors();
  21271. if (ngContentSelectors) {
  21272. definitionMap.set('ngContentSelectors', ngContentSelectors);
  21273. }
  21274. // e.g. `decls: 2`
  21275. // definitionMap.set('decls', o.literal(tpl.root.decls!));
  21276. definitionMap.set('decls', literal(templateBuilder.getConstCount()));
  21277. // e.g. `vars: 2`
  21278. // definitionMap.set('vars', o.literal(tpl.root.vars!));
  21279. definitionMap.set('vars', literal(templateBuilder.getVarCount()));
  21280. // Generate `consts` section of ComponentDef:
  21281. // - either as an array:
  21282. // `consts: [['one', 'two'], ['three', 'four']]`
  21283. // - or as a factory function in case additional statements are present (to support i18n):
  21284. // `consts: function() { var i18n_0; if (ngI18nClosureMode) {...} else {...} return [i18n_0];
  21285. // }`
  21286. const { constExpressions, prepareStatements } = templateBuilder.getConsts();
  21287. if (constExpressions.length > 0) {
  21288. let constsExpr = literalArr(constExpressions);
  21289. // Prepare statements are present - turn `consts` into a function.
  21290. if (prepareStatements.length > 0) {
  21291. constsExpr = fn([], [...prepareStatements, new ReturnStatement(constsExpr)]);
  21292. }
  21293. definitionMap.set('consts', constsExpr);
  21294. }
  21295. definitionMap.set('template', templateFunctionExpression);
  21296. }
  21297. else {
  21298. // This path compiles the template using the prototype template pipeline. First the template is
  21299. // ingested into IR:
  21300. const tpl = ingest(meta.name, meta.template.nodes);
  21301. // Then the IR is transformed to prepare it for cod egeneration.
  21302. transformTemplate(tpl);
  21303. // Finally we emit the template function:
  21304. const templateFn = emitTemplateFn(tpl, constantPool);
  21305. definitionMap.set('decls', literal(tpl.root.decls));
  21306. definitionMap.set('vars', literal(tpl.root.vars));
  21307. if (tpl.consts.length > 0) {
  21308. definitionMap.set('consts', literalArr(tpl.consts));
  21309. }
  21310. definitionMap.set('template', templateFn);
  21311. }
  21312. if (meta.declarations.length > 0) {
  21313. definitionMap.set('dependencies', compileDeclarationList(literalArr(meta.declarations.map(decl => decl.type)), meta.declarationListEmitMode));
  21314. }
  21315. if (meta.encapsulation === null) {
  21316. meta.encapsulation = ViewEncapsulation.Emulated;
  21317. }
  21318. // e.g. `styles: [str1, str2]`
  21319. if (meta.styles && meta.styles.length) {
  21320. const styleValues = meta.encapsulation == ViewEncapsulation.Emulated ?
  21321. compileStyles(meta.styles, CONTENT_ATTR, HOST_ATTR) :
  21322. meta.styles;
  21323. const styleNodes = styleValues.reduce((result, style) => {
  21324. if (style.trim().length > 0) {
  21325. result.push(constantPool.getConstLiteral(literal(style)));
  21326. }
  21327. return result;
  21328. }, []);
  21329. if (styleNodes.length > 0) {
  21330. definitionMap.set('styles', literalArr(styleNodes));
  21331. }
  21332. }
  21333. else if (meta.encapsulation === ViewEncapsulation.Emulated) {
  21334. // If there is no style, don't generate css selectors on elements
  21335. meta.encapsulation = ViewEncapsulation.None;
  21336. }
  21337. // Only set view encapsulation if it's not the default value
  21338. if (meta.encapsulation !== ViewEncapsulation.Emulated) {
  21339. definitionMap.set('encapsulation', literal(meta.encapsulation));
  21340. }
  21341. // e.g. `animation: [trigger('123', [])]`
  21342. if (meta.animations !== null) {
  21343. definitionMap.set('data', literalMap([{ key: 'animation', value: meta.animations, quoted: false }]));
  21344. }
  21345. // Only set the change detection flag if it's defined and it's not the default.
  21346. if (changeDetection != null && changeDetection !== ChangeDetectionStrategy.Default) {
  21347. definitionMap.set('changeDetection', literal(changeDetection));
  21348. }
  21349. const expression = importExpr(Identifiers.defineComponent).callFn([definitionMap.toLiteralMap()], undefined, true);
  21350. const type = createComponentType(meta);
  21351. return { expression, type, statements: [] };
  21352. }
  21353. /**
  21354. * Creates the type specification from the component meta. This type is inserted into .d.ts files
  21355. * to be consumed by upstream compilations.
  21356. */
  21357. function createComponentType(meta) {
  21358. const typeParams = createBaseDirectiveTypeParams(meta);
  21359. typeParams.push(stringArrayAsType(meta.template.ngContentSelectors));
  21360. typeParams.push(expressionType(literal(meta.isStandalone)));
  21361. typeParams.push(createHostDirectivesType(meta));
  21362. return expressionType(importExpr(Identifiers.ComponentDeclaration, typeParams));
  21363. }
  21364. /**
  21365. * Compiles the array literal of declarations into an expression according to the provided emit
  21366. * mode.
  21367. */
  21368. function compileDeclarationList(list, mode) {
  21369. switch (mode) {
  21370. case 0 /* DeclarationListEmitMode.Direct */:
  21371. // directives: [MyDir],
  21372. return list;
  21373. case 1 /* DeclarationListEmitMode.Closure */:
  21374. // directives: function () { return [MyDir]; }
  21375. return fn([], [new ReturnStatement(list)]);
  21376. case 2 /* DeclarationListEmitMode.ClosureResolved */:
  21377. // directives: function () { return [MyDir].map(ng.resolveForwardRef); }
  21378. const resolvedList = list.prop('map').callFn([importExpr(Identifiers.resolveForwardRef)]);
  21379. return fn([], [new ReturnStatement(resolvedList)]);
  21380. }
  21381. }
  21382. function prepareQueryParams(query, constantPool) {
  21383. const parameters = [getQueryPredicate(query, constantPool), literal(toQueryFlags(query))];
  21384. if (query.read) {
  21385. parameters.push(query.read);
  21386. }
  21387. return parameters;
  21388. }
  21389. /**
  21390. * Translates query flags into `TQueryFlags` type in packages/core/src/render3/interfaces/query.ts
  21391. * @param query
  21392. */
  21393. function toQueryFlags(query) {
  21394. return (query.descendants ? 1 /* QueryFlags.descendants */ : 0 /* QueryFlags.none */) |
  21395. (query.static ? 2 /* QueryFlags.isStatic */ : 0 /* QueryFlags.none */) |
  21396. (query.emitDistinctChangesOnly ? 4 /* QueryFlags.emitDistinctChangesOnly */ : 0 /* QueryFlags.none */);
  21397. }
  21398. function convertAttributesToExpressions(attributes) {
  21399. const values = [];
  21400. for (let key of Object.getOwnPropertyNames(attributes)) {
  21401. const value = attributes[key];
  21402. values.push(literal(key), value);
  21403. }
  21404. return values;
  21405. }
  21406. // Define and update any content queries
  21407. function createContentQueriesFunction(queries, constantPool, name) {
  21408. const createStatements = [];
  21409. const updateStatements = [];
  21410. const tempAllocator = temporaryAllocator(updateStatements, TEMPORARY_NAME);
  21411. for (const query of queries) {
  21412. // creation, e.g. r3.contentQuery(dirIndex, somePredicate, true, null);
  21413. createStatements.push(importExpr(Identifiers.contentQuery)
  21414. .callFn([variable('dirIndex'), ...prepareQueryParams(query, constantPool)])
  21415. .toStmt());
  21416. // update, e.g. (r3.queryRefresh(tmp = r3.loadQuery()) && (ctx.someDir = tmp));
  21417. const temporary = tempAllocator();
  21418. const getQueryList = importExpr(Identifiers.loadQuery).callFn([]);
  21419. const refresh = importExpr(Identifiers.queryRefresh).callFn([temporary.set(getQueryList)]);
  21420. const updateDirective = variable(CONTEXT_NAME)
  21421. .prop(query.propertyName)
  21422. .set(query.first ? temporary.prop('first') : temporary);
  21423. updateStatements.push(refresh.and(updateDirective).toStmt());
  21424. }
  21425. const contentQueriesFnName = name ? `${name}_ContentQueries` : null;
  21426. return fn([
  21427. new FnParam(RENDER_FLAGS, NUMBER_TYPE), new FnParam(CONTEXT_NAME, null),
  21428. new FnParam('dirIndex', null)
  21429. ], [
  21430. renderFlagCheckIfStmt(1 /* core.RenderFlags.Create */, createStatements),
  21431. renderFlagCheckIfStmt(2 /* core.RenderFlags.Update */, updateStatements)
  21432. ], INFERRED_TYPE, null, contentQueriesFnName);
  21433. }
  21434. function stringAsType(str) {
  21435. return expressionType(literal(str));
  21436. }
  21437. function stringMapAsLiteralExpression(map) {
  21438. const mapValues = Object.keys(map).map(key => {
  21439. const value = Array.isArray(map[key]) ? map[key][0] : map[key];
  21440. return {
  21441. key,
  21442. value: literal(value),
  21443. quoted: true,
  21444. };
  21445. });
  21446. return literalMap(mapValues);
  21447. }
  21448. function stringArrayAsType(arr) {
  21449. return arr.length > 0 ? expressionType(literalArr(arr.map(value => literal(value)))) :
  21450. NONE_TYPE;
  21451. }
  21452. function createBaseDirectiveTypeParams(meta) {
  21453. // On the type side, remove newlines from the selector as it will need to fit into a TypeScript
  21454. // string literal, which must be on one line.
  21455. const selectorForType = meta.selector !== null ? meta.selector.replace(/\n/g, '') : null;
  21456. return [
  21457. typeWithParameters(meta.type.type, meta.typeArgumentCount),
  21458. selectorForType !== null ? stringAsType(selectorForType) : NONE_TYPE,
  21459. meta.exportAs !== null ? stringArrayAsType(meta.exportAs) : NONE_TYPE,
  21460. expressionType(getInputsTypeExpression(meta)),
  21461. expressionType(stringMapAsLiteralExpression(meta.outputs)),
  21462. stringArrayAsType(meta.queries.map(q => q.propertyName)),
  21463. ];
  21464. }
  21465. function getInputsTypeExpression(meta) {
  21466. return literalMap(Object.keys(meta.inputs).map(key => {
  21467. const value = meta.inputs[key];
  21468. return {
  21469. key,
  21470. value: literalMap([
  21471. { key: 'alias', value: literal(value.bindingPropertyName), quoted: true },
  21472. { key: 'required', value: literal(value.required), quoted: true }
  21473. ]),
  21474. quoted: true
  21475. };
  21476. }));
  21477. }
  21478. /**
  21479. * Creates the type specification from the directive meta. This type is inserted into .d.ts files
  21480. * to be consumed by upstream compilations.
  21481. */
  21482. function createDirectiveType(meta) {
  21483. const typeParams = createBaseDirectiveTypeParams(meta);
  21484. // Directives have no NgContentSelectors slot, but instead express a `never` type
  21485. // so that future fields align.
  21486. typeParams.push(NONE_TYPE);
  21487. typeParams.push(expressionType(literal(meta.isStandalone)));
  21488. typeParams.push(createHostDirectivesType(meta));
  21489. return expressionType(importExpr(Identifiers.DirectiveDeclaration, typeParams));
  21490. }
  21491. // Define and update any view queries
  21492. function createViewQueriesFunction(viewQueries, constantPool, name) {
  21493. const createStatements = [];
  21494. const updateStatements = [];
  21495. const tempAllocator = temporaryAllocator(updateStatements, TEMPORARY_NAME);
  21496. viewQueries.forEach((query) => {
  21497. // creation, e.g. r3.viewQuery(somePredicate, true);
  21498. const queryDefinition = importExpr(Identifiers.viewQuery).callFn(prepareQueryParams(query, constantPool));
  21499. createStatements.push(queryDefinition.toStmt());
  21500. // update, e.g. (r3.queryRefresh(tmp = r3.loadQuery()) && (ctx.someDir = tmp));
  21501. const temporary = tempAllocator();
  21502. const getQueryList = importExpr(Identifiers.loadQuery).callFn([]);
  21503. const refresh = importExpr(Identifiers.queryRefresh).callFn([temporary.set(getQueryList)]);
  21504. const updateDirective = variable(CONTEXT_NAME)
  21505. .prop(query.propertyName)
  21506. .set(query.first ? temporary.prop('first') : temporary);
  21507. updateStatements.push(refresh.and(updateDirective).toStmt());
  21508. });
  21509. const viewQueryFnName = name ? `${name}_Query` : null;
  21510. return fn([new FnParam(RENDER_FLAGS, NUMBER_TYPE), new FnParam(CONTEXT_NAME, null)], [
  21511. renderFlagCheckIfStmt(1 /* core.RenderFlags.Create */, createStatements),
  21512. renderFlagCheckIfStmt(2 /* core.RenderFlags.Update */, updateStatements)
  21513. ], INFERRED_TYPE, null, viewQueryFnName);
  21514. }
  21515. // Return a host binding function or null if one is not necessary.
  21516. function createHostBindingsFunction(hostBindingsMetadata, typeSourceSpan, bindingParser, constantPool, selector, name, definitionMap) {
  21517. const bindingContext = variable(CONTEXT_NAME);
  21518. const styleBuilder = new StylingBuilder(bindingContext);
  21519. const { styleAttr, classAttr } = hostBindingsMetadata.specialAttributes;
  21520. if (styleAttr !== undefined) {
  21521. styleBuilder.registerStyleAttr(styleAttr);
  21522. }
  21523. if (classAttr !== undefined) {
  21524. styleBuilder.registerClassAttr(classAttr);
  21525. }
  21526. const createInstructions = [];
  21527. const updateInstructions = [];
  21528. const updateVariables = [];
  21529. const hostBindingSourceSpan = typeSourceSpan;
  21530. // Calculate host event bindings
  21531. const eventBindings = bindingParser.createDirectiveHostEventAsts(hostBindingsMetadata.listeners, hostBindingSourceSpan);
  21532. if (eventBindings && eventBindings.length) {
  21533. createInstructions.push(...createHostListeners(eventBindings, name));
  21534. }
  21535. // Calculate the host property bindings
  21536. const bindings = bindingParser.createBoundHostProperties(hostBindingsMetadata.properties, hostBindingSourceSpan);
  21537. const allOtherBindings = [];
  21538. // We need to calculate the total amount of binding slots required by
  21539. // all the instructions together before any value conversions happen.
  21540. // Value conversions may require additional slots for interpolation and
  21541. // bindings with pipes. These calculates happen after this block.
  21542. let totalHostVarsCount = 0;
  21543. bindings && bindings.forEach((binding) => {
  21544. const stylingInputWasSet = styleBuilder.registerInputBasedOnName(binding.name, binding.expression, hostBindingSourceSpan);
  21545. if (stylingInputWasSet) {
  21546. totalHostVarsCount += MIN_STYLING_BINDING_SLOTS_REQUIRED;
  21547. }
  21548. else {
  21549. allOtherBindings.push(binding);
  21550. totalHostVarsCount++;
  21551. }
  21552. });
  21553. let valueConverter;
  21554. const getValueConverter = () => {
  21555. if (!valueConverter) {
  21556. const hostVarsCountFn = (numSlots) => {
  21557. const originalVarsCount = totalHostVarsCount;
  21558. totalHostVarsCount += numSlots;
  21559. return originalVarsCount;
  21560. };
  21561. valueConverter = new ValueConverter(constantPool, () => error('Unexpected node'), // new nodes are illegal here
  21562. hostVarsCountFn, () => error('Unexpected pipe')); // pipes are illegal here
  21563. }
  21564. return valueConverter;
  21565. };
  21566. const propertyBindings = [];
  21567. const attributeBindings = [];
  21568. const syntheticHostBindings = [];
  21569. for (const binding of allOtherBindings) {
  21570. // resolve literal arrays and literal objects
  21571. const value = binding.expression.visit(getValueConverter());
  21572. const bindingExpr = bindingFn(bindingContext, value);
  21573. const { bindingName, instruction, isAttribute } = getBindingNameAndInstruction(binding);
  21574. const securityContexts = bindingParser.calcPossibleSecurityContexts(selector, bindingName, isAttribute)
  21575. .filter(context => context !== SecurityContext.NONE);
  21576. let sanitizerFn = null;
  21577. if (securityContexts.length) {
  21578. if (securityContexts.length === 2 &&
  21579. securityContexts.indexOf(SecurityContext.URL) > -1 &&
  21580. securityContexts.indexOf(SecurityContext.RESOURCE_URL) > -1) {
  21581. // Special case for some URL attributes (such as "src" and "href") that may be a part
  21582. // of different security contexts. In this case we use special sanitization function and
  21583. // select the actual sanitizer at runtime based on a tag name that is provided while
  21584. // invoking sanitization function.
  21585. sanitizerFn = importExpr(Identifiers.sanitizeUrlOrResourceUrl);
  21586. }
  21587. else {
  21588. sanitizerFn = resolveSanitizationFn(securityContexts[0], isAttribute);
  21589. }
  21590. }
  21591. const instructionParams = [literal(bindingName), bindingExpr.currValExpr];
  21592. if (sanitizerFn) {
  21593. instructionParams.push(sanitizerFn);
  21594. }
  21595. else {
  21596. // If there was no sanitization function found based on the security context
  21597. // of an attribute/property binding - check whether this attribute/property is
  21598. // one of the security-sensitive <iframe> attributes.
  21599. // Note: for host bindings defined on a directive, we do not try to find all
  21600. // possible places where it can be matched, so we can not determine whether
  21601. // the host element is an <iframe>. In this case, if an attribute/binding
  21602. // name is in the `IFRAME_SECURITY_SENSITIVE_ATTRS` set - append a validation
  21603. // function, which would be invoked at runtime and would have access to the
  21604. // underlying DOM element, check if it's an <iframe> and if so - runs extra checks.
  21605. if (isIframeSecuritySensitiveAttr(bindingName)) {
  21606. instructionParams.push(importExpr(Identifiers.validateIframeAttribute));
  21607. }
  21608. }
  21609. updateVariables.push(...bindingExpr.stmts);
  21610. if (instruction === Identifiers.hostProperty) {
  21611. propertyBindings.push(instructionParams);
  21612. }
  21613. else if (instruction === Identifiers.attribute) {
  21614. attributeBindings.push(instructionParams);
  21615. }
  21616. else if (instruction === Identifiers.syntheticHostProperty) {
  21617. syntheticHostBindings.push(instructionParams);
  21618. }
  21619. else {
  21620. updateInstructions.push({ reference: instruction, paramsOrFn: instructionParams, span: null });
  21621. }
  21622. }
  21623. for (const bindingParams of propertyBindings) {
  21624. updateInstructions.push({ reference: Identifiers.hostProperty, paramsOrFn: bindingParams, span: null });
  21625. }
  21626. for (const bindingParams of attributeBindings) {
  21627. updateInstructions.push({ reference: Identifiers.attribute, paramsOrFn: bindingParams, span: null });
  21628. }
  21629. for (const bindingParams of syntheticHostBindings) {
  21630. updateInstructions.push({ reference: Identifiers.syntheticHostProperty, paramsOrFn: bindingParams, span: null });
  21631. }
  21632. // since we're dealing with directives/components and both have hostBinding
  21633. // functions, we need to generate a special hostAttrs instruction that deals
  21634. // with both the assignment of styling as well as static attributes to the host
  21635. // element. The instruction below will instruct all initial styling (styling
  21636. // that is inside of a host binding within a directive/component) to be attached
  21637. // to the host element alongside any of the provided host attributes that were
  21638. // collected earlier.
  21639. const hostAttrs = convertAttributesToExpressions(hostBindingsMetadata.attributes);
  21640. styleBuilder.assignHostAttrs(hostAttrs, definitionMap);
  21641. if (styleBuilder.hasBindings) {
  21642. // finally each binding that was registered in the statement above will need to be added to
  21643. // the update block of a component/directive templateFn/hostBindingsFn so that the bindings
  21644. // are evaluated and updated for the element.
  21645. styleBuilder.buildUpdateLevelInstructions(getValueConverter()).forEach(instruction => {
  21646. for (const call of instruction.calls) {
  21647. // we subtract a value of `1` here because the binding slot was already allocated
  21648. // at the top of this method when all the input bindings were counted.
  21649. totalHostVarsCount +=
  21650. Math.max(call.allocateBindingSlots - MIN_STYLING_BINDING_SLOTS_REQUIRED, 0);
  21651. updateInstructions.push({
  21652. reference: instruction.reference,
  21653. paramsOrFn: convertStylingCall(call, bindingContext, bindingFn),
  21654. span: null
  21655. });
  21656. }
  21657. });
  21658. }
  21659. if (totalHostVarsCount) {
  21660. definitionMap.set('hostVars', literal(totalHostVarsCount));
  21661. }
  21662. if (createInstructions.length > 0 || updateInstructions.length > 0) {
  21663. const hostBindingsFnName = name ? `${name}_HostBindings` : null;
  21664. const statements = [];
  21665. if (createInstructions.length > 0) {
  21666. statements.push(renderFlagCheckIfStmt(1 /* core.RenderFlags.Create */, getInstructionStatements(createInstructions)));
  21667. }
  21668. if (updateInstructions.length > 0) {
  21669. statements.push(renderFlagCheckIfStmt(2 /* core.RenderFlags.Update */, updateVariables.concat(getInstructionStatements(updateInstructions))));
  21670. }
  21671. return fn([new FnParam(RENDER_FLAGS, NUMBER_TYPE), new FnParam(CONTEXT_NAME, null)], statements, INFERRED_TYPE, null, hostBindingsFnName);
  21672. }
  21673. return null;
  21674. }
  21675. function bindingFn(implicit, value) {
  21676. return convertPropertyBinding(null, implicit, value, 'b');
  21677. }
  21678. function convertStylingCall(call, bindingContext, bindingFn) {
  21679. return call.params(value => bindingFn(bindingContext, value).currValExpr);
  21680. }
  21681. function getBindingNameAndInstruction(binding) {
  21682. let bindingName = binding.name;
  21683. let instruction;
  21684. // Check to see if this is an attr binding or a property binding
  21685. const attrMatches = bindingName.match(ATTR_REGEX);
  21686. if (attrMatches) {
  21687. bindingName = attrMatches[1];
  21688. instruction = Identifiers.attribute;
  21689. }
  21690. else {
  21691. if (binding.isAnimation) {
  21692. bindingName = prepareSyntheticPropertyName(bindingName);
  21693. // host bindings that have a synthetic property (e.g. @foo) should always be rendered
  21694. // in the context of the component and not the parent. Therefore there is a special
  21695. // compatibility instruction available for this purpose.
  21696. instruction = Identifiers.syntheticHostProperty;
  21697. }
  21698. else {
  21699. instruction = Identifiers.hostProperty;
  21700. }
  21701. }
  21702. return { bindingName, instruction, isAttribute: !!attrMatches };
  21703. }
  21704. function createHostListeners(eventBindings, name) {
  21705. const listenerParams = [];
  21706. const syntheticListenerParams = [];
  21707. const instructions = [];
  21708. for (const binding of eventBindings) {
  21709. let bindingName = binding.name && sanitizeIdentifier(binding.name);
  21710. const bindingFnName = binding.type === 1 /* ParsedEventType.Animation */ ?
  21711. prepareSyntheticListenerFunctionName(bindingName, binding.targetOrPhase) :
  21712. bindingName;
  21713. const handlerName = name && bindingName ? `${name}_${bindingFnName}_HostBindingHandler` : null;
  21714. const params = prepareEventListenerParameters(BoundEvent.fromParsedEvent(binding), handlerName);
  21715. if (binding.type == 1 /* ParsedEventType.Animation */) {
  21716. syntheticListenerParams.push(params);
  21717. }
  21718. else {
  21719. listenerParams.push(params);
  21720. }
  21721. }
  21722. for (const params of syntheticListenerParams) {
  21723. instructions.push({ reference: Identifiers.syntheticHostListener, paramsOrFn: params, span: null });
  21724. }
  21725. for (const params of listenerParams) {
  21726. instructions.push({ reference: Identifiers.listener, paramsOrFn: params, span: null });
  21727. }
  21728. return instructions;
  21729. }
  21730. const HOST_REG_EXP = /^(?:\[([^\]]+)\])|(?:\(([^\)]+)\))$/;
  21731. function parseHostBindings(host) {
  21732. const attributes = {};
  21733. const listeners = {};
  21734. const properties = {};
  21735. const specialAttributes = {};
  21736. for (const key of Object.keys(host)) {
  21737. const value = host[key];
  21738. const matches = key.match(HOST_REG_EXP);
  21739. if (matches === null) {
  21740. switch (key) {
  21741. case 'class':
  21742. if (typeof value !== 'string') {
  21743. // TODO(alxhub): make this a diagnostic.
  21744. throw new Error(`Class binding must be string`);
  21745. }
  21746. specialAttributes.classAttr = value;
  21747. break;
  21748. case 'style':
  21749. if (typeof value !== 'string') {
  21750. // TODO(alxhub): make this a diagnostic.
  21751. throw new Error(`Style binding must be string`);
  21752. }
  21753. specialAttributes.styleAttr = value;
  21754. break;
  21755. default:
  21756. if (typeof value === 'string') {
  21757. attributes[key] = literal(value);
  21758. }
  21759. else {
  21760. attributes[key] = value;
  21761. }
  21762. }
  21763. }
  21764. else if (matches[1 /* HostBindingGroup.Binding */] != null) {
  21765. if (typeof value !== 'string') {
  21766. // TODO(alxhub): make this a diagnostic.
  21767. throw new Error(`Property binding must be string`);
  21768. }
  21769. // synthetic properties (the ones that have a `@` as a prefix)
  21770. // are still treated the same as regular properties. Therefore
  21771. // there is no point in storing them in a separate map.
  21772. properties[matches[1 /* HostBindingGroup.Binding */]] = value;
  21773. }
  21774. else if (matches[2 /* HostBindingGroup.Event */] != null) {
  21775. if (typeof value !== 'string') {
  21776. // TODO(alxhub): make this a diagnostic.
  21777. throw new Error(`Event binding must be string`);
  21778. }
  21779. listeners[matches[2 /* HostBindingGroup.Event */]] = value;
  21780. }
  21781. }
  21782. return { attributes, listeners, properties, specialAttributes };
  21783. }
  21784. /**
  21785. * Verifies host bindings and returns the list of errors (if any). Empty array indicates that a
  21786. * given set of host bindings has no errors.
  21787. *
  21788. * @param bindings set of host bindings to verify.
  21789. * @param sourceSpan source span where host bindings were defined.
  21790. * @returns array of errors associated with a given set of host bindings.
  21791. */
  21792. function verifyHostBindings(bindings, sourceSpan) {
  21793. // TODO: abstract out host bindings verification logic and use it instead of
  21794. // creating events and properties ASTs to detect errors (FW-996)
  21795. const bindingParser = makeBindingParser();
  21796. bindingParser.createDirectiveHostEventAsts(bindings.listeners, sourceSpan);
  21797. bindingParser.createBoundHostProperties(bindings.properties, sourceSpan);
  21798. return bindingParser.errors;
  21799. }
  21800. function compileStyles(styles, selector, hostSelector) {
  21801. const shadowCss = new ShadowCss();
  21802. return styles.map(style => {
  21803. return shadowCss.shimCssText(style, selector, hostSelector);
  21804. });
  21805. }
  21806. function createHostDirectivesType(meta) {
  21807. if (!meta.hostDirectives?.length) {
  21808. return NONE_TYPE;
  21809. }
  21810. return expressionType(literalArr(meta.hostDirectives.map(hostMeta => literalMap([
  21811. { key: 'directive', value: typeofExpr(hostMeta.directive.type), quoted: false },
  21812. { key: 'inputs', value: stringMapAsLiteralExpression(hostMeta.inputs || {}), quoted: false },
  21813. { key: 'outputs', value: stringMapAsLiteralExpression(hostMeta.outputs || {}), quoted: false },
  21814. ]))));
  21815. }
  21816. function createHostDirectivesFeatureArg(hostDirectives) {
  21817. const expressions = [];
  21818. let hasForwardRef = false;
  21819. for (const current of hostDirectives) {
  21820. // Use a shorthand if there are no inputs or outputs.
  21821. if (!current.inputs && !current.outputs) {
  21822. expressions.push(current.directive.type);
  21823. }
  21824. else {
  21825. const keys = [{ key: 'directive', value: current.directive.type, quoted: false }];
  21826. if (current.inputs) {
  21827. const inputsLiteral = createHostDirectivesMappingArray(current.inputs);
  21828. if (inputsLiteral) {
  21829. keys.push({ key: 'inputs', value: inputsLiteral, quoted: false });
  21830. }
  21831. }
  21832. if (current.outputs) {
  21833. const outputsLiteral = createHostDirectivesMappingArray(current.outputs);
  21834. if (outputsLiteral) {
  21835. keys.push({ key: 'outputs', value: outputsLiteral, quoted: false });
  21836. }
  21837. }
  21838. expressions.push(literalMap(keys));
  21839. }
  21840. if (current.isForwardReference) {
  21841. hasForwardRef = true;
  21842. }
  21843. }
  21844. // If there's a forward reference, we generate a `function() { return [HostDir] }`,
  21845. // otherwise we can save some bytes by using a plain array, e.g. `[HostDir]`.
  21846. return hasForwardRef ?
  21847. new FunctionExpr([], [new ReturnStatement(literalArr(expressions))]) :
  21848. literalArr(expressions);
  21849. }
  21850. /**
  21851. * Converts an input/output mapping object literal into an array where the even keys are the
  21852. * public name of the binding and the odd ones are the name it was aliased to. E.g.
  21853. * `{inputOne: 'aliasOne', inputTwo: 'aliasTwo'}` will become
  21854. * `['inputOne', 'aliasOne', 'inputTwo', 'aliasTwo']`.
  21855. *
  21856. * This conversion is necessary, because hosts bind to the public name of the host directive and
  21857. * keeping the mapping in an object literal will break for apps using property renaming.
  21858. */
  21859. function createHostDirectivesMappingArray(mapping) {
  21860. const elements = [];
  21861. for (const publicName in mapping) {
  21862. if (mapping.hasOwnProperty(publicName)) {
  21863. elements.push(literal(publicName), literal(mapping[publicName]));
  21864. }
  21865. }
  21866. return elements.length > 0 ? literalArr(elements) : null;
  21867. }
  21868. /**
  21869. * An interface for retrieving documents by URL that the compiler uses to
  21870. * load templates.
  21871. *
  21872. * This is an abstract class, rather than an interface, so that it can be used
  21873. * as injection token.
  21874. */
  21875. class ResourceLoader {
  21876. }
  21877. class CompilerFacadeImpl {
  21878. constructor(jitEvaluator = new JitEvaluator()) {
  21879. this.jitEvaluator = jitEvaluator;
  21880. this.FactoryTarget = FactoryTarget$1;
  21881. this.ResourceLoader = ResourceLoader;
  21882. this.elementSchemaRegistry = new DomElementSchemaRegistry();
  21883. }
  21884. compilePipe(angularCoreEnv, sourceMapUrl, facade) {
  21885. const metadata = {
  21886. name: facade.name,
  21887. type: wrapReference(facade.type),
  21888. typeArgumentCount: 0,
  21889. deps: null,
  21890. pipeName: facade.pipeName,
  21891. pure: facade.pure,
  21892. isStandalone: facade.isStandalone,
  21893. };
  21894. const res = compilePipeFromMetadata(metadata);
  21895. return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, []);
  21896. }
  21897. compilePipeDeclaration(angularCoreEnv, sourceMapUrl, declaration) {
  21898. const meta = convertDeclarePipeFacadeToMetadata(declaration);
  21899. const res = compilePipeFromMetadata(meta);
  21900. return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, []);
  21901. }
  21902. compileInjectable(angularCoreEnv, sourceMapUrl, facade) {
  21903. const { expression, statements } = compileInjectable({
  21904. name: facade.name,
  21905. type: wrapReference(facade.type),
  21906. typeArgumentCount: facade.typeArgumentCount,
  21907. providedIn: computeProvidedIn(facade.providedIn),
  21908. useClass: convertToProviderExpression(facade, 'useClass'),
  21909. useFactory: wrapExpression(facade, 'useFactory'),
  21910. useValue: convertToProviderExpression(facade, 'useValue'),
  21911. useExisting: convertToProviderExpression(facade, 'useExisting'),
  21912. deps: facade.deps?.map(convertR3DependencyMetadata),
  21913. },
  21914. /* resolveForwardRefs */ true);
  21915. return this.jitExpression(expression, angularCoreEnv, sourceMapUrl, statements);
  21916. }
  21917. compileInjectableDeclaration(angularCoreEnv, sourceMapUrl, facade) {
  21918. const { expression, statements } = compileInjectable({
  21919. name: facade.type.name,
  21920. type: wrapReference(facade.type),
  21921. typeArgumentCount: 0,
  21922. providedIn: computeProvidedIn(facade.providedIn),
  21923. useClass: convertToProviderExpression(facade, 'useClass'),
  21924. useFactory: wrapExpression(facade, 'useFactory'),
  21925. useValue: convertToProviderExpression(facade, 'useValue'),
  21926. useExisting: convertToProviderExpression(facade, 'useExisting'),
  21927. deps: facade.deps?.map(convertR3DeclareDependencyMetadata),
  21928. },
  21929. /* resolveForwardRefs */ true);
  21930. return this.jitExpression(expression, angularCoreEnv, sourceMapUrl, statements);
  21931. }
  21932. compileInjector(angularCoreEnv, sourceMapUrl, facade) {
  21933. const meta = {
  21934. name: facade.name,
  21935. type: wrapReference(facade.type),
  21936. providers: facade.providers && facade.providers.length > 0 ?
  21937. new WrappedNodeExpr(facade.providers) :
  21938. null,
  21939. imports: facade.imports.map(i => new WrappedNodeExpr(i)),
  21940. };
  21941. const res = compileInjector(meta);
  21942. return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, []);
  21943. }
  21944. compileInjectorDeclaration(angularCoreEnv, sourceMapUrl, declaration) {
  21945. const meta = convertDeclareInjectorFacadeToMetadata(declaration);
  21946. const res = compileInjector(meta);
  21947. return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, []);
  21948. }
  21949. compileNgModule(angularCoreEnv, sourceMapUrl, facade) {
  21950. const meta = {
  21951. type: wrapReference(facade.type),
  21952. bootstrap: facade.bootstrap.map(wrapReference),
  21953. declarations: facade.declarations.map(wrapReference),
  21954. publicDeclarationTypes: null,
  21955. imports: facade.imports.map(wrapReference),
  21956. includeImportTypes: true,
  21957. exports: facade.exports.map(wrapReference),
  21958. selectorScopeMode: R3SelectorScopeMode.Inline,
  21959. containsForwardDecls: false,
  21960. schemas: facade.schemas ? facade.schemas.map(wrapReference) : null,
  21961. id: facade.id ? new WrappedNodeExpr(facade.id) : null,
  21962. };
  21963. const res = compileNgModule(meta);
  21964. return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, []);
  21965. }
  21966. compileNgModuleDeclaration(angularCoreEnv, sourceMapUrl, declaration) {
  21967. const expression = compileNgModuleDeclarationExpression(declaration);
  21968. return this.jitExpression(expression, angularCoreEnv, sourceMapUrl, []);
  21969. }
  21970. compileDirective(angularCoreEnv, sourceMapUrl, facade) {
  21971. const meta = convertDirectiveFacadeToMetadata(facade);
  21972. return this.compileDirectiveFromMeta(angularCoreEnv, sourceMapUrl, meta);
  21973. }
  21974. compileDirectiveDeclaration(angularCoreEnv, sourceMapUrl, declaration) {
  21975. const typeSourceSpan = this.createParseSourceSpan('Directive', declaration.type.name, sourceMapUrl);
  21976. const meta = convertDeclareDirectiveFacadeToMetadata(declaration, typeSourceSpan);
  21977. return this.compileDirectiveFromMeta(angularCoreEnv, sourceMapUrl, meta);
  21978. }
  21979. compileDirectiveFromMeta(angularCoreEnv, sourceMapUrl, meta) {
  21980. const constantPool = new ConstantPool();
  21981. const bindingParser = makeBindingParser();
  21982. const res = compileDirectiveFromMetadata(meta, constantPool, bindingParser);
  21983. return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, constantPool.statements);
  21984. }
  21985. compileComponent(angularCoreEnv, sourceMapUrl, facade) {
  21986. // Parse the template and check for errors.
  21987. const { template, interpolation } = parseJitTemplate(facade.template, facade.name, sourceMapUrl, facade.preserveWhitespaces, facade.interpolation);
  21988. // Compile the component metadata, including template, into an expression.
  21989. const meta = {
  21990. ...facade,
  21991. ...convertDirectiveFacadeToMetadata(facade),
  21992. selector: facade.selector || this.elementSchemaRegistry.getDefaultComponentElementName(),
  21993. template,
  21994. declarations: facade.declarations.map(convertDeclarationFacadeToMetadata),
  21995. declarationListEmitMode: 0 /* DeclarationListEmitMode.Direct */,
  21996. styles: [...facade.styles, ...template.styles],
  21997. encapsulation: facade.encapsulation,
  21998. interpolation,
  21999. changeDetection: facade.changeDetection,
  22000. animations: facade.animations != null ? new WrappedNodeExpr(facade.animations) : null,
  22001. viewProviders: facade.viewProviders != null ? new WrappedNodeExpr(facade.viewProviders) :
  22002. null,
  22003. relativeContextFilePath: '',
  22004. i18nUseExternalIds: true,
  22005. };
  22006. const jitExpressionSourceMap = `ng:///${facade.name}.js`;
  22007. return this.compileComponentFromMeta(angularCoreEnv, jitExpressionSourceMap, meta);
  22008. }
  22009. compileComponentDeclaration(angularCoreEnv, sourceMapUrl, declaration) {
  22010. const typeSourceSpan = this.createParseSourceSpan('Component', declaration.type.name, sourceMapUrl);
  22011. const meta = convertDeclareComponentFacadeToMetadata(declaration, typeSourceSpan, sourceMapUrl);
  22012. return this.compileComponentFromMeta(angularCoreEnv, sourceMapUrl, meta);
  22013. }
  22014. compileComponentFromMeta(angularCoreEnv, sourceMapUrl, meta) {
  22015. const constantPool = new ConstantPool();
  22016. const bindingParser = makeBindingParser(meta.interpolation);
  22017. const res = compileComponentFromMetadata(meta, constantPool, bindingParser);
  22018. return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, constantPool.statements);
  22019. }
  22020. compileFactory(angularCoreEnv, sourceMapUrl, meta) {
  22021. const factoryRes = compileFactoryFunction({
  22022. name: meta.name,
  22023. type: wrapReference(meta.type),
  22024. typeArgumentCount: meta.typeArgumentCount,
  22025. deps: convertR3DependencyMetadataArray(meta.deps),
  22026. target: meta.target,
  22027. });
  22028. return this.jitExpression(factoryRes.expression, angularCoreEnv, sourceMapUrl, factoryRes.statements);
  22029. }
  22030. compileFactoryDeclaration(angularCoreEnv, sourceMapUrl, meta) {
  22031. const factoryRes = compileFactoryFunction({
  22032. name: meta.type.name,
  22033. type: wrapReference(meta.type),
  22034. typeArgumentCount: 0,
  22035. deps: Array.isArray(meta.deps) ? meta.deps.map(convertR3DeclareDependencyMetadata) :
  22036. meta.deps,
  22037. target: meta.target,
  22038. });
  22039. return this.jitExpression(factoryRes.expression, angularCoreEnv, sourceMapUrl, factoryRes.statements);
  22040. }
  22041. createParseSourceSpan(kind, typeName, sourceUrl) {
  22042. return r3JitTypeSourceSpan(kind, typeName, sourceUrl);
  22043. }
  22044. /**
  22045. * JIT compiles an expression and returns the result of executing that expression.
  22046. *
  22047. * @param def the definition which will be compiled and executed to get the value to patch
  22048. * @param context an object map of @angular/core symbol names to symbols which will be available
  22049. * in the context of the compiled expression
  22050. * @param sourceUrl a URL to use for the source map of the compiled expression
  22051. * @param preStatements a collection of statements that should be evaluated before the expression.
  22052. */
  22053. jitExpression(def, context, sourceUrl, preStatements) {
  22054. // The ConstantPool may contain Statements which declare variables used in the final expression.
  22055. // Therefore, its statements need to precede the actual JIT operation. The final statement is a
  22056. // declaration of $def which is set to the expression being compiled.
  22057. const statements = [
  22058. ...preStatements,
  22059. new DeclareVarStmt('$def', def, undefined, StmtModifier.Exported),
  22060. ];
  22061. const res = this.jitEvaluator.evaluateStatements(sourceUrl, statements, new R3JitReflector(context), /* enableSourceMaps */ true);
  22062. return res['$def'];
  22063. }
  22064. }
  22065. function convertToR3QueryMetadata(facade) {
  22066. return {
  22067. ...facade,
  22068. predicate: convertQueryPredicate(facade.predicate),
  22069. read: facade.read ? new WrappedNodeExpr(facade.read) : null,
  22070. static: facade.static,
  22071. emitDistinctChangesOnly: facade.emitDistinctChangesOnly,
  22072. };
  22073. }
  22074. function convertQueryDeclarationToMetadata(declaration) {
  22075. return {
  22076. propertyName: declaration.propertyName,
  22077. first: declaration.first ?? false,
  22078. predicate: convertQueryPredicate(declaration.predicate),
  22079. descendants: declaration.descendants ?? false,
  22080. read: declaration.read ? new WrappedNodeExpr(declaration.read) : null,
  22081. static: declaration.static ?? false,
  22082. emitDistinctChangesOnly: declaration.emitDistinctChangesOnly ?? true,
  22083. };
  22084. }
  22085. function convertQueryPredicate(predicate) {
  22086. return Array.isArray(predicate) ?
  22087. // The predicate is an array of strings so pass it through.
  22088. predicate :
  22089. // The predicate is a type - assume that we will need to unwrap any `forwardRef()` calls.
  22090. createMayBeForwardRefExpression(new WrappedNodeExpr(predicate), 1 /* ForwardRefHandling.Wrapped */);
  22091. }
  22092. function convertDirectiveFacadeToMetadata(facade) {
  22093. const inputsFromMetadata = parseInputsArray(facade.inputs || []);
  22094. const outputsFromMetadata = parseMappingStringArray(facade.outputs || []);
  22095. const propMetadata = facade.propMetadata;
  22096. const inputsFromType = {};
  22097. const outputsFromType = {};
  22098. for (const field in propMetadata) {
  22099. if (propMetadata.hasOwnProperty(field)) {
  22100. propMetadata[field].forEach(ann => {
  22101. if (isInput(ann)) {
  22102. inputsFromType[field] = {
  22103. bindingPropertyName: ann.alias || field,
  22104. classPropertyName: field,
  22105. required: ann.required || false
  22106. };
  22107. }
  22108. else if (isOutput(ann)) {
  22109. outputsFromType[field] = ann.alias || field;
  22110. }
  22111. });
  22112. }
  22113. }
  22114. return {
  22115. ...facade,
  22116. typeArgumentCount: 0,
  22117. typeSourceSpan: facade.typeSourceSpan,
  22118. type: wrapReference(facade.type),
  22119. deps: null,
  22120. host: extractHostBindings(facade.propMetadata, facade.typeSourceSpan, facade.host),
  22121. inputs: { ...inputsFromMetadata, ...inputsFromType },
  22122. outputs: { ...outputsFromMetadata, ...outputsFromType },
  22123. queries: facade.queries.map(convertToR3QueryMetadata),
  22124. providers: facade.providers != null ? new WrappedNodeExpr(facade.providers) : null,
  22125. viewQueries: facade.viewQueries.map(convertToR3QueryMetadata),
  22126. fullInheritance: false,
  22127. hostDirectives: convertHostDirectivesToMetadata(facade),
  22128. };
  22129. }
  22130. function convertDeclareDirectiveFacadeToMetadata(declaration, typeSourceSpan) {
  22131. return {
  22132. name: declaration.type.name,
  22133. type: wrapReference(declaration.type),
  22134. typeSourceSpan,
  22135. selector: declaration.selector ?? null,
  22136. inputs: declaration.inputs ? inputsMappingToInputMetadata(declaration.inputs) : {},
  22137. outputs: declaration.outputs ?? {},
  22138. host: convertHostDeclarationToMetadata(declaration.host),
  22139. queries: (declaration.queries ?? []).map(convertQueryDeclarationToMetadata),
  22140. viewQueries: (declaration.viewQueries ?? []).map(convertQueryDeclarationToMetadata),
  22141. providers: declaration.providers !== undefined ? new WrappedNodeExpr(declaration.providers) :
  22142. null,
  22143. exportAs: declaration.exportAs ?? null,
  22144. usesInheritance: declaration.usesInheritance ?? false,
  22145. lifecycle: { usesOnChanges: declaration.usesOnChanges ?? false },
  22146. deps: null,
  22147. typeArgumentCount: 0,
  22148. fullInheritance: false,
  22149. isStandalone: declaration.isStandalone ?? false,
  22150. hostDirectives: convertHostDirectivesToMetadata(declaration),
  22151. };
  22152. }
  22153. function convertHostDeclarationToMetadata(host = {}) {
  22154. return {
  22155. attributes: convertOpaqueValuesToExpressions(host.attributes ?? {}),
  22156. listeners: host.listeners ?? {},
  22157. properties: host.properties ?? {},
  22158. specialAttributes: {
  22159. classAttr: host.classAttribute,
  22160. styleAttr: host.styleAttribute,
  22161. },
  22162. };
  22163. }
  22164. function convertHostDirectivesToMetadata(metadata) {
  22165. if (metadata.hostDirectives?.length) {
  22166. return metadata.hostDirectives.map(hostDirective => {
  22167. return typeof hostDirective === 'function' ?
  22168. {
  22169. directive: wrapReference(hostDirective),
  22170. inputs: null,
  22171. outputs: null,
  22172. isForwardReference: false
  22173. } :
  22174. {
  22175. directive: wrapReference(hostDirective.directive),
  22176. isForwardReference: false,
  22177. inputs: hostDirective.inputs ? parseMappingStringArray(hostDirective.inputs) : null,
  22178. outputs: hostDirective.outputs ? parseMappingStringArray(hostDirective.outputs) : null,
  22179. };
  22180. });
  22181. }
  22182. return null;
  22183. }
  22184. function convertOpaqueValuesToExpressions(obj) {
  22185. const result = {};
  22186. for (const key of Object.keys(obj)) {
  22187. result[key] = new WrappedNodeExpr(obj[key]);
  22188. }
  22189. return result;
  22190. }
  22191. function convertDeclareComponentFacadeToMetadata(decl, typeSourceSpan, sourceMapUrl) {
  22192. const { template, interpolation } = parseJitTemplate(decl.template, decl.type.name, sourceMapUrl, decl.preserveWhitespaces ?? false, decl.interpolation);
  22193. const declarations = [];
  22194. if (decl.dependencies) {
  22195. for (const innerDep of decl.dependencies) {
  22196. switch (innerDep.kind) {
  22197. case 'directive':
  22198. case 'component':
  22199. declarations.push(convertDirectiveDeclarationToMetadata(innerDep));
  22200. break;
  22201. case 'pipe':
  22202. declarations.push(convertPipeDeclarationToMetadata(innerDep));
  22203. break;
  22204. }
  22205. }
  22206. }
  22207. else if (decl.components || decl.directives || decl.pipes) {
  22208. // Existing declarations on NPM may not be using the new `dependencies` merged field, and may
  22209. // have separate fields for dependencies instead. Unify them for JIT compilation.
  22210. decl.components &&
  22211. declarations.push(...decl.components.map(dir => convertDirectiveDeclarationToMetadata(dir, /* isComponent */ true)));
  22212. decl.directives &&
  22213. declarations.push(...decl.directives.map(dir => convertDirectiveDeclarationToMetadata(dir)));
  22214. decl.pipes && declarations.push(...convertPipeMapToMetadata(decl.pipes));
  22215. }
  22216. return {
  22217. ...convertDeclareDirectiveFacadeToMetadata(decl, typeSourceSpan),
  22218. template,
  22219. styles: decl.styles ?? [],
  22220. declarations,
  22221. viewProviders: decl.viewProviders !== undefined ? new WrappedNodeExpr(decl.viewProviders) :
  22222. null,
  22223. animations: decl.animations !== undefined ? new WrappedNodeExpr(decl.animations) : null,
  22224. changeDetection: decl.changeDetection ?? ChangeDetectionStrategy.Default,
  22225. encapsulation: decl.encapsulation ?? ViewEncapsulation.Emulated,
  22226. interpolation,
  22227. declarationListEmitMode: 2 /* DeclarationListEmitMode.ClosureResolved */,
  22228. relativeContextFilePath: '',
  22229. i18nUseExternalIds: true,
  22230. };
  22231. }
  22232. function convertDeclarationFacadeToMetadata(declaration) {
  22233. return {
  22234. ...declaration,
  22235. type: new WrappedNodeExpr(declaration.type),
  22236. };
  22237. }
  22238. function convertDirectiveDeclarationToMetadata(declaration, isComponent = null) {
  22239. return {
  22240. kind: R3TemplateDependencyKind.Directive,
  22241. isComponent: isComponent || declaration.kind === 'component',
  22242. selector: declaration.selector,
  22243. type: new WrappedNodeExpr(declaration.type),
  22244. inputs: declaration.inputs ?? [],
  22245. outputs: declaration.outputs ?? [],
  22246. exportAs: declaration.exportAs ?? null,
  22247. };
  22248. }
  22249. function convertPipeMapToMetadata(pipes) {
  22250. if (!pipes) {
  22251. return [];
  22252. }
  22253. return Object.keys(pipes).map(name => {
  22254. return {
  22255. kind: R3TemplateDependencyKind.Pipe,
  22256. name,
  22257. type: new WrappedNodeExpr(pipes[name]),
  22258. };
  22259. });
  22260. }
  22261. function convertPipeDeclarationToMetadata(pipe) {
  22262. return {
  22263. kind: R3TemplateDependencyKind.Pipe,
  22264. name: pipe.name,
  22265. type: new WrappedNodeExpr(pipe.type),
  22266. };
  22267. }
  22268. function parseJitTemplate(template, typeName, sourceMapUrl, preserveWhitespaces, interpolation) {
  22269. const interpolationConfig = interpolation ? InterpolationConfig.fromArray(interpolation) : DEFAULT_INTERPOLATION_CONFIG;
  22270. // Parse the template and check for errors.
  22271. const parsed = parseTemplate(template, sourceMapUrl, { preserveWhitespaces, interpolationConfig });
  22272. if (parsed.errors !== null) {
  22273. const errors = parsed.errors.map(err => err.toString()).join(', ');
  22274. throw new Error(`Errors during JIT compilation of template for ${typeName}: ${errors}`);
  22275. }
  22276. return { template: parsed, interpolation: interpolationConfig };
  22277. }
  22278. /**
  22279. * Convert the expression, if present to an `R3ProviderExpression`.
  22280. *
  22281. * In JIT mode we do not want the compiler to wrap the expression in a `forwardRef()` call because,
  22282. * if it is referencing a type that has not yet been defined, it will have already been wrapped in
  22283. * a `forwardRef()` - either by the application developer or during partial-compilation. Thus we can
  22284. * use `ForwardRefHandling.None`.
  22285. */
  22286. function convertToProviderExpression(obj, property) {
  22287. if (obj.hasOwnProperty(property)) {
  22288. return createMayBeForwardRefExpression(new WrappedNodeExpr(obj[property]), 0 /* ForwardRefHandling.None */);
  22289. }
  22290. else {
  22291. return undefined;
  22292. }
  22293. }
  22294. function wrapExpression(obj, property) {
  22295. if (obj.hasOwnProperty(property)) {
  22296. return new WrappedNodeExpr(obj[property]);
  22297. }
  22298. else {
  22299. return undefined;
  22300. }
  22301. }
  22302. function computeProvidedIn(providedIn) {
  22303. const expression = typeof providedIn === 'function' ? new WrappedNodeExpr(providedIn) :
  22304. new LiteralExpr(providedIn ?? null);
  22305. // See `convertToProviderExpression()` for why this uses `ForwardRefHandling.None`.
  22306. return createMayBeForwardRefExpression(expression, 0 /* ForwardRefHandling.None */);
  22307. }
  22308. function convertR3DependencyMetadataArray(facades) {
  22309. return facades == null ? null : facades.map(convertR3DependencyMetadata);
  22310. }
  22311. function convertR3DependencyMetadata(facade) {
  22312. const isAttributeDep = facade.attribute != null; // both `null` and `undefined`
  22313. const rawToken = facade.token === null ? null : new WrappedNodeExpr(facade.token);
  22314. // In JIT mode, if the dep is an `@Attribute()` then we use the attribute name given in
  22315. // `attribute` rather than the `token`.
  22316. const token = isAttributeDep ? new WrappedNodeExpr(facade.attribute) : rawToken;
  22317. return createR3DependencyMetadata(token, isAttributeDep, facade.host, facade.optional, facade.self, facade.skipSelf);
  22318. }
  22319. function convertR3DeclareDependencyMetadata(facade) {
  22320. const isAttributeDep = facade.attribute ?? false;
  22321. const token = facade.token === null ? null : new WrappedNodeExpr(facade.token);
  22322. return createR3DependencyMetadata(token, isAttributeDep, facade.host ?? false, facade.optional ?? false, facade.self ?? false, facade.skipSelf ?? false);
  22323. }
  22324. function createR3DependencyMetadata(token, isAttributeDep, host, optional, self, skipSelf) {
  22325. // If the dep is an `@Attribute()` the `attributeNameType` ought to be the `unknown` type.
  22326. // But types are not available at runtime so we just use a literal `"<unknown>"` string as a dummy
  22327. // marker.
  22328. const attributeNameType = isAttributeDep ? literal('unknown') : null;
  22329. return { token, attributeNameType, host, optional, self, skipSelf };
  22330. }
  22331. function extractHostBindings(propMetadata, sourceSpan, host) {
  22332. // First parse the declarations from the metadata.
  22333. const bindings = parseHostBindings(host || {});
  22334. // After that check host bindings for errors
  22335. const errors = verifyHostBindings(bindings, sourceSpan);
  22336. if (errors.length) {
  22337. throw new Error(errors.map((error) => error.msg).join('\n'));
  22338. }
  22339. // Next, loop over the properties of the object, looking for @HostBinding and @HostListener.
  22340. for (const field in propMetadata) {
  22341. if (propMetadata.hasOwnProperty(field)) {
  22342. propMetadata[field].forEach(ann => {
  22343. if (isHostBinding(ann)) {
  22344. // Since this is a decorator, we know that the value is a class member. Always access it
  22345. // through `this` so that further down the line it can't be confused for a literal value
  22346. // (e.g. if there's a property called `true`).
  22347. bindings.properties[ann.hostPropertyName || field] =
  22348. getSafePropertyAccessString('this', field);
  22349. }
  22350. else if (isHostListener(ann)) {
  22351. bindings.listeners[ann.eventName || field] = `${field}(${(ann.args || []).join(',')})`;
  22352. }
  22353. });
  22354. }
  22355. }
  22356. return bindings;
  22357. }
  22358. function isHostBinding(value) {
  22359. return value.ngMetadataName === 'HostBinding';
  22360. }
  22361. function isHostListener(value) {
  22362. return value.ngMetadataName === 'HostListener';
  22363. }
  22364. function isInput(value) {
  22365. return value.ngMetadataName === 'Input';
  22366. }
  22367. function isOutput(value) {
  22368. return value.ngMetadataName === 'Output';
  22369. }
  22370. function inputsMappingToInputMetadata(inputs) {
  22371. return Object.keys(inputs).reduce((result, key) => {
  22372. const value = inputs[key];
  22373. result[key] = typeof value === 'string' ?
  22374. { bindingPropertyName: value, classPropertyName: value, required: false } :
  22375. { bindingPropertyName: value[0], classPropertyName: value[1], required: false };
  22376. return result;
  22377. }, {});
  22378. }
  22379. function parseInputsArray(values) {
  22380. return values.reduce((results, value) => {
  22381. if (typeof value === 'string') {
  22382. const [bindingPropertyName, classPropertyName] = parseMappingString(value);
  22383. results[classPropertyName] = { bindingPropertyName, classPropertyName, required: false };
  22384. }
  22385. else {
  22386. results[value.name] = {
  22387. bindingPropertyName: value.alias || value.name,
  22388. classPropertyName: value.name,
  22389. required: value.required || false
  22390. };
  22391. }
  22392. return results;
  22393. }, {});
  22394. }
  22395. function parseMappingStringArray(values) {
  22396. return values.reduce((results, value) => {
  22397. const [alias, fieldName] = parseMappingString(value);
  22398. results[fieldName] = alias;
  22399. return results;
  22400. }, {});
  22401. }
  22402. function parseMappingString(value) {
  22403. // Either the value is 'field' or 'field: property'. In the first case, `property` will
  22404. // be undefined, in which case the field name should also be used as the property name.
  22405. const [fieldName, bindingPropertyName] = value.split(':', 2).map(str => str.trim());
  22406. return [bindingPropertyName ?? fieldName, fieldName];
  22407. }
  22408. function convertDeclarePipeFacadeToMetadata(declaration) {
  22409. return {
  22410. name: declaration.type.name,
  22411. type: wrapReference(declaration.type),
  22412. typeArgumentCount: 0,
  22413. pipeName: declaration.name,
  22414. deps: null,
  22415. pure: declaration.pure ?? true,
  22416. isStandalone: declaration.isStandalone ?? false,
  22417. };
  22418. }
  22419. function convertDeclareInjectorFacadeToMetadata(declaration) {
  22420. return {
  22421. name: declaration.type.name,
  22422. type: wrapReference(declaration.type),
  22423. providers: declaration.providers !== undefined && declaration.providers.length > 0 ?
  22424. new WrappedNodeExpr(declaration.providers) :
  22425. null,
  22426. imports: declaration.imports !== undefined ?
  22427. declaration.imports.map(i => new WrappedNodeExpr(i)) :
  22428. [],
  22429. };
  22430. }
  22431. function publishFacade(global) {
  22432. const ng = global.ng || (global.ng = {});
  22433. ng.ɵcompilerFacade = new CompilerFacadeImpl();
  22434. }
  22435. /**
  22436. * @module
  22437. * @description
  22438. * Entry point for all public APIs of the compiler package.
  22439. */
  22440. const VERSION = new Version('16.0.4');
  22441. class CompilerConfig {
  22442. constructor({ defaultEncapsulation = ViewEncapsulation.Emulated, useJit = true, missingTranslation = null, preserveWhitespaces, strictInjectionParameters } = {}) {
  22443. this.defaultEncapsulation = defaultEncapsulation;
  22444. this.useJit = !!useJit;
  22445. this.missingTranslation = missingTranslation;
  22446. this.preserveWhitespaces = preserveWhitespacesDefault(noUndefined(preserveWhitespaces));
  22447. this.strictInjectionParameters = strictInjectionParameters === true;
  22448. }
  22449. }
  22450. function preserveWhitespacesDefault(preserveWhitespacesOption, defaultSetting = false) {
  22451. return preserveWhitespacesOption === null ? defaultSetting : preserveWhitespacesOption;
  22452. }
  22453. const _I18N_ATTR = 'i18n';
  22454. const _I18N_ATTR_PREFIX = 'i18n-';
  22455. const _I18N_COMMENT_PREFIX_REGEXP = /^i18n:?/;
  22456. const MEANING_SEPARATOR = '|';
  22457. const ID_SEPARATOR = '@@';
  22458. let i18nCommentsWarned = false;
  22459. /**
  22460. * Extract translatable messages from an html AST
  22461. */
  22462. function extractMessages(nodes, interpolationConfig, implicitTags, implicitAttrs) {
  22463. const visitor = new _Visitor(implicitTags, implicitAttrs);
  22464. return visitor.extract(nodes, interpolationConfig);
  22465. }
  22466. function mergeTranslations(nodes, translations, interpolationConfig, implicitTags, implicitAttrs) {
  22467. const visitor = new _Visitor(implicitTags, implicitAttrs);
  22468. return visitor.merge(nodes, translations, interpolationConfig);
  22469. }
  22470. class ExtractionResult {
  22471. constructor(messages, errors) {
  22472. this.messages = messages;
  22473. this.errors = errors;
  22474. }
  22475. }
  22476. var _VisitorMode;
  22477. (function (_VisitorMode) {
  22478. _VisitorMode[_VisitorMode["Extract"] = 0] = "Extract";
  22479. _VisitorMode[_VisitorMode["Merge"] = 1] = "Merge";
  22480. })(_VisitorMode || (_VisitorMode = {}));
  22481. /**
  22482. * This Visitor is used:
  22483. * 1. to extract all the translatable strings from an html AST (see `extract()`),
  22484. * 2. to replace the translatable strings with the actual translations (see `merge()`)
  22485. *
  22486. * @internal
  22487. */
  22488. class _Visitor {
  22489. constructor(_implicitTags, _implicitAttrs) {
  22490. this._implicitTags = _implicitTags;
  22491. this._implicitAttrs = _implicitAttrs;
  22492. }
  22493. /**
  22494. * Extracts the messages from the tree
  22495. */
  22496. extract(nodes, interpolationConfig) {
  22497. this._init(_VisitorMode.Extract, interpolationConfig);
  22498. nodes.forEach(node => node.visit(this, null));
  22499. if (this._inI18nBlock) {
  22500. this._reportError(nodes[nodes.length - 1], 'Unclosed block');
  22501. }
  22502. return new ExtractionResult(this._messages, this._errors);
  22503. }
  22504. /**
  22505. * Returns a tree where all translatable nodes are translated
  22506. */
  22507. merge(nodes, translations, interpolationConfig) {
  22508. this._init(_VisitorMode.Merge, interpolationConfig);
  22509. this._translations = translations;
  22510. // Construct a single fake root element
  22511. const wrapper = new Element('wrapper', [], nodes, undefined, undefined, undefined);
  22512. const translatedNode = wrapper.visit(this, null);
  22513. if (this._inI18nBlock) {
  22514. this._reportError(nodes[nodes.length - 1], 'Unclosed block');
  22515. }
  22516. return new ParseTreeResult(translatedNode.children, this._errors);
  22517. }
  22518. visitExpansionCase(icuCase, context) {
  22519. // Parse cases for translatable html attributes
  22520. const expression = visitAll(this, icuCase.expression, context);
  22521. if (this._mode === _VisitorMode.Merge) {
  22522. return new ExpansionCase(icuCase.value, expression, icuCase.sourceSpan, icuCase.valueSourceSpan, icuCase.expSourceSpan);
  22523. }
  22524. }
  22525. visitExpansion(icu, context) {
  22526. this._mayBeAddBlockChildren(icu);
  22527. const wasInIcu = this._inIcu;
  22528. if (!this._inIcu) {
  22529. // nested ICU messages should not be extracted but top-level translated as a whole
  22530. if (this._isInTranslatableSection) {
  22531. this._addMessage([icu]);
  22532. }
  22533. this._inIcu = true;
  22534. }
  22535. const cases = visitAll(this, icu.cases, context);
  22536. if (this._mode === _VisitorMode.Merge) {
  22537. icu = new Expansion(icu.switchValue, icu.type, cases, icu.sourceSpan, icu.switchValueSourceSpan);
  22538. }
  22539. this._inIcu = wasInIcu;
  22540. return icu;
  22541. }
  22542. visitComment(comment, context) {
  22543. const isOpening = _isOpeningComment(comment);
  22544. if (isOpening && this._isInTranslatableSection) {
  22545. this._reportError(comment, 'Could not start a block inside a translatable section');
  22546. return;
  22547. }
  22548. const isClosing = _isClosingComment(comment);
  22549. if (isClosing && !this._inI18nBlock) {
  22550. this._reportError(comment, 'Trying to close an unopened block');
  22551. return;
  22552. }
  22553. if (!this._inI18nNode && !this._inIcu) {
  22554. if (!this._inI18nBlock) {
  22555. if (isOpening) {
  22556. // deprecated from v5 you should use <ng-container i18n> instead of i18n comments
  22557. if (!i18nCommentsWarned && console && console.warn) {
  22558. i18nCommentsWarned = true;
  22559. const details = comment.sourceSpan.details ? `, ${comment.sourceSpan.details}` : '';
  22560. // TODO(ocombe): use a log service once there is a public one available
  22561. console.warn(`I18n comments are deprecated, use an <ng-container> element instead (${comment.sourceSpan.start}${details})`);
  22562. }
  22563. this._inI18nBlock = true;
  22564. this._blockStartDepth = this._depth;
  22565. this._blockChildren = [];
  22566. this._blockMeaningAndDesc =
  22567. comment.value.replace(_I18N_COMMENT_PREFIX_REGEXP, '').trim();
  22568. this._openTranslatableSection(comment);
  22569. }
  22570. }
  22571. else {
  22572. if (isClosing) {
  22573. if (this._depth == this._blockStartDepth) {
  22574. this._closeTranslatableSection(comment, this._blockChildren);
  22575. this._inI18nBlock = false;
  22576. const message = this._addMessage(this._blockChildren, this._blockMeaningAndDesc);
  22577. // merge attributes in sections
  22578. const nodes = this._translateMessage(comment, message);
  22579. return visitAll(this, nodes);
  22580. }
  22581. else {
  22582. this._reportError(comment, 'I18N blocks should not cross element boundaries');
  22583. return;
  22584. }
  22585. }
  22586. }
  22587. }
  22588. }
  22589. visitText(text, context) {
  22590. if (this._isInTranslatableSection) {
  22591. this._mayBeAddBlockChildren(text);
  22592. }
  22593. return text;
  22594. }
  22595. visitElement(el, context) {
  22596. this._mayBeAddBlockChildren(el);
  22597. this._depth++;
  22598. const wasInI18nNode = this._inI18nNode;
  22599. const wasInImplicitNode = this._inImplicitNode;
  22600. let childNodes = [];
  22601. let translatedChildNodes = undefined;
  22602. // Extract:
  22603. // - top level nodes with the (implicit) "i18n" attribute if not already in a section
  22604. // - ICU messages
  22605. const i18nAttr = _getI18nAttr(el);
  22606. const i18nMeta = i18nAttr ? i18nAttr.value : '';
  22607. const isImplicit = this._implicitTags.some(tag => el.name === tag) && !this._inIcu &&
  22608. !this._isInTranslatableSection;
  22609. const isTopLevelImplicit = !wasInImplicitNode && isImplicit;
  22610. this._inImplicitNode = wasInImplicitNode || isImplicit;
  22611. if (!this._isInTranslatableSection && !this._inIcu) {
  22612. if (i18nAttr || isTopLevelImplicit) {
  22613. this._inI18nNode = true;
  22614. const message = this._addMessage(el.children, i18nMeta);
  22615. translatedChildNodes = this._translateMessage(el, message);
  22616. }
  22617. if (this._mode == _VisitorMode.Extract) {
  22618. const isTranslatable = i18nAttr || isTopLevelImplicit;
  22619. if (isTranslatable)
  22620. this._openTranslatableSection(el);
  22621. visitAll(this, el.children);
  22622. if (isTranslatable)
  22623. this._closeTranslatableSection(el, el.children);
  22624. }
  22625. }
  22626. else {
  22627. if (i18nAttr || isTopLevelImplicit) {
  22628. this._reportError(el, 'Could not mark an element as translatable inside a translatable section');
  22629. }
  22630. if (this._mode == _VisitorMode.Extract) {
  22631. // Descend into child nodes for extraction
  22632. visitAll(this, el.children);
  22633. }
  22634. }
  22635. if (this._mode === _VisitorMode.Merge) {
  22636. const visitNodes = translatedChildNodes || el.children;
  22637. visitNodes.forEach(child => {
  22638. const visited = child.visit(this, context);
  22639. if (visited && !this._isInTranslatableSection) {
  22640. // Do not add the children from translatable sections (= i18n blocks here)
  22641. // They will be added later in this loop when the block closes (i.e. on `<!-- /i18n -->`)
  22642. childNodes = childNodes.concat(visited);
  22643. }
  22644. });
  22645. }
  22646. this._visitAttributesOf(el);
  22647. this._depth--;
  22648. this._inI18nNode = wasInI18nNode;
  22649. this._inImplicitNode = wasInImplicitNode;
  22650. if (this._mode === _VisitorMode.Merge) {
  22651. const translatedAttrs = this._translateAttributes(el);
  22652. return new Element(el.name, translatedAttrs, childNodes, el.sourceSpan, el.startSourceSpan, el.endSourceSpan);
  22653. }
  22654. return null;
  22655. }
  22656. visitAttribute(attribute, context) {
  22657. throw new Error('unreachable code');
  22658. }
  22659. _init(mode, interpolationConfig) {
  22660. this._mode = mode;
  22661. this._inI18nBlock = false;
  22662. this._inI18nNode = false;
  22663. this._depth = 0;
  22664. this._inIcu = false;
  22665. this._msgCountAtSectionStart = undefined;
  22666. this._errors = [];
  22667. this._messages = [];
  22668. this._inImplicitNode = false;
  22669. this._createI18nMessage = createI18nMessageFactory(interpolationConfig);
  22670. }
  22671. // looks for translatable attributes
  22672. _visitAttributesOf(el) {
  22673. const explicitAttrNameToValue = {};
  22674. const implicitAttrNames = this._implicitAttrs[el.name] || [];
  22675. el.attrs.filter(attr => attr.name.startsWith(_I18N_ATTR_PREFIX))
  22676. .forEach(attr => explicitAttrNameToValue[attr.name.slice(_I18N_ATTR_PREFIX.length)] =
  22677. attr.value);
  22678. el.attrs.forEach(attr => {
  22679. if (attr.name in explicitAttrNameToValue) {
  22680. this._addMessage([attr], explicitAttrNameToValue[attr.name]);
  22681. }
  22682. else if (implicitAttrNames.some(name => attr.name === name)) {
  22683. this._addMessage([attr]);
  22684. }
  22685. });
  22686. }
  22687. // add a translatable message
  22688. _addMessage(ast, msgMeta) {
  22689. if (ast.length == 0 ||
  22690. ast.length == 1 && ast[0] instanceof Attribute && !ast[0].value) {
  22691. // Do not create empty messages
  22692. return null;
  22693. }
  22694. const { meaning, description, id } = _parseMessageMeta(msgMeta);
  22695. const message = this._createI18nMessage(ast, meaning, description, id);
  22696. this._messages.push(message);
  22697. return message;
  22698. }
  22699. // Translates the given message given the `TranslationBundle`
  22700. // This is used for translating elements / blocks - see `_translateAttributes` for attributes
  22701. // no-op when called in extraction mode (returns [])
  22702. _translateMessage(el, message) {
  22703. if (message && this._mode === _VisitorMode.Merge) {
  22704. const nodes = this._translations.get(message);
  22705. if (nodes) {
  22706. return nodes;
  22707. }
  22708. this._reportError(el, `Translation unavailable for message id="${this._translations.digest(message)}"`);
  22709. }
  22710. return [];
  22711. }
  22712. // translate the attributes of an element and remove i18n specific attributes
  22713. _translateAttributes(el) {
  22714. const attributes = el.attrs;
  22715. const i18nParsedMessageMeta = {};
  22716. attributes.forEach(attr => {
  22717. if (attr.name.startsWith(_I18N_ATTR_PREFIX)) {
  22718. i18nParsedMessageMeta[attr.name.slice(_I18N_ATTR_PREFIX.length)] =
  22719. _parseMessageMeta(attr.value);
  22720. }
  22721. });
  22722. const translatedAttributes = [];
  22723. attributes.forEach((attr) => {
  22724. if (attr.name === _I18N_ATTR || attr.name.startsWith(_I18N_ATTR_PREFIX)) {
  22725. // strip i18n specific attributes
  22726. return;
  22727. }
  22728. if (attr.value && attr.value != '' && i18nParsedMessageMeta.hasOwnProperty(attr.name)) {
  22729. const { meaning, description, id } = i18nParsedMessageMeta[attr.name];
  22730. const message = this._createI18nMessage([attr], meaning, description, id);
  22731. const nodes = this._translations.get(message);
  22732. if (nodes) {
  22733. if (nodes.length == 0) {
  22734. translatedAttributes.push(new Attribute(attr.name, '', attr.sourceSpan, undefined /* keySpan */, undefined /* valueSpan */, undefined /* valueTokens */, undefined /* i18n */));
  22735. }
  22736. else if (nodes[0] instanceof Text) {
  22737. const value = nodes[0].value;
  22738. translatedAttributes.push(new Attribute(attr.name, value, attr.sourceSpan, undefined /* keySpan */, undefined /* valueSpan */, undefined /* valueTokens */, undefined /* i18n */));
  22739. }
  22740. else {
  22741. this._reportError(el, `Unexpected translation for attribute "${attr.name}" (id="${id || this._translations.digest(message)}")`);
  22742. }
  22743. }
  22744. else {
  22745. this._reportError(el, `Translation unavailable for attribute "${attr.name}" (id="${id || this._translations.digest(message)}")`);
  22746. }
  22747. }
  22748. else {
  22749. translatedAttributes.push(attr);
  22750. }
  22751. });
  22752. return translatedAttributes;
  22753. }
  22754. /**
  22755. * Add the node as a child of the block when:
  22756. * - we are in a block,
  22757. * - we are not inside a ICU message (those are handled separately),
  22758. * - the node is a "direct child" of the block
  22759. */
  22760. _mayBeAddBlockChildren(node) {
  22761. if (this._inI18nBlock && !this._inIcu && this._depth == this._blockStartDepth) {
  22762. this._blockChildren.push(node);
  22763. }
  22764. }
  22765. /**
  22766. * Marks the start of a section, see `_closeTranslatableSection`
  22767. */
  22768. _openTranslatableSection(node) {
  22769. if (this._isInTranslatableSection) {
  22770. this._reportError(node, 'Unexpected section start');
  22771. }
  22772. else {
  22773. this._msgCountAtSectionStart = this._messages.length;
  22774. }
  22775. }
  22776. /**
  22777. * A translatable section could be:
  22778. * - the content of translatable element,
  22779. * - nodes between `<!-- i18n -->` and `<!-- /i18n -->` comments
  22780. */
  22781. get _isInTranslatableSection() {
  22782. return this._msgCountAtSectionStart !== void 0;
  22783. }
  22784. /**
  22785. * Terminates a section.
  22786. *
  22787. * If a section has only one significant children (comments not significant) then we should not
  22788. * keep the message from this children:
  22789. *
  22790. * `<p i18n="meaning|description">{ICU message}</p>` would produce two messages:
  22791. * - one for the <p> content with meaning and description,
  22792. * - another one for the ICU message.
  22793. *
  22794. * In this case the last message is discarded as it contains less information (the AST is
  22795. * otherwise identical).
  22796. *
  22797. * Note that we should still keep messages extracted from attributes inside the section (ie in the
  22798. * ICU message here)
  22799. */
  22800. _closeTranslatableSection(node, directChildren) {
  22801. if (!this._isInTranslatableSection) {
  22802. this._reportError(node, 'Unexpected section end');
  22803. return;
  22804. }
  22805. const startIndex = this._msgCountAtSectionStart;
  22806. const significantChildren = directChildren.reduce((count, node) => count + (node instanceof Comment ? 0 : 1), 0);
  22807. if (significantChildren == 1) {
  22808. for (let i = this._messages.length - 1; i >= startIndex; i--) {
  22809. const ast = this._messages[i].nodes;
  22810. if (!(ast.length == 1 && ast[0] instanceof Text$2)) {
  22811. this._messages.splice(i, 1);
  22812. break;
  22813. }
  22814. }
  22815. }
  22816. this._msgCountAtSectionStart = undefined;
  22817. }
  22818. _reportError(node, msg) {
  22819. this._errors.push(new I18nError(node.sourceSpan, msg));
  22820. }
  22821. }
  22822. function _isOpeningComment(n) {
  22823. return !!(n instanceof Comment && n.value && n.value.startsWith('i18n'));
  22824. }
  22825. function _isClosingComment(n) {
  22826. return !!(n instanceof Comment && n.value && n.value === '/i18n');
  22827. }
  22828. function _getI18nAttr(p) {
  22829. return p.attrs.find(attr => attr.name === _I18N_ATTR) || null;
  22830. }
  22831. function _parseMessageMeta(i18n) {
  22832. if (!i18n)
  22833. return { meaning: '', description: '', id: '' };
  22834. const idIndex = i18n.indexOf(ID_SEPARATOR);
  22835. const descIndex = i18n.indexOf(MEANING_SEPARATOR);
  22836. const [meaningAndDesc, id] = (idIndex > -1) ? [i18n.slice(0, idIndex), i18n.slice(idIndex + 2)] : [i18n, ''];
  22837. const [meaning, description] = (descIndex > -1) ?
  22838. [meaningAndDesc.slice(0, descIndex), meaningAndDesc.slice(descIndex + 1)] :
  22839. ['', meaningAndDesc];
  22840. return { meaning, description, id: id.trim() };
  22841. }
  22842. class XmlTagDefinition {
  22843. constructor() {
  22844. this.closedByParent = false;
  22845. this.implicitNamespacePrefix = null;
  22846. this.isVoid = false;
  22847. this.ignoreFirstLf = false;
  22848. this.canSelfClose = true;
  22849. this.preventNamespaceInheritance = false;
  22850. }
  22851. requireExtraParent(currentParent) {
  22852. return false;
  22853. }
  22854. isClosedByChild(name) {
  22855. return false;
  22856. }
  22857. getContentType() {
  22858. return TagContentType.PARSABLE_DATA;
  22859. }
  22860. }
  22861. const _TAG_DEFINITION = new XmlTagDefinition();
  22862. function getXmlTagDefinition(tagName) {
  22863. return _TAG_DEFINITION;
  22864. }
  22865. class XmlParser extends Parser {
  22866. constructor() {
  22867. super(getXmlTagDefinition);
  22868. }
  22869. parse(source, url, options) {
  22870. return super.parse(source, url, options);
  22871. }
  22872. }
  22873. const _VERSION$1 = '1.2';
  22874. const _XMLNS$1 = 'urn:oasis:names:tc:xliff:document:1.2';
  22875. // TODO(vicb): make this a param (s/_/-/)
  22876. const _DEFAULT_SOURCE_LANG$1 = 'en';
  22877. const _PLACEHOLDER_TAG$2 = 'x';
  22878. const _MARKER_TAG$1 = 'mrk';
  22879. const _FILE_TAG = 'file';
  22880. const _SOURCE_TAG$1 = 'source';
  22881. const _SEGMENT_SOURCE_TAG = 'seg-source';
  22882. const _ALT_TRANS_TAG = 'alt-trans';
  22883. const _TARGET_TAG$1 = 'target';
  22884. const _UNIT_TAG$1 = 'trans-unit';
  22885. const _CONTEXT_GROUP_TAG = 'context-group';
  22886. const _CONTEXT_TAG = 'context';
  22887. // https://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html
  22888. // https://docs.oasis-open.org/xliff/v1.2/xliff-profile-html/xliff-profile-html-1.2.html
  22889. class Xliff extends Serializer {
  22890. write(messages, locale) {
  22891. const visitor = new _WriteVisitor$1();
  22892. const transUnits = [];
  22893. messages.forEach(message => {
  22894. let contextTags = [];
  22895. message.sources.forEach((source) => {
  22896. let contextGroupTag = new Tag(_CONTEXT_GROUP_TAG, { purpose: 'location' });
  22897. contextGroupTag.children.push(new CR(10), new Tag(_CONTEXT_TAG, { 'context-type': 'sourcefile' }, [new Text$1(source.filePath)]), new CR(10), new Tag(_CONTEXT_TAG, { 'context-type': 'linenumber' }, [new Text$1(`${source.startLine}`)]), new CR(8));
  22898. contextTags.push(new CR(8), contextGroupTag);
  22899. });
  22900. const transUnit = new Tag(_UNIT_TAG$1, { id: message.id, datatype: 'html' });
  22901. transUnit.children.push(new CR(8), new Tag(_SOURCE_TAG$1, {}, visitor.serialize(message.nodes)), ...contextTags);
  22902. if (message.description) {
  22903. transUnit.children.push(new CR(8), new Tag('note', { priority: '1', from: 'description' }, [new Text$1(message.description)]));
  22904. }
  22905. if (message.meaning) {
  22906. transUnit.children.push(new CR(8), new Tag('note', { priority: '1', from: 'meaning' }, [new Text$1(message.meaning)]));
  22907. }
  22908. transUnit.children.push(new CR(6));
  22909. transUnits.push(new CR(6), transUnit);
  22910. });
  22911. const body = new Tag('body', {}, [...transUnits, new CR(4)]);
  22912. const file = new Tag('file', {
  22913. 'source-language': locale || _DEFAULT_SOURCE_LANG$1,
  22914. datatype: 'plaintext',
  22915. original: 'ng2.template',
  22916. }, [new CR(4), body, new CR(2)]);
  22917. const xliff = new Tag('xliff', { version: _VERSION$1, xmlns: _XMLNS$1 }, [new CR(2), file, new CR()]);
  22918. return serialize([
  22919. new Declaration({ version: '1.0', encoding: 'UTF-8' }), new CR(), xliff, new CR()
  22920. ]);
  22921. }
  22922. load(content, url) {
  22923. // xliff to xml nodes
  22924. const xliffParser = new XliffParser();
  22925. const { locale, msgIdToHtml, errors } = xliffParser.parse(content, url);
  22926. // xml nodes to i18n nodes
  22927. const i18nNodesByMsgId = {};
  22928. const converter = new XmlToI18n$2();
  22929. Object.keys(msgIdToHtml).forEach(msgId => {
  22930. const { i18nNodes, errors: e } = converter.convert(msgIdToHtml[msgId], url);
  22931. errors.push(...e);
  22932. i18nNodesByMsgId[msgId] = i18nNodes;
  22933. });
  22934. if (errors.length) {
  22935. throw new Error(`xliff parse errors:\n${errors.join('\n')}`);
  22936. }
  22937. return { locale: locale, i18nNodesByMsgId };
  22938. }
  22939. digest(message) {
  22940. return digest$1(message);
  22941. }
  22942. }
  22943. class _WriteVisitor$1 {
  22944. visitText(text, context) {
  22945. return [new Text$1(text.value)];
  22946. }
  22947. visitContainer(container, context) {
  22948. const nodes = [];
  22949. container.children.forEach((node) => nodes.push(...node.visit(this)));
  22950. return nodes;
  22951. }
  22952. visitIcu(icu, context) {
  22953. const nodes = [new Text$1(`{${icu.expressionPlaceholder}, ${icu.type}, `)];
  22954. Object.keys(icu.cases).forEach((c) => {
  22955. nodes.push(new Text$1(`${c} {`), ...icu.cases[c].visit(this), new Text$1(`} `));
  22956. });
  22957. nodes.push(new Text$1(`}`));
  22958. return nodes;
  22959. }
  22960. visitTagPlaceholder(ph, context) {
  22961. const ctype = getCtypeForTag(ph.tag);
  22962. if (ph.isVoid) {
  22963. // void tags have no children nor closing tags
  22964. return [new Tag(_PLACEHOLDER_TAG$2, { id: ph.startName, ctype, 'equiv-text': `<${ph.tag}/>` })];
  22965. }
  22966. const startTagPh = new Tag(_PLACEHOLDER_TAG$2, { id: ph.startName, ctype, 'equiv-text': `<${ph.tag}>` });
  22967. const closeTagPh = new Tag(_PLACEHOLDER_TAG$2, { id: ph.closeName, ctype, 'equiv-text': `</${ph.tag}>` });
  22968. return [startTagPh, ...this.serialize(ph.children), closeTagPh];
  22969. }
  22970. visitPlaceholder(ph, context) {
  22971. return [new Tag(_PLACEHOLDER_TAG$2, { id: ph.name, 'equiv-text': `{{${ph.value}}}` })];
  22972. }
  22973. visitIcuPlaceholder(ph, context) {
  22974. const equivText = `{${ph.value.expression}, ${ph.value.type}, ${Object.keys(ph.value.cases).map((value) => value + ' {...}').join(' ')}}`;
  22975. return [new Tag(_PLACEHOLDER_TAG$2, { id: ph.name, 'equiv-text': equivText })];
  22976. }
  22977. serialize(nodes) {
  22978. return [].concat(...nodes.map(node => node.visit(this)));
  22979. }
  22980. }
  22981. // TODO(vicb): add error management (structure)
  22982. // Extract messages as xml nodes from the xliff file
  22983. class XliffParser {
  22984. constructor() {
  22985. this._locale = null;
  22986. }
  22987. parse(xliff, url) {
  22988. this._unitMlString = null;
  22989. this._msgIdToHtml = {};
  22990. const xml = new XmlParser().parse(xliff, url);
  22991. this._errors = xml.errors;
  22992. visitAll(this, xml.rootNodes, null);
  22993. return {
  22994. msgIdToHtml: this._msgIdToHtml,
  22995. errors: this._errors,
  22996. locale: this._locale,
  22997. };
  22998. }
  22999. visitElement(element, context) {
  23000. switch (element.name) {
  23001. case _UNIT_TAG$1:
  23002. this._unitMlString = null;
  23003. const idAttr = element.attrs.find((attr) => attr.name === 'id');
  23004. if (!idAttr) {
  23005. this._addError(element, `<${_UNIT_TAG$1}> misses the "id" attribute`);
  23006. }
  23007. else {
  23008. const id = idAttr.value;
  23009. if (this._msgIdToHtml.hasOwnProperty(id)) {
  23010. this._addError(element, `Duplicated translations for msg ${id}`);
  23011. }
  23012. else {
  23013. visitAll(this, element.children, null);
  23014. if (typeof this._unitMlString === 'string') {
  23015. this._msgIdToHtml[id] = this._unitMlString;
  23016. }
  23017. else {
  23018. this._addError(element, `Message ${id} misses a translation`);
  23019. }
  23020. }
  23021. }
  23022. break;
  23023. // ignore those tags
  23024. case _SOURCE_TAG$1:
  23025. case _SEGMENT_SOURCE_TAG:
  23026. case _ALT_TRANS_TAG:
  23027. break;
  23028. case _TARGET_TAG$1:
  23029. const innerTextStart = element.startSourceSpan.end.offset;
  23030. const innerTextEnd = element.endSourceSpan.start.offset;
  23031. const content = element.startSourceSpan.start.file.content;
  23032. const innerText = content.slice(innerTextStart, innerTextEnd);
  23033. this._unitMlString = innerText;
  23034. break;
  23035. case _FILE_TAG:
  23036. const localeAttr = element.attrs.find((attr) => attr.name === 'target-language');
  23037. if (localeAttr) {
  23038. this._locale = localeAttr.value;
  23039. }
  23040. visitAll(this, element.children, null);
  23041. break;
  23042. default:
  23043. // TODO(vicb): assert file structure, xliff version
  23044. // For now only recurse on unhandled nodes
  23045. visitAll(this, element.children, null);
  23046. }
  23047. }
  23048. visitAttribute(attribute, context) { }
  23049. visitText(text, context) { }
  23050. visitComment(comment, context) { }
  23051. visitExpansion(expansion, context) { }
  23052. visitExpansionCase(expansionCase, context) { }
  23053. _addError(node, message) {
  23054. this._errors.push(new I18nError(node.sourceSpan, message));
  23055. }
  23056. }
  23057. // Convert ml nodes (xliff syntax) to i18n nodes
  23058. class XmlToI18n$2 {
  23059. convert(message, url) {
  23060. const xmlIcu = new XmlParser().parse(message, url, { tokenizeExpansionForms: true });
  23061. this._errors = xmlIcu.errors;
  23062. const i18nNodes = this._errors.length > 0 || xmlIcu.rootNodes.length == 0 ?
  23063. [] :
  23064. [].concat(...visitAll(this, xmlIcu.rootNodes));
  23065. return {
  23066. i18nNodes: i18nNodes,
  23067. errors: this._errors,
  23068. };
  23069. }
  23070. visitText(text, context) {
  23071. return new Text$2(text.value, text.sourceSpan);
  23072. }
  23073. visitElement(el, context) {
  23074. if (el.name === _PLACEHOLDER_TAG$2) {
  23075. const nameAttr = el.attrs.find((attr) => attr.name === 'id');
  23076. if (nameAttr) {
  23077. return new Placeholder('', nameAttr.value, el.sourceSpan);
  23078. }
  23079. this._addError(el, `<${_PLACEHOLDER_TAG$2}> misses the "id" attribute`);
  23080. return null;
  23081. }
  23082. if (el.name === _MARKER_TAG$1) {
  23083. return [].concat(...visitAll(this, el.children));
  23084. }
  23085. this._addError(el, `Unexpected tag`);
  23086. return null;
  23087. }
  23088. visitExpansion(icu, context) {
  23089. const caseMap = {};
  23090. visitAll(this, icu.cases).forEach((c) => {
  23091. caseMap[c.value] = new Container(c.nodes, icu.sourceSpan);
  23092. });
  23093. return new Icu(icu.switchValue, icu.type, caseMap, icu.sourceSpan);
  23094. }
  23095. visitExpansionCase(icuCase, context) {
  23096. return {
  23097. value: icuCase.value,
  23098. nodes: visitAll(this, icuCase.expression),
  23099. };
  23100. }
  23101. visitComment(comment, context) { }
  23102. visitAttribute(attribute, context) { }
  23103. _addError(node, message) {
  23104. this._errors.push(new I18nError(node.sourceSpan, message));
  23105. }
  23106. }
  23107. function getCtypeForTag(tag) {
  23108. switch (tag.toLowerCase()) {
  23109. case 'br':
  23110. return 'lb';
  23111. case 'img':
  23112. return 'image';
  23113. default:
  23114. return `x-${tag}`;
  23115. }
  23116. }
  23117. const _VERSION = '2.0';
  23118. const _XMLNS = 'urn:oasis:names:tc:xliff:document:2.0';
  23119. // TODO(vicb): make this a param (s/_/-/)
  23120. const _DEFAULT_SOURCE_LANG = 'en';
  23121. const _PLACEHOLDER_TAG$1 = 'ph';
  23122. const _PLACEHOLDER_SPANNING_TAG = 'pc';
  23123. const _MARKER_TAG = 'mrk';
  23124. const _XLIFF_TAG = 'xliff';
  23125. const _SOURCE_TAG = 'source';
  23126. const _TARGET_TAG = 'target';
  23127. const _UNIT_TAG = 'unit';
  23128. // https://docs.oasis-open.org/xliff/xliff-core/v2.0/os/xliff-core-v2.0-os.html
  23129. class Xliff2 extends Serializer {
  23130. write(messages, locale) {
  23131. const visitor = new _WriteVisitor();
  23132. const units = [];
  23133. messages.forEach(message => {
  23134. const unit = new Tag(_UNIT_TAG, { id: message.id });
  23135. const notes = new Tag('notes');
  23136. if (message.description || message.meaning) {
  23137. if (message.description) {
  23138. notes.children.push(new CR(8), new Tag('note', { category: 'description' }, [new Text$1(message.description)]));
  23139. }
  23140. if (message.meaning) {
  23141. notes.children.push(new CR(8), new Tag('note', { category: 'meaning' }, [new Text$1(message.meaning)]));
  23142. }
  23143. }
  23144. message.sources.forEach((source) => {
  23145. notes.children.push(new CR(8), new Tag('note', { category: 'location' }, [
  23146. new Text$1(`${source.filePath}:${source.startLine}${source.endLine !== source.startLine ? ',' + source.endLine : ''}`)
  23147. ]));
  23148. });
  23149. notes.children.push(new CR(6));
  23150. unit.children.push(new CR(6), notes);
  23151. const segment = new Tag('segment');
  23152. segment.children.push(new CR(8), new Tag(_SOURCE_TAG, {}, visitor.serialize(message.nodes)), new CR(6));
  23153. unit.children.push(new CR(6), segment, new CR(4));
  23154. units.push(new CR(4), unit);
  23155. });
  23156. const file = new Tag('file', { 'original': 'ng.template', id: 'ngi18n' }, [...units, new CR(2)]);
  23157. const xliff = new Tag(_XLIFF_TAG, { version: _VERSION, xmlns: _XMLNS, srcLang: locale || _DEFAULT_SOURCE_LANG }, [new CR(2), file, new CR()]);
  23158. return serialize([
  23159. new Declaration({ version: '1.0', encoding: 'UTF-8' }), new CR(), xliff, new CR()
  23160. ]);
  23161. }
  23162. load(content, url) {
  23163. // xliff to xml nodes
  23164. const xliff2Parser = new Xliff2Parser();
  23165. const { locale, msgIdToHtml, errors } = xliff2Parser.parse(content, url);
  23166. // xml nodes to i18n nodes
  23167. const i18nNodesByMsgId = {};
  23168. const converter = new XmlToI18n$1();
  23169. Object.keys(msgIdToHtml).forEach(msgId => {
  23170. const { i18nNodes, errors: e } = converter.convert(msgIdToHtml[msgId], url);
  23171. errors.push(...e);
  23172. i18nNodesByMsgId[msgId] = i18nNodes;
  23173. });
  23174. if (errors.length) {
  23175. throw new Error(`xliff2 parse errors:\n${errors.join('\n')}`);
  23176. }
  23177. return { locale: locale, i18nNodesByMsgId };
  23178. }
  23179. digest(message) {
  23180. return decimalDigest(message);
  23181. }
  23182. }
  23183. class _WriteVisitor {
  23184. constructor() {
  23185. this._nextPlaceholderId = 0;
  23186. }
  23187. visitText(text, context) {
  23188. return [new Text$1(text.value)];
  23189. }
  23190. visitContainer(container, context) {
  23191. const nodes = [];
  23192. container.children.forEach((node) => nodes.push(...node.visit(this)));
  23193. return nodes;
  23194. }
  23195. visitIcu(icu, context) {
  23196. const nodes = [new Text$1(`{${icu.expressionPlaceholder}, ${icu.type}, `)];
  23197. Object.keys(icu.cases).forEach((c) => {
  23198. nodes.push(new Text$1(`${c} {`), ...icu.cases[c].visit(this), new Text$1(`} `));
  23199. });
  23200. nodes.push(new Text$1(`}`));
  23201. return nodes;
  23202. }
  23203. visitTagPlaceholder(ph, context) {
  23204. const type = getTypeForTag(ph.tag);
  23205. if (ph.isVoid) {
  23206. const tagPh = new Tag(_PLACEHOLDER_TAG$1, {
  23207. id: (this._nextPlaceholderId++).toString(),
  23208. equiv: ph.startName,
  23209. type: type,
  23210. disp: `<${ph.tag}/>`,
  23211. });
  23212. return [tagPh];
  23213. }
  23214. const tagPc = new Tag(_PLACEHOLDER_SPANNING_TAG, {
  23215. id: (this._nextPlaceholderId++).toString(),
  23216. equivStart: ph.startName,
  23217. equivEnd: ph.closeName,
  23218. type: type,
  23219. dispStart: `<${ph.tag}>`,
  23220. dispEnd: `</${ph.tag}>`,
  23221. });
  23222. const nodes = [].concat(...ph.children.map(node => node.visit(this)));
  23223. if (nodes.length) {
  23224. nodes.forEach((node) => tagPc.children.push(node));
  23225. }
  23226. else {
  23227. tagPc.children.push(new Text$1(''));
  23228. }
  23229. return [tagPc];
  23230. }
  23231. visitPlaceholder(ph, context) {
  23232. const idStr = (this._nextPlaceholderId++).toString();
  23233. return [new Tag(_PLACEHOLDER_TAG$1, {
  23234. id: idStr,
  23235. equiv: ph.name,
  23236. disp: `{{${ph.value}}}`,
  23237. })];
  23238. }
  23239. visitIcuPlaceholder(ph, context) {
  23240. const cases = Object.keys(ph.value.cases).map((value) => value + ' {...}').join(' ');
  23241. const idStr = (this._nextPlaceholderId++).toString();
  23242. return [new Tag(_PLACEHOLDER_TAG$1, { id: idStr, equiv: ph.name, disp: `{${ph.value.expression}, ${ph.value.type}, ${cases}}` })];
  23243. }
  23244. serialize(nodes) {
  23245. this._nextPlaceholderId = 0;
  23246. return [].concat(...nodes.map(node => node.visit(this)));
  23247. }
  23248. }
  23249. // Extract messages as xml nodes from the xliff file
  23250. class Xliff2Parser {
  23251. constructor() {
  23252. this._locale = null;
  23253. }
  23254. parse(xliff, url) {
  23255. this._unitMlString = null;
  23256. this._msgIdToHtml = {};
  23257. const xml = new XmlParser().parse(xliff, url);
  23258. this._errors = xml.errors;
  23259. visitAll(this, xml.rootNodes, null);
  23260. return {
  23261. msgIdToHtml: this._msgIdToHtml,
  23262. errors: this._errors,
  23263. locale: this._locale,
  23264. };
  23265. }
  23266. visitElement(element, context) {
  23267. switch (element.name) {
  23268. case _UNIT_TAG:
  23269. this._unitMlString = null;
  23270. const idAttr = element.attrs.find((attr) => attr.name === 'id');
  23271. if (!idAttr) {
  23272. this._addError(element, `<${_UNIT_TAG}> misses the "id" attribute`);
  23273. }
  23274. else {
  23275. const id = idAttr.value;
  23276. if (this._msgIdToHtml.hasOwnProperty(id)) {
  23277. this._addError(element, `Duplicated translations for msg ${id}`);
  23278. }
  23279. else {
  23280. visitAll(this, element.children, null);
  23281. if (typeof this._unitMlString === 'string') {
  23282. this._msgIdToHtml[id] = this._unitMlString;
  23283. }
  23284. else {
  23285. this._addError(element, `Message ${id} misses a translation`);
  23286. }
  23287. }
  23288. }
  23289. break;
  23290. case _SOURCE_TAG:
  23291. // ignore source message
  23292. break;
  23293. case _TARGET_TAG:
  23294. const innerTextStart = element.startSourceSpan.end.offset;
  23295. const innerTextEnd = element.endSourceSpan.start.offset;
  23296. const content = element.startSourceSpan.start.file.content;
  23297. const innerText = content.slice(innerTextStart, innerTextEnd);
  23298. this._unitMlString = innerText;
  23299. break;
  23300. case _XLIFF_TAG:
  23301. const localeAttr = element.attrs.find((attr) => attr.name === 'trgLang');
  23302. if (localeAttr) {
  23303. this._locale = localeAttr.value;
  23304. }
  23305. const versionAttr = element.attrs.find((attr) => attr.name === 'version');
  23306. if (versionAttr) {
  23307. const version = versionAttr.value;
  23308. if (version !== '2.0') {
  23309. this._addError(element, `The XLIFF file version ${version} is not compatible with XLIFF 2.0 serializer`);
  23310. }
  23311. else {
  23312. visitAll(this, element.children, null);
  23313. }
  23314. }
  23315. break;
  23316. default:
  23317. visitAll(this, element.children, null);
  23318. }
  23319. }
  23320. visitAttribute(attribute, context) { }
  23321. visitText(text, context) { }
  23322. visitComment(comment, context) { }
  23323. visitExpansion(expansion, context) { }
  23324. visitExpansionCase(expansionCase, context) { }
  23325. _addError(node, message) {
  23326. this._errors.push(new I18nError(node.sourceSpan, message));
  23327. }
  23328. }
  23329. // Convert ml nodes (xliff syntax) to i18n nodes
  23330. class XmlToI18n$1 {
  23331. convert(message, url) {
  23332. const xmlIcu = new XmlParser().parse(message, url, { tokenizeExpansionForms: true });
  23333. this._errors = xmlIcu.errors;
  23334. const i18nNodes = this._errors.length > 0 || xmlIcu.rootNodes.length == 0 ?
  23335. [] :
  23336. [].concat(...visitAll(this, xmlIcu.rootNodes));
  23337. return {
  23338. i18nNodes,
  23339. errors: this._errors,
  23340. };
  23341. }
  23342. visitText(text, context) {
  23343. return new Text$2(text.value, text.sourceSpan);
  23344. }
  23345. visitElement(el, context) {
  23346. switch (el.name) {
  23347. case _PLACEHOLDER_TAG$1:
  23348. const nameAttr = el.attrs.find((attr) => attr.name === 'equiv');
  23349. if (nameAttr) {
  23350. return [new Placeholder('', nameAttr.value, el.sourceSpan)];
  23351. }
  23352. this._addError(el, `<${_PLACEHOLDER_TAG$1}> misses the "equiv" attribute`);
  23353. break;
  23354. case _PLACEHOLDER_SPANNING_TAG:
  23355. const startAttr = el.attrs.find((attr) => attr.name === 'equivStart');
  23356. const endAttr = el.attrs.find((attr) => attr.name === 'equivEnd');
  23357. if (!startAttr) {
  23358. this._addError(el, `<${_PLACEHOLDER_TAG$1}> misses the "equivStart" attribute`);
  23359. }
  23360. else if (!endAttr) {
  23361. this._addError(el, `<${_PLACEHOLDER_TAG$1}> misses the "equivEnd" attribute`);
  23362. }
  23363. else {
  23364. const startId = startAttr.value;
  23365. const endId = endAttr.value;
  23366. const nodes = [];
  23367. return nodes.concat(new Placeholder('', startId, el.sourceSpan), ...el.children.map(node => node.visit(this, null)), new Placeholder('', endId, el.sourceSpan));
  23368. }
  23369. break;
  23370. case _MARKER_TAG:
  23371. return [].concat(...visitAll(this, el.children));
  23372. default:
  23373. this._addError(el, `Unexpected tag`);
  23374. }
  23375. return null;
  23376. }
  23377. visitExpansion(icu, context) {
  23378. const caseMap = {};
  23379. visitAll(this, icu.cases).forEach((c) => {
  23380. caseMap[c.value] = new Container(c.nodes, icu.sourceSpan);
  23381. });
  23382. return new Icu(icu.switchValue, icu.type, caseMap, icu.sourceSpan);
  23383. }
  23384. visitExpansionCase(icuCase, context) {
  23385. return {
  23386. value: icuCase.value,
  23387. nodes: [].concat(...visitAll(this, icuCase.expression)),
  23388. };
  23389. }
  23390. visitComment(comment, context) { }
  23391. visitAttribute(attribute, context) { }
  23392. _addError(node, message) {
  23393. this._errors.push(new I18nError(node.sourceSpan, message));
  23394. }
  23395. }
  23396. function getTypeForTag(tag) {
  23397. switch (tag.toLowerCase()) {
  23398. case 'br':
  23399. case 'b':
  23400. case 'i':
  23401. case 'u':
  23402. return 'fmt';
  23403. case 'img':
  23404. return 'image';
  23405. case 'a':
  23406. return 'link';
  23407. default:
  23408. return 'other';
  23409. }
  23410. }
  23411. const _TRANSLATIONS_TAG = 'translationbundle';
  23412. const _TRANSLATION_TAG = 'translation';
  23413. const _PLACEHOLDER_TAG = 'ph';
  23414. class Xtb extends Serializer {
  23415. write(messages, locale) {
  23416. throw new Error('Unsupported');
  23417. }
  23418. load(content, url) {
  23419. // xtb to xml nodes
  23420. const xtbParser = new XtbParser();
  23421. const { locale, msgIdToHtml, errors } = xtbParser.parse(content, url);
  23422. // xml nodes to i18n nodes
  23423. const i18nNodesByMsgId = {};
  23424. const converter = new XmlToI18n();
  23425. // Because we should be able to load xtb files that rely on features not supported by angular,
  23426. // we need to delay the conversion of html to i18n nodes so that non angular messages are not
  23427. // converted
  23428. Object.keys(msgIdToHtml).forEach(msgId => {
  23429. const valueFn = function () {
  23430. const { i18nNodes, errors } = converter.convert(msgIdToHtml[msgId], url);
  23431. if (errors.length) {
  23432. throw new Error(`xtb parse errors:\n${errors.join('\n')}`);
  23433. }
  23434. return i18nNodes;
  23435. };
  23436. createLazyProperty(i18nNodesByMsgId, msgId, valueFn);
  23437. });
  23438. if (errors.length) {
  23439. throw new Error(`xtb parse errors:\n${errors.join('\n')}`);
  23440. }
  23441. return { locale: locale, i18nNodesByMsgId };
  23442. }
  23443. digest(message) {
  23444. return digest(message);
  23445. }
  23446. createNameMapper(message) {
  23447. return new SimplePlaceholderMapper(message, toPublicName);
  23448. }
  23449. }
  23450. function createLazyProperty(messages, id, valueFn) {
  23451. Object.defineProperty(messages, id, {
  23452. configurable: true,
  23453. enumerable: true,
  23454. get: function () {
  23455. const value = valueFn();
  23456. Object.defineProperty(messages, id, { enumerable: true, value });
  23457. return value;
  23458. },
  23459. set: _ => {
  23460. throw new Error('Could not overwrite an XTB translation');
  23461. },
  23462. });
  23463. }
  23464. // Extract messages as xml nodes from the xtb file
  23465. class XtbParser {
  23466. constructor() {
  23467. this._locale = null;
  23468. }
  23469. parse(xtb, url) {
  23470. this._bundleDepth = 0;
  23471. this._msgIdToHtml = {};
  23472. // We can not parse the ICU messages at this point as some messages might not originate
  23473. // from Angular that could not be lex'd.
  23474. const xml = new XmlParser().parse(xtb, url);
  23475. this._errors = xml.errors;
  23476. visitAll(this, xml.rootNodes);
  23477. return {
  23478. msgIdToHtml: this._msgIdToHtml,
  23479. errors: this._errors,
  23480. locale: this._locale,
  23481. };
  23482. }
  23483. visitElement(element, context) {
  23484. switch (element.name) {
  23485. case _TRANSLATIONS_TAG:
  23486. this._bundleDepth++;
  23487. if (this._bundleDepth > 1) {
  23488. this._addError(element, `<${_TRANSLATIONS_TAG}> elements can not be nested`);
  23489. }
  23490. const langAttr = element.attrs.find((attr) => attr.name === 'lang');
  23491. if (langAttr) {
  23492. this._locale = langAttr.value;
  23493. }
  23494. visitAll(this, element.children, null);
  23495. this._bundleDepth--;
  23496. break;
  23497. case _TRANSLATION_TAG:
  23498. const idAttr = element.attrs.find((attr) => attr.name === 'id');
  23499. if (!idAttr) {
  23500. this._addError(element, `<${_TRANSLATION_TAG}> misses the "id" attribute`);
  23501. }
  23502. else {
  23503. const id = idAttr.value;
  23504. if (this._msgIdToHtml.hasOwnProperty(id)) {
  23505. this._addError(element, `Duplicated translations for msg ${id}`);
  23506. }
  23507. else {
  23508. const innerTextStart = element.startSourceSpan.end.offset;
  23509. const innerTextEnd = element.endSourceSpan.start.offset;
  23510. const content = element.startSourceSpan.start.file.content;
  23511. const innerText = content.slice(innerTextStart, innerTextEnd);
  23512. this._msgIdToHtml[id] = innerText;
  23513. }
  23514. }
  23515. break;
  23516. default:
  23517. this._addError(element, 'Unexpected tag');
  23518. }
  23519. }
  23520. visitAttribute(attribute, context) { }
  23521. visitText(text, context) { }
  23522. visitComment(comment, context) { }
  23523. visitExpansion(expansion, context) { }
  23524. visitExpansionCase(expansionCase, context) { }
  23525. _addError(node, message) {
  23526. this._errors.push(new I18nError(node.sourceSpan, message));
  23527. }
  23528. }
  23529. // Convert ml nodes (xtb syntax) to i18n nodes
  23530. class XmlToI18n {
  23531. convert(message, url) {
  23532. const xmlIcu = new XmlParser().parse(message, url, { tokenizeExpansionForms: true });
  23533. this._errors = xmlIcu.errors;
  23534. const i18nNodes = this._errors.length > 0 || xmlIcu.rootNodes.length == 0 ?
  23535. [] :
  23536. visitAll(this, xmlIcu.rootNodes);
  23537. return {
  23538. i18nNodes,
  23539. errors: this._errors,
  23540. };
  23541. }
  23542. visitText(text, context) {
  23543. return new Text$2(text.value, text.sourceSpan);
  23544. }
  23545. visitExpansion(icu, context) {
  23546. const caseMap = {};
  23547. visitAll(this, icu.cases).forEach(c => {
  23548. caseMap[c.value] = new Container(c.nodes, icu.sourceSpan);
  23549. });
  23550. return new Icu(icu.switchValue, icu.type, caseMap, icu.sourceSpan);
  23551. }
  23552. visitExpansionCase(icuCase, context) {
  23553. return {
  23554. value: icuCase.value,
  23555. nodes: visitAll(this, icuCase.expression),
  23556. };
  23557. }
  23558. visitElement(el, context) {
  23559. if (el.name === _PLACEHOLDER_TAG) {
  23560. const nameAttr = el.attrs.find((attr) => attr.name === 'name');
  23561. if (nameAttr) {
  23562. return new Placeholder('', nameAttr.value, el.sourceSpan);
  23563. }
  23564. this._addError(el, `<${_PLACEHOLDER_TAG}> misses the "name" attribute`);
  23565. }
  23566. else {
  23567. this._addError(el, `Unexpected tag`);
  23568. }
  23569. return null;
  23570. }
  23571. visitComment(comment, context) { }
  23572. visitAttribute(attribute, context) { }
  23573. _addError(node, message) {
  23574. this._errors.push(new I18nError(node.sourceSpan, message));
  23575. }
  23576. }
  23577. /**
  23578. * A container for translated messages
  23579. */
  23580. class TranslationBundle {
  23581. constructor(_i18nNodesByMsgId = {}, locale, digest, mapperFactory, missingTranslationStrategy = MissingTranslationStrategy.Warning, console) {
  23582. this._i18nNodesByMsgId = _i18nNodesByMsgId;
  23583. this.digest = digest;
  23584. this.mapperFactory = mapperFactory;
  23585. this._i18nToHtml = new I18nToHtmlVisitor(_i18nNodesByMsgId, locale, digest, mapperFactory, missingTranslationStrategy, console);
  23586. }
  23587. // Creates a `TranslationBundle` by parsing the given `content` with the `serializer`.
  23588. static load(content, url, serializer, missingTranslationStrategy, console) {
  23589. const { locale, i18nNodesByMsgId } = serializer.load(content, url);
  23590. const digestFn = (m) => serializer.digest(m);
  23591. const mapperFactory = (m) => serializer.createNameMapper(m);
  23592. return new TranslationBundle(i18nNodesByMsgId, locale, digestFn, mapperFactory, missingTranslationStrategy, console);
  23593. }
  23594. // Returns the translation as HTML nodes from the given source message.
  23595. get(srcMsg) {
  23596. const html = this._i18nToHtml.convert(srcMsg);
  23597. if (html.errors.length) {
  23598. throw new Error(html.errors.join('\n'));
  23599. }
  23600. return html.nodes;
  23601. }
  23602. has(srcMsg) {
  23603. return this.digest(srcMsg) in this._i18nNodesByMsgId;
  23604. }
  23605. }
  23606. class I18nToHtmlVisitor {
  23607. constructor(_i18nNodesByMsgId = {}, _locale, _digest, _mapperFactory, _missingTranslationStrategy, _console) {
  23608. this._i18nNodesByMsgId = _i18nNodesByMsgId;
  23609. this._locale = _locale;
  23610. this._digest = _digest;
  23611. this._mapperFactory = _mapperFactory;
  23612. this._missingTranslationStrategy = _missingTranslationStrategy;
  23613. this._console = _console;
  23614. this._errors = [];
  23615. this._contextStack = [];
  23616. }
  23617. convert(srcMsg) {
  23618. this._contextStack.length = 0;
  23619. this._errors.length = 0;
  23620. // i18n to text
  23621. const text = this._convertToText(srcMsg);
  23622. // text to html
  23623. const url = srcMsg.nodes[0].sourceSpan.start.file.url;
  23624. const html = new HtmlParser().parse(text, url, { tokenizeExpansionForms: true });
  23625. return {
  23626. nodes: html.rootNodes,
  23627. errors: [...this._errors, ...html.errors],
  23628. };
  23629. }
  23630. visitText(text, context) {
  23631. // `convert()` uses an `HtmlParser` to return `html.Node`s
  23632. // we should then make sure that any special characters are escaped
  23633. return escapeXml(text.value);
  23634. }
  23635. visitContainer(container, context) {
  23636. return container.children.map(n => n.visit(this)).join('');
  23637. }
  23638. visitIcu(icu, context) {
  23639. const cases = Object.keys(icu.cases).map(k => `${k} {${icu.cases[k].visit(this)}}`);
  23640. // TODO(vicb): Once all format switch to using expression placeholders
  23641. // we should throw when the placeholder is not in the source message
  23642. const exp = this._srcMsg.placeholders.hasOwnProperty(icu.expression) ?
  23643. this._srcMsg.placeholders[icu.expression].text :
  23644. icu.expression;
  23645. return `{${exp}, ${icu.type}, ${cases.join(' ')}}`;
  23646. }
  23647. visitPlaceholder(ph, context) {
  23648. const phName = this._mapper(ph.name);
  23649. if (this._srcMsg.placeholders.hasOwnProperty(phName)) {
  23650. return this._srcMsg.placeholders[phName].text;
  23651. }
  23652. if (this._srcMsg.placeholderToMessage.hasOwnProperty(phName)) {
  23653. return this._convertToText(this._srcMsg.placeholderToMessage[phName]);
  23654. }
  23655. this._addError(ph, `Unknown placeholder "${ph.name}"`);
  23656. return '';
  23657. }
  23658. // Loaded message contains only placeholders (vs tag and icu placeholders).
  23659. // However when a translation can not be found, we need to serialize the source message
  23660. // which can contain tag placeholders
  23661. visitTagPlaceholder(ph, context) {
  23662. const tag = `${ph.tag}`;
  23663. const attrs = Object.keys(ph.attrs).map(name => `${name}="${ph.attrs[name]}"`).join(' ');
  23664. if (ph.isVoid) {
  23665. return `<${tag} ${attrs}/>`;
  23666. }
  23667. const children = ph.children.map((c) => c.visit(this)).join('');
  23668. return `<${tag} ${attrs}>${children}</${tag}>`;
  23669. }
  23670. // Loaded message contains only placeholders (vs tag and icu placeholders).
  23671. // However when a translation can not be found, we need to serialize the source message
  23672. // which can contain tag placeholders
  23673. visitIcuPlaceholder(ph, context) {
  23674. // An ICU placeholder references the source message to be serialized
  23675. return this._convertToText(this._srcMsg.placeholderToMessage[ph.name]);
  23676. }
  23677. /**
  23678. * Convert a source message to a translated text string:
  23679. * - text nodes are replaced with their translation,
  23680. * - placeholders are replaced with their content,
  23681. * - ICU nodes are converted to ICU expressions.
  23682. */
  23683. _convertToText(srcMsg) {
  23684. const id = this._digest(srcMsg);
  23685. const mapper = this._mapperFactory ? this._mapperFactory(srcMsg) : null;
  23686. let nodes;
  23687. this._contextStack.push({ msg: this._srcMsg, mapper: this._mapper });
  23688. this._srcMsg = srcMsg;
  23689. if (this._i18nNodesByMsgId.hasOwnProperty(id)) {
  23690. // When there is a translation use its nodes as the source
  23691. // And create a mapper to convert serialized placeholder names to internal names
  23692. nodes = this._i18nNodesByMsgId[id];
  23693. this._mapper = (name) => mapper ? mapper.toInternalName(name) : name;
  23694. }
  23695. else {
  23696. // When no translation has been found
  23697. // - report an error / a warning / nothing,
  23698. // - use the nodes from the original message
  23699. // - placeholders are already internal and need no mapper
  23700. if (this._missingTranslationStrategy === MissingTranslationStrategy.Error) {
  23701. const ctx = this._locale ? ` for locale "${this._locale}"` : '';
  23702. this._addError(srcMsg.nodes[0], `Missing translation for message "${id}"${ctx}`);
  23703. }
  23704. else if (this._console &&
  23705. this._missingTranslationStrategy === MissingTranslationStrategy.Warning) {
  23706. const ctx = this._locale ? ` for locale "${this._locale}"` : '';
  23707. this._console.warn(`Missing translation for message "${id}"${ctx}`);
  23708. }
  23709. nodes = srcMsg.nodes;
  23710. this._mapper = (name) => name;
  23711. }
  23712. const text = nodes.map(node => node.visit(this)).join('');
  23713. const context = this._contextStack.pop();
  23714. this._srcMsg = context.msg;
  23715. this._mapper = context.mapper;
  23716. return text;
  23717. }
  23718. _addError(el, msg) {
  23719. this._errors.push(new I18nError(el.sourceSpan, msg));
  23720. }
  23721. }
  23722. class I18NHtmlParser {
  23723. constructor(_htmlParser, translations, translationsFormat, missingTranslation = MissingTranslationStrategy.Warning, console) {
  23724. this._htmlParser = _htmlParser;
  23725. if (translations) {
  23726. const serializer = createSerializer(translationsFormat);
  23727. this._translationBundle =
  23728. TranslationBundle.load(translations, 'i18n', serializer, missingTranslation, console);
  23729. }
  23730. else {
  23731. this._translationBundle =
  23732. new TranslationBundle({}, null, digest$1, undefined, missingTranslation, console);
  23733. }
  23734. }
  23735. parse(source, url, options = {}) {
  23736. const interpolationConfig = options.interpolationConfig || DEFAULT_INTERPOLATION_CONFIG;
  23737. const parseResult = this._htmlParser.parse(source, url, { interpolationConfig, ...options });
  23738. if (parseResult.errors.length) {
  23739. return new ParseTreeResult(parseResult.rootNodes, parseResult.errors);
  23740. }
  23741. return mergeTranslations(parseResult.rootNodes, this._translationBundle, interpolationConfig, [], {});
  23742. }
  23743. }
  23744. function createSerializer(format) {
  23745. format = (format || 'xlf').toLowerCase();
  23746. switch (format) {
  23747. case 'xmb':
  23748. return new Xmb();
  23749. case 'xtb':
  23750. return new Xtb();
  23751. case 'xliff2':
  23752. case 'xlf2':
  23753. return new Xliff2();
  23754. case 'xliff':
  23755. case 'xlf':
  23756. default:
  23757. return new Xliff();
  23758. }
  23759. }
  23760. /**
  23761. * A container for message extracted from the templates.
  23762. */
  23763. class MessageBundle {
  23764. constructor(_htmlParser, _implicitTags, _implicitAttrs, _locale = null) {
  23765. this._htmlParser = _htmlParser;
  23766. this._implicitTags = _implicitTags;
  23767. this._implicitAttrs = _implicitAttrs;
  23768. this._locale = _locale;
  23769. this._messages = [];
  23770. }
  23771. updateFromTemplate(html, url, interpolationConfig) {
  23772. const htmlParserResult = this._htmlParser.parse(html, url, { tokenizeExpansionForms: true, interpolationConfig });
  23773. if (htmlParserResult.errors.length) {
  23774. return htmlParserResult.errors;
  23775. }
  23776. const i18nParserResult = extractMessages(htmlParserResult.rootNodes, interpolationConfig, this._implicitTags, this._implicitAttrs);
  23777. if (i18nParserResult.errors.length) {
  23778. return i18nParserResult.errors;
  23779. }
  23780. this._messages.push(...i18nParserResult.messages);
  23781. return [];
  23782. }
  23783. // Return the message in the internal format
  23784. // The public (serialized) format might be different, see the `write` method.
  23785. getMessages() {
  23786. return this._messages;
  23787. }
  23788. write(serializer, filterSources) {
  23789. const messages = {};
  23790. const mapperVisitor = new MapPlaceholderNames();
  23791. // Deduplicate messages based on their ID
  23792. this._messages.forEach(message => {
  23793. const id = serializer.digest(message);
  23794. if (!messages.hasOwnProperty(id)) {
  23795. messages[id] = message;
  23796. }
  23797. else {
  23798. messages[id].sources.push(...message.sources);
  23799. }
  23800. });
  23801. // Transform placeholder names using the serializer mapping
  23802. const msgList = Object.keys(messages).map(id => {
  23803. const mapper = serializer.createNameMapper(messages[id]);
  23804. const src = messages[id];
  23805. const nodes = mapper ? mapperVisitor.convert(src.nodes, mapper) : src.nodes;
  23806. let transformedMessage = new Message(nodes, {}, {}, src.meaning, src.description, id);
  23807. transformedMessage.sources = src.sources;
  23808. if (filterSources) {
  23809. transformedMessage.sources.forEach((source) => source.filePath = filterSources(source.filePath));
  23810. }
  23811. return transformedMessage;
  23812. });
  23813. return serializer.write(msgList, this._locale);
  23814. }
  23815. }
  23816. // Transform an i18n AST by renaming the placeholder nodes with the given mapper
  23817. class MapPlaceholderNames extends CloneVisitor {
  23818. convert(nodes, mapper) {
  23819. return mapper ? nodes.map(n => n.visit(this, mapper)) : nodes;
  23820. }
  23821. visitTagPlaceholder(ph, mapper) {
  23822. const startName = mapper.toPublicName(ph.startName);
  23823. const closeName = ph.closeName ? mapper.toPublicName(ph.closeName) : ph.closeName;
  23824. const children = ph.children.map(n => n.visit(this, mapper));
  23825. return new TagPlaceholder(ph.tag, ph.attrs, startName, closeName, children, ph.isVoid, ph.sourceSpan, ph.startSourceSpan, ph.endSourceSpan);
  23826. }
  23827. visitPlaceholder(ph, mapper) {
  23828. return new Placeholder(ph.value, mapper.toPublicName(ph.name), ph.sourceSpan);
  23829. }
  23830. visitIcuPlaceholder(ph, mapper) {
  23831. return new IcuPlaceholder(ph.value, mapper.toPublicName(ph.name), ph.sourceSpan);
  23832. }
  23833. }
  23834. var FactoryTarget;
  23835. (function (FactoryTarget) {
  23836. FactoryTarget[FactoryTarget["Directive"] = 0] = "Directive";
  23837. FactoryTarget[FactoryTarget["Component"] = 1] = "Component";
  23838. FactoryTarget[FactoryTarget["Injectable"] = 2] = "Injectable";
  23839. FactoryTarget[FactoryTarget["Pipe"] = 3] = "Pipe";
  23840. FactoryTarget[FactoryTarget["NgModule"] = 4] = "NgModule";
  23841. })(FactoryTarget || (FactoryTarget = {}));
  23842. /**
  23843. * Processes `Target`s with a given set of directives and performs a binding operation, which
  23844. * returns an object similar to TypeScript's `ts.TypeChecker` that contains knowledge about the
  23845. * target.
  23846. */
  23847. class R3TargetBinder {
  23848. constructor(directiveMatcher) {
  23849. this.directiveMatcher = directiveMatcher;
  23850. }
  23851. /**
  23852. * Perform a binding operation on the given `Target` and return a `BoundTarget` which contains
  23853. * metadata about the types referenced in the template.
  23854. */
  23855. bind(target) {
  23856. if (!target.template) {
  23857. // TODO(alxhub): handle targets which contain things like HostBindings, etc.
  23858. throw new Error('Binding without a template not yet supported');
  23859. }
  23860. // First, parse the template into a `Scope` structure. This operation captures the syntactic
  23861. // scopes in the template and makes them available for later use.
  23862. const scope = Scope.apply(target.template);
  23863. // Use the `Scope` to extract the entities present at every level of the template.
  23864. const templateEntities = extractTemplateEntities(scope);
  23865. // Next, perform directive matching on the template using the `DirectiveBinder`. This returns:
  23866. // - directives: Map of nodes (elements & ng-templates) to the directives on them.
  23867. // - bindings: Map of inputs, outputs, and attributes to the directive/element that claims
  23868. // them. TODO(alxhub): handle multiple directives claiming an input/output/etc.
  23869. // - references: Map of #references to their targets.
  23870. const { directives, bindings, references } = DirectiveBinder.apply(target.template, this.directiveMatcher);
  23871. // Finally, run the TemplateBinder to bind references, variables, and other entities within the
  23872. // template. This extracts all the metadata that doesn't depend on directive matching.
  23873. const { expressions, symbols, nestingLevel, usedPipes } = TemplateBinder.applyWithScope(target.template, scope);
  23874. return new R3BoundTarget(target, directives, bindings, references, expressions, symbols, nestingLevel, templateEntities, usedPipes);
  23875. }
  23876. }
  23877. /**
  23878. * Represents a binding scope within a template.
  23879. *
  23880. * Any variables, references, or other named entities declared within the template will
  23881. * be captured and available by name in `namedEntities`. Additionally, child templates will
  23882. * be analyzed and have their child `Scope`s available in `childScopes`.
  23883. */
  23884. class Scope {
  23885. constructor(parentScope, template) {
  23886. this.parentScope = parentScope;
  23887. this.template = template;
  23888. /**
  23889. * Named members of the `Scope`, such as `Reference`s or `Variable`s.
  23890. */
  23891. this.namedEntities = new Map();
  23892. /**
  23893. * Child `Scope`s for immediately nested `Template`s.
  23894. */
  23895. this.childScopes = new Map();
  23896. }
  23897. static newRootScope() {
  23898. return new Scope(null, null);
  23899. }
  23900. /**
  23901. * Process a template (either as a `Template` sub-template with variables, or a plain array of
  23902. * template `Node`s) and construct its `Scope`.
  23903. */
  23904. static apply(template) {
  23905. const scope = Scope.newRootScope();
  23906. scope.ingest(template);
  23907. return scope;
  23908. }
  23909. /**
  23910. * Internal method to process the template and populate the `Scope`.
  23911. */
  23912. ingest(template) {
  23913. if (template instanceof Template) {
  23914. // Variables on an <ng-template> are defined in the inner scope.
  23915. template.variables.forEach(node => this.visitVariable(node));
  23916. // Process the nodes of the template.
  23917. template.children.forEach(node => node.visit(this));
  23918. }
  23919. else {
  23920. // No overarching `Template` instance, so process the nodes directly.
  23921. template.forEach(node => node.visit(this));
  23922. }
  23923. }
  23924. visitElement(element) {
  23925. // `Element`s in the template may have `Reference`s which are captured in the scope.
  23926. element.references.forEach(node => this.visitReference(node));
  23927. // Recurse into the `Element`'s children.
  23928. element.children.forEach(node => node.visit(this));
  23929. }
  23930. visitTemplate(template) {
  23931. // References on a <ng-template> are defined in the outer scope, so capture them before
  23932. // processing the template's child scope.
  23933. template.references.forEach(node => this.visitReference(node));
  23934. // Next, create an inner scope and process the template within it.
  23935. const scope = new Scope(this, template);
  23936. scope.ingest(template);
  23937. this.childScopes.set(template, scope);
  23938. }
  23939. visitVariable(variable) {
  23940. // Declare the variable if it's not already.
  23941. this.maybeDeclare(variable);
  23942. }
  23943. visitReference(reference) {
  23944. // Declare the variable if it's not already.
  23945. this.maybeDeclare(reference);
  23946. }
  23947. // Unused visitors.
  23948. visitContent(content) { }
  23949. visitBoundAttribute(attr) { }
  23950. visitBoundEvent(event) { }
  23951. visitBoundText(text) { }
  23952. visitText(text) { }
  23953. visitTextAttribute(attr) { }
  23954. visitIcu(icu) { }
  23955. maybeDeclare(thing) {
  23956. // Declare something with a name, as long as that name isn't taken.
  23957. if (!this.namedEntities.has(thing.name)) {
  23958. this.namedEntities.set(thing.name, thing);
  23959. }
  23960. }
  23961. /**
  23962. * Look up a variable within this `Scope`.
  23963. *
  23964. * This can recurse into a parent `Scope` if it's available.
  23965. */
  23966. lookup(name) {
  23967. if (this.namedEntities.has(name)) {
  23968. // Found in the local scope.
  23969. return this.namedEntities.get(name);
  23970. }
  23971. else if (this.parentScope !== null) {
  23972. // Not in the local scope, but there's a parent scope so check there.
  23973. return this.parentScope.lookup(name);
  23974. }
  23975. else {
  23976. // At the top level and it wasn't found.
  23977. return null;
  23978. }
  23979. }
  23980. /**
  23981. * Get the child scope for a `Template`.
  23982. *
  23983. * This should always be defined.
  23984. */
  23985. getChildScope(template) {
  23986. const res = this.childScopes.get(template);
  23987. if (res === undefined) {
  23988. throw new Error(`Assertion error: child scope for ${template} not found`);
  23989. }
  23990. return res;
  23991. }
  23992. }
  23993. /**
  23994. * Processes a template and matches directives on nodes (elements and templates).
  23995. *
  23996. * Usually used via the static `apply()` method.
  23997. */
  23998. class DirectiveBinder {
  23999. constructor(matcher, directives, bindings, references) {
  24000. this.matcher = matcher;
  24001. this.directives = directives;
  24002. this.bindings = bindings;
  24003. this.references = references;
  24004. }
  24005. /**
  24006. * Process a template (list of `Node`s) and perform directive matching against each node.
  24007. *
  24008. * @param template the list of template `Node`s to match (recursively).
  24009. * @param selectorMatcher a `SelectorMatcher` containing the directives that are in scope for
  24010. * this template.
  24011. * @returns three maps which contain information about directives in the template: the
  24012. * `directives` map which lists directives matched on each node, the `bindings` map which
  24013. * indicates which directives claimed which bindings (inputs, outputs, etc), and the `references`
  24014. * map which resolves #references (`Reference`s) within the template to the named directive or
  24015. * template node.
  24016. */
  24017. static apply(template, selectorMatcher) {
  24018. const directives = new Map();
  24019. const bindings = new Map();
  24020. const references = new Map();
  24021. const matcher = new DirectiveBinder(selectorMatcher, directives, bindings, references);
  24022. matcher.ingest(template);
  24023. return { directives, bindings, references };
  24024. }
  24025. ingest(template) {
  24026. template.forEach(node => node.visit(this));
  24027. }
  24028. visitElement(element) {
  24029. this.visitElementOrTemplate(element.name, element);
  24030. }
  24031. visitTemplate(template) {
  24032. this.visitElementOrTemplate('ng-template', template);
  24033. }
  24034. visitElementOrTemplate(elementName, node) {
  24035. // First, determine the HTML shape of the node for the purpose of directive matching.
  24036. // Do this by building up a `CssSelector` for the node.
  24037. const cssSelector = createCssSelector(elementName, getAttrsForDirectiveMatching(node));
  24038. // Next, use the `SelectorMatcher` to get the list of directives on the node.
  24039. const directives = [];
  24040. this.matcher.match(cssSelector, (_selector, results) => directives.push(...results));
  24041. if (directives.length > 0) {
  24042. this.directives.set(node, directives);
  24043. }
  24044. // Resolve any references that are created on this node.
  24045. node.references.forEach(ref => {
  24046. let dirTarget = null;
  24047. // If the reference expression is empty, then it matches the "primary" directive on the node
  24048. // (if there is one). Otherwise it matches the host node itself (either an element or
  24049. // <ng-template> node).
  24050. if (ref.value.trim() === '') {
  24051. // This could be a reference to a component if there is one.
  24052. dirTarget = directives.find(dir => dir.isComponent) || null;
  24053. }
  24054. else {
  24055. // This should be a reference to a directive exported via exportAs.
  24056. dirTarget =
  24057. directives.find(dir => dir.exportAs !== null && dir.exportAs.some(value => value === ref.value)) ||
  24058. null;
  24059. // Check if a matching directive was found.
  24060. if (dirTarget === null) {
  24061. // No matching directive was found - this reference points to an unknown target. Leave it
  24062. // unmapped.
  24063. return;
  24064. }
  24065. }
  24066. if (dirTarget !== null) {
  24067. // This reference points to a directive.
  24068. this.references.set(ref, { directive: dirTarget, node });
  24069. }
  24070. else {
  24071. // This reference points to the node itself.
  24072. this.references.set(ref, node);
  24073. }
  24074. });
  24075. const setAttributeBinding = (attribute, ioType) => {
  24076. const dir = directives.find(dir => dir[ioType].hasBindingPropertyName(attribute.name));
  24077. const binding = dir !== undefined ? dir : node;
  24078. this.bindings.set(attribute, binding);
  24079. };
  24080. // Node inputs (bound attributes) and text attributes can be bound to an
  24081. // input on a directive.
  24082. node.inputs.forEach(input => setAttributeBinding(input, 'inputs'));
  24083. node.attributes.forEach(attr => setAttributeBinding(attr, 'inputs'));
  24084. if (node instanceof Template) {
  24085. node.templateAttrs.forEach(attr => setAttributeBinding(attr, 'inputs'));
  24086. }
  24087. // Node outputs (bound events) can be bound to an output on a directive.
  24088. node.outputs.forEach(output => setAttributeBinding(output, 'outputs'));
  24089. // Recurse into the node's children.
  24090. node.children.forEach(child => child.visit(this));
  24091. }
  24092. // Unused visitors.
  24093. visitContent(content) { }
  24094. visitVariable(variable) { }
  24095. visitReference(reference) { }
  24096. visitTextAttribute(attribute) { }
  24097. visitBoundAttribute(attribute) { }
  24098. visitBoundEvent(attribute) { }
  24099. visitBoundAttributeOrEvent(node) { }
  24100. visitText(text) { }
  24101. visitBoundText(text) { }
  24102. visitIcu(icu) { }
  24103. }
  24104. /**
  24105. * Processes a template and extract metadata about expressions and symbols within.
  24106. *
  24107. * This is a companion to the `DirectiveBinder` that doesn't require knowledge of directives matched
  24108. * within the template in order to operate.
  24109. *
  24110. * Expressions are visited by the superclass `RecursiveAstVisitor`, with custom logic provided
  24111. * by overridden methods from that visitor.
  24112. */
  24113. class TemplateBinder extends RecursiveAstVisitor {
  24114. constructor(bindings, symbols, usedPipes, nestingLevel, scope, template, level) {
  24115. super();
  24116. this.bindings = bindings;
  24117. this.symbols = symbols;
  24118. this.usedPipes = usedPipes;
  24119. this.nestingLevel = nestingLevel;
  24120. this.scope = scope;
  24121. this.template = template;
  24122. this.level = level;
  24123. // Save a bit of processing time by constructing this closure in advance.
  24124. this.visitNode = (node) => node.visit(this);
  24125. }
  24126. // This method is defined to reconcile the type of TemplateBinder since both
  24127. // RecursiveAstVisitor and Visitor define the visit() method in their
  24128. // interfaces.
  24129. visit(node, context) {
  24130. if (node instanceof AST) {
  24131. node.visit(this, context);
  24132. }
  24133. else {
  24134. node.visit(this);
  24135. }
  24136. }
  24137. /**
  24138. * Process a template and extract metadata about expressions and symbols within.
  24139. *
  24140. * @param template the nodes of the template to process
  24141. * @param scope the `Scope` of the template being processed.
  24142. * @returns three maps which contain metadata about the template: `expressions` which interprets
  24143. * special `AST` nodes in expressions as pointing to references or variables declared within the
  24144. * template, `symbols` which maps those variables and references to the nested `Template` which
  24145. * declares them, if any, and `nestingLevel` which associates each `Template` with a integer
  24146. * nesting level (how many levels deep within the template structure the `Template` is), starting
  24147. * at 1.
  24148. */
  24149. static applyWithScope(template, scope) {
  24150. const expressions = new Map();
  24151. const symbols = new Map();
  24152. const nestingLevel = new Map();
  24153. const usedPipes = new Set();
  24154. // The top-level template has nesting level 0.
  24155. const binder = new TemplateBinder(expressions, symbols, usedPipes, nestingLevel, scope, template instanceof Template ? template : null, 0);
  24156. binder.ingest(template);
  24157. return { expressions, symbols, nestingLevel, usedPipes };
  24158. }
  24159. ingest(template) {
  24160. if (template instanceof Template) {
  24161. // For <ng-template>s, process only variables and child nodes. Inputs, outputs, templateAttrs,
  24162. // and references were all processed in the scope of the containing template.
  24163. template.variables.forEach(this.visitNode);
  24164. template.children.forEach(this.visitNode);
  24165. // Set the nesting level.
  24166. this.nestingLevel.set(template, this.level);
  24167. }
  24168. else {
  24169. // Visit each node from the top-level template.
  24170. template.forEach(this.visitNode);
  24171. }
  24172. }
  24173. visitElement(element) {
  24174. // Visit the inputs, outputs, and children of the element.
  24175. element.inputs.forEach(this.visitNode);
  24176. element.outputs.forEach(this.visitNode);
  24177. element.children.forEach(this.visitNode);
  24178. }
  24179. visitTemplate(template) {
  24180. // First, visit inputs, outputs and template attributes of the template node.
  24181. template.inputs.forEach(this.visitNode);
  24182. template.outputs.forEach(this.visitNode);
  24183. template.templateAttrs.forEach(this.visitNode);
  24184. // References are also evaluated in the outer context.
  24185. template.references.forEach(this.visitNode);
  24186. // Next, recurse into the template using its scope, and bumping the nesting level up by one.
  24187. const childScope = this.scope.getChildScope(template);
  24188. const binder = new TemplateBinder(this.bindings, this.symbols, this.usedPipes, this.nestingLevel, childScope, template, this.level + 1);
  24189. binder.ingest(template);
  24190. }
  24191. visitVariable(variable) {
  24192. // Register the `Variable` as a symbol in the current `Template`.
  24193. if (this.template !== null) {
  24194. this.symbols.set(variable, this.template);
  24195. }
  24196. }
  24197. visitReference(reference) {
  24198. // Register the `Reference` as a symbol in the current `Template`.
  24199. if (this.template !== null) {
  24200. this.symbols.set(reference, this.template);
  24201. }
  24202. }
  24203. // Unused template visitors
  24204. visitText(text) { }
  24205. visitContent(content) { }
  24206. visitTextAttribute(attribute) { }
  24207. visitIcu(icu) {
  24208. Object.keys(icu.vars).forEach(key => icu.vars[key].visit(this));
  24209. Object.keys(icu.placeholders).forEach(key => icu.placeholders[key].visit(this));
  24210. }
  24211. // The remaining visitors are concerned with processing AST expressions within template bindings
  24212. visitBoundAttribute(attribute) {
  24213. attribute.value.visit(this);
  24214. }
  24215. visitBoundEvent(event) {
  24216. event.handler.visit(this);
  24217. }
  24218. visitBoundText(text) {
  24219. text.value.visit(this);
  24220. }
  24221. visitPipe(ast, context) {
  24222. this.usedPipes.add(ast.name);
  24223. return super.visitPipe(ast, context);
  24224. }
  24225. // These five types of AST expressions can refer to expression roots, which could be variables
  24226. // or references in the current scope.
  24227. visitPropertyRead(ast, context) {
  24228. this.maybeMap(context, ast, ast.name);
  24229. return super.visitPropertyRead(ast, context);
  24230. }
  24231. visitSafePropertyRead(ast, context) {
  24232. this.maybeMap(context, ast, ast.name);
  24233. return super.visitSafePropertyRead(ast, context);
  24234. }
  24235. visitPropertyWrite(ast, context) {
  24236. this.maybeMap(context, ast, ast.name);
  24237. return super.visitPropertyWrite(ast, context);
  24238. }
  24239. maybeMap(scope, ast, name) {
  24240. // If the receiver of the expression isn't the `ImplicitReceiver`, this isn't the root of an
  24241. // `AST` expression that maps to a `Variable` or `Reference`.
  24242. if (!(ast.receiver instanceof ImplicitReceiver)) {
  24243. return;
  24244. }
  24245. // Check whether the name exists in the current scope. If so, map it. Otherwise, the name is
  24246. // probably a property on the top-level component context.
  24247. let target = this.scope.lookup(name);
  24248. if (target !== null) {
  24249. this.bindings.set(ast, target);
  24250. }
  24251. }
  24252. }
  24253. /**
  24254. * Metadata container for a `Target` that allows queries for specific bits of metadata.
  24255. *
  24256. * See `BoundTarget` for documentation on the individual methods.
  24257. */
  24258. class R3BoundTarget {
  24259. constructor(target, directives, bindings, references, exprTargets, symbols, nestingLevel, templateEntities, usedPipes) {
  24260. this.target = target;
  24261. this.directives = directives;
  24262. this.bindings = bindings;
  24263. this.references = references;
  24264. this.exprTargets = exprTargets;
  24265. this.symbols = symbols;
  24266. this.nestingLevel = nestingLevel;
  24267. this.templateEntities = templateEntities;
  24268. this.usedPipes = usedPipes;
  24269. }
  24270. getEntitiesInTemplateScope(template) {
  24271. return this.templateEntities.get(template) ?? new Set();
  24272. }
  24273. getDirectivesOfNode(node) {
  24274. return this.directives.get(node) || null;
  24275. }
  24276. getReferenceTarget(ref) {
  24277. return this.references.get(ref) || null;
  24278. }
  24279. getConsumerOfBinding(binding) {
  24280. return this.bindings.get(binding) || null;
  24281. }
  24282. getExpressionTarget(expr) {
  24283. return this.exprTargets.get(expr) || null;
  24284. }
  24285. getTemplateOfSymbol(symbol) {
  24286. return this.symbols.get(symbol) || null;
  24287. }
  24288. getNestingLevel(template) {
  24289. return this.nestingLevel.get(template) || 0;
  24290. }
  24291. getUsedDirectives() {
  24292. const set = new Set();
  24293. this.directives.forEach(dirs => dirs.forEach(dir => set.add(dir)));
  24294. return Array.from(set.values());
  24295. }
  24296. getUsedPipes() {
  24297. return Array.from(this.usedPipes);
  24298. }
  24299. }
  24300. function extractTemplateEntities(rootScope) {
  24301. const entityMap = new Map();
  24302. function extractScopeEntities(scope) {
  24303. if (entityMap.has(scope.template)) {
  24304. return entityMap.get(scope.template);
  24305. }
  24306. const currentEntities = scope.namedEntities;
  24307. let templateEntities;
  24308. if (scope.parentScope !== null) {
  24309. templateEntities = new Map([...extractScopeEntities(scope.parentScope), ...currentEntities]);
  24310. }
  24311. else {
  24312. templateEntities = new Map(currentEntities);
  24313. }
  24314. entityMap.set(scope.template, templateEntities);
  24315. return templateEntities;
  24316. }
  24317. const scopesToProcess = [rootScope];
  24318. while (scopesToProcess.length > 0) {
  24319. const scope = scopesToProcess.pop();
  24320. for (const childScope of scope.childScopes.values()) {
  24321. scopesToProcess.push(childScope);
  24322. }
  24323. extractScopeEntities(scope);
  24324. }
  24325. const templateEntities = new Map();
  24326. for (const [template, entities] of entityMap) {
  24327. templateEntities.set(template, new Set(entities.values()));
  24328. }
  24329. return templateEntities;
  24330. }
  24331. function compileClassMetadata(metadata) {
  24332. // Generate an ngDevMode guarded call to setClassMetadata with the class identifier and its
  24333. // metadata.
  24334. const fnCall = importExpr(Identifiers.setClassMetadata).callFn([
  24335. metadata.type,
  24336. metadata.decorators,
  24337. metadata.ctorParameters ?? literal(null),
  24338. metadata.propDecorators ?? literal(null),
  24339. ]);
  24340. const iife = fn([], [devOnlyGuardedExpression(fnCall).toStmt()]);
  24341. return iife.callFn([]);
  24342. }
  24343. /**
  24344. * Every time we make a breaking change to the declaration interface or partial-linker behavior, we
  24345. * must update this constant to prevent old partial-linkers from incorrectly processing the
  24346. * declaration.
  24347. *
  24348. * Do not include any prerelease in these versions as they are ignored.
  24349. */
  24350. const MINIMUM_PARTIAL_LINKER_VERSION$6 = '12.0.0';
  24351. function compileDeclareClassMetadata(metadata) {
  24352. const definitionMap = new DefinitionMap();
  24353. definitionMap.set('minVersion', literal(MINIMUM_PARTIAL_LINKER_VERSION$6));
  24354. definitionMap.set('version', literal('16.0.4'));
  24355. definitionMap.set('ngImport', importExpr(Identifiers.core));
  24356. definitionMap.set('type', metadata.type);
  24357. definitionMap.set('decorators', metadata.decorators);
  24358. definitionMap.set('ctorParameters', metadata.ctorParameters);
  24359. definitionMap.set('propDecorators', metadata.propDecorators);
  24360. return importExpr(Identifiers.declareClassMetadata).callFn([definitionMap.toLiteralMap()]);
  24361. }
  24362. /**
  24363. * Creates an array literal expression from the given array, mapping all values to an expression
  24364. * using the provided mapping function. If the array is empty or null, then null is returned.
  24365. *
  24366. * @param values The array to transfer into literal array expression.
  24367. * @param mapper The logic to use for creating an expression for the array's values.
  24368. * @returns An array literal expression representing `values`, or null if `values` is empty or
  24369. * is itself null.
  24370. */
  24371. function toOptionalLiteralArray(values, mapper) {
  24372. if (values === null || values.length === 0) {
  24373. return null;
  24374. }
  24375. return literalArr(values.map(value => mapper(value)));
  24376. }
  24377. /**
  24378. * Creates an object literal expression from the given object, mapping all values to an expression
  24379. * using the provided mapping function. If the object has no keys, then null is returned.
  24380. *
  24381. * @param object The object to transfer into an object literal expression.
  24382. * @param mapper The logic to use for creating an expression for the object's values.
  24383. * @returns An object literal expression representing `object`, or null if `object` does not have
  24384. * any keys.
  24385. */
  24386. function toOptionalLiteralMap(object, mapper) {
  24387. const entries = Object.keys(object).map(key => {
  24388. const value = object[key];
  24389. return { key, value: mapper(value), quoted: true };
  24390. });
  24391. if (entries.length > 0) {
  24392. return literalMap(entries);
  24393. }
  24394. else {
  24395. return null;
  24396. }
  24397. }
  24398. function compileDependencies(deps) {
  24399. if (deps === 'invalid') {
  24400. // The `deps` can be set to the string "invalid" by the `unwrapConstructorDependencies()`
  24401. // function, which tries to convert `ConstructorDeps` into `R3DependencyMetadata[]`.
  24402. return literal('invalid');
  24403. }
  24404. else if (deps === null) {
  24405. return literal(null);
  24406. }
  24407. else {
  24408. return literalArr(deps.map(compileDependency));
  24409. }
  24410. }
  24411. function compileDependency(dep) {
  24412. const depMeta = new DefinitionMap();
  24413. depMeta.set('token', dep.token);
  24414. if (dep.attributeNameType !== null) {
  24415. depMeta.set('attribute', literal(true));
  24416. }
  24417. if (dep.host) {
  24418. depMeta.set('host', literal(true));
  24419. }
  24420. if (dep.optional) {
  24421. depMeta.set('optional', literal(true));
  24422. }
  24423. if (dep.self) {
  24424. depMeta.set('self', literal(true));
  24425. }
  24426. if (dep.skipSelf) {
  24427. depMeta.set('skipSelf', literal(true));
  24428. }
  24429. return depMeta.toLiteralMap();
  24430. }
  24431. /**
  24432. * Every time we make a breaking change to the declaration interface or partial-linker behavior, we
  24433. * must update this constant to prevent old partial-linkers from incorrectly processing the
  24434. * declaration.
  24435. *
  24436. * Do not include any prerelease in these versions as they are ignored.
  24437. */
  24438. const MINIMUM_PARTIAL_LINKER_VERSION$5 = '14.0.0';
  24439. /**
  24440. * Compile a directive declaration defined by the `R3DirectiveMetadata`.
  24441. */
  24442. function compileDeclareDirectiveFromMetadata(meta) {
  24443. const definitionMap = createDirectiveDefinitionMap(meta);
  24444. const expression = importExpr(Identifiers.declareDirective).callFn([definitionMap.toLiteralMap()]);
  24445. const type = createDirectiveType(meta);
  24446. return { expression, type, statements: [] };
  24447. }
  24448. /**
  24449. * Gathers the declaration fields for a directive into a `DefinitionMap`. This allows for reusing
  24450. * this logic for components, as they extend the directive metadata.
  24451. */
  24452. function createDirectiveDefinitionMap(meta) {
  24453. const definitionMap = new DefinitionMap();
  24454. definitionMap.set('minVersion', literal(MINIMUM_PARTIAL_LINKER_VERSION$5));
  24455. definitionMap.set('version', literal('16.0.4'));
  24456. // e.g. `type: MyDirective`
  24457. definitionMap.set('type', meta.type.value);
  24458. if (meta.isStandalone) {
  24459. definitionMap.set('isStandalone', literal(meta.isStandalone));
  24460. }
  24461. // e.g. `selector: 'some-dir'`
  24462. if (meta.selector !== null) {
  24463. definitionMap.set('selector', literal(meta.selector));
  24464. }
  24465. definitionMap.set('inputs', conditionallyCreateDirectiveBindingLiteral(meta.inputs, true));
  24466. definitionMap.set('outputs', conditionallyCreateDirectiveBindingLiteral(meta.outputs));
  24467. definitionMap.set('host', compileHostMetadata(meta.host));
  24468. definitionMap.set('providers', meta.providers);
  24469. if (meta.queries.length > 0) {
  24470. definitionMap.set('queries', literalArr(meta.queries.map(compileQuery)));
  24471. }
  24472. if (meta.viewQueries.length > 0) {
  24473. definitionMap.set('viewQueries', literalArr(meta.viewQueries.map(compileQuery)));
  24474. }
  24475. if (meta.exportAs !== null) {
  24476. definitionMap.set('exportAs', asLiteral(meta.exportAs));
  24477. }
  24478. if (meta.usesInheritance) {
  24479. definitionMap.set('usesInheritance', literal(true));
  24480. }
  24481. if (meta.lifecycle.usesOnChanges) {
  24482. definitionMap.set('usesOnChanges', literal(true));
  24483. }
  24484. if (meta.hostDirectives?.length) {
  24485. definitionMap.set('hostDirectives', createHostDirectives(meta.hostDirectives));
  24486. }
  24487. definitionMap.set('ngImport', importExpr(Identifiers.core));
  24488. return definitionMap;
  24489. }
  24490. /**
  24491. * Compiles the metadata of a single query into its partial declaration form as declared
  24492. * by `R3DeclareQueryMetadata`.
  24493. */
  24494. function compileQuery(query) {
  24495. const meta = new DefinitionMap();
  24496. meta.set('propertyName', literal(query.propertyName));
  24497. if (query.first) {
  24498. meta.set('first', literal(true));
  24499. }
  24500. meta.set('predicate', Array.isArray(query.predicate) ? asLiteral(query.predicate) :
  24501. convertFromMaybeForwardRefExpression(query.predicate));
  24502. if (!query.emitDistinctChangesOnly) {
  24503. // `emitDistinctChangesOnly` is special because we expect it to be `true`.
  24504. // Therefore we explicitly emit the field, and explicitly place it only when it's `false`.
  24505. meta.set('emitDistinctChangesOnly', literal(false));
  24506. }
  24507. else {
  24508. // The linker will assume that an absent `emitDistinctChangesOnly` flag is by default `true`.
  24509. }
  24510. if (query.descendants) {
  24511. meta.set('descendants', literal(true));
  24512. }
  24513. meta.set('read', query.read);
  24514. if (query.static) {
  24515. meta.set('static', literal(true));
  24516. }
  24517. return meta.toLiteralMap();
  24518. }
  24519. /**
  24520. * Compiles the host metadata into its partial declaration form as declared
  24521. * in `R3DeclareDirectiveMetadata['host']`
  24522. */
  24523. function compileHostMetadata(meta) {
  24524. const hostMetadata = new DefinitionMap();
  24525. hostMetadata.set('attributes', toOptionalLiteralMap(meta.attributes, expression => expression));
  24526. hostMetadata.set('listeners', toOptionalLiteralMap(meta.listeners, literal));
  24527. hostMetadata.set('properties', toOptionalLiteralMap(meta.properties, literal));
  24528. if (meta.specialAttributes.styleAttr) {
  24529. hostMetadata.set('styleAttribute', literal(meta.specialAttributes.styleAttr));
  24530. }
  24531. if (meta.specialAttributes.classAttr) {
  24532. hostMetadata.set('classAttribute', literal(meta.specialAttributes.classAttr));
  24533. }
  24534. if (hostMetadata.values.length > 0) {
  24535. return hostMetadata.toLiteralMap();
  24536. }
  24537. else {
  24538. return null;
  24539. }
  24540. }
  24541. function createHostDirectives(hostDirectives) {
  24542. const expressions = hostDirectives.map(current => {
  24543. const keys = [{
  24544. key: 'directive',
  24545. value: current.isForwardReference ? generateForwardRef(current.directive.type) :
  24546. current.directive.type,
  24547. quoted: false
  24548. }];
  24549. const inputsLiteral = current.inputs ? createHostDirectivesMappingArray(current.inputs) : null;
  24550. const outputsLiteral = current.outputs ? createHostDirectivesMappingArray(current.outputs) : null;
  24551. if (inputsLiteral) {
  24552. keys.push({ key: 'inputs', value: inputsLiteral, quoted: false });
  24553. }
  24554. if (outputsLiteral) {
  24555. keys.push({ key: 'outputs', value: outputsLiteral, quoted: false });
  24556. }
  24557. return literalMap(keys);
  24558. });
  24559. // If there's a forward reference, we generate a `function() { return [{directive: HostDir}] }`,
  24560. // otherwise we can save some bytes by using a plain array, e.g. `[{directive: HostDir}]`.
  24561. return literalArr(expressions);
  24562. }
  24563. /**
  24564. * Compile a component declaration defined by the `R3ComponentMetadata`.
  24565. */
  24566. function compileDeclareComponentFromMetadata(meta, template, additionalTemplateInfo) {
  24567. const definitionMap = createComponentDefinitionMap(meta, template, additionalTemplateInfo);
  24568. const expression = importExpr(Identifiers.declareComponent).callFn([definitionMap.toLiteralMap()]);
  24569. const type = createComponentType(meta);
  24570. return { expression, type, statements: [] };
  24571. }
  24572. /**
  24573. * Gathers the declaration fields for a component into a `DefinitionMap`.
  24574. */
  24575. function createComponentDefinitionMap(meta, template, templateInfo) {
  24576. const definitionMap = createDirectiveDefinitionMap(meta);
  24577. definitionMap.set('template', getTemplateExpression(template, templateInfo));
  24578. if (templateInfo.isInline) {
  24579. definitionMap.set('isInline', literal(true));
  24580. }
  24581. definitionMap.set('styles', toOptionalLiteralArray(meta.styles, literal));
  24582. definitionMap.set('dependencies', compileUsedDependenciesMetadata(meta));
  24583. definitionMap.set('viewProviders', meta.viewProviders);
  24584. definitionMap.set('animations', meta.animations);
  24585. if (meta.changeDetection !== undefined) {
  24586. definitionMap.set('changeDetection', importExpr(Identifiers.ChangeDetectionStrategy)
  24587. .prop(ChangeDetectionStrategy[meta.changeDetection]));
  24588. }
  24589. if (meta.encapsulation !== ViewEncapsulation.Emulated) {
  24590. definitionMap.set('encapsulation', importExpr(Identifiers.ViewEncapsulation).prop(ViewEncapsulation[meta.encapsulation]));
  24591. }
  24592. if (meta.interpolation !== DEFAULT_INTERPOLATION_CONFIG) {
  24593. definitionMap.set('interpolation', literalArr([literal(meta.interpolation.start), literal(meta.interpolation.end)]));
  24594. }
  24595. if (template.preserveWhitespaces === true) {
  24596. definitionMap.set('preserveWhitespaces', literal(true));
  24597. }
  24598. return definitionMap;
  24599. }
  24600. function getTemplateExpression(template, templateInfo) {
  24601. // If the template has been defined using a direct literal, we use that expression directly
  24602. // without any modifications. This is ensures proper source mapping from the partially
  24603. // compiled code to the source file declaring the template. Note that this does not capture
  24604. // template literals referenced indirectly through an identifier.
  24605. if (templateInfo.inlineTemplateLiteralExpression !== null) {
  24606. return templateInfo.inlineTemplateLiteralExpression;
  24607. }
  24608. // If the template is defined inline but not through a literal, the template has been resolved
  24609. // through static interpretation. We create a literal but cannot provide any source span. Note
  24610. // that we cannot use the expression defining the template because the linker expects the template
  24611. // to be defined as a literal in the declaration.
  24612. if (templateInfo.isInline) {
  24613. return literal(templateInfo.content, null, null);
  24614. }
  24615. // The template is external so we must synthesize an expression node with
  24616. // the appropriate source-span.
  24617. const contents = templateInfo.content;
  24618. const file = new ParseSourceFile(contents, templateInfo.sourceUrl);
  24619. const start = new ParseLocation(file, 0, 0, 0);
  24620. const end = computeEndLocation(file, contents);
  24621. const span = new ParseSourceSpan(start, end);
  24622. return literal(contents, null, span);
  24623. }
  24624. function computeEndLocation(file, contents) {
  24625. const length = contents.length;
  24626. let lineStart = 0;
  24627. let lastLineStart = 0;
  24628. let line = 0;
  24629. do {
  24630. lineStart = contents.indexOf('\n', lastLineStart);
  24631. if (lineStart !== -1) {
  24632. lastLineStart = lineStart + 1;
  24633. line++;
  24634. }
  24635. } while (lineStart !== -1);
  24636. return new ParseLocation(file, length, line, length - lastLineStart);
  24637. }
  24638. function compileUsedDependenciesMetadata(meta) {
  24639. const wrapType = meta.declarationListEmitMode !== 0 /* DeclarationListEmitMode.Direct */ ?
  24640. generateForwardRef :
  24641. (expr) => expr;
  24642. return toOptionalLiteralArray(meta.declarations, decl => {
  24643. switch (decl.kind) {
  24644. case R3TemplateDependencyKind.Directive:
  24645. const dirMeta = new DefinitionMap();
  24646. dirMeta.set('kind', literal(decl.isComponent ? 'component' : 'directive'));
  24647. dirMeta.set('type', wrapType(decl.type));
  24648. dirMeta.set('selector', literal(decl.selector));
  24649. dirMeta.set('inputs', toOptionalLiteralArray(decl.inputs, literal));
  24650. dirMeta.set('outputs', toOptionalLiteralArray(decl.outputs, literal));
  24651. dirMeta.set('exportAs', toOptionalLiteralArray(decl.exportAs, literal));
  24652. return dirMeta.toLiteralMap();
  24653. case R3TemplateDependencyKind.Pipe:
  24654. const pipeMeta = new DefinitionMap();
  24655. pipeMeta.set('kind', literal('pipe'));
  24656. pipeMeta.set('type', wrapType(decl.type));
  24657. pipeMeta.set('name', literal(decl.name));
  24658. return pipeMeta.toLiteralMap();
  24659. case R3TemplateDependencyKind.NgModule:
  24660. const ngModuleMeta = new DefinitionMap();
  24661. ngModuleMeta.set('kind', literal('ngmodule'));
  24662. ngModuleMeta.set('type', wrapType(decl.type));
  24663. return ngModuleMeta.toLiteralMap();
  24664. }
  24665. });
  24666. }
  24667. /**
  24668. * Every time we make a breaking change to the declaration interface or partial-linker behavior, we
  24669. * must update this constant to prevent old partial-linkers from incorrectly processing the
  24670. * declaration.
  24671. *
  24672. * Do not include any prerelease in these versions as they are ignored.
  24673. */
  24674. const MINIMUM_PARTIAL_LINKER_VERSION$4 = '12.0.0';
  24675. function compileDeclareFactoryFunction(meta) {
  24676. const definitionMap = new DefinitionMap();
  24677. definitionMap.set('minVersion', literal(MINIMUM_PARTIAL_LINKER_VERSION$4));
  24678. definitionMap.set('version', literal('16.0.4'));
  24679. definitionMap.set('ngImport', importExpr(Identifiers.core));
  24680. definitionMap.set('type', meta.type.value);
  24681. definitionMap.set('deps', compileDependencies(meta.deps));
  24682. definitionMap.set('target', importExpr(Identifiers.FactoryTarget).prop(FactoryTarget$1[meta.target]));
  24683. return {
  24684. expression: importExpr(Identifiers.declareFactory).callFn([definitionMap.toLiteralMap()]),
  24685. statements: [],
  24686. type: createFactoryType(meta),
  24687. };
  24688. }
  24689. /**
  24690. * Every time we make a breaking change to the declaration interface or partial-linker behavior, we
  24691. * must update this constant to prevent old partial-linkers from incorrectly processing the
  24692. * declaration.
  24693. *
  24694. * Do not include any prerelease in these versions as they are ignored.
  24695. */
  24696. const MINIMUM_PARTIAL_LINKER_VERSION$3 = '12.0.0';
  24697. /**
  24698. * Compile a Injectable declaration defined by the `R3InjectableMetadata`.
  24699. */
  24700. function compileDeclareInjectableFromMetadata(meta) {
  24701. const definitionMap = createInjectableDefinitionMap(meta);
  24702. const expression = importExpr(Identifiers.declareInjectable).callFn([definitionMap.toLiteralMap()]);
  24703. const type = createInjectableType(meta);
  24704. return { expression, type, statements: [] };
  24705. }
  24706. /**
  24707. * Gathers the declaration fields for a Injectable into a `DefinitionMap`.
  24708. */
  24709. function createInjectableDefinitionMap(meta) {
  24710. const definitionMap = new DefinitionMap();
  24711. definitionMap.set('minVersion', literal(MINIMUM_PARTIAL_LINKER_VERSION$3));
  24712. definitionMap.set('version', literal('16.0.4'));
  24713. definitionMap.set('ngImport', importExpr(Identifiers.core));
  24714. definitionMap.set('type', meta.type.value);
  24715. // Only generate providedIn property if it has a non-null value
  24716. if (meta.providedIn !== undefined) {
  24717. const providedIn = convertFromMaybeForwardRefExpression(meta.providedIn);
  24718. if (providedIn.value !== null) {
  24719. definitionMap.set('providedIn', providedIn);
  24720. }
  24721. }
  24722. if (meta.useClass !== undefined) {
  24723. definitionMap.set('useClass', convertFromMaybeForwardRefExpression(meta.useClass));
  24724. }
  24725. if (meta.useExisting !== undefined) {
  24726. definitionMap.set('useExisting', convertFromMaybeForwardRefExpression(meta.useExisting));
  24727. }
  24728. if (meta.useValue !== undefined) {
  24729. definitionMap.set('useValue', convertFromMaybeForwardRefExpression(meta.useValue));
  24730. }
  24731. // Factories do not contain `ForwardRef`s since any types are already wrapped in a function call
  24732. // so the types will not be eagerly evaluated. Therefore we do not need to process this expression
  24733. // with `convertFromProviderExpression()`.
  24734. if (meta.useFactory !== undefined) {
  24735. definitionMap.set('useFactory', meta.useFactory);
  24736. }
  24737. if (meta.deps !== undefined) {
  24738. definitionMap.set('deps', literalArr(meta.deps.map(compileDependency)));
  24739. }
  24740. return definitionMap;
  24741. }
  24742. /**
  24743. * Every time we make a breaking change to the declaration interface or partial-linker behavior, we
  24744. * must update this constant to prevent old partial-linkers from incorrectly processing the
  24745. * declaration.
  24746. *
  24747. * Do not include any prerelease in these versions as they are ignored.
  24748. */
  24749. const MINIMUM_PARTIAL_LINKER_VERSION$2 = '12.0.0';
  24750. function compileDeclareInjectorFromMetadata(meta) {
  24751. const definitionMap = createInjectorDefinitionMap(meta);
  24752. const expression = importExpr(Identifiers.declareInjector).callFn([definitionMap.toLiteralMap()]);
  24753. const type = createInjectorType(meta);
  24754. return { expression, type, statements: [] };
  24755. }
  24756. /**
  24757. * Gathers the declaration fields for an Injector into a `DefinitionMap`.
  24758. */
  24759. function createInjectorDefinitionMap(meta) {
  24760. const definitionMap = new DefinitionMap();
  24761. definitionMap.set('minVersion', literal(MINIMUM_PARTIAL_LINKER_VERSION$2));
  24762. definitionMap.set('version', literal('16.0.4'));
  24763. definitionMap.set('ngImport', importExpr(Identifiers.core));
  24764. definitionMap.set('type', meta.type.value);
  24765. definitionMap.set('providers', meta.providers);
  24766. if (meta.imports.length > 0) {
  24767. definitionMap.set('imports', literalArr(meta.imports));
  24768. }
  24769. return definitionMap;
  24770. }
  24771. /**
  24772. * Every time we make a breaking change to the declaration interface or partial-linker behavior, we
  24773. * must update this constant to prevent old partial-linkers from incorrectly processing the
  24774. * declaration.
  24775. *
  24776. * Do not include any prerelease in these versions as they are ignored.
  24777. */
  24778. const MINIMUM_PARTIAL_LINKER_VERSION$1 = '14.0.0';
  24779. function compileDeclareNgModuleFromMetadata(meta) {
  24780. const definitionMap = createNgModuleDefinitionMap(meta);
  24781. const expression = importExpr(Identifiers.declareNgModule).callFn([definitionMap.toLiteralMap()]);
  24782. const type = createNgModuleType(meta);
  24783. return { expression, type, statements: [] };
  24784. }
  24785. /**
  24786. * Gathers the declaration fields for an NgModule into a `DefinitionMap`.
  24787. */
  24788. function createNgModuleDefinitionMap(meta) {
  24789. const definitionMap = new DefinitionMap();
  24790. definitionMap.set('minVersion', literal(MINIMUM_PARTIAL_LINKER_VERSION$1));
  24791. definitionMap.set('version', literal('16.0.4'));
  24792. definitionMap.set('ngImport', importExpr(Identifiers.core));
  24793. definitionMap.set('type', meta.type.value);
  24794. // We only generate the keys in the metadata if the arrays contain values.
  24795. // We must wrap the arrays inside a function if any of the values are a forward reference to a
  24796. // not-yet-declared class. This is to support JIT execution of the `ɵɵngDeclareNgModule()` call.
  24797. // In the linker these wrappers are stripped and then reapplied for the `ɵɵdefineNgModule()` call.
  24798. if (meta.bootstrap.length > 0) {
  24799. definitionMap.set('bootstrap', refsToArray(meta.bootstrap, meta.containsForwardDecls));
  24800. }
  24801. if (meta.declarations.length > 0) {
  24802. definitionMap.set('declarations', refsToArray(meta.declarations, meta.containsForwardDecls));
  24803. }
  24804. if (meta.imports.length > 0) {
  24805. definitionMap.set('imports', refsToArray(meta.imports, meta.containsForwardDecls));
  24806. }
  24807. if (meta.exports.length > 0) {
  24808. definitionMap.set('exports', refsToArray(meta.exports, meta.containsForwardDecls));
  24809. }
  24810. if (meta.schemas !== null && meta.schemas.length > 0) {
  24811. definitionMap.set('schemas', literalArr(meta.schemas.map(ref => ref.value)));
  24812. }
  24813. if (meta.id !== null) {
  24814. definitionMap.set('id', meta.id);
  24815. }
  24816. return definitionMap;
  24817. }
  24818. /**
  24819. * Every time we make a breaking change to the declaration interface or partial-linker behavior, we
  24820. * must update this constant to prevent old partial-linkers from incorrectly processing the
  24821. * declaration.
  24822. *
  24823. * Do not include any prerelease in these versions as they are ignored.
  24824. */
  24825. const MINIMUM_PARTIAL_LINKER_VERSION = '14.0.0';
  24826. /**
  24827. * Compile a Pipe declaration defined by the `R3PipeMetadata`.
  24828. */
  24829. function compileDeclarePipeFromMetadata(meta) {
  24830. const definitionMap = createPipeDefinitionMap(meta);
  24831. const expression = importExpr(Identifiers.declarePipe).callFn([definitionMap.toLiteralMap()]);
  24832. const type = createPipeType(meta);
  24833. return { expression, type, statements: [] };
  24834. }
  24835. /**
  24836. * Gathers the declaration fields for a Pipe into a `DefinitionMap`.
  24837. */
  24838. function createPipeDefinitionMap(meta) {
  24839. const definitionMap = new DefinitionMap();
  24840. definitionMap.set('minVersion', literal(MINIMUM_PARTIAL_LINKER_VERSION));
  24841. definitionMap.set('version', literal('16.0.4'));
  24842. definitionMap.set('ngImport', importExpr(Identifiers.core));
  24843. // e.g. `type: MyPipe`
  24844. definitionMap.set('type', meta.type.value);
  24845. if (meta.isStandalone) {
  24846. definitionMap.set('isStandalone', literal(meta.isStandalone));
  24847. }
  24848. // e.g. `name: "myPipe"`
  24849. definitionMap.set('name', literal(meta.pipeName));
  24850. if (meta.pure === false) {
  24851. // e.g. `pure: false`
  24852. definitionMap.set('pure', literal(meta.pure));
  24853. }
  24854. return definitionMap;
  24855. }
  24856. //////////////////////////////////////
  24857. // This file only reexports content of the `src` folder. Keep it that way.
  24858. // This function call has a global side effects and publishes the compiler into global namespace for
  24859. // the late binding of the Compiler to the @angular/core for jit compilation.
  24860. publishFacade(_global);
  24861. /**
  24862. * @module
  24863. * @description
  24864. * Entry point for all public APIs of this package.
  24865. */
  24866. // This file only reexports content of the `src` folder. Keep it that way.
  24867. // This file is not used to build this module. It is only used during editing
  24868. // This file is not used to build this module. It is only used during editing
  24869. export { AST, ASTWithName, ASTWithSource, AbsoluteSourceSpan, ArrayType, AstMemoryEfficientTransformer, AstTransformer, Attribute, Binary, BinaryOperator, BinaryOperatorExpr, BindingPipe, BoundElementProperty, BuiltinType, BuiltinTypeName, CUSTOM_ELEMENTS_SCHEMA, Call, Chain, ChangeDetectionStrategy, CommaExpr, Comment, CompilerConfig, Conditional, ConditionalExpr, ConstantPool, CssSelector, DEFAULT_INTERPOLATION_CONFIG, DYNAMIC_TYPE, DeclareFunctionStmt, DeclareVarStmt, DomElementSchemaRegistry, EOF, Element, ElementSchemaRegistry, EmitterVisitorContext, EmptyExpr, Expansion, ExpansionCase, Expression, ExpressionBinding, ExpressionStatement, ExpressionType, ExternalExpr, ExternalReference, FactoryTarget$1 as FactoryTarget, FunctionExpr, HtmlParser, HtmlTagDefinition, I18NHtmlParser, IfStmt, ImplicitReceiver, InstantiateExpr, Interpolation, InterpolationConfig, InvokeFunctionExpr, JSDocComment, JitEvaluator, KeyedRead, KeyedWrite, LeadingComment, 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, ParsedProperty, ParsedPropertyType, ParsedVariable, Parser$1 as Parser, ParserError, PrefixNot, PropertyRead, PropertyWrite, R3BoundTarget, Identifiers as R3Identifiers, R3SelectorScopeMode, R3TargetBinder, R3TemplateDependencyKind, ReadKeyExpr, ReadPropExpr, ReadVarExpr, RecursiveAstVisitor, RecursiveVisitor, ResourceLoader, ReturnStatement, STRING_TYPE, SafeCall, SafeKeyedRead, SafePropertyRead, SelectorContext, SelectorListContext, SelectorMatcher, Serializer, SplitInterpolation, Statement, StmtModifier, TagContentType, TaggedTemplateExpr, TemplateBindingParseResult, TemplateLiteral, TemplateLiteralElement, Text, ThisReceiver, BoundAttribute as TmplAstBoundAttribute, BoundEvent as TmplAstBoundEvent, BoundText as TmplAstBoundText, Content as TmplAstContent, Element$1 as TmplAstElement, Icu$1 as TmplAstIcu, RecursiveVisitor$1 as TmplAstRecursiveVisitor, Reference as TmplAstReference, Template as TmplAstTemplate, Text$3 as TmplAstText, TextAttribute as TmplAstTextAttribute, Variable as TmplAstVariable, Token, TokenType, TransplantedType, TreeError, Type, TypeModifier, TypeofExpr, Unary, UnaryOperator, UnaryOperatorExpr, VERSION, VariableBinding, Version, ViewEncapsulation, WrappedNodeExpr, WriteKeyExpr, WritePropExpr, WriteVarExpr, Xliff, Xliff2, Xmb, XmlParser, Xtb, _ParseAST, compileClassMetadata, compileComponentFromMetadata, compileDeclareClassMetadata, compileDeclareComponentFromMetadata, compileDeclareDirectiveFromMetadata, compileDeclareFactoryFunction, compileDeclareInjectableFromMetadata, compileDeclareInjectorFromMetadata, compileDeclareNgModuleFromMetadata, compileDeclarePipeFromMetadata, compileDirectiveFromMetadata, compileFactoryFunction, compileInjectable, compileInjector, compileNgModule, compilePipeFromMetadata, computeMsgId, core, createInjectableType, createMayBeForwardRefExpression, devOnlyGuardedExpression, emitDistinctChangesOnlyDefaultValue, getHtmlTagDefinition, getNsPrefix, getSafePropertyAccessString, identifierName, isIdentifier, isNgContainer, isNgContent, isNgTemplate, jsDocComment, leadingComment, literalMap, makeBindingParser, mergeNsAndName, output_ast as outputAst, parseHostBindings, parseTemplate, preserveWhitespacesDefault, publishFacade, r3JitTypeSourceSpan, sanitizeIdentifier, splitNsName, verifyHostBindings, visitAll };
  24870. //# sourceMappingURL=compiler.mjs.map