ueditor.all.js 1.1 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438
  1. /*!
  2. * UEditor
  3. * version: ueditor
  4. * build: Thu Feb 25 2016 11:44:16 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. {
  177. geckoRelease = geckoRelease[1].split( '.' );
  178. version = geckoRelease[0] * 10000 + ( geckoRelease[1] || 0 ) * 100 + ( geckoRelease[2] || 0 ) * 1;
  179. }
  180. }
  181. /**
  182. * @property { Number } chrome 检测当前浏览器是否为Chrome, 如果是,则返回Chrome的大版本号
  183. * @warning 如果浏览器不是chrome, 则该值为undefined
  184. * @example
  185. * ```javascript
  186. * if ( UE.browser.chrome ) {
  187. * console.log( '当前浏览器是Chrome' );
  188. * }
  189. * ```
  190. */
  191. if (/chrome\/(\d+\.\d)/i.test(agent)) {
  192. browser.chrome = + RegExp['\x241'];
  193. }
  194. /**
  195. * @property { Number } safari 检测当前浏览器是否为Safari, 如果是,则返回Safari的大版本号
  196. * @warning 如果浏览器不是safari, 则该值为undefined
  197. * @example
  198. * ```javascript
  199. * if ( UE.browser.safari ) {
  200. * console.log( '当前浏览器是Safari' );
  201. * }
  202. * ```
  203. */
  204. if(/(\d+\.\d)?(?:\.\d)?\s+safari\/?(\d+\.\d+)?/i.test(agent) && !/chrome/i.test(agent)){
  205. browser.safari = + (RegExp['\x241'] || RegExp['\x242']);
  206. }
  207. // Opera 9.50+
  208. if ( browser.opera )
  209. version = parseFloat( opera.version() );
  210. // WebKit 522+ (Safari 3+)
  211. if ( browser.webkit )
  212. version = parseFloat( agent.match( / applewebkit\/(\d+)/ )[1] );
  213. /**
  214. * @property { Number } version 检测当前浏览器版本号
  215. * @remind
  216. * <ul>
  217. * <li>IE系列返回值为5,6,7,8,9,10等</li>
  218. * <li>gecko系列会返回10900,158900等</li>
  219. * <li>webkit系列会返回其build号 (如 522等)</li>
  220. * </ul>
  221. * @example
  222. * ```javascript
  223. * console.log( '当前浏览器版本号是: ' + UE.browser.version );
  224. * ```
  225. */
  226. browser.version = version;
  227. /**
  228. * @property { boolean } isCompatible 检测当前浏览器是否能够与UEditor良好兼容
  229. * @example
  230. * ```javascript
  231. * if ( UE.browser.isCompatible ) {
  232. * console.log( '浏览器与UEditor能够良好兼容' );
  233. * }
  234. * ```
  235. */
  236. browser.isCompatible =
  237. !browser.mobile && (
  238. ( browser.ie && version >= 6 ) ||
  239. ( browser.gecko && version >= 10801 ) ||
  240. ( browser.opera && version >= 9.5 ) ||
  241. ( browser.air && version >= 1 ) ||
  242. ( browser.webkit && version >= 522 ) ||
  243. false );
  244. return browser;
  245. }();
  246. //快捷方式
  247. var ie = browser.ie,
  248. webkit = browser.webkit,
  249. gecko = browser.gecko,
  250. opera = browser.opera;
  251. // core/utils.js
  252. /**
  253. * 工具函数包
  254. * @file
  255. * @module UE.utils
  256. * @since 1.2.6.1
  257. */
  258. /**
  259. * UEditor封装使用的静态工具函数
  260. * @module UE.utils
  261. * @unfile
  262. */
  263. var utils = UE.utils = {
  264. /**
  265. * 用给定的迭代器遍历对象
  266. * @method each
  267. * @param { Object } obj 需要遍历的对象
  268. * @param { Function } iterator 迭代器, 该方法接受两个参数, 第一个参数是当前所处理的value, 第二个参数是当前遍历对象的key
  269. * @example
  270. * ```javascript
  271. * var demoObj = {
  272. * key1: 1,
  273. * key2: 2
  274. * };
  275. *
  276. * //output: key1: 1, key2: 2
  277. * UE.utils.each( demoObj, funciton ( value, key ) {
  278. *
  279. * console.log( key + ":" + value );
  280. *
  281. * } );
  282. * ```
  283. */
  284. /**
  285. * 用给定的迭代器遍历数组或类数组对象
  286. * @method each
  287. * @param { Array } array 需要遍历的数组或者类数组
  288. * @param { Function } iterator 迭代器, 该方法接受两个参数, 第一个参数是当前所处理的value, 第二个参数是当前遍历对象的key
  289. * @example
  290. * ```javascript
  291. * var divs = document.getElmentByTagNames( "div" );
  292. *
  293. * //output: 0: DIV, 1: DIV ...
  294. * UE.utils.each( divs, funciton ( value, key ) {
  295. *
  296. * console.log( key + ":" + value.tagName );
  297. *
  298. * } );
  299. * ```
  300. */
  301. each : function(obj, iterator, context) {
  302. if (obj == null) return;
  303. if (obj.length === +obj.length) {
  304. for (var i = 0, l = obj.length; i < l; i++) {
  305. if(iterator.call(context, obj[i], i, obj) === false)
  306. return false;
  307. }
  308. } else {
  309. for (var key in obj) {
  310. if (obj.hasOwnProperty(key)) {
  311. if(iterator.call(context, obj[key], key, obj) === false)
  312. return false;
  313. }
  314. }
  315. }
  316. },
  317. /**
  318. * 以给定对象作为原型创建一个新对象
  319. * @method makeInstance
  320. * @param { Object } protoObject 该对象将作为新创建对象的原型
  321. * @return { Object } 新的对象, 该对象的原型是给定的protoObject对象
  322. * @example
  323. * ```javascript
  324. *
  325. * var protoObject = { sayHello: function () { console.log('Hello UEditor!'); } };
  326. *
  327. * var newObject = UE.utils.makeInstance( protoObject );
  328. * //output: Hello UEditor!
  329. * newObject.sayHello();
  330. * ```
  331. */
  332. makeInstance:function (obj) {
  333. var noop = new Function();
  334. noop.prototype = obj;
  335. obj = new noop;
  336. noop.prototype = null;
  337. return obj;
  338. },
  339. /**
  340. * 将source对象中的属性扩展到target对象上
  341. * @method extend
  342. * @remind 该方法将强制把source对象上的属性复制到target对象上
  343. * @see UE.utils.extend(Object,Object,Boolean)
  344. * @param { Object } target 目标对象, 新的属性将附加到该对象上
  345. * @param { Object } source 源对象, 该对象的属性会被附加到target对象上
  346. * @return { Object } 返回target对象
  347. * @example
  348. * ```javascript
  349. *
  350. * var target = { name: 'target', sex: 1 },
  351. * source = { name: 'source', age: 17 };
  352. *
  353. * UE.utils.extend( target, source );
  354. *
  355. * //output: { name: 'source', sex: 1, age: 17 }
  356. * console.log( target );
  357. *
  358. * ```
  359. */
  360. /**
  361. * 将source对象中的属性扩展到target对象上, 根据指定的isKeepTarget值决定是否保留目标对象中与
  362. * 源对象属性名相同的属性值。
  363. * @method extend
  364. * @param { Object } target 目标对象, 新的属性将附加到该对象上
  365. * @param { Object } source 源对象, 该对象的属性会被附加到target对象上
  366. * @param { Boolean } isKeepTarget 是否保留目标对象中与源对象中属性名相同的属性
  367. * @return { Object } 返回target对象
  368. * @example
  369. * ```javascript
  370. *
  371. * var target = { name: 'target', sex: 1 },
  372. * source = { name: 'source', age: 17 };
  373. *
  374. * UE.utils.extend( target, source, true );
  375. *
  376. * //output: { name: 'target', sex: 1, age: 17 }
  377. * console.log( target );
  378. *
  379. * ```
  380. */
  381. extend:function (t, s, b) {
  382. if (s) {
  383. for (var k in s) {
  384. if (!b || !t.hasOwnProperty(k)) {
  385. t[k] = s[k];
  386. }
  387. }
  388. }
  389. return t;
  390. },
  391. /**
  392. * 将给定的多个对象的属性复制到目标对象target上
  393. * @method extend2
  394. * @remind 该方法将强制把源对象上的属性复制到target对象上
  395. * @remind 该方法支持两个及以上的参数, 从第二个参数开始, 其属性都会被复制到第一个参数上。 如果遇到同名的属性,
  396. * 将会覆盖掉之前的值。
  397. * @param { Object } target 目标对象, 新的属性将附加到该对象上
  398. * @param { Object... } source 源对象, 支持多个对象, 该对象的属性会被附加到target对象上
  399. * @return { Object } 返回target对象
  400. * @example
  401. * ```javascript
  402. *
  403. * var target = {},
  404. * source1 = { name: 'source', age: 17 },
  405. * source2 = { title: 'dev' };
  406. *
  407. * UE.utils.extend2( target, source1, source2 );
  408. *
  409. * //output: { name: 'source', age: 17, title: 'dev' }
  410. * console.log( target );
  411. *
  412. * ```
  413. */
  414. extend2:function (t) {
  415. var a = arguments;
  416. for (var i = 1; i < a.length; i++) {
  417. var x = a[i];
  418. for (var k in x) {
  419. if (!t.hasOwnProperty(k)) {
  420. t[k] = x[k];
  421. }
  422. }
  423. }
  424. return t;
  425. },
  426. /**
  427. * 模拟继承机制, 使得subClass继承自superClass
  428. * @method inherits
  429. * @param { Object } subClass 子类对象
  430. * @param { Object } superClass 超类对象
  431. * @warning 该方法只能让subClass继承超类的原型, subClass对象自身的属性和方法不会被继承
  432. * @return { Object } 继承superClass后的子类对象
  433. * @example
  434. * ```javascript
  435. * function SuperClass(){
  436. * this.name = "小李";
  437. * }
  438. *
  439. * SuperClass.prototype = {
  440. * hello:function(str){
  441. * console.log(this.name + str);
  442. * }
  443. * }
  444. *
  445. * function SubClass(){
  446. * this.name = "小张";
  447. * }
  448. *
  449. * UE.utils.inherits(SubClass,SuperClass);
  450. *
  451. * var sub = new SubClass();
  452. * //output: '小张早上好!
  453. * sub.hello("早上好!");
  454. * ```
  455. */
  456. inherits:function (subClass, superClass) {
  457. var oldP = subClass.prototype,
  458. newP = utils.makeInstance(superClass.prototype);
  459. utils.extend(newP, oldP, true);
  460. subClass.prototype = newP;
  461. return (newP.constructor = subClass);
  462. },
  463. /**
  464. * 用指定的context对象作为函数fn的上下文
  465. * @method bind
  466. * @param { Function } fn 需要绑定上下文的函数对象
  467. * @param { Object } content 函数fn新的上下文对象
  468. * @return { Function } 一个新的函数, 该函数作为原始函数fn的代理, 将完成fn的上下文调换工作。
  469. * @example
  470. * ```javascript
  471. *
  472. * var name = 'window',
  473. * newTest = null;
  474. *
  475. * function test () {
  476. * console.log( this.name );
  477. * }
  478. *
  479. * newTest = UE.utils.bind( test, { name: 'object' } );
  480. *
  481. * //output: object
  482. * newTest();
  483. *
  484. * //output: window
  485. * test();
  486. *
  487. * ```
  488. */
  489. bind:function (fn, context) {
  490. return function () {
  491. return fn.apply(context, arguments);
  492. };
  493. },
  494. /**
  495. * 创建延迟指定时间后执行的函数fn
  496. * @method defer
  497. * @param { Function } fn 需要延迟执行的函数对象
  498. * @param { int } delay 延迟的时间, 单位是毫秒
  499. * @warning 该方法的时间控制是不精确的,仅仅只能保证函数的执行是在给定的时间之后,
  500. * 而不能保证刚好到达延迟时间时执行。
  501. * @return { Function } 目标函数fn的代理函数, 只有执行该函数才能起到延时效果
  502. * @example
  503. * ```javascript
  504. * var start = 0;
  505. *
  506. * function test(){
  507. * console.log( new Date() - start );
  508. * }
  509. *
  510. * var testDefer = UE.utils.defer( test, 1000 );
  511. * //
  512. * start = new Date();
  513. * //output: (大约在1000毫秒之后输出) 1000
  514. * testDefer();
  515. * ```
  516. */
  517. /**
  518. * 创建延迟指定时间后执行的函数fn, 如果在延迟时间内再次执行该方法, 将会根据指定的exclusion的值,
  519. * 决定是否取消前一次函数的执行, 如果exclusion的值为true, 则取消执行,反之,将继续执行前一个方法。
  520. * @method defer
  521. * @param { Function } fn 需要延迟执行的函数对象
  522. * @param { int } delay 延迟的时间, 单位是毫秒
  523. * @param { Boolean } exclusion 如果在延迟时间内再次执行该函数,该值将决定是否取消执行前一次函数的执行,
  524. * 值为true表示取消执行, 反之则将在执行前一次函数之后才执行本次函数调用。
  525. * @warning 该方法的时间控制是不精确的,仅仅只能保证函数的执行是在给定的时间之后,
  526. * 而不能保证刚好到达延迟时间时执行。
  527. * @return { Function } 目标函数fn的代理函数, 只有执行该函数才能起到延时效果
  528. * @example
  529. * ```javascript
  530. *
  531. * function test(){
  532. * console.log(1);
  533. * }
  534. *
  535. * var testDefer = UE.utils.defer( test, 1000, true );
  536. *
  537. * //output: (两次调用仅有一次输出) 1
  538. * testDefer();
  539. * testDefer();
  540. * ```
  541. */
  542. defer:function (fn, delay, exclusion) {
  543. var timerID;
  544. return function () {
  545. if (exclusion) {
  546. clearTimeout(timerID);
  547. }
  548. timerID = setTimeout(fn, delay);
  549. };
  550. },
  551. /**
  552. * 获取元素item在数组array中首次出现的位置, 如果未找到item, 则返回-1
  553. * @method indexOf
  554. * @remind 该方法的匹配过程使用的是恒等“===”
  555. * @param { Array } array 需要查找的数组对象
  556. * @param { * } item 需要在目标数组中查找的值
  557. * @return { int } 返回item在目标数组array中首次出现的位置, 如果在数组中未找到item, 则返回-1
  558. * @example
  559. * ```javascript
  560. * var item = 1,
  561. * arr = [ 3, 4, 6, 8, 1, 1, 2 ];
  562. *
  563. * //output: 4
  564. * console.log( UE.utils.indexOf( arr, item ) );
  565. * ```
  566. */
  567. /**
  568. * 获取元素item数组array中首次出现的位置, 如果未找到item, 则返回-1。通过start的值可以指定搜索的起始位置。
  569. * @method indexOf
  570. * @remind 该方法的匹配过程使用的是恒等“===”
  571. * @param { Array } array 需要查找的数组对象
  572. * @param { * } item 需要在目标数组中查找的值
  573. * @param { int } start 搜索的起始位置
  574. * @return { int } 返回item在目标数组array中的start位置之后首次出现的位置, 如果在数组中未找到item, 则返回-1
  575. * @example
  576. * ```javascript
  577. * var item = 1,
  578. * arr = [ 3, 4, 6, 8, 1, 2, 8, 3, 2, 1, 1, 4 ];
  579. *
  580. * //output: 9
  581. * console.log( UE.utils.indexOf( arr, item, 5 ) );
  582. * ```
  583. */
  584. indexOf:function (array, item, start) {
  585. var index = -1;
  586. start = this.isNumber(start) ? start : 0;
  587. this.each(array, function (v, i) {
  588. if (i >= start && v === item) {
  589. index = i;
  590. return false;
  591. }
  592. });
  593. return index;
  594. },
  595. /**
  596. * 移除数组array中所有的元素item
  597. * @method removeItem
  598. * @param { Array } array 要移除元素的目标数组
  599. * @param { * } item 将要被移除的元素
  600. * @remind 该方法的匹配过程使用的是恒等“===”
  601. * @example
  602. * ```javascript
  603. * var arr = [ 4, 5, 7, 1, 3, 4, 6 ];
  604. *
  605. * UE.utils.removeItem( arr, 4 );
  606. * //output: [ 5, 7, 1, 3, 6 ]
  607. * console.log( arr );
  608. *
  609. * ```
  610. */
  611. removeItem:function (array, item) {
  612. for (var i = 0, l = array.length; i < l; i++) {
  613. if (array[i] === item) {
  614. array.splice(i, 1);
  615. i--;
  616. }
  617. }
  618. },
  619. /**
  620. * 删除字符串str的首尾空格
  621. * @method trim
  622. * @param { String } str 需要删除首尾空格的字符串
  623. * @return { String } 删除了首尾的空格后的字符串
  624. * @example
  625. * ```javascript
  626. *
  627. * var str = " UEdtior ";
  628. *
  629. * //output: 9
  630. * console.log( str.length );
  631. *
  632. * //output: 7
  633. * console.log( UE.utils.trim( " UEdtior " ).length );
  634. *
  635. * //output: 9
  636. * console.log( str.length );
  637. *
  638. * ```
  639. */
  640. trim:function (str) {
  641. return str.replace(/(^[ \t\n\r]+)|([ \t\n\r]+$)/g, '');
  642. },
  643. /**
  644. * 将字符串str以','分隔成数组后,将该数组转换成哈希对象, 其生成的hash对象的key为数组中的元素, value为1
  645. * @method listToMap
  646. * @warning 该方法在生成的hash对象中,会为每一个key同时生成一个另一个全大写的key。
  647. * @param { String } str 该字符串将被以','分割为数组, 然后进行转化
  648. * @return { Object } 转化之后的hash对象
  649. * @example
  650. * ```javascript
  651. *
  652. * //output: Object {UEdtior: 1, UEDTIOR: 1, Hello: 1, HELLO: 1}
  653. * console.log( UE.utils.listToMap( 'UEdtior,Hello' ) );
  654. *
  655. * ```
  656. */
  657. /**
  658. * 将字符串数组转换成哈希对象, 其生成的hash对象的key为数组中的元素, value为1
  659. * @method listToMap
  660. * @warning 该方法在生成的hash对象中,会为每一个key同时生成一个另一个全大写的key。
  661. * @param { Array } arr 字符串数组
  662. * @return { Object } 转化之后的hash对象
  663. * @example
  664. * ```javascript
  665. *
  666. * //output: Object {UEdtior: 1, UEDTIOR: 1, Hello: 1, HELLO: 1}
  667. * console.log( UE.utils.listToMap( [ 'UEdtior', 'Hello' ] ) );
  668. *
  669. * ```
  670. */
  671. listToMap:function (list) {
  672. if (!list)return {};
  673. list = utils.isArray(list) ? list : list.split(',');
  674. for (var i = 0, ci, obj = {}; ci = list[i++];) {
  675. obj[ci.toUpperCase()] = obj[ci] = 1;
  676. }
  677. return obj;
  678. },
  679. /**
  680. * 将str中的html符号转义,将转义“',&,<,",>”五个字符
  681. * @method unhtml
  682. * @param { String } str 需要转义的字符串
  683. * @return { String } 转义后的字符串
  684. * @example
  685. * ```javascript
  686. * var html = '<body>&</body>';
  687. *
  688. * //output: &lt;body&gt;&amp;&lt;/body&gt;
  689. * console.log( UE.utils.unhtml( html ) );
  690. *
  691. * ```
  692. */
  693. unhtml:function (str, reg) {
  694. return str ? str.replace(reg || /[&<">'](?:(amp|lt|quot|gt|#39|nbsp|#\d+);)?/g, function (a, b) {
  695. if (b) {
  696. return a;
  697. } else {
  698. return {
  699. '<':'&lt;',
  700. '&':'&amp;',
  701. '"':'&quot;',
  702. '>':'&gt;',
  703. "'":'&#39;'
  704. }[a]
  705. }
  706. }) : '';
  707. },
  708. /**
  709. * 将str中的转义字符还原成html字符
  710. * @see UE.utils.unhtml(String);
  711. * @method html
  712. * @param { String } str 需要逆转义的字符串
  713. * @return { String } 逆转义后的字符串
  714. * @example
  715. * ```javascript
  716. *
  717. * var str = '&lt;body&gt;&amp;&lt;/body&gt;';
  718. *
  719. * //output: <body>&</body>
  720. * console.log( UE.utils.html( str ) );
  721. *
  722. * ```
  723. */
  724. html:function (str) {
  725. return str ? str.replace(/&((g|l|quo)t|amp|#39|nbsp);/g, function (m) {
  726. return {
  727. '&lt;':'<',
  728. '&amp;':'&',
  729. '&quot;':'"',
  730. '&gt;':'>',
  731. '&#39;':"'",
  732. '&nbsp;':' '
  733. }[m]
  734. }) : '';
  735. },
  736. /**
  737. * 将css样式转换为驼峰的形式
  738. * @method cssStyleToDomStyle
  739. * @param { String } cssName 需要转换的css样式名
  740. * @return { String } 转换成驼峰形式后的css样式名
  741. * @example
  742. * ```javascript
  743. *
  744. * var str = 'border-top';
  745. *
  746. * //output: borderTop
  747. * console.log( UE.utils.cssStyleToDomStyle( str ) );
  748. *
  749. * ```
  750. */
  751. cssStyleToDomStyle:function () {
  752. var test = document.createElement('div').style,
  753. cache = {
  754. 'float':test.cssFloat != undefined ? 'cssFloat' : test.styleFloat != undefined ? 'styleFloat' : 'float'
  755. };
  756. return function (cssName) {
  757. return cache[cssName] || (cache[cssName] = cssName.toLowerCase().replace(/-./g, function (match) {
  758. return match.charAt(1).toUpperCase();
  759. }));
  760. };
  761. }(),
  762. /**
  763. * 动态加载文件到doc中
  764. * @method loadFile
  765. * @param { DomDocument } document 需要加载资源文件的文档对象
  766. * @param { Object } options 加载资源文件的属性集合, 取值请参考代码示例
  767. * @example
  768. * ```javascript
  769. *
  770. * UE.utils.loadFile( document, {
  771. * src:"test.js",
  772. * tag:"script",
  773. * type:"text/javascript",
  774. * defer:"defer"
  775. * } );
  776. *
  777. * ```
  778. */
  779. /**
  780. * 动态加载文件到doc中,加载成功后执行的回调函数fn
  781. * @method loadFile
  782. * @param { DomDocument } document 需要加载资源文件的文档对象
  783. * @param { Object } options 加载资源文件的属性集合, 该集合支持的值是script标签和style标签支持的所有属性。
  784. * @param { Function } fn 资源文件加载成功之后执行的回调
  785. * @warning 对于在同一个文档中多次加载同一URL的文件, 该方法会在第一次加载之后缓存该请求,
  786. * 在此之后的所有同一URL的请求, 将会直接触发回调。
  787. * @example
  788. * ```javascript
  789. *
  790. * UE.utils.loadFile( document, {
  791. * src:"test.js",
  792. * tag:"script",
  793. * type:"text/javascript",
  794. * defer:"defer"
  795. * }, function () {
  796. * console.log('加载成功');
  797. * } );
  798. *
  799. * ```
  800. */
  801. loadFile:function () {
  802. var tmpList = [];
  803. function getItem(doc, obj) {
  804. try {
  805. for (var i = 0, ci; ci = tmpList[i++];) {
  806. if (ci.doc === doc && ci.url == (obj.src || obj.href)) {
  807. return ci;
  808. }
  809. }
  810. } catch (e) {
  811. return null;
  812. }
  813. }
  814. return function (doc, obj, fn) {
  815. var item = getItem(doc, obj);
  816. if (item) {
  817. if (item.ready) {
  818. fn && fn();
  819. } else {
  820. item.funs.push(fn)
  821. }
  822. return;
  823. }
  824. tmpList.push({
  825. doc:doc,
  826. url:obj.src || obj.href,
  827. funs:[fn]
  828. });
  829. if (!doc.body) {
  830. var html = [];
  831. for (var p in obj) {
  832. if (p == 'tag')continue;
  833. html.push(p + '="' + obj[p] + '"')
  834. }
  835. doc.write('<' + obj.tag + ' ' + html.join(' ') + ' ></' + obj.tag + '>');
  836. return;
  837. }
  838. if (obj.id && doc.getElementById(obj.id)) {
  839. return;
  840. }
  841. var element = doc.createElement(obj.tag);
  842. delete obj.tag;
  843. for (var p in obj) {
  844. element.setAttribute(p, obj[p]);
  845. }
  846. element.onload = element.onreadystatechange = function () {
  847. if (!this.readyState || /loaded|complete/.test(this.readyState)) {
  848. item = getItem(doc, obj);
  849. if (item.funs.length > 0) {
  850. item.ready = 1;
  851. for (var fi; fi = item.funs.pop();) {
  852. fi();
  853. }
  854. }
  855. element.onload = element.onreadystatechange = null;
  856. }
  857. };
  858. element.onerror = function () {
  859. throw Error('The load ' + (obj.href || obj.src) + ' fails,check the url settings of file ueditor.config.js ')
  860. };
  861. doc.getElementsByTagName("head")[0].appendChild(element);
  862. }
  863. }(),
  864. /**
  865. * 判断obj对象是否为空
  866. * @method isEmptyObject
  867. * @param { * } obj 需要判断的对象
  868. * @remind 如果判断的对象是NULL, 将直接返回true, 如果是数组且为空, 返回true, 如果是字符串, 且字符串为空,
  869. * 返回true, 如果是普通对象, 且该对象没有任何实例属性, 返回true
  870. * @return { Boolean } 对象是否为空
  871. * @example
  872. * ```javascript
  873. *
  874. * //output: true
  875. * console.log( UE.utils.isEmptyObject( {} ) );
  876. *
  877. * //output: true
  878. * console.log( UE.utils.isEmptyObject( [] ) );
  879. *
  880. * //output: true
  881. * console.log( UE.utils.isEmptyObject( "" ) );
  882. *
  883. * //output: false
  884. * console.log( UE.utils.isEmptyObject( { key: 1 } ) );
  885. *
  886. * //output: false
  887. * console.log( UE.utils.isEmptyObject( [1] ) );
  888. *
  889. * //output: false
  890. * console.log( UE.utils.isEmptyObject( "1" ) );
  891. *
  892. * ```
  893. */
  894. isEmptyObject:function (obj) {
  895. if (obj == null) return true;
  896. if (this.isArray(obj) || this.isString(obj)) return obj.length === 0;
  897. for (var key in obj) if (obj.hasOwnProperty(key)) return false;
  898. return true;
  899. },
  900. /**
  901. * 把rgb格式的颜色值转换成16进制格式
  902. * @method fixColor
  903. * @param { String } rgb格式的颜色值
  904. * @param { String }
  905. * @example
  906. * rgb(255,255,255) => "#ffffff"
  907. */
  908. fixColor:function (name, value) {
  909. if (/color/i.test(name) && /rgba?/.test(value)) {
  910. var array = value.split(",");
  911. if (array.length > 3)
  912. return "";
  913. value = "#";
  914. for (var i = 0, color; color = array[i++];) {
  915. color = parseInt(color.replace(/[^\d]/gi, ''), 10).toString(16);
  916. value += color.length == 1 ? "0" + color : color;
  917. }
  918. value = value.toUpperCase();
  919. }
  920. return value;
  921. },
  922. /**
  923. * 只针对border,padding,margin做了处理,因为性能问题
  924. * @public
  925. * @function
  926. * @param {String} val style字符串
  927. */
  928. optCss:function (val) {
  929. var padding, margin, border;
  930. val = val.replace(/(padding|margin|border)\-([^:]+):([^;]+);?/gi, function (str, key, name, val) {
  931. if (val.split(' ').length == 1) {
  932. switch (key) {
  933. case 'padding':
  934. !padding && (padding = {});
  935. padding[name] = val;
  936. return '';
  937. case 'margin':
  938. !margin && (margin = {});
  939. margin[name] = val;
  940. return '';
  941. case 'border':
  942. return val == 'initial' ? '' : str;
  943. }
  944. }
  945. return str;
  946. });
  947. function opt(obj, name) {
  948. if (!obj) {
  949. return '';
  950. }
  951. var t = obj.top , b = obj.bottom, l = obj.left, r = obj.right, val = '';
  952. if (!t || !l || !b || !r) {
  953. for (var p in obj) {
  954. val += ';' + name + '-' + p + ':' + obj[p] + ';';
  955. }
  956. } else {
  957. val += ';' + name + ':' +
  958. (t == b && b == l && l == r ? t :
  959. t == b && l == r ? (t + ' ' + l) :
  960. l == r ? (t + ' ' + l + ' ' + b) : (t + ' ' + r + ' ' + b + ' ' + l)) + ';'
  961. }
  962. return val;
  963. }
  964. val += opt(padding, 'padding') + opt(margin, 'margin');
  965. return val.replace(/^[ \n\r\t;]*|[ \n\r\t]*$/, '').replace(/;([ \n\r\t]+)|\1;/g, ';')
  966. .replace(/(&((l|g)t|quot|#39))?;{2,}/g, function (a, b) {
  967. return b ? b + ";;" : ';'
  968. });
  969. },
  970. /**
  971. * 克隆对象
  972. * @method clone
  973. * @param { Object } source 源对象
  974. * @return { Object } source的一个副本
  975. */
  976. /**
  977. * 深度克隆对象,将source的属性克隆到target对象, 会覆盖target重名的属性。
  978. * @method clone
  979. * @param { Object } source 源对象
  980. * @param { Object } target 目标对象
  981. * @return { Object } 附加了source对象所有属性的target对象
  982. */
  983. clone:function (source, target) {
  984. var tmp;
  985. target = target || {};
  986. for (var i in source) {
  987. if (source.hasOwnProperty(i)) {
  988. tmp = source[i];
  989. if (typeof tmp == 'object') {
  990. target[i] = utils.isArray(tmp) ? [] : {};
  991. utils.clone(source[i], target[i])
  992. } else {
  993. target[i] = tmp;
  994. }
  995. }
  996. }
  997. return target;
  998. },
  999. /**
  1000. * 把cm/pt为单位的值转换为px为单位的值
  1001. * @method transUnitToPx
  1002. * @param { String } 待转换的带单位的字符串
  1003. * @return { String } 转换为px为计量单位的值的字符串
  1004. * @example
  1005. * ```javascript
  1006. *
  1007. * //output: 500px
  1008. * console.log( UE.utils.transUnitToPx( '20cm' ) );
  1009. *
  1010. * //output: 27px
  1011. * console.log( UE.utils.transUnitToPx( '20pt' ) );
  1012. *
  1013. * ```
  1014. */
  1015. transUnitToPx:function (val) {
  1016. if (!/(pt|cm)/.test(val)) {
  1017. return val
  1018. }
  1019. var unit;
  1020. val.replace(/([\d.]+)(\w+)/, function (str, v, u) {
  1021. val = v;
  1022. unit = u;
  1023. });
  1024. switch (unit) {
  1025. case 'cm':
  1026. val = parseFloat(val) * 25;
  1027. break;
  1028. case 'pt':
  1029. val = Math.round(parseFloat(val) * 96 / 72);
  1030. }
  1031. return val + (val ? 'px' : '');
  1032. },
  1033. /**
  1034. * 在dom树ready之后执行给定的回调函数
  1035. * @method domReady
  1036. * @remind 如果在执行该方法的时候, dom树已经ready, 那么回调函数将立刻执行
  1037. * @param { Function } fn dom树ready之后的回调函数
  1038. * @example
  1039. * ```javascript
  1040. *
  1041. * UE.utils.domReady( function () {
  1042. *
  1043. * console.log('123');
  1044. *
  1045. * } );
  1046. *
  1047. * ```
  1048. */
  1049. domReady:function () {
  1050. var fnArr = [];
  1051. function doReady(doc) {
  1052. //确保onready只执行一次
  1053. doc.isReady = true;
  1054. for (var ci; ci = fnArr.pop(); ci()) {
  1055. }
  1056. }
  1057. return function (onready, win) {
  1058. win = win || window;
  1059. var doc = win.document;
  1060. onready && fnArr.push(onready);
  1061. if (doc.readyState === "complete") {
  1062. doReady(doc);
  1063. } else {
  1064. doc.isReady && doReady(doc);
  1065. if (browser.ie && browser.version != 11) {
  1066. (function () {
  1067. if (doc.isReady) return;
  1068. try {
  1069. doc.documentElement.doScroll("left");
  1070. } catch (error) {
  1071. setTimeout(arguments.callee, 0);
  1072. return;
  1073. }
  1074. doReady(doc);
  1075. })();
  1076. win.attachEvent('onload', function () {
  1077. doReady(doc)
  1078. });
  1079. } else {
  1080. doc.addEventListener("DOMContentLoaded", function () {
  1081. doc.removeEventListener("DOMContentLoaded", arguments.callee, false);
  1082. doReady(doc);
  1083. }, false);
  1084. win.addEventListener('load', function () {
  1085. doReady(doc)
  1086. }, false);
  1087. }
  1088. }
  1089. }
  1090. }(),
  1091. /**
  1092. * 动态添加css样式
  1093. * @method cssRule
  1094. * @param { String } 节点名称
  1095. * @grammar UE.utils.cssRule('添加的样式的节点名称',['样式','放到哪个document上'])
  1096. * @grammar UE.utils.cssRule('body','body{background:#ccc}') => null //给body添加背景颜色
  1097. * @grammar UE.utils.cssRule('body') =>样式的字符串 //取得key值为body的样式的内容,如果没有找到key值先关的样式将返回空,例如刚才那个背景颜色,将返回 body{background:#ccc}
  1098. * @grammar UE.utils.cssRule('body',document) => 返回指定key的样式,并且指定是哪个document
  1099. * @grammar UE.utils.cssRule('body','') =>null //清空给定的key值的背景颜色
  1100. */
  1101. cssRule:browser.ie && browser.version != 11 ? function (key, style, doc) {
  1102. var indexList, index;
  1103. if(style === undefined || style && style.nodeType && style.nodeType == 9){
  1104. //获取样式
  1105. doc = style && style.nodeType && style.nodeType == 9 ? style : (doc || document);
  1106. indexList = doc.indexList || (doc.indexList = {});
  1107. index = indexList[key];
  1108. if(index !== undefined){
  1109. return doc.styleSheets[index].cssText
  1110. }
  1111. return undefined;
  1112. }
  1113. doc = doc || document;
  1114. indexList = doc.indexList || (doc.indexList = {});
  1115. index = indexList[key];
  1116. //清除样式
  1117. if(style === ''){
  1118. if(index!== undefined){
  1119. doc.styleSheets[index].cssText = '';
  1120. delete indexList[key];
  1121. return true
  1122. }
  1123. return false;
  1124. }
  1125. //添加样式
  1126. if(index!== undefined){
  1127. sheetStyle = doc.styleSheets[index];
  1128. }else{
  1129. sheetStyle = doc.createStyleSheet('', index = doc.styleSheets.length);
  1130. indexList[key] = index;
  1131. }
  1132. sheetStyle.cssText = style;
  1133. }: function (key, style, doc) {
  1134. var head, node;
  1135. if(style === undefined || style && style.nodeType && style.nodeType == 9){
  1136. //获取样式
  1137. doc = style && style.nodeType && style.nodeType == 9 ? style : (doc || document);
  1138. node = doc.getElementById(key);
  1139. return node ? node.innerHTML : undefined;
  1140. }
  1141. doc = doc || document;
  1142. node = doc.getElementById(key);
  1143. //清除样式
  1144. if(style === ''){
  1145. if(node){
  1146. node.parentNode.removeChild(node);
  1147. return true
  1148. }
  1149. return false;
  1150. }
  1151. //添加样式
  1152. if(node){
  1153. node.innerHTML = style;
  1154. }else{
  1155. node = doc.createElement('style');
  1156. node.id = key;
  1157. node.innerHTML = style;
  1158. doc.getElementsByTagName('head')[0].appendChild(node);
  1159. }
  1160. },
  1161. sort:function(array,compareFn){
  1162. compareFn = compareFn || function(item1, item2){ return item1.localeCompare(item2);};
  1163. for(var i= 0,len = array.length; i<len; i++){
  1164. for(var j = i,length = array.length; j<length; j++){
  1165. if(compareFn(array[i], array[j]) > 0){
  1166. var t = array[i];
  1167. array[i] = array[j];
  1168. array[j] = t;
  1169. }
  1170. }
  1171. }
  1172. return array;
  1173. },
  1174. serializeParam:function (json) {
  1175. var strArr = [];
  1176. for (var i in json) {
  1177. //忽略默认的几个参数
  1178. if(i=="method" || i=="timeout" || i=="async") continue;
  1179. //传递过来的对象和函数不在提交之列
  1180. if (!((typeof json[i]).toLowerCase() == "function" || (typeof json[i]).toLowerCase() == "object")) {
  1181. strArr.push( encodeURIComponent(i) + "="+encodeURIComponent(json[i]) );
  1182. } else if (utils.isArray(json[i])) {
  1183. //支持传数组内容
  1184. for(var j = 0; j < json[i].length; j++) {
  1185. strArr.push( encodeURIComponent(i) + "[]="+encodeURIComponent(json[i][j]) );
  1186. }
  1187. }
  1188. }
  1189. return strArr.join("&");
  1190. },
  1191. formatUrl:function (url) {
  1192. var u = url.replace(/&&/g, '&');
  1193. u = u.replace(/\?&/g, '?');
  1194. u = u.replace(/&$/g, '');
  1195. u = u.replace(/&#/g, '#');
  1196. u = u.replace(/&+/g, '&');
  1197. return u;
  1198. },
  1199. isCrossDomainUrl:function (url) {
  1200. var a = document.createElement('a');
  1201. a.href = url;
  1202. if (browser.ie) {
  1203. a.href = a.href;
  1204. }
  1205. return !(a.protocol == location.protocol && a.hostname == location.hostname &&
  1206. (a.port == location.port || (a.port == '80' && location.port == '') || (a.port == '' && location.port == '80')));
  1207. },
  1208. clearEmptyAttrs : function(obj){
  1209. for(var p in obj){
  1210. if(obj[p] === ''){
  1211. delete obj[p]
  1212. }
  1213. }
  1214. return obj;
  1215. },
  1216. str2json : function(s){
  1217. if (!utils.isString(s)) return null;
  1218. if (window.JSON) {
  1219. return JSON.parse(s);
  1220. } else {
  1221. return (new Function("return " + utils.trim(s || '')))();
  1222. }
  1223. },
  1224. json2str : (function(){
  1225. if (window.JSON) {
  1226. return JSON.stringify;
  1227. } else {
  1228. var escapeMap = {
  1229. "\b": '\\b',
  1230. "\t": '\\t',
  1231. "\n": '\\n',
  1232. "\f": '\\f',
  1233. "\r": '\\r',
  1234. '"' : '\\"',
  1235. "\\": '\\\\'
  1236. };
  1237. function encodeString(source) {
  1238. if (/["\\\x00-\x1f]/.test(source)) {
  1239. source = source.replace(
  1240. /["\\\x00-\x1f]/g,
  1241. function (match) {
  1242. var c = escapeMap[match];
  1243. if (c) {
  1244. return c;
  1245. }
  1246. c = match.charCodeAt();
  1247. return "\\u00"
  1248. + Math.floor(c / 16).toString(16)
  1249. + (c % 16).toString(16);
  1250. });
  1251. }
  1252. return '"' + source + '"';
  1253. }
  1254. function encodeArray(source) {
  1255. var result = ["["],
  1256. l = source.length,
  1257. preComma, i, item;
  1258. for (i = 0; i < l; i++) {
  1259. item = source[i];
  1260. switch (typeof item) {
  1261. case "undefined":
  1262. case "function":
  1263. case "unknown":
  1264. break;
  1265. default:
  1266. if(preComma) {
  1267. result.push(',');
  1268. }
  1269. result.push(utils.json2str(item));
  1270. preComma = 1;
  1271. }
  1272. }
  1273. result.push("]");
  1274. return result.join("");
  1275. }
  1276. function pad(source) {
  1277. return source < 10 ? '0' + source : source;
  1278. }
  1279. function encodeDate(source){
  1280. return '"' + source.getFullYear() + "-"
  1281. + pad(source.getMonth() + 1) + "-"
  1282. + pad(source.getDate()) + "T"
  1283. + pad(source.getHours()) + ":"
  1284. + pad(source.getMinutes()) + ":"
  1285. + pad(source.getSeconds()) + '"';
  1286. }
  1287. return function (value) {
  1288. switch (typeof value) {
  1289. case 'undefined':
  1290. return 'undefined';
  1291. case 'number':
  1292. return isFinite(value) ? String(value) : "null";
  1293. case 'string':
  1294. return encodeString(value);
  1295. case 'boolean':
  1296. return String(value);
  1297. default:
  1298. if (value === null) {
  1299. return 'null';
  1300. } else if (utils.isArray(value)) {
  1301. return encodeArray(value);
  1302. } else if (utils.isDate(value)) {
  1303. return encodeDate(value);
  1304. } else {
  1305. var result = ['{'],
  1306. encode = utils.json2str,
  1307. preComma,
  1308. item;
  1309. for (var key in value) {
  1310. if (Object.prototype.hasOwnProperty.call(value, key)) {
  1311. item = value[key];
  1312. switch (typeof item) {
  1313. case 'undefined':
  1314. case 'unknown':
  1315. case 'function':
  1316. break;
  1317. default:
  1318. if (preComma) {
  1319. result.push(',');
  1320. }
  1321. preComma = 1;
  1322. result.push(encode(key) + ':' + encode(item));
  1323. }
  1324. }
  1325. }
  1326. result.push('}');
  1327. return result.join('');
  1328. }
  1329. }
  1330. };
  1331. }
  1332. })()
  1333. };
  1334. /**
  1335. * 判断给定的对象是否是字符串
  1336. * @method isString
  1337. * @param { * } object 需要判断的对象
  1338. * @return { Boolean } 给定的对象是否是字符串
  1339. */
  1340. /**
  1341. * 判断给定的对象是否是数组
  1342. * @method isArray
  1343. * @param { * } object 需要判断的对象
  1344. * @return { Boolean } 给定的对象是否是数组
  1345. */
  1346. /**
  1347. * 判断给定的对象是否是一个Function
  1348. * @method isFunction
  1349. * @param { * } object 需要判断的对象
  1350. * @return { Boolean } 给定的对象是否是Function
  1351. */
  1352. /**
  1353. * 判断给定的对象是否是Number
  1354. * @method isNumber
  1355. * @param { * } object 需要判断的对象
  1356. * @return { Boolean } 给定的对象是否是Number
  1357. */
  1358. /**
  1359. * 判断给定的对象是否是一个正则表达式
  1360. * @method isRegExp
  1361. * @param { * } object 需要判断的对象
  1362. * @return { Boolean } 给定的对象是否是正则表达式
  1363. */
  1364. /**
  1365. * 判断给定的对象是否是一个普通对象
  1366. * @method isObject
  1367. * @param { * } object 需要判断的对象
  1368. * @return { Boolean } 给定的对象是否是普通对象
  1369. */
  1370. utils.each(['String', 'Function', 'Array', 'Number', 'RegExp', 'Object', 'Date'], function (v) {
  1371. UE.utils['is' + v] = function (obj) {
  1372. return Object.prototype.toString.apply(obj) == '[object ' + v + ']';
  1373. }
  1374. });
  1375. // core/EventBase.js
  1376. /**
  1377. * UE采用的事件基类
  1378. * @file
  1379. * @module UE
  1380. * @class EventBase
  1381. * @since 1.2.6.1
  1382. */
  1383. /**
  1384. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  1385. * @unfile
  1386. * @module UE
  1387. */
  1388. /**
  1389. * UE采用的事件基类,继承此类的对应类将获取addListener,removeListener,fireEvent方法。
  1390. * 在UE中,Editor以及所有ui实例都继承了该类,故可以在对应的ui对象以及editor对象上使用上述方法。
  1391. * @unfile
  1392. * @module UE
  1393. * @class EventBase
  1394. */
  1395. /**
  1396. * 通过此构造器,子类可以继承EventBase获取事件监听的方法
  1397. * @constructor
  1398. * @example
  1399. * ```javascript
  1400. * UE.EventBase.call(editor);
  1401. * ```
  1402. */
  1403. var EventBase = UE.EventBase = function () {};
  1404. EventBase.prototype = {
  1405. /**
  1406. * 注册事件监听器
  1407. * @method addListener
  1408. * @param { String } types 监听的事件名称,同时监听多个事件使用空格分隔
  1409. * @param { Function } fn 监听的事件被触发时,会执行该回调函数
  1410. * @waining 事件被触发时,监听的函数假如返回的值恒等于true,回调函数的队列中后面的函数将不执行
  1411. * @example
  1412. * ```javascript
  1413. * editor.addListener('selectionchange',function(){
  1414. * console.log("选区已经变化!");
  1415. * })
  1416. * editor.addListener('beforegetcontent aftergetcontent',function(type){
  1417. * if(type == 'beforegetcontent'){
  1418. * //do something
  1419. * }else{
  1420. * //do something
  1421. * }
  1422. * console.log(this.getContent) // this是注册的事件的编辑器实例
  1423. * })
  1424. * ```
  1425. * @see UE.EventBase:fireEvent(String)
  1426. */
  1427. addListener:function (types, listener) {
  1428. types = utils.trim(types).split(/\s+/);
  1429. for (var i = 0, ti; ti = types[i++];) {
  1430. getListener(this, ti, true).push(listener);
  1431. }
  1432. },
  1433. on : function(types, listener){
  1434. return this.addListener(types,listener);
  1435. },
  1436. off : function(types, listener){
  1437. return this.removeListener(types, listener)
  1438. },
  1439. trigger:function(){
  1440. return this.fireEvent.apply(this,arguments);
  1441. },
  1442. /**
  1443. * 移除事件监听器
  1444. * @method removeListener
  1445. * @param { String } types 移除的事件名称,同时移除多个事件使用空格分隔
  1446. * @param { Function } fn 移除监听事件的函数引用
  1447. * @example
  1448. * ```javascript
  1449. * //changeCallback为方法体
  1450. * editor.removeListener("selectionchange",changeCallback);
  1451. * ```
  1452. */
  1453. removeListener:function (types, listener) {
  1454. types = utils.trim(types).split(/\s+/);
  1455. for (var i = 0, ti; ti = types[i++];) {
  1456. utils.removeItem(getListener(this, ti) || [], listener);
  1457. }
  1458. },
  1459. /**
  1460. * 触发事件
  1461. * @method fireEvent
  1462. * @param { String } types 触发的事件名称,同时触发多个事件使用空格分隔
  1463. * @remind 该方法会触发addListener
  1464. * @return { * } 返回触发事件的队列中,最后执行的回调函数的返回值
  1465. * @example
  1466. * ```javascript
  1467. * editor.fireEvent("selectionchange");
  1468. * ```
  1469. */
  1470. /**
  1471. * 触发事件
  1472. * @method fireEvent
  1473. * @param { String } types 触发的事件名称,同时触发多个事件使用空格分隔
  1474. * @param { *... } options 可选参数,可以传入一个或多个参数,会传给事件触发的回调函数
  1475. * @return { * } 返回触发事件的队列中,最后执行的回调函数的返回值
  1476. * @example
  1477. * ```javascript
  1478. *
  1479. * editor.addListener( "selectionchange", function ( type, arg1, arg2 ) {
  1480. *
  1481. * console.log( arg1 + " " + arg2 );
  1482. *
  1483. * } );
  1484. *
  1485. * //触发selectionchange事件, 会执行上面的事件监听器
  1486. * //output: Hello World
  1487. * editor.fireEvent("selectionchange", "Hello", "World");
  1488. * ```
  1489. */
  1490. fireEvent:function () {
  1491. var types = arguments[0];
  1492. types = utils.trim(types).split(' ');
  1493. for (var i = 0, ti; ti = types[i++];) {
  1494. var listeners = getListener(this, ti),
  1495. r, t, k;
  1496. if (listeners) {
  1497. k = listeners.length;
  1498. while (k--) {
  1499. if(!listeners[k])continue;
  1500. t = listeners[k].apply(this, arguments);
  1501. if(t === true){
  1502. return t;
  1503. }
  1504. if (t !== undefined) {
  1505. r = t;
  1506. }
  1507. }
  1508. }
  1509. if (t = this['on' + ti.toLowerCase()]) {
  1510. r = t.apply(this, arguments);
  1511. }
  1512. }
  1513. return r;
  1514. }
  1515. };
  1516. /**
  1517. * 获得对象所拥有监听类型的所有监听器
  1518. * @unfile
  1519. * @module UE
  1520. * @since 1.2.6.1
  1521. * @method getListener
  1522. * @public
  1523. * @param { Object } obj 查询监听器的对象
  1524. * @param { String } type 事件类型
  1525. * @param { Boolean } force 为true且当前所有type类型的侦听器不存在时,创建一个空监听器数组
  1526. * @return { Array } 监听器数组
  1527. */
  1528. function getListener(obj, type, force) {
  1529. var allListeners;
  1530. type = type.toLowerCase();
  1531. return ( ( allListeners = ( obj.__allListeners || force && ( obj.__allListeners = {} ) ) )
  1532. && ( allListeners[type] || force && ( allListeners[type] = [] ) ) );
  1533. }
  1534. // core/dtd.js
  1535. ///import editor.js
  1536. ///import core/dom/dom.js
  1537. ///import core/utils.js
  1538. /**
  1539. * dtd html语义化的体现类
  1540. * @constructor
  1541. * @namespace dtd
  1542. */
  1543. var dtd = dom.dtd = (function() {
  1544. function _( s ) {
  1545. for (var k in s) {
  1546. s[k.toUpperCase()] = s[k];
  1547. }
  1548. return s;
  1549. }
  1550. var X = utils.extend2;
  1551. var A = _({isindex:1,fieldset:1}),
  1552. B = _({input:1,button:1,select:1,textarea:1,label:1}),
  1553. C = X( _({a:1}), B ),
  1554. D = X( {iframe:1}, C ),
  1555. E = _({hr:1,ul:1,menu:1,div:1,blockquote:1,noscript:1,table:1,center:1,address:1,dir:1,pre:1,h5:1,dl:1,h4:1,noframes:1,h6:1,ol:1,h1:1,h3:1,h2:1}),
  1556. F = _({ins:1,del:1,script:1,style:1}),
  1557. G = X( _({b:1,acronym:1,bdo:1,'var':1,'#':1,abbr:1,code:1,br:1,i:1,cite:1,kbd:1,u:1,strike:1,s:1,tt:1,strong:1,q:1,samp:1,em:1,dfn:1,span:1}), F ),
  1558. H = X( _({sub:1,img:1,embed:1,object:1,sup:1,basefont:1,map:1,applet:1,font:1,big:1,small:1}), G ),
  1559. I = X( _({p:1}), H ),
  1560. J = X( _({iframe:1}), H, B ),
  1561. K = _({img:1,embed:1,noscript:1,br:1,kbd:1,center:1,button:1,basefont:1,h5:1,h4:1,samp:1,h6:1,ol:1,h1:1,h3:1,h2:1,form:1,font:1,'#':1,select:1,menu:1,ins:1,abbr:1,label:1,code:1,table:1,script:1,cite:1,input:1,iframe:1,strong:1,textarea:1,noframes:1,big:1,small:1,span:1,hr:1,sub:1,bdo:1,'var':1,div:1,object:1,sup:1,strike:1,dir:1,map:1,dl:1,applet:1,del:1,isindex:1,fieldset:1,ul:1,b:1,acronym:1,a:1,blockquote:1,i:1,u:1,s:1,tt:1,address:1,q:1,pre:1,p:1,em:1,dfn:1}),
  1562. L = X( _({a:0}), J ),//a不能被切开,所以把他
  1563. M = _({tr:1}),
  1564. N = _({'#':1}),
  1565. O = X( _({param:1}), K ),
  1566. P = X( _({form:1}), A, D, E, I ),
  1567. Q = _({li:1,ol:1,ul:1}),
  1568. R = _({style:1,script:1}),
  1569. S = _({base:1,link:1,meta:1,title:1}),
  1570. T = X( S, R ),
  1571. U = _({head:1,body:1}),
  1572. V = _({html:1});
  1573. var block = _({address:1,blockquote:1,center:1,dir:1,div:1,dl:1,fieldset:1,form:1,h1:1,h2:1,h3:1,h4:1,h5:1,h6:1,hr:1,isindex:1,menu:1,noframes:1,ol:1,p:1,pre:1,table:1,ul:1}),
  1574. empty = _({area:1,base:1,basefont:1,br:1,col:1,command:1,dialog:1,embed:1,hr:1,img:1,input:1,isindex:1,keygen:1,link:1,meta:1,param:1,source:1,track:1,wbr:1});
  1575. return _({
  1576. // $ 表示自定的属性
  1577. // body外的元素列表.
  1578. $nonBodyContent: X( V, U, S ),
  1579. //块结构元素列表
  1580. $block : block,
  1581. //内联元素列表
  1582. $inline : L,
  1583. $inlineWithA : X(_({a:1}),L),
  1584. $body : X( _({script:1,style:1}), block ),
  1585. $cdata : _({script:1,style:1}),
  1586. //自闭和元素
  1587. $empty : empty,
  1588. //不是自闭合,但不能让range选中里边
  1589. $nonChild : _({iframe:1,textarea:1}),
  1590. //列表元素列表
  1591. $listItem : _({dd:1,dt:1,li:1}),
  1592. //列表根元素列表
  1593. $list: _({ul:1,ol:1,dl:1}),
  1594. //不能认为是空的元素
  1595. $isNotEmpty : _({table:1,ul:1,ol:1,dl:1,iframe:1,area:1,base:1,col:1,hr:1,img:1,embed:1,input:1,link:1,meta:1,param:1,h1:1,h2:1,h3:1,h4:1,h5:1,h6:1}),
  1596. //如果没有子节点就可以删除的元素列表,像span,a
  1597. $removeEmpty : _({a:1,abbr:1,acronym:1,address:1,b:1,bdo:1,big:1,cite:1,code:1,del:1,dfn:1,em:1,font:1,i:1,ins:1,label:1,kbd:1,q:1,s:1,samp:1,small:1,span:1,strike:1,strong:1,sub:1,sup:1,tt:1,u:1,'var':1}),
  1598. $removeEmptyBlock : _({'p':1,'div':1}),
  1599. //在table元素里的元素列表
  1600. $tableContent : _({caption:1,col:1,colgroup:1,tbody:1,td:1,tfoot:1,th:1,thead:1,tr:1,table:1}),
  1601. //不转换的标签
  1602. $notTransContent : _({pre:1,script:1,style:1,textarea:1}),
  1603. html: U,
  1604. head: T,
  1605. style: N,
  1606. script: N,
  1607. body: P,
  1608. base: {},
  1609. link: {},
  1610. meta: {},
  1611. title: N,
  1612. col : {},
  1613. tr : _({td:1,th:1}),
  1614. img : {},
  1615. embed: {},
  1616. colgroup : _({thead:1,col:1,tbody:1,tr:1,tfoot:1}),
  1617. noscript : P,
  1618. td : P,
  1619. br : {},
  1620. th : P,
  1621. center : P,
  1622. kbd : L,
  1623. button : X( I, E ),
  1624. basefont : {},
  1625. h5 : L,
  1626. h4 : L,
  1627. samp : L,
  1628. h6 : L,
  1629. ol : Q,
  1630. h1 : L,
  1631. h3 : L,
  1632. option : N,
  1633. h2 : L,
  1634. form : X( A, D, E, I ),
  1635. select : _({optgroup:1,option:1}),
  1636. font : L,
  1637. ins : L,
  1638. menu : Q,
  1639. abbr : L,
  1640. label : L,
  1641. table : _({thead:1,col:1,tbody:1,tr:1,colgroup:1,caption:1,tfoot:1}),
  1642. code : L,
  1643. tfoot : M,
  1644. cite : L,
  1645. li : P,
  1646. input : {},
  1647. iframe : P,
  1648. strong : L,
  1649. textarea : N,
  1650. noframes : P,
  1651. big : L,
  1652. small : L,
  1653. //trace:
  1654. span :_({'#':1,br:1,b:1,strong:1,u:1,i:1,em:1,sub:1,sup:1,strike:1,span:1}),
  1655. hr : L,
  1656. dt : L,
  1657. sub : L,
  1658. optgroup : _({option:1}),
  1659. param : {},
  1660. bdo : L,
  1661. 'var' : L,
  1662. div : P,
  1663. object : O,
  1664. sup : L,
  1665. dd : P,
  1666. strike : L,
  1667. area : {},
  1668. dir : Q,
  1669. map : X( _({area:1,form:1,p:1}), A, F, E ),
  1670. applet : O,
  1671. dl : _({dt:1,dd:1}),
  1672. del : L,
  1673. isindex : {},
  1674. fieldset : X( _({legend:1}), K ),
  1675. thead : M,
  1676. ul : Q,
  1677. acronym : L,
  1678. b : L,
  1679. a : X( _({a:1}), J ),
  1680. blockquote :X(_({td:1,tr:1,tbody:1,li:1}),P),
  1681. caption : L,
  1682. i : L,
  1683. u : L,
  1684. tbody : M,
  1685. s : L,
  1686. address : X( D, I ),
  1687. tt : L,
  1688. legend : L,
  1689. q : L,
  1690. pre : X( G, C ),
  1691. p : X(_({'a':1}),L),
  1692. em :L,
  1693. dfn : L
  1694. });
  1695. })();
  1696. // core/domUtils.js
  1697. /**
  1698. * Dom操作工具包
  1699. * @file
  1700. * @module UE.dom.domUtils
  1701. * @since 1.2.6.1
  1702. */
  1703. /**
  1704. * Dom操作工具包
  1705. * @unfile
  1706. * @module UE.dom.domUtils
  1707. */
  1708. function getDomNode(node, start, ltr, startFromChild, fn, guard) {
  1709. var tmpNode = startFromChild && node[start],
  1710. parent;
  1711. !tmpNode && (tmpNode = node[ltr]);
  1712. while (!tmpNode && (parent = (parent || node).parentNode)) {
  1713. if (parent.tagName == 'BODY' || guard && !guard(parent)) {
  1714. return null;
  1715. }
  1716. tmpNode = parent[ltr];
  1717. }
  1718. if (tmpNode && fn && !fn(tmpNode)) {
  1719. return getDomNode(tmpNode, start, ltr, false, fn);
  1720. }
  1721. return tmpNode;
  1722. }
  1723. var attrFix = ie && browser.version < 9 ? {
  1724. tabindex:"tabIndex",
  1725. readonly:"readOnly",
  1726. "for":"htmlFor",
  1727. "class":"className",
  1728. maxlength:"maxLength",
  1729. cellspacing:"cellSpacing",
  1730. cellpadding:"cellPadding",
  1731. rowspan:"rowSpan",
  1732. colspan:"colSpan",
  1733. usemap:"useMap",
  1734. frameborder:"frameBorder"
  1735. } : {
  1736. tabindex:"tabIndex",
  1737. readonly:"readOnly"
  1738. },
  1739. styleBlock = utils.listToMap([
  1740. '-webkit-box', '-moz-box', 'block' ,
  1741. 'list-item' , 'table' , 'table-row-group' ,
  1742. 'table-header-group', 'table-footer-group' ,
  1743. 'table-row' , 'table-column-group' , 'table-column' ,
  1744. 'table-cell' , 'table-caption'
  1745. ]);
  1746. var domUtils = dom.domUtils = {
  1747. //节点常量
  1748. NODE_ELEMENT:1,
  1749. NODE_DOCUMENT:9,
  1750. NODE_TEXT:3,
  1751. NODE_COMMENT:8,
  1752. NODE_DOCUMENT_FRAGMENT:11,
  1753. //位置关系
  1754. POSITION_IDENTICAL:0,
  1755. POSITION_DISCONNECTED:1,
  1756. POSITION_FOLLOWING:2,
  1757. POSITION_PRECEDING:4,
  1758. POSITION_IS_CONTAINED:8,
  1759. POSITION_CONTAINS:16,
  1760. //ie6使用其他的会有一段空白出现
  1761. fillChar:ie && browser.version == '6' ? '\ufeff' : '\u200B',
  1762. //-------------------------Node部分--------------------------------
  1763. keys:{
  1764. /*Backspace*/ 8:1, /*Delete*/ 46:1,
  1765. /*Shift*/ 16:1, /*Ctrl*/ 17:1, /*Alt*/ 18:1,
  1766. 37:1, 38:1, 39:1, 40:1,
  1767. 13:1 /*enter*/
  1768. },
  1769. /**
  1770. * 获取节点A相对于节点B的位置关系
  1771. * @method getPosition
  1772. * @param { Node } nodeA 需要查询位置关系的节点A
  1773. * @param { Node } nodeB 需要查询位置关系的节点B
  1774. * @return { Number } 节点A与节点B的关系
  1775. * @example
  1776. * ```javascript
  1777. * //output: 20
  1778. * var position = UE.dom.domUtils.getPosition( document.documentElement, document.body );
  1779. *
  1780. * switch ( position ) {
  1781. *
  1782. * //0
  1783. * case UE.dom.domUtils.POSITION_IDENTICAL:
  1784. * console.log('元素相同');
  1785. * break;
  1786. * //1
  1787. * case UE.dom.domUtils.POSITION_DISCONNECTED:
  1788. * console.log('两个节点在不同的文档中');
  1789. * break;
  1790. * //2
  1791. * case UE.dom.domUtils.POSITION_FOLLOWING:
  1792. * console.log('节点A在节点B之后');
  1793. * break;
  1794. * //4
  1795. * case UE.dom.domUtils.POSITION_PRECEDING;
  1796. * console.log('节点A在节点B之前');
  1797. * break;
  1798. * //8
  1799. * case UE.dom.domUtils.POSITION_IS_CONTAINED:
  1800. * console.log('节点A被节点B包含');
  1801. * break;
  1802. * case 10:
  1803. * console.log('节点A被节点B包含且节点A在节点B之后');
  1804. * break;
  1805. * //16
  1806. * case UE.dom.domUtils.POSITION_CONTAINS:
  1807. * console.log('节点A包含节点B');
  1808. * break;
  1809. * case 20:
  1810. * console.log('节点A包含节点B且节点A在节点B之前');
  1811. * break;
  1812. *
  1813. * }
  1814. * ```
  1815. */
  1816. getPosition:function (nodeA, nodeB) {
  1817. // 如果两个节点是同一个节点
  1818. if (nodeA === nodeB) {
  1819. // domUtils.POSITION_IDENTICAL
  1820. return 0;
  1821. }
  1822. var node,
  1823. parentsA = [nodeA],
  1824. parentsB = [nodeB];
  1825. node = nodeA;
  1826. while (node = node.parentNode) {
  1827. // 如果nodeB是nodeA的祖先节点
  1828. if (node === nodeB) {
  1829. // domUtils.POSITION_IS_CONTAINED + domUtils.POSITION_FOLLOWING
  1830. return 10;
  1831. }
  1832. parentsA.push(node);
  1833. }
  1834. node = nodeB;
  1835. while (node = node.parentNode) {
  1836. // 如果nodeA是nodeB的祖先节点
  1837. if (node === nodeA) {
  1838. // domUtils.POSITION_CONTAINS + domUtils.POSITION_PRECEDING
  1839. return 20;
  1840. }
  1841. parentsB.push(node);
  1842. }
  1843. parentsA.reverse();
  1844. parentsB.reverse();
  1845. if (parentsA[0] !== parentsB[0]) {
  1846. // domUtils.POSITION_DISCONNECTED
  1847. return 1;
  1848. }
  1849. var i = -1;
  1850. while (i++, parentsA[i] === parentsB[i]) {
  1851. }
  1852. nodeA = parentsA[i];
  1853. nodeB = parentsB[i];
  1854. while (nodeA = nodeA.nextSibling) {
  1855. if (nodeA === nodeB) {
  1856. // domUtils.POSITION_PRECEDING
  1857. return 4
  1858. }
  1859. }
  1860. // domUtils.POSITION_FOLLOWING
  1861. return 2;
  1862. },
  1863. /**
  1864. * 检测节点node在父节点中的索引位置
  1865. * @method getNodeIndex
  1866. * @param { Node } node 需要检测的节点对象
  1867. * @return { Number } 该节点在父节点中的位置
  1868. * @see UE.dom.domUtils.getNodeIndex(Node,Boolean)
  1869. */
  1870. /**
  1871. * 检测节点node在父节点中的索引位置, 根据给定的mergeTextNode参数决定是否要合并多个连续的文本节点为一个节点
  1872. * @method getNodeIndex
  1873. * @param { Node } node 需要检测的节点对象
  1874. * @param { Boolean } mergeTextNode 是否合并多个连续的文本节点为一个节点
  1875. * @return { Number } 该节点在父节点中的位置
  1876. * @example
  1877. * ```javascript
  1878. *
  1879. * var node = document.createElement("div");
  1880. *
  1881. * node.appendChild( document.createTextNode( "hello" ) );
  1882. * node.appendChild( document.createTextNode( "world" ) );
  1883. * node.appendChild( node = document.createElement( "div" ) );
  1884. *
  1885. * //output: 2
  1886. * console.log( UE.dom.domUtils.getNodeIndex( node ) );
  1887. *
  1888. * //output: 1
  1889. * console.log( UE.dom.domUtils.getNodeIndex( node, true ) );
  1890. *
  1891. * ```
  1892. */
  1893. getNodeIndex:function (node, ignoreTextNode) {
  1894. var preNode = node,
  1895. i = 0;
  1896. while (preNode = preNode.previousSibling) {
  1897. if (ignoreTextNode && preNode.nodeType == 3) {
  1898. if(preNode.nodeType != preNode.nextSibling.nodeType ){
  1899. i++;
  1900. }
  1901. continue;
  1902. }
  1903. i++;
  1904. }
  1905. return i;
  1906. },
  1907. /**
  1908. * 检测节点node是否在给定的document对象上
  1909. * @method inDoc
  1910. * @param { Node } node 需要检测的节点对象
  1911. * @param { DomDocument } doc 需要检测的document对象
  1912. * @return { Boolean } 该节点node是否在给定的document的dom树上
  1913. * @example
  1914. * ```javascript
  1915. *
  1916. * var node = document.createElement("div");
  1917. *
  1918. * //output: false
  1919. * console.log( UE.do.domUtils.inDoc( node, document ) );
  1920. *
  1921. * document.body.appendChild( node );
  1922. *
  1923. * //output: true
  1924. * console.log( UE.do.domUtils.inDoc( node, document ) );
  1925. *
  1926. * ```
  1927. */
  1928. inDoc:function (node, doc) {
  1929. return domUtils.getPosition(node, doc) == 10;
  1930. },
  1931. /**
  1932. * 根据给定的过滤规则filterFn, 查找符合该过滤规则的node节点的第一个祖先节点,
  1933. * 查找的起点是给定node节点的父节点。
  1934. * @method findParent
  1935. * @param { Node } node 需要查找的节点
  1936. * @param { Function } filterFn 自定义的过滤方法。
  1937. * @warning 查找的终点是到body节点为止
  1938. * @remind 自定义的过滤方法filterFn接受一个Node对象作为参数, 该对象代表当前执行检测的祖先节点。 如果该
  1939. * 节点满足过滤条件, 则要求返回true, 这时将直接返回该节点作为findParent()的结果, 否则, 请返回false。
  1940. * @return { Node | Null } 如果找到符合过滤条件的节点, 就返回该节点, 否则返回NULL
  1941. * @example
  1942. * ```javascript
  1943. * var filterNode = UE.dom.domUtils.findParent( document.body.firstChild, function ( node ) {
  1944. *
  1945. * //由于查找的终点是body节点, 所以永远也不会匹配当前过滤器的条件, 即这里永远会返回false
  1946. * return node.tagName === "HTML";
  1947. *
  1948. * } );
  1949. *
  1950. * //output: true
  1951. * console.log( filterNode === null );
  1952. * ```
  1953. */
  1954. /**
  1955. * 根据给定的过滤规则filterFn, 查找符合该过滤规则的node节点的第一个祖先节点,
  1956. * 如果includeSelf的值为true,则查找的起点是给定的节点node, 否则, 起点是node的父节点
  1957. * @method findParent
  1958. * @param { Node } node 需要查找的节点
  1959. * @param { Function } filterFn 自定义的过滤方法。
  1960. * @param { Boolean } includeSelf 查找过程是否包含自身
  1961. * @warning 查找的终点是到body节点为止
  1962. * @remind 自定义的过滤方法filterFn接受一个Node对象作为参数, 该对象代表当前执行检测的祖先节点。 如果该
  1963. * 节点满足过滤条件, 则要求返回true, 这时将直接返回该节点作为findParent()的结果, 否则, 请返回false。
  1964. * @remind 如果includeSelf为true, 则过滤器第一次执行时的参数会是节点本身。
  1965. * 反之, 过滤器第一次执行时的参数将是该节点的父节点。
  1966. * @return { Node | Null } 如果找到符合过滤条件的节点, 就返回该节点, 否则返回NULL
  1967. * @example
  1968. * ```html
  1969. * <body>
  1970. *
  1971. * <div id="test">
  1972. * </div>
  1973. *
  1974. * <script type="text/javascript">
  1975. *
  1976. * //output: DIV, BODY
  1977. * var filterNode = UE.dom.domUtils.findParent( document.getElementById( "test" ), function ( node ) {
  1978. *
  1979. * console.log( node.tagName );
  1980. * return false;
  1981. *
  1982. * }, true );
  1983. *
  1984. * </script>
  1985. * </body>
  1986. * ```
  1987. */
  1988. findParent:function (node, filterFn, includeSelf) {
  1989. if (node && !domUtils.isBody(node)) {
  1990. node = includeSelf ? node : node.parentNode;
  1991. while (node) {
  1992. if (!filterFn || filterFn(node) || domUtils.isBody(node)) {
  1993. return filterFn && !filterFn(node) && domUtils.isBody(node) ? null : node;
  1994. }
  1995. node = node.parentNode;
  1996. }
  1997. }
  1998. return null;
  1999. },
  2000. /**
  2001. * 查找node的节点名为tagName的第一个祖先节点, 查找的起点是node节点的父节点。
  2002. * @method findParentByTagName
  2003. * @param { Node } node 需要查找的节点对象
  2004. * @param { Array } tagNames 需要查找的父节点的名称数组
  2005. * @warning 查找的终点是到body节点为止
  2006. * @return { Node | NULL } 如果找到符合条件的节点, 则返回该节点, 否则返回NULL
  2007. * @example
  2008. * ```javascript
  2009. * var node = UE.dom.domUtils.findParentByTagName( document.getElementsByTagName("div")[0], [ "BODY" ] );
  2010. * //output: BODY
  2011. * console.log( node.tagName );
  2012. * ```
  2013. */
  2014. /**
  2015. * 查找node的节点名为tagName的祖先节点, 如果includeSelf的值为true,则查找的起点是给定的节点node,
  2016. * 否则, 起点是node的父节点。
  2017. * @method findParentByTagName
  2018. * @param { Node } node 需要查找的节点对象
  2019. * @param { Array } tagNames 需要查找的父节点的名称数组
  2020. * @param { Boolean } includeSelf 查找过程是否包含node节点自身
  2021. * @warning 查找的终点是到body节点为止
  2022. * @return { Node | NULL } 如果找到符合条件的节点, 则返回该节点, 否则返回NULL
  2023. * @example
  2024. * ```javascript
  2025. * var queryTarget = document.getElementsByTagName("div")[0];
  2026. * var node = UE.dom.domUtils.findParentByTagName( queryTarget, [ "DIV" ], true );
  2027. * //output: true
  2028. * console.log( queryTarget === node );
  2029. * ```
  2030. */
  2031. findParentByTagName:function (node, tagNames, includeSelf, excludeFn) {
  2032. tagNames = utils.listToMap(utils.isArray(tagNames) ? tagNames : [tagNames]);
  2033. return domUtils.findParent(node, function (node) {
  2034. return tagNames[node.tagName] && !(excludeFn && excludeFn(node));
  2035. }, includeSelf);
  2036. },
  2037. /**
  2038. * 查找节点node的祖先节点集合, 查找的起点是给定节点的父节点,结果集中不包含给定的节点。
  2039. * @method findParents
  2040. * @param { Node } node 需要查找的节点对象
  2041. * @return { Array } 给定节点的祖先节点数组
  2042. * @grammar UE.dom.domUtils.findParents(node) => Array //返回一个祖先节点数组集合,不包含自身
  2043. * @grammar UE.dom.domUtils.findParents(node,includeSelf) => Array //返回一个祖先节点数组集合,includeSelf指定是否包含自身
  2044. * @grammar UE.dom.domUtils.findParents(node,includeSelf,filterFn) => Array //返回一个祖先节点数组集合,filterFn指定过滤条件,返回true的node将被选取
  2045. * @grammar UE.dom.domUtils.findParents(node,includeSelf,filterFn,closerFirst) => Array //返回一个祖先节点数组集合,closerFirst为true的话,node的直接父亲节点是数组的第0个
  2046. */
  2047. /**
  2048. * 查找节点node的祖先节点集合, 如果includeSelf的值为true,
  2049. * 则返回的结果集中允许出现当前给定的节点, 否则, 该节点不会出现在其结果集中。
  2050. * @method findParents
  2051. * @param { Node } node 需要查找的节点对象
  2052. * @param { Boolean } includeSelf 查找的结果中是否允许包含当前查找的节点对象
  2053. * @return { Array } 给定节点的祖先节点数组
  2054. */
  2055. findParents:function (node, includeSelf, filterFn, closerFirst) {
  2056. var parents = includeSelf && ( filterFn && filterFn(node) || !filterFn ) ? [node] : [];
  2057. while (node = domUtils.findParent(node, filterFn)) {
  2058. parents.push(node);
  2059. }
  2060. return closerFirst ? parents : parents.reverse();
  2061. },
  2062. /**
  2063. * 在节点node后面插入新节点newNode
  2064. * @method insertAfter
  2065. * @param { Node } node 目标节点
  2066. * @param { Node } newNode 新插入的节点, 该节点将置于目标节点之后
  2067. * @return { Node } 新插入的节点
  2068. */
  2069. insertAfter:function (node, newNode) {
  2070. return node.nextSibling ? node.parentNode.insertBefore(newNode, node.nextSibling):
  2071. node.parentNode.appendChild(newNode);
  2072. },
  2073. /**
  2074. * 删除节点node及其下属的所有节点
  2075. * @method remove
  2076. * @param { Node } node 需要删除的节点对象
  2077. * @return { Node } 返回刚删除的节点对象
  2078. * @example
  2079. * ```html
  2080. * <div id="test">
  2081. * <div id="child">你好</div>
  2082. * </div>
  2083. * <script>
  2084. * UE.dom.domUtils.remove( document.body, false );
  2085. * //output: false
  2086. * console.log( document.getElementById( "child" ) !== null );
  2087. * </script>
  2088. * ```
  2089. */
  2090. /**
  2091. * 删除节点node,并根据keepChildren的值决定是否保留子节点
  2092. * @method remove
  2093. * @param { Node } node 需要删除的节点对象
  2094. * @param { Boolean } keepChildren 是否需要保留子节点
  2095. * @return { Node } 返回刚删除的节点对象
  2096. * @example
  2097. * ```html
  2098. * <div id="test">
  2099. * <div id="child">你好</div>
  2100. * </div>
  2101. * <script>
  2102. * UE.dom.domUtils.remove( document.body, true );
  2103. * //output: true
  2104. * console.log( document.getElementById( "child" ) !== null );
  2105. * </script>
  2106. * ```
  2107. */
  2108. remove:function (node, keepChildren) {
  2109. var parent = node.parentNode,
  2110. child;
  2111. if (parent) {
  2112. if (keepChildren && node.hasChildNodes()) {
  2113. while (child = node.firstChild) {
  2114. parent.insertBefore(child, node);
  2115. }
  2116. }
  2117. parent.removeChild(node);
  2118. }
  2119. return node;
  2120. },
  2121. /**
  2122. * 取得node节点的下一个兄弟节点, 如果该节点其后没有兄弟节点, 则递归查找其父节点之后的第一个兄弟节点,
  2123. * 直到找到满足条件的节点或者递归到BODY节点之后才会结束。
  2124. * @method getNextDomNode
  2125. * @param { Node } node 需要获取其后的兄弟节点的节点对象
  2126. * @return { Node | NULL } 如果找满足条件的节点, 则返回该节点, 否则返回NULL
  2127. * @example
  2128. * ```html
  2129. * <body>
  2130. * <div id="test">
  2131. * <span></span>
  2132. * </div>
  2133. * <i>xxx</i>
  2134. * </body>
  2135. * <script>
  2136. *
  2137. * //output: i节点
  2138. * console.log( UE.dom.domUtils.getNextDomNode( document.getElementById( "test" ) ) );
  2139. *
  2140. * </script>
  2141. * ```
  2142. * @example
  2143. * ```html
  2144. * <body>
  2145. * <div>
  2146. * <span></span>
  2147. * <i id="test">xxx</i>
  2148. * </div>
  2149. * <b>xxx</b>
  2150. * </body>
  2151. * <script>
  2152. *
  2153. * //由于id为test的i节点之后没有兄弟节点, 则查找其父节点(div)后面的兄弟节点
  2154. * //output: b节点
  2155. * console.log( UE.dom.domUtils.getNextDomNode( document.getElementById( "test" ) ) );
  2156. *
  2157. * </script>
  2158. * ```
  2159. */
  2160. /**
  2161. * 取得node节点的下一个兄弟节点, 如果startFromChild的值为ture,则先获取其子节点,
  2162. * 如果有子节点则直接返回第一个子节点;如果没有子节点或者startFromChild的值为false,
  2163. * 则执行<a href="#UE.dom.domUtils.getNextDomNode(Node)">getNextDomNode(Node node)</a>的查找过程。
  2164. * @method getNextDomNode
  2165. * @param { Node } node 需要获取其后的兄弟节点的节点对象
  2166. * @param { Boolean } startFromChild 查找过程是否从其子节点开始
  2167. * @return { Node | NULL } 如果找满足条件的节点, 则返回该节点, 否则返回NULL
  2168. * @see UE.dom.domUtils.getNextDomNode(Node)
  2169. */
  2170. getNextDomNode:function (node, startFromChild, filterFn, guard) {
  2171. return getDomNode(node, 'firstChild', 'nextSibling', startFromChild, filterFn, guard);
  2172. },
  2173. getPreDomNode:function (node, startFromChild, filterFn, guard) {
  2174. return getDomNode(node, 'lastChild', 'previousSibling', startFromChild, filterFn, guard);
  2175. },
  2176. /**
  2177. * 检测节点node是否属是UEditor定义的bookmark节点
  2178. * @method isBookmarkNode
  2179. * @private
  2180. * @param { Node } node 需要检测的节点对象
  2181. * @return { Boolean } 是否是bookmark节点
  2182. * @example
  2183. * ```html
  2184. * <span id="_baidu_bookmark_1"></span>
  2185. * <script>
  2186. * var bookmarkNode = document.getElementById("_baidu_bookmark_1");
  2187. * //output: true
  2188. * console.log( UE.dom.domUtils.isBookmarkNode( bookmarkNode ) );
  2189. * </script>
  2190. * ```
  2191. */
  2192. isBookmarkNode:function (node) {
  2193. return node.nodeType == 1 && node.id && /^_baidu_bookmark_/i.test(node.id);
  2194. },
  2195. /**
  2196. * 获取节点node所属的window对象
  2197. * @method getWindow
  2198. * @param { Node } node 节点对象
  2199. * @return { Window } 当前节点所属的window对象
  2200. * @example
  2201. * ```javascript
  2202. * //output: true
  2203. * console.log( UE.dom.domUtils.getWindow( document.body ) === window );
  2204. * ```
  2205. */
  2206. getWindow:function (node) {
  2207. var doc = node.ownerDocument || node;
  2208. return doc.defaultView || doc.parentWindow;
  2209. },
  2210. /**
  2211. * 获取离nodeA与nodeB最近的公共的祖先节点
  2212. * @method getCommonAncestor
  2213. * @param { Node } nodeA 第一个节点
  2214. * @param { Node } nodeB 第二个节点
  2215. * @remind 如果给定的两个节点是同一个节点, 将直接返回该节点。
  2216. * @return { Node | NULL } 如果未找到公共节点, 返回NULL, 否则返回最近的公共祖先节点。
  2217. * @example
  2218. * ```javascript
  2219. * var commonAncestor = UE.dom.domUtils.getCommonAncestor( document.body, document.body.firstChild );
  2220. * //output: true
  2221. * console.log( commonAncestor.tagName.toLowerCase() === 'body' );
  2222. * ```
  2223. */
  2224. getCommonAncestor:function (nodeA, nodeB) {
  2225. if (nodeA === nodeB)
  2226. return nodeA;
  2227. var parentsA = [nodeA] , parentsB = [nodeB], parent = nodeA, i = -1;
  2228. while (parent = parent.parentNode) {
  2229. if (parent === nodeB) {
  2230. return parent;
  2231. }
  2232. parentsA.push(parent);
  2233. }
  2234. parent = nodeB;
  2235. while (parent = parent.parentNode) {
  2236. if (parent === nodeA)
  2237. return parent;
  2238. parentsB.push(parent);
  2239. }
  2240. parentsA.reverse();
  2241. parentsB.reverse();
  2242. while (i++, parentsA[i] === parentsB[i]) {
  2243. }
  2244. return i == 0 ? null : parentsA[i - 1];
  2245. },
  2246. /**
  2247. * 清除node节点左右连续为空的兄弟inline节点
  2248. * @method clearEmptySibling
  2249. * @param { Node } node 执行的节点对象, 如果该节点的左右连续的兄弟节点是空的inline节点,
  2250. * 则这些兄弟节点将被删除
  2251. * @grammar UE.dom.domUtils.clearEmptySibling(node,ignoreNext) //ignoreNext指定是否忽略右边空节点
  2252. * @grammar UE.dom.domUtils.clearEmptySibling(node,ignoreNext,ignorePre) //ignorePre指定是否忽略左边空节点
  2253. * @example
  2254. * ```html
  2255. * <body>
  2256. * <div></div>
  2257. * <span id="test"></span>
  2258. * <i></i>
  2259. * <b></b>
  2260. * <em>xxx</em>
  2261. * <span></span>
  2262. * </body>
  2263. * <script>
  2264. *
  2265. * UE.dom.domUtils.clearEmptySibling( document.getElementById( "test" ) );
  2266. *
  2267. * //output: <div></div><span id="test"></span><em>xxx</em><span></span>
  2268. * console.log( document.body.innerHTML );
  2269. *
  2270. * </script>
  2271. * ```
  2272. */
  2273. /**
  2274. * 清除node节点左右连续为空的兄弟inline节点, 如果ignoreNext的值为true,
  2275. * 则忽略对右边兄弟节点的操作。
  2276. * @method clearEmptySibling
  2277. * @param { Node } node 执行的节点对象, 如果该节点的左右连续的兄弟节点是空的inline节点,
  2278. * @param { Boolean } ignoreNext 是否忽略忽略对右边的兄弟节点的操作
  2279. * 则这些兄弟节点将被删除
  2280. * @see UE.dom.domUtils.clearEmptySibling(Node)
  2281. */
  2282. /**
  2283. * 清除node节点左右连续为空的兄弟inline节点, 如果ignoreNext的值为true,
  2284. * 则忽略对右边兄弟节点的操作, 如果ignorePre的值为true,则忽略对左边兄弟节点的操作。
  2285. * @method clearEmptySibling
  2286. * @param { Node } node 执行的节点对象, 如果该节点的左右连续的兄弟节点是空的inline节点,
  2287. * @param { Boolean } ignoreNext 是否忽略忽略对右边的兄弟节点的操作
  2288. * @param { Boolean } ignorePre 是否忽略忽略对左边的兄弟节点的操作
  2289. * 则这些兄弟节点将被删除
  2290. * @see UE.dom.domUtils.clearEmptySibling(Node)
  2291. */
  2292. clearEmptySibling:function (node, ignoreNext, ignorePre) {
  2293. function clear(next, dir) {
  2294. var tmpNode;
  2295. while (next && !domUtils.isBookmarkNode(next) && (domUtils.isEmptyInlineElement(next)
  2296. //这里不能把空格算进来会吧空格干掉,出现文字间的空格丢掉了
  2297. || !new RegExp('[^\t\n\r' + domUtils.fillChar + ']').test(next.nodeValue) )) {
  2298. tmpNode = next[dir];
  2299. domUtils.remove(next);
  2300. next = tmpNode;
  2301. }
  2302. }
  2303. !ignoreNext && clear(node.nextSibling, 'nextSibling');
  2304. !ignorePre && clear(node.previousSibling, 'previousSibling');
  2305. },
  2306. /**
  2307. * 将一个文本节点textNode拆分成两个文本节点,offset指定拆分位置
  2308. * @method split
  2309. * @param { Node } textNode 需要拆分的文本节点对象
  2310. * @param { int } offset 需要拆分的位置, 位置计算从0开始
  2311. * @return { Node } 拆分后形成的新节点
  2312. * @example
  2313. * ```html
  2314. * <div id="test">abcdef</div>
  2315. * <script>
  2316. * var newNode = UE.dom.domUtils.split( document.getElementById( "test" ).firstChild, 3 );
  2317. * //output: def
  2318. * console.log( newNode.nodeValue );
  2319. * </script>
  2320. * ```
  2321. */
  2322. split:function (node, offset) {
  2323. var doc = node.ownerDocument;
  2324. if (browser.ie && offset == node.nodeValue.length) {
  2325. var next = doc.createTextNode('');
  2326. return domUtils.insertAfter(node, next);
  2327. }
  2328. var retval = node.splitText(offset);
  2329. //ie8下splitText不会跟新childNodes,我们手动触发他的更新
  2330. if (browser.ie8) {
  2331. var tmpNode = doc.createTextNode('');
  2332. domUtils.insertAfter(retval, tmpNode);
  2333. domUtils.remove(tmpNode);
  2334. }
  2335. return retval;
  2336. },
  2337. /**
  2338. * 检测文本节点textNode是否为空节点(包括空格、换行、占位符等字符)
  2339. * @method isWhitespace
  2340. * @param { Node } node 需要检测的节点对象
  2341. * @return { Boolean } 检测的节点是否为空
  2342. * @example
  2343. * ```html
  2344. * <div id="test">
  2345. *
  2346. * </div>
  2347. * <script>
  2348. * //output: true
  2349. * console.log( UE.dom.domUtils.isWhitespace( document.getElementById("test").firstChild ) );
  2350. * </script>
  2351. * ```
  2352. */
  2353. isWhitespace:function (node) {
  2354. return !new RegExp('[^ \t\n\r' + domUtils.fillChar + ']').test(node.nodeValue);
  2355. },
  2356. /**
  2357. * 获取元素element相对于viewport的位置坐标
  2358. * @method getXY
  2359. * @param { Node } element 需要计算位置的节点对象
  2360. * @return { Object } 返回形如{x:left,y:top}的一个key-value映射对象, 其中键x代表水平偏移距离,
  2361. * y代表垂直偏移距离。
  2362. *
  2363. * @example
  2364. * ```javascript
  2365. * var location = UE.dom.domUtils.getXY( document.getElementById("test") );
  2366. * //output: test的坐标为: 12, 24
  2367. * console.log( 'test的坐标为: ', location.x, ',', location.y );
  2368. * ```
  2369. */
  2370. getXY:function (element) {
  2371. var x = 0, y = 0;
  2372. while (element.offsetParent) {
  2373. y += element.offsetTop;
  2374. x += element.offsetLeft;
  2375. element = element.offsetParent;
  2376. }
  2377. return { 'x':x, 'y':y};
  2378. },
  2379. /**
  2380. * 为元素element绑定原生DOM事件,type为事件类型,handler为处理函数
  2381. * @method on
  2382. * @param { Node } element 需要绑定事件的节点对象
  2383. * @param { String } type 绑定的事件类型
  2384. * @param { Function } handler 事件处理器
  2385. * @example
  2386. * ```javascript
  2387. * UE.dom.domUtils.on(document.body,"click",function(e){
  2388. * //e为事件对象,this为被点击元素对戏那个
  2389. * });
  2390. * ```
  2391. */
  2392. /**
  2393. * 为元素element绑定原生DOM事件,type为事件类型,handler为处理函数
  2394. * @method on
  2395. * @param { Node } element 需要绑定事件的节点对象
  2396. * @param { Array } type 绑定的事件类型数组
  2397. * @param { Function } handler 事件处理器
  2398. * @example
  2399. * ```javascript
  2400. * UE.dom.domUtils.on(document.body,["click","mousedown"],function(evt){
  2401. * //evt为事件对象,this为被点击元素对象
  2402. * });
  2403. * ```
  2404. */
  2405. on:function (element, type, handler) {
  2406. var types = utils.isArray(type) ? type : utils.trim(type).split(/\s+/),
  2407. k = types.length;
  2408. if (k) while (k--) {
  2409. type = types[k];
  2410. if (element.addEventListener) {
  2411. element.addEventListener(type, handler, false);
  2412. } else {
  2413. if (!handler._d) {
  2414. handler._d = {
  2415. els : []
  2416. };
  2417. }
  2418. var key = type + handler.toString(),index = utils.indexOf(handler._d.els,element);
  2419. if (!handler._d[key] || index == -1) {
  2420. if(index == -1){
  2421. handler._d.els.push(element);
  2422. }
  2423. if(!handler._d[key]){
  2424. handler._d[key] = function (evt) {
  2425. return handler.call(evt.srcElement, evt || window.event);
  2426. };
  2427. }
  2428. element.attachEvent('on' + type, handler._d[key]);
  2429. }
  2430. }
  2431. }
  2432. element = null;
  2433. },
  2434. /**
  2435. * 解除DOM事件绑定
  2436. * @method un
  2437. * @param { Node } element 需要解除事件绑定的节点对象
  2438. * @param { String } type 需要接触绑定的事件类型
  2439. * @param { Function } handler 对应的事件处理器
  2440. * @example
  2441. * ```javascript
  2442. * UE.dom.domUtils.un(document.body,"click",function(evt){
  2443. * //evt为事件对象,this为被点击元素对象
  2444. * });
  2445. * ```
  2446. */
  2447. /**
  2448. * 解除DOM事件绑定
  2449. * @method un
  2450. * @param { Node } element 需要解除事件绑定的节点对象
  2451. * @param { Array } type 需要接触绑定的事件类型数组
  2452. * @param { Function } handler 对应的事件处理器
  2453. * @example
  2454. * ```javascript
  2455. * UE.dom.domUtils.un(document.body, ["click","mousedown"],function(evt){
  2456. * //evt为事件对象,this为被点击元素对象
  2457. * });
  2458. * ```
  2459. */
  2460. un:function (element, type, handler) {
  2461. var types = utils.isArray(type) ? type : utils.trim(type).split(/\s+/),
  2462. k = types.length;
  2463. if (k) while (k--) {
  2464. type = types[k];
  2465. if (element.removeEventListener) {
  2466. element.removeEventListener(type, handler, false);
  2467. } else {
  2468. var key = type + handler.toString();
  2469. try{
  2470. element.detachEvent('on' + type, handler._d ? handler._d[key] : handler);
  2471. }catch(e){}
  2472. if (handler._d && handler._d[key]) {
  2473. var index = utils.indexOf(handler._d.els,element);
  2474. if(index!=-1){
  2475. handler._d.els.splice(index,1);
  2476. }
  2477. handler._d.els.length == 0 && delete handler._d[key];
  2478. }
  2479. }
  2480. }
  2481. },
  2482. /**
  2483. * 比较节点nodeA与节点nodeB是否具有相同的标签名、属性名以及属性值
  2484. * @method isSameElement
  2485. * @param { Node } nodeA 需要比较的节点
  2486. * @param { Node } nodeB 需要比较的节点
  2487. * @return { Boolean } 两个节点是否具有相同的标签名、属性名以及属性值
  2488. * @example
  2489. * ```html
  2490. * <span style="font-size:12px">ssss</span>
  2491. * <span style="font-size:12px">bbbbb</span>
  2492. * <span style="font-size:13px">ssss</span>
  2493. * <span style="font-size:14px">bbbbb</span>
  2494. *
  2495. * <script>
  2496. *
  2497. * var nodes = document.getElementsByTagName( "span" );
  2498. *
  2499. * //output: true
  2500. * console.log( UE.dom.domUtils.isSameElement( nodes[0], nodes[1] ) );
  2501. *
  2502. * //output: false
  2503. * console.log( UE.dom.domUtils.isSameElement( nodes[2], nodes[3] ) );
  2504. *
  2505. * </script>
  2506. * ```
  2507. */
  2508. isSameElement:function (nodeA, nodeB) {
  2509. if (nodeA.tagName != nodeB.tagName) {
  2510. return false;
  2511. }
  2512. var thisAttrs = nodeA.attributes,
  2513. otherAttrs = nodeB.attributes;
  2514. if (!ie && thisAttrs.length != otherAttrs.length) {
  2515. return false;
  2516. }
  2517. var attrA, attrB, al = 0, bl = 0;
  2518. for (var i = 0; attrA = thisAttrs[i++];) {
  2519. if (attrA.nodeName == 'style') {
  2520. if (attrA.specified) {
  2521. al++;
  2522. }
  2523. if (domUtils.isSameStyle(nodeA, nodeB)) {
  2524. continue;
  2525. } else {
  2526. return false;
  2527. }
  2528. }
  2529. if (ie) {
  2530. if (attrA.specified) {
  2531. al++;
  2532. attrB = otherAttrs.getNamedItem(attrA.nodeName);
  2533. } else {
  2534. continue;
  2535. }
  2536. } else {
  2537. attrB = nodeB.attributes[attrA.nodeName];
  2538. }
  2539. if (!attrB.specified || attrA.nodeValue != attrB.nodeValue) {
  2540. return false;
  2541. }
  2542. }
  2543. // 有可能attrB的属性包含了attrA的属性之外还有自己的属性
  2544. if (ie) {
  2545. for (i = 0; attrB = otherAttrs[i++];) {
  2546. if (attrB.specified) {
  2547. bl++;
  2548. }
  2549. }
  2550. if (al != bl) {
  2551. return false;
  2552. }
  2553. }
  2554. return true;
  2555. },
  2556. /**
  2557. * 判断节点nodeA与节点nodeB的元素的style属性是否一致
  2558. * @method isSameStyle
  2559. * @param { Node } nodeA 需要比较的节点
  2560. * @param { Node } nodeB 需要比较的节点
  2561. * @return { Boolean } 两个节点是否具有相同的style属性值
  2562. * @example
  2563. * ```html
  2564. * <span style="font-size:12px">ssss</span>
  2565. * <span style="font-size:12px">bbbbb</span>
  2566. * <span style="font-size:13px">ssss</span>
  2567. * <span style="font-size:14px">bbbbb</span>
  2568. *
  2569. * <script>
  2570. *
  2571. * var nodes = document.getElementsByTagName( "span" );
  2572. *
  2573. * //output: true
  2574. * console.log( UE.dom.domUtils.isSameStyle( nodes[0], nodes[1] ) );
  2575. *
  2576. * //output: false
  2577. * console.log( UE.dom.domUtils.isSameStyle( nodes[2], nodes[3] ) );
  2578. *
  2579. * </script>
  2580. * ```
  2581. */
  2582. isSameStyle:function (nodeA, nodeB) {
  2583. var styleA = nodeA.style.cssText.replace(/( ?; ?)/g, ';').replace(/( ?: ?)/g, ':'),
  2584. styleB = nodeB.style.cssText.replace(/( ?; ?)/g, ';').replace(/( ?: ?)/g, ':');
  2585. if (browser.opera) {
  2586. styleA = nodeA.style;
  2587. styleB = nodeB.style;
  2588. if (styleA.length != styleB.length)
  2589. return false;
  2590. for (var p in styleA) {
  2591. if (/^(\d+|csstext)$/i.test(p)) {
  2592. continue;
  2593. }
  2594. if (styleA[p] != styleB[p]) {
  2595. return false;
  2596. }
  2597. }
  2598. return true;
  2599. }
  2600. if (!styleA || !styleB) {
  2601. return styleA == styleB;
  2602. }
  2603. styleA = styleA.split(';');
  2604. styleB = styleB.split(';');
  2605. if (styleA.length != styleB.length) {
  2606. return false;
  2607. }
  2608. for (var i = 0, ci; ci = styleA[i++];) {
  2609. if (utils.indexOf(styleB, ci) == -1) {
  2610. return false;
  2611. }
  2612. }
  2613. return true;
  2614. },
  2615. /**
  2616. * 检查节点node是否为block元素
  2617. * @method isBlockElm
  2618. * @param { Node } node 需要检测的节点对象
  2619. * @return { Boolean } 是否是block元素节点
  2620. * @warning 该方法的判断规则如下: 如果该元素原本是block元素, 则不论该元素当前的css样式是什么都会返回true;
  2621. * 否则,检测该元素的css样式, 如果该元素当前是block元素, 则返回true。 其余情况下都返回false。
  2622. * @example
  2623. * ```html
  2624. * <span id="test1" style="display: block"></span>
  2625. * <span id="test2"></span>
  2626. * <div id="test3" style="display: inline"></div>
  2627. *
  2628. * <script>
  2629. *
  2630. * //output: true
  2631. * console.log( UE.dom.domUtils.isBlockElm( document.getElementById("test1") ) );
  2632. *
  2633. * //output: false
  2634. * console.log( UE.dom.domUtils.isBlockElm( document.getElementById("test2") ) );
  2635. *
  2636. * //output: true
  2637. * console.log( UE.dom.domUtils.isBlockElm( document.getElementById("test3") ) );
  2638. *
  2639. * </script>
  2640. * ```
  2641. */
  2642. isBlockElm:function (node) {
  2643. return node.nodeType == 1 && (dtd.$block[node.tagName] || styleBlock[domUtils.getComputedStyle(node, 'display')]) && !dtd.$nonChild[node.tagName];
  2644. },
  2645. /**
  2646. * 检测node节点是否为body节点
  2647. * @method isBody
  2648. * @param { Element } node 需要检测的dom元素
  2649. * @return { Boolean } 给定的元素是否是body元素
  2650. * @example
  2651. * ```javascript
  2652. * //output: true
  2653. * console.log( UE.dom.domUtils.isBody( document.body ) );
  2654. * ```
  2655. */
  2656. isBody:function (node) {
  2657. return node && node.nodeType == 1 && node.tagName.toLowerCase() == 'body';
  2658. },
  2659. /**
  2660. * 以node节点为分界,将该节点的指定祖先节点parent拆分成两个独立的节点,
  2661. * 拆分形成的两个节点之间是node节点
  2662. * @method breakParent
  2663. * @param { Node } node 作为分界的节点对象
  2664. * @param { Node } parent 该节点必须是node节点的祖先节点, 且是block节点。
  2665. * @return { Node } 给定的node分界节点
  2666. * @example
  2667. * ```javascript
  2668. *
  2669. * var node = document.createElement("span"),
  2670. * wrapNode = document.createElement( "div" ),
  2671. * parent = document.createElement("p");
  2672. *
  2673. * parent.appendChild( node );
  2674. * wrapNode.appendChild( parent );
  2675. *
  2676. * //拆分前
  2677. * //output: <p><span></span></p>
  2678. * console.log( wrapNode.innerHTML );
  2679. *
  2680. *
  2681. * UE.dom.domUtils.breakParent( node, parent );
  2682. * //拆分后
  2683. * //output: <p></p><span></span><p></p>
  2684. * console.log( wrapNode.innerHTML );
  2685. *
  2686. * ```
  2687. */
  2688. breakParent:function (node, parent) {
  2689. var tmpNode,
  2690. parentClone = node,
  2691. clone = node,
  2692. leftNodes,
  2693. rightNodes;
  2694. do {
  2695. parentClone = parentClone.parentNode;
  2696. if (leftNodes) {
  2697. tmpNode = parentClone.cloneNode(false);
  2698. tmpNode.appendChild(leftNodes);
  2699. leftNodes = tmpNode;
  2700. tmpNode = parentClone.cloneNode(false);
  2701. tmpNode.appendChild(rightNodes);
  2702. rightNodes = tmpNode;
  2703. } else {
  2704. leftNodes = parentClone.cloneNode(false);
  2705. rightNodes = leftNodes.cloneNode(false);
  2706. }
  2707. while (tmpNode = clone.previousSibling) {
  2708. leftNodes.insertBefore(tmpNode, leftNodes.firstChild);
  2709. }
  2710. while (tmpNode = clone.nextSibling) {
  2711. rightNodes.appendChild(tmpNode);
  2712. }
  2713. clone = parentClone;
  2714. } while (parent !== parentClone);
  2715. tmpNode = parent.parentNode;
  2716. tmpNode.insertBefore(leftNodes, parent);
  2717. tmpNode.insertBefore(rightNodes, parent);
  2718. tmpNode.insertBefore(node, rightNodes);
  2719. domUtils.remove(parent);
  2720. return node;
  2721. },
  2722. /**
  2723. * 检查节点node是否是空inline节点
  2724. * @method isEmptyInlineElement
  2725. * @param { Node } node 需要检测的节点对象
  2726. * @return { Number } 如果给定的节点是空的inline节点, 则返回1, 否则返回0。
  2727. * @example
  2728. * ```html
  2729. * <b><i></i></b> => 1
  2730. * <b><i></i><u></u></b> => 1
  2731. * <b></b> => 1
  2732. * <b>xx<i></i></b> => 0
  2733. * ```
  2734. */
  2735. isEmptyInlineElement:function (node) {
  2736. if (node.nodeType != 1 || !dtd.$removeEmpty[ node.tagName ]) {
  2737. return 0;
  2738. }
  2739. node = node.firstChild;
  2740. while (node) {
  2741. //如果是创建的bookmark就跳过
  2742. if (domUtils.isBookmarkNode(node)) {
  2743. return 0;
  2744. }
  2745. if (node.nodeType == 1 && !domUtils.isEmptyInlineElement(node) ||
  2746. node.nodeType == 3 && !domUtils.isWhitespace(node)
  2747. ) {
  2748. return 0;
  2749. }
  2750. node = node.nextSibling;
  2751. }
  2752. return 1;
  2753. },
  2754. /**
  2755. * 删除node节点下首尾两端的空白文本子节点
  2756. * @method trimWhiteTextNode
  2757. * @param { Element } node 需要执行删除操作的元素对象
  2758. * @example
  2759. * ```javascript
  2760. * var node = document.createElement("div");
  2761. *
  2762. * node.appendChild( document.createTextNode( "" ) );
  2763. *
  2764. * node.appendChild( document.createElement("div") );
  2765. *
  2766. * node.appendChild( document.createTextNode( "" ) );
  2767. *
  2768. * //3
  2769. * console.log( node.childNodes.length );
  2770. *
  2771. * UE.dom.domUtils.trimWhiteTextNode( node );
  2772. *
  2773. * //1
  2774. * console.log( node.childNodes.length );
  2775. * ```
  2776. */
  2777. trimWhiteTextNode:function (node) {
  2778. function remove(dir) {
  2779. var child;
  2780. while ((child = node[dir]) && child.nodeType == 3 && domUtils.isWhitespace(child)) {
  2781. node.removeChild(child);
  2782. }
  2783. }
  2784. remove('firstChild');
  2785. remove('lastChild');
  2786. },
  2787. /**
  2788. * 合并node节点下相同的子节点
  2789. * @name mergeChild
  2790. * @desc
  2791. * UE.dom.domUtils.mergeChild(node,tagName) //tagName要合并的子节点的标签
  2792. * @example
  2793. * <p><span style="font-size:12px;">xx<span style="font-size:12px;">aa</span>xx</span></p>
  2794. * ==> UE.dom.domUtils.mergeChild(node,'span')
  2795. * <p><span style="font-size:12px;">xxaaxx</span></p>
  2796. */
  2797. mergeChild:function (node, tagName, attrs) {
  2798. var list = domUtils.getElementsByTagName(node, node.tagName.toLowerCase());
  2799. for (var i = 0, ci; ci = list[i++];) {
  2800. if (!ci.parentNode || domUtils.isBookmarkNode(ci)) {
  2801. continue;
  2802. }
  2803. //span单独处理
  2804. if (ci.tagName.toLowerCase() == 'span') {
  2805. if (node === ci.parentNode) {
  2806. domUtils.trimWhiteTextNode(node);
  2807. if (node.childNodes.length == 1) {
  2808. node.style.cssText = ci.style.cssText + ";" + node.style.cssText;
  2809. domUtils.remove(ci, true);
  2810. continue;
  2811. }
  2812. }
  2813. ci.style.cssText = node.style.cssText + ';' + ci.style.cssText;
  2814. if (attrs) {
  2815. var style = attrs.style;
  2816. if (style) {
  2817. style = style.split(';');
  2818. for (var j = 0, s; s = style[j++];) {
  2819. ci.style[utils.cssStyleToDomStyle(s.split(':')[0])] = s.split(':')[1];
  2820. }
  2821. }
  2822. }
  2823. if (domUtils.isSameStyle(ci, node)) {
  2824. domUtils.remove(ci, true);
  2825. }
  2826. continue;
  2827. }
  2828. if (domUtils.isSameElement(node, ci)) {
  2829. domUtils.remove(ci, true);
  2830. }
  2831. }
  2832. },
  2833. /**
  2834. * 原生方法getElementsByTagName的封装
  2835. * @method getElementsByTagName
  2836. * @param { Node } node 目标节点对象
  2837. * @param { String } tagName 需要查找的节点的tagName, 多个tagName以空格分割
  2838. * @return { Array } 符合条件的节点集合
  2839. */
  2840. getElementsByTagName:function (node, name,filter) {
  2841. if(filter && utils.isString(filter)){
  2842. var className = filter;
  2843. filter = function(node){return domUtils.hasClass(node,className)}
  2844. }
  2845. name = utils.trim(name).replace(/[ ]{2,}/g,' ').split(' ');
  2846. var arr = [];
  2847. for(var n = 0,ni;ni=name[n++];){
  2848. var list = node.getElementsByTagName(ni);
  2849. for (var i = 0, ci; ci = list[i++];) {
  2850. if(!filter || filter(ci))
  2851. arr.push(ci);
  2852. }
  2853. }
  2854. return arr;
  2855. },
  2856. /**
  2857. * 将节点node提取到父节点上
  2858. * @method mergeToParent
  2859. * @param { Element } node 需要提取的元素对象
  2860. * @example
  2861. * ```html
  2862. * <div id="parent">
  2863. * <div id="sub">
  2864. * <span id="child"></span>
  2865. * </div>
  2866. * </div>
  2867. *
  2868. * <script>
  2869. *
  2870. * var child = document.getElementById( "child" );
  2871. *
  2872. * //output: sub
  2873. * console.log( child.parentNode.id );
  2874. *
  2875. * UE.dom.domUtils.mergeToParent( child );
  2876. *
  2877. * //output: parent
  2878. * console.log( child.parentNode.id );
  2879. *
  2880. * </script>
  2881. * ```
  2882. */
  2883. mergeToParent:function (node) {
  2884. var parent = node.parentNode;
  2885. while (parent && dtd.$removeEmpty[parent.tagName]) {
  2886. if (parent.tagName == node.tagName || parent.tagName == 'A') {//针对a标签单独处理
  2887. domUtils.trimWhiteTextNode(parent);
  2888. //span需要特殊处理 不处理这样的情况 <span stlye="color:#fff">xxx<span style="color:#ccc">xxx</span>xxx</span>
  2889. if (parent.tagName == 'SPAN' && !domUtils.isSameStyle(parent, node)
  2890. || (parent.tagName == 'A' && node.tagName == 'SPAN')) {
  2891. if (parent.childNodes.length > 1 || parent !== node.parentNode) {
  2892. node.style.cssText = parent.style.cssText + ";" + node.style.cssText;
  2893. parent = parent.parentNode;
  2894. continue;
  2895. } else {
  2896. parent.style.cssText += ";" + node.style.cssText;
  2897. //trace:952 a标签要保持下划线
  2898. if (parent.tagName == 'A') {
  2899. parent.style.textDecoration = 'underline';
  2900. }
  2901. }
  2902. }
  2903. if (parent.tagName != 'A') {
  2904. parent === node.parentNode && domUtils.remove(node, true);
  2905. break;
  2906. }
  2907. }
  2908. parent = parent.parentNode;
  2909. }
  2910. },
  2911. /**
  2912. * 合并节点node的左右兄弟节点
  2913. * @method mergeSibling
  2914. * @param { Element } node 需要合并的目标节点
  2915. * @example
  2916. * ```html
  2917. * <b>xxxx</b><b id="test">ooo</b><b>xxxx</b>
  2918. *
  2919. * <script>
  2920. * var demoNode = document.getElementById("test");
  2921. * UE.dom.domUtils.mergeSibling( demoNode );
  2922. * //output: xxxxoooxxxx
  2923. * console.log( demoNode.innerHTML );
  2924. * </script>
  2925. * ```
  2926. */
  2927. /**
  2928. * 合并节点node的左右兄弟节点, 可以根据给定的条件选择是否忽略合并左节点。
  2929. * @method mergeSibling
  2930. * @param { Element } node 需要合并的目标节点
  2931. * @param { Boolean } ignorePre 是否忽略合并左节点
  2932. * @example
  2933. * ```html
  2934. * <b>xxxx</b><b id="test">ooo</b><b>xxxx</b>
  2935. *
  2936. * <script>
  2937. * var demoNode = document.getElementById("test");
  2938. * UE.dom.domUtils.mergeSibling( demoNode, true );
  2939. * //output: oooxxxx
  2940. * console.log( demoNode.innerHTML );
  2941. * </script>
  2942. * ```
  2943. */
  2944. /**
  2945. * 合并节点node的左右兄弟节点,可以根据给定的条件选择是否忽略合并左右节点。
  2946. * @method mergeSibling
  2947. * @param { Element } node 需要合并的目标节点
  2948. * @param { Boolean } ignorePre 是否忽略合并左节点
  2949. * @param { Boolean } ignoreNext 是否忽略合并右节点
  2950. * @remind 如果同时忽略左右节点, 则该操作什么也不会做
  2951. * @example
  2952. * ```html
  2953. * <b>xxxx</b><b id="test">ooo</b><b>xxxx</b>
  2954. *
  2955. * <script>
  2956. * var demoNode = document.getElementById("test");
  2957. * UE.dom.domUtils.mergeSibling( demoNode, false, true );
  2958. * //output: xxxxooo
  2959. * console.log( demoNode.innerHTML );
  2960. * </script>
  2961. * ```
  2962. */
  2963. mergeSibling:function (node, ignorePre, ignoreNext) {
  2964. function merge(rtl, start, node) {
  2965. var next;
  2966. if ((next = node[rtl]) && !domUtils.isBookmarkNode(next) && next.nodeType == 1 && domUtils.isSameElement(node, next)) {
  2967. while (next.firstChild) {
  2968. if (start == 'firstChild') {
  2969. node.insertBefore(next.lastChild, node.firstChild);
  2970. } else {
  2971. node.appendChild(next.firstChild);
  2972. }
  2973. }
  2974. domUtils.remove(next);
  2975. }
  2976. }
  2977. !ignorePre && merge('previousSibling', 'firstChild', node);
  2978. !ignoreNext && merge('nextSibling', 'lastChild', node);
  2979. },
  2980. /**
  2981. * 设置节点node及其子节点不会被选中
  2982. * @method unSelectable
  2983. * @param { Element } node 需要执行操作的dom元素
  2984. * @remind 执行该操作后的节点, 将不能被鼠标选中
  2985. * @example
  2986. * ```javascript
  2987. * UE.dom.domUtils.unSelectable( document.body );
  2988. * ```
  2989. */
  2990. unSelectable:ie && browser.ie9below || browser.opera ? function (node) {
  2991. //for ie9
  2992. node.onselectstart = function () {
  2993. return false;
  2994. };
  2995. node.onclick = node.onkeyup = node.onkeydown = function () {
  2996. return false;
  2997. };
  2998. node.unselectable = 'on';
  2999. node.setAttribute("unselectable", "on");
  3000. for (var i = 0, ci; ci = node.all[i++];) {
  3001. switch (ci.tagName.toLowerCase()) {
  3002. case 'iframe' :
  3003. case 'textarea' :
  3004. case 'input' :
  3005. case 'select' :
  3006. break;
  3007. default :
  3008. ci.unselectable = 'on';
  3009. node.setAttribute("unselectable", "on");
  3010. }
  3011. }
  3012. } : function (node) {
  3013. node.style.MozUserSelect =
  3014. node.style.webkitUserSelect =
  3015. node.style.msUserSelect =
  3016. node.style.KhtmlUserSelect = 'none';
  3017. },
  3018. /**
  3019. * 删除节点node上的指定属性名称的属性
  3020. * @method removeAttributes
  3021. * @param { Node } node 需要删除属性的节点对象
  3022. * @param { String } attrNames 可以是空格隔开的多个属性名称,该操作将会依次删除相应的属性
  3023. * @example
  3024. * ```html
  3025. * <div id="wrap">
  3026. * <span style="font-size:14px;" id="test" name="followMe">xxxxx</span>
  3027. * </div>
  3028. *
  3029. * <script>
  3030. *
  3031. * UE.dom.domUtils.removeAttributes( document.getElementById( "test" ), "id name" );
  3032. *
  3033. * //output: <span style="font-size:14px;">xxxxx</span>
  3034. * console.log( document.getElementById("wrap").innerHTML );
  3035. *
  3036. * </script>
  3037. * ```
  3038. */
  3039. /**
  3040. * 删除节点node上的指定属性名称的属性
  3041. * @method removeAttributes
  3042. * @param { Node } node 需要删除属性的节点对象
  3043. * @param { Array } attrNames 需要删除的属性名数组
  3044. * @example
  3045. * ```html
  3046. * <div id="wrap">
  3047. * <span style="font-size:14px;" id="test" name="followMe">xxxxx</span>
  3048. * </div>
  3049. *
  3050. * <script>
  3051. *
  3052. * UE.dom.domUtils.removeAttributes( document.getElementById( "test" ), ["id", "name"] );
  3053. *
  3054. * //output: <span style="font-size:14px;">xxxxx</span>
  3055. * console.log( document.getElementById("wrap").innerHTML );
  3056. *
  3057. * </script>
  3058. * ```
  3059. */
  3060. removeAttributes:function (node, attrNames) {
  3061. attrNames = utils.isArray(attrNames) ? attrNames : utils.trim(attrNames).replace(/[ ]{2,}/g,' ').split(' ');
  3062. for (var i = 0, ci; ci = attrNames[i++];) {
  3063. ci = attrFix[ci] || ci;
  3064. switch (ci) {
  3065. case 'className':
  3066. node[ci] = '';
  3067. break;
  3068. case 'style':
  3069. node.style.cssText = '';
  3070. var val = node.getAttributeNode('style');
  3071. !browser.ie && val && node.removeAttributeNode(val);
  3072. }
  3073. node.removeAttribute(ci);
  3074. }
  3075. },
  3076. /**
  3077. * 在doc下创建一个标签名为tag,属性为attrs的元素
  3078. * @method createElement
  3079. * @param { DomDocument } doc 新创建的元素属于该document节点创建
  3080. * @param { String } tagName 需要创建的元素的标签名
  3081. * @param { Object } attrs 新创建的元素的属性key-value集合
  3082. * @return { Element } 新创建的元素对象
  3083. * @example
  3084. * ```javascript
  3085. * var ele = UE.dom.domUtils.createElement( document, 'div', {
  3086. * id: 'test'
  3087. * } );
  3088. *
  3089. * //output: DIV
  3090. * console.log( ele.tagName );
  3091. *
  3092. * //output: test
  3093. * console.log( ele.id );
  3094. *
  3095. * ```
  3096. */
  3097. createElement:function (doc, tag, attrs) {
  3098. return domUtils.setAttributes(doc.createElement(tag), attrs)
  3099. },
  3100. /**
  3101. * 为节点node添加属性attrs,attrs为属性键值对
  3102. * @method setAttributes
  3103. * @param { Element } node 需要设置属性的元素对象
  3104. * @param { Object } attrs 需要设置的属性名-值对
  3105. * @return { Element } 设置属性的元素对象
  3106. * @example
  3107. * ```html
  3108. * <span id="test"></span>
  3109. *
  3110. * <script>
  3111. *
  3112. * var testNode = UE.dom.domUtils.setAttributes( document.getElementById( "test" ), {
  3113. * id: 'demo'
  3114. * } );
  3115. *
  3116. * //output: demo
  3117. * console.log( testNode.id );
  3118. *
  3119. * </script>
  3120. *
  3121. */
  3122. setAttributes:function (node, attrs) {
  3123. for (var attr in attrs) {
  3124. if(attrs.hasOwnProperty(attr)){
  3125. var value = attrs[attr];
  3126. switch (attr) {
  3127. case 'class':
  3128. //ie下要这样赋值,setAttribute不起作用
  3129. node.className = value;
  3130. break;
  3131. case 'style' :
  3132. node.style.cssText = node.style.cssText + ";" + value;
  3133. break;
  3134. case 'innerHTML':
  3135. node[attr] = value;
  3136. break;
  3137. case 'value':
  3138. node.value = value;
  3139. break;
  3140. default:
  3141. node.setAttribute(attrFix[attr] || attr, value);
  3142. }
  3143. }
  3144. }
  3145. return node;
  3146. },
  3147. /**
  3148. * 获取元素element经过计算后的样式值
  3149. * @method getComputedStyle
  3150. * @param { Element } element 需要获取样式的元素对象
  3151. * @param { String } styleName 需要获取的样式名
  3152. * @return { String } 获取到的样式值
  3153. * @example
  3154. * ```html
  3155. * <style type="text/css">
  3156. * #test {
  3157. * font-size: 15px;
  3158. * }
  3159. * </style>
  3160. *
  3161. * <span id="test"></span>
  3162. *
  3163. * <script>
  3164. * //output: 15px
  3165. * console.log( UE.dom.domUtils.getComputedStyle( document.getElementById( "test" ), 'font-size' ) );
  3166. * </script>
  3167. * ```
  3168. */
  3169. getComputedStyle:function (element, styleName) {
  3170. //一下的属性单独处理
  3171. var pros = 'width height top left';
  3172. if(pros.indexOf(styleName) > -1){
  3173. return element['offset' + styleName.replace(/^\w/,function(s){return s.toUpperCase()})] + 'px';
  3174. }
  3175. //忽略文本节点
  3176. if (element.nodeType == 3) {
  3177. element = element.parentNode;
  3178. }
  3179. //ie下font-size若body下定义了font-size,则从currentStyle里会取到这个font-size. 取不到实际值,故此修改.
  3180. if (browser.ie && browser.version < 9 && styleName == 'font-size' && !element.style.fontSize &&
  3181. !dtd.$empty[element.tagName] && !dtd.$nonChild[element.tagName]) {
  3182. var span = element.ownerDocument.createElement('span');
  3183. span.style.cssText = 'padding:0;border:0;font-family:simsun;';
  3184. span.innerHTML = '.';
  3185. element.appendChild(span);
  3186. var result = span.offsetHeight;
  3187. element.removeChild(span);
  3188. span = null;
  3189. return result + 'px';
  3190. }
  3191. try {
  3192. var value = domUtils.getStyle(element, styleName) ||
  3193. (window.getComputedStyle ? domUtils.getWindow(element).getComputedStyle(element, '').getPropertyValue(styleName) :
  3194. ( element.currentStyle || element.style )[utils.cssStyleToDomStyle(styleName)]);
  3195. } catch (e) {
  3196. return "";
  3197. }
  3198. return utils.transUnitToPx(utils.fixColor(styleName, value));
  3199. },
  3200. /**
  3201. * 删除元素element指定的className
  3202. * @method removeClasses
  3203. * @param { Element } ele 需要删除class的元素节点
  3204. * @param { String } classNames 需要删除的className, 多个className之间以空格分开
  3205. * @example
  3206. * ```html
  3207. * <span id="test" class="test1 test2 test3">xxx</span>
  3208. *
  3209. * <script>
  3210. *
  3211. * var testNode = document.getElementById( "test" );
  3212. * UE.dom.domUtils.removeClasses( testNode, "test1 test2" );
  3213. *
  3214. * //output: test3
  3215. * console.log( testNode.className );
  3216. *
  3217. * </script>
  3218. * ```
  3219. */
  3220. /**
  3221. * 删除元素element指定的className
  3222. * @method removeClasses
  3223. * @param { Element } ele 需要删除class的元素节点
  3224. * @param { Array } classNames 需要删除的className数组
  3225. * @example
  3226. * ```html
  3227. * <span id="test" class="test1 test2 test3">xxx</span>
  3228. *
  3229. * <script>
  3230. *
  3231. * var testNode = document.getElementById( "test" );
  3232. * UE.dom.domUtils.removeClasses( testNode, ["test1", "test2"] );
  3233. *
  3234. * //output: test3
  3235. * console.log( testNode.className );
  3236. *
  3237. * </script>
  3238. * ```
  3239. */
  3240. removeClasses:function (elm, classNames) {
  3241. classNames = utils.isArray(classNames) ? classNames :
  3242. utils.trim(classNames).replace(/[ ]{2,}/g,' ').split(' ');
  3243. for(var i = 0,ci,cls = elm.className;ci=classNames[i++];){
  3244. cls = cls.replace(new RegExp('\\b' + ci + '\\b'),'')
  3245. }
  3246. cls = utils.trim(cls).replace(/[ ]{2,}/g,' ');
  3247. if(cls){
  3248. elm.className = cls;
  3249. }else{
  3250. domUtils.removeAttributes(elm,['class']);
  3251. }
  3252. },
  3253. /**
  3254. * 给元素element添加className
  3255. * @method addClass
  3256. * @param { Node } ele 需要增加className的元素
  3257. * @param { String } classNames 需要添加的className, 多个className之间以空格分割
  3258. * @remind 相同的类名不会被重复添加
  3259. * @example
  3260. * ```html
  3261. * <span id="test" class="cls1 cls2"></span>
  3262. *
  3263. * <script>
  3264. * var testNode = document.getElementById("test");
  3265. *
  3266. * UE.dom.domUtils.addClass( testNode, "cls2 cls3 cls4" );
  3267. *
  3268. * //output: cl1 cls2 cls3 cls4
  3269. * console.log( testNode.className );
  3270. *
  3271. * <script>
  3272. * ```
  3273. */
  3274. /**
  3275. * 给元素element添加className
  3276. * @method addClass
  3277. * @param { Node } ele 需要增加className的元素
  3278. * @param { Array } classNames 需要添加的className的数组
  3279. * @remind 相同的类名不会被重复添加
  3280. * @example
  3281. * ```html
  3282. * <span id="test" class="cls1 cls2"></span>
  3283. *
  3284. * <script>
  3285. * var testNode = document.getElementById("test");
  3286. *
  3287. * UE.dom.domUtils.addClass( testNode, ["cls2", "cls3", "cls4"] );
  3288. *
  3289. * //output: cl1 cls2 cls3 cls4
  3290. * console.log( testNode.className );
  3291. *
  3292. * <script>
  3293. * ```
  3294. */
  3295. addClass:function (elm, classNames) {
  3296. if(!elm)return;
  3297. classNames = utils.trim(classNames).replace(/[ ]{2,}/g,' ').split(' ');
  3298. for(var i = 0,ci,cls = elm.className;ci=classNames[i++];){
  3299. if(!new RegExp('\\b' + ci + '\\b').test(cls)){
  3300. cls += ' ' + ci;
  3301. }
  3302. }
  3303. elm.className = utils.trim(cls);
  3304. },
  3305. /**
  3306. * 判断元素element是否包含给定的样式类名className
  3307. * @method hasClass
  3308. * @param { Node } ele 需要检测的元素
  3309. * @param { String } classNames 需要检测的className, 多个className之间用空格分割
  3310. * @return { Boolean } 元素是否包含所有给定的className
  3311. * @example
  3312. * ```html
  3313. * <span id="test1" class="cls1 cls2"></span>
  3314. *
  3315. * <script>
  3316. * var test1 = document.getElementById("test1");
  3317. *
  3318. * //output: false
  3319. * console.log( UE.dom.domUtils.hasClass( test1, "cls2 cls1 cls3" ) );
  3320. *
  3321. * //output: true
  3322. * console.log( UE.dom.domUtils.hasClass( test1, "cls2 cls1" ) );
  3323. * </script>
  3324. * ```
  3325. */
  3326. /**
  3327. * 判断元素element是否包含给定的样式类名className
  3328. * @method hasClass
  3329. * @param { Node } ele 需要检测的元素
  3330. * @param { Array } classNames 需要检测的className数组
  3331. * @return { Boolean } 元素是否包含所有给定的className
  3332. * @example
  3333. * ```html
  3334. * <span id="test1" class="cls1 cls2"></span>
  3335. *
  3336. * <script>
  3337. * var test1 = document.getElementById("test1");
  3338. *
  3339. * //output: false
  3340. * console.log( UE.dom.domUtils.hasClass( test1, [ "cls2", "cls1", "cls3" ] ) );
  3341. *
  3342. * //output: true
  3343. * console.log( UE.dom.domUtils.hasClass( test1, [ "cls2", "cls1" ]) );
  3344. * </script>
  3345. * ```
  3346. */
  3347. hasClass:function (element, className) {
  3348. if(utils.isRegExp(className)){
  3349. return className.test(element.className)
  3350. }
  3351. className = utils.trim(className).replace(/[ ]{2,}/g,' ').split(' ');
  3352. for(var i = 0,ci,cls = element.className;ci=className[i++];){
  3353. if(!new RegExp('\\b' + ci + '\\b','i').test(cls)){
  3354. return false;
  3355. }
  3356. }
  3357. return i - 1 == className.length;
  3358. },
  3359. /**
  3360. * 阻止事件默认行为
  3361. * @method preventDefault
  3362. * @param { Event } evt 需要阻止默认行为的事件对象
  3363. * @example
  3364. * ```javascript
  3365. * UE.dom.domUtils.preventDefault( evt );
  3366. * ```
  3367. */
  3368. preventDefault:function (evt) {
  3369. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
  3370. },
  3371. /**
  3372. * 删除元素element指定的样式
  3373. * @method removeStyle
  3374. * @param { Element } element 需要删除样式的元素
  3375. * @param { String } styleName 需要删除的样式名
  3376. * @example
  3377. * ```html
  3378. * <span id="test" style="color: red; background: blue;"></span>
  3379. *
  3380. * <script>
  3381. *
  3382. * var testNode = document.getElementById("test");
  3383. *
  3384. * UE.dom.domUtils.removeStyle( testNode, 'color' );
  3385. *
  3386. * //output: background: blue;
  3387. * console.log( testNode.style.cssText );
  3388. *
  3389. * </script>
  3390. * ```
  3391. */
  3392. removeStyle:function (element, name) {
  3393. if(browser.ie ){
  3394. //针对color先单独处理一下
  3395. if(name == 'color'){
  3396. name = '(^|;)' + name;
  3397. }
  3398. element.style.cssText = element.style.cssText.replace(new RegExp(name + '[^:]*:[^;]+;?','ig'),'')
  3399. }else{
  3400. if (element.style.removeProperty) {
  3401. element.style.removeProperty (name);
  3402. }else {
  3403. element.style.removeAttribute (utils.cssStyleToDomStyle(name));
  3404. }
  3405. }
  3406. if (!element.style.cssText) {
  3407. domUtils.removeAttributes(element, ['style']);
  3408. }
  3409. },
  3410. /**
  3411. * 获取元素element的style属性的指定值
  3412. * @method getStyle
  3413. * @param { Element } element 需要获取属性值的元素
  3414. * @param { String } styleName 需要获取的style的名称
  3415. * @warning 该方法仅获取元素style属性中所标明的值
  3416. * @return { String } 该元素包含指定的style属性值
  3417. * @example
  3418. * ```html
  3419. * <div id="test" style="color: red;"></div>
  3420. *
  3421. * <script>
  3422. *
  3423. * var testNode = document.getElementById( "test" );
  3424. *
  3425. * //output: red
  3426. * console.log( UE.dom.domUtils.getStyle( testNode, "color" ) );
  3427. *
  3428. * //output: ""
  3429. * console.log( UE.dom.domUtils.getStyle( testNode, "background" ) );
  3430. *
  3431. * </script>
  3432. * ```
  3433. */
  3434. getStyle:function (element, name) {
  3435. var value = element.style[ utils.cssStyleToDomStyle(name) ];
  3436. return utils.fixColor(name, value);
  3437. },
  3438. /**
  3439. * 为元素element设置样式属性值
  3440. * @method setStyle
  3441. * @param { Element } element 需要设置样式的元素
  3442. * @param { String } styleName 样式名
  3443. * @param { String } styleValue 样式值
  3444. * @example
  3445. * ```html
  3446. * <div id="test"></div>
  3447. *
  3448. * <script>
  3449. *
  3450. * var testNode = document.getElementById( "test" );
  3451. *
  3452. * //output: ""
  3453. * console.log( testNode.style.color );
  3454. *
  3455. * UE.dom.domUtils.setStyle( testNode, 'color', 'red' );
  3456. * //output: "red"
  3457. * console.log( testNode.style.color );
  3458. *
  3459. * </script>
  3460. * ```
  3461. */
  3462. setStyle:function (element, name, value) {
  3463. element.style[utils.cssStyleToDomStyle(name)] = value;
  3464. if(!utils.trim(element.style.cssText)){
  3465. this.removeAttributes(element,'style')
  3466. }
  3467. },
  3468. /**
  3469. * 为元素element设置多个样式属性值
  3470. * @method setStyles
  3471. * @param { Element } element 需要设置样式的元素
  3472. * @param { Object } styles 样式名值对
  3473. * @example
  3474. * ```html
  3475. * <div id="test"></div>
  3476. *
  3477. * <script>
  3478. *
  3479. * var testNode = document.getElementById( "test" );
  3480. *
  3481. * //output: ""
  3482. * console.log( testNode.style.color );
  3483. *
  3484. * UE.dom.domUtils.setStyles( testNode, {
  3485. * 'color': 'red'
  3486. * } );
  3487. * //output: "red"
  3488. * console.log( testNode.style.color );
  3489. *
  3490. * </script>
  3491. * ```
  3492. */
  3493. setStyles:function (element, styles) {
  3494. for (var name in styles) {
  3495. if (styles.hasOwnProperty(name)) {
  3496. domUtils.setStyle(element, name, styles[name]);
  3497. }
  3498. }
  3499. },
  3500. /**
  3501. * 删除_moz_dirty属性
  3502. * @private
  3503. * @method removeDirtyAttr
  3504. */
  3505. removeDirtyAttr:function (node) {
  3506. for (var i = 0, ci, nodes = node.getElementsByTagName('*'); ci = nodes[i++];) {
  3507. ci.removeAttribute('_moz_dirty');
  3508. }
  3509. node.removeAttribute('_moz_dirty');
  3510. },
  3511. /**
  3512. * 获取子节点的数量
  3513. * @method getChildCount
  3514. * @param { Element } node 需要检测的元素
  3515. * @return { Number } 给定的node元素的子节点数量
  3516. * @example
  3517. * ```html
  3518. * <div id="test">
  3519. * <span></span>
  3520. * </div>
  3521. *
  3522. * <script>
  3523. *
  3524. * //output: 3
  3525. * console.log( UE.dom.domUtils.getChildCount( document.getElementById("test") ) );
  3526. *
  3527. * </script>
  3528. * ```
  3529. */
  3530. /**
  3531. * 根据给定的过滤规则, 获取符合条件的子节点的数量
  3532. * @method getChildCount
  3533. * @param { Element } node 需要检测的元素
  3534. * @param { Function } fn 过滤器, 要求对符合条件的子节点返回true, 反之则要求返回false
  3535. * @return { Number } 符合过滤条件的node元素的子节点数量
  3536. * @example
  3537. * ```html
  3538. * <div id="test">
  3539. * <span></span>
  3540. * </div>
  3541. *
  3542. * <script>
  3543. *
  3544. * //output: 1
  3545. * console.log( UE.dom.domUtils.getChildCount( document.getElementById("test"), function ( node ) {
  3546. *
  3547. * return node.nodeType === 1;
  3548. *
  3549. * } ) );
  3550. *
  3551. * </script>
  3552. * ```
  3553. */
  3554. getChildCount:function (node, fn) {
  3555. var count = 0, first = node.firstChild;
  3556. fn = fn || function () {
  3557. return 1;
  3558. };
  3559. while (first) {
  3560. if (fn(first)) {
  3561. count++;
  3562. }
  3563. first = first.nextSibling;
  3564. }
  3565. return count;
  3566. },
  3567. /**
  3568. * 判断给定节点是否为空节点
  3569. * @method isEmptyNode
  3570. * @param { Node } node 需要检测的节点对象
  3571. * @return { Boolean } 节点是否为空
  3572. * @example
  3573. * ```javascript
  3574. * UE.dom.domUtils.isEmptyNode( document.body );
  3575. * ```
  3576. */
  3577. isEmptyNode:function (node) {
  3578. return !node.firstChild || domUtils.getChildCount(node, function (node) {
  3579. return !domUtils.isBr(node) && !domUtils.isBookmarkNode(node) && !domUtils.isWhitespace(node)
  3580. }) == 0
  3581. },
  3582. clearSelectedArr:function (nodes) {
  3583. var node;
  3584. while (node = nodes.pop()) {
  3585. domUtils.removeAttributes(node, ['class']);
  3586. }
  3587. },
  3588. /**
  3589. * 将显示区域滚动到指定节点的位置
  3590. * @method scrollToView
  3591. * @param {Node} node 节点
  3592. * @param {window} win window对象
  3593. * @param {Number} offsetTop 距离上方的偏移量
  3594. */
  3595. scrollToView:function (node, win, offsetTop) {
  3596. var getViewPaneSize = function () {
  3597. var doc = win.document,
  3598. mode = doc.compatMode == 'CSS1Compat';
  3599. return {
  3600. width:( mode ? doc.documentElement.clientWidth : doc.body.clientWidth ) || 0,
  3601. height:( mode ? doc.documentElement.clientHeight : doc.body.clientHeight ) || 0
  3602. };
  3603. },
  3604. getScrollPosition = function (win) {
  3605. if ('pageXOffset' in win) {
  3606. return {
  3607. x:win.pageXOffset || 0,
  3608. y:win.pageYOffset || 0
  3609. };
  3610. }
  3611. else {
  3612. var doc = win.document;
  3613. return {
  3614. x:doc.documentElement.scrollLeft || doc.body.scrollLeft || 0,
  3615. y:doc.documentElement.scrollTop || doc.body.scrollTop || 0
  3616. };
  3617. }
  3618. };
  3619. var winHeight = getViewPaneSize().height, offset = winHeight * -1 + offsetTop;
  3620. offset += (node.offsetHeight || 0);
  3621. var elementPosition = domUtils.getXY(node);
  3622. offset += elementPosition.y;
  3623. var currentScroll = getScrollPosition(win).y;
  3624. // offset += 50;
  3625. if (offset > currentScroll || offset < currentScroll - winHeight) {
  3626. win.scrollTo(0, offset + (offset < 0 ? -20 : 20));
  3627. }
  3628. },
  3629. /**
  3630. * 判断给定节点是否为br
  3631. * @method isBr
  3632. * @param { Node } node 需要判断的节点对象
  3633. * @return { Boolean } 给定的节点是否是br节点
  3634. */
  3635. isBr:function (node) {
  3636. return node.nodeType == 1 && node.tagName == 'BR';
  3637. },
  3638. /**
  3639. * 判断给定的节点是否是一个“填充”节点
  3640. * @private
  3641. * @method isFillChar
  3642. * @param { Node } node 需要判断的节点
  3643. * @param { Boolean } isInStart 是否从节点内容的开始位置匹配
  3644. * @returns { Boolean } 节点是否是填充节点
  3645. */
  3646. isFillChar:function (node,isInStart) {
  3647. if(node.nodeType != 3)
  3648. return false;
  3649. var text = node.nodeValue;
  3650. if(isInStart){
  3651. return new RegExp('^' + domUtils.fillChar).test(text)
  3652. }
  3653. return !text.replace(new RegExp(domUtils.fillChar,'g'), '').length
  3654. },
  3655. isStartInblock:function (range) {
  3656. var tmpRange = range.cloneRange(),
  3657. flag = 0,
  3658. start = tmpRange.startContainer,
  3659. tmp;
  3660. if(start.nodeType == 1 && start.childNodes[tmpRange.startOffset]){
  3661. start = start.childNodes[tmpRange.startOffset];
  3662. var pre = start.previousSibling;
  3663. while(pre && domUtils.isFillChar(pre)){
  3664. start = pre;
  3665. pre = pre.previousSibling;
  3666. }
  3667. }
  3668. if(this.isFillChar(start,true) && tmpRange.startOffset == 1){
  3669. tmpRange.setStartBefore(start);
  3670. start = tmpRange.startContainer;
  3671. }
  3672. while (start && domUtils.isFillChar(start)) {
  3673. tmp = start;
  3674. start = start.previousSibling
  3675. }
  3676. if (tmp) {
  3677. tmpRange.setStartBefore(tmp);
  3678. start = tmpRange.startContainer;
  3679. }
  3680. if (start.nodeType == 1 && domUtils.isEmptyNode(start) && tmpRange.startOffset == 1) {
  3681. tmpRange.setStart(start, 0).collapse(true);
  3682. }
  3683. while (!tmpRange.startOffset) {
  3684. start = tmpRange.startContainer;
  3685. if (domUtils.isBlockElm(start) || domUtils.isBody(start)) {
  3686. flag = 1;
  3687. break;
  3688. }
  3689. var pre = tmpRange.startContainer.previousSibling,
  3690. tmpNode;
  3691. if (!pre) {
  3692. tmpRange.setStartBefore(tmpRange.startContainer);
  3693. } else {
  3694. while (pre && domUtils.isFillChar(pre)) {
  3695. tmpNode = pre;
  3696. pre = pre.previousSibling;
  3697. }
  3698. if (tmpNode) {
  3699. tmpRange.setStartBefore(tmpNode);
  3700. } else {
  3701. tmpRange.setStartBefore(tmpRange.startContainer);
  3702. }
  3703. }
  3704. }
  3705. return flag && !domUtils.isBody(tmpRange.startContainer) ? 1 : 0;
  3706. },
  3707. /**
  3708. * 判断给定的元素是否是一个空元素
  3709. * @method isEmptyBlock
  3710. * @param { Element } node 需要判断的元素
  3711. * @return { Boolean } 是否是空元素
  3712. * @example
  3713. * ```html
  3714. * <div id="test"></div>
  3715. *
  3716. * <script>
  3717. * //output: true
  3718. * console.log( UE.dom.domUtils.isEmptyBlock( document.getElementById("test") ) );
  3719. * </script>
  3720. * ```
  3721. */
  3722. /**
  3723. * 根据指定的判断规则判断给定的元素是否是一个空元素
  3724. * @method isEmptyBlock
  3725. * @param { Element } node 需要判断的元素
  3726. * @param { RegExp } reg 对内容执行判断的正则表达式对象
  3727. * @return { Boolean } 是否是空元素
  3728. */
  3729. isEmptyBlock:function (node,reg) {
  3730. if(node.nodeType != 1)
  3731. return 0;
  3732. reg = reg || new RegExp('[ \xa0\t\r\n' + domUtils.fillChar + ']', 'g');
  3733. if (node[browser.ie ? 'innerText' : 'textContent'].replace(reg, '').length > 0) {
  3734. return 0;
  3735. }
  3736. for (var n in dtd.$isNotEmpty) {
  3737. if (node.getElementsByTagName(n).length) {
  3738. return 0;
  3739. }
  3740. }
  3741. return 1;
  3742. },
  3743. /**
  3744. * 移动元素使得该元素的位置移动指定的偏移量的距离
  3745. * @method setViewportOffset
  3746. * @param { Element } element 需要设置偏移量的元素
  3747. * @param { Object } offset 偏移量, 形如{ left: 100, top: 50 }的一个键值对, 表示该元素将在
  3748. * 现有的位置上向水平方向偏移offset.left的距离, 在竖直方向上偏移
  3749. * offset.top的距离
  3750. * @example
  3751. * ```html
  3752. * <div id="test" style="top: 100px; left: 50px; position: absolute;"></div>
  3753. *
  3754. * <script>
  3755. *
  3756. * var testNode = document.getElementById("test");
  3757. *
  3758. * UE.dom.domUtils.setViewportOffset( testNode, {
  3759. * left: 200,
  3760. * top: 50
  3761. * } );
  3762. *
  3763. * //output: top: 300px; left: 100px; position: absolute;
  3764. * console.log( testNode.style.cssText );
  3765. *
  3766. * </script>
  3767. * ```
  3768. */
  3769. setViewportOffset:function (element, offset) {
  3770. var left = parseInt(element.style.left) | 0;
  3771. var top = parseInt(element.style.top) | 0;
  3772. var rect = element.getBoundingClientRect();
  3773. var offsetLeft = offset.left - rect.left;
  3774. var offsetTop = offset.top - rect.top;
  3775. if (offsetLeft) {
  3776. element.style.left = left + offsetLeft + 'px';
  3777. }
  3778. if (offsetTop) {
  3779. element.style.top = top + offsetTop + 'px';
  3780. }
  3781. },
  3782. /**
  3783. * 用“填充字符”填充节点
  3784. * @method fillNode
  3785. * @private
  3786. * @param { DomDocument } doc 填充的节点所在的docment对象
  3787. * @param { Node } node 需要填充的节点对象
  3788. * @example
  3789. * ```html
  3790. * <div id="test"></div>
  3791. *
  3792. * <script>
  3793. * var testNode = document.getElementById("test");
  3794. *
  3795. * //output: 0
  3796. * console.log( testNode.childNodes.length );
  3797. *
  3798. * UE.dom.domUtils.fillNode( document, testNode );
  3799. *
  3800. * //output: 1
  3801. * console.log( testNode.childNodes.length );
  3802. *
  3803. * </script>
  3804. * ```
  3805. */
  3806. fillNode:function (doc, node) {
  3807. var tmpNode = browser.ie ? doc.createTextNode(domUtils.fillChar) : doc.createElement('br');
  3808. node.innerHTML = '';
  3809. node.appendChild(tmpNode);
  3810. },
  3811. /**
  3812. * 把节点src的所有子节点追加到另一个节点tag上去
  3813. * @method moveChild
  3814. * @param { Node } src 源节点, 该节点下的所有子节点将被移除
  3815. * @param { Node } tag 目标节点, 从源节点移除的子节点将被追加到该节点下
  3816. * @example
  3817. * ```html
  3818. * <div id="test1">
  3819. * <span></span>
  3820. * </div>
  3821. * <div id="test2">
  3822. * <div></div>
  3823. * </div>
  3824. *
  3825. * <script>
  3826. *
  3827. * var test1 = document.getElementById("test1"),
  3828. * test2 = document.getElementById("test2");
  3829. *
  3830. * UE.dom.domUtils.moveChild( test1, test2 );
  3831. *
  3832. * //output: ""(空字符串)
  3833. * console.log( test1.innerHTML );
  3834. *
  3835. * //output: "<div></div><span></span>"
  3836. * console.log( test2.innerHTML );
  3837. *
  3838. * </script>
  3839. * ```
  3840. */
  3841. /**
  3842. * 把节点src的所有子节点移动到另一个节点tag上去, 可以通过dir参数控制附加的行为是“追加”还是“插入顶部”
  3843. * @method moveChild
  3844. * @param { Node } src 源节点, 该节点下的所有子节点将被移除
  3845. * @param { Node } tag 目标节点, 从源节点移除的子节点将被附加到该节点下
  3846. * @param { Boolean } dir 附加方式, 如果为true, 则附加进去的节点将被放到目标节点的顶部, 反之,则放到末尾
  3847. * @example
  3848. * ```html
  3849. * <div id="test1">
  3850. * <span></span>
  3851. * </div>
  3852. * <div id="test2">
  3853. * <div></div>
  3854. * </div>
  3855. *
  3856. * <script>
  3857. *
  3858. * var test1 = document.getElementById("test1"),
  3859. * test2 = document.getElementById("test2");
  3860. *
  3861. * UE.dom.domUtils.moveChild( test1, test2, true );
  3862. *
  3863. * //output: ""(空字符串)
  3864. * console.log( test1.innerHTML );
  3865. *
  3866. * //output: "<span></span><div></div>"
  3867. * console.log( test2.innerHTML );
  3868. *
  3869. * </script>
  3870. * ```
  3871. */
  3872. moveChild:function (src, tag, dir) {
  3873. while (src.firstChild) {
  3874. if (dir && tag.firstChild) {
  3875. tag.insertBefore(src.lastChild, tag.firstChild);
  3876. } else {
  3877. tag.appendChild(src.firstChild);
  3878. }
  3879. }
  3880. },
  3881. /**
  3882. * 判断节点的标签上是否不存在任何属性
  3883. * @method hasNoAttributes
  3884. * @private
  3885. * @param { Node } node 需要检测的节点对象
  3886. * @return { Boolean } 节点是否不包含任何属性
  3887. * @example
  3888. * ```html
  3889. * <div id="test"><span>xxxx</span></div>
  3890. *
  3891. * <script>
  3892. *
  3893. * //output: false
  3894. * console.log( UE.dom.domUtils.hasNoAttributes( document.getElementById("test") ) );
  3895. *
  3896. * //output: true
  3897. * console.log( UE.dom.domUtils.hasNoAttributes( document.getElementById("test").firstChild ) );
  3898. *
  3899. * </script>
  3900. * ```
  3901. */
  3902. hasNoAttributes:function (node) {
  3903. return browser.ie ? /^<\w+\s*?>/.test(node.outerHTML) : node.attributes.length == 0;
  3904. },
  3905. /**
  3906. * 检测节点是否是UEditor所使用的辅助节点
  3907. * @method isCustomeNode
  3908. * @private
  3909. * @param { Node } node 需要检测的节点
  3910. * @remind 辅助节点是指编辑器要完成工作临时添加的节点, 在输出的时候将会从编辑器内移除, 不会影响最终的结果。
  3911. * @return { Boolean } 给定的节点是否是一个辅助节点
  3912. */
  3913. isCustomeNode:function (node) {
  3914. return node.nodeType == 1 && node.getAttribute('_ue_custom_node_');
  3915. },
  3916. /**
  3917. * 检测节点的标签是否是给定的标签
  3918. * @method isTagNode
  3919. * @param { Node } node 需要检测的节点对象
  3920. * @param { String } tagName 标签
  3921. * @return { Boolean } 节点的标签是否是给定的标签
  3922. * @example
  3923. * ```html
  3924. * <div id="test"></div>
  3925. *
  3926. * <script>
  3927. *
  3928. * //output: true
  3929. * console.log( UE.dom.domUtils.isTagNode( document.getElementById("test"), "div" ) );
  3930. *
  3931. * </script>
  3932. * ```
  3933. */
  3934. isTagNode:function (node, tagNames) {
  3935. return node.nodeType == 1 && new RegExp('\\b' + node.tagName + '\\b','i').test(tagNames)
  3936. },
  3937. /**
  3938. * 给定一个节点数组,在通过指定的过滤器过滤后, 获取其中满足过滤条件的第一个节点
  3939. * @method filterNodeList
  3940. * @param { Array } nodeList 需要过滤的节点数组
  3941. * @param { Function } fn 过滤器, 对符合条件的节点, 执行结果返回true, 反之则返回false
  3942. * @return { Node | NULL } 如果找到符合过滤条件的节点, 则返回该节点, 否则返回NULL
  3943. * @example
  3944. * ```javascript
  3945. * var divNodes = document.getElementsByTagName("div");
  3946. * divNodes = [].slice.call( divNodes, 0 );
  3947. *
  3948. * //output: null
  3949. * console.log( UE.dom.domUtils.filterNodeList( divNodes, function ( node ) {
  3950. * return node.tagName.toLowerCase() !== 'div';
  3951. * } ) );
  3952. * ```
  3953. */
  3954. /**
  3955. * 给定一个节点数组nodeList和一组标签名tagNames, 获取其中能够匹配标签名的节点集合中的第一个节点
  3956. * @method filterNodeList
  3957. * @param { Array } nodeList 需要过滤的节点数组
  3958. * @param { String } tagNames 需要匹配的标签名, 多个标签名之间用空格分割
  3959. * @return { Node | NULL } 如果找到标签名匹配的节点, 则返回该节点, 否则返回NULL
  3960. * @example
  3961. * ```javascript
  3962. * var divNodes = document.getElementsByTagName("div");
  3963. * divNodes = [].slice.call( divNodes, 0 );
  3964. *
  3965. * //output: null
  3966. * console.log( UE.dom.domUtils.filterNodeList( divNodes, 'a span' ) );
  3967. * ```
  3968. */
  3969. /**
  3970. * 给定一个节点数组,在通过指定的过滤器过滤后, 如果参数forAll为true, 则会返回所有满足过滤
  3971. * 条件的节点集合, 否则, 返回满足条件的节点集合中的第一个节点
  3972. * @method filterNodeList
  3973. * @param { Array } nodeList 需要过滤的节点数组
  3974. * @param { Function } fn 过滤器, 对符合条件的节点, 执行结果返回true, 反之则返回false
  3975. * @param { Boolean } forAll 是否返回整个节点数组, 如果该参数为false, 则返回节点集合中的第一个节点
  3976. * @return { Array | Node | NULL } 如果找到符合过滤条件的节点, 则根据参数forAll的值决定返回满足
  3977. * 过滤条件的节点数组或第一个节点, 否则返回NULL
  3978. * @example
  3979. * ```javascript
  3980. * var divNodes = document.getElementsByTagName("div");
  3981. * divNodes = [].slice.call( divNodes, 0 );
  3982. *
  3983. * //output: 3(假定有3个div)
  3984. * console.log( divNodes.length );
  3985. *
  3986. * var nodes = UE.dom.domUtils.filterNodeList( divNodes, function ( node ) {
  3987. * return node.tagName.toLowerCase() === 'div';
  3988. * }, true );
  3989. *
  3990. * //output: 3
  3991. * console.log( nodes.length );
  3992. *
  3993. * var node = UE.dom.domUtils.filterNodeList( divNodes, function ( node ) {
  3994. * return node.tagName.toLowerCase() === 'div';
  3995. * }, false );
  3996. *
  3997. * //output: div
  3998. * console.log( node.nodeName );
  3999. * ```
  4000. */
  4001. filterNodeList : function(nodelist,filter,forAll){
  4002. var results = [];
  4003. if(!utils .isFunction(filter)){
  4004. var str = filter;
  4005. filter = function(n){
  4006. return utils.indexOf(utils.isArray(str) ? str:str.split(' '), n.tagName.toLowerCase()) != -1
  4007. };
  4008. }
  4009. utils.each(nodelist,function(n){
  4010. filter(n) && results.push(n)
  4011. });
  4012. return results.length == 0 ? null : results.length == 1 || !forAll ? results[0] : results
  4013. },
  4014. /**
  4015. * 查询给定的range选区是否在给定的node节点内,且在该节点的最末尾
  4016. * @method isInNodeEndBoundary
  4017. * @param { UE.dom.Range } rng 需要判断的range对象, 该对象的startContainer不能为NULL
  4018. * @param node 需要检测的节点对象
  4019. * @return { Number } 如果给定的选取range对象是在node内部的最末端, 则返回1, 否则返回0
  4020. */
  4021. isInNodeEndBoundary : function (rng,node){
  4022. var start = rng.startContainer;
  4023. if(start.nodeType == 3 && rng.startOffset != start.nodeValue.length){
  4024. return 0;
  4025. }
  4026. if(start.nodeType == 1 && rng.startOffset != start.childNodes.length){
  4027. return 0;
  4028. }
  4029. while(start !== node){
  4030. if(start.nextSibling){
  4031. return 0
  4032. };
  4033. start = start.parentNode;
  4034. }
  4035. return 1;
  4036. },
  4037. isBoundaryNode : function (node,dir){
  4038. var tmp;
  4039. while(!domUtils.isBody(node)){
  4040. tmp = node;
  4041. node = node.parentNode;
  4042. if(tmp !== node[dir]){
  4043. return false;
  4044. }
  4045. }
  4046. return true;
  4047. },
  4048. fillHtml : browser.ie11below ? '&nbsp;' : '<br/>'
  4049. };
  4050. var fillCharReg = new RegExp(domUtils.fillChar, 'g');
  4051. // core/Range.js
  4052. /**
  4053. * Range封装
  4054. * @file
  4055. * @module UE.dom
  4056. * @class Range
  4057. * @since 1.2.6.1
  4058. */
  4059. /**
  4060. * dom操作封装
  4061. * @unfile
  4062. * @module UE.dom
  4063. */
  4064. /**
  4065. * Range实现类,本类是UEditor底层核心类,封装不同浏览器之间的Range操作。
  4066. * @unfile
  4067. * @module UE.dom
  4068. * @class Range
  4069. */
  4070. (function () {
  4071. var guid = 0,
  4072. fillChar = domUtils.fillChar,
  4073. fillData;
  4074. /**
  4075. * 更新range的collapse状态
  4076. * @param {Range} range range对象
  4077. */
  4078. function updateCollapse(range) {
  4079. range.collapsed =
  4080. range.startContainer && range.endContainer &&
  4081. range.startContainer === range.endContainer &&
  4082. range.startOffset == range.endOffset;
  4083. }
  4084. function selectOneNode(rng){
  4085. return !rng.collapsed && rng.startContainer.nodeType == 1 && rng.startContainer === rng.endContainer && rng.endOffset - rng.startOffset == 1
  4086. }
  4087. function setEndPoint(toStart, node, offset, range) {
  4088. //如果node是自闭合标签要处理
  4089. if (node.nodeType == 1 && (dtd.$empty[node.tagName] || dtd.$nonChild[node.tagName])) {
  4090. offset = domUtils.getNodeIndex(node) + (toStart ? 0 : 1);
  4091. node = node.parentNode;
  4092. }
  4093. if (toStart) {
  4094. range.startContainer = node;
  4095. range.startOffset = offset;
  4096. if (!range.endContainer) {
  4097. range.collapse(true);
  4098. }
  4099. } else {
  4100. range.endContainer = node;
  4101. range.endOffset = offset;
  4102. if (!range.startContainer) {
  4103. range.collapse(false);
  4104. }
  4105. }
  4106. updateCollapse(range);
  4107. return range;
  4108. }
  4109. function execContentsAction(range, action) {
  4110. //调整边界
  4111. //range.includeBookmark();
  4112. var start = range.startContainer,
  4113. end = range.endContainer,
  4114. startOffset = range.startOffset,
  4115. endOffset = range.endOffset,
  4116. doc = range.document,
  4117. frag = doc.createDocumentFragment(),
  4118. tmpStart, tmpEnd;
  4119. if (start.nodeType == 1) {
  4120. start = start.childNodes[startOffset] || (tmpStart = start.appendChild(doc.createTextNode('')));
  4121. }
  4122. if (end.nodeType == 1) {
  4123. end = end.childNodes[endOffset] || (tmpEnd = end.appendChild(doc.createTextNode('')));
  4124. }
  4125. if (start === end && start.nodeType == 3) {
  4126. frag.appendChild(doc.createTextNode(start.substringData(startOffset, endOffset - startOffset)));
  4127. //is not clone
  4128. if (action) {
  4129. start.deleteData(startOffset, endOffset - startOffset);
  4130. range.collapse(true);
  4131. }
  4132. return frag;
  4133. }
  4134. var current, currentLevel, clone = frag,
  4135. startParents = domUtils.findParents(start, true), endParents = domUtils.findParents(end, true);
  4136. for (var i = 0; startParents[i] == endParents[i];) {
  4137. i++;
  4138. }
  4139. for (var j = i, si; si = startParents[j]; j++) {
  4140. current = si.nextSibling;
  4141. if (si == start) {
  4142. if (!tmpStart) {
  4143. if (range.startContainer.nodeType == 3) {
  4144. clone.appendChild(doc.createTextNode(start.nodeValue.slice(startOffset)));
  4145. //is not clone
  4146. if (action) {
  4147. start.deleteData(startOffset, start.nodeValue.length - startOffset);
  4148. }
  4149. } else {
  4150. clone.appendChild(!action ? start.cloneNode(true) : start);
  4151. }
  4152. }
  4153. } else {
  4154. currentLevel = si.cloneNode(false);
  4155. clone.appendChild(currentLevel);
  4156. }
  4157. while (current) {
  4158. if (current === end || current === endParents[j]) {
  4159. break;
  4160. }
  4161. si = current.nextSibling;
  4162. clone.appendChild(!action ? current.cloneNode(true) : current);
  4163. current = si;
  4164. }
  4165. clone = currentLevel;
  4166. }
  4167. clone = frag;
  4168. if (!startParents[i]) {
  4169. clone.appendChild(startParents[i - 1].cloneNode(false));
  4170. clone = clone.firstChild;
  4171. }
  4172. for (var j = i, ei; ei = endParents[j]; j++) {
  4173. current = ei.previousSibling;
  4174. if (ei == end) {
  4175. if (!tmpEnd && range.endContainer.nodeType == 3) {
  4176. clone.appendChild(doc.createTextNode(end.substringData(0, endOffset)));
  4177. //is not clone
  4178. if (action) {
  4179. end.deleteData(0, endOffset);
  4180. }
  4181. }
  4182. } else {
  4183. currentLevel = ei.cloneNode(false);
  4184. clone.appendChild(currentLevel);
  4185. }
  4186. //如果两端同级,右边第一次已经被开始做了
  4187. if (j != i || !startParents[i]) {
  4188. while (current) {
  4189. if (current === start) {
  4190. break;
  4191. }
  4192. ei = current.previousSibling;
  4193. clone.insertBefore(!action ? current.cloneNode(true) : current, clone.firstChild);
  4194. current = ei;
  4195. }
  4196. }
  4197. clone = currentLevel;
  4198. }
  4199. if (action) {
  4200. range.setStartBefore(!endParents[i] ? endParents[i - 1] : !startParents[i] ? startParents[i - 1] : endParents[i]).collapse(true);
  4201. }
  4202. tmpStart && domUtils.remove(tmpStart);
  4203. tmpEnd && domUtils.remove(tmpEnd);
  4204. return frag;
  4205. }
  4206. /**
  4207. * 创建一个跟document绑定的空的Range实例
  4208. * @constructor
  4209. * @param { Document } document 新建的选区所属的文档对象
  4210. */
  4211. /**
  4212. * @property { Node } startContainer 当前Range的开始边界的容器节点, 可以是一个元素节点或者是文本节点
  4213. */
  4214. /**
  4215. * @property { Node } startOffset 当前Range的开始边界容器节点的偏移量, 如果是元素节点,
  4216. * 该值就是childNodes中的第几个节点, 如果是文本节点就是文本内容的第几个字符
  4217. */
  4218. /**
  4219. * @property { Node } endContainer 当前Range的结束边界的容器节点, 可以是一个元素节点或者是文本节点
  4220. */
  4221. /**
  4222. * @property { Node } endOffset 当前Range的结束边界容器节点的偏移量, 如果是元素节点,
  4223. * 该值就是childNodes中的第几个节点, 如果是文本节点就是文本内容的第几个字符
  4224. */
  4225. /**
  4226. * @property { Boolean } collapsed 当前Range是否闭合
  4227. * @default true
  4228. * @remind Range是闭合的时候, startContainer === endContainer && startOffset === endOffset
  4229. */
  4230. /**
  4231. * @property { Document } document 当前Range所属的Document对象
  4232. * @remind 不同range的的document属性可以是不同的
  4233. */
  4234. var Range = dom.Range = function (document) {
  4235. var me = this;
  4236. me.startContainer =
  4237. me.startOffset =
  4238. me.endContainer =
  4239. me.endOffset = null;
  4240. me.document = document;
  4241. me.collapsed = true;
  4242. };
  4243. /**
  4244. * 删除fillData
  4245. * @param doc
  4246. * @param excludeNode
  4247. */
  4248. function removeFillData(doc, excludeNode) {
  4249. try {
  4250. if (fillData && domUtils.inDoc(fillData, doc)) {
  4251. if (!fillData.nodeValue.replace(fillCharReg, '').length) {
  4252. var tmpNode = fillData.parentNode;
  4253. domUtils.remove(fillData);
  4254. while (tmpNode && domUtils.isEmptyInlineElement(tmpNode) &&
  4255. //safari的contains有bug
  4256. (browser.safari ? !(domUtils.getPosition(tmpNode,excludeNode) & domUtils.POSITION_CONTAINS) : !tmpNode.contains(excludeNode))
  4257. ) {
  4258. fillData = tmpNode.parentNode;
  4259. domUtils.remove(tmpNode);
  4260. tmpNode = fillData;
  4261. }
  4262. } else {
  4263. fillData.nodeValue = fillData.nodeValue.replace(fillCharReg, '');
  4264. }
  4265. }
  4266. } catch (e) {
  4267. }
  4268. }
  4269. /**
  4270. * @param node
  4271. * @param dir
  4272. */
  4273. function mergeSibling(node, dir) {
  4274. var tmpNode;
  4275. node = node[dir];
  4276. while (node && domUtils.isFillChar(node)) {
  4277. tmpNode = node[dir];
  4278. domUtils.remove(node);
  4279. node = tmpNode;
  4280. }
  4281. }
  4282. Range.prototype = {
  4283. /**
  4284. * 克隆选区的内容到一个DocumentFragment里
  4285. * @method cloneContents
  4286. * @return { DocumentFragment | NULL } 如果选区是闭合的将返回null, 否则, 返回包含所clone内容的DocumentFragment元素
  4287. * @example
  4288. * ```html
  4289. * <body>
  4290. * <!-- 中括号表示选区 -->
  4291. * <b>x<i>x[x</i>xx]x</b>
  4292. *
  4293. * <script>
  4294. * //range是已选中的选区
  4295. * var fragment = range.cloneContents(),
  4296. * node = document.createElement("div");
  4297. *
  4298. * node.appendChild( fragment );
  4299. *
  4300. * //output: <i>x</i>xx
  4301. * console.log( node.innerHTML );
  4302. *
  4303. * </script>
  4304. * </body>
  4305. * ```
  4306. */
  4307. cloneContents:function () {
  4308. return this.collapsed ? null : execContentsAction(this, 0);
  4309. },
  4310. /**
  4311. * 删除当前选区范围中的所有内容
  4312. * @method deleteContents
  4313. * @remind 执行完该操作后, 当前Range对象变成了闭合状态
  4314. * @return { UE.dom.Range } 当前操作的Range对象
  4315. * @example
  4316. * ```html
  4317. * <body>
  4318. * <!-- 中括号表示选区 -->
  4319. * <b>x<i>x[x</i>xx]x</b>
  4320. *
  4321. * <script>
  4322. * //range是已选中的选区
  4323. * range.deleteContents();
  4324. *
  4325. * //竖线表示闭合后的选区位置
  4326. * //output: <b>x<i>x</i>|x</b>
  4327. * console.log( document.body.innerHTML );
  4328. *
  4329. * //此时, range的各项属性为
  4330. * //output: B
  4331. * console.log( range.startContainer.tagName );
  4332. * //output: 2
  4333. * console.log( range.startOffset );
  4334. * //output: B
  4335. * console.log( range.endContainer.tagName );
  4336. * //output: 2
  4337. * console.log( range.endOffset );
  4338. * //output: true
  4339. * console.log( range.collapsed );
  4340. *
  4341. * </script>
  4342. * </body>
  4343. * ```
  4344. */
  4345. deleteContents:function () {
  4346. var txt;
  4347. if (!this.collapsed) {
  4348. execContentsAction(this, 1);
  4349. }
  4350. if (browser.webkit) {
  4351. txt = this.startContainer;
  4352. if (txt.nodeType == 3 && !txt.nodeValue.length) {
  4353. this.setStartBefore(txt).collapse(true);
  4354. domUtils.remove(txt);
  4355. }
  4356. }
  4357. return this;
  4358. },
  4359. /**
  4360. * 将当前选区的内容提取到一个DocumentFragment里
  4361. * @method extractContents
  4362. * @remind 执行该操作后, 选区将变成闭合状态
  4363. * @warning 执行该操作后, 原来选区所选中的内容将从dom树上剥离出来
  4364. * @return { DocumentFragment } 返回包含所提取内容的DocumentFragment对象
  4365. * @example
  4366. * ```html
  4367. * <body>
  4368. * <!-- 中括号表示选区 -->
  4369. * <b>x<i>x[x</i>xx]x</b>
  4370. *
  4371. * <script>
  4372. * //range是已选中的选区
  4373. * var fragment = range.extractContents(),
  4374. * node = document.createElement( "div" );
  4375. *
  4376. * node.appendChild( fragment );
  4377. *
  4378. * //竖线表示闭合后的选区位置
  4379. *
  4380. * //output: <b>x<i>x</i>|x</b>
  4381. * console.log( document.body.innerHTML );
  4382. * //output: <i>x</i>xx
  4383. * console.log( node.innerHTML );
  4384. *
  4385. * //此时, range的各项属性为
  4386. * //output: B
  4387. * console.log( range.startContainer.tagName );
  4388. * //output: 2
  4389. * console.log( range.startOffset );
  4390. * //output: B
  4391. * console.log( range.endContainer.tagName );
  4392. * //output: 2
  4393. * console.log( range.endOffset );
  4394. * //output: true
  4395. * console.log( range.collapsed );
  4396. *
  4397. * </script>
  4398. * </body>
  4399. */
  4400. extractContents:function () {
  4401. return this.collapsed ? null : execContentsAction(this, 2);
  4402. },
  4403. /**
  4404. * 设置Range的开始容器节点和偏移量
  4405. * @method setStart
  4406. * @remind 如果给定的节点是元素节点,那么offset指的是其子元素中索引为offset的元素,
  4407. * 如果是文本节点,那么offset指的是其文本内容的第offset个字符
  4408. * @remind 如果提供的容器节点是一个不能包含子元素的节点, 则该选区的开始容器将被设置
  4409. * 为该节点的父节点, 此时, 其距离开始容器的偏移量也变成了该节点在其父节点
  4410. * 中的索引
  4411. * @param { Node } node 将被设为当前选区开始边界容器的节点对象
  4412. * @param { int } offset 选区的开始位置偏移量
  4413. * @return { UE.dom.Range } 当前range对象
  4414. * @example
  4415. * ```html
  4416. * <!-- 选区 -->
  4417. * <b>xxx<i>x<span>xx</span>xx<em>xx</em>xxx</i>[xxx]</b>
  4418. *
  4419. * <script>
  4420. *
  4421. * //执行操作
  4422. * range.setStart( document.getElementsByTagName("i")[0], 1 );
  4423. *
  4424. * //此时, 选区变成了
  4425. * //<b>xxx<i>x[<span>xx</span>xx<em>xx</em>xxx</i>xxx]</b>
  4426. *
  4427. * </script>
  4428. * ```
  4429. * @example
  4430. * ```html
  4431. * <!-- 选区 -->
  4432. * <b>xxx<img>[xx]x</b>
  4433. *
  4434. * <script>
  4435. *
  4436. * //执行操作
  4437. * range.setStart( document.getElementsByTagName("img")[0], 3 );
  4438. *
  4439. * //此时, 选区变成了
  4440. * //<b>xxx[<img>xx]x</b>
  4441. *
  4442. * </script>
  4443. * ```
  4444. */
  4445. setStart:function (node, offset) {
  4446. return setEndPoint(true, node, offset, this);
  4447. },
  4448. /**
  4449. * 设置Range的结束容器和偏移量
  4450. * @method setEnd
  4451. * @param { Node } node 作为当前选区结束边界容器的节点对象
  4452. * @param { int } offset 结束边界的偏移量
  4453. * @see UE.dom.Range:setStart(Node,int)
  4454. * @return { UE.dom.Range } 当前range对象
  4455. */
  4456. setEnd:function (node, offset) {
  4457. return setEndPoint(false, node, offset, this);
  4458. },
  4459. /**
  4460. * 将Range开始位置设置到node节点之后
  4461. * @method setStartAfter
  4462. * @remind 该操作将会把给定节点的父节点作为range的开始容器, 且偏移量是该节点在其父节点中的位置索引+1
  4463. * @param { Node } node 选区的开始边界将紧接着该节点之后
  4464. * @return { UE.dom.Range } 当前range对象
  4465. * @example
  4466. * ```html
  4467. * <!-- 选区示例 -->
  4468. * <b>xx<i>xxx</i><span>xx[x</span>xxx]</b>
  4469. *
  4470. * <script>
  4471. *
  4472. * //执行操作
  4473. * range.setStartAfter( document.getElementsByTagName("i")[0] );
  4474. *
  4475. * //结果选区
  4476. * //<b>xx<i>xxx</i>[<span>xxx</span>xxx]</b>
  4477. *
  4478. * </script>
  4479. * ```
  4480. */
  4481. setStartAfter:function (node) {
  4482. return this.setStart(node.parentNode, domUtils.getNodeIndex(node) + 1);
  4483. },
  4484. /**
  4485. * 将Range开始位置设置到node节点之前
  4486. * @method setStartBefore
  4487. * @remind 该操作将会把给定节点的父节点作为range的开始容器, 且偏移量是该节点在其父节点中的位置索引
  4488. * @param { Node } node 新的选区开始位置在该节点之前
  4489. * @see UE.dom.Range:setStartAfter(Node)
  4490. * @return { UE.dom.Range } 当前range对象
  4491. */
  4492. setStartBefore:function (node) {
  4493. return this.setStart(node.parentNode, domUtils.getNodeIndex(node));
  4494. },
  4495. /**
  4496. * 将Range结束位置设置到node节点之后
  4497. * @method setEndAfter
  4498. * @remind 该操作将会把给定节点的父节点作为range的结束容器, 且偏移量是该节点在其父节点中的位置索引+1
  4499. * @param { Node } node 目标节点
  4500. * @see UE.dom.Range:setStartAfter(Node)
  4501. * @return { UE.dom.Range } 当前range对象
  4502. * @example
  4503. * ```html
  4504. * <!-- 选区示例 -->
  4505. * <b>[xx<i>xxx</i><span>xx]x</span>xxx</b>
  4506. *
  4507. * <script>
  4508. *
  4509. * //执行操作
  4510. * range.setStartAfter( document.getElementsByTagName("span")[0] );
  4511. *
  4512. * //结果选区
  4513. * //<b>[xx<i>xxx</i><span>xxx</span>]xxx</b>
  4514. *
  4515. * </script>
  4516. * ```
  4517. */
  4518. setEndAfter:function (node) {
  4519. return this.setEnd(node.parentNode, domUtils.getNodeIndex(node) + 1);
  4520. },
  4521. /**
  4522. * 将Range结束位置设置到node节点之前
  4523. * @method setEndBefore
  4524. * @remind 该操作将会把给定节点的父节点作为range的结束容器, 且偏移量是该节点在其父节点中的位置索引
  4525. * @param { Node } node 目标节点
  4526. * @see UE.dom.Range:setEndAfter(Node)
  4527. * @return { UE.dom.Range } 当前range对象
  4528. */
  4529. setEndBefore:function (node) {
  4530. return this.setEnd(node.parentNode, domUtils.getNodeIndex(node));
  4531. },
  4532. /**
  4533. * 设置Range的开始位置到node节点内的第一个子节点之前
  4534. * @method setStartAtFirst
  4535. * @remind 选区的开始容器将变成给定的节点, 且偏移量为0
  4536. * @remind 如果给定的节点是元素节点, 则该节点必须是允许包含子节点的元素。
  4537. * @param { Node } node 目标节点
  4538. * @see UE.dom.Range:setStartBefore(Node)
  4539. * @return { UE.dom.Range } 当前range对象
  4540. * @example
  4541. * ```html
  4542. * <!-- 选区示例 -->
  4543. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  4544. *
  4545. * <script>
  4546. *
  4547. * //执行操作
  4548. * range.setStartAtFirst( document.getElementsByTagName("i")[0] );
  4549. *
  4550. * //结果选区
  4551. * //<b>xx<i>[xxx</i><span>xx]x</span>xxx</b>
  4552. *
  4553. * </script>
  4554. * ```
  4555. */
  4556. setStartAtFirst:function (node) {
  4557. return this.setStart(node, 0);
  4558. },
  4559. /**
  4560. * 设置Range的开始位置到node节点内的最后一个节点之后
  4561. * @method setStartAtLast
  4562. * @remind 选区的开始容器将变成给定的节点, 且偏移量为该节点的子节点数
  4563. * @remind 如果给定的节点是元素节点, 则该节点必须是允许包含子节点的元素。
  4564. * @param { Node } node 目标节点
  4565. * @see UE.dom.Range:setStartAtFirst(Node)
  4566. * @return { UE.dom.Range } 当前range对象
  4567. */
  4568. setStartAtLast:function (node) {
  4569. return this.setStart(node, node.nodeType == 3 ? node.nodeValue.length : node.childNodes.length);
  4570. },
  4571. /**
  4572. * 设置Range的结束位置到node节点内的第一个节点之前
  4573. * @method setEndAtFirst
  4574. * @param { Node } node 目标节点
  4575. * @remind 选区的结束容器将变成给定的节点, 且偏移量为0
  4576. * @remind node必须是一个元素节点, 且必须是允许包含子节点的元素。
  4577. * @see UE.dom.Range:setStartAtFirst(Node)
  4578. * @return { UE.dom.Range } 当前range对象
  4579. */
  4580. setEndAtFirst:function (node) {
  4581. return this.setEnd(node, 0);
  4582. },
  4583. /**
  4584. * 设置Range的结束位置到node节点内的最后一个节点之后
  4585. * @method setEndAtLast
  4586. * @param { Node } node 目标节点
  4587. * @remind 选区的结束容器将变成给定的节点, 且偏移量为该节点的子节点数量
  4588. * @remind node必须是一个元素节点, 且必须是允许包含子节点的元素。
  4589. * @see UE.dom.Range:setStartAtFirst(Node)
  4590. * @return { UE.dom.Range } 当前range对象
  4591. */
  4592. setEndAtLast:function (node) {
  4593. return this.setEnd(node, node.nodeType == 3 ? node.nodeValue.length : node.childNodes.length);
  4594. },
  4595. /**
  4596. * 选中给定节点
  4597. * @method selectNode
  4598. * @remind 此时, 选区的开始容器和结束容器都是该节点的父节点, 其startOffset是该节点在父节点中的位置索引,
  4599. * 而endOffset为startOffset+1
  4600. * @param { Node } node 需要选中的节点
  4601. * @return { UE.dom.Range } 当前range对象,此时的range仅包含当前给定的节点对象
  4602. * @example
  4603. * ```html
  4604. * <!-- 选区示例 -->
  4605. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  4606. *
  4607. * <script>
  4608. *
  4609. * //执行操作
  4610. * range.selectNode( document.getElementsByTagName("i")[0] );
  4611. *
  4612. * //结果选区
  4613. * //<b>xx[<i>xxx</i>]<span>xxx</span>xxx</b>
  4614. *
  4615. * </script>
  4616. * ```
  4617. */
  4618. selectNode:function (node) {
  4619. return this.setStartBefore(node).setEndAfter(node);
  4620. },
  4621. /**
  4622. * 选中给定节点内部的所有节点
  4623. * @method selectNodeContents
  4624. * @remind 此时, 选区的开始容器和结束容器都是该节点, 其startOffset为0,
  4625. * 而endOffset是该节点的子节点数。
  4626. * @param { Node } node 目标节点, 当前range将包含该节点内的所有节点
  4627. * @return { UE.dom.Range } 当前range对象, 此时range仅包含给定节点的所有子节点
  4628. * @example
  4629. * ```html
  4630. * <!-- 选区示例 -->
  4631. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  4632. *
  4633. * <script>
  4634. *
  4635. * //执行操作
  4636. * range.selectNode( document.getElementsByTagName("b")[0] );
  4637. *
  4638. * //结果选区
  4639. * //<b>[xx<i>xxx</i><span>xxx</span>xxx]</b>
  4640. *
  4641. * </script>
  4642. * ```
  4643. */
  4644. selectNodeContents:function (node) {
  4645. return this.setStart(node, 0).setEndAtLast(node);
  4646. },
  4647. /**
  4648. * clone当前Range对象
  4649. * @method cloneRange
  4650. * @remind 返回的range是一个全新的range对象, 其内部所有属性与当前被clone的range相同。
  4651. * @return { UE.dom.Range } 当前range对象的一个副本
  4652. */
  4653. cloneRange:function () {
  4654. var me = this;
  4655. return new Range(me.document).setStart(me.startContainer, me.startOffset).setEnd(me.endContainer, me.endOffset);
  4656. },
  4657. /**
  4658. * 向当前选区的结束处闭合选区
  4659. * @method collapse
  4660. * @return { UE.dom.Range } 当前range对象
  4661. * @example
  4662. * ```html
  4663. * <!-- 选区示例 -->
  4664. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  4665. *
  4666. * <script>
  4667. *
  4668. * //执行操作
  4669. * range.collapse();
  4670. *
  4671. * //结果选区
  4672. * //“|”表示选区已闭合
  4673. * //<b>xx<i>xxx</i><span>xx|x</span>xxx</b>
  4674. *
  4675. * </script>
  4676. * ```
  4677. */
  4678. /**
  4679. * 闭合当前选区,根据给定的toStart参数项决定是向当前选区开始处闭合还是向结束处闭合,
  4680. * 如果toStart的值为true,则向开始位置闭合, 反之,向结束位置闭合。
  4681. * @method collapse
  4682. * @param { Boolean } toStart 是否向选区开始处闭合
  4683. * @return { UE.dom.Range } 当前range对象,此时range对象处于闭合状态
  4684. * @see UE.dom.Range:collapse()
  4685. * @example
  4686. * ```html
  4687. * <!-- 选区示例 -->
  4688. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  4689. *
  4690. * <script>
  4691. *
  4692. * //执行操作
  4693. * range.collapse( true );
  4694. *
  4695. * //结果选区
  4696. * //“|”表示选区已闭合
  4697. * //<b>xx<i>xxx</i><span>|xxx</span>xxx</b>
  4698. *
  4699. * </script>
  4700. * ```
  4701. */
  4702. collapse:function (toStart) {
  4703. var me = this;
  4704. if (toStart) {
  4705. me.endContainer = me.startContainer;
  4706. me.endOffset = me.startOffset;
  4707. } else {
  4708. me.startContainer = me.endContainer;
  4709. me.startOffset = me.endOffset;
  4710. }
  4711. me.collapsed = true;
  4712. return me;
  4713. },
  4714. /**
  4715. * 调整range的开始位置和结束位置,使其"收缩"到最小的位置
  4716. * @method shrinkBoundary
  4717. * @return { UE.dom.Range } 当前range对象
  4718. * @example
  4719. * ```html
  4720. * <span>xx<b>xx[</b>xxxxx]</span> => <span>xx<b>xx</b>[xxxxx]</span>
  4721. * ```
  4722. *
  4723. * @example
  4724. * ```html
  4725. * <!-- 选区示例 -->
  4726. * <b>x[xx</b><i>]xxx</i>
  4727. *
  4728. * <script>
  4729. *
  4730. * //执行收缩
  4731. * range.shrinkBoundary();
  4732. *
  4733. * //结果选区
  4734. * //<b>x[xx]</b><i>xxx</i>
  4735. * </script>
  4736. * ```
  4737. *
  4738. * @example
  4739. * ```html
  4740. * [<b><i>xxxx</i>xxxxxxx</b>] => <b><i>[xxxx</i>xxxxxxx]</b>
  4741. * ```
  4742. */
  4743. /**
  4744. * 调整range的开始位置和结束位置,使其"收缩"到最小的位置,
  4745. * 如果ignoreEnd的值为true,则忽略对结束位置的调整
  4746. * @method shrinkBoundary
  4747. * @param { Boolean } ignoreEnd 是否忽略对结束位置的调整
  4748. * @return { UE.dom.Range } 当前range对象
  4749. * @see UE.dom.domUtils.Range:shrinkBoundary()
  4750. */
  4751. shrinkBoundary:function (ignoreEnd) {
  4752. var me = this, child,
  4753. collapsed = me.collapsed;
  4754. function check(node){
  4755. return node.nodeType == 1 && !domUtils.isBookmarkNode(node) && !dtd.$empty[node.tagName] && !dtd.$nonChild[node.tagName]
  4756. }
  4757. while (me.startContainer.nodeType == 1 //是element
  4758. && (child = me.startContainer.childNodes[me.startOffset]) //子节点也是element
  4759. && check(child)) {
  4760. me.setStart(child, 0);
  4761. }
  4762. if (collapsed) {
  4763. return me.collapse(true);
  4764. }
  4765. if (!ignoreEnd) {
  4766. while (me.endContainer.nodeType == 1//是element
  4767. && me.endOffset > 0 //如果是空元素就退出 endOffset=0那么endOffst-1为负值,childNodes[endOffset]报错
  4768. && (child = me.endContainer.childNodes[me.endOffset - 1]) //子节点也是element
  4769. && check(child)) {
  4770. me.setEnd(child, child.childNodes.length);
  4771. }
  4772. }
  4773. return me;
  4774. },
  4775. /**
  4776. * 获取离当前选区内包含的所有节点最近的公共祖先节点,
  4777. * @method getCommonAncestor
  4778. * @remind 返回的公共祖先节点一定不是range自身的容器节点, 但有可能是一个文本节点
  4779. * @return { Node } 当前range对象内所有节点的公共祖先节点
  4780. * @example
  4781. * ```html
  4782. * //选区示例
  4783. * <span>xxx<b>x[x<em>xx]x</em>xxx</b>xx</span>
  4784. * <script>
  4785. *
  4786. * var node = range.getCommonAncestor();
  4787. *
  4788. * //公共祖先节点是: b节点
  4789. * //输出: B
  4790. * console.log(node.tagName);
  4791. *
  4792. * </script>
  4793. * ```
  4794. */
  4795. /**
  4796. * 获取当前选区所包含的所有节点的公共祖先节点, 可以根据给定的参数 includeSelf 决定获取到
  4797. * 的公共祖先节点是否可以是当前选区的startContainer或endContainer节点, 如果 includeSelf
  4798. * 的取值为true, 则返回的节点可以是自身的容器节点, 否则, 则不能是容器节点
  4799. * @method getCommonAncestor
  4800. * @param { Boolean } includeSelf 是否允许获取到的公共祖先节点是当前range对象的容器节点
  4801. * @return { Node } 当前range对象内所有节点的公共祖先节点
  4802. * @see UE.dom.Range:getCommonAncestor()
  4803. * @example
  4804. * ```html
  4805. * <body>
  4806. *
  4807. * <!-- 选区示例 -->
  4808. * <b>xxx<i>xxxx<span>xx[x</span>xx]x</i>xxxxxxx</b>
  4809. *
  4810. * <script>
  4811. *
  4812. * var node = range.getCommonAncestor( false );
  4813. *
  4814. * //这里的公共祖先节点是B而不是I, 是因为参数限制了获取到的节点不能是容器节点
  4815. * //output: B
  4816. * console.log( node.tagName );
  4817. *
  4818. * </script>
  4819. *
  4820. * </body>
  4821. * ```
  4822. */
  4823. /**
  4824. * 获取当前选区所包含的所有节点的公共祖先节点, 可以根据给定的参数 includeSelf 决定获取到
  4825. * 的公共祖先节点是否可以是当前选区的startContainer或endContainer节点, 如果 includeSelf
  4826. * 的取值为true, 则返回的节点可以是自身的容器节点, 否则, 则不能是容器节点; 同时可以根据
  4827. * ignoreTextNode 参数的取值决定是否忽略类型为文本节点的祖先节点。
  4828. * @method getCommonAncestor
  4829. * @param { Boolean } includeSelf 是否允许获取到的公共祖先节点是当前range对象的容器节点
  4830. * @param { Boolean } ignoreTextNode 获取祖先节点的过程中是否忽略类型为文本节点的祖先节点
  4831. * @return { Node } 当前range对象内所有节点的公共祖先节点
  4832. * @see UE.dom.Range:getCommonAncestor()
  4833. * @see UE.dom.Range:getCommonAncestor(Boolean)
  4834. * @example
  4835. * ```html
  4836. * <body>
  4837. *
  4838. * <!-- 选区示例 -->
  4839. * <b>xxx<i>xxxx<span>x[x]x</span>xxx</i>xxxxxxx</b>
  4840. *
  4841. * <script>
  4842. *
  4843. * var node = range.getCommonAncestor( true, false );
  4844. *
  4845. * //output: SPAN
  4846. * console.log( node.tagName );
  4847. *
  4848. * </script>
  4849. *
  4850. * </body>
  4851. * ```
  4852. */
  4853. getCommonAncestor:function (includeSelf, ignoreTextNode) {
  4854. var me = this,
  4855. start = me.startContainer,
  4856. end = me.endContainer;
  4857. if (start === end) {
  4858. if (includeSelf && selectOneNode(this)) {
  4859. start = start.childNodes[me.startOffset];
  4860. if(start.nodeType == 1)
  4861. return start;
  4862. }
  4863. //只有在上来就相等的情况下才会出现是文本的情况
  4864. return ignoreTextNode && start.nodeType == 3 ? start.parentNode : start;
  4865. }
  4866. return domUtils.getCommonAncestor(start, end);
  4867. },
  4868. /**
  4869. * 调整当前Range的开始和结束边界容器,如果是容器节点是文本节点,就调整到包含该文本节点的父节点上
  4870. * @method trimBoundary
  4871. * @remind 该操作有可能会引起文本节点被切开
  4872. * @return { UE.dom.Range } 当前range对象
  4873. * @example
  4874. * ```html
  4875. *
  4876. * //选区示例
  4877. * <b>xxx<i>[xxxxx]</i>xxx</b>
  4878. *
  4879. * <script>
  4880. * //未调整前, 选区的开始容器和结束都是文本节点
  4881. * //执行调整
  4882. * range.trimBoundary();
  4883. *
  4884. * //调整之后, 容器节点变成了i节点
  4885. * //<b>xxx[<i>xxxxx</i>]xxx</b>
  4886. * </script>
  4887. * ```
  4888. */
  4889. /**
  4890. * 调整当前Range的开始和结束边界容器,如果是容器节点是文本节点,就调整到包含该文本节点的父节点上,
  4891. * 可以根据 ignoreEnd 参数的值决定是否调整对结束边界的调整
  4892. * @method trimBoundary
  4893. * @param { Boolean } ignoreEnd 是否忽略对结束边界的调整
  4894. * @return { UE.dom.Range } 当前range对象
  4895. * @example
  4896. * ```html
  4897. *
  4898. * //选区示例
  4899. * <b>xxx<i>[xxxxx]</i>xxx</b>
  4900. *
  4901. * <script>
  4902. * //未调整前, 选区的开始容器和结束都是文本节点
  4903. * //执行调整
  4904. * range.trimBoundary( true );
  4905. *
  4906. * //调整之后, 开始容器节点变成了i节点
  4907. * //但是, 结束容器没有发生变化
  4908. * //<b>xxx[<i>xxxxx]</i>xxx</b>
  4909. * </script>
  4910. * ```
  4911. */
  4912. trimBoundary:function (ignoreEnd) {
  4913. this.txtToElmBoundary();
  4914. var start = this.startContainer,
  4915. offset = this.startOffset,
  4916. collapsed = this.collapsed,
  4917. end = this.endContainer;
  4918. if (start.nodeType == 3) {
  4919. if (offset == 0) {
  4920. this.setStartBefore(start);
  4921. } else {
  4922. if (offset >= start.nodeValue.length) {
  4923. this.setStartAfter(start);
  4924. } else {
  4925. var textNode = domUtils.split(start, offset);
  4926. //跟新结束边界
  4927. if (start === end) {
  4928. this.setEnd(textNode, this.endOffset - offset);
  4929. } else if (start.parentNode === end) {
  4930. this.endOffset += 1;
  4931. }
  4932. this.setStartBefore(textNode);
  4933. }
  4934. }
  4935. if (collapsed) {
  4936. return this.collapse(true);
  4937. }
  4938. }
  4939. if (!ignoreEnd) {
  4940. offset = this.endOffset;
  4941. end = this.endContainer;
  4942. if (end.nodeType == 3) {
  4943. if (offset == 0) {
  4944. this.setEndBefore(end);
  4945. } else {
  4946. offset < end.nodeValue.length && domUtils.split(end, offset);
  4947. this.setEndAfter(end);
  4948. }
  4949. }
  4950. }
  4951. return this;
  4952. },
  4953. /**
  4954. * 如果选区在文本的边界上,就扩展选区到文本的父节点上, 如果当前选区是闭合的, 则什么也不做
  4955. * @method txtToElmBoundary
  4956. * @remind 该操作不会修改dom节点
  4957. * @return { UE.dom.Range } 当前range对象
  4958. */
  4959. /**
  4960. * 如果选区在文本的边界上,就扩展选区到文本的父节点上, 如果当前选区是闭合的, 则根据参数项
  4961. * ignoreCollapsed 的值决定是否执行该调整
  4962. * @method txtToElmBoundary
  4963. * @param { Boolean } ignoreCollapsed 是否忽略选区的闭合状态, 如果该参数取值为true, 则
  4964. * 不论选区是否闭合, 都会执行该操作, 反之, 则不会对闭合的选区执行该操作
  4965. * @return { UE.dom.Range } 当前range对象
  4966. */
  4967. txtToElmBoundary:function (ignoreCollapsed) {
  4968. function adjust(r, c) {
  4969. var container = r[c + 'Container'],
  4970. offset = r[c + 'Offset'];
  4971. if (container.nodeType == 3) {
  4972. if (!offset) {
  4973. r['set' + c.replace(/(\w)/, function (a) {
  4974. return a.toUpperCase();
  4975. }) + 'Before'](container);
  4976. } else if (offset >= container.nodeValue.length) {
  4977. r['set' + c.replace(/(\w)/, function (a) {
  4978. return a.toUpperCase();
  4979. }) + 'After' ](container);
  4980. }
  4981. }
  4982. }
  4983. if (ignoreCollapsed || !this.collapsed) {
  4984. adjust(this, 'start');
  4985. adjust(this, 'end');
  4986. }
  4987. return this;
  4988. },
  4989. /**
  4990. * 在当前选区的开始位置前插入节点,新插入的节点会被该range包含
  4991. * @method insertNode
  4992. * @param { Node } node 需要插入的节点
  4993. * @remind 插入的节点可以是一个DocumentFragment依次插入多个节点
  4994. * @return { UE.dom.Range } 当前range对象
  4995. */
  4996. insertNode:function (node) {
  4997. var first = node, length = 1;
  4998. if (node.nodeType == 11) {
  4999. first = node.firstChild;
  5000. length = node.childNodes.length;
  5001. }
  5002. this.trimBoundary(true);
  5003. var start = this.startContainer,
  5004. offset = this.startOffset;
  5005. var nextNode = start.childNodes[ offset ];
  5006. if (nextNode) {
  5007. start.insertBefore(node, nextNode);
  5008. } else {
  5009. start.appendChild(node);
  5010. }
  5011. if (first.parentNode === this.endContainer) {
  5012. this.endOffset = this.endOffset + length;
  5013. }
  5014. return this.setStartBefore(first);
  5015. },
  5016. /**
  5017. * 闭合选区到当前选区的开始位置, 并且定位光标到闭合后的位置
  5018. * @method setCursor
  5019. * @return { UE.dom.Range } 当前range对象
  5020. * @see UE.dom.Range:collapse()
  5021. */
  5022. /**
  5023. * 闭合选区,可以根据参数toEnd的值控制选区是向前闭合还是向后闭合, 并且定位光标到闭合后的位置。
  5024. * @method setCursor
  5025. * @param { Boolean } toEnd 是否向后闭合, 如果为true, 则闭合选区时, 将向结束容器方向闭合,
  5026. * 反之,则向开始容器方向闭合
  5027. * @return { UE.dom.Range } 当前range对象
  5028. * @see UE.dom.Range:collapse(Boolean)
  5029. */
  5030. setCursor:function (toEnd, noFillData) {
  5031. return this.collapse(!toEnd).select(noFillData);
  5032. },
  5033. /**
  5034. * 创建当前range的一个书签,记录下当前range的位置,方便当dom树改变时,还能找回原来的选区位置
  5035. * @method createBookmark
  5036. * @param { Boolean } serialize 控制返回的标记位置是对当前位置的引用还是ID,如果该值为true,则
  5037. * 返回标记位置的ID, 反之则返回标记位置节点的引用
  5038. * @return { Object } 返回一个书签记录键值对, 其包含的key有: start => 开始标记的ID或者引用,
  5039. * end => 结束标记的ID或引用, id => 当前标记的类型, 如果为true,则表示
  5040. * 返回的记录的类型为ID, 反之则为引用
  5041. */
  5042. createBookmark:function (serialize, same) {
  5043. var endNode,
  5044. startNode = this.document.createElement('span');
  5045. startNode.style.cssText = 'display:none;line-height:0px;';
  5046. startNode.appendChild(this.document.createTextNode('\u200D'));
  5047. startNode.id = '_baidu_bookmark_start_' + (same ? '' : guid++);
  5048. if (!this.collapsed) {
  5049. endNode = startNode.cloneNode(true);
  5050. endNode.id = '_baidu_bookmark_end_' + (same ? '' : guid++);
  5051. }
  5052. this.insertNode(startNode);
  5053. if (endNode) {
  5054. this.collapse().insertNode(endNode).setEndBefore(endNode);
  5055. }
  5056. this.setStartAfter(startNode);
  5057. return {
  5058. start:serialize ? startNode.id : startNode,
  5059. end:endNode ? serialize ? endNode.id : endNode : null,
  5060. id:serialize
  5061. }
  5062. },
  5063. /**
  5064. * 调整当前range的边界到书签位置,并删除该书签对象所标记的位置内的节点
  5065. * @method moveToBookmark
  5066. * @param { BookMark } bookmark createBookmark所创建的标签对象
  5067. * @return { UE.dom.Range } 当前range对象
  5068. * @see UE.dom.Range:createBookmark(Boolean)
  5069. */
  5070. moveToBookmark:function (bookmark) {
  5071. var start = bookmark.id ? this.document.getElementById(bookmark.start) : bookmark.start,
  5072. end = bookmark.end && bookmark.id ? this.document.getElementById(bookmark.end) : bookmark.end;
  5073. this.setStartBefore(start);
  5074. domUtils.remove(start);
  5075. if (end) {
  5076. this.setEndBefore(end);
  5077. domUtils.remove(end);
  5078. } else {
  5079. this.collapse(true);
  5080. }
  5081. return this;
  5082. },
  5083. /**
  5084. * 调整range的边界,使其"放大"到最近的父节点
  5085. * @method enlarge
  5086. * @remind 会引起选区的变化
  5087. * @return { UE.dom.Range } 当前range对象
  5088. */
  5089. /**
  5090. * 调整range的边界,使其"放大"到最近的父节点,根据参数 toBlock 的取值, 可以
  5091. * 要求扩大之后的父节点是block节点
  5092. * @method enlarge
  5093. * @param { Boolean } toBlock 是否要求扩大之后的父节点必须是block节点
  5094. * @return { UE.dom.Range } 当前range对象
  5095. */
  5096. enlarge:function (toBlock, stopFn) {
  5097. var isBody = domUtils.isBody,
  5098. pre, node, tmp = this.document.createTextNode('');
  5099. if (toBlock) {
  5100. node = this.startContainer;
  5101. if (node.nodeType == 1) {
  5102. if (node.childNodes[this.startOffset]) {
  5103. pre = node = node.childNodes[this.startOffset]
  5104. } else {
  5105. node.appendChild(tmp);
  5106. pre = node = tmp;
  5107. }
  5108. } else {
  5109. pre = node;
  5110. }
  5111. while (1) {
  5112. if (domUtils.isBlockElm(node)) {
  5113. node = pre;
  5114. while ((pre = node.previousSibling) && !domUtils.isBlockElm(pre)) {
  5115. node = pre;
  5116. }
  5117. this.setStartBefore(node);
  5118. break;
  5119. }
  5120. pre = node;
  5121. node = node.parentNode;
  5122. }
  5123. node = this.endContainer;
  5124. if (node.nodeType == 1) {
  5125. if (pre = node.childNodes[this.endOffset]) {
  5126. node.insertBefore(tmp, pre);
  5127. } else {
  5128. node.appendChild(tmp);
  5129. }
  5130. pre = node = tmp;
  5131. } else {
  5132. pre = node;
  5133. }
  5134. while (1) {
  5135. if (domUtils.isBlockElm(node)) {
  5136. node = pre;
  5137. while ((pre = node.nextSibling) && !domUtils.isBlockElm(pre)) {
  5138. node = pre;
  5139. }
  5140. this.setEndAfter(node);
  5141. break;
  5142. }
  5143. pre = node;
  5144. node = node.parentNode;
  5145. }
  5146. if (tmp.parentNode === this.endContainer) {
  5147. this.endOffset--;
  5148. }
  5149. domUtils.remove(tmp);
  5150. }
  5151. // 扩展边界到最大
  5152. if (!this.collapsed) {
  5153. while (this.startOffset == 0) {
  5154. if (stopFn && stopFn(this.startContainer)) {
  5155. break;
  5156. }
  5157. if (isBody(this.startContainer)) {
  5158. break;
  5159. }
  5160. this.setStartBefore(this.startContainer);
  5161. }
  5162. while (this.endOffset == (this.endContainer.nodeType == 1 ? this.endContainer.childNodes.length : this.endContainer.nodeValue.length)) {
  5163. if (stopFn && stopFn(this.endContainer)) {
  5164. break;
  5165. }
  5166. if (isBody(this.endContainer)) {
  5167. break;
  5168. }
  5169. this.setEndAfter(this.endContainer);
  5170. }
  5171. }
  5172. return this;
  5173. },
  5174. enlargeToBlockElm:function(ignoreEnd){
  5175. while(!domUtils.isBlockElm(this.startContainer)){
  5176. this.setStartBefore(this.startContainer);
  5177. }
  5178. if(!ignoreEnd){
  5179. while(!domUtils.isBlockElm(this.endContainer)){
  5180. this.setEndAfter(this.endContainer);
  5181. }
  5182. }
  5183. return this;
  5184. },
  5185. /**
  5186. * 调整Range的边界,使其"缩小"到最合适的位置
  5187. * @method adjustmentBoundary
  5188. * @return { UE.dom.Range } 当前range对象
  5189. * @see UE.dom.Range:shrinkBoundary()
  5190. */
  5191. adjustmentBoundary:function () {
  5192. if (!this.collapsed) {
  5193. while (!domUtils.isBody(this.startContainer) &&
  5194. this.startOffset == this.startContainer[this.startContainer.nodeType == 3 ? 'nodeValue' : 'childNodes'].length &&
  5195. this.startContainer[this.startContainer.nodeType == 3 ? 'nodeValue' : 'childNodes'].length
  5196. ) {
  5197. this.setStartAfter(this.startContainer);
  5198. }
  5199. while (!domUtils.isBody(this.endContainer) && !this.endOffset &&
  5200. this.endContainer[this.endContainer.nodeType == 3 ? 'nodeValue' : 'childNodes'].length
  5201. ) {
  5202. this.setEndBefore(this.endContainer);
  5203. }
  5204. }
  5205. return this;
  5206. },
  5207. /**
  5208. * 给range选区中的内容添加给定的inline标签
  5209. * @method applyInlineStyle
  5210. * @param { String } tagName 需要添加的标签名
  5211. * @example
  5212. * ```html
  5213. * <p>xxxx[xxxx]x</p> ==> range.applyInlineStyle("strong") ==> <p>xxxx[<strong>xxxx</strong>]x</p>
  5214. * ```
  5215. */
  5216. /**
  5217. * 给range选区中的内容添加给定的inline标签, 并且为标签附加上一些初始化属性。
  5218. * @method applyInlineStyle
  5219. * @param { String } tagName 需要添加的标签名
  5220. * @param { Object } attrs 跟随新添加的标签的属性
  5221. * @return { UE.dom.Range } 当前选区
  5222. * @example
  5223. * ```html
  5224. * <p>xxxx[xxxx]x</p>
  5225. *
  5226. * ==>
  5227. *
  5228. * <!-- 执行操作 -->
  5229. * range.applyInlineStyle("strong",{"style":"font-size:12px"})
  5230. *
  5231. * ==>
  5232. *
  5233. * <p>xxxx[<strong style="font-size:12px">xxxx</strong>]x</p>
  5234. * ```
  5235. */
  5236. applyInlineStyle:function (tagName, attrs, list) {
  5237. if (this.collapsed)return this;
  5238. this.trimBoundary().enlarge(false,
  5239. function (node) {
  5240. return node.nodeType == 1 && domUtils.isBlockElm(node)
  5241. }).adjustmentBoundary();
  5242. var bookmark = this.createBookmark(),
  5243. end = bookmark.end,
  5244. filterFn = function (node) {
  5245. return node.nodeType == 1 ? node.tagName.toLowerCase() != 'br' : !domUtils.isWhitespace(node);
  5246. },
  5247. current = domUtils.getNextDomNode(bookmark.start, false, filterFn),
  5248. node,
  5249. pre,
  5250. range = this.cloneRange();
  5251. while (current && (domUtils.getPosition(current, end) & domUtils.POSITION_PRECEDING)) {
  5252. if (current.nodeType == 3 || dtd[tagName][current.tagName]) {
  5253. range.setStartBefore(current);
  5254. node = current;
  5255. while (node && (node.nodeType == 3 || dtd[tagName][node.tagName]) && node !== end) {
  5256. pre = node;
  5257. node = domUtils.getNextDomNode(node, node.nodeType == 1, null, function (parent) {
  5258. return dtd[tagName][parent.tagName];
  5259. });
  5260. }
  5261. var frag = range.setEndAfter(pre).extractContents(), elm;
  5262. if (list && list.length > 0) {
  5263. var level, top;
  5264. top = level = list[0].cloneNode(false);
  5265. for (var i = 1, ci; ci = list[i++];) {
  5266. level.appendChild(ci.cloneNode(false));
  5267. level = level.firstChild;
  5268. }
  5269. elm = level;
  5270. } else {
  5271. elm = range.document.createElement(tagName);
  5272. }
  5273. if (attrs) {
  5274. domUtils.setAttributes(elm, attrs);
  5275. }
  5276. elm.appendChild(frag);
  5277. range.insertNode(list ? top : elm);
  5278. //处理下滑线在a上的情况
  5279. var aNode;
  5280. if (tagName == 'span' && attrs.style && /text\-decoration/.test(attrs.style) && (aNode = domUtils.findParentByTagName(elm, 'a', true))) {
  5281. domUtils.setAttributes(aNode, attrs);
  5282. domUtils.remove(elm, true);
  5283. elm = aNode;
  5284. } else {
  5285. domUtils.mergeSibling(elm);
  5286. domUtils.clearEmptySibling(elm);
  5287. }
  5288. //去除子节点相同的
  5289. domUtils.mergeChild(elm, attrs);
  5290. current = domUtils.getNextDomNode(elm, false, filterFn);
  5291. domUtils.mergeToParent(elm);
  5292. if (node === end) {
  5293. break;
  5294. }
  5295. } else {
  5296. current = domUtils.getNextDomNode(current, true, filterFn);
  5297. }
  5298. }
  5299. return this.moveToBookmark(bookmark);
  5300. },
  5301. /**
  5302. * 移除当前选区内指定的inline标签,但保留其中的内容
  5303. * @method removeInlineStyle
  5304. * @param { String } tagName 需要移除的标签名
  5305. * @return { UE.dom.Range } 当前的range对象
  5306. * @example
  5307. * ```html
  5308. * xx[x<span>xxx<em>yyy</em>zz]z</span> => range.removeInlineStyle(["em"]) => xx[x<span>xxxyyyzz]z</span>
  5309. * ```
  5310. */
  5311. /**
  5312. * 移除当前选区内指定的一组inline标签,但保留其中的内容
  5313. * @method removeInlineStyle
  5314. * @param { Array } tagNameArr 需要移除的标签名的数组
  5315. * @return { UE.dom.Range } 当前的range对象
  5316. * @see UE.dom.Range:removeInlineStyle(String)
  5317. */
  5318. removeInlineStyle:function (tagNames) {
  5319. if (this.collapsed)return this;
  5320. tagNames = utils.isArray(tagNames) ? tagNames : [tagNames];
  5321. this.shrinkBoundary().adjustmentBoundary();
  5322. var start = this.startContainer, end = this.endContainer;
  5323. while (1) {
  5324. if (start.nodeType == 1) {
  5325. if (utils.indexOf(tagNames, start.tagName.toLowerCase()) > -1) {
  5326. break;
  5327. }
  5328. if (start.tagName.toLowerCase() == 'body') {
  5329. start = null;
  5330. break;
  5331. }
  5332. }
  5333. start = start.parentNode;
  5334. }
  5335. while (1) {
  5336. if (end.nodeType == 1) {
  5337. if (utils.indexOf(tagNames, end.tagName.toLowerCase()) > -1) {
  5338. break;
  5339. }
  5340. if (end.tagName.toLowerCase() == 'body') {
  5341. end = null;
  5342. break;
  5343. }
  5344. }
  5345. end = end.parentNode;
  5346. }
  5347. var bookmark = this.createBookmark(),
  5348. frag,
  5349. tmpRange;
  5350. if (start) {
  5351. tmpRange = this.cloneRange().setEndBefore(bookmark.start).setStartBefore(start);
  5352. frag = tmpRange.extractContents();
  5353. tmpRange.insertNode(frag);
  5354. domUtils.clearEmptySibling(start, true);
  5355. start.parentNode.insertBefore(bookmark.start, start);
  5356. }
  5357. if (end) {
  5358. tmpRange = this.cloneRange().setStartAfter(bookmark.end).setEndAfter(end);
  5359. frag = tmpRange.extractContents();
  5360. tmpRange.insertNode(frag);
  5361. domUtils.clearEmptySibling(end, false, true);
  5362. end.parentNode.insertBefore(bookmark.end, end.nextSibling);
  5363. }
  5364. var current = domUtils.getNextDomNode(bookmark.start, false, function (node) {
  5365. return node.nodeType == 1;
  5366. }), next;
  5367. while (current && current !== bookmark.end) {
  5368. next = domUtils.getNextDomNode(current, true, function (node) {
  5369. return node.nodeType == 1;
  5370. });
  5371. if (utils.indexOf(tagNames, current.tagName.toLowerCase()) > -1) {
  5372. domUtils.remove(current, true);
  5373. }
  5374. current = next;
  5375. }
  5376. return this.moveToBookmark(bookmark);
  5377. },
  5378. /**
  5379. * 获取当前选中的自闭合的节点
  5380. * @method getClosedNode
  5381. * @return { Node | NULL } 如果当前选中的是自闭合节点, 则返回该节点, 否则返回NULL
  5382. */
  5383. getClosedNode:function () {
  5384. var node;
  5385. if (!this.collapsed) {
  5386. var range = this.cloneRange().adjustmentBoundary().shrinkBoundary();
  5387. if (selectOneNode(range)) {
  5388. var child = range.startContainer.childNodes[range.startOffset];
  5389. if (child && child.nodeType == 1 && (dtd.$empty[child.tagName] || dtd.$nonChild[child.tagName])) {
  5390. node = child;
  5391. }
  5392. }
  5393. }
  5394. return node;
  5395. },
  5396. /**
  5397. * 在页面上高亮range所表示的选区
  5398. * @method select
  5399. * @return { UE.dom.Range } 返回当前Range对象
  5400. */
  5401. //这里不区分ie9以上,trace:3824
  5402. select:browser.ie ? function (noFillData, textRange) {
  5403. var nativeRange;
  5404. if (!this.collapsed)
  5405. this.shrinkBoundary();
  5406. var node = this.getClosedNode();
  5407. if (node && !textRange) {
  5408. try {
  5409. nativeRange = this.document.body.createControlRange();
  5410. nativeRange.addElement(node);
  5411. nativeRange.select();
  5412. } catch (e) {}
  5413. return this;
  5414. }
  5415. var bookmark = this.createBookmark(),
  5416. start = bookmark.start,
  5417. end;
  5418. nativeRange = this.document.body.createTextRange();
  5419. nativeRange.moveToElementText(start);
  5420. nativeRange.moveStart('character', 1);
  5421. if (!this.collapsed) {
  5422. var nativeRangeEnd = this.document.body.createTextRange();
  5423. end = bookmark.end;
  5424. nativeRangeEnd.moveToElementText(end);
  5425. nativeRange.setEndPoint('EndToEnd', nativeRangeEnd);
  5426. } else {
  5427. if (!noFillData && this.startContainer.nodeType != 3) {
  5428. //使用<span>|x<span>固定住光标
  5429. var tmpText = this.document.createTextNode(fillChar),
  5430. tmp = this.document.createElement('span');
  5431. tmp.appendChild(this.document.createTextNode(fillChar));
  5432. start.parentNode.insertBefore(tmp, start);
  5433. start.parentNode.insertBefore(tmpText, start);
  5434. //当点b,i,u时,不能清除i上边的b
  5435. removeFillData(this.document, tmpText);
  5436. fillData = tmpText;
  5437. mergeSibling(tmp, 'previousSibling');
  5438. mergeSibling(start, 'nextSibling');
  5439. nativeRange.moveStart('character', -1);
  5440. nativeRange.collapse(true);
  5441. }
  5442. }
  5443. this.moveToBookmark(bookmark);
  5444. tmp && domUtils.remove(tmp);
  5445. //IE在隐藏状态下不支持range操作,catch一下
  5446. try {
  5447. nativeRange.select();
  5448. } catch (e) {
  5449. }
  5450. return this;
  5451. } : function (notInsertFillData) {
  5452. function checkOffset(rng){
  5453. function check(node,offset,dir){
  5454. if(node.nodeType == 3 && node.nodeValue.length < offset){
  5455. rng[dir + 'Offset'] = node.nodeValue.length
  5456. }
  5457. }
  5458. check(rng.startContainer,rng.startOffset,'start');
  5459. check(rng.endContainer,rng.endOffset,'end');
  5460. }
  5461. var win = domUtils.getWindow(this.document),
  5462. sel = win.getSelection(),
  5463. txtNode;
  5464. //FF下关闭自动长高时滚动条在关闭dialog时会跳
  5465. //ff下如果不body.focus将不能定位闭合光标到编辑器内
  5466. browser.gecko ? this.document.body.focus() : win.focus();
  5467. if (sel) {
  5468. sel.removeAllRanges();
  5469. // trace:870 chrome/safari后边是br对于闭合得range不能定位 所以去掉了判断
  5470. // this.startContainer.nodeType != 3 &&! ((child = this.startContainer.childNodes[this.startOffset]) && child.nodeType == 1 && child.tagName == 'BR'
  5471. if (this.collapsed && !notInsertFillData) {
  5472. // //opear如果没有节点接着,原生的不能够定位,不能在body的第一级插入空白节点
  5473. // if (notInsertFillData && browser.opera && !domUtils.isBody(this.startContainer) && this.startContainer.nodeType == 1) {
  5474. // var tmp = this.document.createTextNode('');
  5475. // this.insertNode(tmp).setStart(tmp, 0).collapse(true);
  5476. // }
  5477. //
  5478. //处理光标落在文本节点的情况
  5479. //处理以下的情况
  5480. //<b>|xxxx</b>
  5481. //<b>xxxx</b>|xxxx
  5482. //xxxx<b>|</b>
  5483. var start = this.startContainer,child = start;
  5484. if(start.nodeType == 1){
  5485. child = start.childNodes[this.startOffset];
  5486. }
  5487. if( !(start.nodeType == 3 && this.startOffset) &&
  5488. (child ?
  5489. (!child.previousSibling || child.previousSibling.nodeType != 3)
  5490. :
  5491. (!start.lastChild || start.lastChild.nodeType != 3)
  5492. )
  5493. ){
  5494. txtNode = this.document.createTextNode(fillChar);
  5495. //跟着前边走
  5496. this.insertNode(txtNode);
  5497. removeFillData(this.document, txtNode);
  5498. mergeSibling(txtNode, 'previousSibling');
  5499. mergeSibling(txtNode, 'nextSibling');
  5500. fillData = txtNode;
  5501. this.setStart(txtNode, browser.webkit ? 1 : 0).collapse(true);
  5502. }
  5503. }
  5504. var nativeRange = this.document.createRange();
  5505. if(this.collapsed && browser.opera && this.startContainer.nodeType == 1){
  5506. var child = this.startContainer.childNodes[this.startOffset];
  5507. if(!child){
  5508. //往前靠拢
  5509. child = this.startContainer.lastChild;
  5510. if( child && domUtils.isBr(child)){
  5511. this.setStartBefore(child).collapse(true);
  5512. }
  5513. }else{
  5514. //向后靠拢
  5515. while(child && domUtils.isBlockElm(child)){
  5516. if(child.nodeType == 1 && child.childNodes[0]){
  5517. child = child.childNodes[0]
  5518. }else{
  5519. break;
  5520. }
  5521. }
  5522. child && this.setStartBefore(child).collapse(true)
  5523. }
  5524. }
  5525. //是createAddress最后一位算的不准,现在这里进行微调
  5526. checkOffset(this);
  5527. nativeRange.setStart(this.startContainer, this.startOffset);
  5528. nativeRange.setEnd(this.endContainer, this.endOffset);
  5529. sel.addRange(nativeRange);
  5530. }
  5531. return this;
  5532. },
  5533. /**
  5534. * 滚动到当前range开始的位置
  5535. * @method scrollToView
  5536. * @param { Window } win 当前range对象所属的window对象
  5537. * @return { UE.dom.Range } 当前Range对象
  5538. */
  5539. /**
  5540. * 滚动到距离当前range开始位置 offset 的位置处
  5541. * @method scrollToView
  5542. * @param { Window } win 当前range对象所属的window对象
  5543. * @param { Number } offset 距离range开始位置处的偏移量, 如果为正数, 则向下偏移, 反之, 则向上偏移
  5544. * @return { UE.dom.Range } 当前Range对象
  5545. */
  5546. scrollToView:function (win, offset) {
  5547. win = win ? window : domUtils.getWindow(this.document);
  5548. var me = this,
  5549. span = me.document.createElement('span');
  5550. //trace:717
  5551. span.innerHTML = '&nbsp;';
  5552. me.cloneRange().insertNode(span);
  5553. domUtils.scrollToView(span, win, offset);
  5554. domUtils.remove(span);
  5555. return me;
  5556. },
  5557. /**
  5558. * 判断当前选区内容是否占位符
  5559. * @private
  5560. * @method inFillChar
  5561. * @return { Boolean } 如果是占位符返回true,否则返回false
  5562. */
  5563. inFillChar : function(){
  5564. var start = this.startContainer;
  5565. if(this.collapsed && start.nodeType == 3
  5566. && start.nodeValue.replace(new RegExp('^' + domUtils.fillChar),'').length + 1 == start.nodeValue.length
  5567. ){
  5568. return true;
  5569. }
  5570. return false;
  5571. },
  5572. /**
  5573. * 保存
  5574. * @method createAddress
  5575. * @private
  5576. * @return { Boolean } 返回开始和结束的位置
  5577. * @example
  5578. * ```html
  5579. * <body>
  5580. * <p>
  5581. * aaaa
  5582. * <em>
  5583. * <!-- 选区开始 -->
  5584. * bbbb
  5585. * <!-- 选区结束 -->
  5586. * </em>
  5587. * </p>
  5588. *
  5589. * <script>
  5590. * //output: {startAddress:[0,1,0,0],endAddress:[0,1,0,4]}
  5591. * console.log( range.createAddress() );
  5592. * </script>
  5593. * </body>
  5594. * ```
  5595. */
  5596. createAddress : function(ignoreEnd,ignoreTxt){
  5597. var addr = {},me = this;
  5598. function getAddress(isStart){
  5599. var node = isStart ? me.startContainer : me.endContainer;
  5600. var parents = domUtils.findParents(node,true,function(node){return !domUtils.isBody(node)}),
  5601. addrs = [];
  5602. for(var i = 0,ci;ci = parents[i++];){
  5603. addrs.push(domUtils.getNodeIndex(ci,ignoreTxt));
  5604. }
  5605. var firstIndex = 0;
  5606. if(ignoreTxt){
  5607. if(node.nodeType == 3){
  5608. var tmpNode = node.previousSibling;
  5609. while(tmpNode && tmpNode.nodeType == 3){
  5610. firstIndex += tmpNode.nodeValue.replace(fillCharReg,'').length;
  5611. tmpNode = tmpNode.previousSibling;
  5612. }
  5613. firstIndex += (isStart ? me.startOffset : me.endOffset)// - (fillCharReg.test(node.nodeValue) ? 1 : 0 )
  5614. }else{
  5615. node = node.childNodes[ isStart ? me.startOffset : me.endOffset];
  5616. if(node){
  5617. firstIndex = domUtils.getNodeIndex(node,ignoreTxt);
  5618. }else{
  5619. node = isStart ? me.startContainer : me.endContainer;
  5620. var first = node.firstChild;
  5621. while(first){
  5622. if(domUtils.isFillChar(first)){
  5623. first = first.nextSibling;
  5624. continue;
  5625. }
  5626. firstIndex++;
  5627. if(first.nodeType == 3){
  5628. while( first && first.nodeType == 3){
  5629. first = first.nextSibling;
  5630. }
  5631. }else{
  5632. first = first.nextSibling;
  5633. }
  5634. }
  5635. }
  5636. }
  5637. }else{
  5638. firstIndex = isStart ? domUtils.isFillChar(node) ? 0 : me.startOffset : me.endOffset
  5639. }
  5640. if(firstIndex < 0){
  5641. firstIndex = 0;
  5642. }
  5643. addrs.push(firstIndex);
  5644. return addrs;
  5645. }
  5646. addr.startAddress = getAddress(true);
  5647. if(!ignoreEnd){
  5648. addr.endAddress = me.collapsed ? [].concat(addr.startAddress) : getAddress();
  5649. }
  5650. return addr;
  5651. },
  5652. /**
  5653. * 保存
  5654. * @method createAddress
  5655. * @private
  5656. * @return { Boolean } 返回开始和结束的位置
  5657. * @example
  5658. * ```html
  5659. * <body>
  5660. * <p>
  5661. * aaaa
  5662. * <em>
  5663. * <!-- 选区开始 -->
  5664. * bbbb
  5665. * <!-- 选区结束 -->
  5666. * </em>
  5667. * </p>
  5668. *
  5669. * <script>
  5670. * var range = editor.selection.getRange();
  5671. * range.moveToAddress({startAddress:[0,1,0,0],endAddress:[0,1,0,4]});
  5672. * range.select();
  5673. * //output: 'bbbb'
  5674. * console.log(editor.selection.getText());
  5675. * </script>
  5676. * </body>
  5677. * ```
  5678. */
  5679. moveToAddress : function(addr,ignoreEnd){
  5680. var me = this;
  5681. function getNode(address,isStart){
  5682. var tmpNode = me.document.body,
  5683. parentNode,offset;
  5684. for(var i= 0,ci,l=address.length;i<l;i++){
  5685. ci = address[i];
  5686. parentNode = tmpNode;
  5687. tmpNode = tmpNode.childNodes[ci];
  5688. if(!tmpNode){
  5689. offset = ci;
  5690. break;
  5691. }
  5692. }
  5693. if(isStart){
  5694. if(tmpNode){
  5695. me.setStartBefore(tmpNode)
  5696. }else{
  5697. me.setStart(parentNode,offset)
  5698. }
  5699. }else{
  5700. if(tmpNode){
  5701. me.setEndBefore(tmpNode)
  5702. }else{
  5703. me.setEnd(parentNode,offset)
  5704. }
  5705. }
  5706. }
  5707. getNode(addr.startAddress,true);
  5708. !ignoreEnd && addr.endAddress && getNode(addr.endAddress);
  5709. return me;
  5710. },
  5711. /**
  5712. * 判断给定的Range对象是否和当前Range对象表示的是同一个选区
  5713. * @method equals
  5714. * @param { UE.dom.Range } 需要判断的Range对象
  5715. * @return { Boolean } 如果给定的Range对象与当前Range对象表示的是同一个选区, 则返回true, 否则返回false
  5716. */
  5717. equals : function(rng){
  5718. for(var p in this){
  5719. if(this.hasOwnProperty(p)){
  5720. if(this[p] !== rng[p])
  5721. return false
  5722. }
  5723. }
  5724. return true;
  5725. },
  5726. /**
  5727. * 遍历range内的节点。每当遍历一个节点时, 都会执行参数项 doFn 指定的函数, 该函数的接受当前遍历的节点
  5728. * 作为其参数。
  5729. * @method traversal
  5730. * @param { Function } doFn 对每个遍历的节点要执行的方法, 该方法接受当前遍历的节点作为其参数
  5731. * @return { UE.dom.Range } 当前range对象
  5732. * @example
  5733. * ```html
  5734. *
  5735. * <body>
  5736. *
  5737. * <!-- 选区开始 -->
  5738. * <span></span>
  5739. * <a></a>
  5740. * <!-- 选区结束 -->
  5741. * </body>
  5742. *
  5743. * <script>
  5744. *
  5745. * //output: <span></span><a></a>
  5746. * console.log( range.cloneContents() );
  5747. *
  5748. * range.traversal( function ( node ) {
  5749. *
  5750. * if ( node.nodeType === 1 ) {
  5751. * node.className = "test";
  5752. * }
  5753. *
  5754. * } );
  5755. *
  5756. * //output: <span class="test"></span><a class="test"></a>
  5757. * console.log( range.cloneContents() );
  5758. *
  5759. * </script>
  5760. * ```
  5761. */
  5762. /**
  5763. * 遍历range内的节点。
  5764. * 每当遍历一个节点时, 都会执行参数项 doFn 指定的函数, 该函数的接受当前遍历的节点
  5765. * 作为其参数。
  5766. * 可以通过参数项 filterFn 来指定一个过滤器, 只有符合该过滤器过滤规则的节点才会触
  5767. * 发doFn函数的执行
  5768. * @method traversal
  5769. * @param { Function } doFn 对每个遍历的节点要执行的方法, 该方法接受当前遍历的节点作为其参数
  5770. * @param { Function } filterFn 过滤器, 该函数接受当前遍历的节点作为参数, 如果该节点满足过滤
  5771. * 规则, 请返回true, 该节点会触发doFn, 否则, 请返回false, 则该节点不
  5772. * 会触发doFn。
  5773. * @return { UE.dom.Range } 当前range对象
  5774. * @see UE.dom.Range:traversal(Function)
  5775. * @example
  5776. * ```html
  5777. *
  5778. * <body>
  5779. *
  5780. * <!-- 选区开始 -->
  5781. * <span></span>
  5782. * <a></a>
  5783. * <!-- 选区结束 -->
  5784. * </body>
  5785. *
  5786. * <script>
  5787. *
  5788. * //output: <span></span><a></a>
  5789. * console.log( range.cloneContents() );
  5790. *
  5791. * range.traversal( function ( node ) {
  5792. *
  5793. * node.className = "test";
  5794. *
  5795. * }, function ( node ) {
  5796. * return node.nodeType === 1;
  5797. * } );
  5798. *
  5799. * //output: <span class="test"></span><a class="test"></a>
  5800. * console.log( range.cloneContents() );
  5801. *
  5802. * </script>
  5803. * ```
  5804. */
  5805. traversal:function(doFn,filterFn){
  5806. if (this.collapsed)
  5807. return this;
  5808. var bookmark = this.createBookmark(),
  5809. end = bookmark.end,
  5810. current = domUtils.getNextDomNode(bookmark.start, false, filterFn);
  5811. while (current && current !== end && (domUtils.getPosition(current, end) & domUtils.POSITION_PRECEDING)) {
  5812. var tmpNode = domUtils.getNextDomNode(current,false,filterFn);
  5813. doFn(current);
  5814. current = tmpNode;
  5815. }
  5816. return this.moveToBookmark(bookmark);
  5817. }
  5818. };
  5819. })();
  5820. // core/Selection.js
  5821. /**
  5822. * 选集
  5823. * @file
  5824. * @module UE.dom
  5825. * @class Selection
  5826. * @since 1.2.6.1
  5827. */
  5828. /**
  5829. * 选区集合
  5830. * @unfile
  5831. * @module UE.dom
  5832. * @class Selection
  5833. */
  5834. (function () {
  5835. function getBoundaryInformation( range, start ) {
  5836. var getIndex = domUtils.getNodeIndex;
  5837. range = range.duplicate();
  5838. range.collapse( start );
  5839. var parent = range.parentElement();
  5840. //如果节点里没有子节点,直接退出
  5841. if ( !parent.hasChildNodes() ) {
  5842. return {container:parent, offset:0};
  5843. }
  5844. var siblings = parent.children,
  5845. child,
  5846. testRange = range.duplicate(),
  5847. startIndex = 0, endIndex = siblings.length - 1, index = -1,
  5848. distance;
  5849. while ( startIndex <= endIndex ) {
  5850. index = Math.floor( (startIndex + endIndex) / 2 );
  5851. child = siblings[index];
  5852. testRange.moveToElementText( child );
  5853. var position = testRange.compareEndPoints( 'StartToStart', range );
  5854. if ( position > 0 ) {
  5855. endIndex = index - 1;
  5856. } else if ( position < 0 ) {
  5857. startIndex = index + 1;
  5858. } else {
  5859. //trace:1043
  5860. return {container:parent, offset:getIndex( child )};
  5861. }
  5862. }
  5863. if ( index == -1 ) {
  5864. testRange.moveToElementText( parent );
  5865. testRange.setEndPoint( 'StartToStart', range );
  5866. distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length;
  5867. siblings = parent.childNodes;
  5868. if ( !distance ) {
  5869. child = siblings[siblings.length - 1];
  5870. return {container:child, offset:child.nodeValue.length};
  5871. }
  5872. var i = siblings.length;
  5873. while ( distance > 0 ){
  5874. distance -= siblings[ --i ].nodeValue.length;
  5875. }
  5876. return {container:siblings[i], offset:-distance};
  5877. }
  5878. testRange.collapse( position > 0 );
  5879. testRange.setEndPoint( position > 0 ? 'StartToStart' : 'EndToStart', range );
  5880. distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length;
  5881. if ( !distance ) {
  5882. return dtd.$empty[child.tagName] || dtd.$nonChild[child.tagName] ?
  5883. {container:parent, offset:getIndex( child ) + (position > 0 ? 0 : 1)} :
  5884. {container:child, offset:position > 0 ? 0 : child.childNodes.length}
  5885. }
  5886. while ( distance > 0 ) {
  5887. try {
  5888. var pre = child;
  5889. child = child[position > 0 ? 'previousSibling' : 'nextSibling'];
  5890. distance -= child.nodeValue.length;
  5891. } catch ( e ) {
  5892. return {container:parent, offset:getIndex( pre )};
  5893. }
  5894. }
  5895. return {container:child, offset:position > 0 ? -distance : child.nodeValue.length + distance}
  5896. }
  5897. /**
  5898. * 将ieRange转换为Range对象
  5899. * @param {Range} ieRange ieRange对象
  5900. * @param {Range} range Range对象
  5901. * @return {Range} range 返回转换后的Range对象
  5902. */
  5903. function transformIERangeToRange( ieRange, range ) {
  5904. if ( ieRange.item ) {
  5905. range.selectNode( ieRange.item( 0 ) );
  5906. } else {
  5907. var bi = getBoundaryInformation( ieRange, true );
  5908. range.setStart( bi.container, bi.offset );
  5909. if ( ieRange.compareEndPoints( 'StartToEnd', ieRange ) != 0 ) {
  5910. bi = getBoundaryInformation( ieRange, false );
  5911. range.setEnd( bi.container, bi.offset );
  5912. }
  5913. }
  5914. return range;
  5915. }
  5916. /**
  5917. * 获得ieRange
  5918. * @param {Selection} sel Selection对象
  5919. * @return {ieRange} 得到ieRange
  5920. */
  5921. function _getIERange( sel ) {
  5922. var ieRange;
  5923. //ie下有可能报错
  5924. try {
  5925. ieRange = sel.getNative().createRange();
  5926. } catch ( e ) {
  5927. return null;
  5928. }
  5929. var el = ieRange.item ? ieRange.item( 0 ) : ieRange.parentElement();
  5930. if ( ( el.ownerDocument || el ) === sel.document ) {
  5931. return ieRange;
  5932. }
  5933. return null;
  5934. }
  5935. var Selection = dom.Selection = function ( doc ) {
  5936. var me = this, iframe;
  5937. me.document = doc;
  5938. if ( browser.ie9below ) {
  5939. iframe = domUtils.getWindow( doc ).frameElement;
  5940. domUtils.on( iframe, 'beforedeactivate', function () {
  5941. me._bakIERange = me.getIERange();
  5942. } );
  5943. domUtils.on( iframe, 'activate', function () {
  5944. try {
  5945. if ( !_getIERange( me ) && me._bakIERange ) {
  5946. me._bakIERange.select();
  5947. }
  5948. } catch ( ex ) {
  5949. }
  5950. me._bakIERange = null;
  5951. } );
  5952. }
  5953. iframe = doc = null;
  5954. };
  5955. Selection.prototype = {
  5956. rangeInBody : function(rng,txtRange){
  5957. var node = browser.ie9below || txtRange ? rng.item ? rng.item() : rng.parentElement() : rng.startContainer;
  5958. return node === this.document.body || domUtils.inDoc(node,this.document);
  5959. },
  5960. /**
  5961. * 获取原生seleciton对象
  5962. * @method getNative
  5963. * @return { Object } 获得selection对象
  5964. * @example
  5965. * ```javascript
  5966. * editor.selection.getNative();
  5967. * ```
  5968. */
  5969. getNative:function () {
  5970. var doc = this.document;
  5971. try {
  5972. return !doc ? null : browser.ie9below ? doc.selection : domUtils.getWindow( doc ).getSelection();
  5973. } catch ( e ) {
  5974. return null;
  5975. }
  5976. },
  5977. /**
  5978. * 获得ieRange
  5979. * @method getIERange
  5980. * @return { Object } 返回ie原生的Range
  5981. * @example
  5982. * ```javascript
  5983. * editor.selection.getIERange();
  5984. * ```
  5985. */
  5986. getIERange:function () {
  5987. var ieRange = _getIERange( this );
  5988. if ( !ieRange ) {
  5989. if ( this._bakIERange ) {
  5990. return this._bakIERange;
  5991. }
  5992. }
  5993. return ieRange;
  5994. },
  5995. /**
  5996. * 缓存当前选区的range和选区的开始节点
  5997. * @method cache
  5998. */
  5999. cache:function () {
  6000. this.clear();
  6001. this._cachedRange = this.getRange();
  6002. this._cachedStartElement = this.getStart();
  6003. this._cachedStartElementPath = this.getStartElementPath();
  6004. },
  6005. /**
  6006. * 获取选区开始位置的父节点到body
  6007. * @method getStartElementPath
  6008. * @return { Array } 返回父节点集合
  6009. * @example
  6010. * ```javascript
  6011. * editor.selection.getStartElementPath();
  6012. * ```
  6013. */
  6014. getStartElementPath:function () {
  6015. if ( this._cachedStartElementPath ) {
  6016. return this._cachedStartElementPath;
  6017. }
  6018. var start = this.getStart();
  6019. if ( start ) {
  6020. return domUtils.findParents( start, true, null, true )
  6021. }
  6022. return [];
  6023. },
  6024. /**
  6025. * 清空缓存
  6026. * @method clear
  6027. */
  6028. clear:function () {
  6029. this._cachedStartElementPath = this._cachedRange = this._cachedStartElement = null;
  6030. },
  6031. /**
  6032. * 编辑器是否得到了选区
  6033. * @method isFocus
  6034. */
  6035. isFocus:function () {
  6036. try {
  6037. if(browser.ie9below){
  6038. var nativeRange = _getIERange(this);
  6039. return !!(nativeRange && this.rangeInBody(nativeRange));
  6040. }else{
  6041. return !!this.getNative().rangeCount;
  6042. }
  6043. } catch ( e ) {
  6044. return false;
  6045. }
  6046. },
  6047. /**
  6048. * 获取选区对应的Range
  6049. * @method getRange
  6050. * @return { Object } 得到Range对象
  6051. * @example
  6052. * ```javascript
  6053. * editor.selection.getRange();
  6054. * ```
  6055. */
  6056. getRange:function () {
  6057. var me = this;
  6058. function optimze( range ) {
  6059. var child = me.document.body.firstChild,
  6060. collapsed = range.collapsed;
  6061. while ( child && child.firstChild ) {
  6062. range.setStart( child, 0 );
  6063. child = child.firstChild;
  6064. }
  6065. if ( !range.startContainer ) {
  6066. range.setStart( me.document.body, 0 )
  6067. }
  6068. if ( collapsed ) {
  6069. range.collapse( true );
  6070. }
  6071. }
  6072. if ( me._cachedRange != null ) {
  6073. return this._cachedRange;
  6074. }
  6075. var range = new baidu.editor.dom.Range( me.document );
  6076. if ( browser.ie9below ) {
  6077. var nativeRange = me.getIERange();
  6078. if ( nativeRange ) {
  6079. //备份的_bakIERange可能已经实效了,dom树发生了变化比如从源码模式切回来,所以try一下,实效就放到body开始位置
  6080. try{
  6081. transformIERangeToRange( nativeRange, range );
  6082. }catch(e){
  6083. optimze( range );
  6084. }
  6085. } else {
  6086. optimze( range );
  6087. }
  6088. } else {
  6089. var sel = me.getNative();
  6090. if ( sel && sel.rangeCount ) {
  6091. var firstRange = sel.getRangeAt( 0 );
  6092. var lastRange = sel.getRangeAt( sel.rangeCount - 1 );
  6093. range.setStart( firstRange.startContainer, firstRange.startOffset ).setEnd( lastRange.endContainer, lastRange.endOffset );
  6094. if ( range.collapsed && domUtils.isBody( range.startContainer ) && !range.startOffset ) {
  6095. optimze( range );
  6096. }
  6097. } else {
  6098. //trace:1734 有可能已经不在dom树上了,标识的节点
  6099. if ( this._bakRange && domUtils.inDoc( this._bakRange.startContainer, this.document ) ){
  6100. return this._bakRange;
  6101. }
  6102. optimze( range );
  6103. }
  6104. }
  6105. return this._bakRange = range;
  6106. },
  6107. /**
  6108. * 获取开始元素,用于状态反射
  6109. * @method getStart
  6110. * @return { Element } 获得开始元素
  6111. * @example
  6112. * ```javascript
  6113. * editor.selection.getStart();
  6114. * ```
  6115. */
  6116. getStart:function () {
  6117. if ( this._cachedStartElement ) {
  6118. return this._cachedStartElement;
  6119. }
  6120. var range = browser.ie9below ? this.getIERange() : this.getRange(),
  6121. tmpRange,
  6122. start, tmp, parent;
  6123. if ( browser.ie9below ) {
  6124. if ( !range ) {
  6125. //todo 给第一个值可能会有问题
  6126. return this.document.body.firstChild;
  6127. }
  6128. //control元素
  6129. if ( range.item ){
  6130. return range.item( 0 );
  6131. }
  6132. tmpRange = range.duplicate();
  6133. //修正ie下<b>x</b>[xx] 闭合后 <b>x|</b>xx
  6134. tmpRange.text.length > 0 && tmpRange.moveStart( 'character', 1 );
  6135. tmpRange.collapse( 1 );
  6136. start = tmpRange.parentElement();
  6137. parent = tmp = range.parentElement();
  6138. while ( tmp = tmp.parentNode ) {
  6139. if ( tmp == start ) {
  6140. start = parent;
  6141. break;
  6142. }
  6143. }
  6144. } else {
  6145. range.shrinkBoundary();
  6146. start = range.startContainer;
  6147. if ( start.nodeType == 1 && start.hasChildNodes() ){
  6148. start = start.childNodes[Math.min( start.childNodes.length - 1, range.startOffset )];
  6149. }
  6150. if ( start.nodeType == 3 ){
  6151. return start.parentNode;
  6152. }
  6153. }
  6154. return start;
  6155. },
  6156. /**
  6157. * 得到选区中的文本
  6158. * @method getText
  6159. * @return { String } 选区中包含的文本
  6160. * @example
  6161. * ```javascript
  6162. * editor.selection.getText();
  6163. * ```
  6164. */
  6165. getText:function () {
  6166. var nativeSel, nativeRange;
  6167. if ( this.isFocus() && (nativeSel = this.getNative()) ) {
  6168. nativeRange = browser.ie9below ? nativeSel.createRange() : nativeSel.getRangeAt( 0 );
  6169. return browser.ie9below ? nativeRange.text : nativeRange.toString();
  6170. }
  6171. return '';
  6172. },
  6173. /**
  6174. * 清除选区
  6175. * @method clearRange
  6176. * @example
  6177. * ```javascript
  6178. * editor.selection.clearRange();
  6179. * ```
  6180. */
  6181. clearRange : function(){
  6182. this.getNative()[browser.ie9below ? 'empty' : 'removeAllRanges']();
  6183. }
  6184. };
  6185. })();
  6186. // core/Editor.js
  6187. /**
  6188. * 编辑器主类,包含编辑器提供的大部分公用接口
  6189. * @file
  6190. * @module UE
  6191. * @class Editor
  6192. * @since 1.2.6.1
  6193. */
  6194. /**
  6195. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  6196. * @unfile
  6197. * @module UE
  6198. */
  6199. /**
  6200. * UEditor的核心类,为用户提供与编辑器交互的接口。
  6201. * @unfile
  6202. * @module UE
  6203. * @class Editor
  6204. */
  6205. (function () {
  6206. var uid = 0, _selectionChangeTimer;
  6207. /**
  6208. * 获取编辑器的html内容,赋值到编辑器所在表单的textarea文本域里面
  6209. * @private
  6210. * @method setValue
  6211. * @param { UE.Editor } editor 编辑器事例
  6212. */
  6213. function setValue(form, editor) {
  6214. var textarea;
  6215. if (editor.textarea) {
  6216. if (utils.isString(editor.textarea)) {
  6217. for (var i = 0, ti, tis = domUtils.getElementsByTagName(form, 'textarea'); ti = tis[i++];) {
  6218. if (ti.id == 'ueditor_textarea_' + editor.options.textarea) {
  6219. textarea = ti;
  6220. break;
  6221. }
  6222. }
  6223. } else {
  6224. textarea = editor.textarea;
  6225. }
  6226. }
  6227. if (!textarea) {
  6228. form.appendChild(textarea = domUtils.createElement(document, 'textarea', {
  6229. 'name': editor.options.textarea,
  6230. 'id': 'ueditor_textarea_' + editor.options.textarea,
  6231. 'style': "display:none"
  6232. }));
  6233. //不要产生多个textarea
  6234. editor.textarea = textarea;
  6235. }
  6236. !textarea.getAttribute('name') && textarea.setAttribute('name', editor.options.textarea );
  6237. textarea.value = editor.hasContents() ?
  6238. (editor.options.allHtmlEnabled ? editor.getAllHtml() : editor.getContent(null, null, true)) :
  6239. ''
  6240. }
  6241. function loadPlugins(me){
  6242. //初始化插件
  6243. for (var pi in UE.plugins) {
  6244. UE.plugins[pi].call(me);
  6245. }
  6246. }
  6247. function checkCurLang(I18N){
  6248. for(var lang in I18N){
  6249. return lang
  6250. }
  6251. }
  6252. function langReadied(me){
  6253. me.langIsReady = true;
  6254. me.fireEvent("langReady");
  6255. }
  6256. /**
  6257. * 编辑器准备就绪后会触发该事件
  6258. * @module UE
  6259. * @class Editor
  6260. * @event ready
  6261. * @remind render方法执行完成之后,会触发该事件
  6262. * @remind
  6263. * @example
  6264. * ```javascript
  6265. * editor.addListener( 'ready', function( editor ) {
  6266. * editor.execCommand( 'focus' ); //编辑器家在完成后,让编辑器拿到焦点
  6267. * } );
  6268. * ```
  6269. */
  6270. /**
  6271. * 执行destroy方法,会触发该事件
  6272. * @module UE
  6273. * @class Editor
  6274. * @event destroy
  6275. * @see UE.Editor:destroy()
  6276. */
  6277. /**
  6278. * 执行reset方法,会触发该事件
  6279. * @module UE
  6280. * @class Editor
  6281. * @event reset
  6282. * @see UE.Editor:reset()
  6283. */
  6284. /**
  6285. * 执行focus方法,会触发该事件
  6286. * @module UE
  6287. * @class Editor
  6288. * @event focus
  6289. * @see UE.Editor:focus(Boolean)
  6290. */
  6291. /**
  6292. * 语言加载完成会触发该事件
  6293. * @module UE
  6294. * @class Editor
  6295. * @event langReady
  6296. */
  6297. /**
  6298. * 运行命令之后会触发该命令
  6299. * @module UE
  6300. * @class Editor
  6301. * @event beforeExecCommand
  6302. */
  6303. /**
  6304. * 运行命令之后会触发该命令
  6305. * @module UE
  6306. * @class Editor
  6307. * @event afterExecCommand
  6308. */
  6309. /**
  6310. * 运行命令之前会触发该命令
  6311. * @module UE
  6312. * @class Editor
  6313. * @event firstBeforeExecCommand
  6314. */
  6315. /**
  6316. * 在getContent方法执行之前会触发该事件
  6317. * @module UE
  6318. * @class Editor
  6319. * @event beforeGetContent
  6320. * @see UE.Editor:getContent()
  6321. */
  6322. /**
  6323. * 在getContent方法执行之后会触发该事件
  6324. * @module UE
  6325. * @class Editor
  6326. * @event afterGetContent
  6327. * @see UE.Editor:getContent()
  6328. */
  6329. /**
  6330. * 在getAllHtml方法执行时会触发该事件
  6331. * @module UE
  6332. * @class Editor
  6333. * @event getAllHtml
  6334. * @see UE.Editor:getAllHtml()
  6335. */
  6336. /**
  6337. * 在setContent方法执行之前会触发该事件
  6338. * @module UE
  6339. * @class Editor
  6340. * @event beforeSetContent
  6341. * @see UE.Editor:setContent(String)
  6342. */
  6343. /**
  6344. * 在setContent方法执行之后会触发该事件
  6345. * @module UE
  6346. * @class Editor
  6347. * @event afterSetContent
  6348. * @see UE.Editor:setContent(String)
  6349. */
  6350. /**
  6351. * 每当编辑器内部选区发生改变时,将触发该事件
  6352. * @event selectionchange
  6353. * @warning 该事件的触发非常频繁,不建议在该事件的处理过程中做重量级的处理
  6354. * @example
  6355. * ```javascript
  6356. * editor.addListener( 'selectionchange', function( editor ) {
  6357. * console.log('选区发生改变');
  6358. * }
  6359. */
  6360. /**
  6361. * 在所有selectionchange的监听函数执行之前,会触发该事件
  6362. * @module UE
  6363. * @class Editor
  6364. * @event beforeSelectionChange
  6365. * @see UE.Editor:selectionchange
  6366. */
  6367. /**
  6368. * 在所有selectionchange的监听函数执行完之后,会触发该事件
  6369. * @module UE
  6370. * @class Editor
  6371. * @event afterSelectionChange
  6372. * @see UE.Editor:selectionchange
  6373. */
  6374. /**
  6375. * 编辑器内容发生改变时会触发该事件
  6376. * @module UE
  6377. * @class Editor
  6378. * @event contentChange
  6379. */
  6380. /**
  6381. * 以默认参数构建一个编辑器实例
  6382. * @constructor
  6383. * @remind 通过 改构造方法实例化的编辑器,不带ui层.需要render到一个容器,编辑器实例才能正常渲染到页面
  6384. * @example
  6385. * ```javascript
  6386. * var editor = new UE.Editor();
  6387. * editor.execCommand('blod');
  6388. * ```
  6389. * @see UE.Config
  6390. */
  6391. /**
  6392. * 以给定的参数集合创建一个编辑器实例,对于未指定的参数,将应用默认参数。
  6393. * @constructor
  6394. * @remind 通过 改构造方法实例化的编辑器,不带ui层.需要render到一个容器,编辑器实例才能正常渲染到页面
  6395. * @param { Object } setting 创建编辑器的参数
  6396. * @example
  6397. * ```javascript
  6398. * var editor = new UE.Editor();
  6399. * editor.execCommand('blod');
  6400. * ```
  6401. * @see UE.Config
  6402. */
  6403. var Editor = UE.Editor = function (options) {
  6404. var me = this;
  6405. me.uid = uid++;
  6406. EventBase.call(me);
  6407. me.commands = {};
  6408. me.options = utils.extend(utils.clone(options || {}), UEDITOR_CONFIG, true);
  6409. me.shortcutkeys = {};
  6410. me.inputRules = [];
  6411. me.outputRules = [];
  6412. //设置默认的常用属性
  6413. me.setOpt(Editor.defaultOptions(me));
  6414. /* 尝试异步加载后台配置 */
  6415. me.loadServerConfig();
  6416. if(!utils.isEmptyObject(UE.I18N)){
  6417. //修改默认的语言类型
  6418. me.options.lang = checkCurLang(UE.I18N);
  6419. UE.plugin.load(me);
  6420. langReadied(me);
  6421. }else{
  6422. utils.loadFile(document, {
  6423. src: me.options.langPath + me.options.lang + "/" + me.options.lang + ".js",
  6424. tag: "script",
  6425. type: "text/javascript",
  6426. defer: "defer"
  6427. }, function () {
  6428. UE.plugin.load(me);
  6429. langReadied(me);
  6430. });
  6431. }
  6432. UE.instants['ueditorInstant' + me.uid] = me;
  6433. };
  6434. Editor.prototype = {
  6435. registerCommand : function(name,obj){
  6436. this.commands[name] = obj;
  6437. },
  6438. /**
  6439. * 编辑器对外提供的监听ready事件的接口, 通过调用该方法,达到的效果与监听ready事件是一致的
  6440. * @method ready
  6441. * @param { Function } fn 编辑器ready之后所执行的回调, 如果在注册事件之前编辑器已经ready,将会
  6442. * 立即触发该回调。
  6443. * @remind 需要等待编辑器加载完成后才能执行的代码,可以使用该方法传入
  6444. * @example
  6445. * ```javascript
  6446. * editor.ready( function( editor ) {
  6447. * editor.setContent('初始化完毕');
  6448. * } );
  6449. * ```
  6450. * @see UE.Editor.event:ready
  6451. */
  6452. ready: function (fn) {
  6453. var me = this;
  6454. if (fn) {
  6455. me.isReady ? fn.apply(me) : me.addListener('ready', fn);
  6456. }
  6457. },
  6458. /**
  6459. * 该方法是提供给插件里面使用,设置配置项默认值
  6460. * @method setOpt
  6461. * @warning 三处设置配置项的优先级: 实例化时传入参数 > setOpt()设置 > config文件里设置
  6462. * @warning 该方法仅供编辑器插件内部和编辑器初始化时调用,其他地方不能调用。
  6463. * @param { String } key 编辑器的可接受的选项名称
  6464. * @param { * } val 该选项可接受的值
  6465. * @example
  6466. * ```javascript
  6467. * editor.setOpt( 'initContent', '欢迎使用编辑器' );
  6468. * ```
  6469. */
  6470. /**
  6471. * 该方法是提供给插件里面使用,以{key:value}集合的方式设置插件内用到的配置项默认值
  6472. * @method setOpt
  6473. * @warning 三处设置配置项的优先级: 实例化时传入参数 > setOpt()设置 > config文件里设置
  6474. * @warning 该方法仅供编辑器插件内部和编辑器初始化时调用,其他地方不能调用。
  6475. * @param { Object } options 将要设置的选项的键值对对象
  6476. * @example
  6477. * ```javascript
  6478. * editor.setOpt( {
  6479. * 'initContent': '欢迎使用编辑器'
  6480. * } );
  6481. * ```
  6482. */
  6483. setOpt: function (key, val) {
  6484. var obj = {};
  6485. if (utils.isString(key)) {
  6486. obj[key] = val
  6487. } else {
  6488. obj = key;
  6489. }
  6490. utils.extend(this.options, obj, true);
  6491. },
  6492. getOpt:function(key){
  6493. return this.options[key]
  6494. },
  6495. /**
  6496. * 销毁编辑器实例,使用textarea代替
  6497. * @method destroy
  6498. * @example
  6499. * ```javascript
  6500. * editor.destroy();
  6501. * ```
  6502. */
  6503. destroy: function () {
  6504. var me = this;
  6505. me.fireEvent('destroy');
  6506. var container = me.container.parentNode;
  6507. var textarea = me.textarea;
  6508. if (!textarea) {
  6509. textarea = document.createElement('textarea');
  6510. container.parentNode.insertBefore(textarea, container);
  6511. } else {
  6512. textarea.style.display = ''
  6513. }
  6514. textarea.style.width = me.iframe.offsetWidth + 'px';
  6515. textarea.style.height = me.iframe.offsetHeight + 'px';
  6516. textarea.value = me.getContent();
  6517. textarea.id = me.key;
  6518. container.innerHTML = '';
  6519. domUtils.remove(container);
  6520. var key = me.key;
  6521. //trace:2004
  6522. for (var p in me) {
  6523. if (me.hasOwnProperty(p)) {
  6524. delete this[p];
  6525. }
  6526. }
  6527. UE.delEditor(key);
  6528. },
  6529. /**
  6530. * 渲染编辑器的DOM到指定容器
  6531. * @method render
  6532. * @param { String } containerId 指定一个容器ID
  6533. * @remind 执行该方法,会触发ready事件
  6534. * @warning 必须且只能调用一次
  6535. */
  6536. /**
  6537. * 渲染编辑器的DOM到指定容器
  6538. * @method render
  6539. * @param { Element } containerDom 直接指定容器对象
  6540. * @remind 执行该方法,会触发ready事件
  6541. * @warning 必须且只能调用一次
  6542. */
  6543. render: function (container) {
  6544. var me = this,
  6545. options = me.options,
  6546. getStyleValue=function(attr){
  6547. return parseInt(domUtils.getComputedStyle(container,attr));
  6548. };
  6549. if (utils.isString(container)) {
  6550. container = document.getElementById(container);
  6551. }
  6552. if (container) {
  6553. if(options.initialFrameWidth){
  6554. options.minFrameWidth = options.initialFrameWidth
  6555. }else{
  6556. options.minFrameWidth = options.initialFrameWidth = container.offsetWidth;
  6557. }
  6558. if(options.initialFrameHeight){
  6559. options.minFrameHeight = options.initialFrameHeight
  6560. }else{
  6561. options.initialFrameHeight = options.minFrameHeight = container.offsetHeight;
  6562. }
  6563. container.style.width = /%$/.test(options.initialFrameWidth) ? '100%' : options.initialFrameWidth-
  6564. getStyleValue("padding-left")- getStyleValue("padding-right") +'px';
  6565. container.style.height = /%$/.test(options.initialFrameHeight) ? '100%' : options.initialFrameHeight -
  6566. getStyleValue("padding-top")- getStyleValue("padding-bottom") +'px';
  6567. container.style.zIndex = options.zIndex;
  6568. var html = ( ie && browser.version < 9 ? '' : '<!DOCTYPE html>') +
  6569. '<html xmlns=\'http://www.w3.org/1999/xhtml\' class=\'view\' ><head>' +
  6570. '<style type=\'text/css\'>' +
  6571. //设置四周的留边
  6572. '.view{padding:0;word-wrap:break-word;cursor:text;height:90%;}\n' +
  6573. //设置默认字体和字号
  6574. //font-family不能呢随便改,在safari下fillchar会有解析问题
  6575. 'body{margin:8px;font-family:sans-serif;font-size:16px;}' +
  6576. //设置段落间距
  6577. 'p{margin:5px 0;}</style>' +
  6578. ( options.iframeCssUrl ? '<link rel=\'stylesheet\' type=\'text/css\' href=\'' + utils.unhtml(options.iframeCssUrl) + '\'/>' : '' ) +
  6579. (options.initialStyle ? '<style>' + options.initialStyle + '</style>' : '') +
  6580. '</head><body class=\'view\' ></body>' +
  6581. '<script type=\'text/javascript\' ' + (ie ? 'defer=\'defer\'' : '' ) +' id=\'_initialScript\'>' +
  6582. 'setTimeout(function(){editor = window.parent.UE.instants[\'ueditorInstant' + me.uid + '\'];editor._setup(document);},0);' +
  6583. 'var _tmpScript = document.getElementById(\'_initialScript\');_tmpScript.parentNode.removeChild(_tmpScript);</script></html>';
  6584. container.appendChild(domUtils.createElement(document, 'iframe', {
  6585. id: 'ueditor_' + me.uid,
  6586. width: "100%",
  6587. height: "100%",
  6588. frameborder: "0",
  6589. //先注释掉了,加的原因忘记了,但开启会直接导致全屏模式下内容多时不会出现滚动条
  6590. // scrolling :'no',
  6591. src: 'javascript:void(function(){document.open();' + (options.customDomain && document.domain != location.hostname ? 'document.domain="' + document.domain + '";' : '') +
  6592. 'document.write("' + html + '");document.close();}())'
  6593. }));
  6594. container.style.overflow = 'hidden';
  6595. //解决如果是给定的百分比,会导致高度算不对的问题
  6596. setTimeout(function(){
  6597. if( /%$/.test(options.initialFrameWidth)){
  6598. options.minFrameWidth = options.initialFrameWidth = container.offsetWidth;
  6599. //如果这里给定宽度,会导致ie在拖动窗口大小时,编辑区域不随着变化
  6600. // container.style.width = options.initialFrameWidth + 'px';
  6601. }
  6602. if(/%$/.test(options.initialFrameHeight)){
  6603. options.minFrameHeight = options.initialFrameHeight = container.offsetHeight;
  6604. container.style.height = options.initialFrameHeight + 'px';
  6605. }
  6606. })
  6607. }
  6608. },
  6609. /**
  6610. * 编辑器初始化
  6611. * @method _setup
  6612. * @private
  6613. * @param { Element } doc 编辑器Iframe中的文档对象
  6614. */
  6615. _setup: function (doc) {
  6616. var me = this,
  6617. options = me.options;
  6618. if (ie) {
  6619. doc.body.disabled = true;
  6620. doc.body.contentEditable = true;
  6621. doc.body.disabled = false;
  6622. } else {
  6623. doc.body.contentEditable = true;
  6624. }
  6625. doc.body.spellcheck = false;
  6626. me.document = doc;
  6627. me.window = doc.defaultView || doc.parentWindow;
  6628. me.iframe = me.window.frameElement;
  6629. me.body = doc.body;
  6630. me.selection = new dom.Selection(doc);
  6631. //gecko初始化就能得到range,无法判断isFocus了
  6632. var geckoSel;
  6633. if (browser.gecko && (geckoSel = this.selection.getNative())) {
  6634. geckoSel.removeAllRanges();
  6635. }
  6636. this._initEvents();
  6637. //为form提交提供一个隐藏的textarea
  6638. for (var form = this.iframe.parentNode; !domUtils.isBody(form); form = form.parentNode) {
  6639. if (form.tagName == 'FORM') {
  6640. me.form = form;
  6641. if(me.options.autoSyncData){
  6642. domUtils.on(me.window,'blur',function(){
  6643. setValue(form,me);
  6644. });
  6645. }else{
  6646. domUtils.on(form, 'submit', function () {
  6647. setValue(this, me);
  6648. });
  6649. }
  6650. break;
  6651. }
  6652. }
  6653. if (options.initialContent) {
  6654. if (options.autoClearinitialContent) {
  6655. var oldExecCommand = me.execCommand;
  6656. me.execCommand = function () {
  6657. me.fireEvent('firstBeforeExecCommand');
  6658. return oldExecCommand.apply(me, arguments);
  6659. };
  6660. this._setDefaultContent(options.initialContent);
  6661. } else
  6662. this.setContent(options.initialContent, false, true);
  6663. }
  6664. //编辑器不能为空内容
  6665. if (domUtils.isEmptyNode(me.body)) {
  6666. me.body.innerHTML = '<p>' + (browser.ie ? '' : '<br/>') + '</p>';
  6667. }
  6668. //如果要求focus, 就把光标定位到内容开始
  6669. if (options.focus) {
  6670. setTimeout(function () {
  6671. me.focus(me.options.focusInEnd);
  6672. //如果自动清除开着,就不需要做selectionchange;
  6673. !me.options.autoClearinitialContent && me._selectionChange();
  6674. }, 0);
  6675. }
  6676. if (!me.container) {
  6677. me.container = this.iframe.parentNode;
  6678. }
  6679. if (options.fullscreen && me.ui) {
  6680. me.ui.setFullScreen(true);
  6681. }
  6682. try {
  6683. me.document.execCommand('2D-position', false, false);
  6684. } catch (e) {
  6685. }
  6686. try {
  6687. me.document.execCommand('enableInlineTableEditing', false, false);
  6688. } catch (e) {
  6689. }
  6690. try {
  6691. me.document.execCommand('enableObjectResizing', false, false);
  6692. } catch (e) {
  6693. }
  6694. //挂接快捷键
  6695. me._bindshortcutKeys();
  6696. me.isReady = 1;
  6697. me.fireEvent('ready');
  6698. options.onready && options.onready.call(me);
  6699. if (!browser.ie9below) {
  6700. domUtils.on(me.window, ['blur', 'focus'], function (e) {
  6701. //chrome下会出现alt+tab切换时,导致选区位置不对
  6702. if (e.type == 'blur') {
  6703. me._bakRange = me.selection.getRange();
  6704. try {
  6705. me._bakNativeRange = me.selection.getNative().getRangeAt(0);
  6706. me.selection.getNative().removeAllRanges();
  6707. } catch (e) {
  6708. me._bakNativeRange = null;
  6709. }
  6710. } else {
  6711. try {
  6712. me._bakRange && me._bakRange.select();
  6713. } catch (e) {
  6714. }
  6715. }
  6716. });
  6717. }
  6718. //trace:1518 ff3.6body不够寛,会导致点击空白处无法获得焦点
  6719. if (browser.gecko && browser.version <= 10902) {
  6720. //修复ff3.6初始化进来,不能点击获得焦点
  6721. me.body.contentEditable = false;
  6722. setTimeout(function () {
  6723. me.body.contentEditable = true;
  6724. }, 100);
  6725. setInterval(function () {
  6726. me.body.style.height = me.iframe.offsetHeight - 20 + 'px'
  6727. }, 100)
  6728. }
  6729. !options.isShow && me.setHide();
  6730. options.readonly && me.setDisabled();
  6731. },
  6732. /**
  6733. * 同步数据到编辑器所在的form
  6734. * 从编辑器的容器节点向上查找form元素,若找到,就同步编辑内容到找到的form里,为提交数据做准备,主要用于是手动提交的情况
  6735. * 后台取得数据的键值,使用你容器上的name属性,如果没有就使用参数里的textarea项
  6736. * @method sync
  6737. * @example
  6738. * ```javascript
  6739. * editor.sync();
  6740. * form.sumbit(); //form变量已经指向了form元素
  6741. * ```
  6742. */
  6743. /**
  6744. * 根据传入的formId,在页面上查找要同步数据的表单,若找到,就同步编辑内容到找到的form里,为提交数据做准备
  6745. * 后台取得数据的键值,该键值默认使用给定的编辑器容器的name属性,如果没有name属性则使用参数项里给定的“textarea”项
  6746. * @method sync
  6747. * @param { String } formID 指定一个要同步数据的form的id,编辑器的数据会同步到你指定form下
  6748. */
  6749. sync: function (formId) {
  6750. var me = this,
  6751. form = formId ? document.getElementById(formId) :
  6752. domUtils.findParent(me.iframe.parentNode, function (node) {
  6753. return node.tagName == 'FORM'
  6754. }, true);
  6755. form && setValue(form, me);
  6756. },
  6757. /**
  6758. * 设置编辑器高度
  6759. * @method setHeight
  6760. * @remind 当配置项autoHeightEnabled为真时,该方法无效
  6761. * @param { Number } number 设置的高度值,纯数值,不带单位
  6762. * @example
  6763. * ```javascript
  6764. * editor.setHeight(number);
  6765. * ```
  6766. */
  6767. setHeight: function (height,notSetHeight) {
  6768. if (height !== parseInt(this.iframe.parentNode.style.height)) {
  6769. this.iframe.parentNode.style.height = height + 'px';
  6770. }
  6771. !notSetHeight && (this.options.minFrameHeight = this.options.initialFrameHeight = height);
  6772. this.body.style.height = height + 'px';
  6773. !notSetHeight && this.trigger('setHeight')
  6774. },
  6775. /**
  6776. * 为编辑器的编辑命令提供快捷键
  6777. * 这个接口是为插件扩展提供的接口,主要是为新添加的插件,如果需要添加快捷键,所提供的接口
  6778. * @method addshortcutkey
  6779. * @param { Object } keyset 命令名和快捷键键值对对象,多个按钮的快捷键用“+”分隔
  6780. * @example
  6781. * ```javascript
  6782. * editor.addshortcutkey({
  6783. * "Bold" : "ctrl+66",//^B
  6784. * "Italic" : "ctrl+73", //^I
  6785. * });
  6786. * ```
  6787. */
  6788. /**
  6789. * 这个接口是为插件扩展提供的接口,主要是为新添加的插件,如果需要添加快捷键,所提供的接口
  6790. * @method addshortcutkey
  6791. * @param { String } cmd 触发快捷键时,响应的命令
  6792. * @param { String } keys 快捷键的字符串,多个按钮用“+”分隔
  6793. * @example
  6794. * ```javascript
  6795. * editor.addshortcutkey("Underline", "ctrl+85"); //^U
  6796. * ```
  6797. */
  6798. addshortcutkey: function (cmd, keys) {
  6799. var obj = {};
  6800. if (keys) {
  6801. obj[cmd] = keys
  6802. } else {
  6803. obj = cmd;
  6804. }
  6805. utils.extend(this.shortcutkeys, obj)
  6806. },
  6807. /**
  6808. * 对编辑器设置keydown事件监听,绑定快捷键和命令,当快捷键组合触发成功,会响应对应的命令
  6809. * @method _bindshortcutKeys
  6810. * @private
  6811. */
  6812. _bindshortcutKeys: function () {
  6813. var me = this, shortcutkeys = this.shortcutkeys;
  6814. me.addListener('keydown', function (type, e) {
  6815. var keyCode = e.keyCode || e.which;
  6816. for (var i in shortcutkeys) {
  6817. var tmp = shortcutkeys[i].split(',');
  6818. for (var t = 0, ti; ti = tmp[t++];) {
  6819. ti = ti.split(':');
  6820. var key = ti[0], param = ti[1];
  6821. if (/^(ctrl)(\+shift)?\+(\d+)$/.test(key.toLowerCase()) || /^(\d+)$/.test(key)) {
  6822. if (( (RegExp.$1 == 'ctrl' ? (e.ctrlKey || e.metaKey) : 0)
  6823. && (RegExp.$2 != "" ? e[RegExp.$2.slice(1) + "Key"] : 1)
  6824. && keyCode == RegExp.$3
  6825. ) ||
  6826. keyCode == RegExp.$1
  6827. ) {
  6828. if (me.queryCommandState(i,param) != -1)
  6829. me.execCommand(i, param);
  6830. domUtils.preventDefault(e);
  6831. }
  6832. }
  6833. }
  6834. }
  6835. });
  6836. },
  6837. /**
  6838. * 获取编辑器的内容
  6839. * @method getContent
  6840. * @warning 该方法获取到的是经过编辑器内置的过滤规则进行过滤后得到的内容
  6841. * @return { String } 编辑器的内容字符串, 如果编辑器的内容为空,或者是空的标签内容(如:”&lt;p&gt;&lt;br/&gt;&lt;/p&gt;“), 则返回空字符串
  6842. * @example
  6843. * ```javascript
  6844. * //编辑器html内容:<p>1<strong>2<em>34</em>5</strong>6</p>
  6845. * var content = editor.getContent(); //返回值:<p>1<strong>2<em>34</em>5</strong>6</p>
  6846. * ```
  6847. */
  6848. /**
  6849. * 获取编辑器的内容。 可以通过参数定义编辑器内置的判空规则
  6850. * @method getContent
  6851. * @param { Function } fn 自定的判空规则, 要求该方法返回一个boolean类型的值,
  6852. * 代表当前编辑器的内容是否空,
  6853. * 如果返回true, 则该方法将直接返回空字符串;如果返回false,则编辑器将返回
  6854. * 经过内置过滤规则处理后的内容。
  6855. * @remind 该方法在处理包含有初始化内容的时候能起到很好的作用。
  6856. * @warning 该方法获取到的是经过编辑器内置的过滤规则进行过滤后得到的内容
  6857. * @return { String } 编辑器的内容字符串
  6858. * @example
  6859. * ```javascript
  6860. * // editor 是一个编辑器的实例
  6861. * var content = editor.getContent( function ( editor ) {
  6862. * return editor.body.innerHTML === '欢迎使用UEditor'; //返回空字符串
  6863. * } );
  6864. * ```
  6865. */
  6866. getContent: function (cmd, fn,notSetCursor,ignoreBlank,formatter) {
  6867. var me = this;
  6868. if (cmd && utils.isFunction(cmd)) {
  6869. fn = cmd;
  6870. cmd = '';
  6871. }
  6872. if (fn ? !fn() : !this.hasContents()) {
  6873. return '';
  6874. }
  6875. me.fireEvent('beforegetcontent');
  6876. var root = UE.htmlparser(me.body.innerHTML,ignoreBlank);
  6877. me.filterOutputRule(root);
  6878. me.fireEvent('aftergetcontent', cmd,root);
  6879. return root.toHtml(formatter);
  6880. },
  6881. /**
  6882. * 取得完整的html代码,可以直接显示成完整的html文档
  6883. * @method getAllHtml
  6884. * @return { String } 编辑器的内容html文档字符串
  6885. * @eaxmple
  6886. * ```javascript
  6887. * editor.getAllHtml(); //返回格式大致是: <html><head>...</head><body>...</body></html>
  6888. * ```
  6889. */
  6890. getAllHtml: function () {
  6891. var me = this,
  6892. headHtml = [],
  6893. html = '';
  6894. me.fireEvent('getAllHtml', headHtml);
  6895. if (browser.ie && browser.version > 8) {
  6896. var headHtmlForIE9 = '';
  6897. utils.each(me.document.styleSheets, function (si) {
  6898. headHtmlForIE9 += ( si.href ? '<link rel="stylesheet" type="text/css" href="' + si.href + '" />' : '<style>' + si.cssText + '</style>');
  6899. });
  6900. utils.each(me.document.getElementsByTagName('script'), function (si) {
  6901. headHtmlForIE9 += si.outerHTML;
  6902. });
  6903. }
  6904. return '<html><head>' + (me.options.charset ? '<meta http-equiv="Content-Type" content="text/html; charset=' + me.options.charset + '"/>' : '')
  6905. + (headHtmlForIE9 || me.document.getElementsByTagName('head')[0].innerHTML) + headHtml.join('\n') + '</head>'
  6906. + '<body ' + (ie && browser.version < 9 ? 'class="view"' : '') + '>' + me.getContent(null, null, true) + '</body></html>';
  6907. },
  6908. /**
  6909. * 得到编辑器的纯文本内容,但会保留段落格式
  6910. * @method getPlainTxt
  6911. * @return { String } 编辑器带段落格式的纯文本内容字符串
  6912. * @example
  6913. * ```javascript
  6914. * //编辑器html内容:<p><strong>1</strong></p><p><strong>2</strong></p>
  6915. * console.log(editor.getPlainTxt()); //输出:"1\n2\n
  6916. * ```
  6917. */
  6918. getPlainTxt: function () {
  6919. var reg = new RegExp(domUtils.fillChar, 'g'),
  6920. html = this.body.innerHTML.replace(/[\n\r]/g, '');//ie要先去了\n在处理
  6921. html = html.replace(/<(p|div)[^>]*>(<br\/?>|&nbsp;)<\/\1>/gi, '\n')
  6922. .replace(/<br\/?>/gi, '\n')
  6923. .replace(/<[^>/]+>/g, '')
  6924. .replace(/(\n)?<\/([^>]+)>/g, function (a, b, c) {
  6925. return dtd.$block[c] ? '\n' : b ? b : '';
  6926. });
  6927. //取出来的空格会有c2a0会变成乱码,处理这种情况\u00a0
  6928. return html.replace(reg, '').replace(/\u00a0/g, ' ').replace(/&nbsp;/g, ' ');
  6929. },
  6930. /**
  6931. * 获取编辑器中的纯文本内容,没有段落格式
  6932. * @method getContentTxt
  6933. * @return { String } 编辑器不带段落格式的纯文本内容字符串
  6934. * @example
  6935. * ```javascript
  6936. * //编辑器html内容:<p><strong>1</strong></p><p><strong>2</strong></p>
  6937. * console.log(editor.getPlainTxt()); //输出:"12
  6938. * ```
  6939. */
  6940. getContentTxt: function () {
  6941. var reg = new RegExp(domUtils.fillChar, 'g');
  6942. //取出来的空格会有c2a0会变成乱码,处理这种情况\u00a0
  6943. return this.body[browser.ie ? 'innerText' : 'textContent'].replace(reg, '').replace(/\u00a0/g, ' ');
  6944. },
  6945. /**
  6946. * 设置编辑器的内容,可修改编辑器当前的html内容
  6947. * @method setContent
  6948. * @warning 通过该方法插入的内容,是经过编辑器内置的过滤规则进行过滤后得到的内容
  6949. * @warning 该方法会触发selectionchange事件
  6950. * @param { String } html 要插入的html内容
  6951. * @example
  6952. * ```javascript
  6953. * editor.getContent('<p>test</p>');
  6954. * ```
  6955. */
  6956. /**
  6957. * 设置编辑器的内容,可修改编辑器当前的html内容
  6958. * @method setContent
  6959. * @warning 通过该方法插入的内容,是经过编辑器内置的过滤规则进行过滤后得到的内容
  6960. * @warning 该方法会触发selectionchange事件
  6961. * @param { String } html 要插入的html内容
  6962. * @param { Boolean } isAppendTo 若传入true,不清空原来的内容,在最后插入内容,否则,清空内容再插入
  6963. * @example
  6964. * ```javascript
  6965. * //假设设置前的编辑器内容是 <p>old text</p>
  6966. * editor.setContent('<p>new text</p>', true); //插入的结果是<p>old text</p><p>new text</p>
  6967. * ```
  6968. */
  6969. setContent: function (html, isAppendTo, notFireSelectionchange) {
  6970. var me = this;
  6971. me.fireEvent('beforesetcontent', html);
  6972. var root = UE.htmlparser(html);
  6973. me.filterInputRule(root);
  6974. html = root.toHtml();
  6975. me.body.innerHTML = (isAppendTo ? me.body.innerHTML : '') + html;
  6976. function isCdataDiv(node){
  6977. return node.tagName == 'DIV' && node.getAttribute('cdata_tag');
  6978. }
  6979. //给文本或者inline节点套p标签
  6980. if (me.options.enterTag == 'p') {
  6981. var child = this.body.firstChild, tmpNode;
  6982. if (!child || child.nodeType == 1 &&
  6983. (dtd.$cdata[child.tagName] || isCdataDiv(child) ||
  6984. domUtils.isCustomeNode(child)
  6985. )
  6986. && child === this.body.lastChild) {
  6987. this.body.innerHTML = '<p>' + (browser.ie ? '&nbsp;' : '<br/>') + '</p>' + this.body.innerHTML;
  6988. } else {
  6989. var p = me.document.createElement('p');
  6990. while (child) {
  6991. while (child && (child.nodeType == 3 || child.nodeType == 1 && dtd.p[child.tagName] && !dtd.$cdata[child.tagName])) {
  6992. tmpNode = child.nextSibling;
  6993. p.appendChild(child);
  6994. child = tmpNode;
  6995. }
  6996. if (p.firstChild) {
  6997. if (!child) {
  6998. me.body.appendChild(p);
  6999. break;
  7000. } else {
  7001. child.parentNode.insertBefore(p, child);
  7002. p = me.document.createElement('p');
  7003. }
  7004. }
  7005. child = child.nextSibling;
  7006. }
  7007. }
  7008. }
  7009. me.fireEvent('aftersetcontent');
  7010. me.fireEvent('contentchange');
  7011. !notFireSelectionchange && me._selectionChange();
  7012. //清除保存的选区
  7013. me._bakRange = me._bakIERange = me._bakNativeRange = null;
  7014. //trace:1742 setContent后gecko能得到焦点问题
  7015. var geckoSel;
  7016. if (browser.gecko && (geckoSel = this.selection.getNative())) {
  7017. geckoSel.removeAllRanges();
  7018. }
  7019. if(me.options.autoSyncData){
  7020. me.form && setValue(me.form,me);
  7021. }
  7022. },
  7023. /**
  7024. * 让编辑器获得焦点,默认focus到编辑器头部
  7025. * @method focus
  7026. * @example
  7027. * ```javascript
  7028. * editor.focus()
  7029. * ```
  7030. */
  7031. /**
  7032. * 让编辑器获得焦点,toEnd确定focus位置
  7033. * @method focus
  7034. * @param { Boolean } toEnd 默认focus到编辑器头部,toEnd为true时focus到内容尾部
  7035. * @example
  7036. * ```javascript
  7037. * editor.focus(true)
  7038. * ```
  7039. */
  7040. focus: function (toEnd) {
  7041. try {
  7042. var me = this,
  7043. rng = me.selection.getRange();
  7044. if (toEnd) {
  7045. var node = me.body.lastChild;
  7046. if(node && node.nodeType == 1 && !dtd.$empty[node.tagName]){
  7047. if(domUtils.isEmptyBlock(node)){
  7048. rng.setStartAtFirst(node)
  7049. }else{
  7050. rng.setStartAtLast(node)
  7051. }
  7052. rng.collapse(true);
  7053. }
  7054. rng.setCursor(true);
  7055. } else {
  7056. if(!rng.collapsed && domUtils.isBody(rng.startContainer) && rng.startOffset == 0){
  7057. var node = me.body.firstChild;
  7058. if(node && node.nodeType == 1 && !dtd.$empty[node.tagName]){
  7059. rng.setStartAtFirst(node).collapse(true);
  7060. }
  7061. }
  7062. rng.select(true);
  7063. }
  7064. this.fireEvent('focus selectionchange');
  7065. } catch (e) {
  7066. }
  7067. },
  7068. isFocus:function(){
  7069. return this.selection.isFocus();
  7070. },
  7071. blur:function(){
  7072. var sel = this.selection.getNative();
  7073. if(sel.empty && browser.ie){
  7074. var nativeRng = document.body.createTextRange();
  7075. nativeRng.moveToElementText(document.body);
  7076. nativeRng.collapse(true);
  7077. nativeRng.select();
  7078. sel.empty()
  7079. }else{
  7080. sel.removeAllRanges()
  7081. }
  7082. //this.fireEvent('blur selectionchange');
  7083. },
  7084. /**
  7085. * 初始化UE事件及部分事件代理
  7086. * @method _initEvents
  7087. * @private
  7088. */
  7089. _initEvents: function () {
  7090. var me = this,
  7091. doc = me.document,
  7092. win = me.window;
  7093. me._proxyDomEvent = utils.bind(me._proxyDomEvent, me);
  7094. domUtils.on(doc, ['click', 'contextmenu', 'mousedown', 'keydown', 'keyup', 'keypress', 'mouseup', 'mouseover', 'mouseout', 'selectstart'], me._proxyDomEvent);
  7095. domUtils.on(win, ['focus', 'blur'], me._proxyDomEvent);
  7096. domUtils.on(me.body,'drop',function(e){
  7097. //阻止ff下默认的弹出新页面打开图片
  7098. if(browser.gecko && e.stopPropagation) { e.stopPropagation(); }
  7099. me.fireEvent('contentchange')
  7100. });
  7101. domUtils.on(doc, ['mouseup', 'keydown'], function (evt) {
  7102. //特殊键不触发selectionchange
  7103. if (evt.type == 'keydown' && (evt.ctrlKey || evt.metaKey || evt.shiftKey || evt.altKey)) {
  7104. return;
  7105. }
  7106. if (evt.button == 2)return;
  7107. me._selectionChange(250, evt);
  7108. });
  7109. },
  7110. /**
  7111. * 触发事件代理
  7112. * @method _proxyDomEvent
  7113. * @private
  7114. * @return { * } fireEvent的返回值
  7115. * @see UE.EventBase:fireEvent(String)
  7116. */
  7117. _proxyDomEvent: function (evt) {
  7118. if(this.fireEvent('before' + evt.type.replace(/^on/, '').toLowerCase()) === false){
  7119. return false;
  7120. }
  7121. if(this.fireEvent(evt.type.replace(/^on/, ''), evt) === false){
  7122. return false;
  7123. }
  7124. return this.fireEvent('after' + evt.type.replace(/^on/, '').toLowerCase())
  7125. },
  7126. /**
  7127. * 变化选区
  7128. * @method _selectionChange
  7129. * @private
  7130. */
  7131. _selectionChange: function (delay, evt) {
  7132. var me = this;
  7133. //有光标才做selectionchange 为了解决未focus时点击source不能触发更改工具栏状态的问题(source命令notNeedUndo=1)
  7134. // if ( !me.selection.isFocus() ){
  7135. // return;
  7136. // }
  7137. var hackForMouseUp = false;
  7138. var mouseX, mouseY;
  7139. if (browser.ie && browser.version < 9 && evt && evt.type == 'mouseup') {
  7140. var range = this.selection.getRange();
  7141. if (!range.collapsed) {
  7142. hackForMouseUp = true;
  7143. mouseX = evt.clientX;
  7144. mouseY = evt.clientY;
  7145. }
  7146. }
  7147. clearTimeout(_selectionChangeTimer);
  7148. _selectionChangeTimer = setTimeout(function () {
  7149. if (!me.selection || !me.selection.getNative()) {
  7150. return;
  7151. }
  7152. //修复一个IE下的bug: 鼠标点击一段已选择的文本中间时,可能在mouseup后的一段时间内取到的range是在selection的type为None下的错误值.
  7153. //IE下如果用户是拖拽一段已选择文本,则不会触发mouseup事件,所以这里的特殊处理不会对其有影响
  7154. var ieRange;
  7155. if (hackForMouseUp && me.selection.getNative().type == 'None') {
  7156. ieRange = me.document.body.createTextRange();
  7157. try {
  7158. ieRange.moveToPoint(mouseX, mouseY);
  7159. } catch (ex) {
  7160. ieRange = null;
  7161. }
  7162. }
  7163. var bakGetIERange;
  7164. if (ieRange) {
  7165. bakGetIERange = me.selection.getIERange;
  7166. me.selection.getIERange = function () {
  7167. return ieRange;
  7168. };
  7169. }
  7170. me.selection.cache();
  7171. if (bakGetIERange) {
  7172. me.selection.getIERange = bakGetIERange;
  7173. }
  7174. if (me.selection._cachedRange && me.selection._cachedStartElement) {
  7175. me.fireEvent('beforeselectionchange');
  7176. // 第二个参数causeByUi为true代表由用户交互造成的selectionchange.
  7177. me.fireEvent('selectionchange', !!evt);
  7178. me.fireEvent('afterselectionchange');
  7179. me.selection.clear();
  7180. }
  7181. }, delay || 50);
  7182. },
  7183. /**
  7184. * 执行编辑命令
  7185. * @method _callCmdFn
  7186. * @private
  7187. * @param { String } fnName 函数名称
  7188. * @param { * } args 传给命令函数的参数
  7189. * @return { * } 返回命令函数运行的返回值
  7190. */
  7191. _callCmdFn: function (fnName, args) {
  7192. var cmdName = args[0].toLowerCase(),
  7193. cmd, cmdFn;
  7194. cmd = this.commands[cmdName] || UE.commands[cmdName];
  7195. cmdFn = cmd && cmd[fnName];
  7196. //没有querycommandstate或者没有command的都默认返回0
  7197. if ((!cmd || !cmdFn) && fnName == 'queryCommandState') {
  7198. return 0;
  7199. } else if (cmdFn) {
  7200. return cmdFn.apply(this, args);
  7201. }
  7202. },
  7203. /**
  7204. * 执行编辑命令cmdName,完成富文本编辑效果
  7205. * @method execCommand
  7206. * @param { String } cmdName 需要执行的命令
  7207. * @remind 具体命令的使用请参考<a href="#COMMAND.LIST">命令列表</a>
  7208. * @return { * } 返回命令函数运行的返回值
  7209. * @example
  7210. * ```javascript
  7211. * editor.execCommand(cmdName);
  7212. * ```
  7213. */
  7214. execCommand: function (cmdName) {
  7215. cmdName = cmdName.toLowerCase();
  7216. var me = this,
  7217. result,
  7218. cmd = me.commands[cmdName] || UE.commands[cmdName];
  7219. if (!cmd || !cmd.execCommand) {
  7220. return null;
  7221. }
  7222. if (!cmd.notNeedUndo && !me.__hasEnterExecCommand) {
  7223. me.__hasEnterExecCommand = true;
  7224. if (me.queryCommandState.apply(me,arguments) != -1) {
  7225. me.fireEvent('saveScene');
  7226. me.fireEvent.apply(me, ['beforeexeccommand', cmdName].concat(arguments));
  7227. result = this._callCmdFn('execCommand', arguments);
  7228. //保存场景时,做了内容对比,再看是否进行contentchange触发,这里多触发了一次,去掉
  7229. // (!cmd.ignoreContentChange && !me._ignoreContentChange) && me.fireEvent('contentchange');
  7230. me.fireEvent.apply(me, ['afterexeccommand', cmdName].concat(arguments));
  7231. me.fireEvent('saveScene');
  7232. }
  7233. me.__hasEnterExecCommand = false;
  7234. } else {
  7235. result = this._callCmdFn('execCommand', arguments);
  7236. (!me.__hasEnterExecCommand && !cmd.ignoreContentChange && !me._ignoreContentChange) && me.fireEvent('contentchange')
  7237. }
  7238. (!me.__hasEnterExecCommand && !cmd.ignoreContentChange && !me._ignoreContentChange) && me._selectionChange();
  7239. return result;
  7240. },
  7241. /**
  7242. * 根据传入的command命令,查选编辑器当前的选区,返回命令的状态
  7243. * @method queryCommandState
  7244. * @param { String } cmdName 需要查询的命令名称
  7245. * @remind 具体命令的使用请参考<a href="#COMMAND.LIST">命令列表</a>
  7246. * @return { Number } number 返回放前命令的状态,返回值三种情况:(-1|0|1)
  7247. * @example
  7248. * ```javascript
  7249. * editor.queryCommandState(cmdName) => (-1|0|1)
  7250. * ```
  7251. * @see COMMAND.LIST
  7252. */
  7253. queryCommandState: function (cmdName) {
  7254. return this._callCmdFn('queryCommandState', arguments);
  7255. },
  7256. /**
  7257. * 根据传入的command命令,查选编辑器当前的选区,根据命令返回相关的值
  7258. * @method queryCommandValue
  7259. * @param { String } cmdName 需要查询的命令名称
  7260. * @remind 具体命令的使用请参考<a href="#COMMAND.LIST">命令列表</a>
  7261. * @remind 只有部分插件有此方法
  7262. * @return { * } 返回每个命令特定的当前状态值
  7263. * @grammar editor.queryCommandValue(cmdName) => {*}
  7264. * @see COMMAND.LIST
  7265. */
  7266. queryCommandValue: function (cmdName) {
  7267. return this._callCmdFn('queryCommandValue', arguments);
  7268. },
  7269. /**
  7270. * 检查编辑区域中是否有内容
  7271. * @method hasContents
  7272. * @remind 默认有文本内容,或者有以下节点都不认为是空
  7273. * table,ul,ol,dl,iframe,area,base,col,hr,img,embed,input,link,meta,param
  7274. * @return { Boolean } 检查有内容返回true,否则返回false
  7275. * @example
  7276. * ```javascript
  7277. * editor.hasContents()
  7278. * ```
  7279. */
  7280. /**
  7281. * 检查编辑区域中是否有内容,若包含参数tags中的节点类型,直接返回true
  7282. * @method hasContents
  7283. * @param { Array } tags 传入数组判断时用到的节点类型
  7284. * @return { Boolean } 若文档中包含tags数组里对应的tag,返回true,否则返回false
  7285. * @example
  7286. * ```javascript
  7287. * editor.hasContents(['span']);
  7288. * ```
  7289. */
  7290. hasContents: function (tags) {
  7291. if (tags) {
  7292. for (var i = 0, ci; ci = tags[i++];) {
  7293. if (this.document.getElementsByTagName(ci).length > 0) {
  7294. return true;
  7295. }
  7296. }
  7297. }
  7298. if (!domUtils.isEmptyBlock(this.body)) {
  7299. return true
  7300. }
  7301. //随时添加,定义的特殊标签如果存在,不能认为是空
  7302. tags = ['div'];
  7303. for (i = 0; ci = tags[i++];) {
  7304. var nodes = domUtils.getElementsByTagName(this.document, ci);
  7305. for (var n = 0, cn; cn = nodes[n++];) {
  7306. if (domUtils.isCustomeNode(cn)) {
  7307. return true;
  7308. }
  7309. }
  7310. }
  7311. return false;
  7312. },
  7313. /**
  7314. * 重置编辑器,可用来做多个tab使用同一个编辑器实例
  7315. * @method reset
  7316. * @remind 此方法会清空编辑器内容,清空回退列表,会触发reset事件
  7317. * @example
  7318. * ```javascript
  7319. * editor.reset()
  7320. * ```
  7321. */
  7322. reset: function () {
  7323. this.fireEvent('reset');
  7324. },
  7325. /**
  7326. * 设置当前编辑区域可以编辑
  7327. * @method setEnabled
  7328. * @example
  7329. * ```javascript
  7330. * editor.setEnabled()
  7331. * ```
  7332. */
  7333. setEnabled: function () {
  7334. var me = this, range;
  7335. if (me.body.contentEditable == 'false') {
  7336. me.body.contentEditable = true;
  7337. range = me.selection.getRange();
  7338. //有可能内容丢失了
  7339. try {
  7340. range.moveToBookmark(me.lastBk);
  7341. delete me.lastBk
  7342. } catch (e) {
  7343. range.setStartAtFirst(me.body).collapse(true)
  7344. }
  7345. range.select(true);
  7346. if (me.bkqueryCommandState) {
  7347. me.queryCommandState = me.bkqueryCommandState;
  7348. delete me.bkqueryCommandState;
  7349. }
  7350. if (me.bkqueryCommandValue) {
  7351. me.queryCommandValue = me.bkqueryCommandValue;
  7352. delete me.bkqueryCommandValue;
  7353. }
  7354. me.fireEvent('selectionchange');
  7355. }
  7356. },
  7357. enable: function () {
  7358. return this.setEnabled();
  7359. },
  7360. /** 设置当前编辑区域不可编辑
  7361. * @method setDisabled
  7362. */
  7363. /** 设置当前编辑区域不可编辑,except中的命令除外
  7364. * @method setDisabled
  7365. * @param { String } except 例外命令的字符串
  7366. * @remind 即使设置了disable,此处配置的例外命令仍然可以执行
  7367. * @example
  7368. * ```javascript
  7369. * editor.setDisabled('bold'); //禁用工具栏中除加粗之外的所有功能
  7370. * ```
  7371. */
  7372. /** 设置当前编辑区域不可编辑,except中的命令除外
  7373. * @method setDisabled
  7374. * @param { Array } except 例外命令的字符串数组,数组中的命令仍然可以执行
  7375. * @remind 即使设置了disable,此处配置的例外命令仍然可以执行
  7376. * @example
  7377. * ```javascript
  7378. * editor.setDisabled(['bold','insertimage']); //禁用工具栏中除加粗和插入图片之外的所有功能
  7379. * ```
  7380. */
  7381. setDisabled: function (except) {
  7382. var me = this;
  7383. except = except ? utils.isArray(except) ? except : [except] : [];
  7384. if (me.body.contentEditable == 'true') {
  7385. if (!me.lastBk) {
  7386. me.lastBk = me.selection.getRange().createBookmark(true);
  7387. }
  7388. me.body.contentEditable = false;
  7389. me.bkqueryCommandState = me.queryCommandState;
  7390. me.bkqueryCommandValue = me.queryCommandValue;
  7391. me.queryCommandState = function (type) {
  7392. if (utils.indexOf(except, type) != -1) {
  7393. return me.bkqueryCommandState.apply(me, arguments);
  7394. }
  7395. return -1;
  7396. };
  7397. me.queryCommandValue = function (type) {
  7398. if (utils.indexOf(except, type) != -1) {
  7399. return me.bkqueryCommandValue.apply(me, arguments);
  7400. }
  7401. return null;
  7402. };
  7403. me.fireEvent('selectionchange');
  7404. }
  7405. },
  7406. disable: function (except) {
  7407. return this.setDisabled(except);
  7408. },
  7409. /**
  7410. * 设置默认内容
  7411. * @method _setDefaultContent
  7412. * @private
  7413. * @param { String } cont 要存入的内容
  7414. */
  7415. _setDefaultContent: function () {
  7416. function clear() {
  7417. var me = this;
  7418. if (me.document.getElementById('initContent')) {
  7419. me.body.innerHTML = '<p>' + (ie ? '' : '<br/>') + '</p>';
  7420. me.removeListener('firstBeforeExecCommand focus', clear);
  7421. setTimeout(function () {
  7422. me.focus();
  7423. me._selectionChange();
  7424. }, 0)
  7425. }
  7426. }
  7427. return function (cont) {
  7428. var me = this;
  7429. me.body.innerHTML = '<p id="initContent">' + cont + '</p>';
  7430. me.addListener('firstBeforeExecCommand focus', clear);
  7431. }
  7432. }(),
  7433. /**
  7434. * 显示编辑器
  7435. * @method setShow
  7436. * @example
  7437. * ```javascript
  7438. * editor.setShow()
  7439. * ```
  7440. */
  7441. setShow: function () {
  7442. var me = this, range = me.selection.getRange();
  7443. if (me.container.style.display == 'none') {
  7444. //有可能内容丢失了
  7445. try {
  7446. range.moveToBookmark(me.lastBk);
  7447. delete me.lastBk
  7448. } catch (e) {
  7449. range.setStartAtFirst(me.body).collapse(true)
  7450. }
  7451. //ie下focus实效,所以做了个延迟
  7452. setTimeout(function () {
  7453. range.select(true);
  7454. }, 100);
  7455. me.container.style.display = '';
  7456. }
  7457. },
  7458. show: function () {
  7459. return this.setShow();
  7460. },
  7461. /**
  7462. * 隐藏编辑器
  7463. * @method setHide
  7464. * @example
  7465. * ```javascript
  7466. * editor.setHide()
  7467. * ```
  7468. */
  7469. setHide: function () {
  7470. var me = this;
  7471. if (!me.lastBk) {
  7472. me.lastBk = me.selection.getRange().createBookmark(true);
  7473. }
  7474. me.container.style.display = 'none'
  7475. },
  7476. hide: function () {
  7477. return this.setHide();
  7478. },
  7479. /**
  7480. * 根据指定的路径,获取对应的语言资源
  7481. * @method getLang
  7482. * @param { String } path 路径根据的是lang目录下的语言文件的路径结构
  7483. * @return { Object | String } 根据路径返回语言资源的Json格式对象或者语言字符串
  7484. * @example
  7485. * ```javascript
  7486. * editor.getLang('contextMenu.delete'); //如果当前是中文,那返回是的是'删除'
  7487. * ```
  7488. */
  7489. getLang: function (path) {
  7490. var lang = UE.I18N[this.options.lang];
  7491. if (!lang) {
  7492. throw Error("not import language file");
  7493. }
  7494. path = (path || "").split(".");
  7495. for (var i = 0, ci; ci = path[i++];) {
  7496. lang = lang[ci];
  7497. if (!lang)break;
  7498. }
  7499. return lang;
  7500. },
  7501. /**
  7502. * 计算编辑器html内容字符串的长度
  7503. * @method getContentLength
  7504. * @return { Number } 返回计算的长度
  7505. * @example
  7506. * ```javascript
  7507. * //编辑器html内容<p><strong>132</strong></p>
  7508. * editor.getContentLength() //返回27
  7509. * ```
  7510. */
  7511. /**
  7512. * 计算编辑器当前纯文本内容的长度
  7513. * @method getContentLength
  7514. * @param { Boolean } ingoneHtml 传入true时,只按照纯文本来计算
  7515. * @return { Number } 返回计算的长度,内容中有hr/img/iframe标签,长度加1
  7516. * @example
  7517. * ```javascript
  7518. * //编辑器html内容<p><strong>132</strong></p>
  7519. * editor.getContentLength() //返回3
  7520. * ```
  7521. */
  7522. getContentLength: function (ingoneHtml, tagNames) {
  7523. var count = this.getContent(false,false,true).length;
  7524. if (ingoneHtml) {
  7525. tagNames = (tagNames || []).concat([ 'hr', 'img', 'iframe']);
  7526. count = this.getContentTxt().replace(/[\t\r\n]+/g, '').length;
  7527. for (var i = 0, ci; ci = tagNames[i++];) {
  7528. count += this.document.getElementsByTagName(ci).length;
  7529. }
  7530. }
  7531. return count;
  7532. },
  7533. /**
  7534. * 注册输入过滤规则
  7535. * @method addInputRule
  7536. * @param { Function } rule 要添加的过滤规则
  7537. * @example
  7538. * ```javascript
  7539. * editor.addInputRule(function(root){
  7540. * $.each(root.getNodesByTagName('div'),function(i,node){
  7541. * node.tagName="p";
  7542. * });
  7543. * });
  7544. * ```
  7545. */
  7546. addInputRule: function (rule) {
  7547. this.inputRules.push(rule);
  7548. },
  7549. /**
  7550. * 执行注册的过滤规则
  7551. * @method filterInputRule
  7552. * @param { UE.uNode } root 要过滤的uNode节点
  7553. * @remind 执行editor.setContent方法和执行'inserthtml'命令后,会运行该过滤函数
  7554. * @example
  7555. * ```javascript
  7556. * editor.filterInputRule(editor.body);
  7557. * ```
  7558. * @see UE.Editor:addInputRule
  7559. */
  7560. filterInputRule: function (root) {
  7561. for (var i = 0, ci; ci = this.inputRules[i++];) {
  7562. ci.call(this, root)
  7563. }
  7564. },
  7565. /**
  7566. * 注册输出过滤规则
  7567. * @method addOutputRule
  7568. * @param { Function } rule 要添加的过滤规则
  7569. * @example
  7570. * ```javascript
  7571. * editor.addOutputRule(function(root){
  7572. * $.each(root.getNodesByTagName('p'),function(i,node){
  7573. * node.tagName="div";
  7574. * });
  7575. * });
  7576. * ```
  7577. */
  7578. addOutputRule: function (rule) {
  7579. this.outputRules.push(rule)
  7580. },
  7581. /**
  7582. * 根据输出过滤规则,过滤编辑器内容
  7583. * @method filterOutputRule
  7584. * @remind 执行editor.getContent方法的时候,会先运行该过滤函数
  7585. * @param { UE.uNode } root 要过滤的uNode节点
  7586. * @example
  7587. * ```javascript
  7588. * editor.filterOutputRule(editor.body);
  7589. * ```
  7590. * @see UE.Editor:addOutputRule
  7591. */
  7592. filterOutputRule: function (root) {
  7593. for (var i = 0, ci; ci = this.outputRules[i++];) {
  7594. ci.call(this, root)
  7595. }
  7596. },
  7597. /**
  7598. * 根据action名称获取请求的路径
  7599. * @method getActionUrl
  7600. * @remind 假如没有设置serverUrl,会根据imageUrl设置默认的controller路径
  7601. * @param { String } action action名称
  7602. * @example
  7603. * ```javascript
  7604. * editor.getActionUrl('config'); //返回 "/ueditor/php/controller.php?action=config"
  7605. * editor.getActionUrl('image'); //返回 "/ueditor/php/controller.php?action=uplaodimage"
  7606. * editor.getActionUrl('scrawl'); //返回 "/ueditor/php/controller.php?action=uplaodscrawl"
  7607. * editor.getActionUrl('imageManager'); //返回 "/ueditor/php/controller.php?action=listimage"
  7608. * ```
  7609. */
  7610. getActionUrl: function(action){
  7611. var actionName = this.getOpt(action) || action,
  7612. imageUrl = this.getOpt('imageUrl'),
  7613. serverUrl = this.getOpt('serverUrl');
  7614. if(!serverUrl && imageUrl) {
  7615. serverUrl = imageUrl.replace(/^(.*[\/]).+([\.].+)$/, '$1controller$2');
  7616. }
  7617. if(serverUrl) {
  7618. serverUrl = serverUrl + (serverUrl.indexOf('?') == -1 ? '?':'&') + 'action=' + (actionName || '');
  7619. return utils.formatUrl(serverUrl);
  7620. } else {
  7621. return '';
  7622. }
  7623. }
  7624. };
  7625. utils.inherits(Editor, EventBase);
  7626. })();
  7627. // core/Editor.defaultoptions.js
  7628. //维护编辑器一下默认的不在插件中的配置项
  7629. UE.Editor.defaultOptions = function(editor){
  7630. var _url = editor.options.UEDITOR_HOME_URL;
  7631. return {
  7632. isShow: true,
  7633. initialContent: '',
  7634. initialStyle:'',
  7635. autoClearinitialContent: false,
  7636. iframeCssUrl: _url + 'themes/iframe.css',
  7637. textarea: 'editorValue',
  7638. focus: false,
  7639. focusInEnd: true,
  7640. autoClearEmptyNode: true,
  7641. fullscreen: false,
  7642. readonly: false,
  7643. zIndex: 999,
  7644. imagePopup: true,
  7645. enterTag: 'p',
  7646. customDomain: false,
  7647. lang: 'zh-cn',
  7648. langPath: _url + 'lang/',
  7649. theme: 'default',
  7650. themePath: _url + 'themes/',
  7651. allHtmlEnabled: false,
  7652. scaleEnabled: false,
  7653. tableNativeEditInFF: false,
  7654. autoSyncData : true,
  7655. fileNameFormat: '{time}{rand:6}'
  7656. }
  7657. };
  7658. // core/loadconfig.js
  7659. (function(){
  7660. UE.Editor.prototype.loadServerConfig = function(){
  7661. var me = this;
  7662. setTimeout(function(){
  7663. try{
  7664. me.options.imageUrl && me.setOpt('serverUrl', me.options.imageUrl.replace(/^(.*[\/]).+([\.].+)$/, '$1controller$2'));
  7665. var configUrl = me.getActionUrl('config'),
  7666. isJsonp = utils.isCrossDomainUrl(configUrl);
  7667. /* 发出ajax请求 */
  7668. me._serverConfigLoaded = false;
  7669. configUrl && UE.ajax.request(configUrl,{
  7670. 'method': 'GET',
  7671. 'dataType': isJsonp ? 'jsonp':'',
  7672. 'onsuccess':function(r){
  7673. try {
  7674. var config = isJsonp ? r:eval("("+r.responseText+")");
  7675. utils.extend(me.options, config);
  7676. me.fireEvent('serverConfigLoaded');
  7677. me._serverConfigLoaded = true;
  7678. } catch (e) {
  7679. showErrorMsg(me.getLang('loadconfigFormatError'));
  7680. }
  7681. },
  7682. 'onerror':function(){
  7683. showErrorMsg(me.getLang('loadconfigHttpError'));
  7684. }
  7685. });
  7686. } catch(e){
  7687. showErrorMsg(me.getLang('loadconfigError'));
  7688. }
  7689. });
  7690. function showErrorMsg(msg) {
  7691. console && console.error(msg);
  7692. //me.fireEvent('showMessage', {
  7693. // 'title': msg,
  7694. // 'type': 'error'
  7695. //});
  7696. }
  7697. };
  7698. UE.Editor.prototype.isServerConfigLoaded = function(){
  7699. var me = this;
  7700. return me._serverConfigLoaded || false;
  7701. };
  7702. UE.Editor.prototype.afterConfigReady = function(handler){
  7703. if (!handler || !utils.isFunction(handler)) return;
  7704. var me = this;
  7705. var readyHandler = function(){
  7706. handler.apply(me, arguments);
  7707. me.removeListener('serverConfigLoaded', readyHandler);
  7708. };
  7709. if (me.isServerConfigLoaded()) {
  7710. handler.call(me, 'serverConfigLoaded');
  7711. } else {
  7712. me.addListener('serverConfigLoaded', readyHandler);
  7713. }
  7714. };
  7715. })();
  7716. // core/ajax.js
  7717. /**
  7718. * @file
  7719. * @module UE.ajax
  7720. * @since 1.2.6.1
  7721. */
  7722. /**
  7723. * 提供对ajax请求的支持
  7724. * @module UE.ajax
  7725. */
  7726. UE.ajax = function() {
  7727. //创建一个ajaxRequest对象
  7728. var fnStr = 'XMLHttpRequest()';
  7729. try {
  7730. new ActiveXObject("Msxml2.XMLHTTP");
  7731. fnStr = 'ActiveXObject(\'Msxml2.XMLHTTP\')';
  7732. } catch (e) {
  7733. try {
  7734. new ActiveXObject("Microsoft.XMLHTTP");
  7735. fnStr = 'ActiveXObject(\'Microsoft.XMLHTTP\')'
  7736. } catch (e) {
  7737. }
  7738. }
  7739. var creatAjaxRequest = new Function('return new ' + fnStr);
  7740. /**
  7741. * 将json参数转化成适合ajax提交的参数列表
  7742. * @param json
  7743. */
  7744. function json2str(json) {
  7745. var strArr = [];
  7746. for (var i in json) {
  7747. //忽略默认的几个参数
  7748. if(i=="method" || i=="timeout" || i=="async" || i=="dataType" || i=="callback") continue;
  7749. //忽略控制
  7750. if(json[i] == undefined || json[i] == null) continue;
  7751. //传递过来的对象和函数不在提交之列
  7752. if (!((typeof json[i]).toLowerCase() == "function" || (typeof json[i]).toLowerCase() == "object")) {
  7753. strArr.push( encodeURIComponent(i) + "="+encodeURIComponent(json[i]) );
  7754. } else if (utils.isArray(json[i])) {
  7755. //支持传数组内容
  7756. for(var j = 0; j < json[i].length; j++) {
  7757. strArr.push( encodeURIComponent(i) + "[]="+encodeURIComponent(json[i][j]) );
  7758. }
  7759. }
  7760. }
  7761. return strArr.join("&");
  7762. }
  7763. function doAjax(url, ajaxOptions) {
  7764. var xhr = creatAjaxRequest(),
  7765. //是否超时
  7766. timeIsOut = false,
  7767. //默认参数
  7768. defaultAjaxOptions = {
  7769. method:"POST",
  7770. timeout:5000,
  7771. async:true,
  7772. data:{},//需要传递对象的话只能覆盖
  7773. onsuccess:function() {
  7774. },
  7775. onerror:function() {
  7776. }
  7777. };
  7778. if (typeof url === "object") {
  7779. ajaxOptions = url;
  7780. url = ajaxOptions.url;
  7781. }
  7782. if (!xhr || !url) return;
  7783. var ajaxOpts = ajaxOptions ? utils.extend(defaultAjaxOptions,ajaxOptions) : defaultAjaxOptions;
  7784. var submitStr = json2str(ajaxOpts); // { name:"Jim",city:"Beijing" } --> "name=Jim&city=Beijing"
  7785. //如果用户直接通过data参数传递json对象过来,则也要将此json对象转化为字符串
  7786. if (!utils.isEmptyObject(ajaxOpts.data)){
  7787. submitStr += (submitStr? "&":"") + json2str(ajaxOpts.data);
  7788. }
  7789. //超时检测
  7790. var timerID = setTimeout(function() {
  7791. if (xhr.readyState != 4) {
  7792. timeIsOut = true;
  7793. xhr.abort();
  7794. clearTimeout(timerID);
  7795. }
  7796. }, ajaxOpts.timeout);
  7797. var method = ajaxOpts.method.toUpperCase();
  7798. var str = url + (url.indexOf("?")==-1?"?":"&") + (method=="POST"?"":submitStr+ "&noCache=" + +new Date);
  7799. xhr.open(method, str, ajaxOpts.async);
  7800. xhr.onreadystatechange = function() {
  7801. if (xhr.readyState == 4) {
  7802. if (!timeIsOut && xhr.status == 200) {
  7803. ajaxOpts.onsuccess(xhr);
  7804. } else {
  7805. ajaxOpts.onerror(xhr);
  7806. }
  7807. }
  7808. };
  7809. if (method == "POST") {
  7810. xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  7811. xhr.send(submitStr);
  7812. } else {
  7813. xhr.send(null);
  7814. }
  7815. }
  7816. function doJsonp(url, opts) {
  7817. var successhandler = opts.onsuccess || function(){},
  7818. scr = document.createElement('SCRIPT'),
  7819. options = opts || {},
  7820. charset = options['charset'],
  7821. callbackField = options['jsonp'] || 'callback',
  7822. callbackFnName,
  7823. timeOut = options['timeOut'] || 0,
  7824. timer,
  7825. reg = new RegExp('(\\?|&)' + callbackField + '=([^&]*)'),
  7826. matches;
  7827. if (utils.isFunction(successhandler)) {
  7828. callbackFnName = 'bd__editor__' + Math.floor(Math.random() * 2147483648).toString(36);
  7829. window[callbackFnName] = getCallBack(0);
  7830. } else if(utils.isString(successhandler)){
  7831. callbackFnName = successhandler;
  7832. } else {
  7833. if (matches = reg.exec(url)) {
  7834. callbackFnName = matches[2];
  7835. }
  7836. }
  7837. url = url.replace(reg, '\x241' + callbackField + '=' + callbackFnName);
  7838. if (url.search(reg) < 0) {
  7839. url += (url.indexOf('?') < 0 ? '?' : '&') + callbackField + '=' + callbackFnName;
  7840. }
  7841. var queryStr = json2str(opts); // { name:"Jim",city:"Beijing" } --> "name=Jim&city=Beijing"
  7842. //如果用户直接通过data参数传递json对象过来,则也要将此json对象转化为字符串
  7843. if (!utils.isEmptyObject(opts.data)){
  7844. queryStr += (queryStr? "&":"") + json2str(opts.data);
  7845. }
  7846. if (queryStr) {
  7847. url = url.replace(/\?/, '?' + queryStr + '&');
  7848. }
  7849. scr.onerror = getCallBack(1);
  7850. if( timeOut ){
  7851. timer = setTimeout(getCallBack(1), timeOut);
  7852. }
  7853. createScriptTag(scr, url, charset);
  7854. function createScriptTag(scr, url, charset) {
  7855. scr.setAttribute('type', 'text/javascript');
  7856. scr.setAttribute('defer', 'defer');
  7857. charset && scr.setAttribute('charset', charset);
  7858. scr.setAttribute('src', url);
  7859. document.getElementsByTagName('head')[0].appendChild(scr);
  7860. }
  7861. function getCallBack(onTimeOut){
  7862. return function(){
  7863. try {
  7864. if(onTimeOut){
  7865. options.onerror && options.onerror();
  7866. }else{
  7867. try{
  7868. clearTimeout(timer);
  7869. successhandler.apply(window, arguments);
  7870. } catch (e){}
  7871. }
  7872. } catch (exception) {
  7873. options.onerror && options.onerror.call(window, exception);
  7874. } finally {
  7875. options.oncomplete && options.oncomplete.apply(window, arguments);
  7876. scr.parentNode && scr.parentNode.removeChild(scr);
  7877. window[callbackFnName] = null;
  7878. try {
  7879. delete window[callbackFnName];
  7880. }catch(e){}
  7881. }
  7882. }
  7883. }
  7884. }
  7885. return {
  7886. /**
  7887. * 根据给定的参数项,向指定的url发起一个ajax请求。 ajax请求完成后,会根据请求结果调用相应回调: 如果请求
  7888. * 成功, 则调用onsuccess回调, 失败则调用 onerror 回调
  7889. * @method request
  7890. * @param { URLString } url ajax请求的url地址
  7891. * @param { Object } ajaxOptions ajax请求选项的键值对,支持的选项如下:
  7892. * @example
  7893. * ```javascript
  7894. * //向sayhello.php发起一个异步的Ajax GET请求, 请求超时时间为10s, 请求完成后执行相应的回调。
  7895. * UE.ajax.requeset( 'sayhello.php', {
  7896. *
  7897. * //请求方法。可选值: 'GET', 'POST',默认值是'POST'
  7898. * method: 'GET',
  7899. *
  7900. * //超时时间。 默认为5000, 单位是ms
  7901. * timeout: 10000,
  7902. *
  7903. * //是否是异步请求。 true为异步请求, false为同步请求
  7904. * async: true,
  7905. *
  7906. * //请求携带的数据。如果请求为GET请求, data会经过stringify后附加到请求url之后。
  7907. * data: {
  7908. * name: 'ueditor'
  7909. * },
  7910. *
  7911. * //请求成功后的回调, 该回调接受当前的XMLHttpRequest对象作为参数。
  7912. * onsuccess: function ( xhr ) {
  7913. * console.log( xhr.responseText );
  7914. * },
  7915. *
  7916. * //请求失败或者超时后的回调。
  7917. * onerror: function ( xhr ) {
  7918. * alert( 'Ajax请求失败' );
  7919. * }
  7920. *
  7921. * } );
  7922. * ```
  7923. */
  7924. /**
  7925. * 根据给定的参数项发起一个ajax请求, 参数项里必须包含一个url地址。 ajax请求完成后,会根据请求结果调用相应回调: 如果请求
  7926. * 成功, 则调用onsuccess回调, 失败则调用 onerror 回调。
  7927. * @method request
  7928. * @warning 如果在参数项里未提供一个key为“url”的地址值,则该请求将直接退出。
  7929. * @param { Object } ajaxOptions ajax请求选项的键值对,支持的选项如下:
  7930. * @example
  7931. * ```javascript
  7932. *
  7933. * //向sayhello.php发起一个异步的Ajax POST请求, 请求超时时间为5s, 请求完成后不执行任何回调。
  7934. * UE.ajax.requeset( 'sayhello.php', {
  7935. *
  7936. * //请求的地址, 该项是必须的。
  7937. * url: 'sayhello.php'
  7938. *
  7939. * } );
  7940. * ```
  7941. */
  7942. request:function(url, opts) {
  7943. if (opts && opts.dataType == 'jsonp') {
  7944. doJsonp(url, opts);
  7945. } else {
  7946. doAjax(url, opts);
  7947. }
  7948. },
  7949. getJSONP:function(url, data, fn) {
  7950. var opts = {
  7951. 'data': data,
  7952. 'oncomplete': fn
  7953. };
  7954. doJsonp(url, opts);
  7955. }
  7956. };
  7957. }();
  7958. // core/filterword.js
  7959. /**
  7960. * UE过滤word的静态方法
  7961. * @file
  7962. */
  7963. /**
  7964. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  7965. * @module UE
  7966. */
  7967. /**
  7968. * 根据传入html字符串过滤word
  7969. * @module UE
  7970. * @since 1.2.6.1
  7971. * @method filterWord
  7972. * @param { String } html html字符串
  7973. * @return { String } 已过滤后的结果字符串
  7974. * @example
  7975. * ```javascript
  7976. * UE.filterWord(html);
  7977. * ```
  7978. */
  7979. var filterWord = UE.filterWord = function () {
  7980. //是否是word过来的内容
  7981. function isWordDocument( str ) {
  7982. return /(class="?Mso|style="[^"]*\bmso\-|w:WordDocument|<(v|o):|lang=)/ig.test( str );
  7983. }
  7984. //去掉小数
  7985. function transUnit( v ) {
  7986. v = v.replace( /[\d.]+\w+/g, function ( m ) {
  7987. return utils.transUnitToPx(m);
  7988. } );
  7989. return v;
  7990. }
  7991. function filterPasteWord( str ) {
  7992. return str.replace(/[\t\r\n]+/g,' ')
  7993. .replace( /<!--[\s\S]*?-->/ig, "" )
  7994. //转换图片
  7995. .replace(/<v:shape [^>]*>[\s\S]*?.<\/v:shape>/gi,function(str){
  7996. //opera能自己解析出image所这里直接返回空
  7997. if(browser.opera){
  7998. return '';
  7999. }
  8000. try{
  8001. //有可能是bitmap占为图,无用,直接过滤掉,主要体现在粘贴excel表格中
  8002. if(/Bitmap/i.test(str)){
  8003. return '';
  8004. }
  8005. var width = str.match(/width:([ \d.]*p[tx])/i)[1],
  8006. height = str.match(/height:([ \d.]*p[tx])/i)[1],
  8007. src = str.match(/src=\s*"([^"]*)"/i)[1];
  8008. return '<img width="'+ transUnit(width) +'" height="'+transUnit(height) +'" src="' + src + '" />';
  8009. } catch(e){
  8010. return '';
  8011. }
  8012. })
  8013. //针对wps添加的多余标签处理
  8014. .replace(/<\/?div[^>]*>/g,'')
  8015. //去掉多余的属性
  8016. .replace( /v:\w+=(["']?)[^'"]+\1/g, '' )
  8017. .replace( /<(!|script[^>]*>.*?<\/script(?=[>\s])|\/?(\?xml(:\w+)?|xml|meta|link|style|\w+:\w+)(?=[\s\/>]))[^>]*>/gi, "" )
  8018. .replace( /<p [^>]*class="?MsoHeading"?[^>]*>(.*?)<\/p>/gi, "<p><strong>$1</strong></p>" )
  8019. //去掉多余的属性
  8020. .replace( /\s+(class|lang|align)\s*=\s*(['"]?)([\w-]+)\2/ig, function(str,name,marks,val){
  8021. //保留list的标示
  8022. return name == 'class' && val == 'MsoListParagraph' ? str : ''
  8023. })
  8024. //清除多余的font/span不能匹配&nbsp;有可能是空格
  8025. .replace( /<(font|span)[^>]*>(\s*)<\/\1>/gi, function(a,b,c){
  8026. return c.replace(/[\t\r\n ]+/g,' ')
  8027. })
  8028. //处理style的问题
  8029. .replace( /(<[a-z][^>]*)\sstyle=(["'])([^\2]*?)\2/gi, function( str, tag, tmp, style ) {
  8030. var n = [],
  8031. s = style.replace( /^\s+|\s+$/, '' )
  8032. .replace(/&#39;/g,'\'')
  8033. .replace( /&quot;/gi, "'" )
  8034. .replace(/[\d.]+(cm|pt)/g,function(str){
  8035. return utils.transUnitToPx(str)
  8036. })
  8037. .split( /;\s*/g );
  8038. for ( var i = 0,v; v = s[i];i++ ) {
  8039. var name, value,
  8040. parts = v.split( ":" );
  8041. if ( parts.length == 2 ) {
  8042. name = parts[0].toLowerCase();
  8043. value = parts[1].toLowerCase();
  8044. if(/^(background)\w*/.test(name) && value.replace(/(initial|\s)/g,'').length == 0
  8045. ||
  8046. /^(margin)\w*/.test(name) && /^0\w+$/.test(value)
  8047. ){
  8048. continue;
  8049. }
  8050. switch ( name ) {
  8051. case "mso-padding-alt":
  8052. case "mso-padding-top-alt":
  8053. case "mso-padding-right-alt":
  8054. case "mso-padding-bottom-alt":
  8055. case "mso-padding-left-alt":
  8056. case "mso-margin-alt":
  8057. case "mso-margin-top-alt":
  8058. case "mso-margin-right-alt":
  8059. case "mso-margin-bottom-alt":
  8060. case "mso-margin-left-alt":
  8061. //ie下会出现挤到一起的情况
  8062. //case "mso-table-layout-alt":
  8063. case "mso-height":
  8064. case "mso-width":
  8065. case "mso-vertical-align-alt":
  8066. //trace:1819 ff下会解析出padding在table上
  8067. if(!/<table/.test(tag))
  8068. n[i] = name.replace( /^mso-|-alt$/g, "" ) + ":" + transUnit( value );
  8069. continue;
  8070. case "horiz-align":
  8071. n[i] = "text-align:" + value;
  8072. continue;
  8073. case "vert-align":
  8074. n[i] = "vertical-align:" + value;
  8075. continue;
  8076. case "font-color":
  8077. case "mso-foreground":
  8078. n[i] = "color:" + value;
  8079. continue;
  8080. case "mso-background":
  8081. case "mso-highlight":
  8082. n[i] = "background:" + value;
  8083. continue;
  8084. case "mso-default-height":
  8085. n[i] = "min-height:" + transUnit( value );
  8086. continue;
  8087. case "mso-default-width":
  8088. n[i] = "min-width:" + transUnit( value );
  8089. continue;
  8090. case "mso-padding-between-alt":
  8091. n[i] = "border-collapse:separate;border-spacing:" + transUnit( value );
  8092. continue;
  8093. case "text-line-through":
  8094. if ( (value == "single") || (value == "double") ) {
  8095. n[i] = "text-decoration:line-through";
  8096. }
  8097. continue;
  8098. case "mso-zero-height":
  8099. if ( value == "yes" ) {
  8100. n[i] = "display:none";
  8101. }
  8102. continue;
  8103. // case 'background':
  8104. // break;
  8105. case 'margin':
  8106. if ( !/[1-9]/.test( value ) ) {
  8107. continue;
  8108. }
  8109. }
  8110. 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 )
  8111. ||
  8112. /text\-indent|padding|margin/.test(name) && /\-[\d.]+/.test(value)
  8113. ) {
  8114. continue;
  8115. }
  8116. n[i] = name + ":" + parts[1];
  8117. }
  8118. }
  8119. return tag + (n.length ? ' style="' + n.join( ';').replace(/;{2,}/g,';') + '"' : '');
  8120. })
  8121. }
  8122. return function ( html ) {
  8123. return (isWordDocument( html ) ? filterPasteWord( html ) : html);
  8124. };
  8125. }();
  8126. // core/node.js
  8127. /**
  8128. * 编辑器模拟的节点类
  8129. * @file
  8130. * @module UE
  8131. * @class uNode
  8132. * @since 1.2.6.1
  8133. */
  8134. /**
  8135. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  8136. * @unfile
  8137. * @module UE
  8138. */
  8139. (function () {
  8140. /**
  8141. * 编辑器模拟的节点类
  8142. * @unfile
  8143. * @module UE
  8144. * @class uNode
  8145. */
  8146. /**
  8147. * 通过一个键值对,创建一个uNode对象
  8148. * @constructor
  8149. * @param { Object } attr 传入要创建的uNode的初始属性
  8150. * @example
  8151. * ```javascript
  8152. * var node = new uNode({
  8153. * type:'element',
  8154. * tagName:'span',
  8155. * attrs:{style:'font-size:14px;'}
  8156. * }
  8157. * ```
  8158. */
  8159. var uNode = UE.uNode = function (obj) {
  8160. this.type = obj.type;
  8161. this.data = obj.data;
  8162. this.tagName = obj.tagName;
  8163. this.parentNode = obj.parentNode;
  8164. this.attrs = obj.attrs || {};
  8165. this.children = obj.children;
  8166. };
  8167. var notTransAttrs = {
  8168. 'href':1,
  8169. 'src':1,
  8170. '_src':1,
  8171. '_href':1,
  8172. 'cdata_data':1
  8173. };
  8174. var notTransTagName = {
  8175. style:1,
  8176. script:1
  8177. };
  8178. var indentChar = ' ',
  8179. breakChar = '\n';
  8180. function insertLine(arr, current, begin) {
  8181. arr.push(breakChar);
  8182. return current + (begin ? 1 : -1);
  8183. }
  8184. function insertIndent(arr, current) {
  8185. //插入缩进
  8186. for (var i = 0; i < current; i++) {
  8187. arr.push(indentChar);
  8188. }
  8189. }
  8190. //创建uNode的静态方法
  8191. //支持标签和html
  8192. uNode.createElement = function (html) {
  8193. if (/[<>]/.test(html)) {
  8194. return UE.htmlparser(html).children[0]
  8195. } else {
  8196. return new uNode({
  8197. type:'element',
  8198. children:[],
  8199. tagName:html
  8200. })
  8201. }
  8202. };
  8203. uNode.createText = function (data,noTrans) {
  8204. return new UE.uNode({
  8205. type:'text',
  8206. 'data':noTrans ? data : utils.unhtml(data || '')
  8207. })
  8208. };
  8209. function nodeToHtml(node, arr, formatter, current) {
  8210. switch (node.type) {
  8211. case 'root':
  8212. for (var i = 0, ci; ci = node.children[i++];) {
  8213. //插入新行
  8214. if (formatter && ci.type == 'element' && !dtd.$inlineWithA[ci.tagName] && i > 1) {
  8215. insertLine(arr, current, true);
  8216. insertIndent(arr, current)
  8217. }
  8218. nodeToHtml(ci, arr, formatter, current)
  8219. }
  8220. break;
  8221. case 'text':
  8222. isText(node, arr);
  8223. break;
  8224. case 'element':
  8225. isElement(node, arr, formatter, current);
  8226. break;
  8227. case 'comment':
  8228. isComment(node, arr, formatter);
  8229. }
  8230. return arr;
  8231. }
  8232. function isText(node, arr) {
  8233. if(node.parentNode.tagName == 'pre'){
  8234. //源码模式下输入html标签,不能做转换处理,直接输出
  8235. arr.push(node.data)
  8236. }else{
  8237. arr.push(notTransTagName[node.parentNode.tagName] ? utils.html(node.data) : node.data.replace(/[ ]{2}/g,' &nbsp;'))
  8238. }
  8239. }
  8240. function isElement(node, arr, formatter, current) {
  8241. var attrhtml = '';
  8242. if (node.attrs) {
  8243. attrhtml = [];
  8244. var attrs = node.attrs;
  8245. for (var a in attrs) {
  8246. //这里就针对
  8247. //<p>'<img src='http://nsclick.baidu.com/u.gif?&asdf=\"sdf&asdfasdfs;asdf'></p>
  8248. //这里边的\"做转换,要不用innerHTML直接被截断了,属性src
  8249. //有可能做的不够
  8250. attrhtml.push(a + (attrs[a] !== undefined ? '="' + (notTransAttrs[a] ? utils.html(attrs[a]).replace(/["]/g, function (a) {
  8251. return '&quot;'
  8252. }) : utils.unhtml(attrs[a])) + '"' : ''))
  8253. }
  8254. attrhtml = attrhtml.join(' ');
  8255. }
  8256. arr.push('<' + node.tagName +
  8257. (attrhtml ? ' ' + attrhtml : '') +
  8258. (dtd.$empty[node.tagName] ? '\/' : '' ) + '>'
  8259. );
  8260. //插入新行
  8261. if (formatter && !dtd.$inlineWithA[node.tagName] && node.tagName != 'pre') {
  8262. if(node.children && node.children.length){
  8263. current = insertLine(arr, current, true);
  8264. insertIndent(arr, current)
  8265. }
  8266. }
  8267. if (node.children && node.children.length) {
  8268. for (var i = 0, ci; ci = node.children[i++];) {
  8269. if (formatter && ci.type == 'element' && !dtd.$inlineWithA[ci.tagName] && i > 1) {
  8270. insertLine(arr, current);
  8271. insertIndent(arr, current)
  8272. }
  8273. nodeToHtml(ci, arr, formatter, current)
  8274. }
  8275. }
  8276. if (!dtd.$empty[node.tagName]) {
  8277. if (formatter && !dtd.$inlineWithA[node.tagName] && node.tagName != 'pre') {
  8278. if(node.children && node.children.length){
  8279. current = insertLine(arr, current);
  8280. insertIndent(arr, current)
  8281. }
  8282. }
  8283. arr.push('<\/' + node.tagName + '>');
  8284. }
  8285. }
  8286. function isComment(node, arr) {
  8287. arr.push('<!--' + node.data + '-->');
  8288. }
  8289. function getNodeById(root, id) {
  8290. var node;
  8291. if (root.type == 'element' && root.getAttr('id') == id) {
  8292. return root;
  8293. }
  8294. if (root.children && root.children.length) {
  8295. for (var i = 0, ci; ci = root.children[i++];) {
  8296. if (node = getNodeById(ci, id)) {
  8297. return node;
  8298. }
  8299. }
  8300. }
  8301. }
  8302. function getNodesByTagName(node, tagName, arr) {
  8303. if (node.type == 'element' && node.tagName == tagName) {
  8304. arr.push(node);
  8305. }
  8306. if (node.children && node.children.length) {
  8307. for (var i = 0, ci; ci = node.children[i++];) {
  8308. getNodesByTagName(ci, tagName, arr)
  8309. }
  8310. }
  8311. }
  8312. function nodeTraversal(root,fn){
  8313. if(root.children && root.children.length){
  8314. for(var i= 0,ci;ci=root.children[i];){
  8315. nodeTraversal(ci,fn);
  8316. //ci被替换的情况,这里就不再走 fn了
  8317. if(ci.parentNode ){
  8318. if(ci.children && ci.children.length){
  8319. fn(ci)
  8320. }
  8321. if(ci.parentNode) i++
  8322. }
  8323. }
  8324. }else{
  8325. fn(root)
  8326. }
  8327. }
  8328. uNode.prototype = {
  8329. /**
  8330. * 当前节点对象,转换成html文本
  8331. * @method toHtml
  8332. * @return { String } 返回转换后的html字符串
  8333. * @example
  8334. * ```javascript
  8335. * node.toHtml();
  8336. * ```
  8337. */
  8338. /**
  8339. * 当前节点对象,转换成html文本
  8340. * @method toHtml
  8341. * @param { Boolean } formatter 是否格式化返回值
  8342. * @return { String } 返回转换后的html字符串
  8343. * @example
  8344. * ```javascript
  8345. * node.toHtml( true );
  8346. * ```
  8347. */
  8348. toHtml:function (formatter) {
  8349. var arr = [];
  8350. nodeToHtml(this, arr, formatter, 0);
  8351. return arr.join('')
  8352. },
  8353. /**
  8354. * 获取节点的html内容
  8355. * @method innerHTML
  8356. * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
  8357. * @return { String } 返回节点的html内容
  8358. * @example
  8359. * ```javascript
  8360. * var htmlstr = node.innerHTML();
  8361. * ```
  8362. */
  8363. /**
  8364. * 设置节点的html内容
  8365. * @method innerHTML
  8366. * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
  8367. * @param { String } htmlstr 传入要设置的html内容
  8368. * @return { UE.uNode } 返回节点本身
  8369. * @example
  8370. * ```javascript
  8371. * node.innerHTML('<span>text</span>');
  8372. * ```
  8373. */
  8374. innerHTML:function (htmlstr) {
  8375. if (this.type != 'element' || dtd.$empty[this.tagName]) {
  8376. return this;
  8377. }
  8378. if (utils.isString(htmlstr)) {
  8379. if(this.children){
  8380. for (var i = 0, ci; ci = this.children[i++];) {
  8381. ci.parentNode = null;
  8382. }
  8383. }
  8384. this.children = [];
  8385. var tmpRoot = UE.htmlparser(htmlstr);
  8386. for (var i = 0, ci; ci = tmpRoot.children[i++];) {
  8387. this.children.push(ci);
  8388. ci.parentNode = this;
  8389. }
  8390. return this;
  8391. } else {
  8392. var tmpRoot = new UE.uNode({
  8393. type:'root',
  8394. children:this.children
  8395. });
  8396. return tmpRoot.toHtml();
  8397. }
  8398. },
  8399. /**
  8400. * 获取节点的纯文本内容
  8401. * @method innerText
  8402. * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
  8403. * @return { String } 返回节点的存文本内容
  8404. * @example
  8405. * ```javascript
  8406. * var textStr = node.innerText();
  8407. * ```
  8408. */
  8409. /**
  8410. * 设置节点的纯文本内容
  8411. * @method innerText
  8412. * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
  8413. * @param { String } textStr 传入要设置的文本内容
  8414. * @return { UE.uNode } 返回节点本身
  8415. * @example
  8416. * ```javascript
  8417. * node.innerText('<span>text</span>');
  8418. * ```
  8419. */
  8420. innerText:function (textStr,noTrans) {
  8421. if (this.type != 'element' || dtd.$empty[this.tagName]) {
  8422. return this;
  8423. }
  8424. if (textStr) {
  8425. if(this.children){
  8426. for (var i = 0, ci; ci = this.children[i++];) {
  8427. ci.parentNode = null;
  8428. }
  8429. }
  8430. this.children = [];
  8431. this.appendChild(uNode.createText(textStr,noTrans));
  8432. return this;
  8433. } else {
  8434. return this.toHtml().replace(/<[^>]+>/g, '');
  8435. }
  8436. },
  8437. /**
  8438. * 获取当前对象的data属性
  8439. * @method getData
  8440. * @return { Object } 若节点的type值是elemenet,返回空字符串,否则返回节点的data属性
  8441. * @example
  8442. * ```javascript
  8443. * node.getData();
  8444. * ```
  8445. */
  8446. getData:function () {
  8447. if (this.type == 'element')
  8448. return '';
  8449. return this.data
  8450. },
  8451. /**
  8452. * 获取当前节点下的第一个子节点
  8453. * @method firstChild
  8454. * @return { UE.uNode } 返回第一个子节点
  8455. * @example
  8456. * ```javascript
  8457. * node.firstChild(); //返回第一个子节点
  8458. * ```
  8459. */
  8460. firstChild:function () {
  8461. // if (this.type != 'element' || dtd.$empty[this.tagName]) {
  8462. // return this;
  8463. // }
  8464. return this.children ? this.children[0] : null;
  8465. },
  8466. /**
  8467. * 获取当前节点下的最后一个子节点
  8468. * @method lastChild
  8469. * @return { UE.uNode } 返回最后一个子节点
  8470. * @example
  8471. * ```javascript
  8472. * node.lastChild(); //返回最后一个子节点
  8473. * ```
  8474. */
  8475. lastChild:function () {
  8476. // if (this.type != 'element' || dtd.$empty[this.tagName] ) {
  8477. // return this;
  8478. // }
  8479. return this.children ? this.children[this.children.length - 1] : null;
  8480. },
  8481. /**
  8482. * 获取和当前节点有相同父亲节点的前一个节点
  8483. * @method previousSibling
  8484. * @return { UE.uNode } 返回前一个节点
  8485. * @example
  8486. * ```javascript
  8487. * node.children[2].previousSibling(); //返回子节点node.children[1]
  8488. * ```
  8489. */
  8490. previousSibling : function(){
  8491. var parent = this.parentNode;
  8492. for (var i = 0, ci; ci = parent.children[i]; i++) {
  8493. if (ci === this) {
  8494. return i == 0 ? null : parent.children[i-1];
  8495. }
  8496. }
  8497. },
  8498. /**
  8499. * 获取和当前节点有相同父亲节点的后一个节点
  8500. * @method nextSibling
  8501. * @return { UE.uNode } 返回后一个节点,找不到返回null
  8502. * @example
  8503. * ```javascript
  8504. * node.children[2].nextSibling(); //如果有,返回子节点node.children[3]
  8505. * ```
  8506. */
  8507. nextSibling : function(){
  8508. var parent = this.parentNode;
  8509. for (var i = 0, ci; ci = parent.children[i++];) {
  8510. if (ci === this) {
  8511. return parent.children[i];
  8512. }
  8513. }
  8514. },
  8515. /**
  8516. * 用新的节点替换当前节点
  8517. * @method replaceChild
  8518. * @param { UE.uNode } target 要替换成该节点参数
  8519. * @param { UE.uNode } source 要被替换掉的节点
  8520. * @return { UE.uNode } 返回替换之后的节点对象
  8521. * @example
  8522. * ```javascript
  8523. * node.replaceChild(newNode, childNode); //用newNode替换childNode,childNode是node的子节点
  8524. * ```
  8525. */
  8526. replaceChild:function (target, source) {
  8527. if (this.children) {
  8528. if(target.parentNode){
  8529. target.parentNode.removeChild(target);
  8530. }
  8531. for (var i = 0, ci; ci = this.children[i]; i++) {
  8532. if (ci === source) {
  8533. this.children.splice(i, 1, target);
  8534. source.parentNode = null;
  8535. target.parentNode = this;
  8536. return target;
  8537. }
  8538. }
  8539. }
  8540. },
  8541. /**
  8542. * 在节点的子节点列表最后位置插入一个节点
  8543. * @method appendChild
  8544. * @param { UE.uNode } node 要插入的节点
  8545. * @return { UE.uNode } 返回刚插入的子节点
  8546. * @example
  8547. * ```javascript
  8548. * node.appendChild( newNode ); //在node内插入子节点newNode
  8549. * ```
  8550. */
  8551. appendChild:function (node) {
  8552. if (this.type == 'root' || (this.type == 'element' && !dtd.$empty[this.tagName])) {
  8553. if (!this.children) {
  8554. this.children = []
  8555. }
  8556. if(node.parentNode){
  8557. node.parentNode.removeChild(node);
  8558. }
  8559. for (var i = 0, ci; ci = this.children[i]; i++) {
  8560. if (ci === node) {
  8561. this.children.splice(i, 1);
  8562. break;
  8563. }
  8564. }
  8565. this.children.push(node);
  8566. node.parentNode = this;
  8567. return node;
  8568. }
  8569. },
  8570. /**
  8571. * 在传入节点的前面插入一个节点
  8572. * @method insertBefore
  8573. * @param { UE.uNode } target 要插入的节点
  8574. * @param { UE.uNode } source 在该参数节点前面插入
  8575. * @return { UE.uNode } 返回刚插入的子节点
  8576. * @example
  8577. * ```javascript
  8578. * node.parentNode.insertBefore(newNode, node); //在node节点后面插入newNode
  8579. * ```
  8580. */
  8581. insertBefore:function (target, source) {
  8582. if (this.children) {
  8583. if(target.parentNode){
  8584. target.parentNode.removeChild(target);
  8585. }
  8586. for (var i = 0, ci; ci = this.children[i]; i++) {
  8587. if (ci === source) {
  8588. this.children.splice(i, 0, target);
  8589. target.parentNode = this;
  8590. return target;
  8591. }
  8592. }
  8593. }
  8594. },
  8595. /**
  8596. * 在传入节点的后面插入一个节点
  8597. * @method insertAfter
  8598. * @param { UE.uNode } target 要插入的节点
  8599. * @param { UE.uNode } source 在该参数节点后面插入
  8600. * @return { UE.uNode } 返回刚插入的子节点
  8601. * @example
  8602. * ```javascript
  8603. * node.parentNode.insertAfter(newNode, node); //在node节点后面插入newNode
  8604. * ```
  8605. */
  8606. insertAfter:function (target, source) {
  8607. if (this.children) {
  8608. if(target.parentNode){
  8609. target.parentNode.removeChild(target);
  8610. }
  8611. for (var i = 0, ci; ci = this.children[i]; i++) {
  8612. if (ci === source) {
  8613. this.children.splice(i + 1, 0, target);
  8614. target.parentNode = this;
  8615. return target;
  8616. }
  8617. }
  8618. }
  8619. },
  8620. /**
  8621. * 从当前节点的子节点列表中,移除节点
  8622. * @method removeChild
  8623. * @param { UE.uNode } node 要移除的节点引用
  8624. * @param { Boolean } keepChildren 是否保留移除节点的子节点,若传入true,自动把移除节点的子节点插入到移除的位置
  8625. * @return { * } 返回刚移除的子节点
  8626. * @example
  8627. * ```javascript
  8628. * node.removeChild(childNode,true); //在node的子节点列表中移除child节点,并且吧child的子节点插入到移除的位置
  8629. * ```
  8630. */
  8631. removeChild:function (node,keepChildren) {
  8632. if (this.children) {
  8633. for (var i = 0, ci; ci = this.children[i]; i++) {
  8634. if (ci === node) {
  8635. this.children.splice(i, 1);
  8636. ci.parentNode = null;
  8637. if(keepChildren && ci.children && ci.children.length){
  8638. for(var j= 0,cj;cj=ci.children[j];j++){
  8639. this.children.splice(i+j,0,cj);
  8640. cj.parentNode = this;
  8641. }
  8642. }
  8643. return ci;
  8644. }
  8645. }
  8646. }
  8647. },
  8648. /**
  8649. * 获取当前节点所代表的元素属性,即获取attrs对象下的属性值
  8650. * @method getAttr
  8651. * @param { String } attrName 要获取的属性名称
  8652. * @return { * } 返回attrs对象下的属性值
  8653. * @example
  8654. * ```javascript
  8655. * node.getAttr('title');
  8656. * ```
  8657. */
  8658. getAttr:function (attrName) {
  8659. return this.attrs && this.attrs[attrName.toLowerCase()]
  8660. },
  8661. /**
  8662. * 设置当前节点所代表的元素属性,即设置attrs对象下的属性值
  8663. * @method setAttr
  8664. * @param { String } attrName 要设置的属性名称
  8665. * @param { * } attrVal 要设置的属性值,类型视设置的属性而定
  8666. * @return { * } 返回attrs对象下的属性值
  8667. * @example
  8668. * ```javascript
  8669. * node.setAttr('title','标题');
  8670. * ```
  8671. */
  8672. setAttr:function (attrName, attrVal) {
  8673. if (!attrName) {
  8674. delete this.attrs;
  8675. return;
  8676. }
  8677. if(!this.attrs){
  8678. this.attrs = {};
  8679. }
  8680. if (utils.isObject(attrName)) {
  8681. for (var a in attrName) {
  8682. if (!attrName[a]) {
  8683. delete this.attrs[a]
  8684. } else {
  8685. this.attrs[a.toLowerCase()] = attrName[a];
  8686. }
  8687. }
  8688. } else {
  8689. if (!attrVal) {
  8690. delete this.attrs[attrName]
  8691. } else {
  8692. this.attrs[attrName.toLowerCase()] = attrVal;
  8693. }
  8694. }
  8695. },
  8696. /**
  8697. * 获取当前节点在父节点下的位置索引
  8698. * @method getIndex
  8699. * @return { Number } 返回索引数值,如果没有父节点,返回-1
  8700. * @example
  8701. * ```javascript
  8702. * node.getIndex();
  8703. * ```
  8704. */
  8705. getIndex:function(){
  8706. var parent = this.parentNode;
  8707. for(var i= 0,ci;ci=parent.children[i];i++){
  8708. if(ci === this){
  8709. return i;
  8710. }
  8711. }
  8712. return -1;
  8713. },
  8714. /**
  8715. * 在当前节点下,根据id查找节点
  8716. * @method getNodeById
  8717. * @param { String } id 要查找的id
  8718. * @return { UE.uNode } 返回找到的节点
  8719. * @example
  8720. * ```javascript
  8721. * node.getNodeById('textId');
  8722. * ```
  8723. */
  8724. getNodeById:function (id) {
  8725. var node;
  8726. if (this.children && this.children.length) {
  8727. for (var i = 0, ci; ci = this.children[i++];) {
  8728. if (node = getNodeById(ci, id)) {
  8729. return node;
  8730. }
  8731. }
  8732. }
  8733. },
  8734. /**
  8735. * 在当前节点下,根据元素名称查找节点列表
  8736. * @method getNodesByTagName
  8737. * @param { String } tagNames 要查找的元素名称
  8738. * @return { Array } 返回找到的节点列表
  8739. * @example
  8740. * ```javascript
  8741. * node.getNodesByTagName('span');
  8742. * ```
  8743. */
  8744. getNodesByTagName:function (tagNames) {
  8745. tagNames = utils.trim(tagNames).replace(/[ ]{2,}/g, ' ').split(' ');
  8746. var arr = [], me = this;
  8747. utils.each(tagNames, function (tagName) {
  8748. if (me.children && me.children.length) {
  8749. for (var i = 0, ci; ci = me.children[i++];) {
  8750. getNodesByTagName(ci, tagName, arr)
  8751. }
  8752. }
  8753. });
  8754. return arr;
  8755. },
  8756. /**
  8757. * 根据样式名称,获取节点的样式值
  8758. * @method getStyle
  8759. * @param { String } name 要获取的样式名称
  8760. * @return { String } 返回样式值
  8761. * @example
  8762. * ```javascript
  8763. * node.getStyle('font-size');
  8764. * ```
  8765. */
  8766. getStyle:function (name) {
  8767. var cssStyle = this.getAttr('style');
  8768. if (!cssStyle) {
  8769. return ''
  8770. }
  8771. var reg = new RegExp('(^|;)\\s*' + name + ':([^;]+)','i');
  8772. var match = cssStyle.match(reg);
  8773. if (match && match[0]) {
  8774. return match[2]
  8775. }
  8776. return '';
  8777. },
  8778. /**
  8779. * 给节点设置样式
  8780. * @method setStyle
  8781. * @param { String } name 要设置的的样式名称
  8782. * @param { String } val 要设置的的样值
  8783. * @example
  8784. * ```javascript
  8785. * node.setStyle('font-size', '12px');
  8786. * ```
  8787. */
  8788. setStyle:function (name, val) {
  8789. function exec(name, val) {
  8790. var reg = new RegExp('(^|;)\\s*' + name + ':([^;]+;?)', 'gi');
  8791. cssStyle = cssStyle.replace(reg, '$1');
  8792. if (val) {
  8793. cssStyle = name + ':' + utils.unhtml(val) + ';' + cssStyle
  8794. }
  8795. }
  8796. var cssStyle = this.getAttr('style');
  8797. if (!cssStyle) {
  8798. cssStyle = '';
  8799. }
  8800. if (utils.isObject(name)) {
  8801. for (var a in name) {
  8802. exec(a, name[a])
  8803. }
  8804. } else {
  8805. exec(name, val)
  8806. }
  8807. this.setAttr('style', utils.trim(cssStyle))
  8808. },
  8809. /**
  8810. * 传入一个函数,递归遍历当前节点下的所有节点
  8811. * @method traversal
  8812. * @param { Function } fn 遍历到节点的时,传入节点作为参数,运行此函数
  8813. * @example
  8814. * ```javascript
  8815. * traversal(node, function(){
  8816. * console.log(node.type);
  8817. * });
  8818. * ```
  8819. */
  8820. traversal:function(fn){
  8821. if(this.children && this.children.length){
  8822. nodeTraversal(this,fn);
  8823. }
  8824. return this;
  8825. }
  8826. }
  8827. })();
  8828. // core/htmlparser.js
  8829. /**
  8830. * html字符串转换成uNode节点
  8831. * @file
  8832. * @module UE
  8833. * @since 1.2.6.1
  8834. */
  8835. /**
  8836. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  8837. * @unfile
  8838. * @module UE
  8839. */
  8840. /**
  8841. * html字符串转换成uNode节点的静态方法
  8842. * @method htmlparser
  8843. * @param { String } htmlstr 要转换的html代码
  8844. * @param { Boolean } ignoreBlank 若设置为true,转换的时候忽略\n\r\t等空白字符
  8845. * @return { uNode } 给定的html片段转换形成的uNode对象
  8846. * @example
  8847. * ```javascript
  8848. * var root = UE.htmlparser('<p><b>htmlparser</b></p>', true);
  8849. * ```
  8850. */
  8851. var htmlparser = UE.htmlparser = function (htmlstr,ignoreBlank) {
  8852. //todo 原来的方式 [^"'<>\/] 有\/就不能配对上 <TD vAlign=top background=../AAA.JPG> 这样的标签了
  8853. //先去掉了,加上的原因忘了,这里先记录
  8854. var re_tag = /<(?:(?:\/([^>]+)>)|(?:!--([\S|\s]*?)-->)|(?:([^\s\/<>]+)\s*((?:(?:"[^"]*")|(?:'[^']*')|[^"'<>])*)\/?>))/g,
  8855. re_attr = /([\w\-:.]+)(?:(?:\s*=\s*(?:(?:"([^"]*)")|(?:'([^']*)')|([^\s>]+)))|(?=\s|$))/g;
  8856. //ie下取得的html可能会有\n存在,要去掉,在处理replace(/[\t\r\n]*/g,'');代码高量的\n不能去除
  8857. var allowEmptyTags = {
  8858. b:1,code:1,i:1,u:1,strike:1,s:1,tt:1,strong:1,q:1,samp:1,em:1,span:1,
  8859. sub:1,img:1,sup:1,font:1,big:1,small:1,iframe:1,a:1,br:1,pre:1
  8860. };
  8861. htmlstr = htmlstr.replace(new RegExp(domUtils.fillChar, 'g'), '');
  8862. if(!ignoreBlank){
  8863. htmlstr = htmlstr.replace(new RegExp('[\\r\\t\\n'+(ignoreBlank?'':' ')+']*<\/?(\\w+)\\s*(?:[^>]*)>[\\r\\t\\n'+(ignoreBlank?'':' ')+']*','g'), function(a,b){
  8864. //br暂时单独处理
  8865. if(b && allowEmptyTags[b.toLowerCase()]){
  8866. return a.replace(/(^[\n\r]+)|([\n\r]+$)/g,'');
  8867. }
  8868. return a.replace(new RegExp('^[\\r\\n'+(ignoreBlank?'':' ')+']+'),'').replace(new RegExp('[\\r\\n'+(ignoreBlank?'':' ')+']+$'),'');
  8869. });
  8870. }
  8871. var notTransAttrs = {
  8872. 'href':1,
  8873. 'src':1
  8874. };
  8875. var uNode = UE.uNode,
  8876. needParentNode = {
  8877. 'td':'tr',
  8878. 'tr':['tbody','thead','tfoot'],
  8879. 'tbody':'table',
  8880. 'th':'tr',
  8881. 'thead':'table',
  8882. 'tfoot':'table',
  8883. 'caption':'table',
  8884. 'li':['ul', 'ol'],
  8885. 'dt':'dl',
  8886. 'dd':'dl',
  8887. 'option':'select'
  8888. },
  8889. needChild = {
  8890. 'ol':'li',
  8891. 'ul':'li'
  8892. };
  8893. function text(parent, data) {
  8894. if(needChild[parent.tagName]){
  8895. var tmpNode = uNode.createElement(needChild[parent.tagName]);
  8896. parent.appendChild(tmpNode);
  8897. tmpNode.appendChild(uNode.createText(data));
  8898. parent = tmpNode;
  8899. }else{
  8900. parent.appendChild(uNode.createText(data));
  8901. }
  8902. }
  8903. function element(parent, tagName, htmlattr) {
  8904. var needParentTag;
  8905. if (needParentTag = needParentNode[tagName]) {
  8906. var tmpParent = parent,hasParent;
  8907. while(tmpParent.type != 'root'){
  8908. if(utils.isArray(needParentTag) ? utils.indexOf(needParentTag, tmpParent.tagName) != -1 : needParentTag == tmpParent.tagName){
  8909. parent = tmpParent;
  8910. hasParent = true;
  8911. break;
  8912. }
  8913. tmpParent = tmpParent.parentNode;
  8914. }
  8915. if(!hasParent){
  8916. parent = element(parent, utils.isArray(needParentTag) ? needParentTag[0] : needParentTag)
  8917. }
  8918. }
  8919. //按dtd处理嵌套
  8920. // if(parent.type != 'root' && !dtd[parent.tagName][tagName])
  8921. // parent = parent.parentNode;
  8922. var elm = new uNode({
  8923. parentNode:parent,
  8924. type:'element',
  8925. tagName:tagName.toLowerCase(),
  8926. //是自闭合的处理一下
  8927. children:dtd.$empty[tagName] ? null : []
  8928. });
  8929. //如果属性存在,处理属性
  8930. if (htmlattr) {
  8931. var attrs = {}, match;
  8932. while (match = re_attr.exec(htmlattr)) {
  8933. attrs[match[1].toLowerCase()] = notTransAttrs[match[1].toLowerCase()] ? (match[2] || match[3] || match[4]) : utils.unhtml(match[2] || match[3] || match[4])
  8934. }
  8935. elm.attrs = attrs;
  8936. }
  8937. //trace:3970
  8938. // //如果parent下不能放elm
  8939. // if(dtd.$inline[parent.tagName] && dtd.$block[elm.tagName] && !dtd[parent.tagName][elm.tagName]){
  8940. // parent = parent.parentNode;
  8941. // elm.parentNode = parent;
  8942. // }
  8943. parent.children.push(elm);
  8944. //如果是自闭合节点返回父亲节点
  8945. return dtd.$empty[tagName] ? parent : elm
  8946. }
  8947. function comment(parent, data) {
  8948. parent.children.push(new uNode({
  8949. type:'comment',
  8950. data:data,
  8951. parentNode:parent
  8952. }));
  8953. }
  8954. var match, currentIndex = 0, nextIndex = 0;
  8955. //设置根节点
  8956. var root = new uNode({
  8957. type:'root',
  8958. children:[]
  8959. });
  8960. var currentParent = root;
  8961. while (match = re_tag.exec(htmlstr)) {
  8962. currentIndex = match.index;
  8963. try{
  8964. if (currentIndex > nextIndex) {
  8965. //text node
  8966. text(currentParent, htmlstr.slice(nextIndex, currentIndex));
  8967. }
  8968. if (match[3]) {
  8969. if(dtd.$cdata[currentParent.tagName]){
  8970. text(currentParent, match[0]);
  8971. }else{
  8972. //start tag
  8973. currentParent = element(currentParent, match[3].toLowerCase(), match[4]);
  8974. }
  8975. } else if (match[1]) {
  8976. if(currentParent.type != 'root'){
  8977. if(dtd.$cdata[currentParent.tagName] && !dtd.$cdata[match[1]]){
  8978. text(currentParent, match[0]);
  8979. }else{
  8980. var tmpParent = currentParent;
  8981. while(currentParent.type == 'element' && currentParent.tagName != match[1].toLowerCase()){
  8982. currentParent = currentParent.parentNode;
  8983. if(currentParent.type == 'root'){
  8984. currentParent = tmpParent;
  8985. throw 'break'
  8986. }
  8987. }
  8988. //end tag
  8989. currentParent = currentParent.parentNode;
  8990. }
  8991. }
  8992. } else if (match[2]) {
  8993. //comment
  8994. comment(currentParent, match[2])
  8995. }
  8996. }catch(e){}
  8997. nextIndex = re_tag.lastIndex;
  8998. }
  8999. //如果结束是文本,就有可能丢掉,所以这里手动判断一下
  9000. //例如 <li>sdfsdfsdf<li>sdfsdfsdfsdf
  9001. if (nextIndex < htmlstr.length) {
  9002. text(currentParent, htmlstr.slice(nextIndex));
  9003. }
  9004. return root;
  9005. };
  9006. // core/filternode.js
  9007. /**
  9008. * UE过滤节点的静态方法
  9009. * @file
  9010. */
  9011. /**
  9012. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  9013. * @module UE
  9014. */
  9015. /**
  9016. * 根据传入节点和过滤规则过滤相应节点
  9017. * @module UE
  9018. * @since 1.2.6.1
  9019. * @method filterNode
  9020. * @param { Object } root 指定root节点
  9021. * @param { Object } rules 过滤规则json对象
  9022. * @example
  9023. * ```javascript
  9024. * UE.filterNode(root,editor.options.filterRules);
  9025. * ```
  9026. */
  9027. var filterNode = UE.filterNode = function () {
  9028. function filterNode(node,rules){
  9029. switch (node.type) {
  9030. case 'text':
  9031. break;
  9032. case 'element':
  9033. var val;
  9034. if(val = rules[node.tagName]){
  9035. if(val === '-'){
  9036. node.parentNode.removeChild(node)
  9037. }else if(utils.isFunction(val)){
  9038. var parentNode = node.parentNode,
  9039. index = node.getIndex();
  9040. val(node);
  9041. if(node.parentNode){
  9042. if(node.children){
  9043. for(var i = 0,ci;ci=node.children[i];){
  9044. filterNode(ci,rules);
  9045. if(ci.parentNode){
  9046. i++;
  9047. }
  9048. }
  9049. }
  9050. }else{
  9051. for(var i = index,ci;ci=parentNode.children[i];){
  9052. filterNode(ci,rules);
  9053. if(ci.parentNode){
  9054. i++;
  9055. }
  9056. }
  9057. }
  9058. }else{
  9059. var attrs = val['$'];
  9060. if(attrs && node.attrs){
  9061. var tmpAttrs = {},tmpVal;
  9062. for(var a in attrs){
  9063. tmpVal = node.getAttr(a);
  9064. //todo 只先对style单独处理
  9065. if(a == 'style' && utils.isArray(attrs[a])){
  9066. var tmpCssStyle = [];
  9067. utils.each(attrs[a],function(v){
  9068. var tmp;
  9069. if(tmp = node.getStyle(v)){
  9070. tmpCssStyle.push(v + ':' + tmp);
  9071. }
  9072. });
  9073. tmpVal = tmpCssStyle.join(';')
  9074. }
  9075. if(tmpVal){
  9076. tmpAttrs[a] = tmpVal;
  9077. }
  9078. }
  9079. node.attrs = tmpAttrs;
  9080. }
  9081. if(node.children){
  9082. for(var i = 0,ci;ci=node.children[i];){
  9083. filterNode(ci,rules);
  9084. if(ci.parentNode){
  9085. i++;
  9086. }
  9087. }
  9088. }
  9089. }
  9090. }else{
  9091. //如果不在名单里扣出子节点并删除该节点,cdata除外
  9092. if(dtd.$cdata[node.tagName]){
  9093. node.parentNode.removeChild(node)
  9094. }else{
  9095. var parentNode = node.parentNode,
  9096. index = node.getIndex();
  9097. node.parentNode.removeChild(node,true);
  9098. for(var i = index,ci;ci=parentNode.children[i];){
  9099. filterNode(ci,rules);
  9100. if(ci.parentNode){
  9101. i++;
  9102. }
  9103. }
  9104. }
  9105. }
  9106. break;
  9107. case 'comment':
  9108. node.parentNode.removeChild(node)
  9109. }
  9110. }
  9111. return function(root,rules){
  9112. if(utils.isEmptyObject(rules)){
  9113. return root;
  9114. }
  9115. var val;
  9116. if(val = rules['-']){
  9117. utils.each(val.split(' '),function(k){
  9118. rules[k] = '-'
  9119. })
  9120. }
  9121. for(var i= 0,ci;ci=root.children[i];){
  9122. filterNode(ci,rules);
  9123. if(ci.parentNode){
  9124. i++;
  9125. }
  9126. }
  9127. return root;
  9128. }
  9129. }();
  9130. // core/plugin.js
  9131. /**
  9132. * Created with JetBrains PhpStorm.
  9133. * User: campaign
  9134. * Date: 10/8/13
  9135. * Time: 6:15 PM
  9136. * To change this template use File | Settings | File Templates.
  9137. */
  9138. UE.plugin = function(){
  9139. var _plugins = {};
  9140. return {
  9141. register : function(pluginName,fn,oldOptionName,afterDisabled){
  9142. if(oldOptionName && utils.isFunction(oldOptionName)){
  9143. afterDisabled = oldOptionName;
  9144. oldOptionName = null
  9145. }
  9146. _plugins[pluginName] = {
  9147. optionName : oldOptionName || pluginName,
  9148. execFn : fn,
  9149. //当插件被禁用时执行
  9150. afterDisabled : afterDisabled
  9151. }
  9152. },
  9153. load : function(editor){
  9154. utils.each(_plugins,function(plugin){
  9155. var _export = plugin.execFn.call(editor);
  9156. if(editor.options[plugin.optionName] !== false){
  9157. if(_export){
  9158. //后边需要再做扩展
  9159. utils.each(_export,function(v,k){
  9160. switch(k.toLowerCase()){
  9161. case 'shortcutkey':
  9162. editor.addshortcutkey(v);
  9163. break;
  9164. case 'bindevents':
  9165. utils.each(v,function(fn,eventName){
  9166. editor.addListener(eventName,fn);
  9167. });
  9168. break;
  9169. case 'bindmultievents':
  9170. utils.each(utils.isArray(v) ? v:[v],function(event){
  9171. var types = utils.trim(event.type).split(/\s+/);
  9172. utils.each(types,function(eventName){
  9173. editor.addListener(eventName, event.handler);
  9174. });
  9175. });
  9176. break;
  9177. case 'commands':
  9178. utils.each(v,function(execFn,execName){
  9179. editor.commands[execName] = execFn
  9180. });
  9181. break;
  9182. case 'outputrule':
  9183. editor.addOutputRule(v);
  9184. break;
  9185. case 'inputrule':
  9186. editor.addInputRule(v);
  9187. break;
  9188. case 'defaultoptions':
  9189. editor.setOpt(v)
  9190. }
  9191. })
  9192. }
  9193. }else if(plugin.afterDisabled){
  9194. plugin.afterDisabled.call(editor)
  9195. }
  9196. });
  9197. //向下兼容
  9198. utils.each(UE.plugins,function(plugin){
  9199. plugin.call(editor);
  9200. });
  9201. },
  9202. run : function(pluginName,editor){
  9203. var plugin = _plugins[pluginName];
  9204. if(plugin){
  9205. plugin.exeFn.call(editor)
  9206. }
  9207. }
  9208. }
  9209. }();
  9210. // core/keymap.js
  9211. var keymap = UE.keymap = {
  9212. 'Backspace' : 8,
  9213. 'Tab' : 9,
  9214. 'Enter' : 13,
  9215. 'Shift':16,
  9216. 'Control':17,
  9217. 'Alt':18,
  9218. 'CapsLock':20,
  9219. 'Esc':27,
  9220. 'Spacebar':32,
  9221. 'PageUp':33,
  9222. 'PageDown':34,
  9223. 'End':35,
  9224. 'Home':36,
  9225. 'Left':37,
  9226. 'Up':38,
  9227. 'Right':39,
  9228. 'Down':40,
  9229. 'Insert':45,
  9230. 'Del':46,
  9231. 'NumLock':144,
  9232. 'Cmd':91,
  9233. '=':187,
  9234. '-':189,
  9235. "b":66,
  9236. 'i':73,
  9237. //回退
  9238. 'z':90,
  9239. 'y':89,
  9240. //粘贴
  9241. 'v' : 86,
  9242. 'x' : 88,
  9243. 's' : 83,
  9244. 'n' : 78
  9245. };
  9246. // core/localstorage.js
  9247. //存储媒介封装
  9248. var LocalStorage = UE.LocalStorage = (function () {
  9249. var storage = window.localStorage || getUserData() || null,
  9250. LOCAL_FILE = 'localStorage';
  9251. return {
  9252. saveLocalData: function (key, data) {
  9253. if (storage && data) {
  9254. storage.setItem(key, data);
  9255. return true;
  9256. }
  9257. return false;
  9258. },
  9259. getLocalData: function (key) {
  9260. if (storage) {
  9261. return storage.getItem(key);
  9262. }
  9263. return null;
  9264. },
  9265. removeItem: function (key) {
  9266. storage && storage.removeItem(key);
  9267. }
  9268. };
  9269. function getUserData() {
  9270. var container = document.createElement("div");
  9271. container.style.display = "none";
  9272. if (!container.addBehavior) {
  9273. return null;
  9274. }
  9275. container.addBehavior("#default#userdata");
  9276. return {
  9277. getItem: function (key) {
  9278. var result = null;
  9279. try {
  9280. document.body.appendChild(container);
  9281. container.load(LOCAL_FILE);
  9282. result = container.getAttribute(key);
  9283. document.body.removeChild(container);
  9284. } catch (e) {
  9285. }
  9286. return result;
  9287. },
  9288. setItem: function (key, value) {
  9289. document.body.appendChild(container);
  9290. container.setAttribute(key, value);
  9291. container.save(LOCAL_FILE);
  9292. document.body.removeChild(container);
  9293. },
  9294. //// 暂时没有用到
  9295. //clear: function () {
  9296. //
  9297. // var expiresTime = new Date();
  9298. // expiresTime.setFullYear(expiresTime.getFullYear() - 1);
  9299. // document.body.appendChild(container);
  9300. // container.expires = expiresTime.toUTCString();
  9301. // container.save(LOCAL_FILE);
  9302. // document.body.removeChild(container);
  9303. //
  9304. //},
  9305. removeItem: function (key) {
  9306. document.body.appendChild(container);
  9307. container.removeAttribute(key);
  9308. container.save(LOCAL_FILE);
  9309. document.body.removeChild(container);
  9310. }
  9311. };
  9312. }
  9313. })();
  9314. (function () {
  9315. var ROOTKEY = 'ueditor_preference';
  9316. UE.Editor.prototype.setPreferences = function(key,value){
  9317. var obj = {};
  9318. if (utils.isString(key)) {
  9319. obj[ key ] = value;
  9320. } else {
  9321. obj = key;
  9322. }
  9323. var data = LocalStorage.getLocalData(ROOTKEY);
  9324. if (data && (data = utils.str2json(data))) {
  9325. utils.extend(data, obj);
  9326. } else {
  9327. data = obj;
  9328. }
  9329. data && LocalStorage.saveLocalData(ROOTKEY, utils.json2str(data));
  9330. };
  9331. UE.Editor.prototype.getPreferences = function(key){
  9332. var data = LocalStorage.getLocalData(ROOTKEY);
  9333. if (data && (data = utils.str2json(data))) {
  9334. return key ? data[key] : data
  9335. }
  9336. return null;
  9337. };
  9338. UE.Editor.prototype.removePreferences = function (key) {
  9339. var data = LocalStorage.getLocalData(ROOTKEY);
  9340. if (data && (data = utils.str2json(data))) {
  9341. data[key] = undefined;
  9342. delete data[key]
  9343. }
  9344. data && LocalStorage.saveLocalData(ROOTKEY, utils.json2str(data));
  9345. };
  9346. })();
  9347. // plugins/defaultfilter.js
  9348. ///import core
  9349. ///plugin 编辑器默认的过滤转换机制
  9350. UE.plugins['defaultfilter'] = function () {
  9351. var me = this;
  9352. me.setOpt({
  9353. 'allowDivTransToP':true,
  9354. 'disabledTableInTable':true
  9355. });
  9356. //默认的过滤处理
  9357. //进入编辑器的内容处理
  9358. me.addInputRule(function (root) {
  9359. var allowDivTransToP = this.options.allowDivTransToP;
  9360. var val;
  9361. function tdParent(node){
  9362. while(node && node.type == 'element'){
  9363. if(node.tagName == 'td'){
  9364. return true;
  9365. }
  9366. node = node.parentNode;
  9367. }
  9368. return false;
  9369. }
  9370. //进行默认的处理
  9371. root.traversal(function (node) {
  9372. if (node.type == 'element') {
  9373. if (!dtd.$cdata[node.tagName] && me.options.autoClearEmptyNode && dtd.$inline[node.tagName] && !dtd.$empty[node.tagName] && (!node.attrs || utils.isEmptyObject(node.attrs))) {
  9374. if (!node.firstChild()) node.parentNode.removeChild(node);
  9375. else if (node.tagName == 'span' && (!node.attrs || utils.isEmptyObject(node.attrs))) {
  9376. node.parentNode.removeChild(node, true)
  9377. }
  9378. return;
  9379. }
  9380. switch (node.tagName) {
  9381. case 'style':
  9382. case 'script':
  9383. node.setAttr({
  9384. cdata_tag: node.tagName,
  9385. cdata_data: (node.innerHTML() || ''),
  9386. '_ue_custom_node_':'true'
  9387. });
  9388. node.tagName = 'div';
  9389. node.innerHTML('');
  9390. break;
  9391. case 'a':
  9392. if (val = node.getAttr('href')) {
  9393. node.setAttr('_href', val)
  9394. }
  9395. break;
  9396. case 'img':
  9397. //todo base64暂时去掉,后边做远程图片上传后,干掉这个
  9398. if (val = node.getAttr('src')) {
  9399. if (/^data:/.test(val)) {
  9400. node.parentNode.removeChild(node);
  9401. break;
  9402. }
  9403. }
  9404. node.setAttr('_src', node.getAttr('src'));
  9405. break;
  9406. case 'span':
  9407. if (browser.webkit && (val = node.getStyle('white-space'))) {
  9408. if (/nowrap|normal/.test(val)) {
  9409. node.setStyle('white-space', '');
  9410. if (me.options.autoClearEmptyNode && utils.isEmptyObject(node.attrs)) {
  9411. node.parentNode.removeChild(node, true)
  9412. }
  9413. }
  9414. }
  9415. val = node.getAttr('id');
  9416. if(val && /^_baidu_bookmark_/i.test(val)){
  9417. node.parentNode.removeChild(node)
  9418. }
  9419. break;
  9420. case 'p':
  9421. if (val = node.getAttr('align')) {
  9422. node.setAttr('align');
  9423. node.setStyle('text-align', val)
  9424. }
  9425. //trace:3431
  9426. // var cssStyle = node.getAttr('style');
  9427. // if (cssStyle) {
  9428. // cssStyle = cssStyle.replace(/(margin|padding)[^;]+/g, '');
  9429. // node.setAttr('style', cssStyle)
  9430. //
  9431. // }
  9432. //p标签不允许嵌套
  9433. utils.each(node.children,function(n){
  9434. if(n.type == 'element' && n.tagName == 'p'){
  9435. var next = n.nextSibling();
  9436. node.parentNode.insertAfter(n,node);
  9437. var last = n;
  9438. while(next){
  9439. var tmp = next.nextSibling();
  9440. node.parentNode.insertAfter(next,last);
  9441. last = next;
  9442. next = tmp;
  9443. }
  9444. return false;
  9445. }
  9446. });
  9447. if (!node.firstChild()) {
  9448. node.innerHTML(browser.ie ? '&nbsp;' : '<br/>')
  9449. }
  9450. break;
  9451. case 'div':
  9452. if(node.getAttr('cdata_tag')){
  9453. break;
  9454. }
  9455. //针对代码这里不处理插入代码的div
  9456. val = node.getAttr('class');
  9457. if(val && /^line number\d+/.test(val)){
  9458. break;
  9459. }
  9460. if(!allowDivTransToP){
  9461. break;
  9462. }
  9463. var tmpNode, p = UE.uNode.createElement('p');
  9464. while (tmpNode = node.firstChild()) {
  9465. if (tmpNode.type == 'text' || !UE.dom.dtd.$block[tmpNode.tagName]) {
  9466. p.appendChild(tmpNode);
  9467. } else {
  9468. if (p.firstChild()) {
  9469. node.parentNode.insertBefore(p, node);
  9470. p = UE.uNode.createElement('p');
  9471. } else {
  9472. node.parentNode.insertBefore(tmpNode, node);
  9473. }
  9474. }
  9475. }
  9476. if (p.firstChild()) {
  9477. node.parentNode.insertBefore(p, node);
  9478. }
  9479. node.parentNode.removeChild(node);
  9480. break;
  9481. case 'dl':
  9482. node.tagName = 'ul';
  9483. break;
  9484. case 'dt':
  9485. case 'dd':
  9486. node.tagName = 'li';
  9487. break;
  9488. case 'li':
  9489. var className = node.getAttr('class');
  9490. if (!className || !/list\-/.test(className)) {
  9491. node.setAttr()
  9492. }
  9493. var tmpNodes = node.getNodesByTagName('ol ul');
  9494. UE.utils.each(tmpNodes, function (n) {
  9495. node.parentNode.insertAfter(n, node);
  9496. });
  9497. break;
  9498. case 'td':
  9499. case 'th':
  9500. case 'caption':
  9501. if(!node.children || !node.children.length){
  9502. node.appendChild(browser.ie11below ? UE.uNode.createText(' ') : UE.uNode.createElement('br'))
  9503. }
  9504. break;
  9505. case 'table':
  9506. if(me.options.disabledTableInTable && tdParent(node)){
  9507. node.parentNode.insertBefore(UE.uNode.createText(node.innerText()),node);
  9508. node.parentNode.removeChild(node)
  9509. }
  9510. }
  9511. }
  9512. // if(node.type == 'comment'){
  9513. // node.parentNode.removeChild(node);
  9514. // }
  9515. })
  9516. });
  9517. //从编辑器出去的内容处理
  9518. me.addOutputRule(function (root) {
  9519. var val;
  9520. root.traversal(function (node) {
  9521. if (node.type == 'element') {
  9522. if (me.options.autoClearEmptyNode && dtd.$inline[node.tagName] && !dtd.$empty[node.tagName] && (!node.attrs || utils.isEmptyObject(node.attrs))) {
  9523. if (!node.firstChild()) node.parentNode.removeChild(node);
  9524. else if (node.tagName == 'span' && (!node.attrs || utils.isEmptyObject(node.attrs))) {
  9525. node.parentNode.removeChild(node, true)
  9526. }
  9527. return;
  9528. }
  9529. switch (node.tagName) {
  9530. case 'div':
  9531. if (val = node.getAttr('cdata_tag')) {
  9532. node.tagName = val;
  9533. node.appendChild(UE.uNode.createText(node.getAttr('cdata_data')));
  9534. node.setAttr({cdata_tag: '', cdata_data: '','_ue_custom_node_':''});
  9535. }
  9536. break;
  9537. case 'a':
  9538. if (val = node.getAttr('_href')) {
  9539. node.setAttr({
  9540. 'href': utils.html(val),
  9541. '_href': ''
  9542. })
  9543. }
  9544. break;
  9545. break;
  9546. case 'span':
  9547. val = node.getAttr('id');
  9548. if(val && /^_baidu_bookmark_/i.test(val)){
  9549. node.parentNode.removeChild(node)
  9550. }
  9551. break;
  9552. case 'img':
  9553. if (val = node.getAttr('_src')) {
  9554. node.setAttr({
  9555. 'src': node.getAttr('_src'),
  9556. '_src': ''
  9557. })
  9558. }
  9559. }
  9560. }
  9561. })
  9562. });
  9563. };
  9564. // plugins/inserthtml.js
  9565. /**
  9566. * 插入html字符串插件
  9567. * @file
  9568. * @since 1.2.6.1
  9569. */
  9570. /**
  9571. * 插入html代码
  9572. * @command inserthtml
  9573. * @method execCommand
  9574. * @param { String } cmd 命令字符串
  9575. * @param { String } html 插入的html字符串
  9576. * @remaind 插入的标签内容是在当前的选区位置上插入,如果当前是闭合状态,那直接插入内容, 如果当前是选中状态,将先清除当前选中内容后,再做插入
  9577. * @warning 注意:该命令会对当前选区的位置,对插入的内容进行过滤转换处理。 过滤的规则遵循html语意化的原则。
  9578. * @example
  9579. * ```javascript
  9580. * //xxx[BB]xxx 当前选区为非闭合选区,选中BB这两个文本
  9581. * //执行命令,插入<b>CC</b>
  9582. * //插入后的效果 xxx<b>CC</b>xxx
  9583. * //<p>xx|xxx</p> 当前选区为闭合状态
  9584. * //插入<p>CC</p>
  9585. * //结果 <p>xx</p><p>CC</p><p>xxx</p>
  9586. * //<p>xxxx</p>|</p>xxx</p> 当前选区在两个p标签之间
  9587. * //插入 xxxx
  9588. * //结果 <p>xxxx</p><p>xxxx</p></p>xxx</p>
  9589. * ```
  9590. */
  9591. UE.commands['inserthtml'] = {
  9592. execCommand: function (command,html,notNeedFilter){
  9593. var me = this,
  9594. range,
  9595. div;
  9596. if(!html){
  9597. return;
  9598. }
  9599. if(me.fireEvent('beforeinserthtml',html) === true){
  9600. return;
  9601. }
  9602. range = me.selection.getRange();
  9603. div = range.document.createElement( 'div' );
  9604. div.style.display = 'inline';
  9605. if (!notNeedFilter) {
  9606. var root = UE.htmlparser(html);
  9607. //如果给了过滤规则就先进行过滤
  9608. if(me.options.filterRules){
  9609. UE.filterNode(root,me.options.filterRules);
  9610. }
  9611. //执行默认的处理
  9612. me.filterInputRule(root);
  9613. html = root.toHtml()
  9614. }
  9615. div.innerHTML = utils.trim( html );
  9616. if ( !range.collapsed ) {
  9617. var tmpNode = range.startContainer;
  9618. if(domUtils.isFillChar(tmpNode)){
  9619. range.setStartBefore(tmpNode)
  9620. }
  9621. tmpNode = range.endContainer;
  9622. if(domUtils.isFillChar(tmpNode)){
  9623. range.setEndAfter(tmpNode)
  9624. }
  9625. range.txtToElmBoundary();
  9626. //结束边界可能放到了br的前边,要把br包含进来
  9627. // x[xxx]<br/>
  9628. if(range.endContainer && range.endContainer.nodeType == 1){
  9629. tmpNode = range.endContainer.childNodes[range.endOffset];
  9630. if(tmpNode && domUtils.isBr(tmpNode)){
  9631. range.setEndAfter(tmpNode);
  9632. }
  9633. }
  9634. if(range.startOffset == 0){
  9635. tmpNode = range.startContainer;
  9636. if(domUtils.isBoundaryNode(tmpNode,'firstChild') ){
  9637. tmpNode = range.endContainer;
  9638. if(range.endOffset == (tmpNode.nodeType == 3 ? tmpNode.nodeValue.length : tmpNode.childNodes.length) && domUtils.isBoundaryNode(tmpNode,'lastChild')){
  9639. me.body.innerHTML = '<p>'+(browser.ie ? '' : '<br/>')+'</p>';
  9640. range.setStart(me.body.firstChild,0).collapse(true)
  9641. }
  9642. }
  9643. }
  9644. !range.collapsed && range.deleteContents();
  9645. if(range.startContainer.nodeType == 1){
  9646. var child = range.startContainer.childNodes[range.startOffset],pre;
  9647. if(child && domUtils.isBlockElm(child) && (pre = child.previousSibling) && domUtils.isBlockElm(pre)){
  9648. range.setEnd(pre,pre.childNodes.length).collapse();
  9649. while(child.firstChild){
  9650. pre.appendChild(child.firstChild);
  9651. }
  9652. domUtils.remove(child);
  9653. }
  9654. }
  9655. }
  9656. var child,parent,pre,tmp,hadBreak = 0, nextNode;
  9657. //如果当前位置选中了fillchar要干掉,要不会产生空行
  9658. if(range.inFillChar()){
  9659. child = range.startContainer;
  9660. if(domUtils.isFillChar(child)){
  9661. range.setStartBefore(child).collapse(true);
  9662. domUtils.remove(child);
  9663. }else if(domUtils.isFillChar(child,true)){
  9664. child.nodeValue = child.nodeValue.replace(fillCharReg,'');
  9665. range.startOffset--;
  9666. range.collapsed && range.collapse(true)
  9667. }
  9668. }
  9669. //列表单独处理
  9670. var li = domUtils.findParentByTagName(range.startContainer,'li',true);
  9671. if(li){
  9672. var next,last;
  9673. while(child = div.firstChild){
  9674. //针对hr单独处理一下先
  9675. while(child && (child.nodeType == 3 || !domUtils.isBlockElm(child) || child.tagName=='HR' )){
  9676. next = child.nextSibling;
  9677. range.insertNode( child).collapse();
  9678. last = child;
  9679. child = next;
  9680. }
  9681. if(child){
  9682. if(/^(ol|ul)$/i.test(child.tagName)){
  9683. while(child.firstChild){
  9684. last = child.firstChild;
  9685. domUtils.insertAfter(li,child.firstChild);
  9686. li = li.nextSibling;
  9687. }
  9688. domUtils.remove(child)
  9689. }else{
  9690. var tmpLi;
  9691. next = child.nextSibling;
  9692. tmpLi = me.document.createElement('li');
  9693. domUtils.insertAfter(li,tmpLi);
  9694. tmpLi.appendChild(child);
  9695. last = child;
  9696. child = next;
  9697. li = tmpLi;
  9698. }
  9699. }
  9700. }
  9701. li = domUtils.findParentByTagName(range.startContainer,'li',true);
  9702. if(domUtils.isEmptyBlock(li)){
  9703. domUtils.remove(li)
  9704. }
  9705. if(last){
  9706. range.setStartAfter(last).collapse(true).select(true)
  9707. }
  9708. }else{
  9709. while ( child = div.firstChild ) {
  9710. if(hadBreak){
  9711. var p = me.document.createElement('p');
  9712. while(child && (child.nodeType == 3 || !dtd.$block[child.tagName])){
  9713. nextNode = child.nextSibling;
  9714. p.appendChild(child);
  9715. child = nextNode;
  9716. }
  9717. if(p.firstChild){
  9718. child = p
  9719. }
  9720. }
  9721. range.insertNode( child );
  9722. nextNode = child.nextSibling;
  9723. if ( !hadBreak && child.nodeType == domUtils.NODE_ELEMENT && domUtils.isBlockElm( child ) ){
  9724. parent = domUtils.findParent( child,function ( node ){ return domUtils.isBlockElm( node ); } );
  9725. if ( parent && parent.tagName.toLowerCase() != 'body' && !(dtd[parent.tagName][child.nodeName] && child.parentNode === parent)){
  9726. if(!dtd[parent.tagName][child.nodeName]){
  9727. pre = parent;
  9728. }else{
  9729. tmp = child.parentNode;
  9730. while (tmp !== parent){
  9731. pre = tmp;
  9732. tmp = tmp.parentNode;
  9733. }
  9734. }
  9735. domUtils.breakParent( child, pre || tmp );
  9736. //去掉break后前一个多余的节点 <p>|<[p> ==> <p></p><div></div><p>|</p>
  9737. var pre = child.previousSibling;
  9738. domUtils.trimWhiteTextNode(pre);
  9739. if(!pre.childNodes.length){
  9740. domUtils.remove(pre);
  9741. }
  9742. //trace:2012,在非ie的情况,切开后剩下的节点有可能不能点入光标添加br占位
  9743. if(!browser.ie &&
  9744. (next = child.nextSibling) &&
  9745. domUtils.isBlockElm(next) &&
  9746. next.lastChild &&
  9747. !domUtils.isBr(next.lastChild)){
  9748. next.appendChild(me.document.createElement('br'));
  9749. }
  9750. hadBreak = 1;
  9751. }
  9752. }
  9753. var next = child.nextSibling;
  9754. if(!div.firstChild && next && domUtils.isBlockElm(next)){
  9755. range.setStart(next,0).collapse(true);
  9756. break;
  9757. }
  9758. range.setEndAfter( child ).collapse();
  9759. }
  9760. child = range.startContainer;
  9761. if(nextNode && domUtils.isBr(nextNode)){
  9762. domUtils.remove(nextNode)
  9763. }
  9764. //用chrome可能有空白展位符
  9765. if(domUtils.isBlockElm(child) && domUtils.isEmptyNode(child)){
  9766. if(nextNode = child.nextSibling){
  9767. domUtils.remove(child);
  9768. if(nextNode.nodeType == 1 && dtd.$block[nextNode.tagName]){
  9769. range.setStart(nextNode,0).collapse(true).shrinkBoundary()
  9770. }
  9771. }else{
  9772. try{
  9773. child.innerHTML = browser.ie ? domUtils.fillChar : '<br/>';
  9774. }catch(e){
  9775. range.setStartBefore(child);
  9776. domUtils.remove(child)
  9777. }
  9778. }
  9779. }
  9780. //加上true因为在删除表情等时会删两次,第一次是删的fillData
  9781. try{
  9782. range.select(true);
  9783. }catch(e){}
  9784. }
  9785. setTimeout(function(){
  9786. range = me.selection.getRange();
  9787. range.scrollToView(me.autoHeightEnabled,me.autoHeightEnabled ? domUtils.getXY(me.iframe).y:0);
  9788. me.fireEvent('afterinserthtml', html);
  9789. },200);
  9790. }
  9791. };
  9792. // plugins/autotypeset.js
  9793. /**
  9794. * 自动排版
  9795. * @file
  9796. * @since 1.2.6.1
  9797. */
  9798. /**
  9799. * 对当前编辑器的内容执行自动排版, 排版的行为根据config配置文件里的“autotypeset”选项进行控制。
  9800. * @command autotypeset
  9801. * @method execCommand
  9802. * @param { String } cmd 命令字符串
  9803. * @example
  9804. * ```javascript
  9805. * editor.execCommand( 'autotypeset' );
  9806. * ```
  9807. */
  9808. UE.plugins['autotypeset'] = function(){
  9809. this.setOpt({'autotypeset': {
  9810. mergeEmptyline: true, //合并空行
  9811. removeClass: true, //去掉冗余的class
  9812. removeEmptyline: false, //去掉空行
  9813. textAlign:"left", //段落的排版方式,可以是 left,right,center,justify 去掉这个属性表示不执行排版
  9814. imageBlockLine: 'center', //图片的浮动方式,独占一行剧中,左右浮动,默认: center,left,right,none 去掉这个属性表示不执行排版
  9815. pasteFilter: false, //根据规则过滤没事粘贴进来的内容
  9816. clearFontSize: false, //去掉所有的内嵌字号,使用编辑器默认的字号
  9817. clearFontFamily: false, //去掉所有的内嵌字体,使用编辑器默认的字体
  9818. removeEmptyNode: false, // 去掉空节点
  9819. //可以去掉的标签
  9820. removeTagNames: utils.extend({div:1},dtd.$removeEmpty),
  9821. indent: false, // 行首缩进
  9822. indentValue : '2em', //行首缩进的大小
  9823. bdc2sb: false,
  9824. tobdc: false
  9825. }});
  9826. var me = this,
  9827. opt = me.options.autotypeset,
  9828. remainClass = {
  9829. 'selectTdClass':1,
  9830. 'pagebreak':1,
  9831. 'anchorclass':1
  9832. },
  9833. remainTag = {
  9834. 'li':1
  9835. },
  9836. tags = {
  9837. div:1,
  9838. p:1,
  9839. //trace:2183 这些也认为是行
  9840. blockquote:1,center:1,h1:1,h2:1,h3:1,h4:1,h5:1,h6:1,
  9841. span:1
  9842. },
  9843. highlightCont;
  9844. //升级了版本,但配置项目里没有autotypeset
  9845. if(!opt){
  9846. return;
  9847. }
  9848. readLocalOpts();
  9849. function isLine(node,notEmpty){
  9850. if(!node || node.nodeType == 3)
  9851. return 0;
  9852. if(domUtils.isBr(node))
  9853. return 1;
  9854. if(node && node.parentNode && tags[node.tagName.toLowerCase()]){
  9855. if(highlightCont && highlightCont.contains(node)
  9856. ||
  9857. node.getAttribute('pagebreak')
  9858. ){
  9859. return 0;
  9860. }
  9861. return notEmpty ? !domUtils.isEmptyBlock(node) : domUtils.isEmptyBlock(node,new RegExp('[\\s'+domUtils.fillChar
  9862. +']','g'));
  9863. }
  9864. }
  9865. function removeNotAttributeSpan(node){
  9866. if(!node.style.cssText){
  9867. domUtils.removeAttributes(node,['style']);
  9868. if(node.tagName.toLowerCase() == 'span' && domUtils.hasNoAttributes(node)){
  9869. domUtils.remove(node,true);
  9870. }
  9871. }
  9872. }
  9873. function autotype(type,html){
  9874. var me = this,cont;
  9875. if(html){
  9876. if(!opt.pasteFilter){
  9877. return;
  9878. }
  9879. cont = me.document.createElement('div');
  9880. cont.innerHTML = html.html;
  9881. }else{
  9882. cont = me.document.body;
  9883. }
  9884. var nodes = domUtils.getElementsByTagName(cont,'*');
  9885. // 行首缩进,段落方向,段间距,段内间距
  9886. for(var i=0,ci;ci=nodes[i++];){
  9887. if(me.fireEvent('excludeNodeinautotype',ci) === true){
  9888. continue;
  9889. }
  9890. //font-size
  9891. if(opt.clearFontSize && ci.style.fontSize){
  9892. domUtils.removeStyle(ci,'font-size');
  9893. removeNotAttributeSpan(ci);
  9894. }
  9895. //font-family
  9896. if(opt.clearFontFamily && ci.style.fontFamily){
  9897. domUtils.removeStyle(ci,'font-family');
  9898. removeNotAttributeSpan(ci);
  9899. }
  9900. if(isLine(ci)){
  9901. //合并空行
  9902. if(opt.mergeEmptyline ){
  9903. var next = ci.nextSibling,tmpNode,isBr = domUtils.isBr(ci);
  9904. while(isLine(next)){
  9905. tmpNode = next;
  9906. next = tmpNode.nextSibling;
  9907. if(isBr && (!next || next && !domUtils.isBr(next))){
  9908. break;
  9909. }
  9910. domUtils.remove(tmpNode);
  9911. }
  9912. }
  9913. //去掉空行,保留占位的空行
  9914. if(opt.removeEmptyline && domUtils.inDoc(ci,cont) && !remainTag[ci.parentNode.tagName.toLowerCase()] ){
  9915. if(domUtils.isBr(ci)){
  9916. next = ci.nextSibling;
  9917. if(next && !domUtils.isBr(next)){
  9918. continue;
  9919. }
  9920. }
  9921. domUtils.remove(ci);
  9922. continue;
  9923. }
  9924. }
  9925. if(isLine(ci,true) && ci.tagName != 'SPAN'){
  9926. if(opt.indent){
  9927. ci.style.textIndent = opt.indentValue;
  9928. }
  9929. if(opt.textAlign){
  9930. ci.style.textAlign = opt.textAlign;
  9931. }
  9932. // if(opt.lineHeight)
  9933. // ci.style.lineHeight = opt.lineHeight + 'cm';
  9934. }
  9935. //去掉class,保留的class不去掉
  9936. if(opt.removeClass && ci.className && !remainClass[ci.className.toLowerCase()]){
  9937. if(highlightCont && highlightCont.contains(ci)){
  9938. continue;
  9939. }
  9940. domUtils.removeAttributes(ci,['class']);
  9941. }
  9942. //表情不处理
  9943. if(opt.imageBlockLine && ci.tagName.toLowerCase() == 'img' && !ci.getAttribute('emotion')){
  9944. if(html){
  9945. var img = ci;
  9946. switch (opt.imageBlockLine){
  9947. case 'left':
  9948. case 'right':
  9949. case 'none':
  9950. var pN = img.parentNode,tmpNode,pre,next;
  9951. while(dtd.$inline[pN.tagName] || pN.tagName == 'A'){
  9952. pN = pN.parentNode;
  9953. }
  9954. tmpNode = pN;
  9955. if(tmpNode.tagName == 'P' && domUtils.getStyle(tmpNode,'text-align') == 'center'){
  9956. if(!domUtils.isBody(tmpNode) && domUtils.getChildCount(tmpNode,function(node){return !domUtils.isBr(node) && !domUtils.isWhitespace(node)}) == 1){
  9957. pre = tmpNode.previousSibling;
  9958. next = tmpNode.nextSibling;
  9959. if(pre && next && pre.nodeType == 1 && next.nodeType == 1 && pre.tagName == next.tagName && domUtils.isBlockElm(pre)){
  9960. pre.appendChild(tmpNode.firstChild);
  9961. while(next.firstChild){
  9962. pre.appendChild(next.firstChild);
  9963. }
  9964. domUtils.remove(tmpNode);
  9965. domUtils.remove(next);
  9966. }else{
  9967. domUtils.setStyle(tmpNode,'text-align','');
  9968. }
  9969. }
  9970. }
  9971. domUtils.setStyle(img,'float', opt.imageBlockLine);
  9972. break;
  9973. case 'center':
  9974. if(me.queryCommandValue('imagefloat') != 'center'){
  9975. pN = img.parentNode;
  9976. domUtils.setStyle(img,'float','none');
  9977. tmpNode = img;
  9978. while(pN && domUtils.getChildCount(pN,function(node){return !domUtils.isBr(node) && !domUtils.isWhitespace(node)}) == 1
  9979. && (dtd.$inline[pN.tagName] || pN.tagName == 'A')){
  9980. tmpNode = pN;
  9981. pN = pN.parentNode;
  9982. }
  9983. var pNode = me.document.createElement('p');
  9984. domUtils.setAttributes(pNode,{
  9985. style:'text-align:center'
  9986. });
  9987. tmpNode.parentNode.insertBefore(pNode,tmpNode);
  9988. pNode.appendChild(tmpNode);
  9989. domUtils.setStyle(tmpNode,'float','');
  9990. }
  9991. }
  9992. } else {
  9993. var range = me.selection.getRange();
  9994. range.selectNode(ci).select();
  9995. me.execCommand('imagefloat', opt.imageBlockLine);
  9996. }
  9997. }
  9998. //去掉冗余的标签
  9999. if(opt.removeEmptyNode){
  10000. if(opt.removeTagNames[ci.tagName.toLowerCase()] && domUtils.hasNoAttributes(ci) && domUtils.isEmptyBlock(ci)){
  10001. domUtils.remove(ci);
  10002. }
  10003. }
  10004. }
  10005. if(opt.tobdc){
  10006. var root = UE.htmlparser(cont.innerHTML);
  10007. root.traversal(function(node){
  10008. if(node.type == 'text'){
  10009. node.data = ToDBC(node.data)
  10010. }
  10011. });
  10012. cont.innerHTML = root.toHtml()
  10013. }
  10014. if(opt.bdc2sb){
  10015. var root = UE.htmlparser(cont.innerHTML);
  10016. root.traversal(function(node){
  10017. if(node.type == 'text'){
  10018. node.data = DBC2SB(node.data)
  10019. }
  10020. });
  10021. cont.innerHTML = root.toHtml()
  10022. }
  10023. if(html){
  10024. html.html = cont.innerHTML;
  10025. }
  10026. }
  10027. if(opt.pasteFilter){
  10028. me.addListener('beforepaste',autotype);
  10029. }
  10030. function DBC2SB(str) {
  10031. var result = '';
  10032. for (var i = 0; i < str.length; i++) {
  10033. var code = str.charCodeAt(i); //获取当前字符的unicode编码
  10034. if (code >= 65281 && code <= 65373)//在这个unicode编码范围中的是所有的英文字母已经各种字符
  10035. {
  10036. result += String.fromCharCode(str.charCodeAt(i) - 65248); //把全角字符的unicode编码转换为对应半角字符的unicode码
  10037. } else if (code == 12288)//空格
  10038. {
  10039. result += String.fromCharCode(str.charCodeAt(i) - 12288 + 32);
  10040. } else {
  10041. result += str.charAt(i);
  10042. }
  10043. }
  10044. return result;
  10045. }
  10046. function ToDBC(txtstring) {
  10047. txtstring = utils.html(txtstring);
  10048. var tmp = "";
  10049. var mark = "";/*用于判断,如果是html尖括里的标记,则不进行全角的转换*/
  10050. for (var i = 0; i < txtstring.length; i++) {
  10051. if (txtstring.charCodeAt(i) == 32) {
  10052. tmp = tmp + String.fromCharCode(12288);
  10053. }
  10054. else if (txtstring.charCodeAt(i) < 127) {
  10055. tmp = tmp + String.fromCharCode(txtstring.charCodeAt(i) + 65248);
  10056. }
  10057. else {
  10058. tmp += txtstring.charAt(i);
  10059. }
  10060. }
  10061. return tmp;
  10062. }
  10063. function readLocalOpts() {
  10064. var cookieOpt = me.getPreferences('autotypeset');
  10065. utils.extend(me.options.autotypeset, cookieOpt);
  10066. }
  10067. me.commands['autotypeset'] = {
  10068. execCommand:function () {
  10069. me.removeListener('beforepaste',autotype);
  10070. if(opt.pasteFilter){
  10071. me.addListener('beforepaste',autotype);
  10072. }
  10073. autotype.call(me)
  10074. }
  10075. };
  10076. };
  10077. // plugins/autosubmit.js
  10078. /**
  10079. * 快捷键提交
  10080. * @file
  10081. * @since 1.2.6.1
  10082. */
  10083. /**
  10084. * 提交表单
  10085. * @command autosubmit
  10086. * @method execCommand
  10087. * @param { String } cmd 命令字符串
  10088. * @example
  10089. * ```javascript
  10090. * editor.execCommand( 'autosubmit' );
  10091. * ```
  10092. */
  10093. UE.plugin.register('autosubmit',function(){
  10094. return {
  10095. shortcutkey:{
  10096. "autosubmit":"ctrl+13" //手动提交
  10097. },
  10098. commands:{
  10099. 'autosubmit':{
  10100. execCommand:function () {
  10101. var me=this,
  10102. form = domUtils.findParentByTagName(me.iframe,"form", false);
  10103. if (form){
  10104. if(me.fireEvent("beforesubmit")===false){
  10105. return;
  10106. }
  10107. me.sync();
  10108. form.submit();
  10109. }
  10110. }
  10111. }
  10112. }
  10113. }
  10114. });
  10115. // plugins/background.js
  10116. /**
  10117. * 背景插件,为UEditor提供设置背景功能
  10118. * @file
  10119. * @since 1.2.6.1
  10120. */
  10121. UE.plugin.register('background', function () {
  10122. var me = this,
  10123. cssRuleId = 'editor_background',
  10124. isSetColored,
  10125. reg = new RegExp('body[\\s]*\\{(.+)\\}', 'i');
  10126. function stringToObj(str) {
  10127. var obj = {}, styles = str.split(';');
  10128. utils.each(styles, function (v) {
  10129. var index = v.indexOf(':'),
  10130. key = utils.trim(v.substr(0, index)).toLowerCase();
  10131. key && (obj[key] = utils.trim(v.substr(index + 1) || ''));
  10132. });
  10133. return obj;
  10134. }
  10135. function setBackground(obj) {
  10136. if (obj) {
  10137. var styles = [];
  10138. for (var name in obj) {
  10139. if (obj.hasOwnProperty(name)) {
  10140. styles.push(name + ":" + obj[name] + '; ');
  10141. }
  10142. }
  10143. utils.cssRule(cssRuleId, styles.length ? ('body{' + styles.join("") + '}') : '', me.document);
  10144. } else {
  10145. utils.cssRule(cssRuleId, '', me.document)
  10146. }
  10147. }
  10148. //重写editor.hasContent方法
  10149. var orgFn = me.hasContents;
  10150. me.hasContents = function(){
  10151. if(me.queryCommandValue('background')){
  10152. return true
  10153. }
  10154. return orgFn.apply(me,arguments);
  10155. };
  10156. return {
  10157. bindEvents: {
  10158. 'getAllHtml': function (type, headHtml) {
  10159. var body = this.body,
  10160. su = domUtils.getComputedStyle(body, "background-image"),
  10161. url = "";
  10162. if (su.indexOf(me.options.imagePath) > 0) {
  10163. url = su.substring(su.indexOf(me.options.imagePath), su.length - 1).replace(/"|\(|\)/ig, "");
  10164. } else {
  10165. url = su != "none" ? su.replace(/url\("?|"?\)/ig, "") : "";
  10166. }
  10167. var html = '<style type="text/css">body{';
  10168. var bgObj = {
  10169. "background-color": domUtils.getComputedStyle(body, "background-color") || "#ffffff",
  10170. 'background-image': url ? 'url(' + url + ')' : '',
  10171. 'background-repeat': domUtils.getComputedStyle(body, "background-repeat") || "",
  10172. 'background-position': browser.ie ? (domUtils.getComputedStyle(body, "background-position-x") + " " + domUtils.getComputedStyle(body, "background-position-y")) : domUtils.getComputedStyle(body, "background-position"),
  10173. 'height': domUtils.getComputedStyle(body, "height")
  10174. };
  10175. for (var name in bgObj) {
  10176. if (bgObj.hasOwnProperty(name)) {
  10177. html += name + ":" + bgObj[name] + "; ";
  10178. }
  10179. }
  10180. html += '}</style> ';
  10181. headHtml.push(html);
  10182. },
  10183. 'aftersetcontent': function () {
  10184. if(isSetColored == false) setBackground();
  10185. }
  10186. },
  10187. inputRule: function (root) {
  10188. isSetColored = false;
  10189. utils.each(root.getNodesByTagName('p'), function (p) {
  10190. var styles = p.getAttr('data-background');
  10191. if (styles) {
  10192. isSetColored = true;
  10193. setBackground(stringToObj(styles));
  10194. p.parentNode.removeChild(p);
  10195. }
  10196. })
  10197. },
  10198. outputRule: function (root) {
  10199. var me = this,
  10200. styles = (utils.cssRule(cssRuleId, me.document) || '').replace(/[\n\r]+/g, '').match(reg);
  10201. if (styles) {
  10202. root.appendChild(UE.uNode.createElement('<p style="display:none;" data-background="' + utils.trim(styles[1].replace(/"/g, '').replace(/[\s]+/g, ' ')) + '"><br/></p>'));
  10203. }
  10204. },
  10205. commands: {
  10206. 'background': {
  10207. execCommand: function (cmd, obj) {
  10208. setBackground(obj);
  10209. },
  10210. queryCommandValue: function () {
  10211. var me = this,
  10212. styles = (utils.cssRule(cssRuleId, me.document) || '').replace(/[\n\r]+/g, '').match(reg);
  10213. return styles ? stringToObj(styles[1]) : null;
  10214. },
  10215. notNeedUndo: true
  10216. }
  10217. }
  10218. }
  10219. });
  10220. // plugins/image.js
  10221. /**
  10222. * 图片插入、排版插件
  10223. * @file
  10224. * @since 1.2.6.1
  10225. */
  10226. /**
  10227. * 图片对齐方式
  10228. * @command imagefloat
  10229. * @method execCommand
  10230. * @remind 值center为独占一行居中
  10231. * @param { String } cmd 命令字符串
  10232. * @param { String } align 对齐方式,可传left、right、none、center
  10233. * @remaind center表示图片独占一行
  10234. * @example
  10235. * ```javascript
  10236. * editor.execCommand( 'imagefloat', 'center' );
  10237. * ```
  10238. */
  10239. /**
  10240. * 如果选区所在位置是图片区域
  10241. * @command imagefloat
  10242. * @method queryCommandValue
  10243. * @param { String } cmd 命令字符串
  10244. * @return { String } 返回图片对齐方式
  10245. * @example
  10246. * ```javascript
  10247. * editor.queryCommandValue( 'imagefloat' );
  10248. * ```
  10249. */
  10250. UE.commands['imagefloat'] = {
  10251. execCommand:function (cmd, align) {
  10252. var me = this,
  10253. range = me.selection.getRange();
  10254. if (!range.collapsed) {
  10255. var img = range.getClosedNode();
  10256. if (img && img.tagName == 'IMG') {
  10257. switch (align) {
  10258. case 'left':
  10259. case 'right':
  10260. case 'none':
  10261. var pN = img.parentNode, tmpNode, pre, next;
  10262. while (dtd.$inline[pN.tagName] || pN.tagName == 'A') {
  10263. pN = pN.parentNode;
  10264. }
  10265. tmpNode = pN;
  10266. if (tmpNode.tagName == 'P' && domUtils.getStyle(tmpNode, 'text-align') == 'center') {
  10267. if (!domUtils.isBody(tmpNode) && domUtils.getChildCount(tmpNode, function (node) {
  10268. return !domUtils.isBr(node) && !domUtils.isWhitespace(node);
  10269. }) == 1) {
  10270. pre = tmpNode.previousSibling;
  10271. next = tmpNode.nextSibling;
  10272. if (pre && next && pre.nodeType == 1 && next.nodeType == 1 && pre.tagName == next.tagName && domUtils.isBlockElm(pre)) {
  10273. pre.appendChild(tmpNode.firstChild);
  10274. while (next.firstChild) {
  10275. pre.appendChild(next.firstChild);
  10276. }
  10277. domUtils.remove(tmpNode);
  10278. domUtils.remove(next);
  10279. } else {
  10280. domUtils.setStyle(tmpNode, 'text-align', '');
  10281. }
  10282. }
  10283. range.selectNode(img).select();
  10284. }
  10285. domUtils.setStyle(img, 'float', align == 'none' ? '' : align);
  10286. if(align == 'none'){
  10287. domUtils.removeAttributes(img,'align');
  10288. }
  10289. break;
  10290. case 'center':
  10291. if (me.queryCommandValue('imagefloat') != 'center') {
  10292. pN = img.parentNode;
  10293. domUtils.setStyle(img, 'float', '');
  10294. domUtils.removeAttributes(img,'align');
  10295. tmpNode = img;
  10296. while (pN && domUtils.getChildCount(pN, function (node) {
  10297. return !domUtils.isBr(node) && !domUtils.isWhitespace(node);
  10298. }) == 1
  10299. && (dtd.$inline[pN.tagName] || pN.tagName == 'A')) {
  10300. tmpNode = pN;
  10301. pN = pN.parentNode;
  10302. }
  10303. range.setStartBefore(tmpNode).setCursor(false);
  10304. pN = me.document.createElement('div');
  10305. pN.appendChild(tmpNode);
  10306. domUtils.setStyle(tmpNode, 'float', '');
  10307. me.execCommand('insertHtml', '<p id="_img_parent_tmp" style="text-align:center">' + pN.innerHTML + '</p>');
  10308. tmpNode = me.document.getElementById('_img_parent_tmp');
  10309. tmpNode.removeAttribute('id');
  10310. tmpNode = tmpNode.firstChild;
  10311. range.selectNode(tmpNode).select();
  10312. //去掉后边多余的元素
  10313. next = tmpNode.parentNode.nextSibling;
  10314. if (next && domUtils.isEmptyNode(next)) {
  10315. domUtils.remove(next);
  10316. }
  10317. }
  10318. break;
  10319. }
  10320. }
  10321. }
  10322. },
  10323. queryCommandValue:function () {
  10324. var range = this.selection.getRange(),
  10325. startNode, floatStyle;
  10326. if (range.collapsed) {
  10327. return 'none';
  10328. }
  10329. startNode = range.getClosedNode();
  10330. if (startNode && startNode.nodeType == 1 && startNode.tagName == 'IMG') {
  10331. floatStyle = domUtils.getComputedStyle(startNode, 'float') || startNode.getAttribute('align');
  10332. if (floatStyle == 'none') {
  10333. floatStyle = domUtils.getComputedStyle(startNode.parentNode, 'text-align') == 'center' ? 'center' : floatStyle;
  10334. }
  10335. return {
  10336. left:1,
  10337. right:1,
  10338. center:1
  10339. }[floatStyle] ? floatStyle : 'none';
  10340. }
  10341. return 'none';
  10342. },
  10343. queryCommandState:function () {
  10344. var range = this.selection.getRange(),
  10345. startNode;
  10346. if (range.collapsed) return -1;
  10347. startNode = range.getClosedNode();
  10348. if (startNode && startNode.nodeType == 1 && startNode.tagName == 'IMG') {
  10349. return 0;
  10350. }
  10351. return -1;
  10352. }
  10353. };
  10354. /**
  10355. * 插入图片
  10356. * @command insertimage
  10357. * @method execCommand
  10358. * @param { String } cmd 命令字符串
  10359. * @param { Object } opt 属性键值对,这些属性都将被复制到当前插入图片
  10360. * @remind 该命令第二个参数可接受一个图片配置项对象的数组,可以插入多张图片,
  10361. * 此时数组的每一个元素都是一个Object类型的图片属性集合。
  10362. * @example
  10363. * ```javascript
  10364. * editor.execCommand( 'insertimage', {
  10365. * src:'a/b/c.jpg',
  10366. * width:'100',
  10367. * height:'100'
  10368. * } );
  10369. * ```
  10370. * @example
  10371. * ```javascript
  10372. * editor.execCommand( 'insertimage', [{
  10373. * src:'a/b/c.jpg',
  10374. * width:'100',
  10375. * height:'100'
  10376. * },{
  10377. * src:'a/b/d.jpg',
  10378. * width:'100',
  10379. * height:'100'
  10380. * }] );
  10381. * ```
  10382. */
  10383. UE.commands['insertimage'] = {
  10384. execCommand:function (cmd, opt) {
  10385. opt = utils.isArray(opt) ? opt : [opt];
  10386. if (!opt.length) {
  10387. return;
  10388. }
  10389. var me = this,
  10390. range = me.selection.getRange(),
  10391. img = range.getClosedNode();
  10392. if(me.fireEvent('beforeinsertimage', opt) === true){
  10393. return;
  10394. }
  10395. if (img && /img/i.test(img.tagName) && (img.className != "edui-faked-video" || img.className.indexOf("edui-upload-video")!=-1) && !img.getAttribute("word_img")) {
  10396. var first = opt.shift();
  10397. var floatStyle = first['floatStyle'];
  10398. delete first['floatStyle'];
  10399. //// img.style.border = (first.border||0) +"px solid #000";
  10400. //// img.style.margin = (first.margin||0) +"px";
  10401. // img.style.cssText += ';margin:' + (first.margin||0) +"px;" + 'border:' + (first.border||0) +"px solid #000";
  10402. domUtils.setAttributes(img, first);
  10403. me.execCommand('imagefloat', floatStyle);
  10404. if (opt.length > 0) {
  10405. range.setStartAfter(img).setCursor(false, true);
  10406. me.execCommand('insertimage', opt);
  10407. }
  10408. } else {
  10409. var html = [], str = '', ci;
  10410. ci = opt[0];
  10411. if (opt.length == 1) {
  10412. str = '<img src="' + ci.src + '" ' + (ci._src ? ' _src="' + ci._src + '" ' : '') +
  10413. (ci.width ? 'width="' + ci.width + '" ' : '') +
  10414. (ci.height ? ' height="' + ci.height + '" ' : '') +
  10415. (ci['floatStyle'] == 'left' || ci['floatStyle'] == 'right' ? ' style="float:' + ci['floatStyle'] + ';"' : '') +
  10416. (ci.title && ci.title != "" ? ' title="' + ci.title + '"' : '') +
  10417. (ci.border && ci.border != "0" ? ' border="' + ci.border + '"' : '') +
  10418. (ci.alt && ci.alt != "" ? ' alt="' + ci.alt + '"' : '') +
  10419. (ci.hspace && ci.hspace != "0" ? ' hspace = "' + ci.hspace + '"' : '') +
  10420. (ci.vspace && ci.vspace != "0" ? ' vspace = "' + ci.vspace + '"' : '') + '/>';
  10421. if (ci['floatStyle'] == 'center') {
  10422. str = '<p style="text-align: center">' + str + '</p>';
  10423. }
  10424. html.push(str);
  10425. } else {
  10426. for (var i = 0; ci = opt[i++];) {
  10427. str = '<p ' + (ci['floatStyle'] == 'center' ? 'style="text-align: center" ' : '') + '><img src="' + ci.src + '" ' +
  10428. (ci.width ? 'width="' + ci.width + '" ' : '') + (ci._src ? ' _src="' + ci._src + '" ' : '') +
  10429. (ci.height ? ' height="' + ci.height + '" ' : '') +
  10430. ' style="' + (ci['floatStyle'] && ci['floatStyle'] != 'center' ? 'float:' + ci['floatStyle'] + ';' : '') +
  10431. (ci.border || '') + '" ' +
  10432. (ci.title ? ' title="' + ci.title + '"' : '') + ' /></p>';
  10433. html.push(str);
  10434. }
  10435. }
  10436. me.execCommand('insertHtml', html.join(''));
  10437. }
  10438. me.fireEvent('afterinsertimage', opt)
  10439. }
  10440. };
  10441. // plugins/justify.js
  10442. /**
  10443. * 段落格式
  10444. * @file
  10445. * @since 1.2.6.1
  10446. */
  10447. /**
  10448. * 段落对齐方式
  10449. * @command justify
  10450. * @method execCommand
  10451. * @param { String } cmd 命令字符串
  10452. * @param { String } align 对齐方式:left => 居左,right => 居右,center => 居中,justify => 两端对齐
  10453. * @example
  10454. * ```javascript
  10455. * editor.execCommand( 'justify', 'center' );
  10456. * ```
  10457. */
  10458. /**
  10459. * 如果选区所在位置是段落区域,返回当前段落对齐方式
  10460. * @command justify
  10461. * @method queryCommandValue
  10462. * @param { String } cmd 命令字符串
  10463. * @return { String } 返回段落对齐方式
  10464. * @example
  10465. * ```javascript
  10466. * editor.queryCommandValue( 'justify' );
  10467. * ```
  10468. */
  10469. UE.plugins['justify']=function(){
  10470. var me=this,
  10471. block = domUtils.isBlockElm,
  10472. defaultValue = {
  10473. left:1,
  10474. right:1,
  10475. center:1,
  10476. justify:1
  10477. },
  10478. doJustify = function (range, style) {
  10479. var bookmark = range.createBookmark(),
  10480. filterFn = function (node) {
  10481. return node.nodeType == 1 ? node.tagName.toLowerCase() != 'br' && !domUtils.isBookmarkNode(node) : !domUtils.isWhitespace(node);
  10482. };
  10483. range.enlarge(true);
  10484. var bookmark2 = range.createBookmark(),
  10485. current = domUtils.getNextDomNode(bookmark2.start, false, filterFn),
  10486. tmpRange = range.cloneRange(),
  10487. tmpNode;
  10488. while (current && !(domUtils.getPosition(current, bookmark2.end) & domUtils.POSITION_FOLLOWING)) {
  10489. if (current.nodeType == 3 || !block(current)) {
  10490. tmpRange.setStartBefore(current);
  10491. while (current && current !== bookmark2.end && !block(current)) {
  10492. tmpNode = current;
  10493. current = domUtils.getNextDomNode(current, false, null, function (node) {
  10494. return !block(node);
  10495. });
  10496. }
  10497. tmpRange.setEndAfter(tmpNode);
  10498. var common = tmpRange.getCommonAncestor();
  10499. if (!domUtils.isBody(common) && block(common)) {
  10500. domUtils.setStyles(common, utils.isString(style) ? {'text-align':style} : style);
  10501. current = common;
  10502. } else {
  10503. var p = range.document.createElement('p');
  10504. domUtils.setStyles(p, utils.isString(style) ? {'text-align':style} : style);
  10505. var frag = tmpRange.extractContents();
  10506. p.appendChild(frag);
  10507. tmpRange.insertNode(p);
  10508. current = p;
  10509. }
  10510. current = domUtils.getNextDomNode(current, false, filterFn);
  10511. } else {
  10512. current = domUtils.getNextDomNode(current, true, filterFn);
  10513. }
  10514. }
  10515. return range.moveToBookmark(bookmark2).moveToBookmark(bookmark);
  10516. };
  10517. UE.commands['justify'] = {
  10518. execCommand:function (cmdName, align) {
  10519. var range = this.selection.getRange(),
  10520. txt;
  10521. //闭合时单独处理
  10522. if (range.collapsed) {
  10523. txt = this.document.createTextNode('p');
  10524. range.insertNode(txt);
  10525. }
  10526. doJustify(range, align);
  10527. if (txt) {
  10528. range.setStartBefore(txt).collapse(true);
  10529. domUtils.remove(txt);
  10530. }
  10531. range.select();
  10532. return true;
  10533. },
  10534. queryCommandValue:function () {
  10535. var startNode = this.selection.getStart(),
  10536. value = domUtils.getComputedStyle(startNode, 'text-align');
  10537. return defaultValue[value] ? value : 'left';
  10538. },
  10539. queryCommandState:function () {
  10540. var start = this.selection.getStart(),
  10541. cell = start && domUtils.findParentByTagName(start, ["td", "th","caption"], true);
  10542. return cell? -1:0;
  10543. }
  10544. };
  10545. };
  10546. // plugins/font.js
  10547. /**
  10548. * 字体颜色,背景色,字号,字体,下划线,删除线
  10549. * @file
  10550. * @since 1.2.6.1
  10551. */
  10552. /**
  10553. * 字体颜色
  10554. * @command forecolor
  10555. * @method execCommand
  10556. * @param { String } cmd 命令字符串
  10557. * @param { String } value 色值(必须十六进制)
  10558. * @example
  10559. * ```javascript
  10560. * editor.execCommand( 'forecolor', '#000' );
  10561. * ```
  10562. */
  10563. /**
  10564. * 返回选区字体颜色
  10565. * @command forecolor
  10566. * @method queryCommandValue
  10567. * @param { String } cmd 命令字符串
  10568. * @return { String } 返回字体颜色
  10569. * @example
  10570. * ```javascript
  10571. * editor.queryCommandValue( 'forecolor' );
  10572. * ```
  10573. */
  10574. /**
  10575. * 字体背景颜色
  10576. * @command backcolor
  10577. * @method execCommand
  10578. * @param { String } cmd 命令字符串
  10579. * @param { String } value 色值(必须十六进制)
  10580. * @example
  10581. * ```javascript
  10582. * editor.execCommand( 'backcolor', '#000' );
  10583. * ```
  10584. */
  10585. /**
  10586. * 返回选区字体颜色
  10587. * @command backcolor
  10588. * @method queryCommandValue
  10589. * @param { String } cmd 命令字符串
  10590. * @return { String } 返回字体背景颜色
  10591. * @example
  10592. * ```javascript
  10593. * editor.queryCommandValue( 'backcolor' );
  10594. * ```
  10595. */
  10596. /**
  10597. * 字体大小
  10598. * @command fontsize
  10599. * @method execCommand
  10600. * @param { String } cmd 命令字符串
  10601. * @param { String } value 字体大小
  10602. * @example
  10603. * ```javascript
  10604. * editor.execCommand( 'fontsize', '14px' );
  10605. * ```
  10606. */
  10607. /**
  10608. * 返回选区字体大小
  10609. * @command fontsize
  10610. * @method queryCommandValue
  10611. * @param { String } cmd 命令字符串
  10612. * @return { String } 返回字体大小
  10613. * @example
  10614. * ```javascript
  10615. * editor.queryCommandValue( 'fontsize' );
  10616. * ```
  10617. */
  10618. /**
  10619. * 字体样式
  10620. * @command fontfamily
  10621. * @method execCommand
  10622. * @param { String } cmd 命令字符串
  10623. * @param { String } value 字体样式
  10624. * @example
  10625. * ```javascript
  10626. * editor.execCommand( 'fontfamily', '微软雅黑' );
  10627. * ```
  10628. */
  10629. /**
  10630. * 返回选区字体样式
  10631. * @command fontfamily
  10632. * @method queryCommandValue
  10633. * @param { String } cmd 命令字符串
  10634. * @return { String } 返回字体样式
  10635. * @example
  10636. * ```javascript
  10637. * editor.queryCommandValue( 'fontfamily' );
  10638. * ```
  10639. */
  10640. /**
  10641. * 字体下划线,与删除线互斥
  10642. * @command underline
  10643. * @method execCommand
  10644. * @param { String } cmd 命令字符串
  10645. * @example
  10646. * ```javascript
  10647. * editor.execCommand( 'underline' );
  10648. * ```
  10649. */
  10650. /**
  10651. * 字体删除线,与下划线互斥
  10652. * @command strikethrough
  10653. * @method execCommand
  10654. * @param { String } cmd 命令字符串
  10655. * @example
  10656. * ```javascript
  10657. * editor.execCommand( 'strikethrough' );
  10658. * ```
  10659. */
  10660. /**
  10661. * 字体边框
  10662. * @command fontborder
  10663. * @method execCommand
  10664. * @param { String } cmd 命令字符串
  10665. * @example
  10666. * ```javascript
  10667. * editor.execCommand( 'fontborder' );
  10668. * ```
  10669. */
  10670. UE.plugins['font'] = function () {
  10671. var me = this,
  10672. fonts = {
  10673. 'forecolor': 'color',
  10674. 'backcolor': 'background-color',
  10675. 'fontsize': 'font-size',
  10676. 'fontfamily': 'font-family',
  10677. 'underline': 'text-decoration',
  10678. 'strikethrough': 'text-decoration',
  10679. 'fontborder': 'border'
  10680. },
  10681. needCmd = {'underline': 1, 'strikethrough': 1, 'fontborder': 1},
  10682. needSetChild = {
  10683. 'forecolor': 'color',
  10684. 'backcolor': 'background-color',
  10685. 'fontsize': 'font-size',
  10686. 'fontfamily': 'font-family'
  10687. };
  10688. me.setOpt({
  10689. 'fontfamily': [
  10690. { name: 'songti', val: '宋体,SimSun'},
  10691. { name: 'yahei', val: '微软雅黑,Microsoft YaHei'},
  10692. { name: 'kaiti', val: '楷体,楷体_GB2312, SimKai'},
  10693. { name: 'heiti', val: '黑体, SimHei'},
  10694. { name: 'lishu', val: '隶书, SimLi'},
  10695. { name: 'andaleMono', val: 'andale mono'},
  10696. { name: 'arial', val: 'arial, helvetica,sans-serif'},
  10697. { name: 'arialBlack', val: 'arial black,avant garde'},
  10698. { name: 'comicSansMs', val: 'comic sans ms'},
  10699. { name: 'impact', val: 'impact,chicago'},
  10700. { name: 'timesNewRoman', val: 'times new roman'}
  10701. ],
  10702. 'fontsize': [10, 11, 12, 14, 16, 18, 20, 24, 36]
  10703. });
  10704. function mergeWithParent(node){
  10705. var parent;
  10706. while(parent = node.parentNode){
  10707. if(parent.tagName == 'SPAN' && domUtils.getChildCount(parent,function(child){
  10708. return !domUtils.isBookmarkNode(child) && !domUtils.isBr(child)
  10709. }) == 1) {
  10710. parent.style.cssText += node.style.cssText;
  10711. domUtils.remove(node,true);
  10712. node = parent;
  10713. }else{
  10714. break;
  10715. }
  10716. }
  10717. }
  10718. function mergeChild(rng,cmdName,value){
  10719. if(needSetChild[cmdName]){
  10720. rng.adjustmentBoundary();
  10721. if(!rng.collapsed && rng.startContainer.nodeType == 1){
  10722. var start = rng.startContainer.childNodes[rng.startOffset];
  10723. if(start && domUtils.isTagNode(start,'span')){
  10724. var bk = rng.createBookmark();
  10725. utils.each(domUtils.getElementsByTagName(start, 'span'), function (span) {
  10726. if (!span.parentNode || domUtils.isBookmarkNode(span))return;
  10727. if(cmdName == 'backcolor' && domUtils.getComputedStyle(span,'background-color').toLowerCase() === value){
  10728. return;
  10729. }
  10730. domUtils.removeStyle(span,needSetChild[cmdName]);
  10731. if(span.style.cssText.replace(/^\s+$/,'').length == 0){
  10732. domUtils.remove(span,true)
  10733. }
  10734. });
  10735. rng.moveToBookmark(bk)
  10736. }
  10737. }
  10738. }
  10739. }
  10740. function mergesibling(rng,cmdName,value) {
  10741. var collapsed = rng.collapsed,
  10742. bk = rng.createBookmark(), common;
  10743. if (collapsed) {
  10744. common = bk.start.parentNode;
  10745. while (dtd.$inline[common.tagName]) {
  10746. common = common.parentNode;
  10747. }
  10748. } else {
  10749. common = domUtils.getCommonAncestor(bk.start, bk.end);
  10750. }
  10751. utils.each(domUtils.getElementsByTagName(common, 'span'), function (span) {
  10752. if (!span.parentNode || domUtils.isBookmarkNode(span))return;
  10753. if (/\s*border\s*:\s*none;?\s*/i.test(span.style.cssText)) {
  10754. if(/^\s*border\s*:\s*none;?\s*$/.test(span.style.cssText)){
  10755. domUtils.remove(span, true);
  10756. }else{
  10757. domUtils.removeStyle(span,'border');
  10758. }
  10759. return
  10760. }
  10761. if (/border/i.test(span.style.cssText) && span.parentNode.tagName == 'SPAN' && /border/i.test(span.parentNode.style.cssText)) {
  10762. span.style.cssText = span.style.cssText.replace(/border[^:]*:[^;]+;?/gi, '');
  10763. }
  10764. if(!(cmdName=='fontborder' && value=='none')){
  10765. var next = span.nextSibling;
  10766. while (next && next.nodeType == 1 && next.tagName == 'SPAN' ) {
  10767. if(domUtils.isBookmarkNode(next) && cmdName == 'fontborder') {
  10768. span.appendChild(next);
  10769. next = span.nextSibling;
  10770. continue;
  10771. }
  10772. if (next.style.cssText == span.style.cssText) {
  10773. domUtils.moveChild(next, span);
  10774. domUtils.remove(next);
  10775. }
  10776. if (span.nextSibling === next)
  10777. break;
  10778. next = span.nextSibling;
  10779. }
  10780. }
  10781. mergeWithParent(span);
  10782. if(browser.ie && browser.version > 8 ){
  10783. //拷贝父亲们的特别的属性,这里只做背景颜色的处理
  10784. var parent = domUtils.findParent(span,function(n){return n.tagName == 'SPAN' && /background-color/.test(n.style.cssText)});
  10785. if(parent && !/background-color/.test(span.style.cssText)){
  10786. span.style.backgroundColor = parent.style.backgroundColor;
  10787. }
  10788. }
  10789. });
  10790. rng.moveToBookmark(bk);
  10791. mergeChild(rng,cmdName,value)
  10792. }
  10793. me.addInputRule(function (root) {
  10794. utils.each(root.getNodesByTagName('u s del font strike'), function (node) {
  10795. if (node.tagName == 'font') {
  10796. var cssStyle = [];
  10797. for (var p in node.attrs) {
  10798. switch (p) {
  10799. case 'size':
  10800. cssStyle.push('font-size:' +
  10801. ({
  10802. '1':'10',
  10803. '2':'12',
  10804. '3':'16',
  10805. '4':'18',
  10806. '5':'24',
  10807. '6':'32',
  10808. '7':'48'
  10809. }[node.attrs[p]] || node.attrs[p]) + 'px');
  10810. break;
  10811. case 'color':
  10812. cssStyle.push('color:' + node.attrs[p]);
  10813. break;
  10814. case 'face':
  10815. cssStyle.push('font-family:' + node.attrs[p]);
  10816. break;
  10817. case 'style':
  10818. cssStyle.push(node.attrs[p]);
  10819. }
  10820. }
  10821. node.attrs = {
  10822. 'style': cssStyle.join(';')
  10823. };
  10824. } else {
  10825. var val = node.tagName == 'u' ? 'underline' : 'line-through';
  10826. node.attrs = {
  10827. 'style': (node.getAttr('style') || '') + 'text-decoration:' + val + ';'
  10828. }
  10829. }
  10830. node.tagName = 'span';
  10831. });
  10832. // utils.each(root.getNodesByTagName('span'), function (node) {
  10833. // var val;
  10834. // if(val = node.getAttr('class')){
  10835. // if(/fontstrikethrough/.test(val)){
  10836. // node.setStyle('text-decoration','line-through');
  10837. // if(node.attrs['class']){
  10838. // node.attrs['class'] = node.attrs['class'].replace(/fontstrikethrough/,'');
  10839. // }else{
  10840. // node.setAttr('class')
  10841. // }
  10842. // }
  10843. // if(/fontborder/.test(val)){
  10844. // node.setStyle('border','1px solid #000');
  10845. // if(node.attrs['class']){
  10846. // node.attrs['class'] = node.attrs['class'].replace(/fontborder/,'');
  10847. // }else{
  10848. // node.setAttr('class')
  10849. // }
  10850. // }
  10851. // }
  10852. // });
  10853. });
  10854. // me.addOutputRule(function(root){
  10855. // utils.each(root.getNodesByTagName('span'), function (node) {
  10856. // var val;
  10857. // if(val = node.getStyle('text-decoration')){
  10858. // if(/line-through/.test(val)){
  10859. // if(node.attrs['class']){
  10860. // node.attrs['class'] += ' fontstrikethrough';
  10861. // }else{
  10862. // node.setAttr('class','fontstrikethrough')
  10863. // }
  10864. // }
  10865. //
  10866. // node.setStyle('text-decoration')
  10867. // }
  10868. // if(val = node.getStyle('border')){
  10869. // if(/1px/.test(val) && /solid/.test(val)){
  10870. // if(node.attrs['class']){
  10871. // node.attrs['class'] += ' fontborder';
  10872. //
  10873. // }else{
  10874. // node.setAttr('class','fontborder')
  10875. // }
  10876. // }
  10877. // node.setStyle('border')
  10878. //
  10879. // }
  10880. // });
  10881. // });
  10882. for (var p in fonts) {
  10883. (function (cmd, style) {
  10884. UE.commands[cmd] = {
  10885. execCommand: function (cmdName, value) {
  10886. value = value || (this.queryCommandState(cmdName) ? 'none' : cmdName == 'underline' ? 'underline' :
  10887. cmdName == 'fontborder' ? '1px solid #000' :
  10888. 'line-through');
  10889. var me = this,
  10890. range = this.selection.getRange(),
  10891. text;
  10892. if (value == 'default') {
  10893. if (range.collapsed) {
  10894. text = me.document.createTextNode('font');
  10895. range.insertNode(text).select();
  10896. }
  10897. me.execCommand('removeFormat', 'span,a', style);
  10898. if (text) {
  10899. range.setStartBefore(text).collapse(true);
  10900. domUtils.remove(text);
  10901. }
  10902. mergesibling(range,cmdName,value);
  10903. range.select()
  10904. } else {
  10905. if (!range.collapsed) {
  10906. if (needCmd[cmd] && me.queryCommandValue(cmd)) {
  10907. me.execCommand('removeFormat', 'span,a', style);
  10908. }
  10909. range = me.selection.getRange();
  10910. range.applyInlineStyle('span', {'style': style + ':' + value});
  10911. mergesibling(range, cmdName,value);
  10912. range.select();
  10913. } else {
  10914. var span = domUtils.findParentByTagName(range.startContainer, 'span', true);
  10915. text = me.document.createTextNode('font');
  10916. if (span && !span.children.length && !span[browser.ie ? 'innerText' : 'textContent'].replace(fillCharReg, '').length) {
  10917. //for ie hack when enter
  10918. range.insertNode(text);
  10919. if (needCmd[cmd]) {
  10920. range.selectNode(text).select();
  10921. me.execCommand('removeFormat', 'span,a', style, null);
  10922. span = domUtils.findParentByTagName(text, 'span', true);
  10923. range.setStartBefore(text);
  10924. }
  10925. span && (span.style.cssText += ';' + style + ':' + value);
  10926. range.collapse(true).select();
  10927. } else {
  10928. range.insertNode(text);
  10929. range.selectNode(text).select();
  10930. span = range.document.createElement('span');
  10931. if (needCmd[cmd]) {
  10932. //a标签内的不处理跳过
  10933. if (domUtils.findParentByTagName(text, 'a', true)) {
  10934. range.setStartBefore(text).setCursor();
  10935. domUtils.remove(text);
  10936. return;
  10937. }
  10938. me.execCommand('removeFormat', 'span,a', style);
  10939. }
  10940. span.style.cssText = style + ':' + value;
  10941. text.parentNode.insertBefore(span, text);
  10942. //修复,span套span 但样式不继承的问题
  10943. if (!browser.ie || browser.ie && browser.version == 9) {
  10944. var spanParent = span.parentNode;
  10945. while (!domUtils.isBlockElm(spanParent)) {
  10946. if (spanParent.tagName == 'SPAN') {
  10947. //opera合并style不会加入";"
  10948. span.style.cssText = spanParent.style.cssText + ";" + span.style.cssText;
  10949. }
  10950. spanParent = spanParent.parentNode;
  10951. }
  10952. }
  10953. if (opera) {
  10954. setTimeout(function () {
  10955. range.setStart(span, 0).collapse(true);
  10956. mergesibling(range, cmdName,value);
  10957. range.select();
  10958. });
  10959. } else {
  10960. range.setStart(span, 0).collapse(true);
  10961. mergesibling(range,cmdName,value);
  10962. range.select();
  10963. }
  10964. //trace:981
  10965. //domUtils.mergeToParent(span)
  10966. }
  10967. domUtils.remove(text);
  10968. }
  10969. }
  10970. return true;
  10971. },
  10972. queryCommandValue: function (cmdName) {
  10973. var startNode = this.selection.getStart();
  10974. //trace:946
  10975. if (cmdName == 'underline' || cmdName == 'strikethrough') {
  10976. var tmpNode = startNode, value;
  10977. while (tmpNode && !domUtils.isBlockElm(tmpNode) && !domUtils.isBody(tmpNode)) {
  10978. if (tmpNode.nodeType == 1) {
  10979. value = domUtils.getComputedStyle(tmpNode, style);
  10980. if (value != 'none') {
  10981. return value;
  10982. }
  10983. }
  10984. tmpNode = tmpNode.parentNode;
  10985. }
  10986. return 'none';
  10987. }
  10988. if (cmdName == 'fontborder') {
  10989. var tmp = startNode, val;
  10990. while (tmp && dtd.$inline[tmp.tagName]) {
  10991. if (val = domUtils.getComputedStyle(tmp, 'border')) {
  10992. if (/1px/.test(val) && /solid/.test(val)) {
  10993. return val;
  10994. }
  10995. }
  10996. tmp = tmp.parentNode;
  10997. }
  10998. return ''
  10999. }
  11000. if( cmdName == 'FontSize' ) {
  11001. var styleVal = domUtils.getComputedStyle(startNode, style),
  11002. tmp = /^([\d\.]+)(\w+)$/.exec( styleVal );
  11003. if( tmp ) {
  11004. return Math.floor( tmp[1] ) + tmp[2];
  11005. }
  11006. return styleVal;
  11007. }
  11008. return domUtils.getComputedStyle(startNode, style);
  11009. },
  11010. queryCommandState: function (cmdName) {
  11011. if (!needCmd[cmdName])
  11012. return 0;
  11013. var val = this.queryCommandValue(cmdName);
  11014. if (cmdName == 'fontborder') {
  11015. return /1px/.test(val) && /solid/.test(val)
  11016. } else {
  11017. return cmdName == 'underline' ? /underline/.test(val) : /line\-through/.test(val);
  11018. }
  11019. }
  11020. };
  11021. })(p, fonts[p]);
  11022. }
  11023. };
  11024. // plugins/link.js
  11025. /**
  11026. * 超链接
  11027. * @file
  11028. * @since 1.2.6.1
  11029. */
  11030. /**
  11031. * 插入超链接
  11032. * @command link
  11033. * @method execCommand
  11034. * @param { String } cmd 命令字符串
  11035. * @param { Object } options 设置自定义属性,例如:url、title、target
  11036. * @example
  11037. * ```javascript
  11038. * editor.execCommand( 'link', '{
  11039. * url:'ueditor.baidu.com',
  11040. * title:'ueditor',
  11041. * target:'_blank'
  11042. * }' );
  11043. * ```
  11044. */
  11045. /**
  11046. * 返回当前选中的第一个超链接节点
  11047. * @command link
  11048. * @method queryCommandValue
  11049. * @param { String } cmd 命令字符串
  11050. * @return { Element } 超链接节点
  11051. * @example
  11052. * ```javascript
  11053. * editor.queryCommandValue( 'link' );
  11054. * ```
  11055. */
  11056. /**
  11057. * 取消超链接
  11058. * @command unlink
  11059. * @method execCommand
  11060. * @param { String } cmd 命令字符串
  11061. * @example
  11062. * ```javascript
  11063. * editor.execCommand( 'unlink');
  11064. * ```
  11065. */
  11066. UE.plugins['link'] = function(){
  11067. function optimize( range ) {
  11068. var start = range.startContainer,end = range.endContainer;
  11069. if ( start = domUtils.findParentByTagName( start, 'a', true ) ) {
  11070. range.setStartBefore( start );
  11071. }
  11072. if ( end = domUtils.findParentByTagName( end, 'a', true ) ) {
  11073. range.setEndAfter( end );
  11074. }
  11075. }
  11076. UE.commands['unlink'] = {
  11077. execCommand : function() {
  11078. var range = this.selection.getRange(),
  11079. bookmark;
  11080. if(range.collapsed && !domUtils.findParentByTagName( range.startContainer, 'a', true )){
  11081. return;
  11082. }
  11083. bookmark = range.createBookmark();
  11084. optimize( range );
  11085. range.removeInlineStyle( 'a' ).moveToBookmark( bookmark ).select();
  11086. },
  11087. queryCommandState : function(){
  11088. return !this.highlight && this.queryCommandValue('link') ? 0 : -1;
  11089. }
  11090. };
  11091. function doLink(range,opt,me){
  11092. var rngClone = range.cloneRange(),
  11093. link = me.queryCommandValue('link');
  11094. optimize( range = range.adjustmentBoundary() );
  11095. var start = range.startContainer;
  11096. if(start.nodeType == 1 && link){
  11097. start = start.childNodes[range.startOffset];
  11098. if(start && start.nodeType == 1 && start.tagName == 'A' && /^(?:https?|ftp|file)\s*:\s*\/\//.test(start[browser.ie?'innerText':'textContent'])){
  11099. start[browser.ie ? 'innerText' : 'textContent'] = utils.html(opt.textValue||opt.href);
  11100. }
  11101. }
  11102. if( !rngClone.collapsed || link){
  11103. range.removeInlineStyle( 'a' );
  11104. rngClone = range.cloneRange();
  11105. }
  11106. if ( rngClone.collapsed ) {
  11107. var a = range.document.createElement( 'a'),
  11108. text = '';
  11109. if(opt.textValue){
  11110. text = utils.html(opt.textValue);
  11111. delete opt.textValue;
  11112. }else{
  11113. text = utils.html(opt.href);
  11114. }
  11115. domUtils.setAttributes( a, opt );
  11116. start = domUtils.findParentByTagName( rngClone.startContainer, 'a', true );
  11117. if(start && domUtils.isInNodeEndBoundary(rngClone,start)){
  11118. range.setStartAfter(start).collapse(true);
  11119. }
  11120. a[browser.ie ? 'innerText' : 'textContent'] = text;
  11121. range.insertNode(a).selectNode( a );
  11122. } else {
  11123. range.applyInlineStyle( 'a', opt );
  11124. }
  11125. }
  11126. UE.commands['link'] = {
  11127. execCommand : function( cmdName, opt ) {
  11128. var range;
  11129. opt._href && (opt._href = utils.unhtml(opt._href,/[<">]/g));
  11130. opt.href && (opt.href = utils.unhtml(opt.href,/[<">]/g));
  11131. opt.textValue && (opt.textValue = utils.unhtml(opt.textValue,/[<">]/g));
  11132. doLink(range=this.selection.getRange(),opt,this);
  11133. //闭合都不加占位符,如果加了会在a后边多个占位符节点,导致a是图片背景组成的列表,出现空白问题
  11134. range.collapse().select(true);
  11135. },
  11136. queryCommandValue : function() {
  11137. var range = this.selection.getRange(),
  11138. node;
  11139. if ( range.collapsed ) {
  11140. // node = this.selection.getStart();
  11141. //在ie下getstart()取值偏上了
  11142. node = range.startContainer;
  11143. node = node.nodeType == 1 ? node : node.parentNode;
  11144. if ( node && (node = domUtils.findParentByTagName( node, 'a', true )) && ! domUtils.isInNodeEndBoundary(range,node)) {
  11145. return node;
  11146. }
  11147. } else {
  11148. //trace:1111 如果是<p><a>xx</a></p> startContainer是p就会找不到a
  11149. range.shrinkBoundary();
  11150. var start = range.startContainer.nodeType == 3 || !range.startContainer.childNodes[range.startOffset] ? range.startContainer : range.startContainer.childNodes[range.startOffset],
  11151. end = range.endContainer.nodeType == 3 || range.endOffset == 0 ? range.endContainer : range.endContainer.childNodes[range.endOffset-1],
  11152. common = range.getCommonAncestor();
  11153. node = domUtils.findParentByTagName( common, 'a', true );
  11154. if ( !node && common.nodeType == 1){
  11155. var as = common.getElementsByTagName( 'a' ),
  11156. ps,pe;
  11157. for ( var i = 0,ci; ci = as[i++]; ) {
  11158. ps = domUtils.getPosition( ci, start ),pe = domUtils.getPosition( ci,end);
  11159. if ( (ps & domUtils.POSITION_FOLLOWING || ps & domUtils.POSITION_CONTAINS)
  11160. &&
  11161. (pe & domUtils.POSITION_PRECEDING || pe & domUtils.POSITION_CONTAINS)
  11162. ) {
  11163. node = ci;
  11164. break;
  11165. }
  11166. }
  11167. }
  11168. return node;
  11169. }
  11170. },
  11171. queryCommandState : function() {
  11172. //判断如果是视频的话连接不可用
  11173. //fix 853
  11174. var img = this.selection.getRange().getClosedNode(),
  11175. flag = img && (img.className == "edui-faked-video" || img.className.indexOf("edui-upload-video")!=-1);
  11176. return flag ? -1 : 0;
  11177. }
  11178. };
  11179. };
  11180. // plugins/iframe.js
  11181. ///import core
  11182. ///import plugins\inserthtml.js
  11183. ///commands 插入框架
  11184. ///commandsName InsertFrame
  11185. ///commandsTitle 插入Iframe
  11186. ///commandsDialog dialogs\insertframe
  11187. UE.plugins['insertframe'] = function() {
  11188. var me =this;
  11189. function deleteIframe(){
  11190. me._iframe && delete me._iframe;
  11191. }
  11192. me.addListener("selectionchange",function(){
  11193. deleteIframe();
  11194. });
  11195. };
  11196. // plugins/scrawl.js
  11197. ///import core
  11198. ///commands 涂鸦
  11199. ///commandsName Scrawl
  11200. ///commandsTitle 涂鸦
  11201. ///commandsDialog dialogs\scrawl
  11202. UE.commands['scrawl'] = {
  11203. queryCommandState : function(){
  11204. return ( browser.ie && browser.version <= 8 ) ? -1 :0;
  11205. }
  11206. };
  11207. // plugins/removeformat.js
  11208. /**
  11209. * 清除格式
  11210. * @file
  11211. * @since 1.2.6.1
  11212. */
  11213. /**
  11214. * 清除文字样式
  11215. * @command removeformat
  11216. * @method execCommand
  11217. * @param { String } cmd 命令字符串
  11218. * @param {String} tags 以逗号隔开的标签。如:strong
  11219. * @param {String} style 样式如:color
  11220. * @param {String} attrs 属性如:width
  11221. * @example
  11222. * ```javascript
  11223. * editor.execCommand( 'removeformat', 'strong','color','width' );
  11224. * ```
  11225. */
  11226. UE.plugins['removeformat'] = function(){
  11227. var me = this;
  11228. me.setOpt({
  11229. 'removeFormatTags': 'b,big,code,del,dfn,em,font,i,ins,kbd,q,samp,small,span,strike,strong,sub,sup,tt,u,var',
  11230. 'removeFormatAttributes':'class,style,lang,width,height,align,hspace,valign'
  11231. });
  11232. me.commands['removeformat'] = {
  11233. execCommand : function( cmdName, tags, style, attrs,notIncludeA ) {
  11234. var tagReg = new RegExp( '^(?:' + (tags || this.options.removeFormatTags).replace( /,/g, '|' ) + ')$', 'i' ) ,
  11235. removeFormatAttributes = style ? [] : (attrs || this.options.removeFormatAttributes).split( ',' ),
  11236. range = new dom.Range( this.document ),
  11237. bookmark,node,parent,
  11238. filter = function( node ) {
  11239. return node.nodeType == 1;
  11240. };
  11241. function isRedundantSpan (node) {
  11242. if (node.nodeType == 3 || node.tagName.toLowerCase() != 'span'){
  11243. return 0;
  11244. }
  11245. if (browser.ie) {
  11246. //ie 下判断实效,所以只能简单用style来判断
  11247. //return node.style.cssText == '' ? 1 : 0;
  11248. var attrs = node.attributes;
  11249. if ( attrs.length ) {
  11250. for ( var i = 0,l = attrs.length; i<l; i++ ) {
  11251. if ( attrs[i].specified ) {
  11252. return 0;
  11253. }
  11254. }
  11255. return 1;
  11256. }
  11257. }
  11258. return !node.attributes.length;
  11259. }
  11260. function doRemove( range ) {
  11261. var bookmark1 = range.createBookmark();
  11262. if ( range.collapsed ) {
  11263. range.enlarge( true );
  11264. }
  11265. //不能把a标签切了
  11266. if(!notIncludeA){
  11267. var aNode = domUtils.findParentByTagName(range.startContainer,'a',true);
  11268. if(aNode){
  11269. range.setStartBefore(aNode);
  11270. }
  11271. aNode = domUtils.findParentByTagName(range.endContainer,'a',true);
  11272. if(aNode){
  11273. range.setEndAfter(aNode);
  11274. }
  11275. }
  11276. bookmark = range.createBookmark();
  11277. node = bookmark.start;
  11278. //切开始
  11279. while ( (parent = node.parentNode) && !domUtils.isBlockElm( parent ) ) {
  11280. domUtils.breakParent( node, parent );
  11281. domUtils.clearEmptySibling( node );
  11282. }
  11283. if ( bookmark.end ) {
  11284. //切结束
  11285. node = bookmark.end;
  11286. while ( (parent = node.parentNode) && !domUtils.isBlockElm( parent ) ) {
  11287. domUtils.breakParent( node, parent );
  11288. domUtils.clearEmptySibling( node );
  11289. }
  11290. //开始去除样式
  11291. var current = domUtils.getNextDomNode( bookmark.start, false, filter ),
  11292. next;
  11293. while ( current ) {
  11294. if ( current == bookmark.end ) {
  11295. break;
  11296. }
  11297. next = domUtils.getNextDomNode( current, true, filter );
  11298. if ( !dtd.$empty[current.tagName.toLowerCase()] && !domUtils.isBookmarkNode( current ) ) {
  11299. if ( tagReg.test( current.tagName ) ) {
  11300. if ( style ) {
  11301. domUtils.removeStyle( current, style );
  11302. if ( isRedundantSpan( current ) && style != 'text-decoration'){
  11303. domUtils.remove( current, true );
  11304. }
  11305. } else {
  11306. domUtils.remove( current, true );
  11307. }
  11308. } else {
  11309. //trace:939 不能把list上的样式去掉
  11310. if(!dtd.$tableContent[current.tagName] && !dtd.$list[current.tagName]){
  11311. domUtils.removeAttributes( current, removeFormatAttributes );
  11312. if ( isRedundantSpan( current ) ){
  11313. domUtils.remove( current, true );
  11314. }
  11315. }
  11316. }
  11317. }
  11318. current = next;
  11319. }
  11320. }
  11321. //trace:1035
  11322. //trace:1096 不能把td上的样式去掉,比如边框
  11323. var pN = bookmark.start.parentNode;
  11324. if(domUtils.isBlockElm(pN) && !dtd.$tableContent[pN.tagName] && !dtd.$list[pN.tagName]){
  11325. domUtils.removeAttributes( pN,removeFormatAttributes );
  11326. }
  11327. pN = bookmark.end.parentNode;
  11328. if(bookmark.end && domUtils.isBlockElm(pN) && !dtd.$tableContent[pN.tagName]&& !dtd.$list[pN.tagName]){
  11329. domUtils.removeAttributes( pN,removeFormatAttributes );
  11330. }
  11331. range.moveToBookmark( bookmark ).moveToBookmark(bookmark1);
  11332. //清除冗余的代码 <b><bookmark></b>
  11333. var node = range.startContainer,
  11334. tmp,
  11335. collapsed = range.collapsed;
  11336. while(node.nodeType == 1 && domUtils.isEmptyNode(node) && dtd.$removeEmpty[node.tagName]){
  11337. tmp = node.parentNode;
  11338. range.setStartBefore(node);
  11339. //trace:937
  11340. //更新结束边界
  11341. if(range.startContainer === range.endContainer){
  11342. range.endOffset--;
  11343. }
  11344. domUtils.remove(node);
  11345. node = tmp;
  11346. }
  11347. if(!collapsed){
  11348. node = range.endContainer;
  11349. while(node.nodeType == 1 && domUtils.isEmptyNode(node) && dtd.$removeEmpty[node.tagName]){
  11350. tmp = node.parentNode;
  11351. range.setEndBefore(node);
  11352. domUtils.remove(node);
  11353. node = tmp;
  11354. }
  11355. }
  11356. }
  11357. range = this.selection.getRange();
  11358. doRemove( range );
  11359. range.select();
  11360. }
  11361. };
  11362. };
  11363. // plugins/blockquote.js
  11364. /**
  11365. * 添加引用
  11366. * @file
  11367. * @since 1.2.6.1
  11368. */
  11369. /**
  11370. * 添加引用
  11371. * @command blockquote
  11372. * @method execCommand
  11373. * @param { String } cmd 命令字符串
  11374. * @example
  11375. * ```javascript
  11376. * editor.execCommand( 'blockquote' );
  11377. * ```
  11378. */
  11379. /**
  11380. * 添加引用
  11381. * @command blockquote
  11382. * @method execCommand
  11383. * @param { String } cmd 命令字符串
  11384. * @param { Object } attrs 节点属性
  11385. * @example
  11386. * ```javascript
  11387. * editor.execCommand( 'blockquote',{
  11388. * style: "color: red;"
  11389. * } );
  11390. * ```
  11391. */
  11392. UE.plugins['blockquote'] = function(){
  11393. var me = this;
  11394. function getObj(editor){
  11395. return domUtils.filterNodeList(editor.selection.getStartElementPath(),'blockquote');
  11396. }
  11397. me.commands['blockquote'] = {
  11398. execCommand : function( cmdName, attrs ) {
  11399. var range = this.selection.getRange(),
  11400. obj = getObj(this),
  11401. blockquote = dtd.blockquote,
  11402. bookmark = range.createBookmark();
  11403. if ( obj ) {
  11404. var start = range.startContainer,
  11405. startBlock = domUtils.isBlockElm(start) ? start : domUtils.findParent(start,function(node){return domUtils.isBlockElm(node)}),
  11406. end = range.endContainer,
  11407. endBlock = domUtils.isBlockElm(end) ? end : domUtils.findParent(end,function(node){return domUtils.isBlockElm(node)});
  11408. //处理一下li
  11409. startBlock = domUtils.findParentByTagName(startBlock,'li',true) || startBlock;
  11410. endBlock = domUtils.findParentByTagName(endBlock,'li',true) || endBlock;
  11411. if(startBlock.tagName == 'LI' || startBlock.tagName == 'TD' || startBlock === obj || domUtils.isBody(startBlock)){
  11412. domUtils.remove(obj,true);
  11413. }else{
  11414. domUtils.breakParent(startBlock,obj);
  11415. }
  11416. if(startBlock !== endBlock){
  11417. obj = domUtils.findParentByTagName(endBlock,'blockquote');
  11418. if(obj){
  11419. if(endBlock.tagName == 'LI' || endBlock.tagName == 'TD'|| domUtils.isBody(endBlock)){
  11420. obj.parentNode && domUtils.remove(obj,true);
  11421. }else{
  11422. domUtils.breakParent(endBlock,obj);
  11423. }
  11424. }
  11425. }
  11426. var blockquotes = domUtils.getElementsByTagName(this.document,'blockquote');
  11427. for(var i=0,bi;bi=blockquotes[i++];){
  11428. if(!bi.childNodes.length){
  11429. domUtils.remove(bi);
  11430. }else if(domUtils.getPosition(bi,startBlock)&domUtils.POSITION_FOLLOWING && domUtils.getPosition(bi,endBlock)&domUtils.POSITION_PRECEDING){
  11431. domUtils.remove(bi,true);
  11432. }
  11433. }
  11434. } else {
  11435. var tmpRange = range.cloneRange(),
  11436. node = tmpRange.startContainer.nodeType == 1 ? tmpRange.startContainer : tmpRange.startContainer.parentNode,
  11437. preNode = node,
  11438. doEnd = 1;
  11439. //调整开始
  11440. while ( 1 ) {
  11441. if ( domUtils.isBody(node) ) {
  11442. if ( preNode !== node ) {
  11443. if ( range.collapsed ) {
  11444. tmpRange.selectNode( preNode );
  11445. doEnd = 0;
  11446. } else {
  11447. tmpRange.setStartBefore( preNode );
  11448. }
  11449. }else{
  11450. tmpRange.setStart(node,0);
  11451. }
  11452. break;
  11453. }
  11454. if ( !blockquote[node.tagName] ) {
  11455. if ( range.collapsed ) {
  11456. tmpRange.selectNode( preNode );
  11457. } else{
  11458. tmpRange.setStartBefore( preNode);
  11459. }
  11460. break;
  11461. }
  11462. preNode = node;
  11463. node = node.parentNode;
  11464. }
  11465. //调整结束
  11466. if ( doEnd ) {
  11467. preNode = node = node = tmpRange.endContainer.nodeType == 1 ? tmpRange.endContainer : tmpRange.endContainer.parentNode;
  11468. while ( 1 ) {
  11469. if ( domUtils.isBody( node ) ) {
  11470. if ( preNode !== node ) {
  11471. tmpRange.setEndAfter( preNode );
  11472. } else {
  11473. tmpRange.setEnd( node, node.childNodes.length );
  11474. }
  11475. break;
  11476. }
  11477. if ( !blockquote[node.tagName] ) {
  11478. tmpRange.setEndAfter( preNode );
  11479. break;
  11480. }
  11481. preNode = node;
  11482. node = node.parentNode;
  11483. }
  11484. }
  11485. node = range.document.createElement( 'blockquote' );
  11486. domUtils.setAttributes( node, attrs );
  11487. node.appendChild( tmpRange.extractContents() );
  11488. tmpRange.insertNode( node );
  11489. //去除重复的
  11490. var childs = domUtils.getElementsByTagName(node,'blockquote');
  11491. for(var i=0,ci;ci=childs[i++];){
  11492. if(ci.parentNode){
  11493. domUtils.remove(ci,true);
  11494. }
  11495. }
  11496. }
  11497. range.moveToBookmark( bookmark ).select();
  11498. },
  11499. queryCommandState : function() {
  11500. return getObj(this) ? 1 : 0;
  11501. }
  11502. };
  11503. };
  11504. // plugins/convertcase.js
  11505. /**
  11506. * 大小写转换
  11507. * @file
  11508. * @since 1.2.6.1
  11509. */
  11510. /**
  11511. * 把选区内文本变大写,与“tolowercase”命令互斥
  11512. * @command touppercase
  11513. * @method execCommand
  11514. * @param { String } cmd 命令字符串
  11515. * @example
  11516. * ```javascript
  11517. * editor.execCommand( 'touppercase' );
  11518. * ```
  11519. */
  11520. /**
  11521. * 把选区内文本变小写,与“touppercase”命令互斥
  11522. * @command tolowercase
  11523. * @method execCommand
  11524. * @param { String } cmd 命令字符串
  11525. * @example
  11526. * ```javascript
  11527. * editor.execCommand( 'tolowercase' );
  11528. * ```
  11529. */
  11530. UE.commands['touppercase'] =
  11531. UE.commands['tolowercase'] = {
  11532. execCommand:function (cmd) {
  11533. var me = this;
  11534. var rng = me.selection.getRange();
  11535. if(rng.collapsed){
  11536. return rng;
  11537. }
  11538. var bk = rng.createBookmark(),
  11539. bkEnd = bk.end,
  11540. filterFn = function( node ) {
  11541. return !domUtils.isBr(node) && !domUtils.isWhitespace( node );
  11542. },
  11543. curNode = domUtils.getNextDomNode( bk.start, false, filterFn );
  11544. while ( curNode && (domUtils.getPosition( curNode, bkEnd ) & domUtils.POSITION_PRECEDING) ) {
  11545. if ( curNode.nodeType == 3 ) {
  11546. curNode.nodeValue = curNode.nodeValue[cmd == 'touppercase' ? 'toUpperCase' : 'toLowerCase']();
  11547. }
  11548. curNode = domUtils.getNextDomNode( curNode, true, filterFn );
  11549. if(curNode === bkEnd){
  11550. break;
  11551. }
  11552. }
  11553. rng.moveToBookmark(bk).select();
  11554. }
  11555. };
  11556. // plugins/indent.js
  11557. /**
  11558. * 首行缩进
  11559. * @file
  11560. * @since 1.2.6.1
  11561. */
  11562. /**
  11563. * 缩进
  11564. * @command indent
  11565. * @method execCommand
  11566. * @param { String } cmd 命令字符串
  11567. * @example
  11568. * ```javascript
  11569. * editor.execCommand( 'indent' );
  11570. * ```
  11571. */
  11572. UE.commands['indent'] = {
  11573. execCommand : function() {
  11574. var me = this,value = me.queryCommandState("indent") ? "0em" : (me.options.indentValue || '2em');
  11575. me.execCommand('Paragraph','p',{style:'text-indent:'+ value});
  11576. },
  11577. queryCommandState : function() {
  11578. var pN = domUtils.filterNodeList(this.selection.getStartElementPath(),'p h1 h2 h3 h4 h5 h6');
  11579. return pN && pN.style.textIndent && parseInt(pN.style.textIndent) ? 1 : 0;
  11580. }
  11581. };
  11582. // plugins/print.js
  11583. /**
  11584. * 打印
  11585. * @file
  11586. * @since 1.2.6.1
  11587. */
  11588. /**
  11589. * 打印
  11590. * @command print
  11591. * @method execCommand
  11592. * @param { String } cmd 命令字符串
  11593. * @example
  11594. * ```javascript
  11595. * editor.execCommand( 'print' );
  11596. * ```
  11597. */
  11598. UE.commands['print'] = {
  11599. execCommand : function(){
  11600. this.window.print();
  11601. },
  11602. notNeedUndo : 1
  11603. };
  11604. // plugins/preview.js
  11605. /**
  11606. * 预览
  11607. * @file
  11608. * @since 1.2.6.1
  11609. */
  11610. /**
  11611. * 预览
  11612. * @command preview
  11613. * @method execCommand
  11614. * @param { String } cmd 命令字符串
  11615. * @example
  11616. * ```javascript
  11617. * editor.execCommand( 'preview' );
  11618. * ```
  11619. */
  11620. UE.commands['preview'] = {
  11621. execCommand : function(){
  11622. var w = window.open('', '_blank', ''),
  11623. d = w.document;
  11624. d.open();
  11625. d.write('<!DOCTYPE html><html><head><meta charset="utf-8"/><script src="'+this.options.UEDITOR_HOME_URL+'ueditor.parse.js"></script><script>' +
  11626. "setTimeout(function(){uParse('div',{rootPath: '"+ this.options.UEDITOR_HOME_URL +"'})},300)" +
  11627. '</script></head><body><div>'+this.getContent(null,null,true)+'</div></body></html>');
  11628. d.close();
  11629. },
  11630. notNeedUndo : 1
  11631. };
  11632. // plugins/selectall.js
  11633. /**
  11634. * 全选
  11635. * @file
  11636. * @since 1.2.6.1
  11637. */
  11638. /**
  11639. * 选中所有内容
  11640. * @command selectall
  11641. * @method execCommand
  11642. * @param { String } cmd 命令字符串
  11643. * @example
  11644. * ```javascript
  11645. * editor.execCommand( 'selectall' );
  11646. * ```
  11647. */
  11648. UE.plugins['selectall'] = function(){
  11649. var me = this;
  11650. me.commands['selectall'] = {
  11651. execCommand : function(){
  11652. //去掉了原生的selectAll,因为会出现报错和当内容为空时,不能出现闭合状态的光标
  11653. var me = this,body = me.body,
  11654. range = me.selection.getRange();
  11655. range.selectNodeContents(body);
  11656. if(domUtils.isEmptyBlock(body)){
  11657. //opera不能自动合并到元素的里边,要手动处理一下
  11658. if(browser.opera && body.firstChild && body.firstChild.nodeType == 1){
  11659. range.setStartAtFirst(body.firstChild);
  11660. }
  11661. range.collapse(true);
  11662. }
  11663. range.select(true);
  11664. },
  11665. notNeedUndo : 1
  11666. };
  11667. //快捷键
  11668. me.addshortcutkey({
  11669. "selectAll" : "ctrl+65"
  11670. });
  11671. };
  11672. // plugins/paragraph.js
  11673. /**
  11674. * 段落样式
  11675. * @file
  11676. * @since 1.2.6.1
  11677. */
  11678. /**
  11679. * 段落格式
  11680. * @command paragraph
  11681. * @method execCommand
  11682. * @param { String } cmd 命令字符串
  11683. * @param {String} style 标签值为:'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'
  11684. * @param {Object} attrs 标签的属性
  11685. * @example
  11686. * ```javascript
  11687. * editor.execCommand( 'Paragraph','h1','{
  11688. * class:'test'
  11689. * }' );
  11690. * ```
  11691. */
  11692. /**
  11693. * 返回选区内节点标签名
  11694. * @command paragraph
  11695. * @method queryCommandValue
  11696. * @param { String } cmd 命令字符串
  11697. * @return { String } 节点标签名
  11698. * @example
  11699. * ```javascript
  11700. * editor.queryCommandValue( 'Paragraph' );
  11701. * ```
  11702. */
  11703. UE.plugins['paragraph'] = function() {
  11704. var me = this,
  11705. block = domUtils.isBlockElm,
  11706. notExchange = ['TD','LI','PRE'],
  11707. doParagraph = function(range,style,attrs,sourceCmdName){
  11708. var bookmark = range.createBookmark(),
  11709. filterFn = function( node ) {
  11710. return node.nodeType == 1 ? node.tagName.toLowerCase() != 'br' && !domUtils.isBookmarkNode(node) : !domUtils.isWhitespace( node );
  11711. },
  11712. para;
  11713. range.enlarge( true );
  11714. var bookmark2 = range.createBookmark(),
  11715. current = domUtils.getNextDomNode( bookmark2.start, false, filterFn ),
  11716. tmpRange = range.cloneRange(),
  11717. tmpNode;
  11718. while ( current && !(domUtils.getPosition( current, bookmark2.end ) & domUtils.POSITION_FOLLOWING) ) {
  11719. if ( current.nodeType == 3 || !block( current ) ) {
  11720. tmpRange.setStartBefore( current );
  11721. while ( current && current !== bookmark2.end && !block( current ) ) {
  11722. tmpNode = current;
  11723. current = domUtils.getNextDomNode( current, false, null, function( node ) {
  11724. return !block( node );
  11725. } );
  11726. }
  11727. tmpRange.setEndAfter( tmpNode );
  11728. para = range.document.createElement( style );
  11729. if(attrs){
  11730. domUtils.setAttributes(para,attrs);
  11731. if(sourceCmdName && sourceCmdName == 'customstyle' && attrs.style){
  11732. para.style.cssText = attrs.style;
  11733. }
  11734. }
  11735. para.appendChild( tmpRange.extractContents() );
  11736. //需要内容占位
  11737. if(domUtils.isEmptyNode(para)){
  11738. domUtils.fillChar(range.document,para);
  11739. }
  11740. tmpRange.insertNode( para );
  11741. var parent = para.parentNode;
  11742. //如果para上一级是一个block元素且不是body,td就删除它
  11743. if ( block( parent ) && !domUtils.isBody( para.parentNode ) && utils.indexOf(notExchange,parent.tagName)==-1) {
  11744. //存储dir,style
  11745. if(!(sourceCmdName && sourceCmdName == 'customstyle')){
  11746. parent.getAttribute('dir') && para.setAttribute('dir',parent.getAttribute('dir'));
  11747. //trace:1070
  11748. parent.style.cssText && (para.style.cssText = parent.style.cssText + ';' + para.style.cssText);
  11749. //trace:1030
  11750. parent.style.textAlign && !para.style.textAlign && (para.style.textAlign = parent.style.textAlign);
  11751. parent.style.textIndent && !para.style.textIndent && (para.style.textIndent = parent.style.textIndent);
  11752. parent.style.padding && !para.style.padding && (para.style.padding = parent.style.padding);
  11753. }
  11754. //trace:1706 选择的就是h1-6要删除
  11755. if(attrs && /h\d/i.test(parent.tagName) && !/h\d/i.test(para.tagName) ){
  11756. domUtils.setAttributes(parent,attrs);
  11757. if(sourceCmdName && sourceCmdName == 'customstyle' && attrs.style){
  11758. parent.style.cssText = attrs.style;
  11759. }
  11760. domUtils.remove(para,true);
  11761. para = parent;
  11762. }else{
  11763. domUtils.remove( para.parentNode, true );
  11764. }
  11765. }
  11766. if( utils.indexOf(notExchange,parent.tagName)!=-1){
  11767. current = parent;
  11768. }else{
  11769. current = para;
  11770. }
  11771. current = domUtils.getNextDomNode( current, false, filterFn );
  11772. } else {
  11773. current = domUtils.getNextDomNode( current, true, filterFn );
  11774. }
  11775. }
  11776. return range.moveToBookmark( bookmark2 ).moveToBookmark( bookmark );
  11777. };
  11778. me.setOpt('paragraph',{'p':'', 'h1':'', 'h2':'', 'h3':'', 'h4':'', 'h5':'', 'h6':''});
  11779. me.commands['paragraph'] = {
  11780. execCommand : function( cmdName, style,attrs,sourceCmdName ) {
  11781. var range = this.selection.getRange();
  11782. //闭合时单独处理
  11783. if(range.collapsed){
  11784. var txt = this.document.createTextNode('p');
  11785. range.insertNode(txt);
  11786. //去掉冗余的fillchar
  11787. if(browser.ie){
  11788. var node = txt.previousSibling;
  11789. if(node && domUtils.isWhitespace(node)){
  11790. domUtils.remove(node);
  11791. }
  11792. node = txt.nextSibling;
  11793. if(node && domUtils.isWhitespace(node)){
  11794. domUtils.remove(node);
  11795. }
  11796. }
  11797. }
  11798. range = doParagraph(range,style,attrs,sourceCmdName);
  11799. if(txt){
  11800. range.setStartBefore(txt).collapse(true);
  11801. pN = txt.parentNode;
  11802. domUtils.remove(txt);
  11803. if(domUtils.isBlockElm(pN)&&domUtils.isEmptyNode(pN)){
  11804. domUtils.fillNode(this.document,pN);
  11805. }
  11806. }
  11807. if(browser.gecko && range.collapsed && range.startContainer.nodeType == 1){
  11808. var child = range.startContainer.childNodes[range.startOffset];
  11809. if(child && child.nodeType == 1 && child.tagName.toLowerCase() == style){
  11810. range.setStart(child,0).collapse(true);
  11811. }
  11812. }
  11813. //trace:1097 原来有true,原因忘了,但去了就不能清除多余的占位符了
  11814. range.select();
  11815. return true;
  11816. },
  11817. queryCommandValue : function() {
  11818. var node = domUtils.filterNodeList(this.selection.getStartElementPath(),'p h1 h2 h3 h4 h5 h6');
  11819. return node ? node.tagName.toLowerCase() : '';
  11820. }
  11821. };
  11822. };
  11823. // plugins/directionality.js
  11824. /**
  11825. * 设置文字输入的方向的插件
  11826. * @file
  11827. * @since 1.2.6.1
  11828. */
  11829. (function() {
  11830. var block = domUtils.isBlockElm ,
  11831. getObj = function(editor){
  11832. // var startNode = editor.selection.getStart(),
  11833. // parents;
  11834. // if ( startNode ) {
  11835. // //查找所有的是block的父亲节点
  11836. // parents = domUtils.findParents( startNode, true, block, true );
  11837. // for ( var i = 0,ci; ci = parents[i++]; ) {
  11838. // if ( ci.getAttribute( 'dir' ) ) {
  11839. // return ci;
  11840. // }
  11841. // }
  11842. // }
  11843. return domUtils.filterNodeList(editor.selection.getStartElementPath(),function(n){return n && n.nodeType == 1 && n.getAttribute('dir')});
  11844. },
  11845. doDirectionality = function(range,editor,forward){
  11846. var bookmark,
  11847. filterFn = function( node ) {
  11848. return node.nodeType == 1 ? !domUtils.isBookmarkNode(node) : !domUtils.isWhitespace(node);
  11849. },
  11850. obj = getObj( editor );
  11851. if ( obj && range.collapsed ) {
  11852. obj.setAttribute( 'dir', forward );
  11853. return range;
  11854. }
  11855. bookmark = range.createBookmark();
  11856. range.enlarge( true );
  11857. var bookmark2 = range.createBookmark(),
  11858. current = domUtils.getNextDomNode( bookmark2.start, false, filterFn ),
  11859. tmpRange = range.cloneRange(),
  11860. tmpNode;
  11861. while ( current && !(domUtils.getPosition( current, bookmark2.end ) & domUtils.POSITION_FOLLOWING) ) {
  11862. if ( current.nodeType == 3 || !block( current ) ) {
  11863. tmpRange.setStartBefore( current );
  11864. while ( current && current !== bookmark2.end && !block( current ) ) {
  11865. tmpNode = current;
  11866. current = domUtils.getNextDomNode( current, false, null, function( node ) {
  11867. return !block( node );
  11868. } );
  11869. }
  11870. tmpRange.setEndAfter( tmpNode );
  11871. var common = tmpRange.getCommonAncestor();
  11872. if ( !domUtils.isBody( common ) && block( common ) ) {
  11873. //遍历到了block节点
  11874. common.setAttribute( 'dir', forward );
  11875. current = common;
  11876. } else {
  11877. //没有遍历到,添加一个block节点
  11878. var p = range.document.createElement( 'p' );
  11879. p.setAttribute( 'dir', forward );
  11880. var frag = tmpRange.extractContents();
  11881. p.appendChild( frag );
  11882. tmpRange.insertNode( p );
  11883. current = p;
  11884. }
  11885. current = domUtils.getNextDomNode( current, false, filterFn );
  11886. } else {
  11887. current = domUtils.getNextDomNode( current, true, filterFn );
  11888. }
  11889. }
  11890. return range.moveToBookmark( bookmark2 ).moveToBookmark( bookmark );
  11891. };
  11892. /**
  11893. * 文字输入方向
  11894. * @command directionality
  11895. * @method execCommand
  11896. * @param { String } cmdName 命令字符串
  11897. * @param { String } forward 传入'ltr'表示从左向右输入,传入'rtl'表示从右向左输入
  11898. * @example
  11899. * ```javascript
  11900. * editor.execCommand( 'directionality', 'ltr');
  11901. * ```
  11902. */
  11903. /**
  11904. * 查询当前选区的文字输入方向
  11905. * @command directionality
  11906. * @method queryCommandValue
  11907. * @param { String } cmdName 命令字符串
  11908. * @return { String } 返回'ltr'表示从左向右输入,返回'rtl'表示从右向左输入
  11909. * @example
  11910. * ```javascript
  11911. * editor.queryCommandValue( 'directionality');
  11912. * ```
  11913. */
  11914. UE.commands['directionality'] = {
  11915. execCommand : function( cmdName,forward ) {
  11916. var range = this.selection.getRange();
  11917. //闭合时单独处理
  11918. if(range.collapsed){
  11919. var txt = this.document.createTextNode('d');
  11920. range.insertNode(txt);
  11921. }
  11922. doDirectionality(range,this,forward);
  11923. if(txt){
  11924. range.setStartBefore(txt).collapse(true);
  11925. domUtils.remove(txt);
  11926. }
  11927. range.select();
  11928. return true;
  11929. },
  11930. queryCommandValue : function() {
  11931. var node = getObj(this);
  11932. return node ? node.getAttribute('dir') : 'ltr';
  11933. }
  11934. };
  11935. })();
  11936. // plugins/horizontal.js
  11937. /**
  11938. * 插入分割线插件
  11939. * @file
  11940. * @since 1.2.6.1
  11941. */
  11942. /**
  11943. * 插入分割线
  11944. * @command horizontal
  11945. * @method execCommand
  11946. * @param { String } cmdName 命令字符串
  11947. * @example
  11948. * ```javascript
  11949. * editor.execCommand( 'horizontal' );
  11950. * ```
  11951. */
  11952. UE.plugins['horizontal'] = function(){
  11953. var me = this;
  11954. me.commands['horizontal'] = {
  11955. execCommand : function( cmdName ) {
  11956. var me = this;
  11957. if(me.queryCommandState(cmdName)!==-1){
  11958. me.execCommand('insertHtml','<hr>');
  11959. var range = me.selection.getRange(),
  11960. start = range.startContainer;
  11961. if(start.nodeType == 1 && !start.childNodes[range.startOffset] ){
  11962. var tmp;
  11963. if(tmp = start.childNodes[range.startOffset - 1]){
  11964. if(tmp.nodeType == 1 && tmp.tagName == 'HR'){
  11965. if(me.options.enterTag == 'p'){
  11966. tmp = me.document.createElement('p');
  11967. range.insertNode(tmp);
  11968. range.setStart(tmp,0).setCursor();
  11969. }else{
  11970. tmp = me.document.createElement('br');
  11971. range.insertNode(tmp);
  11972. range.setStartBefore(tmp).setCursor();
  11973. }
  11974. }
  11975. }
  11976. }
  11977. return true;
  11978. }
  11979. },
  11980. //边界在table里不能加分隔线
  11981. queryCommandState : function() {
  11982. return domUtils.filterNodeList(this.selection.getStartElementPath(),'table') ? -1 : 0;
  11983. }
  11984. };
  11985. // me.addListener('delkeyup',function(){
  11986. // var rng = this.selection.getRange();
  11987. // if(browser.ie && browser.version > 8){
  11988. // rng.txtToElmBoundary(true);
  11989. // if(domUtils.isStartInblock(rng)){
  11990. // var tmpNode = rng.startContainer;
  11991. // var pre = tmpNode.previousSibling;
  11992. // if(pre && domUtils.isTagNode(pre,'hr')){
  11993. // domUtils.remove(pre);
  11994. // rng.select();
  11995. // return;
  11996. // }
  11997. // }
  11998. // }
  11999. // if(domUtils.isBody(rng.startContainer)){
  12000. // var hr = rng.startContainer.childNodes[rng.startOffset -1];
  12001. // if(hr && hr.nodeName == 'HR'){
  12002. // var next = hr.nextSibling;
  12003. // if(next){
  12004. // rng.setStart(next,0)
  12005. // }else if(hr.previousSibling){
  12006. // rng.setStartAtLast(hr.previousSibling)
  12007. // }else{
  12008. // var p = this.document.createElement('p');
  12009. // hr.parentNode.insertBefore(p,hr);
  12010. // domUtils.fillNode(this.document,p);
  12011. // rng.setStart(p,0);
  12012. // }
  12013. // domUtils.remove(hr);
  12014. // rng.setCursor(false,true);
  12015. // }
  12016. // }
  12017. // })
  12018. me.addListener('delkeydown',function(name,evt){
  12019. var rng = this.selection.getRange();
  12020. rng.txtToElmBoundary(true);
  12021. if(domUtils.isStartInblock(rng)){
  12022. var tmpNode = rng.startContainer;
  12023. var pre = tmpNode.previousSibling;
  12024. if(pre && domUtils.isTagNode(pre,'hr')){
  12025. domUtils.remove(pre);
  12026. rng.select();
  12027. domUtils.preventDefault(evt);
  12028. return true;
  12029. }
  12030. }
  12031. })
  12032. };
  12033. // plugins/time.js
  12034. /**
  12035. * 插入时间和日期
  12036. * @file
  12037. * @since 1.2.6.1
  12038. */
  12039. /**
  12040. * 插入时间,默认格式:12:59:59
  12041. * @command time
  12042. * @method execCommand
  12043. * @param { String } cmd 命令字符串
  12044. * @example
  12045. * ```javascript
  12046. * editor.execCommand( 'time');
  12047. * ```
  12048. */
  12049. /**
  12050. * 插入日期,默认格式:2013-08-30
  12051. * @command date
  12052. * @method execCommand
  12053. * @param { String } cmd 命令字符串
  12054. * @example
  12055. * ```javascript
  12056. * editor.execCommand( 'date');
  12057. * ```
  12058. */
  12059. UE.commands['time'] = UE.commands["date"] = {
  12060. execCommand : function(cmd, format){
  12061. var date = new Date;
  12062. function formatTime(date, format) {
  12063. var hh = ('0' + date.getHours()).slice(-2),
  12064. ii = ('0' + date.getMinutes()).slice(-2),
  12065. ss = ('0' + date.getSeconds()).slice(-2);
  12066. format = format || 'hh:ii:ss';
  12067. return format.replace(/hh/ig, hh).replace(/ii/ig, ii).replace(/ss/ig, ss);
  12068. }
  12069. function formatDate(date, format) {
  12070. var yyyy = ('000' + date.getFullYear()).slice(-4),
  12071. yy = yyyy.slice(-2),
  12072. mm = ('0' + (date.getMonth()+1)).slice(-2),
  12073. dd = ('0' + date.getDate()).slice(-2);
  12074. format = format || 'yyyy-mm-dd';
  12075. return format.replace(/yyyy/ig, yyyy).replace(/yy/ig, yy).replace(/mm/ig, mm).replace(/dd/ig, dd);
  12076. }
  12077. this.execCommand('insertHtml',cmd == "time" ? formatTime(date, format):formatDate(date, format) );
  12078. }
  12079. };
  12080. // plugins/rowspacing.js
  12081. /**
  12082. * 段前段后间距插件
  12083. * @file
  12084. * @since 1.2.6.1
  12085. */
  12086. /**
  12087. * 设置段间距
  12088. * @command rowspacing
  12089. * @method execCommand
  12090. * @param { String } cmd 命令字符串
  12091. * @param { String } value 段间距的值,以px为单位
  12092. * @param { String } dir 间距位置,top或bottom,分别表示段前和段后
  12093. * @example
  12094. * ```javascript
  12095. * editor.execCommand( 'rowspacing', '10', 'top' );
  12096. * ```
  12097. */
  12098. UE.plugins['rowspacing'] = function(){
  12099. var me = this;
  12100. me.setOpt({
  12101. 'rowspacingtop':['5', '10', '15', '20', '25'],
  12102. 'rowspacingbottom':['5', '10', '15', '20', '25']
  12103. });
  12104. me.commands['rowspacing'] = {
  12105. execCommand : function( cmdName,value,dir ) {
  12106. this.execCommand('paragraph','p',{style:'margin-'+dir+':'+value + 'px'});
  12107. return true;
  12108. },
  12109. queryCommandValue : function(cmdName,dir) {
  12110. var pN = domUtils.filterNodeList(this.selection.getStartElementPath(),function(node){return domUtils.isBlockElm(node) }),
  12111. value;
  12112. //trace:1026
  12113. if(pN){
  12114. value = domUtils.getComputedStyle(pN,'margin-'+dir).replace(/[^\d]/g,'');
  12115. return !value ? 0 : value;
  12116. }
  12117. return 0;
  12118. }
  12119. };
  12120. };
  12121. // plugins/lineheight.js
  12122. /**
  12123. * 设置行内间距
  12124. * @file
  12125. * @since 1.2.6.1
  12126. */
  12127. UE.plugins['lineheight'] = function(){
  12128. var me = this;
  12129. me.setOpt({'lineheight':['1', '1.5','1.75','2', '3', '4', '5']});
  12130. /**
  12131. * 行距
  12132. * @command lineheight
  12133. * @method execCommand
  12134. * @param { String } cmdName 命令字符串
  12135. * @param { String } value 传入的行高值, 该值是当前字体的倍数, 例如: 1.5, 1.75
  12136. * @example
  12137. * ```javascript
  12138. * editor.execCommand( 'lineheight', 1.5);
  12139. * ```
  12140. */
  12141. /**
  12142. * 查询当前选区内容的行高大小
  12143. * @command lineheight
  12144. * @method queryCommandValue
  12145. * @param { String } cmd 命令字符串
  12146. * @return { String } 返回当前行高大小
  12147. * @example
  12148. * ```javascript
  12149. * editor.queryCommandValue( 'lineheight' );
  12150. * ```
  12151. */
  12152. me.commands['lineheight'] = {
  12153. execCommand : function( cmdName,value ) {
  12154. this.execCommand('paragraph','p',{style:'line-height:'+ (value == "1" ? "normal" : value + 'em') });
  12155. return true;
  12156. },
  12157. queryCommandValue : function() {
  12158. var pN = domUtils.filterNodeList(this.selection.getStartElementPath(),function(node){return domUtils.isBlockElm(node)});
  12159. if(pN){
  12160. var value = domUtils.getComputedStyle(pN,'line-height');
  12161. return value == 'normal' ? 1 : value.replace(/[^\d.]*/ig,"");
  12162. }
  12163. }
  12164. };
  12165. };
  12166. // plugins/insertcode.js
  12167. /**
  12168. * 插入代码插件
  12169. * @file
  12170. * @since 1.2.6.1
  12171. */
  12172. UE.plugins['insertcode'] = function() {
  12173. var me = this;
  12174. me.ready(function(){
  12175. utils.cssRule('pre','pre{margin:.5em 0;padding:.4em .6em;border-radius:8px;background:#f8f8f8;}',
  12176. me.document)
  12177. });
  12178. me.setOpt('insertcode',{
  12179. 'as3':'ActionScript3',
  12180. 'bash':'Bash/Shell',
  12181. 'cpp':'C/C++',
  12182. 'css':'Css',
  12183. 'cf':'CodeFunction',
  12184. 'c#':'C#',
  12185. 'delphi':'Delphi',
  12186. 'diff':'Diff',
  12187. 'erlang':'Erlang',
  12188. 'groovy':'Groovy',
  12189. 'html':'Html',
  12190. 'java':'Java',
  12191. 'jfx':'JavaFx',
  12192. 'js':'Javascript',
  12193. 'pl':'Perl',
  12194. 'php':'Php',
  12195. 'plain':'Plain Text',
  12196. 'ps':'PowerShell',
  12197. 'python':'Python',
  12198. 'ruby':'Ruby',
  12199. 'scala':'Scala',
  12200. 'sql':'Sql',
  12201. 'vb':'Vb',
  12202. 'xml':'Xml'
  12203. });
  12204. /**
  12205. * 插入代码
  12206. * @command insertcode
  12207. * @method execCommand
  12208. * @param { String } cmd 命令字符串
  12209. * @param { String } lang 插入代码的语言
  12210. * @example
  12211. * ```javascript
  12212. * editor.execCommand( 'insertcode', 'javascript' );
  12213. * ```
  12214. */
  12215. /**
  12216. * 如果选区所在位置是插入插入代码区域,返回代码的语言
  12217. * @command insertcode
  12218. * @method queryCommandValue
  12219. * @param { String } cmd 命令字符串
  12220. * @return { String } 返回代码的语言
  12221. * @example
  12222. * ```javascript
  12223. * editor.queryCommandValue( 'insertcode' );
  12224. * ```
  12225. */
  12226. me.commands['insertcode'] = {
  12227. execCommand : function(cmd,lang){
  12228. var me = this,
  12229. rng = me.selection.getRange(),
  12230. pre = domUtils.findParentByTagName(rng.startContainer,'pre',true);
  12231. if(pre){
  12232. pre.className = 'brush:'+lang+';toolbar:false;';
  12233. }else{
  12234. var code = '';
  12235. if(rng.collapsed){
  12236. code = browser.ie && browser.ie11below ? (browser.version <= 8 ? '&nbsp;':''):'<br/>';
  12237. }else{
  12238. var frag = rng.extractContents();
  12239. var div = me.document.createElement('div');
  12240. div.appendChild(frag);
  12241. utils.each(UE.filterNode(UE.htmlparser(div.innerHTML.replace(/[\r\t]/g,'')),me.options.filterTxtRules).children,function(node){
  12242. if(browser.ie && browser.ie11below && browser.version > 8){
  12243. if(node.type =='element'){
  12244. if(node.tagName == 'br'){
  12245. code += '\n'
  12246. }else if(!dtd.$empty[node.tagName]){
  12247. utils.each(node.children,function(cn){
  12248. if(cn.type =='element'){
  12249. if(cn.tagName == 'br'){
  12250. code += '\n'
  12251. }else if(!dtd.$empty[node.tagName]){
  12252. code += cn.innerText();
  12253. }
  12254. }else{
  12255. code += cn.data
  12256. }
  12257. })
  12258. if(!/\n$/.test(code)){
  12259. code += '\n';
  12260. }
  12261. }
  12262. }else{
  12263. code += node.data + '\n'
  12264. }
  12265. if(!node.nextSibling() && /\n$/.test(code)){
  12266. code = code.replace(/\n$/,'');
  12267. }
  12268. }else{
  12269. if(browser.ie && browser.ie11below){
  12270. if(node.type =='element'){
  12271. if(node.tagName == 'br'){
  12272. code += '<br>'
  12273. }else if(!dtd.$empty[node.tagName]){
  12274. utils.each(node.children,function(cn){
  12275. if(cn.type =='element'){
  12276. if(cn.tagName == 'br'){
  12277. code += '<br>'
  12278. }else if(!dtd.$empty[node.tagName]){
  12279. code += cn.innerText();
  12280. }
  12281. }else{
  12282. code += cn.data
  12283. }
  12284. });
  12285. if(!/br>$/.test(code)){
  12286. code += '<br>';
  12287. }
  12288. }
  12289. }else{
  12290. code += node.data + '<br>'
  12291. }
  12292. if(!node.nextSibling() && /<br>$/.test(code)){
  12293. code = code.replace(/<br>$/,'');
  12294. }
  12295. }else{
  12296. code += (node.type == 'element' ? (dtd.$empty[node.tagName] ? '' : node.innerText()) : node.data);
  12297. if(!/br\/?\s*>$/.test(code)){
  12298. if(!node.nextSibling())
  12299. return;
  12300. code += '<br>'
  12301. }
  12302. }
  12303. }
  12304. });
  12305. }
  12306. me.execCommand('inserthtml','<pre id="coder"class="brush:'+lang+';toolbar:false">'+code+'</pre>',true);
  12307. pre = me.document.getElementById('coder');
  12308. domUtils.removeAttributes(pre,'id');
  12309. var tmpNode = pre.previousSibling;
  12310. if(tmpNode && (tmpNode.nodeType == 3 && tmpNode.nodeValue.length == 1 && browser.ie && browser.version == 6 || domUtils.isEmptyBlock(tmpNode))){
  12311. domUtils.remove(tmpNode)
  12312. }
  12313. var rng = me.selection.getRange();
  12314. if(domUtils.isEmptyBlock(pre)){
  12315. rng.setStart(pre,0).setCursor(false,true)
  12316. }else{
  12317. rng.selectNodeContents(pre).select()
  12318. }
  12319. }
  12320. },
  12321. queryCommandValue : function(){
  12322. var path = this.selection.getStartElementPath();
  12323. var lang = '';
  12324. utils.each(path,function(node){
  12325. if(node.nodeName =='PRE'){
  12326. var match = node.className.match(/brush:([^;]+)/);
  12327. lang = match && match[1] ? match[1] : '';
  12328. return false;
  12329. }
  12330. });
  12331. return lang;
  12332. }
  12333. };
  12334. me.addInputRule(function(root){
  12335. utils.each(root.getNodesByTagName('pre'),function(pre){
  12336. var brs = pre.getNodesByTagName('br');
  12337. if(brs.length){
  12338. browser.ie && browser.ie11below && browser.version > 8 && utils.each(brs,function(br){
  12339. var txt = UE.uNode.createText('\n');
  12340. br.parentNode.insertBefore(txt,br);
  12341. br.parentNode.removeChild(br);
  12342. });
  12343. return;
  12344. }
  12345. if(browser.ie && browser.ie11below && browser.version > 8)
  12346. return;
  12347. var code = pre.innerText().split(/\n/);
  12348. pre.innerHTML('');
  12349. utils.each(code,function(c){
  12350. if(c.length){
  12351. pre.appendChild(UE.uNode.createText(c));
  12352. }
  12353. pre.appendChild(UE.uNode.createElement('br'))
  12354. })
  12355. })
  12356. });
  12357. me.addOutputRule(function(root){
  12358. utils.each(root.getNodesByTagName('pre'),function(pre){
  12359. var code = '';
  12360. utils.each(pre.children,function(n){
  12361. if(n.type == 'text'){
  12362. //在ie下文本内容有可能末尾带有\n要去掉
  12363. //trace:3396
  12364. code += n.data.replace(/[ ]/g,'&nbsp;').replace(/\n$/,'');
  12365. }else{
  12366. if(n.tagName == 'br'){
  12367. code += '\n'
  12368. }else{
  12369. code += (!dtd.$empty[n.tagName] ? '' : n.innerText());
  12370. }
  12371. }
  12372. });
  12373. pre.innerText(code.replace(/(&nbsp;|\n)+$/,''))
  12374. })
  12375. });
  12376. //不需要判断highlight的command列表
  12377. me.notNeedCodeQuery ={
  12378. help:1,
  12379. undo:1,
  12380. redo:1,
  12381. source:1,
  12382. print:1,
  12383. searchreplace:1,
  12384. fullscreen:1,
  12385. preview:1,
  12386. insertparagraph:1,
  12387. elementpath:1,
  12388. insertcode:1,
  12389. inserthtml:1,
  12390. selectall:1
  12391. };
  12392. //将queyCommamndState重置
  12393. var orgQuery = me.queryCommandState;
  12394. me.queryCommandState = function(cmd){
  12395. var me = this;
  12396. if(!me.notNeedCodeQuery[cmd.toLowerCase()] && me.selection && me.queryCommandValue('insertcode')){
  12397. return -1;
  12398. }
  12399. return UE.Editor.prototype.queryCommandState.apply(this,arguments)
  12400. };
  12401. me.addListener('beforeenterkeydown',function(){
  12402. var rng = me.selection.getRange();
  12403. var pre = domUtils.findParentByTagName(rng.startContainer,'pre',true);
  12404. if(pre){
  12405. me.fireEvent('saveScene');
  12406. if(!rng.collapsed){
  12407. rng.deleteContents();
  12408. }
  12409. if(!browser.ie || browser.ie9above){
  12410. var tmpNode = me.document.createElement('br'),pre;
  12411. rng.insertNode(tmpNode).setStartAfter(tmpNode).collapse(true);
  12412. var next = tmpNode.nextSibling;
  12413. if(!next && (!browser.ie || browser.version > 10)){
  12414. rng.insertNode(tmpNode.cloneNode(false));
  12415. }else{
  12416. rng.setStartAfter(tmpNode);
  12417. }
  12418. pre = tmpNode.previousSibling;
  12419. var tmp;
  12420. while(pre ){
  12421. tmp = pre;
  12422. pre = pre.previousSibling;
  12423. if(!pre || pre.nodeName == 'BR'){
  12424. pre = tmp;
  12425. break;
  12426. }
  12427. }
  12428. if(pre){
  12429. var str = '';
  12430. while(pre && pre.nodeName != 'BR' && new RegExp('^[\\s'+domUtils.fillChar+']*$').test(pre.nodeValue)){
  12431. str += pre.nodeValue;
  12432. pre = pre.nextSibling;
  12433. }
  12434. if(pre.nodeName != 'BR'){
  12435. var match = pre.nodeValue.match(new RegExp('^([\\s'+domUtils.fillChar+']+)'));
  12436. if(match && match[1]){
  12437. str += match[1]
  12438. }
  12439. }
  12440. if(str){
  12441. str = me.document.createTextNode(str);
  12442. rng.insertNode(str).setStartAfter(str);
  12443. }
  12444. }
  12445. rng.collapse(true).select(true);
  12446. }else{
  12447. if(browser.version > 8){
  12448. var txt = me.document.createTextNode('\n');
  12449. var start = rng.startContainer;
  12450. if(rng.startOffset == 0){
  12451. var preNode = start.previousSibling;
  12452. if(preNode){
  12453. rng.insertNode(txt);
  12454. var fillchar = me.document.createTextNode(' ');
  12455. rng.setStartAfter(txt).insertNode(fillchar).setStart(fillchar,0).collapse(true).select(true)
  12456. }
  12457. }else{
  12458. rng.insertNode(txt).setStartAfter(txt);
  12459. var fillchar = me.document.createTextNode(' ');
  12460. start = rng.startContainer.childNodes[rng.startOffset];
  12461. if(start && !/^\n/.test(start.nodeValue)){
  12462. rng.setStartBefore(txt)
  12463. }
  12464. rng.insertNode(fillchar).setStart(fillchar,0).collapse(true).select(true)
  12465. }
  12466. }else{
  12467. var tmpNode = me.document.createElement('br');
  12468. rng.insertNode(tmpNode);
  12469. rng.insertNode(me.document.createTextNode(domUtils.fillChar));
  12470. rng.setStartAfter(tmpNode);
  12471. pre = tmpNode.previousSibling;
  12472. var tmp;
  12473. while(pre ){
  12474. tmp = pre;
  12475. pre = pre.previousSibling;
  12476. if(!pre || pre.nodeName == 'BR'){
  12477. pre = tmp;
  12478. break;
  12479. }
  12480. }
  12481. if(pre){
  12482. var str = '';
  12483. while(pre && pre.nodeName != 'BR' && new RegExp('^[ '+domUtils.fillChar+']*$').test(pre.nodeValue)){
  12484. str += pre.nodeValue;
  12485. pre = pre.nextSibling;
  12486. }
  12487. if(pre.nodeName != 'BR'){
  12488. var match = pre.nodeValue.match(new RegExp('^([ '+domUtils.fillChar+']+)'));
  12489. if(match && match[1]){
  12490. str += match[1]
  12491. }
  12492. }
  12493. str = me.document.createTextNode(str);
  12494. rng.insertNode(str).setStartAfter(str);
  12495. }
  12496. rng.collapse(true).select();
  12497. }
  12498. }
  12499. me.fireEvent('saveScene');
  12500. return true;
  12501. }
  12502. });
  12503. me.addListener('tabkeydown',function(cmd,evt){
  12504. var rng = me.selection.getRange();
  12505. var pre = domUtils.findParentByTagName(rng.startContainer,'pre',true);
  12506. if(pre){
  12507. me.fireEvent('saveScene');
  12508. if(evt.shiftKey){
  12509. }else{
  12510. if(!rng.collapsed){
  12511. var bk = rng.createBookmark();
  12512. var start = bk.start.previousSibling;
  12513. while(start){
  12514. if(pre.firstChild === start && !domUtils.isBr(start)){
  12515. pre.insertBefore(me.document.createTextNode(' '),start);
  12516. break;
  12517. }
  12518. if(domUtils.isBr(start)){
  12519. pre.insertBefore(me.document.createTextNode(' '),start.nextSibling);
  12520. break;
  12521. }
  12522. start = start.previousSibling;
  12523. }
  12524. var end = bk.end;
  12525. start = bk.start.nextSibling;
  12526. if(pre.firstChild === bk.start){
  12527. pre.insertBefore(me.document.createTextNode(' '),start.nextSibling)
  12528. }
  12529. while(start && start !== end){
  12530. if(domUtils.isBr(start) && start.nextSibling){
  12531. if(start.nextSibling === end){
  12532. break;
  12533. }
  12534. pre.insertBefore(me.document.createTextNode(' '),start.nextSibling)
  12535. }
  12536. start = start.nextSibling;
  12537. }
  12538. rng.moveToBookmark(bk).select();
  12539. }else{
  12540. var tmpNode = me.document.createTextNode(' ');
  12541. rng.insertNode(tmpNode).setStartAfter(tmpNode).collapse(true).select(true);
  12542. }
  12543. }
  12544. me.fireEvent('saveScene');
  12545. return true;
  12546. }
  12547. });
  12548. me.addListener('beforeinserthtml',function(evtName,html){
  12549. var me = this,
  12550. rng = me.selection.getRange(),
  12551. pre = domUtils.findParentByTagName(rng.startContainer,'pre',true);
  12552. if(pre){
  12553. if(!rng.collapsed){
  12554. rng.deleteContents()
  12555. }
  12556. var htmlstr = '';
  12557. if(browser.ie && browser.version > 8){
  12558. utils.each(UE.filterNode(UE.htmlparser(html),me.options.filterTxtRules).children,function(node){
  12559. if(node.type =='element'){
  12560. if(node.tagName == 'br'){
  12561. htmlstr += '\n'
  12562. }else if(!dtd.$empty[node.tagName]){
  12563. utils.each(node.children,function(cn){
  12564. if(cn.type =='element'){
  12565. if(cn.tagName == 'br'){
  12566. htmlstr += '\n'
  12567. }else if(!dtd.$empty[node.tagName]){
  12568. htmlstr += cn.innerText();
  12569. }
  12570. }else{
  12571. htmlstr += cn.data
  12572. }
  12573. })
  12574. if(!/\n$/.test(htmlstr)){
  12575. htmlstr += '\n';
  12576. }
  12577. }
  12578. }else{
  12579. htmlstr += node.data + '\n'
  12580. }
  12581. if(!node.nextSibling() && /\n$/.test(htmlstr)){
  12582. htmlstr = htmlstr.replace(/\n$/,'');
  12583. }
  12584. });
  12585. var tmpNode = me.document.createTextNode(utils.html(htmlstr.replace(/&nbsp;/g,' ')));
  12586. rng.insertNode(tmpNode).selectNode(tmpNode).select();
  12587. }else{
  12588. var frag = me.document.createDocumentFragment();
  12589. utils.each(UE.filterNode(UE.htmlparser(html),me.options.filterTxtRules).children,function(node){
  12590. if(node.type =='element'){
  12591. if(node.tagName == 'br'){
  12592. frag.appendChild(me.document.createElement('br'))
  12593. }else if(!dtd.$empty[node.tagName]){
  12594. utils.each(node.children,function(cn){
  12595. if(cn.type =='element'){
  12596. if(cn.tagName == 'br'){
  12597. frag.appendChild(me.document.createElement('br'))
  12598. }else if(!dtd.$empty[node.tagName]){
  12599. frag.appendChild(me.document.createTextNode(utils.html(cn.innerText().replace(/&nbsp;/g,' '))));
  12600. }
  12601. }else{
  12602. frag.appendChild(me.document.createTextNode(utils.html( cn.data.replace(/&nbsp;/g,' '))));
  12603. }
  12604. })
  12605. if(frag.lastChild.nodeName != 'BR'){
  12606. frag.appendChild(me.document.createElement('br'))
  12607. }
  12608. }
  12609. }else{
  12610. frag.appendChild(me.document.createTextNode(utils.html( node.data.replace(/&nbsp;/g,' '))));
  12611. }
  12612. if(!node.nextSibling() && frag.lastChild.nodeName == 'BR'){
  12613. frag.removeChild(frag.lastChild)
  12614. }
  12615. });
  12616. rng.insertNode(frag).select();
  12617. }
  12618. return true;
  12619. }
  12620. });
  12621. //方向键的处理
  12622. me.addListener('keydown',function(cmd,evt){
  12623. var me = this,keyCode = evt.keyCode || evt.which;
  12624. if(keyCode == 40){
  12625. var rng = me.selection.getRange(),pre,start = rng.startContainer;
  12626. if(rng.collapsed && (pre = domUtils.findParentByTagName(rng.startContainer,'pre',true)) && !pre.nextSibling){
  12627. var last = pre.lastChild
  12628. while(last && last.nodeName == 'BR'){
  12629. last = last.previousSibling;
  12630. }
  12631. if(last === start || rng.startContainer === pre && rng.startOffset == pre.childNodes.length){
  12632. me.execCommand('insertparagraph');
  12633. domUtils.preventDefault(evt)
  12634. }
  12635. }
  12636. }
  12637. });
  12638. //trace:3395
  12639. me.addListener('delkeydown',function(type,evt){
  12640. var rng = this.selection.getRange();
  12641. rng.txtToElmBoundary(true);
  12642. var start = rng.startContainer;
  12643. if(domUtils.isTagNode(start,'pre') && rng.collapsed && domUtils.isStartInblock(rng)){
  12644. var p = me.document.createElement('p');
  12645. domUtils.fillNode(me.document,p);
  12646. start.parentNode.insertBefore(p,start);
  12647. domUtils.remove(start);
  12648. rng.setStart(p,0).setCursor(false,true);
  12649. domUtils.preventDefault(evt);
  12650. return true;
  12651. }
  12652. })
  12653. };
  12654. // plugins/cleardoc.js
  12655. /**
  12656. * 清空文档插件
  12657. * @file
  12658. * @since 1.2.6.1
  12659. */
  12660. /**
  12661. * 清空文档
  12662. * @command cleardoc
  12663. * @method execCommand
  12664. * @param { String } cmd 命令字符串
  12665. * @example
  12666. * ```javascript
  12667. * //editor 是编辑器实例
  12668. * editor.execCommand('cleardoc');
  12669. * ```
  12670. */
  12671. UE.commands['cleardoc'] = {
  12672. execCommand : function( cmdName) {
  12673. var me = this,
  12674. enterTag = me.options.enterTag,
  12675. range = me.selection.getRange();
  12676. if(enterTag == "br"){
  12677. me.body.innerHTML = "<br/>";
  12678. range.setStart(me.body,0).setCursor();
  12679. }else{
  12680. me.body.innerHTML = "<p>"+(ie ? "" : "<br/>")+"</p>";
  12681. range.setStart(me.body.firstChild,0).setCursor(false,true);
  12682. }
  12683. setTimeout(function(){
  12684. me.fireEvent("clearDoc");
  12685. },0);
  12686. }
  12687. };
  12688. // plugins/anchor.js
  12689. /**
  12690. * 锚点插件,为UEditor提供插入锚点支持
  12691. * @file
  12692. * @since 1.2.6.1
  12693. */
  12694. UE.plugin.register('anchor', function (){
  12695. return {
  12696. bindEvents:{
  12697. 'ready':function(){
  12698. utils.cssRule('anchor',
  12699. '.anchorclass{background: url(\''
  12700. + this.options.themePath
  12701. + this.options.theme +'/images/anchor.gif\') no-repeat scroll left center transparent;cursor: auto;display: inline-block;height: 16px;width: 15px;}',
  12702. this.document);
  12703. }
  12704. },
  12705. outputRule: function(root){
  12706. utils.each(root.getNodesByTagName('img'),function(a){
  12707. var val;
  12708. if(val = a.getAttr('anchorname')){
  12709. a.tagName = 'a';
  12710. a.setAttr({
  12711. anchorname : '',
  12712. name : val,
  12713. 'class' : ''
  12714. })
  12715. }
  12716. })
  12717. },
  12718. inputRule:function(root){
  12719. utils.each(root.getNodesByTagName('a'),function(a){
  12720. var val;
  12721. if((val = a.getAttr('name')) && !a.getAttr('href')){
  12722. a.tagName = 'img';
  12723. a.setAttr({
  12724. anchorname :a.getAttr('name'),
  12725. 'class' : 'anchorclass'
  12726. });
  12727. a.setAttr('name')
  12728. }
  12729. })
  12730. },
  12731. commands:{
  12732. /**
  12733. * 插入锚点
  12734. * @command anchor
  12735. * @method execCommand
  12736. * @param { String } cmd 命令字符串
  12737. * @param { String } name 锚点名称字符串
  12738. * @example
  12739. * ```javascript
  12740. * //editor 是编辑器实例
  12741. * editor.execCommand('anchor', 'anchor1');
  12742. * ```
  12743. */
  12744. 'anchor':{
  12745. execCommand:function (cmd, name) {
  12746. var range = this.selection.getRange(),img = range.getClosedNode();
  12747. if (img && img.getAttribute('anchorname')) {
  12748. if (name) {
  12749. img.setAttribute('anchorname', name);
  12750. } else {
  12751. range.setStartBefore(img).setCursor();
  12752. domUtils.remove(img);
  12753. }
  12754. } else {
  12755. if (name) {
  12756. //只在选区的开始插入
  12757. var anchor = this.document.createElement('img');
  12758. range.collapse(true);
  12759. domUtils.setAttributes(anchor,{
  12760. 'anchorname':name,
  12761. 'class':'anchorclass'
  12762. });
  12763. range.insertNode(anchor).setStartAfter(anchor).setCursor(false,true);
  12764. }
  12765. }
  12766. }
  12767. }
  12768. }
  12769. }
  12770. });
  12771. // plugins/wordcount.js
  12772. ///import core
  12773. ///commands 字数统计
  12774. ///commandsName WordCount,wordCount
  12775. ///commandsTitle 字数统计
  12776. /*
  12777. * Created by JetBrains WebStorm.
  12778. * User: taoqili
  12779. * Date: 11-9-7
  12780. * Time: 下午8:18
  12781. * To change this template use File | Settings | File Templates.
  12782. */
  12783. UE.plugins['wordcount'] = function(){
  12784. var me = this;
  12785. me.setOpt('wordCount',true);
  12786. me.addListener('contentchange',function(){
  12787. me.fireEvent('wordcount');
  12788. });
  12789. var timer;
  12790. me.addListener('ready',function(){
  12791. var me = this;
  12792. domUtils.on(me.body,"keyup",function(evt){
  12793. var code = evt.keyCode||evt.which,
  12794. //忽略的按键,ctr,alt,shift,方向键
  12795. ignores = {"16":1,"18":1,"20":1,"37":1,"38":1,"39":1,"40":1};
  12796. if(code in ignores) return;
  12797. clearTimeout(timer);
  12798. timer = setTimeout(function(){
  12799. me.fireEvent('wordcount');
  12800. },200)
  12801. })
  12802. });
  12803. };
  12804. // plugins/pagebreak.js
  12805. /**
  12806. * 分页功能插件
  12807. * @file
  12808. * @since 1.2.6.1
  12809. */
  12810. UE.plugins['pagebreak'] = function () {
  12811. var me = this,
  12812. notBreakTags = ['td'];
  12813. me.setOpt('pageBreakTag','_ueditor_page_break_tag_');
  12814. function fillNode(node){
  12815. if(domUtils.isEmptyBlock(node)){
  12816. var firstChild = node.firstChild,tmpNode;
  12817. while(firstChild && firstChild.nodeType == 1 && domUtils.isEmptyBlock(firstChild)){
  12818. tmpNode = firstChild;
  12819. firstChild = firstChild.firstChild;
  12820. }
  12821. !tmpNode && (tmpNode = node);
  12822. domUtils.fillNode(me.document,tmpNode);
  12823. }
  12824. }
  12825. //分页符样式添加
  12826. me.ready(function(){
  12827. utils.cssRule('pagebreak','.pagebreak{display:block;clear:both !important;cursor:default !important;width: 100% !important;margin:0;}',me.document);
  12828. });
  12829. function isHr(node){
  12830. return node && node.nodeType == 1 && node.tagName == 'HR' && node.className == 'pagebreak';
  12831. }
  12832. me.addInputRule(function(root){
  12833. root.traversal(function(node){
  12834. if(node.type == 'text' && node.data == me.options.pageBreakTag){
  12835. var hr = UE.uNode.createElement('<hr class="pagebreak" noshade="noshade" size="5" style="-webkit-user-select: none;">');
  12836. node.parentNode.insertBefore(hr,node);
  12837. node.parentNode.removeChild(node)
  12838. }
  12839. })
  12840. });
  12841. me.addOutputRule(function(node){
  12842. utils.each(node.getNodesByTagName('hr'),function(n){
  12843. if(n.getAttr('class') == 'pagebreak'){
  12844. var txt = UE.uNode.createText(me.options.pageBreakTag);
  12845. n.parentNode.insertBefore(txt,n);
  12846. n.parentNode.removeChild(n);
  12847. }
  12848. })
  12849. });
  12850. /**
  12851. * 插入分页符
  12852. * @command pagebreak
  12853. * @method execCommand
  12854. * @param { String } cmd 命令字符串
  12855. * @remind 在表格中插入分页符会把表格切分成两部分
  12856. * @remind 获取编辑器内的数据时, 编辑器会把分页符转换成“_ueditor_page_break_tag_”字符串,
  12857. * 以便于提交数据到服务器端后处理分页。
  12858. * @example
  12859. * ```javascript
  12860. * editor.execCommand( 'pagebreak'); //插入一个hr标签,带有样式类名pagebreak
  12861. * ```
  12862. */
  12863. me.commands['pagebreak'] = {
  12864. execCommand:function () {
  12865. var range = me.selection.getRange(),hr = me.document.createElement('hr');
  12866. domUtils.setAttributes(hr,{
  12867. 'class' : 'pagebreak',
  12868. noshade:"noshade",
  12869. size:"5"
  12870. });
  12871. domUtils.unSelectable(hr);
  12872. //table单独处理
  12873. var node = domUtils.findParentByTagName(range.startContainer, notBreakTags, true),
  12874. parents = [], pN;
  12875. if (node) {
  12876. switch (node.tagName) {
  12877. case 'TD':
  12878. pN = node.parentNode;
  12879. if (!pN.previousSibling) {
  12880. var table = domUtils.findParentByTagName(pN, 'table');
  12881. // var tableWrapDiv = table.parentNode;
  12882. // if(tableWrapDiv && tableWrapDiv.nodeType == 1
  12883. // && tableWrapDiv.tagName == 'DIV'
  12884. // && tableWrapDiv.getAttribute('dropdrag')
  12885. // ){
  12886. // domUtils.remove(tableWrapDiv,true);
  12887. // }
  12888. table.parentNode.insertBefore(hr, table);
  12889. parents = domUtils.findParents(hr, true);
  12890. } else {
  12891. pN.parentNode.insertBefore(hr, pN);
  12892. parents = domUtils.findParents(hr);
  12893. }
  12894. pN = parents[1];
  12895. if (hr !== pN) {
  12896. domUtils.breakParent(hr, pN);
  12897. }
  12898. //table要重写绑定一下拖拽
  12899. me.fireEvent('afteradjusttable',me.document);
  12900. }
  12901. } else {
  12902. if (!range.collapsed) {
  12903. range.deleteContents();
  12904. var start = range.startContainer;
  12905. while ( !domUtils.isBody(start) && domUtils.isBlockElm(start) && domUtils.isEmptyNode(start)) {
  12906. range.setStartBefore(start).collapse(true);
  12907. domUtils.remove(start);
  12908. start = range.startContainer;
  12909. }
  12910. }
  12911. range.insertNode(hr);
  12912. var pN = hr.parentNode, nextNode;
  12913. while (!domUtils.isBody(pN)) {
  12914. domUtils.breakParent(hr, pN);
  12915. nextNode = hr.nextSibling;
  12916. if (nextNode && domUtils.isEmptyBlock(nextNode)) {
  12917. domUtils.remove(nextNode);
  12918. }
  12919. pN = hr.parentNode;
  12920. }
  12921. nextNode = hr.nextSibling;
  12922. var pre = hr.previousSibling;
  12923. if(isHr(pre)){
  12924. domUtils.remove(pre);
  12925. }else{
  12926. pre && fillNode(pre);
  12927. }
  12928. if(!nextNode){
  12929. var p = me.document.createElement('p');
  12930. hr.parentNode.appendChild(p);
  12931. domUtils.fillNode(me.document,p);
  12932. range.setStart(p,0).collapse(true);
  12933. }else{
  12934. if(isHr(nextNode)){
  12935. domUtils.remove(nextNode);
  12936. }else{
  12937. fillNode(nextNode);
  12938. }
  12939. range.setEndAfter(hr).collapse(false);
  12940. }
  12941. range.select(true);
  12942. }
  12943. }
  12944. };
  12945. };
  12946. // plugins/wordimage.js
  12947. ///import core
  12948. ///commands 本地图片引导上传
  12949. ///commandsName WordImage
  12950. ///commandsTitle 本地图片引导上传
  12951. ///commandsDialog dialogs\wordimage
  12952. UE.plugin.register('wordimage',function(){
  12953. var me = this,
  12954. images = [];
  12955. return {
  12956. commands : {
  12957. 'wordimage':{
  12958. execCommand:function () {
  12959. var images = domUtils.getElementsByTagName(me.body, "img");
  12960. var urlList = [];
  12961. for (var i = 0, ci; ci = images[i++];) {
  12962. var url = ci.getAttribute("word_img");
  12963. url && urlList.push(url);
  12964. }
  12965. return urlList;
  12966. },
  12967. queryCommandState:function () {
  12968. images = domUtils.getElementsByTagName(me.body, "img");
  12969. for (var i = 0, ci; ci = images[i++];) {
  12970. if (ci.getAttribute("word_img")) {
  12971. return 1;
  12972. }
  12973. }
  12974. return -1;
  12975. },
  12976. notNeedUndo:true
  12977. }
  12978. },
  12979. inputRule : function (root) {
  12980. utils.each(root.getNodesByTagName('img'), function (img) {
  12981. var attrs = img.attrs,
  12982. flag = parseInt(attrs.width) < 128 || parseInt(attrs.height) < 43,
  12983. opt = me.options,
  12984. src = opt.UEDITOR_HOME_URL + 'themes/default/images/spacer.gif';
  12985. if (attrs['src'] && /^(?:(file:\/+))/.test(attrs['src'])) {
  12986. img.setAttr({
  12987. width:attrs.width,
  12988. height:attrs.height,
  12989. alt:attrs.alt,
  12990. word_img: attrs.src,
  12991. src:src,
  12992. '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'
  12993. })
  12994. }
  12995. })
  12996. }
  12997. }
  12998. });
  12999. // plugins/dragdrop.js
  13000. UE.plugins['dragdrop'] = function (){
  13001. var me = this;
  13002. me.ready(function(){
  13003. domUtils.on(this.body,'dragend',function(){
  13004. var rng = me.selection.getRange();
  13005. var node = rng.getClosedNode()||me.selection.getStart();
  13006. if(node && node.tagName == 'IMG'){
  13007. var pre = node.previousSibling,next;
  13008. while(next = node.nextSibling){
  13009. if(next.nodeType == 1 && next.tagName == 'SPAN' && !next.firstChild){
  13010. domUtils.remove(next)
  13011. }else{
  13012. break;
  13013. }
  13014. }
  13015. if((pre && pre.nodeType == 1 && !domUtils.isEmptyBlock(pre) || !pre) && (!next || next && !domUtils.isEmptyBlock(next))){
  13016. if(pre && pre.tagName == 'P' && !domUtils.isEmptyBlock(pre)){
  13017. pre.appendChild(node);
  13018. domUtils.moveChild(next,pre);
  13019. domUtils.remove(next);
  13020. }else if(next && next.tagName == 'P' && !domUtils.isEmptyBlock(next)){
  13021. next.insertBefore(node,next.firstChild);
  13022. }
  13023. if(pre && pre.tagName == 'P' && domUtils.isEmptyBlock(pre)){
  13024. domUtils.remove(pre)
  13025. }
  13026. if(next && next.tagName == 'P' && domUtils.isEmptyBlock(next)){
  13027. domUtils.remove(next)
  13028. }
  13029. rng.selectNode(node).select();
  13030. me.fireEvent('saveScene');
  13031. }
  13032. }
  13033. })
  13034. });
  13035. me.addListener('keyup', function(type, evt) {
  13036. var keyCode = evt.keyCode || evt.which;
  13037. if (keyCode == 13) {
  13038. var rng = me.selection.getRange(),node;
  13039. if(node = domUtils.findParentByTagName(rng.startContainer,'p',true)){
  13040. if(domUtils.getComputedStyle(node,'text-align') == 'center'){
  13041. domUtils.removeStyle(node,'text-align')
  13042. }
  13043. }
  13044. }
  13045. })
  13046. };
  13047. // plugins/undo.js
  13048. /**
  13049. * undo redo
  13050. * @file
  13051. * @since 1.2.6.1
  13052. */
  13053. /**
  13054. * 撤销上一次执行的命令
  13055. * @command undo
  13056. * @method execCommand
  13057. * @param { String } cmd 命令字符串
  13058. * @example
  13059. * ```javascript
  13060. * editor.execCommand( 'undo' );
  13061. * ```
  13062. */
  13063. /**
  13064. * 重做上一次执行的命令
  13065. * @command redo
  13066. * @method execCommand
  13067. * @param { String } cmd 命令字符串
  13068. * @example
  13069. * ```javascript
  13070. * editor.execCommand( 'redo' );
  13071. * ```
  13072. */
  13073. UE.plugins['undo'] = function () {
  13074. var saveSceneTimer;
  13075. var me = this,
  13076. maxUndoCount = me.options.maxUndoCount || 20,
  13077. maxInputCount = me.options.maxInputCount || 20,
  13078. fillchar = new RegExp(domUtils.fillChar + '|<\/hr>', 'gi');// ie会产生多余的</hr>
  13079. var noNeedFillCharTags = {
  13080. ol:1,ul:1,table:1,tbody:1,tr:1,body:1
  13081. };
  13082. var orgState = me.options.autoClearEmptyNode;
  13083. function compareAddr(indexA, indexB) {
  13084. if (indexA.length != indexB.length)
  13085. return 0;
  13086. for (var i = 0, l = indexA.length; i < l; i++) {
  13087. if (indexA[i] != indexB[i])
  13088. return 0
  13089. }
  13090. return 1;
  13091. }
  13092. function compareRangeAddress(rngAddrA, rngAddrB) {
  13093. if (rngAddrA.collapsed != rngAddrB.collapsed) {
  13094. return 0;
  13095. }
  13096. if (!compareAddr(rngAddrA.startAddress, rngAddrB.startAddress) || !compareAddr(rngAddrA.endAddress, rngAddrB.endAddress)) {
  13097. return 0;
  13098. }
  13099. return 1;
  13100. }
  13101. function UndoManager() {
  13102. this.list = [];
  13103. this.index = 0;
  13104. this.hasUndo = false;
  13105. this.hasRedo = false;
  13106. this.undo = function () {
  13107. if (this.hasUndo) {
  13108. if (!this.list[this.index - 1] && this.list.length == 1) {
  13109. this.reset();
  13110. return;
  13111. }
  13112. while (this.list[this.index].content == this.list[this.index - 1].content) {
  13113. this.index--;
  13114. if (this.index == 0) {
  13115. return this.restore(0);
  13116. }
  13117. }
  13118. this.restore(--this.index);
  13119. }
  13120. };
  13121. this.redo = function () {
  13122. if (this.hasRedo) {
  13123. while (this.list[this.index].content == this.list[this.index + 1].content) {
  13124. this.index++;
  13125. if (this.index == this.list.length - 1) {
  13126. return this.restore(this.index);
  13127. }
  13128. }
  13129. this.restore(++this.index);
  13130. }
  13131. };
  13132. this.restore = function () {
  13133. var me = this.editor;
  13134. var scene = this.list[this.index];
  13135. var root = UE.htmlparser(scene.content.replace(fillchar, ''));
  13136. me.options.autoClearEmptyNode = false;
  13137. me.filterInputRule(root);
  13138. me.options.autoClearEmptyNode = orgState;
  13139. //trace:873
  13140. //去掉展位符
  13141. me.document.body.innerHTML = root.toHtml();
  13142. me.fireEvent('afterscencerestore');
  13143. //处理undo后空格不展位的问题
  13144. if (browser.ie) {
  13145. utils.each(domUtils.getElementsByTagName(me.document,'td th caption p'),function(node){
  13146. if(domUtils.isEmptyNode(node)){
  13147. domUtils.fillNode(me.document, node);
  13148. }
  13149. })
  13150. }
  13151. try{
  13152. var rng = new dom.Range(me.document).moveToAddress(scene.address);
  13153. rng.select(noNeedFillCharTags[rng.startContainer.nodeName.toLowerCase()]);
  13154. }catch(e){}
  13155. this.update();
  13156. this.clearKey();
  13157. //不能把自己reset了
  13158. me.fireEvent('reset', true);
  13159. };
  13160. this.getScene = function () {
  13161. var me = this.editor;
  13162. var rng = me.selection.getRange(),
  13163. rngAddress = rng.createAddress(false,true);
  13164. me.fireEvent('beforegetscene');
  13165. var root = UE.htmlparser(me.body.innerHTML);
  13166. me.options.autoClearEmptyNode = false;
  13167. me.filterOutputRule(root);
  13168. me.options.autoClearEmptyNode = orgState;
  13169. var cont = root.toHtml();
  13170. //trace:3461
  13171. //这个会引起回退时导致空格丢失的情况
  13172. // browser.ie && (cont = cont.replace(/>&nbsp;</g, '><').replace(/\s*</g, '<').replace(/>\s*/g, '>'));
  13173. me.fireEvent('aftergetscene');
  13174. return {
  13175. address:rngAddress,
  13176. content:cont
  13177. }
  13178. };
  13179. this.save = function (notCompareRange,notSetCursor) {
  13180. clearTimeout(saveSceneTimer);
  13181. var currentScene = this.getScene(notSetCursor),
  13182. lastScene = this.list[this.index];
  13183. if(lastScene && lastScene.content != currentScene.content){
  13184. me.trigger('contentchange')
  13185. }
  13186. //内容相同位置相同不存
  13187. if (lastScene && lastScene.content == currentScene.content &&
  13188. ( notCompareRange ? 1 : compareRangeAddress(lastScene.address, currentScene.address) )
  13189. ) {
  13190. return;
  13191. }
  13192. this.list = this.list.slice(0, this.index + 1);
  13193. this.list.push(currentScene);
  13194. //如果大于最大数量了,就把最前的剔除
  13195. if (this.list.length > maxUndoCount) {
  13196. this.list.shift();
  13197. }
  13198. this.index = this.list.length - 1;
  13199. this.clearKey();
  13200. //跟新undo/redo状态
  13201. this.update();
  13202. };
  13203. this.update = function () {
  13204. this.hasRedo = !!this.list[this.index + 1];
  13205. this.hasUndo = !!this.list[this.index - 1];
  13206. };
  13207. this.reset = function () {
  13208. this.list = [];
  13209. this.index = 0;
  13210. this.hasUndo = false;
  13211. this.hasRedo = false;
  13212. this.clearKey();
  13213. };
  13214. this.clearKey = function () {
  13215. keycont = 0;
  13216. lastKeyCode = null;
  13217. };
  13218. }
  13219. me.undoManger = new UndoManager();
  13220. me.undoManger.editor = me;
  13221. function saveScene() {
  13222. this.undoManger.save();
  13223. }
  13224. me.addListener('saveScene', function () {
  13225. var args = Array.prototype.splice.call(arguments,1);
  13226. this.undoManger.save.apply(this.undoManger,args);
  13227. });
  13228. // me.addListener('beforeexeccommand', saveScene);
  13229. // me.addListener('afterexeccommand', saveScene);
  13230. me.addListener('reset', function (type, exclude) {
  13231. if (!exclude) {
  13232. this.undoManger.reset();
  13233. }
  13234. });
  13235. me.commands['redo'] = me.commands['undo'] = {
  13236. execCommand:function (cmdName) {
  13237. this.undoManger[cmdName]();
  13238. },
  13239. queryCommandState:function (cmdName) {
  13240. return this.undoManger['has' + (cmdName.toLowerCase() == 'undo' ? 'Undo' : 'Redo')] ? 0 : -1;
  13241. },
  13242. notNeedUndo:1
  13243. };
  13244. var keys = {
  13245. // /*Backspace*/ 8:1, /*Delete*/ 46:1,
  13246. /*Shift*/ 16:1, /*Ctrl*/ 17:1, /*Alt*/ 18:1,
  13247. 37:1, 38:1, 39:1, 40:1
  13248. },
  13249. keycont = 0,
  13250. lastKeyCode;
  13251. //输入法状态下不计算字符数
  13252. var inputType = false;
  13253. me.addListener('ready', function () {
  13254. domUtils.on(this.body, 'compositionstart', function () {
  13255. inputType = true;
  13256. });
  13257. domUtils.on(this.body, 'compositionend', function () {
  13258. inputType = false;
  13259. })
  13260. });
  13261. //快捷键
  13262. me.addshortcutkey({
  13263. "Undo":"ctrl+90", //undo
  13264. "Redo":"ctrl+89" //redo
  13265. });
  13266. var isCollapsed = true;
  13267. me.addListener('keydown', function (type, evt) {
  13268. var me = this;
  13269. var keyCode = evt.keyCode || evt.which;
  13270. if (!keys[keyCode] && !evt.ctrlKey && !evt.metaKey && !evt.shiftKey && !evt.altKey) {
  13271. if (inputType)
  13272. return;
  13273. if(!me.selection.getRange().collapsed){
  13274. me.undoManger.save(false,true);
  13275. isCollapsed = false;
  13276. return;
  13277. }
  13278. if (me.undoManger.list.length == 0) {
  13279. me.undoManger.save(true);
  13280. }
  13281. clearTimeout(saveSceneTimer);
  13282. function save(cont){
  13283. cont.undoManger.save(false,true);
  13284. cont.fireEvent('selectionchange');
  13285. }
  13286. saveSceneTimer = setTimeout(function(){
  13287. if(inputType){
  13288. var interalTimer = setInterval(function(){
  13289. if(!inputType){
  13290. save(me);
  13291. clearInterval(interalTimer)
  13292. }
  13293. },300)
  13294. return;
  13295. }
  13296. save(me);
  13297. },200);
  13298. lastKeyCode = keyCode;
  13299. keycont++;
  13300. if (keycont >= maxInputCount ) {
  13301. save(me)
  13302. }
  13303. }
  13304. });
  13305. me.addListener('keyup', function (type, evt) {
  13306. var keyCode = evt.keyCode || evt.which;
  13307. if (!keys[keyCode] && !evt.ctrlKey && !evt.metaKey && !evt.shiftKey && !evt.altKey) {
  13308. if (inputType)
  13309. return;
  13310. if(!isCollapsed){
  13311. this.undoManger.save(false,true);
  13312. isCollapsed = true;
  13313. }
  13314. }
  13315. });
  13316. //扩展实例,添加关闭和开启命令undo
  13317. me.stopCmdUndo = function(){
  13318. me.__hasEnterExecCommand = true;
  13319. };
  13320. me.startCmdUndo = function(){
  13321. me.__hasEnterExecCommand = false;
  13322. }
  13323. };
  13324. // plugins/copy.js
  13325. UE.plugin.register('copy', function () {
  13326. var me = this;
  13327. function initZeroClipboard() {
  13328. ZeroClipboard.config({
  13329. debug: false,
  13330. swfPath: me.options.UEDITOR_HOME_URL + 'third-party/zeroclipboard/ZeroClipboard.swf'
  13331. });
  13332. var client = me.zeroclipboard = new ZeroClipboard();
  13333. // 复制内容
  13334. client.on('copy', function (e) {
  13335. var client = e.client,
  13336. rng = me.selection.getRange(),
  13337. div = document.createElement('div');
  13338. div.appendChild(rng.cloneContents());
  13339. client.setText(div.innerText || div.textContent);
  13340. client.setHtml(div.innerHTML);
  13341. rng.select();
  13342. });
  13343. // hover事件传递到target
  13344. client.on('mouseover mouseout', function (e) {
  13345. var target = e.target;
  13346. if (e.type == 'mouseover') {
  13347. domUtils.addClass(target, 'edui-state-hover');
  13348. } else if (e.type == 'mouseout') {
  13349. domUtils.removeClasses(target, 'edui-state-hover');
  13350. }
  13351. });
  13352. // flash加载不成功
  13353. client.on('wrongflash noflash', function () {
  13354. ZeroClipboard.destroy();
  13355. });
  13356. }
  13357. return {
  13358. bindEvents: {
  13359. 'ready': function () {
  13360. if (!browser.ie) {
  13361. if (window.ZeroClipboard) {
  13362. initZeroClipboard();
  13363. } else {
  13364. utils.loadFile(document, {
  13365. src: me.options.UEDITOR_HOME_URL + "third-party/zeroclipboard/ZeroClipboard.js",
  13366. tag: "script",
  13367. type: "text/javascript",
  13368. defer: "defer"
  13369. }, function () {
  13370. initZeroClipboard();
  13371. });
  13372. }
  13373. }
  13374. }
  13375. },
  13376. commands: {
  13377. 'copy': {
  13378. execCommand: function (cmd) {
  13379. if (!me.document.execCommand('copy')) {
  13380. alert(me.getLang('copymsg'));
  13381. }
  13382. }
  13383. }
  13384. }
  13385. }
  13386. });
  13387. // plugins/paste.js
  13388. ///import core
  13389. ///import plugins/inserthtml.js
  13390. ///import plugins/undo.js
  13391. ///import plugins/serialize.js
  13392. ///commands 粘贴
  13393. ///commandsName PastePlain
  13394. ///commandsTitle 纯文本粘贴模式
  13395. /**
  13396. * @description 粘贴
  13397. * @author zhanyi
  13398. */
  13399. UE.plugins['paste'] = function () {
  13400. function getClipboardData(callback) {
  13401. var doc = this.document;
  13402. if (doc.getElementById('baidu_pastebin')) {
  13403. return;
  13404. }
  13405. var range = this.selection.getRange(),
  13406. bk = range.createBookmark(),
  13407. //创建剪贴的容器div
  13408. pastebin = doc.createElement('div');
  13409. pastebin.id = 'baidu_pastebin';
  13410. // Safari 要求div必须有内容,才能粘贴内容进来
  13411. browser.webkit && pastebin.appendChild(doc.createTextNode(domUtils.fillChar + domUtils.fillChar));
  13412. doc.body.appendChild(pastebin);
  13413. //trace:717 隐藏的span不能得到top
  13414. //bk.start.innerHTML = '&nbsp;';
  13415. bk.start.style.display = '';
  13416. pastebin.style.cssText = "position:absolute;width:1px;height:1px;overflow:hidden;left:-1000px;white-space:nowrap;top:" +
  13417. //要在现在光标平行的位置加入,否则会出现跳动的问题
  13418. domUtils.getXY(bk.start).y + 'px';
  13419. range.selectNodeContents(pastebin).select(true);
  13420. setTimeout(function () {
  13421. if (browser.webkit) {
  13422. for (var i = 0, pastebins = doc.querySelectorAll('#baidu_pastebin'), pi; pi = pastebins[i++];) {
  13423. if (domUtils.isEmptyNode(pi)) {
  13424. domUtils.remove(pi);
  13425. } else {
  13426. pastebin = pi;
  13427. break;
  13428. }
  13429. }
  13430. }
  13431. try {
  13432. pastebin.parentNode.removeChild(pastebin);
  13433. } catch (e) {
  13434. }
  13435. range.moveToBookmark(bk).select(true);
  13436. callback(pastebin);
  13437. }, 0);
  13438. }
  13439. var me = this;
  13440. me.setOpt({
  13441. retainOnlyLabelPasted : false
  13442. });
  13443. var txtContent, htmlContent, address;
  13444. function getPureHtml(html){
  13445. return html.replace(/<(\/?)([\w\-]+)([^>]*)>/gi, function (a, b, tagName, attrs) {
  13446. tagName = tagName.toLowerCase();
  13447. if ({img: 1}[tagName]) {
  13448. return a;
  13449. }
  13450. attrs = attrs.replace(/([\w\-]*?)\s*=\s*(("([^"]*)")|('([^']*)')|([^\s>]+))/gi, function (str, atr, val) {
  13451. if ({
  13452. 'src': 1,
  13453. 'href': 1,
  13454. 'name': 1
  13455. }[atr.toLowerCase()]) {
  13456. return atr + '=' + val + ' '
  13457. }
  13458. return ''
  13459. });
  13460. if ({
  13461. 'span': 1,
  13462. 'div': 1
  13463. }[tagName]) {
  13464. return ''
  13465. } else {
  13466. return '<' + b + tagName + ' ' + utils.trim(attrs) + '>'
  13467. }
  13468. });
  13469. }
  13470. function filter(div) {
  13471. var html;
  13472. if (div.firstChild) {
  13473. //去掉cut中添加的边界值
  13474. var nodes = domUtils.getElementsByTagName(div, 'span');
  13475. for (var i = 0, ni; ni = nodes[i++];) {
  13476. if (ni.id == '_baidu_cut_start' || ni.id == '_baidu_cut_end') {
  13477. domUtils.remove(ni);
  13478. }
  13479. }
  13480. if (browser.webkit) {
  13481. var brs = div.querySelectorAll('div br');
  13482. for (var i = 0, bi; bi = brs[i++];) {
  13483. var pN = bi.parentNode;
  13484. if (pN.tagName == 'DIV' && pN.childNodes.length == 1) {
  13485. pN.innerHTML = '<p><br/></p>';
  13486. domUtils.remove(pN);
  13487. }
  13488. }
  13489. var divs = div.querySelectorAll('#baidu_pastebin');
  13490. for (var i = 0, di; di = divs[i++];) {
  13491. var tmpP = me.document.createElement('p');
  13492. di.parentNode.insertBefore(tmpP, di);
  13493. while (di.firstChild) {
  13494. tmpP.appendChild(di.firstChild);
  13495. }
  13496. domUtils.remove(di);
  13497. }
  13498. var metas = div.querySelectorAll('meta');
  13499. for (var i = 0, ci; ci = metas[i++];) {
  13500. domUtils.remove(ci);
  13501. }
  13502. var brs = div.querySelectorAll('br');
  13503. for (i = 0; ci = brs[i++];) {
  13504. if (/^apple-/i.test(ci.className)) {
  13505. domUtils.remove(ci);
  13506. }
  13507. }
  13508. }
  13509. if (browser.gecko) {
  13510. var dirtyNodes = div.querySelectorAll('[_moz_dirty]');
  13511. for (i = 0; ci = dirtyNodes[i++];) {
  13512. ci.removeAttribute('_moz_dirty');
  13513. }
  13514. }
  13515. if (!browser.ie) {
  13516. var spans = div.querySelectorAll('span.Apple-style-span');
  13517. for (var i = 0, ci; ci = spans[i++];) {
  13518. domUtils.remove(ci, true);
  13519. }
  13520. }
  13521. //ie下使用innerHTML会产生多余的\r\n字符,也会产生&nbsp;这里过滤掉
  13522. html = div.innerHTML;//.replace(/>(?:(\s|&nbsp;)*?)</g,'><');
  13523. //过滤word粘贴过来的冗余属性
  13524. html = UE.filterWord(html);
  13525. //取消了忽略空白的第二个参数,粘贴过来的有些是有空白的,会被套上相关的标签
  13526. var root = UE.htmlparser(html);
  13527. //如果给了过滤规则就先进行过滤
  13528. if (me.options.filterRules) {
  13529. UE.filterNode(root, me.options.filterRules);
  13530. }
  13531. //执行默认的处理
  13532. me.filterInputRule(root);
  13533. //针对chrome的处理
  13534. if (browser.webkit) {
  13535. var br = root.lastChild();
  13536. if (br && br.type == 'element' && br.tagName == 'br') {
  13537. root.removeChild(br)
  13538. }
  13539. utils.each(me.body.querySelectorAll('div'), function (node) {
  13540. if (domUtils.isEmptyBlock(node)) {
  13541. domUtils.remove(node,true)
  13542. }
  13543. })
  13544. }
  13545. html = {'html': root.toHtml()};
  13546. me.fireEvent('beforepaste', html, root);
  13547. //抢了默认的粘贴,那后边的内容就不执行了,比如表格粘贴
  13548. if(!html.html){
  13549. return;
  13550. }
  13551. root = UE.htmlparser(html.html,true);
  13552. //如果开启了纯文本模式
  13553. if (me.queryCommandState('pasteplain') === 1) {
  13554. me.execCommand('insertHtml', UE.filterNode(root, me.options.filterTxtRules).toHtml(), true);
  13555. } else {
  13556. //文本模式
  13557. UE.filterNode(root, me.options.filterTxtRules);
  13558. txtContent = root.toHtml();
  13559. //完全模式
  13560. htmlContent = html.html;
  13561. address = me.selection.getRange().createAddress(true);
  13562. me.execCommand('insertHtml', me.getOpt('retainOnlyLabelPasted') === true ? getPureHtml(htmlContent) : htmlContent, true);
  13563. }
  13564. me.fireEvent("afterpaste", html);
  13565. }
  13566. }
  13567. me.addListener('pasteTransfer', function (cmd, plainType) {
  13568. if (address && txtContent && htmlContent && txtContent != htmlContent) {
  13569. var range = me.selection.getRange();
  13570. range.moveToAddress(address, true);
  13571. if (!range.collapsed) {
  13572. while (!domUtils.isBody(range.startContainer)
  13573. ) {
  13574. var start = range.startContainer;
  13575. if(start.nodeType == 1){
  13576. start = start.childNodes[range.startOffset];
  13577. if(!start){
  13578. range.setStartBefore(range.startContainer);
  13579. continue;
  13580. }
  13581. var pre = start.previousSibling;
  13582. if(pre && pre.nodeType == 3 && new RegExp('^[\n\r\t '+domUtils.fillChar+']*$').test(pre.nodeValue)){
  13583. range.setStartBefore(pre)
  13584. }
  13585. }
  13586. if(range.startOffset == 0){
  13587. range.setStartBefore(range.startContainer);
  13588. }else{
  13589. break;
  13590. }
  13591. }
  13592. while (!domUtils.isBody(range.endContainer)
  13593. ) {
  13594. var end = range.endContainer;
  13595. if(end.nodeType == 1){
  13596. end = end.childNodes[range.endOffset];
  13597. if(!end){
  13598. range.setEndAfter(range.endContainer);
  13599. continue;
  13600. }
  13601. var next = end.nextSibling;
  13602. if(next && next.nodeType == 3 && new RegExp('^[\n\r\t'+domUtils.fillChar+']*$').test(next.nodeValue)){
  13603. range.setEndAfter(next)
  13604. }
  13605. }
  13606. if(range.endOffset == range.endContainer[range.endContainer.nodeType == 3 ? 'nodeValue' : 'childNodes'].length){
  13607. range.setEndAfter(range.endContainer);
  13608. }else{
  13609. break;
  13610. }
  13611. }
  13612. }
  13613. range.deleteContents();
  13614. range.select(true);
  13615. me.__hasEnterExecCommand = true;
  13616. var html = htmlContent;
  13617. if (plainType === 2 ) {
  13618. html = getPureHtml(html);
  13619. } else if (plainType) {
  13620. html = txtContent;
  13621. }
  13622. me.execCommand('inserthtml', html, true);
  13623. me.__hasEnterExecCommand = false;
  13624. var rng = me.selection.getRange();
  13625. while (!domUtils.isBody(rng.startContainer) && !rng.startOffset &&
  13626. rng.startContainer[rng.startContainer.nodeType == 3 ? 'nodeValue' : 'childNodes'].length
  13627. ) {
  13628. rng.setStartBefore(rng.startContainer);
  13629. }
  13630. var tmpAddress = rng.createAddress(true);
  13631. address.endAddress = tmpAddress.startAddress;
  13632. }
  13633. });
  13634. me.addListener('ready', function () {
  13635. domUtils.on(me.body, 'cut', function () {
  13636. var range = me.selection.getRange();
  13637. if (!range.collapsed && me.undoManger) {
  13638. me.undoManger.save();
  13639. }
  13640. });
  13641. //ie下beforepaste在点击右键时也会触发,所以用监控键盘才处理
  13642. domUtils.on(me.body, browser.ie || browser.opera ? 'keydown' : 'paste', function (e) {
  13643. if ((browser.ie || browser.opera) && ((!e.ctrlKey && !e.metaKey) || e.keyCode != '86')) {
  13644. return;
  13645. }
  13646. getClipboardData.call(me, function (div) {
  13647. filter(div);
  13648. });
  13649. });
  13650. });
  13651. me.commands['paste'] = {
  13652. execCommand: function (cmd) {
  13653. if (browser.ie) {
  13654. getClipboardData.call(me, function (div) {
  13655. filter(div);
  13656. });
  13657. me.document.execCommand('paste');
  13658. } else {
  13659. alert(me.getLang('pastemsg'));
  13660. }
  13661. }
  13662. }
  13663. };
  13664. // plugins/puretxtpaste.js
  13665. /**
  13666. * 纯文本粘贴插件
  13667. * @file
  13668. * @since 1.2.6.1
  13669. */
  13670. UE.plugins['pasteplain'] = function(){
  13671. var me = this;
  13672. me.setOpt({
  13673. 'pasteplain':false,
  13674. 'filterTxtRules' : function(){
  13675. function transP(node){
  13676. node.tagName = 'p';
  13677. node.setStyle();
  13678. }
  13679. function removeNode(node){
  13680. node.parentNode.removeChild(node,true)
  13681. }
  13682. return {
  13683. //直接删除及其字节点内容
  13684. '-' : 'script style object iframe embed input select',
  13685. 'p': {$:{}},
  13686. 'br':{$:{}},
  13687. div: function (node) {
  13688. var tmpNode, p = UE.uNode.createElement('p');
  13689. while (tmpNode = node.firstChild()) {
  13690. if (tmpNode.type == 'text' || !UE.dom.dtd.$block[tmpNode.tagName]) {
  13691. p.appendChild(tmpNode);
  13692. } else {
  13693. if (p.firstChild()) {
  13694. node.parentNode.insertBefore(p, node);
  13695. p = UE.uNode.createElement('p');
  13696. } else {
  13697. node.parentNode.insertBefore(tmpNode, node);
  13698. }
  13699. }
  13700. }
  13701. if (p.firstChild()) {
  13702. node.parentNode.insertBefore(p, node);
  13703. }
  13704. node.parentNode.removeChild(node);
  13705. },
  13706. ol: removeNode,
  13707. ul: removeNode,
  13708. dl:removeNode,
  13709. dt:removeNode,
  13710. dd:removeNode,
  13711. 'li':removeNode,
  13712. 'caption':transP,
  13713. 'th':transP,
  13714. 'tr':transP,
  13715. 'h1':transP,'h2':transP,'h3':transP,'h4':transP,'h5':transP,'h6':transP,
  13716. 'td':function(node){
  13717. //没有内容的td直接删掉
  13718. var txt = !!node.innerText();
  13719. if(txt){
  13720. node.parentNode.insertAfter(UE.uNode.createText(' &nbsp; &nbsp;'),node);
  13721. }
  13722. node.parentNode.removeChild(node,node.innerText())
  13723. }
  13724. }
  13725. }()
  13726. });
  13727. //暂时这里支持一下老版本的属性
  13728. var pasteplain = me.options.pasteplain;
  13729. /**
  13730. * 启用或取消纯文本粘贴模式
  13731. * @command pasteplain
  13732. * @method execCommand
  13733. * @param { String } cmd 命令字符串
  13734. * @example
  13735. * ```javascript
  13736. * editor.queryCommandState( 'pasteplain' );
  13737. * ```
  13738. */
  13739. /**
  13740. * 查询当前是否处于纯文本粘贴模式
  13741. * @command pasteplain
  13742. * @method queryCommandState
  13743. * @param { String } cmd 命令字符串
  13744. * @return { int } 如果处于纯文本模式,返回1,否则,返回0
  13745. * @example
  13746. * ```javascript
  13747. * editor.queryCommandState( 'pasteplain' );
  13748. * ```
  13749. */
  13750. me.commands['pasteplain'] = {
  13751. queryCommandState: function (){
  13752. return pasteplain ? 1 : 0;
  13753. },
  13754. execCommand: function (){
  13755. pasteplain = !pasteplain|0;
  13756. },
  13757. notNeedUndo : 1
  13758. };
  13759. };
  13760. // plugins/list.js
  13761. /**
  13762. * 有序列表,无序列表插件
  13763. * @file
  13764. * @since 1.2.6.1
  13765. */
  13766. UE.plugins['list'] = function () {
  13767. var me = this,
  13768. notExchange = {
  13769. 'TD':1,
  13770. 'PRE':1,
  13771. 'BLOCKQUOTE':1
  13772. };
  13773. var customStyle = {
  13774. 'cn' : 'cn-1-',
  13775. 'cn1' : 'cn-2-',
  13776. 'cn2' : 'cn-3-',
  13777. 'num': 'num-1-',
  13778. 'num1' : 'num-2-',
  13779. 'num2' : 'num-3-',
  13780. 'dash' : 'dash',
  13781. 'dot':'dot'
  13782. };
  13783. me.setOpt( {
  13784. 'autoTransWordToList':false,
  13785. 'insertorderedlist':{
  13786. 'num':'',
  13787. 'num1':'',
  13788. 'num2':'',
  13789. 'cn':'',
  13790. 'cn1':'',
  13791. 'cn2':'',
  13792. 'decimal':'',
  13793. 'lower-alpha':'',
  13794. 'lower-roman':'',
  13795. 'upper-alpha':'',
  13796. 'upper-roman':''
  13797. },
  13798. 'insertunorderedlist':{
  13799. 'circle':'',
  13800. 'disc':'',
  13801. 'square':'',
  13802. 'dash' : '',
  13803. 'dot':''
  13804. },
  13805. listDefaultPaddingLeft : '30',
  13806. listiconpath : 'http://bs.baidu.com/listicon/',
  13807. maxListLevel : -1,//-1不限制
  13808. disablePInList:false
  13809. } );
  13810. function listToArray(list){
  13811. var arr = [];
  13812. for(var p in list){
  13813. arr.push(p)
  13814. }
  13815. return arr;
  13816. }
  13817. var listStyle = {
  13818. 'OL':listToArray(me.options.insertorderedlist),
  13819. 'UL':listToArray(me.options.insertunorderedlist)
  13820. };
  13821. var liiconpath = me.options.listiconpath;
  13822. //根据用户配置,调整customStyle
  13823. for(var s in customStyle){
  13824. if(!me.options.insertorderedlist.hasOwnProperty(s) && !me.options.insertunorderedlist.hasOwnProperty(s)){
  13825. delete customStyle[s];
  13826. }
  13827. }
  13828. me.ready(function () {
  13829. var customCss = [];
  13830. for(var p in customStyle){
  13831. if(p == 'dash' || p == 'dot'){
  13832. customCss.push('li.list-' + customStyle[p] + '{background-image:url(' + liiconpath +customStyle[p]+'.gif)}');
  13833. customCss.push('ul.custom_'+p+'{list-style:none;}ul.custom_'+p+' li{background-position:0 3px;background-repeat:no-repeat}');
  13834. }else{
  13835. for(var i= 0;i<99;i++){
  13836. customCss.push('li.list-' + customStyle[p] + i + '{background-image:url(' + liiconpath + 'list-'+customStyle[p] + i + '.gif)}')
  13837. }
  13838. customCss.push('ol.custom_'+p+'{list-style:none;}ol.custom_'+p+' li{background-position:0 3px;background-repeat:no-repeat}');
  13839. }
  13840. switch(p){
  13841. case 'cn':
  13842. customCss.push('li.list-'+p+'-paddingleft-1{padding-left:25px}');
  13843. customCss.push('li.list-'+p+'-paddingleft-2{padding-left:40px}');
  13844. customCss.push('li.list-'+p+'-paddingleft-3{padding-left:55px}');
  13845. break;
  13846. case 'cn1':
  13847. customCss.push('li.list-'+p+'-paddingleft-1{padding-left:30px}');
  13848. customCss.push('li.list-'+p+'-paddingleft-2{padding-left:40px}');
  13849. customCss.push('li.list-'+p+'-paddingleft-3{padding-left:55px}');
  13850. break;
  13851. case 'cn2':
  13852. customCss.push('li.list-'+p+'-paddingleft-1{padding-left:40px}');
  13853. customCss.push('li.list-'+p+'-paddingleft-2{padding-left:55px}');
  13854. customCss.push('li.list-'+p+'-paddingleft-3{padding-left:68px}');
  13855. break;
  13856. case 'num':
  13857. case 'num1':
  13858. customCss.push('li.list-'+p+'-paddingleft-1{padding-left:25px}');
  13859. break;
  13860. case 'num2':
  13861. customCss.push('li.list-'+p+'-paddingleft-1{padding-left:35px}');
  13862. customCss.push('li.list-'+p+'-paddingleft-2{padding-left:40px}');
  13863. break;
  13864. case 'dash':
  13865. customCss.push('li.list-'+p+'-paddingleft{padding-left:35px}');
  13866. break;
  13867. case 'dot':
  13868. customCss.push('li.list-'+p+'-paddingleft{padding-left:20px}');
  13869. }
  13870. }
  13871. customCss.push('.list-paddingleft-1{padding-left:0}');
  13872. customCss.push('.list-paddingleft-2{padding-left:'+me.options.listDefaultPaddingLeft+'px}');
  13873. customCss.push('.list-paddingleft-3{padding-left:'+me.options.listDefaultPaddingLeft*2+'px}');
  13874. //如果不给宽度会在自定应样式里出现滚动条
  13875. utils.cssRule('list', 'ol,ul{margin:0;pading:0;'+(browser.ie ? '' : 'width:95%')+'}li{clear:both;}'+customCss.join('\n'), me.document);
  13876. });
  13877. //单独处理剪切的问题
  13878. me.ready(function(){
  13879. domUtils.on(me.body,'cut',function(){
  13880. setTimeout(function(){
  13881. var rng = me.selection.getRange(),li;
  13882. //trace:3416
  13883. if(!rng.collapsed){
  13884. if(li = domUtils.findParentByTagName(rng.startContainer,'li',true)){
  13885. if(!li.nextSibling && domUtils.isEmptyBlock(li)){
  13886. var pn = li.parentNode,node;
  13887. if(node = pn.previousSibling){
  13888. domUtils.remove(pn);
  13889. rng.setStartAtLast(node).collapse(true);
  13890. rng.select(true);
  13891. }else if(node = pn.nextSibling){
  13892. domUtils.remove(pn);
  13893. rng.setStartAtFirst(node).collapse(true);
  13894. rng.select(true);
  13895. }else{
  13896. var tmpNode = me.document.createElement('p');
  13897. domUtils.fillNode(me.document,tmpNode);
  13898. pn.parentNode.insertBefore(tmpNode,pn);
  13899. domUtils.remove(pn);
  13900. rng.setStart(tmpNode,0).collapse(true);
  13901. rng.select(true);
  13902. }
  13903. }
  13904. }
  13905. }
  13906. })
  13907. })
  13908. });
  13909. function getStyle(node){
  13910. var cls = node.className;
  13911. if(domUtils.hasClass(node,/custom_/)){
  13912. return cls.match(/custom_(\w+)/)[1]
  13913. }
  13914. return domUtils.getStyle(node, 'list-style-type')
  13915. }
  13916. me.addListener('beforepaste',function(type,html){
  13917. var me = this,
  13918. rng = me.selection.getRange(),li;
  13919. var root = UE.htmlparser(html.html,true);
  13920. if(li = domUtils.findParentByTagName(rng.startContainer,'li',true)){
  13921. var list = li.parentNode,tagName = list.tagName == 'OL' ? 'ul':'ol';
  13922. utils.each(root.getNodesByTagName(tagName),function(n){
  13923. n.tagName = list.tagName;
  13924. n.setAttr();
  13925. if(n.parentNode === root){
  13926. type = getStyle(list) || (list.tagName == 'OL' ? 'decimal' : 'disc')
  13927. }else{
  13928. var className = n.parentNode.getAttr('class');
  13929. if(className && /custom_/.test(className)){
  13930. type = className.match(/custom_(\w+)/)[1]
  13931. }else{
  13932. type = n.parentNode.getStyle('list-style-type');
  13933. }
  13934. if(!type){
  13935. type = list.tagName == 'OL' ? 'decimal' : 'disc';
  13936. }
  13937. }
  13938. var index = utils.indexOf(listStyle[list.tagName], type);
  13939. if(n.parentNode !== root)
  13940. index = index + 1 == listStyle[list.tagName].length ? 0 : index + 1;
  13941. var currentStyle = listStyle[list.tagName][index];
  13942. if(customStyle[currentStyle]){
  13943. n.setAttr('class', 'custom_' + currentStyle)
  13944. }else{
  13945. n.setStyle('list-style-type',currentStyle)
  13946. }
  13947. })
  13948. }
  13949. html.html = root.toHtml();
  13950. });
  13951. //导出时,去掉p标签
  13952. me.getOpt('disablePInList') === true && me.addOutputRule(function(root){
  13953. utils.each(root.getNodesByTagName('li'),function(li){
  13954. var newChildrens = [],index=0;
  13955. utils.each(li.children,function(n){
  13956. if(n.tagName == 'p'){
  13957. var tmpNode;
  13958. while(tmpNode = n.children.pop()) {
  13959. newChildrens.splice(index,0,tmpNode);
  13960. tmpNode.parentNode = li;
  13961. lastNode = tmpNode;
  13962. }
  13963. tmpNode = newChildrens[newChildrens.length-1];
  13964. if(!tmpNode || tmpNode.type != 'element' || tmpNode.tagName != 'br'){
  13965. var br = UE.uNode.createElement('br');
  13966. br.parentNode = li;
  13967. newChildrens.push(br);
  13968. }
  13969. index = newChildrens.length;
  13970. }
  13971. });
  13972. if(newChildrens.length){
  13973. li.children = newChildrens;
  13974. }
  13975. });
  13976. });
  13977. //进入编辑器的li要套p标签
  13978. me.addInputRule(function(root){
  13979. utils.each(root.getNodesByTagName('li'),function(li){
  13980. var tmpP = UE.uNode.createElement('p');
  13981. for(var i= 0,ci;ci=li.children[i];){
  13982. if(ci.type == 'text' || dtd.p[ci.tagName]){
  13983. tmpP.appendChild(ci);
  13984. }else{
  13985. if(tmpP.firstChild()){
  13986. li.insertBefore(tmpP,ci);
  13987. tmpP = UE.uNode.createElement('p');
  13988. i = i + 2;
  13989. }else{
  13990. i++;
  13991. }
  13992. }
  13993. }
  13994. if(tmpP.firstChild() && !tmpP.parentNode || !li.firstChild()){
  13995. li.appendChild(tmpP);
  13996. }
  13997. //trace:3357
  13998. //p不能为空
  13999. if (!tmpP.firstChild()) {
  14000. tmpP.innerHTML(browser.ie ? '&nbsp;' : '<br/>')
  14001. }
  14002. //去掉末尾的空白
  14003. var p = li.firstChild();
  14004. var lastChild = p.lastChild();
  14005. if(lastChild && lastChild.type == 'text' && /^\s*$/.test(lastChild.data)){
  14006. p.removeChild(lastChild)
  14007. }
  14008. });
  14009. if(me.options.autoTransWordToList){
  14010. var orderlisttype = {
  14011. 'num1':/^\d+\)/,
  14012. 'decimal':/^\d+\./,
  14013. 'lower-alpha':/^[a-z]+\)/,
  14014. 'upper-alpha':/^[A-Z]+\./,
  14015. 'cn':/^[\u4E00\u4E8C\u4E09\u56DB\u516d\u4e94\u4e03\u516b\u4e5d]+[\u3001]/,
  14016. 'cn2':/^\([\u4E00\u4E8C\u4E09\u56DB\u516d\u4e94\u4e03\u516b\u4e5d]+\)/
  14017. },
  14018. unorderlisttype = {
  14019. 'square':'n'
  14020. };
  14021. function checkListType(content,container){
  14022. var span = container.firstChild();
  14023. if(span && span.type == 'element' && span.tagName == 'span' && /Wingdings|Symbol/.test(span.getStyle('font-family'))){
  14024. for(var p in unorderlisttype){
  14025. if(unorderlisttype[p] == span.data){
  14026. return p
  14027. }
  14028. }
  14029. return 'disc'
  14030. }
  14031. for(var p in orderlisttype){
  14032. if(orderlisttype[p].test(content)){
  14033. return p;
  14034. }
  14035. }
  14036. }
  14037. utils.each(root.getNodesByTagName('p'),function(node){
  14038. if(node.getAttr('class') != 'MsoListParagraph'){
  14039. return
  14040. }
  14041. //word粘贴过来的会带有margin要去掉,但这样也可能会误命中一些央视
  14042. node.setStyle('margin','');
  14043. node.setStyle('margin-left','');
  14044. node.setAttr('class','');
  14045. function appendLi(list,p,type){
  14046. if(list.tagName == 'ol'){
  14047. if(browser.ie){
  14048. var first = p.firstChild();
  14049. if(first.type =='element' && first.tagName == 'span' && orderlisttype[type].test(first.innerText())){
  14050. p.removeChild(first);
  14051. }
  14052. }else{
  14053. p.innerHTML(p.innerHTML().replace(orderlisttype[type],''));
  14054. }
  14055. }else{
  14056. p.removeChild(p.firstChild())
  14057. }
  14058. var li = UE.uNode.createElement('li');
  14059. li.appendChild(p);
  14060. list.appendChild(li);
  14061. }
  14062. var tmp = node,type,cacheNode = node;
  14063. if(node.parentNode.tagName != 'li' && (type = checkListType(node.innerText(),node))){
  14064. var list = UE.uNode.createElement(me.options.insertorderedlist.hasOwnProperty(type) ? 'ol' : 'ul');
  14065. if(customStyle[type]){
  14066. list.setAttr('class','custom_'+type)
  14067. }else{
  14068. list.setStyle('list-style-type',type)
  14069. }
  14070. while(node && node.parentNode.tagName != 'li' && checkListType(node.innerText(),node)){
  14071. tmp = node.nextSibling();
  14072. if(!tmp){
  14073. node.parentNode.insertBefore(list,node)
  14074. }
  14075. appendLi(list,node,type);
  14076. node = tmp;
  14077. }
  14078. if(!list.parentNode && node && node.parentNode){
  14079. node.parentNode.insertBefore(list,node)
  14080. }
  14081. }
  14082. var span = cacheNode.firstChild();
  14083. if(span && span.type == 'element' && span.tagName == 'span' && /^\s*(&nbsp;)+\s*$/.test(span.innerText())){
  14084. span.parentNode.removeChild(span)
  14085. }
  14086. })
  14087. }
  14088. });
  14089. //调整索引标签
  14090. me.addListener('contentchange',function(){
  14091. adjustListStyle(me.document)
  14092. });
  14093. function adjustListStyle(doc,ignore){
  14094. utils.each(domUtils.getElementsByTagName(doc,'ol ul'),function(node){
  14095. if(!domUtils.inDoc(node,doc))
  14096. return;
  14097. var parent = node.parentNode;
  14098. if(parent.tagName == node.tagName){
  14099. var nodeStyleType = getStyle(node) || (node.tagName == 'OL' ? 'decimal' : 'disc'),
  14100. parentStyleType = getStyle(parent) || (parent.tagName == 'OL' ? 'decimal' : 'disc');
  14101. if(nodeStyleType == parentStyleType){
  14102. var styleIndex = utils.indexOf(listStyle[node.tagName], nodeStyleType);
  14103. styleIndex = styleIndex + 1 == listStyle[node.tagName].length ? 0 : styleIndex + 1;
  14104. setListStyle(node,listStyle[node.tagName][styleIndex])
  14105. }
  14106. }
  14107. var index = 0,type = 2;
  14108. if( domUtils.hasClass(node,/custom_/)){
  14109. if(!(/[ou]l/i.test(parent.tagName) && domUtils.hasClass(parent,/custom_/))){
  14110. type = 1;
  14111. }
  14112. }else{
  14113. if(/[ou]l/i.test(parent.tagName) && domUtils.hasClass(parent,/custom_/)){
  14114. type = 3;
  14115. }
  14116. }
  14117. var style = domUtils.getStyle(node, 'list-style-type');
  14118. style && (node.style.cssText = 'list-style-type:' + style);
  14119. node.className = utils.trim(node.className.replace(/list-paddingleft-\w+/,'')) + ' list-paddingleft-' + type;
  14120. utils.each(domUtils.getElementsByTagName(node,'li'),function(li){
  14121. li.style.cssText && (li.style.cssText = '');
  14122. if(!li.firstChild){
  14123. domUtils.remove(li);
  14124. return;
  14125. }
  14126. if(li.parentNode !== node){
  14127. return;
  14128. }
  14129. index++;
  14130. if(domUtils.hasClass(node,/custom_/) ){
  14131. var paddingLeft = 1,currentStyle = getStyle(node);
  14132. if(node.tagName == 'OL'){
  14133. if(currentStyle){
  14134. switch(currentStyle){
  14135. case 'cn' :
  14136. case 'cn1':
  14137. case 'cn2':
  14138. if(index > 10 && (index % 10 == 0 || index > 10 && index < 20)){
  14139. paddingLeft = 2
  14140. }else if(index > 20){
  14141. paddingLeft = 3
  14142. }
  14143. break;
  14144. case 'num2' :
  14145. if(index > 9){
  14146. paddingLeft = 2
  14147. }
  14148. }
  14149. }
  14150. li.className = 'list-'+customStyle[currentStyle]+ index + ' ' + 'list-'+currentStyle+'-paddingleft-' + paddingLeft;
  14151. }else{
  14152. li.className = 'list-'+customStyle[currentStyle] + ' ' + 'list-'+currentStyle+'-paddingleft';
  14153. }
  14154. }else{
  14155. li.className = li.className.replace(/list-[\w\-]+/gi,'');
  14156. }
  14157. var className = li.getAttribute('class');
  14158. if(className !== null && !className.replace(/\s/g,'')){
  14159. domUtils.removeAttributes(li,'class')
  14160. }
  14161. });
  14162. !ignore && adjustList(node,node.tagName.toLowerCase(),getStyle(node)||domUtils.getStyle(node, 'list-style-type'),true);
  14163. })
  14164. }
  14165. function adjustList(list, tag, style,ignoreEmpty) {
  14166. var nextList = list.nextSibling;
  14167. if (nextList && nextList.nodeType == 1 && nextList.tagName.toLowerCase() == tag && (getStyle(nextList) || domUtils.getStyle(nextList, 'list-style-type') || (tag == 'ol' ? 'decimal' : 'disc')) == style) {
  14168. domUtils.moveChild(nextList, list);
  14169. if (nextList.childNodes.length == 0) {
  14170. domUtils.remove(nextList);
  14171. }
  14172. }
  14173. if(nextList && domUtils.isFillChar(nextList)){
  14174. domUtils.remove(nextList);
  14175. }
  14176. var preList = list.previousSibling;
  14177. if (preList && preList.nodeType == 1 && preList.tagName.toLowerCase() == tag && (getStyle(preList) || domUtils.getStyle(preList, 'list-style-type') || (tag == 'ol' ? 'decimal' : 'disc')) == style) {
  14178. domUtils.moveChild(list, preList);
  14179. }
  14180. if(preList && domUtils.isFillChar(preList)){
  14181. domUtils.remove(preList);
  14182. }
  14183. !ignoreEmpty && domUtils.isEmptyBlock(list) && domUtils.remove(list);
  14184. if(getStyle(list)){
  14185. adjustListStyle(list.ownerDocument,true)
  14186. }
  14187. }
  14188. function setListStyle(list,style){
  14189. if(customStyle[style]){
  14190. list.className = 'custom_' + style;
  14191. }
  14192. try{
  14193. domUtils.setStyle(list, 'list-style-type', style);
  14194. }catch(e){}
  14195. }
  14196. function clearEmptySibling(node) {
  14197. var tmpNode = node.previousSibling;
  14198. if (tmpNode && domUtils.isEmptyBlock(tmpNode)) {
  14199. domUtils.remove(tmpNode);
  14200. }
  14201. tmpNode = node.nextSibling;
  14202. if (tmpNode && domUtils.isEmptyBlock(tmpNode)) {
  14203. domUtils.remove(tmpNode);
  14204. }
  14205. }
  14206. me.addListener('keydown', function (type, evt) {
  14207. function preventAndSave() {
  14208. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
  14209. me.fireEvent('contentchange');
  14210. me.undoManger && me.undoManger.save();
  14211. }
  14212. function findList(node,filterFn){
  14213. while(node && !domUtils.isBody(node)){
  14214. if(filterFn(node)){
  14215. return null
  14216. }
  14217. if(node.nodeType == 1 && /[ou]l/i.test(node.tagName)){
  14218. return node;
  14219. }
  14220. node = node.parentNode;
  14221. }
  14222. return null;
  14223. }
  14224. var keyCode = evt.keyCode || evt.which;
  14225. if (keyCode == 13 && !evt.shiftKey) {//回车
  14226. var rng = me.selection.getRange(),
  14227. parent = domUtils.findParent(rng.startContainer,function(node){return domUtils.isBlockElm(node)},true),
  14228. li = domUtils.findParentByTagName(rng.startContainer,'li',true);
  14229. if(parent && parent.tagName != 'PRE' && !li){
  14230. var html = parent.innerHTML.replace(new RegExp(domUtils.fillChar, 'g'),'');
  14231. if(/^\s*1\s*\.[^\d]/.test(html)){
  14232. parent.innerHTML = html.replace(/^\s*1\s*\./,'');
  14233. rng.setStartAtLast(parent).collapse(true).select();
  14234. me.__hasEnterExecCommand = true;
  14235. me.execCommand('insertorderedlist');
  14236. me.__hasEnterExecCommand = false;
  14237. }
  14238. }
  14239. var range = me.selection.getRange(),
  14240. start = findList(range.startContainer,function (node) {
  14241. return node.tagName == 'TABLE';
  14242. }),
  14243. end = range.collapsed ? start : findList(range.endContainer,function (node) {
  14244. return node.tagName == 'TABLE';
  14245. });
  14246. if (start && end && start === end) {
  14247. if (!range.collapsed) {
  14248. start = domUtils.findParentByTagName(range.startContainer, 'li', true);
  14249. end = domUtils.findParentByTagName(range.endContainer, 'li', true);
  14250. if (start && end && start === end) {
  14251. range.deleteContents();
  14252. li = domUtils.findParentByTagName(range.startContainer, 'li', true);
  14253. if (li && domUtils.isEmptyBlock(li)) {
  14254. pre = li.previousSibling;
  14255. next = li.nextSibling;
  14256. p = me.document.createElement('p');
  14257. domUtils.fillNode(me.document, p);
  14258. parentList = li.parentNode;
  14259. if (pre && next) {
  14260. range.setStart(next, 0).collapse(true).select(true);
  14261. domUtils.remove(li);
  14262. } else {
  14263. if (!pre && !next || !pre) {
  14264. parentList.parentNode.insertBefore(p, parentList);
  14265. } else {
  14266. li.parentNode.parentNode.insertBefore(p, parentList.nextSibling);
  14267. }
  14268. domUtils.remove(li);
  14269. if (!parentList.firstChild) {
  14270. domUtils.remove(parentList);
  14271. }
  14272. range.setStart(p, 0).setCursor();
  14273. }
  14274. preventAndSave();
  14275. return;
  14276. }
  14277. } else {
  14278. var tmpRange = range.cloneRange(),
  14279. bk = tmpRange.collapse(false).createBookmark();
  14280. range.deleteContents();
  14281. tmpRange.moveToBookmark(bk);
  14282. var li = domUtils.findParentByTagName(tmpRange.startContainer, 'li', true);
  14283. clearEmptySibling(li);
  14284. tmpRange.select();
  14285. preventAndSave();
  14286. return;
  14287. }
  14288. }
  14289. li = domUtils.findParentByTagName(range.startContainer, 'li', true);
  14290. if (li) {
  14291. if (domUtils.isEmptyBlock(li)) {
  14292. bk = range.createBookmark();
  14293. var parentList = li.parentNode;
  14294. if (li !== parentList.lastChild) {
  14295. domUtils.breakParent(li, parentList);
  14296. clearEmptySibling(li);
  14297. } else {
  14298. parentList.parentNode.insertBefore(li, parentList.nextSibling);
  14299. if (domUtils.isEmptyNode(parentList)) {
  14300. domUtils.remove(parentList);
  14301. }
  14302. }
  14303. //嵌套不处理
  14304. if (!dtd.$list[li.parentNode.tagName]) {
  14305. if (!domUtils.isBlockElm(li.firstChild)) {
  14306. p = me.document.createElement('p');
  14307. li.parentNode.insertBefore(p, li);
  14308. while (li.firstChild) {
  14309. p.appendChild(li.firstChild);
  14310. }
  14311. domUtils.remove(li);
  14312. } else {
  14313. domUtils.remove(li, true);
  14314. }
  14315. }
  14316. range.moveToBookmark(bk).select();
  14317. } else {
  14318. var first = li.firstChild;
  14319. if (!first || !domUtils.isBlockElm(first)) {
  14320. var p = me.document.createElement('p');
  14321. !li.firstChild && domUtils.fillNode(me.document, p);
  14322. while (li.firstChild) {
  14323. p.appendChild(li.firstChild);
  14324. }
  14325. li.appendChild(p);
  14326. first = p;
  14327. }
  14328. var span = me.document.createElement('span');
  14329. range.insertNode(span);
  14330. domUtils.breakParent(span, li);
  14331. var nextLi = span.nextSibling;
  14332. first = nextLi.firstChild;
  14333. if (!first) {
  14334. p = me.document.createElement('p');
  14335. domUtils.fillNode(me.document, p);
  14336. nextLi.appendChild(p);
  14337. first = p;
  14338. }
  14339. if (domUtils.isEmptyNode(first)) {
  14340. first.innerHTML = '';
  14341. domUtils.fillNode(me.document, first);
  14342. }
  14343. range.setStart(first, 0).collapse(true).shrinkBoundary().select();
  14344. domUtils.remove(span);
  14345. var pre = nextLi.previousSibling;
  14346. if (pre && domUtils.isEmptyBlock(pre)) {
  14347. pre.innerHTML = '<p></p>';
  14348. domUtils.fillNode(me.document, pre.firstChild);
  14349. }
  14350. }
  14351. // }
  14352. preventAndSave();
  14353. }
  14354. }
  14355. }
  14356. if (keyCode == 8) {
  14357. //修中ie中li下的问题
  14358. range = me.selection.getRange();
  14359. if (range.collapsed && domUtils.isStartInblock(range)) {
  14360. tmpRange = range.cloneRange().trimBoundary();
  14361. li = domUtils.findParentByTagName(range.startContainer, 'li', true);
  14362. //要在li的最左边,才能处理
  14363. if (li && domUtils.isStartInblock(tmpRange)) {
  14364. start = domUtils.findParentByTagName(range.startContainer, 'p', true);
  14365. if (start && start !== li.firstChild) {
  14366. var parentList = domUtils.findParentByTagName(start,['ol','ul']);
  14367. domUtils.breakParent(start,parentList);
  14368. clearEmptySibling(start);
  14369. me.fireEvent('contentchange');
  14370. range.setStart(start,0).setCursor(false,true);
  14371. me.fireEvent('saveScene');
  14372. domUtils.preventDefault(evt);
  14373. return;
  14374. }
  14375. if (li && (pre = li.previousSibling)) {
  14376. if (keyCode == 46 && li.childNodes.length) {
  14377. return;
  14378. }
  14379. //有可能上边的兄弟节点是个2级菜单,要追加到2级菜单的最后的li
  14380. if (dtd.$list[pre.tagName]) {
  14381. pre = pre.lastChild;
  14382. }
  14383. me.undoManger && me.undoManger.save();
  14384. first = li.firstChild;
  14385. if (domUtils.isBlockElm(first)) {
  14386. if (domUtils.isEmptyNode(first)) {
  14387. // range.setEnd(pre, pre.childNodes.length).shrinkBoundary().collapse().select(true);
  14388. pre.appendChild(first);
  14389. range.setStart(first, 0).setCursor(false, true);
  14390. //first不是唯一的节点
  14391. while (li.firstChild) {
  14392. pre.appendChild(li.firstChild);
  14393. }
  14394. } else {
  14395. span = me.document.createElement('span');
  14396. range.insertNode(span);
  14397. //判断pre是否是空的节点,如果是<p><br/></p>类型的空节点,干掉p标签防止它占位
  14398. if (domUtils.isEmptyBlock(pre)) {
  14399. pre.innerHTML = '';
  14400. }
  14401. domUtils.moveChild(li, pre);
  14402. range.setStartBefore(span).collapse(true).select(true);
  14403. domUtils.remove(span);
  14404. }
  14405. } else {
  14406. if (domUtils.isEmptyNode(li)) {
  14407. var p = me.document.createElement('p');
  14408. pre.appendChild(p);
  14409. range.setStart(p, 0).setCursor();
  14410. // range.setEnd(pre, pre.childNodes.length).shrinkBoundary().collapse().select(true);
  14411. } else {
  14412. range.setEnd(pre, pre.childNodes.length).collapse().select(true);
  14413. while (li.firstChild) {
  14414. pre.appendChild(li.firstChild);
  14415. }
  14416. }
  14417. }
  14418. domUtils.remove(li);
  14419. me.fireEvent('contentchange');
  14420. me.fireEvent('saveScene');
  14421. domUtils.preventDefault(evt);
  14422. return;
  14423. }
  14424. //trace:980
  14425. if (li && !li.previousSibling) {
  14426. var parentList = li.parentNode;
  14427. var bk = range.createBookmark();
  14428. if(domUtils.isTagNode(parentList.parentNode,'ol ul')){
  14429. parentList.parentNode.insertBefore(li,parentList);
  14430. if(domUtils.isEmptyNode(parentList)){
  14431. domUtils.remove(parentList)
  14432. }
  14433. }else{
  14434. while(li.firstChild){
  14435. parentList.parentNode.insertBefore(li.firstChild,parentList);
  14436. }
  14437. domUtils.remove(li);
  14438. if(domUtils.isEmptyNode(parentList)){
  14439. domUtils.remove(parentList)
  14440. }
  14441. }
  14442. range.moveToBookmark(bk).setCursor(false,true);
  14443. me.fireEvent('contentchange');
  14444. me.fireEvent('saveScene');
  14445. domUtils.preventDefault(evt);
  14446. return;
  14447. }
  14448. }
  14449. }
  14450. }
  14451. });
  14452. me.addListener('keyup',function(type, evt){
  14453. var keyCode = evt.keyCode || evt.which;
  14454. if (keyCode == 8) {
  14455. var rng = me.selection.getRange(),list;
  14456. if(list = domUtils.findParentByTagName(rng.startContainer,['ol', 'ul'],true)){
  14457. adjustList(list,list.tagName.toLowerCase(),getStyle(list)||domUtils.getComputedStyle(list,'list-style-type'),true)
  14458. }
  14459. }
  14460. });
  14461. //处理tab键
  14462. me.addListener('tabkeydown',function(){
  14463. var range = me.selection.getRange();
  14464. //控制级数
  14465. function checkLevel(li){
  14466. if(me.options.maxListLevel != -1){
  14467. var level = li.parentNode,levelNum = 0;
  14468. while(/[ou]l/i.test(level.tagName)){
  14469. levelNum++;
  14470. level = level.parentNode;
  14471. }
  14472. if(levelNum >= me.options.maxListLevel){
  14473. return true;
  14474. }
  14475. }
  14476. }
  14477. //只以开始为准
  14478. //todo 后续改进
  14479. var li = domUtils.findParentByTagName(range.startContainer, 'li', true);
  14480. if(li){
  14481. var bk;
  14482. if(range.collapsed){
  14483. if(checkLevel(li))
  14484. return true;
  14485. var parentLi = li.parentNode,
  14486. list = me.document.createElement(parentLi.tagName),
  14487. index = utils.indexOf(listStyle[list.tagName], getStyle(parentLi)||domUtils.getComputedStyle(parentLi, 'list-style-type'));
  14488. index = index + 1 == listStyle[list.tagName].length ? 0 : index + 1;
  14489. var currentStyle = listStyle[list.tagName][index];
  14490. setListStyle(list,currentStyle);
  14491. if(domUtils.isStartInblock(range)){
  14492. me.fireEvent('saveScene');
  14493. bk = range.createBookmark();
  14494. parentLi.insertBefore(list, li);
  14495. list.appendChild(li);
  14496. adjustList(list,list.tagName.toLowerCase(),currentStyle);
  14497. me.fireEvent('contentchange');
  14498. range.moveToBookmark(bk).select(true);
  14499. return true;
  14500. }
  14501. }else{
  14502. me.fireEvent('saveScene');
  14503. bk = range.createBookmark();
  14504. for(var i= 0,closeList,parents = domUtils.findParents(li),ci;ci=parents[i++];){
  14505. if(domUtils.isTagNode(ci,'ol ul')){
  14506. closeList = ci;
  14507. break;
  14508. }
  14509. }
  14510. var current = li;
  14511. if(bk.end){
  14512. while(current && !(domUtils.getPosition(current, bk.end) & domUtils.POSITION_FOLLOWING)){
  14513. if(checkLevel(current)){
  14514. current = domUtils.getNextDomNode(current,false,null,function(node){return node !== closeList});
  14515. continue;
  14516. }
  14517. var parentLi = current.parentNode,
  14518. list = me.document.createElement(parentLi.tagName),
  14519. index = utils.indexOf(listStyle[list.tagName], getStyle(parentLi)||domUtils.getComputedStyle(parentLi, 'list-style-type'));
  14520. var currentIndex = index + 1 == listStyle[list.tagName].length ? 0 : index + 1;
  14521. var currentStyle = listStyle[list.tagName][currentIndex];
  14522. setListStyle(list,currentStyle);
  14523. parentLi.insertBefore(list, current);
  14524. while(current && !(domUtils.getPosition(current, bk.end) & domUtils.POSITION_FOLLOWING)){
  14525. li = current.nextSibling;
  14526. list.appendChild(current);
  14527. if(!li || domUtils.isTagNode(li,'ol ul')){
  14528. if(li){
  14529. while(li = li.firstChild){
  14530. if(li.tagName == 'LI'){
  14531. break;
  14532. }
  14533. }
  14534. }else{
  14535. li = domUtils.getNextDomNode(current,false,null,function(node){return node !== closeList});
  14536. }
  14537. break;
  14538. }
  14539. current = li;
  14540. }
  14541. adjustList(list,list.tagName.toLowerCase(),currentStyle);
  14542. current = li;
  14543. }
  14544. }
  14545. me.fireEvent('contentchange');
  14546. range.moveToBookmark(bk).select();
  14547. return true;
  14548. }
  14549. }
  14550. });
  14551. function getLi(start){
  14552. while(start && !domUtils.isBody(start)){
  14553. if(start.nodeName == 'TABLE'){
  14554. return null;
  14555. }
  14556. if(start.nodeName == 'LI'){
  14557. return start
  14558. }
  14559. start = start.parentNode;
  14560. }
  14561. }
  14562. /**
  14563. * 有序列表,与“insertunorderedlist”命令互斥
  14564. * @command insertorderedlist
  14565. * @method execCommand
  14566. * @param { String } command 命令字符串
  14567. * @param { String } style 插入的有序列表类型,值为:decimal,lower-alpha,lower-roman,upper-alpha,upper-roman,cn,cn1,cn2,num,num1,num2
  14568. * @example
  14569. * ```javascript
  14570. * editor.execCommand( 'insertorderedlist','decimal');
  14571. * ```
  14572. */
  14573. /**
  14574. * 查询当前选区内容是否有序列表
  14575. * @command insertorderedlist
  14576. * @method queryCommandState
  14577. * @param { String } cmd 命令字符串
  14578. * @return { int } 如果当前选区是有序列表返回1,否则返回0
  14579. * @example
  14580. * ```javascript
  14581. * editor.queryCommandState( 'insertorderedlist' );
  14582. * ```
  14583. */
  14584. /**
  14585. * 查询当前选区内容是否有序列表
  14586. * @command insertorderedlist
  14587. * @method queryCommandValue
  14588. * @param { String } cmd 命令字符串
  14589. * @return { String } 返回当前有序列表的类型,值为null或decimal,lower-alpha,lower-roman,upper-alpha,upper-roman,cn,cn1,cn2,num,num1,num2
  14590. * @example
  14591. * ```javascript
  14592. * editor.queryCommandValue( 'insertorderedlist' );
  14593. * ```
  14594. */
  14595. /**
  14596. * 无序列表,与“insertorderedlist”命令互斥
  14597. * @command insertunorderedlist
  14598. * @method execCommand
  14599. * @param { String } command 命令字符串
  14600. * @param { String } style 插入的无序列表类型,值为:circle,disc,square,dash,dot
  14601. * @example
  14602. * ```javascript
  14603. * editor.execCommand( 'insertunorderedlist','circle');
  14604. * ```
  14605. */
  14606. /**
  14607. * 查询当前是否有word文档粘贴进来的图片
  14608. * @command insertunorderedlist
  14609. * @method insertunorderedlist
  14610. * @param { String } command 命令字符串
  14611. * @return { int } 如果当前选区是无序列表返回1,否则返回0
  14612. * @example
  14613. * ```javascript
  14614. * editor.queryCommandState( 'insertunorderedlist' );
  14615. * ```
  14616. */
  14617. /**
  14618. * 查询当前选区内容是否有序列表
  14619. * @command insertunorderedlist
  14620. * @method queryCommandValue
  14621. * @param { String } command 命令字符串
  14622. * @return { String } 返回当前无序列表的类型,值为null或circle,disc,square,dash,dot
  14623. * @example
  14624. * ```javascript
  14625. * editor.queryCommandValue( 'insertunorderedlist' );
  14626. * ```
  14627. */
  14628. me.commands['insertorderedlist'] =
  14629. me.commands['insertunorderedlist'] = {
  14630. execCommand:function (command, style) {
  14631. if (!style) {
  14632. style = command.toLowerCase() == 'insertorderedlist' ? 'decimal' : 'disc';
  14633. }
  14634. var me = this,
  14635. range = this.selection.getRange(),
  14636. filterFn = function (node) {
  14637. return node.nodeType == 1 ? node.tagName.toLowerCase() != 'br' : !domUtils.isWhitespace(node);
  14638. },
  14639. tag = command.toLowerCase() == 'insertorderedlist' ? 'ol' : 'ul',
  14640. frag = me.document.createDocumentFragment();
  14641. //去掉是因为会出现选到末尾,导致adjustmentBoundary缩到ol/ul的位置
  14642. //range.shrinkBoundary();//.adjustmentBoundary();
  14643. range.adjustmentBoundary().shrinkBoundary();
  14644. var bko = range.createBookmark(true),
  14645. start = getLi(me.document.getElementById(bko.start)),
  14646. modifyStart = 0,
  14647. end = getLi(me.document.getElementById(bko.end)),
  14648. modifyEnd = 0,
  14649. startParent, endParent,
  14650. list, tmp;
  14651. if (start || end) {
  14652. start && (startParent = start.parentNode);
  14653. if (!bko.end) {
  14654. end = start;
  14655. }
  14656. end && (endParent = end.parentNode);
  14657. if (startParent === endParent) {
  14658. while (start !== end) {
  14659. tmp = start;
  14660. start = start.nextSibling;
  14661. if (!domUtils.isBlockElm(tmp.firstChild)) {
  14662. var p = me.document.createElement('p');
  14663. while (tmp.firstChild) {
  14664. p.appendChild(tmp.firstChild);
  14665. }
  14666. tmp.appendChild(p);
  14667. }
  14668. frag.appendChild(tmp);
  14669. }
  14670. tmp = me.document.createElement('span');
  14671. startParent.insertBefore(tmp, end);
  14672. if (!domUtils.isBlockElm(end.firstChild)) {
  14673. p = me.document.createElement('p');
  14674. while (end.firstChild) {
  14675. p.appendChild(end.firstChild);
  14676. }
  14677. end.appendChild(p);
  14678. }
  14679. frag.appendChild(end);
  14680. domUtils.breakParent(tmp, startParent);
  14681. if (domUtils.isEmptyNode(tmp.previousSibling)) {
  14682. domUtils.remove(tmp.previousSibling);
  14683. }
  14684. if (domUtils.isEmptyNode(tmp.nextSibling)) {
  14685. domUtils.remove(tmp.nextSibling)
  14686. }
  14687. var nodeStyle = getStyle(startParent) || domUtils.getComputedStyle(startParent, 'list-style-type') || (command.toLowerCase() == 'insertorderedlist' ? 'decimal' : 'disc');
  14688. if (startParent.tagName.toLowerCase() == tag && nodeStyle == style) {
  14689. for (var i = 0, ci, tmpFrag = me.document.createDocumentFragment(); ci = frag.firstChild;) {
  14690. if(domUtils.isTagNode(ci,'ol ul')){
  14691. // 删除时,子列表不处理
  14692. // utils.each(domUtils.getElementsByTagName(ci,'li'),function(li){
  14693. // while(li.firstChild){
  14694. // tmpFrag.appendChild(li.firstChild);
  14695. // }
  14696. //
  14697. // });
  14698. tmpFrag.appendChild(ci);
  14699. }else{
  14700. while (ci.firstChild) {
  14701. tmpFrag.appendChild(ci.firstChild);
  14702. domUtils.remove(ci);
  14703. }
  14704. }
  14705. }
  14706. tmp.parentNode.insertBefore(tmpFrag, tmp);
  14707. } else {
  14708. list = me.document.createElement(tag);
  14709. setListStyle(list,style);
  14710. list.appendChild(frag);
  14711. tmp.parentNode.insertBefore(list, tmp);
  14712. }
  14713. domUtils.remove(tmp);
  14714. list && adjustList(list, tag, style);
  14715. range.moveToBookmark(bko).select();
  14716. return;
  14717. }
  14718. //开始
  14719. if (start) {
  14720. while (start) {
  14721. tmp = start.nextSibling;
  14722. if (domUtils.isTagNode(start, 'ol ul')) {
  14723. frag.appendChild(start);
  14724. } else {
  14725. var tmpfrag = me.document.createDocumentFragment(),
  14726. hasBlock = 0;
  14727. while (start.firstChild) {
  14728. if (domUtils.isBlockElm(start.firstChild)) {
  14729. hasBlock = 1;
  14730. }
  14731. tmpfrag.appendChild(start.firstChild);
  14732. }
  14733. if (!hasBlock) {
  14734. var tmpP = me.document.createElement('p');
  14735. tmpP.appendChild(tmpfrag);
  14736. frag.appendChild(tmpP);
  14737. } else {
  14738. frag.appendChild(tmpfrag);
  14739. }
  14740. domUtils.remove(start);
  14741. }
  14742. start = tmp;
  14743. }
  14744. startParent.parentNode.insertBefore(frag, startParent.nextSibling);
  14745. if (domUtils.isEmptyNode(startParent)) {
  14746. range.setStartBefore(startParent);
  14747. domUtils.remove(startParent);
  14748. } else {
  14749. range.setStartAfter(startParent);
  14750. }
  14751. modifyStart = 1;
  14752. }
  14753. if (end && domUtils.inDoc(endParent, me.document)) {
  14754. //结束
  14755. start = endParent.firstChild;
  14756. while (start && start !== end) {
  14757. tmp = start.nextSibling;
  14758. if (domUtils.isTagNode(start, 'ol ul')) {
  14759. frag.appendChild(start);
  14760. } else {
  14761. tmpfrag = me.document.createDocumentFragment();
  14762. hasBlock = 0;
  14763. while (start.firstChild) {
  14764. if (domUtils.isBlockElm(start.firstChild)) {
  14765. hasBlock = 1;
  14766. }
  14767. tmpfrag.appendChild(start.firstChild);
  14768. }
  14769. if (!hasBlock) {
  14770. tmpP = me.document.createElement('p');
  14771. tmpP.appendChild(tmpfrag);
  14772. frag.appendChild(tmpP);
  14773. } else {
  14774. frag.appendChild(tmpfrag);
  14775. }
  14776. domUtils.remove(start);
  14777. }
  14778. start = tmp;
  14779. }
  14780. var tmpDiv = domUtils.createElement(me.document, 'div', {
  14781. 'tmpDiv':1
  14782. });
  14783. domUtils.moveChild(end, tmpDiv);
  14784. frag.appendChild(tmpDiv);
  14785. domUtils.remove(end);
  14786. endParent.parentNode.insertBefore(frag, endParent);
  14787. range.setEndBefore(endParent);
  14788. if (domUtils.isEmptyNode(endParent)) {
  14789. domUtils.remove(endParent);
  14790. }
  14791. modifyEnd = 1;
  14792. }
  14793. }
  14794. if (!modifyStart) {
  14795. range.setStartBefore(me.document.getElementById(bko.start));
  14796. }
  14797. if (bko.end && !modifyEnd) {
  14798. range.setEndAfter(me.document.getElementById(bko.end));
  14799. }
  14800. range.enlarge(true, function (node) {
  14801. return notExchange[node.tagName];
  14802. });
  14803. frag = me.document.createDocumentFragment();
  14804. var bk = range.createBookmark(),
  14805. current = domUtils.getNextDomNode(bk.start, false, filterFn),
  14806. tmpRange = range.cloneRange(),
  14807. tmpNode,
  14808. block = domUtils.isBlockElm;
  14809. while (current && current !== bk.end && (domUtils.getPosition(current, bk.end) & domUtils.POSITION_PRECEDING)) {
  14810. if (current.nodeType == 3 || dtd.li[current.tagName]) {
  14811. if (current.nodeType == 1 && dtd.$list[current.tagName]) {
  14812. while (current.firstChild) {
  14813. frag.appendChild(current.firstChild);
  14814. }
  14815. tmpNode = domUtils.getNextDomNode(current, false, filterFn);
  14816. domUtils.remove(current);
  14817. current = tmpNode;
  14818. continue;
  14819. }
  14820. tmpNode = current;
  14821. tmpRange.setStartBefore(current);
  14822. while (current && current !== bk.end && (!block(current) || domUtils.isBookmarkNode(current) )) {
  14823. tmpNode = current;
  14824. current = domUtils.getNextDomNode(current, false, null, function (node) {
  14825. return !notExchange[node.tagName];
  14826. });
  14827. }
  14828. if (current && block(current)) {
  14829. tmp = domUtils.getNextDomNode(tmpNode, false, filterFn);
  14830. if (tmp && domUtils.isBookmarkNode(tmp)) {
  14831. current = domUtils.getNextDomNode(tmp, false, filterFn);
  14832. tmpNode = tmp;
  14833. }
  14834. }
  14835. tmpRange.setEndAfter(tmpNode);
  14836. current = domUtils.getNextDomNode(tmpNode, false, filterFn);
  14837. var li = range.document.createElement('li');
  14838. li.appendChild(tmpRange.extractContents());
  14839. if(domUtils.isEmptyNode(li)){
  14840. var tmpNode = range.document.createElement('p');
  14841. while(li.firstChild){
  14842. tmpNode.appendChild(li.firstChild)
  14843. }
  14844. li.appendChild(tmpNode);
  14845. }
  14846. frag.appendChild(li);
  14847. } else {
  14848. current = domUtils.getNextDomNode(current, true, filterFn);
  14849. }
  14850. }
  14851. range.moveToBookmark(bk).collapse(true);
  14852. list = me.document.createElement(tag);
  14853. setListStyle(list,style);
  14854. list.appendChild(frag);
  14855. range.insertNode(list);
  14856. //当前list上下看能否合并
  14857. adjustList(list, tag, style);
  14858. //去掉冗余的tmpDiv
  14859. for (var i = 0, ci, tmpDivs = domUtils.getElementsByTagName(list, 'div'); ci = tmpDivs[i++];) {
  14860. if (ci.getAttribute('tmpDiv')) {
  14861. domUtils.remove(ci, true)
  14862. }
  14863. }
  14864. range.moveToBookmark(bko).select();
  14865. },
  14866. queryCommandState:function (command) {
  14867. var tag = command.toLowerCase() == 'insertorderedlist' ? 'ol' : 'ul';
  14868. var path = this.selection.getStartElementPath();
  14869. for(var i= 0,ci;ci = path[i++];){
  14870. if(ci.nodeName == 'TABLE'){
  14871. return 0
  14872. }
  14873. if(tag == ci.nodeName.toLowerCase()){
  14874. return 1
  14875. };
  14876. }
  14877. return 0;
  14878. },
  14879. queryCommandValue:function (command) {
  14880. var tag = command.toLowerCase() == 'insertorderedlist' ? 'ol' : 'ul';
  14881. var path = this.selection.getStartElementPath(),
  14882. node;
  14883. for(var i= 0,ci;ci = path[i++];){
  14884. if(ci.nodeName == 'TABLE'){
  14885. node = null;
  14886. break;
  14887. }
  14888. if(tag == ci.nodeName.toLowerCase()){
  14889. node = ci;
  14890. break;
  14891. };
  14892. }
  14893. return node ? getStyle(node) || domUtils.getComputedStyle(node, 'list-style-type') : null;
  14894. }
  14895. };
  14896. };
  14897. // plugins/source.js
  14898. /**
  14899. * 源码编辑插件
  14900. * @file
  14901. * @since 1.2.6.1
  14902. */
  14903. (function (){
  14904. var sourceEditors = {
  14905. textarea: function (editor, holder){
  14906. var textarea = holder.ownerDocument.createElement('textarea');
  14907. textarea.style.cssText = 'position:absolute;resize:none;width:100%;height:100%;border:0;padding:0;margin:0;overflow-y:auto;';
  14908. // todo: IE下只有onresize属性可用... 很纠结
  14909. if (browser.ie && browser.version < 8) {
  14910. textarea.style.width = holder.offsetWidth + 'px';
  14911. textarea.style.height = holder.offsetHeight + 'px';
  14912. holder.onresize = function (){
  14913. textarea.style.width = holder.offsetWidth + 'px';
  14914. textarea.style.height = holder.offsetHeight + 'px';
  14915. };
  14916. }
  14917. holder.appendChild(textarea);
  14918. return {
  14919. setContent: function (content){
  14920. textarea.value = content;
  14921. },
  14922. getContent: function (){
  14923. return textarea.value;
  14924. },
  14925. select: function (){
  14926. var range;
  14927. if (browser.ie) {
  14928. range = textarea.createTextRange();
  14929. range.collapse(true);
  14930. range.select();
  14931. } else {
  14932. //todo: chrome下无法设置焦点
  14933. textarea.setSelectionRange(0, 0);
  14934. textarea.focus();
  14935. }
  14936. },
  14937. dispose: function (){
  14938. holder.removeChild(textarea);
  14939. // todo
  14940. holder.onresize = null;
  14941. textarea = null;
  14942. holder = null;
  14943. }
  14944. };
  14945. },
  14946. codemirror: function (editor, holder){
  14947. var codeEditor = window.CodeMirror(holder, {
  14948. mode: "text/html",
  14949. tabMode: "indent",
  14950. lineNumbers: true,
  14951. lineWrapping:true
  14952. });
  14953. var dom = codeEditor.getWrapperElement();
  14954. dom.style.cssText = 'position:absolute;left:0;top:0;width:100%;height:100%;font-family:consolas,"Courier new",monospace;font-size:13px;';
  14955. codeEditor.getScrollerElement().style.cssText = 'position:absolute;left:0;top:0;width:100%;height:100%;';
  14956. codeEditor.refresh();
  14957. return {
  14958. getCodeMirror:function(){
  14959. return codeEditor;
  14960. },
  14961. setContent: function (content){
  14962. codeEditor.setValue(content);
  14963. },
  14964. getContent: function (){
  14965. return codeEditor.getValue();
  14966. },
  14967. select: function (){
  14968. codeEditor.focus();
  14969. },
  14970. dispose: function (){
  14971. holder.removeChild(dom);
  14972. dom = null;
  14973. codeEditor = null;
  14974. }
  14975. };
  14976. }
  14977. };
  14978. UE.plugins['source'] = function (){
  14979. var me = this;
  14980. var opt = this.options;
  14981. var sourceMode = false;
  14982. var sourceEditor;
  14983. var orgSetContent;
  14984. opt.sourceEditor = browser.ie ? 'textarea' : (opt.sourceEditor || 'codemirror');
  14985. me.setOpt({
  14986. sourceEditorFirst:false
  14987. });
  14988. function createSourceEditor(holder){
  14989. return sourceEditors[opt.sourceEditor == 'codemirror' && window.CodeMirror ? 'codemirror' : 'textarea'](me, holder);
  14990. }
  14991. var bakCssText;
  14992. //解决在源码模式下getContent不能得到最新的内容问题
  14993. var oldGetContent,
  14994. bakAddress;
  14995. /**
  14996. * 切换源码模式和编辑模式
  14997. * @command source
  14998. * @method execCommand
  14999. * @param { String } cmd 命令字符串
  15000. * @example
  15001. * ```javascript
  15002. * editor.execCommand( 'source');
  15003. * ```
  15004. */
  15005. /**
  15006. * 查询当前编辑区域的状态是源码模式还是可视化模式
  15007. * @command source
  15008. * @method queryCommandState
  15009. * @param { String } cmd 命令字符串
  15010. * @return { int } 如果当前是源码编辑模式,返回1,否则返回0
  15011. * @example
  15012. * ```javascript
  15013. * editor.queryCommandState( 'source' );
  15014. * ```
  15015. */
  15016. me.commands['source'] = {
  15017. execCommand: function (){
  15018. sourceMode = !sourceMode;
  15019. if (sourceMode) {
  15020. bakAddress = me.selection.getRange().createAddress(false,true);
  15021. me.undoManger && me.undoManger.save(true);
  15022. if(browser.gecko){
  15023. me.body.contentEditable = false;
  15024. }
  15025. bakCssText = me.iframe.style.cssText;
  15026. me.iframe.style.cssText += 'position:absolute;left:-32768px;top:-32768px;';
  15027. me.fireEvent('beforegetcontent');
  15028. var root = UE.htmlparser(me.body.innerHTML);
  15029. me.filterOutputRule(root);
  15030. root.traversal(function (node) {
  15031. if (node.type == 'element') {
  15032. switch (node.tagName) {
  15033. case 'td':
  15034. case 'th':
  15035. case 'caption':
  15036. if(node.children && node.children.length == 1){
  15037. if(node.firstChild().tagName == 'br' ){
  15038. node.removeChild(node.firstChild())
  15039. }
  15040. };
  15041. break;
  15042. case 'pre':
  15043. node.innerText(node.innerText().replace(/&nbsp;/g,' '))
  15044. }
  15045. }
  15046. });
  15047. me.fireEvent('aftergetcontent');
  15048. var content = root.toHtml(true);
  15049. sourceEditor = createSourceEditor(me.iframe.parentNode);
  15050. sourceEditor.setContent(content);
  15051. orgSetContent = me.setContent;
  15052. me.setContent = function(html){
  15053. //这里暂时不触发事件,防止报错
  15054. var root = UE.htmlparser(html);
  15055. me.filterInputRule(root);
  15056. html = root.toHtml();
  15057. sourceEditor.setContent(html);
  15058. };
  15059. setTimeout(function (){
  15060. sourceEditor.select();
  15061. me.addListener('fullscreenchanged', function(){
  15062. try{
  15063. sourceEditor.getCodeMirror().refresh()
  15064. }catch(e){}
  15065. });
  15066. });
  15067. //重置getContent,源码模式下取值也能是最新的数据
  15068. oldGetContent = me.getContent;
  15069. me.getContent = function (){
  15070. return sourceEditor.getContent() || '<p>' + (browser.ie ? '' : '<br/>')+'</p>';
  15071. };
  15072. } else {
  15073. me.iframe.style.cssText = bakCssText;
  15074. var cont = sourceEditor.getContent() || '<p>' + (browser.ie ? '' : '<br/>')+'</p>';
  15075. //处理掉block节点前后的空格,有可能会误命中,暂时不考虑
  15076. cont = cont.replace(new RegExp('[\\r\\t\\n ]*<\/?(\\w+)\\s*(?:[^>]*)>','g'), function(a,b){
  15077. if(b && !dtd.$inlineWithA[b.toLowerCase()]){
  15078. return a.replace(/(^[\n\r\t ]*)|([\n\r\t ]*$)/g,'');
  15079. }
  15080. return a.replace(/(^[\n\r\t]*)|([\n\r\t]*$)/g,'')
  15081. });
  15082. me.setContent = orgSetContent;
  15083. me.setContent(cont);
  15084. sourceEditor.dispose();
  15085. sourceEditor = null;
  15086. //还原getContent方法
  15087. me.getContent = oldGetContent;
  15088. var first = me.body.firstChild;
  15089. //trace:1106 都删除空了,下边会报错,所以补充一个p占位
  15090. if(!first){
  15091. me.body.innerHTML = '<p>'+(browser.ie?'':'<br/>')+'</p>';
  15092. first = me.body.firstChild;
  15093. }
  15094. //要在ifm为显示时ff才能取到selection,否则报错
  15095. //这里不能比较位置了
  15096. me.undoManger && me.undoManger.save(true);
  15097. if(browser.gecko){
  15098. var input = document.createElement('input');
  15099. input.style.cssText = 'position:absolute;left:0;top:-32768px';
  15100. document.body.appendChild(input);
  15101. me.body.contentEditable = false;
  15102. setTimeout(function(){
  15103. domUtils.setViewportOffset(input, { left: -32768, top: 0 });
  15104. input.focus();
  15105. setTimeout(function(){
  15106. me.body.contentEditable = true;
  15107. me.selection.getRange().moveToAddress(bakAddress).select(true);
  15108. domUtils.remove(input);
  15109. });
  15110. });
  15111. }else{
  15112. //ie下有可能报错,比如在代码顶头的情况
  15113. try{
  15114. me.selection.getRange().moveToAddress(bakAddress).select(true);
  15115. }catch(e){}
  15116. }
  15117. }
  15118. this.fireEvent('sourcemodechanged', sourceMode);
  15119. },
  15120. queryCommandState: function (){
  15121. return sourceMode|0;
  15122. },
  15123. notNeedUndo : 1
  15124. };
  15125. var oldQueryCommandState = me.queryCommandState;
  15126. me.queryCommandState = function (cmdName){
  15127. cmdName = cmdName.toLowerCase();
  15128. if (sourceMode) {
  15129. //源码模式下可以开启的命令
  15130. return cmdName in {
  15131. 'source' : 1,
  15132. 'fullscreen' : 1
  15133. } ? 1 : -1
  15134. }
  15135. return oldQueryCommandState.apply(this, arguments);
  15136. };
  15137. if(opt.sourceEditor == "codemirror"){
  15138. me.addListener("ready",function(){
  15139. utils.loadFile(document,{
  15140. src : opt.codeMirrorJsUrl || opt.UEDITOR_HOME_URL + "third-party/codemirror/codemirror.js",
  15141. tag : "script",
  15142. type : "text/javascript",
  15143. defer : "defer"
  15144. },function(){
  15145. if(opt.sourceEditorFirst){
  15146. setTimeout(function(){
  15147. me.execCommand("source");
  15148. },0);
  15149. }
  15150. });
  15151. utils.loadFile(document,{
  15152. tag : "link",
  15153. rel : "stylesheet",
  15154. type : "text/css",
  15155. href : opt.codeMirrorCssUrl || opt.UEDITOR_HOME_URL + "third-party/codemirror/codemirror.css"
  15156. });
  15157. });
  15158. }
  15159. };
  15160. })();
  15161. // plugins/enterkey.js
  15162. ///import core
  15163. ///import plugins/undo.js
  15164. ///commands 设置回车标签p或br
  15165. ///commandsName EnterKey
  15166. ///commandsTitle 设置回车标签p或br
  15167. /**
  15168. * @description 处理回车
  15169. * @author zhanyi
  15170. */
  15171. UE.plugins['enterkey'] = function() {
  15172. var hTag,
  15173. me = this,
  15174. tag = me.options.enterTag;
  15175. me.addListener('keyup', function(type, evt) {
  15176. var keyCode = evt.keyCode || evt.which;
  15177. if (keyCode == 13) {
  15178. var range = me.selection.getRange(),
  15179. start = range.startContainer,
  15180. doSave;
  15181. //修正在h1-h6里边回车后不能嵌套p的问题
  15182. if (!browser.ie) {
  15183. if (/h\d/i.test(hTag)) {
  15184. if (browser.gecko) {
  15185. var h = domUtils.findParentByTagName(start, [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6','blockquote','caption','table'], true);
  15186. if (!h) {
  15187. me.document.execCommand('formatBlock', false, '<p>');
  15188. doSave = 1;
  15189. }
  15190. } else {
  15191. //chrome remove div
  15192. if (start.nodeType == 1) {
  15193. var tmp = me.document.createTextNode(''),div;
  15194. range.insertNode(tmp);
  15195. div = domUtils.findParentByTagName(tmp, 'div', true);
  15196. if (div) {
  15197. var p = me.document.createElement('p');
  15198. while (div.firstChild) {
  15199. p.appendChild(div.firstChild);
  15200. }
  15201. div.parentNode.insertBefore(p, div);
  15202. domUtils.remove(div);
  15203. range.setStartBefore(tmp).setCursor();
  15204. doSave = 1;
  15205. }
  15206. domUtils.remove(tmp);
  15207. }
  15208. }
  15209. if (me.undoManger && doSave) {
  15210. me.undoManger.save();
  15211. }
  15212. }
  15213. //没有站位符,会出现多行的问题
  15214. browser.opera && range.select();
  15215. }else{
  15216. me.fireEvent('saveScene',true,true)
  15217. }
  15218. }
  15219. });
  15220. me.addListener('keydown', function(type, evt) {
  15221. var keyCode = evt.keyCode || evt.which;
  15222. if (keyCode == 13) {//回车
  15223. if(me.fireEvent('beforeenterkeydown')){
  15224. domUtils.preventDefault(evt);
  15225. return;
  15226. }
  15227. me.fireEvent('saveScene',true,true);
  15228. hTag = '';
  15229. var range = me.selection.getRange();
  15230. if (!range.collapsed) {
  15231. //跨td不能删
  15232. var start = range.startContainer,
  15233. end = range.endContainer,
  15234. startTd = domUtils.findParentByTagName(start, 'td', true),
  15235. endTd = domUtils.findParentByTagName(end, 'td', true);
  15236. if (startTd && endTd && startTd !== endTd || !startTd && endTd || startTd && !endTd) {
  15237. evt.preventDefault ? evt.preventDefault() : ( evt.returnValue = false);
  15238. return;
  15239. }
  15240. }
  15241. if (tag == 'p') {
  15242. if (!browser.ie) {
  15243. start = domUtils.findParentByTagName(range.startContainer, ['ol','ul','p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6','blockquote','caption'], true);
  15244. //opera下执行formatblock会在table的场景下有问题,回车在opera原生支持很好,所以暂时在opera去掉调用这个原生的command
  15245. //trace:2431
  15246. if (!start && !browser.opera) {
  15247. me.document.execCommand('formatBlock', false, '<p>');
  15248. if (browser.gecko) {
  15249. range = me.selection.getRange();
  15250. start = domUtils.findParentByTagName(range.startContainer, 'p', true);
  15251. start && domUtils.removeDirtyAttr(start);
  15252. }
  15253. } else {
  15254. hTag = start.tagName;
  15255. start.tagName.toLowerCase() == 'p' && browser.gecko && domUtils.removeDirtyAttr(start);
  15256. }
  15257. }
  15258. } else {
  15259. evt.preventDefault ? evt.preventDefault() : ( evt.returnValue = false);
  15260. if (!range.collapsed) {
  15261. range.deleteContents();
  15262. start = range.startContainer;
  15263. if (start.nodeType == 1 && (start = start.childNodes[range.startOffset])) {
  15264. while (start.nodeType == 1) {
  15265. if (dtd.$empty[start.tagName]) {
  15266. range.setStartBefore(start).setCursor();
  15267. if (me.undoManger) {
  15268. me.undoManger.save();
  15269. }
  15270. return false;
  15271. }
  15272. if (!start.firstChild) {
  15273. var br = range.document.createElement('br');
  15274. start.appendChild(br);
  15275. range.setStart(start, 0).setCursor();
  15276. if (me.undoManger) {
  15277. me.undoManger.save();
  15278. }
  15279. return false;
  15280. }
  15281. start = start.firstChild;
  15282. }
  15283. if (start === range.startContainer.childNodes[range.startOffset]) {
  15284. br = range.document.createElement('br');
  15285. range.insertNode(br).setCursor();
  15286. } else {
  15287. range.setStart(start, 0).setCursor();
  15288. }
  15289. } else {
  15290. br = range.document.createElement('br');
  15291. range.insertNode(br).setStartAfter(br).setCursor();
  15292. }
  15293. } else {
  15294. br = range.document.createElement('br');
  15295. range.insertNode(br);
  15296. var parent = br.parentNode;
  15297. if (parent.lastChild === br) {
  15298. br.parentNode.insertBefore(br.cloneNode(true), br);
  15299. range.setStartBefore(br);
  15300. } else {
  15301. range.setStartAfter(br);
  15302. }
  15303. range.setCursor();
  15304. }
  15305. }
  15306. }
  15307. });
  15308. };
  15309. // plugins/keystrokes.js
  15310. /* 处理特殊键的兼容性问题 */
  15311. UE.plugins['keystrokes'] = function() {
  15312. var me = this;
  15313. var collapsed = true;
  15314. me.addListener('keydown', function(type, evt) {
  15315. var keyCode = evt.keyCode || evt.which,
  15316. rng = me.selection.getRange();
  15317. //处理全选的情况
  15318. if(!rng.collapsed && !(evt.ctrlKey || evt.shiftKey || evt.altKey || evt.metaKey) && (keyCode >= 65 && keyCode <=90
  15319. || keyCode >= 48 && keyCode <= 57 ||
  15320. keyCode >= 96 && keyCode <= 111 || {
  15321. 13:1,
  15322. 8:1,
  15323. 46:1
  15324. }[keyCode])
  15325. ){
  15326. var tmpNode = rng.startContainer;
  15327. if(domUtils.isFillChar(tmpNode)){
  15328. rng.setStartBefore(tmpNode)
  15329. }
  15330. tmpNode = rng.endContainer;
  15331. if(domUtils.isFillChar(tmpNode)){
  15332. rng.setEndAfter(tmpNode)
  15333. }
  15334. rng.txtToElmBoundary();
  15335. //结束边界可能放到了br的前边,要把br包含进来
  15336. // x[xxx]<br/>
  15337. if(rng.endContainer && rng.endContainer.nodeType == 1){
  15338. tmpNode = rng.endContainer.childNodes[rng.endOffset];
  15339. if(tmpNode && domUtils.isBr(tmpNode)){
  15340. rng.setEndAfter(tmpNode);
  15341. }
  15342. }
  15343. if(rng.startOffset == 0){
  15344. tmpNode = rng.startContainer;
  15345. if(domUtils.isBoundaryNode(tmpNode,'firstChild') ){
  15346. tmpNode = rng.endContainer;
  15347. if(rng.endOffset == (tmpNode.nodeType == 3 ? tmpNode.nodeValue.length : tmpNode.childNodes.length) && domUtils.isBoundaryNode(tmpNode,'lastChild')){
  15348. me.fireEvent('saveScene');
  15349. me.body.innerHTML = '<p>'+(browser.ie ? '' : '<br/>')+'</p>';
  15350. rng.setStart(me.body.firstChild,0).setCursor(false,true);
  15351. me._selectionChange();
  15352. return;
  15353. }
  15354. }
  15355. }
  15356. }
  15357. //处理backspace
  15358. if (keyCode == keymap.Backspace) {
  15359. rng = me.selection.getRange();
  15360. collapsed = rng.collapsed;
  15361. if(me.fireEvent('delkeydown',evt)){
  15362. return;
  15363. }
  15364. var start,end;
  15365. //避免按两次删除才能生效的问题
  15366. if(rng.collapsed && rng.inFillChar()){
  15367. start = rng.startContainer;
  15368. if(domUtils.isFillChar(start)){
  15369. rng.setStartBefore(start).shrinkBoundary(true).collapse(true);
  15370. domUtils.remove(start)
  15371. }else{
  15372. start.nodeValue = start.nodeValue.replace(new RegExp('^' + domUtils.fillChar ),'');
  15373. rng.startOffset--;
  15374. rng.collapse(true).select(true)
  15375. }
  15376. }
  15377. //解决选中control元素不能删除的问题
  15378. if (start = rng.getClosedNode()) {
  15379. me.fireEvent('saveScene');
  15380. rng.setStartBefore(start);
  15381. domUtils.remove(start);
  15382. rng.setCursor();
  15383. me.fireEvent('saveScene');
  15384. domUtils.preventDefault(evt);
  15385. return;
  15386. }
  15387. //阻止在table上的删除
  15388. if (!browser.ie) {
  15389. start = domUtils.findParentByTagName(rng.startContainer, 'table', true);
  15390. end = domUtils.findParentByTagName(rng.endContainer, 'table', true);
  15391. if (start && !end || !start && end || start !== end) {
  15392. evt.preventDefault();
  15393. return;
  15394. }
  15395. }
  15396. }
  15397. //处理tab键的逻辑
  15398. if (keyCode == keymap.Tab) {
  15399. //不处理以下标签
  15400. var excludeTagNameForTabKey = {
  15401. 'ol' : 1,
  15402. 'ul' : 1,
  15403. 'table':1
  15404. };
  15405. //处理组件里的tab按下事件
  15406. if(me.fireEvent('tabkeydown',evt)){
  15407. domUtils.preventDefault(evt);
  15408. return;
  15409. }
  15410. var range = me.selection.getRange();
  15411. me.fireEvent('saveScene');
  15412. for (var i = 0,txt = '',tabSize = me.options.tabSize|| 4,tabNode = me.options.tabNode || '&nbsp;'; i < tabSize; i++) {
  15413. txt += tabNode;
  15414. }
  15415. var span = me.document.createElement('span');
  15416. span.innerHTML = txt + domUtils.fillChar;
  15417. if (range.collapsed) {
  15418. range.insertNode(span.cloneNode(true).firstChild).setCursor(true);
  15419. } else {
  15420. var filterFn = function(node) {
  15421. return domUtils.isBlockElm(node) && !excludeTagNameForTabKey[node.tagName.toLowerCase()]
  15422. };
  15423. //普通的情况
  15424. start = domUtils.findParent(range.startContainer, filterFn,true);
  15425. end = domUtils.findParent(range.endContainer, filterFn,true);
  15426. if (start && end && start === end) {
  15427. range.deleteContents();
  15428. range.insertNode(span.cloneNode(true).firstChild).setCursor(true);
  15429. } else {
  15430. var bookmark = range.createBookmark();
  15431. range.enlarge(true);
  15432. var bookmark2 = range.createBookmark(),
  15433. current = domUtils.getNextDomNode(bookmark2.start, false, filterFn);
  15434. while (current && !(domUtils.getPosition(current, bookmark2.end) & domUtils.POSITION_FOLLOWING)) {
  15435. current.insertBefore(span.cloneNode(true).firstChild, current.firstChild);
  15436. current = domUtils.getNextDomNode(current, false, filterFn);
  15437. }
  15438. range.moveToBookmark(bookmark2).moveToBookmark(bookmark).select();
  15439. }
  15440. }
  15441. domUtils.preventDefault(evt)
  15442. }
  15443. //trace:1634
  15444. //ff的del键在容器空的时候,也会删除
  15445. if(browser.gecko && keyCode == 46){
  15446. range = me.selection.getRange();
  15447. if(range.collapsed){
  15448. start = range.startContainer;
  15449. if(domUtils.isEmptyBlock(start)){
  15450. var parent = start.parentNode;
  15451. while(domUtils.getChildCount(parent) == 1 && !domUtils.isBody(parent)){
  15452. start = parent;
  15453. parent = parent.parentNode;
  15454. }
  15455. if(start === parent.lastChild)
  15456. evt.preventDefault();
  15457. return;
  15458. }
  15459. }
  15460. }
  15461. });
  15462. me.addListener('keyup', function(type, evt) {
  15463. var keyCode = evt.keyCode || evt.which,
  15464. rng,me = this;
  15465. if(keyCode == keymap.Backspace){
  15466. if(me.fireEvent('delkeyup')){
  15467. return;
  15468. }
  15469. rng = me.selection.getRange();
  15470. if(rng.collapsed){
  15471. var tmpNode,
  15472. autoClearTagName = ['h1','h2','h3','h4','h5','h6'];
  15473. if(tmpNode = domUtils.findParentByTagName(rng.startContainer,autoClearTagName,true)){
  15474. if(domUtils.isEmptyBlock(tmpNode)){
  15475. var pre = tmpNode.previousSibling;
  15476. if(pre && pre.nodeName != 'TABLE'){
  15477. domUtils.remove(tmpNode);
  15478. rng.setStartAtLast(pre).setCursor(false,true);
  15479. return;
  15480. }else{
  15481. var next = tmpNode.nextSibling;
  15482. if(next && next.nodeName != 'TABLE'){
  15483. domUtils.remove(tmpNode);
  15484. rng.setStartAtFirst(next).setCursor(false,true);
  15485. return;
  15486. }
  15487. }
  15488. }
  15489. }
  15490. //处理当删除到body时,要重新给p标签展位
  15491. if(domUtils.isBody(rng.startContainer)){
  15492. var tmpNode = domUtils.createElement(me.document,'p',{
  15493. 'innerHTML' : browser.ie ? domUtils.fillChar : '<br/>'
  15494. });
  15495. rng.insertNode(tmpNode).setStart(tmpNode,0).setCursor(false,true);
  15496. }
  15497. }
  15498. //chrome下如果删除了inline标签,浏览器会有记忆,在输入文字还是会套上刚才删除的标签,所以这里再选一次就不会了
  15499. if( !collapsed && (rng.startContainer.nodeType == 3 || rng.startContainer.nodeType == 1 && domUtils.isEmptyBlock(rng.startContainer))){
  15500. if(browser.ie){
  15501. var span = rng.document.createElement('span');
  15502. rng.insertNode(span).setStartBefore(span).collapse(true);
  15503. rng.select();
  15504. domUtils.remove(span)
  15505. }else{
  15506. rng.select()
  15507. }
  15508. }
  15509. }
  15510. })
  15511. };
  15512. // plugins/fiximgclick.js
  15513. ///import core
  15514. ///commands 修复chrome下图片不能点击的问题,出现八个角可改变大小
  15515. ///commandsName FixImgClick
  15516. ///commandsTitle 修复chrome下图片不能点击的问题,出现八个角可改变大小
  15517. //修复chrome下图片不能点击的问题,出现八个角可改变大小
  15518. UE.plugins['fiximgclick'] = (function () {
  15519. var elementUpdated = false;
  15520. function Scale() {
  15521. this.editor = null;
  15522. this.resizer = null;
  15523. this.cover = null;
  15524. this.doc = document;
  15525. this.prePos = {x: 0, y: 0};
  15526. this.startPos = {x: 0, y: 0};
  15527. }
  15528. (function () {
  15529. var rect = [
  15530. //[left, top, width, height]
  15531. [0, 0, -1, -1],
  15532. [0, 0, 0, -1],
  15533. [0, 0, 1, -1],
  15534. [0, 0, -1, 0],
  15535. [0, 0, 1, 0],
  15536. [0, 0, -1, 1],
  15537. [0, 0, 0, 1],
  15538. [0, 0, 1, 1]
  15539. ];
  15540. Scale.prototype = {
  15541. init: function (editor) {
  15542. var me = this;
  15543. me.editor = editor;
  15544. me.startPos = this.prePos = {x: 0, y: 0};
  15545. me.dragId = -1;
  15546. var hands = [],
  15547. cover = me.cover = document.createElement('div'),
  15548. resizer = me.resizer = document.createElement('div');
  15549. cover.id = me.editor.ui.id + '_imagescale_cover';
  15550. cover.style.cssText = 'position:absolute;display:none;z-index:' + (me.editor.options.zIndex) + ';filter:alpha(opacity=0); opacity:0;background:#CCC;';
  15551. domUtils.on(cover, 'mousedown click', function () {
  15552. me.hide();
  15553. });
  15554. for (i = 0; i < 8; i++) {
  15555. hands.push('<span class="edui-editor-imagescale-hand' + i + '"></span>');
  15556. }
  15557. resizer.id = me.editor.ui.id + '_imagescale';
  15558. resizer.className = 'edui-editor-imagescale';
  15559. resizer.innerHTML = hands.join('');
  15560. resizer.style.cssText += ';display:none;border:1px solid #3b77ff;z-index:' + (me.editor.options.zIndex) + ';';
  15561. me.editor.ui.getDom().appendChild(cover);
  15562. me.editor.ui.getDom().appendChild(resizer);
  15563. me.initStyle();
  15564. me.initEvents();
  15565. },
  15566. initStyle: function () {
  15567. 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;}' +
  15568. '.edui-editor-imagescale span{position:absolute;width:6px;height:6px;overflow:hidden;font-size:0px;display:block;background-color:#3C9DD0;}'
  15569. + '.edui-editor-imagescale .edui-editor-imagescale-hand0{cursor:nw-resize;top:0;margin-top:-4px;left:0;margin-left:-4px;}'
  15570. + '.edui-editor-imagescale .edui-editor-imagescale-hand1{cursor:n-resize;top:0;margin-top:-4px;left:50%;margin-left:-4px;}'
  15571. + '.edui-editor-imagescale .edui-editor-imagescale-hand2{cursor:ne-resize;top:0;margin-top:-4px;left:100%;margin-left:-3px;}'
  15572. + '.edui-editor-imagescale .edui-editor-imagescale-hand3{cursor:w-resize;top:50%;margin-top:-4px;left:0;margin-left:-4px;}'
  15573. + '.edui-editor-imagescale .edui-editor-imagescale-hand4{cursor:e-resize;top:50%;margin-top:-4px;left:100%;margin-left:-3px;}'
  15574. + '.edui-editor-imagescale .edui-editor-imagescale-hand5{cursor:sw-resize;top:100%;margin-top:-3px;left:0;margin-left:-4px;}'
  15575. + '.edui-editor-imagescale .edui-editor-imagescale-hand6{cursor:s-resize;top:100%;margin-top:-3px;left:50%;margin-left:-4px;}'
  15576. + '.edui-editor-imagescale .edui-editor-imagescale-hand7{cursor:se-resize;top:100%;margin-top:-3px;left:100%;margin-left:-3px;}');
  15577. },
  15578. initEvents: function () {
  15579. var me = this;
  15580. me.startPos.x = me.startPos.y = 0;
  15581. me.isDraging = false;
  15582. },
  15583. _eventHandler: function (e) {
  15584. var me = this;
  15585. switch (e.type) {
  15586. case 'mousedown':
  15587. var hand = e.target || e.srcElement, hand;
  15588. if (hand.className.indexOf('edui-editor-imagescale-hand') != -1 && me.dragId == -1) {
  15589. me.dragId = hand.className.slice(-1);
  15590. me.startPos.x = me.prePos.x = e.clientX;
  15591. me.startPos.y = me.prePos.y = e.clientY;
  15592. domUtils.on(me.doc,'mousemove', me.proxy(me._eventHandler, me));
  15593. }
  15594. break;
  15595. case 'mousemove':
  15596. if (me.dragId != -1) {
  15597. me.updateContainerStyle(me.dragId, {x: e.clientX - me.prePos.x, y: e.clientY - me.prePos.y});
  15598. me.prePos.x = e.clientX;
  15599. me.prePos.y = e.clientY;
  15600. elementUpdated = true;
  15601. me.updateTargetElement();
  15602. }
  15603. break;
  15604. case 'mouseup':
  15605. if (me.dragId != -1) {
  15606. me.updateContainerStyle(me.dragId, {x: e.clientX - me.prePos.x, y: e.clientY - me.prePos.y});
  15607. me.updateTargetElement();
  15608. if (me.target.parentNode) me.attachTo(me.target);
  15609. me.dragId = -1;
  15610. }
  15611. domUtils.un(me.doc,'mousemove', me.proxy(me._eventHandler, me));
  15612. //修复只是点击挪动点,但没有改变大小,不应该触发contentchange
  15613. if(elementUpdated){
  15614. elementUpdated = false;
  15615. me.editor.fireEvent('contentchange');
  15616. }
  15617. break;
  15618. default:
  15619. break;
  15620. }
  15621. },
  15622. updateTargetElement: function () {
  15623. var me = this;
  15624. domUtils.setStyles(me.target, {
  15625. 'width': me.resizer.style.width,
  15626. 'height': me.resizer.style.height
  15627. });
  15628. me.target.width = parseInt(me.resizer.style.width);
  15629. me.target.height = parseInt(me.resizer.style.height);
  15630. me.attachTo(me.target);
  15631. },
  15632. updateContainerStyle: function (dir, offset) {
  15633. var me = this,
  15634. dom = me.resizer, tmp;
  15635. if (rect[dir][0] != 0) {
  15636. tmp = parseInt(dom.style.left) + offset.x;
  15637. dom.style.left = me._validScaledProp('left', tmp) + 'px';
  15638. }
  15639. if (rect[dir][1] != 0) {
  15640. tmp = parseInt(dom.style.top) + offset.y;
  15641. dom.style.top = me._validScaledProp('top', tmp) + 'px';
  15642. }
  15643. if (rect[dir][2] != 0) {
  15644. tmp = dom.clientWidth + rect[dir][2] * offset.x;
  15645. dom.style.width = me._validScaledProp('width', tmp) + 'px';
  15646. }
  15647. if (rect[dir][3] != 0) {
  15648. tmp = dom.clientHeight + rect[dir][3] * offset.y;
  15649. dom.style.height = me._validScaledProp('height', tmp) + 'px';
  15650. }
  15651. },
  15652. _validScaledProp: function (prop, value) {
  15653. var ele = this.resizer,
  15654. wrap = document;
  15655. value = isNaN(value) ? 0 : value;
  15656. switch (prop) {
  15657. case 'left':
  15658. return value < 0 ? 0 : (value + ele.clientWidth) > wrap.clientWidth ? wrap.clientWidth - ele.clientWidth : value;
  15659. case 'top':
  15660. return value < 0 ? 0 : (value + ele.clientHeight) > wrap.clientHeight ? wrap.clientHeight - ele.clientHeight : value;
  15661. case 'width':
  15662. return value <= 0 ? 1 : (value + ele.offsetLeft) > wrap.clientWidth ? wrap.clientWidth - ele.offsetLeft : value;
  15663. case 'height':
  15664. return value <= 0 ? 1 : (value + ele.offsetTop) > wrap.clientHeight ? wrap.clientHeight - ele.offsetTop : value;
  15665. }
  15666. },
  15667. hideCover: function () {
  15668. this.cover.style.display = 'none';
  15669. },
  15670. showCover: function () {
  15671. var me = this,
  15672. editorPos = domUtils.getXY(me.editor.ui.getDom()),
  15673. iframePos = domUtils.getXY(me.editor.iframe);
  15674. domUtils.setStyles(me.cover, {
  15675. 'width': me.editor.iframe.offsetWidth + 'px',
  15676. 'height': me.editor.iframe.offsetHeight + 'px',
  15677. 'top': iframePos.y - editorPos.y + 'px',
  15678. 'left': iframePos.x - editorPos.x + 'px',
  15679. 'position': 'absolute',
  15680. 'display': ''
  15681. })
  15682. },
  15683. show: function (targetObj) {
  15684. var me = this;
  15685. me.resizer.style.display = 'block';
  15686. if(targetObj) me.attachTo(targetObj);
  15687. domUtils.on(this.resizer, 'mousedown', me.proxy(me._eventHandler, me));
  15688. domUtils.on(me.doc, 'mouseup', me.proxy(me._eventHandler, me));
  15689. me.showCover();
  15690. me.editor.fireEvent('afterscaleshow', me);
  15691. me.editor.fireEvent('saveScene');
  15692. },
  15693. hide: function () {
  15694. var me = this;
  15695. me.hideCover();
  15696. me.resizer.style.display = 'none';
  15697. domUtils.un(me.resizer, 'mousedown', me.proxy(me._eventHandler, me));
  15698. domUtils.un(me.doc, 'mouseup', me.proxy(me._eventHandler, me));
  15699. me.editor.fireEvent('afterscalehide', me);
  15700. },
  15701. proxy: function( fn, context ) {
  15702. return function(e) {
  15703. return fn.apply( context || this, arguments);
  15704. };
  15705. },
  15706. attachTo: function (targetObj) {
  15707. var me = this,
  15708. target = me.target = targetObj,
  15709. resizer = this.resizer,
  15710. imgPos = domUtils.getXY(target),
  15711. iframePos = domUtils.getXY(me.editor.iframe),
  15712. editorPos = domUtils.getXY(resizer.parentNode);
  15713. domUtils.setStyles(resizer, {
  15714. 'width': target.width + 'px',
  15715. 'height': target.height + 'px',
  15716. 'left': iframePos.x + imgPos.x - me.editor.document.body.scrollLeft - editorPos.x - parseInt(resizer.style.borderLeftWidth) + 'px',
  15717. 'top': iframePos.y + imgPos.y - me.editor.document.body.scrollTop - editorPos.y - parseInt(resizer.style.borderTopWidth) + 'px'
  15718. });
  15719. }
  15720. }
  15721. })();
  15722. return function () {
  15723. var me = this,
  15724. imageScale;
  15725. me.setOpt('imageScaleEnabled', true);
  15726. if ( !browser.ie && me.options.imageScaleEnabled) {
  15727. me.addListener('click', function (type, e) {
  15728. var range = me.selection.getRange(),
  15729. img = range.getClosedNode();
  15730. if (img && img.tagName == 'IMG' && me.body.contentEditable!="false") {
  15731. if (img.className.indexOf("edui-faked-music") != -1 ||
  15732. img.getAttribute("anchorname") ||
  15733. domUtils.hasClass(img, 'loadingclass') ||
  15734. domUtils.hasClass(img, 'loaderrorclass')) { return }
  15735. if (!imageScale) {
  15736. imageScale = new Scale();
  15737. imageScale.init(me);
  15738. me.ui.getDom().appendChild(imageScale.resizer);
  15739. var _keyDownHandler = function (e) {
  15740. imageScale.hide();
  15741. if(imageScale.target) me.selection.getRange().selectNode(imageScale.target).select();
  15742. }, _mouseDownHandler = function (e) {
  15743. var ele = e.target || e.srcElement;
  15744. if (ele && (ele.className===undefined || ele.className.indexOf('edui-editor-imagescale') == -1)) {
  15745. _keyDownHandler(e);
  15746. }
  15747. }, timer;
  15748. me.addListener('afterscaleshow', function (e) {
  15749. me.addListener('beforekeydown', _keyDownHandler);
  15750. me.addListener('beforemousedown', _mouseDownHandler);
  15751. domUtils.on(document, 'keydown', _keyDownHandler);
  15752. domUtils.on(document,'mousedown', _mouseDownHandler);
  15753. me.selection.getNative().removeAllRanges();
  15754. });
  15755. me.addListener('afterscalehide', function (e) {
  15756. me.removeListener('beforekeydown', _keyDownHandler);
  15757. me.removeListener('beforemousedown', _mouseDownHandler);
  15758. domUtils.un(document, 'keydown', _keyDownHandler);
  15759. domUtils.un(document,'mousedown', _mouseDownHandler);
  15760. var target = imageScale.target;
  15761. if (target.parentNode) {
  15762. me.selection.getRange().selectNode(target).select();
  15763. }
  15764. });
  15765. //TODO 有iframe的情况,mousedown不能往下传。。
  15766. domUtils.on(imageScale.resizer, 'mousedown', function (e) {
  15767. me.selection.getNative().removeAllRanges();
  15768. var ele = e.target || e.srcElement;
  15769. if (ele && ele.className.indexOf('edui-editor-imagescale-hand') == -1) {
  15770. timer = setTimeout(function () {
  15771. imageScale.hide();
  15772. if(imageScale.target) me.selection.getRange().selectNode(ele).select();
  15773. }, 200);
  15774. }
  15775. });
  15776. domUtils.on(imageScale.resizer, 'mouseup', function (e) {
  15777. var ele = e.target || e.srcElement;
  15778. if (ele && ele.className.indexOf('edui-editor-imagescale-hand') == -1) {
  15779. clearTimeout(timer);
  15780. }
  15781. });
  15782. }
  15783. imageScale.show(img);
  15784. } else {
  15785. if (imageScale && imageScale.resizer.style.display != 'none') imageScale.hide();
  15786. }
  15787. });
  15788. }
  15789. if (browser.webkit) {
  15790. me.addListener('click', function (type, e) {
  15791. if (e.target.tagName == 'IMG' && me.body.contentEditable!="false") {
  15792. var range = new dom.Range(me.document);
  15793. range.selectNode(e.target).select();
  15794. }
  15795. });
  15796. }
  15797. }
  15798. })();
  15799. // plugins/autolink.js
  15800. ///import core
  15801. ///commands 为非ie浏览器自动添加a标签
  15802. ///commandsName AutoLink
  15803. ///commandsTitle 自动增加链接
  15804. /**
  15805. * @description 为非ie浏览器自动添加a标签
  15806. * @author zhanyi
  15807. */
  15808. UE.plugin.register('autolink',function(){
  15809. var cont = 0;
  15810. return !browser.ie ? {
  15811. bindEvents:{
  15812. 'reset' : function(){
  15813. cont = 0;
  15814. },
  15815. 'keydown':function(type, evt) {
  15816. var me = this;
  15817. var keyCode = evt.keyCode || evt.which;
  15818. if (keyCode == 32 || keyCode == 13) {
  15819. var sel = me.selection.getNative(),
  15820. range = sel.getRangeAt(0).cloneRange(),
  15821. offset,
  15822. charCode;
  15823. var start = range.startContainer;
  15824. while (start.nodeType == 1 && range.startOffset > 0) {
  15825. start = range.startContainer.childNodes[range.startOffset - 1];
  15826. if (!start){
  15827. break;
  15828. }
  15829. range.setStart(start, start.nodeType == 1 ? start.childNodes.length : start.nodeValue.length);
  15830. range.collapse(true);
  15831. start = range.startContainer;
  15832. }
  15833. do{
  15834. if (range.startOffset == 0) {
  15835. start = range.startContainer.previousSibling;
  15836. while (start && start.nodeType == 1) {
  15837. start = start.lastChild;
  15838. }
  15839. if (!start || domUtils.isFillChar(start)){
  15840. break;
  15841. }
  15842. offset = start.nodeValue.length;
  15843. } else {
  15844. start = range.startContainer;
  15845. offset = range.startOffset;
  15846. }
  15847. range.setStart(start, offset - 1);
  15848. charCode = range.toString().charCodeAt(0);
  15849. } while (charCode != 160 && charCode != 32);
  15850. if (range.toString().replace(new RegExp(domUtils.fillChar, 'g'), '').match(/(?:https?:\/\/|ssh:\/\/|ftp:\/\/|file:\/|www\.)/i)) {
  15851. while(range.toString().length){
  15852. if(/^(?:https?:\/\/|ssh:\/\/|ftp:\/\/|file:\/|www\.)/i.test(range.toString())){
  15853. break;
  15854. }
  15855. try{
  15856. range.setStart(range.startContainer,range.startOffset+1);
  15857. }catch(e){
  15858. //trace:2121
  15859. var start = range.startContainer;
  15860. while(!(next = start.nextSibling)){
  15861. if(domUtils.isBody(start)){
  15862. return;
  15863. }
  15864. start = start.parentNode;
  15865. }
  15866. range.setStart(next,0);
  15867. }
  15868. }
  15869. //range的开始边界已经在a标签里的不再处理
  15870. if(domUtils.findParentByTagName(range.startContainer,'a',true)){
  15871. return;
  15872. }
  15873. var a = me.document.createElement('a'),text = me.document.createTextNode(' '),href;
  15874. me.undoManger && me.undoManger.save();
  15875. a.appendChild(range.extractContents());
  15876. a.href = a.innerHTML = a.innerHTML.replace(/<[^>]+>/g,'');
  15877. href = a.getAttribute("href").replace(new RegExp(domUtils.fillChar,'g'),'');
  15878. href = /^(?:https?:\/\/)/ig.test(href) ? href : "http://"+ href;
  15879. a.setAttribute('_src',utils.html(href));
  15880. a.href = utils.html(href);
  15881. range.insertNode(a);
  15882. a.parentNode.insertBefore(text, a.nextSibling);
  15883. range.setStart(text, 0);
  15884. range.collapse(true);
  15885. sel.removeAllRanges();
  15886. sel.addRange(range);
  15887. me.undoManger && me.undoManger.save();
  15888. }
  15889. }
  15890. }
  15891. }
  15892. }:{}
  15893. },function(){
  15894. var keyCodes = {
  15895. 37:1, 38:1, 39:1, 40:1,
  15896. 13:1,32:1
  15897. };
  15898. function checkIsCludeLink(node){
  15899. if(node.nodeType == 3){
  15900. return null
  15901. }
  15902. if(node.nodeName == 'A'){
  15903. return node;
  15904. }
  15905. var lastChild = node.lastChild;
  15906. while(lastChild){
  15907. if(lastChild.nodeName == 'A'){
  15908. return lastChild;
  15909. }
  15910. if(lastChild.nodeType == 3){
  15911. if(domUtils.isWhitespace(lastChild)){
  15912. lastChild = lastChild.previousSibling;
  15913. continue;
  15914. }
  15915. return null
  15916. }
  15917. lastChild = lastChild.lastChild;
  15918. }
  15919. }
  15920. browser.ie && this.addListener('keyup',function(cmd,evt){
  15921. var me = this,keyCode = evt.keyCode;
  15922. if(keyCodes[keyCode]){
  15923. var rng = me.selection.getRange();
  15924. var start = rng.startContainer;
  15925. if(keyCode == 13){
  15926. while(start && !domUtils.isBody(start) && !domUtils.isBlockElm(start)){
  15927. start = start.parentNode;
  15928. }
  15929. if(start && !domUtils.isBody(start) && start.nodeName == 'P'){
  15930. var pre = start.previousSibling;
  15931. if(pre && pre.nodeType == 1){
  15932. var pre = checkIsCludeLink(pre);
  15933. if(pre && !pre.getAttribute('_href')){
  15934. domUtils.remove(pre,true);
  15935. }
  15936. }
  15937. }
  15938. }else if(keyCode == 32 ){
  15939. if(start.nodeType == 3 && /^\s$/.test(start.nodeValue)){
  15940. start = start.previousSibling;
  15941. if(start && start.nodeName == 'A' && !start.getAttribute('_href')){
  15942. domUtils.remove(start,true);
  15943. }
  15944. }
  15945. }else {
  15946. start = domUtils.findParentByTagName(start,'a',true);
  15947. if(start && !start.getAttribute('_href')){
  15948. var bk = rng.createBookmark();
  15949. domUtils.remove(start,true);
  15950. rng.moveToBookmark(bk).select(true)
  15951. }
  15952. }
  15953. }
  15954. });
  15955. }
  15956. );
  15957. // plugins/autoheight.js
  15958. ///import core
  15959. ///commands 当输入内容超过编辑器高度时,编辑器自动增高
  15960. ///commandsName AutoHeight,autoHeightEnabled
  15961. ///commandsTitle 自动增高
  15962. /**
  15963. * @description 自动伸展
  15964. * @author zhanyi
  15965. */
  15966. UE.plugins['autoheight'] = function () {
  15967. var me = this;
  15968. //提供开关,就算加载也可以关闭
  15969. me.autoHeightEnabled = me.options.autoHeightEnabled !== false;
  15970. if (!me.autoHeightEnabled) {
  15971. return;
  15972. }
  15973. var bakOverflow,
  15974. lastHeight = 0,
  15975. options = me.options,
  15976. currentHeight,
  15977. timer;
  15978. function adjustHeight() {
  15979. var me = this;
  15980. clearTimeout(timer);
  15981. if(isFullscreen)return;
  15982. if (!me.queryCommandState || me.queryCommandState && me.queryCommandState('source') != 1) {
  15983. timer = setTimeout(function(){
  15984. var node = me.body.lastChild;
  15985. while(node && node.nodeType != 1){
  15986. node = node.previousSibling;
  15987. }
  15988. if(node && node.nodeType == 1){
  15989. node.style.clear = 'both';
  15990. currentHeight = Math.max(domUtils.getXY(node).y + node.offsetHeight + 25 ,Math.max(options.minFrameHeight, options.initialFrameHeight)) ;
  15991. if (currentHeight != lastHeight) {
  15992. if (currentHeight !== parseInt(me.iframe.parentNode.style.height)) {
  15993. me.iframe.parentNode.style.height = currentHeight + 'px';
  15994. }
  15995. me.body.style.height = currentHeight + 'px';
  15996. lastHeight = currentHeight;
  15997. }
  15998. domUtils.removeStyle(node,'clear');
  15999. }
  16000. },50)
  16001. }
  16002. }
  16003. var isFullscreen;
  16004. me.addListener('fullscreenchanged',function(cmd,f){
  16005. isFullscreen = f
  16006. });
  16007. me.addListener('destroy', function () {
  16008. me.removeListener('contentchange afterinserthtml keyup mouseup',adjustHeight)
  16009. });
  16010. me.enableAutoHeight = function () {
  16011. var me = this;
  16012. if (!me.autoHeightEnabled) {
  16013. return;
  16014. }
  16015. var doc = me.document;
  16016. me.autoHeightEnabled = true;
  16017. bakOverflow = doc.body.style.overflowY;
  16018. doc.body.style.overflowY = 'hidden';
  16019. me.addListener('contentchange afterinserthtml keyup mouseup',adjustHeight);
  16020. //ff不给事件算得不对
  16021. setTimeout(function () {
  16022. adjustHeight.call(me);
  16023. }, browser.gecko ? 100 : 0);
  16024. me.fireEvent('autoheightchanged', me.autoHeightEnabled);
  16025. };
  16026. me.disableAutoHeight = function () {
  16027. me.body.style.overflowY = bakOverflow || '';
  16028. me.removeListener('contentchange', adjustHeight);
  16029. me.removeListener('keyup', adjustHeight);
  16030. me.removeListener('mouseup', adjustHeight);
  16031. me.autoHeightEnabled = false;
  16032. me.fireEvent('autoheightchanged', me.autoHeightEnabled);
  16033. };
  16034. me.on('setHeight',function(){
  16035. me.disableAutoHeight()
  16036. });
  16037. me.addListener('ready', function () {
  16038. me.enableAutoHeight();
  16039. //trace:1764
  16040. var timer;
  16041. domUtils.on(browser.ie ? me.body : me.document, browser.webkit ? 'dragover' : 'drop', function () {
  16042. clearTimeout(timer);
  16043. timer = setTimeout(function () {
  16044. //trace:3681
  16045. adjustHeight.call(me);
  16046. }, 100);
  16047. });
  16048. //修复内容过多时,回到顶部,顶部内容被工具栏遮挡问题
  16049. var lastScrollY;
  16050. window.onscroll = function(){
  16051. if(lastScrollY === null){
  16052. lastScrollY = this.scrollY
  16053. }else if(this.scrollY == 0 && lastScrollY != 0){
  16054. me.window.scrollTo(0,0);
  16055. lastScrollY = null;
  16056. }
  16057. }
  16058. });
  16059. };
  16060. // plugins/autofloat.js
  16061. ///import core
  16062. ///commands 悬浮工具栏
  16063. ///commandsName AutoFloat,autoFloatEnabled
  16064. ///commandsTitle 悬浮工具栏
  16065. /**
  16066. * modified by chengchao01
  16067. * 注意: 引入此功能后,在IE6下会将body的背景图片覆盖掉!
  16068. */
  16069. UE.plugins['autofloat'] = function() {
  16070. var me = this,
  16071. lang = me.getLang();
  16072. me.setOpt({
  16073. topOffset:0
  16074. });
  16075. var optsAutoFloatEnabled = me.options.autoFloatEnabled !== false,
  16076. topOffset = me.options.topOffset;
  16077. //如果不固定toolbar的位置,则直接退出
  16078. if(!optsAutoFloatEnabled){
  16079. return;
  16080. }
  16081. var uiUtils = UE.ui.uiUtils,
  16082. LteIE6 = browser.ie && browser.version <= 6,
  16083. quirks = browser.quirks;
  16084. function checkHasUI(){
  16085. if(!UE.ui){
  16086. alert(lang.autofloatMsg);
  16087. return 0;
  16088. }
  16089. return 1;
  16090. }
  16091. function fixIE6FixedPos(){
  16092. var docStyle = document.body.style;
  16093. docStyle.backgroundImage = 'url("about:blank")';
  16094. docStyle.backgroundAttachment = 'fixed';
  16095. }
  16096. var bakCssText,
  16097. placeHolder = document.createElement('div'),
  16098. toolbarBox,orgTop,
  16099. getPosition,
  16100. flag =true; //ie7模式下需要偏移
  16101. function setFloating(){
  16102. var toobarBoxPos = domUtils.getXY(toolbarBox),
  16103. origalFloat = domUtils.getComputedStyle(toolbarBox,'position'),
  16104. origalLeft = domUtils.getComputedStyle(toolbarBox,'left');
  16105. toolbarBox.style.width = toolbarBox.offsetWidth + 'px';
  16106. toolbarBox.style.zIndex = me.options.zIndex * 1 + 1;
  16107. toolbarBox.parentNode.insertBefore(placeHolder, toolbarBox);
  16108. if (LteIE6 || (quirks && browser.ie)) {
  16109. if(toolbarBox.style.position != 'absolute'){
  16110. toolbarBox.style.position = 'absolute';
  16111. }
  16112. toolbarBox.style.top = (document.body.scrollTop||document.documentElement.scrollTop) - orgTop + topOffset + 'px';
  16113. } else {
  16114. if (browser.ie7Compat && flag) {
  16115. flag = false;
  16116. toolbarBox.style.left = domUtils.getXY(toolbarBox).x - document.documentElement.getBoundingClientRect().left+2 + 'px';
  16117. }
  16118. if(toolbarBox.style.position != 'fixed'){
  16119. toolbarBox.style.position = 'fixed';
  16120. toolbarBox.style.top = topOffset +"px";
  16121. ((origalFloat == 'absolute' || origalFloat == 'relative') && parseFloat(origalLeft)) && (toolbarBox.style.left = toobarBoxPos.x + 'px');
  16122. }
  16123. }
  16124. }
  16125. function unsetFloating(){
  16126. flag = true;
  16127. if(placeHolder.parentNode){
  16128. placeHolder.parentNode.removeChild(placeHolder);
  16129. }
  16130. toolbarBox.style.cssText = bakCssText;
  16131. }
  16132. function updateFloating(){
  16133. var rect3 = getPosition(me.container);
  16134. var offset=me.options.toolbarTopOffset||0;
  16135. if (rect3.top < 0 && rect3.bottom - toolbarBox.offsetHeight > offset) {
  16136. setFloating();
  16137. }else{
  16138. unsetFloating();
  16139. }
  16140. }
  16141. var defer_updateFloating = utils.defer(function(){
  16142. updateFloating();
  16143. },browser.ie ? 200 : 100,true);
  16144. me.addListener('destroy',function(){
  16145. domUtils.un(window, ['scroll','resize'], updateFloating);
  16146. me.removeListener('keydown', defer_updateFloating);
  16147. });
  16148. me.addListener('ready', function(){
  16149. if(checkHasUI(me)){
  16150. //加载了ui组件,但在new时,没有加载ui,导致编辑器实例上没有ui类,所以这里做判断
  16151. if(!me.ui){
  16152. return;
  16153. }
  16154. getPosition = uiUtils.getClientRect;
  16155. toolbarBox = me.ui.getDom('toolbarbox');
  16156. orgTop = getPosition(toolbarBox).top;
  16157. bakCssText = toolbarBox.style.cssText;
  16158. placeHolder.style.height = toolbarBox.offsetHeight + 'px';
  16159. if(LteIE6){
  16160. fixIE6FixedPos();
  16161. }
  16162. domUtils.on(window, ['scroll','resize'], updateFloating);
  16163. me.addListener('keydown', defer_updateFloating);
  16164. me.addListener('beforefullscreenchange', function (t, enabled){
  16165. if (enabled) {
  16166. unsetFloating();
  16167. }
  16168. });
  16169. me.addListener('fullscreenchanged', function (t, enabled){
  16170. if (!enabled) {
  16171. updateFloating();
  16172. }
  16173. });
  16174. me.addListener('sourcemodechanged', function (t, enabled){
  16175. setTimeout(function (){
  16176. updateFloating();
  16177. },0);
  16178. });
  16179. me.addListener("clearDoc",function(){
  16180. setTimeout(function(){
  16181. updateFloating();
  16182. },0);
  16183. })
  16184. }
  16185. });
  16186. };
  16187. // plugins/video.js
  16188. /**
  16189. * video插件, 为UEditor提供视频插入支持
  16190. * @file
  16191. * @since 1.2.6.1
  16192. */
  16193. UE.plugins['video'] = function (){
  16194. var me =this;
  16195. /**
  16196. * 创建插入视频字符窜
  16197. * @param url 视频地址
  16198. * @param width 视频宽度
  16199. * @param height 视频高度
  16200. * @param align 视频对齐
  16201. * @param toEmbed 是否以flash代替显示
  16202. * @param addParagraph 是否需要添加P 标签
  16203. */
  16204. function creatInsertStr(url,width,height,id,align,classname,type){
  16205. var str;
  16206. switch (type){
  16207. case 'image':
  16208. str = '<img ' + (id ? 'id="' + id+'"' : '') + ' width="'+ width +'" height="' + height + '" _url="'+url+'" class="' + classname.replace(/\bvideo-js\b/, '') + '"' +
  16209. ' 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 + ';': '')+'" />'
  16210. break;
  16211. case 'embed':
  16212. // str = '<embed type="application/x-shockwave1-flash" class="' + classname + '" pluginspage="http://www.macromedia.com/go/getflashplayer"' +
  16213. // ' src="' + utils.html(url) + '" width="' + width + '" height="' + height + '"' + (align ? ' style="float:' + align + '"': '') +
  16214. // ' wmode="transparent" play="true" loop="false" menu="false" allowscriptaccess="never" allowfullscreen="true" >';
  16215. // break;
  16216. case 'video':
  16217. // var ext = url.substr(url.lastIndexOf('.') + 1);
  16218. // if(ext == 'ogv') ext = 'ogg';
  16219. // str = '<video' + (id ? ' id="' + id + '"' : '') + ' class="' + classname + ' video-js" ' + (align ? ' style="float:' + align + '"': '') +
  16220. // ' controls preload="none" width="' + width + '" height="' + height + '" src="' + url + '" data-setup="{}">' +
  16221. // '<source src="' + url + '" type="video/' + ext + '" /></video>';
  16222. //leefouce
  16223. // var ext = url.substr(url.lastIndexOf('.') + 1);
  16224. // if(ext == 'ogv') ext = 'ogg';
  16225. str = '<video' + (id ? ' id="' + id + '"' : '') + ' class="' + classname + ' video-js" ' + (align ? ' style="float:' + align + '"': '') +
  16226. ' controls preload="none" width="' + width + '" height="' + height + '" src="' + url + '" data-setup="{}">' +
  16227. '<source src="' + url + '" type="video/mp4" /></video>';
  16228. break;
  16229. }
  16230. return str;
  16231. }
  16232. function switchImgAndVideo(root,img2video){
  16233. utils.each(root.getNodesByTagName(img2video ? 'img' : 'embed video'),function(node){
  16234. var className = node.getAttr('class');
  16235. if(className && className.indexOf('edui-faked-video') != -1){
  16236. var html = creatInsertStr( img2video ? node.getAttr('_url') : node.getAttr('src'),node.getAttr('width'),node.getAttr('height'),null,node.getStyle('float') || '',className,img2video ? 'embed':'image');
  16237. node.parentNode.replaceChild(UE.uNode.createElement(html),node);
  16238. }
  16239. if(className && className.indexOf('edui-upload-video') != -1){
  16240. var html = creatInsertStr( img2video ? node.getAttr('_url') : node.getAttr('src'),node.getAttr('width'),node.getAttr('height'),null,node.getStyle('float') || '',className,img2video ? 'video':'image');
  16241. node.parentNode.replaceChild(UE.uNode.createElement(html),node);
  16242. }
  16243. })
  16244. }
  16245. me.addOutputRule(function(root){
  16246. switchImgAndVideo(root,true)
  16247. });
  16248. me.addInputRule(function(root){
  16249. switchImgAndVideo(root)
  16250. });
  16251. /**
  16252. * 插入视频
  16253. * @command insertvideo
  16254. * @method execCommand
  16255. * @param { String } cmd 命令字符串
  16256. * @param { Object } videoAttr 键值对对象, 描述一个视频的所有属性
  16257. * @example
  16258. * ```javascript
  16259. *
  16260. * var videoAttr = {
  16261. * //视频地址
  16262. * url: 'http://www.youku.com/xxx',
  16263. * //视频宽高值, 单位px
  16264. * width: 200,
  16265. * height: 100
  16266. * };
  16267. *
  16268. * //editor 是编辑器实例
  16269. * //向编辑器插入单个视频
  16270. * editor.execCommand( 'insertvideo', videoAttr );
  16271. * ```
  16272. */
  16273. /**
  16274. * 插入视频
  16275. * @command insertvideo
  16276. * @method execCommand
  16277. * @param { String } cmd 命令字符串
  16278. * @param { Array } videoArr 需要插入的视频的数组, 其中的每一个元素都是一个键值对对象, 描述了一个视频的所有属性
  16279. * @example
  16280. * ```javascript
  16281. *
  16282. * var videoAttr1 = {
  16283. * //视频地址
  16284. * url: 'http://www.youku.com/xxx',
  16285. * //视频宽高值, 单位px
  16286. * width: 200,
  16287. * height: 100
  16288. * },
  16289. * videoAttr2 = {
  16290. * //视频地址
  16291. * url: 'http://www.youku.com/xxx',
  16292. * //视频宽高值, 单位px
  16293. * width: 200,
  16294. * height: 100
  16295. * }
  16296. *
  16297. * //editor 是编辑器实例
  16298. * //该方法将会向编辑器内插入两个视频
  16299. * editor.execCommand( 'insertvideo', [ videoAttr1, videoAttr2 ] );
  16300. * ```
  16301. */
  16302. /**
  16303. * 查询当前光标所在处是否是一个视频
  16304. * @command insertvideo
  16305. * @method queryCommandState
  16306. * @param { String } cmd 需要查询的命令字符串
  16307. * @return { int } 如果当前光标所在处的元素是一个视频对象, 则返回1,否则返回0
  16308. * @example
  16309. * ```javascript
  16310. *
  16311. * //editor 是编辑器实例
  16312. * editor.queryCommandState( 'insertvideo' );
  16313. * ```
  16314. */
  16315. me.commands["insertvideo"] = {
  16316. execCommand: function (cmd, videoObjs, type){
  16317. videoObjs = utils.isArray(videoObjs)?videoObjs:[videoObjs];
  16318. var html = [],id = 'tmpVedio', cl;
  16319. for(var i=0,vi,len = videoObjs.length;i<len;i++){
  16320. vi = videoObjs[i];
  16321. cl = (type == 'upload' ? 'edui-upload-video video-js vjs-default-skin':'edui-faked-video');
  16322. html.push(creatInsertStr( vi.url, vi.width || 420, vi.height || 280, id + i, null, cl, 'image'));
  16323. }
  16324. me.execCommand("inserthtml",html.join(""),true);
  16325. var rng = this.selection.getRange();
  16326. for(var i= 0,len=videoObjs.length;i<len;i++){
  16327. var img = this.document.getElementById('tmpVedio'+i);
  16328. domUtils.removeAttributes(img,'id');
  16329. rng.selectNode(img).select();
  16330. me.execCommand('imagefloat',videoObjs[i].align)
  16331. }
  16332. },
  16333. queryCommandState : function(){
  16334. var img = me.selection.getRange().getClosedNode(),
  16335. flag = img && (img.className == "edui-faked-video" || img.className.indexOf("edui-upload-video")!=-1);
  16336. return flag ? 1 : 0;
  16337. }
  16338. };
  16339. };
  16340. // plugins/table.core.js
  16341. /**
  16342. * Created with JetBrains WebStorm.
  16343. * User: taoqili
  16344. * Date: 13-1-18
  16345. * Time: 上午11:09
  16346. * To change this template use File | Settings | File Templates.
  16347. */
  16348. /**
  16349. * UE表格操作类
  16350. * @param table
  16351. * @constructor
  16352. */
  16353. (function () {
  16354. var UETable = UE.UETable = function (table) {
  16355. this.table = table;
  16356. this.indexTable = [];
  16357. this.selectedTds = [];
  16358. this.cellsRange = {};
  16359. this.update(table);
  16360. };
  16361. //===以下为静态工具方法===
  16362. UETable.removeSelectedClass = function (cells) {
  16363. utils.each(cells, function (cell) {
  16364. domUtils.removeClasses(cell, "selectTdClass");
  16365. })
  16366. };
  16367. UETable.addSelectedClass = function (cells) {
  16368. utils.each(cells, function (cell) {
  16369. domUtils.addClass(cell, "selectTdClass");
  16370. })
  16371. };
  16372. UETable.isEmptyBlock = function (node) {
  16373. var reg = new RegExp(domUtils.fillChar, 'g');
  16374. if (node[browser.ie ? 'innerText' : 'textContent'].replace(/^\s*$/, '').replace(reg, '').length > 0) {
  16375. return 0;
  16376. }
  16377. for (var i in dtd.$isNotEmpty) if (dtd.$isNotEmpty.hasOwnProperty(i)) {
  16378. if (node.getElementsByTagName(i).length) {
  16379. return 0;
  16380. }
  16381. }
  16382. return 1;
  16383. };
  16384. UETable.getWidth = function (cell) {
  16385. if (!cell)return 0;
  16386. return parseInt(domUtils.getComputedStyle(cell, "width"), 10);
  16387. };
  16388. /**
  16389. * 获取单元格或者单元格组的“对齐”状态。 如果当前的检测对象是一个单元格组, 只有在满足所有单元格的 水平和竖直 对齐属性都相同的
  16390. * 条件时才会返回其状态值,否则将返回null; 如果当前只检测了一个单元格, 则直接返回当前单元格的对齐状态;
  16391. * @param table cell or table cells , 支持单个单元格dom对象 或者 单元格dom对象数组
  16392. * @return { align: 'left' || 'right' || 'center', valign: 'top' || 'middle' || 'bottom' } 或者 null
  16393. */
  16394. UETable.getTableCellAlignState = function ( cells ) {
  16395. !utils.isArray( cells ) && ( cells = [cells] );
  16396. var result = {},
  16397. status = ['align', 'valign'],
  16398. tempStatus = null,
  16399. isSame = true;//状态是否相同
  16400. utils.each( cells, function( cellNode ){
  16401. utils.each( status, function( currentState ){
  16402. tempStatus = cellNode.getAttribute( currentState );
  16403. if( !result[ currentState ] && tempStatus ) {
  16404. result[ currentState ] = tempStatus;
  16405. } else if( !result[ currentState ] || ( tempStatus !== result[ currentState ] ) ) {
  16406. isSame = false;
  16407. return false;
  16408. }
  16409. } );
  16410. return isSame;
  16411. });
  16412. return isSame ? result : null;
  16413. };
  16414. /**
  16415. * 根据当前选区获取相关的table信息
  16416. * @return {Object}
  16417. */
  16418. UETable.getTableItemsByRange = function (editor) {
  16419. var start = editor.selection.getStart();
  16420. //ff下会选中bookmark
  16421. if( start && start.id && start.id.indexOf('_baidu_bookmark_start_') === 0 && start.nextSibling) {
  16422. start = start.nextSibling;
  16423. }
  16424. //在table或者td边缘有可能存在选中tr的情况
  16425. var cell = start && domUtils.findParentByTagName(start, ["td", "th"], true),
  16426. tr = cell && cell.parentNode,
  16427. caption = start && domUtils.findParentByTagName(start, 'caption', true),
  16428. table = caption ? caption.parentNode : tr && tr.parentNode.parentNode;
  16429. return {
  16430. cell:cell,
  16431. tr:tr,
  16432. table:table,
  16433. caption:caption
  16434. }
  16435. };
  16436. UETable.getUETableBySelected = function (editor) {
  16437. var table = UETable.getTableItemsByRange(editor).table;
  16438. if (table && table.ueTable && table.ueTable.selectedTds.length) {
  16439. return table.ueTable;
  16440. }
  16441. return null;
  16442. };
  16443. UETable.getDefaultValue = function (editor, table) {
  16444. var borderMap = {
  16445. thin:'0px',
  16446. medium:'1px',
  16447. thick:'2px'
  16448. },
  16449. tableBorder, tdPadding, tdBorder, tmpValue;
  16450. if (!table) {
  16451. table = editor.document.createElement('table');
  16452. table.insertRow(0).insertCell(0).innerHTML = 'xxx';
  16453. editor.body.appendChild(table);
  16454. var td = table.getElementsByTagName('td')[0];
  16455. tmpValue = domUtils.getComputedStyle(table, 'border-left-width');
  16456. tableBorder = parseInt(borderMap[tmpValue] || tmpValue, 10);
  16457. tmpValue = domUtils.getComputedStyle(td, 'padding-left');
  16458. tdPadding = parseInt(borderMap[tmpValue] || tmpValue, 10);
  16459. tmpValue = domUtils.getComputedStyle(td, 'border-left-width');
  16460. tdBorder = parseInt(borderMap[tmpValue] || tmpValue, 10);
  16461. domUtils.remove(table);
  16462. return {
  16463. tableBorder:tableBorder,
  16464. tdPadding:tdPadding,
  16465. tdBorder:tdBorder
  16466. };
  16467. } else {
  16468. td = table.getElementsByTagName('td')[0];
  16469. tmpValue = domUtils.getComputedStyle(table, 'border-left-width');
  16470. tableBorder = parseInt(borderMap[tmpValue] || tmpValue, 10);
  16471. tmpValue = domUtils.getComputedStyle(td, 'padding-left');
  16472. tdPadding = parseInt(borderMap[tmpValue] || tmpValue, 10);
  16473. tmpValue = domUtils.getComputedStyle(td, 'border-left-width');
  16474. tdBorder = parseInt(borderMap[tmpValue] || tmpValue, 10);
  16475. return {
  16476. tableBorder:tableBorder,
  16477. tdPadding:tdPadding,
  16478. tdBorder:tdBorder
  16479. };
  16480. }
  16481. };
  16482. /**
  16483. * 根据当前点击的td或者table获取索引对象
  16484. * @param tdOrTable
  16485. */
  16486. UETable.getUETable = function (tdOrTable) {
  16487. var tag = tdOrTable.tagName.toLowerCase();
  16488. tdOrTable = (tag == "td" || tag == "th" || tag == 'caption') ? domUtils.findParentByTagName(tdOrTable, "table", true) : tdOrTable;
  16489. if (!tdOrTable.ueTable) {
  16490. tdOrTable.ueTable = new UETable(tdOrTable);
  16491. }
  16492. return tdOrTable.ueTable;
  16493. };
  16494. UETable.cloneCell = function(cell,ignoreMerge,keepPro){
  16495. if (!cell || utils.isString(cell)) {
  16496. return this.table.ownerDocument.createElement(cell || 'td');
  16497. }
  16498. var flag = domUtils.hasClass(cell, "selectTdClass");
  16499. flag && domUtils.removeClasses(cell, "selectTdClass");
  16500. var tmpCell = cell.cloneNode(true);
  16501. if (ignoreMerge) {
  16502. tmpCell.rowSpan = tmpCell.colSpan = 1;
  16503. }
  16504. //去掉宽高
  16505. !keepPro && domUtils.removeAttributes(tmpCell,'width height');
  16506. !keepPro && domUtils.removeAttributes(tmpCell,'style');
  16507. tmpCell.style.borderLeftStyle = "";
  16508. tmpCell.style.borderTopStyle = "";
  16509. tmpCell.style.borderLeftColor = cell.style.borderRightColor;
  16510. tmpCell.style.borderLeftWidth = cell.style.borderRightWidth;
  16511. tmpCell.style.borderTopColor = cell.style.borderBottomColor;
  16512. tmpCell.style.borderTopWidth = cell.style.borderBottomWidth;
  16513. flag && domUtils.addClass(cell, "selectTdClass");
  16514. return tmpCell;
  16515. }
  16516. UETable.prototype = {
  16517. getMaxRows:function () {
  16518. var rows = this.table.rows, maxLen = 1;
  16519. for (var i = 0, row; row = rows[i]; i++) {
  16520. var currentMax = 1;
  16521. for (var j = 0, cj; cj = row.cells[j++];) {
  16522. currentMax = Math.max(cj.rowSpan || 1, currentMax);
  16523. }
  16524. maxLen = Math.max(currentMax + i, maxLen);
  16525. }
  16526. return maxLen;
  16527. },
  16528. /**
  16529. * 获取当前表格的最大列数
  16530. */
  16531. getMaxCols:function () {
  16532. var rows = this.table.rows, maxLen = 0, cellRows = {};
  16533. for (var i = 0, row; row = rows[i]; i++) {
  16534. var cellsNum = 0;
  16535. for (var j = 0, cj; cj = row.cells[j++];) {
  16536. cellsNum += (cj.colSpan || 1);
  16537. if (cj.rowSpan && cj.rowSpan > 1) {
  16538. for (var k = 1; k < cj.rowSpan; k++) {
  16539. if (!cellRows['row_' + (i + k)]) {
  16540. cellRows['row_' + (i + k)] = (cj.colSpan || 1);
  16541. } else {
  16542. cellRows['row_' + (i + k)]++
  16543. }
  16544. }
  16545. }
  16546. }
  16547. cellsNum += cellRows['row_' + i] || 0;
  16548. maxLen = Math.max(cellsNum, maxLen);
  16549. }
  16550. return maxLen;
  16551. },
  16552. getCellColIndex:function (cell) {
  16553. },
  16554. /**
  16555. * 获取当前cell旁边的单元格,
  16556. * @param cell
  16557. * @param right
  16558. */
  16559. getHSideCell:function (cell, right) {
  16560. try {
  16561. var cellInfo = this.getCellInfo(cell),
  16562. previewRowIndex, previewColIndex;
  16563. var len = this.selectedTds.length,
  16564. range = this.cellsRange;
  16565. //首行或者首列没有前置单元格
  16566. if ((!right && (!len ? !cellInfo.colIndex : !range.beginColIndex)) || (right && (!len ? (cellInfo.colIndex == (this.colsNum - 1)) : (range.endColIndex == this.colsNum - 1)))) return null;
  16567. previewRowIndex = !len ? cellInfo.rowIndex : range.beginRowIndex;
  16568. previewColIndex = !right ? ( !len ? (cellInfo.colIndex < 1 ? 0 : (cellInfo.colIndex - 1)) : range.beginColIndex - 1)
  16569. : ( !len ? cellInfo.colIndex + 1 : range.endColIndex + 1);
  16570. return this.getCell(this.indexTable[previewRowIndex][previewColIndex].rowIndex, this.indexTable[previewRowIndex][previewColIndex].cellIndex);
  16571. } catch (e) {
  16572. showError(e);
  16573. }
  16574. },
  16575. getTabNextCell:function (cell, preRowIndex) {
  16576. var cellInfo = this.getCellInfo(cell),
  16577. rowIndex = preRowIndex || cellInfo.rowIndex,
  16578. colIndex = cellInfo.colIndex + 1 + (cellInfo.colSpan - 1),
  16579. nextCell;
  16580. try {
  16581. nextCell = this.getCell(this.indexTable[rowIndex][colIndex].rowIndex, this.indexTable[rowIndex][colIndex].cellIndex);
  16582. } catch (e) {
  16583. try {
  16584. rowIndex = rowIndex * 1 + 1;
  16585. colIndex = 0;
  16586. nextCell = this.getCell(this.indexTable[rowIndex][colIndex].rowIndex, this.indexTable[rowIndex][colIndex].cellIndex);
  16587. } catch (e) {
  16588. }
  16589. }
  16590. return nextCell;
  16591. },
  16592. /**
  16593. * 获取视觉上的后置单元格
  16594. * @param cell
  16595. * @param bottom
  16596. */
  16597. getVSideCell:function (cell, bottom, ignoreRange) {
  16598. try {
  16599. var cellInfo = this.getCellInfo(cell),
  16600. nextRowIndex, nextColIndex;
  16601. var len = this.selectedTds.length && !ignoreRange,
  16602. range = this.cellsRange;
  16603. //末行或者末列没有后置单元格
  16604. if ((!bottom && (cellInfo.rowIndex == 0)) || (bottom && (!len ? (cellInfo.rowIndex + cellInfo.rowSpan > this.rowsNum - 1) : (range.endRowIndex == this.rowsNum - 1)))) return null;
  16605. nextRowIndex = !bottom ? ( !len ? cellInfo.rowIndex - 1 : range.beginRowIndex - 1)
  16606. : ( !len ? (cellInfo.rowIndex + cellInfo.rowSpan) : range.endRowIndex + 1);
  16607. nextColIndex = !len ? cellInfo.colIndex : range.beginColIndex;
  16608. return this.getCell(this.indexTable[nextRowIndex][nextColIndex].rowIndex, this.indexTable[nextRowIndex][nextColIndex].cellIndex);
  16609. } catch (e) {
  16610. showError(e);
  16611. }
  16612. },
  16613. /**
  16614. * 获取相同结束位置的单元格,xOrY指代了是获取x轴相同还是y轴相同
  16615. */
  16616. getSameEndPosCells:function (cell, xOrY) {
  16617. try {
  16618. var flag = (xOrY.toLowerCase() === "x"),
  16619. end = domUtils.getXY(cell)[flag ? 'x' : 'y'] + cell["offset" + (flag ? 'Width' : 'Height')],
  16620. rows = this.table.rows,
  16621. cells = null, returns = [];
  16622. for (var i = 0; i < this.rowsNum; i++) {
  16623. cells = rows[i].cells;
  16624. for (var j = 0, tmpCell; tmpCell = cells[j++];) {
  16625. var tmpEnd = domUtils.getXY(tmpCell)[flag ? 'x' : 'y'] + tmpCell["offset" + (flag ? 'Width' : 'Height')];
  16626. //对应行的td已经被上面行rowSpan了
  16627. if (tmpEnd > end && flag) break;
  16628. if (cell == tmpCell || end == tmpEnd) {
  16629. //只获取单一的单元格
  16630. //todo 仅获取单一单元格在特定情况下会造成returns为空,从而影响后续的拖拽实现,修正这个。需考虑性能
  16631. if (tmpCell[flag ? "colSpan" : "rowSpan"] == 1) {
  16632. returns.push(tmpCell);
  16633. }
  16634. if (flag) break;
  16635. }
  16636. }
  16637. }
  16638. return returns;
  16639. } catch (e) {
  16640. showError(e);
  16641. }
  16642. },
  16643. setCellContent:function (cell, content) {
  16644. cell.innerHTML = content || (browser.ie ? domUtils.fillChar : "<br />");
  16645. },
  16646. cloneCell:UETable.cloneCell,
  16647. /**
  16648. * 获取跟当前单元格的右边竖线为左边的所有未合并单元格
  16649. */
  16650. getSameStartPosXCells:function (cell) {
  16651. try {
  16652. var start = domUtils.getXY(cell).x + cell.offsetWidth,
  16653. rows = this.table.rows, cells , returns = [];
  16654. for (var i = 0; i < this.rowsNum; i++) {
  16655. cells = rows[i].cells;
  16656. for (var j = 0, tmpCell; tmpCell = cells[j++];) {
  16657. var tmpStart = domUtils.getXY(tmpCell).x;
  16658. if (tmpStart > start) break;
  16659. if (tmpStart == start && tmpCell.colSpan == 1) {
  16660. returns.push(tmpCell);
  16661. break;
  16662. }
  16663. }
  16664. }
  16665. return returns;
  16666. } catch (e) {
  16667. showError(e);
  16668. }
  16669. },
  16670. /**
  16671. * 更新table对应的索引表
  16672. */
  16673. update:function (table) {
  16674. this.table = table || this.table;
  16675. this.selectedTds = [];
  16676. this.cellsRange = {};
  16677. this.indexTable = [];
  16678. var rows = this.table.rows,
  16679. rowsNum = this.getMaxRows(),
  16680. dNum = rowsNum - rows.length,
  16681. colsNum = this.getMaxCols();
  16682. while (dNum--) {
  16683. this.table.insertRow(rows.length);
  16684. }
  16685. this.rowsNum = rowsNum;
  16686. this.colsNum = colsNum;
  16687. for (var i = 0, len = rows.length; i < len; i++) {
  16688. this.indexTable[i] = new Array(colsNum);
  16689. }
  16690. //填充索引表
  16691. for (var rowIndex = 0, row; row = rows[rowIndex]; rowIndex++) {
  16692. for (var cellIndex = 0, cell, cells = row.cells; cell = cells[cellIndex]; cellIndex++) {
  16693. //修正整行被rowSpan时导致的行数计算错误
  16694. if (cell.rowSpan > rowsNum) {
  16695. cell.rowSpan = rowsNum;
  16696. }
  16697. var colIndex = cellIndex,
  16698. rowSpan = cell.rowSpan || 1,
  16699. colSpan = cell.colSpan || 1;
  16700. //当已经被上一行rowSpan或者被前一列colSpan了,则跳到下一个单元格进行
  16701. while (this.indexTable[rowIndex][colIndex]) colIndex++;
  16702. for (var j = 0; j < rowSpan; j++) {
  16703. for (var k = 0; k < colSpan; k++) {
  16704. this.indexTable[rowIndex + j][colIndex + k] = {
  16705. rowIndex:rowIndex,
  16706. cellIndex:cellIndex,
  16707. colIndex:colIndex,
  16708. rowSpan:rowSpan,
  16709. colSpan:colSpan
  16710. }
  16711. }
  16712. }
  16713. }
  16714. }
  16715. //修复残缺td
  16716. for (j = 0; j < rowsNum; j++) {
  16717. for (k = 0; k < colsNum; k++) {
  16718. if (this.indexTable[j][k] === undefined) {
  16719. row = rows[j];
  16720. cell = row.cells[row.cells.length - 1];
  16721. cell = cell ? cell.cloneNode(true) : this.table.ownerDocument.createElement("td");
  16722. this.setCellContent(cell);
  16723. if (cell.colSpan !== 1)cell.colSpan = 1;
  16724. if (cell.rowSpan !== 1)cell.rowSpan = 1;
  16725. row.appendChild(cell);
  16726. this.indexTable[j][k] = {
  16727. rowIndex:j,
  16728. cellIndex:cell.cellIndex,
  16729. colIndex:k,
  16730. rowSpan:1,
  16731. colSpan:1
  16732. }
  16733. }
  16734. }
  16735. }
  16736. //当框选后删除行或者列后撤销,需要重建选区。
  16737. var tds = domUtils.getElementsByTagName(this.table, "td"),
  16738. selectTds = [];
  16739. utils.each(tds, function (td) {
  16740. if (domUtils.hasClass(td, "selectTdClass")) {
  16741. selectTds.push(td);
  16742. }
  16743. });
  16744. if (selectTds.length) {
  16745. var start = selectTds[0],
  16746. end = selectTds[selectTds.length - 1],
  16747. startInfo = this.getCellInfo(start),
  16748. endInfo = this.getCellInfo(end);
  16749. this.selectedTds = selectTds;
  16750. this.cellsRange = {
  16751. beginRowIndex:startInfo.rowIndex,
  16752. beginColIndex:startInfo.colIndex,
  16753. endRowIndex:endInfo.rowIndex + endInfo.rowSpan - 1,
  16754. endColIndex:endInfo.colIndex + endInfo.colSpan - 1
  16755. };
  16756. }
  16757. //给第一行设置firstRow的样式名称,在排序图标的样式上使用到
  16758. if(!domUtils.hasClass(this.table.rows[0], "firstRow")) {
  16759. domUtils.addClass(this.table.rows[0], "firstRow");
  16760. for(var i = 1; i< this.table.rows.length; i++) {
  16761. domUtils.removeClasses(this.table.rows[i], "firstRow");
  16762. }
  16763. }
  16764. },
  16765. /**
  16766. * 获取单元格的索引信息
  16767. */
  16768. getCellInfo:function (cell) {
  16769. if (!cell) return;
  16770. var cellIndex = cell.cellIndex,
  16771. rowIndex = cell.parentNode.rowIndex,
  16772. rowInfo = this.indexTable[rowIndex],
  16773. numCols = this.colsNum;
  16774. for (var colIndex = cellIndex; colIndex < numCols; colIndex++) {
  16775. var cellInfo = rowInfo[colIndex];
  16776. if (cellInfo.rowIndex === rowIndex && cellInfo.cellIndex === cellIndex) {
  16777. return cellInfo;
  16778. }
  16779. }
  16780. },
  16781. /**
  16782. * 根据行列号获取单元格
  16783. */
  16784. getCell:function (rowIndex, cellIndex) {
  16785. return rowIndex < this.rowsNum && this.table.rows[rowIndex].cells[cellIndex] || null;
  16786. },
  16787. /**
  16788. * 删除单元格
  16789. */
  16790. deleteCell:function (cell, rowIndex) {
  16791. rowIndex = typeof rowIndex == 'number' ? rowIndex : cell.parentNode.rowIndex;
  16792. var row = this.table.rows[rowIndex];
  16793. row.deleteCell(cell.cellIndex);
  16794. },
  16795. /**
  16796. * 根据始末两个单元格获取被框选的所有单元格范围
  16797. */
  16798. getCellsRange:function (cellA, cellB) {
  16799. function checkRange(beginRowIndex, beginColIndex, endRowIndex, endColIndex) {
  16800. var tmpBeginRowIndex = beginRowIndex,
  16801. tmpBeginColIndex = beginColIndex,
  16802. tmpEndRowIndex = endRowIndex,
  16803. tmpEndColIndex = endColIndex,
  16804. cellInfo, colIndex, rowIndex;
  16805. // 通过indexTable检查是否存在超出TableRange上边界的情况
  16806. if (beginRowIndex > 0) {
  16807. for (colIndex = beginColIndex; colIndex < endColIndex; colIndex++) {
  16808. cellInfo = me.indexTable[beginRowIndex][colIndex];
  16809. rowIndex = cellInfo.rowIndex;
  16810. if (rowIndex < beginRowIndex) {
  16811. tmpBeginRowIndex = Math.min(rowIndex, tmpBeginRowIndex);
  16812. }
  16813. }
  16814. }
  16815. // 通过indexTable检查是否存在超出TableRange右边界的情况
  16816. if (endColIndex < me.colsNum) {
  16817. for (rowIndex = beginRowIndex; rowIndex < endRowIndex; rowIndex++) {
  16818. cellInfo = me.indexTable[rowIndex][endColIndex];
  16819. colIndex = cellInfo.colIndex + cellInfo.colSpan - 1;
  16820. if (colIndex > endColIndex) {
  16821. tmpEndColIndex = Math.max(colIndex, tmpEndColIndex);
  16822. }
  16823. }
  16824. }
  16825. // 检查是否有超出TableRange下边界的情况
  16826. if (endRowIndex < me.rowsNum) {
  16827. for (colIndex = beginColIndex; colIndex < endColIndex; colIndex++) {
  16828. cellInfo = me.indexTable[endRowIndex][colIndex];
  16829. rowIndex = cellInfo.rowIndex + cellInfo.rowSpan - 1;
  16830. if (rowIndex > endRowIndex) {
  16831. tmpEndRowIndex = Math.max(rowIndex, tmpEndRowIndex);
  16832. }
  16833. }
  16834. }
  16835. // 检查是否有超出TableRange左边界的情况
  16836. if (beginColIndex > 0) {
  16837. for (rowIndex = beginRowIndex; rowIndex < endRowIndex; rowIndex++) {
  16838. cellInfo = me.indexTable[rowIndex][beginColIndex];
  16839. colIndex = cellInfo.colIndex;
  16840. if (colIndex < beginColIndex) {
  16841. tmpBeginColIndex = Math.min(cellInfo.colIndex, tmpBeginColIndex);
  16842. }
  16843. }
  16844. }
  16845. //递归调用直至所有完成所有框选单元格的扩展
  16846. if (tmpBeginRowIndex != beginRowIndex || tmpBeginColIndex != beginColIndex || tmpEndRowIndex != endRowIndex || tmpEndColIndex != endColIndex) {
  16847. return checkRange(tmpBeginRowIndex, tmpBeginColIndex, tmpEndRowIndex, tmpEndColIndex);
  16848. } else {
  16849. // 不需要扩展TableRange的情况
  16850. return {
  16851. beginRowIndex:beginRowIndex,
  16852. beginColIndex:beginColIndex,
  16853. endRowIndex:endRowIndex,
  16854. endColIndex:endColIndex
  16855. };
  16856. }
  16857. }
  16858. try {
  16859. var me = this,
  16860. cellAInfo = me.getCellInfo(cellA);
  16861. if (cellA === cellB) {
  16862. return {
  16863. beginRowIndex:cellAInfo.rowIndex,
  16864. beginColIndex:cellAInfo.colIndex,
  16865. endRowIndex:cellAInfo.rowIndex + cellAInfo.rowSpan - 1,
  16866. endColIndex:cellAInfo.colIndex + cellAInfo.colSpan - 1
  16867. };
  16868. }
  16869. var cellBInfo = me.getCellInfo(cellB);
  16870. // 计算TableRange的四个边
  16871. var beginRowIndex = Math.min(cellAInfo.rowIndex, cellBInfo.rowIndex),
  16872. beginColIndex = Math.min(cellAInfo.colIndex, cellBInfo.colIndex),
  16873. endRowIndex = Math.max(cellAInfo.rowIndex + cellAInfo.rowSpan - 1, cellBInfo.rowIndex + cellBInfo.rowSpan - 1),
  16874. endColIndex = Math.max(cellAInfo.colIndex + cellAInfo.colSpan - 1, cellBInfo.colIndex + cellBInfo.colSpan - 1);
  16875. return checkRange(beginRowIndex, beginColIndex, endRowIndex, endColIndex);
  16876. } catch (e) {
  16877. //throw e;
  16878. }
  16879. },
  16880. /**
  16881. * 依据cellsRange获取对应的单元格集合
  16882. */
  16883. getCells:function (range) {
  16884. //每次获取cells之前必须先清除上次的选择,否则会对后续获取操作造成影响
  16885. this.clearSelected();
  16886. var beginRowIndex = range.beginRowIndex,
  16887. beginColIndex = range.beginColIndex,
  16888. endRowIndex = range.endRowIndex,
  16889. endColIndex = range.endColIndex,
  16890. cellInfo, rowIndex, colIndex, tdHash = {}, returnTds = [];
  16891. for (var i = beginRowIndex; i <= endRowIndex; i++) {
  16892. for (var j = beginColIndex; j <= endColIndex; j++) {
  16893. cellInfo = this.indexTable[i][j];
  16894. rowIndex = cellInfo.rowIndex;
  16895. colIndex = cellInfo.colIndex;
  16896. // 如果Cells里已经包含了此Cell则跳过
  16897. var key = rowIndex + '|' + colIndex;
  16898. if (tdHash[key]) continue;
  16899. tdHash[key] = 1;
  16900. if (rowIndex < i || colIndex < j || rowIndex + cellInfo.rowSpan - 1 > endRowIndex || colIndex + cellInfo.colSpan - 1 > endColIndex) {
  16901. return null;
  16902. }
  16903. returnTds.push(this.getCell(rowIndex, cellInfo.cellIndex));
  16904. }
  16905. }
  16906. return returnTds;
  16907. },
  16908. /**
  16909. * 清理已经选中的单元格
  16910. */
  16911. clearSelected:function () {
  16912. UETable.removeSelectedClass(this.selectedTds);
  16913. this.selectedTds = [];
  16914. this.cellsRange = {};
  16915. },
  16916. /**
  16917. * 根据range设置已经选中的单元格
  16918. */
  16919. setSelected:function (range) {
  16920. var cells = this.getCells(range);
  16921. UETable.addSelectedClass(cells);
  16922. this.selectedTds = cells;
  16923. this.cellsRange = range;
  16924. },
  16925. isFullRow:function () {
  16926. var range = this.cellsRange;
  16927. return (range.endColIndex - range.beginColIndex + 1) == this.colsNum;
  16928. },
  16929. isFullCol:function () {
  16930. var range = this.cellsRange,
  16931. table = this.table,
  16932. ths = table.getElementsByTagName("th"),
  16933. rows = range.endRowIndex - range.beginRowIndex + 1;
  16934. return !ths.length ? rows == this.rowsNum : rows == this.rowsNum || (rows == this.rowsNum - 1);
  16935. },
  16936. /**
  16937. * 获取视觉上的前置单元格,默认是左边,top传入时
  16938. * @param cell
  16939. * @param top
  16940. */
  16941. getNextCell:function (cell, bottom, ignoreRange) {
  16942. try {
  16943. var cellInfo = this.getCellInfo(cell),
  16944. nextRowIndex, nextColIndex;
  16945. var len = this.selectedTds.length && !ignoreRange,
  16946. range = this.cellsRange;
  16947. //末行或者末列没有后置单元格
  16948. if ((!bottom && (cellInfo.rowIndex == 0)) || (bottom && (!len ? (cellInfo.rowIndex + cellInfo.rowSpan > this.rowsNum - 1) : (range.endRowIndex == this.rowsNum - 1)))) return null;
  16949. nextRowIndex = !bottom ? ( !len ? cellInfo.rowIndex - 1 : range.beginRowIndex - 1)
  16950. : ( !len ? (cellInfo.rowIndex + cellInfo.rowSpan) : range.endRowIndex + 1);
  16951. nextColIndex = !len ? cellInfo.colIndex : range.beginColIndex;
  16952. return this.getCell(this.indexTable[nextRowIndex][nextColIndex].rowIndex, this.indexTable[nextRowIndex][nextColIndex].cellIndex);
  16953. } catch (e) {
  16954. showError(e);
  16955. }
  16956. },
  16957. getPreviewCell:function (cell, top) {
  16958. try {
  16959. var cellInfo = this.getCellInfo(cell),
  16960. previewRowIndex, previewColIndex;
  16961. var len = this.selectedTds.length,
  16962. range = this.cellsRange;
  16963. //首行或者首列没有前置单元格
  16964. if ((!top && (!len ? !cellInfo.colIndex : !range.beginColIndex)) || (top && (!len ? (cellInfo.rowIndex > (this.colsNum - 1)) : (range.endColIndex == this.colsNum - 1)))) return null;
  16965. previewRowIndex = !top ? ( !len ? cellInfo.rowIndex : range.beginRowIndex )
  16966. : ( !len ? (cellInfo.rowIndex < 1 ? 0 : (cellInfo.rowIndex - 1)) : range.beginRowIndex);
  16967. previewColIndex = !top ? ( !len ? (cellInfo.colIndex < 1 ? 0 : (cellInfo.colIndex - 1)) : range.beginColIndex - 1)
  16968. : ( !len ? cellInfo.colIndex : range.endColIndex + 1);
  16969. return this.getCell(this.indexTable[previewRowIndex][previewColIndex].rowIndex, this.indexTable[previewRowIndex][previewColIndex].cellIndex);
  16970. } catch (e) {
  16971. showError(e);
  16972. }
  16973. },
  16974. /**
  16975. * 移动单元格中的内容
  16976. */
  16977. moveContent:function (cellTo, cellFrom) {
  16978. if (UETable.isEmptyBlock(cellFrom)) return;
  16979. if (UETable.isEmptyBlock(cellTo)) {
  16980. cellTo.innerHTML = cellFrom.innerHTML;
  16981. return;
  16982. }
  16983. var child = cellTo.lastChild;
  16984. if (child.nodeType == 3 || !dtd.$block[child.tagName]) {
  16985. cellTo.appendChild(cellTo.ownerDocument.createElement('br'))
  16986. }
  16987. while (child = cellFrom.firstChild) {
  16988. cellTo.appendChild(child);
  16989. }
  16990. },
  16991. /**
  16992. * 向右合并单元格
  16993. */
  16994. mergeRight:function (cell) {
  16995. var cellInfo = this.getCellInfo(cell),
  16996. rightColIndex = cellInfo.colIndex + cellInfo.colSpan,
  16997. rightCellInfo = this.indexTable[cellInfo.rowIndex][rightColIndex],
  16998. rightCell = this.getCell(rightCellInfo.rowIndex, rightCellInfo.cellIndex);
  16999. //合并
  17000. cell.colSpan = cellInfo.colSpan + rightCellInfo.colSpan;
  17001. //被合并的单元格不应存在宽度属性
  17002. cell.removeAttribute("width");
  17003. //移动内容
  17004. this.moveContent(cell, rightCell);
  17005. //删掉被合并的Cell
  17006. this.deleteCell(rightCell, rightCellInfo.rowIndex);
  17007. this.update();
  17008. },
  17009. /**
  17010. * 向下合并单元格
  17011. */
  17012. mergeDown:function (cell) {
  17013. var cellInfo = this.getCellInfo(cell),
  17014. downRowIndex = cellInfo.rowIndex + cellInfo.rowSpan,
  17015. downCellInfo = this.indexTable[downRowIndex][cellInfo.colIndex],
  17016. downCell = this.getCell(downCellInfo.rowIndex, downCellInfo.cellIndex);
  17017. cell.rowSpan = cellInfo.rowSpan + downCellInfo.rowSpan;
  17018. cell.removeAttribute("height");
  17019. this.moveContent(cell, downCell);
  17020. this.deleteCell(downCell, downCellInfo.rowIndex);
  17021. this.update();
  17022. },
  17023. /**
  17024. * 合并整个range中的内容
  17025. */
  17026. mergeRange:function () {
  17027. //由于合并操作可以在任意时刻进行,所以无法通过鼠标位置等信息实时生成range,只能通过缓存实例中的cellsRange对象来访问
  17028. var range = this.cellsRange,
  17029. leftTopCell = this.getCell(range.beginRowIndex, this.indexTable[range.beginRowIndex][range.beginColIndex].cellIndex);
  17030. if (leftTopCell.tagName == "TH" && range.endRowIndex !== range.beginRowIndex) {
  17031. var index = this.indexTable,
  17032. info = this.getCellInfo(leftTopCell);
  17033. leftTopCell = this.getCell(1, index[1][info.colIndex].cellIndex);
  17034. range = this.getCellsRange(leftTopCell, this.getCell(index[this.rowsNum - 1][info.colIndex].rowIndex, index[this.rowsNum - 1][info.colIndex].cellIndex));
  17035. }
  17036. // 删除剩余的Cells
  17037. var cells = this.getCells(range);
  17038. for(var i= 0,ci;ci=cells[i++];){
  17039. if (ci !== leftTopCell) {
  17040. this.moveContent(leftTopCell, ci);
  17041. this.deleteCell(ci);
  17042. }
  17043. }
  17044. // 修改左上角Cell的rowSpan和colSpan,并调整宽度属性设置
  17045. leftTopCell.rowSpan = range.endRowIndex - range.beginRowIndex + 1;
  17046. leftTopCell.rowSpan > 1 && leftTopCell.removeAttribute("height");
  17047. leftTopCell.colSpan = range.endColIndex - range.beginColIndex + 1;
  17048. leftTopCell.colSpan > 1 && leftTopCell.removeAttribute("width");
  17049. if (leftTopCell.rowSpan == this.rowsNum && leftTopCell.colSpan != 1) {
  17050. leftTopCell.colSpan = 1;
  17051. }
  17052. if (leftTopCell.colSpan == this.colsNum && leftTopCell.rowSpan != 1) {
  17053. var rowIndex = leftTopCell.parentNode.rowIndex;
  17054. //解决IE下的表格操作问题
  17055. if( this.table.deleteRow ) {
  17056. for (var i = rowIndex+ 1, curIndex=rowIndex+ 1, len=leftTopCell.rowSpan; i < len; i++) {
  17057. this.table.deleteRow(curIndex);
  17058. }
  17059. } else {
  17060. for (var i = 0, len=leftTopCell.rowSpan - 1; i < len; i++) {
  17061. var row = this.table.rows[rowIndex + 1];
  17062. row.parentNode.removeChild(row);
  17063. }
  17064. }
  17065. leftTopCell.rowSpan = 1;
  17066. }
  17067. this.update();
  17068. },
  17069. /**
  17070. * 插入一行单元格
  17071. */
  17072. insertRow:function (rowIndex, sourceCell) {
  17073. var numCols = this.colsNum,
  17074. table = this.table,
  17075. row = table.insertRow(rowIndex), cell,
  17076. isInsertTitle = typeof sourceCell == 'string' && sourceCell.toUpperCase() == 'TH';
  17077. function replaceTdToTh(colIndex, cell, tableRow) {
  17078. if (colIndex == 0) {
  17079. var tr = tableRow.nextSibling || tableRow.previousSibling,
  17080. th = tr.cells[colIndex];
  17081. if (th.tagName == 'TH') {
  17082. th = cell.ownerDocument.createElement("th");
  17083. th.appendChild(cell.firstChild);
  17084. tableRow.insertBefore(th, cell);
  17085. domUtils.remove(cell)
  17086. }
  17087. }else{
  17088. if (cell.tagName == 'TH') {
  17089. var td = cell.ownerDocument.createElement("td");
  17090. td.appendChild(cell.firstChild);
  17091. tableRow.insertBefore(td, cell);
  17092. domUtils.remove(cell)
  17093. }
  17094. }
  17095. }
  17096. //首行直接插入,无需考虑部分单元格被rowspan的情况
  17097. if (rowIndex == 0 || rowIndex == this.rowsNum) {
  17098. for (var colIndex = 0; colIndex < numCols; colIndex++) {
  17099. cell = this.cloneCell(sourceCell, true);
  17100. this.setCellContent(cell);
  17101. cell.getAttribute('vAlign') && cell.setAttribute('vAlign', cell.getAttribute('vAlign'));
  17102. row.appendChild(cell);
  17103. if(!isInsertTitle) replaceTdToTh(colIndex, cell, row);
  17104. }
  17105. } else {
  17106. var infoRow = this.indexTable[rowIndex],
  17107. cellIndex = 0;
  17108. for (colIndex = 0; colIndex < numCols; colIndex++) {
  17109. var cellInfo = infoRow[colIndex];
  17110. //如果存在某个单元格的rowspan穿过待插入行的位置,则修改该单元格的rowspan即可,无需插入单元格
  17111. if (cellInfo.rowIndex < rowIndex) {
  17112. cell = this.getCell(cellInfo.rowIndex, cellInfo.cellIndex);
  17113. cell.rowSpan = cellInfo.rowSpan + 1;
  17114. } else {
  17115. cell = this.cloneCell(sourceCell, true);
  17116. this.setCellContent(cell);
  17117. row.appendChild(cell);
  17118. }
  17119. if(!isInsertTitle) replaceTdToTh(colIndex, cell, row);
  17120. }
  17121. }
  17122. //框选时插入不触发contentchange,需要手动更新索引。
  17123. this.update();
  17124. return row;
  17125. },
  17126. /**
  17127. * 删除一行单元格
  17128. * @param rowIndex
  17129. */
  17130. deleteRow:function (rowIndex) {
  17131. var row = this.table.rows[rowIndex],
  17132. infoRow = this.indexTable[rowIndex],
  17133. colsNum = this.colsNum,
  17134. count = 0; //处理计数
  17135. for (var colIndex = 0; colIndex < colsNum;) {
  17136. var cellInfo = infoRow[colIndex],
  17137. cell = this.getCell(cellInfo.rowIndex, cellInfo.cellIndex);
  17138. if (cell.rowSpan > 1) {
  17139. if (cellInfo.rowIndex == rowIndex) {
  17140. var clone = cell.cloneNode(true);
  17141. clone.rowSpan = cell.rowSpan - 1;
  17142. clone.innerHTML = "";
  17143. cell.rowSpan = 1;
  17144. var nextRowIndex = rowIndex + 1,
  17145. nextRow = this.table.rows[nextRowIndex],
  17146. insertCellIndex,
  17147. preMerged = this.getPreviewMergedCellsNum(nextRowIndex, colIndex) - count;
  17148. if (preMerged < colIndex) {
  17149. insertCellIndex = colIndex - preMerged - 1;
  17150. //nextRow.insertCell(insertCellIndex);
  17151. domUtils.insertAfter(nextRow.cells[insertCellIndex], clone);
  17152. } else {
  17153. if (nextRow.cells.length) nextRow.insertBefore(clone, nextRow.cells[0])
  17154. }
  17155. count += 1;
  17156. //cell.parentNode.removeChild(cell);
  17157. }
  17158. }
  17159. colIndex += cell.colSpan || 1;
  17160. }
  17161. var deleteTds = [], cacheMap = {};
  17162. for (colIndex = 0; colIndex < colsNum; colIndex++) {
  17163. var tmpRowIndex = infoRow[colIndex].rowIndex,
  17164. tmpCellIndex = infoRow[colIndex].cellIndex,
  17165. key = tmpRowIndex + "_" + tmpCellIndex;
  17166. if (cacheMap[key])continue;
  17167. cacheMap[key] = 1;
  17168. cell = this.getCell(tmpRowIndex, tmpCellIndex);
  17169. deleteTds.push(cell);
  17170. }
  17171. var mergeTds = [];
  17172. utils.each(deleteTds, function (td) {
  17173. if (td.rowSpan == 1) {
  17174. td.parentNode.removeChild(td);
  17175. } else {
  17176. mergeTds.push(td);
  17177. }
  17178. });
  17179. utils.each(mergeTds, function (td) {
  17180. td.rowSpan--;
  17181. });
  17182. row.parentNode.removeChild(row);
  17183. //浏览器方法本身存在bug,采用自定义方法删除
  17184. //this.table.deleteRow(rowIndex);
  17185. this.update();
  17186. },
  17187. insertCol:function (colIndex, sourceCell, defaultValue) {
  17188. var rowsNum = this.rowsNum,
  17189. rowIndex = 0,
  17190. tableRow, cell,
  17191. backWidth = parseInt((this.table.offsetWidth - (this.colsNum + 1) * 20 - (this.colsNum + 1)) / (this.colsNum + 1), 10),
  17192. isInsertTitleCol = typeof sourceCell == 'string' && sourceCell.toUpperCase() == 'TH';
  17193. function replaceTdToTh(rowIndex, cell, tableRow) {
  17194. if (rowIndex == 0) {
  17195. var th = cell.nextSibling || cell.previousSibling;
  17196. if (th.tagName == 'TH') {
  17197. th = cell.ownerDocument.createElement("th");
  17198. th.appendChild(cell.firstChild);
  17199. tableRow.insertBefore(th, cell);
  17200. domUtils.remove(cell)
  17201. }
  17202. }else{
  17203. if (cell.tagName == 'TH') {
  17204. var td = cell.ownerDocument.createElement("td");
  17205. td.appendChild(cell.firstChild);
  17206. tableRow.insertBefore(td, cell);
  17207. domUtils.remove(cell)
  17208. }
  17209. }
  17210. }
  17211. var preCell;
  17212. if (colIndex == 0 || colIndex == this.colsNum) {
  17213. for (; rowIndex < rowsNum; rowIndex++) {
  17214. tableRow = this.table.rows[rowIndex];
  17215. preCell = tableRow.cells[colIndex == 0 ? colIndex : tableRow.cells.length];
  17216. cell = this.cloneCell(sourceCell, true); //tableRow.insertCell(colIndex == 0 ? colIndex : tableRow.cells.length);
  17217. this.setCellContent(cell);
  17218. cell.setAttribute('vAlign', cell.getAttribute('vAlign'));
  17219. preCell && cell.setAttribute('width', preCell.getAttribute('width'));
  17220. if (!colIndex) {
  17221. tableRow.insertBefore(cell, tableRow.cells[0]);
  17222. } else {
  17223. domUtils.insertAfter(tableRow.cells[tableRow.cells.length - 1], cell);
  17224. }
  17225. if(!isInsertTitleCol) replaceTdToTh(rowIndex, cell, tableRow)
  17226. }
  17227. } else {
  17228. for (; rowIndex < rowsNum; rowIndex++) {
  17229. var cellInfo = this.indexTable[rowIndex][colIndex];
  17230. if (cellInfo.colIndex < colIndex) {
  17231. cell = this.getCell(cellInfo.rowIndex, cellInfo.cellIndex);
  17232. cell.colSpan = cellInfo.colSpan + 1;
  17233. } else {
  17234. tableRow = this.table.rows[rowIndex];
  17235. preCell = tableRow.cells[cellInfo.cellIndex];
  17236. cell = this.cloneCell(sourceCell, true);//tableRow.insertCell(cellInfo.cellIndex);
  17237. this.setCellContent(cell);
  17238. cell.setAttribute('vAlign', cell.getAttribute('vAlign'));
  17239. preCell && cell.setAttribute('width', preCell.getAttribute('width'));
  17240. //防止IE下报错
  17241. preCell ? tableRow.insertBefore(cell, preCell) : tableRow.appendChild(cell);
  17242. }
  17243. if(!isInsertTitleCol) replaceTdToTh(rowIndex, cell, tableRow);
  17244. }
  17245. }
  17246. //框选时插入不触发contentchange,需要手动更新索引
  17247. this.update();
  17248. this.updateWidth(backWidth, defaultValue || {tdPadding:10, tdBorder:1});
  17249. },
  17250. updateWidth:function (width, defaultValue) {
  17251. var table = this.table,
  17252. tmpWidth = UETable.getWidth(table) - defaultValue.tdPadding * 2 - defaultValue.tdBorder + width;
  17253. if (tmpWidth < table.ownerDocument.body.offsetWidth) {
  17254. table.setAttribute("width", tmpWidth);
  17255. return;
  17256. }
  17257. var tds = domUtils.getElementsByTagName(this.table, "td th");
  17258. utils.each(tds, function (td) {
  17259. td.setAttribute("width", width);
  17260. })
  17261. },
  17262. deleteCol:function (colIndex) {
  17263. var indexTable = this.indexTable,
  17264. tableRows = this.table.rows,
  17265. backTableWidth = this.table.getAttribute("width"),
  17266. backTdWidth = 0,
  17267. rowsNum = this.rowsNum,
  17268. cacheMap = {};
  17269. for (var rowIndex = 0; rowIndex < rowsNum;) {
  17270. var infoRow = indexTable[rowIndex],
  17271. cellInfo = infoRow[colIndex],
  17272. key = cellInfo.rowIndex + '_' + cellInfo.colIndex;
  17273. // 跳过已经处理过的Cell
  17274. if (cacheMap[key])continue;
  17275. cacheMap[key] = 1;
  17276. var cell = this.getCell(cellInfo.rowIndex, cellInfo.cellIndex);
  17277. if (!backTdWidth) backTdWidth = cell && parseInt(cell.offsetWidth / cell.colSpan, 10).toFixed(0);
  17278. // 如果Cell的colSpan大于1, 就修改colSpan, 否则就删掉这个Cell
  17279. if (cell.colSpan > 1) {
  17280. cell.colSpan--;
  17281. } else {
  17282. tableRows[rowIndex].deleteCell(cellInfo.cellIndex);
  17283. }
  17284. rowIndex += cellInfo.rowSpan || 1;
  17285. }
  17286. this.table.setAttribute("width", backTableWidth - backTdWidth);
  17287. this.update();
  17288. },
  17289. splitToCells:function (cell) {
  17290. var me = this,
  17291. cells = this.splitToRows(cell);
  17292. utils.each(cells, function (cell) {
  17293. me.splitToCols(cell);
  17294. })
  17295. },
  17296. splitToRows:function (cell) {
  17297. var cellInfo = this.getCellInfo(cell),
  17298. rowIndex = cellInfo.rowIndex,
  17299. colIndex = cellInfo.colIndex,
  17300. results = [];
  17301. // 修改Cell的rowSpan
  17302. cell.rowSpan = 1;
  17303. results.push(cell);
  17304. // 补齐单元格
  17305. for (var i = rowIndex, endRow = rowIndex + cellInfo.rowSpan; i < endRow; i++) {
  17306. if (i == rowIndex)continue;
  17307. var tableRow = this.table.rows[i],
  17308. tmpCell = tableRow.insertCell(colIndex - this.getPreviewMergedCellsNum(i, colIndex));
  17309. tmpCell.colSpan = cellInfo.colSpan;
  17310. this.setCellContent(tmpCell);
  17311. tmpCell.setAttribute('vAlign', cell.getAttribute('vAlign'));
  17312. tmpCell.setAttribute('align', cell.getAttribute('align'));
  17313. if (cell.style.cssText) {
  17314. tmpCell.style.cssText = cell.style.cssText;
  17315. }
  17316. results.push(tmpCell);
  17317. }
  17318. this.update();
  17319. return results;
  17320. },
  17321. getPreviewMergedCellsNum:function (rowIndex, colIndex) {
  17322. var indexRow = this.indexTable[rowIndex],
  17323. num = 0;
  17324. for (var i = 0; i < colIndex;) {
  17325. var colSpan = indexRow[i].colSpan,
  17326. tmpRowIndex = indexRow[i].rowIndex;
  17327. num += (colSpan - (tmpRowIndex == rowIndex ? 1 : 0));
  17328. i += colSpan;
  17329. }
  17330. return num;
  17331. },
  17332. splitToCols:function (cell) {
  17333. var backWidth = (cell.offsetWidth / cell.colSpan - 22).toFixed(0),
  17334. cellInfo = this.getCellInfo(cell),
  17335. rowIndex = cellInfo.rowIndex,
  17336. colIndex = cellInfo.colIndex,
  17337. results = [];
  17338. // 修改Cell的rowSpan
  17339. cell.colSpan = 1;
  17340. cell.setAttribute("width", backWidth);
  17341. results.push(cell);
  17342. // 补齐单元格
  17343. for (var j = colIndex, endCol = colIndex + cellInfo.colSpan; j < endCol; j++) {
  17344. if (j == colIndex)continue;
  17345. var tableRow = this.table.rows[rowIndex],
  17346. tmpCell = tableRow.insertCell(this.indexTable[rowIndex][j].cellIndex + 1);
  17347. tmpCell.rowSpan = cellInfo.rowSpan;
  17348. this.setCellContent(tmpCell);
  17349. tmpCell.setAttribute('vAlign', cell.getAttribute('vAlign'));
  17350. tmpCell.setAttribute('align', cell.getAttribute('align'));
  17351. tmpCell.setAttribute('width', backWidth);
  17352. if (cell.style.cssText) {
  17353. tmpCell.style.cssText = cell.style.cssText;
  17354. }
  17355. //处理th的情况
  17356. if (cell.tagName == 'TH') {
  17357. var th = cell.ownerDocument.createElement('th');
  17358. th.appendChild(tmpCell.firstChild);
  17359. th.setAttribute('vAlign', cell.getAttribute('vAlign'));
  17360. th.rowSpan = tmpCell.rowSpan;
  17361. tableRow.insertBefore(th, tmpCell);
  17362. domUtils.remove(tmpCell);
  17363. }
  17364. results.push(tmpCell);
  17365. }
  17366. this.update();
  17367. return results;
  17368. },
  17369. isLastCell:function (cell, rowsNum, colsNum) {
  17370. rowsNum = rowsNum || this.rowsNum;
  17371. colsNum = colsNum || this.colsNum;
  17372. var cellInfo = this.getCellInfo(cell);
  17373. return ((cellInfo.rowIndex + cellInfo.rowSpan) == rowsNum) &&
  17374. ((cellInfo.colIndex + cellInfo.colSpan) == colsNum);
  17375. },
  17376. getLastCell:function (cells) {
  17377. cells = cells || this.table.getElementsByTagName("td");
  17378. var firstInfo = this.getCellInfo(cells[0]);
  17379. var me = this, last = cells[0],
  17380. tr = last.parentNode,
  17381. cellsNum = 0, cols = 0, rows;
  17382. utils.each(cells, function (cell) {
  17383. if (cell.parentNode == tr)cols += cell.colSpan || 1;
  17384. cellsNum += cell.rowSpan * cell.colSpan || 1;
  17385. });
  17386. rows = cellsNum / cols;
  17387. utils.each(cells, function (cell) {
  17388. if (me.isLastCell(cell, rows, cols)) {
  17389. last = cell;
  17390. return false;
  17391. }
  17392. });
  17393. return last;
  17394. },
  17395. selectRow:function (rowIndex) {
  17396. var indexRow = this.indexTable[rowIndex],
  17397. start = this.getCell(indexRow[0].rowIndex, indexRow[0].cellIndex),
  17398. end = this.getCell(indexRow[this.colsNum - 1].rowIndex, indexRow[this.colsNum - 1].cellIndex),
  17399. range = this.getCellsRange(start, end);
  17400. this.setSelected(range);
  17401. },
  17402. selectTable:function () {
  17403. var tds = this.table.getElementsByTagName("td"),
  17404. range = this.getCellsRange(tds[0], tds[tds.length - 1]);
  17405. this.setSelected(range);
  17406. },
  17407. setBackground:function (cells, value) {
  17408. if (typeof value === "string") {
  17409. utils.each(cells, function (cell) {
  17410. cell.style.backgroundColor = value;
  17411. })
  17412. } else if (typeof value === "object") {
  17413. value = utils.extend({
  17414. repeat:true,
  17415. colorList:["#ddd", "#fff"]
  17416. }, value);
  17417. var rowIndex = this.getCellInfo(cells[0]).rowIndex,
  17418. count = 0,
  17419. colors = value.colorList,
  17420. getColor = function (list, index, repeat) {
  17421. return list[index] ? list[index] : repeat ? list[index % list.length] : "";
  17422. };
  17423. for (var i = 0, cell; cell = cells[i++];) {
  17424. var cellInfo = this.getCellInfo(cell);
  17425. cell.style.backgroundColor = getColor(colors, ((rowIndex + count) == cellInfo.rowIndex) ? count : ++count, value.repeat);
  17426. }
  17427. }
  17428. },
  17429. removeBackground:function (cells) {
  17430. utils.each(cells, function (cell) {
  17431. cell.style.backgroundColor = "";
  17432. })
  17433. }
  17434. };
  17435. function showError(e) {
  17436. }
  17437. })();
  17438. // plugins/table.cmds.js
  17439. /**
  17440. * Created with JetBrains PhpStorm.
  17441. * User: taoqili
  17442. * Date: 13-2-20
  17443. * Time: 下午6:25
  17444. * To change this template use File | Settings | File Templates.
  17445. */
  17446. ;
  17447. (function () {
  17448. var UT = UE.UETable,
  17449. getTableItemsByRange = function (editor) {
  17450. return UT.getTableItemsByRange(editor);
  17451. },
  17452. getUETableBySelected = function (editor) {
  17453. return UT.getUETableBySelected(editor)
  17454. },
  17455. getDefaultValue = function (editor, table) {
  17456. return UT.getDefaultValue(editor, table);
  17457. },
  17458. getUETable = function (tdOrTable) {
  17459. return UT.getUETable(tdOrTable);
  17460. };
  17461. UE.commands['inserttable'] = {
  17462. queryCommandState: function () {
  17463. return getTableItemsByRange(this).table ? -1 : 0;
  17464. },
  17465. execCommand: function (cmd, opt) {
  17466. function createTable(opt, tdWidth) {
  17467. var html = [],
  17468. rowsNum = opt.numRows,
  17469. colsNum = opt.numCols;
  17470. for (var r = 0; r < rowsNum; r++) {
  17471. html.push('<tr' + (r == 0 ? ' class="firstRow"':'') + '>');
  17472. for (var c = 0; c < colsNum; c++) {
  17473. html.push('<td width="' + tdWidth + '" vAlign="' + opt.tdvalign + '" >' + (browser.ie && browser.version < 11 ? domUtils.fillChar : '<br/>') + '</td>')
  17474. }
  17475. html.push('</tr>')
  17476. }
  17477. //禁止指定table-width
  17478. return '<table><tbody>' + html.join('') + '</tbody></table>'
  17479. }
  17480. if (!opt) {
  17481. opt = utils.extend({}, {
  17482. numCols: this.options.defaultCols,
  17483. numRows: this.options.defaultRows,
  17484. tdvalign: this.options.tdvalign
  17485. })
  17486. }
  17487. var me = this;
  17488. var range = this.selection.getRange(),
  17489. start = range.startContainer,
  17490. firstParentBlock = domUtils.findParent(start, function (node) {
  17491. return domUtils.isBlockElm(node);
  17492. }, true) || me.body;
  17493. var defaultValue = getDefaultValue(me),
  17494. tableWidth = firstParentBlock.offsetWidth,
  17495. tdWidth = Math.floor(tableWidth / opt.numCols - defaultValue.tdPadding * 2 - defaultValue.tdBorder);
  17496. //todo其他属性
  17497. !opt.tdvalign && (opt.tdvalign = me.options.tdvalign);
  17498. me.execCommand("inserthtml", createTable(opt, tdWidth));
  17499. }
  17500. };
  17501. UE.commands['insertparagraphbeforetable'] = {
  17502. queryCommandState: function () {
  17503. return getTableItemsByRange(this).cell ? 0 : -1;
  17504. },
  17505. execCommand: function () {
  17506. var table = getTableItemsByRange(this).table;
  17507. if (table) {
  17508. var p = this.document.createElement("p");
  17509. p.innerHTML = browser.ie ? '&nbsp;' : '<br />';
  17510. table.parentNode.insertBefore(p, table);
  17511. this.selection.getRange().setStart(p, 0).setCursor();
  17512. }
  17513. }
  17514. };
  17515. UE.commands['deletetable'] = {
  17516. queryCommandState: function () {
  17517. var rng = this.selection.getRange();
  17518. return domUtils.findParentByTagName(rng.startContainer, 'table', true) ? 0 : -1;
  17519. },
  17520. execCommand: function (cmd, table) {
  17521. var rng = this.selection.getRange();
  17522. table = table || domUtils.findParentByTagName(rng.startContainer, 'table', true);
  17523. if (table) {
  17524. var next = table.nextSibling;
  17525. if (!next) {
  17526. next = domUtils.createElement(this.document, 'p', {
  17527. 'innerHTML': browser.ie ? domUtils.fillChar : '<br/>'
  17528. });
  17529. table.parentNode.insertBefore(next, table);
  17530. }
  17531. domUtils.remove(table);
  17532. rng = this.selection.getRange();
  17533. if (next.nodeType == 3) {
  17534. rng.setStartBefore(next)
  17535. } else {
  17536. rng.setStart(next, 0)
  17537. }
  17538. rng.setCursor(false, true)
  17539. this.fireEvent("tablehasdeleted")
  17540. }
  17541. }
  17542. };
  17543. UE.commands['cellalign'] = {
  17544. queryCommandState: function () {
  17545. return getSelectedArr(this).length ? 0 : -1
  17546. },
  17547. execCommand: function (cmd, align) {
  17548. var selectedTds = getSelectedArr(this);
  17549. if (selectedTds.length) {
  17550. for (var i = 0, ci; ci = selectedTds[i++];) {
  17551. ci.setAttribute('align', align);
  17552. }
  17553. }
  17554. }
  17555. };
  17556. UE.commands['cellvalign'] = {
  17557. queryCommandState: function () {
  17558. return getSelectedArr(this).length ? 0 : -1;
  17559. },
  17560. execCommand: function (cmd, valign) {
  17561. var selectedTds = getSelectedArr(this);
  17562. if (selectedTds.length) {
  17563. for (var i = 0, ci; ci = selectedTds[i++];) {
  17564. ci.setAttribute('vAlign', valign);
  17565. }
  17566. }
  17567. }
  17568. };
  17569. UE.commands['insertcaption'] = {
  17570. queryCommandState: function () {
  17571. var table = getTableItemsByRange(this).table;
  17572. if (table) {
  17573. return table.getElementsByTagName('caption').length == 0 ? 1 : -1;
  17574. }
  17575. return -1;
  17576. },
  17577. execCommand: function () {
  17578. var table = getTableItemsByRange(this).table;
  17579. if (table) {
  17580. var caption = this.document.createElement('caption');
  17581. caption.innerHTML = browser.ie ? domUtils.fillChar : '<br/>';
  17582. table.insertBefore(caption, table.firstChild);
  17583. var range = this.selection.getRange();
  17584. range.setStart(caption, 0).setCursor();
  17585. }
  17586. }
  17587. };
  17588. UE.commands['deletecaption'] = {
  17589. queryCommandState: function () {
  17590. var rng = this.selection.getRange(),
  17591. table = domUtils.findParentByTagName(rng.startContainer, 'table');
  17592. if (table) {
  17593. return table.getElementsByTagName('caption').length == 0 ? -1 : 1;
  17594. }
  17595. return -1;
  17596. },
  17597. execCommand: function () {
  17598. var rng = this.selection.getRange(),
  17599. table = domUtils.findParentByTagName(rng.startContainer, 'table');
  17600. if (table) {
  17601. domUtils.remove(table.getElementsByTagName('caption')[0]);
  17602. var range = this.selection.getRange();
  17603. range.setStart(table.rows[0].cells[0], 0).setCursor();
  17604. }
  17605. }
  17606. };
  17607. UE.commands['inserttitle'] = {
  17608. queryCommandState: function () {
  17609. var table = getTableItemsByRange(this).table;
  17610. if (table) {
  17611. var firstRow = table.rows[0];
  17612. return firstRow.cells[firstRow.cells.length-1].tagName.toLowerCase() != 'th' ? 0 : -1
  17613. }
  17614. return -1;
  17615. },
  17616. execCommand: function () {
  17617. var table = getTableItemsByRange(this).table;
  17618. if (table) {
  17619. getUETable(table).insertRow(0, 'th');
  17620. }
  17621. var th = table.getElementsByTagName('th')[0];
  17622. this.selection.getRange().setStart(th, 0).setCursor(false, true);
  17623. }
  17624. };
  17625. UE.commands['deletetitle'] = {
  17626. queryCommandState: function () {
  17627. var table = getTableItemsByRange(this).table;
  17628. if (table) {
  17629. var firstRow = table.rows[0];
  17630. return firstRow.cells[firstRow.cells.length-1].tagName.toLowerCase() == 'th' ? 0 : -1
  17631. }
  17632. return -1;
  17633. },
  17634. execCommand: function () {
  17635. var table = getTableItemsByRange(this).table;
  17636. if (table) {
  17637. domUtils.remove(table.rows[0])
  17638. }
  17639. var td = table.getElementsByTagName('td')[0];
  17640. this.selection.getRange().setStart(td, 0).setCursor(false, true);
  17641. }
  17642. };
  17643. UE.commands['inserttitlecol'] = {
  17644. queryCommandState: function () {
  17645. var table = getTableItemsByRange(this).table;
  17646. if (table) {
  17647. var lastRow = table.rows[table.rows.length-1];
  17648. return lastRow.getElementsByTagName('th').length ? -1 : 0;
  17649. }
  17650. return -1;
  17651. },
  17652. execCommand: function (cmd) {
  17653. var table = getTableItemsByRange(this).table;
  17654. if (table) {
  17655. getUETable(table).insertCol(0, 'th');
  17656. }
  17657. resetTdWidth(table, this);
  17658. var th = table.getElementsByTagName('th')[0];
  17659. this.selection.getRange().setStart(th, 0).setCursor(false, true);
  17660. }
  17661. };
  17662. UE.commands['deletetitlecol'] = {
  17663. queryCommandState: function () {
  17664. var table = getTableItemsByRange(this).table;
  17665. if (table) {
  17666. var lastRow = table.rows[table.rows.length-1];
  17667. return lastRow.getElementsByTagName('th').length ? 0 : -1;
  17668. }
  17669. return -1;
  17670. },
  17671. execCommand: function () {
  17672. var table = getTableItemsByRange(this).table;
  17673. if (table) {
  17674. for(var i = 0; i< table.rows.length; i++ ){
  17675. domUtils.remove(table.rows[i].children[0])
  17676. }
  17677. }
  17678. resetTdWidth(table, this);
  17679. var td = table.getElementsByTagName('td')[0];
  17680. this.selection.getRange().setStart(td, 0).setCursor(false, true);
  17681. }
  17682. };
  17683. UE.commands["mergeright"] = {
  17684. queryCommandState: function (cmd) {
  17685. var tableItems = getTableItemsByRange(this),
  17686. table = tableItems.table,
  17687. cell = tableItems.cell;
  17688. if (!table || !cell) return -1;
  17689. var ut = getUETable(table);
  17690. if (ut.selectedTds.length) return -1;
  17691. var cellInfo = ut.getCellInfo(cell),
  17692. rightColIndex = cellInfo.colIndex + cellInfo.colSpan;
  17693. if (rightColIndex >= ut.colsNum) return -1; // 如果处于最右边则不能向右合并
  17694. var rightCellInfo = ut.indexTable[cellInfo.rowIndex][rightColIndex],
  17695. rightCell = table.rows[rightCellInfo.rowIndex].cells[rightCellInfo.cellIndex];
  17696. if (!rightCell || cell.tagName != rightCell.tagName) return -1; // TH和TD不能相互合并
  17697. // 当且仅当两个Cell的开始列号和结束列号一致时能进行合并
  17698. return (rightCellInfo.rowIndex == cellInfo.rowIndex && rightCellInfo.rowSpan == cellInfo.rowSpan) ? 0 : -1;
  17699. },
  17700. execCommand: function (cmd) {
  17701. var rng = this.selection.getRange(),
  17702. bk = rng.createBookmark(true);
  17703. var cell = getTableItemsByRange(this).cell,
  17704. ut = getUETable(cell);
  17705. ut.mergeRight(cell);
  17706. rng.moveToBookmark(bk).select();
  17707. }
  17708. };
  17709. UE.commands["mergedown"] = {
  17710. queryCommandState: function (cmd) {
  17711. var tableItems = getTableItemsByRange(this),
  17712. table = tableItems.table,
  17713. cell = tableItems.cell;
  17714. if (!table || !cell) return -1;
  17715. var ut = getUETable(table);
  17716. if (ut.selectedTds.length)return -1;
  17717. var cellInfo = ut.getCellInfo(cell),
  17718. downRowIndex = cellInfo.rowIndex + cellInfo.rowSpan;
  17719. if (downRowIndex >= ut.rowsNum) return -1; // 如果处于最下边则不能向下合并
  17720. var downCellInfo = ut.indexTable[downRowIndex][cellInfo.colIndex],
  17721. downCell = table.rows[downCellInfo.rowIndex].cells[downCellInfo.cellIndex];
  17722. if (!downCell || cell.tagName != downCell.tagName) return -1; // TH和TD不能相互合并
  17723. // 当且仅当两个Cell的开始列号和结束列号一致时能进行合并
  17724. return (downCellInfo.colIndex == cellInfo.colIndex && downCellInfo.colSpan == cellInfo.colSpan) ? 0 : -1;
  17725. },
  17726. execCommand: function () {
  17727. var rng = this.selection.getRange(),
  17728. bk = rng.createBookmark(true);
  17729. var cell = getTableItemsByRange(this).cell,
  17730. ut = getUETable(cell);
  17731. ut.mergeDown(cell);
  17732. rng.moveToBookmark(bk).select();
  17733. }
  17734. };
  17735. UE.commands["mergecells"] = {
  17736. queryCommandState: function () {
  17737. return getUETableBySelected(this) ? 0 : -1;
  17738. },
  17739. execCommand: function () {
  17740. var ut = getUETableBySelected(this);
  17741. if (ut && ut.selectedTds.length) {
  17742. var cell = ut.selectedTds[0];
  17743. ut.mergeRange();
  17744. var rng = this.selection.getRange();
  17745. if (domUtils.isEmptyBlock(cell)) {
  17746. rng.setStart(cell, 0).collapse(true)
  17747. } else {
  17748. rng.selectNodeContents(cell)
  17749. }
  17750. rng.select();
  17751. }
  17752. }
  17753. };
  17754. UE.commands["insertrow"] = {
  17755. queryCommandState: function () {
  17756. var tableItems = getTableItemsByRange(this),
  17757. cell = tableItems.cell;
  17758. return cell && (cell.tagName == "TD" || (cell.tagName == 'TH' && tableItems.tr !== tableItems.table.rows[0])) &&
  17759. getUETable(tableItems.table).rowsNum < this.options.maxRowNum ? 0 : -1;
  17760. },
  17761. execCommand: function () {
  17762. var rng = this.selection.getRange(),
  17763. bk = rng.createBookmark(true);
  17764. var tableItems = getTableItemsByRange(this),
  17765. cell = tableItems.cell,
  17766. table = tableItems.table,
  17767. ut = getUETable(table),
  17768. cellInfo = ut.getCellInfo(cell);
  17769. //ut.insertRow(!ut.selectedTds.length ? cellInfo.rowIndex:ut.cellsRange.beginRowIndex,'');
  17770. if (!ut.selectedTds.length) {
  17771. ut.insertRow(cellInfo.rowIndex, cell);
  17772. } else {
  17773. var range = ut.cellsRange;
  17774. for (var i = 0, len = range.endRowIndex - range.beginRowIndex + 1; i < len; i++) {
  17775. ut.insertRow(range.beginRowIndex, cell);
  17776. }
  17777. }
  17778. rng.moveToBookmark(bk).select();
  17779. if (table.getAttribute("interlaced") === "enabled")this.fireEvent("interlacetable", table);
  17780. }
  17781. };
  17782. //后插入行
  17783. UE.commands["insertrownext"] = {
  17784. queryCommandState: function () {
  17785. var tableItems = getTableItemsByRange(this),
  17786. cell = tableItems.cell;
  17787. return cell && (cell.tagName == "TD") && getUETable(tableItems.table).rowsNum < this.options.maxRowNum ? 0 : -1;
  17788. },
  17789. execCommand: function () {
  17790. var rng = this.selection.getRange(),
  17791. bk = rng.createBookmark(true);
  17792. var tableItems = getTableItemsByRange(this),
  17793. cell = tableItems.cell,
  17794. table = tableItems.table,
  17795. ut = getUETable(table),
  17796. cellInfo = ut.getCellInfo(cell);
  17797. //ut.insertRow(!ut.selectedTds.length? cellInfo.rowIndex + cellInfo.rowSpan : ut.cellsRange.endRowIndex + 1,'');
  17798. if (!ut.selectedTds.length) {
  17799. ut.insertRow(cellInfo.rowIndex + cellInfo.rowSpan, cell);
  17800. } else {
  17801. var range = ut.cellsRange;
  17802. for (var i = 0, len = range.endRowIndex - range.beginRowIndex + 1; i < len; i++) {
  17803. ut.insertRow(range.endRowIndex + 1, cell);
  17804. }
  17805. }
  17806. rng.moveToBookmark(bk).select();
  17807. if (table.getAttribute("interlaced") === "enabled")this.fireEvent("interlacetable", table);
  17808. }
  17809. };
  17810. UE.commands["deleterow"] = {
  17811. queryCommandState: function () {
  17812. var tableItems = getTableItemsByRange(this);
  17813. return tableItems.cell ? 0 : -1;
  17814. },
  17815. execCommand: function () {
  17816. var cell = getTableItemsByRange(this).cell,
  17817. ut = getUETable(cell),
  17818. cellsRange = ut.cellsRange,
  17819. cellInfo = ut.getCellInfo(cell),
  17820. preCell = ut.getVSideCell(cell),
  17821. nextCell = ut.getVSideCell(cell, true),
  17822. rng = this.selection.getRange();
  17823. if (utils.isEmptyObject(cellsRange)) {
  17824. ut.deleteRow(cellInfo.rowIndex);
  17825. } else {
  17826. for (var i = cellsRange.beginRowIndex; i < cellsRange.endRowIndex + 1; i++) {
  17827. ut.deleteRow(cellsRange.beginRowIndex);
  17828. }
  17829. }
  17830. var table = ut.table;
  17831. if (!table.getElementsByTagName('td').length) {
  17832. var nextSibling = table.nextSibling;
  17833. domUtils.remove(table);
  17834. if (nextSibling) {
  17835. rng.setStart(nextSibling, 0).setCursor(false, true);
  17836. }
  17837. } else {
  17838. if (cellInfo.rowSpan == 1 || cellInfo.rowSpan == cellsRange.endRowIndex - cellsRange.beginRowIndex + 1) {
  17839. if (nextCell || preCell) rng.selectNodeContents(nextCell || preCell).setCursor(false, true);
  17840. } else {
  17841. var newCell = ut.getCell(cellInfo.rowIndex, ut.indexTable[cellInfo.rowIndex][cellInfo.colIndex].cellIndex);
  17842. if (newCell) rng.selectNodeContents(newCell).setCursor(false, true);
  17843. }
  17844. }
  17845. if (table.getAttribute("interlaced") === "enabled")this.fireEvent("interlacetable", table);
  17846. }
  17847. };
  17848. UE.commands["insertcol"] = {
  17849. queryCommandState: function (cmd) {
  17850. var tableItems = getTableItemsByRange(this),
  17851. cell = tableItems.cell;
  17852. return cell && (cell.tagName == "TD" || (cell.tagName == 'TH' && cell !== tableItems.tr.cells[0])) &&
  17853. getUETable(tableItems.table).colsNum < this.options.maxColNum ? 0 : -1;
  17854. },
  17855. execCommand: function (cmd) {
  17856. var rng = this.selection.getRange(),
  17857. bk = rng.createBookmark(true);
  17858. if (this.queryCommandState(cmd) == -1)return;
  17859. var cell = getTableItemsByRange(this).cell,
  17860. ut = getUETable(cell),
  17861. cellInfo = ut.getCellInfo(cell);
  17862. //ut.insertCol(!ut.selectedTds.length ? cellInfo.colIndex:ut.cellsRange.beginColIndex);
  17863. if (!ut.selectedTds.length) {
  17864. ut.insertCol(cellInfo.colIndex, cell);
  17865. } else {
  17866. var range = ut.cellsRange;
  17867. for (var i = 0, len = range.endColIndex - range.beginColIndex + 1; i < len; i++) {
  17868. ut.insertCol(range.beginColIndex, cell);
  17869. }
  17870. }
  17871. rng.moveToBookmark(bk).select(true);
  17872. }
  17873. };
  17874. UE.commands["insertcolnext"] = {
  17875. queryCommandState: function () {
  17876. var tableItems = getTableItemsByRange(this),
  17877. cell = tableItems.cell;
  17878. return cell && getUETable(tableItems.table).colsNum < this.options.maxColNum ? 0 : -1;
  17879. },
  17880. execCommand: function () {
  17881. var rng = this.selection.getRange(),
  17882. bk = rng.createBookmark(true);
  17883. var cell = getTableItemsByRange(this).cell,
  17884. ut = getUETable(cell),
  17885. cellInfo = ut.getCellInfo(cell);
  17886. //ut.insertCol(!ut.selectedTds.length ? cellInfo.colIndex + cellInfo.colSpan:ut.cellsRange.endColIndex +1);
  17887. if (!ut.selectedTds.length) {
  17888. ut.insertCol(cellInfo.colIndex + cellInfo.colSpan, cell);
  17889. } else {
  17890. var range = ut.cellsRange;
  17891. for (var i = 0, len = range.endColIndex - range.beginColIndex + 1; i < len; i++) {
  17892. ut.insertCol(range.endColIndex + 1, cell);
  17893. }
  17894. }
  17895. rng.moveToBookmark(bk).select();
  17896. }
  17897. };
  17898. UE.commands["deletecol"] = {
  17899. queryCommandState: function () {
  17900. var tableItems = getTableItemsByRange(this);
  17901. return tableItems.cell ? 0 : -1;
  17902. },
  17903. execCommand: function () {
  17904. var cell = getTableItemsByRange(this).cell,
  17905. ut = getUETable(cell),
  17906. range = ut.cellsRange,
  17907. cellInfo = ut.getCellInfo(cell),
  17908. preCell = ut.getHSideCell(cell),
  17909. nextCell = ut.getHSideCell(cell, true);
  17910. if (utils.isEmptyObject(range)) {
  17911. ut.deleteCol(cellInfo.colIndex);
  17912. } else {
  17913. for (var i = range.beginColIndex; i < range.endColIndex + 1; i++) {
  17914. ut.deleteCol(range.beginColIndex);
  17915. }
  17916. }
  17917. var table = ut.table,
  17918. rng = this.selection.getRange();
  17919. if (!table.getElementsByTagName('td').length) {
  17920. var nextSibling = table.nextSibling;
  17921. domUtils.remove(table);
  17922. if (nextSibling) {
  17923. rng.setStart(nextSibling, 0).setCursor(false, true);
  17924. }
  17925. } else {
  17926. if (domUtils.inDoc(cell, this.document)) {
  17927. rng.setStart(cell, 0).setCursor(false, true);
  17928. } else {
  17929. if (nextCell && domUtils.inDoc(nextCell, this.document)) {
  17930. rng.selectNodeContents(nextCell).setCursor(false, true);
  17931. } else {
  17932. if (preCell && domUtils.inDoc(preCell, this.document)) {
  17933. rng.selectNodeContents(preCell).setCursor(true, true);
  17934. }
  17935. }
  17936. }
  17937. }
  17938. }
  17939. };
  17940. UE.commands["splittocells"] = {
  17941. queryCommandState: function () {
  17942. var tableItems = getTableItemsByRange(this),
  17943. cell = tableItems.cell;
  17944. if (!cell) return -1;
  17945. var ut = getUETable(tableItems.table);
  17946. if (ut.selectedTds.length > 0) return -1;
  17947. return cell && (cell.colSpan > 1 || cell.rowSpan > 1) ? 0 : -1;
  17948. },
  17949. execCommand: function () {
  17950. var rng = this.selection.getRange(),
  17951. bk = rng.createBookmark(true);
  17952. var cell = getTableItemsByRange(this).cell,
  17953. ut = getUETable(cell);
  17954. ut.splitToCells(cell);
  17955. rng.moveToBookmark(bk).select();
  17956. }
  17957. };
  17958. UE.commands["splittorows"] = {
  17959. queryCommandState: function () {
  17960. var tableItems = getTableItemsByRange(this),
  17961. cell = tableItems.cell;
  17962. if (!cell) return -1;
  17963. var ut = getUETable(tableItems.table);
  17964. if (ut.selectedTds.length > 0) return -1;
  17965. return cell && cell.rowSpan > 1 ? 0 : -1;
  17966. },
  17967. execCommand: function () {
  17968. var rng = this.selection.getRange(),
  17969. bk = rng.createBookmark(true);
  17970. var cell = getTableItemsByRange(this).cell,
  17971. ut = getUETable(cell);
  17972. ut.splitToRows(cell);
  17973. rng.moveToBookmark(bk).select();
  17974. }
  17975. };
  17976. UE.commands["splittocols"] = {
  17977. queryCommandState: function () {
  17978. var tableItems = getTableItemsByRange(this),
  17979. cell = tableItems.cell;
  17980. if (!cell) return -1;
  17981. var ut = getUETable(tableItems.table);
  17982. if (ut.selectedTds.length > 0) return -1;
  17983. return cell && cell.colSpan > 1 ? 0 : -1;
  17984. },
  17985. execCommand: function () {
  17986. var rng = this.selection.getRange(),
  17987. bk = rng.createBookmark(true);
  17988. var cell = getTableItemsByRange(this).cell,
  17989. ut = getUETable(cell);
  17990. ut.splitToCols(cell);
  17991. rng.moveToBookmark(bk).select();
  17992. }
  17993. };
  17994. UE.commands["adaptbytext"] =
  17995. UE.commands["adaptbywindow"] = {
  17996. queryCommandState: function () {
  17997. return getTableItemsByRange(this).table ? 0 : -1
  17998. },
  17999. execCommand: function (cmd) {
  18000. var tableItems = getTableItemsByRange(this),
  18001. table = tableItems.table;
  18002. if (table) {
  18003. if (cmd == 'adaptbywindow') {
  18004. resetTdWidth(table, this);
  18005. } else {
  18006. var cells = domUtils.getElementsByTagName(table, "td th");
  18007. utils.each(cells, function (cell) {
  18008. cell.removeAttribute("width");
  18009. });
  18010. table.removeAttribute("width");
  18011. }
  18012. }
  18013. }
  18014. };
  18015. //平均分配各列
  18016. UE.commands['averagedistributecol'] = {
  18017. queryCommandState: function () {
  18018. var ut = getUETableBySelected(this);
  18019. if (!ut) return -1;
  18020. return ut.isFullRow() || ut.isFullCol() ? 0 : -1;
  18021. },
  18022. execCommand: function (cmd) {
  18023. var me = this,
  18024. ut = getUETableBySelected(me);
  18025. function getAverageWidth() {
  18026. var tb = ut.table,
  18027. averageWidth, sumWidth = 0, colsNum = 0,
  18028. tbAttr = getDefaultValue(me, tb);
  18029. if (ut.isFullRow()) {
  18030. sumWidth = tb.offsetWidth;
  18031. colsNum = ut.colsNum;
  18032. } else {
  18033. var begin = ut.cellsRange.beginColIndex,
  18034. end = ut.cellsRange.endColIndex,
  18035. node;
  18036. for (var i = begin; i <= end;) {
  18037. node = ut.selectedTds[i];
  18038. sumWidth += node.offsetWidth;
  18039. i += node.colSpan;
  18040. colsNum += 1;
  18041. }
  18042. }
  18043. averageWidth = Math.ceil(sumWidth / colsNum) - tbAttr.tdBorder * 2 - tbAttr.tdPadding * 2;
  18044. return averageWidth;
  18045. }
  18046. function setAverageWidth(averageWidth) {
  18047. utils.each(domUtils.getElementsByTagName(ut.table, "th"), function (node) {
  18048. node.setAttribute("width", "");
  18049. });
  18050. var cells = ut.isFullRow() ? domUtils.getElementsByTagName(ut.table, "td") : ut.selectedTds;
  18051. utils.each(cells, function (node) {
  18052. if (node.colSpan == 1) {
  18053. node.setAttribute("width", averageWidth);
  18054. }
  18055. });
  18056. }
  18057. if (ut && ut.selectedTds.length) {
  18058. setAverageWidth(getAverageWidth());
  18059. }
  18060. }
  18061. };
  18062. //平均分配各行
  18063. UE.commands['averagedistributerow'] = {
  18064. queryCommandState: function () {
  18065. var ut = getUETableBySelected(this);
  18066. if (!ut) return -1;
  18067. if (ut.selectedTds && /th/ig.test(ut.selectedTds[0].tagName)) return -1;
  18068. return ut.isFullRow() || ut.isFullCol() ? 0 : -1;
  18069. },
  18070. execCommand: function (cmd) {
  18071. var me = this,
  18072. ut = getUETableBySelected(me);
  18073. function getAverageHeight() {
  18074. var averageHeight, rowNum, sumHeight = 0,
  18075. tb = ut.table,
  18076. tbAttr = getDefaultValue(me, tb),
  18077. tdpadding = parseInt(domUtils.getComputedStyle(tb.getElementsByTagName('td')[0], "padding-top"));
  18078. if (ut.isFullCol()) {
  18079. var captionArr = domUtils.getElementsByTagName(tb, "caption"),
  18080. thArr = domUtils.getElementsByTagName(tb, "th"),
  18081. captionHeight, thHeight;
  18082. if (captionArr.length > 0) {
  18083. captionHeight = captionArr[0].offsetHeight;
  18084. }
  18085. if (thArr.length > 0) {
  18086. thHeight = thArr[0].offsetHeight;
  18087. }
  18088. sumHeight = tb.offsetHeight - (captionHeight || 0) - (thHeight || 0);
  18089. rowNum = thArr.length == 0 ? ut.rowsNum : (ut.rowsNum - 1);
  18090. } else {
  18091. var begin = ut.cellsRange.beginRowIndex,
  18092. end = ut.cellsRange.endRowIndex,
  18093. count = 0,
  18094. trs = domUtils.getElementsByTagName(tb, "tr");
  18095. for (var i = begin; i <= end; i++) {
  18096. sumHeight += trs[i].offsetHeight;
  18097. count += 1;
  18098. }
  18099. rowNum = count;
  18100. }
  18101. //ie8下是混杂模式
  18102. if (browser.ie && browser.version < 9) {
  18103. averageHeight = Math.ceil(sumHeight / rowNum);
  18104. } else {
  18105. averageHeight = Math.ceil(sumHeight / rowNum) - tbAttr.tdBorder * 2 - tdpadding * 2;
  18106. }
  18107. return averageHeight;
  18108. }
  18109. function setAverageHeight(averageHeight) {
  18110. var cells = ut.isFullCol() ? domUtils.getElementsByTagName(ut.table, "td") : ut.selectedTds;
  18111. utils.each(cells, function (node) {
  18112. if (node.rowSpan == 1) {
  18113. node.setAttribute("height", averageHeight);
  18114. }
  18115. });
  18116. }
  18117. if (ut && ut.selectedTds.length) {
  18118. setAverageHeight(getAverageHeight());
  18119. }
  18120. }
  18121. };
  18122. //单元格对齐方式
  18123. UE.commands['cellalignment'] = {
  18124. queryCommandState: function () {
  18125. return getTableItemsByRange(this).table ? 0 : -1
  18126. },
  18127. execCommand: function (cmd, data) {
  18128. var me = this,
  18129. ut = getUETableBySelected(me);
  18130. if (!ut) {
  18131. var start = me.selection.getStart(),
  18132. cell = start && domUtils.findParentByTagName(start, ["td", "th", "caption"], true);
  18133. if (!/caption/ig.test(cell.tagName)) {
  18134. domUtils.setAttributes(cell, data);
  18135. } else {
  18136. cell.style.textAlign = data.align;
  18137. cell.style.verticalAlign = data.vAlign;
  18138. }
  18139. me.selection.getRange().setCursor(true);
  18140. } else {
  18141. utils.each(ut.selectedTds, function (cell) {
  18142. domUtils.setAttributes(cell, data);
  18143. });
  18144. }
  18145. },
  18146. /**
  18147. * 查询当前点击的单元格的对齐状态, 如果当前已经选择了多个单元格, 则会返回所有单元格经过统一协调过后的状态
  18148. * @see UE.UETable.getTableCellAlignState
  18149. */
  18150. queryCommandValue: function (cmd) {
  18151. var activeMenuCell = getTableItemsByRange( this).cell;
  18152. if( !activeMenuCell ) {
  18153. activeMenuCell = getSelectedArr(this)[0];
  18154. }
  18155. if (!activeMenuCell) {
  18156. return null;
  18157. } else {
  18158. //获取同时选中的其他单元格
  18159. var cells = UE.UETable.getUETable(activeMenuCell).selectedTds;
  18160. !cells.length && ( cells = activeMenuCell );
  18161. return UE.UETable.getTableCellAlignState(cells);
  18162. }
  18163. }
  18164. };
  18165. //表格对齐方式
  18166. UE.commands['tablealignment'] = {
  18167. queryCommandState: function () {
  18168. if (browser.ie && browser.version < 8) {
  18169. return -1;
  18170. }
  18171. return getTableItemsByRange(this).table ? 0 : -1
  18172. },
  18173. execCommand: function (cmd, value) {
  18174. var me = this,
  18175. start = me.selection.getStart(),
  18176. table = start && domUtils.findParentByTagName(start, ["table"], true);
  18177. if (table) {
  18178. table.setAttribute("align",value);
  18179. }
  18180. }
  18181. };
  18182. //表格属性
  18183. UE.commands['edittable'] = {
  18184. queryCommandState: function () {
  18185. return getTableItemsByRange(this).table ? 0 : -1
  18186. },
  18187. execCommand: function (cmd, color) {
  18188. var rng = this.selection.getRange(),
  18189. table = domUtils.findParentByTagName(rng.startContainer, 'table');
  18190. if (table) {
  18191. var arr = domUtils.getElementsByTagName(table, "td").concat(
  18192. domUtils.getElementsByTagName(table, "th"),
  18193. domUtils.getElementsByTagName(table, "caption")
  18194. );
  18195. utils.each(arr, function (node) {
  18196. node.style.borderColor = color;
  18197. });
  18198. }
  18199. }
  18200. };
  18201. //单元格属性
  18202. UE.commands['edittd'] = {
  18203. queryCommandState: function () {
  18204. return getTableItemsByRange(this).table ? 0 : -1
  18205. },
  18206. execCommand: function (cmd, bkColor) {
  18207. var me = this,
  18208. ut = getUETableBySelected(me);
  18209. if (!ut) {
  18210. var start = me.selection.getStart(),
  18211. cell = start && domUtils.findParentByTagName(start, ["td", "th", "caption"], true);
  18212. if (cell) {
  18213. cell.style.backgroundColor = bkColor;
  18214. }
  18215. } else {
  18216. utils.each(ut.selectedTds, function (cell) {
  18217. cell.style.backgroundColor = bkColor;
  18218. });
  18219. }
  18220. }
  18221. };
  18222. UE.commands["settablebackground"] = {
  18223. queryCommandState: function () {
  18224. return getSelectedArr(this).length > 1 ? 0 : -1;
  18225. },
  18226. execCommand: function (cmd, value) {
  18227. var cells, ut;
  18228. cells = getSelectedArr(this);
  18229. ut = getUETable(cells[0]);
  18230. ut.setBackground(cells, value);
  18231. }
  18232. };
  18233. UE.commands["cleartablebackground"] = {
  18234. queryCommandState: function () {
  18235. var cells = getSelectedArr(this);
  18236. if (!cells.length)return -1;
  18237. for (var i = 0, cell; cell = cells[i++];) {
  18238. if (cell.style.backgroundColor !== "") return 0;
  18239. }
  18240. return -1;
  18241. },
  18242. execCommand: function () {
  18243. var cells = getSelectedArr(this),
  18244. ut = getUETable(cells[0]);
  18245. ut.removeBackground(cells);
  18246. }
  18247. };
  18248. UE.commands["interlacetable"] = UE.commands["uninterlacetable"] = {
  18249. queryCommandState: function (cmd) {
  18250. var table = getTableItemsByRange(this).table;
  18251. if (!table) return -1;
  18252. var interlaced = table.getAttribute("interlaced");
  18253. if (cmd == "interlacetable") {
  18254. //TODO 待定
  18255. //是否需要待定,如果设置,则命令只能单次执行成功,但反射具备toggle效果;否则可以覆盖前次命令,但反射将不存在toggle效果
  18256. return (interlaced === "enabled") ? -1 : 0;
  18257. } else {
  18258. return (!interlaced || interlaced === "disabled") ? -1 : 0;
  18259. }
  18260. },
  18261. execCommand: function (cmd, classList) {
  18262. var table = getTableItemsByRange(this).table;
  18263. if (cmd == "interlacetable") {
  18264. table.setAttribute("interlaced", "enabled");
  18265. this.fireEvent("interlacetable", table, classList);
  18266. } else {
  18267. table.setAttribute("interlaced", "disabled");
  18268. this.fireEvent("uninterlacetable", table);
  18269. }
  18270. }
  18271. };
  18272. UE.commands["setbordervisible"] = {
  18273. queryCommandState: function (cmd) {
  18274. var table = getTableItemsByRange(this).table;
  18275. if (!table) return -1;
  18276. return 0;
  18277. },
  18278. execCommand: function () {
  18279. var table = getTableItemsByRange(this).table;
  18280. utils.each(domUtils.getElementsByTagName(table,'td'),function(td){
  18281. td.style.borderWidth = '1px';
  18282. td.style.borderStyle = 'solid';
  18283. })
  18284. }
  18285. };
  18286. function resetTdWidth(table, editor) {
  18287. var tds = domUtils.getElementsByTagName(table,'td th');
  18288. utils.each(tds, function (td) {
  18289. td.removeAttribute("width");
  18290. });
  18291. table.setAttribute('width', getTableWidth(editor, true, getDefaultValue(editor, table)));
  18292. var tdsWidths = [];
  18293. setTimeout(function () {
  18294. utils.each(tds, function (td) {
  18295. (td.colSpan == 1) && tdsWidths.push(td.offsetWidth)
  18296. })
  18297. utils.each(tds, function (td,i) {
  18298. (td.colSpan == 1) && td.setAttribute("width", tdsWidths[i] + "");
  18299. })
  18300. }, 0);
  18301. }
  18302. function getTableWidth(editor, needIEHack, defaultValue) {
  18303. var body = editor.body;
  18304. return body.offsetWidth - (needIEHack ? parseInt(domUtils.getComputedStyle(body, 'margin-left'), 10) * 2 : 0) - defaultValue.tableBorder * 2 - (editor.options.offsetWidth || 0);
  18305. }
  18306. function getSelectedArr(editor) {
  18307. var cell = getTableItemsByRange(editor).cell;
  18308. if (cell) {
  18309. var ut = getUETable(cell);
  18310. return ut.selectedTds.length ? ut.selectedTds : [cell];
  18311. } else {
  18312. return [];
  18313. }
  18314. }
  18315. })();
  18316. // plugins/table.action.js
  18317. /**
  18318. * Created with JetBrains PhpStorm.
  18319. * User: taoqili
  18320. * Date: 12-10-12
  18321. * Time: 上午10:05
  18322. * To change this template use File | Settings | File Templates.
  18323. */
  18324. UE.plugins['table'] = function () {
  18325. var me = this,
  18326. tabTimer = null,
  18327. //拖动计时器
  18328. tableDragTimer = null,
  18329. //双击计时器
  18330. tableResizeTimer = null,
  18331. //单元格最小宽度
  18332. cellMinWidth = 5,
  18333. isInResizeBuffer = false,
  18334. //单元格边框大小
  18335. cellBorderWidth = 5,
  18336. //鼠标偏移距离
  18337. offsetOfTableCell = 10,
  18338. //记录在有限时间内的点击状态, 共有3个取值, 0, 1, 2。 0代表未初始化, 1代表单击了1次,2代表2次
  18339. singleClickState = 0,
  18340. userActionStatus = null,
  18341. //双击允许的时间范围
  18342. dblclickTime = 360,
  18343. UT = UE.UETable,
  18344. getUETable = function (tdOrTable) {
  18345. return UT.getUETable(tdOrTable);
  18346. },
  18347. getUETableBySelected = function (editor) {
  18348. return UT.getUETableBySelected(editor);
  18349. },
  18350. getDefaultValue = function (editor, table) {
  18351. return UT.getDefaultValue(editor, table);
  18352. },
  18353. removeSelectedClass = function (cells) {
  18354. return UT.removeSelectedClass(cells);
  18355. };
  18356. function showError(e) {
  18357. // throw e;
  18358. }
  18359. me.ready(function(){
  18360. var me = this;
  18361. var orgGetText = me.selection.getText;
  18362. me.selection.getText = function(){
  18363. var table = getUETableBySelected(me);
  18364. if(table){
  18365. var str = '';
  18366. utils.each(table.selectedTds,function(td){
  18367. str += td[browser.ie?'innerText':'textContent'];
  18368. })
  18369. return str;
  18370. }else{
  18371. return orgGetText.call(me.selection)
  18372. }
  18373. }
  18374. })
  18375. //处理拖动及框选相关方法
  18376. var startTd = null, //鼠标按下时的锚点td
  18377. currentTd = null, //当前鼠标经过时的td
  18378. onDrag = "", //指示当前拖动状态,其值可为"","h","v" ,分别表示未拖动状态,横向拖动状态,纵向拖动状态,用于鼠标移动过程中的判断
  18379. onBorder = false, //检测鼠标按下时是否处在单元格边缘位置
  18380. dragButton = null,
  18381. dragOver = false,
  18382. dragLine = null, //模拟的拖动线
  18383. dragTd = null; //发生拖动的目标td
  18384. var mousedown = false,
  18385. //todo 判断混乱模式
  18386. needIEHack = true;
  18387. me.setOpt({
  18388. 'maxColNum':20,
  18389. 'maxRowNum':100,
  18390. 'defaultCols':5,
  18391. 'defaultRows':5,
  18392. 'tdvalign':'top',
  18393. 'cursorpath':me.options.UEDITOR_HOME_URL + "themes/default/images/cursor_",
  18394. 'tableDragable':false,
  18395. 'classList':["ue-table-interlace-color-single","ue-table-interlace-color-double"]
  18396. });
  18397. me.getUETable = getUETable;
  18398. var commands = {
  18399. 'deletetable':1,
  18400. 'inserttable':1,
  18401. 'cellvalign':1,
  18402. 'insertcaption':1,
  18403. 'deletecaption':1,
  18404. 'inserttitle':1,
  18405. 'deletetitle':1,
  18406. "mergeright":1,
  18407. "mergedown":1,
  18408. "mergecells":1,
  18409. "insertrow":1,
  18410. "insertrownext":1,
  18411. "deleterow":1,
  18412. "insertcol":1,
  18413. "insertcolnext":1,
  18414. "deletecol":1,
  18415. "splittocells":1,
  18416. "splittorows":1,
  18417. "splittocols":1,
  18418. "adaptbytext":1,
  18419. "adaptbywindow":1,
  18420. "adaptbycustomer":1,
  18421. "insertparagraph":1,
  18422. "insertparagraphbeforetable":1,
  18423. "averagedistributecol":1,
  18424. "averagedistributerow":1
  18425. };
  18426. me.ready(function () {
  18427. utils.cssRule('table',
  18428. //选中的td上的样式
  18429. '.selectTdClass{background-color:#edf5fa !important}' +
  18430. 'table.noBorderTable td,table.noBorderTable th,table.noBorderTable caption{border:1px dashed #ddd !important}' +
  18431. //插入的表格的默认样式
  18432. 'table{margin-bottom:10px;border-collapse:collapse;display:table;}' +
  18433. 'td,th{padding: 5px 10px;border: 1px solid #DDD;}' +
  18434. 'caption{border:1px dashed #DDD;border-bottom:0;padding:3px;text-align:center;}' +
  18435. 'th{border-top:1px solid #BBB;background-color:#F7F7F7;}' +
  18436. 'table tr.firstRow th{border-top-width:2px;}' +
  18437. '.ue-table-interlace-color-single{ background-color: #fcfcfc; } .ue-table-interlace-color-double{ background-color: #f7faff; }' +
  18438. 'td p{margin:0;padding:0;}', me.document);
  18439. var tableCopyList, isFullCol, isFullRow;
  18440. //注册del/backspace事件
  18441. me.addListener('keydown', function (cmd, evt) {
  18442. var me = this;
  18443. var keyCode = evt.keyCode || evt.which;
  18444. if (keyCode == 8) {
  18445. var ut = getUETableBySelected(me);
  18446. if (ut && ut.selectedTds.length) {
  18447. if (ut.isFullCol()) {
  18448. me.execCommand('deletecol')
  18449. } else if (ut.isFullRow()) {
  18450. me.execCommand('deleterow')
  18451. } else {
  18452. me.fireEvent('delcells');
  18453. }
  18454. domUtils.preventDefault(evt);
  18455. }
  18456. var caption = domUtils.findParentByTagName(me.selection.getStart(), 'caption', true),
  18457. range = me.selection.getRange();
  18458. if (range.collapsed && caption && isEmptyBlock(caption)) {
  18459. me.fireEvent('saveScene');
  18460. var table = caption.parentNode;
  18461. domUtils.remove(caption);
  18462. if (table) {
  18463. range.setStart(table.rows[0].cells[0], 0).setCursor(false, true);
  18464. }
  18465. me.fireEvent('saveScene');
  18466. }
  18467. }
  18468. if (keyCode == 46) {
  18469. ut = getUETableBySelected(me);
  18470. if (ut) {
  18471. me.fireEvent('saveScene');
  18472. for (var i = 0, ci; ci = ut.selectedTds[i++];) {
  18473. domUtils.fillNode(me.document, ci)
  18474. }
  18475. me.fireEvent('saveScene');
  18476. domUtils.preventDefault(evt);
  18477. }
  18478. }
  18479. if (keyCode == 13) {
  18480. var rng = me.selection.getRange(),
  18481. caption = domUtils.findParentByTagName(rng.startContainer, 'caption', true);
  18482. if (caption) {
  18483. var table = domUtils.findParentByTagName(caption, 'table');
  18484. if (!rng.collapsed) {
  18485. rng.deleteContents();
  18486. me.fireEvent('saveScene');
  18487. } else {
  18488. if (caption) {
  18489. rng.setStart(table.rows[0].cells[0], 0).setCursor(false, true);
  18490. }
  18491. }
  18492. domUtils.preventDefault(evt);
  18493. return;
  18494. }
  18495. if (rng.collapsed) {
  18496. var table = domUtils.findParentByTagName(rng.startContainer, 'table');
  18497. if (table) {
  18498. var cell = table.rows[0].cells[0],
  18499. start = domUtils.findParentByTagName(me.selection.getStart(), ['td', 'th'], true),
  18500. preNode = table.previousSibling;
  18501. if (cell === start && (!preNode || preNode.nodeType == 1 && preNode.tagName == 'TABLE' ) && domUtils.isStartInblock(rng)) {
  18502. var first = domUtils.findParent(me.selection.getStart(), function(n){return domUtils.isBlockElm(n)}, true);
  18503. if(first && ( /t(h|d)/i.test(first.tagName) || first === start.firstChild )){
  18504. me.execCommand('insertparagraphbeforetable');
  18505. domUtils.preventDefault(evt);
  18506. }
  18507. }
  18508. }
  18509. }
  18510. }
  18511. if ((evt.ctrlKey || evt.metaKey) && evt.keyCode == '67') {
  18512. tableCopyList = null;
  18513. var ut = getUETableBySelected(me);
  18514. if (ut) {
  18515. var tds = ut.selectedTds;
  18516. isFullCol = ut.isFullCol();
  18517. isFullRow = ut.isFullRow();
  18518. tableCopyList = [
  18519. [ut.cloneCell(tds[0],null,true)]
  18520. ];
  18521. for (var i = 1, ci; ci = tds[i]; i++) {
  18522. if (ci.parentNode !== tds[i - 1].parentNode) {
  18523. tableCopyList.push([ut.cloneCell(ci,null,true)]);
  18524. } else {
  18525. tableCopyList[tableCopyList.length - 1].push(ut.cloneCell(ci,null,true));
  18526. }
  18527. }
  18528. }
  18529. }
  18530. });
  18531. me.addListener("tablehasdeleted",function(){
  18532. toggleDraggableState(this, false, "", null);
  18533. if (dragButton)domUtils.remove(dragButton);
  18534. });
  18535. me.addListener('beforepaste', function (cmd, html) {
  18536. var me = this;
  18537. var rng = me.selection.getRange();
  18538. if (domUtils.findParentByTagName(rng.startContainer, 'caption', true)) {
  18539. var div = me.document.createElement("div");
  18540. div.innerHTML = html.html;
  18541. //trace:3729
  18542. html.html = div[browser.ie9below ? 'innerText' : 'textContent'];
  18543. return;
  18544. }
  18545. var table = getUETableBySelected(me);
  18546. if (tableCopyList) {
  18547. me.fireEvent('saveScene');
  18548. var rng = me.selection.getRange();
  18549. var td = domUtils.findParentByTagName(rng.startContainer, ['td', 'th'], true), tmpNode, preNode;
  18550. if (td) {
  18551. var ut = getUETable(td);
  18552. if (isFullRow) {
  18553. var rowIndex = ut.getCellInfo(td).rowIndex;
  18554. if (td.tagName == 'TH') {
  18555. rowIndex++;
  18556. }
  18557. for (var i = 0, ci; ci = tableCopyList[i++];) {
  18558. var tr = ut.insertRow(rowIndex++, "td");
  18559. for (var j = 0, cj; cj = ci[j]; j++) {
  18560. var cell = tr.cells[j];
  18561. if (!cell) {
  18562. cell = tr.insertCell(j)
  18563. }
  18564. cell.innerHTML = cj.innerHTML;
  18565. cj.getAttribute('width') && cell.setAttribute('width', cj.getAttribute('width'));
  18566. cj.getAttribute('vAlign') && cell.setAttribute('vAlign', cj.getAttribute('vAlign'));
  18567. cj.getAttribute('align') && cell.setAttribute('align', cj.getAttribute('align'));
  18568. cj.style.cssText && (cell.style.cssText = cj.style.cssText)
  18569. }
  18570. for (var j = 0, cj; cj = tr.cells[j]; j++) {
  18571. if (!ci[j])
  18572. break;
  18573. cj.innerHTML = ci[j].innerHTML;
  18574. ci[j].getAttribute('width') && cj.setAttribute('width', ci[j].getAttribute('width'));
  18575. ci[j].getAttribute('vAlign') && cj.setAttribute('vAlign', ci[j].getAttribute('vAlign'));
  18576. ci[j].getAttribute('align') && cj.setAttribute('align', ci[j].getAttribute('align'));
  18577. ci[j].style.cssText && (cj.style.cssText = ci[j].style.cssText)
  18578. }
  18579. }
  18580. } else {
  18581. if (isFullCol) {
  18582. cellInfo = ut.getCellInfo(td);
  18583. var maxColNum = 0;
  18584. for (var j = 0, ci = tableCopyList[0], cj; cj = ci[j++];) {
  18585. maxColNum += cj.colSpan || 1;
  18586. }
  18587. me.__hasEnterExecCommand = true;
  18588. for (i = 0; i < maxColNum; i++) {
  18589. me.execCommand('insertcol');
  18590. }
  18591. me.__hasEnterExecCommand = false;
  18592. td = ut.table.rows[0].cells[cellInfo.cellIndex];
  18593. if (td.tagName == 'TH') {
  18594. td = ut.table.rows[1].cells[cellInfo.cellIndex];
  18595. }
  18596. }
  18597. for (var i = 0, ci; ci = tableCopyList[i++];) {
  18598. tmpNode = td;
  18599. for (var j = 0, cj; cj = ci[j++];) {
  18600. if (td) {
  18601. td.innerHTML = cj.innerHTML;
  18602. //todo 定制处理
  18603. cj.getAttribute('width') && td.setAttribute('width', cj.getAttribute('width'));
  18604. cj.getAttribute('vAlign') && td.setAttribute('vAlign', cj.getAttribute('vAlign'));
  18605. cj.getAttribute('align') && td.setAttribute('align', cj.getAttribute('align'));
  18606. cj.style.cssText && (td.style.cssText = cj.style.cssText);
  18607. preNode = td;
  18608. td = td.nextSibling;
  18609. } else {
  18610. var cloneTd = cj.cloneNode(true);
  18611. domUtils.removeAttributes(cloneTd, ['class', 'rowSpan', 'colSpan']);
  18612. preNode.parentNode.appendChild(cloneTd)
  18613. }
  18614. }
  18615. td = ut.getNextCell(tmpNode, true, true);
  18616. if (!tableCopyList[i])
  18617. break;
  18618. if (!td) {
  18619. var cellInfo = ut.getCellInfo(tmpNode);
  18620. ut.table.insertRow(ut.table.rows.length);
  18621. ut.update();
  18622. td = ut.getVSideCell(tmpNode, true);
  18623. }
  18624. }
  18625. }
  18626. ut.update();
  18627. } else {
  18628. table = me.document.createElement('table');
  18629. for (var i = 0, ci; ci = tableCopyList[i++];) {
  18630. var tr = table.insertRow(table.rows.length);
  18631. for (var j = 0, cj; cj = ci[j++];) {
  18632. cloneTd = UT.cloneCell(cj,null,true);
  18633. domUtils.removeAttributes(cloneTd, ['class']);
  18634. tr.appendChild(cloneTd)
  18635. }
  18636. if (j == 2 && cloneTd.rowSpan > 1) {
  18637. cloneTd.rowSpan = 1;
  18638. }
  18639. }
  18640. var defaultValue = getDefaultValue(me),
  18641. width = me.body.offsetWidth -
  18642. (needIEHack ? parseInt(domUtils.getComputedStyle(me.body, 'margin-left'), 10) * 2 : 0) - defaultValue.tableBorder * 2 - (me.options.offsetWidth || 0);
  18643. me.execCommand('insertHTML', '<table ' +
  18644. ( isFullCol && isFullRow ? 'width="' + width + '"' : '') +
  18645. '>' + table.innerHTML.replace(/>\s*</g, '><').replace(/\bth\b/gi, "td") + '</table>')
  18646. }
  18647. me.fireEvent('contentchange');
  18648. me.fireEvent('saveScene');
  18649. html.html = '';
  18650. return true;
  18651. } else {
  18652. var div = me.document.createElement("div"), tables;
  18653. div.innerHTML = html.html;
  18654. tables = div.getElementsByTagName("table");
  18655. if (domUtils.findParentByTagName(me.selection.getStart(), 'table')) {
  18656. utils.each(tables, function (t) {
  18657. domUtils.remove(t)
  18658. });
  18659. if (domUtils.findParentByTagName(me.selection.getStart(), 'caption', true)) {
  18660. div.innerHTML = div[browser.ie ? 'innerText' : 'textContent'];
  18661. }
  18662. } else {
  18663. utils.each(tables, function (table) {
  18664. removeStyleSize(table, true);
  18665. domUtils.removeAttributes(table, ['style', 'border']);
  18666. utils.each(domUtils.getElementsByTagName(table, "td"), function (td) {
  18667. if (isEmptyBlock(td)) {
  18668. domUtils.fillNode(me.document, td);
  18669. }
  18670. removeStyleSize(td, true);
  18671. // domUtils.removeAttributes(td, ['style'])
  18672. });
  18673. });
  18674. }
  18675. html.html = div.innerHTML;
  18676. }
  18677. });
  18678. me.addListener('afterpaste', function () {
  18679. utils.each(domUtils.getElementsByTagName(me.body, "table"), function (table) {
  18680. if (table.offsetWidth > me.body.offsetWidth) {
  18681. var defaultValue = getDefaultValue(me, table);
  18682. 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'
  18683. }
  18684. })
  18685. });
  18686. me.addListener('blur', function () {
  18687. tableCopyList = null;
  18688. });
  18689. var timer;
  18690. me.addListener('keydown', function () {
  18691. clearTimeout(timer);
  18692. timer = setTimeout(function () {
  18693. var rng = me.selection.getRange(),
  18694. cell = domUtils.findParentByTagName(rng.startContainer, ['th', 'td'], true);
  18695. if (cell) {
  18696. var table = cell.parentNode.parentNode.parentNode;
  18697. if (table.offsetWidth > table.getAttribute("width")) {
  18698. cell.style.wordBreak = "break-all";
  18699. }
  18700. }
  18701. }, 100);
  18702. });
  18703. me.addListener("selectionchange", function () {
  18704. toggleDraggableState(me, false, "", null);
  18705. });
  18706. //内容变化时触发索引更新
  18707. //todo 可否考虑标记检测,如果不涉及表格的变化就不进行索引重建和更新
  18708. me.addListener("contentchange", function () {
  18709. var me = this;
  18710. //尽可能排除一些不需要更新的状况
  18711. hideDragLine(me);
  18712. if (getUETableBySelected(me))return;
  18713. var rng = me.selection.getRange();
  18714. var start = rng.startContainer;
  18715. start = domUtils.findParentByTagName(start, ['td', 'th'], true);
  18716. utils.each(domUtils.getElementsByTagName(me.document, 'table'), function (table) {
  18717. if (me.fireEvent("excludetable", table) === true) return;
  18718. table.ueTable = new UT(table);
  18719. //trace:3742
  18720. // utils.each(domUtils.getElementsByTagName(me.document, 'td'), function (td) {
  18721. //
  18722. // if (domUtils.isEmptyBlock(td) && td !== start) {
  18723. // domUtils.fillNode(me.document, td);
  18724. // if (browser.ie && browser.version == 6) {
  18725. // td.innerHTML = '&nbsp;'
  18726. // }
  18727. // }
  18728. // });
  18729. // utils.each(domUtils.getElementsByTagName(me.document, 'th'), function (th) {
  18730. // if (domUtils.isEmptyBlock(th) && th !== start) {
  18731. // domUtils.fillNode(me.document, th);
  18732. // if (browser.ie && browser.version == 6) {
  18733. // th.innerHTML = '&nbsp;'
  18734. // }
  18735. // }
  18736. // });
  18737. table.onmouseover = function () {
  18738. me.fireEvent('tablemouseover', table);
  18739. };
  18740. table.onmousemove = function () {
  18741. me.fireEvent('tablemousemove', table);
  18742. me.options.tableDragable && toggleDragButton(true, this, me);
  18743. utils.defer(function(){
  18744. me.fireEvent('contentchange',50)
  18745. },true)
  18746. };
  18747. table.onmouseout = function () {
  18748. me.fireEvent('tablemouseout', table);
  18749. toggleDraggableState(me, false, "", null);
  18750. hideDragLine(me);
  18751. };
  18752. table.onclick = function (evt) {
  18753. evt = me.window.event || evt;
  18754. var target = getParentTdOrTh(evt.target || evt.srcElement);
  18755. if (!target)return;
  18756. var ut = getUETable(target),
  18757. table = ut.table,
  18758. cellInfo = ut.getCellInfo(target),
  18759. cellsRange,
  18760. rng = me.selection.getRange();
  18761. // if ("topLeft" == inPosition(table, mouseCoords(evt))) {
  18762. // cellsRange = ut.getCellsRange(ut.table.rows[0].cells[0], ut.getLastCell());
  18763. // ut.setSelected(cellsRange);
  18764. // return;
  18765. // }
  18766. // if ("bottomRight" == inPosition(table, mouseCoords(evt))) {
  18767. //
  18768. // return;
  18769. // }
  18770. if (inTableSide(table, target, evt, true)) {
  18771. var endTdCol = ut.getCell(ut.indexTable[ut.rowsNum - 1][cellInfo.colIndex].rowIndex, ut.indexTable[ut.rowsNum - 1][cellInfo.colIndex].cellIndex);
  18772. if (evt.shiftKey && ut.selectedTds.length) {
  18773. if (ut.selectedTds[0] !== endTdCol) {
  18774. cellsRange = ut.getCellsRange(ut.selectedTds[0], endTdCol);
  18775. ut.setSelected(cellsRange);
  18776. } else {
  18777. rng && rng.selectNodeContents(endTdCol).select();
  18778. }
  18779. } else {
  18780. if (target !== endTdCol) {
  18781. cellsRange = ut.getCellsRange(target, endTdCol);
  18782. ut.setSelected(cellsRange);
  18783. } else {
  18784. rng && rng.selectNodeContents(endTdCol).select();
  18785. }
  18786. }
  18787. return;
  18788. }
  18789. if (inTableSide(table, target, evt)) {
  18790. var endTdRow = ut.getCell(ut.indexTable[cellInfo.rowIndex][ut.colsNum - 1].rowIndex, ut.indexTable[cellInfo.rowIndex][ut.colsNum - 1].cellIndex);
  18791. if (evt.shiftKey && ut.selectedTds.length) {
  18792. if (ut.selectedTds[0] !== endTdRow) {
  18793. cellsRange = ut.getCellsRange(ut.selectedTds[0], endTdRow);
  18794. ut.setSelected(cellsRange);
  18795. } else {
  18796. rng && rng.selectNodeContents(endTdRow).select();
  18797. }
  18798. } else {
  18799. if (target !== endTdRow) {
  18800. cellsRange = ut.getCellsRange(target, endTdRow);
  18801. ut.setSelected(cellsRange);
  18802. } else {
  18803. rng && rng.selectNodeContents(endTdRow).select();
  18804. }
  18805. }
  18806. }
  18807. };
  18808. });
  18809. switchBorderColor(me, true);
  18810. });
  18811. domUtils.on(me.document, "mousemove", mouseMoveEvent);
  18812. domUtils.on(me.document, "mouseout", function (evt) {
  18813. var target = evt.target || evt.srcElement;
  18814. if (target.tagName == "TABLE") {
  18815. toggleDraggableState(me, false, "", null);
  18816. }
  18817. });
  18818. /**
  18819. * 表格隔行变色
  18820. */
  18821. me.addListener("interlacetable",function(type,table,classList){
  18822. if(!table) return;
  18823. var me = this,
  18824. rows = table.rows,
  18825. len = rows.length,
  18826. getClass = function(list,index,repeat){
  18827. return list[index] ? list[index] : repeat ? list[index % list.length]: "";
  18828. };
  18829. for(var i = 0;i<len;i++){
  18830. rows[i].className = getClass( classList|| me.options.classList,i,true);
  18831. }
  18832. });
  18833. me.addListener("uninterlacetable",function(type,table){
  18834. if(!table) return;
  18835. var me = this,
  18836. rows = table.rows,
  18837. classList = me.options.classList,
  18838. len = rows.length;
  18839. for(var i = 0;i<len;i++){
  18840. domUtils.removeClasses( rows[i], classList );
  18841. }
  18842. });
  18843. me.addListener("mousedown", mouseDownEvent);
  18844. me.addListener("mouseup", mouseUpEvent);
  18845. //拖动的时候触发mouseup
  18846. domUtils.on( me.body, 'dragstart', function( evt ){
  18847. mouseUpEvent.call( me, 'dragstart', evt );
  18848. });
  18849. me.addOutputRule(function(root){
  18850. utils.each(root.getNodesByTagName('div'),function(n){
  18851. if (n.getAttr('id') == 'ue_tableDragLine') {
  18852. n.parentNode.removeChild(n);
  18853. }
  18854. });
  18855. });
  18856. var currentRowIndex = 0;
  18857. me.addListener("mousedown", function () {
  18858. currentRowIndex = 0;
  18859. });
  18860. me.addListener('tabkeydown', function () {
  18861. var range = this.selection.getRange(),
  18862. common = range.getCommonAncestor(true, true),
  18863. table = domUtils.findParentByTagName(common, 'table');
  18864. if (table) {
  18865. if (domUtils.findParentByTagName(common, 'caption', true)) {
  18866. var cell = domUtils.getElementsByTagName(table, 'th td');
  18867. if (cell && cell.length) {
  18868. range.setStart(cell[0], 0).setCursor(false, true)
  18869. }
  18870. } else {
  18871. var cell = domUtils.findParentByTagName(common, ['td', 'th'], true),
  18872. ua = getUETable(cell);
  18873. currentRowIndex = cell.rowSpan > 1 ? currentRowIndex : ua.getCellInfo(cell).rowIndex;
  18874. var nextCell = ua.getTabNextCell(cell, currentRowIndex);
  18875. if (nextCell) {
  18876. if (isEmptyBlock(nextCell)) {
  18877. range.setStart(nextCell, 0).setCursor(false, true)
  18878. } else {
  18879. range.selectNodeContents(nextCell).select()
  18880. }
  18881. } else {
  18882. me.fireEvent('saveScene');
  18883. me.__hasEnterExecCommand = true;
  18884. this.execCommand('insertrownext');
  18885. me.__hasEnterExecCommand = false;
  18886. range = this.selection.getRange();
  18887. range.setStart(table.rows[table.rows.length - 1].cells[0], 0).setCursor();
  18888. me.fireEvent('saveScene');
  18889. }
  18890. }
  18891. return true;
  18892. }
  18893. });
  18894. browser.ie && me.addListener('selectionchange', function () {
  18895. toggleDraggableState(this, false, "", null);
  18896. });
  18897. me.addListener("keydown", function (type, evt) {
  18898. var me = this;
  18899. //处理在表格的最后一个输入tab产生新的表格
  18900. var keyCode = evt.keyCode || evt.which;
  18901. if (keyCode == 8 || keyCode == 46) {
  18902. return;
  18903. }
  18904. var notCtrlKey = !evt.ctrlKey && !evt.metaKey && !evt.shiftKey && !evt.altKey;
  18905. notCtrlKey && removeSelectedClass(domUtils.getElementsByTagName(me.body, "td"));
  18906. var ut = getUETableBySelected(me);
  18907. if (!ut) return;
  18908. notCtrlKey && ut.clearSelected();
  18909. });
  18910. me.addListener("beforegetcontent", function () {
  18911. switchBorderColor(this, false);
  18912. browser.ie && utils.each(this.document.getElementsByTagName('caption'), function (ci) {
  18913. if (domUtils.isEmptyNode(ci)) {
  18914. ci.innerHTML = '&nbsp;'
  18915. }
  18916. });
  18917. });
  18918. me.addListener("aftergetcontent", function () {
  18919. switchBorderColor(this, true);
  18920. });
  18921. me.addListener("getAllHtml", function () {
  18922. removeSelectedClass(me.document.getElementsByTagName("td"));
  18923. });
  18924. //修正全屏状态下插入的表格宽度在非全屏状态下撑开编辑器的情况
  18925. me.addListener("fullscreenchanged", function (type, fullscreen) {
  18926. if (!fullscreen) {
  18927. var ratio = this.body.offsetWidth / document.body.offsetWidth,
  18928. tables = domUtils.getElementsByTagName(this.body, "table");
  18929. utils.each(tables, function (table) {
  18930. if (table.offsetWidth < me.body.offsetWidth) return false;
  18931. var tds = domUtils.getElementsByTagName(table, "td"),
  18932. backWidths = [];
  18933. utils.each(tds, function (td) {
  18934. backWidths.push(td.offsetWidth);
  18935. });
  18936. for (var i = 0, td; td = tds[i]; i++) {
  18937. td.setAttribute("width", Math.floor(backWidths[i] * ratio));
  18938. }
  18939. table.setAttribute("width", Math.floor(getTableWidth(me, needIEHack, getDefaultValue(me))))
  18940. });
  18941. }
  18942. });
  18943. //重写execCommand命令,用于处理框选时的处理
  18944. var oldExecCommand = me.execCommand;
  18945. me.execCommand = function (cmd, datatat) {
  18946. var me = this,
  18947. args = arguments;
  18948. cmd = cmd.toLowerCase();
  18949. var ut = getUETableBySelected(me), tds,
  18950. range = new dom.Range(me.document),
  18951. cmdFun = me.commands[cmd] || UE.commands[cmd],
  18952. result;
  18953. if (!cmdFun) return;
  18954. if (ut && !commands[cmd] && !cmdFun.notNeedUndo && !me.__hasEnterExecCommand) {
  18955. me.__hasEnterExecCommand = true;
  18956. me.fireEvent("beforeexeccommand", cmd);
  18957. tds = ut.selectedTds;
  18958. var lastState = -2, lastValue = -2, value, state;
  18959. for (var i = 0, td; td = tds[i]; i++) {
  18960. if (isEmptyBlock(td)) {
  18961. range.setStart(td, 0).setCursor(false, true)
  18962. } else {
  18963. range.selectNode(td).select(true);
  18964. }
  18965. state = me.queryCommandState(cmd);
  18966. value = me.queryCommandValue(cmd);
  18967. if (state != -1) {
  18968. if (lastState !== state || lastValue !== value) {
  18969. me._ignoreContentChange = true;
  18970. result = oldExecCommand.apply(me, arguments);
  18971. me._ignoreContentChange = false;
  18972. }
  18973. lastState = me.queryCommandState(cmd);
  18974. lastValue = me.queryCommandValue(cmd);
  18975. if (domUtils.isEmptyBlock(td)) {
  18976. domUtils.fillNode(me.document, td)
  18977. }
  18978. }
  18979. }
  18980. range.setStart(tds[0], 0).shrinkBoundary(true).setCursor(false, true);
  18981. me.fireEvent('contentchange');
  18982. me.fireEvent("afterexeccommand", cmd);
  18983. me.__hasEnterExecCommand = false;
  18984. me._selectionChange();
  18985. } else {
  18986. result = oldExecCommand.apply(me, arguments);
  18987. }
  18988. return result;
  18989. };
  18990. });
  18991. /**
  18992. * 删除obj的宽高style,改成属性宽高
  18993. * @param obj
  18994. * @param replaceToProperty
  18995. */
  18996. function removeStyleSize(obj, replaceToProperty) {
  18997. removeStyle(obj, "width", true);
  18998. removeStyle(obj, "height", true);
  18999. }
  19000. function removeStyle(obj, styleName, replaceToProperty) {
  19001. if (obj.style[styleName]) {
  19002. replaceToProperty && obj.setAttribute(styleName, parseInt(obj.style[styleName], 10));
  19003. obj.style[styleName] = "";
  19004. }
  19005. }
  19006. function getParentTdOrTh(ele) {
  19007. if (ele.tagName == "TD" || ele.tagName == "TH") return ele;
  19008. var td;
  19009. if (td = domUtils.findParentByTagName(ele, "td", true) || domUtils.findParentByTagName(ele, "th", true)) return td;
  19010. return null;
  19011. }
  19012. function isEmptyBlock(node) {
  19013. var reg = new RegExp(domUtils.fillChar, 'g');
  19014. if (node[browser.ie ? 'innerText' : 'textContent'].replace(/^\s*$/, '').replace(reg, '').length > 0) {
  19015. return 0;
  19016. }
  19017. for (var n in dtd.$isNotEmpty) {
  19018. if (node.getElementsByTagName(n).length) {
  19019. return 0;
  19020. }
  19021. }
  19022. return 1;
  19023. }
  19024. function mouseCoords(evt) {
  19025. if (evt.pageX || evt.pageY) {
  19026. return { x:evt.pageX, y:evt.pageY };
  19027. }
  19028. return {
  19029. x:evt.clientX + me.document.body.scrollLeft - me.document.body.clientLeft,
  19030. y:evt.clientY + me.document.body.scrollTop - me.document.body.clientTop
  19031. };
  19032. }
  19033. function mouseMoveEvent(evt) {
  19034. if( isEditorDisabled() ) {
  19035. return;
  19036. }
  19037. try {
  19038. //普通状态下鼠标移动
  19039. var target = getParentTdOrTh(evt.target || evt.srcElement),
  19040. pos;
  19041. //区分用户的行为是拖动还是双击
  19042. if( isInResizeBuffer ) {
  19043. me.body.style.webkitUserSelect = 'none';
  19044. if( Math.abs( userActionStatus.x - evt.clientX ) > offsetOfTableCell || Math.abs( userActionStatus.y - evt.clientY ) > offsetOfTableCell ) {
  19045. clearTableDragTimer();
  19046. isInResizeBuffer = false;
  19047. singleClickState = 0;
  19048. //drag action
  19049. tableBorderDrag(evt);
  19050. }
  19051. }
  19052. //修改单元格大小时的鼠标移动
  19053. if (onDrag && dragTd) {
  19054. singleClickState = 0;
  19055. me.body.style.webkitUserSelect = 'none';
  19056. me.selection.getNative()[browser.ie9below ? 'empty' : 'removeAllRanges']();
  19057. pos = mouseCoords(evt);
  19058. toggleDraggableState(me, true, onDrag, pos, target);
  19059. if (onDrag == "h") {
  19060. dragLine.style.left = getPermissionX(dragTd, evt) + "px";
  19061. } else if (onDrag == "v") {
  19062. dragLine.style.top = getPermissionY(dragTd, evt) + "px";
  19063. }
  19064. return;
  19065. }
  19066. //当鼠标处于table上时,修改移动过程中的光标状态
  19067. if (target) {
  19068. //针对使用table作为容器的组件不触发拖拽效果
  19069. if (me.fireEvent('excludetable', target) === true)
  19070. return;
  19071. pos = mouseCoords(evt);
  19072. var state = getRelation(target, pos),
  19073. table = domUtils.findParentByTagName(target, "table", true);
  19074. if (inTableSide(table, target, evt, true)) {
  19075. if (me.fireEvent("excludetable", table) === true) return;
  19076. me.body.style.cursor = "url(" + me.options.cursorpath + "h.png),pointer";
  19077. } else if (inTableSide(table, target, evt)) {
  19078. if (me.fireEvent("excludetable", table) === true) return;
  19079. me.body.style.cursor = "url(" + me.options.cursorpath + "v.png),pointer";
  19080. } else {
  19081. me.body.style.cursor = "text";
  19082. var curCell = target;
  19083. if (/\d/.test(state)) {
  19084. state = state.replace(/\d/, '');
  19085. target = getUETable(target).getPreviewCell(target, state == "v");
  19086. }
  19087. //位于第一行的顶部或者第一列的左边时不可拖动
  19088. toggleDraggableState(me, target ? !!state : false, target ? state : '', pos, target);
  19089. }
  19090. } else {
  19091. toggleDragButton(false, table, me);
  19092. }
  19093. } catch (e) {
  19094. showError(e);
  19095. }
  19096. }
  19097. var dragButtonTimer;
  19098. function toggleDragButton(show, table, editor) {
  19099. if (!show) {
  19100. if (dragOver)return;
  19101. dragButtonTimer = setTimeout(function () {
  19102. !dragOver && dragButton && dragButton.parentNode && dragButton.parentNode.removeChild(dragButton);
  19103. }, 2000);
  19104. } else {
  19105. createDragButton(table, editor);
  19106. }
  19107. }
  19108. function createDragButton(table, editor) {
  19109. var pos = domUtils.getXY(table),
  19110. doc = table.ownerDocument;
  19111. if (dragButton && dragButton.parentNode)return dragButton;
  19112. dragButton = doc.createElement("div");
  19113. dragButton.contentEditable = false;
  19114. dragButton.innerHTML = "";
  19115. 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;";
  19116. domUtils.unSelectable(dragButton);
  19117. dragButton.onmouseover = function (evt) {
  19118. dragOver = true;
  19119. };
  19120. dragButton.onmouseout = function (evt) {
  19121. dragOver = false;
  19122. };
  19123. domUtils.on(dragButton, 'click', function (type, evt) {
  19124. doClick(evt, this);
  19125. });
  19126. domUtils.on(dragButton, 'dblclick', function (type, evt) {
  19127. doDblClick(evt);
  19128. });
  19129. domUtils.on(dragButton, 'dragstart', function (type, evt) {
  19130. domUtils.preventDefault(evt);
  19131. });
  19132. var timer;
  19133. function doClick(evt, button) {
  19134. // 部分浏览器下需要清理
  19135. clearTimeout(timer);
  19136. timer = setTimeout(function () {
  19137. editor.fireEvent("tableClicked", table, button);
  19138. }, 300);
  19139. }
  19140. function doDblClick(evt) {
  19141. clearTimeout(timer);
  19142. var ut = getUETable(table),
  19143. start = table.rows[0].cells[0],
  19144. end = ut.getLastCell(),
  19145. range = ut.getCellsRange(start, end);
  19146. editor.selection.getRange().setStart(start, 0).setCursor(false, true);
  19147. ut.setSelected(range);
  19148. }
  19149. doc.body.appendChild(dragButton);
  19150. }
  19151. // function inPosition(table, pos) {
  19152. // var tablePos = domUtils.getXY(table),
  19153. // width = table.offsetWidth,
  19154. // height = table.offsetHeight;
  19155. // if (pos.x - tablePos.x < 5 && pos.y - tablePos.y < 5) {
  19156. // return "topLeft";
  19157. // } else if (tablePos.x + width - pos.x < 5 && tablePos.y + height - pos.y < 5) {
  19158. // return "bottomRight";
  19159. // }
  19160. // }
  19161. function inTableSide(table, cell, evt, top) {
  19162. var pos = mouseCoords(evt),
  19163. state = getRelation(cell, pos);
  19164. if (top) {
  19165. var caption = table.getElementsByTagName("caption")[0],
  19166. capHeight = caption ? caption.offsetHeight : 0;
  19167. return (state == "v1") && ((pos.y - domUtils.getXY(table).y - capHeight) < 8);
  19168. } else {
  19169. return (state == "h1") && ((pos.x - domUtils.getXY(table).x) < 8);
  19170. }
  19171. }
  19172. /**
  19173. * 获取拖动时允许的X轴坐标
  19174. * @param dragTd
  19175. * @param evt
  19176. */
  19177. function getPermissionX(dragTd, evt) {
  19178. var ut = getUETable(dragTd);
  19179. if (ut) {
  19180. var preTd = ut.getSameEndPosCells(dragTd, "x")[0],
  19181. nextTd = ut.getSameStartPosXCells(dragTd)[0],
  19182. mouseX = mouseCoords(evt).x,
  19183. left = (preTd ? domUtils.getXY(preTd).x : domUtils.getXY(ut.table).x) + 20 ,
  19184. right = nextTd ? domUtils.getXY(nextTd).x + nextTd.offsetWidth - 20 : (me.body.offsetWidth + 5 || parseInt(domUtils.getComputedStyle(me.body, "width"), 10));
  19185. left += cellMinWidth;
  19186. right -= cellMinWidth;
  19187. return mouseX < left ? left : mouseX > right ? right : mouseX;
  19188. }
  19189. }
  19190. /**
  19191. * 获取拖动时允许的Y轴坐标
  19192. */
  19193. function getPermissionY(dragTd, evt) {
  19194. try {
  19195. var top = domUtils.getXY(dragTd).y,
  19196. mousePosY = mouseCoords(evt).y;
  19197. return mousePosY < top ? top : mousePosY;
  19198. } catch (e) {
  19199. showError(e);
  19200. }
  19201. }
  19202. /**
  19203. * 移动状态切换
  19204. */
  19205. function toggleDraggableState(editor, draggable, dir, mousePos, cell) {
  19206. try {
  19207. editor.body.style.cursor = dir == "h" ? "col-resize" : dir == "v" ? "row-resize" : "text";
  19208. if (browser.ie) {
  19209. if (dir && !mousedown && !getUETableBySelected(editor)) {
  19210. getDragLine(editor, editor.document);
  19211. showDragLineAt(dir, cell);
  19212. } else {
  19213. hideDragLine(editor)
  19214. }
  19215. }
  19216. onBorder = draggable;
  19217. } catch (e) {
  19218. showError(e);
  19219. }
  19220. }
  19221. /**
  19222. * 获取与UETable相关的resize line
  19223. * @param uetable UETable对象
  19224. */
  19225. function getResizeLineByUETable() {
  19226. var lineId = '_UETableResizeLine',
  19227. line = this.document.getElementById( lineId );
  19228. if( !line ) {
  19229. line = this.document.createElement("div");
  19230. line.id = lineId;
  19231. line.contnetEditable = false;
  19232. line.setAttribute("unselectable", "on");
  19233. var styles = {
  19234. width: 2*cellBorderWidth + 1 + 'px',
  19235. position: 'absolute',
  19236. 'z-index': 100000,
  19237. cursor: 'col-resize',
  19238. background: 'red',
  19239. display: 'none'
  19240. };
  19241. //切换状态
  19242. line.onmouseout = function(){
  19243. this.style.display = 'none';
  19244. };
  19245. utils.extend( line.style, styles );
  19246. this.document.body.appendChild( line );
  19247. }
  19248. return line;
  19249. }
  19250. /**
  19251. * 更新resize-line
  19252. */
  19253. function updateResizeLine( cell, uetable ) {
  19254. var line = getResizeLineByUETable.call( this ),
  19255. table = uetable.table,
  19256. styles = {
  19257. top: domUtils.getXY( table ).y + 'px',
  19258. left: domUtils.getXY( cell).x + cell.offsetWidth - cellBorderWidth + 'px',
  19259. display: 'block',
  19260. height: table.offsetHeight + 'px'
  19261. };
  19262. utils.extend( line.style, styles );
  19263. }
  19264. /**
  19265. * 显示resize-line
  19266. */
  19267. function showResizeLine( cell ) {
  19268. var uetable = getUETable( cell );
  19269. updateResizeLine.call( this, cell, uetable );
  19270. }
  19271. /**
  19272. * 获取鼠标与当前单元格的相对位置
  19273. * @param ele
  19274. * @param mousePos
  19275. */
  19276. function getRelation(ele, mousePos) {
  19277. var elePos = domUtils.getXY(ele);
  19278. if( !elePos ) {
  19279. return '';
  19280. }
  19281. if (elePos.x + ele.offsetWidth - mousePos.x < cellBorderWidth) {
  19282. return "h";
  19283. }
  19284. if (mousePos.x - elePos.x < cellBorderWidth) {
  19285. return 'h1'
  19286. }
  19287. if (elePos.y + ele.offsetHeight - mousePos.y < cellBorderWidth) {
  19288. return "v";
  19289. }
  19290. if (mousePos.y - elePos.y < cellBorderWidth) {
  19291. return 'v1'
  19292. }
  19293. return '';
  19294. }
  19295. function mouseDownEvent(type, evt) {
  19296. if( isEditorDisabled() ) {
  19297. return ;
  19298. }
  19299. userActionStatus = {
  19300. x: evt.clientX,
  19301. y: evt.clientY
  19302. };
  19303. //右键菜单单独处理
  19304. if (evt.button == 2) {
  19305. var ut = getUETableBySelected(me),
  19306. flag = false;
  19307. if (ut) {
  19308. var td = getTargetTd(me, evt);
  19309. utils.each(ut.selectedTds, function (ti) {
  19310. if (ti === td) {
  19311. flag = true;
  19312. }
  19313. });
  19314. if (!flag) {
  19315. removeSelectedClass(domUtils.getElementsByTagName(me.body, "th td"));
  19316. ut.clearSelected()
  19317. } else {
  19318. td = ut.selectedTds[0];
  19319. setTimeout(function () {
  19320. me.selection.getRange().setStart(td, 0).setCursor(false, true);
  19321. }, 0);
  19322. }
  19323. }
  19324. } else {
  19325. tableClickHander( evt );
  19326. }
  19327. }
  19328. //清除表格的计时器
  19329. function clearTableTimer() {
  19330. tabTimer && clearTimeout( tabTimer );
  19331. tabTimer = null;
  19332. }
  19333. //双击收缩
  19334. function tableDbclickHandler(evt) {
  19335. singleClickState = 0;
  19336. evt = evt || me.window.event;
  19337. var target = getParentTdOrTh(evt.target || evt.srcElement);
  19338. if (target) {
  19339. var h;
  19340. if (h = getRelation(target, mouseCoords(evt))) {
  19341. hideDragLine( me );
  19342. if (h == 'h1') {
  19343. h = 'h';
  19344. if (inTableSide(domUtils.findParentByTagName(target, "table"), target, evt)) {
  19345. me.execCommand('adaptbywindow');
  19346. } else {
  19347. target = getUETable(target).getPreviewCell(target);
  19348. if (target) {
  19349. var rng = me.selection.getRange();
  19350. rng.selectNodeContents(target).setCursor(true, true)
  19351. }
  19352. }
  19353. }
  19354. if (h == 'h') {
  19355. var ut = getUETable(target),
  19356. table = ut.table,
  19357. cells = getCellsByMoveBorder( target, table, true );
  19358. cells = extractArray( cells, 'left' );
  19359. ut.width = ut.offsetWidth;
  19360. var oldWidth = [],
  19361. newWidth = [];
  19362. utils.each( cells, function( cell ){
  19363. oldWidth.push( cell.offsetWidth );
  19364. } );
  19365. utils.each( cells, function( cell ){
  19366. cell.removeAttribute("width");
  19367. } );
  19368. window.setTimeout( function(){
  19369. //是否允许改变
  19370. var changeable = true;
  19371. utils.each( cells, function( cell, index ){
  19372. var width = cell.offsetWidth;
  19373. if( width > oldWidth[index] ) {
  19374. changeable = false;
  19375. return false;
  19376. }
  19377. newWidth.push( width );
  19378. } );
  19379. var change = changeable ? newWidth : oldWidth;
  19380. utils.each( cells, function( cell, index ){
  19381. cell.width = change[index] - getTabcellSpace();
  19382. } );
  19383. }, 0 );
  19384. // minWidth -= cellMinWidth;
  19385. //
  19386. // table.removeAttribute("width");
  19387. // utils.each(cells, function (cell) {
  19388. // cell.style.width = "";
  19389. // cell.width -= minWidth;
  19390. // });
  19391. }
  19392. }
  19393. }
  19394. }
  19395. function tableClickHander( evt ) {
  19396. removeSelectedClass(domUtils.getElementsByTagName(me.body, "td th"));
  19397. //trace:3113
  19398. //选中单元格,点击table外部,不会清掉table上挂的ueTable,会引起getUETableBySelected方法返回值
  19399. utils.each(me.document.getElementsByTagName('table'), function (t) {
  19400. t.ueTable = null;
  19401. });
  19402. startTd = getTargetTd(me, evt);
  19403. if( !startTd ) return;
  19404. var table = domUtils.findParentByTagName(startTd, "table", true);
  19405. ut = getUETable(table);
  19406. ut && ut.clearSelected();
  19407. //判断当前鼠标状态
  19408. if (!onBorder) {
  19409. me.document.body.style.webkitUserSelect = '';
  19410. mousedown = true;
  19411. me.addListener('mouseover', mouseOverEvent);
  19412. } else {
  19413. //边框上的动作处理
  19414. borderActionHandler( evt );
  19415. }
  19416. }
  19417. //处理表格边框上的动作, 这里做延时处理,避免两种动作互相影响
  19418. function borderActionHandler( evt ) {
  19419. if ( browser.ie ) {
  19420. evt = reconstruct(evt );
  19421. }
  19422. clearTableDragTimer();
  19423. //是否正在等待resize的缓冲中
  19424. isInResizeBuffer = true;
  19425. tableDragTimer = setTimeout(function(){
  19426. tableBorderDrag( evt );
  19427. }, dblclickTime);
  19428. }
  19429. function extractArray( originArr, key ) {
  19430. var result = [],
  19431. tmp = null;
  19432. for( var i = 0, len = originArr.length; i<len; i++ ) {
  19433. tmp = originArr[ i ][ key ];
  19434. if( tmp ) {
  19435. result.push( tmp );
  19436. }
  19437. }
  19438. return result;
  19439. }
  19440. function clearTableDragTimer() {
  19441. tableDragTimer && clearTimeout(tableDragTimer);
  19442. tableDragTimer = null;
  19443. }
  19444. function reconstruct( obj ) {
  19445. var attrs = ['pageX', 'pageY', 'clientX', 'clientY', 'srcElement', 'target'],
  19446. newObj = {};
  19447. if( obj ) {
  19448. for( var i = 0, key, val; key = attrs[i]; i++ ) {
  19449. val=obj[ key ];
  19450. val && (newObj[ key ] = val);
  19451. }
  19452. }
  19453. return newObj;
  19454. }
  19455. //边框拖动
  19456. function tableBorderDrag( evt ) {
  19457. isInResizeBuffer = false;
  19458. startTd = evt.target || evt.srcElement;
  19459. if( !startTd ) return;
  19460. var state = getRelation(startTd, mouseCoords(evt));
  19461. if (/\d/.test(state)) {
  19462. state = state.replace(/\d/, '');
  19463. startTd = getUETable(startTd).getPreviewCell(startTd, state == 'v');
  19464. }
  19465. hideDragLine(me);
  19466. getDragLine(me, me.document);
  19467. me.fireEvent('saveScene');
  19468. showDragLineAt(state, startTd);
  19469. mousedown = true;
  19470. //拖动开始
  19471. onDrag = state;
  19472. dragTd = startTd;
  19473. }
  19474. function mouseUpEvent(type, evt) {
  19475. if( isEditorDisabled() ) {
  19476. return ;
  19477. }
  19478. clearTableDragTimer();
  19479. isInResizeBuffer = false;
  19480. if( onBorder ) {
  19481. singleClickState = ++singleClickState % 3;
  19482. userActionStatus = {
  19483. x: evt.clientX,
  19484. y: evt.clientY
  19485. };
  19486. tableResizeTimer = setTimeout(function(){
  19487. singleClickState > 0 && singleClickState--;
  19488. }, dblclickTime );
  19489. if( singleClickState === 2 ) {
  19490. singleClickState = 0;
  19491. tableDbclickHandler(evt);
  19492. return;
  19493. }
  19494. }
  19495. if (evt.button == 2)return;
  19496. var me = this;
  19497. //清除表格上原生跨选问题
  19498. var range = me.selection.getRange(),
  19499. start = domUtils.findParentByTagName(range.startContainer, 'table', true),
  19500. end = domUtils.findParentByTagName(range.endContainer, 'table', true);
  19501. if (start || end) {
  19502. if (start === end) {
  19503. start = domUtils.findParentByTagName(range.startContainer, ['td', 'th', 'caption'], true);
  19504. end = domUtils.findParentByTagName(range.endContainer, ['td', 'th', 'caption'], true);
  19505. if (start !== end) {
  19506. me.selection.clearRange()
  19507. }
  19508. } else {
  19509. me.selection.clearRange()
  19510. }
  19511. }
  19512. mousedown = false;
  19513. me.document.body.style.webkitUserSelect = '';
  19514. //拖拽状态下的mouseUP
  19515. if ( onDrag && dragTd ) {
  19516. me.selection.getNative()[browser.ie9below ? 'empty' : 'removeAllRanges']();
  19517. singleClickState = 0;
  19518. dragLine = me.document.getElementById('ue_tableDragLine');
  19519. // trace 3973
  19520. if (dragLine) {
  19521. var dragTdPos = domUtils.getXY(dragTd),
  19522. dragLinePos = domUtils.getXY(dragLine);
  19523. switch (onDrag) {
  19524. case "h":
  19525. changeColWidth(dragTd, dragLinePos.x - dragTdPos.x);
  19526. break;
  19527. case "v":
  19528. changeRowHeight(dragTd, dragLinePos.y - dragTdPos.y - dragTd.offsetHeight);
  19529. break;
  19530. default:
  19531. }
  19532. onDrag = "";
  19533. dragTd = null;
  19534. hideDragLine(me);
  19535. me.fireEvent('saveScene');
  19536. return;
  19537. }
  19538. }
  19539. //正常状态下的mouseup
  19540. if (!startTd) {
  19541. var target = domUtils.findParentByTagName(evt.target || evt.srcElement, "td", true);
  19542. if (!target) target = domUtils.findParentByTagName(evt.target || evt.srcElement, "th", true);
  19543. if (target && (target.tagName == "TD" || target.tagName == "TH")) {
  19544. if (me.fireEvent("excludetable", target) === true) return;
  19545. range = new dom.Range(me.document);
  19546. range.setStart(target, 0).setCursor(false, true);
  19547. }
  19548. } else {
  19549. var ut = getUETable(startTd),
  19550. cell = ut ? ut.selectedTds[0] : null;
  19551. if (cell) {
  19552. range = new dom.Range(me.document);
  19553. if (domUtils.isEmptyBlock(cell)) {
  19554. range.setStart(cell, 0).setCursor(false, true);
  19555. } else {
  19556. range.selectNodeContents(cell).shrinkBoundary().setCursor(false, true);
  19557. }
  19558. } else {
  19559. range = me.selection.getRange().shrinkBoundary();
  19560. if (!range.collapsed) {
  19561. var start = domUtils.findParentByTagName(range.startContainer, ['td', 'th'], true),
  19562. end = domUtils.findParentByTagName(range.endContainer, ['td', 'th'], true);
  19563. //在table里边的不能清除
  19564. if (start && !end || !start && end || start && end && start !== end) {
  19565. range.setCursor(false, true);
  19566. }
  19567. }
  19568. }
  19569. startTd = null;
  19570. me.removeListener('mouseover', mouseOverEvent);
  19571. }
  19572. me._selectionChange(250, evt);
  19573. }
  19574. function mouseOverEvent(type, evt) {
  19575. if( isEditorDisabled() ) {
  19576. return;
  19577. }
  19578. var me = this,
  19579. tar = evt.target || evt.srcElement;
  19580. currentTd = domUtils.findParentByTagName(tar, "td", true) || domUtils.findParentByTagName(tar, "th", true);
  19581. //需要判断两个TD是否位于同一个表格内
  19582. if (startTd && currentTd &&
  19583. ((startTd.tagName == "TD" && currentTd.tagName == "TD") || (startTd.tagName == "TH" && currentTd.tagName == "TH")) &&
  19584. domUtils.findParentByTagName(startTd, 'table') == domUtils.findParentByTagName(currentTd, 'table')) {
  19585. var ut = getUETable(currentTd);
  19586. if (startTd != currentTd) {
  19587. me.document.body.style.webkitUserSelect = 'none';
  19588. me.selection.getNative()[browser.ie9below ? 'empty' : 'removeAllRanges']();
  19589. var range = ut.getCellsRange(startTd, currentTd);
  19590. ut.setSelected(range);
  19591. } else {
  19592. me.document.body.style.webkitUserSelect = '';
  19593. ut.clearSelected();
  19594. }
  19595. }
  19596. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
  19597. }
  19598. function setCellHeight(cell, height, backHeight) {
  19599. var lineHight = parseInt(domUtils.getComputedStyle(cell, "line-height"), 10),
  19600. tmpHeight = backHeight + height;
  19601. height = tmpHeight < lineHight ? lineHight : tmpHeight;
  19602. if (cell.style.height) cell.style.height = "";
  19603. cell.rowSpan == 1 ? cell.setAttribute("height", height) : (cell.removeAttribute && cell.removeAttribute("height"));
  19604. }
  19605. function getWidth(cell) {
  19606. if (!cell)return 0;
  19607. return parseInt(domUtils.getComputedStyle(cell, "width"), 10);
  19608. }
  19609. function changeColWidth(cell, changeValue) {
  19610. var ut = getUETable(cell);
  19611. if (ut) {
  19612. //根据当前移动的边框获取相关的单元格
  19613. var table = ut.table,
  19614. cells = getCellsByMoveBorder( cell, table );
  19615. table.style.width = "";
  19616. table.removeAttribute("width");
  19617. //修正改变量
  19618. changeValue = correctChangeValue( changeValue, cell, cells );
  19619. if (cell.nextSibling) {
  19620. var i=0;
  19621. utils.each( cells, function( cellGroup ){
  19622. cellGroup.left.width = (+cellGroup.left.width)+changeValue;
  19623. cellGroup.right && ( cellGroup.right.width = (+cellGroup.right.width)-changeValue );
  19624. } );
  19625. } else {
  19626. utils.each( cells, function( cellGroup ){
  19627. cellGroup.left.width -= -changeValue;
  19628. } );
  19629. }
  19630. }
  19631. }
  19632. function isEditorDisabled() {
  19633. return me.body.contentEditable === "false";
  19634. }
  19635. function changeRowHeight(td, changeValue) {
  19636. if (Math.abs(changeValue) < 10) return;
  19637. var ut = getUETable(td);
  19638. if (ut) {
  19639. var cells = ut.getSameEndPosCells(td, "y"),
  19640. //备份需要连带变化的td的原始高度,否则后期无法获取正确的值
  19641. backHeight = cells[0] ? cells[0].offsetHeight : 0;
  19642. for (var i = 0, cell; cell = cells[i++];) {
  19643. setCellHeight(cell, changeValue, backHeight);
  19644. }
  19645. }
  19646. }
  19647. /**
  19648. * 获取调整单元格大小的相关单元格
  19649. * @isContainMergeCell 返回的结果中是否包含发生合并后的单元格
  19650. */
  19651. function getCellsByMoveBorder( cell, table, isContainMergeCell ) {
  19652. if( !table ) {
  19653. table = domUtils.findParentByTagName( cell, 'table' );
  19654. }
  19655. if( !table ) {
  19656. return null;
  19657. }
  19658. //获取到该单元格所在行的序列号
  19659. var index = domUtils.getNodeIndex( cell ),
  19660. temp = cell,
  19661. rows = table.rows,
  19662. colIndex = 0;
  19663. while( temp ) {
  19664. //获取到当前单元格在未发生单元格合并时的序列
  19665. if( temp.nodeType === 1 ) {
  19666. colIndex += (temp.colSpan || 1);
  19667. }
  19668. temp = temp.previousSibling;
  19669. }
  19670. temp = null;
  19671. //记录想关的单元格
  19672. var borderCells = [];
  19673. utils.each(rows, function( tabRow ){
  19674. var cells = tabRow.cells,
  19675. currIndex = 0;
  19676. utils.each( cells, function( tabCell ){
  19677. currIndex += (tabCell.colSpan || 1);
  19678. if( currIndex === colIndex ) {
  19679. borderCells.push({
  19680. left: tabCell,
  19681. right: tabCell.nextSibling || null
  19682. });
  19683. return false;
  19684. } else if( currIndex > colIndex ) {
  19685. if( isContainMergeCell ) {
  19686. borderCells.push({
  19687. left: tabCell
  19688. });
  19689. }
  19690. return false;
  19691. }
  19692. } );
  19693. });
  19694. return borderCells;
  19695. }
  19696. /**
  19697. * 通过给定的单元格集合获取最小的单元格width
  19698. */
  19699. function getMinWidthByTableCells( cells ) {
  19700. var minWidth = Number.MAX_VALUE;
  19701. for( var i = 0, curCell; curCell = cells[ i ] ; i++ ) {
  19702. minWidth = Math.min( minWidth, curCell.width || getTableCellWidth( curCell ) );
  19703. }
  19704. return minWidth;
  19705. }
  19706. function correctChangeValue( changeValue, relatedCell, cells ) {
  19707. //为单元格的paading预留空间
  19708. changeValue -= getTabcellSpace();
  19709. if( changeValue < 0 ) {
  19710. return 0;
  19711. }
  19712. changeValue -= getTableCellWidth( relatedCell );
  19713. //确定方向
  19714. var direction = changeValue < 0 ? 'left':'right';
  19715. changeValue = Math.abs(changeValue);
  19716. //只关心非最后一个单元格就可以
  19717. utils.each( cells, function( cellGroup ){
  19718. var curCell = cellGroup[direction];
  19719. //为单元格保留最小空间
  19720. if( curCell ) {
  19721. changeValue = Math.min( changeValue, getTableCellWidth( curCell )-cellMinWidth );
  19722. }
  19723. } );
  19724. //修正越界
  19725. changeValue = changeValue < 0 ? 0 : changeValue;
  19726. return direction === 'left' ? -changeValue : changeValue;
  19727. }
  19728. function getTableCellWidth( cell ) {
  19729. var width = 0,
  19730. //偏移纠正量
  19731. offset = 0,
  19732. width = cell.offsetWidth - getTabcellSpace();
  19733. //最后一个节点纠正一下
  19734. if( !cell.nextSibling ) {
  19735. width -= getTableCellOffset( cell );
  19736. }
  19737. width = width < 0 ? 0 : width;
  19738. try {
  19739. cell.width = width;
  19740. } catch(e) {
  19741. }
  19742. return width;
  19743. }
  19744. /**
  19745. * 获取单元格所在表格的最末单元格的偏移量
  19746. */
  19747. function getTableCellOffset( cell ) {
  19748. tab = domUtils.findParentByTagName( cell, "table", false);
  19749. if( tab.offsetVal === undefined ) {
  19750. var prev = cell.previousSibling;
  19751. if( prev ) {
  19752. //最后一个单元格和前一个单元格的width diff结果 如果恰好为一个border width, 则条件成立
  19753. tab.offsetVal = cell.offsetWidth - prev.offsetWidth === UT.borderWidth ? UT.borderWidth : 0;
  19754. } else {
  19755. tab.offsetVal = 0;
  19756. }
  19757. }
  19758. return tab.offsetVal;
  19759. }
  19760. function getTabcellSpace() {
  19761. if( UT.tabcellSpace === undefined ) {
  19762. var cell = null,
  19763. tab = me.document.createElement("table"),
  19764. tbody = me.document.createElement("tbody"),
  19765. trow = me.document.createElement("tr"),
  19766. tabcell = me.document.createElement("td"),
  19767. mirror = null;
  19768. tabcell.style.cssText = 'border: 0;';
  19769. tabcell.width = 1;
  19770. trow.appendChild( tabcell );
  19771. trow.appendChild( mirror = tabcell.cloneNode( false ) );
  19772. tbody.appendChild( trow );
  19773. tab.appendChild( tbody );
  19774. tab.style.cssText = "visibility: hidden;";
  19775. me.body.appendChild( tab );
  19776. UT.paddingSpace = tabcell.offsetWidth - 1;
  19777. var tmpTabWidth = tab.offsetWidth;
  19778. tabcell.style.cssText = '';
  19779. mirror.style.cssText = '';
  19780. UT.borderWidth = ( tab.offsetWidth - tmpTabWidth ) / 3;
  19781. UT.tabcellSpace = UT.paddingSpace + UT.borderWidth;
  19782. me.body.removeChild( tab );
  19783. }
  19784. getTabcellSpace = function(){ return UT.tabcellSpace; };
  19785. return UT.tabcellSpace;
  19786. }
  19787. function getDragLine(editor, doc) {
  19788. if (mousedown)return;
  19789. dragLine = editor.document.createElement("div");
  19790. domUtils.setAttributes(dragLine, {
  19791. id:"ue_tableDragLine",
  19792. unselectable:'on',
  19793. contenteditable:false,
  19794. 'onresizestart':'return false',
  19795. 'ondragstart':'return false',
  19796. 'onselectstart':'return false',
  19797. style:"background-color:blue;position:absolute;padding:0;margin:0;background-image:none;border:0px none;opacity:0;filter:alpha(opacity=0)"
  19798. });
  19799. editor.body.appendChild(dragLine);
  19800. }
  19801. function hideDragLine(editor) {
  19802. if (mousedown)return;
  19803. var line;
  19804. while (line = editor.document.getElementById('ue_tableDragLine')) {
  19805. domUtils.remove(line)
  19806. }
  19807. }
  19808. /**
  19809. * 依据state(v|h)在cell位置显示横线
  19810. * @param state
  19811. * @param cell
  19812. */
  19813. function showDragLineAt(state, cell) {
  19814. if (!cell) return;
  19815. var table = domUtils.findParentByTagName(cell, "table"),
  19816. caption = table.getElementsByTagName('caption'),
  19817. width = table.offsetWidth,
  19818. height = table.offsetHeight - (caption.length > 0 ? caption[0].offsetHeight : 0),
  19819. tablePos = domUtils.getXY(table),
  19820. cellPos = domUtils.getXY(cell), css;
  19821. switch (state) {
  19822. case "h":
  19823. css = 'height:' + height + 'px;top:' + (tablePos.y + (caption.length > 0 ? caption[0].offsetHeight : 0)) + 'px;left:' + (cellPos.x + cell.offsetWidth);
  19824. dragLine.style.cssText = css + 'px;position: absolute;display:block;background-color:blue;width:1px;border:0; color:blue;opacity:.3;filter:alpha(opacity=30)';
  19825. break;
  19826. case "v":
  19827. css = 'width:' + width + 'px;left:' + tablePos.x + 'px;top:' + (cellPos.y + cell.offsetHeight );
  19828. //必须加上border:0和color:blue,否则低版ie不支持背景色显示
  19829. 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)';
  19830. break;
  19831. default:
  19832. }
  19833. }
  19834. /**
  19835. * 当表格边框颜色为白色时设置为虚线,true为添加虚线
  19836. * @param editor
  19837. * @param flag
  19838. */
  19839. function switchBorderColor(editor, flag) {
  19840. var tableArr = domUtils.getElementsByTagName(editor.body, "table"), color;
  19841. for (var i = 0, node; node = tableArr[i++];) {
  19842. var td = domUtils.getElementsByTagName(node, "td");
  19843. if (td[0]) {
  19844. if (flag) {
  19845. color = (td[0].style.borderColor).replace(/\s/g, "");
  19846. if (/(#ffffff)|(rgb\(255,255,255\))/ig.test(color))
  19847. domUtils.addClass(node, "noBorderTable")
  19848. } else {
  19849. domUtils.removeClasses(node, "noBorderTable")
  19850. }
  19851. }
  19852. }
  19853. }
  19854. function getTableWidth(editor, needIEHack, defaultValue) {
  19855. var body = editor.body;
  19856. return body.offsetWidth - (needIEHack ? parseInt(domUtils.getComputedStyle(body, 'margin-left'), 10) * 2 : 0) - defaultValue.tableBorder * 2 - (editor.options.offsetWidth || 0);
  19857. }
  19858. /**
  19859. * 获取当前拖动的单元格
  19860. */
  19861. function getTargetTd(editor, evt) {
  19862. var target = domUtils.findParentByTagName(evt.target || evt.srcElement, ["td", "th"], true),
  19863. dir = null;
  19864. if( !target ) {
  19865. return null;
  19866. }
  19867. dir = getRelation( target, mouseCoords( evt ) );
  19868. //如果有前一个节点, 需要做一个修正, 否则可能会得到一个错误的td
  19869. if( !target ) {
  19870. return null;
  19871. }
  19872. if( dir === 'h1' && target.previousSibling ) {
  19873. var position = domUtils.getXY( target),
  19874. cellWidth = target.offsetWidth;
  19875. if( Math.abs( position.x + cellWidth - evt.clientX ) > cellWidth / 3 ) {
  19876. target = target.previousSibling;
  19877. }
  19878. } else if( dir === 'v1' && target.parentNode.previousSibling ) {
  19879. var position = domUtils.getXY( target),
  19880. cellHeight = target.offsetHeight;
  19881. if( Math.abs( position.y + cellHeight - evt.clientY ) > cellHeight / 3 ) {
  19882. target = target.parentNode.previousSibling.firstChild;
  19883. }
  19884. }
  19885. //排除了非td内部以及用于代码高亮部分的td
  19886. return target && !(editor.fireEvent("excludetable", target) === true) ? target : null;
  19887. }
  19888. };
  19889. // plugins/table.sort.js
  19890. /**
  19891. * Created with JetBrains PhpStorm.
  19892. * User: Jinqn
  19893. * Date: 13-10-12
  19894. * Time: 上午10:20
  19895. * To change this template use File | Settings | File Templates.
  19896. */
  19897. UE.UETable.prototype.sortTable = function (sortByCellIndex, compareFn) {
  19898. var table = this.table,
  19899. rows = table.rows,
  19900. trArray = [],
  19901. flag = rows[0].cells[0].tagName === "TH",
  19902. lastRowIndex = 0;
  19903. if(this.selectedTds.length){
  19904. var range = this.cellsRange,
  19905. len = range.endRowIndex + 1;
  19906. for (var i = range.beginRowIndex; i < len; i++) {
  19907. trArray[i] = rows[i];
  19908. }
  19909. trArray.splice(0,range.beginRowIndex);
  19910. lastRowIndex = (range.endRowIndex +1) === this.rowsNum ? 0 : range.endRowIndex +1;
  19911. }else{
  19912. for (var i = 0,len = rows.length; i < len; i++) {
  19913. trArray[i] = rows[i];
  19914. }
  19915. }
  19916. var Fn = {
  19917. 'reversecurrent': function(td1,td2){
  19918. return 1;
  19919. },
  19920. 'orderbyasc': function(td1,td2){
  19921. var value1 = td1.innerText||td1.textContent,
  19922. value2 = td2.innerText||td2.textContent;
  19923. return value1.localeCompare(value2);
  19924. },
  19925. 'reversebyasc': function(td1,td2){
  19926. var value1 = td1.innerHTML,
  19927. value2 = td2.innerHTML;
  19928. return value2.localeCompare(value1);
  19929. },
  19930. 'orderbynum': function(td1,td2){
  19931. var value1 = td1[browser.ie ? 'innerText':'textContent'].match(/\d+/),
  19932. value2 = td2[browser.ie ? 'innerText':'textContent'].match(/\d+/);
  19933. if(value1) value1 = +value1[0];
  19934. if(value2) value2 = +value2[0];
  19935. return (value1||0) - (value2||0);
  19936. },
  19937. 'reversebynum': function(td1,td2){
  19938. var value1 = td1[browser.ie ? 'innerText':'textContent'].match(/\d+/),
  19939. value2 = td2[browser.ie ? 'innerText':'textContent'].match(/\d+/);
  19940. if(value1) value1 = +value1[0];
  19941. if(value2) value2 = +value2[0];
  19942. return (value2||0) - (value1||0);
  19943. }
  19944. };
  19945. //对表格设置排序的标记data-sort-type
  19946. table.setAttribute('data-sort-type', compareFn && typeof compareFn === "string" && Fn[compareFn] ? compareFn:'');
  19947. //th不参与排序
  19948. flag && trArray.splice(0, 1);
  19949. trArray = utils.sort(trArray,function (tr1, tr2) {
  19950. var result;
  19951. if (compareFn && typeof compareFn === "function") {
  19952. result = compareFn.call(this, tr1.cells[sortByCellIndex], tr2.cells[sortByCellIndex]);
  19953. } else if (compareFn && typeof compareFn === "number") {
  19954. result = 1;
  19955. } else if (compareFn && typeof compareFn === "string" && Fn[compareFn]) {
  19956. result = Fn[compareFn].call(this, tr1.cells[sortByCellIndex], tr2.cells[sortByCellIndex]);
  19957. } else {
  19958. result = Fn['orderbyasc'].call(this, tr1.cells[sortByCellIndex], tr2.cells[sortByCellIndex]);
  19959. }
  19960. return result;
  19961. });
  19962. var fragment = table.ownerDocument.createDocumentFragment();
  19963. for (var j = 0, len = trArray.length; j < len; j++) {
  19964. fragment.appendChild(trArray[j]);
  19965. }
  19966. var tbody = table.getElementsByTagName("tbody")[0];
  19967. if(!lastRowIndex){
  19968. tbody.appendChild(fragment);
  19969. }else{
  19970. tbody.insertBefore(fragment,rows[lastRowIndex- range.endRowIndex + range.beginRowIndex - 1])
  19971. }
  19972. };
  19973. UE.plugins['tablesort'] = function () {
  19974. var me = this,
  19975. UT = UE.UETable,
  19976. getUETable = function (tdOrTable) {
  19977. return UT.getUETable(tdOrTable);
  19978. },
  19979. getTableItemsByRange = function (editor) {
  19980. return UT.getTableItemsByRange(editor);
  19981. };
  19982. me.ready(function () {
  19983. //添加表格可排序的样式
  19984. utils.cssRule('tablesort',
  19985. 'table.sortEnabled tr.firstRow th,table.sortEnabled tr.firstRow td{padding-right:20px;background-repeat: no-repeat;background-position: center right;' +
  19986. ' background-image:url(' + me.options.themePath + me.options.theme + '/images/sortable.png);}',
  19987. me.document);
  19988. //做单元格合并操作时,清除可排序标识
  19989. me.addListener("afterexeccommand", function (type, cmd) {
  19990. if( cmd == 'mergeright' || cmd == 'mergedown' || cmd == 'mergecells') {
  19991. this.execCommand('disablesort');
  19992. }
  19993. });
  19994. });
  19995. //表格排序
  19996. UE.commands['sorttable'] = {
  19997. queryCommandState: function () {
  19998. var me = this,
  19999. tableItems = getTableItemsByRange(me);
  20000. if (!tableItems.cell) return -1;
  20001. var table = tableItems.table,
  20002. cells = table.getElementsByTagName("td");
  20003. for (var i = 0, cell; cell = cells[i++];) {
  20004. if (cell.rowSpan != 1 || cell.colSpan != 1) return -1;
  20005. }
  20006. return 0;
  20007. },
  20008. execCommand: function (cmd, fn) {
  20009. var me = this,
  20010. range = me.selection.getRange(),
  20011. bk = range.createBookmark(true),
  20012. tableItems = getTableItemsByRange(me),
  20013. cell = tableItems.cell,
  20014. ut = getUETable(tableItems.table),
  20015. cellInfo = ut.getCellInfo(cell);
  20016. ut.sortTable(cellInfo.cellIndex, fn);
  20017. range.moveToBookmark(bk);
  20018. try{
  20019. range.select();
  20020. }catch(e){}
  20021. }
  20022. };
  20023. //设置表格可排序,清除表格可排序
  20024. UE.commands["enablesort"] = UE.commands["disablesort"] = {
  20025. queryCommandState: function (cmd) {
  20026. var table = getTableItemsByRange(this).table;
  20027. if(table && cmd=='enablesort') {
  20028. var cells = domUtils.getElementsByTagName(table, 'th td');
  20029. for(var i = 0; i<cells.length; i++) {
  20030. if(cells[i].getAttribute('colspan')>1 || cells[i].getAttribute('rowspan')>1) return -1;
  20031. }
  20032. }
  20033. return !table ? -1: cmd=='enablesort' ^ table.getAttribute('data-sort')!='sortEnabled' ? -1:0;
  20034. },
  20035. execCommand: function (cmd) {
  20036. var table = getTableItemsByRange(this).table;
  20037. table.setAttribute("data-sort", cmd == "enablesort" ? "sortEnabled" : "sortDisabled");
  20038. cmd == "enablesort" ? domUtils.addClass(table,"sortEnabled"):domUtils.removeClasses(table,"sortEnabled");
  20039. }
  20040. };
  20041. };
  20042. // plugins/contextmenu.js
  20043. ///import core
  20044. ///commands 右键菜单
  20045. ///commandsName ContextMenu
  20046. ///commandsTitle 右键菜单
  20047. /**
  20048. * 右键菜单
  20049. * @function
  20050. * @name baidu.editor.plugins.contextmenu
  20051. * @author zhanyi
  20052. */
  20053. UE.plugins['contextmenu'] = function () {
  20054. var me = this;
  20055. me.setOpt('enableContextMenu',true);
  20056. if(me.getOpt('enableContextMenu') === false){
  20057. return;
  20058. }
  20059. var lang = me.getLang( "contextMenu" ),
  20060. menu,
  20061. items = me.options.contextMenu || [
  20062. {label:lang['selectall'], cmdName:'selectall'},
  20063. {
  20064. label:lang.cleardoc,
  20065. cmdName:'cleardoc',
  20066. exec:function () {
  20067. if ( confirm( lang.confirmclear ) ) {
  20068. this.execCommand( 'cleardoc' );
  20069. }
  20070. }
  20071. },
  20072. '-',
  20073. {
  20074. label:lang.unlink,
  20075. cmdName:'unlink'
  20076. },
  20077. '-',
  20078. {
  20079. group:lang.paragraph,
  20080. icon:'justifyjustify',
  20081. subMenu:[
  20082. {
  20083. label:lang.justifyleft,
  20084. cmdName:'justify',
  20085. value:'left'
  20086. },
  20087. {
  20088. label:lang.justifyright,
  20089. cmdName:'justify',
  20090. value:'right'
  20091. },
  20092. {
  20093. label:lang.justifycenter,
  20094. cmdName:'justify',
  20095. value:'center'
  20096. },
  20097. {
  20098. label:lang.justifyjustify,
  20099. cmdName:'justify',
  20100. value:'justify'
  20101. }
  20102. ]
  20103. },
  20104. '-',
  20105. {
  20106. group:lang.table,
  20107. icon:'table',
  20108. subMenu:[
  20109. {
  20110. label:lang.inserttable,
  20111. cmdName:'inserttable'
  20112. },
  20113. {
  20114. label:lang.deletetable,
  20115. cmdName:'deletetable'
  20116. },
  20117. '-',
  20118. {
  20119. label:lang.deleterow,
  20120. cmdName:'deleterow'
  20121. },
  20122. {
  20123. label:lang.deletecol,
  20124. cmdName:'deletecol'
  20125. },
  20126. {
  20127. label:lang.insertcol,
  20128. cmdName:'insertcol'
  20129. },
  20130. {
  20131. label:lang.insertcolnext,
  20132. cmdName:'insertcolnext'
  20133. },
  20134. {
  20135. label:lang.insertrow,
  20136. cmdName:'insertrow'
  20137. },
  20138. {
  20139. label:lang.insertrownext,
  20140. cmdName:'insertrownext'
  20141. },
  20142. '-',
  20143. {
  20144. label:lang.insertcaption,
  20145. cmdName:'insertcaption'
  20146. },
  20147. {
  20148. label:lang.deletecaption,
  20149. cmdName:'deletecaption'
  20150. },
  20151. {
  20152. label:lang.inserttitle,
  20153. cmdName:'inserttitle'
  20154. },
  20155. {
  20156. label:lang.deletetitle,
  20157. cmdName:'deletetitle'
  20158. },
  20159. {
  20160. label:lang.inserttitlecol,
  20161. cmdName:'inserttitlecol'
  20162. },
  20163. {
  20164. label:lang.deletetitlecol,
  20165. cmdName:'deletetitlecol'
  20166. },
  20167. '-',
  20168. {
  20169. label:lang.mergecells,
  20170. cmdName:'mergecells'
  20171. },
  20172. {
  20173. label:lang.mergeright,
  20174. cmdName:'mergeright'
  20175. },
  20176. {
  20177. label:lang.mergedown,
  20178. cmdName:'mergedown'
  20179. },
  20180. '-',
  20181. {
  20182. label:lang.splittorows,
  20183. cmdName:'splittorows'
  20184. },
  20185. {
  20186. label:lang.splittocols,
  20187. cmdName:'splittocols'
  20188. },
  20189. {
  20190. label:lang.splittocells,
  20191. cmdName:'splittocells'
  20192. },
  20193. '-',
  20194. {
  20195. label:lang.averageDiseRow,
  20196. cmdName:'averagedistributerow'
  20197. },
  20198. {
  20199. label:lang.averageDisCol,
  20200. cmdName:'averagedistributecol'
  20201. },
  20202. '-',
  20203. {
  20204. label:lang.edittd,
  20205. cmdName:'edittd',
  20206. exec:function () {
  20207. if ( UE.ui['edittd'] ) {
  20208. new UE.ui['edittd']( this );
  20209. }
  20210. this.getDialog('edittd').open();
  20211. }
  20212. },
  20213. {
  20214. label:lang.edittable,
  20215. cmdName:'edittable',
  20216. exec:function () {
  20217. if ( UE.ui['edittable'] ) {
  20218. new UE.ui['edittable']( this );
  20219. }
  20220. this.getDialog('edittable').open();
  20221. }
  20222. },
  20223. {
  20224. label:lang.setbordervisible,
  20225. cmdName:'setbordervisible'
  20226. }
  20227. ]
  20228. },
  20229. {
  20230. group:lang.tablesort,
  20231. icon:'tablesort',
  20232. subMenu:[
  20233. {
  20234. label:lang.enablesort,
  20235. cmdName:'enablesort'
  20236. },
  20237. {
  20238. label:lang.disablesort,
  20239. cmdName:'disablesort'
  20240. },
  20241. '-',
  20242. {
  20243. label:lang.reversecurrent,
  20244. cmdName:'sorttable',
  20245. value:'reversecurrent'
  20246. },
  20247. {
  20248. label:lang.orderbyasc,
  20249. cmdName:'sorttable',
  20250. value:'orderbyasc'
  20251. },
  20252. {
  20253. label:lang.reversebyasc,
  20254. cmdName:'sorttable',
  20255. value:'reversebyasc'
  20256. },
  20257. {
  20258. label:lang.orderbynum,
  20259. cmdName:'sorttable',
  20260. value:'orderbynum'
  20261. },
  20262. {
  20263. label:lang.reversebynum,
  20264. cmdName:'sorttable',
  20265. value:'reversebynum'
  20266. }
  20267. ]
  20268. },
  20269. {
  20270. group:lang.borderbk,
  20271. icon:'borderBack',
  20272. subMenu:[
  20273. {
  20274. label:lang.setcolor,
  20275. cmdName:"interlacetable",
  20276. exec:function(){
  20277. this.execCommand("interlacetable");
  20278. }
  20279. },
  20280. {
  20281. label:lang.unsetcolor,
  20282. cmdName:"uninterlacetable",
  20283. exec:function(){
  20284. this.execCommand("uninterlacetable");
  20285. }
  20286. },
  20287. {
  20288. label:lang.setbackground,
  20289. cmdName:"settablebackground",
  20290. exec:function(){
  20291. this.execCommand("settablebackground",{repeat:true,colorList:["#bbb","#ccc"]});
  20292. }
  20293. },
  20294. {
  20295. label:lang.unsetbackground,
  20296. cmdName:"cleartablebackground",
  20297. exec:function(){
  20298. this.execCommand("cleartablebackground");
  20299. }
  20300. },
  20301. {
  20302. label:lang.redandblue,
  20303. cmdName:"settablebackground",
  20304. exec:function(){
  20305. this.execCommand("settablebackground",{repeat:true,colorList:["red","blue"]});
  20306. }
  20307. },
  20308. {
  20309. label:lang.threecolorgradient,
  20310. cmdName:"settablebackground",
  20311. exec:function(){
  20312. this.execCommand("settablebackground",{repeat:true,colorList:["#aaa","#bbb","#ccc"]});
  20313. }
  20314. }
  20315. ]
  20316. },
  20317. {
  20318. group:lang.aligntd,
  20319. icon:'aligntd',
  20320. subMenu:[
  20321. {
  20322. cmdName:'cellalignment',
  20323. value:{align:'left',vAlign:'top'}
  20324. },
  20325. {
  20326. cmdName:'cellalignment',
  20327. value:{align:'center',vAlign:'top'}
  20328. },
  20329. {
  20330. cmdName:'cellalignment',
  20331. value:{align:'right',vAlign:'top'}
  20332. },
  20333. {
  20334. cmdName:'cellalignment',
  20335. value:{align:'left',vAlign:'middle'}
  20336. },
  20337. {
  20338. cmdName:'cellalignment',
  20339. value:{align:'center',vAlign:'middle'}
  20340. },
  20341. {
  20342. cmdName:'cellalignment',
  20343. value:{align:'right',vAlign:'middle'}
  20344. },
  20345. {
  20346. cmdName:'cellalignment',
  20347. value:{align:'left',vAlign:'bottom'}
  20348. },
  20349. {
  20350. cmdName:'cellalignment',
  20351. value:{align:'center',vAlign:'bottom'}
  20352. },
  20353. {
  20354. cmdName:'cellalignment',
  20355. value:{align:'right',vAlign:'bottom'}
  20356. }
  20357. ]
  20358. },
  20359. {
  20360. group:lang.aligntable,
  20361. icon:'aligntable',
  20362. subMenu:[
  20363. {
  20364. cmdName:'tablealignment',
  20365. className: 'left',
  20366. label:lang.tableleft,
  20367. value:"left"
  20368. },
  20369. {
  20370. cmdName:'tablealignment',
  20371. className: 'center',
  20372. label:lang.tablecenter,
  20373. value:"center"
  20374. },
  20375. {
  20376. cmdName:'tablealignment',
  20377. className: 'right',
  20378. label:lang.tableright,
  20379. value:"right"
  20380. }
  20381. ]
  20382. },
  20383. '-',
  20384. {
  20385. label:lang.insertparagraphbefore,
  20386. cmdName:'insertparagraph',
  20387. value:true
  20388. },
  20389. {
  20390. label:lang.insertparagraphafter,
  20391. cmdName:'insertparagraph'
  20392. },
  20393. {
  20394. label:lang['copy'],
  20395. cmdName:'copy'
  20396. },
  20397. {
  20398. label:lang['paste'],
  20399. cmdName:'paste'
  20400. }
  20401. ];
  20402. if ( !items.length ) {
  20403. return;
  20404. }
  20405. var uiUtils = UE.ui.uiUtils;
  20406. me.addListener( 'contextmenu', function ( type, evt ) {
  20407. var offset = uiUtils.getViewportOffsetByEvent( evt );
  20408. me.fireEvent( 'beforeselectionchange' );
  20409. if ( menu ) {
  20410. menu.destroy();
  20411. }
  20412. for ( var i = 0, ti, contextItems = []; ti = items[i]; i++ ) {
  20413. var last;
  20414. (function ( item ) {
  20415. if ( item == '-' ) {
  20416. if ( (last = contextItems[contextItems.length - 1 ] ) && last !== '-' ) {
  20417. contextItems.push( '-' );
  20418. }
  20419. } else if ( item.hasOwnProperty( "group" ) ) {
  20420. for ( var j = 0, cj, subMenu = []; cj = item.subMenu[j]; j++ ) {
  20421. (function ( subItem ) {
  20422. if ( subItem == '-' ) {
  20423. if ( (last = subMenu[subMenu.length - 1 ] ) && last !== '-' ) {
  20424. subMenu.push( '-' );
  20425. }else{
  20426. subMenu.splice(subMenu.length-1);
  20427. }
  20428. } else {
  20429. if ( (me.commands[subItem.cmdName] || UE.commands[subItem.cmdName] || subItem.query) &&
  20430. (subItem.query ? subItem.query() : me.queryCommandState( subItem.cmdName )) > -1 ) {
  20431. subMenu.push( {
  20432. 'label':subItem.label || me.getLang( "contextMenu." + subItem.cmdName + (subItem.value || '') )||"",
  20433. 'className':'edui-for-' +subItem.cmdName + ( subItem.className ? ( ' edui-for-' + subItem.cmdName + '-' + subItem.className ) : '' ),
  20434. onclick:subItem.exec ? function () {
  20435. subItem.exec.call( me );
  20436. } : function () {
  20437. me.execCommand( subItem.cmdName, subItem.value );
  20438. }
  20439. } );
  20440. }
  20441. }
  20442. })( cj );
  20443. }
  20444. if ( subMenu.length ) {
  20445. function getLabel(){
  20446. switch (item.icon){
  20447. case "table":
  20448. return me.getLang( "contextMenu.table" );
  20449. case "justifyjustify":
  20450. return me.getLang( "contextMenu.paragraph" );
  20451. case "aligntd":
  20452. return me.getLang("contextMenu.aligntd");
  20453. case "aligntable":
  20454. return me.getLang("contextMenu.aligntable");
  20455. case "tablesort":
  20456. return lang.tablesort;
  20457. case "borderBack":
  20458. return lang.borderbk;
  20459. default :
  20460. return '';
  20461. }
  20462. }
  20463. contextItems.push( {
  20464. //todo 修正成自动获取方式
  20465. 'label':getLabel(),
  20466. className:'edui-for-' + item.icon,
  20467. 'subMenu':{
  20468. items:subMenu,
  20469. editor:me
  20470. }
  20471. } );
  20472. }
  20473. } else {
  20474. //有可能commmand没有加载右键不能出来,或者没有command也想能展示出来添加query方法
  20475. if ( (me.commands[item.cmdName] || UE.commands[item.cmdName] || item.query) &&
  20476. (item.query ? item.query.call(me) : me.queryCommandState( item.cmdName )) > -1 ) {
  20477. contextItems.push( {
  20478. 'label':item.label || me.getLang( "contextMenu." + item.cmdName ),
  20479. className:'edui-for-' + (item.icon ? item.icon : item.cmdName + (item.value || '')),
  20480. onclick:item.exec ? function () {
  20481. item.exec.call( me );
  20482. } : function () {
  20483. me.execCommand( item.cmdName, item.value );
  20484. }
  20485. } );
  20486. }
  20487. }
  20488. })( ti );
  20489. }
  20490. if ( contextItems[contextItems.length - 1] == '-' ) {
  20491. contextItems.pop();
  20492. }
  20493. menu = new UE.ui.Menu( {
  20494. items:contextItems,
  20495. className:"edui-contextmenu",
  20496. editor:me
  20497. } );
  20498. menu.render();
  20499. menu.showAt( offset );
  20500. me.fireEvent("aftershowcontextmenu",menu);
  20501. domUtils.preventDefault( evt );
  20502. if ( browser.ie ) {
  20503. var ieRange;
  20504. try {
  20505. ieRange = me.selection.getNative().createRange();
  20506. } catch ( e ) {
  20507. return;
  20508. }
  20509. if ( ieRange.item ) {
  20510. var range = new dom.Range( me.document );
  20511. range.selectNode( ieRange.item( 0 ) ).select( true, true );
  20512. }
  20513. }
  20514. });
  20515. // 添加复制的flash按钮
  20516. me.addListener('aftershowcontextmenu', function(type, menu) {
  20517. if (me.zeroclipboard) {
  20518. var items = menu.items;
  20519. for (var key in items) {
  20520. if (items[key].className == 'edui-for-copy') {
  20521. me.zeroclipboard.clip(items[key].getDom());
  20522. }
  20523. }
  20524. }
  20525. });
  20526. };
  20527. // plugins/shortcutmenu.js
  20528. ///import core
  20529. ///commands 弹出菜单
  20530. // commandsName popupmenu
  20531. ///commandsTitle 弹出菜单
  20532. /**
  20533. * 弹出菜单
  20534. * @function
  20535. * @name baidu.editor.plugins.popupmenu
  20536. * @author xuheng
  20537. */
  20538. UE.plugins['shortcutmenu'] = function () {
  20539. var me = this,
  20540. menu,
  20541. items = me.options.shortcutMenu || [];
  20542. if (!items.length) {
  20543. return;
  20544. }
  20545. me.addListener ('contextmenu mouseup' , function (type , e) {
  20546. var me = this,
  20547. customEvt = {
  20548. type : type ,
  20549. target : e.target || e.srcElement ,
  20550. screenX : e.screenX ,
  20551. screenY : e.screenY ,
  20552. clientX : e.clientX ,
  20553. clientY : e.clientY
  20554. };
  20555. setTimeout (function () {
  20556. var rng = me.selection.getRange ();
  20557. if (rng.collapsed === false || type == "contextmenu") {
  20558. if (!menu) {
  20559. menu = new baidu.editor.ui.ShortCutMenu ({
  20560. editor : me ,
  20561. items : items ,
  20562. theme : me.options.theme ,
  20563. className : 'edui-shortcutmenu'
  20564. });
  20565. menu.render ();
  20566. me.fireEvent ("afterrendershortcutmenu" , menu);
  20567. }
  20568. menu.show (customEvt , !!UE.plugins['contextmenu']);
  20569. }
  20570. });
  20571. if (type == 'contextmenu') {
  20572. domUtils.preventDefault (e);
  20573. if (browser.ie9below) {
  20574. var ieRange;
  20575. try {
  20576. ieRange = me.selection.getNative().createRange();
  20577. } catch (e) {
  20578. return;
  20579. }
  20580. if (ieRange.item) {
  20581. var range = new dom.Range (me.document);
  20582. range.selectNode (ieRange.item (0)).select (true , true);
  20583. }
  20584. }
  20585. }
  20586. });
  20587. me.addListener ('keydown' , function (type) {
  20588. if (type == "keydown") {
  20589. menu && !menu.isHidden && menu.hide ();
  20590. }
  20591. });
  20592. };
  20593. // plugins/basestyle.js
  20594. /**
  20595. * B、I、sub、super命令支持
  20596. * @file
  20597. * @since 1.2.6.1
  20598. */
  20599. UE.plugins['basestyle'] = function(){
  20600. /**
  20601. * 字体加粗
  20602. * @command bold
  20603. * @param { String } cmd 命令字符串
  20604. * @remind 对已加粗的文本内容执行该命令, 将取消加粗
  20605. * @method execCommand
  20606. * @example
  20607. * ```javascript
  20608. * //editor是编辑器实例
  20609. * //对当前选中的文本内容执行加粗操作
  20610. * //第一次执行, 文本内容加粗
  20611. * editor.execCommand( 'bold' );
  20612. *
  20613. * //第二次执行, 文本内容取消加粗
  20614. * editor.execCommand( 'bold' );
  20615. * ```
  20616. */
  20617. /**
  20618. * 字体倾斜
  20619. * @command italic
  20620. * @method execCommand
  20621. * @param { String } cmd 命令字符串
  20622. * @remind 对已倾斜的文本内容执行该命令, 将取消倾斜
  20623. * @example
  20624. * ```javascript
  20625. * //editor是编辑器实例
  20626. * //对当前选中的文本内容执行斜体操作
  20627. * //第一次操作, 文本内容将变成斜体
  20628. * editor.execCommand( 'italic' );
  20629. *
  20630. * //再次对同一文本内容执行, 则文本内容将恢复正常
  20631. * editor.execCommand( 'italic' );
  20632. * ```
  20633. */
  20634. /**
  20635. * 下标文本,与“superscript”命令互斥
  20636. * @command subscript
  20637. * @method execCommand
  20638. * @remind 把选中的文本内容切换成下标文本, 如果当前选中的文本已经是下标, 则该操作会把文本内容还原成正常文本
  20639. * @param { String } cmd 命令字符串
  20640. * @example
  20641. * ```javascript
  20642. * //editor是编辑器实例
  20643. * //对当前选中的文本内容执行下标操作
  20644. * //第一次操作, 文本内容将变成下标文本
  20645. * editor.execCommand( 'subscript' );
  20646. *
  20647. * //再次对同一文本内容执行, 则文本内容将恢复正常
  20648. * editor.execCommand( 'subscript' );
  20649. * ```
  20650. */
  20651. /**
  20652. * 上标文本,与“subscript”命令互斥
  20653. * @command superscript
  20654. * @method execCommand
  20655. * @remind 把选中的文本内容切换成上标文本, 如果当前选中的文本已经是上标, 则该操作会把文本内容还原成正常文本
  20656. * @param { String } cmd 命令字符串
  20657. * @example
  20658. * ```javascript
  20659. * //editor是编辑器实例
  20660. * //对当前选中的文本内容执行上标操作
  20661. * //第一次操作, 文本内容将变成上标文本
  20662. * editor.execCommand( 'superscript' );
  20663. *
  20664. * //再次对同一文本内容执行, 则文本内容将恢复正常
  20665. * editor.execCommand( 'superscript' );
  20666. * ```
  20667. */
  20668. var basestyles = {
  20669. 'bold':['strong','b'],
  20670. 'italic':['em','i'],
  20671. 'subscript':['sub'],
  20672. 'superscript':['sup']
  20673. },
  20674. getObj = function(editor,tagNames){
  20675. return domUtils.filterNodeList(editor.selection.getStartElementPath(),tagNames);
  20676. },
  20677. me = this;
  20678. //添加快捷键
  20679. me.addshortcutkey({
  20680. "Bold" : "ctrl+66",//^B
  20681. "Italic" : "ctrl+73", //^I
  20682. "Underline" : "ctrl+85"//^U
  20683. });
  20684. me.addInputRule(function(root){
  20685. utils.each(root.getNodesByTagName('b i'),function(node){
  20686. switch (node.tagName){
  20687. case 'b':
  20688. node.tagName = 'strong';
  20689. break;
  20690. case 'i':
  20691. node.tagName = 'em';
  20692. }
  20693. });
  20694. });
  20695. for ( var style in basestyles ) {
  20696. (function( cmd, tagNames ) {
  20697. me.commands[cmd] = {
  20698. execCommand : function( cmdName ) {
  20699. var range = me.selection.getRange(),obj = getObj(this,tagNames);
  20700. if ( range.collapsed ) {
  20701. if ( obj ) {
  20702. var tmpText = me.document.createTextNode('');
  20703. range.insertNode( tmpText ).removeInlineStyle( tagNames );
  20704. range.setStartBefore(tmpText);
  20705. domUtils.remove(tmpText);
  20706. } else {
  20707. var tmpNode = range.document.createElement( tagNames[0] );
  20708. if(cmdName == 'superscript' || cmdName == 'subscript'){
  20709. tmpText = me.document.createTextNode('');
  20710. range.insertNode(tmpText)
  20711. .removeInlineStyle(['sub','sup'])
  20712. .setStartBefore(tmpText)
  20713. .collapse(true);
  20714. }
  20715. range.insertNode( tmpNode ).setStart( tmpNode, 0 );
  20716. }
  20717. range.collapse( true );
  20718. } else {
  20719. if(cmdName == 'superscript' || cmdName == 'subscript'){
  20720. if(!obj || obj.tagName.toLowerCase() != cmdName){
  20721. range.removeInlineStyle(['sub','sup']);
  20722. }
  20723. }
  20724. obj ? range.removeInlineStyle( tagNames ) : range.applyInlineStyle( tagNames[0] );
  20725. }
  20726. range.select();
  20727. },
  20728. queryCommandState : function() {
  20729. return getObj(this,tagNames) ? 1 : 0;
  20730. }
  20731. };
  20732. })( style, basestyles[style] );
  20733. }
  20734. };
  20735. // plugins/elementpath.js
  20736. /**
  20737. * 选取路径命令
  20738. * @file
  20739. */
  20740. UE.plugins['elementpath'] = function(){
  20741. var currentLevel,
  20742. tagNames,
  20743. me = this;
  20744. me.setOpt('elementPathEnabled',true);
  20745. if(!me.options.elementPathEnabled){
  20746. return;
  20747. }
  20748. me.commands['elementpath'] = {
  20749. execCommand : function( cmdName, level ) {
  20750. var start = tagNames[level],
  20751. range = me.selection.getRange();
  20752. currentLevel = level*1;
  20753. range.selectNode(start).select();
  20754. },
  20755. queryCommandValue : function() {
  20756. //产生一个副本,不能修改原来的startElementPath;
  20757. var parents = [].concat(this.selection.getStartElementPath()).reverse(),
  20758. names = [];
  20759. tagNames = parents;
  20760. for(var i=0,ci;ci=parents[i];i++){
  20761. if(ci.nodeType == 3) {
  20762. continue;
  20763. }
  20764. var name = ci.tagName.toLowerCase();
  20765. if(name == 'img' && ci.getAttribute('anchorname')){
  20766. name = 'anchor';
  20767. }
  20768. names[i] = name;
  20769. if(currentLevel == i){
  20770. currentLevel = -1;
  20771. break;
  20772. }
  20773. }
  20774. return names;
  20775. }
  20776. };
  20777. };
  20778. // plugins/formatmatch.js
  20779. /**
  20780. * 格式刷,只格式inline的
  20781. * @file
  20782. * @since 1.2.6.1
  20783. */
  20784. /**
  20785. * 格式刷
  20786. * @command formatmatch
  20787. * @method execCommand
  20788. * @remind 该操作不能复制段落格式
  20789. * @param { String } cmd 命令字符串
  20790. * @example
  20791. * ```javascript
  20792. * //editor是编辑器实例
  20793. * //获取格式刷
  20794. * editor.execCommand( 'formatmatch' );
  20795. * ```
  20796. */
  20797. UE.plugins['formatmatch'] = function(){
  20798. var me = this,
  20799. list = [],img,
  20800. flag = 0;
  20801. me.addListener('reset',function(){
  20802. list = [];
  20803. flag = 0;
  20804. });
  20805. function addList(type,evt){
  20806. if(browser.webkit){
  20807. var target = evt.target.tagName == 'IMG' ? evt.target : null;
  20808. }
  20809. function addFormat(range){
  20810. if(text){
  20811. range.selectNode(text);
  20812. }
  20813. return range.applyInlineStyle(list[list.length-1].tagName,null,list);
  20814. }
  20815. me.undoManger && me.undoManger.save();
  20816. var range = me.selection.getRange(),
  20817. imgT = target || range.getClosedNode();
  20818. if(img && imgT && imgT.tagName == 'IMG'){
  20819. //trace:964
  20820. imgT.style.cssText += ';float:' + (img.style.cssFloat || img.style.styleFloat ||'none') + ';display:' + (img.style.display||'inline');
  20821. img = null;
  20822. }else{
  20823. if(!img){
  20824. var collapsed = range.collapsed;
  20825. if(collapsed){
  20826. var text = me.document.createTextNode('match');
  20827. range.insertNode(text).select();
  20828. }
  20829. me.__hasEnterExecCommand = true;
  20830. //不能把block上的属性干掉
  20831. //trace:1553
  20832. var removeFormatAttributes = me.options.removeFormatAttributes;
  20833. me.options.removeFormatAttributes = '';
  20834. me.execCommand('removeformat');
  20835. me.options.removeFormatAttributes = removeFormatAttributes;
  20836. me.__hasEnterExecCommand = false;
  20837. //trace:969
  20838. range = me.selection.getRange();
  20839. if(list.length){
  20840. addFormat(range);
  20841. }
  20842. if(text){
  20843. range.setStartBefore(text).collapse(true);
  20844. }
  20845. range.select();
  20846. text && domUtils.remove(text);
  20847. }
  20848. }
  20849. me.undoManger && me.undoManger.save();
  20850. me.removeListener('mouseup',addList);
  20851. flag = 0;
  20852. }
  20853. me.commands['formatmatch'] = {
  20854. execCommand : function( cmdName ) {
  20855. if(flag){
  20856. flag = 0;
  20857. list = [];
  20858. me.removeListener('mouseup',addList);
  20859. return;
  20860. }
  20861. var range = me.selection.getRange();
  20862. img = range.getClosedNode();
  20863. if(!img || img.tagName != 'IMG'){
  20864. range.collapse(true).shrinkBoundary();
  20865. var start = range.startContainer;
  20866. list = domUtils.findParents(start,true,function(node){
  20867. return !domUtils.isBlockElm(node) && node.nodeType == 1;
  20868. });
  20869. //a不能加入格式刷, 并且克隆节点
  20870. for(var i=0,ci;ci=list[i];i++){
  20871. if(ci.tagName == 'A'){
  20872. list.splice(i,1);
  20873. break;
  20874. }
  20875. }
  20876. }
  20877. me.addListener('mouseup',addList);
  20878. flag = 1;
  20879. },
  20880. queryCommandState : function() {
  20881. return flag;
  20882. },
  20883. notNeedUndo : 1
  20884. };
  20885. };
  20886. // plugins/searchreplace.js
  20887. ///import core
  20888. ///commands 查找替换
  20889. ///commandsName SearchReplace
  20890. ///commandsTitle 查询替换
  20891. ///commandsDialog dialogs\searchreplace
  20892. /**
  20893. * @description 查找替换
  20894. * @author zhanyi
  20895. */
  20896. UE.plugin.register('searchreplace',function(){
  20897. var me = this;
  20898. var _blockElm = {'table':1,'tbody':1,'tr':1,'ol':1,'ul':1};
  20899. function findTextInString(textContent,opt,currentIndex){
  20900. var str = opt.searchStr;
  20901. if(opt.dir == -1){
  20902. textContent = textContent.split('').reverse().join('');
  20903. str = str.split('').reverse().join('');
  20904. currentIndex = textContent.length - currentIndex;
  20905. }
  20906. var reg = new RegExp(str,'g' + (opt.casesensitive ? '' : 'i')),match;
  20907. while(match = reg.exec(textContent)){
  20908. if(match.index >= currentIndex){
  20909. return opt.dir == -1 ? textContent.length - match.index - opt.searchStr.length : match.index;
  20910. }
  20911. }
  20912. return -1
  20913. }
  20914. function findTextBlockElm(node,currentIndex,opt){
  20915. var textContent,index,methodName = opt.all || opt.dir == 1 ? 'getNextDomNode' : 'getPreDomNode';
  20916. if(domUtils.isBody(node)){
  20917. node = node.firstChild;
  20918. }
  20919. var first = 1;
  20920. while(node){
  20921. textContent = node.nodeType == 3 ? node.nodeValue : node[browser.ie ? 'innerText' : 'textContent'];
  20922. index = findTextInString(textContent,opt,currentIndex );
  20923. first = 0;
  20924. if(index!=-1){
  20925. return {
  20926. 'node':node,
  20927. 'index':index
  20928. }
  20929. }
  20930. node = domUtils[methodName](node);
  20931. while(node && _blockElm[node.nodeName.toLowerCase()]){
  20932. node = domUtils[methodName](node,true);
  20933. }
  20934. if(node){
  20935. currentIndex = opt.dir == -1 ? (node.nodeType == 3 ? node.nodeValue : node[browser.ie ? 'innerText' : 'textContent']).length : 0;
  20936. }
  20937. }
  20938. }
  20939. function findNTextInBlockElm(node,index,str){
  20940. var currentIndex = 0,
  20941. currentNode = node.firstChild,
  20942. currentNodeLength = 0,
  20943. result;
  20944. while(currentNode){
  20945. if(currentNode.nodeType == 3){
  20946. currentNodeLength = currentNode.nodeValue.replace(/(^[\t\r\n]+)|([\t\r\n]+$)/,'').length;
  20947. currentIndex += currentNodeLength;
  20948. if(currentIndex >= index){
  20949. return {
  20950. 'node':currentNode,
  20951. 'index': currentNodeLength - (currentIndex - index)
  20952. }
  20953. }
  20954. }else if(!dtd.$empty[currentNode.tagName]){
  20955. currentNodeLength = currentNode[browser.ie ? 'innerText' : 'textContent'].replace(/(^[\t\r\n]+)|([\t\r\n]+$)/,'').length
  20956. currentIndex += currentNodeLength;
  20957. if(currentIndex >= index){
  20958. result = findNTextInBlockElm(currentNode,currentNodeLength - (currentIndex - index),str);
  20959. if(result){
  20960. return result;
  20961. }
  20962. }
  20963. }
  20964. currentNode = domUtils.getNextDomNode(currentNode);
  20965. }
  20966. }
  20967. function searchReplace(me,opt){
  20968. var rng = me.selection.getRange(),
  20969. startBlockNode,
  20970. searchStr = opt.searchStr,
  20971. span = me.document.createElement('span');
  20972. span.innerHTML = '$$ueditor_searchreplace_key$$';
  20973. rng.shrinkBoundary(true);
  20974. //判断是不是第一次选中
  20975. if(!rng.collapsed){
  20976. rng.select();
  20977. var rngText = me.selection.getText();
  20978. if(new RegExp('^' + opt.searchStr + '$',(opt.casesensitive ? '' : 'i')).test(rngText)){
  20979. if(opt.replaceStr != undefined){
  20980. replaceText(rng,opt.replaceStr);
  20981. rng.select();
  20982. return true;
  20983. }else{
  20984. rng.collapse(opt.dir == -1)
  20985. }
  20986. }
  20987. }
  20988. rng.insertNode(span);
  20989. rng.enlargeToBlockElm(true);
  20990. startBlockNode = rng.startContainer;
  20991. var currentIndex = startBlockNode[browser.ie ? 'innerText' : 'textContent'].indexOf('$$ueditor_searchreplace_key$$');
  20992. rng.setStartBefore(span);
  20993. domUtils.remove(span);
  20994. var result = findTextBlockElm(startBlockNode,currentIndex,opt);
  20995. if(result){
  20996. var rngStart = findNTextInBlockElm(result.node,result.index,searchStr);
  20997. var rngEnd = findNTextInBlockElm(result.node,result.index + searchStr.length,searchStr);
  20998. rng.setStart(rngStart.node,rngStart.index).setEnd(rngEnd.node,rngEnd.index);
  20999. if(opt.replaceStr !== undefined){
  21000. replaceText(rng,opt.replaceStr)
  21001. }
  21002. rng.select();
  21003. return true;
  21004. }else{
  21005. rng.setCursor()
  21006. }
  21007. }
  21008. function replaceText(rng,str){
  21009. str = me.document.createTextNode(str);
  21010. rng.deleteContents().insertNode(str);
  21011. }
  21012. return {
  21013. commands:{
  21014. 'searchreplace':{
  21015. execCommand:function(cmdName,opt){
  21016. utils.extend(opt,{
  21017. all : false,
  21018. casesensitive : false,
  21019. dir : 1
  21020. },true);
  21021. var num = 0;
  21022. if(opt.all){
  21023. var rng = me.selection.getRange(),
  21024. first = me.body.firstChild;
  21025. if(first && first.nodeType == 1){
  21026. rng.setStart(first,0);
  21027. rng.shrinkBoundary(true);
  21028. }else if(first.nodeType == 3){
  21029. rng.setStartBefore(first)
  21030. }
  21031. rng.collapse(true).select(true);
  21032. if(opt.replaceStr !== undefined){
  21033. me.fireEvent('saveScene');
  21034. }
  21035. while(searchReplace(this,opt)){
  21036. num++;
  21037. }
  21038. if(num){
  21039. me.fireEvent('saveScene');
  21040. }
  21041. }else{
  21042. if(opt.replaceStr !== undefined){
  21043. me.fireEvent('saveScene');
  21044. }
  21045. if(searchReplace(this,opt)){
  21046. num++
  21047. }
  21048. if(num){
  21049. me.fireEvent('saveScene');
  21050. }
  21051. }
  21052. return num;
  21053. },
  21054. notNeedUndo:1
  21055. }
  21056. }
  21057. }
  21058. });
  21059. // plugins/customstyle.js
  21060. /**
  21061. * 自定义样式
  21062. * @file
  21063. * @since 1.2.6.1
  21064. */
  21065. /**
  21066. * 根据config配置文件里“customstyle”选项的值对匹配的标签执行样式替换。
  21067. * @command customstyle
  21068. * @method execCommand
  21069. * @param { String } cmd 命令字符串
  21070. * @example
  21071. * ```javascript
  21072. * editor.execCommand( 'customstyle' );
  21073. * ```
  21074. */
  21075. UE.plugins['customstyle'] = function() {
  21076. var me = this;
  21077. me.setOpt({ 'customstyle':[
  21078. {tag:'h1',name:'tc', 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;'},
  21079. {tag:'h1',name:'tl', 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;'},
  21080. {tag:'span',name:'im', style:'font-size:16px;font-style:italic;font-weight:bold;line-height:18px;'},
  21081. {tag:'span',name:'hi', style:'font-size:16px;font-style:italic;font-weight:bold;color:rgb(51, 153, 204);line-height:18px;'}
  21082. ]});
  21083. me.commands['customstyle'] = {
  21084. execCommand : function(cmdName, obj) {
  21085. var me = this,
  21086. tagName = obj.tag,
  21087. node = domUtils.findParent(me.selection.getStart(), function(node) {
  21088. return node.getAttribute('label');
  21089. }, true),
  21090. range,bk,tmpObj = {};
  21091. for (var p in obj) {
  21092. if(obj[p]!==undefined)
  21093. tmpObj[p] = obj[p];
  21094. }
  21095. delete tmpObj.tag;
  21096. if (node && node.getAttribute('label') == obj.label) {
  21097. range = this.selection.getRange();
  21098. bk = range.createBookmark();
  21099. if (range.collapsed) {
  21100. //trace:1732 删掉自定义标签,要有p来回填站位
  21101. if(dtd.$block[node.tagName]){
  21102. var fillNode = me.document.createElement('p');
  21103. domUtils.moveChild(node, fillNode);
  21104. node.parentNode.insertBefore(fillNode, node);
  21105. domUtils.remove(node);
  21106. }else{
  21107. domUtils.remove(node,true);
  21108. }
  21109. } else {
  21110. var common = domUtils.getCommonAncestor(bk.start, bk.end),
  21111. nodes = domUtils.getElementsByTagName(common, tagName);
  21112. if(new RegExp(tagName,'i').test(common.tagName)){
  21113. nodes.push(common);
  21114. }
  21115. for (var i = 0,ni; ni = nodes[i++];) {
  21116. if (ni.getAttribute('label') == obj.label) {
  21117. var ps = domUtils.getPosition(ni, bk.start),pe = domUtils.getPosition(ni, bk.end);
  21118. if ((ps & domUtils.POSITION_FOLLOWING || ps & domUtils.POSITION_CONTAINS)
  21119. &&
  21120. (pe & domUtils.POSITION_PRECEDING || pe & domUtils.POSITION_CONTAINS)
  21121. )
  21122. if (dtd.$block[tagName]) {
  21123. var fillNode = me.document.createElement('p');
  21124. domUtils.moveChild(ni, fillNode);
  21125. ni.parentNode.insertBefore(fillNode, ni);
  21126. }
  21127. domUtils.remove(ni, true);
  21128. }
  21129. }
  21130. node = domUtils.findParent(common, function(node) {
  21131. return node.getAttribute('label') == obj.label;
  21132. }, true);
  21133. if (node) {
  21134. domUtils.remove(node, true);
  21135. }
  21136. }
  21137. range.moveToBookmark(bk).select();
  21138. } else {
  21139. if (dtd.$block[tagName]) {
  21140. this.execCommand('paragraph', tagName, tmpObj,'customstyle');
  21141. range = me.selection.getRange();
  21142. if (!range.collapsed) {
  21143. range.collapse();
  21144. node = domUtils.findParent(me.selection.getStart(), function(node) {
  21145. return node.getAttribute('label') == obj.label;
  21146. }, true);
  21147. var pNode = me.document.createElement('p');
  21148. domUtils.insertAfter(node, pNode);
  21149. domUtils.fillNode(me.document, pNode);
  21150. range.setStart(pNode, 0).setCursor();
  21151. }
  21152. } else {
  21153. range = me.selection.getRange();
  21154. if (range.collapsed) {
  21155. node = me.document.createElement(tagName);
  21156. domUtils.setAttributes(node, tmpObj);
  21157. range.insertNode(node).setStart(node, 0).setCursor();
  21158. return;
  21159. }
  21160. bk = range.createBookmark();
  21161. range.applyInlineStyle(tagName, tmpObj).moveToBookmark(bk).select();
  21162. }
  21163. }
  21164. },
  21165. queryCommandValue : function() {
  21166. var parent = domUtils.filterNodeList(
  21167. this.selection.getStartElementPath(),
  21168. function(node){return node.getAttribute('label')}
  21169. );
  21170. return parent ? parent.getAttribute('label') : '';
  21171. }
  21172. };
  21173. //当去掉customstyle是,如果是块元素,用p代替
  21174. me.addListener('keyup', function(type, evt) {
  21175. var keyCode = evt.keyCode || evt.which;
  21176. if (keyCode == 32 || keyCode == 13) {
  21177. var range = me.selection.getRange();
  21178. if (range.collapsed) {
  21179. var node = domUtils.findParent(me.selection.getStart(), function(node) {
  21180. return node.getAttribute('label');
  21181. }, true);
  21182. if (node && dtd.$block[node.tagName] && domUtils.isEmptyNode(node)) {
  21183. var p = me.document.createElement('p');
  21184. domUtils.insertAfter(node, p);
  21185. domUtils.fillNode(me.document, p);
  21186. domUtils.remove(node);
  21187. range.setStart(p, 0).setCursor();
  21188. }
  21189. }
  21190. }
  21191. });
  21192. };
  21193. // plugins/catchremoteimage.js
  21194. ///import core
  21195. ///commands 远程图片抓取
  21196. ///commandsName catchRemoteImage,catchremoteimageenable
  21197. ///commandsTitle 远程图片抓取
  21198. /**
  21199. * 远程图片抓取,当开启本插件时所有不符合本地域名的图片都将被抓取成为本地服务器上的图片
  21200. */
  21201. UE.plugins['catchremoteimage'] = function () {
  21202. var me = this,
  21203. ajax = UE.ajax;
  21204. /* 设置默认值 */
  21205. if (me.options.catchRemoteImageEnable === false) return;
  21206. me.setOpt({
  21207. catchRemoteImageEnable: false
  21208. });
  21209. me.addListener("afterpaste", function () {
  21210. me.fireEvent("catchRemoteImage");
  21211. });
  21212. me.addListener("catchRemoteImage", function () {
  21213. var catcherLocalDomain = me.getOpt('catcherLocalDomain'),
  21214. catcherActionUrl = me.getActionUrl(me.getOpt('catcherActionName')),
  21215. catcherUrlPrefix = me.getOpt('catcherUrlPrefix'),
  21216. catcherFieldName = me.getOpt('catcherFieldName');
  21217. var remoteImages = [],
  21218. imgs = domUtils.getElementsByTagName(me.document, "img"),
  21219. test = function (src, urls) {
  21220. if (src.indexOf(location.host) != -1 || /(^\.)|(^\/)/.test(src)) {
  21221. return true;
  21222. }
  21223. if (urls) {
  21224. for (var j = 0, url; url = urls[j++];) {
  21225. if (src.indexOf(url) !== -1) {
  21226. return true;
  21227. }
  21228. }
  21229. }
  21230. return false;
  21231. };
  21232. for (var i = 0, ci; ci = imgs[i++];) {
  21233. if (ci.getAttribute("word_img")) {
  21234. continue;
  21235. }
  21236. var src = ci.getAttribute("_src") || ci.src || "";
  21237. if (/^(https?|ftp):/i.test(src) && !test(src, catcherLocalDomain)) {
  21238. remoteImages.push(src);
  21239. }
  21240. }
  21241. if (remoteImages.length) {
  21242. catchremoteimage(remoteImages, {
  21243. //成功抓取
  21244. success: function (r) {
  21245. try {
  21246. var info = r.state !== undefined ? r:eval("(" + r.responseText + ")");
  21247. } catch (e) {
  21248. return;
  21249. }
  21250. /* 获取源路径和新路径 */
  21251. var i, j, ci, cj, oldSrc, newSrc, list = info.list;
  21252. for (i = 0; ci = imgs[i++];) {
  21253. oldSrc = ci.getAttribute("_src") || ci.src || "";
  21254. for (j = 0; cj = list[j++];) {
  21255. if (oldSrc == cj.source && cj.state == "SUCCESS") { //抓取失败时不做替换处理
  21256. newSrc = catcherUrlPrefix + cj.url;
  21257. domUtils.setAttributes(ci, {
  21258. "src": newSrc,
  21259. "_src": newSrc
  21260. });
  21261. break;
  21262. }
  21263. }
  21264. }
  21265. me.fireEvent('catchremotesuccess')
  21266. },
  21267. //回调失败,本次请求超时
  21268. error: function () {
  21269. me.fireEvent("catchremoteerror");
  21270. }
  21271. });
  21272. }
  21273. function catchremoteimage(imgs, callbacks) {
  21274. var params = utils.serializeParam(me.queryCommandValue('serverparam')) || '',
  21275. url = utils.formatUrl(catcherActionUrl + (catcherActionUrl.indexOf('?') == -1 ? '?':'&') + params),
  21276. isJsonp = utils.isCrossDomainUrl(url),
  21277. opt = {
  21278. 'method': 'POST',
  21279. 'dataType': isJsonp ? 'jsonp':'',
  21280. 'timeout': 60000, //单位:毫秒,回调请求超时设置。目标用户如果网速不是很快的话此处建议设置一个较大的数值
  21281. 'onsuccess': callbacks["success"],
  21282. 'onerror': callbacks["error"]
  21283. };
  21284. opt[catcherFieldName] = imgs;
  21285. ajax.request(url, opt);
  21286. }
  21287. });
  21288. };
  21289. // plugins/snapscreen.js
  21290. /**
  21291. * 截屏插件,为UEditor提供插入支持
  21292. * @file
  21293. * @since 1.4.2
  21294. */
  21295. UE.plugin.register('snapscreen', function (){
  21296. var me = this;
  21297. var snapplugin;
  21298. function getLocation(url){
  21299. var search,
  21300. a = document.createElement('a'),
  21301. params = utils.serializeParam(me.queryCommandValue('serverparam')) || '';
  21302. a.href = url;
  21303. if (browser.ie) {
  21304. a.href = a.href;
  21305. }
  21306. search = a.search;
  21307. if (params) {
  21308. search = search + (search.indexOf('?') == -1 ? '?':'&')+ params;
  21309. search = search.replace(/[&]+/ig, '&');
  21310. }
  21311. return {
  21312. 'port': a.port,
  21313. 'hostname': a.hostname,
  21314. 'path': a.pathname + search || + a.hash
  21315. }
  21316. }
  21317. return {
  21318. commands:{
  21319. /**
  21320. * 字体背景颜色
  21321. * @command snapscreen
  21322. * @method execCommand
  21323. * @param { String } cmd 命令字符串
  21324. * @example
  21325. * ```javascript
  21326. * editor.execCommand('snapscreen');
  21327. * ```
  21328. */
  21329. 'snapscreen':{
  21330. execCommand:function (cmd) {
  21331. var url, local, res;
  21332. var lang = me.getLang("snapScreen_plugin");
  21333. if(!snapplugin){
  21334. var container = me.container;
  21335. var doc = me.container.ownerDocument || me.container.document;
  21336. snapplugin = doc.createElement("object");
  21337. try{snapplugin.type = "application/x-pluginbaidusnap";}catch(e){
  21338. return;
  21339. }
  21340. snapplugin.style.cssText = "position:absolute;left:-9999px;width:0;height:0;";
  21341. snapplugin.setAttribute("width","0");
  21342. snapplugin.setAttribute("height","0");
  21343. container.appendChild(snapplugin);
  21344. }
  21345. function onSuccess(rs){
  21346. try{
  21347. rs = eval("("+ rs +")");
  21348. if(rs.state == 'SUCCESS'){
  21349. var opt = me.options;
  21350. me.execCommand('insertimage', {
  21351. src: opt.snapscreenUrlPrefix + rs.url,
  21352. _src: opt.snapscreenUrlPrefix + rs.url,
  21353. alt: rs.title || '',
  21354. floatStyle: opt.snapscreenImgAlign
  21355. });
  21356. } else {
  21357. alert(rs.state);
  21358. }
  21359. }catch(e){
  21360. alert(lang.callBackErrorMsg);
  21361. }
  21362. }
  21363. url = me.getActionUrl(me.getOpt('snapscreenActionName'));
  21364. local = getLocation(url);
  21365. setTimeout(function () {
  21366. try{
  21367. res =snapplugin.saveSnapshot(local.hostname, local.path, local.port);
  21368. }catch(e){
  21369. me.ui._dialogs['snapscreenDialog'].open();
  21370. return;
  21371. }
  21372. onSuccess(res);
  21373. }, 50);
  21374. },
  21375. queryCommandState: function(){
  21376. return (navigator.userAgent.indexOf("Windows",0) != -1) ? 0:-1;
  21377. }
  21378. }
  21379. }
  21380. }
  21381. });
  21382. // plugins/insertparagraph.js
  21383. /**
  21384. * 插入段落
  21385. * @file
  21386. * @since 1.2.6.1
  21387. */
  21388. /**
  21389. * 插入段落
  21390. * @command insertparagraph
  21391. * @method execCommand
  21392. * @param { String } cmd 命令字符串
  21393. * @example
  21394. * ```javascript
  21395. * //editor是编辑器实例
  21396. * editor.execCommand( 'insertparagraph' );
  21397. * ```
  21398. */
  21399. UE.commands['insertparagraph'] = {
  21400. execCommand : function( cmdName,front) {
  21401. var me = this,
  21402. range = me.selection.getRange(),
  21403. start = range.startContainer,tmpNode;
  21404. while(start ){
  21405. if(domUtils.isBody(start)){
  21406. break;
  21407. }
  21408. tmpNode = start;
  21409. start = start.parentNode;
  21410. }
  21411. if(tmpNode){
  21412. var p = me.document.createElement('p');
  21413. if(front){
  21414. tmpNode.parentNode.insertBefore(p,tmpNode)
  21415. }else{
  21416. tmpNode.parentNode.insertBefore(p,tmpNode.nextSibling)
  21417. }
  21418. domUtils.fillNode(me.document,p);
  21419. range.setStart(p,0).setCursor(false,true);
  21420. }
  21421. }
  21422. };
  21423. // plugins/webapp.js
  21424. /**
  21425. * 百度应用
  21426. * @file
  21427. * @since 1.2.6.1
  21428. */
  21429. /**
  21430. * 插入百度应用
  21431. * @command webapp
  21432. * @method execCommand
  21433. * @remind 需要百度APPKey
  21434. * @remind 百度应用主页: <a href="http://app.baidu.com/" target="_blank">http://app.baidu.com/</a>
  21435. * @param { Object } appOptions 应用所需的参数项, 支持的key有: title=>应用标题, width=>应用容器宽度,
  21436. * height=>应用容器高度,logo=>应用logo,url=>应用地址
  21437. * @example
  21438. * ```javascript
  21439. * //editor是编辑器实例
  21440. * //在编辑器里插入一个“植物大战僵尸”的APP
  21441. * editor.execCommand( 'webapp' , {
  21442. * title: '植物大战僵尸',
  21443. * width: 560,
  21444. * height: 465,
  21445. * logo: '应用展示的图片',
  21446. * url: '百度应用的地址'
  21447. * } );
  21448. * ```
  21449. */
  21450. //UE.plugins['webapp'] = function () {
  21451. // var me = this;
  21452. // function createInsertStr( obj, toIframe, addParagraph ) {
  21453. // return !toIframe ?
  21454. // (addParagraph ? '<p>' : '') + '<img title="'+obj.title+'" width="' + obj.width + '" height="' + obj.height + '"' +
  21455. // ' 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 + '" />' +
  21456. // (addParagraph ? '</p>' : '')
  21457. // :
  21458. // '<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>';
  21459. // }
  21460. //
  21461. // function switchImgAndIframe( img2frame ) {
  21462. // var tmpdiv,
  21463. // nodes = domUtils.getElementsByTagName( me.document, !img2frame ? "iframe" : "img" );
  21464. // for ( var i = 0, node; node = nodes[i++]; ) {
  21465. // if ( node.className != "edui-faked-webapp" ){
  21466. // continue;
  21467. // }
  21468. // tmpdiv = me.document.createElement( "div" );
  21469. // 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 );
  21470. // node.parentNode.replaceChild( tmpdiv.firstChild, node );
  21471. // }
  21472. // }
  21473. //
  21474. // me.addListener( "beforegetcontent", function () {
  21475. // switchImgAndIframe( true );
  21476. // } );
  21477. // me.addListener( 'aftersetcontent', function () {
  21478. // switchImgAndIframe( false );
  21479. // } );
  21480. // me.addListener( 'aftergetcontent', function ( cmdName ) {
  21481. // if ( cmdName == 'aftergetcontent' && me.queryCommandState( 'source' ) ){
  21482. // return;
  21483. // }
  21484. // switchImgAndIframe( false );
  21485. // } );
  21486. //
  21487. // me.commands['webapp'] = {
  21488. // execCommand:function ( cmd, obj ) {
  21489. // me.execCommand( "inserthtml", createInsertStr( obj, false,true ) );
  21490. // }
  21491. // };
  21492. //};
  21493. UE.plugin.register('webapp', function (){
  21494. var me = this;
  21495. function createInsertStr(obj,toEmbed){
  21496. return !toEmbed ?
  21497. '<img title="'+obj.title+'" width="' + obj.width + '" height="' + obj.height + '"' +
  21498. ' src="' + me.options.UEDITOR_HOME_URL + 'themes/default/images/spacer.gif" _logo_url="'+obj.logo+'" style="background:url(' + obj.logo
  21499. +') no-repeat center center; border:1px solid gray;" class="edui-faked-webapp" _url="' + obj.url + '" ' +
  21500. (obj.align && !obj.cssfloat? 'align="' + obj.align + '"' : '') +
  21501. (obj.cssfloat ? 'style="float:' + obj.cssfloat + '"' : '') +
  21502. '/>'
  21503. :
  21504. '<iframe class="edui-faked-webapp" title="'+obj.title+'" ' +
  21505. (obj.align && !obj.cssfloat? 'align="' + obj.align + '"' : '') +
  21506. (obj.cssfloat ? 'style="float:' + obj.cssfloat + '"' : '') +
  21507. 'width="' + obj.width + '" height="' + obj.height + '" scrolling="no" frameborder="0" src="' + obj.url + '" logo_url = "'+obj.logo+'"></iframe>'
  21508. }
  21509. return {
  21510. outputRule: function(root){
  21511. utils.each(root.getNodesByTagName('img'),function(node){
  21512. var html;
  21513. if(node.getAttr('class') == 'edui-faked-webapp'){
  21514. html = createInsertStr({
  21515. title:node.getAttr('title'),
  21516. 'width':node.getAttr('width'),
  21517. 'height':node.getAttr('height'),
  21518. 'align':node.getAttr('align'),
  21519. 'cssfloat':node.getStyle('float'),
  21520. 'url':node.getAttr("_url"),
  21521. 'logo':node.getAttr('_logo_url')
  21522. },true);
  21523. var embed = UE.uNode.createElement(html);
  21524. node.parentNode.replaceChild(embed,node);
  21525. }
  21526. })
  21527. },
  21528. inputRule:function(root){
  21529. utils.each(root.getNodesByTagName('iframe'),function(node){
  21530. if(node.getAttr('class') == 'edui-faked-webapp'){
  21531. var img = UE.uNode.createElement(createInsertStr({
  21532. title:node.getAttr('title'),
  21533. 'width':node.getAttr('width'),
  21534. 'height':node.getAttr('height'),
  21535. 'align':node.getAttr('align'),
  21536. 'cssfloat':node.getStyle('float'),
  21537. 'url':node.getAttr("src"),
  21538. 'logo':node.getAttr('logo_url')
  21539. }));
  21540. node.parentNode.replaceChild(img,node);
  21541. }
  21542. })
  21543. },
  21544. commands:{
  21545. /**
  21546. * 插入百度应用
  21547. * @command webapp
  21548. * @method execCommand
  21549. * @remind 需要百度APPKey
  21550. * @remind 百度应用主页: <a href="http://app.baidu.com/" target="_blank">http://app.baidu.com/</a>
  21551. * @param { Object } appOptions 应用所需的参数项, 支持的key有: title=>应用标题, width=>应用容器宽度,
  21552. * height=>应用容器高度,logo=>应用logo,url=>应用地址
  21553. * @example
  21554. * ```javascript
  21555. * //editor是编辑器实例
  21556. * //在编辑器里插入一个“植物大战僵尸”的APP
  21557. * editor.execCommand( 'webapp' , {
  21558. * title: '植物大战僵尸',
  21559. * width: 560,
  21560. * height: 465,
  21561. * logo: '应用展示的图片',
  21562. * url: '百度应用的地址'
  21563. * } );
  21564. * ```
  21565. */
  21566. 'webapp':{
  21567. execCommand:function (cmd, obj) {
  21568. var me = this,
  21569. str = createInsertStr(utils.extend(obj,{
  21570. align:'none'
  21571. }), false);
  21572. me.execCommand("inserthtml",str);
  21573. },
  21574. queryCommandState:function () {
  21575. var me = this,
  21576. img = me.selection.getRange().getClosedNode(),
  21577. flag = img && (img.className == "edui-faked-webapp");
  21578. return flag ? 1 : 0;
  21579. }
  21580. }
  21581. }
  21582. }
  21583. });
  21584. // plugins/template.js
  21585. ///import core
  21586. ///import plugins\inserthtml.js
  21587. ///import plugins\cleardoc.js
  21588. ///commands 模板
  21589. ///commandsName template
  21590. ///commandsTitle 模板
  21591. ///commandsDialog dialogs\template
  21592. UE.plugins['template'] = function () {
  21593. UE.commands['template'] = {
  21594. execCommand:function (cmd, obj) {
  21595. obj.html && this.execCommand("inserthtml", obj.html);
  21596. }
  21597. };
  21598. this.addListener("click", function (type, evt) {
  21599. var el = evt.target || evt.srcElement,
  21600. range = this.selection.getRange();
  21601. var tnode = domUtils.findParent(el, function (node) {
  21602. if (node.className && domUtils.hasClass(node, "ue_t")) {
  21603. return node;
  21604. }
  21605. }, true);
  21606. tnode && range.selectNode(tnode).shrinkBoundary().select();
  21607. });
  21608. this.addListener("keydown", function (type, evt) {
  21609. var range = this.selection.getRange();
  21610. if (!range.collapsed) {
  21611. if (!evt.ctrlKey && !evt.metaKey && !evt.shiftKey && !evt.altKey) {
  21612. var tnode = domUtils.findParent(range.startContainer, function (node) {
  21613. if (node.className && domUtils.hasClass(node, "ue_t")) {
  21614. return node;
  21615. }
  21616. }, true);
  21617. if (tnode) {
  21618. domUtils.removeClasses(tnode, ["ue_t"]);
  21619. }
  21620. }
  21621. }
  21622. });
  21623. };
  21624. // plugins/music.js
  21625. /**
  21626. * 插入音乐命令
  21627. * @file
  21628. */
  21629. UE.plugin.register('music', function (){
  21630. var me = this;
  21631. function creatInsertStr(url,width,height,align,cssfloat,toEmbed){
  21632. return !toEmbed ?
  21633. '<img ' +
  21634. (align && !cssfloat? 'align="' + align + '"' : '') +
  21635. (cssfloat ? 'style="float:' + cssfloat + '"' : '') +
  21636. ' width="'+ width +'" height="' + height + '" _url="'+url+'" class="edui-faked-music"' +
  21637. ' src="'+me.options.langPath+me.options.lang+'/images/music.png" />'
  21638. :
  21639. '<embed type="application/x-shockwave-flash" class="edui-faked-music" pluginspage="http://www.macromedia.com/go/getflashplayer"' +
  21640. ' src="' + url + '" width="' + width + '" height="' + height + '" '+ (align && !cssfloat? 'align="' + align + '"' : '') +
  21641. (cssfloat ? 'style="float:' + cssfloat + '"' : '') +
  21642. ' wmode="transparent" play="true" loop="false" menu="false" allowscriptaccess="never" allowfullscreen="true" >';
  21643. }
  21644. return {
  21645. outputRule: function(root){
  21646. utils.each(root.getNodesByTagName('img'),function(node){
  21647. var html;
  21648. if(node.getAttr('class') == 'edui-faked-music'){
  21649. var cssfloat = node.getStyle('float');
  21650. var align = node.getAttr('align');
  21651. html = creatInsertStr(node.getAttr("_url"), node.getAttr('width'), node.getAttr('height'), align, cssfloat, true);
  21652. var embed = UE.uNode.createElement(html);
  21653. node.parentNode.replaceChild(embed,node);
  21654. }
  21655. })
  21656. },
  21657. inputRule:function(root){
  21658. utils.each(root.getNodesByTagName('embed'),function(node){
  21659. if(node.getAttr('class') == 'edui-faked-music'){
  21660. var cssfloat = node.getStyle('float');
  21661. var align = node.getAttr('align');
  21662. html = creatInsertStr(node.getAttr("src"), node.getAttr('width'), node.getAttr('height'), align, cssfloat,false);
  21663. var img = UE.uNode.createElement(html);
  21664. node.parentNode.replaceChild(img,node);
  21665. }
  21666. })
  21667. },
  21668. commands:{
  21669. /**
  21670. * 插入音乐
  21671. * @command music
  21672. * @method execCommand
  21673. * @param { Object } musicOptions 插入音乐的参数项, 支持的key有: url=>音乐地址;
  21674. * width=>音乐容器宽度;height=>音乐容器高度;align=>音乐文件的对齐方式, 可选值有: left, center, right, none
  21675. * @example
  21676. * ```javascript
  21677. * //editor是编辑器实例
  21678. * //在编辑器里插入一个“植物大战僵尸”的APP
  21679. * editor.execCommand( 'music' , {
  21680. * width: 400,
  21681. * height: 95,
  21682. * align: "center",
  21683. * url: "音乐地址"
  21684. * } );
  21685. * ```
  21686. */
  21687. 'music':{
  21688. execCommand:function (cmd, musicObj) {
  21689. var me = this,
  21690. str = creatInsertStr(musicObj.url, musicObj.width || 400, musicObj.height || 95, "none", false);
  21691. me.execCommand("inserthtml",str);
  21692. },
  21693. queryCommandState:function () {
  21694. var me = this,
  21695. img = me.selection.getRange().getClosedNode(),
  21696. flag = img && (img.className == "edui-faked-music");
  21697. return flag ? 1 : 0;
  21698. }
  21699. }
  21700. }
  21701. }
  21702. });
  21703. // plugins/autoupload.js
  21704. /**
  21705. * @description
  21706. * 1.拖放文件到编辑区域,自动上传并插入到选区
  21707. * 2.插入粘贴板的图片,自动上传并插入到选区
  21708. * @author Jinqn
  21709. * @date 2013-10-14
  21710. */
  21711. UE.plugin.register('autoupload', function (){
  21712. function sendAndInsertFile(file, editor) {
  21713. var me = editor;
  21714. //模拟数据
  21715. var fieldName, urlPrefix, maxSize, allowFiles, actionUrl,
  21716. loadingHtml, errorHandler, successHandler,
  21717. filetype = /image\/\w+/i.test(file.type) ? 'image':'file',
  21718. loadingId = 'loading_' + (+new Date()).toString(36);
  21719. fieldName = me.getOpt(filetype + 'FieldName');
  21720. urlPrefix = me.getOpt(filetype + 'UrlPrefix');
  21721. maxSize = me.getOpt(filetype + 'MaxSize');
  21722. allowFiles = me.getOpt(filetype + 'AllowFiles');
  21723. actionUrl = me.getActionUrl(me.getOpt(filetype + 'ActionName'));
  21724. errorHandler = function(title) {
  21725. var loader = me.document.getElementById(loadingId);
  21726. loader && domUtils.remove(loader);
  21727. me.fireEvent('showmessage', {
  21728. 'id': loadingId,
  21729. 'content': title,
  21730. 'type': 'error',
  21731. 'timeout': 4000
  21732. });
  21733. };
  21734. if (filetype == 'image') {
  21735. loadingHtml = '<img class="loadingclass" id="' + loadingId + '" src="' +
  21736. me.options.themePath + me.options.theme +
  21737. '/images/spacer.gif" title="' + (me.getLang('autoupload.loading') || '') + '" >';
  21738. successHandler = function(data) {
  21739. var link = urlPrefix + data.url,
  21740. loader = me.document.getElementById(loadingId);
  21741. if (loader) {
  21742. loader.setAttribute('src', link);
  21743. loader.setAttribute('_src', link);
  21744. loader.setAttribute('title', data.title || '');
  21745. loader.setAttribute('alt', data.original || '');
  21746. loader.removeAttribute('id');
  21747. domUtils.removeClasses(loader, 'loadingclass');
  21748. }
  21749. };
  21750. } else {
  21751. loadingHtml = '<p>' +
  21752. '<img class="loadingclass" id="' + loadingId + '" src="' +
  21753. me.options.themePath + me.options.theme +
  21754. '/images/spacer.gif" title="' + (me.getLang('autoupload.loading') || '') + '" >' +
  21755. '</p>';
  21756. successHandler = function(data) {
  21757. var link = urlPrefix + data.url,
  21758. loader = me.document.getElementById(loadingId);
  21759. var rng = me.selection.getRange(),
  21760. bk = rng.createBookmark();
  21761. rng.selectNode(loader).select();
  21762. me.execCommand('insertfile', {'url': link});
  21763. rng.moveToBookmark(bk).select();
  21764. };
  21765. }
  21766. /* 插入loading的占位符 */
  21767. me.execCommand('inserthtml', loadingHtml);
  21768. /* 判断后端配置是否没有加载成功 */
  21769. if (!me.getOpt(filetype + 'ActionName')) {
  21770. errorHandler(me.getLang('autoupload.errorLoadConfig'));
  21771. return;
  21772. }
  21773. /* 判断文件大小是否超出限制 */
  21774. if(file.size > maxSize) {
  21775. errorHandler(me.getLang('autoupload.exceedSizeError'));
  21776. return;
  21777. }
  21778. /* 判断文件格式是否超出允许 */
  21779. var fileext = file.name ? file.name.substr(file.name.lastIndexOf('.')):'';
  21780. if ((fileext && filetype != 'image') || (allowFiles && (allowFiles.join('') + '.').indexOf(fileext.toLowerCase() + '.') == -1)) {
  21781. errorHandler(me.getLang('autoupload.exceedTypeError'));
  21782. return;
  21783. }
  21784. /* 创建Ajax并提交 */
  21785. var xhr = new XMLHttpRequest(),
  21786. fd = new FormData(),
  21787. params = utils.serializeParam(me.queryCommandValue('serverparam')) || '',
  21788. url = utils.formatUrl(actionUrl + (actionUrl.indexOf('?') == -1 ? '?':'&') + params);
  21789. fd.append(fieldName, file, file.name || ('blob.' + file.type.substr('image/'.length)));
  21790. fd.append('type', 'ajax');
  21791. xhr.open("post", url, true);
  21792. xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
  21793. xhr.addEventListener('load', function (e) {
  21794. try{
  21795. var json = (new Function("return " + utils.trim(e.target.response)))();
  21796. if (json.state == 'SUCCESS' && json.url) {
  21797. successHandler(json);
  21798. } else {
  21799. errorHandler(json.state);
  21800. }
  21801. }catch(er){
  21802. errorHandler(me.getLang('autoupload.loadError'));
  21803. }
  21804. });
  21805. xhr.send(fd);
  21806. }
  21807. function getPasteImage(e){
  21808. return e.clipboardData && e.clipboardData.items && e.clipboardData.items.length == 1 && /^image\//.test(e.clipboardData.items[0].type) ? e.clipboardData.items:null;
  21809. }
  21810. function getDropImage(e){
  21811. return e.dataTransfer && e.dataTransfer.files ? e.dataTransfer.files:null;
  21812. }
  21813. return {
  21814. outputRule: function(root){
  21815. utils.each(root.getNodesByTagName('img'),function(n){
  21816. if (/\b(loaderrorclass)|(bloaderrorclass)\b/.test(n.getAttr('class'))) {
  21817. n.parentNode.removeChild(n);
  21818. }
  21819. });
  21820. utils.each(root.getNodesByTagName('p'),function(n){
  21821. if (/\bloadpara\b/.test(n.getAttr('class'))) {
  21822. n.parentNode.removeChild(n);
  21823. }
  21824. });
  21825. },
  21826. bindEvents:{
  21827. //插入粘贴板的图片,拖放插入图片
  21828. 'ready':function(e){
  21829. var me = this;
  21830. if(window.FormData && window.FileReader) {
  21831. domUtils.on(me.body, 'paste drop', function(e){
  21832. var hasImg = false,
  21833. items;
  21834. //获取粘贴板文件列表或者拖放文件列表
  21835. items = e.type == 'paste' ? getPasteImage(e):getDropImage(e);
  21836. if(items){
  21837. var len = items.length,
  21838. file;
  21839. while (len--){
  21840. file = items[len];
  21841. if(file.getAsFile) file = file.getAsFile();
  21842. if(file && file.size > 0) {
  21843. sendAndInsertFile(file, me);
  21844. hasImg = true;
  21845. }
  21846. }
  21847. hasImg && e.preventDefault();
  21848. }
  21849. });
  21850. //取消拖放图片时出现的文字光标位置提示
  21851. domUtils.on(me.body, 'dragover', function (e) {
  21852. if(e.dataTransfer.types[0] == 'Files') {
  21853. e.preventDefault();
  21854. }
  21855. });
  21856. //设置loading的样式
  21857. utils.cssRule('loading',
  21858. '.loadingclass{display:inline-block;cursor:default;background: url(\''
  21859. + this.options.themePath
  21860. + this.options.theme +'/images/loading.gif\') no-repeat center center transparent;border:1px solid #cccccc;margin-left:1px;height: 22px;width: 22px;}\n' +
  21861. '.loaderrorclass{display:inline-block;cursor:default;background: url(\''
  21862. + this.options.themePath
  21863. + this.options.theme +'/images/loaderror.png\') no-repeat center center transparent;border:1px solid #cccccc;margin-right:1px;height: 22px;width: 22px;' +
  21864. '}',
  21865. this.document);
  21866. }
  21867. }
  21868. }
  21869. }
  21870. });
  21871. // plugins/autosave.js
  21872. UE.plugin.register('autosave', function (){
  21873. var me = this,
  21874. //无限循环保护
  21875. lastSaveTime = new Date(),
  21876. //最小保存间隔时间
  21877. MIN_TIME = 20,
  21878. //auto save key
  21879. saveKey = null;
  21880. function save ( editor ) {
  21881. var saveData;
  21882. if ( new Date() - lastSaveTime < MIN_TIME ) {
  21883. return;
  21884. }
  21885. if ( !editor.hasContents() ) {
  21886. //这里不能调用命令来删除, 会造成事件死循环
  21887. saveKey && me.removePreferences( saveKey );
  21888. return;
  21889. }
  21890. lastSaveTime = new Date();
  21891. editor._saveFlag = null;
  21892. saveData = me.body.innerHTML;
  21893. if ( editor.fireEvent( "beforeautosave", {
  21894. content: saveData
  21895. } ) === false ) {
  21896. return;
  21897. }
  21898. me.setPreferences( saveKey, saveData );
  21899. editor.fireEvent( "afterautosave", {
  21900. content: saveData
  21901. } );
  21902. }
  21903. return {
  21904. defaultOptions: {
  21905. //默认间隔时间
  21906. saveInterval: 500
  21907. },
  21908. bindEvents:{
  21909. 'ready':function(){
  21910. var _suffix = "-drafts-data",
  21911. key = null;
  21912. if ( me.key ) {
  21913. key = me.key + _suffix;
  21914. } else {
  21915. key = ( me.container.parentNode.id || 'ue-common' ) + _suffix;
  21916. }
  21917. //页面地址+编辑器ID 保持唯一
  21918. saveKey = ( location.protocol + location.host + location.pathname ).replace( /[.:\/]/g, '_' ) + key;
  21919. },
  21920. 'contentchange': function () {
  21921. if ( !saveKey ) {
  21922. return;
  21923. }
  21924. if ( me._saveFlag ) {
  21925. window.clearTimeout( me._saveFlag );
  21926. }
  21927. if ( me.options.saveInterval > 0 ) {
  21928. me._saveFlag = window.setTimeout( function () {
  21929. save( me );
  21930. }, me.options.saveInterval );
  21931. } else {
  21932. save(me);
  21933. }
  21934. }
  21935. },
  21936. commands:{
  21937. 'clearlocaldata':{
  21938. execCommand:function (cmd, name) {
  21939. if ( saveKey && me.getPreferences( saveKey ) ) {
  21940. me.removePreferences( saveKey )
  21941. }
  21942. },
  21943. notNeedUndo: true,
  21944. ignoreContentChange:true
  21945. },
  21946. 'getlocaldata':{
  21947. execCommand:function (cmd, name) {
  21948. return saveKey ? me.getPreferences( saveKey ) || '' : '';
  21949. },
  21950. notNeedUndo: true,
  21951. ignoreContentChange:true
  21952. },
  21953. 'drafts':{
  21954. execCommand:function (cmd, name) {
  21955. if ( saveKey ) {
  21956. me.body.innerHTML = me.getPreferences( saveKey ) || '<p>'+domUtils.fillHtml+'</p>';
  21957. me.focus(true);
  21958. }
  21959. },
  21960. queryCommandState: function () {
  21961. return saveKey ? ( me.getPreferences( saveKey ) === null ? -1 : 0 ) : -1;
  21962. },
  21963. notNeedUndo: true,
  21964. ignoreContentChange:true
  21965. }
  21966. }
  21967. }
  21968. });
  21969. // plugins/charts.js
  21970. UE.plugin.register('charts', function (){
  21971. var me = this;
  21972. return {
  21973. bindEvents: {
  21974. 'chartserror': function () {
  21975. }
  21976. },
  21977. commands:{
  21978. 'charts': {
  21979. execCommand: function ( cmd, data ) {
  21980. var tableNode = domUtils.findParentByTagName(this.selection.getRange().startContainer, 'table', true),
  21981. flagText = [],
  21982. config = {};
  21983. if ( !tableNode ) {
  21984. return false;
  21985. }
  21986. if ( !validData( tableNode ) ) {
  21987. me.fireEvent( "chartserror" );
  21988. return false;
  21989. }
  21990. config.title = data.title || '';
  21991. config.subTitle = data.subTitle || '';
  21992. config.xTitle = data.xTitle || '';
  21993. config.yTitle = data.yTitle || '';
  21994. config.suffix = data.suffix || '';
  21995. config.tip = data.tip || '';
  21996. //数据对齐方式
  21997. config.dataFormat = data.tableDataFormat || '';
  21998. //图表类型
  21999. config.chartType = data.chartType || 0;
  22000. for ( var key in config ) {
  22001. if ( !config.hasOwnProperty( key ) ) {
  22002. continue;
  22003. }
  22004. flagText.push( key+":"+config[ key ] );
  22005. }
  22006. tableNode.setAttribute( "data-chart", flagText.join( ";" ) );
  22007. domUtils.addClass( tableNode, "edui-charts-table" );
  22008. },
  22009. queryCommandState: function ( cmd, name ) {
  22010. var tableNode = domUtils.findParentByTagName(this.selection.getRange().startContainer, 'table', true);
  22011. return tableNode && validData( tableNode ) ? 0 : -1;
  22012. }
  22013. }
  22014. },
  22015. inputRule:function(root){
  22016. utils.each(root.getNodesByTagName('table'),function( tableNode ){
  22017. if ( tableNode.getAttr("data-chart") !== undefined ) {
  22018. tableNode.setAttr("style");
  22019. }
  22020. })
  22021. },
  22022. outputRule:function(root){
  22023. utils.each(root.getNodesByTagName('table'),function( tableNode ){
  22024. if ( tableNode.getAttr("data-chart") !== undefined ) {
  22025. tableNode.setAttr("style", "display: none;");
  22026. }
  22027. })
  22028. }
  22029. }
  22030. function validData ( table ) {
  22031. var firstRows = null,
  22032. cellCount = 0;
  22033. //行数不够
  22034. if ( table.rows.length < 2 ) {
  22035. return false;
  22036. }
  22037. //列数不够
  22038. if ( table.rows[0].cells.length < 2 ) {
  22039. return false;
  22040. }
  22041. //第一行所有cell必须是th
  22042. firstRows = table.rows[ 0 ].cells;
  22043. cellCount = firstRows.length;
  22044. for ( var i = 0, cell; cell = firstRows[ i ]; i++ ) {
  22045. if ( cell.tagName.toLowerCase() !== 'th' ) {
  22046. return false;
  22047. }
  22048. }
  22049. for ( var i = 1, row; row = table.rows[ i ]; i++ ) {
  22050. //每行单元格数不匹配, 返回false
  22051. if ( row.cells.length != cellCount ) {
  22052. return false;
  22053. }
  22054. //第一列不是th也返回false
  22055. if ( row.cells[0].tagName.toLowerCase() !== 'th' ) {
  22056. return false;
  22057. }
  22058. for ( var j = 1, cell; cell = row.cells[ j ]; j++ ) {
  22059. var value = utils.trim( ( cell.innerText || cell.textContent || '' ) );
  22060. value = value.replace( new RegExp( UE.dom.domUtils.fillChar, 'g' ), '' ).replace( /^\s+|\s+$/g, '' );
  22061. //必须是数字
  22062. if ( !/^\d*\.?\d+$/.test( value ) ) {
  22063. return false;
  22064. }
  22065. }
  22066. }
  22067. return true;
  22068. }
  22069. });
  22070. // plugins/section.js
  22071. /**
  22072. * 目录大纲支持插件
  22073. * @file
  22074. * @since 1.3.0
  22075. */
  22076. UE.plugin.register('section', function (){
  22077. /* 目录节点对象 */
  22078. function Section(option){
  22079. this.tag = '';
  22080. this.level = -1,
  22081. this.dom = null;
  22082. this.nextSection = null;
  22083. this.previousSection = null;
  22084. this.parentSection = null;
  22085. this.startAddress = [];
  22086. this.endAddress = [];
  22087. this.children = [];
  22088. }
  22089. function getSection(option) {
  22090. var section = new Section();
  22091. return utils.extend(section, option);
  22092. }
  22093. function getNodeFromAddress(startAddress, root) {
  22094. var current = root;
  22095. for(var i = 0;i < startAddress.length; i++) {
  22096. if(!current.childNodes) return null;
  22097. current = current.childNodes[startAddress[i]];
  22098. }
  22099. return current;
  22100. }
  22101. var me = this;
  22102. return {
  22103. bindMultiEvents:{
  22104. type: 'aftersetcontent afterscencerestore',
  22105. handler: function(){
  22106. me.fireEvent('updateSections');
  22107. }
  22108. },
  22109. bindEvents:{
  22110. /* 初始化、拖拽、粘贴、执行setcontent之后 */
  22111. 'ready': function (){
  22112. me.fireEvent('updateSections');
  22113. domUtils.on(me.body, 'drop paste', function(){
  22114. me.fireEvent('updateSections');
  22115. });
  22116. },
  22117. /* 执行paragraph命令之后 */
  22118. 'afterexeccommand': function (type, cmd) {
  22119. if(cmd == 'paragraph') {
  22120. me.fireEvent('updateSections');
  22121. }
  22122. },
  22123. /* 部分键盘操作,触发updateSections事件 */
  22124. 'keyup': function (type, e) {
  22125. var me = this,
  22126. range = me.selection.getRange();
  22127. if(range.collapsed != true) {
  22128. me.fireEvent('updateSections');
  22129. } else {
  22130. var keyCode = e.keyCode || e.which;
  22131. if(keyCode == 13 || keyCode == 8 || keyCode == 46) {
  22132. me.fireEvent('updateSections');
  22133. }
  22134. }
  22135. }
  22136. },
  22137. commands:{
  22138. 'getsections': {
  22139. execCommand: function (cmd, levels) {
  22140. var levelFn = levels || ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
  22141. for (var i = 0; i < levelFn.length; i++) {
  22142. if (typeof levelFn[i] == 'string') {
  22143. levelFn[i] = function(fn){
  22144. return function(node){
  22145. return node.tagName == fn.toUpperCase()
  22146. };
  22147. }(levelFn[i]);
  22148. } else if (typeof levelFn[i] != 'function') {
  22149. levelFn[i] = function (node) {
  22150. return null;
  22151. }
  22152. }
  22153. }
  22154. function getSectionLevel(node) {
  22155. for (var i = 0; i < levelFn.length; i++) {
  22156. if (levelFn[i](node)) return i;
  22157. }
  22158. return -1;
  22159. }
  22160. var me = this,
  22161. Directory = getSection({'level':-1, 'title':'root'}),
  22162. previous = Directory;
  22163. function traversal(node, Directory) {
  22164. var level,
  22165. tmpSection = null,
  22166. parent,
  22167. child,
  22168. children = node.childNodes;
  22169. for (var i = 0, len = children.length; i < len; i++) {
  22170. child = children[i];
  22171. level = getSectionLevel(child);
  22172. if (level >= 0) {
  22173. var address = me.selection.getRange().selectNode(child).createAddress(true).startAddress,
  22174. current = getSection({
  22175. 'tag': child.tagName,
  22176. 'title': child.innerText || child.textContent || '',
  22177. 'level': level,
  22178. 'dom': child,
  22179. 'startAddress': utils.clone(address, []),
  22180. 'endAddress': utils.clone(address, []),
  22181. 'children': []
  22182. });
  22183. previous.nextSection = current;
  22184. current.previousSection = previous;
  22185. parent = previous;
  22186. while(level <= parent.level){
  22187. parent = parent.parentSection;
  22188. }
  22189. current.parentSection = parent;
  22190. parent.children.push(current);
  22191. tmpSection = previous = current;
  22192. } else {
  22193. child.nodeType === 1 && traversal(child, Directory);
  22194. tmpSection && tmpSection.endAddress[tmpSection.endAddress.length - 1] ++;
  22195. }
  22196. }
  22197. }
  22198. traversal(me.body, Directory);
  22199. return Directory;
  22200. },
  22201. notNeedUndo: true
  22202. },
  22203. 'movesection': {
  22204. execCommand: function (cmd, sourceSection, targetSection, isAfter) {
  22205. var me = this,
  22206. targetAddress,
  22207. target;
  22208. if(!sourceSection || !targetSection || targetSection.level == -1) return;
  22209. targetAddress = isAfter ? targetSection.endAddress:targetSection.startAddress;
  22210. target = getNodeFromAddress(targetAddress, me.body);
  22211. /* 判断目标地址是否被源章节包含 */
  22212. if(!targetAddress || !target || isContainsAddress(sourceSection.startAddress, sourceSection.endAddress, targetAddress)) return;
  22213. var startNode = getNodeFromAddress(sourceSection.startAddress, me.body),
  22214. endNode = getNodeFromAddress(sourceSection.endAddress, me.body),
  22215. current,
  22216. nextNode;
  22217. if(isAfter) {
  22218. current = endNode;
  22219. while ( current && !(domUtils.getPosition( startNode, current ) & domUtils.POSITION_FOLLOWING) ) {
  22220. nextNode = current.previousSibling;
  22221. domUtils.insertAfter(target, current);
  22222. if(current == startNode) break;
  22223. current = nextNode;
  22224. }
  22225. } else {
  22226. current = startNode;
  22227. while ( current && !(domUtils.getPosition( current, endNode ) & domUtils.POSITION_FOLLOWING) ) {
  22228. nextNode = current.nextSibling;
  22229. target.parentNode.insertBefore(current, target);
  22230. if(current == endNode) break;
  22231. current = nextNode;
  22232. }
  22233. }
  22234. me.fireEvent('updateSections');
  22235. /* 获取地址的包含关系 */
  22236. function isContainsAddress(startAddress, endAddress, addressTarget){
  22237. var isAfterStartAddress = false,
  22238. isBeforeEndAddress = false;
  22239. for(var i = 0; i< startAddress.length; i++){
  22240. if(i >= addressTarget.length) break;
  22241. if(addressTarget[i] > startAddress[i]) {
  22242. isAfterStartAddress = true;
  22243. break;
  22244. } else if(addressTarget[i] < startAddress[i]) {
  22245. break;
  22246. }
  22247. }
  22248. for(var i = 0; i< endAddress.length; i++){
  22249. if(i >= addressTarget.length) break;
  22250. if(addressTarget[i] < startAddress[i]) {
  22251. isBeforeEndAddress = true;
  22252. break;
  22253. } else if(addressTarget[i] > startAddress[i]) {
  22254. break;
  22255. }
  22256. }
  22257. return isAfterStartAddress && isBeforeEndAddress;
  22258. }
  22259. }
  22260. },
  22261. 'deletesection': {
  22262. execCommand: function (cmd, section, keepChildren) {
  22263. var me = this;
  22264. if(!section) return;
  22265. function getNodeFromAddress(startAddress) {
  22266. var current = me.body;
  22267. for(var i = 0;i < startAddress.length; i++) {
  22268. if(!current.childNodes) return null;
  22269. current = current.childNodes[startAddress[i]];
  22270. }
  22271. return current;
  22272. }
  22273. var startNode = getNodeFromAddress(section.startAddress),
  22274. endNode = getNodeFromAddress(section.endAddress),
  22275. current = startNode,
  22276. nextNode;
  22277. if(!keepChildren) {
  22278. while ( current && domUtils.inDoc(endNode, me.document) && !(domUtils.getPosition( current, endNode ) & domUtils.POSITION_FOLLOWING) ) {
  22279. nextNode = current.nextSibling;
  22280. domUtils.remove(current);
  22281. current = nextNode;
  22282. }
  22283. } else {
  22284. domUtils.remove(current);
  22285. }
  22286. me.fireEvent('updateSections');
  22287. }
  22288. },
  22289. 'selectsection': {
  22290. execCommand: function (cmd, section) {
  22291. if(!section && !section.dom) return false;
  22292. var me = this,
  22293. range = me.selection.getRange(),
  22294. address = {
  22295. 'startAddress':utils.clone(section.startAddress, []),
  22296. 'endAddress':utils.clone(section.endAddress, [])
  22297. };
  22298. address.endAddress[address.endAddress.length - 1]++;
  22299. range.moveToAddress(address).select().scrollToView();
  22300. return true;
  22301. },
  22302. notNeedUndo: true
  22303. },
  22304. 'scrolltosection': {
  22305. execCommand: function (cmd, section) {
  22306. if(!section && !section.dom) return false;
  22307. var me = this,
  22308. range = me.selection.getRange(),
  22309. address = {
  22310. 'startAddress':section.startAddress,
  22311. 'endAddress':section.endAddress
  22312. };
  22313. address.endAddress[address.endAddress.length - 1]++;
  22314. range.moveToAddress(address).scrollToView();
  22315. return true;
  22316. },
  22317. notNeedUndo: true
  22318. }
  22319. }
  22320. }
  22321. });
  22322. // plugins/simpleupload.js
  22323. /**
  22324. * @description
  22325. * 简单上传:点击按钮,直接选择文件上传
  22326. * @author Jinqn
  22327. * @date 2014-03-31
  22328. */
  22329. UE.plugin.register('simpleupload', function (){
  22330. var me = this,
  22331. isLoaded = false,
  22332. containerBtn;
  22333. function initUploadBtn(){
  22334. var w = containerBtn.offsetWidth || 20,
  22335. h = containerBtn.offsetHeight || 20,
  22336. btnIframe = document.createElement('iframe'),
  22337. 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;';
  22338. domUtils.on(btnIframe, 'load', function(){
  22339. var timestrap = (+new Date()).toString(36),
  22340. wrapper,
  22341. btnIframeDoc,
  22342. btnIframeBody;
  22343. btnIframeDoc = (btnIframe.contentDocument || btnIframe.contentWindow.document);
  22344. btnIframeBody = btnIframeDoc.body;
  22345. wrapper = btnIframeDoc.createElement('div');
  22346. wrapper.innerHTML = '<form id="edui_form_' + timestrap + '" target="edui_iframe_' + timestrap + '" method="POST" enctype="multipart/form-data" action="' + me.getOpt('serverUrl') + '" ' +
  22347. 'style="' + btnStyle + '">' +
  22348. '<input id="edui_input_' + timestrap + '" type="file" accept="image/*" name="' + me.options.imageFieldName + '" ' +
  22349. 'style="' + btnStyle + '">' +
  22350. '</form>' +
  22351. '<iframe id="edui_iframe_' + timestrap + '" name="edui_iframe_' + timestrap + '" style="display:none;width:0;height:0;border:0;margin:0;padding:0;position:absolute;"></iframe>';
  22352. wrapper.className = 'edui-' + me.options.theme;
  22353. wrapper.id = me.ui.id + '_iframeupload';
  22354. btnIframeBody.style.cssText = btnStyle;
  22355. btnIframeBody.style.width = w + 'px';
  22356. btnIframeBody.style.height = h + 'px';
  22357. btnIframeBody.appendChild(wrapper);
  22358. if (btnIframeBody.parentNode) {
  22359. btnIframeBody.parentNode.style.width = w + 'px';
  22360. btnIframeBody.parentNode.style.height = w + 'px';
  22361. }
  22362. var form = btnIframeDoc.getElementById('edui_form_' + timestrap);
  22363. var input = btnIframeDoc.getElementById('edui_input_' + timestrap);
  22364. var iframe = btnIframeDoc.getElementById('edui_iframe_' + timestrap);
  22365. domUtils.on(input, 'change', function(){
  22366. if(!input.value) return;
  22367. var loadingId = 'loading_' + (+new Date()).toString(36);
  22368. var params = utils.serializeParam(me.queryCommandValue('serverparam')) || '';
  22369. var imageActionUrl = me.getActionUrl(me.getOpt('imageActionName'));
  22370. var allowFiles = me.getOpt('imageAllowFiles');
  22371. me.focus();
  22372. me.execCommand('inserthtml', '<img class="loadingclass" id="' + loadingId + '" src="' + me.options.themePath + me.options.theme +'/images/spacer.gif" title="' + (me.getLang('simpleupload.loading') || '') + '" >');
  22373. function callback(){
  22374. try{
  22375. var link, json, loader,
  22376. body = (iframe.contentDocument || iframe.contentWindow.document).body,
  22377. result = body.innerText || body.textContent || '';
  22378. json = (new Function("return " + result))();
  22379. link = me.options.imageUrlPrefix + json.url;
  22380. if(json.state == 'SUCCESS' && json.url) {
  22381. loader = me.document.getElementById(loadingId);
  22382. loader.setAttribute('src', link);
  22383. loader.setAttribute('_src', link);
  22384. loader.setAttribute('title', json.title || '');
  22385. loader.setAttribute('alt', json.original || '');
  22386. loader.removeAttribute('id');
  22387. domUtils.removeClasses(loader, 'loadingclass');
  22388. } else {
  22389. showErrorLoader && showErrorLoader(json.state);
  22390. }
  22391. }catch(er){
  22392. showErrorLoader && showErrorLoader(me.getLang('simpleupload.loadError'));
  22393. }
  22394. form.reset();
  22395. domUtils.un(iframe, 'load', callback);
  22396. }
  22397. function showErrorLoader(title){
  22398. if(loadingId) {
  22399. var loader = me.document.getElementById(loadingId);
  22400. loader && domUtils.remove(loader);
  22401. me.fireEvent('showmessage', {
  22402. 'id': loadingId,
  22403. 'content': title,
  22404. 'type': 'error',
  22405. 'timeout': 4000
  22406. });
  22407. }
  22408. }
  22409. /* 判断后端配置是否没有加载成功 */
  22410. if (!me.getOpt('imageActionName')) {
  22411. errorHandler(me.getLang('autoupload.errorLoadConfig'));
  22412. return;
  22413. }
  22414. // 判断文件格式是否错误
  22415. var filename = input.value,
  22416. fileext = filename ? filename.substr(filename.lastIndexOf('.')):'';
  22417. if (!fileext || (allowFiles && (allowFiles.join('') + '.').indexOf(fileext.toLowerCase() + '.') == -1)) {
  22418. showErrorLoader(me.getLang('simpleupload.exceedTypeError'));
  22419. return;
  22420. }
  22421. domUtils.on(iframe, 'load', callback);
  22422. form.action = utils.formatUrl(imageActionUrl + (imageActionUrl.indexOf('?') == -1 ? '?':'&') + params);
  22423. form.submit();
  22424. });
  22425. var stateTimer;
  22426. me.addListener('selectionchange', function () {
  22427. clearTimeout(stateTimer);
  22428. stateTimer = setTimeout(function() {
  22429. var state = me.queryCommandState('simpleupload');
  22430. if (state == -1) {
  22431. input.disabled = 'disabled';
  22432. } else {
  22433. input.disabled = false;
  22434. }
  22435. }, 400);
  22436. });
  22437. isLoaded = true;
  22438. });
  22439. btnIframe.style.cssText = btnStyle;
  22440. containerBtn.appendChild(btnIframe);
  22441. }
  22442. return {
  22443. bindEvents:{
  22444. 'ready': function() {
  22445. //设置loading的样式
  22446. utils.cssRule('loading',
  22447. '.loadingclass{display:inline-block;cursor:default;background: url(\''
  22448. + this.options.themePath
  22449. + this.options.theme +'/images/loading.gif\') no-repeat center center transparent;border:1px solid #cccccc;margin-right:1px;height: 22px;width: 22px;}\n' +
  22450. '.loaderrorclass{display:inline-block;cursor:default;background: url(\''
  22451. + this.options.themePath
  22452. + this.options.theme +'/images/loaderror.png\') no-repeat center center transparent;border:1px solid #cccccc;margin-right:1px;height: 22px;width: 22px;' +
  22453. '}',
  22454. this.document);
  22455. },
  22456. /* 初始化简单上传按钮 */
  22457. 'simpleuploadbtnready': function(type, container) {
  22458. containerBtn = container;
  22459. me.afterConfigReady(initUploadBtn);
  22460. }
  22461. },
  22462. outputRule: function(root){
  22463. utils.each(root.getNodesByTagName('img'),function(n){
  22464. if (/\b(loaderrorclass)|(bloaderrorclass)\b/.test(n.getAttr('class'))) {
  22465. n.parentNode.removeChild(n);
  22466. }
  22467. });
  22468. },
  22469. commands: {
  22470. 'simpleupload': {
  22471. queryCommandState: function () {
  22472. return isLoaded ? 0:-1;
  22473. }
  22474. }
  22475. }
  22476. }
  22477. });
  22478. // plugins/serverparam.js
  22479. /**
  22480. * 服务器提交的额外参数列表设置插件
  22481. * @file
  22482. * @since 1.2.6.1
  22483. */
  22484. UE.plugin.register('serverparam', function (){
  22485. var me = this,
  22486. serverParam = {};
  22487. return {
  22488. commands:{
  22489. /**
  22490. * 修改服务器提交的额外参数列表,清除所有项
  22491. * @command serverparam
  22492. * @method execCommand
  22493. * @param { String } cmd 命令字符串
  22494. * @example
  22495. * ```javascript
  22496. * editor.execCommand('serverparam');
  22497. * editor.queryCommandValue('serverparam'); //返回空
  22498. * ```
  22499. */
  22500. /**
  22501. * 修改服务器提交的额外参数列表,删除指定项
  22502. * @command serverparam
  22503. * @method execCommand
  22504. * @param { String } cmd 命令字符串
  22505. * @param { String } key 要清除的属性
  22506. * @example
  22507. * ```javascript
  22508. * editor.execCommand('serverparam', 'name'); //删除属性name
  22509. * ```
  22510. */
  22511. /**
  22512. * 修改服务器提交的额外参数列表,使用键值添加项
  22513. * @command serverparam
  22514. * @method execCommand
  22515. * @param { String } cmd 命令字符串
  22516. * @param { String } key 要添加的属性
  22517. * @param { String } value 要添加属性的值
  22518. * @example
  22519. * ```javascript
  22520. * editor.execCommand('serverparam', 'name', 'hello');
  22521. * editor.queryCommandValue('serverparam'); //返回对象 {'name': 'hello'}
  22522. * ```
  22523. */
  22524. /**
  22525. * 修改服务器提交的额外参数列表,传入键值对对象添加多项
  22526. * @command serverparam
  22527. * @method execCommand
  22528. * @param { String } cmd 命令字符串
  22529. * @param { Object } key 传入的键值对对象
  22530. * @example
  22531. * ```javascript
  22532. * editor.execCommand('serverparam', {'name': 'hello'});
  22533. * editor.queryCommandValue('serverparam'); //返回对象 {'name': 'hello'}
  22534. * ```
  22535. */
  22536. /**
  22537. * 修改服务器提交的额外参数列表,使用自定义函数添加多项
  22538. * @command serverparam
  22539. * @method execCommand
  22540. * @param { String } cmd 命令字符串
  22541. * @param { Function } key 自定义获取参数的函数
  22542. * @example
  22543. * ```javascript
  22544. * editor.execCommand('serverparam', function(editor){
  22545. * return {'key': 'value'};
  22546. * });
  22547. * editor.queryCommandValue('serverparam'); //返回对象 {'key': 'value'}
  22548. * ```
  22549. */
  22550. /**
  22551. * 获取服务器提交的额外参数列表
  22552. * @command serverparam
  22553. * @method queryCommandValue
  22554. * @param { String } cmd 命令字符串
  22555. * @example
  22556. * ```javascript
  22557. * editor.queryCommandValue( 'serverparam' ); //返回对象 {'key': 'value'}
  22558. * ```
  22559. */
  22560. 'serverparam':{
  22561. execCommand:function (cmd, key, value) {
  22562. if (key === undefined || key === null) { //不传参数,清空列表
  22563. serverParam = {};
  22564. } else if (utils.isString(key)) { //传入键值
  22565. if(value === undefined || value === null) {
  22566. delete serverParam[key];
  22567. } else {
  22568. serverParam[key] = value;
  22569. }
  22570. } else if (utils.isObject(key)) { //传入对象,覆盖列表项
  22571. utils.extend(serverParam, key, true);
  22572. } else if (utils.isFunction(key)){ //传入函数,添加列表项
  22573. utils.extend(serverParam, key(), true);
  22574. }
  22575. },
  22576. queryCommandValue: function(){
  22577. return serverParam || {};
  22578. }
  22579. }
  22580. }
  22581. }
  22582. });
  22583. // plugins/insertfile.js
  22584. /**
  22585. * 插入附件
  22586. */
  22587. UE.plugin.register('insertfile', function (){
  22588. var me = this;
  22589. function getFileIcon(url){
  22590. var ext = url.substr(url.lastIndexOf('.') + 1).toLowerCase(),
  22591. maps = {
  22592. "rar":"icon_rar.gif",
  22593. "zip":"icon_rar.gif",
  22594. "tar":"icon_rar.gif",
  22595. "gz":"icon_rar.gif",
  22596. "bz2":"icon_rar.gif",
  22597. "doc":"icon_doc.gif",
  22598. "docx":"icon_doc.gif",
  22599. "pdf":"icon_pdf.gif",
  22600. "mp3":"icon_mp3.gif",
  22601. "xls":"icon_xls.gif",
  22602. "chm":"icon_chm.gif",
  22603. "ppt":"icon_ppt.gif",
  22604. "pptx":"icon_ppt.gif",
  22605. "avi":"icon_mv.gif",
  22606. "rmvb":"icon_mv.gif",
  22607. "wmv":"icon_mv.gif",
  22608. "flv":"icon_mv.gif",
  22609. "swf":"icon_mv.gif",
  22610. "rm":"icon_mv.gif",
  22611. "exe":"icon_exe.gif",
  22612. "psd":"icon_psd.gif",
  22613. "txt":"icon_txt.gif",
  22614. "jpg":"icon_jpg.gif",
  22615. "png":"icon_jpg.gif",
  22616. "jpeg":"icon_jpg.gif",
  22617. "gif":"icon_jpg.gif",
  22618. "ico":"icon_jpg.gif",
  22619. "bmp":"icon_jpg.gif"
  22620. };
  22621. return maps[ext] ? maps[ext]:maps['txt'];
  22622. }
  22623. return {
  22624. commands:{
  22625. 'insertfile': {
  22626. execCommand: function (command, filelist){
  22627. filelist = utils.isArray(filelist) ? filelist : [filelist];
  22628. var i, item, icon, title,
  22629. html = '',
  22630. URL = me.getOpt('UEDITOR_HOME_URL'),
  22631. iconDir = URL + (URL.substr(URL.length - 1) == '/' ? '':'/') + 'dialogs/attachment/fileTypeImages/';
  22632. for (i = 0; i < filelist.length; i++) {
  22633. item = filelist[i];
  22634. icon = iconDir + getFileIcon(item.url);
  22635. title = item.title || item.url.substr(item.url.lastIndexOf('/') + 1);
  22636. html += '<p style="line-height: 16px;">' +
  22637. '<img style="vertical-align: middle; margin-right: 2px;" src="'+ icon + '" _src="' + icon + '" />' +
  22638. '<a style="font-size:12px; color:#0066cc;" href="' + item.url +'" title="' + title + '">' + title + '</a>' +
  22639. '</p>';
  22640. }
  22641. me.execCommand('insertHtml', html);
  22642. }
  22643. }
  22644. }
  22645. }
  22646. });
  22647. // ui/ui.js
  22648. var baidu = baidu || {};
  22649. baidu.editor = baidu.editor || {};
  22650. UE.ui = baidu.editor.ui = {};
  22651. // ui/uiutils.js
  22652. (function (){
  22653. var browser = baidu.editor.browser,
  22654. domUtils = baidu.editor.dom.domUtils;
  22655. var magic = '$EDITORUI';
  22656. var root = window[magic] = {};
  22657. var uidMagic = 'ID' + magic;
  22658. var uidCount = 0;
  22659. var uiUtils = baidu.editor.ui.uiUtils = {
  22660. uid: function (obj){
  22661. return (obj ? obj[uidMagic] || (obj[uidMagic] = ++ uidCount) : ++ uidCount);
  22662. },
  22663. hook: function ( fn, callback ) {
  22664. var dg;
  22665. if (fn && fn._callbacks) {
  22666. dg = fn;
  22667. } else {
  22668. dg = function (){
  22669. var q;
  22670. if (fn) {
  22671. q = fn.apply(this, arguments);
  22672. }
  22673. var callbacks = dg._callbacks;
  22674. var k = callbacks.length;
  22675. while (k --) {
  22676. var r = callbacks[k].apply(this, arguments);
  22677. if (q === undefined) {
  22678. q = r;
  22679. }
  22680. }
  22681. return q;
  22682. };
  22683. dg._callbacks = [];
  22684. }
  22685. dg._callbacks.push(callback);
  22686. return dg;
  22687. },
  22688. createElementByHtml: function (html){
  22689. var el = document.createElement('div');
  22690. el.innerHTML = html;
  22691. el = el.firstChild;
  22692. el.parentNode.removeChild(el);
  22693. return el;
  22694. },
  22695. getViewportElement: function (){
  22696. return (browser.ie && browser.quirks) ?
  22697. document.body : document.documentElement;
  22698. },
  22699. getClientRect: function (element){
  22700. var bcr;
  22701. //trace IE6下在控制编辑器显隐时可能会报错,catch一下
  22702. try{
  22703. bcr = element.getBoundingClientRect();
  22704. }catch(e){
  22705. bcr={left:0,top:0,height:0,width:0}
  22706. }
  22707. var rect = {
  22708. left: Math.round(bcr.left),
  22709. top: Math.round(bcr.top),
  22710. height: Math.round(bcr.bottom - bcr.top),
  22711. width: Math.round(bcr.right - bcr.left)
  22712. };
  22713. var doc;
  22714. while ((doc = element.ownerDocument) !== document &&
  22715. (element = domUtils.getWindow(doc).frameElement)) {
  22716. bcr = element.getBoundingClientRect();
  22717. rect.left += bcr.left;
  22718. rect.top += bcr.top;
  22719. }
  22720. rect.bottom = rect.top + rect.height;
  22721. rect.right = rect.left + rect.width;
  22722. return rect;
  22723. },
  22724. getViewportRect: function (){
  22725. var viewportEl = uiUtils.getViewportElement();
  22726. var width = (window.innerWidth || viewportEl.clientWidth) | 0;
  22727. var height = (window.innerHeight ||viewportEl.clientHeight) | 0;
  22728. return {
  22729. left: 0,
  22730. top: 0,
  22731. height: height,
  22732. width: width,
  22733. bottom: height,
  22734. right: width
  22735. };
  22736. },
  22737. setViewportOffset: function (element, offset){
  22738. var rect;
  22739. var fixedLayer = uiUtils.getFixedLayer();
  22740. if (element.parentNode === fixedLayer) {
  22741. element.style.left = offset.left + 'px';
  22742. element.style.top = offset.top + 'px';
  22743. } else {
  22744. domUtils.setViewportOffset(element, offset);
  22745. }
  22746. },
  22747. getEventOffset: function (evt){
  22748. var el = evt.target || evt.srcElement;
  22749. var rect = uiUtils.getClientRect(el);
  22750. var offset = uiUtils.getViewportOffsetByEvent(evt);
  22751. return {
  22752. left: offset.left - rect.left,
  22753. top: offset.top - rect.top
  22754. };
  22755. },
  22756. getViewportOffsetByEvent: function (evt){
  22757. var el = evt.target || evt.srcElement;
  22758. var frameEl = domUtils.getWindow(el).frameElement;
  22759. var offset = {
  22760. left: evt.clientX,
  22761. top: evt.clientY
  22762. };
  22763. if (frameEl && el.ownerDocument !== document) {
  22764. var rect = uiUtils.getClientRect(frameEl);
  22765. offset.left += rect.left;
  22766. offset.top += rect.top;
  22767. }
  22768. return offset;
  22769. },
  22770. setGlobal: function (id, obj){
  22771. root[id] = obj;
  22772. return magic + '["' + id + '"]';
  22773. },
  22774. unsetGlobal: function (id){
  22775. delete root[id];
  22776. },
  22777. copyAttributes: function (tgt, src){
  22778. var attributes = src.attributes;
  22779. var k = attributes.length;
  22780. while (k --) {
  22781. var attrNode = attributes[k];
  22782. if ( attrNode.nodeName != 'style' && attrNode.nodeName != 'class' && (!browser.ie || attrNode.specified) ) {
  22783. tgt.setAttribute(attrNode.nodeName, attrNode.nodeValue);
  22784. }
  22785. }
  22786. if (src.className) {
  22787. domUtils.addClass(tgt,src.className);
  22788. }
  22789. if (src.style.cssText) {
  22790. tgt.style.cssText += ';' + src.style.cssText;
  22791. }
  22792. },
  22793. removeStyle: function (el, styleName){
  22794. if (el.style.removeProperty) {
  22795. el.style.removeProperty(styleName);
  22796. } else if (el.style.removeAttribute) {
  22797. el.style.removeAttribute(styleName);
  22798. } else throw '';
  22799. },
  22800. contains: function (elA, elB){
  22801. return elA && elB && (elA === elB ? false : (
  22802. elA.contains ? elA.contains(elB) :
  22803. elA.compareDocumentPosition(elB) & 16
  22804. ));
  22805. },
  22806. startDrag: function (evt, callbacks,doc){
  22807. var doc = doc || document;
  22808. var startX = evt.clientX;
  22809. var startY = evt.clientY;
  22810. function handleMouseMove(evt){
  22811. var x = evt.clientX - startX;
  22812. var y = evt.clientY - startY;
  22813. callbacks.ondragmove(x, y,evt);
  22814. if (evt.stopPropagation) {
  22815. evt.stopPropagation();
  22816. } else {
  22817. evt.cancelBubble = true;
  22818. }
  22819. }
  22820. if (doc.addEventListener) {
  22821. function handleMouseUp(evt){
  22822. doc.removeEventListener('mousemove', handleMouseMove, true);
  22823. doc.removeEventListener('mouseup', handleMouseUp, true);
  22824. window.removeEventListener('mouseup', handleMouseUp, true);
  22825. callbacks.ondragstop();
  22826. }
  22827. doc.addEventListener('mousemove', handleMouseMove, true);
  22828. doc.addEventListener('mouseup', handleMouseUp, true);
  22829. window.addEventListener('mouseup', handleMouseUp, true);
  22830. evt.preventDefault();
  22831. } else {
  22832. var elm = evt.srcElement;
  22833. elm.setCapture();
  22834. function releaseCaptrue(){
  22835. elm.releaseCapture();
  22836. elm.detachEvent('onmousemove', handleMouseMove);
  22837. elm.detachEvent('onmouseup', releaseCaptrue);
  22838. elm.detachEvent('onlosecaptrue', releaseCaptrue);
  22839. callbacks.ondragstop();
  22840. }
  22841. elm.attachEvent('onmousemove', handleMouseMove);
  22842. elm.attachEvent('onmouseup', releaseCaptrue);
  22843. elm.attachEvent('onlosecaptrue', releaseCaptrue);
  22844. evt.returnValue = false;
  22845. }
  22846. callbacks.ondragstart();
  22847. },
  22848. getFixedLayer: function (){
  22849. var layer = document.getElementById('edui_fixedlayer');
  22850. if (layer == null) {
  22851. layer = document.createElement('div');
  22852. layer.id = 'edui_fixedlayer';
  22853. document.body.appendChild(layer);
  22854. if (browser.ie && browser.version <= 8) {
  22855. layer.style.position = 'absolute';
  22856. bindFixedLayer();
  22857. setTimeout(updateFixedOffset);
  22858. } else {
  22859. layer.style.position = 'fixed';
  22860. }
  22861. layer.style.left = '0';
  22862. layer.style.top = '0';
  22863. layer.style.width = '0';
  22864. layer.style.height = '0';
  22865. }
  22866. return layer;
  22867. },
  22868. makeUnselectable: function (element){
  22869. if (browser.opera || (browser.ie && browser.version < 9)) {
  22870. element.unselectable = 'on';
  22871. if (element.hasChildNodes()) {
  22872. for (var i=0; i<element.childNodes.length; i++) {
  22873. if (element.childNodes[i].nodeType == 1) {
  22874. uiUtils.makeUnselectable(element.childNodes[i]);
  22875. }
  22876. }
  22877. }
  22878. } else {
  22879. if (element.style.MozUserSelect !== undefined) {
  22880. element.style.MozUserSelect = 'none';
  22881. } else if (element.style.WebkitUserSelect !== undefined) {
  22882. element.style.WebkitUserSelect = 'none';
  22883. } else if (element.style.KhtmlUserSelect !== undefined) {
  22884. element.style.KhtmlUserSelect = 'none';
  22885. }
  22886. }
  22887. }
  22888. };
  22889. function updateFixedOffset(){
  22890. var layer = document.getElementById('edui_fixedlayer');
  22891. uiUtils.setViewportOffset(layer, {
  22892. left: 0,
  22893. top: 0
  22894. });
  22895. // layer.style.display = 'none';
  22896. // layer.style.display = 'block';
  22897. //#trace: 1354
  22898. // setTimeout(updateFixedOffset);
  22899. }
  22900. function bindFixedLayer(adjOffset){
  22901. domUtils.on(window, 'scroll', updateFixedOffset);
  22902. domUtils.on(window, 'resize', baidu.editor.utils.defer(updateFixedOffset, 0, true));
  22903. }
  22904. })();
  22905. // ui/uibase.js
  22906. (function () {
  22907. var utils = baidu.editor.utils,
  22908. uiUtils = baidu.editor.ui.uiUtils,
  22909. EventBase = baidu.editor.EventBase,
  22910. UIBase = baidu.editor.ui.UIBase = function () {
  22911. };
  22912. UIBase.prototype = {
  22913. className:'',
  22914. uiName:'',
  22915. initOptions:function (options) {
  22916. var me = this;
  22917. for (var k in options) {
  22918. me[k] = options[k];
  22919. }
  22920. this.id = this.id || 'edui' + uiUtils.uid();
  22921. },
  22922. initUIBase:function () {
  22923. this._globalKey = utils.unhtml(uiUtils.setGlobal(this.id, this));
  22924. },
  22925. render:function (holder) {
  22926. var html = this.renderHtml();
  22927. var el = uiUtils.createElementByHtml(html);
  22928. //by xuheng 给每个node添加class
  22929. var list = domUtils.getElementsByTagName(el, "*");
  22930. var theme = "edui-" + (this.theme || this.editor.options.theme);
  22931. var layer = document.getElementById('edui_fixedlayer');
  22932. for (var i = 0, node; node = list[i++];) {
  22933. domUtils.addClass(node, theme);
  22934. }
  22935. domUtils.addClass(el, theme);
  22936. if(layer){
  22937. layer.className="";
  22938. domUtils.addClass(layer,theme);
  22939. }
  22940. var seatEl = this.getDom();
  22941. if (seatEl != null) {
  22942. seatEl.parentNode.replaceChild(el, seatEl);
  22943. uiUtils.copyAttributes(el, seatEl);
  22944. } else {
  22945. if (typeof holder == 'string') {
  22946. holder = document.getElementById(holder);
  22947. }
  22948. holder = holder || uiUtils.getFixedLayer();
  22949. domUtils.addClass(holder, theme);
  22950. holder.appendChild(el);
  22951. }
  22952. this.postRender();
  22953. },
  22954. getDom:function (name) {
  22955. if (!name) {
  22956. return document.getElementById(this.id);
  22957. } else {
  22958. return document.getElementById(this.id + '_' + name);
  22959. }
  22960. },
  22961. postRender:function () {
  22962. this.fireEvent('postrender');
  22963. },
  22964. getHtmlTpl:function () {
  22965. return '';
  22966. },
  22967. formatHtml:function (tpl) {
  22968. var prefix = 'edui-' + this.uiName;
  22969. return (tpl
  22970. .replace(/##/g, this.id)
  22971. .replace(/%%-/g, this.uiName ? prefix + '-' : '')
  22972. .replace(/%%/g, (this.uiName ? prefix : '') + ' ' + this.className)
  22973. .replace(/\$\$/g, this._globalKey));
  22974. },
  22975. renderHtml:function () {
  22976. return this.formatHtml(this.getHtmlTpl());
  22977. },
  22978. dispose:function () {
  22979. var box = this.getDom();
  22980. if (box) baidu.editor.dom.domUtils.remove(box);
  22981. uiUtils.unsetGlobal(this.id);
  22982. }
  22983. };
  22984. utils.inherits(UIBase, EventBase);
  22985. })();
  22986. // ui/separator.js
  22987. (function (){
  22988. var utils = baidu.editor.utils,
  22989. UIBase = baidu.editor.ui.UIBase,
  22990. Separator = baidu.editor.ui.Separator = function (options){
  22991. this.initOptions(options);
  22992. this.initSeparator();
  22993. };
  22994. Separator.prototype = {
  22995. uiName: 'separator',
  22996. initSeparator: function (){
  22997. this.initUIBase();
  22998. },
  22999. getHtmlTpl: function (){
  23000. return '<div id="##" class="edui-box %%"></div>';
  23001. }
  23002. };
  23003. utils.inherits(Separator, UIBase);
  23004. })();
  23005. // ui/mask.js
  23006. ///import core
  23007. ///import uicore
  23008. (function (){
  23009. var utils = baidu.editor.utils,
  23010. domUtils = baidu.editor.dom.domUtils,
  23011. UIBase = baidu.editor.ui.UIBase,
  23012. uiUtils = baidu.editor.ui.uiUtils;
  23013. var Mask = baidu.editor.ui.Mask = function (options){
  23014. this.initOptions(options);
  23015. this.initUIBase();
  23016. };
  23017. Mask.prototype = {
  23018. getHtmlTpl: function (){
  23019. return '<div id="##" class="edui-mask %%" onclick="return $$._onClick(event, this);" onmousedown="return $$._onMouseDown(event, this);"></div>';
  23020. },
  23021. postRender: function (){
  23022. var me = this;
  23023. domUtils.on(window, 'resize', function (){
  23024. setTimeout(function (){
  23025. if (!me.isHidden()) {
  23026. me._fill();
  23027. }
  23028. });
  23029. });
  23030. },
  23031. show: function (zIndex){
  23032. this._fill();
  23033. this.getDom().style.display = '';
  23034. this.getDom().style.zIndex = zIndex;
  23035. },
  23036. hide: function (){
  23037. this.getDom().style.display = 'none';
  23038. this.getDom().style.zIndex = '';
  23039. },
  23040. isHidden: function (){
  23041. return this.getDom().style.display == 'none';
  23042. },
  23043. _onMouseDown: function (){
  23044. return false;
  23045. },
  23046. _onClick: function (e, target){
  23047. this.fireEvent('click', e, target);
  23048. },
  23049. _fill: function (){
  23050. var el = this.getDom();
  23051. var vpRect = uiUtils.getViewportRect();
  23052. el.style.width = vpRect.width + 'px';
  23053. el.style.height = vpRect.height + 'px';
  23054. }
  23055. };
  23056. utils.inherits(Mask, UIBase);
  23057. })();
  23058. // ui/popup.js
  23059. ///import core
  23060. ///import uicore
  23061. (function () {
  23062. var utils = baidu.editor.utils,
  23063. uiUtils = baidu.editor.ui.uiUtils,
  23064. domUtils = baidu.editor.dom.domUtils,
  23065. UIBase = baidu.editor.ui.UIBase,
  23066. Popup = baidu.editor.ui.Popup = function (options){
  23067. this.initOptions(options);
  23068. this.initPopup();
  23069. };
  23070. var allPopups = [];
  23071. function closeAllPopup( evt,el ){
  23072. for ( var i = 0; i < allPopups.length; i++ ) {
  23073. var pop = allPopups[i];
  23074. if (!pop.isHidden()) {
  23075. if (pop.queryAutoHide(el) !== false) {
  23076. if(evt&&/scroll/ig.test(evt.type)&&pop.className=="edui-wordpastepop") return;
  23077. pop.hide();
  23078. }
  23079. }
  23080. }
  23081. if(allPopups.length)
  23082. pop.editor.fireEvent("afterhidepop");
  23083. }
  23084. Popup.postHide = closeAllPopup;
  23085. var ANCHOR_CLASSES = ['edui-anchor-topleft','edui-anchor-topright',
  23086. 'edui-anchor-bottomleft','edui-anchor-bottomright'];
  23087. Popup.prototype = {
  23088. SHADOW_RADIUS: 5,
  23089. content: null,
  23090. _hidden: false,
  23091. autoRender: true,
  23092. canSideLeft: true,
  23093. canSideUp: true,
  23094. initPopup: function (){
  23095. this.initUIBase();
  23096. allPopups.push( this );
  23097. },
  23098. getHtmlTpl: function (){
  23099. return '<div id="##" class="edui-popup %%" onmousedown="return false;">' +
  23100. ' <div id="##_body" class="edui-popup-body">' +
  23101. ' <iframe style="position:absolute;z-index:-1;left:0;top:0;background-color: transparent;" frameborder="0" width="100%" height="100%" src="about:blank"></iframe>' +
  23102. ' <div class="edui-shadow"></div>' +
  23103. ' <div id="##_content" class="edui-popup-content">' +
  23104. this.getContentHtmlTpl() +
  23105. ' </div>' +
  23106. ' </div>' +
  23107. '</div>';
  23108. },
  23109. getContentHtmlTpl: function (){
  23110. if(this.content){
  23111. if (typeof this.content == 'string') {
  23112. return this.content;
  23113. }
  23114. return this.content.renderHtml();
  23115. }else{
  23116. return ''
  23117. }
  23118. },
  23119. _UIBase_postRender: UIBase.prototype.postRender,
  23120. postRender: function (){
  23121. if (this.content instanceof UIBase) {
  23122. this.content.postRender();
  23123. }
  23124. //捕获鼠标滚轮
  23125. if( this.captureWheel && !this.captured ) {
  23126. this.captured = true;
  23127. var winHeight = ( document.documentElement.clientHeight || document.body.clientHeight ) - 80,
  23128. _height = this.getDom().offsetHeight,
  23129. _top = uiUtils.getClientRect( this.combox.getDom() ).top,
  23130. content = this.getDom('content'),
  23131. ifr = this.getDom('body').getElementsByTagName('iframe'),
  23132. me = this;
  23133. ifr.length && ( ifr = ifr[0] );
  23134. while( _top + _height > winHeight ) {
  23135. _height -= 30;
  23136. }
  23137. content.style.height = _height + 'px';
  23138. //同步更改iframe高度
  23139. ifr && ( ifr.style.height = _height + 'px' );
  23140. //阻止在combox上的鼠标滚轮事件, 防止用户的正常操作被误解
  23141. if( window.XMLHttpRequest ) {
  23142. domUtils.on( content, ( 'onmousewheel' in document.body ) ? 'mousewheel' :'DOMMouseScroll' , function(e){
  23143. if(e.preventDefault) {
  23144. e.preventDefault();
  23145. } else {
  23146. e.returnValue = false;
  23147. }
  23148. if( e.wheelDelta ) {
  23149. content.scrollTop -= ( e.wheelDelta / 120 )*60;
  23150. } else {
  23151. content.scrollTop -= ( e.detail / -3 )*60;
  23152. }
  23153. });
  23154. } else {
  23155. //ie6
  23156. domUtils.on( this.getDom(), 'mousewheel' , function(e){
  23157. e.returnValue = false;
  23158. me.getDom('content').scrollTop -= ( e.wheelDelta / 120 )*60;
  23159. });
  23160. }
  23161. }
  23162. this.fireEvent('postRenderAfter');
  23163. this.hide(true);
  23164. this._UIBase_postRender();
  23165. },
  23166. _doAutoRender: function (){
  23167. if (!this.getDom() && this.autoRender) {
  23168. this.render();
  23169. }
  23170. },
  23171. mesureSize: function (){
  23172. var box = this.getDom('content');
  23173. return uiUtils.getClientRect(box);
  23174. },
  23175. fitSize: function (){
  23176. if( this.captureWheel && this.sized ) {
  23177. return this.__size;
  23178. }
  23179. this.sized = true;
  23180. var popBodyEl = this.getDom('body');
  23181. popBodyEl.style.width = '';
  23182. popBodyEl.style.height = '';
  23183. var size = this.mesureSize();
  23184. if( this.captureWheel ) {
  23185. popBodyEl.style.width = -(-20 -size.width) + 'px';
  23186. var height = parseInt( this.getDom('content').style.height, 10 );
  23187. !window.isNaN( height ) && ( size.height = height );
  23188. } else {
  23189. popBodyEl.style.width = size.width + 'px';
  23190. }
  23191. popBodyEl.style.height = size.height + 'px';
  23192. this.__size = size;
  23193. this.captureWheel && (this.getDom('content').style.overflow = 'auto');
  23194. return size;
  23195. },
  23196. showAnchor: function ( element, hoz ){
  23197. this.showAnchorRect( uiUtils.getClientRect( element ), hoz );
  23198. },
  23199. showAnchorRect: function ( rect, hoz, adj ){
  23200. this._doAutoRender();
  23201. var vpRect = uiUtils.getViewportRect();
  23202. this.getDom().style.visibility = 'hidden';
  23203. this._show();
  23204. var popSize = this.fitSize();
  23205. var sideLeft, sideUp, left, top;
  23206. if (hoz) {
  23207. sideLeft = this.canSideLeft && (rect.right + popSize.width > vpRect.right && rect.left > popSize.width);
  23208. sideUp = this.canSideUp && (rect.top + popSize.height > vpRect.bottom && rect.bottom > popSize.height);
  23209. left = (sideLeft ? rect.left - popSize.width : rect.right);
  23210. top = (sideUp ? rect.bottom - popSize.height : rect.top);
  23211. } else {
  23212. sideLeft = this.canSideLeft && (rect.right + popSize.width > vpRect.right && rect.left > popSize.width);
  23213. sideUp = this.canSideUp && (rect.top + popSize.height > vpRect.bottom && rect.bottom > popSize.height);
  23214. left = (sideLeft ? rect.right - popSize.width : rect.left);
  23215. top = (sideUp ? rect.top - popSize.height : rect.bottom);
  23216. }
  23217. var popEl = this.getDom();
  23218. uiUtils.setViewportOffset(popEl, {
  23219. left: left,
  23220. top: top
  23221. });
  23222. domUtils.removeClasses(popEl, ANCHOR_CLASSES);
  23223. popEl.className += ' ' + ANCHOR_CLASSES[(sideUp ? 1 : 0) * 2 + (sideLeft ? 1 : 0)];
  23224. if(this.editor){
  23225. popEl.style.zIndex = this.editor.container.style.zIndex * 1 + 10;
  23226. baidu.editor.ui.uiUtils.getFixedLayer().style.zIndex = popEl.style.zIndex - 1;
  23227. }
  23228. this.getDom().style.visibility = 'visible';
  23229. },
  23230. showAt: function (offset) {
  23231. var left = offset.left;
  23232. var top = offset.top;
  23233. var rect = {
  23234. left: left,
  23235. top: top,
  23236. right: left,
  23237. bottom: top,
  23238. height: 0,
  23239. width: 0
  23240. };
  23241. this.showAnchorRect(rect, false, true);
  23242. },
  23243. _show: function (){
  23244. if (this._hidden) {
  23245. var box = this.getDom();
  23246. box.style.display = '';
  23247. this._hidden = false;
  23248. // if (box.setActive) {
  23249. // box.setActive();
  23250. // }
  23251. this.fireEvent('show');
  23252. }
  23253. },
  23254. isHidden: function (){
  23255. return this._hidden;
  23256. },
  23257. show: function (){
  23258. this._doAutoRender();
  23259. this._show();
  23260. },
  23261. hide: function (notNofity){
  23262. if (!this._hidden && this.getDom()) {
  23263. this.getDom().style.display = 'none';
  23264. this._hidden = true;
  23265. if (!notNofity) {
  23266. this.fireEvent('hide');
  23267. }
  23268. }
  23269. },
  23270. queryAutoHide: function (el){
  23271. return !el || !uiUtils.contains(this.getDom(), el);
  23272. }
  23273. };
  23274. utils.inherits(Popup, UIBase);
  23275. domUtils.on( document, 'mousedown', function ( evt ) {
  23276. var el = evt.target || evt.srcElement;
  23277. closeAllPopup( evt,el );
  23278. } );
  23279. domUtils.on( window, 'scroll', function (evt,el) {
  23280. closeAllPopup( evt,el );
  23281. } );
  23282. })();
  23283. // ui/colorpicker.js
  23284. ///import core
  23285. ///import uicore
  23286. (function (){
  23287. var utils = baidu.editor.utils,
  23288. UIBase = baidu.editor.ui.UIBase,
  23289. ColorPicker = baidu.editor.ui.ColorPicker = function (options){
  23290. this.initOptions(options);
  23291. this.noColorText = this.noColorText || this.editor.getLang("clearColor");
  23292. this.initUIBase();
  23293. };
  23294. ColorPicker.prototype = {
  23295. getHtmlTpl: function (){
  23296. return genColorPicker(this.noColorText,this.editor);
  23297. },
  23298. _onTableClick: function (evt){
  23299. var tgt = evt.target || evt.srcElement;
  23300. var color = tgt.getAttribute('data-color');
  23301. if (color) {
  23302. this.fireEvent('pickcolor', color);
  23303. }
  23304. },
  23305. _onTableOver: function (evt){
  23306. var tgt = evt.target || evt.srcElement;
  23307. var color = tgt.getAttribute('data-color');
  23308. if (color) {
  23309. this.getDom('preview').style.backgroundColor = color;
  23310. }
  23311. },
  23312. _onTableOut: function (){
  23313. this.getDom('preview').style.backgroundColor = '';
  23314. },
  23315. _onPickNoColor: function (){
  23316. this.fireEvent('picknocolor');
  23317. }
  23318. };
  23319. utils.inherits(ColorPicker, UIBase);
  23320. var COLORS = (
  23321. 'ffffff,000000,eeece1,1f497d,4f81bd,c0504d,9bbb59,8064a2,4bacc6,f79646,' +
  23322. 'f2f2f2,7f7f7f,ddd9c3,c6d9f0,dbe5f1,f2dcdb,ebf1dd,e5e0ec,dbeef3,fdeada,' +
  23323. 'd8d8d8,595959,c4bd97,8db3e2,b8cce4,e5b9b7,d7e3bc,ccc1d9,b7dde8,fbd5b5,' +
  23324. 'bfbfbf,3f3f3f,938953,548dd4,95b3d7,d99694,c3d69b,b2a2c7,92cddc,fac08f,' +
  23325. 'a5a5a5,262626,494429,17365d,366092,953734,76923c,5f497a,31859b,e36c09,' +
  23326. '7f7f7f,0c0c0c,1d1b10,0f243e,244061,632423,4f6128,3f3151,205867,974806,' +
  23327. 'c00000,ff0000,ffc000,ffff00,92d050,00b050,00b0f0,0070c0,002060,7030a0,').split(',');
  23328. function genColorPicker(noColorText,editor){
  23329. var html = '<div id="##" class="edui-colorpicker %%">' +
  23330. '<div class="edui-colorpicker-topbar edui-clearfix">' +
  23331. '<div unselectable="on" id="##_preview" class="edui-colorpicker-preview"></div>' +
  23332. '<div unselectable="on" class="edui-colorpicker-nocolor" onclick="$$._onPickNoColor(event, this);">'+ noColorText +'</div>' +
  23333. '</div>' +
  23334. '<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">' +
  23335. '<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>'+
  23336. '<tr class="edui-colorpicker-tablefirstrow" >';
  23337. for (var i=0; i<COLORS.length; i++) {
  23338. if (i && i%10 === 0) {
  23339. 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"':'')+'>';
  23340. }
  23341. 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"' +
  23342. ' data-color="#'+ COLORS[i] +'"'+
  23343. ' style="background-color:#'+ COLORS[i] +';border:solid #ccc;'+
  23344. (i<10 || i>=60?'border-width:1px;':
  23345. i>=10&&i<20?'border-width:1px 1px 0 1px;':
  23346. 'border-width:0 1px 0 1px;')+
  23347. '"' +
  23348. '></a></td>':'';
  23349. }
  23350. html += '</tr></table></div>';
  23351. return html;
  23352. }
  23353. })();
  23354. // ui/tablepicker.js
  23355. ///import core
  23356. ///import uicore
  23357. (function (){
  23358. var utils = baidu.editor.utils,
  23359. uiUtils = baidu.editor.ui.uiUtils,
  23360. UIBase = baidu.editor.ui.UIBase;
  23361. var TablePicker = baidu.editor.ui.TablePicker = function (options){
  23362. this.initOptions(options);
  23363. this.initTablePicker();
  23364. };
  23365. TablePicker.prototype = {
  23366. defaultNumRows: 10,
  23367. defaultNumCols: 10,
  23368. maxNumRows: 20,
  23369. maxNumCols: 20,
  23370. numRows: 10,
  23371. numCols: 10,
  23372. lengthOfCellSide: 22,
  23373. initTablePicker: function (){
  23374. this.initUIBase();
  23375. },
  23376. getHtmlTpl: function (){
  23377. var me = this;
  23378. return '<div id="##" class="edui-tablepicker %%">' +
  23379. '<div class="edui-tablepicker-body">' +
  23380. '<div class="edui-infoarea">' +
  23381. '<span id="##_label" class="edui-label"></span>' +
  23382. '</div>' +
  23383. '<div class="edui-pickarea"' +
  23384. ' onmousemove="$$._onMouseMove(event, this);"' +
  23385. ' onmouseover="$$._onMouseOver(event, this);"' +
  23386. ' onmouseout="$$._onMouseOut(event, this);"' +
  23387. ' onclick="$$._onClick(event, this);"' +
  23388. '>' +
  23389. '<div id="##_overlay" class="edui-overlay"></div>' +
  23390. '</div>' +
  23391. '</div>' +
  23392. '</div>';
  23393. },
  23394. _UIBase_render: UIBase.prototype.render,
  23395. render: function (holder){
  23396. this._UIBase_render(holder);
  23397. this.getDom('label').innerHTML = '0'+this.editor.getLang("t_row")+' x 0'+this.editor.getLang("t_col");
  23398. },
  23399. _track: function (numCols, numRows){
  23400. var style = this.getDom('overlay').style;
  23401. var sideLen = this.lengthOfCellSide;
  23402. style.width = numCols * sideLen + 'px';
  23403. style.height = numRows * sideLen + 'px';
  23404. var label = this.getDom('label');
  23405. label.innerHTML = numCols +this.editor.getLang("t_col")+' x ' + numRows + this.editor.getLang("t_row");
  23406. this.numCols = numCols;
  23407. this.numRows = numRows;
  23408. },
  23409. _onMouseOver: function (evt, el){
  23410. var rel = evt.relatedTarget || evt.fromElement;
  23411. if (!uiUtils.contains(el, rel) && el !== rel) {
  23412. this.getDom('label').innerHTML = '0'+this.editor.getLang("t_col")+' x 0'+this.editor.getLang("t_row");
  23413. this.getDom('overlay').style.visibility = '';
  23414. }
  23415. },
  23416. _onMouseOut: function (evt, el){
  23417. var rel = evt.relatedTarget || evt.toElement;
  23418. if (!uiUtils.contains(el, rel) && el !== rel) {
  23419. this.getDom('label').innerHTML = '0'+this.editor.getLang("t_col")+' x 0'+this.editor.getLang("t_row");
  23420. this.getDom('overlay').style.visibility = 'hidden';
  23421. }
  23422. },
  23423. _onMouseMove: function (evt, el){
  23424. var style = this.getDom('overlay').style;
  23425. var offset = uiUtils.getEventOffset(evt);
  23426. var sideLen = this.lengthOfCellSide;
  23427. var numCols = Math.ceil(offset.left / sideLen);
  23428. var numRows = Math.ceil(offset.top / sideLen);
  23429. this._track(numCols, numRows);
  23430. },
  23431. _onClick: function (){
  23432. this.fireEvent('picktable', this.numCols, this.numRows);
  23433. }
  23434. };
  23435. utils.inherits(TablePicker, UIBase);
  23436. })();
  23437. // ui/stateful.js
  23438. (function (){
  23439. var browser = baidu.editor.browser,
  23440. domUtils = baidu.editor.dom.domUtils,
  23441. uiUtils = baidu.editor.ui.uiUtils;
  23442. var TPL_STATEFUL = 'onmousedown="$$.Stateful_onMouseDown(event, this);"' +
  23443. ' onmouseup="$$.Stateful_onMouseUp(event, this);"' +
  23444. ( browser.ie ? (
  23445. ' onmouseenter="$$.Stateful_onMouseEnter(event, this);"' +
  23446. ' onmouseleave="$$.Stateful_onMouseLeave(event, this);"' )
  23447. : (
  23448. ' onmouseover="$$.Stateful_onMouseOver(event, this);"' +
  23449. ' onmouseout="$$.Stateful_onMouseOut(event, this);"' ));
  23450. baidu.editor.ui.Stateful = {
  23451. alwalysHoverable: false,
  23452. target:null,//目标元素和this指向dom不一样
  23453. Stateful_init: function (){
  23454. this._Stateful_dGetHtmlTpl = this.getHtmlTpl;
  23455. this.getHtmlTpl = this.Stateful_getHtmlTpl;
  23456. },
  23457. Stateful_getHtmlTpl: function (){
  23458. var tpl = this._Stateful_dGetHtmlTpl();
  23459. // 使用function避免$转义
  23460. return tpl.replace(/stateful/g, function (){ return TPL_STATEFUL; });
  23461. },
  23462. Stateful_onMouseEnter: function (evt, el){
  23463. this.target=el;
  23464. if (!this.isDisabled() || this.alwalysHoverable) {
  23465. this.addState('hover');
  23466. this.fireEvent('over');
  23467. }
  23468. },
  23469. Stateful_onMouseLeave: function (evt, el){
  23470. if (!this.isDisabled() || this.alwalysHoverable) {
  23471. this.removeState('hover');
  23472. this.removeState('active');
  23473. this.fireEvent('out');
  23474. }
  23475. },
  23476. Stateful_onMouseOver: function (evt, el){
  23477. var rel = evt.relatedTarget;
  23478. if (!uiUtils.contains(el, rel) && el !== rel) {
  23479. this.Stateful_onMouseEnter(evt, el);
  23480. }
  23481. },
  23482. Stateful_onMouseOut: function (evt, el){
  23483. var rel = evt.relatedTarget;
  23484. if (!uiUtils.contains(el, rel) && el !== rel) {
  23485. this.Stateful_onMouseLeave(evt, el);
  23486. }
  23487. },
  23488. Stateful_onMouseDown: function (evt, el){
  23489. if (!this.isDisabled()) {
  23490. this.addState('active');
  23491. }
  23492. },
  23493. Stateful_onMouseUp: function (evt, el){
  23494. if (!this.isDisabled()) {
  23495. this.removeState('active');
  23496. }
  23497. },
  23498. Stateful_postRender: function (){
  23499. if (this.disabled && !this.hasState('disabled')) {
  23500. this.addState('disabled');
  23501. }
  23502. },
  23503. hasState: function (state){
  23504. return domUtils.hasClass(this.getStateDom(), 'edui-state-' + state);
  23505. },
  23506. addState: function (state){
  23507. if (!this.hasState(state)) {
  23508. this.getStateDom().className += ' edui-state-' + state;
  23509. }
  23510. },
  23511. removeState: function (state){
  23512. if (this.hasState(state)) {
  23513. domUtils.removeClasses(this.getStateDom(), ['edui-state-' + state]);
  23514. }
  23515. },
  23516. getStateDom: function (){
  23517. return this.getDom('state');
  23518. },
  23519. isChecked: function (){
  23520. return this.hasState('checked');
  23521. },
  23522. setChecked: function (checked){
  23523. if (!this.isDisabled() && checked) {
  23524. this.addState('checked');
  23525. } else {
  23526. this.removeState('checked');
  23527. }
  23528. },
  23529. isDisabled: function (){
  23530. return this.hasState('disabled');
  23531. },
  23532. setDisabled: function (disabled){
  23533. if (disabled) {
  23534. this.removeState('hover');
  23535. this.removeState('checked');
  23536. this.removeState('active');
  23537. this.addState('disabled');
  23538. } else {
  23539. this.removeState('disabled');
  23540. }
  23541. }
  23542. };
  23543. })();
  23544. // ui/button.js
  23545. ///import core
  23546. ///import uicore
  23547. ///import ui/stateful.js
  23548. (function (){
  23549. var utils = baidu.editor.utils,
  23550. UIBase = baidu.editor.ui.UIBase,
  23551. Stateful = baidu.editor.ui.Stateful,
  23552. Button = baidu.editor.ui.Button = function (options){
  23553. if(options.name){
  23554. var btnName = options.name;
  23555. var cssRules = options.cssRules;
  23556. if(!options.className){
  23557. options.className = 'edui-for-' + btnName;
  23558. }
  23559. options.cssRules = '.edui-default .edui-for-'+ btnName +' .edui-icon {'+ cssRules +'}'
  23560. }
  23561. this.initOptions(options);
  23562. this.initButton();
  23563. };
  23564. Button.prototype = {
  23565. uiName: 'button',
  23566. label: '',
  23567. title: '',
  23568. showIcon: true,
  23569. showText: true,
  23570. cssRules:'',
  23571. initButton: function (){
  23572. this.initUIBase();
  23573. this.Stateful_init();
  23574. if(this.cssRules){
  23575. utils.cssRule('edui-customize-'+this.name+'-style',this.cssRules);
  23576. }
  23577. },
  23578. getHtmlTpl: function (){
  23579. return '<div id="##" class="edui-box %%">' +
  23580. '<div id="##_state" stateful>' +
  23581. '<div class="%%-wrap"><div id="##_body" unselectable="on" ' + (this.title ? 'title="' + this.title + '"' : '') +
  23582. ' class="%%-body" onmousedown="return $$._onMouseDown(event, this);" onclick="return $$._onClick(event, this);">' +
  23583. (this.showIcon ? '<div class="edui-box edui-icon"></div>' : '') +
  23584. (this.showText ? '<div class="edui-box edui-label">' + this.label + '</div>' : '') +
  23585. '</div>' +
  23586. '</div>' +
  23587. '</div></div>';
  23588. },
  23589. postRender: function (){
  23590. this.Stateful_postRender();
  23591. this.setDisabled(this.disabled)
  23592. },
  23593. _onMouseDown: function (e){
  23594. var target = e.target || e.srcElement,
  23595. tagName = target && target.tagName && target.tagName.toLowerCase();
  23596. if (tagName == 'input' || tagName == 'object' || tagName == 'object') {
  23597. return false;
  23598. }
  23599. },
  23600. _onClick: function (){
  23601. if (!this.isDisabled()) {
  23602. this.fireEvent('click');
  23603. }
  23604. },
  23605. setTitle: function(text){
  23606. var label = this.getDom('label');
  23607. label.innerHTML = text;
  23608. }
  23609. };
  23610. utils.inherits(Button, UIBase);
  23611. utils.extend(Button.prototype, Stateful);
  23612. })();
  23613. // ui/splitbutton.js
  23614. ///import core
  23615. ///import uicore
  23616. ///import ui/stateful.js
  23617. (function (){
  23618. var utils = baidu.editor.utils,
  23619. uiUtils = baidu.editor.ui.uiUtils,
  23620. domUtils = baidu.editor.dom.domUtils,
  23621. UIBase = baidu.editor.ui.UIBase,
  23622. Stateful = baidu.editor.ui.Stateful,
  23623. SplitButton = baidu.editor.ui.SplitButton = function (options){
  23624. this.initOptions(options);
  23625. this.initSplitButton();
  23626. };
  23627. SplitButton.prototype = {
  23628. popup: null,
  23629. uiName: 'splitbutton',
  23630. title: '',
  23631. initSplitButton: function (){
  23632. this.initUIBase();
  23633. this.Stateful_init();
  23634. var me = this;
  23635. if (this.popup != null) {
  23636. var popup = this.popup;
  23637. this.popup = null;
  23638. this.setPopup(popup);
  23639. }
  23640. },
  23641. _UIBase_postRender: UIBase.prototype.postRender,
  23642. postRender: function (){
  23643. this.Stateful_postRender();
  23644. this._UIBase_postRender();
  23645. },
  23646. setPopup: function (popup){
  23647. if (this.popup === popup) return;
  23648. if (this.popup != null) {
  23649. this.popup.dispose();
  23650. }
  23651. popup.addListener('show', utils.bind(this._onPopupShow, this));
  23652. popup.addListener('hide', utils.bind(this._onPopupHide, this));
  23653. popup.addListener('postrender', utils.bind(function (){
  23654. popup.getDom('body').appendChild(
  23655. uiUtils.createElementByHtml('<div id="' +
  23656. this.popup.id + '_bordereraser" class="edui-bordereraser edui-background" style="width:' +
  23657. (uiUtils.getClientRect(this.getDom()).width + 20) + 'px"></div>')
  23658. );
  23659. popup.getDom().className += ' ' + this.className;
  23660. }, this));
  23661. this.popup = popup;
  23662. },
  23663. _onPopupShow: function (){
  23664. this.addState('opened');
  23665. },
  23666. _onPopupHide: function (){
  23667. this.removeState('opened');
  23668. },
  23669. getHtmlTpl: function (){
  23670. return '<div id="##" class="edui-box %%">' +
  23671. '<div '+ (this.title ? 'title="' + this.title + '"' : '') +' id="##_state" stateful><div class="%%-body">' +
  23672. '<div id="##_button_body" class="edui-box edui-button-body" onclick="$$._onButtonClick(event, this);">' +
  23673. '<div class="edui-box edui-icon"></div>' +
  23674. '</div>' +
  23675. '<div class="edui-box edui-splitborder"></div>' +
  23676. '<div class="edui-box edui-arrow" onclick="$$._onArrowClick();"></div>' +
  23677. '</div></div></div>';
  23678. },
  23679. showPopup: function (){
  23680. // 当popup往上弹出的时候,做特殊处理
  23681. var rect = uiUtils.getClientRect(this.getDom());
  23682. rect.top -= this.popup.SHADOW_RADIUS;
  23683. rect.height += this.popup.SHADOW_RADIUS;
  23684. this.popup.showAnchorRect(rect);
  23685. },
  23686. _onArrowClick: function (event, el){
  23687. if (!this.isDisabled()) {
  23688. this.showPopup();
  23689. }
  23690. },
  23691. _onButtonClick: function (){
  23692. if (!this.isDisabled()) {
  23693. this.fireEvent('buttonclick');
  23694. }
  23695. }
  23696. };
  23697. utils.inherits(SplitButton, UIBase);
  23698. utils.extend(SplitButton.prototype, Stateful, true);
  23699. })();
  23700. // ui/colorbutton.js
  23701. ///import core
  23702. ///import uicore
  23703. ///import ui/colorpicker.js
  23704. ///import ui/popup.js
  23705. ///import ui/splitbutton.js
  23706. (function (){
  23707. var utils = baidu.editor.utils,
  23708. uiUtils = baidu.editor.ui.uiUtils,
  23709. ColorPicker = baidu.editor.ui.ColorPicker,
  23710. Popup = baidu.editor.ui.Popup,
  23711. SplitButton = baidu.editor.ui.SplitButton,
  23712. ColorButton = baidu.editor.ui.ColorButton = function (options){
  23713. this.initOptions(options);
  23714. this.initColorButton();
  23715. };
  23716. ColorButton.prototype = {
  23717. initColorButton: function (){
  23718. var me = this;
  23719. this.popup = new Popup({
  23720. content: new ColorPicker({
  23721. noColorText: me.editor.getLang("clearColor"),
  23722. editor:me.editor,
  23723. onpickcolor: function (t, color){
  23724. me._onPickColor(color);
  23725. },
  23726. onpicknocolor: function (t, color){
  23727. me._onPickNoColor(color);
  23728. }
  23729. }),
  23730. editor:me.editor
  23731. });
  23732. this.initSplitButton();
  23733. },
  23734. _SplitButton_postRender: SplitButton.prototype.postRender,
  23735. postRender: function (){
  23736. this._SplitButton_postRender();
  23737. this.getDom('button_body').appendChild(
  23738. uiUtils.createElementByHtml('<div id="' + this.id + '_colorlump" class="edui-colorlump"></div>')
  23739. );
  23740. this.getDom().className += ' edui-colorbutton';
  23741. },
  23742. setColor: function (color){
  23743. this.getDom('colorlump').style.backgroundColor = color;
  23744. this.color = color;
  23745. },
  23746. _onPickColor: function (color){
  23747. if (this.fireEvent('pickcolor', color) !== false) {
  23748. this.setColor(color);
  23749. this.popup.hide();
  23750. }
  23751. },
  23752. _onPickNoColor: function (color){
  23753. if (this.fireEvent('picknocolor') !== false) {
  23754. this.popup.hide();
  23755. }
  23756. }
  23757. };
  23758. utils.inherits(ColorButton, SplitButton);
  23759. })();
  23760. // ui/tablebutton.js
  23761. ///import core
  23762. ///import uicore
  23763. ///import ui/popup.js
  23764. ///import ui/tablepicker.js
  23765. ///import ui/splitbutton.js
  23766. (function (){
  23767. var utils = baidu.editor.utils,
  23768. Popup = baidu.editor.ui.Popup,
  23769. TablePicker = baidu.editor.ui.TablePicker,
  23770. SplitButton = baidu.editor.ui.SplitButton,
  23771. TableButton = baidu.editor.ui.TableButton = function (options){
  23772. this.initOptions(options);
  23773. this.initTableButton();
  23774. };
  23775. TableButton.prototype = {
  23776. initTableButton: function (){
  23777. var me = this;
  23778. this.popup = new Popup({
  23779. content: new TablePicker({
  23780. editor:me.editor,
  23781. onpicktable: function (t, numCols, numRows){
  23782. me._onPickTable(numCols, numRows);
  23783. }
  23784. }),
  23785. 'editor':me.editor
  23786. });
  23787. this.initSplitButton();
  23788. },
  23789. _onPickTable: function (numCols, numRows){
  23790. if (this.fireEvent('picktable', numCols, numRows) !== false) {
  23791. this.popup.hide();
  23792. }
  23793. }
  23794. };
  23795. utils.inherits(TableButton, SplitButton);
  23796. })();
  23797. // ui/autotypesetpicker.js
  23798. ///import core
  23799. ///import uicore
  23800. (function () {
  23801. var utils = baidu.editor.utils,
  23802. UIBase = baidu.editor.ui.UIBase;
  23803. var AutoTypeSetPicker = baidu.editor.ui.AutoTypeSetPicker = function (options) {
  23804. this.initOptions(options);
  23805. this.initAutoTypeSetPicker();
  23806. };
  23807. AutoTypeSetPicker.prototype = {
  23808. initAutoTypeSetPicker:function () {
  23809. this.initUIBase();
  23810. },
  23811. getHtmlTpl:function () {
  23812. var me = this.editor,
  23813. opt = me.options.autotypeset,
  23814. lang = me.getLang("autoTypeSet");
  23815. var textAlignInputName = 'textAlignValue' + me.uid,
  23816. imageBlockInputName = 'imageBlockLineValue' + me.uid,
  23817. symbolConverInputName = 'symbolConverValue' + me.uid;
  23818. return '<div id="##" class="edui-autotypesetpicker %%">' +
  23819. '<div class="edui-autotypesetpicker-body">' +
  23820. '<table >' +
  23821. '<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>' +
  23822. '<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>' +
  23823. '<tr>' +
  23824. '<td nowrap><input type="checkbox" name="textAlign" ' + (opt["textAlign"] ? "checked" : "" ) + '>' + lang.alignment + '</td>' +
  23825. '<td colspan="2" id="' + textAlignInputName + '">' +
  23826. '<input type="radio" name="'+ textAlignInputName +'" value="left" ' + ((opt["textAlign"] && opt["textAlign"] == "left") ? "checked" : "") + '>' + me.getLang("justifyleft") +
  23827. '<input type="radio" name="'+ textAlignInputName +'" value="center" ' + ((opt["textAlign"] && opt["textAlign"] == "center") ? "checked" : "") + '>' + me.getLang("justifycenter") +
  23828. '<input type="radio" name="'+ textAlignInputName +'" value="right" ' + ((opt["textAlign"] && opt["textAlign"] == "right") ? "checked" : "") + '>' + me.getLang("justifyright") +
  23829. '</td>' +
  23830. '</tr>' +
  23831. '<tr>' +
  23832. '<td nowrap><input type="checkbox" name="imageBlockLine" ' + (opt["imageBlockLine"] ? "checked" : "" ) + '>' + lang.imageFloat + '</td>' +
  23833. '<td nowrap id="'+ imageBlockInputName +'">' +
  23834. '<input type="radio" name="'+ imageBlockInputName +'" value="none" ' + ((opt["imageBlockLine"] && opt["imageBlockLine"] == "none") ? "checked" : "") + '>' + me.getLang("default") +
  23835. '<input type="radio" name="'+ imageBlockInputName +'" value="left" ' + ((opt["imageBlockLine"] && opt["imageBlockLine"] == "left") ? "checked" : "") + '>' + me.getLang("justifyleft") +
  23836. '<input type="radio" name="'+ imageBlockInputName +'" value="center" ' + ((opt["imageBlockLine"] && opt["imageBlockLine"] == "center") ? "checked" : "") + '>' + me.getLang("justifycenter") +
  23837. '<input type="radio" name="'+ imageBlockInputName +'" value="right" ' + ((opt["imageBlockLine"] && opt["imageBlockLine"] == "right") ? "checked" : "") + '>' + me.getLang("justifyright") +
  23838. '</td>' +
  23839. '</tr>' +
  23840. '<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>' +
  23841. '<tr><td nowrap colspan="3"><input type="checkbox" name="removeEmptyNode" ' + (opt["removeEmptyNode"] ? "checked" : "" ) + '>' + lang.removeHtml + '</td></tr>' +
  23842. '<tr><td nowrap colspan="3"><input type="checkbox" name="pasteFilter" ' + (opt["pasteFilter"] ? "checked" : "" ) + '>' + lang.pasteFilter + '</td></tr>' +
  23843. '<tr>' +
  23844. '<td nowrap><input type="checkbox" name="symbolConver" ' + (opt["bdc2sb"] || opt["tobdc"] ? "checked" : "" ) + '>' + lang.symbol + '</td>' +
  23845. '<td id="' + symbolConverInputName + '">' +
  23846. '<input type="radio" name="bdc" value="bdc2sb" ' + (opt["bdc2sb"] ? "checked" : "" ) + '>' + lang.bdc2sb +
  23847. '<input type="radio" name="bdc" value="tobdc" ' + (opt["tobdc"] ? "checked" : "" ) + '>' + lang.tobdc + '' +
  23848. '</td>' +
  23849. '<td nowrap align="right"><button >' + lang.run + '</button></td>' +
  23850. '</tr>' +
  23851. '</table>' +
  23852. '</div>' +
  23853. '</div>';
  23854. },
  23855. _UIBase_render:UIBase.prototype.render
  23856. };
  23857. utils.inherits(AutoTypeSetPicker, UIBase);
  23858. })();
  23859. // ui/autotypesetbutton.js
  23860. ///import core
  23861. ///import uicore
  23862. ///import ui/popup.js
  23863. ///import ui/autotypesetpicker.js
  23864. ///import ui/splitbutton.js
  23865. (function (){
  23866. var utils = baidu.editor.utils,
  23867. Popup = baidu.editor.ui.Popup,
  23868. AutoTypeSetPicker = baidu.editor.ui.AutoTypeSetPicker,
  23869. SplitButton = baidu.editor.ui.SplitButton,
  23870. AutoTypeSetButton = baidu.editor.ui.AutoTypeSetButton = function (options){
  23871. this.initOptions(options);
  23872. this.initAutoTypeSetButton();
  23873. };
  23874. function getPara(me){
  23875. var opt = {},
  23876. cont = me.getDom(),
  23877. editorId = me.editor.uid,
  23878. inputType = null,
  23879. attrName = null,
  23880. ipts = domUtils.getElementsByTagName(cont,"input");
  23881. for(var i=ipts.length-1,ipt;ipt=ipts[i--];){
  23882. inputType = ipt.getAttribute("type");
  23883. if(inputType=="checkbox"){
  23884. attrName = ipt.getAttribute("name");
  23885. opt[attrName] && delete opt[attrName];
  23886. if(ipt.checked){
  23887. var attrValue = document.getElementById( attrName + "Value" + editorId );
  23888. if(attrValue){
  23889. if(/input/ig.test(attrValue.tagName)){
  23890. opt[attrName] = attrValue.value;
  23891. } else {
  23892. var iptChilds = attrValue.getElementsByTagName("input");
  23893. for(var j=iptChilds.length-1,iptchild;iptchild=iptChilds[j--];){
  23894. if(iptchild.checked){
  23895. opt[attrName] = iptchild.value;
  23896. break;
  23897. }
  23898. }
  23899. }
  23900. } else {
  23901. opt[attrName] = true;
  23902. }
  23903. } else {
  23904. opt[attrName] = false;
  23905. }
  23906. } else {
  23907. opt[ipt.getAttribute("value")] = ipt.checked;
  23908. }
  23909. }
  23910. var selects = domUtils.getElementsByTagName(cont,"select");
  23911. for(var i=0,si;si=selects[i++];){
  23912. var attr = si.getAttribute('name');
  23913. opt[attr] = opt[attr] ? si.value : '';
  23914. }
  23915. utils.extend(me.editor.options.autotypeset,opt);
  23916. me.editor.setPreferences('autotypeset', opt);
  23917. }
  23918. AutoTypeSetButton.prototype = {
  23919. initAutoTypeSetButton: function (){
  23920. var me = this;
  23921. this.popup = new Popup({
  23922. //传入配置参数
  23923. content: new AutoTypeSetPicker({editor:me.editor}),
  23924. 'editor':me.editor,
  23925. hide : function(){
  23926. if (!this._hidden && this.getDom()) {
  23927. getPara(this);
  23928. this.getDom().style.display = 'none';
  23929. this._hidden = true;
  23930. this.fireEvent('hide');
  23931. }
  23932. }
  23933. });
  23934. var flag = 0;
  23935. this.popup.addListener('postRenderAfter',function(){
  23936. var popupUI = this;
  23937. if(flag)return;
  23938. var cont = this.getDom(),
  23939. btn = cont.getElementsByTagName('button')[0];
  23940. btn.onclick = function(){
  23941. getPara(popupUI);
  23942. me.editor.execCommand('autotypeset');
  23943. popupUI.hide()
  23944. };
  23945. domUtils.on(cont, 'click', function(e) {
  23946. var target = e.target || e.srcElement,
  23947. editorId = me.editor.uid;
  23948. if (target && target.tagName == 'INPUT') {
  23949. // 点击图片浮动的checkbox,去除对应的radio
  23950. if (target.name == 'imageBlockLine' || target.name == 'textAlign' || target.name == 'symbolConver') {
  23951. var checked = target.checked,
  23952. radioTd = document.getElementById( target.name + 'Value' + editorId),
  23953. radios = radioTd.getElementsByTagName('input'),
  23954. defalutSelect = {
  23955. 'imageBlockLine': 'none',
  23956. 'textAlign': 'left',
  23957. 'symbolConver': 'tobdc'
  23958. };
  23959. for (var i = 0; i < radios.length; i++) {
  23960. if (checked) {
  23961. if (radios[i].value == defalutSelect[target.name]) {
  23962. radios[i].checked = 'checked';
  23963. }
  23964. } else {
  23965. radios[i].checked = false;
  23966. }
  23967. }
  23968. }
  23969. // 点击radio,选中对应的checkbox
  23970. if (target.name == ('imageBlockLineValue' + editorId) || target.name == ('textAlignValue' + editorId) || target.name == 'bdc') {
  23971. var checkboxs = target.parentNode.previousSibling.getElementsByTagName('input');
  23972. checkboxs && (checkboxs[0].checked = true);
  23973. }
  23974. getPara(popupUI);
  23975. }
  23976. });
  23977. flag = 1;
  23978. });
  23979. this.initSplitButton();
  23980. }
  23981. };
  23982. utils.inherits(AutoTypeSetButton, SplitButton);
  23983. })();
  23984. // ui/cellalignpicker.js
  23985. ///import core
  23986. ///import uicore
  23987. (function () {
  23988. var utils = baidu.editor.utils,
  23989. Popup = baidu.editor.ui.Popup,
  23990. Stateful = baidu.editor.ui.Stateful,
  23991. UIBase = baidu.editor.ui.UIBase;
  23992. /**
  23993. * 该参数将新增一个参数: selected, 参数类型为一个Object, 形如{ 'align': 'center', 'valign': 'top' }, 表示单元格的初始
  23994. * 对齐状态为: 竖直居上,水平居中; 其中 align的取值为:'center', 'left', 'right'; valign的取值为: 'top', 'middle', 'bottom'
  23995. * @update 2013/4/2 hancong03@baidu.com
  23996. */
  23997. var CellAlignPicker = baidu.editor.ui.CellAlignPicker = function (options) {
  23998. this.initOptions(options);
  23999. this.initSelected();
  24000. this.initCellAlignPicker();
  24001. };
  24002. CellAlignPicker.prototype = {
  24003. //初始化选中状态, 该方法将根据传递进来的参数获取到应该选中的对齐方式图标的索引
  24004. initSelected: function(){
  24005. var status = {
  24006. valign: {
  24007. top: 0,
  24008. middle: 1,
  24009. bottom: 2
  24010. },
  24011. align: {
  24012. left: 0,
  24013. center: 1,
  24014. right: 2
  24015. },
  24016. count: 3
  24017. },
  24018. result = -1;
  24019. if( this.selected ) {
  24020. this.selectedIndex = status.valign[ this.selected.valign ] * status.count + status.align[ this.selected.align ];
  24021. }
  24022. },
  24023. initCellAlignPicker:function () {
  24024. this.initUIBase();
  24025. this.Stateful_init();
  24026. },
  24027. getHtmlTpl:function () {
  24028. var alignType = [ 'left', 'center', 'right' ],
  24029. COUNT = 9,
  24030. tempClassName = null,
  24031. tempIndex = -1,
  24032. tmpl = [];
  24033. for( var i= 0; i<COUNT; i++ ) {
  24034. tempClassName = this.selectedIndex === i ? ' class="edui-cellalign-selected" ' : '';
  24035. tempIndex = i % 3;
  24036. tempIndex === 0 && tmpl.push('<tr>');
  24037. tmpl.push( '<td index="'+ i +'" ' + tempClassName + ' stateful><div class="edui-icon edui-'+ alignType[ tempIndex ] +'"></div></td>' );
  24038. tempIndex === 2 && tmpl.push('</tr>');
  24039. }
  24040. return '<div id="##" class="edui-cellalignpicker %%">' +
  24041. '<div class="edui-cellalignpicker-body">' +
  24042. '<table onclick="$$._onClick(event);">' +
  24043. tmpl.join('') +
  24044. '</table>' +
  24045. '</div>' +
  24046. '</div>';
  24047. },
  24048. getStateDom: function (){
  24049. return this.target;
  24050. },
  24051. _onClick: function (evt){
  24052. var target= evt.target || evt.srcElement;
  24053. if(/icon/.test(target.className)){
  24054. this.items[target.parentNode.getAttribute("index")].onclick();
  24055. Popup.postHide(evt);
  24056. }
  24057. },
  24058. _UIBase_render:UIBase.prototype.render
  24059. };
  24060. utils.inherits(CellAlignPicker, UIBase);
  24061. utils.extend(CellAlignPicker.prototype, Stateful,true);
  24062. })();
  24063. // ui/pastepicker.js
  24064. ///import core
  24065. ///import uicore
  24066. (function () {
  24067. var utils = baidu.editor.utils,
  24068. Stateful = baidu.editor.ui.Stateful,
  24069. uiUtils = baidu.editor.ui.uiUtils,
  24070. UIBase = baidu.editor.ui.UIBase;
  24071. var PastePicker = baidu.editor.ui.PastePicker = function (options) {
  24072. this.initOptions(options);
  24073. this.initPastePicker();
  24074. };
  24075. PastePicker.prototype = {
  24076. initPastePicker:function () {
  24077. this.initUIBase();
  24078. this.Stateful_init();
  24079. },
  24080. getHtmlTpl:function () {
  24081. return '<div class="edui-pasteicon" onclick="$$._onClick(this)"></div>' +
  24082. '<div class="edui-pastecontainer">' +
  24083. '<div class="edui-title">' + this.editor.getLang("pasteOpt") + '</div>' +
  24084. '<div class="edui-button">' +
  24085. '<div title="' + this.editor.getLang("pasteSourceFormat") + '" onclick="$$.format(false)" stateful>' +
  24086. '<div class="edui-richtxticon"></div></div>' +
  24087. '<div title="' + this.editor.getLang("tagFormat") + '" onclick="$$.format(2)" stateful>' +
  24088. '<div class="edui-tagicon"></div></div>' +
  24089. '<div title="' + this.editor.getLang("pasteTextFormat") + '" onclick="$$.format(true)" stateful>' +
  24090. '<div class="edui-plaintxticon"></div></div>' +
  24091. '</div>' +
  24092. '</div>' +
  24093. '</div>'
  24094. },
  24095. getStateDom:function () {
  24096. return this.target;
  24097. },
  24098. format:function (param) {
  24099. this.editor.ui._isTransfer = true;
  24100. this.editor.fireEvent('pasteTransfer', param);
  24101. },
  24102. _onClick:function (cur) {
  24103. var node = domUtils.getNextDomNode(cur),
  24104. screenHt = uiUtils.getViewportRect().height,
  24105. subPop = uiUtils.getClientRect(node);
  24106. if ((subPop.top + subPop.height) > screenHt)
  24107. node.style.top = (-subPop.height - cur.offsetHeight) + "px";
  24108. else
  24109. node.style.top = "";
  24110. if (/hidden/ig.test(domUtils.getComputedStyle(node, "visibility"))) {
  24111. node.style.visibility = "visible";
  24112. domUtils.addClass(cur, "edui-state-opened");
  24113. } else {
  24114. node.style.visibility = "hidden";
  24115. domUtils.removeClasses(cur, "edui-state-opened")
  24116. }
  24117. },
  24118. _UIBase_render:UIBase.prototype.render
  24119. };
  24120. utils.inherits(PastePicker, UIBase);
  24121. utils.extend(PastePicker.prototype, Stateful, true);
  24122. })();
  24123. // ui/toolbar.js
  24124. (function (){
  24125. var utils = baidu.editor.utils,
  24126. uiUtils = baidu.editor.ui.uiUtils,
  24127. UIBase = baidu.editor.ui.UIBase,
  24128. Toolbar = baidu.editor.ui.Toolbar = function (options){
  24129. this.initOptions(options);
  24130. this.initToolbar();
  24131. };
  24132. Toolbar.prototype = {
  24133. items: null,
  24134. initToolbar: function (){
  24135. this.items = this.items || [];
  24136. this.initUIBase();
  24137. },
  24138. add: function (item,index){
  24139. if(index === undefined){
  24140. this.items.push(item);
  24141. }else{
  24142. this.items.splice(index,0,item)
  24143. }
  24144. },
  24145. getHtmlTpl: function (){
  24146. var buff = [];
  24147. for (var i=0; i<this.items.length; i++) {
  24148. buff[i] = this.items[i].renderHtml();
  24149. }
  24150. return '<div id="##" class="edui-toolbar %%" onselectstart="return false;" onmousedown="return $$._onMouseDown(event, this);">' +
  24151. buff.join('') +
  24152. '</div>'
  24153. },
  24154. postRender: function (){
  24155. var box = this.getDom();
  24156. for (var i=0; i<this.items.length; i++) {
  24157. this.items[i].postRender();
  24158. }
  24159. uiUtils.makeUnselectable(box);
  24160. },
  24161. _onMouseDown: function (e){
  24162. var target = e.target || e.srcElement,
  24163. tagName = target && target.tagName && target.tagName.toLowerCase();
  24164. if (tagName == 'input' || tagName == 'object' || tagName == 'object') {
  24165. return false;
  24166. }
  24167. }
  24168. };
  24169. utils.inherits(Toolbar, UIBase);
  24170. })();
  24171. // ui/menu.js
  24172. ///import core
  24173. ///import uicore
  24174. ///import ui\popup.js
  24175. ///import ui\stateful.js
  24176. (function () {
  24177. var utils = baidu.editor.utils,
  24178. domUtils = baidu.editor.dom.domUtils,
  24179. uiUtils = baidu.editor.ui.uiUtils,
  24180. UIBase = baidu.editor.ui.UIBase,
  24181. Popup = baidu.editor.ui.Popup,
  24182. Stateful = baidu.editor.ui.Stateful,
  24183. CellAlignPicker = baidu.editor.ui.CellAlignPicker,
  24184. Menu = baidu.editor.ui.Menu = function (options) {
  24185. this.initOptions(options);
  24186. this.initMenu();
  24187. };
  24188. var menuSeparator = {
  24189. renderHtml:function () {
  24190. return '<div class="edui-menuitem edui-menuseparator"><div class="edui-menuseparator-inner"></div></div>';
  24191. },
  24192. postRender:function () {
  24193. },
  24194. queryAutoHide:function () {
  24195. return true;
  24196. }
  24197. };
  24198. Menu.prototype = {
  24199. items:null,
  24200. uiName:'menu',
  24201. initMenu:function () {
  24202. this.items = this.items || [];
  24203. this.initPopup();
  24204. this.initItems();
  24205. },
  24206. initItems:function () {
  24207. for (var i = 0; i < this.items.length; i++) {
  24208. var item = this.items[i];
  24209. if (item == '-') {
  24210. this.items[i] = this.getSeparator();
  24211. } else if (!(item instanceof MenuItem)) {
  24212. item.editor = this.editor;
  24213. item.theme = this.editor.options.theme;
  24214. this.items[i] = this.createItem(item);
  24215. }
  24216. }
  24217. },
  24218. getSeparator:function () {
  24219. return menuSeparator;
  24220. },
  24221. createItem:function (item) {
  24222. //新增一个参数menu, 该参数存储了menuItem所对应的menu引用
  24223. item.menu = this;
  24224. return new MenuItem(item);
  24225. },
  24226. _Popup_getContentHtmlTpl:Popup.prototype.getContentHtmlTpl,
  24227. getContentHtmlTpl:function () {
  24228. if (this.items.length == 0) {
  24229. return this._Popup_getContentHtmlTpl();
  24230. }
  24231. var buff = [];
  24232. for (var i = 0; i < this.items.length; i++) {
  24233. var item = this.items[i];
  24234. buff[i] = item.renderHtml();
  24235. }
  24236. return ('<div class="%%-body">' + buff.join('') + '</div>');
  24237. },
  24238. _Popup_postRender:Popup.prototype.postRender,
  24239. postRender:function () {
  24240. var me = this;
  24241. for (var i = 0; i < this.items.length; i++) {
  24242. var item = this.items[i];
  24243. item.ownerMenu = this;
  24244. item.postRender();
  24245. }
  24246. domUtils.on(this.getDom(), 'mouseover', function (evt) {
  24247. evt = evt || event;
  24248. var rel = evt.relatedTarget || evt.fromElement;
  24249. var el = me.getDom();
  24250. if (!uiUtils.contains(el, rel) && el !== rel) {
  24251. me.fireEvent('over');
  24252. }
  24253. });
  24254. this._Popup_postRender();
  24255. },
  24256. queryAutoHide:function (el) {
  24257. if (el) {
  24258. if (uiUtils.contains(this.getDom(), el)) {
  24259. return false;
  24260. }
  24261. for (var i = 0; i < this.items.length; i++) {
  24262. var item = this.items[i];
  24263. if (item.queryAutoHide(el) === false) {
  24264. return false;
  24265. }
  24266. }
  24267. }
  24268. },
  24269. clearItems:function () {
  24270. for (var i = 0; i < this.items.length; i++) {
  24271. var item = this.items[i];
  24272. clearTimeout(item._showingTimer);
  24273. clearTimeout(item._closingTimer);
  24274. if (item.subMenu) {
  24275. item.subMenu.destroy();
  24276. }
  24277. }
  24278. this.items = [];
  24279. },
  24280. destroy:function () {
  24281. if (this.getDom()) {
  24282. domUtils.remove(this.getDom());
  24283. }
  24284. this.clearItems();
  24285. },
  24286. dispose:function () {
  24287. this.destroy();
  24288. }
  24289. };
  24290. utils.inherits(Menu, Popup);
  24291. /**
  24292. * @update 2013/04/03 hancong03 新增一个参数menu, 该参数存储了menuItem所对应的menu引用
  24293. * @type {Function}
  24294. */
  24295. var MenuItem = baidu.editor.ui.MenuItem = function (options) {
  24296. this.initOptions(options);
  24297. this.initUIBase();
  24298. this.Stateful_init();
  24299. if (this.subMenu && !(this.subMenu instanceof Menu)) {
  24300. if (options.className && options.className.indexOf("aligntd") != -1) {
  24301. var me = this;
  24302. //获取单元格对齐初始状态
  24303. this.subMenu.selected = this.editor.queryCommandValue( 'cellalignment' );
  24304. this.subMenu = new Popup({
  24305. content:new CellAlignPicker(this.subMenu),
  24306. parentMenu:me,
  24307. editor:me.editor,
  24308. destroy:function () {
  24309. if (this.getDom()) {
  24310. domUtils.remove(this.getDom());
  24311. }
  24312. }
  24313. });
  24314. this.subMenu.addListener("postRenderAfter", function () {
  24315. domUtils.on(this.getDom(), "mouseover", function () {
  24316. me.addState('opened');
  24317. });
  24318. });
  24319. } else {
  24320. this.subMenu = new Menu(this.subMenu);
  24321. }
  24322. }
  24323. };
  24324. MenuItem.prototype = {
  24325. label:'',
  24326. subMenu:null,
  24327. ownerMenu:null,
  24328. uiName:'menuitem',
  24329. alwalysHoverable:true,
  24330. getHtmlTpl:function () {
  24331. return '<div id="##" class="%%" stateful onclick="$$._onClick(event, this);">' +
  24332. '<div class="%%-body">' +
  24333. this.renderLabelHtml() +
  24334. '</div>' +
  24335. '</div>';
  24336. },
  24337. postRender:function () {
  24338. var me = this;
  24339. this.addListener('over', function () {
  24340. me.ownerMenu.fireEvent('submenuover', me);
  24341. if (me.subMenu) {
  24342. me.delayShowSubMenu();
  24343. }
  24344. });
  24345. if (this.subMenu) {
  24346. this.getDom().className += ' edui-hassubmenu';
  24347. this.subMenu.render();
  24348. this.addListener('out', function () {
  24349. me.delayHideSubMenu();
  24350. });
  24351. this.subMenu.addListener('over', function () {
  24352. clearTimeout(me._closingTimer);
  24353. me._closingTimer = null;
  24354. me.addState('opened');
  24355. });
  24356. this.ownerMenu.addListener('hide', function () {
  24357. me.hideSubMenu();
  24358. });
  24359. this.ownerMenu.addListener('submenuover', function (t, subMenu) {
  24360. if (subMenu !== me) {
  24361. me.delayHideSubMenu();
  24362. }
  24363. });
  24364. this.subMenu._bakQueryAutoHide = this.subMenu.queryAutoHide;
  24365. this.subMenu.queryAutoHide = function (el) {
  24366. if (el && uiUtils.contains(me.getDom(), el)) {
  24367. return false;
  24368. }
  24369. return this._bakQueryAutoHide(el);
  24370. };
  24371. }
  24372. this.getDom().style.tabIndex = '-1';
  24373. uiUtils.makeUnselectable(this.getDom());
  24374. this.Stateful_postRender();
  24375. },
  24376. delayShowSubMenu:function () {
  24377. var me = this;
  24378. if (!me.isDisabled()) {
  24379. me.addState('opened');
  24380. clearTimeout(me._showingTimer);
  24381. clearTimeout(me._closingTimer);
  24382. me._closingTimer = null;
  24383. me._showingTimer = setTimeout(function () {
  24384. me.showSubMenu();
  24385. }, 250);
  24386. }
  24387. },
  24388. delayHideSubMenu:function () {
  24389. var me = this;
  24390. if (!me.isDisabled()) {
  24391. me.removeState('opened');
  24392. clearTimeout(me._showingTimer);
  24393. if (!me._closingTimer) {
  24394. me._closingTimer = setTimeout(function () {
  24395. if (!me.hasState('opened')) {
  24396. me.hideSubMenu();
  24397. }
  24398. me._closingTimer = null;
  24399. }, 400);
  24400. }
  24401. }
  24402. },
  24403. renderLabelHtml:function () {
  24404. return '<div class="edui-arrow"></div>' +
  24405. '<div class="edui-box edui-icon"></div>' +
  24406. '<div class="edui-box edui-label %%-label">' + (this.label || '') + '</div>';
  24407. },
  24408. getStateDom:function () {
  24409. return this.getDom();
  24410. },
  24411. queryAutoHide:function (el) {
  24412. if (this.subMenu && this.hasState('opened')) {
  24413. return this.subMenu.queryAutoHide(el);
  24414. }
  24415. },
  24416. _onClick:function (event, this_) {
  24417. if (this.hasState('disabled')) return;
  24418. if (this.fireEvent('click', event, this_) !== false) {
  24419. if (this.subMenu) {
  24420. this.showSubMenu();
  24421. } else {
  24422. Popup.postHide(event);
  24423. }
  24424. }
  24425. },
  24426. showSubMenu:function () {
  24427. var rect = uiUtils.getClientRect(this.getDom());
  24428. rect.right -= 5;
  24429. rect.left += 2;
  24430. rect.width -= 7;
  24431. rect.top -= 4;
  24432. rect.bottom += 4;
  24433. rect.height += 8;
  24434. this.subMenu.showAnchorRect(rect, true, true);
  24435. },
  24436. hideSubMenu:function () {
  24437. this.subMenu.hide();
  24438. }
  24439. };
  24440. utils.inherits(MenuItem, UIBase);
  24441. utils.extend(MenuItem.prototype, Stateful, true);
  24442. })();
  24443. // ui/combox.js
  24444. ///import core
  24445. ///import uicore
  24446. ///import ui/menu.js
  24447. ///import ui/splitbutton.js
  24448. (function (){
  24449. // todo: menu和item提成通用list
  24450. var utils = baidu.editor.utils,
  24451. uiUtils = baidu.editor.ui.uiUtils,
  24452. Menu = baidu.editor.ui.Menu,
  24453. SplitButton = baidu.editor.ui.SplitButton,
  24454. Combox = baidu.editor.ui.Combox = function (options){
  24455. this.initOptions(options);
  24456. this.initCombox();
  24457. };
  24458. Combox.prototype = {
  24459. uiName: 'combox',
  24460. onbuttonclick:function () {
  24461. this.showPopup();
  24462. },
  24463. initCombox: function (){
  24464. var me = this;
  24465. this.items = this.items || [];
  24466. for (var i=0; i<this.items.length; i++) {
  24467. var item = this.items[i];
  24468. item.uiName = 'listitem';
  24469. item.index = i;
  24470. item.onclick = function (){
  24471. me.selectByIndex(this.index);
  24472. };
  24473. }
  24474. this.popup = new Menu({
  24475. items: this.items,
  24476. uiName: 'list',
  24477. editor:this.editor,
  24478. captureWheel: true,
  24479. combox: this
  24480. });
  24481. this.initSplitButton();
  24482. },
  24483. _SplitButton_postRender: SplitButton.prototype.postRender,
  24484. postRender: function (){
  24485. this._SplitButton_postRender();
  24486. this.setLabel(this.label || '');
  24487. this.setValue(this.initValue || '');
  24488. },
  24489. showPopup: function (){
  24490. var rect = uiUtils.getClientRect(this.getDom());
  24491. rect.top += 1;
  24492. rect.bottom -= 1;
  24493. rect.height -= 2;
  24494. this.popup.showAnchorRect(rect);
  24495. },
  24496. getValue: function (){
  24497. return this.value;
  24498. },
  24499. setValue: function (value){
  24500. var index = this.indexByValue(value);
  24501. if (index != -1) {
  24502. this.selectedIndex = index;
  24503. this.setLabel(this.items[index].label);
  24504. this.value = this.items[index].value;
  24505. } else {
  24506. this.selectedIndex = -1;
  24507. this.setLabel(this.getLabelForUnknowValue(value));
  24508. this.value = value;
  24509. }
  24510. },
  24511. setLabel: function (label){
  24512. this.getDom('button_body').innerHTML = label;
  24513. this.label = label;
  24514. },
  24515. getLabelForUnknowValue: function (value){
  24516. return value;
  24517. },
  24518. indexByValue: function (value){
  24519. for (var i=0; i<this.items.length; i++) {
  24520. if (value == this.items[i].value) {
  24521. return i;
  24522. }
  24523. }
  24524. return -1;
  24525. },
  24526. getItem: function (index){
  24527. return this.items[index];
  24528. },
  24529. selectByIndex: function (index){
  24530. if (index < this.items.length && this.fireEvent('select', index) !== false) {
  24531. this.selectedIndex = index;
  24532. this.value = this.items[index].value;
  24533. this.setLabel(this.items[index].label);
  24534. }
  24535. }
  24536. };
  24537. utils.inherits(Combox, SplitButton);
  24538. })();
  24539. // ui/dialog.js
  24540. ///import core
  24541. ///import uicore
  24542. ///import ui/mask.js
  24543. ///import ui/button.js
  24544. (function (){
  24545. var utils = baidu.editor.utils,
  24546. domUtils = baidu.editor.dom.domUtils,
  24547. uiUtils = baidu.editor.ui.uiUtils,
  24548. Mask = baidu.editor.ui.Mask,
  24549. UIBase = baidu.editor.ui.UIBase,
  24550. Button = baidu.editor.ui.Button,
  24551. Dialog = baidu.editor.ui.Dialog = function (options){
  24552. if(options.name){
  24553. var name = options.name;
  24554. var cssRules = options.cssRules;
  24555. if(!options.className){
  24556. options.className = 'edui-for-' + name;
  24557. }
  24558. if(cssRules){
  24559. options.cssRules = '.edui-default .edui-for-'+ name +' .edui-dialog-content {'+ cssRules +'}'
  24560. }
  24561. }
  24562. this.initOptions(utils.extend({
  24563. autoReset: true,
  24564. draggable: true,
  24565. onok: function (){},
  24566. oncancel: function (){},
  24567. onclose: function (t, ok){
  24568. return ok ? this.onok() : this.oncancel();
  24569. },
  24570. //是否控制dialog中的scroll事件, 默认为不阻止
  24571. holdScroll: false
  24572. },options));
  24573. this.initDialog();
  24574. };
  24575. var modalMask;
  24576. var dragMask;
  24577. var activeDialog;
  24578. Dialog.prototype = {
  24579. draggable: false,
  24580. uiName: 'dialog',
  24581. initDialog: function (){
  24582. var me = this,
  24583. theme=this.editor.options.theme;
  24584. if(this.cssRules){
  24585. utils.cssRule('edui-customize-'+this.name+'-style',this.cssRules);
  24586. }
  24587. this.initUIBase();
  24588. this.modalMask = (modalMask || (modalMask = new Mask({
  24589. className: 'edui-dialog-modalmask',
  24590. theme:theme,
  24591. onclick: function (){
  24592. activeDialog && activeDialog.close(false);
  24593. }
  24594. })));
  24595. this.dragMask = (dragMask || (dragMask = new Mask({
  24596. className: 'edui-dialog-dragmask',
  24597. theme:theme
  24598. })));
  24599. this.closeButton = new Button({
  24600. className: 'edui-dialog-closebutton',
  24601. title: me.closeDialog,
  24602. theme:theme,
  24603. onclick: function (){
  24604. me.close(false);
  24605. }
  24606. });
  24607. this.fullscreen && this.initResizeEvent();
  24608. if (this.buttons) {
  24609. for (var i=0; i<this.buttons.length; i++) {
  24610. if (!(this.buttons[i] instanceof Button)) {
  24611. this.buttons[i] = new Button(utils.extend(this.buttons[i],{
  24612. editor : this.editor
  24613. },true));
  24614. }
  24615. }
  24616. }
  24617. },
  24618. initResizeEvent: function () {
  24619. var me = this;
  24620. domUtils.on( window, "resize", function () {
  24621. if ( me._hidden || me._hidden === undefined ) {
  24622. return;
  24623. }
  24624. if ( me.__resizeTimer ) {
  24625. window.clearTimeout( me.__resizeTimer );
  24626. }
  24627. me.__resizeTimer = window.setTimeout( function () {
  24628. me.__resizeTimer = null;
  24629. var dialogWrapNode = me.getDom(),
  24630. contentNode = me.getDom('content'),
  24631. wrapRect = UE.ui.uiUtils.getClientRect( dialogWrapNode ),
  24632. contentRect = UE.ui.uiUtils.getClientRect( contentNode ),
  24633. vpRect = uiUtils.getViewportRect();
  24634. contentNode.style.width = ( vpRect.width - wrapRect.width + contentRect.width ) + "px";
  24635. contentNode.style.height = ( vpRect.height - wrapRect.height + contentRect.height ) + "px";
  24636. dialogWrapNode.style.width = vpRect.width + "px";
  24637. dialogWrapNode.style.height = vpRect.height + "px";
  24638. me.fireEvent( "resize" );
  24639. }, 100 );
  24640. } );
  24641. },
  24642. fitSize: function (){
  24643. var popBodyEl = this.getDom('body');
  24644. // if (!(baidu.editor.browser.ie && baidu.editor.browser.version == 7)) {
  24645. // uiUtils.removeStyle(popBodyEl, 'width');
  24646. // uiUtils.removeStyle(popBodyEl, 'height');
  24647. // }
  24648. var size = this.mesureSize();
  24649. popBodyEl.style.width = size.width + 'px';
  24650. popBodyEl.style.height = size.height + 'px';
  24651. return size;
  24652. },
  24653. safeSetOffset: function (offset){
  24654. var me = this;
  24655. var el = me.getDom();
  24656. var vpRect = uiUtils.getViewportRect();
  24657. var rect = uiUtils.getClientRect(el);
  24658. var left = offset.left;
  24659. if (left + rect.width > vpRect.right) {
  24660. left = vpRect.right - rect.width;
  24661. }
  24662. var top = offset.top;
  24663. if (top + rect.height > vpRect.bottom) {
  24664. top = vpRect.bottom - rect.height;
  24665. }
  24666. el.style.left = Math.max(left, 0) + 'px';
  24667. el.style.top = Math.max(top, 0) + 'px';
  24668. },
  24669. showAtCenter: function (){
  24670. var vpRect = uiUtils.getViewportRect();
  24671. if ( !this.fullscreen ) {
  24672. this.getDom().style.display = '';
  24673. var popSize = this.fitSize();
  24674. var titleHeight = this.getDom('titlebar').offsetHeight | 0;
  24675. var left = vpRect.width / 2 - popSize.width / 2;
  24676. var top = vpRect.height / 2 - (popSize.height - titleHeight) / 2 - titleHeight;
  24677. var popEl = this.getDom();
  24678. this.safeSetOffset({
  24679. left: Math.max(left | 0, 0),
  24680. top: Math.max(top | 0, 0)
  24681. });
  24682. if (!domUtils.hasClass(popEl, 'edui-state-centered')) {
  24683. popEl.className += ' edui-state-centered';
  24684. }
  24685. } else {
  24686. var dialogWrapNode = this.getDom(),
  24687. contentNode = this.getDom('content');
  24688. dialogWrapNode.style.display = "block";
  24689. var wrapRect = UE.ui.uiUtils.getClientRect( dialogWrapNode ),
  24690. contentRect = UE.ui.uiUtils.getClientRect( contentNode );
  24691. dialogWrapNode.style.left = "-100000px";
  24692. contentNode.style.width = ( vpRect.width - wrapRect.width + contentRect.width ) + "px";
  24693. contentNode.style.height = ( vpRect.height - wrapRect.height + contentRect.height ) + "px";
  24694. dialogWrapNode.style.width = vpRect.width + "px";
  24695. dialogWrapNode.style.height = vpRect.height + "px";
  24696. dialogWrapNode.style.left = 0;
  24697. //保存环境的overflow值
  24698. this._originalContext = {
  24699. html: {
  24700. overflowX: document.documentElement.style.overflowX,
  24701. overflowY: document.documentElement.style.overflowY
  24702. },
  24703. body: {
  24704. overflowX: document.body.style.overflowX,
  24705. overflowY: document.body.style.overflowY
  24706. }
  24707. };
  24708. document.documentElement.style.overflowX = 'hidden';
  24709. document.documentElement.style.overflowY = 'hidden';
  24710. document.body.style.overflowX = 'hidden';
  24711. document.body.style.overflowY = 'hidden';
  24712. }
  24713. this._show();
  24714. },
  24715. getContentHtml: function (){
  24716. var contentHtml = '';
  24717. if (typeof this.content == 'string') {
  24718. contentHtml = this.content;
  24719. } else if (this.iframeUrl) {
  24720. contentHtml = '<span id="'+ this.id +'_contmask" class="dialogcontmask"></span><iframe id="'+ this.id +
  24721. '_iframe" class="%%-iframe" height="100%" width="100%" frameborder="0" src="'+ this.iframeUrl +'"></iframe>';
  24722. }
  24723. return contentHtml;
  24724. },
  24725. getHtmlTpl: function (){
  24726. var footHtml = '';
  24727. if (this.buttons) {
  24728. var buff = [];
  24729. for (var i=0; i<this.buttons.length; i++) {
  24730. buff[i] = this.buttons[i].renderHtml();
  24731. }
  24732. footHtml = '<div class="%%-foot">' +
  24733. '<div id="##_buttons" class="%%-buttons">' + buff.join('') + '</div>' +
  24734. '</div>';
  24735. }
  24736. return '<div id="##" class="%%"><div '+ ( !this.fullscreen ? 'class="%%"' : 'class="%%-wrap edui-dialog-fullscreen-flag"' ) +'><div id="##_body" class="%%-body">' +
  24737. '<div class="%%-shadow"></div>' +
  24738. '<div id="##_titlebar" class="%%-titlebar">' +
  24739. '<div class="%%-draghandle" onmousedown="$$._onTitlebarMouseDown(event, this);">' +
  24740. '<span class="%%-caption">' + (this.title || '') + '</span>' +
  24741. '</div>' +
  24742. this.closeButton.renderHtml() +
  24743. '</div>' +
  24744. '<div id="##_content" class="%%-content">'+ ( this.autoReset ? '' : this.getContentHtml()) +'</div>' +
  24745. footHtml +
  24746. '</div></div></div>';
  24747. },
  24748. postRender: function (){
  24749. // todo: 保持居中/记住上次关闭位置选项
  24750. if (!this.modalMask.getDom()) {
  24751. this.modalMask.render();
  24752. this.modalMask.hide();
  24753. }
  24754. if (!this.dragMask.getDom()) {
  24755. this.dragMask.render();
  24756. this.dragMask.hide();
  24757. }
  24758. var me = this;
  24759. this.addListener('show', function (){
  24760. me.modalMask.show(this.getDom().style.zIndex - 2);
  24761. });
  24762. this.addListener('hide', function (){
  24763. me.modalMask.hide();
  24764. });
  24765. if (this.buttons) {
  24766. for (var i=0; i<this.buttons.length; i++) {
  24767. this.buttons[i].postRender();
  24768. }
  24769. }
  24770. domUtils.on(window, 'resize', function (){
  24771. setTimeout(function (){
  24772. if (!me.isHidden()) {
  24773. me.safeSetOffset(uiUtils.getClientRect(me.getDom()));
  24774. }
  24775. });
  24776. });
  24777. //hold住scroll事件,防止dialog的滚动影响页面
  24778. // if( this.holdScroll ) {
  24779. //
  24780. // if( !me.iframeUrl ) {
  24781. // domUtils.on( document.getElementById( me.id + "_iframe"), !browser.gecko ? "mousewheel" : "DOMMouseScroll", function(e){
  24782. // domUtils.preventDefault(e);
  24783. // } );
  24784. // } else {
  24785. // me.addListener('dialogafterreset', function(){
  24786. // window.setTimeout(function(){
  24787. // var iframeWindow = document.getElementById( me.id + "_iframe").contentWindow;
  24788. //
  24789. // if( browser.ie ) {
  24790. //
  24791. // var timer = window.setInterval(function(){
  24792. //
  24793. // if( iframeWindow.document && iframeWindow.document.body ) {
  24794. // window.clearInterval( timer );
  24795. // timer = null;
  24796. // domUtils.on( iframeWindow.document.body, !browser.gecko ? "mousewheel" : "DOMMouseScroll", function(e){
  24797. // domUtils.preventDefault(e);
  24798. // } );
  24799. // }
  24800. //
  24801. // }, 100);
  24802. //
  24803. // } else {
  24804. // domUtils.on( iframeWindow, !browser.gecko ? "mousewheel" : "DOMMouseScroll", function(e){
  24805. // domUtils.preventDefault(e);
  24806. // } );
  24807. // }
  24808. //
  24809. // }, 1);
  24810. // });
  24811. // }
  24812. //
  24813. // }
  24814. this._hide();
  24815. },
  24816. mesureSize: function (){
  24817. var body = this.getDom('body');
  24818. var width = uiUtils.getClientRect(this.getDom('content')).width;
  24819. var dialogBodyStyle = body.style;
  24820. dialogBodyStyle.width = width;
  24821. return uiUtils.getClientRect(body);
  24822. },
  24823. _onTitlebarMouseDown: function (evt, el){
  24824. if (this.draggable) {
  24825. var rect;
  24826. var vpRect = uiUtils.getViewportRect();
  24827. var me = this;
  24828. uiUtils.startDrag(evt, {
  24829. ondragstart: function (){
  24830. rect = uiUtils.getClientRect(me.getDom());
  24831. me.getDom('contmask').style.visibility = 'visible';
  24832. me.dragMask.show(me.getDom().style.zIndex - 1);
  24833. },
  24834. ondragmove: function (x, y){
  24835. var left = rect.left + x;
  24836. var top = rect.top + y;
  24837. me.safeSetOffset({
  24838. left: left,
  24839. top: top
  24840. });
  24841. },
  24842. ondragstop: function (){
  24843. me.getDom('contmask').style.visibility = 'hidden';
  24844. domUtils.removeClasses(me.getDom(), ['edui-state-centered']);
  24845. me.dragMask.hide();
  24846. }
  24847. });
  24848. }
  24849. },
  24850. reset: function (){
  24851. this.getDom('content').innerHTML = this.getContentHtml();
  24852. this.fireEvent('dialogafterreset');
  24853. },
  24854. _show: function (){
  24855. if (this._hidden) {
  24856. this.getDom().style.display = '';
  24857. //要高过编辑器的zindxe
  24858. this.editor.container.style.zIndex && (this.getDom().style.zIndex = this.editor.container.style.zIndex * 1 + 10);
  24859. this._hidden = false;
  24860. this.fireEvent('show');
  24861. baidu.editor.ui.uiUtils.getFixedLayer().style.zIndex = this.getDom().style.zIndex - 4;
  24862. }
  24863. },
  24864. isHidden: function (){
  24865. return this._hidden;
  24866. },
  24867. _hide: function (){
  24868. if (!this._hidden) {
  24869. var wrapNode = this.getDom();
  24870. wrapNode.style.display = 'none';
  24871. wrapNode.style.zIndex = '';
  24872. wrapNode.style.width = '';
  24873. wrapNode.style.height = '';
  24874. this._hidden = true;
  24875. this.fireEvent('hide');
  24876. }
  24877. },
  24878. open: function (){
  24879. if (this.autoReset) {
  24880. //有可能还没有渲染
  24881. try{
  24882. this.reset();
  24883. }catch(e){
  24884. this.render();
  24885. this.open()
  24886. }
  24887. }
  24888. this.showAtCenter();
  24889. if (this.iframeUrl) {
  24890. try {
  24891. this.getDom('iframe').focus();
  24892. } catch(ex){}
  24893. }
  24894. activeDialog = this;
  24895. },
  24896. _onCloseButtonClick: function (evt, el){
  24897. this.close(false);
  24898. },
  24899. close: function (ok){
  24900. if (this.fireEvent('close', ok) !== false) {
  24901. //还原环境
  24902. if ( this.fullscreen ) {
  24903. document.documentElement.style.overflowX = this._originalContext.html.overflowX;
  24904. document.documentElement.style.overflowY = this._originalContext.html.overflowY;
  24905. document.body.style.overflowX = this._originalContext.body.overflowX;
  24906. document.body.style.overflowY = this._originalContext.body.overflowY;
  24907. delete this._originalContext;
  24908. }
  24909. this._hide();
  24910. //销毁content
  24911. var content = this.getDom('content');
  24912. var iframe = this.getDom('iframe');
  24913. if (content && iframe) {
  24914. var doc = iframe.contentDocument || iframe.contentWindow.document;
  24915. doc && (doc.body.innerHTML = '');
  24916. domUtils.remove(content);
  24917. }
  24918. }
  24919. }
  24920. };
  24921. utils.inherits(Dialog, UIBase);
  24922. })();
  24923. // ui/menubutton.js
  24924. ///import core
  24925. ///import uicore
  24926. ///import ui/menu.js
  24927. ///import ui/splitbutton.js
  24928. (function (){
  24929. var utils = baidu.editor.utils,
  24930. Menu = baidu.editor.ui.Menu,
  24931. SplitButton = baidu.editor.ui.SplitButton,
  24932. MenuButton = baidu.editor.ui.MenuButton = function (options){
  24933. this.initOptions(options);
  24934. this.initMenuButton();
  24935. };
  24936. MenuButton.prototype = {
  24937. initMenuButton: function (){
  24938. var me = this;
  24939. this.uiName = "menubutton";
  24940. this.popup = new Menu({
  24941. items: me.items,
  24942. className: me.className,
  24943. editor:me.editor
  24944. });
  24945. this.popup.addListener('show', function (){
  24946. var list = this;
  24947. for (var i=0; i<list.items.length; i++) {
  24948. list.items[i].removeState('checked');
  24949. if (list.items[i].value == me._value) {
  24950. list.items[i].addState('checked');
  24951. this.value = me._value;
  24952. }
  24953. }
  24954. });
  24955. this.initSplitButton();
  24956. },
  24957. setValue : function(value){
  24958. this._value = value;
  24959. }
  24960. };
  24961. utils.inherits(MenuButton, SplitButton);
  24962. })();
  24963. // ui/multiMenu.js
  24964. ///import core
  24965. ///import uicore
  24966. ///commands 表情
  24967. (function(){
  24968. var utils = baidu.editor.utils,
  24969. Popup = baidu.editor.ui.Popup,
  24970. SplitButton = baidu.editor.ui.SplitButton,
  24971. MultiMenuPop = baidu.editor.ui.MultiMenuPop = function(options){
  24972. this.initOptions(options);
  24973. this.initMultiMenu();
  24974. };
  24975. MultiMenuPop.prototype = {
  24976. initMultiMenu: function (){
  24977. var me = this;
  24978. this.popup = new Popup({
  24979. content: '',
  24980. editor : me.editor,
  24981. iframe_rendered: false,
  24982. onshow: function (){
  24983. if (!this.iframe_rendered) {
  24984. this.iframe_rendered = true;
  24985. this.getDom('content').innerHTML = '<iframe id="'+me.id+'_iframe" src="'+ me.iframeUrl +'" frameborder="0"></iframe>';
  24986. me.editor.container.style.zIndex && (this.getDom().style.zIndex = me.editor.container.style.zIndex * 1 + 1);
  24987. }
  24988. }
  24989. // canSideUp:false,
  24990. // canSideLeft:false
  24991. });
  24992. this.onbuttonclick = function(){
  24993. this.showPopup();
  24994. };
  24995. this.initSplitButton();
  24996. }
  24997. };
  24998. utils.inherits(MultiMenuPop, SplitButton);
  24999. })();
  25000. // ui/shortcutmenu.js
  25001. (function () {
  25002. var UI = baidu.editor.ui,
  25003. UIBase = UI.UIBase,
  25004. uiUtils = UI.uiUtils,
  25005. utils = baidu.editor.utils,
  25006. domUtils = baidu.editor.dom.domUtils;
  25007. var allMenus = [],//存储所有快捷菜单
  25008. timeID,
  25009. isSubMenuShow = false;//是否有子pop显示
  25010. var ShortCutMenu = UI.ShortCutMenu = function (options) {
  25011. this.initOptions (options);
  25012. this.initShortCutMenu ();
  25013. };
  25014. ShortCutMenu.postHide = hideAllMenu;
  25015. ShortCutMenu.prototype = {
  25016. isHidden : true ,
  25017. SPACE : 5 ,
  25018. initShortCutMenu : function () {
  25019. this.items = this.items || [];
  25020. this.initUIBase ();
  25021. this.initItems ();
  25022. this.initEvent ();
  25023. allMenus.push (this);
  25024. } ,
  25025. initEvent : function () {
  25026. var me = this,
  25027. doc = me.editor.document;
  25028. domUtils.on (doc , "mousemove" , function (e) {
  25029. if (me.isHidden === false) {
  25030. //有pop显示就不隐藏快捷菜单
  25031. if (me.getSubMenuMark () || me.eventType == "contextmenu") return;
  25032. var flag = true,
  25033. el = me.getDom (),
  25034. wt = el.offsetWidth,
  25035. ht = el.offsetHeight,
  25036. distanceX = wt / 2 + me.SPACE,//距离中心X标准
  25037. distanceY = ht / 2,//距离中心Y标准
  25038. x = Math.abs (e.screenX - me.left),//离中心距离横坐标
  25039. y = Math.abs (e.screenY - me.top);//离中心距离纵坐标
  25040. clearTimeout (timeID);
  25041. timeID = setTimeout (function () {
  25042. if (y > 0 && y < distanceY) {
  25043. me.setOpacity (el , "1");
  25044. } else if (y > distanceY && y < distanceY + 70) {
  25045. me.setOpacity (el , "0.5");
  25046. flag = false;
  25047. } else if (y > distanceY + 70 && y < distanceY + 140) {
  25048. me.hide ();
  25049. }
  25050. if (flag && x > 0 && x < distanceX) {
  25051. me.setOpacity (el , "1")
  25052. } else if (x > distanceX && x < distanceX + 70) {
  25053. me.setOpacity (el , "0.5")
  25054. } else if (x > distanceX + 70 && x < distanceX + 140) {
  25055. me.hide ();
  25056. }
  25057. });
  25058. }
  25059. });
  25060. //ie\ff下 mouseout不准
  25061. if (browser.chrome) {
  25062. domUtils.on (doc , "mouseout" , function (e) {
  25063. var relatedTgt = e.relatedTarget || e.toElement;
  25064. if (relatedTgt == null || relatedTgt.tagName == "HTML") {
  25065. me.hide ();
  25066. }
  25067. });
  25068. }
  25069. me.editor.addListener ("afterhidepop" , function () {
  25070. if (!me.isHidden) {
  25071. isSubMenuShow = true;
  25072. }
  25073. });
  25074. } ,
  25075. initItems : function () {
  25076. if (utils.isArray (this.items)) {
  25077. for (var i = 0, len = this.items.length ; i < len ; i++) {
  25078. var item = this.items[i].toLowerCase ();
  25079. if (UI[item]) {
  25080. this.items[i] = new UI[item] (this.editor);
  25081. this.items[i].className += " edui-shortcutsubmenu ";
  25082. }
  25083. }
  25084. }
  25085. } ,
  25086. setOpacity : function (el , value) {
  25087. if (browser.ie && browser.version < 9) {
  25088. el.style.filter = "alpha(opacity = " + parseFloat (value) * 100 + ");"
  25089. } else {
  25090. el.style.opacity = value;
  25091. }
  25092. } ,
  25093. getSubMenuMark : function () {
  25094. isSubMenuShow = false;
  25095. var layerEle = uiUtils.getFixedLayer ();
  25096. var list = domUtils.getElementsByTagName (layerEle , "div" , function (node) {
  25097. return domUtils.hasClass (node , "edui-shortcutsubmenu edui-popup")
  25098. });
  25099. for (var i = 0, node ; node = list[i++] ;) {
  25100. if (node.style.display != "none") {
  25101. isSubMenuShow = true;
  25102. }
  25103. }
  25104. return isSubMenuShow;
  25105. } ,
  25106. show : function (e , hasContextmenu) {
  25107. var me = this,
  25108. offset = {},
  25109. el = this.getDom (),
  25110. fixedlayer = uiUtils.getFixedLayer ();
  25111. function setPos (offset) {
  25112. if (offset.left < 0) {
  25113. offset.left = 0;
  25114. }
  25115. if (offset.top < 0) {
  25116. offset.top = 0;
  25117. }
  25118. el.style.cssText = "position:absolute;left:" + offset.left + "px;top:" + offset.top + "px;";
  25119. }
  25120. function setPosByCxtMenu (menu) {
  25121. if (!menu.tagName) {
  25122. menu = menu.getDom ();
  25123. }
  25124. offset.left = parseInt (menu.style.left);
  25125. offset.top = parseInt (menu.style.top);
  25126. offset.top -= el.offsetHeight + 15;
  25127. setPos (offset);
  25128. }
  25129. me.eventType = e.type;
  25130. el.style.cssText = "display:block;left:-9999px";
  25131. if (e.type == "contextmenu" && hasContextmenu) {
  25132. var menu = domUtils.getElementsByTagName (fixedlayer , "div" , "edui-contextmenu")[0];
  25133. if (menu) {
  25134. setPosByCxtMenu (menu)
  25135. } else {
  25136. me.editor.addListener ("aftershowcontextmenu" , function (type , menu) {
  25137. setPosByCxtMenu (menu);
  25138. });
  25139. }
  25140. } else {
  25141. offset = uiUtils.getViewportOffsetByEvent (e);
  25142. offset.top -= el.offsetHeight + me.SPACE;
  25143. offset.left += me.SPACE + 20;
  25144. setPos (offset);
  25145. me.setOpacity (el , 0.2);
  25146. }
  25147. me.isHidden = false;
  25148. me.left = e.screenX + el.offsetWidth / 2 - me.SPACE;
  25149. me.top = e.screenY - (el.offsetHeight / 2) - me.SPACE;
  25150. if (me.editor) {
  25151. el.style.zIndex = me.editor.container.style.zIndex * 1 + 10;
  25152. fixedlayer.style.zIndex = el.style.zIndex - 1;
  25153. }
  25154. } ,
  25155. hide : function () {
  25156. if (this.getDom ()) {
  25157. this.getDom ().style.display = "none";
  25158. }
  25159. this.isHidden = true;
  25160. } ,
  25161. postRender : function () {
  25162. if (utils.isArray (this.items)) {
  25163. for (var i = 0, item ; item = this.items[i++] ;) {
  25164. item.postRender ();
  25165. }
  25166. }
  25167. } ,
  25168. getHtmlTpl : function () {
  25169. var buff;
  25170. if (utils.isArray (this.items)) {
  25171. buff = [];
  25172. for (var i = 0 ; i < this.items.length ; i++) {
  25173. buff[i] = this.items[i].renderHtml ();
  25174. }
  25175. buff = buff.join ("");
  25176. } else {
  25177. buff = this.items;
  25178. }
  25179. return '<div id="##" class="%% edui-toolbar" data-src="shortcutmenu" onmousedown="return false;" onselectstart="return false;" >' +
  25180. buff +
  25181. '</div>';
  25182. }
  25183. };
  25184. utils.inherits (ShortCutMenu , UIBase);
  25185. function hideAllMenu (e) {
  25186. var tgt = e.target || e.srcElement,
  25187. cur = domUtils.findParent (tgt , function (node) {
  25188. return domUtils.hasClass (node , "edui-shortcutmenu") || domUtils.hasClass (node , "edui-popup");
  25189. } , true);
  25190. if (!cur) {
  25191. for (var i = 0, menu ; menu = allMenus[i++] ;) {
  25192. menu.hide ()
  25193. }
  25194. }
  25195. }
  25196. domUtils.on (document , 'mousedown' , function (e) {
  25197. hideAllMenu (e);
  25198. });
  25199. domUtils.on (window , 'scroll' , function (e) {
  25200. hideAllMenu (e);
  25201. });
  25202. }) ();
  25203. // ui/breakline.js
  25204. (function (){
  25205. var utils = baidu.editor.utils,
  25206. UIBase = baidu.editor.ui.UIBase,
  25207. Breakline = baidu.editor.ui.Breakline = function (options){
  25208. this.initOptions(options);
  25209. this.initSeparator();
  25210. };
  25211. Breakline.prototype = {
  25212. uiName: 'Breakline',
  25213. initSeparator: function (){
  25214. this.initUIBase();
  25215. },
  25216. getHtmlTpl: function (){
  25217. return '<br/>';
  25218. }
  25219. };
  25220. utils.inherits(Breakline, UIBase);
  25221. })();
  25222. // ui/message.js
  25223. ///import core
  25224. ///import uicore
  25225. (function () {
  25226. var utils = baidu.editor.utils,
  25227. domUtils = baidu.editor.dom.domUtils,
  25228. UIBase = baidu.editor.ui.UIBase,
  25229. Message = baidu.editor.ui.Message = function (options){
  25230. this.initOptions(options);
  25231. this.initMessage();
  25232. };
  25233. Message.prototype = {
  25234. initMessage: function (){
  25235. this.initUIBase();
  25236. },
  25237. getHtmlTpl: function (){
  25238. return '<div id="##" class="edui-message %%">' +
  25239. ' <div id="##_closer" class="edui-message-closer">×</div>' +
  25240. ' <div id="##_body" class="edui-message-body edui-message-type-info">' +
  25241. ' <iframe style="position:absolute;z-index:-1;left:0;top:0;background-color: transparent;" frameborder="0" width="100%" height="100%" src="about:blank"></iframe>' +
  25242. ' <div class="edui-shadow"></div>' +
  25243. ' <div id="##_content" class="edui-message-content">' +
  25244. ' </div>' +
  25245. ' </div>' +
  25246. '</div>';
  25247. },
  25248. reset: function(opt){
  25249. var me = this;
  25250. if (!opt.keepshow) {
  25251. clearTimeout(this.timer);
  25252. me.timer = setTimeout(function(){
  25253. me.hide();
  25254. }, opt.timeout || 4000);
  25255. }
  25256. opt.content !== undefined && me.setContent(opt.content);
  25257. opt.type !== undefined && me.setType(opt.type);
  25258. me.show();
  25259. },
  25260. postRender: function(){
  25261. var me = this,
  25262. closer = this.getDom('closer');
  25263. closer && domUtils.on(closer, 'click', function(){
  25264. me.hide();
  25265. });
  25266. },
  25267. setContent: function(content){
  25268. this.getDom('content').innerHTML = content;
  25269. },
  25270. setType: function(type){
  25271. type = type || 'info';
  25272. var body = this.getDom('body');
  25273. body.className = body.className.replace(/edui-message-type-[\w-]+/, 'edui-message-type-' + type);
  25274. },
  25275. getContent: function(){
  25276. return this.getDom('content').innerHTML;
  25277. },
  25278. getType: function(){
  25279. var arr = this.getDom('body').match(/edui-message-type-([\w-]+)/);
  25280. return arr ? arr[1]:'';
  25281. },
  25282. show: function (){
  25283. this.getDom().style.display = 'block';
  25284. },
  25285. hide: function (){
  25286. var dom = this.getDom();
  25287. if (dom) {
  25288. dom.style.display = 'none';
  25289. dom.parentNode && dom.parentNode.removeChild(dom);
  25290. }
  25291. }
  25292. };
  25293. utils.inherits(Message, UIBase);
  25294. })();
  25295. // adapter/editorui.js
  25296. //ui跟编辑器的适配層
  25297. //那个按钮弹出是dialog,是下拉筐等都是在这个js中配置
  25298. //自己写的ui也要在这里配置,放到baidu.editor.ui下边,当编辑器实例化的时候会根据ueditor.config中的toolbars找到相应的进行实例化
  25299. (function () {
  25300. var utils = baidu.editor.utils;
  25301. var editorui = baidu.editor.ui;
  25302. var _Dialog = editorui.Dialog;
  25303. editorui.buttons = {};
  25304. editorui.Dialog = function (options) {
  25305. var dialog = new _Dialog(options);
  25306. dialog.addListener('hide', function () {
  25307. if (dialog.editor) {
  25308. var editor = dialog.editor;
  25309. try {
  25310. if (browser.gecko) {
  25311. var y = editor.window.scrollY,
  25312. x = editor.window.scrollX;
  25313. editor.body.focus();
  25314. editor.window.scrollTo(x, y);
  25315. } else {
  25316. editor.focus();
  25317. }
  25318. } catch (ex) {
  25319. }
  25320. }
  25321. });
  25322. return dialog;
  25323. };
  25324. var iframeUrlMap = {
  25325. 'anchor':'~/dialogs/anchor/anchor.html',
  25326. 'insertimage':'~/dialogs/image/image.html',
  25327. 'link':'~/dialogs/link/link.html',
  25328. 'spechars':'~/dialogs/spechars/spechars.html',
  25329. 'searchreplace':'~/dialogs/searchreplace/searchreplace.html',
  25330. 'map':'~/dialogs/map/map.html',
  25331. 'gmap':'~/dialogs/gmap/gmap.html',
  25332. 'insertvideo':'~/dialogs/video/video.html',
  25333. 'help':'~/dialogs/help/help.html',
  25334. 'preview':'~/dialogs/preview/preview.html',
  25335. 'emotion':'~/dialogs/emotion/emotion.html',
  25336. 'wordimage':'~/dialogs/wordimage/wordimage.html',
  25337. 'attachment':'~/dialogs/attachment/attachment.html',
  25338. 'insertframe':'~/dialogs/insertframe/insertframe.html',
  25339. 'edittip':'~/dialogs/table/edittip.html',
  25340. 'edittable':'~/dialogs/table/edittable.html',
  25341. 'edittd':'~/dialogs/table/edittd.html',
  25342. 'webapp':'~/dialogs/webapp/webapp.html',
  25343. 'snapscreen':'~/dialogs/snapscreen/snapscreen.html',
  25344. 'scrawl':'~/dialogs/scrawl/scrawl.html',
  25345. 'music':'~/dialogs/music/music.html',
  25346. 'template':'~/dialogs/template/template.html',
  25347. 'background':'~/dialogs/background/background.html',
  25348. 'charts': '~/dialogs/charts/charts.html'
  25349. };
  25350. //为工具栏添加按钮,以下都是统一的按钮触发命令,所以写在一起
  25351. var btnCmds = ['undo', 'redo', 'formatmatch',
  25352. 'bold', 'italic', 'underline', 'fontborder', 'touppercase', 'tolowercase',
  25353. 'strikethrough', 'subscript', 'superscript', 'source', 'indent', 'outdent',
  25354. 'blockquote', 'pasteplain', 'pagebreak',
  25355. 'selectall', 'print','horizontal', 'removeformat', 'time', 'date', 'unlink',
  25356. 'insertparagraphbeforetable', 'insertrow', 'insertcol', 'mergeright', 'mergedown', 'deleterow',
  25357. 'deletecol', 'splittorows', 'splittocols', 'splittocells', 'mergecells', 'deletetable', 'drafts'];
  25358. for (var i = 0, ci; ci = btnCmds[i++];) {
  25359. ci = ci.toLowerCase();
  25360. editorui[ci] = function (cmd) {
  25361. return function (editor) {
  25362. var ui = new editorui.Button({
  25363. className:'edui-for-' + cmd,
  25364. title:editor.options.labelMap[cmd] || editor.getLang("labelMap." + cmd) || '',
  25365. onclick:function () {
  25366. editor.execCommand(cmd);
  25367. },
  25368. theme:editor.options.theme,
  25369. showText:false
  25370. });
  25371. editorui.buttons[cmd] = ui;
  25372. editor.addListener('selectionchange', function (type, causeByUi, uiReady) {
  25373. var state = editor.queryCommandState(cmd);
  25374. if (state == -1) {
  25375. ui.setDisabled(true);
  25376. ui.setChecked(false);
  25377. } else {
  25378. if (!uiReady) {
  25379. ui.setDisabled(false);
  25380. ui.setChecked(state);
  25381. }
  25382. }
  25383. });
  25384. return ui;
  25385. };
  25386. }(ci);
  25387. }
  25388. //清除文档
  25389. editorui.cleardoc = function (editor) {
  25390. var ui = new editorui.Button({
  25391. className:'edui-for-cleardoc',
  25392. title:editor.options.labelMap.cleardoc || editor.getLang("labelMap.cleardoc") || '',
  25393. theme:editor.options.theme,
  25394. onclick:function () {
  25395. if (confirm(editor.getLang("confirmClear"))) {
  25396. editor.execCommand('cleardoc');
  25397. }
  25398. }
  25399. });
  25400. editorui.buttons["cleardoc"] = ui;
  25401. editor.addListener('selectionchange', function () {
  25402. ui.setDisabled(editor.queryCommandState('cleardoc') == -1);
  25403. });
  25404. return ui;
  25405. };
  25406. //排版,图片排版,文字方向
  25407. var typeset = {
  25408. 'justify':['left', 'right', 'center', 'justify'],
  25409. 'imagefloat':['none', 'left', 'center', 'right'],
  25410. 'directionality':['ltr', 'rtl']
  25411. };
  25412. for (var p in typeset) {
  25413. (function (cmd, val) {
  25414. for (var i = 0, ci; ci = val[i++];) {
  25415. (function (cmd2) {
  25416. editorui[cmd.replace('float', '') + cmd2] = function (editor) {
  25417. var ui = new editorui.Button({
  25418. className:'edui-for-' + cmd.replace('float', '') + cmd2,
  25419. title:editor.options.labelMap[cmd.replace('float', '') + cmd2] || editor.getLang("labelMap." + cmd.replace('float', '') + cmd2) || '',
  25420. theme:editor.options.theme,
  25421. onclick:function () {
  25422. editor.execCommand(cmd, cmd2);
  25423. }
  25424. });
  25425. editorui.buttons[cmd] = ui;
  25426. editor.addListener('selectionchange', function (type, causeByUi, uiReady) {
  25427. ui.setDisabled(editor.queryCommandState(cmd) == -1);
  25428. ui.setChecked(editor.queryCommandValue(cmd) == cmd2 && !uiReady);
  25429. });
  25430. return ui;
  25431. };
  25432. })(ci)
  25433. }
  25434. })(p, typeset[p])
  25435. }
  25436. //字体颜色和背景颜色
  25437. for (var i = 0, ci; ci = ['backcolor', 'forecolor'][i++];) {
  25438. editorui[ci] = function (cmd) {
  25439. return function (editor) {
  25440. var ui = new editorui.ColorButton({
  25441. className:'edui-for-' + cmd,
  25442. color:'default',
  25443. title:editor.options.labelMap[cmd] || editor.getLang("labelMap." + cmd) || '',
  25444. editor:editor,
  25445. onpickcolor:function (t, color) {
  25446. editor.execCommand(cmd, color);
  25447. },
  25448. onpicknocolor:function () {
  25449. editor.execCommand(cmd, 'default');
  25450. this.setColor('transparent');
  25451. this.color = 'default';
  25452. },
  25453. onbuttonclick:function () {
  25454. editor.execCommand(cmd, this.color);
  25455. }
  25456. });
  25457. editorui.buttons[cmd] = ui;
  25458. editor.addListener('selectionchange', function () {
  25459. ui.setDisabled(editor.queryCommandState(cmd) == -1);
  25460. });
  25461. return ui;
  25462. };
  25463. }(ci);
  25464. }
  25465. var dialogBtns = {
  25466. noOk:['searchreplace', 'help', 'spechars', 'webapp','preview'],
  25467. ok:['attachment', 'anchor', 'link', 'insertimage', 'map', 'gmap', 'insertframe', 'wordimage',
  25468. 'insertvideo', 'insertframe', 'edittip', 'edittable', 'edittd', 'scrawl', 'template', 'music', 'background', 'charts']
  25469. };
  25470. for (var p in dialogBtns) {
  25471. (function (type, vals) {
  25472. for (var i = 0, ci; ci = vals[i++];) {
  25473. //todo opera下存在问题
  25474. if (browser.opera && ci === "searchreplace") {
  25475. continue;
  25476. }
  25477. (function (cmd) {
  25478. editorui[cmd] = function (editor, iframeUrl, title) {
  25479. iframeUrl = iframeUrl || (editor.options.iframeUrlMap || {})[cmd] || iframeUrlMap[cmd];
  25480. title = editor.options.labelMap[cmd] || editor.getLang("labelMap." + cmd) || '';
  25481. var dialog;
  25482. //没有iframeUrl不创建dialog
  25483. if (iframeUrl) {
  25484. dialog = new editorui.Dialog(utils.extend({
  25485. iframeUrl:editor.ui.mapUrl(iframeUrl),
  25486. editor:editor,
  25487. className:'edui-for-' + cmd,
  25488. title:title,
  25489. holdScroll: cmd === 'insertimage',
  25490. fullscreen: /charts|preview/.test(cmd),
  25491. closeDialog:editor.getLang("closeDialog")
  25492. }, type == 'ok' ? {
  25493. buttons:[
  25494. {
  25495. className:'edui-okbutton',
  25496. label:editor.getLang("ok"),
  25497. editor:editor,
  25498. onclick:function () {
  25499. dialog.close(true);
  25500. }
  25501. },
  25502. {
  25503. className:'edui-cancelbutton',
  25504. label:editor.getLang("cancel"),
  25505. editor:editor,
  25506. onclick:function () {
  25507. dialog.close(false);
  25508. }
  25509. }
  25510. ]
  25511. } : {}));
  25512. editor.ui._dialogs[cmd + "Dialog"] = dialog;
  25513. }
  25514. var ui = new editorui.Button({
  25515. className:'edui-for-' + cmd,
  25516. title:title,
  25517. onclick:function () {
  25518. if (dialog) {
  25519. switch (cmd) {
  25520. case "wordimage":
  25521. var images = editor.execCommand("wordimage");
  25522. if (images && images.length) {
  25523. dialog.render();
  25524. dialog.open();
  25525. }
  25526. break;
  25527. case "scrawl":
  25528. if (editor.queryCommandState("scrawl") != -1) {
  25529. dialog.render();
  25530. dialog.open();
  25531. }
  25532. break;
  25533. default:
  25534. dialog.render();
  25535. dialog.open();
  25536. }
  25537. }
  25538. },
  25539. theme:editor.options.theme,
  25540. disabled:(cmd == 'scrawl' && editor.queryCommandState("scrawl") == -1) || ( cmd == 'charts' )
  25541. });
  25542. editorui.buttons[cmd] = ui;
  25543. editor.addListener('selectionchange', function () {
  25544. //只存在于右键菜单而无工具栏按钮的ui不需要检测状态
  25545. var unNeedCheckState = {'edittable':1};
  25546. if (cmd in unNeedCheckState)return;
  25547. var state = editor.queryCommandState(cmd);
  25548. if (ui.getDom()) {
  25549. ui.setDisabled(state == -1);
  25550. ui.setChecked(state);
  25551. }
  25552. });
  25553. return ui;
  25554. };
  25555. })(ci.toLowerCase())
  25556. }
  25557. })(p, dialogBtns[p]);
  25558. }
  25559. editorui.snapscreen = function (editor, iframeUrl, title) {
  25560. title = editor.options.labelMap['snapscreen'] || editor.getLang("labelMap.snapscreen") || '';
  25561. var ui = new editorui.Button({
  25562. className:'edui-for-snapscreen',
  25563. title:title,
  25564. onclick:function () {
  25565. editor.execCommand("snapscreen");
  25566. },
  25567. theme:editor.options.theme
  25568. });
  25569. editorui.buttons['snapscreen'] = ui;
  25570. iframeUrl = iframeUrl || (editor.options.iframeUrlMap || {})["snapscreen"] || iframeUrlMap["snapscreen"];
  25571. if (iframeUrl) {
  25572. var dialog = new editorui.Dialog({
  25573. iframeUrl:editor.ui.mapUrl(iframeUrl),
  25574. editor:editor,
  25575. className:'edui-for-snapscreen',
  25576. title:title,
  25577. buttons:[
  25578. {
  25579. className:'edui-okbutton',
  25580. label:editor.getLang("ok"),
  25581. editor:editor,
  25582. onclick:function () {
  25583. dialog.close(true);
  25584. }
  25585. },
  25586. {
  25587. className:'edui-cancelbutton',
  25588. label:editor.getLang("cancel"),
  25589. editor:editor,
  25590. onclick:function () {
  25591. dialog.close(false);
  25592. }
  25593. }
  25594. ]
  25595. });
  25596. dialog.render();
  25597. editor.ui._dialogs["snapscreenDialog"] = dialog;
  25598. }
  25599. editor.addListener('selectionchange', function () {
  25600. ui.setDisabled(editor.queryCommandState('snapscreen') == -1);
  25601. });
  25602. return ui;
  25603. };
  25604. editorui.insertcode = function (editor, list, title) {
  25605. list = editor.options['insertcode'] || [];
  25606. title = editor.options.labelMap['insertcode'] || editor.getLang("labelMap.insertcode") || '';
  25607. // if (!list.length) return;
  25608. var items = [];
  25609. utils.each(list,function(key,val){
  25610. items.push({
  25611. label:key,
  25612. value:val,
  25613. theme:editor.options.theme,
  25614. renderLabelHtml:function () {
  25615. return '<div class="edui-label %%-label" >' + (this.label || '') + '</div>';
  25616. }
  25617. });
  25618. });
  25619. var ui = new editorui.Combox({
  25620. editor:editor,
  25621. items:items,
  25622. onselect:function (t, index) {
  25623. editor.execCommand('insertcode', this.items[index].value);
  25624. },
  25625. onbuttonclick:function () {
  25626. this.showPopup();
  25627. },
  25628. title:title,
  25629. initValue:title,
  25630. className:'edui-for-insertcode',
  25631. indexByValue:function (value) {
  25632. if (value) {
  25633. for (var i = 0, ci; ci = this.items[i]; i++) {
  25634. if (ci.value.indexOf(value) != -1)
  25635. return i;
  25636. }
  25637. }
  25638. return -1;
  25639. }
  25640. });
  25641. editorui.buttons['insertcode'] = ui;
  25642. editor.addListener('selectionchange', function (type, causeByUi, uiReady) {
  25643. if (!uiReady) {
  25644. var state = editor.queryCommandState('insertcode');
  25645. if (state == -1) {
  25646. ui.setDisabled(true);
  25647. } else {
  25648. ui.setDisabled(false);
  25649. var value = editor.queryCommandValue('insertcode');
  25650. if(!value){
  25651. ui.setValue(title);
  25652. return;
  25653. }
  25654. //trace:1871 ie下从源码模式切换回来时,字体会带单引号,而且会有逗号
  25655. value && (value = value.replace(/['"]/g, '').split(',')[0]);
  25656. ui.setValue(value);
  25657. }
  25658. }
  25659. });
  25660. return ui;
  25661. };
  25662. editorui.fontfamily = function (editor, list, title) {
  25663. list = editor.options['fontfamily'] || [];
  25664. title = editor.options.labelMap['fontfamily'] || editor.getLang("labelMap.fontfamily") || '';
  25665. if (!list.length) return;
  25666. for (var i = 0, ci, items = []; ci = list[i]; i++) {
  25667. var langLabel = editor.getLang('fontfamily')[ci.name] || "";
  25668. (function (key, val) {
  25669. items.push({
  25670. label:key,
  25671. value:val,
  25672. theme:editor.options.theme,
  25673. renderLabelHtml:function () {
  25674. return '<div class="edui-label %%-label" style="font-family:' +
  25675. utils.unhtml(this.value) + '">' + (this.label || '') + '</div>';
  25676. }
  25677. });
  25678. })(ci.label || langLabel, ci.val)
  25679. }
  25680. var ui = new editorui.Combox({
  25681. editor:editor,
  25682. items:items,
  25683. onselect:function (t, index) {
  25684. editor.execCommand('FontFamily', this.items[index].value);
  25685. },
  25686. onbuttonclick:function () {
  25687. this.showPopup();
  25688. },
  25689. title:title,
  25690. initValue:title,
  25691. className:'edui-for-fontfamily',
  25692. indexByValue:function (value) {
  25693. if (value) {
  25694. for (var i = 0, ci; ci = this.items[i]; i++) {
  25695. if (ci.value.indexOf(value) != -1)
  25696. return i;
  25697. }
  25698. }
  25699. return -1;
  25700. }
  25701. });
  25702. editorui.buttons['fontfamily'] = ui;
  25703. editor.addListener('selectionchange', function (type, causeByUi, uiReady) {
  25704. if (!uiReady) {
  25705. var state = editor.queryCommandState('FontFamily');
  25706. if (state == -1) {
  25707. ui.setDisabled(true);
  25708. } else {
  25709. ui.setDisabled(false);
  25710. var value = editor.queryCommandValue('FontFamily');
  25711. //trace:1871 ie下从源码模式切换回来时,字体会带单引号,而且会有逗号
  25712. value && (value = value.replace(/['"]/g, '').split(',')[0]);
  25713. ui.setValue(value);
  25714. }
  25715. }
  25716. });
  25717. return ui;
  25718. };
  25719. editorui.fontsize = function (editor, list, title) {
  25720. title = editor.options.labelMap['fontsize'] || editor.getLang("labelMap.fontsize") || '';
  25721. list = list || editor.options['fontsize'] || [];
  25722. if (!list.length) return;
  25723. var items = [];
  25724. for (var i = 0; i < list.length; i++) {
  25725. var size = list[i] + 'px';
  25726. items.push({
  25727. label:size,
  25728. value:size,
  25729. theme:editor.options.theme,
  25730. renderLabelHtml:function () {
  25731. return '<div class="edui-label %%-label" style="line-height:1;font-size:' +
  25732. this.value + '">' + (this.label || '') + '</div>';
  25733. }
  25734. });
  25735. }
  25736. var ui = new editorui.Combox({
  25737. editor:editor,
  25738. items:items,
  25739. title:title,
  25740. initValue:title,
  25741. onselect:function (t, index) {
  25742. editor.execCommand('FontSize', this.items[index].value);
  25743. },
  25744. onbuttonclick:function () {
  25745. this.showPopup();
  25746. },
  25747. className:'edui-for-fontsize'
  25748. });
  25749. editorui.buttons['fontsize'] = ui;
  25750. editor.addListener('selectionchange', function (type, causeByUi, uiReady) {
  25751. if (!uiReady) {
  25752. var state = editor.queryCommandState('FontSize');
  25753. if (state == -1) {
  25754. ui.setDisabled(true);
  25755. } else {
  25756. ui.setDisabled(false);
  25757. ui.setValue(editor.queryCommandValue('FontSize'));
  25758. }
  25759. }
  25760. });
  25761. return ui;
  25762. };
  25763. editorui.paragraph = function (editor, list, title) {
  25764. title = editor.options.labelMap['paragraph'] || editor.getLang("labelMap.paragraph") || '';
  25765. list = editor.options['paragraph'] || [];
  25766. if (utils.isEmptyObject(list)) return;
  25767. var items = [];
  25768. for (var i in list) {
  25769. items.push({
  25770. value:i,
  25771. label:list[i] || editor.getLang("paragraph")[i],
  25772. theme:editor.options.theme,
  25773. renderLabelHtml:function () {
  25774. return '<div class="edui-label %%-label"><span class="edui-for-' + this.value + '">' + (this.label || '') + '</span></div>';
  25775. }
  25776. })
  25777. }
  25778. var ui = new editorui.Combox({
  25779. editor:editor,
  25780. items:items,
  25781. title:title,
  25782. initValue:title,
  25783. className:'edui-for-paragraph',
  25784. onselect:function (t, index) {
  25785. editor.execCommand('Paragraph', this.items[index].value);
  25786. },
  25787. onbuttonclick:function () {
  25788. this.showPopup();
  25789. }
  25790. });
  25791. editorui.buttons['paragraph'] = ui;
  25792. editor.addListener('selectionchange', function (type, causeByUi, uiReady) {
  25793. if (!uiReady) {
  25794. var state = editor.queryCommandState('Paragraph');
  25795. if (state == -1) {
  25796. ui.setDisabled(true);
  25797. } else {
  25798. ui.setDisabled(false);
  25799. var value = editor.queryCommandValue('Paragraph');
  25800. var index = ui.indexByValue(value);
  25801. if (index != -1) {
  25802. ui.setValue(value);
  25803. } else {
  25804. ui.setValue(ui.initValue);
  25805. }
  25806. }
  25807. }
  25808. });
  25809. return ui;
  25810. };
  25811. //自定义标题
  25812. editorui.customstyle = function (editor) {
  25813. var list = editor.options['customstyle'] || [],
  25814. title = editor.options.labelMap['customstyle'] || editor.getLang("labelMap.customstyle") || '';
  25815. if (!list.length)return;
  25816. var langCs = editor.getLang('customstyle');
  25817. for (var i = 0, items = [], t; t = list[i++];) {
  25818. (function (t) {
  25819. var ck = {};
  25820. ck.label = t.label ? t.label : langCs[t.name];
  25821. ck.style = t.style;
  25822. ck.className = t.className;
  25823. ck.tag = t.tag;
  25824. items.push({
  25825. label:ck.label,
  25826. value:ck,
  25827. theme:editor.options.theme,
  25828. renderLabelHtml:function () {
  25829. return '<div class="edui-label %%-label">' + '<' + ck.tag + ' ' + (ck.className ? ' class="' + ck.className + '"' : "")
  25830. + (ck.style ? ' style="' + ck.style + '"' : "") + '>' + ck.label + "<\/" + ck.tag + ">"
  25831. + '</div>';
  25832. }
  25833. });
  25834. })(t);
  25835. }
  25836. var ui = new editorui.Combox({
  25837. editor:editor,
  25838. items:items,
  25839. title:title,
  25840. initValue:title,
  25841. className:'edui-for-customstyle',
  25842. onselect:function (t, index) {
  25843. editor.execCommand('customstyle', this.items[index].value);
  25844. },
  25845. onbuttonclick:function () {
  25846. this.showPopup();
  25847. },
  25848. indexByValue:function (value) {
  25849. for (var i = 0, ti; ti = this.items[i++];) {
  25850. if (ti.label == value) {
  25851. return i - 1
  25852. }
  25853. }
  25854. return -1;
  25855. }
  25856. });
  25857. editorui.buttons['customstyle'] = ui;
  25858. editor.addListener('selectionchange', function (type, causeByUi, uiReady) {
  25859. if (!uiReady) {
  25860. var state = editor.queryCommandState('customstyle');
  25861. if (state == -1) {
  25862. ui.setDisabled(true);
  25863. } else {
  25864. ui.setDisabled(false);
  25865. var value = editor.queryCommandValue('customstyle');
  25866. var index = ui.indexByValue(value);
  25867. if (index != -1) {
  25868. ui.setValue(value);
  25869. } else {
  25870. ui.setValue(ui.initValue);
  25871. }
  25872. }
  25873. }
  25874. });
  25875. return ui;
  25876. };
  25877. editorui.inserttable = function (editor, iframeUrl, title) {
  25878. title = editor.options.labelMap['inserttable'] || editor.getLang("labelMap.inserttable") || '';
  25879. var ui = new editorui.TableButton({
  25880. editor:editor,
  25881. title:title,
  25882. className:'edui-for-inserttable',
  25883. onpicktable:function (t, numCols, numRows) {
  25884. editor.execCommand('InsertTable', {numRows:numRows, numCols:numCols, border:1});
  25885. },
  25886. onbuttonclick:function () {
  25887. this.showPopup();
  25888. }
  25889. });
  25890. editorui.buttons['inserttable'] = ui;
  25891. editor.addListener('selectionchange', function () {
  25892. ui.setDisabled(editor.queryCommandState('inserttable') == -1);
  25893. });
  25894. return ui;
  25895. };
  25896. editorui.lineheight = function (editor) {
  25897. var val = editor.options.lineheight || [];
  25898. if (!val.length)return;
  25899. for (var i = 0, ci, items = []; ci = val[i++];) {
  25900. items.push({
  25901. //todo:写死了
  25902. label:ci,
  25903. value:ci,
  25904. theme:editor.options.theme,
  25905. onclick:function () {
  25906. editor.execCommand("lineheight", this.value);
  25907. }
  25908. })
  25909. }
  25910. var ui = new editorui.MenuButton({
  25911. editor:editor,
  25912. className:'edui-for-lineheight',
  25913. title:editor.options.labelMap['lineheight'] || editor.getLang("labelMap.lineheight") || '',
  25914. items:items,
  25915. onbuttonclick:function () {
  25916. var value = editor.queryCommandValue('LineHeight') || this.value;
  25917. editor.execCommand("LineHeight", value);
  25918. }
  25919. });
  25920. editorui.buttons['lineheight'] = ui;
  25921. editor.addListener('selectionchange', function () {
  25922. var state = editor.queryCommandState('LineHeight');
  25923. if (state == -1) {
  25924. ui.setDisabled(true);
  25925. } else {
  25926. ui.setDisabled(false);
  25927. var value = editor.queryCommandValue('LineHeight');
  25928. value && ui.setValue((value + '').replace(/cm/, ''));
  25929. ui.setChecked(state)
  25930. }
  25931. });
  25932. return ui;
  25933. };
  25934. var rowspacings = ['top', 'bottom'];
  25935. for (var r = 0, ri; ri = rowspacings[r++];) {
  25936. (function (cmd) {
  25937. editorui['rowspacing' + cmd] = function (editor) {
  25938. var val = editor.options['rowspacing' + cmd] || [];
  25939. if (!val.length) return null;
  25940. for (var i = 0, ci, items = []; ci = val[i++];) {
  25941. items.push({
  25942. label:ci,
  25943. value:ci,
  25944. theme:editor.options.theme,
  25945. onclick:function () {
  25946. editor.execCommand("rowspacing", this.value, cmd);
  25947. }
  25948. })
  25949. }
  25950. var ui = new editorui.MenuButton({
  25951. editor:editor,
  25952. className:'edui-for-rowspacing' + cmd,
  25953. title:editor.options.labelMap['rowspacing' + cmd] || editor.getLang("labelMap.rowspacing" + cmd) || '',
  25954. items:items,
  25955. onbuttonclick:function () {
  25956. var value = editor.queryCommandValue('rowspacing', cmd) || this.value;
  25957. editor.execCommand("rowspacing", value, cmd);
  25958. }
  25959. });
  25960. editorui.buttons[cmd] = ui;
  25961. editor.addListener('selectionchange', function () {
  25962. var state = editor.queryCommandState('rowspacing', cmd);
  25963. if (state == -1) {
  25964. ui.setDisabled(true);
  25965. } else {
  25966. ui.setDisabled(false);
  25967. var value = editor.queryCommandValue('rowspacing', cmd);
  25968. value && ui.setValue((value + '').replace(/%/, ''));
  25969. ui.setChecked(state)
  25970. }
  25971. });
  25972. return ui;
  25973. }
  25974. })(ri)
  25975. }
  25976. //有序,无序列表
  25977. var lists = ['insertorderedlist', 'insertunorderedlist'];
  25978. for (var l = 0, cl; cl = lists[l++];) {
  25979. (function (cmd) {
  25980. editorui[cmd] = function (editor) {
  25981. var vals = editor.options[cmd],
  25982. _onMenuClick = function () {
  25983. editor.execCommand(cmd, this.value);
  25984. }, items = [];
  25985. for (var i in vals) {
  25986. items.push({
  25987. label:vals[i] || editor.getLang()[cmd][i] || "",
  25988. value:i,
  25989. theme:editor.options.theme,
  25990. onclick:_onMenuClick
  25991. })
  25992. }
  25993. var ui = new editorui.MenuButton({
  25994. editor:editor,
  25995. className:'edui-for-' + cmd,
  25996. title:editor.getLang("labelMap." + cmd) || '',
  25997. 'items':items,
  25998. onbuttonclick:function () {
  25999. var value = editor.queryCommandValue(cmd) || this.value;
  26000. editor.execCommand(cmd, value);
  26001. }
  26002. });
  26003. editorui.buttons[cmd] = ui;
  26004. editor.addListener('selectionchange', function () {
  26005. var state = editor.queryCommandState(cmd);
  26006. if (state == -1) {
  26007. ui.setDisabled(true);
  26008. } else {
  26009. ui.setDisabled(false);
  26010. var value = editor.queryCommandValue(cmd);
  26011. ui.setValue(value);
  26012. ui.setChecked(state)
  26013. }
  26014. });
  26015. return ui;
  26016. };
  26017. })(cl)
  26018. }
  26019. editorui.fullscreen = function (editor, title) {
  26020. title = editor.options.labelMap['fullscreen'] || editor.getLang("labelMap.fullscreen") || '';
  26021. var ui = new editorui.Button({
  26022. className:'edui-for-fullscreen',
  26023. title:title,
  26024. theme:editor.options.theme,
  26025. onclick:function () {
  26026. if (editor.ui) {
  26027. editor.ui.setFullScreen(!editor.ui.isFullScreen());
  26028. }
  26029. this.setChecked(editor.ui.isFullScreen());
  26030. }
  26031. });
  26032. editorui.buttons['fullscreen'] = ui;
  26033. editor.addListener('selectionchange', function () {
  26034. var state = editor.queryCommandState('fullscreen');
  26035. ui.setDisabled(state == -1);
  26036. ui.setChecked(editor.ui.isFullScreen());
  26037. });
  26038. return ui;
  26039. };
  26040. // 表情
  26041. editorui["emotion"] = function (editor, iframeUrl) {
  26042. var cmd = "emotion";
  26043. var ui = new editorui.MultiMenuPop({
  26044. title:editor.options.labelMap[cmd] || editor.getLang("labelMap." + cmd + "") || '',
  26045. editor:editor,
  26046. className:'edui-for-' + cmd,
  26047. iframeUrl:editor.ui.mapUrl(iframeUrl || (editor.options.iframeUrlMap || {})[cmd] || iframeUrlMap[cmd])
  26048. });
  26049. editorui.buttons[cmd] = ui;
  26050. editor.addListener('selectionchange', function () {
  26051. ui.setDisabled(editor.queryCommandState(cmd) == -1)
  26052. });
  26053. return ui;
  26054. };
  26055. editorui.autotypeset = function (editor) {
  26056. var ui = new editorui.AutoTypeSetButton({
  26057. editor:editor,
  26058. title:editor.options.labelMap['autotypeset'] || editor.getLang("labelMap.autotypeset") || '',
  26059. className:'edui-for-autotypeset',
  26060. onbuttonclick:function () {
  26061. editor.execCommand('autotypeset')
  26062. }
  26063. });
  26064. editorui.buttons['autotypeset'] = ui;
  26065. editor.addListener('selectionchange', function () {
  26066. ui.setDisabled(editor.queryCommandState('autotypeset') == -1);
  26067. });
  26068. return ui;
  26069. };
  26070. /* 简单上传插件 */
  26071. editorui["simpleupload"] = function (editor) {
  26072. var name = 'simpleupload',
  26073. ui = new editorui.Button({
  26074. className:'edui-for-' + name,
  26075. title:editor.options.labelMap[name] || editor.getLang("labelMap." + name) || '',
  26076. onclick:function () {},
  26077. theme:editor.options.theme,
  26078. showText:false
  26079. });
  26080. editorui.buttons[name] = ui;
  26081. editor.addListener('ready', function() {
  26082. var b = ui.getDom('body'),
  26083. iconSpan = b.children[0];
  26084. editor.fireEvent('simpleuploadbtnready', iconSpan);
  26085. });
  26086. editor.addListener('selectionchange', function (type, causeByUi, uiReady) {
  26087. var state = editor.queryCommandState(name);
  26088. if (state == -1) {
  26089. ui.setDisabled(true);
  26090. ui.setChecked(false);
  26091. } else {
  26092. if (!uiReady) {
  26093. ui.setDisabled(false);
  26094. ui.setChecked(state);
  26095. }
  26096. }
  26097. });
  26098. return ui;
  26099. };
  26100. })();
  26101. // adapter/editor.js
  26102. ///import core
  26103. ///commands 全屏
  26104. ///commandsName FullScreen
  26105. ///commandsTitle 全屏
  26106. (function () {
  26107. var utils = baidu.editor.utils,
  26108. uiUtils = baidu.editor.ui.uiUtils,
  26109. UIBase = baidu.editor.ui.UIBase,
  26110. domUtils = baidu.editor.dom.domUtils;
  26111. var nodeStack = [];
  26112. function EditorUI(options) {
  26113. this.initOptions(options);
  26114. this.initEditorUI();
  26115. }
  26116. EditorUI.prototype = {
  26117. uiName:'editor',
  26118. initEditorUI:function () {
  26119. this.editor.ui = this;
  26120. this._dialogs = {};
  26121. this.initUIBase();
  26122. this._initToolbars();
  26123. var editor = this.editor,
  26124. me = this;
  26125. editor.addListener('ready', function () {
  26126. //提供getDialog方法
  26127. editor.getDialog = function (name) {
  26128. return editor.ui._dialogs[name + "Dialog"];
  26129. };
  26130. domUtils.on(editor.window, 'scroll', function (evt) {
  26131. baidu.editor.ui.Popup.postHide(evt);
  26132. });
  26133. //提供编辑器实时宽高(全屏时宽高不变化)
  26134. editor.ui._actualFrameWidth = editor.options.initialFrameWidth;
  26135. UE.browser.ie && UE.browser.version === 6 && editor.container.ownerDocument.execCommand("BackgroundImageCache", false, true);
  26136. //display bottom-bar label based on config
  26137. if (editor.options.elementPathEnabled) {
  26138. editor.ui.getDom('elementpath').innerHTML = '<div class="edui-editor-breadcrumb">' + editor.getLang("elementPathTip") + ':</div>';
  26139. }
  26140. if (editor.options.wordCount) {
  26141. function countFn() {
  26142. setCount(editor,me);
  26143. domUtils.un(editor.document, "click", arguments.callee);
  26144. }
  26145. domUtils.on(editor.document, "click", countFn);
  26146. editor.ui.getDom('wordcount').innerHTML = editor.getLang("wordCountTip");
  26147. }
  26148. editor.ui._scale();
  26149. if (editor.options.scaleEnabled) {
  26150. if (editor.autoHeightEnabled) {
  26151. editor.disableAutoHeight();
  26152. }
  26153. me.enableScale();
  26154. } else {
  26155. me.disableScale();
  26156. }
  26157. if (!editor.options.elementPathEnabled && !editor.options.wordCount && !editor.options.scaleEnabled) {
  26158. editor.ui.getDom('elementpath').style.display = "none";
  26159. editor.ui.getDom('wordcount').style.display = "none";
  26160. editor.ui.getDom('scale').style.display = "none";
  26161. }
  26162. if (!editor.selection.isFocus())return;
  26163. editor.fireEvent('selectionchange', false, true);
  26164. });
  26165. editor.addListener('mousedown', function (t, evt) {
  26166. var el = evt.target || evt.srcElement;
  26167. baidu.editor.ui.Popup.postHide(evt, el);
  26168. baidu.editor.ui.ShortCutMenu.postHide(evt);
  26169. });
  26170. editor.addListener("delcells", function () {
  26171. if (UE.ui['edittip']) {
  26172. new UE.ui['edittip'](editor);
  26173. }
  26174. editor.getDialog('edittip').open();
  26175. });
  26176. var pastePop, isPaste = false, timer;
  26177. editor.addListener("afterpaste", function () {
  26178. if(editor.queryCommandState('pasteplain'))
  26179. return;
  26180. if(baidu.editor.ui.PastePicker){
  26181. pastePop = new baidu.editor.ui.Popup({
  26182. content:new baidu.editor.ui.PastePicker({editor:editor}),
  26183. editor:editor,
  26184. className:'edui-wordpastepop'
  26185. });
  26186. pastePop.render();
  26187. }
  26188. isPaste = true;
  26189. });
  26190. editor.addListener("afterinserthtml", function () {
  26191. clearTimeout(timer);
  26192. timer = setTimeout(function () {
  26193. if (pastePop && (isPaste || editor.ui._isTransfer)) {
  26194. if(pastePop.isHidden()){
  26195. var span = domUtils.createElement(editor.document, 'span', {
  26196. 'style':"line-height:0px;",
  26197. 'innerHTML':'\ufeff'
  26198. }),
  26199. range = editor.selection.getRange();
  26200. range.insertNode(span);
  26201. var tmp= getDomNode(span, 'firstChild', 'previousSibling');
  26202. tmp && pastePop.showAnchor(tmp.nodeType == 3 ? tmp.parentNode : tmp);
  26203. domUtils.remove(span);
  26204. }else{
  26205. pastePop.show();
  26206. }
  26207. delete editor.ui._isTransfer;
  26208. isPaste = false;
  26209. }
  26210. }, 200)
  26211. });
  26212. editor.addListener('contextmenu', function (t, evt) {
  26213. baidu.editor.ui.Popup.postHide(evt);
  26214. });
  26215. editor.addListener('keydown', function (t, evt) {
  26216. if (pastePop) pastePop.dispose(evt);
  26217. var keyCode = evt.keyCode || evt.which;
  26218. if(evt.altKey&&keyCode==90){
  26219. UE.ui.buttons['fullscreen'].onclick();
  26220. }
  26221. });
  26222. editor.addListener('wordcount', function (type) {
  26223. setCount(this,me);
  26224. });
  26225. function setCount(editor,ui) {
  26226. editor.setOpt({
  26227. wordCount:true,
  26228. maximumWords:10000,
  26229. wordCountMsg:editor.options.wordCountMsg || editor.getLang("wordCountMsg"),
  26230. wordOverFlowMsg:editor.options.wordOverFlowMsg || editor.getLang("wordOverFlowMsg")
  26231. });
  26232. var opt = editor.options,
  26233. max = opt.maximumWords,
  26234. msg = opt.wordCountMsg ,
  26235. errMsg = opt.wordOverFlowMsg,
  26236. countDom = ui.getDom('wordcount');
  26237. if (!opt.wordCount) {
  26238. return;
  26239. }
  26240. var count = editor.getContentLength(true);
  26241. if (count > max) {
  26242. countDom.innerHTML = errMsg;
  26243. editor.fireEvent("wordcountoverflow");
  26244. } else {
  26245. countDom.innerHTML = msg.replace("{#leave}", max - count).replace("{#count}", count);
  26246. }
  26247. }
  26248. editor.addListener('selectionchange', function () {
  26249. if (editor.options.elementPathEnabled) {
  26250. me[(editor.queryCommandState('elementpath') == -1 ? 'dis' : 'en') + 'ableElementPath']()
  26251. }
  26252. if (editor.options.scaleEnabled) {
  26253. me[(editor.queryCommandState('scale') == -1 ? 'dis' : 'en') + 'ableScale']();
  26254. }
  26255. });
  26256. var popup = new baidu.editor.ui.Popup({
  26257. editor:editor,
  26258. content:'',
  26259. className:'edui-bubble',
  26260. _onEditButtonClick:function () {
  26261. this.hide();
  26262. editor.ui._dialogs.linkDialog.open();
  26263. },
  26264. _onImgEditButtonClick:function (name) {
  26265. this.hide();
  26266. editor.ui._dialogs[name] && editor.ui._dialogs[name].open();
  26267. },
  26268. _onImgSetFloat:function (value) {
  26269. this.hide();
  26270. editor.execCommand("imagefloat", value);
  26271. },
  26272. _setIframeAlign:function (value) {
  26273. var frame = popup.anchorEl;
  26274. var newFrame = frame.cloneNode(true);
  26275. switch (value) {
  26276. case -2:
  26277. newFrame.setAttribute("align", "");
  26278. break;
  26279. case -1:
  26280. newFrame.setAttribute("align", "left");
  26281. break;
  26282. case 1:
  26283. newFrame.setAttribute("align", "right");
  26284. break;
  26285. }
  26286. frame.parentNode.insertBefore(newFrame, frame);
  26287. domUtils.remove(frame);
  26288. popup.anchorEl = newFrame;
  26289. popup.showAnchor(popup.anchorEl);
  26290. },
  26291. _updateIframe:function () {
  26292. var frame = editor._iframe = popup.anchorEl;
  26293. if(domUtils.hasClass(frame, 'ueditor_baidumap')) {
  26294. editor.selection.getRange().selectNode(frame).select();
  26295. editor.ui._dialogs.mapDialog.open();
  26296. popup.hide();
  26297. } else {
  26298. editor.ui._dialogs.insertframeDialog.open();
  26299. popup.hide();
  26300. }
  26301. },
  26302. _onRemoveButtonClick:function (cmdName) {
  26303. editor.execCommand(cmdName);
  26304. this.hide();
  26305. },
  26306. queryAutoHide:function (el) {
  26307. if (el && el.ownerDocument == editor.document) {
  26308. if (el.tagName.toLowerCase() == 'img' || domUtils.findParentByTagName(el, 'a', true)) {
  26309. return el !== popup.anchorEl;
  26310. }
  26311. }
  26312. return baidu.editor.ui.Popup.prototype.queryAutoHide.call(this, el);
  26313. }
  26314. });
  26315. popup.render();
  26316. if (editor.options.imagePopup) {
  26317. editor.addListener('mouseover', function (t, evt) {
  26318. evt = evt || window.event;
  26319. var el = evt.target || evt.srcElement;
  26320. if (editor.ui._dialogs.insertframeDialog && /iframe/ig.test(el.tagName)) {
  26321. var html = popup.formatHtml(
  26322. '<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;' +
  26323. ' <span onclick="$$._updateIframe( this);" class="edui-clickable">' + editor.getLang("modify") + '</span></nobr>');
  26324. if (html) {
  26325. popup.getDom('content').innerHTML = html;
  26326. popup.anchorEl = el;
  26327. popup.showAnchor(popup.anchorEl);
  26328. } else {
  26329. popup.hide();
  26330. }
  26331. }
  26332. });
  26333. editor.addListener('selectionchange', function (t, causeByUi) {
  26334. if (!causeByUi) return;
  26335. var html = '', str = "",
  26336. img = editor.selection.getRange().getClosedNode(),
  26337. dialogs = editor.ui._dialogs;
  26338. if (img && img.tagName == 'IMG') {
  26339. var dialogName = 'insertimageDialog';
  26340. if (img.className.indexOf("edui-faked-video") != -1 || img.className.indexOf("edui-upload-video") != -1) {
  26341. dialogName = "insertvideoDialog"
  26342. }
  26343. if (img.className.indexOf("edui-faked-webapp") != -1) {
  26344. dialogName = "webappDialog"
  26345. }
  26346. if (img.src.indexOf("http://api.map.baidu.com") != -1) {
  26347. dialogName = "mapDialog"
  26348. }
  26349. if (img.className.indexOf("edui-faked-music") != -1) {
  26350. dialogName = "musicDialog"
  26351. }
  26352. if (img.src.indexOf("http://maps.google.com/maps/api/staticmap") != -1) {
  26353. dialogName = "gmapDialog"
  26354. }
  26355. if (img.getAttribute("anchorname")) {
  26356. dialogName = "anchorDialog";
  26357. html = popup.formatHtml(
  26358. '<nobr>' + editor.getLang("property") + ': <span onclick=$$._onImgEditButtonClick("anchorDialog") class="edui-clickable">' + editor.getLang("modify") + '</span>&nbsp;&nbsp;' +
  26359. '<span onclick=$$._onRemoveButtonClick(\'anchor\') class="edui-clickable">' + editor.getLang("delete") + '</span></nobr>');
  26360. }
  26361. if (img.getAttribute("word_img")) {
  26362. //todo 放到dialog去做查询
  26363. editor.word_img = [img.getAttribute("word_img")];
  26364. dialogName = "wordimageDialog"
  26365. }
  26366. if(domUtils.hasClass(img, 'loadingclass') || domUtils.hasClass(img, 'loaderrorclass')) {
  26367. dialogName = "";
  26368. }
  26369. if (!dialogs[dialogName]) {
  26370. return;
  26371. }
  26372. str = '<nobr>' + editor.getLang("property") + ': '+
  26373. '<span onclick=$$._onImgSetFloat("none") class="edui-clickable">' + editor.getLang("default") + '</span>&nbsp;&nbsp;' +
  26374. '<span onclick=$$._onImgSetFloat("left") class="edui-clickable">' + editor.getLang("justifyleft") + '</span>&nbsp;&nbsp;' +
  26375. '<span onclick=$$._onImgSetFloat("right") class="edui-clickable">' + editor.getLang("justifyright") + '</span>&nbsp;&nbsp;' +
  26376. '<span onclick=$$._onImgSetFloat("center") class="edui-clickable">' + editor.getLang("justifycenter") + '</span>&nbsp;&nbsp;'+
  26377. '<span onclick="$$._onImgEditButtonClick(\'' + dialogName + '\');" class="edui-clickable">' + editor.getLang("modify") + '</span></nobr>';
  26378. !html && (html = popup.formatHtml(str))
  26379. }
  26380. if (editor.ui._dialogs.linkDialog) {
  26381. var link = editor.queryCommandValue('link');
  26382. var url;
  26383. if (link && (url = (link.getAttribute('_href') || link.getAttribute('href', 2)))) {
  26384. var txt = url;
  26385. if (url.length > 30) {
  26386. txt = url.substring(0, 20) + "...";
  26387. }
  26388. if (html) {
  26389. html += '<div style="height:5px;"></div>'
  26390. }
  26391. html += popup.formatHtml(
  26392. '<nobr>' + editor.getLang("anthorMsg") + ': <a target="_blank" href="' + url + '" title="' + url + '" >' + txt + '</a>' +
  26393. ' <span class="edui-clickable" onclick="$$._onEditButtonClick();">' + editor.getLang("modify") + '</span>' +
  26394. ' <span class="edui-clickable" onclick="$$._onRemoveButtonClick(\'unlink\');"> ' + editor.getLang("clear") + '</span></nobr>');
  26395. popup.showAnchor(link);
  26396. }
  26397. }
  26398. if (html) {
  26399. popup.getDom('content').innerHTML = html;
  26400. popup.anchorEl = img || link;
  26401. popup.showAnchor(popup.anchorEl);
  26402. } else {
  26403. popup.hide();
  26404. }
  26405. });
  26406. }
  26407. },
  26408. _initToolbars:function () {
  26409. var editor = this.editor;
  26410. var toolbars = this.toolbars || [];
  26411. var toolbarUis = [];
  26412. for (var i = 0; i < toolbars.length; i++) {
  26413. var toolbar = toolbars[i];
  26414. var toolbarUi = new baidu.editor.ui.Toolbar({theme:editor.options.theme});
  26415. for (var j = 0; j < toolbar.length; j++) {
  26416. var toolbarItem = toolbar[j];
  26417. var toolbarItemUi = null;
  26418. if (typeof toolbarItem == 'string') {
  26419. toolbarItem = toolbarItem.toLowerCase();
  26420. if (toolbarItem == '|') {
  26421. toolbarItem = 'Separator';
  26422. }
  26423. if(toolbarItem == '||'){
  26424. toolbarItem = 'Breakline';
  26425. }
  26426. if (baidu.editor.ui[toolbarItem]) {
  26427. toolbarItemUi = new baidu.editor.ui[toolbarItem](editor);
  26428. }
  26429. //fullscreen这里单独处理一下,放到首行去
  26430. if (toolbarItem == 'fullscreen') {
  26431. if (toolbarUis && toolbarUis[0]) {
  26432. toolbarUis[0].items.splice(0, 0, toolbarItemUi);
  26433. } else {
  26434. toolbarItemUi && toolbarUi.items.splice(0, 0, toolbarItemUi);
  26435. }
  26436. continue;
  26437. }
  26438. } else {
  26439. toolbarItemUi = toolbarItem;
  26440. }
  26441. if (toolbarItemUi && toolbarItemUi.id) {
  26442. toolbarUi.add(toolbarItemUi);
  26443. }
  26444. }
  26445. toolbarUis[i] = toolbarUi;
  26446. }
  26447. //接受外部定制的UI
  26448. utils.each(UE._customizeUI,function(obj,key){
  26449. var itemUI,index;
  26450. if(obj.id && obj.id != editor.key){
  26451. return false;
  26452. }
  26453. itemUI = obj.execFn.call(editor,editor,key);
  26454. if(itemUI){
  26455. index = obj.index;
  26456. if(index === undefined){
  26457. index = toolbarUi.items.length;
  26458. }
  26459. toolbarUi.add(itemUI,index)
  26460. }
  26461. });
  26462. this.toolbars = toolbarUis;
  26463. },
  26464. getHtmlTpl:function () {
  26465. return '<div id="##" class="%%">' +
  26466. '<div id="##_toolbarbox" class="%%-toolbarbox">' +
  26467. (this.toolbars.length ?
  26468. '<div id="##_toolbarboxouter" class="%%-toolbarboxouter"><div class="%%-toolbarboxinner">' +
  26469. this.renderToolbarBoxHtml() +
  26470. '</div></div>' : '') +
  26471. '<div id="##_toolbarmsg" class="%%-toolbarmsg" style="display:none;">' +
  26472. '<div id = "##_upload_dialog" class="%%-toolbarmsg-upload" onclick="$$.showWordImageDialog();">' + this.editor.getLang("clickToUpload") + '</div>' +
  26473. '<div class="%%-toolbarmsg-close" onclick="$$.hideToolbarMsg();">x</div>' +
  26474. '<div id="##_toolbarmsg_label" class="%%-toolbarmsg-label"></div>' +
  26475. '<div style="height:0;overflow:hidden;clear:both;"></div>' +
  26476. '</div>' +
  26477. '<div id="##_message_holder" class="%%-messageholder"></div>' +
  26478. '</div>' +
  26479. '<div id="##_iframeholder" class="%%-iframeholder">' +
  26480. '</div>' +
  26481. //modify wdcount by matao
  26482. '<div id="##_bottombar" class="%%-bottomContainer"><table><tr>' +
  26483. '<td id="##_elementpath" class="%%-bottombar"></td>' +
  26484. '<td id="##_wordcount" class="%%-wordcount"></td>' +
  26485. '<td id="##_scale" class="%%-scale"><div class="%%-icon"></div></td>' +
  26486. '</tr></table></div>' +
  26487. '<div id="##_scalelayer"></div>' +
  26488. '</div>';
  26489. },
  26490. showWordImageDialog:function () {
  26491. this._dialogs['wordimageDialog'].open();
  26492. },
  26493. renderToolbarBoxHtml:function () {
  26494. var buff = [];
  26495. for (var i = 0; i < this.toolbars.length; i++) {
  26496. buff.push(this.toolbars[i].renderHtml());
  26497. }
  26498. return buff.join('');
  26499. },
  26500. setFullScreen:function (fullscreen) {
  26501. var editor = this.editor,
  26502. container = editor.container.parentNode.parentNode;
  26503. if (this._fullscreen != fullscreen) {
  26504. this._fullscreen = fullscreen;
  26505. this.editor.fireEvent('beforefullscreenchange', fullscreen);
  26506. if (baidu.editor.browser.gecko) {
  26507. var bk = editor.selection.getRange().createBookmark();
  26508. }
  26509. if (fullscreen) {
  26510. while (container.tagName != "BODY") {
  26511. var position = baidu.editor.dom.domUtils.getComputedStyle(container, "position");
  26512. nodeStack.push(position);
  26513. container.style.position = "static";
  26514. container = container.parentNode;
  26515. }
  26516. this._bakHtmlOverflow = document.documentElement.style.overflow;
  26517. this._bakBodyOverflow = document.body.style.overflow;
  26518. this._bakAutoHeight = this.editor.autoHeightEnabled;
  26519. this._bakScrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
  26520. this._bakEditorContaninerWidth = editor.iframe.parentNode.offsetWidth;
  26521. if (this._bakAutoHeight) {
  26522. //当全屏时不能执行自动长高
  26523. editor.autoHeightEnabled = false;
  26524. this.editor.disableAutoHeight();
  26525. }
  26526. document.documentElement.style.overflow = 'hidden';
  26527. //修复,滚动条不收起的问题
  26528. window.scrollTo(0,window.scrollY);
  26529. this._bakCssText = this.getDom().style.cssText;
  26530. this._bakCssText1 = this.getDom('iframeholder').style.cssText;
  26531. editor.iframe.parentNode.style.width = '';
  26532. this._updateFullScreen();
  26533. } else {
  26534. while (container.tagName != "BODY") {
  26535. container.style.position = nodeStack.shift();
  26536. container = container.parentNode;
  26537. }
  26538. this.getDom().style.cssText = this._bakCssText;
  26539. this.getDom('iframeholder').style.cssText = this._bakCssText1;
  26540. if (this._bakAutoHeight) {
  26541. editor.autoHeightEnabled = true;
  26542. this.editor.enableAutoHeight();
  26543. }
  26544. document.documentElement.style.overflow = this._bakHtmlOverflow;
  26545. document.body.style.overflow = this._bakBodyOverflow;
  26546. editor.iframe.parentNode.style.width = this._bakEditorContaninerWidth + 'px';
  26547. window.scrollTo(0, this._bakScrollTop);
  26548. }
  26549. if (browser.gecko && editor.body.contentEditable === 'true') {
  26550. var input = document.createElement('input');
  26551. document.body.appendChild(input);
  26552. editor.body.contentEditable = false;
  26553. setTimeout(function () {
  26554. input.focus();
  26555. setTimeout(function () {
  26556. editor.body.contentEditable = true;
  26557. editor.fireEvent('fullscreenchanged', fullscreen);
  26558. editor.selection.getRange().moveToBookmark(bk).select(true);
  26559. baidu.editor.dom.domUtils.remove(input);
  26560. fullscreen && window.scroll(0, 0);
  26561. }, 0)
  26562. }, 0)
  26563. }
  26564. if(editor.body.contentEditable === 'true'){
  26565. this.editor.fireEvent('fullscreenchanged', fullscreen);
  26566. this.triggerLayout();
  26567. }
  26568. }
  26569. },
  26570. _updateFullScreen:function () {
  26571. if (this._fullscreen) {
  26572. var vpRect = uiUtils.getViewportRect();
  26573. 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);
  26574. uiUtils.setViewportOffset(this.getDom(), { left:0, top:this.editor.options.topOffset || 0 });
  26575. this.editor.setHeight(vpRect.height - this.getDom('toolbarbox').offsetHeight - this.getDom('bottombar').offsetHeight - (this.editor.options.topOffset || 0),true);
  26576. //不手动调一下,会导致全屏失效
  26577. if(browser.gecko){
  26578. try{
  26579. window.onresize();
  26580. }catch(e){
  26581. }
  26582. }
  26583. }
  26584. },
  26585. _updateElementPath:function () {
  26586. var bottom = this.getDom('elementpath'), list;
  26587. if (this.elementPathEnabled && (list = this.editor.queryCommandValue('elementpath'))) {
  26588. var buff = [];
  26589. for (var i = 0, ci; ci = list[i]; i++) {
  26590. buff[i] = this.formatHtml('<span unselectable="on" onclick="$$.editor.execCommand(&quot;elementpath&quot;, &quot;' + i + '&quot;);">' + ci + '</span>');
  26591. }
  26592. bottom.innerHTML = '<div class="edui-editor-breadcrumb" onmousedown="return false;">' + this.editor.getLang("elementPathTip") + ': ' + buff.join(' &gt; ') + '</div>';
  26593. } else {
  26594. bottom.style.display = 'none'
  26595. }
  26596. },
  26597. disableElementPath:function () {
  26598. var bottom = this.getDom('elementpath');
  26599. bottom.innerHTML = '';
  26600. bottom.style.display = 'none';
  26601. this.elementPathEnabled = false;
  26602. },
  26603. enableElementPath:function () {
  26604. var bottom = this.getDom('elementpath');
  26605. bottom.style.display = '';
  26606. this.elementPathEnabled = true;
  26607. this._updateElementPath();
  26608. },
  26609. _scale:function () {
  26610. var doc = document,
  26611. editor = this.editor,
  26612. editorHolder = editor.container,
  26613. editorDocument = editor.document,
  26614. toolbarBox = this.getDom("toolbarbox"),
  26615. bottombar = this.getDom("bottombar"),
  26616. scale = this.getDom("scale"),
  26617. scalelayer = this.getDom("scalelayer");
  26618. var isMouseMove = false,
  26619. position = null,
  26620. minEditorHeight = 0,
  26621. minEditorWidth = editor.options.minFrameWidth,
  26622. pageX = 0,
  26623. pageY = 0,
  26624. scaleWidth = 0,
  26625. scaleHeight = 0;
  26626. function down() {
  26627. position = domUtils.getXY(editorHolder);
  26628. if (!minEditorHeight) {
  26629. minEditorHeight = editor.options.minFrameHeight + toolbarBox.offsetHeight + bottombar.offsetHeight;
  26630. }
  26631. 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:"
  26632. + editorHolder.offsetHeight + "px;z-index:" + (editor.options.zIndex + 1);
  26633. domUtils.on(doc, "mousemove", move);
  26634. domUtils.on(editorDocument, "mouseup", up);
  26635. domUtils.on(doc, "mouseup", up);
  26636. }
  26637. var me = this;
  26638. //by xuheng 全屏时关掉缩放
  26639. this.editor.addListener('fullscreenchanged', function (e, fullScreen) {
  26640. if (fullScreen) {
  26641. me.disableScale();
  26642. } else {
  26643. if (me.editor.options.scaleEnabled) {
  26644. me.enableScale();
  26645. var tmpNode = me.editor.document.createElement('span');
  26646. me.editor.body.appendChild(tmpNode);
  26647. me.editor.body.style.height = Math.max(domUtils.getXY(tmpNode).y, me.editor.iframe.offsetHeight - 20) + 'px';
  26648. domUtils.remove(tmpNode)
  26649. }
  26650. }
  26651. });
  26652. function move(event) {
  26653. clearSelection();
  26654. var e = event || window.event;
  26655. pageX = e.pageX || (doc.documentElement.scrollLeft + e.clientX);
  26656. pageY = e.pageY || (doc.documentElement.scrollTop + e.clientY);
  26657. scaleWidth = pageX - position.x;
  26658. scaleHeight = pageY - position.y;
  26659. if (scaleWidth >= minEditorWidth) {
  26660. isMouseMove = true;
  26661. scalelayer.style.width = scaleWidth + 'px';
  26662. }
  26663. if (scaleHeight >= minEditorHeight) {
  26664. isMouseMove = true;
  26665. scalelayer.style.height = scaleHeight + "px";
  26666. }
  26667. }
  26668. function up() {
  26669. if (isMouseMove) {
  26670. isMouseMove = false;
  26671. editor.ui._actualFrameWidth = scalelayer.offsetWidth - 2;
  26672. editorHolder.style.width = editor.ui._actualFrameWidth + 'px';
  26673. editor.setHeight(scalelayer.offsetHeight - bottombar.offsetHeight - toolbarBox.offsetHeight - 2,true);
  26674. }
  26675. if (scalelayer) {
  26676. scalelayer.style.display = "none";
  26677. }
  26678. clearSelection();
  26679. domUtils.un(doc, "mousemove", move);
  26680. domUtils.un(editorDocument, "mouseup", up);
  26681. domUtils.un(doc, "mouseup", up);
  26682. }
  26683. function clearSelection() {
  26684. if (browser.ie)
  26685. doc.selection.clear();
  26686. else
  26687. window.getSelection().removeAllRanges();
  26688. }
  26689. this.enableScale = function () {
  26690. //trace:2868
  26691. if (editor.queryCommandState("source") == 1) return;
  26692. scale.style.display = "";
  26693. this.scaleEnabled = true;
  26694. domUtils.on(scale, "mousedown", down);
  26695. };
  26696. this.disableScale = function () {
  26697. scale.style.display = "none";
  26698. this.scaleEnabled = false;
  26699. domUtils.un(scale, "mousedown", down);
  26700. };
  26701. },
  26702. isFullScreen:function () {
  26703. return this._fullscreen;
  26704. },
  26705. postRender:function () {
  26706. UIBase.prototype.postRender.call(this);
  26707. for (var i = 0; i < this.toolbars.length; i++) {
  26708. this.toolbars[i].postRender();
  26709. }
  26710. var me = this;
  26711. var timerId,
  26712. domUtils = baidu.editor.dom.domUtils,
  26713. updateFullScreenTime = function () {
  26714. clearTimeout(timerId);
  26715. timerId = setTimeout(function () {
  26716. me._updateFullScreen();
  26717. });
  26718. };
  26719. domUtils.on(window, 'resize', updateFullScreenTime);
  26720. me.addListener('destroy', function () {
  26721. domUtils.un(window, 'resize', updateFullScreenTime);
  26722. clearTimeout(timerId);
  26723. })
  26724. },
  26725. showToolbarMsg:function (msg, flag) {
  26726. this.getDom('toolbarmsg_label').innerHTML = msg;
  26727. this.getDom('toolbarmsg').style.display = '';
  26728. //
  26729. if (!flag) {
  26730. var w = this.getDom('upload_dialog');
  26731. w.style.display = 'none';
  26732. }
  26733. },
  26734. hideToolbarMsg:function () {
  26735. this.getDom('toolbarmsg').style.display = 'none';
  26736. },
  26737. mapUrl:function (url) {
  26738. return url ? url.replace('~/', this.editor.options.UEDITOR_HOME_URL || '') : ''
  26739. },
  26740. triggerLayout:function () {
  26741. var dom = this.getDom();
  26742. if (dom.style.zoom == '1') {
  26743. dom.style.zoom = '100%';
  26744. } else {
  26745. dom.style.zoom = '1';
  26746. }
  26747. }
  26748. };
  26749. utils.inherits(EditorUI, baidu.editor.ui.UIBase);
  26750. var instances = {};
  26751. UE.ui.Editor = function (options) {
  26752. var editor = new UE.Editor(options);
  26753. editor.options.editor = editor;
  26754. utils.loadFile(document, {
  26755. href:editor.options.themePath + editor.options.theme + "/css/ueditor.css",
  26756. tag:"link",
  26757. type:"text/css",
  26758. rel:"stylesheet"
  26759. });
  26760. var oldRender = editor.render;
  26761. editor.render = function (holder) {
  26762. if (holder.constructor === String) {
  26763. editor.key = holder;
  26764. instances[holder] = editor;
  26765. }
  26766. utils.domReady(function () {
  26767. editor.langIsReady ? renderUI() : editor.addListener("langReady", renderUI);
  26768. function renderUI() {
  26769. editor.setOpt({
  26770. labelMap:editor.options.labelMap || editor.getLang('labelMap')
  26771. });
  26772. new EditorUI(editor.options);
  26773. if (holder) {
  26774. if (holder.constructor === String) {
  26775. holder = document.getElementById(holder);
  26776. }
  26777. holder && holder.getAttribute('name') && ( editor.options.textarea = holder.getAttribute('name'));
  26778. if (holder && /script|textarea/ig.test(holder.tagName)) {
  26779. var newDiv = document.createElement('div');
  26780. holder.parentNode.insertBefore(newDiv, holder);
  26781. var cont = holder.value || holder.innerHTML;
  26782. editor.options.initialContent = /^[\t\r\n ]*$/.test(cont) ? editor.options.initialContent :
  26783. cont.replace(/>[\n\r\t]+([ ]{4})+/g, '>')
  26784. .replace(/[\n\r\t]+([ ]{4})+</g, '<')
  26785. .replace(/>[\n\r\t]+</g, '><');
  26786. holder.className && (newDiv.className = holder.className);
  26787. holder.style.cssText && (newDiv.style.cssText = holder.style.cssText);
  26788. if (/textarea/i.test(holder.tagName)) {
  26789. editor.textarea = holder;
  26790. editor.textarea.style.display = 'none';
  26791. } else {
  26792. holder.parentNode.removeChild(holder);
  26793. }
  26794. if(holder.id){
  26795. newDiv.id = holder.id;
  26796. domUtils.removeAttributes(holder,'id');
  26797. }
  26798. holder = newDiv;
  26799. holder.innerHTML = '';
  26800. }
  26801. }
  26802. domUtils.addClass(holder, "edui-" + editor.options.theme);
  26803. editor.ui.render(holder);
  26804. var opt = editor.options;
  26805. //给实例添加一个编辑器的容器引用
  26806. editor.container = editor.ui.getDom();
  26807. var parents = domUtils.findParents(holder,true);
  26808. var displays = [];
  26809. for(var i = 0 ,ci;ci=parents[i];i++){
  26810. displays[i] = ci.style.display;
  26811. ci.style.display = 'block'
  26812. }
  26813. if (opt.initialFrameWidth) {
  26814. opt.minFrameWidth = opt.initialFrameWidth;
  26815. } else {
  26816. opt.minFrameWidth = opt.initialFrameWidth = holder.offsetWidth;
  26817. var styleWidth = holder.style.width;
  26818. if(/%$/.test(styleWidth)) {
  26819. opt.initialFrameWidth = styleWidth;
  26820. }
  26821. }
  26822. if (opt.initialFrameHeight) {
  26823. opt.minFrameHeight = opt.initialFrameHeight;
  26824. } else {
  26825. opt.initialFrameHeight = opt.minFrameHeight = holder.offsetHeight;
  26826. }
  26827. for(var i = 0 ,ci;ci=parents[i];i++){
  26828. ci.style.display = displays[i]
  26829. }
  26830. //编辑器最外容器设置了高度,会导致,编辑器不占位
  26831. //todo 先去掉,没有找到原因
  26832. if(holder.style.height){
  26833. holder.style.height = ''
  26834. }
  26835. editor.container.style.width = opt.initialFrameWidth + (/%$/.test(opt.initialFrameWidth) ? '' : 'px');
  26836. editor.container.style.zIndex = opt.zIndex;
  26837. oldRender.call(editor, editor.ui.getDom('iframeholder'));
  26838. editor.fireEvent("afteruiready");
  26839. }
  26840. })
  26841. };
  26842. return editor;
  26843. };
  26844. /**
  26845. * @file
  26846. * @name UE
  26847. * @short UE
  26848. * @desc UEditor的顶部命名空间
  26849. */
  26850. /**
  26851. * @name getEditor
  26852. * @since 1.2.4+
  26853. * @grammar UE.getEditor(id,[opt]) => Editor实例
  26854. * @desc 提供一个全局的方法得到编辑器实例
  26855. *
  26856. * * ''id'' 放置编辑器的容器id, 如果容器下的编辑器已经存在,就直接返回
  26857. * * ''opt'' 编辑器的可选参数
  26858. * @example
  26859. * UE.getEditor('containerId',{onready:function(){//创建一个编辑器实例
  26860. * this.setContent('hello')
  26861. * }});
  26862. * UE.getEditor('containerId'); //返回刚创建的实例
  26863. *
  26864. */
  26865. UE.getEditor = function (id, opt) {
  26866. var editor = instances[id];
  26867. if (!editor) {
  26868. editor = instances[id] = new UE.ui.Editor(opt);
  26869. editor.render(id);
  26870. }
  26871. return editor;
  26872. };
  26873. UE.delEditor = function (id) {
  26874. var editor;
  26875. if (editor = instances[id]) {
  26876. editor.key && editor.destroy();
  26877. delete instances[id]
  26878. }
  26879. };
  26880. UE.registerUI = function(uiName,fn,index,editorId){
  26881. utils.each(uiName.split(/\s+/), function (name) {
  26882. UE._customizeUI[name] = {
  26883. id : editorId,
  26884. execFn:fn,
  26885. index:index
  26886. };
  26887. })
  26888. }
  26889. })();
  26890. // adapter/message.js
  26891. UE.registerUI('message', function(editor) {
  26892. var editorui = baidu.editor.ui;
  26893. var Message = editorui.Message;
  26894. var holder;
  26895. var _messageItems = [];
  26896. var me = editor;
  26897. me.addListener('ready', function(){
  26898. holder = document.getElementById(me.ui.id + '_message_holder');
  26899. updateHolderPos();
  26900. setTimeout(function(){
  26901. updateHolderPos();
  26902. }, 500);
  26903. });
  26904. me.addListener('showmessage', function(type, opt){
  26905. opt = utils.isString(opt) ? {
  26906. 'content': opt
  26907. } : opt;
  26908. var message = new Message({
  26909. 'timeout': opt.timeout,
  26910. 'type': opt.type,
  26911. 'content': opt.content,
  26912. 'keepshow': opt.keepshow,
  26913. 'editor': me
  26914. }),
  26915. mid = opt.id || ('msg_' + (+new Date()).toString(36));
  26916. message.render(holder);
  26917. _messageItems[mid] = message;
  26918. message.reset(opt);
  26919. updateHolderPos();
  26920. return mid;
  26921. });
  26922. me.addListener('updatemessage',function(type, id, opt){
  26923. opt = utils.isString(opt) ? {
  26924. 'content': opt
  26925. } : opt;
  26926. var message = _messageItems[id];
  26927. message.render(holder);
  26928. message && message.reset(opt);
  26929. });
  26930. me.addListener('hidemessage',function(type, id){
  26931. var message = _messageItems[id];
  26932. message && message.hide();
  26933. });
  26934. function updateHolderPos(){
  26935. var toolbarbox = me.ui.getDom('toolbarbox');
  26936. if (toolbarbox) {
  26937. holder.style.top = toolbarbox.offsetHeight + 3 + 'px';
  26938. }
  26939. holder.style.zIndex = Math.max(me.options.zIndex, me.iframe.style.zIndex) + 1;
  26940. }
  26941. });
  26942. // adapter/autosave.js
  26943. UE.registerUI('autosave', function(editor) {
  26944. var timer = null,uid = null;
  26945. editor.on('afterautosave',function(){
  26946. clearTimeout(timer);
  26947. timer = setTimeout(function(){
  26948. if(uid){
  26949. editor.trigger('hidemessage',uid);
  26950. }
  26951. uid = editor.trigger('showmessage',{
  26952. content : editor.getLang('autosave.success'),
  26953. timeout : 2000
  26954. });
  26955. },2000)
  26956. })
  26957. });
  26958. })();