安图前端代码

kindeditor.js 156KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955
  1. /*******************************************************************************
  2. * KindEditor - WYSIWYG HTML Editor for Internet
  3. * Copyright (C) 2006-2013 kindsoft.net
  4. *
  5. * @author Roddy <luolonghao@gmail.com>
  6. * @website http://www.kindsoft.net/
  7. * @licence http://www.kindsoft.net/license.php
  8. * @version 4.1.10 (2013-11-23)
  9. *******************************************************************************/
  10. (function (window, undefined) {
  11. if (window.KindEditor) {
  12. return;
  13. }
  14. if (!window.console) {
  15. window.console = {};
  16. }
  17. if (!console.log) {
  18. console.log = function () {};
  19. }
  20. var _VERSION = '4.1.10 (2013-11-23)',
  21. _ua = navigator.userAgent.toLowerCase(),
  22. _IE = _ua.indexOf('msie') > -1 && _ua.indexOf('opera') == -1,
  23. _NEWIE = _ua.indexOf('msie') == -1 && _ua.indexOf('trident') > -1,
  24. _GECKO = _ua.indexOf('gecko') > -1 && _ua.indexOf('khtml') == -1,
  25. _WEBKIT = _ua.indexOf('applewebkit') > -1,
  26. _OPERA = _ua.indexOf('opera') > -1,
  27. _MOBILE = _ua.indexOf('mobile') > -1,
  28. _IOS = /ipad|iphone|ipod/.test(_ua),
  29. _QUIRKS = document.compatMode != 'CSS1Compat',
  30. _IERANGE = !window.getSelection,
  31. _matches = /(?:msie|firefox|webkit|opera)[\/:\s](\d+)/.exec(_ua),
  32. _V = _matches ? _matches[1] : '0',
  33. _TIME = new Date().getTime();
  34. function _isArray(val) {
  35. if (!val) {
  36. return false;
  37. }
  38. return Object.prototype.toString.call(val) === '[object Array]';
  39. }
  40. function _isFunction(val) {
  41. if (!val) {
  42. return false;
  43. }
  44. return Object.prototype.toString.call(val) === '[object Function]';
  45. }
  46. function _inArray(val, arr) {
  47. for (var i = 0, len = arr.length; i < len; i++) {
  48. if (val === arr[i]) {
  49. return i;
  50. }
  51. }
  52. return -1;
  53. }
  54. function _each(obj, fn) {
  55. if (_isArray(obj)) {
  56. for (var i = 0, len = obj.length; i < len; i++) {
  57. if (fn.call(obj[i], i, obj[i]) === false) {
  58. break;
  59. }
  60. }
  61. } else {
  62. for (var key in obj) {
  63. if (obj.hasOwnProperty(key)) {
  64. if (fn.call(obj[key], key, obj[key]) === false) {
  65. break;
  66. }
  67. }
  68. }
  69. }
  70. }
  71. function _trim(str) {
  72. return str.replace(/(?:^[ \t\n\r]+)|(?:[ \t\n\r]+$)/g, '');
  73. }
  74. function _inString(val, str, delimiter) {
  75. delimiter = delimiter === undefined ? ',' : delimiter;
  76. return (delimiter + str + delimiter).indexOf(delimiter + val + delimiter) >= 0;
  77. }
  78. function _addUnit(val, unit) {
  79. unit = unit || 'px';
  80. return val && /^\d+$/.test(val) ? val + unit : val;
  81. }
  82. function _removeUnit(val) {
  83. var match;
  84. return val && (match = /(\d+)/.exec(val)) ? parseInt(match[1], 10) : 0;
  85. }
  86. function _escape(val) {
  87. return val.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
  88. }
  89. function _unescape(val) {
  90. return val.replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '"').replace(/&amp;/g, '&');
  91. }
  92. function _toCamel(str) {
  93. var arr = str.split('-');
  94. str = '';
  95. _each(arr, function(key, val) {
  96. str += (key > 0) ? val.charAt(0).toUpperCase() + val.substr(1) : val;
  97. });
  98. return str;
  99. }
  100. function _toHex(val) {
  101. function hex(d) {
  102. var s = parseInt(d, 10).toString(16).toUpperCase();
  103. return s.length > 1 ? s : '0' + s;
  104. }
  105. return val.replace(/rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)/ig,
  106. function($0, $1, $2, $3) {
  107. return '#' + hex($1) + hex($2) + hex($3);
  108. }
  109. );
  110. }
  111. function _toMap(val, delimiter) {
  112. delimiter = delimiter === undefined ? ',' : delimiter;
  113. var map = {}, arr = _isArray(val) ? val : val.split(delimiter), match;
  114. _each(arr, function(key, val) {
  115. if ((match = /^(\d+)\.\.(\d+)$/.exec(val))) {
  116. for (var i = parseInt(match[1], 10); i <= parseInt(match[2], 10); i++) {
  117. map[i.toString()] = true;
  118. }
  119. } else {
  120. map[val] = true;
  121. }
  122. });
  123. return map;
  124. }
  125. function _toArray(obj, offset) {
  126. return Array.prototype.slice.call(obj, offset || 0);
  127. }
  128. function _undef(val, defaultVal) {
  129. return val === undefined ? defaultVal : val;
  130. }
  131. function _invalidUrl(url) {
  132. return !url || /[<>"]/.test(url);
  133. }
  134. function _addParam(url, param) {
  135. return url.indexOf('?') >= 0 ? url + '&' + param : url + '?' + param;
  136. }
  137. function _extend(child, parent, proto) {
  138. if (!proto) {
  139. proto = parent;
  140. parent = null;
  141. }
  142. var childProto;
  143. if (parent) {
  144. var fn = function () {};
  145. fn.prototype = parent.prototype;
  146. childProto = new fn();
  147. _each(proto, function(key, val) {
  148. childProto[key] = val;
  149. });
  150. } else {
  151. childProto = proto;
  152. }
  153. childProto.constructor = child;
  154. child.prototype = childProto;
  155. child.parent = parent ? parent.prototype : null;
  156. }
  157. function _json(text) {
  158. var match;
  159. if ((match = /\{[\s\S]*\}|\[[\s\S]*\]/.exec(text))) {
  160. text = match[0];
  161. }
  162. var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
  163. cx.lastIndex = 0;
  164. if (cx.test(text)) {
  165. text = text.replace(cx, function (a) {
  166. return '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
  167. });
  168. }
  169. if (/^[\],:{}\s]*$/.
  170. test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@').
  171. replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
  172. replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
  173. return eval('(' + text + ')');
  174. }
  175. throw 'JSON parse error';
  176. }
  177. var _round = Math.round;
  178. var K = {
  179. DEBUG : false,
  180. VERSION : _VERSION,
  181. IE : _IE,
  182. GECKO : _GECKO,
  183. WEBKIT : _WEBKIT,
  184. OPERA : _OPERA,
  185. V : _V,
  186. TIME : _TIME,
  187. each : _each,
  188. isArray : _isArray,
  189. isFunction : _isFunction,
  190. inArray : _inArray,
  191. inString : _inString,
  192. trim : _trim,
  193. addUnit : _addUnit,
  194. removeUnit : _removeUnit,
  195. escape : _escape,
  196. unescape : _unescape,
  197. toCamel : _toCamel,
  198. toHex : _toHex,
  199. toMap : _toMap,
  200. toArray : _toArray,
  201. undef : _undef,
  202. invalidUrl : _invalidUrl,
  203. addParam : _addParam,
  204. extend : _extend,
  205. json : _json
  206. };
  207. var _INLINE_TAG_MAP = _toMap('a,abbr,acronym,b,basefont,bdo,big,br,button,cite,code,del,dfn,em,font,i,img,input,ins,kbd,label,map,q,s,samp,select,small,span,strike,strong,sub,sup,textarea,tt,u,var'),
  208. _BLOCK_TAG_MAP = _toMap('address,applet,blockquote,body,center,dd,dir,div,dl,dt,fieldset,form,frameset,h1,h2,h3,h4,h5,h6,head,hr,html,iframe,ins,isindex,li,map,menu,meta,noframes,noscript,object,ol,p,pre,script,style,table,tbody,td,tfoot,th,thead,title,tr,ul'),
  209. _SINGLE_TAG_MAP = _toMap('area,base,basefont,br,col,frame,hr,img,input,isindex,link,meta,param,embed'),
  210. _STYLE_TAG_MAP = _toMap('b,basefont,big,del,em,font,i,s,small,span,strike,strong,sub,sup,u'),
  211. _CONTROL_TAG_MAP = _toMap('img,table,input,textarea,button'),
  212. _PRE_TAG_MAP = _toMap('pre,style,script'),
  213. _NOSPLIT_TAG_MAP = _toMap('html,head,body,td,tr,table,ol,ul,li'),
  214. _AUTOCLOSE_TAG_MAP = _toMap('colgroup,dd,dt,li,options,p,td,tfoot,th,thead,tr'),
  215. _FILL_ATTR_MAP = _toMap('checked,compact,declare,defer,disabled,ismap,multiple,nohref,noresize,noshade,nowrap,readonly,selected'),
  216. _VALUE_TAG_MAP = _toMap('input,button,textarea,select');
  217. function _getBasePath() {
  218. var els = document.getElementsByTagName('script'), src;
  219. for (var i = 0, len = els.length; i < len; i++) {
  220. src = els[i].src || '';
  221. if (/kindeditor[\w\-\.]*\.js/.test(src)) {
  222. return src.substring(0, src.lastIndexOf('/') + 1);
  223. }
  224. }
  225. return '';
  226. }
  227. K.basePath = _getBasePath();
  228. K.options = {
  229. designMode : true,
  230. fullscreenMode : false,
  231. filterMode : true,
  232. wellFormatMode : true,
  233. shadowMode : true,
  234. loadStyleMode : true,
  235. basePath : K.basePath,
  236. themesPath : K.basePath + 'themes/',
  237. langPath : K.basePath + 'lang/',
  238. pluginsPath : K.basePath + 'plugins/',
  239. themeType : 'default',
  240. langType : 'zh_CN',
  241. urlType : '',
  242. newlineTag : 'p',
  243. resizeType : 2,
  244. syncType : 'form',
  245. pasteType : 2,
  246. dialogAlignType : 'page',
  247. useContextmenu : true,
  248. fullscreenShortcut : false,
  249. bodyClass : 'ke-content',
  250. indentChar : '\t',
  251. cssPath : '',
  252. cssData : '',
  253. minWidth : 650,
  254. minHeight : 100,
  255. minChangeSize : 50,
  256. zIndex : 811213,
  257. items : [
  258. 'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
  259. 'italic', 'underline'
  260. ],
  261. noDisableItems : ['source', 'fullscreen'],
  262. colorTable : [
  263. ['#E53333', '#E56600', '#FF9900', '#64451D', '#DFC5A4', '#FFE500'],
  264. ['#009900', '#006600', '#99BB00', '#B8D100', '#60D978', '#00D5FF'],
  265. ['#337FE5', '#003399', '#4C33E5', '#9933E5', '#CC33E5', '#EE33EE'],
  266. ['#FFFFFF', '#CCCCCC', '#999999', '#666666', '#333333', '#000000']
  267. ],
  268. fontSizeTable : ['9px', '10px', '12px', '14px', '16px', '18px', '24px', '32px'],
  269. htmlTags : {
  270. font : ['id', 'class', 'color', 'size', 'face', '.background-color'],
  271. span : [
  272. 'id', 'class', '.color', '.background-color', '.font-size', '.font-family', '.background',
  273. '.font-weight', '.font-style', '.text-decoration', '.vertical-align', '.line-height'
  274. ],
  275. div : [
  276. 'id', 'class', 'align', '.border', '.margin', '.padding', '.text-align', '.color',
  277. '.background-color', '.font-size', '.font-family', '.font-weight', '.background',
  278. '.font-style', '.text-decoration', '.vertical-align', '.margin-left'
  279. ],
  280. table: [
  281. 'id', 'class', 'border', 'cellspacing', 'cellpadding', 'width', 'height', 'align', 'bordercolor',
  282. '.padding', '.margin', '.border', 'bgcolor', '.text-align', '.color', '.background-color',
  283. '.font-size', '.font-family', '.font-weight', '.font-style', '.text-decoration', '.background',
  284. '.width', '.height', '.border-collapse'
  285. ],
  286. 'td,th': [
  287. 'id', 'class', 'align', 'valign', 'width', 'height', 'colspan', 'rowspan', 'bgcolor',
  288. '.text-align', '.color', '.background-color', '.font-size', '.font-family', '.font-weight',
  289. '.font-style', '.text-decoration', '.vertical-align', '.background', '.border'
  290. ],
  291. a : ['id', 'class', 'href', 'target', 'name'],
  292. embed : ['id', 'class', 'src', 'width', 'height', 'type', 'loop', 'autostart', 'quality', '.width', '.height', 'align', 'allowscriptaccess'],
  293. img : ['id', 'class', 'src', 'width', 'height', 'border', 'alt', 'title', 'align', '.width', '.height', '.border'],
  294. 'p,ol,ul,li,blockquote,h1,h2,h3,h4,h5,h6' : [
  295. 'id', 'class', 'align', '.text-align', '.color', '.background-color', '.font-size', '.font-family', '.background',
  296. '.font-weight', '.font-style', '.text-decoration', '.vertical-align', '.text-indent', '.margin-left'
  297. ],
  298. pre : ['id', 'class'],
  299. hr : ['id', 'class', '.page-break-after'],
  300. 'br,tbody,tr,strong,b,sub,sup,em,i,u,strike,s,del' : ['id', 'class'],
  301. iframe : ['id', 'class', 'src', 'frameborder', 'width', 'height', '.width', '.height']
  302. },
  303. layout : '<div class="container"><div class="toolbar"></div><div class="edit"></div><div class="statusbar"></div></div>'
  304. };
  305. var _useCapture = false;
  306. var _INPUT_KEY_MAP = _toMap('8,9,13,32,46,48..57,59,61,65..90,106,109..111,188,190..192,219..222');
  307. var _CURSORMOVE_KEY_MAP = _toMap('33..40');
  308. var _CHANGE_KEY_MAP = {};
  309. _each(_INPUT_KEY_MAP, function(key, val) {
  310. _CHANGE_KEY_MAP[key] = val;
  311. });
  312. _each(_CURSORMOVE_KEY_MAP, function(key, val) {
  313. _CHANGE_KEY_MAP[key] = val;
  314. });
  315. function _bindEvent(el, type, fn) {
  316. if (el.addEventListener){
  317. el.addEventListener(type, fn, _useCapture);
  318. } else if (el.attachEvent){
  319. el.attachEvent('on' + type, fn);
  320. }
  321. }
  322. function _unbindEvent(el, type, fn) {
  323. if (el.removeEventListener){
  324. el.removeEventListener(type, fn, _useCapture);
  325. } else if (el.detachEvent){
  326. el.detachEvent('on' + type, fn);
  327. }
  328. }
  329. var _EVENT_PROPS = ('altKey,attrChange,attrName,bubbles,button,cancelable,charCode,clientX,clientY,ctrlKey,currentTarget,' +
  330. 'data,detail,eventPhase,fromElement,handler,keyCode,metaKey,newValue,offsetX,offsetY,originalTarget,pageX,' +
  331. 'pageY,prevValue,relatedNode,relatedTarget,screenX,screenY,shiftKey,srcElement,target,toElement,view,wheelDelta,which').split(',');
  332. function KEvent(el, event) {
  333. this.init(el, event);
  334. }
  335. _extend(KEvent, {
  336. init : function(el, event) {
  337. var self = this, doc = el.ownerDocument || el.document || el;
  338. self.event = event;
  339. _each(_EVENT_PROPS, function(key, val) {
  340. self[val] = event[val];
  341. });
  342. if (!self.target) {
  343. self.target = self.srcElement || doc;
  344. }
  345. if (self.target.nodeType === 3) {
  346. self.target = self.target.parentNode;
  347. }
  348. if (!self.relatedTarget && self.fromElement) {
  349. self.relatedTarget = self.fromElement === self.target ? self.toElement : self.fromElement;
  350. }
  351. if (self.pageX == null && self.clientX != null) {
  352. var d = doc.documentElement, body = doc.body;
  353. self.pageX = self.clientX + (d && d.scrollLeft || body && body.scrollLeft || 0) - (d && d.clientLeft || body && body.clientLeft || 0);
  354. self.pageY = self.clientY + (d && d.scrollTop || body && body.scrollTop || 0) - (d && d.clientTop || body && body.clientTop || 0);
  355. }
  356. if (!self.which && ((self.charCode || self.charCode === 0) ? self.charCode : self.keyCode)) {
  357. self.which = self.charCode || self.keyCode;
  358. }
  359. if (!self.metaKey && self.ctrlKey) {
  360. self.metaKey = self.ctrlKey;
  361. }
  362. if (!self.which && self.button !== undefined) {
  363. self.which = (self.button & 1 ? 1 : (self.button & 2 ? 3 : (self.button & 4 ? 2 : 0)));
  364. }
  365. switch (self.which) {
  366. case 186 :
  367. self.which = 59;
  368. break;
  369. case 187 :
  370. case 107 :
  371. case 43 :
  372. self.which = 61;
  373. break;
  374. case 189 :
  375. case 45 :
  376. self.which = 109;
  377. break;
  378. case 42 :
  379. self.which = 106;
  380. break;
  381. case 47 :
  382. self.which = 111;
  383. break;
  384. case 78 :
  385. self.which = 110;
  386. break;
  387. }
  388. if (self.which >= 96 && self.which <= 105) {
  389. self.which -= 48;
  390. }
  391. },
  392. preventDefault : function() {
  393. var ev = this.event;
  394. if (ev.preventDefault) {
  395. ev.preventDefault();
  396. } else {
  397. ev.returnValue = false;
  398. }
  399. },
  400. stopPropagation : function() {
  401. var ev = this.event;
  402. if (ev.stopPropagation) {
  403. ev.stopPropagation();
  404. } else {
  405. ev.cancelBubble = true;
  406. }
  407. },
  408. stop : function() {
  409. this.preventDefault();
  410. this.stopPropagation();
  411. }
  412. });
  413. var _eventExpendo = 'kindeditor_' + _TIME, _eventId = 0, _eventData = {};
  414. function _getId(el) {
  415. return el[_eventExpendo] || null;
  416. }
  417. function _setId(el) {
  418. el[_eventExpendo] = ++_eventId;
  419. return _eventId;
  420. }
  421. function _removeId(el) {
  422. try {
  423. delete el[_eventExpendo];
  424. } catch(e) {
  425. if (el.removeAttribute) {
  426. el.removeAttribute(_eventExpendo);
  427. }
  428. }
  429. }
  430. function _bind(el, type, fn) {
  431. if (type.indexOf(',') >= 0) {
  432. _each(type.split(','), function() {
  433. _bind(el, this, fn);
  434. });
  435. return;
  436. }
  437. var id = _getId(el);
  438. if (!id) {
  439. id = _setId(el);
  440. }
  441. if (_eventData[id] === undefined) {
  442. _eventData[id] = {};
  443. }
  444. var events = _eventData[id][type];
  445. if (events && events.length > 0) {
  446. _unbindEvent(el, type, events[0]);
  447. } else {
  448. _eventData[id][type] = [];
  449. _eventData[id].el = el;
  450. }
  451. events = _eventData[id][type];
  452. if (events.length === 0) {
  453. events[0] = function(e) {
  454. var kevent = e ? new KEvent(el, e) : undefined;
  455. _each(events, function(i, event) {
  456. if (i > 0 && event) {
  457. event.call(el, kevent);
  458. }
  459. });
  460. };
  461. }
  462. if (_inArray(fn, events) < 0) {
  463. events.push(fn);
  464. }
  465. _bindEvent(el, type, events[0]);
  466. }
  467. function _unbind(el, type, fn) {
  468. if (type && type.indexOf(',') >= 0) {
  469. _each(type.split(','), function() {
  470. _unbind(el, this, fn);
  471. });
  472. return;
  473. }
  474. var id = _getId(el);
  475. if (!id) {
  476. return;
  477. }
  478. if (type === undefined) {
  479. if (id in _eventData) {
  480. _each(_eventData[id], function(key, events) {
  481. if (key != 'el' && events.length > 0) {
  482. _unbindEvent(el, key, events[0]);
  483. }
  484. });
  485. delete _eventData[id];
  486. _removeId(el);
  487. }
  488. return;
  489. }
  490. if (!_eventData[id]) {
  491. return;
  492. }
  493. var events = _eventData[id][type];
  494. if (events && events.length > 0) {
  495. if (fn === undefined) {
  496. _unbindEvent(el, type, events[0]);
  497. delete _eventData[id][type];
  498. } else {
  499. _each(events, function(i, event) {
  500. if (i > 0 && event === fn) {
  501. events.splice(i, 1);
  502. }
  503. });
  504. if (events.length == 1) {
  505. _unbindEvent(el, type, events[0]);
  506. delete _eventData[id][type];
  507. }
  508. }
  509. var count = 0;
  510. _each(_eventData[id], function() {
  511. count++;
  512. });
  513. if (count < 2) {
  514. delete _eventData[id];
  515. _removeId(el);
  516. }
  517. }
  518. }
  519. function _fire(el, type) {
  520. if (type.indexOf(',') >= 0) {
  521. _each(type.split(','), function() {
  522. _fire(el, this);
  523. });
  524. return;
  525. }
  526. var id = _getId(el);
  527. if (!id) {
  528. return;
  529. }
  530. var events = _eventData[id][type];
  531. if (_eventData[id] && events && events.length > 0) {
  532. events[0]();
  533. }
  534. }
  535. function _ctrl(el, key, fn) {
  536. var self = this;
  537. key = /^\d{2,}$/.test(key) ? key : key.toUpperCase().charCodeAt(0);
  538. _bind(el, 'keydown', function(e) {
  539. if (e.ctrlKey && e.which == key && !e.shiftKey && !e.altKey) {
  540. fn.call(el);
  541. e.stop();
  542. }
  543. });
  544. }
  545. var _readyFinished = false;
  546. function _ready(fn) {
  547. if (_readyFinished) {
  548. fn(KindEditor);
  549. return;
  550. }
  551. var loaded = false;
  552. function readyFunc() {
  553. if (!loaded) {
  554. loaded = true;
  555. fn(KindEditor);
  556. _readyFinished = true;
  557. }
  558. }
  559. function ieReadyFunc() {
  560. if (!loaded) {
  561. try {
  562. document.documentElement.doScroll('left');
  563. } catch(e) {
  564. setTimeout(ieReadyFunc, 100);
  565. return;
  566. }
  567. readyFunc();
  568. }
  569. }
  570. function ieReadyStateFunc() {
  571. if (document.readyState === 'complete') {
  572. readyFunc();
  573. }
  574. }
  575. if (document.addEventListener) {
  576. _bind(document, 'DOMContentLoaded', readyFunc);
  577. } else if (document.attachEvent) {
  578. _bind(document, 'readystatechange', ieReadyStateFunc);
  579. var toplevel = false;
  580. try {
  581. toplevel = window.frameElement == null;
  582. } catch(e) {}
  583. if (document.documentElement.doScroll && toplevel) {
  584. ieReadyFunc();
  585. }
  586. }
  587. _bind(window, 'load', readyFunc);
  588. }
  589. if (_IE) {
  590. window.attachEvent('onunload', function() {
  591. _each(_eventData, function(key, events) {
  592. if (events.el) {
  593. _unbind(events.el);
  594. }
  595. });
  596. });
  597. }
  598. K.ctrl = _ctrl;
  599. K.ready = _ready;
  600. function _getCssList(css) {
  601. var list = {},
  602. reg = /\s*([\w\-]+)\s*:([^;]*)(;|$)/g,
  603. match;
  604. while ((match = reg.exec(css))) {
  605. var key = _trim(match[1].toLowerCase()),
  606. val = _trim(_toHex(match[2]));
  607. list[key] = val;
  608. }
  609. return list;
  610. }
  611. function _getAttrList(tag) {
  612. var list = {},
  613. reg = /\s+(?:([\w\-:]+)|(?:([\w\-:]+)=([^\s"'<>]+))|(?:([\w\-:"]+)="([^"]*)")|(?:([\w\-:"]+)='([^']*)'))(?=(?:\s|\/|>)+)/g,
  614. match;
  615. while ((match = reg.exec(tag))) {
  616. var key = (match[1] || match[2] || match[4] || match[6]).toLowerCase(),
  617. val = (match[2] ? match[3] : (match[4] ? match[5] : match[7])) || '';
  618. list[key] = val;
  619. }
  620. return list;
  621. }
  622. function _addClassToTag(tag, className) {
  623. if (/\s+class\s*=/.test(tag)) {
  624. tag = tag.replace(/(\s+class=["']?)([^"']*)(["']?[\s>])/, function($0, $1, $2, $3) {
  625. if ((' ' + $2 + ' ').indexOf(' ' + className + ' ') < 0) {
  626. return $2 === '' ? $1 + className + $3 : $1 + $2 + ' ' + className + $3;
  627. } else {
  628. return $0;
  629. }
  630. });
  631. } else {
  632. tag = tag.substr(0, tag.length - 1) + ' class="' + className + '">';
  633. }
  634. return tag;
  635. }
  636. function _formatCss(css) {
  637. var str = '';
  638. _each(_getCssList(css), function(key, val) {
  639. str += key + ':' + val + ';';
  640. });
  641. return str;
  642. }
  643. function _formatUrl(url, mode, host, pathname) {
  644. mode = _undef(mode, '').toLowerCase();
  645. if (url.substr(0, 5) != 'data:') {
  646. url = url.replace(/([^:])\/\//g, '$1/');
  647. }
  648. if (_inArray(mode, ['absolute', 'relative', 'domain']) < 0) {
  649. return url;
  650. }
  651. host = host || location.protocol + '//' + location.host;
  652. if (pathname === undefined) {
  653. var m = location.pathname.match(/^(\/.*)\//);
  654. pathname = m ? m[1] : '';
  655. }
  656. var match;
  657. if ((match = /^(\w+:\/\/[^\/]*)/.exec(url))) {
  658. if (match[1] !== host) {
  659. return url;
  660. }
  661. } else if (/^\w+:/.test(url)) {
  662. return url;
  663. }
  664. function getRealPath(path) {
  665. var parts = path.split('/'), paths = [];
  666. for (var i = 0, len = parts.length; i < len; i++) {
  667. var part = parts[i];
  668. if (part == '..') {
  669. if (paths.length > 0) {
  670. paths.pop();
  671. }
  672. } else if (part !== '' && part != '.') {
  673. paths.push(part);
  674. }
  675. }
  676. return '/' + paths.join('/');
  677. }
  678. if (/^\//.test(url)) {
  679. url = host + getRealPath(url.substr(1));
  680. } else if (!/^\w+:\/\//.test(url)) {
  681. url = host + getRealPath(pathname + '/' + url);
  682. }
  683. function getRelativePath(path, depth) {
  684. if (url.substr(0, path.length) === path) {
  685. var arr = [];
  686. for (var i = 0; i < depth; i++) {
  687. arr.push('..');
  688. }
  689. var prefix = '.';
  690. if (arr.length > 0) {
  691. prefix += '/' + arr.join('/');
  692. }
  693. if (pathname == '/') {
  694. prefix += '/';
  695. }
  696. return prefix + url.substr(path.length);
  697. } else {
  698. if ((match = /^(.*)\//.exec(path))) {
  699. return getRelativePath(match[1], ++depth);
  700. }
  701. }
  702. }
  703. if (mode === 'relative') {
  704. url = getRelativePath(host + pathname, 0).substr(2);
  705. } else if (mode === 'absolute') {
  706. if (url.substr(0, host.length) === host) {
  707. url = url.substr(host.length);
  708. }
  709. }
  710. return url;
  711. }
  712. function _formatHtml(html, htmlTags, urlType, wellFormatted, indentChar) {
  713. if (html == null) {
  714. html = '';
  715. }
  716. urlType = urlType || '';
  717. wellFormatted = _undef(wellFormatted, false);
  718. indentChar = _undef(indentChar, '\t');
  719. var fontSizeList = 'xx-small,x-small,small,medium,large,x-large,xx-large'.split(',');
  720. html = html.replace(/(<(?:pre|pre\s[^>]*)>)([\s\S]*?)(<\/pre>)/ig, function($0, $1, $2, $3) {
  721. return $1 + $2.replace(/<(?:br|br\s[^>]*)>/ig, '\n') + $3;
  722. });
  723. html = html.replace(/<(?:br|br\s[^>]*)\s*\/?>\s*<\/p>/ig, '</p>');
  724. html = html.replace(/(<(?:p|p\s[^>]*)>)\s*(<\/p>)/ig, '$1<br />$2');
  725. html = html.replace(/\u200B/g, '');
  726. html = html.replace(/\u00A9/g, '&copy;');
  727. html = html.replace(/\u00AE/g, '&reg;');
  728. html = html.replace(/<[^>]+/g, function($0) {
  729. return $0.replace(/\s+/g, ' ');
  730. });
  731. var htmlTagMap = {};
  732. if (htmlTags) {
  733. _each(htmlTags, function(key, val) {
  734. var arr = key.split(',');
  735. for (var i = 0, len = arr.length; i < len; i++) {
  736. htmlTagMap[arr[i]] = _toMap(val);
  737. }
  738. });
  739. if (!htmlTagMap.script) {
  740. html = html.replace(/(<(?:script|script\s[^>]*)>)([\s\S]*?)(<\/script>)/ig, '');
  741. }
  742. if (!htmlTagMap.style) {
  743. html = html.replace(/(<(?:style|style\s[^>]*)>)([\s\S]*?)(<\/style>)/ig, '');
  744. }
  745. }
  746. var re = /(\s*)<(\/)?([\w\-:]+)((?:\s+|(?:\s+[\w\-:]+)|(?:\s+[\w\-:]+=[^\s"'<>]+)|(?:\s+[\w\-:"]+="[^"]*")|(?:\s+[\w\-:"]+='[^']*'))*)(\/)?>(\s*)/g;
  747. var tagStack = [];
  748. html = html.replace(re, function($0, $1, $2, $3, $4, $5, $6) {
  749. var full = $0,
  750. startNewline = $1 || '',
  751. startSlash = $2 || '',
  752. tagName = $3.toLowerCase(),
  753. attr = $4 || '',
  754. endSlash = $5 ? ' ' + $5 : '',
  755. endNewline = $6 || '';
  756. if (htmlTags && !htmlTagMap[tagName]) {
  757. return '';
  758. }
  759. if (endSlash === '' && _SINGLE_TAG_MAP[tagName]) {
  760. endSlash = ' /';
  761. }
  762. if (_INLINE_TAG_MAP[tagName]) {
  763. if (startNewline) {
  764. startNewline = ' ';
  765. }
  766. if (endNewline) {
  767. endNewline = ' ';
  768. }
  769. }
  770. if (_PRE_TAG_MAP[tagName]) {
  771. if (startSlash) {
  772. endNewline = '\n';
  773. } else {
  774. startNewline = '\n';
  775. }
  776. }
  777. if (wellFormatted && tagName == 'br') {
  778. endNewline = '\n';
  779. }
  780. if (_BLOCK_TAG_MAP[tagName] && !_PRE_TAG_MAP[tagName]) {
  781. if (wellFormatted) {
  782. if (startSlash && tagStack.length > 0 && tagStack[tagStack.length - 1] === tagName) {
  783. tagStack.pop();
  784. } else {
  785. tagStack.push(tagName);
  786. }
  787. startNewline = '\n';
  788. endNewline = '\n';
  789. for (var i = 0, len = startSlash ? tagStack.length : tagStack.length - 1; i < len; i++) {
  790. startNewline += indentChar;
  791. if (!startSlash) {
  792. endNewline += indentChar;
  793. }
  794. }
  795. if (endSlash) {
  796. tagStack.pop();
  797. } else if (!startSlash) {
  798. endNewline += indentChar;
  799. }
  800. } else {
  801. startNewline = endNewline = '';
  802. }
  803. }
  804. if (attr !== '') {
  805. var attrMap = _getAttrList(full);
  806. if (tagName === 'font') {
  807. var fontStyleMap = {}, fontStyle = '';
  808. _each(attrMap, function(key, val) {
  809. if (key === 'color') {
  810. fontStyleMap.color = val;
  811. delete attrMap[key];
  812. }
  813. if (key === 'size') {
  814. fontStyleMap['font-size'] = fontSizeList[parseInt(val, 10) - 1] || '';
  815. delete attrMap[key];
  816. }
  817. if (key === 'face') {
  818. fontStyleMap['font-family'] = val;
  819. delete attrMap[key];
  820. }
  821. if (key === 'style') {
  822. fontStyle = val;
  823. }
  824. });
  825. if (fontStyle && !/;$/.test(fontStyle)) {
  826. fontStyle += ';';
  827. }
  828. _each(fontStyleMap, function(key, val) {
  829. if (val === '') {
  830. return;
  831. }
  832. if (/\s/.test(val)) {
  833. val = "'" + val + "'";
  834. }
  835. fontStyle += key + ':' + val + ';';
  836. });
  837. attrMap.style = fontStyle;
  838. }
  839. _each(attrMap, function(key, val) {
  840. if (_FILL_ATTR_MAP[key]) {
  841. attrMap[key] = key;
  842. }
  843. if (_inArray(key, ['src', 'href']) >= 0) {
  844. attrMap[key] = _formatUrl(val, urlType);
  845. }
  846. if (htmlTags && key !== 'style' && !htmlTagMap[tagName]['*'] && !htmlTagMap[tagName][key] ||
  847. tagName === 'body' && key === 'contenteditable' ||
  848. /^kindeditor_\d+$/.test(key)) {
  849. delete attrMap[key];
  850. }
  851. if (key === 'style' && val !== '') {
  852. var styleMap = _getCssList(val);
  853. _each(styleMap, function(k, v) {
  854. if (htmlTags && !htmlTagMap[tagName].style && !htmlTagMap[tagName]['.' + k]) {
  855. delete styleMap[k];
  856. }
  857. });
  858. var style = '';
  859. _each(styleMap, function(k, v) {
  860. style += k + ':' + v + ';';
  861. });
  862. attrMap.style = style;
  863. }
  864. });
  865. attr = '';
  866. _each(attrMap, function(key, val) {
  867. if (key === 'style' && val === '') {
  868. return;
  869. }
  870. val = val.replace(/"/g, '&quot;');
  871. attr += ' ' + key + '="' + val + '"';
  872. });
  873. }
  874. if (tagName === 'font') {
  875. tagName = 'span';
  876. }
  877. return startNewline + '<' + startSlash + tagName + attr + endSlash + '>' + endNewline;
  878. });
  879. html = html.replace(/(<(?:pre|pre\s[^>]*)>)([\s\S]*?)(<\/pre>)/ig, function($0, $1, $2, $3) {
  880. return $1 + $2.replace(/\n/g, '<span id="__kindeditor_pre_newline__">\n') + $3;
  881. });
  882. html = html.replace(/\n\s*\n/g, '\n');
  883. html = html.replace(/<span id="__kindeditor_pre_newline__">\n/g, '\n');
  884. return _trim(html);
  885. }
  886. function _clearMsWord(html, htmlTags) {
  887. html = html.replace(/<meta[\s\S]*?>/ig, '')
  888. .replace(/<![\s\S]*?>/ig, '')
  889. .replace(/<style[^>]*>[\s\S]*?<\/style>/ig, '')
  890. .replace(/<script[^>]*>[\s\S]*?<\/script>/ig, '')
  891. .replace(/<w:[^>]+>[\s\S]*?<\/w:[^>]+>/ig, '')
  892. .replace(/<o:[^>]+>[\s\S]*?<\/o:[^>]+>/ig, '')
  893. .replace(/<xml>[\s\S]*?<\/xml>/ig, '')
  894. .replace(/<(?:table|td)[^>]*>/ig, function(full) {
  895. return full.replace(/border-bottom:([#\w\s]+)/ig, 'border:$1');
  896. });
  897. return _formatHtml(html, htmlTags);
  898. }
  899. function _mediaType(src) {
  900. if (/\.(rm|rmvb)(\?|$)/i.test(src)) {
  901. return 'audio/x-pn-realaudio-plugin';
  902. }
  903. if (/\.(swf|flv)(\?|$)/i.test(src)) {
  904. return 'application/x-shockwave-flash';
  905. }
  906. return 'video/x-ms-asf-plugin';
  907. }
  908. function _mediaClass(type) {
  909. if (/realaudio/i.test(type)) {
  910. return 'ke-rm';
  911. }
  912. if (/flash/i.test(type)) {
  913. return 'ke-flash';
  914. }
  915. return 'ke-media';
  916. }
  917. function _mediaAttrs(srcTag) {
  918. return _getAttrList(unescape(srcTag));
  919. }
  920. function _mediaEmbed(attrs) {
  921. var html = '<embed ';
  922. _each(attrs, function(key, val) {
  923. html += key + '="' + val + '" ';
  924. });
  925. html += '/>';
  926. return html;
  927. }
  928. function _mediaImg(blankPath, attrs) {
  929. var width = attrs.width,
  930. height = attrs.height,
  931. type = attrs.type || _mediaType(attrs.src),
  932. srcTag = _mediaEmbed(attrs),
  933. style = '';
  934. if (/\D/.test(width)) {
  935. style += 'width:' + width + ';';
  936. } else if (width > 0) {
  937. style += 'width:' + width + 'px;';
  938. }
  939. if (/\D/.test(height)) {
  940. style += 'height:' + height + ';';
  941. } else if (height > 0) {
  942. style += 'height:' + height + 'px;';
  943. }
  944. var html = '<img class="' + _mediaClass(type) + '" src="' + blankPath + '" ';
  945. if (style !== '') {
  946. html += 'style="' + style + '" ';
  947. }
  948. html += 'data-ke-tag="' + escape(srcTag) + '" alt="" />';
  949. return html;
  950. }
  951. function _tmpl(str, data) {
  952. var fn = new Function("obj",
  953. "var p=[],print=function(){p.push.apply(p,arguments);};" +
  954. "with(obj){p.push('" +
  955. str.replace(/[\r\t\n]/g, " ")
  956. .split("<%").join("\t")
  957. .replace(/((^|%>)[^\t]*)'/g, "$1\r")
  958. .replace(/\t=(.*?)%>/g, "',$1,'")
  959. .split("\t").join("');")
  960. .split("%>").join("p.push('")
  961. .split("\r").join("\\'") + "');}return p.join('');");
  962. return data ? fn(data) : fn;
  963. }
  964. K.formatUrl = _formatUrl;
  965. K.formatHtml = _formatHtml;
  966. K.getCssList = _getCssList;
  967. K.getAttrList = _getAttrList;
  968. K.mediaType = _mediaType;
  969. K.mediaAttrs = _mediaAttrs;
  970. K.mediaEmbed = _mediaEmbed;
  971. K.mediaImg = _mediaImg;
  972. K.clearMsWord = _clearMsWord;
  973. K.tmpl = _tmpl;
  974. function _contains(nodeA, nodeB) {
  975. if (nodeA.nodeType == 9 && nodeB.nodeType != 9) {
  976. return true;
  977. }
  978. while ((nodeB = nodeB.parentNode)) {
  979. if (nodeB == nodeA) {
  980. return true;
  981. }
  982. }
  983. return false;
  984. }
  985. var _getSetAttrDiv = document.createElement('div');
  986. _getSetAttrDiv.setAttribute('className', 't');
  987. var _GET_SET_ATTRIBUTE = _getSetAttrDiv.className !== 't';
  988. function _getAttr(el, key) {
  989. key = key.toLowerCase();
  990. var val = null;
  991. if (!_GET_SET_ATTRIBUTE && el.nodeName.toLowerCase() != 'script') {
  992. var div = el.ownerDocument.createElement('div');
  993. div.appendChild(el.cloneNode(false));
  994. var list = _getAttrList(_unescape(div.innerHTML));
  995. if (key in list) {
  996. val = list[key];
  997. }
  998. } else {
  999. try {
  1000. val = el.getAttribute(key, 2);
  1001. } catch(e) {
  1002. val = el.getAttribute(key, 1);
  1003. }
  1004. }
  1005. if (key === 'style' && val !== null) {
  1006. val = _formatCss(val);
  1007. }
  1008. return val;
  1009. }
  1010. function _queryAll(expr, root) {
  1011. var exprList = expr.split(',');
  1012. if (exprList.length > 1) {
  1013. var mergedResults = [];
  1014. _each(exprList, function() {
  1015. _each(_queryAll(this, root), function() {
  1016. if (_inArray(this, mergedResults) < 0) {
  1017. mergedResults.push(this);
  1018. }
  1019. });
  1020. });
  1021. return mergedResults;
  1022. }
  1023. root = root || document;
  1024. function escape(str) {
  1025. if (typeof str != 'string') {
  1026. return str;
  1027. }
  1028. return str.replace(/([^\w\-])/g, '\\$1');
  1029. }
  1030. function stripslashes(str) {
  1031. return str.replace(/\\/g, '');
  1032. }
  1033. function cmpTag(tagA, tagB) {
  1034. return tagA === '*' || tagA.toLowerCase() === escape(tagB.toLowerCase());
  1035. }
  1036. function byId(id, tag, root) {
  1037. var arr = [],
  1038. doc = root.ownerDocument || root,
  1039. el = doc.getElementById(stripslashes(id));
  1040. if (el) {
  1041. if (cmpTag(tag, el.nodeName) && _contains(root, el)) {
  1042. arr.push(el);
  1043. }
  1044. }
  1045. return arr;
  1046. }
  1047. function byClass(className, tag, root) {
  1048. var doc = root.ownerDocument || root, arr = [], els, i, len, el;
  1049. if (root.getElementsByClassName) {
  1050. els = root.getElementsByClassName(stripslashes(className));
  1051. for (i = 0, len = els.length; i < len; i++) {
  1052. el = els[i];
  1053. if (cmpTag(tag, el.nodeName)) {
  1054. arr.push(el);
  1055. }
  1056. }
  1057. } else if (doc.querySelectorAll) {
  1058. els = doc.querySelectorAll((root.nodeName !== '#document' ? root.nodeName + ' ' : '') + tag + '.' + className);
  1059. for (i = 0, len = els.length; i < len; i++) {
  1060. el = els[i];
  1061. if (_contains(root, el)) {
  1062. arr.push(el);
  1063. }
  1064. }
  1065. } else {
  1066. els = root.getElementsByTagName(tag);
  1067. className = ' ' + className + ' ';
  1068. for (i = 0, len = els.length; i < len; i++) {
  1069. el = els[i];
  1070. if (el.nodeType == 1) {
  1071. var cls = el.className;
  1072. if (cls && (' ' + cls + ' ').indexOf(className) > -1) {
  1073. arr.push(el);
  1074. }
  1075. }
  1076. }
  1077. }
  1078. return arr;
  1079. }
  1080. function byName(name, tag, root) {
  1081. var arr = [], doc = root.ownerDocument || root,
  1082. els = doc.getElementsByName(stripslashes(name)), el;
  1083. for (var i = 0, len = els.length; i < len; i++) {
  1084. el = els[i];
  1085. if (cmpTag(tag, el.nodeName) && _contains(root, el)) {
  1086. if (el.getAttribute('name') !== null) {
  1087. arr.push(el);
  1088. }
  1089. }
  1090. }
  1091. return arr;
  1092. }
  1093. function byAttr(key, val, tag, root) {
  1094. var arr = [], els = root.getElementsByTagName(tag), el;
  1095. for (var i = 0, len = els.length; i < len; i++) {
  1096. el = els[i];
  1097. if (el.nodeType == 1) {
  1098. if (val === null) {
  1099. if (_getAttr(el, key) !== null) {
  1100. arr.push(el);
  1101. }
  1102. } else {
  1103. if (val === escape(_getAttr(el, key))) {
  1104. arr.push(el);
  1105. }
  1106. }
  1107. }
  1108. }
  1109. return arr;
  1110. }
  1111. function select(expr, root) {
  1112. var arr = [], matches;
  1113. matches = /^((?:\\.|[^.#\s\[<>])+)/.exec(expr);
  1114. var tag = matches ? matches[1] : '*';
  1115. if ((matches = /#((?:[\w\-]|\\.)+)$/.exec(expr))) {
  1116. arr = byId(matches[1], tag, root);
  1117. } else if ((matches = /\.((?:[\w\-]|\\.)+)$/.exec(expr))) {
  1118. arr = byClass(matches[1], tag, root);
  1119. } else if ((matches = /\[((?:[\w\-]|\\.)+)\]/.exec(expr))) {
  1120. arr = byAttr(matches[1].toLowerCase(), null, tag, root);
  1121. } else if ((matches = /\[((?:[\w\-]|\\.)+)\s*=\s*['"]?((?:\\.|[^'"]+)+)['"]?\]/.exec(expr))) {
  1122. var key = matches[1].toLowerCase(), val = matches[2];
  1123. if (key === 'id') {
  1124. arr = byId(val, tag, root);
  1125. } else if (key === 'class') {
  1126. arr = byClass(val, tag, root);
  1127. } else if (key === 'name') {
  1128. arr = byName(val, tag, root);
  1129. } else {
  1130. arr = byAttr(key, val, tag, root);
  1131. }
  1132. } else {
  1133. var els = root.getElementsByTagName(tag), el;
  1134. for (var i = 0, len = els.length; i < len; i++) {
  1135. el = els[i];
  1136. if (el.nodeType == 1) {
  1137. arr.push(el);
  1138. }
  1139. }
  1140. }
  1141. return arr;
  1142. }
  1143. var parts = [], arr, re = /((?:\\.|[^\s>])+|[\s>])/g;
  1144. while ((arr = re.exec(expr))) {
  1145. if (arr[1] !== ' ') {
  1146. parts.push(arr[1]);
  1147. }
  1148. }
  1149. var results = [];
  1150. if (parts.length == 1) {
  1151. return select(parts[0], root);
  1152. }
  1153. var isChild = false, part, els, subResults, val, v, i, j, k, length, len, l;
  1154. for (i = 0, lenth = parts.length; i < lenth; i++) {
  1155. part = parts[i];
  1156. if (part === '>') {
  1157. isChild = true;
  1158. continue;
  1159. }
  1160. if (i > 0) {
  1161. els = [];
  1162. for (j = 0, len = results.length; j < len; j++) {
  1163. val = results[j];
  1164. subResults = select(part, val);
  1165. for (k = 0, l = subResults.length; k < l; k++) {
  1166. v = subResults[k];
  1167. if (isChild) {
  1168. if (val === v.parentNode) {
  1169. els.push(v);
  1170. }
  1171. } else {
  1172. els.push(v);
  1173. }
  1174. }
  1175. }
  1176. results = els;
  1177. } else {
  1178. results = select(part, root);
  1179. }
  1180. if (results.length === 0) {
  1181. return [];
  1182. }
  1183. }
  1184. return results;
  1185. }
  1186. function _query(expr, root) {
  1187. var arr = _queryAll(expr, root);
  1188. return arr.length > 0 ? arr[0] : null;
  1189. }
  1190. K.query = _query;
  1191. K.queryAll = _queryAll;
  1192. function _get(val) {
  1193. return K(val)[0];
  1194. }
  1195. function _getDoc(node) {
  1196. if (!node) {
  1197. return document;
  1198. }
  1199. return node.ownerDocument || node.document || node;
  1200. }
  1201. function _getWin(node) {
  1202. if (!node) {
  1203. return window;
  1204. }
  1205. var doc = _getDoc(node);
  1206. return doc.parentWindow || doc.defaultView;
  1207. }
  1208. function _setHtml(el, html) {
  1209. if (el.nodeType != 1) {
  1210. return;
  1211. }
  1212. var doc = _getDoc(el);
  1213. try {
  1214. el.innerHTML = '<img id="__kindeditor_temp_tag__" width="0" height="0" style="display:none;" />' + html;
  1215. var temp = doc.getElementById('__kindeditor_temp_tag__');
  1216. temp.parentNode.removeChild(temp);
  1217. } catch(e) {
  1218. K(el).empty();
  1219. K('@' + html, doc).each(function() {
  1220. el.appendChild(this);
  1221. });
  1222. }
  1223. }
  1224. function _hasClass(el, cls) {
  1225. return _inString(cls, el.className, ' ');
  1226. }
  1227. function _setAttr(el, key, val) {
  1228. if (_IE && _V < 8 && key.toLowerCase() == 'class') {
  1229. key = 'className';
  1230. }
  1231. el.setAttribute(key, '' + val);
  1232. }
  1233. function _removeAttr(el, key) {
  1234. if (_IE && _V < 8 && key.toLowerCase() == 'class') {
  1235. key = 'className';
  1236. }
  1237. _setAttr(el, key, '');
  1238. el.removeAttribute(key);
  1239. }
  1240. function _getNodeName(node) {
  1241. if (!node || !node.nodeName) {
  1242. return '';
  1243. }
  1244. return node.nodeName.toLowerCase();
  1245. }
  1246. function _computedCss(el, key) {
  1247. var self = this, win = _getWin(el), camelKey = _toCamel(key), val = '';
  1248. if (win.getComputedStyle) {
  1249. var style = win.getComputedStyle(el, null);
  1250. val = style[camelKey] || style.getPropertyValue(key) || el.style[camelKey];
  1251. } else if (el.currentStyle) {
  1252. val = el.currentStyle[camelKey] || el.style[camelKey];
  1253. }
  1254. return val;
  1255. }
  1256. function _hasVal(node) {
  1257. return !!_VALUE_TAG_MAP[_getNodeName(node)];
  1258. }
  1259. function _docElement(doc) {
  1260. doc = doc || document;
  1261. return _QUIRKS ? doc.body : doc.documentElement;
  1262. }
  1263. function _docHeight(doc) {
  1264. var el = _docElement(doc);
  1265. return Math.max(el.scrollHeight, el.clientHeight);
  1266. }
  1267. function _docWidth(doc) {
  1268. var el = _docElement(doc);
  1269. return Math.max(el.scrollWidth, el.clientWidth);
  1270. }
  1271. function _getScrollPos(doc) {
  1272. doc = doc || document;
  1273. var x, y;
  1274. if (_IE || _NEWIE || _OPERA) {
  1275. x = _docElement(doc).scrollLeft;
  1276. y = _docElement(doc).scrollTop;
  1277. } else {
  1278. x = _getWin(doc).scrollX;
  1279. y = _getWin(doc).scrollY;
  1280. }
  1281. return {x : x, y : y};
  1282. }
  1283. function KNode(node) {
  1284. this.init(node);
  1285. }
  1286. _extend(KNode, {
  1287. init : function(node) {
  1288. var self = this;
  1289. node = _isArray(node) ? node : [node];
  1290. var length = 0;
  1291. for (var i = 0, len = node.length; i < len; i++) {
  1292. if (node[i]) {
  1293. self[i] = node[i].constructor === KNode ? node[i][0] : node[i];
  1294. length++;
  1295. }
  1296. }
  1297. self.length = length;
  1298. self.doc = _getDoc(self[0]);
  1299. self.name = _getNodeName(self[0]);
  1300. self.type = self.length > 0 ? self[0].nodeType : null;
  1301. self.win = _getWin(self[0]);
  1302. },
  1303. each : function(fn) {
  1304. var self = this;
  1305. for (var i = 0; i < self.length; i++) {
  1306. if (fn.call(self[i], i, self[i]) === false) {
  1307. return self;
  1308. }
  1309. }
  1310. return self;
  1311. },
  1312. bind : function(type, fn) {
  1313. this.each(function() {
  1314. _bind(this, type, fn);
  1315. });
  1316. return this;
  1317. },
  1318. unbind : function(type, fn) {
  1319. this.each(function() {
  1320. _unbind(this, type, fn);
  1321. });
  1322. return this;
  1323. },
  1324. fire : function(type) {
  1325. if (this.length < 1) {
  1326. return this;
  1327. }
  1328. _fire(this[0], type);
  1329. return this;
  1330. },
  1331. hasAttr : function(key) {
  1332. if (this.length < 1) {
  1333. return false;
  1334. }
  1335. return !!_getAttr(this[0], key);
  1336. },
  1337. attr : function(key, val) {
  1338. var self = this;
  1339. if (key === undefined) {
  1340. return _getAttrList(self.outer());
  1341. }
  1342. if (typeof key === 'object') {
  1343. _each(key, function(k, v) {
  1344. self.attr(k, v);
  1345. });
  1346. return self;
  1347. }
  1348. if (val === undefined) {
  1349. val = self.length < 1 ? null : _getAttr(self[0], key);
  1350. return val === null ? '' : val;
  1351. }
  1352. self.each(function() {
  1353. _setAttr(this, key, val);
  1354. });
  1355. return self;
  1356. },
  1357. removeAttr : function(key) {
  1358. this.each(function() {
  1359. _removeAttr(this, key);
  1360. });
  1361. return this;
  1362. },
  1363. get : function(i) {
  1364. if (this.length < 1) {
  1365. return null;
  1366. }
  1367. return this[i || 0];
  1368. },
  1369. eq : function(i) {
  1370. if (this.length < 1) {
  1371. return null;
  1372. }
  1373. return this[i] ? new KNode(this[i]) : null;
  1374. },
  1375. hasClass : function(cls) {
  1376. if (this.length < 1) {
  1377. return false;
  1378. }
  1379. return _hasClass(this[0], cls);
  1380. },
  1381. addClass : function(cls) {
  1382. this.each(function() {
  1383. if (!_hasClass(this, cls)) {
  1384. this.className = _trim(this.className + ' ' + cls);
  1385. }
  1386. });
  1387. return this;
  1388. },
  1389. removeClass : function(cls) {
  1390. this.each(function() {
  1391. if (_hasClass(this, cls)) {
  1392. this.className = _trim(this.className.replace(new RegExp('(^|\\s)' + cls + '(\\s|$)'), ' '));
  1393. }
  1394. });
  1395. return this;
  1396. },
  1397. html : function(val) {
  1398. var self = this;
  1399. if (val === undefined) {
  1400. if (self.length < 1 || self.type != 1) {
  1401. return '';
  1402. }
  1403. return _formatHtml(self[0].innerHTML);
  1404. }
  1405. self.each(function() {
  1406. _setHtml(this, val);
  1407. });
  1408. return self;
  1409. },
  1410. text : function() {
  1411. var self = this;
  1412. if (self.length < 1) {
  1413. return '';
  1414. }
  1415. return _IE ? self[0].innerText : self[0].textContent;
  1416. },
  1417. hasVal : function() {
  1418. if (this.length < 1) {
  1419. return false;
  1420. }
  1421. return _hasVal(this[0]);
  1422. },
  1423. val : function(val) {
  1424. var self = this;
  1425. if (val === undefined) {
  1426. if (self.length < 1) {
  1427. return '';
  1428. }
  1429. return self.hasVal() ? self[0].value : self.attr('value');
  1430. } else {
  1431. self.each(function() {
  1432. if (_hasVal(this)) {
  1433. this.value = val;
  1434. } else {
  1435. _setAttr(this, 'value' , val);
  1436. }
  1437. });
  1438. return self;
  1439. }
  1440. },
  1441. css : function(key, val) {
  1442. var self = this;
  1443. if (key === undefined) {
  1444. return _getCssList(self.attr('style'));
  1445. }
  1446. if (typeof key === 'object') {
  1447. _each(key, function(k, v) {
  1448. self.css(k, v);
  1449. });
  1450. return self;
  1451. }
  1452. if (val === undefined) {
  1453. if (self.length < 1) {
  1454. return '';
  1455. }
  1456. return self[0].style[_toCamel(key)] || _computedCss(self[0], key) || '';
  1457. }
  1458. self.each(function() {
  1459. this.style[_toCamel(key)] = val;
  1460. });
  1461. return self;
  1462. },
  1463. width : function(val) {
  1464. var self = this;
  1465. if (val === undefined) {
  1466. if (self.length < 1) {
  1467. return 0;
  1468. }
  1469. return self[0].offsetWidth;
  1470. }
  1471. return self.css('width', _addUnit(val));
  1472. },
  1473. height : function(val) {
  1474. var self = this;
  1475. if (val === undefined) {
  1476. if (self.length < 1) {
  1477. return 0;
  1478. }
  1479. return self[0].offsetHeight;
  1480. }
  1481. return self.css('height', _addUnit(val));
  1482. },
  1483. opacity : function(val) {
  1484. this.each(function() {
  1485. if (this.style.opacity === undefined) {
  1486. this.style.filter = val == 1 ? '' : 'alpha(opacity=' + (val * 100) + ')';
  1487. } else {
  1488. this.style.opacity = val == 1 ? '' : val;
  1489. }
  1490. });
  1491. return this;
  1492. },
  1493. data : function(key, val) {
  1494. var self = this;
  1495. key = 'kindeditor_data_' + key;
  1496. if (val === undefined) {
  1497. if (self.length < 1) {
  1498. return null;
  1499. }
  1500. return self[0][key];
  1501. }
  1502. this.each(function() {
  1503. this[key] = val;
  1504. });
  1505. return self;
  1506. },
  1507. pos : function() {
  1508. var self = this, node = self[0], x = 0, y = 0;
  1509. if (node) {
  1510. if (node.getBoundingClientRect) {
  1511. var box = node.getBoundingClientRect(),
  1512. pos = _getScrollPos(self.doc);
  1513. x = box.left + pos.x;
  1514. y = box.top + pos.y;
  1515. } else {
  1516. while (node) {
  1517. x += node.offsetLeft;
  1518. y += node.offsetTop;
  1519. node = node.offsetParent;
  1520. }
  1521. }
  1522. }
  1523. return {x : _round(x), y : _round(y)};
  1524. },
  1525. clone : function(bool) {
  1526. if (this.length < 1) {
  1527. return new KNode([]);
  1528. }
  1529. return new KNode(this[0].cloneNode(bool));
  1530. },
  1531. append : function(expr) {
  1532. this.each(function() {
  1533. if (this.appendChild) {
  1534. this.appendChild(_get(expr));
  1535. }
  1536. });
  1537. return this;
  1538. },
  1539. appendTo : function(expr) {
  1540. this.each(function() {
  1541. _get(expr).appendChild(this);
  1542. });
  1543. return this;
  1544. },
  1545. before : function(expr) {
  1546. this.each(function() {
  1547. this.parentNode.insertBefore(_get(expr), this);
  1548. });
  1549. return this;
  1550. },
  1551. after : function(expr) {
  1552. this.each(function() {
  1553. if (this.nextSibling) {
  1554. this.parentNode.insertBefore(_get(expr), this.nextSibling);
  1555. } else {
  1556. this.parentNode.appendChild(_get(expr));
  1557. }
  1558. });
  1559. return this;
  1560. },
  1561. replaceWith : function(expr) {
  1562. var nodes = [];
  1563. this.each(function(i, node) {
  1564. _unbind(node);
  1565. var newNode = _get(expr);
  1566. node.parentNode.replaceChild(newNode, node);
  1567. nodes.push(newNode);
  1568. });
  1569. return K(nodes);
  1570. },
  1571. empty : function() {
  1572. var self = this;
  1573. self.each(function(i, node) {
  1574. var child = node.firstChild;
  1575. while (child) {
  1576. if (!node.parentNode) {
  1577. return;
  1578. }
  1579. var next = child.nextSibling;
  1580. child.parentNode.removeChild(child);
  1581. child = next;
  1582. }
  1583. });
  1584. return self;
  1585. },
  1586. remove : function(keepChilds) {
  1587. var self = this;
  1588. self.each(function(i, node) {
  1589. if (!node.parentNode) {
  1590. return;
  1591. }
  1592. _unbind(node);
  1593. if (keepChilds) {
  1594. var child = node.firstChild;
  1595. while (child) {
  1596. var next = child.nextSibling;
  1597. node.parentNode.insertBefore(child, node);
  1598. child = next;
  1599. }
  1600. }
  1601. node.parentNode.removeChild(node);
  1602. delete self[i];
  1603. });
  1604. self.length = 0;
  1605. return self;
  1606. },
  1607. show : function(val) {
  1608. var self = this;
  1609. if (val === undefined) {
  1610. val = self._originDisplay || '';
  1611. }
  1612. if (self.css('display') != 'none') {
  1613. return self;
  1614. }
  1615. return self.css('display', val);
  1616. },
  1617. hide : function() {
  1618. var self = this;
  1619. if (self.length < 1) {
  1620. return self;
  1621. }
  1622. self._originDisplay = self[0].style.display;
  1623. return self.css('display', 'none');
  1624. },
  1625. outer : function() {
  1626. var self = this;
  1627. if (self.length < 1) {
  1628. return '';
  1629. }
  1630. var div = self.doc.createElement('div'), html;
  1631. div.appendChild(self[0].cloneNode(true));
  1632. html = _formatHtml(div.innerHTML);
  1633. div = null;
  1634. return html;
  1635. },
  1636. isSingle : function() {
  1637. return !!_SINGLE_TAG_MAP[this.name];
  1638. },
  1639. isInline : function() {
  1640. return !!_INLINE_TAG_MAP[this.name];
  1641. },
  1642. isBlock : function() {
  1643. return !!_BLOCK_TAG_MAP[this.name];
  1644. },
  1645. isStyle : function() {
  1646. return !!_STYLE_TAG_MAP[this.name];
  1647. },
  1648. isControl : function() {
  1649. return !!_CONTROL_TAG_MAP[this.name];
  1650. },
  1651. contains : function(otherNode) {
  1652. if (this.length < 1) {
  1653. return false;
  1654. }
  1655. return _contains(this[0], _get(otherNode));
  1656. },
  1657. parent : function() {
  1658. if (this.length < 1) {
  1659. return null;
  1660. }
  1661. var node = this[0].parentNode;
  1662. return node ? new KNode(node) : null;
  1663. },
  1664. children : function() {
  1665. if (this.length < 1) {
  1666. return new KNode([]);
  1667. }
  1668. var list = [], child = this[0].firstChild;
  1669. while (child) {
  1670. if (child.nodeType != 3 || _trim(child.nodeValue) !== '') {
  1671. list.push(child);
  1672. }
  1673. child = child.nextSibling;
  1674. }
  1675. return new KNode(list);
  1676. },
  1677. first : function() {
  1678. var list = this.children();
  1679. return list.length > 0 ? list.eq(0) : null;
  1680. },
  1681. last : function() {
  1682. var list = this.children();
  1683. return list.length > 0 ? list.eq(list.length - 1) : null;
  1684. },
  1685. index : function() {
  1686. if (this.length < 1) {
  1687. return -1;
  1688. }
  1689. var i = -1, sibling = this[0];
  1690. while (sibling) {
  1691. i++;
  1692. sibling = sibling.previousSibling;
  1693. }
  1694. return i;
  1695. },
  1696. prev : function() {
  1697. if (this.length < 1) {
  1698. return null;
  1699. }
  1700. var node = this[0].previousSibling;
  1701. return node ? new KNode(node) : null;
  1702. },
  1703. next : function() {
  1704. if (this.length < 1) {
  1705. return null;
  1706. }
  1707. var node = this[0].nextSibling;
  1708. return node ? new KNode(node) : null;
  1709. },
  1710. scan : function(fn, order) {
  1711. if (this.length < 1) {
  1712. return;
  1713. }
  1714. order = (order === undefined) ? true : order;
  1715. function walk(node) {
  1716. var n = order ? node.firstChild : node.lastChild;
  1717. while (n) {
  1718. var next = order ? n.nextSibling : n.previousSibling;
  1719. if (fn(n) === false) {
  1720. return false;
  1721. }
  1722. if (walk(n) === false) {
  1723. return false;
  1724. }
  1725. n = next;
  1726. }
  1727. }
  1728. walk(this[0]);
  1729. return this;
  1730. }
  1731. });
  1732. _each(('blur,focus,focusin,focusout,load,resize,scroll,unload,click,dblclick,' +
  1733. 'mousedown,mouseup,mousemove,mouseover,mouseout,mouseenter,mouseleave,' +
  1734. 'change,select,submit,keydown,keypress,keyup,error,contextmenu').split(','), function(i, type) {
  1735. KNode.prototype[type] = function(fn) {
  1736. return fn ? this.bind(type, fn) : this.fire(type);
  1737. };
  1738. });
  1739. var _K = K;
  1740. K = function(expr, root) {
  1741. if (expr === undefined || expr === null) {
  1742. return;
  1743. }
  1744. function newNode(node) {
  1745. if (!node[0]) {
  1746. node = [];
  1747. }
  1748. return new KNode(node);
  1749. }
  1750. if (typeof expr === 'string') {
  1751. if (root) {
  1752. root = _get(root);
  1753. }
  1754. var length = expr.length;
  1755. if (expr.charAt(0) === '@') {
  1756. expr = expr.substr(1);
  1757. }
  1758. if (expr.length !== length || /<.+>/.test(expr)) {
  1759. var doc = root ? root.ownerDocument || root : document,
  1760. div = doc.createElement('div'), list = [];
  1761. div.innerHTML = '<img id="__kindeditor_temp_tag__" width="0" height="0" style="display:none;" />' + expr;
  1762. for (var i = 0, len = div.childNodes.length; i < len; i++) {
  1763. var child = div.childNodes[i];
  1764. if (child.id == '__kindeditor_temp_tag__') {
  1765. continue;
  1766. }
  1767. list.push(child);
  1768. }
  1769. return newNode(list);
  1770. }
  1771. return newNode(_queryAll(expr, root));
  1772. }
  1773. if (expr && expr.constructor === KNode) {
  1774. return expr;
  1775. }
  1776. if (expr.toArray) {
  1777. expr = expr.toArray();
  1778. }
  1779. if (_isArray(expr)) {
  1780. return newNode(expr);
  1781. }
  1782. return newNode(_toArray(arguments));
  1783. };
  1784. _each(_K, function(key, val) {
  1785. K[key] = val;
  1786. });
  1787. K.NodeClass = KNode;
  1788. window.KindEditor = K;
  1789. var _START_TO_START = 0,
  1790. _START_TO_END = 1,
  1791. _END_TO_END = 2,
  1792. _END_TO_START = 3,
  1793. _BOOKMARK_ID = 0;
  1794. function _updateCollapsed(range) {
  1795. range.collapsed = (range.startContainer === range.endContainer && range.startOffset === range.endOffset);
  1796. return range;
  1797. }
  1798. function _copyAndDelete(range, isCopy, isDelete) {
  1799. var doc = range.doc, nodeList = [];
  1800. function splitTextNode(node, startOffset, endOffset) {
  1801. var length = node.nodeValue.length, centerNode;
  1802. if (isCopy) {
  1803. var cloneNode = node.cloneNode(true);
  1804. if (startOffset > 0) {
  1805. centerNode = cloneNode.splitText(startOffset);
  1806. } else {
  1807. centerNode = cloneNode;
  1808. }
  1809. if (endOffset < length) {
  1810. centerNode.splitText(endOffset - startOffset);
  1811. }
  1812. }
  1813. if (isDelete) {
  1814. var center = node;
  1815. if (startOffset > 0) {
  1816. center = node.splitText(startOffset);
  1817. range.setStart(node, startOffset);
  1818. }
  1819. if (endOffset < length) {
  1820. var right = center.splitText(endOffset - startOffset);
  1821. range.setEnd(right, 0);
  1822. }
  1823. nodeList.push(center);
  1824. }
  1825. return centerNode;
  1826. }
  1827. function removeNodes() {
  1828. if (isDelete) {
  1829. range.up().collapse(true);
  1830. }
  1831. for (var i = 0, len = nodeList.length; i < len; i++) {
  1832. var node = nodeList[i];
  1833. if (node.parentNode) {
  1834. node.parentNode.removeChild(node);
  1835. }
  1836. }
  1837. }
  1838. var copyRange = range.cloneRange().down();
  1839. var start = -1, incStart = -1, incEnd = -1, end = -1,
  1840. ancestor = range.commonAncestor(), frag = doc.createDocumentFragment();
  1841. if (ancestor.nodeType == 3) {
  1842. var textNode = splitTextNode(ancestor, range.startOffset, range.endOffset);
  1843. if (isCopy) {
  1844. frag.appendChild(textNode);
  1845. }
  1846. removeNodes();
  1847. return isCopy ? frag : range;
  1848. }
  1849. function extractNodes(parent, frag) {
  1850. var node = parent.firstChild, nextNode;
  1851. while (node) {
  1852. var testRange = new KRange(doc).selectNode(node);
  1853. start = testRange.compareBoundaryPoints(_START_TO_END, range);
  1854. if (start >= 0 && incStart <= 0) {
  1855. incStart = testRange.compareBoundaryPoints(_START_TO_START, range);
  1856. }
  1857. if (incStart >= 0 && incEnd <= 0) {
  1858. incEnd = testRange.compareBoundaryPoints(_END_TO_END, range);
  1859. }
  1860. if (incEnd >= 0 && end <= 0) {
  1861. end = testRange.compareBoundaryPoints(_END_TO_START, range);
  1862. }
  1863. if (end >= 0) {
  1864. return false;
  1865. }
  1866. nextNode = node.nextSibling;
  1867. if (start > 0) {
  1868. if (node.nodeType == 1) {
  1869. if (incStart >= 0 && incEnd <= 0) {
  1870. if (isCopy) {
  1871. frag.appendChild(node.cloneNode(true));
  1872. }
  1873. if (isDelete) {
  1874. nodeList.push(node);
  1875. }
  1876. } else {
  1877. var childFlag;
  1878. if (isCopy) {
  1879. childFlag = node.cloneNode(false);
  1880. frag.appendChild(childFlag);
  1881. }
  1882. if (extractNodes(node, childFlag) === false) {
  1883. return false;
  1884. }
  1885. }
  1886. } else if (node.nodeType == 3) {
  1887. var textNode;
  1888. if (node == copyRange.startContainer) {
  1889. textNode = splitTextNode(node, copyRange.startOffset, node.nodeValue.length);
  1890. } else if (node == copyRange.endContainer) {
  1891. textNode = splitTextNode(node, 0, copyRange.endOffset);
  1892. } else {
  1893. textNode = splitTextNode(node, 0, node.nodeValue.length);
  1894. }
  1895. if (isCopy) {
  1896. try {
  1897. frag.appendChild(textNode);
  1898. } catch(e) {}
  1899. }
  1900. }
  1901. }
  1902. node = nextNode;
  1903. }
  1904. }
  1905. extractNodes(ancestor, frag);
  1906. if (isDelete) {
  1907. range.up().collapse(true);
  1908. }
  1909. for (var i = 0, len = nodeList.length; i < len; i++) {
  1910. var node = nodeList[i];
  1911. if (node.parentNode) {
  1912. node.parentNode.removeChild(node);
  1913. }
  1914. }
  1915. return isCopy ? frag : range;
  1916. }
  1917. function _moveToElementText(range, el) {
  1918. var node = el;
  1919. while (node) {
  1920. var knode = K(node);
  1921. if (knode.name == 'marquee' || knode.name == 'select') {
  1922. return;
  1923. }
  1924. node = node.parentNode;
  1925. }
  1926. try {
  1927. range.moveToElementText(el);
  1928. } catch(e) {}
  1929. }
  1930. function _getStartEnd(rng, isStart) {
  1931. var doc = rng.parentElement().ownerDocument,
  1932. pointRange = rng.duplicate();
  1933. pointRange.collapse(isStart);
  1934. var parent = pointRange.parentElement(),
  1935. nodes = parent.childNodes;
  1936. if (nodes.length === 0) {
  1937. return {node: parent.parentNode, offset: K(parent).index()};
  1938. }
  1939. var startNode = doc, startPos = 0, cmp = -1;
  1940. var testRange = rng.duplicate();
  1941. _moveToElementText(testRange, parent);
  1942. for (var i = 0, len = nodes.length; i < len; i++) {
  1943. var node = nodes[i];
  1944. cmp = testRange.compareEndPoints('StartToStart', pointRange);
  1945. if (cmp === 0) {
  1946. return {node: node.parentNode, offset: i};
  1947. }
  1948. if (node.nodeType == 1) {
  1949. var nodeRange = rng.duplicate(), dummy, knode = K(node), newNode = node;
  1950. if (knode.isControl()) {
  1951. dummy = doc.createElement('span');
  1952. knode.after(dummy);
  1953. newNode = dummy;
  1954. startPos += knode.text().replace(/\r\n|\n|\r/g, '').length;
  1955. }
  1956. _moveToElementText(nodeRange, newNode);
  1957. testRange.setEndPoint('StartToEnd', nodeRange);
  1958. if (cmp > 0) {
  1959. startPos += nodeRange.text.replace(/\r\n|\n|\r/g, '').length;
  1960. } else {
  1961. startPos = 0;
  1962. }
  1963. if (dummy) {
  1964. K(dummy).remove();
  1965. }
  1966. } else if (node.nodeType == 3) {
  1967. testRange.moveStart('character', node.nodeValue.length);
  1968. startPos += node.nodeValue.length;
  1969. }
  1970. if (cmp < 0) {
  1971. startNode = node;
  1972. }
  1973. }
  1974. if (cmp < 0 && startNode.nodeType == 1) {
  1975. return {node: parent, offset: K(parent.lastChild).index() + 1};
  1976. }
  1977. if (cmp > 0) {
  1978. while (startNode.nextSibling && startNode.nodeType == 1) {
  1979. startNode = startNode.nextSibling;
  1980. }
  1981. }
  1982. testRange = rng.duplicate();
  1983. _moveToElementText(testRange, parent);
  1984. testRange.setEndPoint('StartToEnd', pointRange);
  1985. startPos -= testRange.text.replace(/\r\n|\n|\r/g, '').length;
  1986. if (cmp > 0 && startNode.nodeType == 3) {
  1987. var prevNode = startNode.previousSibling;
  1988. while (prevNode && prevNode.nodeType == 3) {
  1989. startPos -= prevNode.nodeValue.length;
  1990. prevNode = prevNode.previousSibling;
  1991. }
  1992. }
  1993. return {node: startNode, offset: startPos};
  1994. }
  1995. function _getEndRange(node, offset) {
  1996. var doc = node.ownerDocument || node,
  1997. range = doc.body.createTextRange();
  1998. if (doc == node) {
  1999. range.collapse(true);
  2000. return range;
  2001. }
  2002. if (node.nodeType == 1 && node.childNodes.length > 0) {
  2003. var children = node.childNodes, isStart, child;
  2004. if (offset === 0) {
  2005. child = children[0];
  2006. isStart = true;
  2007. } else {
  2008. child = children[offset - 1];
  2009. isStart = false;
  2010. }
  2011. if (!child) {
  2012. return range;
  2013. }
  2014. if (K(child).name === 'head') {
  2015. if (offset === 1) {
  2016. isStart = true;
  2017. }
  2018. if (offset === 2) {
  2019. isStart = false;
  2020. }
  2021. range.collapse(isStart);
  2022. return range;
  2023. }
  2024. if (child.nodeType == 1) {
  2025. var kchild = K(child), span;
  2026. if (kchild.isControl()) {
  2027. span = doc.createElement('span');
  2028. if (isStart) {
  2029. kchild.before(span);
  2030. } else {
  2031. kchild.after(span);
  2032. }
  2033. child = span;
  2034. }
  2035. _moveToElementText(range, child);
  2036. range.collapse(isStart);
  2037. if (span) {
  2038. K(span).remove();
  2039. }
  2040. return range;
  2041. }
  2042. node = child;
  2043. offset = isStart ? 0 : child.nodeValue.length;
  2044. }
  2045. var dummy = doc.createElement('span');
  2046. K(node).before(dummy);
  2047. _moveToElementText(range, dummy);
  2048. range.moveStart('character', offset);
  2049. K(dummy).remove();
  2050. return range;
  2051. }
  2052. function _toRange(rng) {
  2053. var doc, range;
  2054. function tr2td(start) {
  2055. if (K(start.node).name == 'tr') {
  2056. start.node = start.node.cells[start.offset];
  2057. start.offset = 0;
  2058. }
  2059. }
  2060. if (_IERANGE) {
  2061. if (rng.item) {
  2062. doc = _getDoc(rng.item(0));
  2063. range = new KRange(doc);
  2064. range.selectNode(rng.item(0));
  2065. return range;
  2066. }
  2067. doc = rng.parentElement().ownerDocument;
  2068. var start = _getStartEnd(rng, true),
  2069. end = _getStartEnd(rng, false);
  2070. tr2td(start);
  2071. tr2td(end);
  2072. range = new KRange(doc);
  2073. range.setStart(start.node, start.offset);
  2074. range.setEnd(end.node, end.offset);
  2075. return range;
  2076. }
  2077. var startContainer = rng.startContainer;
  2078. doc = startContainer.ownerDocument || startContainer;
  2079. range = new KRange(doc);
  2080. range.setStart(startContainer, rng.startOffset);
  2081. range.setEnd(rng.endContainer, rng.endOffset);
  2082. return range;
  2083. }
  2084. function KRange(doc) {
  2085. this.init(doc);
  2086. }
  2087. _extend(KRange, {
  2088. init : function(doc) {
  2089. var self = this;
  2090. self.startContainer = doc;
  2091. self.startOffset = 0;
  2092. self.endContainer = doc;
  2093. self.endOffset = 0;
  2094. self.collapsed = true;
  2095. self.doc = doc;
  2096. },
  2097. commonAncestor : function() {
  2098. function getParents(node) {
  2099. var parents = [];
  2100. while (node) {
  2101. parents.push(node);
  2102. node = node.parentNode;
  2103. }
  2104. return parents;
  2105. }
  2106. var parentsA = getParents(this.startContainer),
  2107. parentsB = getParents(this.endContainer),
  2108. i = 0, lenA = parentsA.length, lenB = parentsB.length, parentA, parentB;
  2109. while (++i) {
  2110. parentA = parentsA[lenA - i];
  2111. parentB = parentsB[lenB - i];
  2112. if (!parentA || !parentB || parentA !== parentB) {
  2113. break;
  2114. }
  2115. }
  2116. return parentsA[lenA - i + 1];
  2117. },
  2118. setStart : function(node, offset) {
  2119. var self = this, doc = self.doc;
  2120. self.startContainer = node;
  2121. self.startOffset = offset;
  2122. if (self.endContainer === doc) {
  2123. self.endContainer = node;
  2124. self.endOffset = offset;
  2125. }
  2126. return _updateCollapsed(this);
  2127. },
  2128. setEnd : function(node, offset) {
  2129. var self = this, doc = self.doc;
  2130. self.endContainer = node;
  2131. self.endOffset = offset;
  2132. if (self.startContainer === doc) {
  2133. self.startContainer = node;
  2134. self.startOffset = offset;
  2135. }
  2136. return _updateCollapsed(this);
  2137. },
  2138. setStartBefore : function(node) {
  2139. return this.setStart(node.parentNode || this.doc, K(node).index());
  2140. },
  2141. setStartAfter : function(node) {
  2142. return this.setStart(node.parentNode || this.doc, K(node).index() + 1);
  2143. },
  2144. setEndBefore : function(node) {
  2145. return this.setEnd(node.parentNode || this.doc, K(node).index());
  2146. },
  2147. setEndAfter : function(node) {
  2148. return this.setEnd(node.parentNode || this.doc, K(node).index() + 1);
  2149. },
  2150. selectNode : function(node) {
  2151. return this.setStartBefore(node).setEndAfter(node);
  2152. },
  2153. selectNodeContents : function(node) {
  2154. var knode = K(node);
  2155. if (knode.type == 3 || knode.isSingle()) {
  2156. return this.selectNode(node);
  2157. }
  2158. var children = knode.children();
  2159. if (children.length > 0) {
  2160. return this.setStartBefore(children[0]).setEndAfter(children[children.length - 1]);
  2161. }
  2162. return this.setStart(node, 0).setEnd(node, 0);
  2163. },
  2164. collapse : function(toStart) {
  2165. if (toStart) {
  2166. return this.setEnd(this.startContainer, this.startOffset);
  2167. }
  2168. return this.setStart(this.endContainer, this.endOffset);
  2169. },
  2170. compareBoundaryPoints : function(how, range) {
  2171. var rangeA = this.get(), rangeB = range.get();
  2172. if (_IERANGE) {
  2173. var arr = {};
  2174. arr[_START_TO_START] = 'StartToStart';
  2175. arr[_START_TO_END] = 'EndToStart';
  2176. arr[_END_TO_END] = 'EndToEnd';
  2177. arr[_END_TO_START] = 'StartToEnd';
  2178. var cmp = rangeA.compareEndPoints(arr[how], rangeB);
  2179. if (cmp !== 0) {
  2180. return cmp;
  2181. }
  2182. var nodeA, nodeB, nodeC, posA, posB;
  2183. if (how === _START_TO_START || how === _END_TO_START) {
  2184. nodeA = this.startContainer;
  2185. posA = this.startOffset;
  2186. }
  2187. if (how === _START_TO_END || how === _END_TO_END) {
  2188. nodeA = this.endContainer;
  2189. posA = this.endOffset;
  2190. }
  2191. if (how === _START_TO_START || how === _START_TO_END) {
  2192. nodeB = range.startContainer;
  2193. posB = range.startOffset;
  2194. }
  2195. if (how === _END_TO_END || how === _END_TO_START) {
  2196. nodeB = range.endContainer;
  2197. posB = range.endOffset;
  2198. }
  2199. if (nodeA === nodeB) {
  2200. var diff = posA - posB;
  2201. return diff > 0 ? 1 : (diff < 0 ? -1 : 0);
  2202. }
  2203. nodeC = nodeB;
  2204. while (nodeC && nodeC.parentNode !== nodeA) {
  2205. nodeC = nodeC.parentNode;
  2206. }
  2207. if (nodeC) {
  2208. return K(nodeC).index() >= posA ? -1 : 1;
  2209. }
  2210. nodeC = nodeA;
  2211. while (nodeC && nodeC.parentNode !== nodeB) {
  2212. nodeC = nodeC.parentNode;
  2213. }
  2214. if (nodeC) {
  2215. return K(nodeC).index() >= posB ? 1 : -1;
  2216. }
  2217. nodeC = K(nodeB).next();
  2218. if (nodeC && nodeC.contains(nodeA)) {
  2219. return 1;
  2220. }
  2221. nodeC = K(nodeA).next();
  2222. if (nodeC && nodeC.contains(nodeB)) {
  2223. return -1;
  2224. }
  2225. } else {
  2226. return rangeA.compareBoundaryPoints(how, rangeB);
  2227. }
  2228. },
  2229. cloneRange : function() {
  2230. return new KRange(this.doc).setStart(this.startContainer, this.startOffset).setEnd(this.endContainer, this.endOffset);
  2231. },
  2232. toString : function() {
  2233. var rng = this.get(), str = _IERANGE ? rng.text : rng.toString();
  2234. return str.replace(/\r\n|\n|\r/g, '');
  2235. },
  2236. cloneContents : function() {
  2237. return _copyAndDelete(this, true, false);
  2238. },
  2239. deleteContents : function() {
  2240. return _copyAndDelete(this, false, true);
  2241. },
  2242. extractContents : function() {
  2243. return _copyAndDelete(this, true, true);
  2244. },
  2245. insertNode : function(node) {
  2246. var self = this,
  2247. sc = self.startContainer, so = self.startOffset,
  2248. ec = self.endContainer, eo = self.endOffset,
  2249. firstChild, lastChild, c, nodeCount = 1;
  2250. if (node.nodeName.toLowerCase() === '#document-fragment') {
  2251. firstChild = node.firstChild;
  2252. lastChild = node.lastChild;
  2253. nodeCount = node.childNodes.length;
  2254. }
  2255. if (sc.nodeType == 1) {
  2256. c = sc.childNodes[so];
  2257. if (c) {
  2258. sc.insertBefore(node, c);
  2259. if (sc === ec) {
  2260. eo += nodeCount;
  2261. }
  2262. } else {
  2263. sc.appendChild(node);
  2264. }
  2265. } else if (sc.nodeType == 3) {
  2266. if (so === 0) {
  2267. sc.parentNode.insertBefore(node, sc);
  2268. if (sc.parentNode === ec) {
  2269. eo += nodeCount;
  2270. }
  2271. } else if (so >= sc.nodeValue.length) {
  2272. if (sc.nextSibling) {
  2273. sc.parentNode.insertBefore(node, sc.nextSibling);
  2274. } else {
  2275. sc.parentNode.appendChild(node);
  2276. }
  2277. } else {
  2278. if (so > 0) {
  2279. c = sc.splitText(so);
  2280. } else {
  2281. c = sc;
  2282. }
  2283. sc.parentNode.insertBefore(node, c);
  2284. if (sc === ec) {
  2285. ec = c;
  2286. eo -= so;
  2287. }
  2288. }
  2289. }
  2290. if (firstChild) {
  2291. self.setStartBefore(firstChild).setEndAfter(lastChild);
  2292. } else {
  2293. self.selectNode(node);
  2294. }
  2295. if (self.compareBoundaryPoints(_END_TO_END, self.cloneRange().setEnd(ec, eo)) >= 1) {
  2296. return self;
  2297. }
  2298. return self.setEnd(ec, eo);
  2299. },
  2300. surroundContents : function(node) {
  2301. node.appendChild(this.extractContents());
  2302. return this.insertNode(node).selectNode(node);
  2303. },
  2304. isControl : function() {
  2305. var self = this,
  2306. sc = self.startContainer, so = self.startOffset,
  2307. ec = self.endContainer, eo = self.endOffset, rng;
  2308. return sc.nodeType == 1 && sc === ec && so + 1 === eo && K(sc.childNodes[so]).isControl();
  2309. },
  2310. get : function(hasControlRange) {
  2311. var self = this, doc = self.doc, node, rng;
  2312. if (!_IERANGE) {
  2313. rng = doc.createRange();
  2314. try {
  2315. rng.setStart(self.startContainer, self.startOffset);
  2316. rng.setEnd(self.endContainer, self.endOffset);
  2317. } catch (e) {}
  2318. return rng;
  2319. }
  2320. if (hasControlRange && self.isControl()) {
  2321. rng = doc.body.createControlRange();
  2322. rng.addElement(self.startContainer.childNodes[self.startOffset]);
  2323. return rng;
  2324. }
  2325. var range = self.cloneRange().down();
  2326. rng = doc.body.createTextRange();
  2327. rng.setEndPoint('StartToStart', _getEndRange(range.startContainer, range.startOffset));
  2328. rng.setEndPoint('EndToStart', _getEndRange(range.endContainer, range.endOffset));
  2329. return rng;
  2330. },
  2331. html : function() {
  2332. return K(this.cloneContents()).outer();
  2333. },
  2334. down : function() {
  2335. var self = this;
  2336. function downPos(node, pos, isStart) {
  2337. if (node.nodeType != 1) {
  2338. return;
  2339. }
  2340. var children = K(node).children();
  2341. if (children.length === 0) {
  2342. return;
  2343. }
  2344. var left, right, child, offset;
  2345. if (pos > 0) {
  2346. left = children.eq(pos - 1);
  2347. }
  2348. if (pos < children.length) {
  2349. right = children.eq(pos);
  2350. }
  2351. if (left && left.type == 3) {
  2352. child = left[0];
  2353. offset = child.nodeValue.length;
  2354. }
  2355. if (right && right.type == 3) {
  2356. child = right[0];
  2357. offset = 0;
  2358. }
  2359. if (!child) {
  2360. return;
  2361. }
  2362. if (isStart) {
  2363. self.setStart(child, offset);
  2364. } else {
  2365. self.setEnd(child, offset);
  2366. }
  2367. }
  2368. downPos(self.startContainer, self.startOffset, true);
  2369. downPos(self.endContainer, self.endOffset, false);
  2370. return self;
  2371. },
  2372. up : function() {
  2373. var self = this;
  2374. function upPos(node, pos, isStart) {
  2375. if (node.nodeType != 3) {
  2376. return;
  2377. }
  2378. if (pos === 0) {
  2379. if (isStart) {
  2380. self.setStartBefore(node);
  2381. } else {
  2382. self.setEndBefore(node);
  2383. }
  2384. } else if (pos == node.nodeValue.length) {
  2385. if (isStart) {
  2386. self.setStartAfter(node);
  2387. } else {
  2388. self.setEndAfter(node);
  2389. }
  2390. }
  2391. }
  2392. upPos(self.startContainer, self.startOffset, true);
  2393. upPos(self.endContainer, self.endOffset, false);
  2394. return self;
  2395. },
  2396. enlarge : function(toBlock) {
  2397. var self = this;
  2398. self.up();
  2399. function enlargePos(node, pos, isStart) {
  2400. var knode = K(node), parent;
  2401. if (knode.type == 3 || _NOSPLIT_TAG_MAP[knode.name] || !toBlock && knode.isBlock()) {
  2402. return;
  2403. }
  2404. if (pos === 0) {
  2405. while (!knode.prev()) {
  2406. parent = knode.parent();
  2407. if (!parent || _NOSPLIT_TAG_MAP[parent.name] || !toBlock && parent.isBlock()) {
  2408. break;
  2409. }
  2410. knode = parent;
  2411. }
  2412. if (isStart) {
  2413. self.setStartBefore(knode[0]);
  2414. } else {
  2415. self.setEndBefore(knode[0]);
  2416. }
  2417. } else if (pos == knode.children().length) {
  2418. while (!knode.next()) {
  2419. parent = knode.parent();
  2420. if (!parent || _NOSPLIT_TAG_MAP[parent.name] || !toBlock && parent.isBlock()) {
  2421. break;
  2422. }
  2423. knode = parent;
  2424. }
  2425. if (isStart) {
  2426. self.setStartAfter(knode[0]);
  2427. } else {
  2428. self.setEndAfter(knode[0]);
  2429. }
  2430. }
  2431. }
  2432. enlargePos(self.startContainer, self.startOffset, true);
  2433. enlargePos(self.endContainer, self.endOffset, false);
  2434. return self;
  2435. },
  2436. shrink : function() {
  2437. var self = this, child, collapsed = self.collapsed;
  2438. while (self.startContainer.nodeType == 1 && (child = self.startContainer.childNodes[self.startOffset]) && child.nodeType == 1 && !K(child).isSingle()) {
  2439. self.setStart(child, 0);
  2440. }
  2441. if (collapsed) {
  2442. return self.collapse(collapsed);
  2443. }
  2444. while (self.endContainer.nodeType == 1 && self.endOffset > 0 && (child = self.endContainer.childNodes[self.endOffset - 1]) && child.nodeType == 1 && !K(child).isSingle()) {
  2445. self.setEnd(child, child.childNodes.length);
  2446. }
  2447. return self;
  2448. },
  2449. createBookmark : function(serialize) {
  2450. var self = this, doc = self.doc, endNode,
  2451. startNode = K('<span style="display:none;"></span>', doc)[0];
  2452. startNode.id = '__kindeditor_bookmark_start_' + (_BOOKMARK_ID++) + '__';
  2453. if (!self.collapsed) {
  2454. endNode = startNode.cloneNode(true);
  2455. endNode.id = '__kindeditor_bookmark_end_' + (_BOOKMARK_ID++) + '__';
  2456. }
  2457. if (endNode) {
  2458. self.cloneRange().collapse(false).insertNode(endNode).setEndBefore(endNode);
  2459. }
  2460. self.insertNode(startNode).setStartAfter(startNode);
  2461. return {
  2462. start : serialize ? '#' + startNode.id : startNode,
  2463. end : endNode ? (serialize ? '#' + endNode.id : endNode) : null
  2464. };
  2465. },
  2466. moveToBookmark : function(bookmark) {
  2467. var self = this, doc = self.doc,
  2468. start = K(bookmark.start, doc), end = bookmark.end ? K(bookmark.end, doc) : null;
  2469. if (!start || start.length < 1) {
  2470. return self;
  2471. }
  2472. self.setStartBefore(start[0]);
  2473. start.remove();
  2474. if (end && end.length > 0) {
  2475. self.setEndBefore(end[0]);
  2476. end.remove();
  2477. } else {
  2478. self.collapse(true);
  2479. }
  2480. return self;
  2481. },
  2482. dump : function() {
  2483. console.log('--------------------');
  2484. console.log(this.startContainer.nodeType == 3 ? this.startContainer.nodeValue : this.startContainer, this.startOffset);
  2485. console.log(this.endContainer.nodeType == 3 ? this.endContainer.nodeValue : this.endContainer, this.endOffset);
  2486. }
  2487. });
  2488. function _range(mixed) {
  2489. if (!mixed.nodeName) {
  2490. return mixed.constructor === KRange ? mixed : _toRange(mixed);
  2491. }
  2492. return new KRange(mixed);
  2493. }
  2494. K.RangeClass = KRange;
  2495. K.range = _range;
  2496. K.START_TO_START = _START_TO_START;
  2497. K.START_TO_END = _START_TO_END;
  2498. K.END_TO_END = _END_TO_END;
  2499. K.END_TO_START = _END_TO_START;
  2500. function _nativeCommand(doc, key, val) {
  2501. try {
  2502. doc.execCommand(key, false, val);
  2503. } catch(e) {}
  2504. }
  2505. function _nativeCommandValue(doc, key) {
  2506. var val = '';
  2507. try {
  2508. val = doc.queryCommandValue(key);
  2509. } catch (e) {}
  2510. if (typeof val !== 'string') {
  2511. val = '';
  2512. }
  2513. return val;
  2514. }
  2515. function _getSel(doc) {
  2516. var win = _getWin(doc);
  2517. return _IERANGE ? doc.selection : win.getSelection();
  2518. }
  2519. function _getRng(doc) {
  2520. var sel = _getSel(doc), rng;
  2521. try {
  2522. if (sel.rangeCount > 0) {
  2523. rng = sel.getRangeAt(0);
  2524. } else {
  2525. rng = sel.createRange();
  2526. }
  2527. } catch(e) {}
  2528. if (_IERANGE && (!rng || (!rng.item && rng.parentElement().ownerDocument !== doc))) {
  2529. return null;
  2530. }
  2531. return rng;
  2532. }
  2533. function _singleKeyMap(map) {
  2534. var newMap = {}, arr, v;
  2535. _each(map, function(key, val) {
  2536. arr = key.split(',');
  2537. for (var i = 0, len = arr.length; i < len; i++) {
  2538. v = arr[i];
  2539. newMap[v] = val;
  2540. }
  2541. });
  2542. return newMap;
  2543. }
  2544. function _hasAttrOrCss(knode, map) {
  2545. return _hasAttrOrCssByKey(knode, map, '*') || _hasAttrOrCssByKey(knode, map);
  2546. }
  2547. function _hasAttrOrCssByKey(knode, map, mapKey) {
  2548. mapKey = mapKey || knode.name;
  2549. if (knode.type !== 1) {
  2550. return false;
  2551. }
  2552. var newMap = _singleKeyMap(map);
  2553. if (!newMap[mapKey]) {
  2554. return false;
  2555. }
  2556. var arr = newMap[mapKey].split(',');
  2557. for (var i = 0, len = arr.length; i < len; i++) {
  2558. var key = arr[i];
  2559. if (key === '*') {
  2560. return true;
  2561. }
  2562. var match = /^(\.?)([^=]+)(?:=([^=]*))?$/.exec(key);
  2563. var method = match[1] ? 'css' : 'attr';
  2564. key = match[2];
  2565. var val = match[3] || '';
  2566. if (val === '' && knode[method](key) !== '') {
  2567. return true;
  2568. }
  2569. if (val !== '' && knode[method](key) === val) {
  2570. return true;
  2571. }
  2572. }
  2573. return false;
  2574. }
  2575. function _removeAttrOrCss(knode, map) {
  2576. if (knode.type != 1) {
  2577. return;
  2578. }
  2579. _removeAttrOrCssByKey(knode, map, '*');
  2580. _removeAttrOrCssByKey(knode, map);
  2581. }
  2582. function _removeAttrOrCssByKey(knode, map, mapKey) {
  2583. mapKey = mapKey || knode.name;
  2584. if (knode.type !== 1) {
  2585. return;
  2586. }
  2587. var newMap = _singleKeyMap(map);
  2588. if (!newMap[mapKey]) {
  2589. return;
  2590. }
  2591. var arr = newMap[mapKey].split(','), allFlag = false;
  2592. for (var i = 0, len = arr.length; i < len; i++) {
  2593. var key = arr[i];
  2594. if (key === '*') {
  2595. allFlag = true;
  2596. break;
  2597. }
  2598. var match = /^(\.?)([^=]+)(?:=([^=]*))?$/.exec(key);
  2599. key = match[2];
  2600. if (match[1]) {
  2601. key = _toCamel(key);
  2602. if (knode[0].style[key]) {
  2603. knode[0].style[key] = '';
  2604. }
  2605. } else {
  2606. knode.removeAttr(key);
  2607. }
  2608. }
  2609. if (allFlag) {
  2610. knode.remove(true);
  2611. }
  2612. }
  2613. function _getInnerNode(knode) {
  2614. var inner = knode;
  2615. while (inner.first()) {
  2616. inner = inner.first();
  2617. }
  2618. return inner;
  2619. }
  2620. function _isEmptyNode(knode) {
  2621. if (knode.type != 1 || knode.isSingle()) {
  2622. return false;
  2623. }
  2624. return knode.html().replace(/<[^>]+>/g, '') === '';
  2625. }
  2626. function _mergeWrapper(a, b) {
  2627. a = a.clone(true);
  2628. var lastA = _getInnerNode(a), childA = a, merged = false;
  2629. while (b) {
  2630. while (childA) {
  2631. if (childA.name === b.name) {
  2632. _mergeAttrs(childA, b.attr(), b.css());
  2633. merged = true;
  2634. }
  2635. childA = childA.first();
  2636. }
  2637. if (!merged) {
  2638. lastA.append(b.clone(false));
  2639. }
  2640. merged = false;
  2641. b = b.first();
  2642. }
  2643. return a;
  2644. }
  2645. function _wrapNode(knode, wrapper) {
  2646. wrapper = wrapper.clone(true);
  2647. if (knode.type == 3) {
  2648. _getInnerNode(wrapper).append(knode.clone(false));
  2649. knode.replaceWith(wrapper);
  2650. return wrapper;
  2651. }
  2652. var nodeWrapper = knode, child;
  2653. while ((child = knode.first()) && child.children().length == 1) {
  2654. knode = child;
  2655. }
  2656. child = knode.first();
  2657. var frag = knode.doc.createDocumentFragment();
  2658. while (child) {
  2659. frag.appendChild(child[0]);
  2660. child = child.next();
  2661. }
  2662. wrapper = _mergeWrapper(nodeWrapper, wrapper);
  2663. if (frag.firstChild) {
  2664. _getInnerNode(wrapper).append(frag);
  2665. }
  2666. nodeWrapper.replaceWith(wrapper);
  2667. return wrapper;
  2668. }
  2669. function _mergeAttrs(knode, attrs, styles) {
  2670. _each(attrs, function(key, val) {
  2671. if (key !== 'style') {
  2672. knode.attr(key, val);
  2673. }
  2674. });
  2675. _each(styles, function(key, val) {
  2676. knode.css(key, val);
  2677. });
  2678. }
  2679. function _inPreElement(knode) {
  2680. while (knode && knode.name != 'body') {
  2681. if (_PRE_TAG_MAP[knode.name] || knode.name == 'div' && knode.hasClass('ke-script')) {
  2682. return true;
  2683. }
  2684. knode = knode.parent();
  2685. }
  2686. return false;
  2687. }
  2688. function KCmd(range) {
  2689. this.init(range);
  2690. }
  2691. _extend(KCmd, {
  2692. init : function(range) {
  2693. var self = this, doc = range.doc;
  2694. self.doc = doc;
  2695. self.win = _getWin(doc);
  2696. self.sel = _getSel(doc);
  2697. self.range = range;
  2698. },
  2699. selection : function(forceReset) {
  2700. var self = this, doc = self.doc, rng = _getRng(doc);
  2701. self.sel = _getSel(doc);
  2702. if (rng) {
  2703. self.range = _range(rng);
  2704. if (K(self.range.startContainer).name == 'html') {
  2705. self.range.selectNodeContents(doc.body).collapse(false);
  2706. }
  2707. return self;
  2708. }
  2709. if (forceReset) {
  2710. self.range.selectNodeContents(doc.body).collapse(false);
  2711. }
  2712. return self;
  2713. },
  2714. select : function(hasDummy) {
  2715. hasDummy = _undef(hasDummy, true);
  2716. var self = this, sel = self.sel, range = self.range.cloneRange().shrink(),
  2717. sc = range.startContainer, so = range.startOffset,
  2718. ec = range.endContainer, eo = range.endOffset,
  2719. doc = _getDoc(sc), win = self.win, rng, hasU200b = false;
  2720. if (hasDummy && sc.nodeType == 1 && range.collapsed) {
  2721. if (_IERANGE) {
  2722. var dummy = K('<span>&nbsp;</span>', doc);
  2723. range.insertNode(dummy[0]);
  2724. rng = doc.body.createTextRange();
  2725. try {
  2726. rng.moveToElementText(dummy[0]);
  2727. } catch(ex) {}
  2728. rng.collapse(false);
  2729. rng.select();
  2730. dummy.remove();
  2731. win.focus();
  2732. return self;
  2733. }
  2734. if (_WEBKIT) {
  2735. var children = sc.childNodes;
  2736. if (K(sc).isInline() || so > 0 && K(children[so - 1]).isInline() || children[so] && K(children[so]).isInline()) {
  2737. range.insertNode(doc.createTextNode('\u200B'));
  2738. hasU200b = true;
  2739. }
  2740. }
  2741. }
  2742. if (_IERANGE) {
  2743. try {
  2744. rng = range.get(true);
  2745. rng.select();
  2746. } catch(e) {}
  2747. } else {
  2748. if (hasU200b) {
  2749. range.collapse(false);
  2750. }
  2751. rng = range.get(true);
  2752. sel.removeAllRanges();
  2753. sel.addRange(rng);
  2754. if (doc !== document) {
  2755. var pos = K(rng.endContainer).pos();
  2756. win.scrollTo(pos.x, pos.y);
  2757. }
  2758. }
  2759. win.focus();
  2760. return self;
  2761. },
  2762. wrap : function(val) {
  2763. var self = this, doc = self.doc, range = self.range, wrapper;
  2764. wrapper = K(val, doc);
  2765. if (range.collapsed) {
  2766. range.shrink();
  2767. range.insertNode(wrapper[0]).selectNodeContents(wrapper[0]);
  2768. return self;
  2769. }
  2770. if (wrapper.isBlock()) {
  2771. var copyWrapper = wrapper.clone(true), child = copyWrapper;
  2772. while (child.first()) {
  2773. child = child.first();
  2774. }
  2775. child.append(range.extractContents());
  2776. range.insertNode(copyWrapper[0]).selectNode(copyWrapper[0]);
  2777. return self;
  2778. }
  2779. range.enlarge();
  2780. var bookmark = range.createBookmark(), ancestor = range.commonAncestor(), isStart = false;
  2781. K(ancestor).scan(function(node) {
  2782. if (!isStart && node == bookmark.start) {
  2783. isStart = true;
  2784. return;
  2785. }
  2786. if (isStart) {
  2787. if (node == bookmark.end) {
  2788. return false;
  2789. }
  2790. var knode = K(node);
  2791. if (_inPreElement(knode)) {
  2792. return;
  2793. }
  2794. if (knode.type == 3 && _trim(node.nodeValue).length > 0) {
  2795. var parent;
  2796. while ((parent = knode.parent()) && parent.isStyle() && parent.children().length == 1) {
  2797. knode = parent;
  2798. }
  2799. _wrapNode(knode, wrapper);
  2800. }
  2801. }
  2802. });
  2803. range.moveToBookmark(bookmark);
  2804. return self;
  2805. },
  2806. split : function(isStart, map) {
  2807. var range = this.range, doc = range.doc;
  2808. var tempRange = range.cloneRange().collapse(isStart);
  2809. var node = tempRange.startContainer, pos = tempRange.startOffset,
  2810. parent = node.nodeType == 3 ? node.parentNode : node,
  2811. needSplit = false, knode;
  2812. while (parent && parent.parentNode) {
  2813. knode = K(parent);
  2814. if (map) {
  2815. if (!knode.isStyle()) {
  2816. break;
  2817. }
  2818. if (!_hasAttrOrCss(knode, map)) {
  2819. break;
  2820. }
  2821. } else {
  2822. if (_NOSPLIT_TAG_MAP[knode.name]) {
  2823. break;
  2824. }
  2825. }
  2826. needSplit = true;
  2827. parent = parent.parentNode;
  2828. }
  2829. if (needSplit) {
  2830. var dummy = doc.createElement('span');
  2831. range.cloneRange().collapse(!isStart).insertNode(dummy);
  2832. if (isStart) {
  2833. tempRange.setStartBefore(parent.firstChild).setEnd(node, pos);
  2834. } else {
  2835. tempRange.setStart(node, pos).setEndAfter(parent.lastChild);
  2836. }
  2837. var frag = tempRange.extractContents(),
  2838. first = frag.firstChild, last = frag.lastChild;
  2839. if (isStart) {
  2840. tempRange.insertNode(frag);
  2841. range.setStartAfter(last).setEndBefore(dummy);
  2842. } else {
  2843. parent.appendChild(frag);
  2844. range.setStartBefore(dummy).setEndBefore(first);
  2845. }
  2846. var dummyParent = dummy.parentNode;
  2847. if (dummyParent == range.endContainer) {
  2848. var prev = K(dummy).prev(), next = K(dummy).next();
  2849. if (prev && next && prev.type == 3 && next.type == 3) {
  2850. range.setEnd(prev[0], prev[0].nodeValue.length);
  2851. } else if (!isStart) {
  2852. range.setEnd(range.endContainer, range.endOffset - 1);
  2853. }
  2854. }
  2855. dummyParent.removeChild(dummy);
  2856. }
  2857. return this;
  2858. },
  2859. remove : function(map) {
  2860. var self = this, doc = self.doc, range = self.range;
  2861. range.enlarge();
  2862. if (range.startOffset === 0) {
  2863. var ksc = K(range.startContainer), parent;
  2864. while ((parent = ksc.parent()) && parent.isStyle() && parent.children().length == 1) {
  2865. ksc = parent;
  2866. }
  2867. range.setStart(ksc[0], 0);
  2868. ksc = K(range.startContainer);
  2869. if (ksc.isBlock()) {
  2870. _removeAttrOrCss(ksc, map);
  2871. }
  2872. var kscp = ksc.parent();
  2873. if (kscp && kscp.isBlock()) {
  2874. _removeAttrOrCss(kscp, map);
  2875. }
  2876. }
  2877. var sc, so;
  2878. if (range.collapsed) {
  2879. self.split(true, map);
  2880. sc = range.startContainer;
  2881. so = range.startOffset;
  2882. if (so > 0) {
  2883. var sb = K(sc.childNodes[so - 1]);
  2884. if (sb && _isEmptyNode(sb)) {
  2885. sb.remove();
  2886. range.setStart(sc, so - 1);
  2887. }
  2888. }
  2889. var sa = K(sc.childNodes[so]);
  2890. if (sa && _isEmptyNode(sa)) {
  2891. sa.remove();
  2892. }
  2893. if (_isEmptyNode(sc)) {
  2894. range.startBefore(sc);
  2895. sc.remove();
  2896. }
  2897. range.collapse(true);
  2898. return self;
  2899. }
  2900. self.split(true, map);
  2901. self.split(false, map);
  2902. var startDummy = doc.createElement('span'), endDummy = doc.createElement('span');
  2903. range.cloneRange().collapse(false).insertNode(endDummy);
  2904. range.cloneRange().collapse(true).insertNode(startDummy);
  2905. var nodeList = [], cmpStart = false;
  2906. K(range.commonAncestor()).scan(function(node) {
  2907. if (!cmpStart && node == startDummy) {
  2908. cmpStart = true;
  2909. return;
  2910. }
  2911. if (node == endDummy) {
  2912. return false;
  2913. }
  2914. if (cmpStart) {
  2915. nodeList.push(node);
  2916. }
  2917. });
  2918. K(startDummy).remove();
  2919. K(endDummy).remove();
  2920. sc = range.startContainer;
  2921. so = range.startOffset;
  2922. var ec = range.endContainer, eo = range.endOffset;
  2923. if (so > 0) {
  2924. var startBefore = K(sc.childNodes[so - 1]);
  2925. if (startBefore && _isEmptyNode(startBefore)) {
  2926. startBefore.remove();
  2927. range.setStart(sc, so - 1);
  2928. if (sc == ec) {
  2929. range.setEnd(ec, eo - 1);
  2930. }
  2931. }
  2932. var startAfter = K(sc.childNodes[so]);
  2933. if (startAfter && _isEmptyNode(startAfter)) {
  2934. startAfter.remove();
  2935. if (sc == ec) {
  2936. range.setEnd(ec, eo - 1);
  2937. }
  2938. }
  2939. }
  2940. var endAfter = K(ec.childNodes[range.endOffset]);
  2941. if (endAfter && _isEmptyNode(endAfter)) {
  2942. endAfter.remove();
  2943. }
  2944. var bookmark = range.createBookmark(true);
  2945. _each(nodeList, function(i, node) {
  2946. _removeAttrOrCss(K(node), map);
  2947. });
  2948. range.moveToBookmark(bookmark);
  2949. return self;
  2950. },
  2951. commonNode : function(map) {
  2952. var range = this.range;
  2953. var ec = range.endContainer, eo = range.endOffset,
  2954. node = (ec.nodeType == 3 || eo === 0) ? ec : ec.childNodes[eo - 1];
  2955. function find(node) {
  2956. var child = node, parent = node;
  2957. while (parent) {
  2958. if (_hasAttrOrCss(K(parent), map)) {
  2959. return K(parent);
  2960. }
  2961. parent = parent.parentNode;
  2962. }
  2963. while (child && (child = child.lastChild)) {
  2964. if (_hasAttrOrCss(K(child), map)) {
  2965. return K(child);
  2966. }
  2967. }
  2968. return null;
  2969. }
  2970. var cNode = find(node);
  2971. if (cNode) {
  2972. return cNode;
  2973. }
  2974. if (node.nodeType == 1 || (ec.nodeType == 3 && eo === 0)) {
  2975. var prev = K(node).prev();
  2976. if (prev) {
  2977. return find(prev);
  2978. }
  2979. }
  2980. return null;
  2981. },
  2982. commonAncestor : function(tagName) {
  2983. var range = this.range,
  2984. sc = range.startContainer, so = range.startOffset,
  2985. ec = range.endContainer, eo = range.endOffset,
  2986. startNode = (sc.nodeType == 3 || so === 0) ? sc : sc.childNodes[so - 1],
  2987. endNode = (ec.nodeType == 3 || eo === 0) ? ec : ec.childNodes[eo - 1];
  2988. function find(node) {
  2989. while (node) {
  2990. if (node.nodeType == 1) {
  2991. if (node.tagName.toLowerCase() === tagName) {
  2992. return node;
  2993. }
  2994. }
  2995. node = node.parentNode;
  2996. }
  2997. return null;
  2998. }
  2999. var start = find(startNode), end = find(endNode);
  3000. if (start && end && start === end) {
  3001. return K(start);
  3002. }
  3003. return null;
  3004. },
  3005. state : function(key) {
  3006. var self = this, doc = self.doc, bool = false;
  3007. try {
  3008. bool = doc.queryCommandState(key);
  3009. } catch (e) {}
  3010. return bool;
  3011. },
  3012. val : function(key) {
  3013. var self = this, doc = self.doc, range = self.range;
  3014. function lc(val) {
  3015. return val.toLowerCase();
  3016. }
  3017. key = lc(key);
  3018. var val = '', knode;
  3019. if (key === 'fontfamily' || key === 'fontname') {
  3020. val = _nativeCommandValue(doc, 'fontname');
  3021. val = val.replace(/['"]/g, '');
  3022. return lc(val);
  3023. }
  3024. if (key === 'formatblock') {
  3025. val = _nativeCommandValue(doc, key);
  3026. if (val === '') {
  3027. knode = self.commonNode({'h1,h2,h3,h4,h5,h6,p,div,pre,address' : '*'});
  3028. if (knode) {
  3029. val = knode.name;
  3030. }
  3031. }
  3032. if (val === 'Normal') {
  3033. val = 'p';
  3034. }
  3035. return lc(val);
  3036. }
  3037. if (key === 'fontsize') {
  3038. knode = self.commonNode({'*' : '.font-size'});
  3039. if (knode) {
  3040. val = knode.css('font-size');
  3041. }
  3042. return lc(val);
  3043. }
  3044. if (key === 'forecolor') {
  3045. knode = self.commonNode({'*' : '.color'});
  3046. if (knode) {
  3047. val = knode.css('color');
  3048. }
  3049. val = _toHex(val);
  3050. if (val === '') {
  3051. val = 'default';
  3052. }
  3053. return lc(val);
  3054. }
  3055. if (key === 'hilitecolor') {
  3056. knode = self.commonNode({'*' : '.background-color'});
  3057. if (knode) {
  3058. val = knode.css('background-color');
  3059. }
  3060. val = _toHex(val);
  3061. if (val === '') {
  3062. val = 'default';
  3063. }
  3064. return lc(val);
  3065. }
  3066. return val;
  3067. },
  3068. toggle : function(wrapper, map) {
  3069. var self = this;
  3070. if (self.commonNode(map)) {
  3071. self.remove(map);
  3072. } else {
  3073. self.wrap(wrapper);
  3074. }
  3075. return self.select();
  3076. },
  3077. bold : function() {
  3078. return this.toggle('<strong></strong>', {
  3079. span : '.font-weight=bold',
  3080. strong : '*',
  3081. b : '*'
  3082. });
  3083. },
  3084. italic : function() {
  3085. return this.toggle('<em></em>', {
  3086. span : '.font-style=italic',
  3087. em : '*',
  3088. i : '*'
  3089. });
  3090. },
  3091. underline : function() {
  3092. return this.toggle('<u></u>', {
  3093. span : '.text-decoration=underline',
  3094. u : '*'
  3095. });
  3096. },
  3097. strikethrough : function() {
  3098. return this.toggle('<s></s>', {
  3099. span : '.text-decoration=line-through',
  3100. s : '*'
  3101. });
  3102. },
  3103. forecolor : function(val) {
  3104. return this.wrap('<span style="color:' + val + ';"></span>').select();
  3105. },
  3106. hilitecolor : function(val) {
  3107. return this.wrap('<span style="background-color:' + val + ';"></span>').select();
  3108. },
  3109. fontsize : function(val) {
  3110. return this.wrap('<span style="font-size:' + val + ';"></span>').select();
  3111. },
  3112. fontname : function(val) {
  3113. return this.fontfamily(val);
  3114. },
  3115. fontfamily : function(val) {
  3116. return this.wrap('<span style="font-family:' + val + ';"></span>').select();
  3117. },
  3118. removeformat : function() {
  3119. var map = {
  3120. '*' : '.font-weight,.font-style,.text-decoration,.color,.background-color,.font-size,.font-family,.text-indent'
  3121. },
  3122. tags = _STYLE_TAG_MAP;
  3123. _each(tags, function(key, val) {
  3124. map[key] = '*';
  3125. });
  3126. this.remove(map);
  3127. return this.select();
  3128. },
  3129. inserthtml : function(val, quickMode) {
  3130. var self = this, range = self.range;
  3131. if (val === '') {
  3132. return self;
  3133. }
  3134. function pasteHtml(range, val) {
  3135. val = '<img id="__kindeditor_temp_tag__" width="0" height="0" style="display:none;" />' + val;
  3136. var rng = range.get();
  3137. if (rng.item) {
  3138. rng.item(0).outerHTML = val;
  3139. } else {
  3140. rng.pasteHTML(val);
  3141. }
  3142. var temp = range.doc.getElementById('__kindeditor_temp_tag__');
  3143. temp.parentNode.removeChild(temp);
  3144. var newRange = _toRange(rng);
  3145. range.setEnd(newRange.endContainer, newRange.endOffset);
  3146. range.collapse(false);
  3147. self.select(false);
  3148. }
  3149. function insertHtml(range, val) {
  3150. var doc = range.doc,
  3151. frag = doc.createDocumentFragment();
  3152. K('@' + val, doc).each(function() {
  3153. frag.appendChild(this);
  3154. });
  3155. range.deleteContents();
  3156. range.insertNode(frag);
  3157. range.collapse(false);
  3158. self.select(false);
  3159. }
  3160. if (_IERANGE && quickMode) {
  3161. try {
  3162. pasteHtml(range, val);
  3163. } catch(e) {
  3164. insertHtml(range, val);
  3165. }
  3166. return self;
  3167. }
  3168. insertHtml(range, val);
  3169. return self;
  3170. },
  3171. hr : function() {
  3172. return this.inserthtml('<hr />');
  3173. },
  3174. print : function() {
  3175. this.win.print();
  3176. return this;
  3177. },
  3178. insertimage : function(url, title, width, height, border, align) {
  3179. title = _undef(title, '');
  3180. border = _undef(border, 0);
  3181. var html = '<img src="' + _escape(url) + '" data-ke-src="' + _escape(url) + '" ';
  3182. if (width) {
  3183. html += 'width="' + _escape(width) + '" ';
  3184. }
  3185. if (height) {
  3186. html += 'height="' + _escape(height) + '" ';
  3187. }
  3188. if (title) {
  3189. html += 'title="' + _escape(title) + '" ';
  3190. }
  3191. if (align) {
  3192. html += 'align="' + _escape(align) + '" ';
  3193. }
  3194. html += 'alt="' + _escape(title) + '" ';
  3195. html += '/>';
  3196. return this.inserthtml(html);
  3197. },
  3198. createlink : function(url, type) {
  3199. var self = this, doc = self.doc, range = self.range;
  3200. self.select();
  3201. var a = self.commonNode({ a : '*' });
  3202. if (a && !range.isControl()) {
  3203. range.selectNode(a.get());
  3204. self.select();
  3205. }
  3206. var html = '<a href="' + _escape(url) + '" data-ke-src="' + _escape(url) + '" ';
  3207. if (type) {
  3208. html += ' target="' + _escape(type) + '"';
  3209. }
  3210. if (range.collapsed) {
  3211. html += '>' + _escape(url) + '</a>';
  3212. return self.inserthtml(html);
  3213. }
  3214. if (range.isControl()) {
  3215. var node = K(range.startContainer.childNodes[range.startOffset]);
  3216. html += '></a>';
  3217. node.after(K(html, doc));
  3218. node.next().append(node);
  3219. range.selectNode(node[0]);
  3220. return self.select();
  3221. }
  3222. function setAttr(node, url, type) {
  3223. K(node).attr('href', url).attr('data-ke-src', url);
  3224. if (type) {
  3225. K(node).attr('target', type);
  3226. } else {
  3227. K(node).removeAttr('target');
  3228. }
  3229. }
  3230. var sc = range.startContainer, so = range.startOffset,
  3231. ec = range.endContainer, eo = range.endOffset;
  3232. if (sc.nodeType == 1 && sc === ec && so + 1 === eo) {
  3233. var child = sc.childNodes[so];
  3234. if (child.nodeName.toLowerCase() == 'a') {
  3235. setAttr(child, url, type);
  3236. return self;
  3237. }
  3238. }
  3239. _nativeCommand(doc, 'createlink', '__kindeditor_temp_url__');
  3240. K('a[href="__kindeditor_temp_url__"]', doc).each(function() {
  3241. setAttr(this, url, type);
  3242. });
  3243. return self;
  3244. },
  3245. unlink : function() {
  3246. var self = this, doc = self.doc, range = self.range;
  3247. self.select();
  3248. if (range.collapsed) {
  3249. var a = self.commonNode({ a : '*' });
  3250. if (a) {
  3251. range.selectNode(a.get());
  3252. self.select();
  3253. }
  3254. _nativeCommand(doc, 'unlink', null);
  3255. if (_WEBKIT && K(range.startContainer).name === 'img') {
  3256. var parent = K(range.startContainer).parent();
  3257. if (parent.name === 'a') {
  3258. parent.remove(true);
  3259. }
  3260. }
  3261. } else {
  3262. _nativeCommand(doc, 'unlink', null);
  3263. }
  3264. return self;
  3265. }
  3266. });
  3267. _each(('formatblock,selectall,justifyleft,justifycenter,justifyright,justifyfull,insertorderedlist,' +
  3268. 'insertunorderedlist,indent,outdent,subscript,superscript').split(','), function(i, name) {
  3269. KCmd.prototype[name] = function(val) {
  3270. var self = this;
  3271. self.select();
  3272. _nativeCommand(self.doc, name, val);
  3273. if (_IERANGE && _inArray(name, 'justifyleft,justifycenter,justifyright,justifyfull'.split(',')) >= 0) {
  3274. self.selection();
  3275. }
  3276. if (!_IERANGE || _inArray(name, 'formatblock,selectall,insertorderedlist,insertunorderedlist'.split(',')) >= 0) {
  3277. self.selection();
  3278. }
  3279. return self;
  3280. };
  3281. });
  3282. _each('cut,copy,paste'.split(','), function(i, name) {
  3283. KCmd.prototype[name] = function() {
  3284. var self = this;
  3285. if (!self.doc.queryCommandSupported(name)) {
  3286. throw 'not supported';
  3287. }
  3288. self.select();
  3289. _nativeCommand(self.doc, name, null);
  3290. return self;
  3291. };
  3292. });
  3293. function _cmd(mixed) {
  3294. if (mixed.nodeName) {
  3295. var doc = _getDoc(mixed);
  3296. mixed = _range(doc).selectNodeContents(doc.body).collapse(false);
  3297. }
  3298. return new KCmd(mixed);
  3299. }
  3300. K.CmdClass = KCmd;
  3301. K.cmd = _cmd;
  3302. function _drag(options) {
  3303. var moveEl = options.moveEl,
  3304. moveFn = options.moveFn,
  3305. clickEl = options.clickEl || moveEl,
  3306. beforeDrag = options.beforeDrag,
  3307. iframeFix = options.iframeFix === undefined ? true : options.iframeFix;
  3308. var docs = [document];
  3309. if (iframeFix) {
  3310. K('iframe').each(function() {
  3311. var src = _formatUrl(this.src || '', 'absolute');
  3312. if (/^https?:\/\//.test(src)) {
  3313. return;
  3314. }
  3315. var doc;
  3316. try {
  3317. doc = _iframeDoc(this);
  3318. } catch(e) {}
  3319. if (doc) {
  3320. var pos = K(this).pos();
  3321. K(doc).data('pos-x', pos.x);
  3322. K(doc).data('pos-y', pos.y);
  3323. docs.push(doc);
  3324. }
  3325. });
  3326. }
  3327. clickEl.mousedown(function(e) {
  3328. e.stopPropagation();
  3329. var self = clickEl.get(),
  3330. x = _removeUnit(moveEl.css('left')),
  3331. y = _removeUnit(moveEl.css('top')),
  3332. width = moveEl.width(),
  3333. height = moveEl.height(),
  3334. pageX = e.pageX,
  3335. pageY = e.pageY;
  3336. if (beforeDrag) {
  3337. beforeDrag();
  3338. }
  3339. function moveListener(e) {
  3340. e.preventDefault();
  3341. var kdoc = K(_getDoc(e.target));
  3342. var diffX = _round((kdoc.data('pos-x') || 0) + e.pageX - pageX);
  3343. var diffY = _round((kdoc.data('pos-y') || 0) + e.pageY - pageY);
  3344. moveFn.call(clickEl, x, y, width, height, diffX, diffY);
  3345. }
  3346. function selectListener(e) {
  3347. e.preventDefault();
  3348. }
  3349. function upListener(e) {
  3350. e.preventDefault();
  3351. K(docs).unbind('mousemove', moveListener)
  3352. .unbind('mouseup', upListener)
  3353. .unbind('selectstart', selectListener);
  3354. if (self.releaseCapture) {
  3355. self.releaseCapture();
  3356. }
  3357. }
  3358. K(docs).mousemove(moveListener)
  3359. .mouseup(upListener)
  3360. .bind('selectstart', selectListener);
  3361. if (self.setCapture) {
  3362. self.setCapture();
  3363. }
  3364. });
  3365. }
  3366. function KWidget(options) {
  3367. this.init(options);
  3368. }
  3369. _extend(KWidget, {
  3370. init : function(options) {
  3371. var self = this;
  3372. self.name = options.name || '';
  3373. self.doc = options.doc || document;
  3374. self.win = _getWin(self.doc);
  3375. self.x = _addUnit(options.x);
  3376. self.y = _addUnit(options.y);
  3377. self.z = options.z;
  3378. self.width = _addUnit(options.width);
  3379. self.height = _addUnit(options.height);
  3380. self.div = K('<div style="display:block;"></div>');
  3381. self.options = options;
  3382. self._alignEl = options.alignEl;
  3383. if (self.width) {
  3384. self.div.css('width', self.width);
  3385. }
  3386. if (self.height) {
  3387. self.div.css('height', self.height);
  3388. }
  3389. if (self.z) {
  3390. self.div.css({
  3391. position : 'absolute',
  3392. left : self.x,
  3393. top : self.y,
  3394. 'z-index' : self.z
  3395. });
  3396. }
  3397. if (self.z && (self.x === undefined || self.y === undefined)) {
  3398. self.autoPos(self.width, self.height);
  3399. }
  3400. if (options.cls) {
  3401. self.div.addClass(options.cls);
  3402. }
  3403. if (options.shadowMode) {
  3404. self.div.addClass('ke-shadow');
  3405. }
  3406. if (options.css) {
  3407. self.div.css(options.css);
  3408. }
  3409. if (options.src) {
  3410. K(options.src).replaceWith(self.div);
  3411. } else {
  3412. K(self.doc.body).append(self.div);
  3413. }
  3414. if (options.html) {
  3415. self.div.html(options.html);
  3416. }
  3417. if (options.autoScroll) {
  3418. if (_IE && _V < 7 || _QUIRKS) {
  3419. var scrollPos = _getScrollPos();
  3420. K(self.win).bind('scroll', function(e) {
  3421. var pos = _getScrollPos(),
  3422. diffX = pos.x - scrollPos.x,
  3423. diffY = pos.y - scrollPos.y;
  3424. self.pos(_removeUnit(self.x) + diffX, _removeUnit(self.y) + diffY, false);
  3425. });
  3426. } else {
  3427. self.div.css('position', 'fixed');
  3428. }
  3429. }
  3430. },
  3431. pos : function(x, y, updateProp) {
  3432. var self = this;
  3433. updateProp = _undef(updateProp, true);
  3434. if (x !== null) {
  3435. x = x < 0 ? 0 : _addUnit(x);
  3436. self.div.css('left', x);
  3437. if (updateProp) {
  3438. self.x = x;
  3439. }
  3440. }
  3441. if (y !== null) {
  3442. y = y < 0 ? 0 : _addUnit(y);
  3443. self.div.css('top', y);
  3444. if (updateProp) {
  3445. self.y = y;
  3446. }
  3447. }
  3448. return self;
  3449. },
  3450. autoPos : function(width, height) {
  3451. var self = this,
  3452. w = _removeUnit(width) || 0,
  3453. h = _removeUnit(height) || 0,
  3454. scrollPos = _getScrollPos();
  3455. if (self._alignEl) {
  3456. var knode = K(self._alignEl),
  3457. pos = knode.pos(),
  3458. diffX = _round(knode[0].clientWidth / 2 - w / 2),
  3459. diffY = _round(knode[0].clientHeight / 2 - h / 2);
  3460. x = diffX < 0 ? pos.x : pos.x + diffX;
  3461. y = diffY < 0 ? pos.y : pos.y + diffY;
  3462. } else {
  3463. var docEl = _docElement(self.doc);
  3464. x = _round(scrollPos.x + (docEl.clientWidth - w) / 2);
  3465. y = _round(scrollPos.y + (docEl.clientHeight - h) / 2);
  3466. }
  3467. if (!(_IE && _V < 7 || _QUIRKS)) {
  3468. x -= scrollPos.x;
  3469. y -= scrollPos.y;
  3470. }
  3471. return self.pos(x, y);
  3472. },
  3473. remove : function() {
  3474. var self = this;
  3475. if (_IE && _V < 7 || _QUIRKS) {
  3476. K(self.win).unbind('scroll');
  3477. }
  3478. self.div.remove();
  3479. _each(self, function(i) {
  3480. self[i] = null;
  3481. });
  3482. return this;
  3483. },
  3484. show : function() {
  3485. this.div.show();
  3486. return this;
  3487. },
  3488. hide : function() {
  3489. this.div.hide();
  3490. return this;
  3491. },
  3492. draggable : function(options) {
  3493. var self = this;
  3494. options = options || {};
  3495. options.moveEl = self.div;
  3496. options.moveFn = function(x, y, width, height, diffX, diffY) {
  3497. if ((x = x + diffX) < 0) {
  3498. x = 0;
  3499. }
  3500. if ((y = y + diffY) < 0) {
  3501. y = 0;
  3502. }
  3503. self.pos(x, y);
  3504. };
  3505. _drag(options);
  3506. return self;
  3507. }
  3508. });
  3509. function _widget(options) {
  3510. return new KWidget(options);
  3511. }
  3512. K.WidgetClass = KWidget;
  3513. K.widget = _widget;
  3514. function _iframeDoc(iframe) {
  3515. iframe = _get(iframe);
  3516. return iframe.contentDocument || iframe.contentWindow.document;
  3517. }
  3518. var html, _direction = '';
  3519. if ((html = document.getElementsByTagName('html'))) {
  3520. _direction = html[0].dir;
  3521. }
  3522. function _getInitHtml(themesPath, bodyClass, cssPath, cssData) {
  3523. var arr = [
  3524. (_direction === '' ? '<html>' : '<html dir="' + _direction + '">'),
  3525. '<head><meta charset="utf-8" /><title></title>',
  3526. '<style>',
  3527. 'html {margin:0;padding:0;}',
  3528. 'body {margin:0;padding:5px;}',
  3529. 'body, td {font:12px/1.5 "sans serif",tahoma,verdana,helvetica;}',
  3530. 'body, p, div {word-wrap: break-word;}',
  3531. 'p {margin:5px 0;}',
  3532. 'table {border-collapse:collapse;}',
  3533. 'img {border:0;}',
  3534. 'noscript {display:none;}',
  3535. 'table.ke-zeroborder td {border:1px dotted #AAA;}',
  3536. 'img.ke-flash {',
  3537. ' border:1px solid #AAA;',
  3538. ' background-image:url(' + themesPath + 'common/flash.gif);',
  3539. ' background-position:center center;',
  3540. ' background-repeat:no-repeat;',
  3541. ' width:100px;',
  3542. ' height:100px;',
  3543. '}',
  3544. 'img.ke-rm {',
  3545. ' border:1px solid #AAA;',
  3546. ' background-image:url(' + themesPath + 'common/rm.gif);',
  3547. ' background-position:center center;',
  3548. ' background-repeat:no-repeat;',
  3549. ' width:100px;',
  3550. ' height:100px;',
  3551. '}',
  3552. 'img.ke-media {',
  3553. ' border:1px solid #AAA;',
  3554. ' background-image:url(' + themesPath + 'common/media.gif);',
  3555. ' background-position:center center;',
  3556. ' background-repeat:no-repeat;',
  3557. ' width:100px;',
  3558. ' height:100px;',
  3559. '}',
  3560. 'img.ke-anchor {',
  3561. ' border:1px dashed #666;',
  3562. ' width:16px;',
  3563. ' height:16px;',
  3564. '}',
  3565. '.ke-script, .ke-noscript, .ke-display-none {',
  3566. ' display:none;',
  3567. ' font-size:0;',
  3568. ' width:0;',
  3569. ' height:0;',
  3570. '}',
  3571. '.ke-pagebreak {',
  3572. ' border:1px dotted #AAA;',
  3573. ' font-size:0;',
  3574. ' height:2px;',
  3575. '}',
  3576. '</style>'
  3577. ];
  3578. if (!_isArray(cssPath)) {
  3579. cssPath = [cssPath];
  3580. }
  3581. _each(cssPath, function(i, path) {
  3582. if (path) {
  3583. arr.push('<link href="' + path + '" rel="stylesheet" />');
  3584. }
  3585. });
  3586. if (cssData) {
  3587. arr.push('<style>' + cssData + '</style>');
  3588. }
  3589. arr.push('</head><body ' + (bodyClass ? 'class="' + bodyClass + '"' : '') + '></body></html>');
  3590. return arr.join('\n');
  3591. }
  3592. function _elementVal(knode, val) {
  3593. if (knode.hasVal()) {
  3594. if (val === undefined) {
  3595. var html = knode.val();
  3596. html = html.replace(/(<(?:p|p\s[^>]*)>) *(<\/p>)/ig, '');
  3597. return html;
  3598. }
  3599. return knode.val(val);
  3600. }
  3601. return knode.html(val);
  3602. }
  3603. function KEdit(options) {
  3604. this.init(options);
  3605. }
  3606. _extend(KEdit, KWidget, {
  3607. init : function(options) {
  3608. var self = this;
  3609. KEdit.parent.init.call(self, options);
  3610. self.srcElement = K(options.srcElement);
  3611. self.div.addClass('ke-edit');
  3612. self.designMode = _undef(options.designMode, true);
  3613. self.beforeGetHtml = options.beforeGetHtml;
  3614. self.beforeSetHtml = options.beforeSetHtml;
  3615. self.afterSetHtml = options.afterSetHtml;
  3616. var themesPath = _undef(options.themesPath, ''),
  3617. bodyClass = options.bodyClass,
  3618. cssPath = options.cssPath,
  3619. cssData = options.cssData,
  3620. isDocumentDomain = location.protocol != 'res:' && location.host.replace(/:\d+/, '') !== document.domain,
  3621. srcScript = ('document.open();' +
  3622. (isDocumentDomain ? 'document.domain="' + document.domain + '";' : '') +
  3623. 'document.close();'),
  3624. iframeSrc = _IE ? ' src="javascript:void(function(){' + encodeURIComponent(srcScript) + '}())"' : '';
  3625. self.iframe = K('<iframe class="ke-edit-iframe" hidefocus="true" frameborder="0"' + iframeSrc + '></iframe>').css('width', '100%');
  3626. self.textarea = K('<textarea class="ke-edit-textarea" hidefocus="true"></textarea>').css('width', '100%');
  3627. self.tabIndex = isNaN(parseInt(options.tabIndex, 10)) ? self.srcElement.attr('tabindex') : parseInt(options.tabIndex, 10);
  3628. self.iframe.attr('tabindex', self.tabIndex);
  3629. self.textarea.attr('tabindex', self.tabIndex);
  3630. if (self.width) {
  3631. self.setWidth(self.width);
  3632. }
  3633. if (self.height) {
  3634. self.setHeight(self.height);
  3635. }
  3636. if (self.designMode) {
  3637. self.textarea.hide();
  3638. } else {
  3639. self.iframe.hide();
  3640. }
  3641. function ready() {
  3642. var doc = _iframeDoc(self.iframe);
  3643. doc.open();
  3644. if (isDocumentDomain) {
  3645. doc.domain = document.domain;
  3646. }
  3647. doc.write(_getInitHtml(themesPath, bodyClass, cssPath, cssData));
  3648. doc.close();
  3649. self.win = self.iframe[0].contentWindow;
  3650. self.doc = doc;
  3651. var cmd = _cmd(doc);
  3652. self.afterChange(function(e) {
  3653. cmd.selection();
  3654. });
  3655. if (_WEBKIT) {
  3656. K(doc).click(function(e) {
  3657. if (K(e.target).name === 'img') {
  3658. cmd.selection(true);
  3659. cmd.range.selectNode(e.target);
  3660. cmd.select();
  3661. }
  3662. });
  3663. }
  3664. if (_IE) {
  3665. self._mousedownHandler = function() {
  3666. var newRange = cmd.range.cloneRange();
  3667. newRange.shrink();
  3668. if (newRange.isControl()) {
  3669. self.blur();
  3670. }
  3671. };
  3672. K(document).mousedown(self._mousedownHandler);
  3673. K(doc).keydown(function(e) {
  3674. if (e.which == 8) {
  3675. cmd.selection();
  3676. var rng = cmd.range;
  3677. if (rng.isControl()) {
  3678. rng.collapse(true);
  3679. K(rng.startContainer.childNodes[rng.startOffset]).remove();
  3680. e.preventDefault();
  3681. }
  3682. }
  3683. });
  3684. }
  3685. self.cmd = cmd;
  3686. self.html(_elementVal(self.srcElement));
  3687. if (_IE) {
  3688. doc.body.disabled = true;
  3689. doc.body.contentEditable = true;
  3690. doc.body.removeAttribute('disabled');
  3691. } else {
  3692. doc.designMode = 'on';
  3693. }
  3694. if (options.afterCreate) {
  3695. options.afterCreate.call(self);
  3696. }
  3697. }
  3698. if (isDocumentDomain) {
  3699. self.iframe.bind('load', function(e) {
  3700. self.iframe.unbind('load');
  3701. if (_IE) {
  3702. ready();
  3703. } else {
  3704. setTimeout(ready, 0);
  3705. }
  3706. });
  3707. }
  3708. self.div.append(self.iframe);
  3709. self.div.append(self.textarea);
  3710. self.srcElement.hide();
  3711. !isDocumentDomain && ready();
  3712. },
  3713. setWidth : function(val) {
  3714. var self = this;
  3715. val = _addUnit(val);
  3716. self.width = val;
  3717. self.div.css('width', val);
  3718. return self;
  3719. },
  3720. setHeight : function(val) {
  3721. var self = this;
  3722. val = _addUnit(val);
  3723. self.height = val;
  3724. self.div.css('height', val);
  3725. self.iframe.css('height', val);
  3726. if ((_IE && _V < 8) || _QUIRKS) {
  3727. val = _addUnit(_removeUnit(val) - 2);
  3728. }
  3729. self.textarea.css('height', val);
  3730. return self;
  3731. },
  3732. remove : function() {
  3733. var self = this, doc = self.doc;
  3734. K(doc.body).unbind();
  3735. K(doc).unbind();
  3736. K(self.win).unbind();
  3737. if (self._mousedownHandler) {
  3738. K(document).unbind('mousedown', self._mousedownHandler);
  3739. }
  3740. _elementVal(self.srcElement, self.html());
  3741. self.srcElement.show();
  3742. doc.write('');
  3743. self.iframe.unbind();
  3744. self.textarea.unbind();
  3745. KEdit.parent.remove.call(self);
  3746. },
  3747. html : function(val, isFull) {
  3748. var self = this, doc = self.doc;
  3749. if (self.designMode) {
  3750. var body = doc.body;
  3751. if (val === undefined) {
  3752. if (isFull) {
  3753. val = '<!doctype html><html>' + body.parentNode.innerHTML + '</html>';
  3754. } else {
  3755. val = body.innerHTML;
  3756. }
  3757. if (self.beforeGetHtml) {
  3758. val = self.beforeGetHtml(val);
  3759. }
  3760. if (_GECKO && val == '<br />') {
  3761. val = '';
  3762. }
  3763. return val;
  3764. }
  3765. if (self.beforeSetHtml) {
  3766. val = self.beforeSetHtml(val);
  3767. }
  3768. if (_IE && _V >= 9) {
  3769. val = val.replace(/(<.*?checked=")checked(".*>)/ig, '$1$2');
  3770. }
  3771. K(body).html(val);
  3772. if (self.afterSetHtml) {
  3773. self.afterSetHtml();
  3774. }
  3775. return self;
  3776. }
  3777. if (val === undefined) {
  3778. return self.textarea.val();
  3779. }
  3780. self.textarea.val(val);
  3781. return self;
  3782. },
  3783. design : function(bool) {
  3784. var self = this, val;
  3785. if (bool === undefined ? !self.designMode : bool) {
  3786. if (!self.designMode) {
  3787. val = self.html();
  3788. self.designMode = true;
  3789. self.html(val);
  3790. self.textarea.hide();
  3791. self.iframe.show();
  3792. }
  3793. } else {
  3794. if (self.designMode) {
  3795. val = self.html();
  3796. self.designMode = false;
  3797. self.html(val);
  3798. self.iframe.hide();
  3799. self.textarea.show();
  3800. }
  3801. }
  3802. return self.focus();
  3803. },
  3804. focus : function() {
  3805. var self = this;
  3806. self.designMode ? self.win.focus() : self.textarea[0].focus();
  3807. return self;
  3808. },
  3809. blur : function() {
  3810. var self = this;
  3811. if (_IE) {
  3812. var input = K('<input type="text" style="float:left;width:0;height:0;padding:0;margin:0;border:0;" value="" />', self.div);
  3813. self.div.append(input);
  3814. input[0].focus();
  3815. input.remove();
  3816. } else {
  3817. self.designMode ? self.win.blur() : self.textarea[0].blur();
  3818. }
  3819. return self;
  3820. },
  3821. afterChange : function(fn) {
  3822. var self = this, doc = self.doc, body = doc.body;
  3823. K(doc).keyup(function(e) {
  3824. if (!e.ctrlKey && !e.altKey && _CHANGE_KEY_MAP[e.which]) {
  3825. fn(e);
  3826. }
  3827. });
  3828. K(doc).mouseup(fn).contextmenu(fn);
  3829. K(self.win).blur(fn);
  3830. function timeoutHandler(e) {
  3831. setTimeout(function() {
  3832. fn(e);
  3833. }, 1);
  3834. }
  3835. K(body).bind('paste', timeoutHandler);
  3836. K(body).bind('cut', timeoutHandler);
  3837. return self;
  3838. }
  3839. });
  3840. function _edit(options) {
  3841. return new KEdit(options);
  3842. }
  3843. K.EditClass = KEdit;
  3844. K.edit = _edit;
  3845. K.iframeDoc = _iframeDoc;
  3846. function _selectToolbar(name, fn) {
  3847. var self = this,
  3848. knode = self.get(name);
  3849. if (knode) {
  3850. if (knode.hasClass('ke-disabled')) {
  3851. return;
  3852. }
  3853. fn(knode);
  3854. }
  3855. }
  3856. function KToolbar(options) {
  3857. this.init(options);
  3858. }
  3859. _extend(KToolbar, KWidget, {
  3860. init : function(options) {
  3861. var self = this;
  3862. KToolbar.parent.init.call(self, options);
  3863. self.disableMode = _undef(options.disableMode, false);
  3864. self.noDisableItemMap = _toMap(_undef(options.noDisableItems, []));
  3865. self._itemMap = {};
  3866. self.div.addClass('ke-toolbar').bind('contextmenu,mousedown,mousemove', function(e) {
  3867. e.preventDefault();
  3868. }).attr('unselectable', 'on');
  3869. function find(target) {
  3870. var knode = K(target);
  3871. if (knode.hasClass('ke-outline')) {
  3872. return knode;
  3873. }
  3874. if (knode.hasClass('ke-toolbar-icon')) {
  3875. return knode.parent();
  3876. }
  3877. }
  3878. function hover(e, method) {
  3879. var knode = find(e.target);
  3880. if (knode) {
  3881. if (knode.hasClass('ke-disabled')) {
  3882. return;
  3883. }
  3884. if (knode.hasClass('ke-selected')) {
  3885. return;
  3886. }
  3887. knode[method]('ke-on');
  3888. }
  3889. }
  3890. self.div.mouseover(function(e) {
  3891. hover(e, 'addClass');
  3892. })
  3893. .mouseout(function(e) {
  3894. hover(e, 'removeClass');
  3895. })
  3896. .click(function(e) {
  3897. var knode = find(e.target);
  3898. if (knode) {
  3899. if (knode.hasClass('ke-disabled')) {
  3900. return;
  3901. }
  3902. self.options.click.call(this, e, knode.attr('data-name'));
  3903. }
  3904. });
  3905. },
  3906. get : function(name) {
  3907. if (this._itemMap[name]) {
  3908. return this._itemMap[name];
  3909. }
  3910. return (this._itemMap[name] = K('span.ke-icon-' + name, this.div).parent());
  3911. },
  3912. select : function(name) {
  3913. _selectToolbar.call(this, name, function(knode) {
  3914. knode.addClass('ke-selected');
  3915. });
  3916. return self;
  3917. },
  3918. unselect : function(name) {
  3919. _selectToolbar.call(this, name, function(knode) {
  3920. knode.removeClass('ke-selected').removeClass('ke-on');
  3921. });
  3922. return self;
  3923. },
  3924. enable : function(name) {
  3925. var self = this,
  3926. knode = name.get ? name : self.get(name);
  3927. if (knode) {
  3928. knode.removeClass('ke-disabled');
  3929. knode.opacity(1);
  3930. }
  3931. return self;
  3932. },
  3933. disable : function(name) {
  3934. var self = this,
  3935. knode = name.get ? name : self.get(name);
  3936. if (knode) {
  3937. knode.removeClass('ke-selected').addClass('ke-disabled');
  3938. knode.opacity(0.5);
  3939. }
  3940. return self;
  3941. },
  3942. disableAll : function(bool, noDisableItems) {
  3943. var self = this, map = self.noDisableItemMap, item;
  3944. if (noDisableItems) {
  3945. map = _toMap(noDisableItems);
  3946. }
  3947. if (bool === undefined ? !self.disableMode : bool) {
  3948. K('span.ke-outline', self.div).each(function() {
  3949. var knode = K(this),
  3950. name = knode[0].getAttribute('data-name', 2);
  3951. if (!map[name]) {
  3952. self.disable(knode);
  3953. }
  3954. });
  3955. self.disableMode = true;
  3956. } else {
  3957. K('span.ke-outline', self.div).each(function() {
  3958. var knode = K(this),
  3959. name = knode[0].getAttribute('data-name', 2);
  3960. if (!map[name]) {
  3961. self.enable(knode);
  3962. }
  3963. });
  3964. self.disableMode = false;
  3965. }
  3966. return self;
  3967. }
  3968. });
  3969. function _toolbar(options) {
  3970. return new KToolbar(options);
  3971. }
  3972. K.ToolbarClass = KToolbar;
  3973. K.toolbar = _toolbar;
  3974. function KMenu(options) {
  3975. this.init(options);
  3976. }
  3977. _extend(KMenu, KWidget, {
  3978. init : function(options) {
  3979. var self = this;
  3980. options.z = options.z || 811213;
  3981. KMenu.parent.init.call(self, options);
  3982. self.centerLineMode = _undef(options.centerLineMode, true);
  3983. self.div.addClass('ke-menu').bind('click,mousedown', function(e){
  3984. e.stopPropagation();
  3985. }).attr('unselectable', 'on');
  3986. },
  3987. addItem : function(item) {
  3988. var self = this;
  3989. if (item.title === '-') {
  3990. self.div.append(K('<div class="ke-menu-separator"></div>'));
  3991. return;
  3992. }
  3993. var itemDiv = K('<div class="ke-menu-item" unselectable="on"></div>'),
  3994. leftDiv = K('<div class="ke-inline-block ke-menu-item-left"></div>'),
  3995. rightDiv = K('<div class="ke-inline-block ke-menu-item-right"></div>'),
  3996. height = _addUnit(item.height),
  3997. iconClass = _undef(item.iconClass, '');
  3998. self.div.append(itemDiv);
  3999. if (height) {
  4000. itemDiv.css('height', height);
  4001. rightDiv.css('line-height', height);
  4002. }
  4003. var centerDiv;
  4004. if (self.centerLineMode) {
  4005. centerDiv = K('<div class="ke-inline-block ke-menu-item-center"></div>');
  4006. if (height) {
  4007. centerDiv.css('height', height);
  4008. }
  4009. }
  4010. itemDiv.mouseover(function(e) {
  4011. K(this).addClass('ke-menu-item-on');
  4012. if (centerDiv) {
  4013. centerDiv.addClass('ke-menu-item-center-on');
  4014. }
  4015. })
  4016. .mouseout(function(e) {
  4017. K(this).removeClass('ke-menu-item-on');
  4018. if (centerDiv) {
  4019. centerDiv.removeClass('ke-menu-item-center-on');
  4020. }
  4021. })
  4022. .click(function(e) {
  4023. item.click.call(K(this));
  4024. e.stopPropagation();
  4025. })
  4026. .append(leftDiv);
  4027. if (centerDiv) {
  4028. itemDiv.append(centerDiv);
  4029. }
  4030. itemDiv.append(rightDiv);
  4031. if (item.checked) {
  4032. iconClass = 'ke-icon-checked';
  4033. }
  4034. if (iconClass !== '') {
  4035. leftDiv.html('<span class="ke-inline-block ke-toolbar-icon ke-toolbar-icon-url ' + iconClass + '"></span>');
  4036. }
  4037. rightDiv.html(item.title);
  4038. return self;
  4039. },
  4040. remove : function() {
  4041. var self = this;
  4042. if (self.options.beforeRemove) {
  4043. self.options.beforeRemove.call(self);
  4044. }
  4045. K('.ke-menu-item', self.div[0]).unbind();
  4046. KMenu.parent.remove.call(self);
  4047. return self;
  4048. }
  4049. });
  4050. function _menu(options) {
  4051. return new KMenu(options);
  4052. }
  4053. K.MenuClass = KMenu;
  4054. K.menu = _menu;
  4055. function KColorPicker(options) {
  4056. this.init(options);
  4057. }
  4058. _extend(KColorPicker, KWidget, {
  4059. init : function(options) {
  4060. var self = this;
  4061. options.z = options.z || 811213;
  4062. KColorPicker.parent.init.call(self, options);
  4063. var colors = options.colors || [
  4064. ['#E53333', '#E56600', '#FF9900', '#64451D', '#DFC5A4', '#FFE500'],
  4065. ['#009900', '#006600', '#99BB00', '#B8D100', '#60D978', '#00D5FF'],
  4066. ['#337FE5', '#003399', '#4C33E5', '#9933E5', '#CC33E5', '#EE33EE'],
  4067. ['#FFFFFF', '#CCCCCC', '#999999', '#666666', '#333333', '#000000']
  4068. ];
  4069. self.selectedColor = (options.selectedColor || '').toLowerCase();
  4070. self._cells = [];
  4071. self.div.addClass('ke-colorpicker').bind('click,mousedown', function(e){
  4072. e.stopPropagation();
  4073. }).attr('unselectable', 'on');
  4074. var table = self.doc.createElement('table');
  4075. self.div.append(table);
  4076. table.className = 'ke-colorpicker-table';
  4077. table.cellPadding = 0;
  4078. table.cellSpacing = 0;
  4079. table.border = 0;
  4080. var row = table.insertRow(0), cell = row.insertCell(0);
  4081. cell.colSpan = colors[0].length;
  4082. self._addAttr(cell, '', 'ke-colorpicker-cell-top');
  4083. for (var i = 0; i < colors.length; i++) {
  4084. row = table.insertRow(i + 1);
  4085. for (var j = 0; j < colors[i].length; j++) {
  4086. cell = row.insertCell(j);
  4087. self._addAttr(cell, colors[i][j], 'ke-colorpicker-cell');
  4088. }
  4089. }
  4090. },
  4091. _addAttr : function(cell, color, cls) {
  4092. var self = this;
  4093. cell = K(cell).addClass(cls);
  4094. if (self.selectedColor === color.toLowerCase()) {
  4095. cell.addClass('ke-colorpicker-cell-selected');
  4096. }
  4097. cell.attr('title', color || self.options.noColor);
  4098. cell.mouseover(function(e) {
  4099. K(this).addClass('ke-colorpicker-cell-on');
  4100. });
  4101. cell.mouseout(function(e) {
  4102. K(this).removeClass('ke-colorpicker-cell-on');
  4103. });
  4104. cell.click(function(e) {
  4105. e.stop();
  4106. self.options.click.call(K(this), color);
  4107. });
  4108. if (color) {
  4109. cell.append(K('<div class="ke-colorpicker-cell-color" unselectable="on"></div>').css('background-color', color));
  4110. } else {
  4111. cell.html(self.options.noColor);
  4112. }
  4113. K(cell).attr('unselectable', 'on');
  4114. self._cells.push(cell);
  4115. },
  4116. remove : function() {
  4117. var self = this;
  4118. _each(self._cells, function() {
  4119. this.unbind();
  4120. });
  4121. KColorPicker.parent.remove.call(self);
  4122. return self;
  4123. }
  4124. });
  4125. function _colorpicker(options) {
  4126. return new KColorPicker(options);
  4127. }
  4128. K.ColorPickerClass = KColorPicker;
  4129. K.colorpicker = _colorpicker;
  4130. function KUploadButton(options) {
  4131. this.init(options);
  4132. }
  4133. _extend(KUploadButton, {
  4134. init : function(options) {
  4135. var self = this,
  4136. button = K(options.button),
  4137. fieldName = options.fieldName || 'file',
  4138. url = options.url || '',
  4139. title = button.val(),
  4140. extraParams = options.extraParams || {},
  4141. cls = button[0].className || '',
  4142. target = options.target || 'kindeditor_upload_iframe_' + new Date().getTime();
  4143. options.afterError = options.afterError || function(str) {
  4144. alert(str);
  4145. };
  4146. var hiddenElements = [];
  4147. for(var k in extraParams){
  4148. hiddenElements.push('<input type="hidden" name="' + k + '" value="' + extraParams[k] + '" />');
  4149. }
  4150. var html = [
  4151. '<div class="ke-inline-block ' + cls + '">',
  4152. (options.target ? '' : '<iframe name="' + target + '" style="display:none;"></iframe>'),
  4153. (options.form ? '<div class="ke-upload-area">' : '<form class="ke-upload-area ke-form" method="post" enctype="multipart/form-data" target="' + target + '" action="' + url + '">'),
  4154. '<span class="ke-button-common">',
  4155. hiddenElements.join(''),
  4156. '<input type="button" class="ke-button-common ke-button" value="' + title + '" />',
  4157. '</span>',
  4158. '<input type="file" class="ke-upload-file" name="' + fieldName + '" tabindex="-1" />',
  4159. (options.form ? '</div>' : '</form>'),
  4160. '</div>'].join('');
  4161. var div = K(html, button.doc);
  4162. button.hide();
  4163. button.before(div);
  4164. self.div = div;
  4165. self.button = button;
  4166. self.iframe = options.target ? K('iframe[name="' + target + '"]') : K('iframe', div);
  4167. self.form = options.form ? K(options.form) : K('form', div);
  4168. self.fileBox = K('.ke-upload-file', div);
  4169. var width = options.width || K('.ke-button-common', div).width();
  4170. K('.ke-upload-area', div).width(width);
  4171. self.options = options;
  4172. },
  4173. submit : function() {
  4174. var self = this,
  4175. iframe = self.iframe;
  4176. iframe.bind('load', function() {
  4177. iframe.unbind();
  4178. var tempForm = document.createElement('form');
  4179. self.fileBox.before(tempForm);
  4180. K(tempForm).append(self.fileBox);
  4181. tempForm.reset();
  4182. K(tempForm).remove(true);
  4183. var doc = K.iframeDoc(iframe),
  4184. pre = doc.getElementsByTagName('pre')[0],
  4185. str = '', data;
  4186. if (pre) {
  4187. str = pre.innerHTML;
  4188. } else {
  4189. str = doc.body.innerHTML;
  4190. }
  4191. str = _unescape(str);
  4192. iframe[0].src = 'javascript:false';
  4193. try {
  4194. data = K.json(str);
  4195. } catch (e) {
  4196. self.options.afterError.call(self, '<!doctype html><html>' + doc.body.parentNode.innerHTML + '</html>');
  4197. }
  4198. if (data) {
  4199. self.options.afterUpload.call(self, data);
  4200. }
  4201. });
  4202. self.form[0].submit();
  4203. return self;
  4204. },
  4205. remove : function() {
  4206. var self = this;
  4207. if (self.fileBox) {
  4208. self.fileBox.unbind();
  4209. }
  4210. self.iframe.remove();
  4211. self.div.remove();
  4212. self.button.show();
  4213. return self;
  4214. }
  4215. });
  4216. function _uploadbutton(options) {
  4217. return new KUploadButton(options);
  4218. }
  4219. K.UploadButtonClass = KUploadButton;
  4220. K.uploadbutton = _uploadbutton;
  4221. function _createButton(arg) {
  4222. arg = arg || {};
  4223. var name = arg.name || '',
  4224. span = K('<span class="ke-button-common ke-button-outer" title="' + name + '"></span>'),
  4225. btn = K('<input class="ke-button-common ke-button" type="button" value="' + name + '" />');
  4226. if (arg.click) {
  4227. btn.click(arg.click);
  4228. }
  4229. span.append(btn);
  4230. return span;
  4231. }
  4232. function KDialog(options) {
  4233. this.init(options);
  4234. }
  4235. _extend(KDialog, KWidget, {
  4236. init : function(options) {
  4237. var self = this;
  4238. var shadowMode = _undef(options.shadowMode, true);
  4239. options.z = options.z || 811213;
  4240. options.shadowMode = false;
  4241. options.autoScroll = _undef(options.autoScroll, true);
  4242. KDialog.parent.init.call(self, options);
  4243. var title = options.title,
  4244. body = K(options.body, self.doc),
  4245. previewBtn = options.previewBtn,
  4246. yesBtn = options.yesBtn,
  4247. noBtn = options.noBtn,
  4248. closeBtn = options.closeBtn,
  4249. showMask = _undef(options.showMask, true);
  4250. self.div.addClass('ke-dialog').bind('click,mousedown', function(e){
  4251. e.stopPropagation();
  4252. });
  4253. var contentDiv = K('<div class="ke-dialog-content"></div>').appendTo(self.div);
  4254. if (_IE && _V < 7) {
  4255. self.iframeMask = K('<iframe src="about:blank" class="ke-dialog-shadow"></iframe>').appendTo(self.div);
  4256. } else if (shadowMode) {
  4257. K('<div class="ke-dialog-shadow"></div>').appendTo(self.div);
  4258. }
  4259. var headerDiv = K('<div class="ke-dialog-header"></div>');
  4260. contentDiv.append(headerDiv);
  4261. headerDiv.html(title);
  4262. self.closeIcon = K('<span class="ke-dialog-icon-close" title="' + closeBtn.name + '"></span>').click(closeBtn.click);
  4263. headerDiv.append(self.closeIcon);
  4264. self.draggable({
  4265. clickEl : headerDiv,
  4266. beforeDrag : options.beforeDrag
  4267. });
  4268. var bodyDiv = K('<div class="ke-dialog-body"></div>');
  4269. contentDiv.append(bodyDiv);
  4270. bodyDiv.append(body);
  4271. var footerDiv = K('<div class="ke-dialog-footer"></div>');
  4272. if (previewBtn || yesBtn || noBtn) {
  4273. contentDiv.append(footerDiv);
  4274. }
  4275. _each([
  4276. { btn : previewBtn, name : 'preview' },
  4277. { btn : yesBtn, name : 'yes' },
  4278. { btn : noBtn, name : 'no' }
  4279. ], function() {
  4280. if (this.btn) {
  4281. var button = _createButton(this.btn);
  4282. button.addClass('ke-dialog-' + this.name);
  4283. footerDiv.append(button);
  4284. }
  4285. });
  4286. if (self.height) {
  4287. bodyDiv.height(_removeUnit(self.height) - headerDiv.height() - footerDiv.height());
  4288. }
  4289. self.div.width(self.div.width());
  4290. self.div.height(self.div.height());
  4291. self.mask = null;
  4292. if (showMask) {
  4293. var docEl = _docElement(self.doc),
  4294. docWidth = Math.max(docEl.scrollWidth, docEl.clientWidth),
  4295. docHeight = Math.max(docEl.scrollHeight, docEl.clientHeight);
  4296. self.mask = _widget({
  4297. x : 0,
  4298. y : 0,
  4299. z : self.z - 1,
  4300. cls : 'ke-dialog-mask',
  4301. width : docWidth,
  4302. height : docHeight
  4303. });
  4304. }
  4305. self.autoPos(self.div.width(), self.div.height());
  4306. self.footerDiv = footerDiv;
  4307. self.bodyDiv = bodyDiv;
  4308. self.headerDiv = headerDiv;
  4309. self.isLoading = false;
  4310. },
  4311. setMaskIndex : function(z) {
  4312. var self = this;
  4313. self.mask.div.css('z-index', z);
  4314. },
  4315. showLoading : function(msg) {
  4316. msg = _undef(msg, '');
  4317. var self = this, body = self.bodyDiv;
  4318. self.loading = K('<div class="ke-dialog-loading"><div class="ke-inline-block ke-dialog-loading-content" style="margin-top:' + Math.round(body.height() / 3) + 'px;">' + msg + '</div></div>')
  4319. .width(body.width()).height(body.height())
  4320. .css('top', self.headerDiv.height() + 'px');
  4321. body.css('visibility', 'hidden').after(self.loading);
  4322. self.isLoading = true;
  4323. return self;
  4324. },
  4325. hideLoading : function() {
  4326. this.loading && this.loading.remove();
  4327. this.bodyDiv.css('visibility', 'visible');
  4328. this.isLoading = false;
  4329. return this;
  4330. },
  4331. remove : function() {
  4332. var self = this;
  4333. if (self.options.beforeRemove) {
  4334. self.options.beforeRemove.call(self);
  4335. }
  4336. self.mask && self.mask.remove();
  4337. self.iframeMask && self.iframeMask.remove();
  4338. self.closeIcon.unbind();
  4339. K('input', self.div).unbind();
  4340. K('button', self.div).unbind();
  4341. self.footerDiv.unbind();
  4342. self.bodyDiv.unbind();
  4343. self.headerDiv.unbind();
  4344. K('iframe', self.div).each(function() {
  4345. K(this).remove();
  4346. });
  4347. KDialog.parent.remove.call(self);
  4348. return self;
  4349. }
  4350. });
  4351. function _dialog(options) {
  4352. return new KDialog(options);
  4353. }
  4354. K.DialogClass = KDialog;
  4355. K.dialog = _dialog;
  4356. function _tabs(options) {
  4357. var self = _widget(options),
  4358. remove = self.remove,
  4359. afterSelect = options.afterSelect,
  4360. div = self.div,
  4361. liList = [];
  4362. div.addClass('ke-tabs')
  4363. .bind('contextmenu,mousedown,mousemove', function(e) {
  4364. e.preventDefault();
  4365. });
  4366. var ul = K('<ul class="ke-tabs-ul ke-clearfix"></ul>');
  4367. div.append(ul);
  4368. self.add = function(tab) {
  4369. var li = K('<li class="ke-tabs-li">' + tab.title + '</li>');
  4370. li.data('tab', tab);
  4371. liList.push(li);
  4372. ul.append(li);
  4373. };
  4374. self.selectedIndex = 0;
  4375. self.select = function(index) {
  4376. self.selectedIndex = index;
  4377. _each(liList, function(i, li) {
  4378. li.unbind();
  4379. if (i === index) {
  4380. li.addClass('ke-tabs-li-selected');
  4381. K(li.data('tab').panel).show('');
  4382. } else {
  4383. li.removeClass('ke-tabs-li-selected').removeClass('ke-tabs-li-on')
  4384. .mouseover(function() {
  4385. K(this).addClass('ke-tabs-li-on');
  4386. })
  4387. .mouseout(function() {
  4388. K(this).removeClass('ke-tabs-li-on');
  4389. })
  4390. .click(function() {
  4391. self.select(i);
  4392. });
  4393. K(li.data('tab').panel).hide();
  4394. }
  4395. });
  4396. if (afterSelect) {
  4397. afterSelect.call(self, index);
  4398. }
  4399. };
  4400. self.remove = function() {
  4401. _each(liList, function() {
  4402. this.remove();
  4403. });
  4404. ul.remove();
  4405. remove.call(self);
  4406. };
  4407. return self;
  4408. }
  4409. K.tabs = _tabs;
  4410. function _loadScript(url, fn) {
  4411. var head = document.getElementsByTagName('head')[0] || (_QUIRKS ? document.body : document.documentElement),
  4412. script = document.createElement('script');
  4413. head.appendChild(script);
  4414. script.src = url;
  4415. script.charset = 'utf-8';
  4416. script.onload = script.onreadystatechange = function() {
  4417. if (!this.readyState || this.readyState === 'loaded') {
  4418. if (fn) {
  4419. fn();
  4420. }
  4421. script.onload = script.onreadystatechange = null;
  4422. head.removeChild(script);
  4423. }
  4424. };
  4425. }
  4426. function _chopQuery(url) {
  4427. var index = url.indexOf('?');
  4428. return index > 0 ? url.substr(0, index) : url;
  4429. }
  4430. function _loadStyle(url) {
  4431. var head = document.getElementsByTagName('head')[0] || (_QUIRKS ? document.body : document.documentElement),
  4432. link = document.createElement('link'),
  4433. absoluteUrl = _chopQuery(_formatUrl(url, 'absolute'));
  4434. var links = K('link[rel="stylesheet"]', head);
  4435. for (var i = 0, len = links.length; i < len; i++) {
  4436. if (_chopQuery(_formatUrl(links[i].href, 'absolute')) === absoluteUrl) {
  4437. return;
  4438. }
  4439. }
  4440. head.appendChild(link);
  4441. link.href = url;
  4442. link.rel = 'stylesheet';
  4443. }
  4444. function _ajax(url, fn, method, param, dataType) {
  4445. method = method || 'GET';
  4446. dataType = dataType || 'json';
  4447. var xhr = window.XMLHttpRequest ? new window.XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
  4448. xhr.open(method, url, true);
  4449. xhr.onreadystatechange = function () {
  4450. if (xhr.readyState == 4 && xhr.status == 200) {
  4451. if (fn) {
  4452. var data = _trim(xhr.responseText);
  4453. if (dataType == 'json') {
  4454. data = _json(data);
  4455. }
  4456. fn(data);
  4457. }
  4458. }
  4459. };
  4460. if (method == 'POST') {
  4461. var params = [];
  4462. _each(param, function(key, val) {
  4463. params.push(encodeURIComponent(key) + '=' + encodeURIComponent(val));
  4464. });
  4465. try {
  4466. xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  4467. } catch (e) {}
  4468. xhr.send(params.join('&'));
  4469. } else {
  4470. xhr.send(null);
  4471. }
  4472. }
  4473. K.loadScript = _loadScript;
  4474. K.loadStyle = _loadStyle;
  4475. K.ajax = _ajax;
  4476. var _plugins = {};
  4477. function _plugin(name, fn) {
  4478. if (name === undefined) {
  4479. return _plugins;
  4480. }
  4481. if (!fn) {
  4482. return _plugins[name];
  4483. }
  4484. _plugins[name] = fn;
  4485. }
  4486. var _language = {};
  4487. function _parseLangKey(key) {
  4488. var match, ns = 'core';
  4489. if ((match = /^(\w+)\.(\w+)$/.exec(key))) {
  4490. ns = match[1];
  4491. key = match[2];
  4492. }
  4493. return { ns : ns, key : key };
  4494. }
  4495. function _lang(mixed, langType) {
  4496. langType = langType === undefined ? K.options.langType : langType;
  4497. if (typeof mixed === 'string') {
  4498. if (!_language[langType]) {
  4499. return 'no language';
  4500. }
  4501. var pos = mixed.length - 1;
  4502. if (mixed.substr(pos) === '.') {
  4503. return _language[langType][mixed.substr(0, pos)];
  4504. }
  4505. var obj = _parseLangKey(mixed);
  4506. return _language[langType][obj.ns][obj.key];
  4507. }
  4508. _each(mixed, function(key, val) {
  4509. var obj = _parseLangKey(key);
  4510. if (!_language[langType]) {
  4511. _language[langType] = {};
  4512. }
  4513. if (!_language[langType][obj.ns]) {
  4514. _language[langType][obj.ns] = {};
  4515. }
  4516. _language[langType][obj.ns][obj.key] = val;
  4517. });
  4518. }
  4519. function _getImageFromRange(range, fn) {
  4520. if (range.collapsed) {
  4521. return;
  4522. }
  4523. range = range.cloneRange().up();
  4524. var sc = range.startContainer, so = range.startOffset;
  4525. if (!_WEBKIT && !range.isControl()) {
  4526. return;
  4527. }
  4528. var img = K(sc.childNodes[so]);
  4529. if (!img || img.name != 'img') {
  4530. return;
  4531. }
  4532. if (fn(img)) {
  4533. return img;
  4534. }
  4535. }
  4536. function _bindContextmenuEvent() {
  4537. var self = this, doc = self.edit.doc;
  4538. K(doc).contextmenu(function(e) {
  4539. if (self.menu) {
  4540. self.hideMenu();
  4541. }
  4542. if (!self.useContextmenu) {
  4543. e.preventDefault();
  4544. return;
  4545. }
  4546. if (self._contextmenus.length === 0) {
  4547. return;
  4548. }
  4549. var maxWidth = 0, items = [];
  4550. _each(self._contextmenus, function() {
  4551. if (this.title == '-') {
  4552. items.push(this);
  4553. return;
  4554. }
  4555. if (this.cond && this.cond()) {
  4556. items.push(this);
  4557. if (this.width && this.width > maxWidth) {
  4558. maxWidth = this.width;
  4559. }
  4560. }
  4561. });
  4562. while (items.length > 0 && items[0].title == '-') {
  4563. items.shift();
  4564. }
  4565. while (items.length > 0 && items[items.length - 1].title == '-') {
  4566. items.pop();
  4567. }
  4568. var prevItem = null;
  4569. _each(items, function(i) {
  4570. if (this.title == '-' && prevItem.title == '-') {
  4571. delete items[i];
  4572. }
  4573. prevItem = this;
  4574. });
  4575. if (items.length > 0) {
  4576. e.preventDefault();
  4577. var pos = K(self.edit.iframe).pos(),
  4578. menu = _menu({
  4579. x : pos.x + e.clientX,
  4580. y : pos.y + e.clientY,
  4581. width : maxWidth,
  4582. css : { visibility: 'hidden' },
  4583. shadowMode : self.shadowMode
  4584. });
  4585. _each(items, function() {
  4586. if (this.title) {
  4587. menu.addItem(this);
  4588. }
  4589. });
  4590. var docEl = _docElement(menu.doc),
  4591. menuHeight = menu.div.height();
  4592. if (e.clientY + menuHeight >= docEl.clientHeight - 100) {
  4593. menu.pos(menu.x, _removeUnit(menu.y) - menuHeight);
  4594. }
  4595. menu.div.css('visibility', 'visible');
  4596. self.menu = menu;
  4597. }
  4598. });
  4599. }
  4600. function _bindNewlineEvent() {
  4601. var self = this, doc = self.edit.doc, newlineTag = self.newlineTag;
  4602. if (_IE && newlineTag !== 'br') {
  4603. return;
  4604. }
  4605. if (_GECKO && _V < 3 && newlineTag !== 'p') {
  4606. return;
  4607. }
  4608. if (_OPERA && _V < 9) {
  4609. return;
  4610. }
  4611. var brSkipTagMap = _toMap('h1,h2,h3,h4,h5,h6,pre,li'),
  4612. pSkipTagMap = _toMap('p,h1,h2,h3,h4,h5,h6,pre,li,blockquote');
  4613. function getAncestorTagName(range) {
  4614. var ancestor = K(range.commonAncestor());
  4615. while (ancestor) {
  4616. if (ancestor.type == 1 && !ancestor.isStyle()) {
  4617. break;
  4618. }
  4619. ancestor = ancestor.parent();
  4620. }
  4621. return ancestor.name;
  4622. }
  4623. K(doc).keydown(function(e) {
  4624. if (e.which != 13 || e.shiftKey || e.ctrlKey || e.altKey) {
  4625. return;
  4626. }
  4627. self.cmd.selection();
  4628. var tagName = getAncestorTagName(self.cmd.range);
  4629. if (tagName == 'marquee' || tagName == 'select') {
  4630. return;
  4631. }
  4632. if (newlineTag === 'br' && !brSkipTagMap[tagName]) {
  4633. e.preventDefault();
  4634. self.insertHtml('<br />' + (_IE && _V < 9 ? '' : '\u200B'));
  4635. return;
  4636. }
  4637. if (!pSkipTagMap[tagName]) {
  4638. _nativeCommand(doc, 'formatblock', '<p>');
  4639. }
  4640. });
  4641. K(doc).keyup(function(e) {
  4642. if (e.which != 13 || e.shiftKey || e.ctrlKey || e.altKey) {
  4643. return;
  4644. }
  4645. if (newlineTag == 'br') {
  4646. return;
  4647. }
  4648. if (_GECKO) {
  4649. var root = self.cmd.commonAncestor('p');
  4650. var a = self.cmd.commonAncestor('a');
  4651. if (a && a.text() == '') {
  4652. a.remove(true);
  4653. self.cmd.range.selectNodeContents(root[0]).collapse(true);
  4654. self.cmd.select();
  4655. }
  4656. return;
  4657. }
  4658. self.cmd.selection();
  4659. var tagName = getAncestorTagName(self.cmd.range);
  4660. if (tagName == 'marquee' || tagName == 'select') {
  4661. return;
  4662. }
  4663. if (!pSkipTagMap[tagName]) {
  4664. _nativeCommand(doc, 'formatblock', '<p>');
  4665. }
  4666. var div = self.cmd.commonAncestor('div');
  4667. if (div) {
  4668. var p = K('<p></p>'),
  4669. child = div[0].firstChild;
  4670. while (child) {
  4671. var next = child.nextSibling;
  4672. p.append(child);
  4673. child = next;
  4674. }
  4675. div.before(p);
  4676. div.remove();
  4677. self.cmd.range.selectNodeContents(p[0]);
  4678. self.cmd.select();
  4679. }
  4680. });
  4681. }
  4682. function _bindTabEvent() {
  4683. var self = this, doc = self.edit.doc;
  4684. K(doc).keydown(function(e) {
  4685. if (e.which == 9) {
  4686. e.preventDefault();
  4687. if (self.afterTab) {
  4688. self.afterTab.call(self, e);
  4689. return;
  4690. }
  4691. var cmd = self.cmd, range = cmd.range;
  4692. range.shrink();
  4693. if (range.collapsed && range.startContainer.nodeType == 1) {
  4694. range.insertNode(K('@&nbsp;', doc)[0]);
  4695. cmd.select();
  4696. }
  4697. self.insertHtml('&nbsp;&nbsp;&nbsp;&nbsp;');
  4698. }
  4699. });
  4700. }
  4701. function _bindFocusEvent() {
  4702. var self = this;
  4703. K(self.edit.textarea[0], self.edit.win).focus(function(e) {
  4704. if (self.afterFocus) {
  4705. self.afterFocus.call(self, e);
  4706. }
  4707. }).blur(function(e) {
  4708. if (self.afterBlur) {
  4709. self.afterBlur.call(self, e);
  4710. }
  4711. });
  4712. }
  4713. function _removeBookmarkTag(html) {
  4714. return _trim(html.replace(/<span [^>]*id="?__kindeditor_bookmark_\w+_\d+__"?[^>]*><\/span>/ig, ''));
  4715. }
  4716. function _removeTempTag(html) {
  4717. return html.replace(/<div[^>]+class="?__kindeditor_paste__"?[^>]*>[\s\S]*?<\/div>/ig, '');
  4718. }
  4719. function _addBookmarkToStack(stack, bookmark) {
  4720. if (stack.length === 0) {
  4721. stack.push(bookmark);
  4722. return;
  4723. }
  4724. var prev = stack[stack.length - 1];
  4725. if (_removeBookmarkTag(bookmark.html) !== _removeBookmarkTag(prev.html)) {
  4726. stack.push(bookmark);
  4727. }
  4728. }
  4729. function _undoToRedo(fromStack, toStack) {
  4730. var self = this, edit = self.edit,
  4731. body = edit.doc.body,
  4732. range, bookmark;
  4733. if (fromStack.length === 0) {
  4734. return self;
  4735. }
  4736. if (edit.designMode) {
  4737. range = self.cmd.range;
  4738. bookmark = range.createBookmark(true);
  4739. bookmark.html = body.innerHTML;
  4740. } else {
  4741. bookmark = {
  4742. html : body.innerHTML
  4743. };
  4744. }
  4745. _addBookmarkToStack(toStack, bookmark);
  4746. var prev = fromStack.pop();
  4747. if (_removeBookmarkTag(bookmark.html) === _removeBookmarkTag(prev.html) && fromStack.length > 0) {
  4748. prev = fromStack.pop();
  4749. }
  4750. if (edit.designMode) {
  4751. edit.html(prev.html);
  4752. if (prev.start) {
  4753. range.moveToBookmark(prev);
  4754. self.select();
  4755. }
  4756. } else {
  4757. K(body).html(_removeBookmarkTag(prev.html));
  4758. }
  4759. return self;
  4760. }
  4761. function KEditor(options) {
  4762. var self = this;
  4763. self.options = {};
  4764. function setOption(key, val) {
  4765. if (KEditor.prototype[key] === undefined) {
  4766. self[key] = val;
  4767. }
  4768. self.options[key] = val;
  4769. }
  4770. _each(options, function(key, val) {
  4771. setOption(key, options[key]);
  4772. });
  4773. _each(K.options, function(key, val) {
  4774. if (self[key] === undefined) {
  4775. setOption(key, val);
  4776. }
  4777. });
  4778. var se = K(self.srcElement || '<textarea/>');
  4779. if (!self.width) {
  4780. self.width = se[0].style.width || se.width();
  4781. }
  4782. if (!self.height) {
  4783. self.height = se[0].style.height || se.height();
  4784. }
  4785. setOption('width', _undef(self.width, self.minWidth));
  4786. setOption('height', _undef(self.height, self.minHeight));
  4787. setOption('width', _addUnit(self.width));
  4788. setOption('height', _addUnit(self.height));
  4789. if (_MOBILE && (!_IOS || _V < 534)) {
  4790. self.designMode = false;
  4791. }
  4792. self.srcElement = se;
  4793. self.initContent = '';
  4794. self.plugin = {};
  4795. self.isCreated = false;
  4796. self._handlers = {};
  4797. self._contextmenus = [];
  4798. self._undoStack = [];
  4799. self._redoStack = [];
  4800. self._firstAddBookmark = true;
  4801. self.menu = self.contextmenu = null;
  4802. self.dialogs = [];
  4803. }
  4804. KEditor.prototype = {
  4805. lang : function(mixed) {
  4806. return _lang(mixed, this.langType);
  4807. },
  4808. loadPlugin : function(name, fn) {
  4809. var self = this;
  4810. if (_plugins[name]) {
  4811. if (!_isFunction(_plugins[name])) {
  4812. setTimeout(function() {
  4813. self.loadPlugin(name, fn);
  4814. }, 100);
  4815. return self;
  4816. }
  4817. _plugins[name].call(self, KindEditor);
  4818. if (fn) {
  4819. fn.call(self);
  4820. }
  4821. return self;
  4822. }
  4823. _plugins[name] = 'loading';
  4824. _loadScript(self.pluginsPath + name + '/' + name + '.js?ver=' + encodeURIComponent(K.DEBUG ? _TIME : _VERSION), function() {
  4825. setTimeout(function() {
  4826. if (_plugins[name]) {
  4827. self.loadPlugin(name, fn);
  4828. }
  4829. }, 0);
  4830. });
  4831. return self;
  4832. },
  4833. handler : function(key, fn) {
  4834. var self = this;
  4835. if (!self._handlers[key]) {
  4836. self._handlers[key] = [];
  4837. }
  4838. if (_isFunction(fn)) {
  4839. self._handlers[key].push(fn);
  4840. return self;
  4841. }
  4842. _each(self._handlers[key], function() {
  4843. fn = this.call(self, fn);
  4844. });
  4845. return fn;
  4846. },
  4847. clickToolbar : function(name, fn) {
  4848. var self = this, key = 'clickToolbar' + name;
  4849. if (fn === undefined) {
  4850. if (self._handlers[key]) {
  4851. return self.handler(key);
  4852. }
  4853. self.loadPlugin(name, function() {
  4854. self.handler(key);
  4855. });
  4856. return self;
  4857. }
  4858. return self.handler(key, fn);
  4859. },
  4860. updateState : function() {
  4861. var self = this;
  4862. _each(('justifyleft,justifycenter,justifyright,justifyfull,insertorderedlist,insertunorderedlist,' +
  4863. 'subscript,superscript,bold,italic,underline,strikethrough').split(','), function(i, name) {
  4864. self.cmd.state(name) ? self.toolbar.select(name) : self.toolbar.unselect(name);
  4865. });
  4866. return self;
  4867. },
  4868. addContextmenu : function(item) {
  4869. this._contextmenus.push(item);
  4870. return this;
  4871. },
  4872. afterCreate : function(fn) {
  4873. return this.handler('afterCreate', fn);
  4874. },
  4875. beforeRemove : function(fn) {
  4876. return this.handler('beforeRemove', fn);
  4877. },
  4878. beforeGetHtml : function(fn) {
  4879. return this.handler('beforeGetHtml', fn);
  4880. },
  4881. beforeSetHtml : function(fn) {
  4882. return this.handler('beforeSetHtml', fn);
  4883. },
  4884. afterSetHtml : function(fn) {
  4885. return this.handler('afterSetHtml', fn);
  4886. },
  4887. create : function() {
  4888. var self = this, fullscreenMode = self.fullscreenMode;
  4889. if (self.isCreated) {
  4890. return self;
  4891. }
  4892. if (self.srcElement.data('kindeditor')) {
  4893. return self;
  4894. }
  4895. self.srcElement.data('kindeditor', 'true');
  4896. if (fullscreenMode) {
  4897. _docElement().style.overflow = 'hidden';
  4898. } else {
  4899. _docElement().style.overflow = '';
  4900. }
  4901. var width = fullscreenMode ? _docElement().clientWidth + 'px' : self.width,
  4902. height = fullscreenMode ? _docElement().clientHeight + 'px' : self.height;
  4903. if ((_IE && _V < 8) || _QUIRKS) {
  4904. height = _addUnit(_removeUnit(height) + 2);
  4905. }
  4906. var container = self.container = K(self.layout);
  4907. if (fullscreenMode) {
  4908. K(document.body).append(container);
  4909. } else {
  4910. self.srcElement.before(container);
  4911. }
  4912. var toolbarDiv = K('.toolbar', container),
  4913. editDiv = K('.edit', container),
  4914. statusbar = self.statusbar = K('.statusbar', container);
  4915. container.removeClass('container')
  4916. .addClass('ke-container ke-container-' + self.themeType).css('width', width);
  4917. if (fullscreenMode) {
  4918. container.css({
  4919. position : 'absolute',
  4920. left : 0,
  4921. top : 0,
  4922. 'z-index' : 811211
  4923. });
  4924. if (!_GECKO) {
  4925. self._scrollPos = _getScrollPos();
  4926. }
  4927. window.scrollTo(0, 0);
  4928. K(document.body).css({
  4929. 'height' : '1px',
  4930. 'overflow' : 'hidden'
  4931. });
  4932. K(document.body.parentNode).css('overflow', 'hidden');
  4933. self._fullscreenExecuted = true;
  4934. } else {
  4935. if (self._fullscreenExecuted) {
  4936. K(document.body).css({
  4937. 'height' : '',
  4938. 'overflow' : ''
  4939. });
  4940. K(document.body.parentNode).css('overflow', '');
  4941. }
  4942. if (self._scrollPos) {
  4943. window.scrollTo(self._scrollPos.x, self._scrollPos.y);
  4944. }
  4945. }
  4946. var htmlList = [];
  4947. K.each(self.items, function(i, name) {
  4948. if (name == '|') {
  4949. htmlList.push('<span class="ke-inline-block ke-separator"></span>');
  4950. } else if (name == '/') {
  4951. htmlList.push('<div class="ke-hr"></div>');
  4952. } else {
  4953. htmlList.push('<span class="ke-outline" data-name="' + name + '" title="' + self.lang(name) + '" unselectable="on">');
  4954. htmlList.push('<span class="ke-toolbar-icon ke-toolbar-icon-url ke-icon-' + name + '" unselectable="on"></span></span>');
  4955. }
  4956. });
  4957. var toolbar = self.toolbar = _toolbar({
  4958. src : toolbarDiv,
  4959. html : htmlList.join(''),
  4960. noDisableItems : self.noDisableItems,
  4961. click : function(e, name) {
  4962. e.stop();
  4963. if (self.menu) {
  4964. var menuName = self.menu.name;
  4965. self.hideMenu();
  4966. if (menuName === name) {
  4967. return;
  4968. }
  4969. }
  4970. self.clickToolbar(name);
  4971. }
  4972. });
  4973. var editHeight = _removeUnit(height) - toolbar.div.height();
  4974. var edit = self.edit = _edit({
  4975. height : editHeight > 0 && _removeUnit(height) > self.minHeight ? editHeight : self.minHeight,
  4976. src : editDiv,
  4977. srcElement : self.srcElement,
  4978. designMode : self.designMode,
  4979. themesPath : self.themesPath,
  4980. bodyClass : self.bodyClass,
  4981. cssPath : self.cssPath,
  4982. cssData : self.cssData,
  4983. beforeGetHtml : function(html) {
  4984. html = self.beforeGetHtml(html);
  4985. html = _removeBookmarkTag(_removeTempTag(html));
  4986. return _formatHtml(html, self.filterMode ? self.htmlTags : null, self.urlType, self.wellFormatMode, self.indentChar);
  4987. },
  4988. beforeSetHtml : function(html) {
  4989. html = _formatHtml(html, self.filterMode ? self.htmlTags : null, '', false);
  4990. return self.beforeSetHtml(html);
  4991. },
  4992. afterSetHtml : function() {
  4993. self.edit = edit = this;
  4994. self.afterSetHtml();
  4995. },
  4996. afterCreate : function() {
  4997. self.edit = edit = this;
  4998. self.cmd = edit.cmd;
  4999. self._docMousedownFn = function(e) {
  5000. if (self.menu) {
  5001. self.hideMenu();
  5002. }
  5003. };
  5004. K(edit.doc, document).mousedown(self._docMousedownFn);
  5005. _bindContextmenuEvent.call(self);
  5006. _bindNewlineEvent.call(self);
  5007. _bindTabEvent.call(self);
  5008. _bindFocusEvent.call(self);
  5009. edit.afterChange(function(e) {
  5010. if (!edit.designMode) {
  5011. return;
  5012. }
  5013. self.updateState();
  5014. self.addBookmark();
  5015. if (self.options.afterChange) {
  5016. self.options.afterChange.call(self);
  5017. }
  5018. });
  5019. edit.textarea.keyup(function(e) {
  5020. if (!e.ctrlKey && !e.altKey && _INPUT_KEY_MAP[e.which]) {
  5021. if (self.options.afterChange) {
  5022. self.options.afterChange.call(self);
  5023. }
  5024. }
  5025. });
  5026. if (self.readonlyMode) {
  5027. self.readonly();
  5028. }
  5029. self.isCreated = true;
  5030. if (self.initContent === '') {
  5031. self.initContent = self.html();
  5032. }
  5033. if (self._undoStack.length > 0) {
  5034. var prev = self._undoStack.pop();
  5035. if (prev.start) {
  5036. self.html(prev.html);
  5037. edit.cmd.range.moveToBookmark(prev);
  5038. self.select();
  5039. }
  5040. }
  5041. self.afterCreate();
  5042. if (self.options.afterCreate) {
  5043. self.options.afterCreate.call(self);
  5044. }
  5045. }
  5046. });
  5047. statusbar.removeClass('statusbar').addClass('ke-statusbar')
  5048. .append('<span class="ke-inline-block ke-statusbar-center-icon"></span>')
  5049. .append('<span class="ke-inline-block ke-statusbar-right-icon"></span>');
  5050. if (self._fullscreenResizeHandler) {
  5051. K(window).unbind('resize', self._fullscreenResizeHandler);
  5052. self._fullscreenResizeHandler = null;
  5053. }
  5054. function initResize() {
  5055. if (statusbar.height() === 0) {
  5056. setTimeout(initResize, 100);
  5057. return;
  5058. }
  5059. self.resize(width, height, false);
  5060. }
  5061. initResize();
  5062. if (fullscreenMode) {
  5063. self._fullscreenResizeHandler = function(e) {
  5064. if (self.isCreated) {
  5065. self.resize(_docElement().clientWidth, _docElement().clientHeight, false);
  5066. }
  5067. };
  5068. K(window).bind('resize', self._fullscreenResizeHandler);
  5069. toolbar.select('fullscreen');
  5070. statusbar.first().css('visibility', 'hidden');
  5071. statusbar.last().css('visibility', 'hidden');
  5072. } else {
  5073. if (_GECKO) {
  5074. K(window).bind('scroll', function(e) {
  5075. self._scrollPos = _getScrollPos();
  5076. });
  5077. }
  5078. if (self.resizeType > 0) {
  5079. _drag({
  5080. moveEl : container,
  5081. clickEl : statusbar,
  5082. moveFn : function(x, y, width, height, diffX, diffY) {
  5083. height += diffY;
  5084. self.resize(null, height);
  5085. }
  5086. });
  5087. } else {
  5088. statusbar.first().css('visibility', 'hidden');
  5089. }
  5090. if (self.resizeType === 2) {
  5091. _drag({
  5092. moveEl : container,
  5093. clickEl : statusbar.last(),
  5094. moveFn : function(x, y, width, height, diffX, diffY) {
  5095. width += diffX;
  5096. height += diffY;
  5097. self.resize(width, height);
  5098. }
  5099. });
  5100. } else {
  5101. statusbar.last().css('visibility', 'hidden');
  5102. }
  5103. }
  5104. return self;
  5105. },
  5106. remove : function() {
  5107. var self = this;
  5108. if (!self.isCreated) {
  5109. return self;
  5110. }
  5111. self.beforeRemove();
  5112. self.srcElement.data('kindeditor', '');
  5113. if (self.menu) {
  5114. self.hideMenu();
  5115. }
  5116. _each(self.dialogs, function() {
  5117. self.hideDialog();
  5118. });
  5119. K(document).unbind('mousedown', self._docMousedownFn);
  5120. self.toolbar.remove();
  5121. self.edit.remove();
  5122. self.statusbar.last().unbind();
  5123. self.statusbar.unbind();
  5124. self.container.remove();
  5125. self.container = self.toolbar = self.edit = self.menu = null;
  5126. self.dialogs = [];
  5127. self.isCreated = false;
  5128. return self;
  5129. },
  5130. resize : function(width, height, updateProp) {
  5131. var self = this;
  5132. updateProp = _undef(updateProp, true);
  5133. if (width) {
  5134. if (!/%/.test(width)) {
  5135. width = _removeUnit(width);
  5136. width = width < self.minWidth ? self.minWidth : width;
  5137. }
  5138. self.container.css('width', _addUnit(width));
  5139. if (updateProp) {
  5140. self.width = _addUnit(width);
  5141. }
  5142. }
  5143. if (height) {
  5144. height = _removeUnit(height);
  5145. editHeight = _removeUnit(height) - self.toolbar.div.height() - self.statusbar.height();
  5146. editHeight = editHeight < self.minHeight ? self.minHeight : editHeight;
  5147. self.edit.setHeight(editHeight);
  5148. if (updateProp) {
  5149. self.height = _addUnit(height);
  5150. }
  5151. }
  5152. return self;
  5153. },
  5154. select : function() {
  5155. this.isCreated && this.cmd.select();
  5156. return this;
  5157. },
  5158. html : function(val) {
  5159. var self = this;
  5160. if (val === undefined) {
  5161. return self.isCreated ? self.edit.html() : _elementVal(self.srcElement);
  5162. }
  5163. self.isCreated ? self.edit.html(val) : _elementVal(self.srcElement, val);
  5164. if (self.isCreated) {
  5165. self.cmd.selection();
  5166. }
  5167. return self;
  5168. },
  5169. fullHtml : function() {
  5170. return this.isCreated ? this.edit.html(undefined, true) : '';
  5171. },
  5172. text : function(val) {
  5173. var self = this;
  5174. if (val === undefined) {
  5175. return _trim(self.html().replace(/<(?!img|embed).*?>/ig, '').replace(/&nbsp;/ig, ' '));
  5176. } else {
  5177. return self.html(_escape(val));
  5178. }
  5179. },
  5180. isEmpty : function() {
  5181. return _trim(this.text().replace(/\r\n|\n|\r/, '')) === '';
  5182. },
  5183. isDirty : function() {
  5184. return _trim(this.initContent.replace(/\r\n|\n|\r|t/g, '')) !== _trim(this.html().replace(/\r\n|\n|\r|t/g, ''));
  5185. },
  5186. selectedHtml : function() {
  5187. var val = this.isCreated ? this.cmd.range.html() : '';
  5188. val = _removeBookmarkTag(_removeTempTag(val));
  5189. return val;
  5190. },
  5191. count : function(mode) {
  5192. var self = this;
  5193. mode = (mode || 'html').toLowerCase();
  5194. if (mode === 'html') {
  5195. return self.html().length;
  5196. }
  5197. if (mode === 'text') {
  5198. return self.text().replace(/<(?:img|embed).*?>/ig, 'K').replace(/\r\n|\n|\r/g, '').length;
  5199. }
  5200. return 0;
  5201. },
  5202. exec : function(key) {
  5203. key = key.toLowerCase();
  5204. var self = this, cmd = self.cmd,
  5205. changeFlag = _inArray(key, 'selectall,copy,paste,print'.split(',')) < 0;
  5206. if (changeFlag) {
  5207. self.addBookmark(false);
  5208. }
  5209. cmd[key].apply(cmd, _toArray(arguments, 1));
  5210. if (changeFlag) {
  5211. self.updateState();
  5212. self.addBookmark(false);
  5213. if (self.options.afterChange) {
  5214. self.options.afterChange.call(self);
  5215. }
  5216. }
  5217. return self;
  5218. },
  5219. insertHtml : function(val, quickMode) {
  5220. if (!this.isCreated) {
  5221. return this;
  5222. }
  5223. val = this.beforeSetHtml(val);
  5224. this.exec('inserthtml', val, quickMode);
  5225. return this;
  5226. },
  5227. appendHtml : function(val) {
  5228. this.html(this.html() + val);
  5229. if (this.isCreated) {
  5230. var cmd = this.cmd;
  5231. cmd.range.selectNodeContents(cmd.doc.body).collapse(false);
  5232. cmd.select();
  5233. }
  5234. return this;
  5235. },
  5236. sync : function() {
  5237. _elementVal(this.srcElement, this.html());
  5238. return this;
  5239. },
  5240. focus : function() {
  5241. this.isCreated ? this.edit.focus() : this.srcElement[0].focus();
  5242. return this;
  5243. },
  5244. blur : function() {
  5245. this.isCreated ? this.edit.blur() : this.srcElement[0].blur();
  5246. return this;
  5247. },
  5248. addBookmark : function(checkSize) {
  5249. checkSize = _undef(checkSize, true);
  5250. var self = this, edit = self.edit,
  5251. body = edit.doc.body,
  5252. html = _removeTempTag(body.innerHTML), bookmark;
  5253. if (checkSize && self._undoStack.length > 0) {
  5254. var prev = self._undoStack[self._undoStack.length - 1];
  5255. if (Math.abs(html.length - _removeBookmarkTag(prev.html).length) < self.minChangeSize) {
  5256. return self;
  5257. }
  5258. }
  5259. if (edit.designMode && !self._firstAddBookmark) {
  5260. var range = self.cmd.range;
  5261. bookmark = range.createBookmark(true);
  5262. bookmark.html = _removeTempTag(body.innerHTML);
  5263. range.moveToBookmark(bookmark);
  5264. } else {
  5265. bookmark = {
  5266. html : html
  5267. };
  5268. }
  5269. self._firstAddBookmark = false;
  5270. _addBookmarkToStack(self._undoStack, bookmark);
  5271. return self;
  5272. },
  5273. undo : function() {
  5274. return _undoToRedo.call(this, this._undoStack, this._redoStack);
  5275. },
  5276. redo : function() {
  5277. return _undoToRedo.call(this, this._redoStack, this._undoStack);
  5278. },
  5279. fullscreen : function(bool) {
  5280. this.fullscreenMode = (bool === undefined ? !this.fullscreenMode : bool);
  5281. this.addBookmark(false);
  5282. return this.remove().create();
  5283. },
  5284. readonly : function(isReadonly) {
  5285. isReadonly = _undef(isReadonly, true);
  5286. var self = this, edit = self.edit, doc = edit.doc;
  5287. if (self.designMode) {
  5288. self.toolbar.disableAll(isReadonly, []);
  5289. } else {
  5290. _each(self.noDisableItems, function() {
  5291. self.toolbar[isReadonly ? 'disable' : 'enable'](this);
  5292. });
  5293. }
  5294. if (_IE) {
  5295. doc.body.contentEditable = !isReadonly;
  5296. } else {
  5297. doc.designMode = isReadonly ? 'off' : 'on';
  5298. }
  5299. edit.textarea[0].disabled = isReadonly;
  5300. },
  5301. createMenu : function(options) {
  5302. var self = this,
  5303. name = options.name,
  5304. knode = self.toolbar.get(name),
  5305. pos = knode.pos();
  5306. options.x = pos.x;
  5307. options.y = pos.y + knode.height();
  5308. options.z = self.options.zIndex;
  5309. options.shadowMode = _undef(options.shadowMode, self.shadowMode);
  5310. if (options.selectedColor !== undefined) {
  5311. options.cls = 'ke-colorpicker-' + self.themeType;
  5312. options.noColor = self.lang('noColor');
  5313. self.menu = _colorpicker(options);
  5314. } else {
  5315. options.cls = 'ke-menu-' + self.themeType;
  5316. options.centerLineMode = false;
  5317. self.menu = _menu(options);
  5318. }
  5319. return self.menu;
  5320. },
  5321. hideMenu : function() {
  5322. this.menu.remove();
  5323. this.menu = null;
  5324. return this;
  5325. },
  5326. hideContextmenu : function() {
  5327. this.contextmenu.remove();
  5328. this.contextmenu = null;
  5329. return this;
  5330. },
  5331. createDialog : function(options) {
  5332. var self = this, name = options.name;
  5333. options.z = self.options.zIndex;
  5334. options.shadowMode = _undef(options.shadowMode, self.shadowMode);
  5335. options.closeBtn = _undef(options.closeBtn, {
  5336. name : self.lang('close'),
  5337. click : function(e) {
  5338. self.hideDialog();
  5339. if (_IE && self.cmd) {
  5340. self.cmd.select();
  5341. }
  5342. }
  5343. });
  5344. options.noBtn = _undef(options.noBtn, {
  5345. name : self.lang(options.yesBtn ? 'no' : 'close'),
  5346. click : function(e) {
  5347. self.hideDialog();
  5348. if (_IE && self.cmd) {
  5349. self.cmd.select();
  5350. }
  5351. }
  5352. });
  5353. if (self.dialogAlignType != 'page') {
  5354. options.alignEl = self.container;
  5355. }
  5356. options.cls = 'ke-dialog-' + self.themeType;
  5357. if (self.dialogs.length > 0) {
  5358. var firstDialog = self.dialogs[0],
  5359. parentDialog = self.dialogs[self.dialogs.length - 1];
  5360. firstDialog.setMaskIndex(parentDialog.z + 2);
  5361. options.z = parentDialog.z + 3;
  5362. options.showMask = false;
  5363. }
  5364. var dialog = _dialog(options);
  5365. self.dialogs.push(dialog);
  5366. return dialog;
  5367. },
  5368. hideDialog : function() {
  5369. var self = this;
  5370. if (self.dialogs.length > 0) {
  5371. self.dialogs.pop().remove();
  5372. }
  5373. if (self.dialogs.length > 0) {
  5374. var firstDialog = self.dialogs[0],
  5375. parentDialog = self.dialogs[self.dialogs.length - 1];
  5376. firstDialog.setMaskIndex(parentDialog.z - 1);
  5377. }
  5378. return self;
  5379. },
  5380. errorDialog : function(html) {
  5381. var self = this;
  5382. var dialog = self.createDialog({
  5383. width : 750,
  5384. title : self.lang('uploadError'),
  5385. body : '<div style="padding:10px 20px;"><iframe frameborder="0" style="width:708px;height:400px;"></iframe></div>'
  5386. });
  5387. var iframe = K('iframe', dialog.div), doc = K.iframeDoc(iframe);
  5388. doc.open();
  5389. doc.write(html);
  5390. doc.close();
  5391. K(doc.body).css('background-color', '#FFF');
  5392. iframe[0].contentWindow.focus();
  5393. return self;
  5394. }
  5395. };
  5396. function _editor(options) {
  5397. return new KEditor(options);
  5398. }
  5399. _instances = [];
  5400. function _create(expr, options) {
  5401. options = options || {};
  5402. options.basePath = _undef(options.basePath, K.basePath);
  5403. options.themesPath = _undef(options.themesPath, options.basePath + 'themes/');
  5404. options.langPath = _undef(options.langPath, options.basePath + 'lang/');
  5405. options.pluginsPath = _undef(options.pluginsPath, options.basePath + 'plugins/');
  5406. if (_undef(options.loadStyleMode, K.options.loadStyleMode)) {
  5407. var themeType = _undef(options.themeType, K.options.themeType);
  5408. _loadStyle(options.themesPath + 'default/default.css');
  5409. _loadStyle(options.themesPath + themeType + '/' + themeType + '.css');
  5410. }
  5411. function create(editor) {
  5412. _each(_plugins, function(name, fn) {
  5413. if (_isFunction(fn)) {
  5414. fn.call(editor, KindEditor);
  5415. }
  5416. });
  5417. return editor.create();
  5418. }
  5419. var knode = K(expr);
  5420. if (!knode || knode.length === 0) {
  5421. return;
  5422. }
  5423. if (knode.length > 1) {
  5424. knode.each(function() {
  5425. _create(this, options);
  5426. });
  5427. return _instances[0];
  5428. }
  5429. options.srcElement = knode[0];
  5430. var editor = new KEditor(options);
  5431. _instances.push(editor);
  5432. if (_language[editor.langType]) {
  5433. return create(editor);
  5434. }
  5435. _loadScript(editor.langPath + editor.langType + '.js?ver=' + encodeURIComponent(K.DEBUG ? _TIME : _VERSION), function() {
  5436. create(editor);
  5437. });
  5438. return editor;
  5439. }
  5440. function _eachEditor(expr, fn) {
  5441. K(expr).each(function(i, el) {
  5442. K.each(_instances, function(j, editor) {
  5443. if (editor && editor.srcElement[0] == el) {
  5444. fn.call(editor, j);
  5445. return false;
  5446. }
  5447. });
  5448. });
  5449. }
  5450. K.remove = function(expr) {
  5451. _eachEditor(expr, function(i) {
  5452. this.remove();
  5453. _instances.splice(i, 1);
  5454. });
  5455. };
  5456. K.sync = function(expr) {
  5457. _eachEditor(expr, function() {
  5458. this.sync();
  5459. });
  5460. };
  5461. K.html = function(expr, val) {
  5462. _eachEditor(expr, function() {
  5463. this.html(val);
  5464. });
  5465. };
  5466. K.insertHtml = function(expr, val) {
  5467. _eachEditor(expr, function() {
  5468. this.insertHtml(val);
  5469. });
  5470. };
  5471. K.appendHtml = function(expr, val) {
  5472. _eachEditor(expr, function() {
  5473. this.appendHtml(val);
  5474. });
  5475. };
  5476. if (_IE && _V < 7) {
  5477. _nativeCommand(document, 'BackgroundImageCache', true);
  5478. }
  5479. K.EditorClass = KEditor;
  5480. K.editor = _editor;
  5481. K.create = _create;
  5482. K.instances = _instances;
  5483. K.plugin = _plugin;
  5484. K.lang = _lang;
  5485. _plugin('core', function(K) {
  5486. var self = this,
  5487. shortcutKeys = {
  5488. undo : 'Z', redo : 'Y', bold : 'B', italic : 'I', underline : 'U', print : 'P', selectall : 'A'
  5489. };
  5490. self.afterSetHtml(function() {
  5491. if (self.options.afterChange) {
  5492. self.options.afterChange.call(self);
  5493. }
  5494. });
  5495. self.afterCreate(function() {
  5496. if (self.syncType != 'form') {
  5497. return;
  5498. }
  5499. var el = K(self.srcElement), hasForm = false;
  5500. while ((el = el.parent())) {
  5501. if (el.name == 'form') {
  5502. hasForm = true;
  5503. break;
  5504. }
  5505. }
  5506. if (hasForm) {
  5507. el.bind('submit', function(e) {
  5508. self.sync();
  5509. K(window).bind('unload', function() {
  5510. self.edit.textarea.remove();
  5511. });
  5512. });
  5513. var resetBtn = K('[type="reset"]', el);
  5514. resetBtn.click(function() {
  5515. self.html(self.initContent);
  5516. self.cmd.selection();
  5517. });
  5518. self.beforeRemove(function() {
  5519. el.unbind();
  5520. resetBtn.unbind();
  5521. });
  5522. }
  5523. });
  5524. self.clickToolbar('source', function() {
  5525. if (self.edit.designMode) {
  5526. self.toolbar.disableAll(true);
  5527. self.edit.design(false);
  5528. self.toolbar.select('source');
  5529. } else {
  5530. self.toolbar.disableAll(false);
  5531. self.edit.design(true);
  5532. self.toolbar.unselect('source');
  5533. if (_GECKO) {
  5534. setTimeout(function() {
  5535. self.cmd.selection();
  5536. }, 0);
  5537. } else {
  5538. self.cmd.selection();
  5539. }
  5540. }
  5541. self.designMode = self.edit.designMode;
  5542. });
  5543. self.afterCreate(function() {
  5544. if (!self.designMode) {
  5545. self.toolbar.disableAll(true).select('source');
  5546. }
  5547. });
  5548. self.clickToolbar('fullscreen', function() {
  5549. self.fullscreen();
  5550. });
  5551. if (self.fullscreenShortcut) {
  5552. var loaded = false;
  5553. self.afterCreate(function() {
  5554. K(self.edit.doc, self.edit.textarea).keyup(function(e) {
  5555. if (e.which == 27) {
  5556. setTimeout(function() {
  5557. self.fullscreen();
  5558. }, 0);
  5559. }
  5560. });
  5561. if (loaded) {
  5562. if (_IE && !self.designMode) {
  5563. return;
  5564. }
  5565. self.focus();
  5566. }
  5567. if (!loaded) {
  5568. loaded = true;
  5569. }
  5570. });
  5571. }
  5572. _each('undo,redo'.split(','), function(i, name) {
  5573. if (shortcutKeys[name]) {
  5574. self.afterCreate(function() {
  5575. _ctrl(this.edit.doc, shortcutKeys[name], function() {
  5576. self.clickToolbar(name);
  5577. });
  5578. });
  5579. }
  5580. self.clickToolbar(name, function() {
  5581. self[name]();
  5582. });
  5583. });
  5584. self.clickToolbar('formatblock', function() {
  5585. var blocks = self.lang('formatblock.formatBlock'),
  5586. heights = {
  5587. h1 : 28,
  5588. h2 : 24,
  5589. h3 : 18,
  5590. H4 : 14,
  5591. p : 12
  5592. },
  5593. curVal = self.cmd.val('formatblock'),
  5594. menu = self.createMenu({
  5595. name : 'formatblock',
  5596. width : self.langType == 'en' ? 200 : 150
  5597. });
  5598. _each(blocks, function(key, val) {
  5599. var style = 'font-size:' + heights[key] + 'px;';
  5600. if (key.charAt(0) === 'h') {
  5601. style += 'font-weight:bold;';
  5602. }
  5603. menu.addItem({
  5604. title : '<span style="' + style + '" unselectable="on">' + val + '</span>',
  5605. height : heights[key] + 12,
  5606. checked : (curVal === key || curVal === val),
  5607. click : function() {
  5608. self.select().exec('formatblock', '<' + key + '>').hideMenu();
  5609. }
  5610. });
  5611. });
  5612. });
  5613. self.clickToolbar('fontname', function() {
  5614. var curVal = self.cmd.val('fontname'),
  5615. menu = self.createMenu({
  5616. name : 'fontname',
  5617. width : 150
  5618. });
  5619. _each(self.lang('fontname.fontName'), function(key, val) {
  5620. menu.addItem({
  5621. title : '<span style="font-family: ' + key + ';" unselectable="on">' + val + '</span>',
  5622. checked : (curVal === key.toLowerCase() || curVal === val.toLowerCase()),
  5623. click : function() {
  5624. self.exec('fontname', key).hideMenu();
  5625. }
  5626. });
  5627. });
  5628. });
  5629. self.clickToolbar('fontsize', function() {
  5630. var curVal = self.cmd.val('fontsize'),
  5631. menu = self.createMenu({
  5632. name : 'fontsize',
  5633. width : 150
  5634. });
  5635. _each(self.fontSizeTable, function(i, val) {
  5636. menu.addItem({
  5637. title : '<span style="font-size:' + val + ';" unselectable="on">' + val + '</span>',
  5638. height : _removeUnit(val) + 12,
  5639. checked : curVal === val,
  5640. click : function() {
  5641. self.exec('fontsize', val).hideMenu();
  5642. }
  5643. });
  5644. });
  5645. });
  5646. _each('forecolor,hilitecolor'.split(','), function(i, name) {
  5647. self.clickToolbar(name, function() {
  5648. self.createMenu({
  5649. name : name,
  5650. selectedColor : self.cmd.val(name) || 'default',
  5651. colors : self.colorTable,
  5652. click : function(color) {
  5653. self.exec(name, color).hideMenu();
  5654. }
  5655. });
  5656. });
  5657. });
  5658. _each(('cut,copy,paste').split(','), function(i, name) {
  5659. self.clickToolbar(name, function() {
  5660. self.focus();
  5661. try {
  5662. self.exec(name, null);
  5663. } catch(e) {
  5664. alert(self.lang(name + 'Error'));
  5665. }
  5666. });
  5667. });
  5668. self.clickToolbar('about', function() {
  5669. var html = '<div style="margin:20px;">' +
  5670. '<div>KindEditor ' + _VERSION + '</div>' +
  5671. '<div>Copyright &copy; <a href="http://www.kindsoft.net/" target="_blank">kindsoft.net</a> All rights reserved.</div>' +
  5672. '</div>';
  5673. self.createDialog({
  5674. name : 'about',
  5675. width : 350,
  5676. title : self.lang('about'),
  5677. body : html
  5678. });
  5679. });
  5680. self.plugin.getSelectedLink = function() {
  5681. return self.cmd.commonAncestor('a');
  5682. };
  5683. self.plugin.getSelectedImage = function() {
  5684. return _getImageFromRange(self.edit.cmd.range, function(img) {
  5685. return !/^ke-\w+$/i.test(img[0].className);
  5686. });
  5687. };
  5688. self.plugin.getSelectedFlash = function() {
  5689. return _getImageFromRange(self.edit.cmd.range, function(img) {
  5690. return img[0].className == 'ke-flash';
  5691. });
  5692. };
  5693. self.plugin.getSelectedMedia = function() {
  5694. return _getImageFromRange(self.edit.cmd.range, function(img) {
  5695. return img[0].className == 'ke-media' || img[0].className == 'ke-rm';
  5696. });
  5697. };
  5698. self.plugin.getSelectedAnchor = function() {
  5699. return _getImageFromRange(self.edit.cmd.range, function(img) {
  5700. return img[0].className == 'ke-anchor';
  5701. });
  5702. };
  5703. _each('link,image,flash,media,anchor'.split(','), function(i, name) {
  5704. var uName = name.charAt(0).toUpperCase() + name.substr(1);
  5705. _each('edit,delete'.split(','), function(j, val) {
  5706. self.addContextmenu({
  5707. title : self.lang(val + uName),
  5708. click : function() {
  5709. self.loadPlugin(name, function() {
  5710. self.plugin[name][val]();
  5711. self.hideMenu();
  5712. });
  5713. },
  5714. cond : self.plugin['getSelected' + uName],
  5715. width : 150,
  5716. iconClass : val == 'edit' ? 'ke-icon-' + name : undefined
  5717. });
  5718. });
  5719. self.addContextmenu({ title : '-' });
  5720. });
  5721. self.plugin.getSelectedTable = function() {
  5722. return self.cmd.commonAncestor('table');
  5723. };
  5724. self.plugin.getSelectedRow = function() {
  5725. return self.cmd.commonAncestor('tr');
  5726. };
  5727. self.plugin.getSelectedCell = function() {
  5728. return self.cmd.commonAncestor('td');
  5729. };
  5730. _each(('prop,cellprop,colinsertleft,colinsertright,rowinsertabove,rowinsertbelow,rowmerge,colmerge,' +
  5731. 'rowsplit,colsplit,coldelete,rowdelete,insert,delete').split(','), function(i, val) {
  5732. var cond = _inArray(val, ['prop', 'delete']) < 0 ? self.plugin.getSelectedCell : self.plugin.getSelectedTable;
  5733. self.addContextmenu({
  5734. title : self.lang('table' + val),
  5735. click : function() {
  5736. self.loadPlugin('table', function() {
  5737. self.plugin.table[val]();
  5738. self.hideMenu();
  5739. });
  5740. },
  5741. cond : cond,
  5742. width : 170,
  5743. iconClass : 'ke-icon-table' + val
  5744. });
  5745. });
  5746. self.addContextmenu({ title : '-' });
  5747. _each(('selectall,justifyleft,justifycenter,justifyright,justifyfull,insertorderedlist,' +
  5748. 'insertunorderedlist,indent,outdent,subscript,superscript,hr,print,' +
  5749. 'bold,italic,underline,strikethrough,removeformat,unlink').split(','), function(i, name) {
  5750. if (shortcutKeys[name]) {
  5751. self.afterCreate(function() {
  5752. _ctrl(this.edit.doc, shortcutKeys[name], function() {
  5753. self.cmd.selection();
  5754. self.clickToolbar(name);
  5755. });
  5756. });
  5757. }
  5758. self.clickToolbar(name, function() {
  5759. self.focus().exec(name, null);
  5760. });
  5761. });
  5762. self.afterCreate(function() {
  5763. var doc = self.edit.doc, cmd, bookmark, div,
  5764. cls = '__kindeditor_paste__', pasting = false;
  5765. function movePastedData() {
  5766. cmd.range.moveToBookmark(bookmark);
  5767. cmd.select();
  5768. if (_WEBKIT) {
  5769. K('div.' + cls, div).each(function() {
  5770. K(this).after('<br />').remove(true);
  5771. });
  5772. K('span.Apple-style-span', div).remove(true);
  5773. K('span.Apple-tab-span', div).remove(true);
  5774. K('span[style]', div).each(function() {
  5775. if (K(this).css('white-space') == 'nowrap') {
  5776. K(this).remove(true);
  5777. }
  5778. });
  5779. K('meta', div).remove();
  5780. }
  5781. var html = div[0].innerHTML;
  5782. div.remove();
  5783. if (html === '') {
  5784. return;
  5785. }
  5786. if (_WEBKIT) {
  5787. html = html.replace(/(<br>)\1/ig, '$1');
  5788. }
  5789. if (self.pasteType === 2) {
  5790. html = html.replace(/(<(?:p|p\s[^>]*)>) *(<\/p>)/ig, '');
  5791. if (/schemas-microsoft-com|worddocument|mso-\w+/i.test(html)) {
  5792. html = _clearMsWord(html, self.filterMode ? self.htmlTags : K.options.htmlTags);
  5793. } else {
  5794. html = _formatHtml(html, self.filterMode ? self.htmlTags : null);
  5795. html = self.beforeSetHtml(html);
  5796. }
  5797. }
  5798. if (self.pasteType === 1) {
  5799. html = html.replace(/&nbsp;/ig, ' ');
  5800. html = html.replace(/\n\s*\n/g, '\n');
  5801. html = html.replace(/<br[^>]*>/ig, '\n');
  5802. html = html.replace(/<\/p><p[^>]*>/ig, '\n');
  5803. html = html.replace(/<[^>]+>/g, '');
  5804. html = html.replace(/ {2}/g, ' &nbsp;');
  5805. if (self.newlineTag == 'p') {
  5806. if (/\n/.test(html)) {
  5807. html = html.replace(/^/, '<p>').replace(/$/, '<br /></p>').replace(/\n/g, '<br /></p><p>');
  5808. }
  5809. } else {
  5810. html = html.replace(/\n/g, '<br />$&');
  5811. }
  5812. }
  5813. self.insertHtml(html, true);
  5814. }
  5815. K(doc.body).bind('paste', function(e){
  5816. if (self.pasteType === 0) {
  5817. e.stop();
  5818. return;
  5819. }
  5820. if (pasting) {
  5821. return;
  5822. }
  5823. pasting = true;
  5824. K('div.' + cls, doc).remove();
  5825. cmd = self.cmd.selection();
  5826. bookmark = cmd.range.createBookmark();
  5827. div = K('<div class="' + cls + '"></div>', doc).css({
  5828. position : 'absolute',
  5829. width : '1px',
  5830. height : '1px',
  5831. overflow : 'hidden',
  5832. left : '-1981px',
  5833. top : K(bookmark.start).pos().y + 'px',
  5834. 'white-space' : 'nowrap'
  5835. });
  5836. K(doc.body).append(div);
  5837. if (_IE) {
  5838. var rng = cmd.range.get(true);
  5839. rng.moveToElementText(div[0]);
  5840. rng.select();
  5841. rng.execCommand('paste');
  5842. e.preventDefault();
  5843. } else {
  5844. cmd.range.selectNodeContents(div[0]);
  5845. cmd.select();
  5846. }
  5847. setTimeout(function() {
  5848. movePastedData();
  5849. pasting = false;
  5850. }, 0);
  5851. });
  5852. });
  5853. self.beforeGetHtml(function(html) {
  5854. if (_IE && _V <= 8) {
  5855. html = html.replace(/<div\s+[^>]*data-ke-input-tag="([^"]*)"[^>]*>([\s\S]*?)<\/div>/ig, function(full, tag) {
  5856. return unescape(tag);
  5857. });
  5858. html = html.replace(/(<input)((?:\s+[^>]*)?>)/ig, function($0, $1, $2) {
  5859. if (!/\s+type="[^"]+"/i.test($0)) {
  5860. return $1 + ' type="text"' + $2;
  5861. }
  5862. return $0;
  5863. });
  5864. }
  5865. return html.replace(/(<(?:noscript|noscript\s[^>]*)>)([\s\S]*?)(<\/noscript>)/ig, function($0, $1, $2, $3) {
  5866. return $1 + _unescape($2).replace(/\s+/g, ' ') + $3;
  5867. })
  5868. .replace(/<img[^>]*class="?ke-(flash|rm|media)"?[^>]*>/ig, function(full) {
  5869. var imgAttrs = _getAttrList(full);
  5870. var styles = _getCssList(imgAttrs.style || '');
  5871. var attrs = _mediaAttrs(imgAttrs['data-ke-tag']);
  5872. var width = _undef(styles.width, '');
  5873. var height = _undef(styles.height, '');
  5874. if (/px/i.test(width)) {
  5875. width = _removeUnit(width);
  5876. }
  5877. if (/px/i.test(height)) {
  5878. height = _removeUnit(height);
  5879. }
  5880. attrs.width = _undef(imgAttrs.width, width);
  5881. attrs.height = _undef(imgAttrs.height, height);
  5882. return _mediaEmbed(attrs);
  5883. })
  5884. .replace(/<img[^>]*class="?ke-anchor"?[^>]*>/ig, function(full) {
  5885. var imgAttrs = _getAttrList(full);
  5886. return '<a name="' + unescape(imgAttrs['data-ke-name']) + '"></a>';
  5887. })
  5888. .replace(/<div\s+[^>]*data-ke-script-attr="([^"]*)"[^>]*>([\s\S]*?)<\/div>/ig, function(full, attr, code) {
  5889. return '<script' + unescape(attr) + '>' + unescape(code) + '</script>';
  5890. })
  5891. .replace(/<div\s+[^>]*data-ke-noscript-attr="([^"]*)"[^>]*>([\s\S]*?)<\/div>/ig, function(full, attr, code) {
  5892. return '<noscript' + unescape(attr) + '>' + unescape(code) + '</noscript>';
  5893. })
  5894. .replace(/(<[^>]*)data-ke-src="([^"]*)"([^>]*>)/ig, function(full, start, src, end) {
  5895. full = full.replace(/(\s+(?:href|src)=")[^"]*(")/i, function($0, $1, $2) {
  5896. return $1 + _unescape(src) + $2;
  5897. });
  5898. full = full.replace(/\s+data-ke-src="[^"]*"/i, '');
  5899. return full;
  5900. })
  5901. .replace(/(<[^>]+\s)data-ke-(on\w+="[^"]*"[^>]*>)/ig, function(full, start, end) {
  5902. return start + end;
  5903. });
  5904. });
  5905. self.beforeSetHtml(function(html) {
  5906. if (_IE && _V <= 8) {
  5907. html = html.replace(/<input[^>]*>|<(select|button)[^>]*>[\s\S]*?<\/\1>/ig, function(full) {
  5908. var attrs = _getAttrList(full);
  5909. var styles = _getCssList(attrs.style || '');
  5910. if (styles.display == 'none') {
  5911. return '<div class="ke-display-none" data-ke-input-tag="' + escape(full) + '"></div>';
  5912. }
  5913. return full;
  5914. });
  5915. }
  5916. return html.replace(/<embed[^>]*type="([^"]+)"[^>]*>(?:<\/embed>)?/ig, function(full) {
  5917. var attrs = _getAttrList(full);
  5918. attrs.src = _undef(attrs.src, '');
  5919. attrs.width = _undef(attrs.width, 0);
  5920. attrs.height = _undef(attrs.height, 0);
  5921. return _mediaImg(self.themesPath + 'common/blank.gif', attrs);
  5922. })
  5923. .replace(/<a[^>]*name="([^"]+)"[^>]*>(?:<\/a>)?/ig, function(full) {
  5924. var attrs = _getAttrList(full);
  5925. if (attrs.href !== undefined) {
  5926. return full;
  5927. }
  5928. return '<img class="ke-anchor" src="' + self.themesPath + 'common/anchor.gif" data-ke-name="' + escape(attrs.name) + '" />';
  5929. })
  5930. .replace(/<script([^>]*)>([\s\S]*?)<\/script>/ig, function(full, attr, code) {
  5931. return '<div class="ke-script" data-ke-script-attr="' + escape(attr) + '">' + escape(code) + '</div>';
  5932. })
  5933. .replace(/<noscript([^>]*)>([\s\S]*?)<\/noscript>/ig, function(full, attr, code) {
  5934. return '<div class="ke-noscript" data-ke-noscript-attr="' + escape(attr) + '">' + escape(code) + '</div>';
  5935. })
  5936. .replace(/(<[^>]*)(href|src)="([^"]*)"([^>]*>)/ig, function(full, start, key, src, end) {
  5937. if (full.match(/\sdata-ke-src="[^"]*"/i)) {
  5938. return full;
  5939. }
  5940. full = start + key + '="' + src + '"' + ' data-ke-src="' + _escape(src) + '"' + end;
  5941. return full;
  5942. })
  5943. .replace(/(<[^>]+\s)(on\w+="[^"]*"[^>]*>)/ig, function(full, start, end) {
  5944. return start + 'data-ke-' + end;
  5945. })
  5946. .replace(/<table[^>]*\s+border="0"[^>]*>/ig, function(full) {
  5947. if (full.indexOf('ke-zeroborder') >= 0) {
  5948. return full;
  5949. }
  5950. return _addClassToTag(full, 'ke-zeroborder');
  5951. });
  5952. });
  5953. });
  5954. })(window);