de341aa85af89475365aa1d35068dff4ead5700f.svn-base 157 KB

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