dav.js 215 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296
  1. /**
  2. * WebDAV, CalDAV, and CardDAV client for nodejs and the browser.
  3. * from https://github.com/lambdabaa/dav/
  4. */
  5. /**
  6. * Polyfill from developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/find
  7. */
  8. if (!Array.prototype.find) {
  9. Array.prototype.find = function(predicate) {
  10. if (this == null) {
  11. throw new TypeError('Array.prototype.find called on null or undefined');
  12. }
  13. if (typeof predicate !== 'function') {
  14. throw new TypeError('predicate must be a function');
  15. }
  16. var list = Object(this);
  17. var length = list.length >>> 0;
  18. var thisArg = arguments[1];
  19. var value;
  20. for (var i = 0; i < length; i++) {
  21. value = list[i];
  22. if (predicate.call(thisArg, value, i, list)) {
  23. return value;
  24. }
  25. }
  26. return undefined;
  27. };
  28. }
  29. /**
  30. * Polyfill from developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
  31. */
  32. if (!Object.assign) {
  33. Object.defineProperty(Object, 'assign', {
  34. enumerable: false,
  35. configurable: true,
  36. writable: true,
  37. value: function(target, firstSource) {
  38. 'use strict';
  39. if (target === undefined || target === null) {
  40. throw new TypeError('Cannot convert first argument to object');
  41. }
  42. var to = Object(target);
  43. for (var i = 1; i < arguments.length; i++) {
  44. var nextSource = arguments[i];
  45. if (nextSource === undefined || nextSource === null) {
  46. continue;
  47. }
  48. nextSource = Object(nextSource);
  49. var keysArray = Object.keys(Object(nextSource));
  50. for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) {
  51. var nextKey = keysArray[nextIndex];
  52. var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey);
  53. if (desc !== undefined && desc.enumerable) {
  54. to[nextKey] = nextSource[nextKey];
  55. }
  56. }
  57. }
  58. return to;
  59. }
  60. });
  61. }
  62. /**
  63. * Copyright (c) 2014, Facebook, Inc.
  64. * All rights reserved.
  65. *
  66. * This source code is licensed under the BSD-style license found in the
  67. * https://raw.github.com/facebook/regenerator/master/LICENSE file. An
  68. * additional grant of patent rights can be found in the PATENTS file in
  69. * the same directory.
  70. */
  71. !(function(global) {
  72. "use strict";
  73. var hasOwn = Object.prototype.hasOwnProperty;
  74. var undefined; // More compressible than void 0.
  75. var iteratorSymbol =
  76. typeof Symbol === "function" && Symbol.iterator || "@@iterator";
  77. var inModule = typeof module === "object";
  78. var runtime = global.regeneratorRuntime;
  79. if (runtime) {
  80. if (inModule) {
  81. // If regeneratorRuntime is defined globally and we're in a module,
  82. // make the exports object identical to regeneratorRuntime.
  83. module.exports = runtime;
  84. }
  85. // Don't bother evaluating the rest of this file if the runtime was
  86. // already defined globally.
  87. return;
  88. }
  89. // Define the runtime globally (as expected by generated code) as either
  90. // module.exports (if we're in a module) or a new, empty object.
  91. runtime = global.regeneratorRuntime = inModule ? module.exports : {};
  92. function wrap(innerFn, outerFn, self, tryLocsList) {
  93. // If outerFn provided, then outerFn.prototype instanceof Generator.
  94. var generator = Object.create((outerFn || Generator).prototype);
  95. generator._invoke = makeInvokeMethod(
  96. innerFn, self || null,
  97. new Context(tryLocsList || [])
  98. );
  99. return generator;
  100. }
  101. runtime.wrap = wrap;
  102. // Try/catch helper to minimize deoptimizations. Returns a completion
  103. // record like context.tryEntries[i].completion. This interface could
  104. // have been (and was previously) designed to take a closure to be
  105. // invoked without arguments, but in all the cases we care about we
  106. // already have an existing method we want to call, so there's no need
  107. // to create a new function object. We can even get away with assuming
  108. // the method takes exactly one argument, since that happens to be true
  109. // in every case, so we don't have to touch the arguments object. The
  110. // only additional allocation required is the completion record, which
  111. // has a stable shape and so hopefully should be cheap to allocate.
  112. function tryCatch(fn, obj, arg) {
  113. try {
  114. return { type: "normal", arg: fn.call(obj, arg) };
  115. } catch (err) {
  116. return { type: "throw", arg: err };
  117. }
  118. }
  119. var GenStateSuspendedStart = "suspendedStart";
  120. var GenStateSuspendedYield = "suspendedYield";
  121. var GenStateExecuting = "executing";
  122. var GenStateCompleted = "completed";
  123. // Returning this object from the innerFn has the same effect as
  124. // breaking out of the dispatch switch statement.
  125. var ContinueSentinel = {};
  126. // Dummy constructor functions that we use as the .constructor and
  127. // .constructor.prototype properties for functions that return Generator
  128. // objects. For full spec compliance, you may wish to configure your
  129. // minifier not to mangle the names of these two functions.
  130. function Generator() {}
  131. function GeneratorFunction() {}
  132. function GeneratorFunctionPrototype() {}
  133. var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype;
  134. GeneratorFunction.prototype = Gp.constructor = GeneratorFunctionPrototype;
  135. GeneratorFunctionPrototype.constructor = GeneratorFunction;
  136. GeneratorFunction.displayName = "GeneratorFunction";
  137. runtime.isGeneratorFunction = function(genFun) {
  138. var ctor = typeof genFun === "function" && genFun.constructor;
  139. return ctor
  140. ? ctor === GeneratorFunction ||
  141. // For the native GeneratorFunction constructor, the best we can
  142. // do is to check its .name property.
  143. (ctor.displayName || ctor.name) === "GeneratorFunction"
  144. : false;
  145. };
  146. runtime.mark = function(genFun) {
  147. genFun.__proto__ = GeneratorFunctionPrototype;
  148. genFun.prototype = Object.create(Gp);
  149. return genFun;
  150. };
  151. runtime.async = function(innerFn, outerFn, self, tryLocsList) {
  152. return new Promise(function(resolve, reject) {
  153. var generator = wrap(innerFn, outerFn, self, tryLocsList);
  154. var callNext = step.bind(generator, "next");
  155. var callThrow = step.bind(generator, "throw");
  156. function step(method, arg) {
  157. var record = tryCatch(generator[method], generator, arg);
  158. if (record.type === "throw") {
  159. reject(record.arg);
  160. return;
  161. }
  162. var info = record.arg;
  163. if (info.done) {
  164. resolve(info.value);
  165. } else {
  166. Promise.resolve(info.value).then(callNext, callThrow);
  167. }
  168. }
  169. callNext();
  170. });
  171. };
  172. function makeInvokeMethod(innerFn, self, context) {
  173. var state = GenStateSuspendedStart;
  174. return function invoke(method, arg) {
  175. if (state === GenStateExecuting) {
  176. throw new Error("Generator is already running");
  177. }
  178. if (state === GenStateCompleted) {
  179. // Be forgiving, per 25.3.3.3.3 of the spec:
  180. // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-generatorresume
  181. return doneResult();
  182. }
  183. while (true) {
  184. var delegate = context.delegate;
  185. if (delegate) {
  186. if (method === "return" ||
  187. (method === "throw" && delegate.iterator[method] === undefined)) {
  188. // A return or throw (when the delegate iterator has no throw
  189. // method) always terminates the yield* loop.
  190. context.delegate = null;
  191. // If the delegate iterator has a return method, give it a
  192. // chance to clean up.
  193. var returnMethod = delegate.iterator["return"];
  194. if (returnMethod) {
  195. var record = tryCatch(returnMethod, delegate.iterator, arg);
  196. if (record.type === "throw") {
  197. // If the return method threw an exception, let that
  198. // exception prevail over the original return or throw.
  199. method = "throw";
  200. arg = record.arg;
  201. continue;
  202. }
  203. }
  204. if (method === "return") {
  205. // Continue with the outer return, now that the delegate
  206. // iterator has been terminated.
  207. continue;
  208. }
  209. }
  210. var record = tryCatch(
  211. delegate.iterator[method],
  212. delegate.iterator,
  213. arg
  214. );
  215. if (record.type === "throw") {
  216. context.delegate = null;
  217. // Like returning generator.throw(uncaught), but without the
  218. // overhead of an extra function call.
  219. method = "throw";
  220. arg = record.arg;
  221. continue;
  222. }
  223. // Delegate generator ran and handled its own exceptions so
  224. // regardless of what the method was, we continue as if it is
  225. // "next" with an undefined arg.
  226. method = "next";
  227. arg = undefined;
  228. var info = record.arg;
  229. if (info.done) {
  230. context[delegate.resultName] = info.value;
  231. context.next = delegate.nextLoc;
  232. } else {
  233. state = GenStateSuspendedYield;
  234. return info;
  235. }
  236. context.delegate = null;
  237. }
  238. if (method === "next") {
  239. if (state === GenStateSuspendedYield) {
  240. context.sent = arg;
  241. } else {
  242. delete context.sent;
  243. }
  244. } else if (method === "throw") {
  245. if (state === GenStateSuspendedStart) {
  246. state = GenStateCompleted;
  247. throw arg;
  248. }
  249. if (context.dispatchException(arg)) {
  250. // If the dispatched exception was caught by a catch block,
  251. // then let that catch block handle the exception normally.
  252. method = "next";
  253. arg = undefined;
  254. }
  255. } else if (method === "return") {
  256. context.abrupt("return", arg);
  257. }
  258. state = GenStateExecuting;
  259. var record = tryCatch(innerFn, self, context);
  260. if (record.type === "normal") {
  261. // If an exception is thrown from innerFn, we leave state ===
  262. // GenStateExecuting and loop back for another invocation.
  263. state = context.done
  264. ? GenStateCompleted
  265. : GenStateSuspendedYield;
  266. var info = {
  267. value: record.arg,
  268. done: context.done
  269. };
  270. if (record.arg === ContinueSentinel) {
  271. if (context.delegate && method === "next") {
  272. // Deliberately forget the last sent value so that we don't
  273. // accidentally pass it on to the delegate.
  274. arg = undefined;
  275. }
  276. } else {
  277. return info;
  278. }
  279. } else if (record.type === "throw") {
  280. state = GenStateCompleted;
  281. // Dispatch the exception by looping back around to the
  282. // context.dispatchException(arg) call above.
  283. method = "throw";
  284. arg = record.arg;
  285. }
  286. }
  287. };
  288. }
  289. function defineGeneratorMethod(method) {
  290. Gp[method] = function(arg) {
  291. return this._invoke(method, arg);
  292. };
  293. }
  294. defineGeneratorMethod("next");
  295. defineGeneratorMethod("throw");
  296. defineGeneratorMethod("return");
  297. Gp[iteratorSymbol] = function() {
  298. return this;
  299. };
  300. Gp.toString = function() {
  301. return "[object Generator]";
  302. };
  303. function pushTryEntry(locs) {
  304. var entry = { tryLoc: locs[0] };
  305. if (1 in locs) {
  306. entry.catchLoc = locs[1];
  307. }
  308. if (2 in locs) {
  309. entry.finallyLoc = locs[2];
  310. entry.afterLoc = locs[3];
  311. }
  312. this.tryEntries.push(entry);
  313. }
  314. function resetTryEntry(entry) {
  315. var record = entry.completion || {};
  316. record.type = "normal";
  317. delete record.arg;
  318. entry.completion = record;
  319. }
  320. function Context(tryLocsList) {
  321. // The root entry object (effectively a try statement without a catch
  322. // or a finally block) gives us a place to store values thrown from
  323. // locations where there is no enclosing try statement.
  324. this.tryEntries = [{ tryLoc: "root" }];
  325. tryLocsList.forEach(pushTryEntry, this);
  326. this.reset();
  327. }
  328. runtime.keys = function(object) {
  329. var keys = [];
  330. for (var key in object) {
  331. keys.push(key);
  332. }
  333. keys.reverse();
  334. // Rather than returning an object with a next method, we keep
  335. // things simple and return the next function itself.
  336. return function next() {
  337. while (keys.length) {
  338. var key = keys.pop();
  339. if (key in object) {
  340. next.value = key;
  341. next.done = false;
  342. return next;
  343. }
  344. }
  345. // To avoid creating an additional object, we just hang the .value
  346. // and .done properties off the next function object itself. This
  347. // also ensures that the minifier will not anonymize the function.
  348. next.done = true;
  349. return next;
  350. };
  351. };
  352. function values(iterable) {
  353. if (iterable) {
  354. var iteratorMethod = iterable[iteratorSymbol];
  355. if (iteratorMethod) {
  356. return iteratorMethod.call(iterable);
  357. }
  358. if (typeof iterable.next === "function") {
  359. return iterable;
  360. }
  361. if (!isNaN(iterable.length)) {
  362. var i = -1, next = function next() {
  363. while (++i < iterable.length) {
  364. if (hasOwn.call(iterable, i)) {
  365. next.value = iterable[i];
  366. next.done = false;
  367. return next;
  368. }
  369. }
  370. next.value = undefined;
  371. next.done = true;
  372. return next;
  373. };
  374. return next.next = next;
  375. }
  376. }
  377. // Return an iterator with no values.
  378. return { next: doneResult };
  379. }
  380. runtime.values = values;
  381. function doneResult() {
  382. return { value: undefined, done: true };
  383. }
  384. Context.prototype = {
  385. constructor: Context,
  386. reset: function() {
  387. this.prev = 0;
  388. this.next = 0;
  389. this.sent = undefined;
  390. this.done = false;
  391. this.delegate = null;
  392. this.tryEntries.forEach(resetTryEntry);
  393. // Pre-initialize at least 20 temporary variables to enable hidden
  394. // class optimizations for simple generators.
  395. for (var tempIndex = 0, tempName;
  396. hasOwn.call(this, tempName = "t" + tempIndex) || tempIndex < 20;
  397. ++tempIndex) {
  398. this[tempName] = null;
  399. }
  400. },
  401. stop: function() {
  402. this.done = true;
  403. var rootEntry = this.tryEntries[0];
  404. var rootRecord = rootEntry.completion;
  405. if (rootRecord.type === "throw") {
  406. throw rootRecord.arg;
  407. }
  408. return this.rval;
  409. },
  410. dispatchException: function(exception) {
  411. if (this.done) {
  412. throw exception;
  413. }
  414. var context = this;
  415. function handle(loc, caught) {
  416. record.type = "throw";
  417. record.arg = exception;
  418. context.next = loc;
  419. return !!caught;
  420. }
  421. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  422. var entry = this.tryEntries[i];
  423. var record = entry.completion;
  424. if (entry.tryLoc === "root") {
  425. // Exception thrown outside of any try block that could handle
  426. // it, so set the completion value of the entire function to
  427. // throw the exception.
  428. return handle("end");
  429. }
  430. if (entry.tryLoc <= this.prev) {
  431. var hasCatch = hasOwn.call(entry, "catchLoc");
  432. var hasFinally = hasOwn.call(entry, "finallyLoc");
  433. if (hasCatch && hasFinally) {
  434. if (this.prev < entry.catchLoc) {
  435. return handle(entry.catchLoc, true);
  436. } else if (this.prev < entry.finallyLoc) {
  437. return handle(entry.finallyLoc);
  438. }
  439. } else if (hasCatch) {
  440. if (this.prev < entry.catchLoc) {
  441. return handle(entry.catchLoc, true);
  442. }
  443. } else if (hasFinally) {
  444. if (this.prev < entry.finallyLoc) {
  445. return handle(entry.finallyLoc);
  446. }
  447. } else {
  448. throw new Error("try statement without catch or finally");
  449. }
  450. }
  451. }
  452. },
  453. abrupt: function(type, arg) {
  454. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  455. var entry = this.tryEntries[i];
  456. if (entry.tryLoc <= this.prev &&
  457. hasOwn.call(entry, "finallyLoc") &&
  458. this.prev < entry.finallyLoc) {
  459. var finallyEntry = entry;
  460. break;
  461. }
  462. }
  463. if (finallyEntry &&
  464. (type === "break" ||
  465. type === "continue") &&
  466. finallyEntry.tryLoc <= arg &&
  467. arg <= finallyEntry.finallyLoc) {
  468. // Ignore the finally entry if control is not jumping to a
  469. // location outside the try/catch block.
  470. finallyEntry = null;
  471. }
  472. var record = finallyEntry ? finallyEntry.completion : {};
  473. record.type = type;
  474. record.arg = arg;
  475. if (finallyEntry) {
  476. this.next = finallyEntry.finallyLoc;
  477. } else {
  478. this.complete(record);
  479. }
  480. return ContinueSentinel;
  481. },
  482. complete: function(record, afterLoc) {
  483. if (record.type === "throw") {
  484. throw record.arg;
  485. }
  486. if (record.type === "break" ||
  487. record.type === "continue") {
  488. this.next = record.arg;
  489. } else if (record.type === "return") {
  490. this.rval = record.arg;
  491. this.next = "end";
  492. } else if (record.type === "normal" && afterLoc) {
  493. this.next = afterLoc;
  494. }
  495. },
  496. finish: function(finallyLoc) {
  497. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  498. var entry = this.tryEntries[i];
  499. if (entry.finallyLoc === finallyLoc) {
  500. this.complete(entry.completion, entry.afterLoc);
  501. resetTryEntry(entry);
  502. return ContinueSentinel;
  503. }
  504. }
  505. },
  506. "catch": function(tryLoc) {
  507. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  508. var entry = this.tryEntries[i];
  509. if (entry.tryLoc === tryLoc) {
  510. var record = entry.completion;
  511. if (record.type === "throw") {
  512. var thrown = record.arg;
  513. resetTryEntry(entry);
  514. }
  515. return thrown;
  516. }
  517. }
  518. // The context.catch method must only be called with a location
  519. // argument that corresponds to a known catch block.
  520. throw new Error("illegal catch attempt");
  521. },
  522. delegateYield: function(iterable, resultName, nextLoc) {
  523. this.delegate = {
  524. iterator: values(iterable),
  525. resultName: resultName,
  526. nextLoc: nextLoc
  527. };
  528. return ContinueSentinel;
  529. }
  530. };
  531. })(
  532. // Among the various tricks for obtaining a reference to the global
  533. // object, this seems to be the most reliable technique that does not
  534. // use indirect eval (which violates Content Security Policy).
  535. typeof global === "object" ? global :
  536. typeof window === "object" ? window :
  537. typeof self === "object" ? self : this
  538. );
  539. (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.dav = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  540. 'use strict';
  541. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }
  542. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  543. var _co = require('co');
  544. var _co2 = _interopRequireDefault(_co);
  545. var _url = require('url');
  546. var _url2 = _interopRequireDefault(_url);
  547. var _calendars = require('./calendars');
  548. var _contacts = require('./contacts');
  549. var _fuzzy_url_equals = require('./fuzzy_url_equals');
  550. var _fuzzy_url_equals2 = _interopRequireDefault(_fuzzy_url_equals);
  551. var _model = require('./model');
  552. var _namespace = require('./namespace');
  553. var ns = _interopRequireWildcard(_namespace);
  554. var _request = require('./request');
  555. var request = _interopRequireWildcard(_request);
  556. var debug = require('./debug')('dav:accounts');
  557. var defaults = {
  558. accountType: 'caldav',
  559. loadCollections: true,
  560. loadObjects: false
  561. };
  562. /**
  563. * rfc 6764.
  564. *
  565. * @param {dav.Account} account to find root url for.
  566. */
  567. var serviceDiscovery = _co2['default'].wrap(regeneratorRuntime.mark(function callee$0$0(account, options) {
  568. var endpoint, uri, req, xhr, _location;
  569. return regeneratorRuntime.wrap(function callee$0$0$(context$1$0) {
  570. while (1) switch (context$1$0.prev = context$1$0.next) {
  571. case 0:
  572. debug('Attempt service discovery.');
  573. endpoint = _url2['default'].parse(account.server);
  574. endpoint.protocol = endpoint.protocol || 'http'; // TODO(gareth) https?
  575. uri = _url2['default'].format({
  576. protocol: endpoint.protocol,
  577. host: endpoint.host,
  578. pathname: '/.well-known/' + options.accountType
  579. });
  580. req = request.basic({ method: 'GET' });
  581. context$1$0.prev = 5;
  582. context$1$0.next = 8;
  583. return options.xhr.send(req, uri, { sandbox: options.sandbox });
  584. case 8:
  585. xhr = context$1$0.sent;
  586. if (!(xhr.status >= 300 && xhr.status < 400)) {
  587. context$1$0.next = 14;
  588. break;
  589. }
  590. _location = xhr.getResponseHeader('Location');
  591. if (!(typeof _location === 'string' && _location.length)) {
  592. context$1$0.next = 14;
  593. break;
  594. }
  595. debug('Discovery redirected to ' + _location);
  596. return context$1$0.abrupt('return', _url2['default'].format({
  597. protocol: endpoint.protocol,
  598. host: endpoint.host,
  599. pathname: _location
  600. }));
  601. case 14:
  602. context$1$0.next = 19;
  603. break;
  604. case 16:
  605. context$1$0.prev = 16;
  606. context$1$0.t0 = context$1$0['catch'](5);
  607. debug('Discovery failed... failover to the provided url');
  608. case 19:
  609. return context$1$0.abrupt('return', endpoint.href);
  610. case 20:
  611. case 'end':
  612. return context$1$0.stop();
  613. }
  614. }, callee$0$0, this, [[5, 16]]);
  615. }));
  616. /**
  617. * rfc 5397.
  618. *
  619. * @param {dav.Account} account to get principal url for.
  620. */
  621. var principalUrl = _co2['default'].wrap(regeneratorRuntime.mark(function callee$0$0(account, options) {
  622. var req, res, container;
  623. return regeneratorRuntime.wrap(function callee$0$0$(context$1$0) {
  624. while (1) switch (context$1$0.prev = context$1$0.next) {
  625. case 0:
  626. debug('Fetch principal url from context path ' + account.rootUrl + '.');
  627. req = request.propfind({
  628. props: [{ name: 'current-user-principal', namespace: ns.DAV }],
  629. depth: 0,
  630. mergeResponses: true
  631. });
  632. context$1$0.next = 4;
  633. return options.xhr.send(req, account.rootUrl, {
  634. sandbox: options.sandbox
  635. });
  636. case 4:
  637. res = context$1$0.sent;
  638. container = res.props;
  639. debug('Received principal: ' + container.currentUserPrincipal);
  640. return context$1$0.abrupt('return', _url2['default'].resolve(account.rootUrl, container.currentUserPrincipal));
  641. case 8:
  642. case 'end':
  643. return context$1$0.stop();
  644. }
  645. }, callee$0$0, this);
  646. }));
  647. /**
  648. * @param {dav.Account} account to get home url for.
  649. */
  650. var homeUrl = _co2['default'].wrap(regeneratorRuntime.mark(function callee$0$0(account, options) {
  651. var prop, req, responses, response, container, href;
  652. return regeneratorRuntime.wrap(function callee$0$0$(context$1$0) {
  653. while (1) switch (context$1$0.prev = context$1$0.next) {
  654. case 0:
  655. debug('Fetch home url from principal url ' + account.principalUrl + '.');
  656. prop = undefined;
  657. if (options.accountType === 'caldav') {
  658. prop = { name: 'calendar-home-set', namespace: ns.CALDAV };
  659. } else if (options.accountType === 'carddav') {
  660. prop = { name: 'addressbook-home-set', namespace: ns.CARDDAV };
  661. }
  662. req = request.propfind({ props: [prop] });
  663. context$1$0.next = 6;
  664. return options.xhr.send(req, account.principalUrl, {
  665. sandbox: options.sandbox
  666. });
  667. case 6:
  668. responses = context$1$0.sent;
  669. response = responses.find(function (response) {
  670. return (0, _fuzzy_url_equals2['default'])(account.principalUrl, response.href);
  671. });
  672. container = response.props;
  673. href = undefined;
  674. if (options.accountType === 'caldav') {
  675. debug('Received home: ' + container.calendarHomeSet);
  676. href = container.calendarHomeSet;
  677. } else if (options.accountType === 'carddav') {
  678. debug('Received home: ' + container.addressbookHomeSet);
  679. href = container.addressbookHomeSet;
  680. }
  681. return context$1$0.abrupt('return', _url2['default'].resolve(account.rootUrl, href));
  682. case 12:
  683. case 'end':
  684. return context$1$0.stop();
  685. }
  686. }, callee$0$0, this);
  687. }));
  688. /**
  689. * Options:
  690. *
  691. * (String) accountType - one of 'caldav' or 'carddav'. Defaults to 'caldav'.
  692. * (Array.<Object>) filters - list of caldav filters to send with request.
  693. * (Boolean) loadCollections - whether or not to load dav collections.
  694. * (Boolean) loadObjects - whether or not to load dav objects.
  695. * (dav.Sandbox) sandbox - optional request sandbox.
  696. * (String) server - some url for server (needn't be base url).
  697. * (String) timezone - VTIMEZONE calendar object.
  698. * (dav.Transport) xhr - request sender.
  699. *
  700. * @return {Promise} a promise that will resolve with a dav.Account object.
  701. */
  702. exports.createAccount = _co2['default'].wrap(regeneratorRuntime.mark(function callee$0$0(options) {
  703. var account, key, loadCollections, loadObjects, collections;
  704. return regeneratorRuntime.wrap(function callee$0$0$(context$1$0) {
  705. while (1) switch (context$1$0.prev = context$1$0.next) {
  706. case 0:
  707. options = Object.assign({}, defaults, options);
  708. if (typeof options.loadObjects !== 'boolean') {
  709. options.loadObjects = options.loadCollections;
  710. }
  711. account = new _model.Account({
  712. server: options.server,
  713. credentials: options.xhr.credentials
  714. });
  715. context$1$0.next = 5;
  716. return serviceDiscovery(account, options);
  717. case 5:
  718. account.rootUrl = context$1$0.sent;
  719. context$1$0.next = 8;
  720. return principalUrl(account, options);
  721. case 8:
  722. account.principalUrl = context$1$0.sent;
  723. context$1$0.next = 11;
  724. return homeUrl(account, options);
  725. case 11:
  726. account.homeUrl = context$1$0.sent;
  727. if (options.loadCollections) {
  728. context$1$0.next = 14;
  729. break;
  730. }
  731. return context$1$0.abrupt('return', account);
  732. case 14:
  733. key = undefined, loadCollections = undefined, loadObjects = undefined;
  734. if (options.accountType === 'caldav') {
  735. key = 'calendars';
  736. loadCollections = _calendars.listCalendars;
  737. loadObjects = _calendars.listCalendarObjects;
  738. } else if (options.accountType === 'carddav') {
  739. key = 'addressBooks';
  740. loadCollections = _contacts.listAddressBooks;
  741. loadObjects = _contacts.listVCards;
  742. }
  743. context$1$0.next = 18;
  744. return loadCollections(account, options);
  745. case 18:
  746. collections = context$1$0.sent;
  747. account[key] = collections;
  748. if (options.loadObjects) {
  749. context$1$0.next = 22;
  750. break;
  751. }
  752. return context$1$0.abrupt('return', account);
  753. case 22:
  754. context$1$0.next = 24;
  755. return collections.map(_co2['default'].wrap(regeneratorRuntime.mark(function callee$1$0(collection) {
  756. return regeneratorRuntime.wrap(function callee$1$0$(context$2$0) {
  757. while (1) switch (context$2$0.prev = context$2$0.next) {
  758. case 0:
  759. context$2$0.prev = 0;
  760. context$2$0.next = 3;
  761. return loadObjects(collection, options);
  762. case 3:
  763. collection.objects = context$2$0.sent;
  764. context$2$0.next = 9;
  765. break;
  766. case 6:
  767. context$2$0.prev = 6;
  768. context$2$0.t0 = context$2$0['catch'](0);
  769. collection.error = context$2$0.t0;
  770. case 9:
  771. case 'end':
  772. return context$2$0.stop();
  773. }
  774. }, callee$1$0, this, [[0, 6]]);
  775. })));
  776. case 24:
  777. account[key] = account[key].filter(function (collection) {
  778. return !collection.error;
  779. });
  780. return context$1$0.abrupt('return', account);
  781. case 26:
  782. case 'end':
  783. return context$1$0.stop();
  784. }
  785. }, callee$0$0, this);
  786. }));
  787. // http redirect.
  788. },{"./calendars":2,"./contacts":5,"./debug":6,"./fuzzy_url_equals":7,"./model":9,"./namespace":10,"./request":12,"co":24,"url":29}],2:[function(require,module,exports){
  789. 'use strict';
  790. Object.defineProperty(exports, '__esModule', {
  791. value: true
  792. });
  793. exports.createCalendarObject = createCalendarObject;
  794. exports.updateCalendarObject = updateCalendarObject;
  795. exports.deleteCalendarObject = deleteCalendarObject;
  796. exports.syncCalendar = syncCalendar;
  797. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }
  798. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  799. var _co = require('co');
  800. var _co2 = _interopRequireDefault(_co);
  801. var _url = require('url');
  802. var _url2 = _interopRequireDefault(_url);
  803. var _fuzzy_url_equals = require('./fuzzy_url_equals');
  804. var _fuzzy_url_equals2 = _interopRequireDefault(_fuzzy_url_equals);
  805. var _model = require('./model');
  806. var _namespace = require('./namespace');
  807. var ns = _interopRequireWildcard(_namespace);
  808. var _request = require('./request');
  809. var request = _interopRequireWildcard(_request);
  810. var _webdav = require('./webdav');
  811. var webdav = _interopRequireWildcard(_webdav);
  812. var debug = require('./debug')('dav:calendars');
  813. var ICAL_OBJS = new Set(['VEVENT', 'VTODO', 'VJOURNAL', 'VFREEBUSY', 'VTIMEZONE', 'VALARM']);
  814. /**
  815. * @param {dav.Account} account to fetch calendars for.
  816. */
  817. var listCalendars = _co2['default'].wrap(regeneratorRuntime.mark(function callee$0$0(account, options) {
  818. var req, responses, cals;
  819. return regeneratorRuntime.wrap(function callee$0$0$(context$1$0) {
  820. while (1) switch (context$1$0.prev = context$1$0.next) {
  821. case 0:
  822. debug('Fetch calendars from home url ' + account.homeUrl);
  823. req = request.propfind({
  824. props: [{ name: 'calendar-description', namespace: ns.CALDAV }, { name: 'calendar-timezone', namespace: ns.CALDAV }, { name: 'displayname', namespace: ns.DAV }, { name: 'getctag', namespace: ns.CALENDAR_SERVER }, { name: 'resourcetype', namespace: ns.DAV }, { name: 'supported-calendar-component-set', namespace: ns.CALDAV }, { name: 'sync-token', namespace: ns.DAV }],
  825. depth: 1
  826. });
  827. context$1$0.next = 4;
  828. return options.xhr.send(req, account.homeUrl, {
  829. sandbox: options.sandbox
  830. });
  831. case 4:
  832. responses = context$1$0.sent;
  833. debug('Found ' + responses.length + ' calendars.');
  834. cals = responses.filter(function (res) {
  835. return res.props.resourcetype.includes('calendar');
  836. }).filter(function (res) {
  837. // We only want the calendar if it contains iCalendar objects.
  838. var components = res.props.supportedCalendarComponentSet || [];
  839. return components.reduce(function (hasObjs, component) {
  840. return hasObjs || ICAL_OBJS.has(component);
  841. }, false);
  842. }).map(function (res) {
  843. debug('Found calendar ' + res.props.displayname + ',\n props: ' + JSON.stringify(res.props));
  844. return new _model.Calendar({
  845. data: res,
  846. account: account,
  847. description: res.props.calendarDescription,
  848. timezone: res.props.calendarTimezone,
  849. url: _url2['default'].resolve(account.rootUrl, res.href),
  850. ctag: res.props.getctag,
  851. displayName: res.props.displayname,
  852. components: res.props.supportedCalendarComponentSet,
  853. resourcetype: res.props.resourcetype,
  854. syncToken: res.props.syncToken
  855. });
  856. });
  857. context$1$0.next = 9;
  858. return cals.map(_co2['default'].wrap(regeneratorRuntime.mark(function callee$1$0(cal) {
  859. return regeneratorRuntime.wrap(function callee$1$0$(context$2$0) {
  860. while (1) switch (context$2$0.prev = context$2$0.next) {
  861. case 0:
  862. context$2$0.next = 2;
  863. return webdav.supportedReportSet(cal, options);
  864. case 2:
  865. cal.reports = context$2$0.sent;
  866. case 3:
  867. case 'end':
  868. return context$2$0.stop();
  869. }
  870. }, callee$1$0, this);
  871. })));
  872. case 9:
  873. return context$1$0.abrupt('return', cals);
  874. case 10:
  875. case 'end':
  876. return context$1$0.stop();
  877. }
  878. }, callee$0$0, this);
  879. }));
  880. exports.listCalendars = listCalendars;
  881. /**
  882. * @param {dav.Calendar} calendar the calendar to put the object on.
  883. * @return {Promise} promise will resolve when the calendar has been created.
  884. *
  885. * Options:
  886. *
  887. * (String) data - rfc 5545 VCALENDAR object.
  888. * (String) filename - name for the calendar ics file.
  889. * (dav.Sandbox) sandbox - optional request sandbox.
  890. * (dav.Transport) xhr - request sender.
  891. */
  892. function createCalendarObject(calendar, options) {
  893. var objectUrl = _url2['default'].resolve(calendar.url, options.filename);
  894. options.contentType = options.contentType || "text/calendar; charset=utf-8";
  895. return webdav.createObject(objectUrl, options.data, options);
  896. }
  897. ;
  898. /**
  899. * @param {dav.CalendarObject} calendarObject updated calendar object.
  900. * @return {Promise} promise will resolve when the calendar has been updated.
  901. *
  902. * Options:
  903. *
  904. * (dav.Sandbox) sandbox - optional request sandbox.
  905. * (dav.Transport) xhr - request sender.
  906. */
  907. function updateCalendarObject(calendarObject, options) {
  908. options.contentType = options.contentType || "text/calendar; charset=utf-8";
  909. return webdav.updateObject(calendarObject.url, calendarObject.calendarData, calendarObject.etag, options);
  910. }
  911. /**
  912. * @param {dav.CalendarObject} calendarObject target calendar object.
  913. * @return {Promise} promise will resolve when the calendar has been deleted.
  914. *
  915. * Options:
  916. *
  917. * (dav.Sandbox) sandbox - optional request sandbox.
  918. * (dav.Transport) xhr - request sender.
  919. */
  920. function deleteCalendarObject(calendarObject, options) {
  921. return webdav.deleteObject(calendarObject.url, calendarObject.etag, options);
  922. }
  923. /**
  924. * @param {dav.Calendar} calendar the calendar to fetch objects for.
  925. *
  926. * Options:
  927. *
  928. * (Array.<Object>) filters - optional caldav filters.
  929. * (dav.Sandbox) sandbox - optional request sandbox.
  930. * (dav.Transport) xhr - request sender.
  931. */
  932. var listCalendarObjects = _co2['default'].wrap(regeneratorRuntime.mark(function callee$0$0(calendar, options) {
  933. var filters, req, responses;
  934. return regeneratorRuntime.wrap(function callee$0$0$(context$1$0) {
  935. while (1) switch (context$1$0.prev = context$1$0.next) {
  936. case 0:
  937. debug('Doing REPORT on calendar ' + calendar.url + ' which belongs to\n ' + calendar.account.credentials.username);
  938. filters = options.filters || [{
  939. type: 'comp-filter',
  940. attrs: { name: 'VCALENDAR' },
  941. children: [{
  942. type: 'comp-filter',
  943. attrs: { name: 'VEVENT' }
  944. }]
  945. }];
  946. req = request.calendarQuery({
  947. depth: 1,
  948. props: [{ name: 'getetag', namespace: ns.DAV }, { name: 'calendar-data', namespace: ns.CALDAV }],
  949. filters: filters
  950. });
  951. context$1$0.next = 5;
  952. return options.xhr.send(req, calendar.url, {
  953. sandbox: options.sandbox
  954. });
  955. case 5:
  956. responses = context$1$0.sent;
  957. return context$1$0.abrupt('return', responses.map(function (res) {
  958. debug('Found calendar object with url ' + res.href);
  959. return new _model.CalendarObject({
  960. data: res,
  961. calendar: calendar,
  962. url: _url2['default'].resolve(calendar.account.rootUrl, res.href),
  963. etag: res.props.getetag,
  964. calendarData: res.props.calendarData
  965. });
  966. }));
  967. case 7:
  968. case 'end':
  969. return context$1$0.stop();
  970. }
  971. }, callee$0$0, this);
  972. }));
  973. exports.listCalendarObjects = listCalendarObjects;
  974. /**
  975. * @param {dav.Calendar} calendar the calendar to fetch updates to.
  976. * @return {Promise} promise will resolve with updated calendar object.
  977. *
  978. * Options:
  979. *
  980. * (Array.<Object>) filters - list of caldav filters to send with request.
  981. * (dav.Sandbox) sandbox - optional request sandbox.
  982. * (String) syncMethod - either 'basic' or 'webdav'. If unspecified, will
  983. * try to do webdav sync and failover to basic sync if rfc 6578 is not
  984. * supported by the server.
  985. * (String) timezone - VTIMEZONE calendar object.
  986. * (dav.Transport) xhr - request sender.
  987. */
  988. function syncCalendar(calendar, options) {
  989. options.basicSync = basicSync;
  990. options.webdavSync = webdavSync;
  991. return webdav.syncCollection(calendar, options);
  992. }
  993. /**
  994. * @param {dav.Account} account the account to fetch updates for.
  995. * @return {Promise} promise will resolve with updated account.
  996. *
  997. * Options:
  998. *
  999. * (dav.Sandbox) sandbox - optional request sandbox.
  1000. * (dav.Transport) xhr - request sender.
  1001. */
  1002. var syncCaldavAccount = _co2['default'].wrap(regeneratorRuntime.mark(function callee$0$0(account) {
  1003. var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
  1004. var cals;
  1005. return regeneratorRuntime.wrap(function callee$0$0$(context$1$0) {
  1006. while (1) switch (context$1$0.prev = context$1$0.next) {
  1007. case 0:
  1008. options.loadObjects = false;
  1009. if (!account.calendars) account.calendars = [];
  1010. context$1$0.next = 4;
  1011. return listCalendars(account, options);
  1012. case 4:
  1013. cals = context$1$0.sent;
  1014. cals.filter(function (cal) {
  1015. // Filter the calendars not previously seen.
  1016. return account.calendars.every(function (prev) {
  1017. return !(0, _fuzzy_url_equals2['default'])(prev.url, cal.url);
  1018. });
  1019. }).forEach(function (cal) {
  1020. // Add them to the account's calendar list.
  1021. account.calendars.push(cal);
  1022. });
  1023. options.loadObjects = true;
  1024. context$1$0.next = 9;
  1025. return account.calendars.map(_co2['default'].wrap(regeneratorRuntime.mark(function callee$1$0(cal, index) {
  1026. return regeneratorRuntime.wrap(function callee$1$0$(context$2$0) {
  1027. while (1) switch (context$2$0.prev = context$2$0.next) {
  1028. case 0:
  1029. context$2$0.prev = 0;
  1030. context$2$0.next = 3;
  1031. return syncCalendar(cal, options);
  1032. case 3:
  1033. context$2$0.next = 9;
  1034. break;
  1035. case 5:
  1036. context$2$0.prev = 5;
  1037. context$2$0.t0 = context$2$0['catch'](0);
  1038. debug('Sync calendar ' + cal.displayName + ' failed with ' + context$2$0.t0);
  1039. account.calendars.splice(index, 1);
  1040. case 9:
  1041. case 'end':
  1042. return context$2$0.stop();
  1043. }
  1044. }, callee$1$0, this, [[0, 5]]);
  1045. })));
  1046. case 9:
  1047. return context$1$0.abrupt('return', account);
  1048. case 10:
  1049. case 'end':
  1050. return context$1$0.stop();
  1051. }
  1052. }, callee$0$0, this);
  1053. }));
  1054. exports.syncCaldavAccount = syncCaldavAccount;
  1055. var basicSync = _co2['default'].wrap(regeneratorRuntime.mark(function callee$0$0(calendar, options) {
  1056. var sync;
  1057. return regeneratorRuntime.wrap(function callee$0$0$(context$1$0) {
  1058. while (1) switch (context$1$0.prev = context$1$0.next) {
  1059. case 0:
  1060. context$1$0.next = 2;
  1061. return webdav.isCollectionDirty(calendar, options);
  1062. case 2:
  1063. sync = context$1$0.sent;
  1064. if (sync) {
  1065. context$1$0.next = 6;
  1066. break;
  1067. }
  1068. debug('Local ctag matched remote! No need to sync :).');
  1069. return context$1$0.abrupt('return', calendar);
  1070. case 6:
  1071. debug('ctag changed so we need to fetch stuffs.');
  1072. context$1$0.next = 9;
  1073. return listCalendarObjects(calendar, options);
  1074. case 9:
  1075. calendar.objects = context$1$0.sent;
  1076. return context$1$0.abrupt('return', calendar);
  1077. case 11:
  1078. case 'end':
  1079. return context$1$0.stop();
  1080. }
  1081. }, callee$0$0, this);
  1082. }));
  1083. var webdavSync = _co2['default'].wrap(regeneratorRuntime.mark(function callee$0$0(calendar, options) {
  1084. var req, result;
  1085. return regeneratorRuntime.wrap(function callee$0$0$(context$1$0) {
  1086. while (1) switch (context$1$0.prev = context$1$0.next) {
  1087. case 0:
  1088. req = request.syncCollection({
  1089. props: [{ name: 'getetag', namespace: ns.DAV }, { name: 'calendar-data', namespace: ns.CALDAV }],
  1090. syncLevel: 1,
  1091. syncToken: calendar.syncToken
  1092. });
  1093. context$1$0.next = 3;
  1094. return options.xhr.send(req, calendar.url, {
  1095. sandbox: options.sandbox
  1096. });
  1097. case 3:
  1098. result = context$1$0.sent;
  1099. // TODO(gareth): Handle creations and deletions.
  1100. result.responses.forEach(function (response) {
  1101. // Find the calendar object that this response corresponds with.
  1102. var calendarObject = calendar.objects.filter(function (object) {
  1103. return (0, _fuzzy_url_equals2['default'])(object.url, response.href);
  1104. })[0];
  1105. if (!calendarObject) {
  1106. return;
  1107. }
  1108. calendarObject.etag = response.props.getetag;
  1109. calendarObject.calendarData = response.props.calendarData;
  1110. });
  1111. calendar.syncToken = result.syncToken;
  1112. return context$1$0.abrupt('return', calendar);
  1113. case 7:
  1114. case 'end':
  1115. return context$1$0.stop();
  1116. }
  1117. }, callee$0$0, this);
  1118. }));
  1119. },{"./debug":6,"./fuzzy_url_equals":7,"./model":9,"./namespace":10,"./request":12,"./webdav":22,"co":24,"url":29}],3:[function(require,module,exports){
  1120. /**
  1121. * @fileoverview Camelcase something.
  1122. */
  1123. 'use strict';
  1124. Object.defineProperty(exports, '__esModule', {
  1125. value: true
  1126. });
  1127. exports['default'] = camelize;
  1128. function camelize(str) {
  1129. var delimiter = arguments.length <= 1 || arguments[1] === undefined ? '_' : arguments[1];
  1130. var words = str.split(delimiter);
  1131. return [words[0]].concat(words.slice(1).map(function (word) {
  1132. return word.charAt(0).toUpperCase() + word.slice(1);
  1133. })).join('');
  1134. }
  1135. module.exports = exports['default'];
  1136. },{}],4:[function(require,module,exports){
  1137. 'use strict';
  1138. Object.defineProperty(exports, '__esModule', {
  1139. value: true
  1140. });
  1141. var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
  1142. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }
  1143. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  1144. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
  1145. var _url = require('url');
  1146. var _url2 = _interopRequireDefault(_url);
  1147. var _accounts = require('./accounts');
  1148. var accounts = _interopRequireWildcard(_accounts);
  1149. var _calendars = require('./calendars');
  1150. var calendars = _interopRequireWildcard(_calendars);
  1151. var _contacts = require('./contacts');
  1152. var contacts = _interopRequireWildcard(_contacts);
  1153. /**
  1154. * @param {dav.Transport} xhr - request sender.
  1155. *
  1156. * Options:
  1157. *
  1158. * (String) baseUrl - root url to resolve relative request urls with.
  1159. */
  1160. var Client = (function () {
  1161. function Client(xhr) {
  1162. var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
  1163. _classCallCheck(this, Client);
  1164. this.xhr = xhr;
  1165. Object.assign(this, options);
  1166. // Expose internal modules for unit testing
  1167. this._accounts = accounts;
  1168. this._calendars = calendars;
  1169. this._contacts = contacts;
  1170. }
  1171. /**
  1172. * @param {dav.Request} req - dav request.
  1173. * @param {String} uri - where to send request.
  1174. * @return {Promise} a promise that will be resolved with an xhr request
  1175. * after its readyState is 4 or the result of applying an optional
  1176. * request `transformResponse` function to the xhr object after its
  1177. * readyState is 4.
  1178. *
  1179. * Options:
  1180. *
  1181. * (Object) sandbox - optional request sandbox.
  1182. */
  1183. _createClass(Client, [{
  1184. key: 'send',
  1185. value: function send(req, uri, options) {
  1186. if (this.baseUrl) {
  1187. var urlObj = _url2['default'].parse(uri);
  1188. uri = _url2['default'].resolve(this.baseUrl, urlObj.path);
  1189. }
  1190. return this.xhr.send(req, uri, options);
  1191. }
  1192. }, {
  1193. key: 'createAccount',
  1194. value: function createAccount() {
  1195. var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
  1196. options.xhr = options.xhr || this.xhr;
  1197. return accounts.createAccount(options);
  1198. }
  1199. }, {
  1200. key: 'createCalendarObject',
  1201. value: function createCalendarObject(calendar) {
  1202. var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
  1203. options.xhr = options.xhr || this.xhr;
  1204. return calendars.createCalendarObject(calendar, options);
  1205. }
  1206. }, {
  1207. key: 'updateCalendarObject',
  1208. value: function updateCalendarObject(calendarObject) {
  1209. var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
  1210. options.xhr = options.xhr || this.xhr;
  1211. return calendars.updateCalendarObject(calendarObject, options);
  1212. }
  1213. }, {
  1214. key: 'deleteCalendarObject',
  1215. value: function deleteCalendarObject(calendarObject) {
  1216. var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
  1217. options.xhr = options.xhr || this.xhr;
  1218. return calendars.deleteCalendarObject(calendarObject, options);
  1219. }
  1220. }, {
  1221. key: 'syncCalendar',
  1222. value: function syncCalendar(calendar) {
  1223. var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
  1224. options.xhr = options.xhr || this.xhr;
  1225. return calendars.syncCalendar(calendar, options);
  1226. }
  1227. }, {
  1228. key: 'syncCaldavAccount',
  1229. value: function syncCaldavAccount(account) {
  1230. var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
  1231. options.xhr = options.xhr || this.xhr;
  1232. return calendars.syncCaldavAccount(account, options);
  1233. }
  1234. }, {
  1235. key: 'createCard',
  1236. value: function createCard(addressBook) {
  1237. var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
  1238. options.xhr = options.xhr || this.xhr;
  1239. return contacts.createCard(addressBook, options);
  1240. }
  1241. }, {
  1242. key: 'updateCard',
  1243. value: function updateCard(card) {
  1244. var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
  1245. options.xhr = options.xhr || this.xhr;
  1246. return contacts.updateCard(card, options);
  1247. }
  1248. }, {
  1249. key: 'deleteCard',
  1250. value: function deleteCard(card) {
  1251. var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
  1252. options.xhr = options.xhr || this.xhr;
  1253. return contacts.deleteCard(card, options);
  1254. }
  1255. }, {
  1256. key: 'syncAddressBook',
  1257. value: function syncAddressBook(addressBook) {
  1258. var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
  1259. options.xhr = options.xhr || this.xhr;
  1260. return contacts.syncAddressBook(addressBook, options);
  1261. }
  1262. }, {
  1263. key: 'syncCarddavAccount',
  1264. value: function syncCarddavAccount(account) {
  1265. var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
  1266. options.xhr = options.xhr || this.xhr;
  1267. return contacts.syncCarddavAccount(account, options);
  1268. }
  1269. }]);
  1270. return Client;
  1271. })();
  1272. exports.Client = Client;
  1273. },{"./accounts":1,"./calendars":2,"./contacts":5,"url":29}],5:[function(require,module,exports){
  1274. 'use strict';
  1275. Object.defineProperty(exports, '__esModule', {
  1276. value: true
  1277. });
  1278. exports.createCard = createCard;
  1279. exports.updateCard = updateCard;
  1280. exports.deleteCard = deleteCard;
  1281. exports.syncAddressBook = syncAddressBook;
  1282. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }
  1283. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  1284. var _co = require('co');
  1285. var _co2 = _interopRequireDefault(_co);
  1286. var _url = require('url');
  1287. var _url2 = _interopRequireDefault(_url);
  1288. var _fuzzy_url_equals = require('./fuzzy_url_equals');
  1289. var _fuzzy_url_equals2 = _interopRequireDefault(_fuzzy_url_equals);
  1290. var _model = require('./model');
  1291. var _namespace = require('./namespace');
  1292. var ns = _interopRequireWildcard(_namespace);
  1293. var _request = require('./request');
  1294. var request = _interopRequireWildcard(_request);
  1295. var _webdav = require('./webdav');
  1296. var webdav = _interopRequireWildcard(_webdav);
  1297. var debug = require('./debug')('dav:contacts');
  1298. /**
  1299. * @param {dav.Account} account to fetch address books for.
  1300. */
  1301. var listAddressBooks = _co2['default'].wrap(regeneratorRuntime.mark(function callee$0$0(account, options) {
  1302. var req, responses, addressBooks;
  1303. return regeneratorRuntime.wrap(function callee$0$0$(context$1$0) {
  1304. while (1) switch (context$1$0.prev = context$1$0.next) {
  1305. case 0:
  1306. debug('Fetch address books from home url ' + account.homeUrl);
  1307. req = request.propfind({
  1308. props: [{ name: 'displayname', namespace: ns.DAV }, { name: 'getctag', namespace: ns.CALENDAR_SERVER }, { name: 'resourcetype', namespace: ns.DAV }, { name: 'sync-token', namespace: ns.DAV }],
  1309. depth: 1
  1310. });
  1311. context$1$0.next = 4;
  1312. return options.xhr.send(req, account.homeUrl, {
  1313. sandbox: options.sandbox
  1314. });
  1315. case 4:
  1316. responses = context$1$0.sent;
  1317. addressBooks = responses.filter(function (res) {
  1318. return typeof res.props.displayname === 'string';
  1319. }).map(function (res) {
  1320. debug('Found address book named ' + res.props.displayname + ',\n props: ' + JSON.stringify(res.props));
  1321. return new _model.AddressBook({
  1322. data: res,
  1323. account: account,
  1324. url: _url2['default'].resolve(account.rootUrl, res.href),
  1325. ctag: res.props.getctag,
  1326. displayName: res.props.displayname,
  1327. resourcetype: res.props.resourcetype,
  1328. syncToken: res.props.syncToken
  1329. });
  1330. });
  1331. context$1$0.next = 8;
  1332. return addressBooks.map(_co2['default'].wrap(regeneratorRuntime.mark(function callee$1$0(addressBook) {
  1333. return regeneratorRuntime.wrap(function callee$1$0$(context$2$0) {
  1334. while (1) switch (context$2$0.prev = context$2$0.next) {
  1335. case 0:
  1336. context$2$0.next = 2;
  1337. return webdav.supportedReportSet(addressBook, options);
  1338. case 2:
  1339. addressBook.reports = context$2$0.sent;
  1340. case 3:
  1341. case 'end':
  1342. return context$2$0.stop();
  1343. }
  1344. }, callee$1$0, this);
  1345. })));
  1346. case 8:
  1347. return context$1$0.abrupt('return', addressBooks);
  1348. case 9:
  1349. case 'end':
  1350. return context$1$0.stop();
  1351. }
  1352. }, callee$0$0, this);
  1353. }));
  1354. exports.listAddressBooks = listAddressBooks;
  1355. /**
  1356. * @param {dav.AddressBook} addressBook the address book to put the object on.
  1357. * @return {Promise} promise will resolve when the card has been created.
  1358. *
  1359. * Options:
  1360. *
  1361. * (String) data - vcard object.
  1362. * (String) filename - name for the address book vcf file.
  1363. * (dav.Sandbox) sandbox - optional request sandbox.
  1364. * (dav.Transport) xhr - request sender.
  1365. */
  1366. function createCard(addressBook, options) {
  1367. var objectUrl = _url2['default'].resolve(addressBook.url, options.filename);
  1368. return webdav.createObject(objectUrl, options.data, options);
  1369. }
  1370. /**
  1371. * Options:
  1372. *
  1373. * (dav.Sandbox) sandbox - optional request sandbox.
  1374. */
  1375. var listVCards = _co2['default'].wrap(regeneratorRuntime.mark(function callee$0$0(addressBook, options) {
  1376. var req, responses;
  1377. return regeneratorRuntime.wrap(function callee$0$0$(context$1$0) {
  1378. while (1) switch (context$1$0.prev = context$1$0.next) {
  1379. case 0:
  1380. debug('Doing REPORT on address book ' + addressBook.url + ' which belongs to\n ' + addressBook.account.credentials.username);
  1381. req = request.addressBookQuery({
  1382. depth: 1,
  1383. props: [{ name: 'getetag', namespace: ns.DAV }, { name: 'address-data', namespace: ns.CARDDAV }]
  1384. });
  1385. context$1$0.next = 4;
  1386. return options.xhr.send(req, addressBook.url, {
  1387. sandbox: options.sandbox
  1388. });
  1389. case 4:
  1390. responses = context$1$0.sent;
  1391. return context$1$0.abrupt('return', responses.map(function (res) {
  1392. debug('Found vcard with url ' + res.href);
  1393. return new _model.VCard({
  1394. data: res,
  1395. addressBook: addressBook,
  1396. url: _url2['default'].resolve(addressBook.account.rootUrl, res.href),
  1397. etag: res.props.getetag,
  1398. addressData: res.props.addressData
  1399. });
  1400. }));
  1401. case 6:
  1402. case 'end':
  1403. return context$1$0.stop();
  1404. }
  1405. }, callee$0$0, this);
  1406. }));
  1407. exports.listVCards = listVCards;
  1408. /**
  1409. * @param {dav.VCard} card updated vcard object.
  1410. * @return {Promise} promise will resolve when the card has been updated.
  1411. *
  1412. * Options:
  1413. *
  1414. * (dav.Sandbox) sandbox - optional request sandbox.
  1415. * (dav.Transport) xhr - request sender.
  1416. */
  1417. function updateCard(card, options) {
  1418. return webdav.updateObject(card.url, card.addressData, card.etag, options);
  1419. }
  1420. /**
  1421. * @param {dav.VCard} card target vcard object.
  1422. * @return {Promise} promise will resolve when the calendar has been deleted.
  1423. *
  1424. * Options:
  1425. *
  1426. * (dav.Sandbox) sandbox - optional request sandbox.
  1427. * (dav.Transport) xhr - request sender.
  1428. */
  1429. function deleteCard(card, options) {
  1430. return webdav.deleteObject(card.url, card.etag, options);
  1431. }
  1432. /**
  1433. * @param {dav.Calendar} calendar the calendar to fetch updates to.
  1434. * @return {Promise} promise will resolve with updated calendar object.
  1435. *
  1436. * Options:
  1437. *
  1438. * (dav.Sandbox) sandbox - optional request sandbox.
  1439. * (String) syncMethod - either 'basic' or 'webdav'. If unspecified, will
  1440. * try to do webdav sync and failover to basic sync if rfc 6578 is not
  1441. * supported by the server.
  1442. * (dav.Transport) xhr - request sender.
  1443. */
  1444. function syncAddressBook(addressBook, options) {
  1445. options.basicSync = basicSync;
  1446. options.webdavSync = webdavSync;
  1447. return webdav.syncCollection(addressBook, options);
  1448. }
  1449. /**
  1450. * @param {dav.Account} account the account to fetch updates for.
  1451. * @return {Promise} promise will resolve with updated account.
  1452. *
  1453. * Options:
  1454. *
  1455. * (dav.Sandbox) sandbox - optional request sandbox.
  1456. * (dav.Transport) xhr - request sender.
  1457. */
  1458. var syncCarddavAccount = _co2['default'].wrap(regeneratorRuntime.mark(function callee$0$0(account) {
  1459. var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
  1460. var addressBooks;
  1461. return regeneratorRuntime.wrap(function callee$0$0$(context$1$0) {
  1462. while (1) switch (context$1$0.prev = context$1$0.next) {
  1463. case 0:
  1464. options.loadObjects = false;
  1465. if (!account.addressBooks) {
  1466. account.addressBooks = [];
  1467. }
  1468. context$1$0.next = 4;
  1469. return listAddressBooks(account, options);
  1470. case 4:
  1471. addressBooks = context$1$0.sent;
  1472. addressBooks.filter(function (addressBook) {
  1473. // Filter the address books not previously seen.
  1474. return account.addressBooks.every(function (prev) {
  1475. return !(0, _fuzzy_url_equals2['default'])(prev.url, addressBook.url);
  1476. });
  1477. }).forEach(function (addressBook) {
  1478. return account.addressBooks.push(addressBook);
  1479. });
  1480. options.loadObjects = true;
  1481. context$1$0.next = 9;
  1482. return account.addressBooks.map(_co2['default'].wrap(regeneratorRuntime.mark(function callee$1$0(addressBook, index) {
  1483. return regeneratorRuntime.wrap(function callee$1$0$(context$2$0) {
  1484. while (1) switch (context$2$0.prev = context$2$0.next) {
  1485. case 0:
  1486. context$2$0.prev = 0;
  1487. context$2$0.next = 3;
  1488. return syncAddressBook(addressBook, options);
  1489. case 3:
  1490. context$2$0.next = 9;
  1491. break;
  1492. case 5:
  1493. context$2$0.prev = 5;
  1494. context$2$0.t0 = context$2$0['catch'](0);
  1495. debug('Syncing ' + addressBook.displayName + ' failed with ' + context$2$0.t0);
  1496. account.addressBooks.splice(index, 1);
  1497. case 9:
  1498. case 'end':
  1499. return context$2$0.stop();
  1500. }
  1501. }, callee$1$0, this, [[0, 5]]);
  1502. })));
  1503. case 9:
  1504. return context$1$0.abrupt('return', account);
  1505. case 10:
  1506. case 'end':
  1507. return context$1$0.stop();
  1508. }
  1509. }, callee$0$0, this);
  1510. }));
  1511. exports.syncCarddavAccount = syncCarddavAccount;
  1512. var basicSync = _co2['default'].wrap(regeneratorRuntime.mark(function callee$0$0(addressBook, options) {
  1513. var sync;
  1514. return regeneratorRuntime.wrap(function callee$0$0$(context$1$0) {
  1515. while (1) switch (context$1$0.prev = context$1$0.next) {
  1516. case 0:
  1517. sync = webdav.isCollectionDirty(addressBook, options);
  1518. if (sync) {
  1519. context$1$0.next = 4;
  1520. break;
  1521. }
  1522. debug('Local ctag matched remote! No need to sync :).');
  1523. return context$1$0.abrupt('return', addressBook);
  1524. case 4:
  1525. debug('ctag changed so we need to fetch stuffs.');
  1526. context$1$0.next = 7;
  1527. return listVCards(addressBook, options);
  1528. case 7:
  1529. addressBook.objects = context$1$0.sent;
  1530. return context$1$0.abrupt('return', addressBook);
  1531. case 9:
  1532. case 'end':
  1533. return context$1$0.stop();
  1534. }
  1535. }, callee$0$0, this);
  1536. }));
  1537. var webdavSync = _co2['default'].wrap(regeneratorRuntime.mark(function callee$0$0(addressBook, options) {
  1538. var req, result;
  1539. return regeneratorRuntime.wrap(function callee$0$0$(context$1$0) {
  1540. while (1) switch (context$1$0.prev = context$1$0.next) {
  1541. case 0:
  1542. req = request.syncCollection({
  1543. props: [{ name: 'getetag', namespace: ns.DAV }, { name: 'address-data', namespace: ns.CARDDAV }],
  1544. syncLevel: 1,
  1545. syncToken: addressBook.syncToken
  1546. });
  1547. context$1$0.next = 3;
  1548. return options.xhr.send(req, addressBook.url, {
  1549. sandbox: options.sandbox
  1550. });
  1551. case 3:
  1552. result = context$1$0.sent;
  1553. // TODO(gareth): Handle creations and deletions.
  1554. result.responses.forEach(function (response) {
  1555. // Find the vcard that this response corresponds with.
  1556. var vcard = addressBook.objects.filter(function (object) {
  1557. return (0, _fuzzy_url_equals2['default'])(object.url, response.href);
  1558. })[0];
  1559. if (!vcard) return;
  1560. vcard.etag = response.props.getetag;
  1561. vcard.addressData = response.props.addressData;
  1562. });
  1563. addressBook.syncToken = result.syncToken;
  1564. return context$1$0.abrupt('return', addressBook);
  1565. case 7:
  1566. case 'end':
  1567. return context$1$0.stop();
  1568. }
  1569. }, callee$0$0, this);
  1570. }));
  1571. },{"./debug":6,"./fuzzy_url_equals":7,"./model":9,"./namespace":10,"./request":12,"./webdav":22,"co":24,"url":29}],6:[function(require,module,exports){
  1572. "use strict";
  1573. Object.defineProperty(exports, "__esModule", {
  1574. value: true
  1575. });
  1576. exports["default"] = debug;
  1577. function debug(topic) {
  1578. return function (message) {
  1579. if (debug.enabled) {
  1580. console.log("[" + topic + "] " + message);
  1581. }
  1582. };
  1583. }
  1584. module.exports = exports["default"];
  1585. },{}],7:[function(require,module,exports){
  1586. 'use strict';
  1587. Object.defineProperty(exports, '__esModule', {
  1588. value: true
  1589. });
  1590. exports['default'] = fuzzyUrlEquals;
  1591. function fuzzyUrlEquals(one, other) {
  1592. return fuzzyIncludes(one, other) || fuzzyIncludes(other, one);
  1593. }
  1594. ;
  1595. function fuzzyIncludes(one, other) {
  1596. return one.indexOf(other) !== -1 || other.charAt(other.length - 1) === '/' && one.indexOf(other.slice(0, -1)) !== -1;
  1597. }
  1598. module.exports = exports['default'];
  1599. },{}],8:[function(require,module,exports){
  1600. 'use strict';
  1601. Object.defineProperty(exports, '__esModule', {
  1602. value: true
  1603. });
  1604. function _interopExportWildcard(obj, defaults) { var newObj = defaults({}, obj); delete newObj['default']; return newObj; }
  1605. function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; }
  1606. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }
  1607. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  1608. var _debug = require('./debug');
  1609. var _debug2 = _interopRequireDefault(_debug);
  1610. var _namespace = require('./namespace');
  1611. var ns = _interopRequireWildcard(_namespace);
  1612. var _request = require('./request');
  1613. var request = _interopRequireWildcard(_request);
  1614. var _transport = require('./transport');
  1615. var transport = _interopRequireWildcard(_transport);
  1616. var _package = require('../package');
  1617. Object.defineProperty(exports, 'version', {
  1618. enumerable: true,
  1619. get: function get() {
  1620. return _package.version;
  1621. }
  1622. });
  1623. var _accounts = require('./accounts');
  1624. Object.defineProperty(exports, 'createAccount', {
  1625. enumerable: true,
  1626. get: function get() {
  1627. return _accounts.createAccount;
  1628. }
  1629. });
  1630. var _calendars = require('./calendars');
  1631. _defaults(exports, _interopExportWildcard(_calendars, _defaults));
  1632. var _client = require('./client');
  1633. Object.defineProperty(exports, 'Client', {
  1634. enumerable: true,
  1635. get: function get() {
  1636. return _client.Client;
  1637. }
  1638. });
  1639. var _contacts = require('./contacts');
  1640. _defaults(exports, _interopExportWildcard(_contacts, _defaults));
  1641. var _model = require('./model');
  1642. _defaults(exports, _interopExportWildcard(_model, _defaults));
  1643. Object.defineProperty(exports, 'Request', {
  1644. enumerable: true,
  1645. get: function get() {
  1646. return _request.Request;
  1647. }
  1648. });
  1649. var _sandbox = require('./sandbox');
  1650. Object.defineProperty(exports, 'Sandbox', {
  1651. enumerable: true,
  1652. get: function get() {
  1653. return _sandbox.Sandbox;
  1654. }
  1655. });
  1656. Object.defineProperty(exports, 'createSandbox', {
  1657. enumerable: true,
  1658. get: function get() {
  1659. return _sandbox.createSandbox;
  1660. }
  1661. });
  1662. exports.debug = _debug2['default'];
  1663. exports.ns = ns;
  1664. exports.request = request;
  1665. exports.transport = transport;
  1666. },{"../package":33,"./accounts":1,"./calendars":2,"./client":4,"./contacts":5,"./debug":6,"./model":9,"./namespace":10,"./request":12,"./sandbox":13,"./transport":21}],9:[function(require,module,exports){
  1667. "use strict";
  1668. Object.defineProperty(exports, "__esModule", {
  1669. value: true
  1670. });
  1671. var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
  1672. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  1673. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  1674. var Account = function Account(options) {
  1675. _classCallCheck(this, Account);
  1676. Object.assign(this, {
  1677. server: null,
  1678. credentials: null,
  1679. rootUrl: null,
  1680. principalUrl: null,
  1681. homeUrl: null,
  1682. calendars: null,
  1683. addressBooks: null
  1684. }, options);
  1685. }
  1686. /**
  1687. * Options:
  1688. * (String) username - username (perhaps email) for calendar user.
  1689. * (String) password - plaintext password for calendar user.
  1690. * (String) clientId - oauth client id.
  1691. * (String) clientSecret - oauth client secret.
  1692. * (String) authorizationCode - oauth code.
  1693. * (String) redirectUrl - oauth redirect url.
  1694. * (String) tokenUrl - oauth token url.
  1695. * (String) accessToken - oauth access token.
  1696. * (String) refreshToken - oauth refresh token.
  1697. * (Number) expiration - unix time for access token expiration.
  1698. */
  1699. ;
  1700. exports.Account = Account;
  1701. var Credentials = function Credentials(options) {
  1702. _classCallCheck(this, Credentials);
  1703. Object.assign(this, {
  1704. username: null,
  1705. password: null,
  1706. clientId: null,
  1707. clientSecret: null,
  1708. authorizationCode: null,
  1709. redirectUrl: null,
  1710. tokenUrl: null,
  1711. accessToken: null,
  1712. refreshToken: null,
  1713. expiration: null
  1714. }, options);
  1715. };
  1716. exports.Credentials = Credentials;
  1717. var DAVCollection = function DAVCollection(options) {
  1718. _classCallCheck(this, DAVCollection);
  1719. Object.assign(this, {
  1720. data: null,
  1721. objects: null,
  1722. account: null,
  1723. ctag: null,
  1724. description: null,
  1725. displayName: null,
  1726. reports: null,
  1727. resourcetype: null,
  1728. syncToken: null,
  1729. url: null
  1730. }, options);
  1731. };
  1732. exports.DAVCollection = DAVCollection;
  1733. var AddressBook = (function (_DAVCollection) {
  1734. _inherits(AddressBook, _DAVCollection);
  1735. function AddressBook(options) {
  1736. _classCallCheck(this, AddressBook);
  1737. _get(Object.getPrototypeOf(AddressBook.prototype), "constructor", this).call(this, options);
  1738. }
  1739. return AddressBook;
  1740. })(DAVCollection);
  1741. exports.AddressBook = AddressBook;
  1742. var Calendar = (function (_DAVCollection2) {
  1743. _inherits(Calendar, _DAVCollection2);
  1744. function Calendar(options) {
  1745. _classCallCheck(this, Calendar);
  1746. _get(Object.getPrototypeOf(Calendar.prototype), "constructor", this).call(this, options);
  1747. Object.assign(this, {
  1748. components: null,
  1749. timezone: null
  1750. }, options);
  1751. }
  1752. return Calendar;
  1753. })(DAVCollection);
  1754. exports.Calendar = Calendar;
  1755. var DAVObject = function DAVObject(options) {
  1756. _classCallCheck(this, DAVObject);
  1757. Object.assign(this, {
  1758. data: null,
  1759. etag: null,
  1760. url: null
  1761. }, options);
  1762. };
  1763. exports.DAVObject = DAVObject;
  1764. var CalendarObject = (function (_DAVObject) {
  1765. _inherits(CalendarObject, _DAVObject);
  1766. function CalendarObject(options) {
  1767. _classCallCheck(this, CalendarObject);
  1768. _get(Object.getPrototypeOf(CalendarObject.prototype), "constructor", this).call(this, options);
  1769. Object.assign(this, {
  1770. calendar: null,
  1771. calendarData: null
  1772. }, options);
  1773. }
  1774. return CalendarObject;
  1775. })(DAVObject);
  1776. exports.CalendarObject = CalendarObject;
  1777. var VCard = (function (_DAVObject2) {
  1778. _inherits(VCard, _DAVObject2);
  1779. function VCard(options) {
  1780. _classCallCheck(this, VCard);
  1781. _get(Object.getPrototypeOf(VCard.prototype), "constructor", this).call(this, options);
  1782. Object.assign(this, {
  1783. addressBook: null,
  1784. addressData: null
  1785. }, options);
  1786. }
  1787. return VCard;
  1788. })(DAVObject);
  1789. exports.VCard = VCard;
  1790. },{}],10:[function(require,module,exports){
  1791. 'use strict';
  1792. Object.defineProperty(exports, '__esModule', {
  1793. value: true
  1794. });
  1795. var CALENDAR_SERVER = 'http://calendarserver.org/ns/';
  1796. exports.CALENDAR_SERVER = CALENDAR_SERVER;
  1797. var CALDAV_APPLE = 'http://apple.com/ns/ical/';
  1798. exports.CALDAV_APPLE = CALDAV_APPLE;
  1799. var CALDAV = 'urn:ietf:params:xml:ns:caldav';
  1800. exports.CALDAV = CALDAV;
  1801. var CARDDAV = 'urn:ietf:params:xml:ns:carddav';
  1802. exports.CARDDAV = CARDDAV;
  1803. var DAV = 'DAV:';
  1804. exports.DAV = DAV;
  1805. },{}],11:[function(require,module,exports){
  1806. 'use strict';
  1807. Object.defineProperty(exports, '__esModule', {
  1808. value: true
  1809. });
  1810. exports.multistatus = multistatus;
  1811. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  1812. var _camelize = require('./camelize');
  1813. var _camelize2 = _interopRequireDefault(_camelize);
  1814. var debug = require('./debug')('dav:parser');
  1815. var DOMParser = undefined;
  1816. if (typeof self !== 'undefined' && 'DOMParser' in self) {
  1817. // browser main thread
  1818. DOMParser = self.DOMParser;
  1819. } else {
  1820. // nodejs or web worker
  1821. DOMParser = require('xmldom').DOMParser;
  1822. }
  1823. function multistatus(string) {
  1824. var parser = new DOMParser();
  1825. var doc = parser.parseFromString(string, 'text/xml');
  1826. var result = traverse.multistatus(child(doc, 'multistatus'));
  1827. debug('input:\n' + string + '\noutput:\n' + JSON.stringify(result) + '\n');
  1828. return result;
  1829. }
  1830. var traverse = {
  1831. // { response: [x, y, z] }
  1832. multistatus: function multistatus(node) {
  1833. return complex(node, { response: true });
  1834. },
  1835. // { propstat: [x, y, z] }
  1836. response: function response(node) {
  1837. return complex(node, { propstat: true, href: false });
  1838. },
  1839. // { prop: x }
  1840. propstat: function propstat(node) {
  1841. return complex(node, { prop: false });
  1842. },
  1843. // {
  1844. // resourcetype: x
  1845. // supportedCalendarComponentSet: y,
  1846. // supportedReportSet: z
  1847. // }
  1848. prop: function prop(node) {
  1849. return complex(node, {
  1850. resourcetype: false,
  1851. supportedCalendarComponentSet: false,
  1852. supportedReportSet: false,
  1853. currentUserPrincipal: false
  1854. });
  1855. },
  1856. resourcetype: function resourcetype(node) {
  1857. return childNodes(node).map(function (childNode) {
  1858. return childNode.localName;
  1859. });
  1860. },
  1861. // [x, y, z]
  1862. supportedCalendarComponentSet: function supportedCalendarComponentSet(node) {
  1863. return complex(node, { comp: true }, 'comp');
  1864. },
  1865. // [x, y, z]
  1866. supportedReportSet: function supportedReportSet(node) {
  1867. return complex(node, { supportedReport: true }, 'supportedReport');
  1868. },
  1869. comp: function comp(node) {
  1870. return node.getAttribute('name');
  1871. },
  1872. // x
  1873. supportedReport: function supportedReport(node) {
  1874. return complex(node, { report: false }, 'report');
  1875. },
  1876. report: function report(node) {
  1877. return childNodes(node).map(function (childNode) {
  1878. return childNode.localName;
  1879. });
  1880. },
  1881. href: function href(node) {
  1882. return decodeURIComponent(childNodes(node)[0].nodeValue);
  1883. },
  1884. currentUserPrincipal: function currentUserPrincipal(node) {
  1885. return complex(node, { href: false }, 'href');
  1886. }
  1887. };
  1888. function complex(node, childspec, collapse) {
  1889. var result = {};
  1890. for (var key in childspec) {
  1891. if (childspec[key]) {
  1892. // Create array since we're expecting multiple.
  1893. result[key] = [];
  1894. }
  1895. }
  1896. childNodes(node).forEach(function (childNode) {
  1897. return traverseChild(node, childNode, childspec, result);
  1898. });
  1899. return maybeCollapse(result, childspec, collapse);
  1900. }
  1901. /**
  1902. * Parse child childNode of node with childspec and write outcome to result.
  1903. */
  1904. function traverseChild(node, childNode, childspec, result) {
  1905. if (childNode.nodeType === 3 && /^\s+$/.test(childNode.nodeValue)) {
  1906. // Whitespace... nothing to do.
  1907. return;
  1908. }
  1909. var localName = (0, _camelize2['default'])(childNode.localName, '-');
  1910. if (!(localName in childspec)) {
  1911. debug('Unexpected node of type ' + localName + ' encountered while ' + 'parsing ' + node.localName + ' node!');
  1912. var value = childNode.textContent;
  1913. if (localName in result) {
  1914. if (!Array.isArray(result[localName])) {
  1915. // Since we've already encountered this node type and we haven't yet
  1916. // made an array for it, make an array now.
  1917. result[localName] = [result[localName]];
  1918. }
  1919. result[localName].push(value);
  1920. return;
  1921. }
  1922. // First time we're encountering this node.
  1923. result[localName] = value;
  1924. return;
  1925. }
  1926. var traversal = traverse[localName](childNode);
  1927. if (childspec[localName]) {
  1928. // Expect multiple.
  1929. result[localName].push(traversal);
  1930. } else {
  1931. // Expect single.
  1932. result[localName] = traversal;
  1933. }
  1934. }
  1935. function maybeCollapse(result, childspec, collapse) {
  1936. if (!collapse) {
  1937. return result;
  1938. }
  1939. if (!childspec[collapse]) {
  1940. return result[collapse];
  1941. }
  1942. // Collapse array.
  1943. return result[collapse].reduce(function (a, b) {
  1944. return a.concat(b);
  1945. }, []);
  1946. }
  1947. function childNodes(node) {
  1948. var result = node.childNodes;
  1949. if (!Array.isArray(result)) {
  1950. result = Array.prototype.slice.call(result);
  1951. }
  1952. return result;
  1953. }
  1954. function children(node, localName) {
  1955. return childNodes(node).filter(function (childNode) {
  1956. return childNode.localName === localName;
  1957. });
  1958. }
  1959. function child(node, localName) {
  1960. return children(node, localName)[0];
  1961. }
  1962. },{"./camelize":3,"./debug":6,"xmldom":30}],12:[function(require,module,exports){
  1963. 'use strict';
  1964. Object.defineProperty(exports, '__esModule', {
  1965. value: true
  1966. });
  1967. exports.addressBookQuery = addressBookQuery;
  1968. exports.basic = basic;
  1969. exports.calendarQuery = calendarQuery;
  1970. exports.collectionQuery = collectionQuery;
  1971. exports.propfind = propfind;
  1972. exports.syncCollection = syncCollection;
  1973. exports.mergeProps = mergeProps;
  1974. exports.getProps = getProps;
  1975. exports.setRequestHeaders = setRequestHeaders;
  1976. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }
  1977. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
  1978. var _parser = require('./parser');
  1979. var _template = require('./template');
  1980. var template = _interopRequireWildcard(_template);
  1981. /**
  1982. * Options:
  1983. *
  1984. * (String) depth - optional value for Depth header.
  1985. * (Array.<Object>) props - list of props to request.
  1986. */
  1987. function addressBookQuery(options) {
  1988. return collectionQuery(template.addressBookQuery({ props: options.props || [] }), { depth: options.depth });
  1989. }
  1990. /**
  1991. * Options:
  1992. *
  1993. * (String) data - put request body.
  1994. * (String) method - http method.
  1995. * (String) etag - cached calendar object etag.
  1996. */
  1997. function basic(options) {
  1998. function transformRequest(xhr) {
  1999. setRequestHeaders(xhr, options);
  2000. }
  2001. return new Request({
  2002. method: options.method,
  2003. requestData: options.data,
  2004. transformRequest: transformRequest
  2005. });
  2006. }
  2007. /**
  2008. * Options:
  2009. *
  2010. * (String) depth - optional value for Depth header.
  2011. * (Array.<Object>) filters - list of filters to send with request.
  2012. * (Array.<Object>) props - list of props to request.
  2013. * (String) timezone - VTIMEZONE calendar object.
  2014. */
  2015. function calendarQuery(options) {
  2016. return collectionQuery(template.calendarQuery({
  2017. props: options.props || [],
  2018. filters: options.filters || [],
  2019. timezone: options.timezone
  2020. }), {
  2021. depth: options.depth
  2022. });
  2023. }
  2024. function collectionQuery(requestData, options) {
  2025. function transformRequest(xhr) {
  2026. setRequestHeaders(xhr, options);
  2027. }
  2028. function transformResponse(xhr) {
  2029. return (0, _parser.multistatus)(xhr.responseText).response.map(function (res) {
  2030. return { href: res.href, props: getProps(res.propstat) };
  2031. });
  2032. }
  2033. return new Request({
  2034. method: 'REPORT',
  2035. requestData: requestData,
  2036. transformRequest: transformRequest,
  2037. transformResponse: transformResponse
  2038. });
  2039. }
  2040. /**
  2041. * Options:
  2042. *
  2043. * (String) depth - optional value for Depth header.
  2044. * (Array.<Object>) props - list of props to request.
  2045. */
  2046. function propfind(options) {
  2047. var requestData = template.propfind({ props: options.props });
  2048. function transformRequest(xhr) {
  2049. setRequestHeaders(xhr, options);
  2050. }
  2051. function transformResponse(xhr) {
  2052. var responses = (0, _parser.multistatus)(xhr.responseText).response.map(function (res) {
  2053. return { href: res.href, props: getProps(res.propstat) };
  2054. });
  2055. if (!options.mergeResponses) {
  2056. return responses;
  2057. }
  2058. // Merge the props.
  2059. var merged = mergeProps(responses.map(function (res) {
  2060. return res.props;
  2061. }));
  2062. var hrefs = responses.map(function (res) {
  2063. return res.href;
  2064. });
  2065. return { props: merged, hrefs: hrefs };
  2066. }
  2067. return new Request({
  2068. method: 'PROPFIND',
  2069. requestData: requestData,
  2070. transformRequest: transformRequest,
  2071. transformResponse: transformResponse
  2072. });
  2073. }
  2074. /**
  2075. * Options:
  2076. *
  2077. * (String) depth - option value for Depth header.
  2078. * (Array.<Object>) props - list of props to request.
  2079. * (Number) syncLevel - indicates scope of the sync report request.
  2080. * (String) syncToken - synchronization token provided by the server.
  2081. */
  2082. function syncCollection(options) {
  2083. var requestData = template.syncCollection({
  2084. props: options.props,
  2085. syncLevel: options.syncLevel,
  2086. syncToken: options.syncToken
  2087. });
  2088. function transformRequest(xhr) {
  2089. setRequestHeaders(xhr, options);
  2090. }
  2091. function transformResponse(xhr) {
  2092. var object = (0, _parser.multistatus)(xhr.responseText);
  2093. var responses = object.response.map(function (res) {
  2094. return { href: res.href, props: getProps(res.propstat) };
  2095. });
  2096. return { responses: responses, syncToken: object.syncToken };
  2097. }
  2098. return new Request({
  2099. method: 'REPORT',
  2100. requestData: requestData,
  2101. transformRequest: transformRequest,
  2102. transformResponse: transformResponse
  2103. });
  2104. }
  2105. var Request = function Request() {
  2106. var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
  2107. _classCallCheck(this, Request);
  2108. Object.assign(this, {
  2109. method: null,
  2110. requestData: null,
  2111. transformRequest: null,
  2112. transformResponse: null,
  2113. onerror: null
  2114. }, options);
  2115. };
  2116. exports.Request = Request;
  2117. function getProp(propstat) {
  2118. if (/404/g.test(propstat.status)) {
  2119. return null;
  2120. }
  2121. if (/5\d{2}/g.test(propstat.status) || /4\d{2}/g.test(propstat.status)) {
  2122. throw new Error('Bad status on propstat: ' + propstat.status);
  2123. }
  2124. return 'prop' in propstat ? propstat.prop : null;
  2125. }
  2126. function mergeProps(props) {
  2127. return props.reduce(function (a, b) {
  2128. return Object.assign(a, b);
  2129. }, {});
  2130. }
  2131. /**
  2132. * Map propstats to props.
  2133. */
  2134. function getProps(propstats) {
  2135. return mergeProps(propstats.map(getProp).filter(function (prop) {
  2136. return prop && typeof prop === 'object';
  2137. }));
  2138. }
  2139. function setRequestHeaders(request, options) {
  2140. request.setRequestHeader('Content-Type', options.contentType || 'application/xml;charset=utf-8');
  2141. if ('depth' in options) {
  2142. request.setRequestHeader('Depth', options.depth);
  2143. }
  2144. if ('etag' in options && options.etag) {
  2145. request.setRequestHeader('If-Match', options.etag);
  2146. }
  2147. }
  2148. },{"./parser":11,"./template":17}],13:[function(require,module,exports){
  2149. /**
  2150. * @fileoverview Group requests together and then abort as a group.
  2151. *
  2152. * var sandbox = new dav.Sandbox();
  2153. * return Promise.all([
  2154. * dav.createEvent(event, { sandbox: sandbox }),
  2155. * dav.deleteEvent(other, { sandbox: sandbox })
  2156. * ])
  2157. * .catch(function() {
  2158. * // Something went wrong so abort all requests.
  2159. * sandbox.abort;
  2160. * });
  2161. */
  2162. 'use strict';
  2163. Object.defineProperty(exports, '__esModule', {
  2164. value: true
  2165. });
  2166. var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
  2167. exports.createSandbox = createSandbox;
  2168. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
  2169. var debug = require('./debug')('dav:sandbox');
  2170. var Sandbox = (function () {
  2171. function Sandbox() {
  2172. _classCallCheck(this, Sandbox);
  2173. this.requestList = [];
  2174. }
  2175. _createClass(Sandbox, [{
  2176. key: 'add',
  2177. value: function add(request) {
  2178. debug('Adding request to sandbox.');
  2179. this.requestList.push(request);
  2180. }
  2181. }, {
  2182. key: 'abort',
  2183. value: function abort() {
  2184. debug('Aborting sandboxed requests.');
  2185. this.requestList.forEach(function (request) {
  2186. return request.abort();
  2187. });
  2188. }
  2189. }]);
  2190. return Sandbox;
  2191. })();
  2192. exports.Sandbox = Sandbox;
  2193. function createSandbox() {
  2194. return new Sandbox();
  2195. }
  2196. },{"./debug":6}],14:[function(require,module,exports){
  2197. 'use strict';
  2198. Object.defineProperty(exports, '__esModule', {
  2199. value: true
  2200. });
  2201. exports['default'] = addressBookQuery;
  2202. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  2203. var _prop = require('./prop');
  2204. var _prop2 = _interopRequireDefault(_prop);
  2205. function addressBookQuery(object) {
  2206. return '<card:addressbook-query xmlns:card="urn:ietf:params:xml:ns:carddav"\n xmlns:d="DAV:">\n <d:prop>\n ' + object.props.map(_prop2['default']) + '\n </d:prop>\n <!-- According to http://stackoverflow.com/questions/23742568/google-carddav-api-addressbook-multiget-returns-400-bad-request,\n Google\'s CardDAV server requires a filter element. I don\'t think all addressbook-query calls need a filter in the spec though? -->\n <card:filter>\n <card:prop-filter name="FN">\n </card:prop-filter>\n </card:filter>\n </card:addressbook-query>';
  2207. }
  2208. module.exports = exports['default'];
  2209. },{"./prop":18}],15:[function(require,module,exports){
  2210. 'use strict';
  2211. Object.defineProperty(exports, '__esModule', {
  2212. value: true
  2213. });
  2214. exports['default'] = calendarQuery;
  2215. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  2216. var _filter = require('./filter');
  2217. var _filter2 = _interopRequireDefault(_filter);
  2218. var _prop = require('./prop');
  2219. var _prop2 = _interopRequireDefault(_prop);
  2220. function calendarQuery(object) {
  2221. return '<c:calendar-query xmlns:c="urn:ietf:params:xml:ns:caldav"\n xmlns:cs="http://calendarserver.org/ns/"\n xmlns:ca="http://apple.com/ns/ical/"\n xmlns:d="DAV:">\n <d:prop>\n ' + object.props.map(_prop2['default']) + '\n </d:prop>\n <c:filter>\n ' + object.filters.map(_filter2['default']) + '\n </c:filter>\n ' + (object.timezone ? '<c:timezone>' + object.timezone + '</c:timezone>' : '') + '\n </c:calendar-query>';
  2222. }
  2223. module.exports = exports['default'];
  2224. },{"./filter":16,"./prop":18}],16:[function(require,module,exports){
  2225. 'use strict';
  2226. Object.defineProperty(exports, '__esModule', {
  2227. value: true
  2228. });
  2229. exports['default'] = filter;
  2230. function filter(item) {
  2231. if (!item.children || !item.children.length) {
  2232. if (typeof item.value === 'undefined') {
  2233. return '<c:' + item.type + ' ' + formatAttrs(item.attrs) + '/>';
  2234. }
  2235. return '<c:' + item.type + ' ' + formatAttrs(item.attrs) + '>' + item.value + '</c:' + item.type + '>';
  2236. }
  2237. var children = item.children.map(filter);
  2238. return '<c:' + item.type + ' ' + formatAttrs(item.attrs) + '>\n ' + children + '\n </c:' + item.type + '>';
  2239. }
  2240. function formatAttrs(attrs) {
  2241. if (typeof attrs !== 'object') {
  2242. return '';
  2243. }
  2244. return Object.keys(attrs).map(function (attr) {
  2245. return attr + '="' + attrs[attr] + '"';
  2246. }).join(' ');
  2247. }
  2248. module.exports = exports['default'];
  2249. },{}],17:[function(require,module,exports){
  2250. 'use strict';
  2251. exports.addressBookQuery = require('./address_book_query');
  2252. exports.calendarQuery = require('./calendar_query');
  2253. exports.propfind = require('./propfind');
  2254. exports.syncCollection = require('./sync_collection');
  2255. },{"./address_book_query":14,"./calendar_query":15,"./propfind":19,"./sync_collection":20}],18:[function(require,module,exports){
  2256. 'use strict';
  2257. Object.defineProperty(exports, '__esModule', {
  2258. value: true
  2259. });
  2260. exports['default'] = prop;
  2261. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }
  2262. var _namespace = require('../namespace');
  2263. var ns = _interopRequireWildcard(_namespace);
  2264. /**
  2265. * @param {Object} filter looks like
  2266. *
  2267. * {
  2268. * type: 'comp-filter',
  2269. * attrs: {
  2270. * name: 'VCALENDAR'
  2271. * }
  2272. * }
  2273. *
  2274. * Or maybe
  2275. *
  2276. * {
  2277. * type: 'time-range',
  2278. * attrs: {
  2279. * start: '20060104T000000Z',
  2280. * end: '20060105T000000Z'
  2281. * }
  2282. * }
  2283. *
  2284. * You can nest them like so:
  2285. *
  2286. * {
  2287. * type: 'comp-filter',
  2288. * attrs: { name: 'VCALENDAR' },
  2289. * children: [{
  2290. * type: 'comp-filter',
  2291. * attrs: { name: 'VEVENT' },
  2292. * children: [{
  2293. * type: 'time-range',
  2294. * attrs: { start: '20060104T000000Z', end: '20060105T000000Z' }
  2295. * }]
  2296. * }]
  2297. * }
  2298. */
  2299. function prop(item) {
  2300. return '<' + xmlnsPrefix(item.namespace) + ':' + item.name + ' />';
  2301. }
  2302. function xmlnsPrefix(namespace) {
  2303. switch (namespace) {
  2304. case ns.DAV:
  2305. return 'd';
  2306. case ns.CALENDAR_SERVER:
  2307. return 'cs';
  2308. case ns.CALDAV_APPLE:
  2309. return 'ca';
  2310. case ns.CALDAV:
  2311. return 'c';
  2312. case ns.CARDDAV:
  2313. return 'card';
  2314. default:
  2315. throw new Error('Unrecognized xmlns ' + namespace);
  2316. }
  2317. }
  2318. module.exports = exports['default'];
  2319. },{"../namespace":10}],19:[function(require,module,exports){
  2320. 'use strict';
  2321. Object.defineProperty(exports, '__esModule', {
  2322. value: true
  2323. });
  2324. exports['default'] = propfind;
  2325. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  2326. var _prop = require('./prop');
  2327. var _prop2 = _interopRequireDefault(_prop);
  2328. function propfind(object) {
  2329. return '<d:propfind xmlns:c="urn:ietf:params:xml:ns:caldav"\n xmlns:card="urn:ietf:params:xml:ns:carddav"\n xmlns:cs="http://calendarserver.org/ns/"\n xmlns:ca="http://apple.com/ns/ical/"\n xmlns:d="DAV:">\n <d:prop>\n ' + object.props.map(_prop2['default']) + '\n </d:prop>\n </d:propfind>';
  2330. }
  2331. module.exports = exports['default'];
  2332. },{"./prop":18}],20:[function(require,module,exports){
  2333. 'use strict';
  2334. Object.defineProperty(exports, '__esModule', {
  2335. value: true
  2336. });
  2337. exports['default'] = syncCollection;
  2338. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  2339. var _prop = require('./prop');
  2340. var _prop2 = _interopRequireDefault(_prop);
  2341. function syncCollection(object) {
  2342. return '<d:sync-collection xmlns:c="urn:ietf:params:xml:ns:caldav"\n xmlns:card="urn:ietf:params:xml:ns:carddav"\n xmlns:d="DAV:">\n <d:sync-level>' + object.syncLevel + '</d:sync-level>\n <d:sync-token>' + object.syncToken + '</d:sync-token>\n <d:prop>\n ' + object.props.map(_prop2['default']) + '\n </d:prop>\n </d:sync-collection>';
  2343. }
  2344. module.exports = exports['default'];
  2345. },{"./prop":18}],21:[function(require,module,exports){
  2346. 'use strict';
  2347. Object.defineProperty(exports, '__esModule', {
  2348. value: true
  2349. });
  2350. var _get = function get(_x2, _x3, _x4) { var _again = true; _function: while (_again) { var object = _x2, property = _x3, receiver = _x4; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x2 = parent; _x3 = property; _x4 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
  2351. var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
  2352. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  2353. function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  2354. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
  2355. var _co = require('co');
  2356. var _co2 = _interopRequireDefault(_co);
  2357. var _querystring = require('querystring');
  2358. var _querystring2 = _interopRequireDefault(_querystring);
  2359. var _xmlhttprequest = require('./xmlhttprequest');
  2360. var _xmlhttprequest2 = _interopRequireDefault(_xmlhttprequest);
  2361. var Transport = (function () {
  2362. /**
  2363. * @param {dav.Credentials} credentials user authorization.
  2364. */
  2365. function Transport(credentials) {
  2366. _classCallCheck(this, Transport);
  2367. this.credentials = credentials || null;
  2368. }
  2369. /**
  2370. * @param {dav.Request} request object with request info.
  2371. * @return {Promise} a promise that will be resolved with an xhr request after
  2372. * its readyState is 4 or the result of applying an optional request
  2373. * `transformResponse` function to the xhr object after its readyState is 4.
  2374. *
  2375. * Options:
  2376. *
  2377. * (Object) sandbox - optional request sandbox.
  2378. */
  2379. _createClass(Transport, [{
  2380. key: 'send',
  2381. value: function send() {}
  2382. }]);
  2383. return Transport;
  2384. })();
  2385. exports.Transport = Transport;
  2386. var Basic = (function (_Transport) {
  2387. _inherits(Basic, _Transport);
  2388. /**
  2389. * @param {dav.Credentials} credentials user authorization.
  2390. */
  2391. function Basic(credentials) {
  2392. _classCallCheck(this, Basic);
  2393. _get(Object.getPrototypeOf(Basic.prototype), 'constructor', this).call(this, credentials);
  2394. }
  2395. /**
  2396. * @param {dav.Credentials} credentials user authorization.
  2397. */
  2398. _createClass(Basic, [{
  2399. key: 'send',
  2400. value: function send(request, url, options) {
  2401. return (0, _co2['default'])(regeneratorRuntime.mark(function callee$2$0() {
  2402. var sandbox, transformRequest, transformResponse, onerror, xhr, result;
  2403. return regeneratorRuntime.wrap(function callee$2$0$(context$3$0) {
  2404. while (1) switch (context$3$0.prev = context$3$0.next) {
  2405. case 0:
  2406. sandbox = options && options.sandbox;
  2407. transformRequest = request.transformRequest;
  2408. transformResponse = request.transformResponse;
  2409. onerror = request.onerror;
  2410. xhr = new _xmlhttprequest2['default']();
  2411. if (sandbox) sandbox.add(xhr);
  2412. xhr.open(request.method, url, true, /* async */
  2413. this.credentials.username, this.credentials.password);
  2414. if (transformRequest) transformRequest(xhr);
  2415. result = undefined;
  2416. context$3$0.prev = 9;
  2417. context$3$0.next = 12;
  2418. return xhr.send(request.requestData);
  2419. case 12:
  2420. result = transformResponse ? transformResponse(xhr) : xhr;
  2421. context$3$0.next = 19;
  2422. break;
  2423. case 15:
  2424. context$3$0.prev = 15;
  2425. context$3$0.t0 = context$3$0['catch'](9);
  2426. if (onerror) onerror(context$3$0.t0);
  2427. throw context$3$0.t0;
  2428. case 19:
  2429. return context$3$0.abrupt('return', result);
  2430. case 20:
  2431. case 'end':
  2432. return context$3$0.stop();
  2433. }
  2434. }, callee$2$0, this, [[9, 15]]);
  2435. }).bind(this));
  2436. }
  2437. }]);
  2438. return Basic;
  2439. })(Transport);
  2440. exports.Basic = Basic;
  2441. var OAuth2 = (function (_Transport2) {
  2442. _inherits(OAuth2, _Transport2);
  2443. function OAuth2(credentials) {
  2444. _classCallCheck(this, OAuth2);
  2445. _get(Object.getPrototypeOf(OAuth2.prototype), 'constructor', this).call(this, credentials);
  2446. }
  2447. /**
  2448. * @return {Promise} promise that will resolve with access token.
  2449. */
  2450. _createClass(OAuth2, [{
  2451. key: 'send',
  2452. value: function send(request, url) {
  2453. var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
  2454. return (0, _co2['default'])(regeneratorRuntime.mark(function callee$2$0() {
  2455. var sandbox, transformRequest, transformResponse, onerror, result, xhr, token;
  2456. return regeneratorRuntime.wrap(function callee$2$0$(context$3$0) {
  2457. while (1) switch (context$3$0.prev = context$3$0.next) {
  2458. case 0:
  2459. sandbox = options.sandbox;
  2460. transformRequest = request.transformRequest;
  2461. transformResponse = request.transformResponse;
  2462. onerror = request.onerror;
  2463. if (!('retry' in options)) options.retry = true;
  2464. result = undefined, xhr = undefined;
  2465. context$3$0.prev = 6;
  2466. context$3$0.next = 9;
  2467. return access(this.credentials, options);
  2468. case 9:
  2469. token = context$3$0.sent;
  2470. xhr = new _xmlhttprequest2['default']();
  2471. if (sandbox) sandbox.add(xhr);
  2472. xhr.open(request.method, url, true /* async */);
  2473. xhr.setRequestHeader('Authorization', 'Bearer ' + token);
  2474. if (transformRequest) transformRequest(xhr);
  2475. context$3$0.next = 17;
  2476. return xhr.send(request.requestData);
  2477. case 17:
  2478. result = transformResponse ? transformResponse(xhr) : xhr;
  2479. context$3$0.next = 28;
  2480. break;
  2481. case 20:
  2482. context$3$0.prev = 20;
  2483. context$3$0.t0 = context$3$0['catch'](6);
  2484. if (!(options.retry && xhr.status === 401)) {
  2485. context$3$0.next = 26;
  2486. break;
  2487. }
  2488. // Force expiration.
  2489. this.credentials.expiration = 0;
  2490. // Retry once at most.
  2491. options.retry = false;
  2492. return context$3$0.abrupt('return', this.send(request, url, options));
  2493. case 26:
  2494. if (onerror) onerror(context$3$0.t0);
  2495. throw context$3$0.t0;
  2496. case 28:
  2497. return context$3$0.abrupt('return', result);
  2498. case 29:
  2499. case 'end':
  2500. return context$3$0.stop();
  2501. }
  2502. }, callee$2$0, this, [[6, 20]]);
  2503. }).bind(this));
  2504. }
  2505. }]);
  2506. return OAuth2;
  2507. })(Transport);
  2508. exports.OAuth2 = OAuth2;
  2509. function access(credentials, options) {
  2510. if (!credentials.accessToken) {
  2511. return getAccessToken(credentials, options);
  2512. }
  2513. if (credentials.refreshToken && isExpired(credentials)) {
  2514. return refreshAccessToken(credentials, options);
  2515. }
  2516. return Promise.resolve(credentials.accessToken);
  2517. }
  2518. function isExpired(credentials) {
  2519. return typeof credentials.expiration === 'number' && Date.now() > credentials.expiration;
  2520. }
  2521. var getAccessToken = _co2['default'].wrap(regeneratorRuntime.mark(function callee$0$0(credentials, options) {
  2522. var sandbox, xhr, data, now, response;
  2523. return regeneratorRuntime.wrap(function callee$0$0$(context$1$0) {
  2524. while (1) switch (context$1$0.prev = context$1$0.next) {
  2525. case 0:
  2526. sandbox = options.sandbox;
  2527. xhr = new _xmlhttprequest2['default']();
  2528. if (sandbox) sandbox.add(xhr);
  2529. xhr.open('POST', credentials.tokenUrl, true /* async */);
  2530. xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  2531. data = _querystring2['default'].stringify({
  2532. code: credentials.authorizationCode,
  2533. redirect_uri: credentials.redirectUrl,
  2534. client_id: credentials.clientId,
  2535. client_secret: credentials.clientSecret,
  2536. grant_type: 'authorization_code'
  2537. });
  2538. now = Date.now();
  2539. context$1$0.next = 9;
  2540. return xhr.send(data);
  2541. case 9:
  2542. response = JSON.parse(xhr.responseText);
  2543. credentials.accessToken = response.access_token;
  2544. credentials.refreshToken = 'refresh_token' in response ? response.refresh_token : null;
  2545. credentials.expiration = 'expires_in' in response ? now + response.expires_in : null;
  2546. return context$1$0.abrupt('return', response.access_token);
  2547. case 14:
  2548. case 'end':
  2549. return context$1$0.stop();
  2550. }
  2551. }, callee$0$0, this);
  2552. }));
  2553. var refreshAccessToken = _co2['default'].wrap(regeneratorRuntime.mark(function callee$0$0(credentials, options) {
  2554. var sandbox, xhr, data, now, response;
  2555. return regeneratorRuntime.wrap(function callee$0$0$(context$1$0) {
  2556. while (1) switch (context$1$0.prev = context$1$0.next) {
  2557. case 0:
  2558. sandbox = options.sandbox;
  2559. xhr = new _xmlhttprequest2['default']();
  2560. if (sandbox) sandbox.add(xhr);
  2561. xhr.open('POST', credentials.tokenUrl, true /* async */);
  2562. xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  2563. data = _querystring2['default'].stringify({
  2564. client_id: credentials.clientId,
  2565. client_secret: credentials.clientSecret,
  2566. refresh_token: credentials.refreshToken,
  2567. grant_type: 'refresh_token'
  2568. });
  2569. now = Date.now();
  2570. context$1$0.next = 9;
  2571. return xhr.send(data);
  2572. case 9:
  2573. response = JSON.parse(xhr.responseText);
  2574. credentials.accessToken = response.access_token;
  2575. credentials.expiration = 'expires_in' in response ? now + response.expires_in : null;
  2576. return context$1$0.abrupt('return', response.access_token);
  2577. case 13:
  2578. case 'end':
  2579. return context$1$0.stop();
  2580. }
  2581. }, callee$0$0, this);
  2582. }));
  2583. },{"./xmlhttprequest":23,"co":24,"querystring":28}],22:[function(require,module,exports){
  2584. 'use strict';
  2585. Object.defineProperty(exports, '__esModule', {
  2586. value: true
  2587. });
  2588. exports.createObject = createObject;
  2589. exports.updateObject = updateObject;
  2590. exports.deleteObject = deleteObject;
  2591. exports.syncCollection = syncCollection;
  2592. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }
  2593. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  2594. var _co = require('co');
  2595. var _co2 = _interopRequireDefault(_co);
  2596. var _fuzzy_url_equals = require('./fuzzy_url_equals');
  2597. var _fuzzy_url_equals2 = _interopRequireDefault(_fuzzy_url_equals);
  2598. var _namespace = require('./namespace');
  2599. var ns = _interopRequireWildcard(_namespace);
  2600. var _request = require('./request');
  2601. var request = _interopRequireWildcard(_request);
  2602. var debug = require('./debug')('dav:webdav');
  2603. /**
  2604. * @param {String} objectUrl url for webdav object.
  2605. * @param {String} objectData webdav object data.
  2606. */
  2607. function createObject(objectUrl, objectData, options) {
  2608. var req = request.basic({ method: 'PUT', data: objectData, contentType: options.contentType });
  2609. return options.xhr.send(req, objectUrl, { sandbox: options.sandbox });
  2610. }
  2611. function updateObject(objectUrl, objectData, etag, options) {
  2612. var req = request.basic({ method: 'PUT', data: objectData, etag: etag, contentType: options.contentType });
  2613. return options.xhr.send(req, objectUrl, { sandbox: options.sandbox });
  2614. }
  2615. function deleteObject(objectUrl, etag, options) {
  2616. var req = request.basic({ method: 'DELETE', etag: etag });
  2617. return options.xhr.send(req, objectUrl, { sandbox: options.sandbox });
  2618. }
  2619. function syncCollection(collection, options) {
  2620. var syncMethod = undefined;
  2621. if ('syncMethod' in options) {
  2622. syncMethod = options.syncMethod;
  2623. } else if (collection.reports && collection.reports.indexOf('syncCollection') !== -1) {
  2624. syncMethod = 'webdav';
  2625. } else {
  2626. syncMethod = 'basic';
  2627. }
  2628. if (syncMethod === 'webdav') {
  2629. debug('rfc 6578 sync.');
  2630. return options.webdavSync(collection, options);
  2631. } else {
  2632. debug('basic sync.');
  2633. return options.basicSync(collection, options);
  2634. }
  2635. }
  2636. /**
  2637. * @param {dav.DAVCollection} collection to fetch report set for.
  2638. */
  2639. var supportedReportSet = _co2['default'].wrap(regeneratorRuntime.mark(function callee$0$0(collection, options) {
  2640. var req, response;
  2641. return regeneratorRuntime.wrap(function callee$0$0$(context$1$0) {
  2642. while (1) switch (context$1$0.prev = context$1$0.next) {
  2643. case 0:
  2644. debug('Checking supported report set for collection at ' + collection.url);
  2645. req = request.propfind({
  2646. props: [{ name: 'supported-report-set', namespace: ns.DAV }],
  2647. depth: 1,
  2648. mergeResponses: true
  2649. });
  2650. context$1$0.next = 4;
  2651. return options.xhr.send(req, collection.url, {
  2652. sandbox: options.sandbox
  2653. });
  2654. case 4:
  2655. response = context$1$0.sent;
  2656. return context$1$0.abrupt('return', response.props.supportedReportSet);
  2657. case 6:
  2658. case 'end':
  2659. return context$1$0.stop();
  2660. }
  2661. }, callee$0$0, this);
  2662. }));
  2663. exports.supportedReportSet = supportedReportSet;
  2664. var isCollectionDirty = _co2['default'].wrap(regeneratorRuntime.mark(function callee$0$0(collection, options) {
  2665. var req, responses, response;
  2666. return regeneratorRuntime.wrap(function callee$0$0$(context$1$0) {
  2667. while (1) switch (context$1$0.prev = context$1$0.next) {
  2668. case 0:
  2669. if (collection.ctag) {
  2670. context$1$0.next = 3;
  2671. break;
  2672. }
  2673. debug('Missing ctag.');
  2674. return context$1$0.abrupt('return', false);
  2675. case 3:
  2676. debug('Fetch remote getctag prop.');
  2677. req = request.propfind({
  2678. props: [{ name: 'getctag', namespace: ns.CALENDAR_SERVER }],
  2679. depth: 0
  2680. });
  2681. context$1$0.next = 7;
  2682. return options.xhr.send(req, collection.account.homeUrl, {
  2683. sandbox: options.sandbox
  2684. });
  2685. case 7:
  2686. responses = context$1$0.sent;
  2687. response = responses.filter(function (response) {
  2688. // Find the response that corresponds to the parameter collection.
  2689. return (0, _fuzzy_url_equals2['default'])(collection.url, response.href);
  2690. })[0];
  2691. if (response) {
  2692. context$1$0.next = 11;
  2693. break;
  2694. }
  2695. throw new Error('Could not find collection on remote. Was it deleted?');
  2696. case 11:
  2697. debug('Check whether cached ctag matches remote.');
  2698. return context$1$0.abrupt('return', collection.ctag !== response.props.getctag);
  2699. case 13:
  2700. case 'end':
  2701. return context$1$0.stop();
  2702. }
  2703. }, callee$0$0, this);
  2704. }));
  2705. exports.isCollectionDirty = isCollectionDirty;
  2706. },{"./debug":6,"./fuzzy_url_equals":7,"./namespace":10,"./request":12,"co":24}],23:[function(require,module,exports){
  2707. 'use strict';
  2708. Object.defineProperty(exports, '__esModule', {
  2709. value: true
  2710. });
  2711. var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
  2712. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
  2713. var debug = require('./debug')('dav:xmlhttprequest');
  2714. var Native = undefined;
  2715. if (typeof self !== 'undefined' && 'XMLHttpRequest' in self) {
  2716. Native = self.XMLHttpRequest;
  2717. } else {
  2718. // Trick browserify into not loading XMLHttpRequest polyfill
  2719. // since it is available in the platform (including web workers)
  2720. Native = require(false || 'xmlhttprequest').XMLHttpRequest;
  2721. }
  2722. /**
  2723. * @fileoverview Promise wrapper around native xhr api.
  2724. */
  2725. var XMLHttpRequest = (function () {
  2726. function XMLHttpRequest(options) {
  2727. var _this = this;
  2728. _classCallCheck(this, XMLHttpRequest);
  2729. this.request = new Native(options);
  2730. this.sandbox = null;
  2731. /* readwrite */
  2732. ['response', 'responseText', 'responseType', 'responseXML', 'timeout', 'upload', 'withCredentials'].forEach(function (attribute) {
  2733. Object.defineProperty(_this, attribute, {
  2734. get: function get() {
  2735. return this.request[attribute];
  2736. },
  2737. set: function set(value) {
  2738. this.request[attribute] = value;
  2739. }
  2740. });
  2741. });
  2742. /* readonly */
  2743. ['status', 'statusText'].forEach(function (attribute) {
  2744. Object.defineProperty(_this, attribute, {
  2745. get: function get() {
  2746. return this.request[attribute];
  2747. }
  2748. });
  2749. });
  2750. }
  2751. _createClass(XMLHttpRequest, [{
  2752. key: 'abort',
  2753. value: function abort() {
  2754. return this._callNative('abort', arguments);
  2755. }
  2756. }, {
  2757. key: 'getAllResponseHeaders',
  2758. value: function getAllResponseHeaders() {
  2759. return this._callNative('getAllResponseHeaders', arguments);
  2760. }
  2761. }, {
  2762. key: 'getResponseHeader',
  2763. value: function getResponseHeader() {
  2764. return this._callNative('getResponseHeader', arguments);
  2765. }
  2766. }, {
  2767. key: 'open',
  2768. value: function open() {
  2769. return this._callNative('open', arguments);
  2770. }
  2771. }, {
  2772. key: 'overrideMimeType',
  2773. value: function overrideMimeType() {
  2774. return this._callNative('overrideMimeType', arguments);
  2775. }
  2776. }, {
  2777. key: 'setRequestHeader',
  2778. value: function setRequestHeader() {
  2779. return this._callNative('setRequestHeader', arguments);
  2780. }
  2781. }, {
  2782. key: 'send',
  2783. value: function send(data) {
  2784. debug('Sending request data: ' + data);
  2785. if (this.sandbox) this.sandbox.add(this);
  2786. var request = this.request;
  2787. request.send(data);
  2788. return new Promise(function (resolve, reject) {
  2789. request.onreadystatechange = function () {
  2790. if (request.readyState !== 4 /* done */) {
  2791. return;
  2792. }
  2793. if (request.status < 200 || request.status >= 400) {
  2794. return reject(new Error('Bad status: ' + request.status));
  2795. }
  2796. return resolve(request.responseText);
  2797. };
  2798. request.ontimeout = function () {
  2799. reject(new Error('Request timed out after ' + request.timeout + ' ms'));
  2800. };
  2801. });
  2802. }
  2803. }, {
  2804. key: '_callNative',
  2805. value: function _callNative(method, args) {
  2806. return this.request[method].apply(this.request, args);
  2807. }
  2808. }]);
  2809. return XMLHttpRequest;
  2810. })();
  2811. exports['default'] = XMLHttpRequest;
  2812. module.exports = exports['default'];
  2813. },{"./debug":6}],24:[function(require,module,exports){
  2814. /**
  2815. * slice() reference.
  2816. */
  2817. var slice = Array.prototype.slice;
  2818. /**
  2819. * Expose `co`.
  2820. */
  2821. module.exports = co['default'] = co.co = co;
  2822. /**
  2823. * Wrap the given generator `fn` into a
  2824. * function that returns a promise.
  2825. * This is a separate function so that
  2826. * every `co()` call doesn't create a new,
  2827. * unnecessary closure.
  2828. *
  2829. * @param {GeneratorFunction} fn
  2830. * @return {Function}
  2831. * @api public
  2832. */
  2833. co.wrap = function (fn) {
  2834. createPromise.__generatorFunction__ = fn;
  2835. return createPromise;
  2836. function createPromise() {
  2837. return co.call(this, fn.apply(this, arguments));
  2838. }
  2839. };
  2840. /**
  2841. * Execute the generator function or a generator
  2842. * and return a promise.
  2843. *
  2844. * @param {Function} fn
  2845. * @return {Promise}
  2846. * @api public
  2847. */
  2848. function co(gen) {
  2849. var ctx = this;
  2850. var args = slice.call(arguments, 1)
  2851. // we wrap everything in a promise to avoid promise chaining,
  2852. // which leads to memory leak errors.
  2853. // see https://github.com/tj/co/issues/180
  2854. return new Promise(function(resolve, reject) {
  2855. if (typeof gen === 'function') gen = gen.apply(ctx, args);
  2856. if (!gen || typeof gen.next !== 'function') return resolve(gen);
  2857. onFulfilled();
  2858. /**
  2859. * @param {Mixed} res
  2860. * @return {Promise}
  2861. * @api private
  2862. */
  2863. function onFulfilled(res) {
  2864. var ret;
  2865. try {
  2866. ret = gen.next(res);
  2867. } catch (e) {
  2868. return reject(e);
  2869. }
  2870. next(ret);
  2871. }
  2872. /**
  2873. * @param {Error} err
  2874. * @return {Promise}
  2875. * @api private
  2876. */
  2877. function onRejected(err) {
  2878. var ret;
  2879. try {
  2880. ret = gen.throw(err);
  2881. } catch (e) {
  2882. return reject(e);
  2883. }
  2884. next(ret);
  2885. }
  2886. /**
  2887. * Get the next value in the generator,
  2888. * return a promise.
  2889. *
  2890. * @param {Object} ret
  2891. * @return {Promise}
  2892. * @api private
  2893. */
  2894. function next(ret) {
  2895. if (ret.done) return resolve(ret.value);
  2896. var value = toPromise.call(ctx, ret.value);
  2897. if (value && isPromise(value)) return value.then(onFulfilled, onRejected);
  2898. return onRejected(new TypeError('You may only yield a function, promise, generator, array, or object, '
  2899. + 'but the following object was passed: "' + String(ret.value) + '"'));
  2900. }
  2901. });
  2902. }
  2903. /**
  2904. * Convert a `yield`ed value into a promise.
  2905. *
  2906. * @param {Mixed} obj
  2907. * @return {Promise}
  2908. * @api private
  2909. */
  2910. function toPromise(obj) {
  2911. if (!obj) return obj;
  2912. if (isPromise(obj)) return obj;
  2913. if (isGeneratorFunction(obj) || isGenerator(obj)) return co.call(this, obj);
  2914. if ('function' == typeof obj) return thunkToPromise.call(this, obj);
  2915. if (Array.isArray(obj)) return arrayToPromise.call(this, obj);
  2916. if (isObject(obj)) return objectToPromise.call(this, obj);
  2917. return obj;
  2918. }
  2919. /**
  2920. * Convert a thunk to a promise.
  2921. *
  2922. * @param {Function}
  2923. * @return {Promise}
  2924. * @api private
  2925. */
  2926. function thunkToPromise(fn) {
  2927. var ctx = this;
  2928. return new Promise(function (resolve, reject) {
  2929. fn.call(ctx, function (err, res) {
  2930. if (err) return reject(err);
  2931. if (arguments.length > 2) res = slice.call(arguments, 1);
  2932. resolve(res);
  2933. });
  2934. });
  2935. }
  2936. /**
  2937. * Convert an array of "yieldables" to a promise.
  2938. * Uses `Promise.all()` internally.
  2939. *
  2940. * @param {Array} obj
  2941. * @return {Promise}
  2942. * @api private
  2943. */
  2944. function arrayToPromise(obj) {
  2945. return Promise.all(obj.map(toPromise, this));
  2946. }
  2947. /**
  2948. * Convert an object of "yieldables" to a promise.
  2949. * Uses `Promise.all()` internally.
  2950. *
  2951. * @param {Object} obj
  2952. * @return {Promise}
  2953. * @api private
  2954. */
  2955. function objectToPromise(obj){
  2956. var results = new obj.constructor();
  2957. var keys = Object.keys(obj);
  2958. var promises = [];
  2959. for (var i = 0; i < keys.length; i++) {
  2960. var key = keys[i];
  2961. var promise = toPromise.call(this, obj[key]);
  2962. if (promise && isPromise(promise)) defer(promise, key);
  2963. else results[key] = obj[key];
  2964. }
  2965. return Promise.all(promises).then(function () {
  2966. return results;
  2967. });
  2968. function defer(promise, key) {
  2969. // predefine the key in the result
  2970. results[key] = undefined;
  2971. promises.push(promise.then(function (res) {
  2972. results[key] = res;
  2973. }));
  2974. }
  2975. }
  2976. /**
  2977. * Check if `obj` is a promise.
  2978. *
  2979. * @param {Object} obj
  2980. * @return {Boolean}
  2981. * @api private
  2982. */
  2983. function isPromise(obj) {
  2984. return 'function' == typeof obj.then;
  2985. }
  2986. /**
  2987. * Check if `obj` is a generator.
  2988. *
  2989. * @param {Mixed} obj
  2990. * @return {Boolean}
  2991. * @api private
  2992. */
  2993. function isGenerator(obj) {
  2994. return 'function' == typeof obj.next && 'function' == typeof obj.throw;
  2995. }
  2996. /**
  2997. * Check if `obj` is a generator function.
  2998. *
  2999. * @param {Mixed} obj
  3000. * @return {Boolean}
  3001. * @api private
  3002. */
  3003. function isGeneratorFunction(obj) {
  3004. var constructor = obj.constructor;
  3005. if (!constructor) return false;
  3006. if ('GeneratorFunction' === constructor.name || 'GeneratorFunction' === constructor.displayName) return true;
  3007. return isGenerator(constructor.prototype);
  3008. }
  3009. /**
  3010. * Check for plain object.
  3011. *
  3012. * @param {Mixed} val
  3013. * @return {Boolean}
  3014. * @api private
  3015. */
  3016. function isObject(val) {
  3017. return Object == val.constructor;
  3018. }
  3019. },{}],25:[function(require,module,exports){
  3020. (function (global){
  3021. /*! https://mths.be/punycode v1.4.1 by @mathias */
  3022. ;(function(root) {
  3023. /** Detect free variables */
  3024. var freeExports = typeof exports == 'object' && exports &&
  3025. !exports.nodeType && exports;
  3026. var freeModule = typeof module == 'object' && module &&
  3027. !module.nodeType && module;
  3028. var freeGlobal = typeof global == 'object' && global;
  3029. if (
  3030. freeGlobal.global === freeGlobal ||
  3031. freeGlobal.window === freeGlobal ||
  3032. freeGlobal.self === freeGlobal
  3033. ) {
  3034. root = freeGlobal;
  3035. }
  3036. /**
  3037. * The `punycode` object.
  3038. * @name punycode
  3039. * @type Object
  3040. */
  3041. var punycode,
  3042. /** Highest positive signed 32-bit float value */
  3043. maxInt = 2147483647, // aka. 0x7FFFFFFF or 2^31-1
  3044. /** Bootstring parameters */
  3045. base = 36,
  3046. tMin = 1,
  3047. tMax = 26,
  3048. skew = 38,
  3049. damp = 700,
  3050. initialBias = 72,
  3051. initialN = 128, // 0x80
  3052. delimiter = '-', // '\x2D'
  3053. /** Regular expressions */
  3054. regexPunycode = /^xn--/,
  3055. regexNonASCII = /[^\x20-\x7E]/, // unprintable ASCII chars + non-ASCII chars
  3056. regexSeparators = /[\x2E\u3002\uFF0E\uFF61]/g, // RFC 3490 separators
  3057. /** Error messages */
  3058. errors = {
  3059. 'overflow': 'Overflow: input needs wider integers to process',
  3060. 'not-basic': 'Illegal input >= 0x80 (not a basic code point)',
  3061. 'invalid-input': 'Invalid input'
  3062. },
  3063. /** Convenience shortcuts */
  3064. baseMinusTMin = base - tMin,
  3065. floor = Math.floor,
  3066. stringFromCharCode = String.fromCharCode,
  3067. /** Temporary variable */
  3068. key;
  3069. /*--------------------------------------------------------------------------*/
  3070. /**
  3071. * A generic error utility function.
  3072. * @private
  3073. * @param {String} type The error type.
  3074. * @returns {Error} Throws a `RangeError` with the applicable error message.
  3075. */
  3076. function error(type) {
  3077. throw new RangeError(errors[type]);
  3078. }
  3079. /**
  3080. * A generic `Array#map` utility function.
  3081. * @private
  3082. * @param {Array} array The array to iterate over.
  3083. * @param {Function} callback The function that gets called for every array
  3084. * item.
  3085. * @returns {Array} A new array of values returned by the callback function.
  3086. */
  3087. function map(array, fn) {
  3088. var length = array.length;
  3089. var result = [];
  3090. while (length--) {
  3091. result[length] = fn(array[length]);
  3092. }
  3093. return result;
  3094. }
  3095. /**
  3096. * A simple `Array#map`-like wrapper to work with domain name strings or email
  3097. * addresses.
  3098. * @private
  3099. * @param {String} domain The domain name or email address.
  3100. * @param {Function} callback The function that gets called for every
  3101. * character.
  3102. * @returns {Array} A new string of characters returned by the callback
  3103. * function.
  3104. */
  3105. function mapDomain(string, fn) {
  3106. var parts = string.split('@');
  3107. var result = '';
  3108. if (parts.length > 1) {
  3109. // In email addresses, only the domain name should be punycoded. Leave
  3110. // the local part (i.e. everything up to `@`) intact.
  3111. result = parts[0] + '@';
  3112. string = parts[1];
  3113. }
  3114. // Avoid `split(regex)` for IE8 compatibility. See #17.
  3115. string = string.replace(regexSeparators, '\x2E');
  3116. var labels = string.split('.');
  3117. var encoded = map(labels, fn).join('.');
  3118. return result + encoded;
  3119. }
  3120. /**
  3121. * Creates an array containing the numeric code points of each Unicode
  3122. * character in the string. While JavaScript uses UCS-2 internally,
  3123. * this function will convert a pair of surrogate halves (each of which
  3124. * UCS-2 exposes as separate characters) into a single code point,
  3125. * matching UTF-16.
  3126. * @see `punycode.ucs2.encode`
  3127. * @see <https://mathiasbynens.be/notes/javascript-encoding>
  3128. * @memberOf punycode.ucs2
  3129. * @name decode
  3130. * @param {String} string The Unicode input string (UCS-2).
  3131. * @returns {Array} The new array of code points.
  3132. */
  3133. function ucs2decode(string) {
  3134. var output = [],
  3135. counter = 0,
  3136. length = string.length,
  3137. value,
  3138. extra;
  3139. while (counter < length) {
  3140. value = string.charCodeAt(counter++);
  3141. if (value >= 0xD800 && value <= 0xDBFF && counter < length) {
  3142. // high surrogate, and there is a next character
  3143. extra = string.charCodeAt(counter++);
  3144. if ((extra & 0xFC00) == 0xDC00) { // low surrogate
  3145. output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000);
  3146. } else {
  3147. // unmatched surrogate; only append this code unit, in case the next
  3148. // code unit is the high surrogate of a surrogate pair
  3149. output.push(value);
  3150. counter--;
  3151. }
  3152. } else {
  3153. output.push(value);
  3154. }
  3155. }
  3156. return output;
  3157. }
  3158. /**
  3159. * Creates a string based on an array of numeric code points.
  3160. * @see `punycode.ucs2.decode`
  3161. * @memberOf punycode.ucs2
  3162. * @name encode
  3163. * @param {Array} codePoints The array of numeric code points.
  3164. * @returns {String} The new Unicode string (UCS-2).
  3165. */
  3166. function ucs2encode(array) {
  3167. return map(array, function(value) {
  3168. var output = '';
  3169. if (value > 0xFFFF) {
  3170. value -= 0x10000;
  3171. output += stringFromCharCode(value >>> 10 & 0x3FF | 0xD800);
  3172. value = 0xDC00 | value & 0x3FF;
  3173. }
  3174. output += stringFromCharCode(value);
  3175. return output;
  3176. }).join('');
  3177. }
  3178. /**
  3179. * Converts a basic code point into a digit/integer.
  3180. * @see `digitToBasic()`
  3181. * @private
  3182. * @param {Number} codePoint The basic numeric code point value.
  3183. * @returns {Number} The numeric value of a basic code point (for use in
  3184. * representing integers) in the range `0` to `base - 1`, or `base` if
  3185. * the code point does not represent a value.
  3186. */
  3187. function basicToDigit(codePoint) {
  3188. if (codePoint - 48 < 10) {
  3189. return codePoint - 22;
  3190. }
  3191. if (codePoint - 65 < 26) {
  3192. return codePoint - 65;
  3193. }
  3194. if (codePoint - 97 < 26) {
  3195. return codePoint - 97;
  3196. }
  3197. return base;
  3198. }
  3199. /**
  3200. * Converts a digit/integer into a basic code point.
  3201. * @see `basicToDigit()`
  3202. * @private
  3203. * @param {Number} digit The numeric value of a basic code point.
  3204. * @returns {Number} The basic code point whose value (when used for
  3205. * representing integers) is `digit`, which needs to be in the range
  3206. * `0` to `base - 1`. If `flag` is non-zero, the uppercase form is
  3207. * used; else, the lowercase form is used. The behavior is undefined
  3208. * if `flag` is non-zero and `digit` has no uppercase form.
  3209. */
  3210. function digitToBasic(digit, flag) {
  3211. // 0..25 map to ASCII a..z or A..Z
  3212. // 26..35 map to ASCII 0..9
  3213. return digit + 22 + 75 * (digit < 26) - ((flag != 0) << 5);
  3214. }
  3215. /**
  3216. * Bias adaptation function as per section 3.4 of RFC 3492.
  3217. * https://tools.ietf.org/html/rfc3492#section-3.4
  3218. * @private
  3219. */
  3220. function adapt(delta, numPoints, firstTime) {
  3221. var k = 0;
  3222. delta = firstTime ? floor(delta / damp) : delta >> 1;
  3223. delta += floor(delta / numPoints);
  3224. for (/* no initialization */; delta > baseMinusTMin * tMax >> 1; k += base) {
  3225. delta = floor(delta / baseMinusTMin);
  3226. }
  3227. return floor(k + (baseMinusTMin + 1) * delta / (delta + skew));
  3228. }
  3229. /**
  3230. * Converts a Punycode string of ASCII-only symbols to a string of Unicode
  3231. * symbols.
  3232. * @memberOf punycode
  3233. * @param {String} input The Punycode string of ASCII-only symbols.
  3234. * @returns {String} The resulting string of Unicode symbols.
  3235. */
  3236. function decode(input) {
  3237. // Don't use UCS-2
  3238. var output = [],
  3239. inputLength = input.length,
  3240. out,
  3241. i = 0,
  3242. n = initialN,
  3243. bias = initialBias,
  3244. basic,
  3245. j,
  3246. index,
  3247. oldi,
  3248. w,
  3249. k,
  3250. digit,
  3251. t,
  3252. /** Cached calculation results */
  3253. baseMinusT;
  3254. // Handle the basic code points: let `basic` be the number of input code
  3255. // points before the last delimiter, or `0` if there is none, then copy
  3256. // the first basic code points to the output.
  3257. basic = input.lastIndexOf(delimiter);
  3258. if (basic < 0) {
  3259. basic = 0;
  3260. }
  3261. for (j = 0; j < basic; ++j) {
  3262. // if it's not a basic code point
  3263. if (input.charCodeAt(j) >= 0x80) {
  3264. error('not-basic');
  3265. }
  3266. output.push(input.charCodeAt(j));
  3267. }
  3268. // Main decoding loop: start just after the last delimiter if any basic code
  3269. // points were copied; start at the beginning otherwise.
  3270. for (index = basic > 0 ? basic + 1 : 0; index < inputLength; /* no final expression */) {
  3271. // `index` is the index of the next character to be consumed.
  3272. // Decode a generalized variable-length integer into `delta`,
  3273. // which gets added to `i`. The overflow checking is easier
  3274. // if we increase `i` as we go, then subtract off its starting
  3275. // value at the end to obtain `delta`.
  3276. for (oldi = i, w = 1, k = base; /* no condition */; k += base) {
  3277. if (index >= inputLength) {
  3278. error('invalid-input');
  3279. }
  3280. digit = basicToDigit(input.charCodeAt(index++));
  3281. if (digit >= base || digit > floor((maxInt - i) / w)) {
  3282. error('overflow');
  3283. }
  3284. i += digit * w;
  3285. t = k <= bias ? tMin : (k >= bias + tMax ? tMax : k - bias);
  3286. if (digit < t) {
  3287. break;
  3288. }
  3289. baseMinusT = base - t;
  3290. if (w > floor(maxInt / baseMinusT)) {
  3291. error('overflow');
  3292. }
  3293. w *= baseMinusT;
  3294. }
  3295. out = output.length + 1;
  3296. bias = adapt(i - oldi, out, oldi == 0);
  3297. // `i` was supposed to wrap around from `out` to `0`,
  3298. // incrementing `n` each time, so we'll fix that now:
  3299. if (floor(i / out) > maxInt - n) {
  3300. error('overflow');
  3301. }
  3302. n += floor(i / out);
  3303. i %= out;
  3304. // Insert `n` at position `i` of the output
  3305. output.splice(i++, 0, n);
  3306. }
  3307. return ucs2encode(output);
  3308. }
  3309. /**
  3310. * Converts a string of Unicode symbols (e.g. a domain name label) to a
  3311. * Punycode string of ASCII-only symbols.
  3312. * @memberOf punycode
  3313. * @param {String} input The string of Unicode symbols.
  3314. * @returns {String} The resulting Punycode string of ASCII-only symbols.
  3315. */
  3316. function encode(input) {
  3317. var n,
  3318. delta,
  3319. handledCPCount,
  3320. basicLength,
  3321. bias,
  3322. j,
  3323. m,
  3324. q,
  3325. k,
  3326. t,
  3327. currentValue,
  3328. output = [],
  3329. /** `inputLength` will hold the number of code points in `input`. */
  3330. inputLength,
  3331. /** Cached calculation results */
  3332. handledCPCountPlusOne,
  3333. baseMinusT,
  3334. qMinusT;
  3335. // Convert the input in UCS-2 to Unicode
  3336. input = ucs2decode(input);
  3337. // Cache the length
  3338. inputLength = input.length;
  3339. // Initialize the state
  3340. n = initialN;
  3341. delta = 0;
  3342. bias = initialBias;
  3343. // Handle the basic code points
  3344. for (j = 0; j < inputLength; ++j) {
  3345. currentValue = input[j];
  3346. if (currentValue < 0x80) {
  3347. output.push(stringFromCharCode(currentValue));
  3348. }
  3349. }
  3350. handledCPCount = basicLength = output.length;
  3351. // `handledCPCount` is the number of code points that have been handled;
  3352. // `basicLength` is the number of basic code points.
  3353. // Finish the basic string - if it is not empty - with a delimiter
  3354. if (basicLength) {
  3355. output.push(delimiter);
  3356. }
  3357. // Main encoding loop:
  3358. while (handledCPCount < inputLength) {
  3359. // All non-basic code points < n have been handled already. Find the next
  3360. // larger one:
  3361. for (m = maxInt, j = 0; j < inputLength; ++j) {
  3362. currentValue = input[j];
  3363. if (currentValue >= n && currentValue < m) {
  3364. m = currentValue;
  3365. }
  3366. }
  3367. // Increase `delta` enough to advance the decoder's <n,i> state to <m,0>,
  3368. // but guard against overflow
  3369. handledCPCountPlusOne = handledCPCount + 1;
  3370. if (m - n > floor((maxInt - delta) / handledCPCountPlusOne)) {
  3371. error('overflow');
  3372. }
  3373. delta += (m - n) * handledCPCountPlusOne;
  3374. n = m;
  3375. for (j = 0; j < inputLength; ++j) {
  3376. currentValue = input[j];
  3377. if (currentValue < n && ++delta > maxInt) {
  3378. error('overflow');
  3379. }
  3380. if (currentValue == n) {
  3381. // Represent delta as a generalized variable-length integer
  3382. for (q = delta, k = base; /* no condition */; k += base) {
  3383. t = k <= bias ? tMin : (k >= bias + tMax ? tMax : k - bias);
  3384. if (q < t) {
  3385. break;
  3386. }
  3387. qMinusT = q - t;
  3388. baseMinusT = base - t;
  3389. output.push(
  3390. stringFromCharCode(digitToBasic(t + qMinusT % baseMinusT, 0))
  3391. );
  3392. q = floor(qMinusT / baseMinusT);
  3393. }
  3394. output.push(stringFromCharCode(digitToBasic(q, 0)));
  3395. bias = adapt(delta, handledCPCountPlusOne, handledCPCount == basicLength);
  3396. delta = 0;
  3397. ++handledCPCount;
  3398. }
  3399. }
  3400. ++delta;
  3401. ++n;
  3402. }
  3403. return output.join('');
  3404. }
  3405. /**
  3406. * Converts a Punycode string representing a domain name or an email address
  3407. * to Unicode. Only the Punycoded parts of the input will be converted, i.e.
  3408. * it doesn't matter if you call it on a string that has already been
  3409. * converted to Unicode.
  3410. * @memberOf punycode
  3411. * @param {String} input The Punycoded domain name or email address to
  3412. * convert to Unicode.
  3413. * @returns {String} The Unicode representation of the given Punycode
  3414. * string.
  3415. */
  3416. function toUnicode(input) {
  3417. return mapDomain(input, function(string) {
  3418. return regexPunycode.test(string)
  3419. ? decode(string.slice(4).toLowerCase())
  3420. : string;
  3421. });
  3422. }
  3423. /**
  3424. * Converts a Unicode string representing a domain name or an email address to
  3425. * Punycode. Only the non-ASCII parts of the domain name will be converted,
  3426. * i.e. it doesn't matter if you call it with a domain that's already in
  3427. * ASCII.
  3428. * @memberOf punycode
  3429. * @param {String} input The domain name or email address to convert, as a
  3430. * Unicode string.
  3431. * @returns {String} The Punycode representation of the given domain name or
  3432. * email address.
  3433. */
  3434. function toASCII(input) {
  3435. return mapDomain(input, function(string) {
  3436. return regexNonASCII.test(string)
  3437. ? 'xn--' + encode(string)
  3438. : string;
  3439. });
  3440. }
  3441. /*--------------------------------------------------------------------------*/
  3442. /** Define the public API */
  3443. punycode = {
  3444. /**
  3445. * A string representing the current Punycode.js version number.
  3446. * @memberOf punycode
  3447. * @type String
  3448. */
  3449. 'version': '1.4.1',
  3450. /**
  3451. * An object of methods to convert from JavaScript's internal character
  3452. * representation (UCS-2) to Unicode code points, and back.
  3453. * @see <https://mathiasbynens.be/notes/javascript-encoding>
  3454. * @memberOf punycode
  3455. * @type Object
  3456. */
  3457. 'ucs2': {
  3458. 'decode': ucs2decode,
  3459. 'encode': ucs2encode
  3460. },
  3461. 'decode': decode,
  3462. 'encode': encode,
  3463. 'toASCII': toASCII,
  3464. 'toUnicode': toUnicode
  3465. };
  3466. /** Expose `punycode` */
  3467. // Some AMD build optimizers, like r.js, check for specific condition patterns
  3468. // like the following:
  3469. if (
  3470. typeof define == 'function' &&
  3471. typeof define.amd == 'object' &&
  3472. define.amd
  3473. ) {
  3474. define('punycode', function() {
  3475. return punycode;
  3476. });
  3477. } else if (freeExports && freeModule) {
  3478. if (module.exports == freeExports) {
  3479. // in Node.js, io.js, or RingoJS v0.8.0+
  3480. freeModule.exports = punycode;
  3481. } else {
  3482. // in Narwhal or RingoJS v0.7.0-
  3483. for (key in punycode) {
  3484. punycode.hasOwnProperty(key) && (freeExports[key] = punycode[key]);
  3485. }
  3486. }
  3487. } else {
  3488. // in Rhino or a web browser
  3489. root.punycode = punycode;
  3490. }
  3491. }(this));
  3492. }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  3493. },{}],26:[function(require,module,exports){
  3494. // Copyright Joyent, Inc. and other Node contributors.
  3495. //
  3496. // Permission is hereby granted, free of charge, to any person obtaining a
  3497. // copy of this software and associated documentation files (the
  3498. // "Software"), to deal in the Software without restriction, including
  3499. // without limitation the rights to use, copy, modify, merge, publish,
  3500. // distribute, sublicense, and/or sell copies of the Software, and to permit
  3501. // persons to whom the Software is furnished to do so, subject to the
  3502. // following conditions:
  3503. //
  3504. // The above copyright notice and this permission notice shall be included
  3505. // in all copies or substantial portions of the Software.
  3506. //
  3507. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  3508. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  3509. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  3510. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  3511. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  3512. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  3513. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  3514. 'use strict';
  3515. // If obj.hasOwnProperty has been overridden, then calling
  3516. // obj.hasOwnProperty(prop) will break.
  3517. // See: https://github.com/joyent/node/issues/1707
  3518. function hasOwnProperty(obj, prop) {
  3519. return Object.prototype.hasOwnProperty.call(obj, prop);
  3520. }
  3521. module.exports = function(qs, sep, eq, options) {
  3522. sep = sep || '&';
  3523. eq = eq || '=';
  3524. var obj = {};
  3525. if (typeof qs !== 'string' || qs.length === 0) {
  3526. return obj;
  3527. }
  3528. var regexp = /\+/g;
  3529. qs = qs.split(sep);
  3530. var maxKeys = 1000;
  3531. if (options && typeof options.maxKeys === 'number') {
  3532. maxKeys = options.maxKeys;
  3533. }
  3534. var len = qs.length;
  3535. // maxKeys <= 0 means that we should not limit keys count
  3536. if (maxKeys > 0 && len > maxKeys) {
  3537. len = maxKeys;
  3538. }
  3539. for (var i = 0; i < len; ++i) {
  3540. var x = qs[i].replace(regexp, '%20'),
  3541. idx = x.indexOf(eq),
  3542. kstr, vstr, k, v;
  3543. if (idx >= 0) {
  3544. kstr = x.substr(0, idx);
  3545. vstr = x.substr(idx + 1);
  3546. } else {
  3547. kstr = x;
  3548. vstr = '';
  3549. }
  3550. k = decodeURIComponent(kstr);
  3551. v = decodeURIComponent(vstr);
  3552. if (!hasOwnProperty(obj, k)) {
  3553. obj[k] = v;
  3554. } else if (isArray(obj[k])) {
  3555. obj[k].push(v);
  3556. } else {
  3557. obj[k] = [obj[k], v];
  3558. }
  3559. }
  3560. return obj;
  3561. };
  3562. var isArray = Array.isArray || function (xs) {
  3563. return Object.prototype.toString.call(xs) === '[object Array]';
  3564. };
  3565. },{}],27:[function(require,module,exports){
  3566. // Copyright Joyent, Inc. and other Node contributors.
  3567. //
  3568. // Permission is hereby granted, free of charge, to any person obtaining a
  3569. // copy of this software and associated documentation files (the
  3570. // "Software"), to deal in the Software without restriction, including
  3571. // without limitation the rights to use, copy, modify, merge, publish,
  3572. // distribute, sublicense, and/or sell copies of the Software, and to permit
  3573. // persons to whom the Software is furnished to do so, subject to the
  3574. // following conditions:
  3575. //
  3576. // The above copyright notice and this permission notice shall be included
  3577. // in all copies or substantial portions of the Software.
  3578. //
  3579. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  3580. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  3581. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  3582. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  3583. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  3584. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  3585. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  3586. 'use strict';
  3587. var stringifyPrimitive = function(v) {
  3588. switch (typeof v) {
  3589. case 'string':
  3590. return v;
  3591. case 'boolean':
  3592. return v ? 'true' : 'false';
  3593. case 'number':
  3594. return isFinite(v) ? v : '';
  3595. default:
  3596. return '';
  3597. }
  3598. };
  3599. module.exports = function(obj, sep, eq, name) {
  3600. sep = sep || '&';
  3601. eq = eq || '=';
  3602. if (obj === null) {
  3603. obj = undefined;
  3604. }
  3605. if (typeof obj === 'object') {
  3606. return map(objectKeys(obj), function(k) {
  3607. var ks = encodeURIComponent(stringifyPrimitive(k)) + eq;
  3608. if (isArray(obj[k])) {
  3609. return map(obj[k], function(v) {
  3610. return ks + encodeURIComponent(stringifyPrimitive(v));
  3611. }).join(sep);
  3612. } else {
  3613. return ks + encodeURIComponent(stringifyPrimitive(obj[k]));
  3614. }
  3615. }).join(sep);
  3616. }
  3617. if (!name) return '';
  3618. return encodeURIComponent(stringifyPrimitive(name)) + eq +
  3619. encodeURIComponent(stringifyPrimitive(obj));
  3620. };
  3621. var isArray = Array.isArray || function (xs) {
  3622. return Object.prototype.toString.call(xs) === '[object Array]';
  3623. };
  3624. function map (xs, f) {
  3625. if (xs.map) return xs.map(f);
  3626. var res = [];
  3627. for (var i = 0; i < xs.length; i++) {
  3628. res.push(f(xs[i], i));
  3629. }
  3630. return res;
  3631. }
  3632. var objectKeys = Object.keys || function (obj) {
  3633. var res = [];
  3634. for (var key in obj) {
  3635. if (Object.prototype.hasOwnProperty.call(obj, key)) res.push(key);
  3636. }
  3637. return res;
  3638. };
  3639. },{}],28:[function(require,module,exports){
  3640. 'use strict';
  3641. exports.decode = exports.parse = require('./decode');
  3642. exports.encode = exports.stringify = require('./encode');
  3643. },{"./decode":26,"./encode":27}],29:[function(require,module,exports){
  3644. // Copyright Joyent, Inc. and other Node contributors.
  3645. //
  3646. // Permission is hereby granted, free of charge, to any person obtaining a
  3647. // copy of this software and associated documentation files (the
  3648. // "Software"), to deal in the Software without restriction, including
  3649. // without limitation the rights to use, copy, modify, merge, publish,
  3650. // distribute, sublicense, and/or sell copies of the Software, and to permit
  3651. // persons to whom the Software is furnished to do so, subject to the
  3652. // following conditions:
  3653. //
  3654. // The above copyright notice and this permission notice shall be included
  3655. // in all copies or substantial portions of the Software.
  3656. //
  3657. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  3658. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  3659. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  3660. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  3661. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  3662. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  3663. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  3664. var punycode = require('punycode');
  3665. exports.parse = urlParse;
  3666. exports.resolve = urlResolve;
  3667. exports.resolveObject = urlResolveObject;
  3668. exports.format = urlFormat;
  3669. exports.Url = Url;
  3670. function Url() {
  3671. this.protocol = null;
  3672. this.slashes = null;
  3673. this.auth = null;
  3674. this.host = null;
  3675. this.port = null;
  3676. this.hostname = null;
  3677. this.hash = null;
  3678. this.search = null;
  3679. this.query = null;
  3680. this.pathname = null;
  3681. this.path = null;
  3682. this.href = null;
  3683. }
  3684. // Reference: RFC 3986, RFC 1808, RFC 2396
  3685. // define these here so at least they only have to be
  3686. // compiled once on the first module load.
  3687. var protocolPattern = /^([a-z0-9.+-]+:)/i,
  3688. portPattern = /:[0-9]*$/,
  3689. // RFC 2396: characters reserved for delimiting URLs.
  3690. // We actually just auto-escape these.
  3691. delims = ['<', '>', '"', '`', ' ', '\r', '\n', '\t'],
  3692. // RFC 2396: characters not allowed for various reasons.
  3693. unwise = ['{', '}', '|', '\\', '^', '`'].concat(delims),
  3694. // Allowed by RFCs, but cause of XSS attacks. Always escape these.
  3695. autoEscape = ['\''].concat(unwise),
  3696. // Characters that are never ever allowed in a hostname.
  3697. // Note that any invalid chars are also handled, but these
  3698. // are the ones that are *expected* to be seen, so we fast-path
  3699. // them.
  3700. nonHostChars = ['%', '/', '?', ';', '#'].concat(autoEscape),
  3701. hostEndingChars = ['/', '?', '#'],
  3702. hostnameMaxLen = 255,
  3703. hostnamePartPattern = /^[a-z0-9A-Z_-]{0,63}$/,
  3704. hostnamePartStart = /^([a-z0-9A-Z_-]{0,63})(.*)$/,
  3705. // protocols that can allow "unsafe" and "unwise" chars.
  3706. unsafeProtocol = {
  3707. 'javascript': true,
  3708. 'javascript:': true
  3709. },
  3710. // protocols that never have a hostname.
  3711. hostlessProtocol = {
  3712. 'javascript': true,
  3713. 'javascript:': true
  3714. },
  3715. // protocols that always contain a // bit.
  3716. slashedProtocol = {
  3717. 'http': true,
  3718. 'https': true,
  3719. 'ftp': true,
  3720. 'gopher': true,
  3721. 'file': true,
  3722. 'http:': true,
  3723. 'https:': true,
  3724. 'ftp:': true,
  3725. 'gopher:': true,
  3726. 'file:': true
  3727. },
  3728. querystring = require('querystring');
  3729. function urlParse(url, parseQueryString, slashesDenoteHost) {
  3730. if (url && isObject(url) && url instanceof Url) return url;
  3731. var u = new Url;
  3732. u.parse(url, parseQueryString, slashesDenoteHost);
  3733. return u;
  3734. }
  3735. Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) {
  3736. if (!isString(url)) {
  3737. throw new TypeError("Parameter 'url' must be a string, not " + typeof url);
  3738. }
  3739. var rest = url;
  3740. // trim before proceeding.
  3741. // This is to support parse stuff like " http://foo.com \n"
  3742. rest = rest.trim();
  3743. var proto = protocolPattern.exec(rest);
  3744. if (proto) {
  3745. proto = proto[0];
  3746. var lowerProto = proto.toLowerCase();
  3747. this.protocol = lowerProto;
  3748. rest = rest.substr(proto.length);
  3749. }
  3750. // figure out if it's got a host
  3751. // user@server is *always* interpreted as a hostname, and url
  3752. // resolution will treat //foo/bar as host=foo,path=bar because that's
  3753. // how the browser resolves relative URLs.
  3754. if (slashesDenoteHost || proto || rest.match(/^\/\/[^@\/]+@[^@\/]+/)) {
  3755. var slashes = rest.substr(0, 2) === '//';
  3756. if (slashes && !(proto && hostlessProtocol[proto])) {
  3757. rest = rest.substr(2);
  3758. this.slashes = true;
  3759. }
  3760. }
  3761. if (!hostlessProtocol[proto] &&
  3762. (slashes || (proto && !slashedProtocol[proto]))) {
  3763. // there's a hostname.
  3764. // the first instance of /, ?, ;, or # ends the host.
  3765. //
  3766. // If there is an @ in the hostname, then non-host chars *are* allowed
  3767. // to the left of the last @ sign, unless some host-ending character
  3768. // comes *before* the @-sign.
  3769. // URLs are obnoxious.
  3770. //
  3771. // ex:
  3772. // http://a@b@c/ => user:a@b host:c
  3773. // http://a@b?@c => user:a host:c path:/?@c
  3774. // v0.12 TODO(isaacs): This is not quite how Chrome does things.
  3775. // Review our test case against browsers more comprehensively.
  3776. // find the first instance of any hostEndingChars
  3777. var hostEnd = -1;
  3778. for (var i = 0; i < hostEndingChars.length; i++) {
  3779. var hec = rest.indexOf(hostEndingChars[i]);
  3780. if (hec !== -1 && (hostEnd === -1 || hec < hostEnd))
  3781. hostEnd = hec;
  3782. }
  3783. // at this point, either we have an explicit point where the
  3784. // auth portion cannot go past, or the last @ char is the decider.
  3785. var auth, atSign;
  3786. if (hostEnd === -1) {
  3787. // atSign can be anywhere.
  3788. atSign = rest.lastIndexOf('@');
  3789. } else {
  3790. // atSign must be in auth portion.
  3791. // http://a@b/c@d => host:b auth:a path:/c@d
  3792. atSign = rest.lastIndexOf('@', hostEnd);
  3793. }
  3794. // Now we have a portion which is definitely the auth.
  3795. // Pull that off.
  3796. if (atSign !== -1) {
  3797. auth = rest.slice(0, atSign);
  3798. rest = rest.slice(atSign + 1);
  3799. this.auth = decodeURIComponent(auth);
  3800. }
  3801. // the host is the remaining to the left of the first non-host char
  3802. hostEnd = -1;
  3803. for (var i = 0; i < nonHostChars.length; i++) {
  3804. var hec = rest.indexOf(nonHostChars[i]);
  3805. if (hec !== -1 && (hostEnd === -1 || hec < hostEnd))
  3806. hostEnd = hec;
  3807. }
  3808. // if we still have not hit it, then the entire thing is a host.
  3809. if (hostEnd === -1)
  3810. hostEnd = rest.length;
  3811. this.host = rest.slice(0, hostEnd);
  3812. rest = rest.slice(hostEnd);
  3813. // pull out port.
  3814. this.parseHost();
  3815. // we've indicated that there is a hostname,
  3816. // so even if it's empty, it has to be present.
  3817. this.hostname = this.hostname || '';
  3818. // if hostname begins with [ and ends with ]
  3819. // assume that it's an IPv6 address.
  3820. var ipv6Hostname = this.hostname[0] === '[' &&
  3821. this.hostname[this.hostname.length - 1] === ']';
  3822. // validate a little.
  3823. if (!ipv6Hostname) {
  3824. var hostparts = this.hostname.split(/\./);
  3825. for (var i = 0, l = hostparts.length; i < l; i++) {
  3826. var part = hostparts[i];
  3827. if (!part) continue;
  3828. if (!part.match(hostnamePartPattern)) {
  3829. var newpart = '';
  3830. for (var j = 0, k = part.length; j < k; j++) {
  3831. if (part.charCodeAt(j) > 127) {
  3832. // we replace non-ASCII char with a temporary placeholder
  3833. // we need this to make sure size of hostname is not
  3834. // broken by replacing non-ASCII by nothing
  3835. newpart += 'x';
  3836. } else {
  3837. newpart += part[j];
  3838. }
  3839. }
  3840. // we test again with ASCII char only
  3841. if (!newpart.match(hostnamePartPattern)) {
  3842. var validParts = hostparts.slice(0, i);
  3843. var notHost = hostparts.slice(i + 1);
  3844. var bit = part.match(hostnamePartStart);
  3845. if (bit) {
  3846. validParts.push(bit[1]);
  3847. notHost.unshift(bit[2]);
  3848. }
  3849. if (notHost.length) {
  3850. rest = '/' + notHost.join('.') + rest;
  3851. }
  3852. this.hostname = validParts.join('.');
  3853. break;
  3854. }
  3855. }
  3856. }
  3857. }
  3858. if (this.hostname.length > hostnameMaxLen) {
  3859. this.hostname = '';
  3860. } else {
  3861. // hostnames are always lower case.
  3862. this.hostname = this.hostname.toLowerCase();
  3863. }
  3864. if (!ipv6Hostname) {
  3865. // IDNA Support: Returns a puny coded representation of "domain".
  3866. // It only converts the part of the domain name that
  3867. // has non ASCII characters. I.e. it dosent matter if
  3868. // you call it with a domain that already is in ASCII.
  3869. var domainArray = this.hostname.split('.');
  3870. var newOut = [];
  3871. for (var i = 0; i < domainArray.length; ++i) {
  3872. var s = domainArray[i];
  3873. newOut.push(s.match(/[^A-Za-z0-9_-]/) ?
  3874. 'xn--' + punycode.encode(s) : s);
  3875. }
  3876. this.hostname = newOut.join('.');
  3877. }
  3878. var p = this.port ? ':' + this.port : '';
  3879. var h = this.hostname || '';
  3880. this.host = h + p;
  3881. this.href += this.host;
  3882. // strip [ and ] from the hostname
  3883. // the host field still retains them, though
  3884. if (ipv6Hostname) {
  3885. this.hostname = this.hostname.substr(1, this.hostname.length - 2);
  3886. if (rest[0] !== '/') {
  3887. rest = '/' + rest;
  3888. }
  3889. }
  3890. }
  3891. // now rest is set to the post-host stuff.
  3892. // chop off any delim chars.
  3893. if (!unsafeProtocol[lowerProto]) {
  3894. // First, make 100% sure that any "autoEscape" chars get
  3895. // escaped, even if encodeURIComponent doesn't think they
  3896. // need to be.
  3897. for (var i = 0, l = autoEscape.length; i < l; i++) {
  3898. var ae = autoEscape[i];
  3899. var esc = encodeURIComponent(ae);
  3900. if (esc === ae) {
  3901. esc = escape(ae);
  3902. }
  3903. rest = rest.split(ae).join(esc);
  3904. }
  3905. }
  3906. // chop off from the tail first.
  3907. var hash = rest.indexOf('#');
  3908. if (hash !== -1) {
  3909. // got a fragment string.
  3910. this.hash = rest.substr(hash);
  3911. rest = rest.slice(0, hash);
  3912. }
  3913. var qm = rest.indexOf('?');
  3914. if (qm !== -1) {
  3915. this.search = rest.substr(qm);
  3916. this.query = rest.substr(qm + 1);
  3917. if (parseQueryString) {
  3918. this.query = querystring.parse(this.query);
  3919. }
  3920. rest = rest.slice(0, qm);
  3921. } else if (parseQueryString) {
  3922. // no query string, but parseQueryString still requested
  3923. this.search = '';
  3924. this.query = {};
  3925. }
  3926. if (rest) this.pathname = rest;
  3927. if (slashedProtocol[lowerProto] &&
  3928. this.hostname && !this.pathname) {
  3929. this.pathname = '/';
  3930. }
  3931. //to support http.request
  3932. if (this.pathname || this.search) {
  3933. var p = this.pathname || '';
  3934. var s = this.search || '';
  3935. this.path = p + s;
  3936. }
  3937. // finally, reconstruct the href based on what has been validated.
  3938. this.href = this.format();
  3939. return this;
  3940. };
  3941. // format a parsed object into a url string
  3942. function urlFormat(obj) {
  3943. // ensure it's an object, and not a string url.
  3944. // If it's an obj, this is a no-op.
  3945. // this way, you can call url_format() on strings
  3946. // to clean up potentially wonky urls.
  3947. if (isString(obj)) obj = urlParse(obj);
  3948. if (!(obj instanceof Url)) return Url.prototype.format.call(obj);
  3949. return obj.format();
  3950. }
  3951. Url.prototype.format = function() {
  3952. var auth = this.auth || '';
  3953. if (auth) {
  3954. auth = encodeURIComponent(auth);
  3955. auth = auth.replace(/%3A/i, ':');
  3956. auth += '@';
  3957. }
  3958. var protocol = this.protocol || '',
  3959. pathname = this.pathname || '',
  3960. hash = this.hash || '',
  3961. host = false,
  3962. query = '';
  3963. if (this.host) {
  3964. host = auth + this.host;
  3965. } else if (this.hostname) {
  3966. host = auth + (this.hostname.indexOf(':') === -1 ?
  3967. this.hostname :
  3968. '[' + this.hostname + ']');
  3969. if (this.port) {
  3970. host += ':' + this.port;
  3971. }
  3972. }
  3973. if (this.query &&
  3974. isObject(this.query) &&
  3975. Object.keys(this.query).length) {
  3976. query = querystring.stringify(this.query);
  3977. }
  3978. var search = this.search || (query && ('?' + query)) || '';
  3979. if (protocol && protocol.substr(-1) !== ':') protocol += ':';
  3980. // only the slashedProtocols get the //. Not mailto:, xmpp:, etc.
  3981. // unless they had them to begin with.
  3982. if (this.slashes ||
  3983. (!protocol || slashedProtocol[protocol]) && host !== false) {
  3984. host = '//' + (host || '');
  3985. if (pathname && pathname.charAt(0) !== '/') pathname = '/' + pathname;
  3986. } else if (!host) {
  3987. host = '';
  3988. }
  3989. if (hash && hash.charAt(0) !== '#') hash = '#' + hash;
  3990. if (search && search.charAt(0) !== '?') search = '?' + search;
  3991. pathname = pathname.replace(/[?#]/g, function(match) {
  3992. return encodeURIComponent(match);
  3993. });
  3994. search = search.replace('#', '%23');
  3995. return protocol + host + pathname + search + hash;
  3996. };
  3997. function urlResolve(source, relative) {
  3998. return urlParse(source, false, true).resolve(relative);
  3999. }
  4000. Url.prototype.resolve = function(relative) {
  4001. return this.resolveObject(urlParse(relative, false, true)).format();
  4002. };
  4003. function urlResolveObject(source, relative) {
  4004. if (!source) return relative;
  4005. return urlParse(source, false, true).resolveObject(relative);
  4006. }
  4007. Url.prototype.resolveObject = function(relative) {
  4008. if (isString(relative)) {
  4009. var rel = new Url();
  4010. rel.parse(relative, false, true);
  4011. relative = rel;
  4012. }
  4013. var result = new Url();
  4014. Object.keys(this).forEach(function(k) {
  4015. result[k] = this[k];
  4016. }, this);
  4017. // hash is always overridden, no matter what.
  4018. // even href="" will remove it.
  4019. result.hash = relative.hash;
  4020. // if the relative url is empty, then there's nothing left to do here.
  4021. if (relative.href === '') {
  4022. result.href = result.format();
  4023. return result;
  4024. }
  4025. // hrefs like //foo/bar always cut to the protocol.
  4026. if (relative.slashes && !relative.protocol) {
  4027. // take everything except the protocol from relative
  4028. Object.keys(relative).forEach(function(k) {
  4029. if (k !== 'protocol')
  4030. result[k] = relative[k];
  4031. });
  4032. //urlParse appends trailing / to urls like http://www.example.com
  4033. if (slashedProtocol[result.protocol] &&
  4034. result.hostname && !result.pathname) {
  4035. result.path = result.pathname = '/';
  4036. }
  4037. result.href = result.format();
  4038. return result;
  4039. }
  4040. if (relative.protocol && relative.protocol !== result.protocol) {
  4041. // if it's a known url protocol, then changing
  4042. // the protocol does weird things
  4043. // first, if it's not file:, then we MUST have a host,
  4044. // and if there was a path
  4045. // to begin with, then we MUST have a path.
  4046. // if it is file:, then the host is dropped,
  4047. // because that's known to be hostless.
  4048. // anything else is assumed to be absolute.
  4049. if (!slashedProtocol[relative.protocol]) {
  4050. Object.keys(relative).forEach(function(k) {
  4051. result[k] = relative[k];
  4052. });
  4053. result.href = result.format();
  4054. return result;
  4055. }
  4056. result.protocol = relative.protocol;
  4057. if (!relative.host && !hostlessProtocol[relative.protocol]) {
  4058. var relPath = (relative.pathname || '').split('/');
  4059. while (relPath.length && !(relative.host = relPath.shift()));
  4060. if (!relative.host) relative.host = '';
  4061. if (!relative.hostname) relative.hostname = '';
  4062. if (relPath[0] !== '') relPath.unshift('');
  4063. if (relPath.length < 2) relPath.unshift('');
  4064. result.pathname = relPath.join('/');
  4065. } else {
  4066. result.pathname = relative.pathname;
  4067. }
  4068. result.search = relative.search;
  4069. result.query = relative.query;
  4070. result.host = relative.host || '';
  4071. result.auth = relative.auth;
  4072. result.hostname = relative.hostname || relative.host;
  4073. result.port = relative.port;
  4074. // to support http.request
  4075. if (result.pathname || result.search) {
  4076. var p = result.pathname || '';
  4077. var s = result.search || '';
  4078. result.path = p + s;
  4079. }
  4080. result.slashes = result.slashes || relative.slashes;
  4081. result.href = result.format();
  4082. return result;
  4083. }
  4084. var isSourceAbs = (result.pathname && result.pathname.charAt(0) === '/'),
  4085. isRelAbs = (
  4086. relative.host ||
  4087. relative.pathname && relative.pathname.charAt(0) === '/'
  4088. ),
  4089. mustEndAbs = (isRelAbs || isSourceAbs ||
  4090. (result.host && relative.pathname)),
  4091. removeAllDots = mustEndAbs,
  4092. srcPath = result.pathname && result.pathname.split('/') || [],
  4093. relPath = relative.pathname && relative.pathname.split('/') || [],
  4094. psychotic = result.protocol && !slashedProtocol[result.protocol];
  4095. // if the url is a non-slashed url, then relative
  4096. // links like ../.. should be able
  4097. // to crawl up to the hostname, as well. This is strange.
  4098. // result.protocol has already been set by now.
  4099. // Later on, put the first path part into the host field.
  4100. if (psychotic) {
  4101. result.hostname = '';
  4102. result.port = null;
  4103. if (result.host) {
  4104. if (srcPath[0] === '') srcPath[0] = result.host;
  4105. else srcPath.unshift(result.host);
  4106. }
  4107. result.host = '';
  4108. if (relative.protocol) {
  4109. relative.hostname = null;
  4110. relative.port = null;
  4111. if (relative.host) {
  4112. if (relPath[0] === '') relPath[0] = relative.host;
  4113. else relPath.unshift(relative.host);
  4114. }
  4115. relative.host = null;
  4116. }
  4117. mustEndAbs = mustEndAbs && (relPath[0] === '' || srcPath[0] === '');
  4118. }
  4119. if (isRelAbs) {
  4120. // it's absolute.
  4121. result.host = (relative.host || relative.host === '') ?
  4122. relative.host : result.host;
  4123. result.hostname = (relative.hostname || relative.hostname === '') ?
  4124. relative.hostname : result.hostname;
  4125. result.search = relative.search;
  4126. result.query = relative.query;
  4127. srcPath = relPath;
  4128. // fall through to the dot-handling below.
  4129. } else if (relPath.length) {
  4130. // it's relative
  4131. // throw away the existing file, and take the new path instead.
  4132. if (!srcPath) srcPath = [];
  4133. srcPath.pop();
  4134. srcPath = srcPath.concat(relPath);
  4135. result.search = relative.search;
  4136. result.query = relative.query;
  4137. } else if (!isNullOrUndefined(relative.search)) {
  4138. // just pull out the search.
  4139. // like href='?foo'.
  4140. // Put this after the other two cases because it simplifies the booleans
  4141. if (psychotic) {
  4142. result.hostname = result.host = srcPath.shift();
  4143. //occationaly the auth can get stuck only in host
  4144. //this especialy happens in cases like
  4145. //url.resolveObject('mailto:local1@domain1', 'local2@domain2')
  4146. var authInHost = result.host && result.host.indexOf('@') > 0 ?
  4147. result.host.split('@') : false;
  4148. if (authInHost) {
  4149. result.auth = authInHost.shift();
  4150. result.host = result.hostname = authInHost.shift();
  4151. }
  4152. }
  4153. result.search = relative.search;
  4154. result.query = relative.query;
  4155. //to support http.request
  4156. if (!isNull(result.pathname) || !isNull(result.search)) {
  4157. result.path = (result.pathname ? result.pathname : '') +
  4158. (result.search ? result.search : '');
  4159. }
  4160. result.href = result.format();
  4161. return result;
  4162. }
  4163. if (!srcPath.length) {
  4164. // no path at all. easy.
  4165. // we've already handled the other stuff above.
  4166. result.pathname = null;
  4167. //to support http.request
  4168. if (result.search) {
  4169. result.path = '/' + result.search;
  4170. } else {
  4171. result.path = null;
  4172. }
  4173. result.href = result.format();
  4174. return result;
  4175. }
  4176. // if a url ENDs in . or .., then it must get a trailing slash.
  4177. // however, if it ends in anything else non-slashy,
  4178. // then it must NOT get a trailing slash.
  4179. var last = srcPath.slice(-1)[0];
  4180. var hasTrailingSlash = (
  4181. (result.host || relative.host) && (last === '.' || last === '..') ||
  4182. last === '');
  4183. // strip single dots, resolve double dots to parent dir
  4184. // if the path tries to go above the root, `up` ends up > 0
  4185. var up = 0;
  4186. for (var i = srcPath.length; i >= 0; i--) {
  4187. last = srcPath[i];
  4188. if (last == '.') {
  4189. srcPath.splice(i, 1);
  4190. } else if (last === '..') {
  4191. srcPath.splice(i, 1);
  4192. up++;
  4193. } else if (up) {
  4194. srcPath.splice(i, 1);
  4195. up--;
  4196. }
  4197. }
  4198. // if the path is allowed to go above the root, restore leading ..s
  4199. if (!mustEndAbs && !removeAllDots) {
  4200. for (; up--; up) {
  4201. srcPath.unshift('..');
  4202. }
  4203. }
  4204. if (mustEndAbs && srcPath[0] !== '' &&
  4205. (!srcPath[0] || srcPath[0].charAt(0) !== '/')) {
  4206. srcPath.unshift('');
  4207. }
  4208. if (hasTrailingSlash && (srcPath.join('/').substr(-1) !== '/')) {
  4209. srcPath.push('');
  4210. }
  4211. var isAbsolute = srcPath[0] === '' ||
  4212. (srcPath[0] && srcPath[0].charAt(0) === '/');
  4213. // put the host back
  4214. if (psychotic) {
  4215. result.hostname = result.host = isAbsolute ? '' :
  4216. srcPath.length ? srcPath.shift() : '';
  4217. //occationaly the auth can get stuck only in host
  4218. //this especialy happens in cases like
  4219. //url.resolveObject('mailto:local1@domain1', 'local2@domain2')
  4220. var authInHost = result.host && result.host.indexOf('@') > 0 ?
  4221. result.host.split('@') : false;
  4222. if (authInHost) {
  4223. result.auth = authInHost.shift();
  4224. result.host = result.hostname = authInHost.shift();
  4225. }
  4226. }
  4227. mustEndAbs = mustEndAbs || (result.host && srcPath.length);
  4228. if (mustEndAbs && !isAbsolute) {
  4229. srcPath.unshift('');
  4230. }
  4231. if (!srcPath.length) {
  4232. result.pathname = null;
  4233. result.path = null;
  4234. } else {
  4235. result.pathname = srcPath.join('/');
  4236. }
  4237. //to support request.http
  4238. if (!isNull(result.pathname) || !isNull(result.search)) {
  4239. result.path = (result.pathname ? result.pathname : '') +
  4240. (result.search ? result.search : '');
  4241. }
  4242. result.auth = relative.auth || result.auth;
  4243. result.slashes = result.slashes || relative.slashes;
  4244. result.href = result.format();
  4245. return result;
  4246. };
  4247. Url.prototype.parseHost = function() {
  4248. var host = this.host;
  4249. var port = portPattern.exec(host);
  4250. if (port) {
  4251. port = port[0];
  4252. if (port !== ':') {
  4253. this.port = port.substr(1);
  4254. }
  4255. host = host.substr(0, host.length - port.length);
  4256. }
  4257. if (host) this.hostname = host;
  4258. };
  4259. function isString(arg) {
  4260. return typeof arg === "string";
  4261. }
  4262. function isObject(arg) {
  4263. return typeof arg === 'object' && arg !== null;
  4264. }
  4265. function isNull(arg) {
  4266. return arg === null;
  4267. }
  4268. function isNullOrUndefined(arg) {
  4269. return arg == null;
  4270. }
  4271. },{"punycode":25,"querystring":28}],30:[function(require,module,exports){
  4272. function DOMParser(options){
  4273. this.options = options ||{locator:{}};
  4274. }
  4275. DOMParser.prototype.parseFromString = function(source,mimeType){
  4276. var options = this.options;
  4277. var sax = new XMLReader();
  4278. var domBuilder = options.domBuilder || new DOMHandler();//contentHandler and LexicalHandler
  4279. var errorHandler = options.errorHandler;
  4280. var locator = options.locator;
  4281. var defaultNSMap = options.xmlns||{};
  4282. var entityMap = {'lt':'<','gt':'>','amp':'&','quot':'"','apos':"'"}
  4283. if(locator){
  4284. domBuilder.setDocumentLocator(locator)
  4285. }
  4286. sax.errorHandler = buildErrorHandler(errorHandler,domBuilder,locator);
  4287. sax.domBuilder = options.domBuilder || domBuilder;
  4288. if(/\/x?html?$/.test(mimeType)){
  4289. entityMap.nbsp = '\xa0';
  4290. entityMap.copy = '\xa9';
  4291. defaultNSMap['']= 'http://www.w3.org/1999/xhtml';
  4292. }
  4293. defaultNSMap.xml = defaultNSMap.xml || 'http://www.w3.org/XML/1998/namespace';
  4294. if(source){
  4295. sax.parse(source,defaultNSMap,entityMap);
  4296. }else{
  4297. sax.errorHandler.error("invalid doc source");
  4298. }
  4299. return domBuilder.doc;
  4300. }
  4301. function buildErrorHandler(errorImpl,domBuilder,locator){
  4302. if(!errorImpl){
  4303. if(domBuilder instanceof DOMHandler){
  4304. return domBuilder;
  4305. }
  4306. errorImpl = domBuilder ;
  4307. }
  4308. var errorHandler = {}
  4309. var isCallback = errorImpl instanceof Function;
  4310. locator = locator||{}
  4311. function build(key){
  4312. var fn = errorImpl[key];
  4313. if(!fn && isCallback){
  4314. fn = errorImpl.length == 2?function(msg){errorImpl(key,msg)}:errorImpl;
  4315. }
  4316. errorHandler[key] = fn && function(msg){
  4317. fn('[xmldom '+key+']\t'+msg+_locator(locator));
  4318. }||function(){};
  4319. }
  4320. build('warning');
  4321. build('error');
  4322. build('fatalError');
  4323. return errorHandler;
  4324. }
  4325. //console.log('#\n\n\n\n\n\n\n####')
  4326. /**
  4327. * +ContentHandler+ErrorHandler
  4328. * +LexicalHandler+EntityResolver2
  4329. * -DeclHandler-DTDHandler
  4330. *
  4331. * DefaultHandler:EntityResolver, DTDHandler, ContentHandler, ErrorHandler
  4332. * DefaultHandler2:DefaultHandler,LexicalHandler, DeclHandler, EntityResolver2
  4333. * @link http://www.saxproject.org/apidoc/org/xml/sax/helpers/DefaultHandler.html
  4334. */
  4335. function DOMHandler() {
  4336. this.cdata = false;
  4337. }
  4338. function position(locator,node){
  4339. node.lineNumber = locator.lineNumber;
  4340. node.columnNumber = locator.columnNumber;
  4341. }
  4342. /**
  4343. * @see org.xml.sax.ContentHandler#startDocument
  4344. * @link http://www.saxproject.org/apidoc/org/xml/sax/ContentHandler.html
  4345. */
  4346. DOMHandler.prototype = {
  4347. startDocument : function() {
  4348. this.doc = new DOMImplementation().createDocument(null, null, null);
  4349. if (this.locator) {
  4350. this.doc.documentURI = this.locator.systemId;
  4351. }
  4352. },
  4353. startElement:function(namespaceURI, localName, qName, attrs) {
  4354. var doc = this.doc;
  4355. var el = doc.createElementNS(namespaceURI, qName||localName);
  4356. var len = attrs.length;
  4357. appendElement(this, el);
  4358. this.currentElement = el;
  4359. this.locator && position(this.locator,el)
  4360. for (var i = 0 ; i < len; i++) {
  4361. var namespaceURI = attrs.getURI(i);
  4362. var value = attrs.getValue(i);
  4363. var qName = attrs.getQName(i);
  4364. var attr = doc.createAttributeNS(namespaceURI, qName);
  4365. this.locator &&position(attrs.getLocator(i),attr);
  4366. attr.value = attr.nodeValue = value;
  4367. el.setAttributeNode(attr)
  4368. }
  4369. },
  4370. endElement:function(namespaceURI, localName, qName) {
  4371. var current = this.currentElement
  4372. var tagName = current.tagName;
  4373. this.currentElement = current.parentNode;
  4374. },
  4375. startPrefixMapping:function(prefix, uri) {
  4376. },
  4377. endPrefixMapping:function(prefix) {
  4378. },
  4379. processingInstruction:function(target, data) {
  4380. var ins = this.doc.createProcessingInstruction(target, data);
  4381. this.locator && position(this.locator,ins)
  4382. appendElement(this, ins);
  4383. },
  4384. ignorableWhitespace:function(ch, start, length) {
  4385. },
  4386. characters:function(chars, start, length) {
  4387. chars = _toString.apply(this,arguments)
  4388. //console.log(chars)
  4389. if(chars){
  4390. if (this.cdata) {
  4391. var charNode = this.doc.createCDATASection(chars);
  4392. } else {
  4393. var charNode = this.doc.createTextNode(chars);
  4394. }
  4395. if(this.currentElement){
  4396. this.currentElement.appendChild(charNode);
  4397. }else if(/^\s*$/.test(chars)){
  4398. this.doc.appendChild(charNode);
  4399. //process xml
  4400. }
  4401. this.locator && position(this.locator,charNode)
  4402. }
  4403. },
  4404. skippedEntity:function(name) {
  4405. },
  4406. endDocument:function() {
  4407. this.doc.normalize();
  4408. },
  4409. setDocumentLocator:function (locator) {
  4410. if(this.locator = locator){// && !('lineNumber' in locator)){
  4411. locator.lineNumber = 0;
  4412. }
  4413. },
  4414. //LexicalHandler
  4415. comment:function(chars, start, length) {
  4416. chars = _toString.apply(this,arguments)
  4417. var comm = this.doc.createComment(chars);
  4418. this.locator && position(this.locator,comm)
  4419. appendElement(this, comm);
  4420. },
  4421. startCDATA:function() {
  4422. //used in characters() methods
  4423. this.cdata = true;
  4424. },
  4425. endCDATA:function() {
  4426. this.cdata = false;
  4427. },
  4428. startDTD:function(name, publicId, systemId) {
  4429. var impl = this.doc.implementation;
  4430. if (impl && impl.createDocumentType) {
  4431. var dt = impl.createDocumentType(name, publicId, systemId);
  4432. this.locator && position(this.locator,dt)
  4433. appendElement(this, dt);
  4434. }
  4435. },
  4436. /**
  4437. * @see org.xml.sax.ErrorHandler
  4438. * @link http://www.saxproject.org/apidoc/org/xml/sax/ErrorHandler.html
  4439. */
  4440. warning:function(error) {
  4441. console.warn('[xmldom warning]\t'+error,_locator(this.locator));
  4442. },
  4443. error:function(error) {
  4444. console.error('[xmldom error]\t'+error,_locator(this.locator));
  4445. },
  4446. fatalError:function(error) {
  4447. console.error('[xmldom fatalError]\t'+error,_locator(this.locator));
  4448. throw error;
  4449. }
  4450. }
  4451. function _locator(l){
  4452. if(l){
  4453. return '\n@'+(l.systemId ||'')+'#[line:'+l.lineNumber+',col:'+l.columnNumber+']'
  4454. }
  4455. }
  4456. function _toString(chars,start,length){
  4457. if(typeof chars == 'string'){
  4458. return chars.substr(start,length)
  4459. }else{//java sax connect width xmldom on rhino(what about: "? && !(chars instanceof String)")
  4460. if(chars.length >= start+length || start){
  4461. return new java.lang.String(chars,start,length)+'';
  4462. }
  4463. return chars;
  4464. }
  4465. }
  4466. /*
  4467. * @link http://www.saxproject.org/apidoc/org/xml/sax/ext/LexicalHandler.html
  4468. * used method of org.xml.sax.ext.LexicalHandler:
  4469. * #comment(chars, start, length)
  4470. * #startCDATA()
  4471. * #endCDATA()
  4472. * #startDTD(name, publicId, systemId)
  4473. *
  4474. *
  4475. * IGNORED method of org.xml.sax.ext.LexicalHandler:
  4476. * #endDTD()
  4477. * #startEntity(name)
  4478. * #endEntity(name)
  4479. *
  4480. *
  4481. * @link http://www.saxproject.org/apidoc/org/xml/sax/ext/DeclHandler.html
  4482. * IGNORED method of org.xml.sax.ext.DeclHandler
  4483. * #attributeDecl(eName, aName, type, mode, value)
  4484. * #elementDecl(name, model)
  4485. * #externalEntityDecl(name, publicId, systemId)
  4486. * #internalEntityDecl(name, value)
  4487. * @link http://www.saxproject.org/apidoc/org/xml/sax/ext/EntityResolver2.html
  4488. * IGNORED method of org.xml.sax.EntityResolver2
  4489. * #resolveEntity(String name,String publicId,String baseURI,String systemId)
  4490. * #resolveEntity(publicId, systemId)
  4491. * #getExternalSubset(name, baseURI)
  4492. * @link http://www.saxproject.org/apidoc/org/xml/sax/DTDHandler.html
  4493. * IGNORED method of org.xml.sax.DTDHandler
  4494. * #notationDecl(name, publicId, systemId) {};
  4495. * #unparsedEntityDecl(name, publicId, systemId, notationName) {};
  4496. */
  4497. "endDTD,startEntity,endEntity,attributeDecl,elementDecl,externalEntityDecl,internalEntityDecl,resolveEntity,getExternalSubset,notationDecl,unparsedEntityDecl".replace(/\w+/g,function(key){
  4498. DOMHandler.prototype[key] = function(){return null}
  4499. })
  4500. /* Private static helpers treated below as private instance methods, so don't need to add these to the public API; we might use a Relator to also get rid of non-standard public properties */
  4501. function appendElement (hander,node) {
  4502. if (!hander.currentElement) {
  4503. hander.doc.appendChild(node);
  4504. } else {
  4505. hander.currentElement.appendChild(node);
  4506. }
  4507. }//appendChild and setAttributeNS are preformance key
  4508. //if(typeof require == 'function'){
  4509. var XMLReader = require('./sax').XMLReader;
  4510. var DOMImplementation = exports.DOMImplementation = require('./dom').DOMImplementation;
  4511. exports.XMLSerializer = require('./dom').XMLSerializer ;
  4512. exports.DOMParser = DOMParser;
  4513. //}
  4514. },{"./dom":31,"./sax":32}],31:[function(require,module,exports){
  4515. /*
  4516. * DOM Level 2
  4517. * Object DOMException
  4518. * @see http://www.w3.org/TR/REC-DOM-Level-1/ecma-script-language-binding.html
  4519. * @see http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/ecma-script-binding.html
  4520. */
  4521. function copy(src,dest){
  4522. for(var p in src){
  4523. dest[p] = src[p];
  4524. }
  4525. }
  4526. /**
  4527. ^\w+\.prototype\.([_\w]+)\s*=\s*((?:.*\{\s*?[\r\n][\s\S]*?^})|\S.*?(?=[;\r\n]));?
  4528. ^\w+\.prototype\.([_\w]+)\s*=\s*(\S.*?(?=[;\r\n]));?
  4529. */
  4530. function _extends(Class,Super){
  4531. var pt = Class.prototype;
  4532. if(Object.create){
  4533. var ppt = Object.create(Super.prototype)
  4534. pt.__proto__ = ppt;
  4535. }
  4536. if(!(pt instanceof Super)){
  4537. function t(){};
  4538. t.prototype = Super.prototype;
  4539. t = new t();
  4540. copy(pt,t);
  4541. Class.prototype = pt = t;
  4542. }
  4543. if(pt.constructor != Class){
  4544. if(typeof Class != 'function'){
  4545. console.error("unknow Class:"+Class)
  4546. }
  4547. pt.constructor = Class
  4548. }
  4549. }
  4550. var htmlns = 'http://www.w3.org/1999/xhtml' ;
  4551. // Node Types
  4552. var NodeType = {}
  4553. var ELEMENT_NODE = NodeType.ELEMENT_NODE = 1;
  4554. var ATTRIBUTE_NODE = NodeType.ATTRIBUTE_NODE = 2;
  4555. var TEXT_NODE = NodeType.TEXT_NODE = 3;
  4556. var CDATA_SECTION_NODE = NodeType.CDATA_SECTION_NODE = 4;
  4557. var ENTITY_REFERENCE_NODE = NodeType.ENTITY_REFERENCE_NODE = 5;
  4558. var ENTITY_NODE = NodeType.ENTITY_NODE = 6;
  4559. var PROCESSING_INSTRUCTION_NODE = NodeType.PROCESSING_INSTRUCTION_NODE = 7;
  4560. var COMMENT_NODE = NodeType.COMMENT_NODE = 8;
  4561. var DOCUMENT_NODE = NodeType.DOCUMENT_NODE = 9;
  4562. var DOCUMENT_TYPE_NODE = NodeType.DOCUMENT_TYPE_NODE = 10;
  4563. var DOCUMENT_FRAGMENT_NODE = NodeType.DOCUMENT_FRAGMENT_NODE = 11;
  4564. var NOTATION_NODE = NodeType.NOTATION_NODE = 12;
  4565. // ExceptionCode
  4566. var ExceptionCode = {}
  4567. var ExceptionMessage = {};
  4568. var INDEX_SIZE_ERR = ExceptionCode.INDEX_SIZE_ERR = ((ExceptionMessage[1]="Index size error"),1);
  4569. var DOMSTRING_SIZE_ERR = ExceptionCode.DOMSTRING_SIZE_ERR = ((ExceptionMessage[2]="DOMString size error"),2);
  4570. var HIERARCHY_REQUEST_ERR = ExceptionCode.HIERARCHY_REQUEST_ERR = ((ExceptionMessage[3]="Hierarchy request error"),3);
  4571. var WRONG_DOCUMENT_ERR = ExceptionCode.WRONG_DOCUMENT_ERR = ((ExceptionMessage[4]="Wrong document"),4);
  4572. var INVALID_CHARACTER_ERR = ExceptionCode.INVALID_CHARACTER_ERR = ((ExceptionMessage[5]="Invalid character"),5);
  4573. var NO_DATA_ALLOWED_ERR = ExceptionCode.NO_DATA_ALLOWED_ERR = ((ExceptionMessage[6]="No data allowed"),6);
  4574. var NO_MODIFICATION_ALLOWED_ERR = ExceptionCode.NO_MODIFICATION_ALLOWED_ERR = ((ExceptionMessage[7]="No modification allowed"),7);
  4575. var NOT_FOUND_ERR = ExceptionCode.NOT_FOUND_ERR = ((ExceptionMessage[8]="Not found"),8);
  4576. var NOT_SUPPORTED_ERR = ExceptionCode.NOT_SUPPORTED_ERR = ((ExceptionMessage[9]="Not supported"),9);
  4577. var INUSE_ATTRIBUTE_ERR = ExceptionCode.INUSE_ATTRIBUTE_ERR = ((ExceptionMessage[10]="Attribute in use"),10);
  4578. //level2
  4579. var INVALID_STATE_ERR = ExceptionCode.INVALID_STATE_ERR = ((ExceptionMessage[11]="Invalid state"),11);
  4580. var SYNTAX_ERR = ExceptionCode.SYNTAX_ERR = ((ExceptionMessage[12]="Syntax error"),12);
  4581. var INVALID_MODIFICATION_ERR = ExceptionCode.INVALID_MODIFICATION_ERR = ((ExceptionMessage[13]="Invalid modification"),13);
  4582. var NAMESPACE_ERR = ExceptionCode.NAMESPACE_ERR = ((ExceptionMessage[14]="Invalid namespace"),14);
  4583. var INVALID_ACCESS_ERR = ExceptionCode.INVALID_ACCESS_ERR = ((ExceptionMessage[15]="Invalid access"),15);
  4584. function DOMException(code, message) {
  4585. if(message instanceof Error){
  4586. var error = message;
  4587. }else{
  4588. error = this;
  4589. Error.call(this, ExceptionMessage[code]);
  4590. this.message = ExceptionMessage[code];
  4591. if(Error.captureStackTrace) Error.captureStackTrace(this, DOMException);
  4592. }
  4593. error.code = code;
  4594. if(message) this.message = this.message + ": " + message;
  4595. return error;
  4596. };
  4597. DOMException.prototype = Error.prototype;
  4598. copy(ExceptionCode,DOMException)
  4599. /**
  4600. * @see http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-536297177
  4601. * The NodeList interface provides the abstraction of an ordered collection of nodes, without defining or constraining how this collection is implemented. NodeList objects in the DOM are live.
  4602. * The items in the NodeList are accessible via an integral index, starting from 0.
  4603. */
  4604. function NodeList() {
  4605. };
  4606. NodeList.prototype = {
  4607. /**
  4608. * The number of nodes in the list. The range of valid child node indices is 0 to length-1 inclusive.
  4609. * @standard level1
  4610. */
  4611. length:0,
  4612. /**
  4613. * Returns the indexth item in the collection. If index is greater than or equal to the number of nodes in the list, this returns null.
  4614. * @standard level1
  4615. * @param index unsigned long
  4616. * Index into the collection.
  4617. * @return Node
  4618. * The node at the indexth position in the NodeList, or null if that is not a valid index.
  4619. */
  4620. item: function(index) {
  4621. return this[index] || null;
  4622. },
  4623. toString:function(isHTML,nodeFilter){
  4624. for(var buf = [], i = 0;i<this.length;i++){
  4625. serializeToString(this[i],buf,isHTML,nodeFilter);
  4626. }
  4627. return buf.join('');
  4628. }
  4629. };
  4630. function LiveNodeList(node,refresh){
  4631. this._node = node;
  4632. this._refresh = refresh
  4633. _updateLiveList(this);
  4634. }
  4635. function _updateLiveList(list){
  4636. var inc = list._node._inc || list._node.ownerDocument._inc;
  4637. if(list._inc != inc){
  4638. var ls = list._refresh(list._node);
  4639. //console.log(ls.length)
  4640. __set__(list,'length',ls.length);
  4641. copy(ls,list);
  4642. list._inc = inc;
  4643. }
  4644. }
  4645. LiveNodeList.prototype.item = function(i){
  4646. _updateLiveList(this);
  4647. return this[i];
  4648. }
  4649. _extends(LiveNodeList,NodeList);
  4650. /**
  4651. *
  4652. * Objects implementing the NamedNodeMap interface are used to represent collections of nodes that can be accessed by name. Note that NamedNodeMap does not inherit from NodeList; NamedNodeMaps are not maintained in any particular order. Objects contained in an object implementing NamedNodeMap may also be accessed by an ordinal index, but this is simply to allow convenient enumeration of the contents of a NamedNodeMap, and does not imply that the DOM specifies an order to these Nodes.
  4653. * NamedNodeMap objects in the DOM are live.
  4654. * used for attributes or DocumentType entities
  4655. */
  4656. function NamedNodeMap() {
  4657. };
  4658. function _findNodeIndex(list,node){
  4659. var i = list.length;
  4660. while(i--){
  4661. if(list[i] === node){return i}
  4662. }
  4663. }
  4664. function _addNamedNode(el,list,newAttr,oldAttr){
  4665. if(oldAttr){
  4666. list[_findNodeIndex(list,oldAttr)] = newAttr;
  4667. }else{
  4668. list[list.length++] = newAttr;
  4669. }
  4670. if(el){
  4671. newAttr.ownerElement = el;
  4672. var doc = el.ownerDocument;
  4673. if(doc){
  4674. oldAttr && _onRemoveAttribute(doc,el,oldAttr);
  4675. _onAddAttribute(doc,el,newAttr);
  4676. }
  4677. }
  4678. }
  4679. function _removeNamedNode(el,list,attr){
  4680. //console.log('remove attr:'+attr)
  4681. var i = _findNodeIndex(list,attr);
  4682. if(i>=0){
  4683. var lastIndex = list.length-1
  4684. while(i<lastIndex){
  4685. list[i] = list[++i]
  4686. }
  4687. list.length = lastIndex;
  4688. if(el){
  4689. var doc = el.ownerDocument;
  4690. if(doc){
  4691. _onRemoveAttribute(doc,el,attr);
  4692. attr.ownerElement = null;
  4693. }
  4694. }
  4695. }else{
  4696. throw DOMException(NOT_FOUND_ERR,new Error(el.tagName+'@'+attr))
  4697. }
  4698. }
  4699. NamedNodeMap.prototype = {
  4700. length:0,
  4701. item:NodeList.prototype.item,
  4702. getNamedItem: function(key) {
  4703. // if(key.indexOf(':')>0 || key == 'xmlns'){
  4704. // return null;
  4705. // }
  4706. //console.log()
  4707. var i = this.length;
  4708. while(i--){
  4709. var attr = this[i];
  4710. //console.log(attr.nodeName,key)
  4711. if(attr.nodeName == key){
  4712. return attr;
  4713. }
  4714. }
  4715. },
  4716. setNamedItem: function(attr) {
  4717. var el = attr.ownerElement;
  4718. if(el && el!=this._ownerElement){
  4719. throw new DOMException(INUSE_ATTRIBUTE_ERR);
  4720. }
  4721. var oldAttr = this.getNamedItem(attr.nodeName);
  4722. _addNamedNode(this._ownerElement,this,attr,oldAttr);
  4723. return oldAttr;
  4724. },
  4725. /* returns Node */
  4726. setNamedItemNS: function(attr) {// raises: WRONG_DOCUMENT_ERR,NO_MODIFICATION_ALLOWED_ERR,INUSE_ATTRIBUTE_ERR
  4727. var el = attr.ownerElement, oldAttr;
  4728. if(el && el!=this._ownerElement){
  4729. throw new DOMException(INUSE_ATTRIBUTE_ERR);
  4730. }
  4731. oldAttr = this.getNamedItemNS(attr.namespaceURI,attr.localName);
  4732. _addNamedNode(this._ownerElement,this,attr,oldAttr);
  4733. return oldAttr;
  4734. },
  4735. /* returns Node */
  4736. removeNamedItem: function(key) {
  4737. var attr = this.getNamedItem(key);
  4738. _removeNamedNode(this._ownerElement,this,attr);
  4739. return attr;
  4740. },// raises: NOT_FOUND_ERR,NO_MODIFICATION_ALLOWED_ERR
  4741. //for level2
  4742. removeNamedItemNS:function(namespaceURI,localName){
  4743. var attr = this.getNamedItemNS(namespaceURI,localName);
  4744. _removeNamedNode(this._ownerElement,this,attr);
  4745. return attr;
  4746. },
  4747. getNamedItemNS: function(namespaceURI, localName) {
  4748. var i = this.length;
  4749. while(i--){
  4750. var node = this[i];
  4751. if(node.localName == localName && node.namespaceURI == namespaceURI){
  4752. return node;
  4753. }
  4754. }
  4755. return null;
  4756. }
  4757. };
  4758. /**
  4759. * @see http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html#ID-102161490
  4760. */
  4761. function DOMImplementation(/* Object */ features) {
  4762. this._features = {};
  4763. if (features) {
  4764. for (var feature in features) {
  4765. this._features = features[feature];
  4766. }
  4767. }
  4768. };
  4769. DOMImplementation.prototype = {
  4770. hasFeature: function(/* string */ feature, /* string */ version) {
  4771. var versions = this._features[feature.toLowerCase()];
  4772. if (versions && (!version || version in versions)) {
  4773. return true;
  4774. } else {
  4775. return false;
  4776. }
  4777. },
  4778. // Introduced in DOM Level 2:
  4779. createDocument:function(namespaceURI, qualifiedName, doctype){// raises:INVALID_CHARACTER_ERR,NAMESPACE_ERR,WRONG_DOCUMENT_ERR
  4780. var doc = new Document();
  4781. doc.implementation = this;
  4782. doc.childNodes = new NodeList();
  4783. doc.doctype = doctype;
  4784. if(doctype){
  4785. doc.appendChild(doctype);
  4786. }
  4787. if(qualifiedName){
  4788. var root = doc.createElementNS(namespaceURI,qualifiedName);
  4789. doc.appendChild(root);
  4790. }
  4791. return doc;
  4792. },
  4793. // Introduced in DOM Level 2:
  4794. createDocumentType:function(qualifiedName, publicId, systemId){// raises:INVALID_CHARACTER_ERR,NAMESPACE_ERR
  4795. var node = new DocumentType();
  4796. node.name = qualifiedName;
  4797. node.nodeName = qualifiedName;
  4798. node.publicId = publicId;
  4799. node.systemId = systemId;
  4800. // Introduced in DOM Level 2:
  4801. //readonly attribute DOMString internalSubset;
  4802. //TODO:..
  4803. // readonly attribute NamedNodeMap entities;
  4804. // readonly attribute NamedNodeMap notations;
  4805. return node;
  4806. }
  4807. };
  4808. /**
  4809. * @see http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-1950641247
  4810. */
  4811. function Node() {
  4812. };
  4813. Node.prototype = {
  4814. firstChild : null,
  4815. lastChild : null,
  4816. previousSibling : null,
  4817. nextSibling : null,
  4818. attributes : null,
  4819. parentNode : null,
  4820. childNodes : null,
  4821. ownerDocument : null,
  4822. nodeValue : null,
  4823. namespaceURI : null,
  4824. prefix : null,
  4825. localName : null,
  4826. // Modified in DOM Level 2:
  4827. insertBefore:function(newChild, refChild){//raises
  4828. return _insertBefore(this,newChild,refChild);
  4829. },
  4830. replaceChild:function(newChild, oldChild){//raises
  4831. this.insertBefore(newChild,oldChild);
  4832. if(oldChild){
  4833. this.removeChild(oldChild);
  4834. }
  4835. },
  4836. removeChild:function(oldChild){
  4837. return _removeChild(this,oldChild);
  4838. },
  4839. appendChild:function(newChild){
  4840. return this.insertBefore(newChild,null);
  4841. },
  4842. hasChildNodes:function(){
  4843. return this.firstChild != null;
  4844. },
  4845. cloneNode:function(deep){
  4846. return cloneNode(this.ownerDocument||this,this,deep);
  4847. },
  4848. // Modified in DOM Level 2:
  4849. normalize:function(){
  4850. var child = this.firstChild;
  4851. while(child){
  4852. var next = child.nextSibling;
  4853. if(next && next.nodeType == TEXT_NODE && child.nodeType == TEXT_NODE){
  4854. this.removeChild(next);
  4855. child.appendData(next.data);
  4856. }else{
  4857. child.normalize();
  4858. child = next;
  4859. }
  4860. }
  4861. },
  4862. // Introduced in DOM Level 2:
  4863. isSupported:function(feature, version){
  4864. return this.ownerDocument.implementation.hasFeature(feature,version);
  4865. },
  4866. // Introduced in DOM Level 2:
  4867. hasAttributes:function(){
  4868. return this.attributes.length>0;
  4869. },
  4870. lookupPrefix:function(namespaceURI){
  4871. var el = this;
  4872. while(el){
  4873. var map = el._nsMap;
  4874. //console.dir(map)
  4875. if(map){
  4876. for(var n in map){
  4877. if(map[n] == namespaceURI){
  4878. return n;
  4879. }
  4880. }
  4881. }
  4882. el = el.nodeType == ATTRIBUTE_NODE?el.ownerDocument : el.parentNode;
  4883. }
  4884. return null;
  4885. },
  4886. // Introduced in DOM Level 3:
  4887. lookupNamespaceURI:function(prefix){
  4888. var el = this;
  4889. while(el){
  4890. var map = el._nsMap;
  4891. //console.dir(map)
  4892. if(map){
  4893. if(prefix in map){
  4894. return map[prefix] ;
  4895. }
  4896. }
  4897. el = el.nodeType == ATTRIBUTE_NODE?el.ownerDocument : el.parentNode;
  4898. }
  4899. return null;
  4900. },
  4901. // Introduced in DOM Level 3:
  4902. isDefaultNamespace:function(namespaceURI){
  4903. var prefix = this.lookupPrefix(namespaceURI);
  4904. return prefix == null;
  4905. }
  4906. };
  4907. function _xmlEncoder(c){
  4908. return c == '<' && '&lt;' ||
  4909. c == '>' && '&gt;' ||
  4910. c == '&' && '&amp;' ||
  4911. c == '"' && '&quot;' ||
  4912. '&#'+c.charCodeAt()+';'
  4913. }
  4914. copy(NodeType,Node);
  4915. copy(NodeType,Node.prototype);
  4916. /**
  4917. * @param callback return true for continue,false for break
  4918. * @return boolean true: break visit;
  4919. */
  4920. function _visitNode(node,callback){
  4921. if(callback(node)){
  4922. return true;
  4923. }
  4924. if(node = node.firstChild){
  4925. do{
  4926. if(_visitNode(node,callback)){return true}
  4927. }while(node=node.nextSibling)
  4928. }
  4929. }
  4930. function Document(){
  4931. }
  4932. function _onAddAttribute(doc,el,newAttr){
  4933. doc && doc._inc++;
  4934. var ns = newAttr.namespaceURI ;
  4935. if(ns == 'http://www.w3.org/2000/xmlns/'){
  4936. //update namespace
  4937. el._nsMap[newAttr.prefix?newAttr.localName:''] = newAttr.value
  4938. }
  4939. }
  4940. function _onRemoveAttribute(doc,el,newAttr,remove){
  4941. doc && doc._inc++;
  4942. var ns = newAttr.namespaceURI ;
  4943. if(ns == 'http://www.w3.org/2000/xmlns/'){
  4944. //update namespace
  4945. delete el._nsMap[newAttr.prefix?newAttr.localName:'']
  4946. }
  4947. }
  4948. function _onUpdateChild(doc,el,newChild){
  4949. if(doc && doc._inc){
  4950. doc._inc++;
  4951. //update childNodes
  4952. var cs = el.childNodes;
  4953. if(newChild){
  4954. cs[cs.length++] = newChild;
  4955. }else{
  4956. //console.log(1)
  4957. var child = el.firstChild;
  4958. var i = 0;
  4959. while(child){
  4960. cs[i++] = child;
  4961. child =child.nextSibling;
  4962. }
  4963. cs.length = i;
  4964. }
  4965. }
  4966. }
  4967. /**
  4968. * attributes;
  4969. * children;
  4970. *
  4971. * writeable properties:
  4972. * nodeValue,Attr:value,CharacterData:data
  4973. * prefix
  4974. */
  4975. function _removeChild(parentNode,child){
  4976. var previous = child.previousSibling;
  4977. var next = child.nextSibling;
  4978. if(previous){
  4979. previous.nextSibling = next;
  4980. }else{
  4981. parentNode.firstChild = next
  4982. }
  4983. if(next){
  4984. next.previousSibling = previous;
  4985. }else{
  4986. parentNode.lastChild = previous;
  4987. }
  4988. _onUpdateChild(parentNode.ownerDocument,parentNode);
  4989. return child;
  4990. }
  4991. /**
  4992. * preformance key(refChild == null)
  4993. */
  4994. function _insertBefore(parentNode,newChild,nextChild){
  4995. var cp = newChild.parentNode;
  4996. if(cp){
  4997. cp.removeChild(newChild);//remove and update
  4998. }
  4999. if(newChild.nodeType === DOCUMENT_FRAGMENT_NODE){
  5000. var newFirst = newChild.firstChild;
  5001. if (newFirst == null) {
  5002. return newChild;
  5003. }
  5004. var newLast = newChild.lastChild;
  5005. }else{
  5006. newFirst = newLast = newChild;
  5007. }
  5008. var pre = nextChild ? nextChild.previousSibling : parentNode.lastChild;
  5009. newFirst.previousSibling = pre;
  5010. newLast.nextSibling = nextChild;
  5011. if(pre){
  5012. pre.nextSibling = newFirst;
  5013. }else{
  5014. parentNode.firstChild = newFirst;
  5015. }
  5016. if(nextChild == null){
  5017. parentNode.lastChild = newLast;
  5018. }else{
  5019. nextChild.previousSibling = newLast;
  5020. }
  5021. do{
  5022. newFirst.parentNode = parentNode;
  5023. }while(newFirst !== newLast && (newFirst= newFirst.nextSibling))
  5024. _onUpdateChild(parentNode.ownerDocument||parentNode,parentNode);
  5025. //console.log(parentNode.lastChild.nextSibling == null)
  5026. if (newChild.nodeType == DOCUMENT_FRAGMENT_NODE) {
  5027. newChild.firstChild = newChild.lastChild = null;
  5028. }
  5029. return newChild;
  5030. }
  5031. function _appendSingleChild(parentNode,newChild){
  5032. var cp = newChild.parentNode;
  5033. if(cp){
  5034. var pre = parentNode.lastChild;
  5035. cp.removeChild(newChild);//remove and update
  5036. var pre = parentNode.lastChild;
  5037. }
  5038. var pre = parentNode.lastChild;
  5039. newChild.parentNode = parentNode;
  5040. newChild.previousSibling = pre;
  5041. newChild.nextSibling = null;
  5042. if(pre){
  5043. pre.nextSibling = newChild;
  5044. }else{
  5045. parentNode.firstChild = newChild;
  5046. }
  5047. parentNode.lastChild = newChild;
  5048. _onUpdateChild(parentNode.ownerDocument,parentNode,newChild);
  5049. return newChild;
  5050. //console.log("__aa",parentNode.lastChild.nextSibling == null)
  5051. }
  5052. Document.prototype = {
  5053. //implementation : null,
  5054. nodeName : '#document',
  5055. nodeType : DOCUMENT_NODE,
  5056. doctype : null,
  5057. documentElement : null,
  5058. _inc : 1,
  5059. insertBefore : function(newChild, refChild){//raises
  5060. if(newChild.nodeType == DOCUMENT_FRAGMENT_NODE){
  5061. var child = newChild.firstChild;
  5062. while(child){
  5063. var next = child.nextSibling;
  5064. this.insertBefore(child,refChild);
  5065. child = next;
  5066. }
  5067. return newChild;
  5068. }
  5069. if(this.documentElement == null && newChild.nodeType == ELEMENT_NODE){
  5070. this.documentElement = newChild;
  5071. }
  5072. return _insertBefore(this,newChild,refChild),(newChild.ownerDocument = this),newChild;
  5073. },
  5074. removeChild : function(oldChild){
  5075. if(this.documentElement == oldChild){
  5076. this.documentElement = null;
  5077. }
  5078. return _removeChild(this,oldChild);
  5079. },
  5080. // Introduced in DOM Level 2:
  5081. importNode : function(importedNode,deep){
  5082. return importNode(this,importedNode,deep);
  5083. },
  5084. // Introduced in DOM Level 2:
  5085. getElementById : function(id){
  5086. var rtv = null;
  5087. _visitNode(this.documentElement,function(node){
  5088. if(node.nodeType == ELEMENT_NODE){
  5089. if(node.getAttribute('id') == id){
  5090. rtv = node;
  5091. return true;
  5092. }
  5093. }
  5094. })
  5095. return rtv;
  5096. },
  5097. //document factory method:
  5098. createElement : function(tagName){
  5099. var node = new Element();
  5100. node.ownerDocument = this;
  5101. node.nodeName = tagName;
  5102. node.tagName = tagName;
  5103. node.childNodes = new NodeList();
  5104. var attrs = node.attributes = new NamedNodeMap();
  5105. attrs._ownerElement = node;
  5106. return node;
  5107. },
  5108. createDocumentFragment : function(){
  5109. var node = new DocumentFragment();
  5110. node.ownerDocument = this;
  5111. node.childNodes = new NodeList();
  5112. return node;
  5113. },
  5114. createTextNode : function(data){
  5115. var node = new Text();
  5116. node.ownerDocument = this;
  5117. node.appendData(data)
  5118. return node;
  5119. },
  5120. createComment : function(data){
  5121. var node = new Comment();
  5122. node.ownerDocument = this;
  5123. node.appendData(data)
  5124. return node;
  5125. },
  5126. createCDATASection : function(data){
  5127. var node = new CDATASection();
  5128. node.ownerDocument = this;
  5129. node.appendData(data)
  5130. return node;
  5131. },
  5132. createProcessingInstruction : function(target,data){
  5133. var node = new ProcessingInstruction();
  5134. node.ownerDocument = this;
  5135. node.tagName = node.target = target;
  5136. node.nodeValue= node.data = data;
  5137. return node;
  5138. },
  5139. createAttribute : function(name){
  5140. var node = new Attr();
  5141. node.ownerDocument = this;
  5142. node.name = name;
  5143. node.nodeName = name;
  5144. node.localName = name;
  5145. node.specified = true;
  5146. return node;
  5147. },
  5148. createEntityReference : function(name){
  5149. var node = new EntityReference();
  5150. node.ownerDocument = this;
  5151. node.nodeName = name;
  5152. return node;
  5153. },
  5154. // Introduced in DOM Level 2:
  5155. createElementNS : function(namespaceURI,qualifiedName){
  5156. var node = new Element();
  5157. var pl = qualifiedName.split(':');
  5158. var attrs = node.attributes = new NamedNodeMap();
  5159. node.childNodes = new NodeList();
  5160. node.ownerDocument = this;
  5161. node.nodeName = qualifiedName;
  5162. node.tagName = qualifiedName;
  5163. node.namespaceURI = namespaceURI;
  5164. if(pl.length == 2){
  5165. node.prefix = pl[0];
  5166. node.localName = pl[1];
  5167. }else{
  5168. //el.prefix = null;
  5169. node.localName = qualifiedName;
  5170. }
  5171. attrs._ownerElement = node;
  5172. return node;
  5173. },
  5174. // Introduced in DOM Level 2:
  5175. createAttributeNS : function(namespaceURI,qualifiedName){
  5176. var node = new Attr();
  5177. var pl = qualifiedName.split(':');
  5178. node.ownerDocument = this;
  5179. node.nodeName = qualifiedName;
  5180. node.name = qualifiedName;
  5181. node.namespaceURI = namespaceURI;
  5182. node.specified = true;
  5183. if(pl.length == 2){
  5184. node.prefix = pl[0];
  5185. node.localName = pl[1];
  5186. }else{
  5187. //el.prefix = null;
  5188. node.localName = qualifiedName;
  5189. }
  5190. return node;
  5191. }
  5192. };
  5193. _extends(Document,Node);
  5194. function Element() {
  5195. this._nsMap = {};
  5196. };
  5197. Element.prototype = {
  5198. nodeType : ELEMENT_NODE,
  5199. hasAttribute : function(name){
  5200. return this.getAttributeNode(name)!=null;
  5201. },
  5202. getAttribute : function(name){
  5203. var attr = this.getAttributeNode(name);
  5204. return attr && attr.value || '';
  5205. },
  5206. getAttributeNode : function(name){
  5207. return this.attributes.getNamedItem(name);
  5208. },
  5209. setAttribute : function(name, value){
  5210. var attr = this.ownerDocument.createAttribute(name);
  5211. attr.value = attr.nodeValue = "" + value;
  5212. this.setAttributeNode(attr)
  5213. },
  5214. removeAttribute : function(name){
  5215. var attr = this.getAttributeNode(name)
  5216. attr && this.removeAttributeNode(attr);
  5217. },
  5218. //four real opeartion method
  5219. appendChild:function(newChild){
  5220. if(newChild.nodeType === DOCUMENT_FRAGMENT_NODE){
  5221. return this.insertBefore(newChild,null);
  5222. }else{
  5223. return _appendSingleChild(this,newChild);
  5224. }
  5225. },
  5226. setAttributeNode : function(newAttr){
  5227. return this.attributes.setNamedItem(newAttr);
  5228. },
  5229. setAttributeNodeNS : function(newAttr){
  5230. return this.attributes.setNamedItemNS(newAttr);
  5231. },
  5232. removeAttributeNode : function(oldAttr){
  5233. //console.log(this == oldAttr.ownerElement)
  5234. return this.attributes.removeNamedItem(oldAttr.nodeName);
  5235. },
  5236. //get real attribute name,and remove it by removeAttributeNode
  5237. removeAttributeNS : function(namespaceURI, localName){
  5238. var old = this.getAttributeNodeNS(namespaceURI, localName);
  5239. old && this.removeAttributeNode(old);
  5240. },
  5241. hasAttributeNS : function(namespaceURI, localName){
  5242. return this.getAttributeNodeNS(namespaceURI, localName)!=null;
  5243. },
  5244. getAttributeNS : function(namespaceURI, localName){
  5245. var attr = this.getAttributeNodeNS(namespaceURI, localName);
  5246. return attr && attr.value || '';
  5247. },
  5248. setAttributeNS : function(namespaceURI, qualifiedName, value){
  5249. var attr = this.ownerDocument.createAttributeNS(namespaceURI, qualifiedName);
  5250. attr.value = attr.nodeValue = "" + value;
  5251. this.setAttributeNode(attr)
  5252. },
  5253. getAttributeNodeNS : function(namespaceURI, localName){
  5254. return this.attributes.getNamedItemNS(namespaceURI, localName);
  5255. },
  5256. getElementsByTagName : function(tagName){
  5257. return new LiveNodeList(this,function(base){
  5258. var ls = [];
  5259. _visitNode(base,function(node){
  5260. if(node !== base && node.nodeType == ELEMENT_NODE && (tagName === '*' || node.tagName == tagName)){
  5261. ls.push(node);
  5262. }
  5263. });
  5264. return ls;
  5265. });
  5266. },
  5267. getElementsByTagNameNS : function(namespaceURI, localName){
  5268. return new LiveNodeList(this,function(base){
  5269. var ls = [];
  5270. _visitNode(base,function(node){
  5271. if(node !== base && node.nodeType === ELEMENT_NODE && (namespaceURI === '*' || node.namespaceURI === namespaceURI) && (localName === '*' || node.localName == localName)){
  5272. ls.push(node);
  5273. }
  5274. });
  5275. return ls;
  5276. });
  5277. }
  5278. };
  5279. Document.prototype.getElementsByTagName = Element.prototype.getElementsByTagName;
  5280. Document.prototype.getElementsByTagNameNS = Element.prototype.getElementsByTagNameNS;
  5281. _extends(Element,Node);
  5282. function Attr() {
  5283. };
  5284. Attr.prototype.nodeType = ATTRIBUTE_NODE;
  5285. _extends(Attr,Node);
  5286. function CharacterData() {
  5287. };
  5288. CharacterData.prototype = {
  5289. data : '',
  5290. substringData : function(offset, count) {
  5291. return this.data.substring(offset, offset+count);
  5292. },
  5293. appendData: function(text) {
  5294. text = this.data+text;
  5295. this.nodeValue = this.data = text;
  5296. this.length = text.length;
  5297. },
  5298. insertData: function(offset,text) {
  5299. this.replaceData(offset,0,text);
  5300. },
  5301. appendChild:function(newChild){
  5302. throw new Error(ExceptionMessage[HIERARCHY_REQUEST_ERR])
  5303. },
  5304. deleteData: function(offset, count) {
  5305. this.replaceData(offset,count,"");
  5306. },
  5307. replaceData: function(offset, count, text) {
  5308. var start = this.data.substring(0,offset);
  5309. var end = this.data.substring(offset+count);
  5310. text = start + text + end;
  5311. this.nodeValue = this.data = text;
  5312. this.length = text.length;
  5313. }
  5314. }
  5315. _extends(CharacterData,Node);
  5316. function Text() {
  5317. };
  5318. Text.prototype = {
  5319. nodeName : "#text",
  5320. nodeType : TEXT_NODE,
  5321. splitText : function(offset) {
  5322. var text = this.data;
  5323. var newText = text.substring(offset);
  5324. text = text.substring(0, offset);
  5325. this.data = this.nodeValue = text;
  5326. this.length = text.length;
  5327. var newNode = this.ownerDocument.createTextNode(newText);
  5328. if(this.parentNode){
  5329. this.parentNode.insertBefore(newNode, this.nextSibling);
  5330. }
  5331. return newNode;
  5332. }
  5333. }
  5334. _extends(Text,CharacterData);
  5335. function Comment() {
  5336. };
  5337. Comment.prototype = {
  5338. nodeName : "#comment",
  5339. nodeType : COMMENT_NODE
  5340. }
  5341. _extends(Comment,CharacterData);
  5342. function CDATASection() {
  5343. };
  5344. CDATASection.prototype = {
  5345. nodeName : "#cdata-section",
  5346. nodeType : CDATA_SECTION_NODE
  5347. }
  5348. _extends(CDATASection,CharacterData);
  5349. function DocumentType() {
  5350. };
  5351. DocumentType.prototype.nodeType = DOCUMENT_TYPE_NODE;
  5352. _extends(DocumentType,Node);
  5353. function Notation() {
  5354. };
  5355. Notation.prototype.nodeType = NOTATION_NODE;
  5356. _extends(Notation,Node);
  5357. function Entity() {
  5358. };
  5359. Entity.prototype.nodeType = ENTITY_NODE;
  5360. _extends(Entity,Node);
  5361. function EntityReference() {
  5362. };
  5363. EntityReference.prototype.nodeType = ENTITY_REFERENCE_NODE;
  5364. _extends(EntityReference,Node);
  5365. function DocumentFragment() {
  5366. };
  5367. DocumentFragment.prototype.nodeName = "#document-fragment";
  5368. DocumentFragment.prototype.nodeType = DOCUMENT_FRAGMENT_NODE;
  5369. _extends(DocumentFragment,Node);
  5370. function ProcessingInstruction() {
  5371. }
  5372. ProcessingInstruction.prototype.nodeType = PROCESSING_INSTRUCTION_NODE;
  5373. _extends(ProcessingInstruction,Node);
  5374. function XMLSerializer(){}
  5375. XMLSerializer.prototype.serializeToString = function(node,isHtml,nodeFilter){
  5376. return nodeSerializeToString.call(node,isHtml,nodeFilter);
  5377. }
  5378. Node.prototype.toString = nodeSerializeToString;
  5379. function nodeSerializeToString(isHtml,nodeFilter){
  5380. var buf = [];
  5381. var refNode = this.nodeType == 9?this.documentElement:this;
  5382. var prefix = refNode.prefix;
  5383. var uri = refNode.namespaceURI;
  5384. if(uri && prefix == null){
  5385. //console.log(prefix)
  5386. var prefix = refNode.lookupPrefix(uri);
  5387. if(prefix == null){
  5388. //isHTML = true;
  5389. var visibleNamespaces=[
  5390. {namespace:uri,prefix:null}
  5391. //{namespace:uri,prefix:''}
  5392. ]
  5393. }
  5394. }
  5395. serializeToString(this,buf,isHtml,nodeFilter,visibleNamespaces);
  5396. //console.log('###',this.nodeType,uri,prefix,buf.join(''))
  5397. return buf.join('');
  5398. }
  5399. function needNamespaceDefine(node,isHTML, visibleNamespaces) {
  5400. var prefix = node.prefix||'';
  5401. var uri = node.namespaceURI;
  5402. if (!prefix && !uri){
  5403. return false;
  5404. }
  5405. if (prefix === "xml" && uri === "http://www.w3.org/XML/1998/namespace"
  5406. || uri == 'http://www.w3.org/2000/xmlns/'){
  5407. return false;
  5408. }
  5409. var i = visibleNamespaces.length
  5410. //console.log('@@@@',node.tagName,prefix,uri,visibleNamespaces)
  5411. while (i--) {
  5412. var ns = visibleNamespaces[i];
  5413. // get namespace prefix
  5414. //console.log(node.nodeType,node.tagName,ns.prefix,prefix)
  5415. if (ns.prefix == prefix){
  5416. return ns.namespace != uri;
  5417. }
  5418. }
  5419. //console.log(isHTML,uri,prefix=='')
  5420. //if(isHTML && prefix ==null && uri == 'http://www.w3.org/1999/xhtml'){
  5421. // return false;
  5422. //}
  5423. //node.flag = '11111'
  5424. //console.error(3,true,node.flag,node.prefix,node.namespaceURI)
  5425. return true;
  5426. }
  5427. function serializeToString(node,buf,isHTML,nodeFilter,visibleNamespaces){
  5428. if(nodeFilter){
  5429. node = nodeFilter(node);
  5430. if(node){
  5431. if(typeof node == 'string'){
  5432. buf.push(node);
  5433. return;
  5434. }
  5435. }else{
  5436. return;
  5437. }
  5438. //buf.sort.apply(attrs, attributeSorter);
  5439. }
  5440. switch(node.nodeType){
  5441. case ELEMENT_NODE:
  5442. if (!visibleNamespaces) visibleNamespaces = [];
  5443. var startVisibleNamespaces = visibleNamespaces.length;
  5444. var attrs = node.attributes;
  5445. var len = attrs.length;
  5446. var child = node.firstChild;
  5447. var nodeName = node.tagName;
  5448. isHTML = (htmlns === node.namespaceURI) ||isHTML
  5449. buf.push('<',nodeName);
  5450. for(var i=0;i<len;i++){
  5451. // add namespaces for attributes
  5452. var attr = attrs.item(i);
  5453. if (attr.prefix == 'xmlns') {
  5454. visibleNamespaces.push({ prefix: attr.localName, namespace: attr.value });
  5455. }else if(attr.nodeName == 'xmlns'){
  5456. visibleNamespaces.push({ prefix: '', namespace: attr.value });
  5457. }
  5458. }
  5459. for(var i=0;i<len;i++){
  5460. var attr = attrs.item(i);
  5461. if (needNamespaceDefine(attr,isHTML, visibleNamespaces)) {
  5462. var prefix = attr.prefix||'';
  5463. var uri = attr.namespaceURI;
  5464. var ns = prefix ? ' xmlns:' + prefix : " xmlns";
  5465. buf.push(ns, '="' , uri , '"');
  5466. visibleNamespaces.push({ prefix: prefix, namespace:uri });
  5467. }
  5468. serializeToString(attr,buf,isHTML,nodeFilter,visibleNamespaces);
  5469. }
  5470. // add namespace for current node
  5471. if (needNamespaceDefine(node,isHTML, visibleNamespaces)) {
  5472. var prefix = node.prefix||'';
  5473. var uri = node.namespaceURI;
  5474. var ns = prefix ? ' xmlns:' + prefix : " xmlns";
  5475. buf.push(ns, '="' , uri , '"');
  5476. visibleNamespaces.push({ prefix: prefix, namespace:uri });
  5477. }
  5478. if(child || isHTML && !/^(?:meta|link|img|br|hr|input)$/i.test(nodeName)){
  5479. buf.push('>');
  5480. //if is cdata child node
  5481. if(isHTML && /^script$/i.test(nodeName)){
  5482. while(child){
  5483. if(child.data){
  5484. buf.push(child.data);
  5485. }else{
  5486. serializeToString(child,buf,isHTML,nodeFilter,visibleNamespaces);
  5487. }
  5488. child = child.nextSibling;
  5489. }
  5490. }else
  5491. {
  5492. while(child){
  5493. serializeToString(child,buf,isHTML,nodeFilter,visibleNamespaces);
  5494. child = child.nextSibling;
  5495. }
  5496. }
  5497. buf.push('</',nodeName,'>');
  5498. }else{
  5499. buf.push('/>');
  5500. }
  5501. // remove added visible namespaces
  5502. //visibleNamespaces.length = startVisibleNamespaces;
  5503. return;
  5504. case DOCUMENT_NODE:
  5505. case DOCUMENT_FRAGMENT_NODE:
  5506. var child = node.firstChild;
  5507. while(child){
  5508. serializeToString(child,buf,isHTML,nodeFilter,visibleNamespaces);
  5509. child = child.nextSibling;
  5510. }
  5511. return;
  5512. case ATTRIBUTE_NODE:
  5513. return buf.push(' ',node.name,'="',node.value.replace(/[<&"]/g,_xmlEncoder),'"');
  5514. case TEXT_NODE:
  5515. return buf.push(node.data.replace(/[<&]/g,_xmlEncoder));
  5516. case CDATA_SECTION_NODE:
  5517. return buf.push( '<![CDATA[',node.data,']]>');
  5518. case COMMENT_NODE:
  5519. return buf.push( "<!--",node.data,"-->");
  5520. case DOCUMENT_TYPE_NODE:
  5521. var pubid = node.publicId;
  5522. var sysid = node.systemId;
  5523. buf.push('<!DOCTYPE ',node.name);
  5524. if(pubid){
  5525. buf.push(' PUBLIC "',pubid);
  5526. if (sysid && sysid!='.') {
  5527. buf.push( '" "',sysid);
  5528. }
  5529. buf.push('">');
  5530. }else if(sysid && sysid!='.'){
  5531. buf.push(' SYSTEM "',sysid,'">');
  5532. }else{
  5533. var sub = node.internalSubset;
  5534. if(sub){
  5535. buf.push(" [",sub,"]");
  5536. }
  5537. buf.push(">");
  5538. }
  5539. return;
  5540. case PROCESSING_INSTRUCTION_NODE:
  5541. return buf.push( "<?",node.target," ",node.data,"?>");
  5542. case ENTITY_REFERENCE_NODE:
  5543. return buf.push( '&',node.nodeName,';');
  5544. //case ENTITY_NODE:
  5545. //case NOTATION_NODE:
  5546. default:
  5547. buf.push('??',node.nodeName);
  5548. }
  5549. }
  5550. function importNode(doc,node,deep){
  5551. var node2;
  5552. switch (node.nodeType) {
  5553. case ELEMENT_NODE:
  5554. node2 = node.cloneNode(false);
  5555. node2.ownerDocument = doc;
  5556. //var attrs = node2.attributes;
  5557. //var len = attrs.length;
  5558. //for(var i=0;i<len;i++){
  5559. //node2.setAttributeNodeNS(importNode(doc,attrs.item(i),deep));
  5560. //}
  5561. case DOCUMENT_FRAGMENT_NODE:
  5562. break;
  5563. case ATTRIBUTE_NODE:
  5564. deep = true;
  5565. break;
  5566. //case ENTITY_REFERENCE_NODE:
  5567. //case PROCESSING_INSTRUCTION_NODE:
  5568. ////case TEXT_NODE:
  5569. //case CDATA_SECTION_NODE:
  5570. //case COMMENT_NODE:
  5571. // deep = false;
  5572. // break;
  5573. //case DOCUMENT_NODE:
  5574. //case DOCUMENT_TYPE_NODE:
  5575. //cannot be imported.
  5576. //case ENTITY_NODE:
  5577. //case NOTATION_NODE:
  5578. //can not hit in level3
  5579. //default:throw e;
  5580. }
  5581. if(!node2){
  5582. node2 = node.cloneNode(false);//false
  5583. }
  5584. node2.ownerDocument = doc;
  5585. node2.parentNode = null;
  5586. if(deep){
  5587. var child = node.firstChild;
  5588. while(child){
  5589. node2.appendChild(importNode(doc,child,deep));
  5590. child = child.nextSibling;
  5591. }
  5592. }
  5593. return node2;
  5594. }
  5595. //
  5596. //var _relationMap = {firstChild:1,lastChild:1,previousSibling:1,nextSibling:1,
  5597. // attributes:1,childNodes:1,parentNode:1,documentElement:1,doctype,};
  5598. function cloneNode(doc,node,deep){
  5599. var node2 = new node.constructor();
  5600. for(var n in node){
  5601. var v = node[n];
  5602. if(typeof v != 'object' ){
  5603. if(v != node2[n]){
  5604. node2[n] = v;
  5605. }
  5606. }
  5607. }
  5608. if(node.childNodes){
  5609. node2.childNodes = new NodeList();
  5610. }
  5611. node2.ownerDocument = doc;
  5612. switch (node2.nodeType) {
  5613. case ELEMENT_NODE:
  5614. var attrs = node.attributes;
  5615. var attrs2 = node2.attributes = new NamedNodeMap();
  5616. var len = attrs.length
  5617. attrs2._ownerElement = node2;
  5618. for(var i=0;i<len;i++){
  5619. node2.setAttributeNode(cloneNode(doc,attrs.item(i),true));
  5620. }
  5621. break;;
  5622. case ATTRIBUTE_NODE:
  5623. deep = true;
  5624. }
  5625. if(deep){
  5626. var child = node.firstChild;
  5627. while(child){
  5628. node2.appendChild(cloneNode(doc,child,deep));
  5629. child = child.nextSibling;
  5630. }
  5631. }
  5632. return node2;
  5633. }
  5634. function __set__(object,key,value){
  5635. object[key] = value
  5636. }
  5637. //do dynamic
  5638. try{
  5639. if(Object.defineProperty){
  5640. Object.defineProperty(LiveNodeList.prototype,'length',{
  5641. get:function(){
  5642. _updateLiveList(this);
  5643. return this.$$length;
  5644. }
  5645. });
  5646. Object.defineProperty(Node.prototype,'textContent',{
  5647. get:function(){
  5648. return getTextContent(this);
  5649. },
  5650. set:function(data){
  5651. switch(this.nodeType){
  5652. case ELEMENT_NODE:
  5653. case DOCUMENT_FRAGMENT_NODE:
  5654. while(this.firstChild){
  5655. this.removeChild(this.firstChild);
  5656. }
  5657. if(data || String(data)){
  5658. this.appendChild(this.ownerDocument.createTextNode(data));
  5659. }
  5660. break;
  5661. default:
  5662. //TODO:
  5663. this.data = data;
  5664. this.value = data;
  5665. this.nodeValue = data;
  5666. }
  5667. }
  5668. })
  5669. function getTextContent(node){
  5670. switch(node.nodeType){
  5671. case ELEMENT_NODE:
  5672. case DOCUMENT_FRAGMENT_NODE:
  5673. var buf = [];
  5674. node = node.firstChild;
  5675. while(node){
  5676. if(node.nodeType!==7 && node.nodeType !==8){
  5677. buf.push(getTextContent(node));
  5678. }
  5679. node = node.nextSibling;
  5680. }
  5681. return buf.join('');
  5682. default:
  5683. return node.nodeValue;
  5684. }
  5685. }
  5686. __set__ = function(object,key,value){
  5687. //console.log(value)
  5688. object['$$'+key] = value
  5689. }
  5690. }
  5691. }catch(e){//ie8
  5692. }
  5693. //if(typeof require == 'function'){
  5694. exports.DOMImplementation = DOMImplementation;
  5695. exports.XMLSerializer = XMLSerializer;
  5696. //}
  5697. },{}],32:[function(require,module,exports){
  5698. //[4] NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
  5699. //[4a] NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
  5700. //[5] Name ::= NameStartChar (NameChar)*
  5701. var nameStartChar = /[A-Z_a-z\xC0-\xD6\xD8-\xF6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]///\u10000-\uEFFFF
  5702. var nameChar = new RegExp("[\\-\\.0-9"+nameStartChar.source.slice(1,-1)+"\\u00B7\\u0300-\\u036F\\u203F-\\u2040]");
  5703. var tagNamePattern = new RegExp('^'+nameStartChar.source+nameChar.source+'*(?:\:'+nameStartChar.source+nameChar.source+'*)?$');
  5704. //var tagNamePattern = /^[a-zA-Z_][\w\-\.]*(?:\:[a-zA-Z_][\w\-\.]*)?$/
  5705. //var handlers = 'resolveEntity,getExternalSubset,characters,endDocument,endElement,endPrefixMapping,ignorableWhitespace,processingInstruction,setDocumentLocator,skippedEntity,startDocument,startElement,startPrefixMapping,notationDecl,unparsedEntityDecl,error,fatalError,warning,attributeDecl,elementDecl,externalEntityDecl,internalEntityDecl,comment,endCDATA,endDTD,endEntity,startCDATA,startDTD,startEntity'.split(',')
  5706. //S_TAG, S_ATTR, S_EQ, S_ATTR_NOQUOT_VALUE
  5707. //S_ATTR_SPACE, S_ATTR_END, S_TAG_SPACE, S_TAG_CLOSE
  5708. var S_TAG = 0;//tag name offerring
  5709. var S_ATTR = 1;//attr name offerring
  5710. var S_ATTR_SPACE=2;//attr name end and space offer
  5711. var S_EQ = 3;//=space?
  5712. var S_ATTR_NOQUOT_VALUE = 4;//attr value(no quot value only)
  5713. var S_ATTR_END = 5;//attr value end and no space(quot end)
  5714. var S_TAG_SPACE = 6;//(attr value end || tag end ) && (space offer)
  5715. var S_TAG_CLOSE = 7;//closed el<el />
  5716. function XMLReader(){
  5717. }
  5718. XMLReader.prototype = {
  5719. parse:function(source,defaultNSMap,entityMap){
  5720. var domBuilder = this.domBuilder;
  5721. domBuilder.startDocument();
  5722. _copy(defaultNSMap ,defaultNSMap = {})
  5723. parse(source,defaultNSMap,entityMap,
  5724. domBuilder,this.errorHandler);
  5725. domBuilder.endDocument();
  5726. }
  5727. }
  5728. function parse(source,defaultNSMapCopy,entityMap,domBuilder,errorHandler){
  5729. function fixedFromCharCode(code) {
  5730. // String.prototype.fromCharCode does not supports
  5731. // > 2 bytes unicode chars directly
  5732. if (code > 0xffff) {
  5733. code -= 0x10000;
  5734. var surrogate1 = 0xd800 + (code >> 10)
  5735. , surrogate2 = 0xdc00 + (code & 0x3ff);
  5736. return String.fromCharCode(surrogate1, surrogate2);
  5737. } else {
  5738. return String.fromCharCode(code);
  5739. }
  5740. }
  5741. function entityReplacer(a){
  5742. var k = a.slice(1,-1);
  5743. if(k in entityMap){
  5744. return entityMap[k];
  5745. }else if(k.charAt(0) === '#'){
  5746. return fixedFromCharCode(parseInt(k.substr(1).replace('x','0x')))
  5747. }else{
  5748. errorHandler.error('entity not found:'+a);
  5749. return a;
  5750. }
  5751. }
  5752. function appendText(end){//has some bugs
  5753. if(end>start){
  5754. var xt = source.substring(start,end).replace(/&#?\w+;/g,entityReplacer);
  5755. locator&&position(start);
  5756. domBuilder.characters(xt,0,end-start);
  5757. start = end
  5758. }
  5759. }
  5760. function position(p,m){
  5761. while(p>=lineEnd && (m = linePattern.exec(source))){
  5762. lineStart = m.index;
  5763. lineEnd = lineStart + m[0].length;
  5764. locator.lineNumber++;
  5765. //console.log('line++:',locator,startPos,endPos)
  5766. }
  5767. locator.columnNumber = p-lineStart+1;
  5768. }
  5769. var lineStart = 0;
  5770. var lineEnd = 0;
  5771. var linePattern = /.*(?:\r\n?|\n)|.*$/g
  5772. var locator = domBuilder.locator;
  5773. var parseStack = [{currentNSMap:defaultNSMapCopy}]
  5774. var closeMap = {};
  5775. var start = 0;
  5776. while(true){
  5777. try{
  5778. var tagStart = source.indexOf('<',start);
  5779. if(tagStart<0){
  5780. if(!source.substr(start).match(/^\s*$/)){
  5781. var doc = domBuilder.doc;
  5782. var text = doc.createTextNode(source.substr(start));
  5783. doc.appendChild(text);
  5784. domBuilder.currentElement = text;
  5785. }
  5786. return;
  5787. }
  5788. if(tagStart>start){
  5789. appendText(tagStart);
  5790. }
  5791. switch(source.charAt(tagStart+1)){
  5792. case '/':
  5793. var end = source.indexOf('>',tagStart+3);
  5794. var tagName = source.substring(tagStart+2,end);
  5795. var config = parseStack.pop();
  5796. if(end<0){
  5797. tagName = source.substring(tagStart+2).replace(/[\s<].*/,'');
  5798. //console.error('#@@@@@@'+tagName)
  5799. errorHandler.error("end tag name: "+tagName+' is not complete:'+config.tagName);
  5800. end = tagStart+1+tagName.length;
  5801. }else if(tagName.match(/\s</)){
  5802. tagName = tagName.replace(/[\s<].*/,'');
  5803. errorHandler.error("end tag name: "+tagName+' maybe not complete');
  5804. end = tagStart+1+tagName.length;
  5805. }
  5806. //console.error(parseStack.length,parseStack)
  5807. //console.error(config);
  5808. var localNSMap = config.localNSMap;
  5809. var endMatch = config.tagName == tagName;
  5810. var endIgnoreCaseMach = endMatch || config.tagName&&config.tagName.toLowerCase() == tagName.toLowerCase()
  5811. if(endIgnoreCaseMach){
  5812. domBuilder.endElement(config.uri,config.localName,tagName);
  5813. if(localNSMap){
  5814. for(var prefix in localNSMap){
  5815. domBuilder.endPrefixMapping(prefix) ;
  5816. }
  5817. }
  5818. if(!endMatch){
  5819. errorHandler.fatalError("end tag name: "+tagName+' is not match the current start tagName:'+config.tagName );
  5820. }
  5821. }else{
  5822. parseStack.push(config)
  5823. }
  5824. end++;
  5825. break;
  5826. // end elment
  5827. case '?':// <?...?>
  5828. locator&&position(tagStart);
  5829. end = parseInstruction(source,tagStart,domBuilder);
  5830. break;
  5831. case '!':// <!doctype,<![CDATA,<!--
  5832. locator&&position(tagStart);
  5833. end = parseDCC(source,tagStart,domBuilder,errorHandler);
  5834. break;
  5835. default:
  5836. locator&&position(tagStart);
  5837. var el = new ElementAttributes();
  5838. var currentNSMap = parseStack[parseStack.length-1].currentNSMap;
  5839. //elStartEnd
  5840. var end = parseElementStartPart(source,tagStart,el,currentNSMap,entityReplacer,errorHandler);
  5841. var len = el.length;
  5842. if(!el.closed && fixSelfClosed(source,end,el.tagName,closeMap)){
  5843. el.closed = true;
  5844. if(!entityMap.nbsp){
  5845. errorHandler.warning('unclosed xml attribute');
  5846. }
  5847. }
  5848. if(locator && len){
  5849. var locator2 = copyLocator(locator,{});
  5850. //try{//attribute position fixed
  5851. for(var i = 0;i<len;i++){
  5852. var a = el[i];
  5853. position(a.offset);
  5854. a.locator = copyLocator(locator,{});
  5855. }
  5856. //}catch(e){console.error('@@@@@'+e)}
  5857. domBuilder.locator = locator2
  5858. if(appendElement(el,domBuilder,currentNSMap)){
  5859. parseStack.push(el)
  5860. }
  5861. domBuilder.locator = locator;
  5862. }else{
  5863. if(appendElement(el,domBuilder,currentNSMap)){
  5864. parseStack.push(el)
  5865. }
  5866. }
  5867. if(el.uri === 'http://www.w3.org/1999/xhtml' && !el.closed){
  5868. end = parseHtmlSpecialContent(source,end,el.tagName,entityReplacer,domBuilder)
  5869. }else{
  5870. end++;
  5871. }
  5872. }
  5873. }catch(e){
  5874. errorHandler.error('element parse error: '+e)
  5875. //errorHandler.error('element parse error: '+e);
  5876. end = -1;
  5877. //throw e;
  5878. }
  5879. if(end>start){
  5880. start = end;
  5881. }else{
  5882. //TODO: 这里有可能sax回退,有位置错误风险
  5883. appendText(Math.max(tagStart,start)+1);
  5884. }
  5885. }
  5886. }
  5887. function copyLocator(f,t){
  5888. t.lineNumber = f.lineNumber;
  5889. t.columnNumber = f.columnNumber;
  5890. return t;
  5891. }
  5892. /**
  5893. * @see #appendElement(source,elStartEnd,el,selfClosed,entityReplacer,domBuilder,parseStack);
  5894. * @return end of the elementStartPart(end of elementEndPart for selfClosed el)
  5895. */
  5896. function parseElementStartPart(source,start,el,currentNSMap,entityReplacer,errorHandler){
  5897. var attrName;
  5898. var value;
  5899. var p = ++start;
  5900. var s = S_TAG;//status
  5901. while(true){
  5902. var c = source.charAt(p);
  5903. switch(c){
  5904. case '=':
  5905. if(s === S_ATTR){//attrName
  5906. attrName = source.slice(start,p);
  5907. s = S_EQ;
  5908. }else if(s === S_ATTR_SPACE){
  5909. s = S_EQ;
  5910. }else{
  5911. //fatalError: equal must after attrName or space after attrName
  5912. throw new Error('attribute equal must after attrName');
  5913. }
  5914. break;
  5915. case '\'':
  5916. case '"':
  5917. if(s === S_EQ || s === S_ATTR //|| s == S_ATTR_SPACE
  5918. ){//equal
  5919. if(s === S_ATTR){
  5920. errorHandler.warning('attribute value must after "="')
  5921. attrName = source.slice(start,p)
  5922. }
  5923. start = p+1;
  5924. p = source.indexOf(c,start)
  5925. if(p>0){
  5926. value = source.slice(start,p).replace(/&#?\w+;/g,entityReplacer);
  5927. el.add(attrName,value,start-1);
  5928. s = S_ATTR_END;
  5929. }else{
  5930. //fatalError: no end quot match
  5931. throw new Error('attribute value no end \''+c+'\' match');
  5932. }
  5933. }else if(s == S_ATTR_NOQUOT_VALUE){
  5934. value = source.slice(start,p).replace(/&#?\w+;/g,entityReplacer);
  5935. //console.log(attrName,value,start,p)
  5936. el.add(attrName,value,start);
  5937. //console.dir(el)
  5938. errorHandler.warning('attribute "'+attrName+'" missed start quot('+c+')!!');
  5939. start = p+1;
  5940. s = S_ATTR_END
  5941. }else{
  5942. //fatalError: no equal before
  5943. throw new Error('attribute value must after "="');
  5944. }
  5945. break;
  5946. case '/':
  5947. switch(s){
  5948. case S_TAG:
  5949. el.setTagName(source.slice(start,p));
  5950. case S_ATTR_END:
  5951. case S_TAG_SPACE:
  5952. case S_TAG_CLOSE:
  5953. s =S_TAG_CLOSE;
  5954. el.closed = true;
  5955. case S_ATTR_NOQUOT_VALUE:
  5956. case S_ATTR:
  5957. case S_ATTR_SPACE:
  5958. break;
  5959. //case S_EQ:
  5960. default:
  5961. throw new Error("attribute invalid close char('/')")
  5962. }
  5963. break;
  5964. case ''://end document
  5965. //throw new Error('unexpected end of input')
  5966. errorHandler.error('unexpected end of input');
  5967. if(s == S_TAG){
  5968. el.setTagName(source.slice(start,p));
  5969. }
  5970. return p;
  5971. case '>':
  5972. switch(s){
  5973. case S_TAG:
  5974. el.setTagName(source.slice(start,p));
  5975. case S_ATTR_END:
  5976. case S_TAG_SPACE:
  5977. case S_TAG_CLOSE:
  5978. break;//normal
  5979. case S_ATTR_NOQUOT_VALUE://Compatible state
  5980. case S_ATTR:
  5981. value = source.slice(start,p);
  5982. if(value.slice(-1) === '/'){
  5983. el.closed = true;
  5984. value = value.slice(0,-1)
  5985. }
  5986. case S_ATTR_SPACE:
  5987. if(s === S_ATTR_SPACE){
  5988. value = attrName;
  5989. }
  5990. if(s == S_ATTR_NOQUOT_VALUE){
  5991. errorHandler.warning('attribute "'+value+'" missed quot(")!!');
  5992. el.add(attrName,value.replace(/&#?\w+;/g,entityReplacer),start)
  5993. }else{
  5994. if(currentNSMap[''] !== 'http://www.w3.org/1999/xhtml' || !value.match(/^(?:disabled|checked|selected)$/i)){
  5995. errorHandler.warning('attribute "'+value+'" missed value!! "'+value+'" instead!!')
  5996. }
  5997. el.add(value,value,start)
  5998. }
  5999. break;
  6000. case S_EQ:
  6001. throw new Error('attribute value missed!!');
  6002. }
  6003. // console.log(tagName,tagNamePattern,tagNamePattern.test(tagName))
  6004. return p;
  6005. /*xml space '\x20' | #x9 | #xD | #xA; */
  6006. case '\u0080':
  6007. c = ' ';
  6008. default:
  6009. if(c<= ' '){//space
  6010. switch(s){
  6011. case S_TAG:
  6012. el.setTagName(source.slice(start,p));//tagName
  6013. s = S_TAG_SPACE;
  6014. break;
  6015. case S_ATTR:
  6016. attrName = source.slice(start,p)
  6017. s = S_ATTR_SPACE;
  6018. break;
  6019. case S_ATTR_NOQUOT_VALUE:
  6020. var value = source.slice(start,p).replace(/&#?\w+;/g,entityReplacer);
  6021. errorHandler.warning('attribute "'+value+'" missed quot(")!!');
  6022. el.add(attrName,value,start)
  6023. case S_ATTR_END:
  6024. s = S_TAG_SPACE;
  6025. break;
  6026. //case S_TAG_SPACE:
  6027. //case S_EQ:
  6028. //case S_ATTR_SPACE:
  6029. // void();break;
  6030. //case S_TAG_CLOSE:
  6031. //ignore warning
  6032. }
  6033. }else{//not space
  6034. //S_TAG, S_ATTR, S_EQ, S_ATTR_NOQUOT_VALUE
  6035. //S_ATTR_SPACE, S_ATTR_END, S_TAG_SPACE, S_TAG_CLOSE
  6036. switch(s){
  6037. //case S_TAG:void();break;
  6038. //case S_ATTR:void();break;
  6039. //case S_ATTR_NOQUOT_VALUE:void();break;
  6040. case S_ATTR_SPACE:
  6041. var tagName = el.tagName;
  6042. if(currentNSMap[''] !== 'http://www.w3.org/1999/xhtml' || !attrName.match(/^(?:disabled|checked|selected)$/i)){
  6043. errorHandler.warning('attribute "'+attrName+'" missed value!! "'+attrName+'" instead2!!')
  6044. }
  6045. el.add(attrName,attrName,start);
  6046. start = p;
  6047. s = S_ATTR;
  6048. break;
  6049. case S_ATTR_END:
  6050. errorHandler.warning('attribute space is required"'+attrName+'"!!')
  6051. case S_TAG_SPACE:
  6052. s = S_ATTR;
  6053. start = p;
  6054. break;
  6055. case S_EQ:
  6056. s = S_ATTR_NOQUOT_VALUE;
  6057. start = p;
  6058. break;
  6059. case S_TAG_CLOSE:
  6060. throw new Error("elements closed character '/' and '>' must be connected to");
  6061. }
  6062. }
  6063. }//end outer switch
  6064. //console.log('p++',p)
  6065. p++;
  6066. }
  6067. }
  6068. /**
  6069. * @return true if has new namespace define
  6070. */
  6071. function appendElement(el,domBuilder,currentNSMap){
  6072. var tagName = el.tagName;
  6073. var localNSMap = null;
  6074. //var currentNSMap = parseStack[parseStack.length-1].currentNSMap;
  6075. var i = el.length;
  6076. while(i--){
  6077. var a = el[i];
  6078. var qName = a.qName;
  6079. var value = a.value;
  6080. var nsp = qName.indexOf(':');
  6081. if(nsp>0){
  6082. var prefix = a.prefix = qName.slice(0,nsp);
  6083. var localName = qName.slice(nsp+1);
  6084. var nsPrefix = prefix === 'xmlns' && localName
  6085. }else{
  6086. localName = qName;
  6087. prefix = null
  6088. nsPrefix = qName === 'xmlns' && ''
  6089. }
  6090. //can not set prefix,because prefix !== ''
  6091. a.localName = localName ;
  6092. //prefix == null for no ns prefix attribute
  6093. if(nsPrefix !== false){//hack!!
  6094. if(localNSMap == null){
  6095. localNSMap = {}
  6096. //console.log(currentNSMap,0)
  6097. _copy(currentNSMap,currentNSMap={})
  6098. //console.log(currentNSMap,1)
  6099. }
  6100. currentNSMap[nsPrefix] = localNSMap[nsPrefix] = value;
  6101. a.uri = 'http://www.w3.org/2000/xmlns/'
  6102. domBuilder.startPrefixMapping(nsPrefix, value)
  6103. }
  6104. }
  6105. var i = el.length;
  6106. while(i--){
  6107. a = el[i];
  6108. var prefix = a.prefix;
  6109. if(prefix){//no prefix attribute has no namespace
  6110. if(prefix === 'xml'){
  6111. a.uri = 'http://www.w3.org/XML/1998/namespace';
  6112. }if(prefix !== 'xmlns'){
  6113. a.uri = currentNSMap[prefix || '']
  6114. //{console.log('###'+a.qName,domBuilder.locator.systemId+'',currentNSMap,a.uri)}
  6115. }
  6116. }
  6117. }
  6118. var nsp = tagName.indexOf(':');
  6119. if(nsp>0){
  6120. prefix = el.prefix = tagName.slice(0,nsp);
  6121. localName = el.localName = tagName.slice(nsp+1);
  6122. }else{
  6123. prefix = null;//important!!
  6124. localName = el.localName = tagName;
  6125. }
  6126. //no prefix element has default namespace
  6127. var ns = el.uri = currentNSMap[prefix || ''];
  6128. domBuilder.startElement(ns,localName,tagName,el);
  6129. //endPrefixMapping and startPrefixMapping have not any help for dom builder
  6130. //localNSMap = null
  6131. if(el.closed){
  6132. domBuilder.endElement(ns,localName,tagName);
  6133. if(localNSMap){
  6134. for(prefix in localNSMap){
  6135. domBuilder.endPrefixMapping(prefix)
  6136. }
  6137. }
  6138. }else{
  6139. el.currentNSMap = currentNSMap;
  6140. el.localNSMap = localNSMap;
  6141. //parseStack.push(el);
  6142. return true;
  6143. }
  6144. }
  6145. function parseHtmlSpecialContent(source,elStartEnd,tagName,entityReplacer,domBuilder){
  6146. if(/^(?:script|textarea)$/i.test(tagName)){
  6147. var elEndStart = source.indexOf('</'+tagName+'>',elStartEnd);
  6148. var text = source.substring(elStartEnd+1,elEndStart);
  6149. if(/[&<]/.test(text)){
  6150. if(/^script$/i.test(tagName)){
  6151. //if(!/\]\]>/.test(text)){
  6152. //lexHandler.startCDATA();
  6153. domBuilder.characters(text,0,text.length);
  6154. //lexHandler.endCDATA();
  6155. return elEndStart;
  6156. //}
  6157. }//}else{//text area
  6158. text = text.replace(/&#?\w+;/g,entityReplacer);
  6159. domBuilder.characters(text,0,text.length);
  6160. return elEndStart;
  6161. //}
  6162. }
  6163. }
  6164. return elStartEnd+1;
  6165. }
  6166. function fixSelfClosed(source,elStartEnd,tagName,closeMap){
  6167. //if(tagName in closeMap){
  6168. var pos = closeMap[tagName];
  6169. if(pos == null){
  6170. //console.log(tagName)
  6171. pos = source.lastIndexOf('</'+tagName+'>')
  6172. if(pos<elStartEnd){//忘记闭合
  6173. pos = source.lastIndexOf('</'+tagName)
  6174. }
  6175. closeMap[tagName] =pos
  6176. }
  6177. return pos<elStartEnd;
  6178. //}
  6179. }
  6180. function _copy(source,target){
  6181. for(var n in source){target[n] = source[n]}
  6182. }
  6183. function parseDCC(source,start,domBuilder,errorHandler){//sure start with '<!'
  6184. var next= source.charAt(start+2)
  6185. switch(next){
  6186. case '-':
  6187. if(source.charAt(start + 3) === '-'){
  6188. var end = source.indexOf('-->',start+4);
  6189. //append comment source.substring(4,end)//<!--
  6190. if(end>start){
  6191. domBuilder.comment(source,start+4,end-start-4);
  6192. return end+3;
  6193. }else{
  6194. errorHandler.error("Unclosed comment");
  6195. return -1;
  6196. }
  6197. }else{
  6198. //error
  6199. return -1;
  6200. }
  6201. default:
  6202. if(source.substr(start+3,6) == 'CDATA['){
  6203. var end = source.indexOf(']]>',start+9);
  6204. domBuilder.startCDATA();
  6205. domBuilder.characters(source,start+9,end-start-9);
  6206. domBuilder.endCDATA()
  6207. return end+3;
  6208. }
  6209. //<!DOCTYPE
  6210. //startDTD(java.lang.String name, java.lang.String publicId, java.lang.String systemId)
  6211. var matchs = split(source,start);
  6212. var len = matchs.length;
  6213. if(len>1 && /!doctype/i.test(matchs[0][0])){
  6214. var name = matchs[1][0];
  6215. var pubid = len>3 && /^public$/i.test(matchs[2][0]) && matchs[3][0]
  6216. var sysid = len>4 && matchs[4][0];
  6217. var lastMatch = matchs[len-1]
  6218. domBuilder.startDTD(name,pubid && pubid.replace(/^(['"])(.*?)\1$/,'$2'),
  6219. sysid && sysid.replace(/^(['"])(.*?)\1$/,'$2'));
  6220. domBuilder.endDTD();
  6221. return lastMatch.index+lastMatch[0].length
  6222. }
  6223. }
  6224. return -1;
  6225. }
  6226. function parseInstruction(source,start,domBuilder){
  6227. var end = source.indexOf('?>',start);
  6228. if(end){
  6229. var match = source.substring(start,end).match(/^<\?(\S*)\s*([\s\S]*?)\s*$/);
  6230. if(match){
  6231. var len = match[0].length;
  6232. domBuilder.processingInstruction(match[1], match[2]) ;
  6233. return end+2;
  6234. }else{//error
  6235. return -1;
  6236. }
  6237. }
  6238. return -1;
  6239. }
  6240. /**
  6241. * @param source
  6242. */
  6243. function ElementAttributes(source){
  6244. }
  6245. ElementAttributes.prototype = {
  6246. setTagName:function(tagName){
  6247. if(!tagNamePattern.test(tagName)){
  6248. throw new Error('invalid tagName:'+tagName)
  6249. }
  6250. this.tagName = tagName
  6251. },
  6252. add:function(qName,value,offset){
  6253. if(!tagNamePattern.test(qName)){
  6254. throw new Error('invalid attribute:'+qName)
  6255. }
  6256. this[this.length++] = {qName:qName,value:value,offset:offset}
  6257. },
  6258. length:0,
  6259. getLocalName:function(i){return this[i].localName},
  6260. getLocator:function(i){return this[i].locator},
  6261. getQName:function(i){return this[i].qName},
  6262. getURI:function(i){return this[i].uri},
  6263. getValue:function(i){return this[i].value}
  6264. // ,getIndex:function(uri, localName)){
  6265. // if(localName){
  6266. //
  6267. // }else{
  6268. // var qName = uri
  6269. // }
  6270. // },
  6271. // getValue:function(){return this.getValue(this.getIndex.apply(this,arguments))},
  6272. // getType:function(uri,localName){}
  6273. // getType:function(i){},
  6274. }
  6275. function _set_proto_(thiz,parent){
  6276. thiz.__proto__ = parent;
  6277. return thiz;
  6278. }
  6279. if(!(_set_proto_({},_set_proto_.prototype) instanceof _set_proto_)){
  6280. _set_proto_ = function(thiz,parent){
  6281. function p(){};
  6282. p.prototype = parent;
  6283. p = new p();
  6284. for(parent in thiz){
  6285. p[parent] = thiz[parent];
  6286. }
  6287. return p;
  6288. }
  6289. }
  6290. function split(source,start){
  6291. var match;
  6292. var buf = [];
  6293. var reg = /'[^']+'|"[^"]+"|[^\s<>\/=]+=?|(\/?\s*>|<)/g;
  6294. reg.lastIndex = start;
  6295. reg.exec(source);//skip <
  6296. while(match = reg.exec(source)){
  6297. buf.push(match);
  6298. if(match[1])return buf;
  6299. }
  6300. }
  6301. exports.XMLReader = XMLReader;
  6302. },{}],33:[function(require,module,exports){
  6303. module.exports={
  6304. "name": "dav",
  6305. "version": "1.8.0",
  6306. "author": "Gareth Aye [:gaye] <gaye@mozilla.com>",
  6307. "description": "WebDAV, CalDAV, and CardDAV client for nodejs and the browser",
  6308. "license": "MPL-2.0",
  6309. "main": "./dav.js",
  6310. "repository": "https://github.com/gaye/dav",
  6311. "keywords": [
  6312. "address book",
  6313. "calendar",
  6314. "contacts",
  6315. "dav",
  6316. "caldav",
  6317. "carddav",
  6318. "webdav",
  6319. "ical",
  6320. "vcard",
  6321. "sync",
  6322. "rfc 4791",
  6323. "rfc 6352",
  6324. "rfc 6578"
  6325. ],
  6326. "dependencies": {
  6327. "co": "^4.6.0",
  6328. "xmldom": "^0.1.19",
  6329. "xmlhttprequest": "^1.7.0"
  6330. },
  6331. "devDependencies": {
  6332. "babel": "^5.8.23",
  6333. "browserify": "^11.0.1",
  6334. "chai": "^3.2.0",
  6335. "doctoc": "^0.15.0",
  6336. "mocha": "^2.3.2",
  6337. "nock": "^2.10.0",
  6338. "sinon": "^1.16.1",
  6339. "tcp-port-used": "^0.1.2",
  6340. "uglify-js": "^2.4.24"
  6341. },
  6342. "scripts": {
  6343. "test": "make test"
  6344. }
  6345. }
  6346. },{}]},{},[8])(8)
  6347. });