ueditor.all.min.js 985 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356
  1. /*!
  2. * UEditor
  3. * version: ueditor
  4. * build: Wed Dec 26 2018 17:25:05 GMT+0800 (CST)
  5. */
  6. (function () {
  7. // editor.js
  8. UEDITOR_CONFIG = window.UEDITOR_CONFIG || {};
  9. var baidu = window.baidu || {};
  10. window.baidu = baidu;
  11. window.UE = baidu.editor = window.UE || {};
  12. UE.plugins = {};
  13. UE.commands = {};
  14. UE.instants = {};
  15. UE.I18N = {};
  16. UE._customizeUI = {};
  17. UE.version = "1.4.3";
  18. var dom = UE.dom = {};
  19. // core/browser.js
  20. /**
  21. * 浏览器判断模块
  22. * @file
  23. * @module UE.browser
  24. * @since 1.2.6.1
  25. */
  26. /**
  27. * 提供浏览器检测的模块
  28. * @unfile
  29. * @module UE.browser
  30. */
  31. var browser = UE.browser = function () {
  32. var agent = navigator.userAgent.toLowerCase(),
  33. opera = window.opera,
  34. browser = {
  35. /**
  36. * @property {boolean} ie 检测当前浏览器是否为IE
  37. * @example
  38. * ```javascript
  39. * if ( UE.browser.ie ) {
  40. * console.log( '当前浏览器是IE' );
  41. * }
  42. * ```
  43. */
  44. ie: /(msie\s|trident.*rv:)([\w.]+)/.test(agent),
  45. /**
  46. * @property {boolean} opera 检测当前浏览器是否为Opera
  47. * @example
  48. * ```javascript
  49. * if ( UE.browser.opera ) {
  50. * console.log( '当前浏览器是Opera' );
  51. * }
  52. * ```
  53. */
  54. opera: (!!opera && opera.version),
  55. /**
  56. * @property {boolean} webkit 检测当前浏览器是否是webkit内核的浏览器
  57. * @example
  58. * ```javascript
  59. * if ( UE.browser.webkit ) {
  60. * console.log( '当前浏览器是webkit内核浏览器' );
  61. * }
  62. * ```
  63. */
  64. webkit: (agent.indexOf(' applewebkit/') > -1),
  65. /**
  66. * @property {boolean} mac 检测当前浏览器是否是运行在mac平台下
  67. * @example
  68. * ```javascript
  69. * if ( UE.browser.mac ) {
  70. * console.log( '当前浏览器运行在mac平台下' );
  71. * }
  72. * ```
  73. */
  74. mac: (agent.indexOf('macintosh') > -1),
  75. /**
  76. * @property {boolean} quirks 检测当前浏览器是否处于“怪异模式”下
  77. * @example
  78. * ```javascript
  79. * if ( UE.browser.quirks ) {
  80. * console.log( '当前浏览器运行处于“怪异模式”' );
  81. * }
  82. * ```
  83. */
  84. quirks: (document.compatMode == 'BackCompat')
  85. };
  86. /**
  87. * @property {boolean} gecko 检测当前浏览器内核是否是gecko内核
  88. * @example
  89. * ```javascript
  90. * if ( UE.browser.gecko ) {
  91. * console.log( '当前浏览器内核是gecko内核' );
  92. * }
  93. * ```
  94. */
  95. browser.gecko = (navigator.product == 'Gecko' && !browser.webkit && !browser.opera && !browser.ie);
  96. var version = 0;
  97. // Internet Explorer 6.0+
  98. if (browser.ie) {
  99. var v1 = agent.match(/(?:msie\s([\w.]+))/);
  100. var v2 = agent.match(/(?:trident.*rv:([\w.]+))/);
  101. if (v1 && v2 && v1[1] && v2[1]) {
  102. version = Math.max(v1[1] * 1, v2[1] * 1);
  103. } else if (v1 && v1[1]) {
  104. version = v1[1] * 1;
  105. } else if (v2 && v2[1]) {
  106. version = v2[1] * 1;
  107. } else {
  108. version = 0;
  109. }
  110. browser.ie11Compat = document.documentMode == 11;
  111. /**
  112. * @property { boolean } ie9Compat 检测浏览器模式是否为 IE9 兼容模式
  113. * @warning 如果浏览器不是IE, 则该值为undefined
  114. * @example
  115. * ```javascript
  116. * if ( UE.browser.ie9Compat ) {
  117. * console.log( '当前浏览器运行在IE9兼容模式下' );
  118. * }
  119. * ```
  120. */
  121. browser.ie9Compat = document.documentMode == 9;
  122. /**
  123. * @property { boolean } ie8 检测浏览器是否是IE8浏览器
  124. * @warning 如果浏览器不是IE, 则该值为undefined
  125. * @example
  126. * ```javascript
  127. * if ( UE.browser.ie8 ) {
  128. * console.log( '当前浏览器是IE8浏览器' );
  129. * }
  130. * ```
  131. */
  132. browser.ie8 = !!document.documentMode;
  133. /**
  134. * @property { boolean } ie8Compat 检测浏览器模式是否为 IE8 兼容模式
  135. * @warning 如果浏览器不是IE, 则该值为undefined
  136. * @example
  137. * ```javascript
  138. * if ( UE.browser.ie8Compat ) {
  139. * console.log( '当前浏览器运行在IE8兼容模式下' );
  140. * }
  141. * ```
  142. */
  143. browser.ie8Compat = document.documentMode == 8;
  144. /**
  145. * @property { boolean } ie7Compat 检测浏览器模式是否为 IE7 兼容模式
  146. * @warning 如果浏览器不是IE, 则该值为undefined
  147. * @example
  148. * ```javascript
  149. * if ( UE.browser.ie7Compat ) {
  150. * console.log( '当前浏览器运行在IE7兼容模式下' );
  151. * }
  152. * ```
  153. */
  154. browser.ie7Compat = ((version == 7 && !document.documentMode) ||
  155. document.documentMode == 7);
  156. /**
  157. * @property { boolean } ie6Compat 检测浏览器模式是否为 IE6 模式 或者怪异模式
  158. * @warning 如果浏览器不是IE, 则该值为undefined
  159. * @example
  160. * ```javascript
  161. * if ( UE.browser.ie6Compat ) {
  162. * console.log( '当前浏览器运行在IE6模式或者怪异模式下' );
  163. * }
  164. * ```
  165. */
  166. browser.ie6Compat = (version < 7 || browser.quirks);
  167. browser.ie9above = version > 8;
  168. browser.ie9below = version < 9;
  169. browser.ie11above = version > 10;
  170. browser.ie11below = version < 11;
  171. }
  172. // Gecko.
  173. if (browser.gecko) {
  174. var geckoRelease = agent.match(/rv:([\d\.]+)/);
  175. if (geckoRelease) {
  176. geckoRelease = geckoRelease[1].split('.');
  177. version = geckoRelease[0] * 10000 + (geckoRelease[1] || 0) * 100 + (geckoRelease[2] || 0) * 1;
  178. }
  179. }
  180. /**
  181. * @property { Number } chrome 检测当前浏览器是否为Chrome, 如果是,则返回Chrome的大版本号
  182. * @warning 如果浏览器不是chrome, 则该值为undefined
  183. * @example
  184. * ```javascript
  185. * if ( UE.browser.chrome ) {
  186. * console.log( '当前浏览器是Chrome' );
  187. * }
  188. * ```
  189. */
  190. if (/chrome\/(\d+\.\d)/i.test(agent)) {
  191. browser.chrome = +RegExp['\x241'];
  192. }
  193. /**
  194. * @property { Number } safari 检测当前浏览器是否为Safari, 如果是,则返回Safari的大版本号
  195. * @warning 如果浏览器不是safari, 则该值为undefined
  196. * @example
  197. * ```javascript
  198. * if ( UE.browser.safari ) {
  199. * console.log( '当前浏览器是Safari' );
  200. * }
  201. * ```
  202. */
  203. if (/(\d+\.\d)?(?:\.\d)?\s+safari\/?(\d+\.\d+)?/i.test(agent) && !/chrome/i.test(agent)) {
  204. browser.safari = +(RegExp['\x241'] || RegExp['\x242']);
  205. }
  206. // Opera 9.50+
  207. if (browser.opera)
  208. version = parseFloat(opera.version());
  209. // WebKit 522+ (Safari 3+)
  210. if (browser.webkit)
  211. version = parseFloat(agent.match(/ applewebkit\/(\d+)/)[1]);
  212. /**
  213. * @property { Number } version 检测当前浏览器版本号
  214. * @remind
  215. * <ul>
  216. * <li>IE系列返回值为5,6,7,8,9,10等</li>
  217. * <li>gecko系列会返回10900,158900等</li>
  218. * <li>webkit系列会返回其build号 (如 522等)</li>
  219. * </ul>
  220. * @example
  221. * ```javascript
  222. * console.log( '当前浏览器版本号是: ' + UE.browser.version );
  223. * ```
  224. */
  225. browser.version = version;
  226. /**
  227. * @property { boolean } isCompatible 检测当前浏览器是否能够与UEditor良好兼容
  228. * @example
  229. * ```javascript
  230. * if ( UE.browser.isCompatible ) {
  231. * console.log( '浏览器与UEditor能够良好兼容' );
  232. * }
  233. * ```
  234. */
  235. browser.isCompatible = !browser.mobile && (
  236. (browser.ie && version >= 6) ||
  237. (browser.gecko && version >= 10801) ||
  238. (browser.opera && version >= 9.5) ||
  239. (browser.air && version >= 1) ||
  240. (browser.webkit && version >= 522) ||
  241. false);
  242. return browser;
  243. }();
  244. //快捷方式
  245. var ie = browser.ie,
  246. webkit = browser.webkit,
  247. gecko = browser.gecko,
  248. opera = browser.opera;
  249. // core/utils.js
  250. /**
  251. * 工具函数包
  252. * @file
  253. * @module UE.utils
  254. * @since 1.2.6.1
  255. */
  256. /**
  257. * UEditor封装使用的静态工具函数
  258. * @module UE.utils
  259. * @unfile
  260. */
  261. var utils = UE.utils = {
  262. /**
  263. * 用给定的迭代器遍历对象
  264. * @method each
  265. * @param { Object } obj 需要遍历的对象
  266. * @param { Function } iterator 迭代器, 该方法接受两个参数, 第一个参数是当前所处理的value, 第二个参数是当前遍历对象的key
  267. * @example
  268. * ```javascript
  269. * var demoObj = {
  270. * key1: 1,
  271. * key2: 2
  272. * };
  273. *
  274. * //output: key1: 1, key2: 2
  275. * UE.utils.each( demoObj, funciton ( value, key ) {
  276. *
  277. * console.log( key + ":" + value );
  278. *
  279. * } );
  280. * ```
  281. */
  282. /**
  283. * 用给定的迭代器遍历数组或类数组对象
  284. * @method each
  285. * @param { Array } array 需要遍历的数组或者类数组
  286. * @param { Function } iterator 迭代器, 该方法接受两个参数, 第一个参数是当前所处理的value, 第二个参数是当前遍历对象的key
  287. * @example
  288. * ```javascript
  289. * var divs = document.getElmentByTagNames( "div" );
  290. *
  291. * //output: 0: DIV, 1: DIV ...
  292. * UE.utils.each( divs, funciton ( value, key ) {
  293. *
  294. * console.log( key + ":" + value.tagName );
  295. *
  296. * } );
  297. * ```
  298. */
  299. each: function (obj, iterator, context) {
  300. if (obj == null) return;
  301. if (obj.length === +obj.length) {
  302. for (var i = 0, l = obj.length; i < l; i++) {
  303. if (iterator.call(context, obj[i], i, obj) === false)
  304. return false;
  305. }
  306. } else {
  307. for (var key in obj) {
  308. if (obj.hasOwnProperty(key)) {
  309. if (iterator.call(context, obj[key], key, obj) === false)
  310. return false;
  311. }
  312. }
  313. }
  314. },
  315. /**
  316. * 以给定对象作为原型创建一个新对象
  317. * @method makeInstance
  318. * @param { Object } protoObject 该对象将作为新创建对象的原型
  319. * @return { Object } 新的对象, 该对象的原型是给定的protoObject对象
  320. * @example
  321. * ```javascript
  322. *
  323. * var protoObject = { sayHello: function () { console.log('Hello UEditor!'); } };
  324. *
  325. * var newObject = UE.utils.makeInstance( protoObject );
  326. * //output: Hello UEditor!
  327. * newObject.sayHello();
  328. * ```
  329. */
  330. makeInstance: function (obj) {
  331. var noop = new Function();
  332. noop.prototype = obj;
  333. obj = new noop;
  334. noop.prototype = null;
  335. return obj;
  336. },
  337. /**
  338. * 将source对象中的属性扩展到target对象上
  339. * @method extend
  340. * @remind 该方法将强制把source对象上的属性复制到target对象上
  341. * @see UE.utils.extend(Object,Object,Boolean)
  342. * @param { Object } target 目标对象, 新的属性将附加到该对象上
  343. * @param { Object } source 源对象, 该对象的属性会被附加到target对象上
  344. * @return { Object } 返回target对象
  345. * @example
  346. * ```javascript
  347. *
  348. * var target = { name: 'target', sex: 1 },
  349. * source = { name: 'source', age: 17 };
  350. *
  351. * UE.utils.extend( target, source );
  352. *
  353. * //output: { name: 'source', sex: 1, age: 17 }
  354. * console.log( target );
  355. *
  356. * ```
  357. */
  358. /**
  359. * 将source对象中的属性扩展到target对象上, 根据指定的isKeepTarget值决定是否保留目标对象中与
  360. * 源对象属性名相同的属性值。
  361. * @method extend
  362. * @param { Object } target 目标对象, 新的属性将附加到该对象上
  363. * @param { Object } source 源对象, 该对象的属性会被附加到target对象上
  364. * @param { Boolean } isKeepTarget 是否保留目标对象中与源对象中属性名相同的属性
  365. * @return { Object } 返回target对象
  366. * @example
  367. * ```javascript
  368. *
  369. * var target = { name: 'target', sex: 1 },
  370. * source = { name: 'source', age: 17 };
  371. *
  372. * UE.utils.extend( target, source, true );
  373. *
  374. * //output: { name: 'target', sex: 1, age: 17 }
  375. * console.log( target );
  376. *
  377. * ```
  378. */
  379. extend: function (t, s, b) {
  380. if (s) {
  381. for (var k in s) {
  382. if (!b || !t.hasOwnProperty(k)) {
  383. t[k] = s[k];
  384. }
  385. }
  386. }
  387. return t;
  388. },
  389. /**
  390. * 将给定的多个对象的属性复制到目标对象target上
  391. * @method extend2
  392. * @remind 该方法将强制把源对象上的属性复制到target对象上
  393. * @remind 该方法支持两个及以上的参数, 从第二个参数开始, 其属性都会被复制到第一个参数上。 如果遇到同名的属性,
  394. * 将会覆盖掉之前的值。
  395. * @param { Object } target 目标对象, 新的属性将附加到该对象上
  396. * @param { Object... } source 源对象, 支持多个对象, 该对象的属性会被附加到target对象上
  397. * @return { Object } 返回target对象
  398. * @example
  399. * ```javascript
  400. *
  401. * var target = {},
  402. * source1 = { name: 'source', age: 17 },
  403. * source2 = { title: 'dev' };
  404. *
  405. * UE.utils.extend2( target, source1, source2 );
  406. *
  407. * //output: { name: 'source', age: 17, title: 'dev' }
  408. * console.log( target );
  409. *
  410. * ```
  411. */
  412. extend2: function (t) {
  413. var a = arguments;
  414. for (var i = 1; i < a.length; i++) {
  415. var x = a[i];
  416. for (var k in x) {
  417. if (!t.hasOwnProperty(k)) {
  418. t[k] = x[k];
  419. }
  420. }
  421. }
  422. return t;
  423. },
  424. /**
  425. * 模拟继承机制, 使得subClass继承自superClass
  426. * @method inherits
  427. * @param { Object } subClass 子类对象
  428. * @param { Object } superClass 超类对象
  429. * @warning 该方法只能让subClass继承超类的原型, subClass对象自身的属性和方法不会被继承
  430. * @return { Object } 继承superClass后的子类对象
  431. * @example
  432. * ```javascript
  433. * function SuperClass(){
  434. * this.name = "小李";
  435. * }
  436. *
  437. * SuperClass.prototype = {
  438. * hello:function(str){
  439. * console.log(this.name + str);
  440. * }
  441. * }
  442. *
  443. * function SubClass(){
  444. * this.name = "小张";
  445. * }
  446. *
  447. * UE.utils.inherits(SubClass,SuperClass);
  448. *
  449. * var sub = new SubClass();
  450. * //output: '小张早上好!
  451. * sub.hello("早上好!");
  452. * ```
  453. */
  454. inherits: function (subClass, superClass) {
  455. var oldP = subClass.prototype,
  456. newP = utils.makeInstance(superClass.prototype);
  457. utils.extend(newP, oldP, true);
  458. subClass.prototype = newP;
  459. return (newP.constructor = subClass);
  460. },
  461. /**
  462. * 用指定的context对象作为函数fn的上下文
  463. * @method bind
  464. * @param { Function } fn 需要绑定上下文的函数对象
  465. * @param { Object } content 函数fn新的上下文对象
  466. * @return { Function } 一个新的函数, 该函数作为原始函数fn的代理, 将完成fn的上下文调换工作。
  467. * @example
  468. * ```javascript
  469. *
  470. * var name = 'window',
  471. * newTest = null;
  472. *
  473. * function test () {
  474. * console.log( this.name );
  475. * }
  476. *
  477. * newTest = UE.utils.bind( test, { name: 'object' } );
  478. *
  479. * //output: object
  480. * newTest();
  481. *
  482. * //output: window
  483. * test();
  484. *
  485. * ```
  486. */
  487. bind: function (fn, context) {
  488. return function () {
  489. return fn.apply(context, arguments);
  490. };
  491. },
  492. /**
  493. * 创建延迟指定时间后执行的函数fn
  494. * @method defer
  495. * @param { Function } fn 需要延迟执行的函数对象
  496. * @param { int } delay 延迟的时间, 单位是毫秒
  497. * @warning 该方法的时间控制是不精确的,仅仅只能保证函数的执行是在给定的时间之后,
  498. * 而不能保证刚好到达延迟时间时执行。
  499. * @return { Function } 目标函数fn的代理函数, 只有执行该函数才能起到延时效果
  500. * @example
  501. * ```javascript
  502. * var start = 0;
  503. *
  504. * function test(){
  505. * console.log( new Date() - start );
  506. * }
  507. *
  508. * var testDefer = UE.utils.defer( test, 1000 );
  509. * //
  510. * start = new Date();
  511. * //output: (大约在1000毫秒之后输出) 1000
  512. * testDefer();
  513. * ```
  514. */
  515. /**
  516. * 创建延迟指定时间后执行的函数fn, 如果在延迟时间内再次执行该方法, 将会根据指定的exclusion的值,
  517. * 决定是否取消前一次函数的执行, 如果exclusion的值为true, 则取消执行,反之,将继续执行前一个方法。
  518. * @method defer
  519. * @param { Function } fn 需要延迟执行的函数对象
  520. * @param { int } delay 延迟的时间, 单位是毫秒
  521. * @param { Boolean } exclusion 如果在延迟时间内再次执行该函数,该值将决定是否取消执行前一次函数的执行,
  522. * 值为true表示取消执行, 反之则将在执行前一次函数之后才执行本次函数调用。
  523. * @warning 该方法的时间控制是不精确的,仅仅只能保证函数的执行是在给定的时间之后,
  524. * 而不能保证刚好到达延迟时间时执行。
  525. * @return { Function } 目标函数fn的代理函数, 只有执行该函数才能起到延时效果
  526. * @example
  527. * ```javascript
  528. *
  529. * function test(){
  530. * console.log(1);
  531. * }
  532. *
  533. * var testDefer = UE.utils.defer( test, 1000, true );
  534. *
  535. * //output: (两次调用仅有一次输出) 1
  536. * testDefer();
  537. * testDefer();
  538. * ```
  539. */
  540. defer: function (fn, delay, exclusion) {
  541. var timerID;
  542. return function () {
  543. if (exclusion) {
  544. clearTimeout(timerID);
  545. }
  546. timerID = setTimeout(fn, delay);
  547. };
  548. },
  549. /**
  550. * 获取元素item在数组array中首次出现的位置, 如果未找到item, 则返回-1
  551. * @method indexOf
  552. * @remind 该方法的匹配过程使用的是恒等“===”
  553. * @param { Array } array 需要查找的数组对象
  554. * @param { * } item 需要在目标数组中查找的值
  555. * @return { int } 返回item在目标数组array中首次出现的位置, 如果在数组中未找到item, 则返回-1
  556. * @example
  557. * ```javascript
  558. * var item = 1,
  559. * arr = [ 3, 4, 6, 8, 1, 1, 2 ];
  560. *
  561. * //output: 4
  562. * console.log( UE.utils.indexOf( arr, item ) );
  563. * ```
  564. */
  565. /**
  566. * 获取元素item数组array中首次出现的位置, 如果未找到item, 则返回-1。通过start的值可以指定搜索的起始位置。
  567. * @method indexOf
  568. * @remind 该方法的匹配过程使用的是恒等“===”
  569. * @param { Array } array 需要查找的数组对象
  570. * @param { * } item 需要在目标数组中查找的值
  571. * @param { int } start 搜索的起始位置
  572. * @return { int } 返回item在目标数组array中的start位置之后首次出现的位置, 如果在数组中未找到item, 则返回-1
  573. * @example
  574. * ```javascript
  575. * var item = 1,
  576. * arr = [ 3, 4, 6, 8, 1, 2, 8, 3, 2, 1, 1, 4 ];
  577. *
  578. * //output: 9
  579. * console.log( UE.utils.indexOf( arr, item, 5 ) );
  580. * ```
  581. */
  582. indexOf: function (array, item, start) {
  583. var index = -1;
  584. start = this.isNumber(start) ? start : 0;
  585. this.each(array, function (v, i) {
  586. if (i >= start && v === item) {
  587. index = i;
  588. return false;
  589. }
  590. });
  591. return index;
  592. },
  593. /**
  594. * 移除数组array中所有的元素item
  595. * @method removeItem
  596. * @param { Array } array 要移除元素的目标数组
  597. * @param { * } item 将要被移除的元素
  598. * @remind 该方法的匹配过程使用的是恒等“===”
  599. * @example
  600. * ```javascript
  601. * var arr = [ 4, 5, 7, 1, 3, 4, 6 ];
  602. *
  603. * UE.utils.removeItem( arr, 4 );
  604. * //output: [ 5, 7, 1, 3, 6 ]
  605. * console.log( arr );
  606. *
  607. * ```
  608. */
  609. removeItem: function (array, item) {
  610. for (var i = 0, l = array.length; i < l; i++) {
  611. if (array[i] === item) {
  612. array.splice(i, 1);
  613. i--;
  614. }
  615. }
  616. },
  617. /**
  618. * 删除字符串str的首尾空格
  619. * @method trim
  620. * @param { String } str 需要删除首尾空格的字符串
  621. * @return { String } 删除了首尾的空格后的字符串
  622. * @example
  623. * ```javascript
  624. *
  625. * var str = " UEdtior ";
  626. *
  627. * //output: 9
  628. * console.log( str.length );
  629. *
  630. * //output: 7
  631. * console.log( UE.utils.trim( " UEdtior " ).length );
  632. *
  633. * //output: 9
  634. * console.log( str.length );
  635. *
  636. * ```
  637. */
  638. trim: function (str) {
  639. return str.replace(/(^[ \t\n\r]+)|([ \t\n\r]+$)/g, '');
  640. },
  641. /**
  642. * 将字符串str以','分隔成数组后,将该数组转换成哈希对象, 其生成的hash对象的key为数组中的元素, value为1
  643. * @method listToMap
  644. * @warning 该方法在生成的hash对象中,会为每一个key同时生成一个另一个全大写的key。
  645. * @param { String } str 该字符串将被以','分割为数组, 然后进行转化
  646. * @return { Object } 转化之后的hash对象
  647. * @example
  648. * ```javascript
  649. *
  650. * //output: Object {UEdtior: 1, UEDTIOR: 1, Hello: 1, HELLO: 1}
  651. * console.log( UE.utils.listToMap( 'UEdtior,Hello' ) );
  652. *
  653. * ```
  654. */
  655. /**
  656. * 将字符串数组转换成哈希对象, 其生成的hash对象的key为数组中的元素, value为1
  657. * @method listToMap
  658. * @warning 该方法在生成的hash对象中,会为每一个key同时生成一个另一个全大写的key。
  659. * @param { Array } arr 字符串数组
  660. * @return { Object } 转化之后的hash对象
  661. * @example
  662. * ```javascript
  663. *
  664. * //output: Object {UEdtior: 1, UEDTIOR: 1, Hello: 1, HELLO: 1}
  665. * console.log( UE.utils.listToMap( [ 'UEdtior', 'Hello' ] ) );
  666. *
  667. * ```
  668. */
  669. listToMap: function (list) {
  670. if (!list) return {};
  671. list = utils.isArray(list) ? list : list.split(',');
  672. for (var i = 0, ci, obj = {}; ci = list[i++];) {
  673. obj[ci.toUpperCase()] = obj[ci] = 1;
  674. }
  675. return obj;
  676. },
  677. /**
  678. * 将str中的html符号转义,将转义“',&,<,",>”五个字符
  679. * @method unhtml
  680. * @param { String } str 需要转义的字符串
  681. * @return { String } 转义后的字符串
  682. * @example
  683. * ```javascript
  684. * var html = '<body>&</body>';
  685. *
  686. * //output: &lt;body&gt;&amp;&lt;/body&gt;
  687. * console.log( UE.utils.unhtml( html ) );
  688. *
  689. * ```
  690. */
  691. unhtml: function (str, reg) {
  692. return str ? str.replace(reg || /[&<">'](?:(amp|lt|quot|gt|#39|nbsp|#\d+);)?/g, function (a, b) {
  693. if (b) {
  694. return a;
  695. } else {
  696. return {
  697. '<': '&lt;',
  698. '&': '&amp;',
  699. '"': '&quot;',
  700. '>': '&gt;',
  701. "'": '&#39;'
  702. } [a]
  703. }
  704. }) : '';
  705. },
  706. /**
  707. * 将url中的html字符转义, 仅转义 ', ", <, > 四个字符
  708. * @param { String } str 需要转义的字符串
  709. * @param { RegExp } reg 自定义的正则
  710. * @return { String } 转义后的字符串
  711. */
  712. unhtmlForUrl: function (str, reg) {
  713. return str ? str.replace(reg || /[<">']/g, function (a) {
  714. return {
  715. '<': '&lt;',
  716. '&': '&amp;',
  717. '"': '&quot;',
  718. '>': '&gt;',
  719. "'": '&#39;'
  720. } [a]
  721. }) : '';
  722. },
  723. /**
  724. * 将str中的转义字符还原成html字符
  725. * @see UE.utils.unhtml(String);
  726. * @method html
  727. * @param { String } str 需要逆转义的字符串
  728. * @return { String } 逆转义后的字符串
  729. * @example
  730. * ```javascript
  731. *
  732. * var str = '&lt;body&gt;&amp;&lt;/body&gt;';
  733. *
  734. * //output: <body>&</body>
  735. * console.log( UE.utils.html( str ) );
  736. *
  737. * ```
  738. */
  739. html: function (str) {
  740. return str ? str.replace(/&((g|l|quo)t|amp|#39|nbsp);/g, function (m) {
  741. return {
  742. '&lt;': '<',
  743. '&amp;': '&',
  744. '&quot;': '"',
  745. '&gt;': '>',
  746. '&#39;': "'",
  747. '&nbsp;': ' '
  748. } [m]
  749. }) : '';
  750. },
  751. /**
  752. * 将css样式转换为驼峰的形式
  753. * @method cssStyleToDomStyle
  754. * @param { String } cssName 需要转换的css样式名
  755. * @return { String } 转换成驼峰形式后的css样式名
  756. * @example
  757. * ```javascript
  758. *
  759. * var str = 'border-top';
  760. *
  761. * //output: borderTop
  762. * console.log( UE.utils.cssStyleToDomStyle( str ) );
  763. *
  764. * ```
  765. */
  766. cssStyleToDomStyle: function () {
  767. var test = document.createElement('div').style,
  768. cache = {
  769. 'float': test.cssFloat != undefined ? 'cssFloat' : test.styleFloat != undefined ? 'styleFloat' : 'float'
  770. };
  771. return function (cssName) {
  772. return cache[cssName] || (cache[cssName] = cssName.toLowerCase().replace(/-./g, function (match) {
  773. return match.charAt(1).toUpperCase();
  774. }));
  775. };
  776. }(),
  777. /**
  778. * 动态加载文件到doc中
  779. * @method loadFile
  780. * @param { DomDocument } document 需要加载资源文件的文档对象
  781. * @param { Object } options 加载资源文件的属性集合, 取值请参考代码示例
  782. * @example
  783. * ```javascript
  784. *
  785. * UE.utils.loadFile( document, {
  786. * src:"test.js",
  787. * tag:"script",
  788. * type:"text/javascript",
  789. * defer:"defer"
  790. * } );
  791. *
  792. * ```
  793. */
  794. /**
  795. * 动态加载文件到doc中,加载成功后执行的回调函数fn
  796. * @method loadFile
  797. * @param { DomDocument } document 需要加载资源文件的文档对象
  798. * @param { Object } options 加载资源文件的属性集合, 该集合支持的值是script标签和style标签支持的所有属性。
  799. * @param { Function } fn 资源文件加载成功之后执行的回调
  800. * @warning 对于在同一个文档中多次加载同一URL的文件, 该方法会在第一次加载之后缓存该请求,
  801. * 在此之后的所有同一URL的请求, 将会直接触发回调。
  802. * @example
  803. * ```javascript
  804. *
  805. * UE.utils.loadFile( document, {
  806. * src:"test.js",
  807. * tag:"script",
  808. * type:"text/javascript",
  809. * defer:"defer"
  810. * }, function () {
  811. * console.log('加载成功');
  812. * } );
  813. *
  814. * ```
  815. */
  816. loadFile: function () {
  817. var tmpList = [];
  818. function getItem(doc, obj) {
  819. try {
  820. for (var i = 0, ci; ci = tmpList[i++];) {
  821. if (ci.doc === doc && ci.url == (obj.src || obj.href)) {
  822. return ci;
  823. }
  824. }
  825. } catch (e) {
  826. return null;
  827. }
  828. }
  829. return function (doc, obj, fn) {
  830. var item = getItem(doc, obj);
  831. if (item) {
  832. if (item.ready) {
  833. fn && fn();
  834. } else {
  835. item.funs.push(fn)
  836. }
  837. return;
  838. }
  839. tmpList.push({
  840. doc: doc,
  841. url: obj.src || obj.href,
  842. funs: [fn]
  843. });
  844. if (!doc.body) {
  845. var html = [];
  846. for (var p in obj) {
  847. if (p == 'tag') continue;
  848. html.push(p + '="' + obj[p] + '"')
  849. }
  850. doc.write('<' + obj.tag + ' ' + html.join(' ') + ' ></' + obj.tag + '>');
  851. return;
  852. }
  853. if (obj.id && doc.getElementById(obj.id)) {
  854. return;
  855. }
  856. var element = doc.createElement(obj.tag);
  857. delete obj.tag;
  858. for (var p in obj) {
  859. element.setAttribute(p, obj[p]);
  860. }
  861. element.onload = element.onreadystatechange = function () {
  862. if (!this.readyState || /loaded|complete/.test(this.readyState)) {
  863. item = getItem(doc, obj);
  864. if (item.funs.length > 0) {
  865. item.ready = 1;
  866. for (var fi; fi = item.funs.pop();) {
  867. fi();
  868. }
  869. }
  870. element.onload = element.onreadystatechange = null;
  871. }
  872. };
  873. element.onerror = function () {
  874. throw Error('The load ' + (obj.href || obj.src) + ' fails,check the url settings of file ueditor.config.js ')
  875. };
  876. doc.getElementsByTagName("head")[0].appendChild(element);
  877. }
  878. }(),
  879. /**
  880. * 判断obj对象是否为空
  881. * @method isEmptyObject
  882. * @param { * } obj 需要判断的对象
  883. * @remind 如果判断的对象是NULL, 将直接返回true, 如果是数组且为空, 返回true, 如果是字符串, 且字符串为空,
  884. * 返回true, 如果是普通对象, 且该对象没有任何实例属性, 返回true
  885. * @return { Boolean } 对象是否为空
  886. * @example
  887. * ```javascript
  888. *
  889. * //output: true
  890. * console.log( UE.utils.isEmptyObject( {} ) );
  891. *
  892. * //output: true
  893. * console.log( UE.utils.isEmptyObject( [] ) );
  894. *
  895. * //output: true
  896. * console.log( UE.utils.isEmptyObject( "" ) );
  897. *
  898. * //output: false
  899. * console.log( UE.utils.isEmptyObject( { key: 1 } ) );
  900. *
  901. * //output: false
  902. * console.log( UE.utils.isEmptyObject( [1] ) );
  903. *
  904. * //output: false
  905. * console.log( UE.utils.isEmptyObject( "1" ) );
  906. *
  907. * ```
  908. */
  909. isEmptyObject: function (obj) {
  910. if (obj == null) return true;
  911. if (this.isArray(obj) || this.isString(obj)) return obj.length === 0;
  912. for (var key in obj)
  913. if (obj.hasOwnProperty(key)) return false;
  914. return true;
  915. },
  916. /**
  917. * 把rgb格式的颜色值转换成16进制格式
  918. * @method fixColor
  919. * @param { String } rgb格式的颜色值
  920. * @param { String }
  921. * @example
  922. * rgb(255,255,255) => "#ffffff"
  923. */
  924. fixColor: function (name, value) {
  925. if (/color/i.test(name) && /rgba?/.test(value)) {
  926. var array = value.split(",");
  927. if (array.length > 3)
  928. return "";
  929. value = "#";
  930. for (var i = 0, color; color = array[i++];) {
  931. color = parseInt(color.replace(/[^\d]/gi, ''), 10).toString(16);
  932. value += color.length == 1 ? "0" + color : color;
  933. }
  934. value = value.toUpperCase();
  935. }
  936. return value;
  937. },
  938. /**
  939. * 只针对border,padding,margin做了处理,因为性能问题
  940. * @public
  941. * @function
  942. * @param {String} val style字符串
  943. */
  944. optCss: function (val) {
  945. var padding, margin, border;
  946. val = val.replace(/(padding|margin|border)\-([^:]+):([^;]+);?/gi, function (str, key, name, val) {
  947. if (val.split(' ').length == 1) {
  948. switch (key) {
  949. case 'padding':
  950. !padding && (padding = {});
  951. padding[name] = val;
  952. return '';
  953. case 'margin':
  954. !margin && (margin = {});
  955. margin[name] = val;
  956. return '';
  957. case 'border':
  958. return val == 'initial' ? '' : str;
  959. }
  960. }
  961. return str;
  962. });
  963. function opt(obj, name) {
  964. if (!obj) {
  965. return '';
  966. }
  967. var t = obj.top,
  968. b = obj.bottom,
  969. l = obj.left,
  970. r = obj.right,
  971. val = '';
  972. if (!t || !l || !b || !r) {
  973. for (var p in obj) {
  974. val += ';' + name + '-' + p + ':' + obj[p] + ';';
  975. }
  976. } else {
  977. val += ';' + name + ':' +
  978. (t == b && b == l && l == r ? t :
  979. t == b && l == r ? (t + ' ' + l) :
  980. l == r ? (t + ' ' + l + ' ' + b) : (t + ' ' + r + ' ' + b + ' ' + l)) + ';'
  981. }
  982. return val;
  983. }
  984. val += opt(padding, 'padding') + opt(margin, 'margin');
  985. return val.replace(/^[ \n\r\t;]*|[ \n\r\t]*$/, '').replace(/;([ \n\r\t]+)|\1;/g, ';')
  986. .replace(/(&((l|g)t|quot|#39))?;{2,}/g, function (a, b) {
  987. return b ? b + ";;" : ';'
  988. });
  989. },
  990. /**
  991. * 克隆对象
  992. * @method clone
  993. * @param { Object } source 源对象
  994. * @return { Object } source的一个副本
  995. */
  996. /**
  997. * 深度克隆对象,将source的属性克隆到target对象, 会覆盖target重名的属性。
  998. * @method clone
  999. * @param { Object } source 源对象
  1000. * @param { Object } target 目标对象
  1001. * @return { Object } 附加了source对象所有属性的target对象
  1002. */
  1003. clone: function (source, target) {
  1004. var tmp;
  1005. target = target || {};
  1006. for (var i in source) {
  1007. if (source.hasOwnProperty(i)) {
  1008. tmp = source[i];
  1009. if (typeof tmp == 'object') {
  1010. target[i] = utils.isArray(tmp) ? [] : {};
  1011. utils.clone(source[i], target[i])
  1012. } else {
  1013. target[i] = tmp;
  1014. }
  1015. }
  1016. }
  1017. return target;
  1018. },
  1019. /**
  1020. * 把cm/pt为单位的值转换为px为单位的值
  1021. * @method transUnitToPx
  1022. * @param { String } 待转换的带单位的字符串
  1023. * @return { String } 转换为px为计量单位的值的字符串
  1024. * @example
  1025. * ```javascript
  1026. *
  1027. * //output: 500px
  1028. * console.log( UE.utils.transUnitToPx( '20cm' ) );
  1029. *
  1030. * //output: 27px
  1031. * console.log( UE.utils.transUnitToPx( '20pt' ) );
  1032. *
  1033. * ```
  1034. */
  1035. transUnitToPx: function (val) {
  1036. if (!/(pt|cm)/.test(val)) {
  1037. return val
  1038. }
  1039. var unit;
  1040. val.replace(/([\d.]+)(\w+)/, function (str, v, u) {
  1041. val = v;
  1042. unit = u;
  1043. });
  1044. switch (unit) {
  1045. case 'cm':
  1046. val = parseFloat(val) * 25;
  1047. break;
  1048. case 'pt':
  1049. val = Math.round(parseFloat(val) * 96 / 72);
  1050. }
  1051. return val + (val ? 'px' : '');
  1052. },
  1053. /**
  1054. * 在dom树ready之后执行给定的回调函数
  1055. * @method domReady
  1056. * @remind 如果在执行该方法的时候, dom树已经ready, 那么回调函数将立刻执行
  1057. * @param { Function } fn dom树ready之后的回调函数
  1058. * @example
  1059. * ```javascript
  1060. *
  1061. * UE.utils.domReady( function () {
  1062. *
  1063. * console.log('123');
  1064. *
  1065. * } );
  1066. *
  1067. * ```
  1068. */
  1069. domReady: function () {
  1070. var fnArr = [];
  1071. function doReady(doc) {
  1072. //确保onready只执行一次
  1073. doc.isReady = true;
  1074. for (var ci; ci = fnArr.pop(); ci()) {}
  1075. }
  1076. return function (onready, win) {
  1077. win = win || window;
  1078. var doc = win.document;
  1079. onready && fnArr.push(onready);
  1080. if (doc.readyState === "complete") {
  1081. doReady(doc);
  1082. } else {
  1083. doc.isReady && doReady(doc);
  1084. if (browser.ie && browser.version != 11) {
  1085. (function () {
  1086. if (doc.isReady) return;
  1087. try {
  1088. doc.documentElement.doScroll("left");
  1089. } catch (error) {
  1090. setTimeout(arguments.callee, 0);
  1091. return;
  1092. }
  1093. doReady(doc);
  1094. })();
  1095. win.attachEvent('onload', function () {
  1096. doReady(doc)
  1097. });
  1098. } else {
  1099. doc.addEventListener("DOMContentLoaded", function () {
  1100. doc.removeEventListener("DOMContentLoaded", arguments.callee, false);
  1101. doReady(doc);
  1102. }, false);
  1103. win.addEventListener('load', function () {
  1104. doReady(doc)
  1105. }, false);
  1106. }
  1107. }
  1108. }
  1109. }(),
  1110. /**
  1111. * 动态添加css样式
  1112. * @method cssRule
  1113. * @param { String } 节点名称
  1114. * @grammar UE.utils.cssRule('添加的样式的节点名称',['样式','放到哪个document上'])
  1115. * @grammar UE.utils.cssRule('body','body{background:#ccc}') => null //给body添加背景颜色
  1116. * @grammar UE.utils.cssRule('body') =>样式的字符串 //取得key值为body的样式的内容,如果没有找到key值先关的样式将返回空,例如刚才那个背景颜色,将返回 body{background:#ccc}
  1117. * @grammar UE.utils.cssRule('body',document) => 返回指定key的样式,并且指定是哪个document
  1118. * @grammar UE.utils.cssRule('body','') =>null //清空给定的key值的背景颜色
  1119. */
  1120. cssRule: browser.ie && browser.version != 11 ? function (key, style, doc) {
  1121. var indexList, index;
  1122. if (style === undefined || style && style.nodeType && style.nodeType == 9) {
  1123. //获取样式
  1124. doc = style && style.nodeType && style.nodeType == 9 ? style : (doc || document);
  1125. indexList = doc.indexList || (doc.indexList = {});
  1126. index = indexList[key];
  1127. if (index !== undefined) {
  1128. return doc.styleSheets[index].cssText
  1129. }
  1130. return undefined;
  1131. }
  1132. doc = doc || document;
  1133. indexList = doc.indexList || (doc.indexList = {});
  1134. index = indexList[key];
  1135. //清除样式
  1136. if (style === '') {
  1137. if (index !== undefined) {
  1138. doc.styleSheets[index].cssText = '';
  1139. delete indexList[key];
  1140. return true
  1141. }
  1142. return false;
  1143. }
  1144. //添加样式
  1145. if (index !== undefined) {
  1146. sheetStyle = doc.styleSheets[index];
  1147. } else {
  1148. sheetStyle = doc.createStyleSheet('', index = doc.styleSheets.length);
  1149. indexList[key] = index;
  1150. }
  1151. sheetStyle.cssText = style;
  1152. } : function (key, style, doc) {
  1153. var head, node;
  1154. if (style === undefined || style && style.nodeType && style.nodeType == 9) {
  1155. //获取样式
  1156. doc = style && style.nodeType && style.nodeType == 9 ? style : (doc || document);
  1157. node = doc.getElementById(key);
  1158. return node ? node.innerHTML : undefined;
  1159. }
  1160. doc = doc || document;
  1161. node = doc.getElementById(key);
  1162. //清除样式
  1163. if (style === '') {
  1164. if (node) {
  1165. node.parentNode.removeChild(node);
  1166. return true
  1167. }
  1168. return false;
  1169. }
  1170. //添加样式
  1171. if (node) {
  1172. node.innerHTML = style;
  1173. } else {
  1174. node = doc.createElement('style');
  1175. node.id = key;
  1176. node.innerHTML = style;
  1177. doc.getElementsByTagName('head')[0].appendChild(node);
  1178. }
  1179. },
  1180. sort: function (array, compareFn) {
  1181. compareFn = compareFn || function (item1, item2) {
  1182. return item1.localeCompare(item2);
  1183. };
  1184. for (var i = 0, len = array.length; i < len; i++) {
  1185. for (var j = i, length = array.length; j < length; j++) {
  1186. if (compareFn(array[i], array[j]) > 0) {
  1187. var t = array[i];
  1188. array[i] = array[j];
  1189. array[j] = t;
  1190. }
  1191. }
  1192. }
  1193. return array;
  1194. },
  1195. serializeParam: function (json) {
  1196. var strArr = [];
  1197. for (var i in json) {
  1198. //忽略默认的几个参数
  1199. if (i == "method" || i == "timeout" || i == "async") continue;
  1200. //传递过来的对象和函数不在提交之列
  1201. if (!((typeof json[i]).toLowerCase() == "function" || (typeof json[i]).toLowerCase() == "object")) {
  1202. strArr.push(encodeURIComponent(i) + "=" + encodeURIComponent(json[i]));
  1203. } else if (utils.isArray(json[i])) {
  1204. //支持传数组内容
  1205. for (var j = 0; j < json[i].length; j++) {
  1206. strArr.push(encodeURIComponent(i) + "[]=" + encodeURIComponent(json[i][j]));
  1207. }
  1208. }
  1209. }
  1210. return strArr.join("&");
  1211. },
  1212. formatUrl: function (url) {
  1213. var u = url.replace(/&&/g, '&');
  1214. u = u.replace(/\?&/g, '?');
  1215. u = u.replace(/&$/g, '');
  1216. u = u.replace(/&#/g, '#');
  1217. u = u.replace(/&+/g, '&');
  1218. return u;
  1219. },
  1220. isCrossDomainUrl: function (url) {
  1221. var a = document.createElement('a');
  1222. a.href = url;
  1223. if (browser.ie) {
  1224. a.href = a.href;
  1225. }
  1226. return !(a.protocol == location.protocol && a.hostname == location.hostname &&
  1227. (a.port == location.port || (a.port == '80' && location.port == '') || (a.port == '' && location.port == '80')));
  1228. },
  1229. clearEmptyAttrs: function (obj) {
  1230. for (var p in obj) {
  1231. if (obj[p] === '') {
  1232. delete obj[p]
  1233. }
  1234. }
  1235. return obj;
  1236. },
  1237. str2json: function (s) {
  1238. if (!utils.isString(s)) return null;
  1239. if (window.JSON) {
  1240. return JSON.parse(s);
  1241. } else {
  1242. return (new Function("return " + utils.trim(s || '')))();
  1243. }
  1244. },
  1245. json2str: (function () {
  1246. if (window.JSON) {
  1247. return JSON.stringify;
  1248. } else {
  1249. var escapeMap = {
  1250. "\b": '\\b',
  1251. "\t": '\\t',
  1252. "\n": '\\n',
  1253. "\f": '\\f',
  1254. "\r": '\\r',
  1255. '"': '\\"',
  1256. "\\": '\\\\'
  1257. };
  1258. function encodeString(source) {
  1259. if (/["\\\x00-\x1f]/.test(source)) {
  1260. source = source.replace(
  1261. /["\\\x00-\x1f]/g,
  1262. function (match) {
  1263. var c = escapeMap[match];
  1264. if (c) {
  1265. return c;
  1266. }
  1267. c = match.charCodeAt();
  1268. return "\\u00" +
  1269. Math.floor(c / 16).toString(16) +
  1270. (c % 16).toString(16);
  1271. });
  1272. }
  1273. return '"' + source + '"';
  1274. }
  1275. function encodeArray(source) {
  1276. var result = ["["],
  1277. l = source.length,
  1278. preComma, i, item;
  1279. for (i = 0; i < l; i++) {
  1280. item = source[i];
  1281. switch (typeof item) {
  1282. case "undefined":
  1283. case "function":
  1284. case "unknown":
  1285. break;
  1286. default:
  1287. if (preComma) {
  1288. result.push(',');
  1289. }
  1290. result.push(utils.json2str(item));
  1291. preComma = 1;
  1292. }
  1293. }
  1294. result.push("]");
  1295. return result.join("");
  1296. }
  1297. function pad(source) {
  1298. return source < 10 ? '0' + source : source;
  1299. }
  1300. function encodeDate(source) {
  1301. return '"' + source.getFullYear() + "-" +
  1302. pad(source.getMonth() + 1) + "-" +
  1303. pad(source.getDate()) + "T" +
  1304. pad(source.getHours()) + ":" +
  1305. pad(source.getMinutes()) + ":" +
  1306. pad(source.getSeconds()) + '"';
  1307. }
  1308. return function (value) {
  1309. switch (typeof value) {
  1310. case 'undefined':
  1311. return 'undefined';
  1312. case 'number':
  1313. return isFinite(value) ? String(value) : "null";
  1314. case 'string':
  1315. return encodeString(value);
  1316. case 'boolean':
  1317. return String(value);
  1318. default:
  1319. if (value === null) {
  1320. return 'null';
  1321. } else if (utils.isArray(value)) {
  1322. return encodeArray(value);
  1323. } else if (utils.isDate(value)) {
  1324. return encodeDate(value);
  1325. } else {
  1326. var result = ['{'],
  1327. encode = utils.json2str,
  1328. preComma,
  1329. item;
  1330. for (var key in value) {
  1331. if (Object.prototype.hasOwnProperty.call(value, key)) {
  1332. item = value[key];
  1333. switch (typeof item) {
  1334. case 'undefined':
  1335. case 'unknown':
  1336. case 'function':
  1337. break;
  1338. default:
  1339. if (preComma) {
  1340. result.push(',');
  1341. }
  1342. preComma = 1;
  1343. result.push(encode(key) + ':' + encode(item));
  1344. }
  1345. }
  1346. }
  1347. result.push('}');
  1348. return result.join('');
  1349. }
  1350. }
  1351. };
  1352. }
  1353. })()
  1354. };
  1355. /**
  1356. * 判断给定的对象是否是字符串
  1357. * @method isString
  1358. * @param { * } object 需要判断的对象
  1359. * @return { Boolean } 给定的对象是否是字符串
  1360. */
  1361. /**
  1362. * 判断给定的对象是否是数组
  1363. * @method isArray
  1364. * @param { * } object 需要判断的对象
  1365. * @return { Boolean } 给定的对象是否是数组
  1366. */
  1367. /**
  1368. * 判断给定的对象是否是一个Function
  1369. * @method isFunction
  1370. * @param { * } object 需要判断的对象
  1371. * @return { Boolean } 给定的对象是否是Function
  1372. */
  1373. /**
  1374. * 判断给定的对象是否是Number
  1375. * @method isNumber
  1376. * @param { * } object 需要判断的对象
  1377. * @return { Boolean } 给定的对象是否是Number
  1378. */
  1379. /**
  1380. * 判断给定的对象是否是一个正则表达式
  1381. * @method isRegExp
  1382. * @param { * } object 需要判断的对象
  1383. * @return { Boolean } 给定的对象是否是正则表达式
  1384. */
  1385. /**
  1386. * 判断给定的对象是否是一个普通对象
  1387. * @method isObject
  1388. * @param { * } object 需要判断的对象
  1389. * @return { Boolean } 给定的对象是否是普通对象
  1390. */
  1391. utils.each(['String', 'Function', 'Array', 'Number', 'RegExp', 'Object', 'Date'], function (v) {
  1392. UE.utils['is' + v] = function (obj) {
  1393. return Object.prototype.toString.apply(obj) == '[object ' + v + ']';
  1394. }
  1395. });
  1396. // core/EventBase.js
  1397. /**
  1398. * UE采用的事件基类
  1399. * @file
  1400. * @module UE
  1401. * @class EventBase
  1402. * @since 1.2.6.1
  1403. */
  1404. /**
  1405. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  1406. * @unfile
  1407. * @module UE
  1408. */
  1409. /**
  1410. * UE采用的事件基类,继承此类的对应类将获取addListener,removeListener,fireEvent方法。
  1411. * 在UE中,Editor以及所有ui实例都继承了该类,故可以在对应的ui对象以及editor对象上使用上述方法。
  1412. * @unfile
  1413. * @module UE
  1414. * @class EventBase
  1415. */
  1416. /**
  1417. * 通过此构造器,子类可以继承EventBase获取事件监听的方法
  1418. * @constructor
  1419. * @example
  1420. * ```javascript
  1421. * UE.EventBase.call(editor);
  1422. * ```
  1423. */
  1424. var EventBase = UE.EventBase = function () {};
  1425. EventBase.prototype = {
  1426. /**
  1427. * 注册事件监听器
  1428. * @method addListener
  1429. * @param { String } types 监听的事件名称,同时监听多个事件使用空格分隔
  1430. * @param { Function } fn 监听的事件被触发时,会执行该回调函数
  1431. * @waining 事件被触发时,监听的函数假如返回的值恒等于true,回调函数的队列中后面的函数将不执行
  1432. * @example
  1433. * ```javascript
  1434. * editor.addListener('selectionchange',function(){
  1435. * console.log("选区已经变化!");
  1436. * })
  1437. * editor.addListener('beforegetcontent aftergetcontent',function(type){
  1438. * if(type == 'beforegetcontent'){
  1439. * //do something
  1440. * }else{
  1441. * //do something
  1442. * }
  1443. * console.log(this.getContent) // this是注册的事件的编辑器实例
  1444. * })
  1445. * ```
  1446. * @see UE.EventBase:fireEvent(String)
  1447. */
  1448. addListener: function (types, listener) {
  1449. types = utils.trim(types).split(/\s+/);
  1450. for (var i = 0, ti; ti = types[i++];) {
  1451. getListener(this, ti, true).push(listener);
  1452. }
  1453. },
  1454. on: function (types, listener) {
  1455. return this.addListener(types, listener);
  1456. },
  1457. off: function (types, listener) {
  1458. return this.removeListener(types, listener)
  1459. },
  1460. trigger: function () {
  1461. return this.fireEvent.apply(this, arguments);
  1462. },
  1463. /**
  1464. * 移除事件监听器
  1465. * @method removeListener
  1466. * @param { String } types 移除的事件名称,同时移除多个事件使用空格分隔
  1467. * @param { Function } fn 移除监听事件的函数引用
  1468. * @example
  1469. * ```javascript
  1470. * //changeCallback为方法体
  1471. * editor.removeListener("selectionchange",changeCallback);
  1472. * ```
  1473. */
  1474. removeListener: function (types, listener) {
  1475. types = utils.trim(types).split(/\s+/);
  1476. for (var i = 0, ti; ti = types[i++];) {
  1477. utils.removeItem(getListener(this, ti) || [], listener);
  1478. }
  1479. },
  1480. /**
  1481. * 触发事件
  1482. * @method fireEvent
  1483. * @param { String } types 触发的事件名称,同时触发多个事件使用空格分隔
  1484. * @remind 该方法会触发addListener
  1485. * @return { * } 返回触发事件的队列中,最后执行的回调函数的返回值
  1486. * @example
  1487. * ```javascript
  1488. * editor.fireEvent("selectionchange");
  1489. * ```
  1490. */
  1491. /**
  1492. * 触发事件
  1493. * @method fireEvent
  1494. * @param { String } types 触发的事件名称,同时触发多个事件使用空格分隔
  1495. * @param { *... } options 可选参数,可以传入一个或多个参数,会传给事件触发的回调函数
  1496. * @return { * } 返回触发事件的队列中,最后执行的回调函数的返回值
  1497. * @example
  1498. * ```javascript
  1499. *
  1500. * editor.addListener( "selectionchange", function ( type, arg1, arg2 ) {
  1501. *
  1502. * console.log( arg1 + " " + arg2 );
  1503. *
  1504. * } );
  1505. *
  1506. * //触发selectionchange事件, 会执行上面的事件监听器
  1507. * //output: Hello World
  1508. * editor.fireEvent("selectionchange", "Hello", "World");
  1509. * ```
  1510. */
  1511. fireEvent: function () {
  1512. var types = arguments[0];
  1513. types = utils.trim(types).split(' ');
  1514. for (var i = 0, ti; ti = types[i++];) {
  1515. var listeners = getListener(this, ti),
  1516. r, t, k;
  1517. if (listeners) {
  1518. k = listeners.length;
  1519. while (k--) {
  1520. if (!listeners[k]) continue;
  1521. t = listeners[k].apply(this, arguments);
  1522. if (t === true) {
  1523. return t;
  1524. }
  1525. if (t !== undefined) {
  1526. r = t;
  1527. }
  1528. }
  1529. }
  1530. if (t = this['on' + ti.toLowerCase()]) {
  1531. r = t.apply(this, arguments);
  1532. }
  1533. }
  1534. return r;
  1535. }
  1536. };
  1537. /**
  1538. * 获得对象所拥有监听类型的所有监听器
  1539. * @unfile
  1540. * @module UE
  1541. * @since 1.2.6.1
  1542. * @method getListener
  1543. * @public
  1544. * @param { Object } obj 查询监听器的对象
  1545. * @param { String } type 事件类型
  1546. * @param { Boolean } force 为true且当前所有type类型的侦听器不存在时,创建一个空监听器数组
  1547. * @return { Array } 监听器数组
  1548. */
  1549. function getListener(obj, type, force) {
  1550. var allListeners;
  1551. type = type.toLowerCase();
  1552. return ((allListeners = (obj.__allListeners || force && (obj.__allListeners = {}))) &&
  1553. (allListeners[type] || force && (allListeners[type] = [])));
  1554. }
  1555. // core/dtd.js
  1556. ///import editor.js
  1557. ///import core/dom/dom.js
  1558. ///import core/utils.js
  1559. /**
  1560. * dtd html语义化的体现类
  1561. * @constructor
  1562. * @namespace dtd
  1563. */
  1564. var dtd = dom.dtd = (function () {
  1565. function _(s) {
  1566. for (var k in s) {
  1567. s[k.toUpperCase()] = s[k];
  1568. }
  1569. return s;
  1570. }
  1571. var X = utils.extend2;
  1572. var A = _({
  1573. isindex: 1,
  1574. fieldset: 1
  1575. }),
  1576. B = _({
  1577. input: 1,
  1578. button: 1,
  1579. select: 1,
  1580. textarea: 1,
  1581. label: 1
  1582. }),
  1583. C = X(_({
  1584. a: 1
  1585. }), B),
  1586. D = X({
  1587. iframe: 1
  1588. }, C),
  1589. E = _({
  1590. hr: 1,
  1591. ul: 1,
  1592. menu: 1,
  1593. div: 1,
  1594. blockquote: 1,
  1595. noscript: 1,
  1596. table: 1,
  1597. center: 1,
  1598. address: 1,
  1599. dir: 1,
  1600. pre: 1,
  1601. h5: 1,
  1602. dl: 1,
  1603. h4: 1,
  1604. noframes: 1,
  1605. h6: 1,
  1606. ol: 1,
  1607. h1: 1,
  1608. h3: 1,
  1609. h2: 1
  1610. }),
  1611. F = _({
  1612. ins: 1,
  1613. del: 1,
  1614. script: 1,
  1615. style: 1
  1616. }),
  1617. G = X(_({
  1618. b: 1,
  1619. acronym: 1,
  1620. bdo: 1,
  1621. 'var': 1,
  1622. '#': 1,
  1623. abbr: 1,
  1624. code: 1,
  1625. br: 1,
  1626. i: 1,
  1627. cite: 1,
  1628. kbd: 1,
  1629. u: 1,
  1630. strike: 1,
  1631. s: 1,
  1632. tt: 1,
  1633. strong: 1,
  1634. q: 1,
  1635. samp: 1,
  1636. em: 1,
  1637. dfn: 1,
  1638. span: 1
  1639. }), F),
  1640. H = X(_({
  1641. sub: 1,
  1642. img: 1,
  1643. embed: 1,
  1644. object: 1,
  1645. sup: 1,
  1646. basefont: 1,
  1647. map: 1,
  1648. applet: 1,
  1649. font: 1,
  1650. big: 1,
  1651. small: 1
  1652. }), G),
  1653. I = X(_({
  1654. p: 1
  1655. }), H),
  1656. J = X(_({
  1657. iframe: 1
  1658. }), H, B),
  1659. K = _({
  1660. img: 1,
  1661. embed: 1,
  1662. noscript: 1,
  1663. br: 1,
  1664. kbd: 1,
  1665. center: 1,
  1666. button: 1,
  1667. basefont: 1,
  1668. h5: 1,
  1669. h4: 1,
  1670. samp: 1,
  1671. h6: 1,
  1672. ol: 1,
  1673. h1: 1,
  1674. h3: 1,
  1675. h2: 1,
  1676. form: 1,
  1677. font: 1,
  1678. '#': 1,
  1679. select: 1,
  1680. menu: 1,
  1681. ins: 1,
  1682. abbr: 1,
  1683. label: 1,
  1684. code: 1,
  1685. table: 1,
  1686. script: 1,
  1687. cite: 1,
  1688. input: 1,
  1689. iframe: 1,
  1690. strong: 1,
  1691. textarea: 1,
  1692. noframes: 1,
  1693. big: 1,
  1694. small: 1,
  1695. span: 1,
  1696. hr: 1,
  1697. sub: 1,
  1698. bdo: 1,
  1699. 'var': 1,
  1700. div: 1,
  1701. object: 1,
  1702. sup: 1,
  1703. strike: 1,
  1704. dir: 1,
  1705. map: 1,
  1706. dl: 1,
  1707. applet: 1,
  1708. del: 1,
  1709. isindex: 1,
  1710. fieldset: 1,
  1711. ul: 1,
  1712. b: 1,
  1713. acronym: 1,
  1714. a: 1,
  1715. blockquote: 1,
  1716. i: 1,
  1717. u: 1,
  1718. s: 1,
  1719. tt: 1,
  1720. address: 1,
  1721. q: 1,
  1722. pre: 1,
  1723. p: 1,
  1724. em: 1,
  1725. dfn: 1
  1726. }),
  1727. L = X(_({
  1728. a: 0
  1729. }), J), //a不能被切开,所以把他
  1730. M = _({
  1731. tr: 1
  1732. }),
  1733. N = _({
  1734. '#': 1
  1735. }),
  1736. O = X(_({
  1737. param: 1
  1738. }), K),
  1739. P = X(_({
  1740. form: 1
  1741. }), A, D, E, I),
  1742. Q = _({
  1743. li: 1,
  1744. ol: 1,
  1745. ul: 1
  1746. }),
  1747. R = _({
  1748. style: 1,
  1749. script: 1
  1750. }),
  1751. S = _({
  1752. base: 1,
  1753. link: 1,
  1754. meta: 1,
  1755. title: 1
  1756. }),
  1757. T = X(S, R),
  1758. U = _({
  1759. head: 1,
  1760. body: 1
  1761. }),
  1762. V = _({
  1763. html: 1
  1764. });
  1765. var block = _({
  1766. address: 1,
  1767. blockquote: 1,
  1768. center: 1,
  1769. dir: 1,
  1770. div: 1,
  1771. dl: 1,
  1772. fieldset: 1,
  1773. form: 1,
  1774. h1: 1,
  1775. h2: 1,
  1776. h3: 1,
  1777. h4: 1,
  1778. h5: 1,
  1779. h6: 1,
  1780. hr: 1,
  1781. isindex: 1,
  1782. menu: 1,
  1783. noframes: 1,
  1784. ol: 1,
  1785. p: 1,
  1786. pre: 1,
  1787. table: 1,
  1788. ul: 1
  1789. }),
  1790. empty = _({
  1791. area: 1,
  1792. base: 1,
  1793. basefont: 1,
  1794. br: 1,
  1795. col: 1,
  1796. command: 1,
  1797. dialog: 1,
  1798. embed: 1,
  1799. hr: 1,
  1800. img: 1,
  1801. input: 1,
  1802. isindex: 1,
  1803. keygen: 1,
  1804. link: 1,
  1805. meta: 1,
  1806. param: 1,
  1807. source: 1,
  1808. track: 1,
  1809. wbr: 1
  1810. });
  1811. return _({
  1812. // $ 表示自定的属性
  1813. // body外的元素列表.
  1814. $nonBodyContent: X(V, U, S),
  1815. //块结构元素列表
  1816. $block: block,
  1817. //内联元素列表
  1818. $inline: L,
  1819. $inlineWithA: X(_({
  1820. a: 1
  1821. }), L),
  1822. $body: X(_({
  1823. script: 1,
  1824. style: 1
  1825. }), block),
  1826. $cdata: _({
  1827. script: 1,
  1828. style: 1
  1829. }),
  1830. //自闭和元素
  1831. $empty: empty,
  1832. //不是自闭合,但不能让range选中里边
  1833. $nonChild: _({
  1834. iframe: 1,
  1835. textarea: 1
  1836. }),
  1837. //列表元素列表
  1838. $listItem: _({
  1839. dd: 1,
  1840. dt: 1,
  1841. li: 1
  1842. }),
  1843. //列表根元素列表
  1844. $list: _({
  1845. ul: 1,
  1846. ol: 1,
  1847. dl: 1
  1848. }),
  1849. //不能认为是空的元素
  1850. $isNotEmpty: _({
  1851. table: 1,
  1852. ul: 1,
  1853. ol: 1,
  1854. dl: 1,
  1855. iframe: 1,
  1856. area: 1,
  1857. base: 1,
  1858. col: 1,
  1859. hr: 1,
  1860. img: 1,
  1861. embed: 1,
  1862. input: 1,
  1863. link: 1,
  1864. meta: 1,
  1865. param: 1,
  1866. h1: 1,
  1867. h2: 1,
  1868. h3: 1,
  1869. h4: 1,
  1870. h5: 1,
  1871. h6: 1
  1872. }),
  1873. //如果没有子节点就可以删除的元素列表,像span,a
  1874. $removeEmpty: _({
  1875. a: 1,
  1876. abbr: 1,
  1877. acronym: 1,
  1878. address: 1,
  1879. b: 1,
  1880. bdo: 1,
  1881. big: 1,
  1882. cite: 1,
  1883. code: 1,
  1884. del: 1,
  1885. dfn: 1,
  1886. em: 1,
  1887. font: 1,
  1888. i: 1,
  1889. ins: 1,
  1890. label: 1,
  1891. kbd: 1,
  1892. q: 1,
  1893. s: 1,
  1894. samp: 1,
  1895. small: 1,
  1896. span: 1,
  1897. strike: 1,
  1898. strong: 1,
  1899. sub: 1,
  1900. sup: 1,
  1901. tt: 1,
  1902. u: 1,
  1903. 'var': 1
  1904. }),
  1905. $removeEmptyBlock: _({
  1906. 'p': 1,
  1907. 'div': 1
  1908. }),
  1909. //在table元素里的元素列表
  1910. $tableContent: _({
  1911. caption: 1,
  1912. col: 1,
  1913. colgroup: 1,
  1914. tbody: 1,
  1915. td: 1,
  1916. tfoot: 1,
  1917. th: 1,
  1918. thead: 1,
  1919. tr: 1,
  1920. table: 1
  1921. }),
  1922. //不转换的标签
  1923. $notTransContent: _({
  1924. pre: 1,
  1925. script: 1,
  1926. style: 1,
  1927. textarea: 1
  1928. }),
  1929. html: U,
  1930. head: T,
  1931. style: N,
  1932. script: N,
  1933. body: P,
  1934. base: {},
  1935. link: {},
  1936. meta: {},
  1937. title: N,
  1938. col: {},
  1939. tr: _({
  1940. td: 1,
  1941. th: 1
  1942. }),
  1943. img: {},
  1944. embed: {},
  1945. colgroup: _({
  1946. thead: 1,
  1947. col: 1,
  1948. tbody: 1,
  1949. tr: 1,
  1950. tfoot: 1
  1951. }),
  1952. noscript: P,
  1953. td: P,
  1954. br: {},
  1955. th: P,
  1956. center: P,
  1957. kbd: L,
  1958. button: X(I, E),
  1959. basefont: {},
  1960. h5: L,
  1961. h4: L,
  1962. samp: L,
  1963. h6: L,
  1964. ol: Q,
  1965. h1: L,
  1966. h3: L,
  1967. option: N,
  1968. h2: L,
  1969. form: X(A, D, E, I),
  1970. select: _({
  1971. optgroup: 1,
  1972. option: 1
  1973. }),
  1974. font: L,
  1975. ins: L,
  1976. menu: Q,
  1977. abbr: L,
  1978. label: L,
  1979. table: _({
  1980. thead: 1,
  1981. col: 1,
  1982. tbody: 1,
  1983. tr: 1,
  1984. colgroup: 1,
  1985. caption: 1,
  1986. tfoot: 1
  1987. }),
  1988. code: L,
  1989. tfoot: M,
  1990. cite: L,
  1991. li: P,
  1992. input: {},
  1993. iframe: P,
  1994. strong: L,
  1995. textarea: N,
  1996. noframes: P,
  1997. big: L,
  1998. small: L,
  1999. //trace:
  2000. span: _({
  2001. '#': 1,
  2002. br: 1,
  2003. b: 1,
  2004. strong: 1,
  2005. u: 1,
  2006. i: 1,
  2007. em: 1,
  2008. sub: 1,
  2009. sup: 1,
  2010. strike: 1,
  2011. span: 1
  2012. }),
  2013. hr: L,
  2014. dt: L,
  2015. sub: L,
  2016. optgroup: _({
  2017. option: 1
  2018. }),
  2019. param: {},
  2020. bdo: L,
  2021. 'var': L,
  2022. div: P,
  2023. object: O,
  2024. sup: L,
  2025. dd: P,
  2026. strike: L,
  2027. area: {},
  2028. dir: Q,
  2029. map: X(_({
  2030. area: 1,
  2031. form: 1,
  2032. p: 1
  2033. }), A, F, E),
  2034. applet: O,
  2035. dl: _({
  2036. dt: 1,
  2037. dd: 1
  2038. }),
  2039. del: L,
  2040. isindex: {},
  2041. fieldset: X(_({
  2042. legend: 1
  2043. }), K),
  2044. thead: M,
  2045. ul: Q,
  2046. acronym: L,
  2047. b: L,
  2048. a: X(_({
  2049. a: 1
  2050. }), J),
  2051. blockquote: X(_({
  2052. td: 1,
  2053. tr: 1,
  2054. tbody: 1,
  2055. li: 1
  2056. }), P),
  2057. caption: L,
  2058. i: L,
  2059. u: L,
  2060. tbody: M,
  2061. s: L,
  2062. address: X(D, I),
  2063. tt: L,
  2064. legend: L,
  2065. q: L,
  2066. pre: X(G, C),
  2067. p: X(_({
  2068. 'a': 1
  2069. }), L),
  2070. em: L,
  2071. dfn: L
  2072. });
  2073. })();
  2074. // core/domUtils.js
  2075. /**
  2076. * Dom操作工具包
  2077. * @file
  2078. * @module UE.dom.domUtils
  2079. * @since 1.2.6.1
  2080. */
  2081. /**
  2082. * Dom操作工具包
  2083. * @unfile
  2084. * @module UE.dom.domUtils
  2085. */
  2086. function getDomNode(node, start, ltr, startFromChild, fn, guard) {
  2087. var tmpNode = startFromChild && node[start],
  2088. parent;
  2089. !tmpNode && (tmpNode = node[ltr]);
  2090. while (!tmpNode && (parent = (parent || node).parentNode)) {
  2091. if (parent.tagName == 'BODY' || guard && !guard(parent)) {
  2092. return null;
  2093. }
  2094. tmpNode = parent[ltr];
  2095. }
  2096. if (tmpNode && fn && !fn(tmpNode)) {
  2097. return getDomNode(tmpNode, start, ltr, false, fn);
  2098. }
  2099. return tmpNode;
  2100. }
  2101. var attrFix = ie && browser.version < 9 ? {
  2102. tabindex: "tabIndex",
  2103. readonly: "readOnly",
  2104. "for": "htmlFor",
  2105. "class": "className",
  2106. maxlength: "maxLength",
  2107. cellspacing: "cellSpacing",
  2108. cellpadding: "cellPadding",
  2109. rowspan: "rowSpan",
  2110. colspan: "colSpan",
  2111. usemap: "useMap",
  2112. frameborder: "frameBorder"
  2113. } : {
  2114. tabindex: "tabIndex",
  2115. readonly: "readOnly"
  2116. },
  2117. styleBlock = utils.listToMap([
  2118. '-webkit-box', '-moz-box', 'block',
  2119. 'list-item', 'table', 'table-row-group',
  2120. 'table-header-group', 'table-footer-group',
  2121. 'table-row', 'table-column-group', 'table-column',
  2122. 'table-cell', 'table-caption'
  2123. ]);
  2124. var domUtils = dom.domUtils = {
  2125. //节点常量
  2126. NODE_ELEMENT: 1,
  2127. NODE_DOCUMENT: 9,
  2128. NODE_TEXT: 3,
  2129. NODE_COMMENT: 8,
  2130. NODE_DOCUMENT_FRAGMENT: 11,
  2131. //位置关系
  2132. POSITION_IDENTICAL: 0,
  2133. POSITION_DISCONNECTED: 1,
  2134. POSITION_FOLLOWING: 2,
  2135. POSITION_PRECEDING: 4,
  2136. POSITION_IS_CONTAINED: 8,
  2137. POSITION_CONTAINS: 16,
  2138. //ie6使用其他的会有一段空白出现
  2139. fillChar: ie && browser.version == '6' ? '\ufeff' : '\u200B',
  2140. //-------------------------Node部分--------------------------------
  2141. keys: {
  2142. /*Backspace*/
  2143. 8: 1,
  2144. /*Delete*/ 46: 1,
  2145. /*Shift*/
  2146. 16: 1,
  2147. /*Ctrl*/ 17: 1,
  2148. /*Alt*/ 18: 1,
  2149. 37: 1,
  2150. 38: 1,
  2151. 39: 1,
  2152. 40: 1,
  2153. 13: 1 /*enter*/
  2154. },
  2155. /**
  2156. * 获取节点A相对于节点B的位置关系
  2157. * @method getPosition
  2158. * @param { Node } nodeA 需要查询位置关系的节点A
  2159. * @param { Node } nodeB 需要查询位置关系的节点B
  2160. * @return { Number } 节点A与节点B的关系
  2161. * @example
  2162. * ```javascript
  2163. * //output: 20
  2164. * var position = UE.dom.domUtils.getPosition( document.documentElement, document.body );
  2165. *
  2166. * switch ( position ) {
  2167. *
  2168. * //0
  2169. * case UE.dom.domUtils.POSITION_IDENTICAL:
  2170. * console.log('元素相同');
  2171. * break;
  2172. * //1
  2173. * case UE.dom.domUtils.POSITION_DISCONNECTED:
  2174. * console.log('两个节点在不同的文档中');
  2175. * break;
  2176. * //2
  2177. * case UE.dom.domUtils.POSITION_FOLLOWING:
  2178. * console.log('节点A在节点B之后');
  2179. * break;
  2180. * //4
  2181. * case UE.dom.domUtils.POSITION_PRECEDING;
  2182. * console.log('节点A在节点B之前');
  2183. * break;
  2184. * //8
  2185. * case UE.dom.domUtils.POSITION_IS_CONTAINED:
  2186. * console.log('节点A被节点B包含');
  2187. * break;
  2188. * case 10:
  2189. * console.log('节点A被节点B包含且节点A在节点B之后');
  2190. * break;
  2191. * //16
  2192. * case UE.dom.domUtils.POSITION_CONTAINS:
  2193. * console.log('节点A包含节点B');
  2194. * break;
  2195. * case 20:
  2196. * console.log('节点A包含节点B且节点A在节点B之前');
  2197. * break;
  2198. *
  2199. * }
  2200. * ```
  2201. */
  2202. getPosition: function (nodeA, nodeB) {
  2203. // 如果两个节点是同一个节点
  2204. if (nodeA === nodeB) {
  2205. // domUtils.POSITION_IDENTICAL
  2206. return 0;
  2207. }
  2208. var node,
  2209. parentsA = [nodeA],
  2210. parentsB = [nodeB];
  2211. node = nodeA;
  2212. while (node = node.parentNode) {
  2213. // 如果nodeB是nodeA的祖先节点
  2214. if (node === nodeB) {
  2215. // domUtils.POSITION_IS_CONTAINED + domUtils.POSITION_FOLLOWING
  2216. return 10;
  2217. }
  2218. parentsA.push(node);
  2219. }
  2220. node = nodeB;
  2221. while (node = node.parentNode) {
  2222. // 如果nodeA是nodeB的祖先节点
  2223. if (node === nodeA) {
  2224. // domUtils.POSITION_CONTAINS + domUtils.POSITION_PRECEDING
  2225. return 20;
  2226. }
  2227. parentsB.push(node);
  2228. }
  2229. parentsA.reverse();
  2230. parentsB.reverse();
  2231. if (parentsA[0] !== parentsB[0]) {
  2232. // domUtils.POSITION_DISCONNECTED
  2233. return 1;
  2234. }
  2235. var i = -1;
  2236. while (i++, parentsA[i] === parentsB[i]) {}
  2237. nodeA = parentsA[i];
  2238. nodeB = parentsB[i];
  2239. while (nodeA = nodeA.nextSibling) {
  2240. if (nodeA === nodeB) {
  2241. // domUtils.POSITION_PRECEDING
  2242. return 4
  2243. }
  2244. }
  2245. // domUtils.POSITION_FOLLOWING
  2246. return 2;
  2247. },
  2248. /**
  2249. * 检测节点node在父节点中的索引位置
  2250. * @method getNodeIndex
  2251. * @param { Node } node 需要检测的节点对象
  2252. * @return { Number } 该节点在父节点中的位置
  2253. * @see UE.dom.domUtils.getNodeIndex(Node,Boolean)
  2254. */
  2255. /**
  2256. * 检测节点node在父节点中的索引位置, 根据给定的mergeTextNode参数决定是否要合并多个连续的文本节点为一个节点
  2257. * @method getNodeIndex
  2258. * @param { Node } node 需要检测的节点对象
  2259. * @param { Boolean } mergeTextNode 是否合并多个连续的文本节点为一个节点
  2260. * @return { Number } 该节点在父节点中的位置
  2261. * @example
  2262. * ```javascript
  2263. *
  2264. * var node = document.createElement("div");
  2265. *
  2266. * node.appendChild( document.createTextNode( "hello" ) );
  2267. * node.appendChild( document.createTextNode( "world" ) );
  2268. * node.appendChild( node = document.createElement( "div" ) );
  2269. *
  2270. * //output: 2
  2271. * console.log( UE.dom.domUtils.getNodeIndex( node ) );
  2272. *
  2273. * //output: 1
  2274. * console.log( UE.dom.domUtils.getNodeIndex( node, true ) );
  2275. *
  2276. * ```
  2277. */
  2278. getNodeIndex: function (node, ignoreTextNode) {
  2279. var preNode = node,
  2280. i = 0;
  2281. while (preNode = preNode.previousSibling) {
  2282. if (ignoreTextNode && preNode.nodeType == 3) {
  2283. if (preNode.nodeType != preNode.nextSibling.nodeType) {
  2284. i++;
  2285. }
  2286. continue;
  2287. }
  2288. i++;
  2289. }
  2290. return i;
  2291. },
  2292. /**
  2293. * 检测节点node是否在给定的document对象上
  2294. * @method inDoc
  2295. * @param { Node } node 需要检测的节点对象
  2296. * @param { DomDocument } doc 需要检测的document对象
  2297. * @return { Boolean } 该节点node是否在给定的document的dom树上
  2298. * @example
  2299. * ```javascript
  2300. *
  2301. * var node = document.createElement("div");
  2302. *
  2303. * //output: false
  2304. * console.log( UE.do.domUtils.inDoc( node, document ) );
  2305. *
  2306. * document.body.appendChild( node );
  2307. *
  2308. * //output: true
  2309. * console.log( UE.do.domUtils.inDoc( node, document ) );
  2310. *
  2311. * ```
  2312. */
  2313. inDoc: function (node, doc) {
  2314. return domUtils.getPosition(node, doc) == 10;
  2315. },
  2316. /**
  2317. * 根据给定的过滤规则filterFn, 查找符合该过滤规则的node节点的第一个祖先节点,
  2318. * 查找的起点是给定node节点的父节点。
  2319. * @method findParent
  2320. * @param { Node } node 需要查找的节点
  2321. * @param { Function } filterFn 自定义的过滤方法。
  2322. * @warning 查找的终点是到body节点为止
  2323. * @remind 自定义的过滤方法filterFn接受一个Node对象作为参数, 该对象代表当前执行检测的祖先节点。 如果该
  2324. * 节点满足过滤条件, 则要求返回true, 这时将直接返回该节点作为findParent()的结果, 否则, 请返回false。
  2325. * @return { Node | Null } 如果找到符合过滤条件的节点, 就返回该节点, 否则返回NULL
  2326. * @example
  2327. * ```javascript
  2328. * var filterNode = UE.dom.domUtils.findParent( document.body.firstChild, function ( node ) {
  2329. *
  2330. * //由于查找的终点是body节点, 所以永远也不会匹配当前过滤器的条件, 即这里永远会返回false
  2331. * return node.tagName === "HTML";
  2332. *
  2333. * } );
  2334. *
  2335. * //output: true
  2336. * console.log( filterNode === null );
  2337. * ```
  2338. */
  2339. /**
  2340. * 根据给定的过滤规则filterFn, 查找符合该过滤规则的node节点的第一个祖先节点,
  2341. * 如果includeSelf的值为true,则查找的起点是给定的节点node, 否则, 起点是node的父节点
  2342. * @method findParent
  2343. * @param { Node } node 需要查找的节点
  2344. * @param { Function } filterFn 自定义的过滤方法。
  2345. * @param { Boolean } includeSelf 查找过程是否包含自身
  2346. * @warning 查找的终点是到body节点为止
  2347. * @remind 自定义的过滤方法filterFn接受一个Node对象作为参数, 该对象代表当前执行检测的祖先节点。 如果该
  2348. * 节点满足过滤条件, 则要求返回true, 这时将直接返回该节点作为findParent()的结果, 否则, 请返回false。
  2349. * @remind 如果includeSelf为true, 则过滤器第一次执行时的参数会是节点本身。
  2350. * 反之, 过滤器第一次执行时的参数将是该节点的父节点。
  2351. * @return { Node | Null } 如果找到符合过滤条件的节点, 就返回该节点, 否则返回NULL
  2352. * @example
  2353. * ```html
  2354. * <body>
  2355. *
  2356. * <div id="test">
  2357. * </div>
  2358. *
  2359. * <script type="text/javascript">
  2360. *
  2361. * //output: DIV, BODY
  2362. * var filterNode = UE.dom.domUtils.findParent( document.getElementById( "test" ), function ( node ) {
  2363. *
  2364. * console.log( node.tagName );
  2365. * return false;
  2366. *
  2367. * }, true );
  2368. *
  2369. * </script>
  2370. * </body>
  2371. * ```
  2372. */
  2373. findParent: function (node, filterFn, includeSelf) {
  2374. if (node && !domUtils.isBody(node)) {
  2375. node = includeSelf ? node : node.parentNode;
  2376. while (node) {
  2377. if (!filterFn || filterFn(node) || domUtils.isBody(node)) {
  2378. return filterFn && !filterFn(node) && domUtils.isBody(node) ? null : node;
  2379. }
  2380. node = node.parentNode;
  2381. }
  2382. }
  2383. return null;
  2384. },
  2385. /**
  2386. * 查找node的节点名为tagName的第一个祖先节点, 查找的起点是node节点的父节点。
  2387. * @method findParentByTagName
  2388. * @param { Node } node 需要查找的节点对象
  2389. * @param { Array } tagNames 需要查找的父节点的名称数组
  2390. * @warning 查找的终点是到body节点为止
  2391. * @return { Node | NULL } 如果找到符合条件的节点, 则返回该节点, 否则返回NULL
  2392. * @example
  2393. * ```javascript
  2394. * var node = UE.dom.domUtils.findParentByTagName( document.getElementsByTagName("div")[0], [ "BODY" ] );
  2395. * //output: BODY
  2396. * console.log( node.tagName );
  2397. * ```
  2398. */
  2399. /**
  2400. * 查找node的节点名为tagName的祖先节点, 如果includeSelf的值为true,则查找的起点是给定的节点node,
  2401. * 否则, 起点是node的父节点。
  2402. * @method findParentByTagName
  2403. * @param { Node } node 需要查找的节点对象
  2404. * @param { Array } tagNames 需要查找的父节点的名称数组
  2405. * @param { Boolean } includeSelf 查找过程是否包含node节点自身
  2406. * @warning 查找的终点是到body节点为止
  2407. * @return { Node | NULL } 如果找到符合条件的节点, 则返回该节点, 否则返回NULL
  2408. * @example
  2409. * ```javascript
  2410. * var queryTarget = document.getElementsByTagName("div")[0];
  2411. * var node = UE.dom.domUtils.findParentByTagName( queryTarget, [ "DIV" ], true );
  2412. * //output: true
  2413. * console.log( queryTarget === node );
  2414. * ```
  2415. */
  2416. findParentByTagName: function (node, tagNames, includeSelf, excludeFn) {
  2417. tagNames = utils.listToMap(utils.isArray(tagNames) ? tagNames : [tagNames]);
  2418. return domUtils.findParent(node, function (node) {
  2419. return tagNames[node.tagName] && !(excludeFn && excludeFn(node));
  2420. }, includeSelf);
  2421. },
  2422. /**
  2423. * 查找节点node的祖先节点集合, 查找的起点是给定节点的父节点,结果集中不包含给定的节点。
  2424. * @method findParents
  2425. * @param { Node } node 需要查找的节点对象
  2426. * @return { Array } 给定节点的祖先节点数组
  2427. * @grammar UE.dom.domUtils.findParents(node) => Array //返回一个祖先节点数组集合,不包含自身
  2428. * @grammar UE.dom.domUtils.findParents(node,includeSelf) => Array //返回一个祖先节点数组集合,includeSelf指定是否包含自身
  2429. * @grammar UE.dom.domUtils.findParents(node,includeSelf,filterFn) => Array //返回一个祖先节点数组集合,filterFn指定过滤条件,返回true的node将被选取
  2430. * @grammar UE.dom.domUtils.findParents(node,includeSelf,filterFn,closerFirst) => Array //返回一个祖先节点数组集合,closerFirst为true的话,node的直接父亲节点是数组的第0个
  2431. */
  2432. /**
  2433. * 查找节点node的祖先节点集合, 如果includeSelf的值为true,
  2434. * 则返回的结果集中允许出现当前给定的节点, 否则, 该节点不会出现在其结果集中。
  2435. * @method findParents
  2436. * @param { Node } node 需要查找的节点对象
  2437. * @param { Boolean } includeSelf 查找的结果中是否允许包含当前查找的节点对象
  2438. * @return { Array } 给定节点的祖先节点数组
  2439. */
  2440. findParents: function (node, includeSelf, filterFn, closerFirst) {
  2441. var parents = includeSelf && (filterFn && filterFn(node) || !filterFn) ? [node] : [];
  2442. while (node = domUtils.findParent(node, filterFn)) {
  2443. parents.push(node);
  2444. }
  2445. return closerFirst ? parents : parents.reverse();
  2446. },
  2447. /**
  2448. * 在节点node后面插入新节点newNode
  2449. * @method insertAfter
  2450. * @param { Node } node 目标节点
  2451. * @param { Node } newNode 新插入的节点, 该节点将置于目标节点之后
  2452. * @return { Node } 新插入的节点
  2453. */
  2454. insertAfter: function (node, newNode) {
  2455. return node.nextSibling ? node.parentNode.insertBefore(newNode, node.nextSibling) :
  2456. node.parentNode.appendChild(newNode);
  2457. },
  2458. /**
  2459. * 删除节点node及其下属的所有节点
  2460. * @method remove
  2461. * @param { Node } node 需要删除的节点对象
  2462. * @return { Node } 返回刚删除的节点对象
  2463. * @example
  2464. * ```html
  2465. * <div id="test">
  2466. * <div id="child">你好</div>
  2467. * </div>
  2468. * <script>
  2469. * UE.dom.domUtils.remove( document.body, false );
  2470. * //output: false
  2471. * console.log( document.getElementById( "child" ) !== null );
  2472. * </script>
  2473. * ```
  2474. */
  2475. /**
  2476. * 删除节点node,并根据keepChildren的值决定是否保留子节点
  2477. * @method remove
  2478. * @param { Node } node 需要删除的节点对象
  2479. * @param { Boolean } keepChildren 是否需要保留子节点
  2480. * @return { Node } 返回刚删除的节点对象
  2481. * @example
  2482. * ```html
  2483. * <div id="test">
  2484. * <div id="child">你好</div>
  2485. * </div>
  2486. * <script>
  2487. * UE.dom.domUtils.remove( document.body, true );
  2488. * //output: true
  2489. * console.log( document.getElementById( "child" ) !== null );
  2490. * </script>
  2491. * ```
  2492. */
  2493. remove: function (node, keepChildren) {
  2494. var parent = node.parentNode,
  2495. child;
  2496. if (parent) {
  2497. if (keepChildren && node.hasChildNodes()) {
  2498. while (child = node.firstChild) {
  2499. parent.insertBefore(child, node);
  2500. }
  2501. }
  2502. parent.removeChild(node);
  2503. }
  2504. return node;
  2505. },
  2506. /**
  2507. * 取得node节点的下一个兄弟节点, 如果该节点其后没有兄弟节点, 则递归查找其父节点之后的第一个兄弟节点,
  2508. * 直到找到满足条件的节点或者递归到BODY节点之后才会结束。
  2509. * @method getNextDomNode
  2510. * @param { Node } node 需要获取其后的兄弟节点的节点对象
  2511. * @return { Node | NULL } 如果找满足条件的节点, 则返回该节点, 否则返回NULL
  2512. * @example
  2513. * ```html
  2514. * <body>
  2515. * <div id="test">
  2516. * <span></span>
  2517. * </div>
  2518. * <i>xxx</i>
  2519. * </body>
  2520. * <script>
  2521. *
  2522. * //output: i节点
  2523. * console.log( UE.dom.domUtils.getNextDomNode( document.getElementById( "test" ) ) );
  2524. *
  2525. * </script>
  2526. * ```
  2527. * @example
  2528. * ```html
  2529. * <body>
  2530. * <div>
  2531. * <span></span>
  2532. * <i id="test">xxx</i>
  2533. * </div>
  2534. * <b>xxx</b>
  2535. * </body>
  2536. * <script>
  2537. *
  2538. * //由于id为test的i节点之后没有兄弟节点, 则查找其父节点(div)后面的兄弟节点
  2539. * //output: b节点
  2540. * console.log( UE.dom.domUtils.getNextDomNode( document.getElementById( "test" ) ) );
  2541. *
  2542. * </script>
  2543. * ```
  2544. */
  2545. /**
  2546. * 取得node节点的下一个兄弟节点, 如果startFromChild的值为ture,则先获取其子节点,
  2547. * 如果有子节点则直接返回第一个子节点;如果没有子节点或者startFromChild的值为false,
  2548. * 则执行<a href="#UE.dom.domUtils.getNextDomNode(Node)">getNextDomNode(Node node)</a>的查找过程。
  2549. * @method getNextDomNode
  2550. * @param { Node } node 需要获取其后的兄弟节点的节点对象
  2551. * @param { Boolean } startFromChild 查找过程是否从其子节点开始
  2552. * @return { Node | NULL } 如果找满足条件的节点, 则返回该节点, 否则返回NULL
  2553. * @see UE.dom.domUtils.getNextDomNode(Node)
  2554. */
  2555. getNextDomNode: function (node, startFromChild, filterFn, guard) {
  2556. return getDomNode(node, 'firstChild', 'nextSibling', startFromChild, filterFn, guard);
  2557. },
  2558. getPreDomNode: function (node, startFromChild, filterFn, guard) {
  2559. return getDomNode(node, 'lastChild', 'previousSibling', startFromChild, filterFn, guard);
  2560. },
  2561. /**
  2562. * 检测节点node是否属是UEditor定义的bookmark节点
  2563. * @method isBookmarkNode
  2564. * @private
  2565. * @param { Node } node 需要检测的节点对象
  2566. * @return { Boolean } 是否是bookmark节点
  2567. * @example
  2568. * ```html
  2569. * <span id="_baidu_bookmark_1"></span>
  2570. * <script>
  2571. * var bookmarkNode = document.getElementById("_baidu_bookmark_1");
  2572. * //output: true
  2573. * console.log( UE.dom.domUtils.isBookmarkNode( bookmarkNode ) );
  2574. * </script>
  2575. * ```
  2576. */
  2577. isBookmarkNode: function (node) {
  2578. return node.nodeType == 1 && node.id && /^_baidu_bookmark_/i.test(node.id);
  2579. },
  2580. /**
  2581. * 获取节点node所属的window对象
  2582. * @method getWindow
  2583. * @param { Node } node 节点对象
  2584. * @return { Window } 当前节点所属的window对象
  2585. * @example
  2586. * ```javascript
  2587. * //output: true
  2588. * console.log( UE.dom.domUtils.getWindow( document.body ) === window );
  2589. * ```
  2590. */
  2591. getWindow: function (node) {
  2592. var doc = node.ownerDocument || node;
  2593. return doc.defaultView || doc.parentWindow;
  2594. },
  2595. /**
  2596. * 获取离nodeA与nodeB最近的公共的祖先节点
  2597. * @method getCommonAncestor
  2598. * @param { Node } nodeA 第一个节点
  2599. * @param { Node } nodeB 第二个节点
  2600. * @remind 如果给定的两个节点是同一个节点, 将直接返回该节点。
  2601. * @return { Node | NULL } 如果未找到公共节点, 返回NULL, 否则返回最近的公共祖先节点。
  2602. * @example
  2603. * ```javascript
  2604. * var commonAncestor = UE.dom.domUtils.getCommonAncestor( document.body, document.body.firstChild );
  2605. * //output: true
  2606. * console.log( commonAncestor.tagName.toLowerCase() === 'body' );
  2607. * ```
  2608. */
  2609. getCommonAncestor: function (nodeA, nodeB) {
  2610. if (nodeA === nodeB)
  2611. return nodeA;
  2612. var parentsA = [nodeA],
  2613. parentsB = [nodeB],
  2614. parent = nodeA,
  2615. i = -1;
  2616. while (parent = parent.parentNode) {
  2617. if (parent === nodeB) {
  2618. return parent;
  2619. }
  2620. parentsA.push(parent);
  2621. }
  2622. parent = nodeB;
  2623. while (parent = parent.parentNode) {
  2624. if (parent === nodeA)
  2625. return parent;
  2626. parentsB.push(parent);
  2627. }
  2628. parentsA.reverse();
  2629. parentsB.reverse();
  2630. while (i++, parentsA[i] === parentsB[i]) {}
  2631. return i == 0 ? null : parentsA[i - 1];
  2632. },
  2633. /**
  2634. * 清除node节点左右连续为空的兄弟inline节点
  2635. * @method clearEmptySibling
  2636. * @param { Node } node 执行的节点对象, 如果该节点的左右连续的兄弟节点是空的inline节点,
  2637. * 则这些兄弟节点将被删除
  2638. * @grammar UE.dom.domUtils.clearEmptySibling(node,ignoreNext) //ignoreNext指定是否忽略右边空节点
  2639. * @grammar UE.dom.domUtils.clearEmptySibling(node,ignoreNext,ignorePre) //ignorePre指定是否忽略左边空节点
  2640. * @example
  2641. * ```html
  2642. * <body>
  2643. * <div></div>
  2644. * <span id="test"></span>
  2645. * <i></i>
  2646. * <b></b>
  2647. * <em>xxx</em>
  2648. * <span></span>
  2649. * </body>
  2650. * <script>
  2651. *
  2652. * UE.dom.domUtils.clearEmptySibling( document.getElementById( "test" ) );
  2653. *
  2654. * //output: <div></div><span id="test"></span><em>xxx</em><span></span>
  2655. * console.log( document.body.innerHTML );
  2656. *
  2657. * </script>
  2658. * ```
  2659. */
  2660. /**
  2661. * 清除node节点左右连续为空的兄弟inline节点, 如果ignoreNext的值为true,
  2662. * 则忽略对右边兄弟节点的操作。
  2663. * @method clearEmptySibling
  2664. * @param { Node } node 执行的节点对象, 如果该节点的左右连续的兄弟节点是空的inline节点,
  2665. * @param { Boolean } ignoreNext 是否忽略忽略对右边的兄弟节点的操作
  2666. * 则这些兄弟节点将被删除
  2667. * @see UE.dom.domUtils.clearEmptySibling(Node)
  2668. */
  2669. /**
  2670. * 清除node节点左右连续为空的兄弟inline节点, 如果ignoreNext的值为true,
  2671. * 则忽略对右边兄弟节点的操作, 如果ignorePre的值为true,则忽略对左边兄弟节点的操作。
  2672. * @method clearEmptySibling
  2673. * @param { Node } node 执行的节点对象, 如果该节点的左右连续的兄弟节点是空的inline节点,
  2674. * @param { Boolean } ignoreNext 是否忽略忽略对右边的兄弟节点的操作
  2675. * @param { Boolean } ignorePre 是否忽略忽略对左边的兄弟节点的操作
  2676. * 则这些兄弟节点将被删除
  2677. * @see UE.dom.domUtils.clearEmptySibling(Node)
  2678. */
  2679. clearEmptySibling: function (node, ignoreNext, ignorePre) {
  2680. function clear(next, dir) {
  2681. var tmpNode;
  2682. while (next && !domUtils.isBookmarkNode(next) && (domUtils.isEmptyInlineElement(next)
  2683. //这里不能把空格算进来会吧空格干掉,出现文字间的空格丢掉了
  2684. ||
  2685. !new RegExp('[^\t\n\r' + domUtils.fillChar + ']').test(next.nodeValue))) {
  2686. tmpNode = next[dir];
  2687. domUtils.remove(next);
  2688. next = tmpNode;
  2689. }
  2690. }!ignoreNext && clear(node.nextSibling, 'nextSibling');
  2691. !ignorePre && clear(node.previousSibling, 'previousSibling');
  2692. },
  2693. /**
  2694. * 将一个文本节点textNode拆分成两个文本节点,offset指定拆分位置
  2695. * @method split
  2696. * @param { Node } textNode 需要拆分的文本节点对象
  2697. * @param { int } offset 需要拆分的位置, 位置计算从0开始
  2698. * @return { Node } 拆分后形成的新节点
  2699. * @example
  2700. * ```html
  2701. * <div id="test">abcdef</div>
  2702. * <script>
  2703. * var newNode = UE.dom.domUtils.split( document.getElementById( "test" ).firstChild, 3 );
  2704. * //output: def
  2705. * console.log( newNode.nodeValue );
  2706. * </script>
  2707. * ```
  2708. */
  2709. split: function (node, offset) {
  2710. var doc = node.ownerDocument;
  2711. if (browser.ie && offset == node.nodeValue.length) {
  2712. var next = doc.createTextNode('');
  2713. return domUtils.insertAfter(node, next);
  2714. }
  2715. var retval = node.splitText(offset);
  2716. //ie8下splitText不会跟新childNodes,我们手动触发他的更新
  2717. if (browser.ie8) {
  2718. var tmpNode = doc.createTextNode('');
  2719. domUtils.insertAfter(retval, tmpNode);
  2720. domUtils.remove(tmpNode);
  2721. }
  2722. return retval;
  2723. },
  2724. /**
  2725. * 检测文本节点textNode是否为空节点(包括空格、换行、占位符等字符)
  2726. * @method isWhitespace
  2727. * @param { Node } node 需要检测的节点对象
  2728. * @return { Boolean } 检测的节点是否为空
  2729. * @example
  2730. * ```html
  2731. * <div id="test">
  2732. *
  2733. * </div>
  2734. * <script>
  2735. * //output: true
  2736. * console.log( UE.dom.domUtils.isWhitespace( document.getElementById("test").firstChild ) );
  2737. * </script>
  2738. * ```
  2739. */
  2740. isWhitespace: function (node) {
  2741. return !new RegExp('[^ \t\n\r' + domUtils.fillChar + ']').test(node.nodeValue);
  2742. },
  2743. /**
  2744. * 获取元素element相对于viewport的位置坐标
  2745. * @method getXY
  2746. * @param { Node } element 需要计算位置的节点对象
  2747. * @return { Object } 返回形如{x:left,y:top}的一个key-value映射对象, 其中键x代表水平偏移距离,
  2748. * y代表垂直偏移距离。
  2749. *
  2750. * @example
  2751. * ```javascript
  2752. * var location = UE.dom.domUtils.getXY( document.getElementById("test") );
  2753. * //output: test的坐标为: 12, 24
  2754. * console.log( 'test的坐标为: ', location.x, ',', location.y );
  2755. * ```
  2756. */
  2757. getXY: function (element) {
  2758. var x = 0,
  2759. y = 0;
  2760. while (element.offsetParent) {
  2761. y += element.offsetTop;
  2762. x += element.offsetLeft;
  2763. element = element.offsetParent;
  2764. }
  2765. return {
  2766. 'x': x,
  2767. 'y': y
  2768. };
  2769. },
  2770. /**
  2771. * 为元素element绑定原生DOM事件,type为事件类型,handler为处理函数
  2772. * @method on
  2773. * @param { Node } element 需要绑定事件的节点对象
  2774. * @param { String } type 绑定的事件类型
  2775. * @param { Function } handler 事件处理器
  2776. * @example
  2777. * ```javascript
  2778. * UE.dom.domUtils.on(document.body,"click",function(e){
  2779. * //e为事件对象,this为被点击元素对戏那个
  2780. * });
  2781. * ```
  2782. */
  2783. /**
  2784. * 为元素element绑定原生DOM事件,type为事件类型,handler为处理函数
  2785. * @method on
  2786. * @param { Node } element 需要绑定事件的节点对象
  2787. * @param { Array } type 绑定的事件类型数组
  2788. * @param { Function } handler 事件处理器
  2789. * @example
  2790. * ```javascript
  2791. * UE.dom.domUtils.on(document.body,["click","mousedown"],function(evt){
  2792. * //evt为事件对象,this为被点击元素对象
  2793. * });
  2794. * ```
  2795. */
  2796. on: function (element, type, handler) {
  2797. var types = utils.isArray(type) ? type : utils.trim(type).split(/\s+/),
  2798. k = types.length;
  2799. if (k)
  2800. while (k--) {
  2801. type = types[k];
  2802. if (element.addEventListener) {
  2803. element.addEventListener(type, handler, false);
  2804. } else {
  2805. if (!handler._d) {
  2806. handler._d = {
  2807. els: []
  2808. };
  2809. }
  2810. var key = type + handler.toString(),
  2811. index = utils.indexOf(handler._d.els, element);
  2812. if (!handler._d[key] || index == -1) {
  2813. if (index == -1) {
  2814. handler._d.els.push(element);
  2815. }
  2816. if (!handler._d[key]) {
  2817. handler._d[key] = function (evt) {
  2818. return handler.call(evt.srcElement, evt || window.event);
  2819. };
  2820. }
  2821. element.attachEvent('on' + type, handler._d[key]);
  2822. }
  2823. }
  2824. }
  2825. element = null;
  2826. },
  2827. /**
  2828. * 解除DOM事件绑定
  2829. * @method un
  2830. * @param { Node } element 需要解除事件绑定的节点对象
  2831. * @param { String } type 需要接触绑定的事件类型
  2832. * @param { Function } handler 对应的事件处理器
  2833. * @example
  2834. * ```javascript
  2835. * UE.dom.domUtils.un(document.body,"click",function(evt){
  2836. * //evt为事件对象,this为被点击元素对象
  2837. * });
  2838. * ```
  2839. */
  2840. /**
  2841. * 解除DOM事件绑定
  2842. * @method un
  2843. * @param { Node } element 需要解除事件绑定的节点对象
  2844. * @param { Array } type 需要接触绑定的事件类型数组
  2845. * @param { Function } handler 对应的事件处理器
  2846. * @example
  2847. * ```javascript
  2848. * UE.dom.domUtils.un(document.body, ["click","mousedown"],function(evt){
  2849. * //evt为事件对象,this为被点击元素对象
  2850. * });
  2851. * ```
  2852. */
  2853. un: function (element, type, handler) {
  2854. var types = utils.isArray(type) ? type : utils.trim(type).split(/\s+/),
  2855. k = types.length;
  2856. if (k)
  2857. while (k--) {
  2858. type = types[k];
  2859. if (element.removeEventListener) {
  2860. element.removeEventListener(type, handler, false);
  2861. } else {
  2862. var key = type + handler.toString();
  2863. try {
  2864. element.detachEvent('on' + type, handler._d ? handler._d[key] : handler);
  2865. } catch (e) {}
  2866. if (handler._d && handler._d[key]) {
  2867. var index = utils.indexOf(handler._d.els, element);
  2868. if (index != -1) {
  2869. handler._d.els.splice(index, 1);
  2870. }
  2871. handler._d.els.length == 0 && delete handler._d[key];
  2872. }
  2873. }
  2874. }
  2875. },
  2876. /**
  2877. * 比较节点nodeA与节点nodeB是否具有相同的标签名、属性名以及属性值
  2878. * @method isSameElement
  2879. * @param { Node } nodeA 需要比较的节点
  2880. * @param { Node } nodeB 需要比较的节点
  2881. * @return { Boolean } 两个节点是否具有相同的标签名、属性名以及属性值
  2882. * @example
  2883. * ```html
  2884. * <span style="font-size:12px">ssss</span>
  2885. * <span style="font-size:12px">bbbbb</span>
  2886. * <span style="font-size:13px">ssss</span>
  2887. * <span style="font-size:14px">bbbbb</span>
  2888. *
  2889. * <script>
  2890. *
  2891. * var nodes = document.getElementsByTagName( "span" );
  2892. *
  2893. * //output: true
  2894. * console.log( UE.dom.domUtils.isSameElement( nodes[0], nodes[1] ) );
  2895. *
  2896. * //output: false
  2897. * console.log( UE.dom.domUtils.isSameElement( nodes[2], nodes[3] ) );
  2898. *
  2899. * </script>
  2900. * ```
  2901. */
  2902. isSameElement: function (nodeA, nodeB) {
  2903. if (nodeA.tagName != nodeB.tagName) {
  2904. return false;
  2905. }
  2906. var thisAttrs = nodeA.attributes,
  2907. otherAttrs = nodeB.attributes;
  2908. if (!ie && thisAttrs.length != otherAttrs.length) {
  2909. return false;
  2910. }
  2911. var attrA, attrB, al = 0,
  2912. bl = 0;
  2913. for (var i = 0; attrA = thisAttrs[i++];) {
  2914. if (attrA.nodeName == 'style') {
  2915. if (attrA.specified) {
  2916. al++;
  2917. }
  2918. if (domUtils.isSameStyle(nodeA, nodeB)) {
  2919. continue;
  2920. } else {
  2921. return false;
  2922. }
  2923. }
  2924. if (ie) {
  2925. if (attrA.specified) {
  2926. al++;
  2927. attrB = otherAttrs.getNamedItem(attrA.nodeName);
  2928. } else {
  2929. continue;
  2930. }
  2931. } else {
  2932. attrB = nodeB.attributes[attrA.nodeName];
  2933. }
  2934. if (!attrB.specified || attrA.nodeValue != attrB.nodeValue) {
  2935. return false;
  2936. }
  2937. }
  2938. // 有可能attrB的属性包含了attrA的属性之外还有自己的属性
  2939. if (ie) {
  2940. for (i = 0; attrB = otherAttrs[i++];) {
  2941. if (attrB.specified) {
  2942. bl++;
  2943. }
  2944. }
  2945. if (al != bl) {
  2946. return false;
  2947. }
  2948. }
  2949. return true;
  2950. },
  2951. /**
  2952. * 判断节点nodeA与节点nodeB的元素的style属性是否一致
  2953. * @method isSameStyle
  2954. * @param { Node } nodeA 需要比较的节点
  2955. * @param { Node } nodeB 需要比较的节点
  2956. * @return { Boolean } 两个节点是否具有相同的style属性值
  2957. * @example
  2958. * ```html
  2959. * <span style="font-size:12px">ssss</span>
  2960. * <span style="font-size:12px">bbbbb</span>
  2961. * <span style="font-size:13px">ssss</span>
  2962. * <span style="font-size:14px">bbbbb</span>
  2963. *
  2964. * <script>
  2965. *
  2966. * var nodes = document.getElementsByTagName( "span" );
  2967. *
  2968. * //output: true
  2969. * console.log( UE.dom.domUtils.isSameStyle( nodes[0], nodes[1] ) );
  2970. *
  2971. * //output: false
  2972. * console.log( UE.dom.domUtils.isSameStyle( nodes[2], nodes[3] ) );
  2973. *
  2974. * </script>
  2975. * ```
  2976. */
  2977. isSameStyle: function (nodeA, nodeB) {
  2978. var styleA = nodeA.style.cssText.replace(/( ?; ?)/g, ';').replace(/( ?: ?)/g, ':'),
  2979. styleB = nodeB.style.cssText.replace(/( ?; ?)/g, ';').replace(/( ?: ?)/g, ':');
  2980. if (browser.opera) {
  2981. styleA = nodeA.style;
  2982. styleB = nodeB.style;
  2983. if (styleA.length != styleB.length)
  2984. return false;
  2985. for (var p in styleA) {
  2986. if (/^(\d+|csstext)$/i.test(p)) {
  2987. continue;
  2988. }
  2989. if (styleA[p] != styleB[p]) {
  2990. return false;
  2991. }
  2992. }
  2993. return true;
  2994. }
  2995. if (!styleA || !styleB) {
  2996. return styleA == styleB;
  2997. }
  2998. styleA = styleA.split(';');
  2999. styleB = styleB.split(';');
  3000. if (styleA.length != styleB.length) {
  3001. return false;
  3002. }
  3003. for (var i = 0, ci; ci = styleA[i++];) {
  3004. if (utils.indexOf(styleB, ci) == -1) {
  3005. return false;
  3006. }
  3007. }
  3008. return true;
  3009. },
  3010. /**
  3011. * 检查节点node是否为block元素
  3012. * @method isBlockElm
  3013. * @param { Node } node 需要检测的节点对象
  3014. * @return { Boolean } 是否是block元素节点
  3015. * @warning 该方法的判断规则如下: 如果该元素原本是block元素, 则不论该元素当前的css样式是什么都会返回true;
  3016. * 否则,检测该元素的css样式, 如果该元素当前是block元素, 则返回true。 其余情况下都返回false。
  3017. * @example
  3018. * ```html
  3019. * <span id="test1" style="display: block"></span>
  3020. * <span id="test2"></span>
  3021. * <div id="test3" style="display: inline"></div>
  3022. *
  3023. * <script>
  3024. *
  3025. * //output: true
  3026. * console.log( UE.dom.domUtils.isBlockElm( document.getElementById("test1") ) );
  3027. *
  3028. * //output: false
  3029. * console.log( UE.dom.domUtils.isBlockElm( document.getElementById("test2") ) );
  3030. *
  3031. * //output: true
  3032. * console.log( UE.dom.domUtils.isBlockElm( document.getElementById("test3") ) );
  3033. *
  3034. * </script>
  3035. * ```
  3036. */
  3037. isBlockElm: function (node) {
  3038. return node.nodeType == 1 && (dtd.$block[node.tagName] || styleBlock[domUtils.getComputedStyle(node, 'display')]) && !dtd.$nonChild[node.tagName];
  3039. },
  3040. /**
  3041. * 检测node节点是否为body节点
  3042. * @method isBody
  3043. * @param { Element } node 需要检测的dom元素
  3044. * @return { Boolean } 给定的元素是否是body元素
  3045. * @example
  3046. * ```javascript
  3047. * //output: true
  3048. * console.log( UE.dom.domUtils.isBody( document.body ) );
  3049. * ```
  3050. */
  3051. isBody: function (node) {
  3052. return node && node.nodeType == 1 && node.tagName.toLowerCase() == 'body';
  3053. },
  3054. /**
  3055. * 以node节点为分界,将该节点的指定祖先节点parent拆分成两个独立的节点,
  3056. * 拆分形成的两个节点之间是node节点
  3057. * @method breakParent
  3058. * @param { Node } node 作为分界的节点对象
  3059. * @param { Node } parent 该节点必须是node节点的祖先节点, 且是block节点。
  3060. * @return { Node } 给定的node分界节点
  3061. * @example
  3062. * ```javascript
  3063. *
  3064. * var node = document.createElement("span"),
  3065. * wrapNode = document.createElement( "div" ),
  3066. * parent = document.createElement("p");
  3067. *
  3068. * parent.appendChild( node );
  3069. * wrapNode.appendChild( parent );
  3070. *
  3071. * //拆分前
  3072. * //output: <p><span></span></p>
  3073. * console.log( wrapNode.innerHTML );
  3074. *
  3075. *
  3076. * UE.dom.domUtils.breakParent( node, parent );
  3077. * //拆分后
  3078. * //output: <p></p><span></span><p></p>
  3079. * console.log( wrapNode.innerHTML );
  3080. *
  3081. * ```
  3082. */
  3083. breakParent: function (node, parent) {
  3084. var tmpNode,
  3085. parentClone = node,
  3086. clone = node,
  3087. leftNodes,
  3088. rightNodes;
  3089. do {
  3090. parentClone = parentClone.parentNode;
  3091. if (leftNodes) {
  3092. tmpNode = parentClone.cloneNode(false);
  3093. tmpNode.appendChild(leftNodes);
  3094. leftNodes = tmpNode;
  3095. tmpNode = parentClone.cloneNode(false);
  3096. tmpNode.appendChild(rightNodes);
  3097. rightNodes = tmpNode;
  3098. } else {
  3099. leftNodes = parentClone.cloneNode(false);
  3100. rightNodes = leftNodes.cloneNode(false);
  3101. }
  3102. while (tmpNode = clone.previousSibling) {
  3103. leftNodes.insertBefore(tmpNode, leftNodes.firstChild);
  3104. }
  3105. while (tmpNode = clone.nextSibling) {
  3106. rightNodes.appendChild(tmpNode);
  3107. }
  3108. clone = parentClone;
  3109. } while (parent !== parentClone);
  3110. tmpNode = parent.parentNode;
  3111. tmpNode.insertBefore(leftNodes, parent);
  3112. tmpNode.insertBefore(rightNodes, parent);
  3113. tmpNode.insertBefore(node, rightNodes);
  3114. domUtils.remove(parent);
  3115. return node;
  3116. },
  3117. /**
  3118. * 检查节点node是否是空inline节点
  3119. * @method isEmptyInlineElement
  3120. * @param { Node } node 需要检测的节点对象
  3121. * @return { Number } 如果给定的节点是空的inline节点, 则返回1, 否则返回0。
  3122. * @example
  3123. * ```html
  3124. * <b><i></i></b> => 1
  3125. * <b><i></i><u></u></b> => 1
  3126. * <b></b> => 1
  3127. * <b>xx<i></i></b> => 0
  3128. * ```
  3129. */
  3130. isEmptyInlineElement: function (node) {
  3131. if (node.nodeType != 1 || !dtd.$removeEmpty[node.tagName]) {
  3132. return 0;
  3133. }
  3134. node = node.firstChild;
  3135. while (node) {
  3136. //如果是创建的bookmark就跳过
  3137. if (domUtils.isBookmarkNode(node)) {
  3138. return 0;
  3139. }
  3140. if (node.nodeType == 1 && !domUtils.isEmptyInlineElement(node) ||
  3141. node.nodeType == 3 && !domUtils.isWhitespace(node)
  3142. ) {
  3143. return 0;
  3144. }
  3145. node = node.nextSibling;
  3146. }
  3147. return 1;
  3148. },
  3149. /**
  3150. * 删除node节点下首尾两端的空白文本子节点
  3151. * @method trimWhiteTextNode
  3152. * @param { Element } node 需要执行删除操作的元素对象
  3153. * @example
  3154. * ```javascript
  3155. * var node = document.createElement("div");
  3156. *
  3157. * node.appendChild( document.createTextNode( "" ) );
  3158. *
  3159. * node.appendChild( document.createElement("div") );
  3160. *
  3161. * node.appendChild( document.createTextNode( "" ) );
  3162. *
  3163. * //3
  3164. * console.log( node.childNodes.length );
  3165. *
  3166. * UE.dom.domUtils.trimWhiteTextNode( node );
  3167. *
  3168. * //1
  3169. * console.log( node.childNodes.length );
  3170. * ```
  3171. */
  3172. trimWhiteTextNode: function (node) {
  3173. function remove(dir) {
  3174. var child;
  3175. while ((child = node[dir]) && child.nodeType == 3 && domUtils.isWhitespace(child)) {
  3176. node.removeChild(child);
  3177. }
  3178. }
  3179. remove('firstChild');
  3180. remove('lastChild');
  3181. },
  3182. /**
  3183. * 合并node节点下相同的子节点
  3184. * @name mergeChild
  3185. * @desc
  3186. * UE.dom.domUtils.mergeChild(node,tagName) //tagName要合并的子节点的标签
  3187. * @example
  3188. * <p><span style="font-size:12px;">xx<span style="font-size:12px;">aa</span>xx</span></p>
  3189. * ==> UE.dom.domUtils.mergeChild(node,'span')
  3190. * <p><span style="font-size:12px;">xxaaxx</span></p>
  3191. */
  3192. mergeChild: function (node, tagName, attrs) {
  3193. var list = domUtils.getElementsByTagName(node, node.tagName.toLowerCase());
  3194. for (var i = 0, ci; ci = list[i++];) {
  3195. if (!ci.parentNode || domUtils.isBookmarkNode(ci)) {
  3196. continue;
  3197. }
  3198. //span单独处理
  3199. if (ci.tagName.toLowerCase() == 'span') {
  3200. if (node === ci.parentNode) {
  3201. domUtils.trimWhiteTextNode(node);
  3202. if (node.childNodes.length == 1) {
  3203. node.style.cssText = ci.style.cssText + ";" + node.style.cssText;
  3204. domUtils.remove(ci, true);
  3205. continue;
  3206. }
  3207. }
  3208. ci.style.cssText = node.style.cssText + ';' + ci.style.cssText;
  3209. if (attrs) {
  3210. var style = attrs.style;
  3211. if (style) {
  3212. style = style.split(';');
  3213. for (var j = 0, s; s = style[j++];) {
  3214. ci.style[utils.cssStyleToDomStyle(s.split(':')[0])] = s.split(':')[1];
  3215. }
  3216. }
  3217. }
  3218. if (domUtils.isSameStyle(ci, node)) {
  3219. domUtils.remove(ci, true);
  3220. }
  3221. continue;
  3222. }
  3223. if (domUtils.isSameElement(node, ci)) {
  3224. domUtils.remove(ci, true);
  3225. }
  3226. }
  3227. },
  3228. /**
  3229. * 原生方法getElementsByTagName的封装
  3230. * @method getElementsByTagName
  3231. * @param { Node } node 目标节点对象
  3232. * @param { String } tagName 需要查找的节点的tagName, 多个tagName以空格分割
  3233. * @return { Array } 符合条件的节点集合
  3234. */
  3235. getElementsByTagName: function (node, name, filter) {
  3236. if (filter && utils.isString(filter)) {
  3237. var className = filter;
  3238. filter = function (node) {
  3239. return domUtils.hasClass(node, className)
  3240. }
  3241. }
  3242. name = utils.trim(name).replace(/[ ]{2,}/g, ' ').split(' ');
  3243. var arr = [];
  3244. for (var n = 0, ni; ni = name[n++];) {
  3245. var list = node.getElementsByTagName(ni);
  3246. for (var i = 0, ci; ci = list[i++];) {
  3247. if (!filter || filter(ci))
  3248. arr.push(ci);
  3249. }
  3250. }
  3251. return arr;
  3252. },
  3253. /**
  3254. * 将节点node提取到父节点上
  3255. * @method mergeToParent
  3256. * @param { Element } node 需要提取的元素对象
  3257. * @example
  3258. * ```html
  3259. * <div id="parent">
  3260. * <div id="sub">
  3261. * <span id="child"></span>
  3262. * </div>
  3263. * </div>
  3264. *
  3265. * <script>
  3266. *
  3267. * var child = document.getElementById( "child" );
  3268. *
  3269. * //output: sub
  3270. * console.log( child.parentNode.id );
  3271. *
  3272. * UE.dom.domUtils.mergeToParent( child );
  3273. *
  3274. * //output: parent
  3275. * console.log( child.parentNode.id );
  3276. *
  3277. * </script>
  3278. * ```
  3279. */
  3280. mergeToParent: function (node) {
  3281. var parent = node.parentNode;
  3282. while (parent && dtd.$removeEmpty[parent.tagName]) {
  3283. if (parent.tagName == node.tagName || parent.tagName == 'A') { //针对a标签单独处理
  3284. domUtils.trimWhiteTextNode(parent);
  3285. //span需要特殊处理 不处理这样的情况 <span stlye="color:#fff">xxx<span style="color:#ccc">xxx</span>xxx</span>
  3286. if (parent.tagName == 'SPAN' && !domUtils.isSameStyle(parent, node) ||
  3287. (parent.tagName == 'A' && node.tagName == 'SPAN')) {
  3288. if (parent.childNodes.length > 1 || parent !== node.parentNode) {
  3289. node.style.cssText = parent.style.cssText + ";" + node.style.cssText;
  3290. parent = parent.parentNode;
  3291. continue;
  3292. } else {
  3293. parent.style.cssText += ";" + node.style.cssText;
  3294. //trace:952 a标签要保持下划线
  3295. if (parent.tagName == 'A') {
  3296. parent.style.textDecoration = 'underline';
  3297. }
  3298. }
  3299. }
  3300. if (parent.tagName != 'A') {
  3301. parent === node.parentNode && domUtils.remove(node, true);
  3302. break;
  3303. }
  3304. }
  3305. parent = parent.parentNode;
  3306. }
  3307. },
  3308. /**
  3309. * 合并节点node的左右兄弟节点
  3310. * @method mergeSibling
  3311. * @param { Element } node 需要合并的目标节点
  3312. * @example
  3313. * ```html
  3314. * <b>xxxx</b><b id="test">ooo</b><b>xxxx</b>
  3315. *
  3316. * <script>
  3317. * var demoNode = document.getElementById("test");
  3318. * UE.dom.domUtils.mergeSibling( demoNode );
  3319. * //output: xxxxoooxxxx
  3320. * console.log( demoNode.innerHTML );
  3321. * </script>
  3322. * ```
  3323. */
  3324. /**
  3325. * 合并节点node的左右兄弟节点, 可以根据给定的条件选择是否忽略合并左节点。
  3326. * @method mergeSibling
  3327. * @param { Element } node 需要合并的目标节点
  3328. * @param { Boolean } ignorePre 是否忽略合并左节点
  3329. * @example
  3330. * ```html
  3331. * <b>xxxx</b><b id="test">ooo</b><b>xxxx</b>
  3332. *
  3333. * <script>
  3334. * var demoNode = document.getElementById("test");
  3335. * UE.dom.domUtils.mergeSibling( demoNode, true );
  3336. * //output: oooxxxx
  3337. * console.log( demoNode.innerHTML );
  3338. * </script>
  3339. * ```
  3340. */
  3341. /**
  3342. * 合并节点node的左右兄弟节点,可以根据给定的条件选择是否忽略合并左右节点。
  3343. * @method mergeSibling
  3344. * @param { Element } node 需要合并的目标节点
  3345. * @param { Boolean } ignorePre 是否忽略合并左节点
  3346. * @param { Boolean } ignoreNext 是否忽略合并右节点
  3347. * @remind 如果同时忽略左右节点, 则该操作什么也不会做
  3348. * @example
  3349. * ```html
  3350. * <b>xxxx</b><b id="test">ooo</b><b>xxxx</b>
  3351. *
  3352. * <script>
  3353. * var demoNode = document.getElementById("test");
  3354. * UE.dom.domUtils.mergeSibling( demoNode, false, true );
  3355. * //output: xxxxooo
  3356. * console.log( demoNode.innerHTML );
  3357. * </script>
  3358. * ```
  3359. */
  3360. mergeSibling: function (node, ignorePre, ignoreNext) {
  3361. function merge(rtl, start, node) {
  3362. var next;
  3363. if ((next = node[rtl]) && !domUtils.isBookmarkNode(next) && next.nodeType == 1 && domUtils.isSameElement(node, next)) {
  3364. while (next.firstChild) {
  3365. if (start == 'firstChild') {
  3366. node.insertBefore(next.lastChild, node.firstChild);
  3367. } else {
  3368. node.appendChild(next.firstChild);
  3369. }
  3370. }
  3371. domUtils.remove(next);
  3372. }
  3373. }!ignorePre && merge('previousSibling', 'firstChild', node);
  3374. !ignoreNext && merge('nextSibling', 'lastChild', node);
  3375. },
  3376. /**
  3377. * 设置节点node及其子节点不会被选中
  3378. * @method unSelectable
  3379. * @param { Element } node 需要执行操作的dom元素
  3380. * @remind 执行该操作后的节点, 将不能被鼠标选中
  3381. * @example
  3382. * ```javascript
  3383. * UE.dom.domUtils.unSelectable( document.body );
  3384. * ```
  3385. */
  3386. unSelectable: ie && browser.ie9below || browser.opera ? function (node) {
  3387. //for ie9
  3388. node.onselectstart = function () {
  3389. return false;
  3390. };
  3391. node.onclick = node.onkeyup = node.onkeydown = function () {
  3392. return false;
  3393. };
  3394. node.unselectable = 'on';
  3395. node.setAttribute("unselectable", "on");
  3396. for (var i = 0, ci; ci = node.all[i++];) {
  3397. switch (ci.tagName.toLowerCase()) {
  3398. case 'iframe':
  3399. case 'textarea':
  3400. case 'input':
  3401. case 'select':
  3402. break;
  3403. default:
  3404. ci.unselectable = 'on';
  3405. node.setAttribute("unselectable", "on");
  3406. }
  3407. }
  3408. } : function (node) {
  3409. node.style.MozUserSelect =
  3410. node.style.webkitUserSelect =
  3411. node.style.msUserSelect =
  3412. node.style.KhtmlUserSelect = 'none';
  3413. },
  3414. /**
  3415. * 删除节点node上的指定属性名称的属性
  3416. * @method removeAttributes
  3417. * @param { Node } node 需要删除属性的节点对象
  3418. * @param { String } attrNames 可以是空格隔开的多个属性名称,该操作将会依次删除相应的属性
  3419. * @example
  3420. * ```html
  3421. * <div id="wrap">
  3422. * <span style="font-size:14px;" id="test" name="followMe">xxxxx</span>
  3423. * </div>
  3424. *
  3425. * <script>
  3426. *
  3427. * UE.dom.domUtils.removeAttributes( document.getElementById( "test" ), "id name" );
  3428. *
  3429. * //output: <span style="font-size:14px;">xxxxx</span>
  3430. * console.log( document.getElementById("wrap").innerHTML );
  3431. *
  3432. * </script>
  3433. * ```
  3434. */
  3435. /**
  3436. * 删除节点node上的指定属性名称的属性
  3437. * @method removeAttributes
  3438. * @param { Node } node 需要删除属性的节点对象
  3439. * @param { Array } attrNames 需要删除的属性名数组
  3440. * @example
  3441. * ```html
  3442. * <div id="wrap">
  3443. * <span style="font-size:14px;" id="test" name="followMe">xxxxx</span>
  3444. * </div>
  3445. *
  3446. * <script>
  3447. *
  3448. * UE.dom.domUtils.removeAttributes( document.getElementById( "test" ), ["id", "name"] );
  3449. *
  3450. * //output: <span style="font-size:14px;">xxxxx</span>
  3451. * console.log( document.getElementById("wrap").innerHTML );
  3452. *
  3453. * </script>
  3454. * ```
  3455. */
  3456. removeAttributes: function (node, attrNames) {
  3457. attrNames = utils.isArray(attrNames) ? attrNames : utils.trim(attrNames).replace(/[ ]{2,}/g, ' ').split(' ');
  3458. for (var i = 0, ci; ci = attrNames[i++];) {
  3459. ci = attrFix[ci] || ci;
  3460. switch (ci) {
  3461. case 'className':
  3462. node[ci] = '';
  3463. break;
  3464. case 'style':
  3465. node.style.cssText = '';
  3466. var val = node.getAttributeNode('style');
  3467. !browser.ie && val && node.removeAttributeNode(val);
  3468. }
  3469. node.removeAttribute(ci);
  3470. }
  3471. },
  3472. /**
  3473. * 在doc下创建一个标签名为tag,属性为attrs的元素
  3474. * @method createElement
  3475. * @param { DomDocument } doc 新创建的元素属于该document节点创建
  3476. * @param { String } tagName 需要创建的元素的标签名
  3477. * @param { Object } attrs 新创建的元素的属性key-value集合
  3478. * @return { Element } 新创建的元素对象
  3479. * @example
  3480. * ```javascript
  3481. * var ele = UE.dom.domUtils.createElement( document, 'div', {
  3482. * id: 'test'
  3483. * } );
  3484. *
  3485. * //output: DIV
  3486. * console.log( ele.tagName );
  3487. *
  3488. * //output: test
  3489. * console.log( ele.id );
  3490. *
  3491. * ```
  3492. */
  3493. createElement: function (doc, tag, attrs) {
  3494. return domUtils.setAttributes(doc.createElement(tag), attrs)
  3495. },
  3496. /**
  3497. * 为节点node添加属性attrs,attrs为属性键值对
  3498. * @method setAttributes
  3499. * @param { Element } node 需要设置属性的元素对象
  3500. * @param { Object } attrs 需要设置的属性名-值对
  3501. * @return { Element } 设置属性的元素对象
  3502. * @example
  3503. * ```html
  3504. * <span id="test"></span>
  3505. *
  3506. * <script>
  3507. *
  3508. * var testNode = UE.dom.domUtils.setAttributes( document.getElementById( "test" ), {
  3509. * id: 'demo'
  3510. * } );
  3511. *
  3512. * //output: demo
  3513. * console.log( testNode.id );
  3514. *
  3515. * </script>
  3516. *
  3517. */
  3518. setAttributes: function (node, attrs) {
  3519. for (var attr in attrs) {
  3520. if (attrs.hasOwnProperty(attr)) {
  3521. var value = attrs[attr];
  3522. switch (attr) {
  3523. case 'class':
  3524. //ie下要这样赋值,setAttribute不起作用
  3525. node.className = value;
  3526. break;
  3527. case 'style':
  3528. node.style.cssText = node.style.cssText + ";" + value;
  3529. break;
  3530. case 'innerHTML':
  3531. node[attr] = value;
  3532. break;
  3533. case 'value':
  3534. node.value = value;
  3535. break;
  3536. default:
  3537. node.setAttribute(attrFix[attr] || attr, value);
  3538. }
  3539. }
  3540. }
  3541. return node;
  3542. },
  3543. /**
  3544. * 获取元素element经过计算后的样式值
  3545. * @method getComputedStyle
  3546. * @param { Element } element 需要获取样式的元素对象
  3547. * @param { String } styleName 需要获取的样式名
  3548. * @return { String } 获取到的样式值
  3549. * @example
  3550. * ```html
  3551. * <style type="text/css">
  3552. * #test {
  3553. * font-size: 15px;
  3554. * }
  3555. * </style>
  3556. *
  3557. * <span id="test"></span>
  3558. *
  3559. * <script>
  3560. * //output: 15px
  3561. * console.log( UE.dom.domUtils.getComputedStyle( document.getElementById( "test" ), 'font-size' ) );
  3562. * </script>
  3563. * ```
  3564. */
  3565. getComputedStyle: function (element, styleName) {
  3566. //一下的属性单独处理
  3567. var pros = 'width height top left';
  3568. if (pros.indexOf(styleName) > -1) {
  3569. return element['offset' + styleName.replace(/^\w/, function (s) {
  3570. return s.toUpperCase()
  3571. })] + 'px';
  3572. }
  3573. //忽略文本节点
  3574. if (element.nodeType == 3) {
  3575. element = element.parentNode;
  3576. }
  3577. //ie下font-size若body下定义了font-size,则从currentStyle里会取到这个font-size. 取不到实际值,故此修改.
  3578. if (browser.ie && browser.version < 9 && styleName == 'font-size' && !element.style.fontSize &&
  3579. !dtd.$empty[element.tagName] && !dtd.$nonChild[element.tagName]) {
  3580. var span = element.ownerDocument.createElement('span');
  3581. span.style.cssText = 'padding:0;border:0;font-family:simsun;';
  3582. span.innerHTML = '.';
  3583. element.appendChild(span);
  3584. var result = span.offsetHeight;
  3585. element.removeChild(span);
  3586. span = null;
  3587. return result + 'px';
  3588. }
  3589. try {
  3590. var value = domUtils.getStyle(element, styleName) ||
  3591. (window.getComputedStyle ? domUtils.getWindow(element).getComputedStyle(element, '').getPropertyValue(styleName) :
  3592. (element.currentStyle || element.style)[utils.cssStyleToDomStyle(styleName)]);
  3593. } catch (e) {
  3594. return "";
  3595. }
  3596. return utils.transUnitToPx(utils.fixColor(styleName, value));
  3597. },
  3598. /**
  3599. * 删除元素element指定的className
  3600. * @method removeClasses
  3601. * @param { Element } ele 需要删除class的元素节点
  3602. * @param { String } classNames 需要删除的className, 多个className之间以空格分开
  3603. * @example
  3604. * ```html
  3605. * <span id="test" class="test1 test2 test3">xxx</span>
  3606. *
  3607. * <script>
  3608. *
  3609. * var testNode = document.getElementById( "test" );
  3610. * UE.dom.domUtils.removeClasses( testNode, "test1 test2" );
  3611. *
  3612. * //output: test3
  3613. * console.log( testNode.className );
  3614. *
  3615. * </script>
  3616. * ```
  3617. */
  3618. /**
  3619. * 删除元素element指定的className
  3620. * @method removeClasses
  3621. * @param { Element } ele 需要删除class的元素节点
  3622. * @param { Array } classNames 需要删除的className数组
  3623. * @example
  3624. * ```html
  3625. * <span id="test" class="test1 test2 test3">xxx</span>
  3626. *
  3627. * <script>
  3628. *
  3629. * var testNode = document.getElementById( "test" );
  3630. * UE.dom.domUtils.removeClasses( testNode, ["test1", "test2"] );
  3631. *
  3632. * //output: test3
  3633. * console.log( testNode.className );
  3634. *
  3635. * </script>
  3636. * ```
  3637. */
  3638. removeClasses: function (elm, classNames) {
  3639. classNames = utils.isArray(classNames) ? classNames :
  3640. utils.trim(classNames).replace(/[ ]{2,}/g, ' ').split(' ');
  3641. for (var i = 0, ci, cls = elm.className; ci = classNames[i++];) {
  3642. cls = cls.replace(new RegExp('\\b' + ci + '\\b'), '')
  3643. }
  3644. cls = utils.trim(cls).replace(/[ ]{2,}/g, ' ');
  3645. if (cls) {
  3646. elm.className = cls;
  3647. } else {
  3648. domUtils.removeAttributes(elm, ['class']);
  3649. }
  3650. },
  3651. /**
  3652. * 给元素element添加className
  3653. * @method addClass
  3654. * @param { Node } ele 需要增加className的元素
  3655. * @param { String } classNames 需要添加的className, 多个className之间以空格分割
  3656. * @remind 相同的类名不会被重复添加
  3657. * @example
  3658. * ```html
  3659. * <span id="test" class="cls1 cls2"></span>
  3660. *
  3661. * <script>
  3662. * var testNode = document.getElementById("test");
  3663. *
  3664. * UE.dom.domUtils.addClass( testNode, "cls2 cls3 cls4" );
  3665. *
  3666. * //output: cl1 cls2 cls3 cls4
  3667. * console.log( testNode.className );
  3668. *
  3669. * <script>
  3670. * ```
  3671. */
  3672. /**
  3673. * 给元素element添加className
  3674. * @method addClass
  3675. * @param { Node } ele 需要增加className的元素
  3676. * @param { Array } classNames 需要添加的className的数组
  3677. * @remind 相同的类名不会被重复添加
  3678. * @example
  3679. * ```html
  3680. * <span id="test" class="cls1 cls2"></span>
  3681. *
  3682. * <script>
  3683. * var testNode = document.getElementById("test");
  3684. *
  3685. * UE.dom.domUtils.addClass( testNode, ["cls2", "cls3", "cls4"] );
  3686. *
  3687. * //output: cl1 cls2 cls3 cls4
  3688. * console.log( testNode.className );
  3689. *
  3690. * <script>
  3691. * ```
  3692. */
  3693. addClass: function (elm, classNames) {
  3694. if (!elm) return;
  3695. classNames = utils.trim(classNames).replace(/[ ]{2,}/g, ' ').split(' ');
  3696. for (var i = 0, ci, cls = elm.className; ci = classNames[i++];) {
  3697. if (!new RegExp('\\b' + ci + '\\b').test(cls)) {
  3698. cls += ' ' + ci;
  3699. }
  3700. }
  3701. elm.className = utils.trim(cls);
  3702. },
  3703. /**
  3704. * 判断元素element是否包含给定的样式类名className
  3705. * @method hasClass
  3706. * @param { Node } ele 需要检测的元素
  3707. * @param { String } classNames 需要检测的className, 多个className之间用空格分割
  3708. * @return { Boolean } 元素是否包含所有给定的className
  3709. * @example
  3710. * ```html
  3711. * <span id="test1" class="cls1 cls2"></span>
  3712. *
  3713. * <script>
  3714. * var test1 = document.getElementById("test1");
  3715. *
  3716. * //output: false
  3717. * console.log( UE.dom.domUtils.hasClass( test1, "cls2 cls1 cls3" ) );
  3718. *
  3719. * //output: true
  3720. * console.log( UE.dom.domUtils.hasClass( test1, "cls2 cls1" ) );
  3721. * </script>
  3722. * ```
  3723. */
  3724. /**
  3725. * 判断元素element是否包含给定的样式类名className
  3726. * @method hasClass
  3727. * @param { Node } ele 需要检测的元素
  3728. * @param { Array } classNames 需要检测的className数组
  3729. * @return { Boolean } 元素是否包含所有给定的className
  3730. * @example
  3731. * ```html
  3732. * <span id="test1" class="cls1 cls2"></span>
  3733. *
  3734. * <script>
  3735. * var test1 = document.getElementById("test1");
  3736. *
  3737. * //output: false
  3738. * console.log( UE.dom.domUtils.hasClass( test1, [ "cls2", "cls1", "cls3" ] ) );
  3739. *
  3740. * //output: true
  3741. * console.log( UE.dom.domUtils.hasClass( test1, [ "cls2", "cls1" ]) );
  3742. * </script>
  3743. * ```
  3744. */
  3745. hasClass: function (element, className) {
  3746. if (utils.isRegExp(className)) {
  3747. return className.test(element.className)
  3748. }
  3749. className = utils.trim(className).replace(/[ ]{2,}/g, ' ').split(' ');
  3750. for (var i = 0, ci, cls = element.className; ci = className[i++];) {
  3751. if (!new RegExp('\\b' + ci + '\\b', 'i').test(cls)) {
  3752. return false;
  3753. }
  3754. }
  3755. return i - 1 == className.length;
  3756. },
  3757. /**
  3758. * 阻止事件默认行为
  3759. * @method preventDefault
  3760. * @param { Event } evt 需要阻止默认行为的事件对象
  3761. * @example
  3762. * ```javascript
  3763. * UE.dom.domUtils.preventDefault( evt );
  3764. * ```
  3765. */
  3766. preventDefault: function (evt) {
  3767. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
  3768. },
  3769. /**
  3770. * 删除元素element指定的样式
  3771. * @method removeStyle
  3772. * @param { Element } element 需要删除样式的元素
  3773. * @param { String } styleName 需要删除的样式名
  3774. * @example
  3775. * ```html
  3776. * <span id="test" style="color: red; background: blue;"></span>
  3777. *
  3778. * <script>
  3779. *
  3780. * var testNode = document.getElementById("test");
  3781. *
  3782. * UE.dom.domUtils.removeStyle( testNode, 'color' );
  3783. *
  3784. * //output: background: blue;
  3785. * console.log( testNode.style.cssText );
  3786. *
  3787. * </script>
  3788. * ```
  3789. */
  3790. removeStyle: function (element, name) {
  3791. if (browser.ie) {
  3792. //针对color先单独处理一下
  3793. if (name == 'color') {
  3794. name = '(^|;)' + name;
  3795. }
  3796. element.style.cssText = element.style.cssText.replace(new RegExp(name + '[^:]*:[^;]+;?', 'ig'), '')
  3797. } else {
  3798. if (element.style.removeProperty) {
  3799. element.style.removeProperty(name);
  3800. } else {
  3801. element.style.removeAttribute(utils.cssStyleToDomStyle(name));
  3802. }
  3803. }
  3804. if (!element.style.cssText) {
  3805. domUtils.removeAttributes(element, ['style']);
  3806. }
  3807. },
  3808. /**
  3809. * 获取元素element的style属性的指定值
  3810. * @method getStyle
  3811. * @param { Element } element 需要获取属性值的元素
  3812. * @param { String } styleName 需要获取的style的名称
  3813. * @warning 该方法仅获取元素style属性中所标明的值
  3814. * @return { String } 该元素包含指定的style属性值
  3815. * @example
  3816. * ```html
  3817. * <div id="test" style="color: red;"></div>
  3818. *
  3819. * <script>
  3820. *
  3821. * var testNode = document.getElementById( "test" );
  3822. *
  3823. * //output: red
  3824. * console.log( UE.dom.domUtils.getStyle( testNode, "color" ) );
  3825. *
  3826. * //output: ""
  3827. * console.log( UE.dom.domUtils.getStyle( testNode, "background" ) );
  3828. *
  3829. * </script>
  3830. * ```
  3831. */
  3832. getStyle: function (element, name) {
  3833. var value = element.style[utils.cssStyleToDomStyle(name)];
  3834. return utils.fixColor(name, value);
  3835. },
  3836. /**
  3837. * 为元素element设置样式属性值
  3838. * @method setStyle
  3839. * @param { Element } element 需要设置样式的元素
  3840. * @param { String } styleName 样式名
  3841. * @param { String } styleValue 样式值
  3842. * @example
  3843. * ```html
  3844. * <div id="test"></div>
  3845. *
  3846. * <script>
  3847. *
  3848. * var testNode = document.getElementById( "test" );
  3849. *
  3850. * //output: ""
  3851. * console.log( testNode.style.color );
  3852. *
  3853. * UE.dom.domUtils.setStyle( testNode, 'color', 'red' );
  3854. * //output: "red"
  3855. * console.log( testNode.style.color );
  3856. *
  3857. * </script>
  3858. * ```
  3859. */
  3860. setStyle: function (element, name, value) {
  3861. element.style[utils.cssStyleToDomStyle(name)] = value;
  3862. if (!utils.trim(element.style.cssText)) {
  3863. this.removeAttributes(element, 'style')
  3864. }
  3865. },
  3866. /**
  3867. * 为元素element设置多个样式属性值
  3868. * @method setStyles
  3869. * @param { Element } element 需要设置样式的元素
  3870. * @param { Object } styles 样式名值对
  3871. * @example
  3872. * ```html
  3873. * <div id="test"></div>
  3874. *
  3875. * <script>
  3876. *
  3877. * var testNode = document.getElementById( "test" );
  3878. *
  3879. * //output: ""
  3880. * console.log( testNode.style.color );
  3881. *
  3882. * UE.dom.domUtils.setStyles( testNode, {
  3883. * 'color': 'red'
  3884. * } );
  3885. * //output: "red"
  3886. * console.log( testNode.style.color );
  3887. *
  3888. * </script>
  3889. * ```
  3890. */
  3891. setStyles: function (element, styles) {
  3892. for (var name in styles) {
  3893. if (styles.hasOwnProperty(name)) {
  3894. domUtils.setStyle(element, name, styles[name]);
  3895. }
  3896. }
  3897. },
  3898. /**
  3899. * 删除_moz_dirty属性
  3900. * @private
  3901. * @method removeDirtyAttr
  3902. */
  3903. removeDirtyAttr: function (node) {
  3904. for (var i = 0, ci, nodes = node.getElementsByTagName('*'); ci = nodes[i++];) {
  3905. ci.removeAttribute('_moz_dirty');
  3906. }
  3907. node.removeAttribute('_moz_dirty');
  3908. },
  3909. /**
  3910. * 获取子节点的数量
  3911. * @method getChildCount
  3912. * @param { Element } node 需要检测的元素
  3913. * @return { Number } 给定的node元素的子节点数量
  3914. * @example
  3915. * ```html
  3916. * <div id="test">
  3917. * <span></span>
  3918. * </div>
  3919. *
  3920. * <script>
  3921. *
  3922. * //output: 3
  3923. * console.log( UE.dom.domUtils.getChildCount( document.getElementById("test") ) );
  3924. *
  3925. * </script>
  3926. * ```
  3927. */
  3928. /**
  3929. * 根据给定的过滤规则, 获取符合条件的子节点的数量
  3930. * @method getChildCount
  3931. * @param { Element } node 需要检测的元素
  3932. * @param { Function } fn 过滤器, 要求对符合条件的子节点返回true, 反之则要求返回false
  3933. * @return { Number } 符合过滤条件的node元素的子节点数量
  3934. * @example
  3935. * ```html
  3936. * <div id="test">
  3937. * <span></span>
  3938. * </div>
  3939. *
  3940. * <script>
  3941. *
  3942. * //output: 1
  3943. * console.log( UE.dom.domUtils.getChildCount( document.getElementById("test"), function ( node ) {
  3944. *
  3945. * return node.nodeType === 1;
  3946. *
  3947. * } ) );
  3948. *
  3949. * </script>
  3950. * ```
  3951. */
  3952. getChildCount: function (node, fn) {
  3953. var count = 0,
  3954. first = node.firstChild;
  3955. fn = fn || function () {
  3956. return 1;
  3957. };
  3958. while (first) {
  3959. if (fn(first)) {
  3960. count++;
  3961. }
  3962. first = first.nextSibling;
  3963. }
  3964. return count;
  3965. },
  3966. /**
  3967. * 判断给定节点是否为空节点
  3968. * @method isEmptyNode
  3969. * @param { Node } node 需要检测的节点对象
  3970. * @return { Boolean } 节点是否为空
  3971. * @example
  3972. * ```javascript
  3973. * UE.dom.domUtils.isEmptyNode( document.body );
  3974. * ```
  3975. */
  3976. isEmptyNode: function (node) {
  3977. return !node.firstChild || domUtils.getChildCount(node, function (node) {
  3978. return !domUtils.isBr(node) && !domUtils.isBookmarkNode(node) && !domUtils.isWhitespace(node)
  3979. }) == 0
  3980. },
  3981. clearSelectedArr: function (nodes) {
  3982. var node;
  3983. while (node = nodes.pop()) {
  3984. domUtils.removeAttributes(node, ['class']);
  3985. }
  3986. },
  3987. /**
  3988. * 将显示区域滚动到指定节点的位置
  3989. * @method scrollToView
  3990. * @param {Node} node 节点
  3991. * @param {window} win window对象
  3992. * @param {Number} offsetTop 距离上方的偏移量
  3993. */
  3994. scrollToView: function (node, win, offsetTop) {
  3995. var getViewPaneSize = function () {
  3996. var doc = win.document,
  3997. mode = doc.compatMode == 'CSS1Compat';
  3998. return {
  3999. width: (mode ? doc.documentElement.clientWidth : doc.body.clientWidth) || 0,
  4000. height: (mode ? doc.documentElement.clientHeight : doc.body.clientHeight) || 0
  4001. };
  4002. },
  4003. getScrollPosition = function (win) {
  4004. if ('pageXOffset' in win) {
  4005. return {
  4006. x: win.pageXOffset || 0,
  4007. y: win.pageYOffset || 0
  4008. };
  4009. } else {
  4010. var doc = win.document;
  4011. return {
  4012. x: doc.documentElement.scrollLeft || doc.body.scrollLeft || 0,
  4013. y: doc.documentElement.scrollTop || doc.body.scrollTop || 0
  4014. };
  4015. }
  4016. };
  4017. var winHeight = getViewPaneSize().height,
  4018. offset = winHeight * -1 + offsetTop;
  4019. offset += (node.offsetHeight || 0);
  4020. var elementPosition = domUtils.getXY(node);
  4021. offset += elementPosition.y;
  4022. var currentScroll = getScrollPosition(win).y;
  4023. // offset += 50;
  4024. if (offset > currentScroll || offset < currentScroll - winHeight) {
  4025. win.scrollTo(0, offset + (offset < 0 ? -20 : 20));
  4026. }
  4027. },
  4028. /**
  4029. * 判断给定节点是否为br
  4030. * @method isBr
  4031. * @param { Node } node 需要判断的节点对象
  4032. * @return { Boolean } 给定的节点是否是br节点
  4033. */
  4034. isBr: function (node) {
  4035. return node.nodeType == 1 && node.tagName == 'BR';
  4036. },
  4037. /**
  4038. * 判断给定的节点是否是一个“填充”节点
  4039. * @private
  4040. * @method isFillChar
  4041. * @param { Node } node 需要判断的节点
  4042. * @param { Boolean } isInStart 是否从节点内容的开始位置匹配
  4043. * @returns { Boolean } 节点是否是填充节点
  4044. */
  4045. isFillChar: function (node, isInStart) {
  4046. if (node.nodeType != 3)
  4047. return false;
  4048. var text = node.nodeValue;
  4049. if (isInStart) {
  4050. return new RegExp('^' + domUtils.fillChar).test(text)
  4051. }
  4052. return !text.replace(new RegExp(domUtils.fillChar, 'g'), '').length
  4053. },
  4054. isStartInblock: function (range) {
  4055. var tmpRange = range.cloneRange(),
  4056. flag = 0,
  4057. start = tmpRange.startContainer,
  4058. tmp;
  4059. if (start.nodeType == 1 && start.childNodes[tmpRange.startOffset]) {
  4060. start = start.childNodes[tmpRange.startOffset];
  4061. var pre = start.previousSibling;
  4062. while (pre && domUtils.isFillChar(pre)) {
  4063. start = pre;
  4064. pre = pre.previousSibling;
  4065. }
  4066. }
  4067. if (this.isFillChar(start, true) && tmpRange.startOffset == 1) {
  4068. tmpRange.setStartBefore(start);
  4069. start = tmpRange.startContainer;
  4070. }
  4071. while (start && domUtils.isFillChar(start)) {
  4072. tmp = start;
  4073. start = start.previousSibling
  4074. }
  4075. if (tmp) {
  4076. tmpRange.setStartBefore(tmp);
  4077. start = tmpRange.startContainer;
  4078. }
  4079. if (start.nodeType == 1 && domUtils.isEmptyNode(start) && tmpRange.startOffset == 1) {
  4080. tmpRange.setStart(start, 0).collapse(true);
  4081. }
  4082. while (!tmpRange.startOffset) {
  4083. start = tmpRange.startContainer;
  4084. if (domUtils.isBlockElm(start) || domUtils.isBody(start)) {
  4085. flag = 1;
  4086. break;
  4087. }
  4088. var pre = tmpRange.startContainer.previousSibling,
  4089. tmpNode;
  4090. if (!pre) {
  4091. tmpRange.setStartBefore(tmpRange.startContainer);
  4092. } else {
  4093. while (pre && domUtils.isFillChar(pre)) {
  4094. tmpNode = pre;
  4095. pre = pre.previousSibling;
  4096. }
  4097. if (tmpNode) {
  4098. tmpRange.setStartBefore(tmpNode);
  4099. } else {
  4100. tmpRange.setStartBefore(tmpRange.startContainer);
  4101. }
  4102. }
  4103. }
  4104. return flag && !domUtils.isBody(tmpRange.startContainer) ? 1 : 0;
  4105. },
  4106. /**
  4107. * 判断给定的元素是否是一个空元素
  4108. * @method isEmptyBlock
  4109. * @param { Element } node 需要判断的元素
  4110. * @return { Boolean } 是否是空元素
  4111. * @example
  4112. * ```html
  4113. * <div id="test"></div>
  4114. *
  4115. * <script>
  4116. * //output: true
  4117. * console.log( UE.dom.domUtils.isEmptyBlock( document.getElementById("test") ) );
  4118. * </script>
  4119. * ```
  4120. */
  4121. /**
  4122. * 根据指定的判断规则判断给定的元素是否是一个空元素
  4123. * @method isEmptyBlock
  4124. * @param { Element } node 需要判断的元素
  4125. * @param { RegExp } reg 对内容执行判断的正则表达式对象
  4126. * @return { Boolean } 是否是空元素
  4127. */
  4128. isEmptyBlock: function (node, reg) {
  4129. // HaoChuan9421
  4130. if (!node) {
  4131. return;
  4132. }
  4133. if (node.nodeType != 1)
  4134. return 0;
  4135. reg = reg || new RegExp('[ \xa0\t\r\n' + domUtils.fillChar + ']', 'g');
  4136. if (node[browser.ie ? 'innerText' : 'textContent'].replace(reg, '').length > 0) {
  4137. return 0;
  4138. }
  4139. for (var n in dtd.$isNotEmpty) {
  4140. if (node.getElementsByTagName(n).length) {
  4141. return 0;
  4142. }
  4143. }
  4144. return 1;
  4145. },
  4146. /**
  4147. * 移动元素使得该元素的位置移动指定的偏移量的距离
  4148. * @method setViewportOffset
  4149. * @param { Element } element 需要设置偏移量的元素
  4150. * @param { Object } offset 偏移量, 形如{ left: 100, top: 50 }的一个键值对, 表示该元素将在
  4151. * 现有的位置上向水平方向偏移offset.left的距离, 在竖直方向上偏移
  4152. * offset.top的距离
  4153. * @example
  4154. * ```html
  4155. * <div id="test" style="top: 100px; left: 50px; position: absolute;"></div>
  4156. *
  4157. * <script>
  4158. *
  4159. * var testNode = document.getElementById("test");
  4160. *
  4161. * UE.dom.domUtils.setViewportOffset( testNode, {
  4162. * left: 200,
  4163. * top: 50
  4164. * } );
  4165. *
  4166. * //output: top: 300px; left: 100px; position: absolute;
  4167. * console.log( testNode.style.cssText );
  4168. *
  4169. * </script>
  4170. * ```
  4171. */
  4172. setViewportOffset: function (element, offset) {
  4173. var left = parseInt(element.style.left) | 0;
  4174. var top = parseInt(element.style.top) | 0;
  4175. var rect = element.getBoundingClientRect();
  4176. var offsetLeft = offset.left - rect.left;
  4177. var offsetTop = offset.top - rect.top;
  4178. if (offsetLeft) {
  4179. element.style.left = left + offsetLeft + 'px';
  4180. }
  4181. if (offsetTop) {
  4182. element.style.top = top + offsetTop + 'px';
  4183. }
  4184. },
  4185. /**
  4186. * 用“填充字符”填充节点
  4187. * @method fillNode
  4188. * @private
  4189. * @param { DomDocument } doc 填充的节点所在的docment对象
  4190. * @param { Node } node 需要填充的节点对象
  4191. * @example
  4192. * ```html
  4193. * <div id="test"></div>
  4194. *
  4195. * <script>
  4196. * var testNode = document.getElementById("test");
  4197. *
  4198. * //output: 0
  4199. * console.log( testNode.childNodes.length );
  4200. *
  4201. * UE.dom.domUtils.fillNode( document, testNode );
  4202. *
  4203. * //output: 1
  4204. * console.log( testNode.childNodes.length );
  4205. *
  4206. * </script>
  4207. * ```
  4208. */
  4209. fillNode: function (doc, node) {
  4210. var tmpNode = browser.ie ? doc.createTextNode(domUtils.fillChar) : doc.createElement('br');
  4211. node.innerHTML = '';
  4212. node.appendChild(tmpNode);
  4213. },
  4214. /**
  4215. * 把节点src的所有子节点追加到另一个节点tag上去
  4216. * @method moveChild
  4217. * @param { Node } src 源节点, 该节点下的所有子节点将被移除
  4218. * @param { Node } tag 目标节点, 从源节点移除的子节点将被追加到该节点下
  4219. * @example
  4220. * ```html
  4221. * <div id="test1">
  4222. * <span></span>
  4223. * </div>
  4224. * <div id="test2">
  4225. * <div></div>
  4226. * </div>
  4227. *
  4228. * <script>
  4229. *
  4230. * var test1 = document.getElementById("test1"),
  4231. * test2 = document.getElementById("test2");
  4232. *
  4233. * UE.dom.domUtils.moveChild( test1, test2 );
  4234. *
  4235. * //output: ""(空字符串)
  4236. * console.log( test1.innerHTML );
  4237. *
  4238. * //output: "<div></div><span></span>"
  4239. * console.log( test2.innerHTML );
  4240. *
  4241. * </script>
  4242. * ```
  4243. */
  4244. /**
  4245. * 把节点src的所有子节点移动到另一个节点tag上去, 可以通过dir参数控制附加的行为是“追加”还是“插入顶部”
  4246. * @method moveChild
  4247. * @param { Node } src 源节点, 该节点下的所有子节点将被移除
  4248. * @param { Node } tag 目标节点, 从源节点移除的子节点将被附加到该节点下
  4249. * @param { Boolean } dir 附加方式, 如果为true, 则附加进去的节点将被放到目标节点的顶部, 反之,则放到末尾
  4250. * @example
  4251. * ```html
  4252. * <div id="test1">
  4253. * <span></span>
  4254. * </div>
  4255. * <div id="test2">
  4256. * <div></div>
  4257. * </div>
  4258. *
  4259. * <script>
  4260. *
  4261. * var test1 = document.getElementById("test1"),
  4262. * test2 = document.getElementById("test2");
  4263. *
  4264. * UE.dom.domUtils.moveChild( test1, test2, true );
  4265. *
  4266. * //output: ""(空字符串)
  4267. * console.log( test1.innerHTML );
  4268. *
  4269. * //output: "<span></span><div></div>"
  4270. * console.log( test2.innerHTML );
  4271. *
  4272. * </script>
  4273. * ```
  4274. */
  4275. moveChild: function (src, tag, dir) {
  4276. while (src.firstChild) {
  4277. if (dir && tag.firstChild) {
  4278. tag.insertBefore(src.lastChild, tag.firstChild);
  4279. } else {
  4280. tag.appendChild(src.firstChild);
  4281. }
  4282. }
  4283. },
  4284. /**
  4285. * 判断节点的标签上是否不存在任何属性
  4286. * @method hasNoAttributes
  4287. * @private
  4288. * @param { Node } node 需要检测的节点对象
  4289. * @return { Boolean } 节点是否不包含任何属性
  4290. * @example
  4291. * ```html
  4292. * <div id="test"><span>xxxx</span></div>
  4293. *
  4294. * <script>
  4295. *
  4296. * //output: false
  4297. * console.log( UE.dom.domUtils.hasNoAttributes( document.getElementById("test") ) );
  4298. *
  4299. * //output: true
  4300. * console.log( UE.dom.domUtils.hasNoAttributes( document.getElementById("test").firstChild ) );
  4301. *
  4302. * </script>
  4303. * ```
  4304. */
  4305. hasNoAttributes: function (node) {
  4306. return browser.ie ? /^<\w+\s*?>/.test(node.outerHTML) : node.attributes.length == 0;
  4307. },
  4308. /**
  4309. * 检测节点是否是UEditor所使用的辅助节点
  4310. * @method isCustomeNode
  4311. * @private
  4312. * @param { Node } node 需要检测的节点
  4313. * @remind 辅助节点是指编辑器要完成工作临时添加的节点, 在输出的时候将会从编辑器内移除, 不会影响最终的结果。
  4314. * @return { Boolean } 给定的节点是否是一个辅助节点
  4315. */
  4316. isCustomeNode: function (node) {
  4317. return node.nodeType == 1 && node.getAttribute('_ue_custom_node_');
  4318. },
  4319. /**
  4320. * 检测节点的标签是否是给定的标签
  4321. * @method isTagNode
  4322. * @param { Node } node 需要检测的节点对象
  4323. * @param { String } tagName 标签
  4324. * @return { Boolean } 节点的标签是否是给定的标签
  4325. * @example
  4326. * ```html
  4327. * <div id="test"></div>
  4328. *
  4329. * <script>
  4330. *
  4331. * //output: true
  4332. * console.log( UE.dom.domUtils.isTagNode( document.getElementById("test"), "div" ) );
  4333. *
  4334. * </script>
  4335. * ```
  4336. */
  4337. isTagNode: function (node, tagNames) {
  4338. return node.nodeType == 1 && new RegExp('\\b' + node.tagName + '\\b', 'i').test(tagNames)
  4339. },
  4340. /**
  4341. * 给定一个节点数组,在通过指定的过滤器过滤后, 获取其中满足过滤条件的第一个节点
  4342. * @method filterNodeList
  4343. * @param { Array } nodeList 需要过滤的节点数组
  4344. * @param { Function } fn 过滤器, 对符合条件的节点, 执行结果返回true, 反之则返回false
  4345. * @return { Node | NULL } 如果找到符合过滤条件的节点, 则返回该节点, 否则返回NULL
  4346. * @example
  4347. * ```javascript
  4348. * var divNodes = document.getElementsByTagName("div");
  4349. * divNodes = [].slice.call( divNodes, 0 );
  4350. *
  4351. * //output: null
  4352. * console.log( UE.dom.domUtils.filterNodeList( divNodes, function ( node ) {
  4353. * return node.tagName.toLowerCase() !== 'div';
  4354. * } ) );
  4355. * ```
  4356. */
  4357. /**
  4358. * 给定一个节点数组nodeList和一组标签名tagNames, 获取其中能够匹配标签名的节点集合中的第一个节点
  4359. * @method filterNodeList
  4360. * @param { Array } nodeList 需要过滤的节点数组
  4361. * @param { String } tagNames 需要匹配的标签名, 多个标签名之间用空格分割
  4362. * @return { Node | NULL } 如果找到标签名匹配的节点, 则返回该节点, 否则返回NULL
  4363. * @example
  4364. * ```javascript
  4365. * var divNodes = document.getElementsByTagName("div");
  4366. * divNodes = [].slice.call( divNodes, 0 );
  4367. *
  4368. * //output: null
  4369. * console.log( UE.dom.domUtils.filterNodeList( divNodes, 'a span' ) );
  4370. * ```
  4371. */
  4372. /**
  4373. * 给定一个节点数组,在通过指定的过滤器过滤后, 如果参数forAll为true, 则会返回所有满足过滤
  4374. * 条件的节点集合, 否则, 返回满足条件的节点集合中的第一个节点
  4375. * @method filterNodeList
  4376. * @param { Array } nodeList 需要过滤的节点数组
  4377. * @param { Function } fn 过滤器, 对符合条件的节点, 执行结果返回true, 反之则返回false
  4378. * @param { Boolean } forAll 是否返回整个节点数组, 如果该参数为false, 则返回节点集合中的第一个节点
  4379. * @return { Array | Node | NULL } 如果找到符合过滤条件的节点, 则根据参数forAll的值决定返回满足
  4380. * 过滤条件的节点数组或第一个节点, 否则返回NULL
  4381. * @example
  4382. * ```javascript
  4383. * var divNodes = document.getElementsByTagName("div");
  4384. * divNodes = [].slice.call( divNodes, 0 );
  4385. *
  4386. * //output: 3(假定有3个div)
  4387. * console.log( divNodes.length );
  4388. *
  4389. * var nodes = UE.dom.domUtils.filterNodeList( divNodes, function ( node ) {
  4390. * return node.tagName.toLowerCase() === 'div';
  4391. * }, true );
  4392. *
  4393. * //output: 3
  4394. * console.log( nodes.length );
  4395. *
  4396. * var node = UE.dom.domUtils.filterNodeList( divNodes, function ( node ) {
  4397. * return node.tagName.toLowerCase() === 'div';
  4398. * }, false );
  4399. *
  4400. * //output: div
  4401. * console.log( node.nodeName );
  4402. * ```
  4403. */
  4404. filterNodeList: function (nodelist, filter, forAll) {
  4405. var results = [];
  4406. if (!utils.isFunction(filter)) {
  4407. var str = filter;
  4408. filter = function (n) {
  4409. return utils.indexOf(utils.isArray(str) ? str : str.split(' '), n.tagName.toLowerCase()) != -1
  4410. };
  4411. }
  4412. utils.each(nodelist, function (n) {
  4413. filter(n) && results.push(n)
  4414. });
  4415. return results.length == 0 ? null : results.length == 1 || !forAll ? results[0] : results
  4416. },
  4417. /**
  4418. * 查询给定的range选区是否在给定的node节点内,且在该节点的最末尾
  4419. * @method isInNodeEndBoundary
  4420. * @param { UE.dom.Range } rng 需要判断的range对象, 该对象的startContainer不能为NULL
  4421. * @param node 需要检测的节点对象
  4422. * @return { Number } 如果给定的选取range对象是在node内部的最末端, 则返回1, 否则返回0
  4423. */
  4424. isInNodeEndBoundary: function (rng, node) {
  4425. var start = rng.startContainer;
  4426. if (start.nodeType == 3 && rng.startOffset != start.nodeValue.length) {
  4427. return 0;
  4428. }
  4429. if (start.nodeType == 1 && rng.startOffset != start.childNodes.length) {
  4430. return 0;
  4431. }
  4432. while (start !== node) {
  4433. if (start.nextSibling) {
  4434. return 0
  4435. };
  4436. start = start.parentNode;
  4437. }
  4438. return 1;
  4439. },
  4440. isBoundaryNode: function (node, dir) {
  4441. var tmp;
  4442. while (!domUtils.isBody(node)) {
  4443. tmp = node;
  4444. node = node.parentNode;
  4445. if (tmp !== node[dir]) {
  4446. return false;
  4447. }
  4448. }
  4449. return true;
  4450. },
  4451. fillHtml: browser.ie11below ? '&nbsp;' : '<br/>'
  4452. };
  4453. var fillCharReg = new RegExp(domUtils.fillChar, 'g');
  4454. // core/Range.js
  4455. /**
  4456. * Range封装
  4457. * @file
  4458. * @module UE.dom
  4459. * @class Range
  4460. * @since 1.2.6.1
  4461. */
  4462. /**
  4463. * dom操作封装
  4464. * @unfile
  4465. * @module UE.dom
  4466. */
  4467. /**
  4468. * Range实现类,本类是UEditor底层核心类,封装不同浏览器之间的Range操作。
  4469. * @unfile
  4470. * @module UE.dom
  4471. * @class Range
  4472. */
  4473. (function () {
  4474. var guid = 0,
  4475. fillChar = domUtils.fillChar,
  4476. fillData;
  4477. /**
  4478. * 更新range的collapse状态
  4479. * @param {Range} range range对象
  4480. */
  4481. function updateCollapse(range) {
  4482. range.collapsed =
  4483. range.startContainer && range.endContainer &&
  4484. range.startContainer === range.endContainer &&
  4485. range.startOffset == range.endOffset;
  4486. }
  4487. function selectOneNode(rng) {
  4488. return !rng.collapsed && rng.startContainer.nodeType == 1 && rng.startContainer === rng.endContainer && rng.endOffset - rng.startOffset == 1
  4489. }
  4490. function setEndPoint(toStart, node, offset, range) {
  4491. //如果node是自闭合标签要处理
  4492. if (node.nodeType == 1 && (dtd.$empty[node.tagName] || dtd.$nonChild[node.tagName])) {
  4493. offset = domUtils.getNodeIndex(node) + (toStart ? 0 : 1);
  4494. node = node.parentNode;
  4495. }
  4496. if (toStart) {
  4497. range.startContainer = node;
  4498. range.startOffset = offset;
  4499. if (!range.endContainer) {
  4500. range.collapse(true);
  4501. }
  4502. } else {
  4503. range.endContainer = node;
  4504. range.endOffset = offset;
  4505. if (!range.startContainer) {
  4506. range.collapse(false);
  4507. }
  4508. }
  4509. updateCollapse(range);
  4510. return range;
  4511. }
  4512. function execContentsAction(range, action) {
  4513. //调整边界
  4514. //range.includeBookmark();
  4515. var start = range.startContainer,
  4516. end = range.endContainer,
  4517. startOffset = range.startOffset,
  4518. endOffset = range.endOffset,
  4519. doc = range.document,
  4520. frag = doc.createDocumentFragment(),
  4521. tmpStart, tmpEnd;
  4522. if (start.nodeType == 1) {
  4523. start = start.childNodes[startOffset] || (tmpStart = start.appendChild(doc.createTextNode('')));
  4524. }
  4525. if (end.nodeType == 1) {
  4526. end = end.childNodes[endOffset] || (tmpEnd = end.appendChild(doc.createTextNode('')));
  4527. }
  4528. if (start === end && start.nodeType == 3) {
  4529. frag.appendChild(doc.createTextNode(start.substringData(startOffset, endOffset - startOffset)));
  4530. //is not clone
  4531. if (action) {
  4532. start.deleteData(startOffset, endOffset - startOffset);
  4533. range.collapse(true);
  4534. }
  4535. return frag;
  4536. }
  4537. var current, currentLevel, clone = frag,
  4538. startParents = domUtils.findParents(start, true),
  4539. endParents = domUtils.findParents(end, true);
  4540. for (var i = 0; startParents[i] == endParents[i];) {
  4541. i++;
  4542. }
  4543. for (var j = i, si; si = startParents[j]; j++) {
  4544. current = si.nextSibling;
  4545. if (si == start) {
  4546. if (!tmpStart) {
  4547. if (range.startContainer.nodeType == 3) {
  4548. clone.appendChild(doc.createTextNode(start.nodeValue.slice(startOffset)));
  4549. //is not clone
  4550. if (action) {
  4551. start.deleteData(startOffset, start.nodeValue.length - startOffset);
  4552. }
  4553. } else {
  4554. clone.appendChild(!action ? start.cloneNode(true) : start);
  4555. }
  4556. }
  4557. } else {
  4558. currentLevel = si.cloneNode(false);
  4559. clone.appendChild(currentLevel);
  4560. }
  4561. while (current) {
  4562. if (current === end || current === endParents[j]) {
  4563. break;
  4564. }
  4565. si = current.nextSibling;
  4566. clone.appendChild(!action ? current.cloneNode(true) : current);
  4567. current = si;
  4568. }
  4569. clone = currentLevel;
  4570. }
  4571. clone = frag;
  4572. if (!startParents[i]) {
  4573. clone.appendChild(startParents[i - 1].cloneNode(false));
  4574. clone = clone.firstChild;
  4575. }
  4576. for (var j = i, ei; ei = endParents[j]; j++) {
  4577. current = ei.previousSibling;
  4578. if (ei == end) {
  4579. if (!tmpEnd && range.endContainer.nodeType == 3) {
  4580. clone.appendChild(doc.createTextNode(end.substringData(0, endOffset)));
  4581. //is not clone
  4582. if (action) {
  4583. end.deleteData(0, endOffset);
  4584. }
  4585. }
  4586. } else {
  4587. currentLevel = ei.cloneNode(false);
  4588. clone.appendChild(currentLevel);
  4589. }
  4590. //如果两端同级,右边第一次已经被开始做了
  4591. if (j != i || !startParents[i]) {
  4592. while (current) {
  4593. if (current === start) {
  4594. break;
  4595. }
  4596. ei = current.previousSibling;
  4597. clone.insertBefore(!action ? current.cloneNode(true) : current, clone.firstChild);
  4598. current = ei;
  4599. }
  4600. }
  4601. clone = currentLevel;
  4602. }
  4603. if (action) {
  4604. range.setStartBefore(!endParents[i] ? endParents[i - 1] : !startParents[i] ? startParents[i - 1] : endParents[i]).collapse(true);
  4605. }
  4606. tmpStart && domUtils.remove(tmpStart);
  4607. tmpEnd && domUtils.remove(tmpEnd);
  4608. return frag;
  4609. }
  4610. /**
  4611. * 创建一个跟document绑定的空的Range实例
  4612. * @constructor
  4613. * @param { Document } document 新建的选区所属的文档对象
  4614. */
  4615. /**
  4616. * @property { Node } startContainer 当前Range的开始边界的容器节点, 可以是一个元素节点或者是文本节点
  4617. */
  4618. /**
  4619. * @property { Node } startOffset 当前Range的开始边界容器节点的偏移量, 如果是元素节点,
  4620. * 该值就是childNodes中的第几个节点, 如果是文本节点就是文本内容的第几个字符
  4621. */
  4622. /**
  4623. * @property { Node } endContainer 当前Range的结束边界的容器节点, 可以是一个元素节点或者是文本节点
  4624. */
  4625. /**
  4626. * @property { Node } endOffset 当前Range的结束边界容器节点的偏移量, 如果是元素节点,
  4627. * 该值就是childNodes中的第几个节点, 如果是文本节点就是文本内容的第几个字符
  4628. */
  4629. /**
  4630. * @property { Boolean } collapsed 当前Range是否闭合
  4631. * @default true
  4632. * @remind Range是闭合的时候, startContainer === endContainer && startOffset === endOffset
  4633. */
  4634. /**
  4635. * @property { Document } document 当前Range所属的Document对象
  4636. * @remind 不同range的的document属性可以是不同的
  4637. */
  4638. var Range = dom.Range = function (document) {
  4639. var me = this;
  4640. me.startContainer =
  4641. me.startOffset =
  4642. me.endContainer =
  4643. me.endOffset = null;
  4644. me.document = document;
  4645. me.collapsed = true;
  4646. };
  4647. /**
  4648. * 删除fillData
  4649. * @param doc
  4650. * @param excludeNode
  4651. */
  4652. function removeFillData(doc, excludeNode) {
  4653. try {
  4654. if (fillData && domUtils.inDoc(fillData, doc)) {
  4655. if (!fillData.nodeValue.replace(fillCharReg, '').length) {
  4656. var tmpNode = fillData.parentNode;
  4657. domUtils.remove(fillData);
  4658. while (tmpNode && domUtils.isEmptyInlineElement(tmpNode) &&
  4659. //safari的contains有bug
  4660. (browser.safari ? !(domUtils.getPosition(tmpNode, excludeNode) & domUtils.POSITION_CONTAINS) : !tmpNode.contains(excludeNode))
  4661. ) {
  4662. fillData = tmpNode.parentNode;
  4663. domUtils.remove(tmpNode);
  4664. tmpNode = fillData;
  4665. }
  4666. } else {
  4667. fillData.nodeValue = fillData.nodeValue.replace(fillCharReg, '');
  4668. }
  4669. }
  4670. } catch (e) {}
  4671. }
  4672. /**
  4673. * @param node
  4674. * @param dir
  4675. */
  4676. function mergeSibling(node, dir) {
  4677. var tmpNode;
  4678. node = node[dir];
  4679. while (node && domUtils.isFillChar(node)) {
  4680. tmpNode = node[dir];
  4681. domUtils.remove(node);
  4682. node = tmpNode;
  4683. }
  4684. }
  4685. Range.prototype = {
  4686. /**
  4687. * 克隆选区的内容到一个DocumentFragment里
  4688. * @method cloneContents
  4689. * @return { DocumentFragment | NULL } 如果选区是闭合的将返回null, 否则, 返回包含所clone内容的DocumentFragment元素
  4690. * @example
  4691. * ```html
  4692. * <body>
  4693. * <!-- 中括号表示选区 -->
  4694. * <b>x<i>x[x</i>xx]x</b>
  4695. *
  4696. * <script>
  4697. * //range是已选中的选区
  4698. * var fragment = range.cloneContents(),
  4699. * node = document.createElement("div");
  4700. *
  4701. * node.appendChild( fragment );
  4702. *
  4703. * //output: <i>x</i>xx
  4704. * console.log( node.innerHTML );
  4705. *
  4706. * </script>
  4707. * </body>
  4708. * ```
  4709. */
  4710. cloneContents: function () {
  4711. return this.collapsed ? null : execContentsAction(this, 0);
  4712. },
  4713. /**
  4714. * 删除当前选区范围中的所有内容
  4715. * @method deleteContents
  4716. * @remind 执行完该操作后, 当前Range对象变成了闭合状态
  4717. * @return { UE.dom.Range } 当前操作的Range对象
  4718. * @example
  4719. * ```html
  4720. * <body>
  4721. * <!-- 中括号表示选区 -->
  4722. * <b>x<i>x[x</i>xx]x</b>
  4723. *
  4724. * <script>
  4725. * //range是已选中的选区
  4726. * range.deleteContents();
  4727. *
  4728. * //竖线表示闭合后的选区位置
  4729. * //output: <b>x<i>x</i>|x</b>
  4730. * console.log( document.body.innerHTML );
  4731. *
  4732. * //此时, range的各项属性为
  4733. * //output: B
  4734. * console.log( range.startContainer.tagName );
  4735. * //output: 2
  4736. * console.log( range.startOffset );
  4737. * //output: B
  4738. * console.log( range.endContainer.tagName );
  4739. * //output: 2
  4740. * console.log( range.endOffset );
  4741. * //output: true
  4742. * console.log( range.collapsed );
  4743. *
  4744. * </script>
  4745. * </body>
  4746. * ```
  4747. */
  4748. deleteContents: function () {
  4749. var txt;
  4750. if (!this.collapsed) {
  4751. execContentsAction(this, 1);
  4752. }
  4753. if (browser.webkit) {
  4754. txt = this.startContainer;
  4755. if (txt.nodeType == 3 && !txt.nodeValue.length) {
  4756. this.setStartBefore(txt).collapse(true);
  4757. domUtils.remove(txt);
  4758. }
  4759. }
  4760. return this;
  4761. },
  4762. /**
  4763. * 将当前选区的内容提取到一个DocumentFragment里
  4764. * @method extractContents
  4765. * @remind 执行该操作后, 选区将变成闭合状态
  4766. * @warning 执行该操作后, 原来选区所选中的内容将从dom树上剥离出来
  4767. * @return { DocumentFragment } 返回包含所提取内容的DocumentFragment对象
  4768. * @example
  4769. * ```html
  4770. * <body>
  4771. * <!-- 中括号表示选区 -->
  4772. * <b>x<i>x[x</i>xx]x</b>
  4773. *
  4774. * <script>
  4775. * //range是已选中的选区
  4776. * var fragment = range.extractContents(),
  4777. * node = document.createElement( "div" );
  4778. *
  4779. * node.appendChild( fragment );
  4780. *
  4781. * //竖线表示闭合后的选区位置
  4782. *
  4783. * //output: <b>x<i>x</i>|x</b>
  4784. * console.log( document.body.innerHTML );
  4785. * //output: <i>x</i>xx
  4786. * console.log( node.innerHTML );
  4787. *
  4788. * //此时, range的各项属性为
  4789. * //output: B
  4790. * console.log( range.startContainer.tagName );
  4791. * //output: 2
  4792. * console.log( range.startOffset );
  4793. * //output: B
  4794. * console.log( range.endContainer.tagName );
  4795. * //output: 2
  4796. * console.log( range.endOffset );
  4797. * //output: true
  4798. * console.log( range.collapsed );
  4799. *
  4800. * </script>
  4801. * </body>
  4802. */
  4803. extractContents: function () {
  4804. return this.collapsed ? null : execContentsAction(this, 2);
  4805. },
  4806. /**
  4807. * 设置Range的开始容器节点和偏移量
  4808. * @method setStart
  4809. * @remind 如果给定的节点是元素节点,那么offset指的是其子元素中索引为offset的元素,
  4810. * 如果是文本节点,那么offset指的是其文本内容的第offset个字符
  4811. * @remind 如果提供的容器节点是一个不能包含子元素的节点, 则该选区的开始容器将被设置
  4812. * 为该节点的父节点, 此时, 其距离开始容器的偏移量也变成了该节点在其父节点
  4813. * 中的索引
  4814. * @param { Node } node 将被设为当前选区开始边界容器的节点对象
  4815. * @param { int } offset 选区的开始位置偏移量
  4816. * @return { UE.dom.Range } 当前range对象
  4817. * @example
  4818. * ```html
  4819. * <!-- 选区 -->
  4820. * <b>xxx<i>x<span>xx</span>xx<em>xx</em>xxx</i>[xxx]</b>
  4821. *
  4822. * <script>
  4823. *
  4824. * //执行操作
  4825. * range.setStart( document.getElementsByTagName("i")[0], 1 );
  4826. *
  4827. * //此时, 选区变成了
  4828. * //<b>xxx<i>x[<span>xx</span>xx<em>xx</em>xxx</i>xxx]</b>
  4829. *
  4830. * </script>
  4831. * ```
  4832. * @example
  4833. * ```html
  4834. * <!-- 选区 -->
  4835. * <b>xxx<img>[xx]x</b>
  4836. *
  4837. * <script>
  4838. *
  4839. * //执行操作
  4840. * range.setStart( document.getElementsByTagName("img")[0], 3 );
  4841. *
  4842. * //此时, 选区变成了
  4843. * //<b>xxx[<img>xx]x</b>
  4844. *
  4845. * </script>
  4846. * ```
  4847. */
  4848. setStart: function (node, offset) {
  4849. return setEndPoint(true, node, offset, this);
  4850. },
  4851. /**
  4852. * 设置Range的结束容器和偏移量
  4853. * @method setEnd
  4854. * @param { Node } node 作为当前选区结束边界容器的节点对象
  4855. * @param { int } offset 结束边界的偏移量
  4856. * @see UE.dom.Range:setStart(Node,int)
  4857. * @return { UE.dom.Range } 当前range对象
  4858. */
  4859. setEnd: function (node, offset) {
  4860. return setEndPoint(false, node, offset, this);
  4861. },
  4862. /**
  4863. * 将Range开始位置设置到node节点之后
  4864. * @method setStartAfter
  4865. * @remind 该操作将会把给定节点的父节点作为range的开始容器, 且偏移量是该节点在其父节点中的位置索引+1
  4866. * @param { Node } node 选区的开始边界将紧接着该节点之后
  4867. * @return { UE.dom.Range } 当前range对象
  4868. * @example
  4869. * ```html
  4870. * <!-- 选区示例 -->
  4871. * <b>xx<i>xxx</i><span>xx[x</span>xxx]</b>
  4872. *
  4873. * <script>
  4874. *
  4875. * //执行操作
  4876. * range.setStartAfter( document.getElementsByTagName("i")[0] );
  4877. *
  4878. * //结果选区
  4879. * //<b>xx<i>xxx</i>[<span>xxx</span>xxx]</b>
  4880. *
  4881. * </script>
  4882. * ```
  4883. */
  4884. setStartAfter: function (node) {
  4885. return this.setStart(node.parentNode, domUtils.getNodeIndex(node) + 1);
  4886. },
  4887. /**
  4888. * 将Range开始位置设置到node节点之前
  4889. * @method setStartBefore
  4890. * @remind 该操作将会把给定节点的父节点作为range的开始容器, 且偏移量是该节点在其父节点中的位置索引
  4891. * @param { Node } node 新的选区开始位置在该节点之前
  4892. * @see UE.dom.Range:setStartAfter(Node)
  4893. * @return { UE.dom.Range } 当前range对象
  4894. */
  4895. setStartBefore: function (node) {
  4896. return this.setStart(node.parentNode, domUtils.getNodeIndex(node));
  4897. },
  4898. /**
  4899. * 将Range结束位置设置到node节点之后
  4900. * @method setEndAfter
  4901. * @remind 该操作将会把给定节点的父节点作为range的结束容器, 且偏移量是该节点在其父节点中的位置索引+1
  4902. * @param { Node } node 目标节点
  4903. * @see UE.dom.Range:setStartAfter(Node)
  4904. * @return { UE.dom.Range } 当前range对象
  4905. * @example
  4906. * ```html
  4907. * <!-- 选区示例 -->
  4908. * <b>[xx<i>xxx</i><span>xx]x</span>xxx</b>
  4909. *
  4910. * <script>
  4911. *
  4912. * //执行操作
  4913. * range.setStartAfter( document.getElementsByTagName("span")[0] );
  4914. *
  4915. * //结果选区
  4916. * //<b>[xx<i>xxx</i><span>xxx</span>]xxx</b>
  4917. *
  4918. * </script>
  4919. * ```
  4920. */
  4921. setEndAfter: function (node) {
  4922. return this.setEnd(node.parentNode, domUtils.getNodeIndex(node) + 1);
  4923. },
  4924. /**
  4925. * 将Range结束位置设置到node节点之前
  4926. * @method setEndBefore
  4927. * @remind 该操作将会把给定节点的父节点作为range的结束容器, 且偏移量是该节点在其父节点中的位置索引
  4928. * @param { Node } node 目标节点
  4929. * @see UE.dom.Range:setEndAfter(Node)
  4930. * @return { UE.dom.Range } 当前range对象
  4931. */
  4932. setEndBefore: function (node) {
  4933. return this.setEnd(node.parentNode, domUtils.getNodeIndex(node));
  4934. },
  4935. /**
  4936. * 设置Range的开始位置到node节点内的第一个子节点之前
  4937. * @method setStartAtFirst
  4938. * @remind 选区的开始容器将变成给定的节点, 且偏移量为0
  4939. * @remind 如果给定的节点是元素节点, 则该节点必须是允许包含子节点的元素。
  4940. * @param { Node } node 目标节点
  4941. * @see UE.dom.Range:setStartBefore(Node)
  4942. * @return { UE.dom.Range } 当前range对象
  4943. * @example
  4944. * ```html
  4945. * <!-- 选区示例 -->
  4946. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  4947. *
  4948. * <script>
  4949. *
  4950. * //执行操作
  4951. * range.setStartAtFirst( document.getElementsByTagName("i")[0] );
  4952. *
  4953. * //结果选区
  4954. * //<b>xx<i>[xxx</i><span>xx]x</span>xxx</b>
  4955. *
  4956. * </script>
  4957. * ```
  4958. */
  4959. setStartAtFirst: function (node) {
  4960. return this.setStart(node, 0);
  4961. },
  4962. /**
  4963. * 设置Range的开始位置到node节点内的最后一个节点之后
  4964. * @method setStartAtLast
  4965. * @remind 选区的开始容器将变成给定的节点, 且偏移量为该节点的子节点数
  4966. * @remind 如果给定的节点是元素节点, 则该节点必须是允许包含子节点的元素。
  4967. * @param { Node } node 目标节点
  4968. * @see UE.dom.Range:setStartAtFirst(Node)
  4969. * @return { UE.dom.Range } 当前range对象
  4970. */
  4971. setStartAtLast: function (node) {
  4972. return this.setStart(node, node.nodeType == 3 ? node.nodeValue.length : node.childNodes.length);
  4973. },
  4974. /**
  4975. * 设置Range的结束位置到node节点内的第一个节点之前
  4976. * @method setEndAtFirst
  4977. * @param { Node } node 目标节点
  4978. * @remind 选区的结束容器将变成给定的节点, 且偏移量为0
  4979. * @remind node必须是一个元素节点, 且必须是允许包含子节点的元素。
  4980. * @see UE.dom.Range:setStartAtFirst(Node)
  4981. * @return { UE.dom.Range } 当前range对象
  4982. */
  4983. setEndAtFirst: function (node) {
  4984. return this.setEnd(node, 0);
  4985. },
  4986. /**
  4987. * 设置Range的结束位置到node节点内的最后一个节点之后
  4988. * @method setEndAtLast
  4989. * @param { Node } node 目标节点
  4990. * @remind 选区的结束容器将变成给定的节点, 且偏移量为该节点的子节点数量
  4991. * @remind node必须是一个元素节点, 且必须是允许包含子节点的元素。
  4992. * @see UE.dom.Range:setStartAtFirst(Node)
  4993. * @return { UE.dom.Range } 当前range对象
  4994. */
  4995. setEndAtLast: function (node) {
  4996. return this.setEnd(node, node.nodeType == 3 ? node.nodeValue.length : node.childNodes.length);
  4997. },
  4998. /**
  4999. * 选中给定节点
  5000. * @method selectNode
  5001. * @remind 此时, 选区的开始容器和结束容器都是该节点的父节点, 其startOffset是该节点在父节点中的位置索引,
  5002. * 而endOffset为startOffset+1
  5003. * @param { Node } node 需要选中的节点
  5004. * @return { UE.dom.Range } 当前range对象,此时的range仅包含当前给定的节点对象
  5005. * @example
  5006. * ```html
  5007. * <!-- 选区示例 -->
  5008. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5009. *
  5010. * <script>
  5011. *
  5012. * //执行操作
  5013. * range.selectNode( document.getElementsByTagName("i")[0] );
  5014. *
  5015. * //结果选区
  5016. * //<b>xx[<i>xxx</i>]<span>xxx</span>xxx</b>
  5017. *
  5018. * </script>
  5019. * ```
  5020. */
  5021. selectNode: function (node) {
  5022. return this.setStartBefore(node).setEndAfter(node);
  5023. },
  5024. /**
  5025. * 选中给定节点内部的所有节点
  5026. * @method selectNodeContents
  5027. * @remind 此时, 选区的开始容器和结束容器都是该节点, 其startOffset为0,
  5028. * 而endOffset是该节点的子节点数。
  5029. * @param { Node } node 目标节点, 当前range将包含该节点内的所有节点
  5030. * @return { UE.dom.Range } 当前range对象, 此时range仅包含给定节点的所有子节点
  5031. * @example
  5032. * ```html
  5033. * <!-- 选区示例 -->
  5034. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5035. *
  5036. * <script>
  5037. *
  5038. * //执行操作
  5039. * range.selectNode( document.getElementsByTagName("b")[0] );
  5040. *
  5041. * //结果选区
  5042. * //<b>[xx<i>xxx</i><span>xxx</span>xxx]</b>
  5043. *
  5044. * </script>
  5045. * ```
  5046. */
  5047. selectNodeContents: function (node) {
  5048. return this.setStart(node, 0).setEndAtLast(node);
  5049. },
  5050. /**
  5051. * clone当前Range对象
  5052. * @method cloneRange
  5053. * @remind 返回的range是一个全新的range对象, 其内部所有属性与当前被clone的range相同。
  5054. * @return { UE.dom.Range } 当前range对象的一个副本
  5055. */
  5056. cloneRange: function () {
  5057. var me = this;
  5058. return new Range(me.document).setStart(me.startContainer, me.startOffset).setEnd(me.endContainer, me.endOffset);
  5059. },
  5060. /**
  5061. * 向当前选区的结束处闭合选区
  5062. * @method collapse
  5063. * @return { UE.dom.Range } 当前range对象
  5064. * @example
  5065. * ```html
  5066. * <!-- 选区示例 -->
  5067. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5068. *
  5069. * <script>
  5070. *
  5071. * //执行操作
  5072. * range.collapse();
  5073. *
  5074. * //结果选区
  5075. * //“|”表示选区已闭合
  5076. * //<b>xx<i>xxx</i><span>xx|x</span>xxx</b>
  5077. *
  5078. * </script>
  5079. * ```
  5080. */
  5081. /**
  5082. * 闭合当前选区,根据给定的toStart参数项决定是向当前选区开始处闭合还是向结束处闭合,
  5083. * 如果toStart的值为true,则向开始位置闭合, 反之,向结束位置闭合。
  5084. * @method collapse
  5085. * @param { Boolean } toStart 是否向选区开始处闭合
  5086. * @return { UE.dom.Range } 当前range对象,此时range对象处于闭合状态
  5087. * @see UE.dom.Range:collapse()
  5088. * @example
  5089. * ```html
  5090. * <!-- 选区示例 -->
  5091. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5092. *
  5093. * <script>
  5094. *
  5095. * //执行操作
  5096. * range.collapse( true );
  5097. *
  5098. * //结果选区
  5099. * //“|”表示选区已闭合
  5100. * //<b>xx<i>xxx</i><span>|xxx</span>xxx</b>
  5101. *
  5102. * </script>
  5103. * ```
  5104. */
  5105. collapse: function (toStart) {
  5106. var me = this;
  5107. if (toStart) {
  5108. me.endContainer = me.startContainer;
  5109. me.endOffset = me.startOffset;
  5110. } else {
  5111. me.startContainer = me.endContainer;
  5112. me.startOffset = me.endOffset;
  5113. }
  5114. me.collapsed = true;
  5115. return me;
  5116. },
  5117. /**
  5118. * 调整range的开始位置和结束位置,使其"收缩"到最小的位置
  5119. * @method shrinkBoundary
  5120. * @return { UE.dom.Range } 当前range对象
  5121. * @example
  5122. * ```html
  5123. * <span>xx<b>xx[</b>xxxxx]</span> => <span>xx<b>xx</b>[xxxxx]</span>
  5124. * ```
  5125. *
  5126. * @example
  5127. * ```html
  5128. * <!-- 选区示例 -->
  5129. * <b>x[xx</b><i>]xxx</i>
  5130. *
  5131. * <script>
  5132. *
  5133. * //执行收缩
  5134. * range.shrinkBoundary();
  5135. *
  5136. * //结果选区
  5137. * //<b>x[xx]</b><i>xxx</i>
  5138. * </script>
  5139. * ```
  5140. *
  5141. * @example
  5142. * ```html
  5143. * [<b><i>xxxx</i>xxxxxxx</b>] => <b><i>[xxxx</i>xxxxxxx]</b>
  5144. * ```
  5145. */
  5146. /**
  5147. * 调整range的开始位置和结束位置,使其"收缩"到最小的位置,
  5148. * 如果ignoreEnd的值为true,则忽略对结束位置的调整
  5149. * @method shrinkBoundary
  5150. * @param { Boolean } ignoreEnd 是否忽略对结束位置的调整
  5151. * @return { UE.dom.Range } 当前range对象
  5152. * @see UE.dom.domUtils.Range:shrinkBoundary()
  5153. */
  5154. shrinkBoundary: function (ignoreEnd) {
  5155. var me = this,
  5156. child,
  5157. collapsed = me.collapsed;
  5158. function check(node) {
  5159. return node.nodeType == 1 && !domUtils.isBookmarkNode(node) && !dtd.$empty[node.tagName] && !dtd.$nonChild[node.tagName]
  5160. }
  5161. while (me.startContainer.nodeType == 1 //是element
  5162. &&
  5163. (child = me.startContainer.childNodes[me.startOffset]) //子节点也是element
  5164. &&
  5165. check(child)) {
  5166. me.setStart(child, 0);
  5167. }
  5168. if (collapsed) {
  5169. return me.collapse(true);
  5170. }
  5171. if (!ignoreEnd) {
  5172. while (me.endContainer.nodeType == 1 //是element
  5173. &&
  5174. me.endOffset > 0 //如果是空元素就退出 endOffset=0那么endOffst-1为负值,childNodes[endOffset]报错
  5175. &&
  5176. (child = me.endContainer.childNodes[me.endOffset - 1]) //子节点也是element
  5177. &&
  5178. check(child)) {
  5179. me.setEnd(child, child.childNodes.length);
  5180. }
  5181. }
  5182. return me;
  5183. },
  5184. /**
  5185. * 获取离当前选区内包含的所有节点最近的公共祖先节点,
  5186. * @method getCommonAncestor
  5187. * @remind 返回的公共祖先节点一定不是range自身的容器节点, 但有可能是一个文本节点
  5188. * @return { Node } 当前range对象内所有节点的公共祖先节点
  5189. * @example
  5190. * ```html
  5191. * //选区示例
  5192. * <span>xxx<b>x[x<em>xx]x</em>xxx</b>xx</span>
  5193. * <script>
  5194. *
  5195. * var node = range.getCommonAncestor();
  5196. *
  5197. * //公共祖先节点是: b节点
  5198. * //输出: B
  5199. * console.log(node.tagName);
  5200. *
  5201. * </script>
  5202. * ```
  5203. */
  5204. /**
  5205. * 获取当前选区所包含的所有节点的公共祖先节点, 可以根据给定的参数 includeSelf 决定获取到
  5206. * 的公共祖先节点是否可以是当前选区的startContainer或endContainer节点, 如果 includeSelf
  5207. * 的取值为true, 则返回的节点可以是自身的容器节点, 否则, 则不能是容器节点
  5208. * @method getCommonAncestor
  5209. * @param { Boolean } includeSelf 是否允许获取到的公共祖先节点是当前range对象的容器节点
  5210. * @return { Node } 当前range对象内所有节点的公共祖先节点
  5211. * @see UE.dom.Range:getCommonAncestor()
  5212. * @example
  5213. * ```html
  5214. * <body>
  5215. *
  5216. * <!-- 选区示例 -->
  5217. * <b>xxx<i>xxxx<span>xx[x</span>xx]x</i>xxxxxxx</b>
  5218. *
  5219. * <script>
  5220. *
  5221. * var node = range.getCommonAncestor( false );
  5222. *
  5223. * //这里的公共祖先节点是B而不是I, 是因为参数限制了获取到的节点不能是容器节点
  5224. * //output: B
  5225. * console.log( node.tagName );
  5226. *
  5227. * </script>
  5228. *
  5229. * </body>
  5230. * ```
  5231. */
  5232. /**
  5233. * 获取当前选区所包含的所有节点的公共祖先节点, 可以根据给定的参数 includeSelf 决定获取到
  5234. * 的公共祖先节点是否可以是当前选区的startContainer或endContainer节点, 如果 includeSelf
  5235. * 的取值为true, 则返回的节点可以是自身的容器节点, 否则, 则不能是容器节点; 同时可以根据
  5236. * ignoreTextNode 参数的取值决定是否忽略类型为文本节点的祖先节点。
  5237. * @method getCommonAncestor
  5238. * @param { Boolean } includeSelf 是否允许获取到的公共祖先节点是当前range对象的容器节点
  5239. * @param { Boolean } ignoreTextNode 获取祖先节点的过程中是否忽略类型为文本节点的祖先节点
  5240. * @return { Node } 当前range对象内所有节点的公共祖先节点
  5241. * @see UE.dom.Range:getCommonAncestor()
  5242. * @see UE.dom.Range:getCommonAncestor(Boolean)
  5243. * @example
  5244. * ```html
  5245. * <body>
  5246. *
  5247. * <!-- 选区示例 -->
  5248. * <b>xxx<i>xxxx<span>x[x]x</span>xxx</i>xxxxxxx</b>
  5249. *
  5250. * <script>
  5251. *
  5252. * var node = range.getCommonAncestor( true, false );
  5253. *
  5254. * //output: SPAN
  5255. * console.log( node.tagName );
  5256. *
  5257. * </script>
  5258. *
  5259. * </body>
  5260. * ```
  5261. */
  5262. getCommonAncestor: function (includeSelf, ignoreTextNode) {
  5263. var me = this,
  5264. start = me.startContainer,
  5265. end = me.endContainer;
  5266. if (start === end) {
  5267. if (includeSelf && selectOneNode(this)) {
  5268. start = start.childNodes[me.startOffset];
  5269. if (start.nodeType == 1)
  5270. return start;
  5271. }
  5272. //只有在上来就相等的情况下才会出现是文本的情况
  5273. return ignoreTextNode && start.nodeType == 3 ? start.parentNode : start;
  5274. }
  5275. return domUtils.getCommonAncestor(start, end);
  5276. },
  5277. /**
  5278. * 调整当前Range的开始和结束边界容器,如果是容器节点是文本节点,就调整到包含该文本节点的父节点上
  5279. * @method trimBoundary
  5280. * @remind 该操作有可能会引起文本节点被切开
  5281. * @return { UE.dom.Range } 当前range对象
  5282. * @example
  5283. * ```html
  5284. *
  5285. * //选区示例
  5286. * <b>xxx<i>[xxxxx]</i>xxx</b>
  5287. *
  5288. * <script>
  5289. * //未调整前, 选区的开始容器和结束都是文本节点
  5290. * //执行调整
  5291. * range.trimBoundary();
  5292. *
  5293. * //调整之后, 容器节点变成了i节点
  5294. * //<b>xxx[<i>xxxxx</i>]xxx</b>
  5295. * </script>
  5296. * ```
  5297. */
  5298. /**
  5299. * 调整当前Range的开始和结束边界容器,如果是容器节点是文本节点,就调整到包含该文本节点的父节点上,
  5300. * 可以根据 ignoreEnd 参数的值决定是否调整对结束边界的调整
  5301. * @method trimBoundary
  5302. * @param { Boolean } ignoreEnd 是否忽略对结束边界的调整
  5303. * @return { UE.dom.Range } 当前range对象
  5304. * @example
  5305. * ```html
  5306. *
  5307. * //选区示例
  5308. * <b>xxx<i>[xxxxx]</i>xxx</b>
  5309. *
  5310. * <script>
  5311. * //未调整前, 选区的开始容器和结束都是文本节点
  5312. * //执行调整
  5313. * range.trimBoundary( true );
  5314. *
  5315. * //调整之后, 开始容器节点变成了i节点
  5316. * //但是, 结束容器没有发生变化
  5317. * //<b>xxx[<i>xxxxx]</i>xxx</b>
  5318. * </script>
  5319. * ```
  5320. */
  5321. trimBoundary: function (ignoreEnd) {
  5322. this.txtToElmBoundary();
  5323. var start = this.startContainer,
  5324. offset = this.startOffset,
  5325. collapsed = this.collapsed,
  5326. end = this.endContainer;
  5327. if (start.nodeType == 3) {
  5328. if (offset == 0) {
  5329. this.setStartBefore(start);
  5330. } else {
  5331. if (offset >= start.nodeValue.length) {
  5332. this.setStartAfter(start);
  5333. } else {
  5334. var textNode = domUtils.split(start, offset);
  5335. //跟新结束边界
  5336. if (start === end) {
  5337. this.setEnd(textNode, this.endOffset - offset);
  5338. } else if (start.parentNode === end) {
  5339. this.endOffset += 1;
  5340. }
  5341. this.setStartBefore(textNode);
  5342. }
  5343. }
  5344. if (collapsed) {
  5345. return this.collapse(true);
  5346. }
  5347. }
  5348. if (!ignoreEnd) {
  5349. offset = this.endOffset;
  5350. end = this.endContainer;
  5351. if (end.nodeType == 3) {
  5352. if (offset == 0) {
  5353. this.setEndBefore(end);
  5354. } else {
  5355. offset < end.nodeValue.length && domUtils.split(end, offset);
  5356. this.setEndAfter(end);
  5357. }
  5358. }
  5359. }
  5360. return this;
  5361. },
  5362. /**
  5363. * 如果选区在文本的边界上,就扩展选区到文本的父节点上, 如果当前选区是闭合的, 则什么也不做
  5364. * @method txtToElmBoundary
  5365. * @remind 该操作不会修改dom节点
  5366. * @return { UE.dom.Range } 当前range对象
  5367. */
  5368. /**
  5369. * 如果选区在文本的边界上,就扩展选区到文本的父节点上, 如果当前选区是闭合的, 则根据参数项
  5370. * ignoreCollapsed 的值决定是否执行该调整
  5371. * @method txtToElmBoundary
  5372. * @param { Boolean } ignoreCollapsed 是否忽略选区的闭合状态, 如果该参数取值为true, 则
  5373. * 不论选区是否闭合, 都会执行该操作, 反之, 则不会对闭合的选区执行该操作
  5374. * @return { UE.dom.Range } 当前range对象
  5375. */
  5376. txtToElmBoundary: function (ignoreCollapsed) {
  5377. function adjust(r, c) {
  5378. var container = r[c + 'Container'],
  5379. offset = r[c + 'Offset'];
  5380. if (container.nodeType == 3) {
  5381. if (!offset) {
  5382. r['set' + c.replace(/(\w)/, function (a) {
  5383. return a.toUpperCase();
  5384. }) + 'Before'](container);
  5385. } else if (offset >= container.nodeValue.length) {
  5386. r['set' + c.replace(/(\w)/, function (a) {
  5387. return a.toUpperCase();
  5388. }) + 'After'](container);
  5389. }
  5390. }
  5391. }
  5392. if (ignoreCollapsed || !this.collapsed) {
  5393. adjust(this, 'start');
  5394. adjust(this, 'end');
  5395. }
  5396. return this;
  5397. },
  5398. /**
  5399. * 在当前选区的开始位置前插入节点,新插入的节点会被该range包含
  5400. * @method insertNode
  5401. * @param { Node } node 需要插入的节点
  5402. * @remind 插入的节点可以是一个DocumentFragment依次插入多个节点
  5403. * @return { UE.dom.Range } 当前range对象
  5404. */
  5405. insertNode: function (node) {
  5406. var first = node,
  5407. length = 1;
  5408. if (node.nodeType == 11) {
  5409. first = node.firstChild;
  5410. length = node.childNodes.length;
  5411. }
  5412. this.trimBoundary(true);
  5413. var start = this.startContainer,
  5414. offset = this.startOffset;
  5415. var nextNode = start.childNodes[offset];
  5416. if (nextNode) {
  5417. start.insertBefore(node, nextNode);
  5418. } else {
  5419. start.appendChild(node);
  5420. }
  5421. if (first.parentNode === this.endContainer) {
  5422. this.endOffset = this.endOffset + length;
  5423. }
  5424. return this.setStartBefore(first);
  5425. },
  5426. /**
  5427. * 闭合选区到当前选区的开始位置, 并且定位光标到闭合后的位置
  5428. * @method setCursor
  5429. * @return { UE.dom.Range } 当前range对象
  5430. * @see UE.dom.Range:collapse()
  5431. */
  5432. /**
  5433. * 闭合选区,可以根据参数toEnd的值控制选区是向前闭合还是向后闭合, 并且定位光标到闭合后的位置。
  5434. * @method setCursor
  5435. * @param { Boolean } toEnd 是否向后闭合, 如果为true, 则闭合选区时, 将向结束容器方向闭合,
  5436. * 反之,则向开始容器方向闭合
  5437. * @return { UE.dom.Range } 当前range对象
  5438. * @see UE.dom.Range:collapse(Boolean)
  5439. */
  5440. setCursor: function (toEnd, noFillData) {
  5441. return this.collapse(!toEnd).select(noFillData);
  5442. },
  5443. /**
  5444. * 创建当前range的一个书签,记录下当前range的位置,方便当dom树改变时,还能找回原来的选区位置
  5445. * @method createBookmark
  5446. * @param { Boolean } serialize 控制返回的标记位置是对当前位置的引用还是ID,如果该值为true,则
  5447. * 返回标记位置的ID, 反之则返回标记位置节点的引用
  5448. * @return { Object } 返回一个书签记录键值对, 其包含的key有: start => 开始标记的ID或者引用,
  5449. * end => 结束标记的ID或引用, id => 当前标记的类型, 如果为true,则表示
  5450. * 返回的记录的类型为ID, 反之则为引用
  5451. */
  5452. createBookmark: function (serialize, same) {
  5453. var endNode,
  5454. startNode = this.document.createElement('span');
  5455. startNode.style.cssText = 'display:none;line-height:0px;';
  5456. startNode.appendChild(this.document.createTextNode('\u200D'));
  5457. startNode.id = '_baidu_bookmark_start_' + (same ? '' : guid++);
  5458. if (!this.collapsed) {
  5459. endNode = startNode.cloneNode(true);
  5460. endNode.id = '_baidu_bookmark_end_' + (same ? '' : guid++);
  5461. }
  5462. this.insertNode(startNode);
  5463. if (endNode) {
  5464. this.collapse().insertNode(endNode).setEndBefore(endNode);
  5465. }
  5466. this.setStartAfter(startNode);
  5467. return {
  5468. start: serialize ? startNode.id : startNode,
  5469. end: endNode ? serialize ? endNode.id : endNode : null,
  5470. id: serialize
  5471. }
  5472. },
  5473. /**
  5474. * 调整当前range的边界到书签位置,并删除该书签对象所标记的位置内的节点
  5475. * @method moveToBookmark
  5476. * @param { BookMark } bookmark createBookmark所创建的标签对象
  5477. * @return { UE.dom.Range } 当前range对象
  5478. * @see UE.dom.Range:createBookmark(Boolean)
  5479. */
  5480. moveToBookmark: function (bookmark) {
  5481. var start = bookmark.id ? this.document.getElementById(bookmark.start) : bookmark.start,
  5482. end = bookmark.end && bookmark.id ? this.document.getElementById(bookmark.end) : bookmark.end;
  5483. this.setStartBefore(start);
  5484. domUtils.remove(start);
  5485. if (end) {
  5486. this.setEndBefore(end);
  5487. domUtils.remove(end);
  5488. } else {
  5489. this.collapse(true);
  5490. }
  5491. return this;
  5492. },
  5493. /**
  5494. * 调整range的边界,使其"放大"到最近的父节点
  5495. * @method enlarge
  5496. * @remind 会引起选区的变化
  5497. * @return { UE.dom.Range } 当前range对象
  5498. */
  5499. /**
  5500. * 调整range的边界,使其"放大"到最近的父节点,根据参数 toBlock 的取值, 可以
  5501. * 要求扩大之后的父节点是block节点
  5502. * @method enlarge
  5503. * @param { Boolean } toBlock 是否要求扩大之后的父节点必须是block节点
  5504. * @return { UE.dom.Range } 当前range对象
  5505. */
  5506. enlarge: function (toBlock, stopFn) {
  5507. var isBody = domUtils.isBody,
  5508. pre, node, tmp = this.document.createTextNode('');
  5509. if (toBlock) {
  5510. node = this.startContainer;
  5511. if (node.nodeType == 1) {
  5512. if (node.childNodes[this.startOffset]) {
  5513. pre = node = node.childNodes[this.startOffset]
  5514. } else {
  5515. node.appendChild(tmp);
  5516. pre = node = tmp;
  5517. }
  5518. } else {
  5519. pre = node;
  5520. }
  5521. while (1) {
  5522. if (domUtils.isBlockElm(node)) {
  5523. node = pre;
  5524. while ((pre = node.previousSibling) && !domUtils.isBlockElm(pre)) {
  5525. node = pre;
  5526. }
  5527. this.setStartBefore(node);
  5528. break;
  5529. }
  5530. pre = node;
  5531. node = node.parentNode;
  5532. }
  5533. node = this.endContainer;
  5534. if (node.nodeType == 1) {
  5535. if (pre = node.childNodes[this.endOffset]) {
  5536. node.insertBefore(tmp, pre);
  5537. } else {
  5538. node.appendChild(tmp);
  5539. }
  5540. pre = node = tmp;
  5541. } else {
  5542. pre = node;
  5543. }
  5544. while (1) {
  5545. if (domUtils.isBlockElm(node)) {
  5546. node = pre;
  5547. while ((pre = node.nextSibling) && !domUtils.isBlockElm(pre)) {
  5548. node = pre;
  5549. }
  5550. this.setEndAfter(node);
  5551. break;
  5552. }
  5553. pre = node;
  5554. node = node.parentNode;
  5555. }
  5556. if (tmp.parentNode === this.endContainer) {
  5557. this.endOffset--;
  5558. }
  5559. domUtils.remove(tmp);
  5560. }
  5561. // 扩展边界到最大
  5562. if (!this.collapsed) {
  5563. while (this.startOffset == 0) {
  5564. if (stopFn && stopFn(this.startContainer)) {
  5565. break;
  5566. }
  5567. if (isBody(this.startContainer)) {
  5568. break;
  5569. }
  5570. this.setStartBefore(this.startContainer);
  5571. }
  5572. while (this.endOffset == (this.endContainer.nodeType == 1 ? this.endContainer.childNodes.length : this.endContainer.nodeValue.length)) {
  5573. if (stopFn && stopFn(this.endContainer)) {
  5574. break;
  5575. }
  5576. if (isBody(this.endContainer)) {
  5577. break;
  5578. }
  5579. this.setEndAfter(this.endContainer);
  5580. }
  5581. }
  5582. return this;
  5583. },
  5584. enlargeToBlockElm: function (ignoreEnd) {
  5585. while (!domUtils.isBlockElm(this.startContainer)) {
  5586. this.setStartBefore(this.startContainer);
  5587. }
  5588. if (!ignoreEnd) {
  5589. while (!domUtils.isBlockElm(this.endContainer)) {
  5590. this.setEndAfter(this.endContainer);
  5591. }
  5592. }
  5593. return this;
  5594. },
  5595. /**
  5596. * 调整Range的边界,使其"缩小"到最合适的位置
  5597. * @method adjustmentBoundary
  5598. * @return { UE.dom.Range } 当前range对象
  5599. * @see UE.dom.Range:shrinkBoundary()
  5600. */
  5601. adjustmentBoundary: function () {
  5602. if (!this.collapsed) {
  5603. while (!domUtils.isBody(this.startContainer) &&
  5604. this.startOffset == this.startContainer[this.startContainer.nodeType == 3 ? 'nodeValue' : 'childNodes'].length &&
  5605. this.startContainer[this.startContainer.nodeType == 3 ? 'nodeValue' : 'childNodes'].length
  5606. ) {
  5607. this.setStartAfter(this.startContainer);
  5608. }
  5609. while (!domUtils.isBody(this.endContainer) && !this.endOffset &&
  5610. this.endContainer[this.endContainer.nodeType == 3 ? 'nodeValue' : 'childNodes'].length
  5611. ) {
  5612. this.setEndBefore(this.endContainer);
  5613. }
  5614. }
  5615. return this;
  5616. },
  5617. /**
  5618. * 给range选区中的内容添加给定的inline标签
  5619. * @method applyInlineStyle
  5620. * @param { String } tagName 需要添加的标签名
  5621. * @example
  5622. * ```html
  5623. * <p>xxxx[xxxx]x</p> ==> range.applyInlineStyle("strong") ==> <p>xxxx[<strong>xxxx</strong>]x</p>
  5624. * ```
  5625. */
  5626. /**
  5627. * 给range选区中的内容添加给定的inline标签, 并且为标签附加上一些初始化属性。
  5628. * @method applyInlineStyle
  5629. * @param { String } tagName 需要添加的标签名
  5630. * @param { Object } attrs 跟随新添加的标签的属性
  5631. * @return { UE.dom.Range } 当前选区
  5632. * @example
  5633. * ```html
  5634. * <p>xxxx[xxxx]x</p>
  5635. *
  5636. * ==>
  5637. *
  5638. * <!-- 执行操作 -->
  5639. * range.applyInlineStyle("strong",{"style":"font-size:12px"})
  5640. *
  5641. * ==>
  5642. *
  5643. * <p>xxxx[<strong style="font-size:12px">xxxx</strong>]x</p>
  5644. * ```
  5645. */
  5646. applyInlineStyle: function (tagName, attrs, list) {
  5647. if (this.collapsed) return this;
  5648. this.trimBoundary().enlarge(false,
  5649. function (node) {
  5650. return node.nodeType == 1 && domUtils.isBlockElm(node)
  5651. }).adjustmentBoundary();
  5652. var bookmark = this.createBookmark(),
  5653. end = bookmark.end,
  5654. filterFn = function (node) {
  5655. return node.nodeType == 1 ? node.tagName.toLowerCase() != 'br' : !domUtils.isWhitespace(node);
  5656. },
  5657. current = domUtils.getNextDomNode(bookmark.start, false, filterFn),
  5658. node,
  5659. pre,
  5660. range = this.cloneRange();
  5661. while (current && (domUtils.getPosition(current, end) & domUtils.POSITION_PRECEDING)) {
  5662. if (current.nodeType == 3 || dtd[tagName][current.tagName]) {
  5663. range.setStartBefore(current);
  5664. node = current;
  5665. while (node && (node.nodeType == 3 || dtd[tagName][node.tagName]) && node !== end) {
  5666. pre = node;
  5667. node = domUtils.getNextDomNode(node, node.nodeType == 1, null, function (parent) {
  5668. return dtd[tagName][parent.tagName];
  5669. });
  5670. }
  5671. var frag = range.setEndAfter(pre).extractContents(),
  5672. elm;
  5673. if (list && list.length > 0) {
  5674. var level, top;
  5675. top = level = list[0].cloneNode(false);
  5676. for (var i = 1, ci; ci = list[i++];) {
  5677. level.appendChild(ci.cloneNode(false));
  5678. level = level.firstChild;
  5679. }
  5680. elm = level;
  5681. } else {
  5682. elm = range.document.createElement(tagName);
  5683. }
  5684. if (attrs) {
  5685. domUtils.setAttributes(elm, attrs);
  5686. }
  5687. elm.appendChild(frag);
  5688. range.insertNode(list ? top : elm);
  5689. //处理下滑线在a上的情况
  5690. var aNode;
  5691. if (tagName == 'span' && attrs.style && /text\-decoration/.test(attrs.style) && (aNode = domUtils.findParentByTagName(elm, 'a', true))) {
  5692. domUtils.setAttributes(aNode, attrs);
  5693. domUtils.remove(elm, true);
  5694. elm = aNode;
  5695. } else {
  5696. domUtils.mergeSibling(elm);
  5697. domUtils.clearEmptySibling(elm);
  5698. }
  5699. //去除子节点相同的
  5700. domUtils.mergeChild(elm, attrs);
  5701. current = domUtils.getNextDomNode(elm, false, filterFn);
  5702. domUtils.mergeToParent(elm);
  5703. if (node === end) {
  5704. break;
  5705. }
  5706. } else {
  5707. current = domUtils.getNextDomNode(current, true, filterFn);
  5708. }
  5709. }
  5710. return this.moveToBookmark(bookmark);
  5711. },
  5712. /**
  5713. * 移除当前选区内指定的inline标签,但保留其中的内容
  5714. * @method removeInlineStyle
  5715. * @param { String } tagName 需要移除的标签名
  5716. * @return { UE.dom.Range } 当前的range对象
  5717. * @example
  5718. * ```html
  5719. * xx[x<span>xxx<em>yyy</em>zz]z</span> => range.removeInlineStyle(["em"]) => xx[x<span>xxxyyyzz]z</span>
  5720. * ```
  5721. */
  5722. /**
  5723. * 移除当前选区内指定的一组inline标签,但保留其中的内容
  5724. * @method removeInlineStyle
  5725. * @param { Array } tagNameArr 需要移除的标签名的数组
  5726. * @return { UE.dom.Range } 当前的range对象
  5727. * @see UE.dom.Range:removeInlineStyle(String)
  5728. */
  5729. removeInlineStyle: function (tagNames) {
  5730. if (this.collapsed) return this;
  5731. tagNames = utils.isArray(tagNames) ? tagNames : [tagNames];
  5732. this.shrinkBoundary().adjustmentBoundary();
  5733. var start = this.startContainer,
  5734. end = this.endContainer;
  5735. while (1) {
  5736. if (start.nodeType == 1) {
  5737. if (utils.indexOf(tagNames, start.tagName.toLowerCase()) > -1) {
  5738. break;
  5739. }
  5740. if (start.tagName.toLowerCase() == 'body') {
  5741. start = null;
  5742. break;
  5743. }
  5744. }
  5745. start = start.parentNode;
  5746. }
  5747. while (1) {
  5748. if (end.nodeType == 1) {
  5749. if (utils.indexOf(tagNames, end.tagName.toLowerCase()) > -1) {
  5750. break;
  5751. }
  5752. if (end.tagName.toLowerCase() == 'body') {
  5753. end = null;
  5754. break;
  5755. }
  5756. }
  5757. end = end.parentNode;
  5758. }
  5759. var bookmark = this.createBookmark(),
  5760. frag,
  5761. tmpRange;
  5762. if (start) {
  5763. tmpRange = this.cloneRange().setEndBefore(bookmark.start).setStartBefore(start);
  5764. frag = tmpRange.extractContents();
  5765. tmpRange.insertNode(frag);
  5766. domUtils.clearEmptySibling(start, true);
  5767. start.parentNode.insertBefore(bookmark.start, start);
  5768. }
  5769. if (end) {
  5770. tmpRange = this.cloneRange().setStartAfter(bookmark.end).setEndAfter(end);
  5771. frag = tmpRange.extractContents();
  5772. tmpRange.insertNode(frag);
  5773. domUtils.clearEmptySibling(end, false, true);
  5774. end.parentNode.insertBefore(bookmark.end, end.nextSibling);
  5775. }
  5776. var current = domUtils.getNextDomNode(bookmark.start, false, function (node) {
  5777. return node.nodeType == 1;
  5778. }),
  5779. next;
  5780. while (current && current !== bookmark.end) {
  5781. next = domUtils.getNextDomNode(current, true, function (node) {
  5782. return node.nodeType == 1;
  5783. });
  5784. if (utils.indexOf(tagNames, current.tagName.toLowerCase()) > -1) {
  5785. domUtils.remove(current, true);
  5786. }
  5787. current = next;
  5788. }
  5789. return this.moveToBookmark(bookmark);
  5790. },
  5791. /**
  5792. * 获取当前选中的自闭合的节点
  5793. * @method getClosedNode
  5794. * @return { Node | NULL } 如果当前选中的是自闭合节点, 则返回该节点, 否则返回NULL
  5795. */
  5796. getClosedNode: function () {
  5797. var node;
  5798. if (!this.collapsed) {
  5799. var range = this.cloneRange().adjustmentBoundary().shrinkBoundary();
  5800. if (selectOneNode(range)) {
  5801. var child = range.startContainer.childNodes[range.startOffset];
  5802. if (child && child.nodeType == 1 && (dtd.$empty[child.tagName] || dtd.$nonChild[child.tagName])) {
  5803. node = child;
  5804. }
  5805. }
  5806. }
  5807. return node;
  5808. },
  5809. /**
  5810. * 在页面上高亮range所表示的选区
  5811. * @method select
  5812. * @return { UE.dom.Range } 返回当前Range对象
  5813. */
  5814. //这里不区分ie9以上,trace:3824
  5815. select: browser.ie ? function (noFillData, textRange) {
  5816. var nativeRange;
  5817. if (!this.collapsed)
  5818. this.shrinkBoundary();
  5819. var node = this.getClosedNode();
  5820. if (node && !textRange) {
  5821. try {
  5822. nativeRange = this.document.body.createControlRange();
  5823. nativeRange.addElement(node);
  5824. nativeRange.select();
  5825. } catch (e) {}
  5826. return this;
  5827. }
  5828. var bookmark = this.createBookmark(),
  5829. start = bookmark.start,
  5830. end;
  5831. nativeRange = this.document.body.createTextRange();
  5832. nativeRange.moveToElementText(start);
  5833. nativeRange.moveStart('character', 1);
  5834. if (!this.collapsed) {
  5835. var nativeRangeEnd = this.document.body.createTextRange();
  5836. end = bookmark.end;
  5837. nativeRangeEnd.moveToElementText(end);
  5838. nativeRange.setEndPoint('EndToEnd', nativeRangeEnd);
  5839. } else {
  5840. if (!noFillData && this.startContainer.nodeType != 3) {
  5841. //使用<span>|x<span>固定住光标
  5842. var tmpText = this.document.createTextNode(fillChar),
  5843. tmp = this.document.createElement('span');
  5844. tmp.appendChild(this.document.createTextNode(fillChar));
  5845. start.parentNode.insertBefore(tmp, start);
  5846. start.parentNode.insertBefore(tmpText, start);
  5847. //当点b,i,u时,不能清除i上边的b
  5848. removeFillData(this.document, tmpText);
  5849. fillData = tmpText;
  5850. mergeSibling(tmp, 'previousSibling');
  5851. mergeSibling(start, 'nextSibling');
  5852. nativeRange.moveStart('character', -1);
  5853. nativeRange.collapse(true);
  5854. }
  5855. }
  5856. this.moveToBookmark(bookmark);
  5857. tmp && domUtils.remove(tmp);
  5858. //IE在隐藏状态下不支持range操作,catch一下
  5859. try {
  5860. nativeRange.select();
  5861. } catch (e) {}
  5862. return this;
  5863. } : function (notInsertFillData) {
  5864. function checkOffset(rng) {
  5865. function check(node, offset, dir) {
  5866. if (node.nodeType == 3 && node.nodeValue.length < offset) {
  5867. rng[dir + 'Offset'] = node.nodeValue.length
  5868. }
  5869. }
  5870. check(rng.startContainer, rng.startOffset, 'start');
  5871. check(rng.endContainer, rng.endOffset, 'end');
  5872. }
  5873. var win = domUtils.getWindow(this.document),
  5874. sel = win.getSelection(),
  5875. txtNode;
  5876. //FF下关闭自动长高时滚动条在关闭dialog时会跳
  5877. //ff下如果不body.focus将不能定位闭合光标到编辑器内
  5878. browser.gecko ? this.document.body.focus() : win.focus();
  5879. if (sel) {
  5880. sel.removeAllRanges();
  5881. // trace:870 chrome/safari后边是br对于闭合得range不能定位 所以去掉了判断
  5882. // this.startContainer.nodeType != 3 &&! ((child = this.startContainer.childNodes[this.startOffset]) && child.nodeType == 1 && child.tagName == 'BR'
  5883. if (this.collapsed && !notInsertFillData) {
  5884. // //opear如果没有节点接着,原生的不能够定位,不能在body的第一级插入空白节点
  5885. // if (notInsertFillData && browser.opera && !domUtils.isBody(this.startContainer) && this.startContainer.nodeType == 1) {
  5886. // var tmp = this.document.createTextNode('');
  5887. // this.insertNode(tmp).setStart(tmp, 0).collapse(true);
  5888. // }
  5889. //
  5890. //处理光标落在文本节点的情况
  5891. //处理以下的情况
  5892. //<b>|xxxx</b>
  5893. //<b>xxxx</b>|xxxx
  5894. //xxxx<b>|</b>
  5895. var start = this.startContainer,
  5896. child = start;
  5897. if (start.nodeType == 1) {
  5898. child = start.childNodes[this.startOffset];
  5899. }
  5900. if (!(start.nodeType == 3 && this.startOffset) &&
  5901. (child ?
  5902. (!child.previousSibling || child.previousSibling.nodeType != 3) :
  5903. (!start.lastChild || start.lastChild.nodeType != 3)
  5904. )
  5905. ) {
  5906. txtNode = this.document.createTextNode(fillChar);
  5907. //跟着前边走
  5908. this.insertNode(txtNode);
  5909. removeFillData(this.document, txtNode);
  5910. mergeSibling(txtNode, 'previousSibling');
  5911. mergeSibling(txtNode, 'nextSibling');
  5912. fillData = txtNode;
  5913. this.setStart(txtNode, browser.webkit ? 1 : 0).collapse(true);
  5914. }
  5915. }
  5916. var nativeRange = this.document.createRange();
  5917. if (this.collapsed && browser.opera && this.startContainer.nodeType == 1) {
  5918. var child = this.startContainer.childNodes[this.startOffset];
  5919. if (!child) {
  5920. //往前靠拢
  5921. child = this.startContainer.lastChild;
  5922. if (child && domUtils.isBr(child)) {
  5923. this.setStartBefore(child).collapse(true);
  5924. }
  5925. } else {
  5926. //向后靠拢
  5927. while (child && domUtils.isBlockElm(child)) {
  5928. if (child.nodeType == 1 && child.childNodes[0]) {
  5929. child = child.childNodes[0]
  5930. } else {
  5931. break;
  5932. }
  5933. }
  5934. child && this.setStartBefore(child).collapse(true)
  5935. }
  5936. }
  5937. //是createAddress最后一位算的不准,现在这里进行微调
  5938. checkOffset(this);
  5939. nativeRange.setStart(this.startContainer, this.startOffset);
  5940. nativeRange.setEnd(this.endContainer, this.endOffset);
  5941. sel.addRange(nativeRange);
  5942. }
  5943. return this;
  5944. },
  5945. /**
  5946. * 滚动到当前range开始的位置
  5947. * @method scrollToView
  5948. * @param { Window } win 当前range对象所属的window对象
  5949. * @return { UE.dom.Range } 当前Range对象
  5950. */
  5951. /**
  5952. * 滚动到距离当前range开始位置 offset 的位置处
  5953. * @method scrollToView
  5954. * @param { Window } win 当前range对象所属的window对象
  5955. * @param { Number } offset 距离range开始位置处的偏移量, 如果为正数, 则向下偏移, 反之, 则向上偏移
  5956. * @return { UE.dom.Range } 当前Range对象
  5957. */
  5958. scrollToView: function (win, offset) {
  5959. win = win ? window : domUtils.getWindow(this.document);
  5960. var me = this,
  5961. span = me.document.createElement('span');
  5962. //trace:717
  5963. span.innerHTML = '&nbsp;';
  5964. me.cloneRange().insertNode(span);
  5965. domUtils.scrollToView(span, win, offset);
  5966. domUtils.remove(span);
  5967. return me;
  5968. },
  5969. /**
  5970. * 判断当前选区内容是否占位符
  5971. * @private
  5972. * @method inFillChar
  5973. * @return { Boolean } 如果是占位符返回true,否则返回false
  5974. */
  5975. inFillChar: function () {
  5976. var start = this.startContainer;
  5977. if (this.collapsed && start.nodeType == 3 &&
  5978. start.nodeValue.replace(new RegExp('^' + domUtils.fillChar), '').length + 1 == start.nodeValue.length
  5979. ) {
  5980. return true;
  5981. }
  5982. return false;
  5983. },
  5984. /**
  5985. * 保存
  5986. * @method createAddress
  5987. * @private
  5988. * @return { Boolean } 返回开始和结束的位置
  5989. * @example
  5990. * ```html
  5991. * <body>
  5992. * <p>
  5993. * aaaa
  5994. * <em>
  5995. * <!-- 选区开始 -->
  5996. * bbbb
  5997. * <!-- 选区结束 -->
  5998. * </em>
  5999. * </p>
  6000. *
  6001. * <script>
  6002. * //output: {startAddress:[0,1,0,0],endAddress:[0,1,0,4]}
  6003. * console.log( range.createAddress() );
  6004. * </script>
  6005. * </body>
  6006. * ```
  6007. */
  6008. createAddress: function (ignoreEnd, ignoreTxt) {
  6009. var addr = {},
  6010. me = this;
  6011. function getAddress(isStart) {
  6012. var node = isStart ? me.startContainer : me.endContainer;
  6013. var parents = domUtils.findParents(node, true, function (node) {
  6014. return !domUtils.isBody(node)
  6015. }),
  6016. addrs = [];
  6017. for (var i = 0, ci; ci = parents[i++];) {
  6018. addrs.push(domUtils.getNodeIndex(ci, ignoreTxt));
  6019. }
  6020. var firstIndex = 0;
  6021. if (ignoreTxt) {
  6022. if (node.nodeType == 3) {
  6023. var tmpNode = node.previousSibling;
  6024. while (tmpNode && tmpNode.nodeType == 3) {
  6025. firstIndex += tmpNode.nodeValue.replace(fillCharReg, '').length;
  6026. tmpNode = tmpNode.previousSibling;
  6027. }
  6028. firstIndex += (isStart ? me.startOffset : me.endOffset) // - (fillCharReg.test(node.nodeValue) ? 1 : 0 )
  6029. } else {
  6030. node = node.childNodes[isStart ? me.startOffset : me.endOffset];
  6031. if (node) {
  6032. firstIndex = domUtils.getNodeIndex(node, ignoreTxt);
  6033. } else {
  6034. node = isStart ? me.startContainer : me.endContainer;
  6035. var first = node.firstChild;
  6036. while (first) {
  6037. if (domUtils.isFillChar(first)) {
  6038. first = first.nextSibling;
  6039. continue;
  6040. }
  6041. firstIndex++;
  6042. if (first.nodeType == 3) {
  6043. while (first && first.nodeType == 3) {
  6044. first = first.nextSibling;
  6045. }
  6046. } else {
  6047. first = first.nextSibling;
  6048. }
  6049. }
  6050. }
  6051. }
  6052. } else {
  6053. firstIndex = isStart ? domUtils.isFillChar(node) ? 0 : me.startOffset : me.endOffset
  6054. }
  6055. if (firstIndex < 0) {
  6056. firstIndex = 0;
  6057. }
  6058. addrs.push(firstIndex);
  6059. return addrs;
  6060. }
  6061. addr.startAddress = getAddress(true);
  6062. if (!ignoreEnd) {
  6063. addr.endAddress = me.collapsed ? [].concat(addr.startAddress) : getAddress();
  6064. }
  6065. return addr;
  6066. },
  6067. /**
  6068. * 保存
  6069. * @method createAddress
  6070. * @private
  6071. * @return { Boolean } 返回开始和结束的位置
  6072. * @example
  6073. * ```html
  6074. * <body>
  6075. * <p>
  6076. * aaaa
  6077. * <em>
  6078. * <!-- 选区开始 -->
  6079. * bbbb
  6080. * <!-- 选区结束 -->
  6081. * </em>
  6082. * </p>
  6083. *
  6084. * <script>
  6085. * var range = editor.selection.getRange();
  6086. * range.moveToAddress({startAddress:[0,1,0,0],endAddress:[0,1,0,4]});
  6087. * range.select();
  6088. * //output: 'bbbb'
  6089. * console.log(editor.selection.getText());
  6090. * </script>
  6091. * </body>
  6092. * ```
  6093. */
  6094. moveToAddress: function (addr, ignoreEnd) {
  6095. var me = this;
  6096. function getNode(address, isStart) {
  6097. var tmpNode = me.document.body,
  6098. parentNode, offset;
  6099. for (var i = 0, ci, l = address.length; i < l; i++) {
  6100. ci = address[i];
  6101. parentNode = tmpNode;
  6102. tmpNode = tmpNode.childNodes[ci];
  6103. if (!tmpNode) {
  6104. offset = ci;
  6105. break;
  6106. }
  6107. }
  6108. if (isStart) {
  6109. if (tmpNode) {
  6110. me.setStartBefore(tmpNode)
  6111. } else {
  6112. me.setStart(parentNode, offset)
  6113. }
  6114. } else {
  6115. if (tmpNode) {
  6116. me.setEndBefore(tmpNode)
  6117. } else {
  6118. me.setEnd(parentNode, offset)
  6119. }
  6120. }
  6121. }
  6122. getNode(addr.startAddress, true);
  6123. !ignoreEnd && addr.endAddress && getNode(addr.endAddress);
  6124. return me;
  6125. },
  6126. /**
  6127. * 判断给定的Range对象是否和当前Range对象表示的是同一个选区
  6128. * @method equals
  6129. * @param { UE.dom.Range } 需要判断的Range对象
  6130. * @return { Boolean } 如果给定的Range对象与当前Range对象表示的是同一个选区, 则返回true, 否则返回false
  6131. */
  6132. equals: function (rng) {
  6133. for (var p in this) {
  6134. if (this.hasOwnProperty(p)) {
  6135. if (this[p] !== rng[p])
  6136. return false
  6137. }
  6138. }
  6139. return true;
  6140. },
  6141. /**
  6142. * 遍历range内的节点。每当遍历一个节点时, 都会执行参数项 doFn 指定的函数, 该函数的接受当前遍历的节点
  6143. * 作为其参数。
  6144. * @method traversal
  6145. * @param { Function } doFn 对每个遍历的节点要执行的方法, 该方法接受当前遍历的节点作为其参数
  6146. * @return { UE.dom.Range } 当前range对象
  6147. * @example
  6148. * ```html
  6149. *
  6150. * <body>
  6151. *
  6152. * <!-- 选区开始 -->
  6153. * <span></span>
  6154. * <a></a>
  6155. * <!-- 选区结束 -->
  6156. * </body>
  6157. *
  6158. * <script>
  6159. *
  6160. * //output: <span></span><a></a>
  6161. * console.log( range.cloneContents() );
  6162. *
  6163. * range.traversal( function ( node ) {
  6164. *
  6165. * if ( node.nodeType === 1 ) {
  6166. * node.className = "test";
  6167. * }
  6168. *
  6169. * } );
  6170. *
  6171. * //output: <span class="test"></span><a class="test"></a>
  6172. * console.log( range.cloneContents() );
  6173. *
  6174. * </script>
  6175. * ```
  6176. */
  6177. /**
  6178. * 遍历range内的节点。
  6179. * 每当遍历一个节点时, 都会执行参数项 doFn 指定的函数, 该函数的接受当前遍历的节点
  6180. * 作为其参数。
  6181. * 可以通过参数项 filterFn 来指定一个过滤器, 只有符合该过滤器过滤规则的节点才会触
  6182. * 发doFn函数的执行
  6183. * @method traversal
  6184. * @param { Function } doFn 对每个遍历的节点要执行的方法, 该方法接受当前遍历的节点作为其参数
  6185. * @param { Function } filterFn 过滤器, 该函数接受当前遍历的节点作为参数, 如果该节点满足过滤
  6186. * 规则, 请返回true, 该节点会触发doFn, 否则, 请返回false, 则该节点不
  6187. * 会触发doFn。
  6188. * @return { UE.dom.Range } 当前range对象
  6189. * @see UE.dom.Range:traversal(Function)
  6190. * @example
  6191. * ```html
  6192. *
  6193. * <body>
  6194. *
  6195. * <!-- 选区开始 -->
  6196. * <span></span>
  6197. * <a></a>
  6198. * <!-- 选区结束 -->
  6199. * </body>
  6200. *
  6201. * <script>
  6202. *
  6203. * //output: <span></span><a></a>
  6204. * console.log( range.cloneContents() );
  6205. *
  6206. * range.traversal( function ( node ) {
  6207. *
  6208. * node.className = "test";
  6209. *
  6210. * }, function ( node ) {
  6211. * return node.nodeType === 1;
  6212. * } );
  6213. *
  6214. * //output: <span class="test"></span><a class="test"></a>
  6215. * console.log( range.cloneContents() );
  6216. *
  6217. * </script>
  6218. * ```
  6219. */
  6220. traversal: function (doFn, filterFn) {
  6221. if (this.collapsed)
  6222. return this;
  6223. var bookmark = this.createBookmark(),
  6224. end = bookmark.end,
  6225. current = domUtils.getNextDomNode(bookmark.start, false, filterFn);
  6226. while (current && current !== end && (domUtils.getPosition(current, end) & domUtils.POSITION_PRECEDING)) {
  6227. var tmpNode = domUtils.getNextDomNode(current, false, filterFn);
  6228. doFn(current);
  6229. current = tmpNode;
  6230. }
  6231. return this.moveToBookmark(bookmark);
  6232. }
  6233. };
  6234. })();
  6235. // core/Selection.js
  6236. /**
  6237. * 选集
  6238. * @file
  6239. * @module UE.dom
  6240. * @class Selection
  6241. * @since 1.2.6.1
  6242. */
  6243. /**
  6244. * 选区集合
  6245. * @unfile
  6246. * @module UE.dom
  6247. * @class Selection
  6248. */
  6249. (function () {
  6250. function getBoundaryInformation(range, start) {
  6251. var getIndex = domUtils.getNodeIndex;
  6252. range = range.duplicate();
  6253. range.collapse(start);
  6254. var parent = range.parentElement();
  6255. //如果节点里没有子节点,直接退出
  6256. if (!parent.hasChildNodes()) {
  6257. return {
  6258. container: parent,
  6259. offset: 0
  6260. };
  6261. }
  6262. var siblings = parent.children,
  6263. child,
  6264. testRange = range.duplicate(),
  6265. startIndex = 0,
  6266. endIndex = siblings.length - 1,
  6267. index = -1,
  6268. distance;
  6269. while (startIndex <= endIndex) {
  6270. index = Math.floor((startIndex + endIndex) / 2);
  6271. child = siblings[index];
  6272. testRange.moveToElementText(child);
  6273. var position = testRange.compareEndPoints('StartToStart', range);
  6274. if (position > 0) {
  6275. endIndex = index - 1;
  6276. } else if (position < 0) {
  6277. startIndex = index + 1;
  6278. } else {
  6279. //trace:1043
  6280. return {
  6281. container: parent,
  6282. offset: getIndex(child)
  6283. };
  6284. }
  6285. }
  6286. if (index == -1) {
  6287. testRange.moveToElementText(parent);
  6288. testRange.setEndPoint('StartToStart', range);
  6289. distance = testRange.text.replace(/(\r\n|\r)/g, '\n').length;
  6290. siblings = parent.childNodes;
  6291. if (!distance) {
  6292. child = siblings[siblings.length - 1];
  6293. return {
  6294. container: child,
  6295. offset: child.nodeValue.length
  6296. };
  6297. }
  6298. var i = siblings.length;
  6299. while (distance > 0) {
  6300. distance -= siblings[--i].nodeValue.length;
  6301. }
  6302. return {
  6303. container: siblings[i],
  6304. offset: -distance
  6305. };
  6306. }
  6307. testRange.collapse(position > 0);
  6308. testRange.setEndPoint(position > 0 ? 'StartToStart' : 'EndToStart', range);
  6309. distance = testRange.text.replace(/(\r\n|\r)/g, '\n').length;
  6310. if (!distance) {
  6311. return dtd.$empty[child.tagName] || dtd.$nonChild[child.tagName] ? {
  6312. container: parent,
  6313. offset: getIndex(child) + (position > 0 ? 0 : 1)
  6314. } : {
  6315. container: child,
  6316. offset: position > 0 ? 0 : child.childNodes.length
  6317. }
  6318. }
  6319. while (distance > 0) {
  6320. try {
  6321. var pre = child;
  6322. child = child[position > 0 ? 'previousSibling' : 'nextSibling'];
  6323. distance -= child.nodeValue.length;
  6324. } catch (e) {
  6325. return {
  6326. container: parent,
  6327. offset: getIndex(pre)
  6328. };
  6329. }
  6330. }
  6331. return {
  6332. container: child,
  6333. offset: position > 0 ? -distance : child.nodeValue.length + distance
  6334. }
  6335. }
  6336. /**
  6337. * 将ieRange转换为Range对象
  6338. * @param {Range} ieRange ieRange对象
  6339. * @param {Range} range Range对象
  6340. * @return {Range} range 返回转换后的Range对象
  6341. */
  6342. function transformIERangeToRange(ieRange, range) {
  6343. if (ieRange.item) {
  6344. range.selectNode(ieRange.item(0));
  6345. } else {
  6346. var bi = getBoundaryInformation(ieRange, true);
  6347. range.setStart(bi.container, bi.offset);
  6348. if (ieRange.compareEndPoints('StartToEnd', ieRange) != 0) {
  6349. bi = getBoundaryInformation(ieRange, false);
  6350. range.setEnd(bi.container, bi.offset);
  6351. }
  6352. }
  6353. return range;
  6354. }
  6355. /**
  6356. * 获得ieRange
  6357. * @param {Selection} sel Selection对象
  6358. * @return {ieRange} 得到ieRange
  6359. */
  6360. function _getIERange(sel) {
  6361. var ieRange;
  6362. //ie下有可能报错
  6363. try {
  6364. ieRange = sel.getNative().createRange();
  6365. } catch (e) {
  6366. return null;
  6367. }
  6368. var el = ieRange.item ? ieRange.item(0) : ieRange.parentElement();
  6369. if ((el.ownerDocument || el) === sel.document) {
  6370. return ieRange;
  6371. }
  6372. return null;
  6373. }
  6374. var Selection = dom.Selection = function (doc) {
  6375. var me = this,
  6376. iframe;
  6377. me.document = doc;
  6378. if (browser.ie9below) {
  6379. iframe = domUtils.getWindow(doc).frameElement;
  6380. domUtils.on(iframe, 'beforedeactivate', function () {
  6381. me._bakIERange = me.getIERange();
  6382. });
  6383. domUtils.on(iframe, 'activate', function () {
  6384. try {
  6385. if (!_getIERange(me) && me._bakIERange) {
  6386. me._bakIERange.select();
  6387. }
  6388. } catch (ex) {}
  6389. me._bakIERange = null;
  6390. });
  6391. }
  6392. iframe = doc = null;
  6393. };
  6394. Selection.prototype = {
  6395. rangeInBody: function (rng, txtRange) {
  6396. var node = browser.ie9below || txtRange ? rng.item ? rng.item() : rng.parentElement() : rng.startContainer;
  6397. return node === this.document.body || domUtils.inDoc(node, this.document);
  6398. },
  6399. /**
  6400. * 获取原生seleciton对象
  6401. * @method getNative
  6402. * @return { Object } 获得selection对象
  6403. * @example
  6404. * ```javascript
  6405. * editor.selection.getNative();
  6406. * ```
  6407. */
  6408. getNative: function () {
  6409. var doc = this.document;
  6410. try {
  6411. return !doc ? null : browser.ie9below ? doc.selection : domUtils.getWindow(doc).getSelection();
  6412. } catch (e) {
  6413. return null;
  6414. }
  6415. },
  6416. /**
  6417. * 获得ieRange
  6418. * @method getIERange
  6419. * @return { Object } 返回ie原生的Range
  6420. * @example
  6421. * ```javascript
  6422. * editor.selection.getIERange();
  6423. * ```
  6424. */
  6425. getIERange: function () {
  6426. var ieRange = _getIERange(this);
  6427. if (!ieRange) {
  6428. if (this._bakIERange) {
  6429. return this._bakIERange;
  6430. }
  6431. }
  6432. return ieRange;
  6433. },
  6434. /**
  6435. * 缓存当前选区的range和选区的开始节点
  6436. * @method cache
  6437. */
  6438. cache: function () {
  6439. this.clear();
  6440. this._cachedRange = this.getRange();
  6441. this._cachedStartElement = this.getStart();
  6442. this._cachedStartElementPath = this.getStartElementPath();
  6443. },
  6444. /**
  6445. * 获取选区开始位置的父节点到body
  6446. * @method getStartElementPath
  6447. * @return { Array } 返回父节点集合
  6448. * @example
  6449. * ```javascript
  6450. * editor.selection.getStartElementPath();
  6451. * ```
  6452. */
  6453. getStartElementPath: function () {
  6454. if (this._cachedStartElementPath) {
  6455. return this._cachedStartElementPath;
  6456. }
  6457. var start = this.getStart();
  6458. if (start) {
  6459. return domUtils.findParents(start, true, null, true)
  6460. }
  6461. return [];
  6462. },
  6463. /**
  6464. * 清空缓存
  6465. * @method clear
  6466. */
  6467. clear: function () {
  6468. this._cachedStartElementPath = this._cachedRange = this._cachedStartElement = null;
  6469. },
  6470. /**
  6471. * 编辑器是否得到了选区
  6472. * @method isFocus
  6473. */
  6474. isFocus: function () {
  6475. try {
  6476. if (browser.ie9below) {
  6477. var nativeRange = _getIERange(this);
  6478. return !!(nativeRange && this.rangeInBody(nativeRange));
  6479. } else {
  6480. return !!this.getNative().rangeCount;
  6481. }
  6482. } catch (e) {
  6483. return false;
  6484. }
  6485. },
  6486. /**
  6487. * 获取选区对应的Range
  6488. * @method getRange
  6489. * @return { Object } 得到Range对象
  6490. * @example
  6491. * ```javascript
  6492. * editor.selection.getRange();
  6493. * ```
  6494. */
  6495. getRange: function () {
  6496. var me = this;
  6497. function optimze(range) {
  6498. var child = me.document.body.firstChild,
  6499. collapsed = range.collapsed;
  6500. while (child && child.firstChild) {
  6501. range.setStart(child, 0);
  6502. child = child.firstChild;
  6503. }
  6504. if (!range.startContainer) {
  6505. range.setStart(me.document.body, 0)
  6506. }
  6507. if (collapsed) {
  6508. range.collapse(true);
  6509. }
  6510. }
  6511. if (me._cachedRange != null) {
  6512. return this._cachedRange;
  6513. }
  6514. var range = new baidu.editor.dom.Range(me.document);
  6515. if (browser.ie9below) {
  6516. var nativeRange = me.getIERange();
  6517. if (nativeRange) {
  6518. //备份的_bakIERange可能已经实效了,dom树发生了变化比如从源码模式切回来,所以try一下,实效就放到body开始位置
  6519. try {
  6520. transformIERangeToRange(nativeRange, range);
  6521. } catch (e) {
  6522. optimze(range);
  6523. }
  6524. } else {
  6525. optimze(range);
  6526. }
  6527. } else {
  6528. var sel = me.getNative();
  6529. if (sel && sel.rangeCount) {
  6530. var firstRange = sel.getRangeAt(0);
  6531. var lastRange = sel.getRangeAt(sel.rangeCount - 1);
  6532. range.setStart(firstRange.startContainer, firstRange.startOffset).setEnd(lastRange.endContainer, lastRange.endOffset);
  6533. if (range.collapsed && domUtils.isBody(range.startContainer) && !range.startOffset) {
  6534. optimze(range);
  6535. }
  6536. } else {
  6537. //trace:1734 有可能已经不在dom树上了,标识的节点
  6538. if (this._bakRange && domUtils.inDoc(this._bakRange.startContainer, this.document)) {
  6539. return this._bakRange;
  6540. }
  6541. optimze(range);
  6542. }
  6543. }
  6544. return this._bakRange = range;
  6545. },
  6546. /**
  6547. * 获取开始元素,用于状态反射
  6548. * @method getStart
  6549. * @return { Element } 获得开始元素
  6550. * @example
  6551. * ```javascript
  6552. * editor.selection.getStart();
  6553. * ```
  6554. */
  6555. getStart: function () {
  6556. if (this._cachedStartElement) {
  6557. return this._cachedStartElement;
  6558. }
  6559. var range = browser.ie9below ? this.getIERange() : this.getRange(),
  6560. tmpRange,
  6561. start, tmp, parent;
  6562. if (browser.ie9below) {
  6563. if (!range) {
  6564. //todo 给第一个值可能会有问题
  6565. return this.document.body.firstChild;
  6566. }
  6567. //control元素
  6568. if (range.item) {
  6569. return range.item(0);
  6570. }
  6571. tmpRange = range.duplicate();
  6572. //修正ie下<b>x</b>[xx] 闭合后 <b>x|</b>xx
  6573. tmpRange.text.length > 0 && tmpRange.moveStart('character', 1);
  6574. tmpRange.collapse(1);
  6575. start = tmpRange.parentElement();
  6576. parent = tmp = range.parentElement();
  6577. while (tmp = tmp.parentNode) {
  6578. if (tmp == start) {
  6579. start = parent;
  6580. break;
  6581. }
  6582. }
  6583. } else {
  6584. range.shrinkBoundary();
  6585. start = range.startContainer;
  6586. if (start.nodeType == 1 && start.hasChildNodes()) {
  6587. start = start.childNodes[Math.min(start.childNodes.length - 1, range.startOffset)];
  6588. }
  6589. if (start.nodeType == 3) {
  6590. return start.parentNode;
  6591. }
  6592. }
  6593. return start;
  6594. },
  6595. /**
  6596. * 得到选区中的文本
  6597. * @method getText
  6598. * @return { String } 选区中包含的文本
  6599. * @example
  6600. * ```javascript
  6601. * editor.selection.getText();
  6602. * ```
  6603. */
  6604. getText: function () {
  6605. var nativeSel, nativeRange;
  6606. if (this.isFocus() && (nativeSel = this.getNative())) {
  6607. nativeRange = browser.ie9below ? nativeSel.createRange() : nativeSel.getRangeAt(0);
  6608. return browser.ie9below ? nativeRange.text : nativeRange.toString();
  6609. }
  6610. return '';
  6611. },
  6612. /**
  6613. * 清除选区
  6614. * @method clearRange
  6615. * @example
  6616. * ```javascript
  6617. * editor.selection.clearRange();
  6618. * ```
  6619. */
  6620. clearRange: function () {
  6621. this.getNative()[browser.ie9below ? 'empty' : 'removeAllRanges']();
  6622. }
  6623. };
  6624. })();
  6625. // core/Editor.js
  6626. /**
  6627. * 编辑器主类,包含编辑器提供的大部分公用接口
  6628. * @file
  6629. * @module UE
  6630. * @class Editor
  6631. * @since 1.2.6.1
  6632. */
  6633. /**
  6634. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  6635. * @unfile
  6636. * @module UE
  6637. */
  6638. /**
  6639. * UEditor的核心类,为用户提供与编辑器交互的接口。
  6640. * @unfile
  6641. * @module UE
  6642. * @class Editor
  6643. */
  6644. (function () {
  6645. var uid = 0,
  6646. _selectionChangeTimer;
  6647. /**
  6648. * 获取编辑器的html内容,赋值到编辑器所在表单的textarea文本域里面
  6649. * @private
  6650. * @method setValue
  6651. * @param { UE.Editor } editor 编辑器事例
  6652. */
  6653. function setValue(form, editor) {
  6654. var textarea;
  6655. if (editor.textarea) {
  6656. if (utils.isString(editor.textarea)) {
  6657. for (var i = 0, ti, tis = domUtils.getElementsByTagName(form, 'textarea'); ti = tis[i++];) {
  6658. if (ti.id == 'ueditor_textarea_' + editor.options.textarea) {
  6659. textarea = ti;
  6660. break;
  6661. }
  6662. }
  6663. } else {
  6664. textarea = editor.textarea;
  6665. }
  6666. }
  6667. if (!textarea) {
  6668. form.appendChild(textarea = domUtils.createElement(document, 'textarea', {
  6669. 'name': editor.options.textarea,
  6670. 'id': 'ueditor_textarea_' + editor.options.textarea,
  6671. 'style': "display:none"
  6672. }));
  6673. //不要产生多个textarea
  6674. editor.textarea = textarea;
  6675. }!textarea.getAttribute('name') && textarea.setAttribute('name', editor.options.textarea);
  6676. textarea.value = editor.hasContents() ?
  6677. (editor.options.allHtmlEnabled ? editor.getAllHtml() : editor.getContent(null, null, true)) :
  6678. ''
  6679. }
  6680. function loadPlugins(me) {
  6681. //初始化插件
  6682. for (var pi in UE.plugins) {
  6683. UE.plugins[pi].call(me);
  6684. }
  6685. }
  6686. function checkCurLang(I18N) {
  6687. for (var lang in I18N) {
  6688. return lang
  6689. }
  6690. }
  6691. function langReadied(me) {
  6692. me.langIsReady = true;
  6693. me.fireEvent("langReady");
  6694. }
  6695. /**
  6696. * 编辑器准备就绪后会触发该事件
  6697. * @module UE
  6698. * @class Editor
  6699. * @event ready
  6700. * @remind render方法执行完成之后,会触发该事件
  6701. * @remind
  6702. * @example
  6703. * ```javascript
  6704. * editor.addListener( 'ready', function( editor ) {
  6705. * editor.execCommand( 'focus' ); //编辑器家在完成后,让编辑器拿到焦点
  6706. * } );
  6707. * ```
  6708. */
  6709. /**
  6710. * 执行destroy方法,会触发该事件
  6711. * @module UE
  6712. * @class Editor
  6713. * @event destroy
  6714. * @see UE.Editor:destroy()
  6715. */
  6716. /**
  6717. * 执行reset方法,会触发该事件
  6718. * @module UE
  6719. * @class Editor
  6720. * @event reset
  6721. * @see UE.Editor:reset()
  6722. */
  6723. /**
  6724. * 执行focus方法,会触发该事件
  6725. * @module UE
  6726. * @class Editor
  6727. * @event focus
  6728. * @see UE.Editor:focus(Boolean)
  6729. */
  6730. /**
  6731. * 语言加载完成会触发该事件
  6732. * @module UE
  6733. * @class Editor
  6734. * @event langReady
  6735. */
  6736. /**
  6737. * 运行命令之后会触发该命令
  6738. * @module UE
  6739. * @class Editor
  6740. * @event beforeExecCommand
  6741. */
  6742. /**
  6743. * 运行命令之后会触发该命令
  6744. * @module UE
  6745. * @class Editor
  6746. * @event afterExecCommand
  6747. */
  6748. /**
  6749. * 运行命令之前会触发该命令
  6750. * @module UE
  6751. * @class Editor
  6752. * @event firstBeforeExecCommand
  6753. */
  6754. /**
  6755. * 在getContent方法执行之前会触发该事件
  6756. * @module UE
  6757. * @class Editor
  6758. * @event beforeGetContent
  6759. * @see UE.Editor:getContent()
  6760. */
  6761. /**
  6762. * 在getContent方法执行之后会触发该事件
  6763. * @module UE
  6764. * @class Editor
  6765. * @event afterGetContent
  6766. * @see UE.Editor:getContent()
  6767. */
  6768. /**
  6769. * 在getAllHtml方法执行时会触发该事件
  6770. * @module UE
  6771. * @class Editor
  6772. * @event getAllHtml
  6773. * @see UE.Editor:getAllHtml()
  6774. */
  6775. /**
  6776. * 在setContent方法执行之前会触发该事件
  6777. * @module UE
  6778. * @class Editor
  6779. * @event beforeSetContent
  6780. * @see UE.Editor:setContent(String)
  6781. */
  6782. /**
  6783. * 在setContent方法执行之后会触发该事件
  6784. * @module UE
  6785. * @class Editor
  6786. * @event afterSetContent
  6787. * @see UE.Editor:setContent(String)
  6788. */
  6789. /**
  6790. * 每当编辑器内部选区发生改变时,将触发该事件
  6791. * @event selectionchange
  6792. * @warning 该事件的触发非常频繁,不建议在该事件的处理过程中做重量级的处理
  6793. * @example
  6794. * ```javascript
  6795. * editor.addListener( 'selectionchange', function( editor ) {
  6796. * console.log('选区发生改变');
  6797. * }
  6798. */
  6799. /**
  6800. * 在所有selectionchange的监听函数执行之前,会触发该事件
  6801. * @module UE
  6802. * @class Editor
  6803. * @event beforeSelectionChange
  6804. * @see UE.Editor:selectionchange
  6805. */
  6806. /**
  6807. * 在所有selectionchange的监听函数执行完之后,会触发该事件
  6808. * @module UE
  6809. * @class Editor
  6810. * @event afterSelectionChange
  6811. * @see UE.Editor:selectionchange
  6812. */
  6813. /**
  6814. * 编辑器内容发生改变时会触发该事件
  6815. * @module UE
  6816. * @class Editor
  6817. * @event contentChange
  6818. */
  6819. /**
  6820. * 以默认参数构建一个编辑器实例
  6821. * @constructor
  6822. * @remind 通过 改构造方法实例化的编辑器,不带ui层.需要render到一个容器,编辑器实例才能正常渲染到页面
  6823. * @example
  6824. * ```javascript
  6825. * var editor = new UE.Editor();
  6826. * editor.execCommand('blod');
  6827. * ```
  6828. * @see UE.Config
  6829. */
  6830. /**
  6831. * 以给定的参数集合创建一个编辑器实例,对于未指定的参数,将应用默认参数。
  6832. * @constructor
  6833. * @remind 通过 改构造方法实例化的编辑器,不带ui层.需要render到一个容器,编辑器实例才能正常渲染到页面
  6834. * @param { Object } setting 创建编辑器的参数
  6835. * @example
  6836. * ```javascript
  6837. * var editor = new UE.Editor();
  6838. * editor.execCommand('blod');
  6839. * ```
  6840. * @see UE.Config
  6841. */
  6842. var Editor = UE.Editor = function (options) {
  6843. var me = this;
  6844. me.uid = uid++;
  6845. EventBase.call(me);
  6846. me.commands = {};
  6847. me.options = utils.extend(utils.clone(options || {}), UEDITOR_CONFIG, true);
  6848. me.shortcutkeys = {};
  6849. me.inputRules = [];
  6850. me.outputRules = [];
  6851. //设置默认的常用属性
  6852. me.setOpt(Editor.defaultOptions(me));
  6853. /* 尝试异步加载后台配置 */
  6854. me.loadServerConfig();
  6855. if (!utils.isEmptyObject(UE.I18N)) {
  6856. //修改默认的语言类型
  6857. me.options.lang = checkCurLang(UE.I18N);
  6858. UE.plugin.load(me);
  6859. langReadied(me);
  6860. } else {
  6861. utils.loadFile(document, {
  6862. src: me.options.langPath + me.options.lang + "/" + me.options.lang + ".js",
  6863. tag: "script",
  6864. type: "text/javascript",
  6865. defer: "defer"
  6866. }, function () {
  6867. UE.plugin.load(me);
  6868. langReadied(me);
  6869. });
  6870. }
  6871. UE.instants['ueditorInstant' + me.uid] = me;
  6872. };
  6873. Editor.prototype = {
  6874. registerCommand: function (name, obj) {
  6875. this.commands[name] = obj;
  6876. },
  6877. /**
  6878. * 编辑器对外提供的监听ready事件的接口, 通过调用该方法,达到的效果与监听ready事件是一致的
  6879. * @method ready
  6880. * @param { Function } fn 编辑器ready之后所执行的回调, 如果在注册事件之前编辑器已经ready,将会
  6881. * 立即触发该回调。
  6882. * @remind 需要等待编辑器加载完成后才能执行的代码,可以使用该方法传入
  6883. * @example
  6884. * ```javascript
  6885. * editor.ready( function( editor ) {
  6886. * editor.setContent('初始化完毕');
  6887. * } );
  6888. * ```
  6889. * @see UE.Editor.event:ready
  6890. */
  6891. ready: function (fn) {
  6892. var me = this;
  6893. if (fn) {
  6894. me.isReady ? fn.apply(me) : me.addListener('ready', fn);
  6895. }
  6896. },
  6897. /**
  6898. * 该方法是提供给插件里面使用,设置配置项默认值
  6899. * @method setOpt
  6900. * @warning 三处设置配置项的优先级: 实例化时传入参数 > setOpt()设置 > config文件里设置
  6901. * @warning 该方法仅供编辑器插件内部和编辑器初始化时调用,其他地方不能调用。
  6902. * @param { String } key 编辑器的可接受的选项名称
  6903. * @param { * } val 该选项可接受的值
  6904. * @example
  6905. * ```javascript
  6906. * editor.setOpt( 'initContent', '欢迎使用编辑器' );
  6907. * ```
  6908. */
  6909. /**
  6910. * 该方法是提供给插件里面使用,以{key:value}集合的方式设置插件内用到的配置项默认值
  6911. * @method setOpt
  6912. * @warning 三处设置配置项的优先级: 实例化时传入参数 > setOpt()设置 > config文件里设置
  6913. * @warning 该方法仅供编辑器插件内部和编辑器初始化时调用,其他地方不能调用。
  6914. * @param { Object } options 将要设置的选项的键值对对象
  6915. * @example
  6916. * ```javascript
  6917. * editor.setOpt( {
  6918. * 'initContent': '欢迎使用编辑器'
  6919. * } );
  6920. * ```
  6921. */
  6922. setOpt: function (key, val) {
  6923. var obj = {};
  6924. if (utils.isString(key)) {
  6925. obj[key] = val
  6926. } else {
  6927. obj = key;
  6928. }
  6929. utils.extend(this.options, obj, true);
  6930. },
  6931. getOpt: function (key) {
  6932. return this.options[key]
  6933. },
  6934. /**
  6935. * 销毁编辑器实例,使用textarea代替
  6936. * @method destroy
  6937. * @example
  6938. * ```javascript
  6939. * editor.destroy();
  6940. * ```
  6941. */
  6942. destroy: function () {
  6943. var me = this;
  6944. me.fireEvent('destroy');
  6945. var container = me.container.parentNode;
  6946. var textarea = me.textarea;
  6947. if (!textarea) {
  6948. textarea = document.createElement('textarea');
  6949. container.parentNode.insertBefore(textarea, container);
  6950. } else {
  6951. textarea.style.display = ''
  6952. }
  6953. textarea.style.width = me.iframe.offsetWidth + 'px';
  6954. textarea.style.height = me.iframe.offsetHeight + 'px';
  6955. textarea.value = me.getContent();
  6956. textarea.id = me.key;
  6957. container.innerHTML = '';
  6958. domUtils.remove(container);
  6959. var key = me.key;
  6960. //trace:2004
  6961. for (var p in me) {
  6962. if (me.hasOwnProperty(p)) {
  6963. delete this[p];
  6964. }
  6965. }
  6966. UE.delEditor(key);
  6967. },
  6968. /**
  6969. * 渲染编辑器的DOM到指定容器
  6970. * @method render
  6971. * @param { String } containerId 指定一个容器ID
  6972. * @remind 执行该方法,会触发ready事件
  6973. * @warning 必须且只能调用一次
  6974. */
  6975. /**
  6976. * 渲染编辑器的DOM到指定容器
  6977. * @method render
  6978. * @param { Element } containerDom 直接指定容器对象
  6979. * @remind 执行该方法,会触发ready事件
  6980. * @warning 必须且只能调用一次
  6981. */
  6982. render: function (container) {
  6983. var me = this,
  6984. options = me.options,
  6985. getStyleValue = function (attr) {
  6986. return parseInt(domUtils.getComputedStyle(container, attr));
  6987. };
  6988. if (utils.isString(container)) {
  6989. container = document.getElementById(container);
  6990. }
  6991. if (container) {
  6992. if (options.initialFrameWidth) {
  6993. options.minFrameWidth = options.initialFrameWidth
  6994. } else {
  6995. options.minFrameWidth = options.initialFrameWidth = container.offsetWidth;
  6996. }
  6997. if (options.initialFrameHeight) {
  6998. options.minFrameHeight = options.initialFrameHeight
  6999. } else {
  7000. options.initialFrameHeight = options.minFrameHeight = container.offsetHeight;
  7001. }
  7002. container.style.width = /%$/.test(options.initialFrameWidth) ? '100%' : options.initialFrameWidth -
  7003. getStyleValue("padding-left") - getStyleValue("padding-right") + 'px';
  7004. container.style.height = /%$/.test(options.initialFrameHeight) ? '100%' : options.initialFrameHeight -
  7005. getStyleValue("padding-top") - getStyleValue("padding-bottom") + 'px';
  7006. container.style.zIndex = options.zIndex;
  7007. var html = (ie && browser.version < 9 ? '' : '<!DOCTYPE html>') +
  7008. '<html xmlns=\'http://www.w3.org/1999/xhtml\' class=\'view\' ><head>' +
  7009. '<style type=\'text/css\'>' +
  7010. //设置四周的留边
  7011. '.view{padding:0;word-wrap:break-word;cursor:text;height:90%;}\n' +
  7012. //设置默认字体和字号
  7013. //font-family不能呢随便改,在safari下fillchar会有解析问题
  7014. 'body{margin:8px;font-family:sans-serif;font-size:16px;}' +
  7015. //设置段落间距
  7016. 'p{margin:5px 0;}</style>' +
  7017. (options.iframeCssUrl ? '<link rel=\'stylesheet\' type=\'text/css\' href=\'' + utils.unhtml(options.iframeCssUrl) + '\'/>' : '') +
  7018. (options.initialStyle ? '<style>' + options.initialStyle + '</style>' : '') +
  7019. '</head><body class=\'view\' ></body>' +
  7020. '<script type=\'text/javascript\' ' + (ie ? 'defer=\'defer\'' : '') + ' id=\'_initialScript\'>' +
  7021. 'setTimeout(function(){editor = window.parent.UE.instants[\'ueditorInstant' + me.uid + '\'];editor._setup(document);},0);' +
  7022. 'var _tmpScript = document.getElementById(\'_initialScript\');_tmpScript.parentNode.removeChild(_tmpScript);</script></html>';
  7023. container.appendChild(domUtils.createElement(document, 'iframe', {
  7024. id: 'ueditor_' + me.uid,
  7025. width: "100%",
  7026. height: "100%",
  7027. frameborder: "0",
  7028. //先注释掉了,加的原因忘记了,但开启会直接导致全屏模式下内容多时不会出现滚动条
  7029. // scrolling :'no',
  7030. src: 'javascript:void(function(){document.open();' + (options.customDomain && document.domain != location.hostname ? 'document.domain="' + document.domain + '";' : '') +
  7031. 'document.write("' + html + '");document.close();}())'
  7032. }));
  7033. container.style.overflow = 'hidden';
  7034. //解决如果是给定的百分比,会导致高度算不对的问题
  7035. setTimeout(function () {
  7036. if (/%$/.test(options.initialFrameWidth)) {
  7037. options.minFrameWidth = options.initialFrameWidth = container.offsetWidth;
  7038. //如果这里给定宽度,会导致ie在拖动窗口大小时,编辑区域不随着变化
  7039. // container.style.width = options.initialFrameWidth + 'px';
  7040. }
  7041. if (/%$/.test(options.initialFrameHeight)) {
  7042. options.minFrameHeight = options.initialFrameHeight = container.offsetHeight;
  7043. container.style.height = options.initialFrameHeight + 'px';
  7044. }
  7045. })
  7046. }
  7047. },
  7048. /**
  7049. * 编辑器初始化
  7050. * @method _setup
  7051. * @private
  7052. * @param { Element } doc 编辑器Iframe中的文档对象
  7053. */
  7054. _setup: function (doc) {
  7055. var me = this,
  7056. options = me.options;
  7057. if (ie) {
  7058. doc.body.disabled = true;
  7059. doc.body.contentEditable = true;
  7060. doc.body.disabled = false;
  7061. } else {
  7062. doc.body.contentEditable = true;
  7063. }
  7064. doc.body.spellcheck = false;
  7065. me.document = doc;
  7066. me.window = doc.defaultView || doc.parentWindow;
  7067. me.iframe = me.window.frameElement;
  7068. me.body = doc.body;
  7069. me.selection = new dom.Selection(doc);
  7070. //gecko初始化就能得到range,无法判断isFocus了
  7071. var geckoSel;
  7072. if (browser.gecko && (geckoSel = this.selection.getNative())) {
  7073. geckoSel.removeAllRanges();
  7074. }
  7075. this._initEvents();
  7076. //为form提交提供一个隐藏的textarea
  7077. for (var form = this.iframe.parentNode; !domUtils.isBody(form); form = form.parentNode) {
  7078. if (form.tagName == 'FORM') {
  7079. me.form = form;
  7080. if (me.options.autoSyncData) {
  7081. domUtils.on(me.window, 'blur', function () {
  7082. setValue(form, me);
  7083. });
  7084. } else {
  7085. domUtils.on(form, 'submit', function () {
  7086. setValue(this, me);
  7087. });
  7088. }
  7089. break;
  7090. }
  7091. }
  7092. if (options.initialContent) {
  7093. if (options.autoClearinitialContent) {
  7094. var oldExecCommand = me.execCommand;
  7095. me.execCommand = function () {
  7096. me.fireEvent('firstBeforeExecCommand');
  7097. return oldExecCommand.apply(me, arguments);
  7098. };
  7099. this._setDefaultContent(options.initialContent);
  7100. } else
  7101. this.setContent(options.initialContent, false, true);
  7102. }
  7103. //编辑器不能为空内容
  7104. if (domUtils.isEmptyNode(me.body)) {
  7105. me.body.innerHTML = '<p>' + (browser.ie ? '' : '<br/>') + '</p>';
  7106. }
  7107. //如果要求focus, 就把光标定位到内容开始
  7108. if (options.focus) {
  7109. setTimeout(function () {
  7110. me.focus(me.options.focusInEnd);
  7111. //如果自动清除开着,就不需要做selectionchange;
  7112. !me.options.autoClearinitialContent && me._selectionChange();
  7113. }, 0);
  7114. }
  7115. if (!me.container) {
  7116. me.container = this.iframe.parentNode;
  7117. }
  7118. if (options.fullscreen && me.ui) {
  7119. me.ui.setFullScreen(true);
  7120. }
  7121. try {
  7122. me.document.execCommand('2D-position', false, false);
  7123. } catch (e) {}
  7124. try {
  7125. me.document.execCommand('enableInlineTableEditing', false, false);
  7126. } catch (e) {}
  7127. try {
  7128. me.document.execCommand('enableObjectResizing', false, false);
  7129. } catch (e) {}
  7130. //挂接快捷键
  7131. me._bindshortcutKeys();
  7132. me.isReady = 1;
  7133. me.fireEvent('ready');
  7134. options.onready && options.onready.call(me);
  7135. if (!browser.ie9below) {
  7136. domUtils.on(me.window, ['blur', 'focus'], function (e) {
  7137. //chrome下会出现alt+tab切换时,导致选区位置不对
  7138. if (e.type == 'blur') {
  7139. me._bakRange = me.selection.getRange();
  7140. try {
  7141. me._bakNativeRange = me.selection.getNative().getRangeAt(0);
  7142. me.selection.getNative().removeAllRanges();
  7143. } catch (e) {
  7144. me._bakNativeRange = null;
  7145. }
  7146. } else {
  7147. try {
  7148. me._bakRange && me._bakRange.select();
  7149. } catch (e) {}
  7150. }
  7151. });
  7152. }
  7153. //trace:1518 ff3.6body不够寛,会导致点击空白处无法获得焦点
  7154. if (browser.gecko && browser.version <= 10902) {
  7155. //修复ff3.6初始化进来,不能点击获得焦点
  7156. me.body.contentEditable = false;
  7157. setTimeout(function () {
  7158. me.body.contentEditable = true;
  7159. }, 100);
  7160. setInterval(function () {
  7161. me.body.style.height = me.iframe.offsetHeight - 20 + 'px'
  7162. }, 100)
  7163. }
  7164. !options.isShow && me.setHide();
  7165. options.readonly && me.setDisabled();
  7166. },
  7167. /**
  7168. * 同步数据到编辑器所在的form
  7169. * 从编辑器的容器节点向上查找form元素,若找到,就同步编辑内容到找到的form里,为提交数据做准备,主要用于是手动提交的情况
  7170. * 后台取得数据的键值,使用你容器上的name属性,如果没有就使用参数里的textarea项
  7171. * @method sync
  7172. * @example
  7173. * ```javascript
  7174. * editor.sync();
  7175. * form.sumbit(); //form变量已经指向了form元素
  7176. * ```
  7177. */
  7178. /**
  7179. * 根据传入的formId,在页面上查找要同步数据的表单,若找到,就同步编辑内容到找到的form里,为提交数据做准备
  7180. * 后台取得数据的键值,该键值默认使用给定的编辑器容器的name属性,如果没有name属性则使用参数项里给定的“textarea”项
  7181. * @method sync
  7182. * @param { String } formID 指定一个要同步数据的form的id,编辑器的数据会同步到你指定form下
  7183. */
  7184. sync: function (formId) {
  7185. var me = this,
  7186. form = formId ? document.getElementById(formId) :
  7187. domUtils.findParent(me.iframe.parentNode, function (node) {
  7188. return node.tagName == 'FORM'
  7189. }, true);
  7190. form && setValue(form, me);
  7191. },
  7192. /**
  7193. * 设置编辑器高度
  7194. * @method setHeight
  7195. * @remind 当配置项autoHeightEnabled为真时,该方法无效
  7196. * @param { Number } number 设置的高度值,纯数值,不带单位
  7197. * @example
  7198. * ```javascript
  7199. * editor.setHeight(number);
  7200. * ```
  7201. */
  7202. setHeight: function (height, notSetHeight) {
  7203. if (height !== parseInt(this.iframe.parentNode.style.height)) {
  7204. this.iframe.parentNode.style.height = height + 'px';
  7205. }!notSetHeight && (this.options.minFrameHeight = this.options.initialFrameHeight = height);
  7206. this.body.style.height = height + 'px';
  7207. !notSetHeight && this.trigger('setHeight')
  7208. },
  7209. /**
  7210. * 为编辑器的编辑命令提供快捷键
  7211. * 这个接口是为插件扩展提供的接口,主要是为新添加的插件,如果需要添加快捷键,所提供的接口
  7212. * @method addshortcutkey
  7213. * @param { Object } keyset 命令名和快捷键键值对对象,多个按钮的快捷键用“+”分隔
  7214. * @example
  7215. * ```javascript
  7216. * editor.addshortcutkey({
  7217. * "Bold" : "ctrl+66",//^B
  7218. * "Italic" : "ctrl+73", //^I
  7219. * });
  7220. * ```
  7221. */
  7222. /**
  7223. * 这个接口是为插件扩展提供的接口,主要是为新添加的插件,如果需要添加快捷键,所提供的接口
  7224. * @method addshortcutkey
  7225. * @param { String } cmd 触发快捷键时,响应的命令
  7226. * @param { String } keys 快捷键的字符串,多个按钮用“+”分隔
  7227. * @example
  7228. * ```javascript
  7229. * editor.addshortcutkey("Underline", "ctrl+85"); //^U
  7230. * ```
  7231. */
  7232. addshortcutkey: function (cmd, keys) {
  7233. var obj = {};
  7234. if (keys) {
  7235. obj[cmd] = keys
  7236. } else {
  7237. obj = cmd;
  7238. }
  7239. utils.extend(this.shortcutkeys, obj)
  7240. },
  7241. /**
  7242. * 对编辑器设置keydown事件监听,绑定快捷键和命令,当快捷键组合触发成功,会响应对应的命令
  7243. * @method _bindshortcutKeys
  7244. * @private
  7245. */
  7246. _bindshortcutKeys: function () {
  7247. var me = this,
  7248. shortcutkeys = this.shortcutkeys;
  7249. me.addListener('keydown', function (type, e) {
  7250. var keyCode = e.keyCode || e.which;
  7251. for (var i in shortcutkeys) {
  7252. var tmp = shortcutkeys[i].split(',');
  7253. for (var t = 0, ti; ti = tmp[t++];) {
  7254. ti = ti.split(':');
  7255. var key = ti[0],
  7256. param = ti[1];
  7257. if (/^(ctrl)(\+shift)?\+(\d+)$/.test(key.toLowerCase()) || /^(\d+)$/.test(key)) {
  7258. if (((RegExp.$1 == 'ctrl' ? (e.ctrlKey || e.metaKey) : 0) &&
  7259. (RegExp.$2 != "" ? e[RegExp.$2.slice(1) + "Key"] : 1) &&
  7260. keyCode == RegExp.$3
  7261. ) ||
  7262. keyCode == RegExp.$1
  7263. ) {
  7264. if (me.queryCommandState(i, param) != -1)
  7265. me.execCommand(i, param);
  7266. domUtils.preventDefault(e);
  7267. }
  7268. }
  7269. }
  7270. }
  7271. });
  7272. },
  7273. /**
  7274. * 获取编辑器的内容
  7275. * @method getContent
  7276. * @warning 该方法获取到的是经过编辑器内置的过滤规则进行过滤后得到的内容
  7277. * @return { String } 编辑器的内容字符串, 如果编辑器的内容为空,或者是空的标签内容(如:”&lt;p&gt;&lt;br/&gt;&lt;/p&gt;“), 则返回空字符串
  7278. * @example
  7279. * ```javascript
  7280. * //编辑器html内容:<p>1<strong>2<em>34</em>5</strong>6</p>
  7281. * var content = editor.getContent(); //返回值:<p>1<strong>2<em>34</em>5</strong>6</p>
  7282. * ```
  7283. */
  7284. /**
  7285. * 获取编辑器的内容。 可以通过参数定义编辑器内置的判空规则
  7286. * @method getContent
  7287. * @param { Function } fn 自定的判空规则, 要求该方法返回一个boolean类型的值,
  7288. * 代表当前编辑器的内容是否空,
  7289. * 如果返回true, 则该方法将直接返回空字符串;如果返回false,则编辑器将返回
  7290. * 经过内置过滤规则处理后的内容。
  7291. * @remind 该方法在处理包含有初始化内容的时候能起到很好的作用。
  7292. * @warning 该方法获取到的是经过编辑器内置的过滤规则进行过滤后得到的内容
  7293. * @return { String } 编辑器的内容字符串
  7294. * @example
  7295. * ```javascript
  7296. * // editor 是一个编辑器的实例
  7297. * var content = editor.getContent( function ( editor ) {
  7298. * return editor.body.innerHTML === '欢迎使用UEditor'; //返回空字符串
  7299. * } );
  7300. * ```
  7301. */
  7302. getContent: function (cmd, fn, notSetCursor, ignoreBlank, formatter) {
  7303. var me = this;
  7304. if (cmd && utils.isFunction(cmd)) {
  7305. fn = cmd;
  7306. cmd = '';
  7307. }
  7308. if (fn ? !fn() : !this.hasContents()) {
  7309. return '';
  7310. }
  7311. me.fireEvent('beforegetcontent');
  7312. var root = UE.htmlparser(me.body.innerHTML, ignoreBlank);
  7313. me.filterOutputRule(root);
  7314. me.fireEvent('aftergetcontent', cmd, root);
  7315. return root.toHtml(formatter);
  7316. },
  7317. /**
  7318. * 取得完整的html代码,可以直接显示成完整的html文档
  7319. * @method getAllHtml
  7320. * @return { String } 编辑器的内容html文档字符串
  7321. * @eaxmple
  7322. * ```javascript
  7323. * editor.getAllHtml(); //返回格式大致是: <html><head>...</head><body>...</body></html>
  7324. * ```
  7325. */
  7326. getAllHtml: function () {
  7327. var me = this,
  7328. headHtml = [],
  7329. html = '';
  7330. me.fireEvent('getAllHtml', headHtml);
  7331. if (browser.ie && browser.version > 8) {
  7332. var headHtmlForIE9 = '';
  7333. utils.each(me.document.styleSheets, function (si) {
  7334. headHtmlForIE9 += (si.href ? '<link rel="stylesheet" type="text/css" href="' + si.href + '" />' : '<style>' + si.cssText + '</style>');
  7335. });
  7336. utils.each(me.document.getElementsByTagName('script'), function (si) {
  7337. headHtmlForIE9 += si.outerHTML;
  7338. });
  7339. }
  7340. return '<html><head>' + (me.options.charset ? '<meta http-equiv="Content-Type" content="text/html; charset=' + me.options.charset + '"/>' : '') +
  7341. (headHtmlForIE9 || me.document.getElementsByTagName('head')[0].innerHTML) + headHtml.join('\n') + '</head>' +
  7342. '<body ' + (ie && browser.version < 9 ? 'class="view"' : '') + '>' + me.getContent(null, null, true) + '</body></html>';
  7343. },
  7344. /**
  7345. * 得到编辑器的纯文本内容,但会保留段落格式
  7346. * @method getPlainTxt
  7347. * @return { String } 编辑器带段落格式的纯文本内容字符串
  7348. * @example
  7349. * ```javascript
  7350. * //编辑器html内容:<p><strong>1</strong></p><p><strong>2</strong></p>
  7351. * console.log(editor.getPlainTxt()); //输出:"1\n2\n
  7352. * ```
  7353. */
  7354. getPlainTxt: function () {
  7355. var reg = new RegExp(domUtils.fillChar, 'g'),
  7356. html = this.body.innerHTML.replace(/[\n\r]/g, ''); //ie要先去了\n在处理
  7357. html = html.replace(/<(p|div)[^>]*>(<br\/?>|&nbsp;)<\/\1>/gi, '\n')
  7358. .replace(/<br\/?>/gi, '\n')
  7359. .replace(/<[^>/]+>/g, '')
  7360. .replace(/(\n)?<\/([^>]+)>/g, function (a, b, c) {
  7361. return dtd.$block[c] ? '\n' : b ? b : '';
  7362. });
  7363. //取出来的空格会有c2a0会变成乱码,处理这种情况\u00a0
  7364. return html.replace(reg, '').replace(/\u00a0/g, ' ').replace(/&nbsp;/g, ' ');
  7365. },
  7366. /**
  7367. * 获取编辑器中的纯文本内容,没有段落格式
  7368. * @method getContentTxt
  7369. * @return { String } 编辑器不带段落格式的纯文本内容字符串
  7370. * @example
  7371. * ```javascript
  7372. * //编辑器html内容:<p><strong>1</strong></p><p><strong>2</strong></p>
  7373. * console.log(editor.getPlainTxt()); //输出:"12
  7374. * ```
  7375. */
  7376. getContentTxt: function () {
  7377. var reg = new RegExp(domUtils.fillChar, 'g');
  7378. //取出来的空格会有c2a0会变成乱码,处理这种情况\u00a0
  7379. return this.body[browser.ie ? 'innerText' : 'textContent'].replace(reg, '').replace(/\u00a0/g, ' ');
  7380. },
  7381. /**
  7382. * 设置编辑器的内容,可修改编辑器当前的html内容
  7383. * @method setContent
  7384. * @warning 通过该方法插入的内容,是经过编辑器内置的过滤规则进行过滤后得到的内容
  7385. * @warning 该方法会触发selectionchange事件
  7386. * @param { String } html 要插入的html内容
  7387. * @example
  7388. * ```javascript
  7389. * editor.getContent('<p>test</p>');
  7390. * ```
  7391. */
  7392. /**
  7393. * 设置编辑器的内容,可修改编辑器当前的html内容
  7394. * @method setContent
  7395. * @warning 通过该方法插入的内容,是经过编辑器内置的过滤规则进行过滤后得到的内容
  7396. * @warning 该方法会触发selectionchange事件
  7397. * @param { String } html 要插入的html内容
  7398. * @param { Boolean } isAppendTo 若传入true,不清空原来的内容,在最后插入内容,否则,清空内容再插入
  7399. * @example
  7400. * ```javascript
  7401. * //假设设置前的编辑器内容是 <p>old text</p>
  7402. * editor.setContent('<p>new text</p>', true); //插入的结果是<p>old text</p><p>new text</p>
  7403. * ```
  7404. */
  7405. setContent: function (html, isAppendTo, notFireSelectionchange) {
  7406. var me = this;
  7407. me.fireEvent('beforesetcontent', html);
  7408. var root = UE.htmlparser(html);
  7409. me.filterInputRule(root);
  7410. html = root.toHtml();
  7411. me.body.innerHTML = (isAppendTo ? me.body.innerHTML : '') + html;
  7412. function isCdataDiv(node) {
  7413. return node.tagName == 'DIV' && node.getAttribute('cdata_tag');
  7414. }
  7415. //给文本或者inline节点套p标签
  7416. if (me.options.enterTag == 'p') {
  7417. var child = this.body.firstChild,
  7418. tmpNode;
  7419. if (!child || child.nodeType == 1 &&
  7420. (dtd.$cdata[child.tagName] || isCdataDiv(child) ||
  7421. domUtils.isCustomeNode(child)
  7422. ) &&
  7423. child === this.body.lastChild) {
  7424. this.body.innerHTML = '<p>' + (browser.ie ? '&nbsp;' : '<br/>') + '</p>' + this.body.innerHTML;
  7425. } else {
  7426. var p = me.document.createElement('p');
  7427. while (child) {
  7428. while (child && (child.nodeType == 3 || child.nodeType == 1 && dtd.p[child.tagName] && !dtd.$cdata[child.tagName])) {
  7429. tmpNode = child.nextSibling;
  7430. p.appendChild(child);
  7431. child = tmpNode;
  7432. }
  7433. if (p.firstChild) {
  7434. if (!child) {
  7435. me.body.appendChild(p);
  7436. break;
  7437. } else {
  7438. child.parentNode.insertBefore(p, child);
  7439. p = me.document.createElement('p');
  7440. }
  7441. }
  7442. child = child.nextSibling;
  7443. }
  7444. }
  7445. }
  7446. me.fireEvent('aftersetcontent');
  7447. me.fireEvent('contentchange');
  7448. !notFireSelectionchange && me._selectionChange();
  7449. //清除保存的选区
  7450. me._bakRange = me._bakIERange = me._bakNativeRange = null;
  7451. //trace:1742 setContent后gecko能得到焦点问题
  7452. var geckoSel;
  7453. if (browser.gecko && (geckoSel = this.selection.getNative())) {
  7454. geckoSel.removeAllRanges();
  7455. }
  7456. if (me.options.autoSyncData) {
  7457. me.form && setValue(me.form, me);
  7458. }
  7459. },
  7460. /**
  7461. * 让编辑器获得焦点,默认focus到编辑器头部
  7462. * @method focus
  7463. * @example
  7464. * ```javascript
  7465. * editor.focus()
  7466. * ```
  7467. */
  7468. /**
  7469. * 让编辑器获得焦点,toEnd确定focus位置
  7470. * @method focus
  7471. * @param { Boolean } toEnd 默认focus到编辑器头部,toEnd为true时focus到内容尾部
  7472. * @example
  7473. * ```javascript
  7474. * editor.focus(true)
  7475. * ```
  7476. */
  7477. focus: function (toEnd) {
  7478. try {
  7479. var me = this,
  7480. rng = me.selection.getRange();
  7481. if (toEnd) {
  7482. var node = me.body.lastChild;
  7483. if (node && node.nodeType == 1 && !dtd.$empty[node.tagName]) {
  7484. if (domUtils.isEmptyBlock(node)) {
  7485. rng.setStartAtFirst(node)
  7486. } else {
  7487. rng.setStartAtLast(node)
  7488. }
  7489. rng.collapse(true);
  7490. }
  7491. rng.setCursor(true);
  7492. } else {
  7493. if (!rng.collapsed && domUtils.isBody(rng.startContainer) && rng.startOffset == 0) {
  7494. var node = me.body.firstChild;
  7495. if (node && node.nodeType == 1 && !dtd.$empty[node.tagName]) {
  7496. rng.setStartAtFirst(node).collapse(true);
  7497. }
  7498. }
  7499. rng.select(true);
  7500. }
  7501. this.fireEvent('focus selectionchange');
  7502. } catch (e) {}
  7503. },
  7504. isFocus: function () {
  7505. return this.selection.isFocus();
  7506. },
  7507. blur: function () {
  7508. var sel = this.selection.getNative();
  7509. if (sel.empty && browser.ie) {
  7510. var nativeRng = document.body.createTextRange();
  7511. nativeRng.moveToElementText(document.body);
  7512. nativeRng.collapse(true);
  7513. nativeRng.select();
  7514. sel.empty()
  7515. } else {
  7516. sel.removeAllRanges()
  7517. }
  7518. //this.fireEvent('blur selectionchange');
  7519. },
  7520. /**
  7521. * 初始化UE事件及部分事件代理
  7522. * @method _initEvents
  7523. * @private
  7524. */
  7525. _initEvents: function () {
  7526. var me = this,
  7527. doc = me.document,
  7528. win = me.window;
  7529. me._proxyDomEvent = utils.bind(me._proxyDomEvent, me);
  7530. domUtils.on(doc, ['click', 'contextmenu', 'mousedown', 'keydown', 'keyup', 'keypress', 'mouseup', 'mouseover', 'mouseout', 'selectstart'], me._proxyDomEvent);
  7531. domUtils.on(win, ['focus', 'blur'], me._proxyDomEvent);
  7532. domUtils.on(me.body, 'drop', function (e) {
  7533. //阻止ff下默认的弹出新页面打开图片
  7534. if (browser.gecko && e.stopPropagation) {
  7535. e.stopPropagation();
  7536. }
  7537. me.fireEvent('contentchange')
  7538. });
  7539. domUtils.on(doc, ['mouseup', 'keydown'], function (evt) {
  7540. //特殊键不触发selectionchange
  7541. if (evt.type == 'keydown' && (evt.ctrlKey || evt.metaKey || evt.shiftKey || evt.altKey)) {
  7542. return;
  7543. }
  7544. if (evt.button == 2) return;
  7545. me._selectionChange(250, evt);
  7546. });
  7547. },
  7548. /**
  7549. * 触发事件代理
  7550. * @method _proxyDomEvent
  7551. * @private
  7552. * @return { * } fireEvent的返回值
  7553. * @see UE.EventBase:fireEvent(String)
  7554. */
  7555. _proxyDomEvent: function (evt) {
  7556. if (this.fireEvent('before' + evt.type.replace(/^on/, '').toLowerCase()) === false) {
  7557. return false;
  7558. }
  7559. if (this.fireEvent(evt.type.replace(/^on/, ''), evt) === false) {
  7560. return false;
  7561. }
  7562. return this.fireEvent('after' + evt.type.replace(/^on/, '').toLowerCase())
  7563. },
  7564. /**
  7565. * 变化选区
  7566. * @method _selectionChange
  7567. * @private
  7568. */
  7569. _selectionChange: function (delay, evt) {
  7570. var me = this;
  7571. //有光标才做selectionchange 为了解决未focus时点击source不能触发更改工具栏状态的问题(source命令notNeedUndo=1)
  7572. // if ( !me.selection.isFocus() ){
  7573. // return;
  7574. // }
  7575. var hackForMouseUp = false;
  7576. var mouseX, mouseY;
  7577. if (browser.ie && browser.version < 9 && evt && evt.type == 'mouseup') {
  7578. var range = this.selection.getRange();
  7579. if (!range.collapsed) {
  7580. hackForMouseUp = true;
  7581. mouseX = evt.clientX;
  7582. mouseY = evt.clientY;
  7583. }
  7584. }
  7585. clearTimeout(_selectionChangeTimer);
  7586. _selectionChangeTimer = setTimeout(function () {
  7587. if (!me.selection || !me.selection.getNative()) {
  7588. return;
  7589. }
  7590. //修复一个IE下的bug: 鼠标点击一段已选择的文本中间时,可能在mouseup后的一段时间内取到的range是在selection的type为None下的错误值.
  7591. //IE下如果用户是拖拽一段已选择文本,则不会触发mouseup事件,所以这里的特殊处理不会对其有影响
  7592. var ieRange;
  7593. if (hackForMouseUp && me.selection.getNative().type == 'None') {
  7594. ieRange = me.document.body.createTextRange();
  7595. try {
  7596. ieRange.moveToPoint(mouseX, mouseY);
  7597. } catch (ex) {
  7598. ieRange = null;
  7599. }
  7600. }
  7601. var bakGetIERange;
  7602. if (ieRange) {
  7603. bakGetIERange = me.selection.getIERange;
  7604. me.selection.getIERange = function () {
  7605. return ieRange;
  7606. };
  7607. }
  7608. me.selection.cache();
  7609. if (bakGetIERange) {
  7610. me.selection.getIERange = bakGetIERange;
  7611. }
  7612. if (me.selection._cachedRange && me.selection._cachedStartElement) {
  7613. me.fireEvent('beforeselectionchange');
  7614. // 第二个参数causeByUi为true代表由用户交互造成的selectionchange.
  7615. me.fireEvent('selectionchange', !!evt);
  7616. me.fireEvent('afterselectionchange');
  7617. me.selection.clear();
  7618. }
  7619. }, delay || 50);
  7620. },
  7621. /**
  7622. * 执行编辑命令
  7623. * @method _callCmdFn
  7624. * @private
  7625. * @param { String } fnName 函数名称
  7626. * @param { * } args 传给命令函数的参数
  7627. * @return { * } 返回命令函数运行的返回值
  7628. */
  7629. _callCmdFn: function (fnName, args) {
  7630. var cmdName = args[0].toLowerCase(),
  7631. cmd, cmdFn;
  7632. cmd = this.commands[cmdName] || UE.commands[cmdName];
  7633. cmdFn = cmd && cmd[fnName];
  7634. //没有querycommandstate或者没有command的都默认返回0
  7635. if ((!cmd || !cmdFn) && fnName == 'queryCommandState') {
  7636. return 0;
  7637. } else if (cmdFn) {
  7638. return cmdFn.apply(this, args);
  7639. }
  7640. },
  7641. /**
  7642. * 执行编辑命令cmdName,完成富文本编辑效果
  7643. * @method execCommand
  7644. * @param { String } cmdName 需要执行的命令
  7645. * @remind 具体命令的使用请参考<a href="#COMMAND.LIST">命令列表</a>
  7646. * @return { * } 返回命令函数运行的返回值
  7647. * @example
  7648. * ```javascript
  7649. * editor.execCommand(cmdName);
  7650. * ```
  7651. */
  7652. execCommand: function (cmdName) {
  7653. cmdName = cmdName.toLowerCase();
  7654. var me = this,
  7655. result,
  7656. cmd = me.commands[cmdName] || UE.commands[cmdName];
  7657. if (!cmd || !cmd.execCommand) {
  7658. return null;
  7659. }
  7660. if (!cmd.notNeedUndo && !me.__hasEnterExecCommand) {
  7661. me.__hasEnterExecCommand = true;
  7662. if (me.queryCommandState.apply(me, arguments) != -1) {
  7663. me.fireEvent('saveScene');
  7664. me.fireEvent.apply(me, ['beforeexeccommand', cmdName].concat(arguments));
  7665. result = this._callCmdFn('execCommand', arguments);
  7666. //保存场景时,做了内容对比,再看是否进行contentchange触发,这里多触发了一次,去掉
  7667. // (!cmd.ignoreContentChange && !me._ignoreContentChange) && me.fireEvent('contentchange');
  7668. me.fireEvent.apply(me, ['afterexeccommand', cmdName].concat(arguments));
  7669. me.fireEvent('saveScene');
  7670. }
  7671. me.__hasEnterExecCommand = false;
  7672. } else {
  7673. result = this._callCmdFn('execCommand', arguments);
  7674. (!me.__hasEnterExecCommand && !cmd.ignoreContentChange && !me._ignoreContentChange) && me.fireEvent('contentchange')
  7675. }
  7676. (!me.__hasEnterExecCommand && !cmd.ignoreContentChange && !me._ignoreContentChange) && me._selectionChange();
  7677. return result;
  7678. },
  7679. /**
  7680. * 根据传入的command命令,查选编辑器当前的选区,返回命令的状态
  7681. * @method queryCommandState
  7682. * @param { String } cmdName 需要查询的命令名称
  7683. * @remind 具体命令的使用请参考<a href="#COMMAND.LIST">命令列表</a>
  7684. * @return { Number } number 返回放前命令的状态,返回值三种情况:(-1|0|1)
  7685. * @example
  7686. * ```javascript
  7687. * editor.queryCommandState(cmdName) => (-1|0|1)
  7688. * ```
  7689. * @see COMMAND.LIST
  7690. */
  7691. queryCommandState: function (cmdName) {
  7692. return this._callCmdFn('queryCommandState', arguments);
  7693. },
  7694. /**
  7695. * 根据传入的command命令,查选编辑器当前的选区,根据命令返回相关的值
  7696. * @method queryCommandValue
  7697. * @param { String } cmdName 需要查询的命令名称
  7698. * @remind 具体命令的使用请参考<a href="#COMMAND.LIST">命令列表</a>
  7699. * @remind 只有部分插件有此方法
  7700. * @return { * } 返回每个命令特定的当前状态值
  7701. * @grammar editor.queryCommandValue(cmdName) => {*}
  7702. * @see COMMAND.LIST
  7703. */
  7704. queryCommandValue: function (cmdName) {
  7705. return this._callCmdFn('queryCommandValue', arguments);
  7706. },
  7707. /**
  7708. * 检查编辑区域中是否有内容
  7709. * @method hasContents
  7710. * @remind 默认有文本内容,或者有以下节点都不认为是空
  7711. * table,ul,ol,dl,iframe,area,base,col,hr,img,embed,input,link,meta,param
  7712. * @return { Boolean } 检查有内容返回true,否则返回false
  7713. * @example
  7714. * ```javascript
  7715. * editor.hasContents()
  7716. * ```
  7717. */
  7718. /**
  7719. * 检查编辑区域中是否有内容,若包含参数tags中的节点类型,直接返回true
  7720. * @method hasContents
  7721. * @param { Array } tags 传入数组判断时用到的节点类型
  7722. * @return { Boolean } 若文档中包含tags数组里对应的tag,返回true,否则返回false
  7723. * @example
  7724. * ```javascript
  7725. * editor.hasContents(['span']);
  7726. * ```
  7727. */
  7728. hasContents: function (tags) {
  7729. if (tags) {
  7730. for (var i = 0, ci; ci = tags[i++];) {
  7731. if (this.document.getElementsByTagName(ci).length > 0) {
  7732. return true;
  7733. }
  7734. }
  7735. }
  7736. if (!domUtils.isEmptyBlock(this.body)) {
  7737. return true
  7738. }
  7739. //随时添加,定义的特殊标签如果存在,不能认为是空
  7740. tags = ['div'];
  7741. for (i = 0; ci = tags[i++];) {
  7742. var nodes = domUtils.getElementsByTagName(this.document, ci);
  7743. for (var n = 0, cn; cn = nodes[n++];) {
  7744. if (domUtils.isCustomeNode(cn)) {
  7745. return true;
  7746. }
  7747. }
  7748. }
  7749. return false;
  7750. },
  7751. /**
  7752. * 重置编辑器,可用来做多个tab使用同一个编辑器实例
  7753. * @method reset
  7754. * @remind 此方法会清空编辑器内容,清空回退列表,会触发reset事件
  7755. * @example
  7756. * ```javascript
  7757. * editor.reset()
  7758. * ```
  7759. */
  7760. reset: function () {
  7761. this.fireEvent('reset');
  7762. },
  7763. /**
  7764. * 设置当前编辑区域可以编辑
  7765. * @method setEnabled
  7766. * @example
  7767. * ```javascript
  7768. * editor.setEnabled()
  7769. * ```
  7770. */
  7771. setEnabled: function () {
  7772. var me = this,
  7773. range;
  7774. if (me.body.contentEditable == 'false') {
  7775. me.body.contentEditable = true;
  7776. range = me.selection.getRange();
  7777. //有可能内容丢失了
  7778. try {
  7779. range.moveToBookmark(me.lastBk);
  7780. delete me.lastBk
  7781. } catch (e) {
  7782. range.setStartAtFirst(me.body).collapse(true)
  7783. }
  7784. range.select(true);
  7785. if (me.bkqueryCommandState) {
  7786. me.queryCommandState = me.bkqueryCommandState;
  7787. delete me.bkqueryCommandState;
  7788. }
  7789. if (me.bkqueryCommandValue) {
  7790. me.queryCommandValue = me.bkqueryCommandValue;
  7791. delete me.bkqueryCommandValue;
  7792. }
  7793. me.fireEvent('selectionchange');
  7794. }
  7795. },
  7796. enable: function () {
  7797. return this.setEnabled();
  7798. },
  7799. /** 设置当前编辑区域不可编辑
  7800. * @method setDisabled
  7801. */
  7802. /** 设置当前编辑区域不可编辑,except中的命令除外
  7803. * @method setDisabled
  7804. * @param { String } except 例外命令的字符串
  7805. * @remind 即使设置了disable,此处配置的例外命令仍然可以执行
  7806. * @example
  7807. * ```javascript
  7808. * editor.setDisabled('bold'); //禁用工具栏中除加粗之外的所有功能
  7809. * ```
  7810. */
  7811. /** 设置当前编辑区域不可编辑,except中的命令除外
  7812. * @method setDisabled
  7813. * @param { Array } except 例外命令的字符串数组,数组中的命令仍然可以执行
  7814. * @remind 即使设置了disable,此处配置的例外命令仍然可以执行
  7815. * @example
  7816. * ```javascript
  7817. * editor.setDisabled(['bold','insertimage']); //禁用工具栏中除加粗和插入图片之外的所有功能
  7818. * ```
  7819. */
  7820. setDisabled: function (except) {
  7821. var me = this;
  7822. except = except ? utils.isArray(except) ? except : [except] : [];
  7823. if (me.body.contentEditable == 'true') {
  7824. if (!me.lastBk) {
  7825. me.lastBk = me.selection.getRange().createBookmark(true);
  7826. }
  7827. me.body.contentEditable = false;
  7828. me.bkqueryCommandState = me.queryCommandState;
  7829. me.bkqueryCommandValue = me.queryCommandValue;
  7830. me.queryCommandState = function (type) {
  7831. if (utils.indexOf(except, type) != -1) {
  7832. return me.bkqueryCommandState.apply(me, arguments);
  7833. }
  7834. return -1;
  7835. };
  7836. me.queryCommandValue = function (type) {
  7837. if (utils.indexOf(except, type) != -1) {
  7838. return me.bkqueryCommandValue.apply(me, arguments);
  7839. }
  7840. return null;
  7841. };
  7842. me.fireEvent('selectionchange');
  7843. }
  7844. },
  7845. disable: function (except) {
  7846. return this.setDisabled(except);
  7847. },
  7848. /**
  7849. * 设置默认内容
  7850. * @method _setDefaultContent
  7851. * @private
  7852. * @param { String } cont 要存入的内容
  7853. */
  7854. _setDefaultContent: function () {
  7855. function clear() {
  7856. var me = this;
  7857. if (me.document.getElementById('initContent')) {
  7858. me.body.innerHTML = '<p>' + (ie ? '' : '<br/>') + '</p>';
  7859. me.removeListener('firstBeforeExecCommand focus', clear);
  7860. setTimeout(function () {
  7861. me.focus();
  7862. me._selectionChange();
  7863. }, 0)
  7864. }
  7865. }
  7866. return function (cont) {
  7867. var me = this;
  7868. me.body.innerHTML = '<p id="initContent">' + cont + '</p>';
  7869. me.addListener('firstBeforeExecCommand focus', clear);
  7870. }
  7871. }(),
  7872. /**
  7873. * 显示编辑器
  7874. * @method setShow
  7875. * @example
  7876. * ```javascript
  7877. * editor.setShow()
  7878. * ```
  7879. */
  7880. setShow: function () {
  7881. var me = this,
  7882. range = me.selection.getRange();
  7883. if (me.container.style.display == 'none') {
  7884. //有可能内容丢失了
  7885. try {
  7886. range.moveToBookmark(me.lastBk);
  7887. delete me.lastBk
  7888. } catch (e) {
  7889. range.setStartAtFirst(me.body).collapse(true)
  7890. }
  7891. //ie下focus实效,所以做了个延迟
  7892. setTimeout(function () {
  7893. range.select(true);
  7894. }, 100);
  7895. me.container.style.display = '';
  7896. }
  7897. },
  7898. show: function () {
  7899. return this.setShow();
  7900. },
  7901. /**
  7902. * 隐藏编辑器
  7903. * @method setHide
  7904. * @example
  7905. * ```javascript
  7906. * editor.setHide()
  7907. * ```
  7908. */
  7909. setHide: function () {
  7910. var me = this;
  7911. if (!me.lastBk) {
  7912. me.lastBk = me.selection.getRange().createBookmark(true);
  7913. }
  7914. me.container.style.display = 'none'
  7915. },
  7916. hide: function () {
  7917. return this.setHide();
  7918. },
  7919. /**
  7920. * 根据指定的路径,获取对应的语言资源
  7921. * @method getLang
  7922. * @param { String } path 路径根据的是lang目录下的语言文件的路径结构
  7923. * @return { Object | String } 根据路径返回语言资源的Json格式对象或者语言字符串
  7924. * @example
  7925. * ```javascript
  7926. * editor.getLang('contextMenu.delete'); //如果当前是中文,那返回是的是'删除'
  7927. * ```
  7928. */
  7929. getLang: function (path) {
  7930. // HaoChuan9421
  7931. if (!this.options) {
  7932. return '';
  7933. }
  7934. var lang = UE.I18N[this.options.lang] || 'zh-cn';
  7935. if (!lang) {
  7936. throw Error("not import language file");
  7937. }
  7938. path = (path || "").split(".");
  7939. for (var i = 0, ci; ci = path[i++];) {
  7940. lang = lang[ci];
  7941. if (!lang) break;
  7942. }
  7943. return lang;
  7944. },
  7945. /**
  7946. * 计算编辑器html内容字符串的长度
  7947. * @method getContentLength
  7948. * @return { Number } 返回计算的长度
  7949. * @example
  7950. * ```javascript
  7951. * //编辑器html内容<p><strong>132</strong></p>
  7952. * editor.getContentLength() //返回27
  7953. * ```
  7954. */
  7955. /**
  7956. * 计算编辑器当前纯文本内容的长度
  7957. * @method getContentLength
  7958. * @param { Boolean } ingoneHtml 传入true时,只按照纯文本来计算
  7959. * @return { Number } 返回计算的长度,内容中有hr/img/iframe标签,长度加1
  7960. * @example
  7961. * ```javascript
  7962. * //编辑器html内容<p><strong>132</strong></p>
  7963. * editor.getContentLength() //返回3
  7964. * ```
  7965. */
  7966. getContentLength: function (ingoneHtml, tagNames) {
  7967. var count = this.getContent(false, false, true).length;
  7968. if (ingoneHtml) {
  7969. tagNames = (tagNames || []).concat(['hr', 'img', 'iframe']);
  7970. count = this.getContentTxt().replace(/[\t\r\n]+/g, '').length;
  7971. for (var i = 0, ci; ci = tagNames[i++];) {
  7972. count += this.document.getElementsByTagName(ci).length;
  7973. }
  7974. }
  7975. return count;
  7976. },
  7977. /**
  7978. * 注册输入过滤规则
  7979. * @method addInputRule
  7980. * @param { Function } rule 要添加的过滤规则
  7981. * @example
  7982. * ```javascript
  7983. * editor.addInputRule(function(root){
  7984. * $.each(root.getNodesByTagName('div'),function(i,node){
  7985. * node.tagName="p";
  7986. * });
  7987. * });
  7988. * ```
  7989. */
  7990. addInputRule: function (rule) {
  7991. this.inputRules.push(rule);
  7992. },
  7993. /**
  7994. * 执行注册的过滤规则
  7995. * @method filterInputRule
  7996. * @param { UE.uNode } root 要过滤的uNode节点
  7997. * @remind 执行editor.setContent方法和执行'inserthtml'命令后,会运行该过滤函数
  7998. * @example
  7999. * ```javascript
  8000. * editor.filterInputRule(editor.body);
  8001. * ```
  8002. * @see UE.Editor:addInputRule
  8003. */
  8004. filterInputRule: function (root) {
  8005. for (var i = 0, ci; ci = this.inputRules[i++];) {
  8006. ci.call(this, root)
  8007. }
  8008. },
  8009. /**
  8010. * 注册输出过滤规则
  8011. * @method addOutputRule
  8012. * @param { Function } rule 要添加的过滤规则
  8013. * @example
  8014. * ```javascript
  8015. * editor.addOutputRule(function(root){
  8016. * $.each(root.getNodesByTagName('p'),function(i,node){
  8017. * node.tagName="div";
  8018. * });
  8019. * });
  8020. * ```
  8021. */
  8022. addOutputRule: function (rule) {
  8023. this.outputRules.push(rule)
  8024. },
  8025. /**
  8026. * 根据输出过滤规则,过滤编辑器内容
  8027. * @method filterOutputRule
  8028. * @remind 执行editor.getContent方法的时候,会先运行该过滤函数
  8029. * @param { UE.uNode } root 要过滤的uNode节点
  8030. * @example
  8031. * ```javascript
  8032. * editor.filterOutputRule(editor.body);
  8033. * ```
  8034. * @see UE.Editor:addOutputRule
  8035. */
  8036. filterOutputRule: function (root) {
  8037. for (var i = 0, ci; ci = this.outputRules[i++];) {
  8038. ci.call(this, root)
  8039. }
  8040. },
  8041. /**
  8042. * 根据action名称获取请求的路径
  8043. * @method getActionUrl
  8044. * @remind 假如没有设置serverUrl,会根据imageUrl设置默认的controller路径
  8045. * @param { String } action action名称
  8046. * @example
  8047. * ```javascript
  8048. * editor.getActionUrl('config'); //返回 "/ueditor/php/controller.php?action=config"
  8049. * editor.getActionUrl('image'); //返回 "/ueditor/php/controller.php?action=uplaodimage"
  8050. * editor.getActionUrl('scrawl'); //返回 "/ueditor/php/controller.php?action=uplaodscrawl"
  8051. * editor.getActionUrl('imageManager'); //返回 "/ueditor/php/controller.php?action=listimage"
  8052. * ```
  8053. */
  8054. getActionUrl: function (action) {
  8055. var actionName = this.getOpt(action) || action,
  8056. imageUrl = this.getOpt('imageUrl'),
  8057. serverUrl = this.getOpt('serverUrl');
  8058. console.log(serverUrl);
  8059. if (!serverUrl && imageUrl) {
  8060. serverUrl = imageUrl.replace(/^(.*[\/]).+([\.].+)$/, '$1controller$2');
  8061. }
  8062. if (serverUrl) {
  8063. serverUrl = serverUrl + (serverUrl.indexOf('?') == -1 ? '?' : '&') + 'action=' + (actionName || '');
  8064. return utils.formatUrl(serverUrl);
  8065. } else {
  8066. return '';
  8067. }
  8068. }
  8069. };
  8070. utils.inherits(Editor, EventBase);
  8071. })();
  8072. // core/Editor.defaultoptions.js
  8073. //维护编辑器一下默认的不在插件中的配置项
  8074. UE.Editor.defaultOptions = function (editor) {
  8075. var _url = editor.options.UEDITOR_HOME_URL;
  8076. return {
  8077. isShow: true,
  8078. initialContent: '',
  8079. initialStyle: '',
  8080. autoClearinitialContent: false,
  8081. iframeCssUrl: _url + 'themes/iframe.css',
  8082. textarea: 'editorValue',
  8083. focus: false,
  8084. focusInEnd: true,
  8085. autoClearEmptyNode: true,
  8086. fullscreen: false,
  8087. readonly: false,
  8088. zIndex: 999,
  8089. imagePopup: true,
  8090. enterTag: 'p',
  8091. customDomain: false,
  8092. lang: 'zh-cn',
  8093. langPath: _url + 'lang/',
  8094. theme: 'default',
  8095. themePath: _url + 'themes/',
  8096. allHtmlEnabled: false,
  8097. scaleEnabled: false,
  8098. tableNativeEditInFF: false,
  8099. autoSyncData: true,
  8100. fileNameFormat: '{time}{rand:6}'
  8101. }
  8102. };
  8103. // core/loadconfig.js
  8104. (function () {
  8105. UE.Editor.prototype.loadServerConfig = function () {
  8106. var me = this;
  8107. setTimeout(function () {
  8108. try {
  8109. me.options.imageUrl && me.setOpt('serverUrl', me.options.imageUrl.replace(/^(.*[\/]).+([\.].+)$/, '$1controller$2'));
  8110. var configUrl = me.getActionUrl('config'),
  8111. isJsonp = ''; // utils.isCrossDomainUrl(configUrl);
  8112. /* 发出ajax请求 */
  8113. me._serverConfigLoaded = false;
  8114. configUrl && UE.ajax.request(configUrl, {
  8115. 'method': 'GET',
  8116. 'dataType': isJsonp ? 'jsonp' : '',
  8117. 'onsuccess': function (r) {
  8118. try {
  8119. var config = isJsonp ? r : eval("(" + r.responseText + ")");
  8120. console.warn(config);
  8121. utils.extend(me.options, config);
  8122. me.fireEvent('serverConfigLoaded');
  8123. me._serverConfigLoaded = true;
  8124. } catch (e) {
  8125. showErrorMsg(me.getLang('loadconfigFormatError'));
  8126. }
  8127. },
  8128. 'onerror': function () {
  8129. showErrorMsg(me.getLang('loadconfigHttpError'));
  8130. }
  8131. });
  8132. } catch (e) {
  8133. showErrorMsg(me.getLang('loadconfigError'));
  8134. }
  8135. });
  8136. function showErrorMsg(msg) {
  8137. console && console.error(msg);
  8138. //me.fireEvent('showMessage', {
  8139. // 'title': msg,
  8140. // 'type': 'error'
  8141. //});
  8142. }
  8143. };
  8144. UE.Editor.prototype.isServerConfigLoaded = function () {
  8145. var me = this;
  8146. return me._serverConfigLoaded || false;
  8147. };
  8148. UE.Editor.prototype.afterConfigReady = function (handler) {
  8149. if (!handler || !utils.isFunction(handler)) return;
  8150. var me = this;
  8151. var readyHandler = function () {
  8152. handler.apply(me, arguments);
  8153. me.removeListener('serverConfigLoaded', readyHandler);
  8154. };
  8155. if (me.isServerConfigLoaded()) {
  8156. handler.call(me, 'serverConfigLoaded');
  8157. } else {
  8158. me.addListener('serverConfigLoaded', readyHandler);
  8159. }
  8160. };
  8161. })();
  8162. // core/ajax.js
  8163. /**
  8164. * @file
  8165. * @module UE.ajax
  8166. * @since 1.2.6.1
  8167. */
  8168. /**
  8169. * 提供对ajax请求的支持
  8170. * @module UE.ajax
  8171. */
  8172. UE.ajax = function () {
  8173. //创建一个ajaxRequest对象
  8174. var fnStr = 'XMLHttpRequest()';
  8175. try {
  8176. new ActiveXObject("Msxml2.XMLHTTP");
  8177. fnStr = 'ActiveXObject(\'Msxml2.XMLHTTP\')';
  8178. } catch (e) {
  8179. try {
  8180. new ActiveXObject("Microsoft.XMLHTTP");
  8181. fnStr = 'ActiveXObject(\'Microsoft.XMLHTTP\')'
  8182. } catch (e) {}
  8183. }
  8184. var creatAjaxRequest = new Function('return new ' + fnStr);
  8185. /**
  8186. * 将json参数转化成适合ajax提交的参数列表
  8187. * @param json
  8188. */
  8189. function json2str(json) {
  8190. var strArr = [];
  8191. for (var i in json) {
  8192. //忽略默认的几个参数
  8193. if (i == "method" || i == "timeout" || i == "async" || i == "dataType" || i == "callback") continue;
  8194. //忽略控制
  8195. if (json[i] == undefined || json[i] == null) continue;
  8196. //传递过来的对象和函数不在提交之列
  8197. if (!((typeof json[i]).toLowerCase() == "function" || (typeof json[i]).toLowerCase() == "object")) {
  8198. strArr.push(encodeURIComponent(i) + "=" + encodeURIComponent(json[i]));
  8199. } else if (utils.isArray(json[i])) {
  8200. //支持传数组内容
  8201. for (var j = 0; j < json[i].length; j++) {
  8202. strArr.push(encodeURIComponent(i) + "[]=" + encodeURIComponent(json[i][j]));
  8203. }
  8204. }
  8205. }
  8206. return strArr.join("&");
  8207. }
  8208. function doAjax(url, ajaxOptions) {
  8209. var xhr = creatAjaxRequest(),
  8210. //是否超时
  8211. timeIsOut = false,
  8212. //默认参数
  8213. defaultAjaxOptions = {
  8214. method: "POST",
  8215. timeout: 5000,
  8216. async: true,
  8217. data: {}, //需要传递对象的话只能覆盖
  8218. onsuccess: function () {},
  8219. onerror: function () {}
  8220. };
  8221. if (typeof url === "object") {
  8222. ajaxOptions = url;
  8223. url = ajaxOptions.url;
  8224. }
  8225. if (!xhr || !url) return;
  8226. var ajaxOpts = ajaxOptions ? utils.extend(defaultAjaxOptions, ajaxOptions) : defaultAjaxOptions;
  8227. var submitStr = json2str(ajaxOpts); // { name:"Jim",city:"Beijing" } --> "name=Jim&city=Beijing"
  8228. //如果用户直接通过data参数传递json对象过来,则也要将此json对象转化为字符串
  8229. if (!utils.isEmptyObject(ajaxOpts.data)) {
  8230. submitStr += (submitStr ? "&" : "") + json2str(ajaxOpts.data);
  8231. }
  8232. //超时检测
  8233. var timerID = setTimeout(function () {
  8234. if (xhr.readyState != 4) {
  8235. timeIsOut = true;
  8236. xhr.abort();
  8237. clearTimeout(timerID);
  8238. }
  8239. }, ajaxOpts.timeout);
  8240. var method = ajaxOpts.method.toUpperCase();
  8241. var str = url + (url.indexOf("?") == -1 ? "?" : "&") + (method == "POST" ? "" : submitStr + "&noCache=" + +new Date);
  8242. xhr.open(method, str, ajaxOpts.async);
  8243. xhr.onreadystatechange = function () {
  8244. if (xhr.readyState == 4) {
  8245. if (!timeIsOut && xhr.status == 200) {
  8246. ajaxOpts.onsuccess(xhr);
  8247. } else {
  8248. ajaxOpts.onerror(xhr);
  8249. }
  8250. }
  8251. };
  8252. if (method == "POST") {
  8253. xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  8254. xhr.send(submitStr);
  8255. } else {
  8256. xhr.send(null);
  8257. }
  8258. }
  8259. function doJsonp(url, opts) {
  8260. var successhandler = opts.onsuccess || function () {},
  8261. scr = document.createElement('SCRIPT'),
  8262. options = opts || {},
  8263. charset = options['charset'],
  8264. callbackField = options['jsonp'] || 'callback',
  8265. callbackFnName,
  8266. timeOut = options['timeOut'] || 0,
  8267. timer,
  8268. reg = new RegExp('(\\?|&)' + callbackField + '=([^&]*)'),
  8269. matches;
  8270. if (utils.isFunction(successhandler)) {
  8271. callbackFnName = 'bd__editor__' + Math.floor(Math.random() * 2147483648).toString(36);
  8272. window[callbackFnName] = getCallBack(0);
  8273. } else if (utils.isString(successhandler)) {
  8274. callbackFnName = successhandler;
  8275. } else {
  8276. if (matches = reg.exec(url)) {
  8277. callbackFnName = matches[2];
  8278. }
  8279. }
  8280. url = url.replace(reg, '\x241' + callbackField + '=' + callbackFnName);
  8281. if (url.search(reg) < 0) {
  8282. url += (url.indexOf('?') < 0 ? '?' : '&') + callbackField + '=' + callbackFnName;
  8283. }
  8284. var queryStr = json2str(opts); // { name:"Jim",city:"Beijing" } --> "name=Jim&city=Beijing"
  8285. //如果用户直接通过data参数传递json对象过来,则也要将此json对象转化为字符串
  8286. if (!utils.isEmptyObject(opts.data)) {
  8287. queryStr += (queryStr ? "&" : "") + json2str(opts.data);
  8288. }
  8289. if (queryStr) {
  8290. url = url.replace(/\?/, '?' + queryStr + '&');
  8291. }
  8292. scr.onerror = getCallBack(1);
  8293. if (timeOut) {
  8294. timer = setTimeout(getCallBack(1), timeOut);
  8295. }
  8296. createScriptTag(scr, url, charset);
  8297. function createScriptTag(scr, url, charset) {
  8298. scr.setAttribute('type', 'text/javascript');
  8299. scr.setAttribute('defer', 'defer');
  8300. charset && scr.setAttribute('charset', charset);
  8301. scr.setAttribute('src', url);
  8302. document.getElementsByTagName('head')[0].appendChild(scr);
  8303. }
  8304. function getCallBack(onTimeOut) {
  8305. return function () {
  8306. try {
  8307. if (onTimeOut) {
  8308. options.onerror && options.onerror();
  8309. } else {
  8310. try {
  8311. clearTimeout(timer);
  8312. successhandler.apply(window, arguments);
  8313. } catch (e) {}
  8314. }
  8315. } catch (exception) {
  8316. options.onerror && options.onerror.call(window, exception);
  8317. } finally {
  8318. options.oncomplete && options.oncomplete.apply(window, arguments);
  8319. scr.parentNode && scr.parentNode.removeChild(scr);
  8320. window[callbackFnName] = null;
  8321. try {
  8322. delete window[callbackFnName];
  8323. } catch (e) {}
  8324. }
  8325. }
  8326. }
  8327. }
  8328. return {
  8329. /**
  8330. * 根据给定的参数项,向指定的url发起一个ajax请求。 ajax请求完成后,会根据请求结果调用相应回调: 如果请求
  8331. * 成功, 则调用onsuccess回调, 失败则调用 onerror 回调
  8332. * @method request
  8333. * @param { URLString } url ajax请求的url地址
  8334. * @param { Object } ajaxOptions ajax请求选项的键值对,支持的选项如下:
  8335. * @example
  8336. * ```javascript
  8337. * //向sayhello.php发起一个异步的Ajax GET请求, 请求超时时间为10s, 请求完成后执行相应的回调。
  8338. * UE.ajax.requeset( 'sayhello.php', {
  8339. *
  8340. * //请求方法。可选值: 'GET', 'POST',默认值是'POST'
  8341. * method: 'GET',
  8342. *
  8343. * //超时时间。 默认为5000, 单位是ms
  8344. * timeout: 10000,
  8345. *
  8346. * //是否是异步请求。 true为异步请求, false为同步请求
  8347. * async: true,
  8348. *
  8349. * //请求携带的数据。如果请求为GET请求, data会经过stringify后附加到请求url之后。
  8350. * data: {
  8351. * name: 'ueditor'
  8352. * },
  8353. *
  8354. * //请求成功后的回调, 该回调接受当前的XMLHttpRequest对象作为参数。
  8355. * onsuccess: function ( xhr ) {
  8356. * console.log( xhr.responseText );
  8357. * },
  8358. *
  8359. * //请求失败或者超时后的回调。
  8360. * onerror: function ( xhr ) {
  8361. * alert( 'Ajax请求失败' );
  8362. * }
  8363. *
  8364. * } );
  8365. * ```
  8366. */
  8367. /**
  8368. * 根据给定的参数项发起一个ajax请求, 参数项里必须包含一个url地址。 ajax请求完成后,会根据请求结果调用相应回调: 如果请求
  8369. * 成功, 则调用onsuccess回调, 失败则调用 onerror 回调。
  8370. * @method request
  8371. * @warning 如果在参数项里未提供一个key为“url”的地址值,则该请求将直接退出。
  8372. * @param { Object } ajaxOptions ajax请求选项的键值对,支持的选项如下:
  8373. * @example
  8374. * ```javascript
  8375. *
  8376. * //向sayhello.php发起一个异步的Ajax POST请求, 请求超时时间为5s, 请求完成后不执行任何回调。
  8377. * UE.ajax.requeset( 'sayhello.php', {
  8378. *
  8379. * //请求的地址, 该项是必须的。
  8380. * url: 'sayhello.php'
  8381. *
  8382. * } );
  8383. * ```
  8384. */
  8385. request: function (url, opts) {
  8386. if (opts && opts.dataType == 'jsonp') {
  8387. doJsonp(url, opts);
  8388. } else {
  8389. doAjax(url, opts);
  8390. }
  8391. },
  8392. getJSONP: function (url, data, fn) {
  8393. var opts = {
  8394. 'data': data,
  8395. 'oncomplete': fn
  8396. };
  8397. doJsonp(url, opts);
  8398. }
  8399. };
  8400. }();
  8401. // core/filterword.js
  8402. /**
  8403. * UE过滤word的静态方法
  8404. * @file
  8405. */
  8406. /**
  8407. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  8408. * @module UE
  8409. */
  8410. /**
  8411. * 根据传入html字符串过滤word
  8412. * @module UE
  8413. * @since 1.2.6.1
  8414. * @method filterWord
  8415. * @param { String } html html字符串
  8416. * @return { String } 已过滤后的结果字符串
  8417. * @example
  8418. * ```javascript
  8419. * UE.filterWord(html);
  8420. * ```
  8421. */
  8422. var filterWord = UE.filterWord = function () {
  8423. //是否是word过来的内容
  8424. function isWordDocument(str) {
  8425. return /(class="?Mso|style="[^"]*\bmso\-|w:WordDocument|<(v|o):|lang=)/ig.test(str);
  8426. }
  8427. //去掉小数
  8428. function transUnit(v) {
  8429. v = v.replace(/[\d.]+\w+/g, function (m) {
  8430. return utils.transUnitToPx(m);
  8431. });
  8432. return v;
  8433. }
  8434. function filterPasteWord(str) {
  8435. return str.replace(/[\t\r\n]+/g, ' ')
  8436. .replace(/<!--[\s\S]*?-->/ig, "")
  8437. //转换图片
  8438. .replace(/<v:shape [^>]*>[\s\S]*?.<\/v:shape>/gi, function (str) {
  8439. //opera能自己解析出image所这里直接返回空
  8440. if (browser.opera) {
  8441. return '';
  8442. }
  8443. try {
  8444. //有可能是bitmap占为图,无用,直接过滤掉,主要体现在粘贴excel表格中
  8445. if (/Bitmap/i.test(str)) {
  8446. return '';
  8447. }
  8448. var width = str.match(/width:([ \d.]*p[tx])/i)[1],
  8449. height = str.match(/height:([ \d.]*p[tx])/i)[1],
  8450. src = str.match(/src=\s*"([^"]*)"/i)[1];
  8451. return '<img width="' + transUnit(width) + '" height="' + transUnit(height) + '" src="' + src + '" />';
  8452. } catch (e) {
  8453. return '';
  8454. }
  8455. })
  8456. //针对wps添加的多余标签处理
  8457. .replace(/<\/?div[^>]*>/g, '')
  8458. //去掉多余的属性
  8459. .replace(/v:\w+=(["']?)[^'"]+\1/g, '')
  8460. .replace(/<(!|script[^>]*>.*?<\/script(?=[>\s])|\/?(\?xml(:\w+)?|xml|meta|link|style|\w+:\w+)(?=[\s\/>]))[^>]*>/gi, "")
  8461. .replace(/<p [^>]*class="?MsoHeading"?[^>]*>(.*?)<\/p>/gi, "<p><strong>$1</strong></p>")
  8462. //去掉多余的属性
  8463. .replace(/\s+(class|lang|align)\s*=\s*(['"]?)([\w-]+)\2/ig, function (str, name, marks, val) {
  8464. //保留list的标示
  8465. return name == 'class' && val == 'MsoListParagraph' ? str : ''
  8466. })
  8467. //清除多余的font/span不能匹配&nbsp;有可能是空格
  8468. .replace(/<(font|span)[^>]*>(\s*)<\/\1>/gi, function (a, b, c) {
  8469. return c.replace(/[\t\r\n ]+/g, ' ')
  8470. })
  8471. //处理style的问题
  8472. .replace(/(<[a-z][^>]*)\sstyle=(["'])([^\2]*?)\2/gi, function (str, tag, tmp, style) {
  8473. var n = [],
  8474. s = style.replace(/^\s+|\s+$/, '')
  8475. .replace(/&#39;/g, '\'')
  8476. .replace(/&quot;/gi, "'")
  8477. .replace(/[\d.]+(cm|pt)/g, function (str) {
  8478. return utils.transUnitToPx(str)
  8479. })
  8480. .split(/;\s*/g);
  8481. for (var i = 0, v; v = s[i]; i++) {
  8482. var name, value,
  8483. parts = v.split(":");
  8484. if (parts.length == 2) {
  8485. name = parts[0].toLowerCase();
  8486. value = parts[1].toLowerCase();
  8487. if (/^(background)\w*/.test(name) && value.replace(/(initial|\s)/g, '').length == 0 ||
  8488. /^(margin)\w*/.test(name) && /^0\w+$/.test(value)
  8489. ) {
  8490. continue;
  8491. }
  8492. switch (name) {
  8493. case "mso-padding-alt":
  8494. case "mso-padding-top-alt":
  8495. case "mso-padding-right-alt":
  8496. case "mso-padding-bottom-alt":
  8497. case "mso-padding-left-alt":
  8498. case "mso-margin-alt":
  8499. case "mso-margin-top-alt":
  8500. case "mso-margin-right-alt":
  8501. case "mso-margin-bottom-alt":
  8502. case "mso-margin-left-alt":
  8503. //ie下会出现挤到一起的情况
  8504. //case "mso-table-layout-alt":
  8505. case "mso-height":
  8506. case "mso-width":
  8507. case "mso-vertical-align-alt":
  8508. //trace:1819 ff下会解析出padding在table上
  8509. if (!/<table/.test(tag))
  8510. n[i] = name.replace(/^mso-|-alt$/g, "") + ":" + transUnit(value);
  8511. continue;
  8512. case "horiz-align":
  8513. n[i] = "text-align:" + value;
  8514. continue;
  8515. case "vert-align":
  8516. n[i] = "vertical-align:" + value;
  8517. continue;
  8518. case "font-color":
  8519. case "mso-foreground":
  8520. n[i] = "color:" + value;
  8521. continue;
  8522. case "mso-background":
  8523. case "mso-highlight":
  8524. n[i] = "background:" + value;
  8525. continue;
  8526. case "mso-default-height":
  8527. n[i] = "min-height:" + transUnit(value);
  8528. continue;
  8529. case "mso-default-width":
  8530. n[i] = "min-width:" + transUnit(value);
  8531. continue;
  8532. case "mso-padding-between-alt":
  8533. n[i] = "border-collapse:separate;border-spacing:" + transUnit(value);
  8534. continue;
  8535. case "text-line-through":
  8536. if ((value == "single") || (value == "double")) {
  8537. n[i] = "text-decoration:line-through";
  8538. }
  8539. continue;
  8540. case "mso-zero-height":
  8541. if (value == "yes") {
  8542. n[i] = "display:none";
  8543. }
  8544. continue;
  8545. // case 'background':
  8546. // break;
  8547. case 'margin':
  8548. if (!/[1-9]/.test(value)) {
  8549. continue;
  8550. }
  8551. }
  8552. if (/^(mso|column|font-emph|lang|layout|line-break|list-image|nav|panose|punct|row|ruby|sep|size|src|tab-|table-border|text-(?:decor|trans)|top-bar|version|vnd|word-break)/.test(name) ||
  8553. /text\-indent|padding|margin/.test(name) && /\-[\d.]+/.test(value)
  8554. ) {
  8555. continue;
  8556. }
  8557. n[i] = name + ":" + parts[1];
  8558. }
  8559. }
  8560. return tag + (n.length ? ' style="' + n.join(';').replace(/;{2,}/g, ';') + '"' : '');
  8561. })
  8562. }
  8563. return function (html) {
  8564. return (isWordDocument(html) ? filterPasteWord(html) : html);
  8565. };
  8566. }();
  8567. // core/node.js
  8568. /**
  8569. * 编辑器模拟的节点类
  8570. * @file
  8571. * @module UE
  8572. * @class uNode
  8573. * @since 1.2.6.1
  8574. */
  8575. /**
  8576. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  8577. * @unfile
  8578. * @module UE
  8579. */
  8580. (function () {
  8581. /**
  8582. * 编辑器模拟的节点类
  8583. * @unfile
  8584. * @module UE
  8585. * @class uNode
  8586. */
  8587. /**
  8588. * 通过一个键值对,创建一个uNode对象
  8589. * @constructor
  8590. * @param { Object } attr 传入要创建的uNode的初始属性
  8591. * @example
  8592. * ```javascript
  8593. * var node = new uNode({
  8594. * type:'element',
  8595. * tagName:'span',
  8596. * attrs:{style:'font-size:14px;'}
  8597. * }
  8598. * ```
  8599. */
  8600. var uNode = UE.uNode = function (obj) {
  8601. this.type = obj.type;
  8602. this.data = obj.data;
  8603. this.tagName = obj.tagName;
  8604. this.parentNode = obj.parentNode;
  8605. this.attrs = obj.attrs || {};
  8606. this.children = obj.children;
  8607. };
  8608. var notTransAttrs = {
  8609. 'href': 1,
  8610. 'src': 1,
  8611. '_src': 1,
  8612. '_href': 1,
  8613. 'cdata_data': 1
  8614. };
  8615. var notTransTagName = {
  8616. style: 1,
  8617. script: 1
  8618. };
  8619. var indentChar = ' ',
  8620. breakChar = '\n';
  8621. function insertLine(arr, current, begin) {
  8622. arr.push(breakChar);
  8623. return current + (begin ? 1 : -1);
  8624. }
  8625. function insertIndent(arr, current) {
  8626. //插入缩进
  8627. for (var i = 0; i < current; i++) {
  8628. arr.push(indentChar);
  8629. }
  8630. }
  8631. //创建uNode的静态方法
  8632. //支持标签和html
  8633. uNode.createElement = function (html) {
  8634. if (/[<>]/.test(html)) {
  8635. return UE.htmlparser(html).children[0]
  8636. } else {
  8637. return new uNode({
  8638. type: 'element',
  8639. children: [],
  8640. tagName: html
  8641. })
  8642. }
  8643. };
  8644. uNode.createText = function (data, noTrans) {
  8645. return new UE.uNode({
  8646. type: 'text',
  8647. 'data': noTrans ? data : utils.unhtml(data || '')
  8648. })
  8649. };
  8650. function nodeToHtml(node, arr, formatter, current) {
  8651. switch (node.type) {
  8652. case 'root':
  8653. for (var i = 0, ci; ci = node.children[i++];) {
  8654. //插入新行
  8655. if (formatter && ci.type == 'element' && !dtd.$inlineWithA[ci.tagName] && i > 1) {
  8656. insertLine(arr, current, true);
  8657. insertIndent(arr, current)
  8658. }
  8659. nodeToHtml(ci, arr, formatter, current)
  8660. }
  8661. break;
  8662. case 'text':
  8663. isText(node, arr);
  8664. break;
  8665. case 'element':
  8666. isElement(node, arr, formatter, current);
  8667. break;
  8668. case 'comment':
  8669. isComment(node, arr, formatter);
  8670. }
  8671. return arr;
  8672. }
  8673. function isText(node, arr) {
  8674. if (node.parentNode.tagName == 'pre') {
  8675. //源码模式下输入html标签,不能做转换处理,直接输出
  8676. arr.push(node.data)
  8677. } else {
  8678. arr.push(notTransTagName[node.parentNode.tagName] ? utils.html(node.data) : node.data.replace(/[ ]{2}/g, ' &nbsp;'))
  8679. }
  8680. }
  8681. function isElement(node, arr, formatter, current) {
  8682. var attrhtml = '';
  8683. if (node.attrs) {
  8684. attrhtml = [];
  8685. var attrs = node.attrs;
  8686. for (var a in attrs) {
  8687. //这里就针对
  8688. //<p>'<img src='http://nsclick.baidu.com/u.gif?&asdf=\"sdf&asdfasdfs;asdf'></p>
  8689. //这里边的\"做转换,要不用innerHTML直接被截断了,属性src
  8690. //有可能做的不够
  8691. attrhtml.push(a + (attrs[a] !== undefined ? '="' + (notTransAttrs[a] ? utils.html(attrs[a]).replace(/["]/g, function (a) {
  8692. return '&quot;'
  8693. }) : utils.unhtml(attrs[a])) + '"' : ''))
  8694. }
  8695. attrhtml = attrhtml.join(' ');
  8696. }
  8697. arr.push('<' + node.tagName +
  8698. (attrhtml ? ' ' + attrhtml : '') +
  8699. (dtd.$empty[node.tagName] ? '\/' : '') + '>'
  8700. );
  8701. //插入新行
  8702. if (formatter && !dtd.$inlineWithA[node.tagName] && node.tagName != 'pre') {
  8703. if (node.children && node.children.length) {
  8704. current = insertLine(arr, current, true);
  8705. insertIndent(arr, current)
  8706. }
  8707. }
  8708. if (node.children && node.children.length) {
  8709. for (var i = 0, ci; ci = node.children[i++];) {
  8710. if (formatter && ci.type == 'element' && !dtd.$inlineWithA[ci.tagName] && i > 1) {
  8711. insertLine(arr, current);
  8712. insertIndent(arr, current)
  8713. }
  8714. nodeToHtml(ci, arr, formatter, current)
  8715. }
  8716. }
  8717. if (!dtd.$empty[node.tagName]) {
  8718. if (formatter && !dtd.$inlineWithA[node.tagName] && node.tagName != 'pre') {
  8719. if (node.children && node.children.length) {
  8720. current = insertLine(arr, current);
  8721. insertIndent(arr, current)
  8722. }
  8723. }
  8724. arr.push('<\/' + node.tagName + '>');
  8725. }
  8726. }
  8727. function isComment(node, arr) {
  8728. arr.push('<!--' + node.data + '-->');
  8729. }
  8730. function getNodeById(root, id) {
  8731. var node;
  8732. if (root.type == 'element' && root.getAttr('id') == id) {
  8733. return root;
  8734. }
  8735. if (root.children && root.children.length) {
  8736. for (var i = 0, ci; ci = root.children[i++];) {
  8737. if (node = getNodeById(ci, id)) {
  8738. return node;
  8739. }
  8740. }
  8741. }
  8742. }
  8743. function getNodesByTagName(node, tagName, arr) {
  8744. if (node.type == 'element' && node.tagName == tagName) {
  8745. arr.push(node);
  8746. }
  8747. if (node.children && node.children.length) {
  8748. for (var i = 0, ci; ci = node.children[i++];) {
  8749. getNodesByTagName(ci, tagName, arr)
  8750. }
  8751. }
  8752. }
  8753. function nodeTraversal(root, fn) {
  8754. if (root.children && root.children.length) {
  8755. for (var i = 0, ci; ci = root.children[i];) {
  8756. nodeTraversal(ci, fn);
  8757. //ci被替换的情况,这里就不再走 fn了
  8758. if (ci.parentNode) {
  8759. if (ci.children && ci.children.length) {
  8760. fn(ci)
  8761. }
  8762. if (ci.parentNode) i++
  8763. }
  8764. }
  8765. } else {
  8766. fn(root)
  8767. }
  8768. }
  8769. uNode.prototype = {
  8770. /**
  8771. * 当前节点对象,转换成html文本
  8772. * @method toHtml
  8773. * @return { String } 返回转换后的html字符串
  8774. * @example
  8775. * ```javascript
  8776. * node.toHtml();
  8777. * ```
  8778. */
  8779. /**
  8780. * 当前节点对象,转换成html文本
  8781. * @method toHtml
  8782. * @param { Boolean } formatter 是否格式化返回值
  8783. * @return { String } 返回转换后的html字符串
  8784. * @example
  8785. * ```javascript
  8786. * node.toHtml( true );
  8787. * ```
  8788. */
  8789. toHtml: function (formatter) {
  8790. var arr = [];
  8791. nodeToHtml(this, arr, formatter, 0);
  8792. return arr.join('')
  8793. },
  8794. /**
  8795. * 获取节点的html内容
  8796. * @method innerHTML
  8797. * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
  8798. * @return { String } 返回节点的html内容
  8799. * @example
  8800. * ```javascript
  8801. * var htmlstr = node.innerHTML();
  8802. * ```
  8803. */
  8804. /**
  8805. * 设置节点的html内容
  8806. * @method innerHTML
  8807. * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
  8808. * @param { String } htmlstr 传入要设置的html内容
  8809. * @return { UE.uNode } 返回节点本身
  8810. * @example
  8811. * ```javascript
  8812. * node.innerHTML('<span>text</span>');
  8813. * ```
  8814. */
  8815. innerHTML: function (htmlstr) {
  8816. if (this.type != 'element' || dtd.$empty[this.tagName]) {
  8817. return this;
  8818. }
  8819. if (utils.isString(htmlstr)) {
  8820. if (this.children) {
  8821. for (var i = 0, ci; ci = this.children[i++];) {
  8822. ci.parentNode = null;
  8823. }
  8824. }
  8825. this.children = [];
  8826. var tmpRoot = UE.htmlparser(htmlstr);
  8827. for (var i = 0, ci; ci = tmpRoot.children[i++];) {
  8828. this.children.push(ci);
  8829. ci.parentNode = this;
  8830. }
  8831. return this;
  8832. } else {
  8833. var tmpRoot = new UE.uNode({
  8834. type: 'root',
  8835. children: this.children
  8836. });
  8837. return tmpRoot.toHtml();
  8838. }
  8839. },
  8840. /**
  8841. * 获取节点的纯文本内容
  8842. * @method innerText
  8843. * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
  8844. * @return { String } 返回节点的存文本内容
  8845. * @example
  8846. * ```javascript
  8847. * var textStr = node.innerText();
  8848. * ```
  8849. */
  8850. /**
  8851. * 设置节点的纯文本内容
  8852. * @method innerText
  8853. * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
  8854. * @param { String } textStr 传入要设置的文本内容
  8855. * @return { UE.uNode } 返回节点本身
  8856. * @example
  8857. * ```javascript
  8858. * node.innerText('<span>text</span>');
  8859. * ```
  8860. */
  8861. innerText: function (textStr, noTrans) {
  8862. if (this.type != 'element' || dtd.$empty[this.tagName]) {
  8863. return this;
  8864. }
  8865. if (textStr) {
  8866. if (this.children) {
  8867. for (var i = 0, ci; ci = this.children[i++];) {
  8868. ci.parentNode = null;
  8869. }
  8870. }
  8871. this.children = [];
  8872. this.appendChild(uNode.createText(textStr, noTrans));
  8873. return this;
  8874. } else {
  8875. return this.toHtml().replace(/<[^>]+>/g, '');
  8876. }
  8877. },
  8878. /**
  8879. * 获取当前对象的data属性
  8880. * @method getData
  8881. * @return { Object } 若节点的type值是elemenet,返回空字符串,否则返回节点的data属性
  8882. * @example
  8883. * ```javascript
  8884. * node.getData();
  8885. * ```
  8886. */
  8887. getData: function () {
  8888. if (this.type == 'element')
  8889. return '';
  8890. return this.data
  8891. },
  8892. /**
  8893. * 获取当前节点下的第一个子节点
  8894. * @method firstChild
  8895. * @return { UE.uNode } 返回第一个子节点
  8896. * @example
  8897. * ```javascript
  8898. * node.firstChild(); //返回第一个子节点
  8899. * ```
  8900. */
  8901. firstChild: function () {
  8902. // if (this.type != 'element' || dtd.$empty[this.tagName]) {
  8903. // return this;
  8904. // }
  8905. return this.children ? this.children[0] : null;
  8906. },
  8907. /**
  8908. * 获取当前节点下的最后一个子节点
  8909. * @method lastChild
  8910. * @return { UE.uNode } 返回最后一个子节点
  8911. * @example
  8912. * ```javascript
  8913. * node.lastChild(); //返回最后一个子节点
  8914. * ```
  8915. */
  8916. lastChild: function () {
  8917. // if (this.type != 'element' || dtd.$empty[this.tagName] ) {
  8918. // return this;
  8919. // }
  8920. return this.children ? this.children[this.children.length - 1] : null;
  8921. },
  8922. /**
  8923. * 获取和当前节点有相同父亲节点的前一个节点
  8924. * @method previousSibling
  8925. * @return { UE.uNode } 返回前一个节点
  8926. * @example
  8927. * ```javascript
  8928. * node.children[2].previousSibling(); //返回子节点node.children[1]
  8929. * ```
  8930. */
  8931. previousSibling: function () {
  8932. var parent = this.parentNode;
  8933. for (var i = 0, ci; ci = parent.children[i]; i++) {
  8934. if (ci === this) {
  8935. return i == 0 ? null : parent.children[i - 1];
  8936. }
  8937. }
  8938. },
  8939. /**
  8940. * 获取和当前节点有相同父亲节点的后一个节点
  8941. * @method nextSibling
  8942. * @return { UE.uNode } 返回后一个节点,找不到返回null
  8943. * @example
  8944. * ```javascript
  8945. * node.children[2].nextSibling(); //如果有,返回子节点node.children[3]
  8946. * ```
  8947. */
  8948. nextSibling: function () {
  8949. var parent = this.parentNode;
  8950. for (var i = 0, ci; ci = parent.children[i++];) {
  8951. if (ci === this) {
  8952. return parent.children[i];
  8953. }
  8954. }
  8955. },
  8956. /**
  8957. * 用新的节点替换当前节点
  8958. * @method replaceChild
  8959. * @param { UE.uNode } target 要替换成该节点参数
  8960. * @param { UE.uNode } source 要被替换掉的节点
  8961. * @return { UE.uNode } 返回替换之后的节点对象
  8962. * @example
  8963. * ```javascript
  8964. * node.replaceChild(newNode, childNode); //用newNode替换childNode,childNode是node的子节点
  8965. * ```
  8966. */
  8967. replaceChild: function (target, source) {
  8968. if (this.children) {
  8969. if (target.parentNode) {
  8970. target.parentNode.removeChild(target);
  8971. }
  8972. for (var i = 0, ci; ci = this.children[i]; i++) {
  8973. if (ci === source) {
  8974. this.children.splice(i, 1, target);
  8975. source.parentNode = null;
  8976. target.parentNode = this;
  8977. return target;
  8978. }
  8979. }
  8980. }
  8981. },
  8982. /**
  8983. * 在节点的子节点列表最后位置插入一个节点
  8984. * @method appendChild
  8985. * @param { UE.uNode } node 要插入的节点
  8986. * @return { UE.uNode } 返回刚插入的子节点
  8987. * @example
  8988. * ```javascript
  8989. * node.appendChild( newNode ); //在node内插入子节点newNode
  8990. * ```
  8991. */
  8992. appendChild: function (node) {
  8993. if (this.type == 'root' || (this.type == 'element' && !dtd.$empty[this.tagName])) {
  8994. if (!this.children) {
  8995. this.children = []
  8996. }
  8997. if (node.parentNode) {
  8998. node.parentNode.removeChild(node);
  8999. }
  9000. for (var i = 0, ci; ci = this.children[i]; i++) {
  9001. if (ci === node) {
  9002. this.children.splice(i, 1);
  9003. break;
  9004. }
  9005. }
  9006. this.children.push(node);
  9007. node.parentNode = this;
  9008. return node;
  9009. }
  9010. },
  9011. /**
  9012. * 在传入节点的前面插入一个节点
  9013. * @method insertBefore
  9014. * @param { UE.uNode } target 要插入的节点
  9015. * @param { UE.uNode } source 在该参数节点前面插入
  9016. * @return { UE.uNode } 返回刚插入的子节点
  9017. * @example
  9018. * ```javascript
  9019. * node.parentNode.insertBefore(newNode, node); //在node节点后面插入newNode
  9020. * ```
  9021. */
  9022. insertBefore: function (target, source) {
  9023. if (this.children) {
  9024. if (target.parentNode) {
  9025. target.parentNode.removeChild(target);
  9026. }
  9027. for (var i = 0, ci; ci = this.children[i]; i++) {
  9028. if (ci === source) {
  9029. this.children.splice(i, 0, target);
  9030. target.parentNode = this;
  9031. return target;
  9032. }
  9033. }
  9034. }
  9035. },
  9036. /**
  9037. * 在传入节点的后面插入一个节点
  9038. * @method insertAfter
  9039. * @param { UE.uNode } target 要插入的节点
  9040. * @param { UE.uNode } source 在该参数节点后面插入
  9041. * @return { UE.uNode } 返回刚插入的子节点
  9042. * @example
  9043. * ```javascript
  9044. * node.parentNode.insertAfter(newNode, node); //在node节点后面插入newNode
  9045. * ```
  9046. */
  9047. insertAfter: function (target, source) {
  9048. if (this.children) {
  9049. if (target.parentNode) {
  9050. target.parentNode.removeChild(target);
  9051. }
  9052. for (var i = 0, ci; ci = this.children[i]; i++) {
  9053. if (ci === source) {
  9054. this.children.splice(i + 1, 0, target);
  9055. target.parentNode = this;
  9056. return target;
  9057. }
  9058. }
  9059. }
  9060. },
  9061. /**
  9062. * 从当前节点的子节点列表中,移除节点
  9063. * @method removeChild
  9064. * @param { UE.uNode } node 要移除的节点引用
  9065. * @param { Boolean } keepChildren 是否保留移除节点的子节点,若传入true,自动把移除节点的子节点插入到移除的位置
  9066. * @return { * } 返回刚移除的子节点
  9067. * @example
  9068. * ```javascript
  9069. * node.removeChild(childNode,true); //在node的子节点列表中移除child节点,并且吧child的子节点插入到移除的位置
  9070. * ```
  9071. */
  9072. removeChild: function (node, keepChildren) {
  9073. if (this.children) {
  9074. for (var i = 0, ci; ci = this.children[i]; i++) {
  9075. if (ci === node) {
  9076. this.children.splice(i, 1);
  9077. ci.parentNode = null;
  9078. if (keepChildren && ci.children && ci.children.length) {
  9079. for (var j = 0, cj; cj = ci.children[j]; j++) {
  9080. this.children.splice(i + j, 0, cj);
  9081. cj.parentNode = this;
  9082. }
  9083. }
  9084. return ci;
  9085. }
  9086. }
  9087. }
  9088. },
  9089. /**
  9090. * 获取当前节点所代表的元素属性,即获取attrs对象下的属性值
  9091. * @method getAttr
  9092. * @param { String } attrName 要获取的属性名称
  9093. * @return { * } 返回attrs对象下的属性值
  9094. * @example
  9095. * ```javascript
  9096. * node.getAttr('title');
  9097. * ```
  9098. */
  9099. getAttr: function (attrName) {
  9100. return this.attrs && this.attrs[attrName.toLowerCase()]
  9101. },
  9102. /**
  9103. * 设置当前节点所代表的元素属性,即设置attrs对象下的属性值
  9104. * @method setAttr
  9105. * @param { String } attrName 要设置的属性名称
  9106. * @param { * } attrVal 要设置的属性值,类型视设置的属性而定
  9107. * @return { * } 返回attrs对象下的属性值
  9108. * @example
  9109. * ```javascript
  9110. * node.setAttr('title','标题');
  9111. * ```
  9112. */
  9113. setAttr: function (attrName, attrVal) {
  9114. if (!attrName) {
  9115. delete this.attrs;
  9116. return;
  9117. }
  9118. if (!this.attrs) {
  9119. this.attrs = {};
  9120. }
  9121. if (utils.isObject(attrName)) {
  9122. for (var a in attrName) {
  9123. if (!attrName[a]) {
  9124. delete this.attrs[a]
  9125. } else {
  9126. this.attrs[a.toLowerCase()] = attrName[a];
  9127. }
  9128. }
  9129. } else {
  9130. if (!attrVal) {
  9131. delete this.attrs[attrName]
  9132. } else {
  9133. this.attrs[attrName.toLowerCase()] = attrVal;
  9134. }
  9135. }
  9136. },
  9137. /**
  9138. * 获取当前节点在父节点下的位置索引
  9139. * @method getIndex
  9140. * @return { Number } 返回索引数值,如果没有父节点,返回-1
  9141. * @example
  9142. * ```javascript
  9143. * node.getIndex();
  9144. * ```
  9145. */
  9146. getIndex: function () {
  9147. var parent = this.parentNode;
  9148. for (var i = 0, ci; ci = parent.children[i]; i++) {
  9149. if (ci === this) {
  9150. return i;
  9151. }
  9152. }
  9153. return -1;
  9154. },
  9155. /**
  9156. * 在当前节点下,根据id查找节点
  9157. * @method getNodeById
  9158. * @param { String } id 要查找的id
  9159. * @return { UE.uNode } 返回找到的节点
  9160. * @example
  9161. * ```javascript
  9162. * node.getNodeById('textId');
  9163. * ```
  9164. */
  9165. getNodeById: function (id) {
  9166. var node;
  9167. if (this.children && this.children.length) {
  9168. for (var i = 0, ci; ci = this.children[i++];) {
  9169. if (node = getNodeById(ci, id)) {
  9170. return node;
  9171. }
  9172. }
  9173. }
  9174. },
  9175. /**
  9176. * 在当前节点下,根据元素名称查找节点列表
  9177. * @method getNodesByTagName
  9178. * @param { String } tagNames 要查找的元素名称
  9179. * @return { Array } 返回找到的节点列表
  9180. * @example
  9181. * ```javascript
  9182. * node.getNodesByTagName('span');
  9183. * ```
  9184. */
  9185. getNodesByTagName: function (tagNames) {
  9186. tagNames = utils.trim(tagNames).replace(/[ ]{2,}/g, ' ').split(' ');
  9187. var arr = [],
  9188. me = this;
  9189. utils.each(tagNames, function (tagName) {
  9190. if (me.children && me.children.length) {
  9191. for (var i = 0, ci; ci = me.children[i++];) {
  9192. getNodesByTagName(ci, tagName, arr)
  9193. }
  9194. }
  9195. });
  9196. return arr;
  9197. },
  9198. /**
  9199. * 根据样式名称,获取节点的样式值
  9200. * @method getStyle
  9201. * @param { String } name 要获取的样式名称
  9202. * @return { String } 返回样式值
  9203. * @example
  9204. * ```javascript
  9205. * node.getStyle('font-size');
  9206. * ```
  9207. */
  9208. getStyle: function (name) {
  9209. var cssStyle = this.getAttr('style');
  9210. if (!cssStyle) {
  9211. return ''
  9212. }
  9213. var reg = new RegExp('(^|;)\\s*' + name + ':([^;]+)', 'i');
  9214. var match = cssStyle.match(reg);
  9215. if (match && match[0]) {
  9216. return match[2]
  9217. }
  9218. return '';
  9219. },
  9220. /**
  9221. * 给节点设置样式
  9222. * @method setStyle
  9223. * @param { String } name 要设置的的样式名称
  9224. * @param { String } val 要设置的的样值
  9225. * @example
  9226. * ```javascript
  9227. * node.setStyle('font-size', '12px');
  9228. * ```
  9229. */
  9230. setStyle: function (name, val) {
  9231. function exec(name, val) {
  9232. var reg = new RegExp('(^|;)\\s*' + name + ':([^;]+;?)', 'gi');
  9233. cssStyle = cssStyle.replace(reg, '$1');
  9234. if (val) {
  9235. cssStyle = name + ':' + utils.unhtml(val) + ';' + cssStyle
  9236. }
  9237. }
  9238. var cssStyle = this.getAttr('style');
  9239. if (!cssStyle) {
  9240. cssStyle = '';
  9241. }
  9242. if (utils.isObject(name)) {
  9243. for (var a in name) {
  9244. exec(a, name[a])
  9245. }
  9246. } else {
  9247. exec(name, val)
  9248. }
  9249. this.setAttr('style', utils.trim(cssStyle))
  9250. },
  9251. /**
  9252. * 传入一个函数,递归遍历当前节点下的所有节点
  9253. * @method traversal
  9254. * @param { Function } fn 遍历到节点的时,传入节点作为参数,运行此函数
  9255. * @example
  9256. * ```javascript
  9257. * traversal(node, function(){
  9258. * console.log(node.type);
  9259. * });
  9260. * ```
  9261. */
  9262. traversal: function (fn) {
  9263. if (this.children && this.children.length) {
  9264. nodeTraversal(this, fn);
  9265. }
  9266. return this;
  9267. }
  9268. }
  9269. })();
  9270. // core/htmlparser.js
  9271. /**
  9272. * html字符串转换成uNode节点
  9273. * @file
  9274. * @module UE
  9275. * @since 1.2.6.1
  9276. */
  9277. /**
  9278. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  9279. * @unfile
  9280. * @module UE
  9281. */
  9282. /**
  9283. * html字符串转换成uNode节点的静态方法
  9284. * @method htmlparser
  9285. * @param { String } htmlstr 要转换的html代码
  9286. * @param { Boolean } ignoreBlank 若设置为true,转换的时候忽略\n\r\t等空白字符
  9287. * @return { uNode } 给定的html片段转换形成的uNode对象
  9288. * @example
  9289. * ```javascript
  9290. * var root = UE.htmlparser('<p><b>htmlparser</b></p>', true);
  9291. * ```
  9292. */
  9293. var htmlparser = UE.htmlparser = function (htmlstr, ignoreBlank) {
  9294. //todo 原来的方式 [^"'<>\/] 有\/就不能配对上 <TD vAlign=top background=../AAA.JPG> 这样的标签了
  9295. //先去掉了,加上的原因忘了,这里先记录
  9296. var re_tag = /<(?:(?:\/([^>]+)>)|(?:!--([\S|\s]*?)-->)|(?:([^\s\/<>]+)\s*((?:(?:"[^"]*")|(?:'[^']*')|[^"'<>])*)\/?>))/g,
  9297. re_attr = /([\w\-:.]+)(?:(?:\s*=\s*(?:(?:"([^"]*)")|(?:'([^']*)')|([^\s>]+)))|(?=\s|$))/g;
  9298. //ie下取得的html可能会有\n存在,要去掉,在处理replace(/[\t\r\n]*/g,'');代码高量的\n不能去除
  9299. var allowEmptyTags = {
  9300. b: 1,
  9301. code: 1,
  9302. i: 1,
  9303. u: 1,
  9304. strike: 1,
  9305. s: 1,
  9306. tt: 1,
  9307. strong: 1,
  9308. q: 1,
  9309. samp: 1,
  9310. em: 1,
  9311. span: 1,
  9312. sub: 1,
  9313. img: 1,
  9314. sup: 1,
  9315. font: 1,
  9316. big: 1,
  9317. small: 1,
  9318. iframe: 1,
  9319. a: 1,
  9320. br: 1,
  9321. pre: 1
  9322. };
  9323. htmlstr = htmlstr.replace(new RegExp(domUtils.fillChar, 'g'), '');
  9324. if (!ignoreBlank) {
  9325. htmlstr = htmlstr.replace(new RegExp('[\\r\\t\\n' + (ignoreBlank ? '' : ' ') + ']*<\/?(\\w+)\\s*(?:[^>]*)>[\\r\\t\\n' + (ignoreBlank ? '' : ' ') + ']*', 'g'), function (a, b) {
  9326. //br暂时单独处理
  9327. if (b && allowEmptyTags[b.toLowerCase()]) {
  9328. return a.replace(/(^[\n\r]+)|([\n\r]+$)/g, '');
  9329. }
  9330. return a.replace(new RegExp('^[\\r\\n' + (ignoreBlank ? '' : ' ') + ']+'), '').replace(new RegExp('[\\r\\n' + (ignoreBlank ? '' : ' ') + ']+$'), '');
  9331. });
  9332. }
  9333. var notTransAttrs = {
  9334. 'href': 1,
  9335. 'src': 1
  9336. };
  9337. var uNode = UE.uNode,
  9338. needParentNode = {
  9339. 'td': 'tr',
  9340. 'tr': ['tbody', 'thead', 'tfoot'],
  9341. 'tbody': 'table',
  9342. 'th': 'tr',
  9343. 'thead': 'table',
  9344. 'tfoot': 'table',
  9345. 'caption': 'table',
  9346. 'li': ['ul', 'ol'],
  9347. 'dt': 'dl',
  9348. 'dd': 'dl',
  9349. 'option': 'select'
  9350. },
  9351. needChild = {
  9352. 'ol': 'li',
  9353. 'ul': 'li'
  9354. };
  9355. function text(parent, data) {
  9356. if (needChild[parent.tagName]) {
  9357. var tmpNode = uNode.createElement(needChild[parent.tagName]);
  9358. parent.appendChild(tmpNode);
  9359. tmpNode.appendChild(uNode.createText(data));
  9360. parent = tmpNode;
  9361. } else {
  9362. parent.appendChild(uNode.createText(data));
  9363. }
  9364. }
  9365. function element(parent, tagName, htmlattr) {
  9366. var needParentTag;
  9367. if (needParentTag = needParentNode[tagName]) {
  9368. var tmpParent = parent,
  9369. hasParent;
  9370. while (tmpParent.type != 'root') {
  9371. if (utils.isArray(needParentTag) ? utils.indexOf(needParentTag, tmpParent.tagName) != -1 : needParentTag == tmpParent.tagName) {
  9372. parent = tmpParent;
  9373. hasParent = true;
  9374. break;
  9375. }
  9376. tmpParent = tmpParent.parentNode;
  9377. }
  9378. if (!hasParent) {
  9379. parent = element(parent, utils.isArray(needParentTag) ? needParentTag[0] : needParentTag)
  9380. }
  9381. }
  9382. //按dtd处理嵌套
  9383. // if(parent.type != 'root' && !dtd[parent.tagName][tagName])
  9384. // parent = parent.parentNode;
  9385. var elm = new uNode({
  9386. parentNode: parent,
  9387. type: 'element',
  9388. tagName: tagName.toLowerCase(),
  9389. //是自闭合的处理一下
  9390. children: dtd.$empty[tagName] ? null : []
  9391. });
  9392. //如果属性存在,处理属性
  9393. if (htmlattr) {
  9394. var attrs = {},
  9395. match;
  9396. while (match = re_attr.exec(htmlattr)) {
  9397. attrs[match[1].toLowerCase()] = notTransAttrs[match[1].toLowerCase()] ? (match[2] || match[3] || match[4]) : utils.unhtml(match[2] || match[3] || match[4])
  9398. }
  9399. elm.attrs = attrs;
  9400. }
  9401. //trace:3970
  9402. // //如果parent下不能放elm
  9403. // if(dtd.$inline[parent.tagName] && dtd.$block[elm.tagName] && !dtd[parent.tagName][elm.tagName]){
  9404. // parent = parent.parentNode;
  9405. // elm.parentNode = parent;
  9406. // }
  9407. parent.children.push(elm);
  9408. //如果是自闭合节点返回父亲节点
  9409. return dtd.$empty[tagName] ? parent : elm
  9410. }
  9411. function comment(parent, data) {
  9412. parent.children.push(new uNode({
  9413. type: 'comment',
  9414. data: data,
  9415. parentNode: parent
  9416. }));
  9417. }
  9418. var match, currentIndex = 0,
  9419. nextIndex = 0;
  9420. //设置根节点
  9421. var root = new uNode({
  9422. type: 'root',
  9423. children: []
  9424. });
  9425. var currentParent = root;
  9426. while (match = re_tag.exec(htmlstr)) {
  9427. currentIndex = match.index;
  9428. try {
  9429. if (currentIndex > nextIndex) {
  9430. //text node
  9431. text(currentParent, htmlstr.slice(nextIndex, currentIndex));
  9432. }
  9433. if (match[3]) {
  9434. if (dtd.$cdata[currentParent.tagName]) {
  9435. text(currentParent, match[0]);
  9436. } else {
  9437. //start tag
  9438. currentParent = element(currentParent, match[3].toLowerCase(), match[4]);
  9439. }
  9440. } else if (match[1]) {
  9441. if (currentParent.type != 'root') {
  9442. if (dtd.$cdata[currentParent.tagName] && !dtd.$cdata[match[1]]) {
  9443. text(currentParent, match[0]);
  9444. } else {
  9445. var tmpParent = currentParent;
  9446. while (currentParent.type == 'element' && currentParent.tagName != match[1].toLowerCase()) {
  9447. currentParent = currentParent.parentNode;
  9448. if (currentParent.type == 'root') {
  9449. currentParent = tmpParent;
  9450. throw 'break'
  9451. }
  9452. }
  9453. //end tag
  9454. currentParent = currentParent.parentNode;
  9455. }
  9456. }
  9457. } else if (match[2]) {
  9458. //comment
  9459. comment(currentParent, match[2])
  9460. }
  9461. } catch (e) {}
  9462. nextIndex = re_tag.lastIndex;
  9463. }
  9464. //如果结束是文本,就有可能丢掉,所以这里手动判断一下
  9465. //例如 <li>sdfsdfsdf<li>sdfsdfsdfsdf
  9466. if (nextIndex < htmlstr.length) {
  9467. text(currentParent, htmlstr.slice(nextIndex));
  9468. }
  9469. return root;
  9470. };
  9471. // core/filternode.js
  9472. /**
  9473. * UE过滤节点的静态方法
  9474. * @file
  9475. */
  9476. /**
  9477. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  9478. * @module UE
  9479. */
  9480. /**
  9481. * 根据传入节点和过滤规则过滤相应节点
  9482. * @module UE
  9483. * @since 1.2.6.1
  9484. * @method filterNode
  9485. * @param { Object } root 指定root节点
  9486. * @param { Object } rules 过滤规则json对象
  9487. * @example
  9488. * ```javascript
  9489. * UE.filterNode(root,editor.options.filterRules);
  9490. * ```
  9491. */
  9492. var filterNode = UE.filterNode = function () {
  9493. function filterNode(node, rules) {
  9494. switch (node.type) {
  9495. case 'text':
  9496. break;
  9497. case 'element':
  9498. var val;
  9499. if (val = rules[node.tagName]) {
  9500. if (val === '-') {
  9501. node.parentNode.removeChild(node)
  9502. } else if (utils.isFunction(val)) {
  9503. var parentNode = node.parentNode,
  9504. index = node.getIndex();
  9505. val(node);
  9506. if (node.parentNode) {
  9507. if (node.children) {
  9508. for (var i = 0, ci; ci = node.children[i];) {
  9509. filterNode(ci, rules);
  9510. if (ci.parentNode) {
  9511. i++;
  9512. }
  9513. }
  9514. }
  9515. } else {
  9516. for (var i = index, ci; ci = parentNode.children[i];) {
  9517. filterNode(ci, rules);
  9518. if (ci.parentNode) {
  9519. i++;
  9520. }
  9521. }
  9522. }
  9523. } else {
  9524. var attrs = val['$'];
  9525. if (attrs && node.attrs) {
  9526. var tmpAttrs = {},
  9527. tmpVal;
  9528. for (var a in attrs) {
  9529. tmpVal = node.getAttr(a);
  9530. //todo 只先对style单独处理
  9531. if (a == 'style' && utils.isArray(attrs[a])) {
  9532. var tmpCssStyle = [];
  9533. utils.each(attrs[a], function (v) {
  9534. var tmp;
  9535. if (tmp = node.getStyle(v)) {
  9536. tmpCssStyle.push(v + ':' + tmp);
  9537. }
  9538. });
  9539. tmpVal = tmpCssStyle.join(';')
  9540. }
  9541. if (tmpVal) {
  9542. tmpAttrs[a] = tmpVal;
  9543. }
  9544. }
  9545. node.attrs = tmpAttrs;
  9546. }
  9547. if (node.children) {
  9548. for (var i = 0, ci; ci = node.children[i];) {
  9549. filterNode(ci, rules);
  9550. if (ci.parentNode) {
  9551. i++;
  9552. }
  9553. }
  9554. }
  9555. }
  9556. } else {
  9557. //如果不在名单里扣出子节点并删除该节点,cdata除外
  9558. if (dtd.$cdata[node.tagName]) {
  9559. node.parentNode.removeChild(node)
  9560. } else {
  9561. var parentNode = node.parentNode,
  9562. index = node.getIndex();
  9563. node.parentNode.removeChild(node, true);
  9564. for (var i = index, ci; ci = parentNode.children[i];) {
  9565. filterNode(ci, rules);
  9566. if (ci.parentNode) {
  9567. i++;
  9568. }
  9569. }
  9570. }
  9571. }
  9572. break;
  9573. case 'comment':
  9574. node.parentNode.removeChild(node)
  9575. }
  9576. }
  9577. return function (root, rules) {
  9578. if (utils.isEmptyObject(rules)) {
  9579. return root;
  9580. }
  9581. var val;
  9582. if (val = rules['-']) {
  9583. utils.each(val.split(' '), function (k) {
  9584. rules[k] = '-'
  9585. })
  9586. }
  9587. for (var i = 0, ci; ci = root.children[i];) {
  9588. filterNode(ci, rules);
  9589. if (ci.parentNode) {
  9590. i++;
  9591. }
  9592. }
  9593. return root;
  9594. }
  9595. }();
  9596. // core/plugin.js
  9597. /**
  9598. * Created with JetBrains PhpStorm.
  9599. * User: campaign
  9600. * Date: 10/8/13
  9601. * Time: 6:15 PM
  9602. * To change this template use File | Settings | File Templates.
  9603. */
  9604. UE.plugin = function () {
  9605. var _plugins = {};
  9606. return {
  9607. register: function (pluginName, fn, oldOptionName, afterDisabled) {
  9608. if (oldOptionName && utils.isFunction(oldOptionName)) {
  9609. afterDisabled = oldOptionName;
  9610. oldOptionName = null
  9611. }
  9612. _plugins[pluginName] = {
  9613. optionName: oldOptionName || pluginName,
  9614. execFn: fn,
  9615. //当插件被禁用时执行
  9616. afterDisabled: afterDisabled
  9617. }
  9618. },
  9619. load: function (editor) {
  9620. utils.each(_plugins, function (plugin) {
  9621. var _export = plugin.execFn.call(editor);
  9622. if (editor.options[plugin.optionName] !== false) {
  9623. if (_export) {
  9624. //后边需要再做扩展
  9625. utils.each(_export, function (v, k) {
  9626. switch (k.toLowerCase()) {
  9627. case 'shortcutkey':
  9628. editor.addshortcutkey(v);
  9629. break;
  9630. case 'bindevents':
  9631. utils.each(v, function (fn, eventName) {
  9632. editor.addListener(eventName, fn);
  9633. });
  9634. break;
  9635. case 'bindmultievents':
  9636. utils.each(utils.isArray(v) ? v : [v], function (event) {
  9637. var types = utils.trim(event.type).split(/\s+/);
  9638. utils.each(types, function (eventName) {
  9639. editor.addListener(eventName, event.handler);
  9640. });
  9641. });
  9642. break;
  9643. case 'commands':
  9644. utils.each(v, function (execFn, execName) {
  9645. editor.commands[execName] = execFn
  9646. });
  9647. break;
  9648. case 'outputrule':
  9649. editor.addOutputRule(v);
  9650. break;
  9651. case 'inputrule':
  9652. editor.addInputRule(v);
  9653. break;
  9654. case 'defaultoptions':
  9655. editor.setOpt(v)
  9656. }
  9657. })
  9658. }
  9659. } else if (plugin.afterDisabled) {
  9660. plugin.afterDisabled.call(editor)
  9661. }
  9662. });
  9663. //向下兼容
  9664. utils.each(UE.plugins, function (plugin) {
  9665. plugin.call(editor);
  9666. });
  9667. },
  9668. run: function (pluginName, editor) {
  9669. var plugin = _plugins[pluginName];
  9670. if (plugin) {
  9671. plugin.exeFn.call(editor)
  9672. }
  9673. }
  9674. }
  9675. }();
  9676. // core/keymap.js
  9677. var keymap = UE.keymap = {
  9678. 'Backspace': 8,
  9679. 'Tab': 9,
  9680. 'Enter': 13,
  9681. 'Shift': 16,
  9682. 'Control': 17,
  9683. 'Alt': 18,
  9684. 'CapsLock': 20,
  9685. 'Esc': 27,
  9686. 'Spacebar': 32,
  9687. 'PageUp': 33,
  9688. 'PageDown': 34,
  9689. 'End': 35,
  9690. 'Home': 36,
  9691. 'Left': 37,
  9692. 'Up': 38,
  9693. 'Right': 39,
  9694. 'Down': 40,
  9695. 'Insert': 45,
  9696. 'Del': 46,
  9697. 'NumLock': 144,
  9698. 'Cmd': 91,
  9699. '=': 187,
  9700. '-': 189,
  9701. "b": 66,
  9702. 'i': 73,
  9703. //回退
  9704. 'z': 90,
  9705. 'y': 89,
  9706. //粘贴
  9707. 'v': 86,
  9708. 'x': 88,
  9709. 's': 83,
  9710. 'n': 78
  9711. };
  9712. // core/localstorage.js
  9713. //存储媒介封装
  9714. var LocalStorage = UE.LocalStorage = (function () {
  9715. var storage = window.localStorage || getUserData() || null,
  9716. LOCAL_FILE = 'localStorage';
  9717. return {
  9718. saveLocalData: function (key, data) {
  9719. if (storage && data) {
  9720. storage.setItem(key, data);
  9721. return true;
  9722. }
  9723. return false;
  9724. },
  9725. getLocalData: function (key) {
  9726. if (storage) {
  9727. return storage.getItem(key);
  9728. }
  9729. return null;
  9730. },
  9731. removeItem: function (key) {
  9732. storage && storage.removeItem(key);
  9733. }
  9734. };
  9735. function getUserData() {
  9736. var container = document.createElement("div");
  9737. container.style.display = "none";
  9738. if (!container.addBehavior) {
  9739. return null;
  9740. }
  9741. container.addBehavior("#default#userdata");
  9742. return {
  9743. getItem: function (key) {
  9744. var result = null;
  9745. try {
  9746. document.body.appendChild(container);
  9747. container.load(LOCAL_FILE);
  9748. result = container.getAttribute(key);
  9749. document.body.removeChild(container);
  9750. } catch (e) {}
  9751. return result;
  9752. },
  9753. setItem: function (key, value) {
  9754. document.body.appendChild(container);
  9755. container.setAttribute(key, value);
  9756. container.save(LOCAL_FILE);
  9757. document.body.removeChild(container);
  9758. },
  9759. //// 暂时没有用到
  9760. //clear: function () {
  9761. //
  9762. // var expiresTime = new Date();
  9763. // expiresTime.setFullYear(expiresTime.getFullYear() - 1);
  9764. // document.body.appendChild(container);
  9765. // container.expires = expiresTime.toUTCString();
  9766. // container.save(LOCAL_FILE);
  9767. // document.body.removeChild(container);
  9768. //
  9769. //},
  9770. removeItem: function (key) {
  9771. document.body.appendChild(container);
  9772. container.removeAttribute(key);
  9773. container.save(LOCAL_FILE);
  9774. document.body.removeChild(container);
  9775. }
  9776. };
  9777. }
  9778. })();
  9779. (function () {
  9780. var ROOTKEY = 'ueditor_preference';
  9781. UE.Editor.prototype.setPreferences = function (key, value) {
  9782. var obj = {};
  9783. if (utils.isString(key)) {
  9784. obj[key] = value;
  9785. } else {
  9786. obj = key;
  9787. }
  9788. var data = LocalStorage.getLocalData(ROOTKEY);
  9789. if (data && (data = utils.str2json(data))) {
  9790. utils.extend(data, obj);
  9791. } else {
  9792. data = obj;
  9793. }
  9794. data && LocalStorage.saveLocalData(ROOTKEY, utils.json2str(data));
  9795. };
  9796. UE.Editor.prototype.getPreferences = function (key) {
  9797. var data = LocalStorage.getLocalData(ROOTKEY);
  9798. if (data && (data = utils.str2json(data))) {
  9799. return key ? data[key] : data
  9800. }
  9801. return null;
  9802. };
  9803. UE.Editor.prototype.removePreferences = function (key) {
  9804. var data = LocalStorage.getLocalData(ROOTKEY);
  9805. if (data && (data = utils.str2json(data))) {
  9806. data[key] = undefined;
  9807. delete data[key]
  9808. }
  9809. data && LocalStorage.saveLocalData(ROOTKEY, utils.json2str(data));
  9810. };
  9811. })();
  9812. // plugins/defaultfilter.js
  9813. ///import core
  9814. ///plugin 编辑器默认的过滤转换机制
  9815. UE.plugins['defaultfilter'] = function () {
  9816. var me = this;
  9817. me.setOpt({
  9818. 'allowDivTransToP': true,
  9819. 'disabledTableInTable': true
  9820. });
  9821. //默认的过滤处理
  9822. //进入编辑器的内容处理
  9823. me.addInputRule(function (root) {
  9824. var allowDivTransToP = this.options.allowDivTransToP;
  9825. var val;
  9826. function tdParent(node) {
  9827. while (node && node.type == 'element') {
  9828. if (node.tagName == 'td') {
  9829. return true;
  9830. }
  9831. node = node.parentNode;
  9832. }
  9833. return false;
  9834. }
  9835. //进行默认的处理
  9836. root.traversal(function (node) {
  9837. if (node.type == 'element') {
  9838. if (!dtd.$cdata[node.tagName] && me.options.autoClearEmptyNode && dtd.$inline[node.tagName] && !dtd.$empty[node.tagName] && (!node.attrs || utils.isEmptyObject(node.attrs))) {
  9839. if (!node.firstChild()) node.parentNode.removeChild(node);
  9840. else if (node.tagName == 'span' && (!node.attrs || utils.isEmptyObject(node.attrs))) {
  9841. node.parentNode.removeChild(node, true)
  9842. }
  9843. return;
  9844. }
  9845. switch (node.tagName) {
  9846. case 'style':
  9847. case 'script':
  9848. node.setAttr({
  9849. cdata_tag: node.tagName,
  9850. cdata_data: (node.innerHTML() || ''),
  9851. '_ue_custom_node_': 'true'
  9852. });
  9853. node.tagName = 'div';
  9854. node.innerHTML('');
  9855. break;
  9856. case 'a':
  9857. if (val = node.getAttr('href')) {
  9858. node.setAttr('_href', val)
  9859. }
  9860. break;
  9861. // case 'img':
  9862. // //todo base64暂时去掉,后边做远程图片上传后,干掉这个
  9863. // if (val = node.getAttr('src')) {
  9864. // if (/^data:/.test(val)) {
  9865. // node.parentNode.removeChild(node);
  9866. // break;
  9867. // }
  9868. // }
  9869. // node.setAttr('_src', node.getAttr('src'));
  9870. // break;
  9871. case 'span':
  9872. if (browser.webkit && (val = node.getStyle('white-space'))) {
  9873. if (/nowrap|normal/.test(val)) {
  9874. node.setStyle('white-space', '');
  9875. if (me.options.autoClearEmptyNode && utils.isEmptyObject(node.attrs)) {
  9876. node.parentNode.removeChild(node, true)
  9877. }
  9878. }
  9879. }
  9880. val = node.getAttr('id');
  9881. if (val && /^_baidu_bookmark_/i.test(val)) {
  9882. node.parentNode.removeChild(node)
  9883. }
  9884. break;
  9885. case 'p':
  9886. if (val = node.getAttr('align')) {
  9887. node.setAttr('align');
  9888. node.setStyle('text-align', val)
  9889. }
  9890. //trace:3431
  9891. // var cssStyle = node.getAttr('style');
  9892. // if (cssStyle) {
  9893. // cssStyle = cssStyle.replace(/(margin|padding)[^;]+/g, '');
  9894. // node.setAttr('style', cssStyle)
  9895. //
  9896. // }
  9897. //p标签不允许嵌套
  9898. utils.each(node.children, function (n) {
  9899. if (n.type == 'element' && n.tagName == 'p') {
  9900. var next = n.nextSibling();
  9901. node.parentNode.insertAfter(n, node);
  9902. var last = n;
  9903. while (next) {
  9904. var tmp = next.nextSibling();
  9905. node.parentNode.insertAfter(next, last);
  9906. last = next;
  9907. next = tmp;
  9908. }
  9909. return false;
  9910. }
  9911. });
  9912. if (!node.firstChild()) {
  9913. node.innerHTML(browser.ie ? '&nbsp;' : '<br/>')
  9914. }
  9915. break;
  9916. case 'div':
  9917. if (node.getAttr('cdata_tag')) {
  9918. break;
  9919. }
  9920. //针对代码这里不处理插入代码的div
  9921. val = node.getAttr('class');
  9922. if (val && /^line number\d+/.test(val)) {
  9923. break;
  9924. }
  9925. if (!allowDivTransToP) {
  9926. break;
  9927. }
  9928. var tmpNode, p = UE.uNode.createElement('p');
  9929. while (tmpNode = node.firstChild()) {
  9930. if (tmpNode.type == 'text' || !UE.dom.dtd.$block[tmpNode.tagName]) {
  9931. p.appendChild(tmpNode);
  9932. } else {
  9933. if (p.firstChild()) {
  9934. node.parentNode.insertBefore(p, node);
  9935. p = UE.uNode.createElement('p');
  9936. } else {
  9937. node.parentNode.insertBefore(tmpNode, node);
  9938. }
  9939. }
  9940. }
  9941. if (p.firstChild()) {
  9942. node.parentNode.insertBefore(p, node);
  9943. }
  9944. node.parentNode.removeChild(node);
  9945. break;
  9946. case 'dl':
  9947. node.tagName = 'ul';
  9948. break;
  9949. case 'dt':
  9950. case 'dd':
  9951. node.tagName = 'li';
  9952. break;
  9953. case 'li':
  9954. var className = node.getAttr('class');
  9955. if (!className || !/list\-/.test(className)) {
  9956. node.setAttr()
  9957. }
  9958. var tmpNodes = node.getNodesByTagName('ol ul');
  9959. UE.utils.each(tmpNodes, function (n) {
  9960. node.parentNode.insertAfter(n, node);
  9961. });
  9962. break;
  9963. case 'td':
  9964. case 'th':
  9965. case 'caption':
  9966. if (!node.children || !node.children.length) {
  9967. node.appendChild(browser.ie11below ? UE.uNode.createText(' ') : UE.uNode.createElement('br'))
  9968. }
  9969. break;
  9970. case 'table':
  9971. if (me.options.disabledTableInTable && tdParent(node)) {
  9972. node.parentNode.insertBefore(UE.uNode.createText(node.innerText()), node);
  9973. node.parentNode.removeChild(node)
  9974. }
  9975. }
  9976. }
  9977. // if(node.type == 'comment'){
  9978. // node.parentNode.removeChild(node);
  9979. // }
  9980. })
  9981. });
  9982. //从编辑器出去的内容处理
  9983. me.addOutputRule(function (root) {
  9984. var val;
  9985. root.traversal(function (node) {
  9986. if (node.type == 'element') {
  9987. if (me.options.autoClearEmptyNode && dtd.$inline[node.tagName] && !dtd.$empty[node.tagName] && (!node.attrs || utils.isEmptyObject(node.attrs))) {
  9988. if (!node.firstChild()) node.parentNode.removeChild(node);
  9989. else if (node.tagName == 'span' && (!node.attrs || utils.isEmptyObject(node.attrs))) {
  9990. node.parentNode.removeChild(node, true)
  9991. }
  9992. return;
  9993. }
  9994. switch (node.tagName) {
  9995. case 'div':
  9996. if (val = node.getAttr('cdata_tag')) {
  9997. node.tagName = val;
  9998. node.appendChild(UE.uNode.createText(node.getAttr('cdata_data')));
  9999. node.setAttr({
  10000. cdata_tag: '',
  10001. cdata_data: '',
  10002. '_ue_custom_node_': ''
  10003. });
  10004. }
  10005. break;
  10006. case 'a':
  10007. if (val = node.getAttr('_href')) {
  10008. node.setAttr({
  10009. 'href': utils.html(val),
  10010. '_href': ''
  10011. })
  10012. }
  10013. break;
  10014. break;
  10015. case 'span':
  10016. val = node.getAttr('id');
  10017. if (val && /^_baidu_bookmark_/i.test(val)) {
  10018. node.parentNode.removeChild(node)
  10019. }
  10020. break;
  10021. case 'img':
  10022. if (val = node.getAttr('_src')) {
  10023. node.setAttr({
  10024. 'src': node.getAttr('_src'),
  10025. '_src': ''
  10026. })
  10027. }
  10028. }
  10029. }
  10030. })
  10031. });
  10032. };
  10033. // plugins/inserthtml.js
  10034. /**
  10035. * 插入html字符串插件
  10036. * @file
  10037. * @since 1.2.6.1
  10038. */
  10039. /**
  10040. * 插入html代码
  10041. * @command inserthtml
  10042. * @method execCommand
  10043. * @param { String } cmd 命令字符串
  10044. * @param { String } html 插入的html字符串
  10045. * @remaind 插入的标签内容是在当前的选区位置上插入,如果当前是闭合状态,那直接插入内容, 如果当前是选中状态,将先清除当前选中内容后,再做插入
  10046. * @warning 注意:该命令会对当前选区的位置,对插入的内容进行过滤转换处理。 过滤的规则遵循html语意化的原则。
  10047. * @example
  10048. * ```javascript
  10049. * //xxx[BB]xxx 当前选区为非闭合选区,选中BB这两个文本
  10050. * //执行命令,插入<b>CC</b>
  10051. * //插入后的效果 xxx<b>CC</b>xxx
  10052. * //<p>xx|xxx</p> 当前选区为闭合状态
  10053. * //插入<p>CC</p>
  10054. * //结果 <p>xx</p><p>CC</p><p>xxx</p>
  10055. * //<p>xxxx</p>|</p>xxx</p> 当前选区在两个p标签之间
  10056. * //插入 xxxx
  10057. * //结果 <p>xxxx</p><p>xxxx</p></p>xxx</p>
  10058. * ```
  10059. */
  10060. UE.commands['inserthtml'] = {
  10061. execCommand: function (command, html, notNeedFilter) {
  10062. var me = this,
  10063. range,
  10064. div;
  10065. if (!html) {
  10066. return;
  10067. }
  10068. if (me.fireEvent('beforeinserthtml', html) === true) {
  10069. return;
  10070. }
  10071. range = me.selection.getRange();
  10072. div = range.document.createElement('div');
  10073. div.style.display = 'inline';
  10074. if (!notNeedFilter) {
  10075. var root = UE.htmlparser(html);
  10076. //如果给了过滤规则就先进行过滤
  10077. if (me.options.filterRules) {
  10078. UE.filterNode(root, me.options.filterRules);
  10079. }
  10080. //执行默认的处理
  10081. me.filterInputRule(root);
  10082. html = root.toHtml()
  10083. }
  10084. div.innerHTML = utils.trim(html);
  10085. if (!range.collapsed) {
  10086. var tmpNode = range.startContainer;
  10087. if (domUtils.isFillChar(tmpNode)) {
  10088. range.setStartBefore(tmpNode)
  10089. }
  10090. tmpNode = range.endContainer;
  10091. if (domUtils.isFillChar(tmpNode)) {
  10092. range.setEndAfter(tmpNode)
  10093. }
  10094. range.txtToElmBoundary();
  10095. //结束边界可能放到了br的前边,要把br包含进来
  10096. // x[xxx]<br/>
  10097. if (range.endContainer && range.endContainer.nodeType == 1) {
  10098. tmpNode = range.endContainer.childNodes[range.endOffset];
  10099. if (tmpNode && domUtils.isBr(tmpNode)) {
  10100. range.setEndAfter(tmpNode);
  10101. }
  10102. }
  10103. if (range.startOffset == 0) {
  10104. tmpNode = range.startContainer;
  10105. if (domUtils.isBoundaryNode(tmpNode, 'firstChild')) {
  10106. tmpNode = range.endContainer;
  10107. if (range.endOffset == (tmpNode.nodeType == 3 ? tmpNode.nodeValue.length : tmpNode.childNodes.length) && domUtils.isBoundaryNode(tmpNode, 'lastChild')) {
  10108. me.body.innerHTML = '<p>' + (browser.ie ? '' : '<br/>') + '</p>';
  10109. range.setStart(me.body.firstChild, 0).collapse(true)
  10110. }
  10111. }
  10112. }!range.collapsed && range.deleteContents();
  10113. if (range.startContainer.nodeType == 1) {
  10114. var child = range.startContainer.childNodes[range.startOffset],
  10115. pre;
  10116. if (child && domUtils.isBlockElm(child) && (pre = child.previousSibling) && domUtils.isBlockElm(pre)) {
  10117. range.setEnd(pre, pre.childNodes.length).collapse();
  10118. while (child.firstChild) {
  10119. pre.appendChild(child.firstChild);
  10120. }
  10121. domUtils.remove(child);
  10122. }
  10123. }
  10124. }
  10125. var child, parent, pre, tmp, hadBreak = 0,
  10126. nextNode;
  10127. //如果当前位置选中了fillchar要干掉,要不会产生空行
  10128. if (range.inFillChar()) {
  10129. child = range.startContainer;
  10130. if (domUtils.isFillChar(child)) {
  10131. range.setStartBefore(child).collapse(true);
  10132. domUtils.remove(child);
  10133. } else if (domUtils.isFillChar(child, true)) {
  10134. child.nodeValue = child.nodeValue.replace(fillCharReg, '');
  10135. range.startOffset--;
  10136. range.collapsed && range.collapse(true)
  10137. }
  10138. }
  10139. //列表单独处理
  10140. var li = domUtils.findParentByTagName(range.startContainer, 'li', true);
  10141. if (li) {
  10142. var next, last;
  10143. while (child = div.firstChild) {
  10144. //针对hr单独处理一下先
  10145. while (child && (child.nodeType == 3 || !domUtils.isBlockElm(child) || child.tagName == 'HR')) {
  10146. next = child.nextSibling;
  10147. range.insertNode(child).collapse();
  10148. last = child;
  10149. child = next;
  10150. }
  10151. if (child) {
  10152. if (/^(ol|ul)$/i.test(child.tagName)) {
  10153. while (child.firstChild) {
  10154. last = child.firstChild;
  10155. domUtils.insertAfter(li, child.firstChild);
  10156. li = li.nextSibling;
  10157. }
  10158. domUtils.remove(child)
  10159. } else {
  10160. var tmpLi;
  10161. next = child.nextSibling;
  10162. tmpLi = me.document.createElement('li');
  10163. domUtils.insertAfter(li, tmpLi);
  10164. tmpLi.appendChild(child);
  10165. last = child;
  10166. child = next;
  10167. li = tmpLi;
  10168. }
  10169. }
  10170. }
  10171. li = domUtils.findParentByTagName(range.startContainer, 'li', true);
  10172. if (domUtils.isEmptyBlock(li)) {
  10173. domUtils.remove(li)
  10174. }
  10175. if (last) {
  10176. range.setStartAfter(last).collapse(true).select(true)
  10177. }
  10178. } else {
  10179. while (child = div.firstChild) {
  10180. if (hadBreak) {
  10181. var p = me.document.createElement('p');
  10182. while (child && (child.nodeType == 3 || !dtd.$block[child.tagName])) {
  10183. nextNode = child.nextSibling;
  10184. p.appendChild(child);
  10185. child = nextNode;
  10186. }
  10187. if (p.firstChild) {
  10188. child = p
  10189. }
  10190. }
  10191. range.insertNode(child);
  10192. nextNode = child.nextSibling;
  10193. if (!hadBreak && child.nodeType == domUtils.NODE_ELEMENT && domUtils.isBlockElm(child)) {
  10194. parent = domUtils.findParent(child, function (node) {
  10195. return domUtils.isBlockElm(node);
  10196. });
  10197. if (parent && parent.tagName.toLowerCase() != 'body' && !(dtd[parent.tagName][child.nodeName] && child.parentNode === parent)) {
  10198. if (!dtd[parent.tagName][child.nodeName]) {
  10199. pre = parent;
  10200. } else {
  10201. tmp = child.parentNode;
  10202. while (tmp !== parent) {
  10203. pre = tmp;
  10204. tmp = tmp.parentNode;
  10205. }
  10206. }
  10207. domUtils.breakParent(child, pre || tmp);
  10208. //去掉break后前一个多余的节点 <p>|<[p> ==> <p></p><div></div><p>|</p>
  10209. var pre = child.previousSibling;
  10210. domUtils.trimWhiteTextNode(pre);
  10211. if (!pre.childNodes.length) {
  10212. domUtils.remove(pre);
  10213. }
  10214. //trace:2012,在非ie的情况,切开后剩下的节点有可能不能点入光标添加br占位
  10215. if (!browser.ie &&
  10216. (next = child.nextSibling) &&
  10217. domUtils.isBlockElm(next) &&
  10218. next.lastChild &&
  10219. !domUtils.isBr(next.lastChild)) {
  10220. next.appendChild(me.document.createElement('br'));
  10221. }
  10222. hadBreak = 1;
  10223. }
  10224. }
  10225. var next = child.nextSibling;
  10226. if (!div.firstChild && next && domUtils.isBlockElm(next)) {
  10227. range.setStart(next, 0).collapse(true);
  10228. break;
  10229. }
  10230. range.setEndAfter(child).collapse();
  10231. }
  10232. child = range.startContainer;
  10233. if (nextNode && domUtils.isBr(nextNode)) {
  10234. domUtils.remove(nextNode)
  10235. }
  10236. //用chrome可能有空白展位符
  10237. if (domUtils.isBlockElm(child) && domUtils.isEmptyNode(child)) {
  10238. if (nextNode = child.nextSibling) {
  10239. domUtils.remove(child);
  10240. if (nextNode.nodeType == 1 && dtd.$block[nextNode.tagName]) {
  10241. range.setStart(nextNode, 0).collapse(true).shrinkBoundary()
  10242. }
  10243. } else {
  10244. try {
  10245. child.innerHTML = browser.ie ? domUtils.fillChar : '<br/>';
  10246. } catch (e) {
  10247. range.setStartBefore(child);
  10248. domUtils.remove(child)
  10249. }
  10250. }
  10251. }
  10252. //加上true因为在删除表情等时会删两次,第一次是删的fillData
  10253. try {
  10254. range.select(true);
  10255. } catch (e) {}
  10256. }
  10257. setTimeout(function () {
  10258. range = me.selection.getRange();
  10259. range.scrollToView(me.autoHeightEnabled, me.autoHeightEnabled ? domUtils.getXY(me.iframe).y : 0);
  10260. me.fireEvent('afterinserthtml', html);
  10261. }, 200);
  10262. }
  10263. };
  10264. // plugins/autotypeset.js
  10265. /**
  10266. * 自动排版
  10267. * @file
  10268. * @since 1.2.6.1
  10269. */
  10270. /**
  10271. * 对当前编辑器的内容执行自动排版, 排版的行为根据config配置文件里的“autotypeset”选项进行控制。
  10272. * @command autotypeset
  10273. * @method execCommand
  10274. * @param { String } cmd 命令字符串
  10275. * @example
  10276. * ```javascript
  10277. * editor.execCommand( 'autotypeset' );
  10278. * ```
  10279. */
  10280. UE.plugins['autotypeset'] = function () {
  10281. this.setOpt({
  10282. 'autotypeset': {
  10283. mergeEmptyline: true, //合并空行
  10284. removeClass: true, //去掉冗余的class
  10285. removeEmptyline: false, //去掉空行
  10286. textAlign: "left", //段落的排版方式,可以是 left,right,center,justify 去掉这个属性表示不执行排版
  10287. imageBlockLine: 'center', //图片的浮动方式,独占一行剧中,左右浮动,默认: center,left,right,none 去掉这个属性表示不执行排版
  10288. pasteFilter: false, //根据规则过滤没事粘贴进来的内容
  10289. clearFontSize: false, //去掉所有的内嵌字号,使用编辑器默认的字号
  10290. clearFontFamily: false, //去掉所有的内嵌字体,使用编辑器默认的字体
  10291. removeEmptyNode: false, // 去掉空节点
  10292. //可以去掉的标签
  10293. removeTagNames: utils.extend({
  10294. div: 1
  10295. }, dtd.$removeEmpty),
  10296. indent: false, // 行首缩进
  10297. indentValue: '2em', //行首缩进的大小
  10298. bdc2sb: false,
  10299. tobdc: false
  10300. }
  10301. });
  10302. var me = this,
  10303. opt = me.options.autotypeset,
  10304. remainClass = {
  10305. 'selectTdClass': 1,
  10306. 'pagebreak': 1,
  10307. 'anchorclass': 1
  10308. },
  10309. remainTag = {
  10310. 'li': 1
  10311. },
  10312. tags = {
  10313. div: 1,
  10314. p: 1,
  10315. //trace:2183 这些也认为是行
  10316. blockquote: 1,
  10317. center: 1,
  10318. h1: 1,
  10319. h2: 1,
  10320. h3: 1,
  10321. h4: 1,
  10322. h5: 1,
  10323. h6: 1,
  10324. span: 1
  10325. },
  10326. highlightCont;
  10327. //升级了版本,但配置项目里没有autotypeset
  10328. if (!opt) {
  10329. return;
  10330. }
  10331. readLocalOpts();
  10332. function isLine(node, notEmpty) {
  10333. if (!node || node.nodeType == 3)
  10334. return 0;
  10335. if (domUtils.isBr(node))
  10336. return 1;
  10337. if (node && node.parentNode && tags[node.tagName.toLowerCase()]) {
  10338. if (highlightCont && highlightCont.contains(node) ||
  10339. node.getAttribute('pagebreak')
  10340. ) {
  10341. return 0;
  10342. }
  10343. return notEmpty ? !domUtils.isEmptyBlock(node) : domUtils.isEmptyBlock(node, new RegExp('[\\s' + domUtils.fillChar +
  10344. ']', 'g'));
  10345. }
  10346. }
  10347. function removeNotAttributeSpan(node) {
  10348. if (!node.style.cssText) {
  10349. domUtils.removeAttributes(node, ['style']);
  10350. if (node.tagName.toLowerCase() == 'span' && domUtils.hasNoAttributes(node)) {
  10351. domUtils.remove(node, true);
  10352. }
  10353. }
  10354. }
  10355. function autotype(type, html) {
  10356. var me = this,
  10357. cont;
  10358. if (html) {
  10359. if (!opt.pasteFilter) {
  10360. return;
  10361. }
  10362. cont = me.document.createElement('div');
  10363. cont.innerHTML = html.html;
  10364. } else {
  10365. cont = me.document.body;
  10366. }
  10367. var nodes = domUtils.getElementsByTagName(cont, '*');
  10368. // 行首缩进,段落方向,段间距,段内间距
  10369. for (var i = 0, ci; ci = nodes[i++];) {
  10370. if (me.fireEvent('excludeNodeinautotype', ci) === true) {
  10371. continue;
  10372. }
  10373. //font-size
  10374. if (opt.clearFontSize && ci.style.fontSize) {
  10375. domUtils.removeStyle(ci, 'font-size');
  10376. removeNotAttributeSpan(ci);
  10377. }
  10378. //font-family
  10379. if (opt.clearFontFamily && ci.style.fontFamily) {
  10380. domUtils.removeStyle(ci, 'font-family');
  10381. removeNotAttributeSpan(ci);
  10382. }
  10383. if (isLine(ci)) {
  10384. //合并空行
  10385. if (opt.mergeEmptyline) {
  10386. var next = ci.nextSibling,
  10387. tmpNode, isBr = domUtils.isBr(ci);
  10388. while (isLine(next)) {
  10389. tmpNode = next;
  10390. next = tmpNode.nextSibling;
  10391. if (isBr && (!next || next && !domUtils.isBr(next))) {
  10392. break;
  10393. }
  10394. domUtils.remove(tmpNode);
  10395. }
  10396. }
  10397. //去掉空行,保留占位的空行
  10398. if (opt.removeEmptyline && domUtils.inDoc(ci, cont) && !remainTag[ci.parentNode.tagName.toLowerCase()]) {
  10399. if (domUtils.isBr(ci)) {
  10400. next = ci.nextSibling;
  10401. if (next && !domUtils.isBr(next)) {
  10402. continue;
  10403. }
  10404. }
  10405. domUtils.remove(ci);
  10406. continue;
  10407. }
  10408. }
  10409. if (isLine(ci, true) && ci.tagName != 'SPAN') {
  10410. if (opt.indent) {
  10411. ci.style.textIndent = opt.indentValue;
  10412. }
  10413. if (opt.textAlign) {
  10414. ci.style.textAlign = opt.textAlign;
  10415. }
  10416. // if(opt.lineHeight)
  10417. // ci.style.lineHeight = opt.lineHeight + 'cm';
  10418. }
  10419. //去掉class,保留的class不去掉
  10420. if (opt.removeClass && ci.className && !remainClass[ci.className.toLowerCase()]) {
  10421. if (highlightCont && highlightCont.contains(ci)) {
  10422. continue;
  10423. }
  10424. domUtils.removeAttributes(ci, ['class']);
  10425. }
  10426. //表情不处理
  10427. if (opt.imageBlockLine && ci.tagName.toLowerCase() == 'img' && !ci.getAttribute('emotion')) {
  10428. if (html) {
  10429. var img = ci;
  10430. switch (opt.imageBlockLine) {
  10431. case 'left':
  10432. case 'right':
  10433. case 'none':
  10434. var pN = img.parentNode,
  10435. tmpNode, pre, next;
  10436. while (dtd.$inline[pN.tagName] || pN.tagName == 'A') {
  10437. pN = pN.parentNode;
  10438. }
  10439. tmpNode = pN;
  10440. if (tmpNode.tagName == 'P' && domUtils.getStyle(tmpNode, 'text-align') == 'center') {
  10441. if (!domUtils.isBody(tmpNode) && domUtils.getChildCount(tmpNode, function (node) {
  10442. return !domUtils.isBr(node) && !domUtils.isWhitespace(node)
  10443. }) == 1) {
  10444. pre = tmpNode.previousSibling;
  10445. next = tmpNode.nextSibling;
  10446. if (pre && next && pre.nodeType == 1 && next.nodeType == 1 && pre.tagName == next.tagName && domUtils.isBlockElm(pre)) {
  10447. pre.appendChild(tmpNode.firstChild);
  10448. while (next.firstChild) {
  10449. pre.appendChild(next.firstChild);
  10450. }
  10451. domUtils.remove(tmpNode);
  10452. domUtils.remove(next);
  10453. } else {
  10454. domUtils.setStyle(tmpNode, 'text-align', '');
  10455. }
  10456. }
  10457. }
  10458. domUtils.setStyle(img, 'float', opt.imageBlockLine);
  10459. break;
  10460. case 'center':
  10461. if (me.queryCommandValue('imagefloat') != 'center') {
  10462. pN = img.parentNode;
  10463. domUtils.setStyle(img, 'float', 'none');
  10464. tmpNode = img;
  10465. while (pN && domUtils.getChildCount(pN, function (node) {
  10466. return !domUtils.isBr(node) && !domUtils.isWhitespace(node)
  10467. }) == 1 &&
  10468. (dtd.$inline[pN.tagName] || pN.tagName == 'A')) {
  10469. tmpNode = pN;
  10470. pN = pN.parentNode;
  10471. }
  10472. var pNode = me.document.createElement('p');
  10473. domUtils.setAttributes(pNode, {
  10474. style: 'text-align:center'
  10475. });
  10476. tmpNode.parentNode.insertBefore(pNode, tmpNode);
  10477. pNode.appendChild(tmpNode);
  10478. domUtils.setStyle(tmpNode, 'float', '');
  10479. }
  10480. }
  10481. } else {
  10482. var range = me.selection.getRange();
  10483. range.selectNode(ci).select();
  10484. me.execCommand('imagefloat', opt.imageBlockLine);
  10485. }
  10486. }
  10487. //去掉冗余的标签
  10488. if (opt.removeEmptyNode) {
  10489. if (opt.removeTagNames[ci.tagName.toLowerCase()] && domUtils.hasNoAttributes(ci) && domUtils.isEmptyBlock(ci)) {
  10490. domUtils.remove(ci);
  10491. }
  10492. }
  10493. }
  10494. if (opt.tobdc) {
  10495. var root = UE.htmlparser(cont.innerHTML);
  10496. root.traversal(function (node) {
  10497. if (node.type == 'text') {
  10498. node.data = ToDBC(node.data)
  10499. }
  10500. });
  10501. cont.innerHTML = root.toHtml()
  10502. }
  10503. if (opt.bdc2sb) {
  10504. var root = UE.htmlparser(cont.innerHTML);
  10505. root.traversal(function (node) {
  10506. if (node.type == 'text') {
  10507. node.data = DBC2SB(node.data)
  10508. }
  10509. });
  10510. cont.innerHTML = root.toHtml()
  10511. }
  10512. if (html) {
  10513. html.html = cont.innerHTML;
  10514. }
  10515. }
  10516. if (opt.pasteFilter) {
  10517. me.addListener('beforepaste', autotype);
  10518. }
  10519. function DBC2SB(str) {
  10520. var result = '';
  10521. for (var i = 0; i < str.length; i++) {
  10522. var code = str.charCodeAt(i); //获取当前字符的unicode编码
  10523. if (code >= 65281 && code <= 65373) //在这个unicode编码范围中的是所有的英文字母已经各种字符
  10524. {
  10525. result += String.fromCharCode(str.charCodeAt(i) - 65248); //把全角字符的unicode编码转换为对应半角字符的unicode码
  10526. } else if (code == 12288) //空格
  10527. {
  10528. result += String.fromCharCode(str.charCodeAt(i) - 12288 + 32);
  10529. } else {
  10530. result += str.charAt(i);
  10531. }
  10532. }
  10533. return result;
  10534. }
  10535. function ToDBC(txtstring) {
  10536. txtstring = utils.html(txtstring);
  10537. var tmp = "";
  10538. var mark = ""; /*用于判断,如果是html尖括里的标记,则不进行全角的转换*/
  10539. for (var i = 0; i < txtstring.length; i++) {
  10540. if (txtstring.charCodeAt(i) == 32) {
  10541. tmp = tmp + String.fromCharCode(12288);
  10542. } else if (txtstring.charCodeAt(i) < 127) {
  10543. tmp = tmp + String.fromCharCode(txtstring.charCodeAt(i) + 65248);
  10544. } else {
  10545. tmp += txtstring.charAt(i);
  10546. }
  10547. }
  10548. return tmp;
  10549. }
  10550. function readLocalOpts() {
  10551. var cookieOpt = me.getPreferences('autotypeset');
  10552. utils.extend(me.options.autotypeset, cookieOpt);
  10553. }
  10554. me.commands['autotypeset'] = {
  10555. execCommand: function () {
  10556. me.removeListener('beforepaste', autotype);
  10557. if (opt.pasteFilter) {
  10558. me.addListener('beforepaste', autotype);
  10559. }
  10560. autotype.call(me)
  10561. }
  10562. };
  10563. };
  10564. // plugins/autosubmit.js
  10565. /**
  10566. * 快捷键提交
  10567. * @file
  10568. * @since 1.2.6.1
  10569. */
  10570. /**
  10571. * 提交表单
  10572. * @command autosubmit
  10573. * @method execCommand
  10574. * @param { String } cmd 命令字符串
  10575. * @example
  10576. * ```javascript
  10577. * editor.execCommand( 'autosubmit' );
  10578. * ```
  10579. */
  10580. UE.plugin.register('autosubmit', function () {
  10581. return {
  10582. shortcutkey: {
  10583. "autosubmit": "ctrl+13" //手动提交
  10584. },
  10585. commands: {
  10586. 'autosubmit': {
  10587. execCommand: function () {
  10588. var me = this,
  10589. form = domUtils.findParentByTagName(me.iframe, "form", false);
  10590. if (form) {
  10591. if (me.fireEvent("beforesubmit") === false) {
  10592. return;
  10593. }
  10594. me.sync();
  10595. form.submit();
  10596. }
  10597. }
  10598. }
  10599. }
  10600. }
  10601. });
  10602. // plugins/background.js
  10603. /**
  10604. * 背景插件,为UEditor提供设置背景功能
  10605. * @file
  10606. * @since 1.2.6.1
  10607. */
  10608. UE.plugin.register('background', function () {
  10609. var me = this,
  10610. cssRuleId = 'editor_background',
  10611. isSetColored,
  10612. reg = new RegExp('body[\\s]*\\{(.+)\\}', 'i');
  10613. function stringToObj(str) {
  10614. var obj = {},
  10615. styles = str.split(';');
  10616. utils.each(styles, function (v) {
  10617. var index = v.indexOf(':'),
  10618. key = utils.trim(v.substr(0, index)).toLowerCase();
  10619. key && (obj[key] = utils.trim(v.substr(index + 1) || ''));
  10620. });
  10621. return obj;
  10622. }
  10623. function setBackground(obj) {
  10624. if (obj) {
  10625. var styles = [];
  10626. for (var name in obj) {
  10627. if (obj.hasOwnProperty(name)) {
  10628. styles.push(name + ":" + obj[name] + '; ');
  10629. }
  10630. }
  10631. utils.cssRule(cssRuleId, styles.length ? ('body{' + styles.join("") + '}') : '', me.document);
  10632. } else {
  10633. utils.cssRule(cssRuleId, '', me.document)
  10634. }
  10635. }
  10636. //重写editor.hasContent方法
  10637. var orgFn = me.hasContents;
  10638. me.hasContents = function () {
  10639. if (me.queryCommandValue('background')) {
  10640. return true
  10641. }
  10642. return orgFn.apply(me, arguments);
  10643. };
  10644. return {
  10645. bindEvents: {
  10646. 'getAllHtml': function (type, headHtml) {
  10647. var body = this.body,
  10648. su = domUtils.getComputedStyle(body, "background-image"),
  10649. url = "";
  10650. if (su.indexOf(me.options.imagePath) > 0) {
  10651. url = su.substring(su.indexOf(me.options.imagePath), su.length - 1).replace(/"|\(|\)/ig, "");
  10652. } else {
  10653. url = su != "none" ? su.replace(/url\("?|"?\)/ig, "") : "";
  10654. }
  10655. var html = '<style type="text/css">body{';
  10656. var bgObj = {
  10657. "background-color": domUtils.getComputedStyle(body, "background-color") || "#ffffff",
  10658. 'background-image': url ? 'url(' + url + ')' : '',
  10659. 'background-repeat': domUtils.getComputedStyle(body, "background-repeat") || "",
  10660. 'background-position': browser.ie ? (domUtils.getComputedStyle(body, "background-position-x") + " " + domUtils.getComputedStyle(body, "background-position-y")) : domUtils.getComputedStyle(body, "background-position"),
  10661. 'height': domUtils.getComputedStyle(body, "height")
  10662. };
  10663. for (var name in bgObj) {
  10664. if (bgObj.hasOwnProperty(name)) {
  10665. html += name + ":" + bgObj[name] + "; ";
  10666. }
  10667. }
  10668. html += '}</style> ';
  10669. headHtml.push(html);
  10670. },
  10671. 'aftersetcontent': function () {
  10672. if (isSetColored == false) setBackground();
  10673. }
  10674. },
  10675. inputRule: function (root) {
  10676. isSetColored = false;
  10677. utils.each(root.getNodesByTagName('p'), function (p) {
  10678. var styles = p.getAttr('data-background');
  10679. if (styles) {
  10680. isSetColored = true;
  10681. setBackground(stringToObj(styles));
  10682. p.parentNode.removeChild(p);
  10683. }
  10684. })
  10685. },
  10686. outputRule: function (root) {
  10687. var me = this,
  10688. styles = (utils.cssRule(cssRuleId, me.document) || '').replace(/[\n\r]+/g, '').match(reg);
  10689. if (styles) {
  10690. root.appendChild(UE.uNode.createElement('<p style="display:none;" data-background="' + utils.trim(styles[1].replace(/"/g, '').replace(/[\s]+/g, ' ')) + '"><br/></p>'));
  10691. }
  10692. },
  10693. commands: {
  10694. 'background': {
  10695. execCommand: function (cmd, obj) {
  10696. setBackground(obj);
  10697. },
  10698. queryCommandValue: function () {
  10699. var me = this,
  10700. styles = (utils.cssRule(cssRuleId, me.document) || '').replace(/[\n\r]+/g, '').match(reg);
  10701. return styles ? stringToObj(styles[1]) : null;
  10702. },
  10703. notNeedUndo: true
  10704. }
  10705. }
  10706. }
  10707. });
  10708. // plugins/image.js
  10709. /**
  10710. * 图片插入、排版插件
  10711. * @file
  10712. * @since 1.2.6.1
  10713. */
  10714. /**
  10715. * 图片对齐方式
  10716. * @command imagefloat
  10717. * @method execCommand
  10718. * @remind 值center为独占一行居中
  10719. * @param { String } cmd 命令字符串
  10720. * @param { String } align 对齐方式,可传left、right、none、center
  10721. * @remaind center表示图片独占一行
  10722. * @example
  10723. * ```javascript
  10724. * editor.execCommand( 'imagefloat', 'center' );
  10725. * ```
  10726. */
  10727. /**
  10728. * 如果选区所在位置是图片区域
  10729. * @command imagefloat
  10730. * @method queryCommandValue
  10731. * @param { String } cmd 命令字符串
  10732. * @return { String } 返回图片对齐方式
  10733. * @example
  10734. * ```javascript
  10735. * editor.queryCommandValue( 'imagefloat' );
  10736. * ```
  10737. */
  10738. UE.commands['imagefloat'] = {
  10739. execCommand: function (cmd, align) {
  10740. var me = this,
  10741. range = me.selection.getRange();
  10742. if (!range.collapsed) {
  10743. var img = range.getClosedNode();
  10744. if (img && img.tagName == 'IMG') {
  10745. switch (align) {
  10746. case 'left':
  10747. case 'right':
  10748. case 'none':
  10749. var pN = img.parentNode,
  10750. tmpNode, pre, next;
  10751. while (dtd.$inline[pN.tagName] || pN.tagName == 'A') {
  10752. pN = pN.parentNode;
  10753. }
  10754. tmpNode = pN;
  10755. if (tmpNode.tagName == 'P' && domUtils.getStyle(tmpNode, 'text-align') == 'center') {
  10756. if (!domUtils.isBody(tmpNode) && domUtils.getChildCount(tmpNode, function (node) {
  10757. return !domUtils.isBr(node) && !domUtils.isWhitespace(node);
  10758. }) == 1) {
  10759. pre = tmpNode.previousSibling;
  10760. next = tmpNode.nextSibling;
  10761. if (pre && next && pre.nodeType == 1 && next.nodeType == 1 && pre.tagName == next.tagName && domUtils.isBlockElm(pre)) {
  10762. pre.appendChild(tmpNode.firstChild);
  10763. while (next.firstChild) {
  10764. pre.appendChild(next.firstChild);
  10765. }
  10766. domUtils.remove(tmpNode);
  10767. domUtils.remove(next);
  10768. } else {
  10769. domUtils.setStyle(tmpNode, 'text-align', '');
  10770. }
  10771. }
  10772. range.selectNode(img).select();
  10773. }
  10774. domUtils.setStyle(img, 'float', align == 'none' ? '' : align);
  10775. if (align == 'none') {
  10776. domUtils.removeAttributes(img, 'align');
  10777. }
  10778. break;
  10779. case 'center':
  10780. if (me.queryCommandValue('imagefloat') != 'center') {
  10781. pN = img.parentNode;
  10782. domUtils.setStyle(img, 'float', '');
  10783. domUtils.removeAttributes(img, 'align');
  10784. tmpNode = img;
  10785. while (pN && domUtils.getChildCount(pN, function (node) {
  10786. return !domUtils.isBr(node) && !domUtils.isWhitespace(node);
  10787. }) == 1 &&
  10788. (dtd.$inline[pN.tagName] || pN.tagName == 'A')) {
  10789. tmpNode = pN;
  10790. pN = pN.parentNode;
  10791. }
  10792. range.setStartBefore(tmpNode).setCursor(false);
  10793. pN = me.document.createElement('div');
  10794. pN.appendChild(tmpNode);
  10795. domUtils.setStyle(tmpNode, 'float', '');
  10796. me.execCommand('insertHtml', '<p id="_img_parent_tmp" style="text-align:center">' + pN.innerHTML + '</p>');
  10797. tmpNode = me.document.getElementById('_img_parent_tmp');
  10798. tmpNode.removeAttribute('id');
  10799. tmpNode = tmpNode.firstChild;
  10800. range.selectNode(tmpNode).select();
  10801. //去掉后边多余的元素
  10802. next = tmpNode.parentNode.nextSibling;
  10803. if (next && domUtils.isEmptyNode(next)) {
  10804. domUtils.remove(next);
  10805. }
  10806. }
  10807. break;
  10808. }
  10809. }
  10810. }
  10811. },
  10812. queryCommandValue: function () {
  10813. var range = this.selection.getRange(),
  10814. startNode, floatStyle;
  10815. if (range.collapsed) {
  10816. return 'none';
  10817. }
  10818. startNode = range.getClosedNode();
  10819. if (startNode && startNode.nodeType == 1 && startNode.tagName == 'IMG') {
  10820. floatStyle = domUtils.getComputedStyle(startNode, 'float') || startNode.getAttribute('align');
  10821. if (floatStyle == 'none') {
  10822. floatStyle = domUtils.getComputedStyle(startNode.parentNode, 'text-align') == 'center' ? 'center' : floatStyle;
  10823. }
  10824. return {
  10825. left: 1,
  10826. right: 1,
  10827. center: 1
  10828. } [floatStyle] ? floatStyle: 'none';
  10829. }
  10830. return 'none';
  10831. },
  10832. queryCommandState: function () {
  10833. var range = this.selection.getRange(),
  10834. startNode;
  10835. if (range.collapsed) return -1;
  10836. startNode = range.getClosedNode();
  10837. if (startNode && startNode.nodeType == 1 && startNode.tagName == 'IMG') {
  10838. return 0;
  10839. }
  10840. return -1;
  10841. }
  10842. };
  10843. /**
  10844. * 插入图片
  10845. * @command insertimage
  10846. * @method execCommand
  10847. * @param { String } cmd 命令字符串
  10848. * @param { Object } opt 属性键值对,这些属性都将被复制到当前插入图片
  10849. * @remind 该命令第二个参数可接受一个图片配置项对象的数组,可以插入多张图片,
  10850. * 此时数组的每一个元素都是一个Object类型的图片属性集合。
  10851. * @example
  10852. * ```javascript
  10853. * editor.execCommand( 'insertimage', {
  10854. * src:'a/b/c.jpg',
  10855. * width:'100',
  10856. * height:'100'
  10857. * } );
  10858. * ```
  10859. * @example
  10860. * ```javascript
  10861. * editor.execCommand( 'insertimage', [{
  10862. * src:'a/b/c.jpg',
  10863. * width:'100',
  10864. * height:'100'
  10865. * },{
  10866. * src:'a/b/d.jpg',
  10867. * width:'100',
  10868. * height:'100'
  10869. * }] );
  10870. * ```
  10871. */
  10872. UE.commands['insertimage'] = {
  10873. execCommand: function (cmd, opt) {
  10874. opt = utils.isArray(opt) ? opt : [opt];
  10875. if (!opt.length) {
  10876. return;
  10877. }
  10878. var me = this,
  10879. range = me.selection.getRange(),
  10880. img = range.getClosedNode();
  10881. if (me.fireEvent('beforeinsertimage', opt) === true) {
  10882. return;
  10883. }
  10884. function unhtmlData(imgCi) {
  10885. utils.each('width,height,border,hspace,vspace'.split(','), function (item) {
  10886. if (imgCi[item]) {
  10887. imgCi[item] = parseInt(imgCi[item], 10) || 0;
  10888. }
  10889. });
  10890. utils.each('src,_src'.split(','), function (item) {
  10891. if (imgCi[item]) {
  10892. imgCi[item] = utils.unhtmlForUrl(imgCi[item]);
  10893. }
  10894. });
  10895. utils.each('title,alt'.split(','), function (item) {
  10896. if (imgCi[item]) {
  10897. imgCi[item] = utils.unhtml(imgCi[item]);
  10898. }
  10899. });
  10900. }
  10901. if (img && /img/i.test(img.tagName) && (img.className != "edui-faked-video" || img.className.indexOf("edui-upload-video") != -1) && !img.getAttribute("word_img")) {
  10902. var first = opt.shift();
  10903. var floatStyle = first['floatStyle'];
  10904. delete first['floatStyle'];
  10905. //// img.style.border = (first.border||0) +"px solid #000";
  10906. //// img.style.margin = (first.margin||0) +"px";
  10907. // img.style.cssText += ';margin:' + (first.margin||0) +"px;" + 'border:' + (first.border||0) +"px solid #000";
  10908. domUtils.setAttributes(img, first);
  10909. me.execCommand('imagefloat', floatStyle);
  10910. if (opt.length > 0) {
  10911. range.setStartAfter(img).setCursor(false, true);
  10912. me.execCommand('insertimage', opt);
  10913. }
  10914. } else {
  10915. var html = [],
  10916. str = '',
  10917. ci;
  10918. ci = opt[0];
  10919. if (opt.length == 1) {
  10920. unhtmlData(ci);
  10921. str = '<img src="' + ci.src + '" ' + (ci._src ? ' _src="' + ci._src + '" ' : '') +
  10922. (ci.width ? 'width="' + ci.width + '" ' : '') +
  10923. (ci.height ? ' height="' + ci.height + '" ' : '') +
  10924. (ci['floatStyle'] == 'left' || ci['floatStyle'] == 'right' ? ' style="float:' + ci['floatStyle'] + ';"' : '') +
  10925. (ci.title && ci.title != "" ? ' title="' + ci.title + '"' : '') +
  10926. (ci.border && ci.border != "0" ? ' border="' + ci.border + '"' : '') +
  10927. (ci.alt && ci.alt != "" ? ' alt="' + ci.alt + '"' : '') +
  10928. (ci.hspace && ci.hspace != "0" ? ' hspace = "' + ci.hspace + '"' : '') +
  10929. (ci.vspace && ci.vspace != "0" ? ' vspace = "' + ci.vspace + '"' : '') + '/>';
  10930. if (ci['floatStyle'] == 'center') {
  10931. str = '<p style="text-align: center">' + str + '</p>';
  10932. }
  10933. html.push(str);
  10934. } else {
  10935. for (var i = 0; ci = opt[i++];) {
  10936. unhtmlData(ci);
  10937. str = '<p ' + (ci['floatStyle'] == 'center' ? 'style="text-align: center" ' : '') + '><img src="' + ci.src + '" ' +
  10938. (ci.width ? 'width="' + ci.width + '" ' : '') + (ci._src ? ' _src="' + ci._src + '" ' : '') +
  10939. (ci.height ? ' height="' + ci.height + '" ' : '') +
  10940. ' style="' + (ci['floatStyle'] && ci['floatStyle'] != 'center' ? 'float:' + ci['floatStyle'] + ';' : '') +
  10941. (ci.border || '') + '" ' +
  10942. (ci.title ? ' title="' + ci.title + '"' : '') + ' /></p>';
  10943. html.push(str);
  10944. }
  10945. }
  10946. me.execCommand('insertHtml', html.join(''));
  10947. }
  10948. me.fireEvent('afterinsertimage', opt)
  10949. }
  10950. };
  10951. // plugins/justify.js
  10952. /**
  10953. * 段落格式
  10954. * @file
  10955. * @since 1.2.6.1
  10956. */
  10957. /**
  10958. * 段落对齐方式
  10959. * @command justify
  10960. * @method execCommand
  10961. * @param { String } cmd 命令字符串
  10962. * @param { String } align 对齐方式:left => 居左,right => 居右,center => 居中,justify => 两端对齐
  10963. * @example
  10964. * ```javascript
  10965. * editor.execCommand( 'justify', 'center' );
  10966. * ```
  10967. */
  10968. /**
  10969. * 如果选区所在位置是段落区域,返回当前段落对齐方式
  10970. * @command justify
  10971. * @method queryCommandValue
  10972. * @param { String } cmd 命令字符串
  10973. * @return { String } 返回段落对齐方式
  10974. * @example
  10975. * ```javascript
  10976. * editor.queryCommandValue( 'justify' );
  10977. * ```
  10978. */
  10979. UE.plugins['justify'] = function () {
  10980. var me = this,
  10981. block = domUtils.isBlockElm,
  10982. defaultValue = {
  10983. left: 1,
  10984. right: 1,
  10985. center: 1,
  10986. justify: 1
  10987. },
  10988. doJustify = function (range, style) {
  10989. var bookmark = range.createBookmark(),
  10990. filterFn = function (node) {
  10991. return node.nodeType == 1 ? node.tagName.toLowerCase() != 'br' && !domUtils.isBookmarkNode(node) : !domUtils.isWhitespace(node);
  10992. };
  10993. range.enlarge(true);
  10994. var bookmark2 = range.createBookmark(),
  10995. current = domUtils.getNextDomNode(bookmark2.start, false, filterFn),
  10996. tmpRange = range.cloneRange(),
  10997. tmpNode;
  10998. while (current && !(domUtils.getPosition(current, bookmark2.end) & domUtils.POSITION_FOLLOWING)) {
  10999. if (current.nodeType == 3 || !block(current)) {
  11000. tmpRange.setStartBefore(current);
  11001. while (current && current !== bookmark2.end && !block(current)) {
  11002. tmpNode = current;
  11003. current = domUtils.getNextDomNode(current, false, null, function (node) {
  11004. return !block(node);
  11005. });
  11006. }
  11007. tmpRange.setEndAfter(tmpNode);
  11008. var common = tmpRange.getCommonAncestor();
  11009. if (!domUtils.isBody(common) && block(common)) {
  11010. domUtils.setStyles(common, utils.isString(style) ? {
  11011. 'text-align': style
  11012. } : style);
  11013. current = common;
  11014. } else {
  11015. var p = range.document.createElement('p');
  11016. domUtils.setStyles(p, utils.isString(style) ? {
  11017. 'text-align': style
  11018. } : style);
  11019. var frag = tmpRange.extractContents();
  11020. p.appendChild(frag);
  11021. tmpRange.insertNode(p);
  11022. current = p;
  11023. }
  11024. current = domUtils.getNextDomNode(current, false, filterFn);
  11025. } else {
  11026. current = domUtils.getNextDomNode(current, true, filterFn);
  11027. }
  11028. }
  11029. return range.moveToBookmark(bookmark2).moveToBookmark(bookmark);
  11030. };
  11031. UE.commands['justify'] = {
  11032. execCommand: function (cmdName, align) {
  11033. var range = this.selection.getRange(),
  11034. txt;
  11035. //闭合时单独处理
  11036. if (range.collapsed) {
  11037. txt = this.document.createTextNode('p');
  11038. range.insertNode(txt);
  11039. }
  11040. doJustify(range, align);
  11041. if (txt) {
  11042. range.setStartBefore(txt).collapse(true);
  11043. domUtils.remove(txt);
  11044. }
  11045. range.select();
  11046. return true;
  11047. },
  11048. queryCommandValue: function () {
  11049. var startNode = this.selection.getStart(),
  11050. value = domUtils.getComputedStyle(startNode, 'text-align');
  11051. return defaultValue[value] ? value : 'left';
  11052. },
  11053. queryCommandState: function () {
  11054. var start = this.selection.getStart(),
  11055. cell = start && domUtils.findParentByTagName(start, ["td", "th", "caption"], true);
  11056. return cell ? -1 : 0;
  11057. }
  11058. };
  11059. };
  11060. // plugins/font.js
  11061. /**
  11062. * 字体颜色,背景色,字号,字体,下划线,删除线
  11063. * @file
  11064. * @since 1.2.6.1
  11065. */
  11066. /**
  11067. * 字体颜色
  11068. * @command forecolor
  11069. * @method execCommand
  11070. * @param { String } cmd 命令字符串
  11071. * @param { String } value 色值(必须十六进制)
  11072. * @example
  11073. * ```javascript
  11074. * editor.execCommand( 'forecolor', '#000' );
  11075. * ```
  11076. */
  11077. /**
  11078. * 返回选区字体颜色
  11079. * @command forecolor
  11080. * @method queryCommandValue
  11081. * @param { String } cmd 命令字符串
  11082. * @return { String } 返回字体颜色
  11083. * @example
  11084. * ```javascript
  11085. * editor.queryCommandValue( 'forecolor' );
  11086. * ```
  11087. */
  11088. /**
  11089. * 字体背景颜色
  11090. * @command backcolor
  11091. * @method execCommand
  11092. * @param { String } cmd 命令字符串
  11093. * @param { String } value 色值(必须十六进制)
  11094. * @example
  11095. * ```javascript
  11096. * editor.execCommand( 'backcolor', '#000' );
  11097. * ```
  11098. */
  11099. /**
  11100. * 返回选区字体颜色
  11101. * @command backcolor
  11102. * @method queryCommandValue
  11103. * @param { String } cmd 命令字符串
  11104. * @return { String } 返回字体背景颜色
  11105. * @example
  11106. * ```javascript
  11107. * editor.queryCommandValue( 'backcolor' );
  11108. * ```
  11109. */
  11110. /**
  11111. * 字体大小
  11112. * @command fontsize
  11113. * @method execCommand
  11114. * @param { String } cmd 命令字符串
  11115. * @param { String } value 字体大小
  11116. * @example
  11117. * ```javascript
  11118. * editor.execCommand( 'fontsize', '14px' );
  11119. * ```
  11120. */
  11121. /**
  11122. * 返回选区字体大小
  11123. * @command fontsize
  11124. * @method queryCommandValue
  11125. * @param { String } cmd 命令字符串
  11126. * @return { String } 返回字体大小
  11127. * @example
  11128. * ```javascript
  11129. * editor.queryCommandValue( 'fontsize' );
  11130. * ```
  11131. */
  11132. /**
  11133. * 字体样式
  11134. * @command fontfamily
  11135. * @method execCommand
  11136. * @param { String } cmd 命令字符串
  11137. * @param { String } value 字体样式
  11138. * @example
  11139. * ```javascript
  11140. * editor.execCommand( 'fontfamily', '微软雅黑' );
  11141. * ```
  11142. */
  11143. /**
  11144. * 返回选区字体样式
  11145. * @command fontfamily
  11146. * @method queryCommandValue
  11147. * @param { String } cmd 命令字符串
  11148. * @return { String } 返回字体样式
  11149. * @example
  11150. * ```javascript
  11151. * editor.queryCommandValue( 'fontfamily' );
  11152. * ```
  11153. */
  11154. /**
  11155. * 字体下划线,与删除线互斥
  11156. * @command underline
  11157. * @method execCommand
  11158. * @param { String } cmd 命令字符串
  11159. * @example
  11160. * ```javascript
  11161. * editor.execCommand( 'underline' );
  11162. * ```
  11163. */
  11164. /**
  11165. * 字体删除线,与下划线互斥
  11166. * @command strikethrough
  11167. * @method execCommand
  11168. * @param { String } cmd 命令字符串
  11169. * @example
  11170. * ```javascript
  11171. * editor.execCommand( 'strikethrough' );
  11172. * ```
  11173. */
  11174. /**
  11175. * 字体边框
  11176. * @command fontborder
  11177. * @method execCommand
  11178. * @param { String } cmd 命令字符串
  11179. * @example
  11180. * ```javascript
  11181. * editor.execCommand( 'fontborder' );
  11182. * ```
  11183. */
  11184. UE.plugins['font'] = function () {
  11185. var me = this,
  11186. fonts = {
  11187. 'forecolor': 'color',
  11188. 'backcolor': 'background-color',
  11189. 'fontsize': 'font-size',
  11190. 'fontfamily': 'font-family',
  11191. 'underline': 'text-decoration',
  11192. 'strikethrough': 'text-decoration',
  11193. 'fontborder': 'border'
  11194. },
  11195. needCmd = {
  11196. 'underline': 1,
  11197. 'strikethrough': 1,
  11198. 'fontborder': 1
  11199. },
  11200. needSetChild = {
  11201. 'forecolor': 'color',
  11202. 'backcolor': 'background-color',
  11203. 'fontsize': 'font-size',
  11204. 'fontfamily': 'font-family'
  11205. };
  11206. me.setOpt({
  11207. 'fontfamily': [{
  11208. name: 'songti',
  11209. val: '宋体,SimSun'
  11210. },
  11211. {
  11212. name: 'yahei',
  11213. val: '微软雅黑,Microsoft YaHei'
  11214. },
  11215. {
  11216. name: 'kaiti',
  11217. val: '楷体,楷体_GB2312, SimKai'
  11218. },
  11219. {
  11220. name: 'heiti',
  11221. val: '黑体, SimHei'
  11222. },
  11223. {
  11224. name: 'lishu',
  11225. val: '隶书, SimLi'
  11226. },
  11227. {
  11228. name: 'andaleMono',
  11229. val: 'andale mono'
  11230. },
  11231. {
  11232. name: 'arial',
  11233. val: 'arial, helvetica,sans-serif'
  11234. },
  11235. {
  11236. name: 'arialBlack',
  11237. val: 'arial black,avant garde'
  11238. },
  11239. {
  11240. name: 'comicSansMs',
  11241. val: 'comic sans ms'
  11242. },
  11243. {
  11244. name: 'impact',
  11245. val: 'impact,chicago'
  11246. },
  11247. {
  11248. name: 'timesNewRoman',
  11249. val: 'times new roman'
  11250. }
  11251. ],
  11252. 'fontsize': [10, 11, 12, 14, 16, 18, 20, 24, 36]
  11253. });
  11254. function mergeWithParent(node) {
  11255. var parent;
  11256. while (parent = node.parentNode) {
  11257. if (parent.tagName == 'SPAN' && domUtils.getChildCount(parent, function (child) {
  11258. return !domUtils.isBookmarkNode(child) && !domUtils.isBr(child)
  11259. }) == 1) {
  11260. parent.style.cssText += node.style.cssText;
  11261. domUtils.remove(node, true);
  11262. node = parent;
  11263. } else {
  11264. break;
  11265. }
  11266. }
  11267. }
  11268. function mergeChild(rng, cmdName, value) {
  11269. if (needSetChild[cmdName]) {
  11270. rng.adjustmentBoundary();
  11271. if (!rng.collapsed && rng.startContainer.nodeType == 1) {
  11272. var start = rng.startContainer.childNodes[rng.startOffset];
  11273. if (start && domUtils.isTagNode(start, 'span')) {
  11274. var bk = rng.createBookmark();
  11275. utils.each(domUtils.getElementsByTagName(start, 'span'), function (span) {
  11276. if (!span.parentNode || domUtils.isBookmarkNode(span)) return;
  11277. if (cmdName == 'backcolor' && domUtils.getComputedStyle(span, 'background-color').toLowerCase() === value) {
  11278. return;
  11279. }
  11280. domUtils.removeStyle(span, needSetChild[cmdName]);
  11281. if (span.style.cssText.replace(/^\s+$/, '').length == 0) {
  11282. domUtils.remove(span, true)
  11283. }
  11284. });
  11285. rng.moveToBookmark(bk)
  11286. }
  11287. }
  11288. }
  11289. }
  11290. function mergesibling(rng, cmdName, value) {
  11291. var collapsed = rng.collapsed,
  11292. bk = rng.createBookmark(),
  11293. common;
  11294. if (collapsed) {
  11295. common = bk.start.parentNode;
  11296. while (dtd.$inline[common.tagName]) {
  11297. common = common.parentNode;
  11298. }
  11299. } else {
  11300. common = domUtils.getCommonAncestor(bk.start, bk.end);
  11301. }
  11302. utils.each(domUtils.getElementsByTagName(common, 'span'), function (span) {
  11303. if (!span.parentNode || domUtils.isBookmarkNode(span)) return;
  11304. if (/\s*border\s*:\s*none;?\s*/i.test(span.style.cssText)) {
  11305. if (/^\s*border\s*:\s*none;?\s*$/.test(span.style.cssText)) {
  11306. domUtils.remove(span, true);
  11307. } else {
  11308. domUtils.removeStyle(span, 'border');
  11309. }
  11310. return
  11311. }
  11312. if (/border/i.test(span.style.cssText) && span.parentNode.tagName == 'SPAN' && /border/i.test(span.parentNode.style.cssText)) {
  11313. span.style.cssText = span.style.cssText.replace(/border[^:]*:[^;]+;?/gi, '');
  11314. }
  11315. if (!(cmdName == 'fontborder' && value == 'none')) {
  11316. var next = span.nextSibling;
  11317. while (next && next.nodeType == 1 && next.tagName == 'SPAN') {
  11318. if (domUtils.isBookmarkNode(next) && cmdName == 'fontborder') {
  11319. span.appendChild(next);
  11320. next = span.nextSibling;
  11321. continue;
  11322. }
  11323. if (next.style.cssText == span.style.cssText) {
  11324. domUtils.moveChild(next, span);
  11325. domUtils.remove(next);
  11326. }
  11327. if (span.nextSibling === next)
  11328. break;
  11329. next = span.nextSibling;
  11330. }
  11331. }
  11332. mergeWithParent(span);
  11333. if (browser.ie && browser.version > 8) {
  11334. //拷贝父亲们的特别的属性,这里只做背景颜色的处理
  11335. var parent = domUtils.findParent(span, function (n) {
  11336. return n.tagName == 'SPAN' && /background-color/.test(n.style.cssText)
  11337. });
  11338. if (parent && !/background-color/.test(span.style.cssText)) {
  11339. span.style.backgroundColor = parent.style.backgroundColor;
  11340. }
  11341. }
  11342. });
  11343. rng.moveToBookmark(bk);
  11344. mergeChild(rng, cmdName, value)
  11345. }
  11346. me.addInputRule(function (root) {
  11347. utils.each(root.getNodesByTagName('u s del font strike'), function (node) {
  11348. if (node.tagName == 'font') {
  11349. var cssStyle = [];
  11350. for (var p in node.attrs) {
  11351. switch (p) {
  11352. case 'size':
  11353. cssStyle.push('font-size:' +
  11354. ({
  11355. '1': '10',
  11356. '2': '12',
  11357. '3': '16',
  11358. '4': '18',
  11359. '5': '24',
  11360. '6': '32',
  11361. '7': '48'
  11362. } [node.attrs[p]] || node.attrs[p]) + 'px');
  11363. break;
  11364. case 'color':
  11365. cssStyle.push('color:' + node.attrs[p]);
  11366. break;
  11367. case 'face':
  11368. cssStyle.push('font-family:' + node.attrs[p]);
  11369. break;
  11370. case 'style':
  11371. cssStyle.push(node.attrs[p]);
  11372. }
  11373. }
  11374. node.attrs = {
  11375. 'style': cssStyle.join(';')
  11376. };
  11377. } else {
  11378. var val = node.tagName == 'u' ? 'underline' : 'line-through';
  11379. node.attrs = {
  11380. 'style': (node.getAttr('style') || '') + 'text-decoration:' + val + ';'
  11381. }
  11382. }
  11383. node.tagName = 'span';
  11384. });
  11385. // utils.each(root.getNodesByTagName('span'), function (node) {
  11386. // var val;
  11387. // if(val = node.getAttr('class')){
  11388. // if(/fontstrikethrough/.test(val)){
  11389. // node.setStyle('text-decoration','line-through');
  11390. // if(node.attrs['class']){
  11391. // node.attrs['class'] = node.attrs['class'].replace(/fontstrikethrough/,'');
  11392. // }else{
  11393. // node.setAttr('class')
  11394. // }
  11395. // }
  11396. // if(/fontborder/.test(val)){
  11397. // node.setStyle('border','1px solid #000');
  11398. // if(node.attrs['class']){
  11399. // node.attrs['class'] = node.attrs['class'].replace(/fontborder/,'');
  11400. // }else{
  11401. // node.setAttr('class')
  11402. // }
  11403. // }
  11404. // }
  11405. // });
  11406. });
  11407. // me.addOutputRule(function(root){
  11408. // utils.each(root.getNodesByTagName('span'), function (node) {
  11409. // var val;
  11410. // if(val = node.getStyle('text-decoration')){
  11411. // if(/line-through/.test(val)){
  11412. // if(node.attrs['class']){
  11413. // node.attrs['class'] += ' fontstrikethrough';
  11414. // }else{
  11415. // node.setAttr('class','fontstrikethrough')
  11416. // }
  11417. // }
  11418. //
  11419. // node.setStyle('text-decoration')
  11420. // }
  11421. // if(val = node.getStyle('border')){
  11422. // if(/1px/.test(val) && /solid/.test(val)){
  11423. // if(node.attrs['class']){
  11424. // node.attrs['class'] += ' fontborder';
  11425. //
  11426. // }else{
  11427. // node.setAttr('class','fontborder')
  11428. // }
  11429. // }
  11430. // node.setStyle('border')
  11431. //
  11432. // }
  11433. // });
  11434. // });
  11435. for (var p in fonts) {
  11436. (function (cmd, style) {
  11437. UE.commands[cmd] = {
  11438. execCommand: function (cmdName, value) {
  11439. value = value || (this.queryCommandState(cmdName) ? 'none' : cmdName == 'underline' ? 'underline' :
  11440. cmdName == 'fontborder' ? '1px solid #000' :
  11441. 'line-through');
  11442. var me = this,
  11443. range = this.selection.getRange(),
  11444. text;
  11445. if (value == 'default') {
  11446. if (range.collapsed) {
  11447. text = me.document.createTextNode('font');
  11448. range.insertNode(text).select();
  11449. }
  11450. me.execCommand('removeFormat', 'span,a', style);
  11451. if (text) {
  11452. range.setStartBefore(text).collapse(true);
  11453. domUtils.remove(text);
  11454. }
  11455. mergesibling(range, cmdName, value);
  11456. range.select()
  11457. } else {
  11458. if (!range.collapsed) {
  11459. if (needCmd[cmd] && me.queryCommandValue(cmd)) {
  11460. me.execCommand('removeFormat', 'span,a', style);
  11461. }
  11462. range = me.selection.getRange();
  11463. range.applyInlineStyle('span', {
  11464. 'style': style + ':' + value
  11465. });
  11466. mergesibling(range, cmdName, value);
  11467. range.select();
  11468. } else {
  11469. var span = domUtils.findParentByTagName(range.startContainer, 'span', true);
  11470. text = me.document.createTextNode('font');
  11471. if (span && !span.children.length && !span[browser.ie ? 'innerText' : 'textContent'].replace(fillCharReg, '').length) {
  11472. //for ie hack when enter
  11473. range.insertNode(text);
  11474. if (needCmd[cmd]) {
  11475. range.selectNode(text).select();
  11476. me.execCommand('removeFormat', 'span,a', style, null);
  11477. span = domUtils.findParentByTagName(text, 'span', true);
  11478. range.setStartBefore(text);
  11479. }
  11480. span && (span.style.cssText += ';' + style + ':' + value);
  11481. range.collapse(true).select();
  11482. } else {
  11483. range.insertNode(text);
  11484. range.selectNode(text).select();
  11485. span = range.document.createElement('span');
  11486. if (needCmd[cmd]) {
  11487. //a标签内的不处理跳过
  11488. if (domUtils.findParentByTagName(text, 'a', true)) {
  11489. range.setStartBefore(text).setCursor();
  11490. domUtils.remove(text);
  11491. return;
  11492. }
  11493. me.execCommand('removeFormat', 'span,a', style);
  11494. }
  11495. span.style.cssText = style + ':' + value;
  11496. text.parentNode.insertBefore(span, text);
  11497. //修复,span套span 但样式不继承的问题
  11498. if (!browser.ie || browser.ie && browser.version == 9) {
  11499. var spanParent = span.parentNode;
  11500. while (!domUtils.isBlockElm(spanParent)) {
  11501. if (spanParent.tagName == 'SPAN') {
  11502. //opera合并style不会加入";"
  11503. span.style.cssText = spanParent.style.cssText + ";" + span.style.cssText;
  11504. }
  11505. spanParent = spanParent.parentNode;
  11506. }
  11507. }
  11508. if (opera) {
  11509. setTimeout(function () {
  11510. range.setStart(span, 0).collapse(true);
  11511. mergesibling(range, cmdName, value);
  11512. range.select();
  11513. });
  11514. } else {
  11515. range.setStart(span, 0).collapse(true);
  11516. mergesibling(range, cmdName, value);
  11517. range.select();
  11518. }
  11519. //trace:981
  11520. //domUtils.mergeToParent(span)
  11521. }
  11522. domUtils.remove(text);
  11523. }
  11524. }
  11525. return true;
  11526. },
  11527. queryCommandValue: function (cmdName) {
  11528. var startNode = this.selection.getStart();
  11529. //trace:946
  11530. if (cmdName == 'underline' || cmdName == 'strikethrough') {
  11531. var tmpNode = startNode,
  11532. value;
  11533. while (tmpNode && !domUtils.isBlockElm(tmpNode) && !domUtils.isBody(tmpNode)) {
  11534. if (tmpNode.nodeType == 1) {
  11535. value = domUtils.getComputedStyle(tmpNode, style);
  11536. if (value != 'none') {
  11537. return value;
  11538. }
  11539. }
  11540. tmpNode = tmpNode.parentNode;
  11541. }
  11542. return 'none';
  11543. }
  11544. if (cmdName == 'fontborder') {
  11545. var tmp = startNode,
  11546. val;
  11547. while (tmp && dtd.$inline[tmp.tagName]) {
  11548. if (val = domUtils.getComputedStyle(tmp, 'border')) {
  11549. if (/1px/.test(val) && /solid/.test(val)) {
  11550. return val;
  11551. }
  11552. }
  11553. tmp = tmp.parentNode;
  11554. }
  11555. return ''
  11556. }
  11557. if (cmdName == 'FontSize') {
  11558. var styleVal = domUtils.getComputedStyle(startNode, style),
  11559. tmp = /^([\d\.]+)(\w+)$/.exec(styleVal);
  11560. if (tmp) {
  11561. return Math.floor(tmp[1]) + tmp[2];
  11562. }
  11563. return styleVal;
  11564. }
  11565. return domUtils.getComputedStyle(startNode, style);
  11566. },
  11567. queryCommandState: function (cmdName) {
  11568. if (!needCmd[cmdName])
  11569. return 0;
  11570. var val = this.queryCommandValue(cmdName);
  11571. if (cmdName == 'fontborder') {
  11572. return /1px/.test(val) && /solid/.test(val)
  11573. } else {
  11574. return cmdName == 'underline' ? /underline/.test(val) : /line\-through/.test(val);
  11575. }
  11576. }
  11577. };
  11578. })(p, fonts[p]);
  11579. }
  11580. };
  11581. // plugins/link.js
  11582. /**
  11583. * 超链接
  11584. * @file
  11585. * @since 1.2.6.1
  11586. */
  11587. /**
  11588. * 插入超链接
  11589. * @command link
  11590. * @method execCommand
  11591. * @param { String } cmd 命令字符串
  11592. * @param { Object } options 设置自定义属性,例如:url、title、target
  11593. * @example
  11594. * ```javascript
  11595. * editor.execCommand( 'link', '{
  11596. * url:'ueditor.baidu.com',
  11597. * title:'ueditor',
  11598. * target:'_blank'
  11599. * }' );
  11600. * ```
  11601. */
  11602. /**
  11603. * 返回当前选中的第一个超链接节点
  11604. * @command link
  11605. * @method queryCommandValue
  11606. * @param { String } cmd 命令字符串
  11607. * @return { Element } 超链接节点
  11608. * @example
  11609. * ```javascript
  11610. * editor.queryCommandValue( 'link' );
  11611. * ```
  11612. */
  11613. /**
  11614. * 取消超链接
  11615. * @command unlink
  11616. * @method execCommand
  11617. * @param { String } cmd 命令字符串
  11618. * @example
  11619. * ```javascript
  11620. * editor.execCommand( 'unlink');
  11621. * ```
  11622. */
  11623. UE.plugins['link'] = function () {
  11624. function optimize(range) {
  11625. var start = range.startContainer,
  11626. end = range.endContainer;
  11627. if (start = domUtils.findParentByTagName(start, 'a', true)) {
  11628. range.setStartBefore(start);
  11629. }
  11630. if (end = domUtils.findParentByTagName(end, 'a', true)) {
  11631. range.setEndAfter(end);
  11632. }
  11633. }
  11634. UE.commands['unlink'] = {
  11635. execCommand: function () {
  11636. var range = this.selection.getRange(),
  11637. bookmark;
  11638. if (range.collapsed && !domUtils.findParentByTagName(range.startContainer, 'a', true)) {
  11639. return;
  11640. }
  11641. bookmark = range.createBookmark();
  11642. optimize(range);
  11643. range.removeInlineStyle('a').moveToBookmark(bookmark).select();
  11644. },
  11645. queryCommandState: function () {
  11646. return !this.highlight && this.queryCommandValue('link') ? 0 : -1;
  11647. }
  11648. };
  11649. function doLink(range, opt, me) {
  11650. var rngClone = range.cloneRange(),
  11651. link = me.queryCommandValue('link');
  11652. optimize(range = range.adjustmentBoundary());
  11653. var start = range.startContainer;
  11654. if (start.nodeType == 1 && link) {
  11655. start = start.childNodes[range.startOffset];
  11656. if (start && start.nodeType == 1 && start.tagName == 'A' && /^(?:https?|ftp|file)\s*:\s*\/\//.test(start[browser.ie ? 'innerText' : 'textContent'])) {
  11657. start[browser.ie ? 'innerText' : 'textContent'] = utils.html(opt.textValue || opt.href);
  11658. }
  11659. }
  11660. if (!rngClone.collapsed || link) {
  11661. range.removeInlineStyle('a');
  11662. rngClone = range.cloneRange();
  11663. }
  11664. if (rngClone.collapsed) {
  11665. var a = range.document.createElement('a'),
  11666. text = '';
  11667. if (opt.textValue) {
  11668. text = utils.html(opt.textValue);
  11669. delete opt.textValue;
  11670. } else {
  11671. text = utils.html(opt.href);
  11672. }
  11673. domUtils.setAttributes(a, opt);
  11674. start = domUtils.findParentByTagName(rngClone.startContainer, 'a', true);
  11675. if (start && domUtils.isInNodeEndBoundary(rngClone, start)) {
  11676. range.setStartAfter(start).collapse(true);
  11677. }
  11678. a[browser.ie ? 'innerText' : 'textContent'] = text;
  11679. range.insertNode(a).selectNode(a);
  11680. } else {
  11681. range.applyInlineStyle('a', opt);
  11682. }
  11683. }
  11684. UE.commands['link'] = {
  11685. execCommand: function (cmdName, opt) {
  11686. var range;
  11687. opt._href && (opt._href = utils.unhtml(opt._href, /[<">]/g));
  11688. opt.href && (opt.href = utils.unhtml(opt.href, /[<">]/g));
  11689. opt.textValue && (opt.textValue = utils.unhtml(opt.textValue, /[<">]/g));
  11690. doLink(range = this.selection.getRange(), opt, this);
  11691. //闭合都不加占位符,如果加了会在a后边多个占位符节点,导致a是图片背景组成的列表,出现空白问题
  11692. range.collapse().select(true);
  11693. },
  11694. queryCommandValue: function () {
  11695. var range = this.selection.getRange(),
  11696. node;
  11697. if (range.collapsed) {
  11698. // node = this.selection.getStart();
  11699. //在ie下getstart()取值偏上了
  11700. node = range.startContainer;
  11701. node = node.nodeType == 1 ? node : node.parentNode;
  11702. if (node && (node = domUtils.findParentByTagName(node, 'a', true)) && !domUtils.isInNodeEndBoundary(range, node)) {
  11703. return node;
  11704. }
  11705. } else {
  11706. //trace:1111 如果是<p><a>xx</a></p> startContainer是p就会找不到a
  11707. range.shrinkBoundary();
  11708. var start = range.startContainer.nodeType == 3 || !range.startContainer.childNodes[range.startOffset] ? range.startContainer : range.startContainer.childNodes[range.startOffset],
  11709. end = range.endContainer.nodeType == 3 || range.endOffset == 0 ? range.endContainer : range.endContainer.childNodes[range.endOffset - 1],
  11710. common = range.getCommonAncestor();
  11711. node = domUtils.findParentByTagName(common, 'a', true);
  11712. if (!node && common.nodeType == 1) {
  11713. var as = common.getElementsByTagName('a'),
  11714. ps, pe;
  11715. for (var i = 0, ci; ci = as[i++];) {
  11716. ps = domUtils.getPosition(ci, start), pe = domUtils.getPosition(ci, end);
  11717. if ((ps & domUtils.POSITION_FOLLOWING || ps & domUtils.POSITION_CONTAINS) &&
  11718. (pe & domUtils.POSITION_PRECEDING || pe & domUtils.POSITION_CONTAINS)
  11719. ) {
  11720. node = ci;
  11721. break;
  11722. }
  11723. }
  11724. }
  11725. return node;
  11726. }
  11727. },
  11728. queryCommandState: function () {
  11729. //判断如果是视频的话连接不可用
  11730. //fix 853
  11731. var img = this.selection.getRange().getClosedNode(),
  11732. flag = img && (img.className == "edui-faked-video" || img.className.indexOf("edui-upload-video") != -1);
  11733. return flag ? -1 : 0;
  11734. }
  11735. };
  11736. };
  11737. // plugins/iframe.js
  11738. ///import core
  11739. ///import plugins\inserthtml.js
  11740. ///commands 插入框架
  11741. ///commandsName InsertFrame
  11742. ///commandsTitle 插入Iframe
  11743. ///commandsDialog dialogs\insertframe
  11744. UE.plugins['insertframe'] = function () {
  11745. var me = this;
  11746. function deleteIframe() {
  11747. me._iframe && delete me._iframe;
  11748. }
  11749. me.addListener("selectionchange", function () {
  11750. deleteIframe();
  11751. });
  11752. };
  11753. // plugins/scrawl.js
  11754. ///import core
  11755. ///commands 涂鸦
  11756. ///commandsName Scrawl
  11757. ///commandsTitle 涂鸦
  11758. ///commandsDialog dialogs\scrawl
  11759. UE.commands['scrawl'] = {
  11760. queryCommandState: function () {
  11761. return (browser.ie && browser.version <= 8) ? -1 : 0;
  11762. }
  11763. };
  11764. // plugins/removeformat.js
  11765. /**
  11766. * 清除格式
  11767. * @file
  11768. * @since 1.2.6.1
  11769. */
  11770. /**
  11771. * 清除文字样式
  11772. * @command removeformat
  11773. * @method execCommand
  11774. * @param { String } cmd 命令字符串
  11775. * @param {String} tags 以逗号隔开的标签。如:strong
  11776. * @param {String} style 样式如:color
  11777. * @param {String} attrs 属性如:width
  11778. * @example
  11779. * ```javascript
  11780. * editor.execCommand( 'removeformat', 'strong','color','width' );
  11781. * ```
  11782. */
  11783. UE.plugins['removeformat'] = function () {
  11784. var me = this;
  11785. me.setOpt({
  11786. 'removeFormatTags': 'b,big,code,del,dfn,em,font,i,ins,kbd,q,samp,small,span,strike,strong,sub,sup,tt,u,var',
  11787. 'removeFormatAttributes': 'class,style,lang,width,height,align,hspace,valign'
  11788. });
  11789. me.commands['removeformat'] = {
  11790. execCommand: function (cmdName, tags, style, attrs, notIncludeA) {
  11791. var tagReg = new RegExp('^(?:' + (tags || this.options.removeFormatTags).replace(/,/g, '|') + ')$', 'i'),
  11792. removeFormatAttributes = style ? [] : (attrs || this.options.removeFormatAttributes).split(','),
  11793. range = new dom.Range(this.document),
  11794. bookmark, node, parent,
  11795. filter = function (node) {
  11796. return node.nodeType == 1;
  11797. };
  11798. function isRedundantSpan(node) {
  11799. if (node.nodeType == 3 || node.tagName.toLowerCase() != 'span') {
  11800. return 0;
  11801. }
  11802. if (browser.ie) {
  11803. //ie 下判断实效,所以只能简单用style来判断
  11804. //return node.style.cssText == '' ? 1 : 0;
  11805. var attrs = node.attributes;
  11806. if (attrs.length) {
  11807. for (var i = 0, l = attrs.length; i < l; i++) {
  11808. if (attrs[i].specified) {
  11809. return 0;
  11810. }
  11811. }
  11812. return 1;
  11813. }
  11814. }
  11815. return !node.attributes.length;
  11816. }
  11817. function doRemove(range) {
  11818. var bookmark1 = range.createBookmark();
  11819. if (range.collapsed) {
  11820. range.enlarge(true);
  11821. }
  11822. //不能把a标签切了
  11823. if (!notIncludeA) {
  11824. var aNode = domUtils.findParentByTagName(range.startContainer, 'a', true);
  11825. if (aNode) {
  11826. range.setStartBefore(aNode);
  11827. }
  11828. aNode = domUtils.findParentByTagName(range.endContainer, 'a', true);
  11829. if (aNode) {
  11830. range.setEndAfter(aNode);
  11831. }
  11832. }
  11833. bookmark = range.createBookmark();
  11834. node = bookmark.start;
  11835. //切开始
  11836. while ((parent = node.parentNode) && !domUtils.isBlockElm(parent)) {
  11837. domUtils.breakParent(node, parent);
  11838. domUtils.clearEmptySibling(node);
  11839. }
  11840. if (bookmark.end) {
  11841. //切结束
  11842. node = bookmark.end;
  11843. while ((parent = node.parentNode) && !domUtils.isBlockElm(parent)) {
  11844. domUtils.breakParent(node, parent);
  11845. domUtils.clearEmptySibling(node);
  11846. }
  11847. //开始去除样式
  11848. var current = domUtils.getNextDomNode(bookmark.start, false, filter),
  11849. next;
  11850. while (current) {
  11851. if (current == bookmark.end) {
  11852. break;
  11853. }
  11854. next = domUtils.getNextDomNode(current, true, filter);
  11855. if (!dtd.$empty[current.tagName.toLowerCase()] && !domUtils.isBookmarkNode(current)) {
  11856. if (tagReg.test(current.tagName)) {
  11857. if (style) {
  11858. domUtils.removeStyle(current, style);
  11859. if (isRedundantSpan(current) && style != 'text-decoration') {
  11860. domUtils.remove(current, true);
  11861. }
  11862. } else {
  11863. domUtils.remove(current, true);
  11864. }
  11865. } else {
  11866. //trace:939 不能把list上的样式去掉
  11867. if (!dtd.$tableContent[current.tagName] && !dtd.$list[current.tagName]) {
  11868. domUtils.removeAttributes(current, removeFormatAttributes);
  11869. if (isRedundantSpan(current)) {
  11870. domUtils.remove(current, true);
  11871. }
  11872. }
  11873. }
  11874. }
  11875. current = next;
  11876. }
  11877. }
  11878. //trace:1035
  11879. //trace:1096 不能把td上的样式去掉,比如边框
  11880. var pN = bookmark.start.parentNode;
  11881. if (domUtils.isBlockElm(pN) && !dtd.$tableContent[pN.tagName] && !dtd.$list[pN.tagName]) {
  11882. domUtils.removeAttributes(pN, removeFormatAttributes);
  11883. }
  11884. pN = bookmark.end.parentNode;
  11885. if (bookmark.end && domUtils.isBlockElm(pN) && !dtd.$tableContent[pN.tagName] && !dtd.$list[pN.tagName]) {
  11886. domUtils.removeAttributes(pN, removeFormatAttributes);
  11887. }
  11888. range.moveToBookmark(bookmark).moveToBookmark(bookmark1);
  11889. //清除冗余的代码 <b><bookmark></b>
  11890. var node = range.startContainer,
  11891. tmp,
  11892. collapsed = range.collapsed;
  11893. while (node.nodeType == 1 && domUtils.isEmptyNode(node) && dtd.$removeEmpty[node.tagName]) {
  11894. tmp = node.parentNode;
  11895. range.setStartBefore(node);
  11896. //trace:937
  11897. //更新结束边界
  11898. if (range.startContainer === range.endContainer) {
  11899. range.endOffset--;
  11900. }
  11901. domUtils.remove(node);
  11902. node = tmp;
  11903. }
  11904. if (!collapsed) {
  11905. node = range.endContainer;
  11906. while (node.nodeType == 1 && domUtils.isEmptyNode(node) && dtd.$removeEmpty[node.tagName]) {
  11907. tmp = node.parentNode;
  11908. range.setEndBefore(node);
  11909. domUtils.remove(node);
  11910. node = tmp;
  11911. }
  11912. }
  11913. }
  11914. range = this.selection.getRange();
  11915. doRemove(range);
  11916. range.select();
  11917. }
  11918. };
  11919. };
  11920. // plugins/blockquote.js
  11921. /**
  11922. * 添加引用
  11923. * @file
  11924. * @since 1.2.6.1
  11925. */
  11926. /**
  11927. * 添加引用
  11928. * @command blockquote
  11929. * @method execCommand
  11930. * @param { String } cmd 命令字符串
  11931. * @example
  11932. * ```javascript
  11933. * editor.execCommand( 'blockquote' );
  11934. * ```
  11935. */
  11936. /**
  11937. * 添加引用
  11938. * @command blockquote
  11939. * @method execCommand
  11940. * @param { String } cmd 命令字符串
  11941. * @param { Object } attrs 节点属性
  11942. * @example
  11943. * ```javascript
  11944. * editor.execCommand( 'blockquote',{
  11945. * style: "color: red;"
  11946. * } );
  11947. * ```
  11948. */
  11949. UE.plugins['blockquote'] = function () {
  11950. var me = this;
  11951. function getObj(editor) {
  11952. return domUtils.filterNodeList(editor.selection.getStartElementPath(), 'blockquote');
  11953. }
  11954. me.commands['blockquote'] = {
  11955. execCommand: function (cmdName, attrs) {
  11956. var range = this.selection.getRange(),
  11957. obj = getObj(this),
  11958. blockquote = dtd.blockquote,
  11959. bookmark = range.createBookmark();
  11960. if (obj) {
  11961. var start = range.startContainer,
  11962. startBlock = domUtils.isBlockElm(start) ? start : domUtils.findParent(start, function (node) {
  11963. return domUtils.isBlockElm(node)
  11964. }),
  11965. end = range.endContainer,
  11966. endBlock = domUtils.isBlockElm(end) ? end : domUtils.findParent(end, function (node) {
  11967. return domUtils.isBlockElm(node)
  11968. });
  11969. //处理一下li
  11970. startBlock = domUtils.findParentByTagName(startBlock, 'li', true) || startBlock;
  11971. endBlock = domUtils.findParentByTagName(endBlock, 'li', true) || endBlock;
  11972. if (startBlock.tagName == 'LI' || startBlock.tagName == 'TD' || startBlock === obj || domUtils.isBody(startBlock)) {
  11973. domUtils.remove(obj, true);
  11974. } else {
  11975. domUtils.breakParent(startBlock, obj);
  11976. }
  11977. if (startBlock !== endBlock) {
  11978. obj = domUtils.findParentByTagName(endBlock, 'blockquote');
  11979. if (obj) {
  11980. if (endBlock.tagName == 'LI' || endBlock.tagName == 'TD' || domUtils.isBody(endBlock)) {
  11981. obj.parentNode && domUtils.remove(obj, true);
  11982. } else {
  11983. domUtils.breakParent(endBlock, obj);
  11984. }
  11985. }
  11986. }
  11987. var blockquotes = domUtils.getElementsByTagName(this.document, 'blockquote');
  11988. for (var i = 0, bi; bi = blockquotes[i++];) {
  11989. if (!bi.childNodes.length) {
  11990. domUtils.remove(bi);
  11991. } else if (domUtils.getPosition(bi, startBlock) & domUtils.POSITION_FOLLOWING && domUtils.getPosition(bi, endBlock) & domUtils.POSITION_PRECEDING) {
  11992. domUtils.remove(bi, true);
  11993. }
  11994. }
  11995. } else {
  11996. var tmpRange = range.cloneRange(),
  11997. node = tmpRange.startContainer.nodeType == 1 ? tmpRange.startContainer : tmpRange.startContainer.parentNode,
  11998. preNode = node,
  11999. doEnd = 1;
  12000. //调整开始
  12001. while (1) {
  12002. if (domUtils.isBody(node)) {
  12003. if (preNode !== node) {
  12004. if (range.collapsed) {
  12005. tmpRange.selectNode(preNode);
  12006. doEnd = 0;
  12007. } else {
  12008. tmpRange.setStartBefore(preNode);
  12009. }
  12010. } else {
  12011. tmpRange.setStart(node, 0);
  12012. }
  12013. break;
  12014. }
  12015. if (!blockquote[node.tagName]) {
  12016. if (range.collapsed) {
  12017. tmpRange.selectNode(preNode);
  12018. } else {
  12019. tmpRange.setStartBefore(preNode);
  12020. }
  12021. break;
  12022. }
  12023. preNode = node;
  12024. node = node.parentNode;
  12025. }
  12026. //调整结束
  12027. if (doEnd) {
  12028. preNode = node = node = tmpRange.endContainer.nodeType == 1 ? tmpRange.endContainer : tmpRange.endContainer.parentNode;
  12029. while (1) {
  12030. if (domUtils.isBody(node)) {
  12031. if (preNode !== node) {
  12032. tmpRange.setEndAfter(preNode);
  12033. } else {
  12034. tmpRange.setEnd(node, node.childNodes.length);
  12035. }
  12036. break;
  12037. }
  12038. if (!blockquote[node.tagName]) {
  12039. tmpRange.setEndAfter(preNode);
  12040. break;
  12041. }
  12042. preNode = node;
  12043. node = node.parentNode;
  12044. }
  12045. }
  12046. node = range.document.createElement('blockquote');
  12047. domUtils.setAttributes(node, attrs);
  12048. node.appendChild(tmpRange.extractContents());
  12049. tmpRange.insertNode(node);
  12050. //去除重复的
  12051. var childs = domUtils.getElementsByTagName(node, 'blockquote');
  12052. for (var i = 0, ci; ci = childs[i++];) {
  12053. if (ci.parentNode) {
  12054. domUtils.remove(ci, true);
  12055. }
  12056. }
  12057. }
  12058. range.moveToBookmark(bookmark).select();
  12059. },
  12060. queryCommandState: function () {
  12061. return getObj(this) ? 1 : 0;
  12062. }
  12063. };
  12064. };
  12065. // plugins/convertcase.js
  12066. /**
  12067. * 大小写转换
  12068. * @file
  12069. * @since 1.2.6.1
  12070. */
  12071. /**
  12072. * 把选区内文本变大写,与“tolowercase”命令互斥
  12073. * @command touppercase
  12074. * @method execCommand
  12075. * @param { String } cmd 命令字符串
  12076. * @example
  12077. * ```javascript
  12078. * editor.execCommand( 'touppercase' );
  12079. * ```
  12080. */
  12081. /**
  12082. * 把选区内文本变小写,与“touppercase”命令互斥
  12083. * @command tolowercase
  12084. * @method execCommand
  12085. * @param { String } cmd 命令字符串
  12086. * @example
  12087. * ```javascript
  12088. * editor.execCommand( 'tolowercase' );
  12089. * ```
  12090. */
  12091. UE.commands['touppercase'] =
  12092. UE.commands['tolowercase'] = {
  12093. execCommand: function (cmd) {
  12094. var me = this;
  12095. var rng = me.selection.getRange();
  12096. if (rng.collapsed) {
  12097. return rng;
  12098. }
  12099. var bk = rng.createBookmark(),
  12100. bkEnd = bk.end,
  12101. filterFn = function (node) {
  12102. return !domUtils.isBr(node) && !domUtils.isWhitespace(node);
  12103. },
  12104. curNode = domUtils.getNextDomNode(bk.start, false, filterFn);
  12105. while (curNode && (domUtils.getPosition(curNode, bkEnd) & domUtils.POSITION_PRECEDING)) {
  12106. if (curNode.nodeType == 3) {
  12107. curNode.nodeValue = curNode.nodeValue[cmd == 'touppercase' ? 'toUpperCase' : 'toLowerCase']();
  12108. }
  12109. curNode = domUtils.getNextDomNode(curNode, true, filterFn);
  12110. if (curNode === bkEnd) {
  12111. break;
  12112. }
  12113. }
  12114. rng.moveToBookmark(bk).select();
  12115. }
  12116. };
  12117. // plugins/indent.js
  12118. /**
  12119. * 首行缩进
  12120. * @file
  12121. * @since 1.2.6.1
  12122. */
  12123. /**
  12124. * 缩进
  12125. * @command indent
  12126. * @method execCommand
  12127. * @param { String } cmd 命令字符串
  12128. * @example
  12129. * ```javascript
  12130. * editor.execCommand( 'indent' );
  12131. * ```
  12132. */
  12133. UE.commands['indent'] = {
  12134. execCommand: function () {
  12135. var me = this,
  12136. value = me.queryCommandState("indent") ? "0em" : (me.options.indentValue || '2em');
  12137. me.execCommand('Paragraph', 'p', {
  12138. style: 'text-indent:' + value
  12139. });
  12140. },
  12141. queryCommandState: function () {
  12142. var pN = domUtils.filterNodeList(this.selection.getStartElementPath(), 'p h1 h2 h3 h4 h5 h6');
  12143. return pN && pN.style.textIndent && parseInt(pN.style.textIndent) ? 1 : 0;
  12144. }
  12145. };
  12146. // plugins/print.js
  12147. /**
  12148. * 打印
  12149. * @file
  12150. * @since 1.2.6.1
  12151. */
  12152. /**
  12153. * 打印
  12154. * @command print
  12155. * @method execCommand
  12156. * @param { String } cmd 命令字符串
  12157. * @example
  12158. * ```javascript
  12159. * editor.execCommand( 'print' );
  12160. * ```
  12161. */
  12162. UE.commands['print'] = {
  12163. execCommand: function () {
  12164. this.window.print();
  12165. },
  12166. notNeedUndo: 1
  12167. };
  12168. // plugins/preview.js
  12169. /**
  12170. * 预览
  12171. * @file
  12172. * @since 1.2.6.1
  12173. */
  12174. /**
  12175. * 预览
  12176. * @command preview
  12177. * @method execCommand
  12178. * @param { String } cmd 命令字符串
  12179. * @example
  12180. * ```javascript
  12181. * editor.execCommand( 'preview' );
  12182. * ```
  12183. */
  12184. UE.commands['preview'] = {
  12185. execCommand: function () {
  12186. var w = window.open('', '_blank', ''),
  12187. d = w.document;
  12188. d.open();
  12189. d.write('<!DOCTYPE html><html><head><meta charset="utf-8"/><script src="' + this.options.UEDITOR_HOME_URL + 'ueditor.parse.js"></script><script>' +
  12190. "setTimeout(function(){uParse('div',{rootPath: '" + this.options.UEDITOR_HOME_URL + "'})},300)" +
  12191. '</script></head><body><div>' + this.getContent(null, null, true) + '</div></body></html>');
  12192. d.close();
  12193. },
  12194. notNeedUndo: 1
  12195. };
  12196. // plugins/selectall.js
  12197. /**
  12198. * 全选
  12199. * @file
  12200. * @since 1.2.6.1
  12201. */
  12202. /**
  12203. * 选中所有内容
  12204. * @command selectall
  12205. * @method execCommand
  12206. * @param { String } cmd 命令字符串
  12207. * @example
  12208. * ```javascript
  12209. * editor.execCommand( 'selectall' );
  12210. * ```
  12211. */
  12212. UE.plugins['selectall'] = function () {
  12213. var me = this;
  12214. me.commands['selectall'] = {
  12215. execCommand: function () {
  12216. //去掉了原生的selectAll,因为会出现报错和当内容为空时,不能出现闭合状态的光标
  12217. var me = this,
  12218. body = me.body,
  12219. range = me.selection.getRange();
  12220. range.selectNodeContents(body);
  12221. if (domUtils.isEmptyBlock(body)) {
  12222. //opera不能自动合并到元素的里边,要手动处理一下
  12223. if (browser.opera && body.firstChild && body.firstChild.nodeType == 1) {
  12224. range.setStartAtFirst(body.firstChild);
  12225. }
  12226. range.collapse(true);
  12227. }
  12228. range.select(true);
  12229. },
  12230. notNeedUndo: 1
  12231. };
  12232. //快捷键
  12233. me.addshortcutkey({
  12234. "selectAll": "ctrl+65"
  12235. });
  12236. };
  12237. // plugins/paragraph.js
  12238. /**
  12239. * 段落样式
  12240. * @file
  12241. * @since 1.2.6.1
  12242. */
  12243. /**
  12244. * 段落格式
  12245. * @command paragraph
  12246. * @method execCommand
  12247. * @param { String } cmd 命令字符串
  12248. * @param {String} style 标签值为:'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'
  12249. * @param {Object} attrs 标签的属性
  12250. * @example
  12251. * ```javascript
  12252. * editor.execCommand( 'Paragraph','h1','{
  12253. * class:'test'
  12254. * }' );
  12255. * ```
  12256. */
  12257. /**
  12258. * 返回选区内节点标签名
  12259. * @command paragraph
  12260. * @method queryCommandValue
  12261. * @param { String } cmd 命令字符串
  12262. * @return { String } 节点标签名
  12263. * @example
  12264. * ```javascript
  12265. * editor.queryCommandValue( 'Paragraph' );
  12266. * ```
  12267. */
  12268. UE.plugins['paragraph'] = function () {
  12269. var me = this,
  12270. block = domUtils.isBlockElm,
  12271. notExchange = ['TD', 'LI', 'PRE'],
  12272. doParagraph = function (range, style, attrs, sourceCmdName) {
  12273. var bookmark = range.createBookmark(),
  12274. filterFn = function (node) {
  12275. return node.nodeType == 1 ? node.tagName.toLowerCase() != 'br' && !domUtils.isBookmarkNode(node) : !domUtils.isWhitespace(node);
  12276. },
  12277. para;
  12278. range.enlarge(true);
  12279. var bookmark2 = range.createBookmark(),
  12280. current = domUtils.getNextDomNode(bookmark2.start, false, filterFn),
  12281. tmpRange = range.cloneRange(),
  12282. tmpNode;
  12283. while (current && !(domUtils.getPosition(current, bookmark2.end) & domUtils.POSITION_FOLLOWING)) {
  12284. if (current.nodeType == 3 || !block(current)) {
  12285. tmpRange.setStartBefore(current);
  12286. while (current && current !== bookmark2.end && !block(current)) {
  12287. tmpNode = current;
  12288. current = domUtils.getNextDomNode(current, false, null, function (node) {
  12289. return !block(node);
  12290. });
  12291. }
  12292. tmpRange.setEndAfter(tmpNode);
  12293. para = range.document.createElement(style);
  12294. if (attrs) {
  12295. domUtils.setAttributes(para, attrs);
  12296. if (sourceCmdName && sourceCmdName == 'customstyle' && attrs.style) {
  12297. para.style.cssText = attrs.style;
  12298. }
  12299. }
  12300. para.appendChild(tmpRange.extractContents());
  12301. //需要内容占位
  12302. if (domUtils.isEmptyNode(para)) {
  12303. domUtils.fillChar(range.document, para);
  12304. }
  12305. tmpRange.insertNode(para);
  12306. var parent = para.parentNode;
  12307. //如果para上一级是一个block元素且不是body,td就删除它
  12308. if (block(parent) && !domUtils.isBody(para.parentNode) && utils.indexOf(notExchange, parent.tagName) == -1) {
  12309. //存储dir,style
  12310. if (!(sourceCmdName && sourceCmdName == 'customstyle')) {
  12311. parent.getAttribute('dir') && para.setAttribute('dir', parent.getAttribute('dir'));
  12312. //trace:1070
  12313. parent.style.cssText && (para.style.cssText = parent.style.cssText + ';' + para.style.cssText);
  12314. //trace:1030
  12315. parent.style.textAlign && !para.style.textAlign && (para.style.textAlign = parent.style.textAlign);
  12316. parent.style.textIndent && !para.style.textIndent && (para.style.textIndent = parent.style.textIndent);
  12317. parent.style.padding && !para.style.padding && (para.style.padding = parent.style.padding);
  12318. }
  12319. //trace:1706 选择的就是h1-6要删除
  12320. if (attrs && /h\d/i.test(parent.tagName) && !/h\d/i.test(para.tagName)) {
  12321. domUtils.setAttributes(parent, attrs);
  12322. if (sourceCmdName && sourceCmdName == 'customstyle' && attrs.style) {
  12323. parent.style.cssText = attrs.style;
  12324. }
  12325. domUtils.remove(para, true);
  12326. para = parent;
  12327. } else {
  12328. domUtils.remove(para.parentNode, true);
  12329. }
  12330. }
  12331. if (utils.indexOf(notExchange, parent.tagName) != -1) {
  12332. current = parent;
  12333. } else {
  12334. current = para;
  12335. }
  12336. current = domUtils.getNextDomNode(current, false, filterFn);
  12337. } else {
  12338. current = domUtils.getNextDomNode(current, true, filterFn);
  12339. }
  12340. }
  12341. return range.moveToBookmark(bookmark2).moveToBookmark(bookmark);
  12342. };
  12343. me.setOpt('paragraph', {
  12344. 'p': '',
  12345. 'h1': '',
  12346. 'h2': '',
  12347. 'h3': '',
  12348. 'h4': '',
  12349. 'h5': '',
  12350. 'h6': ''
  12351. });
  12352. me.commands['paragraph'] = {
  12353. execCommand: function (cmdName, style, attrs, sourceCmdName) {
  12354. var range = this.selection.getRange();
  12355. //闭合时单独处理
  12356. if (range.collapsed) {
  12357. var txt = this.document.createTextNode('p');
  12358. range.insertNode(txt);
  12359. //去掉冗余的fillchar
  12360. if (browser.ie) {
  12361. var node = txt.previousSibling;
  12362. if (node && domUtils.isWhitespace(node)) {
  12363. domUtils.remove(node);
  12364. }
  12365. node = txt.nextSibling;
  12366. if (node && domUtils.isWhitespace(node)) {
  12367. domUtils.remove(node);
  12368. }
  12369. }
  12370. }
  12371. range = doParagraph(range, style, attrs, sourceCmdName);
  12372. if (txt) {
  12373. range.setStartBefore(txt).collapse(true);
  12374. pN = txt.parentNode;
  12375. domUtils.remove(txt);
  12376. if (domUtils.isBlockElm(pN) && domUtils.isEmptyNode(pN)) {
  12377. domUtils.fillNode(this.document, pN);
  12378. }
  12379. }
  12380. if (browser.gecko && range.collapsed && range.startContainer.nodeType == 1) {
  12381. var child = range.startContainer.childNodes[range.startOffset];
  12382. if (child && child.nodeType == 1 && child.tagName.toLowerCase() == style) {
  12383. range.setStart(child, 0).collapse(true);
  12384. }
  12385. }
  12386. //trace:1097 原来有true,原因忘了,但去了就不能清除多余的占位符了
  12387. range.select();
  12388. return true;
  12389. },
  12390. queryCommandValue: function () {
  12391. var node = domUtils.filterNodeList(this.selection.getStartElementPath(), 'p h1 h2 h3 h4 h5 h6');
  12392. return node ? node.tagName.toLowerCase() : '';
  12393. }
  12394. };
  12395. };
  12396. // plugins/directionality.js
  12397. /**
  12398. * 设置文字输入的方向的插件
  12399. * @file
  12400. * @since 1.2.6.1
  12401. */
  12402. (function () {
  12403. var block = domUtils.isBlockElm,
  12404. getObj = function (editor) {
  12405. // var startNode = editor.selection.getStart(),
  12406. // parents;
  12407. // if ( startNode ) {
  12408. // //查找所有的是block的父亲节点
  12409. // parents = domUtils.findParents( startNode, true, block, true );
  12410. // for ( var i = 0,ci; ci = parents[i++]; ) {
  12411. // if ( ci.getAttribute( 'dir' ) ) {
  12412. // return ci;
  12413. // }
  12414. // }
  12415. // }
  12416. return domUtils.filterNodeList(editor.selection.getStartElementPath(), function (n) {
  12417. return n && n.nodeType == 1 && n.getAttribute('dir')
  12418. });
  12419. },
  12420. doDirectionality = function (range, editor, forward) {
  12421. var bookmark,
  12422. filterFn = function (node) {
  12423. return node.nodeType == 1 ? !domUtils.isBookmarkNode(node) : !domUtils.isWhitespace(node);
  12424. },
  12425. obj = getObj(editor);
  12426. if (obj && range.collapsed) {
  12427. obj.setAttribute('dir', forward);
  12428. return range;
  12429. }
  12430. bookmark = range.createBookmark();
  12431. range.enlarge(true);
  12432. var bookmark2 = range.createBookmark(),
  12433. current = domUtils.getNextDomNode(bookmark2.start, false, filterFn),
  12434. tmpRange = range.cloneRange(),
  12435. tmpNode;
  12436. while (current && !(domUtils.getPosition(current, bookmark2.end) & domUtils.POSITION_FOLLOWING)) {
  12437. if (current.nodeType == 3 || !block(current)) {
  12438. tmpRange.setStartBefore(current);
  12439. while (current && current !== bookmark2.end && !block(current)) {
  12440. tmpNode = current;
  12441. current = domUtils.getNextDomNode(current, false, null, function (node) {
  12442. return !block(node);
  12443. });
  12444. }
  12445. tmpRange.setEndAfter(tmpNode);
  12446. var common = tmpRange.getCommonAncestor();
  12447. if (!domUtils.isBody(common) && block(common)) {
  12448. //遍历到了block节点
  12449. common.setAttribute('dir', forward);
  12450. current = common;
  12451. } else {
  12452. //没有遍历到,添加一个block节点
  12453. var p = range.document.createElement('p');
  12454. p.setAttribute('dir', forward);
  12455. var frag = tmpRange.extractContents();
  12456. p.appendChild(frag);
  12457. tmpRange.insertNode(p);
  12458. current = p;
  12459. }
  12460. current = domUtils.getNextDomNode(current, false, filterFn);
  12461. } else {
  12462. current = domUtils.getNextDomNode(current, true, filterFn);
  12463. }
  12464. }
  12465. return range.moveToBookmark(bookmark2).moveToBookmark(bookmark);
  12466. };
  12467. /**
  12468. * 文字输入方向
  12469. * @command directionality
  12470. * @method execCommand
  12471. * @param { String } cmdName 命令字符串
  12472. * @param { String } forward 传入'ltr'表示从左向右输入,传入'rtl'表示从右向左输入
  12473. * @example
  12474. * ```javascript
  12475. * editor.execCommand( 'directionality', 'ltr');
  12476. * ```
  12477. */
  12478. /**
  12479. * 查询当前选区的文字输入方向
  12480. * @command directionality
  12481. * @method queryCommandValue
  12482. * @param { String } cmdName 命令字符串
  12483. * @return { String } 返回'ltr'表示从左向右输入,返回'rtl'表示从右向左输入
  12484. * @example
  12485. * ```javascript
  12486. * editor.queryCommandValue( 'directionality');
  12487. * ```
  12488. */
  12489. UE.commands['directionality'] = {
  12490. execCommand: function (cmdName, forward) {
  12491. var range = this.selection.getRange();
  12492. //闭合时单独处理
  12493. if (range.collapsed) {
  12494. var txt = this.document.createTextNode('d');
  12495. range.insertNode(txt);
  12496. }
  12497. doDirectionality(range, this, forward);
  12498. if (txt) {
  12499. range.setStartBefore(txt).collapse(true);
  12500. domUtils.remove(txt);
  12501. }
  12502. range.select();
  12503. return true;
  12504. },
  12505. queryCommandValue: function () {
  12506. var node = getObj(this);
  12507. return node ? node.getAttribute('dir') : 'ltr';
  12508. }
  12509. };
  12510. })();
  12511. // plugins/horizontal.js
  12512. /**
  12513. * 插入分割线插件
  12514. * @file
  12515. * @since 1.2.6.1
  12516. */
  12517. /**
  12518. * 插入分割线
  12519. * @command horizontal
  12520. * @method execCommand
  12521. * @param { String } cmdName 命令字符串
  12522. * @example
  12523. * ```javascript
  12524. * editor.execCommand( 'horizontal' );
  12525. * ```
  12526. */
  12527. UE.plugins['horizontal'] = function () {
  12528. var me = this;
  12529. me.commands['horizontal'] = {
  12530. execCommand: function (cmdName) {
  12531. var me = this;
  12532. if (me.queryCommandState(cmdName) !== -1) {
  12533. me.execCommand('insertHtml', '<hr>');
  12534. var range = me.selection.getRange(),
  12535. start = range.startContainer;
  12536. if (start.nodeType == 1 && !start.childNodes[range.startOffset]) {
  12537. var tmp;
  12538. if (tmp = start.childNodes[range.startOffset - 1]) {
  12539. if (tmp.nodeType == 1 && tmp.tagName == 'HR') {
  12540. if (me.options.enterTag == 'p') {
  12541. tmp = me.document.createElement('p');
  12542. range.insertNode(tmp);
  12543. range.setStart(tmp, 0).setCursor();
  12544. } else {
  12545. tmp = me.document.createElement('br');
  12546. range.insertNode(tmp);
  12547. range.setStartBefore(tmp).setCursor();
  12548. }
  12549. }
  12550. }
  12551. }
  12552. return true;
  12553. }
  12554. },
  12555. //边界在table里不能加分隔线
  12556. queryCommandState: function () {
  12557. return domUtils.filterNodeList(this.selection.getStartElementPath(), 'table') ? -1 : 0;
  12558. }
  12559. };
  12560. // me.addListener('delkeyup',function(){
  12561. // var rng = this.selection.getRange();
  12562. // if(browser.ie && browser.version > 8){
  12563. // rng.txtToElmBoundary(true);
  12564. // if(domUtils.isStartInblock(rng)){
  12565. // var tmpNode = rng.startContainer;
  12566. // var pre = tmpNode.previousSibling;
  12567. // if(pre && domUtils.isTagNode(pre,'hr')){
  12568. // domUtils.remove(pre);
  12569. // rng.select();
  12570. // return;
  12571. // }
  12572. // }
  12573. // }
  12574. // if(domUtils.isBody(rng.startContainer)){
  12575. // var hr = rng.startContainer.childNodes[rng.startOffset -1];
  12576. // if(hr && hr.nodeName == 'HR'){
  12577. // var next = hr.nextSibling;
  12578. // if(next){
  12579. // rng.setStart(next,0)
  12580. // }else if(hr.previousSibling){
  12581. // rng.setStartAtLast(hr.previousSibling)
  12582. // }else{
  12583. // var p = this.document.createElement('p');
  12584. // hr.parentNode.insertBefore(p,hr);
  12585. // domUtils.fillNode(this.document,p);
  12586. // rng.setStart(p,0);
  12587. // }
  12588. // domUtils.remove(hr);
  12589. // rng.setCursor(false,true);
  12590. // }
  12591. // }
  12592. // })
  12593. me.addListener('delkeydown', function (name, evt) {
  12594. var rng = this.selection.getRange();
  12595. rng.txtToElmBoundary(true);
  12596. if (domUtils.isStartInblock(rng)) {
  12597. var tmpNode = rng.startContainer;
  12598. var pre = tmpNode.previousSibling;
  12599. if (pre && domUtils.isTagNode(pre, 'hr')) {
  12600. domUtils.remove(pre);
  12601. rng.select();
  12602. domUtils.preventDefault(evt);
  12603. return true;
  12604. }
  12605. }
  12606. })
  12607. };
  12608. // plugins/time.js
  12609. /**
  12610. * 插入时间和日期
  12611. * @file
  12612. * @since 1.2.6.1
  12613. */
  12614. /**
  12615. * 插入时间,默认格式:12:59:59
  12616. * @command time
  12617. * @method execCommand
  12618. * @param { String } cmd 命令字符串
  12619. * @example
  12620. * ```javascript
  12621. * editor.execCommand( 'time');
  12622. * ```
  12623. */
  12624. /**
  12625. * 插入日期,默认格式:2013-08-30
  12626. * @command date
  12627. * @method execCommand
  12628. * @param { String } cmd 命令字符串
  12629. * @example
  12630. * ```javascript
  12631. * editor.execCommand( 'date');
  12632. * ```
  12633. */
  12634. UE.commands['time'] = UE.commands["date"] = {
  12635. execCommand: function (cmd, format) {
  12636. var date = new Date;
  12637. function formatTime(date, format) {
  12638. var hh = ('0' + date.getHours()).slice(-2),
  12639. ii = ('0' + date.getMinutes()).slice(-2),
  12640. ss = ('0' + date.getSeconds()).slice(-2);
  12641. format = format || 'hh:ii:ss';
  12642. return format.replace(/hh/ig, hh).replace(/ii/ig, ii).replace(/ss/ig, ss);
  12643. }
  12644. function formatDate(date, format) {
  12645. var yyyy = ('000' + date.getFullYear()).slice(-4),
  12646. yy = yyyy.slice(-2),
  12647. mm = ('0' + (date.getMonth() + 1)).slice(-2),
  12648. dd = ('0' + date.getDate()).slice(-2);
  12649. format = format || 'yyyy-mm-dd';
  12650. return format.replace(/yyyy/ig, yyyy).replace(/yy/ig, yy).replace(/mm/ig, mm).replace(/dd/ig, dd);
  12651. }
  12652. this.execCommand('insertHtml', cmd == "time" ? formatTime(date, format) : formatDate(date, format));
  12653. }
  12654. };
  12655. // plugins/rowspacing.js
  12656. /**
  12657. * 段前段后间距插件
  12658. * @file
  12659. * @since 1.2.6.1
  12660. */
  12661. /**
  12662. * 设置段间距
  12663. * @command rowspacing
  12664. * @method execCommand
  12665. * @param { String } cmd 命令字符串
  12666. * @param { String } value 段间距的值,以px为单位
  12667. * @param { String } dir 间距位置,top或bottom,分别表示段前和段后
  12668. * @example
  12669. * ```javascript
  12670. * editor.execCommand( 'rowspacing', '10', 'top' );
  12671. * ```
  12672. */
  12673. UE.plugins['rowspacing'] = function () {
  12674. var me = this;
  12675. me.setOpt({
  12676. 'rowspacingtop': ['5', '10', '15', '20', '25'],
  12677. 'rowspacingbottom': ['5', '10', '15', '20', '25']
  12678. });
  12679. me.commands['rowspacing'] = {
  12680. execCommand: function (cmdName, value, dir) {
  12681. this.execCommand('paragraph', 'p', {
  12682. style: 'margin-' + dir + ':' + value + 'px'
  12683. });
  12684. return true;
  12685. },
  12686. queryCommandValue: function (cmdName, dir) {
  12687. var pN = domUtils.filterNodeList(this.selection.getStartElementPath(), function (node) {
  12688. return domUtils.isBlockElm(node)
  12689. }),
  12690. value;
  12691. //trace:1026
  12692. if (pN) {
  12693. value = domUtils.getComputedStyle(pN, 'margin-' + dir).replace(/[^\d]/g, '');
  12694. return !value ? 0 : value;
  12695. }
  12696. return 0;
  12697. }
  12698. };
  12699. };
  12700. // plugins/lineheight.js
  12701. /**
  12702. * 设置行内间距
  12703. * @file
  12704. * @since 1.2.6.1
  12705. */
  12706. UE.plugins['lineheight'] = function () {
  12707. var me = this;
  12708. me.setOpt({
  12709. 'lineheight': ['1', '1.5', '1.75', '2', '3', '4', '5']
  12710. });
  12711. /**
  12712. * 行距
  12713. * @command lineheight
  12714. * @method execCommand
  12715. * @param { String } cmdName 命令字符串
  12716. * @param { String } value 传入的行高值, 该值是当前字体的倍数, 例如: 1.5, 1.75
  12717. * @example
  12718. * ```javascript
  12719. * editor.execCommand( 'lineheight', 1.5);
  12720. * ```
  12721. */
  12722. /**
  12723. * 查询当前选区内容的行高大小
  12724. * @command lineheight
  12725. * @method queryCommandValue
  12726. * @param { String } cmd 命令字符串
  12727. * @return { String } 返回当前行高大小
  12728. * @example
  12729. * ```javascript
  12730. * editor.queryCommandValue( 'lineheight' );
  12731. * ```
  12732. */
  12733. me.commands['lineheight'] = {
  12734. execCommand: function (cmdName, value) {
  12735. this.execCommand('paragraph', 'p', {
  12736. style: 'line-height:' + (value == "1" ? "normal" : value + 'em')
  12737. });
  12738. return true;
  12739. },
  12740. queryCommandValue: function () {
  12741. var pN = domUtils.filterNodeList(this.selection.getStartElementPath(), function (node) {
  12742. return domUtils.isBlockElm(node)
  12743. });
  12744. if (pN) {
  12745. var value = domUtils.getComputedStyle(pN, 'line-height');
  12746. return value == 'normal' ? 1 : value.replace(/[^\d.]*/ig, "");
  12747. }
  12748. }
  12749. };
  12750. };
  12751. // plugins/insertcode.js
  12752. /**
  12753. * 插入代码插件
  12754. * @file
  12755. * @since 1.2.6.1
  12756. */
  12757. UE.plugins['insertcode'] = function () {
  12758. var me = this;
  12759. me.ready(function () {
  12760. utils.cssRule('pre', 'pre{margin:.5em 0;padding:.4em .6em;border-radius:8px;background:#f8f8f8;}',
  12761. me.document)
  12762. });
  12763. me.setOpt('insertcode', {
  12764. 'as3': 'ActionScript3',
  12765. 'bash': 'Bash/Shell',
  12766. 'cpp': 'C/C++',
  12767. 'css': 'Css',
  12768. 'cf': 'CodeFunction',
  12769. 'c#': 'C#',
  12770. 'delphi': 'Delphi',
  12771. 'diff': 'Diff',
  12772. 'erlang': 'Erlang',
  12773. 'groovy': 'Groovy',
  12774. 'html': 'Html',
  12775. 'java': 'Java',
  12776. 'jfx': 'JavaFx',
  12777. 'js': 'Javascript',
  12778. 'pl': 'Perl',
  12779. 'php': 'Php',
  12780. 'plain': 'Plain Text',
  12781. 'ps': 'PowerShell',
  12782. 'python': 'Python',
  12783. 'ruby': 'Ruby',
  12784. 'scala': 'Scala',
  12785. 'sql': 'Sql',
  12786. 'vb': 'Vb',
  12787. 'xml': 'Xml'
  12788. });
  12789. /**
  12790. * 插入代码
  12791. * @command insertcode
  12792. * @method execCommand
  12793. * @param { String } cmd 命令字符串
  12794. * @param { String } lang 插入代码的语言
  12795. * @example
  12796. * ```javascript
  12797. * editor.execCommand( 'insertcode', 'javascript' );
  12798. * ```
  12799. */
  12800. /**
  12801. * 如果选区所在位置是插入插入代码区域,返回代码的语言
  12802. * @command insertcode
  12803. * @method queryCommandValue
  12804. * @param { String } cmd 命令字符串
  12805. * @return { String } 返回代码的语言
  12806. * @example
  12807. * ```javascript
  12808. * editor.queryCommandValue( 'insertcode' );
  12809. * ```
  12810. */
  12811. me.commands['insertcode'] = {
  12812. execCommand: function (cmd, lang) {
  12813. var me = this,
  12814. rng = me.selection.getRange(),
  12815. pre = domUtils.findParentByTagName(rng.startContainer, 'pre', true);
  12816. if (pre) {
  12817. pre.className = 'brush:' + lang + ';toolbar:false;';
  12818. } else {
  12819. var code = '';
  12820. if (rng.collapsed) {
  12821. code = browser.ie && browser.ie11below ? (browser.version <= 8 ? '&nbsp;' : '') : '<br/>';
  12822. } else {
  12823. var frag = rng.extractContents();
  12824. var div = me.document.createElement('div');
  12825. div.appendChild(frag);
  12826. utils.each(UE.filterNode(UE.htmlparser(div.innerHTML.replace(/[\r\t]/g, '')), me.options.filterTxtRules).children, function (node) {
  12827. if (browser.ie && browser.ie11below && browser.version > 8) {
  12828. if (node.type == 'element') {
  12829. if (node.tagName == 'br') {
  12830. code += '\n'
  12831. } else if (!dtd.$empty[node.tagName]) {
  12832. utils.each(node.children, function (cn) {
  12833. if (cn.type == 'element') {
  12834. if (cn.tagName == 'br') {
  12835. code += '\n'
  12836. } else if (!dtd.$empty[node.tagName]) {
  12837. code += cn.innerText();
  12838. }
  12839. } else {
  12840. code += cn.data
  12841. }
  12842. })
  12843. if (!/\n$/.test(code)) {
  12844. code += '\n';
  12845. }
  12846. }
  12847. } else {
  12848. code += node.data + '\n'
  12849. }
  12850. if (!node.nextSibling() && /\n$/.test(code)) {
  12851. code = code.replace(/\n$/, '');
  12852. }
  12853. } else {
  12854. if (browser.ie && browser.ie11below) {
  12855. if (node.type == 'element') {
  12856. if (node.tagName == 'br') {
  12857. code += '<br>'
  12858. } else if (!dtd.$empty[node.tagName]) {
  12859. utils.each(node.children, function (cn) {
  12860. if (cn.type == 'element') {
  12861. if (cn.tagName == 'br') {
  12862. code += '<br>'
  12863. } else if (!dtd.$empty[node.tagName]) {
  12864. code += cn.innerText();
  12865. }
  12866. } else {
  12867. code += cn.data
  12868. }
  12869. });
  12870. if (!/br>$/.test(code)) {
  12871. code += '<br>';
  12872. }
  12873. }
  12874. } else {
  12875. code += node.data + '<br>'
  12876. }
  12877. if (!node.nextSibling() && /<br>$/.test(code)) {
  12878. code = code.replace(/<br>$/, '');
  12879. }
  12880. } else {
  12881. code += (node.type == 'element' ? (dtd.$empty[node.tagName] ? '' : node.innerText()) : node.data);
  12882. if (!/br\/?\s*>$/.test(code)) {
  12883. if (!node.nextSibling())
  12884. return;
  12885. code += '<br>'
  12886. }
  12887. }
  12888. }
  12889. });
  12890. }
  12891. me.execCommand('inserthtml', '<pre id="coder"class="brush:' + lang + ';toolbar:false">' + code + '</pre>', true);
  12892. pre = me.document.getElementById('coder');
  12893. domUtils.removeAttributes(pre, 'id');
  12894. var tmpNode = pre.previousSibling;
  12895. if (tmpNode && (tmpNode.nodeType == 3 && tmpNode.nodeValue.length == 1 && browser.ie && browser.version == 6 || domUtils.isEmptyBlock(tmpNode))) {
  12896. domUtils.remove(tmpNode)
  12897. }
  12898. var rng = me.selection.getRange();
  12899. if (domUtils.isEmptyBlock(pre)) {
  12900. rng.setStart(pre, 0).setCursor(false, true)
  12901. } else {
  12902. rng.selectNodeContents(pre).select()
  12903. }
  12904. }
  12905. },
  12906. queryCommandValue: function () {
  12907. var path = this.selection.getStartElementPath();
  12908. var lang = '';
  12909. utils.each(path, function (node) {
  12910. if (node.nodeName == 'PRE') {
  12911. var match = node.className.match(/brush:([^;]+)/);
  12912. lang = match && match[1] ? match[1] : '';
  12913. return false;
  12914. }
  12915. });
  12916. return lang;
  12917. }
  12918. };
  12919. me.addInputRule(function (root) {
  12920. utils.each(root.getNodesByTagName('pre'), function (pre) {
  12921. var brs = pre.getNodesByTagName('br');
  12922. if (brs.length) {
  12923. browser.ie && browser.ie11below && browser.version > 8 && utils.each(brs, function (br) {
  12924. var txt = UE.uNode.createText('\n');
  12925. br.parentNode.insertBefore(txt, br);
  12926. br.parentNode.removeChild(br);
  12927. });
  12928. return;
  12929. }
  12930. if (browser.ie && browser.ie11below && browser.version > 8)
  12931. return;
  12932. var code = pre.innerText().split(/\n/);
  12933. pre.innerHTML('');
  12934. utils.each(code, function (c) {
  12935. if (c.length) {
  12936. pre.appendChild(UE.uNode.createText(c));
  12937. }
  12938. pre.appendChild(UE.uNode.createElement('br'))
  12939. })
  12940. })
  12941. });
  12942. me.addOutputRule(function (root) {
  12943. utils.each(root.getNodesByTagName('pre'), function (pre) {
  12944. var code = '';
  12945. utils.each(pre.children, function (n) {
  12946. if (n.type == 'text') {
  12947. //在ie下文本内容有可能末尾带有\n要去掉
  12948. //trace:3396
  12949. code += n.data.replace(/[ ]/g, '&nbsp;').replace(/\n$/, '');
  12950. } else {
  12951. if (n.tagName == 'br') {
  12952. code += '\n'
  12953. } else {
  12954. code += (!dtd.$empty[n.tagName] ? '' : n.innerText());
  12955. }
  12956. }
  12957. });
  12958. pre.innerText(code.replace(/(&nbsp;|\n)+$/, ''))
  12959. })
  12960. });
  12961. //不需要判断highlight的command列表
  12962. me.notNeedCodeQuery = {
  12963. help: 1,
  12964. undo: 1,
  12965. redo: 1,
  12966. source: 1,
  12967. print: 1,
  12968. searchreplace: 1,
  12969. fullscreen: 1,
  12970. preview: 1,
  12971. insertparagraph: 1,
  12972. elementpath: 1,
  12973. insertcode: 1,
  12974. inserthtml: 1,
  12975. selectall: 1
  12976. };
  12977. //将queyCommamndState重置
  12978. var orgQuery = me.queryCommandState;
  12979. me.queryCommandState = function (cmd) {
  12980. var me = this;
  12981. if (!me.notNeedCodeQuery[cmd.toLowerCase()] && me.selection && me.queryCommandValue('insertcode')) {
  12982. return -1;
  12983. }
  12984. return UE.Editor.prototype.queryCommandState.apply(this, arguments)
  12985. };
  12986. me.addListener('beforeenterkeydown', function () {
  12987. var rng = me.selection.getRange();
  12988. var pre = domUtils.findParentByTagName(rng.startContainer, 'pre', true);
  12989. if (pre) {
  12990. me.fireEvent('saveScene');
  12991. if (!rng.collapsed) {
  12992. rng.deleteContents();
  12993. }
  12994. if (!browser.ie || browser.ie9above) {
  12995. var tmpNode = me.document.createElement('br'),
  12996. pre;
  12997. rng.insertNode(tmpNode).setStartAfter(tmpNode).collapse(true);
  12998. var next = tmpNode.nextSibling;
  12999. if (!next && (!browser.ie || browser.version > 10)) {
  13000. rng.insertNode(tmpNode.cloneNode(false));
  13001. } else {
  13002. rng.setStartAfter(tmpNode);
  13003. }
  13004. pre = tmpNode.previousSibling;
  13005. var tmp;
  13006. while (pre) {
  13007. tmp = pre;
  13008. pre = pre.previousSibling;
  13009. if (!pre || pre.nodeName == 'BR') {
  13010. pre = tmp;
  13011. break;
  13012. }
  13013. }
  13014. if (pre) {
  13015. var str = '';
  13016. while (pre && pre.nodeName != 'BR' && new RegExp('^[\\s' + domUtils.fillChar + ']*$').test(pre.nodeValue)) {
  13017. str += pre.nodeValue;
  13018. pre = pre.nextSibling;
  13019. }
  13020. if (pre.nodeName != 'BR') {
  13021. var match = pre.nodeValue.match(new RegExp('^([\\s' + domUtils.fillChar + ']+)'));
  13022. if (match && match[1]) {
  13023. str += match[1]
  13024. }
  13025. }
  13026. if (str) {
  13027. str = me.document.createTextNode(str);
  13028. rng.insertNode(str).setStartAfter(str);
  13029. }
  13030. }
  13031. rng.collapse(true).select(true);
  13032. } else {
  13033. if (browser.version > 8) {
  13034. var txt = me.document.createTextNode('\n');
  13035. var start = rng.startContainer;
  13036. if (rng.startOffset == 0) {
  13037. var preNode = start.previousSibling;
  13038. if (preNode) {
  13039. rng.insertNode(txt);
  13040. var fillchar = me.document.createTextNode(' ');
  13041. rng.setStartAfter(txt).insertNode(fillchar).setStart(fillchar, 0).collapse(true).select(true)
  13042. }
  13043. } else {
  13044. rng.insertNode(txt).setStartAfter(txt);
  13045. var fillchar = me.document.createTextNode(' ');
  13046. start = rng.startContainer.childNodes[rng.startOffset];
  13047. if (start && !/^\n/.test(start.nodeValue)) {
  13048. rng.setStartBefore(txt)
  13049. }
  13050. rng.insertNode(fillchar).setStart(fillchar, 0).collapse(true).select(true)
  13051. }
  13052. } else {
  13053. var tmpNode = me.document.createElement('br');
  13054. rng.insertNode(tmpNode);
  13055. rng.insertNode(me.document.createTextNode(domUtils.fillChar));
  13056. rng.setStartAfter(tmpNode);
  13057. pre = tmpNode.previousSibling;
  13058. var tmp;
  13059. while (pre) {
  13060. tmp = pre;
  13061. pre = pre.previousSibling;
  13062. if (!pre || pre.nodeName == 'BR') {
  13063. pre = tmp;
  13064. break;
  13065. }
  13066. }
  13067. if (pre) {
  13068. var str = '';
  13069. while (pre && pre.nodeName != 'BR' && new RegExp('^[ ' + domUtils.fillChar + ']*$').test(pre.nodeValue)) {
  13070. str += pre.nodeValue;
  13071. pre = pre.nextSibling;
  13072. }
  13073. if (pre.nodeName != 'BR') {
  13074. var match = pre.nodeValue.match(new RegExp('^([ ' + domUtils.fillChar + ']+)'));
  13075. if (match && match[1]) {
  13076. str += match[1]
  13077. }
  13078. }
  13079. str = me.document.createTextNode(str);
  13080. rng.insertNode(str).setStartAfter(str);
  13081. }
  13082. rng.collapse(true).select();
  13083. }
  13084. }
  13085. me.fireEvent('saveScene');
  13086. return true;
  13087. }
  13088. });
  13089. me.addListener('tabkeydown', function (cmd, evt) {
  13090. var rng = me.selection.getRange();
  13091. var pre = domUtils.findParentByTagName(rng.startContainer, 'pre', true);
  13092. if (pre) {
  13093. me.fireEvent('saveScene');
  13094. if (evt.shiftKey) {
  13095. } else {
  13096. if (!rng.collapsed) {
  13097. var bk = rng.createBookmark();
  13098. var start = bk.start.previousSibling;
  13099. while (start) {
  13100. if (pre.firstChild === start && !domUtils.isBr(start)) {
  13101. pre.insertBefore(me.document.createTextNode(' '), start);
  13102. break;
  13103. }
  13104. if (domUtils.isBr(start)) {
  13105. pre.insertBefore(me.document.createTextNode(' '), start.nextSibling);
  13106. break;
  13107. }
  13108. start = start.previousSibling;
  13109. }
  13110. var end = bk.end;
  13111. start = bk.start.nextSibling;
  13112. if (pre.firstChild === bk.start) {
  13113. pre.insertBefore(me.document.createTextNode(' '), start.nextSibling)
  13114. }
  13115. while (start && start !== end) {
  13116. if (domUtils.isBr(start) && start.nextSibling) {
  13117. if (start.nextSibling === end) {
  13118. break;
  13119. }
  13120. pre.insertBefore(me.document.createTextNode(' '), start.nextSibling)
  13121. }
  13122. start = start.nextSibling;
  13123. }
  13124. rng.moveToBookmark(bk).select();
  13125. } else {
  13126. var tmpNode = me.document.createTextNode(' ');
  13127. rng.insertNode(tmpNode).setStartAfter(tmpNode).collapse(true).select(true);
  13128. }
  13129. }
  13130. me.fireEvent('saveScene');
  13131. return true;
  13132. }
  13133. });
  13134. me.addListener('beforeinserthtml', function (evtName, html) {
  13135. var me = this,
  13136. rng = me.selection.getRange(),
  13137. pre = domUtils.findParentByTagName(rng.startContainer, 'pre', true);
  13138. if (pre) {
  13139. if (!rng.collapsed) {
  13140. rng.deleteContents()
  13141. }
  13142. var htmlstr = '';
  13143. if (browser.ie && browser.version > 8) {
  13144. utils.each(UE.filterNode(UE.htmlparser(html), me.options.filterTxtRules).children, function (node) {
  13145. if (node.type == 'element') {
  13146. if (node.tagName == 'br') {
  13147. htmlstr += '\n'
  13148. } else if (!dtd.$empty[node.tagName]) {
  13149. utils.each(node.children, function (cn) {
  13150. if (cn.type == 'element') {
  13151. if (cn.tagName == 'br') {
  13152. htmlstr += '\n'
  13153. } else if (!dtd.$empty[node.tagName]) {
  13154. htmlstr += cn.innerText();
  13155. }
  13156. } else {
  13157. htmlstr += cn.data
  13158. }
  13159. })
  13160. if (!/\n$/.test(htmlstr)) {
  13161. htmlstr += '\n';
  13162. }
  13163. }
  13164. } else {
  13165. htmlstr += node.data + '\n'
  13166. }
  13167. if (!node.nextSibling() && /\n$/.test(htmlstr)) {
  13168. htmlstr = htmlstr.replace(/\n$/, '');
  13169. }
  13170. });
  13171. var tmpNode = me.document.createTextNode(utils.html(htmlstr.replace(/&nbsp;/g, ' ')));
  13172. rng.insertNode(tmpNode).selectNode(tmpNode).select();
  13173. } else {
  13174. var frag = me.document.createDocumentFragment();
  13175. utils.each(UE.filterNode(UE.htmlparser(html), me.options.filterTxtRules).children, function (node) {
  13176. if (node.type == 'element') {
  13177. if (node.tagName == 'br') {
  13178. frag.appendChild(me.document.createElement('br'))
  13179. } else if (!dtd.$empty[node.tagName]) {
  13180. utils.each(node.children, function (cn) {
  13181. if (cn.type == 'element') {
  13182. if (cn.tagName == 'br') {
  13183. frag.appendChild(me.document.createElement('br'))
  13184. } else if (!dtd.$empty[node.tagName]) {
  13185. frag.appendChild(me.document.createTextNode(utils.html(cn.innerText().replace(/&nbsp;/g, ' '))));
  13186. }
  13187. } else {
  13188. frag.appendChild(me.document.createTextNode(utils.html(cn.data.replace(/&nbsp;/g, ' '))));
  13189. }
  13190. })
  13191. if (frag.lastChild.nodeName != 'BR') {
  13192. frag.appendChild(me.document.createElement('br'))
  13193. }
  13194. }
  13195. } else {
  13196. frag.appendChild(me.document.createTextNode(utils.html(node.data.replace(/&nbsp;/g, ' '))));
  13197. }
  13198. if (!node.nextSibling() && frag.lastChild.nodeName == 'BR') {
  13199. frag.removeChild(frag.lastChild)
  13200. }
  13201. });
  13202. rng.insertNode(frag).select();
  13203. }
  13204. return true;
  13205. }
  13206. });
  13207. //方向键的处理
  13208. me.addListener('keydown', function (cmd, evt) {
  13209. var me = this,
  13210. keyCode = evt.keyCode || evt.which;
  13211. if (keyCode == 40) {
  13212. var rng = me.selection.getRange(),
  13213. pre, start = rng.startContainer;
  13214. if (rng.collapsed && (pre = domUtils.findParentByTagName(rng.startContainer, 'pre', true)) && !pre.nextSibling) {
  13215. var last = pre.lastChild
  13216. while (last && last.nodeName == 'BR') {
  13217. last = last.previousSibling;
  13218. }
  13219. if (last === start || rng.startContainer === pre && rng.startOffset == pre.childNodes.length) {
  13220. me.execCommand('insertparagraph');
  13221. domUtils.preventDefault(evt)
  13222. }
  13223. }
  13224. }
  13225. });
  13226. //trace:3395
  13227. me.addListener('delkeydown', function (type, evt) {
  13228. var rng = this.selection.getRange();
  13229. rng.txtToElmBoundary(true);
  13230. var start = rng.startContainer;
  13231. if (domUtils.isTagNode(start, 'pre') && rng.collapsed && domUtils.isStartInblock(rng)) {
  13232. var p = me.document.createElement('p');
  13233. domUtils.fillNode(me.document, p);
  13234. start.parentNode.insertBefore(p, start);
  13235. domUtils.remove(start);
  13236. rng.setStart(p, 0).setCursor(false, true);
  13237. domUtils.preventDefault(evt);
  13238. return true;
  13239. }
  13240. })
  13241. };
  13242. // plugins/cleardoc.js
  13243. /**
  13244. * 清空文档插件
  13245. * @file
  13246. * @since 1.2.6.1
  13247. */
  13248. /**
  13249. * 清空文档
  13250. * @command cleardoc
  13251. * @method execCommand
  13252. * @param { String } cmd 命令字符串
  13253. * @example
  13254. * ```javascript
  13255. * //editor 是编辑器实例
  13256. * editor.execCommand('cleardoc');
  13257. * ```
  13258. */
  13259. UE.commands['cleardoc'] = {
  13260. execCommand: function (cmdName) {
  13261. var me = this,
  13262. enterTag = me.options.enterTag,
  13263. range = me.selection.getRange();
  13264. if (enterTag == "br") {
  13265. me.body.innerHTML = "<br/>";
  13266. range.setStart(me.body, 0).setCursor();
  13267. } else {
  13268. me.body.innerHTML = "<p>" + (ie ? "" : "<br/>") + "</p>";
  13269. range.setStart(me.body.firstChild, 0).setCursor(false, true);
  13270. }
  13271. setTimeout(function () {
  13272. me.fireEvent("clearDoc");
  13273. }, 0);
  13274. }
  13275. };
  13276. // plugins/anchor.js
  13277. /**
  13278. * 锚点插件,为UEditor提供插入锚点支持
  13279. * @file
  13280. * @since 1.2.6.1
  13281. */
  13282. UE.plugin.register('anchor', function () {
  13283. return {
  13284. bindEvents: {
  13285. 'ready': function () {
  13286. utils.cssRule('anchor',
  13287. '.anchorclass{background: url(\'' +
  13288. this.options.themePath +
  13289. this.options.theme + '/images/anchor.gif\') no-repeat scroll left center transparent;cursor: auto;display: inline-block;height: 16px;width: 15px;}',
  13290. this.document);
  13291. }
  13292. },
  13293. outputRule: function (root) {
  13294. utils.each(root.getNodesByTagName('img'), function (a) {
  13295. var val;
  13296. if (val = a.getAttr('anchorname')) {
  13297. a.tagName = 'a';
  13298. a.setAttr({
  13299. anchorname: '',
  13300. name: val,
  13301. 'class': ''
  13302. })
  13303. }
  13304. })
  13305. },
  13306. inputRule: function (root) {
  13307. utils.each(root.getNodesByTagName('a'), function (a) {
  13308. var val;
  13309. if ((val = a.getAttr('name')) && !a.getAttr('href')) {
  13310. a.tagName = 'img';
  13311. a.setAttr({
  13312. anchorname: a.getAttr('name'),
  13313. 'class': 'anchorclass'
  13314. });
  13315. a.setAttr('name')
  13316. }
  13317. })
  13318. },
  13319. commands: {
  13320. /**
  13321. * 插入锚点
  13322. * @command anchor
  13323. * @method execCommand
  13324. * @param { String } cmd 命令字符串
  13325. * @param { String } name 锚点名称字符串
  13326. * @example
  13327. * ```javascript
  13328. * //editor 是编辑器实例
  13329. * editor.execCommand('anchor', 'anchor1');
  13330. * ```
  13331. */
  13332. 'anchor': {
  13333. execCommand: function (cmd, name) {
  13334. var range = this.selection.getRange(),
  13335. img = range.getClosedNode();
  13336. if (img && img.getAttribute('anchorname')) {
  13337. if (name) {
  13338. img.setAttribute('anchorname', name);
  13339. } else {
  13340. range.setStartBefore(img).setCursor();
  13341. domUtils.remove(img);
  13342. }
  13343. } else {
  13344. if (name) {
  13345. //只在选区的开始插入
  13346. var anchor = this.document.createElement('img');
  13347. range.collapse(true);
  13348. domUtils.setAttributes(anchor, {
  13349. 'anchorname': name,
  13350. 'class': 'anchorclass'
  13351. });
  13352. range.insertNode(anchor).setStartAfter(anchor).setCursor(false, true);
  13353. }
  13354. }
  13355. }
  13356. }
  13357. }
  13358. }
  13359. });
  13360. // plugins/wordcount.js
  13361. ///import core
  13362. ///commands 字数统计
  13363. ///commandsName WordCount,wordCount
  13364. ///commandsTitle 字数统计
  13365. /*
  13366. * Created by JetBrains WebStorm.
  13367. * User: taoqili
  13368. * Date: 11-9-7
  13369. * Time: 下午8:18
  13370. * To change this template use File | Settings | File Templates.
  13371. */
  13372. UE.plugins['wordcount'] = function () {
  13373. var me = this;
  13374. me.setOpt('wordCount', true);
  13375. me.addListener('contentchange', function () {
  13376. me.fireEvent('wordcount');
  13377. });
  13378. var timer;
  13379. me.addListener('ready', function () {
  13380. var me = this;
  13381. domUtils.on(me.body, "keyup", function (evt) {
  13382. var code = evt.keyCode || evt.which,
  13383. //忽略的按键,ctr,alt,shift,方向键
  13384. ignores = {
  13385. "16": 1,
  13386. "18": 1,
  13387. "20": 1,
  13388. "37": 1,
  13389. "38": 1,
  13390. "39": 1,
  13391. "40": 1
  13392. };
  13393. if (code in ignores) return;
  13394. clearTimeout(timer);
  13395. timer = setTimeout(function () {
  13396. me.fireEvent('wordcount');
  13397. }, 200)
  13398. })
  13399. });
  13400. };
  13401. // plugins/pagebreak.js
  13402. /**
  13403. * 分页功能插件
  13404. * @file
  13405. * @since 1.2.6.1
  13406. */
  13407. UE.plugins['pagebreak'] = function () {
  13408. var me = this,
  13409. notBreakTags = ['td'];
  13410. me.setOpt('pageBreakTag', '_ueditor_page_break_tag_');
  13411. function fillNode(node) {
  13412. if (domUtils.isEmptyBlock(node)) {
  13413. var firstChild = node.firstChild,
  13414. tmpNode;
  13415. while (firstChild && firstChild.nodeType == 1 && domUtils.isEmptyBlock(firstChild)) {
  13416. tmpNode = firstChild;
  13417. firstChild = firstChild.firstChild;
  13418. }!tmpNode && (tmpNode = node);
  13419. domUtils.fillNode(me.document, tmpNode);
  13420. }
  13421. }
  13422. //分页符样式添加
  13423. me.ready(function () {
  13424. utils.cssRule('pagebreak', '.pagebreak{display:block;clear:both !important;cursor:default !important;width: 100% !important;margin:0;}', me.document);
  13425. });
  13426. function isHr(node) {
  13427. return node && node.nodeType == 1 && node.tagName == 'HR' && node.className == 'pagebreak';
  13428. }
  13429. me.addInputRule(function (root) {
  13430. root.traversal(function (node) {
  13431. if (node.type == 'text' && node.data == me.options.pageBreakTag) {
  13432. var hr = UE.uNode.createElement('<hr class="pagebreak" noshade="noshade" size="5" style="-webkit-user-select: none;">');
  13433. node.parentNode.insertBefore(hr, node);
  13434. node.parentNode.removeChild(node)
  13435. }
  13436. })
  13437. });
  13438. me.addOutputRule(function (node) {
  13439. utils.each(node.getNodesByTagName('hr'), function (n) {
  13440. if (n.getAttr('class') == 'pagebreak') {
  13441. var txt = UE.uNode.createText(me.options.pageBreakTag);
  13442. n.parentNode.insertBefore(txt, n);
  13443. n.parentNode.removeChild(n);
  13444. }
  13445. })
  13446. });
  13447. /**
  13448. * 插入分页符
  13449. * @command pagebreak
  13450. * @method execCommand
  13451. * @param { String } cmd 命令字符串
  13452. * @remind 在表格中插入分页符会把表格切分成两部分
  13453. * @remind 获取编辑器内的数据时, 编辑器会把分页符转换成“_ueditor_page_break_tag_”字符串,
  13454. * 以便于提交数据到服务器端后处理分页。
  13455. * @example
  13456. * ```javascript
  13457. * editor.execCommand( 'pagebreak'); //插入一个hr标签,带有样式类名pagebreak
  13458. * ```
  13459. */
  13460. me.commands['pagebreak'] = {
  13461. execCommand: function () {
  13462. var range = me.selection.getRange(),
  13463. hr = me.document.createElement('hr');
  13464. domUtils.setAttributes(hr, {
  13465. 'class': 'pagebreak',
  13466. noshade: "noshade",
  13467. size: "5"
  13468. });
  13469. domUtils.unSelectable(hr);
  13470. //table单独处理
  13471. var node = domUtils.findParentByTagName(range.startContainer, notBreakTags, true),
  13472. parents = [],
  13473. pN;
  13474. if (node) {
  13475. switch (node.tagName) {
  13476. case 'TD':
  13477. pN = node.parentNode;
  13478. if (!pN.previousSibling) {
  13479. var table = domUtils.findParentByTagName(pN, 'table');
  13480. // var tableWrapDiv = table.parentNode;
  13481. // if(tableWrapDiv && tableWrapDiv.nodeType == 1
  13482. // && tableWrapDiv.tagName == 'DIV'
  13483. // && tableWrapDiv.getAttribute('dropdrag')
  13484. // ){
  13485. // domUtils.remove(tableWrapDiv,true);
  13486. // }
  13487. table.parentNode.insertBefore(hr, table);
  13488. parents = domUtils.findParents(hr, true);
  13489. } else {
  13490. pN.parentNode.insertBefore(hr, pN);
  13491. parents = domUtils.findParents(hr);
  13492. }
  13493. pN = parents[1];
  13494. if (hr !== pN) {
  13495. domUtils.breakParent(hr, pN);
  13496. }
  13497. //table要重写绑定一下拖拽
  13498. me.fireEvent('afteradjusttable', me.document);
  13499. }
  13500. } else {
  13501. if (!range.collapsed) {
  13502. range.deleteContents();
  13503. var start = range.startContainer;
  13504. while (!domUtils.isBody(start) && domUtils.isBlockElm(start) && domUtils.isEmptyNode(start)) {
  13505. range.setStartBefore(start).collapse(true);
  13506. domUtils.remove(start);
  13507. start = range.startContainer;
  13508. }
  13509. }
  13510. range.insertNode(hr);
  13511. var pN = hr.parentNode,
  13512. nextNode;
  13513. while (!domUtils.isBody(pN)) {
  13514. domUtils.breakParent(hr, pN);
  13515. nextNode = hr.nextSibling;
  13516. if (nextNode && domUtils.isEmptyBlock(nextNode)) {
  13517. domUtils.remove(nextNode);
  13518. }
  13519. pN = hr.parentNode;
  13520. }
  13521. nextNode = hr.nextSibling;
  13522. var pre = hr.previousSibling;
  13523. if (isHr(pre)) {
  13524. domUtils.remove(pre);
  13525. } else {
  13526. pre && fillNode(pre);
  13527. }
  13528. if (!nextNode) {
  13529. var p = me.document.createElement('p');
  13530. hr.parentNode.appendChild(p);
  13531. domUtils.fillNode(me.document, p);
  13532. range.setStart(p, 0).collapse(true);
  13533. } else {
  13534. if (isHr(nextNode)) {
  13535. domUtils.remove(nextNode);
  13536. } else {
  13537. fillNode(nextNode);
  13538. }
  13539. range.setEndAfter(hr).collapse(false);
  13540. }
  13541. range.select(true);
  13542. }
  13543. }
  13544. };
  13545. };
  13546. // plugins/wordimage.js
  13547. ///import core
  13548. ///commands 本地图片引导上传
  13549. ///commandsName WordImage
  13550. ///commandsTitle 本地图片引导上传
  13551. ///commandsDialog dialogs\wordimage
  13552. UE.plugin.register('wordimage', function () {
  13553. var me = this,
  13554. images = [];
  13555. return {
  13556. commands: {
  13557. 'wordimage': {
  13558. execCommand: function () {
  13559. var images = domUtils.getElementsByTagName(me.body, "img");
  13560. var urlList = [];
  13561. for (var i = 0, ci; ci = images[i++];) {
  13562. var url = ci.getAttribute("word_img");
  13563. url && urlList.push(url);
  13564. }
  13565. return urlList;
  13566. },
  13567. queryCommandState: function () {
  13568. images = domUtils.getElementsByTagName(me.body, "img");
  13569. for (var i = 0, ci; ci = images[i++];) {
  13570. if (ci.getAttribute("word_img")) {
  13571. return 1;
  13572. }
  13573. }
  13574. return -1;
  13575. },
  13576. notNeedUndo: true
  13577. }
  13578. },
  13579. inputRule: function (root) {
  13580. utils.each(root.getNodesByTagName('img'), function (img) {
  13581. var attrs = img.attrs,
  13582. flag = parseInt(attrs.width) < 128 || parseInt(attrs.height) < 43,
  13583. opt = me.options,
  13584. src = opt.UEDITOR_HOME_URL + 'themes/default/images/spacer.gif';
  13585. if (attrs['src'] && /^(?:(file:\/+))/.test(attrs['src'])) {
  13586. img.setAttr({
  13587. width: attrs.width,
  13588. height: attrs.height,
  13589. alt: attrs.alt,
  13590. word_img: attrs.src,
  13591. src: src,
  13592. 'style': 'background:url(' + (flag ? opt.themePath + opt.theme + '/images/word.gif' : opt.langPath + opt.lang + '/images/localimage.png') + ') no-repeat center center;border:1px solid #ddd'
  13593. })
  13594. }
  13595. })
  13596. }
  13597. }
  13598. });
  13599. // plugins/dragdrop.js
  13600. UE.plugins['dragdrop'] = function () {
  13601. var me = this;
  13602. me.ready(function () {
  13603. domUtils.on(this.body, 'dragend', function () {
  13604. var rng = me.selection.getRange();
  13605. var node = rng.getClosedNode() || me.selection.getStart();
  13606. if (node && node.tagName == 'IMG') {
  13607. var pre = node.previousSibling,
  13608. next;
  13609. while (next = node.nextSibling) {
  13610. if (next.nodeType == 1 && next.tagName == 'SPAN' && !next.firstChild) {
  13611. domUtils.remove(next)
  13612. } else {
  13613. break;
  13614. }
  13615. }
  13616. if ((pre && pre.nodeType == 1 && !domUtils.isEmptyBlock(pre) || !pre) && (!next || next && !domUtils.isEmptyBlock(next))) {
  13617. if (pre && pre.tagName == 'P' && !domUtils.isEmptyBlock(pre)) {
  13618. pre.appendChild(node);
  13619. domUtils.moveChild(next, pre);
  13620. domUtils.remove(next);
  13621. } else if (next && next.tagName == 'P' && !domUtils.isEmptyBlock(next)) {
  13622. next.insertBefore(node, next.firstChild);
  13623. }
  13624. if (pre && pre.tagName == 'P' && domUtils.isEmptyBlock(pre)) {
  13625. domUtils.remove(pre)
  13626. }
  13627. if (next && next.tagName == 'P' && domUtils.isEmptyBlock(next)) {
  13628. domUtils.remove(next)
  13629. }
  13630. rng.selectNode(node).select();
  13631. me.fireEvent('saveScene');
  13632. }
  13633. }
  13634. })
  13635. });
  13636. me.addListener('keyup', function (type, evt) {
  13637. var keyCode = evt.keyCode || evt.which;
  13638. if (keyCode == 13) {
  13639. var rng = me.selection.getRange(),
  13640. node;
  13641. if (node = domUtils.findParentByTagName(rng.startContainer, 'p', true)) {
  13642. if (domUtils.getComputedStyle(node, 'text-align') == 'center') {
  13643. domUtils.removeStyle(node, 'text-align')
  13644. }
  13645. }
  13646. }
  13647. })
  13648. };
  13649. // plugins/undo.js
  13650. /**
  13651. * undo redo
  13652. * @file
  13653. * @since 1.2.6.1
  13654. */
  13655. /**
  13656. * 撤销上一次执行的命令
  13657. * @command undo
  13658. * @method execCommand
  13659. * @param { String } cmd 命令字符串
  13660. * @example
  13661. * ```javascript
  13662. * editor.execCommand( 'undo' );
  13663. * ```
  13664. */
  13665. /**
  13666. * 重做上一次执行的命令
  13667. * @command redo
  13668. * @method execCommand
  13669. * @param { String } cmd 命令字符串
  13670. * @example
  13671. * ```javascript
  13672. * editor.execCommand( 'redo' );
  13673. * ```
  13674. */
  13675. UE.plugins['undo'] = function () {
  13676. var saveSceneTimer;
  13677. var me = this,
  13678. maxUndoCount = me.options.maxUndoCount || 20,
  13679. maxInputCount = me.options.maxInputCount || 20,
  13680. fillchar = new RegExp(domUtils.fillChar + '|<\/hr>', 'gi'); // ie会产生多余的</hr>
  13681. var noNeedFillCharTags = {
  13682. ol: 1,
  13683. ul: 1,
  13684. table: 1,
  13685. tbody: 1,
  13686. tr: 1,
  13687. body: 1
  13688. };
  13689. var orgState = me.options.autoClearEmptyNode;
  13690. function compareAddr(indexA, indexB) {
  13691. if (indexA.length != indexB.length)
  13692. return 0;
  13693. for (var i = 0, l = indexA.length; i < l; i++) {
  13694. if (indexA[i] != indexB[i])
  13695. return 0
  13696. }
  13697. return 1;
  13698. }
  13699. function compareRangeAddress(rngAddrA, rngAddrB) {
  13700. if (rngAddrA.collapsed != rngAddrB.collapsed) {
  13701. return 0;
  13702. }
  13703. if (!compareAddr(rngAddrA.startAddress, rngAddrB.startAddress) || !compareAddr(rngAddrA.endAddress, rngAddrB.endAddress)) {
  13704. return 0;
  13705. }
  13706. return 1;
  13707. }
  13708. function UndoManager() {
  13709. this.list = [];
  13710. this.index = 0;
  13711. this.hasUndo = false;
  13712. this.hasRedo = false;
  13713. this.undo = function () {
  13714. if (this.hasUndo) {
  13715. if (!this.list[this.index - 1] && this.list.length == 1) {
  13716. this.reset();
  13717. return;
  13718. }
  13719. while (this.list[this.index].content == this.list[this.index - 1].content) {
  13720. this.index--;
  13721. if (this.index == 0) {
  13722. return this.restore(0);
  13723. }
  13724. }
  13725. this.restore(--this.index);
  13726. }
  13727. };
  13728. this.redo = function () {
  13729. if (this.hasRedo) {
  13730. while (this.list[this.index].content == this.list[this.index + 1].content) {
  13731. this.index++;
  13732. if (this.index == this.list.length - 1) {
  13733. return this.restore(this.index);
  13734. }
  13735. }
  13736. this.restore(++this.index);
  13737. }
  13738. };
  13739. this.restore = function () {
  13740. var me = this.editor;
  13741. var scene = this.list[this.index];
  13742. var root = UE.htmlparser(scene.content.replace(fillchar, ''));
  13743. me.options.autoClearEmptyNode = false;
  13744. me.filterInputRule(root);
  13745. me.options.autoClearEmptyNode = orgState;
  13746. //trace:873
  13747. //去掉展位符
  13748. me.document.body.innerHTML = root.toHtml();
  13749. me.fireEvent('afterscencerestore');
  13750. //处理undo后空格不展位的问题
  13751. if (browser.ie) {
  13752. utils.each(domUtils.getElementsByTagName(me.document, 'td th caption p'), function (node) {
  13753. if (domUtils.isEmptyNode(node)) {
  13754. domUtils.fillNode(me.document, node);
  13755. }
  13756. })
  13757. }
  13758. try {
  13759. var rng = new dom.Range(me.document).moveToAddress(scene.address);
  13760. rng.select(noNeedFillCharTags[rng.startContainer.nodeName.toLowerCase()]);
  13761. } catch (e) {}
  13762. this.update();
  13763. this.clearKey();
  13764. //不能把自己reset了
  13765. me.fireEvent('reset', true);
  13766. };
  13767. this.getScene = function () {
  13768. var me = this.editor;
  13769. var rng = me.selection.getRange(),
  13770. rngAddress = rng.createAddress(false, true);
  13771. me.fireEvent('beforegetscene');
  13772. var root = UE.htmlparser(me.body.innerHTML);
  13773. me.options.autoClearEmptyNode = false;
  13774. me.filterOutputRule(root);
  13775. me.options.autoClearEmptyNode = orgState;
  13776. var cont = root.toHtml();
  13777. //trace:3461
  13778. //这个会引起回退时导致空格丢失的情况
  13779. // browser.ie && (cont = cont.replace(/>&nbsp;</g, '><').replace(/\s*</g, '<').replace(/>\s*/g, '>'));
  13780. me.fireEvent('aftergetscene');
  13781. return {
  13782. address: rngAddress,
  13783. content: cont
  13784. }
  13785. };
  13786. this.save = function (notCompareRange, notSetCursor) {
  13787. clearTimeout(saveSceneTimer);
  13788. var currentScene = this.getScene(notSetCursor),
  13789. lastScene = this.list[this.index];
  13790. if (lastScene && lastScene.content != currentScene.content) {
  13791. me.trigger('contentchange')
  13792. }
  13793. //内容相同位置相同不存
  13794. if (lastScene && lastScene.content == currentScene.content &&
  13795. (notCompareRange ? 1 : compareRangeAddress(lastScene.address, currentScene.address))
  13796. ) {
  13797. return;
  13798. }
  13799. this.list = this.list.slice(0, this.index + 1);
  13800. this.list.push(currentScene);
  13801. //如果大于最大数量了,就把最前的剔除
  13802. if (this.list.length > maxUndoCount) {
  13803. this.list.shift();
  13804. }
  13805. this.index = this.list.length - 1;
  13806. this.clearKey();
  13807. //跟新undo/redo状态
  13808. this.update();
  13809. };
  13810. this.update = function () {
  13811. this.hasRedo = !!this.list[this.index + 1];
  13812. this.hasUndo = !!this.list[this.index - 1];
  13813. };
  13814. this.reset = function () {
  13815. this.list = [];
  13816. this.index = 0;
  13817. this.hasUndo = false;
  13818. this.hasRedo = false;
  13819. this.clearKey();
  13820. };
  13821. this.clearKey = function () {
  13822. keycont = 0;
  13823. lastKeyCode = null;
  13824. };
  13825. }
  13826. me.undoManger = new UndoManager();
  13827. me.undoManger.editor = me;
  13828. function saveScene() {
  13829. this.undoManger.save();
  13830. }
  13831. me.addListener('saveScene', function () {
  13832. var args = Array.prototype.splice.call(arguments, 1);
  13833. this.undoManger.save.apply(this.undoManger, args);
  13834. });
  13835. // me.addListener('beforeexeccommand', saveScene);
  13836. // me.addListener('afterexeccommand', saveScene);
  13837. me.addListener('reset', function (type, exclude) {
  13838. if (!exclude) {
  13839. this.undoManger.reset();
  13840. }
  13841. });
  13842. me.commands['redo'] = me.commands['undo'] = {
  13843. execCommand: function (cmdName) {
  13844. this.undoManger[cmdName]();
  13845. },
  13846. queryCommandState: function (cmdName) {
  13847. return this.undoManger['has' + (cmdName.toLowerCase() == 'undo' ? 'Undo' : 'Redo')] ? 0 : -1;
  13848. },
  13849. notNeedUndo: 1
  13850. };
  13851. var keys = {
  13852. // /*Backspace*/ 8:1, /*Delete*/ 46:1,
  13853. /*Shift*/
  13854. 16: 1,
  13855. /*Ctrl*/ 17: 1,
  13856. /*Alt*/ 18: 1,
  13857. 37: 1,
  13858. 38: 1,
  13859. 39: 1,
  13860. 40: 1
  13861. },
  13862. keycont = 0,
  13863. lastKeyCode;
  13864. //输入法状态下不计算字符数
  13865. var inputType = false;
  13866. me.addListener('ready', function () {
  13867. domUtils.on(this.body, 'compositionstart', function () {
  13868. inputType = true;
  13869. });
  13870. domUtils.on(this.body, 'compositionend', function () {
  13871. inputType = false;
  13872. })
  13873. });
  13874. //快捷键
  13875. me.addshortcutkey({
  13876. "Undo": "ctrl+90", //undo
  13877. "Redo": "ctrl+89" //redo
  13878. });
  13879. var isCollapsed = true;
  13880. me.addListener('keydown', function (type, evt) {
  13881. var me = this;
  13882. var keyCode = evt.keyCode || evt.which;
  13883. if (!keys[keyCode] && !evt.ctrlKey && !evt.metaKey && !evt.shiftKey && !evt.altKey) {
  13884. if (inputType)
  13885. return;
  13886. if (!me.selection.getRange().collapsed) {
  13887. me.undoManger.save(false, true);
  13888. isCollapsed = false;
  13889. return;
  13890. }
  13891. if (me.undoManger.list.length == 0) {
  13892. me.undoManger.save(true);
  13893. }
  13894. clearTimeout(saveSceneTimer);
  13895. function save(cont) {
  13896. cont.undoManger.save(false, true);
  13897. cont.fireEvent('selectionchange');
  13898. }
  13899. saveSceneTimer = setTimeout(function () {
  13900. if (inputType) {
  13901. var interalTimer = setInterval(function () {
  13902. if (!inputType) {
  13903. save(me);
  13904. clearInterval(interalTimer)
  13905. }
  13906. }, 300)
  13907. return;
  13908. }
  13909. save(me);
  13910. }, 200);
  13911. lastKeyCode = keyCode;
  13912. keycont++;
  13913. if (keycont >= maxInputCount) {
  13914. save(me)
  13915. }
  13916. }
  13917. });
  13918. me.addListener('keyup', function (type, evt) {
  13919. var keyCode = evt.keyCode || evt.which;
  13920. if (!keys[keyCode] && !evt.ctrlKey && !evt.metaKey && !evt.shiftKey && !evt.altKey) {
  13921. if (inputType)
  13922. return;
  13923. if (!isCollapsed) {
  13924. this.undoManger.save(false, true);
  13925. isCollapsed = true;
  13926. }
  13927. }
  13928. });
  13929. //扩展实例,添加关闭和开启命令undo
  13930. me.stopCmdUndo = function () {
  13931. me.__hasEnterExecCommand = true;
  13932. };
  13933. me.startCmdUndo = function () {
  13934. me.__hasEnterExecCommand = false;
  13935. }
  13936. };
  13937. // plugins/copy.js
  13938. UE.plugin.register('copy', function () {
  13939. var me = this;
  13940. function initZeroClipboard() {
  13941. ZeroClipboard.config({
  13942. debug: false,
  13943. swfPath: me.options.UEDITOR_HOME_URL + 'third-party/zeroclipboard/ZeroClipboard.swf'
  13944. });
  13945. var client = me.zeroclipboard = new ZeroClipboard();
  13946. // 复制内容
  13947. client.on('copy', function (e) {
  13948. var client = e.client,
  13949. rng = me.selection.getRange(),
  13950. div = document.createElement('div');
  13951. div.appendChild(rng.cloneContents());
  13952. client.setText(div.innerText || div.textContent);
  13953. client.setHtml(div.innerHTML);
  13954. rng.select();
  13955. });
  13956. // hover事件传递到target
  13957. client.on('mouseover mouseout', function (e) {
  13958. var target = e.target;
  13959. if (e.type == 'mouseover') {
  13960. domUtils.addClass(target, 'edui-state-hover');
  13961. } else if (e.type == 'mouseout') {
  13962. domUtils.removeClasses(target, 'edui-state-hover');
  13963. }
  13964. });
  13965. // flash加载不成功
  13966. client.on('wrongflash noflash', function () {
  13967. ZeroClipboard.destroy();
  13968. });
  13969. }
  13970. return {
  13971. bindEvents: {
  13972. 'ready': function () {
  13973. if (!browser.ie) {
  13974. if (window.ZeroClipboard) {
  13975. initZeroClipboard();
  13976. } else {
  13977. utils.loadFile(document, {
  13978. src: me.options.UEDITOR_HOME_URL + "third-party/zeroclipboard/ZeroClipboard.js",
  13979. tag: "script",
  13980. type: "text/javascript",
  13981. defer: "defer"
  13982. }, function () {
  13983. initZeroClipboard();
  13984. });
  13985. }
  13986. }
  13987. }
  13988. },
  13989. commands: {
  13990. 'copy': {
  13991. execCommand: function (cmd) {
  13992. if (!me.document.execCommand('copy')) {
  13993. alert(me.getLang('copymsg'));
  13994. }
  13995. }
  13996. }
  13997. }
  13998. }
  13999. });
  14000. // plugins/paste.js
  14001. ///import core
  14002. ///import plugins/inserthtml.js
  14003. ///import plugins/undo.js
  14004. ///import plugins/serialize.js
  14005. ///commands 粘贴
  14006. ///commandsName PastePlain
  14007. ///commandsTitle 纯文本粘贴模式
  14008. /**
  14009. * @description 粘贴
  14010. * @author zhanyi
  14011. */
  14012. UE.plugins['paste'] = function () {
  14013. function getClipboardData(callback) {
  14014. var doc = this.document;
  14015. if (doc.getElementById('baidu_pastebin')) {
  14016. return;
  14017. }
  14018. var range = this.selection.getRange(),
  14019. bk = range.createBookmark(),
  14020. //创建剪贴的容器div
  14021. pastebin = doc.createElement('div');
  14022. pastebin.id = 'baidu_pastebin';
  14023. // Safari 要求div必须有内容,才能粘贴内容进来
  14024. browser.webkit && pastebin.appendChild(doc.createTextNode(domUtils.fillChar + domUtils.fillChar));
  14025. doc.body.appendChild(pastebin);
  14026. //trace:717 隐藏的span不能得到top
  14027. //bk.start.innerHTML = '&nbsp;';
  14028. bk.start.style.display = '';
  14029. pastebin.style.cssText = "position:absolute;width:1px;height:1px;overflow:hidden;left:-1000px;white-space:nowrap;top:" +
  14030. //要在现在光标平行的位置加入,否则会出现跳动的问题
  14031. domUtils.getXY(bk.start).y + 'px';
  14032. range.selectNodeContents(pastebin).select(true);
  14033. setTimeout(function () {
  14034. if (browser.webkit) {
  14035. for (var i = 0, pastebins = doc.querySelectorAll('#baidu_pastebin'), pi; pi = pastebins[i++];) {
  14036. if (domUtils.isEmptyNode(pi)) {
  14037. domUtils.remove(pi);
  14038. } else {
  14039. pastebin = pi;
  14040. break;
  14041. }
  14042. }
  14043. }
  14044. try {
  14045. pastebin.parentNode.removeChild(pastebin);
  14046. } catch (e) {}
  14047. range.moveToBookmark(bk).select(true);
  14048. callback(pastebin);
  14049. }, 0);
  14050. }
  14051. var me = this;
  14052. me.setOpt({
  14053. retainOnlyLabelPasted: false
  14054. });
  14055. var txtContent, htmlContent, address;
  14056. function getPureHtml(html) {
  14057. return html.replace(/<(\/?)([\w\-]+)([^>]*)>/gi, function (a, b, tagName, attrs) {
  14058. tagName = tagName.toLowerCase();
  14059. if ({
  14060. img: 1
  14061. } [tagName]) {
  14062. return a;
  14063. }
  14064. attrs = attrs.replace(/([\w\-]*?)\s*=\s*(("([^"]*)")|('([^']*)')|([^\s>]+))/gi, function (str, atr, val) {
  14065. if ({
  14066. 'src': 1,
  14067. 'href': 1,
  14068. 'name': 1
  14069. } [atr.toLowerCase()]) {
  14070. return atr + '=' + val + ' '
  14071. }
  14072. return ''
  14073. });
  14074. if ({
  14075. 'span': 1,
  14076. 'div': 1
  14077. } [tagName]) {
  14078. return ''
  14079. } else {
  14080. return '<' + b + tagName + ' ' + utils.trim(attrs) + '>'
  14081. }
  14082. });
  14083. }
  14084. function filter(div) {
  14085. var html;
  14086. if (div.firstChild) {
  14087. //去掉cut中添加的边界值
  14088. var nodes = domUtils.getElementsByTagName(div, 'span');
  14089. for (var i = 0, ni; ni = nodes[i++];) {
  14090. if (ni.id == '_baidu_cut_start' || ni.id == '_baidu_cut_end') {
  14091. domUtils.remove(ni);
  14092. }
  14093. }
  14094. if (browser.webkit) {
  14095. var brs = div.querySelectorAll('div br');
  14096. for (var i = 0, bi; bi = brs[i++];) {
  14097. var pN = bi.parentNode;
  14098. if (pN.tagName == 'DIV' && pN.childNodes.length == 1) {
  14099. pN.innerHTML = '<p><br/></p>';
  14100. domUtils.remove(pN);
  14101. }
  14102. }
  14103. var divs = div.querySelectorAll('#baidu_pastebin');
  14104. for (var i = 0, di; di = divs[i++];) {
  14105. var tmpP = me.document.createElement('p');
  14106. di.parentNode.insertBefore(tmpP, di);
  14107. while (di.firstChild) {
  14108. tmpP.appendChild(di.firstChild);
  14109. }
  14110. domUtils.remove(di);
  14111. }
  14112. var metas = div.querySelectorAll('meta');
  14113. for (var i = 0, ci; ci = metas[i++];) {
  14114. domUtils.remove(ci);
  14115. }
  14116. var brs = div.querySelectorAll('br');
  14117. for (i = 0; ci = brs[i++];) {
  14118. if (/^apple-/i.test(ci.className)) {
  14119. domUtils.remove(ci);
  14120. }
  14121. }
  14122. }
  14123. if (browser.gecko) {
  14124. var dirtyNodes = div.querySelectorAll('[_moz_dirty]');
  14125. for (i = 0; ci = dirtyNodes[i++];) {
  14126. ci.removeAttribute('_moz_dirty');
  14127. }
  14128. }
  14129. if (!browser.ie) {
  14130. var spans = div.querySelectorAll('span.Apple-style-span');
  14131. for (var i = 0, ci; ci = spans[i++];) {
  14132. domUtils.remove(ci, true);
  14133. }
  14134. }
  14135. //ie下使用innerHTML会产生多余的\r\n字符,也会产生&nbsp;这里过滤掉
  14136. html = div.innerHTML; //.replace(/>(?:(\s|&nbsp;)*?)</g,'><');
  14137. //过滤word粘贴过来的冗余属性
  14138. html = UE.filterWord(html);
  14139. //取消了忽略空白的第二个参数,粘贴过来的有些是有空白的,会被套上相关的标签
  14140. var root = UE.htmlparser(html);
  14141. //如果给了过滤规则就先进行过滤
  14142. if (me.options.filterRules) {
  14143. UE.filterNode(root, me.options.filterRules);
  14144. }
  14145. //执行默认的处理
  14146. me.filterInputRule(root);
  14147. //针对chrome的处理
  14148. if (browser.webkit) {
  14149. var br = root.lastChild();
  14150. if (br && br.type == 'element' && br.tagName == 'br') {
  14151. root.removeChild(br)
  14152. }
  14153. utils.each(me.body.querySelectorAll('div'), function (node) {
  14154. if (domUtils.isEmptyBlock(node)) {
  14155. domUtils.remove(node, true)
  14156. }
  14157. })
  14158. }
  14159. html = {
  14160. 'html': root.toHtml()
  14161. };
  14162. me.fireEvent('beforepaste', html, root);
  14163. //抢了默认的粘贴,那后边的内容就不执行了,比如表格粘贴
  14164. if (!html.html) {
  14165. return;
  14166. }
  14167. root = UE.htmlparser(html.html, true);
  14168. //如果开启了纯文本模式
  14169. if (me.queryCommandState('pasteplain') === 1) {
  14170. me.execCommand('insertHtml', UE.filterNode(root, me.options.filterTxtRules).toHtml(), true);
  14171. } else {
  14172. //文本模式
  14173. UE.filterNode(root, me.options.filterTxtRules);
  14174. txtContent = root.toHtml();
  14175. //完全模式
  14176. htmlContent = html.html;
  14177. address = me.selection.getRange().createAddress(true);
  14178. me.execCommand('insertHtml', me.getOpt('retainOnlyLabelPasted') === true ? getPureHtml(htmlContent) : htmlContent, true);
  14179. }
  14180. me.fireEvent("afterpaste", html);
  14181. }
  14182. }
  14183. me.addListener('pasteTransfer', function (cmd, plainType) {
  14184. if (address && txtContent && htmlContent && txtContent != htmlContent) {
  14185. var range = me.selection.getRange();
  14186. range.moveToAddress(address, true);
  14187. if (!range.collapsed) {
  14188. while (!domUtils.isBody(range.startContainer)) {
  14189. var start = range.startContainer;
  14190. if (start.nodeType == 1) {
  14191. start = start.childNodes[range.startOffset];
  14192. if (!start) {
  14193. range.setStartBefore(range.startContainer);
  14194. continue;
  14195. }
  14196. var pre = start.previousSibling;
  14197. if (pre && pre.nodeType == 3 && new RegExp('^[\n\r\t ' + domUtils.fillChar + ']*$').test(pre.nodeValue)) {
  14198. range.setStartBefore(pre)
  14199. }
  14200. }
  14201. if (range.startOffset == 0) {
  14202. range.setStartBefore(range.startContainer);
  14203. } else {
  14204. break;
  14205. }
  14206. }
  14207. while (!domUtils.isBody(range.endContainer)) {
  14208. var end = range.endContainer;
  14209. if (end.nodeType == 1) {
  14210. end = end.childNodes[range.endOffset];
  14211. if (!end) {
  14212. range.setEndAfter(range.endContainer);
  14213. continue;
  14214. }
  14215. var next = end.nextSibling;
  14216. if (next && next.nodeType == 3 && new RegExp('^[\n\r\t' + domUtils.fillChar + ']*$').test(next.nodeValue)) {
  14217. range.setEndAfter(next)
  14218. }
  14219. }
  14220. if (range.endOffset == range.endContainer[range.endContainer.nodeType == 3 ? 'nodeValue' : 'childNodes'].length) {
  14221. range.setEndAfter(range.endContainer);
  14222. } else {
  14223. break;
  14224. }
  14225. }
  14226. }
  14227. range.deleteContents();
  14228. range.select(true);
  14229. me.__hasEnterExecCommand = true;
  14230. var html = htmlContent;
  14231. if (plainType === 2) {
  14232. html = getPureHtml(html);
  14233. } else if (plainType) {
  14234. html = txtContent;
  14235. }
  14236. me.execCommand('inserthtml', html, true);
  14237. me.__hasEnterExecCommand = false;
  14238. var rng = me.selection.getRange();
  14239. while (!domUtils.isBody(rng.startContainer) && !rng.startOffset &&
  14240. rng.startContainer[rng.startContainer.nodeType == 3 ? 'nodeValue' : 'childNodes'].length
  14241. ) {
  14242. rng.setStartBefore(rng.startContainer);
  14243. }
  14244. var tmpAddress = rng.createAddress(true);
  14245. address.endAddress = tmpAddress.startAddress;
  14246. }
  14247. });
  14248. me.addListener('ready', function () {
  14249. domUtils.on(me.body, 'cut', function () {
  14250. var range = me.selection.getRange();
  14251. if (!range.collapsed && me.undoManger) {
  14252. me.undoManger.save();
  14253. }
  14254. });
  14255. //ie下beforepaste在点击右键时也会触发,所以用监控键盘才处理
  14256. domUtils.on(me.body, browser.ie || browser.opera ? 'keydown' : 'paste', function (e) {
  14257. if ((browser.ie || browser.opera) && ((!e.ctrlKey && !e.metaKey) || e.keyCode != '86')) {
  14258. return;
  14259. }
  14260. getClipboardData.call(me, function (div) {
  14261. filter(div);
  14262. });
  14263. });
  14264. });
  14265. me.commands['paste'] = {
  14266. execCommand: function (cmd) {
  14267. if (browser.ie) {
  14268. getClipboardData.call(me, function (div) {
  14269. filter(div);
  14270. });
  14271. me.document.execCommand('paste');
  14272. } else {
  14273. alert(me.getLang('pastemsg'));
  14274. }
  14275. }
  14276. }
  14277. };
  14278. // plugins/puretxtpaste.js
  14279. /**
  14280. * 纯文本粘贴插件
  14281. * @file
  14282. * @since 1.2.6.1
  14283. */
  14284. UE.plugins['pasteplain'] = function () {
  14285. var me = this;
  14286. me.setOpt({
  14287. 'pasteplain': false,
  14288. 'filterTxtRules': function () {
  14289. function transP(node) {
  14290. node.tagName = 'p';
  14291. node.setStyle();
  14292. }
  14293. function removeNode(node) {
  14294. node.parentNode.removeChild(node, true)
  14295. }
  14296. return {
  14297. //直接删除及其字节点内容
  14298. '-': 'script style object iframe embed input select',
  14299. 'p': {
  14300. $: {}
  14301. },
  14302. 'br': {
  14303. $: {}
  14304. },
  14305. div: function (node) {
  14306. var tmpNode, p = UE.uNode.createElement('p');
  14307. while (tmpNode = node.firstChild()) {
  14308. if (tmpNode.type == 'text' || !UE.dom.dtd.$block[tmpNode.tagName]) {
  14309. p.appendChild(tmpNode);
  14310. } else {
  14311. if (p.firstChild()) {
  14312. node.parentNode.insertBefore(p, node);
  14313. p = UE.uNode.createElement('p');
  14314. } else {
  14315. node.parentNode.insertBefore(tmpNode, node);
  14316. }
  14317. }
  14318. }
  14319. if (p.firstChild()) {
  14320. node.parentNode.insertBefore(p, node);
  14321. }
  14322. node.parentNode.removeChild(node);
  14323. },
  14324. ol: removeNode,
  14325. ul: removeNode,
  14326. dl: removeNode,
  14327. dt: removeNode,
  14328. dd: removeNode,
  14329. 'li': removeNode,
  14330. 'caption': transP,
  14331. 'th': transP,
  14332. 'tr': transP,
  14333. 'h1': transP,
  14334. 'h2': transP,
  14335. 'h3': transP,
  14336. 'h4': transP,
  14337. 'h5': transP,
  14338. 'h6': transP,
  14339. 'td': function (node) {
  14340. //没有内容的td直接删掉
  14341. var txt = !!node.innerText();
  14342. if (txt) {
  14343. node.parentNode.insertAfter(UE.uNode.createText(' &nbsp; &nbsp;'), node);
  14344. }
  14345. node.parentNode.removeChild(node, node.innerText())
  14346. }
  14347. }
  14348. }()
  14349. });
  14350. //暂时这里支持一下老版本的属性
  14351. var pasteplain = me.options.pasteplain;
  14352. /**
  14353. * 启用或取消纯文本粘贴模式
  14354. * @command pasteplain
  14355. * @method execCommand
  14356. * @param { String } cmd 命令字符串
  14357. * @example
  14358. * ```javascript
  14359. * editor.queryCommandState( 'pasteplain' );
  14360. * ```
  14361. */
  14362. /**
  14363. * 查询当前是否处于纯文本粘贴模式
  14364. * @command pasteplain
  14365. * @method queryCommandState
  14366. * @param { String } cmd 命令字符串
  14367. * @return { int } 如果处于纯文本模式,返回1,否则,返回0
  14368. * @example
  14369. * ```javascript
  14370. * editor.queryCommandState( 'pasteplain' );
  14371. * ```
  14372. */
  14373. me.commands['pasteplain'] = {
  14374. queryCommandState: function () {
  14375. return pasteplain ? 1 : 0;
  14376. },
  14377. execCommand: function () {
  14378. pasteplain = !pasteplain | 0;
  14379. },
  14380. notNeedUndo: 1
  14381. };
  14382. };
  14383. // plugins/list.js
  14384. /**
  14385. * 有序列表,无序列表插件
  14386. * @file
  14387. * @since 1.2.6.1
  14388. */
  14389. UE.plugins['list'] = function () {
  14390. var me = this,
  14391. notExchange = {
  14392. 'TD': 1,
  14393. 'PRE': 1,
  14394. 'BLOCKQUOTE': 1
  14395. };
  14396. var customStyle = {
  14397. 'cn': 'cn-1-',
  14398. 'cn1': 'cn-2-',
  14399. 'cn2': 'cn-3-',
  14400. 'num': 'num-1-',
  14401. 'num1': 'num-2-',
  14402. 'num2': 'num-3-',
  14403. 'dash': 'dash',
  14404. 'dot': 'dot'
  14405. };
  14406. me.setOpt({
  14407. 'autoTransWordToList': false,
  14408. 'insertorderedlist': {
  14409. 'num': '',
  14410. 'num1': '',
  14411. 'num2': '',
  14412. 'cn': '',
  14413. 'cn1': '',
  14414. 'cn2': '',
  14415. 'decimal': '',
  14416. 'lower-alpha': '',
  14417. 'lower-roman': '',
  14418. 'upper-alpha': '',
  14419. 'upper-roman': ''
  14420. },
  14421. 'insertunorderedlist': {
  14422. 'circle': '',
  14423. 'disc': '',
  14424. 'square': '',
  14425. 'dash': '',
  14426. 'dot': ''
  14427. },
  14428. listDefaultPaddingLeft: '30',
  14429. listiconpath: 'http://bs.baidu.com/listicon/',
  14430. maxListLevel: -1, //-1不限制
  14431. disablePInList: false
  14432. });
  14433. function listToArray(list) {
  14434. var arr = [];
  14435. for (var p in list) {
  14436. arr.push(p)
  14437. }
  14438. return arr;
  14439. }
  14440. var listStyle = {
  14441. 'OL': listToArray(me.options.insertorderedlist),
  14442. 'UL': listToArray(me.options.insertunorderedlist)
  14443. };
  14444. var liiconpath = me.options.listiconpath;
  14445. //根据用户配置,调整customStyle
  14446. for (var s in customStyle) {
  14447. if (!me.options.insertorderedlist.hasOwnProperty(s) && !me.options.insertunorderedlist.hasOwnProperty(s)) {
  14448. delete customStyle[s];
  14449. }
  14450. }
  14451. me.ready(function () {
  14452. var customCss = [];
  14453. for (var p in customStyle) {
  14454. if (p == 'dash' || p == 'dot') {
  14455. customCss.push('li.list-' + customStyle[p] + '{background-image:url(' + liiconpath + customStyle[p] + '.gif)}');
  14456. customCss.push('ul.custom_' + p + '{list-style:none;}ul.custom_' + p + ' li{background-position:0 3px;background-repeat:no-repeat}');
  14457. } else {
  14458. for (var i = 0; i < 99; i++) {
  14459. customCss.push('li.list-' + customStyle[p] + i + '{background-image:url(' + liiconpath + 'list-' + customStyle[p] + i + '.gif)}')
  14460. }
  14461. customCss.push('ol.custom_' + p + '{list-style:none;}ol.custom_' + p + ' li{background-position:0 3px;background-repeat:no-repeat}');
  14462. }
  14463. switch (p) {
  14464. case 'cn':
  14465. customCss.push('li.list-' + p + '-paddingleft-1{padding-left:25px}');
  14466. customCss.push('li.list-' + p + '-paddingleft-2{padding-left:40px}');
  14467. customCss.push('li.list-' + p + '-paddingleft-3{padding-left:55px}');
  14468. break;
  14469. case 'cn1':
  14470. customCss.push('li.list-' + p + '-paddingleft-1{padding-left:30px}');
  14471. customCss.push('li.list-' + p + '-paddingleft-2{padding-left:40px}');
  14472. customCss.push('li.list-' + p + '-paddingleft-3{padding-left:55px}');
  14473. break;
  14474. case 'cn2':
  14475. customCss.push('li.list-' + p + '-paddingleft-1{padding-left:40px}');
  14476. customCss.push('li.list-' + p + '-paddingleft-2{padding-left:55px}');
  14477. customCss.push('li.list-' + p + '-paddingleft-3{padding-left:68px}');
  14478. break;
  14479. case 'num':
  14480. case 'num1':
  14481. customCss.push('li.list-' + p + '-paddingleft-1{padding-left:25px}');
  14482. break;
  14483. case 'num2':
  14484. customCss.push('li.list-' + p + '-paddingleft-1{padding-left:35px}');
  14485. customCss.push('li.list-' + p + '-paddingleft-2{padding-left:40px}');
  14486. break;
  14487. case 'dash':
  14488. customCss.push('li.list-' + p + '-paddingleft{padding-left:35px}');
  14489. break;
  14490. case 'dot':
  14491. customCss.push('li.list-' + p + '-paddingleft{padding-left:20px}');
  14492. }
  14493. }
  14494. customCss.push('.list-paddingleft-1{padding-left:0}');
  14495. customCss.push('.list-paddingleft-2{padding-left:' + me.options.listDefaultPaddingLeft + 'px}');
  14496. customCss.push('.list-paddingleft-3{padding-left:' + me.options.listDefaultPaddingLeft * 2 + 'px}');
  14497. //如果不给宽度会在自定应样式里出现滚动条
  14498. utils.cssRule('list', 'ol,ul{margin:0;pading:0;' + (browser.ie ? '' : 'width:95%') + '}li{clear:both;}' + customCss.join('\n'), me.document);
  14499. });
  14500. //单独处理剪切的问题
  14501. me.ready(function () {
  14502. domUtils.on(me.body, 'cut', function () {
  14503. setTimeout(function () {
  14504. var rng = me.selection.getRange(),
  14505. li;
  14506. //trace:3416
  14507. if (!rng.collapsed) {
  14508. if (li = domUtils.findParentByTagName(rng.startContainer, 'li', true)) {
  14509. if (!li.nextSibling && domUtils.isEmptyBlock(li)) {
  14510. var pn = li.parentNode,
  14511. node;
  14512. if (node = pn.previousSibling) {
  14513. domUtils.remove(pn);
  14514. rng.setStartAtLast(node).collapse(true);
  14515. rng.select(true);
  14516. } else if (node = pn.nextSibling) {
  14517. domUtils.remove(pn);
  14518. rng.setStartAtFirst(node).collapse(true);
  14519. rng.select(true);
  14520. } else {
  14521. var tmpNode = me.document.createElement('p');
  14522. domUtils.fillNode(me.document, tmpNode);
  14523. pn.parentNode.insertBefore(tmpNode, pn);
  14524. domUtils.remove(pn);
  14525. rng.setStart(tmpNode, 0).collapse(true);
  14526. rng.select(true);
  14527. }
  14528. }
  14529. }
  14530. }
  14531. })
  14532. })
  14533. });
  14534. function getStyle(node) {
  14535. var cls = node.className;
  14536. if (domUtils.hasClass(node, /custom_/)) {
  14537. return cls.match(/custom_(\w+)/)[1]
  14538. }
  14539. return domUtils.getStyle(node, 'list-style-type')
  14540. }
  14541. me.addListener('beforepaste', function (type, html) {
  14542. var me = this,
  14543. rng = me.selection.getRange(),
  14544. li;
  14545. var root = UE.htmlparser(html.html, true);
  14546. if (li = domUtils.findParentByTagName(rng.startContainer, 'li', true)) {
  14547. var list = li.parentNode,
  14548. tagName = list.tagName == 'OL' ? 'ul' : 'ol';
  14549. utils.each(root.getNodesByTagName(tagName), function (n) {
  14550. n.tagName = list.tagName;
  14551. n.setAttr();
  14552. if (n.parentNode === root) {
  14553. type = getStyle(list) || (list.tagName == 'OL' ? 'decimal' : 'disc')
  14554. } else {
  14555. var className = n.parentNode.getAttr('class');
  14556. if (className && /custom_/.test(className)) {
  14557. type = className.match(/custom_(\w+)/)[1]
  14558. } else {
  14559. type = n.parentNode.getStyle('list-style-type');
  14560. }
  14561. if (!type) {
  14562. type = list.tagName == 'OL' ? 'decimal' : 'disc';
  14563. }
  14564. }
  14565. var index = utils.indexOf(listStyle[list.tagName], type);
  14566. if (n.parentNode !== root)
  14567. index = index + 1 == listStyle[list.tagName].length ? 0 : index + 1;
  14568. var currentStyle = listStyle[list.tagName][index];
  14569. if (customStyle[currentStyle]) {
  14570. n.setAttr('class', 'custom_' + currentStyle)
  14571. } else {
  14572. n.setStyle('list-style-type', currentStyle)
  14573. }
  14574. })
  14575. }
  14576. html.html = root.toHtml();
  14577. });
  14578. //导出时,去掉p标签
  14579. me.getOpt('disablePInList') === true && me.addOutputRule(function (root) {
  14580. utils.each(root.getNodesByTagName('li'), function (li) {
  14581. var newChildrens = [],
  14582. index = 0;
  14583. utils.each(li.children, function (n) {
  14584. if (n.tagName == 'p') {
  14585. var tmpNode;
  14586. while (tmpNode = n.children.pop()) {
  14587. newChildrens.splice(index, 0, tmpNode);
  14588. tmpNode.parentNode = li;
  14589. lastNode = tmpNode;
  14590. }
  14591. tmpNode = newChildrens[newChildrens.length - 1];
  14592. if (!tmpNode || tmpNode.type != 'element' || tmpNode.tagName != 'br') {
  14593. var br = UE.uNode.createElement('br');
  14594. br.parentNode = li;
  14595. newChildrens.push(br);
  14596. }
  14597. index = newChildrens.length;
  14598. }
  14599. });
  14600. if (newChildrens.length) {
  14601. li.children = newChildrens;
  14602. }
  14603. });
  14604. });
  14605. //进入编辑器的li要套p标签
  14606. me.addInputRule(function (root) {
  14607. utils.each(root.getNodesByTagName('li'), function (li) {
  14608. var tmpP = UE.uNode.createElement('p');
  14609. for (var i = 0, ci; ci = li.children[i];) {
  14610. if (ci.type == 'text' || dtd.p[ci.tagName]) {
  14611. tmpP.appendChild(ci);
  14612. } else {
  14613. if (tmpP.firstChild()) {
  14614. li.insertBefore(tmpP, ci);
  14615. tmpP = UE.uNode.createElement('p');
  14616. i = i + 2;
  14617. } else {
  14618. i++;
  14619. }
  14620. }
  14621. }
  14622. if (tmpP.firstChild() && !tmpP.parentNode || !li.firstChild()) {
  14623. li.appendChild(tmpP);
  14624. }
  14625. //trace:3357
  14626. //p不能为空
  14627. if (!tmpP.firstChild()) {
  14628. tmpP.innerHTML(browser.ie ? '&nbsp;' : '<br/>')
  14629. }
  14630. //去掉末尾的空白
  14631. var p = li.firstChild();
  14632. var lastChild = p.lastChild();
  14633. if (lastChild && lastChild.type == 'text' && /^\s*$/.test(lastChild.data)) {
  14634. p.removeChild(lastChild)
  14635. }
  14636. });
  14637. if (me.options.autoTransWordToList) {
  14638. var orderlisttype = {
  14639. 'num1': /^\d+\)/,
  14640. 'decimal': /^\d+\./,
  14641. 'lower-alpha': /^[a-z]+\)/,
  14642. 'upper-alpha': /^[A-Z]+\./,
  14643. 'cn': /^[\u4E00\u4E8C\u4E09\u56DB\u516d\u4e94\u4e03\u516b\u4e5d]+[\u3001]/,
  14644. 'cn2': /^\([\u4E00\u4E8C\u4E09\u56DB\u516d\u4e94\u4e03\u516b\u4e5d]+\)/
  14645. },
  14646. unorderlisttype = {
  14647. 'square': 'n'
  14648. };
  14649. function checkListType(content, container) {
  14650. var span = container.firstChild();
  14651. if (span && span.type == 'element' && span.tagName == 'span' && /Wingdings|Symbol/.test(span.getStyle('font-family'))) {
  14652. for (var p in unorderlisttype) {
  14653. if (unorderlisttype[p] == span.data) {
  14654. return p
  14655. }
  14656. }
  14657. return 'disc'
  14658. }
  14659. for (var p in orderlisttype) {
  14660. if (orderlisttype[p].test(content)) {
  14661. return p;
  14662. }
  14663. }
  14664. }
  14665. utils.each(root.getNodesByTagName('p'), function (node) {
  14666. if (node.getAttr('class') != 'MsoListParagraph') {
  14667. return
  14668. }
  14669. //word粘贴过来的会带有margin要去掉,但这样也可能会误命中一些央视
  14670. node.setStyle('margin', '');
  14671. node.setStyle('margin-left', '');
  14672. node.setAttr('class', '');
  14673. function appendLi(list, p, type) {
  14674. if (list.tagName == 'ol') {
  14675. if (browser.ie) {
  14676. var first = p.firstChild();
  14677. if (first.type == 'element' && first.tagName == 'span' && orderlisttype[type].test(first.innerText())) {
  14678. p.removeChild(first);
  14679. }
  14680. } else {
  14681. p.innerHTML(p.innerHTML().replace(orderlisttype[type], ''));
  14682. }
  14683. } else {
  14684. p.removeChild(p.firstChild())
  14685. }
  14686. var li = UE.uNode.createElement('li');
  14687. li.appendChild(p);
  14688. list.appendChild(li);
  14689. }
  14690. var tmp = node,
  14691. type, cacheNode = node;
  14692. if (node.parentNode.tagName != 'li' && (type = checkListType(node.innerText(), node))) {
  14693. var list = UE.uNode.createElement(me.options.insertorderedlist.hasOwnProperty(type) ? 'ol' : 'ul');
  14694. if (customStyle[type]) {
  14695. list.setAttr('class', 'custom_' + type)
  14696. } else {
  14697. list.setStyle('list-style-type', type)
  14698. }
  14699. while (node && node.parentNode.tagName != 'li' && checkListType(node.innerText(), node)) {
  14700. tmp = node.nextSibling();
  14701. if (!tmp) {
  14702. node.parentNode.insertBefore(list, node)
  14703. }
  14704. appendLi(list, node, type);
  14705. node = tmp;
  14706. }
  14707. if (!list.parentNode && node && node.parentNode) {
  14708. node.parentNode.insertBefore(list, node)
  14709. }
  14710. }
  14711. var span = cacheNode.firstChild();
  14712. if (span && span.type == 'element' && span.tagName == 'span' && /^\s*(&nbsp;)+\s*$/.test(span.innerText())) {
  14713. span.parentNode.removeChild(span)
  14714. }
  14715. })
  14716. }
  14717. });
  14718. //调整索引标签
  14719. me.addListener('contentchange', function () {
  14720. adjustListStyle(me.document)
  14721. });
  14722. function adjustListStyle(doc, ignore) {
  14723. utils.each(domUtils.getElementsByTagName(doc, 'ol ul'), function (node) {
  14724. if (!domUtils.inDoc(node, doc))
  14725. return;
  14726. var parent = node.parentNode;
  14727. if (parent.tagName == node.tagName) {
  14728. var nodeStyleType = getStyle(node) || (node.tagName == 'OL' ? 'decimal' : 'disc'),
  14729. parentStyleType = getStyle(parent) || (parent.tagName == 'OL' ? 'decimal' : 'disc');
  14730. if (nodeStyleType == parentStyleType) {
  14731. var styleIndex = utils.indexOf(listStyle[node.tagName], nodeStyleType);
  14732. styleIndex = styleIndex + 1 == listStyle[node.tagName].length ? 0 : styleIndex + 1;
  14733. setListStyle(node, listStyle[node.tagName][styleIndex])
  14734. }
  14735. }
  14736. var index = 0,
  14737. type = 2;
  14738. if (domUtils.hasClass(node, /custom_/)) {
  14739. if (!(/[ou]l/i.test(parent.tagName) && domUtils.hasClass(parent, /custom_/))) {
  14740. type = 1;
  14741. }
  14742. } else {
  14743. if (/[ou]l/i.test(parent.tagName) && domUtils.hasClass(parent, /custom_/)) {
  14744. type = 3;
  14745. }
  14746. }
  14747. var style = domUtils.getStyle(node, 'list-style-type');
  14748. style && (node.style.cssText = 'list-style-type:' + style);
  14749. node.className = utils.trim(node.className.replace(/list-paddingleft-\w+/, '')) + ' list-paddingleft-' + type;
  14750. utils.each(domUtils.getElementsByTagName(node, 'li'), function (li) {
  14751. li.style.cssText && (li.style.cssText = '');
  14752. if (!li.firstChild) {
  14753. domUtils.remove(li);
  14754. return;
  14755. }
  14756. if (li.parentNode !== node) {
  14757. return;
  14758. }
  14759. index++;
  14760. if (domUtils.hasClass(node, /custom_/)) {
  14761. var paddingLeft = 1,
  14762. currentStyle = getStyle(node);
  14763. if (node.tagName == 'OL') {
  14764. if (currentStyle) {
  14765. switch (currentStyle) {
  14766. case 'cn':
  14767. case 'cn1':
  14768. case 'cn2':
  14769. if (index > 10 && (index % 10 == 0 || index > 10 && index < 20)) {
  14770. paddingLeft = 2
  14771. } else if (index > 20) {
  14772. paddingLeft = 3
  14773. }
  14774. break;
  14775. case 'num2':
  14776. if (index > 9) {
  14777. paddingLeft = 2
  14778. }
  14779. }
  14780. }
  14781. li.className = 'list-' + customStyle[currentStyle] + index + ' ' + 'list-' + currentStyle + '-paddingleft-' + paddingLeft;
  14782. } else {
  14783. li.className = 'list-' + customStyle[currentStyle] + ' ' + 'list-' + currentStyle + '-paddingleft';
  14784. }
  14785. } else {
  14786. li.className = li.className.replace(/list-[\w\-]+/gi, '');
  14787. }
  14788. var className = li.getAttribute('class');
  14789. if (className !== null && !className.replace(/\s/g, '')) {
  14790. domUtils.removeAttributes(li, 'class')
  14791. }
  14792. });
  14793. !ignore && adjustList(node, node.tagName.toLowerCase(), getStyle(node) || domUtils.getStyle(node, 'list-style-type'), true);
  14794. })
  14795. }
  14796. function adjustList(list, tag, style, ignoreEmpty) {
  14797. var nextList = list.nextSibling;
  14798. if (nextList && nextList.nodeType == 1 && nextList.tagName.toLowerCase() == tag && (getStyle(nextList) || domUtils.getStyle(nextList, 'list-style-type') || (tag == 'ol' ? 'decimal' : 'disc')) == style) {
  14799. domUtils.moveChild(nextList, list);
  14800. if (nextList.childNodes.length == 0) {
  14801. domUtils.remove(nextList);
  14802. }
  14803. }
  14804. if (nextList && domUtils.isFillChar(nextList)) {
  14805. domUtils.remove(nextList);
  14806. }
  14807. var preList = list.previousSibling;
  14808. if (preList && preList.nodeType == 1 && preList.tagName.toLowerCase() == tag && (getStyle(preList) || domUtils.getStyle(preList, 'list-style-type') || (tag == 'ol' ? 'decimal' : 'disc')) == style) {
  14809. domUtils.moveChild(list, preList);
  14810. }
  14811. if (preList && domUtils.isFillChar(preList)) {
  14812. domUtils.remove(preList);
  14813. }!ignoreEmpty && domUtils.isEmptyBlock(list) && domUtils.remove(list);
  14814. if (getStyle(list)) {
  14815. adjustListStyle(list.ownerDocument, true)
  14816. }
  14817. }
  14818. function setListStyle(list, style) {
  14819. if (customStyle[style]) {
  14820. list.className = 'custom_' + style;
  14821. }
  14822. try {
  14823. domUtils.setStyle(list, 'list-style-type', style);
  14824. } catch (e) {}
  14825. }
  14826. function clearEmptySibling(node) {
  14827. var tmpNode = node.previousSibling;
  14828. if (tmpNode && domUtils.isEmptyBlock(tmpNode)) {
  14829. domUtils.remove(tmpNode);
  14830. }
  14831. tmpNode = node.nextSibling;
  14832. if (tmpNode && domUtils.isEmptyBlock(tmpNode)) {
  14833. domUtils.remove(tmpNode);
  14834. }
  14835. }
  14836. me.addListener('keydown', function (type, evt) {
  14837. function preventAndSave() {
  14838. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
  14839. me.fireEvent('contentchange');
  14840. me.undoManger && me.undoManger.save();
  14841. }
  14842. function findList(node, filterFn) {
  14843. while (node && !domUtils.isBody(node)) {
  14844. if (filterFn(node)) {
  14845. return null
  14846. }
  14847. if (node.nodeType == 1 && /[ou]l/i.test(node.tagName)) {
  14848. return node;
  14849. }
  14850. node = node.parentNode;
  14851. }
  14852. return null;
  14853. }
  14854. var keyCode = evt.keyCode || evt.which;
  14855. if (keyCode == 13 && !evt.shiftKey) { //回车
  14856. var rng = me.selection.getRange(),
  14857. parent = domUtils.findParent(rng.startContainer, function (node) {
  14858. return domUtils.isBlockElm(node)
  14859. }, true),
  14860. li = domUtils.findParentByTagName(rng.startContainer, 'li', true);
  14861. if (parent && parent.tagName != 'PRE' && !li) {
  14862. var html = parent.innerHTML.replace(new RegExp(domUtils.fillChar, 'g'), '');
  14863. if (/^\s*1\s*\.[^\d]/.test(html)) {
  14864. parent.innerHTML = html.replace(/^\s*1\s*\./, '');
  14865. rng.setStartAtLast(parent).collapse(true).select();
  14866. me.__hasEnterExecCommand = true;
  14867. me.execCommand('insertorderedlist');
  14868. me.__hasEnterExecCommand = false;
  14869. }
  14870. }
  14871. var range = me.selection.getRange(),
  14872. start = findList(range.startContainer, function (node) {
  14873. return node.tagName == 'TABLE';
  14874. }),
  14875. end = range.collapsed ? start : findList(range.endContainer, function (node) {
  14876. return node.tagName == 'TABLE';
  14877. });
  14878. if (start && end && start === end) {
  14879. if (!range.collapsed) {
  14880. start = domUtils.findParentByTagName(range.startContainer, 'li', true);
  14881. end = domUtils.findParentByTagName(range.endContainer, 'li', true);
  14882. if (start && end && start === end) {
  14883. range.deleteContents();
  14884. li = domUtils.findParentByTagName(range.startContainer, 'li', true);
  14885. if (li && domUtils.isEmptyBlock(li)) {
  14886. pre = li.previousSibling;
  14887. next = li.nextSibling;
  14888. p = me.document.createElement('p');
  14889. domUtils.fillNode(me.document, p);
  14890. parentList = li.parentNode;
  14891. if (pre && next) {
  14892. range.setStart(next, 0).collapse(true).select(true);
  14893. domUtils.remove(li);
  14894. } else {
  14895. if (!pre && !next || !pre) {
  14896. parentList.parentNode.insertBefore(p, parentList);
  14897. } else {
  14898. li.parentNode.parentNode.insertBefore(p, parentList.nextSibling);
  14899. }
  14900. domUtils.remove(li);
  14901. if (!parentList.firstChild) {
  14902. domUtils.remove(parentList);
  14903. }
  14904. range.setStart(p, 0).setCursor();
  14905. }
  14906. preventAndSave();
  14907. return;
  14908. }
  14909. } else {
  14910. var tmpRange = range.cloneRange(),
  14911. bk = tmpRange.collapse(false).createBookmark();
  14912. range.deleteContents();
  14913. tmpRange.moveToBookmark(bk);
  14914. var li = domUtils.findParentByTagName(tmpRange.startContainer, 'li', true);
  14915. clearEmptySibling(li);
  14916. tmpRange.select();
  14917. preventAndSave();
  14918. return;
  14919. }
  14920. }
  14921. li = domUtils.findParentByTagName(range.startContainer, 'li', true);
  14922. if (li) {
  14923. if (domUtils.isEmptyBlock(li)) {
  14924. bk = range.createBookmark();
  14925. var parentList = li.parentNode;
  14926. if (li !== parentList.lastChild) {
  14927. domUtils.breakParent(li, parentList);
  14928. clearEmptySibling(li);
  14929. } else {
  14930. parentList.parentNode.insertBefore(li, parentList.nextSibling);
  14931. if (domUtils.isEmptyNode(parentList)) {
  14932. domUtils.remove(parentList);
  14933. }
  14934. }
  14935. //嵌套不处理
  14936. if (!dtd.$list[li.parentNode.tagName]) {
  14937. if (!domUtils.isBlockElm(li.firstChild)) {
  14938. p = me.document.createElement('p');
  14939. li.parentNode.insertBefore(p, li);
  14940. while (li.firstChild) {
  14941. p.appendChild(li.firstChild);
  14942. }
  14943. domUtils.remove(li);
  14944. } else {
  14945. domUtils.remove(li, true);
  14946. }
  14947. }
  14948. range.moveToBookmark(bk).select();
  14949. } else {
  14950. var first = li.firstChild;
  14951. if (!first || !domUtils.isBlockElm(first)) {
  14952. var p = me.document.createElement('p');
  14953. !li.firstChild && domUtils.fillNode(me.document, p);
  14954. while (li.firstChild) {
  14955. p.appendChild(li.firstChild);
  14956. }
  14957. li.appendChild(p);
  14958. first = p;
  14959. }
  14960. var span = me.document.createElement('span');
  14961. range.insertNode(span);
  14962. domUtils.breakParent(span, li);
  14963. var nextLi = span.nextSibling;
  14964. first = nextLi.firstChild;
  14965. if (!first) {
  14966. p = me.document.createElement('p');
  14967. domUtils.fillNode(me.document, p);
  14968. nextLi.appendChild(p);
  14969. first = p;
  14970. }
  14971. if (domUtils.isEmptyNode(first)) {
  14972. first.innerHTML = '';
  14973. domUtils.fillNode(me.document, first);
  14974. }
  14975. range.setStart(first, 0).collapse(true).shrinkBoundary().select();
  14976. domUtils.remove(span);
  14977. var pre = nextLi.previousSibling;
  14978. if (pre && domUtils.isEmptyBlock(pre)) {
  14979. pre.innerHTML = '<p></p>';
  14980. domUtils.fillNode(me.document, pre.firstChild);
  14981. }
  14982. }
  14983. // }
  14984. preventAndSave();
  14985. }
  14986. }
  14987. }
  14988. if (keyCode == 8) {
  14989. //修中ie中li下的问题
  14990. range = me.selection.getRange();
  14991. if (range.collapsed && domUtils.isStartInblock(range)) {
  14992. tmpRange = range.cloneRange().trimBoundary();
  14993. li = domUtils.findParentByTagName(range.startContainer, 'li', true);
  14994. //要在li的最左边,才能处理
  14995. if (li && domUtils.isStartInblock(tmpRange)) {
  14996. start = domUtils.findParentByTagName(range.startContainer, 'p', true);
  14997. if (start && start !== li.firstChild) {
  14998. var parentList = domUtils.findParentByTagName(start, ['ol', 'ul']);
  14999. domUtils.breakParent(start, parentList);
  15000. clearEmptySibling(start);
  15001. me.fireEvent('contentchange');
  15002. range.setStart(start, 0).setCursor(false, true);
  15003. me.fireEvent('saveScene');
  15004. domUtils.preventDefault(evt);
  15005. return;
  15006. }
  15007. if (li && (pre = li.previousSibling)) {
  15008. if (keyCode == 46 && li.childNodes.length) {
  15009. return;
  15010. }
  15011. //有可能上边的兄弟节点是个2级菜单,要追加到2级菜单的最后的li
  15012. if (dtd.$list[pre.tagName]) {
  15013. pre = pre.lastChild;
  15014. }
  15015. me.undoManger && me.undoManger.save();
  15016. first = li.firstChild;
  15017. if (domUtils.isBlockElm(first)) {
  15018. if (domUtils.isEmptyNode(first)) {
  15019. // range.setEnd(pre, pre.childNodes.length).shrinkBoundary().collapse().select(true);
  15020. pre.appendChild(first);
  15021. range.setStart(first, 0).setCursor(false, true);
  15022. //first不是唯一的节点
  15023. while (li.firstChild) {
  15024. pre.appendChild(li.firstChild);
  15025. }
  15026. } else {
  15027. span = me.document.createElement('span');
  15028. range.insertNode(span);
  15029. //判断pre是否是空的节点,如果是<p><br/></p>类型的空节点,干掉p标签防止它占位
  15030. if (domUtils.isEmptyBlock(pre)) {
  15031. pre.innerHTML = '';
  15032. }
  15033. domUtils.moveChild(li, pre);
  15034. range.setStartBefore(span).collapse(true).select(true);
  15035. domUtils.remove(span);
  15036. }
  15037. } else {
  15038. if (domUtils.isEmptyNode(li)) {
  15039. var p = me.document.createElement('p');
  15040. pre.appendChild(p);
  15041. range.setStart(p, 0).setCursor();
  15042. // range.setEnd(pre, pre.childNodes.length).shrinkBoundary().collapse().select(true);
  15043. } else {
  15044. range.setEnd(pre, pre.childNodes.length).collapse().select(true);
  15045. while (li.firstChild) {
  15046. pre.appendChild(li.firstChild);
  15047. }
  15048. }
  15049. }
  15050. domUtils.remove(li);
  15051. me.fireEvent('contentchange');
  15052. me.fireEvent('saveScene');
  15053. domUtils.preventDefault(evt);
  15054. return;
  15055. }
  15056. //trace:980
  15057. if (li && !li.previousSibling) {
  15058. var parentList = li.parentNode;
  15059. var bk = range.createBookmark();
  15060. if (domUtils.isTagNode(parentList.parentNode, 'ol ul')) {
  15061. parentList.parentNode.insertBefore(li, parentList);
  15062. if (domUtils.isEmptyNode(parentList)) {
  15063. domUtils.remove(parentList)
  15064. }
  15065. } else {
  15066. while (li.firstChild) {
  15067. parentList.parentNode.insertBefore(li.firstChild, parentList);
  15068. }
  15069. domUtils.remove(li);
  15070. if (domUtils.isEmptyNode(parentList)) {
  15071. domUtils.remove(parentList)
  15072. }
  15073. }
  15074. range.moveToBookmark(bk).setCursor(false, true);
  15075. me.fireEvent('contentchange');
  15076. me.fireEvent('saveScene');
  15077. domUtils.preventDefault(evt);
  15078. return;
  15079. }
  15080. }
  15081. }
  15082. }
  15083. });
  15084. me.addListener('keyup', function (type, evt) {
  15085. var keyCode = evt.keyCode || evt.which;
  15086. if (keyCode == 8) {
  15087. var rng = me.selection.getRange(),
  15088. list;
  15089. if (list = domUtils.findParentByTagName(rng.startContainer, ['ol', 'ul'], true)) {
  15090. adjustList(list, list.tagName.toLowerCase(), getStyle(list) || domUtils.getComputedStyle(list, 'list-style-type'), true)
  15091. }
  15092. }
  15093. });
  15094. //处理tab键
  15095. me.addListener('tabkeydown', function () {
  15096. var range = me.selection.getRange();
  15097. //控制级数
  15098. function checkLevel(li) {
  15099. if (me.options.maxListLevel != -1) {
  15100. var level = li.parentNode,
  15101. levelNum = 0;
  15102. while (/[ou]l/i.test(level.tagName)) {
  15103. levelNum++;
  15104. level = level.parentNode;
  15105. }
  15106. if (levelNum >= me.options.maxListLevel) {
  15107. return true;
  15108. }
  15109. }
  15110. }
  15111. //只以开始为准
  15112. //todo 后续改进
  15113. var li = domUtils.findParentByTagName(range.startContainer, 'li', true);
  15114. if (li) {
  15115. var bk;
  15116. if (range.collapsed) {
  15117. if (checkLevel(li))
  15118. return true;
  15119. var parentLi = li.parentNode,
  15120. list = me.document.createElement(parentLi.tagName),
  15121. index = utils.indexOf(listStyle[list.tagName], getStyle(parentLi) || domUtils.getComputedStyle(parentLi, 'list-style-type'));
  15122. index = index + 1 == listStyle[list.tagName].length ? 0 : index + 1;
  15123. var currentStyle = listStyle[list.tagName][index];
  15124. setListStyle(list, currentStyle);
  15125. if (domUtils.isStartInblock(range)) {
  15126. me.fireEvent('saveScene');
  15127. bk = range.createBookmark();
  15128. parentLi.insertBefore(list, li);
  15129. list.appendChild(li);
  15130. adjustList(list, list.tagName.toLowerCase(), currentStyle);
  15131. me.fireEvent('contentchange');
  15132. range.moveToBookmark(bk).select(true);
  15133. return true;
  15134. }
  15135. } else {
  15136. me.fireEvent('saveScene');
  15137. bk = range.createBookmark();
  15138. for (var i = 0, closeList, parents = domUtils.findParents(li), ci; ci = parents[i++];) {
  15139. if (domUtils.isTagNode(ci, 'ol ul')) {
  15140. closeList = ci;
  15141. break;
  15142. }
  15143. }
  15144. var current = li;
  15145. if (bk.end) {
  15146. while (current && !(domUtils.getPosition(current, bk.end) & domUtils.POSITION_FOLLOWING)) {
  15147. if (checkLevel(current)) {
  15148. current = domUtils.getNextDomNode(current, false, null, function (node) {
  15149. return node !== closeList
  15150. });
  15151. continue;
  15152. }
  15153. var parentLi = current.parentNode,
  15154. list = me.document.createElement(parentLi.tagName),
  15155. index = utils.indexOf(listStyle[list.tagName], getStyle(parentLi) || domUtils.getComputedStyle(parentLi, 'list-style-type'));
  15156. var currentIndex = index + 1 == listStyle[list.tagName].length ? 0 : index + 1;
  15157. var currentStyle = listStyle[list.tagName][currentIndex];
  15158. setListStyle(list, currentStyle);
  15159. parentLi.insertBefore(list, current);
  15160. while (current && !(domUtils.getPosition(current, bk.end) & domUtils.POSITION_FOLLOWING)) {
  15161. li = current.nextSibling;
  15162. list.appendChild(current);
  15163. if (!li || domUtils.isTagNode(li, 'ol ul')) {
  15164. if (li) {
  15165. while (li = li.firstChild) {
  15166. if (li.tagName == 'LI') {
  15167. break;
  15168. }
  15169. }
  15170. } else {
  15171. li = domUtils.getNextDomNode(current, false, null, function (node) {
  15172. return node !== closeList
  15173. });
  15174. }
  15175. break;
  15176. }
  15177. current = li;
  15178. }
  15179. adjustList(list, list.tagName.toLowerCase(), currentStyle);
  15180. current = li;
  15181. }
  15182. }
  15183. me.fireEvent('contentchange');
  15184. range.moveToBookmark(bk).select();
  15185. return true;
  15186. }
  15187. }
  15188. });
  15189. function getLi(start) {
  15190. while (start && !domUtils.isBody(start)) {
  15191. if (start.nodeName == 'TABLE') {
  15192. return null;
  15193. }
  15194. if (start.nodeName == 'LI') {
  15195. return start
  15196. }
  15197. start = start.parentNode;
  15198. }
  15199. }
  15200. /**
  15201. * 有序列表,与“insertunorderedlist”命令互斥
  15202. * @command insertorderedlist
  15203. * @method execCommand
  15204. * @param { String } command 命令字符串
  15205. * @param { String } style 插入的有序列表类型,值为:decimal,lower-alpha,lower-roman,upper-alpha,upper-roman,cn,cn1,cn2,num,num1,num2
  15206. * @example
  15207. * ```javascript
  15208. * editor.execCommand( 'insertorderedlist','decimal');
  15209. * ```
  15210. */
  15211. /**
  15212. * 查询当前选区内容是否有序列表
  15213. * @command insertorderedlist
  15214. * @method queryCommandState
  15215. * @param { String } cmd 命令字符串
  15216. * @return { int } 如果当前选区是有序列表返回1,否则返回0
  15217. * @example
  15218. * ```javascript
  15219. * editor.queryCommandState( 'insertorderedlist' );
  15220. * ```
  15221. */
  15222. /**
  15223. * 查询当前选区内容是否有序列表
  15224. * @command insertorderedlist
  15225. * @method queryCommandValue
  15226. * @param { String } cmd 命令字符串
  15227. * @return { String } 返回当前有序列表的类型,值为null或decimal,lower-alpha,lower-roman,upper-alpha,upper-roman,cn,cn1,cn2,num,num1,num2
  15228. * @example
  15229. * ```javascript
  15230. * editor.queryCommandValue( 'insertorderedlist' );
  15231. * ```
  15232. */
  15233. /**
  15234. * 无序列表,与“insertorderedlist”命令互斥
  15235. * @command insertunorderedlist
  15236. * @method execCommand
  15237. * @param { String } command 命令字符串
  15238. * @param { String } style 插入的无序列表类型,值为:circle,disc,square,dash,dot
  15239. * @example
  15240. * ```javascript
  15241. * editor.execCommand( 'insertunorderedlist','circle');
  15242. * ```
  15243. */
  15244. /**
  15245. * 查询当前是否有word文档粘贴进来的图片
  15246. * @command insertunorderedlist
  15247. * @method insertunorderedlist
  15248. * @param { String } command 命令字符串
  15249. * @return { int } 如果当前选区是无序列表返回1,否则返回0
  15250. * @example
  15251. * ```javascript
  15252. * editor.queryCommandState( 'insertunorderedlist' );
  15253. * ```
  15254. */
  15255. /**
  15256. * 查询当前选区内容是否有序列表
  15257. * @command insertunorderedlist
  15258. * @method queryCommandValue
  15259. * @param { String } command 命令字符串
  15260. * @return { String } 返回当前无序列表的类型,值为null或circle,disc,square,dash,dot
  15261. * @example
  15262. * ```javascript
  15263. * editor.queryCommandValue( 'insertunorderedlist' );
  15264. * ```
  15265. */
  15266. me.commands['insertorderedlist'] =
  15267. me.commands['insertunorderedlist'] = {
  15268. execCommand: function (command, style) {
  15269. if (!style) {
  15270. style = command.toLowerCase() == 'insertorderedlist' ? 'decimal' : 'disc';
  15271. }
  15272. var me = this,
  15273. range = this.selection.getRange(),
  15274. filterFn = function (node) {
  15275. return node.nodeType == 1 ? node.tagName.toLowerCase() != 'br' : !domUtils.isWhitespace(node);
  15276. },
  15277. tag = command.toLowerCase() == 'insertorderedlist' ? 'ol' : 'ul',
  15278. frag = me.document.createDocumentFragment();
  15279. //去掉是因为会出现选到末尾,导致adjustmentBoundary缩到ol/ul的位置
  15280. //range.shrinkBoundary();//.adjustmentBoundary();
  15281. range.adjustmentBoundary().shrinkBoundary();
  15282. var bko = range.createBookmark(true),
  15283. start = getLi(me.document.getElementById(bko.start)),
  15284. modifyStart = 0,
  15285. end = getLi(me.document.getElementById(bko.end)),
  15286. modifyEnd = 0,
  15287. startParent, endParent,
  15288. list, tmp;
  15289. if (start || end) {
  15290. start && (startParent = start.parentNode);
  15291. if (!bko.end) {
  15292. end = start;
  15293. }
  15294. end && (endParent = end.parentNode);
  15295. if (startParent === endParent) {
  15296. while (start !== end) {
  15297. tmp = start;
  15298. start = start.nextSibling;
  15299. if (!domUtils.isBlockElm(tmp.firstChild)) {
  15300. var p = me.document.createElement('p');
  15301. while (tmp.firstChild) {
  15302. p.appendChild(tmp.firstChild);
  15303. }
  15304. tmp.appendChild(p);
  15305. }
  15306. frag.appendChild(tmp);
  15307. }
  15308. tmp = me.document.createElement('span');
  15309. startParent.insertBefore(tmp, end);
  15310. if (!domUtils.isBlockElm(end.firstChild)) {
  15311. p = me.document.createElement('p');
  15312. while (end.firstChild) {
  15313. p.appendChild(end.firstChild);
  15314. }
  15315. end.appendChild(p);
  15316. }
  15317. frag.appendChild(end);
  15318. domUtils.breakParent(tmp, startParent);
  15319. if (domUtils.isEmptyNode(tmp.previousSibling)) {
  15320. domUtils.remove(tmp.previousSibling);
  15321. }
  15322. if (domUtils.isEmptyNode(tmp.nextSibling)) {
  15323. domUtils.remove(tmp.nextSibling)
  15324. }
  15325. var nodeStyle = getStyle(startParent) || domUtils.getComputedStyle(startParent, 'list-style-type') || (command.toLowerCase() == 'insertorderedlist' ? 'decimal' : 'disc');
  15326. if (startParent.tagName.toLowerCase() == tag && nodeStyle == style) {
  15327. for (var i = 0, ci, tmpFrag = me.document.createDocumentFragment(); ci = frag.firstChild;) {
  15328. if (domUtils.isTagNode(ci, 'ol ul')) {
  15329. // 删除时,子列表不处理
  15330. // utils.each(domUtils.getElementsByTagName(ci,'li'),function(li){
  15331. // while(li.firstChild){
  15332. // tmpFrag.appendChild(li.firstChild);
  15333. // }
  15334. //
  15335. // });
  15336. tmpFrag.appendChild(ci);
  15337. } else {
  15338. while (ci.firstChild) {
  15339. tmpFrag.appendChild(ci.firstChild);
  15340. domUtils.remove(ci);
  15341. }
  15342. }
  15343. }
  15344. tmp.parentNode.insertBefore(tmpFrag, tmp);
  15345. } else {
  15346. list = me.document.createElement(tag);
  15347. setListStyle(list, style);
  15348. list.appendChild(frag);
  15349. tmp.parentNode.insertBefore(list, tmp);
  15350. }
  15351. domUtils.remove(tmp);
  15352. list && adjustList(list, tag, style);
  15353. range.moveToBookmark(bko).select();
  15354. return;
  15355. }
  15356. //开始
  15357. if (start) {
  15358. while (start) {
  15359. tmp = start.nextSibling;
  15360. if (domUtils.isTagNode(start, 'ol ul')) {
  15361. frag.appendChild(start);
  15362. } else {
  15363. var tmpfrag = me.document.createDocumentFragment(),
  15364. hasBlock = 0;
  15365. while (start.firstChild) {
  15366. if (domUtils.isBlockElm(start.firstChild)) {
  15367. hasBlock = 1;
  15368. }
  15369. tmpfrag.appendChild(start.firstChild);
  15370. }
  15371. if (!hasBlock) {
  15372. var tmpP = me.document.createElement('p');
  15373. tmpP.appendChild(tmpfrag);
  15374. frag.appendChild(tmpP);
  15375. } else {
  15376. frag.appendChild(tmpfrag);
  15377. }
  15378. domUtils.remove(start);
  15379. }
  15380. start = tmp;
  15381. }
  15382. startParent.parentNode.insertBefore(frag, startParent.nextSibling);
  15383. if (domUtils.isEmptyNode(startParent)) {
  15384. range.setStartBefore(startParent);
  15385. domUtils.remove(startParent);
  15386. } else {
  15387. range.setStartAfter(startParent);
  15388. }
  15389. modifyStart = 1;
  15390. }
  15391. if (end && domUtils.inDoc(endParent, me.document)) {
  15392. //结束
  15393. start = endParent.firstChild;
  15394. while (start && start !== end) {
  15395. tmp = start.nextSibling;
  15396. if (domUtils.isTagNode(start, 'ol ul')) {
  15397. frag.appendChild(start);
  15398. } else {
  15399. tmpfrag = me.document.createDocumentFragment();
  15400. hasBlock = 0;
  15401. while (start.firstChild) {
  15402. if (domUtils.isBlockElm(start.firstChild)) {
  15403. hasBlock = 1;
  15404. }
  15405. tmpfrag.appendChild(start.firstChild);
  15406. }
  15407. if (!hasBlock) {
  15408. tmpP = me.document.createElement('p');
  15409. tmpP.appendChild(tmpfrag);
  15410. frag.appendChild(tmpP);
  15411. } else {
  15412. frag.appendChild(tmpfrag);
  15413. }
  15414. domUtils.remove(start);
  15415. }
  15416. start = tmp;
  15417. }
  15418. var tmpDiv = domUtils.createElement(me.document, 'div', {
  15419. 'tmpDiv': 1
  15420. });
  15421. domUtils.moveChild(end, tmpDiv);
  15422. frag.appendChild(tmpDiv);
  15423. domUtils.remove(end);
  15424. endParent.parentNode.insertBefore(frag, endParent);
  15425. range.setEndBefore(endParent);
  15426. if (domUtils.isEmptyNode(endParent)) {
  15427. domUtils.remove(endParent);
  15428. }
  15429. modifyEnd = 1;
  15430. }
  15431. }
  15432. if (!modifyStart) {
  15433. range.setStartBefore(me.document.getElementById(bko.start));
  15434. }
  15435. if (bko.end && !modifyEnd) {
  15436. range.setEndAfter(me.document.getElementById(bko.end));
  15437. }
  15438. range.enlarge(true, function (node) {
  15439. return notExchange[node.tagName];
  15440. });
  15441. frag = me.document.createDocumentFragment();
  15442. var bk = range.createBookmark(),
  15443. current = domUtils.getNextDomNode(bk.start, false, filterFn),
  15444. tmpRange = range.cloneRange(),
  15445. tmpNode,
  15446. block = domUtils.isBlockElm;
  15447. while (current && current !== bk.end && (domUtils.getPosition(current, bk.end) & domUtils.POSITION_PRECEDING)) {
  15448. if (current.nodeType == 3 || dtd.li[current.tagName]) {
  15449. if (current.nodeType == 1 && dtd.$list[current.tagName]) {
  15450. while (current.firstChild) {
  15451. frag.appendChild(current.firstChild);
  15452. }
  15453. tmpNode = domUtils.getNextDomNode(current, false, filterFn);
  15454. domUtils.remove(current);
  15455. current = tmpNode;
  15456. continue;
  15457. }
  15458. tmpNode = current;
  15459. tmpRange.setStartBefore(current);
  15460. while (current && current !== bk.end && (!block(current) || domUtils.isBookmarkNode(current))) {
  15461. tmpNode = current;
  15462. current = domUtils.getNextDomNode(current, false, null, function (node) {
  15463. return !notExchange[node.tagName];
  15464. });
  15465. }
  15466. if (current && block(current)) {
  15467. tmp = domUtils.getNextDomNode(tmpNode, false, filterFn);
  15468. if (tmp && domUtils.isBookmarkNode(tmp)) {
  15469. current = domUtils.getNextDomNode(tmp, false, filterFn);
  15470. tmpNode = tmp;
  15471. }
  15472. }
  15473. tmpRange.setEndAfter(tmpNode);
  15474. current = domUtils.getNextDomNode(tmpNode, false, filterFn);
  15475. var li = range.document.createElement('li');
  15476. li.appendChild(tmpRange.extractContents());
  15477. if (domUtils.isEmptyNode(li)) {
  15478. var tmpNode = range.document.createElement('p');
  15479. while (li.firstChild) {
  15480. tmpNode.appendChild(li.firstChild)
  15481. }
  15482. li.appendChild(tmpNode);
  15483. }
  15484. frag.appendChild(li);
  15485. } else {
  15486. current = domUtils.getNextDomNode(current, true, filterFn);
  15487. }
  15488. }
  15489. range.moveToBookmark(bk).collapse(true);
  15490. list = me.document.createElement(tag);
  15491. setListStyle(list, style);
  15492. list.appendChild(frag);
  15493. range.insertNode(list);
  15494. //当前list上下看能否合并
  15495. adjustList(list, tag, style);
  15496. //去掉冗余的tmpDiv
  15497. for (var i = 0, ci, tmpDivs = domUtils.getElementsByTagName(list, 'div'); ci = tmpDivs[i++];) {
  15498. if (ci.getAttribute('tmpDiv')) {
  15499. domUtils.remove(ci, true)
  15500. }
  15501. }
  15502. range.moveToBookmark(bko).select();
  15503. },
  15504. queryCommandState: function (command) {
  15505. var tag = command.toLowerCase() == 'insertorderedlist' ? 'ol' : 'ul';
  15506. var path = this.selection.getStartElementPath();
  15507. for (var i = 0, ci; ci = path[i++];) {
  15508. if (ci.nodeName == 'TABLE') {
  15509. return 0
  15510. }
  15511. if (tag == ci.nodeName.toLowerCase()) {
  15512. return 1
  15513. };
  15514. }
  15515. return 0;
  15516. },
  15517. queryCommandValue: function (command) {
  15518. var tag = command.toLowerCase() == 'insertorderedlist' ? 'ol' : 'ul';
  15519. var path = this.selection.getStartElementPath(),
  15520. node;
  15521. for (var i = 0, ci; ci = path[i++];) {
  15522. if (ci.nodeName == 'TABLE') {
  15523. node = null;
  15524. break;
  15525. }
  15526. if (tag == ci.nodeName.toLowerCase()) {
  15527. node = ci;
  15528. break;
  15529. };
  15530. }
  15531. return node ? getStyle(node) || domUtils.getComputedStyle(node, 'list-style-type') : null;
  15532. }
  15533. };
  15534. };
  15535. // plugins/source.js
  15536. /**
  15537. * 源码编辑插件
  15538. * @file
  15539. * @since 1.2.6.1
  15540. */
  15541. (function () {
  15542. var sourceEditors = {
  15543. textarea: function (editor, holder) {
  15544. var textarea = holder.ownerDocument.createElement('textarea');
  15545. textarea.style.cssText = 'position:absolute;resize:none;width:100%;height:100%;border:0;padding:0;margin:0;overflow-y:auto;';
  15546. // todo: IE下只有onresize属性可用... 很纠结
  15547. if (browser.ie && browser.version < 8) {
  15548. textarea.style.width = holder.offsetWidth + 'px';
  15549. textarea.style.height = holder.offsetHeight + 'px';
  15550. holder.onresize = function () {
  15551. textarea.style.width = holder.offsetWidth + 'px';
  15552. textarea.style.height = holder.offsetHeight + 'px';
  15553. };
  15554. }
  15555. holder.appendChild(textarea);
  15556. return {
  15557. setContent: function (content) {
  15558. textarea.value = content;
  15559. },
  15560. getContent: function () {
  15561. return textarea.value;
  15562. },
  15563. select: function () {
  15564. var range;
  15565. if (browser.ie) {
  15566. range = textarea.createTextRange();
  15567. range.collapse(true);
  15568. range.select();
  15569. } else {
  15570. //todo: chrome下无法设置焦点
  15571. textarea.setSelectionRange(0, 0);
  15572. textarea.focus();
  15573. }
  15574. },
  15575. dispose: function () {
  15576. holder.removeChild(textarea);
  15577. // todo
  15578. holder.onresize = null;
  15579. textarea = null;
  15580. holder = null;
  15581. }
  15582. };
  15583. },
  15584. codemirror: function (editor, holder) {
  15585. var codeEditor = window.CodeMirror(holder, {
  15586. mode: "text/html",
  15587. tabMode: "indent",
  15588. lineNumbers: true,
  15589. lineWrapping: true
  15590. });
  15591. var dom = codeEditor.getWrapperElement();
  15592. dom.style.cssText = 'position:absolute;left:0;top:0;width:100%;height:100%;font-family:consolas,"Courier new",monospace;font-size:13px;';
  15593. codeEditor.getScrollerElement().style.cssText = 'position:absolute;left:0;top:0;width:100%;height:100%;';
  15594. codeEditor.refresh();
  15595. return {
  15596. getCodeMirror: function () {
  15597. return codeEditor;
  15598. },
  15599. setContent: function (content) {
  15600. codeEditor.setValue(content);
  15601. },
  15602. getContent: function () {
  15603. return codeEditor.getValue();
  15604. },
  15605. select: function () {
  15606. codeEditor.focus();
  15607. },
  15608. dispose: function () {
  15609. holder.removeChild(dom);
  15610. dom = null;
  15611. codeEditor = null;
  15612. }
  15613. };
  15614. }
  15615. };
  15616. UE.plugins['source'] = function () {
  15617. var me = this;
  15618. var opt = this.options;
  15619. var sourceMode = false;
  15620. var sourceEditor;
  15621. var orgSetContent;
  15622. opt.sourceEditor = browser.ie ? 'textarea' : (opt.sourceEditor || 'codemirror');
  15623. me.setOpt({
  15624. sourceEditorFirst: false
  15625. });
  15626. function createSourceEditor(holder) {
  15627. return sourceEditors[opt.sourceEditor == 'codemirror' && window.CodeMirror ? 'codemirror' : 'textarea'](me, holder);
  15628. }
  15629. var bakCssText;
  15630. //解决在源码模式下getContent不能得到最新的内容问题
  15631. var oldGetContent,
  15632. bakAddress;
  15633. /**
  15634. * 切换源码模式和编辑模式
  15635. * @command source
  15636. * @method execCommand
  15637. * @param { String } cmd 命令字符串
  15638. * @example
  15639. * ```javascript
  15640. * editor.execCommand( 'source');
  15641. * ```
  15642. */
  15643. /**
  15644. * 查询当前编辑区域的状态是源码模式还是可视化模式
  15645. * @command source
  15646. * @method queryCommandState
  15647. * @param { String } cmd 命令字符串
  15648. * @return { int } 如果当前是源码编辑模式,返回1,否则返回0
  15649. * @example
  15650. * ```javascript
  15651. * editor.queryCommandState( 'source' );
  15652. * ```
  15653. */
  15654. me.commands['source'] = {
  15655. execCommand: function () {
  15656. sourceMode = !sourceMode;
  15657. if (sourceMode) {
  15658. bakAddress = me.selection.getRange().createAddress(false, true);
  15659. me.undoManger && me.undoManger.save(true);
  15660. if (browser.gecko) {
  15661. me.body.contentEditable = false;
  15662. }
  15663. bakCssText = me.iframe.style.cssText;
  15664. me.iframe.style.cssText += 'position:absolute;left:-32768px;top:-32768px;';
  15665. me.fireEvent('beforegetcontent');
  15666. var root = UE.htmlparser(me.body.innerHTML);
  15667. me.filterOutputRule(root);
  15668. root.traversal(function (node) {
  15669. if (node.type == 'element') {
  15670. switch (node.tagName) {
  15671. case 'td':
  15672. case 'th':
  15673. case 'caption':
  15674. if (node.children && node.children.length == 1) {
  15675. if (node.firstChild().tagName == 'br') {
  15676. node.removeChild(node.firstChild())
  15677. }
  15678. };
  15679. break;
  15680. case 'pre':
  15681. node.innerText(node.innerText().replace(/&nbsp;/g, ' '))
  15682. }
  15683. }
  15684. });
  15685. me.fireEvent('aftergetcontent');
  15686. var content = root.toHtml(true);
  15687. sourceEditor = createSourceEditor(me.iframe.parentNode);
  15688. sourceEditor.setContent(content);
  15689. orgSetContent = me.setContent;
  15690. me.setContent = function (html) {
  15691. //这里暂时不触发事件,防止报错
  15692. var root = UE.htmlparser(html);
  15693. me.filterInputRule(root);
  15694. html = root.toHtml();
  15695. sourceEditor.setContent(html);
  15696. };
  15697. setTimeout(function () {
  15698. sourceEditor.select();
  15699. me.addListener('fullscreenchanged', function () {
  15700. try {
  15701. sourceEditor.getCodeMirror().refresh()
  15702. } catch (e) {}
  15703. });
  15704. });
  15705. //重置getContent,源码模式下取值也能是最新的数据
  15706. oldGetContent = me.getContent;
  15707. me.getContent = function () {
  15708. return sourceEditor.getContent() || '<p>' + (browser.ie ? '' : '<br/>') + '</p>';
  15709. };
  15710. } else {
  15711. me.iframe.style.cssText = bakCssText;
  15712. var cont = sourceEditor.getContent() || '<p>' + (browser.ie ? '' : '<br/>') + '</p>';
  15713. //处理掉block节点前后的空格,有可能会误命中,暂时不考虑
  15714. cont = cont.replace(new RegExp('[\\r\\t\\n ]*<\/?(\\w+)\\s*(?:[^>]*)>', 'g'), function (a, b) {
  15715. if (b && !dtd.$inlineWithA[b.toLowerCase()]) {
  15716. return a.replace(/(^[\n\r\t ]*)|([\n\r\t ]*$)/g, '');
  15717. }
  15718. return a.replace(/(^[\n\r\t]*)|([\n\r\t]*$)/g, '')
  15719. });
  15720. me.setContent = orgSetContent;
  15721. me.setContent(cont);
  15722. sourceEditor.dispose();
  15723. sourceEditor = null;
  15724. //还原getContent方法
  15725. me.getContent = oldGetContent;
  15726. var first = me.body.firstChild;
  15727. //trace:1106 都删除空了,下边会报错,所以补充一个p占位
  15728. if (!first) {
  15729. me.body.innerHTML = '<p>' + (browser.ie ? '' : '<br/>') + '</p>';
  15730. first = me.body.firstChild;
  15731. }
  15732. //要在ifm为显示时ff才能取到selection,否则报错
  15733. //这里不能比较位置了
  15734. me.undoManger && me.undoManger.save(true);
  15735. if (browser.gecko) {
  15736. var input = document.createElement('input');
  15737. input.style.cssText = 'position:absolute;left:0;top:-32768px';
  15738. document.body.appendChild(input);
  15739. me.body.contentEditable = false;
  15740. setTimeout(function () {
  15741. domUtils.setViewportOffset(input, {
  15742. left: -32768,
  15743. top: 0
  15744. });
  15745. input.focus();
  15746. setTimeout(function () {
  15747. me.body.contentEditable = true;
  15748. me.selection.getRange().moveToAddress(bakAddress).select(true);
  15749. domUtils.remove(input);
  15750. });
  15751. });
  15752. } else {
  15753. //ie下有可能报错,比如在代码顶头的情况
  15754. try {
  15755. me.selection.getRange().moveToAddress(bakAddress).select(true);
  15756. } catch (e) {}
  15757. }
  15758. }
  15759. this.fireEvent('sourcemodechanged', sourceMode);
  15760. },
  15761. queryCommandState: function () {
  15762. return sourceMode | 0;
  15763. },
  15764. notNeedUndo: 1
  15765. };
  15766. var oldQueryCommandState = me.queryCommandState;
  15767. me.queryCommandState = function (cmdName) {
  15768. cmdName = cmdName.toLowerCase();
  15769. if (sourceMode) {
  15770. //源码模式下可以开启的命令
  15771. return cmdName in {
  15772. 'source': 1,
  15773. 'fullscreen': 1
  15774. } ? 1 : -1
  15775. }
  15776. return oldQueryCommandState.apply(this, arguments);
  15777. };
  15778. if (opt.sourceEditor == "codemirror") {
  15779. me.addListener("ready", function () {
  15780. utils.loadFile(document, {
  15781. src: opt.codeMirrorJsUrl || opt.UEDITOR_HOME_URL + "third-party/codemirror/codemirror.js",
  15782. tag: "script",
  15783. type: "text/javascript",
  15784. defer: "defer"
  15785. }, function () {
  15786. if (opt.sourceEditorFirst) {
  15787. setTimeout(function () {
  15788. me.execCommand("source");
  15789. }, 0);
  15790. }
  15791. });
  15792. utils.loadFile(document, {
  15793. tag: "link",
  15794. rel: "stylesheet",
  15795. type: "text/css",
  15796. href: opt.codeMirrorCssUrl || opt.UEDITOR_HOME_URL + "third-party/codemirror/codemirror.css"
  15797. });
  15798. });
  15799. }
  15800. };
  15801. })();
  15802. // plugins/enterkey.js
  15803. ///import core
  15804. ///import plugins/undo.js
  15805. ///commands 设置回车标签p或br
  15806. ///commandsName EnterKey
  15807. ///commandsTitle 设置回车标签p或br
  15808. /**
  15809. * @description 处理回车
  15810. * @author zhanyi
  15811. */
  15812. UE.plugins['enterkey'] = function () {
  15813. var hTag,
  15814. me = this,
  15815. tag = me.options.enterTag;
  15816. me.addListener('keyup', function (type, evt) {
  15817. var keyCode = evt.keyCode || evt.which;
  15818. if (keyCode == 13) {
  15819. var range = me.selection.getRange(),
  15820. start = range.startContainer,
  15821. doSave;
  15822. //修正在h1-h6里边回车后不能嵌套p的问题
  15823. if (!browser.ie) {
  15824. if (/h\d/i.test(hTag)) {
  15825. if (browser.gecko) {
  15826. var h = domUtils.findParentByTagName(start, ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote', 'caption', 'table'], true);
  15827. if (!h) {
  15828. me.document.execCommand('formatBlock', false, '<p>');
  15829. doSave = 1;
  15830. }
  15831. } else {
  15832. //chrome remove div
  15833. if (start.nodeType == 1) {
  15834. var tmp = me.document.createTextNode(''),
  15835. div;
  15836. range.insertNode(tmp);
  15837. div = domUtils.findParentByTagName(tmp, 'div', true);
  15838. if (div) {
  15839. var p = me.document.createElement('p');
  15840. while (div.firstChild) {
  15841. p.appendChild(div.firstChild);
  15842. }
  15843. div.parentNode.insertBefore(p, div);
  15844. domUtils.remove(div);
  15845. range.setStartBefore(tmp).setCursor();
  15846. doSave = 1;
  15847. }
  15848. domUtils.remove(tmp);
  15849. }
  15850. }
  15851. if (me.undoManger && doSave) {
  15852. me.undoManger.save();
  15853. }
  15854. }
  15855. //没有站位符,会出现多行的问题
  15856. browser.opera && range.select();
  15857. } else {
  15858. me.fireEvent('saveScene', true, true)
  15859. }
  15860. }
  15861. });
  15862. me.addListener('keydown', function (type, evt) {
  15863. var keyCode = evt.keyCode || evt.which;
  15864. if (keyCode == 13) { //回车
  15865. if (me.fireEvent('beforeenterkeydown')) {
  15866. domUtils.preventDefault(evt);
  15867. return;
  15868. }
  15869. me.fireEvent('saveScene', true, true);
  15870. hTag = '';
  15871. var range = me.selection.getRange();
  15872. if (!range.collapsed) {
  15873. //跨td不能删
  15874. var start = range.startContainer,
  15875. end = range.endContainer,
  15876. startTd = domUtils.findParentByTagName(start, 'td', true),
  15877. endTd = domUtils.findParentByTagName(end, 'td', true);
  15878. if (startTd && endTd && startTd !== endTd || !startTd && endTd || startTd && !endTd) {
  15879. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
  15880. return;
  15881. }
  15882. }
  15883. if (tag == 'p') {
  15884. if (!browser.ie) {
  15885. start = domUtils.findParentByTagName(range.startContainer, ['ol', 'ul', 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote', 'caption'], true);
  15886. //opera下执行formatblock会在table的场景下有问题,回车在opera原生支持很好,所以暂时在opera去掉调用这个原生的command
  15887. //trace:2431
  15888. if (!start && !browser.opera) {
  15889. me.document.execCommand('formatBlock', false, '<p>');
  15890. if (browser.gecko) {
  15891. range = me.selection.getRange();
  15892. start = domUtils.findParentByTagName(range.startContainer, 'p', true);
  15893. start && domUtils.removeDirtyAttr(start);
  15894. }
  15895. } else {
  15896. hTag = start.tagName;
  15897. start.tagName.toLowerCase() == 'p' && browser.gecko && domUtils.removeDirtyAttr(start);
  15898. }
  15899. }
  15900. } else {
  15901. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
  15902. if (!range.collapsed) {
  15903. range.deleteContents();
  15904. start = range.startContainer;
  15905. if (start.nodeType == 1 && (start = start.childNodes[range.startOffset])) {
  15906. while (start.nodeType == 1) {
  15907. if (dtd.$empty[start.tagName]) {
  15908. range.setStartBefore(start).setCursor();
  15909. if (me.undoManger) {
  15910. me.undoManger.save();
  15911. }
  15912. return false;
  15913. }
  15914. if (!start.firstChild) {
  15915. var br = range.document.createElement('br');
  15916. start.appendChild(br);
  15917. range.setStart(start, 0).setCursor();
  15918. if (me.undoManger) {
  15919. me.undoManger.save();
  15920. }
  15921. return false;
  15922. }
  15923. start = start.firstChild;
  15924. }
  15925. if (start === range.startContainer.childNodes[range.startOffset]) {
  15926. br = range.document.createElement('br');
  15927. range.insertNode(br).setCursor();
  15928. } else {
  15929. range.setStart(start, 0).setCursor();
  15930. }
  15931. } else {
  15932. br = range.document.createElement('br');
  15933. range.insertNode(br).setStartAfter(br).setCursor();
  15934. }
  15935. } else {
  15936. br = range.document.createElement('br');
  15937. range.insertNode(br);
  15938. var parent = br.parentNode;
  15939. if (parent.lastChild === br) {
  15940. br.parentNode.insertBefore(br.cloneNode(true), br);
  15941. range.setStartBefore(br);
  15942. } else {
  15943. range.setStartAfter(br);
  15944. }
  15945. range.setCursor();
  15946. }
  15947. }
  15948. }
  15949. });
  15950. };
  15951. // plugins/keystrokes.js
  15952. /* 处理特殊键的兼容性问题 */
  15953. UE.plugins['keystrokes'] = function () {
  15954. var me = this;
  15955. var collapsed = true;
  15956. me.addListener('keydown', function (type, evt) {
  15957. var keyCode = evt.keyCode || evt.which,
  15958. rng = me.selection.getRange();
  15959. //处理全选的情况
  15960. if (!rng.collapsed && !(evt.ctrlKey || evt.shiftKey || evt.altKey || evt.metaKey) && (keyCode >= 65 && keyCode <= 90 ||
  15961. keyCode >= 48 && keyCode <= 57 ||
  15962. keyCode >= 96 && keyCode <= 111 || {
  15963. 13: 1,
  15964. 8: 1,
  15965. 46: 1
  15966. } [keyCode])) {
  15967. var tmpNode = rng.startContainer;
  15968. if (domUtils.isFillChar(tmpNode)) {
  15969. rng.setStartBefore(tmpNode)
  15970. }
  15971. tmpNode = rng.endContainer;
  15972. if (domUtils.isFillChar(tmpNode)) {
  15973. rng.setEndAfter(tmpNode)
  15974. }
  15975. rng.txtToElmBoundary();
  15976. //结束边界可能放到了br的前边,要把br包含进来
  15977. // x[xxx]<br/>
  15978. if (rng.endContainer && rng.endContainer.nodeType == 1) {
  15979. tmpNode = rng.endContainer.childNodes[rng.endOffset];
  15980. if (tmpNode && domUtils.isBr(tmpNode)) {
  15981. rng.setEndAfter(tmpNode);
  15982. }
  15983. }
  15984. if (rng.startOffset == 0) {
  15985. tmpNode = rng.startContainer;
  15986. if (domUtils.isBoundaryNode(tmpNode, 'firstChild')) {
  15987. tmpNode = rng.endContainer;
  15988. if (rng.endOffset == (tmpNode.nodeType == 3 ? tmpNode.nodeValue.length : tmpNode.childNodes.length) && domUtils.isBoundaryNode(tmpNode, 'lastChild')) {
  15989. me.fireEvent('saveScene');
  15990. me.body.innerHTML = '<p>' + (browser.ie ? '' : '<br/>') + '</p>';
  15991. rng.setStart(me.body.firstChild, 0).setCursor(false, true);
  15992. me._selectionChange();
  15993. return;
  15994. }
  15995. }
  15996. }
  15997. }
  15998. //处理backspace
  15999. if (keyCode == keymap.Backspace) {
  16000. rng = me.selection.getRange();
  16001. collapsed = rng.collapsed;
  16002. if (me.fireEvent('delkeydown', evt)) {
  16003. return;
  16004. }
  16005. var start, end;
  16006. //避免按两次删除才能生效的问题
  16007. if (rng.collapsed && rng.inFillChar()) {
  16008. start = rng.startContainer;
  16009. if (domUtils.isFillChar(start)) {
  16010. rng.setStartBefore(start).shrinkBoundary(true).collapse(true);
  16011. domUtils.remove(start)
  16012. } else {
  16013. start.nodeValue = start.nodeValue.replace(new RegExp('^' + domUtils.fillChar), '');
  16014. rng.startOffset--;
  16015. rng.collapse(true).select(true)
  16016. }
  16017. }
  16018. //解决选中control元素不能删除的问题
  16019. if (start = rng.getClosedNode()) {
  16020. me.fireEvent('saveScene');
  16021. rng.setStartBefore(start);
  16022. domUtils.remove(start);
  16023. rng.setCursor();
  16024. me.fireEvent('saveScene');
  16025. domUtils.preventDefault(evt);
  16026. return;
  16027. }
  16028. //阻止在table上的删除
  16029. if (!browser.ie) {
  16030. start = domUtils.findParentByTagName(rng.startContainer, 'table', true);
  16031. end = domUtils.findParentByTagName(rng.endContainer, 'table', true);
  16032. if (start && !end || !start && end || start !== end) {
  16033. evt.preventDefault();
  16034. return;
  16035. }
  16036. }
  16037. }
  16038. //处理tab键的逻辑
  16039. if (keyCode == keymap.Tab) {
  16040. //不处理以下标签
  16041. var excludeTagNameForTabKey = {
  16042. 'ol': 1,
  16043. 'ul': 1,
  16044. 'table': 1
  16045. };
  16046. //处理组件里的tab按下事件
  16047. if (me.fireEvent('tabkeydown', evt)) {
  16048. domUtils.preventDefault(evt);
  16049. return;
  16050. }
  16051. var range = me.selection.getRange();
  16052. me.fireEvent('saveScene');
  16053. for (var i = 0, txt = '', tabSize = me.options.tabSize || 4, tabNode = me.options.tabNode || '&nbsp;'; i < tabSize; i++) {
  16054. txt += tabNode;
  16055. }
  16056. var span = me.document.createElement('span');
  16057. span.innerHTML = txt + domUtils.fillChar;
  16058. if (range.collapsed) {
  16059. range.insertNode(span.cloneNode(true).firstChild).setCursor(true);
  16060. } else {
  16061. var filterFn = function (node) {
  16062. return domUtils.isBlockElm(node) && !excludeTagNameForTabKey[node.tagName.toLowerCase()]
  16063. };
  16064. //普通的情况
  16065. start = domUtils.findParent(range.startContainer, filterFn, true);
  16066. end = domUtils.findParent(range.endContainer, filterFn, true);
  16067. if (start && end && start === end) {
  16068. range.deleteContents();
  16069. range.insertNode(span.cloneNode(true).firstChild).setCursor(true);
  16070. } else {
  16071. var bookmark = range.createBookmark();
  16072. range.enlarge(true);
  16073. var bookmark2 = range.createBookmark(),
  16074. current = domUtils.getNextDomNode(bookmark2.start, false, filterFn);
  16075. while (current && !(domUtils.getPosition(current, bookmark2.end) & domUtils.POSITION_FOLLOWING)) {
  16076. current.insertBefore(span.cloneNode(true).firstChild, current.firstChild);
  16077. current = domUtils.getNextDomNode(current, false, filterFn);
  16078. }
  16079. range.moveToBookmark(bookmark2).moveToBookmark(bookmark).select();
  16080. }
  16081. }
  16082. domUtils.preventDefault(evt)
  16083. }
  16084. //trace:1634
  16085. //ff的del键在容器空的时候,也会删除
  16086. if (browser.gecko && keyCode == 46) {
  16087. range = me.selection.getRange();
  16088. if (range.collapsed) {
  16089. start = range.startContainer;
  16090. if (domUtils.isEmptyBlock(start)) {
  16091. var parent = start.parentNode;
  16092. while (domUtils.getChildCount(parent) == 1 && !domUtils.isBody(parent)) {
  16093. start = parent;
  16094. parent = parent.parentNode;
  16095. }
  16096. if (start === parent.lastChild)
  16097. evt.preventDefault();
  16098. return;
  16099. }
  16100. }
  16101. }
  16102. });
  16103. me.addListener('keyup', function (type, evt) {
  16104. var keyCode = evt.keyCode || evt.which,
  16105. rng, me = this;
  16106. if (keyCode == keymap.Backspace) {
  16107. if (me.fireEvent('delkeyup')) {
  16108. return;
  16109. }
  16110. rng = me.selection.getRange();
  16111. if (rng.collapsed) {
  16112. var tmpNode,
  16113. autoClearTagName = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
  16114. if (tmpNode = domUtils.findParentByTagName(rng.startContainer, autoClearTagName, true)) {
  16115. if (domUtils.isEmptyBlock(tmpNode)) {
  16116. var pre = tmpNode.previousSibling;
  16117. if (pre && pre.nodeName != 'TABLE') {
  16118. domUtils.remove(tmpNode);
  16119. rng.setStartAtLast(pre).setCursor(false, true);
  16120. return;
  16121. } else {
  16122. var next = tmpNode.nextSibling;
  16123. if (next && next.nodeName != 'TABLE') {
  16124. domUtils.remove(tmpNode);
  16125. rng.setStartAtFirst(next).setCursor(false, true);
  16126. return;
  16127. }
  16128. }
  16129. }
  16130. }
  16131. //处理当删除到body时,要重新给p标签展位
  16132. if (domUtils.isBody(rng.startContainer)) {
  16133. var tmpNode = domUtils.createElement(me.document, 'p', {
  16134. 'innerHTML': browser.ie ? domUtils.fillChar : '<br/>'
  16135. });
  16136. rng.insertNode(tmpNode).setStart(tmpNode, 0).setCursor(false, true);
  16137. }
  16138. }
  16139. //chrome下如果删除了inline标签,浏览器会有记忆,在输入文字还是会套上刚才删除的标签,所以这里再选一次就不会了
  16140. if (!collapsed && (rng.startContainer.nodeType == 3 || rng.startContainer.nodeType == 1 && domUtils.isEmptyBlock(rng.startContainer))) {
  16141. if (browser.ie) {
  16142. var span = rng.document.createElement('span');
  16143. rng.insertNode(span).setStartBefore(span).collapse(true);
  16144. rng.select();
  16145. domUtils.remove(span)
  16146. } else {
  16147. rng.select()
  16148. }
  16149. }
  16150. }
  16151. })
  16152. };
  16153. // plugins/fiximgclick.js
  16154. ///import core
  16155. ///commands 修复chrome下图片不能点击的问题,出现八个角可改变大小
  16156. ///commandsName FixImgClick
  16157. ///commandsTitle 修复chrome下图片不能点击的问题,出现八个角可改变大小
  16158. //修复chrome下图片不能点击的问题,出现八个角可改变大小
  16159. UE.plugins['fiximgclick'] = (function () {
  16160. var elementUpdated = false;
  16161. function Scale() {
  16162. this.editor = null;
  16163. this.resizer = null;
  16164. this.cover = null;
  16165. this.doc = document;
  16166. this.prePos = {
  16167. x: 0,
  16168. y: 0
  16169. };
  16170. this.startPos = {
  16171. x: 0,
  16172. y: 0
  16173. };
  16174. }
  16175. (function () {
  16176. var rect = [
  16177. //[left, top, width, height]
  16178. [0, 0, -1, -1],
  16179. [0, 0, 0, -1],
  16180. [0, 0, 1, -1],
  16181. [0, 0, -1, 0],
  16182. [0, 0, 1, 0],
  16183. [0, 0, -1, 1],
  16184. [0, 0, 0, 1],
  16185. [0, 0, 1, 1]
  16186. ];
  16187. Scale.prototype = {
  16188. init: function (editor) {
  16189. var me = this;
  16190. me.editor = editor;
  16191. me.startPos = this.prePos = {
  16192. x: 0,
  16193. y: 0
  16194. };
  16195. me.dragId = -1;
  16196. var hands = [],
  16197. cover = me.cover = document.createElement('div'),
  16198. resizer = me.resizer = document.createElement('div');
  16199. cover.id = me.editor.ui.id + '_imagescale_cover';
  16200. cover.style.cssText = 'position:absolute;display:none;z-index:' + (me.editor.options.zIndex) + ';filter:alpha(opacity=0); opacity:0;background:#CCC;';
  16201. domUtils.on(cover, 'mousedown click', function () {
  16202. me.hide();
  16203. });
  16204. for (i = 0; i < 8; i++) {
  16205. hands.push('<span class="edui-editor-imagescale-hand' + i + '"></span>');
  16206. }
  16207. resizer.id = me.editor.ui.id + '_imagescale';
  16208. resizer.className = 'edui-editor-imagescale';
  16209. resizer.innerHTML = hands.join('');
  16210. resizer.style.cssText += ';display:none;border:1px solid #3b77ff;z-index:' + (me.editor.options.zIndex) + ';';
  16211. me.editor.ui.getDom().appendChild(cover);
  16212. me.editor.ui.getDom().appendChild(resizer);
  16213. me.initStyle();
  16214. me.initEvents();
  16215. },
  16216. initStyle: function () {
  16217. utils.cssRule('imagescale', '.edui-editor-imagescale{display:none;position:absolute;border:1px solid #38B2CE;cursor:hand;-webkit-box-sizing: content-box;-moz-box-sizing: content-box;box-sizing: content-box;}' +
  16218. '.edui-editor-imagescale span{position:absolute;width:6px;height:6px;overflow:hidden;font-size:0px;display:block;background-color:#3C9DD0;}' +
  16219. '.edui-editor-imagescale .edui-editor-imagescale-hand0{cursor:nw-resize;top:0;margin-top:-4px;left:0;margin-left:-4px;}' +
  16220. '.edui-editor-imagescale .edui-editor-imagescale-hand1{cursor:n-resize;top:0;margin-top:-4px;left:50%;margin-left:-4px;}' +
  16221. '.edui-editor-imagescale .edui-editor-imagescale-hand2{cursor:ne-resize;top:0;margin-top:-4px;left:100%;margin-left:-3px;}' +
  16222. '.edui-editor-imagescale .edui-editor-imagescale-hand3{cursor:w-resize;top:50%;margin-top:-4px;left:0;margin-left:-4px;}' +
  16223. '.edui-editor-imagescale .edui-editor-imagescale-hand4{cursor:e-resize;top:50%;margin-top:-4px;left:100%;margin-left:-3px;}' +
  16224. '.edui-editor-imagescale .edui-editor-imagescale-hand5{cursor:sw-resize;top:100%;margin-top:-3px;left:0;margin-left:-4px;}' +
  16225. '.edui-editor-imagescale .edui-editor-imagescale-hand6{cursor:s-resize;top:100%;margin-top:-3px;left:50%;margin-left:-4px;}' +
  16226. '.edui-editor-imagescale .edui-editor-imagescale-hand7{cursor:se-resize;top:100%;margin-top:-3px;left:100%;margin-left:-3px;}');
  16227. },
  16228. initEvents: function () {
  16229. var me = this;
  16230. me.startPos.x = me.startPos.y = 0;
  16231. me.isDraging = false;
  16232. },
  16233. _eventHandler: function (e) {
  16234. var me = this;
  16235. switch (e.type) {
  16236. case 'mousedown':
  16237. var hand = e.target || e.srcElement,
  16238. hand;
  16239. if (hand.className.indexOf('edui-editor-imagescale-hand') != -1 && me.dragId == -1) {
  16240. me.dragId = hand.className.slice(-1);
  16241. me.startPos.x = me.prePos.x = e.clientX;
  16242. me.startPos.y = me.prePos.y = e.clientY;
  16243. domUtils.on(me.doc, 'mousemove', me.proxy(me._eventHandler, me));
  16244. }
  16245. break;
  16246. case 'mousemove':
  16247. if (me.dragId != -1) {
  16248. me.updateContainerStyle(me.dragId, {
  16249. x: e.clientX - me.prePos.x,
  16250. y: e.clientY - me.prePos.y
  16251. });
  16252. me.prePos.x = e.clientX;
  16253. me.prePos.y = e.clientY;
  16254. elementUpdated = true;
  16255. me.updateTargetElement();
  16256. }
  16257. break;
  16258. case 'mouseup':
  16259. if (me.dragId != -1) {
  16260. me.updateContainerStyle(me.dragId, {
  16261. x: e.clientX - me.prePos.x,
  16262. y: e.clientY - me.prePos.y
  16263. });
  16264. me.updateTargetElement();
  16265. if (me.target.parentNode) me.attachTo(me.target);
  16266. me.dragId = -1;
  16267. }
  16268. domUtils.un(me.doc, 'mousemove', me.proxy(me._eventHandler, me));
  16269. //修复只是点击挪动点,但没有改变大小,不应该触发contentchange
  16270. if (elementUpdated) {
  16271. elementUpdated = false;
  16272. me.editor.fireEvent('contentchange');
  16273. }
  16274. break;
  16275. default:
  16276. break;
  16277. }
  16278. },
  16279. updateTargetElement: function () {
  16280. var me = this;
  16281. domUtils.setStyles(me.target, {
  16282. 'width': me.resizer.style.width,
  16283. 'height': me.resizer.style.height
  16284. });
  16285. me.target.width = parseInt(me.resizer.style.width);
  16286. me.target.height = parseInt(me.resizer.style.height);
  16287. me.attachTo(me.target);
  16288. },
  16289. updateContainerStyle: function (dir, offset) {
  16290. var me = this,
  16291. dom = me.resizer,
  16292. tmp;
  16293. if (rect[dir][0] != 0) {
  16294. tmp = parseInt(dom.style.left) + offset.x;
  16295. dom.style.left = me._validScaledProp('left', tmp) + 'px';
  16296. }
  16297. if (rect[dir][1] != 0) {
  16298. tmp = parseInt(dom.style.top) + offset.y;
  16299. dom.style.top = me._validScaledProp('top', tmp) + 'px';
  16300. }
  16301. if (rect[dir][2] != 0) {
  16302. tmp = dom.clientWidth + rect[dir][2] * offset.x;
  16303. dom.style.width = me._validScaledProp('width', tmp) + 'px';
  16304. }
  16305. if (rect[dir][3] != 0) {
  16306. tmp = dom.clientHeight + rect[dir][3] * offset.y;
  16307. dom.style.height = me._validScaledProp('height', tmp) + 'px';
  16308. }
  16309. },
  16310. _validScaledProp: function (prop, value) {
  16311. var ele = this.resizer,
  16312. wrap = document;
  16313. value = isNaN(value) ? 0 : value;
  16314. switch (prop) {
  16315. case 'left':
  16316. return value < 0 ? 0 : (value + ele.clientWidth) > wrap.clientWidth ? wrap.clientWidth - ele.clientWidth : value;
  16317. case 'top':
  16318. return value < 0 ? 0 : (value + ele.clientHeight) > wrap.clientHeight ? wrap.clientHeight - ele.clientHeight : value;
  16319. case 'width':
  16320. return value <= 0 ? 1 : (value + ele.offsetLeft) > wrap.clientWidth ? wrap.clientWidth - ele.offsetLeft : value;
  16321. case 'height':
  16322. return value <= 0 ? 1 : (value + ele.offsetTop) > wrap.clientHeight ? wrap.clientHeight - ele.offsetTop : value;
  16323. }
  16324. },
  16325. hideCover: function () {
  16326. this.cover.style.display = 'none';
  16327. },
  16328. showCover: function () {
  16329. var me = this,
  16330. editorPos = domUtils.getXY(me.editor.ui.getDom()),
  16331. iframePos = domUtils.getXY(me.editor.iframe);
  16332. domUtils.setStyles(me.cover, {
  16333. 'width': me.editor.iframe.offsetWidth + 'px',
  16334. 'height': me.editor.iframe.offsetHeight + 'px',
  16335. 'top': iframePos.y - editorPos.y + 'px',
  16336. 'left': iframePos.x - editorPos.x + 'px',
  16337. 'position': 'absolute',
  16338. 'display': ''
  16339. })
  16340. },
  16341. show: function (targetObj) {
  16342. var me = this;
  16343. me.resizer.style.display = 'block';
  16344. if (targetObj) me.attachTo(targetObj);
  16345. domUtils.on(this.resizer, 'mousedown', me.proxy(me._eventHandler, me));
  16346. domUtils.on(me.doc, 'mouseup', me.proxy(me._eventHandler, me));
  16347. me.showCover();
  16348. me.editor.fireEvent('afterscaleshow', me);
  16349. me.editor.fireEvent('saveScene');
  16350. },
  16351. hide: function () {
  16352. var me = this;
  16353. me.hideCover();
  16354. me.resizer.style.display = 'none';
  16355. domUtils.un(me.resizer, 'mousedown', me.proxy(me._eventHandler, me));
  16356. domUtils.un(me.doc, 'mouseup', me.proxy(me._eventHandler, me));
  16357. me.editor.fireEvent('afterscalehide', me);
  16358. },
  16359. proxy: function (fn, context) {
  16360. return function (e) {
  16361. return fn.apply(context || this, arguments);
  16362. };
  16363. },
  16364. attachTo: function (targetObj) {
  16365. var me = this,
  16366. target = me.target = targetObj,
  16367. resizer = this.resizer,
  16368. imgPos = domUtils.getXY(target),
  16369. iframePos = domUtils.getXY(me.editor.iframe),
  16370. editorPos = domUtils.getXY(resizer.parentNode);
  16371. domUtils.setStyles(resizer, {
  16372. 'width': target.width + 'px',
  16373. 'height': target.height + 'px',
  16374. 'left': iframePos.x + imgPos.x - me.editor.document.body.scrollLeft - editorPos.x - parseInt(resizer.style.borderLeftWidth) + 'px',
  16375. 'top': iframePos.y + imgPos.y - me.editor.document.body.scrollTop - editorPos.y - parseInt(resizer.style.borderTopWidth) + 'px'
  16376. });
  16377. }
  16378. }
  16379. })();
  16380. return function () {
  16381. var me = this,
  16382. imageScale;
  16383. me.setOpt('imageScaleEnabled', true);
  16384. if (!browser.ie && me.options.imageScaleEnabled) {
  16385. me.addListener('click', function (type, e) {
  16386. var range = me.selection.getRange(),
  16387. img = range.getClosedNode();
  16388. if (img && img.tagName == 'IMG' && me.body.contentEditable != "false") {
  16389. if (img.className.indexOf("edui-faked-music") != -1 ||
  16390. img.getAttribute("anchorname") ||
  16391. domUtils.hasClass(img, 'loadingclass') ||
  16392. domUtils.hasClass(img, 'loaderrorclass')) {
  16393. return
  16394. }
  16395. if (!imageScale) {
  16396. imageScale = new Scale();
  16397. imageScale.init(me);
  16398. me.ui.getDom().appendChild(imageScale.resizer);
  16399. var _keyDownHandler = function (e) {
  16400. imageScale.hide();
  16401. if (imageScale.target) me.selection.getRange().selectNode(imageScale.target).select();
  16402. },
  16403. _mouseDownHandler = function (e) {
  16404. var ele = e.target || e.srcElement;
  16405. if (ele && (ele.className === undefined || ele.className.indexOf('edui-editor-imagescale') == -1)) {
  16406. _keyDownHandler(e);
  16407. }
  16408. },
  16409. timer;
  16410. me.addListener('afterscaleshow', function (e) {
  16411. me.addListener('beforekeydown', _keyDownHandler);
  16412. me.addListener('beforemousedown', _mouseDownHandler);
  16413. domUtils.on(document, 'keydown', _keyDownHandler);
  16414. domUtils.on(document, 'mousedown', _mouseDownHandler);
  16415. me.selection.getNative().removeAllRanges();
  16416. });
  16417. me.addListener('afterscalehide', function (e) {
  16418. me.removeListener('beforekeydown', _keyDownHandler);
  16419. me.removeListener('beforemousedown', _mouseDownHandler);
  16420. domUtils.un(document, 'keydown', _keyDownHandler);
  16421. domUtils.un(document, 'mousedown', _mouseDownHandler);
  16422. var target = imageScale.target;
  16423. if (target.parentNode) {
  16424. me.selection.getRange().selectNode(target).select();
  16425. }
  16426. });
  16427. //TODO 有iframe的情况,mousedown不能往下传。。
  16428. domUtils.on(imageScale.resizer, 'mousedown', function (e) {
  16429. me.selection.getNative().removeAllRanges();
  16430. var ele = e.target || e.srcElement;
  16431. if (ele && ele.className.indexOf('edui-editor-imagescale-hand') == -1) {
  16432. timer = setTimeout(function () {
  16433. imageScale.hide();
  16434. if (imageScale.target) me.selection.getRange().selectNode(ele).select();
  16435. }, 200);
  16436. }
  16437. });
  16438. domUtils.on(imageScale.resizer, 'mouseup', function (e) {
  16439. var ele = e.target || e.srcElement;
  16440. if (ele && ele.className.indexOf('edui-editor-imagescale-hand') == -1) {
  16441. clearTimeout(timer);
  16442. }
  16443. });
  16444. }
  16445. imageScale.show(img);
  16446. } else {
  16447. if (imageScale && imageScale.resizer.style.display != 'none') imageScale.hide();
  16448. }
  16449. });
  16450. }
  16451. if (browser.webkit) {
  16452. me.addListener('click', function (type, e) {
  16453. if (e.target.tagName == 'IMG' && me.body.contentEditable != "false") {
  16454. var range = new dom.Range(me.document);
  16455. range.selectNode(e.target).select();
  16456. }
  16457. });
  16458. }
  16459. }
  16460. })();
  16461. // plugins/autolink.js
  16462. ///import core
  16463. ///commands 为非ie浏览器自动添加a标签
  16464. ///commandsName AutoLink
  16465. ///commandsTitle 自动增加链接
  16466. /**
  16467. * @description 为非ie浏览器自动添加a标签
  16468. * @author zhanyi
  16469. */
  16470. UE.plugin.register('autolink', function () {
  16471. var cont = 0;
  16472. return !browser.ie ? {
  16473. bindEvents: {
  16474. 'reset': function () {
  16475. cont = 0;
  16476. },
  16477. 'keydown': function (type, evt) {
  16478. var me = this;
  16479. var keyCode = evt.keyCode || evt.which;
  16480. if (keyCode == 32 || keyCode == 13) {
  16481. var sel = me.selection.getNative(),
  16482. range = sel.getRangeAt(0).cloneRange(),
  16483. offset,
  16484. charCode;
  16485. var start = range.startContainer;
  16486. while (start.nodeType == 1 && range.startOffset > 0) {
  16487. start = range.startContainer.childNodes[range.startOffset - 1];
  16488. if (!start) {
  16489. break;
  16490. }
  16491. range.setStart(start, start.nodeType == 1 ? start.childNodes.length : start.nodeValue.length);
  16492. range.collapse(true);
  16493. start = range.startContainer;
  16494. }
  16495. do {
  16496. if (range.startOffset == 0) {
  16497. start = range.startContainer.previousSibling;
  16498. while (start && start.nodeType == 1) {
  16499. start = start.lastChild;
  16500. }
  16501. if (!start || domUtils.isFillChar(start)) {
  16502. break;
  16503. }
  16504. offset = start.nodeValue.length;
  16505. } else {
  16506. start = range.startContainer;
  16507. offset = range.startOffset;
  16508. }
  16509. range.setStart(start, offset - 1);
  16510. charCode = range.toString().charCodeAt(0);
  16511. } while (charCode != 160 && charCode != 32);
  16512. if (range.toString().replace(new RegExp(domUtils.fillChar, 'g'), '').match(/(?:https?:\/\/|ssh:\/\/|ftp:\/\/|file:\/|www\.)/i)) {
  16513. while (range.toString().length) {
  16514. if (/^(?:https?:\/\/|ssh:\/\/|ftp:\/\/|file:\/|www\.)/i.test(range.toString())) {
  16515. break;
  16516. }
  16517. try {
  16518. range.setStart(range.startContainer, range.startOffset + 1);
  16519. } catch (e) {
  16520. //trace:2121
  16521. var start = range.startContainer;
  16522. while (!(next = start.nextSibling)) {
  16523. if (domUtils.isBody(start)) {
  16524. return;
  16525. }
  16526. start = start.parentNode;
  16527. }
  16528. range.setStart(next, 0);
  16529. }
  16530. }
  16531. //range的开始边界已经在a标签里的不再处理
  16532. if (domUtils.findParentByTagName(range.startContainer, 'a', true)) {
  16533. return;
  16534. }
  16535. var a = me.document.createElement('a'),
  16536. text = me.document.createTextNode(' '),
  16537. href;
  16538. me.undoManger && me.undoManger.save();
  16539. a.appendChild(range.extractContents());
  16540. a.href = a.innerHTML = a.innerHTML.replace(/<[^>]+>/g, '');
  16541. href = a.getAttribute("href").replace(new RegExp(domUtils.fillChar, 'g'), '');
  16542. href = /^(?:https?:\/\/)/ig.test(href) ? href : "http://" + href;
  16543. a.setAttribute('_src', utils.html(href));
  16544. a.href = utils.html(href);
  16545. range.insertNode(a);
  16546. a.parentNode.insertBefore(text, a.nextSibling);
  16547. range.setStart(text, 0);
  16548. range.collapse(true);
  16549. sel.removeAllRanges();
  16550. sel.addRange(range);
  16551. me.undoManger && me.undoManger.save();
  16552. }
  16553. }
  16554. }
  16555. }
  16556. } : {}
  16557. }, function () {
  16558. var keyCodes = {
  16559. 37: 1,
  16560. 38: 1,
  16561. 39: 1,
  16562. 40: 1,
  16563. 13: 1,
  16564. 32: 1
  16565. };
  16566. function checkIsCludeLink(node) {
  16567. if (node.nodeType == 3) {
  16568. return null
  16569. }
  16570. if (node.nodeName == 'A') {
  16571. return node;
  16572. }
  16573. var lastChild = node.lastChild;
  16574. while (lastChild) {
  16575. if (lastChild.nodeName == 'A') {
  16576. return lastChild;
  16577. }
  16578. if (lastChild.nodeType == 3) {
  16579. if (domUtils.isWhitespace(lastChild)) {
  16580. lastChild = lastChild.previousSibling;
  16581. continue;
  16582. }
  16583. return null
  16584. }
  16585. lastChild = lastChild.lastChild;
  16586. }
  16587. }
  16588. browser.ie && this.addListener('keyup', function (cmd, evt) {
  16589. var me = this,
  16590. keyCode = evt.keyCode;
  16591. if (keyCodes[keyCode]) {
  16592. var rng = me.selection.getRange();
  16593. var start = rng.startContainer;
  16594. if (keyCode == 13) {
  16595. while (start && !domUtils.isBody(start) && !domUtils.isBlockElm(start)) {
  16596. start = start.parentNode;
  16597. }
  16598. if (start && !domUtils.isBody(start) && start.nodeName == 'P') {
  16599. var pre = start.previousSibling;
  16600. if (pre && pre.nodeType == 1) {
  16601. var pre = checkIsCludeLink(pre);
  16602. if (pre && !pre.getAttribute('_href')) {
  16603. domUtils.remove(pre, true);
  16604. }
  16605. }
  16606. }
  16607. } else if (keyCode == 32) {
  16608. if (start.nodeType == 3 && /^\s$/.test(start.nodeValue)) {
  16609. start = start.previousSibling;
  16610. if (start && start.nodeName == 'A' && !start.getAttribute('_href')) {
  16611. domUtils.remove(start, true);
  16612. }
  16613. }
  16614. } else {
  16615. start = domUtils.findParentByTagName(start, 'a', true);
  16616. if (start && !start.getAttribute('_href')) {
  16617. var bk = rng.createBookmark();
  16618. domUtils.remove(start, true);
  16619. rng.moveToBookmark(bk).select(true)
  16620. }
  16621. }
  16622. }
  16623. });
  16624. });
  16625. // plugins/autoheight.js
  16626. ///import core
  16627. ///commands 当输入内容超过编辑器高度时,编辑器自动增高
  16628. ///commandsName AutoHeight,autoHeightEnabled
  16629. ///commandsTitle 自动增高
  16630. /**
  16631. * @description 自动伸展
  16632. * @author zhanyi
  16633. */
  16634. UE.plugins['autoheight'] = function () {
  16635. var me = this;
  16636. //提供开关,就算加载也可以关闭
  16637. me.autoHeightEnabled = me.options.autoHeightEnabled !== false;
  16638. if (!me.autoHeightEnabled) {
  16639. return;
  16640. }
  16641. var bakOverflow,
  16642. lastHeight = 0,
  16643. options = me.options,
  16644. currentHeight,
  16645. timer;
  16646. function adjustHeight() {
  16647. var me = this;
  16648. clearTimeout(timer);
  16649. if (isFullscreen) return;
  16650. if (!me.queryCommandState || me.queryCommandState && me.queryCommandState('source') != 1) {
  16651. timer = setTimeout(function () {
  16652. var node = me.body.lastChild;
  16653. while (node && node.nodeType != 1) {
  16654. node = node.previousSibling;
  16655. }
  16656. if (node && node.nodeType == 1) {
  16657. node.style.clear = 'both';
  16658. currentHeight = Math.max(domUtils.getXY(node).y + node.offsetHeight + 25, Math.max(options.minFrameHeight, options.initialFrameHeight));
  16659. if (currentHeight != lastHeight) {
  16660. if (currentHeight !== parseInt(me.iframe.parentNode.style.height)) {
  16661. me.iframe.parentNode.style.height = currentHeight + 'px';
  16662. }
  16663. me.body.style.height = currentHeight + 'px';
  16664. lastHeight = currentHeight;
  16665. }
  16666. domUtils.removeStyle(node, 'clear');
  16667. }
  16668. }, 50)
  16669. }
  16670. }
  16671. var isFullscreen;
  16672. me.addListener('fullscreenchanged', function (cmd, f) {
  16673. isFullscreen = f
  16674. });
  16675. me.addListener('destroy', function () {
  16676. me.removeListener('contentchange afterinserthtml keyup mouseup', adjustHeight)
  16677. });
  16678. me.enableAutoHeight = function () {
  16679. var me = this;
  16680. if (!me.autoHeightEnabled) {
  16681. return;
  16682. }
  16683. var doc = me.document;
  16684. me.autoHeightEnabled = true;
  16685. bakOverflow = doc.body.style.overflowY;
  16686. doc.body.style.overflowY = 'hidden';
  16687. me.addListener('contentchange afterinserthtml keyup mouseup', adjustHeight);
  16688. //ff不给事件算得不对
  16689. setTimeout(function () {
  16690. adjustHeight.call(me);
  16691. }, browser.gecko ? 100 : 0);
  16692. me.fireEvent('autoheightchanged', me.autoHeightEnabled);
  16693. };
  16694. me.disableAutoHeight = function () {
  16695. me.body.style.overflowY = bakOverflow || '';
  16696. me.removeListener('contentchange', adjustHeight);
  16697. me.removeListener('keyup', adjustHeight);
  16698. me.removeListener('mouseup', adjustHeight);
  16699. me.autoHeightEnabled = false;
  16700. me.fireEvent('autoheightchanged', me.autoHeightEnabled);
  16701. };
  16702. me.on('setHeight', function () {
  16703. me.disableAutoHeight()
  16704. });
  16705. me.addListener('ready', function () {
  16706. me.enableAutoHeight();
  16707. //trace:1764
  16708. var timer;
  16709. domUtils.on(browser.ie ? me.body : me.document, browser.webkit ? 'dragover' : 'drop', function () {
  16710. clearTimeout(timer);
  16711. timer = setTimeout(function () {
  16712. //trace:3681
  16713. adjustHeight.call(me);
  16714. }, 100);
  16715. });
  16716. //修复内容过多时,回到顶部,顶部内容被工具栏遮挡问题
  16717. var lastScrollY;
  16718. window.onscroll = function () {
  16719. if (lastScrollY === null) {
  16720. lastScrollY = this.scrollY
  16721. } else if (this.scrollY == 0 && lastScrollY != 0) {
  16722. me.window.scrollTo(0, 0);
  16723. lastScrollY = null;
  16724. }
  16725. }
  16726. });
  16727. };
  16728. // plugins/autofloat.js
  16729. ///import core
  16730. ///commands 悬浮工具栏
  16731. ///commandsName AutoFloat,autoFloatEnabled
  16732. ///commandsTitle 悬浮工具栏
  16733. /**
  16734. * modified by chengchao01
  16735. * 注意: 引入此功能后,在IE6下会将body的背景图片覆盖掉!
  16736. */
  16737. UE.plugins['autofloat'] = function () {
  16738. var me = this,
  16739. lang = me.getLang();
  16740. me.setOpt({
  16741. topOffset: 0
  16742. });
  16743. var optsAutoFloatEnabled = me.options.autoFloatEnabled !== false,
  16744. topOffset = me.options.topOffset;
  16745. //如果不固定toolbar的位置,则直接退出
  16746. if (!optsAutoFloatEnabled) {
  16747. return;
  16748. }
  16749. var uiUtils = UE.ui.uiUtils,
  16750. LteIE6 = browser.ie && browser.version <= 6,
  16751. quirks = browser.quirks;
  16752. function checkHasUI() {
  16753. if (!UE.ui) {
  16754. alert(lang.autofloatMsg);
  16755. return 0;
  16756. }
  16757. return 1;
  16758. }
  16759. function fixIE6FixedPos() {
  16760. var docStyle = document.body.style;
  16761. docStyle.backgroundImage = 'url("about:blank")';
  16762. docStyle.backgroundAttachment = 'fixed';
  16763. }
  16764. var bakCssText,
  16765. placeHolder = document.createElement('div'),
  16766. toolbarBox, orgTop,
  16767. getPosition,
  16768. flag = true; //ie7模式下需要偏移
  16769. function setFloating() {
  16770. var toobarBoxPos = domUtils.getXY(toolbarBox),
  16771. origalFloat = domUtils.getComputedStyle(toolbarBox, 'position'),
  16772. origalLeft = domUtils.getComputedStyle(toolbarBox, 'left');
  16773. toolbarBox.style.width = toolbarBox.offsetWidth + 'px';
  16774. toolbarBox.style.zIndex = me.options.zIndex * 1 + 1;
  16775. toolbarBox.parentNode.insertBefore(placeHolder, toolbarBox);
  16776. if (LteIE6 || (quirks && browser.ie)) {
  16777. if (toolbarBox.style.position != 'absolute') {
  16778. toolbarBox.style.position = 'absolute';
  16779. }
  16780. toolbarBox.style.top = (document.body.scrollTop || document.documentElement.scrollTop) - orgTop + topOffset + 'px';
  16781. } else {
  16782. if (browser.ie7Compat && flag) {
  16783. flag = false;
  16784. toolbarBox.style.left = domUtils.getXY(toolbarBox).x - document.documentElement.getBoundingClientRect().left + 2 + 'px';
  16785. }
  16786. if (toolbarBox.style.position != 'fixed') {
  16787. toolbarBox.style.position = 'fixed';
  16788. toolbarBox.style.top = topOffset + "px";
  16789. ((origalFloat == 'absolute' || origalFloat == 'relative') && parseFloat(origalLeft)) && (toolbarBox.style.left = toobarBoxPos.x + 'px');
  16790. }
  16791. }
  16792. }
  16793. function unsetFloating() {
  16794. flag = true;
  16795. if (placeHolder.parentNode) {
  16796. placeHolder.parentNode.removeChild(placeHolder);
  16797. }
  16798. toolbarBox.style.cssText = bakCssText;
  16799. }
  16800. function updateFloating() {
  16801. var rect3 = getPosition(me.container);
  16802. var offset = me.options.toolbarTopOffset || 0;
  16803. if (rect3.top < 0 && rect3.bottom - toolbarBox.offsetHeight > offset) {
  16804. setFloating();
  16805. } else {
  16806. unsetFloating();
  16807. }
  16808. }
  16809. var defer_updateFloating = utils.defer(function () {
  16810. updateFloating();
  16811. }, browser.ie ? 200 : 100, true);
  16812. me.addListener('destroy', function () {
  16813. domUtils.un(window, ['scroll', 'resize'], updateFloating);
  16814. me.removeListener('keydown', defer_updateFloating);
  16815. });
  16816. me.addListener('ready', function () {
  16817. if (checkHasUI(me)) {
  16818. //加载了ui组件,但在new时,没有加载ui,导致编辑器实例上没有ui类,所以这里做判断
  16819. if (!me.ui) {
  16820. return;
  16821. }
  16822. getPosition = uiUtils.getClientRect;
  16823. toolbarBox = me.ui.getDom('toolbarbox');
  16824. orgTop = getPosition(toolbarBox).top;
  16825. bakCssText = toolbarBox.style.cssText;
  16826. placeHolder.style.height = toolbarBox.offsetHeight + 'px';
  16827. if (LteIE6) {
  16828. fixIE6FixedPos();
  16829. }
  16830. domUtils.on(window, ['scroll', 'resize'], updateFloating);
  16831. me.addListener('keydown', defer_updateFloating);
  16832. me.addListener('beforefullscreenchange', function (t, enabled) {
  16833. if (enabled) {
  16834. unsetFloating();
  16835. }
  16836. });
  16837. me.addListener('fullscreenchanged', function (t, enabled) {
  16838. if (!enabled) {
  16839. updateFloating();
  16840. }
  16841. });
  16842. me.addListener('sourcemodechanged', function (t, enabled) {
  16843. setTimeout(function () {
  16844. updateFloating();
  16845. }, 0);
  16846. });
  16847. me.addListener("clearDoc", function () {
  16848. setTimeout(function () {
  16849. updateFloating();
  16850. }, 0);
  16851. })
  16852. }
  16853. });
  16854. };
  16855. // plugins/video.js
  16856. /**
  16857. * video插件, 为UEditor提供视频插入支持
  16858. * @file
  16859. * @since 1.2.6.1
  16860. */
  16861. UE.plugins['video'] = function () {
  16862. var me = this;
  16863. /**
  16864. * 创建插入视频字符窜
  16865. * @param url 视频地址
  16866. * @param width 视频宽度
  16867. * @param height 视频高度
  16868. * @param align 视频对齐
  16869. * @param toEmbed 是否以flash代替显示
  16870. * @param addParagraph 是否需要添加P 标签
  16871. */
  16872. function creatInsertStr(url, width, height, id, align, classname, type) {
  16873. url = utils.unhtmlForUrl(url);
  16874. align = utils.unhtml(align);
  16875. classname = utils.unhtml(classname).trim();
  16876. width = parseInt(width, 10) || 0;
  16877. height = parseInt(height, 10) || 0;
  16878. var str;
  16879. switch (type) {
  16880. case 'image':
  16881. str = '<img ' + (id ? 'id="' + id + '"' : '') + ' width="' + width + '" height="' + height + '" _url="' + url + '" class="' + classname.replace(/\bvideo-js\b/, '') + '"' +
  16882. ' src="' + me.options.UEDITOR_HOME_URL + 'themes/default/images/spacer.gif" style="background:url(' + me.options.UEDITOR_HOME_URL + 'themes/default/images/videologo.gif) no-repeat center center; border:1px solid gray;' + (align ? 'float:' + align + ';' : '') + '" />'
  16883. break;
  16884. case 'embed':
  16885. str = '<embed type="application/x-shockwave-flash" class="' + classname + '" pluginspage="http://www.macromedia.com/go/getflashplayer"' +
  16886. ' src="' + utils.html(url) + '" width="' + width + '" height="' + height + '"' + (align ? ' style="float:' + align + '"' : '') +
  16887. ' wmode="transparent" play="true" loop="false" menu="false" allowscriptaccess="never" allowfullscreen="true" >';
  16888. break;
  16889. case 'video':
  16890. var ext = url.substr(url.lastIndexOf('.') + 1);
  16891. if (ext == 'ogv') ext = 'ogg';
  16892. str = '<video' + (id ? ' id="' + id + '"' : '') + ' class="' + classname + ' video-js" ' + (align ? ' style="float:' + align + '"' : '') +
  16893. ' controls preload="none" width="' + width + '" height="' + height + '" src="' + url + '" data-setup="{}">' +
  16894. '<source src="' + url + '" type="video/' + ext + '" /></video>';
  16895. break;
  16896. }
  16897. return str;
  16898. }
  16899. function switchImgAndVideo(root, img2video) {
  16900. utils.each(root.getNodesByTagName(img2video ? 'img' : 'embed video'), function (node) {
  16901. var className = node.getAttr('class');
  16902. if (className && className.indexOf('edui-faked-video') != -1) {
  16903. var html = creatInsertStr(img2video ? node.getAttr('_url') : node.getAttr('src'), node.getAttr('width'), node.getAttr('height'), null, node.getStyle('float') || '', className, img2video ? 'embed' : 'image');
  16904. node.parentNode.replaceChild(UE.uNode.createElement(html), node);
  16905. }
  16906. if (className && className.indexOf('edui-upload-video') != -1) {
  16907. var html = creatInsertStr(img2video ? node.getAttr('_url') : node.getAttr('src'), node.getAttr('width'), node.getAttr('height'), null, node.getStyle('float') || '', className, img2video ? 'video' : 'image');
  16908. node.parentNode.replaceChild(UE.uNode.createElement(html), node);
  16909. }
  16910. })
  16911. }
  16912. me.addOutputRule(function (root) {
  16913. switchImgAndVideo(root, true)
  16914. });
  16915. me.addInputRule(function (root) {
  16916. switchImgAndVideo(root)
  16917. });
  16918. /**
  16919. * 插入视频
  16920. * @command insertvideo
  16921. * @method execCommand
  16922. * @param { String } cmd 命令字符串
  16923. * @param { Object } videoAttr 键值对对象, 描述一个视频的所有属性
  16924. * @example
  16925. * ```javascript
  16926. *
  16927. * var videoAttr = {
  16928. * //视频地址
  16929. * url: 'http://www.youku.com/xxx',
  16930. * //视频宽高值, 单位px
  16931. * width: 200,
  16932. * height: 100
  16933. * };
  16934. *
  16935. * //editor 是编辑器实例
  16936. * //向编辑器插入单个视频
  16937. * editor.execCommand( 'insertvideo', videoAttr );
  16938. * ```
  16939. */
  16940. /**
  16941. * 插入视频
  16942. * @command insertvideo
  16943. * @method execCommand
  16944. * @param { String } cmd 命令字符串
  16945. * @param { Array } videoArr 需要插入的视频的数组, 其中的每一个元素都是一个键值对对象, 描述了一个视频的所有属性
  16946. * @example
  16947. * ```javascript
  16948. *
  16949. * var videoAttr1 = {
  16950. * //视频地址
  16951. * url: 'http://www.youku.com/xxx',
  16952. * //视频宽高值, 单位px
  16953. * width: 200,
  16954. * height: 100
  16955. * },
  16956. * videoAttr2 = {
  16957. * //视频地址
  16958. * url: 'http://www.youku.com/xxx',
  16959. * //视频宽高值, 单位px
  16960. * width: 200,
  16961. * height: 100
  16962. * }
  16963. *
  16964. * //editor 是编辑器实例
  16965. * //该方法将会向编辑器内插入两个视频
  16966. * editor.execCommand( 'insertvideo', [ videoAttr1, videoAttr2 ] );
  16967. * ```
  16968. */
  16969. /**
  16970. * 查询当前光标所在处是否是一个视频
  16971. * @command insertvideo
  16972. * @method queryCommandState
  16973. * @param { String } cmd 需要查询的命令字符串
  16974. * @return { int } 如果当前光标所在处的元素是一个视频对象, 则返回1,否则返回0
  16975. * @example
  16976. * ```javascript
  16977. *
  16978. * //editor 是编辑器实例
  16979. * editor.queryCommandState( 'insertvideo' );
  16980. * ```
  16981. */
  16982. me.commands["insertvideo"] = {
  16983. execCommand: function (cmd, videoObjs, type) {
  16984. videoObjs = utils.isArray(videoObjs) ? videoObjs : [videoObjs];
  16985. var html = [],
  16986. id = 'tmpVedio',
  16987. cl;
  16988. for (var i = 0, vi, len = videoObjs.length; i < len; i++) {
  16989. vi = videoObjs[i];
  16990. cl = (type == 'upload' ? 'edui-upload-video video-js vjs-default-skin' : 'edui-faked-video');
  16991. html.push(creatInsertStr(vi.url, vi.width || 420, vi.height || 280, id + i, null, cl, 'image'));
  16992. }
  16993. me.execCommand("inserthtml", html.join(""), true);
  16994. var rng = this.selection.getRange();
  16995. for (var i = 0, len = videoObjs.length; i < len; i++) {
  16996. var img = this.document.getElementById('tmpVedio' + i);
  16997. domUtils.removeAttributes(img, 'id');
  16998. rng.selectNode(img).select();
  16999. me.execCommand('imagefloat', videoObjs[i].align)
  17000. }
  17001. },
  17002. queryCommandState: function () {
  17003. var img = me.selection.getRange().getClosedNode(),
  17004. flag = img && (img.className == "edui-faked-video" || img.className.indexOf("edui-upload-video") != -1);
  17005. return flag ? 1 : 0;
  17006. }
  17007. };
  17008. };
  17009. // plugins/table.core.js
  17010. /**
  17011. * Created with JetBrains WebStorm.
  17012. * User: taoqili
  17013. * Date: 13-1-18
  17014. * Time: 上午11:09
  17015. * To change this template use File | Settings | File Templates.
  17016. */
  17017. /**
  17018. * UE表格操作类
  17019. * @param table
  17020. * @constructor
  17021. */
  17022. (function () {
  17023. var UETable = UE.UETable = function (table) {
  17024. this.table = table;
  17025. this.indexTable = [];
  17026. this.selectedTds = [];
  17027. this.cellsRange = {};
  17028. this.update(table);
  17029. };
  17030. //===以下为静态工具方法===
  17031. UETable.removeSelectedClass = function (cells) {
  17032. utils.each(cells, function (cell) {
  17033. domUtils.removeClasses(cell, "selectTdClass");
  17034. })
  17035. };
  17036. UETable.addSelectedClass = function (cells) {
  17037. utils.each(cells, function (cell) {
  17038. domUtils.addClass(cell, "selectTdClass");
  17039. })
  17040. };
  17041. UETable.isEmptyBlock = function (node) {
  17042. var reg = new RegExp(domUtils.fillChar, 'g');
  17043. if (node[browser.ie ? 'innerText' : 'textContent'].replace(/^\s*$/, '').replace(reg, '').length > 0) {
  17044. return 0;
  17045. }
  17046. for (var i in dtd.$isNotEmpty)
  17047. if (dtd.$isNotEmpty.hasOwnProperty(i)) {
  17048. if (node.getElementsByTagName(i).length) {
  17049. return 0;
  17050. }
  17051. }
  17052. return 1;
  17053. };
  17054. UETable.getWidth = function (cell) {
  17055. if (!cell) return 0;
  17056. return parseInt(domUtils.getComputedStyle(cell, "width"), 10);
  17057. };
  17058. /**
  17059. * 获取单元格或者单元格组的“对齐”状态。 如果当前的检测对象是一个单元格组, 只有在满足所有单元格的 水平和竖直 对齐属性都相同的
  17060. * 条件时才会返回其状态值,否则将返回null; 如果当前只检测了一个单元格, 则直接返回当前单元格的对齐状态;
  17061. * @param table cell or table cells , 支持单个单元格dom对象 或者 单元格dom对象数组
  17062. * @return { align: 'left' || 'right' || 'center', valign: 'top' || 'middle' || 'bottom' } 或者 null
  17063. */
  17064. UETable.getTableCellAlignState = function (cells) {
  17065. !utils.isArray(cells) && (cells = [cells]);
  17066. var result = {},
  17067. status = ['align', 'valign'],
  17068. tempStatus = null,
  17069. isSame = true; //状态是否相同
  17070. utils.each(cells, function (cellNode) {
  17071. utils.each(status, function (currentState) {
  17072. tempStatus = cellNode.getAttribute(currentState);
  17073. if (!result[currentState] && tempStatus) {
  17074. result[currentState] = tempStatus;
  17075. } else if (!result[currentState] || (tempStatus !== result[currentState])) {
  17076. isSame = false;
  17077. return false;
  17078. }
  17079. });
  17080. return isSame;
  17081. });
  17082. return isSame ? result : null;
  17083. };
  17084. /**
  17085. * 根据当前选区获取相关的table信息
  17086. * @return {Object}
  17087. */
  17088. UETable.getTableItemsByRange = function (editor) {
  17089. var start = editor.selection.getStart();
  17090. //ff下会选中bookmark
  17091. if (start && start.id && start.id.indexOf('_baidu_bookmark_start_') === 0 && start.nextSibling) {
  17092. start = start.nextSibling;
  17093. }
  17094. //在table或者td边缘有可能存在选中tr的情况
  17095. var cell = start && domUtils.findParentByTagName(start, ["td", "th"], true),
  17096. tr = cell && cell.parentNode,
  17097. caption = start && domUtils.findParentByTagName(start, 'caption', true),
  17098. table = caption ? caption.parentNode : tr && tr.parentNode.parentNode;
  17099. return {
  17100. cell: cell,
  17101. tr: tr,
  17102. table: table,
  17103. caption: caption
  17104. }
  17105. };
  17106. UETable.getUETableBySelected = function (editor) {
  17107. var table = UETable.getTableItemsByRange(editor).table;
  17108. if (table && table.ueTable && table.ueTable.selectedTds.length) {
  17109. return table.ueTable;
  17110. }
  17111. return null;
  17112. };
  17113. UETable.getDefaultValue = function (editor, table) {
  17114. var borderMap = {
  17115. thin: '0px',
  17116. medium: '1px',
  17117. thick: '2px'
  17118. },
  17119. tableBorder, tdPadding, tdBorder, tmpValue;
  17120. if (!table) {
  17121. table = editor.document.createElement('table');
  17122. table.insertRow(0).insertCell(0).innerHTML = 'xxx';
  17123. editor.body.appendChild(table);
  17124. var td = table.getElementsByTagName('td')[0];
  17125. tmpValue = domUtils.getComputedStyle(table, 'border-left-width');
  17126. tableBorder = parseInt(borderMap[tmpValue] || tmpValue, 10);
  17127. tmpValue = domUtils.getComputedStyle(td, 'padding-left');
  17128. tdPadding = parseInt(borderMap[tmpValue] || tmpValue, 10);
  17129. tmpValue = domUtils.getComputedStyle(td, 'border-left-width');
  17130. tdBorder = parseInt(borderMap[tmpValue] || tmpValue, 10);
  17131. domUtils.remove(table);
  17132. return {
  17133. tableBorder: tableBorder,
  17134. tdPadding: tdPadding,
  17135. tdBorder: tdBorder
  17136. };
  17137. } else {
  17138. td = table.getElementsByTagName('td')[0];
  17139. tmpValue = domUtils.getComputedStyle(table, 'border-left-width');
  17140. tableBorder = parseInt(borderMap[tmpValue] || tmpValue, 10);
  17141. tmpValue = domUtils.getComputedStyle(td, 'padding-left');
  17142. tdPadding = parseInt(borderMap[tmpValue] || tmpValue, 10);
  17143. tmpValue = domUtils.getComputedStyle(td, 'border-left-width');
  17144. tdBorder = parseInt(borderMap[tmpValue] || tmpValue, 10);
  17145. return {
  17146. tableBorder: tableBorder,
  17147. tdPadding: tdPadding,
  17148. tdBorder: tdBorder
  17149. };
  17150. }
  17151. };
  17152. /**
  17153. * 根据当前点击的td或者table获取索引对象
  17154. * @param tdOrTable
  17155. */
  17156. UETable.getUETable = function (tdOrTable) {
  17157. var tag = tdOrTable.tagName.toLowerCase();
  17158. tdOrTable = (tag == "td" || tag == "th" || tag == 'caption') ? domUtils.findParentByTagName(tdOrTable, "table", true) : tdOrTable;
  17159. if (!tdOrTable.ueTable) {
  17160. tdOrTable.ueTable = new UETable(tdOrTable);
  17161. }
  17162. return tdOrTable.ueTable;
  17163. };
  17164. UETable.cloneCell = function (cell, ignoreMerge, keepPro) {
  17165. if (!cell || utils.isString(cell)) {
  17166. return this.table.ownerDocument.createElement(cell || 'td');
  17167. }
  17168. var flag = domUtils.hasClass(cell, "selectTdClass");
  17169. flag && domUtils.removeClasses(cell, "selectTdClass");
  17170. var tmpCell = cell.cloneNode(true);
  17171. if (ignoreMerge) {
  17172. tmpCell.rowSpan = tmpCell.colSpan = 1;
  17173. }
  17174. //去掉宽高
  17175. !keepPro && domUtils.removeAttributes(tmpCell, 'width height');
  17176. !keepPro && domUtils.removeAttributes(tmpCell, 'style');
  17177. tmpCell.style.borderLeftStyle = "";
  17178. tmpCell.style.borderTopStyle = "";
  17179. tmpCell.style.borderLeftColor = cell.style.borderRightColor;
  17180. tmpCell.style.borderLeftWidth = cell.style.borderRightWidth;
  17181. tmpCell.style.borderTopColor = cell.style.borderBottomColor;
  17182. tmpCell.style.borderTopWidth = cell.style.borderBottomWidth;
  17183. flag && domUtils.addClass(cell, "selectTdClass");
  17184. return tmpCell;
  17185. }
  17186. UETable.prototype = {
  17187. getMaxRows: function () {
  17188. var rows = this.table.rows,
  17189. maxLen = 1;
  17190. for (var i = 0, row; row = rows[i]; i++) {
  17191. var currentMax = 1;
  17192. for (var j = 0, cj; cj = row.cells[j++];) {
  17193. currentMax = Math.max(cj.rowSpan || 1, currentMax);
  17194. }
  17195. maxLen = Math.max(currentMax + i, maxLen);
  17196. }
  17197. return maxLen;
  17198. },
  17199. /**
  17200. * 获取当前表格的最大列数
  17201. */
  17202. getMaxCols: function () {
  17203. var rows = this.table.rows,
  17204. maxLen = 0,
  17205. cellRows = {};
  17206. for (var i = 0, row; row = rows[i]; i++) {
  17207. var cellsNum = 0;
  17208. for (var j = 0, cj; cj = row.cells[j++];) {
  17209. cellsNum += (cj.colSpan || 1);
  17210. if (cj.rowSpan && cj.rowSpan > 1) {
  17211. for (var k = 1; k < cj.rowSpan; k++) {
  17212. if (!cellRows['row_' + (i + k)]) {
  17213. cellRows['row_' + (i + k)] = (cj.colSpan || 1);
  17214. } else {
  17215. cellRows['row_' + (i + k)]++
  17216. }
  17217. }
  17218. }
  17219. }
  17220. cellsNum += cellRows['row_' + i] || 0;
  17221. maxLen = Math.max(cellsNum, maxLen);
  17222. }
  17223. return maxLen;
  17224. },
  17225. getCellColIndex: function (cell) {
  17226. },
  17227. /**
  17228. * 获取当前cell旁边的单元格,
  17229. * @param cell
  17230. * @param right
  17231. */
  17232. getHSideCell: function (cell, right) {
  17233. try {
  17234. var cellInfo = this.getCellInfo(cell),
  17235. previewRowIndex, previewColIndex;
  17236. var len = this.selectedTds.length,
  17237. range = this.cellsRange;
  17238. //首行或者首列没有前置单元格
  17239. if ((!right && (!len ? !cellInfo.colIndex : !range.beginColIndex)) || (right && (!len ? (cellInfo.colIndex == (this.colsNum - 1)) : (range.endColIndex == this.colsNum - 1)))) return null;
  17240. previewRowIndex = !len ? cellInfo.rowIndex : range.beginRowIndex;
  17241. previewColIndex = !right ? (!len ? (cellInfo.colIndex < 1 ? 0 : (cellInfo.colIndex - 1)) : range.beginColIndex - 1) :
  17242. (!len ? cellInfo.colIndex + 1 : range.endColIndex + 1);
  17243. return this.getCell(this.indexTable[previewRowIndex][previewColIndex].rowIndex, this.indexTable[previewRowIndex][previewColIndex].cellIndex);
  17244. } catch (e) {
  17245. showError(e);
  17246. }
  17247. },
  17248. getTabNextCell: function (cell, preRowIndex) {
  17249. var cellInfo = this.getCellInfo(cell),
  17250. rowIndex = preRowIndex || cellInfo.rowIndex,
  17251. colIndex = cellInfo.colIndex + 1 + (cellInfo.colSpan - 1),
  17252. nextCell;
  17253. try {
  17254. nextCell = this.getCell(this.indexTable[rowIndex][colIndex].rowIndex, this.indexTable[rowIndex][colIndex].cellIndex);
  17255. } catch (e) {
  17256. try {
  17257. rowIndex = rowIndex * 1 + 1;
  17258. colIndex = 0;
  17259. nextCell = this.getCell(this.indexTable[rowIndex][colIndex].rowIndex, this.indexTable[rowIndex][colIndex].cellIndex);
  17260. } catch (e) {}
  17261. }
  17262. return nextCell;
  17263. },
  17264. /**
  17265. * 获取视觉上的后置单元格
  17266. * @param cell
  17267. * @param bottom
  17268. */
  17269. getVSideCell: function (cell, bottom, ignoreRange) {
  17270. try {
  17271. var cellInfo = this.getCellInfo(cell),
  17272. nextRowIndex, nextColIndex;
  17273. var len = this.selectedTds.length && !ignoreRange,
  17274. range = this.cellsRange;
  17275. //末行或者末列没有后置单元格
  17276. if ((!bottom && (cellInfo.rowIndex == 0)) || (bottom && (!len ? (cellInfo.rowIndex + cellInfo.rowSpan > this.rowsNum - 1) : (range.endRowIndex == this.rowsNum - 1)))) return null;
  17277. nextRowIndex = !bottom ? (!len ? cellInfo.rowIndex - 1 : range.beginRowIndex - 1) :
  17278. (!len ? (cellInfo.rowIndex + cellInfo.rowSpan) : range.endRowIndex + 1);
  17279. nextColIndex = !len ? cellInfo.colIndex : range.beginColIndex;
  17280. return this.getCell(this.indexTable[nextRowIndex][nextColIndex].rowIndex, this.indexTable[nextRowIndex][nextColIndex].cellIndex);
  17281. } catch (e) {
  17282. showError(e);
  17283. }
  17284. },
  17285. /**
  17286. * 获取相同结束位置的单元格,xOrY指代了是获取x轴相同还是y轴相同
  17287. */
  17288. getSameEndPosCells: function (cell, xOrY) {
  17289. try {
  17290. var flag = (xOrY.toLowerCase() === "x"),
  17291. end = domUtils.getXY(cell)[flag ? 'x' : 'y'] + cell["offset" + (flag ? 'Width' : 'Height')],
  17292. rows = this.table.rows,
  17293. cells = null,
  17294. returns = [];
  17295. for (var i = 0; i < this.rowsNum; i++) {
  17296. cells = rows[i].cells;
  17297. for (var j = 0, tmpCell; tmpCell = cells[j++];) {
  17298. var tmpEnd = domUtils.getXY(tmpCell)[flag ? 'x' : 'y'] + tmpCell["offset" + (flag ? 'Width' : 'Height')];
  17299. //对应行的td已经被上面行rowSpan了
  17300. if (tmpEnd > end && flag) break;
  17301. if (cell == tmpCell || end == tmpEnd) {
  17302. //只获取单一的单元格
  17303. //todo 仅获取单一单元格在特定情况下会造成returns为空,从而影响后续的拖拽实现,修正这个。需考虑性能
  17304. if (tmpCell[flag ? "colSpan" : "rowSpan"] == 1) {
  17305. returns.push(tmpCell);
  17306. }
  17307. if (flag) break;
  17308. }
  17309. }
  17310. }
  17311. return returns;
  17312. } catch (e) {
  17313. showError(e);
  17314. }
  17315. },
  17316. setCellContent: function (cell, content) {
  17317. cell.innerHTML = content || (browser.ie ? domUtils.fillChar : "<br />");
  17318. },
  17319. cloneCell: UETable.cloneCell,
  17320. /**
  17321. * 获取跟当前单元格的右边竖线为左边的所有未合并单元格
  17322. */
  17323. getSameStartPosXCells: function (cell) {
  17324. try {
  17325. var start = domUtils.getXY(cell).x + cell.offsetWidth,
  17326. rows = this.table.rows,
  17327. cells, returns = [];
  17328. for (var i = 0; i < this.rowsNum; i++) {
  17329. cells = rows[i].cells;
  17330. for (var j = 0, tmpCell; tmpCell = cells[j++];) {
  17331. var tmpStart = domUtils.getXY(tmpCell).x;
  17332. if (tmpStart > start) break;
  17333. if (tmpStart == start && tmpCell.colSpan == 1) {
  17334. returns.push(tmpCell);
  17335. break;
  17336. }
  17337. }
  17338. }
  17339. return returns;
  17340. } catch (e) {
  17341. showError(e);
  17342. }
  17343. },
  17344. /**
  17345. * 更新table对应的索引表
  17346. */
  17347. update: function (table) {
  17348. this.table = table || this.table;
  17349. this.selectedTds = [];
  17350. this.cellsRange = {};
  17351. this.indexTable = [];
  17352. var rows = this.table.rows,
  17353. rowsNum = this.getMaxRows(),
  17354. dNum = rowsNum - rows.length,
  17355. colsNum = this.getMaxCols();
  17356. while (dNum--) {
  17357. this.table.insertRow(rows.length);
  17358. }
  17359. this.rowsNum = rowsNum;
  17360. this.colsNum = colsNum;
  17361. for (var i = 0, len = rows.length; i < len; i++) {
  17362. this.indexTable[i] = new Array(colsNum);
  17363. }
  17364. //填充索引表
  17365. for (var rowIndex = 0, row; row = rows[rowIndex]; rowIndex++) {
  17366. for (var cellIndex = 0, cell, cells = row.cells; cell = cells[cellIndex]; cellIndex++) {
  17367. //修正整行被rowSpan时导致的行数计算错误
  17368. if (cell.rowSpan > rowsNum) {
  17369. cell.rowSpan = rowsNum;
  17370. }
  17371. var colIndex = cellIndex,
  17372. rowSpan = cell.rowSpan || 1,
  17373. colSpan = cell.colSpan || 1;
  17374. //当已经被上一行rowSpan或者被前一列colSpan了,则跳到下一个单元格进行
  17375. while (this.indexTable[rowIndex][colIndex]) colIndex++;
  17376. for (var j = 0; j < rowSpan; j++) {
  17377. for (var k = 0; k < colSpan; k++) {
  17378. this.indexTable[rowIndex + j][colIndex + k] = {
  17379. rowIndex: rowIndex,
  17380. cellIndex: cellIndex,
  17381. colIndex: colIndex,
  17382. rowSpan: rowSpan,
  17383. colSpan: colSpan
  17384. }
  17385. }
  17386. }
  17387. }
  17388. }
  17389. //修复残缺td
  17390. for (j = 0; j < rowsNum; j++) {
  17391. for (k = 0; k < colsNum; k++) {
  17392. if (this.indexTable[j][k] === undefined) {
  17393. row = rows[j];
  17394. cell = row.cells[row.cells.length - 1];
  17395. cell = cell ? cell.cloneNode(true) : this.table.ownerDocument.createElement("td");
  17396. this.setCellContent(cell);
  17397. if (cell.colSpan !== 1) cell.colSpan = 1;
  17398. if (cell.rowSpan !== 1) cell.rowSpan = 1;
  17399. row.appendChild(cell);
  17400. this.indexTable[j][k] = {
  17401. rowIndex: j,
  17402. cellIndex: cell.cellIndex,
  17403. colIndex: k,
  17404. rowSpan: 1,
  17405. colSpan: 1
  17406. }
  17407. }
  17408. }
  17409. }
  17410. //当框选后删除行或者列后撤销,需要重建选区。
  17411. var tds = domUtils.getElementsByTagName(this.table, "td"),
  17412. selectTds = [];
  17413. utils.each(tds, function (td) {
  17414. if (domUtils.hasClass(td, "selectTdClass")) {
  17415. selectTds.push(td);
  17416. }
  17417. });
  17418. if (selectTds.length) {
  17419. var start = selectTds[0],
  17420. end = selectTds[selectTds.length - 1],
  17421. startInfo = this.getCellInfo(start),
  17422. endInfo = this.getCellInfo(end);
  17423. this.selectedTds = selectTds;
  17424. this.cellsRange = {
  17425. beginRowIndex: startInfo.rowIndex,
  17426. beginColIndex: startInfo.colIndex,
  17427. endRowIndex: endInfo.rowIndex + endInfo.rowSpan - 1,
  17428. endColIndex: endInfo.colIndex + endInfo.colSpan - 1
  17429. };
  17430. }
  17431. //给第一行设置firstRow的样式名称,在排序图标的样式上使用到
  17432. if (!domUtils.hasClass(this.table.rows[0], "firstRow")) {
  17433. domUtils.addClass(this.table.rows[0], "firstRow");
  17434. for (var i = 1; i < this.table.rows.length; i++) {
  17435. domUtils.removeClasses(this.table.rows[i], "firstRow");
  17436. }
  17437. }
  17438. },
  17439. /**
  17440. * 获取单元格的索引信息
  17441. */
  17442. getCellInfo: function (cell) {
  17443. if (!cell) return;
  17444. var cellIndex = cell.cellIndex,
  17445. rowIndex = cell.parentNode.rowIndex,
  17446. rowInfo = this.indexTable[rowIndex],
  17447. numCols = this.colsNum;
  17448. for (var colIndex = cellIndex; colIndex < numCols; colIndex++) {
  17449. var cellInfo = rowInfo[colIndex];
  17450. if (cellInfo.rowIndex === rowIndex && cellInfo.cellIndex === cellIndex) {
  17451. return cellInfo;
  17452. }
  17453. }
  17454. },
  17455. /**
  17456. * 根据行列号获取单元格
  17457. */
  17458. getCell: function (rowIndex, cellIndex) {
  17459. return rowIndex < this.rowsNum && this.table.rows[rowIndex].cells[cellIndex] || null;
  17460. },
  17461. /**
  17462. * 删除单元格
  17463. */
  17464. deleteCell: function (cell, rowIndex) {
  17465. rowIndex = typeof rowIndex == 'number' ? rowIndex : cell.parentNode.rowIndex;
  17466. var row = this.table.rows[rowIndex];
  17467. row.deleteCell(cell.cellIndex);
  17468. },
  17469. /**
  17470. * 根据始末两个单元格获取被框选的所有单元格范围
  17471. */
  17472. getCellsRange: function (cellA, cellB) {
  17473. function checkRange(beginRowIndex, beginColIndex, endRowIndex, endColIndex) {
  17474. var tmpBeginRowIndex = beginRowIndex,
  17475. tmpBeginColIndex = beginColIndex,
  17476. tmpEndRowIndex = endRowIndex,
  17477. tmpEndColIndex = endColIndex,
  17478. cellInfo, colIndex, rowIndex;
  17479. // 通过indexTable检查是否存在超出TableRange上边界的情况
  17480. if (beginRowIndex > 0) {
  17481. for (colIndex = beginColIndex; colIndex < endColIndex; colIndex++) {
  17482. cellInfo = me.indexTable[beginRowIndex][colIndex];
  17483. rowIndex = cellInfo.rowIndex;
  17484. if (rowIndex < beginRowIndex) {
  17485. tmpBeginRowIndex = Math.min(rowIndex, tmpBeginRowIndex);
  17486. }
  17487. }
  17488. }
  17489. // 通过indexTable检查是否存在超出TableRange右边界的情况
  17490. if (endColIndex < me.colsNum) {
  17491. for (rowIndex = beginRowIndex; rowIndex < endRowIndex; rowIndex++) {
  17492. cellInfo = me.indexTable[rowIndex][endColIndex];
  17493. colIndex = cellInfo.colIndex + cellInfo.colSpan - 1;
  17494. if (colIndex > endColIndex) {
  17495. tmpEndColIndex = Math.max(colIndex, tmpEndColIndex);
  17496. }
  17497. }
  17498. }
  17499. // 检查是否有超出TableRange下边界的情况
  17500. if (endRowIndex < me.rowsNum) {
  17501. for (colIndex = beginColIndex; colIndex < endColIndex; colIndex++) {
  17502. cellInfo = me.indexTable[endRowIndex][colIndex];
  17503. rowIndex = cellInfo.rowIndex + cellInfo.rowSpan - 1;
  17504. if (rowIndex > endRowIndex) {
  17505. tmpEndRowIndex = Math.max(rowIndex, tmpEndRowIndex);
  17506. }
  17507. }
  17508. }
  17509. // 检查是否有超出TableRange左边界的情况
  17510. if (beginColIndex > 0) {
  17511. for (rowIndex = beginRowIndex; rowIndex < endRowIndex; rowIndex++) {
  17512. cellInfo = me.indexTable[rowIndex][beginColIndex];
  17513. colIndex = cellInfo.colIndex;
  17514. if (colIndex < beginColIndex) {
  17515. tmpBeginColIndex = Math.min(cellInfo.colIndex, tmpBeginColIndex);
  17516. }
  17517. }
  17518. }
  17519. //递归调用直至所有完成所有框选单元格的扩展
  17520. if (tmpBeginRowIndex != beginRowIndex || tmpBeginColIndex != beginColIndex || tmpEndRowIndex != endRowIndex || tmpEndColIndex != endColIndex) {
  17521. return checkRange(tmpBeginRowIndex, tmpBeginColIndex, tmpEndRowIndex, tmpEndColIndex);
  17522. } else {
  17523. // 不需要扩展TableRange的情况
  17524. return {
  17525. beginRowIndex: beginRowIndex,
  17526. beginColIndex: beginColIndex,
  17527. endRowIndex: endRowIndex,
  17528. endColIndex: endColIndex
  17529. };
  17530. }
  17531. }
  17532. try {
  17533. var me = this,
  17534. cellAInfo = me.getCellInfo(cellA);
  17535. if (cellA === cellB) {
  17536. return {
  17537. beginRowIndex: cellAInfo.rowIndex,
  17538. beginColIndex: cellAInfo.colIndex,
  17539. endRowIndex: cellAInfo.rowIndex + cellAInfo.rowSpan - 1,
  17540. endColIndex: cellAInfo.colIndex + cellAInfo.colSpan - 1
  17541. };
  17542. }
  17543. var cellBInfo = me.getCellInfo(cellB);
  17544. // 计算TableRange的四个边
  17545. var beginRowIndex = Math.min(cellAInfo.rowIndex, cellBInfo.rowIndex),
  17546. beginColIndex = Math.min(cellAInfo.colIndex, cellBInfo.colIndex),
  17547. endRowIndex = Math.max(cellAInfo.rowIndex + cellAInfo.rowSpan - 1, cellBInfo.rowIndex + cellBInfo.rowSpan - 1),
  17548. endColIndex = Math.max(cellAInfo.colIndex + cellAInfo.colSpan - 1, cellBInfo.colIndex + cellBInfo.colSpan - 1);
  17549. return checkRange(beginRowIndex, beginColIndex, endRowIndex, endColIndex);
  17550. } catch (e) {
  17551. //throw e;
  17552. }
  17553. },
  17554. /**
  17555. * 依据cellsRange获取对应的单元格集合
  17556. */
  17557. getCells: function (range) {
  17558. //每次获取cells之前必须先清除上次的选择,否则会对后续获取操作造成影响
  17559. this.clearSelected();
  17560. var beginRowIndex = range.beginRowIndex,
  17561. beginColIndex = range.beginColIndex,
  17562. endRowIndex = range.endRowIndex,
  17563. endColIndex = range.endColIndex,
  17564. cellInfo, rowIndex, colIndex, tdHash = {},
  17565. returnTds = [];
  17566. for (var i = beginRowIndex; i <= endRowIndex; i++) {
  17567. for (var j = beginColIndex; j <= endColIndex; j++) {
  17568. cellInfo = this.indexTable[i][j];
  17569. rowIndex = cellInfo.rowIndex;
  17570. colIndex = cellInfo.colIndex;
  17571. // 如果Cells里已经包含了此Cell则跳过
  17572. var key = rowIndex + '|' + colIndex;
  17573. if (tdHash[key]) continue;
  17574. tdHash[key] = 1;
  17575. if (rowIndex < i || colIndex < j || rowIndex + cellInfo.rowSpan - 1 > endRowIndex || colIndex + cellInfo.colSpan - 1 > endColIndex) {
  17576. return null;
  17577. }
  17578. returnTds.push(this.getCell(rowIndex, cellInfo.cellIndex));
  17579. }
  17580. }
  17581. return returnTds;
  17582. },
  17583. /**
  17584. * 清理已经选中的单元格
  17585. */
  17586. clearSelected: function () {
  17587. UETable.removeSelectedClass(this.selectedTds);
  17588. this.selectedTds = [];
  17589. this.cellsRange = {};
  17590. },
  17591. /**
  17592. * 根据range设置已经选中的单元格
  17593. */
  17594. setSelected: function (range) {
  17595. var cells = this.getCells(range);
  17596. UETable.addSelectedClass(cells);
  17597. this.selectedTds = cells;
  17598. this.cellsRange = range;
  17599. },
  17600. isFullRow: function () {
  17601. var range = this.cellsRange;
  17602. return (range.endColIndex - range.beginColIndex + 1) == this.colsNum;
  17603. },
  17604. isFullCol: function () {
  17605. var range = this.cellsRange,
  17606. table = this.table,
  17607. ths = table.getElementsByTagName("th"),
  17608. rows = range.endRowIndex - range.beginRowIndex + 1;
  17609. return !ths.length ? rows == this.rowsNum : rows == this.rowsNum || (rows == this.rowsNum - 1);
  17610. },
  17611. /**
  17612. * 获取视觉上的前置单元格,默认是左边,top传入时
  17613. * @param cell
  17614. * @param top
  17615. */
  17616. getNextCell: function (cell, bottom, ignoreRange) {
  17617. try {
  17618. var cellInfo = this.getCellInfo(cell),
  17619. nextRowIndex, nextColIndex;
  17620. var len = this.selectedTds.length && !ignoreRange,
  17621. range = this.cellsRange;
  17622. //末行或者末列没有后置单元格
  17623. if ((!bottom && (cellInfo.rowIndex == 0)) || (bottom && (!len ? (cellInfo.rowIndex + cellInfo.rowSpan > this.rowsNum - 1) : (range.endRowIndex == this.rowsNum - 1)))) return null;
  17624. nextRowIndex = !bottom ? (!len ? cellInfo.rowIndex - 1 : range.beginRowIndex - 1) :
  17625. (!len ? (cellInfo.rowIndex + cellInfo.rowSpan) : range.endRowIndex + 1);
  17626. nextColIndex = !len ? cellInfo.colIndex : range.beginColIndex;
  17627. return this.getCell(this.indexTable[nextRowIndex][nextColIndex].rowIndex, this.indexTable[nextRowIndex][nextColIndex].cellIndex);
  17628. } catch (e) {
  17629. showError(e);
  17630. }
  17631. },
  17632. getPreviewCell: function (cell, top) {
  17633. try {
  17634. var cellInfo = this.getCellInfo(cell),
  17635. previewRowIndex, previewColIndex;
  17636. var len = this.selectedTds.length,
  17637. range = this.cellsRange;
  17638. //首行或者首列没有前置单元格
  17639. if ((!top && (!len ? !cellInfo.colIndex : !range.beginColIndex)) || (top && (!len ? (cellInfo.rowIndex > (this.colsNum - 1)) : (range.endColIndex == this.colsNum - 1)))) return null;
  17640. previewRowIndex = !top ? (!len ? cellInfo.rowIndex : range.beginRowIndex) :
  17641. (!len ? (cellInfo.rowIndex < 1 ? 0 : (cellInfo.rowIndex - 1)) : range.beginRowIndex);
  17642. previewColIndex = !top ? (!len ? (cellInfo.colIndex < 1 ? 0 : (cellInfo.colIndex - 1)) : range.beginColIndex - 1) :
  17643. (!len ? cellInfo.colIndex : range.endColIndex + 1);
  17644. return this.getCell(this.indexTable[previewRowIndex][previewColIndex].rowIndex, this.indexTable[previewRowIndex][previewColIndex].cellIndex);
  17645. } catch (e) {
  17646. showError(e);
  17647. }
  17648. },
  17649. /**
  17650. * 移动单元格中的内容
  17651. */
  17652. moveContent: function (cellTo, cellFrom) {
  17653. if (UETable.isEmptyBlock(cellFrom)) return;
  17654. if (UETable.isEmptyBlock(cellTo)) {
  17655. cellTo.innerHTML = cellFrom.innerHTML;
  17656. return;
  17657. }
  17658. var child = cellTo.lastChild;
  17659. if (child.nodeType == 3 || !dtd.$block[child.tagName]) {
  17660. cellTo.appendChild(cellTo.ownerDocument.createElement('br'))
  17661. }
  17662. while (child = cellFrom.firstChild) {
  17663. cellTo.appendChild(child);
  17664. }
  17665. },
  17666. /**
  17667. * 向右合并单元格
  17668. */
  17669. mergeRight: function (cell) {
  17670. var cellInfo = this.getCellInfo(cell),
  17671. rightColIndex = cellInfo.colIndex + cellInfo.colSpan,
  17672. rightCellInfo = this.indexTable[cellInfo.rowIndex][rightColIndex],
  17673. rightCell = this.getCell(rightCellInfo.rowIndex, rightCellInfo.cellIndex);
  17674. //合并
  17675. cell.colSpan = cellInfo.colSpan + rightCellInfo.colSpan;
  17676. //被合并的单元格不应存在宽度属性
  17677. cell.removeAttribute("width");
  17678. //移动内容
  17679. this.moveContent(cell, rightCell);
  17680. //删掉被合并的Cell
  17681. this.deleteCell(rightCell, rightCellInfo.rowIndex);
  17682. this.update();
  17683. },
  17684. /**
  17685. * 向下合并单元格
  17686. */
  17687. mergeDown: function (cell) {
  17688. var cellInfo = this.getCellInfo(cell),
  17689. downRowIndex = cellInfo.rowIndex + cellInfo.rowSpan,
  17690. downCellInfo = this.indexTable[downRowIndex][cellInfo.colIndex],
  17691. downCell = this.getCell(downCellInfo.rowIndex, downCellInfo.cellIndex);
  17692. cell.rowSpan = cellInfo.rowSpan + downCellInfo.rowSpan;
  17693. cell.removeAttribute("height");
  17694. this.moveContent(cell, downCell);
  17695. this.deleteCell(downCell, downCellInfo.rowIndex);
  17696. this.update();
  17697. },
  17698. /**
  17699. * 合并整个range中的内容
  17700. */
  17701. mergeRange: function () {
  17702. //由于合并操作可以在任意时刻进行,所以无法通过鼠标位置等信息实时生成range,只能通过缓存实例中的cellsRange对象来访问
  17703. var range = this.cellsRange,
  17704. leftTopCell = this.getCell(range.beginRowIndex, this.indexTable[range.beginRowIndex][range.beginColIndex].cellIndex);
  17705. if (leftTopCell.tagName == "TH" && range.endRowIndex !== range.beginRowIndex) {
  17706. var index = this.indexTable,
  17707. info = this.getCellInfo(leftTopCell);
  17708. leftTopCell = this.getCell(1, index[1][info.colIndex].cellIndex);
  17709. range = this.getCellsRange(leftTopCell, this.getCell(index[this.rowsNum - 1][info.colIndex].rowIndex, index[this.rowsNum - 1][info.colIndex].cellIndex));
  17710. }
  17711. // 删除剩余的Cells
  17712. var cells = this.getCells(range);
  17713. for (var i = 0, ci; ci = cells[i++];) {
  17714. if (ci !== leftTopCell) {
  17715. this.moveContent(leftTopCell, ci);
  17716. this.deleteCell(ci);
  17717. }
  17718. }
  17719. // 修改左上角Cell的rowSpan和colSpan,并调整宽度属性设置
  17720. leftTopCell.rowSpan = range.endRowIndex - range.beginRowIndex + 1;
  17721. leftTopCell.rowSpan > 1 && leftTopCell.removeAttribute("height");
  17722. leftTopCell.colSpan = range.endColIndex - range.beginColIndex + 1;
  17723. leftTopCell.colSpan > 1 && leftTopCell.removeAttribute("width");
  17724. if (leftTopCell.rowSpan == this.rowsNum && leftTopCell.colSpan != 1) {
  17725. leftTopCell.colSpan = 1;
  17726. }
  17727. if (leftTopCell.colSpan == this.colsNum && leftTopCell.rowSpan != 1) {
  17728. var rowIndex = leftTopCell.parentNode.rowIndex;
  17729. //解决IE下的表格操作问题
  17730. if (this.table.deleteRow) {
  17731. for (var i = rowIndex + 1, curIndex = rowIndex + 1, len = leftTopCell.rowSpan; i < len; i++) {
  17732. this.table.deleteRow(curIndex);
  17733. }
  17734. } else {
  17735. for (var i = 0, len = leftTopCell.rowSpan - 1; i < len; i++) {
  17736. var row = this.table.rows[rowIndex + 1];
  17737. row.parentNode.removeChild(row);
  17738. }
  17739. }
  17740. leftTopCell.rowSpan = 1;
  17741. }
  17742. this.update();
  17743. },
  17744. /**
  17745. * 插入一行单元格
  17746. */
  17747. insertRow: function (rowIndex, sourceCell) {
  17748. var numCols = this.colsNum,
  17749. table = this.table,
  17750. row = table.insertRow(rowIndex),
  17751. cell,
  17752. isInsertTitle = typeof sourceCell == 'string' && sourceCell.toUpperCase() == 'TH';
  17753. function replaceTdToTh(colIndex, cell, tableRow) {
  17754. if (colIndex == 0) {
  17755. var tr = tableRow.nextSibling || tableRow.previousSibling,
  17756. th = tr.cells[colIndex];
  17757. if (th.tagName == 'TH') {
  17758. th = cell.ownerDocument.createElement("th");
  17759. th.appendChild(cell.firstChild);
  17760. tableRow.insertBefore(th, cell);
  17761. domUtils.remove(cell)
  17762. }
  17763. } else {
  17764. if (cell.tagName == 'TH') {
  17765. var td = cell.ownerDocument.createElement("td");
  17766. td.appendChild(cell.firstChild);
  17767. tableRow.insertBefore(td, cell);
  17768. domUtils.remove(cell)
  17769. }
  17770. }
  17771. }
  17772. //首行直接插入,无需考虑部分单元格被rowspan的情况
  17773. if (rowIndex == 0 || rowIndex == this.rowsNum) {
  17774. for (var colIndex = 0; colIndex < numCols; colIndex++) {
  17775. cell = this.cloneCell(sourceCell, true);
  17776. this.setCellContent(cell);
  17777. cell.getAttribute('vAlign') && cell.setAttribute('vAlign', cell.getAttribute('vAlign'));
  17778. row.appendChild(cell);
  17779. if (!isInsertTitle) replaceTdToTh(colIndex, cell, row);
  17780. }
  17781. } else {
  17782. var infoRow = this.indexTable[rowIndex],
  17783. cellIndex = 0;
  17784. for (colIndex = 0; colIndex < numCols; colIndex++) {
  17785. var cellInfo = infoRow[colIndex];
  17786. //如果存在某个单元格的rowspan穿过待插入行的位置,则修改该单元格的rowspan即可,无需插入单元格
  17787. if (cellInfo.rowIndex < rowIndex) {
  17788. cell = this.getCell(cellInfo.rowIndex, cellInfo.cellIndex);
  17789. cell.rowSpan = cellInfo.rowSpan + 1;
  17790. } else {
  17791. cell = this.cloneCell(sourceCell, true);
  17792. this.setCellContent(cell);
  17793. row.appendChild(cell);
  17794. }
  17795. if (!isInsertTitle) replaceTdToTh(colIndex, cell, row);
  17796. }
  17797. }
  17798. //框选时插入不触发contentchange,需要手动更新索引。
  17799. this.update();
  17800. return row;
  17801. },
  17802. /**
  17803. * 删除一行单元格
  17804. * @param rowIndex
  17805. */
  17806. deleteRow: function (rowIndex) {
  17807. var row = this.table.rows[rowIndex],
  17808. infoRow = this.indexTable[rowIndex],
  17809. colsNum = this.colsNum,
  17810. count = 0; //处理计数
  17811. for (var colIndex = 0; colIndex < colsNum;) {
  17812. var cellInfo = infoRow[colIndex],
  17813. cell = this.getCell(cellInfo.rowIndex, cellInfo.cellIndex);
  17814. if (cell.rowSpan > 1) {
  17815. if (cellInfo.rowIndex == rowIndex) {
  17816. var clone = cell.cloneNode(true);
  17817. clone.rowSpan = cell.rowSpan - 1;
  17818. clone.innerHTML = "";
  17819. cell.rowSpan = 1;
  17820. var nextRowIndex = rowIndex + 1,
  17821. nextRow = this.table.rows[nextRowIndex],
  17822. insertCellIndex,
  17823. preMerged = this.getPreviewMergedCellsNum(nextRowIndex, colIndex) - count;
  17824. if (preMerged < colIndex) {
  17825. insertCellIndex = colIndex - preMerged - 1;
  17826. //nextRow.insertCell(insertCellIndex);
  17827. domUtils.insertAfter(nextRow.cells[insertCellIndex], clone);
  17828. } else {
  17829. if (nextRow.cells.length) nextRow.insertBefore(clone, nextRow.cells[0])
  17830. }
  17831. count += 1;
  17832. //cell.parentNode.removeChild(cell);
  17833. }
  17834. }
  17835. colIndex += cell.colSpan || 1;
  17836. }
  17837. var deleteTds = [],
  17838. cacheMap = {};
  17839. for (colIndex = 0; colIndex < colsNum; colIndex++) {
  17840. var tmpRowIndex = infoRow[colIndex].rowIndex,
  17841. tmpCellIndex = infoRow[colIndex].cellIndex,
  17842. key = tmpRowIndex + "_" + tmpCellIndex;
  17843. if (cacheMap[key]) continue;
  17844. cacheMap[key] = 1;
  17845. cell = this.getCell(tmpRowIndex, tmpCellIndex);
  17846. deleteTds.push(cell);
  17847. }
  17848. var mergeTds = [];
  17849. utils.each(deleteTds, function (td) {
  17850. if (td.rowSpan == 1) {
  17851. td.parentNode.removeChild(td);
  17852. } else {
  17853. mergeTds.push(td);
  17854. }
  17855. });
  17856. utils.each(mergeTds, function (td) {
  17857. td.rowSpan--;
  17858. });
  17859. row.parentNode.removeChild(row);
  17860. //浏览器方法本身存在bug,采用自定义方法删除
  17861. //this.table.deleteRow(rowIndex);
  17862. this.update();
  17863. },
  17864. insertCol: function (colIndex, sourceCell, defaultValue) {
  17865. var rowsNum = this.rowsNum,
  17866. rowIndex = 0,
  17867. tableRow, cell,
  17868. backWidth = parseInt((this.table.offsetWidth - (this.colsNum + 1) * 20 - (this.colsNum + 1)) / (this.colsNum + 1), 10),
  17869. isInsertTitleCol = typeof sourceCell == 'string' && sourceCell.toUpperCase() == 'TH';
  17870. function replaceTdToTh(rowIndex, cell, tableRow) {
  17871. if (rowIndex == 0) {
  17872. var th = cell.nextSibling || cell.previousSibling;
  17873. if (th.tagName == 'TH') {
  17874. th = cell.ownerDocument.createElement("th");
  17875. th.appendChild(cell.firstChild);
  17876. tableRow.insertBefore(th, cell);
  17877. domUtils.remove(cell)
  17878. }
  17879. } else {
  17880. if (cell.tagName == 'TH') {
  17881. var td = cell.ownerDocument.createElement("td");
  17882. td.appendChild(cell.firstChild);
  17883. tableRow.insertBefore(td, cell);
  17884. domUtils.remove(cell)
  17885. }
  17886. }
  17887. }
  17888. var preCell;
  17889. if (colIndex == 0 || colIndex == this.colsNum) {
  17890. for (; rowIndex < rowsNum; rowIndex++) {
  17891. tableRow = this.table.rows[rowIndex];
  17892. preCell = tableRow.cells[colIndex == 0 ? colIndex : tableRow.cells.length];
  17893. cell = this.cloneCell(sourceCell, true); //tableRow.insertCell(colIndex == 0 ? colIndex : tableRow.cells.length);
  17894. this.setCellContent(cell);
  17895. cell.setAttribute('vAlign', cell.getAttribute('vAlign'));
  17896. preCell && cell.setAttribute('width', preCell.getAttribute('width'));
  17897. if (!colIndex) {
  17898. tableRow.insertBefore(cell, tableRow.cells[0]);
  17899. } else {
  17900. domUtils.insertAfter(tableRow.cells[tableRow.cells.length - 1], cell);
  17901. }
  17902. if (!isInsertTitleCol) replaceTdToTh(rowIndex, cell, tableRow)
  17903. }
  17904. } else {
  17905. for (; rowIndex < rowsNum; rowIndex++) {
  17906. var cellInfo = this.indexTable[rowIndex][colIndex];
  17907. if (cellInfo.colIndex < colIndex) {
  17908. cell = this.getCell(cellInfo.rowIndex, cellInfo.cellIndex);
  17909. cell.colSpan = cellInfo.colSpan + 1;
  17910. } else {
  17911. tableRow = this.table.rows[rowIndex];
  17912. preCell = tableRow.cells[cellInfo.cellIndex];
  17913. cell = this.cloneCell(sourceCell, true); //tableRow.insertCell(cellInfo.cellIndex);
  17914. this.setCellContent(cell);
  17915. cell.setAttribute('vAlign', cell.getAttribute('vAlign'));
  17916. preCell && cell.setAttribute('width', preCell.getAttribute('width'));
  17917. //防止IE下报错
  17918. preCell ? tableRow.insertBefore(cell, preCell) : tableRow.appendChild(cell);
  17919. }
  17920. if (!isInsertTitleCol) replaceTdToTh(rowIndex, cell, tableRow);
  17921. }
  17922. }
  17923. //框选时插入不触发contentchange,需要手动更新索引
  17924. this.update();
  17925. this.updateWidth(backWidth, defaultValue || {
  17926. tdPadding: 10,
  17927. tdBorder: 1
  17928. });
  17929. },
  17930. updateWidth: function (width, defaultValue) {
  17931. var table = this.table,
  17932. tmpWidth = UETable.getWidth(table) - defaultValue.tdPadding * 2 - defaultValue.tdBorder + width;
  17933. if (tmpWidth < table.ownerDocument.body.offsetWidth) {
  17934. table.setAttribute("width", tmpWidth);
  17935. return;
  17936. }
  17937. var tds = domUtils.getElementsByTagName(this.table, "td th");
  17938. utils.each(tds, function (td) {
  17939. td.setAttribute("width", width);
  17940. })
  17941. },
  17942. deleteCol: function (colIndex) {
  17943. var indexTable = this.indexTable,
  17944. tableRows = this.table.rows,
  17945. backTableWidth = this.table.getAttribute("width"),
  17946. backTdWidth = 0,
  17947. rowsNum = this.rowsNum,
  17948. cacheMap = {};
  17949. for (var rowIndex = 0; rowIndex < rowsNum;) {
  17950. var infoRow = indexTable[rowIndex],
  17951. cellInfo = infoRow[colIndex],
  17952. key = cellInfo.rowIndex + '_' + cellInfo.colIndex;
  17953. // 跳过已经处理过的Cell
  17954. if (cacheMap[key]) continue;
  17955. cacheMap[key] = 1;
  17956. var cell = this.getCell(cellInfo.rowIndex, cellInfo.cellIndex);
  17957. if (!backTdWidth) backTdWidth = cell && parseInt(cell.offsetWidth / cell.colSpan, 10).toFixed(0);
  17958. // 如果Cell的colSpan大于1, 就修改colSpan, 否则就删掉这个Cell
  17959. if (cell.colSpan > 1) {
  17960. cell.colSpan--;
  17961. } else {
  17962. tableRows[rowIndex].deleteCell(cellInfo.cellIndex);
  17963. }
  17964. rowIndex += cellInfo.rowSpan || 1;
  17965. }
  17966. this.table.setAttribute("width", backTableWidth - backTdWidth);
  17967. this.update();
  17968. },
  17969. splitToCells: function (cell) {
  17970. var me = this,
  17971. cells = this.splitToRows(cell);
  17972. utils.each(cells, function (cell) {
  17973. me.splitToCols(cell);
  17974. })
  17975. },
  17976. splitToRows: function (cell) {
  17977. var cellInfo = this.getCellInfo(cell),
  17978. rowIndex = cellInfo.rowIndex,
  17979. colIndex = cellInfo.colIndex,
  17980. results = [];
  17981. // 修改Cell的rowSpan
  17982. cell.rowSpan = 1;
  17983. results.push(cell);
  17984. // 补齐单元格
  17985. for (var i = rowIndex, endRow = rowIndex + cellInfo.rowSpan; i < endRow; i++) {
  17986. if (i == rowIndex) continue;
  17987. var tableRow = this.table.rows[i],
  17988. tmpCell = tableRow.insertCell(colIndex - this.getPreviewMergedCellsNum(i, colIndex));
  17989. tmpCell.colSpan = cellInfo.colSpan;
  17990. this.setCellContent(tmpCell);
  17991. tmpCell.setAttribute('vAlign', cell.getAttribute('vAlign'));
  17992. tmpCell.setAttribute('align', cell.getAttribute('align'));
  17993. if (cell.style.cssText) {
  17994. tmpCell.style.cssText = cell.style.cssText;
  17995. }
  17996. results.push(tmpCell);
  17997. }
  17998. this.update();
  17999. return results;
  18000. },
  18001. getPreviewMergedCellsNum: function (rowIndex, colIndex) {
  18002. var indexRow = this.indexTable[rowIndex],
  18003. num = 0;
  18004. for (var i = 0; i < colIndex;) {
  18005. var colSpan = indexRow[i].colSpan,
  18006. tmpRowIndex = indexRow[i].rowIndex;
  18007. num += (colSpan - (tmpRowIndex == rowIndex ? 1 : 0));
  18008. i += colSpan;
  18009. }
  18010. return num;
  18011. },
  18012. splitToCols: function (cell) {
  18013. var backWidth = (cell.offsetWidth / cell.colSpan - 22).toFixed(0),
  18014. cellInfo = this.getCellInfo(cell),
  18015. rowIndex = cellInfo.rowIndex,
  18016. colIndex = cellInfo.colIndex,
  18017. results = [];
  18018. // 修改Cell的rowSpan
  18019. cell.colSpan = 1;
  18020. cell.setAttribute("width", backWidth);
  18021. results.push(cell);
  18022. // 补齐单元格
  18023. for (var j = colIndex, endCol = colIndex + cellInfo.colSpan; j < endCol; j++) {
  18024. if (j == colIndex) continue;
  18025. var tableRow = this.table.rows[rowIndex],
  18026. tmpCell = tableRow.insertCell(this.indexTable[rowIndex][j].cellIndex + 1);
  18027. tmpCell.rowSpan = cellInfo.rowSpan;
  18028. this.setCellContent(tmpCell);
  18029. tmpCell.setAttribute('vAlign', cell.getAttribute('vAlign'));
  18030. tmpCell.setAttribute('align', cell.getAttribute('align'));
  18031. tmpCell.setAttribute('width', backWidth);
  18032. if (cell.style.cssText) {
  18033. tmpCell.style.cssText = cell.style.cssText;
  18034. }
  18035. //处理th的情况
  18036. if (cell.tagName == 'TH') {
  18037. var th = cell.ownerDocument.createElement('th');
  18038. th.appendChild(tmpCell.firstChild);
  18039. th.setAttribute('vAlign', cell.getAttribute('vAlign'));
  18040. th.rowSpan = tmpCell.rowSpan;
  18041. tableRow.insertBefore(th, tmpCell);
  18042. domUtils.remove(tmpCell);
  18043. }
  18044. results.push(tmpCell);
  18045. }
  18046. this.update();
  18047. return results;
  18048. },
  18049. isLastCell: function (cell, rowsNum, colsNum) {
  18050. rowsNum = rowsNum || this.rowsNum;
  18051. colsNum = colsNum || this.colsNum;
  18052. var cellInfo = this.getCellInfo(cell);
  18053. return ((cellInfo.rowIndex + cellInfo.rowSpan) == rowsNum) &&
  18054. ((cellInfo.colIndex + cellInfo.colSpan) == colsNum);
  18055. },
  18056. getLastCell: function (cells) {
  18057. cells = cells || this.table.getElementsByTagName("td");
  18058. var firstInfo = this.getCellInfo(cells[0]);
  18059. var me = this,
  18060. last = cells[0],
  18061. tr = last.parentNode,
  18062. cellsNum = 0,
  18063. cols = 0,
  18064. rows;
  18065. utils.each(cells, function (cell) {
  18066. if (cell.parentNode == tr) cols += cell.colSpan || 1;
  18067. cellsNum += cell.rowSpan * cell.colSpan || 1;
  18068. });
  18069. rows = cellsNum / cols;
  18070. utils.each(cells, function (cell) {
  18071. if (me.isLastCell(cell, rows, cols)) {
  18072. last = cell;
  18073. return false;
  18074. }
  18075. });
  18076. return last;
  18077. },
  18078. selectRow: function (rowIndex) {
  18079. var indexRow = this.indexTable[rowIndex],
  18080. start = this.getCell(indexRow[0].rowIndex, indexRow[0].cellIndex),
  18081. end = this.getCell(indexRow[this.colsNum - 1].rowIndex, indexRow[this.colsNum - 1].cellIndex),
  18082. range = this.getCellsRange(start, end);
  18083. this.setSelected(range);
  18084. },
  18085. selectTable: function () {
  18086. var tds = this.table.getElementsByTagName("td"),
  18087. range = this.getCellsRange(tds[0], tds[tds.length - 1]);
  18088. this.setSelected(range);
  18089. },
  18090. setBackground: function (cells, value) {
  18091. if (typeof value === "string") {
  18092. utils.each(cells, function (cell) {
  18093. cell.style.backgroundColor = value;
  18094. })
  18095. } else if (typeof value === "object") {
  18096. value = utils.extend({
  18097. repeat: true,
  18098. colorList: ["#ddd", "#fff"]
  18099. }, value);
  18100. var rowIndex = this.getCellInfo(cells[0]).rowIndex,
  18101. count = 0,
  18102. colors = value.colorList,
  18103. getColor = function (list, index, repeat) {
  18104. return list[index] ? list[index] : repeat ? list[index % list.length] : "";
  18105. };
  18106. for (var i = 0, cell; cell = cells[i++];) {
  18107. var cellInfo = this.getCellInfo(cell);
  18108. cell.style.backgroundColor = getColor(colors, ((rowIndex + count) == cellInfo.rowIndex) ? count : ++count, value.repeat);
  18109. }
  18110. }
  18111. },
  18112. removeBackground: function (cells) {
  18113. utils.each(cells, function (cell) {
  18114. cell.style.backgroundColor = "";
  18115. })
  18116. }
  18117. };
  18118. function showError(e) {}
  18119. })();
  18120. // plugins/table.cmds.js
  18121. /**
  18122. * Created with JetBrains PhpStorm.
  18123. * User: taoqili
  18124. * Date: 13-2-20
  18125. * Time: 下午6:25
  18126. * To change this template use File | Settings | File Templates.
  18127. */
  18128. ;
  18129. (function () {
  18130. var UT = UE.UETable,
  18131. getTableItemsByRange = function (editor) {
  18132. return UT.getTableItemsByRange(editor);
  18133. },
  18134. getUETableBySelected = function (editor) {
  18135. return UT.getUETableBySelected(editor)
  18136. },
  18137. getDefaultValue = function (editor, table) {
  18138. return UT.getDefaultValue(editor, table);
  18139. },
  18140. getUETable = function (tdOrTable) {
  18141. return UT.getUETable(tdOrTable);
  18142. };
  18143. UE.commands['inserttable'] = {
  18144. queryCommandState: function () {
  18145. return getTableItemsByRange(this).table ? -1 : 0;
  18146. },
  18147. execCommand: function (cmd, opt) {
  18148. function createTable(opt, tdWidth) {
  18149. var html = [],
  18150. rowsNum = opt.numRows,
  18151. colsNum = opt.numCols;
  18152. for (var r = 0; r < rowsNum; r++) {
  18153. html.push('<tr' + (r == 0 ? ' class="firstRow"' : '') + '>');
  18154. for (var c = 0; c < colsNum; c++) {
  18155. html.push('<td width="' + tdWidth + '" vAlign="' + opt.tdvalign + '" >' + (browser.ie && browser.version < 11 ? domUtils.fillChar : '<br/>') + '</td>')
  18156. }
  18157. html.push('</tr>')
  18158. }
  18159. //禁止指定table-width
  18160. return '<table><tbody>' + html.join('') + '</tbody></table>'
  18161. }
  18162. if (!opt) {
  18163. opt = utils.extend({}, {
  18164. numCols: this.options.defaultCols,
  18165. numRows: this.options.defaultRows,
  18166. tdvalign: this.options.tdvalign
  18167. })
  18168. }
  18169. var me = this;
  18170. var range = this.selection.getRange(),
  18171. start = range.startContainer,
  18172. firstParentBlock = domUtils.findParent(start, function (node) {
  18173. return domUtils.isBlockElm(node);
  18174. }, true) || me.body;
  18175. var defaultValue = getDefaultValue(me),
  18176. tableWidth = firstParentBlock.offsetWidth,
  18177. tdWidth = Math.floor(tableWidth / opt.numCols - defaultValue.tdPadding * 2 - defaultValue.tdBorder);
  18178. //todo其他属性
  18179. !opt.tdvalign && (opt.tdvalign = me.options.tdvalign);
  18180. me.execCommand("inserthtml", createTable(opt, tdWidth));
  18181. }
  18182. };
  18183. UE.commands['insertparagraphbeforetable'] = {
  18184. queryCommandState: function () {
  18185. return getTableItemsByRange(this).cell ? 0 : -1;
  18186. },
  18187. execCommand: function () {
  18188. var table = getTableItemsByRange(this).table;
  18189. if (table) {
  18190. var p = this.document.createElement("p");
  18191. p.innerHTML = browser.ie ? '&nbsp;' : '<br />';
  18192. table.parentNode.insertBefore(p, table);
  18193. this.selection.getRange().setStart(p, 0).setCursor();
  18194. }
  18195. }
  18196. };
  18197. UE.commands['deletetable'] = {
  18198. queryCommandState: function () {
  18199. var rng = this.selection.getRange();
  18200. return domUtils.findParentByTagName(rng.startContainer, 'table', true) ? 0 : -1;
  18201. },
  18202. execCommand: function (cmd, table) {
  18203. var rng = this.selection.getRange();
  18204. table = table || domUtils.findParentByTagName(rng.startContainer, 'table', true);
  18205. if (table) {
  18206. var next = table.nextSibling;
  18207. if (!next) {
  18208. next = domUtils.createElement(this.document, 'p', {
  18209. 'innerHTML': browser.ie ? domUtils.fillChar : '<br/>'
  18210. });
  18211. table.parentNode.insertBefore(next, table);
  18212. }
  18213. domUtils.remove(table);
  18214. rng = this.selection.getRange();
  18215. if (next.nodeType == 3) {
  18216. rng.setStartBefore(next)
  18217. } else {
  18218. rng.setStart(next, 0)
  18219. }
  18220. rng.setCursor(false, true)
  18221. this.fireEvent("tablehasdeleted")
  18222. }
  18223. }
  18224. };
  18225. UE.commands['cellalign'] = {
  18226. queryCommandState: function () {
  18227. return getSelectedArr(this).length ? 0 : -1
  18228. },
  18229. execCommand: function (cmd, align) {
  18230. var selectedTds = getSelectedArr(this);
  18231. if (selectedTds.length) {
  18232. for (var i = 0, ci; ci = selectedTds[i++];) {
  18233. ci.setAttribute('align', align);
  18234. }
  18235. }
  18236. }
  18237. };
  18238. UE.commands['cellvalign'] = {
  18239. queryCommandState: function () {
  18240. return getSelectedArr(this).length ? 0 : -1;
  18241. },
  18242. execCommand: function (cmd, valign) {
  18243. var selectedTds = getSelectedArr(this);
  18244. if (selectedTds.length) {
  18245. for (var i = 0, ci; ci = selectedTds[i++];) {
  18246. ci.setAttribute('vAlign', valign);
  18247. }
  18248. }
  18249. }
  18250. };
  18251. UE.commands['insertcaption'] = {
  18252. queryCommandState: function () {
  18253. var table = getTableItemsByRange(this).table;
  18254. if (table) {
  18255. return table.getElementsByTagName('caption').length == 0 ? 1 : -1;
  18256. }
  18257. return -1;
  18258. },
  18259. execCommand: function () {
  18260. var table = getTableItemsByRange(this).table;
  18261. if (table) {
  18262. var caption = this.document.createElement('caption');
  18263. caption.innerHTML = browser.ie ? domUtils.fillChar : '<br/>';
  18264. table.insertBefore(caption, table.firstChild);
  18265. var range = this.selection.getRange();
  18266. range.setStart(caption, 0).setCursor();
  18267. }
  18268. }
  18269. };
  18270. UE.commands['deletecaption'] = {
  18271. queryCommandState: function () {
  18272. var rng = this.selection.getRange(),
  18273. table = domUtils.findParentByTagName(rng.startContainer, 'table');
  18274. if (table) {
  18275. return table.getElementsByTagName('caption').length == 0 ? -1 : 1;
  18276. }
  18277. return -1;
  18278. },
  18279. execCommand: function () {
  18280. var rng = this.selection.getRange(),
  18281. table = domUtils.findParentByTagName(rng.startContainer, 'table');
  18282. if (table) {
  18283. domUtils.remove(table.getElementsByTagName('caption')[0]);
  18284. var range = this.selection.getRange();
  18285. range.setStart(table.rows[0].cells[0], 0).setCursor();
  18286. }
  18287. }
  18288. };
  18289. UE.commands['inserttitle'] = {
  18290. queryCommandState: function () {
  18291. var table = getTableItemsByRange(this).table;
  18292. if (table) {
  18293. var firstRow = table.rows[0];
  18294. return firstRow.cells[firstRow.cells.length - 1].tagName.toLowerCase() != 'th' ? 0 : -1
  18295. }
  18296. return -1;
  18297. },
  18298. execCommand: function () {
  18299. var table = getTableItemsByRange(this).table;
  18300. if (table) {
  18301. getUETable(table).insertRow(0, 'th');
  18302. }
  18303. var th = table.getElementsByTagName('th')[0];
  18304. this.selection.getRange().setStart(th, 0).setCursor(false, true);
  18305. }
  18306. };
  18307. UE.commands['deletetitle'] = {
  18308. queryCommandState: function () {
  18309. var table = getTableItemsByRange(this).table;
  18310. if (table) {
  18311. var firstRow = table.rows[0];
  18312. return firstRow.cells[firstRow.cells.length - 1].tagName.toLowerCase() == 'th' ? 0 : -1
  18313. }
  18314. return -1;
  18315. },
  18316. execCommand: function () {
  18317. var table = getTableItemsByRange(this).table;
  18318. if (table) {
  18319. domUtils.remove(table.rows[0])
  18320. }
  18321. var td = table.getElementsByTagName('td')[0];
  18322. this.selection.getRange().setStart(td, 0).setCursor(false, true);
  18323. }
  18324. };
  18325. UE.commands['inserttitlecol'] = {
  18326. queryCommandState: function () {
  18327. var table = getTableItemsByRange(this).table;
  18328. if (table) {
  18329. var lastRow = table.rows[table.rows.length - 1];
  18330. return lastRow.getElementsByTagName('th').length ? -1 : 0;
  18331. }
  18332. return -1;
  18333. },
  18334. execCommand: function (cmd) {
  18335. var table = getTableItemsByRange(this).table;
  18336. if (table) {
  18337. getUETable(table).insertCol(0, 'th');
  18338. }
  18339. resetTdWidth(table, this);
  18340. var th = table.getElementsByTagName('th')[0];
  18341. this.selection.getRange().setStart(th, 0).setCursor(false, true);
  18342. }
  18343. };
  18344. UE.commands['deletetitlecol'] = {
  18345. queryCommandState: function () {
  18346. var table = getTableItemsByRange(this).table;
  18347. if (table) {
  18348. var lastRow = table.rows[table.rows.length - 1];
  18349. return lastRow.getElementsByTagName('th').length ? 0 : -1;
  18350. }
  18351. return -1;
  18352. },
  18353. execCommand: function () {
  18354. var table = getTableItemsByRange(this).table;
  18355. if (table) {
  18356. for (var i = 0; i < table.rows.length; i++) {
  18357. domUtils.remove(table.rows[i].children[0])
  18358. }
  18359. }
  18360. resetTdWidth(table, this);
  18361. var td = table.getElementsByTagName('td')[0];
  18362. this.selection.getRange().setStart(td, 0).setCursor(false, true);
  18363. }
  18364. };
  18365. UE.commands["mergeright"] = {
  18366. queryCommandState: function (cmd) {
  18367. var tableItems = getTableItemsByRange(this),
  18368. table = tableItems.table,
  18369. cell = tableItems.cell;
  18370. if (!table || !cell) return -1;
  18371. var ut = getUETable(table);
  18372. if (ut.selectedTds.length) return -1;
  18373. var cellInfo = ut.getCellInfo(cell),
  18374. rightColIndex = cellInfo.colIndex + cellInfo.colSpan;
  18375. if (rightColIndex >= ut.colsNum) return -1; // 如果处于最右边则不能向右合并
  18376. var rightCellInfo = ut.indexTable[cellInfo.rowIndex][rightColIndex],
  18377. rightCell = table.rows[rightCellInfo.rowIndex].cells[rightCellInfo.cellIndex];
  18378. if (!rightCell || cell.tagName != rightCell.tagName) return -1; // TH和TD不能相互合并
  18379. // 当且仅当两个Cell的开始列号和结束列号一致时能进行合并
  18380. return (rightCellInfo.rowIndex == cellInfo.rowIndex && rightCellInfo.rowSpan == cellInfo.rowSpan) ? 0 : -1;
  18381. },
  18382. execCommand: function (cmd) {
  18383. var rng = this.selection.getRange(),
  18384. bk = rng.createBookmark(true);
  18385. var cell = getTableItemsByRange(this).cell,
  18386. ut = getUETable(cell);
  18387. ut.mergeRight(cell);
  18388. rng.moveToBookmark(bk).select();
  18389. }
  18390. };
  18391. UE.commands["mergedown"] = {
  18392. queryCommandState: function (cmd) {
  18393. var tableItems = getTableItemsByRange(this),
  18394. table = tableItems.table,
  18395. cell = tableItems.cell;
  18396. if (!table || !cell) return -1;
  18397. var ut = getUETable(table);
  18398. if (ut.selectedTds.length) return -1;
  18399. var cellInfo = ut.getCellInfo(cell),
  18400. downRowIndex = cellInfo.rowIndex + cellInfo.rowSpan;
  18401. if (downRowIndex >= ut.rowsNum) return -1; // 如果处于最下边则不能向下合并
  18402. var downCellInfo = ut.indexTable[downRowIndex][cellInfo.colIndex],
  18403. downCell = table.rows[downCellInfo.rowIndex].cells[downCellInfo.cellIndex];
  18404. if (!downCell || cell.tagName != downCell.tagName) return -1; // TH和TD不能相互合并
  18405. // 当且仅当两个Cell的开始列号和结束列号一致时能进行合并
  18406. return (downCellInfo.colIndex == cellInfo.colIndex && downCellInfo.colSpan == cellInfo.colSpan) ? 0 : -1;
  18407. },
  18408. execCommand: function () {
  18409. var rng = this.selection.getRange(),
  18410. bk = rng.createBookmark(true);
  18411. var cell = getTableItemsByRange(this).cell,
  18412. ut = getUETable(cell);
  18413. ut.mergeDown(cell);
  18414. rng.moveToBookmark(bk).select();
  18415. }
  18416. };
  18417. UE.commands["mergecells"] = {
  18418. queryCommandState: function () {
  18419. return getUETableBySelected(this) ? 0 : -1;
  18420. },
  18421. execCommand: function () {
  18422. var ut = getUETableBySelected(this);
  18423. if (ut && ut.selectedTds.length) {
  18424. var cell = ut.selectedTds[0];
  18425. ut.mergeRange();
  18426. var rng = this.selection.getRange();
  18427. if (domUtils.isEmptyBlock(cell)) {
  18428. rng.setStart(cell, 0).collapse(true)
  18429. } else {
  18430. rng.selectNodeContents(cell)
  18431. }
  18432. rng.select();
  18433. }
  18434. }
  18435. };
  18436. UE.commands["insertrow"] = {
  18437. queryCommandState: function () {
  18438. var tableItems = getTableItemsByRange(this),
  18439. cell = tableItems.cell;
  18440. return cell && (cell.tagName == "TD" || (cell.tagName == 'TH' && tableItems.tr !== tableItems.table.rows[0])) &&
  18441. getUETable(tableItems.table).rowsNum < this.options.maxRowNum ? 0 : -1;
  18442. },
  18443. execCommand: function () {
  18444. var rng = this.selection.getRange(),
  18445. bk = rng.createBookmark(true);
  18446. var tableItems = getTableItemsByRange(this),
  18447. cell = tableItems.cell,
  18448. table = tableItems.table,
  18449. ut = getUETable(table),
  18450. cellInfo = ut.getCellInfo(cell);
  18451. //ut.insertRow(!ut.selectedTds.length ? cellInfo.rowIndex:ut.cellsRange.beginRowIndex,'');
  18452. if (!ut.selectedTds.length) {
  18453. ut.insertRow(cellInfo.rowIndex, cell);
  18454. } else {
  18455. var range = ut.cellsRange;
  18456. for (var i = 0, len = range.endRowIndex - range.beginRowIndex + 1; i < len; i++) {
  18457. ut.insertRow(range.beginRowIndex, cell);
  18458. }
  18459. }
  18460. rng.moveToBookmark(bk).select();
  18461. if (table.getAttribute("interlaced") === "enabled") this.fireEvent("interlacetable", table);
  18462. }
  18463. };
  18464. //后插入行
  18465. UE.commands["insertrownext"] = {
  18466. queryCommandState: function () {
  18467. var tableItems = getTableItemsByRange(this),
  18468. cell = tableItems.cell;
  18469. return cell && (cell.tagName == "TD") && getUETable(tableItems.table).rowsNum < this.options.maxRowNum ? 0 : -1;
  18470. },
  18471. execCommand: function () {
  18472. var rng = this.selection.getRange(),
  18473. bk = rng.createBookmark(true);
  18474. var tableItems = getTableItemsByRange(this),
  18475. cell = tableItems.cell,
  18476. table = tableItems.table,
  18477. ut = getUETable(table),
  18478. cellInfo = ut.getCellInfo(cell);
  18479. //ut.insertRow(!ut.selectedTds.length? cellInfo.rowIndex + cellInfo.rowSpan : ut.cellsRange.endRowIndex + 1,'');
  18480. if (!ut.selectedTds.length) {
  18481. ut.insertRow(cellInfo.rowIndex + cellInfo.rowSpan, cell);
  18482. } else {
  18483. var range = ut.cellsRange;
  18484. for (var i = 0, len = range.endRowIndex - range.beginRowIndex + 1; i < len; i++) {
  18485. ut.insertRow(range.endRowIndex + 1, cell);
  18486. }
  18487. }
  18488. rng.moveToBookmark(bk).select();
  18489. if (table.getAttribute("interlaced") === "enabled") this.fireEvent("interlacetable", table);
  18490. }
  18491. };
  18492. UE.commands["deleterow"] = {
  18493. queryCommandState: function () {
  18494. var tableItems = getTableItemsByRange(this);
  18495. return tableItems.cell ? 0 : -1;
  18496. },
  18497. execCommand: function () {
  18498. var cell = getTableItemsByRange(this).cell,
  18499. ut = getUETable(cell),
  18500. cellsRange = ut.cellsRange,
  18501. cellInfo = ut.getCellInfo(cell),
  18502. preCell = ut.getVSideCell(cell),
  18503. nextCell = ut.getVSideCell(cell, true),
  18504. rng = this.selection.getRange();
  18505. if (utils.isEmptyObject(cellsRange)) {
  18506. ut.deleteRow(cellInfo.rowIndex);
  18507. } else {
  18508. for (var i = cellsRange.beginRowIndex; i < cellsRange.endRowIndex + 1; i++) {
  18509. ut.deleteRow(cellsRange.beginRowIndex);
  18510. }
  18511. }
  18512. var table = ut.table;
  18513. if (!table.getElementsByTagName('td').length) {
  18514. var nextSibling = table.nextSibling;
  18515. domUtils.remove(table);
  18516. if (nextSibling) {
  18517. rng.setStart(nextSibling, 0).setCursor(false, true);
  18518. }
  18519. } else {
  18520. if (cellInfo.rowSpan == 1 || cellInfo.rowSpan == cellsRange.endRowIndex - cellsRange.beginRowIndex + 1) {
  18521. if (nextCell || preCell) rng.selectNodeContents(nextCell || preCell).setCursor(false, true);
  18522. } else {
  18523. var newCell = ut.getCell(cellInfo.rowIndex, ut.indexTable[cellInfo.rowIndex][cellInfo.colIndex].cellIndex);
  18524. if (newCell) rng.selectNodeContents(newCell).setCursor(false, true);
  18525. }
  18526. }
  18527. if (table.getAttribute("interlaced") === "enabled") this.fireEvent("interlacetable", table);
  18528. }
  18529. };
  18530. UE.commands["insertcol"] = {
  18531. queryCommandState: function (cmd) {
  18532. var tableItems = getTableItemsByRange(this),
  18533. cell = tableItems.cell;
  18534. return cell && (cell.tagName == "TD" || (cell.tagName == 'TH' && cell !== tableItems.tr.cells[0])) &&
  18535. getUETable(tableItems.table).colsNum < this.options.maxColNum ? 0 : -1;
  18536. },
  18537. execCommand: function (cmd) {
  18538. var rng = this.selection.getRange(),
  18539. bk = rng.createBookmark(true);
  18540. if (this.queryCommandState(cmd) == -1) return;
  18541. var cell = getTableItemsByRange(this).cell,
  18542. ut = getUETable(cell),
  18543. cellInfo = ut.getCellInfo(cell);
  18544. //ut.insertCol(!ut.selectedTds.length ? cellInfo.colIndex:ut.cellsRange.beginColIndex);
  18545. if (!ut.selectedTds.length) {
  18546. ut.insertCol(cellInfo.colIndex, cell);
  18547. } else {
  18548. var range = ut.cellsRange;
  18549. for (var i = 0, len = range.endColIndex - range.beginColIndex + 1; i < len; i++) {
  18550. ut.insertCol(range.beginColIndex, cell);
  18551. }
  18552. }
  18553. rng.moveToBookmark(bk).select(true);
  18554. }
  18555. };
  18556. UE.commands["insertcolnext"] = {
  18557. queryCommandState: function () {
  18558. var tableItems = getTableItemsByRange(this),
  18559. cell = tableItems.cell;
  18560. return cell && getUETable(tableItems.table).colsNum < this.options.maxColNum ? 0 : -1;
  18561. },
  18562. execCommand: function () {
  18563. var rng = this.selection.getRange(),
  18564. bk = rng.createBookmark(true);
  18565. var cell = getTableItemsByRange(this).cell,
  18566. ut = getUETable(cell),
  18567. cellInfo = ut.getCellInfo(cell);
  18568. //ut.insertCol(!ut.selectedTds.length ? cellInfo.colIndex + cellInfo.colSpan:ut.cellsRange.endColIndex +1);
  18569. if (!ut.selectedTds.length) {
  18570. ut.insertCol(cellInfo.colIndex + cellInfo.colSpan, cell);
  18571. } else {
  18572. var range = ut.cellsRange;
  18573. for (var i = 0, len = range.endColIndex - range.beginColIndex + 1; i < len; i++) {
  18574. ut.insertCol(range.endColIndex + 1, cell);
  18575. }
  18576. }
  18577. rng.moveToBookmark(bk).select();
  18578. }
  18579. };
  18580. UE.commands["deletecol"] = {
  18581. queryCommandState: function () {
  18582. var tableItems = getTableItemsByRange(this);
  18583. return tableItems.cell ? 0 : -1;
  18584. },
  18585. execCommand: function () {
  18586. var cell = getTableItemsByRange(this).cell,
  18587. ut = getUETable(cell),
  18588. range = ut.cellsRange,
  18589. cellInfo = ut.getCellInfo(cell),
  18590. preCell = ut.getHSideCell(cell),
  18591. nextCell = ut.getHSideCell(cell, true);
  18592. if (utils.isEmptyObject(range)) {
  18593. ut.deleteCol(cellInfo.colIndex);
  18594. } else {
  18595. for (var i = range.beginColIndex; i < range.endColIndex + 1; i++) {
  18596. ut.deleteCol(range.beginColIndex);
  18597. }
  18598. }
  18599. var table = ut.table,
  18600. rng = this.selection.getRange();
  18601. if (!table.getElementsByTagName('td').length) {
  18602. var nextSibling = table.nextSibling;
  18603. domUtils.remove(table);
  18604. if (nextSibling) {
  18605. rng.setStart(nextSibling, 0).setCursor(false, true);
  18606. }
  18607. } else {
  18608. if (domUtils.inDoc(cell, this.document)) {
  18609. rng.setStart(cell, 0).setCursor(false, true);
  18610. } else {
  18611. if (nextCell && domUtils.inDoc(nextCell, this.document)) {
  18612. rng.selectNodeContents(nextCell).setCursor(false, true);
  18613. } else {
  18614. if (preCell && domUtils.inDoc(preCell, this.document)) {
  18615. rng.selectNodeContents(preCell).setCursor(true, true);
  18616. }
  18617. }
  18618. }
  18619. }
  18620. }
  18621. };
  18622. UE.commands["splittocells"] = {
  18623. queryCommandState: function () {
  18624. var tableItems = getTableItemsByRange(this),
  18625. cell = tableItems.cell;
  18626. if (!cell) return -1;
  18627. var ut = getUETable(tableItems.table);
  18628. if (ut.selectedTds.length > 0) return -1;
  18629. return cell && (cell.colSpan > 1 || cell.rowSpan > 1) ? 0 : -1;
  18630. },
  18631. execCommand: function () {
  18632. var rng = this.selection.getRange(),
  18633. bk = rng.createBookmark(true);
  18634. var cell = getTableItemsByRange(this).cell,
  18635. ut = getUETable(cell);
  18636. ut.splitToCells(cell);
  18637. rng.moveToBookmark(bk).select();
  18638. }
  18639. };
  18640. UE.commands["splittorows"] = {
  18641. queryCommandState: function () {
  18642. var tableItems = getTableItemsByRange(this),
  18643. cell = tableItems.cell;
  18644. if (!cell) return -1;
  18645. var ut = getUETable(tableItems.table);
  18646. if (ut.selectedTds.length > 0) return -1;
  18647. return cell && cell.rowSpan > 1 ? 0 : -1;
  18648. },
  18649. execCommand: function () {
  18650. var rng = this.selection.getRange(),
  18651. bk = rng.createBookmark(true);
  18652. var cell = getTableItemsByRange(this).cell,
  18653. ut = getUETable(cell);
  18654. ut.splitToRows(cell);
  18655. rng.moveToBookmark(bk).select();
  18656. }
  18657. };
  18658. UE.commands["splittocols"] = {
  18659. queryCommandState: function () {
  18660. var tableItems = getTableItemsByRange(this),
  18661. cell = tableItems.cell;
  18662. if (!cell) return -1;
  18663. var ut = getUETable(tableItems.table);
  18664. if (ut.selectedTds.length > 0) return -1;
  18665. return cell && cell.colSpan > 1 ? 0 : -1;
  18666. },
  18667. execCommand: function () {
  18668. var rng = this.selection.getRange(),
  18669. bk = rng.createBookmark(true);
  18670. var cell = getTableItemsByRange(this).cell,
  18671. ut = getUETable(cell);
  18672. ut.splitToCols(cell);
  18673. rng.moveToBookmark(bk).select();
  18674. }
  18675. };
  18676. UE.commands["adaptbytext"] =
  18677. UE.commands["adaptbywindow"] = {
  18678. queryCommandState: function () {
  18679. return getTableItemsByRange(this).table ? 0 : -1
  18680. },
  18681. execCommand: function (cmd) {
  18682. var tableItems = getTableItemsByRange(this),
  18683. table = tableItems.table;
  18684. if (table) {
  18685. if (cmd == 'adaptbywindow') {
  18686. resetTdWidth(table, this);
  18687. } else {
  18688. var cells = domUtils.getElementsByTagName(table, "td th");
  18689. utils.each(cells, function (cell) {
  18690. cell.removeAttribute("width");
  18691. });
  18692. table.removeAttribute("width");
  18693. }
  18694. }
  18695. }
  18696. };
  18697. //平均分配各列
  18698. UE.commands['averagedistributecol'] = {
  18699. queryCommandState: function () {
  18700. var ut = getUETableBySelected(this);
  18701. if (!ut) return -1;
  18702. return ut.isFullRow() || ut.isFullCol() ? 0 : -1;
  18703. },
  18704. execCommand: function (cmd) {
  18705. var me = this,
  18706. ut = getUETableBySelected(me);
  18707. function getAverageWidth() {
  18708. var tb = ut.table,
  18709. averageWidth, sumWidth = 0,
  18710. colsNum = 0,
  18711. tbAttr = getDefaultValue(me, tb);
  18712. if (ut.isFullRow()) {
  18713. sumWidth = tb.offsetWidth;
  18714. colsNum = ut.colsNum;
  18715. } else {
  18716. var begin = ut.cellsRange.beginColIndex,
  18717. end = ut.cellsRange.endColIndex,
  18718. node;
  18719. for (var i = begin; i <= end;) {
  18720. node = ut.selectedTds[i];
  18721. sumWidth += node.offsetWidth;
  18722. i += node.colSpan;
  18723. colsNum += 1;
  18724. }
  18725. }
  18726. averageWidth = Math.ceil(sumWidth / colsNum) - tbAttr.tdBorder * 2 - tbAttr.tdPadding * 2;
  18727. return averageWidth;
  18728. }
  18729. function setAverageWidth(averageWidth) {
  18730. utils.each(domUtils.getElementsByTagName(ut.table, "th"), function (node) {
  18731. node.setAttribute("width", "");
  18732. });
  18733. var cells = ut.isFullRow() ? domUtils.getElementsByTagName(ut.table, "td") : ut.selectedTds;
  18734. utils.each(cells, function (node) {
  18735. if (node.colSpan == 1) {
  18736. node.setAttribute("width", averageWidth);
  18737. }
  18738. });
  18739. }
  18740. if (ut && ut.selectedTds.length) {
  18741. setAverageWidth(getAverageWidth());
  18742. }
  18743. }
  18744. };
  18745. //平均分配各行
  18746. UE.commands['averagedistributerow'] = {
  18747. queryCommandState: function () {
  18748. var ut = getUETableBySelected(this);
  18749. if (!ut) return -1;
  18750. if (ut.selectedTds && /th/ig.test(ut.selectedTds[0].tagName)) return -1;
  18751. return ut.isFullRow() || ut.isFullCol() ? 0 : -1;
  18752. },
  18753. execCommand: function (cmd) {
  18754. var me = this,
  18755. ut = getUETableBySelected(me);
  18756. function getAverageHeight() {
  18757. var averageHeight, rowNum, sumHeight = 0,
  18758. tb = ut.table,
  18759. tbAttr = getDefaultValue(me, tb),
  18760. tdpadding = parseInt(domUtils.getComputedStyle(tb.getElementsByTagName('td')[0], "padding-top"));
  18761. if (ut.isFullCol()) {
  18762. var captionArr = domUtils.getElementsByTagName(tb, "caption"),
  18763. thArr = domUtils.getElementsByTagName(tb, "th"),
  18764. captionHeight, thHeight;
  18765. if (captionArr.length > 0) {
  18766. captionHeight = captionArr[0].offsetHeight;
  18767. }
  18768. if (thArr.length > 0) {
  18769. thHeight = thArr[0].offsetHeight;
  18770. }
  18771. sumHeight = tb.offsetHeight - (captionHeight || 0) - (thHeight || 0);
  18772. rowNum = thArr.length == 0 ? ut.rowsNum : (ut.rowsNum - 1);
  18773. } else {
  18774. var begin = ut.cellsRange.beginRowIndex,
  18775. end = ut.cellsRange.endRowIndex,
  18776. count = 0,
  18777. trs = domUtils.getElementsByTagName(tb, "tr");
  18778. for (var i = begin; i <= end; i++) {
  18779. sumHeight += trs[i].offsetHeight;
  18780. count += 1;
  18781. }
  18782. rowNum = count;
  18783. }
  18784. //ie8下是混杂模式
  18785. if (browser.ie && browser.version < 9) {
  18786. averageHeight = Math.ceil(sumHeight / rowNum);
  18787. } else {
  18788. averageHeight = Math.ceil(sumHeight / rowNum) - tbAttr.tdBorder * 2 - tdpadding * 2;
  18789. }
  18790. return averageHeight;
  18791. }
  18792. function setAverageHeight(averageHeight) {
  18793. var cells = ut.isFullCol() ? domUtils.getElementsByTagName(ut.table, "td") : ut.selectedTds;
  18794. utils.each(cells, function (node) {
  18795. if (node.rowSpan == 1) {
  18796. node.setAttribute("height", averageHeight);
  18797. }
  18798. });
  18799. }
  18800. if (ut && ut.selectedTds.length) {
  18801. setAverageHeight(getAverageHeight());
  18802. }
  18803. }
  18804. };
  18805. //单元格对齐方式
  18806. UE.commands['cellalignment'] = {
  18807. queryCommandState: function () {
  18808. return getTableItemsByRange(this).table ? 0 : -1
  18809. },
  18810. execCommand: function (cmd, data) {
  18811. var me = this,
  18812. ut = getUETableBySelected(me);
  18813. if (!ut) {
  18814. var start = me.selection.getStart(),
  18815. cell = start && domUtils.findParentByTagName(start, ["td", "th", "caption"], true);
  18816. if (!/caption/ig.test(cell.tagName)) {
  18817. domUtils.setAttributes(cell, data);
  18818. } else {
  18819. cell.style.textAlign = data.align;
  18820. cell.style.verticalAlign = data.vAlign;
  18821. }
  18822. me.selection.getRange().setCursor(true);
  18823. } else {
  18824. utils.each(ut.selectedTds, function (cell) {
  18825. domUtils.setAttributes(cell, data);
  18826. });
  18827. }
  18828. },
  18829. /**
  18830. * 查询当前点击的单元格的对齐状态, 如果当前已经选择了多个单元格, 则会返回所有单元格经过统一协调过后的状态
  18831. * @see UE.UETable.getTableCellAlignState
  18832. */
  18833. queryCommandValue: function (cmd) {
  18834. var activeMenuCell = getTableItemsByRange(this).cell;
  18835. if (!activeMenuCell) {
  18836. activeMenuCell = getSelectedArr(this)[0];
  18837. }
  18838. if (!activeMenuCell) {
  18839. return null;
  18840. } else {
  18841. //获取同时选中的其他单元格
  18842. var cells = UE.UETable.getUETable(activeMenuCell).selectedTds;
  18843. !cells.length && (cells = activeMenuCell);
  18844. return UE.UETable.getTableCellAlignState(cells);
  18845. }
  18846. }
  18847. };
  18848. //表格对齐方式
  18849. UE.commands['tablealignment'] = {
  18850. queryCommandState: function () {
  18851. if (browser.ie && browser.version < 8) {
  18852. return -1;
  18853. }
  18854. return getTableItemsByRange(this).table ? 0 : -1
  18855. },
  18856. execCommand: function (cmd, value) {
  18857. var me = this,
  18858. start = me.selection.getStart(),
  18859. table = start && domUtils.findParentByTagName(start, ["table"], true);
  18860. if (table) {
  18861. table.setAttribute("align", value);
  18862. }
  18863. }
  18864. };
  18865. //表格属性
  18866. UE.commands['edittable'] = {
  18867. queryCommandState: function () {
  18868. return getTableItemsByRange(this).table ? 0 : -1
  18869. },
  18870. execCommand: function (cmd, color) {
  18871. var rng = this.selection.getRange(),
  18872. table = domUtils.findParentByTagName(rng.startContainer, 'table');
  18873. if (table) {
  18874. var arr = domUtils.getElementsByTagName(table, "td").concat(
  18875. domUtils.getElementsByTagName(table, "th"),
  18876. domUtils.getElementsByTagName(table, "caption")
  18877. );
  18878. utils.each(arr, function (node) {
  18879. node.style.borderColor = color;
  18880. });
  18881. }
  18882. }
  18883. };
  18884. //单元格属性
  18885. UE.commands['edittd'] = {
  18886. queryCommandState: function () {
  18887. return getTableItemsByRange(this).table ? 0 : -1
  18888. },
  18889. execCommand: function (cmd, bkColor) {
  18890. var me = this,
  18891. ut = getUETableBySelected(me);
  18892. if (!ut) {
  18893. var start = me.selection.getStart(),
  18894. cell = start && domUtils.findParentByTagName(start, ["td", "th", "caption"], true);
  18895. if (cell) {
  18896. cell.style.backgroundColor = bkColor;
  18897. }
  18898. } else {
  18899. utils.each(ut.selectedTds, function (cell) {
  18900. cell.style.backgroundColor = bkColor;
  18901. });
  18902. }
  18903. }
  18904. };
  18905. UE.commands["settablebackground"] = {
  18906. queryCommandState: function () {
  18907. return getSelectedArr(this).length > 1 ? 0 : -1;
  18908. },
  18909. execCommand: function (cmd, value) {
  18910. var cells, ut;
  18911. cells = getSelectedArr(this);
  18912. ut = getUETable(cells[0]);
  18913. ut.setBackground(cells, value);
  18914. }
  18915. };
  18916. UE.commands["cleartablebackground"] = {
  18917. queryCommandState: function () {
  18918. var cells = getSelectedArr(this);
  18919. if (!cells.length) return -1;
  18920. for (var i = 0, cell; cell = cells[i++];) {
  18921. if (cell.style.backgroundColor !== "") return 0;
  18922. }
  18923. return -1;
  18924. },
  18925. execCommand: function () {
  18926. var cells = getSelectedArr(this),
  18927. ut = getUETable(cells[0]);
  18928. ut.removeBackground(cells);
  18929. }
  18930. };
  18931. UE.commands["interlacetable"] = UE.commands["uninterlacetable"] = {
  18932. queryCommandState: function (cmd) {
  18933. var table = getTableItemsByRange(this).table;
  18934. if (!table) return -1;
  18935. var interlaced = table.getAttribute("interlaced");
  18936. if (cmd == "interlacetable") {
  18937. //TODO 待定
  18938. //是否需要待定,如果设置,则命令只能单次执行成功,但反射具备toggle效果;否则可以覆盖前次命令,但反射将不存在toggle效果
  18939. return (interlaced === "enabled") ? -1 : 0;
  18940. } else {
  18941. return (!interlaced || interlaced === "disabled") ? -1 : 0;
  18942. }
  18943. },
  18944. execCommand: function (cmd, classList) {
  18945. var table = getTableItemsByRange(this).table;
  18946. if (cmd == "interlacetable") {
  18947. table.setAttribute("interlaced", "enabled");
  18948. this.fireEvent("interlacetable", table, classList);
  18949. } else {
  18950. table.setAttribute("interlaced", "disabled");
  18951. this.fireEvent("uninterlacetable", table);
  18952. }
  18953. }
  18954. };
  18955. UE.commands["setbordervisible"] = {
  18956. queryCommandState: function (cmd) {
  18957. var table = getTableItemsByRange(this).table;
  18958. if (!table) return -1;
  18959. return 0;
  18960. },
  18961. execCommand: function () {
  18962. var table = getTableItemsByRange(this).table;
  18963. utils.each(domUtils.getElementsByTagName(table, 'td'), function (td) {
  18964. td.style.borderWidth = '1px';
  18965. td.style.borderStyle = 'solid';
  18966. })
  18967. }
  18968. };
  18969. function resetTdWidth(table, editor) {
  18970. var tds = domUtils.getElementsByTagName(table, 'td th');
  18971. utils.each(tds, function (td) {
  18972. td.removeAttribute("width");
  18973. });
  18974. table.setAttribute('width', getTableWidth(editor, true, getDefaultValue(editor, table)));
  18975. var tdsWidths = [];
  18976. setTimeout(function () {
  18977. utils.each(tds, function (td) {
  18978. (td.colSpan == 1) && tdsWidths.push(td.offsetWidth)
  18979. })
  18980. utils.each(tds, function (td, i) {
  18981. (td.colSpan == 1) && td.setAttribute("width", tdsWidths[i] + "");
  18982. })
  18983. }, 0);
  18984. }
  18985. function getTableWidth(editor, needIEHack, defaultValue) {
  18986. var body = editor.body;
  18987. return body.offsetWidth - (needIEHack ? parseInt(domUtils.getComputedStyle(body, 'margin-left'), 10) * 2 : 0) - defaultValue.tableBorder * 2 - (editor.options.offsetWidth || 0);
  18988. }
  18989. function getSelectedArr(editor) {
  18990. var cell = getTableItemsByRange(editor).cell;
  18991. if (cell) {
  18992. var ut = getUETable(cell);
  18993. return ut.selectedTds.length ? ut.selectedTds : [cell];
  18994. } else {
  18995. return [];
  18996. }
  18997. }
  18998. })();
  18999. // plugins/table.action.js
  19000. /**
  19001. * Created with JetBrains PhpStorm.
  19002. * User: taoqili
  19003. * Date: 12-10-12
  19004. * Time: 上午10:05
  19005. * To change this template use File | Settings | File Templates.
  19006. */
  19007. UE.plugins['table'] = function () {
  19008. var me = this,
  19009. tabTimer = null,
  19010. //拖动计时器
  19011. tableDragTimer = null,
  19012. //双击计时器
  19013. tableResizeTimer = null,
  19014. //单元格最小宽度
  19015. cellMinWidth = 5,
  19016. isInResizeBuffer = false,
  19017. //单元格边框大小
  19018. cellBorderWidth = 5,
  19019. //鼠标偏移距离
  19020. offsetOfTableCell = 10,
  19021. //记录在有限时间内的点击状态, 共有3个取值, 0, 1, 2。 0代表未初始化, 1代表单击了1次,2代表2次
  19022. singleClickState = 0,
  19023. userActionStatus = null,
  19024. //双击允许的时间范围
  19025. dblclickTime = 360,
  19026. UT = UE.UETable,
  19027. getUETable = function (tdOrTable) {
  19028. return UT.getUETable(tdOrTable);
  19029. },
  19030. getUETableBySelected = function (editor) {
  19031. return UT.getUETableBySelected(editor);
  19032. },
  19033. getDefaultValue = function (editor, table) {
  19034. return UT.getDefaultValue(editor, table);
  19035. },
  19036. removeSelectedClass = function (cells) {
  19037. return UT.removeSelectedClass(cells);
  19038. };
  19039. function showError(e) {
  19040. // throw e;
  19041. }
  19042. me.ready(function () {
  19043. var me = this;
  19044. var orgGetText = me.selection.getText;
  19045. me.selection.getText = function () {
  19046. var table = getUETableBySelected(me);
  19047. if (table) {
  19048. var str = '';
  19049. utils.each(table.selectedTds, function (td) {
  19050. str += td[browser.ie ? 'innerText' : 'textContent'];
  19051. })
  19052. return str;
  19053. } else {
  19054. return orgGetText.call(me.selection)
  19055. }
  19056. }
  19057. })
  19058. //处理拖动及框选相关方法
  19059. var startTd = null, //鼠标按下时的锚点td
  19060. currentTd = null, //当前鼠标经过时的td
  19061. onDrag = "", //指示当前拖动状态,其值可为"","h","v" ,分别表示未拖动状态,横向拖动状态,纵向拖动状态,用于鼠标移动过程中的判断
  19062. onBorder = false, //检测鼠标按下时是否处在单元格边缘位置
  19063. dragButton = null,
  19064. dragOver = false,
  19065. dragLine = null, //模拟的拖动线
  19066. dragTd = null; //发生拖动的目标td
  19067. var mousedown = false,
  19068. //todo 判断混乱模式
  19069. needIEHack = true;
  19070. me.setOpt({
  19071. 'maxColNum': 20,
  19072. 'maxRowNum': 100,
  19073. 'defaultCols': 5,
  19074. 'defaultRows': 5,
  19075. 'tdvalign': 'top',
  19076. 'cursorpath': me.options.UEDITOR_HOME_URL + "themes/default/images/cursor_",
  19077. 'tableDragable': false,
  19078. 'classList': ["ue-table-interlace-color-single", "ue-table-interlace-color-double"]
  19079. });
  19080. me.getUETable = getUETable;
  19081. var commands = {
  19082. 'deletetable': 1,
  19083. 'inserttable': 1,
  19084. 'cellvalign': 1,
  19085. 'insertcaption': 1,
  19086. 'deletecaption': 1,
  19087. 'inserttitle': 1,
  19088. 'deletetitle': 1,
  19089. "mergeright": 1,
  19090. "mergedown": 1,
  19091. "mergecells": 1,
  19092. "insertrow": 1,
  19093. "insertrownext": 1,
  19094. "deleterow": 1,
  19095. "insertcol": 1,
  19096. "insertcolnext": 1,
  19097. "deletecol": 1,
  19098. "splittocells": 1,
  19099. "splittorows": 1,
  19100. "splittocols": 1,
  19101. "adaptbytext": 1,
  19102. "adaptbywindow": 1,
  19103. "adaptbycustomer": 1,
  19104. "insertparagraph": 1,
  19105. "insertparagraphbeforetable": 1,
  19106. "averagedistributecol": 1,
  19107. "averagedistributerow": 1
  19108. };
  19109. me.ready(function () {
  19110. utils.cssRule('table',
  19111. //选中的td上的样式
  19112. '.selectTdClass{background-color:#edf5fa !important}' +
  19113. 'table.noBorderTable td,table.noBorderTable th,table.noBorderTable caption{border:1px dashed #ddd !important}' +
  19114. //插入的表格的默认样式
  19115. 'table{margin-bottom:10px;border-collapse:collapse;display:table;}' +
  19116. 'td,th{padding: 5px 10px;border: 1px solid #DDD;}' +
  19117. 'caption{border:1px dashed #DDD;border-bottom:0;padding:3px;text-align:center;}' +
  19118. 'th{border-top:1px solid #BBB;background-color:#F7F7F7;}' +
  19119. 'table tr.firstRow th{border-top-width:2px;}' +
  19120. '.ue-table-interlace-color-single{ background-color: #fcfcfc; } .ue-table-interlace-color-double{ background-color: #f7faff; }' +
  19121. 'td p{margin:0;padding:0;}', me.document);
  19122. var tableCopyList, isFullCol, isFullRow;
  19123. //注册del/backspace事件
  19124. me.addListener('keydown', function (cmd, evt) {
  19125. var me = this;
  19126. var keyCode = evt.keyCode || evt.which;
  19127. if (keyCode == 8) {
  19128. var ut = getUETableBySelected(me);
  19129. if (ut && ut.selectedTds.length) {
  19130. if (ut.isFullCol()) {
  19131. me.execCommand('deletecol')
  19132. } else if (ut.isFullRow()) {
  19133. me.execCommand('deleterow')
  19134. } else {
  19135. me.fireEvent('delcells');
  19136. }
  19137. domUtils.preventDefault(evt);
  19138. }
  19139. var caption = domUtils.findParentByTagName(me.selection.getStart(), 'caption', true),
  19140. range = me.selection.getRange();
  19141. if (range.collapsed && caption && isEmptyBlock(caption)) {
  19142. me.fireEvent('saveScene');
  19143. var table = caption.parentNode;
  19144. domUtils.remove(caption);
  19145. if (table) {
  19146. range.setStart(table.rows[0].cells[0], 0).setCursor(false, true);
  19147. }
  19148. me.fireEvent('saveScene');
  19149. }
  19150. }
  19151. if (keyCode == 46) {
  19152. ut = getUETableBySelected(me);
  19153. if (ut) {
  19154. me.fireEvent('saveScene');
  19155. for (var i = 0, ci; ci = ut.selectedTds[i++];) {
  19156. domUtils.fillNode(me.document, ci)
  19157. }
  19158. me.fireEvent('saveScene');
  19159. domUtils.preventDefault(evt);
  19160. }
  19161. }
  19162. if (keyCode == 13) {
  19163. var rng = me.selection.getRange(),
  19164. caption = domUtils.findParentByTagName(rng.startContainer, 'caption', true);
  19165. if (caption) {
  19166. var table = domUtils.findParentByTagName(caption, 'table');
  19167. if (!rng.collapsed) {
  19168. rng.deleteContents();
  19169. me.fireEvent('saveScene');
  19170. } else {
  19171. if (caption) {
  19172. rng.setStart(table.rows[0].cells[0], 0).setCursor(false, true);
  19173. }
  19174. }
  19175. domUtils.preventDefault(evt);
  19176. return;
  19177. }
  19178. if (rng.collapsed) {
  19179. var table = domUtils.findParentByTagName(rng.startContainer, 'table');
  19180. if (table) {
  19181. var cell = table.rows[0].cells[0],
  19182. start = domUtils.findParentByTagName(me.selection.getStart(), ['td', 'th'], true),
  19183. preNode = table.previousSibling;
  19184. if (cell === start && (!preNode || preNode.nodeType == 1 && preNode.tagName == 'TABLE') && domUtils.isStartInblock(rng)) {
  19185. var first = domUtils.findParent(me.selection.getStart(), function (n) {
  19186. return domUtils.isBlockElm(n)
  19187. }, true);
  19188. if (first && (/t(h|d)/i.test(first.tagName) || first === start.firstChild)) {
  19189. me.execCommand('insertparagraphbeforetable');
  19190. domUtils.preventDefault(evt);
  19191. }
  19192. }
  19193. }
  19194. }
  19195. }
  19196. if ((evt.ctrlKey || evt.metaKey) && evt.keyCode == '67') {
  19197. tableCopyList = null;
  19198. var ut = getUETableBySelected(me);
  19199. if (ut) {
  19200. var tds = ut.selectedTds;
  19201. isFullCol = ut.isFullCol();
  19202. isFullRow = ut.isFullRow();
  19203. tableCopyList = [
  19204. [ut.cloneCell(tds[0], null, true)]
  19205. ];
  19206. for (var i = 1, ci; ci = tds[i]; i++) {
  19207. if (ci.parentNode !== tds[i - 1].parentNode) {
  19208. tableCopyList.push([ut.cloneCell(ci, null, true)]);
  19209. } else {
  19210. tableCopyList[tableCopyList.length - 1].push(ut.cloneCell(ci, null, true));
  19211. }
  19212. }
  19213. }
  19214. }
  19215. });
  19216. me.addListener("tablehasdeleted", function () {
  19217. toggleDraggableState(this, false, "", null);
  19218. if (dragButton) domUtils.remove(dragButton);
  19219. });
  19220. me.addListener('beforepaste', function (cmd, html) {
  19221. var me = this;
  19222. var rng = me.selection.getRange();
  19223. if (domUtils.findParentByTagName(rng.startContainer, 'caption', true)) {
  19224. var div = me.document.createElement("div");
  19225. div.innerHTML = html.html;
  19226. //trace:3729
  19227. html.html = div[browser.ie9below ? 'innerText' : 'textContent'];
  19228. return;
  19229. }
  19230. var table = getUETableBySelected(me);
  19231. if (tableCopyList) {
  19232. me.fireEvent('saveScene');
  19233. var rng = me.selection.getRange();
  19234. var td = domUtils.findParentByTagName(rng.startContainer, ['td', 'th'], true),
  19235. tmpNode, preNode;
  19236. if (td) {
  19237. var ut = getUETable(td);
  19238. if (isFullRow) {
  19239. var rowIndex = ut.getCellInfo(td).rowIndex;
  19240. if (td.tagName == 'TH') {
  19241. rowIndex++;
  19242. }
  19243. for (var i = 0, ci; ci = tableCopyList[i++];) {
  19244. var tr = ut.insertRow(rowIndex++, "td");
  19245. for (var j = 0, cj; cj = ci[j]; j++) {
  19246. var cell = tr.cells[j];
  19247. if (!cell) {
  19248. cell = tr.insertCell(j)
  19249. }
  19250. cell.innerHTML = cj.innerHTML;
  19251. cj.getAttribute('width') && cell.setAttribute('width', cj.getAttribute('width'));
  19252. cj.getAttribute('vAlign') && cell.setAttribute('vAlign', cj.getAttribute('vAlign'));
  19253. cj.getAttribute('align') && cell.setAttribute('align', cj.getAttribute('align'));
  19254. cj.style.cssText && (cell.style.cssText = cj.style.cssText)
  19255. }
  19256. for (var j = 0, cj; cj = tr.cells[j]; j++) {
  19257. if (!ci[j])
  19258. break;
  19259. cj.innerHTML = ci[j].innerHTML;
  19260. ci[j].getAttribute('width') && cj.setAttribute('width', ci[j].getAttribute('width'));
  19261. ci[j].getAttribute('vAlign') && cj.setAttribute('vAlign', ci[j].getAttribute('vAlign'));
  19262. ci[j].getAttribute('align') && cj.setAttribute('align', ci[j].getAttribute('align'));
  19263. ci[j].style.cssText && (cj.style.cssText = ci[j].style.cssText)
  19264. }
  19265. }
  19266. } else {
  19267. if (isFullCol) {
  19268. cellInfo = ut.getCellInfo(td);
  19269. var maxColNum = 0;
  19270. for (var j = 0, ci = tableCopyList[0], cj; cj = ci[j++];) {
  19271. maxColNum += cj.colSpan || 1;
  19272. }
  19273. me.__hasEnterExecCommand = true;
  19274. for (i = 0; i < maxColNum; i++) {
  19275. me.execCommand('insertcol');
  19276. }
  19277. me.__hasEnterExecCommand = false;
  19278. td = ut.table.rows[0].cells[cellInfo.cellIndex];
  19279. if (td.tagName == 'TH') {
  19280. td = ut.table.rows[1].cells[cellInfo.cellIndex];
  19281. }
  19282. }
  19283. for (var i = 0, ci; ci = tableCopyList[i++];) {
  19284. tmpNode = td;
  19285. for (var j = 0, cj; cj = ci[j++];) {
  19286. if (td) {
  19287. td.innerHTML = cj.innerHTML;
  19288. //todo 定制处理
  19289. cj.getAttribute('width') && td.setAttribute('width', cj.getAttribute('width'));
  19290. cj.getAttribute('vAlign') && td.setAttribute('vAlign', cj.getAttribute('vAlign'));
  19291. cj.getAttribute('align') && td.setAttribute('align', cj.getAttribute('align'));
  19292. cj.style.cssText && (td.style.cssText = cj.style.cssText);
  19293. preNode = td;
  19294. td = td.nextSibling;
  19295. } else {
  19296. var cloneTd = cj.cloneNode(true);
  19297. domUtils.removeAttributes(cloneTd, ['class', 'rowSpan', 'colSpan']);
  19298. preNode.parentNode.appendChild(cloneTd)
  19299. }
  19300. }
  19301. td = ut.getNextCell(tmpNode, true, true);
  19302. if (!tableCopyList[i])
  19303. break;
  19304. if (!td) {
  19305. var cellInfo = ut.getCellInfo(tmpNode);
  19306. ut.table.insertRow(ut.table.rows.length);
  19307. ut.update();
  19308. td = ut.getVSideCell(tmpNode, true);
  19309. }
  19310. }
  19311. }
  19312. ut.update();
  19313. } else {
  19314. table = me.document.createElement('table');
  19315. for (var i = 0, ci; ci = tableCopyList[i++];) {
  19316. var tr = table.insertRow(table.rows.length);
  19317. for (var j = 0, cj; cj = ci[j++];) {
  19318. cloneTd = UT.cloneCell(cj, null, true);
  19319. domUtils.removeAttributes(cloneTd, ['class']);
  19320. tr.appendChild(cloneTd)
  19321. }
  19322. if (j == 2 && cloneTd.rowSpan > 1) {
  19323. cloneTd.rowSpan = 1;
  19324. }
  19325. }
  19326. var defaultValue = getDefaultValue(me),
  19327. width = me.body.offsetWidth -
  19328. (needIEHack ? parseInt(domUtils.getComputedStyle(me.body, 'margin-left'), 10) * 2 : 0) - defaultValue.tableBorder * 2 - (me.options.offsetWidth || 0);
  19329. me.execCommand('insertHTML', '<table ' +
  19330. (isFullCol && isFullRow ? 'width="' + width + '"' : '') +
  19331. '>' + table.innerHTML.replace(/>\s*</g, '><').replace(/\bth\b/gi, "td") + '</table>')
  19332. }
  19333. me.fireEvent('contentchange');
  19334. me.fireEvent('saveScene');
  19335. html.html = '';
  19336. return true;
  19337. } else {
  19338. var div = me.document.createElement("div"),
  19339. tables;
  19340. div.innerHTML = html.html;
  19341. tables = div.getElementsByTagName("table");
  19342. if (domUtils.findParentByTagName(me.selection.getStart(), 'table')) {
  19343. utils.each(tables, function (t) {
  19344. domUtils.remove(t)
  19345. });
  19346. if (domUtils.findParentByTagName(me.selection.getStart(), 'caption', true)) {
  19347. div.innerHTML = div[browser.ie ? 'innerText' : 'textContent'];
  19348. }
  19349. } else {
  19350. utils.each(tables, function (table) {
  19351. removeStyleSize(table, true);
  19352. domUtils.removeAttributes(table, ['style', 'border']);
  19353. utils.each(domUtils.getElementsByTagName(table, "td"), function (td) {
  19354. if (isEmptyBlock(td)) {
  19355. domUtils.fillNode(me.document, td);
  19356. }
  19357. removeStyleSize(td, true);
  19358. // domUtils.removeAttributes(td, ['style'])
  19359. });
  19360. });
  19361. }
  19362. html.html = div.innerHTML;
  19363. }
  19364. });
  19365. me.addListener('afterpaste', function () {
  19366. utils.each(domUtils.getElementsByTagName(me.body, "table"), function (table) {
  19367. if (table.offsetWidth > me.body.offsetWidth) {
  19368. var defaultValue = getDefaultValue(me, table);
  19369. table.style.width = me.body.offsetWidth - (needIEHack ? parseInt(domUtils.getComputedStyle(me.body, 'margin-left'), 10) * 2 : 0) - defaultValue.tableBorder * 2 - (me.options.offsetWidth || 0) + 'px'
  19370. }
  19371. })
  19372. });
  19373. me.addListener('blur', function () {
  19374. tableCopyList = null;
  19375. });
  19376. var timer;
  19377. me.addListener('keydown', function () {
  19378. clearTimeout(timer);
  19379. timer = setTimeout(function () {
  19380. var rng = me.selection.getRange(),
  19381. cell = domUtils.findParentByTagName(rng.startContainer, ['th', 'td'], true);
  19382. if (cell) {
  19383. var table = cell.parentNode.parentNode.parentNode;
  19384. if (table.offsetWidth > table.getAttribute("width")) {
  19385. cell.style.wordBreak = "break-all";
  19386. }
  19387. }
  19388. }, 100);
  19389. });
  19390. me.addListener("selectionchange", function () {
  19391. toggleDraggableState(me, false, "", null);
  19392. });
  19393. //内容变化时触发索引更新
  19394. //todo 可否考虑标记检测,如果不涉及表格的变化就不进行索引重建和更新
  19395. me.addListener("contentchange", function () {
  19396. var me = this;
  19397. //尽可能排除一些不需要更新的状况
  19398. hideDragLine(me);
  19399. if (getUETableBySelected(me)) return;
  19400. var rng = me.selection.getRange();
  19401. var start = rng.startContainer;
  19402. start = domUtils.findParentByTagName(start, ['td', 'th'], true);
  19403. utils.each(domUtils.getElementsByTagName(me.document, 'table'), function (table) {
  19404. if (me.fireEvent("excludetable", table) === true) return;
  19405. table.ueTable = new UT(table);
  19406. //trace:3742
  19407. // utils.each(domUtils.getElementsByTagName(me.document, 'td'), function (td) {
  19408. //
  19409. // if (domUtils.isEmptyBlock(td) && td !== start) {
  19410. // domUtils.fillNode(me.document, td);
  19411. // if (browser.ie && browser.version == 6) {
  19412. // td.innerHTML = '&nbsp;'
  19413. // }
  19414. // }
  19415. // });
  19416. // utils.each(domUtils.getElementsByTagName(me.document, 'th'), function (th) {
  19417. // if (domUtils.isEmptyBlock(th) && th !== start) {
  19418. // domUtils.fillNode(me.document, th);
  19419. // if (browser.ie && browser.version == 6) {
  19420. // th.innerHTML = '&nbsp;'
  19421. // }
  19422. // }
  19423. // });
  19424. table.onmouseover = function () {
  19425. me.fireEvent('tablemouseover', table);
  19426. };
  19427. table.onmousemove = function () {
  19428. me.fireEvent('tablemousemove', table);
  19429. me.options.tableDragable && toggleDragButton(true, this, me);
  19430. utils.defer(function () {
  19431. me.fireEvent('contentchange', 50)
  19432. }, true)
  19433. };
  19434. table.onmouseout = function () {
  19435. me.fireEvent('tablemouseout', table);
  19436. toggleDraggableState(me, false, "", null);
  19437. hideDragLine(me);
  19438. };
  19439. table.onclick = function (evt) {
  19440. evt = me.window.event || evt;
  19441. var target = getParentTdOrTh(evt.target || evt.srcElement);
  19442. if (!target) return;
  19443. var ut = getUETable(target),
  19444. table = ut.table,
  19445. cellInfo = ut.getCellInfo(target),
  19446. cellsRange,
  19447. rng = me.selection.getRange();
  19448. // if ("topLeft" == inPosition(table, mouseCoords(evt))) {
  19449. // cellsRange = ut.getCellsRange(ut.table.rows[0].cells[0], ut.getLastCell());
  19450. // ut.setSelected(cellsRange);
  19451. // return;
  19452. // }
  19453. // if ("bottomRight" == inPosition(table, mouseCoords(evt))) {
  19454. //
  19455. // return;
  19456. // }
  19457. if (inTableSide(table, target, evt, true)) {
  19458. var endTdCol = ut.getCell(ut.indexTable[ut.rowsNum - 1][cellInfo.colIndex].rowIndex, ut.indexTable[ut.rowsNum - 1][cellInfo.colIndex].cellIndex);
  19459. if (evt.shiftKey && ut.selectedTds.length) {
  19460. if (ut.selectedTds[0] !== endTdCol) {
  19461. cellsRange = ut.getCellsRange(ut.selectedTds[0], endTdCol);
  19462. ut.setSelected(cellsRange);
  19463. } else {
  19464. rng && rng.selectNodeContents(endTdCol).select();
  19465. }
  19466. } else {
  19467. if (target !== endTdCol) {
  19468. cellsRange = ut.getCellsRange(target, endTdCol);
  19469. ut.setSelected(cellsRange);
  19470. } else {
  19471. rng && rng.selectNodeContents(endTdCol).select();
  19472. }
  19473. }
  19474. return;
  19475. }
  19476. if (inTableSide(table, target, evt)) {
  19477. var endTdRow = ut.getCell(ut.indexTable[cellInfo.rowIndex][ut.colsNum - 1].rowIndex, ut.indexTable[cellInfo.rowIndex][ut.colsNum - 1].cellIndex);
  19478. if (evt.shiftKey && ut.selectedTds.length) {
  19479. if (ut.selectedTds[0] !== endTdRow) {
  19480. cellsRange = ut.getCellsRange(ut.selectedTds[0], endTdRow);
  19481. ut.setSelected(cellsRange);
  19482. } else {
  19483. rng && rng.selectNodeContents(endTdRow).select();
  19484. }
  19485. } else {
  19486. if (target !== endTdRow) {
  19487. cellsRange = ut.getCellsRange(target, endTdRow);
  19488. ut.setSelected(cellsRange);
  19489. } else {
  19490. rng && rng.selectNodeContents(endTdRow).select();
  19491. }
  19492. }
  19493. }
  19494. };
  19495. });
  19496. switchBorderColor(me, true);
  19497. });
  19498. domUtils.on(me.document, "mousemove", mouseMoveEvent);
  19499. domUtils.on(me.document, "mouseout", function (evt) {
  19500. var target = evt.target || evt.srcElement;
  19501. if (target.tagName == "TABLE") {
  19502. toggleDraggableState(me, false, "", null);
  19503. }
  19504. });
  19505. /**
  19506. * 表格隔行变色
  19507. */
  19508. me.addListener("interlacetable", function (type, table, classList) {
  19509. if (!table) return;
  19510. var me = this,
  19511. rows = table.rows,
  19512. len = rows.length,
  19513. getClass = function (list, index, repeat) {
  19514. return list[index] ? list[index] : repeat ? list[index % list.length] : "";
  19515. };
  19516. for (var i = 0; i < len; i++) {
  19517. rows[i].className = getClass(classList || me.options.classList, i, true);
  19518. }
  19519. });
  19520. me.addListener("uninterlacetable", function (type, table) {
  19521. if (!table) return;
  19522. var me = this,
  19523. rows = table.rows,
  19524. classList = me.options.classList,
  19525. len = rows.length;
  19526. for (var i = 0; i < len; i++) {
  19527. domUtils.removeClasses(rows[i], classList);
  19528. }
  19529. });
  19530. me.addListener("mousedown", mouseDownEvent);
  19531. me.addListener("mouseup", mouseUpEvent);
  19532. //拖动的时候触发mouseup
  19533. domUtils.on(me.body, 'dragstart', function (evt) {
  19534. mouseUpEvent.call(me, 'dragstart', evt);
  19535. });
  19536. me.addOutputRule(function (root) {
  19537. utils.each(root.getNodesByTagName('div'), function (n) {
  19538. if (n.getAttr('id') == 'ue_tableDragLine') {
  19539. n.parentNode.removeChild(n);
  19540. }
  19541. });
  19542. });
  19543. var currentRowIndex = 0;
  19544. me.addListener("mousedown", function () {
  19545. currentRowIndex = 0;
  19546. });
  19547. me.addListener('tabkeydown', function () {
  19548. var range = this.selection.getRange(),
  19549. common = range.getCommonAncestor(true, true),
  19550. table = domUtils.findParentByTagName(common, 'table');
  19551. if (table) {
  19552. if (domUtils.findParentByTagName(common, 'caption', true)) {
  19553. var cell = domUtils.getElementsByTagName(table, 'th td');
  19554. if (cell && cell.length) {
  19555. range.setStart(cell[0], 0).setCursor(false, true)
  19556. }
  19557. } else {
  19558. var cell = domUtils.findParentByTagName(common, ['td', 'th'], true),
  19559. ua = getUETable(cell);
  19560. currentRowIndex = cell.rowSpan > 1 ? currentRowIndex : ua.getCellInfo(cell).rowIndex;
  19561. var nextCell = ua.getTabNextCell(cell, currentRowIndex);
  19562. if (nextCell) {
  19563. if (isEmptyBlock(nextCell)) {
  19564. range.setStart(nextCell, 0).setCursor(false, true)
  19565. } else {
  19566. range.selectNodeContents(nextCell).select()
  19567. }
  19568. } else {
  19569. me.fireEvent('saveScene');
  19570. me.__hasEnterExecCommand = true;
  19571. this.execCommand('insertrownext');
  19572. me.__hasEnterExecCommand = false;
  19573. range = this.selection.getRange();
  19574. range.setStart(table.rows[table.rows.length - 1].cells[0], 0).setCursor();
  19575. me.fireEvent('saveScene');
  19576. }
  19577. }
  19578. return true;
  19579. }
  19580. });
  19581. browser.ie && me.addListener('selectionchange', function () {
  19582. toggleDraggableState(this, false, "", null);
  19583. });
  19584. me.addListener("keydown", function (type, evt) {
  19585. var me = this;
  19586. //处理在表格的最后一个输入tab产生新的表格
  19587. var keyCode = evt.keyCode || evt.which;
  19588. if (keyCode == 8 || keyCode == 46) {
  19589. return;
  19590. }
  19591. var notCtrlKey = !evt.ctrlKey && !evt.metaKey && !evt.shiftKey && !evt.altKey;
  19592. notCtrlKey && removeSelectedClass(domUtils.getElementsByTagName(me.body, "td"));
  19593. var ut = getUETableBySelected(me);
  19594. if (!ut) return;
  19595. notCtrlKey && ut.clearSelected();
  19596. });
  19597. me.addListener("beforegetcontent", function () {
  19598. switchBorderColor(this, false);
  19599. browser.ie && utils.each(this.document.getElementsByTagName('caption'), function (ci) {
  19600. if (domUtils.isEmptyNode(ci)) {
  19601. ci.innerHTML = '&nbsp;'
  19602. }
  19603. });
  19604. });
  19605. me.addListener("aftergetcontent", function () {
  19606. switchBorderColor(this, true);
  19607. });
  19608. me.addListener("getAllHtml", function () {
  19609. removeSelectedClass(me.document.getElementsByTagName("td"));
  19610. });
  19611. //修正全屏状态下插入的表格宽度在非全屏状态下撑开编辑器的情况
  19612. me.addListener("fullscreenchanged", function (type, fullscreen) {
  19613. if (!fullscreen) {
  19614. var ratio = this.body.offsetWidth / document.body.offsetWidth,
  19615. tables = domUtils.getElementsByTagName(this.body, "table");
  19616. utils.each(tables, function (table) {
  19617. if (table.offsetWidth < me.body.offsetWidth) return false;
  19618. var tds = domUtils.getElementsByTagName(table, "td"),
  19619. backWidths = [];
  19620. utils.each(tds, function (td) {
  19621. backWidths.push(td.offsetWidth);
  19622. });
  19623. for (var i = 0, td; td = tds[i]; i++) {
  19624. td.setAttribute("width", Math.floor(backWidths[i] * ratio));
  19625. }
  19626. table.setAttribute("width", Math.floor(getTableWidth(me, needIEHack, getDefaultValue(me))))
  19627. });
  19628. }
  19629. });
  19630. //重写execCommand命令,用于处理框选时的处理
  19631. var oldExecCommand = me.execCommand;
  19632. me.execCommand = function (cmd, datatat) {
  19633. var me = this,
  19634. args = arguments;
  19635. cmd = cmd.toLowerCase();
  19636. var ut = getUETableBySelected(me),
  19637. tds,
  19638. range = new dom.Range(me.document),
  19639. cmdFun = me.commands[cmd] || UE.commands[cmd],
  19640. result;
  19641. if (!cmdFun) return;
  19642. if (ut && !commands[cmd] && !cmdFun.notNeedUndo && !me.__hasEnterExecCommand) {
  19643. me.__hasEnterExecCommand = true;
  19644. me.fireEvent("beforeexeccommand", cmd);
  19645. tds = ut.selectedTds;
  19646. var lastState = -2,
  19647. lastValue = -2,
  19648. value, state;
  19649. for (var i = 0, td; td = tds[i]; i++) {
  19650. if (isEmptyBlock(td)) {
  19651. range.setStart(td, 0).setCursor(false, true)
  19652. } else {
  19653. range.selectNode(td).select(true);
  19654. }
  19655. state = me.queryCommandState(cmd);
  19656. value = me.queryCommandValue(cmd);
  19657. if (state != -1) {
  19658. if (lastState !== state || lastValue !== value) {
  19659. me._ignoreContentChange = true;
  19660. result = oldExecCommand.apply(me, arguments);
  19661. me._ignoreContentChange = false;
  19662. }
  19663. lastState = me.queryCommandState(cmd);
  19664. lastValue = me.queryCommandValue(cmd);
  19665. if (domUtils.isEmptyBlock(td)) {
  19666. domUtils.fillNode(me.document, td)
  19667. }
  19668. }
  19669. }
  19670. range.setStart(tds[0], 0).shrinkBoundary(true).setCursor(false, true);
  19671. me.fireEvent('contentchange');
  19672. me.fireEvent("afterexeccommand", cmd);
  19673. me.__hasEnterExecCommand = false;
  19674. me._selectionChange();
  19675. } else {
  19676. result = oldExecCommand.apply(me, arguments);
  19677. }
  19678. return result;
  19679. };
  19680. });
  19681. /**
  19682. * 删除obj的宽高style,改成属性宽高
  19683. * @param obj
  19684. * @param replaceToProperty
  19685. */
  19686. function removeStyleSize(obj, replaceToProperty) {
  19687. removeStyle(obj, "width", true);
  19688. removeStyle(obj, "height", true);
  19689. }
  19690. function removeStyle(obj, styleName, replaceToProperty) {
  19691. if (obj.style[styleName]) {
  19692. replaceToProperty && obj.setAttribute(styleName, parseInt(obj.style[styleName], 10));
  19693. obj.style[styleName] = "";
  19694. }
  19695. }
  19696. function getParentTdOrTh(ele) {
  19697. if (ele.tagName == "TD" || ele.tagName == "TH") return ele;
  19698. var td;
  19699. if (td = domUtils.findParentByTagName(ele, "td", true) || domUtils.findParentByTagName(ele, "th", true)) return td;
  19700. return null;
  19701. }
  19702. function isEmptyBlock(node) {
  19703. var reg = new RegExp(domUtils.fillChar, 'g');
  19704. if (node[browser.ie ? 'innerText' : 'textContent'].replace(/^\s*$/, '').replace(reg, '').length > 0) {
  19705. return 0;
  19706. }
  19707. for (var n in dtd.$isNotEmpty) {
  19708. if (node.getElementsByTagName(n).length) {
  19709. return 0;
  19710. }
  19711. }
  19712. return 1;
  19713. }
  19714. function mouseCoords(evt) {
  19715. if (evt.pageX || evt.pageY) {
  19716. return {
  19717. x: evt.pageX,
  19718. y: evt.pageY
  19719. };
  19720. }
  19721. return {
  19722. x: evt.clientX + me.document.body.scrollLeft - me.document.body.clientLeft,
  19723. y: evt.clientY + me.document.body.scrollTop - me.document.body.clientTop
  19724. };
  19725. }
  19726. function mouseMoveEvent(evt) {
  19727. if (isEditorDisabled()) {
  19728. return;
  19729. }
  19730. try {
  19731. //普通状态下鼠标移动
  19732. var target = getParentTdOrTh(evt.target || evt.srcElement),
  19733. pos;
  19734. //区分用户的行为是拖动还是双击
  19735. if (isInResizeBuffer) {
  19736. me.body.style.webkitUserSelect = 'none';
  19737. if (Math.abs(userActionStatus.x - evt.clientX) > offsetOfTableCell || Math.abs(userActionStatus.y - evt.clientY) > offsetOfTableCell) {
  19738. clearTableDragTimer();
  19739. isInResizeBuffer = false;
  19740. singleClickState = 0;
  19741. //drag action
  19742. tableBorderDrag(evt);
  19743. }
  19744. }
  19745. //修改单元格大小时的鼠标移动
  19746. if (onDrag && dragTd) {
  19747. singleClickState = 0;
  19748. me.body.style.webkitUserSelect = 'none';
  19749. me.selection.getNative()[browser.ie9below ? 'empty' : 'removeAllRanges']();
  19750. pos = mouseCoords(evt);
  19751. toggleDraggableState(me, true, onDrag, pos, target);
  19752. if (onDrag == "h") {
  19753. dragLine.style.left = getPermissionX(dragTd, evt) + "px";
  19754. } else if (onDrag == "v") {
  19755. dragLine.style.top = getPermissionY(dragTd, evt) + "px";
  19756. }
  19757. return;
  19758. }
  19759. //当鼠标处于table上时,修改移动过程中的光标状态
  19760. if (target) {
  19761. //针对使用table作为容器的组件不触发拖拽效果
  19762. if (me.fireEvent('excludetable', target) === true)
  19763. return;
  19764. pos = mouseCoords(evt);
  19765. var state = getRelation(target, pos),
  19766. table = domUtils.findParentByTagName(target, "table", true);
  19767. if (inTableSide(table, target, evt, true)) {
  19768. if (me.fireEvent("excludetable", table) === true) return;
  19769. me.body.style.cursor = "url(" + me.options.cursorpath + "h.png),pointer";
  19770. } else if (inTableSide(table, target, evt)) {
  19771. if (me.fireEvent("excludetable", table) === true) return;
  19772. me.body.style.cursor = "url(" + me.options.cursorpath + "v.png),pointer";
  19773. } else {
  19774. me.body.style.cursor = "text";
  19775. var curCell = target;
  19776. if (/\d/.test(state)) {
  19777. state = state.replace(/\d/, '');
  19778. target = getUETable(target).getPreviewCell(target, state == "v");
  19779. }
  19780. //位于第一行的顶部或者第一列的左边时不可拖动
  19781. toggleDraggableState(me, target ? !!state : false, target ? state : '', pos, target);
  19782. }
  19783. } else {
  19784. toggleDragButton(false, table, me);
  19785. }
  19786. } catch (e) {
  19787. showError(e);
  19788. }
  19789. }
  19790. var dragButtonTimer;
  19791. function toggleDragButton(show, table, editor) {
  19792. if (!show) {
  19793. if (dragOver) return;
  19794. dragButtonTimer = setTimeout(function () {
  19795. !dragOver && dragButton && dragButton.parentNode && dragButton.parentNode.removeChild(dragButton);
  19796. }, 2000);
  19797. } else {
  19798. createDragButton(table, editor);
  19799. }
  19800. }
  19801. function createDragButton(table, editor) {
  19802. var pos = domUtils.getXY(table),
  19803. doc = table.ownerDocument;
  19804. if (dragButton && dragButton.parentNode) return dragButton;
  19805. dragButton = doc.createElement("div");
  19806. dragButton.contentEditable = false;
  19807. dragButton.innerHTML = "";
  19808. dragButton.style.cssText = "width:15px;height:15px;background-image:url(" + editor.options.UEDITOR_HOME_URL + "dialogs/table/dragicon.png);position: absolute;cursor:move;top:" + (pos.y - 15) + "px;left:" + (pos.x) + "px;";
  19809. domUtils.unSelectable(dragButton);
  19810. dragButton.onmouseover = function (evt) {
  19811. dragOver = true;
  19812. };
  19813. dragButton.onmouseout = function (evt) {
  19814. dragOver = false;
  19815. };
  19816. domUtils.on(dragButton, 'click', function (type, evt) {
  19817. doClick(evt, this);
  19818. });
  19819. domUtils.on(dragButton, 'dblclick', function (type, evt) {
  19820. doDblClick(evt);
  19821. });
  19822. domUtils.on(dragButton, 'dragstart', function (type, evt) {
  19823. domUtils.preventDefault(evt);
  19824. });
  19825. var timer;
  19826. function doClick(evt, button) {
  19827. // 部分浏览器下需要清理
  19828. clearTimeout(timer);
  19829. timer = setTimeout(function () {
  19830. editor.fireEvent("tableClicked", table, button);
  19831. }, 300);
  19832. }
  19833. function doDblClick(evt) {
  19834. clearTimeout(timer);
  19835. var ut = getUETable(table),
  19836. start = table.rows[0].cells[0],
  19837. end = ut.getLastCell(),
  19838. range = ut.getCellsRange(start, end);
  19839. editor.selection.getRange().setStart(start, 0).setCursor(false, true);
  19840. ut.setSelected(range);
  19841. }
  19842. doc.body.appendChild(dragButton);
  19843. }
  19844. // function inPosition(table, pos) {
  19845. // var tablePos = domUtils.getXY(table),
  19846. // width = table.offsetWidth,
  19847. // height = table.offsetHeight;
  19848. // if (pos.x - tablePos.x < 5 && pos.y - tablePos.y < 5) {
  19849. // return "topLeft";
  19850. // } else if (tablePos.x + width - pos.x < 5 && tablePos.y + height - pos.y < 5) {
  19851. // return "bottomRight";
  19852. // }
  19853. // }
  19854. function inTableSide(table, cell, evt, top) {
  19855. var pos = mouseCoords(evt),
  19856. state = getRelation(cell, pos);
  19857. if (top) {
  19858. var caption = table.getElementsByTagName("caption")[0],
  19859. capHeight = caption ? caption.offsetHeight : 0;
  19860. return (state == "v1") && ((pos.y - domUtils.getXY(table).y - capHeight) < 8);
  19861. } else {
  19862. return (state == "h1") && ((pos.x - domUtils.getXY(table).x) < 8);
  19863. }
  19864. }
  19865. /**
  19866. * 获取拖动时允许的X轴坐标
  19867. * @param dragTd
  19868. * @param evt
  19869. */
  19870. function getPermissionX(dragTd, evt) {
  19871. var ut = getUETable(dragTd);
  19872. if (ut) {
  19873. var preTd = ut.getSameEndPosCells(dragTd, "x")[0],
  19874. nextTd = ut.getSameStartPosXCells(dragTd)[0],
  19875. mouseX = mouseCoords(evt).x,
  19876. left = (preTd ? domUtils.getXY(preTd).x : domUtils.getXY(ut.table).x) + 20,
  19877. right = nextTd ? domUtils.getXY(nextTd).x + nextTd.offsetWidth - 20 : (me.body.offsetWidth + 5 || parseInt(domUtils.getComputedStyle(me.body, "width"), 10));
  19878. left += cellMinWidth;
  19879. right -= cellMinWidth;
  19880. return mouseX < left ? left : mouseX > right ? right : mouseX;
  19881. }
  19882. }
  19883. /**
  19884. * 获取拖动时允许的Y轴坐标
  19885. */
  19886. function getPermissionY(dragTd, evt) {
  19887. try {
  19888. var top = domUtils.getXY(dragTd).y,
  19889. mousePosY = mouseCoords(evt).y;
  19890. return mousePosY < top ? top : mousePosY;
  19891. } catch (e) {
  19892. showError(e);
  19893. }
  19894. }
  19895. /**
  19896. * 移动状态切换
  19897. */
  19898. function toggleDraggableState(editor, draggable, dir, mousePos, cell) {
  19899. try {
  19900. editor.body.style.cursor = dir == "h" ? "col-resize" : dir == "v" ? "row-resize" : "text";
  19901. if (browser.ie) {
  19902. if (dir && !mousedown && !getUETableBySelected(editor)) {
  19903. getDragLine(editor, editor.document);
  19904. showDragLineAt(dir, cell);
  19905. } else {
  19906. hideDragLine(editor)
  19907. }
  19908. }
  19909. onBorder = draggable;
  19910. } catch (e) {
  19911. showError(e);
  19912. }
  19913. }
  19914. /**
  19915. * 获取与UETable相关的resize line
  19916. * @param uetable UETable对象
  19917. */
  19918. function getResizeLineByUETable() {
  19919. var lineId = '_UETableResizeLine',
  19920. line = this.document.getElementById(lineId);
  19921. if (!line) {
  19922. line = this.document.createElement("div");
  19923. line.id = lineId;
  19924. line.contnetEditable = false;
  19925. line.setAttribute("unselectable", "on");
  19926. var styles = {
  19927. width: 2 * cellBorderWidth + 1 + 'px',
  19928. position: 'absolute',
  19929. 'z-index': 100000,
  19930. cursor: 'col-resize',
  19931. background: 'red',
  19932. display: 'none'
  19933. };
  19934. //切换状态
  19935. line.onmouseout = function () {
  19936. this.style.display = 'none';
  19937. };
  19938. utils.extend(line.style, styles);
  19939. this.document.body.appendChild(line);
  19940. }
  19941. return line;
  19942. }
  19943. /**
  19944. * 更新resize-line
  19945. */
  19946. function updateResizeLine(cell, uetable) {
  19947. var line = getResizeLineByUETable.call(this),
  19948. table = uetable.table,
  19949. styles = {
  19950. top: domUtils.getXY(table).y + 'px',
  19951. left: domUtils.getXY(cell).x + cell.offsetWidth - cellBorderWidth + 'px',
  19952. display: 'block',
  19953. height: table.offsetHeight + 'px'
  19954. };
  19955. utils.extend(line.style, styles);
  19956. }
  19957. /**
  19958. * 显示resize-line
  19959. */
  19960. function showResizeLine(cell) {
  19961. var uetable = getUETable(cell);
  19962. updateResizeLine.call(this, cell, uetable);
  19963. }
  19964. /**
  19965. * 获取鼠标与当前单元格的相对位置
  19966. * @param ele
  19967. * @param mousePos
  19968. */
  19969. function getRelation(ele, mousePos) {
  19970. var elePos = domUtils.getXY(ele);
  19971. if (!elePos) {
  19972. return '';
  19973. }
  19974. if (elePos.x + ele.offsetWidth - mousePos.x < cellBorderWidth) {
  19975. return "h";
  19976. }
  19977. if (mousePos.x - elePos.x < cellBorderWidth) {
  19978. return 'h1'
  19979. }
  19980. if (elePos.y + ele.offsetHeight - mousePos.y < cellBorderWidth) {
  19981. return "v";
  19982. }
  19983. if (mousePos.y - elePos.y < cellBorderWidth) {
  19984. return 'v1'
  19985. }
  19986. return '';
  19987. }
  19988. function mouseDownEvent(type, evt) {
  19989. if (isEditorDisabled()) {
  19990. return;
  19991. }
  19992. userActionStatus = {
  19993. x: evt.clientX,
  19994. y: evt.clientY
  19995. };
  19996. //右键菜单单独处理
  19997. if (evt.button == 2) {
  19998. var ut = getUETableBySelected(me),
  19999. flag = false;
  20000. if (ut) {
  20001. var td = getTargetTd(me, evt);
  20002. utils.each(ut.selectedTds, function (ti) {
  20003. if (ti === td) {
  20004. flag = true;
  20005. }
  20006. });
  20007. if (!flag) {
  20008. removeSelectedClass(domUtils.getElementsByTagName(me.body, "th td"));
  20009. ut.clearSelected()
  20010. } else {
  20011. td = ut.selectedTds[0];
  20012. setTimeout(function () {
  20013. me.selection.getRange().setStart(td, 0).setCursor(false, true);
  20014. }, 0);
  20015. }
  20016. }
  20017. } else {
  20018. tableClickHander(evt);
  20019. }
  20020. }
  20021. //清除表格的计时器
  20022. function clearTableTimer() {
  20023. tabTimer && clearTimeout(tabTimer);
  20024. tabTimer = null;
  20025. }
  20026. //双击收缩
  20027. function tableDbclickHandler(evt) {
  20028. singleClickState = 0;
  20029. evt = evt || me.window.event;
  20030. var target = getParentTdOrTh(evt.target || evt.srcElement);
  20031. if (target) {
  20032. var h;
  20033. if (h = getRelation(target, mouseCoords(evt))) {
  20034. hideDragLine(me);
  20035. if (h == 'h1') {
  20036. h = 'h';
  20037. if (inTableSide(domUtils.findParentByTagName(target, "table"), target, evt)) {
  20038. me.execCommand('adaptbywindow');
  20039. } else {
  20040. target = getUETable(target).getPreviewCell(target);
  20041. if (target) {
  20042. var rng = me.selection.getRange();
  20043. rng.selectNodeContents(target).setCursor(true, true)
  20044. }
  20045. }
  20046. }
  20047. if (h == 'h') {
  20048. var ut = getUETable(target),
  20049. table = ut.table,
  20050. cells = getCellsByMoveBorder(target, table, true);
  20051. cells = extractArray(cells, 'left');
  20052. ut.width = ut.offsetWidth;
  20053. var oldWidth = [],
  20054. newWidth = [];
  20055. utils.each(cells, function (cell) {
  20056. oldWidth.push(cell.offsetWidth);
  20057. });
  20058. utils.each(cells, function (cell) {
  20059. cell.removeAttribute("width");
  20060. });
  20061. window.setTimeout(function () {
  20062. //是否允许改变
  20063. var changeable = true;
  20064. utils.each(cells, function (cell, index) {
  20065. var width = cell.offsetWidth;
  20066. if (width > oldWidth[index]) {
  20067. changeable = false;
  20068. return false;
  20069. }
  20070. newWidth.push(width);
  20071. });
  20072. var change = changeable ? newWidth : oldWidth;
  20073. utils.each(cells, function (cell, index) {
  20074. cell.width = change[index] - getTabcellSpace();
  20075. });
  20076. }, 0);
  20077. // minWidth -= cellMinWidth;
  20078. //
  20079. // table.removeAttribute("width");
  20080. // utils.each(cells, function (cell) {
  20081. // cell.style.width = "";
  20082. // cell.width -= minWidth;
  20083. // });
  20084. }
  20085. }
  20086. }
  20087. }
  20088. function tableClickHander(evt) {
  20089. removeSelectedClass(domUtils.getElementsByTagName(me.body, "td th"));
  20090. //trace:3113
  20091. //选中单元格,点击table外部,不会清掉table上挂的ueTable,会引起getUETableBySelected方法返回值
  20092. utils.each(me.document.getElementsByTagName('table'), function (t) {
  20093. t.ueTable = null;
  20094. });
  20095. startTd = getTargetTd(me, evt);
  20096. if (!startTd) return;
  20097. var table = domUtils.findParentByTagName(startTd, "table", true);
  20098. ut = getUETable(table);
  20099. ut && ut.clearSelected();
  20100. //判断当前鼠标状态
  20101. if (!onBorder) {
  20102. me.document.body.style.webkitUserSelect = '';
  20103. mousedown = true;
  20104. me.addListener('mouseover', mouseOverEvent);
  20105. } else {
  20106. //边框上的动作处理
  20107. borderActionHandler(evt);
  20108. }
  20109. }
  20110. //处理表格边框上的动作, 这里做延时处理,避免两种动作互相影响
  20111. function borderActionHandler(evt) {
  20112. if (browser.ie) {
  20113. evt = reconstruct(evt);
  20114. }
  20115. clearTableDragTimer();
  20116. //是否正在等待resize的缓冲中
  20117. isInResizeBuffer = true;
  20118. tableDragTimer = setTimeout(function () {
  20119. tableBorderDrag(evt);
  20120. }, dblclickTime);
  20121. }
  20122. function extractArray(originArr, key) {
  20123. var result = [],
  20124. tmp = null;
  20125. for (var i = 0, len = originArr.length; i < len; i++) {
  20126. tmp = originArr[i][key];
  20127. if (tmp) {
  20128. result.push(tmp);
  20129. }
  20130. }
  20131. return result;
  20132. }
  20133. function clearTableDragTimer() {
  20134. tableDragTimer && clearTimeout(tableDragTimer);
  20135. tableDragTimer = null;
  20136. }
  20137. function reconstruct(obj) {
  20138. var attrs = ['pageX', 'pageY', 'clientX', 'clientY', 'srcElement', 'target'],
  20139. newObj = {};
  20140. if (obj) {
  20141. for (var i = 0, key, val; key = attrs[i]; i++) {
  20142. val = obj[key];
  20143. val && (newObj[key] = val);
  20144. }
  20145. }
  20146. return newObj;
  20147. }
  20148. //边框拖动
  20149. function tableBorderDrag(evt) {
  20150. isInResizeBuffer = false;
  20151. startTd = evt.target || evt.srcElement;
  20152. if (!startTd) return;
  20153. var state = getRelation(startTd, mouseCoords(evt));
  20154. if (/\d/.test(state)) {
  20155. state = state.replace(/\d/, '');
  20156. startTd = getUETable(startTd).getPreviewCell(startTd, state == 'v');
  20157. }
  20158. hideDragLine(me);
  20159. getDragLine(me, me.document);
  20160. me.fireEvent('saveScene');
  20161. showDragLineAt(state, startTd);
  20162. mousedown = true;
  20163. //拖动开始
  20164. onDrag = state;
  20165. dragTd = startTd;
  20166. }
  20167. function mouseUpEvent(type, evt) {
  20168. if (isEditorDisabled()) {
  20169. return;
  20170. }
  20171. clearTableDragTimer();
  20172. isInResizeBuffer = false;
  20173. if (onBorder) {
  20174. singleClickState = ++singleClickState % 3;
  20175. userActionStatus = {
  20176. x: evt.clientX,
  20177. y: evt.clientY
  20178. };
  20179. tableResizeTimer = setTimeout(function () {
  20180. singleClickState > 0 && singleClickState--;
  20181. }, dblclickTime);
  20182. if (singleClickState === 2) {
  20183. singleClickState = 0;
  20184. tableDbclickHandler(evt);
  20185. return;
  20186. }
  20187. }
  20188. if (evt.button == 2) return;
  20189. var me = this;
  20190. //清除表格上原生跨选问题
  20191. var range = me.selection.getRange(),
  20192. start = domUtils.findParentByTagName(range.startContainer, 'table', true),
  20193. end = domUtils.findParentByTagName(range.endContainer, 'table', true);
  20194. if (start || end) {
  20195. if (start === end) {
  20196. start = domUtils.findParentByTagName(range.startContainer, ['td', 'th', 'caption'], true);
  20197. end = domUtils.findParentByTagName(range.endContainer, ['td', 'th', 'caption'], true);
  20198. if (start !== end) {
  20199. me.selection.clearRange()
  20200. }
  20201. } else {
  20202. me.selection.clearRange()
  20203. }
  20204. }
  20205. mousedown = false;
  20206. me.document.body.style.webkitUserSelect = '';
  20207. //拖拽状态下的mouseUP
  20208. if (onDrag && dragTd) {
  20209. me.selection.getNative()[browser.ie9below ? 'empty' : 'removeAllRanges']();
  20210. singleClickState = 0;
  20211. dragLine = me.document.getElementById('ue_tableDragLine');
  20212. // trace 3973
  20213. if (dragLine) {
  20214. var dragTdPos = domUtils.getXY(dragTd),
  20215. dragLinePos = domUtils.getXY(dragLine);
  20216. switch (onDrag) {
  20217. case "h":
  20218. changeColWidth(dragTd, dragLinePos.x - dragTdPos.x);
  20219. break;
  20220. case "v":
  20221. changeRowHeight(dragTd, dragLinePos.y - dragTdPos.y - dragTd.offsetHeight);
  20222. break;
  20223. default:
  20224. }
  20225. onDrag = "";
  20226. dragTd = null;
  20227. hideDragLine(me);
  20228. me.fireEvent('saveScene');
  20229. return;
  20230. }
  20231. }
  20232. //正常状态下的mouseup
  20233. if (!startTd) {
  20234. var target = domUtils.findParentByTagName(evt.target || evt.srcElement, "td", true);
  20235. if (!target) target = domUtils.findParentByTagName(evt.target || evt.srcElement, "th", true);
  20236. if (target && (target.tagName == "TD" || target.tagName == "TH")) {
  20237. if (me.fireEvent("excludetable", target) === true) return;
  20238. range = new dom.Range(me.document);
  20239. range.setStart(target, 0).setCursor(false, true);
  20240. }
  20241. } else {
  20242. var ut = getUETable(startTd),
  20243. cell = ut ? ut.selectedTds[0] : null;
  20244. if (cell) {
  20245. range = new dom.Range(me.document);
  20246. if (domUtils.isEmptyBlock(cell)) {
  20247. range.setStart(cell, 0).setCursor(false, true);
  20248. } else {
  20249. range.selectNodeContents(cell).shrinkBoundary().setCursor(false, true);
  20250. }
  20251. } else {
  20252. range = me.selection.getRange().shrinkBoundary();
  20253. if (!range.collapsed) {
  20254. var start = domUtils.findParentByTagName(range.startContainer, ['td', 'th'], true),
  20255. end = domUtils.findParentByTagName(range.endContainer, ['td', 'th'], true);
  20256. //在table里边的不能清除
  20257. if (start && !end || !start && end || start && end && start !== end) {
  20258. range.setCursor(false, true);
  20259. }
  20260. }
  20261. }
  20262. startTd = null;
  20263. me.removeListener('mouseover', mouseOverEvent);
  20264. }
  20265. me._selectionChange(250, evt);
  20266. }
  20267. function mouseOverEvent(type, evt) {
  20268. if (isEditorDisabled()) {
  20269. return;
  20270. }
  20271. var me = this,
  20272. tar = evt.target || evt.srcElement;
  20273. currentTd = domUtils.findParentByTagName(tar, "td", true) || domUtils.findParentByTagName(tar, "th", true);
  20274. //需要判断两个TD是否位于同一个表格内
  20275. if (startTd && currentTd &&
  20276. ((startTd.tagName == "TD" && currentTd.tagName == "TD") || (startTd.tagName == "TH" && currentTd.tagName == "TH")) &&
  20277. domUtils.findParentByTagName(startTd, 'table') == domUtils.findParentByTagName(currentTd, 'table')) {
  20278. var ut = getUETable(currentTd);
  20279. if (startTd != currentTd) {
  20280. me.document.body.style.webkitUserSelect = 'none';
  20281. me.selection.getNative()[browser.ie9below ? 'empty' : 'removeAllRanges']();
  20282. var range = ut.getCellsRange(startTd, currentTd);
  20283. ut.setSelected(range);
  20284. } else {
  20285. me.document.body.style.webkitUserSelect = '';
  20286. ut.clearSelected();
  20287. }
  20288. }
  20289. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
  20290. }
  20291. function setCellHeight(cell, height, backHeight) {
  20292. var lineHight = parseInt(domUtils.getComputedStyle(cell, "line-height"), 10),
  20293. tmpHeight = backHeight + height;
  20294. height = tmpHeight < lineHight ? lineHight : tmpHeight;
  20295. if (cell.style.height) cell.style.height = "";
  20296. cell.rowSpan == 1 ? cell.setAttribute("height", height) : (cell.removeAttribute && cell.removeAttribute("height"));
  20297. }
  20298. function getWidth(cell) {
  20299. if (!cell) return 0;
  20300. return parseInt(domUtils.getComputedStyle(cell, "width"), 10);
  20301. }
  20302. function changeColWidth(cell, changeValue) {
  20303. var ut = getUETable(cell);
  20304. if (ut) {
  20305. //根据当前移动的边框获取相关的单元格
  20306. var table = ut.table,
  20307. cells = getCellsByMoveBorder(cell, table);
  20308. table.style.width = "";
  20309. table.removeAttribute("width");
  20310. //修正改变量
  20311. changeValue = correctChangeValue(changeValue, cell, cells);
  20312. if (cell.nextSibling) {
  20313. var i = 0;
  20314. utils.each(cells, function (cellGroup) {
  20315. cellGroup.left.width = (+cellGroup.left.width) + changeValue;
  20316. cellGroup.right && (cellGroup.right.width = (+cellGroup.right.width) - changeValue);
  20317. });
  20318. } else {
  20319. utils.each(cells, function (cellGroup) {
  20320. cellGroup.left.width -= -changeValue;
  20321. });
  20322. }
  20323. }
  20324. }
  20325. function isEditorDisabled() {
  20326. return me.body.contentEditable === "false";
  20327. }
  20328. function changeRowHeight(td, changeValue) {
  20329. if (Math.abs(changeValue) < 10) return;
  20330. var ut = getUETable(td);
  20331. if (ut) {
  20332. var cells = ut.getSameEndPosCells(td, "y"),
  20333. //备份需要连带变化的td的原始高度,否则后期无法获取正确的值
  20334. backHeight = cells[0] ? cells[0].offsetHeight : 0;
  20335. for (var i = 0, cell; cell = cells[i++];) {
  20336. setCellHeight(cell, changeValue, backHeight);
  20337. }
  20338. }
  20339. }
  20340. /**
  20341. * 获取调整单元格大小的相关单元格
  20342. * @isContainMergeCell 返回的结果中是否包含发生合并后的单元格
  20343. */
  20344. function getCellsByMoveBorder(cell, table, isContainMergeCell) {
  20345. if (!table) {
  20346. table = domUtils.findParentByTagName(cell, 'table');
  20347. }
  20348. if (!table) {
  20349. return null;
  20350. }
  20351. //获取到该单元格所在行的序列号
  20352. var index = domUtils.getNodeIndex(cell),
  20353. temp = cell,
  20354. rows = table.rows,
  20355. colIndex = 0;
  20356. while (temp) {
  20357. //获取到当前单元格在未发生单元格合并时的序列
  20358. if (temp.nodeType === 1) {
  20359. colIndex += (temp.colSpan || 1);
  20360. }
  20361. temp = temp.previousSibling;
  20362. }
  20363. temp = null;
  20364. //记录想关的单元格
  20365. var borderCells = [];
  20366. utils.each(rows, function (tabRow) {
  20367. var cells = tabRow.cells,
  20368. currIndex = 0;
  20369. utils.each(cells, function (tabCell) {
  20370. currIndex += (tabCell.colSpan || 1);
  20371. if (currIndex === colIndex) {
  20372. borderCells.push({
  20373. left: tabCell,
  20374. right: tabCell.nextSibling || null
  20375. });
  20376. return false;
  20377. } else if (currIndex > colIndex) {
  20378. if (isContainMergeCell) {
  20379. borderCells.push({
  20380. left: tabCell
  20381. });
  20382. }
  20383. return false;
  20384. }
  20385. });
  20386. });
  20387. return borderCells;
  20388. }
  20389. /**
  20390. * 通过给定的单元格集合获取最小的单元格width
  20391. */
  20392. function getMinWidthByTableCells(cells) {
  20393. var minWidth = Number.MAX_VALUE;
  20394. for (var i = 0, curCell; curCell = cells[i]; i++) {
  20395. minWidth = Math.min(minWidth, curCell.width || getTableCellWidth(curCell));
  20396. }
  20397. return minWidth;
  20398. }
  20399. function correctChangeValue(changeValue, relatedCell, cells) {
  20400. //为单元格的paading预留空间
  20401. changeValue -= getTabcellSpace();
  20402. if (changeValue < 0) {
  20403. return 0;
  20404. }
  20405. changeValue -= getTableCellWidth(relatedCell);
  20406. //确定方向
  20407. var direction = changeValue < 0 ? 'left' : 'right';
  20408. changeValue = Math.abs(changeValue);
  20409. //只关心非最后一个单元格就可以
  20410. utils.each(cells, function (cellGroup) {
  20411. var curCell = cellGroup[direction];
  20412. //为单元格保留最小空间
  20413. if (curCell) {
  20414. changeValue = Math.min(changeValue, getTableCellWidth(curCell) - cellMinWidth);
  20415. }
  20416. });
  20417. //修正越界
  20418. changeValue = changeValue < 0 ? 0 : changeValue;
  20419. return direction === 'left' ? -changeValue : changeValue;
  20420. }
  20421. function getTableCellWidth(cell) {
  20422. var width = 0,
  20423. //偏移纠正量
  20424. offset = 0,
  20425. width = cell.offsetWidth - getTabcellSpace();
  20426. //最后一个节点纠正一下
  20427. if (!cell.nextSibling) {
  20428. width -= getTableCellOffset(cell);
  20429. }
  20430. width = width < 0 ? 0 : width;
  20431. try {
  20432. cell.width = width;
  20433. } catch (e) {}
  20434. return width;
  20435. }
  20436. /**
  20437. * 获取单元格所在表格的最末单元格的偏移量
  20438. */
  20439. function getTableCellOffset(cell) {
  20440. tab = domUtils.findParentByTagName(cell, "table", false);
  20441. if (tab.offsetVal === undefined) {
  20442. var prev = cell.previousSibling;
  20443. if (prev) {
  20444. //最后一个单元格和前一个单元格的width diff结果 如果恰好为一个border width, 则条件成立
  20445. tab.offsetVal = cell.offsetWidth - prev.offsetWidth === UT.borderWidth ? UT.borderWidth : 0;
  20446. } else {
  20447. tab.offsetVal = 0;
  20448. }
  20449. }
  20450. return tab.offsetVal;
  20451. }
  20452. function getTabcellSpace() {
  20453. if (UT.tabcellSpace === undefined) {
  20454. var cell = null,
  20455. tab = me.document.createElement("table"),
  20456. tbody = me.document.createElement("tbody"),
  20457. trow = me.document.createElement("tr"),
  20458. tabcell = me.document.createElement("td"),
  20459. mirror = null;
  20460. tabcell.style.cssText = 'border: 0;';
  20461. tabcell.width = 1;
  20462. trow.appendChild(tabcell);
  20463. trow.appendChild(mirror = tabcell.cloneNode(false));
  20464. tbody.appendChild(trow);
  20465. tab.appendChild(tbody);
  20466. tab.style.cssText = "visibility: hidden;";
  20467. me.body.appendChild(tab);
  20468. UT.paddingSpace = tabcell.offsetWidth - 1;
  20469. var tmpTabWidth = tab.offsetWidth;
  20470. tabcell.style.cssText = '';
  20471. mirror.style.cssText = '';
  20472. UT.borderWidth = (tab.offsetWidth - tmpTabWidth) / 3;
  20473. UT.tabcellSpace = UT.paddingSpace + UT.borderWidth;
  20474. me.body.removeChild(tab);
  20475. }
  20476. getTabcellSpace = function () {
  20477. return UT.tabcellSpace;
  20478. };
  20479. return UT.tabcellSpace;
  20480. }
  20481. function getDragLine(editor, doc) {
  20482. if (mousedown) return;
  20483. dragLine = editor.document.createElement("div");
  20484. domUtils.setAttributes(dragLine, {
  20485. id: "ue_tableDragLine",
  20486. unselectable: 'on',
  20487. contenteditable: false,
  20488. 'onresizestart': 'return false',
  20489. 'ondragstart': 'return false',
  20490. 'onselectstart': 'return false',
  20491. style: "background-color:blue;position:absolute;padding:0;margin:0;background-image:none;border:0px none;opacity:0;filter:alpha(opacity=0)"
  20492. });
  20493. editor.body.appendChild(dragLine);
  20494. }
  20495. function hideDragLine(editor) {
  20496. if (mousedown) return;
  20497. var line;
  20498. while (line = editor.document.getElementById('ue_tableDragLine')) {
  20499. domUtils.remove(line)
  20500. }
  20501. }
  20502. /**
  20503. * 依据state(v|h)在cell位置显示横线
  20504. * @param state
  20505. * @param cell
  20506. */
  20507. function showDragLineAt(state, cell) {
  20508. if (!cell) return;
  20509. var table = domUtils.findParentByTagName(cell, "table"),
  20510. caption = table.getElementsByTagName('caption'),
  20511. width = table.offsetWidth,
  20512. height = table.offsetHeight - (caption.length > 0 ? caption[0].offsetHeight : 0),
  20513. tablePos = domUtils.getXY(table),
  20514. cellPos = domUtils.getXY(cell),
  20515. css;
  20516. switch (state) {
  20517. case "h":
  20518. css = 'height:' + height + 'px;top:' + (tablePos.y + (caption.length > 0 ? caption[0].offsetHeight : 0)) + 'px;left:' + (cellPos.x + cell.offsetWidth);
  20519. dragLine.style.cssText = css + 'px;position: absolute;display:block;background-color:blue;width:1px;border:0; color:blue;opacity:.3;filter:alpha(opacity=30)';
  20520. break;
  20521. case "v":
  20522. css = 'width:' + width + 'px;left:' + tablePos.x + 'px;top:' + (cellPos.y + cell.offsetHeight);
  20523. //必须加上border:0和color:blue,否则低版ie不支持背景色显示
  20524. dragLine.style.cssText = css + 'px;overflow:hidden;position: absolute;display:block;background-color:blue;height:1px;border:0;color:blue;opacity:.2;filter:alpha(opacity=20)';
  20525. break;
  20526. default:
  20527. }
  20528. }
  20529. /**
  20530. * 当表格边框颜色为白色时设置为虚线,true为添加虚线
  20531. * @param editor
  20532. * @param flag
  20533. */
  20534. function switchBorderColor(editor, flag) {
  20535. var tableArr = domUtils.getElementsByTagName(editor.body, "table"),
  20536. color;
  20537. for (var i = 0, node; node = tableArr[i++];) {
  20538. var td = domUtils.getElementsByTagName(node, "td");
  20539. if (td[0]) {
  20540. if (flag) {
  20541. color = (td[0].style.borderColor).replace(/\s/g, "");
  20542. if (/(#ffffff)|(rgb\(255,255,255\))/ig.test(color))
  20543. domUtils.addClass(node, "noBorderTable")
  20544. } else {
  20545. domUtils.removeClasses(node, "noBorderTable")
  20546. }
  20547. }
  20548. }
  20549. }
  20550. function getTableWidth(editor, needIEHack, defaultValue) {
  20551. var body = editor.body;
  20552. return body.offsetWidth - (needIEHack ? parseInt(domUtils.getComputedStyle(body, 'margin-left'), 10) * 2 : 0) - defaultValue.tableBorder * 2 - (editor.options.offsetWidth || 0);
  20553. }
  20554. /**
  20555. * 获取当前拖动的单元格
  20556. */
  20557. function getTargetTd(editor, evt) {
  20558. var target = domUtils.findParentByTagName(evt.target || evt.srcElement, ["td", "th"], true),
  20559. dir = null;
  20560. if (!target) {
  20561. return null;
  20562. }
  20563. dir = getRelation(target, mouseCoords(evt));
  20564. //如果有前一个节点, 需要做一个修正, 否则可能会得到一个错误的td
  20565. if (!target) {
  20566. return null;
  20567. }
  20568. if (dir === 'h1' && target.previousSibling) {
  20569. var position = domUtils.getXY(target),
  20570. cellWidth = target.offsetWidth;
  20571. if (Math.abs(position.x + cellWidth - evt.clientX) > cellWidth / 3) {
  20572. target = target.previousSibling;
  20573. }
  20574. } else if (dir === 'v1' && target.parentNode.previousSibling) {
  20575. var position = domUtils.getXY(target),
  20576. cellHeight = target.offsetHeight;
  20577. if (Math.abs(position.y + cellHeight - evt.clientY) > cellHeight / 3) {
  20578. target = target.parentNode.previousSibling.firstChild;
  20579. }
  20580. }
  20581. //排除了非td内部以及用于代码高亮部分的td
  20582. return target && !(editor.fireEvent("excludetable", target) === true) ? target : null;
  20583. }
  20584. };
  20585. // plugins/table.sort.js
  20586. /**
  20587. * Created with JetBrains PhpStorm.
  20588. * User: Jinqn
  20589. * Date: 13-10-12
  20590. * Time: 上午10:20
  20591. * To change this template use File | Settings | File Templates.
  20592. */
  20593. UE.UETable.prototype.sortTable = function (sortByCellIndex, compareFn) {
  20594. var table = this.table,
  20595. rows = table.rows,
  20596. trArray = [],
  20597. flag = rows[0].cells[0].tagName === "TH",
  20598. lastRowIndex = 0;
  20599. if (this.selectedTds.length) {
  20600. var range = this.cellsRange,
  20601. len = range.endRowIndex + 1;
  20602. for (var i = range.beginRowIndex; i < len; i++) {
  20603. trArray[i] = rows[i];
  20604. }
  20605. trArray.splice(0, range.beginRowIndex);
  20606. lastRowIndex = (range.endRowIndex + 1) === this.rowsNum ? 0 : range.endRowIndex + 1;
  20607. } else {
  20608. for (var i = 0, len = rows.length; i < len; i++) {
  20609. trArray[i] = rows[i];
  20610. }
  20611. }
  20612. var Fn = {
  20613. 'reversecurrent': function (td1, td2) {
  20614. return 1;
  20615. },
  20616. 'orderbyasc': function (td1, td2) {
  20617. var value1 = td1.innerText || td1.textContent,
  20618. value2 = td2.innerText || td2.textContent;
  20619. return value1.localeCompare(value2);
  20620. },
  20621. 'reversebyasc': function (td1, td2) {
  20622. var value1 = td1.innerHTML,
  20623. value2 = td2.innerHTML;
  20624. return value2.localeCompare(value1);
  20625. },
  20626. 'orderbynum': function (td1, td2) {
  20627. var value1 = td1[browser.ie ? 'innerText' : 'textContent'].match(/\d+/),
  20628. value2 = td2[browser.ie ? 'innerText' : 'textContent'].match(/\d+/);
  20629. if (value1) value1 = +value1[0];
  20630. if (value2) value2 = +value2[0];
  20631. return (value1 || 0) - (value2 || 0);
  20632. },
  20633. 'reversebynum': function (td1, td2) {
  20634. var value1 = td1[browser.ie ? 'innerText' : 'textContent'].match(/\d+/),
  20635. value2 = td2[browser.ie ? 'innerText' : 'textContent'].match(/\d+/);
  20636. if (value1) value1 = +value1[0];
  20637. if (value2) value2 = +value2[0];
  20638. return (value2 || 0) - (value1 || 0);
  20639. }
  20640. };
  20641. //对表格设置排序的标记data-sort-type
  20642. table.setAttribute('data-sort-type', compareFn && typeof compareFn === "string" && Fn[compareFn] ? compareFn : '');
  20643. //th不参与排序
  20644. flag && trArray.splice(0, 1);
  20645. trArray = utils.sort(trArray, function (tr1, tr2) {
  20646. var result;
  20647. if (compareFn && typeof compareFn === "function") {
  20648. result = compareFn.call(this, tr1.cells[sortByCellIndex], tr2.cells[sortByCellIndex]);
  20649. } else if (compareFn && typeof compareFn === "number") {
  20650. result = 1;
  20651. } else if (compareFn && typeof compareFn === "string" && Fn[compareFn]) {
  20652. result = Fn[compareFn].call(this, tr1.cells[sortByCellIndex], tr2.cells[sortByCellIndex]);
  20653. } else {
  20654. result = Fn['orderbyasc'].call(this, tr1.cells[sortByCellIndex], tr2.cells[sortByCellIndex]);
  20655. }
  20656. return result;
  20657. });
  20658. var fragment = table.ownerDocument.createDocumentFragment();
  20659. for (var j = 0, len = trArray.length; j < len; j++) {
  20660. fragment.appendChild(trArray[j]);
  20661. }
  20662. var tbody = table.getElementsByTagName("tbody")[0];
  20663. if (!lastRowIndex) {
  20664. tbody.appendChild(fragment);
  20665. } else {
  20666. tbody.insertBefore(fragment, rows[lastRowIndex - range.endRowIndex + range.beginRowIndex - 1])
  20667. }
  20668. };
  20669. UE.plugins['tablesort'] = function () {
  20670. var me = this,
  20671. UT = UE.UETable,
  20672. getUETable = function (tdOrTable) {
  20673. return UT.getUETable(tdOrTable);
  20674. },
  20675. getTableItemsByRange = function (editor) {
  20676. return UT.getTableItemsByRange(editor);
  20677. };
  20678. me.ready(function () {
  20679. //添加表格可排序的样式
  20680. utils.cssRule('tablesort',
  20681. 'table.sortEnabled tr.firstRow th,table.sortEnabled tr.firstRow td{padding-right:20px;background-repeat: no-repeat;background-position: center right;' +
  20682. ' background-image:url(' + me.options.themePath + me.options.theme + '/images/sortable.png);}',
  20683. me.document);
  20684. //做单元格合并操作时,清除可排序标识
  20685. me.addListener("afterexeccommand", function (type, cmd) {
  20686. if (cmd == 'mergeright' || cmd == 'mergedown' || cmd == 'mergecells') {
  20687. this.execCommand('disablesort');
  20688. }
  20689. });
  20690. });
  20691. //表格排序
  20692. UE.commands['sorttable'] = {
  20693. queryCommandState: function () {
  20694. var me = this,
  20695. tableItems = getTableItemsByRange(me);
  20696. if (!tableItems.cell) return -1;
  20697. var table = tableItems.table,
  20698. cells = table.getElementsByTagName("td");
  20699. for (var i = 0, cell; cell = cells[i++];) {
  20700. if (cell.rowSpan != 1 || cell.colSpan != 1) return -1;
  20701. }
  20702. return 0;
  20703. },
  20704. execCommand: function (cmd, fn) {
  20705. var me = this,
  20706. range = me.selection.getRange(),
  20707. bk = range.createBookmark(true),
  20708. tableItems = getTableItemsByRange(me),
  20709. cell = tableItems.cell,
  20710. ut = getUETable(tableItems.table),
  20711. cellInfo = ut.getCellInfo(cell);
  20712. ut.sortTable(cellInfo.cellIndex, fn);
  20713. range.moveToBookmark(bk);
  20714. try {
  20715. range.select();
  20716. } catch (e) {}
  20717. }
  20718. };
  20719. //设置表格可排序,清除表格可排序
  20720. UE.commands["enablesort"] = UE.commands["disablesort"] = {
  20721. queryCommandState: function (cmd) {
  20722. var table = getTableItemsByRange(this).table;
  20723. if (table && cmd == 'enablesort') {
  20724. var cells = domUtils.getElementsByTagName(table, 'th td');
  20725. for (var i = 0; i < cells.length; i++) {
  20726. if (cells[i].getAttribute('colspan') > 1 || cells[i].getAttribute('rowspan') > 1) return -1;
  20727. }
  20728. }
  20729. return !table ? -1 : cmd == 'enablesort' ^ table.getAttribute('data-sort') != 'sortEnabled' ? -1 : 0;
  20730. },
  20731. execCommand: function (cmd) {
  20732. var table = getTableItemsByRange(this).table;
  20733. table.setAttribute("data-sort", cmd == "enablesort" ? "sortEnabled" : "sortDisabled");
  20734. cmd == "enablesort" ? domUtils.addClass(table, "sortEnabled") : domUtils.removeClasses(table, "sortEnabled");
  20735. }
  20736. };
  20737. };
  20738. // plugins/contextmenu.js
  20739. ///import core
  20740. ///commands 右键菜单
  20741. ///commandsName ContextMenu
  20742. ///commandsTitle 右键菜单
  20743. /**
  20744. * 右键菜单
  20745. * @function
  20746. * @name baidu.editor.plugins.contextmenu
  20747. * @author zhanyi
  20748. */
  20749. UE.plugins['contextmenu'] = function () {
  20750. var me = this;
  20751. me.setOpt('enableContextMenu', true);
  20752. if (me.getOpt('enableContextMenu') === false) {
  20753. return;
  20754. }
  20755. var lang = me.getLang("contextMenu"),
  20756. menu,
  20757. items = me.options.contextMenu || [{
  20758. label: lang['selectall'],
  20759. cmdName: 'selectall'
  20760. },
  20761. {
  20762. label: lang.cleardoc,
  20763. cmdName: 'cleardoc',
  20764. exec: function () {
  20765. if (confirm(lang.confirmclear)) {
  20766. this.execCommand('cleardoc');
  20767. }
  20768. }
  20769. },
  20770. '-',
  20771. {
  20772. label: lang.unlink,
  20773. cmdName: 'unlink'
  20774. },
  20775. '-',
  20776. {
  20777. group: lang.paragraph,
  20778. icon: 'justifyjustify',
  20779. subMenu: [{
  20780. label: lang.justifyleft,
  20781. cmdName: 'justify',
  20782. value: 'left'
  20783. },
  20784. {
  20785. label: lang.justifyright,
  20786. cmdName: 'justify',
  20787. value: 'right'
  20788. },
  20789. {
  20790. label: lang.justifycenter,
  20791. cmdName: 'justify',
  20792. value: 'center'
  20793. },
  20794. {
  20795. label: lang.justifyjustify,
  20796. cmdName: 'justify',
  20797. value: 'justify'
  20798. }
  20799. ]
  20800. },
  20801. '-',
  20802. {
  20803. group: lang.table,
  20804. icon: 'table',
  20805. subMenu: [{
  20806. label: lang.inserttable,
  20807. cmdName: 'inserttable'
  20808. },
  20809. {
  20810. label: lang.deletetable,
  20811. cmdName: 'deletetable'
  20812. },
  20813. '-',
  20814. {
  20815. label: lang.deleterow,
  20816. cmdName: 'deleterow'
  20817. },
  20818. {
  20819. label: lang.deletecol,
  20820. cmdName: 'deletecol'
  20821. },
  20822. {
  20823. label: lang.insertcol,
  20824. cmdName: 'insertcol'
  20825. },
  20826. {
  20827. label: lang.insertcolnext,
  20828. cmdName: 'insertcolnext'
  20829. },
  20830. {
  20831. label: lang.insertrow,
  20832. cmdName: 'insertrow'
  20833. },
  20834. {
  20835. label: lang.insertrownext,
  20836. cmdName: 'insertrownext'
  20837. },
  20838. '-',
  20839. {
  20840. label: lang.insertcaption,
  20841. cmdName: 'insertcaption'
  20842. },
  20843. {
  20844. label: lang.deletecaption,
  20845. cmdName: 'deletecaption'
  20846. },
  20847. {
  20848. label: lang.inserttitle,
  20849. cmdName: 'inserttitle'
  20850. },
  20851. {
  20852. label: lang.deletetitle,
  20853. cmdName: 'deletetitle'
  20854. },
  20855. {
  20856. label: lang.inserttitlecol,
  20857. cmdName: 'inserttitlecol'
  20858. },
  20859. {
  20860. label: lang.deletetitlecol,
  20861. cmdName: 'deletetitlecol'
  20862. },
  20863. '-',
  20864. {
  20865. label: lang.mergecells,
  20866. cmdName: 'mergecells'
  20867. },
  20868. {
  20869. label: lang.mergeright,
  20870. cmdName: 'mergeright'
  20871. },
  20872. {
  20873. label: lang.mergedown,
  20874. cmdName: 'mergedown'
  20875. },
  20876. '-',
  20877. {
  20878. label: lang.splittorows,
  20879. cmdName: 'splittorows'
  20880. },
  20881. {
  20882. label: lang.splittocols,
  20883. cmdName: 'splittocols'
  20884. },
  20885. {
  20886. label: lang.splittocells,
  20887. cmdName: 'splittocells'
  20888. },
  20889. '-',
  20890. {
  20891. label: lang.averageDiseRow,
  20892. cmdName: 'averagedistributerow'
  20893. },
  20894. {
  20895. label: lang.averageDisCol,
  20896. cmdName: 'averagedistributecol'
  20897. },
  20898. '-',
  20899. {
  20900. label: lang.edittd,
  20901. cmdName: 'edittd',
  20902. exec: function () {
  20903. if (UE.ui['edittd']) {
  20904. new UE.ui['edittd'](this);
  20905. }
  20906. this.getDialog('edittd').open();
  20907. }
  20908. },
  20909. {
  20910. label: lang.edittable,
  20911. cmdName: 'edittable',
  20912. exec: function () {
  20913. if (UE.ui['edittable']) {
  20914. new UE.ui['edittable'](this);
  20915. }
  20916. this.getDialog('edittable').open();
  20917. }
  20918. },
  20919. {
  20920. label: lang.setbordervisible,
  20921. cmdName: 'setbordervisible'
  20922. }
  20923. ]
  20924. },
  20925. {
  20926. group: lang.tablesort,
  20927. icon: 'tablesort',
  20928. subMenu: [{
  20929. label: lang.enablesort,
  20930. cmdName: 'enablesort'
  20931. },
  20932. {
  20933. label: lang.disablesort,
  20934. cmdName: 'disablesort'
  20935. },
  20936. '-',
  20937. {
  20938. label: lang.reversecurrent,
  20939. cmdName: 'sorttable',
  20940. value: 'reversecurrent'
  20941. },
  20942. {
  20943. label: lang.orderbyasc,
  20944. cmdName: 'sorttable',
  20945. value: 'orderbyasc'
  20946. },
  20947. {
  20948. label: lang.reversebyasc,
  20949. cmdName: 'sorttable',
  20950. value: 'reversebyasc'
  20951. },
  20952. {
  20953. label: lang.orderbynum,
  20954. cmdName: 'sorttable',
  20955. value: 'orderbynum'
  20956. },
  20957. {
  20958. label: lang.reversebynum,
  20959. cmdName: 'sorttable',
  20960. value: 'reversebynum'
  20961. }
  20962. ]
  20963. },
  20964. {
  20965. group: lang.borderbk,
  20966. icon: 'borderBack',
  20967. subMenu: [{
  20968. label: lang.setcolor,
  20969. cmdName: "interlacetable",
  20970. exec: function () {
  20971. this.execCommand("interlacetable");
  20972. }
  20973. },
  20974. {
  20975. label: lang.unsetcolor,
  20976. cmdName: "uninterlacetable",
  20977. exec: function () {
  20978. this.execCommand("uninterlacetable");
  20979. }
  20980. },
  20981. {
  20982. label: lang.setbackground,
  20983. cmdName: "settablebackground",
  20984. exec: function () {
  20985. this.execCommand("settablebackground", {
  20986. repeat: true,
  20987. colorList: ["#bbb", "#ccc"]
  20988. });
  20989. }
  20990. },
  20991. {
  20992. label: lang.unsetbackground,
  20993. cmdName: "cleartablebackground",
  20994. exec: function () {
  20995. this.execCommand("cleartablebackground");
  20996. }
  20997. },
  20998. {
  20999. label: lang.redandblue,
  21000. cmdName: "settablebackground",
  21001. exec: function () {
  21002. this.execCommand("settablebackground", {
  21003. repeat: true,
  21004. colorList: ["red", "blue"]
  21005. });
  21006. }
  21007. },
  21008. {
  21009. label: lang.threecolorgradient,
  21010. cmdName: "settablebackground",
  21011. exec: function () {
  21012. this.execCommand("settablebackground", {
  21013. repeat: true,
  21014. colorList: ["#aaa", "#bbb", "#ccc"]
  21015. });
  21016. }
  21017. }
  21018. ]
  21019. },
  21020. {
  21021. group: lang.aligntd,
  21022. icon: 'aligntd',
  21023. subMenu: [{
  21024. cmdName: 'cellalignment',
  21025. value: {
  21026. align: 'left',
  21027. vAlign: 'top'
  21028. }
  21029. },
  21030. {
  21031. cmdName: 'cellalignment',
  21032. value: {
  21033. align: 'center',
  21034. vAlign: 'top'
  21035. }
  21036. },
  21037. {
  21038. cmdName: 'cellalignment',
  21039. value: {
  21040. align: 'right',
  21041. vAlign: 'top'
  21042. }
  21043. },
  21044. {
  21045. cmdName: 'cellalignment',
  21046. value: {
  21047. align: 'left',
  21048. vAlign: 'middle'
  21049. }
  21050. },
  21051. {
  21052. cmdName: 'cellalignment',
  21053. value: {
  21054. align: 'center',
  21055. vAlign: 'middle'
  21056. }
  21057. },
  21058. {
  21059. cmdName: 'cellalignment',
  21060. value: {
  21061. align: 'right',
  21062. vAlign: 'middle'
  21063. }
  21064. },
  21065. {
  21066. cmdName: 'cellalignment',
  21067. value: {
  21068. align: 'left',
  21069. vAlign: 'bottom'
  21070. }
  21071. },
  21072. {
  21073. cmdName: 'cellalignment',
  21074. value: {
  21075. align: 'center',
  21076. vAlign: 'bottom'
  21077. }
  21078. },
  21079. {
  21080. cmdName: 'cellalignment',
  21081. value: {
  21082. align: 'right',
  21083. vAlign: 'bottom'
  21084. }
  21085. }
  21086. ]
  21087. },
  21088. {
  21089. group: lang.aligntable,
  21090. icon: 'aligntable',
  21091. subMenu: [{
  21092. cmdName: 'tablealignment',
  21093. className: 'left',
  21094. label: lang.tableleft,
  21095. value: "left"
  21096. },
  21097. {
  21098. cmdName: 'tablealignment',
  21099. className: 'center',
  21100. label: lang.tablecenter,
  21101. value: "center"
  21102. },
  21103. {
  21104. cmdName: 'tablealignment',
  21105. className: 'right',
  21106. label: lang.tableright,
  21107. value: "right"
  21108. }
  21109. ]
  21110. },
  21111. '-',
  21112. {
  21113. label: lang.insertparagraphbefore,
  21114. cmdName: 'insertparagraph',
  21115. value: true
  21116. },
  21117. {
  21118. label: lang.insertparagraphafter,
  21119. cmdName: 'insertparagraph'
  21120. },
  21121. {
  21122. label: lang['copy'],
  21123. cmdName: 'copy'
  21124. },
  21125. {
  21126. label: lang['paste'],
  21127. cmdName: 'paste'
  21128. }
  21129. ];
  21130. if (!items.length) {
  21131. return;
  21132. }
  21133. var uiUtils = UE.ui.uiUtils;
  21134. me.addListener('contextmenu', function (type, evt) {
  21135. var offset = uiUtils.getViewportOffsetByEvent(evt);
  21136. me.fireEvent('beforeselectionchange');
  21137. if (menu) {
  21138. menu.destroy();
  21139. }
  21140. for (var i = 0, ti, contextItems = []; ti = items[i]; i++) {
  21141. var last;
  21142. (function (item) {
  21143. if (item == '-') {
  21144. if ((last = contextItems[contextItems.length - 1]) && last !== '-') {
  21145. contextItems.push('-');
  21146. }
  21147. } else if (item.hasOwnProperty("group")) {
  21148. for (var j = 0, cj, subMenu = []; cj = item.subMenu[j]; j++) {
  21149. (function (subItem) {
  21150. if (subItem == '-') {
  21151. if ((last = subMenu[subMenu.length - 1]) && last !== '-') {
  21152. subMenu.push('-');
  21153. } else {
  21154. subMenu.splice(subMenu.length - 1);
  21155. }
  21156. } else {
  21157. if ((me.commands[subItem.cmdName] || UE.commands[subItem.cmdName] || subItem.query) &&
  21158. (subItem.query ? subItem.query() : me.queryCommandState(subItem.cmdName)) > -1) {
  21159. subMenu.push({
  21160. 'label': subItem.label || me.getLang("contextMenu." + subItem.cmdName + (subItem.value || '')) || "",
  21161. 'className': 'edui-for-' + subItem.cmdName + (subItem.className ? (' edui-for-' + subItem.cmdName + '-' + subItem.className) : ''),
  21162. onclick: subItem.exec ? function () {
  21163. subItem.exec.call(me);
  21164. } : function () {
  21165. me.execCommand(subItem.cmdName, subItem.value);
  21166. }
  21167. });
  21168. }
  21169. }
  21170. })(cj);
  21171. }
  21172. if (subMenu.length) {
  21173. function getLabel() {
  21174. switch (item.icon) {
  21175. case "table":
  21176. return me.getLang("contextMenu.table");
  21177. case "justifyjustify":
  21178. return me.getLang("contextMenu.paragraph");
  21179. case "aligntd":
  21180. return me.getLang("contextMenu.aligntd");
  21181. case "aligntable":
  21182. return me.getLang("contextMenu.aligntable");
  21183. case "tablesort":
  21184. return lang.tablesort;
  21185. case "borderBack":
  21186. return lang.borderbk;
  21187. default:
  21188. return '';
  21189. }
  21190. }
  21191. contextItems.push({
  21192. //todo 修正成自动获取方式
  21193. 'label': getLabel(),
  21194. className: 'edui-for-' + item.icon,
  21195. 'subMenu': {
  21196. items: subMenu,
  21197. editor: me
  21198. }
  21199. });
  21200. }
  21201. } else {
  21202. //有可能commmand没有加载右键不能出来,或者没有command也想能展示出来添加query方法
  21203. if ((me.commands[item.cmdName] || UE.commands[item.cmdName] || item.query) &&
  21204. (item.query ? item.query.call(me) : me.queryCommandState(item.cmdName)) > -1) {
  21205. contextItems.push({
  21206. 'label': item.label || me.getLang("contextMenu." + item.cmdName),
  21207. className: 'edui-for-' + (item.icon ? item.icon : item.cmdName + (item.value || '')),
  21208. onclick: item.exec ? function () {
  21209. item.exec.call(me);
  21210. } : function () {
  21211. me.execCommand(item.cmdName, item.value);
  21212. }
  21213. });
  21214. }
  21215. }
  21216. })(ti);
  21217. }
  21218. if (contextItems[contextItems.length - 1] == '-') {
  21219. contextItems.pop();
  21220. }
  21221. menu = new UE.ui.Menu({
  21222. items: contextItems,
  21223. className: "edui-contextmenu",
  21224. editor: me
  21225. });
  21226. menu.render();
  21227. menu.showAt(offset);
  21228. me.fireEvent("aftershowcontextmenu", menu);
  21229. domUtils.preventDefault(evt);
  21230. if (browser.ie) {
  21231. var ieRange;
  21232. try {
  21233. ieRange = me.selection.getNative().createRange();
  21234. } catch (e) {
  21235. return;
  21236. }
  21237. if (ieRange.item) {
  21238. var range = new dom.Range(me.document);
  21239. range.selectNode(ieRange.item(0)).select(true, true);
  21240. }
  21241. }
  21242. });
  21243. // 添加复制的flash按钮
  21244. me.addListener('aftershowcontextmenu', function (type, menu) {
  21245. if (me.zeroclipboard) {
  21246. var items = menu.items;
  21247. for (var key in items) {
  21248. if (items[key].className == 'edui-for-copy') {
  21249. me.zeroclipboard.clip(items[key].getDom());
  21250. }
  21251. }
  21252. }
  21253. });
  21254. };
  21255. // plugins/shortcutmenu.js
  21256. ///import core
  21257. ///commands 弹出菜单
  21258. // commandsName popupmenu
  21259. ///commandsTitle 弹出菜单
  21260. /**
  21261. * 弹出菜单
  21262. * @function
  21263. * @name baidu.editor.plugins.popupmenu
  21264. * @author xuheng
  21265. */
  21266. UE.plugins['shortcutmenu'] = function () {
  21267. var me = this,
  21268. menu,
  21269. items = me.options.shortcutMenu || [];
  21270. if (!items.length) {
  21271. return;
  21272. }
  21273. me.addListener('contextmenu mouseup', function (type, e) {
  21274. var me = this,
  21275. customEvt = {
  21276. type: type,
  21277. target: e.target || e.srcElement,
  21278. screenX: e.screenX,
  21279. screenY: e.screenY,
  21280. clientX: e.clientX,
  21281. clientY: e.clientY
  21282. };
  21283. setTimeout(function () {
  21284. var rng = me.selection.getRange();
  21285. if (rng.collapsed === false || type == "contextmenu") {
  21286. if (!menu) {
  21287. menu = new baidu.editor.ui.ShortCutMenu({
  21288. editor: me,
  21289. items: items,
  21290. theme: me.options.theme,
  21291. className: 'edui-shortcutmenu'
  21292. });
  21293. menu.render();
  21294. me.fireEvent("afterrendershortcutmenu", menu);
  21295. }
  21296. menu.show(customEvt, !!UE.plugins['contextmenu']);
  21297. }
  21298. });
  21299. if (type == 'contextmenu') {
  21300. domUtils.preventDefault(e);
  21301. if (browser.ie9below) {
  21302. var ieRange;
  21303. try {
  21304. ieRange = me.selection.getNative().createRange();
  21305. } catch (e) {
  21306. return;
  21307. }
  21308. if (ieRange.item) {
  21309. var range = new dom.Range(me.document);
  21310. range.selectNode(ieRange.item(0)).select(true, true);
  21311. }
  21312. }
  21313. }
  21314. });
  21315. me.addListener('keydown', function (type) {
  21316. if (type == "keydown") {
  21317. menu && !menu.isHidden && menu.hide();
  21318. }
  21319. });
  21320. };
  21321. // plugins/basestyle.js
  21322. /**
  21323. * B、I、sub、super命令支持
  21324. * @file
  21325. * @since 1.2.6.1
  21326. */
  21327. UE.plugins['basestyle'] = function () {
  21328. /**
  21329. * 字体加粗
  21330. * @command bold
  21331. * @param { String } cmd 命令字符串
  21332. * @remind 对已加粗的文本内容执行该命令, 将取消加粗
  21333. * @method execCommand
  21334. * @example
  21335. * ```javascript
  21336. * //editor是编辑器实例
  21337. * //对当前选中的文本内容执行加粗操作
  21338. * //第一次执行, 文本内容加粗
  21339. * editor.execCommand( 'bold' );
  21340. *
  21341. * //第二次执行, 文本内容取消加粗
  21342. * editor.execCommand( 'bold' );
  21343. * ```
  21344. */
  21345. /**
  21346. * 字体倾斜
  21347. * @command italic
  21348. * @method execCommand
  21349. * @param { String } cmd 命令字符串
  21350. * @remind 对已倾斜的文本内容执行该命令, 将取消倾斜
  21351. * @example
  21352. * ```javascript
  21353. * //editor是编辑器实例
  21354. * //对当前选中的文本内容执行斜体操作
  21355. * //第一次操作, 文本内容将变成斜体
  21356. * editor.execCommand( 'italic' );
  21357. *
  21358. * //再次对同一文本内容执行, 则文本内容将恢复正常
  21359. * editor.execCommand( 'italic' );
  21360. * ```
  21361. */
  21362. /**
  21363. * 下标文本,与“superscript”命令互斥
  21364. * @command subscript
  21365. * @method execCommand
  21366. * @remind 把选中的文本内容切换成下标文本, 如果当前选中的文本已经是下标, 则该操作会把文本内容还原成正常文本
  21367. * @param { String } cmd 命令字符串
  21368. * @example
  21369. * ```javascript
  21370. * //editor是编辑器实例
  21371. * //对当前选中的文本内容执行下标操作
  21372. * //第一次操作, 文本内容将变成下标文本
  21373. * editor.execCommand( 'subscript' );
  21374. *
  21375. * //再次对同一文本内容执行, 则文本内容将恢复正常
  21376. * editor.execCommand( 'subscript' );
  21377. * ```
  21378. */
  21379. /**
  21380. * 上标文本,与“subscript”命令互斥
  21381. * @command superscript
  21382. * @method execCommand
  21383. * @remind 把选中的文本内容切换成上标文本, 如果当前选中的文本已经是上标, 则该操作会把文本内容还原成正常文本
  21384. * @param { String } cmd 命令字符串
  21385. * @example
  21386. * ```javascript
  21387. * //editor是编辑器实例
  21388. * //对当前选中的文本内容执行上标操作
  21389. * //第一次操作, 文本内容将变成上标文本
  21390. * editor.execCommand( 'superscript' );
  21391. *
  21392. * //再次对同一文本内容执行, 则文本内容将恢复正常
  21393. * editor.execCommand( 'superscript' );
  21394. * ```
  21395. */
  21396. var basestyles = {
  21397. 'bold': ['strong', 'b'],
  21398. 'italic': ['em', 'i'],
  21399. 'subscript': ['sub'],
  21400. 'superscript': ['sup']
  21401. },
  21402. getObj = function (editor, tagNames) {
  21403. return domUtils.filterNodeList(editor.selection.getStartElementPath(), tagNames);
  21404. },
  21405. me = this;
  21406. //添加快捷键
  21407. me.addshortcutkey({
  21408. "Bold": "ctrl+66", //^B
  21409. "Italic": "ctrl+73", //^I
  21410. "Underline": "ctrl+85" //^U
  21411. });
  21412. me.addInputRule(function (root) {
  21413. utils.each(root.getNodesByTagName('b i'), function (node) {
  21414. switch (node.tagName) {
  21415. case 'b':
  21416. node.tagName = 'strong';
  21417. break;
  21418. case 'i':
  21419. node.tagName = 'em';
  21420. }
  21421. });
  21422. });
  21423. for (var style in basestyles) {
  21424. (function (cmd, tagNames) {
  21425. me.commands[cmd] = {
  21426. execCommand: function (cmdName) {
  21427. var range = me.selection.getRange(),
  21428. obj = getObj(this, tagNames);
  21429. if (range.collapsed) {
  21430. if (obj) {
  21431. var tmpText = me.document.createTextNode('');
  21432. range.insertNode(tmpText).removeInlineStyle(tagNames);
  21433. range.setStartBefore(tmpText);
  21434. domUtils.remove(tmpText);
  21435. } else {
  21436. var tmpNode = range.document.createElement(tagNames[0]);
  21437. if (cmdName == 'superscript' || cmdName == 'subscript') {
  21438. tmpText = me.document.createTextNode('');
  21439. range.insertNode(tmpText)
  21440. .removeInlineStyle(['sub', 'sup'])
  21441. .setStartBefore(tmpText)
  21442. .collapse(true);
  21443. }
  21444. range.insertNode(tmpNode).setStart(tmpNode, 0);
  21445. }
  21446. range.collapse(true);
  21447. } else {
  21448. if (cmdName == 'superscript' || cmdName == 'subscript') {
  21449. if (!obj || obj.tagName.toLowerCase() != cmdName) {
  21450. range.removeInlineStyle(['sub', 'sup']);
  21451. }
  21452. }
  21453. obj ? range.removeInlineStyle(tagNames) : range.applyInlineStyle(tagNames[0]);
  21454. }
  21455. range.select();
  21456. },
  21457. queryCommandState: function () {
  21458. return getObj(this, tagNames) ? 1 : 0;
  21459. }
  21460. };
  21461. })(style, basestyles[style]);
  21462. }
  21463. };
  21464. // plugins/elementpath.js
  21465. /**
  21466. * 选取路径命令
  21467. * @file
  21468. */
  21469. UE.plugins['elementpath'] = function () {
  21470. var currentLevel,
  21471. tagNames,
  21472. me = this;
  21473. me.setOpt('elementPathEnabled', true);
  21474. if (!me.options.elementPathEnabled) {
  21475. return;
  21476. }
  21477. me.commands['elementpath'] = {
  21478. execCommand: function (cmdName, level) {
  21479. var start = tagNames[level],
  21480. range = me.selection.getRange();
  21481. currentLevel = level * 1;
  21482. range.selectNode(start).select();
  21483. },
  21484. queryCommandValue: function () {
  21485. //产生一个副本,不能修改原来的startElementPath;
  21486. var parents = [].concat(this.selection.getStartElementPath()).reverse(),
  21487. names = [];
  21488. tagNames = parents;
  21489. for (var i = 0, ci; ci = parents[i]; i++) {
  21490. if (ci.nodeType == 3) {
  21491. continue;
  21492. }
  21493. var name = ci.tagName.toLowerCase();
  21494. if (name == 'img' && ci.getAttribute('anchorname')) {
  21495. name = 'anchor';
  21496. }
  21497. names[i] = name;
  21498. if (currentLevel == i) {
  21499. currentLevel = -1;
  21500. break;
  21501. }
  21502. }
  21503. return names;
  21504. }
  21505. };
  21506. };
  21507. // plugins/formatmatch.js
  21508. /**
  21509. * 格式刷,只格式inline的
  21510. * @file
  21511. * @since 1.2.6.1
  21512. */
  21513. /**
  21514. * 格式刷
  21515. * @command formatmatch
  21516. * @method execCommand
  21517. * @remind 该操作不能复制段落格式
  21518. * @param { String } cmd 命令字符串
  21519. * @example
  21520. * ```javascript
  21521. * //editor是编辑器实例
  21522. * //获取格式刷
  21523. * editor.execCommand( 'formatmatch' );
  21524. * ```
  21525. */
  21526. UE.plugins['formatmatch'] = function () {
  21527. var me = this,
  21528. list = [],
  21529. img,
  21530. flag = 0;
  21531. me.addListener('reset', function () {
  21532. list = [];
  21533. flag = 0;
  21534. });
  21535. function addList(type, evt) {
  21536. if (browser.webkit) {
  21537. var target = evt.target.tagName == 'IMG' ? evt.target : null;
  21538. }
  21539. function addFormat(range) {
  21540. if (text) {
  21541. range.selectNode(text);
  21542. }
  21543. return range.applyInlineStyle(list[list.length - 1].tagName, null, list);
  21544. }
  21545. me.undoManger && me.undoManger.save();
  21546. var range = me.selection.getRange(),
  21547. imgT = target || range.getClosedNode();
  21548. if (img && imgT && imgT.tagName == 'IMG') {
  21549. //trace:964
  21550. imgT.style.cssText += ';float:' + (img.style.cssFloat || img.style.styleFloat || 'none') + ';display:' + (img.style.display || 'inline');
  21551. img = null;
  21552. } else {
  21553. if (!img) {
  21554. var collapsed = range.collapsed;
  21555. if (collapsed) {
  21556. var text = me.document.createTextNode('match');
  21557. range.insertNode(text).select();
  21558. }
  21559. me.__hasEnterExecCommand = true;
  21560. //不能把block上的属性干掉
  21561. //trace:1553
  21562. var removeFormatAttributes = me.options.removeFormatAttributes;
  21563. me.options.removeFormatAttributes = '';
  21564. me.execCommand('removeformat');
  21565. me.options.removeFormatAttributes = removeFormatAttributes;
  21566. me.__hasEnterExecCommand = false;
  21567. //trace:969
  21568. range = me.selection.getRange();
  21569. if (list.length) {
  21570. addFormat(range);
  21571. }
  21572. if (text) {
  21573. range.setStartBefore(text).collapse(true);
  21574. }
  21575. range.select();
  21576. text && domUtils.remove(text);
  21577. }
  21578. }
  21579. me.undoManger && me.undoManger.save();
  21580. me.removeListener('mouseup', addList);
  21581. flag = 0;
  21582. }
  21583. me.commands['formatmatch'] = {
  21584. execCommand: function (cmdName) {
  21585. if (flag) {
  21586. flag = 0;
  21587. list = [];
  21588. me.removeListener('mouseup', addList);
  21589. return;
  21590. }
  21591. var range = me.selection.getRange();
  21592. img = range.getClosedNode();
  21593. if (!img || img.tagName != 'IMG') {
  21594. range.collapse(true).shrinkBoundary();
  21595. var start = range.startContainer;
  21596. list = domUtils.findParents(start, true, function (node) {
  21597. return !domUtils.isBlockElm(node) && node.nodeType == 1;
  21598. });
  21599. //a不能加入格式刷, 并且克隆节点
  21600. for (var i = 0, ci; ci = list[i]; i++) {
  21601. if (ci.tagName == 'A') {
  21602. list.splice(i, 1);
  21603. break;
  21604. }
  21605. }
  21606. }
  21607. me.addListener('mouseup', addList);
  21608. flag = 1;
  21609. },
  21610. queryCommandState: function () {
  21611. return flag;
  21612. },
  21613. notNeedUndo: 1
  21614. };
  21615. };
  21616. // plugins/searchreplace.js
  21617. ///import core
  21618. ///commands 查找替换
  21619. ///commandsName SearchReplace
  21620. ///commandsTitle 查询替换
  21621. ///commandsDialog dialogs\searchreplace
  21622. /**
  21623. * @description 查找替换
  21624. * @author zhanyi
  21625. */
  21626. UE.plugin.register('searchreplace', function () {
  21627. var me = this;
  21628. var _blockElm = {
  21629. 'table': 1,
  21630. 'tbody': 1,
  21631. 'tr': 1,
  21632. 'ol': 1,
  21633. 'ul': 1
  21634. };
  21635. function findTextInString(textContent, opt, currentIndex) {
  21636. var str = opt.searchStr;
  21637. if (opt.dir == -1) {
  21638. textContent = textContent.split('').reverse().join('');
  21639. str = str.split('').reverse().join('');
  21640. currentIndex = textContent.length - currentIndex;
  21641. }
  21642. var reg = new RegExp(str, 'g' + (opt.casesensitive ? '' : 'i')),
  21643. match;
  21644. while (match = reg.exec(textContent)) {
  21645. if (match.index >= currentIndex) {
  21646. return opt.dir == -1 ? textContent.length - match.index - opt.searchStr.length : match.index;
  21647. }
  21648. }
  21649. return -1
  21650. }
  21651. function findTextBlockElm(node, currentIndex, opt) {
  21652. var textContent, index, methodName = opt.all || opt.dir == 1 ? 'getNextDomNode' : 'getPreDomNode';
  21653. if (domUtils.isBody(node)) {
  21654. node = node.firstChild;
  21655. }
  21656. var first = 1;
  21657. while (node) {
  21658. textContent = node.nodeType == 3 ? node.nodeValue : node[browser.ie ? 'innerText' : 'textContent'];
  21659. index = findTextInString(textContent, opt, currentIndex);
  21660. first = 0;
  21661. if (index != -1) {
  21662. return {
  21663. 'node': node,
  21664. 'index': index
  21665. }
  21666. }
  21667. node = domUtils[methodName](node);
  21668. while (node && _blockElm[node.nodeName.toLowerCase()]) {
  21669. node = domUtils[methodName](node, true);
  21670. }
  21671. if (node) {
  21672. currentIndex = opt.dir == -1 ? (node.nodeType == 3 ? node.nodeValue : node[browser.ie ? 'innerText' : 'textContent']).length : 0;
  21673. }
  21674. }
  21675. }
  21676. function findNTextInBlockElm(node, index, str) {
  21677. var currentIndex = 0,
  21678. currentNode = node.firstChild,
  21679. currentNodeLength = 0,
  21680. result;
  21681. while (currentNode) {
  21682. if (currentNode.nodeType == 3) {
  21683. currentNodeLength = currentNode.nodeValue.replace(/(^[\t\r\n]+)|([\t\r\n]+$)/, '').length;
  21684. currentIndex += currentNodeLength;
  21685. if (currentIndex >= index) {
  21686. return {
  21687. 'node': currentNode,
  21688. 'index': currentNodeLength - (currentIndex - index)
  21689. }
  21690. }
  21691. } else if (!dtd.$empty[currentNode.tagName]) {
  21692. currentNodeLength = currentNode[browser.ie ? 'innerText' : 'textContent'].replace(/(^[\t\r\n]+)|([\t\r\n]+$)/, '').length
  21693. currentIndex += currentNodeLength;
  21694. if (currentIndex >= index) {
  21695. result = findNTextInBlockElm(currentNode, currentNodeLength - (currentIndex - index), str);
  21696. if (result) {
  21697. return result;
  21698. }
  21699. }
  21700. }
  21701. currentNode = domUtils.getNextDomNode(currentNode);
  21702. }
  21703. }
  21704. function searchReplace(me, opt) {
  21705. var rng = me.selection.getRange(),
  21706. startBlockNode,
  21707. searchStr = opt.searchStr,
  21708. span = me.document.createElement('span');
  21709. span.innerHTML = '$$ueditor_searchreplace_key$$';
  21710. rng.shrinkBoundary(true);
  21711. //判断是不是第一次选中
  21712. if (!rng.collapsed) {
  21713. rng.select();
  21714. var rngText = me.selection.getText();
  21715. if (new RegExp('^' + opt.searchStr + '$', (opt.casesensitive ? '' : 'i')).test(rngText)) {
  21716. if (opt.replaceStr != undefined) {
  21717. replaceText(rng, opt.replaceStr);
  21718. rng.select();
  21719. return true;
  21720. } else {
  21721. rng.collapse(opt.dir == -1)
  21722. }
  21723. }
  21724. }
  21725. rng.insertNode(span);
  21726. rng.enlargeToBlockElm(true);
  21727. startBlockNode = rng.startContainer;
  21728. var currentIndex = startBlockNode[browser.ie ? 'innerText' : 'textContent'].indexOf('$$ueditor_searchreplace_key$$');
  21729. rng.setStartBefore(span);
  21730. domUtils.remove(span);
  21731. var result = findTextBlockElm(startBlockNode, currentIndex, opt);
  21732. if (result) {
  21733. var rngStart = findNTextInBlockElm(result.node, result.index, searchStr);
  21734. var rngEnd = findNTextInBlockElm(result.node, result.index + searchStr.length, searchStr);
  21735. rng.setStart(rngStart.node, rngStart.index).setEnd(rngEnd.node, rngEnd.index);
  21736. if (opt.replaceStr !== undefined) {
  21737. replaceText(rng, opt.replaceStr)
  21738. }
  21739. rng.select();
  21740. return true;
  21741. } else {
  21742. rng.setCursor()
  21743. }
  21744. }
  21745. function replaceText(rng, str) {
  21746. str = me.document.createTextNode(str);
  21747. rng.deleteContents().insertNode(str);
  21748. }
  21749. return {
  21750. commands: {
  21751. 'searchreplace': {
  21752. execCommand: function (cmdName, opt) {
  21753. utils.extend(opt, {
  21754. all: false,
  21755. casesensitive: false,
  21756. dir: 1
  21757. }, true);
  21758. var num = 0;
  21759. if (opt.all) {
  21760. var rng = me.selection.getRange(),
  21761. first = me.body.firstChild;
  21762. if (first && first.nodeType == 1) {
  21763. rng.setStart(first, 0);
  21764. rng.shrinkBoundary(true);
  21765. } else if (first.nodeType == 3) {
  21766. rng.setStartBefore(first)
  21767. }
  21768. rng.collapse(true).select(true);
  21769. if (opt.replaceStr !== undefined) {
  21770. me.fireEvent('saveScene');
  21771. }
  21772. while (searchReplace(this, opt)) {
  21773. num++;
  21774. }
  21775. if (num) {
  21776. me.fireEvent('saveScene');
  21777. }
  21778. } else {
  21779. if (opt.replaceStr !== undefined) {
  21780. me.fireEvent('saveScene');
  21781. }
  21782. if (searchReplace(this, opt)) {
  21783. num++
  21784. }
  21785. if (num) {
  21786. me.fireEvent('saveScene');
  21787. }
  21788. }
  21789. return num;
  21790. },
  21791. notNeedUndo: 1
  21792. }
  21793. }
  21794. }
  21795. });
  21796. // plugins/customstyle.js
  21797. /**
  21798. * 自定义样式
  21799. * @file
  21800. * @since 1.2.6.1
  21801. */
  21802. /**
  21803. * 根据config配置文件里“customstyle”选项的值对匹配的标签执行样式替换。
  21804. * @command customstyle
  21805. * @method execCommand
  21806. * @param { String } cmd 命令字符串
  21807. * @example
  21808. * ```javascript
  21809. * editor.execCommand( 'customstyle' );
  21810. * ```
  21811. */
  21812. UE.plugins['customstyle'] = function () {
  21813. var me = this;
  21814. me.setOpt({
  21815. 'customstyle': [{
  21816. tag: 'h1',
  21817. name: 'tc',
  21818. style: 'font-size:32px;font-weight:bold;border-bottom:#ccc 2px solid;padding:0 4px 0 0;text-align:center;margin:0 0 20px 0;'
  21819. },
  21820. {
  21821. tag: 'h1',
  21822. name: 'tl',
  21823. style: 'font-size:32px;font-weight:bold;border-bottom:#ccc 2px solid;padding:0 4px 0 0;text-align:left;margin:0 0 10px 0;'
  21824. },
  21825. {
  21826. tag: 'span',
  21827. name: 'im',
  21828. style: 'font-size:16px;font-style:italic;font-weight:bold;line-height:18px;'
  21829. },
  21830. {
  21831. tag: 'span',
  21832. name: 'hi',
  21833. style: 'font-size:16px;font-style:italic;font-weight:bold;color:rgb(51, 153, 204);line-height:18px;'
  21834. }
  21835. ]
  21836. });
  21837. me.commands['customstyle'] = {
  21838. execCommand: function (cmdName, obj) {
  21839. var me = this,
  21840. tagName = obj.tag,
  21841. node = domUtils.findParent(me.selection.getStart(), function (node) {
  21842. return node.getAttribute('label');
  21843. }, true),
  21844. range, bk, tmpObj = {};
  21845. for (var p in obj) {
  21846. if (obj[p] !== undefined)
  21847. tmpObj[p] = obj[p];
  21848. }
  21849. delete tmpObj.tag;
  21850. if (node && node.getAttribute('label') == obj.label) {
  21851. range = this.selection.getRange();
  21852. bk = range.createBookmark();
  21853. if (range.collapsed) {
  21854. //trace:1732 删掉自定义标签,要有p来回填站位
  21855. if (dtd.$block[node.tagName]) {
  21856. var fillNode = me.document.createElement('p');
  21857. domUtils.moveChild(node, fillNode);
  21858. node.parentNode.insertBefore(fillNode, node);
  21859. domUtils.remove(node);
  21860. } else {
  21861. domUtils.remove(node, true);
  21862. }
  21863. } else {
  21864. var common = domUtils.getCommonAncestor(bk.start, bk.end),
  21865. nodes = domUtils.getElementsByTagName(common, tagName);
  21866. if (new RegExp(tagName, 'i').test(common.tagName)) {
  21867. nodes.push(common);
  21868. }
  21869. for (var i = 0, ni; ni = nodes[i++];) {
  21870. if (ni.getAttribute('label') == obj.label) {
  21871. var ps = domUtils.getPosition(ni, bk.start),
  21872. pe = domUtils.getPosition(ni, bk.end);
  21873. if ((ps & domUtils.POSITION_FOLLOWING || ps & domUtils.POSITION_CONTAINS) &&
  21874. (pe & domUtils.POSITION_PRECEDING || pe & domUtils.POSITION_CONTAINS)
  21875. )
  21876. if (dtd.$block[tagName]) {
  21877. var fillNode = me.document.createElement('p');
  21878. domUtils.moveChild(ni, fillNode);
  21879. ni.parentNode.insertBefore(fillNode, ni);
  21880. }
  21881. domUtils.remove(ni, true);
  21882. }
  21883. }
  21884. node = domUtils.findParent(common, function (node) {
  21885. return node.getAttribute('label') == obj.label;
  21886. }, true);
  21887. if (node) {
  21888. domUtils.remove(node, true);
  21889. }
  21890. }
  21891. range.moveToBookmark(bk).select();
  21892. } else {
  21893. if (dtd.$block[tagName]) {
  21894. this.execCommand('paragraph', tagName, tmpObj, 'customstyle');
  21895. range = me.selection.getRange();
  21896. if (!range.collapsed) {
  21897. range.collapse();
  21898. node = domUtils.findParent(me.selection.getStart(), function (node) {
  21899. return node.getAttribute('label') == obj.label;
  21900. }, true);
  21901. var pNode = me.document.createElement('p');
  21902. domUtils.insertAfter(node, pNode);
  21903. domUtils.fillNode(me.document, pNode);
  21904. range.setStart(pNode, 0).setCursor();
  21905. }
  21906. } else {
  21907. range = me.selection.getRange();
  21908. if (range.collapsed) {
  21909. node = me.document.createElement(tagName);
  21910. domUtils.setAttributes(node, tmpObj);
  21911. range.insertNode(node).setStart(node, 0).setCursor();
  21912. return;
  21913. }
  21914. bk = range.createBookmark();
  21915. range.applyInlineStyle(tagName, tmpObj).moveToBookmark(bk).select();
  21916. }
  21917. }
  21918. },
  21919. queryCommandValue: function () {
  21920. var parent = domUtils.filterNodeList(
  21921. this.selection.getStartElementPath(),
  21922. function (node) {
  21923. return node.getAttribute('label')
  21924. }
  21925. );
  21926. return parent ? parent.getAttribute('label') : '';
  21927. }
  21928. };
  21929. //当去掉customstyle是,如果是块元素,用p代替
  21930. me.addListener('keyup', function (type, evt) {
  21931. var keyCode = evt.keyCode || evt.which;
  21932. if (keyCode == 32 || keyCode == 13) {
  21933. var range = me.selection.getRange();
  21934. if (range.collapsed) {
  21935. var node = domUtils.findParent(me.selection.getStart(), function (node) {
  21936. return node.getAttribute('label');
  21937. }, true);
  21938. if (node && dtd.$block[node.tagName] && domUtils.isEmptyNode(node)) {
  21939. var p = me.document.createElement('p');
  21940. domUtils.insertAfter(node, p);
  21941. domUtils.fillNode(me.document, p);
  21942. domUtils.remove(node);
  21943. range.setStart(p, 0).setCursor();
  21944. }
  21945. }
  21946. }
  21947. });
  21948. };
  21949. // plugins/catchremoteimage.js
  21950. ///import core
  21951. ///commands 远程图片抓取
  21952. ///commandsName catchRemoteImage,catchremoteimageenable
  21953. ///commandsTitle 远程图片抓取
  21954. /**
  21955. * 远程图片抓取,当开启本插件时所有不符合本地域名的图片都将被抓取成为本地服务器上的图片
  21956. */
  21957. UE.plugins['catchremoteimage'] = function () {
  21958. var me = this,
  21959. ajax = UE.ajax;
  21960. /* 设置默认值 */
  21961. if (me.options.catchRemoteImageEnable === false) return;
  21962. me.setOpt({
  21963. catchRemoteImageEnable: false
  21964. });
  21965. me.addListener("afterpaste", function () {
  21966. me.fireEvent("catchRemoteImage");
  21967. });
  21968. me.addListener("catchRemoteImage", function () {
  21969. var catcherLocalDomain = me.getOpt('catcherLocalDomain'),
  21970. catcherActionUrl = me.getActionUrl(me.getOpt('catcherActionName')),
  21971. catcherUrlPrefix = me.getOpt('catcherUrlPrefix'),
  21972. catcherFieldName = me.getOpt('catcherFieldName');
  21973. var remoteImages = [],
  21974. imgs = domUtils.getElementsByTagName(me.document, "img"),
  21975. test = function (src, urls) {
  21976. if (src.indexOf(location.host) != -1 || /(^\.)|(^\/)/.test(src)) {
  21977. return true;
  21978. }
  21979. if (urls) {
  21980. for (var j = 0, url; url = urls[j++];) {
  21981. if (src.indexOf(url) !== -1) {
  21982. return true;
  21983. }
  21984. }
  21985. }
  21986. return false;
  21987. };
  21988. for (var i = 0, ci; ci = imgs[i++];) {
  21989. if (ci.getAttribute("word_img")) {
  21990. continue;
  21991. }
  21992. var src = ci.getAttribute("_src") || ci.src || "";
  21993. if (/^(https?|ftp):/i.test(src) && !test(src, catcherLocalDomain)) {
  21994. remoteImages.push(src);
  21995. }
  21996. }
  21997. if (remoteImages.length) {
  21998. catchremoteimage(remoteImages, {
  21999. //成功抓取
  22000. success: function (r) {
  22001. try {
  22002. var info = r.state !== undefined ? r : eval("(" + r.responseText + ")");
  22003. } catch (e) {
  22004. return;
  22005. }
  22006. /* 获取源路径和新路径 */
  22007. var i, j, ci, cj, oldSrc, newSrc, list = info.list;
  22008. for (i = 0; ci = imgs[i++];) {
  22009. oldSrc = ci.getAttribute("_src") || ci.src || "";
  22010. for (j = 0; cj = list[j++];) {
  22011. if (oldSrc == cj.source && cj.state == "SUCCESS") { //抓取失败时不做替换处理
  22012. newSrc = catcherUrlPrefix + cj.url;
  22013. domUtils.setAttributes(ci, {
  22014. "src": newSrc,
  22015. "_src": newSrc
  22016. });
  22017. break;
  22018. }
  22019. }
  22020. }
  22021. me.fireEvent('catchremotesuccess')
  22022. },
  22023. //回调失败,本次请求超时
  22024. error: function () {
  22025. me.fireEvent("catchremoteerror");
  22026. }
  22027. });
  22028. }
  22029. function catchremoteimage(imgs, callbacks) {
  22030. var params = utils.serializeParam(me.queryCommandValue('serverparam')) || '',
  22031. url = utils.formatUrl(catcherActionUrl + (catcherActionUrl.indexOf('?') == -1 ? '?' : '&') + params),
  22032. isJsonp = utils.isCrossDomainUrl(url),
  22033. opt = {
  22034. 'method': 'POST',
  22035. 'dataType': isJsonp ? 'jsonp' : '',
  22036. 'timeout': 60000, //单位:毫秒,回调请求超时设置。目标用户如果网速不是很快的话此处建议设置一个较大的数值
  22037. 'onsuccess': callbacks["success"],
  22038. 'onerror': callbacks["error"]
  22039. };
  22040. opt[catcherFieldName] = imgs;
  22041. ajax.request(url, opt);
  22042. }
  22043. });
  22044. };
  22045. // plugins/snapscreen.js
  22046. /**
  22047. * 截屏插件,为UEditor提供插入支持
  22048. * @file
  22049. * @since 1.4.2
  22050. */
  22051. UE.plugin.register('snapscreen', function () {
  22052. var me = this;
  22053. var snapplugin;
  22054. function getLocation(url) {
  22055. var search,
  22056. a = document.createElement('a'),
  22057. params = utils.serializeParam(me.queryCommandValue('serverparam')) || '';
  22058. a.href = url;
  22059. if (browser.ie) {
  22060. a.href = a.href;
  22061. }
  22062. search = a.search;
  22063. if (params) {
  22064. search = search + (search.indexOf('?') == -1 ? '?' : '&') + params;
  22065. search = search.replace(/[&]+/ig, '&');
  22066. }
  22067. return {
  22068. 'port': a.port,
  22069. 'hostname': a.hostname,
  22070. 'path': a.pathname + search || +a.hash
  22071. }
  22072. }
  22073. return {
  22074. commands: {
  22075. /**
  22076. * 字体背景颜色
  22077. * @command snapscreen
  22078. * @method execCommand
  22079. * @param { String } cmd 命令字符串
  22080. * @example
  22081. * ```javascript
  22082. * editor.execCommand('snapscreen');
  22083. * ```
  22084. */
  22085. 'snapscreen': {
  22086. execCommand: function (cmd) {
  22087. var url, local, res;
  22088. var lang = me.getLang("snapScreen_plugin");
  22089. if (!snapplugin) {
  22090. var container = me.container;
  22091. var doc = me.container.ownerDocument || me.container.document;
  22092. snapplugin = doc.createElement("object");
  22093. try {
  22094. snapplugin.type = "application/x-pluginbaidusnap";
  22095. } catch (e) {
  22096. return;
  22097. }
  22098. snapplugin.style.cssText = "position:absolute;left:-9999px;width:0;height:0;";
  22099. snapplugin.setAttribute("width", "0");
  22100. snapplugin.setAttribute("height", "0");
  22101. container.appendChild(snapplugin);
  22102. }
  22103. function onSuccess(rs) {
  22104. try {
  22105. rs = eval("(" + rs + ")");
  22106. if (rs.state == 'SUCCESS') {
  22107. var opt = me.options;
  22108. me.execCommand('insertimage', {
  22109. src: opt.snapscreenUrlPrefix + rs.url,
  22110. _src: opt.snapscreenUrlPrefix + rs.url,
  22111. alt: rs.title || '',
  22112. floatStyle: opt.snapscreenImgAlign
  22113. });
  22114. } else {
  22115. alert(rs.state);
  22116. }
  22117. } catch (e) {
  22118. alert(lang.callBackErrorMsg);
  22119. }
  22120. }
  22121. url = me.getActionUrl(me.getOpt('snapscreenActionName'));
  22122. local = getLocation(url);
  22123. setTimeout(function () {
  22124. try {
  22125. res = snapplugin.saveSnapshot(local.hostname, local.path, local.port);
  22126. } catch (e) {
  22127. me.ui._dialogs['snapscreenDialog'].open();
  22128. return;
  22129. }
  22130. onSuccess(res);
  22131. }, 50);
  22132. },
  22133. queryCommandState: function () {
  22134. return (navigator.userAgent.indexOf("Windows", 0) != -1) ? 0 : -1;
  22135. }
  22136. }
  22137. }
  22138. }
  22139. });
  22140. // plugins/insertparagraph.js
  22141. /**
  22142. * 插入段落
  22143. * @file
  22144. * @since 1.2.6.1
  22145. */
  22146. /**
  22147. * 插入段落
  22148. * @command insertparagraph
  22149. * @method execCommand
  22150. * @param { String } cmd 命令字符串
  22151. * @example
  22152. * ```javascript
  22153. * //editor是编辑器实例
  22154. * editor.execCommand( 'insertparagraph' );
  22155. * ```
  22156. */
  22157. UE.commands['insertparagraph'] = {
  22158. execCommand: function (cmdName, front) {
  22159. var me = this,
  22160. range = me.selection.getRange(),
  22161. start = range.startContainer,
  22162. tmpNode;
  22163. while (start) {
  22164. if (domUtils.isBody(start)) {
  22165. break;
  22166. }
  22167. tmpNode = start;
  22168. start = start.parentNode;
  22169. }
  22170. if (tmpNode) {
  22171. var p = me.document.createElement('p');
  22172. if (front) {
  22173. tmpNode.parentNode.insertBefore(p, tmpNode)
  22174. } else {
  22175. tmpNode.parentNode.insertBefore(p, tmpNode.nextSibling)
  22176. }
  22177. domUtils.fillNode(me.document, p);
  22178. range.setStart(p, 0).setCursor(false, true);
  22179. }
  22180. }
  22181. };
  22182. // plugins/webapp.js
  22183. /**
  22184. * 百度应用
  22185. * @file
  22186. * @since 1.2.6.1
  22187. */
  22188. /**
  22189. * 插入百度应用
  22190. * @command webapp
  22191. * @method execCommand
  22192. * @remind 需要百度APPKey
  22193. * @remind 百度应用主页: <a href="http://app.baidu.com/" target="_blank">http://app.baidu.com/</a>
  22194. * @param { Object } appOptions 应用所需的参数项, 支持的key有: title=>应用标题, width=>应用容器宽度,
  22195. * height=>应用容器高度,logo=>应用logo,url=>应用地址
  22196. * @example
  22197. * ```javascript
  22198. * //editor是编辑器实例
  22199. * //在编辑器里插入一个“植物大战僵尸”的APP
  22200. * editor.execCommand( 'webapp' , {
  22201. * title: '植物大战僵尸',
  22202. * width: 560,
  22203. * height: 465,
  22204. * logo: '应用展示的图片',
  22205. * url: '百度应用的地址'
  22206. * } );
  22207. * ```
  22208. */
  22209. //UE.plugins['webapp'] = function () {
  22210. // var me = this;
  22211. // function createInsertStr( obj, toIframe, addParagraph ) {
  22212. // return !toIframe ?
  22213. // (addParagraph ? '<p>' : '') + '<img title="'+obj.title+'" width="' + obj.width + '" height="' + obj.height + '"' +
  22214. // ' src="' + me.options.UEDITOR_HOME_URL + 'themes/default/images/spacer.gif" style="background:url(' + obj.logo+') no-repeat center center; border:1px solid gray;" class="edui-faked-webapp" _url="' + obj.url + '" />' +
  22215. // (addParagraph ? '</p>' : '')
  22216. // :
  22217. // '<iframe class="edui-faked-webapp" title="'+obj.title+'" width="' + obj.width + '" height="' + obj.height + '" scrolling="no" frameborder="0" src="' + obj.url + '" logo_url = '+obj.logo+'></iframe>';
  22218. // }
  22219. //
  22220. // function switchImgAndIframe( img2frame ) {
  22221. // var tmpdiv,
  22222. // nodes = domUtils.getElementsByTagName( me.document, !img2frame ? "iframe" : "img" );
  22223. // for ( var i = 0, node; node = nodes[i++]; ) {
  22224. // if ( node.className != "edui-faked-webapp" ){
  22225. // continue;
  22226. // }
  22227. // tmpdiv = me.document.createElement( "div" );
  22228. // tmpdiv.innerHTML = createInsertStr( img2frame ? {url:node.getAttribute( "_url" ), width:node.width, height:node.height,title:node.title,logo:node.style.backgroundImage.replace("url(","").replace(")","")} : {url:node.getAttribute( "src", 2 ),title:node.title, width:node.width, height:node.height,logo:node.getAttribute("logo_url")}, img2frame ? true : false,false );
  22229. // node.parentNode.replaceChild( tmpdiv.firstChild, node );
  22230. // }
  22231. // }
  22232. //
  22233. // me.addListener( "beforegetcontent", function () {
  22234. // switchImgAndIframe( true );
  22235. // } );
  22236. // me.addListener( 'aftersetcontent', function () {
  22237. // switchImgAndIframe( false );
  22238. // } );
  22239. // me.addListener( 'aftergetcontent', function ( cmdName ) {
  22240. // if ( cmdName == 'aftergetcontent' && me.queryCommandState( 'source' ) ){
  22241. // return;
  22242. // }
  22243. // switchImgAndIframe( false );
  22244. // } );
  22245. //
  22246. // me.commands['webapp'] = {
  22247. // execCommand:function ( cmd, obj ) {
  22248. // me.execCommand( "inserthtml", createInsertStr( obj, false,true ) );
  22249. // }
  22250. // };
  22251. //};
  22252. UE.plugin.register('webapp', function () {
  22253. var me = this;
  22254. function createInsertStr(obj, toEmbed) {
  22255. return !toEmbed ?
  22256. '<img title="' + obj.title + '" width="' + obj.width + '" height="' + obj.height + '"' +
  22257. ' src="' + me.options.UEDITOR_HOME_URL + 'themes/default/images/spacer.gif" _logo_url="' + obj.logo + '" style="background:url(' + obj.logo +
  22258. ') no-repeat center center; border:1px solid gray;" class="edui-faked-webapp" _url="' + obj.url + '" ' +
  22259. (obj.align && !obj.cssfloat ? 'align="' + obj.align + '"' : '') +
  22260. (obj.cssfloat ? 'style="float:' + obj.cssfloat + '"' : '') +
  22261. '/>' :
  22262. '<iframe class="edui-faked-webapp" title="' + obj.title + '" ' +
  22263. (obj.align && !obj.cssfloat ? 'align="' + obj.align + '"' : '') +
  22264. (obj.cssfloat ? 'style="float:' + obj.cssfloat + '"' : '') +
  22265. 'width="' + obj.width + '" height="' + obj.height + '" scrolling="no" frameborder="0" src="' + obj.url + '" logo_url = "' + obj.logo + '"></iframe>'
  22266. }
  22267. return {
  22268. outputRule: function (root) {
  22269. utils.each(root.getNodesByTagName('img'), function (node) {
  22270. var html;
  22271. if (node.getAttr('class') == 'edui-faked-webapp') {
  22272. html = createInsertStr({
  22273. title: node.getAttr('title'),
  22274. 'width': node.getAttr('width'),
  22275. 'height': node.getAttr('height'),
  22276. 'align': node.getAttr('align'),
  22277. 'cssfloat': node.getStyle('float'),
  22278. 'url': node.getAttr("_url"),
  22279. 'logo': node.getAttr('_logo_url')
  22280. }, true);
  22281. var embed = UE.uNode.createElement(html);
  22282. node.parentNode.replaceChild(embed, node);
  22283. }
  22284. })
  22285. },
  22286. inputRule: function (root) {
  22287. utils.each(root.getNodesByTagName('iframe'), function (node) {
  22288. if (node.getAttr('class') == 'edui-faked-webapp') {
  22289. var img = UE.uNode.createElement(createInsertStr({
  22290. title: node.getAttr('title'),
  22291. 'width': node.getAttr('width'),
  22292. 'height': node.getAttr('height'),
  22293. 'align': node.getAttr('align'),
  22294. 'cssfloat': node.getStyle('float'),
  22295. 'url': node.getAttr("src"),
  22296. 'logo': node.getAttr('logo_url')
  22297. }));
  22298. node.parentNode.replaceChild(img, node);
  22299. }
  22300. })
  22301. },
  22302. commands: {
  22303. /**
  22304. * 插入百度应用
  22305. * @command webapp
  22306. * @method execCommand
  22307. * @remind 需要百度APPKey
  22308. * @remind 百度应用主页: <a href="http://app.baidu.com/" target="_blank">http://app.baidu.com/</a>
  22309. * @param { Object } appOptions 应用所需的参数项, 支持的key有: title=>应用标题, width=>应用容器宽度,
  22310. * height=>应用容器高度,logo=>应用logo,url=>应用地址
  22311. * @example
  22312. * ```javascript
  22313. * //editor是编辑器实例
  22314. * //在编辑器里插入一个“植物大战僵尸”的APP
  22315. * editor.execCommand( 'webapp' , {
  22316. * title: '植物大战僵尸',
  22317. * width: 560,
  22318. * height: 465,
  22319. * logo: '应用展示的图片',
  22320. * url: '百度应用的地址'
  22321. * } );
  22322. * ```
  22323. */
  22324. 'webapp': {
  22325. execCommand: function (cmd, obj) {
  22326. var me = this,
  22327. str = createInsertStr(utils.extend(obj, {
  22328. align: 'none'
  22329. }), false);
  22330. me.execCommand("inserthtml", str);
  22331. },
  22332. queryCommandState: function () {
  22333. var me = this,
  22334. img = me.selection.getRange().getClosedNode(),
  22335. flag = img && (img.className == "edui-faked-webapp");
  22336. return flag ? 1 : 0;
  22337. }
  22338. }
  22339. }
  22340. }
  22341. });
  22342. // plugins/template.js
  22343. ///import core
  22344. ///import plugins\inserthtml.js
  22345. ///import plugins\cleardoc.js
  22346. ///commands 模板
  22347. ///commandsName template
  22348. ///commandsTitle 模板
  22349. ///commandsDialog dialogs\template
  22350. UE.plugins['template'] = function () {
  22351. UE.commands['template'] = {
  22352. execCommand: function (cmd, obj) {
  22353. obj.html && this.execCommand("inserthtml", obj.html);
  22354. }
  22355. };
  22356. this.addListener("click", function (type, evt) {
  22357. var el = evt.target || evt.srcElement,
  22358. range = this.selection.getRange();
  22359. var tnode = domUtils.findParent(el, function (node) {
  22360. if (node.className && domUtils.hasClass(node, "ue_t")) {
  22361. return node;
  22362. }
  22363. }, true);
  22364. tnode && range.selectNode(tnode).shrinkBoundary().select();
  22365. });
  22366. this.addListener("keydown", function (type, evt) {
  22367. var range = this.selection.getRange();
  22368. if (!range.collapsed) {
  22369. if (!evt.ctrlKey && !evt.metaKey && !evt.shiftKey && !evt.altKey) {
  22370. var tnode = domUtils.findParent(range.startContainer, function (node) {
  22371. if (node.className && domUtils.hasClass(node, "ue_t")) {
  22372. return node;
  22373. }
  22374. }, true);
  22375. if (tnode) {
  22376. domUtils.removeClasses(tnode, ["ue_t"]);
  22377. }
  22378. }
  22379. }
  22380. });
  22381. };
  22382. // plugins/music.js
  22383. /**
  22384. * 插入音乐命令
  22385. * @file
  22386. */
  22387. UE.plugin.register('music', function () {
  22388. var me = this;
  22389. function creatInsertStr(url, width, height, align, cssfloat, toEmbed) {
  22390. return !toEmbed ?
  22391. '<img ' +
  22392. (align && !cssfloat ? 'align="' + align + '"' : '') +
  22393. (cssfloat ? 'style="float:' + cssfloat + '"' : '') +
  22394. ' width="' + width + '" height="' + height + '" _url="' + url + '" class="edui-faked-music"' +
  22395. ' src="' + me.options.langPath + me.options.lang + '/images/music.png" />' :
  22396. '<embed type="application/x-shockwave-flash" class="edui-faked-music" pluginspage="http://www.macromedia.com/go/getflashplayer"' +
  22397. ' src="' + url + '" width="' + width + '" height="' + height + '" ' + (align && !cssfloat ? 'align="' + align + '"' : '') +
  22398. (cssfloat ? 'style="float:' + cssfloat + '"' : '') +
  22399. ' wmode="transparent" play="true" loop="false" menu="false" allowscriptaccess="never" allowfullscreen="true" >';
  22400. }
  22401. return {
  22402. outputRule: function (root) {
  22403. utils.each(root.getNodesByTagName('img'), function (node) {
  22404. var html;
  22405. if (node.getAttr('class') == 'edui-faked-music') {
  22406. var cssfloat = node.getStyle('float');
  22407. var align = node.getAttr('align');
  22408. html = creatInsertStr(node.getAttr("_url"), node.getAttr('width'), node.getAttr('height'), align, cssfloat, true);
  22409. var embed = UE.uNode.createElement(html);
  22410. node.parentNode.replaceChild(embed, node);
  22411. }
  22412. })
  22413. },
  22414. inputRule: function (root) {
  22415. utils.each(root.getNodesByTagName('embed'), function (node) {
  22416. if (node.getAttr('class') == 'edui-faked-music') {
  22417. var cssfloat = node.getStyle('float');
  22418. var align = node.getAttr('align');
  22419. html = creatInsertStr(node.getAttr("src"), node.getAttr('width'), node.getAttr('height'), align, cssfloat, false);
  22420. var img = UE.uNode.createElement(html);
  22421. node.parentNode.replaceChild(img, node);
  22422. }
  22423. })
  22424. },
  22425. commands: {
  22426. /**
  22427. * 插入音乐
  22428. * @command music
  22429. * @method execCommand
  22430. * @param { Object } musicOptions 插入音乐的参数项, 支持的key有: url=>音乐地址;
  22431. * width=>音乐容器宽度;height=>音乐容器高度;align=>音乐文件的对齐方式, 可选值有: left, center, right, none
  22432. * @example
  22433. * ```javascript
  22434. * //editor是编辑器实例
  22435. * //在编辑器里插入一个“植物大战僵尸”的APP
  22436. * editor.execCommand( 'music' , {
  22437. * width: 400,
  22438. * height: 95,
  22439. * align: "center",
  22440. * url: "音乐地址"
  22441. * } );
  22442. * ```
  22443. */
  22444. 'music': {
  22445. execCommand: function (cmd, musicObj) {
  22446. var me = this,
  22447. str = creatInsertStr(musicObj.url, musicObj.width || 400, musicObj.height || 95, "none", false);
  22448. me.execCommand("inserthtml", str);
  22449. },
  22450. queryCommandState: function () {
  22451. var me = this,
  22452. img = me.selection.getRange().getClosedNode(),
  22453. flag = img && (img.className == "edui-faked-music");
  22454. return flag ? 1 : 0;
  22455. }
  22456. }
  22457. }
  22458. }
  22459. });
  22460. // plugins/autoupload.js
  22461. /**
  22462. * @description
  22463. * 1.拖放文件到编辑区域,自动上传并插入到选区
  22464. * 2.插入粘贴板的图片,自动上传并插入到选区
  22465. * @author Jinqn
  22466. * @date 2013-10-14
  22467. */
  22468. UE.plugin.register('autoupload', function () {
  22469. function sendAndInsertFile(file, editor) {
  22470. var me = editor;
  22471. //模拟数据
  22472. var fieldName, urlPrefix, maxSize, allowFiles, actionUrl,
  22473. loadingHtml, errorHandler, successHandler,
  22474. filetype = /image\/\w+/i.test(file.type) ? 'image' : 'file',
  22475. loadingId = 'loading_' + (+new Date()).toString(36);
  22476. fieldName = me.getOpt(filetype + 'FieldName');
  22477. urlPrefix = me.getOpt(filetype + 'UrlPrefix');
  22478. maxSize = me.getOpt(filetype + 'MaxSize');
  22479. allowFiles = me.getOpt(filetype + 'AllowFiles');
  22480. actionUrl = me.getActionUrl(me.getOpt(filetype + 'ActionName'));
  22481. errorHandler = function (title) {
  22482. var loader = me.document.getElementById(loadingId);
  22483. loader && domUtils.remove(loader);
  22484. me.fireEvent('showmessage', {
  22485. 'id': loadingId,
  22486. 'content': title,
  22487. 'type': 'error',
  22488. 'timeout': 4000
  22489. });
  22490. };
  22491. if (filetype == 'image') {
  22492. loadingHtml = '<img class="loadingclass" id="' + loadingId + '" src="' +
  22493. me.options.themePath + me.options.theme +
  22494. '/images/spacer.gif" title="' + (me.getLang('autoupload.loading') || '') + '" >';
  22495. successHandler = function (data) {
  22496. var link = urlPrefix + data.url,
  22497. loader = me.document.getElementById(loadingId);
  22498. if (loader) {
  22499. loader.setAttribute('src', link);
  22500. loader.setAttribute('_src', link);
  22501. loader.setAttribute('title', data.title || '');
  22502. loader.setAttribute('alt', data.original || '');
  22503. loader.removeAttribute('id');
  22504. domUtils.removeClasses(loader, 'loadingclass');
  22505. }
  22506. };
  22507. } else {
  22508. loadingHtml = '<p>' +
  22509. '<img class="loadingclass" id="' + loadingId + '" src="' +
  22510. me.options.themePath + me.options.theme +
  22511. '/images/spacer.gif" title="' + (me.getLang('autoupload.loading') || '') + '" >' +
  22512. '</p>';
  22513. successHandler = function (data) {
  22514. var link = urlPrefix + data.url,
  22515. loader = me.document.getElementById(loadingId);
  22516. var rng = me.selection.getRange(),
  22517. bk = rng.createBookmark();
  22518. rng.selectNode(loader).select();
  22519. me.execCommand('insertfile', {
  22520. 'url': link
  22521. });
  22522. rng.moveToBookmark(bk).select();
  22523. };
  22524. }
  22525. /* 插入loading的占位符 */
  22526. me.execCommand('inserthtml', loadingHtml);
  22527. /* 判断后端配置是否没有加载成功 */
  22528. if (!me.getOpt(filetype + 'ActionName')) {
  22529. errorHandler(me.getLang('autoupload.errorLoadConfig'));
  22530. return;
  22531. }
  22532. /* 判断文件大小是否超出限制 */
  22533. if (file.size > maxSize) {
  22534. errorHandler(me.getLang('autoupload.exceedSizeError'));
  22535. return;
  22536. }
  22537. /* 判断文件格式是否超出允许 */
  22538. var fileext = file.name ? file.name.substr(file.name.lastIndexOf('.')) : '';
  22539. if ((fileext && filetype != 'image') || (allowFiles && (allowFiles.join('') + '.').indexOf(fileext.toLowerCase() + '.') == -1)) {
  22540. errorHandler(me.getLang('autoupload.exceedTypeError'));
  22541. return;
  22542. }
  22543. /* 创建Ajax并提交 */
  22544. var xhr = new XMLHttpRequest(),
  22545. fd = new FormData(),
  22546. params = utils.serializeParam(me.queryCommandValue('serverparam')) || '',
  22547. url = utils.formatUrl(actionUrl + (actionUrl.indexOf('?') == -1 ? '?' : '&') + params);
  22548. fd.append(fieldName, file, file.name || ('blob.' + file.type.substr('image/'.length)));
  22549. fd.append('type', 'ajax');
  22550. xhr.open("post", url, true);
  22551. xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
  22552. xhr.addEventListener('load', function (e) {
  22553. try {
  22554. var json = (new Function("return " + utils.trim(e.target.response)))();
  22555. if (json.state == 'SUCCESS' && json.url) {
  22556. successHandler(json);
  22557. } else {
  22558. errorHandler(json.state);
  22559. }
  22560. } catch (er) {
  22561. errorHandler(me.getLang('autoupload.loadError'));
  22562. }
  22563. });
  22564. xhr.send(fd);
  22565. }
  22566. function getPasteImage(e) {
  22567. return e.clipboardData && e.clipboardData.items && e.clipboardData.items.length == 1 && /^image\//.test(e.clipboardData.items[0].type) ? e.clipboardData.items : null;
  22568. }
  22569. function getDropImage(e) {
  22570. return e.dataTransfer && e.dataTransfer.files ? e.dataTransfer.files : null;
  22571. }
  22572. return {
  22573. outputRule: function (root) {
  22574. utils.each(root.getNodesByTagName('img'), function (n) {
  22575. if (/\b(loaderrorclass)|(bloaderrorclass)\b/.test(n.getAttr('class'))) {
  22576. n.parentNode.removeChild(n);
  22577. }
  22578. });
  22579. utils.each(root.getNodesByTagName('p'), function (n) {
  22580. if (/\bloadpara\b/.test(n.getAttr('class'))) {
  22581. n.parentNode.removeChild(n);
  22582. }
  22583. });
  22584. },
  22585. bindEvents: {
  22586. //插入粘贴板的图片,拖放插入图片
  22587. 'ready': function (e) {
  22588. var me = this;
  22589. if (window.FormData && window.FileReader) {
  22590. domUtils.on(me.body, 'paste drop', function (e) {
  22591. var hasImg = false,
  22592. items;
  22593. //获取粘贴板文件列表或者拖放文件列表
  22594. items = e.type == 'paste' ? getPasteImage(e) : getDropImage(e);
  22595. if (items) {
  22596. var len = items.length,
  22597. file;
  22598. while (len--) {
  22599. file = items[len];
  22600. if (file.getAsFile) file = file.getAsFile();
  22601. if (file && file.size > 0) {
  22602. sendAndInsertFile(file, me);
  22603. hasImg = true;
  22604. }
  22605. }
  22606. hasImg && e.preventDefault();
  22607. }
  22608. });
  22609. //取消拖放图片时出现的文字光标位置提示
  22610. domUtils.on(me.body, 'dragover', function (e) {
  22611. if (e.dataTransfer.types[0] == 'Files') {
  22612. e.preventDefault();
  22613. }
  22614. });
  22615. //设置loading的样式
  22616. utils.cssRule('loading',
  22617. '.loadingclass{display:inline-block;cursor:default;background: url(\'' +
  22618. this.options.themePath +
  22619. this.options.theme + '/images/loading.gif\') no-repeat center center transparent;border:1px solid #cccccc;margin-left:1px;height: 22px;width: 22px;}\n' +
  22620. '.loaderrorclass{display:inline-block;cursor:default;background: url(\'' +
  22621. this.options.themePath +
  22622. this.options.theme + '/images/loaderror.png\') no-repeat center center transparent;border:1px solid #cccccc;margin-right:1px;height: 22px;width: 22px;' +
  22623. '}',
  22624. this.document);
  22625. }
  22626. }
  22627. }
  22628. }
  22629. });
  22630. // plugins/autosave.js
  22631. UE.plugin.register('autosave', function () {
  22632. var me = this,
  22633. //无限循环保护
  22634. lastSaveTime = new Date(),
  22635. //最小保存间隔时间
  22636. MIN_TIME = 20,
  22637. //auto save key
  22638. saveKey = null;
  22639. function save(editor) {
  22640. var saveData;
  22641. if (new Date() - lastSaveTime < MIN_TIME) {
  22642. return;
  22643. }
  22644. if (!editor.hasContents()) {
  22645. //这里不能调用命令来删除, 会造成事件死循环
  22646. saveKey && me.removePreferences(saveKey);
  22647. return;
  22648. }
  22649. lastSaveTime = new Date();
  22650. editor._saveFlag = null;
  22651. saveData = me.body.innerHTML;
  22652. if (editor.fireEvent("beforeautosave", {
  22653. content: saveData
  22654. }) === false) {
  22655. return;
  22656. }
  22657. me.setPreferences(saveKey, saveData);
  22658. editor.fireEvent("afterautosave", {
  22659. content: saveData
  22660. });
  22661. }
  22662. return {
  22663. defaultOptions: {
  22664. //默认间隔时间
  22665. saveInterval: 500,
  22666. enableAutoSave: true // HaoChuan9421
  22667. },
  22668. bindEvents: {
  22669. 'ready': function () {
  22670. var _suffix = "-drafts-data",
  22671. key = null;
  22672. if (me.key) {
  22673. key = me.key + _suffix;
  22674. } else {
  22675. key = (me.container.parentNode.id || 'ue-common') + _suffix;
  22676. }
  22677. //页面地址+编辑器ID 保持唯一
  22678. saveKey = (location.protocol + location.host + location.pathname).replace(/[.:\/]/g, '_') + key;
  22679. },
  22680. 'contentchange': function () {
  22681. // HaoChuan9421
  22682. if (!me.getOpt('enableAutoSave')) {
  22683. return;
  22684. }
  22685. if (!saveKey) {
  22686. return;
  22687. }
  22688. if (me._saveFlag) {
  22689. window.clearTimeout(me._saveFlag);
  22690. }
  22691. if (me.options.saveInterval > 0) {
  22692. me._saveFlag = window.setTimeout(function () {
  22693. save(me);
  22694. }, me.options.saveInterval);
  22695. } else {
  22696. save(me);
  22697. }
  22698. }
  22699. },
  22700. commands: {
  22701. 'clearlocaldata': {
  22702. execCommand: function (cmd, name) {
  22703. if (saveKey && me.getPreferences(saveKey)) {
  22704. me.removePreferences(saveKey)
  22705. }
  22706. },
  22707. notNeedUndo: true,
  22708. ignoreContentChange: true
  22709. },
  22710. 'getlocaldata': {
  22711. execCommand: function (cmd, name) {
  22712. return saveKey ? me.getPreferences(saveKey) || '' : '';
  22713. },
  22714. notNeedUndo: true,
  22715. ignoreContentChange: true
  22716. },
  22717. 'drafts': {
  22718. execCommand: function (cmd, name) {
  22719. if (saveKey) {
  22720. me.body.innerHTML = me.getPreferences(saveKey) || '<p>' + domUtils.fillHtml + '</p>';
  22721. me.focus(true);
  22722. }
  22723. },
  22724. queryCommandState: function () {
  22725. return saveKey ? (me.getPreferences(saveKey) === null ? -1 : 0) : -1;
  22726. },
  22727. notNeedUndo: true,
  22728. ignoreContentChange: true
  22729. }
  22730. }
  22731. }
  22732. });
  22733. // plugins/charts.js
  22734. UE.plugin.register('charts', function () {
  22735. var me = this;
  22736. return {
  22737. bindEvents: {
  22738. 'chartserror': function () {}
  22739. },
  22740. commands: {
  22741. 'charts': {
  22742. execCommand: function (cmd, data) {
  22743. var tableNode = domUtils.findParentByTagName(this.selection.getRange().startContainer, 'table', true),
  22744. flagText = [],
  22745. config = {};
  22746. if (!tableNode) {
  22747. return false;
  22748. }
  22749. if (!validData(tableNode)) {
  22750. me.fireEvent("chartserror");
  22751. return false;
  22752. }
  22753. config.title = data.title || '';
  22754. config.subTitle = data.subTitle || '';
  22755. config.xTitle = data.xTitle || '';
  22756. config.yTitle = data.yTitle || '';
  22757. config.suffix = data.suffix || '';
  22758. config.tip = data.tip || '';
  22759. //数据对齐方式
  22760. config.dataFormat = data.tableDataFormat || '';
  22761. //图表类型
  22762. config.chartType = data.chartType || 0;
  22763. for (var key in config) {
  22764. if (!config.hasOwnProperty(key)) {
  22765. continue;
  22766. }
  22767. flagText.push(key + ":" + config[key]);
  22768. }
  22769. tableNode.setAttribute("data-chart", flagText.join(";"));
  22770. domUtils.addClass(tableNode, "edui-charts-table");
  22771. },
  22772. queryCommandState: function (cmd, name) {
  22773. var tableNode = domUtils.findParentByTagName(this.selection.getRange().startContainer, 'table', true);
  22774. return tableNode && validData(tableNode) ? 0 : -1;
  22775. }
  22776. }
  22777. },
  22778. inputRule: function (root) {
  22779. utils.each(root.getNodesByTagName('table'), function (tableNode) {
  22780. if (tableNode.getAttr("data-chart") !== undefined) {
  22781. tableNode.setAttr("style");
  22782. }
  22783. })
  22784. },
  22785. outputRule: function (root) {
  22786. utils.each(root.getNodesByTagName('table'), function (tableNode) {
  22787. if (tableNode.getAttr("data-chart") !== undefined) {
  22788. tableNode.setAttr("style", "display: none;");
  22789. }
  22790. })
  22791. }
  22792. }
  22793. function validData(table) {
  22794. var firstRows = null,
  22795. cellCount = 0;
  22796. //行数不够
  22797. if (table.rows.length < 2) {
  22798. return false;
  22799. }
  22800. //列数不够
  22801. if (table.rows[0].cells.length < 2) {
  22802. return false;
  22803. }
  22804. //第一行所有cell必须是th
  22805. firstRows = table.rows[0].cells;
  22806. cellCount = firstRows.length;
  22807. for (var i = 0, cell; cell = firstRows[i]; i++) {
  22808. if (cell.tagName.toLowerCase() !== 'th') {
  22809. return false;
  22810. }
  22811. }
  22812. for (var i = 1, row; row = table.rows[i]; i++) {
  22813. //每行单元格数不匹配, 返回false
  22814. if (row.cells.length != cellCount) {
  22815. return false;
  22816. }
  22817. //第一列不是th也返回false
  22818. if (row.cells[0].tagName.toLowerCase() !== 'th') {
  22819. return false;
  22820. }
  22821. for (var j = 1, cell; cell = row.cells[j]; j++) {
  22822. var value = utils.trim((cell.innerText || cell.textContent || ''));
  22823. value = value.replace(new RegExp(UE.dom.domUtils.fillChar, 'g'), '').replace(/^\s+|\s+$/g, '');
  22824. //必须是数字
  22825. if (!/^\d*\.?\d+$/.test(value)) {
  22826. return false;
  22827. }
  22828. }
  22829. }
  22830. return true;
  22831. }
  22832. });
  22833. // plugins/section.js
  22834. /**
  22835. * 目录大纲支持插件
  22836. * @file
  22837. * @since 1.3.0
  22838. */
  22839. UE.plugin.register('section', function () {
  22840. /* 目录节点对象 */
  22841. function Section(option) {
  22842. this.tag = '';
  22843. this.level = -1,
  22844. this.dom = null;
  22845. this.nextSection = null;
  22846. this.previousSection = null;
  22847. this.parentSection = null;
  22848. this.startAddress = [];
  22849. this.endAddress = [];
  22850. this.children = [];
  22851. }
  22852. function getSection(option) {
  22853. var section = new Section();
  22854. return utils.extend(section, option);
  22855. }
  22856. function getNodeFromAddress(startAddress, root) {
  22857. var current = root;
  22858. for (var i = 0; i < startAddress.length; i++) {
  22859. if (!current.childNodes) return null;
  22860. current = current.childNodes[startAddress[i]];
  22861. }
  22862. return current;
  22863. }
  22864. var me = this;
  22865. return {
  22866. bindMultiEvents: {
  22867. type: 'aftersetcontent afterscencerestore',
  22868. handler: function () {
  22869. me.fireEvent('updateSections');
  22870. }
  22871. },
  22872. bindEvents: {
  22873. /* 初始化、拖拽、粘贴、执行setcontent之后 */
  22874. 'ready': function () {
  22875. me.fireEvent('updateSections');
  22876. domUtils.on(me.body, 'drop paste', function () {
  22877. me.fireEvent('updateSections');
  22878. });
  22879. },
  22880. /* 执行paragraph命令之后 */
  22881. 'afterexeccommand': function (type, cmd) {
  22882. if (cmd == 'paragraph') {
  22883. me.fireEvent('updateSections');
  22884. }
  22885. },
  22886. /* 部分键盘操作,触发updateSections事件 */
  22887. 'keyup': function (type, e) {
  22888. var me = this,
  22889. range = me.selection.getRange();
  22890. if (range.collapsed != true) {
  22891. me.fireEvent('updateSections');
  22892. } else {
  22893. var keyCode = e.keyCode || e.which;
  22894. if (keyCode == 13 || keyCode == 8 || keyCode == 46) {
  22895. me.fireEvent('updateSections');
  22896. }
  22897. }
  22898. }
  22899. },
  22900. commands: {
  22901. 'getsections': {
  22902. execCommand: function (cmd, levels) {
  22903. var levelFn = levels || ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
  22904. for (var i = 0; i < levelFn.length; i++) {
  22905. if (typeof levelFn[i] == 'string') {
  22906. levelFn[i] = function (fn) {
  22907. return function (node) {
  22908. return node.tagName == fn.toUpperCase()
  22909. };
  22910. }(levelFn[i]);
  22911. } else if (typeof levelFn[i] != 'function') {
  22912. levelFn[i] = function (node) {
  22913. return null;
  22914. }
  22915. }
  22916. }
  22917. function getSectionLevel(node) {
  22918. for (var i = 0; i < levelFn.length; i++) {
  22919. if (levelFn[i](node)) return i;
  22920. }
  22921. return -1;
  22922. }
  22923. var me = this,
  22924. Directory = getSection({
  22925. 'level': -1,
  22926. 'title': 'root'
  22927. }),
  22928. previous = Directory;
  22929. function traversal(node, Directory) {
  22930. var level,
  22931. tmpSection = null,
  22932. parent,
  22933. child,
  22934. children = node.childNodes;
  22935. for (var i = 0, len = children.length; i < len; i++) {
  22936. child = children[i];
  22937. level = getSectionLevel(child);
  22938. if (level >= 0) {
  22939. var address = me.selection.getRange().selectNode(child).createAddress(true).startAddress,
  22940. current = getSection({
  22941. 'tag': child.tagName,
  22942. 'title': child.innerText || child.textContent || '',
  22943. 'level': level,
  22944. 'dom': child,
  22945. 'startAddress': utils.clone(address, []),
  22946. 'endAddress': utils.clone(address, []),
  22947. 'children': []
  22948. });
  22949. previous.nextSection = current;
  22950. current.previousSection = previous;
  22951. parent = previous;
  22952. while (level <= parent.level) {
  22953. parent = parent.parentSection;
  22954. }
  22955. current.parentSection = parent;
  22956. parent.children.push(current);
  22957. tmpSection = previous = current;
  22958. } else {
  22959. child.nodeType === 1 && traversal(child, Directory);
  22960. tmpSection && tmpSection.endAddress[tmpSection.endAddress.length - 1]++;
  22961. }
  22962. }
  22963. }
  22964. traversal(me.body, Directory);
  22965. return Directory;
  22966. },
  22967. notNeedUndo: true
  22968. },
  22969. 'movesection': {
  22970. execCommand: function (cmd, sourceSection, targetSection, isAfter) {
  22971. var me = this,
  22972. targetAddress,
  22973. target;
  22974. if (!sourceSection || !targetSection || targetSection.level == -1) return;
  22975. targetAddress = isAfter ? targetSection.endAddress : targetSection.startAddress;
  22976. target = getNodeFromAddress(targetAddress, me.body);
  22977. /* 判断目标地址是否被源章节包含 */
  22978. if (!targetAddress || !target || isContainsAddress(sourceSection.startAddress, sourceSection.endAddress, targetAddress)) return;
  22979. var startNode = getNodeFromAddress(sourceSection.startAddress, me.body),
  22980. endNode = getNodeFromAddress(sourceSection.endAddress, me.body),
  22981. current,
  22982. nextNode;
  22983. if (isAfter) {
  22984. current = endNode;
  22985. while (current && !(domUtils.getPosition(startNode, current) & domUtils.POSITION_FOLLOWING)) {
  22986. nextNode = current.previousSibling;
  22987. domUtils.insertAfter(target, current);
  22988. if (current == startNode) break;
  22989. current = nextNode;
  22990. }
  22991. } else {
  22992. current = startNode;
  22993. while (current && !(domUtils.getPosition(current, endNode) & domUtils.POSITION_FOLLOWING)) {
  22994. nextNode = current.nextSibling;
  22995. target.parentNode.insertBefore(current, target);
  22996. if (current == endNode) break;
  22997. current = nextNode;
  22998. }
  22999. }
  23000. me.fireEvent('updateSections');
  23001. /* 获取地址的包含关系 */
  23002. function isContainsAddress(startAddress, endAddress, addressTarget) {
  23003. var isAfterStartAddress = false,
  23004. isBeforeEndAddress = false;
  23005. for (var i = 0; i < startAddress.length; i++) {
  23006. if (i >= addressTarget.length) break;
  23007. if (addressTarget[i] > startAddress[i]) {
  23008. isAfterStartAddress = true;
  23009. break;
  23010. } else if (addressTarget[i] < startAddress[i]) {
  23011. break;
  23012. }
  23013. }
  23014. for (var i = 0; i < endAddress.length; i++) {
  23015. if (i >= addressTarget.length) break;
  23016. if (addressTarget[i] < startAddress[i]) {
  23017. isBeforeEndAddress = true;
  23018. break;
  23019. } else if (addressTarget[i] > startAddress[i]) {
  23020. break;
  23021. }
  23022. }
  23023. return isAfterStartAddress && isBeforeEndAddress;
  23024. }
  23025. }
  23026. },
  23027. 'deletesection': {
  23028. execCommand: function (cmd, section, keepChildren) {
  23029. var me = this;
  23030. if (!section) return;
  23031. function getNodeFromAddress(startAddress) {
  23032. var current = me.body;
  23033. for (var i = 0; i < startAddress.length; i++) {
  23034. if (!current.childNodes) return null;
  23035. current = current.childNodes[startAddress[i]];
  23036. }
  23037. return current;
  23038. }
  23039. var startNode = getNodeFromAddress(section.startAddress),
  23040. endNode = getNodeFromAddress(section.endAddress),
  23041. current = startNode,
  23042. nextNode;
  23043. if (!keepChildren) {
  23044. while (current && domUtils.inDoc(endNode, me.document) && !(domUtils.getPosition(current, endNode) & domUtils.POSITION_FOLLOWING)) {
  23045. nextNode = current.nextSibling;
  23046. domUtils.remove(current);
  23047. current = nextNode;
  23048. }
  23049. } else {
  23050. domUtils.remove(current);
  23051. }
  23052. me.fireEvent('updateSections');
  23053. }
  23054. },
  23055. 'selectsection': {
  23056. execCommand: function (cmd, section) {
  23057. if (!section && !section.dom) return false;
  23058. var me = this,
  23059. range = me.selection.getRange(),
  23060. address = {
  23061. 'startAddress': utils.clone(section.startAddress, []),
  23062. 'endAddress': utils.clone(section.endAddress, [])
  23063. };
  23064. address.endAddress[address.endAddress.length - 1]++;
  23065. range.moveToAddress(address).select().scrollToView();
  23066. return true;
  23067. },
  23068. notNeedUndo: true
  23069. },
  23070. 'scrolltosection': {
  23071. execCommand: function (cmd, section) {
  23072. if (!section && !section.dom) return false;
  23073. var me = this,
  23074. range = me.selection.getRange(),
  23075. address = {
  23076. 'startAddress': section.startAddress,
  23077. 'endAddress': section.endAddress
  23078. };
  23079. address.endAddress[address.endAddress.length - 1]++;
  23080. range.moveToAddress(address).scrollToView();
  23081. return true;
  23082. },
  23083. notNeedUndo: true
  23084. }
  23085. }
  23086. }
  23087. });
  23088. // plugins/simpleupload.js
  23089. /**
  23090. * @description
  23091. * 简单上传:点击按钮,直接选择文件上传。
  23092. * 原 UEditor 作者使用了 form 表单 + iframe 的方式上传
  23093. * 但由于同源策略的限制,父页面无法访问跨域的 iframe 内容
  23094. * 导致无法获取接口返回的数据,使得单图上传无法在跨域的情况下使用
  23095. * 这里改为普通的XHR上传,兼容到IE10+
  23096. * @author HaoChuan9421 <hc199421@gmail.com>
  23097. * @date 2018-12-20
  23098. */
  23099. UE.plugin.register('simpleupload', function () {
  23100. var me = this,
  23101. containerBtn,
  23102. timestrap = (+new Date()).toString(36);
  23103. function initUploadBtn() {
  23104. var w = containerBtn.offsetWidth || 20,
  23105. h = containerBtn.offsetHeight || 20,
  23106. btnStyle = 'display:block;width:' + w + 'px;height:' + h + 'px;overflow:hidden;border:0;margin:0;padding:0;position:absolute;top:0;left:0;filter:alpha(opacity=0);-moz-opacity:0;-khtml-opacity: 0;opacity: 0;cursor:pointer;';
  23107. var form = document.createElement('form');
  23108. var input = document.createElement('input');
  23109. form.id = 'edui_form_' + timestrap;
  23110. form.enctype = 'multipart/form-data';
  23111. form.style = btnStyle;
  23112. input.id = 'edui_input_' + timestrap;
  23113. input.type = 'file'
  23114. input.accept = 'image/*';
  23115. input.name = me.options.imageFieldName;
  23116. input.style = btnStyle;
  23117. form.appendChild(input);
  23118. containerBtn.appendChild(form);
  23119. input.addEventListener('change', function (event) {
  23120. if (!input.value) return;
  23121. var loadingId = 'loading_' + (+new Date()).toString(36);
  23122. var imageActionUrl = me.getActionUrl(me.getOpt('imageActionName'));
  23123. var params = utils.serializeParam(me.queryCommandValue('serverparam')) || '';
  23124. var action = utils.formatUrl(imageActionUrl + (imageActionUrl.indexOf('?') == -1 ? '?' : '&') + params);
  23125. var allowFiles = me.getOpt('imageAllowFiles');
  23126. me.focus();
  23127. me.execCommand('inserthtml', '<img class="loadingclass" id="' + loadingId + '" src="' + me.options.themePath + me.options.theme + '/images/spacer.gif" title="' + (me.getLang('simpleupload.loading') || '') + '" >');
  23128. function showErrorLoader(title) {
  23129. if (loadingId) {
  23130. var loader = me.document.getElementById(loadingId);
  23131. loader && domUtils.remove(loader);
  23132. me.fireEvent('showmessage', {
  23133. 'id': loadingId,
  23134. 'content': title,
  23135. 'type': 'error',
  23136. 'timeout': 4000
  23137. });
  23138. }
  23139. }
  23140. /* 判断后端配置是否没有加载成功 */
  23141. if (!me.getOpt('imageActionName')) {
  23142. showErrorLoader(me.getLang('autoupload.errorLoadConfig'));
  23143. return;
  23144. }
  23145. // 判断文件格式是否错误
  23146. var filename = input.value,
  23147. fileext = filename ? filename.substr(filename.lastIndexOf('.')) : '';
  23148. if (!fileext || (allowFiles && (allowFiles.join('') + '.').indexOf(fileext.toLowerCase() + '.') == -1)) {
  23149. showErrorLoader(me.getLang('simpleupload.exceedTypeError'));
  23150. return;
  23151. }
  23152. var xhr = new XMLHttpRequest()
  23153. xhr.open('post', action, true)
  23154. if (me.options.headers && Object.prototype.toString.apply(me.options.headers) === "[object Object]") {
  23155. for (var key in me.options.headers) {
  23156. xhr.setRequestHeader(key, me.options.headers[key])
  23157. }
  23158. }
  23159. xhr.onload = function () {
  23160. if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
  23161. var res = JSON.parse(xhr.responseText)
  23162. var link = me.options.imageUrlPrefix + res.url;
  23163. if (res.state == 'SUCCESS' && res.url) {
  23164. loader = me.document.getElementById(loadingId);
  23165. loader.setAttribute('src', link);
  23166. loader.setAttribute('_src', link);
  23167. loader.setAttribute('title', res.title || '');
  23168. loader.setAttribute('alt', res.original || '');
  23169. loader.removeAttribute('id');
  23170. domUtils.removeClasses(loader, 'loadingclass');
  23171. me.fireEvent("contentchange");
  23172. } else {
  23173. showErrorLoader(res.state);
  23174. }
  23175. } else {
  23176. showErrorLoader(me.getLang('simpleupload.loadError'));
  23177. }
  23178. };
  23179. xhr.onerror = function () {
  23180. showErrorLoader(me.getLang('simpleupload.loadError'));
  23181. };
  23182. xhr.send(new FormData(form));
  23183. form.reset();
  23184. })
  23185. }
  23186. return {
  23187. bindEvents: {
  23188. 'ready': function () {
  23189. //设置loading的样式
  23190. utils.cssRule('loading',
  23191. '.loadingclass{display:inline-block;cursor:default;background: url(\'' +
  23192. this.options.themePath +
  23193. this.options.theme + '/images/loading.gif\') no-repeat center center transparent;border:1px solid #cccccc;margin-right:1px;height: 22px;width: 22px;}\n' +
  23194. '.loaderrorclass{display:inline-block;cursor:default;background: url(\'' +
  23195. this.options.themePath +
  23196. this.options.theme + '/images/loaderror.png\') no-repeat center center transparent;border:1px solid #cccccc;margin-right:1px;height: 22px;width: 22px;' +
  23197. '}',
  23198. this.document);
  23199. },
  23200. /* 初始化简单上传按钮 */
  23201. 'simpleuploadbtnready': function (type, container) {
  23202. containerBtn = container;
  23203. me.afterConfigReady(initUploadBtn);
  23204. }
  23205. },
  23206. outputRule: function (root) {
  23207. utils.each(root.getNodesByTagName('img'), function (n) {
  23208. if (/\b(loaderrorclass)|(bloaderrorclass)\b/.test(n.getAttr('class'))) {
  23209. n.parentNode.removeChild(n);
  23210. }
  23211. });
  23212. }
  23213. }
  23214. });
  23215. // plugins/serverparam.js
  23216. /**
  23217. * 服务器提交的额外参数列表设置插件
  23218. * @file
  23219. * @since 1.2.6.1
  23220. */
  23221. UE.plugin.register('serverparam', function () {
  23222. var me = this,
  23223. serverParam = {};
  23224. return {
  23225. commands: {
  23226. /**
  23227. * 修改服务器提交的额外参数列表,清除所有项
  23228. * @command serverparam
  23229. * @method execCommand
  23230. * @param { String } cmd 命令字符串
  23231. * @example
  23232. * ```javascript
  23233. * editor.execCommand('serverparam');
  23234. * editor.queryCommandValue('serverparam'); //返回空
  23235. * ```
  23236. */
  23237. /**
  23238. * 修改服务器提交的额外参数列表,删除指定项
  23239. * @command serverparam
  23240. * @method execCommand
  23241. * @param { String } cmd 命令字符串
  23242. * @param { String } key 要清除的属性
  23243. * @example
  23244. * ```javascript
  23245. * editor.execCommand('serverparam', 'name'); //删除属性name
  23246. * ```
  23247. */
  23248. /**
  23249. * 修改服务器提交的额外参数列表,使用键值添加项
  23250. * @command serverparam
  23251. * @method execCommand
  23252. * @param { String } cmd 命令字符串
  23253. * @param { String } key 要添加的属性
  23254. * @param { String } value 要添加属性的值
  23255. * @example
  23256. * ```javascript
  23257. * editor.execCommand('serverparam', 'name', 'hello');
  23258. * editor.queryCommandValue('serverparam'); //返回对象 {'name': 'hello'}
  23259. * ```
  23260. */
  23261. /**
  23262. * 修改服务器提交的额外参数列表,传入键值对对象添加多项
  23263. * @command serverparam
  23264. * @method execCommand
  23265. * @param { String } cmd 命令字符串
  23266. * @param { Object } key 传入的键值对对象
  23267. * @example
  23268. * ```javascript
  23269. * editor.execCommand('serverparam', {'name': 'hello'});
  23270. * editor.queryCommandValue('serverparam'); //返回对象 {'name': 'hello'}
  23271. * ```
  23272. */
  23273. /**
  23274. * 修改服务器提交的额外参数列表,使用自定义函数添加多项
  23275. * @command serverparam
  23276. * @method execCommand
  23277. * @param { String } cmd 命令字符串
  23278. * @param { Function } key 自定义获取参数的函数
  23279. * @example
  23280. * ```javascript
  23281. * editor.execCommand('serverparam', function(editor){
  23282. * return {'key': 'value'};
  23283. * });
  23284. * editor.queryCommandValue('serverparam'); //返回对象 {'key': 'value'}
  23285. * ```
  23286. */
  23287. /**
  23288. * 获取服务器提交的额外参数列表
  23289. * @command serverparam
  23290. * @method queryCommandValue
  23291. * @param { String } cmd 命令字符串
  23292. * @example
  23293. * ```javascript
  23294. * editor.queryCommandValue( 'serverparam' ); //返回对象 {'key': 'value'}
  23295. * ```
  23296. */
  23297. 'serverparam': {
  23298. execCommand: function (cmd, key, value) {
  23299. if (key === undefined || key === null) { //不传参数,清空列表
  23300. serverParam = {};
  23301. } else if (utils.isString(key)) { //传入键值
  23302. if (value === undefined || value === null) {
  23303. delete serverParam[key];
  23304. } else {
  23305. serverParam[key] = value;
  23306. }
  23307. } else if (utils.isObject(key)) { //传入对象,覆盖列表项
  23308. utils.extend(serverParam, key, true);
  23309. } else if (utils.isFunction(key)) { //传入函数,添加列表项
  23310. utils.extend(serverParam, key(), true);
  23311. }
  23312. },
  23313. queryCommandValue: function () {
  23314. return serverParam || {};
  23315. }
  23316. }
  23317. }
  23318. }
  23319. });
  23320. // plugins/insertfile.js
  23321. /**
  23322. * 插入附件
  23323. */
  23324. UE.plugin.register('insertfile', function () {
  23325. var me = this;
  23326. function getFileIcon(url) {
  23327. var ext = url.substr(url.lastIndexOf('.') + 1).toLowerCase(),
  23328. maps = {
  23329. "rar": "icon_rar.gif",
  23330. "zip": "icon_rar.gif",
  23331. "tar": "icon_rar.gif",
  23332. "gz": "icon_rar.gif",
  23333. "bz2": "icon_rar.gif",
  23334. "doc": "icon_doc.gif",
  23335. "docx": "icon_doc.gif",
  23336. "pdf": "icon_pdf.gif",
  23337. "mp3": "icon_mp3.gif",
  23338. "xls": "icon_xls.gif",
  23339. "chm": "icon_chm.gif",
  23340. "ppt": "icon_ppt.gif",
  23341. "pptx": "icon_ppt.gif",
  23342. "avi": "icon_mv.gif",
  23343. "rmvb": "icon_mv.gif",
  23344. "wmv": "icon_mv.gif",
  23345. "flv": "icon_mv.gif",
  23346. "swf": "icon_mv.gif",
  23347. "rm": "icon_mv.gif",
  23348. "exe": "icon_exe.gif",
  23349. "psd": "icon_psd.gif",
  23350. "txt": "icon_txt.gif",
  23351. "jpg": "icon_jpg.gif",
  23352. "png": "icon_jpg.gif",
  23353. "jpeg": "icon_jpg.gif",
  23354. "gif": "icon_jpg.gif",
  23355. "ico": "icon_jpg.gif",
  23356. "bmp": "icon_jpg.gif"
  23357. };
  23358. return maps[ext] ? maps[ext] : maps['txt'];
  23359. }
  23360. return {
  23361. commands: {
  23362. 'insertfile': {
  23363. execCommand: function (command, filelist) {
  23364. filelist = utils.isArray(filelist) ? filelist : [filelist];
  23365. var i, item, icon, title,
  23366. html = '',
  23367. URL = me.getOpt('UEDITOR_HOME_URL'),
  23368. iconDir = URL + (URL.substr(URL.length - 1) == '/' ? '' : '/') + 'dialogs/attachment/fileTypeImages/';
  23369. for (i = 0; i < filelist.length; i++) {
  23370. item = filelist[i];
  23371. icon = iconDir + getFileIcon(item.url);
  23372. title = item.title || item.url.substr(item.url.lastIndexOf('/') + 1);
  23373. html += '<p style="line-height: 16px;">' +
  23374. '<img style="vertical-align: middle; margin-right: 2px;" src="' + icon + '" _src="' + icon + '" />' +
  23375. '<a style="font-size:12px; color:#0066cc;" href="' + item.url + '" title="' + title + '">' + title + '</a>' +
  23376. '</p>';
  23377. }
  23378. me.execCommand('insertHtml', html);
  23379. }
  23380. }
  23381. }
  23382. }
  23383. });
  23384. // plugins/xssFilter.js
  23385. /**
  23386. * @file xssFilter.js
  23387. * @desc xss过滤器
  23388. * @author robbenmu
  23389. */
  23390. UE.plugins.xssFilter = function () {
  23391. var config = UEDITOR_CONFIG;
  23392. var whitList = config.whitList;
  23393. function filter(node) {
  23394. var tagName = node.tagName;
  23395. var attrs = node.attrs;
  23396. if (!whitList.hasOwnProperty(tagName)) {
  23397. node.parentNode.removeChild(node);
  23398. return false;
  23399. }
  23400. UE.utils.each(attrs, function (val, key) {
  23401. if (whitList[tagName].indexOf(key) === -1) {
  23402. node.setAttr(key);
  23403. }
  23404. });
  23405. }
  23406. // 添加inserthtml\paste等操作用的过滤规则
  23407. if (whitList && config.xssFilterRules) {
  23408. this.options.filterRules = function () {
  23409. var result = {};
  23410. UE.utils.each(whitList, function (val, key) {
  23411. result[key] = function (node) {
  23412. return filter(node);
  23413. };
  23414. });
  23415. return result;
  23416. }();
  23417. }
  23418. var tagList = [];
  23419. UE.utils.each(whitList, function (val, key) {
  23420. tagList.push(key);
  23421. });
  23422. // 添加input过滤规则
  23423. //
  23424. if (whitList && config.inputXssFilter) {
  23425. this.addInputRule(function (root) {
  23426. root.traversal(function (node) {
  23427. if (node.type !== 'element') {
  23428. return false;
  23429. }
  23430. filter(node);
  23431. });
  23432. });
  23433. }
  23434. // 添加output过滤规则
  23435. //
  23436. if (whitList && config.outputXssFilter) {
  23437. this.addOutputRule(function (root) {
  23438. root.traversal(function (node) {
  23439. if (node.type !== 'element') {
  23440. return false;
  23441. }
  23442. filter(node);
  23443. });
  23444. });
  23445. }
  23446. };
  23447. // ui/ui.js
  23448. var baidu = baidu || {};
  23449. baidu.editor = baidu.editor || {};
  23450. UE.ui = baidu.editor.ui = {};
  23451. // ui/uiutils.js
  23452. (function () {
  23453. var browser = baidu.editor.browser,
  23454. domUtils = baidu.editor.dom.domUtils;
  23455. var magic = '$EDITORUI';
  23456. var root = window[magic] = {};
  23457. var uidMagic = 'ID' + magic;
  23458. var uidCount = 0;
  23459. var uiUtils = baidu.editor.ui.uiUtils = {
  23460. uid: function (obj) {
  23461. return (obj ? obj[uidMagic] || (obj[uidMagic] = ++uidCount) : ++uidCount);
  23462. },
  23463. hook: function (fn, callback) {
  23464. var dg;
  23465. if (fn && fn._callbacks) {
  23466. dg = fn;
  23467. } else {
  23468. dg = function () {
  23469. var q;
  23470. if (fn) {
  23471. q = fn.apply(this, arguments);
  23472. }
  23473. var callbacks = dg._callbacks;
  23474. var k = callbacks.length;
  23475. while (k--) {
  23476. var r = callbacks[k].apply(this, arguments);
  23477. if (q === undefined) {
  23478. q = r;
  23479. }
  23480. }
  23481. return q;
  23482. };
  23483. dg._callbacks = [];
  23484. }
  23485. dg._callbacks.push(callback);
  23486. return dg;
  23487. },
  23488. createElementByHtml: function (html) {
  23489. var el = document.createElement('div');
  23490. el.innerHTML = html;
  23491. el = el.firstChild;
  23492. el.parentNode.removeChild(el);
  23493. return el;
  23494. },
  23495. getViewportElement: function () {
  23496. return (browser.ie && browser.quirks) ?
  23497. document.body : document.documentElement;
  23498. },
  23499. getClientRect: function (element) {
  23500. var bcr;
  23501. //trace IE6下在控制编辑器显隐时可能会报错,catch一下
  23502. try {
  23503. bcr = element.getBoundingClientRect();
  23504. } catch (e) {
  23505. bcr = {
  23506. left: 0,
  23507. top: 0,
  23508. height: 0,
  23509. width: 0
  23510. }
  23511. }
  23512. var rect = {
  23513. left: Math.round(bcr.left),
  23514. top: Math.round(bcr.top),
  23515. height: Math.round(bcr.bottom - bcr.top),
  23516. width: Math.round(bcr.right - bcr.left)
  23517. };
  23518. var doc;
  23519. while ((doc = element.ownerDocument) !== document &&
  23520. (element = domUtils.getWindow(doc).frameElement)) {
  23521. bcr = element.getBoundingClientRect();
  23522. rect.left += bcr.left;
  23523. rect.top += bcr.top;
  23524. }
  23525. rect.bottom = rect.top + rect.height;
  23526. rect.right = rect.left + rect.width;
  23527. return rect;
  23528. },
  23529. getViewportRect: function () {
  23530. var viewportEl = uiUtils.getViewportElement();
  23531. var width = (window.innerWidth || viewportEl.clientWidth) | 0;
  23532. var height = (window.innerHeight || viewportEl.clientHeight) | 0;
  23533. return {
  23534. left: 0,
  23535. top: 0,
  23536. height: height,
  23537. width: width,
  23538. bottom: height,
  23539. right: width
  23540. };
  23541. },
  23542. setViewportOffset: function (element, offset) {
  23543. var rect;
  23544. var fixedLayer = uiUtils.getFixedLayer();
  23545. if (element.parentNode === fixedLayer) {
  23546. element.style.left = offset.left + 'px';
  23547. element.style.top = offset.top + 'px';
  23548. } else {
  23549. domUtils.setViewportOffset(element, offset);
  23550. }
  23551. },
  23552. getEventOffset: function (evt) {
  23553. var el = evt.target || evt.srcElement;
  23554. var rect = uiUtils.getClientRect(el);
  23555. var offset = uiUtils.getViewportOffsetByEvent(evt);
  23556. return {
  23557. left: offset.left - rect.left,
  23558. top: offset.top - rect.top
  23559. };
  23560. },
  23561. getViewportOffsetByEvent: function (evt) {
  23562. var el = evt.target || evt.srcElement;
  23563. var frameEl = domUtils.getWindow(el).frameElement;
  23564. var offset = {
  23565. left: evt.clientX,
  23566. top: evt.clientY
  23567. };
  23568. if (frameEl && el.ownerDocument !== document) {
  23569. var rect = uiUtils.getClientRect(frameEl);
  23570. offset.left += rect.left;
  23571. offset.top += rect.top;
  23572. }
  23573. return offset;
  23574. },
  23575. setGlobal: function (id, obj) {
  23576. root[id] = obj;
  23577. return magic + '["' + id + '"]';
  23578. },
  23579. unsetGlobal: function (id) {
  23580. delete root[id];
  23581. },
  23582. copyAttributes: function (tgt, src) {
  23583. var attributes = src.attributes;
  23584. var k = attributes.length;
  23585. while (k--) {
  23586. var attrNode = attributes[k];
  23587. if (attrNode.nodeName != 'style' && attrNode.nodeName != 'class' && (!browser.ie || attrNode.specified)) {
  23588. tgt.setAttribute(attrNode.nodeName, attrNode.nodeValue);
  23589. }
  23590. }
  23591. if (src.className) {
  23592. domUtils.addClass(tgt, src.className);
  23593. }
  23594. if (src.style.cssText) {
  23595. tgt.style.cssText += ';' + src.style.cssText;
  23596. }
  23597. },
  23598. removeStyle: function (el, styleName) {
  23599. if (el.style.removeProperty) {
  23600. el.style.removeProperty(styleName);
  23601. } else if (el.style.removeAttribute) {
  23602. el.style.removeAttribute(styleName);
  23603. } else throw '';
  23604. },
  23605. contains: function (elA, elB) {
  23606. return elA && elB && (elA === elB ? false : (
  23607. elA.contains ? elA.contains(elB) :
  23608. elA.compareDocumentPosition(elB) & 16
  23609. ));
  23610. },
  23611. startDrag: function (evt, callbacks, doc) {
  23612. var doc = doc || document;
  23613. var startX = evt.clientX;
  23614. var startY = evt.clientY;
  23615. function handleMouseMove(evt) {
  23616. var x = evt.clientX - startX;
  23617. var y = evt.clientY - startY;
  23618. callbacks.ondragmove(x, y, evt);
  23619. if (evt.stopPropagation) {
  23620. evt.stopPropagation();
  23621. } else {
  23622. evt.cancelBubble = true;
  23623. }
  23624. }
  23625. if (doc.addEventListener) {
  23626. function handleMouseUp(evt) {
  23627. doc.removeEventListener('mousemove', handleMouseMove, true);
  23628. doc.removeEventListener('mouseup', handleMouseUp, true);
  23629. window.removeEventListener('mouseup', handleMouseUp, true);
  23630. callbacks.ondragstop();
  23631. }
  23632. doc.addEventListener('mousemove', handleMouseMove, true);
  23633. doc.addEventListener('mouseup', handleMouseUp, true);
  23634. window.addEventListener('mouseup', handleMouseUp, true);
  23635. evt.preventDefault();
  23636. } else {
  23637. var elm = evt.srcElement;
  23638. elm.setCapture();
  23639. function releaseCaptrue() {
  23640. elm.releaseCapture();
  23641. elm.detachEvent('onmousemove', handleMouseMove);
  23642. elm.detachEvent('onmouseup', releaseCaptrue);
  23643. elm.detachEvent('onlosecaptrue', releaseCaptrue);
  23644. callbacks.ondragstop();
  23645. }
  23646. elm.attachEvent('onmousemove', handleMouseMove);
  23647. elm.attachEvent('onmouseup', releaseCaptrue);
  23648. elm.attachEvent('onlosecaptrue', releaseCaptrue);
  23649. evt.returnValue = false;
  23650. }
  23651. callbacks.ondragstart();
  23652. },
  23653. getFixedLayer: function () {
  23654. var layer = document.getElementById('edui_fixedlayer');
  23655. if (layer == null) {
  23656. layer = document.createElement('div');
  23657. layer.id = 'edui_fixedlayer';
  23658. document.body.appendChild(layer);
  23659. if (browser.ie && browser.version <= 8) {
  23660. layer.style.position = 'absolute';
  23661. bindFixedLayer();
  23662. setTimeout(updateFixedOffset);
  23663. } else {
  23664. layer.style.position = 'fixed';
  23665. }
  23666. layer.style.left = '0';
  23667. layer.style.top = '0';
  23668. layer.style.width = '0';
  23669. layer.style.height = '0';
  23670. }
  23671. return layer;
  23672. },
  23673. makeUnselectable: function (element) {
  23674. if (browser.opera || (browser.ie && browser.version < 9)) {
  23675. element.unselectable = 'on';
  23676. if (element.hasChildNodes()) {
  23677. for (var i = 0; i < element.childNodes.length; i++) {
  23678. if (element.childNodes[i].nodeType == 1) {
  23679. uiUtils.makeUnselectable(element.childNodes[i]);
  23680. }
  23681. }
  23682. }
  23683. } else {
  23684. if (element.style.MozUserSelect !== undefined) {
  23685. element.style.MozUserSelect = 'none';
  23686. } else if (element.style.WebkitUserSelect !== undefined) {
  23687. element.style.WebkitUserSelect = 'none';
  23688. } else if (element.style.KhtmlUserSelect !== undefined) {
  23689. element.style.KhtmlUserSelect = 'none';
  23690. }
  23691. }
  23692. }
  23693. };
  23694. function updateFixedOffset() {
  23695. var layer = document.getElementById('edui_fixedlayer');
  23696. uiUtils.setViewportOffset(layer, {
  23697. left: 0,
  23698. top: 0
  23699. });
  23700. // layer.style.display = 'none';
  23701. // layer.style.display = 'block';
  23702. //#trace: 1354
  23703. // setTimeout(updateFixedOffset);
  23704. }
  23705. function bindFixedLayer(adjOffset) {
  23706. domUtils.on(window, 'scroll', updateFixedOffset);
  23707. domUtils.on(window, 'resize', baidu.editor.utils.defer(updateFixedOffset, 0, true));
  23708. }
  23709. })();
  23710. // ui/uibase.js
  23711. (function () {
  23712. var utils = baidu.editor.utils,
  23713. uiUtils = baidu.editor.ui.uiUtils,
  23714. EventBase = baidu.editor.EventBase,
  23715. UIBase = baidu.editor.ui.UIBase = function () {};
  23716. UIBase.prototype = {
  23717. className: '',
  23718. uiName: '',
  23719. initOptions: function (options) {
  23720. var me = this;
  23721. for (var k in options) {
  23722. me[k] = options[k];
  23723. }
  23724. this.id = this.id || 'edui' + uiUtils.uid();
  23725. },
  23726. initUIBase: function () {
  23727. this._globalKey = utils.unhtml(uiUtils.setGlobal(this.id, this));
  23728. },
  23729. render: function (holder) {
  23730. var html = this.renderHtml();
  23731. var el = uiUtils.createElementByHtml(html);
  23732. //by xuheng 给每个node添加class
  23733. var list = domUtils.getElementsByTagName(el, "*");
  23734. var theme = "edui-" + (this.theme || this.editor.options.theme);
  23735. var layer = document.getElementById('edui_fixedlayer');
  23736. for (var i = 0, node; node = list[i++];) {
  23737. domUtils.addClass(node, theme);
  23738. }
  23739. domUtils.addClass(el, theme);
  23740. if (layer) {
  23741. layer.className = "";
  23742. domUtils.addClass(layer, theme);
  23743. }
  23744. var seatEl = this.getDom();
  23745. if (seatEl != null) {
  23746. seatEl.parentNode.replaceChild(el, seatEl);
  23747. uiUtils.copyAttributes(el, seatEl);
  23748. } else {
  23749. if (typeof holder == 'string') {
  23750. holder = document.getElementById(holder);
  23751. }
  23752. holder = holder || uiUtils.getFixedLayer();
  23753. domUtils.addClass(holder, theme);
  23754. holder.appendChild(el);
  23755. }
  23756. this.postRender();
  23757. },
  23758. getDom: function (name) {
  23759. if (!name) {
  23760. return document.getElementById(this.id);
  23761. } else {
  23762. return document.getElementById(this.id + '_' + name);
  23763. }
  23764. },
  23765. postRender: function () {
  23766. this.fireEvent('postrender');
  23767. },
  23768. getHtmlTpl: function () {
  23769. return '';
  23770. },
  23771. formatHtml: function (tpl) {
  23772. var prefix = 'edui-' + this.uiName;
  23773. return (tpl
  23774. .replace(/##/g, this.id)
  23775. .replace(/%%-/g, this.uiName ? prefix + '-' : '')
  23776. .replace(/%%/g, (this.uiName ? prefix : '') + ' ' + this.className)
  23777. .replace(/\$\$/g, this._globalKey));
  23778. },
  23779. renderHtml: function () {
  23780. return this.formatHtml(this.getHtmlTpl());
  23781. },
  23782. dispose: function () {
  23783. var box = this.getDom();
  23784. if (box) baidu.editor.dom.domUtils.remove(box);
  23785. uiUtils.unsetGlobal(this.id);
  23786. }
  23787. };
  23788. utils.inherits(UIBase, EventBase);
  23789. })();
  23790. // ui/separator.js
  23791. (function () {
  23792. var utils = baidu.editor.utils,
  23793. UIBase = baidu.editor.ui.UIBase,
  23794. Separator = baidu.editor.ui.Separator = function (options) {
  23795. this.initOptions(options);
  23796. this.initSeparator();
  23797. };
  23798. Separator.prototype = {
  23799. uiName: 'separator',
  23800. initSeparator: function () {
  23801. this.initUIBase();
  23802. },
  23803. getHtmlTpl: function () {
  23804. return '<div id="##" class="edui-box %%"></div>';
  23805. }
  23806. };
  23807. utils.inherits(Separator, UIBase);
  23808. })();
  23809. // ui/mask.js
  23810. ///import core
  23811. ///import uicore
  23812. (function () {
  23813. var utils = baidu.editor.utils,
  23814. domUtils = baidu.editor.dom.domUtils,
  23815. UIBase = baidu.editor.ui.UIBase,
  23816. uiUtils = baidu.editor.ui.uiUtils;
  23817. var Mask = baidu.editor.ui.Mask = function (options) {
  23818. this.initOptions(options);
  23819. this.initUIBase();
  23820. };
  23821. Mask.prototype = {
  23822. getHtmlTpl: function () {
  23823. return '<div id="##" class="edui-mask %%" onclick="return $$._onClick(event, this);" onmousedown="return $$._onMouseDown(event, this);"></div>';
  23824. },
  23825. postRender: function () {
  23826. var me = this;
  23827. domUtils.on(window, 'resize', function () {
  23828. setTimeout(function () {
  23829. if (!me.isHidden()) {
  23830. me._fill();
  23831. }
  23832. });
  23833. });
  23834. },
  23835. show: function (zIndex) {
  23836. this._fill();
  23837. this.getDom().style.display = '';
  23838. this.getDom().style.zIndex = zIndex;
  23839. },
  23840. hide: function () {
  23841. this.getDom().style.display = 'none';
  23842. this.getDom().style.zIndex = '';
  23843. },
  23844. isHidden: function () {
  23845. return this.getDom().style.display == 'none';
  23846. },
  23847. _onMouseDown: function () {
  23848. return false;
  23849. },
  23850. _onClick: function (e, target) {
  23851. this.fireEvent('click', e, target);
  23852. },
  23853. _fill: function () {
  23854. var el = this.getDom();
  23855. var vpRect = uiUtils.getViewportRect();
  23856. el.style.width = vpRect.width + 'px';
  23857. el.style.height = vpRect.height + 'px';
  23858. }
  23859. };
  23860. utils.inherits(Mask, UIBase);
  23861. })();
  23862. // ui/popup.js
  23863. ///import core
  23864. ///import uicore
  23865. (function () {
  23866. var utils = baidu.editor.utils,
  23867. uiUtils = baidu.editor.ui.uiUtils,
  23868. domUtils = baidu.editor.dom.domUtils,
  23869. UIBase = baidu.editor.ui.UIBase,
  23870. Popup = baidu.editor.ui.Popup = function (options) {
  23871. this.initOptions(options);
  23872. this.initPopup();
  23873. };
  23874. var allPopups = [];
  23875. function closeAllPopup(evt, el) {
  23876. for (var i = 0; i < allPopups.length; i++) {
  23877. var pop = allPopups[i];
  23878. if (!pop.isHidden()) {
  23879. if (pop.queryAutoHide(el) !== false) {
  23880. if (evt && /scroll/ig.test(evt.type) && pop.className == "edui-wordpastepop") return;
  23881. pop.hide();
  23882. }
  23883. }
  23884. }
  23885. if (allPopups.length)
  23886. pop.editor.fireEvent("afterhidepop");
  23887. }
  23888. Popup.postHide = closeAllPopup;
  23889. var ANCHOR_CLASSES = ['edui-anchor-topleft', 'edui-anchor-topright',
  23890. 'edui-anchor-bottomleft', 'edui-anchor-bottomright'
  23891. ];
  23892. Popup.prototype = {
  23893. SHADOW_RADIUS: 5,
  23894. content: null,
  23895. _hidden: false,
  23896. autoRender: true,
  23897. canSideLeft: true,
  23898. canSideUp: true,
  23899. initPopup: function () {
  23900. this.initUIBase();
  23901. allPopups.push(this);
  23902. },
  23903. getHtmlTpl: function () {
  23904. return '<div id="##" class="edui-popup %%" onmousedown="return false;">' +
  23905. ' <div id="##_body" class="edui-popup-body">' +
  23906. ' <iframe style="position:absolute;z-index:-1;left:0;top:0;background-color: transparent;" frameborder="0" width="100%" height="100%" src="about:blank"></iframe>' +
  23907. ' <div class="edui-shadow"></div>' +
  23908. ' <div id="##_content" class="edui-popup-content">' +
  23909. this.getContentHtmlTpl() +
  23910. ' </div>' +
  23911. ' </div>' +
  23912. '</div>';
  23913. },
  23914. getContentHtmlTpl: function () {
  23915. if (this.content) {
  23916. if (typeof this.content == 'string') {
  23917. return this.content;
  23918. }
  23919. return this.content.renderHtml();
  23920. } else {
  23921. return ''
  23922. }
  23923. },
  23924. _UIBase_postRender: UIBase.prototype.postRender,
  23925. postRender: function () {
  23926. if (this.content instanceof UIBase) {
  23927. this.content.postRender();
  23928. }
  23929. //捕获鼠标滚轮
  23930. if (this.captureWheel && !this.captured) {
  23931. this.captured = true;
  23932. var winHeight = (document.documentElement.clientHeight || document.body.clientHeight) - 80,
  23933. _height = this.getDom().offsetHeight,
  23934. _top = uiUtils.getClientRect(this.combox.getDom()).top,
  23935. content = this.getDom('content'),
  23936. ifr = this.getDom('body').getElementsByTagName('iframe'),
  23937. me = this;
  23938. ifr.length && (ifr = ifr[0]);
  23939. while (_top + _height > winHeight) {
  23940. _height -= 30;
  23941. }
  23942. content.style.height = _height + 'px';
  23943. //同步更改iframe高度
  23944. ifr && (ifr.style.height = _height + 'px');
  23945. //阻止在combox上的鼠标滚轮事件, 防止用户的正常操作被误解
  23946. if (window.XMLHttpRequest) {
  23947. domUtils.on(content, ('onmousewheel' in document.body) ? 'mousewheel' : 'DOMMouseScroll', function (e) {
  23948. if (e.preventDefault) {
  23949. e.preventDefault();
  23950. } else {
  23951. e.returnValue = false;
  23952. }
  23953. if (e.wheelDelta) {
  23954. content.scrollTop -= (e.wheelDelta / 120) * 60;
  23955. } else {
  23956. content.scrollTop -= (e.detail / -3) * 60;
  23957. }
  23958. });
  23959. } else {
  23960. //ie6
  23961. domUtils.on(this.getDom(), 'mousewheel', function (e) {
  23962. e.returnValue = false;
  23963. me.getDom('content').scrollTop -= (e.wheelDelta / 120) * 60;
  23964. });
  23965. }
  23966. }
  23967. this.fireEvent('postRenderAfter');
  23968. this.hide(true);
  23969. this._UIBase_postRender();
  23970. },
  23971. _doAutoRender: function () {
  23972. if (!this.getDom() && this.autoRender) {
  23973. this.render();
  23974. }
  23975. },
  23976. mesureSize: function () {
  23977. var box = this.getDom('content');
  23978. return uiUtils.getClientRect(box);
  23979. },
  23980. fitSize: function () {
  23981. if (this.captureWheel && this.sized) {
  23982. return this.__size;
  23983. }
  23984. this.sized = true;
  23985. var popBodyEl = this.getDom('body');
  23986. popBodyEl.style.width = '';
  23987. popBodyEl.style.height = '';
  23988. var size = this.mesureSize();
  23989. if (this.captureWheel) {
  23990. popBodyEl.style.width = -(-20 - size.width) + 'px';
  23991. var height = parseInt(this.getDom('content').style.height, 10);
  23992. !window.isNaN(height) && (size.height = height);
  23993. } else {
  23994. popBodyEl.style.width = size.width + 'px';
  23995. }
  23996. popBodyEl.style.height = size.height + 'px';
  23997. this.__size = size;
  23998. this.captureWheel && (this.getDom('content').style.overflow = 'auto');
  23999. return size;
  24000. },
  24001. showAnchor: function (element, hoz) {
  24002. this.showAnchorRect(uiUtils.getClientRect(element), hoz);
  24003. },
  24004. showAnchorRect: function (rect, hoz, adj) {
  24005. this._doAutoRender();
  24006. var vpRect = uiUtils.getViewportRect();
  24007. this.getDom().style.visibility = 'hidden';
  24008. this._show();
  24009. var popSize = this.fitSize();
  24010. var sideLeft, sideUp, left, top;
  24011. if (hoz) {
  24012. sideLeft = this.canSideLeft && (rect.right + popSize.width > vpRect.right && rect.left > popSize.width);
  24013. sideUp = this.canSideUp && (rect.top + popSize.height > vpRect.bottom && rect.bottom > popSize.height);
  24014. left = (sideLeft ? rect.left - popSize.width : rect.right);
  24015. top = (sideUp ? rect.bottom - popSize.height : rect.top);
  24016. } else {
  24017. sideLeft = this.canSideLeft && (rect.right + popSize.width > vpRect.right && rect.left > popSize.width);
  24018. sideUp = this.canSideUp && (rect.top + popSize.height > vpRect.bottom && rect.bottom > popSize.height);
  24019. left = (sideLeft ? rect.right - popSize.width : rect.left);
  24020. top = (sideUp ? rect.top - popSize.height : rect.bottom);
  24021. }
  24022. var popEl = this.getDom();
  24023. uiUtils.setViewportOffset(popEl, {
  24024. left: left,
  24025. top: top
  24026. });
  24027. domUtils.removeClasses(popEl, ANCHOR_CLASSES);
  24028. popEl.className += ' ' + ANCHOR_CLASSES[(sideUp ? 1 : 0) * 2 + (sideLeft ? 1 : 0)];
  24029. if (this.editor) {
  24030. popEl.style.zIndex = this.editor.container.style.zIndex * 1 + 10;
  24031. baidu.editor.ui.uiUtils.getFixedLayer().style.zIndex = popEl.style.zIndex - 1;
  24032. }
  24033. this.getDom().style.visibility = 'visible';
  24034. },
  24035. showAt: function (offset) {
  24036. var left = offset.left;
  24037. var top = offset.top;
  24038. var rect = {
  24039. left: left,
  24040. top: top,
  24041. right: left,
  24042. bottom: top,
  24043. height: 0,
  24044. width: 0
  24045. };
  24046. this.showAnchorRect(rect, false, true);
  24047. },
  24048. _show: function () {
  24049. if (this._hidden) {
  24050. var box = this.getDom();
  24051. box.style.display = '';
  24052. this._hidden = false;
  24053. // if (box.setActive) {
  24054. // box.setActive();
  24055. // }
  24056. this.fireEvent('show');
  24057. }
  24058. },
  24059. isHidden: function () {
  24060. return this._hidden;
  24061. },
  24062. show: function () {
  24063. this._doAutoRender();
  24064. this._show();
  24065. },
  24066. hide: function (notNofity) {
  24067. if (!this._hidden && this.getDom()) {
  24068. this.getDom().style.display = 'none';
  24069. this._hidden = true;
  24070. if (!notNofity) {
  24071. this.fireEvent('hide');
  24072. }
  24073. }
  24074. },
  24075. queryAutoHide: function (el) {
  24076. return !el || !uiUtils.contains(this.getDom(), el);
  24077. }
  24078. };
  24079. utils.inherits(Popup, UIBase);
  24080. domUtils.on(document, 'mousedown', function (evt) {
  24081. var el = evt.target || evt.srcElement;
  24082. closeAllPopup(evt, el);
  24083. });
  24084. domUtils.on(window, 'scroll', function (evt, el) {
  24085. closeAllPopup(evt, el);
  24086. });
  24087. })();
  24088. // ui/colorpicker.js
  24089. ///import core
  24090. ///import uicore
  24091. (function () {
  24092. var utils = baidu.editor.utils,
  24093. UIBase = baidu.editor.ui.UIBase,
  24094. ColorPicker = baidu.editor.ui.ColorPicker = function (options) {
  24095. this.initOptions(options);
  24096. this.noColorText = this.noColorText || this.editor.getLang("clearColor");
  24097. this.initUIBase();
  24098. };
  24099. ColorPicker.prototype = {
  24100. getHtmlTpl: function () {
  24101. return genColorPicker(this.noColorText, this.editor);
  24102. },
  24103. _onTableClick: function (evt) {
  24104. var tgt = evt.target || evt.srcElement;
  24105. var color = tgt.getAttribute('data-color');
  24106. if (color) {
  24107. this.fireEvent('pickcolor', color);
  24108. }
  24109. },
  24110. _onTableOver: function (evt) {
  24111. var tgt = evt.target || evt.srcElement;
  24112. var color = tgt.getAttribute('data-color');
  24113. if (color) {
  24114. this.getDom('preview').style.backgroundColor = color;
  24115. }
  24116. },
  24117. _onTableOut: function () {
  24118. this.getDom('preview').style.backgroundColor = '';
  24119. },
  24120. _onPickNoColor: function () {
  24121. this.fireEvent('picknocolor');
  24122. }
  24123. };
  24124. utils.inherits(ColorPicker, UIBase);
  24125. var COLORS = (
  24126. 'ffffff,000000,eeece1,1f497d,4f81bd,c0504d,9bbb59,8064a2,4bacc6,f79646,' +
  24127. 'f2f2f2,7f7f7f,ddd9c3,c6d9f0,dbe5f1,f2dcdb,ebf1dd,e5e0ec,dbeef3,fdeada,' +
  24128. 'd8d8d8,595959,c4bd97,8db3e2,b8cce4,e5b9b7,d7e3bc,ccc1d9,b7dde8,fbd5b5,' +
  24129. 'bfbfbf,3f3f3f,938953,548dd4,95b3d7,d99694,c3d69b,b2a2c7,92cddc,fac08f,' +
  24130. 'a5a5a5,262626,494429,17365d,366092,953734,76923c,5f497a,31859b,e36c09,' +
  24131. '7f7f7f,0c0c0c,1d1b10,0f243e,244061,632423,4f6128,3f3151,205867,974806,' +
  24132. 'c00000,ff0000,ffc000,ffff00,92d050,00b050,00b0f0,0070c0,002060,7030a0,').split(',');
  24133. function genColorPicker(noColorText, editor) {
  24134. var html = '<div id="##" class="edui-colorpicker %%">' +
  24135. '<div class="edui-colorpicker-topbar edui-clearfix">' +
  24136. '<div unselectable="on" id="##_preview" class="edui-colorpicker-preview"></div>' +
  24137. '<div unselectable="on" class="edui-colorpicker-nocolor" onclick="$$._onPickNoColor(event, this);">' + noColorText + '</div>' +
  24138. '</div>' +
  24139. '<table class="edui-box" style="border-collapse: collapse;" onmouseover="$$._onTableOver(event, this);" onmouseout="$$._onTableOut(event, this);" onclick="return $$._onTableClick(event, this);" cellspacing="0" cellpadding="0">' +
  24140. '<tr style="border-bottom: 1px solid #ddd;font-size: 13px;line-height: 25px;color:#39C;padding-top: 2px"><td colspan="10">' + editor.getLang("themeColor") + '</td> </tr>' +
  24141. '<tr class="edui-colorpicker-tablefirstrow" >';
  24142. for (var i = 0; i < COLORS.length; i++) {
  24143. if (i && i % 10 === 0) {
  24144. html += '</tr>' + (i == 60 ? '<tr style="border-bottom: 1px solid #ddd;font-size: 13px;line-height: 25px;color:#39C;"><td colspan="10">' + editor.getLang("standardColor") + '</td></tr>' : '') + '<tr' + (i == 60 ? ' class="edui-colorpicker-tablefirstrow"' : '') + '>';
  24145. }
  24146. html += i < 70 ? '<td style="padding: 0 2px;"><a hidefocus title="' + COLORS[i] + '" onclick="return false;" href="javascript:" unselectable="on" class="edui-box edui-colorpicker-colorcell"' +
  24147. ' data-color="#' + COLORS[i] + '"' +
  24148. ' style="background-color:#' + COLORS[i] + ';border:solid #ccc;' +
  24149. (i < 10 || i >= 60 ? 'border-width:1px;' :
  24150. i >= 10 && i < 20 ? 'border-width:1px 1px 0 1px;' :
  24151. 'border-width:0 1px 0 1px;') +
  24152. '"' +
  24153. '></a></td>' : '';
  24154. }
  24155. html += '</tr></table></div>';
  24156. return html;
  24157. }
  24158. })();
  24159. // ui/tablepicker.js
  24160. ///import core
  24161. ///import uicore
  24162. (function () {
  24163. var utils = baidu.editor.utils,
  24164. uiUtils = baidu.editor.ui.uiUtils,
  24165. UIBase = baidu.editor.ui.UIBase;
  24166. var TablePicker = baidu.editor.ui.TablePicker = function (options) {
  24167. this.initOptions(options);
  24168. this.initTablePicker();
  24169. };
  24170. TablePicker.prototype = {
  24171. defaultNumRows: 10,
  24172. defaultNumCols: 10,
  24173. maxNumRows: 20,
  24174. maxNumCols: 20,
  24175. numRows: 10,
  24176. numCols: 10,
  24177. lengthOfCellSide: 22,
  24178. initTablePicker: function () {
  24179. this.initUIBase();
  24180. },
  24181. getHtmlTpl: function () {
  24182. var me = this;
  24183. return '<div id="##" class="edui-tablepicker %%">' +
  24184. '<div class="edui-tablepicker-body">' +
  24185. '<div class="edui-infoarea">' +
  24186. '<span id="##_label" class="edui-label"></span>' +
  24187. '</div>' +
  24188. '<div class="edui-pickarea"' +
  24189. ' onmousemove="$$._onMouseMove(event, this);"' +
  24190. ' onmouseover="$$._onMouseOver(event, this);"' +
  24191. ' onmouseout="$$._onMouseOut(event, this);"' +
  24192. ' onclick="$$._onClick(event, this);"' +
  24193. '>' +
  24194. '<div id="##_overlay" class="edui-overlay"></div>' +
  24195. '</div>' +
  24196. '</div>' +
  24197. '</div>';
  24198. },
  24199. _UIBase_render: UIBase.prototype.render,
  24200. render: function (holder) {
  24201. this._UIBase_render(holder);
  24202. this.getDom('label').innerHTML = '0' + this.editor.getLang("t_row") + ' x 0' + this.editor.getLang("t_col");
  24203. },
  24204. _track: function (numCols, numRows) {
  24205. var style = this.getDom('overlay').style;
  24206. var sideLen = this.lengthOfCellSide;
  24207. style.width = numCols * sideLen + 'px';
  24208. style.height = numRows * sideLen + 'px';
  24209. var label = this.getDom('label');
  24210. label.innerHTML = numCols + this.editor.getLang("t_col") + ' x ' + numRows + this.editor.getLang("t_row");
  24211. this.numCols = numCols;
  24212. this.numRows = numRows;
  24213. },
  24214. _onMouseOver: function (evt, el) {
  24215. var rel = evt.relatedTarget || evt.fromElement;
  24216. if (!uiUtils.contains(el, rel) && el !== rel) {
  24217. this.getDom('label').innerHTML = '0' + this.editor.getLang("t_col") + ' x 0' + this.editor.getLang("t_row");
  24218. this.getDom('overlay').style.visibility = '';
  24219. }
  24220. },
  24221. _onMouseOut: function (evt, el) {
  24222. var rel = evt.relatedTarget || evt.toElement;
  24223. if (!uiUtils.contains(el, rel) && el !== rel) {
  24224. this.getDom('label').innerHTML = '0' + this.editor.getLang("t_col") + ' x 0' + this.editor.getLang("t_row");
  24225. this.getDom('overlay').style.visibility = 'hidden';
  24226. }
  24227. },
  24228. _onMouseMove: function (evt, el) {
  24229. var style = this.getDom('overlay').style;
  24230. var offset = uiUtils.getEventOffset(evt);
  24231. var sideLen = this.lengthOfCellSide;
  24232. var numCols = Math.ceil(offset.left / sideLen);
  24233. var numRows = Math.ceil(offset.top / sideLen);
  24234. this._track(numCols, numRows);
  24235. },
  24236. _onClick: function () {
  24237. this.fireEvent('picktable', this.numCols, this.numRows);
  24238. }
  24239. };
  24240. utils.inherits(TablePicker, UIBase);
  24241. })();
  24242. // ui/stateful.js
  24243. (function () {
  24244. var browser = baidu.editor.browser,
  24245. domUtils = baidu.editor.dom.domUtils,
  24246. uiUtils = baidu.editor.ui.uiUtils;
  24247. var TPL_STATEFUL = 'onmousedown="$$.Stateful_onMouseDown(event, this);"' +
  24248. ' onmouseup="$$.Stateful_onMouseUp(event, this);"' +
  24249. (browser.ie ? (
  24250. ' onmouseenter="$$.Stateful_onMouseEnter(event, this);"' +
  24251. ' onmouseleave="$$.Stateful_onMouseLeave(event, this);"') :
  24252. (
  24253. ' onmouseover="$$.Stateful_onMouseOver(event, this);"' +
  24254. ' onmouseout="$$.Stateful_onMouseOut(event, this);"'));
  24255. baidu.editor.ui.Stateful = {
  24256. alwalysHoverable: false,
  24257. target: null, //目标元素和this指向dom不一样
  24258. Stateful_init: function () {
  24259. this._Stateful_dGetHtmlTpl = this.getHtmlTpl;
  24260. this.getHtmlTpl = this.Stateful_getHtmlTpl;
  24261. },
  24262. Stateful_getHtmlTpl: function () {
  24263. var tpl = this._Stateful_dGetHtmlTpl();
  24264. // 使用function避免$转义
  24265. return tpl.replace(/stateful/g, function () {
  24266. return TPL_STATEFUL;
  24267. });
  24268. },
  24269. Stateful_onMouseEnter: function (evt, el) {
  24270. this.target = el;
  24271. if (!this.isDisabled() || this.alwalysHoverable) {
  24272. this.addState('hover');
  24273. this.fireEvent('over');
  24274. }
  24275. },
  24276. Stateful_onMouseLeave: function (evt, el) {
  24277. if (!this.isDisabled() || this.alwalysHoverable) {
  24278. this.removeState('hover');
  24279. this.removeState('active');
  24280. this.fireEvent('out');
  24281. }
  24282. },
  24283. Stateful_onMouseOver: function (evt, el) {
  24284. var rel = evt.relatedTarget;
  24285. if (!uiUtils.contains(el, rel) && el !== rel) {
  24286. this.Stateful_onMouseEnter(evt, el);
  24287. }
  24288. },
  24289. Stateful_onMouseOut: function (evt, el) {
  24290. var rel = evt.relatedTarget;
  24291. if (!uiUtils.contains(el, rel) && el !== rel) {
  24292. this.Stateful_onMouseLeave(evt, el);
  24293. }
  24294. },
  24295. Stateful_onMouseDown: function (evt, el) {
  24296. if (!this.isDisabled()) {
  24297. this.addState('active');
  24298. }
  24299. },
  24300. Stateful_onMouseUp: function (evt, el) {
  24301. if (!this.isDisabled()) {
  24302. this.removeState('active');
  24303. }
  24304. },
  24305. Stateful_postRender: function () {
  24306. if (this.disabled && !this.hasState('disabled')) {
  24307. this.addState('disabled');
  24308. }
  24309. },
  24310. hasState: function (state) {
  24311. return domUtils.hasClass(this.getStateDom(), 'edui-state-' + state);
  24312. },
  24313. addState: function (state) {
  24314. if (!this.hasState(state)) {
  24315. this.getStateDom().className += ' edui-state-' + state;
  24316. }
  24317. },
  24318. removeState: function (state) {
  24319. if (this.hasState(state)) {
  24320. domUtils.removeClasses(this.getStateDom(), ['edui-state-' + state]);
  24321. }
  24322. },
  24323. getStateDom: function () {
  24324. return this.getDom('state');
  24325. },
  24326. isChecked: function () {
  24327. return this.hasState('checked');
  24328. },
  24329. setChecked: function (checked) {
  24330. if (!this.isDisabled() && checked) {
  24331. this.addState('checked');
  24332. } else {
  24333. this.removeState('checked');
  24334. }
  24335. },
  24336. isDisabled: function () {
  24337. return this.hasState('disabled');
  24338. },
  24339. setDisabled: function (disabled) {
  24340. if (disabled) {
  24341. this.removeState('hover');
  24342. this.removeState('checked');
  24343. this.removeState('active');
  24344. this.addState('disabled');
  24345. } else {
  24346. this.removeState('disabled');
  24347. }
  24348. }
  24349. };
  24350. })();
  24351. // ui/button.js
  24352. ///import core
  24353. ///import uicore
  24354. ///import ui/stateful.js
  24355. (function () {
  24356. var utils = baidu.editor.utils,
  24357. UIBase = baidu.editor.ui.UIBase,
  24358. Stateful = baidu.editor.ui.Stateful,
  24359. Button = baidu.editor.ui.Button = function (options) {
  24360. if (options.name) {
  24361. var btnName = options.name;
  24362. var cssRules = options.cssRules;
  24363. if (!options.className) {
  24364. options.className = 'edui-for-' + btnName;
  24365. }
  24366. options.cssRules = '.edui-default .edui-for-' + btnName + ' .edui-icon {' + cssRules + '}'
  24367. }
  24368. this.initOptions(options);
  24369. this.initButton();
  24370. };
  24371. Button.prototype = {
  24372. uiName: 'button',
  24373. label: '',
  24374. title: '',
  24375. showIcon: true,
  24376. showText: true,
  24377. cssRules: '',
  24378. initButton: function () {
  24379. this.initUIBase();
  24380. this.Stateful_init();
  24381. if (this.cssRules) {
  24382. utils.cssRule('edui-customize-' + this.name + '-style', this.cssRules);
  24383. }
  24384. },
  24385. getHtmlTpl: function () {
  24386. return '<div id="##" class="edui-box %%">' +
  24387. '<div id="##_state" stateful>' +
  24388. '<div class="%%-wrap"><div id="##_body" unselectable="on" ' + (this.title ? 'title="' + this.title + '"' : '') +
  24389. ' class="%%-body" onmousedown="return $$._onMouseDown(event, this);" onclick="return $$._onClick(event, this);">' +
  24390. (this.showIcon ? '<div class="edui-box edui-icon"></div>' : '') +
  24391. (this.showText ? '<div class="edui-box edui-label">' + this.label + '</div>' : '') +
  24392. '</div>' +
  24393. '</div>' +
  24394. '</div></div>';
  24395. },
  24396. postRender: function () {
  24397. this.Stateful_postRender();
  24398. this.setDisabled(this.disabled)
  24399. },
  24400. _onMouseDown: function (e) {
  24401. var target = e.target || e.srcElement,
  24402. tagName = target && target.tagName && target.tagName.toLowerCase();
  24403. if (tagName == 'input' || tagName == 'object' || tagName == 'object') {
  24404. return false;
  24405. }
  24406. },
  24407. _onClick: function () {
  24408. if (!this.isDisabled()) {
  24409. this.fireEvent('click');
  24410. }
  24411. },
  24412. setTitle: function (text) {
  24413. var label = this.getDom('label');
  24414. label.innerHTML = text;
  24415. }
  24416. };
  24417. utils.inherits(Button, UIBase);
  24418. utils.extend(Button.prototype, Stateful);
  24419. })();
  24420. // ui/splitbutton.js
  24421. ///import core
  24422. ///import uicore
  24423. ///import ui/stateful.js
  24424. (function () {
  24425. var utils = baidu.editor.utils,
  24426. uiUtils = baidu.editor.ui.uiUtils,
  24427. domUtils = baidu.editor.dom.domUtils,
  24428. UIBase = baidu.editor.ui.UIBase,
  24429. Stateful = baidu.editor.ui.Stateful,
  24430. SplitButton = baidu.editor.ui.SplitButton = function (options) {
  24431. this.initOptions(options);
  24432. this.initSplitButton();
  24433. };
  24434. SplitButton.prototype = {
  24435. popup: null,
  24436. uiName: 'splitbutton',
  24437. title: '',
  24438. initSplitButton: function () {
  24439. this.initUIBase();
  24440. this.Stateful_init();
  24441. var me = this;
  24442. if (this.popup != null) {
  24443. var popup = this.popup;
  24444. this.popup = null;
  24445. this.setPopup(popup);
  24446. }
  24447. },
  24448. _UIBase_postRender: UIBase.prototype.postRender,
  24449. postRender: function () {
  24450. this.Stateful_postRender();
  24451. this._UIBase_postRender();
  24452. },
  24453. setPopup: function (popup) {
  24454. if (this.popup === popup) return;
  24455. if (this.popup != null) {
  24456. this.popup.dispose();
  24457. }
  24458. popup.addListener('show', utils.bind(this._onPopupShow, this));
  24459. popup.addListener('hide', utils.bind(this._onPopupHide, this));
  24460. popup.addListener('postrender', utils.bind(function () {
  24461. popup.getDom('body').appendChild(
  24462. uiUtils.createElementByHtml('<div id="' +
  24463. this.popup.id + '_bordereraser" class="edui-bordereraser edui-background" style="width:' +
  24464. (uiUtils.getClientRect(this.getDom()).width + 20) + 'px"></div>')
  24465. );
  24466. popup.getDom().className += ' ' + this.className;
  24467. }, this));
  24468. this.popup = popup;
  24469. },
  24470. _onPopupShow: function () {
  24471. this.addState('opened');
  24472. },
  24473. _onPopupHide: function () {
  24474. this.removeState('opened');
  24475. },
  24476. getHtmlTpl: function () {
  24477. return '<div id="##" class="edui-box %%">' +
  24478. '<div ' + (this.title ? 'title="' + this.title + '"' : '') + ' id="##_state" stateful><div class="%%-body">' +
  24479. '<div id="##_button_body" class="edui-box edui-button-body" onclick="$$._onButtonClick(event, this);">' +
  24480. '<div class="edui-box edui-icon"></div>' +
  24481. '</div>' +
  24482. '<div class="edui-box edui-splitborder"></div>' +
  24483. '<div class="edui-box edui-arrow" onclick="$$._onArrowClick();"></div>' +
  24484. '</div></div></div>';
  24485. },
  24486. showPopup: function () {
  24487. // 当popup往上弹出的时候,做特殊处理
  24488. var rect = uiUtils.getClientRect(this.getDom());
  24489. rect.top -= this.popup.SHADOW_RADIUS;
  24490. rect.height += this.popup.SHADOW_RADIUS;
  24491. this.popup.showAnchorRect(rect);
  24492. },
  24493. _onArrowClick: function (event, el) {
  24494. if (!this.isDisabled()) {
  24495. this.showPopup();
  24496. }
  24497. },
  24498. _onButtonClick: function () {
  24499. if (!this.isDisabled()) {
  24500. this.fireEvent('buttonclick');
  24501. }
  24502. }
  24503. };
  24504. utils.inherits(SplitButton, UIBase);
  24505. utils.extend(SplitButton.prototype, Stateful, true);
  24506. })();
  24507. // ui/colorbutton.js
  24508. ///import core
  24509. ///import uicore
  24510. ///import ui/colorpicker.js
  24511. ///import ui/popup.js
  24512. ///import ui/splitbutton.js
  24513. (function () {
  24514. var utils = baidu.editor.utils,
  24515. uiUtils = baidu.editor.ui.uiUtils,
  24516. ColorPicker = baidu.editor.ui.ColorPicker,
  24517. Popup = baidu.editor.ui.Popup,
  24518. SplitButton = baidu.editor.ui.SplitButton,
  24519. ColorButton = baidu.editor.ui.ColorButton = function (options) {
  24520. this.initOptions(options);
  24521. this.initColorButton();
  24522. };
  24523. ColorButton.prototype = {
  24524. initColorButton: function () {
  24525. var me = this;
  24526. this.popup = new Popup({
  24527. content: new ColorPicker({
  24528. noColorText: me.editor.getLang("clearColor"),
  24529. editor: me.editor,
  24530. onpickcolor: function (t, color) {
  24531. me._onPickColor(color);
  24532. },
  24533. onpicknocolor: function (t, color) {
  24534. me._onPickNoColor(color);
  24535. }
  24536. }),
  24537. editor: me.editor
  24538. });
  24539. this.initSplitButton();
  24540. },
  24541. _SplitButton_postRender: SplitButton.prototype.postRender,
  24542. postRender: function () {
  24543. this._SplitButton_postRender();
  24544. this.getDom('button_body').appendChild(
  24545. uiUtils.createElementByHtml('<div id="' + this.id + '_colorlump" class="edui-colorlump"></div>')
  24546. );
  24547. this.getDom().className += ' edui-colorbutton';
  24548. },
  24549. setColor: function (color) {
  24550. this.getDom('colorlump').style.backgroundColor = color;
  24551. this.color = color;
  24552. },
  24553. _onPickColor: function (color) {
  24554. if (this.fireEvent('pickcolor', color) !== false) {
  24555. this.setColor(color);
  24556. this.popup.hide();
  24557. }
  24558. },
  24559. _onPickNoColor: function (color) {
  24560. if (this.fireEvent('picknocolor') !== false) {
  24561. this.popup.hide();
  24562. }
  24563. }
  24564. };
  24565. utils.inherits(ColorButton, SplitButton);
  24566. })();
  24567. // ui/tablebutton.js
  24568. ///import core
  24569. ///import uicore
  24570. ///import ui/popup.js
  24571. ///import ui/tablepicker.js
  24572. ///import ui/splitbutton.js
  24573. (function () {
  24574. var utils = baidu.editor.utils,
  24575. Popup = baidu.editor.ui.Popup,
  24576. TablePicker = baidu.editor.ui.TablePicker,
  24577. SplitButton = baidu.editor.ui.SplitButton,
  24578. TableButton = baidu.editor.ui.TableButton = function (options) {
  24579. this.initOptions(options);
  24580. this.initTableButton();
  24581. };
  24582. TableButton.prototype = {
  24583. initTableButton: function () {
  24584. var me = this;
  24585. this.popup = new Popup({
  24586. content: new TablePicker({
  24587. editor: me.editor,
  24588. onpicktable: function (t, numCols, numRows) {
  24589. me._onPickTable(numCols, numRows);
  24590. }
  24591. }),
  24592. 'editor': me.editor
  24593. });
  24594. this.initSplitButton();
  24595. },
  24596. _onPickTable: function (numCols, numRows) {
  24597. if (this.fireEvent('picktable', numCols, numRows) !== false) {
  24598. this.popup.hide();
  24599. }
  24600. }
  24601. };
  24602. utils.inherits(TableButton, SplitButton);
  24603. })();
  24604. // ui/autotypesetpicker.js
  24605. ///import core
  24606. ///import uicore
  24607. (function () {
  24608. var utils = baidu.editor.utils,
  24609. UIBase = baidu.editor.ui.UIBase;
  24610. var AutoTypeSetPicker = baidu.editor.ui.AutoTypeSetPicker = function (options) {
  24611. this.initOptions(options);
  24612. this.initAutoTypeSetPicker();
  24613. };
  24614. AutoTypeSetPicker.prototype = {
  24615. initAutoTypeSetPicker: function () {
  24616. this.initUIBase();
  24617. },
  24618. getHtmlTpl: function () {
  24619. var me = this.editor,
  24620. opt = me.options.autotypeset,
  24621. lang = me.getLang("autoTypeSet");
  24622. var textAlignInputName = 'textAlignValue' + me.uid,
  24623. imageBlockInputName = 'imageBlockLineValue' + me.uid,
  24624. symbolConverInputName = 'symbolConverValue' + me.uid;
  24625. return '<div id="##" class="edui-autotypesetpicker %%">' +
  24626. '<div class="edui-autotypesetpicker-body">' +
  24627. '<table >' +
  24628. '<tr><td nowrap><input type="checkbox" name="mergeEmptyline" ' + (opt["mergeEmptyline"] ? "checked" : "") + '>' + lang.mergeLine + '</td><td colspan="2"><input type="checkbox" name="removeEmptyline" ' + (opt["removeEmptyline"] ? "checked" : "") + '>' + lang.delLine + '</td></tr>' +
  24629. '<tr><td nowrap><input type="checkbox" name="removeClass" ' + (opt["removeClass"] ? "checked" : "") + '>' + lang.removeFormat + '</td><td colspan="2"><input type="checkbox" name="indent" ' + (opt["indent"] ? "checked" : "") + '>' + lang.indent + '</td></tr>' +
  24630. '<tr>' +
  24631. '<td nowrap><input type="checkbox" name="textAlign" ' + (opt["textAlign"] ? "checked" : "") + '>' + lang.alignment + '</td>' +
  24632. '<td colspan="2" id="' + textAlignInputName + '">' +
  24633. '<input type="radio" name="' + textAlignInputName + '" value="left" ' + ((opt["textAlign"] && opt["textAlign"] == "left") ? "checked" : "") + '>' + me.getLang("justifyleft") +
  24634. '<input type="radio" name="' + textAlignInputName + '" value="center" ' + ((opt["textAlign"] && opt["textAlign"] == "center") ? "checked" : "") + '>' + me.getLang("justifycenter") +
  24635. '<input type="radio" name="' + textAlignInputName + '" value="right" ' + ((opt["textAlign"] && opt["textAlign"] == "right") ? "checked" : "") + '>' + me.getLang("justifyright") +
  24636. '</td>' +
  24637. '</tr>' +
  24638. '<tr>' +
  24639. '<td nowrap><input type="checkbox" name="imageBlockLine" ' + (opt["imageBlockLine"] ? "checked" : "") + '>' + lang.imageFloat + '</td>' +
  24640. '<td nowrap id="' + imageBlockInputName + '">' +
  24641. '<input type="radio" name="' + imageBlockInputName + '" value="none" ' + ((opt["imageBlockLine"] && opt["imageBlockLine"] == "none") ? "checked" : "") + '>' + me.getLang("default") +
  24642. '<input type="radio" name="' + imageBlockInputName + '" value="left" ' + ((opt["imageBlockLine"] && opt["imageBlockLine"] == "left") ? "checked" : "") + '>' + me.getLang("justifyleft") +
  24643. '<input type="radio" name="' + imageBlockInputName + '" value="center" ' + ((opt["imageBlockLine"] && opt["imageBlockLine"] == "center") ? "checked" : "") + '>' + me.getLang("justifycenter") +
  24644. '<input type="radio" name="' + imageBlockInputName + '" value="right" ' + ((opt["imageBlockLine"] && opt["imageBlockLine"] == "right") ? "checked" : "") + '>' + me.getLang("justifyright") +
  24645. '</td>' +
  24646. '</tr>' +
  24647. '<tr><td nowrap><input type="checkbox" name="clearFontSize" ' + (opt["clearFontSize"] ? "checked" : "") + '>' + lang.removeFontsize + '</td><td colspan="2"><input type="checkbox" name="clearFontFamily" ' + (opt["clearFontFamily"] ? "checked" : "") + '>' + lang.removeFontFamily + '</td></tr>' +
  24648. '<tr><td nowrap colspan="3"><input type="checkbox" name="removeEmptyNode" ' + (opt["removeEmptyNode"] ? "checked" : "") + '>' + lang.removeHtml + '</td></tr>' +
  24649. '<tr><td nowrap colspan="3"><input type="checkbox" name="pasteFilter" ' + (opt["pasteFilter"] ? "checked" : "") + '>' + lang.pasteFilter + '</td></tr>' +
  24650. '<tr>' +
  24651. '<td nowrap><input type="checkbox" name="symbolConver" ' + (opt["bdc2sb"] || opt["tobdc"] ? "checked" : "") + '>' + lang.symbol + '</td>' +
  24652. '<td id="' + symbolConverInputName + '">' +
  24653. '<input type="radio" name="bdc" value="bdc2sb" ' + (opt["bdc2sb"] ? "checked" : "") + '>' + lang.bdc2sb +
  24654. '<input type="radio" name="bdc" value="tobdc" ' + (opt["tobdc"] ? "checked" : "") + '>' + lang.tobdc + '' +
  24655. '</td>' +
  24656. '<td nowrap align="right"><button >' + lang.run + '</button></td>' +
  24657. '</tr>' +
  24658. '</table>' +
  24659. '</div>' +
  24660. '</div>';
  24661. },
  24662. _UIBase_render: UIBase.prototype.render
  24663. };
  24664. utils.inherits(AutoTypeSetPicker, UIBase);
  24665. })();
  24666. // ui/autotypesetbutton.js
  24667. ///import core
  24668. ///import uicore
  24669. ///import ui/popup.js
  24670. ///import ui/autotypesetpicker.js
  24671. ///import ui/splitbutton.js
  24672. (function () {
  24673. var utils = baidu.editor.utils,
  24674. Popup = baidu.editor.ui.Popup,
  24675. AutoTypeSetPicker = baidu.editor.ui.AutoTypeSetPicker,
  24676. SplitButton = baidu.editor.ui.SplitButton,
  24677. AutoTypeSetButton = baidu.editor.ui.AutoTypeSetButton = function (options) {
  24678. this.initOptions(options);
  24679. this.initAutoTypeSetButton();
  24680. };
  24681. function getPara(me) {
  24682. var opt = {},
  24683. cont = me.getDom(),
  24684. editorId = me.editor.uid,
  24685. inputType = null,
  24686. attrName = null,
  24687. ipts = domUtils.getElementsByTagName(cont, "input");
  24688. for (var i = ipts.length - 1, ipt; ipt = ipts[i--];) {
  24689. inputType = ipt.getAttribute("type");
  24690. if (inputType == "checkbox") {
  24691. attrName = ipt.getAttribute("name");
  24692. opt[attrName] && delete opt[attrName];
  24693. if (ipt.checked) {
  24694. var attrValue = document.getElementById(attrName + "Value" + editorId);
  24695. if (attrValue) {
  24696. if (/input/ig.test(attrValue.tagName)) {
  24697. opt[attrName] = attrValue.value;
  24698. } else {
  24699. var iptChilds = attrValue.getElementsByTagName("input");
  24700. for (var j = iptChilds.length - 1, iptchild; iptchild = iptChilds[j--];) {
  24701. if (iptchild.checked) {
  24702. opt[attrName] = iptchild.value;
  24703. break;
  24704. }
  24705. }
  24706. }
  24707. } else {
  24708. opt[attrName] = true;
  24709. }
  24710. } else {
  24711. opt[attrName] = false;
  24712. }
  24713. } else {
  24714. opt[ipt.getAttribute("value")] = ipt.checked;
  24715. }
  24716. }
  24717. var selects = domUtils.getElementsByTagName(cont, "select");
  24718. for (var i = 0, si; si = selects[i++];) {
  24719. var attr = si.getAttribute('name');
  24720. opt[attr] = opt[attr] ? si.value : '';
  24721. }
  24722. utils.extend(me.editor.options.autotypeset, opt);
  24723. me.editor.setPreferences('autotypeset', opt);
  24724. }
  24725. AutoTypeSetButton.prototype = {
  24726. initAutoTypeSetButton: function () {
  24727. var me = this;
  24728. this.popup = new Popup({
  24729. //传入配置参数
  24730. content: new AutoTypeSetPicker({
  24731. editor: me.editor
  24732. }),
  24733. 'editor': me.editor,
  24734. hide: function () {
  24735. if (!this._hidden && this.getDom()) {
  24736. getPara(this);
  24737. this.getDom().style.display = 'none';
  24738. this._hidden = true;
  24739. this.fireEvent('hide');
  24740. }
  24741. }
  24742. });
  24743. var flag = 0;
  24744. this.popup.addListener('postRenderAfter', function () {
  24745. var popupUI = this;
  24746. if (flag) return;
  24747. var cont = this.getDom(),
  24748. btn = cont.getElementsByTagName('button')[0];
  24749. btn.onclick = function () {
  24750. getPara(popupUI);
  24751. me.editor.execCommand('autotypeset');
  24752. popupUI.hide()
  24753. };
  24754. domUtils.on(cont, 'click', function (e) {
  24755. var target = e.target || e.srcElement,
  24756. editorId = me.editor.uid;
  24757. if (target && target.tagName == 'INPUT') {
  24758. // 点击图片浮动的checkbox,去除对应的radio
  24759. if (target.name == 'imageBlockLine' || target.name == 'textAlign' || target.name == 'symbolConver') {
  24760. var checked = target.checked,
  24761. radioTd = document.getElementById(target.name + 'Value' + editorId),
  24762. radios = radioTd.getElementsByTagName('input'),
  24763. defalutSelect = {
  24764. 'imageBlockLine': 'none',
  24765. 'textAlign': 'left',
  24766. 'symbolConver': 'tobdc'
  24767. };
  24768. for (var i = 0; i < radios.length; i++) {
  24769. if (checked) {
  24770. if (radios[i].value == defalutSelect[target.name]) {
  24771. radios[i].checked = 'checked';
  24772. }
  24773. } else {
  24774. radios[i].checked = false;
  24775. }
  24776. }
  24777. }
  24778. // 点击radio,选中对应的checkbox
  24779. if (target.name == ('imageBlockLineValue' + editorId) || target.name == ('textAlignValue' + editorId) || target.name == 'bdc') {
  24780. var checkboxs = target.parentNode.previousSibling.getElementsByTagName('input');
  24781. checkboxs && (checkboxs[0].checked = true);
  24782. }
  24783. getPara(popupUI);
  24784. }
  24785. });
  24786. flag = 1;
  24787. });
  24788. this.initSplitButton();
  24789. }
  24790. };
  24791. utils.inherits(AutoTypeSetButton, SplitButton);
  24792. })();
  24793. // ui/cellalignpicker.js
  24794. ///import core
  24795. ///import uicore
  24796. (function () {
  24797. var utils = baidu.editor.utils,
  24798. Popup = baidu.editor.ui.Popup,
  24799. Stateful = baidu.editor.ui.Stateful,
  24800. UIBase = baidu.editor.ui.UIBase;
  24801. /**
  24802. * 该参数将新增一个参数: selected, 参数类型为一个Object, 形如{ 'align': 'center', 'valign': 'top' }, 表示单元格的初始
  24803. * 对齐状态为: 竖直居上,水平居中; 其中 align的取值为:'center', 'left', 'right'; valign的取值为: 'top', 'middle', 'bottom'
  24804. * @update 2013/4/2 hancong03@baidu.com
  24805. */
  24806. var CellAlignPicker = baidu.editor.ui.CellAlignPicker = function (options) {
  24807. this.initOptions(options);
  24808. this.initSelected();
  24809. this.initCellAlignPicker();
  24810. };
  24811. CellAlignPicker.prototype = {
  24812. //初始化选中状态, 该方法将根据传递进来的参数获取到应该选中的对齐方式图标的索引
  24813. initSelected: function () {
  24814. var status = {
  24815. valign: {
  24816. top: 0,
  24817. middle: 1,
  24818. bottom: 2
  24819. },
  24820. align: {
  24821. left: 0,
  24822. center: 1,
  24823. right: 2
  24824. },
  24825. count: 3
  24826. },
  24827. result = -1;
  24828. if (this.selected) {
  24829. this.selectedIndex = status.valign[this.selected.valign] * status.count + status.align[this.selected.align];
  24830. }
  24831. },
  24832. initCellAlignPicker: function () {
  24833. this.initUIBase();
  24834. this.Stateful_init();
  24835. },
  24836. getHtmlTpl: function () {
  24837. var alignType = ['left', 'center', 'right'],
  24838. COUNT = 9,
  24839. tempClassName = null,
  24840. tempIndex = -1,
  24841. tmpl = [];
  24842. for (var i = 0; i < COUNT; i++) {
  24843. tempClassName = this.selectedIndex === i ? ' class="edui-cellalign-selected" ' : '';
  24844. tempIndex = i % 3;
  24845. tempIndex === 0 && tmpl.push('<tr>');
  24846. tmpl.push('<td index="' + i + '" ' + tempClassName + ' stateful><div class="edui-icon edui-' + alignType[tempIndex] + '"></div></td>');
  24847. tempIndex === 2 && tmpl.push('</tr>');
  24848. }
  24849. return '<div id="##" class="edui-cellalignpicker %%">' +
  24850. '<div class="edui-cellalignpicker-body">' +
  24851. '<table onclick="$$._onClick(event);">' +
  24852. tmpl.join('') +
  24853. '</table>' +
  24854. '</div>' +
  24855. '</div>';
  24856. },
  24857. getStateDom: function () {
  24858. return this.target;
  24859. },
  24860. _onClick: function (evt) {
  24861. var target = evt.target || evt.srcElement;
  24862. if (/icon/.test(target.className)) {
  24863. this.items[target.parentNode.getAttribute("index")].onclick();
  24864. Popup.postHide(evt);
  24865. }
  24866. },
  24867. _UIBase_render: UIBase.prototype.render
  24868. };
  24869. utils.inherits(CellAlignPicker, UIBase);
  24870. utils.extend(CellAlignPicker.prototype, Stateful, true);
  24871. })();
  24872. // ui/pastepicker.js
  24873. ///import core
  24874. ///import uicore
  24875. (function () {
  24876. var utils = baidu.editor.utils,
  24877. Stateful = baidu.editor.ui.Stateful,
  24878. uiUtils = baidu.editor.ui.uiUtils,
  24879. UIBase = baidu.editor.ui.UIBase;
  24880. var PastePicker = baidu.editor.ui.PastePicker = function (options) {
  24881. this.initOptions(options);
  24882. this.initPastePicker();
  24883. };
  24884. PastePicker.prototype = {
  24885. initPastePicker: function () {
  24886. this.initUIBase();
  24887. this.Stateful_init();
  24888. },
  24889. getHtmlTpl: function () {
  24890. return '<div class="edui-pasteicon" onclick="$$._onClick(this)"></div>' +
  24891. '<div class="edui-pastecontainer">' +
  24892. '<div class="edui-title">' + this.editor.getLang("pasteOpt") + '</div>' +
  24893. '<div class="edui-button">' +
  24894. '<div title="' + this.editor.getLang("pasteSourceFormat") + '" onclick="$$.format(false)" stateful>' +
  24895. '<div class="edui-richtxticon"></div></div>' +
  24896. '<div title="' + this.editor.getLang("tagFormat") + '" onclick="$$.format(2)" stateful>' +
  24897. '<div class="edui-tagicon"></div></div>' +
  24898. '<div title="' + this.editor.getLang("pasteTextFormat") + '" onclick="$$.format(true)" stateful>' +
  24899. '<div class="edui-plaintxticon"></div></div>' +
  24900. '</div>' +
  24901. '</div>' +
  24902. '</div>'
  24903. },
  24904. getStateDom: function () {
  24905. return this.target;
  24906. },
  24907. format: function (param) {
  24908. this.editor.ui._isTransfer = true;
  24909. this.editor.fireEvent('pasteTransfer', param);
  24910. },
  24911. _onClick: function (cur) {
  24912. var node = domUtils.getNextDomNode(cur),
  24913. screenHt = uiUtils.getViewportRect().height,
  24914. subPop = uiUtils.getClientRect(node);
  24915. if ((subPop.top + subPop.height) > screenHt)
  24916. node.style.top = (-subPop.height - cur.offsetHeight) + "px";
  24917. else
  24918. node.style.top = "";
  24919. if (/hidden/ig.test(domUtils.getComputedStyle(node, "visibility"))) {
  24920. node.style.visibility = "visible";
  24921. domUtils.addClass(cur, "edui-state-opened");
  24922. } else {
  24923. node.style.visibility = "hidden";
  24924. domUtils.removeClasses(cur, "edui-state-opened")
  24925. }
  24926. },
  24927. _UIBase_render: UIBase.prototype.render
  24928. };
  24929. utils.inherits(PastePicker, UIBase);
  24930. utils.extend(PastePicker.prototype, Stateful, true);
  24931. })();
  24932. // ui/toolbar.js
  24933. (function () {
  24934. var utils = baidu.editor.utils,
  24935. uiUtils = baidu.editor.ui.uiUtils,
  24936. UIBase = baidu.editor.ui.UIBase,
  24937. Toolbar = baidu.editor.ui.Toolbar = function (options) {
  24938. this.initOptions(options);
  24939. this.initToolbar();
  24940. };
  24941. Toolbar.prototype = {
  24942. items: null,
  24943. initToolbar: function () {
  24944. this.items = this.items || [];
  24945. this.initUIBase();
  24946. },
  24947. add: function (item, index) {
  24948. if (index === undefined) {
  24949. this.items.push(item);
  24950. } else {
  24951. this.items.splice(index, 0, item)
  24952. }
  24953. },
  24954. getHtmlTpl: function () {
  24955. var buff = [];
  24956. for (var i = 0; i < this.items.length; i++) {
  24957. buff[i] = this.items[i].renderHtml();
  24958. }
  24959. return '<div id="##" class="edui-toolbar %%" onselectstart="return false;" onmousedown="return $$._onMouseDown(event, this);">' +
  24960. buff.join('') +
  24961. '</div>'
  24962. },
  24963. postRender: function () {
  24964. var box = this.getDom();
  24965. for (var i = 0; i < this.items.length; i++) {
  24966. this.items[i].postRender();
  24967. }
  24968. uiUtils.makeUnselectable(box);
  24969. },
  24970. _onMouseDown: function (e) {
  24971. var target = e.target || e.srcElement,
  24972. tagName = target && target.tagName && target.tagName.toLowerCase();
  24973. if (tagName == 'input' || tagName == 'object' || tagName == 'object') {
  24974. return false;
  24975. }
  24976. }
  24977. };
  24978. utils.inherits(Toolbar, UIBase);
  24979. })();
  24980. // ui/menu.js
  24981. ///import core
  24982. ///import uicore
  24983. ///import ui\popup.js
  24984. ///import ui\stateful.js
  24985. (function () {
  24986. var utils = baidu.editor.utils,
  24987. domUtils = baidu.editor.dom.domUtils,
  24988. uiUtils = baidu.editor.ui.uiUtils,
  24989. UIBase = baidu.editor.ui.UIBase,
  24990. Popup = baidu.editor.ui.Popup,
  24991. Stateful = baidu.editor.ui.Stateful,
  24992. CellAlignPicker = baidu.editor.ui.CellAlignPicker,
  24993. Menu = baidu.editor.ui.Menu = function (options) {
  24994. this.initOptions(options);
  24995. this.initMenu();
  24996. };
  24997. var menuSeparator = {
  24998. renderHtml: function () {
  24999. return '<div class="edui-menuitem edui-menuseparator"><div class="edui-menuseparator-inner"></div></div>';
  25000. },
  25001. postRender: function () {},
  25002. queryAutoHide: function () {
  25003. return true;
  25004. }
  25005. };
  25006. Menu.prototype = {
  25007. items: null,
  25008. uiName: 'menu',
  25009. initMenu: function () {
  25010. this.items = this.items || [];
  25011. this.initPopup();
  25012. this.initItems();
  25013. },
  25014. initItems: function () {
  25015. for (var i = 0; i < this.items.length; i++) {
  25016. var item = this.items[i];
  25017. if (item == '-') {
  25018. this.items[i] = this.getSeparator();
  25019. } else if (!(item instanceof MenuItem)) {
  25020. item.editor = this.editor;
  25021. item.theme = this.editor.options.theme;
  25022. this.items[i] = this.createItem(item);
  25023. }
  25024. }
  25025. },
  25026. getSeparator: function () {
  25027. return menuSeparator;
  25028. },
  25029. createItem: function (item) {
  25030. //新增一个参数menu, 该参数存储了menuItem所对应的menu引用
  25031. item.menu = this;
  25032. return new MenuItem(item);
  25033. },
  25034. _Popup_getContentHtmlTpl: Popup.prototype.getContentHtmlTpl,
  25035. getContentHtmlTpl: function () {
  25036. if (this.items.length == 0) {
  25037. return this._Popup_getContentHtmlTpl();
  25038. }
  25039. var buff = [];
  25040. for (var i = 0; i < this.items.length; i++) {
  25041. var item = this.items[i];
  25042. buff[i] = item.renderHtml();
  25043. }
  25044. return ('<div class="%%-body">' + buff.join('') + '</div>');
  25045. },
  25046. _Popup_postRender: Popup.prototype.postRender,
  25047. postRender: function () {
  25048. var me = this;
  25049. for (var i = 0; i < this.items.length; i++) {
  25050. var item = this.items[i];
  25051. item.ownerMenu = this;
  25052. item.postRender();
  25053. }
  25054. domUtils.on(this.getDom(), 'mouseover', function (evt) {
  25055. evt = evt || event;
  25056. var rel = evt.relatedTarget || evt.fromElement;
  25057. var el = me.getDom();
  25058. if (!uiUtils.contains(el, rel) && el !== rel) {
  25059. me.fireEvent('over');
  25060. }
  25061. });
  25062. this._Popup_postRender();
  25063. },
  25064. queryAutoHide: function (el) {
  25065. if (el) {
  25066. if (uiUtils.contains(this.getDom(), el)) {
  25067. return false;
  25068. }
  25069. for (var i = 0; i < this.items.length; i++) {
  25070. var item = this.items[i];
  25071. if (item.queryAutoHide(el) === false) {
  25072. return false;
  25073. }
  25074. }
  25075. }
  25076. },
  25077. clearItems: function () {
  25078. for (var i = 0; i < this.items.length; i++) {
  25079. var item = this.items[i];
  25080. clearTimeout(item._showingTimer);
  25081. clearTimeout(item._closingTimer);
  25082. if (item.subMenu) {
  25083. item.subMenu.destroy();
  25084. }
  25085. }
  25086. this.items = [];
  25087. },
  25088. destroy: function () {
  25089. if (this.getDom()) {
  25090. domUtils.remove(this.getDom());
  25091. }
  25092. this.clearItems();
  25093. },
  25094. dispose: function () {
  25095. this.destroy();
  25096. }
  25097. };
  25098. utils.inherits(Menu, Popup);
  25099. /**
  25100. * @update 2013/04/03 hancong03 新增一个参数menu, 该参数存储了menuItem所对应的menu引用
  25101. * @type {Function}
  25102. */
  25103. var MenuItem = baidu.editor.ui.MenuItem = function (options) {
  25104. this.initOptions(options);
  25105. this.initUIBase();
  25106. this.Stateful_init();
  25107. if (this.subMenu && !(this.subMenu instanceof Menu)) {
  25108. if (options.className && options.className.indexOf("aligntd") != -1) {
  25109. var me = this;
  25110. //获取单元格对齐初始状态
  25111. this.subMenu.selected = this.editor.queryCommandValue('cellalignment');
  25112. this.subMenu = new Popup({
  25113. content: new CellAlignPicker(this.subMenu),
  25114. parentMenu: me,
  25115. editor: me.editor,
  25116. destroy: function () {
  25117. if (this.getDom()) {
  25118. domUtils.remove(this.getDom());
  25119. }
  25120. }
  25121. });
  25122. this.subMenu.addListener("postRenderAfter", function () {
  25123. domUtils.on(this.getDom(), "mouseover", function () {
  25124. me.addState('opened');
  25125. });
  25126. });
  25127. } else {
  25128. this.subMenu = new Menu(this.subMenu);
  25129. }
  25130. }
  25131. };
  25132. MenuItem.prototype = {
  25133. label: '',
  25134. subMenu: null,
  25135. ownerMenu: null,
  25136. uiName: 'menuitem',
  25137. alwalysHoverable: true,
  25138. getHtmlTpl: function () {
  25139. return '<div id="##" class="%%" stateful onclick="$$._onClick(event, this);">' +
  25140. '<div class="%%-body">' +
  25141. this.renderLabelHtml() +
  25142. '</div>' +
  25143. '</div>';
  25144. },
  25145. postRender: function () {
  25146. var me = this;
  25147. this.addListener('over', function () {
  25148. me.ownerMenu.fireEvent('submenuover', me);
  25149. if (me.subMenu) {
  25150. me.delayShowSubMenu();
  25151. }
  25152. });
  25153. if (this.subMenu) {
  25154. this.getDom().className += ' edui-hassubmenu';
  25155. this.subMenu.render();
  25156. this.addListener('out', function () {
  25157. me.delayHideSubMenu();
  25158. });
  25159. this.subMenu.addListener('over', function () {
  25160. clearTimeout(me._closingTimer);
  25161. me._closingTimer = null;
  25162. me.addState('opened');
  25163. });
  25164. this.ownerMenu.addListener('hide', function () {
  25165. me.hideSubMenu();
  25166. });
  25167. this.ownerMenu.addListener('submenuover', function (t, subMenu) {
  25168. if (subMenu !== me) {
  25169. me.delayHideSubMenu();
  25170. }
  25171. });
  25172. this.subMenu._bakQueryAutoHide = this.subMenu.queryAutoHide;
  25173. this.subMenu.queryAutoHide = function (el) {
  25174. if (el && uiUtils.contains(me.getDom(), el)) {
  25175. return false;
  25176. }
  25177. return this._bakQueryAutoHide(el);
  25178. };
  25179. }
  25180. this.getDom().style.tabIndex = '-1';
  25181. uiUtils.makeUnselectable(this.getDom());
  25182. this.Stateful_postRender();
  25183. },
  25184. delayShowSubMenu: function () {
  25185. var me = this;
  25186. if (!me.isDisabled()) {
  25187. me.addState('opened');
  25188. clearTimeout(me._showingTimer);
  25189. clearTimeout(me._closingTimer);
  25190. me._closingTimer = null;
  25191. me._showingTimer = setTimeout(function () {
  25192. me.showSubMenu();
  25193. }, 250);
  25194. }
  25195. },
  25196. delayHideSubMenu: function () {
  25197. var me = this;
  25198. if (!me.isDisabled()) {
  25199. me.removeState('opened');
  25200. clearTimeout(me._showingTimer);
  25201. if (!me._closingTimer) {
  25202. me._closingTimer = setTimeout(function () {
  25203. if (!me.hasState('opened')) {
  25204. me.hideSubMenu();
  25205. }
  25206. me._closingTimer = null;
  25207. }, 400);
  25208. }
  25209. }
  25210. },
  25211. renderLabelHtml: function () {
  25212. return '<div class="edui-arrow"></div>' +
  25213. '<div class="edui-box edui-icon"></div>' +
  25214. '<div class="edui-box edui-label %%-label">' + (this.label || '') + '</div>';
  25215. },
  25216. getStateDom: function () {
  25217. return this.getDom();
  25218. },
  25219. queryAutoHide: function (el) {
  25220. if (this.subMenu && this.hasState('opened')) {
  25221. return this.subMenu.queryAutoHide(el);
  25222. }
  25223. },
  25224. _onClick: function (event, this_) {
  25225. if (this.hasState('disabled')) return;
  25226. if (this.fireEvent('click', event, this_) !== false) {
  25227. if (this.subMenu) {
  25228. this.showSubMenu();
  25229. } else {
  25230. Popup.postHide(event);
  25231. }
  25232. }
  25233. },
  25234. showSubMenu: function () {
  25235. var rect = uiUtils.getClientRect(this.getDom());
  25236. rect.right -= 5;
  25237. rect.left += 2;
  25238. rect.width -= 7;
  25239. rect.top -= 4;
  25240. rect.bottom += 4;
  25241. rect.height += 8;
  25242. this.subMenu.showAnchorRect(rect, true, true);
  25243. },
  25244. hideSubMenu: function () {
  25245. this.subMenu.hide();
  25246. }
  25247. };
  25248. utils.inherits(MenuItem, UIBase);
  25249. utils.extend(MenuItem.prototype, Stateful, true);
  25250. })();
  25251. // ui/combox.js
  25252. ///import core
  25253. ///import uicore
  25254. ///import ui/menu.js
  25255. ///import ui/splitbutton.js
  25256. (function () {
  25257. // todo: menu和item提成通用list
  25258. var utils = baidu.editor.utils,
  25259. uiUtils = baidu.editor.ui.uiUtils,
  25260. Menu = baidu.editor.ui.Menu,
  25261. SplitButton = baidu.editor.ui.SplitButton,
  25262. Combox = baidu.editor.ui.Combox = function (options) {
  25263. this.initOptions(options);
  25264. this.initCombox();
  25265. };
  25266. Combox.prototype = {
  25267. uiName: 'combox',
  25268. onbuttonclick: function () {
  25269. this.showPopup();
  25270. },
  25271. initCombox: function () {
  25272. var me = this;
  25273. this.items = this.items || [];
  25274. for (var i = 0; i < this.items.length; i++) {
  25275. var item = this.items[i];
  25276. item.uiName = 'listitem';
  25277. item.index = i;
  25278. item.onclick = function () {
  25279. me.selectByIndex(this.index);
  25280. };
  25281. }
  25282. this.popup = new Menu({
  25283. items: this.items,
  25284. uiName: 'list',
  25285. editor: this.editor,
  25286. captureWheel: true,
  25287. combox: this
  25288. });
  25289. this.initSplitButton();
  25290. },
  25291. _SplitButton_postRender: SplitButton.prototype.postRender,
  25292. postRender: function () {
  25293. this._SplitButton_postRender();
  25294. this.setLabel(this.label || '');
  25295. this.setValue(this.initValue || '');
  25296. },
  25297. showPopup: function () {
  25298. var rect = uiUtils.getClientRect(this.getDom());
  25299. rect.top += 1;
  25300. rect.bottom -= 1;
  25301. rect.height -= 2;
  25302. this.popup.showAnchorRect(rect);
  25303. },
  25304. getValue: function () {
  25305. return this.value;
  25306. },
  25307. setValue: function (value) {
  25308. var index = this.indexByValue(value);
  25309. if (index != -1) {
  25310. this.selectedIndex = index;
  25311. this.setLabel(this.items[index].label);
  25312. this.value = this.items[index].value;
  25313. } else {
  25314. this.selectedIndex = -1;
  25315. this.setLabel(this.getLabelForUnknowValue(value));
  25316. this.value = value;
  25317. }
  25318. },
  25319. setLabel: function (label) {
  25320. this.getDom('button_body').innerHTML = label;
  25321. this.label = label;
  25322. },
  25323. getLabelForUnknowValue: function (value) {
  25324. return value;
  25325. },
  25326. indexByValue: function (value) {
  25327. for (var i = 0; i < this.items.length; i++) {
  25328. if (value == this.items[i].value) {
  25329. return i;
  25330. }
  25331. }
  25332. return -1;
  25333. },
  25334. getItem: function (index) {
  25335. return this.items[index];
  25336. },
  25337. selectByIndex: function (index) {
  25338. if (index < this.items.length && this.fireEvent('select', index) !== false) {
  25339. this.selectedIndex = index;
  25340. this.value = this.items[index].value;
  25341. this.setLabel(this.items[index].label);
  25342. }
  25343. }
  25344. };
  25345. utils.inherits(Combox, SplitButton);
  25346. })();
  25347. // ui/dialog.js
  25348. ///import core
  25349. ///import uicore
  25350. ///import ui/mask.js
  25351. ///import ui/button.js
  25352. (function () {
  25353. var utils = baidu.editor.utils,
  25354. domUtils = baidu.editor.dom.domUtils,
  25355. uiUtils = baidu.editor.ui.uiUtils,
  25356. Mask = baidu.editor.ui.Mask,
  25357. UIBase = baidu.editor.ui.UIBase,
  25358. Button = baidu.editor.ui.Button,
  25359. Dialog = baidu.editor.ui.Dialog = function (options) {
  25360. if (options.name) {
  25361. var name = options.name;
  25362. var cssRules = options.cssRules;
  25363. if (!options.className) {
  25364. options.className = 'edui-for-' + name;
  25365. }
  25366. if (cssRules) {
  25367. options.cssRules = '.edui-default .edui-for-' + name + ' .edui-dialog-content {' + cssRules + '}'
  25368. }
  25369. }
  25370. this.initOptions(utils.extend({
  25371. autoReset: true,
  25372. draggable: true,
  25373. onok: function () {},
  25374. oncancel: function () {},
  25375. onclose: function (t, ok) {
  25376. return ok ? this.onok() : this.oncancel();
  25377. },
  25378. //是否控制dialog中的scroll事件, 默认为不阻止
  25379. holdScroll: false
  25380. }, options));
  25381. this.initDialog();
  25382. };
  25383. var modalMask;
  25384. var dragMask;
  25385. var activeDialog;
  25386. Dialog.prototype = {
  25387. draggable: false,
  25388. uiName: 'dialog',
  25389. initDialog: function () {
  25390. var me = this,
  25391. theme = this.editor.options.theme;
  25392. if (this.cssRules) {
  25393. utils.cssRule('edui-customize-' + this.name + '-style', this.cssRules);
  25394. }
  25395. this.initUIBase();
  25396. this.modalMask = (modalMask || (modalMask = new Mask({
  25397. className: 'edui-dialog-modalmask',
  25398. theme: theme,
  25399. onclick: function () {
  25400. activeDialog && activeDialog.close(false);
  25401. }
  25402. })));
  25403. this.dragMask = (dragMask || (dragMask = new Mask({
  25404. className: 'edui-dialog-dragmask',
  25405. theme: theme
  25406. })));
  25407. this.closeButton = new Button({
  25408. className: 'edui-dialog-closebutton',
  25409. title: me.closeDialog,
  25410. theme: theme,
  25411. onclick: function () {
  25412. me.close(false);
  25413. }
  25414. });
  25415. this.fullscreen && this.initResizeEvent();
  25416. if (this.buttons) {
  25417. for (var i = 0; i < this.buttons.length; i++) {
  25418. if (!(this.buttons[i] instanceof Button)) {
  25419. this.buttons[i] = new Button(utils.extend(this.buttons[i], {
  25420. editor: this.editor
  25421. }, true));
  25422. }
  25423. }
  25424. }
  25425. },
  25426. initResizeEvent: function () {
  25427. var me = this;
  25428. domUtils.on(window, "resize", function () {
  25429. if (me._hidden || me._hidden === undefined) {
  25430. return;
  25431. }
  25432. if (me.__resizeTimer) {
  25433. window.clearTimeout(me.__resizeTimer);
  25434. }
  25435. me.__resizeTimer = window.setTimeout(function () {
  25436. me.__resizeTimer = null;
  25437. var dialogWrapNode = me.getDom(),
  25438. contentNode = me.getDom('content'),
  25439. wrapRect = UE.ui.uiUtils.getClientRect(dialogWrapNode),
  25440. contentRect = UE.ui.uiUtils.getClientRect(contentNode),
  25441. vpRect = uiUtils.getViewportRect();
  25442. contentNode.style.width = (vpRect.width - wrapRect.width + contentRect.width) + "px";
  25443. contentNode.style.height = (vpRect.height - wrapRect.height + contentRect.height) + "px";
  25444. dialogWrapNode.style.width = vpRect.width + "px";
  25445. dialogWrapNode.style.height = vpRect.height + "px";
  25446. me.fireEvent("resize");
  25447. }, 100);
  25448. });
  25449. },
  25450. fitSize: function () {
  25451. var popBodyEl = this.getDom('body');
  25452. // if (!(baidu.editor.browser.ie && baidu.editor.browser.version == 7)) {
  25453. // uiUtils.removeStyle(popBodyEl, 'width');
  25454. // uiUtils.removeStyle(popBodyEl, 'height');
  25455. // }
  25456. var size = this.mesureSize();
  25457. popBodyEl.style.width = size.width + 'px';
  25458. popBodyEl.style.height = size.height + 'px';
  25459. return size;
  25460. },
  25461. safeSetOffset: function (offset) {
  25462. var me = this;
  25463. var el = me.getDom();
  25464. var vpRect = uiUtils.getViewportRect();
  25465. var rect = uiUtils.getClientRect(el);
  25466. var left = offset.left;
  25467. if (left + rect.width > vpRect.right) {
  25468. left = vpRect.right - rect.width;
  25469. }
  25470. var top = offset.top;
  25471. if (top + rect.height > vpRect.bottom) {
  25472. top = vpRect.bottom - rect.height;
  25473. }
  25474. el.style.left = Math.max(left, 0) + 'px';
  25475. el.style.top = Math.max(top, 0) + 'px';
  25476. },
  25477. showAtCenter: function () {
  25478. var vpRect = uiUtils.getViewportRect();
  25479. if (!this.fullscreen) {
  25480. this.getDom().style.display = '';
  25481. var popSize = this.fitSize();
  25482. var titleHeight = this.getDom('titlebar').offsetHeight | 0;
  25483. var left = vpRect.width / 2 - popSize.width / 2;
  25484. var top = vpRect.height / 2 - (popSize.height - titleHeight) / 2 - titleHeight;
  25485. var popEl = this.getDom();
  25486. this.safeSetOffset({
  25487. left: Math.max(left | 0, 0),
  25488. top: Math.max(top | 0, 0)
  25489. });
  25490. if (!domUtils.hasClass(popEl, 'edui-state-centered')) {
  25491. popEl.className += ' edui-state-centered';
  25492. }
  25493. } else {
  25494. var dialogWrapNode = this.getDom(),
  25495. contentNode = this.getDom('content');
  25496. dialogWrapNode.style.display = "block";
  25497. var wrapRect = UE.ui.uiUtils.getClientRect(dialogWrapNode),
  25498. contentRect = UE.ui.uiUtils.getClientRect(contentNode);
  25499. dialogWrapNode.style.left = "-100000px";
  25500. contentNode.style.width = (vpRect.width - wrapRect.width + contentRect.width) + "px";
  25501. contentNode.style.height = (vpRect.height - wrapRect.height + contentRect.height) + "px";
  25502. dialogWrapNode.style.width = vpRect.width + "px";
  25503. dialogWrapNode.style.height = vpRect.height + "px";
  25504. dialogWrapNode.style.left = 0;
  25505. //保存环境的overflow值
  25506. this._originalContext = {
  25507. html: {
  25508. overflowX: document.documentElement.style.overflowX,
  25509. overflowY: document.documentElement.style.overflowY
  25510. },
  25511. body: {
  25512. overflowX: document.body.style.overflowX,
  25513. overflowY: document.body.style.overflowY
  25514. }
  25515. };
  25516. document.documentElement.style.overflowX = 'hidden';
  25517. document.documentElement.style.overflowY = 'hidden';
  25518. document.body.style.overflowX = 'hidden';
  25519. document.body.style.overflowY = 'hidden';
  25520. }
  25521. this._show();
  25522. },
  25523. getContentHtml: function () {
  25524. var contentHtml = '';
  25525. if (typeof this.content == 'string') {
  25526. contentHtml = this.content;
  25527. } else if (this.iframeUrl) {
  25528. contentHtml = '<span id="' + this.id + '_contmask" class="dialogcontmask"></span><iframe id="' + this.id +
  25529. '_iframe" class="%%-iframe" height="100%" width="100%" frameborder="0" src="' + this.iframeUrl + '"></iframe>';
  25530. }
  25531. return contentHtml;
  25532. },
  25533. getHtmlTpl: function () {
  25534. var footHtml = '';
  25535. if (this.buttons) {
  25536. var buff = [];
  25537. for (var i = 0; i < this.buttons.length; i++) {
  25538. buff[i] = this.buttons[i].renderHtml();
  25539. }
  25540. footHtml = '<div class="%%-foot">' +
  25541. '<div id="##_buttons" class="%%-buttons">' + buff.join('') + '</div>' +
  25542. '</div>';
  25543. }
  25544. return '<div id="##" class="%%"><div ' + (!this.fullscreen ? 'class="%%"' : 'class="%%-wrap edui-dialog-fullscreen-flag"') + '><div id="##_body" class="%%-body">' +
  25545. '<div class="%%-shadow"></div>' +
  25546. '<div id="##_titlebar" class="%%-titlebar">' +
  25547. '<div class="%%-draghandle" onmousedown="$$._onTitlebarMouseDown(event, this);">' +
  25548. '<span class="%%-caption">' + (this.title || '') + '</span>' +
  25549. '</div>' +
  25550. this.closeButton.renderHtml() +
  25551. '</div>' +
  25552. '<div id="##_content" class="%%-content">' + (this.autoReset ? '' : this.getContentHtml()) + '</div>' +
  25553. footHtml +
  25554. '</div></div></div>';
  25555. },
  25556. postRender: function () {
  25557. // todo: 保持居中/记住上次关闭位置选项
  25558. if (!this.modalMask.getDom()) {
  25559. this.modalMask.render();
  25560. this.modalMask.hide();
  25561. }
  25562. if (!this.dragMask.getDom()) {
  25563. this.dragMask.render();
  25564. this.dragMask.hide();
  25565. }
  25566. var me = this;
  25567. this.addListener('show', function () {
  25568. me.modalMask.show(this.getDom().style.zIndex - 2);
  25569. });
  25570. this.addListener('hide', function () {
  25571. me.modalMask.hide();
  25572. });
  25573. if (this.buttons) {
  25574. for (var i = 0; i < this.buttons.length; i++) {
  25575. this.buttons[i].postRender();
  25576. }
  25577. }
  25578. domUtils.on(window, 'resize', function () {
  25579. setTimeout(function () {
  25580. if (!me.isHidden()) {
  25581. me.safeSetOffset(uiUtils.getClientRect(me.getDom()));
  25582. }
  25583. });
  25584. });
  25585. //hold住scroll事件,防止dialog的滚动影响页面
  25586. // if( this.holdScroll ) {
  25587. //
  25588. // if( !me.iframeUrl ) {
  25589. // domUtils.on( document.getElementById( me.id + "_iframe"), !browser.gecko ? "mousewheel" : "DOMMouseScroll", function(e){
  25590. // domUtils.preventDefault(e);
  25591. // } );
  25592. // } else {
  25593. // me.addListener('dialogafterreset', function(){
  25594. // window.setTimeout(function(){
  25595. // var iframeWindow = document.getElementById( me.id + "_iframe").contentWindow;
  25596. //
  25597. // if( browser.ie ) {
  25598. //
  25599. // var timer = window.setInterval(function(){
  25600. //
  25601. // if( iframeWindow.document && iframeWindow.document.body ) {
  25602. // window.clearInterval( timer );
  25603. // timer = null;
  25604. // domUtils.on( iframeWindow.document.body, !browser.gecko ? "mousewheel" : "DOMMouseScroll", function(e){
  25605. // domUtils.preventDefault(e);
  25606. // } );
  25607. // }
  25608. //
  25609. // }, 100);
  25610. //
  25611. // } else {
  25612. // domUtils.on( iframeWindow, !browser.gecko ? "mousewheel" : "DOMMouseScroll", function(e){
  25613. // domUtils.preventDefault(e);
  25614. // } );
  25615. // }
  25616. //
  25617. // }, 1);
  25618. // });
  25619. // }
  25620. //
  25621. // }
  25622. this._hide();
  25623. },
  25624. mesureSize: function () {
  25625. var body = this.getDom('body');
  25626. var width = uiUtils.getClientRect(this.getDom('content')).width;
  25627. var dialogBodyStyle = body.style;
  25628. dialogBodyStyle.width = width;
  25629. return uiUtils.getClientRect(body);
  25630. },
  25631. _onTitlebarMouseDown: function (evt, el) {
  25632. if (this.draggable) {
  25633. var rect;
  25634. var vpRect = uiUtils.getViewportRect();
  25635. var me = this;
  25636. uiUtils.startDrag(evt, {
  25637. ondragstart: function () {
  25638. rect = uiUtils.getClientRect(me.getDom());
  25639. me.getDom('contmask').style.visibility = 'visible';
  25640. me.dragMask.show(me.getDom().style.zIndex - 1);
  25641. },
  25642. ondragmove: function (x, y) {
  25643. var left = rect.left + x;
  25644. var top = rect.top + y;
  25645. me.safeSetOffset({
  25646. left: left,
  25647. top: top
  25648. });
  25649. },
  25650. ondragstop: function () {
  25651. me.getDom('contmask').style.visibility = 'hidden';
  25652. domUtils.removeClasses(me.getDom(), ['edui-state-centered']);
  25653. me.dragMask.hide();
  25654. }
  25655. });
  25656. }
  25657. },
  25658. reset: function () {
  25659. this.getDom('content').innerHTML = this.getContentHtml();
  25660. this.fireEvent('dialogafterreset');
  25661. },
  25662. _show: function () {
  25663. if (this._hidden) {
  25664. this.getDom().style.display = '';
  25665. //要高过编辑器的zindxe
  25666. this.editor.container.style.zIndex && (this.getDom().style.zIndex = this.editor.container.style.zIndex * 1 + 10);
  25667. this._hidden = false;
  25668. this.fireEvent('show');
  25669. baidu.editor.ui.uiUtils.getFixedLayer().style.zIndex = this.getDom().style.zIndex - 4;
  25670. }
  25671. },
  25672. isHidden: function () {
  25673. return this._hidden;
  25674. },
  25675. _hide: function () {
  25676. if (!this._hidden) {
  25677. var wrapNode = this.getDom();
  25678. wrapNode.style.display = 'none';
  25679. wrapNode.style.zIndex = '';
  25680. wrapNode.style.width = '';
  25681. wrapNode.style.height = '';
  25682. this._hidden = true;
  25683. this.fireEvent('hide');
  25684. }
  25685. },
  25686. open: function () {
  25687. if (this.autoReset) {
  25688. //有可能还没有渲染
  25689. try {
  25690. this.reset();
  25691. } catch (e) {
  25692. this.render();
  25693. this.open()
  25694. }
  25695. }
  25696. this.showAtCenter();
  25697. if (this.iframeUrl) {
  25698. try {
  25699. this.getDom('iframe').focus();
  25700. } catch (ex) {}
  25701. }
  25702. activeDialog = this;
  25703. },
  25704. _onCloseButtonClick: function (evt, el) {
  25705. this.close(false);
  25706. },
  25707. close: function (ok) {
  25708. if (this.fireEvent('close', ok) !== false) {
  25709. //还原环境
  25710. if (this.fullscreen) {
  25711. document.documentElement.style.overflowX = this._originalContext.html.overflowX;
  25712. document.documentElement.style.overflowY = this._originalContext.html.overflowY;
  25713. document.body.style.overflowX = this._originalContext.body.overflowX;
  25714. document.body.style.overflowY = this._originalContext.body.overflowY;
  25715. delete this._originalContext;
  25716. }
  25717. this._hide();
  25718. //销毁content
  25719. var content = this.getDom('content');
  25720. var iframe = this.getDom('iframe');
  25721. if (content && iframe) {
  25722. var doc = iframe.contentDocument || iframe.contentWindow.document;
  25723. doc && (doc.body.innerHTML = '');
  25724. domUtils.remove(content);
  25725. }
  25726. }
  25727. }
  25728. };
  25729. utils.inherits(Dialog, UIBase);
  25730. })();
  25731. // ui/menubutton.js
  25732. ///import core
  25733. ///import uicore
  25734. ///import ui/menu.js
  25735. ///import ui/splitbutton.js
  25736. (function () {
  25737. var utils = baidu.editor.utils,
  25738. Menu = baidu.editor.ui.Menu,
  25739. SplitButton = baidu.editor.ui.SplitButton,
  25740. MenuButton = baidu.editor.ui.MenuButton = function (options) {
  25741. this.initOptions(options);
  25742. this.initMenuButton();
  25743. };
  25744. MenuButton.prototype = {
  25745. initMenuButton: function () {
  25746. var me = this;
  25747. this.uiName = "menubutton";
  25748. this.popup = new Menu({
  25749. items: me.items,
  25750. className: me.className,
  25751. editor: me.editor
  25752. });
  25753. this.popup.addListener('show', function () {
  25754. var list = this;
  25755. for (var i = 0; i < list.items.length; i++) {
  25756. list.items[i].removeState('checked');
  25757. if (list.items[i].value == me._value) {
  25758. list.items[i].addState('checked');
  25759. this.value = me._value;
  25760. }
  25761. }
  25762. });
  25763. this.initSplitButton();
  25764. },
  25765. setValue: function (value) {
  25766. this._value = value;
  25767. }
  25768. };
  25769. utils.inherits(MenuButton, SplitButton);
  25770. })();
  25771. // ui/multiMenu.js
  25772. ///import core
  25773. ///import uicore
  25774. ///commands 表情
  25775. (function () {
  25776. var utils = baidu.editor.utils,
  25777. Popup = baidu.editor.ui.Popup,
  25778. SplitButton = baidu.editor.ui.SplitButton,
  25779. MultiMenuPop = baidu.editor.ui.MultiMenuPop = function (options) {
  25780. this.initOptions(options);
  25781. this.initMultiMenu();
  25782. };
  25783. MultiMenuPop.prototype = {
  25784. initMultiMenu: function () {
  25785. var me = this;
  25786. this.popup = new Popup({
  25787. content: '',
  25788. editor: me.editor,
  25789. iframe_rendered: false,
  25790. onshow: function () {
  25791. if (!this.iframe_rendered) {
  25792. this.iframe_rendered = true;
  25793. this.getDom('content').innerHTML = '<iframe id="' + me.id + '_iframe" src="' + me.iframeUrl + '" frameborder="0"></iframe>';
  25794. me.editor.container.style.zIndex && (this.getDom().style.zIndex = me.editor.container.style.zIndex * 1 + 1);
  25795. }
  25796. }
  25797. // canSideUp:false,
  25798. // canSideLeft:false
  25799. });
  25800. this.onbuttonclick = function () {
  25801. this.showPopup();
  25802. };
  25803. this.initSplitButton();
  25804. }
  25805. };
  25806. utils.inherits(MultiMenuPop, SplitButton);
  25807. })();
  25808. // ui/shortcutmenu.js
  25809. (function () {
  25810. var UI = baidu.editor.ui,
  25811. UIBase = UI.UIBase,
  25812. uiUtils = UI.uiUtils,
  25813. utils = baidu.editor.utils,
  25814. domUtils = baidu.editor.dom.domUtils;
  25815. var allMenus = [], //存储所有快捷菜单
  25816. timeID,
  25817. isSubMenuShow = false; //是否有子pop显示
  25818. var ShortCutMenu = UI.ShortCutMenu = function (options) {
  25819. this.initOptions(options);
  25820. this.initShortCutMenu();
  25821. };
  25822. ShortCutMenu.postHide = hideAllMenu;
  25823. ShortCutMenu.prototype = {
  25824. isHidden: true,
  25825. SPACE: 5,
  25826. initShortCutMenu: function () {
  25827. this.items = this.items || [];
  25828. this.initUIBase();
  25829. this.initItems();
  25830. this.initEvent();
  25831. allMenus.push(this);
  25832. },
  25833. initEvent: function () {
  25834. var me = this,
  25835. doc = me.editor.document;
  25836. domUtils.on(doc, "mousemove", function (e) {
  25837. if (me.isHidden === false) {
  25838. //有pop显示就不隐藏快捷菜单
  25839. if (me.getSubMenuMark() || me.eventType == "contextmenu") return;
  25840. var flag = true,
  25841. el = me.getDom(),
  25842. wt = el.offsetWidth,
  25843. ht = el.offsetHeight,
  25844. distanceX = wt / 2 + me.SPACE, //距离中心X标准
  25845. distanceY = ht / 2, //距离中心Y标准
  25846. x = Math.abs(e.screenX - me.left), //离中心距离横坐标
  25847. y = Math.abs(e.screenY - me.top); //离中心距离纵坐标
  25848. clearTimeout(timeID);
  25849. timeID = setTimeout(function () {
  25850. if (y > 0 && y < distanceY) {
  25851. me.setOpacity(el, "1");
  25852. } else if (y > distanceY && y < distanceY + 70) {
  25853. me.setOpacity(el, "0.5");
  25854. flag = false;
  25855. } else if (y > distanceY + 70 && y < distanceY + 140) {
  25856. me.hide();
  25857. }
  25858. if (flag && x > 0 && x < distanceX) {
  25859. me.setOpacity(el, "1")
  25860. } else if (x > distanceX && x < distanceX + 70) {
  25861. me.setOpacity(el, "0.5")
  25862. } else if (x > distanceX + 70 && x < distanceX + 140) {
  25863. me.hide();
  25864. }
  25865. });
  25866. }
  25867. });
  25868. //ie\ff下 mouseout不准
  25869. if (browser.chrome) {
  25870. domUtils.on(doc, "mouseout", function (e) {
  25871. var relatedTgt = e.relatedTarget || e.toElement;
  25872. if (relatedTgt == null || relatedTgt.tagName == "HTML") {
  25873. me.hide();
  25874. }
  25875. });
  25876. }
  25877. me.editor.addListener("afterhidepop", function () {
  25878. if (!me.isHidden) {
  25879. isSubMenuShow = true;
  25880. }
  25881. });
  25882. },
  25883. initItems: function () {
  25884. if (utils.isArray(this.items)) {
  25885. for (var i = 0, len = this.items.length; i < len; i++) {
  25886. var item = this.items[i].toLowerCase();
  25887. if (UI[item]) {
  25888. this.items[i] = new UI[item](this.editor);
  25889. this.items[i].className += " edui-shortcutsubmenu ";
  25890. }
  25891. }
  25892. }
  25893. },
  25894. setOpacity: function (el, value) {
  25895. if (browser.ie && browser.version < 9) {
  25896. el.style.filter = "alpha(opacity = " + parseFloat(value) * 100 + ");"
  25897. } else {
  25898. el.style.opacity = value;
  25899. }
  25900. },
  25901. getSubMenuMark: function () {
  25902. isSubMenuShow = false;
  25903. var layerEle = uiUtils.getFixedLayer();
  25904. var list = domUtils.getElementsByTagName(layerEle, "div", function (node) {
  25905. return domUtils.hasClass(node, "edui-shortcutsubmenu edui-popup")
  25906. });
  25907. for (var i = 0, node; node = list[i++];) {
  25908. if (node.style.display != "none") {
  25909. isSubMenuShow = true;
  25910. }
  25911. }
  25912. return isSubMenuShow;
  25913. },
  25914. show: function (e, hasContextmenu) {
  25915. var me = this,
  25916. offset = {},
  25917. el = this.getDom(),
  25918. fixedlayer = uiUtils.getFixedLayer();
  25919. function setPos(offset) {
  25920. if (offset.left < 0) {
  25921. offset.left = 0;
  25922. }
  25923. if (offset.top < 0) {
  25924. offset.top = 0;
  25925. }
  25926. el.style.cssText = "position:absolute;left:" + offset.left + "px;top:" + offset.top + "px;";
  25927. }
  25928. function setPosByCxtMenu(menu) {
  25929. if (!menu.tagName) {
  25930. menu = menu.getDom();
  25931. }
  25932. offset.left = parseInt(menu.style.left);
  25933. offset.top = parseInt(menu.style.top);
  25934. offset.top -= el.offsetHeight + 15;
  25935. setPos(offset);
  25936. }
  25937. me.eventType = e.type;
  25938. el.style.cssText = "display:block;left:-9999px";
  25939. if (e.type == "contextmenu" && hasContextmenu) {
  25940. var menu = domUtils.getElementsByTagName(fixedlayer, "div", "edui-contextmenu")[0];
  25941. if (menu) {
  25942. setPosByCxtMenu(menu)
  25943. } else {
  25944. me.editor.addListener("aftershowcontextmenu", function (type, menu) {
  25945. setPosByCxtMenu(menu);
  25946. });
  25947. }
  25948. } else {
  25949. offset = uiUtils.getViewportOffsetByEvent(e);
  25950. offset.top -= el.offsetHeight + me.SPACE;
  25951. offset.left += me.SPACE + 20;
  25952. setPos(offset);
  25953. me.setOpacity(el, 0.2);
  25954. }
  25955. me.isHidden = false;
  25956. me.left = e.screenX + el.offsetWidth / 2 - me.SPACE;
  25957. me.top = e.screenY - (el.offsetHeight / 2) - me.SPACE;
  25958. if (me.editor) {
  25959. el.style.zIndex = me.editor.container.style.zIndex * 1 + 10;
  25960. fixedlayer.style.zIndex = el.style.zIndex - 1;
  25961. }
  25962. },
  25963. hide: function () {
  25964. if (this.getDom()) {
  25965. this.getDom().style.display = "none";
  25966. }
  25967. this.isHidden = true;
  25968. },
  25969. postRender: function () {
  25970. if (utils.isArray(this.items)) {
  25971. for (var i = 0, item; item = this.items[i++];) {
  25972. item.postRender();
  25973. }
  25974. }
  25975. },
  25976. getHtmlTpl: function () {
  25977. var buff;
  25978. if (utils.isArray(this.items)) {
  25979. buff = [];
  25980. for (var i = 0; i < this.items.length; i++) {
  25981. buff[i] = this.items[i].renderHtml();
  25982. }
  25983. buff = buff.join("");
  25984. } else {
  25985. buff = this.items;
  25986. }
  25987. return '<div id="##" class="%% edui-toolbar" data-src="shortcutmenu" onmousedown="return false;" onselectstart="return false;" >' +
  25988. buff +
  25989. '</div>';
  25990. }
  25991. };
  25992. utils.inherits(ShortCutMenu, UIBase);
  25993. function hideAllMenu(e) {
  25994. var tgt = e.target || e.srcElement,
  25995. cur = domUtils.findParent(tgt, function (node) {
  25996. return domUtils.hasClass(node, "edui-shortcutmenu") || domUtils.hasClass(node, "edui-popup");
  25997. }, true);
  25998. if (!cur) {
  25999. for (var i = 0, menu; menu = allMenus[i++];) {
  26000. menu.hide()
  26001. }
  26002. }
  26003. }
  26004. domUtils.on(document, 'mousedown', function (e) {
  26005. hideAllMenu(e);
  26006. });
  26007. domUtils.on(window, 'scroll', function (e) {
  26008. hideAllMenu(e);
  26009. });
  26010. })();
  26011. // ui/breakline.js
  26012. (function () {
  26013. var utils = baidu.editor.utils,
  26014. UIBase = baidu.editor.ui.UIBase,
  26015. Breakline = baidu.editor.ui.Breakline = function (options) {
  26016. this.initOptions(options);
  26017. this.initSeparator();
  26018. };
  26019. Breakline.prototype = {
  26020. uiName: 'Breakline',
  26021. initSeparator: function () {
  26022. this.initUIBase();
  26023. },
  26024. getHtmlTpl: function () {
  26025. return '<br/>';
  26026. }
  26027. };
  26028. utils.inherits(Breakline, UIBase);
  26029. })();
  26030. // ui/message.js
  26031. ///import core
  26032. ///import uicore
  26033. (function () {
  26034. var utils = baidu.editor.utils,
  26035. domUtils = baidu.editor.dom.domUtils,
  26036. UIBase = baidu.editor.ui.UIBase,
  26037. Message = baidu.editor.ui.Message = function (options) {
  26038. this.initOptions(options);
  26039. this.initMessage();
  26040. };
  26041. Message.prototype = {
  26042. initMessage: function () {
  26043. this.initUIBase();
  26044. },
  26045. getHtmlTpl: function () {
  26046. return '<div id="##" class="edui-message %%">' +
  26047. ' <div id="##_closer" class="edui-message-closer">×</div>' +
  26048. ' <div id="##_body" class="edui-message-body edui-message-type-info">' +
  26049. ' <iframe style="position:absolute;z-index:-1;left:0;top:0;background-color: transparent;" frameborder="0" width="100%" height="100%" src="about:blank"></iframe>' +
  26050. ' <div class="edui-shadow"></div>' +
  26051. ' <div id="##_content" class="edui-message-content">' +
  26052. ' </div>' +
  26053. ' </div>' +
  26054. '</div>';
  26055. },
  26056. reset: function (opt) {
  26057. var me = this;
  26058. if (!opt.keepshow) {
  26059. clearTimeout(this.timer);
  26060. me.timer = setTimeout(function () {
  26061. me.hide();
  26062. }, opt.timeout || 4000);
  26063. }
  26064. opt.content !== undefined && me.setContent(opt.content);
  26065. opt.type !== undefined && me.setType(opt.type);
  26066. me.show();
  26067. },
  26068. postRender: function () {
  26069. var me = this,
  26070. closer = this.getDom('closer');
  26071. closer && domUtils.on(closer, 'click', function () {
  26072. me.hide();
  26073. });
  26074. },
  26075. setContent: function (content) {
  26076. this.getDom('content').innerHTML = content;
  26077. },
  26078. setType: function (type) {
  26079. type = type || 'info';
  26080. var body = this.getDom('body');
  26081. body.className = body.className.replace(/edui-message-type-[\w-]+/, 'edui-message-type-' + type);
  26082. },
  26083. getContent: function () {
  26084. return this.getDom('content').innerHTML;
  26085. },
  26086. getType: function () {
  26087. var arr = this.getDom('body').match(/edui-message-type-([\w-]+)/);
  26088. return arr ? arr[1] : '';
  26089. },
  26090. show: function () {
  26091. this.getDom().style.display = 'block';
  26092. },
  26093. hide: function () {
  26094. var dom = this.getDom();
  26095. if (dom) {
  26096. dom.style.display = 'none';
  26097. dom.parentNode && dom.parentNode.removeChild(dom);
  26098. }
  26099. }
  26100. };
  26101. utils.inherits(Message, UIBase);
  26102. })();
  26103. // adapter/editorui.js
  26104. //ui跟编辑器的适配層
  26105. //那个按钮弹出是dialog,是下拉筐等都是在这个js中配置
  26106. //自己写的ui也要在这里配置,放到baidu.editor.ui下边,当编辑器实例化的时候会根据ueditor.config中的toolbars找到相应的进行实例化
  26107. (function () {
  26108. var utils = baidu.editor.utils;
  26109. var editorui = baidu.editor.ui;
  26110. var _Dialog = editorui.Dialog;
  26111. editorui.buttons = {};
  26112. editorui.Dialog = function (options) {
  26113. var dialog = new _Dialog(options);
  26114. dialog.addListener('hide', function () {
  26115. if (dialog.editor) {
  26116. var editor = dialog.editor;
  26117. try {
  26118. if (browser.gecko) {
  26119. var y = editor.window.scrollY,
  26120. x = editor.window.scrollX;
  26121. editor.body.focus();
  26122. editor.window.scrollTo(x, y);
  26123. } else {
  26124. editor.focus();
  26125. }
  26126. } catch (ex) {}
  26127. }
  26128. });
  26129. return dialog;
  26130. };
  26131. var iframeUrlMap = {
  26132. 'anchor': '~/dialogs/anchor/anchor.html',
  26133. 'insertimage': '~/dialogs/image/image.html',
  26134. 'link': '~/dialogs/link/link.html',
  26135. 'spechars': '~/dialogs/spechars/spechars.html',
  26136. 'searchreplace': '~/dialogs/searchreplace/searchreplace.html',
  26137. 'map': '~/dialogs/map/map.html',
  26138. 'gmap': '~/dialogs/gmap/gmap.html',
  26139. 'insertvideo': '~/dialogs/video/video.html',
  26140. 'help': '~/dialogs/help/help.html',
  26141. 'preview': '~/dialogs/preview/preview.html',
  26142. 'emotion': '~/dialogs/emotion/emotion.html',
  26143. 'wordimage': '~/dialogs/wordimage/wordimage.html',
  26144. 'attachment': '~/dialogs/attachment/attachment.html',
  26145. 'insertframe': '~/dialogs/insertframe/insertframe.html',
  26146. 'edittip': '~/dialogs/table/edittip.html',
  26147. 'edittable': '~/dialogs/table/edittable.html',
  26148. 'edittd': '~/dialogs/table/edittd.html',
  26149. 'webapp': '~/dialogs/webapp/webapp.html',
  26150. 'snapscreen': '~/dialogs/snapscreen/snapscreen.html',
  26151. 'scrawl': '~/dialogs/scrawl/scrawl.html',
  26152. 'music': '~/dialogs/music/music.html',
  26153. 'template': '~/dialogs/template/template.html',
  26154. 'background': '~/dialogs/background/background.html',
  26155. 'charts': '~/dialogs/charts/charts.html'
  26156. };
  26157. //为工具栏添加按钮,以下都是统一的按钮触发命令,所以写在一起
  26158. var btnCmds = ['undo', 'redo', 'formatmatch',
  26159. 'bold', 'italic', 'underline', 'fontborder', 'touppercase', 'tolowercase',
  26160. 'strikethrough', 'subscript', 'superscript', 'source', 'indent', 'outdent',
  26161. 'blockquote', 'pasteplain', 'pagebreak',
  26162. 'selectall', 'print', 'horizontal', 'removeformat', 'time', 'date', 'unlink',
  26163. 'insertparagraphbeforetable', 'insertrow', 'insertcol', 'mergeright', 'mergedown', 'deleterow',
  26164. 'deletecol', 'splittorows', 'splittocols', 'splittocells', 'mergecells', 'deletetable', 'drafts'
  26165. ];
  26166. for (var i = 0, ci; ci = btnCmds[i++];) {
  26167. ci = ci.toLowerCase();
  26168. editorui[ci] = function (cmd) {
  26169. return function (editor) {
  26170. var ui = new editorui.Button({
  26171. className: 'edui-for-' + cmd,
  26172. title: editor.options.labelMap[cmd] || editor.getLang("labelMap." + cmd) || '',
  26173. onclick: function () {
  26174. editor.execCommand(cmd);
  26175. },
  26176. theme: editor.options.theme,
  26177. showText: false
  26178. });
  26179. editorui.buttons[cmd] = ui;
  26180. editor.addListener('selectionchange', function (type, causeByUi, uiReady) {
  26181. var state = editor.queryCommandState(cmd);
  26182. if (state == -1) {
  26183. ui.setDisabled(true);
  26184. ui.setChecked(false);
  26185. } else {
  26186. if (!uiReady) {
  26187. ui.setDisabled(false);
  26188. ui.setChecked(state);
  26189. }
  26190. }
  26191. });
  26192. return ui;
  26193. };
  26194. }(ci);
  26195. }
  26196. //清除文档
  26197. editorui.cleardoc = function (editor) {
  26198. var ui = new editorui.Button({
  26199. className: 'edui-for-cleardoc',
  26200. title: editor.options.labelMap.cleardoc || editor.getLang("labelMap.cleardoc") || '',
  26201. theme: editor.options.theme,
  26202. onclick: function () {
  26203. if (confirm(editor.getLang("confirmClear"))) {
  26204. editor.execCommand('cleardoc');
  26205. }
  26206. }
  26207. });
  26208. editorui.buttons["cleardoc"] = ui;
  26209. editor.addListener('selectionchange', function () {
  26210. ui.setDisabled(editor.queryCommandState('cleardoc') == -1);
  26211. });
  26212. return ui;
  26213. };
  26214. //排版,图片排版,文字方向
  26215. var typeset = {
  26216. 'justify': ['left', 'right', 'center', 'justify'],
  26217. 'imagefloat': ['none', 'left', 'center', 'right'],
  26218. 'directionality': ['ltr', 'rtl']
  26219. };
  26220. for (var p in typeset) {
  26221. (function (cmd, val) {
  26222. for (var i = 0, ci; ci = val[i++];) {
  26223. (function (cmd2) {
  26224. editorui[cmd.replace('float', '') + cmd2] = function (editor) {
  26225. var ui = new editorui.Button({
  26226. className: 'edui-for-' + cmd.replace('float', '') + cmd2,
  26227. title: editor.options.labelMap[cmd.replace('float', '') + cmd2] || editor.getLang("labelMap." + cmd.replace('float', '') + cmd2) || '',
  26228. theme: editor.options.theme,
  26229. onclick: function () {
  26230. editor.execCommand(cmd, cmd2);
  26231. }
  26232. });
  26233. editorui.buttons[cmd] = ui;
  26234. editor.addListener('selectionchange', function (type, causeByUi, uiReady) {
  26235. ui.setDisabled(editor.queryCommandState(cmd) == -1);
  26236. ui.setChecked(editor.queryCommandValue(cmd) == cmd2 && !uiReady);
  26237. });
  26238. return ui;
  26239. };
  26240. })(ci)
  26241. }
  26242. })(p, typeset[p])
  26243. }
  26244. //字体颜色和背景颜色
  26245. for (var i = 0, ci; ci = ['backcolor', 'forecolor'][i++];) {
  26246. editorui[ci] = function (cmd) {
  26247. return function (editor) {
  26248. var ui = new editorui.ColorButton({
  26249. className: 'edui-for-' + cmd,
  26250. color: 'default',
  26251. title: editor.options.labelMap[cmd] || editor.getLang("labelMap." + cmd) || '',
  26252. editor: editor,
  26253. onpickcolor: function (t, color) {
  26254. editor.execCommand(cmd, color);
  26255. },
  26256. onpicknocolor: function () {
  26257. editor.execCommand(cmd, 'default');
  26258. this.setColor('transparent');
  26259. this.color = 'default';
  26260. },
  26261. onbuttonclick: function () {
  26262. editor.execCommand(cmd, this.color);
  26263. }
  26264. });
  26265. editorui.buttons[cmd] = ui;
  26266. editor.addListener('selectionchange', function () {
  26267. ui.setDisabled(editor.queryCommandState(cmd) == -1);
  26268. });
  26269. return ui;
  26270. };
  26271. }(ci);
  26272. }
  26273. var dialogBtns = {
  26274. noOk: ['searchreplace', 'help', 'spechars', 'webapp', 'preview'],
  26275. ok: ['attachment', 'anchor', 'link', 'insertimage', 'map', 'gmap', 'insertframe', 'wordimage',
  26276. 'insertvideo', 'insertframe', 'edittip', 'edittable', 'edittd', 'scrawl', 'template', 'music', 'background', 'charts'
  26277. ]
  26278. };
  26279. for (var p in dialogBtns) {
  26280. (function (type, vals) {
  26281. for (var i = 0, ci; ci = vals[i++];) {
  26282. //todo opera下存在问题
  26283. if (browser.opera && ci === "searchreplace") {
  26284. continue;
  26285. }
  26286. (function (cmd) {
  26287. editorui[cmd] = function (editor, iframeUrl, title) {
  26288. iframeUrl = iframeUrl || (editor.options.iframeUrlMap || {})[cmd] || iframeUrlMap[cmd];
  26289. title = editor.options.labelMap[cmd] || editor.getLang("labelMap." + cmd) || '';
  26290. var dialog;
  26291. //没有iframeUrl不创建dialog
  26292. if (iframeUrl) {
  26293. dialog = new editorui.Dialog(utils.extend({
  26294. iframeUrl: editor.ui.mapUrl(iframeUrl),
  26295. editor: editor,
  26296. className: 'edui-for-' + cmd,
  26297. title: title,
  26298. holdScroll: cmd === 'insertimage',
  26299. fullscreen: /charts|preview/.test(cmd),
  26300. closeDialog: editor.getLang("closeDialog")
  26301. }, type == 'ok' ? {
  26302. buttons: [{
  26303. className: 'edui-okbutton',
  26304. label: editor.getLang("ok"),
  26305. editor: editor,
  26306. onclick: function () {
  26307. dialog.close(true);
  26308. }
  26309. },
  26310. {
  26311. className: 'edui-cancelbutton',
  26312. label: editor.getLang("cancel"),
  26313. editor: editor,
  26314. onclick: function () {
  26315. dialog.close(false);
  26316. }
  26317. }
  26318. ]
  26319. } : {}));
  26320. editor.ui._dialogs[cmd + "Dialog"] = dialog;
  26321. }
  26322. var ui = new editorui.Button({
  26323. className: 'edui-for-' + cmd,
  26324. title: title,
  26325. onclick: function () {
  26326. if (dialog) {
  26327. switch (cmd) {
  26328. case "wordimage":
  26329. var images = editor.execCommand("wordimage");
  26330. if (images && images.length) {
  26331. dialog.render();
  26332. dialog.open();
  26333. }
  26334. break;
  26335. case "scrawl":
  26336. if (editor.queryCommandState("scrawl") != -1) {
  26337. dialog.render();
  26338. dialog.open();
  26339. }
  26340. break;
  26341. default:
  26342. dialog.render();
  26343. dialog.open();
  26344. }
  26345. }
  26346. },
  26347. theme: editor.options.theme,
  26348. disabled: (cmd == 'scrawl' && editor.queryCommandState("scrawl") == -1) || (cmd == 'charts')
  26349. });
  26350. editorui.buttons[cmd] = ui;
  26351. editor.addListener('selectionchange', function () {
  26352. //只存在于右键菜单而无工具栏按钮的ui不需要检测状态
  26353. var unNeedCheckState = {
  26354. 'edittable': 1
  26355. };
  26356. if (cmd in unNeedCheckState) return;
  26357. var state = editor.queryCommandState(cmd);
  26358. if (ui.getDom()) {
  26359. ui.setDisabled(state == -1);
  26360. ui.setChecked(state);
  26361. }
  26362. });
  26363. return ui;
  26364. };
  26365. })(ci.toLowerCase())
  26366. }
  26367. })(p, dialogBtns[p]);
  26368. }
  26369. editorui.snapscreen = function (editor, iframeUrl, title) {
  26370. title = editor.options.labelMap['snapscreen'] || editor.getLang("labelMap.snapscreen") || '';
  26371. var ui = new editorui.Button({
  26372. className: 'edui-for-snapscreen',
  26373. title: title,
  26374. onclick: function () {
  26375. editor.execCommand("snapscreen");
  26376. },
  26377. theme: editor.options.theme
  26378. });
  26379. editorui.buttons['snapscreen'] = ui;
  26380. iframeUrl = iframeUrl || (editor.options.iframeUrlMap || {})["snapscreen"] || iframeUrlMap["snapscreen"];
  26381. if (iframeUrl) {
  26382. var dialog = new editorui.Dialog({
  26383. iframeUrl: editor.ui.mapUrl(iframeUrl),
  26384. editor: editor,
  26385. className: 'edui-for-snapscreen',
  26386. title: title,
  26387. buttons: [{
  26388. className: 'edui-okbutton',
  26389. label: editor.getLang("ok"),
  26390. editor: editor,
  26391. onclick: function () {
  26392. dialog.close(true);
  26393. }
  26394. },
  26395. {
  26396. className: 'edui-cancelbutton',
  26397. label: editor.getLang("cancel"),
  26398. editor: editor,
  26399. onclick: function () {
  26400. dialog.close(false);
  26401. }
  26402. }
  26403. ]
  26404. });
  26405. dialog.render();
  26406. editor.ui._dialogs["snapscreenDialog"] = dialog;
  26407. }
  26408. editor.addListener('selectionchange', function () {
  26409. ui.setDisabled(editor.queryCommandState('snapscreen') == -1);
  26410. });
  26411. return ui;
  26412. };
  26413. editorui.insertcode = function (editor, list, title) {
  26414. list = editor.options['insertcode'] || [];
  26415. title = editor.options.labelMap['insertcode'] || editor.getLang("labelMap.insertcode") || '';
  26416. // if (!list.length) return;
  26417. var items = [];
  26418. utils.each(list, function (key, val) {
  26419. items.push({
  26420. label: key,
  26421. value: val,
  26422. theme: editor.options.theme,
  26423. renderLabelHtml: function () {
  26424. return '<div class="edui-label %%-label" >' + (this.label || '') + '</div>';
  26425. }
  26426. });
  26427. });
  26428. var ui = new editorui.Combox({
  26429. editor: editor,
  26430. items: items,
  26431. onselect: function (t, index) {
  26432. editor.execCommand('insertcode', this.items[index].value);
  26433. },
  26434. onbuttonclick: function () {
  26435. this.showPopup();
  26436. },
  26437. title: title,
  26438. initValue: title,
  26439. className: 'edui-for-insertcode',
  26440. indexByValue: function (value) {
  26441. if (value) {
  26442. for (var i = 0, ci; ci = this.items[i]; i++) {
  26443. if (ci.value.indexOf(value) != -1)
  26444. return i;
  26445. }
  26446. }
  26447. return -1;
  26448. }
  26449. });
  26450. editorui.buttons['insertcode'] = ui;
  26451. editor.addListener('selectionchange', function (type, causeByUi, uiReady) {
  26452. if (!uiReady) {
  26453. var state = editor.queryCommandState('insertcode');
  26454. if (state == -1) {
  26455. ui.setDisabled(true);
  26456. } else {
  26457. ui.setDisabled(false);
  26458. var value = editor.queryCommandValue('insertcode');
  26459. if (!value) {
  26460. ui.setValue(title);
  26461. return;
  26462. }
  26463. //trace:1871 ie下从源码模式切换回来时,字体会带单引号,而且会有逗号
  26464. value && (value = value.replace(/['"]/g, '').split(',')[0]);
  26465. ui.setValue(value);
  26466. }
  26467. }
  26468. });
  26469. return ui;
  26470. };
  26471. editorui.fontfamily = function (editor, list, title) {
  26472. list = editor.options['fontfamily'] || [];
  26473. title = editor.options.labelMap['fontfamily'] || editor.getLang("labelMap.fontfamily") || '';
  26474. if (!list.length) return;
  26475. for (var i = 0, ci, items = []; ci = list[i]; i++) {
  26476. var langLabel = editor.getLang('fontfamily')[ci.name] || "";
  26477. (function (key, val) {
  26478. items.push({
  26479. label: key,
  26480. value: val,
  26481. theme: editor.options.theme,
  26482. renderLabelHtml: function () {
  26483. return '<div class="edui-label %%-label" style="font-family:' +
  26484. utils.unhtml(this.value) + '">' + (this.label || '') + '</div>';
  26485. }
  26486. });
  26487. })(ci.label || langLabel, ci.val)
  26488. }
  26489. var ui = new editorui.Combox({
  26490. editor: editor,
  26491. items: items,
  26492. onselect: function (t, index) {
  26493. editor.execCommand('FontFamily', this.items[index].value);
  26494. },
  26495. onbuttonclick: function () {
  26496. this.showPopup();
  26497. },
  26498. title: title,
  26499. initValue: title,
  26500. className: 'edui-for-fontfamily',
  26501. indexByValue: function (value) {
  26502. if (value) {
  26503. for (var i = 0, ci; ci = this.items[i]; i++) {
  26504. if (ci.value.indexOf(value) != -1)
  26505. return i;
  26506. }
  26507. }
  26508. return -1;
  26509. }
  26510. });
  26511. editorui.buttons['fontfamily'] = ui;
  26512. editor.addListener('selectionchange', function (type, causeByUi, uiReady) {
  26513. if (!uiReady) {
  26514. var state = editor.queryCommandState('FontFamily');
  26515. if (state == -1) {
  26516. ui.setDisabled(true);
  26517. } else {
  26518. ui.setDisabled(false);
  26519. var value = editor.queryCommandValue('FontFamily');
  26520. //trace:1871 ie下从源码模式切换回来时,字体会带单引号,而且会有逗号
  26521. value && (value = value.replace(/['"]/g, '').split(',')[0]);
  26522. ui.setValue(value);
  26523. }
  26524. }
  26525. });
  26526. return ui;
  26527. };
  26528. editorui.fontsize = function (editor, list, title) {
  26529. title = editor.options.labelMap['fontsize'] || editor.getLang("labelMap.fontsize") || '';
  26530. list = list || editor.options['fontsize'] || [];
  26531. if (!list.length) return;
  26532. var items = [];
  26533. for (var i = 0; i < list.length; i++) {
  26534. var size = list[i] + 'px';
  26535. items.push({
  26536. label: size,
  26537. value: size,
  26538. theme: editor.options.theme,
  26539. renderLabelHtml: function () {
  26540. return '<div class="edui-label %%-label" style="line-height:1;font-size:' +
  26541. this.value + '">' + (this.label || '') + '</div>';
  26542. }
  26543. });
  26544. }
  26545. var ui = new editorui.Combox({
  26546. editor: editor,
  26547. items: items,
  26548. title: title,
  26549. initValue: title,
  26550. onselect: function (t, index) {
  26551. editor.execCommand('FontSize', this.items[index].value);
  26552. },
  26553. onbuttonclick: function () {
  26554. this.showPopup();
  26555. },
  26556. className: 'edui-for-fontsize'
  26557. });
  26558. editorui.buttons['fontsize'] = ui;
  26559. editor.addListener('selectionchange', function (type, causeByUi, uiReady) {
  26560. if (!uiReady) {
  26561. var state = editor.queryCommandState('FontSize');
  26562. if (state == -1) {
  26563. ui.setDisabled(true);
  26564. } else {
  26565. ui.setDisabled(false);
  26566. ui.setValue(editor.queryCommandValue('FontSize'));
  26567. }
  26568. }
  26569. });
  26570. return ui;
  26571. };
  26572. editorui.paragraph = function (editor, list, title) {
  26573. title = editor.options.labelMap['paragraph'] || editor.getLang("labelMap.paragraph") || '';
  26574. list = editor.options['paragraph'] || [];
  26575. if (utils.isEmptyObject(list)) return;
  26576. var items = [];
  26577. for (var i in list) {
  26578. items.push({
  26579. value: i,
  26580. label: list[i] || editor.getLang("paragraph")[i],
  26581. theme: editor.options.theme,
  26582. renderLabelHtml: function () {
  26583. return '<div class="edui-label %%-label"><span class="edui-for-' + this.value + '">' + (this.label || '') + '</span></div>';
  26584. }
  26585. })
  26586. }
  26587. var ui = new editorui.Combox({
  26588. editor: editor,
  26589. items: items,
  26590. title: title,
  26591. initValue: title,
  26592. className: 'edui-for-paragraph',
  26593. onselect: function (t, index) {
  26594. editor.execCommand('Paragraph', this.items[index].value);
  26595. },
  26596. onbuttonclick: function () {
  26597. this.showPopup();
  26598. }
  26599. });
  26600. editorui.buttons['paragraph'] = ui;
  26601. editor.addListener('selectionchange', function (type, causeByUi, uiReady) {
  26602. if (!uiReady) {
  26603. var state = editor.queryCommandState('Paragraph');
  26604. if (state == -1) {
  26605. ui.setDisabled(true);
  26606. } else {
  26607. ui.setDisabled(false);
  26608. var value = editor.queryCommandValue('Paragraph');
  26609. var index = ui.indexByValue(value);
  26610. if (index != -1) {
  26611. ui.setValue(value);
  26612. } else {
  26613. ui.setValue(ui.initValue);
  26614. }
  26615. }
  26616. }
  26617. });
  26618. return ui;
  26619. };
  26620. //自定义标题
  26621. editorui.customstyle = function (editor) {
  26622. var list = editor.options['customstyle'] || [],
  26623. title = editor.options.labelMap['customstyle'] || editor.getLang("labelMap.customstyle") || '';
  26624. if (!list.length) return;
  26625. var langCs = editor.getLang('customstyle');
  26626. for (var i = 0, items = [], t; t = list[i++];) {
  26627. (function (t) {
  26628. var ck = {};
  26629. ck.label = t.label ? t.label : langCs[t.name];
  26630. ck.style = t.style;
  26631. ck.className = t.className;
  26632. ck.tag = t.tag;
  26633. items.push({
  26634. label: ck.label,
  26635. value: ck,
  26636. theme: editor.options.theme,
  26637. renderLabelHtml: function () {
  26638. return '<div class="edui-label %%-label">' + '<' + ck.tag + ' ' + (ck.className ? ' class="' + ck.className + '"' : "") +
  26639. (ck.style ? ' style="' + ck.style + '"' : "") + '>' + ck.label + "<\/" + ck.tag + ">" +
  26640. '</div>';
  26641. }
  26642. });
  26643. })(t);
  26644. }
  26645. var ui = new editorui.Combox({
  26646. editor: editor,
  26647. items: items,
  26648. title: title,
  26649. initValue: title,
  26650. className: 'edui-for-customstyle',
  26651. onselect: function (t, index) {
  26652. editor.execCommand('customstyle', this.items[index].value);
  26653. },
  26654. onbuttonclick: function () {
  26655. this.showPopup();
  26656. },
  26657. indexByValue: function (value) {
  26658. for (var i = 0, ti; ti = this.items[i++];) {
  26659. if (ti.label == value) {
  26660. return i - 1
  26661. }
  26662. }
  26663. return -1;
  26664. }
  26665. });
  26666. editorui.buttons['customstyle'] = ui;
  26667. editor.addListener('selectionchange', function (type, causeByUi, uiReady) {
  26668. if (!uiReady) {
  26669. var state = editor.queryCommandState('customstyle');
  26670. if (state == -1) {
  26671. ui.setDisabled(true);
  26672. } else {
  26673. ui.setDisabled(false);
  26674. var value = editor.queryCommandValue('customstyle');
  26675. var index = ui.indexByValue(value);
  26676. if (index != -1) {
  26677. ui.setValue(value);
  26678. } else {
  26679. ui.setValue(ui.initValue);
  26680. }
  26681. }
  26682. }
  26683. });
  26684. return ui;
  26685. };
  26686. editorui.inserttable = function (editor, iframeUrl, title) {
  26687. title = editor.options.labelMap['inserttable'] || editor.getLang("labelMap.inserttable") || '';
  26688. var ui = new editorui.TableButton({
  26689. editor: editor,
  26690. title: title,
  26691. className: 'edui-for-inserttable',
  26692. onpicktable: function (t, numCols, numRows) {
  26693. editor.execCommand('InsertTable', {
  26694. numRows: numRows,
  26695. numCols: numCols,
  26696. border: 1
  26697. });
  26698. },
  26699. onbuttonclick: function () {
  26700. this.showPopup();
  26701. }
  26702. });
  26703. editorui.buttons['inserttable'] = ui;
  26704. editor.addListener('selectionchange', function () {
  26705. ui.setDisabled(editor.queryCommandState('inserttable') == -1);
  26706. });
  26707. return ui;
  26708. };
  26709. editorui.lineheight = function (editor) {
  26710. var val = editor.options.lineheight || [];
  26711. if (!val.length) return;
  26712. for (var i = 0, ci, items = []; ci = val[i++];) {
  26713. items.push({
  26714. //todo:写死了
  26715. label: ci,
  26716. value: ci,
  26717. theme: editor.options.theme,
  26718. onclick: function () {
  26719. editor.execCommand("lineheight", this.value);
  26720. }
  26721. })
  26722. }
  26723. var ui = new editorui.MenuButton({
  26724. editor: editor,
  26725. className: 'edui-for-lineheight',
  26726. title: editor.options.labelMap['lineheight'] || editor.getLang("labelMap.lineheight") || '',
  26727. items: items,
  26728. onbuttonclick: function () {
  26729. var value = editor.queryCommandValue('LineHeight') || this.value;
  26730. editor.execCommand("LineHeight", value);
  26731. }
  26732. });
  26733. editorui.buttons['lineheight'] = ui;
  26734. editor.addListener('selectionchange', function () {
  26735. var state = editor.queryCommandState('LineHeight');
  26736. if (state == -1) {
  26737. ui.setDisabled(true);
  26738. } else {
  26739. ui.setDisabled(false);
  26740. var value = editor.queryCommandValue('LineHeight');
  26741. value && ui.setValue((value + '').replace(/cm/, ''));
  26742. ui.setChecked(state)
  26743. }
  26744. });
  26745. return ui;
  26746. };
  26747. var rowspacings = ['top', 'bottom'];
  26748. for (var r = 0, ri; ri = rowspacings[r++];) {
  26749. (function (cmd) {
  26750. editorui['rowspacing' + cmd] = function (editor) {
  26751. var val = editor.options['rowspacing' + cmd] || [];
  26752. if (!val.length) return null;
  26753. for (var i = 0, ci, items = []; ci = val[i++];) {
  26754. items.push({
  26755. label: ci,
  26756. value: ci,
  26757. theme: editor.options.theme,
  26758. onclick: function () {
  26759. editor.execCommand("rowspacing", this.value, cmd);
  26760. }
  26761. })
  26762. }
  26763. var ui = new editorui.MenuButton({
  26764. editor: editor,
  26765. className: 'edui-for-rowspacing' + cmd,
  26766. title: editor.options.labelMap['rowspacing' + cmd] || editor.getLang("labelMap.rowspacing" + cmd) || '',
  26767. items: items,
  26768. onbuttonclick: function () {
  26769. var value = editor.queryCommandValue('rowspacing', cmd) || this.value;
  26770. editor.execCommand("rowspacing", value, cmd);
  26771. }
  26772. });
  26773. editorui.buttons[cmd] = ui;
  26774. editor.addListener('selectionchange', function () {
  26775. var state = editor.queryCommandState('rowspacing', cmd);
  26776. if (state == -1) {
  26777. ui.setDisabled(true);
  26778. } else {
  26779. ui.setDisabled(false);
  26780. var value = editor.queryCommandValue('rowspacing', cmd);
  26781. value && ui.setValue((value + '').replace(/%/, ''));
  26782. ui.setChecked(state)
  26783. }
  26784. });
  26785. return ui;
  26786. }
  26787. })(ri)
  26788. }
  26789. //有序,无序列表
  26790. var lists = ['insertorderedlist', 'insertunorderedlist'];
  26791. for (var l = 0, cl; cl = lists[l++];) {
  26792. (function (cmd) {
  26793. editorui[cmd] = function (editor) {
  26794. var vals = editor.options[cmd],
  26795. _onMenuClick = function () {
  26796. editor.execCommand(cmd, this.value);
  26797. },
  26798. items = [];
  26799. for (var i in vals) {
  26800. items.push({
  26801. label: vals[i] || editor.getLang()[cmd][i] || "",
  26802. value: i,
  26803. theme: editor.options.theme,
  26804. onclick: _onMenuClick
  26805. })
  26806. }
  26807. var ui = new editorui.MenuButton({
  26808. editor: editor,
  26809. className: 'edui-for-' + cmd,
  26810. title: editor.getLang("labelMap." + cmd) || '',
  26811. 'items': items,
  26812. onbuttonclick: function () {
  26813. var value = editor.queryCommandValue(cmd) || this.value;
  26814. editor.execCommand(cmd, value);
  26815. }
  26816. });
  26817. editorui.buttons[cmd] = ui;
  26818. editor.addListener('selectionchange', function () {
  26819. var state = editor.queryCommandState(cmd);
  26820. if (state == -1) {
  26821. ui.setDisabled(true);
  26822. } else {
  26823. ui.setDisabled(false);
  26824. var value = editor.queryCommandValue(cmd);
  26825. ui.setValue(value);
  26826. ui.setChecked(state)
  26827. }
  26828. });
  26829. return ui;
  26830. };
  26831. })(cl)
  26832. }
  26833. editorui.fullscreen = function (editor, title) {
  26834. title = editor.options.labelMap['fullscreen'] || editor.getLang("labelMap.fullscreen") || '';
  26835. var ui = new editorui.Button({
  26836. className: 'edui-for-fullscreen',
  26837. title: title,
  26838. theme: editor.options.theme,
  26839. onclick: function () {
  26840. if (editor.ui) {
  26841. editor.ui.setFullScreen(!editor.ui.isFullScreen());
  26842. }
  26843. this.setChecked(editor.ui.isFullScreen());
  26844. }
  26845. });
  26846. editorui.buttons['fullscreen'] = ui;
  26847. editor.addListener('selectionchange', function () {
  26848. var state = editor.queryCommandState('fullscreen');
  26849. ui.setDisabled(state == -1);
  26850. ui.setChecked(editor.ui.isFullScreen());
  26851. });
  26852. return ui;
  26853. };
  26854. // 表情
  26855. editorui["emotion"] = function (editor, iframeUrl) {
  26856. var cmd = "emotion";
  26857. var ui = new editorui.MultiMenuPop({
  26858. title: editor.options.labelMap[cmd] || editor.getLang("labelMap." + cmd + "") || '',
  26859. editor: editor,
  26860. className: 'edui-for-' + cmd,
  26861. iframeUrl: editor.ui.mapUrl(iframeUrl || (editor.options.iframeUrlMap || {})[cmd] || iframeUrlMap[cmd])
  26862. });
  26863. editorui.buttons[cmd] = ui;
  26864. editor.addListener('selectionchange', function () {
  26865. ui.setDisabled(editor.queryCommandState(cmd) == -1)
  26866. });
  26867. return ui;
  26868. };
  26869. editorui.autotypeset = function (editor) {
  26870. var ui = new editorui.AutoTypeSetButton({
  26871. editor: editor,
  26872. title: editor.options.labelMap['autotypeset'] || editor.getLang("labelMap.autotypeset") || '',
  26873. className: 'edui-for-autotypeset',
  26874. onbuttonclick: function () {
  26875. editor.execCommand('autotypeset')
  26876. }
  26877. });
  26878. editorui.buttons['autotypeset'] = ui;
  26879. editor.addListener('selectionchange', function () {
  26880. ui.setDisabled(editor.queryCommandState('autotypeset') == -1);
  26881. });
  26882. return ui;
  26883. };
  26884. /* 简单上传插件 */
  26885. editorui["simpleupload"] = function (editor) {
  26886. var name = 'simpleupload',
  26887. ui = new editorui.Button({
  26888. className: 'edui-for-' + name,
  26889. title: editor.options.labelMap[name] || editor.getLang("labelMap." + name) || '',
  26890. onclick: function () {},
  26891. theme: editor.options.theme,
  26892. showText: false
  26893. });
  26894. editorui.buttons[name] = ui;
  26895. editor.addListener('ready', function () {
  26896. var b = ui.getDom('body'),
  26897. iconSpan = b.children[0];
  26898. editor.fireEvent('simpleuploadbtnready', iconSpan);
  26899. });
  26900. editor.addListener('selectionchange', function (type, causeByUi, uiReady) {
  26901. var state = editor.queryCommandState(name);
  26902. if (state == -1) {
  26903. ui.setDisabled(true);
  26904. ui.setChecked(false);
  26905. } else {
  26906. if (!uiReady) {
  26907. ui.setDisabled(false);
  26908. ui.setChecked(state);
  26909. }
  26910. }
  26911. });
  26912. return ui;
  26913. };
  26914. })();
  26915. // adapter/editor.js
  26916. ///import core
  26917. ///commands 全屏
  26918. ///commandsName FullScreen
  26919. ///commandsTitle 全屏
  26920. (function () {
  26921. var utils = baidu.editor.utils,
  26922. uiUtils = baidu.editor.ui.uiUtils,
  26923. UIBase = baidu.editor.ui.UIBase,
  26924. domUtils = baidu.editor.dom.domUtils;
  26925. var nodeStack = [];
  26926. function EditorUI(options) {
  26927. this.initOptions(options);
  26928. this.initEditorUI();
  26929. }
  26930. EditorUI.prototype = {
  26931. uiName: 'editor',
  26932. initEditorUI: function () {
  26933. this.editor.ui = this;
  26934. this._dialogs = {};
  26935. this.initUIBase();
  26936. this._initToolbars();
  26937. var editor = this.editor,
  26938. me = this;
  26939. editor.addListener('ready', function () {
  26940. //提供getDialog方法
  26941. editor.getDialog = function (name) {
  26942. return editor.ui._dialogs[name + "Dialog"];
  26943. };
  26944. domUtils.on(editor.window, 'scroll', function (evt) {
  26945. baidu.editor.ui.Popup.postHide(evt);
  26946. });
  26947. //提供编辑器实时宽高(全屏时宽高不变化)
  26948. editor.ui._actualFrameWidth = editor.options.initialFrameWidth;
  26949. UE.browser.ie && UE.browser.version === 6 && editor.container.ownerDocument.execCommand("BackgroundImageCache", false, true);
  26950. //display bottom-bar label based on config
  26951. if (editor.options.elementPathEnabled) {
  26952. editor.ui.getDom('elementpath').innerHTML = '<div class="edui-editor-breadcrumb">' + editor.getLang("elementPathTip") + ':</div>';
  26953. }
  26954. if (editor.options.wordCount) {
  26955. function countFn() {
  26956. setCount(editor, me);
  26957. domUtils.un(editor.document, "click", arguments.callee);
  26958. }
  26959. domUtils.on(editor.document, "click", countFn);
  26960. editor.ui.getDom('wordcount').innerHTML = editor.getLang("wordCountTip");
  26961. }
  26962. editor.ui._scale();
  26963. if (editor.options.scaleEnabled) {
  26964. if (editor.autoHeightEnabled) {
  26965. editor.disableAutoHeight();
  26966. }
  26967. me.enableScale();
  26968. } else {
  26969. me.disableScale();
  26970. }
  26971. if (!editor.options.elementPathEnabled && !editor.options.wordCount && !editor.options.scaleEnabled) {
  26972. editor.ui.getDom('elementpath').style.display = "none";
  26973. editor.ui.getDom('wordcount').style.display = "none";
  26974. editor.ui.getDom('scale').style.display = "none";
  26975. }
  26976. if (!editor.selection.isFocus()) return;
  26977. editor.fireEvent('selectionchange', false, true);
  26978. });
  26979. editor.addListener('mousedown', function (t, evt) {
  26980. var el = evt.target || evt.srcElement;
  26981. baidu.editor.ui.Popup.postHide(evt, el);
  26982. baidu.editor.ui.ShortCutMenu.postHide(evt);
  26983. });
  26984. editor.addListener("delcells", function () {
  26985. if (UE.ui['edittip']) {
  26986. new UE.ui['edittip'](editor);
  26987. }
  26988. editor.getDialog('edittip').open();
  26989. });
  26990. var pastePop, isPaste = false,
  26991. timer;
  26992. editor.addListener("afterpaste", function () {
  26993. if (editor.queryCommandState('pasteplain'))
  26994. return;
  26995. if (baidu.editor.ui.PastePicker) {
  26996. pastePop = new baidu.editor.ui.Popup({
  26997. content: new baidu.editor.ui.PastePicker({
  26998. editor: editor
  26999. }),
  27000. editor: editor,
  27001. className: 'edui-wordpastepop'
  27002. });
  27003. pastePop.render();
  27004. }
  27005. isPaste = true;
  27006. });
  27007. editor.addListener("afterinserthtml", function () {
  27008. clearTimeout(timer);
  27009. timer = setTimeout(function () {
  27010. if (pastePop && (isPaste || editor.ui._isTransfer)) {
  27011. if (pastePop.isHidden()) {
  27012. var span = domUtils.createElement(editor.document, 'span', {
  27013. 'style': "line-height:0px;",
  27014. 'innerHTML': '\ufeff'
  27015. }),
  27016. range = editor.selection.getRange();
  27017. range.insertNode(span);
  27018. var tmp = getDomNode(span, 'firstChild', 'previousSibling');
  27019. tmp && pastePop.showAnchor(tmp.nodeType == 3 ? tmp.parentNode : tmp);
  27020. domUtils.remove(span);
  27021. } else {
  27022. pastePop.show();
  27023. }
  27024. delete editor.ui._isTransfer;
  27025. isPaste = false;
  27026. }
  27027. }, 200)
  27028. });
  27029. editor.addListener('contextmenu', function (t, evt) {
  27030. baidu.editor.ui.Popup.postHide(evt);
  27031. });
  27032. editor.addListener('keydown', function (t, evt) {
  27033. if (pastePop) pastePop.dispose(evt);
  27034. var keyCode = evt.keyCode || evt.which;
  27035. if (evt.altKey && keyCode == 90) {
  27036. UE.ui.buttons['fullscreen'].onclick();
  27037. }
  27038. });
  27039. editor.addListener('wordcount', function (type) {
  27040. setCount(this, me);
  27041. });
  27042. function setCount(editor, ui) {
  27043. editor.setOpt({
  27044. wordCount: true,
  27045. maximumWords: 10000,
  27046. wordCountMsg: editor.options.wordCountMsg || editor.getLang("wordCountMsg"),
  27047. wordOverFlowMsg: editor.options.wordOverFlowMsg || editor.getLang("wordOverFlowMsg")
  27048. });
  27049. var opt = editor.options,
  27050. max = opt.maximumWords,
  27051. msg = opt.wordCountMsg,
  27052. errMsg = opt.wordOverFlowMsg,
  27053. countDom = ui.getDom('wordcount');
  27054. if (!opt.wordCount) {
  27055. return;
  27056. }
  27057. var count = editor.getContentLength(true);
  27058. if (count > max) {
  27059. countDom.innerHTML = errMsg;
  27060. editor.fireEvent("wordcountoverflow");
  27061. } else {
  27062. countDom.innerHTML = msg.replace("{#leave}", max - count).replace("{#count}", count);
  27063. }
  27064. }
  27065. editor.addListener('selectionchange', function () {
  27066. if (editor.options.elementPathEnabled) {
  27067. me[(editor.queryCommandState('elementpath') == -1 ? 'dis' : 'en') + 'ableElementPath']()
  27068. }
  27069. if (editor.options.scaleEnabled) {
  27070. me[(editor.queryCommandState('scale') == -1 ? 'dis' : 'en') + 'ableScale']();
  27071. }
  27072. });
  27073. var popup = new baidu.editor.ui.Popup({
  27074. editor: editor,
  27075. content: '',
  27076. className: 'edui-bubble',
  27077. _onEditButtonClick: function () {
  27078. this.hide();
  27079. editor.ui._dialogs.linkDialog.open();
  27080. },
  27081. _onImgEditButtonClick: function (name) {
  27082. this.hide();
  27083. editor.ui._dialogs[name] && editor.ui._dialogs[name].open();
  27084. },
  27085. _onImgSetFloat: function (value) {
  27086. this.hide();
  27087. editor.execCommand("imagefloat", value);
  27088. },
  27089. _setIframeAlign: function (value) {
  27090. var frame = popup.anchorEl;
  27091. var newFrame = frame.cloneNode(true);
  27092. switch (value) {
  27093. case -2:
  27094. newFrame.setAttribute("align", "");
  27095. break;
  27096. case -1:
  27097. newFrame.setAttribute("align", "left");
  27098. break;
  27099. case 1:
  27100. newFrame.setAttribute("align", "right");
  27101. break;
  27102. }
  27103. frame.parentNode.insertBefore(newFrame, frame);
  27104. domUtils.remove(frame);
  27105. popup.anchorEl = newFrame;
  27106. popup.showAnchor(popup.anchorEl);
  27107. },
  27108. _updateIframe: function () {
  27109. var frame = editor._iframe = popup.anchorEl;
  27110. if (domUtils.hasClass(frame, 'ueditor_baidumap')) {
  27111. editor.selection.getRange().selectNode(frame).select();
  27112. editor.ui._dialogs.mapDialog.open();
  27113. popup.hide();
  27114. } else {
  27115. editor.ui._dialogs.insertframeDialog.open();
  27116. popup.hide();
  27117. }
  27118. },
  27119. _onRemoveButtonClick: function (cmdName) {
  27120. editor.execCommand(cmdName);
  27121. this.hide();
  27122. },
  27123. queryAutoHide: function (el) {
  27124. if (el && el.ownerDocument == editor.document) {
  27125. if (el.tagName.toLowerCase() == 'img' || domUtils.findParentByTagName(el, 'a', true)) {
  27126. return el !== popup.anchorEl;
  27127. }
  27128. }
  27129. return baidu.editor.ui.Popup.prototype.queryAutoHide.call(this, el);
  27130. }
  27131. });
  27132. popup.render();
  27133. if (editor.options.imagePopup) {
  27134. editor.addListener('mouseover', function (t, evt) {
  27135. evt = evt || window.event;
  27136. var el = evt.target || evt.srcElement;
  27137. if (editor.ui._dialogs.insertframeDialog && /iframe/ig.test(el.tagName)) {
  27138. var html = popup.formatHtml(
  27139. '<nobr>' + editor.getLang("property") + ': <span onclick=$$._setIframeAlign(-2) class="edui-clickable">' + editor.getLang("default") + '</span>&nbsp;&nbsp;<span onclick=$$._setIframeAlign(-1) class="edui-clickable">' + editor.getLang("justifyleft") + '</span>&nbsp;&nbsp;<span onclick=$$._setIframeAlign(1) class="edui-clickable">' + editor.getLang("justifyright") + '</span>&nbsp;&nbsp;' +
  27140. ' <span onclick="$$._updateIframe( this);" class="edui-clickable">' + editor.getLang("modify") + '</span></nobr>');
  27141. if (html) {
  27142. popup.getDom('content').innerHTML = html;
  27143. popup.anchorEl = el;
  27144. popup.showAnchor(popup.anchorEl);
  27145. } else {
  27146. popup.hide();
  27147. }
  27148. }
  27149. });
  27150. editor.addListener('selectionchange', function (t, causeByUi) {
  27151. if (!causeByUi) return;
  27152. var html = '',
  27153. str = "",
  27154. img = editor.selection.getRange().getClosedNode(),
  27155. dialogs = editor.ui._dialogs;
  27156. if (img && img.tagName == 'IMG') {
  27157. var dialogName = 'insertimageDialog';
  27158. if (img.className.indexOf("edui-faked-video") != -1 || img.className.indexOf("edui-upload-video") != -1) {
  27159. dialogName = "insertvideoDialog"
  27160. }
  27161. if (img.className.indexOf("edui-faked-webapp") != -1) {
  27162. dialogName = "webappDialog"
  27163. }
  27164. if (img.src.indexOf("http://api.map.baidu.com") != -1) {
  27165. dialogName = "mapDialog"
  27166. }
  27167. if (img.className.indexOf("edui-faked-music") != -1) {
  27168. dialogName = "musicDialog"
  27169. }
  27170. if (img.src.indexOf("http://maps.google.com/maps/api/staticmap") != -1) {
  27171. dialogName = "gmapDialog"
  27172. }
  27173. if (img.getAttribute("anchorname")) {
  27174. dialogName = "anchorDialog";
  27175. html = popup.formatHtml(
  27176. '<nobr>' + editor.getLang("property") + ': <span onclick=$$._onImgEditButtonClick("anchorDialog") class="edui-clickable">' + editor.getLang("modify") + '</span>&nbsp;&nbsp;' +
  27177. '<span onclick=$$._onRemoveButtonClick(\'anchor\') class="edui-clickable">' + editor.getLang("delete") + '</span></nobr>');
  27178. }
  27179. if (img.getAttribute("word_img")) {
  27180. //todo 放到dialog去做查询
  27181. editor.word_img = [img.getAttribute("word_img")];
  27182. dialogName = "wordimageDialog"
  27183. }
  27184. if (domUtils.hasClass(img, 'loadingclass') || domUtils.hasClass(img, 'loaderrorclass')) {
  27185. dialogName = "";
  27186. }
  27187. if (!dialogs[dialogName]) {
  27188. return;
  27189. }
  27190. str = '<nobr>' + editor.getLang("property") + ': ' +
  27191. '<span onclick=$$._onImgSetFloat("none") class="edui-clickable">' + editor.getLang("default") + '</span>&nbsp;&nbsp;' +
  27192. '<span onclick=$$._onImgSetFloat("left") class="edui-clickable">' + editor.getLang("justifyleft") + '</span>&nbsp;&nbsp;' +
  27193. '<span onclick=$$._onImgSetFloat("right") class="edui-clickable">' + editor.getLang("justifyright") + '</span>&nbsp;&nbsp;' +
  27194. '<span onclick=$$._onImgSetFloat("center") class="edui-clickable">' + editor.getLang("justifycenter") + '</span>&nbsp;&nbsp;' +
  27195. '<span onclick="$$._onImgEditButtonClick(\'' + dialogName + '\');" class="edui-clickable">' + editor.getLang("modify") + '</span></nobr>';
  27196. !html && (html = popup.formatHtml(str))
  27197. }
  27198. if (editor.ui._dialogs.linkDialog) {
  27199. var link = editor.queryCommandValue('link');
  27200. var url;
  27201. if (link && (url = (link.getAttribute('_href') || link.getAttribute('href', 2)))) {
  27202. var txt = url;
  27203. if (url.length > 30) {
  27204. txt = url.substring(0, 20) + "...";
  27205. }
  27206. if (html) {
  27207. html += '<div style="height:5px;"></div>'
  27208. }
  27209. html += popup.formatHtml(
  27210. '<nobr>' + editor.getLang("anthorMsg") + ': <a target="_blank" href="' + url + '" title="' + url + '" >' + txt + '</a>' +
  27211. ' <span class="edui-clickable" onclick="$$._onEditButtonClick();">' + editor.getLang("modify") + '</span>' +
  27212. ' <span class="edui-clickable" onclick="$$._onRemoveButtonClick(\'unlink\');"> ' + editor.getLang("clear") + '</span></nobr>');
  27213. popup.showAnchor(link);
  27214. }
  27215. }
  27216. if (html) {
  27217. popup.getDom('content').innerHTML = html;
  27218. popup.anchorEl = img || link;
  27219. popup.showAnchor(popup.anchorEl);
  27220. } else {
  27221. popup.hide();
  27222. }
  27223. });
  27224. }
  27225. },
  27226. _initToolbars: function () {
  27227. var editor = this.editor;
  27228. var toolbars = this.toolbars || [];
  27229. var toolbarUis = [];
  27230. for (var i = 0; i < toolbars.length; i++) {
  27231. var toolbar = toolbars[i];
  27232. var toolbarUi = new baidu.editor.ui.Toolbar({
  27233. theme: editor.options.theme
  27234. });
  27235. for (var j = 0; j < toolbar.length; j++) {
  27236. var toolbarItem = toolbar[j];
  27237. var toolbarItemUi = null;
  27238. if (typeof toolbarItem == 'string') {
  27239. toolbarItem = toolbarItem.toLowerCase();
  27240. if (toolbarItem == '|') {
  27241. toolbarItem = 'Separator';
  27242. }
  27243. if (toolbarItem == '||') {
  27244. toolbarItem = 'Breakline';
  27245. }
  27246. if (baidu.editor.ui[toolbarItem]) {
  27247. toolbarItemUi = new baidu.editor.ui[toolbarItem](editor);
  27248. }
  27249. //fullscreen这里单独处理一下,放到首行去
  27250. if (toolbarItem == 'fullscreen') {
  27251. if (toolbarUis && toolbarUis[0]) {
  27252. toolbarUis[0].items.splice(0, 0, toolbarItemUi);
  27253. } else {
  27254. toolbarItemUi && toolbarUi.items.splice(0, 0, toolbarItemUi);
  27255. }
  27256. continue;
  27257. }
  27258. } else {
  27259. toolbarItemUi = toolbarItem;
  27260. }
  27261. if (toolbarItemUi && toolbarItemUi.id) {
  27262. toolbarUi.add(toolbarItemUi);
  27263. }
  27264. }
  27265. toolbarUis[i] = toolbarUi;
  27266. }
  27267. //接受外部定制的UI(修复因 utils.each 无法准确的循环出对象的全部元素而导致的自定义 UI 不符合预期的 BUG by HaoChuan9421)
  27268. // utils.each(UE._customizeUI,function(obj,key){
  27269. // var itemUI,index;
  27270. // if(obj.id && obj.id != editor.key){
  27271. // return false;
  27272. // }
  27273. // itemUI = obj.execFn.call(editor,editor,key);
  27274. // if(itemUI){
  27275. // index = obj.index;
  27276. // if(index === undefined){
  27277. // index = toolbarUi.items.length;
  27278. // }
  27279. // toolbarUi.add(itemUI,index)
  27280. // }
  27281. // });
  27282. for (var key in UE._customizeUI) {
  27283. var obj = UE._customizeUI[key]
  27284. var itemUI, index;
  27285. if (!obj.id || obj.id == editor.key) {
  27286. itemUI = obj.execFn.call(editor, editor, key);
  27287. if (itemUI) {
  27288. index = obj.index;
  27289. if (index === undefined) {
  27290. index = toolbarUi.items.length;
  27291. }
  27292. toolbarUi.add(itemUI, index)
  27293. }
  27294. }
  27295. }
  27296. this.toolbars = toolbarUis;
  27297. },
  27298. getHtmlTpl: function () {
  27299. return '<div id="##" class="%%">' +
  27300. '<div id="##_toolbarbox" class="%%-toolbarbox">' +
  27301. (this.toolbars.length ?
  27302. '<div id="##_toolbarboxouter" class="%%-toolbarboxouter"><div class="%%-toolbarboxinner">' +
  27303. this.renderToolbarBoxHtml() +
  27304. '</div></div>' : '') +
  27305. '<div id="##_toolbarmsg" class="%%-toolbarmsg" style="display:none;">' +
  27306. '<div id = "##_upload_dialog" class="%%-toolbarmsg-upload" onclick="$$.showWordImageDialog();">' + this.editor.getLang("clickToUpload") + '</div>' +
  27307. '<div class="%%-toolbarmsg-close" onclick="$$.hideToolbarMsg();">x</div>' +
  27308. '<div id="##_toolbarmsg_label" class="%%-toolbarmsg-label"></div>' +
  27309. '<div style="height:0;overflow:hidden;clear:both;"></div>' +
  27310. '</div>' +
  27311. '<div id="##_message_holder" class="%%-messageholder"></div>' +
  27312. '</div>' +
  27313. '<div id="##_iframeholder" class="%%-iframeholder">' +
  27314. '</div>' +
  27315. //modify wdcount by matao
  27316. '<div id="##_bottombar" class="%%-bottomContainer"><table><tr>' +
  27317. '<td id="##_elementpath" class="%%-bottombar"></td>' +
  27318. '<td id="##_wordcount" class="%%-wordcount"></td>' +
  27319. '<td id="##_scale" class="%%-scale"><div class="%%-icon"></div></td>' +
  27320. '</tr></table></div>' +
  27321. '<div id="##_scalelayer"></div>' +
  27322. '</div>';
  27323. },
  27324. showWordImageDialog: function () {
  27325. this._dialogs['wordimageDialog'].open();
  27326. },
  27327. renderToolbarBoxHtml: function () {
  27328. var buff = [];
  27329. for (var i = 0; i < this.toolbars.length; i++) {
  27330. buff.push(this.toolbars[i].renderHtml());
  27331. }
  27332. return buff.join('');
  27333. },
  27334. setFullScreen: function (fullscreen) {
  27335. var editor = this.editor,
  27336. container = editor.container.parentNode.parentNode;
  27337. if (this._fullscreen != fullscreen) {
  27338. this._fullscreen = fullscreen;
  27339. this.editor.fireEvent('beforefullscreenchange', fullscreen);
  27340. if (baidu.editor.browser.gecko) {
  27341. var bk = editor.selection.getRange().createBookmark();
  27342. }
  27343. if (fullscreen) {
  27344. while (container.tagName != "BODY") {
  27345. var position = baidu.editor.dom.domUtils.getComputedStyle(container, "position");
  27346. nodeStack.push(position);
  27347. container.style.position = "static";
  27348. container = container.parentNode;
  27349. }
  27350. this._bakHtmlOverflow = document.documentElement.style.overflow;
  27351. this._bakBodyOverflow = document.body.style.overflow;
  27352. this._bakAutoHeight = this.editor.autoHeightEnabled;
  27353. this._bakScrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
  27354. this._bakEditorContaninerWidth = editor.iframe.parentNode.offsetWidth;
  27355. if (this._bakAutoHeight) {
  27356. //当全屏时不能执行自动长高
  27357. editor.autoHeightEnabled = false;
  27358. this.editor.disableAutoHeight();
  27359. }
  27360. document.documentElement.style.overflow = 'hidden';
  27361. //修复,滚动条不收起的问题
  27362. window.scrollTo(0, window.scrollY);
  27363. this._bakCssText = this.getDom().style.cssText;
  27364. this._bakCssText1 = this.getDom('iframeholder').style.cssText;
  27365. editor.iframe.parentNode.style.width = '';
  27366. this._updateFullScreen();
  27367. } else {
  27368. while (container.tagName != "BODY") {
  27369. container.style.position = nodeStack.shift();
  27370. container = container.parentNode;
  27371. }
  27372. this.getDom().style.cssText = this._bakCssText;
  27373. this.getDom('iframeholder').style.cssText = this._bakCssText1;
  27374. if (this._bakAutoHeight) {
  27375. editor.autoHeightEnabled = true;
  27376. this.editor.enableAutoHeight();
  27377. }
  27378. document.documentElement.style.overflow = this._bakHtmlOverflow;
  27379. document.body.style.overflow = this._bakBodyOverflow;
  27380. editor.iframe.parentNode.style.width = this._bakEditorContaninerWidth + 'px';
  27381. window.scrollTo(0, this._bakScrollTop);
  27382. }
  27383. if (browser.gecko && editor.body.contentEditable === 'true') {
  27384. var input = document.createElement('input');
  27385. document.body.appendChild(input);
  27386. editor.body.contentEditable = false;
  27387. setTimeout(function () {
  27388. input.focus();
  27389. setTimeout(function () {
  27390. editor.body.contentEditable = true;
  27391. editor.fireEvent('fullscreenchanged', fullscreen);
  27392. editor.selection.getRange().moveToBookmark(bk).select(true);
  27393. baidu.editor.dom.domUtils.remove(input);
  27394. fullscreen && window.scroll(0, 0);
  27395. }, 0)
  27396. }, 0)
  27397. }
  27398. if (editor.body.contentEditable === 'true') {
  27399. this.editor.fireEvent('fullscreenchanged', fullscreen);
  27400. this.triggerLayout();
  27401. }
  27402. }
  27403. },
  27404. _updateFullScreen: function () {
  27405. if (this._fullscreen) {
  27406. var vpRect = uiUtils.getViewportRect();
  27407. this.getDom().style.cssText = 'border:0;position:absolute;left:0;top:' + (this.editor.options.topOffset || 0) + 'px;width:' + vpRect.width + 'px;height:' + vpRect.height + 'px;z-index:' + (this.getDom().style.zIndex * 1 + 100);
  27408. uiUtils.setViewportOffset(this.getDom(), {
  27409. left: 0,
  27410. top: this.editor.options.topOffset || 0
  27411. });
  27412. this.editor.setHeight(vpRect.height - this.getDom('toolbarbox').offsetHeight - this.getDom('bottombar').offsetHeight - (this.editor.options.topOffset || 0), true);
  27413. //不手动调一下,会导致全屏失效
  27414. if (browser.gecko) {
  27415. try {
  27416. window.onresize();
  27417. } catch (e) {
  27418. }
  27419. }
  27420. }
  27421. },
  27422. _updateElementPath: function () {
  27423. var bottom = this.getDom('elementpath'),
  27424. list;
  27425. if (this.elementPathEnabled && (list = this.editor.queryCommandValue('elementpath'))) {
  27426. var buff = [];
  27427. for (var i = 0, ci; ci = list[i]; i++) {
  27428. buff[i] = this.formatHtml('<span unselectable="on" onclick="$$.editor.execCommand(&quot;elementpath&quot;, &quot;' + i + '&quot;);">' + ci + '</span>');
  27429. }
  27430. bottom.innerHTML = '<div class="edui-editor-breadcrumb" onmousedown="return false;">' + this.editor.getLang("elementPathTip") + ': ' + buff.join(' &gt; ') + '</div>';
  27431. } else {
  27432. bottom.style.display = 'none'
  27433. }
  27434. },
  27435. disableElementPath: function () {
  27436. var bottom = this.getDom('elementpath');
  27437. bottom.innerHTML = '';
  27438. bottom.style.display = 'none';
  27439. this.elementPathEnabled = false;
  27440. },
  27441. enableElementPath: function () {
  27442. var bottom = this.getDom('elementpath');
  27443. bottom.style.display = '';
  27444. this.elementPathEnabled = true;
  27445. this._updateElementPath();
  27446. },
  27447. _scale: function () {
  27448. var doc = document,
  27449. editor = this.editor,
  27450. editorHolder = editor.container,
  27451. editorDocument = editor.document,
  27452. toolbarBox = this.getDom("toolbarbox"),
  27453. bottombar = this.getDom("bottombar"),
  27454. scale = this.getDom("scale"),
  27455. scalelayer = this.getDom("scalelayer");
  27456. var isMouseMove = false,
  27457. position = null,
  27458. minEditorHeight = 0,
  27459. minEditorWidth = editor.options.minFrameWidth,
  27460. pageX = 0,
  27461. pageY = 0,
  27462. scaleWidth = 0,
  27463. scaleHeight = 0;
  27464. function down() {
  27465. position = domUtils.getXY(editorHolder);
  27466. if (!minEditorHeight) {
  27467. minEditorHeight = editor.options.minFrameHeight + toolbarBox.offsetHeight + bottombar.offsetHeight;
  27468. }
  27469. scalelayer.style.cssText = "position:absolute;left:0;display:;top:0;background-color:#41ABFF;opacity:0.4;filter: Alpha(opacity=40);width:" + editorHolder.offsetWidth + "px;height:" +
  27470. editorHolder.offsetHeight + "px;z-index:" + (editor.options.zIndex + 1);
  27471. domUtils.on(doc, "mousemove", move);
  27472. domUtils.on(editorDocument, "mouseup", up);
  27473. domUtils.on(doc, "mouseup", up);
  27474. }
  27475. var me = this;
  27476. //by xuheng 全屏时关掉缩放
  27477. this.editor.addListener('fullscreenchanged', function (e, fullScreen) {
  27478. if (fullScreen) {
  27479. me.disableScale();
  27480. } else {
  27481. if (me.editor.options.scaleEnabled) {
  27482. me.enableScale();
  27483. var tmpNode = me.editor.document.createElement('span');
  27484. me.editor.body.appendChild(tmpNode);
  27485. me.editor.body.style.height = Math.max(domUtils.getXY(tmpNode).y, me.editor.iframe.offsetHeight - 20) + 'px';
  27486. domUtils.remove(tmpNode)
  27487. }
  27488. }
  27489. });
  27490. function move(event) {
  27491. clearSelection();
  27492. var e = event || window.event;
  27493. pageX = e.pageX || (doc.documentElement.scrollLeft + e.clientX);
  27494. pageY = e.pageY || (doc.documentElement.scrollTop + e.clientY);
  27495. scaleWidth = pageX - position.x;
  27496. scaleHeight = pageY - position.y;
  27497. if (scaleWidth >= minEditorWidth) {
  27498. isMouseMove = true;
  27499. scalelayer.style.width = scaleWidth + 'px';
  27500. }
  27501. if (scaleHeight >= minEditorHeight) {
  27502. isMouseMove = true;
  27503. scalelayer.style.height = scaleHeight + "px";
  27504. }
  27505. }
  27506. function up() {
  27507. if (isMouseMove) {
  27508. isMouseMove = false;
  27509. editor.ui._actualFrameWidth = scalelayer.offsetWidth - 2;
  27510. editorHolder.style.width = editor.ui._actualFrameWidth + 'px';
  27511. editor.setHeight(scalelayer.offsetHeight - bottombar.offsetHeight - toolbarBox.offsetHeight - 2, true);
  27512. }
  27513. if (scalelayer) {
  27514. scalelayer.style.display = "none";
  27515. }
  27516. clearSelection();
  27517. domUtils.un(doc, "mousemove", move);
  27518. domUtils.un(editorDocument, "mouseup", up);
  27519. domUtils.un(doc, "mouseup", up);
  27520. }
  27521. function clearSelection() {
  27522. if (browser.ie)
  27523. doc.selection.clear();
  27524. else
  27525. window.getSelection().removeAllRanges();
  27526. }
  27527. this.enableScale = function () {
  27528. //trace:2868
  27529. if (editor.queryCommandState("source") == 1) return;
  27530. scale.style.display = "";
  27531. this.scaleEnabled = true;
  27532. domUtils.on(scale, "mousedown", down);
  27533. };
  27534. this.disableScale = function () {
  27535. scale.style.display = "none";
  27536. this.scaleEnabled = false;
  27537. domUtils.un(scale, "mousedown", down);
  27538. };
  27539. },
  27540. isFullScreen: function () {
  27541. return this._fullscreen;
  27542. },
  27543. postRender: function () {
  27544. UIBase.prototype.postRender.call(this);
  27545. for (var i = 0; i < this.toolbars.length; i++) {
  27546. this.toolbars[i].postRender();
  27547. }
  27548. var me = this;
  27549. var timerId,
  27550. domUtils = baidu.editor.dom.domUtils,
  27551. updateFullScreenTime = function () {
  27552. clearTimeout(timerId);
  27553. timerId = setTimeout(function () {
  27554. me._updateFullScreen();
  27555. });
  27556. };
  27557. domUtils.on(window, 'resize', updateFullScreenTime);
  27558. me.addListener('destroy', function () {
  27559. domUtils.un(window, 'resize', updateFullScreenTime);
  27560. clearTimeout(timerId);
  27561. })
  27562. },
  27563. showToolbarMsg: function (msg, flag) {
  27564. this.getDom('toolbarmsg_label').innerHTML = msg;
  27565. this.getDom('toolbarmsg').style.display = '';
  27566. //
  27567. if (!flag) {
  27568. var w = this.getDom('upload_dialog');
  27569. w.style.display = 'none';
  27570. }
  27571. },
  27572. hideToolbarMsg: function () {
  27573. this.getDom('toolbarmsg').style.display = 'none';
  27574. },
  27575. mapUrl: function (url) {
  27576. return url ? url.replace('~/', this.editor.options.UEDITOR_HOME_URL || '') : ''
  27577. },
  27578. triggerLayout: function () {
  27579. var dom = this.getDom();
  27580. if (dom.style.zoom == '1') {
  27581. dom.style.zoom = '100%';
  27582. } else {
  27583. dom.style.zoom = '1';
  27584. }
  27585. }
  27586. };
  27587. utils.inherits(EditorUI, baidu.editor.ui.UIBase);
  27588. var instances = {};
  27589. UE.ui.Editor = function (options) {
  27590. var editor = new UE.Editor(options);
  27591. editor.options.editor = editor;
  27592. utils.loadFile(document, {
  27593. href: editor.options.themePath + editor.options.theme + "/css/ueditor.css",
  27594. tag: "link",
  27595. type: "text/css",
  27596. rel: "stylesheet"
  27597. });
  27598. var oldRender = editor.render;
  27599. editor.render = function (holder) {
  27600. if (holder.constructor === String) {
  27601. editor.key = holder;
  27602. instances[holder] = editor;
  27603. }
  27604. utils.domReady(function () {
  27605. editor.langIsReady ? renderUI() : editor.addListener("langReady", renderUI);
  27606. function renderUI() {
  27607. editor.setOpt({
  27608. labelMap: editor.options.labelMap || editor.getLang('labelMap')
  27609. });
  27610. new EditorUI(editor.options);
  27611. if (holder) {
  27612. if (holder.constructor === String) {
  27613. holder = document.getElementById(holder);
  27614. }
  27615. holder && holder.getAttribute('name') && (editor.options.textarea = holder.getAttribute('name'));
  27616. if (holder && /script|textarea/ig.test(holder.tagName)) {
  27617. var newDiv = document.createElement('div');
  27618. holder.parentNode.insertBefore(newDiv, holder);
  27619. var cont = holder.value || holder.innerHTML;
  27620. editor.options.initialContent = /^[\t\r\n ]*$/.test(cont) ? editor.options.initialContent :
  27621. cont.replace(/>[\n\r\t]+([ ]{4})+/g, '>')
  27622. .replace(/[\n\r\t]+([ ]{4})+</g, '<')
  27623. .replace(/>[\n\r\t]+</g, '><');
  27624. holder.className && (newDiv.className = holder.className);
  27625. holder.style.cssText && (newDiv.style.cssText = holder.style.cssText);
  27626. if (/textarea/i.test(holder.tagName)) {
  27627. editor.textarea = holder;
  27628. editor.textarea.style.display = 'none';
  27629. } else {
  27630. holder.parentNode.removeChild(holder);
  27631. }
  27632. if (holder.id) {
  27633. newDiv.id = holder.id;
  27634. domUtils.removeAttributes(holder, 'id');
  27635. }
  27636. holder = newDiv;
  27637. holder.innerHTML = '';
  27638. }
  27639. }
  27640. domUtils.addClass(holder, "edui-" + editor.options.theme);
  27641. editor.ui.render(holder);
  27642. var opt = editor.options;
  27643. //给实例添加一个编辑器的容器引用
  27644. editor.container = editor.ui.getDom();
  27645. var parents = domUtils.findParents(holder, true);
  27646. var displays = [];
  27647. for (var i = 0, ci; ci = parents[i]; i++) {
  27648. displays[i] = ci.style.display;
  27649. ci.style.display = 'block'
  27650. }
  27651. if (opt.initialFrameWidth) {
  27652. opt.minFrameWidth = opt.initialFrameWidth;
  27653. } else {
  27654. opt.minFrameWidth = opt.initialFrameWidth = holder.offsetWidth;
  27655. var styleWidth = holder.style.width;
  27656. if (/%$/.test(styleWidth)) {
  27657. opt.initialFrameWidth = styleWidth;
  27658. }
  27659. }
  27660. if (opt.initialFrameHeight) {
  27661. opt.minFrameHeight = opt.initialFrameHeight;
  27662. } else {
  27663. opt.initialFrameHeight = opt.minFrameHeight = holder.offsetHeight;
  27664. }
  27665. for (var i = 0, ci; ci = parents[i]; i++) {
  27666. ci.style.display = displays[i]
  27667. }
  27668. //编辑器最外容器设置了高度,会导致,编辑器不占位
  27669. //todo 先去掉,没有找到原因
  27670. if (holder.style.height) {
  27671. holder.style.height = ''
  27672. }
  27673. editor.container.style.width = opt.initialFrameWidth + (/%$/.test(opt.initialFrameWidth) ? '' : 'px');
  27674. editor.container.style.zIndex = opt.zIndex;
  27675. oldRender.call(editor, editor.ui.getDom('iframeholder'));
  27676. editor.fireEvent("afteruiready");
  27677. }
  27678. })
  27679. };
  27680. return editor;
  27681. };
  27682. /**
  27683. * @file
  27684. * @name UE
  27685. * @short UE
  27686. * @desc UEditor的顶部命名空间
  27687. */
  27688. /**
  27689. * @name getEditor
  27690. * @since 1.2.4+
  27691. * @grammar UE.getEditor(id,[opt]) => Editor实例
  27692. * @desc 提供一个全局的方法得到编辑器实例
  27693. *
  27694. * * ''id'' 放置编辑器的容器id, 如果容器下的编辑器已经存在,就直接返回
  27695. * * ''opt'' 编辑器的可选参数
  27696. * @example
  27697. * UE.getEditor('containerId',{onready:function(){//创建一个编辑器实例
  27698. * this.setContent('hello')
  27699. * }});
  27700. * UE.getEditor('containerId'); //返回刚创建的实例
  27701. *
  27702. */
  27703. UE.getEditor = function (id, opt) {
  27704. var editor = instances[id];
  27705. if (!editor) {
  27706. editor = instances[id] = new UE.ui.Editor(opt);
  27707. editor.render(id);
  27708. }
  27709. return editor;
  27710. };
  27711. UE.delEditor = function (id) {
  27712. var editor;
  27713. if (editor = instances[id]) {
  27714. editor.key && editor.destroy();
  27715. delete instances[id]
  27716. }
  27717. };
  27718. UE.registerUI = function (uiName, fn, index, editorId) {
  27719. utils.each(uiName.split(/\s+/), function (name) {
  27720. UE._customizeUI[name] = {
  27721. id: editorId,
  27722. execFn: fn,
  27723. index: index
  27724. };
  27725. })
  27726. }
  27727. })();
  27728. // adapter/message.js
  27729. UE.registerUI('message', function (editor) {
  27730. var editorui = baidu.editor.ui;
  27731. var Message = editorui.Message;
  27732. var holder;
  27733. var _messageItems = [];
  27734. var me = editor;
  27735. me.addListener('ready', function () {
  27736. holder = document.getElementById(me.ui.id + '_message_holder');
  27737. updateHolderPos();
  27738. // HaoChuan9421
  27739. // setTimeout(function(){
  27740. // updateHolderPos();
  27741. // }, 500);
  27742. });
  27743. me.addListener('showmessage', function (type, opt) {
  27744. opt = utils.isString(opt) ? {
  27745. 'content': opt
  27746. } : opt;
  27747. var message = new Message({
  27748. 'timeout': opt.timeout,
  27749. 'type': opt.type,
  27750. 'content': opt.content,
  27751. 'keepshow': opt.keepshow,
  27752. 'editor': me
  27753. }),
  27754. mid = opt.id || ('msg_' + (+new Date()).toString(36));
  27755. message.render(holder);
  27756. _messageItems[mid] = message;
  27757. message.reset(opt);
  27758. updateHolderPos();
  27759. return mid;
  27760. });
  27761. me.addListener('updatemessage', function (type, id, opt) {
  27762. opt = utils.isString(opt) ? {
  27763. 'content': opt
  27764. } : opt;
  27765. var message = _messageItems[id];
  27766. message.render(holder);
  27767. message && message.reset(opt);
  27768. });
  27769. me.addListener('hidemessage', function (type, id) {
  27770. var message = _messageItems[id];
  27771. message && message.hide();
  27772. });
  27773. function updateHolderPos() {
  27774. var toolbarbox = me.ui.getDom('toolbarbox');
  27775. if (toolbarbox) {
  27776. holder.style.top = toolbarbox.offsetHeight + 3 + 'px';
  27777. }
  27778. holder.style.zIndex = Math.max(me.options.zIndex, me.iframe.style.zIndex) + 1;
  27779. }
  27780. });
  27781. // adapter/autosave.js
  27782. UE.registerUI('autosave', function (editor) {
  27783. var timer = null,
  27784. uid = null;
  27785. editor.on('afterautosave', function () {
  27786. clearTimeout(timer);
  27787. timer = setTimeout(function () {
  27788. if (uid) {
  27789. editor.trigger('hidemessage', uid);
  27790. }
  27791. uid = editor.trigger('showmessage', {
  27792. content: editor.getLang('autosave.success'),
  27793. timeout: 2000
  27794. });
  27795. }, 2000)
  27796. })
  27797. });
  27798. })();