ueditor.all.js 985 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355
  1. /*!
  2. * UEditor
  3. * version: ueditor
  4. * build: Wed Dec 26 2018 17:25:05 GMT+0800 (CST)
  5. */
  6. (function () {
  7. // editor.js
  8. UEDITOR_CONFIG = window.UEDITOR_CONFIG || {};
  9. var baidu = window.baidu || {};
  10. window.baidu = baidu;
  11. window.UE = baidu.editor = window.UE || {};
  12. UE.plugins = {};
  13. UE.commands = {};
  14. UE.instants = {};
  15. UE.I18N = {};
  16. UE._customizeUI = {};
  17. UE.version = "1.4.3";
  18. var dom = UE.dom = {};
  19. // core/browser.js
  20. /**
  21. * 浏览器判断模块
  22. * @file
  23. * @module UE.browser
  24. * @since 1.2.6.1
  25. */
  26. /**
  27. * 提供浏览器检测的模块
  28. * @unfile
  29. * @module UE.browser
  30. */
  31. var browser = UE.browser = function () {
  32. var agent = navigator.userAgent.toLowerCase(),
  33. opera = window.opera,
  34. browser = {
  35. /**
  36. * @property {boolean} ie 检测当前浏览器是否为IE
  37. * @example
  38. * ```javascript
  39. * if ( UE.browser.ie ) {
  40. * console.log( '当前浏览器是IE' );
  41. * }
  42. * ```
  43. */
  44. ie: /(msie\s|trident.*rv:)([\w.]+)/.test(agent),
  45. /**
  46. * @property {boolean} opera 检测当前浏览器是否为Opera
  47. * @example
  48. * ```javascript
  49. * if ( UE.browser.opera ) {
  50. * console.log( '当前浏览器是Opera' );
  51. * }
  52. * ```
  53. */
  54. opera: (!!opera && opera.version),
  55. /**
  56. * @property {boolean} webkit 检测当前浏览器是否是webkit内核的浏览器
  57. * @example
  58. * ```javascript
  59. * if ( UE.browser.webkit ) {
  60. * console.log( '当前浏览器是webkit内核浏览器' );
  61. * }
  62. * ```
  63. */
  64. webkit: (agent.indexOf(' applewebkit/') > -1),
  65. /**
  66. * @property {boolean} mac 检测当前浏览器是否是运行在mac平台下
  67. * @example
  68. * ```javascript
  69. * if ( UE.browser.mac ) {
  70. * console.log( '当前浏览器运行在mac平台下' );
  71. * }
  72. * ```
  73. */
  74. mac: (agent.indexOf('macintosh') > -1),
  75. /**
  76. * @property {boolean} quirks 检测当前浏览器是否处于“怪异模式”下
  77. * @example
  78. * ```javascript
  79. * if ( UE.browser.quirks ) {
  80. * console.log( '当前浏览器运行处于“怪异模式”' );
  81. * }
  82. * ```
  83. */
  84. quirks: (document.compatMode == 'BackCompat')
  85. };
  86. /**
  87. * @property {boolean} gecko 检测当前浏览器内核是否是gecko内核
  88. * @example
  89. * ```javascript
  90. * if ( UE.browser.gecko ) {
  91. * console.log( '当前浏览器内核是gecko内核' );
  92. * }
  93. * ```
  94. */
  95. browser.gecko = (navigator.product == 'Gecko' && !browser.webkit && !browser.opera && !browser.ie);
  96. var version = 0;
  97. // Internet Explorer 6.0+
  98. if (browser.ie) {
  99. var v1 = agent.match(/(?:msie\s([\w.]+))/);
  100. var v2 = agent.match(/(?:trident.*rv:([\w.]+))/);
  101. if (v1 && v2 && v1[1] && v2[1]) {
  102. version = Math.max(v1[1] * 1, v2[1] * 1);
  103. } else if (v1 && v1[1]) {
  104. version = v1[1] * 1;
  105. } else if (v2 && v2[1]) {
  106. version = v2[1] * 1;
  107. } else {
  108. version = 0;
  109. }
  110. browser.ie11Compat = document.documentMode == 11;
  111. /**
  112. * @property { boolean } ie9Compat 检测浏览器模式是否为 IE9 兼容模式
  113. * @warning 如果浏览器不是IE, 则该值为undefined
  114. * @example
  115. * ```javascript
  116. * if ( UE.browser.ie9Compat ) {
  117. * console.log( '当前浏览器运行在IE9兼容模式下' );
  118. * }
  119. * ```
  120. */
  121. browser.ie9Compat = document.documentMode == 9;
  122. /**
  123. * @property { boolean } ie8 检测浏览器是否是IE8浏览器
  124. * @warning 如果浏览器不是IE, 则该值为undefined
  125. * @example
  126. * ```javascript
  127. * if ( UE.browser.ie8 ) {
  128. * console.log( '当前浏览器是IE8浏览器' );
  129. * }
  130. * ```
  131. */
  132. browser.ie8 = !!document.documentMode;
  133. /**
  134. * @property { boolean } ie8Compat 检测浏览器模式是否为 IE8 兼容模式
  135. * @warning 如果浏览器不是IE, 则该值为undefined
  136. * @example
  137. * ```javascript
  138. * if ( UE.browser.ie8Compat ) {
  139. * console.log( '当前浏览器运行在IE8兼容模式下' );
  140. * }
  141. * ```
  142. */
  143. browser.ie8Compat = document.documentMode == 8;
  144. /**
  145. * @property { boolean } ie7Compat 检测浏览器模式是否为 IE7 兼容模式
  146. * @warning 如果浏览器不是IE, 则该值为undefined
  147. * @example
  148. * ```javascript
  149. * if ( UE.browser.ie7Compat ) {
  150. * console.log( '当前浏览器运行在IE7兼容模式下' );
  151. * }
  152. * ```
  153. */
  154. browser.ie7Compat = ((version == 7 && !document.documentMode) ||
  155. document.documentMode == 7);
  156. /**
  157. * @property { boolean } ie6Compat 检测浏览器模式是否为 IE6 模式 或者怪异模式
  158. * @warning 如果浏览器不是IE, 则该值为undefined
  159. * @example
  160. * ```javascript
  161. * if ( UE.browser.ie6Compat ) {
  162. * console.log( '当前浏览器运行在IE6模式或者怪异模式下' );
  163. * }
  164. * ```
  165. */
  166. browser.ie6Compat = (version < 7 || browser.quirks);
  167. browser.ie9above = version > 8;
  168. browser.ie9below = version < 9;
  169. browser.ie11above = version > 10;
  170. browser.ie11below = version < 11;
  171. }
  172. // Gecko.
  173. if (browser.gecko) {
  174. var geckoRelease = agent.match(/rv:([\d\.]+)/);
  175. if (geckoRelease) {
  176. geckoRelease = geckoRelease[1].split('.');
  177. version = geckoRelease[0] * 10000 + (geckoRelease[1] || 0) * 100 + (geckoRelease[2] || 0) * 1;
  178. }
  179. }
  180. /**
  181. * @property { Number } chrome 检测当前浏览器是否为Chrome, 如果是,则返回Chrome的大版本号
  182. * @warning 如果浏览器不是chrome, 则该值为undefined
  183. * @example
  184. * ```javascript
  185. * if ( UE.browser.chrome ) {
  186. * console.log( '当前浏览器是Chrome' );
  187. * }
  188. * ```
  189. */
  190. if (/chrome\/(\d+\.\d)/i.test(agent)) {
  191. browser.chrome = +RegExp['\x241'];
  192. }
  193. /**
  194. * @property { Number } safari 检测当前浏览器是否为Safari, 如果是,则返回Safari的大版本号
  195. * @warning 如果浏览器不是safari, 则该值为undefined
  196. * @example
  197. * ```javascript
  198. * if ( UE.browser.safari ) {
  199. * console.log( '当前浏览器是Safari' );
  200. * }
  201. * ```
  202. */
  203. if (/(\d+\.\d)?(?:\.\d)?\s+safari\/?(\d+\.\d+)?/i.test(agent) && !/chrome/i.test(agent)) {
  204. browser.safari = +(RegExp['\x241'] || RegExp['\x242']);
  205. }
  206. // Opera 9.50+
  207. if (browser.opera)
  208. version = parseFloat(opera.version());
  209. // WebKit 522+ (Safari 3+)
  210. if (browser.webkit)
  211. version = parseFloat(agent.match(/ applewebkit\/(\d+)/)[1]);
  212. /**
  213. * @property { Number } version 检测当前浏览器版本号
  214. * @remind
  215. * <ul>
  216. * <li>IE系列返回值为5,6,7,8,9,10等</li>
  217. * <li>gecko系列会返回10900,158900等</li>
  218. * <li>webkit系列会返回其build号 (如 522等)</li>
  219. * </ul>
  220. * @example
  221. * ```javascript
  222. * console.log( '当前浏览器版本号是: ' + UE.browser.version );
  223. * ```
  224. */
  225. browser.version = version;
  226. /**
  227. * @property { boolean } isCompatible 检测当前浏览器是否能够与UEditor良好兼容
  228. * @example
  229. * ```javascript
  230. * if ( UE.browser.isCompatible ) {
  231. * console.log( '浏览器与UEditor能够良好兼容' );
  232. * }
  233. * ```
  234. */
  235. browser.isCompatible = !browser.mobile && (
  236. (browser.ie && version >= 6) ||
  237. (browser.gecko && version >= 10801) ||
  238. (browser.opera && version >= 9.5) ||
  239. (browser.air && version >= 1) ||
  240. (browser.webkit && version >= 522) ||
  241. false);
  242. return browser;
  243. }();
  244. //快捷方式
  245. var ie = browser.ie,
  246. webkit = browser.webkit,
  247. gecko = browser.gecko,
  248. opera = browser.opera;
  249. // core/utils.js
  250. /**
  251. * 工具函数包
  252. * @file
  253. * @module UE.utils
  254. * @since 1.2.6.1
  255. */
  256. /**
  257. * UEditor封装使用的静态工具函数
  258. * @module UE.utils
  259. * @unfile
  260. */
  261. var utils = UE.utils = {
  262. /**
  263. * 用给定的迭代器遍历对象
  264. * @method each
  265. * @param { Object } obj 需要遍历的对象
  266. * @param { Function } iterator 迭代器, 该方法接受两个参数, 第一个参数是当前所处理的value, 第二个参数是当前遍历对象的key
  267. * @example
  268. * ```javascript
  269. * var demoObj = {
  270. * key1: 1,
  271. * key2: 2
  272. * };
  273. *
  274. * //output: key1: 1, key2: 2
  275. * UE.utils.each( demoObj, funciton ( value, key ) {
  276. *
  277. * console.log( key + ":" + value );
  278. *
  279. * } );
  280. * ```
  281. */
  282. /**
  283. * 用给定的迭代器遍历数组或类数组对象
  284. * @method each
  285. * @param { Array } array 需要遍历的数组或者类数组
  286. * @param { Function } iterator 迭代器, 该方法接受两个参数, 第一个参数是当前所处理的value, 第二个参数是当前遍历对象的key
  287. * @example
  288. * ```javascript
  289. * var divs = document.getElmentByTagNames( "div" );
  290. *
  291. * //output: 0: DIV, 1: DIV ...
  292. * UE.utils.each( divs, funciton ( value, key ) {
  293. *
  294. * console.log( key + ":" + value.tagName );
  295. *
  296. * } );
  297. * ```
  298. */
  299. each: function (obj, iterator, context) {
  300. if (obj == null) return;
  301. if (obj.length === +obj.length) {
  302. for (var i = 0, l = obj.length; i < l; i++) {
  303. if (iterator.call(context, obj[i], i, obj) === false)
  304. return false;
  305. }
  306. } else {
  307. for (var key in obj) {
  308. if (obj.hasOwnProperty(key)) {
  309. if (iterator.call(context, obj[key], key, obj) === false)
  310. return false;
  311. }
  312. }
  313. }
  314. },
  315. /**
  316. * 以给定对象作为原型创建一个新对象
  317. * @method makeInstance
  318. * @param { Object } protoObject 该对象将作为新创建对象的原型
  319. * @return { Object } 新的对象, 该对象的原型是给定的protoObject对象
  320. * @example
  321. * ```javascript
  322. *
  323. * var protoObject = { sayHello: function () { console.log('Hello UEditor!'); } };
  324. *
  325. * var newObject = UE.utils.makeInstance( protoObject );
  326. * //output: Hello UEditor!
  327. * newObject.sayHello();
  328. * ```
  329. */
  330. makeInstance: function (obj) {
  331. var noop = new Function();
  332. noop.prototype = obj;
  333. obj = new noop;
  334. noop.prototype = null;
  335. return obj;
  336. },
  337. /**
  338. * 将source对象中的属性扩展到target对象上
  339. * @method extend
  340. * @remind 该方法将强制把source对象上的属性复制到target对象上
  341. * @see UE.utils.extend(Object,Object,Boolean)
  342. * @param { Object } target 目标对象, 新的属性将附加到该对象上
  343. * @param { Object } source 源对象, 该对象的属性会被附加到target对象上
  344. * @return { Object } 返回target对象
  345. * @example
  346. * ```javascript
  347. *
  348. * var target = { name: 'target', sex: 1 },
  349. * source = { name: 'source', age: 17 };
  350. *
  351. * UE.utils.extend( target, source );
  352. *
  353. * //output: { name: 'source', sex: 1, age: 17 }
  354. * console.log( target );
  355. *
  356. * ```
  357. */
  358. /**
  359. * 将source对象中的属性扩展到target对象上, 根据指定的isKeepTarget值决定是否保留目标对象中与
  360. * 源对象属性名相同的属性值。
  361. * @method extend
  362. * @param { Object } target 目标对象, 新的属性将附加到该对象上
  363. * @param { Object } source 源对象, 该对象的属性会被附加到target对象上
  364. * @param { Boolean } isKeepTarget 是否保留目标对象中与源对象中属性名相同的属性
  365. * @return { Object } 返回target对象
  366. * @example
  367. * ```javascript
  368. *
  369. * var target = { name: 'target', sex: 1 },
  370. * source = { name: 'source', age: 17 };
  371. *
  372. * UE.utils.extend( target, source, true );
  373. *
  374. * //output: { name: 'target', sex: 1, age: 17 }
  375. * console.log( target );
  376. *
  377. * ```
  378. */
  379. extend: function (t, s, b) {
  380. if (s) {
  381. for (var k in s) {
  382. if (!b || !t.hasOwnProperty(k)) {
  383. t[k] = s[k];
  384. }
  385. }
  386. }
  387. return t;
  388. },
  389. /**
  390. * 将给定的多个对象的属性复制到目标对象target上
  391. * @method extend2
  392. * @remind 该方法将强制把源对象上的属性复制到target对象上
  393. * @remind 该方法支持两个及以上的参数, 从第二个参数开始, 其属性都会被复制到第一个参数上。 如果遇到同名的属性,
  394. * 将会覆盖掉之前的值。
  395. * @param { Object } target 目标对象, 新的属性将附加到该对象上
  396. * @param { Object... } source 源对象, 支持多个对象, 该对象的属性会被附加到target对象上
  397. * @return { Object } 返回target对象
  398. * @example
  399. * ```javascript
  400. *
  401. * var target = {},
  402. * source1 = { name: 'source', age: 17 },
  403. * source2 = { title: 'dev' };
  404. *
  405. * UE.utils.extend2( target, source1, source2 );
  406. *
  407. * //output: { name: 'source', age: 17, title: 'dev' }
  408. * console.log( target );
  409. *
  410. * ```
  411. */
  412. extend2: function (t) {
  413. var a = arguments;
  414. for (var i = 1; i < a.length; i++) {
  415. var x = a[i];
  416. for (var k in x) {
  417. if (!t.hasOwnProperty(k)) {
  418. t[k] = x[k];
  419. }
  420. }
  421. }
  422. return t;
  423. },
  424. /**
  425. * 模拟继承机制, 使得subClass继承自superClass
  426. * @method inherits
  427. * @param { Object } subClass 子类对象
  428. * @param { Object } superClass 超类对象
  429. * @warning 该方法只能让subClass继承超类的原型, subClass对象自身的属性和方法不会被继承
  430. * @return { Object } 继承superClass后的子类对象
  431. * @example
  432. * ```javascript
  433. * function SuperClass(){
  434. * this.name = "小李";
  435. * }
  436. *
  437. * SuperClass.prototype = {
  438. * hello:function(str){
  439. * console.log(this.name + str);
  440. * }
  441. * }
  442. *
  443. * function SubClass(){
  444. * this.name = "小张";
  445. * }
  446. *
  447. * UE.utils.inherits(SubClass,SuperClass);
  448. *
  449. * var sub = new SubClass();
  450. * //output: '小张早上好!
  451. * sub.hello("早上好!");
  452. * ```
  453. */
  454. inherits: function (subClass, superClass) {
  455. var oldP = subClass.prototype,
  456. newP = utils.makeInstance(superClass.prototype);
  457. utils.extend(newP, oldP, true);
  458. subClass.prototype = newP;
  459. return (newP.constructor = subClass);
  460. },
  461. /**
  462. * 用指定的context对象作为函数fn的上下文
  463. * @method bind
  464. * @param { Function } fn 需要绑定上下文的函数对象
  465. * @param { Object } content 函数fn新的上下文对象
  466. * @return { Function } 一个新的函数, 该函数作为原始函数fn的代理, 将完成fn的上下文调换工作。
  467. * @example
  468. * ```javascript
  469. *
  470. * var name = 'window',
  471. * newTest = null;
  472. *
  473. * function test () {
  474. * console.log( this.name );
  475. * }
  476. *
  477. * newTest = UE.utils.bind( test, { name: 'object' } );
  478. *
  479. * //output: object
  480. * newTest();
  481. *
  482. * //output: window
  483. * test();
  484. *
  485. * ```
  486. */
  487. bind: function (fn, context) {
  488. return function () {
  489. return fn.apply(context, arguments);
  490. };
  491. },
  492. /**
  493. * 创建延迟指定时间后执行的函数fn
  494. * @method defer
  495. * @param { Function } fn 需要延迟执行的函数对象
  496. * @param { int } delay 延迟的时间, 单位是毫秒
  497. * @warning 该方法的时间控制是不精确的,仅仅只能保证函数的执行是在给定的时间之后,
  498. * 而不能保证刚好到达延迟时间时执行。
  499. * @return { Function } 目标函数fn的代理函数, 只有执行该函数才能起到延时效果
  500. * @example
  501. * ```javascript
  502. * var start = 0;
  503. *
  504. * function test(){
  505. * console.log( new Date() - start );
  506. * }
  507. *
  508. * var testDefer = UE.utils.defer( test, 1000 );
  509. * //
  510. * start = new Date();
  511. * //output: (大约在1000毫秒之后输出) 1000
  512. * testDefer();
  513. * ```
  514. */
  515. /**
  516. * 创建延迟指定时间后执行的函数fn, 如果在延迟时间内再次执行该方法, 将会根据指定的exclusion的值,
  517. * 决定是否取消前一次函数的执行, 如果exclusion的值为true, 则取消执行,反之,将继续执行前一个方法。
  518. * @method defer
  519. * @param { Function } fn 需要延迟执行的函数对象
  520. * @param { int } delay 延迟的时间, 单位是毫秒
  521. * @param { Boolean } exclusion 如果在延迟时间内再次执行该函数,该值将决定是否取消执行前一次函数的执行,
  522. * 值为true表示取消执行, 反之则将在执行前一次函数之后才执行本次函数调用。
  523. * @warning 该方法的时间控制是不精确的,仅仅只能保证函数的执行是在给定的时间之后,
  524. * 而不能保证刚好到达延迟时间时执行。
  525. * @return { Function } 目标函数fn的代理函数, 只有执行该函数才能起到延时效果
  526. * @example
  527. * ```javascript
  528. *
  529. * function test(){
  530. * console.log(1);
  531. * }
  532. *
  533. * var testDefer = UE.utils.defer( test, 1000, true );
  534. *
  535. * //output: (两次调用仅有一次输出) 1
  536. * testDefer();
  537. * testDefer();
  538. * ```
  539. */
  540. defer: function (fn, delay, exclusion) {
  541. var timerID;
  542. return function () {
  543. if (exclusion) {
  544. clearTimeout(timerID);
  545. }
  546. timerID = setTimeout(fn, delay);
  547. };
  548. },
  549. /**
  550. * 获取元素item在数组array中首次出现的位置, 如果未找到item, 则返回-1
  551. * @method indexOf
  552. * @remind 该方法的匹配过程使用的是恒等“===”
  553. * @param { Array } array 需要查找的数组对象
  554. * @param { * } item 需要在目标数组中查找的值
  555. * @return { int } 返回item在目标数组array中首次出现的位置, 如果在数组中未找到item, 则返回-1
  556. * @example
  557. * ```javascript
  558. * var item = 1,
  559. * arr = [ 3, 4, 6, 8, 1, 1, 2 ];
  560. *
  561. * //output: 4
  562. * console.log( UE.utils.indexOf( arr, item ) );
  563. * ```
  564. */
  565. /**
  566. * 获取元素item数组array中首次出现的位置, 如果未找到item, 则返回-1。通过start的值可以指定搜索的起始位置。
  567. * @method indexOf
  568. * @remind 该方法的匹配过程使用的是恒等“===”
  569. * @param { Array } array 需要查找的数组对象
  570. * @param { * } item 需要在目标数组中查找的值
  571. * @param { int } start 搜索的起始位置
  572. * @return { int } 返回item在目标数组array中的start位置之后首次出现的位置, 如果在数组中未找到item, 则返回-1
  573. * @example
  574. * ```javascript
  575. * var item = 1,
  576. * arr = [ 3, 4, 6, 8, 1, 2, 8, 3, 2, 1, 1, 4 ];
  577. *
  578. * //output: 9
  579. * console.log( UE.utils.indexOf( arr, item, 5 ) );
  580. * ```
  581. */
  582. indexOf: function (array, item, start) {
  583. var index = -1;
  584. start = this.isNumber(start) ? start : 0;
  585. this.each(array, function (v, i) {
  586. if (i >= start && v === item) {
  587. index = i;
  588. return false;
  589. }
  590. });
  591. return index;
  592. },
  593. /**
  594. * 移除数组array中所有的元素item
  595. * @method removeItem
  596. * @param { Array } array 要移除元素的目标数组
  597. * @param { * } item 将要被移除的元素
  598. * @remind 该方法的匹配过程使用的是恒等“===”
  599. * @example
  600. * ```javascript
  601. * var arr = [ 4, 5, 7, 1, 3, 4, 6 ];
  602. *
  603. * UE.utils.removeItem( arr, 4 );
  604. * //output: [ 5, 7, 1, 3, 6 ]
  605. * console.log( arr );
  606. *
  607. * ```
  608. */
  609. removeItem: function (array, item) {
  610. for (var i = 0, l = array.length; i < l; i++) {
  611. if (array[i] === item) {
  612. array.splice(i, 1);
  613. i--;
  614. }
  615. }
  616. },
  617. /**
  618. * 删除字符串str的首尾空格
  619. * @method trim
  620. * @param { String } str 需要删除首尾空格的字符串
  621. * @return { String } 删除了首尾的空格后的字符串
  622. * @example
  623. * ```javascript
  624. *
  625. * var str = " UEdtior ";
  626. *
  627. * //output: 9
  628. * console.log( str.length );
  629. *
  630. * //output: 7
  631. * console.log( UE.utils.trim( " UEdtior " ).length );
  632. *
  633. * //output: 9
  634. * console.log( str.length );
  635. *
  636. * ```
  637. */
  638. trim: function (str) {
  639. return str.replace(/(^[ \t\n\r]+)|([ \t\n\r]+$)/g, '');
  640. },
  641. /**
  642. * 将字符串str以','分隔成数组后,将该数组转换成哈希对象, 其生成的hash对象的key为数组中的元素, value为1
  643. * @method listToMap
  644. * @warning 该方法在生成的hash对象中,会为每一个key同时生成一个另一个全大写的key。
  645. * @param { String } str 该字符串将被以','分割为数组, 然后进行转化
  646. * @return { Object } 转化之后的hash对象
  647. * @example
  648. * ```javascript
  649. *
  650. * //output: Object {UEdtior: 1, UEDTIOR: 1, Hello: 1, HELLO: 1}
  651. * console.log( UE.utils.listToMap( 'UEdtior,Hello' ) );
  652. *
  653. * ```
  654. */
  655. /**
  656. * 将字符串数组转换成哈希对象, 其生成的hash对象的key为数组中的元素, value为1
  657. * @method listToMap
  658. * @warning 该方法在生成的hash对象中,会为每一个key同时生成一个另一个全大写的key。
  659. * @param { Array } arr 字符串数组
  660. * @return { Object } 转化之后的hash对象
  661. * @example
  662. * ```javascript
  663. *
  664. * //output: Object {UEdtior: 1, UEDTIOR: 1, Hello: 1, HELLO: 1}
  665. * console.log( UE.utils.listToMap( [ 'UEdtior', 'Hello' ] ) );
  666. *
  667. * ```
  668. */
  669. listToMap: function (list) {
  670. if (!list) return {};
  671. list = utils.isArray(list) ? list : list.split(',');
  672. for (var i = 0, ci, obj = {}; ci = list[i++];) {
  673. obj[ci.toUpperCase()] = obj[ci] = 1;
  674. }
  675. return obj;
  676. },
  677. /**
  678. * 将str中的html符号转义,将转义“',&,<,",>”五个字符
  679. * @method unhtml
  680. * @param { String } str 需要转义的字符串
  681. * @return { String } 转义后的字符串
  682. * @example
  683. * ```javascript
  684. * var html = '<body>&</body>';
  685. *
  686. * //output: &lt;body&gt;&amp;&lt;/body&gt;
  687. * console.log( UE.utils.unhtml( html ) );
  688. *
  689. * ```
  690. */
  691. unhtml: function (str, reg) {
  692. return str ? str.replace(reg || /[&<">'](?:(amp|lt|quot|gt|#39|nbsp|#\d+);)?/g, function (a, b) {
  693. if (b) {
  694. return a;
  695. } else {
  696. return {
  697. '<': '&lt;',
  698. '&': '&amp;',
  699. '"': '&quot;',
  700. '>': '&gt;',
  701. "'": '&#39;'
  702. } [a]
  703. }
  704. }) : '';
  705. },
  706. /**
  707. * 将url中的html字符转义, 仅转义 ', ", <, > 四个字符
  708. * @param { String } str 需要转义的字符串
  709. * @param { RegExp } reg 自定义的正则
  710. * @return { String } 转义后的字符串
  711. */
  712. unhtmlForUrl: function (str, reg) {
  713. return str ? str.replace(reg || /[<">']/g, function (a) {
  714. return {
  715. '<': '&lt;',
  716. '&': '&amp;',
  717. '"': '&quot;',
  718. '>': '&gt;',
  719. "'": '&#39;'
  720. } [a]
  721. }) : '';
  722. },
  723. /**
  724. * 将str中的转义字符还原成html字符
  725. * @see UE.utils.unhtml(String);
  726. * @method html
  727. * @param { String } str 需要逆转义的字符串
  728. * @return { String } 逆转义后的字符串
  729. * @example
  730. * ```javascript
  731. *
  732. * var str = '&lt;body&gt;&amp;&lt;/body&gt;';
  733. *
  734. * //output: <body>&</body>
  735. * console.log( UE.utils.html( str ) );
  736. *
  737. * ```
  738. */
  739. html: function (str) {
  740. return str ? str.replace(/&((g|l|quo)t|amp|#39|nbsp);/g, function (m) {
  741. return {
  742. '&lt;': '<',
  743. '&amp;': '&',
  744. '&quot;': '"',
  745. '&gt;': '>',
  746. '&#39;': "'",
  747. '&nbsp;': ' '
  748. } [m]
  749. }) : '';
  750. },
  751. /**
  752. * 将css样式转换为驼峰的形式
  753. * @method cssStyleToDomStyle
  754. * @param { String } cssName 需要转换的css样式名
  755. * @return { String } 转换成驼峰形式后的css样式名
  756. * @example
  757. * ```javascript
  758. *
  759. * var str = 'border-top';
  760. *
  761. * //output: borderTop
  762. * console.log( UE.utils.cssStyleToDomStyle( str ) );
  763. *
  764. * ```
  765. */
  766. cssStyleToDomStyle: function () {
  767. var test = document.createElement('div').style,
  768. cache = {
  769. 'float': test.cssFloat != undefined ? 'cssFloat' : test.styleFloat != undefined ? 'styleFloat' : 'float'
  770. };
  771. return function (cssName) {
  772. return cache[cssName] || (cache[cssName] = cssName.toLowerCase().replace(/-./g, function (match) {
  773. return match.charAt(1).toUpperCase();
  774. }));
  775. };
  776. }(),
  777. /**
  778. * 动态加载文件到doc中
  779. * @method loadFile
  780. * @param { DomDocument } document 需要加载资源文件的文档对象
  781. * @param { Object } options 加载资源文件的属性集合, 取值请参考代码示例
  782. * @example
  783. * ```javascript
  784. *
  785. * UE.utils.loadFile( document, {
  786. * src:"test.js",
  787. * tag:"script",
  788. * type:"text/javascript",
  789. * defer:"defer"
  790. * } );
  791. *
  792. * ```
  793. */
  794. /**
  795. * 动态加载文件到doc中,加载成功后执行的回调函数fn
  796. * @method loadFile
  797. * @param { DomDocument } document 需要加载资源文件的文档对象
  798. * @param { Object } options 加载资源文件的属性集合, 该集合支持的值是script标签和style标签支持的所有属性。
  799. * @param { Function } fn 资源文件加载成功之后执行的回调
  800. * @warning 对于在同一个文档中多次加载同一URL的文件, 该方法会在第一次加载之后缓存该请求,
  801. * 在此之后的所有同一URL的请求, 将会直接触发回调。
  802. * @example
  803. * ```javascript
  804. *
  805. * UE.utils.loadFile( document, {
  806. * src:"test.js",
  807. * tag:"script",
  808. * type:"text/javascript",
  809. * defer:"defer"
  810. * }, function () {
  811. * console.log('加载成功');
  812. * } );
  813. *
  814. * ```
  815. */
  816. loadFile: function () {
  817. var tmpList = [];
  818. function getItem(doc, obj) {
  819. try {
  820. for (var i = 0, ci; ci = tmpList[i++];) {
  821. if (ci.doc === doc && ci.url == (obj.src || obj.href)) {
  822. return ci;
  823. }
  824. }
  825. } catch (e) {
  826. return null;
  827. }
  828. }
  829. return function (doc, obj, fn) {
  830. var item = getItem(doc, obj);
  831. if (item) {
  832. if (item.ready) {
  833. fn && fn();
  834. } else {
  835. item.funs.push(fn)
  836. }
  837. return;
  838. }
  839. tmpList.push({
  840. doc: doc,
  841. url: obj.src || obj.href,
  842. funs: [fn]
  843. });
  844. if (!doc.body) {
  845. var html = [];
  846. for (var p in obj) {
  847. if (p == 'tag') continue;
  848. html.push(p + '="' + obj[p] + '"')
  849. }
  850. doc.write('<' + obj.tag + ' ' + html.join(' ') + ' ></' + obj.tag + '>');
  851. return;
  852. }
  853. if (obj.id && doc.getElementById(obj.id)) {
  854. return;
  855. }
  856. var element = doc.createElement(obj.tag);
  857. delete obj.tag;
  858. for (var p in obj) {
  859. element.setAttribute(p, obj[p]);
  860. }
  861. element.onload = element.onreadystatechange = function () {
  862. if (!this.readyState || /loaded|complete/.test(this.readyState)) {
  863. item = getItem(doc, obj);
  864. if (item.funs.length > 0) {
  865. item.ready = 1;
  866. for (var fi; fi = item.funs.pop();) {
  867. fi();
  868. }
  869. }
  870. element.onload = element.onreadystatechange = null;
  871. }
  872. };
  873. element.onerror = function () {
  874. throw Error('The load ' + (obj.href || obj.src) + ' fails,check the url settings of file ueditor.config.js ')
  875. };
  876. doc.getElementsByTagName("head")[0].appendChild(element);
  877. }
  878. }(),
  879. /**
  880. * 判断obj对象是否为空
  881. * @method isEmptyObject
  882. * @param { * } obj 需要判断的对象
  883. * @remind 如果判断的对象是NULL, 将直接返回true, 如果是数组且为空, 返回true, 如果是字符串, 且字符串为空,
  884. * 返回true, 如果是普通对象, 且该对象没有任何实例属性, 返回true
  885. * @return { Boolean } 对象是否为空
  886. * @example
  887. * ```javascript
  888. *
  889. * //output: true
  890. * console.log( UE.utils.isEmptyObject( {} ) );
  891. *
  892. * //output: true
  893. * console.log( UE.utils.isEmptyObject( [] ) );
  894. *
  895. * //output: true
  896. * console.log( UE.utils.isEmptyObject( "" ) );
  897. *
  898. * //output: false
  899. * console.log( UE.utils.isEmptyObject( { key: 1 } ) );
  900. *
  901. * //output: false
  902. * console.log( UE.utils.isEmptyObject( [1] ) );
  903. *
  904. * //output: false
  905. * console.log( UE.utils.isEmptyObject( "1" ) );
  906. *
  907. * ```
  908. */
  909. isEmptyObject: function (obj) {
  910. if (obj == null) return true;
  911. if (this.isArray(obj) || this.isString(obj)) return obj.length === 0;
  912. for (var key in obj)
  913. if (obj.hasOwnProperty(key)) return false;
  914. return true;
  915. },
  916. /**
  917. * 把rgb格式的颜色值转换成16进制格式
  918. * @method fixColor
  919. * @param { String } rgb格式的颜色值
  920. * @param { String }
  921. * @example
  922. * rgb(255,255,255) => "#ffffff"
  923. */
  924. fixColor: function (name, value) {
  925. if (/color/i.test(name) && /rgba?/.test(value)) {
  926. var array = value.split(",");
  927. if (array.length > 3)
  928. return "";
  929. value = "#";
  930. for (var i = 0, color; color = array[i++];) {
  931. color = parseInt(color.replace(/[^\d]/gi, ''), 10).toString(16);
  932. value += color.length == 1 ? "0" + color : color;
  933. }
  934. value = value.toUpperCase();
  935. }
  936. return value;
  937. },
  938. /**
  939. * 只针对border,padding,margin做了处理,因为性能问题
  940. * @public
  941. * @function
  942. * @param {String} val style字符串
  943. */
  944. optCss: function (val) {
  945. var padding, margin, border;
  946. val = val.replace(/(padding|margin|border)\-([^:]+):([^;]+);?/gi, function (str, key, name, val) {
  947. if (val.split(' ').length == 1) {
  948. switch (key) {
  949. case 'padding':
  950. !padding && (padding = {});
  951. padding[name] = val;
  952. return '';
  953. case 'margin':
  954. !margin && (margin = {});
  955. margin[name] = val;
  956. return '';
  957. case 'border':
  958. return val == 'initial' ? '' : str;
  959. }
  960. }
  961. return str;
  962. });
  963. function opt(obj, name) {
  964. if (!obj) {
  965. return '';
  966. }
  967. var t = obj.top,
  968. b = obj.bottom,
  969. l = obj.left,
  970. r = obj.right,
  971. val = '';
  972. if (!t || !l || !b || !r) {
  973. for (var p in obj) {
  974. val += ';' + name + '-' + p + ':' + obj[p] + ';';
  975. }
  976. } else {
  977. val += ';' + name + ':' +
  978. (t == b && b == l && l == r ? t :
  979. t == b && l == r ? (t + ' ' + l) :
  980. l == r ? (t + ' ' + l + ' ' + b) : (t + ' ' + r + ' ' + b + ' ' + l)) + ';'
  981. }
  982. return val;
  983. }
  984. val += opt(padding, 'padding') + opt(margin, 'margin');
  985. return val.replace(/^[ \n\r\t;]*|[ \n\r\t]*$/, '').replace(/;([ \n\r\t]+)|\1;/g, ';')
  986. .replace(/(&((l|g)t|quot|#39))?;{2,}/g, function (a, b) {
  987. return b ? b + ";;" : ';'
  988. });
  989. },
  990. /**
  991. * 克隆对象
  992. * @method clone
  993. * @param { Object } source 源对象
  994. * @return { Object } source的一个副本
  995. */
  996. /**
  997. * 深度克隆对象,将source的属性克隆到target对象, 会覆盖target重名的属性。
  998. * @method clone
  999. * @param { Object } source 源对象
  1000. * @param { Object } target 目标对象
  1001. * @return { Object } 附加了source对象所有属性的target对象
  1002. */
  1003. clone: function (source, target) {
  1004. var tmp;
  1005. target = target || {};
  1006. for (var i in source) {
  1007. if (source.hasOwnProperty(i)) {
  1008. tmp = source[i];
  1009. if (typeof tmp == 'object') {
  1010. target[i] = utils.isArray(tmp) ? [] : {};
  1011. utils.clone(source[i], target[i])
  1012. } else {
  1013. target[i] = tmp;
  1014. }
  1015. }
  1016. }
  1017. return target;
  1018. },
  1019. /**
  1020. * 把cm/pt为单位的值转换为px为单位的值
  1021. * @method transUnitToPx
  1022. * @param { String } 待转换的带单位的字符串
  1023. * @return { String } 转换为px为计量单位的值的字符串
  1024. * @example
  1025. * ```javascript
  1026. *
  1027. * //output: 500px
  1028. * console.log( UE.utils.transUnitToPx( '20cm' ) );
  1029. *
  1030. * //output: 27px
  1031. * console.log( UE.utils.transUnitToPx( '20pt' ) );
  1032. *
  1033. * ```
  1034. */
  1035. transUnitToPx: function (val) {
  1036. if (!/(pt|cm)/.test(val)) {
  1037. return val
  1038. }
  1039. var unit;
  1040. val.replace(/([\d.]+)(\w+)/, function (str, v, u) {
  1041. val = v;
  1042. unit = u;
  1043. });
  1044. switch (unit) {
  1045. case 'cm':
  1046. val = parseFloat(val) * 25;
  1047. break;
  1048. case 'pt':
  1049. val = Math.round(parseFloat(val) * 96 / 72);
  1050. }
  1051. return val + (val ? 'px' : '');
  1052. },
  1053. /**
  1054. * 在dom树ready之后执行给定的回调函数
  1055. * @method domReady
  1056. * @remind 如果在执行该方法的时候, dom树已经ready, 那么回调函数将立刻执行
  1057. * @param { Function } fn dom树ready之后的回调函数
  1058. * @example
  1059. * ```javascript
  1060. *
  1061. * UE.utils.domReady( function () {
  1062. *
  1063. * console.log('123');
  1064. *
  1065. * } );
  1066. *
  1067. * ```
  1068. */
  1069. domReady: function () {
  1070. var fnArr = [];
  1071. function doReady(doc) {
  1072. //确保onready只执行一次
  1073. doc.isReady = true;
  1074. for (var ci; ci = fnArr.pop(); ci()) {}
  1075. }
  1076. return function (onready, win) {
  1077. win = win || window;
  1078. var doc = win.document;
  1079. onready && fnArr.push(onready);
  1080. if (doc.readyState === "complete") {
  1081. doReady(doc);
  1082. } else {
  1083. doc.isReady && doReady(doc);
  1084. if (browser.ie && browser.version != 11) {
  1085. (function () {
  1086. if (doc.isReady) return;
  1087. try {
  1088. doc.documentElement.doScroll("left");
  1089. } catch (error) {
  1090. setTimeout(arguments.callee, 0);
  1091. return;
  1092. }
  1093. doReady(doc);
  1094. })();
  1095. win.attachEvent('onload', function () {
  1096. doReady(doc)
  1097. });
  1098. } else {
  1099. doc.addEventListener("DOMContentLoaded", function () {
  1100. doc.removeEventListener("DOMContentLoaded", arguments.callee, false);
  1101. doReady(doc);
  1102. }, false);
  1103. win.addEventListener('load', function () {
  1104. doReady(doc)
  1105. }, false);
  1106. }
  1107. }
  1108. }
  1109. }(),
  1110. /**
  1111. * 动态添加css样式
  1112. * @method cssRule
  1113. * @param { String } 节点名称
  1114. * @grammar UE.utils.cssRule('添加的样式的节点名称',['样式','放到哪个document上'])
  1115. * @grammar UE.utils.cssRule('body','body{background:#ccc}') => null //给body添加背景颜色
  1116. * @grammar UE.utils.cssRule('body') =>样式的字符串 //取得key值为body的样式的内容,如果没有找到key值先关的样式将返回空,例如刚才那个背景颜色,将返回 body{background:#ccc}
  1117. * @grammar UE.utils.cssRule('body',document) => 返回指定key的样式,并且指定是哪个document
  1118. * @grammar UE.utils.cssRule('body','') =>null //清空给定的key值的背景颜色
  1119. */
  1120. cssRule: browser.ie && browser.version != 11 ? function (key, style, doc) {
  1121. var indexList, index;
  1122. if (style === undefined || style && style.nodeType && style.nodeType == 9) {
  1123. //获取样式
  1124. doc = style && style.nodeType && style.nodeType == 9 ? style : (doc || document);
  1125. indexList = doc.indexList || (doc.indexList = {});
  1126. index = indexList[key];
  1127. if (index !== undefined) {
  1128. return doc.styleSheets[index].cssText
  1129. }
  1130. return undefined;
  1131. }
  1132. doc = doc || document;
  1133. indexList = doc.indexList || (doc.indexList = {});
  1134. index = indexList[key];
  1135. //清除样式
  1136. if (style === '') {
  1137. if (index !== undefined) {
  1138. doc.styleSheets[index].cssText = '';
  1139. delete indexList[key];
  1140. return true
  1141. }
  1142. return false;
  1143. }
  1144. //添加样式
  1145. if (index !== undefined) {
  1146. sheetStyle = doc.styleSheets[index];
  1147. } else {
  1148. sheetStyle = doc.createStyleSheet('', index = doc.styleSheets.length);
  1149. indexList[key] = index;
  1150. }
  1151. sheetStyle.cssText = style;
  1152. } : function (key, style, doc) {
  1153. var head, node;
  1154. if (style === undefined || style && style.nodeType && style.nodeType == 9) {
  1155. //获取样式
  1156. doc = style && style.nodeType && style.nodeType == 9 ? style : (doc || document);
  1157. node = doc.getElementById(key);
  1158. return node ? node.innerHTML : undefined;
  1159. }
  1160. doc = doc || document;
  1161. node = doc.getElementById(key);
  1162. //清除样式
  1163. if (style === '') {
  1164. if (node) {
  1165. node.parentNode.removeChild(node);
  1166. return true
  1167. }
  1168. return false;
  1169. }
  1170. //添加样式
  1171. if (node) {
  1172. node.innerHTML = style;
  1173. } else {
  1174. node = doc.createElement('style');
  1175. node.id = key;
  1176. node.innerHTML = style;
  1177. doc.getElementsByTagName('head')[0].appendChild(node);
  1178. }
  1179. },
  1180. sort: function (array, compareFn) {
  1181. compareFn = compareFn || function (item1, item2) {
  1182. return item1.localeCompare(item2);
  1183. };
  1184. for (var i = 0, len = array.length; i < len; i++) {
  1185. for (var j = i, length = array.length; j < length; j++) {
  1186. if (compareFn(array[i], array[j]) > 0) {
  1187. var t = array[i];
  1188. array[i] = array[j];
  1189. array[j] = t;
  1190. }
  1191. }
  1192. }
  1193. return array;
  1194. },
  1195. serializeParam: function (json) {
  1196. var strArr = [];
  1197. for (var i in json) {
  1198. //忽略默认的几个参数
  1199. if (i == "method" || i == "timeout" || i == "async") continue;
  1200. //传递过来的对象和函数不在提交之列
  1201. if (!((typeof json[i]).toLowerCase() == "function" || (typeof json[i]).toLowerCase() == "object")) {
  1202. strArr.push(encodeURIComponent(i) + "=" + encodeURIComponent(json[i]));
  1203. } else if (utils.isArray(json[i])) {
  1204. //支持传数组内容
  1205. for (var j = 0; j < json[i].length; j++) {
  1206. strArr.push(encodeURIComponent(i) + "[]=" + encodeURIComponent(json[i][j]));
  1207. }
  1208. }
  1209. }
  1210. return strArr.join("&");
  1211. },
  1212. formatUrl: function (url) {
  1213. var u = url.replace(/&&/g, '&');
  1214. u = u.replace(/\?&/g, '?');
  1215. u = u.replace(/&$/g, '');
  1216. u = u.replace(/&#/g, '#');
  1217. u = u.replace(/&+/g, '&');
  1218. return u;
  1219. },
  1220. isCrossDomainUrl: function (url) {
  1221. var a = document.createElement('a');
  1222. a.href = url;
  1223. if (browser.ie) {
  1224. a.href = a.href;
  1225. }
  1226. return !(a.protocol == location.protocol && a.hostname == location.hostname &&
  1227. (a.port == location.port || (a.port == '80' && location.port == '') || (a.port == '' && location.port == '80')));
  1228. },
  1229. clearEmptyAttrs: function (obj) {
  1230. for (var p in obj) {
  1231. if (obj[p] === '') {
  1232. delete obj[p]
  1233. }
  1234. }
  1235. return obj;
  1236. },
  1237. str2json: function (s) {
  1238. if (!utils.isString(s)) return null;
  1239. if (window.JSON) {
  1240. return JSON.parse(s);
  1241. } else {
  1242. return (new Function("return " + utils.trim(s || '')))();
  1243. }
  1244. },
  1245. json2str: (function () {
  1246. if (window.JSON) {
  1247. return JSON.stringify;
  1248. } else {
  1249. var escapeMap = {
  1250. "\b": '\\b',
  1251. "\t": '\\t',
  1252. "\n": '\\n',
  1253. "\f": '\\f',
  1254. "\r": '\\r',
  1255. '"': '\\"',
  1256. "\\": '\\\\'
  1257. };
  1258. function encodeString(source) {
  1259. if (/["\\\x00-\x1f]/.test(source)) {
  1260. source = source.replace(
  1261. /["\\\x00-\x1f]/g,
  1262. function (match) {
  1263. var c = escapeMap[match];
  1264. if (c) {
  1265. return c;
  1266. }
  1267. c = match.charCodeAt();
  1268. return "\\u00" +
  1269. Math.floor(c / 16).toString(16) +
  1270. (c % 16).toString(16);
  1271. });
  1272. }
  1273. return '"' + source + '"';
  1274. }
  1275. function encodeArray(source) {
  1276. var result = ["["],
  1277. l = source.length,
  1278. preComma, i, item;
  1279. for (i = 0; i < l; i++) {
  1280. item = source[i];
  1281. switch (typeof item) {
  1282. case "undefined":
  1283. case "function":
  1284. case "unknown":
  1285. break;
  1286. default:
  1287. if (preComma) {
  1288. result.push(',');
  1289. }
  1290. result.push(utils.json2str(item));
  1291. preComma = 1;
  1292. }
  1293. }
  1294. result.push("]");
  1295. return result.join("");
  1296. }
  1297. function pad(source) {
  1298. return source < 10 ? '0' + source : source;
  1299. }
  1300. function encodeDate(source) {
  1301. return '"' + source.getFullYear() + "-" +
  1302. pad(source.getMonth() + 1) + "-" +
  1303. pad(source.getDate()) + "T" +
  1304. pad(source.getHours()) + ":" +
  1305. pad(source.getMinutes()) + ":" +
  1306. pad(source.getSeconds()) + '"';
  1307. }
  1308. return function (value) {
  1309. switch (typeof value) {
  1310. case 'undefined':
  1311. return 'undefined';
  1312. case 'number':
  1313. return isFinite(value) ? String(value) : "null";
  1314. case 'string':
  1315. return encodeString(value);
  1316. case 'boolean':
  1317. return String(value);
  1318. default:
  1319. if (value === null) {
  1320. return 'null';
  1321. } else if (utils.isArray(value)) {
  1322. return encodeArray(value);
  1323. } else if (utils.isDate(value)) {
  1324. return encodeDate(value);
  1325. } else {
  1326. var result = ['{'],
  1327. encode = utils.json2str,
  1328. preComma,
  1329. item;
  1330. for (var key in value) {
  1331. if (Object.prototype.hasOwnProperty.call(value, key)) {
  1332. item = value[key];
  1333. switch (typeof item) {
  1334. case 'undefined':
  1335. case 'unknown':
  1336. case 'function':
  1337. break;
  1338. default:
  1339. if (preComma) {
  1340. result.push(',');
  1341. }
  1342. preComma = 1;
  1343. result.push(encode(key) + ':' + encode(item));
  1344. }
  1345. }
  1346. }
  1347. result.push('}');
  1348. return result.join('');
  1349. }
  1350. }
  1351. };
  1352. }
  1353. })()
  1354. };
  1355. /**
  1356. * 判断给定的对象是否是字符串
  1357. * @method isString
  1358. * @param { * } object 需要判断的对象
  1359. * @return { Boolean } 给定的对象是否是字符串
  1360. */
  1361. /**
  1362. * 判断给定的对象是否是数组
  1363. * @method isArray
  1364. * @param { * } object 需要判断的对象
  1365. * @return { Boolean } 给定的对象是否是数组
  1366. */
  1367. /**
  1368. * 判断给定的对象是否是一个Function
  1369. * @method isFunction
  1370. * @param { * } object 需要判断的对象
  1371. * @return { Boolean } 给定的对象是否是Function
  1372. */
  1373. /**
  1374. * 判断给定的对象是否是Number
  1375. * @method isNumber
  1376. * @param { * } object 需要判断的对象
  1377. * @return { Boolean } 给定的对象是否是Number
  1378. */
  1379. /**
  1380. * 判断给定的对象是否是一个正则表达式
  1381. * @method isRegExp
  1382. * @param { * } object 需要判断的对象
  1383. * @return { Boolean } 给定的对象是否是正则表达式
  1384. */
  1385. /**
  1386. * 判断给定的对象是否是一个普通对象
  1387. * @method isObject
  1388. * @param { * } object 需要判断的对象
  1389. * @return { Boolean } 给定的对象是否是普通对象
  1390. */
  1391. utils.each(['String', 'Function', 'Array', 'Number', 'RegExp', 'Object', 'Date'], function (v) {
  1392. UE.utils['is' + v] = function (obj) {
  1393. return Object.prototype.toString.apply(obj) == '[object ' + v + ']';
  1394. }
  1395. });
  1396. // core/EventBase.js
  1397. /**
  1398. * UE采用的事件基类
  1399. * @file
  1400. * @module UE
  1401. * @class EventBase
  1402. * @since 1.2.6.1
  1403. */
  1404. /**
  1405. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  1406. * @unfile
  1407. * @module UE
  1408. */
  1409. /**
  1410. * UE采用的事件基类,继承此类的对应类将获取addListener,removeListener,fireEvent方法。
  1411. * 在UE中,Editor以及所有ui实例都继承了该类,故可以在对应的ui对象以及editor对象上使用上述方法。
  1412. * @unfile
  1413. * @module UE
  1414. * @class EventBase
  1415. */
  1416. /**
  1417. * 通过此构造器,子类可以继承EventBase获取事件监听的方法
  1418. * @constructor
  1419. * @example
  1420. * ```javascript
  1421. * UE.EventBase.call(editor);
  1422. * ```
  1423. */
  1424. var EventBase = UE.EventBase = function () {};
  1425. EventBase.prototype = {
  1426. /**
  1427. * 注册事件监听器
  1428. * @method addListener
  1429. * @param { String } types 监听的事件名称,同时监听多个事件使用空格分隔
  1430. * @param { Function } fn 监听的事件被触发时,会执行该回调函数
  1431. * @waining 事件被触发时,监听的函数假如返回的值恒等于true,回调函数的队列中后面的函数将不执行
  1432. * @example
  1433. * ```javascript
  1434. * editor.addListener('selectionchange',function(){
  1435. * console.log("选区已经变化!");
  1436. * })
  1437. * editor.addListener('beforegetcontent aftergetcontent',function(type){
  1438. * if(type == 'beforegetcontent'){
  1439. * //do something
  1440. * }else{
  1441. * //do something
  1442. * }
  1443. * console.log(this.getContent) // this是注册的事件的编辑器实例
  1444. * })
  1445. * ```
  1446. * @see UE.EventBase:fireEvent(String)
  1447. */
  1448. addListener: function (types, listener) {
  1449. types = utils.trim(types).split(/\s+/);
  1450. for (var i = 0, ti; ti = types[i++];) {
  1451. getListener(this, ti, true).push(listener);
  1452. }
  1453. },
  1454. on: function (types, listener) {
  1455. return this.addListener(types, listener);
  1456. },
  1457. off: function (types, listener) {
  1458. return this.removeListener(types, listener)
  1459. },
  1460. trigger: function () {
  1461. return this.fireEvent.apply(this, arguments);
  1462. },
  1463. /**
  1464. * 移除事件监听器
  1465. * @method removeListener
  1466. * @param { String } types 移除的事件名称,同时移除多个事件使用空格分隔
  1467. * @param { Function } fn 移除监听事件的函数引用
  1468. * @example
  1469. * ```javascript
  1470. * //changeCallback为方法体
  1471. * editor.removeListener("selectionchange",changeCallback);
  1472. * ```
  1473. */
  1474. removeListener: function (types, listener) {
  1475. types = utils.trim(types).split(/\s+/);
  1476. for (var i = 0, ti; ti = types[i++];) {
  1477. utils.removeItem(getListener(this, ti) || [], listener);
  1478. }
  1479. },
  1480. /**
  1481. * 触发事件
  1482. * @method fireEvent
  1483. * @param { String } types 触发的事件名称,同时触发多个事件使用空格分隔
  1484. * @remind 该方法会触发addListener
  1485. * @return { * } 返回触发事件的队列中,最后执行的回调函数的返回值
  1486. * @example
  1487. * ```javascript
  1488. * editor.fireEvent("selectionchange");
  1489. * ```
  1490. */
  1491. /**
  1492. * 触发事件
  1493. * @method fireEvent
  1494. * @param { String } types 触发的事件名称,同时触发多个事件使用空格分隔
  1495. * @param { *... } options 可选参数,可以传入一个或多个参数,会传给事件触发的回调函数
  1496. * @return { * } 返回触发事件的队列中,最后执行的回调函数的返回值
  1497. * @example
  1498. * ```javascript
  1499. *
  1500. * editor.addListener( "selectionchange", function ( type, arg1, arg2 ) {
  1501. *
  1502. * console.log( arg1 + " " + arg2 );
  1503. *
  1504. * } );
  1505. *
  1506. * //触发selectionchange事件, 会执行上面的事件监听器
  1507. * //output: Hello World
  1508. * editor.fireEvent("selectionchange", "Hello", "World");
  1509. * ```
  1510. */
  1511. fireEvent: function () {
  1512. var types = arguments[0];
  1513. types = utils.trim(types).split(' ');
  1514. for (var i = 0, ti; ti = types[i++];) {
  1515. var listeners = getListener(this, ti),
  1516. r, t, k;
  1517. if (listeners) {
  1518. k = listeners.length;
  1519. while (k--) {
  1520. if (!listeners[k]) continue;
  1521. t = listeners[k].apply(this, arguments);
  1522. if (t === true) {
  1523. return t;
  1524. }
  1525. if (t !== undefined) {
  1526. r = t;
  1527. }
  1528. }
  1529. }
  1530. if (t = this['on' + ti.toLowerCase()]) {
  1531. r = t.apply(this, arguments);
  1532. }
  1533. }
  1534. return r;
  1535. }
  1536. };
  1537. /**
  1538. * 获得对象所拥有监听类型的所有监听器
  1539. * @unfile
  1540. * @module UE
  1541. * @since 1.2.6.1
  1542. * @method getListener
  1543. * @public
  1544. * @param { Object } obj 查询监听器的对象
  1545. * @param { String } type 事件类型
  1546. * @param { Boolean } force 为true且当前所有type类型的侦听器不存在时,创建一个空监听器数组
  1547. * @return { Array } 监听器数组
  1548. */
  1549. function getListener(obj, type, force) {
  1550. var allListeners;
  1551. type = type.toLowerCase();
  1552. return ((allListeners = (obj.__allListeners || force && (obj.__allListeners = {}))) &&
  1553. (allListeners[type] || force && (allListeners[type] = [])));
  1554. }
  1555. // core/dtd.js
  1556. ///import editor.js
  1557. ///import core/dom/dom.js
  1558. ///import core/utils.js
  1559. /**
  1560. * dtd html语义化的体现类
  1561. * @constructor
  1562. * @namespace dtd
  1563. */
  1564. var dtd = dom.dtd = (function () {
  1565. function _(s) {
  1566. for (var k in s) {
  1567. s[k.toUpperCase()] = s[k];
  1568. }
  1569. return s;
  1570. }
  1571. var X = utils.extend2;
  1572. var A = _({
  1573. isindex: 1,
  1574. fieldset: 1
  1575. }),
  1576. B = _({
  1577. input: 1,
  1578. button: 1,
  1579. select: 1,
  1580. textarea: 1,
  1581. label: 1
  1582. }),
  1583. C = X(_({
  1584. a: 1
  1585. }), B),
  1586. D = X({
  1587. iframe: 1
  1588. }, C),
  1589. E = _({
  1590. hr: 1,
  1591. ul: 1,
  1592. menu: 1,
  1593. div: 1,
  1594. blockquote: 1,
  1595. noscript: 1,
  1596. table: 1,
  1597. center: 1,
  1598. address: 1,
  1599. dir: 1,
  1600. pre: 1,
  1601. h5: 1,
  1602. dl: 1,
  1603. h4: 1,
  1604. noframes: 1,
  1605. h6: 1,
  1606. ol: 1,
  1607. h1: 1,
  1608. h3: 1,
  1609. h2: 1
  1610. }),
  1611. F = _({
  1612. ins: 1,
  1613. del: 1,
  1614. script: 1,
  1615. style: 1
  1616. }),
  1617. G = X(_({
  1618. b: 1,
  1619. acronym: 1,
  1620. bdo: 1,
  1621. 'var': 1,
  1622. '#': 1,
  1623. abbr: 1,
  1624. code: 1,
  1625. br: 1,
  1626. i: 1,
  1627. cite: 1,
  1628. kbd: 1,
  1629. u: 1,
  1630. strike: 1,
  1631. s: 1,
  1632. tt: 1,
  1633. strong: 1,
  1634. q: 1,
  1635. samp: 1,
  1636. em: 1,
  1637. dfn: 1,
  1638. span: 1
  1639. }), F),
  1640. H = X(_({
  1641. sub: 1,
  1642. img: 1,
  1643. embed: 1,
  1644. object: 1,
  1645. sup: 1,
  1646. basefont: 1,
  1647. map: 1,
  1648. applet: 1,
  1649. font: 1,
  1650. big: 1,
  1651. small: 1
  1652. }), G),
  1653. I = X(_({
  1654. p: 1
  1655. }), H),
  1656. J = X(_({
  1657. iframe: 1
  1658. }), H, B),
  1659. K = _({
  1660. img: 1,
  1661. embed: 1,
  1662. noscript: 1,
  1663. br: 1,
  1664. kbd: 1,
  1665. center: 1,
  1666. button: 1,
  1667. basefont: 1,
  1668. h5: 1,
  1669. h4: 1,
  1670. samp: 1,
  1671. h6: 1,
  1672. ol: 1,
  1673. h1: 1,
  1674. h3: 1,
  1675. h2: 1,
  1676. form: 1,
  1677. font: 1,
  1678. '#': 1,
  1679. select: 1,
  1680. menu: 1,
  1681. ins: 1,
  1682. abbr: 1,
  1683. label: 1,
  1684. code: 1,
  1685. table: 1,
  1686. script: 1,
  1687. cite: 1,
  1688. input: 1,
  1689. iframe: 1,
  1690. strong: 1,
  1691. textarea: 1,
  1692. noframes: 1,
  1693. big: 1,
  1694. small: 1,
  1695. span: 1,
  1696. hr: 1,
  1697. sub: 1,
  1698. bdo: 1,
  1699. 'var': 1,
  1700. div: 1,
  1701. object: 1,
  1702. sup: 1,
  1703. strike: 1,
  1704. dir: 1,
  1705. map: 1,
  1706. dl: 1,
  1707. applet: 1,
  1708. del: 1,
  1709. isindex: 1,
  1710. fieldset: 1,
  1711. ul: 1,
  1712. b: 1,
  1713. acronym: 1,
  1714. a: 1,
  1715. blockquote: 1,
  1716. i: 1,
  1717. u: 1,
  1718. s: 1,
  1719. tt: 1,
  1720. address: 1,
  1721. q: 1,
  1722. pre: 1,
  1723. p: 1,
  1724. em: 1,
  1725. dfn: 1
  1726. }),
  1727. L = X(_({
  1728. a: 0
  1729. }), J), //a不能被切开,所以把他
  1730. M = _({
  1731. tr: 1
  1732. }),
  1733. N = _({
  1734. '#': 1
  1735. }),
  1736. O = X(_({
  1737. param: 1
  1738. }), K),
  1739. P = X(_({
  1740. form: 1
  1741. }), A, D, E, I),
  1742. Q = _({
  1743. li: 1,
  1744. ol: 1,
  1745. ul: 1
  1746. }),
  1747. R = _({
  1748. style: 1,
  1749. script: 1
  1750. }),
  1751. S = _({
  1752. base: 1,
  1753. link: 1,
  1754. meta: 1,
  1755. title: 1
  1756. }),
  1757. T = X(S, R),
  1758. U = _({
  1759. head: 1,
  1760. body: 1
  1761. }),
  1762. V = _({
  1763. html: 1
  1764. });
  1765. var block = _({
  1766. address: 1,
  1767. blockquote: 1,
  1768. center: 1,
  1769. dir: 1,
  1770. div: 1,
  1771. dl: 1,
  1772. fieldset: 1,
  1773. form: 1,
  1774. h1: 1,
  1775. h2: 1,
  1776. h3: 1,
  1777. h4: 1,
  1778. h5: 1,
  1779. h6: 1,
  1780. hr: 1,
  1781. isindex: 1,
  1782. menu: 1,
  1783. noframes: 1,
  1784. ol: 1,
  1785. p: 1,
  1786. pre: 1,
  1787. table: 1,
  1788. ul: 1
  1789. }),
  1790. empty = _({
  1791. area: 1,
  1792. base: 1,
  1793. basefont: 1,
  1794. br: 1,
  1795. col: 1,
  1796. command: 1,
  1797. dialog: 1,
  1798. embed: 1,
  1799. hr: 1,
  1800. img: 1,
  1801. input: 1,
  1802. isindex: 1,
  1803. keygen: 1,
  1804. link: 1,
  1805. meta: 1,
  1806. param: 1,
  1807. source: 1,
  1808. track: 1,
  1809. wbr: 1
  1810. });
  1811. return _({
  1812. // $ 表示自定的属性
  1813. // body外的元素列表.
  1814. $nonBodyContent: X(V, U, S),
  1815. //块结构元素列表
  1816. $block: block,
  1817. //内联元素列表
  1818. $inline: L,
  1819. $inlineWithA: X(_({
  1820. a: 1
  1821. }), L),
  1822. $body: X(_({
  1823. script: 1,
  1824. style: 1
  1825. }), block),
  1826. $cdata: _({
  1827. script: 1,
  1828. style: 1
  1829. }),
  1830. //自闭和元素
  1831. $empty: empty,
  1832. //不是自闭合,但不能让range选中里边
  1833. $nonChild: _({
  1834. iframe: 1,
  1835. textarea: 1
  1836. }),
  1837. //列表元素列表
  1838. $listItem: _({
  1839. dd: 1,
  1840. dt: 1,
  1841. li: 1
  1842. }),
  1843. //列表根元素列表
  1844. $list: _({
  1845. ul: 1,
  1846. ol: 1,
  1847. dl: 1
  1848. }),
  1849. //不能认为是空的元素
  1850. $isNotEmpty: _({
  1851. table: 1,
  1852. ul: 1,
  1853. ol: 1,
  1854. dl: 1,
  1855. iframe: 1,
  1856. area: 1,
  1857. base: 1,
  1858. col: 1,
  1859. hr: 1,
  1860. img: 1,
  1861. embed: 1,
  1862. input: 1,
  1863. link: 1,
  1864. meta: 1,
  1865. param: 1,
  1866. h1: 1,
  1867. h2: 1,
  1868. h3: 1,
  1869. h4: 1,
  1870. h5: 1,
  1871. h6: 1
  1872. }),
  1873. //如果没有子节点就可以删除的元素列表,像span,a
  1874. $removeEmpty: _({
  1875. a: 1,
  1876. abbr: 1,
  1877. acronym: 1,
  1878. address: 1,
  1879. b: 1,
  1880. bdo: 1,
  1881. big: 1,
  1882. cite: 1,
  1883. code: 1,
  1884. del: 1,
  1885. dfn: 1,
  1886. em: 1,
  1887. font: 1,
  1888. i: 1,
  1889. ins: 1,
  1890. label: 1,
  1891. kbd: 1,
  1892. q: 1,
  1893. s: 1,
  1894. samp: 1,
  1895. small: 1,
  1896. span: 1,
  1897. strike: 1,
  1898. strong: 1,
  1899. sub: 1,
  1900. sup: 1,
  1901. tt: 1,
  1902. u: 1,
  1903. 'var': 1
  1904. }),
  1905. $removeEmptyBlock: _({
  1906. 'p': 1,
  1907. 'div': 1
  1908. }),
  1909. //在table元素里的元素列表
  1910. $tableContent: _({
  1911. caption: 1,
  1912. col: 1,
  1913. colgroup: 1,
  1914. tbody: 1,
  1915. td: 1,
  1916. tfoot: 1,
  1917. th: 1,
  1918. thead: 1,
  1919. tr: 1,
  1920. table: 1
  1921. }),
  1922. //不转换的标签
  1923. $notTransContent: _({
  1924. pre: 1,
  1925. script: 1,
  1926. style: 1,
  1927. textarea: 1
  1928. }),
  1929. html: U,
  1930. head: T,
  1931. style: N,
  1932. script: N,
  1933. body: P,
  1934. base: {},
  1935. link: {},
  1936. meta: {},
  1937. title: N,
  1938. col: {},
  1939. tr: _({
  1940. td: 1,
  1941. th: 1
  1942. }),
  1943. img: {},
  1944. embed: {},
  1945. colgroup: _({
  1946. thead: 1,
  1947. col: 1,
  1948. tbody: 1,
  1949. tr: 1,
  1950. tfoot: 1
  1951. }),
  1952. noscript: P,
  1953. td: P,
  1954. br: {},
  1955. th: P,
  1956. center: P,
  1957. kbd: L,
  1958. button: X(I, E),
  1959. basefont: {},
  1960. h5: L,
  1961. h4: L,
  1962. samp: L,
  1963. h6: L,
  1964. ol: Q,
  1965. h1: L,
  1966. h3: L,
  1967. option: N,
  1968. h2: L,
  1969. form: X(A, D, E, I),
  1970. select: _({
  1971. optgroup: 1,
  1972. option: 1
  1973. }),
  1974. font: L,
  1975. ins: L,
  1976. menu: Q,
  1977. abbr: L,
  1978. label: L,
  1979. table: _({
  1980. thead: 1,
  1981. col: 1,
  1982. tbody: 1,
  1983. tr: 1,
  1984. colgroup: 1,
  1985. caption: 1,
  1986. tfoot: 1
  1987. }),
  1988. code: L,
  1989. tfoot: M,
  1990. cite: L,
  1991. li: P,
  1992. input: {},
  1993. iframe: P,
  1994. strong: L,
  1995. textarea: N,
  1996. noframes: P,
  1997. big: L,
  1998. small: L,
  1999. //trace:
  2000. span: _({
  2001. '#': 1,
  2002. br: 1,
  2003. b: 1,
  2004. strong: 1,
  2005. u: 1,
  2006. i: 1,
  2007. em: 1,
  2008. sub: 1,
  2009. sup: 1,
  2010. strike: 1,
  2011. span: 1
  2012. }),
  2013. hr: L,
  2014. dt: L,
  2015. sub: L,
  2016. optgroup: _({
  2017. option: 1
  2018. }),
  2019. param: {},
  2020. bdo: L,
  2021. 'var': L,
  2022. div: P,
  2023. object: O,
  2024. sup: L,
  2025. dd: P,
  2026. strike: L,
  2027. area: {},
  2028. dir: Q,
  2029. map: X(_({
  2030. area: 1,
  2031. form: 1,
  2032. p: 1
  2033. }), A, F, E),
  2034. applet: O,
  2035. dl: _({
  2036. dt: 1,
  2037. dd: 1
  2038. }),
  2039. del: L,
  2040. isindex: {},
  2041. fieldset: X(_({
  2042. legend: 1
  2043. }), K),
  2044. thead: M,
  2045. ul: Q,
  2046. acronym: L,
  2047. b: L,
  2048. a: X(_({
  2049. a: 1
  2050. }), J),
  2051. blockquote: X(_({
  2052. td: 1,
  2053. tr: 1,
  2054. tbody: 1,
  2055. li: 1
  2056. }), P),
  2057. caption: L,
  2058. i: L,
  2059. u: L,
  2060. tbody: M,
  2061. s: L,
  2062. address: X(D, I),
  2063. tt: L,
  2064. legend: L,
  2065. q: L,
  2066. pre: X(G, C),
  2067. p: X(_({
  2068. 'a': 1
  2069. }), L),
  2070. em: L,
  2071. dfn: L
  2072. });
  2073. })();
  2074. // core/domUtils.js
  2075. /**
  2076. * Dom操作工具包
  2077. * @file
  2078. * @module UE.dom.domUtils
  2079. * @since 1.2.6.1
  2080. */
  2081. /**
  2082. * Dom操作工具包
  2083. * @unfile
  2084. * @module UE.dom.domUtils
  2085. */
  2086. function getDomNode(node, start, ltr, startFromChild, fn, guard) {
  2087. var tmpNode = startFromChild && node[start],
  2088. parent;
  2089. !tmpNode && (tmpNode = node[ltr]);
  2090. while (!tmpNode && (parent = (parent || node).parentNode)) {
  2091. if (parent.tagName == 'BODY' || guard && !guard(parent)) {
  2092. return null;
  2093. }
  2094. tmpNode = parent[ltr];
  2095. }
  2096. if (tmpNode && fn && !fn(tmpNode)) {
  2097. return getDomNode(tmpNode, start, ltr, false, fn);
  2098. }
  2099. return tmpNode;
  2100. }
  2101. var attrFix = ie && browser.version < 9 ? {
  2102. tabindex: "tabIndex",
  2103. readonly: "readOnly",
  2104. "for": "htmlFor",
  2105. "class": "className",
  2106. maxlength: "maxLength",
  2107. cellspacing: "cellSpacing",
  2108. cellpadding: "cellPadding",
  2109. rowspan: "rowSpan",
  2110. colspan: "colSpan",
  2111. usemap: "useMap",
  2112. frameborder: "frameBorder"
  2113. } : {
  2114. tabindex: "tabIndex",
  2115. readonly: "readOnly"
  2116. },
  2117. styleBlock = utils.listToMap([
  2118. '-webkit-box', '-moz-box', 'block',
  2119. 'list-item', 'table', 'table-row-group',
  2120. 'table-header-group', 'table-footer-group',
  2121. 'table-row', 'table-column-group', 'table-column',
  2122. 'table-cell', 'table-caption'
  2123. ]);
  2124. var domUtils = dom.domUtils = {
  2125. //节点常量
  2126. NODE_ELEMENT: 1,
  2127. NODE_DOCUMENT: 9,
  2128. NODE_TEXT: 3,
  2129. NODE_COMMENT: 8,
  2130. NODE_DOCUMENT_FRAGMENT: 11,
  2131. //位置关系
  2132. POSITION_IDENTICAL: 0,
  2133. POSITION_DISCONNECTED: 1,
  2134. POSITION_FOLLOWING: 2,
  2135. POSITION_PRECEDING: 4,
  2136. POSITION_IS_CONTAINED: 8,
  2137. POSITION_CONTAINS: 16,
  2138. //ie6使用其他的会有一段空白出现
  2139. fillChar: ie && browser.version == '6' ? '\ufeff' : '\u200B',
  2140. //-------------------------Node部分--------------------------------
  2141. keys: {
  2142. /*Backspace*/
  2143. 8: 1,
  2144. /*Delete*/ 46: 1,
  2145. /*Shift*/
  2146. 16: 1,
  2147. /*Ctrl*/ 17: 1,
  2148. /*Alt*/ 18: 1,
  2149. 37: 1,
  2150. 38: 1,
  2151. 39: 1,
  2152. 40: 1,
  2153. 13: 1 /*enter*/
  2154. },
  2155. /**
  2156. * 获取节点A相对于节点B的位置关系
  2157. * @method getPosition
  2158. * @param { Node } nodeA 需要查询位置关系的节点A
  2159. * @param { Node } nodeB 需要查询位置关系的节点B
  2160. * @return { Number } 节点A与节点B的关系
  2161. * @example
  2162. * ```javascript
  2163. * //output: 20
  2164. * var position = UE.dom.domUtils.getPosition( document.documentElement, document.body );
  2165. *
  2166. * switch ( position ) {
  2167. *
  2168. * //0
  2169. * case UE.dom.domUtils.POSITION_IDENTICAL:
  2170. * console.log('元素相同');
  2171. * break;
  2172. * //1
  2173. * case UE.dom.domUtils.POSITION_DISCONNECTED:
  2174. * console.log('两个节点在不同的文档中');
  2175. * break;
  2176. * //2
  2177. * case UE.dom.domUtils.POSITION_FOLLOWING:
  2178. * console.log('节点A在节点B之后');
  2179. * break;
  2180. * //4
  2181. * case UE.dom.domUtils.POSITION_PRECEDING;
  2182. * console.log('节点A在节点B之前');
  2183. * break;
  2184. * //8
  2185. * case UE.dom.domUtils.POSITION_IS_CONTAINED:
  2186. * console.log('节点A被节点B包含');
  2187. * break;
  2188. * case 10:
  2189. * console.log('节点A被节点B包含且节点A在节点B之后');
  2190. * break;
  2191. * //16
  2192. * case UE.dom.domUtils.POSITION_CONTAINS:
  2193. * console.log('节点A包含节点B');
  2194. * break;
  2195. * case 20:
  2196. * console.log('节点A包含节点B且节点A在节点B之前');
  2197. * break;
  2198. *
  2199. * }
  2200. * ```
  2201. */
  2202. getPosition: function (nodeA, nodeB) {
  2203. // 如果两个节点是同一个节点
  2204. if (nodeA === nodeB) {
  2205. // domUtils.POSITION_IDENTICAL
  2206. return 0;
  2207. }
  2208. var node,
  2209. parentsA = [nodeA],
  2210. parentsB = [nodeB];
  2211. node = nodeA;
  2212. while (node = node.parentNode) {
  2213. // 如果nodeB是nodeA的祖先节点
  2214. if (node === nodeB) {
  2215. // domUtils.POSITION_IS_CONTAINED + domUtils.POSITION_FOLLOWING
  2216. return 10;
  2217. }
  2218. parentsA.push(node);
  2219. }
  2220. node = nodeB;
  2221. while (node = node.parentNode) {
  2222. // 如果nodeA是nodeB的祖先节点
  2223. if (node === nodeA) {
  2224. // domUtils.POSITION_CONTAINS + domUtils.POSITION_PRECEDING
  2225. return 20;
  2226. }
  2227. parentsB.push(node);
  2228. }
  2229. parentsA.reverse();
  2230. parentsB.reverse();
  2231. if (parentsA[0] !== parentsB[0]) {
  2232. // domUtils.POSITION_DISCONNECTED
  2233. return 1;
  2234. }
  2235. var i = -1;
  2236. while (i++, parentsA[i] === parentsB[i]) {}
  2237. nodeA = parentsA[i];
  2238. nodeB = parentsB[i];
  2239. while (nodeA = nodeA.nextSibling) {
  2240. if (nodeA === nodeB) {
  2241. // domUtils.POSITION_PRECEDING
  2242. return 4
  2243. }
  2244. }
  2245. // domUtils.POSITION_FOLLOWING
  2246. return 2;
  2247. },
  2248. /**
  2249. * 检测节点node在父节点中的索引位置
  2250. * @method getNodeIndex
  2251. * @param { Node } node 需要检测的节点对象
  2252. * @return { Number } 该节点在父节点中的位置
  2253. * @see UE.dom.domUtils.getNodeIndex(Node,Boolean)
  2254. */
  2255. /**
  2256. * 检测节点node在父节点中的索引位置, 根据给定的mergeTextNode参数决定是否要合并多个连续的文本节点为一个节点
  2257. * @method getNodeIndex
  2258. * @param { Node } node 需要检测的节点对象
  2259. * @param { Boolean } mergeTextNode 是否合并多个连续的文本节点为一个节点
  2260. * @return { Number } 该节点在父节点中的位置
  2261. * @example
  2262. * ```javascript
  2263. *
  2264. * var node = document.createElement("div");
  2265. *
  2266. * node.appendChild( document.createTextNode( "hello" ) );
  2267. * node.appendChild( document.createTextNode( "world" ) );
  2268. * node.appendChild( node = document.createElement( "div" ) );
  2269. *
  2270. * //output: 2
  2271. * console.log( UE.dom.domUtils.getNodeIndex( node ) );
  2272. *
  2273. * //output: 1
  2274. * console.log( UE.dom.domUtils.getNodeIndex( node, true ) );
  2275. *
  2276. * ```
  2277. */
  2278. getNodeIndex: function (node, ignoreTextNode) {
  2279. var preNode = node,
  2280. i = 0;
  2281. while (preNode = preNode.previousSibling) {
  2282. if (ignoreTextNode && preNode.nodeType == 3) {
  2283. if (preNode.nodeType != preNode.nextSibling.nodeType) {
  2284. i++;
  2285. }
  2286. continue;
  2287. }
  2288. i++;
  2289. }
  2290. return i;
  2291. },
  2292. /**
  2293. * 检测节点node是否在给定的document对象上
  2294. * @method inDoc
  2295. * @param { Node } node 需要检测的节点对象
  2296. * @param { DomDocument } doc 需要检测的document对象
  2297. * @return { Boolean } 该节点node是否在给定的document的dom树上
  2298. * @example
  2299. * ```javascript
  2300. *
  2301. * var node = document.createElement("div");
  2302. *
  2303. * //output: false
  2304. * console.log( UE.do.domUtils.inDoc( node, document ) );
  2305. *
  2306. * document.body.appendChild( node );
  2307. *
  2308. * //output: true
  2309. * console.log( UE.do.domUtils.inDoc( node, document ) );
  2310. *
  2311. * ```
  2312. */
  2313. inDoc: function (node, doc) {
  2314. return domUtils.getPosition(node, doc) == 10;
  2315. },
  2316. /**
  2317. * 根据给定的过滤规则filterFn, 查找符合该过滤规则的node节点的第一个祖先节点,
  2318. * 查找的起点是给定node节点的父节点。
  2319. * @method findParent
  2320. * @param { Node } node 需要查找的节点
  2321. * @param { Function } filterFn 自定义的过滤方法。
  2322. * @warning 查找的终点是到body节点为止
  2323. * @remind 自定义的过滤方法filterFn接受一个Node对象作为参数, 该对象代表当前执行检测的祖先节点。 如果该
  2324. * 节点满足过滤条件, 则要求返回true, 这时将直接返回该节点作为findParent()的结果, 否则, 请返回false。
  2325. * @return { Node | Null } 如果找到符合过滤条件的节点, 就返回该节点, 否则返回NULL
  2326. * @example
  2327. * ```javascript
  2328. * var filterNode = UE.dom.domUtils.findParent( document.body.firstChild, function ( node ) {
  2329. *
  2330. * //由于查找的终点是body节点, 所以永远也不会匹配当前过滤器的条件, 即这里永远会返回false
  2331. * return node.tagName === "HTML";
  2332. *
  2333. * } );
  2334. *
  2335. * //output: true
  2336. * console.log( filterNode === null );
  2337. * ```
  2338. */
  2339. /**
  2340. * 根据给定的过滤规则filterFn, 查找符合该过滤规则的node节点的第一个祖先节点,
  2341. * 如果includeSelf的值为true,则查找的起点是给定的节点node, 否则, 起点是node的父节点
  2342. * @method findParent
  2343. * @param { Node } node 需要查找的节点
  2344. * @param { Function } filterFn 自定义的过滤方法。
  2345. * @param { Boolean } includeSelf 查找过程是否包含自身
  2346. * @warning 查找的终点是到body节点为止
  2347. * @remind 自定义的过滤方法filterFn接受一个Node对象作为参数, 该对象代表当前执行检测的祖先节点。 如果该
  2348. * 节点满足过滤条件, 则要求返回true, 这时将直接返回该节点作为findParent()的结果, 否则, 请返回false。
  2349. * @remind 如果includeSelf为true, 则过滤器第一次执行时的参数会是节点本身。
  2350. * 反之, 过滤器第一次执行时的参数将是该节点的父节点。
  2351. * @return { Node | Null } 如果找到符合过滤条件的节点, 就返回该节点, 否则返回NULL
  2352. * @example
  2353. * ```html
  2354. * <body>
  2355. *
  2356. * <div id="test">
  2357. * </div>
  2358. *
  2359. * <script type="text/javascript">
  2360. *
  2361. * //output: DIV, BODY
  2362. * var filterNode = UE.dom.domUtils.findParent( document.getElementById( "test" ), function ( node ) {
  2363. *
  2364. * console.log( node.tagName );
  2365. * return false;
  2366. *
  2367. * }, true );
  2368. *
  2369. * </script>
  2370. * </body>
  2371. * ```
  2372. */
  2373. findParent: function (node, filterFn, includeSelf) {
  2374. if (node && !domUtils.isBody(node)) {
  2375. node = includeSelf ? node : node.parentNode;
  2376. while (node) {
  2377. if (!filterFn || filterFn(node) || domUtils.isBody(node)) {
  2378. return filterFn && !filterFn(node) && domUtils.isBody(node) ? null : node;
  2379. }
  2380. node = node.parentNode;
  2381. }
  2382. }
  2383. return null;
  2384. },
  2385. /**
  2386. * 查找node的节点名为tagName的第一个祖先节点, 查找的起点是node节点的父节点。
  2387. * @method findParentByTagName
  2388. * @param { Node } node 需要查找的节点对象
  2389. * @param { Array } tagNames 需要查找的父节点的名称数组
  2390. * @warning 查找的终点是到body节点为止
  2391. * @return { Node | NULL } 如果找到符合条件的节点, 则返回该节点, 否则返回NULL
  2392. * @example
  2393. * ```javascript
  2394. * var node = UE.dom.domUtils.findParentByTagName( document.getElementsByTagName("div")[0], [ "BODY" ] );
  2395. * //output: BODY
  2396. * console.log( node.tagName );
  2397. * ```
  2398. */
  2399. /**
  2400. * 查找node的节点名为tagName的祖先节点, 如果includeSelf的值为true,则查找的起点是给定的节点node,
  2401. * 否则, 起点是node的父节点。
  2402. * @method findParentByTagName
  2403. * @param { Node } node 需要查找的节点对象
  2404. * @param { Array } tagNames 需要查找的父节点的名称数组
  2405. * @param { Boolean } includeSelf 查找过程是否包含node节点自身
  2406. * @warning 查找的终点是到body节点为止
  2407. * @return { Node | NULL } 如果找到符合条件的节点, 则返回该节点, 否则返回NULL
  2408. * @example
  2409. * ```javascript
  2410. * var queryTarget = document.getElementsByTagName("div")[0];
  2411. * var node = UE.dom.domUtils.findParentByTagName( queryTarget, [ "DIV" ], true );
  2412. * //output: true
  2413. * console.log( queryTarget === node );
  2414. * ```
  2415. */
  2416. findParentByTagName: function (node, tagNames, includeSelf, excludeFn) {
  2417. tagNames = utils.listToMap(utils.isArray(tagNames) ? tagNames : [tagNames]);
  2418. return domUtils.findParent(node, function (node) {
  2419. return tagNames[node.tagName] && !(excludeFn && excludeFn(node));
  2420. }, includeSelf);
  2421. },
  2422. /**
  2423. * 查找节点node的祖先节点集合, 查找的起点是给定节点的父节点,结果集中不包含给定的节点。
  2424. * @method findParents
  2425. * @param { Node } node 需要查找的节点对象
  2426. * @return { Array } 给定节点的祖先节点数组
  2427. * @grammar UE.dom.domUtils.findParents(node) => Array //返回一个祖先节点数组集合,不包含自身
  2428. * @grammar UE.dom.domUtils.findParents(node,includeSelf) => Array //返回一个祖先节点数组集合,includeSelf指定是否包含自身
  2429. * @grammar UE.dom.domUtils.findParents(node,includeSelf,filterFn) => Array //返回一个祖先节点数组集合,filterFn指定过滤条件,返回true的node将被选取
  2430. * @grammar UE.dom.domUtils.findParents(node,includeSelf,filterFn,closerFirst) => Array //返回一个祖先节点数组集合,closerFirst为true的话,node的直接父亲节点是数组的第0个
  2431. */
  2432. /**
  2433. * 查找节点node的祖先节点集合, 如果includeSelf的值为true,
  2434. * 则返回的结果集中允许出现当前给定的节点, 否则, 该节点不会出现在其结果集中。
  2435. * @method findParents
  2436. * @param { Node } node 需要查找的节点对象
  2437. * @param { Boolean } includeSelf 查找的结果中是否允许包含当前查找的节点对象
  2438. * @return { Array } 给定节点的祖先节点数组
  2439. */
  2440. findParents: function (node, includeSelf, filterFn, closerFirst) {
  2441. var parents = includeSelf && (filterFn && filterFn(node) || !filterFn) ? [node] : [];
  2442. while (node = domUtils.findParent(node, filterFn)) {
  2443. parents.push(node);
  2444. }
  2445. return closerFirst ? parents : parents.reverse();
  2446. },
  2447. /**
  2448. * 在节点node后面插入新节点newNode
  2449. * @method insertAfter
  2450. * @param { Node } node 目标节点
  2451. * @param { Node } newNode 新插入的节点, 该节点将置于目标节点之后
  2452. * @return { Node } 新插入的节点
  2453. */
  2454. insertAfter: function (node, newNode) {
  2455. return node.nextSibling ? node.parentNode.insertBefore(newNode, node.nextSibling) :
  2456. node.parentNode.appendChild(newNode);
  2457. },
  2458. /**
  2459. * 删除节点node及其下属的所有节点
  2460. * @method remove
  2461. * @param { Node } node 需要删除的节点对象
  2462. * @return { Node } 返回刚删除的节点对象
  2463. * @example
  2464. * ```html
  2465. * <div id="test">
  2466. * <div id="child">你好</div>
  2467. * </div>
  2468. * <script>
  2469. * UE.dom.domUtils.remove( document.body, false );
  2470. * //output: false
  2471. * console.log( document.getElementById( "child" ) !== null );
  2472. * </script>
  2473. * ```
  2474. */
  2475. /**
  2476. * 删除节点node,并根据keepChildren的值决定是否保留子节点
  2477. * @method remove
  2478. * @param { Node } node 需要删除的节点对象
  2479. * @param { Boolean } keepChildren 是否需要保留子节点
  2480. * @return { Node } 返回刚删除的节点对象
  2481. * @example
  2482. * ```html
  2483. * <div id="test">
  2484. * <div id="child">你好</div>
  2485. * </div>
  2486. * <script>
  2487. * UE.dom.domUtils.remove( document.body, true );
  2488. * //output: true
  2489. * console.log( document.getElementById( "child" ) !== null );
  2490. * </script>
  2491. * ```
  2492. */
  2493. remove: function (node, keepChildren) {
  2494. var parent = node.parentNode,
  2495. child;
  2496. if (parent) {
  2497. if (keepChildren && node.hasChildNodes()) {
  2498. while (child = node.firstChild) {
  2499. parent.insertBefore(child, node);
  2500. }
  2501. }
  2502. parent.removeChild(node);
  2503. }
  2504. return node;
  2505. },
  2506. /**
  2507. * 取得node节点的下一个兄弟节点, 如果该节点其后没有兄弟节点, 则递归查找其父节点之后的第一个兄弟节点,
  2508. * 直到找到满足条件的节点或者递归到BODY节点之后才会结束。
  2509. * @method getNextDomNode
  2510. * @param { Node } node 需要获取其后的兄弟节点的节点对象
  2511. * @return { Node | NULL } 如果找满足条件的节点, 则返回该节点, 否则返回NULL
  2512. * @example
  2513. * ```html
  2514. * <body>
  2515. * <div id="test">
  2516. * <span></span>
  2517. * </div>
  2518. * <i>xxx</i>
  2519. * </body>
  2520. * <script>
  2521. *
  2522. * //output: i节点
  2523. * console.log( UE.dom.domUtils.getNextDomNode( document.getElementById( "test" ) ) );
  2524. *
  2525. * </script>
  2526. * ```
  2527. * @example
  2528. * ```html
  2529. * <body>
  2530. * <div>
  2531. * <span></span>
  2532. * <i id="test">xxx</i>
  2533. * </div>
  2534. * <b>xxx</b>
  2535. * </body>
  2536. * <script>
  2537. *
  2538. * //由于id为test的i节点之后没有兄弟节点, 则查找其父节点(div)后面的兄弟节点
  2539. * //output: b节点
  2540. * console.log( UE.dom.domUtils.getNextDomNode( document.getElementById( "test" ) ) );
  2541. *
  2542. * </script>
  2543. * ```
  2544. */
  2545. /**
  2546. * 取得node节点的下一个兄弟节点, 如果startFromChild的值为ture,则先获取其子节点,
  2547. * 如果有子节点则直接返回第一个子节点;如果没有子节点或者startFromChild的值为false,
  2548. * 则执行<a href="#UE.dom.domUtils.getNextDomNode(Node)">getNextDomNode(Node node)</a>的查找过程。
  2549. * @method getNextDomNode
  2550. * @param { Node } node 需要获取其后的兄弟节点的节点对象
  2551. * @param { Boolean } startFromChild 查找过程是否从其子节点开始
  2552. * @return { Node | NULL } 如果找满足条件的节点, 则返回该节点, 否则返回NULL
  2553. * @see UE.dom.domUtils.getNextDomNode(Node)
  2554. */
  2555. getNextDomNode: function (node, startFromChild, filterFn, guard) {
  2556. return getDomNode(node, 'firstChild', 'nextSibling', startFromChild, filterFn, guard);
  2557. },
  2558. getPreDomNode: function (node, startFromChild, filterFn, guard) {
  2559. return getDomNode(node, 'lastChild', 'previousSibling', startFromChild, filterFn, guard);
  2560. },
  2561. /**
  2562. * 检测节点node是否属是UEditor定义的bookmark节点
  2563. * @method isBookmarkNode
  2564. * @private
  2565. * @param { Node } node 需要检测的节点对象
  2566. * @return { Boolean } 是否是bookmark节点
  2567. * @example
  2568. * ```html
  2569. * <span id="_baidu_bookmark_1"></span>
  2570. * <script>
  2571. * var bookmarkNode = document.getElementById("_baidu_bookmark_1");
  2572. * //output: true
  2573. * console.log( UE.dom.domUtils.isBookmarkNode( bookmarkNode ) );
  2574. * </script>
  2575. * ```
  2576. */
  2577. isBookmarkNode: function (node) {
  2578. return node.nodeType == 1 && node.id && /^_baidu_bookmark_/i.test(node.id);
  2579. },
  2580. /**
  2581. * 获取节点node所属的window对象
  2582. * @method getWindow
  2583. * @param { Node } node 节点对象
  2584. * @return { Window } 当前节点所属的window对象
  2585. * @example
  2586. * ```javascript
  2587. * //output: true
  2588. * console.log( UE.dom.domUtils.getWindow( document.body ) === window );
  2589. * ```
  2590. */
  2591. getWindow: function (node) {
  2592. var doc = node.ownerDocument || node;
  2593. return doc.defaultView || doc.parentWindow;
  2594. },
  2595. /**
  2596. * 获取离nodeA与nodeB最近的公共的祖先节点
  2597. * @method getCommonAncestor
  2598. * @param { Node } nodeA 第一个节点
  2599. * @param { Node } nodeB 第二个节点
  2600. * @remind 如果给定的两个节点是同一个节点, 将直接返回该节点。
  2601. * @return { Node | NULL } 如果未找到公共节点, 返回NULL, 否则返回最近的公共祖先节点。
  2602. * @example
  2603. * ```javascript
  2604. * var commonAncestor = UE.dom.domUtils.getCommonAncestor( document.body, document.body.firstChild );
  2605. * //output: true
  2606. * console.log( commonAncestor.tagName.toLowerCase() === 'body' );
  2607. * ```
  2608. */
  2609. getCommonAncestor: function (nodeA, nodeB) {
  2610. if (nodeA === nodeB)
  2611. return nodeA;
  2612. var parentsA = [nodeA],
  2613. parentsB = [nodeB],
  2614. parent = nodeA,
  2615. i = -1;
  2616. while (parent = parent.parentNode) {
  2617. if (parent === nodeB) {
  2618. return parent;
  2619. }
  2620. parentsA.push(parent);
  2621. }
  2622. parent = nodeB;
  2623. while (parent = parent.parentNode) {
  2624. if (parent === nodeA)
  2625. return parent;
  2626. parentsB.push(parent);
  2627. }
  2628. parentsA.reverse();
  2629. parentsB.reverse();
  2630. while (i++, parentsA[i] === parentsB[i]) {}
  2631. return i == 0 ? null : parentsA[i - 1];
  2632. },
  2633. /**
  2634. * 清除node节点左右连续为空的兄弟inline节点
  2635. * @method clearEmptySibling
  2636. * @param { Node } node 执行的节点对象, 如果该节点的左右连续的兄弟节点是空的inline节点,
  2637. * 则这些兄弟节点将被删除
  2638. * @grammar UE.dom.domUtils.clearEmptySibling(node,ignoreNext) //ignoreNext指定是否忽略右边空节点
  2639. * @grammar UE.dom.domUtils.clearEmptySibling(node,ignoreNext,ignorePre) //ignorePre指定是否忽略左边空节点
  2640. * @example
  2641. * ```html
  2642. * <body>
  2643. * <div></div>
  2644. * <span id="test"></span>
  2645. * <i></i>
  2646. * <b></b>
  2647. * <em>xxx</em>
  2648. * <span></span>
  2649. * </body>
  2650. * <script>
  2651. *
  2652. * UE.dom.domUtils.clearEmptySibling( document.getElementById( "test" ) );
  2653. *
  2654. * //output: <div></div><span id="test"></span><em>xxx</em><span></span>
  2655. * console.log( document.body.innerHTML );
  2656. *
  2657. * </script>
  2658. * ```
  2659. */
  2660. /**
  2661. * 清除node节点左右连续为空的兄弟inline节点, 如果ignoreNext的值为true,
  2662. * 则忽略对右边兄弟节点的操作。
  2663. * @method clearEmptySibling
  2664. * @param { Node } node 执行的节点对象, 如果该节点的左右连续的兄弟节点是空的inline节点,
  2665. * @param { Boolean } ignoreNext 是否忽略忽略对右边的兄弟节点的操作
  2666. * 则这些兄弟节点将被删除
  2667. * @see UE.dom.domUtils.clearEmptySibling(Node)
  2668. */
  2669. /**
  2670. * 清除node节点左右连续为空的兄弟inline节点, 如果ignoreNext的值为true,
  2671. * 则忽略对右边兄弟节点的操作, 如果ignorePre的值为true,则忽略对左边兄弟节点的操作。
  2672. * @method clearEmptySibling
  2673. * @param { Node } node 执行的节点对象, 如果该节点的左右连续的兄弟节点是空的inline节点,
  2674. * @param { Boolean } ignoreNext 是否忽略忽略对右边的兄弟节点的操作
  2675. * @param { Boolean } ignorePre 是否忽略忽略对左边的兄弟节点的操作
  2676. * 则这些兄弟节点将被删除
  2677. * @see UE.dom.domUtils.clearEmptySibling(Node)
  2678. */
  2679. clearEmptySibling: function (node, ignoreNext, ignorePre) {
  2680. function clear(next, dir) {
  2681. var tmpNode;
  2682. while (next && !domUtils.isBookmarkNode(next) && (domUtils.isEmptyInlineElement(next)
  2683. //这里不能把空格算进来会吧空格干掉,出现文字间的空格丢掉了
  2684. ||
  2685. !new RegExp('[^\t\n\r' + domUtils.fillChar + ']').test(next.nodeValue))) {
  2686. tmpNode = next[dir];
  2687. domUtils.remove(next);
  2688. next = tmpNode;
  2689. }
  2690. }!ignoreNext && clear(node.nextSibling, 'nextSibling');
  2691. !ignorePre && clear(node.previousSibling, 'previousSibling');
  2692. },
  2693. /**
  2694. * 将一个文本节点textNode拆分成两个文本节点,offset指定拆分位置
  2695. * @method split
  2696. * @param { Node } textNode 需要拆分的文本节点对象
  2697. * @param { int } offset 需要拆分的位置, 位置计算从0开始
  2698. * @return { Node } 拆分后形成的新节点
  2699. * @example
  2700. * ```html
  2701. * <div id="test">abcdef</div>
  2702. * <script>
  2703. * var newNode = UE.dom.domUtils.split( document.getElementById( "test" ).firstChild, 3 );
  2704. * //output: def
  2705. * console.log( newNode.nodeValue );
  2706. * </script>
  2707. * ```
  2708. */
  2709. split: function (node, offset) {
  2710. var doc = node.ownerDocument;
  2711. if (browser.ie && offset == node.nodeValue.length) {
  2712. var next = doc.createTextNode('');
  2713. return domUtils.insertAfter(node, next);
  2714. }
  2715. var retval = node.splitText(offset);
  2716. //ie8下splitText不会跟新childNodes,我们手动触发他的更新
  2717. if (browser.ie8) {
  2718. var tmpNode = doc.createTextNode('');
  2719. domUtils.insertAfter(retval, tmpNode);
  2720. domUtils.remove(tmpNode);
  2721. }
  2722. return retval;
  2723. },
  2724. /**
  2725. * 检测文本节点textNode是否为空节点(包括空格、换行、占位符等字符)
  2726. * @method isWhitespace
  2727. * @param { Node } node 需要检测的节点对象
  2728. * @return { Boolean } 检测的节点是否为空
  2729. * @example
  2730. * ```html
  2731. * <div id="test">
  2732. *
  2733. * </div>
  2734. * <script>
  2735. * //output: true
  2736. * console.log( UE.dom.domUtils.isWhitespace( document.getElementById("test").firstChild ) );
  2737. * </script>
  2738. * ```
  2739. */
  2740. isWhitespace: function (node) {
  2741. return !new RegExp('[^ \t\n\r' + domUtils.fillChar + ']').test(node.nodeValue);
  2742. },
  2743. /**
  2744. * 获取元素element相对于viewport的位置坐标
  2745. * @method getXY
  2746. * @param { Node } element 需要计算位置的节点对象
  2747. * @return { Object } 返回形如{x:left,y:top}的一个key-value映射对象, 其中键x代表水平偏移距离,
  2748. * y代表垂直偏移距离。
  2749. *
  2750. * @example
  2751. * ```javascript
  2752. * var location = UE.dom.domUtils.getXY( document.getElementById("test") );
  2753. * //output: test的坐标为: 12, 24
  2754. * console.log( 'test的坐标为: ', location.x, ',', location.y );
  2755. * ```
  2756. */
  2757. getXY: function (element) {
  2758. var x = 0,
  2759. y = 0;
  2760. while (element.offsetParent) {
  2761. y += element.offsetTop;
  2762. x += element.offsetLeft;
  2763. element = element.offsetParent;
  2764. }
  2765. return {
  2766. 'x': x,
  2767. 'y': y
  2768. };
  2769. },
  2770. /**
  2771. * 为元素element绑定原生DOM事件,type为事件类型,handler为处理函数
  2772. * @method on
  2773. * @param { Node } element 需要绑定事件的节点对象
  2774. * @param { String } type 绑定的事件类型
  2775. * @param { Function } handler 事件处理器
  2776. * @example
  2777. * ```javascript
  2778. * UE.dom.domUtils.on(document.body,"click",function(e){
  2779. * //e为事件对象,this为被点击元素对戏那个
  2780. * });
  2781. * ```
  2782. */
  2783. /**
  2784. * 为元素element绑定原生DOM事件,type为事件类型,handler为处理函数
  2785. * @method on
  2786. * @param { Node } element 需要绑定事件的节点对象
  2787. * @param { Array } type 绑定的事件类型数组
  2788. * @param { Function } handler 事件处理器
  2789. * @example
  2790. * ```javascript
  2791. * UE.dom.domUtils.on(document.body,["click","mousedown"],function(evt){
  2792. * //evt为事件对象,this为被点击元素对象
  2793. * });
  2794. * ```
  2795. */
  2796. on: function (element, type, handler) {
  2797. var types = utils.isArray(type) ? type : utils.trim(type).split(/\s+/),
  2798. k = types.length;
  2799. if (k)
  2800. while (k--) {
  2801. type = types[k];
  2802. if (element.addEventListener) {
  2803. element.addEventListener(type, handler, false);
  2804. } else {
  2805. if (!handler._d) {
  2806. handler._d = {
  2807. els: []
  2808. };
  2809. }
  2810. var key = type + handler.toString(),
  2811. index = utils.indexOf(handler._d.els, element);
  2812. if (!handler._d[key] || index == -1) {
  2813. if (index == -1) {
  2814. handler._d.els.push(element);
  2815. }
  2816. if (!handler._d[key]) {
  2817. handler._d[key] = function (evt) {
  2818. return handler.call(evt.srcElement, evt || window.event);
  2819. };
  2820. }
  2821. element.attachEvent('on' + type, handler._d[key]);
  2822. }
  2823. }
  2824. }
  2825. element = null;
  2826. },
  2827. /**
  2828. * 解除DOM事件绑定
  2829. * @method un
  2830. * @param { Node } element 需要解除事件绑定的节点对象
  2831. * @param { String } type 需要接触绑定的事件类型
  2832. * @param { Function } handler 对应的事件处理器
  2833. * @example
  2834. * ```javascript
  2835. * UE.dom.domUtils.un(document.body,"click",function(evt){
  2836. * //evt为事件对象,this为被点击元素对象
  2837. * });
  2838. * ```
  2839. */
  2840. /**
  2841. * 解除DOM事件绑定
  2842. * @method un
  2843. * @param { Node } element 需要解除事件绑定的节点对象
  2844. * @param { Array } type 需要接触绑定的事件类型数组
  2845. * @param { Function } handler 对应的事件处理器
  2846. * @example
  2847. * ```javascript
  2848. * UE.dom.domUtils.un(document.body, ["click","mousedown"],function(evt){
  2849. * //evt为事件对象,this为被点击元素对象
  2850. * });
  2851. * ```
  2852. */
  2853. un: function (element, type, handler) {
  2854. var types = utils.isArray(type) ? type : utils.trim(type).split(/\s+/),
  2855. k = types.length;
  2856. if (k)
  2857. while (k--) {
  2858. type = types[k];
  2859. if (element.removeEventListener) {
  2860. element.removeEventListener(type, handler, false);
  2861. } else {
  2862. var key = type + handler.toString();
  2863. try {
  2864. element.detachEvent('on' + type, handler._d ? handler._d[key] : handler);
  2865. } catch (e) {}
  2866. if (handler._d && handler._d[key]) {
  2867. var index = utils.indexOf(handler._d.els, element);
  2868. if (index != -1) {
  2869. handler._d.els.splice(index, 1);
  2870. }
  2871. handler._d.els.length == 0 && delete handler._d[key];
  2872. }
  2873. }
  2874. }
  2875. },
  2876. /**
  2877. * 比较节点nodeA与节点nodeB是否具有相同的标签名、属性名以及属性值
  2878. * @method isSameElement
  2879. * @param { Node } nodeA 需要比较的节点
  2880. * @param { Node } nodeB 需要比较的节点
  2881. * @return { Boolean } 两个节点是否具有相同的标签名、属性名以及属性值
  2882. * @example
  2883. * ```html
  2884. * <span style="font-size:12px">ssss</span>
  2885. * <span style="font-size:12px">bbbbb</span>
  2886. * <span style="font-size:13px">ssss</span>
  2887. * <span style="font-size:14px">bbbbb</span>
  2888. *
  2889. * <script>
  2890. *
  2891. * var nodes = document.getElementsByTagName( "span" );
  2892. *
  2893. * //output: true
  2894. * console.log( UE.dom.domUtils.isSameElement( nodes[0], nodes[1] ) );
  2895. *
  2896. * //output: false
  2897. * console.log( UE.dom.domUtils.isSameElement( nodes[2], nodes[3] ) );
  2898. *
  2899. * </script>
  2900. * ```
  2901. */
  2902. isSameElement: function (nodeA, nodeB) {
  2903. if (nodeA.tagName != nodeB.tagName) {
  2904. return false;
  2905. }
  2906. var thisAttrs = nodeA.attributes,
  2907. otherAttrs = nodeB.attributes;
  2908. if (!ie && thisAttrs.length != otherAttrs.length) {
  2909. return false;
  2910. }
  2911. var attrA, attrB, al = 0,
  2912. bl = 0;
  2913. for (var i = 0; attrA = thisAttrs[i++];) {
  2914. if (attrA.nodeName == 'style') {
  2915. if (attrA.specified) {
  2916. al++;
  2917. }
  2918. if (domUtils.isSameStyle(nodeA, nodeB)) {
  2919. continue;
  2920. } else {
  2921. return false;
  2922. }
  2923. }
  2924. if (ie) {
  2925. if (attrA.specified) {
  2926. al++;
  2927. attrB = otherAttrs.getNamedItem(attrA.nodeName);
  2928. } else {
  2929. continue;
  2930. }
  2931. } else {
  2932. attrB = nodeB.attributes[attrA.nodeName];
  2933. }
  2934. if (!attrB.specified || attrA.nodeValue != attrB.nodeValue) {
  2935. return false;
  2936. }
  2937. }
  2938. // 有可能attrB的属性包含了attrA的属性之外还有自己的属性
  2939. if (ie) {
  2940. for (i = 0; attrB = otherAttrs[i++];) {
  2941. if (attrB.specified) {
  2942. bl++;
  2943. }
  2944. }
  2945. if (al != bl) {
  2946. return false;
  2947. }
  2948. }
  2949. return true;
  2950. },
  2951. /**
  2952. * 判断节点nodeA与节点nodeB的元素的style属性是否一致
  2953. * @method isSameStyle
  2954. * @param { Node } nodeA 需要比较的节点
  2955. * @param { Node } nodeB 需要比较的节点
  2956. * @return { Boolean } 两个节点是否具有相同的style属性值
  2957. * @example
  2958. * ```html
  2959. * <span style="font-size:12px">ssss</span>
  2960. * <span style="font-size:12px">bbbbb</span>
  2961. * <span style="font-size:13px">ssss</span>
  2962. * <span style="font-size:14px">bbbbb</span>
  2963. *
  2964. * <script>
  2965. *
  2966. * var nodes = document.getElementsByTagName( "span" );
  2967. *
  2968. * //output: true
  2969. * console.log( UE.dom.domUtils.isSameStyle( nodes[0], nodes[1] ) );
  2970. *
  2971. * //output: false
  2972. * console.log( UE.dom.domUtils.isSameStyle( nodes[2], nodes[3] ) );
  2973. *
  2974. * </script>
  2975. * ```
  2976. */
  2977. isSameStyle: function (nodeA, nodeB) {
  2978. var styleA = nodeA.style.cssText.replace(/( ?; ?)/g, ';').replace(/( ?: ?)/g, ':'),
  2979. styleB = nodeB.style.cssText.replace(/( ?; ?)/g, ';').replace(/( ?: ?)/g, ':');
  2980. if (browser.opera) {
  2981. styleA = nodeA.style;
  2982. styleB = nodeB.style;
  2983. if (styleA.length != styleB.length)
  2984. return false;
  2985. for (var p in styleA) {
  2986. if (/^(\d+|csstext)$/i.test(p)) {
  2987. continue;
  2988. }
  2989. if (styleA[p] != styleB[p]) {
  2990. return false;
  2991. }
  2992. }
  2993. return true;
  2994. }
  2995. if (!styleA || !styleB) {
  2996. return styleA == styleB;
  2997. }
  2998. styleA = styleA.split(';');
  2999. styleB = styleB.split(';');
  3000. if (styleA.length != styleB.length) {
  3001. return false;
  3002. }
  3003. for (var i = 0, ci; ci = styleA[i++];) {
  3004. if (utils.indexOf(styleB, ci) == -1) {
  3005. return false;
  3006. }
  3007. }
  3008. return true;
  3009. },
  3010. /**
  3011. * 检查节点node是否为block元素
  3012. * @method isBlockElm
  3013. * @param { Node } node 需要检测的节点对象
  3014. * @return { Boolean } 是否是block元素节点
  3015. * @warning 该方法的判断规则如下: 如果该元素原本是block元素, 则不论该元素当前的css样式是什么都会返回true;
  3016. * 否则,检测该元素的css样式, 如果该元素当前是block元素, 则返回true。 其余情况下都返回false。
  3017. * @example
  3018. * ```html
  3019. * <span id="test1" style="display: block"></span>
  3020. * <span id="test2"></span>
  3021. * <div id="test3" style="display: inline"></div>
  3022. *
  3023. * <script>
  3024. *
  3025. * //output: true
  3026. * console.log( UE.dom.domUtils.isBlockElm( document.getElementById("test1") ) );
  3027. *
  3028. * //output: false
  3029. * console.log( UE.dom.domUtils.isBlockElm( document.getElementById("test2") ) );
  3030. *
  3031. * //output: true
  3032. * console.log( UE.dom.domUtils.isBlockElm( document.getElementById("test3") ) );
  3033. *
  3034. * </script>
  3035. * ```
  3036. */
  3037. isBlockElm: function (node) {
  3038. return node.nodeType == 1 && (dtd.$block[node.tagName] || styleBlock[domUtils.getComputedStyle(node, 'display')]) && !dtd.$nonChild[node.tagName];
  3039. },
  3040. /**
  3041. * 检测node节点是否为body节点
  3042. * @method isBody
  3043. * @param { Element } node 需要检测的dom元素
  3044. * @return { Boolean } 给定的元素是否是body元素
  3045. * @example
  3046. * ```javascript
  3047. * //output: true
  3048. * console.log( UE.dom.domUtils.isBody( document.body ) );
  3049. * ```
  3050. */
  3051. isBody: function (node) {
  3052. return node && node.nodeType == 1 && node.tagName.toLowerCase() == 'body';
  3053. },
  3054. /**
  3055. * 以node节点为分界,将该节点的指定祖先节点parent拆分成两个独立的节点,
  3056. * 拆分形成的两个节点之间是node节点
  3057. * @method breakParent
  3058. * @param { Node } node 作为分界的节点对象
  3059. * @param { Node } parent 该节点必须是node节点的祖先节点, 且是block节点。
  3060. * @return { Node } 给定的node分界节点
  3061. * @example
  3062. * ```javascript
  3063. *
  3064. * var node = document.createElement("span"),
  3065. * wrapNode = document.createElement( "div" ),
  3066. * parent = document.createElement("p");
  3067. *
  3068. * parent.appendChild( node );
  3069. * wrapNode.appendChild( parent );
  3070. *
  3071. * //拆分前
  3072. * //output: <p><span></span></p>
  3073. * console.log( wrapNode.innerHTML );
  3074. *
  3075. *
  3076. * UE.dom.domUtils.breakParent( node, parent );
  3077. * //拆分后
  3078. * //output: <p></p><span></span><p></p>
  3079. * console.log( wrapNode.innerHTML );
  3080. *
  3081. * ```
  3082. */
  3083. breakParent: function (node, parent) {
  3084. var tmpNode,
  3085. parentClone = node,
  3086. clone = node,
  3087. leftNodes,
  3088. rightNodes;
  3089. do {
  3090. parentClone = parentClone.parentNode;
  3091. if (leftNodes) {
  3092. tmpNode = parentClone.cloneNode(false);
  3093. tmpNode.appendChild(leftNodes);
  3094. leftNodes = tmpNode;
  3095. tmpNode = parentClone.cloneNode(false);
  3096. tmpNode.appendChild(rightNodes);
  3097. rightNodes = tmpNode;
  3098. } else {
  3099. leftNodes = parentClone.cloneNode(false);
  3100. rightNodes = leftNodes.cloneNode(false);
  3101. }
  3102. while (tmpNode = clone.previousSibling) {
  3103. leftNodes.insertBefore(tmpNode, leftNodes.firstChild);
  3104. }
  3105. while (tmpNode = clone.nextSibling) {
  3106. rightNodes.appendChild(tmpNode);
  3107. }
  3108. clone = parentClone;
  3109. } while (parent !== parentClone);
  3110. tmpNode = parent.parentNode;
  3111. tmpNode.insertBefore(leftNodes, parent);
  3112. tmpNode.insertBefore(rightNodes, parent);
  3113. tmpNode.insertBefore(node, rightNodes);
  3114. domUtils.remove(parent);
  3115. return node;
  3116. },
  3117. /**
  3118. * 检查节点node是否是空inline节点
  3119. * @method isEmptyInlineElement
  3120. * @param { Node } node 需要检测的节点对象
  3121. * @return { Number } 如果给定的节点是空的inline节点, 则返回1, 否则返回0。
  3122. * @example
  3123. * ```html
  3124. * <b><i></i></b> => 1
  3125. * <b><i></i><u></u></b> => 1
  3126. * <b></b> => 1
  3127. * <b>xx<i></i></b> => 0
  3128. * ```
  3129. */
  3130. isEmptyInlineElement: function (node) {
  3131. if (node.nodeType != 1 || !dtd.$removeEmpty[node.tagName]) {
  3132. return 0;
  3133. }
  3134. node = node.firstChild;
  3135. while (node) {
  3136. //如果是创建的bookmark就跳过
  3137. if (domUtils.isBookmarkNode(node)) {
  3138. return 0;
  3139. }
  3140. if (node.nodeType == 1 && !domUtils.isEmptyInlineElement(node) ||
  3141. node.nodeType == 3 && !domUtils.isWhitespace(node)
  3142. ) {
  3143. return 0;
  3144. }
  3145. node = node.nextSibling;
  3146. }
  3147. return 1;
  3148. },
  3149. /**
  3150. * 删除node节点下首尾两端的空白文本子节点
  3151. * @method trimWhiteTextNode
  3152. * @param { Element } node 需要执行删除操作的元素对象
  3153. * @example
  3154. * ```javascript
  3155. * var node = document.createElement("div");
  3156. *
  3157. * node.appendChild( document.createTextNode( "" ) );
  3158. *
  3159. * node.appendChild( document.createElement("div") );
  3160. *
  3161. * node.appendChild( document.createTextNode( "" ) );
  3162. *
  3163. * //3
  3164. * console.log( node.childNodes.length );
  3165. *
  3166. * UE.dom.domUtils.trimWhiteTextNode( node );
  3167. *
  3168. * //1
  3169. * console.log( node.childNodes.length );
  3170. * ```
  3171. */
  3172. trimWhiteTextNode: function (node) {
  3173. function remove(dir) {
  3174. var child;
  3175. while ((child = node[dir]) && child.nodeType == 3 && domUtils.isWhitespace(child)) {
  3176. node.removeChild(child);
  3177. }
  3178. }
  3179. remove('firstChild');
  3180. remove('lastChild');
  3181. },
  3182. /**
  3183. * 合并node节点下相同的子节点
  3184. * @name mergeChild
  3185. * @desc
  3186. * UE.dom.domUtils.mergeChild(node,tagName) //tagName要合并的子节点的标签
  3187. * @example
  3188. * <p><span style="font-size:12px;">xx<span style="font-size:12px;">aa</span>xx</span></p>
  3189. * ==> UE.dom.domUtils.mergeChild(node,'span')
  3190. * <p><span style="font-size:12px;">xxaaxx</span></p>
  3191. */
  3192. mergeChild: function (node, tagName, attrs) {
  3193. var list = domUtils.getElementsByTagName(node, node.tagName.toLowerCase());
  3194. for (var i = 0, ci; ci = list[i++];) {
  3195. if (!ci.parentNode || domUtils.isBookmarkNode(ci)) {
  3196. continue;
  3197. }
  3198. //span单独处理
  3199. if (ci.tagName.toLowerCase() == 'span') {
  3200. if (node === ci.parentNode) {
  3201. domUtils.trimWhiteTextNode(node);
  3202. if (node.childNodes.length == 1) {
  3203. node.style.cssText = ci.style.cssText + ";" + node.style.cssText;
  3204. domUtils.remove(ci, true);
  3205. continue;
  3206. }
  3207. }
  3208. ci.style.cssText = node.style.cssText + ';' + ci.style.cssText;
  3209. if (attrs) {
  3210. var style = attrs.style;
  3211. if (style) {
  3212. style = style.split(';');
  3213. for (var j = 0, s; s = style[j++];) {
  3214. ci.style[utils.cssStyleToDomStyle(s.split(':')[0])] = s.split(':')[1];
  3215. }
  3216. }
  3217. }
  3218. if (domUtils.isSameStyle(ci, node)) {
  3219. domUtils.remove(ci, true);
  3220. }
  3221. continue;
  3222. }
  3223. if (domUtils.isSameElement(node, ci)) {
  3224. domUtils.remove(ci, true);
  3225. }
  3226. }
  3227. },
  3228. /**
  3229. * 原生方法getElementsByTagName的封装
  3230. * @method getElementsByTagName
  3231. * @param { Node } node 目标节点对象
  3232. * @param { String } tagName 需要查找的节点的tagName, 多个tagName以空格分割
  3233. * @return { Array } 符合条件的节点集合
  3234. */
  3235. getElementsByTagName: function (node, name, filter) {
  3236. if (filter && utils.isString(filter)) {
  3237. var className = filter;
  3238. filter = function (node) {
  3239. return domUtils.hasClass(node, className)
  3240. }
  3241. }
  3242. name = utils.trim(name).replace(/[ ]{2,}/g, ' ').split(' ');
  3243. var arr = [];
  3244. for (var n = 0, ni; ni = name[n++];) {
  3245. var list = node.getElementsByTagName(ni);
  3246. for (var i = 0, ci; ci = list[i++];) {
  3247. if (!filter || filter(ci))
  3248. arr.push(ci);
  3249. }
  3250. }
  3251. return arr;
  3252. },
  3253. /**
  3254. * 将节点node提取到父节点上
  3255. * @method mergeToParent
  3256. * @param { Element } node 需要提取的元素对象
  3257. * @example
  3258. * ```html
  3259. * <div id="parent">
  3260. * <div id="sub">
  3261. * <span id="child"></span>
  3262. * </div>
  3263. * </div>
  3264. *
  3265. * <script>
  3266. *
  3267. * var child = document.getElementById( "child" );
  3268. *
  3269. * //output: sub
  3270. * console.log( child.parentNode.id );
  3271. *
  3272. * UE.dom.domUtils.mergeToParent( child );
  3273. *
  3274. * //output: parent
  3275. * console.log( child.parentNode.id );
  3276. *
  3277. * </script>
  3278. * ```
  3279. */
  3280. mergeToParent: function (node) {
  3281. var parent = node.parentNode;
  3282. while (parent && dtd.$removeEmpty[parent.tagName]) {
  3283. if (parent.tagName == node.tagName || parent.tagName == 'A') { //针对a标签单独处理
  3284. domUtils.trimWhiteTextNode(parent);
  3285. //span需要特殊处理 不处理这样的情况 <span stlye="color:#fff">xxx<span style="color:#ccc">xxx</span>xxx</span>
  3286. if (parent.tagName == 'SPAN' && !domUtils.isSameStyle(parent, node) ||
  3287. (parent.tagName == 'A' && node.tagName == 'SPAN')) {
  3288. if (parent.childNodes.length > 1 || parent !== node.parentNode) {
  3289. node.style.cssText = parent.style.cssText + ";" + node.style.cssText;
  3290. parent = parent.parentNode;
  3291. continue;
  3292. } else {
  3293. parent.style.cssText += ";" + node.style.cssText;
  3294. //trace:952 a标签要保持下划线
  3295. if (parent.tagName == 'A') {
  3296. parent.style.textDecoration = 'underline';
  3297. }
  3298. }
  3299. }
  3300. if (parent.tagName != 'A') {
  3301. parent === node.parentNode && domUtils.remove(node, true);
  3302. break;
  3303. }
  3304. }
  3305. parent = parent.parentNode;
  3306. }
  3307. },
  3308. /**
  3309. * 合并节点node的左右兄弟节点
  3310. * @method mergeSibling
  3311. * @param { Element } node 需要合并的目标节点
  3312. * @example
  3313. * ```html
  3314. * <b>xxxx</b><b id="test">ooo</b><b>xxxx</b>
  3315. *
  3316. * <script>
  3317. * var demoNode = document.getElementById("test");
  3318. * UE.dom.domUtils.mergeSibling( demoNode );
  3319. * //output: xxxxoooxxxx
  3320. * console.log( demoNode.innerHTML );
  3321. * </script>
  3322. * ```
  3323. */
  3324. /**
  3325. * 合并节点node的左右兄弟节点, 可以根据给定的条件选择是否忽略合并左节点。
  3326. * @method mergeSibling
  3327. * @param { Element } node 需要合并的目标节点
  3328. * @param { Boolean } ignorePre 是否忽略合并左节点
  3329. * @example
  3330. * ```html
  3331. * <b>xxxx</b><b id="test">ooo</b><b>xxxx</b>
  3332. *
  3333. * <script>
  3334. * var demoNode = document.getElementById("test");
  3335. * UE.dom.domUtils.mergeSibling( demoNode, true );
  3336. * //output: oooxxxx
  3337. * console.log( demoNode.innerHTML );
  3338. * </script>
  3339. * ```
  3340. */
  3341. /**
  3342. * 合并节点node的左右兄弟节点,可以根据给定的条件选择是否忽略合并左右节点。
  3343. * @method mergeSibling
  3344. * @param { Element } node 需要合并的目标节点
  3345. * @param { Boolean } ignorePre 是否忽略合并左节点
  3346. * @param { Boolean } ignoreNext 是否忽略合并右节点
  3347. * @remind 如果同时忽略左右节点, 则该操作什么也不会做
  3348. * @example
  3349. * ```html
  3350. * <b>xxxx</b><b id="test">ooo</b><b>xxxx</b>
  3351. *
  3352. * <script>
  3353. * var demoNode = document.getElementById("test");
  3354. * UE.dom.domUtils.mergeSibling( demoNode, false, true );
  3355. * //output: xxxxooo
  3356. * console.log( demoNode.innerHTML );
  3357. * </script>
  3358. * ```
  3359. */
  3360. mergeSibling: function (node, ignorePre, ignoreNext) {
  3361. function merge(rtl, start, node) {
  3362. var next;
  3363. if ((next = node[rtl]) && !domUtils.isBookmarkNode(next) && next.nodeType == 1 && domUtils.isSameElement(node, next)) {
  3364. while (next.firstChild) {
  3365. if (start == 'firstChild') {
  3366. node.insertBefore(next.lastChild, node.firstChild);
  3367. } else {
  3368. node.appendChild(next.firstChild);
  3369. }
  3370. }
  3371. domUtils.remove(next);
  3372. }
  3373. }!ignorePre && merge('previousSibling', 'firstChild', node);
  3374. !ignoreNext && merge('nextSibling', 'lastChild', node);
  3375. },
  3376. /**
  3377. * 设置节点node及其子节点不会被选中
  3378. * @method unSelectable
  3379. * @param { Element } node 需要执行操作的dom元素
  3380. * @remind 执行该操作后的节点, 将不能被鼠标选中
  3381. * @example
  3382. * ```javascript
  3383. * UE.dom.domUtils.unSelectable( document.body );
  3384. * ```
  3385. */
  3386. unSelectable: ie && browser.ie9below || browser.opera ? function (node) {
  3387. //for ie9
  3388. node.onselectstart = function () {
  3389. return false;
  3390. };
  3391. node.onclick = node.onkeyup = node.onkeydown = function () {
  3392. return false;
  3393. };
  3394. node.unselectable = 'on';
  3395. node.setAttribute("unselectable", "on");
  3396. for (var i = 0, ci; ci = node.all[i++];) {
  3397. switch (ci.tagName.toLowerCase()) {
  3398. case 'iframe':
  3399. case 'textarea':
  3400. case 'input':
  3401. case 'select':
  3402. break;
  3403. default:
  3404. ci.unselectable = 'on';
  3405. node.setAttribute("unselectable", "on");
  3406. }
  3407. }
  3408. } : function (node) {
  3409. node.style.MozUserSelect =
  3410. node.style.webkitUserSelect =
  3411. node.style.msUserSelect =
  3412. node.style.KhtmlUserSelect = 'none';
  3413. },
  3414. /**
  3415. * 删除节点node上的指定属性名称的属性
  3416. * @method removeAttributes
  3417. * @param { Node } node 需要删除属性的节点对象
  3418. * @param { String } attrNames 可以是空格隔开的多个属性名称,该操作将会依次删除相应的属性
  3419. * @example
  3420. * ```html
  3421. * <div id="wrap">
  3422. * <span style="font-size:14px;" id="test" name="followMe">xxxxx</span>
  3423. * </div>
  3424. *
  3425. * <script>
  3426. *
  3427. * UE.dom.domUtils.removeAttributes( document.getElementById( "test" ), "id name" );
  3428. *
  3429. * //output: <span style="font-size:14px;">xxxxx</span>
  3430. * console.log( document.getElementById("wrap").innerHTML );
  3431. *
  3432. * </script>
  3433. * ```
  3434. */
  3435. /**
  3436. * 删除节点node上的指定属性名称的属性
  3437. * @method removeAttributes
  3438. * @param { Node } node 需要删除属性的节点对象
  3439. * @param { Array } attrNames 需要删除的属性名数组
  3440. * @example
  3441. * ```html
  3442. * <div id="wrap">
  3443. * <span style="font-size:14px;" id="test" name="followMe">xxxxx</span>
  3444. * </div>
  3445. *
  3446. * <script>
  3447. *
  3448. * UE.dom.domUtils.removeAttributes( document.getElementById( "test" ), ["id", "name"] );
  3449. *
  3450. * //output: <span style="font-size:14px;">xxxxx</span>
  3451. * console.log( document.getElementById("wrap").innerHTML );
  3452. *
  3453. * </script>
  3454. * ```
  3455. */
  3456. removeAttributes: function (node, attrNames) {
  3457. attrNames = utils.isArray(attrNames) ? attrNames : utils.trim(attrNames).replace(/[ ]{2,}/g, ' ').split(' ');
  3458. for (var i = 0, ci; ci = attrNames[i++];) {
  3459. ci = attrFix[ci] || ci;
  3460. switch (ci) {
  3461. case 'className':
  3462. node[ci] = '';
  3463. break;
  3464. case 'style':
  3465. node.style.cssText = '';
  3466. var val = node.getAttributeNode('style');
  3467. !browser.ie && val && node.removeAttributeNode(val);
  3468. }
  3469. node.removeAttribute(ci);
  3470. }
  3471. },
  3472. /**
  3473. * 在doc下创建一个标签名为tag,属性为attrs的元素
  3474. * @method createElement
  3475. * @param { DomDocument } doc 新创建的元素属于该document节点创建
  3476. * @param { String } tagName 需要创建的元素的标签名
  3477. * @param { Object } attrs 新创建的元素的属性key-value集合
  3478. * @return { Element } 新创建的元素对象
  3479. * @example
  3480. * ```javascript
  3481. * var ele = UE.dom.domUtils.createElement( document, 'div', {
  3482. * id: 'test'
  3483. * } );
  3484. *
  3485. * //output: DIV
  3486. * console.log( ele.tagName );
  3487. *
  3488. * //output: test
  3489. * console.log( ele.id );
  3490. *
  3491. * ```
  3492. */
  3493. createElement: function (doc, tag, attrs) {
  3494. return domUtils.setAttributes(doc.createElement(tag), attrs)
  3495. },
  3496. /**
  3497. * 为节点node添加属性attrs,attrs为属性键值对
  3498. * @method setAttributes
  3499. * @param { Element } node 需要设置属性的元素对象
  3500. * @param { Object } attrs 需要设置的属性名-值对
  3501. * @return { Element } 设置属性的元素对象
  3502. * @example
  3503. * ```html
  3504. * <span id="test"></span>
  3505. *
  3506. * <script>
  3507. *
  3508. * var testNode = UE.dom.domUtils.setAttributes( document.getElementById( "test" ), {
  3509. * id: 'demo'
  3510. * } );
  3511. *
  3512. * //output: demo
  3513. * console.log( testNode.id );
  3514. *
  3515. * </script>
  3516. *
  3517. */
  3518. setAttributes: function (node, attrs) {
  3519. for (var attr in attrs) {
  3520. if (attrs.hasOwnProperty(attr)) {
  3521. var value = attrs[attr];
  3522. switch (attr) {
  3523. case 'class':
  3524. //ie下要这样赋值,setAttribute不起作用
  3525. node.className = value;
  3526. break;
  3527. case 'style':
  3528. node.style.cssText = node.style.cssText + ";" + value;
  3529. break;
  3530. case 'innerHTML':
  3531. node[attr] = value;
  3532. break;
  3533. case 'value':
  3534. node.value = value;
  3535. break;
  3536. default:
  3537. node.setAttribute(attrFix[attr] || attr, value);
  3538. }
  3539. }
  3540. }
  3541. return node;
  3542. },
  3543. /**
  3544. * 获取元素element经过计算后的样式值
  3545. * @method getComputedStyle
  3546. * @param { Element } element 需要获取样式的元素对象
  3547. * @param { String } styleName 需要获取的样式名
  3548. * @return { String } 获取到的样式值
  3549. * @example
  3550. * ```html
  3551. * <style type="text/css">
  3552. * #test {
  3553. * font-size: 15px;
  3554. * }
  3555. * </style>
  3556. *
  3557. * <span id="test"></span>
  3558. *
  3559. * <script>
  3560. * //output: 15px
  3561. * console.log( UE.dom.domUtils.getComputedStyle( document.getElementById( "test" ), 'font-size' ) );
  3562. * </script>
  3563. * ```
  3564. */
  3565. getComputedStyle: function (element, styleName) {
  3566. //一下的属性单独处理
  3567. var pros = 'width height top left';
  3568. if (pros.indexOf(styleName) > -1) {
  3569. return element['offset' + styleName.replace(/^\w/, function (s) {
  3570. return s.toUpperCase()
  3571. })] + 'px';
  3572. }
  3573. //忽略文本节点
  3574. if (element.nodeType == 3) {
  3575. element = element.parentNode;
  3576. }
  3577. //ie下font-size若body下定义了font-size,则从currentStyle里会取到这个font-size. 取不到实际值,故此修改.
  3578. if (browser.ie && browser.version < 9 && styleName == 'font-size' && !element.style.fontSize &&
  3579. !dtd.$empty[element.tagName] && !dtd.$nonChild[element.tagName]) {
  3580. var span = element.ownerDocument.createElement('span');
  3581. span.style.cssText = 'padding:0;border:0;font-family:simsun;';
  3582. span.innerHTML = '.';
  3583. element.appendChild(span);
  3584. var result = span.offsetHeight;
  3585. element.removeChild(span);
  3586. span = null;
  3587. return result + 'px';
  3588. }
  3589. try {
  3590. var value = domUtils.getStyle(element, styleName) ||
  3591. (window.getComputedStyle ? domUtils.getWindow(element).getComputedStyle(element, '').getPropertyValue(styleName) :
  3592. (element.currentStyle || element.style)[utils.cssStyleToDomStyle(styleName)]);
  3593. } catch (e) {
  3594. return "";
  3595. }
  3596. return utils.transUnitToPx(utils.fixColor(styleName, value));
  3597. },
  3598. /**
  3599. * 删除元素element指定的className
  3600. * @method removeClasses
  3601. * @param { Element } ele 需要删除class的元素节点
  3602. * @param { String } classNames 需要删除的className, 多个className之间以空格分开
  3603. * @example
  3604. * ```html
  3605. * <span id="test" class="test1 test2 test3">xxx</span>
  3606. *
  3607. * <script>
  3608. *
  3609. * var testNode = document.getElementById( "test" );
  3610. * UE.dom.domUtils.removeClasses( testNode, "test1 test2" );
  3611. *
  3612. * //output: test3
  3613. * console.log( testNode.className );
  3614. *
  3615. * </script>
  3616. * ```
  3617. */
  3618. /**
  3619. * 删除元素element指定的className
  3620. * @method removeClasses
  3621. * @param { Element } ele 需要删除class的元素节点
  3622. * @param { Array } classNames 需要删除的className数组
  3623. * @example
  3624. * ```html
  3625. * <span id="test" class="test1 test2 test3">xxx</span>
  3626. *
  3627. * <script>
  3628. *
  3629. * var testNode = document.getElementById( "test" );
  3630. * UE.dom.domUtils.removeClasses( testNode, ["test1", "test2"] );
  3631. *
  3632. * //output: test3
  3633. * console.log( testNode.className );
  3634. *
  3635. * </script>
  3636. * ```
  3637. */
  3638. removeClasses: function (elm, classNames) {
  3639. classNames = utils.isArray(classNames) ? classNames :
  3640. utils.trim(classNames).replace(/[ ]{2,}/g, ' ').split(' ');
  3641. for (var i = 0, ci, cls = elm.className; ci = classNames[i++];) {
  3642. cls = cls.replace(new RegExp('\\b' + ci + '\\b'), '')
  3643. }
  3644. cls = utils.trim(cls).replace(/[ ]{2,}/g, ' ');
  3645. if (cls) {
  3646. elm.className = cls;
  3647. } else {
  3648. domUtils.removeAttributes(elm, ['class']);
  3649. }
  3650. },
  3651. /**
  3652. * 给元素element添加className
  3653. * @method addClass
  3654. * @param { Node } ele 需要增加className的元素
  3655. * @param { String } classNames 需要添加的className, 多个className之间以空格分割
  3656. * @remind 相同的类名不会被重复添加
  3657. * @example
  3658. * ```html
  3659. * <span id="test" class="cls1 cls2"></span>
  3660. *
  3661. * <script>
  3662. * var testNode = document.getElementById("test");
  3663. *
  3664. * UE.dom.domUtils.addClass( testNode, "cls2 cls3 cls4" );
  3665. *
  3666. * //output: cl1 cls2 cls3 cls4
  3667. * console.log( testNode.className );
  3668. *
  3669. * <script>
  3670. * ```
  3671. */
  3672. /**
  3673. * 给元素element添加className
  3674. * @method addClass
  3675. * @param { Node } ele 需要增加className的元素
  3676. * @param { Array } classNames 需要添加的className的数组
  3677. * @remind 相同的类名不会被重复添加
  3678. * @example
  3679. * ```html
  3680. * <span id="test" class="cls1 cls2"></span>
  3681. *
  3682. * <script>
  3683. * var testNode = document.getElementById("test");
  3684. *
  3685. * UE.dom.domUtils.addClass( testNode, ["cls2", "cls3", "cls4"] );
  3686. *
  3687. * //output: cl1 cls2 cls3 cls4
  3688. * console.log( testNode.className );
  3689. *
  3690. * <script>
  3691. * ```
  3692. */
  3693. addClass: function (elm, classNames) {
  3694. if (!elm) return;
  3695. classNames = utils.trim(classNames).replace(/[ ]{2,}/g, ' ').split(' ');
  3696. for (var i = 0, ci, cls = elm.className; ci = classNames[i++];) {
  3697. if (!new RegExp('\\b' + ci + '\\b').test(cls)) {
  3698. cls += ' ' + ci;
  3699. }
  3700. }
  3701. elm.className = utils.trim(cls);
  3702. },
  3703. /**
  3704. * 判断元素element是否包含给定的样式类名className
  3705. * @method hasClass
  3706. * @param { Node } ele 需要检测的元素
  3707. * @param { String } classNames 需要检测的className, 多个className之间用空格分割
  3708. * @return { Boolean } 元素是否包含所有给定的className
  3709. * @example
  3710. * ```html
  3711. * <span id="test1" class="cls1 cls2"></span>
  3712. *
  3713. * <script>
  3714. * var test1 = document.getElementById("test1");
  3715. *
  3716. * //output: false
  3717. * console.log( UE.dom.domUtils.hasClass( test1, "cls2 cls1 cls3" ) );
  3718. *
  3719. * //output: true
  3720. * console.log( UE.dom.domUtils.hasClass( test1, "cls2 cls1" ) );
  3721. * </script>
  3722. * ```
  3723. */
  3724. /**
  3725. * 判断元素element是否包含给定的样式类名className
  3726. * @method hasClass
  3727. * @param { Node } ele 需要检测的元素
  3728. * @param { Array } classNames 需要检测的className数组
  3729. * @return { Boolean } 元素是否包含所有给定的className
  3730. * @example
  3731. * ```html
  3732. * <span id="test1" class="cls1 cls2"></span>
  3733. *
  3734. * <script>
  3735. * var test1 = document.getElementById("test1");
  3736. *
  3737. * //output: false
  3738. * console.log( UE.dom.domUtils.hasClass( test1, [ "cls2", "cls1", "cls3" ] ) );
  3739. *
  3740. * //output: true
  3741. * console.log( UE.dom.domUtils.hasClass( test1, [ "cls2", "cls1" ]) );
  3742. * </script>
  3743. * ```
  3744. */
  3745. hasClass: function (element, className) {
  3746. if (utils.isRegExp(className)) {
  3747. return className.test(element.className)
  3748. }
  3749. className = utils.trim(className).replace(/[ ]{2,}/g, ' ').split(' ');
  3750. for (var i = 0, ci, cls = element.className; ci = className[i++];) {
  3751. if (!new RegExp('\\b' + ci + '\\b', 'i').test(cls)) {
  3752. return false;
  3753. }
  3754. }
  3755. return i - 1 == className.length;
  3756. },
  3757. /**
  3758. * 阻止事件默认行为
  3759. * @method preventDefault
  3760. * @param { Event } evt 需要阻止默认行为的事件对象
  3761. * @example
  3762. * ```javascript
  3763. * UE.dom.domUtils.preventDefault( evt );
  3764. * ```
  3765. */
  3766. preventDefault: function (evt) {
  3767. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
  3768. },
  3769. /**
  3770. * 删除元素element指定的样式
  3771. * @method removeStyle
  3772. * @param { Element } element 需要删除样式的元素
  3773. * @param { String } styleName 需要删除的样式名
  3774. * @example
  3775. * ```html
  3776. * <span id="test" style="color: red; background: blue;"></span>
  3777. *
  3778. * <script>
  3779. *
  3780. * var testNode = document.getElementById("test");
  3781. *
  3782. * UE.dom.domUtils.removeStyle( testNode, 'color' );
  3783. *
  3784. * //output: background: blue;
  3785. * console.log( testNode.style.cssText );
  3786. *
  3787. * </script>
  3788. * ```
  3789. */
  3790. removeStyle: function (element, name) {
  3791. if (browser.ie) {
  3792. //针对color先单独处理一下
  3793. if (name == 'color') {
  3794. name = '(^|;)' + name;
  3795. }
  3796. element.style.cssText = element.style.cssText.replace(new RegExp(name + '[^:]*:[^;]+;?', 'ig'), '')
  3797. } else {
  3798. if (element.style.removeProperty) {
  3799. element.style.removeProperty(name);
  3800. } else {
  3801. element.style.removeAttribute(utils.cssStyleToDomStyle(name));
  3802. }
  3803. }
  3804. if (!element.style.cssText) {
  3805. domUtils.removeAttributes(element, ['style']);
  3806. }
  3807. },
  3808. /**
  3809. * 获取元素element的style属性的指定值
  3810. * @method getStyle
  3811. * @param { Element } element 需要获取属性值的元素
  3812. * @param { String } styleName 需要获取的style的名称
  3813. * @warning 该方法仅获取元素style属性中所标明的值
  3814. * @return { String } 该元素包含指定的style属性值
  3815. * @example
  3816. * ```html
  3817. * <div id="test" style="color: red;"></div>
  3818. *
  3819. * <script>
  3820. *
  3821. * var testNode = document.getElementById( "test" );
  3822. *
  3823. * //output: red
  3824. * console.log( UE.dom.domUtils.getStyle( testNode, "color" ) );
  3825. *
  3826. * //output: ""
  3827. * console.log( UE.dom.domUtils.getStyle( testNode, "background" ) );
  3828. *
  3829. * </script>
  3830. * ```
  3831. */
  3832. getStyle: function (element, name) {
  3833. var value = element.style[utils.cssStyleToDomStyle(name)];
  3834. return utils.fixColor(name, value);
  3835. },
  3836. /**
  3837. * 为元素element设置样式属性值
  3838. * @method setStyle
  3839. * @param { Element } element 需要设置样式的元素
  3840. * @param { String } styleName 样式名
  3841. * @param { String } styleValue 样式值
  3842. * @example
  3843. * ```html
  3844. * <div id="test"></div>
  3845. *
  3846. * <script>
  3847. *
  3848. * var testNode = document.getElementById( "test" );
  3849. *
  3850. * //output: ""
  3851. * console.log( testNode.style.color );
  3852. *
  3853. * UE.dom.domUtils.setStyle( testNode, 'color', 'red' );
  3854. * //output: "red"
  3855. * console.log( testNode.style.color );
  3856. *
  3857. * </script>
  3858. * ```
  3859. */
  3860. setStyle: function (element, name, value) {
  3861. element.style[utils.cssStyleToDomStyle(name)] = value;
  3862. if (!utils.trim(element.style.cssText)) {
  3863. this.removeAttributes(element, 'style')
  3864. }
  3865. },
  3866. /**
  3867. * 为元素element设置多个样式属性值
  3868. * @method setStyles
  3869. * @param { Element } element 需要设置样式的元素
  3870. * @param { Object } styles 样式名值对
  3871. * @example
  3872. * ```html
  3873. * <div id="test"></div>
  3874. *
  3875. * <script>
  3876. *
  3877. * var testNode = document.getElementById( "test" );
  3878. *
  3879. * //output: ""
  3880. * console.log( testNode.style.color );
  3881. *
  3882. * UE.dom.domUtils.setStyles( testNode, {
  3883. * 'color': 'red'
  3884. * } );
  3885. * //output: "red"
  3886. * console.log( testNode.style.color );
  3887. *
  3888. * </script>
  3889. * ```
  3890. */
  3891. setStyles: function (element, styles) {
  3892. for (var name in styles) {
  3893. if (styles.hasOwnProperty(name)) {
  3894. domUtils.setStyle(element, name, styles[name]);
  3895. }
  3896. }
  3897. },
  3898. /**
  3899. * 删除_moz_dirty属性
  3900. * @private
  3901. * @method removeDirtyAttr
  3902. */
  3903. removeDirtyAttr: function (node) {
  3904. for (var i = 0, ci, nodes = node.getElementsByTagName('*'); ci = nodes[i++];) {
  3905. ci.removeAttribute('_moz_dirty');
  3906. }
  3907. node.removeAttribute('_moz_dirty');
  3908. },
  3909. /**
  3910. * 获取子节点的数量
  3911. * @method getChildCount
  3912. * @param { Element } node 需要检测的元素
  3913. * @return { Number } 给定的node元素的子节点数量
  3914. * @example
  3915. * ```html
  3916. * <div id="test">
  3917. * <span></span>
  3918. * </div>
  3919. *
  3920. * <script>
  3921. *
  3922. * //output: 3
  3923. * console.log( UE.dom.domUtils.getChildCount( document.getElementById("test") ) );
  3924. *
  3925. * </script>
  3926. * ```
  3927. */
  3928. /**
  3929. * 根据给定的过滤规则, 获取符合条件的子节点的数量
  3930. * @method getChildCount
  3931. * @param { Element } node 需要检测的元素
  3932. * @param { Function } fn 过滤器, 要求对符合条件的子节点返回true, 反之则要求返回false
  3933. * @return { Number } 符合过滤条件的node元素的子节点数量
  3934. * @example
  3935. * ```html
  3936. * <div id="test">
  3937. * <span></span>
  3938. * </div>
  3939. *
  3940. * <script>
  3941. *
  3942. * //output: 1
  3943. * console.log( UE.dom.domUtils.getChildCount( document.getElementById("test"), function ( node ) {
  3944. *
  3945. * return node.nodeType === 1;
  3946. *
  3947. * } ) );
  3948. *
  3949. * </script>
  3950. * ```
  3951. */
  3952. getChildCount: function (node, fn) {
  3953. var count = 0,
  3954. first = node.firstChild;
  3955. fn = fn || function () {
  3956. return 1;
  3957. };
  3958. while (first) {
  3959. if (fn(first)) {
  3960. count++;
  3961. }
  3962. first = first.nextSibling;
  3963. }
  3964. return count;
  3965. },
  3966. /**
  3967. * 判断给定节点是否为空节点
  3968. * @method isEmptyNode
  3969. * @param { Node } node 需要检测的节点对象
  3970. * @return { Boolean } 节点是否为空
  3971. * @example
  3972. * ```javascript
  3973. * UE.dom.domUtils.isEmptyNode( document.body );
  3974. * ```
  3975. */
  3976. isEmptyNode: function (node) {
  3977. return !node.firstChild || domUtils.getChildCount(node, function (node) {
  3978. return !domUtils.isBr(node) && !domUtils.isBookmarkNode(node) && !domUtils.isWhitespace(node)
  3979. }) == 0
  3980. },
  3981. clearSelectedArr: function (nodes) {
  3982. var node;
  3983. while (node = nodes.pop()) {
  3984. domUtils.removeAttributes(node, ['class']);
  3985. }
  3986. },
  3987. /**
  3988. * 将显示区域滚动到指定节点的位置
  3989. * @method scrollToView
  3990. * @param {Node} node 节点
  3991. * @param {window} win window对象
  3992. * @param {Number} offsetTop 距离上方的偏移量
  3993. */
  3994. scrollToView: function (node, win, offsetTop) {
  3995. var getViewPaneSize = function () {
  3996. var doc = win.document,
  3997. mode = doc.compatMode == 'CSS1Compat';
  3998. return {
  3999. width: (mode ? doc.documentElement.clientWidth : doc.body.clientWidth) || 0,
  4000. height: (mode ? doc.documentElement.clientHeight : doc.body.clientHeight) || 0
  4001. };
  4002. },
  4003. getScrollPosition = function (win) {
  4004. if ('pageXOffset' in win) {
  4005. return {
  4006. x: win.pageXOffset || 0,
  4007. y: win.pageYOffset || 0
  4008. };
  4009. } else {
  4010. var doc = win.document;
  4011. return {
  4012. x: doc.documentElement.scrollLeft || doc.body.scrollLeft || 0,
  4013. y: doc.documentElement.scrollTop || doc.body.scrollTop || 0
  4014. };
  4015. }
  4016. };
  4017. var winHeight = getViewPaneSize().height,
  4018. offset = winHeight * -1 + offsetTop;
  4019. offset += (node.offsetHeight || 0);
  4020. var elementPosition = domUtils.getXY(node);
  4021. offset += elementPosition.y;
  4022. var currentScroll = getScrollPosition(win).y;
  4023. // offset += 50;
  4024. if (offset > currentScroll || offset < currentScroll - winHeight) {
  4025. win.scrollTo(0, offset + (offset < 0 ? -20 : 20));
  4026. }
  4027. },
  4028. /**
  4029. * 判断给定节点是否为br
  4030. * @method isBr
  4031. * @param { Node } node 需要判断的节点对象
  4032. * @return { Boolean } 给定的节点是否是br节点
  4033. */
  4034. isBr: function (node) {
  4035. return node.nodeType == 1 && node.tagName == 'BR';
  4036. },
  4037. /**
  4038. * 判断给定的节点是否是一个“填充”节点
  4039. * @private
  4040. * @method isFillChar
  4041. * @param { Node } node 需要判断的节点
  4042. * @param { Boolean } isInStart 是否从节点内容的开始位置匹配
  4043. * @returns { Boolean } 节点是否是填充节点
  4044. */
  4045. isFillChar: function (node, isInStart) {
  4046. if (node.nodeType != 3)
  4047. return false;
  4048. var text = node.nodeValue;
  4049. if (isInStart) {
  4050. return new RegExp('^' + domUtils.fillChar).test(text)
  4051. }
  4052. return !text.replace(new RegExp(domUtils.fillChar, 'g'), '').length
  4053. },
  4054. isStartInblock: function (range) {
  4055. var tmpRange = range.cloneRange(),
  4056. flag = 0,
  4057. start = tmpRange.startContainer,
  4058. tmp;
  4059. if (start.nodeType == 1 && start.childNodes[tmpRange.startOffset]) {
  4060. start = start.childNodes[tmpRange.startOffset];
  4061. var pre = start.previousSibling;
  4062. while (pre && domUtils.isFillChar(pre)) {
  4063. start = pre;
  4064. pre = pre.previousSibling;
  4065. }
  4066. }
  4067. if (this.isFillChar(start, true) && tmpRange.startOffset == 1) {
  4068. tmpRange.setStartBefore(start);
  4069. start = tmpRange.startContainer;
  4070. }
  4071. while (start && domUtils.isFillChar(start)) {
  4072. tmp = start;
  4073. start = start.previousSibling
  4074. }
  4075. if (tmp) {
  4076. tmpRange.setStartBefore(tmp);
  4077. start = tmpRange.startContainer;
  4078. }
  4079. if (start.nodeType == 1 && domUtils.isEmptyNode(start) && tmpRange.startOffset == 1) {
  4080. tmpRange.setStart(start, 0).collapse(true);
  4081. }
  4082. while (!tmpRange.startOffset) {
  4083. start = tmpRange.startContainer;
  4084. if (domUtils.isBlockElm(start) || domUtils.isBody(start)) {
  4085. flag = 1;
  4086. break;
  4087. }
  4088. var pre = tmpRange.startContainer.previousSibling,
  4089. tmpNode;
  4090. if (!pre) {
  4091. tmpRange.setStartBefore(tmpRange.startContainer);
  4092. } else {
  4093. while (pre && domUtils.isFillChar(pre)) {
  4094. tmpNode = pre;
  4095. pre = pre.previousSibling;
  4096. }
  4097. if (tmpNode) {
  4098. tmpRange.setStartBefore(tmpNode);
  4099. } else {
  4100. tmpRange.setStartBefore(tmpRange.startContainer);
  4101. }
  4102. }
  4103. }
  4104. return flag && !domUtils.isBody(tmpRange.startContainer) ? 1 : 0;
  4105. },
  4106. /**
  4107. * 判断给定的元素是否是一个空元素
  4108. * @method isEmptyBlock
  4109. * @param { Element } node 需要判断的元素
  4110. * @return { Boolean } 是否是空元素
  4111. * @example
  4112. * ```html
  4113. * <div id="test"></div>
  4114. *
  4115. * <script>
  4116. * //output: true
  4117. * console.log( UE.dom.domUtils.isEmptyBlock( document.getElementById("test") ) );
  4118. * </script>
  4119. * ```
  4120. */
  4121. /**
  4122. * 根据指定的判断规则判断给定的元素是否是一个空元素
  4123. * @method isEmptyBlock
  4124. * @param { Element } node 需要判断的元素
  4125. * @param { RegExp } reg 对内容执行判断的正则表达式对象
  4126. * @return { Boolean } 是否是空元素
  4127. */
  4128. isEmptyBlock: function (node, reg) {
  4129. // HaoChuan9421
  4130. if (!node) {
  4131. return;
  4132. }
  4133. if (node.nodeType != 1)
  4134. return 0;
  4135. reg = reg || new RegExp('[ \xa0\t\r\n' + domUtils.fillChar + ']', 'g');
  4136. if (node[browser.ie ? 'innerText' : 'textContent'].replace(reg, '').length > 0) {
  4137. return 0;
  4138. }
  4139. for (var n in dtd.$isNotEmpty) {
  4140. if (node.getElementsByTagName(n).length) {
  4141. return 0;
  4142. }
  4143. }
  4144. return 1;
  4145. },
  4146. /**
  4147. * 移动元素使得该元素的位置移动指定的偏移量的距离
  4148. * @method setViewportOffset
  4149. * @param { Element } element 需要设置偏移量的元素
  4150. * @param { Object } offset 偏移量, 形如{ left: 100, top: 50 }的一个键值对, 表示该元素将在
  4151. * 现有的位置上向水平方向偏移offset.left的距离, 在竖直方向上偏移
  4152. * offset.top的距离
  4153. * @example
  4154. * ```html
  4155. * <div id="test" style="top: 100px; left: 50px; position: absolute;"></div>
  4156. *
  4157. * <script>
  4158. *
  4159. * var testNode = document.getElementById("test");
  4160. *
  4161. * UE.dom.domUtils.setViewportOffset( testNode, {
  4162. * left: 200,
  4163. * top: 50
  4164. * } );
  4165. *
  4166. * //output: top: 300px; left: 100px; position: absolute;
  4167. * console.log( testNode.style.cssText );
  4168. *
  4169. * </script>
  4170. * ```
  4171. */
  4172. setViewportOffset: function (element, offset) {
  4173. var left = parseInt(element.style.left) | 0;
  4174. var top = parseInt(element.style.top) | 0;
  4175. var rect = element.getBoundingClientRect();
  4176. var offsetLeft = offset.left - rect.left;
  4177. var offsetTop = offset.top - rect.top;
  4178. if (offsetLeft) {
  4179. element.style.left = left + offsetLeft + 'px';
  4180. }
  4181. if (offsetTop) {
  4182. element.style.top = top + offsetTop + 'px';
  4183. }
  4184. },
  4185. /**
  4186. * 用“填充字符”填充节点
  4187. * @method fillNode
  4188. * @private
  4189. * @param { DomDocument } doc 填充的节点所在的docment对象
  4190. * @param { Node } node 需要填充的节点对象
  4191. * @example
  4192. * ```html
  4193. * <div id="test"></div>
  4194. *
  4195. * <script>
  4196. * var testNode = document.getElementById("test");
  4197. *
  4198. * //output: 0
  4199. * console.log( testNode.childNodes.length );
  4200. *
  4201. * UE.dom.domUtils.fillNode( document, testNode );
  4202. *
  4203. * //output: 1
  4204. * console.log( testNode.childNodes.length );
  4205. *
  4206. * </script>
  4207. * ```
  4208. */
  4209. fillNode: function (doc, node) {
  4210. var tmpNode = browser.ie ? doc.createTextNode(domUtils.fillChar) : doc.createElement('br');
  4211. node.innerHTML = '';
  4212. node.appendChild(tmpNode);
  4213. },
  4214. /**
  4215. * 把节点src的所有子节点追加到另一个节点tag上去
  4216. * @method moveChild
  4217. * @param { Node } src 源节点, 该节点下的所有子节点将被移除
  4218. * @param { Node } tag 目标节点, 从源节点移除的子节点将被追加到该节点下
  4219. * @example
  4220. * ```html
  4221. * <div id="test1">
  4222. * <span></span>
  4223. * </div>
  4224. * <div id="test2">
  4225. * <div></div>
  4226. * </div>
  4227. *
  4228. * <script>
  4229. *
  4230. * var test1 = document.getElementById("test1"),
  4231. * test2 = document.getElementById("test2");
  4232. *
  4233. * UE.dom.domUtils.moveChild( test1, test2 );
  4234. *
  4235. * //output: ""(空字符串)
  4236. * console.log( test1.innerHTML );
  4237. *
  4238. * //output: "<div></div><span></span>"
  4239. * console.log( test2.innerHTML );
  4240. *
  4241. * </script>
  4242. * ```
  4243. */
  4244. /**
  4245. * 把节点src的所有子节点移动到另一个节点tag上去, 可以通过dir参数控制附加的行为是“追加”还是“插入顶部”
  4246. * @method moveChild
  4247. * @param { Node } src 源节点, 该节点下的所有子节点将被移除
  4248. * @param { Node } tag 目标节点, 从源节点移除的子节点将被附加到该节点下
  4249. * @param { Boolean } dir 附加方式, 如果为true, 则附加进去的节点将被放到目标节点的顶部, 反之,则放到末尾
  4250. * @example
  4251. * ```html
  4252. * <div id="test1">
  4253. * <span></span>
  4254. * </div>
  4255. * <div id="test2">
  4256. * <div></div>
  4257. * </div>
  4258. *
  4259. * <script>
  4260. *
  4261. * var test1 = document.getElementById("test1"),
  4262. * test2 = document.getElementById("test2");
  4263. *
  4264. * UE.dom.domUtils.moveChild( test1, test2, true );
  4265. *
  4266. * //output: ""(空字符串)
  4267. * console.log( test1.innerHTML );
  4268. *
  4269. * //output: "<span></span><div></div>"
  4270. * console.log( test2.innerHTML );
  4271. *
  4272. * </script>
  4273. * ```
  4274. */
  4275. moveChild: function (src, tag, dir) {
  4276. while (src.firstChild) {
  4277. if (dir && tag.firstChild) {
  4278. tag.insertBefore(src.lastChild, tag.firstChild);
  4279. } else {
  4280. tag.appendChild(src.firstChild);
  4281. }
  4282. }
  4283. },
  4284. /**
  4285. * 判断节点的标签上是否不存在任何属性
  4286. * @method hasNoAttributes
  4287. * @private
  4288. * @param { Node } node 需要检测的节点对象
  4289. * @return { Boolean } 节点是否不包含任何属性
  4290. * @example
  4291. * ```html
  4292. * <div id="test"><span>xxxx</span></div>
  4293. *
  4294. * <script>
  4295. *
  4296. * //output: false
  4297. * console.log( UE.dom.domUtils.hasNoAttributes( document.getElementById("test") ) );
  4298. *
  4299. * //output: true
  4300. * console.log( UE.dom.domUtils.hasNoAttributes( document.getElementById("test").firstChild ) );
  4301. *
  4302. * </script>
  4303. * ```
  4304. */
  4305. hasNoAttributes: function (node) {
  4306. return browser.ie ? /^<\w+\s*?>/.test(node.outerHTML) : node.attributes.length == 0;
  4307. },
  4308. /**
  4309. * 检测节点是否是UEditor所使用的辅助节点
  4310. * @method isCustomeNode
  4311. * @private
  4312. * @param { Node } node 需要检测的节点
  4313. * @remind 辅助节点是指编辑器要完成工作临时添加的节点, 在输出的时候将会从编辑器内移除, 不会影响最终的结果。
  4314. * @return { Boolean } 给定的节点是否是一个辅助节点
  4315. */
  4316. isCustomeNode: function (node) {
  4317. return node.nodeType == 1 && node.getAttribute('_ue_custom_node_');
  4318. },
  4319. /**
  4320. * 检测节点的标签是否是给定的标签
  4321. * @method isTagNode
  4322. * @param { Node } node 需要检测的节点对象
  4323. * @param { String } tagName 标签
  4324. * @return { Boolean } 节点的标签是否是给定的标签
  4325. * @example
  4326. * ```html
  4327. * <div id="test"></div>
  4328. *
  4329. * <script>
  4330. *
  4331. * //output: true
  4332. * console.log( UE.dom.domUtils.isTagNode( document.getElementById("test"), "div" ) );
  4333. *
  4334. * </script>
  4335. * ```
  4336. */
  4337. isTagNode: function (node, tagNames) {
  4338. return node.nodeType == 1 && new RegExp('\\b' + node.tagName + '\\b', 'i').test(tagNames)
  4339. },
  4340. /**
  4341. * 给定一个节点数组,在通过指定的过滤器过滤后, 获取其中满足过滤条件的第一个节点
  4342. * @method filterNodeList
  4343. * @param { Array } nodeList 需要过滤的节点数组
  4344. * @param { Function } fn 过滤器, 对符合条件的节点, 执行结果返回true, 反之则返回false
  4345. * @return { Node | NULL } 如果找到符合过滤条件的节点, 则返回该节点, 否则返回NULL
  4346. * @example
  4347. * ```javascript
  4348. * var divNodes = document.getElementsByTagName("div");
  4349. * divNodes = [].slice.call( divNodes, 0 );
  4350. *
  4351. * //output: null
  4352. * console.log( UE.dom.domUtils.filterNodeList( divNodes, function ( node ) {
  4353. * return node.tagName.toLowerCase() !== 'div';
  4354. * } ) );
  4355. * ```
  4356. */
  4357. /**
  4358. * 给定一个节点数组nodeList和一组标签名tagNames, 获取其中能够匹配标签名的节点集合中的第一个节点
  4359. * @method filterNodeList
  4360. * @param { Array } nodeList 需要过滤的节点数组
  4361. * @param { String } tagNames 需要匹配的标签名, 多个标签名之间用空格分割
  4362. * @return { Node | NULL } 如果找到标签名匹配的节点, 则返回该节点, 否则返回NULL
  4363. * @example
  4364. * ```javascript
  4365. * var divNodes = document.getElementsByTagName("div");
  4366. * divNodes = [].slice.call( divNodes, 0 );
  4367. *
  4368. * //output: null
  4369. * console.log( UE.dom.domUtils.filterNodeList( divNodes, 'a span' ) );
  4370. * ```
  4371. */
  4372. /**
  4373. * 给定一个节点数组,在通过指定的过滤器过滤后, 如果参数forAll为true, 则会返回所有满足过滤
  4374. * 条件的节点集合, 否则, 返回满足条件的节点集合中的第一个节点
  4375. * @method filterNodeList
  4376. * @param { Array } nodeList 需要过滤的节点数组
  4377. * @param { Function } fn 过滤器, 对符合条件的节点, 执行结果返回true, 反之则返回false
  4378. * @param { Boolean } forAll 是否返回整个节点数组, 如果该参数为false, 则返回节点集合中的第一个节点
  4379. * @return { Array | Node | NULL } 如果找到符合过滤条件的节点, 则根据参数forAll的值决定返回满足
  4380. * 过滤条件的节点数组或第一个节点, 否则返回NULL
  4381. * @example
  4382. * ```javascript
  4383. * var divNodes = document.getElementsByTagName("div");
  4384. * divNodes = [].slice.call( divNodes, 0 );
  4385. *
  4386. * //output: 3(假定有3个div)
  4387. * console.log( divNodes.length );
  4388. *
  4389. * var nodes = UE.dom.domUtils.filterNodeList( divNodes, function ( node ) {
  4390. * return node.tagName.toLowerCase() === 'div';
  4391. * }, true );
  4392. *
  4393. * //output: 3
  4394. * console.log( nodes.length );
  4395. *
  4396. * var node = UE.dom.domUtils.filterNodeList( divNodes, function ( node ) {
  4397. * return node.tagName.toLowerCase() === 'div';
  4398. * }, false );
  4399. *
  4400. * //output: div
  4401. * console.log( node.nodeName );
  4402. * ```
  4403. */
  4404. filterNodeList: function (nodelist, filter, forAll) {
  4405. var results = [];
  4406. if (!utils.isFunction(filter)) {
  4407. var str = filter;
  4408. filter = function (n) {
  4409. return utils.indexOf(utils.isArray(str) ? str : str.split(' '), n.tagName.toLowerCase()) != -1
  4410. };
  4411. }
  4412. utils.each(nodelist, function (n) {
  4413. filter(n) && results.push(n)
  4414. });
  4415. return results.length == 0 ? null : results.length == 1 || !forAll ? results[0] : results
  4416. },
  4417. /**
  4418. * 查询给定的range选区是否在给定的node节点内,且在该节点的最末尾
  4419. * @method isInNodeEndBoundary
  4420. * @param { UE.dom.Range } rng 需要判断的range对象, 该对象的startContainer不能为NULL
  4421. * @param node 需要检测的节点对象
  4422. * @return { Number } 如果给定的选取range对象是在node内部的最末端, 则返回1, 否则返回0
  4423. */
  4424. isInNodeEndBoundary: function (rng, node) {
  4425. var start = rng.startContainer;
  4426. if (start.nodeType == 3 && rng.startOffset != start.nodeValue.length) {
  4427. return 0;
  4428. }
  4429. if (start.nodeType == 1 && rng.startOffset != start.childNodes.length) {
  4430. return 0;
  4431. }
  4432. while (start !== node) {
  4433. if (start.nextSibling) {
  4434. return 0
  4435. };
  4436. start = start.parentNode;
  4437. }
  4438. return 1;
  4439. },
  4440. isBoundaryNode: function (node, dir) {
  4441. var tmp;
  4442. while (!domUtils.isBody(node)) {
  4443. tmp = node;
  4444. node = node.parentNode;
  4445. if (tmp !== node[dir]) {
  4446. return false;
  4447. }
  4448. }
  4449. return true;
  4450. },
  4451. fillHtml: browser.ie11below ? '&nbsp;' : '<br/>'
  4452. };
  4453. var fillCharReg = new RegExp(domUtils.fillChar, 'g');
  4454. // core/Range.js
  4455. /**
  4456. * Range封装
  4457. * @file
  4458. * @module UE.dom
  4459. * @class Range
  4460. * @since 1.2.6.1
  4461. */
  4462. /**
  4463. * dom操作封装
  4464. * @unfile
  4465. * @module UE.dom
  4466. */
  4467. /**
  4468. * Range实现类,本类是UEditor底层核心类,封装不同浏览器之间的Range操作。
  4469. * @unfile
  4470. * @module UE.dom
  4471. * @class Range
  4472. */
  4473. (function () {
  4474. var guid = 0,
  4475. fillChar = domUtils.fillChar,
  4476. fillData;
  4477. /**
  4478. * 更新range的collapse状态
  4479. * @param {Range} range range对象
  4480. */
  4481. function updateCollapse(range) {
  4482. range.collapsed =
  4483. range.startContainer && range.endContainer &&
  4484. range.startContainer === range.endContainer &&
  4485. range.startOffset == range.endOffset;
  4486. }
  4487. function selectOneNode(rng) {
  4488. return !rng.collapsed && rng.startContainer.nodeType == 1 && rng.startContainer === rng.endContainer && rng.endOffset - rng.startOffset == 1
  4489. }
  4490. function setEndPoint(toStart, node, offset, range) {
  4491. //如果node是自闭合标签要处理
  4492. if (node.nodeType == 1 && (dtd.$empty[node.tagName] || dtd.$nonChild[node.tagName])) {
  4493. offset = domUtils.getNodeIndex(node) + (toStart ? 0 : 1);
  4494. node = node.parentNode;
  4495. }
  4496. if (toStart) {
  4497. range.startContainer = node;
  4498. range.startOffset = offset;
  4499. if (!range.endContainer) {
  4500. range.collapse(true);
  4501. }
  4502. } else {
  4503. range.endContainer = node;
  4504. range.endOffset = offset;
  4505. if (!range.startContainer) {
  4506. range.collapse(false);
  4507. }
  4508. }
  4509. updateCollapse(range);
  4510. return range;
  4511. }
  4512. function execContentsAction(range, action) {
  4513. //调整边界
  4514. //range.includeBookmark();
  4515. var start = range.startContainer,
  4516. end = range.endContainer,
  4517. startOffset = range.startOffset,
  4518. endOffset = range.endOffset,
  4519. doc = range.document,
  4520. frag = doc.createDocumentFragment(),
  4521. tmpStart, tmpEnd;
  4522. if (start.nodeType == 1) {
  4523. start = start.childNodes[startOffset] || (tmpStart = start.appendChild(doc.createTextNode('')));
  4524. }
  4525. if (end.nodeType == 1) {
  4526. end = end.childNodes[endOffset] || (tmpEnd = end.appendChild(doc.createTextNode('')));
  4527. }
  4528. if (start === end && start.nodeType == 3) {
  4529. frag.appendChild(doc.createTextNode(start.substringData(startOffset, endOffset - startOffset)));
  4530. //is not clone
  4531. if (action) {
  4532. start.deleteData(startOffset, endOffset - startOffset);
  4533. range.collapse(true);
  4534. }
  4535. return frag;
  4536. }
  4537. var current, currentLevel, clone = frag,
  4538. startParents = domUtils.findParents(start, true),
  4539. endParents = domUtils.findParents(end, true);
  4540. for (var i = 0; startParents[i] == endParents[i];) {
  4541. i++;
  4542. }
  4543. for (var j = i, si; si = startParents[j]; j++) {
  4544. current = si.nextSibling;
  4545. if (si == start) {
  4546. if (!tmpStart) {
  4547. if (range.startContainer.nodeType == 3) {
  4548. clone.appendChild(doc.createTextNode(start.nodeValue.slice(startOffset)));
  4549. //is not clone
  4550. if (action) {
  4551. start.deleteData(startOffset, start.nodeValue.length - startOffset);
  4552. }
  4553. } else {
  4554. clone.appendChild(!action ? start.cloneNode(true) : start);
  4555. }
  4556. }
  4557. } else {
  4558. currentLevel = si.cloneNode(false);
  4559. clone.appendChild(currentLevel);
  4560. }
  4561. while (current) {
  4562. if (current === end || current === endParents[j]) {
  4563. break;
  4564. }
  4565. si = current.nextSibling;
  4566. clone.appendChild(!action ? current.cloneNode(true) : current);
  4567. current = si;
  4568. }
  4569. clone = currentLevel;
  4570. }
  4571. clone = frag;
  4572. if (!startParents[i]) {
  4573. clone.appendChild(startParents[i - 1].cloneNode(false));
  4574. clone = clone.firstChild;
  4575. }
  4576. for (var j = i, ei; ei = endParents[j]; j++) {
  4577. current = ei.previousSibling;
  4578. if (ei == end) {
  4579. if (!tmpEnd && range.endContainer.nodeType == 3) {
  4580. clone.appendChild(doc.createTextNode(end.substringData(0, endOffset)));
  4581. //is not clone
  4582. if (action) {
  4583. end.deleteData(0, endOffset);
  4584. }
  4585. }
  4586. } else {
  4587. currentLevel = ei.cloneNode(false);
  4588. clone.appendChild(currentLevel);
  4589. }
  4590. //如果两端同级,右边第一次已经被开始做了
  4591. if (j != i || !startParents[i]) {
  4592. while (current) {
  4593. if (current === start) {
  4594. break;
  4595. }
  4596. ei = current.previousSibling;
  4597. clone.insertBefore(!action ? current.cloneNode(true) : current, clone.firstChild);
  4598. current = ei;
  4599. }
  4600. }
  4601. clone = currentLevel;
  4602. }
  4603. if (action) {
  4604. range.setStartBefore(!endParents[i] ? endParents[i - 1] : !startParents[i] ? startParents[i - 1] : endParents[i]).collapse(true);
  4605. }
  4606. tmpStart && domUtils.remove(tmpStart);
  4607. tmpEnd && domUtils.remove(tmpEnd);
  4608. return frag;
  4609. }
  4610. /**
  4611. * 创建一个跟document绑定的空的Range实例
  4612. * @constructor
  4613. * @param { Document } document 新建的选区所属的文档对象
  4614. */
  4615. /**
  4616. * @property { Node } startContainer 当前Range的开始边界的容器节点, 可以是一个元素节点或者是文本节点
  4617. */
  4618. /**
  4619. * @property { Node } startOffset 当前Range的开始边界容器节点的偏移量, 如果是元素节点,
  4620. * 该值就是childNodes中的第几个节点, 如果是文本节点就是文本内容的第几个字符
  4621. */
  4622. /**
  4623. * @property { Node } endContainer 当前Range的结束边界的容器节点, 可以是一个元素节点或者是文本节点
  4624. */
  4625. /**
  4626. * @property { Node } endOffset 当前Range的结束边界容器节点的偏移量, 如果是元素节点,
  4627. * 该值就是childNodes中的第几个节点, 如果是文本节点就是文本内容的第几个字符
  4628. */
  4629. /**
  4630. * @property { Boolean } collapsed 当前Range是否闭合
  4631. * @default true
  4632. * @remind Range是闭合的时候, startContainer === endContainer && startOffset === endOffset
  4633. */
  4634. /**
  4635. * @property { Document } document 当前Range所属的Document对象
  4636. * @remind 不同range的的document属性可以是不同的
  4637. */
  4638. var Range = dom.Range = function (document) {
  4639. var me = this;
  4640. me.startContainer =
  4641. me.startOffset =
  4642. me.endContainer =
  4643. me.endOffset = null;
  4644. me.document = document;
  4645. me.collapsed = true;
  4646. };
  4647. /**
  4648. * 删除fillData
  4649. * @param doc
  4650. * @param excludeNode
  4651. */
  4652. function removeFillData(doc, excludeNode) {
  4653. try {
  4654. if (fillData && domUtils.inDoc(fillData, doc)) {
  4655. if (!fillData.nodeValue.replace(fillCharReg, '').length) {
  4656. var tmpNode = fillData.parentNode;
  4657. domUtils.remove(fillData);
  4658. while (tmpNode && domUtils.isEmptyInlineElement(tmpNode) &&
  4659. //safari的contains有bug
  4660. (browser.safari ? !(domUtils.getPosition(tmpNode, excludeNode) & domUtils.POSITION_CONTAINS) : !tmpNode.contains(excludeNode))
  4661. ) {
  4662. fillData = tmpNode.parentNode;
  4663. domUtils.remove(tmpNode);
  4664. tmpNode = fillData;
  4665. }
  4666. } else {
  4667. fillData.nodeValue = fillData.nodeValue.replace(fillCharReg, '');
  4668. }
  4669. }
  4670. } catch (e) {}
  4671. }
  4672. /**
  4673. * @param node
  4674. * @param dir
  4675. */
  4676. function mergeSibling(node, dir) {
  4677. var tmpNode;
  4678. node = node[dir];
  4679. while (node && domUtils.isFillChar(node)) {
  4680. tmpNode = node[dir];
  4681. domUtils.remove(node);
  4682. node = tmpNode;
  4683. }
  4684. }
  4685. Range.prototype = {
  4686. /**
  4687. * 克隆选区的内容到一个DocumentFragment里
  4688. * @method cloneContents
  4689. * @return { DocumentFragment | NULL } 如果选区是闭合的将返回null, 否则, 返回包含所clone内容的DocumentFragment元素
  4690. * @example
  4691. * ```html
  4692. * <body>
  4693. * <!-- 中括号表示选区 -->
  4694. * <b>x<i>x[x</i>xx]x</b>
  4695. *
  4696. * <script>
  4697. * //range是已选中的选区
  4698. * var fragment = range.cloneContents(),
  4699. * node = document.createElement("div");
  4700. *
  4701. * node.appendChild( fragment );
  4702. *
  4703. * //output: <i>x</i>xx
  4704. * console.log( node.innerHTML );
  4705. *
  4706. * </script>
  4707. * </body>
  4708. * ```
  4709. */
  4710. cloneContents: function () {
  4711. return this.collapsed ? null : execContentsAction(this, 0);
  4712. },
  4713. /**
  4714. * 删除当前选区范围中的所有内容
  4715. * @method deleteContents
  4716. * @remind 执行完该操作后, 当前Range对象变成了闭合状态
  4717. * @return { UE.dom.Range } 当前操作的Range对象
  4718. * @example
  4719. * ```html
  4720. * <body>
  4721. * <!-- 中括号表示选区 -->
  4722. * <b>x<i>x[x</i>xx]x</b>
  4723. *
  4724. * <script>
  4725. * //range是已选中的选区
  4726. * range.deleteContents();
  4727. *
  4728. * //竖线表示闭合后的选区位置
  4729. * //output: <b>x<i>x</i>|x</b>
  4730. * console.log( document.body.innerHTML );
  4731. *
  4732. * //此时, range的各项属性为
  4733. * //output: B
  4734. * console.log( range.startContainer.tagName );
  4735. * //output: 2
  4736. * console.log( range.startOffset );
  4737. * //output: B
  4738. * console.log( range.endContainer.tagName );
  4739. * //output: 2
  4740. * console.log( range.endOffset );
  4741. * //output: true
  4742. * console.log( range.collapsed );
  4743. *
  4744. * </script>
  4745. * </body>
  4746. * ```
  4747. */
  4748. deleteContents: function () {
  4749. var txt;
  4750. if (!this.collapsed) {
  4751. execContentsAction(this, 1);
  4752. }
  4753. if (browser.webkit) {
  4754. txt = this.startContainer;
  4755. if (txt.nodeType == 3 && !txt.nodeValue.length) {
  4756. this.setStartBefore(txt).collapse(true);
  4757. domUtils.remove(txt);
  4758. }
  4759. }
  4760. return this;
  4761. },
  4762. /**
  4763. * 将当前选区的内容提取到一个DocumentFragment里
  4764. * @method extractContents
  4765. * @remind 执行该操作后, 选区将变成闭合状态
  4766. * @warning 执行该操作后, 原来选区所选中的内容将从dom树上剥离出来
  4767. * @return { DocumentFragment } 返回包含所提取内容的DocumentFragment对象
  4768. * @example
  4769. * ```html
  4770. * <body>
  4771. * <!-- 中括号表示选区 -->
  4772. * <b>x<i>x[x</i>xx]x</b>
  4773. *
  4774. * <script>
  4775. * //range是已选中的选区
  4776. * var fragment = range.extractContents(),
  4777. * node = document.createElement( "div" );
  4778. *
  4779. * node.appendChild( fragment );
  4780. *
  4781. * //竖线表示闭合后的选区位置
  4782. *
  4783. * //output: <b>x<i>x</i>|x</b>
  4784. * console.log( document.body.innerHTML );
  4785. * //output: <i>x</i>xx
  4786. * console.log( node.innerHTML );
  4787. *
  4788. * //此时, range的各项属性为
  4789. * //output: B
  4790. * console.log( range.startContainer.tagName );
  4791. * //output: 2
  4792. * console.log( range.startOffset );
  4793. * //output: B
  4794. * console.log( range.endContainer.tagName );
  4795. * //output: 2
  4796. * console.log( range.endOffset );
  4797. * //output: true
  4798. * console.log( range.collapsed );
  4799. *
  4800. * </script>
  4801. * </body>
  4802. */
  4803. extractContents: function () {
  4804. return this.collapsed ? null : execContentsAction(this, 2);
  4805. },
  4806. /**
  4807. * 设置Range的开始容器节点和偏移量
  4808. * @method setStart
  4809. * @remind 如果给定的节点是元素节点,那么offset指的是其子元素中索引为offset的元素,
  4810. * 如果是文本节点,那么offset指的是其文本内容的第offset个字符
  4811. * @remind 如果提供的容器节点是一个不能包含子元素的节点, 则该选区的开始容器将被设置
  4812. * 为该节点的父节点, 此时, 其距离开始容器的偏移量也变成了该节点在其父节点
  4813. * 中的索引
  4814. * @param { Node } node 将被设为当前选区开始边界容器的节点对象
  4815. * @param { int } offset 选区的开始位置偏移量
  4816. * @return { UE.dom.Range } 当前range对象
  4817. * @example
  4818. * ```html
  4819. * <!-- 选区 -->
  4820. * <b>xxx<i>x<span>xx</span>xx<em>xx</em>xxx</i>[xxx]</b>
  4821. *
  4822. * <script>
  4823. *
  4824. * //执行操作
  4825. * range.setStart( document.getElementsByTagName("i")[0], 1 );
  4826. *
  4827. * //此时, 选区变成了
  4828. * //<b>xxx<i>x[<span>xx</span>xx<em>xx</em>xxx</i>xxx]</b>
  4829. *
  4830. * </script>
  4831. * ```
  4832. * @example
  4833. * ```html
  4834. * <!-- 选区 -->
  4835. * <b>xxx<img>[xx]x</b>
  4836. *
  4837. * <script>
  4838. *
  4839. * //执行操作
  4840. * range.setStart( document.getElementsByTagName("img")[0], 3 );
  4841. *
  4842. * //此时, 选区变成了
  4843. * //<b>xxx[<img>xx]x</b>
  4844. *
  4845. * </script>
  4846. * ```
  4847. */
  4848. setStart: function (node, offset) {
  4849. return setEndPoint(true, node, offset, this);
  4850. },
  4851. /**
  4852. * 设置Range的结束容器和偏移量
  4853. * @method setEnd
  4854. * @param { Node } node 作为当前选区结束边界容器的节点对象
  4855. * @param { int } offset 结束边界的偏移量
  4856. * @see UE.dom.Range:setStart(Node,int)
  4857. * @return { UE.dom.Range } 当前range对象
  4858. */
  4859. setEnd: function (node, offset) {
  4860. return setEndPoint(false, node, offset, this);
  4861. },
  4862. /**
  4863. * 将Range开始位置设置到node节点之后
  4864. * @method setStartAfter
  4865. * @remind 该操作将会把给定节点的父节点作为range的开始容器, 且偏移量是该节点在其父节点中的位置索引+1
  4866. * @param { Node } node 选区的开始边界将紧接着该节点之后
  4867. * @return { UE.dom.Range } 当前range对象
  4868. * @example
  4869. * ```html
  4870. * <!-- 选区示例 -->
  4871. * <b>xx<i>xxx</i><span>xx[x</span>xxx]</b>
  4872. *
  4873. * <script>
  4874. *
  4875. * //执行操作
  4876. * range.setStartAfter( document.getElementsByTagName("i")[0] );
  4877. *
  4878. * //结果选区
  4879. * //<b>xx<i>xxx</i>[<span>xxx</span>xxx]</b>
  4880. *
  4881. * </script>
  4882. * ```
  4883. */
  4884. setStartAfter: function (node) {
  4885. return this.setStart(node.parentNode, domUtils.getNodeIndex(node) + 1);
  4886. },
  4887. /**
  4888. * 将Range开始位置设置到node节点之前
  4889. * @method setStartBefore
  4890. * @remind 该操作将会把给定节点的父节点作为range的开始容器, 且偏移量是该节点在其父节点中的位置索引
  4891. * @param { Node } node 新的选区开始位置在该节点之前
  4892. * @see UE.dom.Range:setStartAfter(Node)
  4893. * @return { UE.dom.Range } 当前range对象
  4894. */
  4895. setStartBefore: function (node) {
  4896. return this.setStart(node.parentNode, domUtils.getNodeIndex(node));
  4897. },
  4898. /**
  4899. * 将Range结束位置设置到node节点之后
  4900. * @method setEndAfter
  4901. * @remind 该操作将会把给定节点的父节点作为range的结束容器, 且偏移量是该节点在其父节点中的位置索引+1
  4902. * @param { Node } node 目标节点
  4903. * @see UE.dom.Range:setStartAfter(Node)
  4904. * @return { UE.dom.Range } 当前range对象
  4905. * @example
  4906. * ```html
  4907. * <!-- 选区示例 -->
  4908. * <b>[xx<i>xxx</i><span>xx]x</span>xxx</b>
  4909. *
  4910. * <script>
  4911. *
  4912. * //执行操作
  4913. * range.setStartAfter( document.getElementsByTagName("span")[0] );
  4914. *
  4915. * //结果选区
  4916. * //<b>[xx<i>xxx</i><span>xxx</span>]xxx</b>
  4917. *
  4918. * </script>
  4919. * ```
  4920. */
  4921. setEndAfter: function (node) {
  4922. return this.setEnd(node.parentNode, domUtils.getNodeIndex(node) + 1);
  4923. },
  4924. /**
  4925. * 将Range结束位置设置到node节点之前
  4926. * @method setEndBefore
  4927. * @remind 该操作将会把给定节点的父节点作为range的结束容器, 且偏移量是该节点在其父节点中的位置索引
  4928. * @param { Node } node 目标节点
  4929. * @see UE.dom.Range:setEndAfter(Node)
  4930. * @return { UE.dom.Range } 当前range对象
  4931. */
  4932. setEndBefore: function (node) {
  4933. return this.setEnd(node.parentNode, domUtils.getNodeIndex(node));
  4934. },
  4935. /**
  4936. * 设置Range的开始位置到node节点内的第一个子节点之前
  4937. * @method setStartAtFirst
  4938. * @remind 选区的开始容器将变成给定的节点, 且偏移量为0
  4939. * @remind 如果给定的节点是元素节点, 则该节点必须是允许包含子节点的元素。
  4940. * @param { Node } node 目标节点
  4941. * @see UE.dom.Range:setStartBefore(Node)
  4942. * @return { UE.dom.Range } 当前range对象
  4943. * @example
  4944. * ```html
  4945. * <!-- 选区示例 -->
  4946. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  4947. *
  4948. * <script>
  4949. *
  4950. * //执行操作
  4951. * range.setStartAtFirst( document.getElementsByTagName("i")[0] );
  4952. *
  4953. * //结果选区
  4954. * //<b>xx<i>[xxx</i><span>xx]x</span>xxx</b>
  4955. *
  4956. * </script>
  4957. * ```
  4958. */
  4959. setStartAtFirst: function (node) {
  4960. return this.setStart(node, 0);
  4961. },
  4962. /**
  4963. * 设置Range的开始位置到node节点内的最后一个节点之后
  4964. * @method setStartAtLast
  4965. * @remind 选区的开始容器将变成给定的节点, 且偏移量为该节点的子节点数
  4966. * @remind 如果给定的节点是元素节点, 则该节点必须是允许包含子节点的元素。
  4967. * @param { Node } node 目标节点
  4968. * @see UE.dom.Range:setStartAtFirst(Node)
  4969. * @return { UE.dom.Range } 当前range对象
  4970. */
  4971. setStartAtLast: function (node) {
  4972. return this.setStart(node, node.nodeType == 3 ? node.nodeValue.length : node.childNodes.length);
  4973. },
  4974. /**
  4975. * 设置Range的结束位置到node节点内的第一个节点之前
  4976. * @method setEndAtFirst
  4977. * @param { Node } node 目标节点
  4978. * @remind 选区的结束容器将变成给定的节点, 且偏移量为0
  4979. * @remind node必须是一个元素节点, 且必须是允许包含子节点的元素。
  4980. * @see UE.dom.Range:setStartAtFirst(Node)
  4981. * @return { UE.dom.Range } 当前range对象
  4982. */
  4983. setEndAtFirst: function (node) {
  4984. return this.setEnd(node, 0);
  4985. },
  4986. /**
  4987. * 设置Range的结束位置到node节点内的最后一个节点之后
  4988. * @method setEndAtLast
  4989. * @param { Node } node 目标节点
  4990. * @remind 选区的结束容器将变成给定的节点, 且偏移量为该节点的子节点数量
  4991. * @remind node必须是一个元素节点, 且必须是允许包含子节点的元素。
  4992. * @see UE.dom.Range:setStartAtFirst(Node)
  4993. * @return { UE.dom.Range } 当前range对象
  4994. */
  4995. setEndAtLast: function (node) {
  4996. return this.setEnd(node, node.nodeType == 3 ? node.nodeValue.length : node.childNodes.length);
  4997. },
  4998. /**
  4999. * 选中给定节点
  5000. * @method selectNode
  5001. * @remind 此时, 选区的开始容器和结束容器都是该节点的父节点, 其startOffset是该节点在父节点中的位置索引,
  5002. * 而endOffset为startOffset+1
  5003. * @param { Node } node 需要选中的节点
  5004. * @return { UE.dom.Range } 当前range对象,此时的range仅包含当前给定的节点对象
  5005. * @example
  5006. * ```html
  5007. * <!-- 选区示例 -->
  5008. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5009. *
  5010. * <script>
  5011. *
  5012. * //执行操作
  5013. * range.selectNode( document.getElementsByTagName("i")[0] );
  5014. *
  5015. * //结果选区
  5016. * //<b>xx[<i>xxx</i>]<span>xxx</span>xxx</b>
  5017. *
  5018. * </script>
  5019. * ```
  5020. */
  5021. selectNode: function (node) {
  5022. return this.setStartBefore(node).setEndAfter(node);
  5023. },
  5024. /**
  5025. * 选中给定节点内部的所有节点
  5026. * @method selectNodeContents
  5027. * @remind 此时, 选区的开始容器和结束容器都是该节点, 其startOffset为0,
  5028. * 而endOffset是该节点的子节点数。
  5029. * @param { Node } node 目标节点, 当前range将包含该节点内的所有节点
  5030. * @return { UE.dom.Range } 当前range对象, 此时range仅包含给定节点的所有子节点
  5031. * @example
  5032. * ```html
  5033. * <!-- 选区示例 -->
  5034. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5035. *
  5036. * <script>
  5037. *
  5038. * //执行操作
  5039. * range.selectNode( document.getElementsByTagName("b")[0] );
  5040. *
  5041. * //结果选区
  5042. * //<b>[xx<i>xxx</i><span>xxx</span>xxx]</b>
  5043. *
  5044. * </script>
  5045. * ```
  5046. */
  5047. selectNodeContents: function (node) {
  5048. return this.setStart(node, 0).setEndAtLast(node);
  5049. },
  5050. /**
  5051. * clone当前Range对象
  5052. * @method cloneRange
  5053. * @remind 返回的range是一个全新的range对象, 其内部所有属性与当前被clone的range相同。
  5054. * @return { UE.dom.Range } 当前range对象的一个副本
  5055. */
  5056. cloneRange: function () {
  5057. var me = this;
  5058. return new Range(me.document).setStart(me.startContainer, me.startOffset).setEnd(me.endContainer, me.endOffset);
  5059. },
  5060. /**
  5061. * 向当前选区的结束处闭合选区
  5062. * @method collapse
  5063. * @return { UE.dom.Range } 当前range对象
  5064. * @example
  5065. * ```html
  5066. * <!-- 选区示例 -->
  5067. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5068. *
  5069. * <script>
  5070. *
  5071. * //执行操作
  5072. * range.collapse();
  5073. *
  5074. * //结果选区
  5075. * //“|”表示选区已闭合
  5076. * //<b>xx<i>xxx</i><span>xx|x</span>xxx</b>
  5077. *
  5078. * </script>
  5079. * ```
  5080. */
  5081. /**
  5082. * 闭合当前选区,根据给定的toStart参数项决定是向当前选区开始处闭合还是向结束处闭合,
  5083. * 如果toStart的值为true,则向开始位置闭合, 反之,向结束位置闭合。
  5084. * @method collapse
  5085. * @param { Boolean } toStart 是否向选区开始处闭合
  5086. * @return { UE.dom.Range } 当前range对象,此时range对象处于闭合状态
  5087. * @see UE.dom.Range:collapse()
  5088. * @example
  5089. * ```html
  5090. * <!-- 选区示例 -->
  5091. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5092. *
  5093. * <script>
  5094. *
  5095. * //执行操作
  5096. * range.collapse( true );
  5097. *
  5098. * //结果选区
  5099. * //“|”表示选区已闭合
  5100. * //<b>xx<i>xxx</i><span>|xxx</span>xxx</b>
  5101. *
  5102. * </script>
  5103. * ```
  5104. */
  5105. collapse: function (toStart) {
  5106. var me = this;
  5107. if (toStart) {
  5108. me.endContainer = me.startContainer;
  5109. me.endOffset = me.startOffset;
  5110. } else {
  5111. me.startContainer = me.endContainer;
  5112. me.startOffset = me.endOffset;
  5113. }
  5114. me.collapsed = true;
  5115. return me;
  5116. },
  5117. /**
  5118. * 调整range的开始位置和结束位置,使其"收缩"到最小的位置
  5119. * @method shrinkBoundary
  5120. * @return { UE.dom.Range } 当前range对象
  5121. * @example
  5122. * ```html
  5123. * <span>xx<b>xx[</b>xxxxx]</span> => <span>xx<b>xx</b>[xxxxx]</span>
  5124. * ```
  5125. *
  5126. * @example
  5127. * ```html
  5128. * <!-- 选区示例 -->
  5129. * <b>x[xx</b><i>]xxx</i>
  5130. *
  5131. * <script>
  5132. *
  5133. * //执行收缩
  5134. * range.shrinkBoundary();
  5135. *
  5136. * //结果选区
  5137. * //<b>x[xx]</b><i>xxx</i>
  5138. * </script>
  5139. * ```
  5140. *
  5141. * @example
  5142. * ```html
  5143. * [<b><i>xxxx</i>xxxxxxx</b>] => <b><i>[xxxx</i>xxxxxxx]</b>
  5144. * ```
  5145. */
  5146. /**
  5147. * 调整range的开始位置和结束位置,使其"收缩"到最小的位置,
  5148. * 如果ignoreEnd的值为true,则忽略对结束位置的调整
  5149. * @method shrinkBoundary
  5150. * @param { Boolean } ignoreEnd 是否忽略对结束位置的调整
  5151. * @return { UE.dom.Range } 当前range对象
  5152. * @see UE.dom.domUtils.Range:shrinkBoundary()
  5153. */
  5154. shrinkBoundary: function (ignoreEnd) {
  5155. var me = this,
  5156. child,
  5157. collapsed = me.collapsed;
  5158. function check(node) {
  5159. return node.nodeType == 1 && !domUtils.isBookmarkNode(node) && !dtd.$empty[node.tagName] && !dtd.$nonChild[node.tagName]
  5160. }
  5161. while (me.startContainer.nodeType == 1 //是element
  5162. &&
  5163. (child = me.startContainer.childNodes[me.startOffset]) //子节点也是element
  5164. &&
  5165. check(child)) {
  5166. me.setStart(child, 0);
  5167. }
  5168. if (collapsed) {
  5169. return me.collapse(true);
  5170. }
  5171. if (!ignoreEnd) {
  5172. while (me.endContainer.nodeType == 1 //是element
  5173. &&
  5174. me.endOffset > 0 //如果是空元素就退出 endOffset=0那么endOffst-1为负值,childNodes[endOffset]报错
  5175. &&
  5176. (child = me.endContainer.childNodes[me.endOffset - 1]) //子节点也是element
  5177. &&
  5178. check(child)) {
  5179. me.setEnd(child, child.childNodes.length);
  5180. }
  5181. }
  5182. return me;
  5183. },
  5184. /**
  5185. * 获取离当前选区内包含的所有节点最近的公共祖先节点,
  5186. * @method getCommonAncestor
  5187. * @remind 返回的公共祖先节点一定不是range自身的容器节点, 但有可能是一个文本节点
  5188. * @return { Node } 当前range对象内所有节点的公共祖先节点
  5189. * @example
  5190. * ```html
  5191. * //选区示例
  5192. * <span>xxx<b>x[x<em>xx]x</em>xxx</b>xx</span>
  5193. * <script>
  5194. *
  5195. * var node = range.getCommonAncestor();
  5196. *
  5197. * //公共祖先节点是: b节点
  5198. * //输出: B
  5199. * console.log(node.tagName);
  5200. *
  5201. * </script>
  5202. * ```
  5203. */
  5204. /**
  5205. * 获取当前选区所包含的所有节点的公共祖先节点, 可以根据给定的参数 includeSelf 决定获取到
  5206. * 的公共祖先节点是否可以是当前选区的startContainer或endContainer节点, 如果 includeSelf
  5207. * 的取值为true, 则返回的节点可以是自身的容器节点, 否则, 则不能是容器节点
  5208. * @method getCommonAncestor
  5209. * @param { Boolean } includeSelf 是否允许获取到的公共祖先节点是当前range对象的容器节点
  5210. * @return { Node } 当前range对象内所有节点的公共祖先节点
  5211. * @see UE.dom.Range:getCommonAncestor()
  5212. * @example
  5213. * ```html
  5214. * <body>
  5215. *
  5216. * <!-- 选区示例 -->
  5217. * <b>xxx<i>xxxx<span>xx[x</span>xx]x</i>xxxxxxx</b>
  5218. *
  5219. * <script>
  5220. *
  5221. * var node = range.getCommonAncestor( false );
  5222. *
  5223. * //这里的公共祖先节点是B而不是I, 是因为参数限制了获取到的节点不能是容器节点
  5224. * //output: B
  5225. * console.log( node.tagName );
  5226. *
  5227. * </script>
  5228. *
  5229. * </body>
  5230. * ```
  5231. */
  5232. /**
  5233. * 获取当前选区所包含的所有节点的公共祖先节点, 可以根据给定的参数 includeSelf 决定获取到
  5234. * 的公共祖先节点是否可以是当前选区的startContainer或endContainer节点, 如果 includeSelf
  5235. * 的取值为true, 则返回的节点可以是自身的容器节点, 否则, 则不能是容器节点; 同时可以根据
  5236. * ignoreTextNode 参数的取值决定是否忽略类型为文本节点的祖先节点。
  5237. * @method getCommonAncestor
  5238. * @param { Boolean } includeSelf 是否允许获取到的公共祖先节点是当前range对象的容器节点
  5239. * @param { Boolean } ignoreTextNode 获取祖先节点的过程中是否忽略类型为文本节点的祖先节点
  5240. * @return { Node } 当前range对象内所有节点的公共祖先节点
  5241. * @see UE.dom.Range:getCommonAncestor()
  5242. * @see UE.dom.Range:getCommonAncestor(Boolean)
  5243. * @example
  5244. * ```html
  5245. * <body>
  5246. *
  5247. * <!-- 选区示例 -->
  5248. * <b>xxx<i>xxxx<span>x[x]x</span>xxx</i>xxxxxxx</b>
  5249. *
  5250. * <script>
  5251. *
  5252. * var node = range.getCommonAncestor( true, false );
  5253. *
  5254. * //output: SPAN
  5255. * console.log( node.tagName );
  5256. *
  5257. * </script>
  5258. *
  5259. * </body>
  5260. * ```
  5261. */
  5262. getCommonAncestor: function (includeSelf, ignoreTextNode) {
  5263. var me = this,
  5264. start = me.startContainer,
  5265. end = me.endContainer;
  5266. if (start === end) {
  5267. if (includeSelf && selectOneNode(this)) {
  5268. start = start.childNodes[me.startOffset];
  5269. if (start.nodeType == 1)
  5270. return start;
  5271. }
  5272. //只有在上来就相等的情况下才会出现是文本的情况
  5273. return ignoreTextNode && start.nodeType == 3 ? start.parentNode : start;
  5274. }
  5275. return domUtils.getCommonAncestor(start, end);
  5276. },
  5277. /**
  5278. * 调整当前Range的开始和结束边界容器,如果是容器节点是文本节点,就调整到包含该文本节点的父节点上
  5279. * @method trimBoundary
  5280. * @remind 该操作有可能会引起文本节点被切开
  5281. * @return { UE.dom.Range } 当前range对象
  5282. * @example
  5283. * ```html
  5284. *
  5285. * //选区示例
  5286. * <b>xxx<i>[xxxxx]</i>xxx</b>
  5287. *
  5288. * <script>
  5289. * //未调整前, 选区的开始容器和结束都是文本节点
  5290. * //执行调整
  5291. * range.trimBoundary();
  5292. *
  5293. * //调整之后, 容器节点变成了i节点
  5294. * //<b>xxx[<i>xxxxx</i>]xxx</b>
  5295. * </script>
  5296. * ```
  5297. */
  5298. /**
  5299. * 调整当前Range的开始和结束边界容器,如果是容器节点是文本节点,就调整到包含该文本节点的父节点上,
  5300. * 可以根据 ignoreEnd 参数的值决定是否调整对结束边界的调整
  5301. * @method trimBoundary
  5302. * @param { Boolean } ignoreEnd 是否忽略对结束边界的调整
  5303. * @return { UE.dom.Range } 当前range对象
  5304. * @example
  5305. * ```html
  5306. *
  5307. * //选区示例
  5308. * <b>xxx<i>[xxxxx]</i>xxx</b>
  5309. *
  5310. * <script>
  5311. * //未调整前, 选区的开始容器和结束都是文本节点
  5312. * //执行调整
  5313. * range.trimBoundary( true );
  5314. *
  5315. * //调整之后, 开始容器节点变成了i节点
  5316. * //但是, 结束容器没有发生变化
  5317. * //<b>xxx[<i>xxxxx]</i>xxx</b>
  5318. * </script>
  5319. * ```
  5320. */
  5321. trimBoundary: function (ignoreEnd) {
  5322. this.txtToElmBoundary();
  5323. var start = this.startContainer,
  5324. offset = this.startOffset,
  5325. collapsed = this.collapsed,
  5326. end = this.endContainer;
  5327. if (start.nodeType == 3) {
  5328. if (offset == 0) {
  5329. this.setStartBefore(start);
  5330. } else {
  5331. if (offset >= start.nodeValue.length) {
  5332. this.setStartAfter(start);
  5333. } else {
  5334. var textNode = domUtils.split(start, offset);
  5335. //跟新结束边界
  5336. if (start === end) {
  5337. this.setEnd(textNode, this.endOffset - offset);
  5338. } else if (start.parentNode === end) {
  5339. this.endOffset += 1;
  5340. }
  5341. this.setStartBefore(textNode);
  5342. }
  5343. }
  5344. if (collapsed) {
  5345. return this.collapse(true);
  5346. }
  5347. }
  5348. if (!ignoreEnd) {
  5349. offset = this.endOffset;
  5350. end = this.endContainer;
  5351. if (end.nodeType == 3) {
  5352. if (offset == 0) {
  5353. this.setEndBefore(end);
  5354. } else {
  5355. offset < end.nodeValue.length && domUtils.split(end, offset);
  5356. this.setEndAfter(end);
  5357. }
  5358. }
  5359. }
  5360. return this;
  5361. },
  5362. /**
  5363. * 如果选区在文本的边界上,就扩展选区到文本的父节点上, 如果当前选区是闭合的, 则什么也不做
  5364. * @method txtToElmBoundary
  5365. * @remind 该操作不会修改dom节点
  5366. * @return { UE.dom.Range } 当前range对象
  5367. */
  5368. /**
  5369. * 如果选区在文本的边界上,就扩展选区到文本的父节点上, 如果当前选区是闭合的, 则根据参数项
  5370. * ignoreCollapsed 的值决定是否执行该调整
  5371. * @method txtToElmBoundary
  5372. * @param { Boolean } ignoreCollapsed 是否忽略选区的闭合状态, 如果该参数取值为true, 则
  5373. * 不论选区是否闭合, 都会执行该操作, 反之, 则不会对闭合的选区执行该操作
  5374. * @return { UE.dom.Range } 当前range对象
  5375. */
  5376. txtToElmBoundary: function (ignoreCollapsed) {
  5377. function adjust(r, c) {
  5378. var container = r[c + 'Container'],
  5379. offset = r[c + 'Offset'];
  5380. if (container.nodeType == 3) {
  5381. if (!offset) {
  5382. r['set' + c.replace(/(\w)/, function (a) {
  5383. return a.toUpperCase();
  5384. }) + 'Before'](container);
  5385. } else if (offset >= container.nodeValue.length) {
  5386. r['set' + c.replace(/(\w)/, function (a) {
  5387. return a.toUpperCase();
  5388. }) + 'After'](container);
  5389. }
  5390. }
  5391. }
  5392. if (ignoreCollapsed || !this.collapsed) {
  5393. adjust(this, 'start');
  5394. adjust(this, 'end');
  5395. }
  5396. return this;
  5397. },
  5398. /**
  5399. * 在当前选区的开始位置前插入节点,新插入的节点会被该range包含
  5400. * @method insertNode
  5401. * @param { Node } node 需要插入的节点
  5402. * @remind 插入的节点可以是一个DocumentFragment依次插入多个节点
  5403. * @return { UE.dom.Range } 当前range对象
  5404. */
  5405. insertNode: function (node) {
  5406. var first = node,
  5407. length = 1;
  5408. if (node.nodeType == 11) {
  5409. first = node.firstChild;
  5410. length = node.childNodes.length;
  5411. }
  5412. this.trimBoundary(true);
  5413. var start = this.startContainer,
  5414. offset = this.startOffset;
  5415. var nextNode = start.childNodes[offset];
  5416. if (nextNode) {
  5417. start.insertBefore(node, nextNode);
  5418. } else {
  5419. start.appendChild(node);
  5420. }
  5421. if (first.parentNode === this.endContainer) {
  5422. this.endOffset = this.endOffset + length;
  5423. }
  5424. return this.setStartBefore(first);
  5425. },
  5426. /**
  5427. * 闭合选区到当前选区的开始位置, 并且定位光标到闭合后的位置
  5428. * @method setCursor
  5429. * @return { UE.dom.Range } 当前range对象
  5430. * @see UE.dom.Range:collapse()
  5431. */
  5432. /**
  5433. * 闭合选区,可以根据参数toEnd的值控制选区是向前闭合还是向后闭合, 并且定位光标到闭合后的位置。
  5434. * @method setCursor
  5435. * @param { Boolean } toEnd 是否向后闭合, 如果为true, 则闭合选区时, 将向结束容器方向闭合,
  5436. * 反之,则向开始容器方向闭合
  5437. * @return { UE.dom.Range } 当前range对象
  5438. * @see UE.dom.Range:collapse(Boolean)
  5439. */
  5440. setCursor: function (toEnd, noFillData) {
  5441. return this.collapse(!toEnd).select(noFillData);
  5442. },
  5443. /**
  5444. * 创建当前range的一个书签,记录下当前range的位置,方便当dom树改变时,还能找回原来的选区位置
  5445. * @method createBookmark
  5446. * @param { Boolean } serialize 控制返回的标记位置是对当前位置的引用还是ID,如果该值为true,则
  5447. * 返回标记位置的ID, 反之则返回标记位置节点的引用
  5448. * @return { Object } 返回一个书签记录键值对, 其包含的key有: start => 开始标记的ID或者引用,
  5449. * end => 结束标记的ID或引用, id => 当前标记的类型, 如果为true,则表示
  5450. * 返回的记录的类型为ID, 反之则为引用
  5451. */
  5452. createBookmark: function (serialize, same) {
  5453. var endNode,
  5454. startNode = this.document.createElement('span');
  5455. startNode.style.cssText = 'display:none;line-height:0px;';
  5456. startNode.appendChild(this.document.createTextNode('\u200D'));
  5457. startNode.id = '_baidu_bookmark_start_' + (same ? '' : guid++);
  5458. if (!this.collapsed) {
  5459. endNode = startNode.cloneNode(true);
  5460. endNode.id = '_baidu_bookmark_end_' + (same ? '' : guid++);
  5461. }
  5462. this.insertNode(startNode);
  5463. if (endNode) {
  5464. this.collapse().insertNode(endNode).setEndBefore(endNode);
  5465. }
  5466. this.setStartAfter(startNode);
  5467. return {
  5468. start: serialize ? startNode.id : startNode,
  5469. end: endNode ? serialize ? endNode.id : endNode : null,
  5470. id: serialize
  5471. }
  5472. },
  5473. /**
  5474. * 调整当前range的边界到书签位置,并删除该书签对象所标记的位置内的节点
  5475. * @method moveToBookmark
  5476. * @param { BookMark } bookmark createBookmark所创建的标签对象
  5477. * @return { UE.dom.Range } 当前range对象
  5478. * @see UE.dom.Range:createBookmark(Boolean)
  5479. */
  5480. moveToBookmark: function (bookmark) {
  5481. var start = bookmark.id ? this.document.getElementById(bookmark.start) : bookmark.start,
  5482. end = bookmark.end && bookmark.id ? this.document.getElementById(bookmark.end) : bookmark.end;
  5483. this.setStartBefore(start);
  5484. domUtils.remove(start);
  5485. if (end) {
  5486. this.setEndBefore(end);
  5487. domUtils.remove(end);
  5488. } else {
  5489. this.collapse(true);
  5490. }
  5491. return this;
  5492. },
  5493. /**
  5494. * 调整range的边界,使其"放大"到最近的父节点
  5495. * @method enlarge
  5496. * @remind 会引起选区的变化
  5497. * @return { UE.dom.Range } 当前range对象
  5498. */
  5499. /**
  5500. * 调整range的边界,使其"放大"到最近的父节点,根据参数 toBlock 的取值, 可以
  5501. * 要求扩大之后的父节点是block节点
  5502. * @method enlarge
  5503. * @param { Boolean } toBlock 是否要求扩大之后的父节点必须是block节点
  5504. * @return { UE.dom.Range } 当前range对象
  5505. */
  5506. enlarge: function (toBlock, stopFn) {
  5507. var isBody = domUtils.isBody,
  5508. pre, node, tmp = this.document.createTextNode('');
  5509. if (toBlock) {
  5510. node = this.startContainer;
  5511. if (node.nodeType == 1) {
  5512. if (node.childNodes[this.startOffset]) {
  5513. pre = node = node.childNodes[this.startOffset]
  5514. } else {
  5515. node.appendChild(tmp);
  5516. pre = node = tmp;
  5517. }
  5518. } else {
  5519. pre = node;
  5520. }
  5521. while (1) {
  5522. if (domUtils.isBlockElm(node)) {
  5523. node = pre;
  5524. while ((pre = node.previousSibling) && !domUtils.isBlockElm(pre)) {
  5525. node = pre;
  5526. }
  5527. this.setStartBefore(node);
  5528. break;
  5529. }
  5530. pre = node;
  5531. node = node.parentNode;
  5532. }
  5533. node = this.endContainer;
  5534. if (node.nodeType == 1) {
  5535. if (pre = node.childNodes[this.endOffset]) {
  5536. node.insertBefore(tmp, pre);
  5537. } else {
  5538. node.appendChild(tmp);
  5539. }
  5540. pre = node = tmp;
  5541. } else {
  5542. pre = node;
  5543. }
  5544. while (1) {
  5545. if (domUtils.isBlockElm(node)) {
  5546. node = pre;
  5547. while ((pre = node.nextSibling) && !domUtils.isBlockElm(pre)) {
  5548. node = pre;
  5549. }
  5550. this.setEndAfter(node);
  5551. break;
  5552. }
  5553. pre = node;
  5554. node = node.parentNode;
  5555. }
  5556. if (tmp.parentNode === this.endContainer) {
  5557. this.endOffset--;
  5558. }
  5559. domUtils.remove(tmp);
  5560. }
  5561. // 扩展边界到最大
  5562. if (!this.collapsed) {
  5563. while (this.startOffset == 0) {
  5564. if (stopFn && stopFn(this.startContainer)) {
  5565. break;
  5566. }
  5567. if (isBody(this.startContainer)) {
  5568. break;
  5569. }
  5570. this.setStartBefore(this.startContainer);
  5571. }
  5572. while (this.endOffset == (this.endContainer.nodeType == 1 ? this.endContainer.childNodes.length : this.endContainer.nodeValue.length)) {
  5573. if (stopFn && stopFn(this.endContainer)) {
  5574. break;
  5575. }
  5576. if (isBody(this.endContainer)) {
  5577. break;
  5578. }
  5579. this.setEndAfter(this.endContainer);
  5580. }
  5581. }
  5582. return this;
  5583. },
  5584. enlargeToBlockElm: function (ignoreEnd) {
  5585. while (!domUtils.isBlockElm(this.startContainer)) {
  5586. this.setStartBefore(this.startContainer);
  5587. }
  5588. if (!ignoreEnd) {
  5589. while (!domUtils.isBlockElm(this.endContainer)) {
  5590. this.setEndAfter(this.endContainer);
  5591. }
  5592. }
  5593. return this;
  5594. },
  5595. /**
  5596. * 调整Range的边界,使其"缩小"到最合适的位置
  5597. * @method adjustmentBoundary
  5598. * @return { UE.dom.Range } 当前range对象
  5599. * @see UE.dom.Range:shrinkBoundary()
  5600. */
  5601. adjustmentBoundary: function () {
  5602. if (!this.collapsed) {
  5603. while (!domUtils.isBody(this.startContainer) &&
  5604. this.startOffset == this.startContainer[this.startContainer.nodeType == 3 ? 'nodeValue' : 'childNodes'].length &&
  5605. this.startContainer[this.startContainer.nodeType == 3 ? 'nodeValue' : 'childNodes'].length
  5606. ) {
  5607. this.setStartAfter(this.startContainer);
  5608. }
  5609. while (!domUtils.isBody(this.endContainer) && !this.endOffset &&
  5610. this.endContainer[this.endContainer.nodeType == 3 ? 'nodeValue' : 'childNodes'].length
  5611. ) {
  5612. this.setEndBefore(this.endContainer);
  5613. }
  5614. }
  5615. return this;
  5616. },
  5617. /**
  5618. * 给range选区中的内容添加给定的inline标签
  5619. * @method applyInlineStyle
  5620. * @param { String } tagName 需要添加的标签名
  5621. * @example
  5622. * ```html
  5623. * <p>xxxx[xxxx]x</p> ==> range.applyInlineStyle("strong") ==> <p>xxxx[<strong>xxxx</strong>]x</p>
  5624. * ```
  5625. */
  5626. /**
  5627. * 给range选区中的内容添加给定的inline标签, 并且为标签附加上一些初始化属性。
  5628. * @method applyInlineStyle
  5629. * @param { String } tagName 需要添加的标签名
  5630. * @param { Object } attrs 跟随新添加的标签的属性
  5631. * @return { UE.dom.Range } 当前选区
  5632. * @example
  5633. * ```html
  5634. * <p>xxxx[xxxx]x</p>
  5635. *
  5636. * ==>
  5637. *
  5638. * <!-- 执行操作 -->
  5639. * range.applyInlineStyle("strong",{"style":"font-size:12px"})
  5640. *
  5641. * ==>
  5642. *
  5643. * <p>xxxx[<strong style="font-size:12px">xxxx</strong>]x</p>
  5644. * ```
  5645. */
  5646. applyInlineStyle: function (tagName, attrs, list) {
  5647. if (this.collapsed) return this;
  5648. this.trimBoundary().enlarge(false,
  5649. function (node) {
  5650. return node.nodeType == 1 && domUtils.isBlockElm(node)
  5651. }).adjustmentBoundary();
  5652. var bookmark = this.createBookmark(),
  5653. end = bookmark.end,
  5654. filterFn = function (node) {
  5655. return node.nodeType == 1 ? node.tagName.toLowerCase() != 'br' : !domUtils.isWhitespace(node);
  5656. },
  5657. current = domUtils.getNextDomNode(bookmark.start, false, filterFn),
  5658. node,
  5659. pre,
  5660. range = this.cloneRange();
  5661. while (current && (domUtils.getPosition(current, end) & domUtils.POSITION_PRECEDING)) {
  5662. if (current.nodeType == 3 || dtd[tagName][current.tagName]) {
  5663. range.setStartBefore(current);
  5664. node = current;
  5665. while (node && (node.nodeType == 3 || dtd[tagName][node.tagName]) && node !== end) {
  5666. pre = node;
  5667. node = domUtils.getNextDomNode(node, node.nodeType == 1, null, function (parent) {
  5668. return dtd[tagName][parent.tagName];
  5669. });
  5670. }
  5671. var frag = range.setEndAfter(pre).extractContents(),
  5672. elm;
  5673. if (list && list.length > 0) {
  5674. var level, top;
  5675. top = level = list[0].cloneNode(false);
  5676. for (var i = 1, ci; ci = list[i++];) {
  5677. level.appendChild(ci.cloneNode(false));
  5678. level = level.firstChild;
  5679. }
  5680. elm = level;
  5681. } else {
  5682. elm = range.document.createElement(tagName);
  5683. }
  5684. if (attrs) {
  5685. domUtils.setAttributes(elm, attrs);
  5686. }
  5687. elm.appendChild(frag);
  5688. range.insertNode(list ? top : elm);
  5689. //处理下滑线在a上的情况
  5690. var aNode;
  5691. if (tagName == 'span' && attrs.style && /text\-decoration/.test(attrs.style) && (aNode = domUtils.findParentByTagName(elm, 'a', true))) {
  5692. domUtils.setAttributes(aNode, attrs);
  5693. domUtils.remove(elm, true);
  5694. elm = aNode;
  5695. } else {
  5696. domUtils.mergeSibling(elm);
  5697. domUtils.clearEmptySibling(elm);
  5698. }
  5699. //去除子节点相同的
  5700. domUtils.mergeChild(elm, attrs);
  5701. current = domUtils.getNextDomNode(elm, false, filterFn);
  5702. domUtils.mergeToParent(elm);
  5703. if (node === end) {
  5704. break;
  5705. }
  5706. } else {
  5707. current = domUtils.getNextDomNode(current, true, filterFn);
  5708. }
  5709. }
  5710. return this.moveToBookmark(bookmark);
  5711. },
  5712. /**
  5713. * 移除当前选区内指定的inline标签,但保留其中的内容
  5714. * @method removeInlineStyle
  5715. * @param { String } tagName 需要移除的标签名
  5716. * @return { UE.dom.Range } 当前的range对象
  5717. * @example
  5718. * ```html
  5719. * xx[x<span>xxx<em>yyy</em>zz]z</span> => range.removeInlineStyle(["em"]) => xx[x<span>xxxyyyzz]z</span>
  5720. * ```
  5721. */
  5722. /**
  5723. * 移除当前选区内指定的一组inline标签,但保留其中的内容
  5724. * @method removeInlineStyle
  5725. * @param { Array } tagNameArr 需要移除的标签名的数组
  5726. * @return { UE.dom.Range } 当前的range对象
  5727. * @see UE.dom.Range:removeInlineStyle(String)
  5728. */
  5729. removeInlineStyle: function (tagNames) {
  5730. if (this.collapsed) return this;
  5731. tagNames = utils.isArray(tagNames) ? tagNames : [tagNames];
  5732. this.shrinkBoundary().adjustmentBoundary();
  5733. var start = this.startContainer,
  5734. end = this.endContainer;
  5735. while (1) {
  5736. if (start.nodeType == 1) {
  5737. if (utils.indexOf(tagNames, start.tagName.toLowerCase()) > -1) {
  5738. break;
  5739. }
  5740. if (start.tagName.toLowerCase() == 'body') {
  5741. start = null;
  5742. break;
  5743. }
  5744. }
  5745. start = start.parentNode;
  5746. }
  5747. while (1) {
  5748. if (end.nodeType == 1) {
  5749. if (utils.indexOf(tagNames, end.tagName.toLowerCase()) > -1) {
  5750. break;
  5751. }
  5752. if (end.tagName.toLowerCase() == 'body') {
  5753. end = null;
  5754. break;
  5755. }
  5756. }
  5757. end = end.parentNode;
  5758. }
  5759. var bookmark = this.createBookmark(),
  5760. frag,
  5761. tmpRange;
  5762. if (start) {
  5763. tmpRange = this.cloneRange().setEndBefore(bookmark.start).setStartBefore(start);
  5764. frag = tmpRange.extractContents();
  5765. tmpRange.insertNode(frag);
  5766. domUtils.clearEmptySibling(start, true);
  5767. start.parentNode.insertBefore(bookmark.start, start);
  5768. }
  5769. if (end) {
  5770. tmpRange = this.cloneRange().setStartAfter(bookmark.end).setEndAfter(end);
  5771. frag = tmpRange.extractContents();
  5772. tmpRange.insertNode(frag);
  5773. domUtils.clearEmptySibling(end, false, true);
  5774. end.parentNode.insertBefore(bookmark.end, end.nextSibling);
  5775. }
  5776. var current = domUtils.getNextDomNode(bookmark.start, false, function (node) {
  5777. return node.nodeType == 1;
  5778. }),
  5779. next;
  5780. while (current && current !== bookmark.end) {
  5781. next = domUtils.getNextDomNode(current, true, function (node) {
  5782. return node.nodeType == 1;
  5783. });
  5784. if (utils.indexOf(tagNames, current.tagName.toLowerCase()) > -1) {
  5785. domUtils.remove(current, true);
  5786. }
  5787. current = next;
  5788. }
  5789. return this.moveToBookmark(bookmark);
  5790. },
  5791. /**
  5792. * 获取当前选中的自闭合的节点
  5793. * @method getClosedNode
  5794. * @return { Node | NULL } 如果当前选中的是自闭合节点, 则返回该节点, 否则返回NULL
  5795. */
  5796. getClosedNode: function () {
  5797. var node;
  5798. if (!this.collapsed) {
  5799. var range = this.cloneRange().adjustmentBoundary().shrinkBoundary();
  5800. if (selectOneNode(range)) {
  5801. var child = range.startContainer.childNodes[range.startOffset];
  5802. if (child && child.nodeType == 1 && (dtd.$empty[child.tagName] || dtd.$nonChild[child.tagName])) {
  5803. node = child;
  5804. }
  5805. }
  5806. }
  5807. return node;
  5808. },
  5809. /**
  5810. * 在页面上高亮range所表示的选区
  5811. * @method select
  5812. * @return { UE.dom.Range } 返回当前Range对象
  5813. */
  5814. //这里不区分ie9以上,trace:3824
  5815. select: browser.ie ? function (noFillData, textRange) {
  5816. var nativeRange;
  5817. if (!this.collapsed)
  5818. this.shrinkBoundary();
  5819. var node = this.getClosedNode();
  5820. if (node && !textRange) {
  5821. try {
  5822. nativeRange = this.document.body.createControlRange();
  5823. nativeRange.addElement(node);
  5824. nativeRange.select();
  5825. } catch (e) {}
  5826. return this;
  5827. }
  5828. var bookmark = this.createBookmark(),
  5829. start = bookmark.start,
  5830. end;
  5831. nativeRange = this.document.body.createTextRange();
  5832. nativeRange.moveToElementText(start);
  5833. nativeRange.moveStart('character', 1);
  5834. if (!this.collapsed) {
  5835. var nativeRangeEnd = this.document.body.createTextRange();
  5836. end = bookmark.end;
  5837. nativeRangeEnd.moveToElementText(end);
  5838. nativeRange.setEndPoint('EndToEnd', nativeRangeEnd);
  5839. } else {
  5840. if (!noFillData && this.startContainer.nodeType != 3) {
  5841. //使用<span>|x<span>固定住光标
  5842. var tmpText = this.document.createTextNode(fillChar),
  5843. tmp = this.document.createElement('span');
  5844. tmp.appendChild(this.document.createTextNode(fillChar));
  5845. start.parentNode.insertBefore(tmp, start);
  5846. start.parentNode.insertBefore(tmpText, start);
  5847. //当点b,i,u时,不能清除i上边的b
  5848. removeFillData(this.document, tmpText);
  5849. fillData = tmpText;
  5850. mergeSibling(tmp, 'previousSibling');
  5851. mergeSibling(start, 'nextSibling');
  5852. nativeRange.moveStart('character', -1);
  5853. nativeRange.collapse(true);
  5854. }
  5855. }
  5856. this.moveToBookmark(bookmark);
  5857. tmp && domUtils.remove(tmp);
  5858. //IE在隐藏状态下不支持range操作,catch一下
  5859. try {
  5860. nativeRange.select();
  5861. } catch (e) {}
  5862. return this;
  5863. } : function (notInsertFillData) {
  5864. function checkOffset(rng) {
  5865. function check(node, offset, dir) {
  5866. if (node.nodeType == 3 && node.nodeValue.length < offset) {
  5867. rng[dir + 'Offset'] = node.nodeValue.length
  5868. }
  5869. }
  5870. check(rng.startContainer, rng.startOffset, 'start');
  5871. check(rng.endContainer, rng.endOffset, 'end');
  5872. }
  5873. var win = domUtils.getWindow(this.document),
  5874. sel = win.getSelection(),
  5875. txtNode;
  5876. //FF下关闭自动长高时滚动条在关闭dialog时会跳
  5877. //ff下如果不body.focus将不能定位闭合光标到编辑器内
  5878. browser.gecko ? this.document.body.focus() : win.focus();
  5879. if (sel) {
  5880. sel.removeAllRanges();
  5881. // trace:870 chrome/safari后边是br对于闭合得range不能定位 所以去掉了判断
  5882. // this.startContainer.nodeType != 3 &&! ((child = this.startContainer.childNodes[this.startOffset]) && child.nodeType == 1 && child.tagName == 'BR'
  5883. if (this.collapsed && !notInsertFillData) {
  5884. // //opear如果没有节点接着,原生的不能够定位,不能在body的第一级插入空白节点
  5885. // if (notInsertFillData && browser.opera && !domUtils.isBody(this.startContainer) && this.startContainer.nodeType == 1) {
  5886. // var tmp = this.document.createTextNode('');
  5887. // this.insertNode(tmp).setStart(tmp, 0).collapse(true);
  5888. // }
  5889. //
  5890. //处理光标落在文本节点的情况
  5891. //处理以下的情况
  5892. //<b>|xxxx</b>
  5893. //<b>xxxx</b>|xxxx
  5894. //xxxx<b>|</b>
  5895. var start = this.startContainer,
  5896. child = start;
  5897. if (start.nodeType == 1) {
  5898. child = start.childNodes[this.startOffset];
  5899. }
  5900. if (!(start.nodeType == 3 && this.startOffset) &&
  5901. (child ?
  5902. (!child.previousSibling || child.previousSibling.nodeType != 3) :
  5903. (!start.lastChild || start.lastChild.nodeType != 3)
  5904. )
  5905. ) {
  5906. txtNode = this.document.createTextNode(fillChar);
  5907. //跟着前边走
  5908. this.insertNode(txtNode);
  5909. removeFillData(this.document, txtNode);
  5910. mergeSibling(txtNode, 'previousSibling');
  5911. mergeSibling(txtNode, 'nextSibling');
  5912. fillData = txtNode;
  5913. this.setStart(txtNode, browser.webkit ? 1 : 0).collapse(true);
  5914. }
  5915. }
  5916. var nativeRange = this.document.createRange();
  5917. if (this.collapsed && browser.opera && this.startContainer.nodeType == 1) {
  5918. var child = this.startContainer.childNodes[this.startOffset];
  5919. if (!child) {
  5920. //往前靠拢
  5921. child = this.startContainer.lastChild;
  5922. if (child && domUtils.isBr(child)) {
  5923. this.setStartBefore(child).collapse(true);
  5924. }
  5925. } else {
  5926. //向后靠拢
  5927. while (child && domUtils.isBlockElm(child)) {
  5928. if (child.nodeType == 1 && child.childNodes[0]) {
  5929. child = child.childNodes[0]
  5930. } else {
  5931. break;
  5932. }
  5933. }
  5934. child && this.setStartBefore(child).collapse(true)
  5935. }
  5936. }
  5937. //是createAddress最后一位算的不准,现在这里进行微调
  5938. checkOffset(this);
  5939. nativeRange.setStart(this.startContainer, this.startOffset);
  5940. nativeRange.setEnd(this.endContainer, this.endOffset);
  5941. sel.addRange(nativeRange);
  5942. }
  5943. return this;
  5944. },
  5945. /**
  5946. * 滚动到当前range开始的位置
  5947. * @method scrollToView
  5948. * @param { Window } win 当前range对象所属的window对象
  5949. * @return { UE.dom.Range } 当前Range对象
  5950. */
  5951. /**
  5952. * 滚动到距离当前range开始位置 offset 的位置处
  5953. * @method scrollToView
  5954. * @param { Window } win 当前range对象所属的window对象
  5955. * @param { Number } offset 距离range开始位置处的偏移量, 如果为正数, 则向下偏移, 反之, 则向上偏移
  5956. * @return { UE.dom.Range } 当前Range对象
  5957. */
  5958. scrollToView: function (win, offset) {
  5959. win = win ? window : domUtils.getWindow(this.document);
  5960. var me = this,
  5961. span = me.document.createElement('span');
  5962. //trace:717
  5963. span.innerHTML = '&nbsp;';
  5964. me.cloneRange().insertNode(span);
  5965. domUtils.scrollToView(span, win, offset);
  5966. domUtils.remove(span);
  5967. return me;
  5968. },
  5969. /**
  5970. * 判断当前选区内容是否占位符
  5971. * @private
  5972. * @method inFillChar
  5973. * @return { Boolean } 如果是占位符返回true,否则返回false
  5974. */
  5975. inFillChar: function () {
  5976. var start = this.startContainer;
  5977. if (this.collapsed && start.nodeType == 3 &&
  5978. start.nodeValue.replace(new RegExp('^' + domUtils.fillChar), '').length + 1 == start.nodeValue.length
  5979. ) {
  5980. return true;
  5981. }
  5982. return false;
  5983. },
  5984. /**
  5985. * 保存
  5986. * @method createAddress
  5987. * @private
  5988. * @return { Boolean } 返回开始和结束的位置
  5989. * @example
  5990. * ```html
  5991. * <body>
  5992. * <p>
  5993. * aaaa
  5994. * <em>
  5995. * <!-- 选区开始 -->
  5996. * bbbb
  5997. * <!-- 选区结束 -->
  5998. * </em>
  5999. * </p>
  6000. *
  6001. * <script>
  6002. * //output: {startAddress:[0,1,0,0],endAddress:[0,1,0,4]}
  6003. * console.log( range.createAddress() );
  6004. * </script>
  6005. * </body>
  6006. * ```
  6007. */
  6008. createAddress: function (ignoreEnd, ignoreTxt) {
  6009. var addr = {},
  6010. me = this;
  6011. function getAddress(isStart) {
  6012. var node = isStart ? me.startContainer : me.endContainer;
  6013. var parents = domUtils.findParents(node, true, function (node) {
  6014. return !domUtils.isBody(node)
  6015. }),
  6016. addrs = [];
  6017. for (var i = 0, ci; ci = parents[i++];) {
  6018. addrs.push(domUtils.getNodeIndex(ci, ignoreTxt));
  6019. }
  6020. var firstIndex = 0;
  6021. if (ignoreTxt) {
  6022. if (node.nodeType == 3) {
  6023. var tmpNode = node.previousSibling;
  6024. while (tmpNode && tmpNode.nodeType == 3) {
  6025. firstIndex += tmpNode.nodeValue.replace(fillCharReg, '').length;
  6026. tmpNode = tmpNode.previousSibling;
  6027. }
  6028. firstIndex += (isStart ? me.startOffset : me.endOffset) // - (fillCharReg.test(node.nodeValue) ? 1 : 0 )
  6029. } else {
  6030. node = node.childNodes[isStart ? me.startOffset : me.endOffset];
  6031. if (node) {
  6032. firstIndex = domUtils.getNodeIndex(node, ignoreTxt);
  6033. } else {
  6034. node = isStart ? me.startContainer : me.endContainer;
  6035. var first = node.firstChild;
  6036. while (first) {
  6037. if (domUtils.isFillChar(first)) {
  6038. first = first.nextSibling;
  6039. continue;
  6040. }
  6041. firstIndex++;
  6042. if (first.nodeType == 3) {
  6043. while (first && first.nodeType == 3) {
  6044. first = first.nextSibling;
  6045. }
  6046. } else {
  6047. first = first.nextSibling;
  6048. }
  6049. }
  6050. }
  6051. }
  6052. } else {
  6053. firstIndex = isStart ? domUtils.isFillChar(node) ? 0 : me.startOffset : me.endOffset
  6054. }
  6055. if (firstIndex < 0) {
  6056. firstIndex = 0;
  6057. }
  6058. addrs.push(firstIndex);
  6059. return addrs;
  6060. }
  6061. addr.startAddress = getAddress(true);
  6062. if (!ignoreEnd) {
  6063. addr.endAddress = me.collapsed ? [].concat(addr.startAddress) : getAddress();
  6064. }
  6065. return addr;
  6066. },
  6067. /**
  6068. * 保存
  6069. * @method createAddress
  6070. * @private
  6071. * @return { Boolean } 返回开始和结束的位置
  6072. * @example
  6073. * ```html
  6074. * <body>
  6075. * <p>
  6076. * aaaa
  6077. * <em>
  6078. * <!-- 选区开始 -->
  6079. * bbbb
  6080. * <!-- 选区结束 -->
  6081. * </em>
  6082. * </p>
  6083. *
  6084. * <script>
  6085. * var range = editor.selection.getRange();
  6086. * range.moveToAddress({startAddress:[0,1,0,0],endAddress:[0,1,0,4]});
  6087. * range.select();
  6088. * //output: 'bbbb'
  6089. * console.log(editor.selection.getText());
  6090. * </script>
  6091. * </body>
  6092. * ```
  6093. */
  6094. moveToAddress: function (addr, ignoreEnd) {
  6095. var me = this;
  6096. function getNode(address, isStart) {
  6097. var tmpNode = me.document.body,
  6098. parentNode, offset;
  6099. for (var i = 0, ci, l = address.length; i < l; i++) {
  6100. ci = address[i];
  6101. parentNode = tmpNode;
  6102. tmpNode = tmpNode.childNodes[ci];
  6103. if (!tmpNode) {
  6104. offset = ci;
  6105. break;
  6106. }
  6107. }
  6108. if (isStart) {
  6109. if (tmpNode) {
  6110. me.setStartBefore(tmpNode)
  6111. } else {
  6112. me.setStart(parentNode, offset)
  6113. }
  6114. } else {
  6115. if (tmpNode) {
  6116. me.setEndBefore(tmpNode)
  6117. } else {
  6118. me.setEnd(parentNode, offset)
  6119. }
  6120. }
  6121. }
  6122. getNode(addr.startAddress, true);
  6123. !ignoreEnd && addr.endAddress && getNode(addr.endAddress);
  6124. return me;
  6125. },
  6126. /**
  6127. * 判断给定的Range对象是否和当前Range对象表示的是同一个选区
  6128. * @method equals
  6129. * @param { UE.dom.Range } 需要判断的Range对象
  6130. * @return { Boolean } 如果给定的Range对象与当前Range对象表示的是同一个选区, 则返回true, 否则返回false
  6131. */
  6132. equals: function (rng) {
  6133. for (var p in this) {
  6134. if (this.hasOwnProperty(p)) {
  6135. if (this[p] !== rng[p])
  6136. return false
  6137. }
  6138. }
  6139. return true;
  6140. },
  6141. /**
  6142. * 遍历range内的节点。每当遍历一个节点时, 都会执行参数项 doFn 指定的函数, 该函数的接受当前遍历的节点
  6143. * 作为其参数。
  6144. * @method traversal
  6145. * @param { Function } doFn 对每个遍历的节点要执行的方法, 该方法接受当前遍历的节点作为其参数
  6146. * @return { UE.dom.Range } 当前range对象
  6147. * @example
  6148. * ```html
  6149. *
  6150. * <body>
  6151. *
  6152. * <!-- 选区开始 -->
  6153. * <span></span>
  6154. * <a></a>
  6155. * <!-- 选区结束 -->
  6156. * </body>
  6157. *
  6158. * <script>
  6159. *
  6160. * //output: <span></span><a></a>
  6161. * console.log( range.cloneContents() );
  6162. *
  6163. * range.traversal( function ( node ) {
  6164. *
  6165. * if ( node.nodeType === 1 ) {
  6166. * node.className = "test";
  6167. * }
  6168. *
  6169. * } );
  6170. *
  6171. * //output: <span class="test"></span><a class="test"></a>
  6172. * console.log( range.cloneContents() );
  6173. *
  6174. * </script>
  6175. * ```
  6176. */
  6177. /**
  6178. * 遍历range内的节点。
  6179. * 每当遍历一个节点时, 都会执行参数项 doFn 指定的函数, 该函数的接受当前遍历的节点
  6180. * 作为其参数。
  6181. * 可以通过参数项 filterFn 来指定一个过滤器, 只有符合该过滤器过滤规则的节点才会触
  6182. * 发doFn函数的执行
  6183. * @method traversal
  6184. * @param { Function } doFn 对每个遍历的节点要执行的方法, 该方法接受当前遍历的节点作为其参数
  6185. * @param { Function } filterFn 过滤器, 该函数接受当前遍历的节点作为参数, 如果该节点满足过滤
  6186. * 规则, 请返回true, 该节点会触发doFn, 否则, 请返回false, 则该节点不
  6187. * 会触发doFn。
  6188. * @return { UE.dom.Range } 当前range对象
  6189. * @see UE.dom.Range:traversal(Function)
  6190. * @example
  6191. * ```html
  6192. *
  6193. * <body>
  6194. *
  6195. * <!-- 选区开始 -->
  6196. * <span></span>
  6197. * <a></a>
  6198. * <!-- 选区结束 -->
  6199. * </body>
  6200. *
  6201. * <script>
  6202. *
  6203. * //output: <span></span><a></a>
  6204. * console.log( range.cloneContents() );
  6205. *
  6206. * range.traversal( function ( node ) {
  6207. *
  6208. * node.className = "test";
  6209. *
  6210. * }, function ( node ) {
  6211. * return node.nodeType === 1;
  6212. * } );
  6213. *
  6214. * //output: <span class="test"></span><a class="test"></a>
  6215. * console.log( range.cloneContents() );
  6216. *
  6217. * </script>
  6218. * ```
  6219. */
  6220. traversal: function (doFn, filterFn) {
  6221. if (this.collapsed)
  6222. return this;
  6223. var bookmark = this.createBookmark(),
  6224. end = bookmark.end,
  6225. current = domUtils.getNextDomNode(bookmark.start, false, filterFn);
  6226. while (current && current !== end && (domUtils.getPosition(current, end) & domUtils.POSITION_PRECEDING)) {
  6227. var tmpNode = domUtils.getNextDomNode(current, false, filterFn);
  6228. doFn(current);
  6229. current = tmpNode;
  6230. }
  6231. return this.moveToBookmark(bookmark);
  6232. }
  6233. };
  6234. })();
  6235. // core/Selection.js
  6236. /**
  6237. * 选集
  6238. * @file
  6239. * @module UE.dom
  6240. * @class Selection
  6241. * @since 1.2.6.1
  6242. */
  6243. /**
  6244. * 选区集合
  6245. * @unfile
  6246. * @module UE.dom
  6247. * @class Selection
  6248. */
  6249. (function () {
  6250. function getBoundaryInformation(range, start) {
  6251. var getIndex = domUtils.getNodeIndex;
  6252. range = range.duplicate();
  6253. range.collapse(start);
  6254. var parent = range.parentElement();
  6255. //如果节点里没有子节点,直接退出
  6256. if (!parent.hasChildNodes()) {
  6257. return {
  6258. container: parent,
  6259. offset: 0
  6260. };
  6261. }
  6262. var siblings = parent.children,
  6263. child,
  6264. testRange = range.duplicate(),
  6265. startIndex = 0,
  6266. endIndex = siblings.length - 1,
  6267. index = -1,
  6268. distance;
  6269. while (startIndex <= endIndex) {
  6270. index = Math.floor((startIndex + endIndex) / 2);
  6271. child = siblings[index];
  6272. testRange.moveToElementText(child);
  6273. var position = testRange.compareEndPoints('StartToStart', range);
  6274. if (position > 0) {
  6275. endIndex = index - 1;
  6276. } else if (position < 0) {
  6277. startIndex = index + 1;
  6278. } else {
  6279. //trace:1043
  6280. return {
  6281. container: parent,
  6282. offset: getIndex(child)
  6283. };
  6284. }
  6285. }
  6286. if (index == -1) {
  6287. testRange.moveToElementText(parent);
  6288. testRange.setEndPoint('StartToStart', range);
  6289. distance = testRange.text.replace(/(\r\n|\r)/g, '\n').length;
  6290. siblings = parent.childNodes;
  6291. if (!distance) {
  6292. child = siblings[siblings.length - 1];
  6293. return {
  6294. container: child,
  6295. offset: child.nodeValue.length
  6296. };
  6297. }
  6298. var i = siblings.length;
  6299. while (distance > 0) {
  6300. distance -= siblings[--i].nodeValue.length;
  6301. }
  6302. return {
  6303. container: siblings[i],
  6304. offset: -distance
  6305. };
  6306. }
  6307. testRange.collapse(position > 0);
  6308. testRange.setEndPoint(position > 0 ? 'StartToStart' : 'EndToStart', range);
  6309. distance = testRange.text.replace(/(\r\n|\r)/g, '\n').length;
  6310. if (!distance) {
  6311. return dtd.$empty[child.tagName] || dtd.$nonChild[child.tagName] ? {
  6312. container: parent,
  6313. offset: getIndex(child) + (position > 0 ? 0 : 1)
  6314. } : {
  6315. container: child,
  6316. offset: position > 0 ? 0 : child.childNodes.length
  6317. }
  6318. }
  6319. while (distance > 0) {
  6320. try {
  6321. var pre = child;
  6322. child = child[position > 0 ? 'previousSibling' : 'nextSibling'];
  6323. distance -= child.nodeValue.length;
  6324. } catch (e) {
  6325. return {
  6326. container: parent,
  6327. offset: getIndex(pre)
  6328. };
  6329. }
  6330. }
  6331. return {
  6332. container: child,
  6333. offset: position > 0 ? -distance : child.nodeValue.length + distance
  6334. }
  6335. }
  6336. /**
  6337. * 将ieRange转换为Range对象
  6338. * @param {Range} ieRange ieRange对象
  6339. * @param {Range} range Range对象
  6340. * @return {Range} range 返回转换后的Range对象
  6341. */
  6342. function transformIERangeToRange(ieRange, range) {
  6343. if (ieRange.item) {
  6344. range.selectNode(ieRange.item(0));
  6345. } else {
  6346. var bi = getBoundaryInformation(ieRange, true);
  6347. range.setStart(bi.container, bi.offset);
  6348. if (ieRange.compareEndPoints('StartToEnd', ieRange) != 0) {
  6349. bi = getBoundaryInformation(ieRange, false);
  6350. range.setEnd(bi.container, bi.offset);
  6351. }
  6352. }
  6353. return range;
  6354. }
  6355. /**
  6356. * 获得ieRange
  6357. * @param {Selection} sel Selection对象
  6358. * @return {ieRange} 得到ieRange
  6359. */
  6360. function _getIERange(sel) {
  6361. var ieRange;
  6362. //ie下有可能报错
  6363. try {
  6364. ieRange = sel.getNative().createRange();
  6365. } catch (e) {
  6366. return null;
  6367. }
  6368. var el = ieRange.item ? ieRange.item(0) : ieRange.parentElement();
  6369. if ((el.ownerDocument || el) === sel.document) {
  6370. return ieRange;
  6371. }
  6372. return null;
  6373. }
  6374. var Selection = dom.Selection = function (doc) {
  6375. var me = this,
  6376. iframe;
  6377. me.document = doc;
  6378. if (browser.ie9below) {
  6379. iframe = domUtils.getWindow(doc).frameElement;
  6380. domUtils.on(iframe, 'beforedeactivate', function () {
  6381. me._bakIERange = me.getIERange();
  6382. });
  6383. domUtils.on(iframe, 'activate', function () {
  6384. try {
  6385. if (!_getIERange(me) && me._bakIERange) {
  6386. me._bakIERange.select();
  6387. }
  6388. } catch (ex) {}
  6389. me._bakIERange = null;
  6390. });
  6391. }
  6392. iframe = doc = null;
  6393. };
  6394. Selection.prototype = {
  6395. rangeInBody: function (rng, txtRange) {
  6396. var node = browser.ie9below || txtRange ? rng.item ? rng.item() : rng.parentElement() : rng.startContainer;
  6397. return node === this.document.body || domUtils.inDoc(node, this.document);
  6398. },
  6399. /**
  6400. * 获取原生seleciton对象
  6401. * @method getNative
  6402. * @return { Object } 获得selection对象
  6403. * @example
  6404. * ```javascript
  6405. * editor.selection.getNative();
  6406. * ```
  6407. */
  6408. getNative: function () {
  6409. var doc = this.document;
  6410. try {
  6411. return !doc ? null : browser.ie9below ? doc.selection : domUtils.getWindow(doc).getSelection();
  6412. } catch (e) {
  6413. return null;
  6414. }
  6415. },
  6416. /**
  6417. * 获得ieRange
  6418. * @method getIERange
  6419. * @return { Object } 返回ie原生的Range
  6420. * @example
  6421. * ```javascript
  6422. * editor.selection.getIERange();
  6423. * ```
  6424. */
  6425. getIERange: function () {
  6426. var ieRange = _getIERange(this);
  6427. if (!ieRange) {
  6428. if (this._bakIERange) {
  6429. return this._bakIERange;
  6430. }
  6431. }
  6432. return ieRange;
  6433. },
  6434. /**
  6435. * 缓存当前选区的range和选区的开始节点
  6436. * @method cache
  6437. */
  6438. cache: function () {
  6439. this.clear();
  6440. this._cachedRange = this.getRange();
  6441. this._cachedStartElement = this.getStart();
  6442. this._cachedStartElementPath = this.getStartElementPath();
  6443. },
  6444. /**
  6445. * 获取选区开始位置的父节点到body
  6446. * @method getStartElementPath
  6447. * @return { Array } 返回父节点集合
  6448. * @example
  6449. * ```javascript
  6450. * editor.selection.getStartElementPath();
  6451. * ```
  6452. */
  6453. getStartElementPath: function () {
  6454. if (this._cachedStartElementPath) {
  6455. return this._cachedStartElementPath;
  6456. }
  6457. var start = this.getStart();
  6458. if (start) {
  6459. return domUtils.findParents(start, true, null, true)
  6460. }
  6461. return [];
  6462. },
  6463. /**
  6464. * 清空缓存
  6465. * @method clear
  6466. */
  6467. clear: function () {
  6468. this._cachedStartElementPath = this._cachedRange = this._cachedStartElement = null;
  6469. },
  6470. /**
  6471. * 编辑器是否得到了选区
  6472. * @method isFocus
  6473. */
  6474. isFocus: function () {
  6475. try {
  6476. if (browser.ie9below) {
  6477. var nativeRange = _getIERange(this);
  6478. return !!(nativeRange && this.rangeInBody(nativeRange));
  6479. } else {
  6480. return !!this.getNative().rangeCount;
  6481. }
  6482. } catch (e) {
  6483. return false;
  6484. }
  6485. },
  6486. /**
  6487. * 获取选区对应的Range
  6488. * @method getRange
  6489. * @return { Object } 得到Range对象
  6490. * @example
  6491. * ```javascript
  6492. * editor.selection.getRange();
  6493. * ```
  6494. */
  6495. getRange: function () {
  6496. var me = this;
  6497. function optimze(range) {
  6498. var child = me.document.body.firstChild,
  6499. collapsed = range.collapsed;
  6500. while (child && child.firstChild) {
  6501. range.setStart(child, 0);
  6502. child = child.firstChild;
  6503. }
  6504. if (!range.startContainer) {
  6505. range.setStart(me.document.body, 0)
  6506. }
  6507. if (collapsed) {
  6508. range.collapse(true);
  6509. }
  6510. }
  6511. if (me._cachedRange != null) {
  6512. return this._cachedRange;
  6513. }
  6514. var range = new baidu.editor.dom.Range(me.document);
  6515. if (browser.ie9below) {
  6516. var nativeRange = me.getIERange();
  6517. if (nativeRange) {
  6518. //备份的_bakIERange可能已经实效了,dom树发生了变化比如从源码模式切回来,所以try一下,实效就放到body开始位置
  6519. try {
  6520. transformIERangeToRange(nativeRange, range);
  6521. } catch (e) {
  6522. optimze(range);
  6523. }
  6524. } else {
  6525. optimze(range);
  6526. }
  6527. } else {
  6528. var sel = me.getNative();
  6529. if (sel && sel.rangeCount) {
  6530. var firstRange = sel.getRangeAt(0);
  6531. var lastRange = sel.getRangeAt(sel.rangeCount - 1);
  6532. range.setStart(firstRange.startContainer, firstRange.startOffset).setEnd(lastRange.endContainer, lastRange.endOffset);
  6533. if (range.collapsed && domUtils.isBody(range.startContainer) && !range.startOffset) {
  6534. optimze(range);
  6535. }
  6536. } else {
  6537. //trace:1734 有可能已经不在dom树上了,标识的节点
  6538. if (this._bakRange && domUtils.inDoc(this._bakRange.startContainer, this.document)) {
  6539. return this._bakRange;
  6540. }
  6541. optimze(range);
  6542. }
  6543. }
  6544. return this._bakRange = range;
  6545. },
  6546. /**
  6547. * 获取开始元素,用于状态反射
  6548. * @method getStart
  6549. * @return { Element } 获得开始元素
  6550. * @example
  6551. * ```javascript
  6552. * editor.selection.getStart();
  6553. * ```
  6554. */
  6555. getStart: function () {
  6556. if (this._cachedStartElement) {
  6557. return this._cachedStartElement;
  6558. }
  6559. var range = browser.ie9below ? this.getIERange() : this.getRange(),
  6560. tmpRange,
  6561. start, tmp, parent;
  6562. if (browser.ie9below) {
  6563. if (!range) {
  6564. //todo 给第一个值可能会有问题
  6565. return this.document.body.firstChild;
  6566. }
  6567. //control元素
  6568. if (range.item) {
  6569. return range.item(0);
  6570. }
  6571. tmpRange = range.duplicate();
  6572. //修正ie下<b>x</b>[xx] 闭合后 <b>x|</b>xx
  6573. tmpRange.text.length > 0 && tmpRange.moveStart('character', 1);
  6574. tmpRange.collapse(1);
  6575. start = tmpRange.parentElement();
  6576. parent = tmp = range.parentElement();
  6577. while (tmp = tmp.parentNode) {
  6578. if (tmp == start) {
  6579. start = parent;
  6580. break;
  6581. }
  6582. }
  6583. } else {
  6584. range.shrinkBoundary();
  6585. start = range.startContainer;
  6586. if (start.nodeType == 1 && start.hasChildNodes()) {
  6587. start = start.childNodes[Math.min(start.childNodes.length - 1, range.startOffset)];
  6588. }
  6589. if (start.nodeType == 3) {
  6590. return start.parentNode;
  6591. }
  6592. }
  6593. return start;
  6594. },
  6595. /**
  6596. * 得到选区中的文本
  6597. * @method getText
  6598. * @return { String } 选区中包含的文本
  6599. * @example
  6600. * ```javascript
  6601. * editor.selection.getText();
  6602. * ```
  6603. */
  6604. getText: function () {
  6605. var nativeSel, nativeRange;
  6606. if (this.isFocus() && (nativeSel = this.getNative())) {
  6607. nativeRange = browser.ie9below ? nativeSel.createRange() : nativeSel.getRangeAt(0);
  6608. return browser.ie9below ? nativeRange.text : nativeRange.toString();
  6609. }
  6610. return '';
  6611. },
  6612. /**
  6613. * 清除选区
  6614. * @method clearRange
  6615. * @example
  6616. * ```javascript
  6617. * editor.selection.clearRange();
  6618. * ```
  6619. */
  6620. clearRange: function () {
  6621. this.getNative()[browser.ie9below ? 'empty' : 'removeAllRanges']();
  6622. }
  6623. };
  6624. })();
  6625. // core/Editor.js
  6626. /**
  6627. * 编辑器主类,包含编辑器提供的大部分公用接口
  6628. * @file
  6629. * @module UE
  6630. * @class Editor
  6631. * @since 1.2.6.1
  6632. */
  6633. /**
  6634. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  6635. * @unfile
  6636. * @module UE
  6637. */
  6638. /**
  6639. * UEditor的核心类,为用户提供与编辑器交互的接口。
  6640. * @unfile
  6641. * @module UE
  6642. * @class Editor
  6643. */
  6644. (function () {
  6645. var uid = 0,
  6646. _selectionChangeTimer;
  6647. /**
  6648. * 获取编辑器的html内容,赋值到编辑器所在表单的textarea文本域里面
  6649. * @private
  6650. * @method setValue
  6651. * @param { UE.Editor } editor 编辑器事例
  6652. */
  6653. function setValue(form, editor) {
  6654. var textarea;
  6655. if (editor.textarea) {
  6656. if (utils.isString(editor.textarea)) {
  6657. for (var i = 0, ti, tis = domUtils.getElementsByTagName(form, 'textarea'); ti = tis[i++];) {
  6658. if (ti.id == 'ueditor_textarea_' + editor.options.textarea) {
  6659. textarea = ti;
  6660. break;
  6661. }
  6662. }
  6663. } else {
  6664. textarea = editor.textarea;
  6665. }
  6666. }
  6667. if (!textarea) {
  6668. form.appendChild(textarea = domUtils.createElement(document, 'textarea', {
  6669. 'name': editor.options.textarea,
  6670. 'id': 'ueditor_textarea_' + editor.options.textarea,
  6671. 'style': "display:none"
  6672. }));
  6673. //不要产生多个textarea
  6674. editor.textarea = textarea;
  6675. }!textarea.getAttribute('name') && textarea.setAttribute('name', editor.options.textarea);
  6676. textarea.value = editor.hasContents() ?
  6677. (editor.options.allHtmlEnabled ? editor.getAllHtml() : editor.getContent(null, null, true)) :
  6678. ''
  6679. }
  6680. function loadPlugins(me) {
  6681. //初始化插件
  6682. for (var pi in UE.plugins) {
  6683. UE.plugins[pi].call(me);
  6684. }
  6685. }
  6686. function checkCurLang(I18N) {
  6687. for (var lang in I18N) {
  6688. return lang
  6689. }
  6690. }
  6691. function langReadied(me) {
  6692. me.langIsReady = true;
  6693. me.fireEvent("langReady");
  6694. }
  6695. /**
  6696. * 编辑器准备就绪后会触发该事件
  6697. * @module UE
  6698. * @class Editor
  6699. * @event ready
  6700. * @remind render方法执行完成之后,会触发该事件
  6701. * @remind
  6702. * @example
  6703. * ```javascript
  6704. * editor.addListener( 'ready', function( editor ) {
  6705. * editor.execCommand( 'focus' ); //编辑器家在完成后,让编辑器拿到焦点
  6706. * } );
  6707. * ```
  6708. */
  6709. /**
  6710. * 执行destroy方法,会触发该事件
  6711. * @module UE
  6712. * @class Editor
  6713. * @event destroy
  6714. * @see UE.Editor:destroy()
  6715. */
  6716. /**
  6717. * 执行reset方法,会触发该事件
  6718. * @module UE
  6719. * @class Editor
  6720. * @event reset
  6721. * @see UE.Editor:reset()
  6722. */
  6723. /**
  6724. * 执行focus方法,会触发该事件
  6725. * @module UE
  6726. * @class Editor
  6727. * @event focus
  6728. * @see UE.Editor:focus(Boolean)
  6729. */
  6730. /**
  6731. * 语言加载完成会触发该事件
  6732. * @module UE
  6733. * @class Editor
  6734. * @event langReady
  6735. */
  6736. /**
  6737. * 运行命令之后会触发该命令
  6738. * @module UE
  6739. * @class Editor
  6740. * @event beforeExecCommand
  6741. */
  6742. /**
  6743. * 运行命令之后会触发该命令
  6744. * @module UE
  6745. * @class Editor
  6746. * @event afterExecCommand
  6747. */
  6748. /**
  6749. * 运行命令之前会触发该命令
  6750. * @module UE
  6751. * @class Editor
  6752. * @event firstBeforeExecCommand
  6753. */
  6754. /**
  6755. * 在getContent方法执行之前会触发该事件
  6756. * @module UE
  6757. * @class Editor
  6758. * @event beforeGetContent
  6759. * @see UE.Editor:getContent()
  6760. */
  6761. /**
  6762. * 在getContent方法执行之后会触发该事件
  6763. * @module UE
  6764. * @class Editor
  6765. * @event afterGetContent
  6766. * @see UE.Editor:getContent()
  6767. */
  6768. /**
  6769. * 在getAllHtml方法执行时会触发该事件
  6770. * @module UE
  6771. * @class Editor
  6772. * @event getAllHtml
  6773. * @see UE.Editor:getAllHtml()
  6774. */
  6775. /**
  6776. * 在setContent方法执行之前会触发该事件
  6777. * @module UE
  6778. * @class Editor
  6779. * @event beforeSetContent
  6780. * @see UE.Editor:setContent(String)
  6781. */
  6782. /**
  6783. * 在setContent方法执行之后会触发该事件
  6784. * @module UE
  6785. * @class Editor
  6786. * @event afterSetContent
  6787. * @see UE.Editor:setContent(String)
  6788. */
  6789. /**
  6790. * 每当编辑器内部选区发生改变时,将触发该事件
  6791. * @event selectionchange
  6792. * @warning 该事件的触发非常频繁,不建议在该事件的处理过程中做重量级的处理
  6793. * @example
  6794. * ```javascript
  6795. * editor.addListener( 'selectionchange', function( editor ) {
  6796. * console.log('选区发生改变');
  6797. * }
  6798. */
  6799. /**
  6800. * 在所有selectionchange的监听函数执行之前,会触发该事件
  6801. * @module UE
  6802. * @class Editor
  6803. * @event beforeSelectionChange
  6804. * @see UE.Editor:selectionchange
  6805. */
  6806. /**
  6807. * 在所有selectionchange的监听函数执行完之后,会触发该事件
  6808. * @module UE
  6809. * @class Editor
  6810. * @event afterSelectionChange
  6811. * @see UE.Editor:selectionchange
  6812. */
  6813. /**
  6814. * 编辑器内容发生改变时会触发该事件
  6815. * @module UE
  6816. * @class Editor
  6817. * @event contentChange
  6818. */
  6819. /**
  6820. * 以默认参数构建一个编辑器实例
  6821. * @constructor
  6822. * @remind 通过 改构造方法实例化的编辑器,不带ui层.需要render到一个容器,编辑器实例才能正常渲染到页面
  6823. * @example
  6824. * ```javascript
  6825. * var editor = new UE.Editor();
  6826. * editor.execCommand('blod');
  6827. * ```
  6828. * @see UE.Config
  6829. */
  6830. /**
  6831. * 以给定的参数集合创建一个编辑器实例,对于未指定的参数,将应用默认参数。
  6832. * @constructor
  6833. * @remind 通过 改构造方法实例化的编辑器,不带ui层.需要render到一个容器,编辑器实例才能正常渲染到页面
  6834. * @param { Object } setting 创建编辑器的参数
  6835. * @example
  6836. * ```javascript
  6837. * var editor = new UE.Editor();
  6838. * editor.execCommand('blod');
  6839. * ```
  6840. * @see UE.Config
  6841. */
  6842. var Editor = UE.Editor = function (options) {
  6843. var me = this;
  6844. me.uid = uid++;
  6845. EventBase.call(me);
  6846. me.commands = {};
  6847. me.options = utils.extend(utils.clone(options || {}), UEDITOR_CONFIG, true);
  6848. me.shortcutkeys = {};
  6849. me.inputRules = [];
  6850. me.outputRules = [];
  6851. //设置默认的常用属性
  6852. me.setOpt(Editor.defaultOptions(me));
  6853. /* 尝试异步加载后台配置 */
  6854. me.loadServerConfig();
  6855. if (!utils.isEmptyObject(UE.I18N)) {
  6856. //修改默认的语言类型
  6857. me.options.lang = checkCurLang(UE.I18N);
  6858. UE.plugin.load(me);
  6859. langReadied(me);
  6860. } else {
  6861. utils.loadFile(document, {
  6862. src: me.options.langPath + me.options.lang + "/" + me.options.lang + ".js",
  6863. tag: "script",
  6864. type: "text/javascript",
  6865. defer: "defer"
  6866. }, function () {
  6867. UE.plugin.load(me);
  6868. langReadied(me);
  6869. });
  6870. }
  6871. UE.instants['ueditorInstant' + me.uid] = me;
  6872. };
  6873. Editor.prototype = {
  6874. registerCommand: function (name, obj) {
  6875. this.commands[name] = obj;
  6876. },
  6877. /**
  6878. * 编辑器对外提供的监听ready事件的接口, 通过调用该方法,达到的效果与监听ready事件是一致的
  6879. * @method ready
  6880. * @param { Function } fn 编辑器ready之后所执行的回调, 如果在注册事件之前编辑器已经ready,将会
  6881. * 立即触发该回调。
  6882. * @remind 需要等待编辑器加载完成后才能执行的代码,可以使用该方法传入
  6883. * @example
  6884. * ```javascript
  6885. * editor.ready( function( editor ) {
  6886. * editor.setContent('初始化完毕');
  6887. * } );
  6888. * ```
  6889. * @see UE.Editor.event:ready
  6890. */
  6891. ready: function (fn) {
  6892. var me = this;
  6893. if (fn) {
  6894. me.isReady ? fn.apply(me) : me.addListener('ready', fn);
  6895. }
  6896. },
  6897. /**
  6898. * 该方法是提供给插件里面使用,设置配置项默认值
  6899. * @method setOpt
  6900. * @warning 三处设置配置项的优先级: 实例化时传入参数 > setOpt()设置 > config文件里设置
  6901. * @warning 该方法仅供编辑器插件内部和编辑器初始化时调用,其他地方不能调用。
  6902. * @param { String } key 编辑器的可接受的选项名称
  6903. * @param { * } val 该选项可接受的值
  6904. * @example
  6905. * ```javascript
  6906. * editor.setOpt( 'initContent', '欢迎使用编辑器' );
  6907. * ```
  6908. */
  6909. /**
  6910. * 该方法是提供给插件里面使用,以{key:value}集合的方式设置插件内用到的配置项默认值
  6911. * @method setOpt
  6912. * @warning 三处设置配置项的优先级: 实例化时传入参数 > setOpt()设置 > config文件里设置
  6913. * @warning 该方法仅供编辑器插件内部和编辑器初始化时调用,其他地方不能调用。
  6914. * @param { Object } options 将要设置的选项的键值对对象
  6915. * @example
  6916. * ```javascript
  6917. * editor.setOpt( {
  6918. * 'initContent': '欢迎使用编辑器'
  6919. * } );
  6920. * ```
  6921. */
  6922. setOpt: function (key, val) {
  6923. var obj = {};
  6924. if (utils.isString(key)) {
  6925. obj[key] = val
  6926. } else {
  6927. obj = key;
  6928. }
  6929. utils.extend(this.options, obj, true);
  6930. },
  6931. getOpt: function (key) {
  6932. return this.options[key]
  6933. },
  6934. /**
  6935. * 销毁编辑器实例,使用textarea代替
  6936. * @method destroy
  6937. * @example
  6938. * ```javascript
  6939. * editor.destroy();
  6940. * ```
  6941. */
  6942. destroy: function () {
  6943. var me = this;
  6944. me.fireEvent('destroy');
  6945. var container = me.container.parentNode;
  6946. var textarea = me.textarea;
  6947. if (!textarea) {
  6948. textarea = document.createElement('textarea');
  6949. container.parentNode.insertBefore(textarea, container);
  6950. } else {
  6951. textarea.style.display = ''
  6952. }
  6953. textarea.style.width = me.iframe.offsetWidth + 'px';
  6954. textarea.style.height = me.iframe.offsetHeight + 'px';
  6955. textarea.value = me.getContent();
  6956. textarea.id = me.key;
  6957. container.innerHTML = '';
  6958. domUtils.remove(container);
  6959. var key = me.key;
  6960. //trace:2004
  6961. for (var p in me) {
  6962. if (me.hasOwnProperty(p)) {
  6963. delete this[p];
  6964. }
  6965. }
  6966. UE.delEditor(key);
  6967. },
  6968. /**
  6969. * 渲染编辑器的DOM到指定容器
  6970. * @method render
  6971. * @param { String } containerId 指定一个容器ID
  6972. * @remind 执行该方法,会触发ready事件
  6973. * @warning 必须且只能调用一次
  6974. */
  6975. /**
  6976. * 渲染编辑器的DOM到指定容器
  6977. * @method render
  6978. * @param { Element } containerDom 直接指定容器对象
  6979. * @remind 执行该方法,会触发ready事件
  6980. * @warning 必须且只能调用一次
  6981. */
  6982. render: function (container) {
  6983. var me = this,
  6984. options = me.options,
  6985. getStyleValue = function (attr) {
  6986. return parseInt(domUtils.getComputedStyle(container, attr));
  6987. };
  6988. if (utils.isString(container)) {
  6989. container = document.getElementById(container);
  6990. }
  6991. if (container) {
  6992. if (options.initialFrameWidth) {
  6993. options.minFrameWidth = options.initialFrameWidth
  6994. } else {
  6995. options.minFrameWidth = options.initialFrameWidth = container.offsetWidth;
  6996. }
  6997. if (options.initialFrameHeight) {
  6998. options.minFrameHeight = options.initialFrameHeight
  6999. } else {
  7000. options.initialFrameHeight = options.minFrameHeight = container.offsetHeight;
  7001. }
  7002. container.style.width = /%$/.test(options.initialFrameWidth) ? '100%' : options.initialFrameWidth -
  7003. getStyleValue("padding-left") - getStyleValue("padding-right") + 'px';
  7004. container.style.height = /%$/.test(options.initialFrameHeight) ? '100%' : options.initialFrameHeight -
  7005. getStyleValue("padding-top") - getStyleValue("padding-bottom") + 'px';
  7006. container.style.zIndex = options.zIndex;
  7007. var html = (ie && browser.version < 9 ? '' : '<!DOCTYPE html>') +
  7008. '<html xmlns=\'http://www.w3.org/1999/xhtml\' class=\'view\' ><head>' +
  7009. '<style type=\'text/css\'>' +
  7010. //设置四周的留边
  7011. '.view{padding:0;word-wrap:break-word;cursor:text;height:90%;}\n' +
  7012. //设置默认字体和字号
  7013. //font-family不能呢随便改,在safari下fillchar会有解析问题
  7014. 'body{margin:8px;font-family:sans-serif;font-size:16px;}' +
  7015. //设置段落间距
  7016. 'p{margin:5px 0;}</style>' +
  7017. (options.iframeCssUrl ? '<link rel=\'stylesheet\' type=\'text/css\' href=\'' + utils.unhtml(options.iframeCssUrl) + '\'/>' : '') +
  7018. (options.initialStyle ? '<style>' + options.initialStyle + '</style>' : '') +
  7019. '</head><body class=\'view\' ></body>' +
  7020. '<script type=\'text/javascript\' ' + (ie ? 'defer=\'defer\'' : '') + ' id=\'_initialScript\'>' +
  7021. 'setTimeout(function(){editor = window.parent.UE.instants[\'ueditorInstant' + me.uid + '\'];editor._setup(document);},0);' +
  7022. 'var _tmpScript = document.getElementById(\'_initialScript\');_tmpScript.parentNode.removeChild(_tmpScript);</script></html>';
  7023. container.appendChild(domUtils.createElement(document, 'iframe', {
  7024. id: 'ueditor_' + me.uid,
  7025. width: "100%",
  7026. height: "100%",
  7027. frameborder: "0",
  7028. //先注释掉了,加的原因忘记了,但开启会直接导致全屏模式下内容多时不会出现滚动条
  7029. // scrolling :'no',
  7030. src: 'javascript:void(function(){document.open();' + (options.customDomain && document.domain != location.hostname ? 'document.domain="' + document.domain + '";' : '') +
  7031. 'document.write("' + html + '");document.close();}())'
  7032. }));
  7033. container.style.overflow = 'hidden';
  7034. //解决如果是给定的百分比,会导致高度算不对的问题
  7035. setTimeout(function () {
  7036. if (/%$/.test(options.initialFrameWidth)) {
  7037. options.minFrameWidth = options.initialFrameWidth = container.offsetWidth;
  7038. //如果这里给定宽度,会导致ie在拖动窗口大小时,编辑区域不随着变化
  7039. // container.style.width = options.initialFrameWidth + 'px';
  7040. }
  7041. if (/%$/.test(options.initialFrameHeight)) {
  7042. options.minFrameHeight = options.initialFrameHeight = container.offsetHeight;
  7043. container.style.height = options.initialFrameHeight + 'px';
  7044. }
  7045. })
  7046. }
  7047. },
  7048. /**
  7049. * 编辑器初始化
  7050. * @method _setup
  7051. * @private
  7052. * @param { Element } doc 编辑器Iframe中的文档对象
  7053. */
  7054. _setup: function (doc) {
  7055. var me = this,
  7056. options = me.options;
  7057. if (ie) {
  7058. doc.body.disabled = true;
  7059. doc.body.contentEditable = true;
  7060. doc.body.disabled = false;
  7061. } else {
  7062. doc.body.contentEditable = true;
  7063. }
  7064. doc.body.spellcheck = false;
  7065. me.document = doc;
  7066. me.window = doc.defaultView || doc.parentWindow;
  7067. me.iframe = me.window.frameElement;
  7068. me.body = doc.body;
  7069. me.selection = new dom.Selection(doc);
  7070. //gecko初始化就能得到range,无法判断isFocus了
  7071. var geckoSel;
  7072. if (browser.gecko && (geckoSel = this.selection.getNative())) {
  7073. geckoSel.removeAllRanges();
  7074. }
  7075. this._initEvents();
  7076. //为form提交提供一个隐藏的textarea
  7077. for (var form = this.iframe.parentNode; !domUtils.isBody(form); form = form.parentNode) {
  7078. if (form.tagName == 'FORM') {
  7079. me.form = form;
  7080. if (me.options.autoSyncData) {
  7081. domUtils.on(me.window, 'blur', function () {
  7082. setValue(form, me);
  7083. });
  7084. } else {
  7085. domUtils.on(form, 'submit', function () {
  7086. setValue(this, me);
  7087. });
  7088. }
  7089. break;
  7090. }
  7091. }
  7092. if (options.initialContent) {
  7093. if (options.autoClearinitialContent) {
  7094. var oldExecCommand = me.execCommand;
  7095. me.execCommand = function () {
  7096. me.fireEvent('firstBeforeExecCommand');
  7097. return oldExecCommand.apply(me, arguments);
  7098. };
  7099. this._setDefaultContent(options.initialContent);
  7100. } else
  7101. this.setContent(options.initialContent, false, true);
  7102. }
  7103. //编辑器不能为空内容
  7104. if (domUtils.isEmptyNode(me.body)) {
  7105. me.body.innerHTML = '<p>' + (browser.ie ? '' : '<br/>') + '</p>';
  7106. }
  7107. //如果要求focus, 就把光标定位到内容开始
  7108. if (options.focus) {
  7109. setTimeout(function () {
  7110. me.focus(me.options.focusInEnd);
  7111. //如果自动清除开着,就不需要做selectionchange;
  7112. !me.options.autoClearinitialContent && me._selectionChange();
  7113. }, 0);
  7114. }
  7115. if (!me.container) {
  7116. me.container = this.iframe.parentNode;
  7117. }
  7118. if (options.fullscreen && me.ui) {
  7119. me.ui.setFullScreen(true);
  7120. }
  7121. try {
  7122. me.document.execCommand('2D-position', false, false);
  7123. } catch (e) {}
  7124. try {
  7125. me.document.execCommand('enableInlineTableEditing', false, false);
  7126. } catch (e) {}
  7127. try {
  7128. me.document.execCommand('enableObjectResizing', false, false);
  7129. } catch (e) {}
  7130. //挂接快捷键
  7131. me._bindshortcutKeys();
  7132. me.isReady = 1;
  7133. me.fireEvent('ready');
  7134. options.onready && options.onready.call(me);
  7135. if (!browser.ie9below) {
  7136. domUtils.on(me.window, ['blur', 'focus'], function (e) {
  7137. //chrome下会出现alt+tab切换时,导致选区位置不对
  7138. if (e.type == 'blur') {
  7139. me._bakRange = me.selection.getRange();
  7140. try {
  7141. me._bakNativeRange = me.selection.getNative().getRangeAt(0);
  7142. me.selection.getNative().removeAllRanges();
  7143. } catch (e) {
  7144. me._bakNativeRange = null;
  7145. }
  7146. } else {
  7147. try {
  7148. me._bakRange && me._bakRange.select();
  7149. } catch (e) {}
  7150. }
  7151. });
  7152. }
  7153. //trace:1518 ff3.6body不够寛,会导致点击空白处无法获得焦点
  7154. if (browser.gecko && browser.version <= 10902) {
  7155. //修复ff3.6初始化进来,不能点击获得焦点
  7156. me.body.contentEditable = false;
  7157. setTimeout(function () {
  7158. me.body.contentEditable = true;
  7159. }, 100);
  7160. setInterval(function () {
  7161. me.body.style.height = me.iframe.offsetHeight - 20 + 'px'
  7162. }, 100)
  7163. }
  7164. !options.isShow && me.setHide();
  7165. options.readonly && me.setDisabled();
  7166. },
  7167. /**
  7168. * 同步数据到编辑器所在的form
  7169. * 从编辑器的容器节点向上查找form元素,若找到,就同步编辑内容到找到的form里,为提交数据做准备,主要用于是手动提交的情况
  7170. * 后台取得数据的键值,使用你容器上的name属性,如果没有就使用参数里的textarea项
  7171. * @method sync
  7172. * @example
  7173. * ```javascript
  7174. * editor.sync();
  7175. * form.sumbit(); //form变量已经指向了form元素
  7176. * ```
  7177. */
  7178. /**
  7179. * 根据传入的formId,在页面上查找要同步数据的表单,若找到,就同步编辑内容到找到的form里,为提交数据做准备
  7180. * 后台取得数据的键值,该键值默认使用给定的编辑器容器的name属性,如果没有name属性则使用参数项里给定的“textarea”项
  7181. * @method sync
  7182. * @param { String } formID 指定一个要同步数据的form的id,编辑器的数据会同步到你指定form下
  7183. */
  7184. sync: function (formId) {
  7185. var me = this,
  7186. form = formId ? document.getElementById(formId) :
  7187. domUtils.findParent(me.iframe.parentNode, function (node) {
  7188. return node.tagName == 'FORM'
  7189. }, true);
  7190. form && setValue(form, me);
  7191. },
  7192. /**
  7193. * 设置编辑器高度
  7194. * @method setHeight
  7195. * @remind 当配置项autoHeightEnabled为真时,该方法无效
  7196. * @param { Number } number 设置的高度值,纯数值,不带单位
  7197. * @example
  7198. * ```javascript
  7199. * editor.setHeight(number);
  7200. * ```
  7201. */
  7202. setHeight: function (height, notSetHeight) {
  7203. if (height !== parseInt(this.iframe.parentNode.style.height)) {
  7204. this.iframe.parentNode.style.height = height + 'px';
  7205. }!notSetHeight && (this.options.minFrameHeight = this.options.initialFrameHeight = height);
  7206. this.body.style.height = height + 'px';
  7207. !notSetHeight && this.trigger('setHeight')
  7208. },
  7209. /**
  7210. * 为编辑器的编辑命令提供快捷键
  7211. * 这个接口是为插件扩展提供的接口,主要是为新添加的插件,如果需要添加快捷键,所提供的接口
  7212. * @method addshortcutkey
  7213. * @param { Object } keyset 命令名和快捷键键值对对象,多个按钮的快捷键用“+”分隔
  7214. * @example
  7215. * ```javascript
  7216. * editor.addshortcutkey({
  7217. * "Bold" : "ctrl+66",//^B
  7218. * "Italic" : "ctrl+73", //^I
  7219. * });
  7220. * ```
  7221. */
  7222. /**
  7223. * 这个接口是为插件扩展提供的接口,主要是为新添加的插件,如果需要添加快捷键,所提供的接口
  7224. * @method addshortcutkey
  7225. * @param { String } cmd 触发快捷键时,响应的命令
  7226. * @param { String } keys 快捷键的字符串,多个按钮用“+”分隔
  7227. * @example
  7228. * ```javascript
  7229. * editor.addshortcutkey("Underline", "ctrl+85"); //^U
  7230. * ```
  7231. */
  7232. addshortcutkey: function (cmd, keys) {
  7233. var obj = {};
  7234. if (keys) {
  7235. obj[cmd] = keys
  7236. } else {
  7237. obj = cmd;
  7238. }
  7239. utils.extend(this.shortcutkeys, obj)
  7240. },
  7241. /**
  7242. * 对编辑器设置keydown事件监听,绑定快捷键和命令,当快捷键组合触发成功,会响应对应的命令
  7243. * @method _bindshortcutKeys
  7244. * @private
  7245. */
  7246. _bindshortcutKeys: function () {
  7247. var me = this,
  7248. shortcutkeys = this.shortcutkeys;
  7249. me.addListener('keydown', function (type, e) {
  7250. var keyCode = e.keyCode || e.which;
  7251. for (var i in shortcutkeys) {
  7252. var tmp = shortcutkeys[i].split(',');
  7253. for (var t = 0, ti; ti = tmp[t++];) {
  7254. ti = ti.split(':');
  7255. var key = ti[0],
  7256. param = ti[1];
  7257. if (/^(ctrl)(\+shift)?\+(\d+)$/.test(key.toLowerCase()) || /^(\d+)$/.test(key)) {
  7258. if (((RegExp.$1 == 'ctrl' ? (e.ctrlKey || e.metaKey) : 0) &&
  7259. (RegExp.$2 != "" ? e[RegExp.$2.slice(1) + "Key"] : 1) &&
  7260. keyCode == RegExp.$3
  7261. ) ||
  7262. keyCode == RegExp.$1
  7263. ) {
  7264. if (me.queryCommandState(i, param) != -1)
  7265. me.execCommand(i, param);
  7266. domUtils.preventDefault(e);
  7267. }
  7268. }
  7269. }
  7270. }
  7271. });
  7272. },
  7273. /**
  7274. * 获取编辑器的内容
  7275. * @method getContent
  7276. * @warning 该方法获取到的是经过编辑器内置的过滤规则进行过滤后得到的内容
  7277. * @return { String } 编辑器的内容字符串, 如果编辑器的内容为空,或者是空的标签内容(如:”&lt;p&gt;&lt;br/&gt;&lt;/p&gt;“), 则返回空字符串
  7278. * @example
  7279. * ```javascript
  7280. * //编辑器html内容:<p>1<strong>2<em>34</em>5</strong>6</p>
  7281. * var content = editor.getContent(); //返回值:<p>1<strong>2<em>34</em>5</strong>6</p>
  7282. * ```
  7283. */
  7284. /**
  7285. * 获取编辑器的内容。 可以通过参数定义编辑器内置的判空规则
  7286. * @method getContent
  7287. * @param { Function } fn 自定的判空规则, 要求该方法返回一个boolean类型的值,
  7288. * 代表当前编辑器的内容是否空,
  7289. * 如果返回true, 则该方法将直接返回空字符串;如果返回false,则编辑器将返回
  7290. * 经过内置过滤规则处理后的内容。
  7291. * @remind 该方法在处理包含有初始化内容的时候能起到很好的作用。
  7292. * @warning 该方法获取到的是经过编辑器内置的过滤规则进行过滤后得到的内容
  7293. * @return { String } 编辑器的内容字符串
  7294. * @example
  7295. * ```javascript
  7296. * // editor 是一个编辑器的实例
  7297. * var content = editor.getContent( function ( editor ) {
  7298. * return editor.body.innerHTML === '欢迎使用UEditor'; //返回空字符串
  7299. * } );
  7300. * ```
  7301. */
  7302. getContent: function (cmd, fn, notSetCursor, ignoreBlank, formatter) {
  7303. var me = this;
  7304. if (cmd && utils.isFunction(cmd)) {
  7305. fn = cmd;
  7306. cmd = '';
  7307. }
  7308. if (fn ? !fn() : !this.hasContents()) {
  7309. return '';
  7310. }
  7311. me.fireEvent('beforegetcontent');
  7312. var root = UE.htmlparser(me.body.innerHTML, ignoreBlank);
  7313. me.filterOutputRule(root);
  7314. me.fireEvent('aftergetcontent', cmd, root);
  7315. return root.toHtml(formatter);
  7316. },
  7317. /**
  7318. * 取得完整的html代码,可以直接显示成完整的html文档
  7319. * @method getAllHtml
  7320. * @return { String } 编辑器的内容html文档字符串
  7321. * @eaxmple
  7322. * ```javascript
  7323. * editor.getAllHtml(); //返回格式大致是: <html><head>...</head><body>...</body></html>
  7324. * ```
  7325. */
  7326. getAllHtml: function () {
  7327. var me = this,
  7328. headHtml = [],
  7329. html = '';
  7330. me.fireEvent('getAllHtml', headHtml);
  7331. if (browser.ie && browser.version > 8) {
  7332. var headHtmlForIE9 = '';
  7333. utils.each(me.document.styleSheets, function (si) {
  7334. headHtmlForIE9 += (si.href ? '<link rel="stylesheet" type="text/css" href="' + si.href + '" />' : '<style>' + si.cssText + '</style>');
  7335. });
  7336. utils.each(me.document.getElementsByTagName('script'), function (si) {
  7337. headHtmlForIE9 += si.outerHTML;
  7338. });
  7339. }
  7340. return '<html><head>' + (me.options.charset ? '<meta http-equiv="Content-Type" content="text/html; charset=' + me.options.charset + '"/>' : '') +
  7341. (headHtmlForIE9 || me.document.getElementsByTagName('head')[0].innerHTML) + headHtml.join('\n') + '</head>' +
  7342. '<body ' + (ie && browser.version < 9 ? 'class="view"' : '') + '>' + me.getContent(null, null, true) + '</body></html>';
  7343. },
  7344. /**
  7345. * 得到编辑器的纯文本内容,但会保留段落格式
  7346. * @method getPlainTxt
  7347. * @return { String } 编辑器带段落格式的纯文本内容字符串
  7348. * @example
  7349. * ```javascript
  7350. * //编辑器html内容:<p><strong>1</strong></p><p><strong>2</strong></p>
  7351. * console.log(editor.getPlainTxt()); //输出:"1\n2\n
  7352. * ```
  7353. */
  7354. getPlainTxt: function () {
  7355. var reg = new RegExp(domUtils.fillChar, 'g'),
  7356. html = this.body.innerHTML.replace(/[\n\r]/g, ''); //ie要先去了\n在处理
  7357. html = html.replace(/<(p|div)[^>]*>(<br\/?>|&nbsp;)<\/\1>/gi, '\n')
  7358. .replace(/<br\/?>/gi, '\n')
  7359. .replace(/<[^>/]+>/g, '')
  7360. .replace(/(\n)?<\/([^>]+)>/g, function (a, b, c) {
  7361. return dtd.$block[c] ? '\n' : b ? b : '';
  7362. });
  7363. //取出来的空格会有c2a0会变成乱码,处理这种情况\u00a0
  7364. return html.replace(reg, '').replace(/\u00a0/g, ' ').replace(/&nbsp;/g, ' ');
  7365. },
  7366. /**
  7367. * 获取编辑器中的纯文本内容,没有段落格式
  7368. * @method getContentTxt
  7369. * @return { String } 编辑器不带段落格式的纯文本内容字符串
  7370. * @example
  7371. * ```javascript
  7372. * //编辑器html内容:<p><strong>1</strong></p><p><strong>2</strong></p>
  7373. * console.log(editor.getPlainTxt()); //输出:"12
  7374. * ```
  7375. */
  7376. getContentTxt: function () {
  7377. var reg = new RegExp(domUtils.fillChar, 'g');
  7378. //取出来的空格会有c2a0会变成乱码,处理这种情况\u00a0
  7379. return this.body[browser.ie ? 'innerText' : 'textContent'].replace(reg, '').replace(/\u00a0/g, ' ');
  7380. },
  7381. /**
  7382. * 设置编辑器的内容,可修改编辑器当前的html内容
  7383. * @method setContent
  7384. * @warning 通过该方法插入的内容,是经过编辑器内置的过滤规则进行过滤后得到的内容
  7385. * @warning 该方法会触发selectionchange事件
  7386. * @param { String } html 要插入的html内容
  7387. * @example
  7388. * ```javascript
  7389. * editor.getContent('<p>test</p>');
  7390. * ```
  7391. */
  7392. /**
  7393. * 设置编辑器的内容,可修改编辑器当前的html内容
  7394. * @method setContent
  7395. * @warning 通过该方法插入的内容,是经过编辑器内置的过滤规则进行过滤后得到的内容
  7396. * @warning 该方法会触发selectionchange事件
  7397. * @param { String } html 要插入的html内容
  7398. * @param { Boolean } isAppendTo 若传入true,不清空原来的内容,在最后插入内容,否则,清空内容再插入
  7399. * @example
  7400. * ```javascript
  7401. * //假设设置前的编辑器内容是 <p>old text</p>
  7402. * editor.setContent('<p>new text</p>', true); //插入的结果是<p>old text</p><p>new text</p>
  7403. * ```
  7404. */
  7405. setContent: function (html, isAppendTo, notFireSelectionchange) {
  7406. var me = this;
  7407. me.fireEvent('beforesetcontent', html);
  7408. var root = UE.htmlparser(html);
  7409. me.filterInputRule(root);
  7410. html = root.toHtml();
  7411. me.body.innerHTML = (isAppendTo ? me.body.innerHTML : '') + html;
  7412. function isCdataDiv(node) {
  7413. return node.tagName == 'DIV' && node.getAttribute('cdata_tag');
  7414. }
  7415. //给文本或者inline节点套p标签
  7416. if (me.options.enterTag == 'p') {
  7417. var child = this.body.firstChild,
  7418. tmpNode;
  7419. if (!child || child.nodeType == 1 &&
  7420. (dtd.$cdata[child.tagName] || isCdataDiv(child) ||
  7421. domUtils.isCustomeNode(child)
  7422. ) &&
  7423. child === this.body.lastChild) {
  7424. this.body.innerHTML = '<p>' + (browser.ie ? '&nbsp;' : '<br/>') + '</p>' + this.body.innerHTML;
  7425. } else {
  7426. var p = me.document.createElement('p');
  7427. while (child) {
  7428. while (child && (child.nodeType == 3 || child.nodeType == 1 && dtd.p[child.tagName] && !dtd.$cdata[child.tagName])) {
  7429. tmpNode = child.nextSibling;
  7430. p.appendChild(child);
  7431. child = tmpNode;
  7432. }
  7433. if (p.firstChild) {
  7434. if (!child) {
  7435. me.body.appendChild(p);
  7436. break;
  7437. } else {
  7438. child.parentNode.insertBefore(p, child);
  7439. p = me.document.createElement('p');
  7440. }
  7441. }
  7442. child = child.nextSibling;
  7443. }
  7444. }
  7445. }
  7446. me.fireEvent('aftersetcontent');
  7447. me.fireEvent('contentchange');
  7448. !notFireSelectionchange && me._selectionChange();
  7449. //清除保存的选区
  7450. me._bakRange = me._bakIERange = me._bakNativeRange = null;
  7451. //trace:1742 setContent后gecko能得到焦点问题
  7452. var geckoSel;
  7453. if (browser.gecko && (geckoSel = this.selection.getNative())) {
  7454. geckoSel.removeAllRanges();
  7455. }
  7456. if (me.options.autoSyncData) {
  7457. me.form && setValue(me.form, me);
  7458. }
  7459. },
  7460. /**
  7461. * 让编辑器获得焦点,默认focus到编辑器头部
  7462. * @method focus
  7463. * @example
  7464. * ```javascript
  7465. * editor.focus()
  7466. * ```
  7467. */
  7468. /**
  7469. * 让编辑器获得焦点,toEnd确定focus位置
  7470. * @method focus
  7471. * @param { Boolean } toEnd 默认focus到编辑器头部,toEnd为true时focus到内容尾部
  7472. * @example
  7473. * ```javascript
  7474. * editor.focus(true)
  7475. * ```
  7476. */
  7477. focus: function (toEnd) {
  7478. try {
  7479. var me = this,
  7480. rng = me.selection.getRange();
  7481. if (toEnd) {
  7482. var node = me.body.lastChild;
  7483. if (node && node.nodeType == 1 && !dtd.$empty[node.tagName]) {
  7484. if (domUtils.isEmptyBlock(node)) {
  7485. rng.setStartAtFirst(node)
  7486. } else {
  7487. rng.setStartAtLast(node)
  7488. }
  7489. rng.collapse(true);
  7490. }
  7491. rng.setCursor(true);
  7492. } else {
  7493. if (!rng.collapsed && domUtils.isBody(rng.startContainer) && rng.startOffset == 0) {
  7494. var node = me.body.firstChild;
  7495. if (node && node.nodeType == 1 && !dtd.$empty[node.tagName]) {
  7496. rng.setStartAtFirst(node).collapse(true);
  7497. }
  7498. }
  7499. rng.select(true);
  7500. }
  7501. this.fireEvent('focus selectionchange');
  7502. } catch (e) {}
  7503. },
  7504. isFocus: function () {
  7505. return this.selection.isFocus();
  7506. },
  7507. blur: function () {
  7508. var sel = this.selection.getNative();
  7509. if (sel.empty && browser.ie) {
  7510. var nativeRng = document.body.createTextRange();
  7511. nativeRng.moveToElementText(document.body);
  7512. nativeRng.collapse(true);
  7513. nativeRng.select();
  7514. sel.empty()
  7515. } else {
  7516. sel.removeAllRanges()
  7517. }
  7518. //this.fireEvent('blur selectionchange');
  7519. },
  7520. /**
  7521. * 初始化UE事件及部分事件代理
  7522. * @method _initEvents
  7523. * @private
  7524. */
  7525. _initEvents: function () {
  7526. var me = this,
  7527. doc = me.document,
  7528. win = me.window;
  7529. me._proxyDomEvent = utils.bind(me._proxyDomEvent, me);
  7530. domUtils.on(doc, ['click', 'contextmenu', 'mousedown', 'keydown', 'keyup', 'keypress', 'mouseup', 'mouseover', 'mouseout', 'selectstart'], me._proxyDomEvent);
  7531. domUtils.on(win, ['focus', 'blur'], me._proxyDomEvent);
  7532. domUtils.on(me.body, 'drop', function (e) {
  7533. //阻止ff下默认的弹出新页面打开图片
  7534. if (browser.gecko && e.stopPropagation) {
  7535. e.stopPropagation();
  7536. }
  7537. me.fireEvent('contentchange')
  7538. });
  7539. domUtils.on(doc, ['mouseup', 'keydown'], function (evt) {
  7540. //特殊键不触发selectionchange
  7541. if (evt.type == 'keydown' && (evt.ctrlKey || evt.metaKey || evt.shiftKey || evt.altKey)) {
  7542. return;
  7543. }
  7544. if (evt.button == 2) return;
  7545. me._selectionChange(250, evt);
  7546. });
  7547. },
  7548. /**
  7549. * 触发事件代理
  7550. * @method _proxyDomEvent
  7551. * @private
  7552. * @return { * } fireEvent的返回值
  7553. * @see UE.EventBase:fireEvent(String)
  7554. */
  7555. _proxyDomEvent: function (evt) {
  7556. if (this.fireEvent('before' + evt.type.replace(/^on/, '').toLowerCase()) === false) {
  7557. return false;
  7558. }
  7559. if (this.fireEvent(evt.type.replace(/^on/, ''), evt) === false) {
  7560. return false;
  7561. }
  7562. return this.fireEvent('after' + evt.type.replace(/^on/, '').toLowerCase())
  7563. },
  7564. /**
  7565. * 变化选区
  7566. * @method _selectionChange
  7567. * @private
  7568. */
  7569. _selectionChange: function (delay, evt) {
  7570. var me = this;
  7571. //有光标才做selectionchange 为了解决未focus时点击source不能触发更改工具栏状态的问题(source命令notNeedUndo=1)
  7572. // if ( !me.selection.isFocus() ){
  7573. // return;
  7574. // }
  7575. var hackForMouseUp = false;
  7576. var mouseX, mouseY;
  7577. if (browser.ie && browser.version < 9 && evt && evt.type == 'mouseup') {
  7578. var range = this.selection.getRange();
  7579. if (!range.collapsed) {
  7580. hackForMouseUp = true;
  7581. mouseX = evt.clientX;
  7582. mouseY = evt.clientY;
  7583. }
  7584. }
  7585. clearTimeout(_selectionChangeTimer);
  7586. _selectionChangeTimer = setTimeout(function () {
  7587. if (!me.selection || !me.selection.getNative()) {
  7588. return;
  7589. }
  7590. //修复一个IE下的bug: 鼠标点击一段已选择的文本中间时,可能在mouseup后的一段时间内取到的range是在selection的type为None下的错误值.
  7591. //IE下如果用户是拖拽一段已选择文本,则不会触发mouseup事件,所以这里的特殊处理不会对其有影响
  7592. var ieRange;
  7593. if (hackForMouseUp && me.selection.getNative().type == 'None') {
  7594. ieRange = me.document.body.createTextRange();
  7595. try {
  7596. ieRange.moveToPoint(mouseX, mouseY);
  7597. } catch (ex) {
  7598. ieRange = null;
  7599. }
  7600. }
  7601. var bakGetIERange;
  7602. if (ieRange) {
  7603. bakGetIERange = me.selection.getIERange;
  7604. me.selection.getIERange = function () {
  7605. return ieRange;
  7606. };
  7607. }
  7608. me.selection.cache();
  7609. if (bakGetIERange) {
  7610. me.selection.getIERange = bakGetIERange;
  7611. }
  7612. if (me.selection._cachedRange && me.selection._cachedStartElement) {
  7613. me.fireEvent('beforeselectionchange');
  7614. // 第二个参数causeByUi为true代表由用户交互造成的selectionchange.
  7615. me.fireEvent('selectionchange', !!evt);
  7616. me.fireEvent('afterselectionchange');
  7617. me.selection.clear();
  7618. }
  7619. }, delay || 50);
  7620. },
  7621. /**
  7622. * 执行编辑命令
  7623. * @method _callCmdFn
  7624. * @private
  7625. * @param { String } fnName 函数名称
  7626. * @param { * } args 传给命令函数的参数
  7627. * @return { * } 返回命令函数运行的返回值
  7628. */
  7629. _callCmdFn: function (fnName, args) {
  7630. var cmdName = args[0].toLowerCase(),
  7631. cmd, cmdFn;
  7632. cmd = this.commands[cmdName] || UE.commands[cmdName];
  7633. cmdFn = cmd && cmd[fnName];
  7634. //没有querycommandstate或者没有command的都默认返回0
  7635. if ((!cmd || !cmdFn) && fnName == 'queryCommandState') {
  7636. return 0;
  7637. } else if (cmdFn) {
  7638. return cmdFn.apply(this, args);
  7639. }
  7640. },
  7641. /**
  7642. * 执行编辑命令cmdName,完成富文本编辑效果
  7643. * @method execCommand
  7644. * @param { String } cmdName 需要执行的命令
  7645. * @remind 具体命令的使用请参考<a href="#COMMAND.LIST">命令列表</a>
  7646. * @return { * } 返回命令函数运行的返回值
  7647. * @example
  7648. * ```javascript
  7649. * editor.execCommand(cmdName);
  7650. * ```
  7651. */
  7652. execCommand: function (cmdName) {
  7653. cmdName = cmdName.toLowerCase();
  7654. var me = this,
  7655. result,
  7656. cmd = me.commands[cmdName] || UE.commands[cmdName];
  7657. if (!cmd || !cmd.execCommand) {
  7658. return null;
  7659. }
  7660. if (!cmd.notNeedUndo && !me.__hasEnterExecCommand) {
  7661. me.__hasEnterExecCommand = true;
  7662. if (me.queryCommandState.apply(me, arguments) != -1) {
  7663. me.fireEvent('saveScene');
  7664. me.fireEvent.apply(me, ['beforeexeccommand', cmdName].concat(arguments));
  7665. result = this._callCmdFn('execCommand', arguments);
  7666. //保存场景时,做了内容对比,再看是否进行contentchange触发,这里多触发了一次,去掉
  7667. // (!cmd.ignoreContentChange && !me._ignoreContentChange) && me.fireEvent('contentchange');
  7668. me.fireEvent.apply(me, ['afterexeccommand', cmdName].concat(arguments));
  7669. me.fireEvent('saveScene');
  7670. }
  7671. me.__hasEnterExecCommand = false;
  7672. } else {
  7673. result = this._callCmdFn('execCommand', arguments);
  7674. (!me.__hasEnterExecCommand && !cmd.ignoreContentChange && !me._ignoreContentChange) && me.fireEvent('contentchange')
  7675. }
  7676. (!me.__hasEnterExecCommand && !cmd.ignoreContentChange && !me._ignoreContentChange) && me._selectionChange();
  7677. return result;
  7678. },
  7679. /**
  7680. * 根据传入的command命令,查选编辑器当前的选区,返回命令的状态
  7681. * @method queryCommandState
  7682. * @param { String } cmdName 需要查询的命令名称
  7683. * @remind 具体命令的使用请参考<a href="#COMMAND.LIST">命令列表</a>
  7684. * @return { Number } number 返回放前命令的状态,返回值三种情况:(-1|0|1)
  7685. * @example
  7686. * ```javascript
  7687. * editor.queryCommandState(cmdName) => (-1|0|1)
  7688. * ```
  7689. * @see COMMAND.LIST
  7690. */
  7691. queryCommandState: function (cmdName) {
  7692. return this._callCmdFn('queryCommandState', arguments);
  7693. },
  7694. /**
  7695. * 根据传入的command命令,查选编辑器当前的选区,根据命令返回相关的值
  7696. * @method queryCommandValue
  7697. * @param { String } cmdName 需要查询的命令名称
  7698. * @remind 具体命令的使用请参考<a href="#COMMAND.LIST">命令列表</a>
  7699. * @remind 只有部分插件有此方法
  7700. * @return { * } 返回每个命令特定的当前状态值
  7701. * @grammar editor.queryCommandValue(cmdName) => {*}
  7702. * @see COMMAND.LIST
  7703. */
  7704. queryCommandValue: function (cmdName) {
  7705. return this._callCmdFn('queryCommandValue', arguments);
  7706. },
  7707. /**
  7708. * 检查编辑区域中是否有内容
  7709. * @method hasContents
  7710. * @remind 默认有文本内容,或者有以下节点都不认为是空
  7711. * table,ul,ol,dl,iframe,area,base,col,hr,img,embed,input,link,meta,param
  7712. * @return { Boolean } 检查有内容返回true,否则返回false
  7713. * @example
  7714. * ```javascript
  7715. * editor.hasContents()
  7716. * ```
  7717. */
  7718. /**
  7719. * 检查编辑区域中是否有内容,若包含参数tags中的节点类型,直接返回true
  7720. * @method hasContents
  7721. * @param { Array } tags 传入数组判断时用到的节点类型
  7722. * @return { Boolean } 若文档中包含tags数组里对应的tag,返回true,否则返回false
  7723. * @example
  7724. * ```javascript
  7725. * editor.hasContents(['span']);
  7726. * ```
  7727. */
  7728. hasContents: function (tags) {
  7729. if (tags) {
  7730. for (var i = 0, ci; ci = tags[i++];) {
  7731. if (this.document.getElementsByTagName(ci).length > 0) {
  7732. return true;
  7733. }
  7734. }
  7735. }
  7736. if (!domUtils.isEmptyBlock(this.body)) {
  7737. return true
  7738. }
  7739. //随时添加,定义的特殊标签如果存在,不能认为是空
  7740. tags = ['div'];
  7741. for (i = 0; ci = tags[i++];) {
  7742. var nodes = domUtils.getElementsByTagName(this.document, ci);
  7743. for (var n = 0, cn; cn = nodes[n++];) {
  7744. if (domUtils.isCustomeNode(cn)) {
  7745. return true;
  7746. }
  7747. }
  7748. }
  7749. return false;
  7750. },
  7751. /**
  7752. * 重置编辑器,可用来做多个tab使用同一个编辑器实例
  7753. * @method reset
  7754. * @remind 此方法会清空编辑器内容,清空回退列表,会触发reset事件
  7755. * @example
  7756. * ```javascript
  7757. * editor.reset()
  7758. * ```
  7759. */
  7760. reset: function () {
  7761. this.fireEvent('reset');
  7762. },
  7763. /**
  7764. * 设置当前编辑区域可以编辑
  7765. * @method setEnabled
  7766. * @example
  7767. * ```javascript
  7768. * editor.setEnabled()
  7769. * ```
  7770. */
  7771. setEnabled: function () {
  7772. var me = this,
  7773. range;
  7774. if (me.body.contentEditable == 'false') {
  7775. me.body.contentEditable = true;
  7776. range = me.selection.getRange();
  7777. //有可能内容丢失了
  7778. try {
  7779. range.moveToBookmark(me.lastBk);
  7780. delete me.lastBk
  7781. } catch (e) {
  7782. range.setStartAtFirst(me.body).collapse(true)
  7783. }
  7784. range.select(true);
  7785. if (me.bkqueryCommandState) {
  7786. me.queryCommandState = me.bkqueryCommandState;
  7787. delete me.bkqueryCommandState;
  7788. }
  7789. if (me.bkqueryCommandValue) {
  7790. me.queryCommandValue = me.bkqueryCommandValue;
  7791. delete me.bkqueryCommandValue;
  7792. }
  7793. me.fireEvent('selectionchange');
  7794. }
  7795. },
  7796. enable: function () {
  7797. return this.setEnabled();
  7798. },
  7799. /** 设置当前编辑区域不可编辑
  7800. * @method setDisabled
  7801. */
  7802. /** 设置当前编辑区域不可编辑,except中的命令除外
  7803. * @method setDisabled
  7804. * @param { String } except 例外命令的字符串
  7805. * @remind 即使设置了disable,此处配置的例外命令仍然可以执行
  7806. * @example
  7807. * ```javascript
  7808. * editor.setDisabled('bold'); //禁用工具栏中除加粗之外的所有功能
  7809. * ```
  7810. */
  7811. /** 设置当前编辑区域不可编辑,except中的命令除外
  7812. * @method setDisabled
  7813. * @param { Array } except 例外命令的字符串数组,数组中的命令仍然可以执行
  7814. * @remind 即使设置了disable,此处配置的例外命令仍然可以执行
  7815. * @example
  7816. * ```javascript
  7817. * editor.setDisabled(['bold','insertimage']); //禁用工具栏中除加粗和插入图片之外的所有功能
  7818. * ```
  7819. */
  7820. setDisabled: function (except) {
  7821. var me = this;
  7822. except = except ? utils.isArray(except) ? except : [except] : [];
  7823. if (me.body.contentEditable == 'true') {
  7824. if (!me.lastBk) {
  7825. me.lastBk = me.selection.getRange().createBookmark(true);
  7826. }
  7827. me.body.contentEditable = false;
  7828. me.bkqueryCommandState = me.queryCommandState;
  7829. me.bkqueryCommandValue = me.queryCommandValue;
  7830. me.queryCommandState = function (type) {
  7831. if (utils.indexOf(except, type) != -1) {
  7832. return me.bkqueryCommandState.apply(me, arguments);
  7833. }
  7834. return -1;
  7835. };
  7836. me.queryCommandValue = function (type) {
  7837. if (utils.indexOf(except, type) != -1) {
  7838. return me.bkqueryCommandValue.apply(me, arguments);
  7839. }
  7840. return null;
  7841. };
  7842. me.fireEvent('selectionchange');
  7843. }
  7844. },
  7845. disable: function (except) {
  7846. return this.setDisabled(except);
  7847. },
  7848. /**
  7849. * 设置默认内容
  7850. * @method _setDefaultContent
  7851. * @private
  7852. * @param { String } cont 要存入的内容
  7853. */
  7854. _setDefaultContent: function () {
  7855. function clear() {
  7856. var me = this;
  7857. if (me.document.getElementById('initContent')) {
  7858. me.body.innerHTML = '<p>' + (ie ? '' : '<br/>') + '</p>';
  7859. me.removeListener('firstBeforeExecCommand focus', clear);
  7860. setTimeout(function () {
  7861. me.focus();
  7862. me._selectionChange();
  7863. }, 0)
  7864. }
  7865. }
  7866. return function (cont) {
  7867. var me = this;
  7868. me.body.innerHTML = '<p id="initContent">' + cont + '</p>';
  7869. me.addListener('firstBeforeExecCommand focus', clear);
  7870. }
  7871. }(),
  7872. /**
  7873. * 显示编辑器
  7874. * @method setShow
  7875. * @example
  7876. * ```javascript
  7877. * editor.setShow()
  7878. * ```
  7879. */
  7880. setShow: function () {
  7881. var me = this,
  7882. range = me.selection.getRange();
  7883. if (me.container.style.display == 'none') {
  7884. //有可能内容丢失了
  7885. try {
  7886. range.moveToBookmark(me.lastBk);
  7887. delete me.lastBk
  7888. } catch (e) {
  7889. range.setStartAtFirst(me.body).collapse(true)
  7890. }
  7891. //ie下focus实效,所以做了个延迟
  7892. setTimeout(function () {
  7893. range.select(true);
  7894. }, 100);
  7895. me.container.style.display = '';
  7896. }
  7897. },
  7898. show: function () {
  7899. return this.setShow();
  7900. },
  7901. /**
  7902. * 隐藏编辑器
  7903. * @method setHide
  7904. * @example
  7905. * ```javascript
  7906. * editor.setHide()
  7907. * ```
  7908. */
  7909. setHide: function () {
  7910. var me = this;
  7911. if (!me.lastBk) {
  7912. me.lastBk = me.selection.getRange().createBookmark(true);
  7913. }
  7914. me.container.style.display = 'none'
  7915. },
  7916. hide: function () {
  7917. return this.setHide();
  7918. },
  7919. /**
  7920. * 根据指定的路径,获取对应的语言资源
  7921. * @method getLang
  7922. * @param { String } path 路径根据的是lang目录下的语言文件的路径结构
  7923. * @return { Object | String } 根据路径返回语言资源的Json格式对象或者语言字符串
  7924. * @example
  7925. * ```javascript
  7926. * editor.getLang('contextMenu.delete'); //如果当前是中文,那返回是的是'删除'
  7927. * ```
  7928. */
  7929. getLang: function (path) {
  7930. // HaoChuan9421
  7931. if (!this.options) {
  7932. return '';
  7933. }
  7934. var lang = UE.I18N[this.options.lang] || 'zh-cn';
  7935. if (!lang) {
  7936. throw Error("not import language file");
  7937. }
  7938. path = (path || "").split(".");
  7939. for (var i = 0, ci; ci = path[i++];) {
  7940. lang = lang[ci];
  7941. if (!lang) break;
  7942. }
  7943. return lang;
  7944. },
  7945. /**
  7946. * 计算编辑器html内容字符串的长度
  7947. * @method getContentLength
  7948. * @return { Number } 返回计算的长度
  7949. * @example
  7950. * ```javascript
  7951. * //编辑器html内容<p><strong>132</strong></p>
  7952. * editor.getContentLength() //返回27
  7953. * ```
  7954. */
  7955. /**
  7956. * 计算编辑器当前纯文本内容的长度
  7957. * @method getContentLength
  7958. * @param { Boolean } ingoneHtml 传入true时,只按照纯文本来计算
  7959. * @return { Number } 返回计算的长度,内容中有hr/img/iframe标签,长度加1
  7960. * @example
  7961. * ```javascript
  7962. * //编辑器html内容<p><strong>132</strong></p>
  7963. * editor.getContentLength() //返回3
  7964. * ```
  7965. */
  7966. getContentLength: function (ingoneHtml, tagNames) {
  7967. var count = this.getContent(false, false, true).length;
  7968. if (ingoneHtml) {
  7969. tagNames = (tagNames || []).concat(['hr', 'img', 'iframe']);
  7970. count = this.getContentTxt().replace(/[\t\r\n]+/g, '').length;
  7971. for (var i = 0, ci; ci = tagNames[i++];) {
  7972. count += this.document.getElementsByTagName(ci).length;
  7973. }
  7974. }
  7975. return count;
  7976. },
  7977. /**
  7978. * 注册输入过滤规则
  7979. * @method addInputRule
  7980. * @param { Function } rule 要添加的过滤规则
  7981. * @example
  7982. * ```javascript
  7983. * editor.addInputRule(function(root){
  7984. * $.each(root.getNodesByTagName('div'),function(i,node){
  7985. * node.tagName="p";
  7986. * });
  7987. * });
  7988. * ```
  7989. */
  7990. addInputRule: function (rule) {
  7991. this.inputRules.push(rule);
  7992. },
  7993. /**
  7994. * 执行注册的过滤规则
  7995. * @method filterInputRule
  7996. * @param { UE.uNode } root 要过滤的uNode节点
  7997. * @remind 执行editor.setContent方法和执行'inserthtml'命令后,会运行该过滤函数
  7998. * @example
  7999. * ```javascript
  8000. * editor.filterInputRule(editor.body);
  8001. * ```
  8002. * @see UE.Editor:addInputRule
  8003. */
  8004. filterInputRule: function (root) {
  8005. for (var i = 0, ci; ci = this.inputRules[i++];) {
  8006. ci.call(this, root)
  8007. }
  8008. },
  8009. /**
  8010. * 注册输出过滤规则
  8011. * @method addOutputRule
  8012. * @param { Function } rule 要添加的过滤规则
  8013. * @example
  8014. * ```javascript
  8015. * editor.addOutputRule(function(root){
  8016. * $.each(root.getNodesByTagName('p'),function(i,node){
  8017. * node.tagName="div";
  8018. * });
  8019. * });
  8020. * ```
  8021. */
  8022. addOutputRule: function (rule) {
  8023. this.outputRules.push(rule)
  8024. },
  8025. /**
  8026. * 根据输出过滤规则,过滤编辑器内容
  8027. * @method filterOutputRule
  8028. * @remind 执行editor.getContent方法的时候,会先运行该过滤函数
  8029. * @param { UE.uNode } root 要过滤的uNode节点
  8030. * @example
  8031. * ```javascript
  8032. * editor.filterOutputRule(editor.body);
  8033. * ```
  8034. * @see UE.Editor:addOutputRule
  8035. */
  8036. filterOutputRule: function (root) {
  8037. for (var i = 0, ci; ci = this.outputRules[i++];) {
  8038. ci.call(this, root)
  8039. }
  8040. },
  8041. /**
  8042. * 根据action名称获取请求的路径
  8043. * @method getActionUrl
  8044. * @remind 假如没有设置serverUrl,会根据imageUrl设置默认的controller路径
  8045. * @param { String } action action名称
  8046. * @example
  8047. * ```javascript
  8048. * editor.getActionUrl('config'); //返回 "/ueditor/php/controller.php?action=config"
  8049. * editor.getActionUrl('image'); //返回 "/ueditor/php/controller.php?action=uplaodimage"
  8050. * editor.getActionUrl('scrawl'); //返回 "/ueditor/php/controller.php?action=uplaodscrawl"
  8051. * editor.getActionUrl('imageManager'); //返回 "/ueditor/php/controller.php?action=listimage"
  8052. * ```
  8053. */
  8054. getActionUrl: function (action) {
  8055. var actionName = this.getOpt(action) || action,
  8056. imageUrl = this.getOpt('imageUrl'),
  8057. serverUrl = this.getOpt('serverUrl');
  8058. console.log(serverUrl);
  8059. if (!serverUrl && imageUrl) {
  8060. serverUrl = imageUrl.replace(/^(.*[\/]).+([\.].+)$/, '$1controller$2');
  8061. }
  8062. if (serverUrl) {
  8063. serverUrl = serverUrl + (serverUrl.indexOf('?') == -1 ? '?' : '&') + 'action=' + (actionName || '');
  8064. return utils.formatUrl(serverUrl);
  8065. } else {
  8066. return '';
  8067. }
  8068. }
  8069. };
  8070. utils.inherits(Editor, EventBase);
  8071. })();
  8072. // core/Editor.defaultoptions.js
  8073. //维护编辑器一下默认的不在插件中的配置项
  8074. UE.Editor.defaultOptions = function (editor) {
  8075. var _url = editor.options.UEDITOR_HOME_URL;
  8076. return {
  8077. isShow: true,
  8078. initialContent: '',
  8079. initialStyle: '',
  8080. autoClearinitialContent: false,
  8081. iframeCssUrl: _url + 'themes/iframe.css',
  8082. textarea: 'editorValue',
  8083. focus: false,
  8084. focusInEnd: true,
  8085. autoClearEmptyNode: true,
  8086. fullscreen: false,
  8087. readonly: false,
  8088. zIndex: 999,
  8089. imagePopup: true,
  8090. enterTag: 'p',
  8091. customDomain: false,
  8092. lang: 'zh-cn',
  8093. langPath: _url + 'lang/',
  8094. theme: 'default',
  8095. themePath: _url + 'themes/',
  8096. allHtmlEnabled: false,
  8097. scaleEnabled: false,
  8098. tableNativeEditInFF: false,
  8099. autoSyncData: true,
  8100. fileNameFormat: '{time}{rand:6}'
  8101. }
  8102. };
  8103. // core/loadconfig.js
  8104. (function () {
  8105. UE.Editor.prototype.loadServerConfig = function () {
  8106. var me = this;
  8107. setTimeout(function () {
  8108. try {
  8109. me.options.imageUrl && me.setOpt('serverUrl', me.options.imageUrl.replace(/^(.*[\/]).+([\.].+)$/, '$1controller$2'));
  8110. var configUrl = me.getActionUrl('config'),
  8111. isJsonp = utils.isCrossDomainUrl(configUrl);
  8112. /* 发出ajax请求 */
  8113. me._serverConfigLoaded = false;
  8114. configUrl && UE.ajax.request(configUrl, {
  8115. 'method': 'GET',
  8116. 'dataType': isJsonp ? 'jsonp' : '',
  8117. 'onsuccess': function (r) {
  8118. try {
  8119. var config = isJsonp ? r : eval("(" + r.responseText + ")");
  8120. utils.extend(me.options, config);
  8121. me.fireEvent('serverConfigLoaded');
  8122. me._serverConfigLoaded = true;
  8123. } catch (e) {
  8124. showErrorMsg(me.getLang('loadconfigFormatError'));
  8125. }
  8126. },
  8127. 'onerror': function () {
  8128. showErrorMsg(me.getLang('loadconfigHttpError'));
  8129. }
  8130. });
  8131. } catch (e) {
  8132. showErrorMsg(me.getLang('loadconfigError'));
  8133. }
  8134. });
  8135. function showErrorMsg(msg) {
  8136. console && console.error(msg);
  8137. //me.fireEvent('showMessage', {
  8138. // 'title': msg,
  8139. // 'type': 'error'
  8140. //});
  8141. }
  8142. };
  8143. UE.Editor.prototype.isServerConfigLoaded = function () {
  8144. var me = this;
  8145. return me._serverConfigLoaded || false;
  8146. };
  8147. UE.Editor.prototype.afterConfigReady = function (handler) {
  8148. if (!handler || !utils.isFunction(handler)) return;
  8149. var me = this;
  8150. var readyHandler = function () {
  8151. handler.apply(me, arguments);
  8152. me.removeListener('serverConfigLoaded', readyHandler);
  8153. };
  8154. if (me.isServerConfigLoaded()) {
  8155. handler.call(me, 'serverConfigLoaded');
  8156. } else {
  8157. me.addListener('serverConfigLoaded', readyHandler);
  8158. }
  8159. };
  8160. })();
  8161. // core/ajax.js
  8162. /**
  8163. * @file
  8164. * @module UE.ajax
  8165. * @since 1.2.6.1
  8166. */
  8167. /**
  8168. * 提供对ajax请求的支持
  8169. * @module UE.ajax
  8170. */
  8171. UE.ajax = function () {
  8172. //创建一个ajaxRequest对象
  8173. var fnStr = 'XMLHttpRequest()';
  8174. try {
  8175. new ActiveXObject("Msxml2.XMLHTTP");
  8176. fnStr = 'ActiveXObject(\'Msxml2.XMLHTTP\')';
  8177. } catch (e) {
  8178. try {
  8179. new ActiveXObject("Microsoft.XMLHTTP");
  8180. fnStr = 'ActiveXObject(\'Microsoft.XMLHTTP\')'
  8181. } catch (e) {}
  8182. }
  8183. var creatAjaxRequest = new Function('return new ' + fnStr);
  8184. /**
  8185. * 将json参数转化成适合ajax提交的参数列表
  8186. * @param json
  8187. */
  8188. function json2str(json) {
  8189. var strArr = [];
  8190. for (var i in json) {
  8191. //忽略默认的几个参数
  8192. if (i == "method" || i == "timeout" || i == "async" || i == "dataType" || i == "callback") continue;
  8193. //忽略控制
  8194. if (json[i] == undefined || json[i] == null) continue;
  8195. //传递过来的对象和函数不在提交之列
  8196. if (!((typeof json[i]).toLowerCase() == "function" || (typeof json[i]).toLowerCase() == "object")) {
  8197. strArr.push(encodeURIComponent(i) + "=" + encodeURIComponent(json[i]));
  8198. } else if (utils.isArray(json[i])) {
  8199. //支持传数组内容
  8200. for (var j = 0; j < json[i].length; j++) {
  8201. strArr.push(encodeURIComponent(i) + "[]=" + encodeURIComponent(json[i][j]));
  8202. }
  8203. }
  8204. }
  8205. return strArr.join("&");
  8206. }
  8207. function doAjax(url, ajaxOptions) {
  8208. var xhr = creatAjaxRequest(),
  8209. //是否超时
  8210. timeIsOut = false,
  8211. //默认参数
  8212. defaultAjaxOptions = {
  8213. method: "POST",
  8214. timeout: 5000,
  8215. async: true,
  8216. data: {}, //需要传递对象的话只能覆盖
  8217. onsuccess: function () {},
  8218. onerror: function () {}
  8219. };
  8220. if (typeof url === "object") {
  8221. ajaxOptions = url;
  8222. url = ajaxOptions.url;
  8223. }
  8224. if (!xhr || !url) return;
  8225. var ajaxOpts = ajaxOptions ? utils.extend(defaultAjaxOptions, ajaxOptions) : defaultAjaxOptions;
  8226. var submitStr = json2str(ajaxOpts); // { name:"Jim",city:"Beijing" } --> "name=Jim&city=Beijing"
  8227. //如果用户直接通过data参数传递json对象过来,则也要将此json对象转化为字符串
  8228. if (!utils.isEmptyObject(ajaxOpts.data)) {
  8229. submitStr += (submitStr ? "&" : "") + json2str(ajaxOpts.data);
  8230. }
  8231. //超时检测
  8232. var timerID = setTimeout(function () {
  8233. if (xhr.readyState != 4) {
  8234. timeIsOut = true;
  8235. xhr.abort();
  8236. clearTimeout(timerID);
  8237. }
  8238. }, ajaxOpts.timeout);
  8239. var method = ajaxOpts.method.toUpperCase();
  8240. var str = url + (url.indexOf("?") == -1 ? "?" : "&") + (method == "POST" ? "" : submitStr + "&noCache=" + +new Date);
  8241. xhr.open(method, str, ajaxOpts.async);
  8242. xhr.onreadystatechange = function () {
  8243. if (xhr.readyState == 4) {
  8244. if (!timeIsOut && xhr.status == 200) {
  8245. ajaxOpts.onsuccess(xhr);
  8246. } else {
  8247. ajaxOpts.onerror(xhr);
  8248. }
  8249. }
  8250. };
  8251. if (method == "POST") {
  8252. xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  8253. xhr.send(submitStr);
  8254. } else {
  8255. xhr.send(null);
  8256. }
  8257. }
  8258. function doJsonp(url, opts) {
  8259. var successhandler = opts.onsuccess || function () {},
  8260. scr = document.createElement('SCRIPT'),
  8261. options = opts || {},
  8262. charset = options['charset'],
  8263. callbackField = options['jsonp'] || 'callback',
  8264. callbackFnName,
  8265. timeOut = options['timeOut'] || 0,
  8266. timer,
  8267. reg = new RegExp('(\\?|&)' + callbackField + '=([^&]*)'),
  8268. matches;
  8269. if (utils.isFunction(successhandler)) {
  8270. callbackFnName = 'bd__editor__' + Math.floor(Math.random() * 2147483648).toString(36);
  8271. window[callbackFnName] = getCallBack(0);
  8272. } else if (utils.isString(successhandler)) {
  8273. callbackFnName = successhandler;
  8274. } else {
  8275. if (matches = reg.exec(url)) {
  8276. callbackFnName = matches[2];
  8277. }
  8278. }
  8279. url = url.replace(reg, '\x241' + callbackField + '=' + callbackFnName);
  8280. if (url.search(reg) < 0) {
  8281. url += (url.indexOf('?') < 0 ? '?' : '&') + callbackField + '=' + callbackFnName;
  8282. }
  8283. var queryStr = json2str(opts); // { name:"Jim",city:"Beijing" } --> "name=Jim&city=Beijing"
  8284. //如果用户直接通过data参数传递json对象过来,则也要将此json对象转化为字符串
  8285. if (!utils.isEmptyObject(opts.data)) {
  8286. queryStr += (queryStr ? "&" : "") + json2str(opts.data);
  8287. }
  8288. if (queryStr) {
  8289. url = url.replace(/\?/, '?' + queryStr + '&');
  8290. }
  8291. scr.onerror = getCallBack(1);
  8292. if (timeOut) {
  8293. timer = setTimeout(getCallBack(1), timeOut);
  8294. }
  8295. createScriptTag(scr, url, charset);
  8296. function createScriptTag(scr, url, charset) {
  8297. scr.setAttribute('type', 'text/javascript');
  8298. scr.setAttribute('defer', 'defer');
  8299. charset && scr.setAttribute('charset', charset);
  8300. scr.setAttribute('src', url);
  8301. document.getElementsByTagName('head')[0].appendChild(scr);
  8302. }
  8303. function getCallBack(onTimeOut) {
  8304. return function () {
  8305. try {
  8306. if (onTimeOut) {
  8307. options.onerror && options.onerror();
  8308. } else {
  8309. try {
  8310. clearTimeout(timer);
  8311. successhandler.apply(window, arguments);
  8312. } catch (e) {}
  8313. }
  8314. } catch (exception) {
  8315. options.onerror && options.onerror.call(window, exception);
  8316. } finally {
  8317. options.oncomplete && options.oncomplete.apply(window, arguments);
  8318. scr.parentNode && scr.parentNode.removeChild(scr);
  8319. window[callbackFnName] = null;
  8320. try {
  8321. delete window[callbackFnName];
  8322. } catch (e) {}
  8323. }
  8324. }
  8325. }
  8326. }
  8327. return {
  8328. /**
  8329. * 根据给定的参数项,向指定的url发起一个ajax请求。 ajax请求完成后,会根据请求结果调用相应回调: 如果请求
  8330. * 成功, 则调用onsuccess回调, 失败则调用 onerror 回调
  8331. * @method request
  8332. * @param { URLString } url ajax请求的url地址
  8333. * @param { Object } ajaxOptions ajax请求选项的键值对,支持的选项如下:
  8334. * @example
  8335. * ```javascript
  8336. * //向sayhello.php发起一个异步的Ajax GET请求, 请求超时时间为10s, 请求完成后执行相应的回调。
  8337. * UE.ajax.requeset( 'sayhello.php', {
  8338. *
  8339. * //请求方法。可选值: 'GET', 'POST',默认值是'POST'
  8340. * method: 'GET',
  8341. *
  8342. * //超时时间。 默认为5000, 单位是ms
  8343. * timeout: 10000,
  8344. *
  8345. * //是否是异步请求。 true为异步请求, false为同步请求
  8346. * async: true,
  8347. *
  8348. * //请求携带的数据。如果请求为GET请求, data会经过stringify后附加到请求url之后。
  8349. * data: {
  8350. * name: 'ueditor'
  8351. * },
  8352. *
  8353. * //请求成功后的回调, 该回调接受当前的XMLHttpRequest对象作为参数。
  8354. * onsuccess: function ( xhr ) {
  8355. * console.log( xhr.responseText );
  8356. * },
  8357. *
  8358. * //请求失败或者超时后的回调。
  8359. * onerror: function ( xhr ) {
  8360. * alert( 'Ajax请求失败' );
  8361. * }
  8362. *
  8363. * } );
  8364. * ```
  8365. */
  8366. /**
  8367. * 根据给定的参数项发起一个ajax请求, 参数项里必须包含一个url地址。 ajax请求完成后,会根据请求结果调用相应回调: 如果请求
  8368. * 成功, 则调用onsuccess回调, 失败则调用 onerror 回调。
  8369. * @method request
  8370. * @warning 如果在参数项里未提供一个key为“url”的地址值,则该请求将直接退出。
  8371. * @param { Object } ajaxOptions ajax请求选项的键值对,支持的选项如下:
  8372. * @example
  8373. * ```javascript
  8374. *
  8375. * //向sayhello.php发起一个异步的Ajax POST请求, 请求超时时间为5s, 请求完成后不执行任何回调。
  8376. * UE.ajax.requeset( 'sayhello.php', {
  8377. *
  8378. * //请求的地址, 该项是必须的。
  8379. * url: 'sayhello.php'
  8380. *
  8381. * } );
  8382. * ```
  8383. */
  8384. request: function (url, opts) {
  8385. if (opts && opts.dataType == 'jsonp') {
  8386. doJsonp(url, opts);
  8387. } else {
  8388. doAjax(url, opts);
  8389. }
  8390. },
  8391. getJSONP: function (url, data, fn) {
  8392. var opts = {
  8393. 'data': data,
  8394. 'oncomplete': fn
  8395. };
  8396. doJsonp(url, opts);
  8397. }
  8398. };
  8399. }();
  8400. // core/filterword.js
  8401. /**
  8402. * UE过滤word的静态方法
  8403. * @file
  8404. */
  8405. /**
  8406. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  8407. * @module UE
  8408. */
  8409. /**
  8410. * 根据传入html字符串过滤word
  8411. * @module UE
  8412. * @since 1.2.6.1
  8413. * @method filterWord
  8414. * @param { String } html html字符串
  8415. * @return { String } 已过滤后的结果字符串
  8416. * @example
  8417. * ```javascript
  8418. * UE.filterWord(html);
  8419. * ```
  8420. */
  8421. var filterWord = UE.filterWord = function () {
  8422. //是否是word过来的内容
  8423. function isWordDocument(str) {
  8424. return /(class="?Mso|style="[^"]*\bmso\-|w:WordDocument|<(v|o):|lang=)/ig.test(str);
  8425. }
  8426. //去掉小数
  8427. function transUnit(v) {
  8428. v = v.replace(/[\d.]+\w+/g, function (m) {
  8429. return utils.transUnitToPx(m);
  8430. });
  8431. return v;
  8432. }
  8433. function filterPasteWord(str) {
  8434. return str.replace(/[\t\r\n]+/g, ' ')
  8435. .replace(/<!--[\s\S]*?-->/ig, "")
  8436. //转换图片
  8437. .replace(/<v:shape [^>]*>[\s\S]*?.<\/v:shape>/gi, function (str) {
  8438. //opera能自己解析出image所这里直接返回空
  8439. if (browser.opera) {
  8440. return '';
  8441. }
  8442. try {
  8443. //有可能是bitmap占为图,无用,直接过滤掉,主要体现在粘贴excel表格中
  8444. if (/Bitmap/i.test(str)) {
  8445. return '';
  8446. }
  8447. var width = str.match(/width:([ \d.]*p[tx])/i)[1],
  8448. height = str.match(/height:([ \d.]*p[tx])/i)[1],
  8449. src = str.match(/src=\s*"([^"]*)"/i)[1];
  8450. return '<img width="' + transUnit(width) + '" height="' + transUnit(height) + '" src="' + src + '" />';
  8451. } catch (e) {
  8452. return '';
  8453. }
  8454. })
  8455. //针对wps添加的多余标签处理
  8456. .replace(/<\/?div[^>]*>/g, '')
  8457. //去掉多余的属性
  8458. .replace(/v:\w+=(["']?)[^'"]+\1/g, '')
  8459. .replace(/<(!|script[^>]*>.*?<\/script(?=[>\s])|\/?(\?xml(:\w+)?|xml|meta|link|style|\w+:\w+)(?=[\s\/>]))[^>]*>/gi, "")
  8460. .replace(/<p [^>]*class="?MsoHeading"?[^>]*>(.*?)<\/p>/gi, "<p><strong>$1</strong></p>")
  8461. //去掉多余的属性
  8462. .replace(/\s+(class|lang|align)\s*=\s*(['"]?)([\w-]+)\2/ig, function (str, name, marks, val) {
  8463. //保留list的标示
  8464. return name == 'class' && val == 'MsoListParagraph' ? str : ''
  8465. })
  8466. //清除多余的font/span不能匹配&nbsp;有可能是空格
  8467. .replace(/<(font|span)[^>]*>(\s*)<\/\1>/gi, function (a, b, c) {
  8468. return c.replace(/[\t\r\n ]+/g, ' ')
  8469. })
  8470. //处理style的问题
  8471. .replace(/(<[a-z][^>]*)\sstyle=(["'])([^\2]*?)\2/gi, function (str, tag, tmp, style) {
  8472. var n = [],
  8473. s = style.replace(/^\s+|\s+$/, '')
  8474. .replace(/&#39;/g, '\'')
  8475. .replace(/&quot;/gi, "'")
  8476. .replace(/[\d.]+(cm|pt)/g, function (str) {
  8477. return utils.transUnitToPx(str)
  8478. })
  8479. .split(/;\s*/g);
  8480. for (var i = 0, v; v = s[i]; i++) {
  8481. var name, value,
  8482. parts = v.split(":");
  8483. if (parts.length == 2) {
  8484. name = parts[0].toLowerCase();
  8485. value = parts[1].toLowerCase();
  8486. if (/^(background)\w*/.test(name) && value.replace(/(initial|\s)/g, '').length == 0 ||
  8487. /^(margin)\w*/.test(name) && /^0\w+$/.test(value)
  8488. ) {
  8489. continue;
  8490. }
  8491. switch (name) {
  8492. case "mso-padding-alt":
  8493. case "mso-padding-top-alt":
  8494. case "mso-padding-right-alt":
  8495. case "mso-padding-bottom-alt":
  8496. case "mso-padding-left-alt":
  8497. case "mso-margin-alt":
  8498. case "mso-margin-top-alt":
  8499. case "mso-margin-right-alt":
  8500. case "mso-margin-bottom-alt":
  8501. case "mso-margin-left-alt":
  8502. //ie下会出现挤到一起的情况
  8503. //case "mso-table-layout-alt":
  8504. case "mso-height":
  8505. case "mso-width":
  8506. case "mso-vertical-align-alt":
  8507. //trace:1819 ff下会解析出padding在table上
  8508. if (!/<table/.test(tag))
  8509. n[i] = name.replace(/^mso-|-alt$/g, "") + ":" + transUnit(value);
  8510. continue;
  8511. case "horiz-align":
  8512. n[i] = "text-align:" + value;
  8513. continue;
  8514. case "vert-align":
  8515. n[i] = "vertical-align:" + value;
  8516. continue;
  8517. case "font-color":
  8518. case "mso-foreground":
  8519. n[i] = "color:" + value;
  8520. continue;
  8521. case "mso-background":
  8522. case "mso-highlight":
  8523. n[i] = "background:" + value;
  8524. continue;
  8525. case "mso-default-height":
  8526. n[i] = "min-height:" + transUnit(value);
  8527. continue;
  8528. case "mso-default-width":
  8529. n[i] = "min-width:" + transUnit(value);
  8530. continue;
  8531. case "mso-padding-between-alt":
  8532. n[i] = "border-collapse:separate;border-spacing:" + transUnit(value);
  8533. continue;
  8534. case "text-line-through":
  8535. if ((value == "single") || (value == "double")) {
  8536. n[i] = "text-decoration:line-through";
  8537. }
  8538. continue;
  8539. case "mso-zero-height":
  8540. if (value == "yes") {
  8541. n[i] = "display:none";
  8542. }
  8543. continue;
  8544. // case 'background':
  8545. // break;
  8546. case 'margin':
  8547. if (!/[1-9]/.test(value)) {
  8548. continue;
  8549. }
  8550. }
  8551. 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) ||
  8552. /text\-indent|padding|margin/.test(name) && /\-[\d.]+/.test(value)
  8553. ) {
  8554. continue;
  8555. }
  8556. n[i] = name + ":" + parts[1];
  8557. }
  8558. }
  8559. return tag + (n.length ? ' style="' + n.join(';').replace(/;{2,}/g, ';') + '"' : '');
  8560. })
  8561. }
  8562. return function (html) {
  8563. return (isWordDocument(html) ? filterPasteWord(html) : html);
  8564. };
  8565. }();
  8566. // core/node.js
  8567. /**
  8568. * 编辑器模拟的节点类
  8569. * @file
  8570. * @module UE
  8571. * @class uNode
  8572. * @since 1.2.6.1
  8573. */
  8574. /**
  8575. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  8576. * @unfile
  8577. * @module UE
  8578. */
  8579. (function () {
  8580. /**
  8581. * 编辑器模拟的节点类
  8582. * @unfile
  8583. * @module UE
  8584. * @class uNode
  8585. */
  8586. /**
  8587. * 通过一个键值对,创建一个uNode对象
  8588. * @constructor
  8589. * @param { Object } attr 传入要创建的uNode的初始属性
  8590. * @example
  8591. * ```javascript
  8592. * var node = new uNode({
  8593. * type:'element',
  8594. * tagName:'span',
  8595. * attrs:{style:'font-size:14px;'}
  8596. * }
  8597. * ```
  8598. */
  8599. var uNode = UE.uNode = function (obj) {
  8600. this.type = obj.type;
  8601. this.data = obj.data;
  8602. this.tagName = obj.tagName;
  8603. this.parentNode = obj.parentNode;
  8604. this.attrs = obj.attrs || {};
  8605. this.children = obj.children;
  8606. };
  8607. var notTransAttrs = {
  8608. 'href': 1,
  8609. 'src': 1,
  8610. '_src': 1,
  8611. '_href': 1,
  8612. 'cdata_data': 1
  8613. };
  8614. var notTransTagName = {
  8615. style: 1,
  8616. script: 1
  8617. };
  8618. var indentChar = ' ',
  8619. breakChar = '\n';
  8620. function insertLine(arr, current, begin) {
  8621. arr.push(breakChar);
  8622. return current + (begin ? 1 : -1);
  8623. }
  8624. function insertIndent(arr, current) {
  8625. //插入缩进
  8626. for (var i = 0; i < current; i++) {
  8627. arr.push(indentChar);
  8628. }
  8629. }
  8630. //创建uNode的静态方法
  8631. //支持标签和html
  8632. uNode.createElement = function (html) {
  8633. if (/[<>]/.test(html)) {
  8634. return UE.htmlparser(html).children[0]
  8635. } else {
  8636. return new uNode({
  8637. type: 'element',
  8638. children: [],
  8639. tagName: html
  8640. })
  8641. }
  8642. };
  8643. uNode.createText = function (data, noTrans) {
  8644. return new UE.uNode({
  8645. type: 'text',
  8646. 'data': noTrans ? data : utils.unhtml(data || '')
  8647. })
  8648. };
  8649. function nodeToHtml(node, arr, formatter, current) {
  8650. switch (node.type) {
  8651. case 'root':
  8652. for (var i = 0, ci; ci = node.children[i++];) {
  8653. //插入新行
  8654. if (formatter && ci.type == 'element' && !dtd.$inlineWithA[ci.tagName] && i > 1) {
  8655. insertLine(arr, current, true);
  8656. insertIndent(arr, current)
  8657. }
  8658. nodeToHtml(ci, arr, formatter, current)
  8659. }
  8660. break;
  8661. case 'text':
  8662. isText(node, arr);
  8663. break;
  8664. case 'element':
  8665. isElement(node, arr, formatter, current);
  8666. break;
  8667. case 'comment':
  8668. isComment(node, arr, formatter);
  8669. }
  8670. return arr;
  8671. }
  8672. function isText(node, arr) {
  8673. if (node.parentNode.tagName == 'pre') {
  8674. //源码模式下输入html标签,不能做转换处理,直接输出
  8675. arr.push(node.data)
  8676. } else {
  8677. arr.push(notTransTagName[node.parentNode.tagName] ? utils.html(node.data) : node.data.replace(/[ ]{2}/g, ' &nbsp;'))
  8678. }
  8679. }
  8680. function isElement(node, arr, formatter, current) {
  8681. var attrhtml = '';
  8682. if (node.attrs) {
  8683. attrhtml = [];
  8684. var attrs = node.attrs;
  8685. for (var a in attrs) {
  8686. //这里就针对
  8687. //<p>'<img src='http://nsclick.baidu.com/u.gif?&asdf=\"sdf&asdfasdfs;asdf'></p>
  8688. //这里边的\"做转换,要不用innerHTML直接被截断了,属性src
  8689. //有可能做的不够
  8690. attrhtml.push(a + (attrs[a] !== undefined ? '="' + (notTransAttrs[a] ? utils.html(attrs[a]).replace(/["]/g, function (a) {
  8691. return '&quot;'
  8692. }) : utils.unhtml(attrs[a])) + '"' : ''))
  8693. }
  8694. attrhtml = attrhtml.join(' ');
  8695. }
  8696. arr.push('<' + node.tagName +
  8697. (attrhtml ? ' ' + attrhtml : '') +
  8698. (dtd.$empty[node.tagName] ? '\/' : '') + '>'
  8699. );
  8700. //插入新行
  8701. if (formatter && !dtd.$inlineWithA[node.tagName] && node.tagName != 'pre') {
  8702. if (node.children && node.children.length) {
  8703. current = insertLine(arr, current, true);
  8704. insertIndent(arr, current)
  8705. }
  8706. }
  8707. if (node.children && node.children.length) {
  8708. for (var i = 0, ci; ci = node.children[i++];) {
  8709. if (formatter && ci.type == 'element' && !dtd.$inlineWithA[ci.tagName] && i > 1) {
  8710. insertLine(arr, current);
  8711. insertIndent(arr, current)
  8712. }
  8713. nodeToHtml(ci, arr, formatter, current)
  8714. }
  8715. }
  8716. if (!dtd.$empty[node.tagName]) {
  8717. if (formatter && !dtd.$inlineWithA[node.tagName] && node.tagName != 'pre') {
  8718. if (node.children && node.children.length) {
  8719. current = insertLine(arr, current);
  8720. insertIndent(arr, current)
  8721. }
  8722. }
  8723. arr.push('<\/' + node.tagName + '>');
  8724. }
  8725. }
  8726. function isComment(node, arr) {
  8727. arr.push('<!--' + node.data + '-->');
  8728. }
  8729. function getNodeById(root, id) {
  8730. var node;
  8731. if (root.type == 'element' && root.getAttr('id') == id) {
  8732. return root;
  8733. }
  8734. if (root.children && root.children.length) {
  8735. for (var i = 0, ci; ci = root.children[i++];) {
  8736. if (node = getNodeById(ci, id)) {
  8737. return node;
  8738. }
  8739. }
  8740. }
  8741. }
  8742. function getNodesByTagName(node, tagName, arr) {
  8743. if (node.type == 'element' && node.tagName == tagName) {
  8744. arr.push(node);
  8745. }
  8746. if (node.children && node.children.length) {
  8747. for (var i = 0, ci; ci = node.children[i++];) {
  8748. getNodesByTagName(ci, tagName, arr)
  8749. }
  8750. }
  8751. }
  8752. function nodeTraversal(root, fn) {
  8753. if (root.children && root.children.length) {
  8754. for (var i = 0, ci; ci = root.children[i];) {
  8755. nodeTraversal(ci, fn);
  8756. //ci被替换的情况,这里就不再走 fn了
  8757. if (ci.parentNode) {
  8758. if (ci.children && ci.children.length) {
  8759. fn(ci)
  8760. }
  8761. if (ci.parentNode) i++
  8762. }
  8763. }
  8764. } else {
  8765. fn(root)
  8766. }
  8767. }
  8768. uNode.prototype = {
  8769. /**
  8770. * 当前节点对象,转换成html文本
  8771. * @method toHtml
  8772. * @return { String } 返回转换后的html字符串
  8773. * @example
  8774. * ```javascript
  8775. * node.toHtml();
  8776. * ```
  8777. */
  8778. /**
  8779. * 当前节点对象,转换成html文本
  8780. * @method toHtml
  8781. * @param { Boolean } formatter 是否格式化返回值
  8782. * @return { String } 返回转换后的html字符串
  8783. * @example
  8784. * ```javascript
  8785. * node.toHtml( true );
  8786. * ```
  8787. */
  8788. toHtml: function (formatter) {
  8789. var arr = [];
  8790. nodeToHtml(this, arr, formatter, 0);
  8791. return arr.join('')
  8792. },
  8793. /**
  8794. * 获取节点的html内容
  8795. * @method innerHTML
  8796. * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
  8797. * @return { String } 返回节点的html内容
  8798. * @example
  8799. * ```javascript
  8800. * var htmlstr = node.innerHTML();
  8801. * ```
  8802. */
  8803. /**
  8804. * 设置节点的html内容
  8805. * @method innerHTML
  8806. * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
  8807. * @param { String } htmlstr 传入要设置的html内容
  8808. * @return { UE.uNode } 返回节点本身
  8809. * @example
  8810. * ```javascript
  8811. * node.innerHTML('<span>text</span>');
  8812. * ```
  8813. */
  8814. innerHTML: function (htmlstr) {
  8815. if (this.type != 'element' || dtd.$empty[this.tagName]) {
  8816. return this;
  8817. }
  8818. if (utils.isString(htmlstr)) {
  8819. if (this.children) {
  8820. for (var i = 0, ci; ci = this.children[i++];) {
  8821. ci.parentNode = null;
  8822. }
  8823. }
  8824. this.children = [];
  8825. var tmpRoot = UE.htmlparser(htmlstr);
  8826. for (var i = 0, ci; ci = tmpRoot.children[i++];) {
  8827. this.children.push(ci);
  8828. ci.parentNode = this;
  8829. }
  8830. return this;
  8831. } else {
  8832. var tmpRoot = new UE.uNode({
  8833. type: 'root',
  8834. children: this.children
  8835. });
  8836. return tmpRoot.toHtml();
  8837. }
  8838. },
  8839. /**
  8840. * 获取节点的纯文本内容
  8841. * @method innerText
  8842. * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
  8843. * @return { String } 返回节点的存文本内容
  8844. * @example
  8845. * ```javascript
  8846. * var textStr = node.innerText();
  8847. * ```
  8848. */
  8849. /**
  8850. * 设置节点的纯文本内容
  8851. * @method innerText
  8852. * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
  8853. * @param { String } textStr 传入要设置的文本内容
  8854. * @return { UE.uNode } 返回节点本身
  8855. * @example
  8856. * ```javascript
  8857. * node.innerText('<span>text</span>');
  8858. * ```
  8859. */
  8860. innerText: function (textStr, noTrans) {
  8861. if (this.type != 'element' || dtd.$empty[this.tagName]) {
  8862. return this;
  8863. }
  8864. if (textStr) {
  8865. if (this.children) {
  8866. for (var i = 0, ci; ci = this.children[i++];) {
  8867. ci.parentNode = null;
  8868. }
  8869. }
  8870. this.children = [];
  8871. this.appendChild(uNode.createText(textStr, noTrans));
  8872. return this;
  8873. } else {
  8874. return this.toHtml().replace(/<[^>]+>/g, '');
  8875. }
  8876. },
  8877. /**
  8878. * 获取当前对象的data属性
  8879. * @method getData
  8880. * @return { Object } 若节点的type值是elemenet,返回空字符串,否则返回节点的data属性
  8881. * @example
  8882. * ```javascript
  8883. * node.getData();
  8884. * ```
  8885. */
  8886. getData: function () {
  8887. if (this.type == 'element')
  8888. return '';
  8889. return this.data
  8890. },
  8891. /**
  8892. * 获取当前节点下的第一个子节点
  8893. * @method firstChild
  8894. * @return { UE.uNode } 返回第一个子节点
  8895. * @example
  8896. * ```javascript
  8897. * node.firstChild(); //返回第一个子节点
  8898. * ```
  8899. */
  8900. firstChild: function () {
  8901. // if (this.type != 'element' || dtd.$empty[this.tagName]) {
  8902. // return this;
  8903. // }
  8904. return this.children ? this.children[0] : null;
  8905. },
  8906. /**
  8907. * 获取当前节点下的最后一个子节点
  8908. * @method lastChild
  8909. * @return { UE.uNode } 返回最后一个子节点
  8910. * @example
  8911. * ```javascript
  8912. * node.lastChild(); //返回最后一个子节点
  8913. * ```
  8914. */
  8915. lastChild: function () {
  8916. // if (this.type != 'element' || dtd.$empty[this.tagName] ) {
  8917. // return this;
  8918. // }
  8919. return this.children ? this.children[this.children.length - 1] : null;
  8920. },
  8921. /**
  8922. * 获取和当前节点有相同父亲节点的前一个节点
  8923. * @method previousSibling
  8924. * @return { UE.uNode } 返回前一个节点
  8925. * @example
  8926. * ```javascript
  8927. * node.children[2].previousSibling(); //返回子节点node.children[1]
  8928. * ```
  8929. */
  8930. previousSibling: function () {
  8931. var parent = this.parentNode;
  8932. for (var i = 0, ci; ci = parent.children[i]; i++) {
  8933. if (ci === this) {
  8934. return i == 0 ? null : parent.children[i - 1];
  8935. }
  8936. }
  8937. },
  8938. /**
  8939. * 获取和当前节点有相同父亲节点的后一个节点
  8940. * @method nextSibling
  8941. * @return { UE.uNode } 返回后一个节点,找不到返回null
  8942. * @example
  8943. * ```javascript
  8944. * node.children[2].nextSibling(); //如果有,返回子节点node.children[3]
  8945. * ```
  8946. */
  8947. nextSibling: function () {
  8948. var parent = this.parentNode;
  8949. for (var i = 0, ci; ci = parent.children[i++];) {
  8950. if (ci === this) {
  8951. return parent.children[i];
  8952. }
  8953. }
  8954. },
  8955. /**
  8956. * 用新的节点替换当前节点
  8957. * @method replaceChild
  8958. * @param { UE.uNode } target 要替换成该节点参数
  8959. * @param { UE.uNode } source 要被替换掉的节点
  8960. * @return { UE.uNode } 返回替换之后的节点对象
  8961. * @example
  8962. * ```javascript
  8963. * node.replaceChild(newNode, childNode); //用newNode替换childNode,childNode是node的子节点
  8964. * ```
  8965. */
  8966. replaceChild: function (target, source) {
  8967. if (this.children) {
  8968. if (target.parentNode) {
  8969. target.parentNode.removeChild(target);
  8970. }
  8971. for (var i = 0, ci; ci = this.children[i]; i++) {
  8972. if (ci === source) {
  8973. this.children.splice(i, 1, target);
  8974. source.parentNode = null;
  8975. target.parentNode = this;
  8976. return target;
  8977. }
  8978. }
  8979. }
  8980. },
  8981. /**
  8982. * 在节点的子节点列表最后位置插入一个节点
  8983. * @method appendChild
  8984. * @param { UE.uNode } node 要插入的节点
  8985. * @return { UE.uNode } 返回刚插入的子节点
  8986. * @example
  8987. * ```javascript
  8988. * node.appendChild( newNode ); //在node内插入子节点newNode
  8989. * ```
  8990. */
  8991. appendChild: function (node) {
  8992. if (this.type == 'root' || (this.type == 'element' && !dtd.$empty[this.tagName])) {
  8993. if (!this.children) {
  8994. this.children = []
  8995. }
  8996. if (node.parentNode) {
  8997. node.parentNode.removeChild(node);
  8998. }
  8999. for (var i = 0, ci; ci = this.children[i]; i++) {
  9000. if (ci === node) {
  9001. this.children.splice(i, 1);
  9002. break;
  9003. }
  9004. }
  9005. this.children.push(node);
  9006. node.parentNode = this;
  9007. return node;
  9008. }
  9009. },
  9010. /**
  9011. * 在传入节点的前面插入一个节点
  9012. * @method insertBefore
  9013. * @param { UE.uNode } target 要插入的节点
  9014. * @param { UE.uNode } source 在该参数节点前面插入
  9015. * @return { UE.uNode } 返回刚插入的子节点
  9016. * @example
  9017. * ```javascript
  9018. * node.parentNode.insertBefore(newNode, node); //在node节点后面插入newNode
  9019. * ```
  9020. */
  9021. insertBefore: function (target, source) {
  9022. if (this.children) {
  9023. if (target.parentNode) {
  9024. target.parentNode.removeChild(target);
  9025. }
  9026. for (var i = 0, ci; ci = this.children[i]; i++) {
  9027. if (ci === source) {
  9028. this.children.splice(i, 0, target);
  9029. target.parentNode = this;
  9030. return target;
  9031. }
  9032. }
  9033. }
  9034. },
  9035. /**
  9036. * 在传入节点的后面插入一个节点
  9037. * @method insertAfter
  9038. * @param { UE.uNode } target 要插入的节点
  9039. * @param { UE.uNode } source 在该参数节点后面插入
  9040. * @return { UE.uNode } 返回刚插入的子节点
  9041. * @example
  9042. * ```javascript
  9043. * node.parentNode.insertAfter(newNode, node); //在node节点后面插入newNode
  9044. * ```
  9045. */
  9046. insertAfter: function (target, source) {
  9047. if (this.children) {
  9048. if (target.parentNode) {
  9049. target.parentNode.removeChild(target);
  9050. }
  9051. for (var i = 0, ci; ci = this.children[i]; i++) {
  9052. if (ci === source) {
  9053. this.children.splice(i + 1, 0, target);
  9054. target.parentNode = this;
  9055. return target;
  9056. }
  9057. }
  9058. }
  9059. },
  9060. /**
  9061. * 从当前节点的子节点列表中,移除节点
  9062. * @method removeChild
  9063. * @param { UE.uNode } node 要移除的节点引用
  9064. * @param { Boolean } keepChildren 是否保留移除节点的子节点,若传入true,自动把移除节点的子节点插入到移除的位置
  9065. * @return { * } 返回刚移除的子节点
  9066. * @example
  9067. * ```javascript
  9068. * node.removeChild(childNode,true); //在node的子节点列表中移除child节点,并且吧child的子节点插入到移除的位置
  9069. * ```
  9070. */
  9071. removeChild: function (node, keepChildren) {
  9072. if (this.children) {
  9073. for (var i = 0, ci; ci = this.children[i]; i++) {
  9074. if (ci === node) {
  9075. this.children.splice(i, 1);
  9076. ci.parentNode = null;
  9077. if (keepChildren && ci.children && ci.children.length) {
  9078. for (var j = 0, cj; cj = ci.children[j]; j++) {
  9079. this.children.splice(i + j, 0, cj);
  9080. cj.parentNode = this;
  9081. }
  9082. }
  9083. return ci;
  9084. }
  9085. }
  9086. }
  9087. },
  9088. /**
  9089. * 获取当前节点所代表的元素属性,即获取attrs对象下的属性值
  9090. * @method getAttr
  9091. * @param { String } attrName 要获取的属性名称
  9092. * @return { * } 返回attrs对象下的属性值
  9093. * @example
  9094. * ```javascript
  9095. * node.getAttr('title');
  9096. * ```
  9097. */
  9098. getAttr: function (attrName) {
  9099. return this.attrs && this.attrs[attrName.toLowerCase()]
  9100. },
  9101. /**
  9102. * 设置当前节点所代表的元素属性,即设置attrs对象下的属性值
  9103. * @method setAttr
  9104. * @param { String } attrName 要设置的属性名称
  9105. * @param { * } attrVal 要设置的属性值,类型视设置的属性而定
  9106. * @return { * } 返回attrs对象下的属性值
  9107. * @example
  9108. * ```javascript
  9109. * node.setAttr('title','标题');
  9110. * ```
  9111. */
  9112. setAttr: function (attrName, attrVal) {
  9113. if (!attrName) {
  9114. delete this.attrs;
  9115. return;
  9116. }
  9117. if (!this.attrs) {
  9118. this.attrs = {};
  9119. }
  9120. if (utils.isObject(attrName)) {
  9121. for (var a in attrName) {
  9122. if (!attrName[a]) {
  9123. delete this.attrs[a]
  9124. } else {
  9125. this.attrs[a.toLowerCase()] = attrName[a];
  9126. }
  9127. }
  9128. } else {
  9129. if (!attrVal) {
  9130. delete this.attrs[attrName]
  9131. } else {
  9132. this.attrs[attrName.toLowerCase()] = attrVal;
  9133. }
  9134. }
  9135. },
  9136. /**
  9137. * 获取当前节点在父节点下的位置索引
  9138. * @method getIndex
  9139. * @return { Number } 返回索引数值,如果没有父节点,返回-1
  9140. * @example
  9141. * ```javascript
  9142. * node.getIndex();
  9143. * ```
  9144. */
  9145. getIndex: function () {
  9146. var parent = this.parentNode;
  9147. for (var i = 0, ci; ci = parent.children[i]; i++) {
  9148. if (ci === this) {
  9149. return i;
  9150. }
  9151. }
  9152. return -1;
  9153. },
  9154. /**
  9155. * 在当前节点下,根据id查找节点
  9156. * @method getNodeById
  9157. * @param { String } id 要查找的id
  9158. * @return { UE.uNode } 返回找到的节点
  9159. * @example
  9160. * ```javascript
  9161. * node.getNodeById('textId');
  9162. * ```
  9163. */
  9164. getNodeById: function (id) {
  9165. var node;
  9166. if (this.children && this.children.length) {
  9167. for (var i = 0, ci; ci = this.children[i++];) {
  9168. if (node = getNodeById(ci, id)) {
  9169. return node;
  9170. }
  9171. }
  9172. }
  9173. },
  9174. /**
  9175. * 在当前节点下,根据元素名称查找节点列表
  9176. * @method getNodesByTagName
  9177. * @param { String } tagNames 要查找的元素名称
  9178. * @return { Array } 返回找到的节点列表
  9179. * @example
  9180. * ```javascript
  9181. * node.getNodesByTagName('span');
  9182. * ```
  9183. */
  9184. getNodesByTagName: function (tagNames) {
  9185. tagNames = utils.trim(tagNames).replace(/[ ]{2,}/g, ' ').split(' ');
  9186. var arr = [],
  9187. me = this;
  9188. utils.each(tagNames, function (tagName) {
  9189. if (me.children && me.children.length) {
  9190. for (var i = 0, ci; ci = me.children[i++];) {
  9191. getNodesByTagName(ci, tagName, arr)
  9192. }
  9193. }
  9194. });
  9195. return arr;
  9196. },
  9197. /**
  9198. * 根据样式名称,获取节点的样式值
  9199. * @method getStyle
  9200. * @param { String } name 要获取的样式名称
  9201. * @return { String } 返回样式值
  9202. * @example
  9203. * ```javascript
  9204. * node.getStyle('font-size');
  9205. * ```
  9206. */
  9207. getStyle: function (name) {
  9208. var cssStyle = this.getAttr('style');
  9209. if (!cssStyle) {
  9210. return ''
  9211. }
  9212. var reg = new RegExp('(^|;)\\s*' + name + ':([^;]+)', 'i');
  9213. var match = cssStyle.match(reg);
  9214. if (match && match[0]) {
  9215. return match[2]
  9216. }
  9217. return '';
  9218. },
  9219. /**
  9220. * 给节点设置样式
  9221. * @method setStyle
  9222. * @param { String } name 要设置的的样式名称
  9223. * @param { String } val 要设置的的样值
  9224. * @example
  9225. * ```javascript
  9226. * node.setStyle('font-size', '12px');
  9227. * ```
  9228. */
  9229. setStyle: function (name, val) {
  9230. function exec(name, val) {
  9231. var reg = new RegExp('(^|;)\\s*' + name + ':([^;]+;?)', 'gi');
  9232. cssStyle = cssStyle.replace(reg, '$1');
  9233. if (val) {
  9234. cssStyle = name + ':' + utils.unhtml(val) + ';' + cssStyle
  9235. }
  9236. }
  9237. var cssStyle = this.getAttr('style');
  9238. if (!cssStyle) {
  9239. cssStyle = '';
  9240. }
  9241. if (utils.isObject(name)) {
  9242. for (var a in name) {
  9243. exec(a, name[a])
  9244. }
  9245. } else {
  9246. exec(name, val)
  9247. }
  9248. this.setAttr('style', utils.trim(cssStyle))
  9249. },
  9250. /**
  9251. * 传入一个函数,递归遍历当前节点下的所有节点
  9252. * @method traversal
  9253. * @param { Function } fn 遍历到节点的时,传入节点作为参数,运行此函数
  9254. * @example
  9255. * ```javascript
  9256. * traversal(node, function(){
  9257. * console.log(node.type);
  9258. * });
  9259. * ```
  9260. */
  9261. traversal: function (fn) {
  9262. if (this.children && this.children.length) {
  9263. nodeTraversal(this, fn);
  9264. }
  9265. return this;
  9266. }
  9267. }
  9268. })();
  9269. // core/htmlparser.js
  9270. /**
  9271. * html字符串转换成uNode节点
  9272. * @file
  9273. * @module UE
  9274. * @since 1.2.6.1
  9275. */
  9276. /**
  9277. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  9278. * @unfile
  9279. * @module UE
  9280. */
  9281. /**
  9282. * html字符串转换成uNode节点的静态方法
  9283. * @method htmlparser
  9284. * @param { String } htmlstr 要转换的html代码
  9285. * @param { Boolean } ignoreBlank 若设置为true,转换的时候忽略\n\r\t等空白字符
  9286. * @return { uNode } 给定的html片段转换形成的uNode对象
  9287. * @example
  9288. * ```javascript
  9289. * var root = UE.htmlparser('<p><b>htmlparser</b></p>', true);
  9290. * ```
  9291. */
  9292. var htmlparser = UE.htmlparser = function (htmlstr, ignoreBlank) {
  9293. //todo 原来的方式 [^"'<>\/] 有\/就不能配对上 <TD vAlign=top background=../AAA.JPG> 这样的标签了
  9294. //先去掉了,加上的原因忘了,这里先记录
  9295. var re_tag = /<(?:(?:\/([^>]+)>)|(?:!--([\S|\s]*?)-->)|(?:([^\s\/<>]+)\s*((?:(?:"[^"]*")|(?:'[^']*')|[^"'<>])*)\/?>))/g,
  9296. re_attr = /([\w\-:.]+)(?:(?:\s*=\s*(?:(?:"([^"]*)")|(?:'([^']*)')|([^\s>]+)))|(?=\s|$))/g;
  9297. //ie下取得的html可能会有\n存在,要去掉,在处理replace(/[\t\r\n]*/g,'');代码高量的\n不能去除
  9298. var allowEmptyTags = {
  9299. b: 1,
  9300. code: 1,
  9301. i: 1,
  9302. u: 1,
  9303. strike: 1,
  9304. s: 1,
  9305. tt: 1,
  9306. strong: 1,
  9307. q: 1,
  9308. samp: 1,
  9309. em: 1,
  9310. span: 1,
  9311. sub: 1,
  9312. img: 1,
  9313. sup: 1,
  9314. font: 1,
  9315. big: 1,
  9316. small: 1,
  9317. iframe: 1,
  9318. a: 1,
  9319. br: 1,
  9320. pre: 1
  9321. };
  9322. htmlstr = htmlstr.replace(new RegExp(domUtils.fillChar, 'g'), '');
  9323. if (!ignoreBlank) {
  9324. htmlstr = htmlstr.replace(new RegExp('[\\r\\t\\n' + (ignoreBlank ? '' : ' ') + ']*<\/?(\\w+)\\s*(?:[^>]*)>[\\r\\t\\n' + (ignoreBlank ? '' : ' ') + ']*', 'g'), function (a, b) {
  9325. //br暂时单独处理
  9326. if (b && allowEmptyTags[b.toLowerCase()]) {
  9327. return a.replace(/(^[\n\r]+)|([\n\r]+$)/g, '');
  9328. }
  9329. return a.replace(new RegExp('^[\\r\\n' + (ignoreBlank ? '' : ' ') + ']+'), '').replace(new RegExp('[\\r\\n' + (ignoreBlank ? '' : ' ') + ']+$'), '');
  9330. });
  9331. }
  9332. var notTransAttrs = {
  9333. 'href': 1,
  9334. 'src': 1
  9335. };
  9336. var uNode = UE.uNode,
  9337. needParentNode = {
  9338. 'td': 'tr',
  9339. 'tr': ['tbody', 'thead', 'tfoot'],
  9340. 'tbody': 'table',
  9341. 'th': 'tr',
  9342. 'thead': 'table',
  9343. 'tfoot': 'table',
  9344. 'caption': 'table',
  9345. 'li': ['ul', 'ol'],
  9346. 'dt': 'dl',
  9347. 'dd': 'dl',
  9348. 'option': 'select'
  9349. },
  9350. needChild = {
  9351. 'ol': 'li',
  9352. 'ul': 'li'
  9353. };
  9354. function text(parent, data) {
  9355. if (needChild[parent.tagName]) {
  9356. var tmpNode = uNode.createElement(needChild[parent.tagName]);
  9357. parent.appendChild(tmpNode);
  9358. tmpNode.appendChild(uNode.createText(data));
  9359. parent = tmpNode;
  9360. } else {
  9361. parent.appendChild(uNode.createText(data));
  9362. }
  9363. }
  9364. function element(parent, tagName, htmlattr) {
  9365. var needParentTag;
  9366. if (needParentTag = needParentNode[tagName]) {
  9367. var tmpParent = parent,
  9368. hasParent;
  9369. while (tmpParent.type != 'root') {
  9370. if (utils.isArray(needParentTag) ? utils.indexOf(needParentTag, tmpParent.tagName) != -1 : needParentTag == tmpParent.tagName) {
  9371. parent = tmpParent;
  9372. hasParent = true;
  9373. break;
  9374. }
  9375. tmpParent = tmpParent.parentNode;
  9376. }
  9377. if (!hasParent) {
  9378. parent = element(parent, utils.isArray(needParentTag) ? needParentTag[0] : needParentTag)
  9379. }
  9380. }
  9381. //按dtd处理嵌套
  9382. // if(parent.type != 'root' && !dtd[parent.tagName][tagName])
  9383. // parent = parent.parentNode;
  9384. var elm = new uNode({
  9385. parentNode: parent,
  9386. type: 'element',
  9387. tagName: tagName.toLowerCase(),
  9388. //是自闭合的处理一下
  9389. children: dtd.$empty[tagName] ? null : []
  9390. });
  9391. //如果属性存在,处理属性
  9392. if (htmlattr) {
  9393. var attrs = {},
  9394. match;
  9395. while (match = re_attr.exec(htmlattr)) {
  9396. attrs[match[1].toLowerCase()] = notTransAttrs[match[1].toLowerCase()] ? (match[2] || match[3] || match[4]) : utils.unhtml(match[2] || match[3] || match[4])
  9397. }
  9398. elm.attrs = attrs;
  9399. }
  9400. //trace:3970
  9401. // //如果parent下不能放elm
  9402. // if(dtd.$inline[parent.tagName] && dtd.$block[elm.tagName] && !dtd[parent.tagName][elm.tagName]){
  9403. // parent = parent.parentNode;
  9404. // elm.parentNode = parent;
  9405. // }
  9406. parent.children.push(elm);
  9407. //如果是自闭合节点返回父亲节点
  9408. return dtd.$empty[tagName] ? parent : elm
  9409. }
  9410. function comment(parent, data) {
  9411. parent.children.push(new uNode({
  9412. type: 'comment',
  9413. data: data,
  9414. parentNode: parent
  9415. }));
  9416. }
  9417. var match, currentIndex = 0,
  9418. nextIndex = 0;
  9419. //设置根节点
  9420. var root = new uNode({
  9421. type: 'root',
  9422. children: []
  9423. });
  9424. var currentParent = root;
  9425. while (match = re_tag.exec(htmlstr)) {
  9426. currentIndex = match.index;
  9427. try {
  9428. if (currentIndex > nextIndex) {
  9429. //text node
  9430. text(currentParent, htmlstr.slice(nextIndex, currentIndex));
  9431. }
  9432. if (match[3]) {
  9433. if (dtd.$cdata[currentParent.tagName]) {
  9434. text(currentParent, match[0]);
  9435. } else {
  9436. //start tag
  9437. currentParent = element(currentParent, match[3].toLowerCase(), match[4]);
  9438. }
  9439. } else if (match[1]) {
  9440. if (currentParent.type != 'root') {
  9441. if (dtd.$cdata[currentParent.tagName] && !dtd.$cdata[match[1]]) {
  9442. text(currentParent, match[0]);
  9443. } else {
  9444. var tmpParent = currentParent;
  9445. while (currentParent.type == 'element' && currentParent.tagName != match[1].toLowerCase()) {
  9446. currentParent = currentParent.parentNode;
  9447. if (currentParent.type == 'root') {
  9448. currentParent = tmpParent;
  9449. throw 'break'
  9450. }
  9451. }
  9452. //end tag
  9453. currentParent = currentParent.parentNode;
  9454. }
  9455. }
  9456. } else if (match[2]) {
  9457. //comment
  9458. comment(currentParent, match[2])
  9459. }
  9460. } catch (e) {}
  9461. nextIndex = re_tag.lastIndex;
  9462. }
  9463. //如果结束是文本,就有可能丢掉,所以这里手动判断一下
  9464. //例如 <li>sdfsdfsdf<li>sdfsdfsdfsdf
  9465. if (nextIndex < htmlstr.length) {
  9466. text(currentParent, htmlstr.slice(nextIndex));
  9467. }
  9468. return root;
  9469. };
  9470. // core/filternode.js
  9471. /**
  9472. * UE过滤节点的静态方法
  9473. * @file
  9474. */
  9475. /**
  9476. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  9477. * @module UE
  9478. */
  9479. /**
  9480. * 根据传入节点和过滤规则过滤相应节点
  9481. * @module UE
  9482. * @since 1.2.6.1
  9483. * @method filterNode
  9484. * @param { Object } root 指定root节点
  9485. * @param { Object } rules 过滤规则json对象
  9486. * @example
  9487. * ```javascript
  9488. * UE.filterNode(root,editor.options.filterRules);
  9489. * ```
  9490. */
  9491. var filterNode = UE.filterNode = function () {
  9492. function filterNode(node, rules) {
  9493. switch (node.type) {
  9494. case 'text':
  9495. break;
  9496. case 'element':
  9497. var val;
  9498. if (val = rules[node.tagName]) {
  9499. if (val === '-') {
  9500. node.parentNode.removeChild(node)
  9501. } else if (utils.isFunction(val)) {
  9502. var parentNode = node.parentNode,
  9503. index = node.getIndex();
  9504. val(node);
  9505. if (node.parentNode) {
  9506. if (node.children) {
  9507. for (var i = 0, ci; ci = node.children[i];) {
  9508. filterNode(ci, rules);
  9509. if (ci.parentNode) {
  9510. i++;
  9511. }
  9512. }
  9513. }
  9514. } else {
  9515. for (var i = index, ci; ci = parentNode.children[i];) {
  9516. filterNode(ci, rules);
  9517. if (ci.parentNode) {
  9518. i++;
  9519. }
  9520. }
  9521. }
  9522. } else {
  9523. var attrs = val['$'];
  9524. if (attrs && node.attrs) {
  9525. var tmpAttrs = {},
  9526. tmpVal;
  9527. for (var a in attrs) {
  9528. tmpVal = node.getAttr(a);
  9529. //todo 只先对style单独处理
  9530. if (a == 'style' && utils.isArray(attrs[a])) {
  9531. var tmpCssStyle = [];
  9532. utils.each(attrs[a], function (v) {
  9533. var tmp;
  9534. if (tmp = node.getStyle(v)) {
  9535. tmpCssStyle.push(v + ':' + tmp);
  9536. }
  9537. });
  9538. tmpVal = tmpCssStyle.join(';')
  9539. }
  9540. if (tmpVal) {
  9541. tmpAttrs[a] = tmpVal;
  9542. }
  9543. }
  9544. node.attrs = tmpAttrs;
  9545. }
  9546. if (node.children) {
  9547. for (var i = 0, ci; ci = node.children[i];) {
  9548. filterNode(ci, rules);
  9549. if (ci.parentNode) {
  9550. i++;
  9551. }
  9552. }
  9553. }
  9554. }
  9555. } else {
  9556. //如果不在名单里扣出子节点并删除该节点,cdata除外
  9557. if (dtd.$cdata[node.tagName]) {
  9558. node.parentNode.removeChild(node)
  9559. } else {
  9560. var parentNode = node.parentNode,
  9561. index = node.getIndex();
  9562. node.parentNode.removeChild(node, true);
  9563. for (var i = index, ci; ci = parentNode.children[i];) {
  9564. filterNode(ci, rules);
  9565. if (ci.parentNode) {
  9566. i++;
  9567. }
  9568. }
  9569. }
  9570. }
  9571. break;
  9572. case 'comment':
  9573. node.parentNode.removeChild(node)
  9574. }
  9575. }
  9576. return function (root, rules) {
  9577. if (utils.isEmptyObject(rules)) {
  9578. return root;
  9579. }
  9580. var val;
  9581. if (val = rules['-']) {
  9582. utils.each(val.split(' '), function (k) {
  9583. rules[k] = '-'
  9584. })
  9585. }
  9586. for (var i = 0, ci; ci = root.children[i];) {
  9587. filterNode(ci, rules);
  9588. if (ci.parentNode) {
  9589. i++;
  9590. }
  9591. }
  9592. return root;
  9593. }
  9594. }();
  9595. // core/plugin.js
  9596. /**
  9597. * Created with JetBrains PhpStorm.
  9598. * User: campaign
  9599. * Date: 10/8/13
  9600. * Time: 6:15 PM
  9601. * To change this template use File | Settings | File Templates.
  9602. */
  9603. UE.plugin = function () {
  9604. var _plugins = {};
  9605. return {
  9606. register: function (pluginName, fn, oldOptionName, afterDisabled) {
  9607. if (oldOptionName && utils.isFunction(oldOptionName)) {
  9608. afterDisabled = oldOptionName;
  9609. oldOptionName = null
  9610. }
  9611. _plugins[pluginName] = {
  9612. optionName: oldOptionName || pluginName,
  9613. execFn: fn,
  9614. //当插件被禁用时执行
  9615. afterDisabled: afterDisabled
  9616. }
  9617. },
  9618. load: function (editor) {
  9619. utils.each(_plugins, function (plugin) {
  9620. var _export = plugin.execFn.call(editor);
  9621. if (editor.options[plugin.optionName] !== false) {
  9622. if (_export) {
  9623. //后边需要再做扩展
  9624. utils.each(_export, function (v, k) {
  9625. switch (k.toLowerCase()) {
  9626. case 'shortcutkey':
  9627. editor.addshortcutkey(v);
  9628. break;
  9629. case 'bindevents':
  9630. utils.each(v, function (fn, eventName) {
  9631. editor.addListener(eventName, fn);
  9632. });
  9633. break;
  9634. case 'bindmultievents':
  9635. utils.each(utils.isArray(v) ? v : [v], function (event) {
  9636. var types = utils.trim(event.type).split(/\s+/);
  9637. utils.each(types, function (eventName) {
  9638. editor.addListener(eventName, event.handler);
  9639. });
  9640. });
  9641. break;
  9642. case 'commands':
  9643. utils.each(v, function (execFn, execName) {
  9644. editor.commands[execName] = execFn
  9645. });
  9646. break;
  9647. case 'outputrule':
  9648. editor.addOutputRule(v);
  9649. break;
  9650. case 'inputrule':
  9651. editor.addInputRule(v);
  9652. break;
  9653. case 'defaultoptions':
  9654. editor.setOpt(v)
  9655. }
  9656. })
  9657. }
  9658. } else if (plugin.afterDisabled) {
  9659. plugin.afterDisabled.call(editor)
  9660. }
  9661. });
  9662. //向下兼容
  9663. utils.each(UE.plugins, function (plugin) {
  9664. plugin.call(editor);
  9665. });
  9666. },
  9667. run: function (pluginName, editor) {
  9668. var plugin = _plugins[pluginName];
  9669. if (plugin) {
  9670. plugin.exeFn.call(editor)
  9671. }
  9672. }
  9673. }
  9674. }();
  9675. // core/keymap.js
  9676. var keymap = UE.keymap = {
  9677. 'Backspace': 8,
  9678. 'Tab': 9,
  9679. 'Enter': 13,
  9680. 'Shift': 16,
  9681. 'Control': 17,
  9682. 'Alt': 18,
  9683. 'CapsLock': 20,
  9684. 'Esc': 27,
  9685. 'Spacebar': 32,
  9686. 'PageUp': 33,
  9687. 'PageDown': 34,
  9688. 'End': 35,
  9689. 'Home': 36,
  9690. 'Left': 37,
  9691. 'Up': 38,
  9692. 'Right': 39,
  9693. 'Down': 40,
  9694. 'Insert': 45,
  9695. 'Del': 46,
  9696. 'NumLock': 144,
  9697. 'Cmd': 91,
  9698. '=': 187,
  9699. '-': 189,
  9700. "b": 66,
  9701. 'i': 73,
  9702. //回退
  9703. 'z': 90,
  9704. 'y': 89,
  9705. //粘贴
  9706. 'v': 86,
  9707. 'x': 88,
  9708. 's': 83,
  9709. 'n': 78
  9710. };
  9711. // core/localstorage.js
  9712. //存储媒介封装
  9713. var LocalStorage = UE.LocalStorage = (function () {
  9714. var storage = window.localStorage || getUserData() || null,
  9715. LOCAL_FILE = 'localStorage';
  9716. return {
  9717. saveLocalData: function (key, data) {
  9718. if (storage && data) {
  9719. storage.setItem(key, data);
  9720. return true;
  9721. }
  9722. return false;
  9723. },
  9724. getLocalData: function (key) {
  9725. if (storage) {
  9726. return storage.getItem(key);
  9727. }
  9728. return null;
  9729. },
  9730. removeItem: function (key) {
  9731. storage && storage.removeItem(key);
  9732. }
  9733. };
  9734. function getUserData() {
  9735. var container = document.createElement("div");
  9736. container.style.display = "none";
  9737. if (!container.addBehavior) {
  9738. return null;
  9739. }
  9740. container.addBehavior("#default#userdata");
  9741. return {
  9742. getItem: function (key) {
  9743. var result = null;
  9744. try {
  9745. document.body.appendChild(container);
  9746. container.load(LOCAL_FILE);
  9747. result = container.getAttribute(key);
  9748. document.body.removeChild(container);
  9749. } catch (e) {}
  9750. return result;
  9751. },
  9752. setItem: function (key, value) {
  9753. document.body.appendChild(container);
  9754. container.setAttribute(key, value);
  9755. container.save(LOCAL_FILE);
  9756. document.body.removeChild(container);
  9757. },
  9758. //// 暂时没有用到
  9759. //clear: function () {
  9760. //
  9761. // var expiresTime = new Date();
  9762. // expiresTime.setFullYear(expiresTime.getFullYear() - 1);
  9763. // document.body.appendChild(container);
  9764. // container.expires = expiresTime.toUTCString();
  9765. // container.save(LOCAL_FILE);
  9766. // document.body.removeChild(container);
  9767. //
  9768. //},
  9769. removeItem: function (key) {
  9770. document.body.appendChild(container);
  9771. container.removeAttribute(key);
  9772. container.save(LOCAL_FILE);
  9773. document.body.removeChild(container);
  9774. }
  9775. };
  9776. }
  9777. })();
  9778. (function () {
  9779. var ROOTKEY = 'ueditor_preference';
  9780. UE.Editor.prototype.setPreferences = function (key, value) {
  9781. var obj = {};
  9782. if (utils.isString(key)) {
  9783. obj[key] = value;
  9784. } else {
  9785. obj = key;
  9786. }
  9787. var data = LocalStorage.getLocalData(ROOTKEY);
  9788. if (data && (data = utils.str2json(data))) {
  9789. utils.extend(data, obj);
  9790. } else {
  9791. data = obj;
  9792. }
  9793. data && LocalStorage.saveLocalData(ROOTKEY, utils.json2str(data));
  9794. };
  9795. UE.Editor.prototype.getPreferences = function (key) {
  9796. var data = LocalStorage.getLocalData(ROOTKEY);
  9797. if (data && (data = utils.str2json(data))) {
  9798. return key ? data[key] : data
  9799. }
  9800. return null;
  9801. };
  9802. UE.Editor.prototype.removePreferences = function (key) {
  9803. var data = LocalStorage.getLocalData(ROOTKEY);
  9804. if (data && (data = utils.str2json(data))) {
  9805. data[key] = undefined;
  9806. delete data[key]
  9807. }
  9808. data && LocalStorage.saveLocalData(ROOTKEY, utils.json2str(data));
  9809. };
  9810. })();
  9811. // plugins/defaultfilter.js
  9812. ///import core
  9813. ///plugin 编辑器默认的过滤转换机制
  9814. UE.plugins['defaultfilter'] = function () {
  9815. var me = this;
  9816. me.setOpt({
  9817. 'allowDivTransToP': true,
  9818. 'disabledTableInTable': true
  9819. });
  9820. //默认的过滤处理
  9821. //进入编辑器的内容处理
  9822. me.addInputRule(function (root) {
  9823. var allowDivTransToP = this.options.allowDivTransToP;
  9824. var val;
  9825. function tdParent(node) {
  9826. while (node && node.type == 'element') {
  9827. if (node.tagName == 'td') {
  9828. return true;
  9829. }
  9830. node = node.parentNode;
  9831. }
  9832. return false;
  9833. }
  9834. //进行默认的处理
  9835. root.traversal(function (node) {
  9836. if (node.type == 'element') {
  9837. if (!dtd.$cdata[node.tagName] && me.options.autoClearEmptyNode && dtd.$inline[node.tagName] && !dtd.$empty[node.tagName] && (!node.attrs || utils.isEmptyObject(node.attrs))) {
  9838. if (!node.firstChild()) node.parentNode.removeChild(node);
  9839. else if (node.tagName == 'span' && (!node.attrs || utils.isEmptyObject(node.attrs))) {
  9840. node.parentNode.removeChild(node, true)
  9841. }
  9842. return;
  9843. }
  9844. switch (node.tagName) {
  9845. case 'style':
  9846. case 'script':
  9847. node.setAttr({
  9848. cdata_tag: node.tagName,
  9849. cdata_data: (node.innerHTML() || ''),
  9850. '_ue_custom_node_': 'true'
  9851. });
  9852. node.tagName = 'div';
  9853. node.innerHTML('');
  9854. break;
  9855. case 'a':
  9856. if (val = node.getAttr('href')) {
  9857. node.setAttr('_href', val)
  9858. }
  9859. break;
  9860. // case 'img':
  9861. // //todo base64暂时去掉,后边做远程图片上传后,干掉这个
  9862. // if (val = node.getAttr('src')) {
  9863. // if (/^data:/.test(val)) {
  9864. // node.parentNode.removeChild(node);
  9865. // break;
  9866. // }
  9867. // }
  9868. // node.setAttr('_src', node.getAttr('src'));
  9869. // break;
  9870. case 'span':
  9871. if (browser.webkit && (val = node.getStyle('white-space'))) {
  9872. if (/nowrap|normal/.test(val)) {
  9873. node.setStyle('white-space', '');
  9874. if (me.options.autoClearEmptyNode && utils.isEmptyObject(node.attrs)) {
  9875. node.parentNode.removeChild(node, true)
  9876. }
  9877. }
  9878. }
  9879. val = node.getAttr('id');
  9880. if (val && /^_baidu_bookmark_/i.test(val)) {
  9881. node.parentNode.removeChild(node)
  9882. }
  9883. break;
  9884. case 'p':
  9885. if (val = node.getAttr('align')) {
  9886. node.setAttr('align');
  9887. node.setStyle('text-align', val)
  9888. }
  9889. //trace:3431
  9890. // var cssStyle = node.getAttr('style');
  9891. // if (cssStyle) {
  9892. // cssStyle = cssStyle.replace(/(margin|padding)[^;]+/g, '');
  9893. // node.setAttr('style', cssStyle)
  9894. //
  9895. // }
  9896. //p标签不允许嵌套
  9897. utils.each(node.children, function (n) {
  9898. if (n.type == 'element' && n.tagName == 'p') {
  9899. var next = n.nextSibling();
  9900. node.parentNode.insertAfter(n, node);
  9901. var last = n;
  9902. while (next) {
  9903. var tmp = next.nextSibling();
  9904. node.parentNode.insertAfter(next, last);
  9905. last = next;
  9906. next = tmp;
  9907. }
  9908. return false;
  9909. }
  9910. });
  9911. if (!node.firstChild()) {
  9912. node.innerHTML(browser.ie ? '&nbsp;' : '<br/>')
  9913. }
  9914. break;
  9915. case 'div':
  9916. if (node.getAttr('cdata_tag')) {
  9917. break;
  9918. }
  9919. //针对代码这里不处理插入代码的div
  9920. val = node.getAttr('class');
  9921. if (val && /^line number\d+/.test(val)) {
  9922. break;
  9923. }
  9924. if (!allowDivTransToP) {
  9925. break;
  9926. }
  9927. var tmpNode, p = UE.uNode.createElement('p');
  9928. while (tmpNode = node.firstChild()) {
  9929. if (tmpNode.type == 'text' || !UE.dom.dtd.$block[tmpNode.tagName]) {
  9930. p.appendChild(tmpNode);
  9931. } else {
  9932. if (p.firstChild()) {
  9933. node.parentNode.insertBefore(p, node);
  9934. p = UE.uNode.createElement('p');
  9935. } else {
  9936. node.parentNode.insertBefore(tmpNode, node);
  9937. }
  9938. }
  9939. }
  9940. if (p.firstChild()) {
  9941. node.parentNode.insertBefore(p, node);
  9942. }
  9943. node.parentNode.removeChild(node);
  9944. break;
  9945. case 'dl':
  9946. node.tagName = 'ul';
  9947. break;
  9948. case 'dt':
  9949. case 'dd':
  9950. node.tagName = 'li';
  9951. break;
  9952. case 'li':
  9953. var className = node.getAttr('class');
  9954. if (!className || !/list\-/.test(className)) {
  9955. node.setAttr()
  9956. }
  9957. var tmpNodes = node.getNodesByTagName('ol ul');
  9958. UE.utils.each(tmpNodes, function (n) {
  9959. node.parentNode.insertAfter(n, node);
  9960. });
  9961. break;
  9962. case 'td':
  9963. case 'th':
  9964. case 'caption':
  9965. if (!node.children || !node.children.length) {
  9966. node.appendChild(browser.ie11below ? UE.uNode.createText(' ') : UE.uNode.createElement('br'))
  9967. }
  9968. break;
  9969. case 'table':
  9970. if (me.options.disabledTableInTable && tdParent(node)) {
  9971. node.parentNode.insertBefore(UE.uNode.createText(node.innerText()), node);
  9972. node.parentNode.removeChild(node)
  9973. }
  9974. }
  9975. }
  9976. // if(node.type == 'comment'){
  9977. // node.parentNode.removeChild(node);
  9978. // }
  9979. })
  9980. });
  9981. //从编辑器出去的内容处理
  9982. me.addOutputRule(function (root) {
  9983. var val;
  9984. root.traversal(function (node) {
  9985. if (node.type == 'element') {
  9986. if (me.options.autoClearEmptyNode && dtd.$inline[node.tagName] && !dtd.$empty[node.tagName] && (!node.attrs || utils.isEmptyObject(node.attrs))) {
  9987. if (!node.firstChild()) node.parentNode.removeChild(node);
  9988. else if (node.tagName == 'span' && (!node.attrs || utils.isEmptyObject(node.attrs))) {
  9989. node.parentNode.removeChild(node, true)
  9990. }
  9991. return;
  9992. }
  9993. switch (node.tagName) {
  9994. case 'div':
  9995. if (val = node.getAttr('cdata_tag')) {
  9996. node.tagName = val;
  9997. node.appendChild(UE.uNode.createText(node.getAttr('cdata_data')));
  9998. node.setAttr({
  9999. cdata_tag: '',
  10000. cdata_data: '',
  10001. '_ue_custom_node_': ''
  10002. });
  10003. }
  10004. break;
  10005. case 'a':
  10006. if (val = node.getAttr('_href')) {
  10007. node.setAttr({
  10008. 'href': utils.html(val),
  10009. '_href': ''
  10010. })
  10011. }
  10012. break;
  10013. break;
  10014. case 'span':
  10015. val = node.getAttr('id');
  10016. if (val && /^_baidu_bookmark_/i.test(val)) {
  10017. node.parentNode.removeChild(node)
  10018. }
  10019. break;
  10020. case 'img':
  10021. if (val = node.getAttr('_src')) {
  10022. node.setAttr({
  10023. 'src': node.getAttr('_src'),
  10024. '_src': ''
  10025. })
  10026. }
  10027. }
  10028. }
  10029. })
  10030. });
  10031. };
  10032. // plugins/inserthtml.js
  10033. /**
  10034. * 插入html字符串插件
  10035. * @file
  10036. * @since 1.2.6.1
  10037. */
  10038. /**
  10039. * 插入html代码
  10040. * @command inserthtml
  10041. * @method execCommand
  10042. * @param { String } cmd 命令字符串
  10043. * @param { String } html 插入的html字符串
  10044. * @remaind 插入的标签内容是在当前的选区位置上插入,如果当前是闭合状态,那直接插入内容, 如果当前是选中状态,将先清除当前选中内容后,再做插入
  10045. * @warning 注意:该命令会对当前选区的位置,对插入的内容进行过滤转换处理。 过滤的规则遵循html语意化的原则。
  10046. * @example
  10047. * ```javascript
  10048. * //xxx[BB]xxx 当前选区为非闭合选区,选中BB这两个文本
  10049. * //执行命令,插入<b>CC</b>
  10050. * //插入后的效果 xxx<b>CC</b>xxx
  10051. * //<p>xx|xxx</p> 当前选区为闭合状态
  10052. * //插入<p>CC</p>
  10053. * //结果 <p>xx</p><p>CC</p><p>xxx</p>
  10054. * //<p>xxxx</p>|</p>xxx</p> 当前选区在两个p标签之间
  10055. * //插入 xxxx
  10056. * //结果 <p>xxxx</p><p>xxxx</p></p>xxx</p>
  10057. * ```
  10058. */
  10059. UE.commands['inserthtml'] = {
  10060. execCommand: function (command, html, notNeedFilter) {
  10061. var me = this,
  10062. range,
  10063. div;
  10064. if (!html) {
  10065. return;
  10066. }
  10067. if (me.fireEvent('beforeinserthtml', html) === true) {
  10068. return;
  10069. }
  10070. range = me.selection.getRange();
  10071. div = range.document.createElement('div');
  10072. div.style.display = 'inline';
  10073. if (!notNeedFilter) {
  10074. var root = UE.htmlparser(html);
  10075. //如果给了过滤规则就先进行过滤
  10076. if (me.options.filterRules) {
  10077. UE.filterNode(root, me.options.filterRules);
  10078. }
  10079. //执行默认的处理
  10080. me.filterInputRule(root);
  10081. html = root.toHtml()
  10082. }
  10083. div.innerHTML = utils.trim(html);
  10084. if (!range.collapsed) {
  10085. var tmpNode = range.startContainer;
  10086. if (domUtils.isFillChar(tmpNode)) {
  10087. range.setStartBefore(tmpNode)
  10088. }
  10089. tmpNode = range.endContainer;
  10090. if (domUtils.isFillChar(tmpNode)) {
  10091. range.setEndAfter(tmpNode)
  10092. }
  10093. range.txtToElmBoundary();
  10094. //结束边界可能放到了br的前边,要把br包含进来
  10095. // x[xxx]<br/>
  10096. if (range.endContainer && range.endContainer.nodeType == 1) {
  10097. tmpNode = range.endContainer.childNodes[range.endOffset];
  10098. if (tmpNode && domUtils.isBr(tmpNode)) {
  10099. range.setEndAfter(tmpNode);
  10100. }
  10101. }
  10102. if (range.startOffset == 0) {
  10103. tmpNode = range.startContainer;
  10104. if (domUtils.isBoundaryNode(tmpNode, 'firstChild')) {
  10105. tmpNode = range.endContainer;
  10106. if (range.endOffset == (tmpNode.nodeType == 3 ? tmpNode.nodeValue.length : tmpNode.childNodes.length) && domUtils.isBoundaryNode(tmpNode, 'lastChild')) {
  10107. me.body.innerHTML = '<p>' + (browser.ie ? '' : '<br/>') + '</p>';
  10108. range.setStart(me.body.firstChild, 0).collapse(true)
  10109. }
  10110. }
  10111. }!range.collapsed && range.deleteContents();
  10112. if (range.startContainer.nodeType == 1) {
  10113. var child = range.startContainer.childNodes[range.startOffset],
  10114. pre;
  10115. if (child && domUtils.isBlockElm(child) && (pre = child.previousSibling) && domUtils.isBlockElm(pre)) {
  10116. range.setEnd(pre, pre.childNodes.length).collapse();
  10117. while (child.firstChild) {
  10118. pre.appendChild(child.firstChild);
  10119. }
  10120. domUtils.remove(child);
  10121. }
  10122. }
  10123. }
  10124. var child, parent, pre, tmp, hadBreak = 0,
  10125. nextNode;
  10126. //如果当前位置选中了fillchar要干掉,要不会产生空行
  10127. if (range.inFillChar()) {
  10128. child = range.startContainer;
  10129. if (domUtils.isFillChar(child)) {
  10130. range.setStartBefore(child).collapse(true);
  10131. domUtils.remove(child);
  10132. } else if (domUtils.isFillChar(child, true)) {
  10133. child.nodeValue = child.nodeValue.replace(fillCharReg, '');
  10134. range.startOffset--;
  10135. range.collapsed && range.collapse(true)
  10136. }
  10137. }
  10138. //列表单独处理
  10139. var li = domUtils.findParentByTagName(range.startContainer, 'li', true);
  10140. if (li) {
  10141. var next, last;
  10142. while (child = div.firstChild) {
  10143. //针对hr单独处理一下先
  10144. while (child && (child.nodeType == 3 || !domUtils.isBlockElm(child) || child.tagName == 'HR')) {
  10145. next = child.nextSibling;
  10146. range.insertNode(child).collapse();
  10147. last = child;
  10148. child = next;
  10149. }
  10150. if (child) {
  10151. if (/^(ol|ul)$/i.test(child.tagName)) {
  10152. while (child.firstChild) {
  10153. last = child.firstChild;
  10154. domUtils.insertAfter(li, child.firstChild);
  10155. li = li.nextSibling;
  10156. }
  10157. domUtils.remove(child)
  10158. } else {
  10159. var tmpLi;
  10160. next = child.nextSibling;
  10161. tmpLi = me.document.createElement('li');
  10162. domUtils.insertAfter(li, tmpLi);
  10163. tmpLi.appendChild(child);
  10164. last = child;
  10165. child = next;
  10166. li = tmpLi;
  10167. }
  10168. }
  10169. }
  10170. li = domUtils.findParentByTagName(range.startContainer, 'li', true);
  10171. if (domUtils.isEmptyBlock(li)) {
  10172. domUtils.remove(li)
  10173. }
  10174. if (last) {
  10175. range.setStartAfter(last).collapse(true).select(true)
  10176. }
  10177. } else {
  10178. while (child = div.firstChild) {
  10179. if (hadBreak) {
  10180. var p = me.document.createElement('p');
  10181. while (child && (child.nodeType == 3 || !dtd.$block[child.tagName])) {
  10182. nextNode = child.nextSibling;
  10183. p.appendChild(child);
  10184. child = nextNode;
  10185. }
  10186. if (p.firstChild) {
  10187. child = p
  10188. }
  10189. }
  10190. range.insertNode(child);
  10191. nextNode = child.nextSibling;
  10192. if (!hadBreak && child.nodeType == domUtils.NODE_ELEMENT && domUtils.isBlockElm(child)) {
  10193. parent = domUtils.findParent(child, function (node) {
  10194. return domUtils.isBlockElm(node);
  10195. });
  10196. if (parent && parent.tagName.toLowerCase() != 'body' && !(dtd[parent.tagName][child.nodeName] && child.parentNode === parent)) {
  10197. if (!dtd[parent.tagName][child.nodeName]) {
  10198. pre = parent;
  10199. } else {
  10200. tmp = child.parentNode;
  10201. while (tmp !== parent) {
  10202. pre = tmp;
  10203. tmp = tmp.parentNode;
  10204. }
  10205. }
  10206. domUtils.breakParent(child, pre || tmp);
  10207. //去掉break后前一个多余的节点 <p>|<[p> ==> <p></p><div></div><p>|</p>
  10208. var pre = child.previousSibling;
  10209. domUtils.trimWhiteTextNode(pre);
  10210. if (!pre.childNodes.length) {
  10211. domUtils.remove(pre);
  10212. }
  10213. //trace:2012,在非ie的情况,切开后剩下的节点有可能不能点入光标添加br占位
  10214. if (!browser.ie &&
  10215. (next = child.nextSibling) &&
  10216. domUtils.isBlockElm(next) &&
  10217. next.lastChild &&
  10218. !domUtils.isBr(next.lastChild)) {
  10219. next.appendChild(me.document.createElement('br'));
  10220. }
  10221. hadBreak = 1;
  10222. }
  10223. }
  10224. var next = child.nextSibling;
  10225. if (!div.firstChild && next && domUtils.isBlockElm(next)) {
  10226. range.setStart(next, 0).collapse(true);
  10227. break;
  10228. }
  10229. range.setEndAfter(child).collapse();
  10230. }
  10231. child = range.startContainer;
  10232. if (nextNode && domUtils.isBr(nextNode)) {
  10233. domUtils.remove(nextNode)
  10234. }
  10235. //用chrome可能有空白展位符
  10236. if (domUtils.isBlockElm(child) && domUtils.isEmptyNode(child)) {
  10237. if (nextNode = child.nextSibling) {
  10238. domUtils.remove(child);
  10239. if (nextNode.nodeType == 1 && dtd.$block[nextNode.tagName]) {
  10240. range.setStart(nextNode, 0).collapse(true).shrinkBoundary()
  10241. }
  10242. } else {
  10243. try {
  10244. child.innerHTML = browser.ie ? domUtils.fillChar : '<br/>';
  10245. } catch (e) {
  10246. range.setStartBefore(child);
  10247. domUtils.remove(child)
  10248. }
  10249. }
  10250. }
  10251. //加上true因为在删除表情等时会删两次,第一次是删的fillData
  10252. try {
  10253. range.select(true);
  10254. } catch (e) {}
  10255. }
  10256. setTimeout(function () {
  10257. range = me.selection.getRange();
  10258. range.scrollToView(me.autoHeightEnabled, me.autoHeightEnabled ? domUtils.getXY(me.iframe).y : 0);
  10259. me.fireEvent('afterinserthtml', html);
  10260. }, 200);
  10261. }
  10262. };
  10263. // plugins/autotypeset.js
  10264. /**
  10265. * 自动排版
  10266. * @file
  10267. * @since 1.2.6.1
  10268. */
  10269. /**
  10270. * 对当前编辑器的内容执行自动排版, 排版的行为根据config配置文件里的“autotypeset”选项进行控制。
  10271. * @command autotypeset
  10272. * @method execCommand
  10273. * @param { String } cmd 命令字符串
  10274. * @example
  10275. * ```javascript
  10276. * editor.execCommand( 'autotypeset' );
  10277. * ```
  10278. */
  10279. UE.plugins['autotypeset'] = function () {
  10280. this.setOpt({
  10281. 'autotypeset': {
  10282. mergeEmptyline: true, //合并空行
  10283. removeClass: true, //去掉冗余的class
  10284. removeEmptyline: false, //去掉空行
  10285. textAlign: "left", //段落的排版方式,可以是 left,right,center,justify 去掉这个属性表示不执行排版
  10286. imageBlockLine: 'center', //图片的浮动方式,独占一行剧中,左右浮动,默认: center,left,right,none 去掉这个属性表示不执行排版
  10287. pasteFilter: false, //根据规则过滤没事粘贴进来的内容
  10288. clearFontSize: false, //去掉所有的内嵌字号,使用编辑器默认的字号
  10289. clearFontFamily: false, //去掉所有的内嵌字体,使用编辑器默认的字体
  10290. removeEmptyNode: false, // 去掉空节点
  10291. //可以去掉的标签
  10292. removeTagNames: utils.extend({
  10293. div: 1
  10294. }, dtd.$removeEmpty),
  10295. indent: false, // 行首缩进
  10296. indentValue: '2em', //行首缩进的大小
  10297. bdc2sb: false,
  10298. tobdc: false
  10299. }
  10300. });
  10301. var me = this,
  10302. opt = me.options.autotypeset,
  10303. remainClass = {
  10304. 'selectTdClass': 1,
  10305. 'pagebreak': 1,
  10306. 'anchorclass': 1
  10307. },
  10308. remainTag = {
  10309. 'li': 1
  10310. },
  10311. tags = {
  10312. div: 1,
  10313. p: 1,
  10314. //trace:2183 这些也认为是行
  10315. blockquote: 1,
  10316. center: 1,
  10317. h1: 1,
  10318. h2: 1,
  10319. h3: 1,
  10320. h4: 1,
  10321. h5: 1,
  10322. h6: 1,
  10323. span: 1
  10324. },
  10325. highlightCont;
  10326. //升级了版本,但配置项目里没有autotypeset
  10327. if (!opt) {
  10328. return;
  10329. }
  10330. readLocalOpts();
  10331. function isLine(node, notEmpty) {
  10332. if (!node || node.nodeType == 3)
  10333. return 0;
  10334. if (domUtils.isBr(node))
  10335. return 1;
  10336. if (node && node.parentNode && tags[node.tagName.toLowerCase()]) {
  10337. if (highlightCont && highlightCont.contains(node) ||
  10338. node.getAttribute('pagebreak')
  10339. ) {
  10340. return 0;
  10341. }
  10342. return notEmpty ? !domUtils.isEmptyBlock(node) : domUtils.isEmptyBlock(node, new RegExp('[\\s' + domUtils.fillChar +
  10343. ']', 'g'));
  10344. }
  10345. }
  10346. function removeNotAttributeSpan(node) {
  10347. if (!node.style.cssText) {
  10348. domUtils.removeAttributes(node, ['style']);
  10349. if (node.tagName.toLowerCase() == 'span' && domUtils.hasNoAttributes(node)) {
  10350. domUtils.remove(node, true);
  10351. }
  10352. }
  10353. }
  10354. function autotype(type, html) {
  10355. var me = this,
  10356. cont;
  10357. if (html) {
  10358. if (!opt.pasteFilter) {
  10359. return;
  10360. }
  10361. cont = me.document.createElement('div');
  10362. cont.innerHTML = html.html;
  10363. } else {
  10364. cont = me.document.body;
  10365. }
  10366. var nodes = domUtils.getElementsByTagName(cont, '*');
  10367. // 行首缩进,段落方向,段间距,段内间距
  10368. for (var i = 0, ci; ci = nodes[i++];) {
  10369. if (me.fireEvent('excludeNodeinautotype', ci) === true) {
  10370. continue;
  10371. }
  10372. //font-size
  10373. if (opt.clearFontSize && ci.style.fontSize) {
  10374. domUtils.removeStyle(ci, 'font-size');
  10375. removeNotAttributeSpan(ci);
  10376. }
  10377. //font-family
  10378. if (opt.clearFontFamily && ci.style.fontFamily) {
  10379. domUtils.removeStyle(ci, 'font-family');
  10380. removeNotAttributeSpan(ci);
  10381. }
  10382. if (isLine(ci)) {
  10383. //合并空行
  10384. if (opt.mergeEmptyline) {
  10385. var next = ci.nextSibling,
  10386. tmpNode, isBr = domUtils.isBr(ci);
  10387. while (isLine(next)) {
  10388. tmpNode = next;
  10389. next = tmpNode.nextSibling;
  10390. if (isBr && (!next || next && !domUtils.isBr(next))) {
  10391. break;
  10392. }
  10393. domUtils.remove(tmpNode);
  10394. }
  10395. }
  10396. //去掉空行,保留占位的空行
  10397. if (opt.removeEmptyline && domUtils.inDoc(ci, cont) && !remainTag[ci.parentNode.tagName.toLowerCase()]) {
  10398. if (domUtils.isBr(ci)) {
  10399. next = ci.nextSibling;
  10400. if (next && !domUtils.isBr(next)) {
  10401. continue;
  10402. }
  10403. }
  10404. domUtils.remove(ci);
  10405. continue;
  10406. }
  10407. }
  10408. if (isLine(ci, true) && ci.tagName != 'SPAN') {
  10409. if (opt.indent) {
  10410. ci.style.textIndent = opt.indentValue;
  10411. }
  10412. if (opt.textAlign) {
  10413. ci.style.textAlign = opt.textAlign;
  10414. }
  10415. // if(opt.lineHeight)
  10416. // ci.style.lineHeight = opt.lineHeight + 'cm';
  10417. }
  10418. //去掉class,保留的class不去掉
  10419. if (opt.removeClass && ci.className && !remainClass[ci.className.toLowerCase()]) {
  10420. if (highlightCont && highlightCont.contains(ci)) {
  10421. continue;
  10422. }
  10423. domUtils.removeAttributes(ci, ['class']);
  10424. }
  10425. //表情不处理
  10426. if (opt.imageBlockLine && ci.tagName.toLowerCase() == 'img' && !ci.getAttribute('emotion')) {
  10427. if (html) {
  10428. var img = ci;
  10429. switch (opt.imageBlockLine) {
  10430. case 'left':
  10431. case 'right':
  10432. case 'none':
  10433. var pN = img.parentNode,
  10434. tmpNode, pre, next;
  10435. while (dtd.$inline[pN.tagName] || pN.tagName == 'A') {
  10436. pN = pN.parentNode;
  10437. }
  10438. tmpNode = pN;
  10439. if (tmpNode.tagName == 'P' && domUtils.getStyle(tmpNode, 'text-align') == 'center') {
  10440. if (!domUtils.isBody(tmpNode) && domUtils.getChildCount(tmpNode, function (node) {
  10441. return !domUtils.isBr(node) && !domUtils.isWhitespace(node)
  10442. }) == 1) {
  10443. pre = tmpNode.previousSibling;
  10444. next = tmpNode.nextSibling;
  10445. if (pre && next && pre.nodeType == 1 && next.nodeType == 1 && pre.tagName == next.tagName && domUtils.isBlockElm(pre)) {
  10446. pre.appendChild(tmpNode.firstChild);
  10447. while (next.firstChild) {
  10448. pre.appendChild(next.firstChild);
  10449. }
  10450. domUtils.remove(tmpNode);
  10451. domUtils.remove(next);
  10452. } else {
  10453. domUtils.setStyle(tmpNode, 'text-align', '');
  10454. }
  10455. }
  10456. }
  10457. domUtils.setStyle(img, 'float', opt.imageBlockLine);
  10458. break;
  10459. case 'center':
  10460. if (me.queryCommandValue('imagefloat') != 'center') {
  10461. pN = img.parentNode;
  10462. domUtils.setStyle(img, 'float', 'none');
  10463. tmpNode = img;
  10464. while (pN && domUtils.getChildCount(pN, function (node) {
  10465. return !domUtils.isBr(node) && !domUtils.isWhitespace(node)
  10466. }) == 1 &&
  10467. (dtd.$inline[pN.tagName] || pN.tagName == 'A')) {
  10468. tmpNode = pN;
  10469. pN = pN.parentNode;
  10470. }
  10471. var pNode = me.document.createElement('p');
  10472. domUtils.setAttributes(pNode, {
  10473. style: 'text-align:center'
  10474. });
  10475. tmpNode.parentNode.insertBefore(pNode, tmpNode);
  10476. pNode.appendChild(tmpNode);
  10477. domUtils.setStyle(tmpNode, 'float', '');
  10478. }
  10479. }
  10480. } else {
  10481. var range = me.selection.getRange();
  10482. range.selectNode(ci).select();
  10483. me.execCommand('imagefloat', opt.imageBlockLine);
  10484. }
  10485. }
  10486. //去掉冗余的标签
  10487. if (opt.removeEmptyNode) {
  10488. if (opt.removeTagNames[ci.tagName.toLowerCase()] && domUtils.hasNoAttributes(ci) && domUtils.isEmptyBlock(ci)) {
  10489. domUtils.remove(ci);
  10490. }
  10491. }
  10492. }
  10493. if (opt.tobdc) {
  10494. var root = UE.htmlparser(cont.innerHTML);
  10495. root.traversal(function (node) {
  10496. if (node.type == 'text') {
  10497. node.data = ToDBC(node.data)
  10498. }
  10499. });
  10500. cont.innerHTML = root.toHtml()
  10501. }
  10502. if (opt.bdc2sb) {
  10503. var root = UE.htmlparser(cont.innerHTML);
  10504. root.traversal(function (node) {
  10505. if (node.type == 'text') {
  10506. node.data = DBC2SB(node.data)
  10507. }
  10508. });
  10509. cont.innerHTML = root.toHtml()
  10510. }
  10511. if (html) {
  10512. html.html = cont.innerHTML;
  10513. }
  10514. }
  10515. if (opt.pasteFilter) {
  10516. me.addListener('beforepaste', autotype);
  10517. }
  10518. function DBC2SB(str) {
  10519. var result = '';
  10520. for (var i = 0; i < str.length; i++) {
  10521. var code = str.charCodeAt(i); //获取当前字符的unicode编码
  10522. if (code >= 65281 && code <= 65373) //在这个unicode编码范围中的是所有的英文字母已经各种字符
  10523. {
  10524. result += String.fromCharCode(str.charCodeAt(i) - 65248); //把全角字符的unicode编码转换为对应半角字符的unicode码
  10525. } else if (code == 12288) //空格
  10526. {
  10527. result += String.fromCharCode(str.charCodeAt(i) - 12288 + 32);
  10528. } else {
  10529. result += str.charAt(i);
  10530. }
  10531. }
  10532. return result;
  10533. }
  10534. function ToDBC(txtstring) {
  10535. txtstring = utils.html(txtstring);
  10536. var tmp = "";
  10537. var mark = ""; /*用于判断,如果是html尖括里的标记,则不进行全角的转换*/
  10538. for (var i = 0; i < txtstring.length; i++) {
  10539. if (txtstring.charCodeAt(i) == 32) {
  10540. tmp = tmp + String.fromCharCode(12288);
  10541. } else if (txtstring.charCodeAt(i) < 127) {
  10542. tmp = tmp + String.fromCharCode(txtstring.charCodeAt(i) + 65248);
  10543. } else {
  10544. tmp += txtstring.charAt(i);
  10545. }
  10546. }
  10547. return tmp;
  10548. }
  10549. function readLocalOpts() {
  10550. var cookieOpt = me.getPreferences('autotypeset');
  10551. utils.extend(me.options.autotypeset, cookieOpt);
  10552. }
  10553. me.commands['autotypeset'] = {
  10554. execCommand: function () {
  10555. me.removeListener('beforepaste', autotype);
  10556. if (opt.pasteFilter) {
  10557. me.addListener('beforepaste', autotype);
  10558. }
  10559. autotype.call(me)
  10560. }
  10561. };
  10562. };
  10563. // plugins/autosubmit.js
  10564. /**
  10565. * 快捷键提交
  10566. * @file
  10567. * @since 1.2.6.1
  10568. */
  10569. /**
  10570. * 提交表单
  10571. * @command autosubmit
  10572. * @method execCommand
  10573. * @param { String } cmd 命令字符串
  10574. * @example
  10575. * ```javascript
  10576. * editor.execCommand( 'autosubmit' );
  10577. * ```
  10578. */
  10579. UE.plugin.register('autosubmit', function () {
  10580. return {
  10581. shortcutkey: {
  10582. "autosubmit": "ctrl+13" //手动提交
  10583. },
  10584. commands: {
  10585. 'autosubmit': {
  10586. execCommand: function () {
  10587. var me = this,
  10588. form = domUtils.findParentByTagName(me.iframe, "form", false);
  10589. if (form) {
  10590. if (me.fireEvent("beforesubmit") === false) {
  10591. return;
  10592. }
  10593. me.sync();
  10594. form.submit();
  10595. }
  10596. }
  10597. }
  10598. }
  10599. }
  10600. });
  10601. // plugins/background.js
  10602. /**
  10603. * 背景插件,为UEditor提供设置背景功能
  10604. * @file
  10605. * @since 1.2.6.1
  10606. */
  10607. UE.plugin.register('background', function () {
  10608. var me = this,
  10609. cssRuleId = 'editor_background',
  10610. isSetColored,
  10611. reg = new RegExp('body[\\s]*\\{(.+)\\}', 'i');
  10612. function stringToObj(str) {
  10613. var obj = {},
  10614. styles = str.split(';');
  10615. utils.each(styles, function (v) {
  10616. var index = v.indexOf(':'),
  10617. key = utils.trim(v.substr(0, index)).toLowerCase();
  10618. key && (obj[key] = utils.trim(v.substr(index + 1) || ''));
  10619. });
  10620. return obj;
  10621. }
  10622. function setBackground(obj) {
  10623. if (obj) {
  10624. var styles = [];
  10625. for (var name in obj) {
  10626. if (obj.hasOwnProperty(name)) {
  10627. styles.push(name + ":" + obj[name] + '; ');
  10628. }
  10629. }
  10630. utils.cssRule(cssRuleId, styles.length ? ('body{' + styles.join("") + '}') : '', me.document);
  10631. } else {
  10632. utils.cssRule(cssRuleId, '', me.document)
  10633. }
  10634. }
  10635. //重写editor.hasContent方法
  10636. var orgFn = me.hasContents;
  10637. me.hasContents = function () {
  10638. if (me.queryCommandValue('background')) {
  10639. return true
  10640. }
  10641. return orgFn.apply(me, arguments);
  10642. };
  10643. return {
  10644. bindEvents: {
  10645. 'getAllHtml': function (type, headHtml) {
  10646. var body = this.body,
  10647. su = domUtils.getComputedStyle(body, "background-image"),
  10648. url = "";
  10649. if (su.indexOf(me.options.imagePath) > 0) {
  10650. url = su.substring(su.indexOf(me.options.imagePath), su.length - 1).replace(/"|\(|\)/ig, "");
  10651. } else {
  10652. url = su != "none" ? su.replace(/url\("?|"?\)/ig, "") : "";
  10653. }
  10654. var html = '<style type="text/css">body{';
  10655. var bgObj = {
  10656. "background-color": domUtils.getComputedStyle(body, "background-color") || "#ffffff",
  10657. 'background-image': url ? 'url(' + url + ')' : '',
  10658. 'background-repeat': domUtils.getComputedStyle(body, "background-repeat") || "",
  10659. 'background-position': browser.ie ? (domUtils.getComputedStyle(body, "background-position-x") + " " + domUtils.getComputedStyle(body, "background-position-y")) : domUtils.getComputedStyle(body, "background-position"),
  10660. 'height': domUtils.getComputedStyle(body, "height")
  10661. };
  10662. for (var name in bgObj) {
  10663. if (bgObj.hasOwnProperty(name)) {
  10664. html += name + ":" + bgObj[name] + "; ";
  10665. }
  10666. }
  10667. html += '}</style> ';
  10668. headHtml.push(html);
  10669. },
  10670. 'aftersetcontent': function () {
  10671. if (isSetColored == false) setBackground();
  10672. }
  10673. },
  10674. inputRule: function (root) {
  10675. isSetColored = false;
  10676. utils.each(root.getNodesByTagName('p'), function (p) {
  10677. var styles = p.getAttr('data-background');
  10678. if (styles) {
  10679. isSetColored = true;
  10680. setBackground(stringToObj(styles));
  10681. p.parentNode.removeChild(p);
  10682. }
  10683. })
  10684. },
  10685. outputRule: function (root) {
  10686. var me = this,
  10687. styles = (utils.cssRule(cssRuleId, me.document) || '').replace(/[\n\r]+/g, '').match(reg);
  10688. if (styles) {
  10689. root.appendChild(UE.uNode.createElement('<p style="display:none;" data-background="' + utils.trim(styles[1].replace(/"/g, '').replace(/[\s]+/g, ' ')) + '"><br/></p>'));
  10690. }
  10691. },
  10692. commands: {
  10693. 'background': {
  10694. execCommand: function (cmd, obj) {
  10695. setBackground(obj);
  10696. },
  10697. queryCommandValue: function () {
  10698. var me = this,
  10699. styles = (utils.cssRule(cssRuleId, me.document) || '').replace(/[\n\r]+/g, '').match(reg);
  10700. return styles ? stringToObj(styles[1]) : null;
  10701. },
  10702. notNeedUndo: true
  10703. }
  10704. }
  10705. }
  10706. });
  10707. // plugins/image.js
  10708. /**
  10709. * 图片插入、排版插件
  10710. * @file
  10711. * @since 1.2.6.1
  10712. */
  10713. /**
  10714. * 图片对齐方式
  10715. * @command imagefloat
  10716. * @method execCommand
  10717. * @remind 值center为独占一行居中
  10718. * @param { String } cmd 命令字符串
  10719. * @param { String } align 对齐方式,可传left、right、none、center
  10720. * @remaind center表示图片独占一行
  10721. * @example
  10722. * ```javascript
  10723. * editor.execCommand( 'imagefloat', 'center' );
  10724. * ```
  10725. */
  10726. /**
  10727. * 如果选区所在位置是图片区域
  10728. * @command imagefloat
  10729. * @method queryCommandValue
  10730. * @param { String } cmd 命令字符串
  10731. * @return { String } 返回图片对齐方式
  10732. * @example
  10733. * ```javascript
  10734. * editor.queryCommandValue( 'imagefloat' );
  10735. * ```
  10736. */
  10737. UE.commands['imagefloat'] = {
  10738. execCommand: function (cmd, align) {
  10739. var me = this,
  10740. range = me.selection.getRange();
  10741. if (!range.collapsed) {
  10742. var img = range.getClosedNode();
  10743. if (img && img.tagName == 'IMG') {
  10744. switch (align) {
  10745. case 'left':
  10746. case 'right':
  10747. case 'none':
  10748. var pN = img.parentNode,
  10749. tmpNode, pre, next;
  10750. while (dtd.$inline[pN.tagName] || pN.tagName == 'A') {
  10751. pN = pN.parentNode;
  10752. }
  10753. tmpNode = pN;
  10754. if (tmpNode.tagName == 'P' && domUtils.getStyle(tmpNode, 'text-align') == 'center') {
  10755. if (!domUtils.isBody(tmpNode) && domUtils.getChildCount(tmpNode, function (node) {
  10756. return !domUtils.isBr(node) && !domUtils.isWhitespace(node);
  10757. }) == 1) {
  10758. pre = tmpNode.previousSibling;
  10759. next = tmpNode.nextSibling;
  10760. if (pre && next && pre.nodeType == 1 && next.nodeType == 1 && pre.tagName == next.tagName && domUtils.isBlockElm(pre)) {
  10761. pre.appendChild(tmpNode.firstChild);
  10762. while (next.firstChild) {
  10763. pre.appendChild(next.firstChild);
  10764. }
  10765. domUtils.remove(tmpNode);
  10766. domUtils.remove(next);
  10767. } else {
  10768. domUtils.setStyle(tmpNode, 'text-align', '');
  10769. }
  10770. }
  10771. range.selectNode(img).select();
  10772. }
  10773. domUtils.setStyle(img, 'float', align == 'none' ? '' : align);
  10774. if (align == 'none') {
  10775. domUtils.removeAttributes(img, 'align');
  10776. }
  10777. break;
  10778. case 'center':
  10779. if (me.queryCommandValue('imagefloat') != 'center') {
  10780. pN = img.parentNode;
  10781. domUtils.setStyle(img, 'float', '');
  10782. domUtils.removeAttributes(img, 'align');
  10783. tmpNode = img;
  10784. while (pN && domUtils.getChildCount(pN, function (node) {
  10785. return !domUtils.isBr(node) && !domUtils.isWhitespace(node);
  10786. }) == 1 &&
  10787. (dtd.$inline[pN.tagName] || pN.tagName == 'A')) {
  10788. tmpNode = pN;
  10789. pN = pN.parentNode;
  10790. }
  10791. range.setStartBefore(tmpNode).setCursor(false);
  10792. pN = me.document.createElement('div');
  10793. pN.appendChild(tmpNode);
  10794. domUtils.setStyle(tmpNode, 'float', '');
  10795. me.execCommand('insertHtml', '<p id="_img_parent_tmp" style="text-align:center">' + pN.innerHTML + '</p>');
  10796. tmpNode = me.document.getElementById('_img_parent_tmp');
  10797. tmpNode.removeAttribute('id');
  10798. tmpNode = tmpNode.firstChild;
  10799. range.selectNode(tmpNode).select();
  10800. //去掉后边多余的元素
  10801. next = tmpNode.parentNode.nextSibling;
  10802. if (next && domUtils.isEmptyNode(next)) {
  10803. domUtils.remove(next);
  10804. }
  10805. }
  10806. break;
  10807. }
  10808. }
  10809. }
  10810. },
  10811. queryCommandValue: function () {
  10812. var range = this.selection.getRange(),
  10813. startNode, floatStyle;
  10814. if (range.collapsed) {
  10815. return 'none';
  10816. }
  10817. startNode = range.getClosedNode();
  10818. if (startNode && startNode.nodeType == 1 && startNode.tagName == 'IMG') {
  10819. floatStyle = domUtils.getComputedStyle(startNode, 'float') || startNode.getAttribute('align');
  10820. if (floatStyle == 'none') {
  10821. floatStyle = domUtils.getComputedStyle(startNode.parentNode, 'text-align') == 'center' ? 'center' : floatStyle;
  10822. }
  10823. return {
  10824. left: 1,
  10825. right: 1,
  10826. center: 1
  10827. } [floatStyle] ? floatStyle: 'none';
  10828. }
  10829. return 'none';
  10830. },
  10831. queryCommandState: function () {
  10832. var range = this.selection.getRange(),
  10833. startNode;
  10834. if (range.collapsed) return -1;
  10835. startNode = range.getClosedNode();
  10836. if (startNode && startNode.nodeType == 1 && startNode.tagName == 'IMG') {
  10837. return 0;
  10838. }
  10839. return -1;
  10840. }
  10841. };
  10842. /**
  10843. * 插入图片
  10844. * @command insertimage
  10845. * @method execCommand
  10846. * @param { String } cmd 命令字符串
  10847. * @param { Object } opt 属性键值对,这些属性都将被复制到当前插入图片
  10848. * @remind 该命令第二个参数可接受一个图片配置项对象的数组,可以插入多张图片,
  10849. * 此时数组的每一个元素都是一个Object类型的图片属性集合。
  10850. * @example
  10851. * ```javascript
  10852. * editor.execCommand( 'insertimage', {
  10853. * src:'a/b/c.jpg',
  10854. * width:'100',
  10855. * height:'100'
  10856. * } );
  10857. * ```
  10858. * @example
  10859. * ```javascript
  10860. * editor.execCommand( 'insertimage', [{
  10861. * src:'a/b/c.jpg',
  10862. * width:'100',
  10863. * height:'100'
  10864. * },{
  10865. * src:'a/b/d.jpg',
  10866. * width:'100',
  10867. * height:'100'
  10868. * }] );
  10869. * ```
  10870. */
  10871. UE.commands['insertimage'] = {
  10872. execCommand: function (cmd, opt) {
  10873. opt = utils.isArray(opt) ? opt : [opt];
  10874. if (!opt.length) {
  10875. return;
  10876. }
  10877. var me = this,
  10878. range = me.selection.getRange(),
  10879. img = range.getClosedNode();
  10880. if (me.fireEvent('beforeinsertimage', opt) === true) {
  10881. return;
  10882. }
  10883. function unhtmlData(imgCi) {
  10884. utils.each('width,height,border,hspace,vspace'.split(','), function (item) {
  10885. if (imgCi[item]) {
  10886. imgCi[item] = parseInt(imgCi[item], 10) || 0;
  10887. }
  10888. });
  10889. utils.each('src,_src'.split(','), function (item) {
  10890. if (imgCi[item]) {
  10891. imgCi[item] = utils.unhtmlForUrl(imgCi[item]);
  10892. }
  10893. });
  10894. utils.each('title,alt'.split(','), function (item) {
  10895. if (imgCi[item]) {
  10896. imgCi[item] = utils.unhtml(imgCi[item]);
  10897. }
  10898. });
  10899. }
  10900. if (img && /img/i.test(img.tagName) && (img.className != "edui-faked-video" || img.className.indexOf("edui-upload-video") != -1) && !img.getAttribute("word_img")) {
  10901. var first = opt.shift();
  10902. var floatStyle = first['floatStyle'];
  10903. delete first['floatStyle'];
  10904. //// img.style.border = (first.border||0) +"px solid #000";
  10905. //// img.style.margin = (first.margin||0) +"px";
  10906. // img.style.cssText += ';margin:' + (first.margin||0) +"px;" + 'border:' + (first.border||0) +"px solid #000";
  10907. domUtils.setAttributes(img, first);
  10908. me.execCommand('imagefloat', floatStyle);
  10909. if (opt.length > 0) {
  10910. range.setStartAfter(img).setCursor(false, true);
  10911. me.execCommand('insertimage', opt);
  10912. }
  10913. } else {
  10914. var html = [],
  10915. str = '',
  10916. ci;
  10917. ci = opt[0];
  10918. if (opt.length == 1) {
  10919. unhtmlData(ci);
  10920. str = '<img src="' + ci.src + '" ' + (ci._src ? ' _src="' + ci._src + '" ' : '') +
  10921. (ci.width ? 'width="' + ci.width + '" ' : '') +
  10922. (ci.height ? ' height="' + ci.height + '" ' : '') +
  10923. (ci['floatStyle'] == 'left' || ci['floatStyle'] == 'right' ? ' style="float:' + ci['floatStyle'] + ';"' : '') +
  10924. (ci.title && ci.title != "" ? ' title="' + ci.title + '"' : '') +
  10925. (ci.border && ci.border != "0" ? ' border="' + ci.border + '"' : '') +
  10926. (ci.alt && ci.alt != "" ? ' alt="' + ci.alt + '"' : '') +
  10927. (ci.hspace && ci.hspace != "0" ? ' hspace = "' + ci.hspace + '"' : '') +
  10928. (ci.vspace && ci.vspace != "0" ? ' vspace = "' + ci.vspace + '"' : '') + '/>';
  10929. if (ci['floatStyle'] == 'center') {
  10930. str = '<p style="text-align: center">' + str + '</p>';
  10931. }
  10932. html.push(str);
  10933. } else {
  10934. for (var i = 0; ci = opt[i++];) {
  10935. unhtmlData(ci);
  10936. str = '<p ' + (ci['floatStyle'] == 'center' ? 'style="text-align: center" ' : '') + '><img src="' + ci.src + '" ' +
  10937. (ci.width ? 'width="' + ci.width + '" ' : '') + (ci._src ? ' _src="' + ci._src + '" ' : '') +
  10938. (ci.height ? ' height="' + ci.height + '" ' : '') +
  10939. ' style="' + (ci['floatStyle'] && ci['floatStyle'] != 'center' ? 'float:' + ci['floatStyle'] + ';' : '') +
  10940. (ci.border || '') + '" ' +
  10941. (ci.title ? ' title="' + ci.title + '"' : '') + ' /></p>';
  10942. html.push(str);
  10943. }
  10944. }
  10945. me.execCommand('insertHtml', html.join(''));
  10946. }
  10947. me.fireEvent('afterinsertimage', opt)
  10948. }
  10949. };
  10950. // plugins/justify.js
  10951. /**
  10952. * 段落格式
  10953. * @file
  10954. * @since 1.2.6.1
  10955. */
  10956. /**
  10957. * 段落对齐方式
  10958. * @command justify
  10959. * @method execCommand
  10960. * @param { String } cmd 命令字符串
  10961. * @param { String } align 对齐方式:left => 居左,right => 居右,center => 居中,justify => 两端对齐
  10962. * @example
  10963. * ```javascript
  10964. * editor.execCommand( 'justify', 'center' );
  10965. * ```
  10966. */
  10967. /**
  10968. * 如果选区所在位置是段落区域,返回当前段落对齐方式
  10969. * @command justify
  10970. * @method queryCommandValue
  10971. * @param { String } cmd 命令字符串
  10972. * @return { String } 返回段落对齐方式
  10973. * @example
  10974. * ```javascript
  10975. * editor.queryCommandValue( 'justify' );
  10976. * ```
  10977. */
  10978. UE.plugins['justify'] = function () {
  10979. var me = this,
  10980. block = domUtils.isBlockElm,
  10981. defaultValue = {
  10982. left: 1,
  10983. right: 1,
  10984. center: 1,
  10985. justify: 1
  10986. },
  10987. doJustify = function (range, style) {
  10988. var bookmark = range.createBookmark(),
  10989. filterFn = function (node) {
  10990. return node.nodeType == 1 ? node.tagName.toLowerCase() != 'br' && !domUtils.isBookmarkNode(node) : !domUtils.isWhitespace(node);
  10991. };
  10992. range.enlarge(true);
  10993. var bookmark2 = range.createBookmark(),
  10994. current = domUtils.getNextDomNode(bookmark2.start, false, filterFn),
  10995. tmpRange = range.cloneRange(),
  10996. tmpNode;
  10997. while (current && !(domUtils.getPosition(current, bookmark2.end) & domUtils.POSITION_FOLLOWING)) {
  10998. if (current.nodeType == 3 || !block(current)) {
  10999. tmpRange.setStartBefore(current);
  11000. while (current && current !== bookmark2.end && !block(current)) {
  11001. tmpNode = current;
  11002. current = domUtils.getNextDomNode(current, false, null, function (node) {
  11003. return !block(node);
  11004. });
  11005. }
  11006. tmpRange.setEndAfter(tmpNode);
  11007. var common = tmpRange.getCommonAncestor();
  11008. if (!domUtils.isBody(common) && block(common)) {
  11009. domUtils.setStyles(common, utils.isString(style) ? {
  11010. 'text-align': style
  11011. } : style);
  11012. current = common;
  11013. } else {
  11014. var p = range.document.createElement('p');
  11015. domUtils.setStyles(p, utils.isString(style) ? {
  11016. 'text-align': style
  11017. } : style);
  11018. var frag = tmpRange.extractContents();
  11019. p.appendChild(frag);
  11020. tmpRange.insertNode(p);
  11021. current = p;
  11022. }
  11023. current = domUtils.getNextDomNode(current, false, filterFn);
  11024. } else {
  11025. current = domUtils.getNextDomNode(current, true, filterFn);
  11026. }
  11027. }
  11028. return range.moveToBookmark(bookmark2).moveToBookmark(bookmark);
  11029. };
  11030. UE.commands['justify'] = {
  11031. execCommand: function (cmdName, align) {
  11032. var range = this.selection.getRange(),
  11033. txt;
  11034. //闭合时单独处理
  11035. if (range.collapsed) {
  11036. txt = this.document.createTextNode('p');
  11037. range.insertNode(txt);
  11038. }
  11039. doJustify(range, align);
  11040. if (txt) {
  11041. range.setStartBefore(txt).collapse(true);
  11042. domUtils.remove(txt);
  11043. }
  11044. range.select();
  11045. return true;
  11046. },
  11047. queryCommandValue: function () {
  11048. var startNode = this.selection.getStart(),
  11049. value = domUtils.getComputedStyle(startNode, 'text-align');
  11050. return defaultValue[value] ? value : 'left';
  11051. },
  11052. queryCommandState: function () {
  11053. var start = this.selection.getStart(),
  11054. cell = start && domUtils.findParentByTagName(start, ["td", "th", "caption"], true);
  11055. return cell ? -1 : 0;
  11056. }
  11057. };
  11058. };
  11059. // plugins/font.js
  11060. /**
  11061. * 字体颜色,背景色,字号,字体,下划线,删除线
  11062. * @file
  11063. * @since 1.2.6.1
  11064. */
  11065. /**
  11066. * 字体颜色
  11067. * @command forecolor
  11068. * @method execCommand
  11069. * @param { String } cmd 命令字符串
  11070. * @param { String } value 色值(必须十六进制)
  11071. * @example
  11072. * ```javascript
  11073. * editor.execCommand( 'forecolor', '#000' );
  11074. * ```
  11075. */
  11076. /**
  11077. * 返回选区字体颜色
  11078. * @command forecolor
  11079. * @method queryCommandValue
  11080. * @param { String } cmd 命令字符串
  11081. * @return { String } 返回字体颜色
  11082. * @example
  11083. * ```javascript
  11084. * editor.queryCommandValue( 'forecolor' );
  11085. * ```
  11086. */
  11087. /**
  11088. * 字体背景颜色
  11089. * @command backcolor
  11090. * @method execCommand
  11091. * @param { String } cmd 命令字符串
  11092. * @param { String } value 色值(必须十六进制)
  11093. * @example
  11094. * ```javascript
  11095. * editor.execCommand( 'backcolor', '#000' );
  11096. * ```
  11097. */
  11098. /**
  11099. * 返回选区字体颜色
  11100. * @command backcolor
  11101. * @method queryCommandValue
  11102. * @param { String } cmd 命令字符串
  11103. * @return { String } 返回字体背景颜色
  11104. * @example
  11105. * ```javascript
  11106. * editor.queryCommandValue( 'backcolor' );
  11107. * ```
  11108. */
  11109. /**
  11110. * 字体大小
  11111. * @command fontsize
  11112. * @method execCommand
  11113. * @param { String } cmd 命令字符串
  11114. * @param { String } value 字体大小
  11115. * @example
  11116. * ```javascript
  11117. * editor.execCommand( 'fontsize', '14px' );
  11118. * ```
  11119. */
  11120. /**
  11121. * 返回选区字体大小
  11122. * @command fontsize
  11123. * @method queryCommandValue
  11124. * @param { String } cmd 命令字符串
  11125. * @return { String } 返回字体大小
  11126. * @example
  11127. * ```javascript
  11128. * editor.queryCommandValue( 'fontsize' );
  11129. * ```
  11130. */
  11131. /**
  11132. * 字体样式
  11133. * @command fontfamily
  11134. * @method execCommand
  11135. * @param { String } cmd 命令字符串
  11136. * @param { String } value 字体样式
  11137. * @example
  11138. * ```javascript
  11139. * editor.execCommand( 'fontfamily', '微软雅黑' );
  11140. * ```
  11141. */
  11142. /**
  11143. * 返回选区字体样式
  11144. * @command fontfamily
  11145. * @method queryCommandValue
  11146. * @param { String } cmd 命令字符串
  11147. * @return { String } 返回字体样式
  11148. * @example
  11149. * ```javascript
  11150. * editor.queryCommandValue( 'fontfamily' );
  11151. * ```
  11152. */
  11153. /**
  11154. * 字体下划线,与删除线互斥
  11155. * @command underline
  11156. * @method execCommand
  11157. * @param { String } cmd 命令字符串
  11158. * @example
  11159. * ```javascript
  11160. * editor.execCommand( 'underline' );
  11161. * ```
  11162. */
  11163. /**
  11164. * 字体删除线,与下划线互斥
  11165. * @command strikethrough
  11166. * @method execCommand
  11167. * @param { String } cmd 命令字符串
  11168. * @example
  11169. * ```javascript
  11170. * editor.execCommand( 'strikethrough' );
  11171. * ```
  11172. */
  11173. /**
  11174. * 字体边框
  11175. * @command fontborder
  11176. * @method execCommand
  11177. * @param { String } cmd 命令字符串
  11178. * @example
  11179. * ```javascript
  11180. * editor.execCommand( 'fontborder' );
  11181. * ```
  11182. */
  11183. UE.plugins['font'] = function () {
  11184. var me = this,
  11185. fonts = {
  11186. 'forecolor': 'color',
  11187. 'backcolor': 'background-color',
  11188. 'fontsize': 'font-size',
  11189. 'fontfamily': 'font-family',
  11190. 'underline': 'text-decoration',
  11191. 'strikethrough': 'text-decoration',
  11192. 'fontborder': 'border'
  11193. },
  11194. needCmd = {
  11195. 'underline': 1,
  11196. 'strikethrough': 1,
  11197. 'fontborder': 1
  11198. },
  11199. needSetChild = {
  11200. 'forecolor': 'color',
  11201. 'backcolor': 'background-color',
  11202. 'fontsize': 'font-size',
  11203. 'fontfamily': 'font-family'
  11204. };
  11205. me.setOpt({
  11206. 'fontfamily': [{
  11207. name: 'songti',
  11208. val: '宋体,SimSun'
  11209. },
  11210. {
  11211. name: 'yahei',
  11212. val: '微软雅黑,Microsoft YaHei'
  11213. },
  11214. {
  11215. name: 'kaiti',
  11216. val: '楷体,楷体_GB2312, SimKai'
  11217. },
  11218. {
  11219. name: 'heiti',
  11220. val: '黑体, SimHei'
  11221. },
  11222. {
  11223. name: 'lishu',
  11224. val: '隶书, SimLi'
  11225. },
  11226. {
  11227. name: 'andaleMono',
  11228. val: 'andale mono'
  11229. },
  11230. {
  11231. name: 'arial',
  11232. val: 'arial, helvetica,sans-serif'
  11233. },
  11234. {
  11235. name: 'arialBlack',
  11236. val: 'arial black,avant garde'
  11237. },
  11238. {
  11239. name: 'comicSansMs',
  11240. val: 'comic sans ms'
  11241. },
  11242. {
  11243. name: 'impact',
  11244. val: 'impact,chicago'
  11245. },
  11246. {
  11247. name: 'timesNewRoman',
  11248. val: 'times new roman'
  11249. }
  11250. ],
  11251. 'fontsize': [10, 11, 12, 14, 16, 18, 20, 24, 36]
  11252. });
  11253. function mergeWithParent(node) {
  11254. var parent;
  11255. while (parent = node.parentNode) {
  11256. if (parent.tagName == 'SPAN' && domUtils.getChildCount(parent, function (child) {
  11257. return !domUtils.isBookmarkNode(child) && !domUtils.isBr(child)
  11258. }) == 1) {
  11259. parent.style.cssText += node.style.cssText;
  11260. domUtils.remove(node, true);
  11261. node = parent;
  11262. } else {
  11263. break;
  11264. }
  11265. }
  11266. }
  11267. function mergeChild(rng, cmdName, value) {
  11268. if (needSetChild[cmdName]) {
  11269. rng.adjustmentBoundary();
  11270. if (!rng.collapsed && rng.startContainer.nodeType == 1) {
  11271. var start = rng.startContainer.childNodes[rng.startOffset];
  11272. if (start && domUtils.isTagNode(start, 'span')) {
  11273. var bk = rng.createBookmark();
  11274. utils.each(domUtils.getElementsByTagName(start, 'span'), function (span) {
  11275. if (!span.parentNode || domUtils.isBookmarkNode(span)) return;
  11276. if (cmdName == 'backcolor' && domUtils.getComputedStyle(span, 'background-color').toLowerCase() === value) {
  11277. return;
  11278. }
  11279. domUtils.removeStyle(span, needSetChild[cmdName]);
  11280. if (span.style.cssText.replace(/^\s+$/, '').length == 0) {
  11281. domUtils.remove(span, true)
  11282. }
  11283. });
  11284. rng.moveToBookmark(bk)
  11285. }
  11286. }
  11287. }
  11288. }
  11289. function mergesibling(rng, cmdName, value) {
  11290. var collapsed = rng.collapsed,
  11291. bk = rng.createBookmark(),
  11292. common;
  11293. if (collapsed) {
  11294. common = bk.start.parentNode;
  11295. while (dtd.$inline[common.tagName]) {
  11296. common = common.parentNode;
  11297. }
  11298. } else {
  11299. common = domUtils.getCommonAncestor(bk.start, bk.end);
  11300. }
  11301. utils.each(domUtils.getElementsByTagName(common, 'span'), function (span) {
  11302. if (!span.parentNode || domUtils.isBookmarkNode(span)) return;
  11303. if (/\s*border\s*:\s*none;?\s*/i.test(span.style.cssText)) {
  11304. if (/^\s*border\s*:\s*none;?\s*$/.test(span.style.cssText)) {
  11305. domUtils.remove(span, true);
  11306. } else {
  11307. domUtils.removeStyle(span, 'border');
  11308. }
  11309. return
  11310. }
  11311. if (/border/i.test(span.style.cssText) && span.parentNode.tagName == 'SPAN' && /border/i.test(span.parentNode.style.cssText)) {
  11312. span.style.cssText = span.style.cssText.replace(/border[^:]*:[^;]+;?/gi, '');
  11313. }
  11314. if (!(cmdName == 'fontborder' && value == 'none')) {
  11315. var next = span.nextSibling;
  11316. while (next && next.nodeType == 1 && next.tagName == 'SPAN') {
  11317. if (domUtils.isBookmarkNode(next) && cmdName == 'fontborder') {
  11318. span.appendChild(next);
  11319. next = span.nextSibling;
  11320. continue;
  11321. }
  11322. if (next.style.cssText == span.style.cssText) {
  11323. domUtils.moveChild(next, span);
  11324. domUtils.remove(next);
  11325. }
  11326. if (span.nextSibling === next)
  11327. break;
  11328. next = span.nextSibling;
  11329. }
  11330. }
  11331. mergeWithParent(span);
  11332. if (browser.ie && browser.version > 8) {
  11333. //拷贝父亲们的特别的属性,这里只做背景颜色的处理
  11334. var parent = domUtils.findParent(span, function (n) {
  11335. return n.tagName == 'SPAN' && /background-color/.test(n.style.cssText)
  11336. });
  11337. if (parent && !/background-color/.test(span.style.cssText)) {
  11338. span.style.backgroundColor = parent.style.backgroundColor;
  11339. }
  11340. }
  11341. });
  11342. rng.moveToBookmark(bk);
  11343. mergeChild(rng, cmdName, value)
  11344. }
  11345. me.addInputRule(function (root) {
  11346. utils.each(root.getNodesByTagName('u s del font strike'), function (node) {
  11347. if (node.tagName == 'font') {
  11348. var cssStyle = [];
  11349. for (var p in node.attrs) {
  11350. switch (p) {
  11351. case 'size':
  11352. cssStyle.push('font-size:' +
  11353. ({
  11354. '1': '10',
  11355. '2': '12',
  11356. '3': '16',
  11357. '4': '18',
  11358. '5': '24',
  11359. '6': '32',
  11360. '7': '48'
  11361. } [node.attrs[p]] || node.attrs[p]) + 'px');
  11362. break;
  11363. case 'color':
  11364. cssStyle.push('color:' + node.attrs[p]);
  11365. break;
  11366. case 'face':
  11367. cssStyle.push('font-family:' + node.attrs[p]);
  11368. break;
  11369. case 'style':
  11370. cssStyle.push(node.attrs[p]);
  11371. }
  11372. }
  11373. node.attrs = {
  11374. 'style': cssStyle.join(';')
  11375. };
  11376. } else {
  11377. var val = node.tagName == 'u' ? 'underline' : 'line-through';
  11378. node.attrs = {
  11379. 'style': (node.getAttr('style') || '') + 'text-decoration:' + val + ';'
  11380. }
  11381. }
  11382. node.tagName = 'span';
  11383. });
  11384. // utils.each(root.getNodesByTagName('span'), function (node) {
  11385. // var val;
  11386. // if(val = node.getAttr('class')){
  11387. // if(/fontstrikethrough/.test(val)){
  11388. // node.setStyle('text-decoration','line-through');
  11389. // if(node.attrs['class']){
  11390. // node.attrs['class'] = node.attrs['class'].replace(/fontstrikethrough/,'');
  11391. // }else{
  11392. // node.setAttr('class')
  11393. // }
  11394. // }
  11395. // if(/fontborder/.test(val)){
  11396. // node.setStyle('border','1px solid #000');
  11397. // if(node.attrs['class']){
  11398. // node.attrs['class'] = node.attrs['class'].replace(/fontborder/,'');
  11399. // }else{
  11400. // node.setAttr('class')
  11401. // }
  11402. // }
  11403. // }
  11404. // });
  11405. });
  11406. // me.addOutputRule(function(root){
  11407. // utils.each(root.getNodesByTagName('span'), function (node) {
  11408. // var val;
  11409. // if(val = node.getStyle('text-decoration')){
  11410. // if(/line-through/.test(val)){
  11411. // if(node.attrs['class']){
  11412. // node.attrs['class'] += ' fontstrikethrough';
  11413. // }else{
  11414. // node.setAttr('class','fontstrikethrough')
  11415. // }
  11416. // }
  11417. //
  11418. // node.setStyle('text-decoration')
  11419. // }
  11420. // if(val = node.getStyle('border')){
  11421. // if(/1px/.test(val) && /solid/.test(val)){
  11422. // if(node.attrs['class']){
  11423. // node.attrs['class'] += ' fontborder';
  11424. //
  11425. // }else{
  11426. // node.setAttr('class','fontborder')
  11427. // }
  11428. // }
  11429. // node.setStyle('border')
  11430. //
  11431. // }
  11432. // });
  11433. // });
  11434. for (var p in fonts) {
  11435. (function (cmd, style) {
  11436. UE.commands[cmd] = {
  11437. execCommand: function (cmdName, value) {
  11438. value = value || (this.queryCommandState(cmdName) ? 'none' : cmdName == 'underline' ? 'underline' :
  11439. cmdName == 'fontborder' ? '1px solid #000' :
  11440. 'line-through');
  11441. var me = this,
  11442. range = this.selection.getRange(),
  11443. text;
  11444. if (value == 'default') {
  11445. if (range.collapsed) {
  11446. text = me.document.createTextNode('font');
  11447. range.insertNode(text).select();
  11448. }
  11449. me.execCommand('removeFormat', 'span,a', style);
  11450. if (text) {
  11451. range.setStartBefore(text).collapse(true);
  11452. domUtils.remove(text);
  11453. }
  11454. mergesibling(range, cmdName, value);
  11455. range.select()
  11456. } else {
  11457. if (!range.collapsed) {
  11458. if (needCmd[cmd] && me.queryCommandValue(cmd)) {
  11459. me.execCommand('removeFormat', 'span,a', style);
  11460. }
  11461. range = me.selection.getRange();
  11462. range.applyInlineStyle('span', {
  11463. 'style': style + ':' + value
  11464. });
  11465. mergesibling(range, cmdName, value);
  11466. range.select();
  11467. } else {
  11468. var span = domUtils.findParentByTagName(range.startContainer, 'span', true);
  11469. text = me.document.createTextNode('font');
  11470. if (span && !span.children.length && !span[browser.ie ? 'innerText' : 'textContent'].replace(fillCharReg, '').length) {
  11471. //for ie hack when enter
  11472. range.insertNode(text);
  11473. if (needCmd[cmd]) {
  11474. range.selectNode(text).select();
  11475. me.execCommand('removeFormat', 'span,a', style, null);
  11476. span = domUtils.findParentByTagName(text, 'span', true);
  11477. range.setStartBefore(text);
  11478. }
  11479. span && (span.style.cssText += ';' + style + ':' + value);
  11480. range.collapse(true).select();
  11481. } else {
  11482. range.insertNode(text);
  11483. range.selectNode(text).select();
  11484. span = range.document.createElement('span');
  11485. if (needCmd[cmd]) {
  11486. //a标签内的不处理跳过
  11487. if (domUtils.findParentByTagName(text, 'a', true)) {
  11488. range.setStartBefore(text).setCursor();
  11489. domUtils.remove(text);
  11490. return;
  11491. }
  11492. me.execCommand('removeFormat', 'span,a', style);
  11493. }
  11494. span.style.cssText = style + ':' + value;
  11495. text.parentNode.insertBefore(span, text);
  11496. //修复,span套span 但样式不继承的问题
  11497. if (!browser.ie || browser.ie && browser.version == 9) {
  11498. var spanParent = span.parentNode;
  11499. while (!domUtils.isBlockElm(spanParent)) {
  11500. if (spanParent.tagName == 'SPAN') {
  11501. //opera合并style不会加入";"
  11502. span.style.cssText = spanParent.style.cssText + ";" + span.style.cssText;
  11503. }
  11504. spanParent = spanParent.parentNode;
  11505. }
  11506. }
  11507. if (opera) {
  11508. setTimeout(function () {
  11509. range.setStart(span, 0).collapse(true);
  11510. mergesibling(range, cmdName, value);
  11511. range.select();
  11512. });
  11513. } else {
  11514. range.setStart(span, 0).collapse(true);
  11515. mergesibling(range, cmdName, value);
  11516. range.select();
  11517. }
  11518. //trace:981
  11519. //domUtils.mergeToParent(span)
  11520. }
  11521. domUtils.remove(text);
  11522. }
  11523. }
  11524. return true;
  11525. },
  11526. queryCommandValue: function (cmdName) {
  11527. var startNode = this.selection.getStart();
  11528. //trace:946
  11529. if (cmdName == 'underline' || cmdName == 'strikethrough') {
  11530. var tmpNode = startNode,
  11531. value;
  11532. while (tmpNode && !domUtils.isBlockElm(tmpNode) && !domUtils.isBody(tmpNode)) {
  11533. if (tmpNode.nodeType == 1) {
  11534. value = domUtils.getComputedStyle(tmpNode, style);
  11535. if (value != 'none') {
  11536. return value;
  11537. }
  11538. }
  11539. tmpNode = tmpNode.parentNode;
  11540. }
  11541. return 'none';
  11542. }
  11543. if (cmdName == 'fontborder') {
  11544. var tmp = startNode,
  11545. val;
  11546. while (tmp && dtd.$inline[tmp.tagName]) {
  11547. if (val = domUtils.getComputedStyle(tmp, 'border')) {
  11548. if (/1px/.test(val) && /solid/.test(val)) {
  11549. return val;
  11550. }
  11551. }
  11552. tmp = tmp.parentNode;
  11553. }
  11554. return ''
  11555. }
  11556. if (cmdName == 'FontSize') {
  11557. var styleVal = domUtils.getComputedStyle(startNode, style),
  11558. tmp = /^([\d\.]+)(\w+)$/.exec(styleVal);
  11559. if (tmp) {
  11560. return Math.floor(tmp[1]) + tmp[2];
  11561. }
  11562. return styleVal;
  11563. }
  11564. return domUtils.getComputedStyle(startNode, style);
  11565. },
  11566. queryCommandState: function (cmdName) {
  11567. if (!needCmd[cmdName])
  11568. return 0;
  11569. var val = this.queryCommandValue(cmdName);
  11570. if (cmdName == 'fontborder') {
  11571. return /1px/.test(val) && /solid/.test(val)
  11572. } else {
  11573. return cmdName == 'underline' ? /underline/.test(val) : /line\-through/.test(val);
  11574. }
  11575. }
  11576. };
  11577. })(p, fonts[p]);
  11578. }
  11579. };
  11580. // plugins/link.js
  11581. /**
  11582. * 超链接
  11583. * @file
  11584. * @since 1.2.6.1
  11585. */
  11586. /**
  11587. * 插入超链接
  11588. * @command link
  11589. * @method execCommand
  11590. * @param { String } cmd 命令字符串
  11591. * @param { Object } options 设置自定义属性,例如:url、title、target
  11592. * @example
  11593. * ```javascript
  11594. * editor.execCommand( 'link', '{
  11595. * url:'ueditor.baidu.com',
  11596. * title:'ueditor',
  11597. * target:'_blank'
  11598. * }' );
  11599. * ```
  11600. */
  11601. /**
  11602. * 返回当前选中的第一个超链接节点
  11603. * @command link
  11604. * @method queryCommandValue
  11605. * @param { String } cmd 命令字符串
  11606. * @return { Element } 超链接节点
  11607. * @example
  11608. * ```javascript
  11609. * editor.queryCommandValue( 'link' );
  11610. * ```
  11611. */
  11612. /**
  11613. * 取消超链接
  11614. * @command unlink
  11615. * @method execCommand
  11616. * @param { String } cmd 命令字符串
  11617. * @example
  11618. * ```javascript
  11619. * editor.execCommand( 'unlink');
  11620. * ```
  11621. */
  11622. UE.plugins['link'] = function () {
  11623. function optimize(range) {
  11624. var start = range.startContainer,
  11625. end = range.endContainer;
  11626. if (start = domUtils.findParentByTagName(start, 'a', true)) {
  11627. range.setStartBefore(start);
  11628. }
  11629. if (end = domUtils.findParentByTagName(end, 'a', true)) {
  11630. range.setEndAfter(end);
  11631. }
  11632. }
  11633. UE.commands['unlink'] = {
  11634. execCommand: function () {
  11635. var range = this.selection.getRange(),
  11636. bookmark;
  11637. if (range.collapsed && !domUtils.findParentByTagName(range.startContainer, 'a', true)) {
  11638. return;
  11639. }
  11640. bookmark = range.createBookmark();
  11641. optimize(range);
  11642. range.removeInlineStyle('a').moveToBookmark(bookmark).select();
  11643. },
  11644. queryCommandState: function () {
  11645. return !this.highlight && this.queryCommandValue('link') ? 0 : -1;
  11646. }
  11647. };
  11648. function doLink(range, opt, me) {
  11649. var rngClone = range.cloneRange(),
  11650. link = me.queryCommandValue('link');
  11651. optimize(range = range.adjustmentBoundary());
  11652. var start = range.startContainer;
  11653. if (start.nodeType == 1 && link) {
  11654. start = start.childNodes[range.startOffset];
  11655. if (start && start.nodeType == 1 && start.tagName == 'A' && /^(?:https?|ftp|file)\s*:\s*\/\//.test(start[browser.ie ? 'innerText' : 'textContent'])) {
  11656. start[browser.ie ? 'innerText' : 'textContent'] = utils.html(opt.textValue || opt.href);
  11657. }
  11658. }
  11659. if (!rngClone.collapsed || link) {
  11660. range.removeInlineStyle('a');
  11661. rngClone = range.cloneRange();
  11662. }
  11663. if (rngClone.collapsed) {
  11664. var a = range.document.createElement('a'),
  11665. text = '';
  11666. if (opt.textValue) {
  11667. text = utils.html(opt.textValue);
  11668. delete opt.textValue;
  11669. } else {
  11670. text = utils.html(opt.href);
  11671. }
  11672. domUtils.setAttributes(a, opt);
  11673. start = domUtils.findParentByTagName(rngClone.startContainer, 'a', true);
  11674. if (start && domUtils.isInNodeEndBoundary(rngClone, start)) {
  11675. range.setStartAfter(start).collapse(true);
  11676. }
  11677. a[browser.ie ? 'innerText' : 'textContent'] = text;
  11678. range.insertNode(a).selectNode(a);
  11679. } else {
  11680. range.applyInlineStyle('a', opt);
  11681. }
  11682. }
  11683. UE.commands['link'] = {
  11684. execCommand: function (cmdName, opt) {
  11685. var range;
  11686. opt._href && (opt._href = utils.unhtml(opt._href, /[<">]/g));
  11687. opt.href && (opt.href = utils.unhtml(opt.href, /[<">]/g));
  11688. opt.textValue && (opt.textValue = utils.unhtml(opt.textValue, /[<">]/g));
  11689. doLink(range = this.selection.getRange(), opt, this);
  11690. //闭合都不加占位符,如果加了会在a后边多个占位符节点,导致a是图片背景组成的列表,出现空白问题
  11691. range.collapse().select(true);
  11692. },
  11693. queryCommandValue: function () {
  11694. var range = this.selection.getRange(),
  11695. node;
  11696. if (range.collapsed) {
  11697. // node = this.selection.getStart();
  11698. //在ie下getstart()取值偏上了
  11699. node = range.startContainer;
  11700. node = node.nodeType == 1 ? node : node.parentNode;
  11701. if (node && (node = domUtils.findParentByTagName(node, 'a', true)) && !domUtils.isInNodeEndBoundary(range, node)) {
  11702. return node;
  11703. }
  11704. } else {
  11705. //trace:1111 如果是<p><a>xx</a></p> startContainer是p就会找不到a
  11706. range.shrinkBoundary();
  11707. var start = range.startContainer.nodeType == 3 || !range.startContainer.childNodes[range.startOffset] ? range.startContainer : range.startContainer.childNodes[range.startOffset],
  11708. end = range.endContainer.nodeType == 3 || range.endOffset == 0 ? range.endContainer : range.endContainer.childNodes[range.endOffset - 1],
  11709. common = range.getCommonAncestor();
  11710. node = domUtils.findParentByTagName(common, 'a', true);
  11711. if (!node && common.nodeType == 1) {
  11712. var as = common.getElementsByTagName('a'),
  11713. ps, pe;
  11714. for (var i = 0, ci; ci = as[i++];) {
  11715. ps = domUtils.getPosition(ci, start), pe = domUtils.getPosition(ci, end);
  11716. if ((ps & domUtils.POSITION_FOLLOWING || ps & domUtils.POSITION_CONTAINS) &&
  11717. (pe & domUtils.POSITION_PRECEDING || pe & domUtils.POSITION_CONTAINS)
  11718. ) {
  11719. node = ci;
  11720. break;
  11721. }
  11722. }
  11723. }
  11724. return node;
  11725. }
  11726. },
  11727. queryCommandState: function () {
  11728. //判断如果是视频的话连接不可用
  11729. //fix 853
  11730. var img = this.selection.getRange().getClosedNode(),
  11731. flag = img && (img.className == "edui-faked-video" || img.className.indexOf("edui-upload-video") != -1);
  11732. return flag ? -1 : 0;
  11733. }
  11734. };
  11735. };
  11736. // plugins/iframe.js
  11737. ///import core
  11738. ///import plugins\inserthtml.js
  11739. ///commands 插入框架
  11740. ///commandsName InsertFrame
  11741. ///commandsTitle 插入Iframe
  11742. ///commandsDialog dialogs\insertframe
  11743. UE.plugins['insertframe'] = function () {
  11744. var me = this;
  11745. function deleteIframe() {
  11746. me._iframe && delete me._iframe;
  11747. }
  11748. me.addListener("selectionchange", function () {
  11749. deleteIframe();
  11750. });
  11751. };
  11752. // plugins/scrawl.js
  11753. ///import core
  11754. ///commands 涂鸦
  11755. ///commandsName Scrawl
  11756. ///commandsTitle 涂鸦
  11757. ///commandsDialog dialogs\scrawl
  11758. UE.commands['scrawl'] = {
  11759. queryCommandState: function () {
  11760. return (browser.ie && browser.version <= 8) ? -1 : 0;
  11761. }
  11762. };
  11763. // plugins/removeformat.js
  11764. /**
  11765. * 清除格式
  11766. * @file
  11767. * @since 1.2.6.1
  11768. */
  11769. /**
  11770. * 清除文字样式
  11771. * @command removeformat
  11772. * @method execCommand
  11773. * @param { String } cmd 命令字符串
  11774. * @param {String} tags 以逗号隔开的标签。如:strong
  11775. * @param {String} style 样式如:color
  11776. * @param {String} attrs 属性如:width
  11777. * @example
  11778. * ```javascript
  11779. * editor.execCommand( 'removeformat', 'strong','color','width' );
  11780. * ```
  11781. */
  11782. UE.plugins['removeformat'] = function () {
  11783. var me = this;
  11784. me.setOpt({
  11785. 'removeFormatTags': 'b,big,code,del,dfn,em,font,i,ins,kbd,q,samp,small,span,strike,strong,sub,sup,tt,u,var',
  11786. 'removeFormatAttributes': 'class,style,lang,width,height,align,hspace,valign'
  11787. });
  11788. me.commands['removeformat'] = {
  11789. execCommand: function (cmdName, tags, style, attrs, notIncludeA) {
  11790. var tagReg = new RegExp('^(?:' + (tags || this.options.removeFormatTags).replace(/,/g, '|') + ')$', 'i'),
  11791. removeFormatAttributes = style ? [] : (attrs || this.options.removeFormatAttributes).split(','),
  11792. range = new dom.Range(this.document),
  11793. bookmark, node, parent,
  11794. filter = function (node) {
  11795. return node.nodeType == 1;
  11796. };
  11797. function isRedundantSpan(node) {
  11798. if (node.nodeType == 3 || node.tagName.toLowerCase() != 'span') {
  11799. return 0;
  11800. }
  11801. if (browser.ie) {
  11802. //ie 下判断实效,所以只能简单用style来判断
  11803. //return node.style.cssText == '' ? 1 : 0;
  11804. var attrs = node.attributes;
  11805. if (attrs.length) {
  11806. for (var i = 0, l = attrs.length; i < l; i++) {
  11807. if (attrs[i].specified) {
  11808. return 0;
  11809. }
  11810. }
  11811. return 1;
  11812. }
  11813. }
  11814. return !node.attributes.length;
  11815. }
  11816. function doRemove(range) {
  11817. var bookmark1 = range.createBookmark();
  11818. if (range.collapsed) {
  11819. range.enlarge(true);
  11820. }
  11821. //不能把a标签切了
  11822. if (!notIncludeA) {
  11823. var aNode = domUtils.findParentByTagName(range.startContainer, 'a', true);
  11824. if (aNode) {
  11825. range.setStartBefore(aNode);
  11826. }
  11827. aNode = domUtils.findParentByTagName(range.endContainer, 'a', true);
  11828. if (aNode) {
  11829. range.setEndAfter(aNode);
  11830. }
  11831. }
  11832. bookmark = range.createBookmark();
  11833. node = bookmark.start;
  11834. //切开始
  11835. while ((parent = node.parentNode) && !domUtils.isBlockElm(parent)) {
  11836. domUtils.breakParent(node, parent);
  11837. domUtils.clearEmptySibling(node);
  11838. }
  11839. if (bookmark.end) {
  11840. //切结束
  11841. node = bookmark.end;
  11842. while ((parent = node.parentNode) && !domUtils.isBlockElm(parent)) {
  11843. domUtils.breakParent(node, parent);
  11844. domUtils.clearEmptySibling(node);
  11845. }
  11846. //开始去除样式
  11847. var current = domUtils.getNextDomNode(bookmark.start, false, filter),
  11848. next;
  11849. while (current) {
  11850. if (current == bookmark.end) {
  11851. break;
  11852. }
  11853. next = domUtils.getNextDomNode(current, true, filter);
  11854. if (!dtd.$empty[current.tagName.toLowerCase()] && !domUtils.isBookmarkNode(current)) {
  11855. if (tagReg.test(current.tagName)) {
  11856. if (style) {
  11857. domUtils.removeStyle(current, style);
  11858. if (isRedundantSpan(current) && style != 'text-decoration') {
  11859. domUtils.remove(current, true);
  11860. }
  11861. } else {
  11862. domUtils.remove(current, true);
  11863. }
  11864. } else {
  11865. //trace:939 不能把list上的样式去掉
  11866. if (!dtd.$tableContent[current.tagName] && !dtd.$list[current.tagName]) {
  11867. domUtils.removeAttributes(current, removeFormatAttributes);
  11868. if (isRedundantSpan(current)) {
  11869. domUtils.remove(current, true);
  11870. }
  11871. }
  11872. }
  11873. }
  11874. current = next;
  11875. }
  11876. }
  11877. //trace:1035
  11878. //trace:1096 不能把td上的样式去掉,比如边框
  11879. var pN = bookmark.start.parentNode;
  11880. if (domUtils.isBlockElm(pN) && !dtd.$tableContent[pN.tagName] && !dtd.$list[pN.tagName]) {
  11881. domUtils.removeAttributes(pN, removeFormatAttributes);
  11882. }
  11883. pN = bookmark.end.parentNode;
  11884. if (bookmark.end && domUtils.isBlockElm(pN) && !dtd.$tableContent[pN.tagName] && !dtd.$list[pN.tagName]) {
  11885. domUtils.removeAttributes(pN, removeFormatAttributes);
  11886. }
  11887. range.moveToBookmark(bookmark).moveToBookmark(bookmark1);
  11888. //清除冗余的代码 <b><bookmark></b>
  11889. var node = range.startContainer,
  11890. tmp,
  11891. collapsed = range.collapsed;
  11892. while (node.nodeType == 1 && domUtils.isEmptyNode(node) && dtd.$removeEmpty[node.tagName]) {
  11893. tmp = node.parentNode;
  11894. range.setStartBefore(node);
  11895. //trace:937
  11896. //更新结束边界
  11897. if (range.startContainer === range.endContainer) {
  11898. range.endOffset--;
  11899. }
  11900. domUtils.remove(node);
  11901. node = tmp;
  11902. }
  11903. if (!collapsed) {
  11904. node = range.endContainer;
  11905. while (node.nodeType == 1 && domUtils.isEmptyNode(node) && dtd.$removeEmpty[node.tagName]) {
  11906. tmp = node.parentNode;
  11907. range.setEndBefore(node);
  11908. domUtils.remove(node);
  11909. node = tmp;
  11910. }
  11911. }
  11912. }
  11913. range = this.selection.getRange();
  11914. doRemove(range);
  11915. range.select();
  11916. }
  11917. };
  11918. };
  11919. // plugins/blockquote.js
  11920. /**
  11921. * 添加引用
  11922. * @file
  11923. * @since 1.2.6.1
  11924. */
  11925. /**
  11926. * 添加引用
  11927. * @command blockquote
  11928. * @method execCommand
  11929. * @param { String } cmd 命令字符串
  11930. * @example
  11931. * ```javascript
  11932. * editor.execCommand( 'blockquote' );
  11933. * ```
  11934. */
  11935. /**
  11936. * 添加引用
  11937. * @command blockquote
  11938. * @method execCommand
  11939. * @param { String } cmd 命令字符串
  11940. * @param { Object } attrs 节点属性
  11941. * @example
  11942. * ```javascript
  11943. * editor.execCommand( 'blockquote',{
  11944. * style: "color: red;"
  11945. * } );
  11946. * ```
  11947. */
  11948. UE.plugins['blockquote'] = function () {
  11949. var me = this;
  11950. function getObj(editor) {
  11951. return domUtils.filterNodeList(editor.selection.getStartElementPath(), 'blockquote');
  11952. }
  11953. me.commands['blockquote'] = {
  11954. execCommand: function (cmdName, attrs) {
  11955. var range = this.selection.getRange(),
  11956. obj = getObj(this),
  11957. blockquote = dtd.blockquote,
  11958. bookmark = range.createBookmark();
  11959. if (obj) {
  11960. var start = range.startContainer,
  11961. startBlock = domUtils.isBlockElm(start) ? start : domUtils.findParent(start, function (node) {
  11962. return domUtils.isBlockElm(node)
  11963. }),
  11964. end = range.endContainer,
  11965. endBlock = domUtils.isBlockElm(end) ? end : domUtils.findParent(end, function (node) {
  11966. return domUtils.isBlockElm(node)
  11967. });
  11968. //处理一下li
  11969. startBlock = domUtils.findParentByTagName(startBlock, 'li', true) || startBlock;
  11970. endBlock = domUtils.findParentByTagName(endBlock, 'li', true) || endBlock;
  11971. if (startBlock.tagName == 'LI' || startBlock.tagName == 'TD' || startBlock === obj || domUtils.isBody(startBlock)) {
  11972. domUtils.remove(obj, true);
  11973. } else {
  11974. domUtils.breakParent(startBlock, obj);
  11975. }
  11976. if (startBlock !== endBlock) {
  11977. obj = domUtils.findParentByTagName(endBlock, 'blockquote');
  11978. if (obj) {
  11979. if (endBlock.tagName == 'LI' || endBlock.tagName == 'TD' || domUtils.isBody(endBlock)) {
  11980. obj.parentNode && domUtils.remove(obj, true);
  11981. } else {
  11982. domUtils.breakParent(endBlock, obj);
  11983. }
  11984. }
  11985. }
  11986. var blockquotes = domUtils.getElementsByTagName(this.document, 'blockquote');
  11987. for (var i = 0, bi; bi = blockquotes[i++];) {
  11988. if (!bi.childNodes.length) {
  11989. domUtils.remove(bi);
  11990. } else if (domUtils.getPosition(bi, startBlock) & domUtils.POSITION_FOLLOWING && domUtils.getPosition(bi, endBlock) & domUtils.POSITION_PRECEDING) {
  11991. domUtils.remove(bi, true);
  11992. }
  11993. }
  11994. } else {
  11995. var tmpRange = range.cloneRange(),
  11996. node = tmpRange.startContainer.nodeType == 1 ? tmpRange.startContainer : tmpRange.startContainer.parentNode,
  11997. preNode = node,
  11998. doEnd = 1;
  11999. //调整开始
  12000. while (1) {
  12001. if (domUtils.isBody(node)) {
  12002. if (preNode !== node) {
  12003. if (range.collapsed) {
  12004. tmpRange.selectNode(preNode);
  12005. doEnd = 0;
  12006. } else {
  12007. tmpRange.setStartBefore(preNode);
  12008. }
  12009. } else {
  12010. tmpRange.setStart(node, 0);
  12011. }
  12012. break;
  12013. }
  12014. if (!blockquote[node.tagName]) {
  12015. if (range.collapsed) {
  12016. tmpRange.selectNode(preNode);
  12017. } else {
  12018. tmpRange.setStartBefore(preNode);
  12019. }
  12020. break;
  12021. }
  12022. preNode = node;
  12023. node = node.parentNode;
  12024. }
  12025. //调整结束
  12026. if (doEnd) {
  12027. preNode = node = node = tmpRange.endContainer.nodeType == 1 ? tmpRange.endContainer : tmpRange.endContainer.parentNode;
  12028. while (1) {
  12029. if (domUtils.isBody(node)) {
  12030. if (preNode !== node) {
  12031. tmpRange.setEndAfter(preNode);
  12032. } else {
  12033. tmpRange.setEnd(node, node.childNodes.length);
  12034. }
  12035. break;
  12036. }
  12037. if (!blockquote[node.tagName]) {
  12038. tmpRange.setEndAfter(preNode);
  12039. break;
  12040. }
  12041. preNode = node;
  12042. node = node.parentNode;
  12043. }
  12044. }
  12045. node = range.document.createElement('blockquote');
  12046. domUtils.setAttributes(node, attrs);
  12047. node.appendChild(tmpRange.extractContents());
  12048. tmpRange.insertNode(node);
  12049. //去除重复的
  12050. var childs = domUtils.getElementsByTagName(node, 'blockquote');
  12051. for (var i = 0, ci; ci = childs[i++];) {
  12052. if (ci.parentNode) {
  12053. domUtils.remove(ci, true);
  12054. }
  12055. }
  12056. }
  12057. range.moveToBookmark(bookmark).select();
  12058. },
  12059. queryCommandState: function () {
  12060. return getObj(this) ? 1 : 0;
  12061. }
  12062. };
  12063. };
  12064. // plugins/convertcase.js
  12065. /**
  12066. * 大小写转换
  12067. * @file
  12068. * @since 1.2.6.1
  12069. */
  12070. /**
  12071. * 把选区内文本变大写,与“tolowercase”命令互斥
  12072. * @command touppercase
  12073. * @method execCommand
  12074. * @param { String } cmd 命令字符串
  12075. * @example
  12076. * ```javascript
  12077. * editor.execCommand( 'touppercase' );
  12078. * ```
  12079. */
  12080. /**
  12081. * 把选区内文本变小写,与“touppercase”命令互斥
  12082. * @command tolowercase
  12083. * @method execCommand
  12084. * @param { String } cmd 命令字符串
  12085. * @example
  12086. * ```javascript
  12087. * editor.execCommand( 'tolowercase' );
  12088. * ```
  12089. */
  12090. UE.commands['touppercase'] =
  12091. UE.commands['tolowercase'] = {
  12092. execCommand: function (cmd) {
  12093. var me = this;
  12094. var rng = me.selection.getRange();
  12095. if (rng.collapsed) {
  12096. return rng;
  12097. }
  12098. var bk = rng.createBookmark(),
  12099. bkEnd = bk.end,
  12100. filterFn = function (node) {
  12101. return !domUtils.isBr(node) && !domUtils.isWhitespace(node);
  12102. },
  12103. curNode = domUtils.getNextDomNode(bk.start, false, filterFn);
  12104. while (curNode && (domUtils.getPosition(curNode, bkEnd) & domUtils.POSITION_PRECEDING)) {
  12105. if (curNode.nodeType == 3) {
  12106. curNode.nodeValue = curNode.nodeValue[cmd == 'touppercase' ? 'toUpperCase' : 'toLowerCase']();
  12107. }
  12108. curNode = domUtils.getNextDomNode(curNode, true, filterFn);
  12109. if (curNode === bkEnd) {
  12110. break;
  12111. }
  12112. }
  12113. rng.moveToBookmark(bk).select();
  12114. }
  12115. };
  12116. // plugins/indent.js
  12117. /**
  12118. * 首行缩进
  12119. * @file
  12120. * @since 1.2.6.1
  12121. */
  12122. /**
  12123. * 缩进
  12124. * @command indent
  12125. * @method execCommand
  12126. * @param { String } cmd 命令字符串
  12127. * @example
  12128. * ```javascript
  12129. * editor.execCommand( 'indent' );
  12130. * ```
  12131. */
  12132. UE.commands['indent'] = {
  12133. execCommand: function () {
  12134. var me = this,
  12135. value = me.queryCommandState("indent") ? "0em" : (me.options.indentValue || '2em');
  12136. me.execCommand('Paragraph', 'p', {
  12137. style: 'text-indent:' + value
  12138. });
  12139. },
  12140. queryCommandState: function () {
  12141. var pN = domUtils.filterNodeList(this.selection.getStartElementPath(), 'p h1 h2 h3 h4 h5 h6');
  12142. return pN && pN.style.textIndent && parseInt(pN.style.textIndent) ? 1 : 0;
  12143. }
  12144. };
  12145. // plugins/print.js
  12146. /**
  12147. * 打印
  12148. * @file
  12149. * @since 1.2.6.1
  12150. */
  12151. /**
  12152. * 打印
  12153. * @command print
  12154. * @method execCommand
  12155. * @param { String } cmd 命令字符串
  12156. * @example
  12157. * ```javascript
  12158. * editor.execCommand( 'print' );
  12159. * ```
  12160. */
  12161. UE.commands['print'] = {
  12162. execCommand: function () {
  12163. this.window.print();
  12164. },
  12165. notNeedUndo: 1
  12166. };
  12167. // plugins/preview.js
  12168. /**
  12169. * 预览
  12170. * @file
  12171. * @since 1.2.6.1
  12172. */
  12173. /**
  12174. * 预览
  12175. * @command preview
  12176. * @method execCommand
  12177. * @param { String } cmd 命令字符串
  12178. * @example
  12179. * ```javascript
  12180. * editor.execCommand( 'preview' );
  12181. * ```
  12182. */
  12183. UE.commands['preview'] = {
  12184. execCommand: function () {
  12185. var w = window.open('', '_blank', ''),
  12186. d = w.document;
  12187. d.open();
  12188. d.write('<!DOCTYPE html><html><head><meta charset="utf-8"/><script src="' + this.options.UEDITOR_HOME_URL + 'ueditor.parse.js"></script><script>' +
  12189. "setTimeout(function(){uParse('div',{rootPath: '" + this.options.UEDITOR_HOME_URL + "'})},300)" +
  12190. '</script></head><body><div>' + this.getContent(null, null, true) + '</div></body></html>');
  12191. d.close();
  12192. },
  12193. notNeedUndo: 1
  12194. };
  12195. // plugins/selectall.js
  12196. /**
  12197. * 全选
  12198. * @file
  12199. * @since 1.2.6.1
  12200. */
  12201. /**
  12202. * 选中所有内容
  12203. * @command selectall
  12204. * @method execCommand
  12205. * @param { String } cmd 命令字符串
  12206. * @example
  12207. * ```javascript
  12208. * editor.execCommand( 'selectall' );
  12209. * ```
  12210. */
  12211. UE.plugins['selectall'] = function () {
  12212. var me = this;
  12213. me.commands['selectall'] = {
  12214. execCommand: function () {
  12215. //去掉了原生的selectAll,因为会出现报错和当内容为空时,不能出现闭合状态的光标
  12216. var me = this,
  12217. body = me.body,
  12218. range = me.selection.getRange();
  12219. range.selectNodeContents(body);
  12220. if (domUtils.isEmptyBlock(body)) {
  12221. //opera不能自动合并到元素的里边,要手动处理一下
  12222. if (browser.opera && body.firstChild && body.firstChild.nodeType == 1) {
  12223. range.setStartAtFirst(body.firstChild);
  12224. }
  12225. range.collapse(true);
  12226. }
  12227. range.select(true);
  12228. },
  12229. notNeedUndo: 1
  12230. };
  12231. //快捷键
  12232. me.addshortcutkey({
  12233. "selectAll": "ctrl+65"
  12234. });
  12235. };
  12236. // plugins/paragraph.js
  12237. /**
  12238. * 段落样式
  12239. * @file
  12240. * @since 1.2.6.1
  12241. */
  12242. /**
  12243. * 段落格式
  12244. * @command paragraph
  12245. * @method execCommand
  12246. * @param { String } cmd 命令字符串
  12247. * @param {String} style 标签值为:'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'
  12248. * @param {Object} attrs 标签的属性
  12249. * @example
  12250. * ```javascript
  12251. * editor.execCommand( 'Paragraph','h1','{
  12252. * class:'test'
  12253. * }' );
  12254. * ```
  12255. */
  12256. /**
  12257. * 返回选区内节点标签名
  12258. * @command paragraph
  12259. * @method queryCommandValue
  12260. * @param { String } cmd 命令字符串
  12261. * @return { String } 节点标签名
  12262. * @example
  12263. * ```javascript
  12264. * editor.queryCommandValue( 'Paragraph' );
  12265. * ```
  12266. */
  12267. UE.plugins['paragraph'] = function () {
  12268. var me = this,
  12269. block = domUtils.isBlockElm,
  12270. notExchange = ['TD', 'LI', 'PRE'],
  12271. doParagraph = function (range, style, attrs, sourceCmdName) {
  12272. var bookmark = range.createBookmark(),
  12273. filterFn = function (node) {
  12274. return node.nodeType == 1 ? node.tagName.toLowerCase() != 'br' && !domUtils.isBookmarkNode(node) : !domUtils.isWhitespace(node);
  12275. },
  12276. para;
  12277. range.enlarge(true);
  12278. var bookmark2 = range.createBookmark(),
  12279. current = domUtils.getNextDomNode(bookmark2.start, false, filterFn),
  12280. tmpRange = range.cloneRange(),
  12281. tmpNode;
  12282. while (current && !(domUtils.getPosition(current, bookmark2.end) & domUtils.POSITION_FOLLOWING)) {
  12283. if (current.nodeType == 3 || !block(current)) {
  12284. tmpRange.setStartBefore(current);
  12285. while (current && current !== bookmark2.end && !block(current)) {
  12286. tmpNode = current;
  12287. current = domUtils.getNextDomNode(current, false, null, function (node) {
  12288. return !block(node);
  12289. });
  12290. }
  12291. tmpRange.setEndAfter(tmpNode);
  12292. para = range.document.createElement(style);
  12293. if (attrs) {
  12294. domUtils.setAttributes(para, attrs);
  12295. if (sourceCmdName && sourceCmdName == 'customstyle' && attrs.style) {
  12296. para.style.cssText = attrs.style;
  12297. }
  12298. }
  12299. para.appendChild(tmpRange.extractContents());
  12300. //需要内容占位
  12301. if (domUtils.isEmptyNode(para)) {
  12302. domUtils.fillChar(range.document, para);
  12303. }
  12304. tmpRange.insertNode(para);
  12305. var parent = para.parentNode;
  12306. //如果para上一级是一个block元素且不是body,td就删除它
  12307. if (block(parent) && !domUtils.isBody(para.parentNode) && utils.indexOf(notExchange, parent.tagName) == -1) {
  12308. //存储dir,style
  12309. if (!(sourceCmdName && sourceCmdName == 'customstyle')) {
  12310. parent.getAttribute('dir') && para.setAttribute('dir', parent.getAttribute('dir'));
  12311. //trace:1070
  12312. parent.style.cssText && (para.style.cssText = parent.style.cssText + ';' + para.style.cssText);
  12313. //trace:1030
  12314. parent.style.textAlign && !para.style.textAlign && (para.style.textAlign = parent.style.textAlign);
  12315. parent.style.textIndent && !para.style.textIndent && (para.style.textIndent = parent.style.textIndent);
  12316. parent.style.padding && !para.style.padding && (para.style.padding = parent.style.padding);
  12317. }
  12318. //trace:1706 选择的就是h1-6要删除
  12319. if (attrs && /h\d/i.test(parent.tagName) && !/h\d/i.test(para.tagName)) {
  12320. domUtils.setAttributes(parent, attrs);
  12321. if (sourceCmdName && sourceCmdName == 'customstyle' && attrs.style) {
  12322. parent.style.cssText = attrs.style;
  12323. }
  12324. domUtils.remove(para, true);
  12325. para = parent;
  12326. } else {
  12327. domUtils.remove(para.parentNode, true);
  12328. }
  12329. }
  12330. if (utils.indexOf(notExchange, parent.tagName) != -1) {
  12331. current = parent;
  12332. } else {
  12333. current = para;
  12334. }
  12335. current = domUtils.getNextDomNode(current, false, filterFn);
  12336. } else {
  12337. current = domUtils.getNextDomNode(current, true, filterFn);
  12338. }
  12339. }
  12340. return range.moveToBookmark(bookmark2).moveToBookmark(bookmark);
  12341. };
  12342. me.setOpt('paragraph', {
  12343. 'p': '',
  12344. 'h1': '',
  12345. 'h2': '',
  12346. 'h3': '',
  12347. 'h4': '',
  12348. 'h5': '',
  12349. 'h6': ''
  12350. });
  12351. me.commands['paragraph'] = {
  12352. execCommand: function (cmdName, style, attrs, sourceCmdName) {
  12353. var range = this.selection.getRange();
  12354. //闭合时单独处理
  12355. if (range.collapsed) {
  12356. var txt = this.document.createTextNode('p');
  12357. range.insertNode(txt);
  12358. //去掉冗余的fillchar
  12359. if (browser.ie) {
  12360. var node = txt.previousSibling;
  12361. if (node && domUtils.isWhitespace(node)) {
  12362. domUtils.remove(node);
  12363. }
  12364. node = txt.nextSibling;
  12365. if (node && domUtils.isWhitespace(node)) {
  12366. domUtils.remove(node);
  12367. }
  12368. }
  12369. }
  12370. range = doParagraph(range, style, attrs, sourceCmdName);
  12371. if (txt) {
  12372. range.setStartBefore(txt).collapse(true);
  12373. pN = txt.parentNode;
  12374. domUtils.remove(txt);
  12375. if (domUtils.isBlockElm(pN) && domUtils.isEmptyNode(pN)) {
  12376. domUtils.fillNode(this.document, pN);
  12377. }
  12378. }
  12379. if (browser.gecko && range.collapsed && range.startContainer.nodeType == 1) {
  12380. var child = range.startContainer.childNodes[range.startOffset];
  12381. if (child && child.nodeType == 1 && child.tagName.toLowerCase() == style) {
  12382. range.setStart(child, 0).collapse(true);
  12383. }
  12384. }
  12385. //trace:1097 原来有true,原因忘了,但去了就不能清除多余的占位符了
  12386. range.select();
  12387. return true;
  12388. },
  12389. queryCommandValue: function () {
  12390. var node = domUtils.filterNodeList(this.selection.getStartElementPath(), 'p h1 h2 h3 h4 h5 h6');
  12391. return node ? node.tagName.toLowerCase() : '';
  12392. }
  12393. };
  12394. };
  12395. // plugins/directionality.js
  12396. /**
  12397. * 设置文字输入的方向的插件
  12398. * @file
  12399. * @since 1.2.6.1
  12400. */
  12401. (function () {
  12402. var block = domUtils.isBlockElm,
  12403. getObj = function (editor) {
  12404. // var startNode = editor.selection.getStart(),
  12405. // parents;
  12406. // if ( startNode ) {
  12407. // //查找所有的是block的父亲节点
  12408. // parents = domUtils.findParents( startNode, true, block, true );
  12409. // for ( var i = 0,ci; ci = parents[i++]; ) {
  12410. // if ( ci.getAttribute( 'dir' ) ) {
  12411. // return ci;
  12412. // }
  12413. // }
  12414. // }
  12415. return domUtils.filterNodeList(editor.selection.getStartElementPath(), function (n) {
  12416. return n && n.nodeType == 1 && n.getAttribute('dir')
  12417. });
  12418. },
  12419. doDirectionality = function (range, editor, forward) {
  12420. var bookmark,
  12421. filterFn = function (node) {
  12422. return node.nodeType == 1 ? !domUtils.isBookmarkNode(node) : !domUtils.isWhitespace(node);
  12423. },
  12424. obj = getObj(editor);
  12425. if (obj && range.collapsed) {
  12426. obj.setAttribute('dir', forward);
  12427. return range;
  12428. }
  12429. bookmark = range.createBookmark();
  12430. range.enlarge(true);
  12431. var bookmark2 = range.createBookmark(),
  12432. current = domUtils.getNextDomNode(bookmark2.start, false, filterFn),
  12433. tmpRange = range.cloneRange(),
  12434. tmpNode;
  12435. while (current && !(domUtils.getPosition(current, bookmark2.end) & domUtils.POSITION_FOLLOWING)) {
  12436. if (current.nodeType == 3 || !block(current)) {
  12437. tmpRange.setStartBefore(current);
  12438. while (current && current !== bookmark2.end && !block(current)) {
  12439. tmpNode = current;
  12440. current = domUtils.getNextDomNode(current, false, null, function (node) {
  12441. return !block(node);
  12442. });
  12443. }
  12444. tmpRange.setEndAfter(tmpNode);
  12445. var common = tmpRange.getCommonAncestor();
  12446. if (!domUtils.isBody(common) && block(common)) {
  12447. //遍历到了block节点
  12448. common.setAttribute('dir', forward);
  12449. current = common;
  12450. } else {
  12451. //没有遍历到,添加一个block节点
  12452. var p = range.document.createElement('p');
  12453. p.setAttribute('dir', forward);
  12454. var frag = tmpRange.extractContents();
  12455. p.appendChild(frag);
  12456. tmpRange.insertNode(p);
  12457. current = p;
  12458. }
  12459. current = domUtils.getNextDomNode(current, false, filterFn);
  12460. } else {
  12461. current = domUtils.getNextDomNode(current, true, filterFn);
  12462. }
  12463. }
  12464. return range.moveToBookmark(bookmark2).moveToBookmark(bookmark);
  12465. };
  12466. /**
  12467. * 文字输入方向
  12468. * @command directionality
  12469. * @method execCommand
  12470. * @param { String } cmdName 命令字符串
  12471. * @param { String } forward 传入'ltr'表示从左向右输入,传入'rtl'表示从右向左输入
  12472. * @example
  12473. * ```javascript
  12474. * editor.execCommand( 'directionality', 'ltr');
  12475. * ```
  12476. */
  12477. /**
  12478. * 查询当前选区的文字输入方向
  12479. * @command directionality
  12480. * @method queryCommandValue
  12481. * @param { String } cmdName 命令字符串
  12482. * @return { String } 返回'ltr'表示从左向右输入,返回'rtl'表示从右向左输入
  12483. * @example
  12484. * ```javascript
  12485. * editor.queryCommandValue( 'directionality');
  12486. * ```
  12487. */
  12488. UE.commands['directionality'] = {
  12489. execCommand: function (cmdName, forward) {
  12490. var range = this.selection.getRange();
  12491. //闭合时单独处理
  12492. if (range.collapsed) {
  12493. var txt = this.document.createTextNode('d');
  12494. range.insertNode(txt);
  12495. }
  12496. doDirectionality(range, this, forward);
  12497. if (txt) {
  12498. range.setStartBefore(txt).collapse(true);
  12499. domUtils.remove(txt);
  12500. }
  12501. range.select();
  12502. return true;
  12503. },
  12504. queryCommandValue: function () {
  12505. var node = getObj(this);
  12506. return node ? node.getAttribute('dir') : 'ltr';
  12507. }
  12508. };
  12509. })();
  12510. // plugins/horizontal.js
  12511. /**
  12512. * 插入分割线插件
  12513. * @file
  12514. * @since 1.2.6.1
  12515. */
  12516. /**
  12517. * 插入分割线
  12518. * @command horizontal
  12519. * @method execCommand
  12520. * @param { String } cmdName 命令字符串
  12521. * @example
  12522. * ```javascript
  12523. * editor.execCommand( 'horizontal' );
  12524. * ```
  12525. */
  12526. UE.plugins['horizontal'] = function () {
  12527. var me = this;
  12528. me.commands['horizontal'] = {
  12529. execCommand: function (cmdName) {
  12530. var me = this;
  12531. if (me.queryCommandState(cmdName) !== -1) {
  12532. me.execCommand('insertHtml', '<hr>');
  12533. var range = me.selection.getRange(),
  12534. start = range.startContainer;
  12535. if (start.nodeType == 1 && !start.childNodes[range.startOffset]) {
  12536. var tmp;
  12537. if (tmp = start.childNodes[range.startOffset - 1]) {
  12538. if (tmp.nodeType == 1 && tmp.tagName == 'HR') {
  12539. if (me.options.enterTag == 'p') {
  12540. tmp = me.document.createElement('p');
  12541. range.insertNode(tmp);
  12542. range.setStart(tmp, 0).setCursor();
  12543. } else {
  12544. tmp = me.document.createElement('br');
  12545. range.insertNode(tmp);
  12546. range.setStartBefore(tmp).setCursor();
  12547. }
  12548. }
  12549. }
  12550. }
  12551. return true;
  12552. }
  12553. },
  12554. //边界在table里不能加分隔线
  12555. queryCommandState: function () {
  12556. return domUtils.filterNodeList(this.selection.getStartElementPath(), 'table') ? -1 : 0;
  12557. }
  12558. };
  12559. // me.addListener('delkeyup',function(){
  12560. // var rng = this.selection.getRange();
  12561. // if(browser.ie && browser.version > 8){
  12562. // rng.txtToElmBoundary(true);
  12563. // if(domUtils.isStartInblock(rng)){
  12564. // var tmpNode = rng.startContainer;
  12565. // var pre = tmpNode.previousSibling;
  12566. // if(pre && domUtils.isTagNode(pre,'hr')){
  12567. // domUtils.remove(pre);
  12568. // rng.select();
  12569. // return;
  12570. // }
  12571. // }
  12572. // }
  12573. // if(domUtils.isBody(rng.startContainer)){
  12574. // var hr = rng.startContainer.childNodes[rng.startOffset -1];
  12575. // if(hr && hr.nodeName == 'HR'){
  12576. // var next = hr.nextSibling;
  12577. // if(next){
  12578. // rng.setStart(next,0)
  12579. // }else if(hr.previousSibling){
  12580. // rng.setStartAtLast(hr.previousSibling)
  12581. // }else{
  12582. // var p = this.document.createElement('p');
  12583. // hr.parentNode.insertBefore(p,hr);
  12584. // domUtils.fillNode(this.document,p);
  12585. // rng.setStart(p,0);
  12586. // }
  12587. // domUtils.remove(hr);
  12588. // rng.setCursor(false,true);
  12589. // }
  12590. // }
  12591. // })
  12592. me.addListener('delkeydown', function (name, evt) {
  12593. var rng = this.selection.getRange();
  12594. rng.txtToElmBoundary(true);
  12595. if (domUtils.isStartInblock(rng)) {
  12596. var tmpNode = rng.startContainer;
  12597. var pre = tmpNode.previousSibling;
  12598. if (pre && domUtils.isTagNode(pre, 'hr')) {
  12599. domUtils.remove(pre);
  12600. rng.select();
  12601. domUtils.preventDefault(evt);
  12602. return true;
  12603. }
  12604. }
  12605. })
  12606. };
  12607. // plugins/time.js
  12608. /**
  12609. * 插入时间和日期
  12610. * @file
  12611. * @since 1.2.6.1
  12612. */
  12613. /**
  12614. * 插入时间,默认格式:12:59:59
  12615. * @command time
  12616. * @method execCommand
  12617. * @param { String } cmd 命令字符串
  12618. * @example
  12619. * ```javascript
  12620. * editor.execCommand( 'time');
  12621. * ```
  12622. */
  12623. /**
  12624. * 插入日期,默认格式:2013-08-30
  12625. * @command date
  12626. * @method execCommand
  12627. * @param { String } cmd 命令字符串
  12628. * @example
  12629. * ```javascript
  12630. * editor.execCommand( 'date');
  12631. * ```
  12632. */
  12633. UE.commands['time'] = UE.commands["date"] = {
  12634. execCommand: function (cmd, format) {
  12635. var date = new Date;
  12636. function formatTime(date, format) {
  12637. var hh = ('0' + date.getHours()).slice(-2),
  12638. ii = ('0' + date.getMinutes()).slice(-2),
  12639. ss = ('0' + date.getSeconds()).slice(-2);
  12640. format = format || 'hh:ii:ss';
  12641. return format.replace(/hh/ig, hh).replace(/ii/ig, ii).replace(/ss/ig, ss);
  12642. }
  12643. function formatDate(date, format) {
  12644. var yyyy = ('000' + date.getFullYear()).slice(-4),
  12645. yy = yyyy.slice(-2),
  12646. mm = ('0' + (date.getMonth() + 1)).slice(-2),
  12647. dd = ('0' + date.getDate()).slice(-2);
  12648. format = format || 'yyyy-mm-dd';
  12649. return format.replace(/yyyy/ig, yyyy).replace(/yy/ig, yy).replace(/mm/ig, mm).replace(/dd/ig, dd);
  12650. }
  12651. this.execCommand('insertHtml', cmd == "time" ? formatTime(date, format) : formatDate(date, format));
  12652. }
  12653. };
  12654. // plugins/rowspacing.js
  12655. /**
  12656. * 段前段后间距插件
  12657. * @file
  12658. * @since 1.2.6.1
  12659. */
  12660. /**
  12661. * 设置段间距
  12662. * @command rowspacing
  12663. * @method execCommand
  12664. * @param { String } cmd 命令字符串
  12665. * @param { String } value 段间距的值,以px为单位
  12666. * @param { String } dir 间距位置,top或bottom,分别表示段前和段后
  12667. * @example
  12668. * ```javascript
  12669. * editor.execCommand( 'rowspacing', '10', 'top' );
  12670. * ```
  12671. */
  12672. UE.plugins['rowspacing'] = function () {
  12673. var me = this;
  12674. me.setOpt({
  12675. 'rowspacingtop': ['5', '10', '15', '20', '25'],
  12676. 'rowspacingbottom': ['5', '10', '15', '20', '25']
  12677. });
  12678. me.commands['rowspacing'] = {
  12679. execCommand: function (cmdName, value, dir) {
  12680. this.execCommand('paragraph', 'p', {
  12681. style: 'margin-' + dir + ':' + value + 'px'
  12682. });
  12683. return true;
  12684. },
  12685. queryCommandValue: function (cmdName, dir) {
  12686. var pN = domUtils.filterNodeList(this.selection.getStartElementPath(), function (node) {
  12687. return domUtils.isBlockElm(node)
  12688. }),
  12689. value;
  12690. //trace:1026
  12691. if (pN) {
  12692. value = domUtils.getComputedStyle(pN, 'margin-' + dir).replace(/[^\d]/g, '');
  12693. return !value ? 0 : value;
  12694. }
  12695. return 0;
  12696. }
  12697. };
  12698. };
  12699. // plugins/lineheight.js
  12700. /**
  12701. * 设置行内间距
  12702. * @file
  12703. * @since 1.2.6.1
  12704. */
  12705. UE.plugins['lineheight'] = function () {
  12706. var me = this;
  12707. me.setOpt({
  12708. 'lineheight': ['1', '1.5', '1.75', '2', '3', '4', '5']
  12709. });
  12710. /**
  12711. * 行距
  12712. * @command lineheight
  12713. * @method execCommand
  12714. * @param { String } cmdName 命令字符串
  12715. * @param { String } value 传入的行高值, 该值是当前字体的倍数, 例如: 1.5, 1.75
  12716. * @example
  12717. * ```javascript
  12718. * editor.execCommand( 'lineheight', 1.5);
  12719. * ```
  12720. */
  12721. /**
  12722. * 查询当前选区内容的行高大小
  12723. * @command lineheight
  12724. * @method queryCommandValue
  12725. * @param { String } cmd 命令字符串
  12726. * @return { String } 返回当前行高大小
  12727. * @example
  12728. * ```javascript
  12729. * editor.queryCommandValue( 'lineheight' );
  12730. * ```
  12731. */
  12732. me.commands['lineheight'] = {
  12733. execCommand: function (cmdName, value) {
  12734. this.execCommand('paragraph', 'p', {
  12735. style: 'line-height:' + (value == "1" ? "normal" : value + 'em')
  12736. });
  12737. return true;
  12738. },
  12739. queryCommandValue: function () {
  12740. var pN = domUtils.filterNodeList(this.selection.getStartElementPath(), function (node) {
  12741. return domUtils.isBlockElm(node)
  12742. });
  12743. if (pN) {
  12744. var value = domUtils.getComputedStyle(pN, 'line-height');
  12745. return value == 'normal' ? 1 : value.replace(/[^\d.]*/ig, "");
  12746. }
  12747. }
  12748. };
  12749. };
  12750. // plugins/insertcode.js
  12751. /**
  12752. * 插入代码插件
  12753. * @file
  12754. * @since 1.2.6.1
  12755. */
  12756. UE.plugins['insertcode'] = function () {
  12757. var me = this;
  12758. me.ready(function () {
  12759. utils.cssRule('pre', 'pre{margin:.5em 0;padding:.4em .6em;border-radius:8px;background:#f8f8f8;}',
  12760. me.document)
  12761. });
  12762. me.setOpt('insertcode', {
  12763. 'as3': 'ActionScript3',
  12764. 'bash': 'Bash/Shell',
  12765. 'cpp': 'C/C++',
  12766. 'css': 'Css',
  12767. 'cf': 'CodeFunction',
  12768. 'c#': 'C#',
  12769. 'delphi': 'Delphi',
  12770. 'diff': 'Diff',
  12771. 'erlang': 'Erlang',
  12772. 'groovy': 'Groovy',
  12773. 'html': 'Html',
  12774. 'java': 'Java',
  12775. 'jfx': 'JavaFx',
  12776. 'js': 'Javascript',
  12777. 'pl': 'Perl',
  12778. 'php': 'Php',
  12779. 'plain': 'Plain Text',
  12780. 'ps': 'PowerShell',
  12781. 'python': 'Python',
  12782. 'ruby': 'Ruby',
  12783. 'scala': 'Scala',
  12784. 'sql': 'Sql',
  12785. 'vb': 'Vb',
  12786. 'xml': 'Xml'
  12787. });
  12788. /**
  12789. * 插入代码
  12790. * @command insertcode
  12791. * @method execCommand
  12792. * @param { String } cmd 命令字符串
  12793. * @param { String } lang 插入代码的语言
  12794. * @example
  12795. * ```javascript
  12796. * editor.execCommand( 'insertcode', 'javascript' );
  12797. * ```
  12798. */
  12799. /**
  12800. * 如果选区所在位置是插入插入代码区域,返回代码的语言
  12801. * @command insertcode
  12802. * @method queryCommandValue
  12803. * @param { String } cmd 命令字符串
  12804. * @return { String } 返回代码的语言
  12805. * @example
  12806. * ```javascript
  12807. * editor.queryCommandValue( 'insertcode' );
  12808. * ```
  12809. */
  12810. me.commands['insertcode'] = {
  12811. execCommand: function (cmd, lang) {
  12812. var me = this,
  12813. rng = me.selection.getRange(),
  12814. pre = domUtils.findParentByTagName(rng.startContainer, 'pre', true);
  12815. if (pre) {
  12816. pre.className = 'brush:' + lang + ';toolbar:false;';
  12817. } else {
  12818. var code = '';
  12819. if (rng.collapsed) {
  12820. code = browser.ie && browser.ie11below ? (browser.version <= 8 ? '&nbsp;' : '') : '<br/>';
  12821. } else {
  12822. var frag = rng.extractContents();
  12823. var div = me.document.createElement('div');
  12824. div.appendChild(frag);
  12825. utils.each(UE.filterNode(UE.htmlparser(div.innerHTML.replace(/[\r\t]/g, '')), me.options.filterTxtRules).children, function (node) {
  12826. if (browser.ie && browser.ie11below && browser.version > 8) {
  12827. if (node.type == 'element') {
  12828. if (node.tagName == 'br') {
  12829. code += '\n'
  12830. } else if (!dtd.$empty[node.tagName]) {
  12831. utils.each(node.children, function (cn) {
  12832. if (cn.type == 'element') {
  12833. if (cn.tagName == 'br') {
  12834. code += '\n'
  12835. } else if (!dtd.$empty[node.tagName]) {
  12836. code += cn.innerText();
  12837. }
  12838. } else {
  12839. code += cn.data
  12840. }
  12841. })
  12842. if (!/\n$/.test(code)) {
  12843. code += '\n';
  12844. }
  12845. }
  12846. } else {
  12847. code += node.data + '\n'
  12848. }
  12849. if (!node.nextSibling() && /\n$/.test(code)) {
  12850. code = code.replace(/\n$/, '');
  12851. }
  12852. } else {
  12853. if (browser.ie && browser.ie11below) {
  12854. if (node.type == 'element') {
  12855. if (node.tagName == 'br') {
  12856. code += '<br>'
  12857. } else if (!dtd.$empty[node.tagName]) {
  12858. utils.each(node.children, function (cn) {
  12859. if (cn.type == 'element') {
  12860. if (cn.tagName == 'br') {
  12861. code += '<br>'
  12862. } else if (!dtd.$empty[node.tagName]) {
  12863. code += cn.innerText();
  12864. }
  12865. } else {
  12866. code += cn.data
  12867. }
  12868. });
  12869. if (!/br>$/.test(code)) {
  12870. code += '<br>';
  12871. }
  12872. }
  12873. } else {
  12874. code += node.data + '<br>'
  12875. }
  12876. if (!node.nextSibling() && /<br>$/.test(code)) {
  12877. code = code.replace(/<br>$/, '');
  12878. }
  12879. } else {
  12880. code += (node.type == 'element' ? (dtd.$empty[node.tagName] ? '' : node.innerText()) : node.data);
  12881. if (!/br\/?\s*>$/.test(code)) {
  12882. if (!node.nextSibling())
  12883. return;
  12884. code += '<br>'
  12885. }
  12886. }
  12887. }
  12888. });
  12889. }
  12890. me.execCommand('inserthtml', '<pre id="coder"class="brush:' + lang + ';toolbar:false">' + code + '</pre>', true);
  12891. pre = me.document.getElementById('coder');
  12892. domUtils.removeAttributes(pre, 'id');
  12893. var tmpNode = pre.previousSibling;
  12894. if (tmpNode && (tmpNode.nodeType == 3 && tmpNode.nodeValue.length == 1 && browser.ie && browser.version == 6 || domUtils.isEmptyBlock(tmpNode))) {
  12895. domUtils.remove(tmpNode)
  12896. }
  12897. var rng = me.selection.getRange();
  12898. if (domUtils.isEmptyBlock(pre)) {
  12899. rng.setStart(pre, 0).setCursor(false, true)
  12900. } else {
  12901. rng.selectNodeContents(pre).select()
  12902. }
  12903. }
  12904. },
  12905. queryCommandValue: function () {
  12906. var path = this.selection.getStartElementPath();
  12907. var lang = '';
  12908. utils.each(path, function (node) {
  12909. if (node.nodeName == 'PRE') {
  12910. var match = node.className.match(/brush:([^;]+)/);
  12911. lang = match && match[1] ? match[1] : '';
  12912. return false;
  12913. }
  12914. });
  12915. return lang;
  12916. }
  12917. };
  12918. me.addInputRule(function (root) {
  12919. utils.each(root.getNodesByTagName('pre'), function (pre) {
  12920. var brs = pre.getNodesByTagName('br');
  12921. if (brs.length) {
  12922. browser.ie && browser.ie11below && browser.version > 8 && utils.each(brs, function (br) {
  12923. var txt = UE.uNode.createText('\n');
  12924. br.parentNode.insertBefore(txt, br);
  12925. br.parentNode.removeChild(br);
  12926. });
  12927. return;
  12928. }
  12929. if (browser.ie && browser.ie11below && browser.version > 8)
  12930. return;
  12931. var code = pre.innerText().split(/\n/);
  12932. pre.innerHTML('');
  12933. utils.each(code, function (c) {
  12934. if (c.length) {
  12935. pre.appendChild(UE.uNode.createText(c));
  12936. }
  12937. pre.appendChild(UE.uNode.createElement('br'))
  12938. })
  12939. })
  12940. });
  12941. me.addOutputRule(function (root) {
  12942. utils.each(root.getNodesByTagName('pre'), function (pre) {
  12943. var code = '';
  12944. utils.each(pre.children, function (n) {
  12945. if (n.type == 'text') {
  12946. //在ie下文本内容有可能末尾带有\n要去掉
  12947. //trace:3396
  12948. code += n.data.replace(/[ ]/g, '&nbsp;').replace(/\n$/, '');
  12949. } else {
  12950. if (n.tagName == 'br') {
  12951. code += '\n'
  12952. } else {
  12953. code += (!dtd.$empty[n.tagName] ? '' : n.innerText());
  12954. }
  12955. }
  12956. });
  12957. pre.innerText(code.replace(/(&nbsp;|\n)+$/, ''))
  12958. })
  12959. });
  12960. //不需要判断highlight的command列表
  12961. me.notNeedCodeQuery = {
  12962. help: 1,
  12963. undo: 1,
  12964. redo: 1,
  12965. source: 1,
  12966. print: 1,
  12967. searchreplace: 1,
  12968. fullscreen: 1,
  12969. preview: 1,
  12970. insertparagraph: 1,
  12971. elementpath: 1,
  12972. insertcode: 1,
  12973. inserthtml: 1,
  12974. selectall: 1
  12975. };
  12976. //将queyCommamndState重置
  12977. var orgQuery = me.queryCommandState;
  12978. me.queryCommandState = function (cmd) {
  12979. var me = this;
  12980. if (!me.notNeedCodeQuery[cmd.toLowerCase()] && me.selection && me.queryCommandValue('insertcode')) {
  12981. return -1;
  12982. }
  12983. return UE.Editor.prototype.queryCommandState.apply(this, arguments)
  12984. };
  12985. me.addListener('beforeenterkeydown', function () {
  12986. var rng = me.selection.getRange();
  12987. var pre = domUtils.findParentByTagName(rng.startContainer, 'pre', true);
  12988. if (pre) {
  12989. me.fireEvent('saveScene');
  12990. if (!rng.collapsed) {
  12991. rng.deleteContents();
  12992. }
  12993. if (!browser.ie || browser.ie9above) {
  12994. var tmpNode = me.document.createElement('br'),
  12995. pre;
  12996. rng.insertNode(tmpNode).setStartAfter(tmpNode).collapse(true);
  12997. var next = tmpNode.nextSibling;
  12998. if (!next && (!browser.ie || browser.version > 10)) {
  12999. rng.insertNode(tmpNode.cloneNode(false));
  13000. } else {
  13001. rng.setStartAfter(tmpNode);
  13002. }
  13003. pre = tmpNode.previousSibling;
  13004. var tmp;
  13005. while (pre) {
  13006. tmp = pre;
  13007. pre = pre.previousSibling;
  13008. if (!pre || pre.nodeName == 'BR') {
  13009. pre = tmp;
  13010. break;
  13011. }
  13012. }
  13013. if (pre) {
  13014. var str = '';
  13015. while (pre && pre.nodeName != 'BR' && new RegExp('^[\\s' + domUtils.fillChar + ']*$').test(pre.nodeValue)) {
  13016. str += pre.nodeValue;
  13017. pre = pre.nextSibling;
  13018. }
  13019. if (pre.nodeName != 'BR') {
  13020. var match = pre.nodeValue.match(new RegExp('^([\\s' + domUtils.fillChar + ']+)'));
  13021. if (match && match[1]) {
  13022. str += match[1]
  13023. }
  13024. }
  13025. if (str) {
  13026. str = me.document.createTextNode(str);
  13027. rng.insertNode(str).setStartAfter(str);
  13028. }
  13029. }
  13030. rng.collapse(true).select(true);
  13031. } else {
  13032. if (browser.version > 8) {
  13033. var txt = me.document.createTextNode('\n');
  13034. var start = rng.startContainer;
  13035. if (rng.startOffset == 0) {
  13036. var preNode = start.previousSibling;
  13037. if (preNode) {
  13038. rng.insertNode(txt);
  13039. var fillchar = me.document.createTextNode(' ');
  13040. rng.setStartAfter(txt).insertNode(fillchar).setStart(fillchar, 0).collapse(true).select(true)
  13041. }
  13042. } else {
  13043. rng.insertNode(txt).setStartAfter(txt);
  13044. var fillchar = me.document.createTextNode(' ');
  13045. start = rng.startContainer.childNodes[rng.startOffset];
  13046. if (start && !/^\n/.test(start.nodeValue)) {
  13047. rng.setStartBefore(txt)
  13048. }
  13049. rng.insertNode(fillchar).setStart(fillchar, 0).collapse(true).select(true)
  13050. }
  13051. } else {
  13052. var tmpNode = me.document.createElement('br');
  13053. rng.insertNode(tmpNode);
  13054. rng.insertNode(me.document.createTextNode(domUtils.fillChar));
  13055. rng.setStartAfter(tmpNode);
  13056. pre = tmpNode.previousSibling;
  13057. var tmp;
  13058. while (pre) {
  13059. tmp = pre;
  13060. pre = pre.previousSibling;
  13061. if (!pre || pre.nodeName == 'BR') {
  13062. pre = tmp;
  13063. break;
  13064. }
  13065. }
  13066. if (pre) {
  13067. var str = '';
  13068. while (pre && pre.nodeName != 'BR' && new RegExp('^[ ' + domUtils.fillChar + ']*$').test(pre.nodeValue)) {
  13069. str += pre.nodeValue;
  13070. pre = pre.nextSibling;
  13071. }
  13072. if (pre.nodeName != 'BR') {
  13073. var match = pre.nodeValue.match(new RegExp('^([ ' + domUtils.fillChar + ']+)'));
  13074. if (match && match[1]) {
  13075. str += match[1]
  13076. }
  13077. }
  13078. str = me.document.createTextNode(str);
  13079. rng.insertNode(str).setStartAfter(str);
  13080. }
  13081. rng.collapse(true).select();
  13082. }
  13083. }
  13084. me.fireEvent('saveScene');
  13085. return true;
  13086. }
  13087. });
  13088. me.addListener('tabkeydown', function (cmd, evt) {
  13089. var rng = me.selection.getRange();
  13090. var pre = domUtils.findParentByTagName(rng.startContainer, 'pre', true);
  13091. if (pre) {
  13092. me.fireEvent('saveScene');
  13093. if (evt.shiftKey) {
  13094. } else {
  13095. if (!rng.collapsed) {
  13096. var bk = rng.createBookmark();
  13097. var start = bk.start.previousSibling;
  13098. while (start) {
  13099. if (pre.firstChild === start && !domUtils.isBr(start)) {
  13100. pre.insertBefore(me.document.createTextNode(' '), start);
  13101. break;
  13102. }
  13103. if (domUtils.isBr(start)) {
  13104. pre.insertBefore(me.document.createTextNode(' '), start.nextSibling);
  13105. break;
  13106. }
  13107. start = start.previousSibling;
  13108. }
  13109. var end = bk.end;
  13110. start = bk.start.nextSibling;
  13111. if (pre.firstChild === bk.start) {
  13112. pre.insertBefore(me.document.createTextNode(' '), start.nextSibling)
  13113. }
  13114. while (start && start !== end) {
  13115. if (domUtils.isBr(start) && start.nextSibling) {
  13116. if (start.nextSibling === end) {
  13117. break;
  13118. }
  13119. pre.insertBefore(me.document.createTextNode(' '), start.nextSibling)
  13120. }
  13121. start = start.nextSibling;
  13122. }
  13123. rng.moveToBookmark(bk).select();
  13124. } else {
  13125. var tmpNode = me.document.createTextNode(' ');
  13126. rng.insertNode(tmpNode).setStartAfter(tmpNode).collapse(true).select(true);
  13127. }
  13128. }
  13129. me.fireEvent('saveScene');
  13130. return true;
  13131. }
  13132. });
  13133. me.addListener('beforeinserthtml', function (evtName, html) {
  13134. var me = this,
  13135. rng = me.selection.getRange(),
  13136. pre = domUtils.findParentByTagName(rng.startContainer, 'pre', true);
  13137. if (pre) {
  13138. if (!rng.collapsed) {
  13139. rng.deleteContents()
  13140. }
  13141. var htmlstr = '';
  13142. if (browser.ie && browser.version > 8) {
  13143. utils.each(UE.filterNode(UE.htmlparser(html), me.options.filterTxtRules).children, function (node) {
  13144. if (node.type == 'element') {
  13145. if (node.tagName == 'br') {
  13146. htmlstr += '\n'
  13147. } else if (!dtd.$empty[node.tagName]) {
  13148. utils.each(node.children, function (cn) {
  13149. if (cn.type == 'element') {
  13150. if (cn.tagName == 'br') {
  13151. htmlstr += '\n'
  13152. } else if (!dtd.$empty[node.tagName]) {
  13153. htmlstr += cn.innerText();
  13154. }
  13155. } else {
  13156. htmlstr += cn.data
  13157. }
  13158. })
  13159. if (!/\n$/.test(htmlstr)) {
  13160. htmlstr += '\n';
  13161. }
  13162. }
  13163. } else {
  13164. htmlstr += node.data + '\n'
  13165. }
  13166. if (!node.nextSibling() && /\n$/.test(htmlstr)) {
  13167. htmlstr = htmlstr.replace(/\n$/, '');
  13168. }
  13169. });
  13170. var tmpNode = me.document.createTextNode(utils.html(htmlstr.replace(/&nbsp;/g, ' ')));
  13171. rng.insertNode(tmpNode).selectNode(tmpNode).select();
  13172. } else {
  13173. var frag = me.document.createDocumentFragment();
  13174. utils.each(UE.filterNode(UE.htmlparser(html), me.options.filterTxtRules).children, function (node) {
  13175. if (node.type == 'element') {
  13176. if (node.tagName == 'br') {
  13177. frag.appendChild(me.document.createElement('br'))
  13178. } else if (!dtd.$empty[node.tagName]) {
  13179. utils.each(node.children, function (cn) {
  13180. if (cn.type == 'element') {
  13181. if (cn.tagName == 'br') {
  13182. frag.appendChild(me.document.createElement('br'))
  13183. } else if (!dtd.$empty[node.tagName]) {
  13184. frag.appendChild(me.document.createTextNode(utils.html(cn.innerText().replace(/&nbsp;/g, ' '))));
  13185. }
  13186. } else {
  13187. frag.appendChild(me.document.createTextNode(utils.html(cn.data.replace(/&nbsp;/g, ' '))));
  13188. }
  13189. })
  13190. if (frag.lastChild.nodeName != 'BR') {
  13191. frag.appendChild(me.document.createElement('br'))
  13192. }
  13193. }
  13194. } else {
  13195. frag.appendChild(me.document.createTextNode(utils.html(node.data.replace(/&nbsp;/g, ' '))));
  13196. }
  13197. if (!node.nextSibling() && frag.lastChild.nodeName == 'BR') {
  13198. frag.removeChild(frag.lastChild)
  13199. }
  13200. });
  13201. rng.insertNode(frag).select();
  13202. }
  13203. return true;
  13204. }
  13205. });
  13206. //方向键的处理
  13207. me.addListener('keydown', function (cmd, evt) {
  13208. var me = this,
  13209. keyCode = evt.keyCode || evt.which;
  13210. if (keyCode == 40) {
  13211. var rng = me.selection.getRange(),
  13212. pre, start = rng.startContainer;
  13213. if (rng.collapsed && (pre = domUtils.findParentByTagName(rng.startContainer, 'pre', true)) && !pre.nextSibling) {
  13214. var last = pre.lastChild
  13215. while (last && last.nodeName == 'BR') {
  13216. last = last.previousSibling;
  13217. }
  13218. if (last === start || rng.startContainer === pre && rng.startOffset == pre.childNodes.length) {
  13219. me.execCommand('insertparagraph');
  13220. domUtils.preventDefault(evt)
  13221. }
  13222. }
  13223. }
  13224. });
  13225. //trace:3395
  13226. me.addListener('delkeydown', function (type, evt) {
  13227. var rng = this.selection.getRange();
  13228. rng.txtToElmBoundary(true);
  13229. var start = rng.startContainer;
  13230. if (domUtils.isTagNode(start, 'pre') && rng.collapsed && domUtils.isStartInblock(rng)) {
  13231. var p = me.document.createElement('p');
  13232. domUtils.fillNode(me.document, p);
  13233. start.parentNode.insertBefore(p, start);
  13234. domUtils.remove(start);
  13235. rng.setStart(p, 0).setCursor(false, true);
  13236. domUtils.preventDefault(evt);
  13237. return true;
  13238. }
  13239. })
  13240. };
  13241. // plugins/cleardoc.js
  13242. /**
  13243. * 清空文档插件
  13244. * @file
  13245. * @since 1.2.6.1
  13246. */
  13247. /**
  13248. * 清空文档
  13249. * @command cleardoc
  13250. * @method execCommand
  13251. * @param { String } cmd 命令字符串
  13252. * @example
  13253. * ```javascript
  13254. * //editor 是编辑器实例
  13255. * editor.execCommand('cleardoc');
  13256. * ```
  13257. */
  13258. UE.commands['cleardoc'] = {
  13259. execCommand: function (cmdName) {
  13260. var me = this,
  13261. enterTag = me.options.enterTag,
  13262. range = me.selection.getRange();
  13263. if (enterTag == "br") {
  13264. me.body.innerHTML = "<br/>";
  13265. range.setStart(me.body, 0).setCursor();
  13266. } else {
  13267. me.body.innerHTML = "<p>" + (ie ? "" : "<br/>") + "</p>";
  13268. range.setStart(me.body.firstChild, 0).setCursor(false, true);
  13269. }
  13270. setTimeout(function () {
  13271. me.fireEvent("clearDoc");
  13272. }, 0);
  13273. }
  13274. };
  13275. // plugins/anchor.js
  13276. /**
  13277. * 锚点插件,为UEditor提供插入锚点支持
  13278. * @file
  13279. * @since 1.2.6.1
  13280. */
  13281. UE.plugin.register('anchor', function () {
  13282. return {
  13283. bindEvents: {
  13284. 'ready': function () {
  13285. utils.cssRule('anchor',
  13286. '.anchorclass{background: url(\'' +
  13287. this.options.themePath +
  13288. this.options.theme + '/images/anchor.gif\') no-repeat scroll left center transparent;cursor: auto;display: inline-block;height: 16px;width: 15px;}',
  13289. this.document);
  13290. }
  13291. },
  13292. outputRule: function (root) {
  13293. utils.each(root.getNodesByTagName('img'), function (a) {
  13294. var val;
  13295. if (val = a.getAttr('anchorname')) {
  13296. a.tagName = 'a';
  13297. a.setAttr({
  13298. anchorname: '',
  13299. name: val,
  13300. 'class': ''
  13301. })
  13302. }
  13303. })
  13304. },
  13305. inputRule: function (root) {
  13306. utils.each(root.getNodesByTagName('a'), function (a) {
  13307. var val;
  13308. if ((val = a.getAttr('name')) && !a.getAttr('href')) {
  13309. a.tagName = 'img';
  13310. a.setAttr({
  13311. anchorname: a.getAttr('name'),
  13312. 'class': 'anchorclass'
  13313. });
  13314. a.setAttr('name')
  13315. }
  13316. })
  13317. },
  13318. commands: {
  13319. /**
  13320. * 插入锚点
  13321. * @command anchor
  13322. * @method execCommand
  13323. * @param { String } cmd 命令字符串
  13324. * @param { String } name 锚点名称字符串
  13325. * @example
  13326. * ```javascript
  13327. * //editor 是编辑器实例
  13328. * editor.execCommand('anchor', 'anchor1');
  13329. * ```
  13330. */
  13331. 'anchor': {
  13332. execCommand: function (cmd, name) {
  13333. var range = this.selection.getRange(),
  13334. img = range.getClosedNode();
  13335. if (img && img.getAttribute('anchorname')) {
  13336. if (name) {
  13337. img.setAttribute('anchorname', name);
  13338. } else {
  13339. range.setStartBefore(img).setCursor();
  13340. domUtils.remove(img);
  13341. }
  13342. } else {
  13343. if (name) {
  13344. //只在选区的开始插入
  13345. var anchor = this.document.createElement('img');
  13346. range.collapse(true);
  13347. domUtils.setAttributes(anchor, {
  13348. 'anchorname': name,
  13349. 'class': 'anchorclass'
  13350. });
  13351. range.insertNode(anchor).setStartAfter(anchor).setCursor(false, true);
  13352. }
  13353. }
  13354. }
  13355. }
  13356. }
  13357. }
  13358. });
  13359. // plugins/wordcount.js
  13360. ///import core
  13361. ///commands 字数统计
  13362. ///commandsName WordCount,wordCount
  13363. ///commandsTitle 字数统计
  13364. /*
  13365. * Created by JetBrains WebStorm.
  13366. * User: taoqili
  13367. * Date: 11-9-7
  13368. * Time: 下午8:18
  13369. * To change this template use File | Settings | File Templates.
  13370. */
  13371. UE.plugins['wordcount'] = function () {
  13372. var me = this;
  13373. me.setOpt('wordCount', true);
  13374. me.addListener('contentchange', function () {
  13375. me.fireEvent('wordcount');
  13376. });
  13377. var timer;
  13378. me.addListener('ready', function () {
  13379. var me = this;
  13380. domUtils.on(me.body, "keyup", function (evt) {
  13381. var code = evt.keyCode || evt.which,
  13382. //忽略的按键,ctr,alt,shift,方向键
  13383. ignores = {
  13384. "16": 1,
  13385. "18": 1,
  13386. "20": 1,
  13387. "37": 1,
  13388. "38": 1,
  13389. "39": 1,
  13390. "40": 1
  13391. };
  13392. if (code in ignores) return;
  13393. clearTimeout(timer);
  13394. timer = setTimeout(function () {
  13395. me.fireEvent('wordcount');
  13396. }, 200)
  13397. })
  13398. });
  13399. };
  13400. // plugins/pagebreak.js
  13401. /**
  13402. * 分页功能插件
  13403. * @file
  13404. * @since 1.2.6.1
  13405. */
  13406. UE.plugins['pagebreak'] = function () {
  13407. var me = this,
  13408. notBreakTags = ['td'];
  13409. me.setOpt('pageBreakTag', '_ueditor_page_break_tag_');
  13410. function fillNode(node) {
  13411. if (domUtils.isEmptyBlock(node)) {
  13412. var firstChild = node.firstChild,
  13413. tmpNode;
  13414. while (firstChild && firstChild.nodeType == 1 && domUtils.isEmptyBlock(firstChild)) {
  13415. tmpNode = firstChild;
  13416. firstChild = firstChild.firstChild;
  13417. }!tmpNode && (tmpNode = node);
  13418. domUtils.fillNode(me.document, tmpNode);
  13419. }
  13420. }
  13421. //分页符样式添加
  13422. me.ready(function () {
  13423. utils.cssRule('pagebreak', '.pagebreak{display:block;clear:both !important;cursor:default !important;width: 100% !important;margin:0;}', me.document);
  13424. });
  13425. function isHr(node) {
  13426. return node && node.nodeType == 1 && node.tagName == 'HR' && node.className == 'pagebreak';
  13427. }
  13428. me.addInputRule(function (root) {
  13429. root.traversal(function (node) {
  13430. if (node.type == 'text' && node.data == me.options.pageBreakTag) {
  13431. var hr = UE.uNode.createElement('<hr class="pagebreak" noshade="noshade" size="5" style="-webkit-user-select: none;">');
  13432. node.parentNode.insertBefore(hr, node);
  13433. node.parentNode.removeChild(node)
  13434. }
  13435. })
  13436. });
  13437. me.addOutputRule(function (node) {
  13438. utils.each(node.getNodesByTagName('hr'), function (n) {
  13439. if (n.getAttr('class') == 'pagebreak') {
  13440. var txt = UE.uNode.createText(me.options.pageBreakTag);
  13441. n.parentNode.insertBefore(txt, n);
  13442. n.parentNode.removeChild(n);
  13443. }
  13444. })
  13445. });
  13446. /**
  13447. * 插入分页符
  13448. * @command pagebreak
  13449. * @method execCommand
  13450. * @param { String } cmd 命令字符串
  13451. * @remind 在表格中插入分页符会把表格切分成两部分
  13452. * @remind 获取编辑器内的数据时, 编辑器会把分页符转换成“_ueditor_page_break_tag_”字符串,
  13453. * 以便于提交数据到服务器端后处理分页。
  13454. * @example
  13455. * ```javascript
  13456. * editor.execCommand( 'pagebreak'); //插入一个hr标签,带有样式类名pagebreak
  13457. * ```
  13458. */
  13459. me.commands['pagebreak'] = {
  13460. execCommand: function () {
  13461. var range = me.selection.getRange(),
  13462. hr = me.document.createElement('hr');
  13463. domUtils.setAttributes(hr, {
  13464. 'class': 'pagebreak',
  13465. noshade: "noshade",
  13466. size: "5"
  13467. });
  13468. domUtils.unSelectable(hr);
  13469. //table单独处理
  13470. var node = domUtils.findParentByTagName(range.startContainer, notBreakTags, true),
  13471. parents = [],
  13472. pN;
  13473. if (node) {
  13474. switch (node.tagName) {
  13475. case 'TD':
  13476. pN = node.parentNode;
  13477. if (!pN.previousSibling) {
  13478. var table = domUtils.findParentByTagName(pN, 'table');
  13479. // var tableWrapDiv = table.parentNode;
  13480. // if(tableWrapDiv && tableWrapDiv.nodeType == 1
  13481. // && tableWrapDiv.tagName == 'DIV'
  13482. // && tableWrapDiv.getAttribute('dropdrag')
  13483. // ){
  13484. // domUtils.remove(tableWrapDiv,true);
  13485. // }
  13486. table.parentNode.insertBefore(hr, table);
  13487. parents = domUtils.findParents(hr, true);
  13488. } else {
  13489. pN.parentNode.insertBefore(hr, pN);
  13490. parents = domUtils.findParents(hr);
  13491. }
  13492. pN = parents[1];
  13493. if (hr !== pN) {
  13494. domUtils.breakParent(hr, pN);
  13495. }
  13496. //table要重写绑定一下拖拽
  13497. me.fireEvent('afteradjusttable', me.document);
  13498. }
  13499. } else {
  13500. if (!range.collapsed) {
  13501. range.deleteContents();
  13502. var start = range.startContainer;
  13503. while (!domUtils.isBody(start) && domUtils.isBlockElm(start) && domUtils.isEmptyNode(start)) {
  13504. range.setStartBefore(start).collapse(true);
  13505. domUtils.remove(start);
  13506. start = range.startContainer;
  13507. }
  13508. }
  13509. range.insertNode(hr);
  13510. var pN = hr.parentNode,
  13511. nextNode;
  13512. while (!domUtils.isBody(pN)) {
  13513. domUtils.breakParent(hr, pN);
  13514. nextNode = hr.nextSibling;
  13515. if (nextNode && domUtils.isEmptyBlock(nextNode)) {
  13516. domUtils.remove(nextNode);
  13517. }
  13518. pN = hr.parentNode;
  13519. }
  13520. nextNode = hr.nextSibling;
  13521. var pre = hr.previousSibling;
  13522. if (isHr(pre)) {
  13523. domUtils.remove(pre);
  13524. } else {
  13525. pre && fillNode(pre);
  13526. }
  13527. if (!nextNode) {
  13528. var p = me.document.createElement('p');
  13529. hr.parentNode.appendChild(p);
  13530. domUtils.fillNode(me.document, p);
  13531. range.setStart(p, 0).collapse(true);
  13532. } else {
  13533. if (isHr(nextNode)) {
  13534. domUtils.remove(nextNode);
  13535. } else {
  13536. fillNode(nextNode);
  13537. }
  13538. range.setEndAfter(hr).collapse(false);
  13539. }
  13540. range.select(true);
  13541. }
  13542. }
  13543. };
  13544. };
  13545. // plugins/wordimage.js
  13546. ///import core
  13547. ///commands 本地图片引导上传
  13548. ///commandsName WordImage
  13549. ///commandsTitle 本地图片引导上传
  13550. ///commandsDialog dialogs\wordimage
  13551. UE.plugin.register('wordimage', function () {
  13552. var me = this,
  13553. images = [];
  13554. return {
  13555. commands: {
  13556. 'wordimage': {
  13557. execCommand: function () {
  13558. var images = domUtils.getElementsByTagName(me.body, "img");
  13559. var urlList = [];
  13560. for (var i = 0, ci; ci = images[i++];) {
  13561. var url = ci.getAttribute("word_img");
  13562. url && urlList.push(url);
  13563. }
  13564. return urlList;
  13565. },
  13566. queryCommandState: function () {
  13567. images = domUtils.getElementsByTagName(me.body, "img");
  13568. for (var i = 0, ci; ci = images[i++];) {
  13569. if (ci.getAttribute("word_img")) {
  13570. return 1;
  13571. }
  13572. }
  13573. return -1;
  13574. },
  13575. notNeedUndo: true
  13576. }
  13577. },
  13578. inputRule: function (root) {
  13579. utils.each(root.getNodesByTagName('img'), function (img) {
  13580. var attrs = img.attrs,
  13581. flag = parseInt(attrs.width) < 128 || parseInt(attrs.height) < 43,
  13582. opt = me.options,
  13583. src = opt.UEDITOR_HOME_URL + 'themes/default/images/spacer.gif';
  13584. if (attrs['src'] && /^(?:(file:\/+))/.test(attrs['src'])) {
  13585. img.setAttr({
  13586. width: attrs.width,
  13587. height: attrs.height,
  13588. alt: attrs.alt,
  13589. word_img: attrs.src,
  13590. src: src,
  13591. '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'
  13592. })
  13593. }
  13594. })
  13595. }
  13596. }
  13597. });
  13598. // plugins/dragdrop.js
  13599. UE.plugins['dragdrop'] = function () {
  13600. var me = this;
  13601. me.ready(function () {
  13602. domUtils.on(this.body, 'dragend', function () {
  13603. var rng = me.selection.getRange();
  13604. var node = rng.getClosedNode() || me.selection.getStart();
  13605. if (node && node.tagName == 'IMG') {
  13606. var pre = node.previousSibling,
  13607. next;
  13608. while (next = node.nextSibling) {
  13609. if (next.nodeType == 1 && next.tagName == 'SPAN' && !next.firstChild) {
  13610. domUtils.remove(next)
  13611. } else {
  13612. break;
  13613. }
  13614. }
  13615. if ((pre && pre.nodeType == 1 && !domUtils.isEmptyBlock(pre) || !pre) && (!next || next && !domUtils.isEmptyBlock(next))) {
  13616. if (pre && pre.tagName == 'P' && !domUtils.isEmptyBlock(pre)) {
  13617. pre.appendChild(node);
  13618. domUtils.moveChild(next, pre);
  13619. domUtils.remove(next);
  13620. } else if (next && next.tagName == 'P' && !domUtils.isEmptyBlock(next)) {
  13621. next.insertBefore(node, next.firstChild);
  13622. }
  13623. if (pre && pre.tagName == 'P' && domUtils.isEmptyBlock(pre)) {
  13624. domUtils.remove(pre)
  13625. }
  13626. if (next && next.tagName == 'P' && domUtils.isEmptyBlock(next)) {
  13627. domUtils.remove(next)
  13628. }
  13629. rng.selectNode(node).select();
  13630. me.fireEvent('saveScene');
  13631. }
  13632. }
  13633. })
  13634. });
  13635. me.addListener('keyup', function (type, evt) {
  13636. var keyCode = evt.keyCode || evt.which;
  13637. if (keyCode == 13) {
  13638. var rng = me.selection.getRange(),
  13639. node;
  13640. if (node = domUtils.findParentByTagName(rng.startContainer, 'p', true)) {
  13641. if (domUtils.getComputedStyle(node, 'text-align') == 'center') {
  13642. domUtils.removeStyle(node, 'text-align')
  13643. }
  13644. }
  13645. }
  13646. })
  13647. };
  13648. // plugins/undo.js
  13649. /**
  13650. * undo redo
  13651. * @file
  13652. * @since 1.2.6.1
  13653. */
  13654. /**
  13655. * 撤销上一次执行的命令
  13656. * @command undo
  13657. * @method execCommand
  13658. * @param { String } cmd 命令字符串
  13659. * @example
  13660. * ```javascript
  13661. * editor.execCommand( 'undo' );
  13662. * ```
  13663. */
  13664. /**
  13665. * 重做上一次执行的命令
  13666. * @command redo
  13667. * @method execCommand
  13668. * @param { String } cmd 命令字符串
  13669. * @example
  13670. * ```javascript
  13671. * editor.execCommand( 'redo' );
  13672. * ```
  13673. */
  13674. UE.plugins['undo'] = function () {
  13675. var saveSceneTimer;
  13676. var me = this,
  13677. maxUndoCount = me.options.maxUndoCount || 20,
  13678. maxInputCount = me.options.maxInputCount || 20,
  13679. fillchar = new RegExp(domUtils.fillChar + '|<\/hr>', 'gi'); // ie会产生多余的</hr>
  13680. var noNeedFillCharTags = {
  13681. ol: 1,
  13682. ul: 1,
  13683. table: 1,
  13684. tbody: 1,
  13685. tr: 1,
  13686. body: 1
  13687. };
  13688. var orgState = me.options.autoClearEmptyNode;
  13689. function compareAddr(indexA, indexB) {
  13690. if (indexA.length != indexB.length)
  13691. return 0;
  13692. for (var i = 0, l = indexA.length; i < l; i++) {
  13693. if (indexA[i] != indexB[i])
  13694. return 0
  13695. }
  13696. return 1;
  13697. }
  13698. function compareRangeAddress(rngAddrA, rngAddrB) {
  13699. if (rngAddrA.collapsed != rngAddrB.collapsed) {
  13700. return 0;
  13701. }
  13702. if (!compareAddr(rngAddrA.startAddress, rngAddrB.startAddress) || !compareAddr(rngAddrA.endAddress, rngAddrB.endAddress)) {
  13703. return 0;
  13704. }
  13705. return 1;
  13706. }
  13707. function UndoManager() {
  13708. this.list = [];
  13709. this.index = 0;
  13710. this.hasUndo = false;
  13711. this.hasRedo = false;
  13712. this.undo = function () {
  13713. if (this.hasUndo) {
  13714. if (!this.list[this.index - 1] && this.list.length == 1) {
  13715. this.reset();
  13716. return;
  13717. }
  13718. while (this.list[this.index].content == this.list[this.index - 1].content) {
  13719. this.index--;
  13720. if (this.index == 0) {
  13721. return this.restore(0);
  13722. }
  13723. }
  13724. this.restore(--this.index);
  13725. }
  13726. };
  13727. this.redo = function () {
  13728. if (this.hasRedo) {
  13729. while (this.list[this.index].content == this.list[this.index + 1].content) {
  13730. this.index++;
  13731. if (this.index == this.list.length - 1) {
  13732. return this.restore(this.index);
  13733. }
  13734. }
  13735. this.restore(++this.index);
  13736. }
  13737. };
  13738. this.restore = function () {
  13739. var me = this.editor;
  13740. var scene = this.list[this.index];
  13741. var root = UE.htmlparser(scene.content.replace(fillchar, ''));
  13742. me.options.autoClearEmptyNode = false;
  13743. me.filterInputRule(root);
  13744. me.options.autoClearEmptyNode = orgState;
  13745. //trace:873
  13746. //去掉展位符
  13747. me.document.body.innerHTML = root.toHtml();
  13748. me.fireEvent('afterscencerestore');
  13749. //处理undo后空格不展位的问题
  13750. if (browser.ie) {
  13751. utils.each(domUtils.getElementsByTagName(me.document, 'td th caption p'), function (node) {
  13752. if (domUtils.isEmptyNode(node)) {
  13753. domUtils.fillNode(me.document, node);
  13754. }
  13755. })
  13756. }
  13757. try {
  13758. var rng = new dom.Range(me.document).moveToAddress(scene.address);
  13759. rng.select(noNeedFillCharTags[rng.startContainer.nodeName.toLowerCase()]);
  13760. } catch (e) {}
  13761. this.update();
  13762. this.clearKey();
  13763. //不能把自己reset了
  13764. me.fireEvent('reset', true);
  13765. };
  13766. this.getScene = function () {
  13767. var me = this.editor;
  13768. var rng = me.selection.getRange(),
  13769. rngAddress = rng.createAddress(false, true);
  13770. me.fireEvent('beforegetscene');
  13771. var root = UE.htmlparser(me.body.innerHTML);
  13772. me.options.autoClearEmptyNode = false;
  13773. me.filterOutputRule(root);
  13774. me.options.autoClearEmptyNode = orgState;
  13775. var cont = root.toHtml();
  13776. //trace:3461
  13777. //这个会引起回退时导致空格丢失的情况
  13778. // browser.ie && (cont = cont.replace(/>&nbsp;</g, '><').replace(/\s*</g, '<').replace(/>\s*/g, '>'));
  13779. me.fireEvent('aftergetscene');
  13780. return {
  13781. address: rngAddress,
  13782. content: cont
  13783. }
  13784. };
  13785. this.save = function (notCompareRange, notSetCursor) {
  13786. clearTimeout(saveSceneTimer);
  13787. var currentScene = this.getScene(notSetCursor),
  13788. lastScene = this.list[this.index];
  13789. if (lastScene && lastScene.content != currentScene.content) {
  13790. me.trigger('contentchange')
  13791. }
  13792. //内容相同位置相同不存
  13793. if (lastScene && lastScene.content == currentScene.content &&
  13794. (notCompareRange ? 1 : compareRangeAddress(lastScene.address, currentScene.address))
  13795. ) {
  13796. return;
  13797. }
  13798. this.list = this.list.slice(0, this.index + 1);
  13799. this.list.push(currentScene);
  13800. //如果大于最大数量了,就把最前的剔除
  13801. if (this.list.length > maxUndoCount) {
  13802. this.list.shift();
  13803. }
  13804. this.index = this.list.length - 1;
  13805. this.clearKey();
  13806. //跟新undo/redo状态
  13807. this.update();
  13808. };
  13809. this.update = function () {
  13810. this.hasRedo = !!this.list[this.index + 1];
  13811. this.hasUndo = !!this.list[this.index - 1];
  13812. };
  13813. this.reset = function () {
  13814. this.list = [];
  13815. this.index = 0;
  13816. this.hasUndo = false;
  13817. this.hasRedo = false;
  13818. this.clearKey();
  13819. };
  13820. this.clearKey = function () {
  13821. keycont = 0;
  13822. lastKeyCode = null;
  13823. };
  13824. }
  13825. me.undoManger = new UndoManager();
  13826. me.undoManger.editor = me;
  13827. function saveScene() {
  13828. this.undoManger.save();
  13829. }
  13830. me.addListener('saveScene', function () {
  13831. var args = Array.prototype.splice.call(arguments, 1);
  13832. this.undoManger.save.apply(this.undoManger, args);
  13833. });
  13834. // me.addListener('beforeexeccommand', saveScene);
  13835. // me.addListener('afterexeccommand', saveScene);
  13836. me.addListener('reset', function (type, exclude) {
  13837. if (!exclude) {
  13838. this.undoManger.reset();
  13839. }
  13840. });
  13841. me.commands['redo'] = me.commands['undo'] = {
  13842. execCommand: function (cmdName) {
  13843. this.undoManger[cmdName]();
  13844. },
  13845. queryCommandState: function (cmdName) {
  13846. return this.undoManger['has' + (cmdName.toLowerCase() == 'undo' ? 'Undo' : 'Redo')] ? 0 : -1;
  13847. },
  13848. notNeedUndo: 1
  13849. };
  13850. var keys = {
  13851. // /*Backspace*/ 8:1, /*Delete*/ 46:1,
  13852. /*Shift*/
  13853. 16: 1,
  13854. /*Ctrl*/ 17: 1,
  13855. /*Alt*/ 18: 1,
  13856. 37: 1,
  13857. 38: 1,
  13858. 39: 1,
  13859. 40: 1
  13860. },
  13861. keycont = 0,
  13862. lastKeyCode;
  13863. //输入法状态下不计算字符数
  13864. var inputType = false;
  13865. me.addListener('ready', function () {
  13866. domUtils.on(this.body, 'compositionstart', function () {
  13867. inputType = true;
  13868. });
  13869. domUtils.on(this.body, 'compositionend', function () {
  13870. inputType = false;
  13871. })
  13872. });
  13873. //快捷键
  13874. me.addshortcutkey({
  13875. "Undo": "ctrl+90", //undo
  13876. "Redo": "ctrl+89" //redo
  13877. });
  13878. var isCollapsed = true;
  13879. me.addListener('keydown', function (type, evt) {
  13880. var me = this;
  13881. var keyCode = evt.keyCode || evt.which;
  13882. if (!keys[keyCode] && !evt.ctrlKey && !evt.metaKey && !evt.shiftKey && !evt.altKey) {
  13883. if (inputType)
  13884. return;
  13885. if (!me.selection.getRange().collapsed) {
  13886. me.undoManger.save(false, true);
  13887. isCollapsed = false;
  13888. return;
  13889. }
  13890. if (me.undoManger.list.length == 0) {
  13891. me.undoManger.save(true);
  13892. }
  13893. clearTimeout(saveSceneTimer);
  13894. function save(cont) {
  13895. cont.undoManger.save(false, true);
  13896. cont.fireEvent('selectionchange');
  13897. }
  13898. saveSceneTimer = setTimeout(function () {
  13899. if (inputType) {
  13900. var interalTimer = setInterval(function () {
  13901. if (!inputType) {
  13902. save(me);
  13903. clearInterval(interalTimer)
  13904. }
  13905. }, 300)
  13906. return;
  13907. }
  13908. save(me);
  13909. }, 200);
  13910. lastKeyCode = keyCode;
  13911. keycont++;
  13912. if (keycont >= maxInputCount) {
  13913. save(me)
  13914. }
  13915. }
  13916. });
  13917. me.addListener('keyup', function (type, evt) {
  13918. var keyCode = evt.keyCode || evt.which;
  13919. if (!keys[keyCode] && !evt.ctrlKey && !evt.metaKey && !evt.shiftKey && !evt.altKey) {
  13920. if (inputType)
  13921. return;
  13922. if (!isCollapsed) {
  13923. this.undoManger.save(false, true);
  13924. isCollapsed = true;
  13925. }
  13926. }
  13927. });
  13928. //扩展实例,添加关闭和开启命令undo
  13929. me.stopCmdUndo = function () {
  13930. me.__hasEnterExecCommand = true;
  13931. };
  13932. me.startCmdUndo = function () {
  13933. me.__hasEnterExecCommand = false;
  13934. }
  13935. };
  13936. // plugins/copy.js
  13937. UE.plugin.register('copy', function () {
  13938. var me = this;
  13939. function initZeroClipboard() {
  13940. ZeroClipboard.config({
  13941. debug: false,
  13942. swfPath: me.options.UEDITOR_HOME_URL + 'third-party/zeroclipboard/ZeroClipboard.swf'
  13943. });
  13944. var client = me.zeroclipboard = new ZeroClipboard();
  13945. // 复制内容
  13946. client.on('copy', function (e) {
  13947. var client = e.client,
  13948. rng = me.selection.getRange(),
  13949. div = document.createElement('div');
  13950. div.appendChild(rng.cloneContents());
  13951. client.setText(div.innerText || div.textContent);
  13952. client.setHtml(div.innerHTML);
  13953. rng.select();
  13954. });
  13955. // hover事件传递到target
  13956. client.on('mouseover mouseout', function (e) {
  13957. var target = e.target;
  13958. if (e.type == 'mouseover') {
  13959. domUtils.addClass(target, 'edui-state-hover');
  13960. } else if (e.type == 'mouseout') {
  13961. domUtils.removeClasses(target, 'edui-state-hover');
  13962. }
  13963. });
  13964. // flash加载不成功
  13965. client.on('wrongflash noflash', function () {
  13966. ZeroClipboard.destroy();
  13967. });
  13968. }
  13969. return {
  13970. bindEvents: {
  13971. 'ready': function () {
  13972. if (!browser.ie) {
  13973. if (window.ZeroClipboard) {
  13974. initZeroClipboard();
  13975. } else {
  13976. utils.loadFile(document, {
  13977. src: me.options.UEDITOR_HOME_URL + "third-party/zeroclipboard/ZeroClipboard.js",
  13978. tag: "script",
  13979. type: "text/javascript",
  13980. defer: "defer"
  13981. }, function () {
  13982. initZeroClipboard();
  13983. });
  13984. }
  13985. }
  13986. }
  13987. },
  13988. commands: {
  13989. 'copy': {
  13990. execCommand: function (cmd) {
  13991. if (!me.document.execCommand('copy')) {
  13992. alert(me.getLang('copymsg'));
  13993. }
  13994. }
  13995. }
  13996. }
  13997. }
  13998. });
  13999. // plugins/paste.js
  14000. ///import core
  14001. ///import plugins/inserthtml.js
  14002. ///import plugins/undo.js
  14003. ///import plugins/serialize.js
  14004. ///commands 粘贴
  14005. ///commandsName PastePlain
  14006. ///commandsTitle 纯文本粘贴模式
  14007. /**
  14008. * @description 粘贴
  14009. * @author zhanyi
  14010. */
  14011. UE.plugins['paste'] = function () {
  14012. function getClipboardData(callback) {
  14013. var doc = this.document;
  14014. if (doc.getElementById('baidu_pastebin')) {
  14015. return;
  14016. }
  14017. var range = this.selection.getRange(),
  14018. bk = range.createBookmark(),
  14019. //创建剪贴的容器div
  14020. pastebin = doc.createElement('div');
  14021. pastebin.id = 'baidu_pastebin';
  14022. // Safari 要求div必须有内容,才能粘贴内容进来
  14023. browser.webkit && pastebin.appendChild(doc.createTextNode(domUtils.fillChar + domUtils.fillChar));
  14024. doc.body.appendChild(pastebin);
  14025. //trace:717 隐藏的span不能得到top
  14026. //bk.start.innerHTML = '&nbsp;';
  14027. bk.start.style.display = '';
  14028. pastebin.style.cssText = "position:absolute;width:1px;height:1px;overflow:hidden;left:-1000px;white-space:nowrap;top:" +
  14029. //要在现在光标平行的位置加入,否则会出现跳动的问题
  14030. domUtils.getXY(bk.start).y + 'px';
  14031. range.selectNodeContents(pastebin).select(true);
  14032. setTimeout(function () {
  14033. if (browser.webkit) {
  14034. for (var i = 0, pastebins = doc.querySelectorAll('#baidu_pastebin'), pi; pi = pastebins[i++];) {
  14035. if (domUtils.isEmptyNode(pi)) {
  14036. domUtils.remove(pi);
  14037. } else {
  14038. pastebin = pi;
  14039. break;
  14040. }
  14041. }
  14042. }
  14043. try {
  14044. pastebin.parentNode.removeChild(pastebin);
  14045. } catch (e) {}
  14046. range.moveToBookmark(bk).select(true);
  14047. callback(pastebin);
  14048. }, 0);
  14049. }
  14050. var me = this;
  14051. me.setOpt({
  14052. retainOnlyLabelPasted: false
  14053. });
  14054. var txtContent, htmlContent, address;
  14055. function getPureHtml(html) {
  14056. return html.replace(/<(\/?)([\w\-]+)([^>]*)>/gi, function (a, b, tagName, attrs) {
  14057. tagName = tagName.toLowerCase();
  14058. if ({
  14059. img: 1
  14060. } [tagName]) {
  14061. return a;
  14062. }
  14063. attrs = attrs.replace(/([\w\-]*?)\s*=\s*(("([^"]*)")|('([^']*)')|([^\s>]+))/gi, function (str, atr, val) {
  14064. if ({
  14065. 'src': 1,
  14066. 'href': 1,
  14067. 'name': 1
  14068. } [atr.toLowerCase()]) {
  14069. return atr + '=' + val + ' '
  14070. }
  14071. return ''
  14072. });
  14073. if ({
  14074. 'span': 1,
  14075. 'div': 1
  14076. } [tagName]) {
  14077. return ''
  14078. } else {
  14079. return '<' + b + tagName + ' ' + utils.trim(attrs) + '>'
  14080. }
  14081. });
  14082. }
  14083. function filter(div) {
  14084. var html;
  14085. if (div.firstChild) {
  14086. //去掉cut中添加的边界值
  14087. var nodes = domUtils.getElementsByTagName(div, 'span');
  14088. for (var i = 0, ni; ni = nodes[i++];) {
  14089. if (ni.id == '_baidu_cut_start' || ni.id == '_baidu_cut_end') {
  14090. domUtils.remove(ni);
  14091. }
  14092. }
  14093. if (browser.webkit) {
  14094. var brs = div.querySelectorAll('div br');
  14095. for (var i = 0, bi; bi = brs[i++];) {
  14096. var pN = bi.parentNode;
  14097. if (pN.tagName == 'DIV' && pN.childNodes.length == 1) {
  14098. pN.innerHTML = '<p><br/></p>';
  14099. domUtils.remove(pN);
  14100. }
  14101. }
  14102. var divs = div.querySelectorAll('#baidu_pastebin');
  14103. for (var i = 0, di; di = divs[i++];) {
  14104. var tmpP = me.document.createElement('p');
  14105. di.parentNode.insertBefore(tmpP, di);
  14106. while (di.firstChild) {
  14107. tmpP.appendChild(di.firstChild);
  14108. }
  14109. domUtils.remove(di);
  14110. }
  14111. var metas = div.querySelectorAll('meta');
  14112. for (var i = 0, ci; ci = metas[i++];) {
  14113. domUtils.remove(ci);
  14114. }
  14115. var brs = div.querySelectorAll('br');
  14116. for (i = 0; ci = brs[i++];) {
  14117. if (/^apple-/i.test(ci.className)) {
  14118. domUtils.remove(ci);
  14119. }
  14120. }
  14121. }
  14122. if (browser.gecko) {
  14123. var dirtyNodes = div.querySelectorAll('[_moz_dirty]');
  14124. for (i = 0; ci = dirtyNodes[i++];) {
  14125. ci.removeAttribute('_moz_dirty');
  14126. }
  14127. }
  14128. if (!browser.ie) {
  14129. var spans = div.querySelectorAll('span.Apple-style-span');
  14130. for (var i = 0, ci; ci = spans[i++];) {
  14131. domUtils.remove(ci, true);
  14132. }
  14133. }
  14134. //ie下使用innerHTML会产生多余的\r\n字符,也会产生&nbsp;这里过滤掉
  14135. html = div.innerHTML; //.replace(/>(?:(\s|&nbsp;)*?)</g,'><');
  14136. //过滤word粘贴过来的冗余属性
  14137. html = UE.filterWord(html);
  14138. //取消了忽略空白的第二个参数,粘贴过来的有些是有空白的,会被套上相关的标签
  14139. var root = UE.htmlparser(html);
  14140. //如果给了过滤规则就先进行过滤
  14141. if (me.options.filterRules) {
  14142. UE.filterNode(root, me.options.filterRules);
  14143. }
  14144. //执行默认的处理
  14145. me.filterInputRule(root);
  14146. //针对chrome的处理
  14147. if (browser.webkit) {
  14148. var br = root.lastChild();
  14149. if (br && br.type == 'element' && br.tagName == 'br') {
  14150. root.removeChild(br)
  14151. }
  14152. utils.each(me.body.querySelectorAll('div'), function (node) {
  14153. if (domUtils.isEmptyBlock(node)) {
  14154. domUtils.remove(node, true)
  14155. }
  14156. })
  14157. }
  14158. html = {
  14159. 'html': root.toHtml()
  14160. };
  14161. me.fireEvent('beforepaste', html, root);
  14162. //抢了默认的粘贴,那后边的内容就不执行了,比如表格粘贴
  14163. if (!html.html) {
  14164. return;
  14165. }
  14166. root = UE.htmlparser(html.html, true);
  14167. //如果开启了纯文本模式
  14168. if (me.queryCommandState('pasteplain') === 1) {
  14169. me.execCommand('insertHtml', UE.filterNode(root, me.options.filterTxtRules).toHtml(), true);
  14170. } else {
  14171. //文本模式
  14172. UE.filterNode(root, me.options.filterTxtRules);
  14173. txtContent = root.toHtml();
  14174. //完全模式
  14175. htmlContent = html.html;
  14176. address = me.selection.getRange().createAddress(true);
  14177. me.execCommand('insertHtml', me.getOpt('retainOnlyLabelPasted') === true ? getPureHtml(htmlContent) : htmlContent, true);
  14178. }
  14179. me.fireEvent("afterpaste", html);
  14180. }
  14181. }
  14182. me.addListener('pasteTransfer', function (cmd, plainType) {
  14183. if (address && txtContent && htmlContent && txtContent != htmlContent) {
  14184. var range = me.selection.getRange();
  14185. range.moveToAddress(address, true);
  14186. if (!range.collapsed) {
  14187. while (!domUtils.isBody(range.startContainer)) {
  14188. var start = range.startContainer;
  14189. if (start.nodeType == 1) {
  14190. start = start.childNodes[range.startOffset];
  14191. if (!start) {
  14192. range.setStartBefore(range.startContainer);
  14193. continue;
  14194. }
  14195. var pre = start.previousSibling;
  14196. if (pre && pre.nodeType == 3 && new RegExp('^[\n\r\t ' + domUtils.fillChar + ']*$').test(pre.nodeValue)) {
  14197. range.setStartBefore(pre)
  14198. }
  14199. }
  14200. if (range.startOffset == 0) {
  14201. range.setStartBefore(range.startContainer);
  14202. } else {
  14203. break;
  14204. }
  14205. }
  14206. while (!domUtils.isBody(range.endContainer)) {
  14207. var end = range.endContainer;
  14208. if (end.nodeType == 1) {
  14209. end = end.childNodes[range.endOffset];
  14210. if (!end) {
  14211. range.setEndAfter(range.endContainer);
  14212. continue;
  14213. }
  14214. var next = end.nextSibling;
  14215. if (next && next.nodeType == 3 && new RegExp('^[\n\r\t' + domUtils.fillChar + ']*$').test(next.nodeValue)) {
  14216. range.setEndAfter(next)
  14217. }
  14218. }
  14219. if (range.endOffset == range.endContainer[range.endContainer.nodeType == 3 ? 'nodeValue' : 'childNodes'].length) {
  14220. range.setEndAfter(range.endContainer);
  14221. } else {
  14222. break;
  14223. }
  14224. }
  14225. }
  14226. range.deleteContents();
  14227. range.select(true);
  14228. me.__hasEnterExecCommand = true;
  14229. var html = htmlContent;
  14230. if (plainType === 2) {
  14231. html = getPureHtml(html);
  14232. } else if (plainType) {
  14233. html = txtContent;
  14234. }
  14235. me.execCommand('inserthtml', html, true);
  14236. me.__hasEnterExecCommand = false;
  14237. var rng = me.selection.getRange();
  14238. while (!domUtils.isBody(rng.startContainer) && !rng.startOffset &&
  14239. rng.startContainer[rng.startContainer.nodeType == 3 ? 'nodeValue' : 'childNodes'].length
  14240. ) {
  14241. rng.setStartBefore(rng.startContainer);
  14242. }
  14243. var tmpAddress = rng.createAddress(true);
  14244. address.endAddress = tmpAddress.startAddress;
  14245. }
  14246. });
  14247. me.addListener('ready', function () {
  14248. domUtils.on(me.body, 'cut', function () {
  14249. var range = me.selection.getRange();
  14250. if (!range.collapsed && me.undoManger) {
  14251. me.undoManger.save();
  14252. }
  14253. });
  14254. //ie下beforepaste在点击右键时也会触发,所以用监控键盘才处理
  14255. domUtils.on(me.body, browser.ie || browser.opera ? 'keydown' : 'paste', function (e) {
  14256. if ((browser.ie || browser.opera) && ((!e.ctrlKey && !e.metaKey) || e.keyCode != '86')) {
  14257. return;
  14258. }
  14259. getClipboardData.call(me, function (div) {
  14260. filter(div);
  14261. });
  14262. });
  14263. });
  14264. me.commands['paste'] = {
  14265. execCommand: function (cmd) {
  14266. if (browser.ie) {
  14267. getClipboardData.call(me, function (div) {
  14268. filter(div);
  14269. });
  14270. me.document.execCommand('paste');
  14271. } else {
  14272. alert(me.getLang('pastemsg'));
  14273. }
  14274. }
  14275. }
  14276. };
  14277. // plugins/puretxtpaste.js
  14278. /**
  14279. * 纯文本粘贴插件
  14280. * @file
  14281. * @since 1.2.6.1
  14282. */
  14283. UE.plugins['pasteplain'] = function () {
  14284. var me = this;
  14285. me.setOpt({
  14286. 'pasteplain': false,
  14287. 'filterTxtRules': function () {
  14288. function transP(node) {
  14289. node.tagName = 'p';
  14290. node.setStyle();
  14291. }
  14292. function removeNode(node) {
  14293. node.parentNode.removeChild(node, true)
  14294. }
  14295. return {
  14296. //直接删除及其字节点内容
  14297. '-': 'script style object iframe embed input select',
  14298. 'p': {
  14299. $: {}
  14300. },
  14301. 'br': {
  14302. $: {}
  14303. },
  14304. div: function (node) {
  14305. var tmpNode, p = UE.uNode.createElement('p');
  14306. while (tmpNode = node.firstChild()) {
  14307. if (tmpNode.type == 'text' || !UE.dom.dtd.$block[tmpNode.tagName]) {
  14308. p.appendChild(tmpNode);
  14309. } else {
  14310. if (p.firstChild()) {
  14311. node.parentNode.insertBefore(p, node);
  14312. p = UE.uNode.createElement('p');
  14313. } else {
  14314. node.parentNode.insertBefore(tmpNode, node);
  14315. }
  14316. }
  14317. }
  14318. if (p.firstChild()) {
  14319. node.parentNode.insertBefore(p, node);
  14320. }
  14321. node.parentNode.removeChild(node);
  14322. },
  14323. ol: removeNode,
  14324. ul: removeNode,
  14325. dl: removeNode,
  14326. dt: removeNode,
  14327. dd: removeNode,
  14328. 'li': removeNode,
  14329. 'caption': transP,
  14330. 'th': transP,
  14331. 'tr': transP,
  14332. 'h1': transP,
  14333. 'h2': transP,
  14334. 'h3': transP,
  14335. 'h4': transP,
  14336. 'h5': transP,
  14337. 'h6': transP,
  14338. 'td': function (node) {
  14339. //没有内容的td直接删掉
  14340. var txt = !!node.innerText();
  14341. if (txt) {
  14342. node.parentNode.insertAfter(UE.uNode.createText(' &nbsp; &nbsp;'), node);
  14343. }
  14344. node.parentNode.removeChild(node, node.innerText())
  14345. }
  14346. }
  14347. }()
  14348. });
  14349. //暂时这里支持一下老版本的属性
  14350. var pasteplain = me.options.pasteplain;
  14351. /**
  14352. * 启用或取消纯文本粘贴模式
  14353. * @command pasteplain
  14354. * @method execCommand
  14355. * @param { String } cmd 命令字符串
  14356. * @example
  14357. * ```javascript
  14358. * editor.queryCommandState( 'pasteplain' );
  14359. * ```
  14360. */
  14361. /**
  14362. * 查询当前是否处于纯文本粘贴模式
  14363. * @command pasteplain
  14364. * @method queryCommandState
  14365. * @param { String } cmd 命令字符串
  14366. * @return { int } 如果处于纯文本模式,返回1,否则,返回0
  14367. * @example
  14368. * ```javascript
  14369. * editor.queryCommandState( 'pasteplain' );
  14370. * ```
  14371. */
  14372. me.commands['pasteplain'] = {
  14373. queryCommandState: function () {
  14374. return pasteplain ? 1 : 0;
  14375. },
  14376. execCommand: function () {
  14377. pasteplain = !pasteplain | 0;
  14378. },
  14379. notNeedUndo: 1
  14380. };
  14381. };
  14382. // plugins/list.js
  14383. /**
  14384. * 有序列表,无序列表插件
  14385. * @file
  14386. * @since 1.2.6.1
  14387. */
  14388. UE.plugins['list'] = function () {
  14389. var me = this,
  14390. notExchange = {
  14391. 'TD': 1,
  14392. 'PRE': 1,
  14393. 'BLOCKQUOTE': 1
  14394. };
  14395. var customStyle = {
  14396. 'cn': 'cn-1-',
  14397. 'cn1': 'cn-2-',
  14398. 'cn2': 'cn-3-',
  14399. 'num': 'num-1-',
  14400. 'num1': 'num-2-',
  14401. 'num2': 'num-3-',
  14402. 'dash': 'dash',
  14403. 'dot': 'dot'
  14404. };
  14405. me.setOpt({
  14406. 'autoTransWordToList': false,
  14407. 'insertorderedlist': {
  14408. 'num': '',
  14409. 'num1': '',
  14410. 'num2': '',
  14411. 'cn': '',
  14412. 'cn1': '',
  14413. 'cn2': '',
  14414. 'decimal': '',
  14415. 'lower-alpha': '',
  14416. 'lower-roman': '',
  14417. 'upper-alpha': '',
  14418. 'upper-roman': ''
  14419. },
  14420. 'insertunorderedlist': {
  14421. 'circle': '',
  14422. 'disc': '',
  14423. 'square': '',
  14424. 'dash': '',
  14425. 'dot': ''
  14426. },
  14427. listDefaultPaddingLeft: '30',
  14428. listiconpath: 'http://bs.baidu.com/listicon/',
  14429. maxListLevel: -1, //-1不限制
  14430. disablePInList: false
  14431. });
  14432. function listToArray(list) {
  14433. var arr = [];
  14434. for (var p in list) {
  14435. arr.push(p)
  14436. }
  14437. return arr;
  14438. }
  14439. var listStyle = {
  14440. 'OL': listToArray(me.options.insertorderedlist),
  14441. 'UL': listToArray(me.options.insertunorderedlist)
  14442. };
  14443. var liiconpath = me.options.listiconpath;
  14444. //根据用户配置,调整customStyle
  14445. for (var s in customStyle) {
  14446. if (!me.options.insertorderedlist.hasOwnProperty(s) && !me.options.insertunorderedlist.hasOwnProperty(s)) {
  14447. delete customStyle[s];
  14448. }
  14449. }
  14450. me.ready(function () {
  14451. var customCss = [];
  14452. for (var p in customStyle) {
  14453. if (p == 'dash' || p == 'dot') {
  14454. customCss.push('li.list-' + customStyle[p] + '{background-image:url(' + liiconpath + customStyle[p] + '.gif)}');
  14455. customCss.push('ul.custom_' + p + '{list-style:none;}ul.custom_' + p + ' li{background-position:0 3px;background-repeat:no-repeat}');
  14456. } else {
  14457. for (var i = 0; i < 99; i++) {
  14458. customCss.push('li.list-' + customStyle[p] + i + '{background-image:url(' + liiconpath + 'list-' + customStyle[p] + i + '.gif)}')
  14459. }
  14460. customCss.push('ol.custom_' + p + '{list-style:none;}ol.custom_' + p + ' li{background-position:0 3px;background-repeat:no-repeat}');
  14461. }
  14462. switch (p) {
  14463. case 'cn':
  14464. customCss.push('li.list-' + p + '-paddingleft-1{padding-left:25px}');
  14465. customCss.push('li.list-' + p + '-paddingleft-2{padding-left:40px}');
  14466. customCss.push('li.list-' + p + '-paddingleft-3{padding-left:55px}');
  14467. break;
  14468. case 'cn1':
  14469. customCss.push('li.list-' + p + '-paddingleft-1{padding-left:30px}');
  14470. customCss.push('li.list-' + p + '-paddingleft-2{padding-left:40px}');
  14471. customCss.push('li.list-' + p + '-paddingleft-3{padding-left:55px}');
  14472. break;
  14473. case 'cn2':
  14474. customCss.push('li.list-' + p + '-paddingleft-1{padding-left:40px}');
  14475. customCss.push('li.list-' + p + '-paddingleft-2{padding-left:55px}');
  14476. customCss.push('li.list-' + p + '-paddingleft-3{padding-left:68px}');
  14477. break;
  14478. case 'num':
  14479. case 'num1':
  14480. customCss.push('li.list-' + p + '-paddingleft-1{padding-left:25px}');
  14481. break;
  14482. case 'num2':
  14483. customCss.push('li.list-' + p + '-paddingleft-1{padding-left:35px}');
  14484. customCss.push('li.list-' + p + '-paddingleft-2{padding-left:40px}');
  14485. break;
  14486. case 'dash':
  14487. customCss.push('li.list-' + p + '-paddingleft{padding-left:35px}');
  14488. break;
  14489. case 'dot':
  14490. customCss.push('li.list-' + p + '-paddingleft{padding-left:20px}');
  14491. }
  14492. }
  14493. customCss.push('.list-paddingleft-1{padding-left:0}');
  14494. customCss.push('.list-paddingleft-2{padding-left:' + me.options.listDefaultPaddingLeft + 'px}');
  14495. customCss.push('.list-paddingleft-3{padding-left:' + me.options.listDefaultPaddingLeft * 2 + 'px}');
  14496. //如果不给宽度会在自定应样式里出现滚动条
  14497. utils.cssRule('list', 'ol,ul{margin:0;pading:0;' + (browser.ie ? '' : 'width:95%') + '}li{clear:both;}' + customCss.join('\n'), me.document);
  14498. });
  14499. //单独处理剪切的问题
  14500. me.ready(function () {
  14501. domUtils.on(me.body, 'cut', function () {
  14502. setTimeout(function () {
  14503. var rng = me.selection.getRange(),
  14504. li;
  14505. //trace:3416
  14506. if (!rng.collapsed) {
  14507. if (li = domUtils.findParentByTagName(rng.startContainer, 'li', true)) {
  14508. if (!li.nextSibling && domUtils.isEmptyBlock(li)) {
  14509. var pn = li.parentNode,
  14510. node;
  14511. if (node = pn.previousSibling) {
  14512. domUtils.remove(pn);
  14513. rng.setStartAtLast(node).collapse(true);
  14514. rng.select(true);
  14515. } else if (node = pn.nextSibling) {
  14516. domUtils.remove(pn);
  14517. rng.setStartAtFirst(node).collapse(true);
  14518. rng.select(true);
  14519. } else {
  14520. var tmpNode = me.document.createElement('p');
  14521. domUtils.fillNode(me.document, tmpNode);
  14522. pn.parentNode.insertBefore(tmpNode, pn);
  14523. domUtils.remove(pn);
  14524. rng.setStart(tmpNode, 0).collapse(true);
  14525. rng.select(true);
  14526. }
  14527. }
  14528. }
  14529. }
  14530. })
  14531. })
  14532. });
  14533. function getStyle(node) {
  14534. var cls = node.className;
  14535. if (domUtils.hasClass(node, /custom_/)) {
  14536. return cls.match(/custom_(\w+)/)[1]
  14537. }
  14538. return domUtils.getStyle(node, 'list-style-type')
  14539. }
  14540. me.addListener('beforepaste', function (type, html) {
  14541. var me = this,
  14542. rng = me.selection.getRange(),
  14543. li;
  14544. var root = UE.htmlparser(html.html, true);
  14545. if (li = domUtils.findParentByTagName(rng.startContainer, 'li', true)) {
  14546. var list = li.parentNode,
  14547. tagName = list.tagName == 'OL' ? 'ul' : 'ol';
  14548. utils.each(root.getNodesByTagName(tagName), function (n) {
  14549. n.tagName = list.tagName;
  14550. n.setAttr();
  14551. if (n.parentNode === root) {
  14552. type = getStyle(list) || (list.tagName == 'OL' ? 'decimal' : 'disc')
  14553. } else {
  14554. var className = n.parentNode.getAttr('class');
  14555. if (className && /custom_/.test(className)) {
  14556. type = className.match(/custom_(\w+)/)[1]
  14557. } else {
  14558. type = n.parentNode.getStyle('list-style-type');
  14559. }
  14560. if (!type) {
  14561. type = list.tagName == 'OL' ? 'decimal' : 'disc';
  14562. }
  14563. }
  14564. var index = utils.indexOf(listStyle[list.tagName], type);
  14565. if (n.parentNode !== root)
  14566. index = index + 1 == listStyle[list.tagName].length ? 0 : index + 1;
  14567. var currentStyle = listStyle[list.tagName][index];
  14568. if (customStyle[currentStyle]) {
  14569. n.setAttr('class', 'custom_' + currentStyle)
  14570. } else {
  14571. n.setStyle('list-style-type', currentStyle)
  14572. }
  14573. })
  14574. }
  14575. html.html = root.toHtml();
  14576. });
  14577. //导出时,去掉p标签
  14578. me.getOpt('disablePInList') === true && me.addOutputRule(function (root) {
  14579. utils.each(root.getNodesByTagName('li'), function (li) {
  14580. var newChildrens = [],
  14581. index = 0;
  14582. utils.each(li.children, function (n) {
  14583. if (n.tagName == 'p') {
  14584. var tmpNode;
  14585. while (tmpNode = n.children.pop()) {
  14586. newChildrens.splice(index, 0, tmpNode);
  14587. tmpNode.parentNode = li;
  14588. lastNode = tmpNode;
  14589. }
  14590. tmpNode = newChildrens[newChildrens.length - 1];
  14591. if (!tmpNode || tmpNode.type != 'element' || tmpNode.tagName != 'br') {
  14592. var br = UE.uNode.createElement('br');
  14593. br.parentNode = li;
  14594. newChildrens.push(br);
  14595. }
  14596. index = newChildrens.length;
  14597. }
  14598. });
  14599. if (newChildrens.length) {
  14600. li.children = newChildrens;
  14601. }
  14602. });
  14603. });
  14604. //进入编辑器的li要套p标签
  14605. me.addInputRule(function (root) {
  14606. utils.each(root.getNodesByTagName('li'), function (li) {
  14607. var tmpP = UE.uNode.createElement('p');
  14608. for (var i = 0, ci; ci = li.children[i];) {
  14609. if (ci.type == 'text' || dtd.p[ci.tagName]) {
  14610. tmpP.appendChild(ci);
  14611. } else {
  14612. if (tmpP.firstChild()) {
  14613. li.insertBefore(tmpP, ci);
  14614. tmpP = UE.uNode.createElement('p');
  14615. i = i + 2;
  14616. } else {
  14617. i++;
  14618. }
  14619. }
  14620. }
  14621. if (tmpP.firstChild() && !tmpP.parentNode || !li.firstChild()) {
  14622. li.appendChild(tmpP);
  14623. }
  14624. //trace:3357
  14625. //p不能为空
  14626. if (!tmpP.firstChild()) {
  14627. tmpP.innerHTML(browser.ie ? '&nbsp;' : '<br/>')
  14628. }
  14629. //去掉末尾的空白
  14630. var p = li.firstChild();
  14631. var lastChild = p.lastChild();
  14632. if (lastChild && lastChild.type == 'text' && /^\s*$/.test(lastChild.data)) {
  14633. p.removeChild(lastChild)
  14634. }
  14635. });
  14636. if (me.options.autoTransWordToList) {
  14637. var orderlisttype = {
  14638. 'num1': /^\d+\)/,
  14639. 'decimal': /^\d+\./,
  14640. 'lower-alpha': /^[a-z]+\)/,
  14641. 'upper-alpha': /^[A-Z]+\./,
  14642. 'cn': /^[\u4E00\u4E8C\u4E09\u56DB\u516d\u4e94\u4e03\u516b\u4e5d]+[\u3001]/,
  14643. 'cn2': /^\([\u4E00\u4E8C\u4E09\u56DB\u516d\u4e94\u4e03\u516b\u4e5d]+\)/
  14644. },
  14645. unorderlisttype = {
  14646. 'square': 'n'
  14647. };
  14648. function checkListType(content, container) {
  14649. var span = container.firstChild();
  14650. if (span && span.type == 'element' && span.tagName == 'span' && /Wingdings|Symbol/.test(span.getStyle('font-family'))) {
  14651. for (var p in unorderlisttype) {
  14652. if (unorderlisttype[p] == span.data) {
  14653. return p
  14654. }
  14655. }
  14656. return 'disc'
  14657. }
  14658. for (var p in orderlisttype) {
  14659. if (orderlisttype[p].test(content)) {
  14660. return p;
  14661. }
  14662. }
  14663. }
  14664. utils.each(root.getNodesByTagName('p'), function (node) {
  14665. if (node.getAttr('class') != 'MsoListParagraph') {
  14666. return
  14667. }
  14668. //word粘贴过来的会带有margin要去掉,但这样也可能会误命中一些央视
  14669. node.setStyle('margin', '');
  14670. node.setStyle('margin-left', '');
  14671. node.setAttr('class', '');
  14672. function appendLi(list, p, type) {
  14673. if (list.tagName == 'ol') {
  14674. if (browser.ie) {
  14675. var first = p.firstChild();
  14676. if (first.type == 'element' && first.tagName == 'span' && orderlisttype[type].test(first.innerText())) {
  14677. p.removeChild(first);
  14678. }
  14679. } else {
  14680. p.innerHTML(p.innerHTML().replace(orderlisttype[type], ''));
  14681. }
  14682. } else {
  14683. p.removeChild(p.firstChild())
  14684. }
  14685. var li = UE.uNode.createElement('li');
  14686. li.appendChild(p);
  14687. list.appendChild(li);
  14688. }
  14689. var tmp = node,
  14690. type, cacheNode = node;
  14691. if (node.parentNode.tagName != 'li' && (type = checkListType(node.innerText(), node))) {
  14692. var list = UE.uNode.createElement(me.options.insertorderedlist.hasOwnProperty(type) ? 'ol' : 'ul');
  14693. if (customStyle[type]) {
  14694. list.setAttr('class', 'custom_' + type)
  14695. } else {
  14696. list.setStyle('list-style-type', type)
  14697. }
  14698. while (node && node.parentNode.tagName != 'li' && checkListType(node.innerText(), node)) {
  14699. tmp = node.nextSibling();
  14700. if (!tmp) {
  14701. node.parentNode.insertBefore(list, node)
  14702. }
  14703. appendLi(list, node, type);
  14704. node = tmp;
  14705. }
  14706. if (!list.parentNode && node && node.parentNode) {
  14707. node.parentNode.insertBefore(list, node)
  14708. }
  14709. }
  14710. var span = cacheNode.firstChild();
  14711. if (span && span.type == 'element' && span.tagName == 'span' && /^\s*(&nbsp;)+\s*$/.test(span.innerText())) {
  14712. span.parentNode.removeChild(span)
  14713. }
  14714. })
  14715. }
  14716. });
  14717. //调整索引标签
  14718. me.addListener('contentchange', function () {
  14719. adjustListStyle(me.document)
  14720. });
  14721. function adjustListStyle(doc, ignore) {
  14722. utils.each(domUtils.getElementsByTagName(doc, 'ol ul'), function (node) {
  14723. if (!domUtils.inDoc(node, doc))
  14724. return;
  14725. var parent = node.parentNode;
  14726. if (parent.tagName == node.tagName) {
  14727. var nodeStyleType = getStyle(node) || (node.tagName == 'OL' ? 'decimal' : 'disc'),
  14728. parentStyleType = getStyle(parent) || (parent.tagName == 'OL' ? 'decimal' : 'disc');
  14729. if (nodeStyleType == parentStyleType) {
  14730. var styleIndex = utils.indexOf(listStyle[node.tagName], nodeStyleType);
  14731. styleIndex = styleIndex + 1 == listStyle[node.tagName].length ? 0 : styleIndex + 1;
  14732. setListStyle(node, listStyle[node.tagName][styleIndex])
  14733. }
  14734. }
  14735. var index = 0,
  14736. type = 2;
  14737. if (domUtils.hasClass(node, /custom_/)) {
  14738. if (!(/[ou]l/i.test(parent.tagName) && domUtils.hasClass(parent, /custom_/))) {
  14739. type = 1;
  14740. }
  14741. } else {
  14742. if (/[ou]l/i.test(parent.tagName) && domUtils.hasClass(parent, /custom_/)) {
  14743. type = 3;
  14744. }
  14745. }
  14746. var style = domUtils.getStyle(node, 'list-style-type');
  14747. style && (node.style.cssText = 'list-style-type:' + style);
  14748. node.className = utils.trim(node.className.replace(/list-paddingleft-\w+/, '')) + ' list-paddingleft-' + type;
  14749. utils.each(domUtils.getElementsByTagName(node, 'li'), function (li) {
  14750. li.style.cssText && (li.style.cssText = '');
  14751. if (!li.firstChild) {
  14752. domUtils.remove(li);
  14753. return;
  14754. }
  14755. if (li.parentNode !== node) {
  14756. return;
  14757. }
  14758. index++;
  14759. if (domUtils.hasClass(node, /custom_/)) {
  14760. var paddingLeft = 1,
  14761. currentStyle = getStyle(node);
  14762. if (node.tagName == 'OL') {
  14763. if (currentStyle) {
  14764. switch (currentStyle) {
  14765. case 'cn':
  14766. case 'cn1':
  14767. case 'cn2':
  14768. if (index > 10 && (index % 10 == 0 || index > 10 && index < 20)) {
  14769. paddingLeft = 2
  14770. } else if (index > 20) {
  14771. paddingLeft = 3
  14772. }
  14773. break;
  14774. case 'num2':
  14775. if (index > 9) {
  14776. paddingLeft = 2
  14777. }
  14778. }
  14779. }
  14780. li.className = 'list-' + customStyle[currentStyle] + index + ' ' + 'list-' + currentStyle + '-paddingleft-' + paddingLeft;
  14781. } else {
  14782. li.className = 'list-' + customStyle[currentStyle] + ' ' + 'list-' + currentStyle + '-paddingleft';
  14783. }
  14784. } else {
  14785. li.className = li.className.replace(/list-[\w\-]+/gi, '');
  14786. }
  14787. var className = li.getAttribute('class');
  14788. if (className !== null && !className.replace(/\s/g, '')) {
  14789. domUtils.removeAttributes(li, 'class')
  14790. }
  14791. });
  14792. !ignore && adjustList(node, node.tagName.toLowerCase(), getStyle(node) || domUtils.getStyle(node, 'list-style-type'), true);
  14793. })
  14794. }
  14795. function adjustList(list, tag, style, ignoreEmpty) {
  14796. var nextList = list.nextSibling;
  14797. if (nextList && nextList.nodeType == 1 && nextList.tagName.toLowerCase() == tag && (getStyle(nextList) || domUtils.getStyle(nextList, 'list-style-type') || (tag == 'ol' ? 'decimal' : 'disc')) == style) {
  14798. domUtils.moveChild(nextList, list);
  14799. if (nextList.childNodes.length == 0) {
  14800. domUtils.remove(nextList);
  14801. }
  14802. }
  14803. if (nextList && domUtils.isFillChar(nextList)) {
  14804. domUtils.remove(nextList);
  14805. }
  14806. var preList = list.previousSibling;
  14807. if (preList && preList.nodeType == 1 && preList.tagName.toLowerCase() == tag && (getStyle(preList) || domUtils.getStyle(preList, 'list-style-type') || (tag == 'ol' ? 'decimal' : 'disc')) == style) {
  14808. domUtils.moveChild(list, preList);
  14809. }
  14810. if (preList && domUtils.isFillChar(preList)) {
  14811. domUtils.remove(preList);
  14812. }!ignoreEmpty && domUtils.isEmptyBlock(list) && domUtils.remove(list);
  14813. if (getStyle(list)) {
  14814. adjustListStyle(list.ownerDocument, true)
  14815. }
  14816. }
  14817. function setListStyle(list, style) {
  14818. if (customStyle[style]) {
  14819. list.className = 'custom_' + style;
  14820. }
  14821. try {
  14822. domUtils.setStyle(list, 'list-style-type', style);
  14823. } catch (e) {}
  14824. }
  14825. function clearEmptySibling(node) {
  14826. var tmpNode = node.previousSibling;
  14827. if (tmpNode && domUtils.isEmptyBlock(tmpNode)) {
  14828. domUtils.remove(tmpNode);
  14829. }
  14830. tmpNode = node.nextSibling;
  14831. if (tmpNode && domUtils.isEmptyBlock(tmpNode)) {
  14832. domUtils.remove(tmpNode);
  14833. }
  14834. }
  14835. me.addListener('keydown', function (type, evt) {
  14836. function preventAndSave() {
  14837. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
  14838. me.fireEvent('contentchange');
  14839. me.undoManger && me.undoManger.save();
  14840. }
  14841. function findList(node, filterFn) {
  14842. while (node && !domUtils.isBody(node)) {
  14843. if (filterFn(node)) {
  14844. return null
  14845. }
  14846. if (node.nodeType == 1 && /[ou]l/i.test(node.tagName)) {
  14847. return node;
  14848. }
  14849. node = node.parentNode;
  14850. }
  14851. return null;
  14852. }
  14853. var keyCode = evt.keyCode || evt.which;
  14854. if (keyCode == 13 && !evt.shiftKey) { //回车
  14855. var rng = me.selection.getRange(),
  14856. parent = domUtils.findParent(rng.startContainer, function (node) {
  14857. return domUtils.isBlockElm(node)
  14858. }, true),
  14859. li = domUtils.findParentByTagName(rng.startContainer, 'li', true);
  14860. if (parent && parent.tagName != 'PRE' && !li) {
  14861. var html = parent.innerHTML.replace(new RegExp(domUtils.fillChar, 'g'), '');
  14862. if (/^\s*1\s*\.[^\d]/.test(html)) {
  14863. parent.innerHTML = html.replace(/^\s*1\s*\./, '');
  14864. rng.setStartAtLast(parent).collapse(true).select();
  14865. me.__hasEnterExecCommand = true;
  14866. me.execCommand('insertorderedlist');
  14867. me.__hasEnterExecCommand = false;
  14868. }
  14869. }
  14870. var range = me.selection.getRange(),
  14871. start = findList(range.startContainer, function (node) {
  14872. return node.tagName == 'TABLE';
  14873. }),
  14874. end = range.collapsed ? start : findList(range.endContainer, function (node) {
  14875. return node.tagName == 'TABLE';
  14876. });
  14877. if (start && end && start === end) {
  14878. if (!range.collapsed) {
  14879. start = domUtils.findParentByTagName(range.startContainer, 'li', true);
  14880. end = domUtils.findParentByTagName(range.endContainer, 'li', true);
  14881. if (start && end && start === end) {
  14882. range.deleteContents();
  14883. li = domUtils.findParentByTagName(range.startContainer, 'li', true);
  14884. if (li && domUtils.isEmptyBlock(li)) {
  14885. pre = li.previousSibling;
  14886. next = li.nextSibling;
  14887. p = me.document.createElement('p');
  14888. domUtils.fillNode(me.document, p);
  14889. parentList = li.parentNode;
  14890. if (pre && next) {
  14891. range.setStart(next, 0).collapse(true).select(true);
  14892. domUtils.remove(li);
  14893. } else {
  14894. if (!pre && !next || !pre) {
  14895. parentList.parentNode.insertBefore(p, parentList);
  14896. } else {
  14897. li.parentNode.parentNode.insertBefore(p, parentList.nextSibling);
  14898. }
  14899. domUtils.remove(li);
  14900. if (!parentList.firstChild) {
  14901. domUtils.remove(parentList);
  14902. }
  14903. range.setStart(p, 0).setCursor();
  14904. }
  14905. preventAndSave();
  14906. return;
  14907. }
  14908. } else {
  14909. var tmpRange = range.cloneRange(),
  14910. bk = tmpRange.collapse(false).createBookmark();
  14911. range.deleteContents();
  14912. tmpRange.moveToBookmark(bk);
  14913. var li = domUtils.findParentByTagName(tmpRange.startContainer, 'li', true);
  14914. clearEmptySibling(li);
  14915. tmpRange.select();
  14916. preventAndSave();
  14917. return;
  14918. }
  14919. }
  14920. li = domUtils.findParentByTagName(range.startContainer, 'li', true);
  14921. if (li) {
  14922. if (domUtils.isEmptyBlock(li)) {
  14923. bk = range.createBookmark();
  14924. var parentList = li.parentNode;
  14925. if (li !== parentList.lastChild) {
  14926. domUtils.breakParent(li, parentList);
  14927. clearEmptySibling(li);
  14928. } else {
  14929. parentList.parentNode.insertBefore(li, parentList.nextSibling);
  14930. if (domUtils.isEmptyNode(parentList)) {
  14931. domUtils.remove(parentList);
  14932. }
  14933. }
  14934. //嵌套不处理
  14935. if (!dtd.$list[li.parentNode.tagName]) {
  14936. if (!domUtils.isBlockElm(li.firstChild)) {
  14937. p = me.document.createElement('p');
  14938. li.parentNode.insertBefore(p, li);
  14939. while (li.firstChild) {
  14940. p.appendChild(li.firstChild);
  14941. }
  14942. domUtils.remove(li);
  14943. } else {
  14944. domUtils.remove(li, true);
  14945. }
  14946. }
  14947. range.moveToBookmark(bk).select();
  14948. } else {
  14949. var first = li.firstChild;
  14950. if (!first || !domUtils.isBlockElm(first)) {
  14951. var p = me.document.createElement('p');
  14952. !li.firstChild && domUtils.fillNode(me.document, p);
  14953. while (li.firstChild) {
  14954. p.appendChild(li.firstChild);
  14955. }
  14956. li.appendChild(p);
  14957. first = p;
  14958. }
  14959. var span = me.document.createElement('span');
  14960. range.insertNode(span);
  14961. domUtils.breakParent(span, li);
  14962. var nextLi = span.nextSibling;
  14963. first = nextLi.firstChild;
  14964. if (!first) {
  14965. p = me.document.createElement('p');
  14966. domUtils.fillNode(me.document, p);
  14967. nextLi.appendChild(p);
  14968. first = p;
  14969. }
  14970. if (domUtils.isEmptyNode(first)) {
  14971. first.innerHTML = '';
  14972. domUtils.fillNode(me.document, first);
  14973. }
  14974. range.setStart(first, 0).collapse(true).shrinkBoundary().select();
  14975. domUtils.remove(span);
  14976. var pre = nextLi.previousSibling;
  14977. if (pre && domUtils.isEmptyBlock(pre)) {
  14978. pre.innerHTML = '<p></p>';
  14979. domUtils.fillNode(me.document, pre.firstChild);
  14980. }
  14981. }
  14982. // }
  14983. preventAndSave();
  14984. }
  14985. }
  14986. }
  14987. if (keyCode == 8) {
  14988. //修中ie中li下的问题
  14989. range = me.selection.getRange();
  14990. if (range.collapsed && domUtils.isStartInblock(range)) {
  14991. tmpRange = range.cloneRange().trimBoundary();
  14992. li = domUtils.findParentByTagName(range.startContainer, 'li', true);
  14993. //要在li的最左边,才能处理
  14994. if (li && domUtils.isStartInblock(tmpRange)) {
  14995. start = domUtils.findParentByTagName(range.startContainer, 'p', true);
  14996. if (start && start !== li.firstChild) {
  14997. var parentList = domUtils.findParentByTagName(start, ['ol', 'ul']);
  14998. domUtils.breakParent(start, parentList);
  14999. clearEmptySibling(start);
  15000. me.fireEvent('contentchange');
  15001. range.setStart(start, 0).setCursor(false, true);
  15002. me.fireEvent('saveScene');
  15003. domUtils.preventDefault(evt);
  15004. return;
  15005. }
  15006. if (li && (pre = li.previousSibling)) {
  15007. if (keyCode == 46 && li.childNodes.length) {
  15008. return;
  15009. }
  15010. //有可能上边的兄弟节点是个2级菜单,要追加到2级菜单的最后的li
  15011. if (dtd.$list[pre.tagName]) {
  15012. pre = pre.lastChild;
  15013. }
  15014. me.undoManger && me.undoManger.save();
  15015. first = li.firstChild;
  15016. if (domUtils.isBlockElm(first)) {
  15017. if (domUtils.isEmptyNode(first)) {
  15018. // range.setEnd(pre, pre.childNodes.length).shrinkBoundary().collapse().select(true);
  15019. pre.appendChild(first);
  15020. range.setStart(first, 0).setCursor(false, true);
  15021. //first不是唯一的节点
  15022. while (li.firstChild) {
  15023. pre.appendChild(li.firstChild);
  15024. }
  15025. } else {
  15026. span = me.document.createElement('span');
  15027. range.insertNode(span);
  15028. //判断pre是否是空的节点,如果是<p><br/></p>类型的空节点,干掉p标签防止它占位
  15029. if (domUtils.isEmptyBlock(pre)) {
  15030. pre.innerHTML = '';
  15031. }
  15032. domUtils.moveChild(li, pre);
  15033. range.setStartBefore(span).collapse(true).select(true);
  15034. domUtils.remove(span);
  15035. }
  15036. } else {
  15037. if (domUtils.isEmptyNode(li)) {
  15038. var p = me.document.createElement('p');
  15039. pre.appendChild(p);
  15040. range.setStart(p, 0).setCursor();
  15041. // range.setEnd(pre, pre.childNodes.length).shrinkBoundary().collapse().select(true);
  15042. } else {
  15043. range.setEnd(pre, pre.childNodes.length).collapse().select(true);
  15044. while (li.firstChild) {
  15045. pre.appendChild(li.firstChild);
  15046. }
  15047. }
  15048. }
  15049. domUtils.remove(li);
  15050. me.fireEvent('contentchange');
  15051. me.fireEvent('saveScene');
  15052. domUtils.preventDefault(evt);
  15053. return;
  15054. }
  15055. //trace:980
  15056. if (li && !li.previousSibling) {
  15057. var parentList = li.parentNode;
  15058. var bk = range.createBookmark();
  15059. if (domUtils.isTagNode(parentList.parentNode, 'ol ul')) {
  15060. parentList.parentNode.insertBefore(li, parentList);
  15061. if (domUtils.isEmptyNode(parentList)) {
  15062. domUtils.remove(parentList)
  15063. }
  15064. } else {
  15065. while (li.firstChild) {
  15066. parentList.parentNode.insertBefore(li.firstChild, parentList);
  15067. }
  15068. domUtils.remove(li);
  15069. if (domUtils.isEmptyNode(parentList)) {
  15070. domUtils.remove(parentList)
  15071. }
  15072. }
  15073. range.moveToBookmark(bk).setCursor(false, true);
  15074. me.fireEvent('contentchange');
  15075. me.fireEvent('saveScene');
  15076. domUtils.preventDefault(evt);
  15077. return;
  15078. }
  15079. }
  15080. }
  15081. }
  15082. });
  15083. me.addListener('keyup', function (type, evt) {
  15084. var keyCode = evt.keyCode || evt.which;
  15085. if (keyCode == 8) {
  15086. var rng = me.selection.getRange(),
  15087. list;
  15088. if (list = domUtils.findParentByTagName(rng.startContainer, ['ol', 'ul'], true)) {
  15089. adjustList(list, list.tagName.toLowerCase(), getStyle(list) || domUtils.getComputedStyle(list, 'list-style-type'), true)
  15090. }
  15091. }
  15092. });
  15093. //处理tab键
  15094. me.addListener('tabkeydown', function () {
  15095. var range = me.selection.getRange();
  15096. //控制级数
  15097. function checkLevel(li) {
  15098. if (me.options.maxListLevel != -1) {
  15099. var level = li.parentNode,
  15100. levelNum = 0;
  15101. while (/[ou]l/i.test(level.tagName)) {
  15102. levelNum++;
  15103. level = level.parentNode;
  15104. }
  15105. if (levelNum >= me.options.maxListLevel) {
  15106. return true;
  15107. }
  15108. }
  15109. }
  15110. //只以开始为准
  15111. //todo 后续改进
  15112. var li = domUtils.findParentByTagName(range.startContainer, 'li', true);
  15113. if (li) {
  15114. var bk;
  15115. if (range.collapsed) {
  15116. if (checkLevel(li))
  15117. return true;
  15118. var parentLi = li.parentNode,
  15119. list = me.document.createElement(parentLi.tagName),
  15120. index = utils.indexOf(listStyle[list.tagName], getStyle(parentLi) || domUtils.getComputedStyle(parentLi, 'list-style-type'));
  15121. index = index + 1 == listStyle[list.tagName].length ? 0 : index + 1;
  15122. var currentStyle = listStyle[list.tagName][index];
  15123. setListStyle(list, currentStyle);
  15124. if (domUtils.isStartInblock(range)) {
  15125. me.fireEvent('saveScene');
  15126. bk = range.createBookmark();
  15127. parentLi.insertBefore(list, li);
  15128. list.appendChild(li);
  15129. adjustList(list, list.tagName.toLowerCase(), currentStyle);
  15130. me.fireEvent('contentchange');
  15131. range.moveToBookmark(bk).select(true);
  15132. return true;
  15133. }
  15134. } else {
  15135. me.fireEvent('saveScene');
  15136. bk = range.createBookmark();
  15137. for (var i = 0, closeList, parents = domUtils.findParents(li), ci; ci = parents[i++];) {
  15138. if (domUtils.isTagNode(ci, 'ol ul')) {
  15139. closeList = ci;
  15140. break;
  15141. }
  15142. }
  15143. var current = li;
  15144. if (bk.end) {
  15145. while (current && !(domUtils.getPosition(current, bk.end) & domUtils.POSITION_FOLLOWING)) {
  15146. if (checkLevel(current)) {
  15147. current = domUtils.getNextDomNode(current, false, null, function (node) {
  15148. return node !== closeList
  15149. });
  15150. continue;
  15151. }
  15152. var parentLi = current.parentNode,
  15153. list = me.document.createElement(parentLi.tagName),
  15154. index = utils.indexOf(listStyle[list.tagName], getStyle(parentLi) || domUtils.getComputedStyle(parentLi, 'list-style-type'));
  15155. var currentIndex = index + 1 == listStyle[list.tagName].length ? 0 : index + 1;
  15156. var currentStyle = listStyle[list.tagName][currentIndex];
  15157. setListStyle(list, currentStyle);
  15158. parentLi.insertBefore(list, current);
  15159. while (current && !(domUtils.getPosition(current, bk.end) & domUtils.POSITION_FOLLOWING)) {
  15160. li = current.nextSibling;
  15161. list.appendChild(current);
  15162. if (!li || domUtils.isTagNode(li, 'ol ul')) {
  15163. if (li) {
  15164. while (li = li.firstChild) {
  15165. if (li.tagName == 'LI') {
  15166. break;
  15167. }
  15168. }
  15169. } else {
  15170. li = domUtils.getNextDomNode(current, false, null, function (node) {
  15171. return node !== closeList
  15172. });
  15173. }
  15174. break;
  15175. }
  15176. current = li;
  15177. }
  15178. adjustList(list, list.tagName.toLowerCase(), currentStyle);
  15179. current = li;
  15180. }
  15181. }
  15182. me.fireEvent('contentchange');
  15183. range.moveToBookmark(bk).select();
  15184. return true;
  15185. }
  15186. }
  15187. });
  15188. function getLi(start) {
  15189. while (start && !domUtils.isBody(start)) {
  15190. if (start.nodeName == 'TABLE') {
  15191. return null;
  15192. }
  15193. if (start.nodeName == 'LI') {
  15194. return start
  15195. }
  15196. start = start.parentNode;
  15197. }
  15198. }
  15199. /**
  15200. * 有序列表,与“insertunorderedlist”命令互斥
  15201. * @command insertorderedlist
  15202. * @method execCommand
  15203. * @param { String } command 命令字符串
  15204. * @param { String } style 插入的有序列表类型,值为:decimal,lower-alpha,lower-roman,upper-alpha,upper-roman,cn,cn1,cn2,num,num1,num2
  15205. * @example
  15206. * ```javascript
  15207. * editor.execCommand( 'insertorderedlist','decimal');
  15208. * ```
  15209. */
  15210. /**
  15211. * 查询当前选区内容是否有序列表
  15212. * @command insertorderedlist
  15213. * @method queryCommandState
  15214. * @param { String } cmd 命令字符串
  15215. * @return { int } 如果当前选区是有序列表返回1,否则返回0
  15216. * @example
  15217. * ```javascript
  15218. * editor.queryCommandState( 'insertorderedlist' );
  15219. * ```
  15220. */
  15221. /**
  15222. * 查询当前选区内容是否有序列表
  15223. * @command insertorderedlist
  15224. * @method queryCommandValue
  15225. * @param { String } cmd 命令字符串
  15226. * @return { String } 返回当前有序列表的类型,值为null或decimal,lower-alpha,lower-roman,upper-alpha,upper-roman,cn,cn1,cn2,num,num1,num2
  15227. * @example
  15228. * ```javascript
  15229. * editor.queryCommandValue( 'insertorderedlist' );
  15230. * ```
  15231. */
  15232. /**
  15233. * 无序列表,与“insertorderedlist”命令互斥
  15234. * @command insertunorderedlist
  15235. * @method execCommand
  15236. * @param { String } command 命令字符串
  15237. * @param { String } style 插入的无序列表类型,值为:circle,disc,square,dash,dot
  15238. * @example
  15239. * ```javascript
  15240. * editor.execCommand( 'insertunorderedlist','circle');
  15241. * ```
  15242. */
  15243. /**
  15244. * 查询当前是否有word文档粘贴进来的图片
  15245. * @command insertunorderedlist
  15246. * @method insertunorderedlist
  15247. * @param { String } command 命令字符串
  15248. * @return { int } 如果当前选区是无序列表返回1,否则返回0
  15249. * @example
  15250. * ```javascript
  15251. * editor.queryCommandState( 'insertunorderedlist' );
  15252. * ```
  15253. */
  15254. /**
  15255. * 查询当前选区内容是否有序列表
  15256. * @command insertunorderedlist
  15257. * @method queryCommandValue
  15258. * @param { String } command 命令字符串
  15259. * @return { String } 返回当前无序列表的类型,值为null或circle,disc,square,dash,dot
  15260. * @example
  15261. * ```javascript
  15262. * editor.queryCommandValue( 'insertunorderedlist' );
  15263. * ```
  15264. */
  15265. me.commands['insertorderedlist'] =
  15266. me.commands['insertunorderedlist'] = {
  15267. execCommand: function (command, style) {
  15268. if (!style) {
  15269. style = command.toLowerCase() == 'insertorderedlist' ? 'decimal' : 'disc';
  15270. }
  15271. var me = this,
  15272. range = this.selection.getRange(),
  15273. filterFn = function (node) {
  15274. return node.nodeType == 1 ? node.tagName.toLowerCase() != 'br' : !domUtils.isWhitespace(node);
  15275. },
  15276. tag = command.toLowerCase() == 'insertorderedlist' ? 'ol' : 'ul',
  15277. frag = me.document.createDocumentFragment();
  15278. //去掉是因为会出现选到末尾,导致adjustmentBoundary缩到ol/ul的位置
  15279. //range.shrinkBoundary();//.adjustmentBoundary();
  15280. range.adjustmentBoundary().shrinkBoundary();
  15281. var bko = range.createBookmark(true),
  15282. start = getLi(me.document.getElementById(bko.start)),
  15283. modifyStart = 0,
  15284. end = getLi(me.document.getElementById(bko.end)),
  15285. modifyEnd = 0,
  15286. startParent, endParent,
  15287. list, tmp;
  15288. if (start || end) {
  15289. start && (startParent = start.parentNode);
  15290. if (!bko.end) {
  15291. end = start;
  15292. }
  15293. end && (endParent = end.parentNode);
  15294. if (startParent === endParent) {
  15295. while (start !== end) {
  15296. tmp = start;
  15297. start = start.nextSibling;
  15298. if (!domUtils.isBlockElm(tmp.firstChild)) {
  15299. var p = me.document.createElement('p');
  15300. while (tmp.firstChild) {
  15301. p.appendChild(tmp.firstChild);
  15302. }
  15303. tmp.appendChild(p);
  15304. }
  15305. frag.appendChild(tmp);
  15306. }
  15307. tmp = me.document.createElement('span');
  15308. startParent.insertBefore(tmp, end);
  15309. if (!domUtils.isBlockElm(end.firstChild)) {
  15310. p = me.document.createElement('p');
  15311. while (end.firstChild) {
  15312. p.appendChild(end.firstChild);
  15313. }
  15314. end.appendChild(p);
  15315. }
  15316. frag.appendChild(end);
  15317. domUtils.breakParent(tmp, startParent);
  15318. if (domUtils.isEmptyNode(tmp.previousSibling)) {
  15319. domUtils.remove(tmp.previousSibling);
  15320. }
  15321. if (domUtils.isEmptyNode(tmp.nextSibling)) {
  15322. domUtils.remove(tmp.nextSibling)
  15323. }
  15324. var nodeStyle = getStyle(startParent) || domUtils.getComputedStyle(startParent, 'list-style-type') || (command.toLowerCase() == 'insertorderedlist' ? 'decimal' : 'disc');
  15325. if (startParent.tagName.toLowerCase() == tag && nodeStyle == style) {
  15326. for (var i = 0, ci, tmpFrag = me.document.createDocumentFragment(); ci = frag.firstChild;) {
  15327. if (domUtils.isTagNode(ci, 'ol ul')) {
  15328. // 删除时,子列表不处理
  15329. // utils.each(domUtils.getElementsByTagName(ci,'li'),function(li){
  15330. // while(li.firstChild){
  15331. // tmpFrag.appendChild(li.firstChild);
  15332. // }
  15333. //
  15334. // });
  15335. tmpFrag.appendChild(ci);
  15336. } else {
  15337. while (ci.firstChild) {
  15338. tmpFrag.appendChild(ci.firstChild);
  15339. domUtils.remove(ci);
  15340. }
  15341. }
  15342. }
  15343. tmp.parentNode.insertBefore(tmpFrag, tmp);
  15344. } else {
  15345. list = me.document.createElement(tag);
  15346. setListStyle(list, style);
  15347. list.appendChild(frag);
  15348. tmp.parentNode.insertBefore(list, tmp);
  15349. }
  15350. domUtils.remove(tmp);
  15351. list && adjustList(list, tag, style);
  15352. range.moveToBookmark(bko).select();
  15353. return;
  15354. }
  15355. //开始
  15356. if (start) {
  15357. while (start) {
  15358. tmp = start.nextSibling;
  15359. if (domUtils.isTagNode(start, 'ol ul')) {
  15360. frag.appendChild(start);
  15361. } else {
  15362. var tmpfrag = me.document.createDocumentFragment(),
  15363. hasBlock = 0;
  15364. while (start.firstChild) {
  15365. if (domUtils.isBlockElm(start.firstChild)) {
  15366. hasBlock = 1;
  15367. }
  15368. tmpfrag.appendChild(start.firstChild);
  15369. }
  15370. if (!hasBlock) {
  15371. var tmpP = me.document.createElement('p');
  15372. tmpP.appendChild(tmpfrag);
  15373. frag.appendChild(tmpP);
  15374. } else {
  15375. frag.appendChild(tmpfrag);
  15376. }
  15377. domUtils.remove(start);
  15378. }
  15379. start = tmp;
  15380. }
  15381. startParent.parentNode.insertBefore(frag, startParent.nextSibling);
  15382. if (domUtils.isEmptyNode(startParent)) {
  15383. range.setStartBefore(startParent);
  15384. domUtils.remove(startParent);
  15385. } else {
  15386. range.setStartAfter(startParent);
  15387. }
  15388. modifyStart = 1;
  15389. }
  15390. if (end && domUtils.inDoc(endParent, me.document)) {
  15391. //结束
  15392. start = endParent.firstChild;
  15393. while (start && start !== end) {
  15394. tmp = start.nextSibling;
  15395. if (domUtils.isTagNode(start, 'ol ul')) {
  15396. frag.appendChild(start);
  15397. } else {
  15398. tmpfrag = me.document.createDocumentFragment();
  15399. hasBlock = 0;
  15400. while (start.firstChild) {
  15401. if (domUtils.isBlockElm(start.firstChild)) {
  15402. hasBlock = 1;
  15403. }
  15404. tmpfrag.appendChild(start.firstChild);
  15405. }
  15406. if (!hasBlock) {
  15407. tmpP = me.document.createElement('p');
  15408. tmpP.appendChild(tmpfrag);
  15409. frag.appendChild(tmpP);
  15410. } else {
  15411. frag.appendChild(tmpfrag);
  15412. }
  15413. domUtils.remove(start);
  15414. }
  15415. start = tmp;
  15416. }
  15417. var tmpDiv = domUtils.createElement(me.document, 'div', {
  15418. 'tmpDiv': 1
  15419. });
  15420. domUtils.moveChild(end, tmpDiv);
  15421. frag.appendChild(tmpDiv);
  15422. domUtils.remove(end);
  15423. endParent.parentNode.insertBefore(frag, endParent);
  15424. range.setEndBefore(endParent);
  15425. if (domUtils.isEmptyNode(endParent)) {
  15426. domUtils.remove(endParent);
  15427. }
  15428. modifyEnd = 1;
  15429. }
  15430. }
  15431. if (!modifyStart) {
  15432. range.setStartBefore(me.document.getElementById(bko.start));
  15433. }
  15434. if (bko.end && !modifyEnd) {
  15435. range.setEndAfter(me.document.getElementById(bko.end));
  15436. }
  15437. range.enlarge(true, function (node) {
  15438. return notExchange[node.tagName];
  15439. });
  15440. frag = me.document.createDocumentFragment();
  15441. var bk = range.createBookmark(),
  15442. current = domUtils.getNextDomNode(bk.start, false, filterFn),
  15443. tmpRange = range.cloneRange(),
  15444. tmpNode,
  15445. block = domUtils.isBlockElm;
  15446. while (current && current !== bk.end && (domUtils.getPosition(current, bk.end) & domUtils.POSITION_PRECEDING)) {
  15447. if (current.nodeType == 3 || dtd.li[current.tagName]) {
  15448. if (current.nodeType == 1 && dtd.$list[current.tagName]) {
  15449. while (current.firstChild) {
  15450. frag.appendChild(current.firstChild);
  15451. }
  15452. tmpNode = domUtils.getNextDomNode(current, false, filterFn);
  15453. domUtils.remove(current);
  15454. current = tmpNode;
  15455. continue;
  15456. }
  15457. tmpNode = current;
  15458. tmpRange.setStartBefore(current);
  15459. while (current && current !== bk.end && (!block(current) || domUtils.isBookmarkNode(current))) {
  15460. tmpNode = current;
  15461. current = domUtils.getNextDomNode(current, false, null, function (node) {
  15462. return !notExchange[node.tagName];
  15463. });
  15464. }
  15465. if (current && block(current)) {
  15466. tmp = domUtils.getNextDomNode(tmpNode, false, filterFn);
  15467. if (tmp && domUtils.isBookmarkNode(tmp)) {
  15468. current = domUtils.getNextDomNode(tmp, false, filterFn);
  15469. tmpNode = tmp;
  15470. }
  15471. }
  15472. tmpRange.setEndAfter(tmpNode);
  15473. current = domUtils.getNextDomNode(tmpNode, false, filterFn);
  15474. var li = range.document.createElement('li');
  15475. li.appendChild(tmpRange.extractContents());
  15476. if (domUtils.isEmptyNode(li)) {
  15477. var tmpNode = range.document.createElement('p');
  15478. while (li.firstChild) {
  15479. tmpNode.appendChild(li.firstChild)
  15480. }
  15481. li.appendChild(tmpNode);
  15482. }
  15483. frag.appendChild(li);
  15484. } else {
  15485. current = domUtils.getNextDomNode(current, true, filterFn);
  15486. }
  15487. }
  15488. range.moveToBookmark(bk).collapse(true);
  15489. list = me.document.createElement(tag);
  15490. setListStyle(list, style);
  15491. list.appendChild(frag);
  15492. range.insertNode(list);
  15493. //当前list上下看能否合并
  15494. adjustList(list, tag, style);
  15495. //去掉冗余的tmpDiv
  15496. for (var i = 0, ci, tmpDivs = domUtils.getElementsByTagName(list, 'div'); ci = tmpDivs[i++];) {
  15497. if (ci.getAttribute('tmpDiv')) {
  15498. domUtils.remove(ci, true)
  15499. }
  15500. }
  15501. range.moveToBookmark(bko).select();
  15502. },
  15503. queryCommandState: function (command) {
  15504. var tag = command.toLowerCase() == 'insertorderedlist' ? 'ol' : 'ul';
  15505. var path = this.selection.getStartElementPath();
  15506. for (var i = 0, ci; ci = path[i++];) {
  15507. if (ci.nodeName == 'TABLE') {
  15508. return 0
  15509. }
  15510. if (tag == ci.nodeName.toLowerCase()) {
  15511. return 1
  15512. };
  15513. }
  15514. return 0;
  15515. },
  15516. queryCommandValue: function (command) {
  15517. var tag = command.toLowerCase() == 'insertorderedlist' ? 'ol' : 'ul';
  15518. var path = this.selection.getStartElementPath(),
  15519. node;
  15520. for (var i = 0, ci; ci = path[i++];) {
  15521. if (ci.nodeName == 'TABLE') {
  15522. node = null;
  15523. break;
  15524. }
  15525. if (tag == ci.nodeName.toLowerCase()) {
  15526. node = ci;
  15527. break;
  15528. };
  15529. }
  15530. return node ? getStyle(node) || domUtils.getComputedStyle(node, 'list-style-type') : null;
  15531. }
  15532. };
  15533. };
  15534. // plugins/source.js
  15535. /**
  15536. * 源码编辑插件
  15537. * @file
  15538. * @since 1.2.6.1
  15539. */
  15540. (function () {
  15541. var sourceEditors = {
  15542. textarea: function (editor, holder) {
  15543. var textarea = holder.ownerDocument.createElement('textarea');
  15544. textarea.style.cssText = 'position:absolute;resize:none;width:100%;height:100%;border:0;padding:0;margin:0;overflow-y:auto;';
  15545. // todo: IE下只有onresize属性可用... 很纠结
  15546. if (browser.ie && browser.version < 8) {
  15547. textarea.style.width = holder.offsetWidth + 'px';
  15548. textarea.style.height = holder.offsetHeight + 'px';
  15549. holder.onresize = function () {
  15550. textarea.style.width = holder.offsetWidth + 'px';
  15551. textarea.style.height = holder.offsetHeight + 'px';
  15552. };
  15553. }
  15554. holder.appendChild(textarea);
  15555. return {
  15556. setContent: function (content) {
  15557. textarea.value = content;
  15558. },
  15559. getContent: function () {
  15560. return textarea.value;
  15561. },
  15562. select: function () {
  15563. var range;
  15564. if (browser.ie) {
  15565. range = textarea.createTextRange();
  15566. range.collapse(true);
  15567. range.select();
  15568. } else {
  15569. //todo: chrome下无法设置焦点
  15570. textarea.setSelectionRange(0, 0);
  15571. textarea.focus();
  15572. }
  15573. },
  15574. dispose: function () {
  15575. holder.removeChild(textarea);
  15576. // todo
  15577. holder.onresize = null;
  15578. textarea = null;
  15579. holder = null;
  15580. }
  15581. };
  15582. },
  15583. codemirror: function (editor, holder) {
  15584. var codeEditor = window.CodeMirror(holder, {
  15585. mode: "text/html",
  15586. tabMode: "indent",
  15587. lineNumbers: true,
  15588. lineWrapping: true
  15589. });
  15590. var dom = codeEditor.getWrapperElement();
  15591. dom.style.cssText = 'position:absolute;left:0;top:0;width:100%;height:100%;font-family:consolas,"Courier new",monospace;font-size:13px;';
  15592. codeEditor.getScrollerElement().style.cssText = 'position:absolute;left:0;top:0;width:100%;height:100%;';
  15593. codeEditor.refresh();
  15594. return {
  15595. getCodeMirror: function () {
  15596. return codeEditor;
  15597. },
  15598. setContent: function (content) {
  15599. codeEditor.setValue(content);
  15600. },
  15601. getContent: function () {
  15602. return codeEditor.getValue();
  15603. },
  15604. select: function () {
  15605. codeEditor.focus();
  15606. },
  15607. dispose: function () {
  15608. holder.removeChild(dom);
  15609. dom = null;
  15610. codeEditor = null;
  15611. }
  15612. };
  15613. }
  15614. };
  15615. UE.plugins['source'] = function () {
  15616. var me = this;
  15617. var opt = this.options;
  15618. var sourceMode = false;
  15619. var sourceEditor;
  15620. var orgSetContent;
  15621. opt.sourceEditor = browser.ie ? 'textarea' : (opt.sourceEditor || 'codemirror');
  15622. me.setOpt({
  15623. sourceEditorFirst: false
  15624. });
  15625. function createSourceEditor(holder) {
  15626. return sourceEditors[opt.sourceEditor == 'codemirror' && window.CodeMirror ? 'codemirror' : 'textarea'](me, holder);
  15627. }
  15628. var bakCssText;
  15629. //解决在源码模式下getContent不能得到最新的内容问题
  15630. var oldGetContent,
  15631. bakAddress;
  15632. /**
  15633. * 切换源码模式和编辑模式
  15634. * @command source
  15635. * @method execCommand
  15636. * @param { String } cmd 命令字符串
  15637. * @example
  15638. * ```javascript
  15639. * editor.execCommand( 'source');
  15640. * ```
  15641. */
  15642. /**
  15643. * 查询当前编辑区域的状态是源码模式还是可视化模式
  15644. * @command source
  15645. * @method queryCommandState
  15646. * @param { String } cmd 命令字符串
  15647. * @return { int } 如果当前是源码编辑模式,返回1,否则返回0
  15648. * @example
  15649. * ```javascript
  15650. * editor.queryCommandState( 'source' );
  15651. * ```
  15652. */
  15653. me.commands['source'] = {
  15654. execCommand: function () {
  15655. sourceMode = !sourceMode;
  15656. if (sourceMode) {
  15657. bakAddress = me.selection.getRange().createAddress(false, true);
  15658. me.undoManger && me.undoManger.save(true);
  15659. if (browser.gecko) {
  15660. me.body.contentEditable = false;
  15661. }
  15662. bakCssText = me.iframe.style.cssText;
  15663. me.iframe.style.cssText += 'position:absolute;left:-32768px;top:-32768px;';
  15664. me.fireEvent('beforegetcontent');
  15665. var root = UE.htmlparser(me.body.innerHTML);
  15666. me.filterOutputRule(root);
  15667. root.traversal(function (node) {
  15668. if (node.type == 'element') {
  15669. switch (node.tagName) {
  15670. case 'td':
  15671. case 'th':
  15672. case 'caption':
  15673. if (node.children && node.children.length == 1) {
  15674. if (node.firstChild().tagName == 'br') {
  15675. node.removeChild(node.firstChild())
  15676. }
  15677. };
  15678. break;
  15679. case 'pre':
  15680. node.innerText(node.innerText().replace(/&nbsp;/g, ' '))
  15681. }
  15682. }
  15683. });
  15684. me.fireEvent('aftergetcontent');
  15685. var content = root.toHtml(true);
  15686. sourceEditor = createSourceEditor(me.iframe.parentNode);
  15687. sourceEditor.setContent(content);
  15688. orgSetContent = me.setContent;
  15689. me.setContent = function (html) {
  15690. //这里暂时不触发事件,防止报错
  15691. var root = UE.htmlparser(html);
  15692. me.filterInputRule(root);
  15693. html = root.toHtml();
  15694. sourceEditor.setContent(html);
  15695. };
  15696. setTimeout(function () {
  15697. sourceEditor.select();
  15698. me.addListener('fullscreenchanged', function () {
  15699. try {
  15700. sourceEditor.getCodeMirror().refresh()
  15701. } catch (e) {}
  15702. });
  15703. });
  15704. //重置getContent,源码模式下取值也能是最新的数据
  15705. oldGetContent = me.getContent;
  15706. me.getContent = function () {
  15707. return sourceEditor.getContent() || '<p>' + (browser.ie ? '' : '<br/>') + '</p>';
  15708. };
  15709. } else {
  15710. me.iframe.style.cssText = bakCssText;
  15711. var cont = sourceEditor.getContent() || '<p>' + (browser.ie ? '' : '<br/>') + '</p>';
  15712. //处理掉block节点前后的空格,有可能会误命中,暂时不考虑
  15713. cont = cont.replace(new RegExp('[\\r\\t\\n ]*<\/?(\\w+)\\s*(?:[^>]*)>', 'g'), function (a, b) {
  15714. if (b && !dtd.$inlineWithA[b.toLowerCase()]) {
  15715. return a.replace(/(^[\n\r\t ]*)|([\n\r\t ]*$)/g, '');
  15716. }
  15717. return a.replace(/(^[\n\r\t]*)|([\n\r\t]*$)/g, '')
  15718. });
  15719. me.setContent = orgSetContent;
  15720. me.setContent(cont);
  15721. sourceEditor.dispose();
  15722. sourceEditor = null;
  15723. //还原getContent方法
  15724. me.getContent = oldGetContent;
  15725. var first = me.body.firstChild;
  15726. //trace:1106 都删除空了,下边会报错,所以补充一个p占位
  15727. if (!first) {
  15728. me.body.innerHTML = '<p>' + (browser.ie ? '' : '<br/>') + '</p>';
  15729. first = me.body.firstChild;
  15730. }
  15731. //要在ifm为显示时ff才能取到selection,否则报错
  15732. //这里不能比较位置了
  15733. me.undoManger && me.undoManger.save(true);
  15734. if (browser.gecko) {
  15735. var input = document.createElement('input');
  15736. input.style.cssText = 'position:absolute;left:0;top:-32768px';
  15737. document.body.appendChild(input);
  15738. me.body.contentEditable = false;
  15739. setTimeout(function () {
  15740. domUtils.setViewportOffset(input, {
  15741. left: -32768,
  15742. top: 0
  15743. });
  15744. input.focus();
  15745. setTimeout(function () {
  15746. me.body.contentEditable = true;
  15747. me.selection.getRange().moveToAddress(bakAddress).select(true);
  15748. domUtils.remove(input);
  15749. });
  15750. });
  15751. } else {
  15752. //ie下有可能报错,比如在代码顶头的情况
  15753. try {
  15754. me.selection.getRange().moveToAddress(bakAddress).select(true);
  15755. } catch (e) {}
  15756. }
  15757. }
  15758. this.fireEvent('sourcemodechanged', sourceMode);
  15759. },
  15760. queryCommandState: function () {
  15761. return sourceMode | 0;
  15762. },
  15763. notNeedUndo: 1
  15764. };
  15765. var oldQueryCommandState = me.queryCommandState;
  15766. me.queryCommandState = function (cmdName) {
  15767. cmdName = cmdName.toLowerCase();
  15768. if (sourceMode) {
  15769. //源码模式下可以开启的命令
  15770. return cmdName in {
  15771. 'source': 1,
  15772. 'fullscreen': 1
  15773. } ? 1 : -1
  15774. }
  15775. return oldQueryCommandState.apply(this, arguments);
  15776. };
  15777. if (opt.sourceEditor == "codemirror") {
  15778. me.addListener("ready", function () {
  15779. utils.loadFile(document, {
  15780. src: opt.codeMirrorJsUrl || opt.UEDITOR_HOME_URL + "third-party/codemirror/codemirror.js",
  15781. tag: "script",
  15782. type: "text/javascript",
  15783. defer: "defer"
  15784. }, function () {
  15785. if (opt.sourceEditorFirst) {
  15786. setTimeout(function () {
  15787. me.execCommand("source");
  15788. }, 0);
  15789. }
  15790. });
  15791. utils.loadFile(document, {
  15792. tag: "link",
  15793. rel: "stylesheet",
  15794. type: "text/css",
  15795. href: opt.codeMirrorCssUrl || opt.UEDITOR_HOME_URL + "third-party/codemirror/codemirror.css"
  15796. });
  15797. });
  15798. }
  15799. };
  15800. })();
  15801. // plugins/enterkey.js
  15802. ///import core
  15803. ///import plugins/undo.js
  15804. ///commands 设置回车标签p或br
  15805. ///commandsName EnterKey
  15806. ///commandsTitle 设置回车标签p或br
  15807. /**
  15808. * @description 处理回车
  15809. * @author zhanyi
  15810. */
  15811. UE.plugins['enterkey'] = function () {
  15812. var hTag,
  15813. me = this,
  15814. tag = me.options.enterTag;
  15815. me.addListener('keyup', function (type, evt) {
  15816. var keyCode = evt.keyCode || evt.which;
  15817. if (keyCode == 13) {
  15818. var range = me.selection.getRange(),
  15819. start = range.startContainer,
  15820. doSave;
  15821. //修正在h1-h6里边回车后不能嵌套p的问题
  15822. if (!browser.ie) {
  15823. if (/h\d/i.test(hTag)) {
  15824. if (browser.gecko) {
  15825. var h = domUtils.findParentByTagName(start, ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote', 'caption', 'table'], true);
  15826. if (!h) {
  15827. me.document.execCommand('formatBlock', false, '<p>');
  15828. doSave = 1;
  15829. }
  15830. } else {
  15831. //chrome remove div
  15832. if (start.nodeType == 1) {
  15833. var tmp = me.document.createTextNode(''),
  15834. div;
  15835. range.insertNode(tmp);
  15836. div = domUtils.findParentByTagName(tmp, 'div', true);
  15837. if (div) {
  15838. var p = me.document.createElement('p');
  15839. while (div.firstChild) {
  15840. p.appendChild(div.firstChild);
  15841. }
  15842. div.parentNode.insertBefore(p, div);
  15843. domUtils.remove(div);
  15844. range.setStartBefore(tmp).setCursor();
  15845. doSave = 1;
  15846. }
  15847. domUtils.remove(tmp);
  15848. }
  15849. }
  15850. if (me.undoManger && doSave) {
  15851. me.undoManger.save();
  15852. }
  15853. }
  15854. //没有站位符,会出现多行的问题
  15855. browser.opera && range.select();
  15856. } else {
  15857. me.fireEvent('saveScene', true, true)
  15858. }
  15859. }
  15860. });
  15861. me.addListener('keydown', function (type, evt) {
  15862. var keyCode = evt.keyCode || evt.which;
  15863. if (keyCode == 13) { //回车
  15864. if (me.fireEvent('beforeenterkeydown')) {
  15865. domUtils.preventDefault(evt);
  15866. return;
  15867. }
  15868. me.fireEvent('saveScene', true, true);
  15869. hTag = '';
  15870. var range = me.selection.getRange();
  15871. if (!range.collapsed) {
  15872. //跨td不能删
  15873. var start = range.startContainer,
  15874. end = range.endContainer,
  15875. startTd = domUtils.findParentByTagName(start, 'td', true),
  15876. endTd = domUtils.findParentByTagName(end, 'td', true);
  15877. if (startTd && endTd && startTd !== endTd || !startTd && endTd || startTd && !endTd) {
  15878. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
  15879. return;
  15880. }
  15881. }
  15882. if (tag == 'p') {
  15883. if (!browser.ie) {
  15884. start = domUtils.findParentByTagName(range.startContainer, ['ol', 'ul', 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote', 'caption'], true);
  15885. //opera下执行formatblock会在table的场景下有问题,回车在opera原生支持很好,所以暂时在opera去掉调用这个原生的command
  15886. //trace:2431
  15887. if (!start && !browser.opera) {
  15888. me.document.execCommand('formatBlock', false, '<p>');
  15889. if (browser.gecko) {
  15890. range = me.selection.getRange();
  15891. start = domUtils.findParentByTagName(range.startContainer, 'p', true);
  15892. start && domUtils.removeDirtyAttr(start);
  15893. }
  15894. } else {
  15895. hTag = start.tagName;
  15896. start.tagName.toLowerCase() == 'p' && browser.gecko && domUtils.removeDirtyAttr(start);
  15897. }
  15898. }
  15899. } else {
  15900. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
  15901. if (!range.collapsed) {
  15902. range.deleteContents();
  15903. start = range.startContainer;
  15904. if (start.nodeType == 1 && (start = start.childNodes[range.startOffset])) {
  15905. while (start.nodeType == 1) {
  15906. if (dtd.$empty[start.tagName]) {
  15907. range.setStartBefore(start).setCursor();
  15908. if (me.undoManger) {
  15909. me.undoManger.save();
  15910. }
  15911. return false;
  15912. }
  15913. if (!start.firstChild) {
  15914. var br = range.document.createElement('br');
  15915. start.appendChild(br);
  15916. range.setStart(start, 0).setCursor();
  15917. if (me.undoManger) {
  15918. me.undoManger.save();
  15919. }
  15920. return false;
  15921. }
  15922. start = start.firstChild;
  15923. }
  15924. if (start === range.startContainer.childNodes[range.startOffset]) {
  15925. br = range.document.createElement('br');
  15926. range.insertNode(br).setCursor();
  15927. } else {
  15928. range.setStart(start, 0).setCursor();
  15929. }
  15930. } else {
  15931. br = range.document.createElement('br');
  15932. range.insertNode(br).setStartAfter(br).setCursor();
  15933. }
  15934. } else {
  15935. br = range.document.createElement('br');
  15936. range.insertNode(br);
  15937. var parent = br.parentNode;
  15938. if (parent.lastChild === br) {
  15939. br.parentNode.insertBefore(br.cloneNode(true), br);
  15940. range.setStartBefore(br);
  15941. } else {
  15942. range.setStartAfter(br);
  15943. }
  15944. range.setCursor();
  15945. }
  15946. }
  15947. }
  15948. });
  15949. };
  15950. // plugins/keystrokes.js
  15951. /* 处理特殊键的兼容性问题 */
  15952. UE.plugins['keystrokes'] = function () {
  15953. var me = this;
  15954. var collapsed = true;
  15955. me.addListener('keydown', function (type, evt) {
  15956. var keyCode = evt.keyCode || evt.which,
  15957. rng = me.selection.getRange();
  15958. //处理全选的情况
  15959. if (!rng.collapsed && !(evt.ctrlKey || evt.shiftKey || evt.altKey || evt.metaKey) && (keyCode >= 65 && keyCode <= 90 ||
  15960. keyCode >= 48 && keyCode <= 57 ||
  15961. keyCode >= 96 && keyCode <= 111 || {
  15962. 13: 1,
  15963. 8: 1,
  15964. 46: 1
  15965. } [keyCode])) {
  15966. var tmpNode = rng.startContainer;
  15967. if (domUtils.isFillChar(tmpNode)) {
  15968. rng.setStartBefore(tmpNode)
  15969. }
  15970. tmpNode = rng.endContainer;
  15971. if (domUtils.isFillChar(tmpNode)) {
  15972. rng.setEndAfter(tmpNode)
  15973. }
  15974. rng.txtToElmBoundary();
  15975. //结束边界可能放到了br的前边,要把br包含进来
  15976. // x[xxx]<br/>
  15977. if (rng.endContainer && rng.endContainer.nodeType == 1) {
  15978. tmpNode = rng.endContainer.childNodes[rng.endOffset];
  15979. if (tmpNode && domUtils.isBr(tmpNode)) {
  15980. rng.setEndAfter(tmpNode);
  15981. }
  15982. }
  15983. if (rng.startOffset == 0) {
  15984. tmpNode = rng.startContainer;
  15985. if (domUtils.isBoundaryNode(tmpNode, 'firstChild')) {
  15986. tmpNode = rng.endContainer;
  15987. if (rng.endOffset == (tmpNode.nodeType == 3 ? tmpNode.nodeValue.length : tmpNode.childNodes.length) && domUtils.isBoundaryNode(tmpNode, 'lastChild')) {
  15988. me.fireEvent('saveScene');
  15989. me.body.innerHTML = '<p>' + (browser.ie ? '' : '<br/>') + '</p>';
  15990. rng.setStart(me.body.firstChild, 0).setCursor(false, true);
  15991. me._selectionChange();
  15992. return;
  15993. }
  15994. }
  15995. }
  15996. }
  15997. //处理backspace
  15998. if (keyCode == keymap.Backspace) {
  15999. rng = me.selection.getRange();
  16000. collapsed = rng.collapsed;
  16001. if (me.fireEvent('delkeydown', evt)) {
  16002. return;
  16003. }
  16004. var start, end;
  16005. //避免按两次删除才能生效的问题
  16006. if (rng.collapsed && rng.inFillChar()) {
  16007. start = rng.startContainer;
  16008. if (domUtils.isFillChar(start)) {
  16009. rng.setStartBefore(start).shrinkBoundary(true).collapse(true);
  16010. domUtils.remove(start)
  16011. } else {
  16012. start.nodeValue = start.nodeValue.replace(new RegExp('^' + domUtils.fillChar), '');
  16013. rng.startOffset--;
  16014. rng.collapse(true).select(true)
  16015. }
  16016. }
  16017. //解决选中control元素不能删除的问题
  16018. if (start = rng.getClosedNode()) {
  16019. me.fireEvent('saveScene');
  16020. rng.setStartBefore(start);
  16021. domUtils.remove(start);
  16022. rng.setCursor();
  16023. me.fireEvent('saveScene');
  16024. domUtils.preventDefault(evt);
  16025. return;
  16026. }
  16027. //阻止在table上的删除
  16028. if (!browser.ie) {
  16029. start = domUtils.findParentByTagName(rng.startContainer, 'table', true);
  16030. end = domUtils.findParentByTagName(rng.endContainer, 'table', true);
  16031. if (start && !end || !start && end || start !== end) {
  16032. evt.preventDefault();
  16033. return;
  16034. }
  16035. }
  16036. }
  16037. //处理tab键的逻辑
  16038. if (keyCode == keymap.Tab) {
  16039. //不处理以下标签
  16040. var excludeTagNameForTabKey = {
  16041. 'ol': 1,
  16042. 'ul': 1,
  16043. 'table': 1
  16044. };
  16045. //处理组件里的tab按下事件
  16046. if (me.fireEvent('tabkeydown', evt)) {
  16047. domUtils.preventDefault(evt);
  16048. return;
  16049. }
  16050. var range = me.selection.getRange();
  16051. me.fireEvent('saveScene');
  16052. for (var i = 0, txt = '', tabSize = me.options.tabSize || 4, tabNode = me.options.tabNode || '&nbsp;'; i < tabSize; i++) {
  16053. txt += tabNode;
  16054. }
  16055. var span = me.document.createElement('span');
  16056. span.innerHTML = txt + domUtils.fillChar;
  16057. if (range.collapsed) {
  16058. range.insertNode(span.cloneNode(true).firstChild).setCursor(true);
  16059. } else {
  16060. var filterFn = function (node) {
  16061. return domUtils.isBlockElm(node) && !excludeTagNameForTabKey[node.tagName.toLowerCase()]
  16062. };
  16063. //普通的情况
  16064. start = domUtils.findParent(range.startContainer, filterFn, true);
  16065. end = domUtils.findParent(range.endContainer, filterFn, true);
  16066. if (start && end && start === end) {
  16067. range.deleteContents();
  16068. range.insertNode(span.cloneNode(true).firstChild).setCursor(true);
  16069. } else {
  16070. var bookmark = range.createBookmark();
  16071. range.enlarge(true);
  16072. var bookmark2 = range.createBookmark(),
  16073. current = domUtils.getNextDomNode(bookmark2.start, false, filterFn);
  16074. while (current && !(domUtils.getPosition(current, bookmark2.end) & domUtils.POSITION_FOLLOWING)) {
  16075. current.insertBefore(span.cloneNode(true).firstChild, current.firstChild);
  16076. current = domUtils.getNextDomNode(current, false, filterFn);
  16077. }
  16078. range.moveToBookmark(bookmark2).moveToBookmark(bookmark).select();
  16079. }
  16080. }
  16081. domUtils.preventDefault(evt)
  16082. }
  16083. //trace:1634
  16084. //ff的del键在容器空的时候,也会删除
  16085. if (browser.gecko && keyCode == 46) {
  16086. range = me.selection.getRange();
  16087. if (range.collapsed) {
  16088. start = range.startContainer;
  16089. if (domUtils.isEmptyBlock(start)) {
  16090. var parent = start.parentNode;
  16091. while (domUtils.getChildCount(parent) == 1 && !domUtils.isBody(parent)) {
  16092. start = parent;
  16093. parent = parent.parentNode;
  16094. }
  16095. if (start === parent.lastChild)
  16096. evt.preventDefault();
  16097. return;
  16098. }
  16099. }
  16100. }
  16101. });
  16102. me.addListener('keyup', function (type, evt) {
  16103. var keyCode = evt.keyCode || evt.which,
  16104. rng, me = this;
  16105. if (keyCode == keymap.Backspace) {
  16106. if (me.fireEvent('delkeyup')) {
  16107. return;
  16108. }
  16109. rng = me.selection.getRange();
  16110. if (rng.collapsed) {
  16111. var tmpNode,
  16112. autoClearTagName = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
  16113. if (tmpNode = domUtils.findParentByTagName(rng.startContainer, autoClearTagName, true)) {
  16114. if (domUtils.isEmptyBlock(tmpNode)) {
  16115. var pre = tmpNode.previousSibling;
  16116. if (pre && pre.nodeName != 'TABLE') {
  16117. domUtils.remove(tmpNode);
  16118. rng.setStartAtLast(pre).setCursor(false, true);
  16119. return;
  16120. } else {
  16121. var next = tmpNode.nextSibling;
  16122. if (next && next.nodeName != 'TABLE') {
  16123. domUtils.remove(tmpNode);
  16124. rng.setStartAtFirst(next).setCursor(false, true);
  16125. return;
  16126. }
  16127. }
  16128. }
  16129. }
  16130. //处理当删除到body时,要重新给p标签展位
  16131. if (domUtils.isBody(rng.startContainer)) {
  16132. var tmpNode = domUtils.createElement(me.document, 'p', {
  16133. 'innerHTML': browser.ie ? domUtils.fillChar : '<br/>'
  16134. });
  16135. rng.insertNode(tmpNode).setStart(tmpNode, 0).setCursor(false, true);
  16136. }
  16137. }
  16138. //chrome下如果删除了inline标签,浏览器会有记忆,在输入文字还是会套上刚才删除的标签,所以这里再选一次就不会了
  16139. if (!collapsed && (rng.startContainer.nodeType == 3 || rng.startContainer.nodeType == 1 && domUtils.isEmptyBlock(rng.startContainer))) {
  16140. if (browser.ie) {
  16141. var span = rng.document.createElement('span');
  16142. rng.insertNode(span).setStartBefore(span).collapse(true);
  16143. rng.select();
  16144. domUtils.remove(span)
  16145. } else {
  16146. rng.select()
  16147. }
  16148. }
  16149. }
  16150. })
  16151. };
  16152. // plugins/fiximgclick.js
  16153. ///import core
  16154. ///commands 修复chrome下图片不能点击的问题,出现八个角可改变大小
  16155. ///commandsName FixImgClick
  16156. ///commandsTitle 修复chrome下图片不能点击的问题,出现八个角可改变大小
  16157. //修复chrome下图片不能点击的问题,出现八个角可改变大小
  16158. UE.plugins['fiximgclick'] = (function () {
  16159. var elementUpdated = false;
  16160. function Scale() {
  16161. this.editor = null;
  16162. this.resizer = null;
  16163. this.cover = null;
  16164. this.doc = document;
  16165. this.prePos = {
  16166. x: 0,
  16167. y: 0
  16168. };
  16169. this.startPos = {
  16170. x: 0,
  16171. y: 0
  16172. };
  16173. }
  16174. (function () {
  16175. var rect = [
  16176. //[left, top, width, height]
  16177. [0, 0, -1, -1],
  16178. [0, 0, 0, -1],
  16179. [0, 0, 1, -1],
  16180. [0, 0, -1, 0],
  16181. [0, 0, 1, 0],
  16182. [0, 0, -1, 1],
  16183. [0, 0, 0, 1],
  16184. [0, 0, 1, 1]
  16185. ];
  16186. Scale.prototype = {
  16187. init: function (editor) {
  16188. var me = this;
  16189. me.editor = editor;
  16190. me.startPos = this.prePos = {
  16191. x: 0,
  16192. y: 0
  16193. };
  16194. me.dragId = -1;
  16195. var hands = [],
  16196. cover = me.cover = document.createElement('div'),
  16197. resizer = me.resizer = document.createElement('div');
  16198. cover.id = me.editor.ui.id + '_imagescale_cover';
  16199. cover.style.cssText = 'position:absolute;display:none;z-index:' + (me.editor.options.zIndex) + ';filter:alpha(opacity=0); opacity:0;background:#CCC;';
  16200. domUtils.on(cover, 'mousedown click', function () {
  16201. me.hide();
  16202. });
  16203. for (i = 0; i < 8; i++) {
  16204. hands.push('<span class="edui-editor-imagescale-hand' + i + '"></span>');
  16205. }
  16206. resizer.id = me.editor.ui.id + '_imagescale';
  16207. resizer.className = 'edui-editor-imagescale';
  16208. resizer.innerHTML = hands.join('');
  16209. resizer.style.cssText += ';display:none;border:1px solid #3b77ff;z-index:' + (me.editor.options.zIndex) + ';';
  16210. me.editor.ui.getDom().appendChild(cover);
  16211. me.editor.ui.getDom().appendChild(resizer);
  16212. me.initStyle();
  16213. me.initEvents();
  16214. },
  16215. initStyle: function () {
  16216. 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;}' +
  16217. '.edui-editor-imagescale span{position:absolute;width:6px;height:6px;overflow:hidden;font-size:0px;display:block;background-color:#3C9DD0;}' +
  16218. '.edui-editor-imagescale .edui-editor-imagescale-hand0{cursor:nw-resize;top:0;margin-top:-4px;left:0;margin-left:-4px;}' +
  16219. '.edui-editor-imagescale .edui-editor-imagescale-hand1{cursor:n-resize;top:0;margin-top:-4px;left:50%;margin-left:-4px;}' +
  16220. '.edui-editor-imagescale .edui-editor-imagescale-hand2{cursor:ne-resize;top:0;margin-top:-4px;left:100%;margin-left:-3px;}' +
  16221. '.edui-editor-imagescale .edui-editor-imagescale-hand3{cursor:w-resize;top:50%;margin-top:-4px;left:0;margin-left:-4px;}' +
  16222. '.edui-editor-imagescale .edui-editor-imagescale-hand4{cursor:e-resize;top:50%;margin-top:-4px;left:100%;margin-left:-3px;}' +
  16223. '.edui-editor-imagescale .edui-editor-imagescale-hand5{cursor:sw-resize;top:100%;margin-top:-3px;left:0;margin-left:-4px;}' +
  16224. '.edui-editor-imagescale .edui-editor-imagescale-hand6{cursor:s-resize;top:100%;margin-top:-3px;left:50%;margin-left:-4px;}' +
  16225. '.edui-editor-imagescale .edui-editor-imagescale-hand7{cursor:se-resize;top:100%;margin-top:-3px;left:100%;margin-left:-3px;}');
  16226. },
  16227. initEvents: function () {
  16228. var me = this;
  16229. me.startPos.x = me.startPos.y = 0;
  16230. me.isDraging = false;
  16231. },
  16232. _eventHandler: function (e) {
  16233. var me = this;
  16234. switch (e.type) {
  16235. case 'mousedown':
  16236. var hand = e.target || e.srcElement,
  16237. hand;
  16238. if (hand.className.indexOf('edui-editor-imagescale-hand') != -1 && me.dragId == -1) {
  16239. me.dragId = hand.className.slice(-1);
  16240. me.startPos.x = me.prePos.x = e.clientX;
  16241. me.startPos.y = me.prePos.y = e.clientY;
  16242. domUtils.on(me.doc, 'mousemove', me.proxy(me._eventHandler, me));
  16243. }
  16244. break;
  16245. case 'mousemove':
  16246. if (me.dragId != -1) {
  16247. me.updateContainerStyle(me.dragId, {
  16248. x: e.clientX - me.prePos.x,
  16249. y: e.clientY - me.prePos.y
  16250. });
  16251. me.prePos.x = e.clientX;
  16252. me.prePos.y = e.clientY;
  16253. elementUpdated = true;
  16254. me.updateTargetElement();
  16255. }
  16256. break;
  16257. case 'mouseup':
  16258. if (me.dragId != -1) {
  16259. me.updateContainerStyle(me.dragId, {
  16260. x: e.clientX - me.prePos.x,
  16261. y: e.clientY - me.prePos.y
  16262. });
  16263. me.updateTargetElement();
  16264. if (me.target.parentNode) me.attachTo(me.target);
  16265. me.dragId = -1;
  16266. }
  16267. domUtils.un(me.doc, 'mousemove', me.proxy(me._eventHandler, me));
  16268. //修复只是点击挪动点,但没有改变大小,不应该触发contentchange
  16269. if (elementUpdated) {
  16270. elementUpdated = false;
  16271. me.editor.fireEvent('contentchange');
  16272. }
  16273. break;
  16274. default:
  16275. break;
  16276. }
  16277. },
  16278. updateTargetElement: function () {
  16279. var me = this;
  16280. domUtils.setStyles(me.target, {
  16281. 'width': me.resizer.style.width,
  16282. 'height': me.resizer.style.height
  16283. });
  16284. me.target.width = parseInt(me.resizer.style.width);
  16285. me.target.height = parseInt(me.resizer.style.height);
  16286. me.attachTo(me.target);
  16287. },
  16288. updateContainerStyle: function (dir, offset) {
  16289. var me = this,
  16290. dom = me.resizer,
  16291. tmp;
  16292. if (rect[dir][0] != 0) {
  16293. tmp = parseInt(dom.style.left) + offset.x;
  16294. dom.style.left = me._validScaledProp('left', tmp) + 'px';
  16295. }
  16296. if (rect[dir][1] != 0) {
  16297. tmp = parseInt(dom.style.top) + offset.y;
  16298. dom.style.top = me._validScaledProp('top', tmp) + 'px';
  16299. }
  16300. if (rect[dir][2] != 0) {
  16301. tmp = dom.clientWidth + rect[dir][2] * offset.x;
  16302. dom.style.width = me._validScaledProp('width', tmp) + 'px';
  16303. }
  16304. if (rect[dir][3] != 0) {
  16305. tmp = dom.clientHeight + rect[dir][3] * offset.y;
  16306. dom.style.height = me._validScaledProp('height', tmp) + 'px';
  16307. }
  16308. },
  16309. _validScaledProp: function (prop, value) {
  16310. var ele = this.resizer,
  16311. wrap = document;
  16312. value = isNaN(value) ? 0 : value;
  16313. switch (prop) {
  16314. case 'left':
  16315. return value < 0 ? 0 : (value + ele.clientWidth) > wrap.clientWidth ? wrap.clientWidth - ele.clientWidth : value;
  16316. case 'top':
  16317. return value < 0 ? 0 : (value + ele.clientHeight) > wrap.clientHeight ? wrap.clientHeight - ele.clientHeight : value;
  16318. case 'width':
  16319. return value <= 0 ? 1 : (value + ele.offsetLeft) > wrap.clientWidth ? wrap.clientWidth - ele.offsetLeft : value;
  16320. case 'height':
  16321. return value <= 0 ? 1 : (value + ele.offsetTop) > wrap.clientHeight ? wrap.clientHeight - ele.offsetTop : value;
  16322. }
  16323. },
  16324. hideCover: function () {
  16325. this.cover.style.display = 'none';
  16326. },
  16327. showCover: function () {
  16328. var me = this,
  16329. editorPos = domUtils.getXY(me.editor.ui.getDom()),
  16330. iframePos = domUtils.getXY(me.editor.iframe);
  16331. domUtils.setStyles(me.cover, {
  16332. 'width': me.editor.iframe.offsetWidth + 'px',
  16333. 'height': me.editor.iframe.offsetHeight + 'px',
  16334. 'top': iframePos.y - editorPos.y + 'px',
  16335. 'left': iframePos.x - editorPos.x + 'px',
  16336. 'position': 'absolute',
  16337. 'display': ''
  16338. })
  16339. },
  16340. show: function (targetObj) {
  16341. var me = this;
  16342. me.resizer.style.display = 'block';
  16343. if (targetObj) me.attachTo(targetObj);
  16344. domUtils.on(this.resizer, 'mousedown', me.proxy(me._eventHandler, me));
  16345. domUtils.on(me.doc, 'mouseup', me.proxy(me._eventHandler, me));
  16346. me.showCover();
  16347. me.editor.fireEvent('afterscaleshow', me);
  16348. me.editor.fireEvent('saveScene');
  16349. },
  16350. hide: function () {
  16351. var me = this;
  16352. me.hideCover();
  16353. me.resizer.style.display = 'none';
  16354. domUtils.un(me.resizer, 'mousedown', me.proxy(me._eventHandler, me));
  16355. domUtils.un(me.doc, 'mouseup', me.proxy(me._eventHandler, me));
  16356. me.editor.fireEvent('afterscalehide', me);
  16357. },
  16358. proxy: function (fn, context) {
  16359. return function (e) {
  16360. return fn.apply(context || this, arguments);
  16361. };
  16362. },
  16363. attachTo: function (targetObj) {
  16364. var me = this,
  16365. target = me.target = targetObj,
  16366. resizer = this.resizer,
  16367. imgPos = domUtils.getXY(target),
  16368. iframePos = domUtils.getXY(me.editor.iframe),
  16369. editorPos = domUtils.getXY(resizer.parentNode);
  16370. domUtils.setStyles(resizer, {
  16371. 'width': target.width + 'px',
  16372. 'height': target.height + 'px',
  16373. 'left': iframePos.x + imgPos.x - me.editor.document.body.scrollLeft - editorPos.x - parseInt(resizer.style.borderLeftWidth) + 'px',
  16374. 'top': iframePos.y + imgPos.y - me.editor.document.body.scrollTop - editorPos.y - parseInt(resizer.style.borderTopWidth) + 'px'
  16375. });
  16376. }
  16377. }
  16378. })();
  16379. return function () {
  16380. var me = this,
  16381. imageScale;
  16382. me.setOpt('imageScaleEnabled', true);
  16383. if (!browser.ie && me.options.imageScaleEnabled) {
  16384. me.addListener('click', function (type, e) {
  16385. var range = me.selection.getRange(),
  16386. img = range.getClosedNode();
  16387. if (img && img.tagName == 'IMG' && me.body.contentEditable != "false") {
  16388. if (img.className.indexOf("edui-faked-music") != -1 ||
  16389. img.getAttribute("anchorname") ||
  16390. domUtils.hasClass(img, 'loadingclass') ||
  16391. domUtils.hasClass(img, 'loaderrorclass')) {
  16392. return
  16393. }
  16394. if (!imageScale) {
  16395. imageScale = new Scale();
  16396. imageScale.init(me);
  16397. me.ui.getDom().appendChild(imageScale.resizer);
  16398. var _keyDownHandler = function (e) {
  16399. imageScale.hide();
  16400. if (imageScale.target) me.selection.getRange().selectNode(imageScale.target).select();
  16401. },
  16402. _mouseDownHandler = function (e) {
  16403. var ele = e.target || e.srcElement;
  16404. if (ele && (ele.className === undefined || ele.className.indexOf('edui-editor-imagescale') == -1)) {
  16405. _keyDownHandler(e);
  16406. }
  16407. },
  16408. timer;
  16409. me.addListener('afterscaleshow', function (e) {
  16410. me.addListener('beforekeydown', _keyDownHandler);
  16411. me.addListener('beforemousedown', _mouseDownHandler);
  16412. domUtils.on(document, 'keydown', _keyDownHandler);
  16413. domUtils.on(document, 'mousedown', _mouseDownHandler);
  16414. me.selection.getNative().removeAllRanges();
  16415. });
  16416. me.addListener('afterscalehide', function (e) {
  16417. me.removeListener('beforekeydown', _keyDownHandler);
  16418. me.removeListener('beforemousedown', _mouseDownHandler);
  16419. domUtils.un(document, 'keydown', _keyDownHandler);
  16420. domUtils.un(document, 'mousedown', _mouseDownHandler);
  16421. var target = imageScale.target;
  16422. if (target.parentNode) {
  16423. me.selection.getRange().selectNode(target).select();
  16424. }
  16425. });
  16426. //TODO 有iframe的情况,mousedown不能往下传。。
  16427. domUtils.on(imageScale.resizer, 'mousedown', function (e) {
  16428. me.selection.getNative().removeAllRanges();
  16429. var ele = e.target || e.srcElement;
  16430. if (ele && ele.className.indexOf('edui-editor-imagescale-hand') == -1) {
  16431. timer = setTimeout(function () {
  16432. imageScale.hide();
  16433. if (imageScale.target) me.selection.getRange().selectNode(ele).select();
  16434. }, 200);
  16435. }
  16436. });
  16437. domUtils.on(imageScale.resizer, 'mouseup', function (e) {
  16438. var ele = e.target || e.srcElement;
  16439. if (ele && ele.className.indexOf('edui-editor-imagescale-hand') == -1) {
  16440. clearTimeout(timer);
  16441. }
  16442. });
  16443. }
  16444. imageScale.show(img);
  16445. } else {
  16446. if (imageScale && imageScale.resizer.style.display != 'none') imageScale.hide();
  16447. }
  16448. });
  16449. }
  16450. if (browser.webkit) {
  16451. me.addListener('click', function (type, e) {
  16452. if (e.target.tagName == 'IMG' && me.body.contentEditable != "false") {
  16453. var range = new dom.Range(me.document);
  16454. range.selectNode(e.target).select();
  16455. }
  16456. });
  16457. }
  16458. }
  16459. })();
  16460. // plugins/autolink.js
  16461. ///import core
  16462. ///commands 为非ie浏览器自动添加a标签
  16463. ///commandsName AutoLink
  16464. ///commandsTitle 自动增加链接
  16465. /**
  16466. * @description 为非ie浏览器自动添加a标签
  16467. * @author zhanyi
  16468. */
  16469. UE.plugin.register('autolink', function () {
  16470. var cont = 0;
  16471. return !browser.ie ? {
  16472. bindEvents: {
  16473. 'reset': function () {
  16474. cont = 0;
  16475. },
  16476. 'keydown': function (type, evt) {
  16477. var me = this;
  16478. var keyCode = evt.keyCode || evt.which;
  16479. if (keyCode == 32 || keyCode == 13) {
  16480. var sel = me.selection.getNative(),
  16481. range = sel.getRangeAt(0).cloneRange(),
  16482. offset,
  16483. charCode;
  16484. var start = range.startContainer;
  16485. while (start.nodeType == 1 && range.startOffset > 0) {
  16486. start = range.startContainer.childNodes[range.startOffset - 1];
  16487. if (!start) {
  16488. break;
  16489. }
  16490. range.setStart(start, start.nodeType == 1 ? start.childNodes.length : start.nodeValue.length);
  16491. range.collapse(true);
  16492. start = range.startContainer;
  16493. }
  16494. do {
  16495. if (range.startOffset == 0) {
  16496. start = range.startContainer.previousSibling;
  16497. while (start && start.nodeType == 1) {
  16498. start = start.lastChild;
  16499. }
  16500. if (!start || domUtils.isFillChar(start)) {
  16501. break;
  16502. }
  16503. offset = start.nodeValue.length;
  16504. } else {
  16505. start = range.startContainer;
  16506. offset = range.startOffset;
  16507. }
  16508. range.setStart(start, offset - 1);
  16509. charCode = range.toString().charCodeAt(0);
  16510. } while (charCode != 160 && charCode != 32);
  16511. if (range.toString().replace(new RegExp(domUtils.fillChar, 'g'), '').match(/(?:https?:\/\/|ssh:\/\/|ftp:\/\/|file:\/|www\.)/i)) {
  16512. while (range.toString().length) {
  16513. if (/^(?:https?:\/\/|ssh:\/\/|ftp:\/\/|file:\/|www\.)/i.test(range.toString())) {
  16514. break;
  16515. }
  16516. try {
  16517. range.setStart(range.startContainer, range.startOffset + 1);
  16518. } catch (e) {
  16519. //trace:2121
  16520. var start = range.startContainer;
  16521. while (!(next = start.nextSibling)) {
  16522. if (domUtils.isBody(start)) {
  16523. return;
  16524. }
  16525. start = start.parentNode;
  16526. }
  16527. range.setStart(next, 0);
  16528. }
  16529. }
  16530. //range的开始边界已经在a标签里的不再处理
  16531. if (domUtils.findParentByTagName(range.startContainer, 'a', true)) {
  16532. return;
  16533. }
  16534. var a = me.document.createElement('a'),
  16535. text = me.document.createTextNode(' '),
  16536. href;
  16537. me.undoManger && me.undoManger.save();
  16538. a.appendChild(range.extractContents());
  16539. a.href = a.innerHTML = a.innerHTML.replace(/<[^>]+>/g, '');
  16540. href = a.getAttribute("href").replace(new RegExp(domUtils.fillChar, 'g'), '');
  16541. href = /^(?:https?:\/\/)/ig.test(href) ? href : "http://" + href;
  16542. a.setAttribute('_src', utils.html(href));
  16543. a.href = utils.html(href);
  16544. range.insertNode(a);
  16545. a.parentNode.insertBefore(text, a.nextSibling);
  16546. range.setStart(text, 0);
  16547. range.collapse(true);
  16548. sel.removeAllRanges();
  16549. sel.addRange(range);
  16550. me.undoManger && me.undoManger.save();
  16551. }
  16552. }
  16553. }
  16554. }
  16555. } : {}
  16556. }, function () {
  16557. var keyCodes = {
  16558. 37: 1,
  16559. 38: 1,
  16560. 39: 1,
  16561. 40: 1,
  16562. 13: 1,
  16563. 32: 1
  16564. };
  16565. function checkIsCludeLink(node) {
  16566. if (node.nodeType == 3) {
  16567. return null
  16568. }
  16569. if (node.nodeName == 'A') {
  16570. return node;
  16571. }
  16572. var lastChild = node.lastChild;
  16573. while (lastChild) {
  16574. if (lastChild.nodeName == 'A') {
  16575. return lastChild;
  16576. }
  16577. if (lastChild.nodeType == 3) {
  16578. if (domUtils.isWhitespace(lastChild)) {
  16579. lastChild = lastChild.previousSibling;
  16580. continue;
  16581. }
  16582. return null
  16583. }
  16584. lastChild = lastChild.lastChild;
  16585. }
  16586. }
  16587. browser.ie && this.addListener('keyup', function (cmd, evt) {
  16588. var me = this,
  16589. keyCode = evt.keyCode;
  16590. if (keyCodes[keyCode]) {
  16591. var rng = me.selection.getRange();
  16592. var start = rng.startContainer;
  16593. if (keyCode == 13) {
  16594. while (start && !domUtils.isBody(start) && !domUtils.isBlockElm(start)) {
  16595. start = start.parentNode;
  16596. }
  16597. if (start && !domUtils.isBody(start) && start.nodeName == 'P') {
  16598. var pre = start.previousSibling;
  16599. if (pre && pre.nodeType == 1) {
  16600. var pre = checkIsCludeLink(pre);
  16601. if (pre && !pre.getAttribute('_href')) {
  16602. domUtils.remove(pre, true);
  16603. }
  16604. }
  16605. }
  16606. } else if (keyCode == 32) {
  16607. if (start.nodeType == 3 && /^\s$/.test(start.nodeValue)) {
  16608. start = start.previousSibling;
  16609. if (start && start.nodeName == 'A' && !start.getAttribute('_href')) {
  16610. domUtils.remove(start, true);
  16611. }
  16612. }
  16613. } else {
  16614. start = domUtils.findParentByTagName(start, 'a', true);
  16615. if (start && !start.getAttribute('_href')) {
  16616. var bk = rng.createBookmark();
  16617. domUtils.remove(start, true);
  16618. rng.moveToBookmark(bk).select(true)
  16619. }
  16620. }
  16621. }
  16622. });
  16623. });
  16624. // plugins/autoheight.js
  16625. ///import core
  16626. ///commands 当输入内容超过编辑器高度时,编辑器自动增高
  16627. ///commandsName AutoHeight,autoHeightEnabled
  16628. ///commandsTitle 自动增高
  16629. /**
  16630. * @description 自动伸展
  16631. * @author zhanyi
  16632. */
  16633. UE.plugins['autoheight'] = function () {
  16634. var me = this;
  16635. //提供开关,就算加载也可以关闭
  16636. me.autoHeightEnabled = me.options.autoHeightEnabled !== false;
  16637. if (!me.autoHeightEnabled) {
  16638. return;
  16639. }
  16640. var bakOverflow,
  16641. lastHeight = 0,
  16642. options = me.options,
  16643. currentHeight,
  16644. timer;
  16645. function adjustHeight() {
  16646. var me = this;
  16647. clearTimeout(timer);
  16648. if (isFullscreen) return;
  16649. if (!me.queryCommandState || me.queryCommandState && me.queryCommandState('source') != 1) {
  16650. timer = setTimeout(function () {
  16651. var node = me.body.lastChild;
  16652. while (node && node.nodeType != 1) {
  16653. node = node.previousSibling;
  16654. }
  16655. if (node && node.nodeType == 1) {
  16656. node.style.clear = 'both';
  16657. currentHeight = Math.max(domUtils.getXY(node).y + node.offsetHeight + 25, Math.max(options.minFrameHeight, options.initialFrameHeight));
  16658. if (currentHeight != lastHeight) {
  16659. if (currentHeight !== parseInt(me.iframe.parentNode.style.height)) {
  16660. me.iframe.parentNode.style.height = currentHeight + 'px';
  16661. }
  16662. me.body.style.height = currentHeight + 'px';
  16663. lastHeight = currentHeight;
  16664. }
  16665. domUtils.removeStyle(node, 'clear');
  16666. }
  16667. }, 50)
  16668. }
  16669. }
  16670. var isFullscreen;
  16671. me.addListener('fullscreenchanged', function (cmd, f) {
  16672. isFullscreen = f
  16673. });
  16674. me.addListener('destroy', function () {
  16675. me.removeListener('contentchange afterinserthtml keyup mouseup', adjustHeight)
  16676. });
  16677. me.enableAutoHeight = function () {
  16678. var me = this;
  16679. if (!me.autoHeightEnabled) {
  16680. return;
  16681. }
  16682. var doc = me.document;
  16683. me.autoHeightEnabled = true;
  16684. bakOverflow = doc.body.style.overflowY;
  16685. doc.body.style.overflowY = 'hidden';
  16686. me.addListener('contentchange afterinserthtml keyup mouseup', adjustHeight);
  16687. //ff不给事件算得不对
  16688. setTimeout(function () {
  16689. adjustHeight.call(me);
  16690. }, browser.gecko ? 100 : 0);
  16691. me.fireEvent('autoheightchanged', me.autoHeightEnabled);
  16692. };
  16693. me.disableAutoHeight = function () {
  16694. me.body.style.overflowY = bakOverflow || '';
  16695. me.removeListener('contentchange', adjustHeight);
  16696. me.removeListener('keyup', adjustHeight);
  16697. me.removeListener('mouseup', adjustHeight);
  16698. me.autoHeightEnabled = false;
  16699. me.fireEvent('autoheightchanged', me.autoHeightEnabled);
  16700. };
  16701. me.on('setHeight', function () {
  16702. me.disableAutoHeight()
  16703. });
  16704. me.addListener('ready', function () {
  16705. me.enableAutoHeight();
  16706. //trace:1764
  16707. var timer;
  16708. domUtils.on(browser.ie ? me.body : me.document, browser.webkit ? 'dragover' : 'drop', function () {
  16709. clearTimeout(timer);
  16710. timer = setTimeout(function () {
  16711. //trace:3681
  16712. adjustHeight.call(me);
  16713. }, 100);
  16714. });
  16715. //修复内容过多时,回到顶部,顶部内容被工具栏遮挡问题
  16716. var lastScrollY;
  16717. window.onscroll = function () {
  16718. if (lastScrollY === null) {
  16719. lastScrollY = this.scrollY
  16720. } else if (this.scrollY == 0 && lastScrollY != 0) {
  16721. me.window.scrollTo(0, 0);
  16722. lastScrollY = null;
  16723. }
  16724. }
  16725. });
  16726. };
  16727. // plugins/autofloat.js
  16728. ///import core
  16729. ///commands 悬浮工具栏
  16730. ///commandsName AutoFloat,autoFloatEnabled
  16731. ///commandsTitle 悬浮工具栏
  16732. /**
  16733. * modified by chengchao01
  16734. * 注意: 引入此功能后,在IE6下会将body的背景图片覆盖掉!
  16735. */
  16736. UE.plugins['autofloat'] = function () {
  16737. var me = this,
  16738. lang = me.getLang();
  16739. me.setOpt({
  16740. topOffset: 0
  16741. });
  16742. var optsAutoFloatEnabled = me.options.autoFloatEnabled !== false,
  16743. topOffset = me.options.topOffset;
  16744. //如果不固定toolbar的位置,则直接退出
  16745. if (!optsAutoFloatEnabled) {
  16746. return;
  16747. }
  16748. var uiUtils = UE.ui.uiUtils,
  16749. LteIE6 = browser.ie && browser.version <= 6,
  16750. quirks = browser.quirks;
  16751. function checkHasUI() {
  16752. if (!UE.ui) {
  16753. alert(lang.autofloatMsg);
  16754. return 0;
  16755. }
  16756. return 1;
  16757. }
  16758. function fixIE6FixedPos() {
  16759. var docStyle = document.body.style;
  16760. docStyle.backgroundImage = 'url("about:blank")';
  16761. docStyle.backgroundAttachment = 'fixed';
  16762. }
  16763. var bakCssText,
  16764. placeHolder = document.createElement('div'),
  16765. toolbarBox, orgTop,
  16766. getPosition,
  16767. flag = true; //ie7模式下需要偏移
  16768. function setFloating() {
  16769. var toobarBoxPos = domUtils.getXY(toolbarBox),
  16770. origalFloat = domUtils.getComputedStyle(toolbarBox, 'position'),
  16771. origalLeft = domUtils.getComputedStyle(toolbarBox, 'left');
  16772. toolbarBox.style.width = toolbarBox.offsetWidth + 'px';
  16773. toolbarBox.style.zIndex = me.options.zIndex * 1 + 1;
  16774. toolbarBox.parentNode.insertBefore(placeHolder, toolbarBox);
  16775. if (LteIE6 || (quirks && browser.ie)) {
  16776. if (toolbarBox.style.position != 'absolute') {
  16777. toolbarBox.style.position = 'absolute';
  16778. }
  16779. toolbarBox.style.top = (document.body.scrollTop || document.documentElement.scrollTop) - orgTop + topOffset + 'px';
  16780. } else {
  16781. if (browser.ie7Compat && flag) {
  16782. flag = false;
  16783. toolbarBox.style.left = domUtils.getXY(toolbarBox).x - document.documentElement.getBoundingClientRect().left + 2 + 'px';
  16784. }
  16785. if (toolbarBox.style.position != 'fixed') {
  16786. toolbarBox.style.position = 'fixed';
  16787. toolbarBox.style.top = topOffset + "px";
  16788. ((origalFloat == 'absolute' || origalFloat == 'relative') && parseFloat(origalLeft)) && (toolbarBox.style.left = toobarBoxPos.x + 'px');
  16789. }
  16790. }
  16791. }
  16792. function unsetFloating() {
  16793. flag = true;
  16794. if (placeHolder.parentNode) {
  16795. placeHolder.parentNode.removeChild(placeHolder);
  16796. }
  16797. toolbarBox.style.cssText = bakCssText;
  16798. }
  16799. function updateFloating() {
  16800. var rect3 = getPosition(me.container);
  16801. var offset = me.options.toolbarTopOffset || 0;
  16802. if (rect3.top < 0 && rect3.bottom - toolbarBox.offsetHeight > offset) {
  16803. setFloating();
  16804. } else {
  16805. unsetFloating();
  16806. }
  16807. }
  16808. var defer_updateFloating = utils.defer(function () {
  16809. updateFloating();
  16810. }, browser.ie ? 200 : 100, true);
  16811. me.addListener('destroy', function () {
  16812. domUtils.un(window, ['scroll', 'resize'], updateFloating);
  16813. me.removeListener('keydown', defer_updateFloating);
  16814. });
  16815. me.addListener('ready', function () {
  16816. if (checkHasUI(me)) {
  16817. //加载了ui组件,但在new时,没有加载ui,导致编辑器实例上没有ui类,所以这里做判断
  16818. if (!me.ui) {
  16819. return;
  16820. }
  16821. getPosition = uiUtils.getClientRect;
  16822. toolbarBox = me.ui.getDom('toolbarbox');
  16823. orgTop = getPosition(toolbarBox).top;
  16824. bakCssText = toolbarBox.style.cssText;
  16825. placeHolder.style.height = toolbarBox.offsetHeight + 'px';
  16826. if (LteIE6) {
  16827. fixIE6FixedPos();
  16828. }
  16829. domUtils.on(window, ['scroll', 'resize'], updateFloating);
  16830. me.addListener('keydown', defer_updateFloating);
  16831. me.addListener('beforefullscreenchange', function (t, enabled) {
  16832. if (enabled) {
  16833. unsetFloating();
  16834. }
  16835. });
  16836. me.addListener('fullscreenchanged', function (t, enabled) {
  16837. if (!enabled) {
  16838. updateFloating();
  16839. }
  16840. });
  16841. me.addListener('sourcemodechanged', function (t, enabled) {
  16842. setTimeout(function () {
  16843. updateFloating();
  16844. }, 0);
  16845. });
  16846. me.addListener("clearDoc", function () {
  16847. setTimeout(function () {
  16848. updateFloating();
  16849. }, 0);
  16850. })
  16851. }
  16852. });
  16853. };
  16854. // plugins/video.js
  16855. /**
  16856. * video插件, 为UEditor提供视频插入支持
  16857. * @file
  16858. * @since 1.2.6.1
  16859. */
  16860. UE.plugins['video'] = function () {
  16861. var me = this;
  16862. /**
  16863. * 创建插入视频字符窜
  16864. * @param url 视频地址
  16865. * @param width 视频宽度
  16866. * @param height 视频高度
  16867. * @param align 视频对齐
  16868. * @param toEmbed 是否以flash代替显示
  16869. * @param addParagraph 是否需要添加P 标签
  16870. */
  16871. function creatInsertStr(url, width, height, id, align, classname, type) {
  16872. url = utils.unhtmlForUrl(url);
  16873. align = utils.unhtml(align);
  16874. classname = utils.unhtml(classname).trim();
  16875. width = parseInt(width, 10) || 0;
  16876. height = parseInt(height, 10) || 0;
  16877. var str;
  16878. switch (type) {
  16879. case 'image':
  16880. str = '<img ' + (id ? 'id="' + id + '"' : '') + ' width="' + width + '" height="' + height + '" _url="' + url + '" class="' + classname.replace(/\bvideo-js\b/, '') + '"' +
  16881. ' 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 + ';' : '') + '" />'
  16882. break;
  16883. case 'embed':
  16884. str = '<embed type="application/x-shockwave-flash" class="' + classname + '" pluginspage="http://www.macromedia.com/go/getflashplayer"' +
  16885. ' src="' + utils.html(url) + '" width="' + width + '" height="' + height + '"' + (align ? ' style="float:' + align + '"' : '') +
  16886. ' wmode="transparent" play="true" loop="false" menu="false" allowscriptaccess="never" allowfullscreen="true" >';
  16887. break;
  16888. case 'video':
  16889. var ext = url.substr(url.lastIndexOf('.') + 1);
  16890. if (ext == 'ogv') ext = 'ogg';
  16891. str = '<video' + (id ? ' id="' + id + '"' : '') + ' class="' + classname + ' video-js" ' + (align ? ' style="float:' + align + '"' : '') +
  16892. ' controls preload="none" width="' + width + '" height="' + height + '" src="' + url + '" data-setup="{}">' +
  16893. '<source src="' + url + '" type="video/' + ext + '" /></video>';
  16894. break;
  16895. }
  16896. return str;
  16897. }
  16898. function switchImgAndVideo(root, img2video) {
  16899. utils.each(root.getNodesByTagName(img2video ? 'img' : 'embed video'), function (node) {
  16900. var className = node.getAttr('class');
  16901. if (className && className.indexOf('edui-faked-video') != -1) {
  16902. var html = creatInsertStr(img2video ? node.getAttr('_url') : node.getAttr('src'), node.getAttr('width'), node.getAttr('height'), null, node.getStyle('float') || '', className, img2video ? 'embed' : 'image');
  16903. node.parentNode.replaceChild(UE.uNode.createElement(html), node);
  16904. }
  16905. if (className && className.indexOf('edui-upload-video') != -1) {
  16906. var html = creatInsertStr(img2video ? node.getAttr('_url') : node.getAttr('src'), node.getAttr('width'), node.getAttr('height'), null, node.getStyle('float') || '', className, img2video ? 'video' : 'image');
  16907. node.parentNode.replaceChild(UE.uNode.createElement(html), node);
  16908. }
  16909. })
  16910. }
  16911. me.addOutputRule(function (root) {
  16912. switchImgAndVideo(root, true)
  16913. });
  16914. me.addInputRule(function (root) {
  16915. switchImgAndVideo(root)
  16916. });
  16917. /**
  16918. * 插入视频
  16919. * @command insertvideo
  16920. * @method execCommand
  16921. * @param { String } cmd 命令字符串
  16922. * @param { Object } videoAttr 键值对对象, 描述一个视频的所有属性
  16923. * @example
  16924. * ```javascript
  16925. *
  16926. * var videoAttr = {
  16927. * //视频地址
  16928. * url: 'http://www.youku.com/xxx',
  16929. * //视频宽高值, 单位px
  16930. * width: 200,
  16931. * height: 100
  16932. * };
  16933. *
  16934. * //editor 是编辑器实例
  16935. * //向编辑器插入单个视频
  16936. * editor.execCommand( 'insertvideo', videoAttr );
  16937. * ```
  16938. */
  16939. /**
  16940. * 插入视频
  16941. * @command insertvideo
  16942. * @method execCommand
  16943. * @param { String } cmd 命令字符串
  16944. * @param { Array } videoArr 需要插入的视频的数组, 其中的每一个元素都是一个键值对对象, 描述了一个视频的所有属性
  16945. * @example
  16946. * ```javascript
  16947. *
  16948. * var videoAttr1 = {
  16949. * //视频地址
  16950. * url: 'http://www.youku.com/xxx',
  16951. * //视频宽高值, 单位px
  16952. * width: 200,
  16953. * height: 100
  16954. * },
  16955. * videoAttr2 = {
  16956. * //视频地址
  16957. * url: 'http://www.youku.com/xxx',
  16958. * //视频宽高值, 单位px
  16959. * width: 200,
  16960. * height: 100
  16961. * }
  16962. *
  16963. * //editor 是编辑器实例
  16964. * //该方法将会向编辑器内插入两个视频
  16965. * editor.execCommand( 'insertvideo', [ videoAttr1, videoAttr2 ] );
  16966. * ```
  16967. */
  16968. /**
  16969. * 查询当前光标所在处是否是一个视频
  16970. * @command insertvideo
  16971. * @method queryCommandState
  16972. * @param { String } cmd 需要查询的命令字符串
  16973. * @return { int } 如果当前光标所在处的元素是一个视频对象, 则返回1,否则返回0
  16974. * @example
  16975. * ```javascript
  16976. *
  16977. * //editor 是编辑器实例
  16978. * editor.queryCommandState( 'insertvideo' );
  16979. * ```
  16980. */
  16981. me.commands["insertvideo"] = {
  16982. execCommand: function (cmd, videoObjs, type) {
  16983. videoObjs = utils.isArray(videoObjs) ? videoObjs : [videoObjs];
  16984. var html = [],
  16985. id = 'tmpVedio',
  16986. cl;
  16987. for (var i = 0, vi, len = videoObjs.length; i < len; i++) {
  16988. vi = videoObjs[i];
  16989. cl = (type == 'upload' ? 'edui-upload-video video-js vjs-default-skin' : 'edui-faked-video');
  16990. html.push(creatInsertStr(vi.url, vi.width || 420, vi.height || 280, id + i, null, cl, 'image'));
  16991. }
  16992. me.execCommand("inserthtml", html.join(""), true);
  16993. var rng = this.selection.getRange();
  16994. for (var i = 0, len = videoObjs.length; i < len; i++) {
  16995. var img = this.document.getElementById('tmpVedio' + i);
  16996. domUtils.removeAttributes(img, 'id');
  16997. rng.selectNode(img).select();
  16998. me.execCommand('imagefloat', videoObjs[i].align)
  16999. }
  17000. },
  17001. queryCommandState: function () {
  17002. var img = me.selection.getRange().getClosedNode(),
  17003. flag = img && (img.className == "edui-faked-video" || img.className.indexOf("edui-upload-video") != -1);
  17004. return flag ? 1 : 0;
  17005. }
  17006. };
  17007. };
  17008. // plugins/table.core.js
  17009. /**
  17010. * Created with JetBrains WebStorm.
  17011. * User: taoqili
  17012. * Date: 13-1-18
  17013. * Time: 上午11:09
  17014. * To change this template use File | Settings | File Templates.
  17015. */
  17016. /**
  17017. * UE表格操作类
  17018. * @param table
  17019. * @constructor
  17020. */
  17021. (function () {
  17022. var UETable = UE.UETable = function (table) {
  17023. this.table = table;
  17024. this.indexTable = [];
  17025. this.selectedTds = [];
  17026. this.cellsRange = {};
  17027. this.update(table);
  17028. };
  17029. //===以下为静态工具方法===
  17030. UETable.removeSelectedClass = function (cells) {
  17031. utils.each(cells, function (cell) {
  17032. domUtils.removeClasses(cell, "selectTdClass");
  17033. })
  17034. };
  17035. UETable.addSelectedClass = function (cells) {
  17036. utils.each(cells, function (cell) {
  17037. domUtils.addClass(cell, "selectTdClass");
  17038. })
  17039. };
  17040. UETable.isEmptyBlock = function (node) {
  17041. var reg = new RegExp(domUtils.fillChar, 'g');
  17042. if (node[browser.ie ? 'innerText' : 'textContent'].replace(/^\s*$/, '').replace(reg, '').length > 0) {
  17043. return 0;
  17044. }
  17045. for (var i in dtd.$isNotEmpty)
  17046. if (dtd.$isNotEmpty.hasOwnProperty(i)) {
  17047. if (node.getElementsByTagName(i).length) {
  17048. return 0;
  17049. }
  17050. }
  17051. return 1;
  17052. };
  17053. UETable.getWidth = function (cell) {
  17054. if (!cell) return 0;
  17055. return parseInt(domUtils.getComputedStyle(cell, "width"), 10);
  17056. };
  17057. /**
  17058. * 获取单元格或者单元格组的“对齐”状态。 如果当前的检测对象是一个单元格组, 只有在满足所有单元格的 水平和竖直 对齐属性都相同的
  17059. * 条件时才会返回其状态值,否则将返回null; 如果当前只检测了一个单元格, 则直接返回当前单元格的对齐状态;
  17060. * @param table cell or table cells , 支持单个单元格dom对象 或者 单元格dom对象数组
  17061. * @return { align: 'left' || 'right' || 'center', valign: 'top' || 'middle' || 'bottom' } 或者 null
  17062. */
  17063. UETable.getTableCellAlignState = function (cells) {
  17064. !utils.isArray(cells) && (cells = [cells]);
  17065. var result = {},
  17066. status = ['align', 'valign'],
  17067. tempStatus = null,
  17068. isSame = true; //状态是否相同
  17069. utils.each(cells, function (cellNode) {
  17070. utils.each(status, function (currentState) {
  17071. tempStatus = cellNode.getAttribute(currentState);
  17072. if (!result[currentState] && tempStatus) {
  17073. result[currentState] = tempStatus;
  17074. } else if (!result[currentState] || (tempStatus !== result[currentState])) {
  17075. isSame = false;
  17076. return false;
  17077. }
  17078. });
  17079. return isSame;
  17080. });
  17081. return isSame ? result : null;
  17082. };
  17083. /**
  17084. * 根据当前选区获取相关的table信息
  17085. * @return {Object}
  17086. */
  17087. UETable.getTableItemsByRange = function (editor) {
  17088. var start = editor.selection.getStart();
  17089. //ff下会选中bookmark
  17090. if (start && start.id && start.id.indexOf('_baidu_bookmark_start_') === 0 && start.nextSibling) {
  17091. start = start.nextSibling;
  17092. }
  17093. //在table或者td边缘有可能存在选中tr的情况
  17094. var cell = start && domUtils.findParentByTagName(start, ["td", "th"], true),
  17095. tr = cell && cell.parentNode,
  17096. caption = start && domUtils.findParentByTagName(start, 'caption', true),
  17097. table = caption ? caption.parentNode : tr && tr.parentNode.parentNode;
  17098. return {
  17099. cell: cell,
  17100. tr: tr,
  17101. table: table,
  17102. caption: caption
  17103. }
  17104. };
  17105. UETable.getUETableBySelected = function (editor) {
  17106. var table = UETable.getTableItemsByRange(editor).table;
  17107. if (table && table.ueTable && table.ueTable.selectedTds.length) {
  17108. return table.ueTable;
  17109. }
  17110. return null;
  17111. };
  17112. UETable.getDefaultValue = function (editor, table) {
  17113. var borderMap = {
  17114. thin: '0px',
  17115. medium: '1px',
  17116. thick: '2px'
  17117. },
  17118. tableBorder, tdPadding, tdBorder, tmpValue;
  17119. if (!table) {
  17120. table = editor.document.createElement('table');
  17121. table.insertRow(0).insertCell(0).innerHTML = 'xxx';
  17122. editor.body.appendChild(table);
  17123. var td = table.getElementsByTagName('td')[0];
  17124. tmpValue = domUtils.getComputedStyle(table, 'border-left-width');
  17125. tableBorder = parseInt(borderMap[tmpValue] || tmpValue, 10);
  17126. tmpValue = domUtils.getComputedStyle(td, 'padding-left');
  17127. tdPadding = parseInt(borderMap[tmpValue] || tmpValue, 10);
  17128. tmpValue = domUtils.getComputedStyle(td, 'border-left-width');
  17129. tdBorder = parseInt(borderMap[tmpValue] || tmpValue, 10);
  17130. domUtils.remove(table);
  17131. return {
  17132. tableBorder: tableBorder,
  17133. tdPadding: tdPadding,
  17134. tdBorder: tdBorder
  17135. };
  17136. } else {
  17137. td = table.getElementsByTagName('td')[0];
  17138. tmpValue = domUtils.getComputedStyle(table, 'border-left-width');
  17139. tableBorder = parseInt(borderMap[tmpValue] || tmpValue, 10);
  17140. tmpValue = domUtils.getComputedStyle(td, 'padding-left');
  17141. tdPadding = parseInt(borderMap[tmpValue] || tmpValue, 10);
  17142. tmpValue = domUtils.getComputedStyle(td, 'border-left-width');
  17143. tdBorder = parseInt(borderMap[tmpValue] || tmpValue, 10);
  17144. return {
  17145. tableBorder: tableBorder,
  17146. tdPadding: tdPadding,
  17147. tdBorder: tdBorder
  17148. };
  17149. }
  17150. };
  17151. /**
  17152. * 根据当前点击的td或者table获取索引对象
  17153. * @param tdOrTable
  17154. */
  17155. UETable.getUETable = function (tdOrTable) {
  17156. var tag = tdOrTable.tagName.toLowerCase();
  17157. tdOrTable = (tag == "td" || tag == "th" || tag == 'caption') ? domUtils.findParentByTagName(tdOrTable, "table", true) : tdOrTable;
  17158. if (!tdOrTable.ueTable) {
  17159. tdOrTable.ueTable = new UETable(tdOrTable);
  17160. }
  17161. return tdOrTable.ueTable;
  17162. };
  17163. UETable.cloneCell = function (cell, ignoreMerge, keepPro) {
  17164. if (!cell || utils.isString(cell)) {
  17165. return this.table.ownerDocument.createElement(cell || 'td');
  17166. }
  17167. var flag = domUtils.hasClass(cell, "selectTdClass");
  17168. flag && domUtils.removeClasses(cell, "selectTdClass");
  17169. var tmpCell = cell.cloneNode(true);
  17170. if (ignoreMerge) {
  17171. tmpCell.rowSpan = tmpCell.colSpan = 1;
  17172. }
  17173. //去掉宽高
  17174. !keepPro && domUtils.removeAttributes(tmpCell, 'width height');
  17175. !keepPro && domUtils.removeAttributes(tmpCell, 'style');
  17176. tmpCell.style.borderLeftStyle = "";
  17177. tmpCell.style.borderTopStyle = "";
  17178. tmpCell.style.borderLeftColor = cell.style.borderRightColor;
  17179. tmpCell.style.borderLeftWidth = cell.style.borderRightWidth;
  17180. tmpCell.style.borderTopColor = cell.style.borderBottomColor;
  17181. tmpCell.style.borderTopWidth = cell.style.borderBottomWidth;
  17182. flag && domUtils.addClass(cell, "selectTdClass");
  17183. return tmpCell;
  17184. }
  17185. UETable.prototype = {
  17186. getMaxRows: function () {
  17187. var rows = this.table.rows,
  17188. maxLen = 1;
  17189. for (var i = 0, row; row = rows[i]; i++) {
  17190. var currentMax = 1;
  17191. for (var j = 0, cj; cj = row.cells[j++];) {
  17192. currentMax = Math.max(cj.rowSpan || 1, currentMax);
  17193. }
  17194. maxLen = Math.max(currentMax + i, maxLen);
  17195. }
  17196. return maxLen;
  17197. },
  17198. /**
  17199. * 获取当前表格的最大列数
  17200. */
  17201. getMaxCols: function () {
  17202. var rows = this.table.rows,
  17203. maxLen = 0,
  17204. cellRows = {};
  17205. for (var i = 0, row; row = rows[i]; i++) {
  17206. var cellsNum = 0;
  17207. for (var j = 0, cj; cj = row.cells[j++];) {
  17208. cellsNum += (cj.colSpan || 1);
  17209. if (cj.rowSpan && cj.rowSpan > 1) {
  17210. for (var k = 1; k < cj.rowSpan; k++) {
  17211. if (!cellRows['row_' + (i + k)]) {
  17212. cellRows['row_' + (i + k)] = (cj.colSpan || 1);
  17213. } else {
  17214. cellRows['row_' + (i + k)]++
  17215. }
  17216. }
  17217. }
  17218. }
  17219. cellsNum += cellRows['row_' + i] || 0;
  17220. maxLen = Math.max(cellsNum, maxLen);
  17221. }
  17222. return maxLen;
  17223. },
  17224. getCellColIndex: function (cell) {
  17225. },
  17226. /**
  17227. * 获取当前cell旁边的单元格,
  17228. * @param cell
  17229. * @param right
  17230. */
  17231. getHSideCell: function (cell, right) {
  17232. try {
  17233. var cellInfo = this.getCellInfo(cell),
  17234. previewRowIndex, previewColIndex;
  17235. var len = this.selectedTds.length,
  17236. range = this.cellsRange;
  17237. //首行或者首列没有前置单元格
  17238. if ((!right && (!len ? !cellInfo.colIndex : !range.beginColIndex)) || (right && (!len ? (cellInfo.colIndex == (this.colsNum - 1)) : (range.endColIndex == this.colsNum - 1)))) return null;
  17239. previewRowIndex = !len ? cellInfo.rowIndex : range.beginRowIndex;
  17240. previewColIndex = !right ? (!len ? (cellInfo.colIndex < 1 ? 0 : (cellInfo.colIndex - 1)) : range.beginColIndex - 1) :
  17241. (!len ? cellInfo.colIndex + 1 : range.endColIndex + 1);
  17242. return this.getCell(this.indexTable[previewRowIndex][previewColIndex].rowIndex, this.indexTable[previewRowIndex][previewColIndex].cellIndex);
  17243. } catch (e) {
  17244. showError(e);
  17245. }
  17246. },
  17247. getTabNextCell: function (cell, preRowIndex) {
  17248. var cellInfo = this.getCellInfo(cell),
  17249. rowIndex = preRowIndex || cellInfo.rowIndex,
  17250. colIndex = cellInfo.colIndex + 1 + (cellInfo.colSpan - 1),
  17251. nextCell;
  17252. try {
  17253. nextCell = this.getCell(this.indexTable[rowIndex][colIndex].rowIndex, this.indexTable[rowIndex][colIndex].cellIndex);
  17254. } catch (e) {
  17255. try {
  17256. rowIndex = rowIndex * 1 + 1;
  17257. colIndex = 0;
  17258. nextCell = this.getCell(this.indexTable[rowIndex][colIndex].rowIndex, this.indexTable[rowIndex][colIndex].cellIndex);
  17259. } catch (e) {}
  17260. }
  17261. return nextCell;
  17262. },
  17263. /**
  17264. * 获取视觉上的后置单元格
  17265. * @param cell
  17266. * @param bottom
  17267. */
  17268. getVSideCell: function (cell, bottom, ignoreRange) {
  17269. try {
  17270. var cellInfo = this.getCellInfo(cell),
  17271. nextRowIndex, nextColIndex;
  17272. var len = this.selectedTds.length && !ignoreRange,
  17273. range = this.cellsRange;
  17274. //末行或者末列没有后置单元格
  17275. if ((!bottom && (cellInfo.rowIndex == 0)) || (bottom && (!len ? (cellInfo.rowIndex + cellInfo.rowSpan > this.rowsNum - 1) : (range.endRowIndex == this.rowsNum - 1)))) return null;
  17276. nextRowIndex = !bottom ? (!len ? cellInfo.rowIndex - 1 : range.beginRowIndex - 1) :
  17277. (!len ? (cellInfo.rowIndex + cellInfo.rowSpan) : range.endRowIndex + 1);
  17278. nextColIndex = !len ? cellInfo.colIndex : range.beginColIndex;
  17279. return this.getCell(this.indexTable[nextRowIndex][nextColIndex].rowIndex, this.indexTable[nextRowIndex][nextColIndex].cellIndex);
  17280. } catch (e) {
  17281. showError(e);
  17282. }
  17283. },
  17284. /**
  17285. * 获取相同结束位置的单元格,xOrY指代了是获取x轴相同还是y轴相同
  17286. */
  17287. getSameEndPosCells: function (cell, xOrY) {
  17288. try {
  17289. var flag = (xOrY.toLowerCase() === "x"),
  17290. end = domUtils.getXY(cell)[flag ? 'x' : 'y'] + cell["offset" + (flag ? 'Width' : 'Height')],
  17291. rows = this.table.rows,
  17292. cells = null,
  17293. returns = [];
  17294. for (var i = 0; i < this.rowsNum; i++) {
  17295. cells = rows[i].cells;
  17296. for (var j = 0, tmpCell; tmpCell = cells[j++];) {
  17297. var tmpEnd = domUtils.getXY(tmpCell)[flag ? 'x' : 'y'] + tmpCell["offset" + (flag ? 'Width' : 'Height')];
  17298. //对应行的td已经被上面行rowSpan了
  17299. if (tmpEnd > end && flag) break;
  17300. if (cell == tmpCell || end == tmpEnd) {
  17301. //只获取单一的单元格
  17302. //todo 仅获取单一单元格在特定情况下会造成returns为空,从而影响后续的拖拽实现,修正这个。需考虑性能
  17303. if (tmpCell[flag ? "colSpan" : "rowSpan"] == 1) {
  17304. returns.push(tmpCell);
  17305. }
  17306. if (flag) break;
  17307. }
  17308. }
  17309. }
  17310. return returns;
  17311. } catch (e) {
  17312. showError(e);
  17313. }
  17314. },
  17315. setCellContent: function (cell, content) {
  17316. cell.innerHTML = content || (browser.ie ? domUtils.fillChar : "<br />");
  17317. },
  17318. cloneCell: UETable.cloneCell,
  17319. /**
  17320. * 获取跟当前单元格的右边竖线为左边的所有未合并单元格
  17321. */
  17322. getSameStartPosXCells: function (cell) {
  17323. try {
  17324. var start = domUtils.getXY(cell).x + cell.offsetWidth,
  17325. rows = this.table.rows,
  17326. cells, returns = [];
  17327. for (var i = 0; i < this.rowsNum; i++) {
  17328. cells = rows[i].cells;
  17329. for (var j = 0, tmpCell; tmpCell = cells[j++];) {
  17330. var tmpStart = domUtils.getXY(tmpCell).x;
  17331. if (tmpStart > start) break;
  17332. if (tmpStart == start && tmpCell.colSpan == 1) {
  17333. returns.push(tmpCell);
  17334. break;
  17335. }
  17336. }
  17337. }
  17338. return returns;
  17339. } catch (e) {
  17340. showError(e);
  17341. }
  17342. },
  17343. /**
  17344. * 更新table对应的索引表
  17345. */
  17346. update: function (table) {
  17347. this.table = table || this.table;
  17348. this.selectedTds = [];
  17349. this.cellsRange = {};
  17350. this.indexTable = [];
  17351. var rows = this.table.rows,
  17352. rowsNum = this.getMaxRows(),
  17353. dNum = rowsNum - rows.length,
  17354. colsNum = this.getMaxCols();
  17355. while (dNum--) {
  17356. this.table.insertRow(rows.length);
  17357. }
  17358. this.rowsNum = rowsNum;
  17359. this.colsNum = colsNum;
  17360. for (var i = 0, len = rows.length; i < len; i++) {
  17361. this.indexTable[i] = new Array(colsNum);
  17362. }
  17363. //填充索引表
  17364. for (var rowIndex = 0, row; row = rows[rowIndex]; rowIndex++) {
  17365. for (var cellIndex = 0, cell, cells = row.cells; cell = cells[cellIndex]; cellIndex++) {
  17366. //修正整行被rowSpan时导致的行数计算错误
  17367. if (cell.rowSpan > rowsNum) {
  17368. cell.rowSpan = rowsNum;
  17369. }
  17370. var colIndex = cellIndex,
  17371. rowSpan = cell.rowSpan || 1,
  17372. colSpan = cell.colSpan || 1;
  17373. //当已经被上一行rowSpan或者被前一列colSpan了,则跳到下一个单元格进行
  17374. while (this.indexTable[rowIndex][colIndex]) colIndex++;
  17375. for (var j = 0; j < rowSpan; j++) {
  17376. for (var k = 0; k < colSpan; k++) {
  17377. this.indexTable[rowIndex + j][colIndex + k] = {
  17378. rowIndex: rowIndex,
  17379. cellIndex: cellIndex,
  17380. colIndex: colIndex,
  17381. rowSpan: rowSpan,
  17382. colSpan: colSpan
  17383. }
  17384. }
  17385. }
  17386. }
  17387. }
  17388. //修复残缺td
  17389. for (j = 0; j < rowsNum; j++) {
  17390. for (k = 0; k < colsNum; k++) {
  17391. if (this.indexTable[j][k] === undefined) {
  17392. row = rows[j];
  17393. cell = row.cells[row.cells.length - 1];
  17394. cell = cell ? cell.cloneNode(true) : this.table.ownerDocument.createElement("td");
  17395. this.setCellContent(cell);
  17396. if (cell.colSpan !== 1) cell.colSpan = 1;
  17397. if (cell.rowSpan !== 1) cell.rowSpan = 1;
  17398. row.appendChild(cell);
  17399. this.indexTable[j][k] = {
  17400. rowIndex: j,
  17401. cellIndex: cell.cellIndex,
  17402. colIndex: k,
  17403. rowSpan: 1,
  17404. colSpan: 1
  17405. }
  17406. }
  17407. }
  17408. }
  17409. //当框选后删除行或者列后撤销,需要重建选区。
  17410. var tds = domUtils.getElementsByTagName(this.table, "td"),
  17411. selectTds = [];
  17412. utils.each(tds, function (td) {
  17413. if (domUtils.hasClass(td, "selectTdClass")) {
  17414. selectTds.push(td);
  17415. }
  17416. });
  17417. if (selectTds.length) {
  17418. var start = selectTds[0],
  17419. end = selectTds[selectTds.length - 1],
  17420. startInfo = this.getCellInfo(start),
  17421. endInfo = this.getCellInfo(end);
  17422. this.selectedTds = selectTds;
  17423. this.cellsRange = {
  17424. beginRowIndex: startInfo.rowIndex,
  17425. beginColIndex: startInfo.colIndex,
  17426. endRowIndex: endInfo.rowIndex + endInfo.rowSpan - 1,
  17427. endColIndex: endInfo.colIndex + endInfo.colSpan - 1
  17428. };
  17429. }
  17430. //给第一行设置firstRow的样式名称,在排序图标的样式上使用到
  17431. if (!domUtils.hasClass(this.table.rows[0], "firstRow")) {
  17432. domUtils.addClass(this.table.rows[0], "firstRow");
  17433. for (var i = 1; i < this.table.rows.length; i++) {
  17434. domUtils.removeClasses(this.table.rows[i], "firstRow");
  17435. }
  17436. }
  17437. },
  17438. /**
  17439. * 获取单元格的索引信息
  17440. */
  17441. getCellInfo: function (cell) {
  17442. if (!cell) return;
  17443. var cellIndex = cell.cellIndex,
  17444. rowIndex = cell.parentNode.rowIndex,
  17445. rowInfo = this.indexTable[rowIndex],
  17446. numCols = this.colsNum;
  17447. for (var colIndex = cellIndex; colIndex < numCols; colIndex++) {
  17448. var cellInfo = rowInfo[colIndex];
  17449. if (cellInfo.rowIndex === rowIndex && cellInfo.cellIndex === cellIndex) {
  17450. return cellInfo;
  17451. }
  17452. }
  17453. },
  17454. /**
  17455. * 根据行列号获取单元格
  17456. */
  17457. getCell: function (rowIndex, cellIndex) {
  17458. return rowIndex < this.rowsNum && this.table.rows[rowIndex].cells[cellIndex] || null;
  17459. },
  17460. /**
  17461. * 删除单元格
  17462. */
  17463. deleteCell: function (cell, rowIndex) {
  17464. rowIndex = typeof rowIndex == 'number' ? rowIndex : cell.parentNode.rowIndex;
  17465. var row = this.table.rows[rowIndex];
  17466. row.deleteCell(cell.cellIndex);
  17467. },
  17468. /**
  17469. * 根据始末两个单元格获取被框选的所有单元格范围
  17470. */
  17471. getCellsRange: function (cellA, cellB) {
  17472. function checkRange(beginRowIndex, beginColIndex, endRowIndex, endColIndex) {
  17473. var tmpBeginRowIndex = beginRowIndex,
  17474. tmpBeginColIndex = beginColIndex,
  17475. tmpEndRowIndex = endRowIndex,
  17476. tmpEndColIndex = endColIndex,
  17477. cellInfo, colIndex, rowIndex;
  17478. // 通过indexTable检查是否存在超出TableRange上边界的情况
  17479. if (beginRowIndex > 0) {
  17480. for (colIndex = beginColIndex; colIndex < endColIndex; colIndex++) {
  17481. cellInfo = me.indexTable[beginRowIndex][colIndex];
  17482. rowIndex = cellInfo.rowIndex;
  17483. if (rowIndex < beginRowIndex) {
  17484. tmpBeginRowIndex = Math.min(rowIndex, tmpBeginRowIndex);
  17485. }
  17486. }
  17487. }
  17488. // 通过indexTable检查是否存在超出TableRange右边界的情况
  17489. if (endColIndex < me.colsNum) {
  17490. for (rowIndex = beginRowIndex; rowIndex < endRowIndex; rowIndex++) {
  17491. cellInfo = me.indexTable[rowIndex][endColIndex];
  17492. colIndex = cellInfo.colIndex + cellInfo.colSpan - 1;
  17493. if (colIndex > endColIndex) {
  17494. tmpEndColIndex = Math.max(colIndex, tmpEndColIndex);
  17495. }
  17496. }
  17497. }
  17498. // 检查是否有超出TableRange下边界的情况
  17499. if (endRowIndex < me.rowsNum) {
  17500. for (colIndex = beginColIndex; colIndex < endColIndex; colIndex++) {
  17501. cellInfo = me.indexTable[endRowIndex][colIndex];
  17502. rowIndex = cellInfo.rowIndex + cellInfo.rowSpan - 1;
  17503. if (rowIndex > endRowIndex) {
  17504. tmpEndRowIndex = Math.max(rowIndex, tmpEndRowIndex);
  17505. }
  17506. }
  17507. }
  17508. // 检查是否有超出TableRange左边界的情况
  17509. if (beginColIndex > 0) {
  17510. for (rowIndex = beginRowIndex; rowIndex < endRowIndex; rowIndex++) {
  17511. cellInfo = me.indexTable[rowIndex][beginColIndex];
  17512. colIndex = cellInfo.colIndex;
  17513. if (colIndex < beginColIndex) {
  17514. tmpBeginColIndex = Math.min(cellInfo.colIndex, tmpBeginColIndex);
  17515. }
  17516. }
  17517. }
  17518. //递归调用直至所有完成所有框选单元格的扩展
  17519. if (tmpBeginRowIndex != beginRowIndex || tmpBeginColIndex != beginColIndex || tmpEndRowIndex != endRowIndex || tmpEndColIndex != endColIndex) {
  17520. return checkRange(tmpBeginRowIndex, tmpBeginColIndex, tmpEndRowIndex, tmpEndColIndex);
  17521. } else {
  17522. // 不需要扩展TableRange的情况
  17523. return {
  17524. beginRowIndex: beginRowIndex,
  17525. beginColIndex: beginColIndex,
  17526. endRowIndex: endRowIndex,
  17527. endColIndex: endColIndex
  17528. };
  17529. }
  17530. }
  17531. try {
  17532. var me = this,
  17533. cellAInfo = me.getCellInfo(cellA);
  17534. if (cellA === cellB) {
  17535. return {
  17536. beginRowIndex: cellAInfo.rowIndex,
  17537. beginColIndex: cellAInfo.colIndex,
  17538. endRowIndex: cellAInfo.rowIndex + cellAInfo.rowSpan - 1,
  17539. endColIndex: cellAInfo.colIndex + cellAInfo.colSpan - 1
  17540. };
  17541. }
  17542. var cellBInfo = me.getCellInfo(cellB);
  17543. // 计算TableRange的四个边
  17544. var beginRowIndex = Math.min(cellAInfo.rowIndex, cellBInfo.rowIndex),
  17545. beginColIndex = Math.min(cellAInfo.colIndex, cellBInfo.colIndex),
  17546. endRowIndex = Math.max(cellAInfo.rowIndex + cellAInfo.rowSpan - 1, cellBInfo.rowIndex + cellBInfo.rowSpan - 1),
  17547. endColIndex = Math.max(cellAInfo.colIndex + cellAInfo.colSpan - 1, cellBInfo.colIndex + cellBInfo.colSpan - 1);
  17548. return checkRange(beginRowIndex, beginColIndex, endRowIndex, endColIndex);
  17549. } catch (e) {
  17550. //throw e;
  17551. }
  17552. },
  17553. /**
  17554. * 依据cellsRange获取对应的单元格集合
  17555. */
  17556. getCells: function (range) {
  17557. //每次获取cells之前必须先清除上次的选择,否则会对后续获取操作造成影响
  17558. this.clearSelected();
  17559. var beginRowIndex = range.beginRowIndex,
  17560. beginColIndex = range.beginColIndex,
  17561. endRowIndex = range.endRowIndex,
  17562. endColIndex = range.endColIndex,
  17563. cellInfo, rowIndex, colIndex, tdHash = {},
  17564. returnTds = [];
  17565. for (var i = beginRowIndex; i <= endRowIndex; i++) {
  17566. for (var j = beginColIndex; j <= endColIndex; j++) {
  17567. cellInfo = this.indexTable[i][j];
  17568. rowIndex = cellInfo.rowIndex;
  17569. colIndex = cellInfo.colIndex;
  17570. // 如果Cells里已经包含了此Cell则跳过
  17571. var key = rowIndex + '|' + colIndex;
  17572. if (tdHash[key]) continue;
  17573. tdHash[key] = 1;
  17574. if (rowIndex < i || colIndex < j || rowIndex + cellInfo.rowSpan - 1 > endRowIndex || colIndex + cellInfo.colSpan - 1 > endColIndex) {
  17575. return null;
  17576. }
  17577. returnTds.push(this.getCell(rowIndex, cellInfo.cellIndex));
  17578. }
  17579. }
  17580. return returnTds;
  17581. },
  17582. /**
  17583. * 清理已经选中的单元格
  17584. */
  17585. clearSelected: function () {
  17586. UETable.removeSelectedClass(this.selectedTds);
  17587. this.selectedTds = [];
  17588. this.cellsRange = {};
  17589. },
  17590. /**
  17591. * 根据range设置已经选中的单元格
  17592. */
  17593. setSelected: function (range) {
  17594. var cells = this.getCells(range);
  17595. UETable.addSelectedClass(cells);
  17596. this.selectedTds = cells;
  17597. this.cellsRange = range;
  17598. },
  17599. isFullRow: function () {
  17600. var range = this.cellsRange;
  17601. return (range.endColIndex - range.beginColIndex + 1) == this.colsNum;
  17602. },
  17603. isFullCol: function () {
  17604. var range = this.cellsRange,
  17605. table = this.table,
  17606. ths = table.getElementsByTagName("th"),
  17607. rows = range.endRowIndex - range.beginRowIndex + 1;
  17608. return !ths.length ? rows == this.rowsNum : rows == this.rowsNum || (rows == this.rowsNum - 1);
  17609. },
  17610. /**
  17611. * 获取视觉上的前置单元格,默认是左边,top传入时
  17612. * @param cell
  17613. * @param top
  17614. */
  17615. getNextCell: function (cell, bottom, ignoreRange) {
  17616. try {
  17617. var cellInfo = this.getCellInfo(cell),
  17618. nextRowIndex, nextColIndex;
  17619. var len = this.selectedTds.length && !ignoreRange,
  17620. range = this.cellsRange;
  17621. //末行或者末列没有后置单元格
  17622. if ((!bottom && (cellInfo.rowIndex == 0)) || (bottom && (!len ? (cellInfo.rowIndex + cellInfo.rowSpan > this.rowsNum - 1) : (range.endRowIndex == this.rowsNum - 1)))) return null;
  17623. nextRowIndex = !bottom ? (!len ? cellInfo.rowIndex - 1 : range.beginRowIndex - 1) :
  17624. (!len ? (cellInfo.rowIndex + cellInfo.rowSpan) : range.endRowIndex + 1);
  17625. nextColIndex = !len ? cellInfo.colIndex : range.beginColIndex;
  17626. return this.getCell(this.indexTable[nextRowIndex][nextColIndex].rowIndex, this.indexTable[nextRowIndex][nextColIndex].cellIndex);
  17627. } catch (e) {
  17628. showError(e);
  17629. }
  17630. },
  17631. getPreviewCell: function (cell, top) {
  17632. try {
  17633. var cellInfo = this.getCellInfo(cell),
  17634. previewRowIndex, previewColIndex;
  17635. var len = this.selectedTds.length,
  17636. range = this.cellsRange;
  17637. //首行或者首列没有前置单元格
  17638. if ((!top && (!len ? !cellInfo.colIndex : !range.beginColIndex)) || (top && (!len ? (cellInfo.rowIndex > (this.colsNum - 1)) : (range.endColIndex == this.colsNum - 1)))) return null;
  17639. previewRowIndex = !top ? (!len ? cellInfo.rowIndex : range.beginRowIndex) :
  17640. (!len ? (cellInfo.rowIndex < 1 ? 0 : (cellInfo.rowIndex - 1)) : range.beginRowIndex);
  17641. previewColIndex = !top ? (!len ? (cellInfo.colIndex < 1 ? 0 : (cellInfo.colIndex - 1)) : range.beginColIndex - 1) :
  17642. (!len ? cellInfo.colIndex : range.endColIndex + 1);
  17643. return this.getCell(this.indexTable[previewRowIndex][previewColIndex].rowIndex, this.indexTable[previewRowIndex][previewColIndex].cellIndex);
  17644. } catch (e) {
  17645. showError(e);
  17646. }
  17647. },
  17648. /**
  17649. * 移动单元格中的内容
  17650. */
  17651. moveContent: function (cellTo, cellFrom) {
  17652. if (UETable.isEmptyBlock(cellFrom)) return;
  17653. if (UETable.isEmptyBlock(cellTo)) {
  17654. cellTo.innerHTML = cellFrom.innerHTML;
  17655. return;
  17656. }
  17657. var child = cellTo.lastChild;
  17658. if (child.nodeType == 3 || !dtd.$block[child.tagName]) {
  17659. cellTo.appendChild(cellTo.ownerDocument.createElement('br'))
  17660. }
  17661. while (child = cellFrom.firstChild) {
  17662. cellTo.appendChild(child);
  17663. }
  17664. },
  17665. /**
  17666. * 向右合并单元格
  17667. */
  17668. mergeRight: function (cell) {
  17669. var cellInfo = this.getCellInfo(cell),
  17670. rightColIndex = cellInfo.colIndex + cellInfo.colSpan,
  17671. rightCellInfo = this.indexTable[cellInfo.rowIndex][rightColIndex],
  17672. rightCell = this.getCell(rightCellInfo.rowIndex, rightCellInfo.cellIndex);
  17673. //合并
  17674. cell.colSpan = cellInfo.colSpan + rightCellInfo.colSpan;
  17675. //被合并的单元格不应存在宽度属性
  17676. cell.removeAttribute("width");
  17677. //移动内容
  17678. this.moveContent(cell, rightCell);
  17679. //删掉被合并的Cell
  17680. this.deleteCell(rightCell, rightCellInfo.rowIndex);
  17681. this.update();
  17682. },
  17683. /**
  17684. * 向下合并单元格
  17685. */
  17686. mergeDown: function (cell) {
  17687. var cellInfo = this.getCellInfo(cell),
  17688. downRowIndex = cellInfo.rowIndex + cellInfo.rowSpan,
  17689. downCellInfo = this.indexTable[downRowIndex][cellInfo.colIndex],
  17690. downCell = this.getCell(downCellInfo.rowIndex, downCellInfo.cellIndex);
  17691. cell.rowSpan = cellInfo.rowSpan + downCellInfo.rowSpan;
  17692. cell.removeAttribute("height");
  17693. this.moveContent(cell, downCell);
  17694. this.deleteCell(downCell, downCellInfo.rowIndex);
  17695. this.update();
  17696. },
  17697. /**
  17698. * 合并整个range中的内容
  17699. */
  17700. mergeRange: function () {
  17701. //由于合并操作可以在任意时刻进行,所以无法通过鼠标位置等信息实时生成range,只能通过缓存实例中的cellsRange对象来访问
  17702. var range = this.cellsRange,
  17703. leftTopCell = this.getCell(range.beginRowIndex, this.indexTable[range.beginRowIndex][range.beginColIndex].cellIndex);
  17704. if (leftTopCell.tagName == "TH" && range.endRowIndex !== range.beginRowIndex) {
  17705. var index = this.indexTable,
  17706. info = this.getCellInfo(leftTopCell);
  17707. leftTopCell = this.getCell(1, index[1][info.colIndex].cellIndex);
  17708. range = this.getCellsRange(leftTopCell, this.getCell(index[this.rowsNum - 1][info.colIndex].rowIndex, index[this.rowsNum - 1][info.colIndex].cellIndex));
  17709. }
  17710. // 删除剩余的Cells
  17711. var cells = this.getCells(range);
  17712. for (var i = 0, ci; ci = cells[i++];) {
  17713. if (ci !== leftTopCell) {
  17714. this.moveContent(leftTopCell, ci);
  17715. this.deleteCell(ci);
  17716. }
  17717. }
  17718. // 修改左上角Cell的rowSpan和colSpan,并调整宽度属性设置
  17719. leftTopCell.rowSpan = range.endRowIndex - range.beginRowIndex + 1;
  17720. leftTopCell.rowSpan > 1 && leftTopCell.removeAttribute("height");
  17721. leftTopCell.colSpan = range.endColIndex - range.beginColIndex + 1;
  17722. leftTopCell.colSpan > 1 && leftTopCell.removeAttribute("width");
  17723. if (leftTopCell.rowSpan == this.rowsNum && leftTopCell.colSpan != 1) {
  17724. leftTopCell.colSpan = 1;
  17725. }
  17726. if (leftTopCell.colSpan == this.colsNum && leftTopCell.rowSpan != 1) {
  17727. var rowIndex = leftTopCell.parentNode.rowIndex;
  17728. //解决IE下的表格操作问题
  17729. if (this.table.deleteRow) {
  17730. for (var i = rowIndex + 1, curIndex = rowIndex + 1, len = leftTopCell.rowSpan; i < len; i++) {
  17731. this.table.deleteRow(curIndex);
  17732. }
  17733. } else {
  17734. for (var i = 0, len = leftTopCell.rowSpan - 1; i < len; i++) {
  17735. var row = this.table.rows[rowIndex + 1];
  17736. row.parentNode.removeChild(row);
  17737. }
  17738. }
  17739. leftTopCell.rowSpan = 1;
  17740. }
  17741. this.update();
  17742. },
  17743. /**
  17744. * 插入一行单元格
  17745. */
  17746. insertRow: function (rowIndex, sourceCell) {
  17747. var numCols = this.colsNum,
  17748. table = this.table,
  17749. row = table.insertRow(rowIndex),
  17750. cell,
  17751. isInsertTitle = typeof sourceCell == 'string' && sourceCell.toUpperCase() == 'TH';
  17752. function replaceTdToTh(colIndex, cell, tableRow) {
  17753. if (colIndex == 0) {
  17754. var tr = tableRow.nextSibling || tableRow.previousSibling,
  17755. th = tr.cells[colIndex];
  17756. if (th.tagName == 'TH') {
  17757. th = cell.ownerDocument.createElement("th");
  17758. th.appendChild(cell.firstChild);
  17759. tableRow.insertBefore(th, cell);
  17760. domUtils.remove(cell)
  17761. }
  17762. } else {
  17763. if (cell.tagName == 'TH') {
  17764. var td = cell.ownerDocument.createElement("td");
  17765. td.appendChild(cell.firstChild);
  17766. tableRow.insertBefore(td, cell);
  17767. domUtils.remove(cell)
  17768. }
  17769. }
  17770. }
  17771. //首行直接插入,无需考虑部分单元格被rowspan的情况
  17772. if (rowIndex == 0 || rowIndex == this.rowsNum) {
  17773. for (var colIndex = 0; colIndex < numCols; colIndex++) {
  17774. cell = this.cloneCell(sourceCell, true);
  17775. this.setCellContent(cell);
  17776. cell.getAttribute('vAlign') && cell.setAttribute('vAlign', cell.getAttribute('vAlign'));
  17777. row.appendChild(cell);
  17778. if (!isInsertTitle) replaceTdToTh(colIndex, cell, row);
  17779. }
  17780. } else {
  17781. var infoRow = this.indexTable[rowIndex],
  17782. cellIndex = 0;
  17783. for (colIndex = 0; colIndex < numCols; colIndex++) {
  17784. var cellInfo = infoRow[colIndex];
  17785. //如果存在某个单元格的rowspan穿过待插入行的位置,则修改该单元格的rowspan即可,无需插入单元格
  17786. if (cellInfo.rowIndex < rowIndex) {
  17787. cell = this.getCell(cellInfo.rowIndex, cellInfo.cellIndex);
  17788. cell.rowSpan = cellInfo.rowSpan + 1;
  17789. } else {
  17790. cell = this.cloneCell(sourceCell, true);
  17791. this.setCellContent(cell);
  17792. row.appendChild(cell);
  17793. }
  17794. if (!isInsertTitle) replaceTdToTh(colIndex, cell, row);
  17795. }
  17796. }
  17797. //框选时插入不触发contentchange,需要手动更新索引。
  17798. this.update();
  17799. return row;
  17800. },
  17801. /**
  17802. * 删除一行单元格
  17803. * @param rowIndex
  17804. */
  17805. deleteRow: function (rowIndex) {
  17806. var row = this.table.rows[rowIndex],
  17807. infoRow = this.indexTable[rowIndex],
  17808. colsNum = this.colsNum,
  17809. count = 0; //处理计数
  17810. for (var colIndex = 0; colIndex < colsNum;) {
  17811. var cellInfo = infoRow[colIndex],
  17812. cell = this.getCell(cellInfo.rowIndex, cellInfo.cellIndex);
  17813. if (cell.rowSpan > 1) {
  17814. if (cellInfo.rowIndex == rowIndex) {
  17815. var clone = cell.cloneNode(true);
  17816. clone.rowSpan = cell.rowSpan - 1;
  17817. clone.innerHTML = "";
  17818. cell.rowSpan = 1;
  17819. var nextRowIndex = rowIndex + 1,
  17820. nextRow = this.table.rows[nextRowIndex],
  17821. insertCellIndex,
  17822. preMerged = this.getPreviewMergedCellsNum(nextRowIndex, colIndex) - count;
  17823. if (preMerged < colIndex) {
  17824. insertCellIndex = colIndex - preMerged - 1;
  17825. //nextRow.insertCell(insertCellIndex);
  17826. domUtils.insertAfter(nextRow.cells[insertCellIndex], clone);
  17827. } else {
  17828. if (nextRow.cells.length) nextRow.insertBefore(clone, nextRow.cells[0])
  17829. }
  17830. count += 1;
  17831. //cell.parentNode.removeChild(cell);
  17832. }
  17833. }
  17834. colIndex += cell.colSpan || 1;
  17835. }
  17836. var deleteTds = [],
  17837. cacheMap = {};
  17838. for (colIndex = 0; colIndex < colsNum; colIndex++) {
  17839. var tmpRowIndex = infoRow[colIndex].rowIndex,
  17840. tmpCellIndex = infoRow[colIndex].cellIndex,
  17841. key = tmpRowIndex + "_" + tmpCellIndex;
  17842. if (cacheMap[key]) continue;
  17843. cacheMap[key] = 1;
  17844. cell = this.getCell(tmpRowIndex, tmpCellIndex);
  17845. deleteTds.push(cell);
  17846. }
  17847. var mergeTds = [];
  17848. utils.each(deleteTds, function (td) {
  17849. if (td.rowSpan == 1) {
  17850. td.parentNode.removeChild(td);
  17851. } else {
  17852. mergeTds.push(td);
  17853. }
  17854. });
  17855. utils.each(mergeTds, function (td) {
  17856. td.rowSpan--;
  17857. });
  17858. row.parentNode.removeChild(row);
  17859. //浏览器方法本身存在bug,采用自定义方法删除
  17860. //this.table.deleteRow(rowIndex);
  17861. this.update();
  17862. },
  17863. insertCol: function (colIndex, sourceCell, defaultValue) {
  17864. var rowsNum = this.rowsNum,
  17865. rowIndex = 0,
  17866. tableRow, cell,
  17867. backWidth = parseInt((this.table.offsetWidth - (this.colsNum + 1) * 20 - (this.colsNum + 1)) / (this.colsNum + 1), 10),
  17868. isInsertTitleCol = typeof sourceCell == 'string' && sourceCell.toUpperCase() == 'TH';
  17869. function replaceTdToTh(rowIndex, cell, tableRow) {
  17870. if (rowIndex == 0) {
  17871. var th = cell.nextSibling || cell.previousSibling;
  17872. if (th.tagName == 'TH') {
  17873. th = cell.ownerDocument.createElement("th");
  17874. th.appendChild(cell.firstChild);
  17875. tableRow.insertBefore(th, cell);
  17876. domUtils.remove(cell)
  17877. }
  17878. } else {
  17879. if (cell.tagName == 'TH') {
  17880. var td = cell.ownerDocument.createElement("td");
  17881. td.appendChild(cell.firstChild);
  17882. tableRow.insertBefore(td, cell);
  17883. domUtils.remove(cell)
  17884. }
  17885. }
  17886. }
  17887. var preCell;
  17888. if (colIndex == 0 || colIndex == this.colsNum) {
  17889. for (; rowIndex < rowsNum; rowIndex++) {
  17890. tableRow = this.table.rows[rowIndex];
  17891. preCell = tableRow.cells[colIndex == 0 ? colIndex : tableRow.cells.length];
  17892. cell = this.cloneCell(sourceCell, true); //tableRow.insertCell(colIndex == 0 ? colIndex : tableRow.cells.length);
  17893. this.setCellContent(cell);
  17894. cell.setAttribute('vAlign', cell.getAttribute('vAlign'));
  17895. preCell && cell.setAttribute('width', preCell.getAttribute('width'));
  17896. if (!colIndex) {
  17897. tableRow.insertBefore(cell, tableRow.cells[0]);
  17898. } else {
  17899. domUtils.insertAfter(tableRow.cells[tableRow.cells.length - 1], cell);
  17900. }
  17901. if (!isInsertTitleCol) replaceTdToTh(rowIndex, cell, tableRow)
  17902. }
  17903. } else {
  17904. for (; rowIndex < rowsNum; rowIndex++) {
  17905. var cellInfo = this.indexTable[rowIndex][colIndex];
  17906. if (cellInfo.colIndex < colIndex) {
  17907. cell = this.getCell(cellInfo.rowIndex, cellInfo.cellIndex);
  17908. cell.colSpan = cellInfo.colSpan + 1;
  17909. } else {
  17910. tableRow = this.table.rows[rowIndex];
  17911. preCell = tableRow.cells[cellInfo.cellIndex];
  17912. cell = this.cloneCell(sourceCell, true); //tableRow.insertCell(cellInfo.cellIndex);
  17913. this.setCellContent(cell);
  17914. cell.setAttribute('vAlign', cell.getAttribute('vAlign'));
  17915. preCell && cell.setAttribute('width', preCell.getAttribute('width'));
  17916. //防止IE下报错
  17917. preCell ? tableRow.insertBefore(cell, preCell) : tableRow.appendChild(cell);
  17918. }
  17919. if (!isInsertTitleCol) replaceTdToTh(rowIndex, cell, tableRow);
  17920. }
  17921. }
  17922. //框选时插入不触发contentchange,需要手动更新索引
  17923. this.update();
  17924. this.updateWidth(backWidth, defaultValue || {
  17925. tdPadding: 10,
  17926. tdBorder: 1
  17927. });
  17928. },
  17929. updateWidth: function (width, defaultValue) {
  17930. var table = this.table,
  17931. tmpWidth = UETable.getWidth(table) - defaultValue.tdPadding * 2 - defaultValue.tdBorder + width;
  17932. if (tmpWidth < table.ownerDocument.body.offsetWidth) {
  17933. table.setAttribute("width", tmpWidth);
  17934. return;
  17935. }
  17936. var tds = domUtils.getElementsByTagName(this.table, "td th");
  17937. utils.each(tds, function (td) {
  17938. td.setAttribute("width", width);
  17939. })
  17940. },
  17941. deleteCol: function (colIndex) {
  17942. var indexTable = this.indexTable,
  17943. tableRows = this.table.rows,
  17944. backTableWidth = this.table.getAttribute("width"),
  17945. backTdWidth = 0,
  17946. rowsNum = this.rowsNum,
  17947. cacheMap = {};
  17948. for (var rowIndex = 0; rowIndex < rowsNum;) {
  17949. var infoRow = indexTable[rowIndex],
  17950. cellInfo = infoRow[colIndex],
  17951. key = cellInfo.rowIndex + '_' + cellInfo.colIndex;
  17952. // 跳过已经处理过的Cell
  17953. if (cacheMap[key]) continue;
  17954. cacheMap[key] = 1;
  17955. var cell = this.getCell(cellInfo.rowIndex, cellInfo.cellIndex);
  17956. if (!backTdWidth) backTdWidth = cell && parseInt(cell.offsetWidth / cell.colSpan, 10).toFixed(0);
  17957. // 如果Cell的colSpan大于1, 就修改colSpan, 否则就删掉这个Cell
  17958. if (cell.colSpan > 1) {
  17959. cell.colSpan--;
  17960. } else {
  17961. tableRows[rowIndex].deleteCell(cellInfo.cellIndex);
  17962. }
  17963. rowIndex += cellInfo.rowSpan || 1;
  17964. }
  17965. this.table.setAttribute("width", backTableWidth - backTdWidth);
  17966. this.update();
  17967. },
  17968. splitToCells: function (cell) {
  17969. var me = this,
  17970. cells = this.splitToRows(cell);
  17971. utils.each(cells, function (cell) {
  17972. me.splitToCols(cell);
  17973. })
  17974. },
  17975. splitToRows: function (cell) {
  17976. var cellInfo = this.getCellInfo(cell),
  17977. rowIndex = cellInfo.rowIndex,
  17978. colIndex = cellInfo.colIndex,
  17979. results = [];
  17980. // 修改Cell的rowSpan
  17981. cell.rowSpan = 1;
  17982. results.push(cell);
  17983. // 补齐单元格
  17984. for (var i = rowIndex, endRow = rowIndex + cellInfo.rowSpan; i < endRow; i++) {
  17985. if (i == rowIndex) continue;
  17986. var tableRow = this.table.rows[i],
  17987. tmpCell = tableRow.insertCell(colIndex - this.getPreviewMergedCellsNum(i, colIndex));
  17988. tmpCell.colSpan = cellInfo.colSpan;
  17989. this.setCellContent(tmpCell);
  17990. tmpCell.setAttribute('vAlign', cell.getAttribute('vAlign'));
  17991. tmpCell.setAttribute('align', cell.getAttribute('align'));
  17992. if (cell.style.cssText) {
  17993. tmpCell.style.cssText = cell.style.cssText;
  17994. }
  17995. results.push(tmpCell);
  17996. }
  17997. this.update();
  17998. return results;
  17999. },
  18000. getPreviewMergedCellsNum: function (rowIndex, colIndex) {
  18001. var indexRow = this.indexTable[rowIndex],
  18002. num = 0;
  18003. for (var i = 0; i < colIndex;) {
  18004. var colSpan = indexRow[i].colSpan,
  18005. tmpRowIndex = indexRow[i].rowIndex;
  18006. num += (colSpan - (tmpRowIndex == rowIndex ? 1 : 0));
  18007. i += colSpan;
  18008. }
  18009. return num;
  18010. },
  18011. splitToCols: function (cell) {
  18012. var backWidth = (cell.offsetWidth / cell.colSpan - 22).toFixed(0),
  18013. cellInfo = this.getCellInfo(cell),
  18014. rowIndex = cellInfo.rowIndex,
  18015. colIndex = cellInfo.colIndex,
  18016. results = [];
  18017. // 修改Cell的rowSpan
  18018. cell.colSpan = 1;
  18019. cell.setAttribute("width", backWidth);
  18020. results.push(cell);
  18021. // 补齐单元格
  18022. for (var j = colIndex, endCol = colIndex + cellInfo.colSpan; j < endCol; j++) {
  18023. if (j == colIndex) continue;
  18024. var tableRow = this.table.rows[rowIndex],
  18025. tmpCell = tableRow.insertCell(this.indexTable[rowIndex][j].cellIndex + 1);
  18026. tmpCell.rowSpan = cellInfo.rowSpan;
  18027. this.setCellContent(tmpCell);
  18028. tmpCell.setAttribute('vAlign', cell.getAttribute('vAlign'));
  18029. tmpCell.setAttribute('align', cell.getAttribute('align'));
  18030. tmpCell.setAttribute('width', backWidth);
  18031. if (cell.style.cssText) {
  18032. tmpCell.style.cssText = cell.style.cssText;
  18033. }
  18034. //处理th的情况
  18035. if (cell.tagName == 'TH') {
  18036. var th = cell.ownerDocument.createElement('th');
  18037. th.appendChild(tmpCell.firstChild);
  18038. th.setAttribute('vAlign', cell.getAttribute('vAlign'));
  18039. th.rowSpan = tmpCell.rowSpan;
  18040. tableRow.insertBefore(th, tmpCell);
  18041. domUtils.remove(tmpCell);
  18042. }
  18043. results.push(tmpCell);
  18044. }
  18045. this.update();
  18046. return results;
  18047. },
  18048. isLastCell: function (cell, rowsNum, colsNum) {
  18049. rowsNum = rowsNum || this.rowsNum;
  18050. colsNum = colsNum || this.colsNum;
  18051. var cellInfo = this.getCellInfo(cell);
  18052. return ((cellInfo.rowIndex + cellInfo.rowSpan) == rowsNum) &&
  18053. ((cellInfo.colIndex + cellInfo.colSpan) == colsNum);
  18054. },
  18055. getLastCell: function (cells) {
  18056. cells = cells || this.table.getElementsByTagName("td");
  18057. var firstInfo = this.getCellInfo(cells[0]);
  18058. var me = this,
  18059. last = cells[0],
  18060. tr = last.parentNode,
  18061. cellsNum = 0,
  18062. cols = 0,
  18063. rows;
  18064. utils.each(cells, function (cell) {
  18065. if (cell.parentNode == tr) cols += cell.colSpan || 1;
  18066. cellsNum += cell.rowSpan * cell.colSpan || 1;
  18067. });
  18068. rows = cellsNum / cols;
  18069. utils.each(cells, function (cell) {
  18070. if (me.isLastCell(cell, rows, cols)) {
  18071. last = cell;
  18072. return false;
  18073. }
  18074. });
  18075. return last;
  18076. },
  18077. selectRow: function (rowIndex) {
  18078. var indexRow = this.indexTable[rowIndex],
  18079. start = this.getCell(indexRow[0].rowIndex, indexRow[0].cellIndex),
  18080. end = this.getCell(indexRow[this.colsNum - 1].rowIndex, indexRow[this.colsNum - 1].cellIndex),
  18081. range = this.getCellsRange(start, end);
  18082. this.setSelected(range);
  18083. },
  18084. selectTable: function () {
  18085. var tds = this.table.getElementsByTagName("td"),
  18086. range = this.getCellsRange(tds[0], tds[tds.length - 1]);
  18087. this.setSelected(range);
  18088. },
  18089. setBackground: function (cells, value) {
  18090. if (typeof value === "string") {
  18091. utils.each(cells, function (cell) {
  18092. cell.style.backgroundColor = value;
  18093. })
  18094. } else if (typeof value === "object") {
  18095. value = utils.extend({
  18096. repeat: true,
  18097. colorList: ["#ddd", "#fff"]
  18098. }, value);
  18099. var rowIndex = this.getCellInfo(cells[0]).rowIndex,
  18100. count = 0,
  18101. colors = value.colorList,
  18102. getColor = function (list, index, repeat) {
  18103. return list[index] ? list[index] : repeat ? list[index % list.length] : "";
  18104. };
  18105. for (var i = 0, cell; cell = cells[i++];) {
  18106. var cellInfo = this.getCellInfo(cell);
  18107. cell.style.backgroundColor = getColor(colors, ((rowIndex + count) == cellInfo.rowIndex) ? count : ++count, value.repeat);
  18108. }
  18109. }
  18110. },
  18111. removeBackground: function (cells) {
  18112. utils.each(cells, function (cell) {
  18113. cell.style.backgroundColor = "";
  18114. })
  18115. }
  18116. };
  18117. function showError(e) {}
  18118. })();
  18119. // plugins/table.cmds.js
  18120. /**
  18121. * Created with JetBrains PhpStorm.
  18122. * User: taoqili
  18123. * Date: 13-2-20
  18124. * Time: 下午6:25
  18125. * To change this template use File | Settings | File Templates.
  18126. */
  18127. ;
  18128. (function () {
  18129. var UT = UE.UETable,
  18130. getTableItemsByRange = function (editor) {
  18131. return UT.getTableItemsByRange(editor);
  18132. },
  18133. getUETableBySelected = function (editor) {
  18134. return UT.getUETableBySelected(editor)
  18135. },
  18136. getDefaultValue = function (editor, table) {
  18137. return UT.getDefaultValue(editor, table);
  18138. },
  18139. getUETable = function (tdOrTable) {
  18140. return UT.getUETable(tdOrTable);
  18141. };
  18142. UE.commands['inserttable'] = {
  18143. queryCommandState: function () {
  18144. return getTableItemsByRange(this).table ? -1 : 0;
  18145. },
  18146. execCommand: function (cmd, opt) {
  18147. function createTable(opt, tdWidth) {
  18148. var html = [],
  18149. rowsNum = opt.numRows,
  18150. colsNum = opt.numCols;
  18151. for (var r = 0; r < rowsNum; r++) {
  18152. html.push('<tr' + (r == 0 ? ' class="firstRow"' : '') + '>');
  18153. for (var c = 0; c < colsNum; c++) {
  18154. html.push('<td width="' + tdWidth + '" vAlign="' + opt.tdvalign + '" >' + (browser.ie && browser.version < 11 ? domUtils.fillChar : '<br/>') + '</td>')
  18155. }
  18156. html.push('</tr>')
  18157. }
  18158. //禁止指定table-width
  18159. return '<table><tbody>' + html.join('') + '</tbody></table>'
  18160. }
  18161. if (!opt) {
  18162. opt = utils.extend({}, {
  18163. numCols: this.options.defaultCols,
  18164. numRows: this.options.defaultRows,
  18165. tdvalign: this.options.tdvalign
  18166. })
  18167. }
  18168. var me = this;
  18169. var range = this.selection.getRange(),
  18170. start = range.startContainer,
  18171. firstParentBlock = domUtils.findParent(start, function (node) {
  18172. return domUtils.isBlockElm(node);
  18173. }, true) || me.body;
  18174. var defaultValue = getDefaultValue(me),
  18175. tableWidth = firstParentBlock.offsetWidth,
  18176. tdWidth = Math.floor(tableWidth / opt.numCols - defaultValue.tdPadding * 2 - defaultValue.tdBorder);
  18177. //todo其他属性
  18178. !opt.tdvalign && (opt.tdvalign = me.options.tdvalign);
  18179. me.execCommand("inserthtml", createTable(opt, tdWidth));
  18180. }
  18181. };
  18182. UE.commands['insertparagraphbeforetable'] = {
  18183. queryCommandState: function () {
  18184. return getTableItemsByRange(this).cell ? 0 : -1;
  18185. },
  18186. execCommand: function () {
  18187. var table = getTableItemsByRange(this).table;
  18188. if (table) {
  18189. var p = this.document.createElement("p");
  18190. p.innerHTML = browser.ie ? '&nbsp;' : '<br />';
  18191. table.parentNode.insertBefore(p, table);
  18192. this.selection.getRange().setStart(p, 0).setCursor();
  18193. }
  18194. }
  18195. };
  18196. UE.commands['deletetable'] = {
  18197. queryCommandState: function () {
  18198. var rng = this.selection.getRange();
  18199. return domUtils.findParentByTagName(rng.startContainer, 'table', true) ? 0 : -1;
  18200. },
  18201. execCommand: function (cmd, table) {
  18202. var rng = this.selection.getRange();
  18203. table = table || domUtils.findParentByTagName(rng.startContainer, 'table', true);
  18204. if (table) {
  18205. var next = table.nextSibling;
  18206. if (!next) {
  18207. next = domUtils.createElement(this.document, 'p', {
  18208. 'innerHTML': browser.ie ? domUtils.fillChar : '<br/>'
  18209. });
  18210. table.parentNode.insertBefore(next, table);
  18211. }
  18212. domUtils.remove(table);
  18213. rng = this.selection.getRange();
  18214. if (next.nodeType == 3) {
  18215. rng.setStartBefore(next)
  18216. } else {
  18217. rng.setStart(next, 0)
  18218. }
  18219. rng.setCursor(false, true)
  18220. this.fireEvent("tablehasdeleted")
  18221. }
  18222. }
  18223. };
  18224. UE.commands['cellalign'] = {
  18225. queryCommandState: function () {
  18226. return getSelectedArr(this).length ? 0 : -1
  18227. },
  18228. execCommand: function (cmd, align) {
  18229. var selectedTds = getSelectedArr(this);
  18230. if (selectedTds.length) {
  18231. for (var i = 0, ci; ci = selectedTds[i++];) {
  18232. ci.setAttribute('align', align);
  18233. }
  18234. }
  18235. }
  18236. };
  18237. UE.commands['cellvalign'] = {
  18238. queryCommandState: function () {
  18239. return getSelectedArr(this).length ? 0 : -1;
  18240. },
  18241. execCommand: function (cmd, valign) {
  18242. var selectedTds = getSelectedArr(this);
  18243. if (selectedTds.length) {
  18244. for (var i = 0, ci; ci = selectedTds[i++];) {
  18245. ci.setAttribute('vAlign', valign);
  18246. }
  18247. }
  18248. }
  18249. };
  18250. UE.commands['insertcaption'] = {
  18251. queryCommandState: function () {
  18252. var table = getTableItemsByRange(this).table;
  18253. if (table) {
  18254. return table.getElementsByTagName('caption').length == 0 ? 1 : -1;
  18255. }
  18256. return -1;
  18257. },
  18258. execCommand: function () {
  18259. var table = getTableItemsByRange(this).table;
  18260. if (table) {
  18261. var caption = this.document.createElement('caption');
  18262. caption.innerHTML = browser.ie ? domUtils.fillChar : '<br/>';
  18263. table.insertBefore(caption, table.firstChild);
  18264. var range = this.selection.getRange();
  18265. range.setStart(caption, 0).setCursor();
  18266. }
  18267. }
  18268. };
  18269. UE.commands['deletecaption'] = {
  18270. queryCommandState: function () {
  18271. var rng = this.selection.getRange(),
  18272. table = domUtils.findParentByTagName(rng.startContainer, 'table');
  18273. if (table) {
  18274. return table.getElementsByTagName('caption').length == 0 ? -1 : 1;
  18275. }
  18276. return -1;
  18277. },
  18278. execCommand: function () {
  18279. var rng = this.selection.getRange(),
  18280. table = domUtils.findParentByTagName(rng.startContainer, 'table');
  18281. if (table) {
  18282. domUtils.remove(table.getElementsByTagName('caption')[0]);
  18283. var range = this.selection.getRange();
  18284. range.setStart(table.rows[0].cells[0], 0).setCursor();
  18285. }
  18286. }
  18287. };
  18288. UE.commands['inserttitle'] = {
  18289. queryCommandState: function () {
  18290. var table = getTableItemsByRange(this).table;
  18291. if (table) {
  18292. var firstRow = table.rows[0];
  18293. return firstRow.cells[firstRow.cells.length - 1].tagName.toLowerCase() != 'th' ? 0 : -1
  18294. }
  18295. return -1;
  18296. },
  18297. execCommand: function () {
  18298. var table = getTableItemsByRange(this).table;
  18299. if (table) {
  18300. getUETable(table).insertRow(0, 'th');
  18301. }
  18302. var th = table.getElementsByTagName('th')[0];
  18303. this.selection.getRange().setStart(th, 0).setCursor(false, true);
  18304. }
  18305. };
  18306. UE.commands['deletetitle'] = {
  18307. queryCommandState: function () {
  18308. var table = getTableItemsByRange(this).table;
  18309. if (table) {
  18310. var firstRow = table.rows[0];
  18311. return firstRow.cells[firstRow.cells.length - 1].tagName.toLowerCase() == 'th' ? 0 : -1
  18312. }
  18313. return -1;
  18314. },
  18315. execCommand: function () {
  18316. var table = getTableItemsByRange(this).table;
  18317. if (table) {
  18318. domUtils.remove(table.rows[0])
  18319. }
  18320. var td = table.getElementsByTagName('td')[0];
  18321. this.selection.getRange().setStart(td, 0).setCursor(false, true);
  18322. }
  18323. };
  18324. UE.commands['inserttitlecol'] = {
  18325. queryCommandState: function () {
  18326. var table = getTableItemsByRange(this).table;
  18327. if (table) {
  18328. var lastRow = table.rows[table.rows.length - 1];
  18329. return lastRow.getElementsByTagName('th').length ? -1 : 0;
  18330. }
  18331. return -1;
  18332. },
  18333. execCommand: function (cmd) {
  18334. var table = getTableItemsByRange(this).table;
  18335. if (table) {
  18336. getUETable(table).insertCol(0, 'th');
  18337. }
  18338. resetTdWidth(table, this);
  18339. var th = table.getElementsByTagName('th')[0];
  18340. this.selection.getRange().setStart(th, 0).setCursor(false, true);
  18341. }
  18342. };
  18343. UE.commands['deletetitlecol'] = {
  18344. queryCommandState: function () {
  18345. var table = getTableItemsByRange(this).table;
  18346. if (table) {
  18347. var lastRow = table.rows[table.rows.length - 1];
  18348. return lastRow.getElementsByTagName('th').length ? 0 : -1;
  18349. }
  18350. return -1;
  18351. },
  18352. execCommand: function () {
  18353. var table = getTableItemsByRange(this).table;
  18354. if (table) {
  18355. for (var i = 0; i < table.rows.length; i++) {
  18356. domUtils.remove(table.rows[i].children[0])
  18357. }
  18358. }
  18359. resetTdWidth(table, this);
  18360. var td = table.getElementsByTagName('td')[0];
  18361. this.selection.getRange().setStart(td, 0).setCursor(false, true);
  18362. }
  18363. };
  18364. UE.commands["mergeright"] = {
  18365. queryCommandState: function (cmd) {
  18366. var tableItems = getTableItemsByRange(this),
  18367. table = tableItems.table,
  18368. cell = tableItems.cell;
  18369. if (!table || !cell) return -1;
  18370. var ut = getUETable(table);
  18371. if (ut.selectedTds.length) return -1;
  18372. var cellInfo = ut.getCellInfo(cell),
  18373. rightColIndex = cellInfo.colIndex + cellInfo.colSpan;
  18374. if (rightColIndex >= ut.colsNum) return -1; // 如果处于最右边则不能向右合并
  18375. var rightCellInfo = ut.indexTable[cellInfo.rowIndex][rightColIndex],
  18376. rightCell = table.rows[rightCellInfo.rowIndex].cells[rightCellInfo.cellIndex];
  18377. if (!rightCell || cell.tagName != rightCell.tagName) return -1; // TH和TD不能相互合并
  18378. // 当且仅当两个Cell的开始列号和结束列号一致时能进行合并
  18379. return (rightCellInfo.rowIndex == cellInfo.rowIndex && rightCellInfo.rowSpan == cellInfo.rowSpan) ? 0 : -1;
  18380. },
  18381. execCommand: function (cmd) {
  18382. var rng = this.selection.getRange(),
  18383. bk = rng.createBookmark(true);
  18384. var cell = getTableItemsByRange(this).cell,
  18385. ut = getUETable(cell);
  18386. ut.mergeRight(cell);
  18387. rng.moveToBookmark(bk).select();
  18388. }
  18389. };
  18390. UE.commands["mergedown"] = {
  18391. queryCommandState: function (cmd) {
  18392. var tableItems = getTableItemsByRange(this),
  18393. table = tableItems.table,
  18394. cell = tableItems.cell;
  18395. if (!table || !cell) return -1;
  18396. var ut = getUETable(table);
  18397. if (ut.selectedTds.length) return -1;
  18398. var cellInfo = ut.getCellInfo(cell),
  18399. downRowIndex = cellInfo.rowIndex + cellInfo.rowSpan;
  18400. if (downRowIndex >= ut.rowsNum) return -1; // 如果处于最下边则不能向下合并
  18401. var downCellInfo = ut.indexTable[downRowIndex][cellInfo.colIndex],
  18402. downCell = table.rows[downCellInfo.rowIndex].cells[downCellInfo.cellIndex];
  18403. if (!downCell || cell.tagName != downCell.tagName) return -1; // TH和TD不能相互合并
  18404. // 当且仅当两个Cell的开始列号和结束列号一致时能进行合并
  18405. return (downCellInfo.colIndex == cellInfo.colIndex && downCellInfo.colSpan == cellInfo.colSpan) ? 0 : -1;
  18406. },
  18407. execCommand: function () {
  18408. var rng = this.selection.getRange(),
  18409. bk = rng.createBookmark(true);
  18410. var cell = getTableItemsByRange(this).cell,
  18411. ut = getUETable(cell);
  18412. ut.mergeDown(cell);
  18413. rng.moveToBookmark(bk).select();
  18414. }
  18415. };
  18416. UE.commands["mergecells"] = {
  18417. queryCommandState: function () {
  18418. return getUETableBySelected(this) ? 0 : -1;
  18419. },
  18420. execCommand: function () {
  18421. var ut = getUETableBySelected(this);
  18422. if (ut && ut.selectedTds.length) {
  18423. var cell = ut.selectedTds[0];
  18424. ut.mergeRange();
  18425. var rng = this.selection.getRange();
  18426. if (domUtils.isEmptyBlock(cell)) {
  18427. rng.setStart(cell, 0).collapse(true)
  18428. } else {
  18429. rng.selectNodeContents(cell)
  18430. }
  18431. rng.select();
  18432. }
  18433. }
  18434. };
  18435. UE.commands["insertrow"] = {
  18436. queryCommandState: function () {
  18437. var tableItems = getTableItemsByRange(this),
  18438. cell = tableItems.cell;
  18439. return cell && (cell.tagName == "TD" || (cell.tagName == 'TH' && tableItems.tr !== tableItems.table.rows[0])) &&
  18440. getUETable(tableItems.table).rowsNum < this.options.maxRowNum ? 0 : -1;
  18441. },
  18442. execCommand: function () {
  18443. var rng = this.selection.getRange(),
  18444. bk = rng.createBookmark(true);
  18445. var tableItems = getTableItemsByRange(this),
  18446. cell = tableItems.cell,
  18447. table = tableItems.table,
  18448. ut = getUETable(table),
  18449. cellInfo = ut.getCellInfo(cell);
  18450. //ut.insertRow(!ut.selectedTds.length ? cellInfo.rowIndex:ut.cellsRange.beginRowIndex,'');
  18451. if (!ut.selectedTds.length) {
  18452. ut.insertRow(cellInfo.rowIndex, cell);
  18453. } else {
  18454. var range = ut.cellsRange;
  18455. for (var i = 0, len = range.endRowIndex - range.beginRowIndex + 1; i < len; i++) {
  18456. ut.insertRow(range.beginRowIndex, cell);
  18457. }
  18458. }
  18459. rng.moveToBookmark(bk).select();
  18460. if (table.getAttribute("interlaced") === "enabled") this.fireEvent("interlacetable", table);
  18461. }
  18462. };
  18463. //后插入行
  18464. UE.commands["insertrownext"] = {
  18465. queryCommandState: function () {
  18466. var tableItems = getTableItemsByRange(this),
  18467. cell = tableItems.cell;
  18468. return cell && (cell.tagName == "TD") && getUETable(tableItems.table).rowsNum < this.options.maxRowNum ? 0 : -1;
  18469. },
  18470. execCommand: function () {
  18471. var rng = this.selection.getRange(),
  18472. bk = rng.createBookmark(true);
  18473. var tableItems = getTableItemsByRange(this),
  18474. cell = tableItems.cell,
  18475. table = tableItems.table,
  18476. ut = getUETable(table),
  18477. cellInfo = ut.getCellInfo(cell);
  18478. //ut.insertRow(!ut.selectedTds.length? cellInfo.rowIndex + cellInfo.rowSpan : ut.cellsRange.endRowIndex + 1,'');
  18479. if (!ut.selectedTds.length) {
  18480. ut.insertRow(cellInfo.rowIndex + cellInfo.rowSpan, cell);
  18481. } else {
  18482. var range = ut.cellsRange;
  18483. for (var i = 0, len = range.endRowIndex - range.beginRowIndex + 1; i < len; i++) {
  18484. ut.insertRow(range.endRowIndex + 1, cell);
  18485. }
  18486. }
  18487. rng.moveToBookmark(bk).select();
  18488. if (table.getAttribute("interlaced") === "enabled") this.fireEvent("interlacetable", table);
  18489. }
  18490. };
  18491. UE.commands["deleterow"] = {
  18492. queryCommandState: function () {
  18493. var tableItems = getTableItemsByRange(this);
  18494. return tableItems.cell ? 0 : -1;
  18495. },
  18496. execCommand: function () {
  18497. var cell = getTableItemsByRange(this).cell,
  18498. ut = getUETable(cell),
  18499. cellsRange = ut.cellsRange,
  18500. cellInfo = ut.getCellInfo(cell),
  18501. preCell = ut.getVSideCell(cell),
  18502. nextCell = ut.getVSideCell(cell, true),
  18503. rng = this.selection.getRange();
  18504. if (utils.isEmptyObject(cellsRange)) {
  18505. ut.deleteRow(cellInfo.rowIndex);
  18506. } else {
  18507. for (var i = cellsRange.beginRowIndex; i < cellsRange.endRowIndex + 1; i++) {
  18508. ut.deleteRow(cellsRange.beginRowIndex);
  18509. }
  18510. }
  18511. var table = ut.table;
  18512. if (!table.getElementsByTagName('td').length) {
  18513. var nextSibling = table.nextSibling;
  18514. domUtils.remove(table);
  18515. if (nextSibling) {
  18516. rng.setStart(nextSibling, 0).setCursor(false, true);
  18517. }
  18518. } else {
  18519. if (cellInfo.rowSpan == 1 || cellInfo.rowSpan == cellsRange.endRowIndex - cellsRange.beginRowIndex + 1) {
  18520. if (nextCell || preCell) rng.selectNodeContents(nextCell || preCell).setCursor(false, true);
  18521. } else {
  18522. var newCell = ut.getCell(cellInfo.rowIndex, ut.indexTable[cellInfo.rowIndex][cellInfo.colIndex].cellIndex);
  18523. if (newCell) rng.selectNodeContents(newCell).setCursor(false, true);
  18524. }
  18525. }
  18526. if (table.getAttribute("interlaced") === "enabled") this.fireEvent("interlacetable", table);
  18527. }
  18528. };
  18529. UE.commands["insertcol"] = {
  18530. queryCommandState: function (cmd) {
  18531. var tableItems = getTableItemsByRange(this),
  18532. cell = tableItems.cell;
  18533. return cell && (cell.tagName == "TD" || (cell.tagName == 'TH' && cell !== tableItems.tr.cells[0])) &&
  18534. getUETable(tableItems.table).colsNum < this.options.maxColNum ? 0 : -1;
  18535. },
  18536. execCommand: function (cmd) {
  18537. var rng = this.selection.getRange(),
  18538. bk = rng.createBookmark(true);
  18539. if (this.queryCommandState(cmd) == -1) return;
  18540. var cell = getTableItemsByRange(this).cell,
  18541. ut = getUETable(cell),
  18542. cellInfo = ut.getCellInfo(cell);
  18543. //ut.insertCol(!ut.selectedTds.length ? cellInfo.colIndex:ut.cellsRange.beginColIndex);
  18544. if (!ut.selectedTds.length) {
  18545. ut.insertCol(cellInfo.colIndex, cell);
  18546. } else {
  18547. var range = ut.cellsRange;
  18548. for (var i = 0, len = range.endColIndex - range.beginColIndex + 1; i < len; i++) {
  18549. ut.insertCol(range.beginColIndex, cell);
  18550. }
  18551. }
  18552. rng.moveToBookmark(bk).select(true);
  18553. }
  18554. };
  18555. UE.commands["insertcolnext"] = {
  18556. queryCommandState: function () {
  18557. var tableItems = getTableItemsByRange(this),
  18558. cell = tableItems.cell;
  18559. return cell && getUETable(tableItems.table).colsNum < this.options.maxColNum ? 0 : -1;
  18560. },
  18561. execCommand: function () {
  18562. var rng = this.selection.getRange(),
  18563. bk = rng.createBookmark(true);
  18564. var cell = getTableItemsByRange(this).cell,
  18565. ut = getUETable(cell),
  18566. cellInfo = ut.getCellInfo(cell);
  18567. //ut.insertCol(!ut.selectedTds.length ? cellInfo.colIndex + cellInfo.colSpan:ut.cellsRange.endColIndex +1);
  18568. if (!ut.selectedTds.length) {
  18569. ut.insertCol(cellInfo.colIndex + cellInfo.colSpan, cell);
  18570. } else {
  18571. var range = ut.cellsRange;
  18572. for (var i = 0, len = range.endColIndex - range.beginColIndex + 1; i < len; i++) {
  18573. ut.insertCol(range.endColIndex + 1, cell);
  18574. }
  18575. }
  18576. rng.moveToBookmark(bk).select();
  18577. }
  18578. };
  18579. UE.commands["deletecol"] = {
  18580. queryCommandState: function () {
  18581. var tableItems = getTableItemsByRange(this);
  18582. return tableItems.cell ? 0 : -1;
  18583. },
  18584. execCommand: function () {
  18585. var cell = getTableItemsByRange(this).cell,
  18586. ut = getUETable(cell),
  18587. range = ut.cellsRange,
  18588. cellInfo = ut.getCellInfo(cell),
  18589. preCell = ut.getHSideCell(cell),
  18590. nextCell = ut.getHSideCell(cell, true);
  18591. if (utils.isEmptyObject(range)) {
  18592. ut.deleteCol(cellInfo.colIndex);
  18593. } else {
  18594. for (var i = range.beginColIndex; i < range.endColIndex + 1; i++) {
  18595. ut.deleteCol(range.beginColIndex);
  18596. }
  18597. }
  18598. var table = ut.table,
  18599. rng = this.selection.getRange();
  18600. if (!table.getElementsByTagName('td').length) {
  18601. var nextSibling = table.nextSibling;
  18602. domUtils.remove(table);
  18603. if (nextSibling) {
  18604. rng.setStart(nextSibling, 0).setCursor(false, true);
  18605. }
  18606. } else {
  18607. if (domUtils.inDoc(cell, this.document)) {
  18608. rng.setStart(cell, 0).setCursor(false, true);
  18609. } else {
  18610. if (nextCell && domUtils.inDoc(nextCell, this.document)) {
  18611. rng.selectNodeContents(nextCell).setCursor(false, true);
  18612. } else {
  18613. if (preCell && domUtils.inDoc(preCell, this.document)) {
  18614. rng.selectNodeContents(preCell).setCursor(true, true);
  18615. }
  18616. }
  18617. }
  18618. }
  18619. }
  18620. };
  18621. UE.commands["splittocells"] = {
  18622. queryCommandState: function () {
  18623. var tableItems = getTableItemsByRange(this),
  18624. cell = tableItems.cell;
  18625. if (!cell) return -1;
  18626. var ut = getUETable(tableItems.table);
  18627. if (ut.selectedTds.length > 0) return -1;
  18628. return cell && (cell.colSpan > 1 || cell.rowSpan > 1) ? 0 : -1;
  18629. },
  18630. execCommand: function () {
  18631. var rng = this.selection.getRange(),
  18632. bk = rng.createBookmark(true);
  18633. var cell = getTableItemsByRange(this).cell,
  18634. ut = getUETable(cell);
  18635. ut.splitToCells(cell);
  18636. rng.moveToBookmark(bk).select();
  18637. }
  18638. };
  18639. UE.commands["splittorows"] = {
  18640. queryCommandState: function () {
  18641. var tableItems = getTableItemsByRange(this),
  18642. cell = tableItems.cell;
  18643. if (!cell) return -1;
  18644. var ut = getUETable(tableItems.table);
  18645. if (ut.selectedTds.length > 0) return -1;
  18646. return cell && cell.rowSpan > 1 ? 0 : -1;
  18647. },
  18648. execCommand: function () {
  18649. var rng = this.selection.getRange(),
  18650. bk = rng.createBookmark(true);
  18651. var cell = getTableItemsByRange(this).cell,
  18652. ut = getUETable(cell);
  18653. ut.splitToRows(cell);
  18654. rng.moveToBookmark(bk).select();
  18655. }
  18656. };
  18657. UE.commands["splittocols"] = {
  18658. queryCommandState: function () {
  18659. var tableItems = getTableItemsByRange(this),
  18660. cell = tableItems.cell;
  18661. if (!cell) return -1;
  18662. var ut = getUETable(tableItems.table);
  18663. if (ut.selectedTds.length > 0) return -1;
  18664. return cell && cell.colSpan > 1 ? 0 : -1;
  18665. },
  18666. execCommand: function () {
  18667. var rng = this.selection.getRange(),
  18668. bk = rng.createBookmark(true);
  18669. var cell = getTableItemsByRange(this).cell,
  18670. ut = getUETable(cell);
  18671. ut.splitToCols(cell);
  18672. rng.moveToBookmark(bk).select();
  18673. }
  18674. };
  18675. UE.commands["adaptbytext"] =
  18676. UE.commands["adaptbywindow"] = {
  18677. queryCommandState: function () {
  18678. return getTableItemsByRange(this).table ? 0 : -1
  18679. },
  18680. execCommand: function (cmd) {
  18681. var tableItems = getTableItemsByRange(this),
  18682. table = tableItems.table;
  18683. if (table) {
  18684. if (cmd == 'adaptbywindow') {
  18685. resetTdWidth(table, this);
  18686. } else {
  18687. var cells = domUtils.getElementsByTagName(table, "td th");
  18688. utils.each(cells, function (cell) {
  18689. cell.removeAttribute("width");
  18690. });
  18691. table.removeAttribute("width");
  18692. }
  18693. }
  18694. }
  18695. };
  18696. //平均分配各列
  18697. UE.commands['averagedistributecol'] = {
  18698. queryCommandState: function () {
  18699. var ut = getUETableBySelected(this);
  18700. if (!ut) return -1;
  18701. return ut.isFullRow() || ut.isFullCol() ? 0 : -1;
  18702. },
  18703. execCommand: function (cmd) {
  18704. var me = this,
  18705. ut = getUETableBySelected(me);
  18706. function getAverageWidth() {
  18707. var tb = ut.table,
  18708. averageWidth, sumWidth = 0,
  18709. colsNum = 0,
  18710. tbAttr = getDefaultValue(me, tb);
  18711. if (ut.isFullRow()) {
  18712. sumWidth = tb.offsetWidth;
  18713. colsNum = ut.colsNum;
  18714. } else {
  18715. var begin = ut.cellsRange.beginColIndex,
  18716. end = ut.cellsRange.endColIndex,
  18717. node;
  18718. for (var i = begin; i <= end;) {
  18719. node = ut.selectedTds[i];
  18720. sumWidth += node.offsetWidth;
  18721. i += node.colSpan;
  18722. colsNum += 1;
  18723. }
  18724. }
  18725. averageWidth = Math.ceil(sumWidth / colsNum) - tbAttr.tdBorder * 2 - tbAttr.tdPadding * 2;
  18726. return averageWidth;
  18727. }
  18728. function setAverageWidth(averageWidth) {
  18729. utils.each(domUtils.getElementsByTagName(ut.table, "th"), function (node) {
  18730. node.setAttribute("width", "");
  18731. });
  18732. var cells = ut.isFullRow() ? domUtils.getElementsByTagName(ut.table, "td") : ut.selectedTds;
  18733. utils.each(cells, function (node) {
  18734. if (node.colSpan == 1) {
  18735. node.setAttribute("width", averageWidth);
  18736. }
  18737. });
  18738. }
  18739. if (ut && ut.selectedTds.length) {
  18740. setAverageWidth(getAverageWidth());
  18741. }
  18742. }
  18743. };
  18744. //平均分配各行
  18745. UE.commands['averagedistributerow'] = {
  18746. queryCommandState: function () {
  18747. var ut = getUETableBySelected(this);
  18748. if (!ut) return -1;
  18749. if (ut.selectedTds && /th/ig.test(ut.selectedTds[0].tagName)) return -1;
  18750. return ut.isFullRow() || ut.isFullCol() ? 0 : -1;
  18751. },
  18752. execCommand: function (cmd) {
  18753. var me = this,
  18754. ut = getUETableBySelected(me);
  18755. function getAverageHeight() {
  18756. var averageHeight, rowNum, sumHeight = 0,
  18757. tb = ut.table,
  18758. tbAttr = getDefaultValue(me, tb),
  18759. tdpadding = parseInt(domUtils.getComputedStyle(tb.getElementsByTagName('td')[0], "padding-top"));
  18760. if (ut.isFullCol()) {
  18761. var captionArr = domUtils.getElementsByTagName(tb, "caption"),
  18762. thArr = domUtils.getElementsByTagName(tb, "th"),
  18763. captionHeight, thHeight;
  18764. if (captionArr.length > 0) {
  18765. captionHeight = captionArr[0].offsetHeight;
  18766. }
  18767. if (thArr.length > 0) {
  18768. thHeight = thArr[0].offsetHeight;
  18769. }
  18770. sumHeight = tb.offsetHeight - (captionHeight || 0) - (thHeight || 0);
  18771. rowNum = thArr.length == 0 ? ut.rowsNum : (ut.rowsNum - 1);
  18772. } else {
  18773. var begin = ut.cellsRange.beginRowIndex,
  18774. end = ut.cellsRange.endRowIndex,
  18775. count = 0,
  18776. trs = domUtils.getElementsByTagName(tb, "tr");
  18777. for (var i = begin; i <= end; i++) {
  18778. sumHeight += trs[i].offsetHeight;
  18779. count += 1;
  18780. }
  18781. rowNum = count;
  18782. }
  18783. //ie8下是混杂模式
  18784. if (browser.ie && browser.version < 9) {
  18785. averageHeight = Math.ceil(sumHeight / rowNum);
  18786. } else {
  18787. averageHeight = Math.ceil(sumHeight / rowNum) - tbAttr.tdBorder * 2 - tdpadding * 2;
  18788. }
  18789. return averageHeight;
  18790. }
  18791. function setAverageHeight(averageHeight) {
  18792. var cells = ut.isFullCol() ? domUtils.getElementsByTagName(ut.table, "td") : ut.selectedTds;
  18793. utils.each(cells, function (node) {
  18794. if (node.rowSpan == 1) {
  18795. node.setAttribute("height", averageHeight);
  18796. }
  18797. });
  18798. }
  18799. if (ut && ut.selectedTds.length) {
  18800. setAverageHeight(getAverageHeight());
  18801. }
  18802. }
  18803. };
  18804. //单元格对齐方式
  18805. UE.commands['cellalignment'] = {
  18806. queryCommandState: function () {
  18807. return getTableItemsByRange(this).table ? 0 : -1
  18808. },
  18809. execCommand: function (cmd, data) {
  18810. var me = this,
  18811. ut = getUETableBySelected(me);
  18812. if (!ut) {
  18813. var start = me.selection.getStart(),
  18814. cell = start && domUtils.findParentByTagName(start, ["td", "th", "caption"], true);
  18815. if (!/caption/ig.test(cell.tagName)) {
  18816. domUtils.setAttributes(cell, data);
  18817. } else {
  18818. cell.style.textAlign = data.align;
  18819. cell.style.verticalAlign = data.vAlign;
  18820. }
  18821. me.selection.getRange().setCursor(true);
  18822. } else {
  18823. utils.each(ut.selectedTds, function (cell) {
  18824. domUtils.setAttributes(cell, data);
  18825. });
  18826. }
  18827. },
  18828. /**
  18829. * 查询当前点击的单元格的对齐状态, 如果当前已经选择了多个单元格, 则会返回所有单元格经过统一协调过后的状态
  18830. * @see UE.UETable.getTableCellAlignState
  18831. */
  18832. queryCommandValue: function (cmd) {
  18833. var activeMenuCell = getTableItemsByRange(this).cell;
  18834. if (!activeMenuCell) {
  18835. activeMenuCell = getSelectedArr(this)[0];
  18836. }
  18837. if (!activeMenuCell) {
  18838. return null;
  18839. } else {
  18840. //获取同时选中的其他单元格
  18841. var cells = UE.UETable.getUETable(activeMenuCell).selectedTds;
  18842. !cells.length && (cells = activeMenuCell);
  18843. return UE.UETable.getTableCellAlignState(cells);
  18844. }
  18845. }
  18846. };
  18847. //表格对齐方式
  18848. UE.commands['tablealignment'] = {
  18849. queryCommandState: function () {
  18850. if (browser.ie && browser.version < 8) {
  18851. return -1;
  18852. }
  18853. return getTableItemsByRange(this).table ? 0 : -1
  18854. },
  18855. execCommand: function (cmd, value) {
  18856. var me = this,
  18857. start = me.selection.getStart(),
  18858. table = start && domUtils.findParentByTagName(start, ["table"], true);
  18859. if (table) {
  18860. table.setAttribute("align", value);
  18861. }
  18862. }
  18863. };
  18864. //表格属性
  18865. UE.commands['edittable'] = {
  18866. queryCommandState: function () {
  18867. return getTableItemsByRange(this).table ? 0 : -1
  18868. },
  18869. execCommand: function (cmd, color) {
  18870. var rng = this.selection.getRange(),
  18871. table = domUtils.findParentByTagName(rng.startContainer, 'table');
  18872. if (table) {
  18873. var arr = domUtils.getElementsByTagName(table, "td").concat(
  18874. domUtils.getElementsByTagName(table, "th"),
  18875. domUtils.getElementsByTagName(table, "caption")
  18876. );
  18877. utils.each(arr, function (node) {
  18878. node.style.borderColor = color;
  18879. });
  18880. }
  18881. }
  18882. };
  18883. //单元格属性
  18884. UE.commands['edittd'] = {
  18885. queryCommandState: function () {
  18886. return getTableItemsByRange(this).table ? 0 : -1
  18887. },
  18888. execCommand: function (cmd, bkColor) {
  18889. var me = this,
  18890. ut = getUETableBySelected(me);
  18891. if (!ut) {
  18892. var start = me.selection.getStart(),
  18893. cell = start && domUtils.findParentByTagName(start, ["td", "th", "caption"], true);
  18894. if (cell) {
  18895. cell.style.backgroundColor = bkColor;
  18896. }
  18897. } else {
  18898. utils.each(ut.selectedTds, function (cell) {
  18899. cell.style.backgroundColor = bkColor;
  18900. });
  18901. }
  18902. }
  18903. };
  18904. UE.commands["settablebackground"] = {
  18905. queryCommandState: function () {
  18906. return getSelectedArr(this).length > 1 ? 0 : -1;
  18907. },
  18908. execCommand: function (cmd, value) {
  18909. var cells, ut;
  18910. cells = getSelectedArr(this);
  18911. ut = getUETable(cells[0]);
  18912. ut.setBackground(cells, value);
  18913. }
  18914. };
  18915. UE.commands["cleartablebackground"] = {
  18916. queryCommandState: function () {
  18917. var cells = getSelectedArr(this);
  18918. if (!cells.length) return -1;
  18919. for (var i = 0, cell; cell = cells[i++];) {
  18920. if (cell.style.backgroundColor !== "") return 0;
  18921. }
  18922. return -1;
  18923. },
  18924. execCommand: function () {
  18925. var cells = getSelectedArr(this),
  18926. ut = getUETable(cells[0]);
  18927. ut.removeBackground(cells);
  18928. }
  18929. };
  18930. UE.commands["interlacetable"] = UE.commands["uninterlacetable"] = {
  18931. queryCommandState: function (cmd) {
  18932. var table = getTableItemsByRange(this).table;
  18933. if (!table) return -1;
  18934. var interlaced = table.getAttribute("interlaced");
  18935. if (cmd == "interlacetable") {
  18936. //TODO 待定
  18937. //是否需要待定,如果设置,则命令只能单次执行成功,但反射具备toggle效果;否则可以覆盖前次命令,但反射将不存在toggle效果
  18938. return (interlaced === "enabled") ? -1 : 0;
  18939. } else {
  18940. return (!interlaced || interlaced === "disabled") ? -1 : 0;
  18941. }
  18942. },
  18943. execCommand: function (cmd, classList) {
  18944. var table = getTableItemsByRange(this).table;
  18945. if (cmd == "interlacetable") {
  18946. table.setAttribute("interlaced", "enabled");
  18947. this.fireEvent("interlacetable", table, classList);
  18948. } else {
  18949. table.setAttribute("interlaced", "disabled");
  18950. this.fireEvent("uninterlacetable", table);
  18951. }
  18952. }
  18953. };
  18954. UE.commands["setbordervisible"] = {
  18955. queryCommandState: function (cmd) {
  18956. var table = getTableItemsByRange(this).table;
  18957. if (!table) return -1;
  18958. return 0;
  18959. },
  18960. execCommand: function () {
  18961. var table = getTableItemsByRange(this).table;
  18962. utils.each(domUtils.getElementsByTagName(table, 'td'), function (td) {
  18963. td.style.borderWidth = '1px';
  18964. td.style.borderStyle = 'solid';
  18965. })
  18966. }
  18967. };
  18968. function resetTdWidth(table, editor) {
  18969. var tds = domUtils.getElementsByTagName(table, 'td th');
  18970. utils.each(tds, function (td) {
  18971. td.removeAttribute("width");
  18972. });
  18973. table.setAttribute('width', getTableWidth(editor, true, getDefaultValue(editor, table)));
  18974. var tdsWidths = [];
  18975. setTimeout(function () {
  18976. utils.each(tds, function (td) {
  18977. (td.colSpan == 1) && tdsWidths.push(td.offsetWidth)
  18978. })
  18979. utils.each(tds, function (td, i) {
  18980. (td.colSpan == 1) && td.setAttribute("width", tdsWidths[i] + "");
  18981. })
  18982. }, 0);
  18983. }
  18984. function getTableWidth(editor, needIEHack, defaultValue) {
  18985. var body = editor.body;
  18986. return body.offsetWidth - (needIEHack ? parseInt(domUtils.getComputedStyle(body, 'margin-left'), 10) * 2 : 0) - defaultValue.tableBorder * 2 - (editor.options.offsetWidth || 0);
  18987. }
  18988. function getSelectedArr(editor) {
  18989. var cell = getTableItemsByRange(editor).cell;
  18990. if (cell) {
  18991. var ut = getUETable(cell);
  18992. return ut.selectedTds.length ? ut.selectedTds : [cell];
  18993. } else {
  18994. return [];
  18995. }
  18996. }
  18997. })();
  18998. // plugins/table.action.js
  18999. /**
  19000. * Created with JetBrains PhpStorm.
  19001. * User: taoqili
  19002. * Date: 12-10-12
  19003. * Time: 上午10:05
  19004. * To change this template use File | Settings | File Templates.
  19005. */
  19006. UE.plugins['table'] = function () {
  19007. var me = this,
  19008. tabTimer = null,
  19009. //拖动计时器
  19010. tableDragTimer = null,
  19011. //双击计时器
  19012. tableResizeTimer = null,
  19013. //单元格最小宽度
  19014. cellMinWidth = 5,
  19015. isInResizeBuffer = false,
  19016. //单元格边框大小
  19017. cellBorderWidth = 5,
  19018. //鼠标偏移距离
  19019. offsetOfTableCell = 10,
  19020. //记录在有限时间内的点击状态, 共有3个取值, 0, 1, 2。 0代表未初始化, 1代表单击了1次,2代表2次
  19021. singleClickState = 0,
  19022. userActionStatus = null,
  19023. //双击允许的时间范围
  19024. dblclickTime = 360,
  19025. UT = UE.UETable,
  19026. getUETable = function (tdOrTable) {
  19027. return UT.getUETable(tdOrTable);
  19028. },
  19029. getUETableBySelected = function (editor) {
  19030. return UT.getUETableBySelected(editor);
  19031. },
  19032. getDefaultValue = function (editor, table) {
  19033. return UT.getDefaultValue(editor, table);
  19034. },
  19035. removeSelectedClass = function (cells) {
  19036. return UT.removeSelectedClass(cells);
  19037. };
  19038. function showError(e) {
  19039. // throw e;
  19040. }
  19041. me.ready(function () {
  19042. var me = this;
  19043. var orgGetText = me.selection.getText;
  19044. me.selection.getText = function () {
  19045. var table = getUETableBySelected(me);
  19046. if (table) {
  19047. var str = '';
  19048. utils.each(table.selectedTds, function (td) {
  19049. str += td[browser.ie ? 'innerText' : 'textContent'];
  19050. })
  19051. return str;
  19052. } else {
  19053. return orgGetText.call(me.selection)
  19054. }
  19055. }
  19056. })
  19057. //处理拖动及框选相关方法
  19058. var startTd = null, //鼠标按下时的锚点td
  19059. currentTd = null, //当前鼠标经过时的td
  19060. onDrag = "", //指示当前拖动状态,其值可为"","h","v" ,分别表示未拖动状态,横向拖动状态,纵向拖动状态,用于鼠标移动过程中的判断
  19061. onBorder = false, //检测鼠标按下时是否处在单元格边缘位置
  19062. dragButton = null,
  19063. dragOver = false,
  19064. dragLine = null, //模拟的拖动线
  19065. dragTd = null; //发生拖动的目标td
  19066. var mousedown = false,
  19067. //todo 判断混乱模式
  19068. needIEHack = true;
  19069. me.setOpt({
  19070. 'maxColNum': 20,
  19071. 'maxRowNum': 100,
  19072. 'defaultCols': 5,
  19073. 'defaultRows': 5,
  19074. 'tdvalign': 'top',
  19075. 'cursorpath': me.options.UEDITOR_HOME_URL + "themes/default/images/cursor_",
  19076. 'tableDragable': false,
  19077. 'classList': ["ue-table-interlace-color-single", "ue-table-interlace-color-double"]
  19078. });
  19079. me.getUETable = getUETable;
  19080. var commands = {
  19081. 'deletetable': 1,
  19082. 'inserttable': 1,
  19083. 'cellvalign': 1,
  19084. 'insertcaption': 1,
  19085. 'deletecaption': 1,
  19086. 'inserttitle': 1,
  19087. 'deletetitle': 1,
  19088. "mergeright": 1,
  19089. "mergedown": 1,
  19090. "mergecells": 1,
  19091. "insertrow": 1,
  19092. "insertrownext": 1,
  19093. "deleterow": 1,
  19094. "insertcol": 1,
  19095. "insertcolnext": 1,
  19096. "deletecol": 1,
  19097. "splittocells": 1,
  19098. "splittorows": 1,
  19099. "splittocols": 1,
  19100. "adaptbytext": 1,
  19101. "adaptbywindow": 1,
  19102. "adaptbycustomer": 1,
  19103. "insertparagraph": 1,
  19104. "insertparagraphbeforetable": 1,
  19105. "averagedistributecol": 1,
  19106. "averagedistributerow": 1
  19107. };
  19108. me.ready(function () {
  19109. utils.cssRule('table',
  19110. //选中的td上的样式
  19111. '.selectTdClass{background-color:#edf5fa !important}' +
  19112. 'table.noBorderTable td,table.noBorderTable th,table.noBorderTable caption{border:1px dashed #ddd !important}' +
  19113. //插入的表格的默认样式
  19114. 'table{margin-bottom:10px;border-collapse:collapse;display:table;}' +
  19115. 'td,th{padding: 5px 10px;border: 1px solid #DDD;}' +
  19116. 'caption{border:1px dashed #DDD;border-bottom:0;padding:3px;text-align:center;}' +
  19117. 'th{border-top:1px solid #BBB;background-color:#F7F7F7;}' +
  19118. 'table tr.firstRow th{border-top-width:2px;}' +
  19119. '.ue-table-interlace-color-single{ background-color: #fcfcfc; } .ue-table-interlace-color-double{ background-color: #f7faff; }' +
  19120. 'td p{margin:0;padding:0;}', me.document);
  19121. var tableCopyList, isFullCol, isFullRow;
  19122. //注册del/backspace事件
  19123. me.addListener('keydown', function (cmd, evt) {
  19124. var me = this;
  19125. var keyCode = evt.keyCode || evt.which;
  19126. if (keyCode == 8) {
  19127. var ut = getUETableBySelected(me);
  19128. if (ut && ut.selectedTds.length) {
  19129. if (ut.isFullCol()) {
  19130. me.execCommand('deletecol')
  19131. } else if (ut.isFullRow()) {
  19132. me.execCommand('deleterow')
  19133. } else {
  19134. me.fireEvent('delcells');
  19135. }
  19136. domUtils.preventDefault(evt);
  19137. }
  19138. var caption = domUtils.findParentByTagName(me.selection.getStart(), 'caption', true),
  19139. range = me.selection.getRange();
  19140. if (range.collapsed && caption && isEmptyBlock(caption)) {
  19141. me.fireEvent('saveScene');
  19142. var table = caption.parentNode;
  19143. domUtils.remove(caption);
  19144. if (table) {
  19145. range.setStart(table.rows[0].cells[0], 0).setCursor(false, true);
  19146. }
  19147. me.fireEvent('saveScene');
  19148. }
  19149. }
  19150. if (keyCode == 46) {
  19151. ut = getUETableBySelected(me);
  19152. if (ut) {
  19153. me.fireEvent('saveScene');
  19154. for (var i = 0, ci; ci = ut.selectedTds[i++];) {
  19155. domUtils.fillNode(me.document, ci)
  19156. }
  19157. me.fireEvent('saveScene');
  19158. domUtils.preventDefault(evt);
  19159. }
  19160. }
  19161. if (keyCode == 13) {
  19162. var rng = me.selection.getRange(),
  19163. caption = domUtils.findParentByTagName(rng.startContainer, 'caption', true);
  19164. if (caption) {
  19165. var table = domUtils.findParentByTagName(caption, 'table');
  19166. if (!rng.collapsed) {
  19167. rng.deleteContents();
  19168. me.fireEvent('saveScene');
  19169. } else {
  19170. if (caption) {
  19171. rng.setStart(table.rows[0].cells[0], 0).setCursor(false, true);
  19172. }
  19173. }
  19174. domUtils.preventDefault(evt);
  19175. return;
  19176. }
  19177. if (rng.collapsed) {
  19178. var table = domUtils.findParentByTagName(rng.startContainer, 'table');
  19179. if (table) {
  19180. var cell = table.rows[0].cells[0],
  19181. start = domUtils.findParentByTagName(me.selection.getStart(), ['td', 'th'], true),
  19182. preNode = table.previousSibling;
  19183. if (cell === start && (!preNode || preNode.nodeType == 1 && preNode.tagName == 'TABLE') && domUtils.isStartInblock(rng)) {
  19184. var first = domUtils.findParent(me.selection.getStart(), function (n) {
  19185. return domUtils.isBlockElm(n)
  19186. }, true);
  19187. if (first && (/t(h|d)/i.test(first.tagName) || first === start.firstChild)) {
  19188. me.execCommand('insertparagraphbeforetable');
  19189. domUtils.preventDefault(evt);
  19190. }
  19191. }
  19192. }
  19193. }
  19194. }
  19195. if ((evt.ctrlKey || evt.metaKey) && evt.keyCode == '67') {
  19196. tableCopyList = null;
  19197. var ut = getUETableBySelected(me);
  19198. if (ut) {
  19199. var tds = ut.selectedTds;
  19200. isFullCol = ut.isFullCol();
  19201. isFullRow = ut.isFullRow();
  19202. tableCopyList = [
  19203. [ut.cloneCell(tds[0], null, true)]
  19204. ];
  19205. for (var i = 1, ci; ci = tds[i]; i++) {
  19206. if (ci.parentNode !== tds[i - 1].parentNode) {
  19207. tableCopyList.push([ut.cloneCell(ci, null, true)]);
  19208. } else {
  19209. tableCopyList[tableCopyList.length - 1].push(ut.cloneCell(ci, null, true));
  19210. }
  19211. }
  19212. }
  19213. }
  19214. });
  19215. me.addListener("tablehasdeleted", function () {
  19216. toggleDraggableState(this, false, "", null);
  19217. if (dragButton) domUtils.remove(dragButton);
  19218. });
  19219. me.addListener('beforepaste', function (cmd, html) {
  19220. var me = this;
  19221. var rng = me.selection.getRange();
  19222. if (domUtils.findParentByTagName(rng.startContainer, 'caption', true)) {
  19223. var div = me.document.createElement("div");
  19224. div.innerHTML = html.html;
  19225. //trace:3729
  19226. html.html = div[browser.ie9below ? 'innerText' : 'textContent'];
  19227. return;
  19228. }
  19229. var table = getUETableBySelected(me);
  19230. if (tableCopyList) {
  19231. me.fireEvent('saveScene');
  19232. var rng = me.selection.getRange();
  19233. var td = domUtils.findParentByTagName(rng.startContainer, ['td', 'th'], true),
  19234. tmpNode, preNode;
  19235. if (td) {
  19236. var ut = getUETable(td);
  19237. if (isFullRow) {
  19238. var rowIndex = ut.getCellInfo(td).rowIndex;
  19239. if (td.tagName == 'TH') {
  19240. rowIndex++;
  19241. }
  19242. for (var i = 0, ci; ci = tableCopyList[i++];) {
  19243. var tr = ut.insertRow(rowIndex++, "td");
  19244. for (var j = 0, cj; cj = ci[j]; j++) {
  19245. var cell = tr.cells[j];
  19246. if (!cell) {
  19247. cell = tr.insertCell(j)
  19248. }
  19249. cell.innerHTML = cj.innerHTML;
  19250. cj.getAttribute('width') && cell.setAttribute('width', cj.getAttribute('width'));
  19251. cj.getAttribute('vAlign') && cell.setAttribute('vAlign', cj.getAttribute('vAlign'));
  19252. cj.getAttribute('align') && cell.setAttribute('align', cj.getAttribute('align'));
  19253. cj.style.cssText && (cell.style.cssText = cj.style.cssText)
  19254. }
  19255. for (var j = 0, cj; cj = tr.cells[j]; j++) {
  19256. if (!ci[j])
  19257. break;
  19258. cj.innerHTML = ci[j].innerHTML;
  19259. ci[j].getAttribute('width') && cj.setAttribute('width', ci[j].getAttribute('width'));
  19260. ci[j].getAttribute('vAlign') && cj.setAttribute('vAlign', ci[j].getAttribute('vAlign'));
  19261. ci[j].getAttribute('align') && cj.setAttribute('align', ci[j].getAttribute('align'));
  19262. ci[j].style.cssText && (cj.style.cssText = ci[j].style.cssText)
  19263. }
  19264. }
  19265. } else {
  19266. if (isFullCol) {
  19267. cellInfo = ut.getCellInfo(td);
  19268. var maxColNum = 0;
  19269. for (var j = 0, ci = tableCopyList[0], cj; cj = ci[j++];) {
  19270. maxColNum += cj.colSpan || 1;
  19271. }
  19272. me.__hasEnterExecCommand = true;
  19273. for (i = 0; i < maxColNum; i++) {
  19274. me.execCommand('insertcol');
  19275. }
  19276. me.__hasEnterExecCommand = false;
  19277. td = ut.table.rows[0].cells[cellInfo.cellIndex];
  19278. if (td.tagName == 'TH') {
  19279. td = ut.table.rows[1].cells[cellInfo.cellIndex];
  19280. }
  19281. }
  19282. for (var i = 0, ci; ci = tableCopyList[i++];) {
  19283. tmpNode = td;
  19284. for (var j = 0, cj; cj = ci[j++];) {
  19285. if (td) {
  19286. td.innerHTML = cj.innerHTML;
  19287. //todo 定制处理
  19288. cj.getAttribute('width') && td.setAttribute('width', cj.getAttribute('width'));
  19289. cj.getAttribute('vAlign') && td.setAttribute('vAlign', cj.getAttribute('vAlign'));
  19290. cj.getAttribute('align') && td.setAttribute('align', cj.getAttribute('align'));
  19291. cj.style.cssText && (td.style.cssText = cj.style.cssText);
  19292. preNode = td;
  19293. td = td.nextSibling;
  19294. } else {
  19295. var cloneTd = cj.cloneNode(true);
  19296. domUtils.removeAttributes(cloneTd, ['class', 'rowSpan', 'colSpan']);
  19297. preNode.parentNode.appendChild(cloneTd)
  19298. }
  19299. }
  19300. td = ut.getNextCell(tmpNode, true, true);
  19301. if (!tableCopyList[i])
  19302. break;
  19303. if (!td) {
  19304. var cellInfo = ut.getCellInfo(tmpNode);
  19305. ut.table.insertRow(ut.table.rows.length);
  19306. ut.update();
  19307. td = ut.getVSideCell(tmpNode, true);
  19308. }
  19309. }
  19310. }
  19311. ut.update();
  19312. } else {
  19313. table = me.document.createElement('table');
  19314. for (var i = 0, ci; ci = tableCopyList[i++];) {
  19315. var tr = table.insertRow(table.rows.length);
  19316. for (var j = 0, cj; cj = ci[j++];) {
  19317. cloneTd = UT.cloneCell(cj, null, true);
  19318. domUtils.removeAttributes(cloneTd, ['class']);
  19319. tr.appendChild(cloneTd)
  19320. }
  19321. if (j == 2 && cloneTd.rowSpan > 1) {
  19322. cloneTd.rowSpan = 1;
  19323. }
  19324. }
  19325. var defaultValue = getDefaultValue(me),
  19326. width = me.body.offsetWidth -
  19327. (needIEHack ? parseInt(domUtils.getComputedStyle(me.body, 'margin-left'), 10) * 2 : 0) - defaultValue.tableBorder * 2 - (me.options.offsetWidth || 0);
  19328. me.execCommand('insertHTML', '<table ' +
  19329. (isFullCol && isFullRow ? 'width="' + width + '"' : '') +
  19330. '>' + table.innerHTML.replace(/>\s*</g, '><').replace(/\bth\b/gi, "td") + '</table>')
  19331. }
  19332. me.fireEvent('contentchange');
  19333. me.fireEvent('saveScene');
  19334. html.html = '';
  19335. return true;
  19336. } else {
  19337. var div = me.document.createElement("div"),
  19338. tables;
  19339. div.innerHTML = html.html;
  19340. tables = div.getElementsByTagName("table");
  19341. if (domUtils.findParentByTagName(me.selection.getStart(), 'table')) {
  19342. utils.each(tables, function (t) {
  19343. domUtils.remove(t)
  19344. });
  19345. if (domUtils.findParentByTagName(me.selection.getStart(), 'caption', true)) {
  19346. div.innerHTML = div[browser.ie ? 'innerText' : 'textContent'];
  19347. }
  19348. } else {
  19349. utils.each(tables, function (table) {
  19350. removeStyleSize(table, true);
  19351. domUtils.removeAttributes(table, ['style', 'border']);
  19352. utils.each(domUtils.getElementsByTagName(table, "td"), function (td) {
  19353. if (isEmptyBlock(td)) {
  19354. domUtils.fillNode(me.document, td);
  19355. }
  19356. removeStyleSize(td, true);
  19357. // domUtils.removeAttributes(td, ['style'])
  19358. });
  19359. });
  19360. }
  19361. html.html = div.innerHTML;
  19362. }
  19363. });
  19364. me.addListener('afterpaste', function () {
  19365. utils.each(domUtils.getElementsByTagName(me.body, "table"), function (table) {
  19366. if (table.offsetWidth > me.body.offsetWidth) {
  19367. var defaultValue = getDefaultValue(me, table);
  19368. 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'
  19369. }
  19370. })
  19371. });
  19372. me.addListener('blur', function () {
  19373. tableCopyList = null;
  19374. });
  19375. var timer;
  19376. me.addListener('keydown', function () {
  19377. clearTimeout(timer);
  19378. timer = setTimeout(function () {
  19379. var rng = me.selection.getRange(),
  19380. cell = domUtils.findParentByTagName(rng.startContainer, ['th', 'td'], true);
  19381. if (cell) {
  19382. var table = cell.parentNode.parentNode.parentNode;
  19383. if (table.offsetWidth > table.getAttribute("width")) {
  19384. cell.style.wordBreak = "break-all";
  19385. }
  19386. }
  19387. }, 100);
  19388. });
  19389. me.addListener("selectionchange", function () {
  19390. toggleDraggableState(me, false, "", null);
  19391. });
  19392. //内容变化时触发索引更新
  19393. //todo 可否考虑标记检测,如果不涉及表格的变化就不进行索引重建和更新
  19394. me.addListener("contentchange", function () {
  19395. var me = this;
  19396. //尽可能排除一些不需要更新的状况
  19397. hideDragLine(me);
  19398. if (getUETableBySelected(me)) return;
  19399. var rng = me.selection.getRange();
  19400. var start = rng.startContainer;
  19401. start = domUtils.findParentByTagName(start, ['td', 'th'], true);
  19402. utils.each(domUtils.getElementsByTagName(me.document, 'table'), function (table) {
  19403. if (me.fireEvent("excludetable", table) === true) return;
  19404. table.ueTable = new UT(table);
  19405. //trace:3742
  19406. // utils.each(domUtils.getElementsByTagName(me.document, 'td'), function (td) {
  19407. //
  19408. // if (domUtils.isEmptyBlock(td) && td !== start) {
  19409. // domUtils.fillNode(me.document, td);
  19410. // if (browser.ie && browser.version == 6) {
  19411. // td.innerHTML = '&nbsp;'
  19412. // }
  19413. // }
  19414. // });
  19415. // utils.each(domUtils.getElementsByTagName(me.document, 'th'), function (th) {
  19416. // if (domUtils.isEmptyBlock(th) && th !== start) {
  19417. // domUtils.fillNode(me.document, th);
  19418. // if (browser.ie && browser.version == 6) {
  19419. // th.innerHTML = '&nbsp;'
  19420. // }
  19421. // }
  19422. // });
  19423. table.onmouseover = function () {
  19424. me.fireEvent('tablemouseover', table);
  19425. };
  19426. table.onmousemove = function () {
  19427. me.fireEvent('tablemousemove', table);
  19428. me.options.tableDragable && toggleDragButton(true, this, me);
  19429. utils.defer(function () {
  19430. me.fireEvent('contentchange', 50)
  19431. }, true)
  19432. };
  19433. table.onmouseout = function () {
  19434. me.fireEvent('tablemouseout', table);
  19435. toggleDraggableState(me, false, "", null);
  19436. hideDragLine(me);
  19437. };
  19438. table.onclick = function (evt) {
  19439. evt = me.window.event || evt;
  19440. var target = getParentTdOrTh(evt.target || evt.srcElement);
  19441. if (!target) return;
  19442. var ut = getUETable(target),
  19443. table = ut.table,
  19444. cellInfo = ut.getCellInfo(target),
  19445. cellsRange,
  19446. rng = me.selection.getRange();
  19447. // if ("topLeft" == inPosition(table, mouseCoords(evt))) {
  19448. // cellsRange = ut.getCellsRange(ut.table.rows[0].cells[0], ut.getLastCell());
  19449. // ut.setSelected(cellsRange);
  19450. // return;
  19451. // }
  19452. // if ("bottomRight" == inPosition(table, mouseCoords(evt))) {
  19453. //
  19454. // return;
  19455. // }
  19456. if (inTableSide(table, target, evt, true)) {
  19457. var endTdCol = ut.getCell(ut.indexTable[ut.rowsNum - 1][cellInfo.colIndex].rowIndex, ut.indexTable[ut.rowsNum - 1][cellInfo.colIndex].cellIndex);
  19458. if (evt.shiftKey && ut.selectedTds.length) {
  19459. if (ut.selectedTds[0] !== endTdCol) {
  19460. cellsRange = ut.getCellsRange(ut.selectedTds[0], endTdCol);
  19461. ut.setSelected(cellsRange);
  19462. } else {
  19463. rng && rng.selectNodeContents(endTdCol).select();
  19464. }
  19465. } else {
  19466. if (target !== endTdCol) {
  19467. cellsRange = ut.getCellsRange(target, endTdCol);
  19468. ut.setSelected(cellsRange);
  19469. } else {
  19470. rng && rng.selectNodeContents(endTdCol).select();
  19471. }
  19472. }
  19473. return;
  19474. }
  19475. if (inTableSide(table, target, evt)) {
  19476. var endTdRow = ut.getCell(ut.indexTable[cellInfo.rowIndex][ut.colsNum - 1].rowIndex, ut.indexTable[cellInfo.rowIndex][ut.colsNum - 1].cellIndex);
  19477. if (evt.shiftKey && ut.selectedTds.length) {
  19478. if (ut.selectedTds[0] !== endTdRow) {
  19479. cellsRange = ut.getCellsRange(ut.selectedTds[0], endTdRow);
  19480. ut.setSelected(cellsRange);
  19481. } else {
  19482. rng && rng.selectNodeContents(endTdRow).select();
  19483. }
  19484. } else {
  19485. if (target !== endTdRow) {
  19486. cellsRange = ut.getCellsRange(target, endTdRow);
  19487. ut.setSelected(cellsRange);
  19488. } else {
  19489. rng && rng.selectNodeContents(endTdRow).select();
  19490. }
  19491. }
  19492. }
  19493. };
  19494. });
  19495. switchBorderColor(me, true);
  19496. });
  19497. domUtils.on(me.document, "mousemove", mouseMoveEvent);
  19498. domUtils.on(me.document, "mouseout", function (evt) {
  19499. var target = evt.target || evt.srcElement;
  19500. if (target.tagName == "TABLE") {
  19501. toggleDraggableState(me, false, "", null);
  19502. }
  19503. });
  19504. /**
  19505. * 表格隔行变色
  19506. */
  19507. me.addListener("interlacetable", function (type, table, classList) {
  19508. if (!table) return;
  19509. var me = this,
  19510. rows = table.rows,
  19511. len = rows.length,
  19512. getClass = function (list, index, repeat) {
  19513. return list[index] ? list[index] : repeat ? list[index % list.length] : "";
  19514. };
  19515. for (var i = 0; i < len; i++) {
  19516. rows[i].className = getClass(classList || me.options.classList, i, true);
  19517. }
  19518. });
  19519. me.addListener("uninterlacetable", function (type, table) {
  19520. if (!table) return;
  19521. var me = this,
  19522. rows = table.rows,
  19523. classList = me.options.classList,
  19524. len = rows.length;
  19525. for (var i = 0; i < len; i++) {
  19526. domUtils.removeClasses(rows[i], classList);
  19527. }
  19528. });
  19529. me.addListener("mousedown", mouseDownEvent);
  19530. me.addListener("mouseup", mouseUpEvent);
  19531. //拖动的时候触发mouseup
  19532. domUtils.on(me.body, 'dragstart', function (evt) {
  19533. mouseUpEvent.call(me, 'dragstart', evt);
  19534. });
  19535. me.addOutputRule(function (root) {
  19536. utils.each(root.getNodesByTagName('div'), function (n) {
  19537. if (n.getAttr('id') == 'ue_tableDragLine') {
  19538. n.parentNode.removeChild(n);
  19539. }
  19540. });
  19541. });
  19542. var currentRowIndex = 0;
  19543. me.addListener("mousedown", function () {
  19544. currentRowIndex = 0;
  19545. });
  19546. me.addListener('tabkeydown', function () {
  19547. var range = this.selection.getRange(),
  19548. common = range.getCommonAncestor(true, true),
  19549. table = domUtils.findParentByTagName(common, 'table');
  19550. if (table) {
  19551. if (domUtils.findParentByTagName(common, 'caption', true)) {
  19552. var cell = domUtils.getElementsByTagName(table, 'th td');
  19553. if (cell && cell.length) {
  19554. range.setStart(cell[0], 0).setCursor(false, true)
  19555. }
  19556. } else {
  19557. var cell = domUtils.findParentByTagName(common, ['td', 'th'], true),
  19558. ua = getUETable(cell);
  19559. currentRowIndex = cell.rowSpan > 1 ? currentRowIndex : ua.getCellInfo(cell).rowIndex;
  19560. var nextCell = ua.getTabNextCell(cell, currentRowIndex);
  19561. if (nextCell) {
  19562. if (isEmptyBlock(nextCell)) {
  19563. range.setStart(nextCell, 0).setCursor(false, true)
  19564. } else {
  19565. range.selectNodeContents(nextCell).select()
  19566. }
  19567. } else {
  19568. me.fireEvent('saveScene');
  19569. me.__hasEnterExecCommand = true;
  19570. this.execCommand('insertrownext');
  19571. me.__hasEnterExecCommand = false;
  19572. range = this.selection.getRange();
  19573. range.setStart(table.rows[table.rows.length - 1].cells[0], 0).setCursor();
  19574. me.fireEvent('saveScene');
  19575. }
  19576. }
  19577. return true;
  19578. }
  19579. });
  19580. browser.ie && me.addListener('selectionchange', function () {
  19581. toggleDraggableState(this, false, "", null);
  19582. });
  19583. me.addListener("keydown", function (type, evt) {
  19584. var me = this;
  19585. //处理在表格的最后一个输入tab产生新的表格
  19586. var keyCode = evt.keyCode || evt.which;
  19587. if (keyCode == 8 || keyCode == 46) {
  19588. return;
  19589. }
  19590. var notCtrlKey = !evt.ctrlKey && !evt.metaKey && !evt.shiftKey && !evt.altKey;
  19591. notCtrlKey && removeSelectedClass(domUtils.getElementsByTagName(me.body, "td"));
  19592. var ut = getUETableBySelected(me);
  19593. if (!ut) return;
  19594. notCtrlKey && ut.clearSelected();
  19595. });
  19596. me.addListener("beforegetcontent", function () {
  19597. switchBorderColor(this, false);
  19598. browser.ie && utils.each(this.document.getElementsByTagName('caption'), function (ci) {
  19599. if (domUtils.isEmptyNode(ci)) {
  19600. ci.innerHTML = '&nbsp;'
  19601. }
  19602. });
  19603. });
  19604. me.addListener("aftergetcontent", function () {
  19605. switchBorderColor(this, true);
  19606. });
  19607. me.addListener("getAllHtml", function () {
  19608. removeSelectedClass(me.document.getElementsByTagName("td"));
  19609. });
  19610. //修正全屏状态下插入的表格宽度在非全屏状态下撑开编辑器的情况
  19611. me.addListener("fullscreenchanged", function (type, fullscreen) {
  19612. if (!fullscreen) {
  19613. var ratio = this.body.offsetWidth / document.body.offsetWidth,
  19614. tables = domUtils.getElementsByTagName(this.body, "table");
  19615. utils.each(tables, function (table) {
  19616. if (table.offsetWidth < me.body.offsetWidth) return false;
  19617. var tds = domUtils.getElementsByTagName(table, "td"),
  19618. backWidths = [];
  19619. utils.each(tds, function (td) {
  19620. backWidths.push(td.offsetWidth);
  19621. });
  19622. for (var i = 0, td; td = tds[i]; i++) {
  19623. td.setAttribute("width", Math.floor(backWidths[i] * ratio));
  19624. }
  19625. table.setAttribute("width", Math.floor(getTableWidth(me, needIEHack, getDefaultValue(me))))
  19626. });
  19627. }
  19628. });
  19629. //重写execCommand命令,用于处理框选时的处理
  19630. var oldExecCommand = me.execCommand;
  19631. me.execCommand = function (cmd, datatat) {
  19632. var me = this,
  19633. args = arguments;
  19634. cmd = cmd.toLowerCase();
  19635. var ut = getUETableBySelected(me),
  19636. tds,
  19637. range = new dom.Range(me.document),
  19638. cmdFun = me.commands[cmd] || UE.commands[cmd],
  19639. result;
  19640. if (!cmdFun) return;
  19641. if (ut && !commands[cmd] && !cmdFun.notNeedUndo && !me.__hasEnterExecCommand) {
  19642. me.__hasEnterExecCommand = true;
  19643. me.fireEvent("beforeexeccommand", cmd);
  19644. tds = ut.selectedTds;
  19645. var lastState = -2,
  19646. lastValue = -2,
  19647. value, state;
  19648. for (var i = 0, td; td = tds[i]; i++) {
  19649. if (isEmptyBlock(td)) {
  19650. range.setStart(td, 0).setCursor(false, true)
  19651. } else {
  19652. range.selectNode(td).select(true);
  19653. }
  19654. state = me.queryCommandState(cmd);
  19655. value = me.queryCommandValue(cmd);
  19656. if (state != -1) {
  19657. if (lastState !== state || lastValue !== value) {
  19658. me._ignoreContentChange = true;
  19659. result = oldExecCommand.apply(me, arguments);
  19660. me._ignoreContentChange = false;
  19661. }
  19662. lastState = me.queryCommandState(cmd);
  19663. lastValue = me.queryCommandValue(cmd);
  19664. if (domUtils.isEmptyBlock(td)) {
  19665. domUtils.fillNode(me.document, td)
  19666. }
  19667. }
  19668. }
  19669. range.setStart(tds[0], 0).shrinkBoundary(true).setCursor(false, true);
  19670. me.fireEvent('contentchange');
  19671. me.fireEvent("afterexeccommand", cmd);
  19672. me.__hasEnterExecCommand = false;
  19673. me._selectionChange();
  19674. } else {
  19675. result = oldExecCommand.apply(me, arguments);
  19676. }
  19677. return result;
  19678. };
  19679. });
  19680. /**
  19681. * 删除obj的宽高style,改成属性宽高
  19682. * @param obj
  19683. * @param replaceToProperty
  19684. */
  19685. function removeStyleSize(obj, replaceToProperty) {
  19686. removeStyle(obj, "width", true);
  19687. removeStyle(obj, "height", true);
  19688. }
  19689. function removeStyle(obj, styleName, replaceToProperty) {
  19690. if (obj.style[styleName]) {
  19691. replaceToProperty && obj.setAttribute(styleName, parseInt(obj.style[styleName], 10));
  19692. obj.style[styleName] = "";
  19693. }
  19694. }
  19695. function getParentTdOrTh(ele) {
  19696. if (ele.tagName == "TD" || ele.tagName == "TH") return ele;
  19697. var td;
  19698. if (td = domUtils.findParentByTagName(ele, "td", true) || domUtils.findParentByTagName(ele, "th", true)) return td;
  19699. return null;
  19700. }
  19701. function isEmptyBlock(node) {
  19702. var reg = new RegExp(domUtils.fillChar, 'g');
  19703. if (node[browser.ie ? 'innerText' : 'textContent'].replace(/^\s*$/, '').replace(reg, '').length > 0) {
  19704. return 0;
  19705. }
  19706. for (var n in dtd.$isNotEmpty) {
  19707. if (node.getElementsByTagName(n).length) {
  19708. return 0;
  19709. }
  19710. }
  19711. return 1;
  19712. }
  19713. function mouseCoords(evt) {
  19714. if (evt.pageX || evt.pageY) {
  19715. return {
  19716. x: evt.pageX,
  19717. y: evt.pageY
  19718. };
  19719. }
  19720. return {
  19721. x: evt.clientX + me.document.body.scrollLeft - me.document.body.clientLeft,
  19722. y: evt.clientY + me.document.body.scrollTop - me.document.body.clientTop
  19723. };
  19724. }
  19725. function mouseMoveEvent(evt) {
  19726. if (isEditorDisabled()) {
  19727. return;
  19728. }
  19729. try {
  19730. //普通状态下鼠标移动
  19731. var target = getParentTdOrTh(evt.target || evt.srcElement),
  19732. pos;
  19733. //区分用户的行为是拖动还是双击
  19734. if (isInResizeBuffer) {
  19735. me.body.style.webkitUserSelect = 'none';
  19736. if (Math.abs(userActionStatus.x - evt.clientX) > offsetOfTableCell || Math.abs(userActionStatus.y - evt.clientY) > offsetOfTableCell) {
  19737. clearTableDragTimer();
  19738. isInResizeBuffer = false;
  19739. singleClickState = 0;
  19740. //drag action
  19741. tableBorderDrag(evt);
  19742. }
  19743. }
  19744. //修改单元格大小时的鼠标移动
  19745. if (onDrag && dragTd) {
  19746. singleClickState = 0;
  19747. me.body.style.webkitUserSelect = 'none';
  19748. me.selection.getNative()[browser.ie9below ? 'empty' : 'removeAllRanges']();
  19749. pos = mouseCoords(evt);
  19750. toggleDraggableState(me, true, onDrag, pos, target);
  19751. if (onDrag == "h") {
  19752. dragLine.style.left = getPermissionX(dragTd, evt) + "px";
  19753. } else if (onDrag == "v") {
  19754. dragLine.style.top = getPermissionY(dragTd, evt) + "px";
  19755. }
  19756. return;
  19757. }
  19758. //当鼠标处于table上时,修改移动过程中的光标状态
  19759. if (target) {
  19760. //针对使用table作为容器的组件不触发拖拽效果
  19761. if (me.fireEvent('excludetable', target) === true)
  19762. return;
  19763. pos = mouseCoords(evt);
  19764. var state = getRelation(target, pos),
  19765. table = domUtils.findParentByTagName(target, "table", true);
  19766. if (inTableSide(table, target, evt, true)) {
  19767. if (me.fireEvent("excludetable", table) === true) return;
  19768. me.body.style.cursor = "url(" + me.options.cursorpath + "h.png),pointer";
  19769. } else if (inTableSide(table, target, evt)) {
  19770. if (me.fireEvent("excludetable", table) === true) return;
  19771. me.body.style.cursor = "url(" + me.options.cursorpath + "v.png),pointer";
  19772. } else {
  19773. me.body.style.cursor = "text";
  19774. var curCell = target;
  19775. if (/\d/.test(state)) {
  19776. state = state.replace(/\d/, '');
  19777. target = getUETable(target).getPreviewCell(target, state == "v");
  19778. }
  19779. //位于第一行的顶部或者第一列的左边时不可拖动
  19780. toggleDraggableState(me, target ? !!state : false, target ? state : '', pos, target);
  19781. }
  19782. } else {
  19783. toggleDragButton(false, table, me);
  19784. }
  19785. } catch (e) {
  19786. showError(e);
  19787. }
  19788. }
  19789. var dragButtonTimer;
  19790. function toggleDragButton(show, table, editor) {
  19791. if (!show) {
  19792. if (dragOver) return;
  19793. dragButtonTimer = setTimeout(function () {
  19794. !dragOver && dragButton && dragButton.parentNode && dragButton.parentNode.removeChild(dragButton);
  19795. }, 2000);
  19796. } else {
  19797. createDragButton(table, editor);
  19798. }
  19799. }
  19800. function createDragButton(table, editor) {
  19801. var pos = domUtils.getXY(table),
  19802. doc = table.ownerDocument;
  19803. if (dragButton && dragButton.parentNode) return dragButton;
  19804. dragButton = doc.createElement("div");
  19805. dragButton.contentEditable = false;
  19806. dragButton.innerHTML = "";
  19807. 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;";
  19808. domUtils.unSelectable(dragButton);
  19809. dragButton.onmouseover = function (evt) {
  19810. dragOver = true;
  19811. };
  19812. dragButton.onmouseout = function (evt) {
  19813. dragOver = false;
  19814. };
  19815. domUtils.on(dragButton, 'click', function (type, evt) {
  19816. doClick(evt, this);
  19817. });
  19818. domUtils.on(dragButton, 'dblclick', function (type, evt) {
  19819. doDblClick(evt);
  19820. });
  19821. domUtils.on(dragButton, 'dragstart', function (type, evt) {
  19822. domUtils.preventDefault(evt);
  19823. });
  19824. var timer;
  19825. function doClick(evt, button) {
  19826. // 部分浏览器下需要清理
  19827. clearTimeout(timer);
  19828. timer = setTimeout(function () {
  19829. editor.fireEvent("tableClicked", table, button);
  19830. }, 300);
  19831. }
  19832. function doDblClick(evt) {
  19833. clearTimeout(timer);
  19834. var ut = getUETable(table),
  19835. start = table.rows[0].cells[0],
  19836. end = ut.getLastCell(),
  19837. range = ut.getCellsRange(start, end);
  19838. editor.selection.getRange().setStart(start, 0).setCursor(false, true);
  19839. ut.setSelected(range);
  19840. }
  19841. doc.body.appendChild(dragButton);
  19842. }
  19843. // function inPosition(table, pos) {
  19844. // var tablePos = domUtils.getXY(table),
  19845. // width = table.offsetWidth,
  19846. // height = table.offsetHeight;
  19847. // if (pos.x - tablePos.x < 5 && pos.y - tablePos.y < 5) {
  19848. // return "topLeft";
  19849. // } else if (tablePos.x + width - pos.x < 5 && tablePos.y + height - pos.y < 5) {
  19850. // return "bottomRight";
  19851. // }
  19852. // }
  19853. function inTableSide(table, cell, evt, top) {
  19854. var pos = mouseCoords(evt),
  19855. state = getRelation(cell, pos);
  19856. if (top) {
  19857. var caption = table.getElementsByTagName("caption")[0],
  19858. capHeight = caption ? caption.offsetHeight : 0;
  19859. return (state == "v1") && ((pos.y - domUtils.getXY(table).y - capHeight) < 8);
  19860. } else {
  19861. return (state == "h1") && ((pos.x - domUtils.getXY(table).x) < 8);
  19862. }
  19863. }
  19864. /**
  19865. * 获取拖动时允许的X轴坐标
  19866. * @param dragTd
  19867. * @param evt
  19868. */
  19869. function getPermissionX(dragTd, evt) {
  19870. var ut = getUETable(dragTd);
  19871. if (ut) {
  19872. var preTd = ut.getSameEndPosCells(dragTd, "x")[0],
  19873. nextTd = ut.getSameStartPosXCells(dragTd)[0],
  19874. mouseX = mouseCoords(evt).x,
  19875. left = (preTd ? domUtils.getXY(preTd).x : domUtils.getXY(ut.table).x) + 20,
  19876. right = nextTd ? domUtils.getXY(nextTd).x + nextTd.offsetWidth - 20 : (me.body.offsetWidth + 5 || parseInt(domUtils.getComputedStyle(me.body, "width"), 10));
  19877. left += cellMinWidth;
  19878. right -= cellMinWidth;
  19879. return mouseX < left ? left : mouseX > right ? right : mouseX;
  19880. }
  19881. }
  19882. /**
  19883. * 获取拖动时允许的Y轴坐标
  19884. */
  19885. function getPermissionY(dragTd, evt) {
  19886. try {
  19887. var top = domUtils.getXY(dragTd).y,
  19888. mousePosY = mouseCoords(evt).y;
  19889. return mousePosY < top ? top : mousePosY;
  19890. } catch (e) {
  19891. showError(e);
  19892. }
  19893. }
  19894. /**
  19895. * 移动状态切换
  19896. */
  19897. function toggleDraggableState(editor, draggable, dir, mousePos, cell) {
  19898. try {
  19899. editor.body.style.cursor = dir == "h" ? "col-resize" : dir == "v" ? "row-resize" : "text";
  19900. if (browser.ie) {
  19901. if (dir && !mousedown && !getUETableBySelected(editor)) {
  19902. getDragLine(editor, editor.document);
  19903. showDragLineAt(dir, cell);
  19904. } else {
  19905. hideDragLine(editor)
  19906. }
  19907. }
  19908. onBorder = draggable;
  19909. } catch (e) {
  19910. showError(e);
  19911. }
  19912. }
  19913. /**
  19914. * 获取与UETable相关的resize line
  19915. * @param uetable UETable对象
  19916. */
  19917. function getResizeLineByUETable() {
  19918. var lineId = '_UETableResizeLine',
  19919. line = this.document.getElementById(lineId);
  19920. if (!line) {
  19921. line = this.document.createElement("div");
  19922. line.id = lineId;
  19923. line.contnetEditable = false;
  19924. line.setAttribute("unselectable", "on");
  19925. var styles = {
  19926. width: 2 * cellBorderWidth + 1 + 'px',
  19927. position: 'absolute',
  19928. 'z-index': 100000,
  19929. cursor: 'col-resize',
  19930. background: 'red',
  19931. display: 'none'
  19932. };
  19933. //切换状态
  19934. line.onmouseout = function () {
  19935. this.style.display = 'none';
  19936. };
  19937. utils.extend(line.style, styles);
  19938. this.document.body.appendChild(line);
  19939. }
  19940. return line;
  19941. }
  19942. /**
  19943. * 更新resize-line
  19944. */
  19945. function updateResizeLine(cell, uetable) {
  19946. var line = getResizeLineByUETable.call(this),
  19947. table = uetable.table,
  19948. styles = {
  19949. top: domUtils.getXY(table).y + 'px',
  19950. left: domUtils.getXY(cell).x + cell.offsetWidth - cellBorderWidth + 'px',
  19951. display: 'block',
  19952. height: table.offsetHeight + 'px'
  19953. };
  19954. utils.extend(line.style, styles);
  19955. }
  19956. /**
  19957. * 显示resize-line
  19958. */
  19959. function showResizeLine(cell) {
  19960. var uetable = getUETable(cell);
  19961. updateResizeLine.call(this, cell, uetable);
  19962. }
  19963. /**
  19964. * 获取鼠标与当前单元格的相对位置
  19965. * @param ele
  19966. * @param mousePos
  19967. */
  19968. function getRelation(ele, mousePos) {
  19969. var elePos = domUtils.getXY(ele);
  19970. if (!elePos) {
  19971. return '';
  19972. }
  19973. if (elePos.x + ele.offsetWidth - mousePos.x < cellBorderWidth) {
  19974. return "h";
  19975. }
  19976. if (mousePos.x - elePos.x < cellBorderWidth) {
  19977. return 'h1'
  19978. }
  19979. if (elePos.y + ele.offsetHeight - mousePos.y < cellBorderWidth) {
  19980. return "v";
  19981. }
  19982. if (mousePos.y - elePos.y < cellBorderWidth) {
  19983. return 'v1'
  19984. }
  19985. return '';
  19986. }
  19987. function mouseDownEvent(type, evt) {
  19988. if (isEditorDisabled()) {
  19989. return;
  19990. }
  19991. userActionStatus = {
  19992. x: evt.clientX,
  19993. y: evt.clientY
  19994. };
  19995. //右键菜单单独处理
  19996. if (evt.button == 2) {
  19997. var ut = getUETableBySelected(me),
  19998. flag = false;
  19999. if (ut) {
  20000. var td = getTargetTd(me, evt);
  20001. utils.each(ut.selectedTds, function (ti) {
  20002. if (ti === td) {
  20003. flag = true;
  20004. }
  20005. });
  20006. if (!flag) {
  20007. removeSelectedClass(domUtils.getElementsByTagName(me.body, "th td"));
  20008. ut.clearSelected()
  20009. } else {
  20010. td = ut.selectedTds[0];
  20011. setTimeout(function () {
  20012. me.selection.getRange().setStart(td, 0).setCursor(false, true);
  20013. }, 0);
  20014. }
  20015. }
  20016. } else {
  20017. tableClickHander(evt);
  20018. }
  20019. }
  20020. //清除表格的计时器
  20021. function clearTableTimer() {
  20022. tabTimer && clearTimeout(tabTimer);
  20023. tabTimer = null;
  20024. }
  20025. //双击收缩
  20026. function tableDbclickHandler(evt) {
  20027. singleClickState = 0;
  20028. evt = evt || me.window.event;
  20029. var target = getParentTdOrTh(evt.target || evt.srcElement);
  20030. if (target) {
  20031. var h;
  20032. if (h = getRelation(target, mouseCoords(evt))) {
  20033. hideDragLine(me);
  20034. if (h == 'h1') {
  20035. h = 'h';
  20036. if (inTableSide(domUtils.findParentByTagName(target, "table"), target, evt)) {
  20037. me.execCommand('adaptbywindow');
  20038. } else {
  20039. target = getUETable(target).getPreviewCell(target);
  20040. if (target) {
  20041. var rng = me.selection.getRange();
  20042. rng.selectNodeContents(target).setCursor(true, true)
  20043. }
  20044. }
  20045. }
  20046. if (h == 'h') {
  20047. var ut = getUETable(target),
  20048. table = ut.table,
  20049. cells = getCellsByMoveBorder(target, table, true);
  20050. cells = extractArray(cells, 'left');
  20051. ut.width = ut.offsetWidth;
  20052. var oldWidth = [],
  20053. newWidth = [];
  20054. utils.each(cells, function (cell) {
  20055. oldWidth.push(cell.offsetWidth);
  20056. });
  20057. utils.each(cells, function (cell) {
  20058. cell.removeAttribute("width");
  20059. });
  20060. window.setTimeout(function () {
  20061. //是否允许改变
  20062. var changeable = true;
  20063. utils.each(cells, function (cell, index) {
  20064. var width = cell.offsetWidth;
  20065. if (width > oldWidth[index]) {
  20066. changeable = false;
  20067. return false;
  20068. }
  20069. newWidth.push(width);
  20070. });
  20071. var change = changeable ? newWidth : oldWidth;
  20072. utils.each(cells, function (cell, index) {
  20073. cell.width = change[index] - getTabcellSpace();
  20074. });
  20075. }, 0);
  20076. // minWidth -= cellMinWidth;
  20077. //
  20078. // table.removeAttribute("width");
  20079. // utils.each(cells, function (cell) {
  20080. // cell.style.width = "";
  20081. // cell.width -= minWidth;
  20082. // });
  20083. }
  20084. }
  20085. }
  20086. }
  20087. function tableClickHander(evt) {
  20088. removeSelectedClass(domUtils.getElementsByTagName(me.body, "td th"));
  20089. //trace:3113
  20090. //选中单元格,点击table外部,不会清掉table上挂的ueTable,会引起getUETableBySelected方法返回值
  20091. utils.each(me.document.getElementsByTagName('table'), function (t) {
  20092. t.ueTable = null;
  20093. });
  20094. startTd = getTargetTd(me, evt);
  20095. if (!startTd) return;
  20096. var table = domUtils.findParentByTagName(startTd, "table", true);
  20097. ut = getUETable(table);
  20098. ut && ut.clearSelected();
  20099. //判断当前鼠标状态
  20100. if (!onBorder) {
  20101. me.document.body.style.webkitUserSelect = '';
  20102. mousedown = true;
  20103. me.addListener('mouseover', mouseOverEvent);
  20104. } else {
  20105. //边框上的动作处理
  20106. borderActionHandler(evt);
  20107. }
  20108. }
  20109. //处理表格边框上的动作, 这里做延时处理,避免两种动作互相影响
  20110. function borderActionHandler(evt) {
  20111. if (browser.ie) {
  20112. evt = reconstruct(evt);
  20113. }
  20114. clearTableDragTimer();
  20115. //是否正在等待resize的缓冲中
  20116. isInResizeBuffer = true;
  20117. tableDragTimer = setTimeout(function () {
  20118. tableBorderDrag(evt);
  20119. }, dblclickTime);
  20120. }
  20121. function extractArray(originArr, key) {
  20122. var result = [],
  20123. tmp = null;
  20124. for (var i = 0, len = originArr.length; i < len; i++) {
  20125. tmp = originArr[i][key];
  20126. if (tmp) {
  20127. result.push(tmp);
  20128. }
  20129. }
  20130. return result;
  20131. }
  20132. function clearTableDragTimer() {
  20133. tableDragTimer && clearTimeout(tableDragTimer);
  20134. tableDragTimer = null;
  20135. }
  20136. function reconstruct(obj) {
  20137. var attrs = ['pageX', 'pageY', 'clientX', 'clientY', 'srcElement', 'target'],
  20138. newObj = {};
  20139. if (obj) {
  20140. for (var i = 0, key, val; key = attrs[i]; i++) {
  20141. val = obj[key];
  20142. val && (newObj[key] = val);
  20143. }
  20144. }
  20145. return newObj;
  20146. }
  20147. //边框拖动
  20148. function tableBorderDrag(evt) {
  20149. isInResizeBuffer = false;
  20150. startTd = evt.target || evt.srcElement;
  20151. if (!startTd) return;
  20152. var state = getRelation(startTd, mouseCoords(evt));
  20153. if (/\d/.test(state)) {
  20154. state = state.replace(/\d/, '');
  20155. startTd = getUETable(startTd).getPreviewCell(startTd, state == 'v');
  20156. }
  20157. hideDragLine(me);
  20158. getDragLine(me, me.document);
  20159. me.fireEvent('saveScene');
  20160. showDragLineAt(state, startTd);
  20161. mousedown = true;
  20162. //拖动开始
  20163. onDrag = state;
  20164. dragTd = startTd;
  20165. }
  20166. function mouseUpEvent(type, evt) {
  20167. if (isEditorDisabled()) {
  20168. return;
  20169. }
  20170. clearTableDragTimer();
  20171. isInResizeBuffer = false;
  20172. if (onBorder) {
  20173. singleClickState = ++singleClickState % 3;
  20174. userActionStatus = {
  20175. x: evt.clientX,
  20176. y: evt.clientY
  20177. };
  20178. tableResizeTimer = setTimeout(function () {
  20179. singleClickState > 0 && singleClickState--;
  20180. }, dblclickTime);
  20181. if (singleClickState === 2) {
  20182. singleClickState = 0;
  20183. tableDbclickHandler(evt);
  20184. return;
  20185. }
  20186. }
  20187. if (evt.button == 2) return;
  20188. var me = this;
  20189. //清除表格上原生跨选问题
  20190. var range = me.selection.getRange(),
  20191. start = domUtils.findParentByTagName(range.startContainer, 'table', true),
  20192. end = domUtils.findParentByTagName(range.endContainer, 'table', true);
  20193. if (start || end) {
  20194. if (start === end) {
  20195. start = domUtils.findParentByTagName(range.startContainer, ['td', 'th', 'caption'], true);
  20196. end = domUtils.findParentByTagName(range.endContainer, ['td', 'th', 'caption'], true);
  20197. if (start !== end) {
  20198. me.selection.clearRange()
  20199. }
  20200. } else {
  20201. me.selection.clearRange()
  20202. }
  20203. }
  20204. mousedown = false;
  20205. me.document.body.style.webkitUserSelect = '';
  20206. //拖拽状态下的mouseUP
  20207. if (onDrag && dragTd) {
  20208. me.selection.getNative()[browser.ie9below ? 'empty' : 'removeAllRanges']();
  20209. singleClickState = 0;
  20210. dragLine = me.document.getElementById('ue_tableDragLine');
  20211. // trace 3973
  20212. if (dragLine) {
  20213. var dragTdPos = domUtils.getXY(dragTd),
  20214. dragLinePos = domUtils.getXY(dragLine);
  20215. switch (onDrag) {
  20216. case "h":
  20217. changeColWidth(dragTd, dragLinePos.x - dragTdPos.x);
  20218. break;
  20219. case "v":
  20220. changeRowHeight(dragTd, dragLinePos.y - dragTdPos.y - dragTd.offsetHeight);
  20221. break;
  20222. default:
  20223. }
  20224. onDrag = "";
  20225. dragTd = null;
  20226. hideDragLine(me);
  20227. me.fireEvent('saveScene');
  20228. return;
  20229. }
  20230. }
  20231. //正常状态下的mouseup
  20232. if (!startTd) {
  20233. var target = domUtils.findParentByTagName(evt.target || evt.srcElement, "td", true);
  20234. if (!target) target = domUtils.findParentByTagName(evt.target || evt.srcElement, "th", true);
  20235. if (target && (target.tagName == "TD" || target.tagName == "TH")) {
  20236. if (me.fireEvent("excludetable", target) === true) return;
  20237. range = new dom.Range(me.document);
  20238. range.setStart(target, 0).setCursor(false, true);
  20239. }
  20240. } else {
  20241. var ut = getUETable(startTd),
  20242. cell = ut ? ut.selectedTds[0] : null;
  20243. if (cell) {
  20244. range = new dom.Range(me.document);
  20245. if (domUtils.isEmptyBlock(cell)) {
  20246. range.setStart(cell, 0).setCursor(false, true);
  20247. } else {
  20248. range.selectNodeContents(cell).shrinkBoundary().setCursor(false, true);
  20249. }
  20250. } else {
  20251. range = me.selection.getRange().shrinkBoundary();
  20252. if (!range.collapsed) {
  20253. var start = domUtils.findParentByTagName(range.startContainer, ['td', 'th'], true),
  20254. end = domUtils.findParentByTagName(range.endContainer, ['td', 'th'], true);
  20255. //在table里边的不能清除
  20256. if (start && !end || !start && end || start && end && start !== end) {
  20257. range.setCursor(false, true);
  20258. }
  20259. }
  20260. }
  20261. startTd = null;
  20262. me.removeListener('mouseover', mouseOverEvent);
  20263. }
  20264. me._selectionChange(250, evt);
  20265. }
  20266. function mouseOverEvent(type, evt) {
  20267. if (isEditorDisabled()) {
  20268. return;
  20269. }
  20270. var me = this,
  20271. tar = evt.target || evt.srcElement;
  20272. currentTd = domUtils.findParentByTagName(tar, "td", true) || domUtils.findParentByTagName(tar, "th", true);
  20273. //需要判断两个TD是否位于同一个表格内
  20274. if (startTd && currentTd &&
  20275. ((startTd.tagName == "TD" && currentTd.tagName == "TD") || (startTd.tagName == "TH" && currentTd.tagName == "TH")) &&
  20276. domUtils.findParentByTagName(startTd, 'table') == domUtils.findParentByTagName(currentTd, 'table')) {
  20277. var ut = getUETable(currentTd);
  20278. if (startTd != currentTd) {
  20279. me.document.body.style.webkitUserSelect = 'none';
  20280. me.selection.getNative()[browser.ie9below ? 'empty' : 'removeAllRanges']();
  20281. var range = ut.getCellsRange(startTd, currentTd);
  20282. ut.setSelected(range);
  20283. } else {
  20284. me.document.body.style.webkitUserSelect = '';
  20285. ut.clearSelected();
  20286. }
  20287. }
  20288. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
  20289. }
  20290. function setCellHeight(cell, height, backHeight) {
  20291. var lineHight = parseInt(domUtils.getComputedStyle(cell, "line-height"), 10),
  20292. tmpHeight = backHeight + height;
  20293. height = tmpHeight < lineHight ? lineHight : tmpHeight;
  20294. if (cell.style.height) cell.style.height = "";
  20295. cell.rowSpan == 1 ? cell.setAttribute("height", height) : (cell.removeAttribute && cell.removeAttribute("height"));
  20296. }
  20297. function getWidth(cell) {
  20298. if (!cell) return 0;
  20299. return parseInt(domUtils.getComputedStyle(cell, "width"), 10);
  20300. }
  20301. function changeColWidth(cell, changeValue) {
  20302. var ut = getUETable(cell);
  20303. if (ut) {
  20304. //根据当前移动的边框获取相关的单元格
  20305. var table = ut.table,
  20306. cells = getCellsByMoveBorder(cell, table);
  20307. table.style.width = "";
  20308. table.removeAttribute("width");
  20309. //修正改变量
  20310. changeValue = correctChangeValue(changeValue, cell, cells);
  20311. if (cell.nextSibling) {
  20312. var i = 0;
  20313. utils.each(cells, function (cellGroup) {
  20314. cellGroup.left.width = (+cellGroup.left.width) + changeValue;
  20315. cellGroup.right && (cellGroup.right.width = (+cellGroup.right.width) - changeValue);
  20316. });
  20317. } else {
  20318. utils.each(cells, function (cellGroup) {
  20319. cellGroup.left.width -= -changeValue;
  20320. });
  20321. }
  20322. }
  20323. }
  20324. function isEditorDisabled() {
  20325. return me.body.contentEditable === "false";
  20326. }
  20327. function changeRowHeight(td, changeValue) {
  20328. if (Math.abs(changeValue) < 10) return;
  20329. var ut = getUETable(td);
  20330. if (ut) {
  20331. var cells = ut.getSameEndPosCells(td, "y"),
  20332. //备份需要连带变化的td的原始高度,否则后期无法获取正确的值
  20333. backHeight = cells[0] ? cells[0].offsetHeight : 0;
  20334. for (var i = 0, cell; cell = cells[i++];) {
  20335. setCellHeight(cell, changeValue, backHeight);
  20336. }
  20337. }
  20338. }
  20339. /**
  20340. * 获取调整单元格大小的相关单元格
  20341. * @isContainMergeCell 返回的结果中是否包含发生合并后的单元格
  20342. */
  20343. function getCellsByMoveBorder(cell, table, isContainMergeCell) {
  20344. if (!table) {
  20345. table = domUtils.findParentByTagName(cell, 'table');
  20346. }
  20347. if (!table) {
  20348. return null;
  20349. }
  20350. //获取到该单元格所在行的序列号
  20351. var index = domUtils.getNodeIndex(cell),
  20352. temp = cell,
  20353. rows = table.rows,
  20354. colIndex = 0;
  20355. while (temp) {
  20356. //获取到当前单元格在未发生单元格合并时的序列
  20357. if (temp.nodeType === 1) {
  20358. colIndex += (temp.colSpan || 1);
  20359. }
  20360. temp = temp.previousSibling;
  20361. }
  20362. temp = null;
  20363. //记录想关的单元格
  20364. var borderCells = [];
  20365. utils.each(rows, function (tabRow) {
  20366. var cells = tabRow.cells,
  20367. currIndex = 0;
  20368. utils.each(cells, function (tabCell) {
  20369. currIndex += (tabCell.colSpan || 1);
  20370. if (currIndex === colIndex) {
  20371. borderCells.push({
  20372. left: tabCell,
  20373. right: tabCell.nextSibling || null
  20374. });
  20375. return false;
  20376. } else if (currIndex > colIndex) {
  20377. if (isContainMergeCell) {
  20378. borderCells.push({
  20379. left: tabCell
  20380. });
  20381. }
  20382. return false;
  20383. }
  20384. });
  20385. });
  20386. return borderCells;
  20387. }
  20388. /**
  20389. * 通过给定的单元格集合获取最小的单元格width
  20390. */
  20391. function getMinWidthByTableCells(cells) {
  20392. var minWidth = Number.MAX_VALUE;
  20393. for (var i = 0, curCell; curCell = cells[i]; i++) {
  20394. minWidth = Math.min(minWidth, curCell.width || getTableCellWidth(curCell));
  20395. }
  20396. return minWidth;
  20397. }
  20398. function correctChangeValue(changeValue, relatedCell, cells) {
  20399. //为单元格的paading预留空间
  20400. changeValue -= getTabcellSpace();
  20401. if (changeValue < 0) {
  20402. return 0;
  20403. }
  20404. changeValue -= getTableCellWidth(relatedCell);
  20405. //确定方向
  20406. var direction = changeValue < 0 ? 'left' : 'right';
  20407. changeValue = Math.abs(changeValue);
  20408. //只关心非最后一个单元格就可以
  20409. utils.each(cells, function (cellGroup) {
  20410. var curCell = cellGroup[direction];
  20411. //为单元格保留最小空间
  20412. if (curCell) {
  20413. changeValue = Math.min(changeValue, getTableCellWidth(curCell) - cellMinWidth);
  20414. }
  20415. });
  20416. //修正越界
  20417. changeValue = changeValue < 0 ? 0 : changeValue;
  20418. return direction === 'left' ? -changeValue : changeValue;
  20419. }
  20420. function getTableCellWidth(cell) {
  20421. var width = 0,
  20422. //偏移纠正量
  20423. offset = 0,
  20424. width = cell.offsetWidth - getTabcellSpace();
  20425. //最后一个节点纠正一下
  20426. if (!cell.nextSibling) {
  20427. width -= getTableCellOffset(cell);
  20428. }
  20429. width = width < 0 ? 0 : width;
  20430. try {
  20431. cell.width = width;
  20432. } catch (e) {}
  20433. return width;
  20434. }
  20435. /**
  20436. * 获取单元格所在表格的最末单元格的偏移量
  20437. */
  20438. function getTableCellOffset(cell) {
  20439. tab = domUtils.findParentByTagName(cell, "table", false);
  20440. if (tab.offsetVal === undefined) {
  20441. var prev = cell.previousSibling;
  20442. if (prev) {
  20443. //最后一个单元格和前一个单元格的width diff结果 如果恰好为一个border width, 则条件成立
  20444. tab.offsetVal = cell.offsetWidth - prev.offsetWidth === UT.borderWidth ? UT.borderWidth : 0;
  20445. } else {
  20446. tab.offsetVal = 0;
  20447. }
  20448. }
  20449. return tab.offsetVal;
  20450. }
  20451. function getTabcellSpace() {
  20452. if (UT.tabcellSpace === undefined) {
  20453. var cell = null,
  20454. tab = me.document.createElement("table"),
  20455. tbody = me.document.createElement("tbody"),
  20456. trow = me.document.createElement("tr"),
  20457. tabcell = me.document.createElement("td"),
  20458. mirror = null;
  20459. tabcell.style.cssText = 'border: 0;';
  20460. tabcell.width = 1;
  20461. trow.appendChild(tabcell);
  20462. trow.appendChild(mirror = tabcell.cloneNode(false));
  20463. tbody.appendChild(trow);
  20464. tab.appendChild(tbody);
  20465. tab.style.cssText = "visibility: hidden;";
  20466. me.body.appendChild(tab);
  20467. UT.paddingSpace = tabcell.offsetWidth - 1;
  20468. var tmpTabWidth = tab.offsetWidth;
  20469. tabcell.style.cssText = '';
  20470. mirror.style.cssText = '';
  20471. UT.borderWidth = (tab.offsetWidth - tmpTabWidth) / 3;
  20472. UT.tabcellSpace = UT.paddingSpace + UT.borderWidth;
  20473. me.body.removeChild(tab);
  20474. }
  20475. getTabcellSpace = function () {
  20476. return UT.tabcellSpace;
  20477. };
  20478. return UT.tabcellSpace;
  20479. }
  20480. function getDragLine(editor, doc) {
  20481. if (mousedown) return;
  20482. dragLine = editor.document.createElement("div");
  20483. domUtils.setAttributes(dragLine, {
  20484. id: "ue_tableDragLine",
  20485. unselectable: 'on',
  20486. contenteditable: false,
  20487. 'onresizestart': 'return false',
  20488. 'ondragstart': 'return false',
  20489. 'onselectstart': 'return false',
  20490. style: "background-color:blue;position:absolute;padding:0;margin:0;background-image:none;border:0px none;opacity:0;filter:alpha(opacity=0)"
  20491. });
  20492. editor.body.appendChild(dragLine);
  20493. }
  20494. function hideDragLine(editor) {
  20495. if (mousedown) return;
  20496. var line;
  20497. while (line = editor.document.getElementById('ue_tableDragLine')) {
  20498. domUtils.remove(line)
  20499. }
  20500. }
  20501. /**
  20502. * 依据state(v|h)在cell位置显示横线
  20503. * @param state
  20504. * @param cell
  20505. */
  20506. function showDragLineAt(state, cell) {
  20507. if (!cell) return;
  20508. var table = domUtils.findParentByTagName(cell, "table"),
  20509. caption = table.getElementsByTagName('caption'),
  20510. width = table.offsetWidth,
  20511. height = table.offsetHeight - (caption.length > 0 ? caption[0].offsetHeight : 0),
  20512. tablePos = domUtils.getXY(table),
  20513. cellPos = domUtils.getXY(cell),
  20514. css;
  20515. switch (state) {
  20516. case "h":
  20517. css = 'height:' + height + 'px;top:' + (tablePos.y + (caption.length > 0 ? caption[0].offsetHeight : 0)) + 'px;left:' + (cellPos.x + cell.offsetWidth);
  20518. dragLine.style.cssText = css + 'px;position: absolute;display:block;background-color:blue;width:1px;border:0; color:blue;opacity:.3;filter:alpha(opacity=30)';
  20519. break;
  20520. case "v":
  20521. css = 'width:' + width + 'px;left:' + tablePos.x + 'px;top:' + (cellPos.y + cell.offsetHeight);
  20522. //必须加上border:0和color:blue,否则低版ie不支持背景色显示
  20523. 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)';
  20524. break;
  20525. default:
  20526. }
  20527. }
  20528. /**
  20529. * 当表格边框颜色为白色时设置为虚线,true为添加虚线
  20530. * @param editor
  20531. * @param flag
  20532. */
  20533. function switchBorderColor(editor, flag) {
  20534. var tableArr = domUtils.getElementsByTagName(editor.body, "table"),
  20535. color;
  20536. for (var i = 0, node; node = tableArr[i++];) {
  20537. var td = domUtils.getElementsByTagName(node, "td");
  20538. if (td[0]) {
  20539. if (flag) {
  20540. color = (td[0].style.borderColor).replace(/\s/g, "");
  20541. if (/(#ffffff)|(rgb\(255,255,255\))/ig.test(color))
  20542. domUtils.addClass(node, "noBorderTable")
  20543. } else {
  20544. domUtils.removeClasses(node, "noBorderTable")
  20545. }
  20546. }
  20547. }
  20548. }
  20549. function getTableWidth(editor, needIEHack, defaultValue) {
  20550. var body = editor.body;
  20551. return body.offsetWidth - (needIEHack ? parseInt(domUtils.getComputedStyle(body, 'margin-left'), 10) * 2 : 0) - defaultValue.tableBorder * 2 - (editor.options.offsetWidth || 0);
  20552. }
  20553. /**
  20554. * 获取当前拖动的单元格
  20555. */
  20556. function getTargetTd(editor, evt) {
  20557. var target = domUtils.findParentByTagName(evt.target || evt.srcElement, ["td", "th"], true),
  20558. dir = null;
  20559. if (!target) {
  20560. return null;
  20561. }
  20562. dir = getRelation(target, mouseCoords(evt));
  20563. //如果有前一个节点, 需要做一个修正, 否则可能会得到一个错误的td
  20564. if (!target) {
  20565. return null;
  20566. }
  20567. if (dir === 'h1' && target.previousSibling) {
  20568. var position = domUtils.getXY(target),
  20569. cellWidth = target.offsetWidth;
  20570. if (Math.abs(position.x + cellWidth - evt.clientX) > cellWidth / 3) {
  20571. target = target.previousSibling;
  20572. }
  20573. } else if (dir === 'v1' && target.parentNode.previousSibling) {
  20574. var position = domUtils.getXY(target),
  20575. cellHeight = target.offsetHeight;
  20576. if (Math.abs(position.y + cellHeight - evt.clientY) > cellHeight / 3) {
  20577. target = target.parentNode.previousSibling.firstChild;
  20578. }
  20579. }
  20580. //排除了非td内部以及用于代码高亮部分的td
  20581. return target && !(editor.fireEvent("excludetable", target) === true) ? target : null;
  20582. }
  20583. };
  20584. // plugins/table.sort.js
  20585. /**
  20586. * Created with JetBrains PhpStorm.
  20587. * User: Jinqn
  20588. * Date: 13-10-12
  20589. * Time: 上午10:20
  20590. * To change this template use File | Settings | File Templates.
  20591. */
  20592. UE.UETable.prototype.sortTable = function (sortByCellIndex, compareFn) {
  20593. var table = this.table,
  20594. rows = table.rows,
  20595. trArray = [],
  20596. flag = rows[0].cells[0].tagName === "TH",
  20597. lastRowIndex = 0;
  20598. if (this.selectedTds.length) {
  20599. var range = this.cellsRange,
  20600. len = range.endRowIndex + 1;
  20601. for (var i = range.beginRowIndex; i < len; i++) {
  20602. trArray[i] = rows[i];
  20603. }
  20604. trArray.splice(0, range.beginRowIndex);
  20605. lastRowIndex = (range.endRowIndex + 1) === this.rowsNum ? 0 : range.endRowIndex + 1;
  20606. } else {
  20607. for (var i = 0, len = rows.length; i < len; i++) {
  20608. trArray[i] = rows[i];
  20609. }
  20610. }
  20611. var Fn = {
  20612. 'reversecurrent': function (td1, td2) {
  20613. return 1;
  20614. },
  20615. 'orderbyasc': function (td1, td2) {
  20616. var value1 = td1.innerText || td1.textContent,
  20617. value2 = td2.innerText || td2.textContent;
  20618. return value1.localeCompare(value2);
  20619. },
  20620. 'reversebyasc': function (td1, td2) {
  20621. var value1 = td1.innerHTML,
  20622. value2 = td2.innerHTML;
  20623. return value2.localeCompare(value1);
  20624. },
  20625. 'orderbynum': function (td1, td2) {
  20626. var value1 = td1[browser.ie ? 'innerText' : 'textContent'].match(/\d+/),
  20627. value2 = td2[browser.ie ? 'innerText' : 'textContent'].match(/\d+/);
  20628. if (value1) value1 = +value1[0];
  20629. if (value2) value2 = +value2[0];
  20630. return (value1 || 0) - (value2 || 0);
  20631. },
  20632. 'reversebynum': function (td1, td2) {
  20633. var value1 = td1[browser.ie ? 'innerText' : 'textContent'].match(/\d+/),
  20634. value2 = td2[browser.ie ? 'innerText' : 'textContent'].match(/\d+/);
  20635. if (value1) value1 = +value1[0];
  20636. if (value2) value2 = +value2[0];
  20637. return (value2 || 0) - (value1 || 0);
  20638. }
  20639. };
  20640. //对表格设置排序的标记data-sort-type
  20641. table.setAttribute('data-sort-type', compareFn && typeof compareFn === "string" && Fn[compareFn] ? compareFn : '');
  20642. //th不参与排序
  20643. flag && trArray.splice(0, 1);
  20644. trArray = utils.sort(trArray, function (tr1, tr2) {
  20645. var result;
  20646. if (compareFn && typeof compareFn === "function") {
  20647. result = compareFn.call(this, tr1.cells[sortByCellIndex], tr2.cells[sortByCellIndex]);
  20648. } else if (compareFn && typeof compareFn === "number") {
  20649. result = 1;
  20650. } else if (compareFn && typeof compareFn === "string" && Fn[compareFn]) {
  20651. result = Fn[compareFn].call(this, tr1.cells[sortByCellIndex], tr2.cells[sortByCellIndex]);
  20652. } else {
  20653. result = Fn['orderbyasc'].call(this, tr1.cells[sortByCellIndex], tr2.cells[sortByCellIndex]);
  20654. }
  20655. return result;
  20656. });
  20657. var fragment = table.ownerDocument.createDocumentFragment();
  20658. for (var j = 0, len = trArray.length; j < len; j++) {
  20659. fragment.appendChild(trArray[j]);
  20660. }
  20661. var tbody = table.getElementsByTagName("tbody")[0];
  20662. if (!lastRowIndex) {
  20663. tbody.appendChild(fragment);
  20664. } else {
  20665. tbody.insertBefore(fragment, rows[lastRowIndex - range.endRowIndex + range.beginRowIndex - 1])
  20666. }
  20667. };
  20668. UE.plugins['tablesort'] = function () {
  20669. var me = this,
  20670. UT = UE.UETable,
  20671. getUETable = function (tdOrTable) {
  20672. return UT.getUETable(tdOrTable);
  20673. },
  20674. getTableItemsByRange = function (editor) {
  20675. return UT.getTableItemsByRange(editor);
  20676. };
  20677. me.ready(function () {
  20678. //添加表格可排序的样式
  20679. utils.cssRule('tablesort',
  20680. 'table.sortEnabled tr.firstRow th,table.sortEnabled tr.firstRow td{padding-right:20px;background-repeat: no-repeat;background-position: center right;' +
  20681. ' background-image:url(' + me.options.themePath + me.options.theme + '/images/sortable.png);}',
  20682. me.document);
  20683. //做单元格合并操作时,清除可排序标识
  20684. me.addListener("afterexeccommand", function (type, cmd) {
  20685. if (cmd == 'mergeright' || cmd == 'mergedown' || cmd == 'mergecells') {
  20686. this.execCommand('disablesort');
  20687. }
  20688. });
  20689. });
  20690. //表格排序
  20691. UE.commands['sorttable'] = {
  20692. queryCommandState: function () {
  20693. var me = this,
  20694. tableItems = getTableItemsByRange(me);
  20695. if (!tableItems.cell) return -1;
  20696. var table = tableItems.table,
  20697. cells = table.getElementsByTagName("td");
  20698. for (var i = 0, cell; cell = cells[i++];) {
  20699. if (cell.rowSpan != 1 || cell.colSpan != 1) return -1;
  20700. }
  20701. return 0;
  20702. },
  20703. execCommand: function (cmd, fn) {
  20704. var me = this,
  20705. range = me.selection.getRange(),
  20706. bk = range.createBookmark(true),
  20707. tableItems = getTableItemsByRange(me),
  20708. cell = tableItems.cell,
  20709. ut = getUETable(tableItems.table),
  20710. cellInfo = ut.getCellInfo(cell);
  20711. ut.sortTable(cellInfo.cellIndex, fn);
  20712. range.moveToBookmark(bk);
  20713. try {
  20714. range.select();
  20715. } catch (e) {}
  20716. }
  20717. };
  20718. //设置表格可排序,清除表格可排序
  20719. UE.commands["enablesort"] = UE.commands["disablesort"] = {
  20720. queryCommandState: function (cmd) {
  20721. var table = getTableItemsByRange(this).table;
  20722. if (table && cmd == 'enablesort') {
  20723. var cells = domUtils.getElementsByTagName(table, 'th td');
  20724. for (var i = 0; i < cells.length; i++) {
  20725. if (cells[i].getAttribute('colspan') > 1 || cells[i].getAttribute('rowspan') > 1) return -1;
  20726. }
  20727. }
  20728. return !table ? -1 : cmd == 'enablesort' ^ table.getAttribute('data-sort') != 'sortEnabled' ? -1 : 0;
  20729. },
  20730. execCommand: function (cmd) {
  20731. var table = getTableItemsByRange(this).table;
  20732. table.setAttribute("data-sort", cmd == "enablesort" ? "sortEnabled" : "sortDisabled");
  20733. cmd == "enablesort" ? domUtils.addClass(table, "sortEnabled") : domUtils.removeClasses(table, "sortEnabled");
  20734. }
  20735. };
  20736. };
  20737. // plugins/contextmenu.js
  20738. ///import core
  20739. ///commands 右键菜单
  20740. ///commandsName ContextMenu
  20741. ///commandsTitle 右键菜单
  20742. /**
  20743. * 右键菜单
  20744. * @function
  20745. * @name baidu.editor.plugins.contextmenu
  20746. * @author zhanyi
  20747. */
  20748. UE.plugins['contextmenu'] = function () {
  20749. var me = this;
  20750. me.setOpt('enableContextMenu', true);
  20751. if (me.getOpt('enableContextMenu') === false) {
  20752. return;
  20753. }
  20754. var lang = me.getLang("contextMenu"),
  20755. menu,
  20756. items = me.options.contextMenu || [{
  20757. label: lang['selectall'],
  20758. cmdName: 'selectall'
  20759. },
  20760. {
  20761. label: lang.cleardoc,
  20762. cmdName: 'cleardoc',
  20763. exec: function () {
  20764. if (confirm(lang.confirmclear)) {
  20765. this.execCommand('cleardoc');
  20766. }
  20767. }
  20768. },
  20769. '-',
  20770. {
  20771. label: lang.unlink,
  20772. cmdName: 'unlink'
  20773. },
  20774. '-',
  20775. {
  20776. group: lang.paragraph,
  20777. icon: 'justifyjustify',
  20778. subMenu: [{
  20779. label: lang.justifyleft,
  20780. cmdName: 'justify',
  20781. value: 'left'
  20782. },
  20783. {
  20784. label: lang.justifyright,
  20785. cmdName: 'justify',
  20786. value: 'right'
  20787. },
  20788. {
  20789. label: lang.justifycenter,
  20790. cmdName: 'justify',
  20791. value: 'center'
  20792. },
  20793. {
  20794. label: lang.justifyjustify,
  20795. cmdName: 'justify',
  20796. value: 'justify'
  20797. }
  20798. ]
  20799. },
  20800. '-',
  20801. {
  20802. group: lang.table,
  20803. icon: 'table',
  20804. subMenu: [{
  20805. label: lang.inserttable,
  20806. cmdName: 'inserttable'
  20807. },
  20808. {
  20809. label: lang.deletetable,
  20810. cmdName: 'deletetable'
  20811. },
  20812. '-',
  20813. {
  20814. label: lang.deleterow,
  20815. cmdName: 'deleterow'
  20816. },
  20817. {
  20818. label: lang.deletecol,
  20819. cmdName: 'deletecol'
  20820. },
  20821. {
  20822. label: lang.insertcol,
  20823. cmdName: 'insertcol'
  20824. },
  20825. {
  20826. label: lang.insertcolnext,
  20827. cmdName: 'insertcolnext'
  20828. },
  20829. {
  20830. label: lang.insertrow,
  20831. cmdName: 'insertrow'
  20832. },
  20833. {
  20834. label: lang.insertrownext,
  20835. cmdName: 'insertrownext'
  20836. },
  20837. '-',
  20838. {
  20839. label: lang.insertcaption,
  20840. cmdName: 'insertcaption'
  20841. },
  20842. {
  20843. label: lang.deletecaption,
  20844. cmdName: 'deletecaption'
  20845. },
  20846. {
  20847. label: lang.inserttitle,
  20848. cmdName: 'inserttitle'
  20849. },
  20850. {
  20851. label: lang.deletetitle,
  20852. cmdName: 'deletetitle'
  20853. },
  20854. {
  20855. label: lang.inserttitlecol,
  20856. cmdName: 'inserttitlecol'
  20857. },
  20858. {
  20859. label: lang.deletetitlecol,
  20860. cmdName: 'deletetitlecol'
  20861. },
  20862. '-',
  20863. {
  20864. label: lang.mergecells,
  20865. cmdName: 'mergecells'
  20866. },
  20867. {
  20868. label: lang.mergeright,
  20869. cmdName: 'mergeright'
  20870. },
  20871. {
  20872. label: lang.mergedown,
  20873. cmdName: 'mergedown'
  20874. },
  20875. '-',
  20876. {
  20877. label: lang.splittorows,
  20878. cmdName: 'splittorows'
  20879. },
  20880. {
  20881. label: lang.splittocols,
  20882. cmdName: 'splittocols'
  20883. },
  20884. {
  20885. label: lang.splittocells,
  20886. cmdName: 'splittocells'
  20887. },
  20888. '-',
  20889. {
  20890. label: lang.averageDiseRow,
  20891. cmdName: 'averagedistributerow'
  20892. },
  20893. {
  20894. label: lang.averageDisCol,
  20895. cmdName: 'averagedistributecol'
  20896. },
  20897. '-',
  20898. {
  20899. label: lang.edittd,
  20900. cmdName: 'edittd',
  20901. exec: function () {
  20902. if (UE.ui['edittd']) {
  20903. new UE.ui['edittd'](this);
  20904. }
  20905. this.getDialog('edittd').open();
  20906. }
  20907. },
  20908. {
  20909. label: lang.edittable,
  20910. cmdName: 'edittable',
  20911. exec: function () {
  20912. if (UE.ui['edittable']) {
  20913. new UE.ui['edittable'](this);
  20914. }
  20915. this.getDialog('edittable').open();
  20916. }
  20917. },
  20918. {
  20919. label: lang.setbordervisible,
  20920. cmdName: 'setbordervisible'
  20921. }
  20922. ]
  20923. },
  20924. {
  20925. group: lang.tablesort,
  20926. icon: 'tablesort',
  20927. subMenu: [{
  20928. label: lang.enablesort,
  20929. cmdName: 'enablesort'
  20930. },
  20931. {
  20932. label: lang.disablesort,
  20933. cmdName: 'disablesort'
  20934. },
  20935. '-',
  20936. {
  20937. label: lang.reversecurrent,
  20938. cmdName: 'sorttable',
  20939. value: 'reversecurrent'
  20940. },
  20941. {
  20942. label: lang.orderbyasc,
  20943. cmdName: 'sorttable',
  20944. value: 'orderbyasc'
  20945. },
  20946. {
  20947. label: lang.reversebyasc,
  20948. cmdName: 'sorttable',
  20949. value: 'reversebyasc'
  20950. },
  20951. {
  20952. label: lang.orderbynum,
  20953. cmdName: 'sorttable',
  20954. value: 'orderbynum'
  20955. },
  20956. {
  20957. label: lang.reversebynum,
  20958. cmdName: 'sorttable',
  20959. value: 'reversebynum'
  20960. }
  20961. ]
  20962. },
  20963. {
  20964. group: lang.borderbk,
  20965. icon: 'borderBack',
  20966. subMenu: [{
  20967. label: lang.setcolor,
  20968. cmdName: "interlacetable",
  20969. exec: function () {
  20970. this.execCommand("interlacetable");
  20971. }
  20972. },
  20973. {
  20974. label: lang.unsetcolor,
  20975. cmdName: "uninterlacetable",
  20976. exec: function () {
  20977. this.execCommand("uninterlacetable");
  20978. }
  20979. },
  20980. {
  20981. label: lang.setbackground,
  20982. cmdName: "settablebackground",
  20983. exec: function () {
  20984. this.execCommand("settablebackground", {
  20985. repeat: true,
  20986. colorList: ["#bbb", "#ccc"]
  20987. });
  20988. }
  20989. },
  20990. {
  20991. label: lang.unsetbackground,
  20992. cmdName: "cleartablebackground",
  20993. exec: function () {
  20994. this.execCommand("cleartablebackground");
  20995. }
  20996. },
  20997. {
  20998. label: lang.redandblue,
  20999. cmdName: "settablebackground",
  21000. exec: function () {
  21001. this.execCommand("settablebackground", {
  21002. repeat: true,
  21003. colorList: ["red", "blue"]
  21004. });
  21005. }
  21006. },
  21007. {
  21008. label: lang.threecolorgradient,
  21009. cmdName: "settablebackground",
  21010. exec: function () {
  21011. this.execCommand("settablebackground", {
  21012. repeat: true,
  21013. colorList: ["#aaa", "#bbb", "#ccc"]
  21014. });
  21015. }
  21016. }
  21017. ]
  21018. },
  21019. {
  21020. group: lang.aligntd,
  21021. icon: 'aligntd',
  21022. subMenu: [{
  21023. cmdName: 'cellalignment',
  21024. value: {
  21025. align: 'left',
  21026. vAlign: 'top'
  21027. }
  21028. },
  21029. {
  21030. cmdName: 'cellalignment',
  21031. value: {
  21032. align: 'center',
  21033. vAlign: 'top'
  21034. }
  21035. },
  21036. {
  21037. cmdName: 'cellalignment',
  21038. value: {
  21039. align: 'right',
  21040. vAlign: 'top'
  21041. }
  21042. },
  21043. {
  21044. cmdName: 'cellalignment',
  21045. value: {
  21046. align: 'left',
  21047. vAlign: 'middle'
  21048. }
  21049. },
  21050. {
  21051. cmdName: 'cellalignment',
  21052. value: {
  21053. align: 'center',
  21054. vAlign: 'middle'
  21055. }
  21056. },
  21057. {
  21058. cmdName: 'cellalignment',
  21059. value: {
  21060. align: 'right',
  21061. vAlign: 'middle'
  21062. }
  21063. },
  21064. {
  21065. cmdName: 'cellalignment',
  21066. value: {
  21067. align: 'left',
  21068. vAlign: 'bottom'
  21069. }
  21070. },
  21071. {
  21072. cmdName: 'cellalignment',
  21073. value: {
  21074. align: 'center',
  21075. vAlign: 'bottom'
  21076. }
  21077. },
  21078. {
  21079. cmdName: 'cellalignment',
  21080. value: {
  21081. align: 'right',
  21082. vAlign: 'bottom'
  21083. }
  21084. }
  21085. ]
  21086. },
  21087. {
  21088. group: lang.aligntable,
  21089. icon: 'aligntable',
  21090. subMenu: [{
  21091. cmdName: 'tablealignment',
  21092. className: 'left',
  21093. label: lang.tableleft,
  21094. value: "left"
  21095. },
  21096. {
  21097. cmdName: 'tablealignment',
  21098. className: 'center',
  21099. label: lang.tablecenter,
  21100. value: "center"
  21101. },
  21102. {
  21103. cmdName: 'tablealignment',
  21104. className: 'right',
  21105. label: lang.tableright,
  21106. value: "right"
  21107. }
  21108. ]
  21109. },
  21110. '-',
  21111. {
  21112. label: lang.insertparagraphbefore,
  21113. cmdName: 'insertparagraph',
  21114. value: true
  21115. },
  21116. {
  21117. label: lang.insertparagraphafter,
  21118. cmdName: 'insertparagraph'
  21119. },
  21120. {
  21121. label: lang['copy'],
  21122. cmdName: 'copy'
  21123. },
  21124. {
  21125. label: lang['paste'],
  21126. cmdName: 'paste'
  21127. }
  21128. ];
  21129. if (!items.length) {
  21130. return;
  21131. }
  21132. var uiUtils = UE.ui.uiUtils;
  21133. me.addListener('contextmenu', function (type, evt) {
  21134. var offset = uiUtils.getViewportOffsetByEvent(evt);
  21135. me.fireEvent('beforeselectionchange');
  21136. if (menu) {
  21137. menu.destroy();
  21138. }
  21139. for (var i = 0, ti, contextItems = []; ti = items[i]; i++) {
  21140. var last;
  21141. (function (item) {
  21142. if (item == '-') {
  21143. if ((last = contextItems[contextItems.length - 1]) && last !== '-') {
  21144. contextItems.push('-');
  21145. }
  21146. } else if (item.hasOwnProperty("group")) {
  21147. for (var j = 0, cj, subMenu = []; cj = item.subMenu[j]; j++) {
  21148. (function (subItem) {
  21149. if (subItem == '-') {
  21150. if ((last = subMenu[subMenu.length - 1]) && last !== '-') {
  21151. subMenu.push('-');
  21152. } else {
  21153. subMenu.splice(subMenu.length - 1);
  21154. }
  21155. } else {
  21156. if ((me.commands[subItem.cmdName] || UE.commands[subItem.cmdName] || subItem.query) &&
  21157. (subItem.query ? subItem.query() : me.queryCommandState(subItem.cmdName)) > -1) {
  21158. subMenu.push({
  21159. 'label': subItem.label || me.getLang("contextMenu." + subItem.cmdName + (subItem.value || '')) || "",
  21160. 'className': 'edui-for-' + subItem.cmdName + (subItem.className ? (' edui-for-' + subItem.cmdName + '-' + subItem.className) : ''),
  21161. onclick: subItem.exec ? function () {
  21162. subItem.exec.call(me);
  21163. } : function () {
  21164. me.execCommand(subItem.cmdName, subItem.value);
  21165. }
  21166. });
  21167. }
  21168. }
  21169. })(cj);
  21170. }
  21171. if (subMenu.length) {
  21172. function getLabel() {
  21173. switch (item.icon) {
  21174. case "table":
  21175. return me.getLang("contextMenu.table");
  21176. case "justifyjustify":
  21177. return me.getLang("contextMenu.paragraph");
  21178. case "aligntd":
  21179. return me.getLang("contextMenu.aligntd");
  21180. case "aligntable":
  21181. return me.getLang("contextMenu.aligntable");
  21182. case "tablesort":
  21183. return lang.tablesort;
  21184. case "borderBack":
  21185. return lang.borderbk;
  21186. default:
  21187. return '';
  21188. }
  21189. }
  21190. contextItems.push({
  21191. //todo 修正成自动获取方式
  21192. 'label': getLabel(),
  21193. className: 'edui-for-' + item.icon,
  21194. 'subMenu': {
  21195. items: subMenu,
  21196. editor: me
  21197. }
  21198. });
  21199. }
  21200. } else {
  21201. //有可能commmand没有加载右键不能出来,或者没有command也想能展示出来添加query方法
  21202. if ((me.commands[item.cmdName] || UE.commands[item.cmdName] || item.query) &&
  21203. (item.query ? item.query.call(me) : me.queryCommandState(item.cmdName)) > -1) {
  21204. contextItems.push({
  21205. 'label': item.label || me.getLang("contextMenu." + item.cmdName),
  21206. className: 'edui-for-' + (item.icon ? item.icon : item.cmdName + (item.value || '')),
  21207. onclick: item.exec ? function () {
  21208. item.exec.call(me);
  21209. } : function () {
  21210. me.execCommand(item.cmdName, item.value);
  21211. }
  21212. });
  21213. }
  21214. }
  21215. })(ti);
  21216. }
  21217. if (contextItems[contextItems.length - 1] == '-') {
  21218. contextItems.pop();
  21219. }
  21220. menu = new UE.ui.Menu({
  21221. items: contextItems,
  21222. className: "edui-contextmenu",
  21223. editor: me
  21224. });
  21225. menu.render();
  21226. menu.showAt(offset);
  21227. me.fireEvent("aftershowcontextmenu", menu);
  21228. domUtils.preventDefault(evt);
  21229. if (browser.ie) {
  21230. var ieRange;
  21231. try {
  21232. ieRange = me.selection.getNative().createRange();
  21233. } catch (e) {
  21234. return;
  21235. }
  21236. if (ieRange.item) {
  21237. var range = new dom.Range(me.document);
  21238. range.selectNode(ieRange.item(0)).select(true, true);
  21239. }
  21240. }
  21241. });
  21242. // 添加复制的flash按钮
  21243. me.addListener('aftershowcontextmenu', function (type, menu) {
  21244. if (me.zeroclipboard) {
  21245. var items = menu.items;
  21246. for (var key in items) {
  21247. if (items[key].className == 'edui-for-copy') {
  21248. me.zeroclipboard.clip(items[key].getDom());
  21249. }
  21250. }
  21251. }
  21252. });
  21253. };
  21254. // plugins/shortcutmenu.js
  21255. ///import core
  21256. ///commands 弹出菜单
  21257. // commandsName popupmenu
  21258. ///commandsTitle 弹出菜单
  21259. /**
  21260. * 弹出菜单
  21261. * @function
  21262. * @name baidu.editor.plugins.popupmenu
  21263. * @author xuheng
  21264. */
  21265. UE.plugins['shortcutmenu'] = function () {
  21266. var me = this,
  21267. menu,
  21268. items = me.options.shortcutMenu || [];
  21269. if (!items.length) {
  21270. return;
  21271. }
  21272. me.addListener('contextmenu mouseup', function (type, e) {
  21273. var me = this,
  21274. customEvt = {
  21275. type: type,
  21276. target: e.target || e.srcElement,
  21277. screenX: e.screenX,
  21278. screenY: e.screenY,
  21279. clientX: e.clientX,
  21280. clientY: e.clientY
  21281. };
  21282. setTimeout(function () {
  21283. var rng = me.selection.getRange();
  21284. if (rng.collapsed === false || type == "contextmenu") {
  21285. if (!menu) {
  21286. menu = new baidu.editor.ui.ShortCutMenu({
  21287. editor: me,
  21288. items: items,
  21289. theme: me.options.theme,
  21290. className: 'edui-shortcutmenu'
  21291. });
  21292. menu.render();
  21293. me.fireEvent("afterrendershortcutmenu", menu);
  21294. }
  21295. menu.show(customEvt, !!UE.plugins['contextmenu']);
  21296. }
  21297. });
  21298. if (type == 'contextmenu') {
  21299. domUtils.preventDefault(e);
  21300. if (browser.ie9below) {
  21301. var ieRange;
  21302. try {
  21303. ieRange = me.selection.getNative().createRange();
  21304. } catch (e) {
  21305. return;
  21306. }
  21307. if (ieRange.item) {
  21308. var range = new dom.Range(me.document);
  21309. range.selectNode(ieRange.item(0)).select(true, true);
  21310. }
  21311. }
  21312. }
  21313. });
  21314. me.addListener('keydown', function (type) {
  21315. if (type == "keydown") {
  21316. menu && !menu.isHidden && menu.hide();
  21317. }
  21318. });
  21319. };
  21320. // plugins/basestyle.js
  21321. /**
  21322. * B、I、sub、super命令支持
  21323. * @file
  21324. * @since 1.2.6.1
  21325. */
  21326. UE.plugins['basestyle'] = function () {
  21327. /**
  21328. * 字体加粗
  21329. * @command bold
  21330. * @param { String } cmd 命令字符串
  21331. * @remind 对已加粗的文本内容执行该命令, 将取消加粗
  21332. * @method execCommand
  21333. * @example
  21334. * ```javascript
  21335. * //editor是编辑器实例
  21336. * //对当前选中的文本内容执行加粗操作
  21337. * //第一次执行, 文本内容加粗
  21338. * editor.execCommand( 'bold' );
  21339. *
  21340. * //第二次执行, 文本内容取消加粗
  21341. * editor.execCommand( 'bold' );
  21342. * ```
  21343. */
  21344. /**
  21345. * 字体倾斜
  21346. * @command italic
  21347. * @method execCommand
  21348. * @param { String } cmd 命令字符串
  21349. * @remind 对已倾斜的文本内容执行该命令, 将取消倾斜
  21350. * @example
  21351. * ```javascript
  21352. * //editor是编辑器实例
  21353. * //对当前选中的文本内容执行斜体操作
  21354. * //第一次操作, 文本内容将变成斜体
  21355. * editor.execCommand( 'italic' );
  21356. *
  21357. * //再次对同一文本内容执行, 则文本内容将恢复正常
  21358. * editor.execCommand( 'italic' );
  21359. * ```
  21360. */
  21361. /**
  21362. * 下标文本,与“superscript”命令互斥
  21363. * @command subscript
  21364. * @method execCommand
  21365. * @remind 把选中的文本内容切换成下标文本, 如果当前选中的文本已经是下标, 则该操作会把文本内容还原成正常文本
  21366. * @param { String } cmd 命令字符串
  21367. * @example
  21368. * ```javascript
  21369. * //editor是编辑器实例
  21370. * //对当前选中的文本内容执行下标操作
  21371. * //第一次操作, 文本内容将变成下标文本
  21372. * editor.execCommand( 'subscript' );
  21373. *
  21374. * //再次对同一文本内容执行, 则文本内容将恢复正常
  21375. * editor.execCommand( 'subscript' );
  21376. * ```
  21377. */
  21378. /**
  21379. * 上标文本,与“subscript”命令互斥
  21380. * @command superscript
  21381. * @method execCommand
  21382. * @remind 把选中的文本内容切换成上标文本, 如果当前选中的文本已经是上标, 则该操作会把文本内容还原成正常文本
  21383. * @param { String } cmd 命令字符串
  21384. * @example
  21385. * ```javascript
  21386. * //editor是编辑器实例
  21387. * //对当前选中的文本内容执行上标操作
  21388. * //第一次操作, 文本内容将变成上标文本
  21389. * editor.execCommand( 'superscript' );
  21390. *
  21391. * //再次对同一文本内容执行, 则文本内容将恢复正常
  21392. * editor.execCommand( 'superscript' );
  21393. * ```
  21394. */
  21395. var basestyles = {
  21396. 'bold': ['strong', 'b'],
  21397. 'italic': ['em', 'i'],
  21398. 'subscript': ['sub'],
  21399. 'superscript': ['sup']
  21400. },
  21401. getObj = function (editor, tagNames) {
  21402. return domUtils.filterNodeList(editor.selection.getStartElementPath(), tagNames);
  21403. },
  21404. me = this;
  21405. //添加快捷键
  21406. me.addshortcutkey({
  21407. "Bold": "ctrl+66", //^B
  21408. "Italic": "ctrl+73", //^I
  21409. "Underline": "ctrl+85" //^U
  21410. });
  21411. me.addInputRule(function (root) {
  21412. utils.each(root.getNodesByTagName('b i'), function (node) {
  21413. switch (node.tagName) {
  21414. case 'b':
  21415. node.tagName = 'strong';
  21416. break;
  21417. case 'i':
  21418. node.tagName = 'em';
  21419. }
  21420. });
  21421. });
  21422. for (var style in basestyles) {
  21423. (function (cmd, tagNames) {
  21424. me.commands[cmd] = {
  21425. execCommand: function (cmdName) {
  21426. var range = me.selection.getRange(),
  21427. obj = getObj(this, tagNames);
  21428. if (range.collapsed) {
  21429. if (obj) {
  21430. var tmpText = me.document.createTextNode('');
  21431. range.insertNode(tmpText).removeInlineStyle(tagNames);
  21432. range.setStartBefore(tmpText);
  21433. domUtils.remove(tmpText);
  21434. } else {
  21435. var tmpNode = range.document.createElement(tagNames[0]);
  21436. if (cmdName == 'superscript' || cmdName == 'subscript') {
  21437. tmpText = me.document.createTextNode('');
  21438. range.insertNode(tmpText)
  21439. .removeInlineStyle(['sub', 'sup'])
  21440. .setStartBefore(tmpText)
  21441. .collapse(true);
  21442. }
  21443. range.insertNode(tmpNode).setStart(tmpNode, 0);
  21444. }
  21445. range.collapse(true);
  21446. } else {
  21447. if (cmdName == 'superscript' || cmdName == 'subscript') {
  21448. if (!obj || obj.tagName.toLowerCase() != cmdName) {
  21449. range.removeInlineStyle(['sub', 'sup']);
  21450. }
  21451. }
  21452. obj ? range.removeInlineStyle(tagNames) : range.applyInlineStyle(tagNames[0]);
  21453. }
  21454. range.select();
  21455. },
  21456. queryCommandState: function () {
  21457. return getObj(this, tagNames) ? 1 : 0;
  21458. }
  21459. };
  21460. })(style, basestyles[style]);
  21461. }
  21462. };
  21463. // plugins/elementpath.js
  21464. /**
  21465. * 选取路径命令
  21466. * @file
  21467. */
  21468. UE.plugins['elementpath'] = function () {
  21469. var currentLevel,
  21470. tagNames,
  21471. me = this;
  21472. me.setOpt('elementPathEnabled', true);
  21473. if (!me.options.elementPathEnabled) {
  21474. return;
  21475. }
  21476. me.commands['elementpath'] = {
  21477. execCommand: function (cmdName, level) {
  21478. var start = tagNames[level],
  21479. range = me.selection.getRange();
  21480. currentLevel = level * 1;
  21481. range.selectNode(start).select();
  21482. },
  21483. queryCommandValue: function () {
  21484. //产生一个副本,不能修改原来的startElementPath;
  21485. var parents = [].concat(this.selection.getStartElementPath()).reverse(),
  21486. names = [];
  21487. tagNames = parents;
  21488. for (var i = 0, ci; ci = parents[i]; i++) {
  21489. if (ci.nodeType == 3) {
  21490. continue;
  21491. }
  21492. var name = ci.tagName.toLowerCase();
  21493. if (name == 'img' && ci.getAttribute('anchorname')) {
  21494. name = 'anchor';
  21495. }
  21496. names[i] = name;
  21497. if (currentLevel == i) {
  21498. currentLevel = -1;
  21499. break;
  21500. }
  21501. }
  21502. return names;
  21503. }
  21504. };
  21505. };
  21506. // plugins/formatmatch.js
  21507. /**
  21508. * 格式刷,只格式inline的
  21509. * @file
  21510. * @since 1.2.6.1
  21511. */
  21512. /**
  21513. * 格式刷
  21514. * @command formatmatch
  21515. * @method execCommand
  21516. * @remind 该操作不能复制段落格式
  21517. * @param { String } cmd 命令字符串
  21518. * @example
  21519. * ```javascript
  21520. * //editor是编辑器实例
  21521. * //获取格式刷
  21522. * editor.execCommand( 'formatmatch' );
  21523. * ```
  21524. */
  21525. UE.plugins['formatmatch'] = function () {
  21526. var me = this,
  21527. list = [],
  21528. img,
  21529. flag = 0;
  21530. me.addListener('reset', function () {
  21531. list = [];
  21532. flag = 0;
  21533. });
  21534. function addList(type, evt) {
  21535. if (browser.webkit) {
  21536. var target = evt.target.tagName == 'IMG' ? evt.target : null;
  21537. }
  21538. function addFormat(range) {
  21539. if (text) {
  21540. range.selectNode(text);
  21541. }
  21542. return range.applyInlineStyle(list[list.length - 1].tagName, null, list);
  21543. }
  21544. me.undoManger && me.undoManger.save();
  21545. var range = me.selection.getRange(),
  21546. imgT = target || range.getClosedNode();
  21547. if (img && imgT && imgT.tagName == 'IMG') {
  21548. //trace:964
  21549. imgT.style.cssText += ';float:' + (img.style.cssFloat || img.style.styleFloat || 'none') + ';display:' + (img.style.display || 'inline');
  21550. img = null;
  21551. } else {
  21552. if (!img) {
  21553. var collapsed = range.collapsed;
  21554. if (collapsed) {
  21555. var text = me.document.createTextNode('match');
  21556. range.insertNode(text).select();
  21557. }
  21558. me.__hasEnterExecCommand = true;
  21559. //不能把block上的属性干掉
  21560. //trace:1553
  21561. var removeFormatAttributes = me.options.removeFormatAttributes;
  21562. me.options.removeFormatAttributes = '';
  21563. me.execCommand('removeformat');
  21564. me.options.removeFormatAttributes = removeFormatAttributes;
  21565. me.__hasEnterExecCommand = false;
  21566. //trace:969
  21567. range = me.selection.getRange();
  21568. if (list.length) {
  21569. addFormat(range);
  21570. }
  21571. if (text) {
  21572. range.setStartBefore(text).collapse(true);
  21573. }
  21574. range.select();
  21575. text && domUtils.remove(text);
  21576. }
  21577. }
  21578. me.undoManger && me.undoManger.save();
  21579. me.removeListener('mouseup', addList);
  21580. flag = 0;
  21581. }
  21582. me.commands['formatmatch'] = {
  21583. execCommand: function (cmdName) {
  21584. if (flag) {
  21585. flag = 0;
  21586. list = [];
  21587. me.removeListener('mouseup', addList);
  21588. return;
  21589. }
  21590. var range = me.selection.getRange();
  21591. img = range.getClosedNode();
  21592. if (!img || img.tagName != 'IMG') {
  21593. range.collapse(true).shrinkBoundary();
  21594. var start = range.startContainer;
  21595. list = domUtils.findParents(start, true, function (node) {
  21596. return !domUtils.isBlockElm(node) && node.nodeType == 1;
  21597. });
  21598. //a不能加入格式刷, 并且克隆节点
  21599. for (var i = 0, ci; ci = list[i]; i++) {
  21600. if (ci.tagName == 'A') {
  21601. list.splice(i, 1);
  21602. break;
  21603. }
  21604. }
  21605. }
  21606. me.addListener('mouseup', addList);
  21607. flag = 1;
  21608. },
  21609. queryCommandState: function () {
  21610. return flag;
  21611. },
  21612. notNeedUndo: 1
  21613. };
  21614. };
  21615. // plugins/searchreplace.js
  21616. ///import core
  21617. ///commands 查找替换
  21618. ///commandsName SearchReplace
  21619. ///commandsTitle 查询替换
  21620. ///commandsDialog dialogs\searchreplace
  21621. /**
  21622. * @description 查找替换
  21623. * @author zhanyi
  21624. */
  21625. UE.plugin.register('searchreplace', function () {
  21626. var me = this;
  21627. var _blockElm = {
  21628. 'table': 1,
  21629. 'tbody': 1,
  21630. 'tr': 1,
  21631. 'ol': 1,
  21632. 'ul': 1
  21633. };
  21634. function findTextInString(textContent, opt, currentIndex) {
  21635. var str = opt.searchStr;
  21636. if (opt.dir == -1) {
  21637. textContent = textContent.split('').reverse().join('');
  21638. str = str.split('').reverse().join('');
  21639. currentIndex = textContent.length - currentIndex;
  21640. }
  21641. var reg = new RegExp(str, 'g' + (opt.casesensitive ? '' : 'i')),
  21642. match;
  21643. while (match = reg.exec(textContent)) {
  21644. if (match.index >= currentIndex) {
  21645. return opt.dir == -1 ? textContent.length - match.index - opt.searchStr.length : match.index;
  21646. }
  21647. }
  21648. return -1
  21649. }
  21650. function findTextBlockElm(node, currentIndex, opt) {
  21651. var textContent, index, methodName = opt.all || opt.dir == 1 ? 'getNextDomNode' : 'getPreDomNode';
  21652. if (domUtils.isBody(node)) {
  21653. node = node.firstChild;
  21654. }
  21655. var first = 1;
  21656. while (node) {
  21657. textContent = node.nodeType == 3 ? node.nodeValue : node[browser.ie ? 'innerText' : 'textContent'];
  21658. index = findTextInString(textContent, opt, currentIndex);
  21659. first = 0;
  21660. if (index != -1) {
  21661. return {
  21662. 'node': node,
  21663. 'index': index
  21664. }
  21665. }
  21666. node = domUtils[methodName](node);
  21667. while (node && _blockElm[node.nodeName.toLowerCase()]) {
  21668. node = domUtils[methodName](node, true);
  21669. }
  21670. if (node) {
  21671. currentIndex = opt.dir == -1 ? (node.nodeType == 3 ? node.nodeValue : node[browser.ie ? 'innerText' : 'textContent']).length : 0;
  21672. }
  21673. }
  21674. }
  21675. function findNTextInBlockElm(node, index, str) {
  21676. var currentIndex = 0,
  21677. currentNode = node.firstChild,
  21678. currentNodeLength = 0,
  21679. result;
  21680. while (currentNode) {
  21681. if (currentNode.nodeType == 3) {
  21682. currentNodeLength = currentNode.nodeValue.replace(/(^[\t\r\n]+)|([\t\r\n]+$)/, '').length;
  21683. currentIndex += currentNodeLength;
  21684. if (currentIndex >= index) {
  21685. return {
  21686. 'node': currentNode,
  21687. 'index': currentNodeLength - (currentIndex - index)
  21688. }
  21689. }
  21690. } else if (!dtd.$empty[currentNode.tagName]) {
  21691. currentNodeLength = currentNode[browser.ie ? 'innerText' : 'textContent'].replace(/(^[\t\r\n]+)|([\t\r\n]+$)/, '').length
  21692. currentIndex += currentNodeLength;
  21693. if (currentIndex >= index) {
  21694. result = findNTextInBlockElm(currentNode, currentNodeLength - (currentIndex - index), str);
  21695. if (result) {
  21696. return result;
  21697. }
  21698. }
  21699. }
  21700. currentNode = domUtils.getNextDomNode(currentNode);
  21701. }
  21702. }
  21703. function searchReplace(me, opt) {
  21704. var rng = me.selection.getRange(),
  21705. startBlockNode,
  21706. searchStr = opt.searchStr,
  21707. span = me.document.createElement('span');
  21708. span.innerHTML = '$$ueditor_searchreplace_key$$';
  21709. rng.shrinkBoundary(true);
  21710. //判断是不是第一次选中
  21711. if (!rng.collapsed) {
  21712. rng.select();
  21713. var rngText = me.selection.getText();
  21714. if (new RegExp('^' + opt.searchStr + '$', (opt.casesensitive ? '' : 'i')).test(rngText)) {
  21715. if (opt.replaceStr != undefined) {
  21716. replaceText(rng, opt.replaceStr);
  21717. rng.select();
  21718. return true;
  21719. } else {
  21720. rng.collapse(opt.dir == -1)
  21721. }
  21722. }
  21723. }
  21724. rng.insertNode(span);
  21725. rng.enlargeToBlockElm(true);
  21726. startBlockNode = rng.startContainer;
  21727. var currentIndex = startBlockNode[browser.ie ? 'innerText' : 'textContent'].indexOf('$$ueditor_searchreplace_key$$');
  21728. rng.setStartBefore(span);
  21729. domUtils.remove(span);
  21730. var result = findTextBlockElm(startBlockNode, currentIndex, opt);
  21731. if (result) {
  21732. var rngStart = findNTextInBlockElm(result.node, result.index, searchStr);
  21733. var rngEnd = findNTextInBlockElm(result.node, result.index + searchStr.length, searchStr);
  21734. rng.setStart(rngStart.node, rngStart.index).setEnd(rngEnd.node, rngEnd.index);
  21735. if (opt.replaceStr !== undefined) {
  21736. replaceText(rng, opt.replaceStr)
  21737. }
  21738. rng.select();
  21739. return true;
  21740. } else {
  21741. rng.setCursor()
  21742. }
  21743. }
  21744. function replaceText(rng, str) {
  21745. str = me.document.createTextNode(str);
  21746. rng.deleteContents().insertNode(str);
  21747. }
  21748. return {
  21749. commands: {
  21750. 'searchreplace': {
  21751. execCommand: function (cmdName, opt) {
  21752. utils.extend(opt, {
  21753. all: false,
  21754. casesensitive: false,
  21755. dir: 1
  21756. }, true);
  21757. var num = 0;
  21758. if (opt.all) {
  21759. var rng = me.selection.getRange(),
  21760. first = me.body.firstChild;
  21761. if (first && first.nodeType == 1) {
  21762. rng.setStart(first, 0);
  21763. rng.shrinkBoundary(true);
  21764. } else if (first.nodeType == 3) {
  21765. rng.setStartBefore(first)
  21766. }
  21767. rng.collapse(true).select(true);
  21768. if (opt.replaceStr !== undefined) {
  21769. me.fireEvent('saveScene');
  21770. }
  21771. while (searchReplace(this, opt)) {
  21772. num++;
  21773. }
  21774. if (num) {
  21775. me.fireEvent('saveScene');
  21776. }
  21777. } else {
  21778. if (opt.replaceStr !== undefined) {
  21779. me.fireEvent('saveScene');
  21780. }
  21781. if (searchReplace(this, opt)) {
  21782. num++
  21783. }
  21784. if (num) {
  21785. me.fireEvent('saveScene');
  21786. }
  21787. }
  21788. return num;
  21789. },
  21790. notNeedUndo: 1
  21791. }
  21792. }
  21793. }
  21794. });
  21795. // plugins/customstyle.js
  21796. /**
  21797. * 自定义样式
  21798. * @file
  21799. * @since 1.2.6.1
  21800. */
  21801. /**
  21802. * 根据config配置文件里“customstyle”选项的值对匹配的标签执行样式替换。
  21803. * @command customstyle
  21804. * @method execCommand
  21805. * @param { String } cmd 命令字符串
  21806. * @example
  21807. * ```javascript
  21808. * editor.execCommand( 'customstyle' );
  21809. * ```
  21810. */
  21811. UE.plugins['customstyle'] = function () {
  21812. var me = this;
  21813. me.setOpt({
  21814. 'customstyle': [{
  21815. tag: 'h1',
  21816. name: 'tc',
  21817. 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;'
  21818. },
  21819. {
  21820. tag: 'h1',
  21821. name: 'tl',
  21822. 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;'
  21823. },
  21824. {
  21825. tag: 'span',
  21826. name: 'im',
  21827. style: 'font-size:16px;font-style:italic;font-weight:bold;line-height:18px;'
  21828. },
  21829. {
  21830. tag: 'span',
  21831. name: 'hi',
  21832. style: 'font-size:16px;font-style:italic;font-weight:bold;color:rgb(51, 153, 204);line-height:18px;'
  21833. }
  21834. ]
  21835. });
  21836. me.commands['customstyle'] = {
  21837. execCommand: function (cmdName, obj) {
  21838. var me = this,
  21839. tagName = obj.tag,
  21840. node = domUtils.findParent(me.selection.getStart(), function (node) {
  21841. return node.getAttribute('label');
  21842. }, true),
  21843. range, bk, tmpObj = {};
  21844. for (var p in obj) {
  21845. if (obj[p] !== undefined)
  21846. tmpObj[p] = obj[p];
  21847. }
  21848. delete tmpObj.tag;
  21849. if (node && node.getAttribute('label') == obj.label) {
  21850. range = this.selection.getRange();
  21851. bk = range.createBookmark();
  21852. if (range.collapsed) {
  21853. //trace:1732 删掉自定义标签,要有p来回填站位
  21854. if (dtd.$block[node.tagName]) {
  21855. var fillNode = me.document.createElement('p');
  21856. domUtils.moveChild(node, fillNode);
  21857. node.parentNode.insertBefore(fillNode, node);
  21858. domUtils.remove(node);
  21859. } else {
  21860. domUtils.remove(node, true);
  21861. }
  21862. } else {
  21863. var common = domUtils.getCommonAncestor(bk.start, bk.end),
  21864. nodes = domUtils.getElementsByTagName(common, tagName);
  21865. if (new RegExp(tagName, 'i').test(common.tagName)) {
  21866. nodes.push(common);
  21867. }
  21868. for (var i = 0, ni; ni = nodes[i++];) {
  21869. if (ni.getAttribute('label') == obj.label) {
  21870. var ps = domUtils.getPosition(ni, bk.start),
  21871. pe = domUtils.getPosition(ni, bk.end);
  21872. if ((ps & domUtils.POSITION_FOLLOWING || ps & domUtils.POSITION_CONTAINS) &&
  21873. (pe & domUtils.POSITION_PRECEDING || pe & domUtils.POSITION_CONTAINS)
  21874. )
  21875. if (dtd.$block[tagName]) {
  21876. var fillNode = me.document.createElement('p');
  21877. domUtils.moveChild(ni, fillNode);
  21878. ni.parentNode.insertBefore(fillNode, ni);
  21879. }
  21880. domUtils.remove(ni, true);
  21881. }
  21882. }
  21883. node = domUtils.findParent(common, function (node) {
  21884. return node.getAttribute('label') == obj.label;
  21885. }, true);
  21886. if (node) {
  21887. domUtils.remove(node, true);
  21888. }
  21889. }
  21890. range.moveToBookmark(bk).select();
  21891. } else {
  21892. if (dtd.$block[tagName]) {
  21893. this.execCommand('paragraph', tagName, tmpObj, 'customstyle');
  21894. range = me.selection.getRange();
  21895. if (!range.collapsed) {
  21896. range.collapse();
  21897. node = domUtils.findParent(me.selection.getStart(), function (node) {
  21898. return node.getAttribute('label') == obj.label;
  21899. }, true);
  21900. var pNode = me.document.createElement('p');
  21901. domUtils.insertAfter(node, pNode);
  21902. domUtils.fillNode(me.document, pNode);
  21903. range.setStart(pNode, 0).setCursor();
  21904. }
  21905. } else {
  21906. range = me.selection.getRange();
  21907. if (range.collapsed) {
  21908. node = me.document.createElement(tagName);
  21909. domUtils.setAttributes(node, tmpObj);
  21910. range.insertNode(node).setStart(node, 0).setCursor();
  21911. return;
  21912. }
  21913. bk = range.createBookmark();
  21914. range.applyInlineStyle(tagName, tmpObj).moveToBookmark(bk).select();
  21915. }
  21916. }
  21917. },
  21918. queryCommandValue: function () {
  21919. var parent = domUtils.filterNodeList(
  21920. this.selection.getStartElementPath(),
  21921. function (node) {
  21922. return node.getAttribute('label')
  21923. }
  21924. );
  21925. return parent ? parent.getAttribute('label') : '';
  21926. }
  21927. };
  21928. //当去掉customstyle是,如果是块元素,用p代替
  21929. me.addListener('keyup', function (type, evt) {
  21930. var keyCode = evt.keyCode || evt.which;
  21931. if (keyCode == 32 || keyCode == 13) {
  21932. var range = me.selection.getRange();
  21933. if (range.collapsed) {
  21934. var node = domUtils.findParent(me.selection.getStart(), function (node) {
  21935. return node.getAttribute('label');
  21936. }, true);
  21937. if (node && dtd.$block[node.tagName] && domUtils.isEmptyNode(node)) {
  21938. var p = me.document.createElement('p');
  21939. domUtils.insertAfter(node, p);
  21940. domUtils.fillNode(me.document, p);
  21941. domUtils.remove(node);
  21942. range.setStart(p, 0).setCursor();
  21943. }
  21944. }
  21945. }
  21946. });
  21947. };
  21948. // plugins/catchremoteimage.js
  21949. ///import core
  21950. ///commands 远程图片抓取
  21951. ///commandsName catchRemoteImage,catchremoteimageenable
  21952. ///commandsTitle 远程图片抓取
  21953. /**
  21954. * 远程图片抓取,当开启本插件时所有不符合本地域名的图片都将被抓取成为本地服务器上的图片
  21955. */
  21956. UE.plugins['catchremoteimage'] = function () {
  21957. var me = this,
  21958. ajax = UE.ajax;
  21959. /* 设置默认值 */
  21960. if (me.options.catchRemoteImageEnable === false) return;
  21961. me.setOpt({
  21962. catchRemoteImageEnable: false
  21963. });
  21964. me.addListener("afterpaste", function () {
  21965. me.fireEvent("catchRemoteImage");
  21966. });
  21967. me.addListener("catchRemoteImage", function () {
  21968. var catcherLocalDomain = me.getOpt('catcherLocalDomain'),
  21969. catcherActionUrl = me.getActionUrl(me.getOpt('catcherActionName')),
  21970. catcherUrlPrefix = me.getOpt('catcherUrlPrefix'),
  21971. catcherFieldName = me.getOpt('catcherFieldName');
  21972. var remoteImages = [],
  21973. imgs = domUtils.getElementsByTagName(me.document, "img"),
  21974. test = function (src, urls) {
  21975. if (src.indexOf(location.host) != -1 || /(^\.)|(^\/)/.test(src)) {
  21976. return true;
  21977. }
  21978. if (urls) {
  21979. for (var j = 0, url; url = urls[j++];) {
  21980. if (src.indexOf(url) !== -1) {
  21981. return true;
  21982. }
  21983. }
  21984. }
  21985. return false;
  21986. };
  21987. for (var i = 0, ci; ci = imgs[i++];) {
  21988. if (ci.getAttribute("word_img")) {
  21989. continue;
  21990. }
  21991. var src = ci.getAttribute("_src") || ci.src || "";
  21992. if (/^(https?|ftp):/i.test(src) && !test(src, catcherLocalDomain)) {
  21993. remoteImages.push(src);
  21994. }
  21995. }
  21996. if (remoteImages.length) {
  21997. catchremoteimage(remoteImages, {
  21998. //成功抓取
  21999. success: function (r) {
  22000. try {
  22001. var info = r.state !== undefined ? r : eval("(" + r.responseText + ")");
  22002. } catch (e) {
  22003. return;
  22004. }
  22005. /* 获取源路径和新路径 */
  22006. var i, j, ci, cj, oldSrc, newSrc, list = info.list;
  22007. for (i = 0; ci = imgs[i++];) {
  22008. oldSrc = ci.getAttribute("_src") || ci.src || "";
  22009. for (j = 0; cj = list[j++];) {
  22010. if (oldSrc == cj.source && cj.state == "SUCCESS") { //抓取失败时不做替换处理
  22011. newSrc = catcherUrlPrefix + cj.url;
  22012. domUtils.setAttributes(ci, {
  22013. "src": newSrc,
  22014. "_src": newSrc
  22015. });
  22016. break;
  22017. }
  22018. }
  22019. }
  22020. me.fireEvent('catchremotesuccess')
  22021. },
  22022. //回调失败,本次请求超时
  22023. error: function () {
  22024. me.fireEvent("catchremoteerror");
  22025. }
  22026. });
  22027. }
  22028. function catchremoteimage(imgs, callbacks) {
  22029. var params = utils.serializeParam(me.queryCommandValue('serverparam')) || '',
  22030. url = utils.formatUrl(catcherActionUrl + (catcherActionUrl.indexOf('?') == -1 ? '?' : '&') + params),
  22031. isJsonp = utils.isCrossDomainUrl(url),
  22032. opt = {
  22033. 'method': 'POST',
  22034. 'dataType': isJsonp ? 'jsonp' : '',
  22035. 'timeout': 60000, //单位:毫秒,回调请求超时设置。目标用户如果网速不是很快的话此处建议设置一个较大的数值
  22036. 'onsuccess': callbacks["success"],
  22037. 'onerror': callbacks["error"]
  22038. };
  22039. opt[catcherFieldName] = imgs;
  22040. ajax.request(url, opt);
  22041. }
  22042. });
  22043. };
  22044. // plugins/snapscreen.js
  22045. /**
  22046. * 截屏插件,为UEditor提供插入支持
  22047. * @file
  22048. * @since 1.4.2
  22049. */
  22050. UE.plugin.register('snapscreen', function () {
  22051. var me = this;
  22052. var snapplugin;
  22053. function getLocation(url) {
  22054. var search,
  22055. a = document.createElement('a'),
  22056. params = utils.serializeParam(me.queryCommandValue('serverparam')) || '';
  22057. a.href = url;
  22058. if (browser.ie) {
  22059. a.href = a.href;
  22060. }
  22061. search = a.search;
  22062. if (params) {
  22063. search = search + (search.indexOf('?') == -1 ? '?' : '&') + params;
  22064. search = search.replace(/[&]+/ig, '&');
  22065. }
  22066. return {
  22067. 'port': a.port,
  22068. 'hostname': a.hostname,
  22069. 'path': a.pathname + search || +a.hash
  22070. }
  22071. }
  22072. return {
  22073. commands: {
  22074. /**
  22075. * 字体背景颜色
  22076. * @command snapscreen
  22077. * @method execCommand
  22078. * @param { String } cmd 命令字符串
  22079. * @example
  22080. * ```javascript
  22081. * editor.execCommand('snapscreen');
  22082. * ```
  22083. */
  22084. 'snapscreen': {
  22085. execCommand: function (cmd) {
  22086. var url, local, res;
  22087. var lang = me.getLang("snapScreen_plugin");
  22088. if (!snapplugin) {
  22089. var container = me.container;
  22090. var doc = me.container.ownerDocument || me.container.document;
  22091. snapplugin = doc.createElement("object");
  22092. try {
  22093. snapplugin.type = "application/x-pluginbaidusnap";
  22094. } catch (e) {
  22095. return;
  22096. }
  22097. snapplugin.style.cssText = "position:absolute;left:-9999px;width:0;height:0;";
  22098. snapplugin.setAttribute("width", "0");
  22099. snapplugin.setAttribute("height", "0");
  22100. container.appendChild(snapplugin);
  22101. }
  22102. function onSuccess(rs) {
  22103. try {
  22104. rs = eval("(" + rs + ")");
  22105. if (rs.state == 'SUCCESS') {
  22106. var opt = me.options;
  22107. me.execCommand('insertimage', {
  22108. src: opt.snapscreenUrlPrefix + rs.url,
  22109. _src: opt.snapscreenUrlPrefix + rs.url,
  22110. alt: rs.title || '',
  22111. floatStyle: opt.snapscreenImgAlign
  22112. });
  22113. } else {
  22114. alert(rs.state);
  22115. }
  22116. } catch (e) {
  22117. alert(lang.callBackErrorMsg);
  22118. }
  22119. }
  22120. url = me.getActionUrl(me.getOpt('snapscreenActionName'));
  22121. local = getLocation(url);
  22122. setTimeout(function () {
  22123. try {
  22124. res = snapplugin.saveSnapshot(local.hostname, local.path, local.port);
  22125. } catch (e) {
  22126. me.ui._dialogs['snapscreenDialog'].open();
  22127. return;
  22128. }
  22129. onSuccess(res);
  22130. }, 50);
  22131. },
  22132. queryCommandState: function () {
  22133. return (navigator.userAgent.indexOf("Windows", 0) != -1) ? 0 : -1;
  22134. }
  22135. }
  22136. }
  22137. }
  22138. });
  22139. // plugins/insertparagraph.js
  22140. /**
  22141. * 插入段落
  22142. * @file
  22143. * @since 1.2.6.1
  22144. */
  22145. /**
  22146. * 插入段落
  22147. * @command insertparagraph
  22148. * @method execCommand
  22149. * @param { String } cmd 命令字符串
  22150. * @example
  22151. * ```javascript
  22152. * //editor是编辑器实例
  22153. * editor.execCommand( 'insertparagraph' );
  22154. * ```
  22155. */
  22156. UE.commands['insertparagraph'] = {
  22157. execCommand: function (cmdName, front) {
  22158. var me = this,
  22159. range = me.selection.getRange(),
  22160. start = range.startContainer,
  22161. tmpNode;
  22162. while (start) {
  22163. if (domUtils.isBody(start)) {
  22164. break;
  22165. }
  22166. tmpNode = start;
  22167. start = start.parentNode;
  22168. }
  22169. if (tmpNode) {
  22170. var p = me.document.createElement('p');
  22171. if (front) {
  22172. tmpNode.parentNode.insertBefore(p, tmpNode)
  22173. } else {
  22174. tmpNode.parentNode.insertBefore(p, tmpNode.nextSibling)
  22175. }
  22176. domUtils.fillNode(me.document, p);
  22177. range.setStart(p, 0).setCursor(false, true);
  22178. }
  22179. }
  22180. };
  22181. // plugins/webapp.js
  22182. /**
  22183. * 百度应用
  22184. * @file
  22185. * @since 1.2.6.1
  22186. */
  22187. /**
  22188. * 插入百度应用
  22189. * @command webapp
  22190. * @method execCommand
  22191. * @remind 需要百度APPKey
  22192. * @remind 百度应用主页: <a href="http://app.baidu.com/" target="_blank">http://app.baidu.com/</a>
  22193. * @param { Object } appOptions 应用所需的参数项, 支持的key有: title=>应用标题, width=>应用容器宽度,
  22194. * height=>应用容器高度,logo=>应用logo,url=>应用地址
  22195. * @example
  22196. * ```javascript
  22197. * //editor是编辑器实例
  22198. * //在编辑器里插入一个“植物大战僵尸”的APP
  22199. * editor.execCommand( 'webapp' , {
  22200. * title: '植物大战僵尸',
  22201. * width: 560,
  22202. * height: 465,
  22203. * logo: '应用展示的图片',
  22204. * url: '百度应用的地址'
  22205. * } );
  22206. * ```
  22207. */
  22208. //UE.plugins['webapp'] = function () {
  22209. // var me = this;
  22210. // function createInsertStr( obj, toIframe, addParagraph ) {
  22211. // return !toIframe ?
  22212. // (addParagraph ? '<p>' : '') + '<img title="'+obj.title+'" width="' + obj.width + '" height="' + obj.height + '"' +
  22213. // ' 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 + '" />' +
  22214. // (addParagraph ? '</p>' : '')
  22215. // :
  22216. // '<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>';
  22217. // }
  22218. //
  22219. // function switchImgAndIframe( img2frame ) {
  22220. // var tmpdiv,
  22221. // nodes = domUtils.getElementsByTagName( me.document, !img2frame ? "iframe" : "img" );
  22222. // for ( var i = 0, node; node = nodes[i++]; ) {
  22223. // if ( node.className != "edui-faked-webapp" ){
  22224. // continue;
  22225. // }
  22226. // tmpdiv = me.document.createElement( "div" );
  22227. // 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 );
  22228. // node.parentNode.replaceChild( tmpdiv.firstChild, node );
  22229. // }
  22230. // }
  22231. //
  22232. // me.addListener( "beforegetcontent", function () {
  22233. // switchImgAndIframe( true );
  22234. // } );
  22235. // me.addListener( 'aftersetcontent', function () {
  22236. // switchImgAndIframe( false );
  22237. // } );
  22238. // me.addListener( 'aftergetcontent', function ( cmdName ) {
  22239. // if ( cmdName == 'aftergetcontent' && me.queryCommandState( 'source' ) ){
  22240. // return;
  22241. // }
  22242. // switchImgAndIframe( false );
  22243. // } );
  22244. //
  22245. // me.commands['webapp'] = {
  22246. // execCommand:function ( cmd, obj ) {
  22247. // me.execCommand( "inserthtml", createInsertStr( obj, false,true ) );
  22248. // }
  22249. // };
  22250. //};
  22251. UE.plugin.register('webapp', function () {
  22252. var me = this;
  22253. function createInsertStr(obj, toEmbed) {
  22254. return !toEmbed ?
  22255. '<img title="' + obj.title + '" width="' + obj.width + '" height="' + obj.height + '"' +
  22256. ' src="' + me.options.UEDITOR_HOME_URL + 'themes/default/images/spacer.gif" _logo_url="' + obj.logo + '" style="background:url(' + obj.logo +
  22257. ') no-repeat center center; border:1px solid gray;" class="edui-faked-webapp" _url="' + obj.url + '" ' +
  22258. (obj.align && !obj.cssfloat ? 'align="' + obj.align + '"' : '') +
  22259. (obj.cssfloat ? 'style="float:' + obj.cssfloat + '"' : '') +
  22260. '/>' :
  22261. '<iframe class="edui-faked-webapp" title="' + obj.title + '" ' +
  22262. (obj.align && !obj.cssfloat ? 'align="' + obj.align + '"' : '') +
  22263. (obj.cssfloat ? 'style="float:' + obj.cssfloat + '"' : '') +
  22264. 'width="' + obj.width + '" height="' + obj.height + '" scrolling="no" frameborder="0" src="' + obj.url + '" logo_url = "' + obj.logo + '"></iframe>'
  22265. }
  22266. return {
  22267. outputRule: function (root) {
  22268. utils.each(root.getNodesByTagName('img'), function (node) {
  22269. var html;
  22270. if (node.getAttr('class') == 'edui-faked-webapp') {
  22271. html = createInsertStr({
  22272. title: node.getAttr('title'),
  22273. 'width': node.getAttr('width'),
  22274. 'height': node.getAttr('height'),
  22275. 'align': node.getAttr('align'),
  22276. 'cssfloat': node.getStyle('float'),
  22277. 'url': node.getAttr("_url"),
  22278. 'logo': node.getAttr('_logo_url')
  22279. }, true);
  22280. var embed = UE.uNode.createElement(html);
  22281. node.parentNode.replaceChild(embed, node);
  22282. }
  22283. })
  22284. },
  22285. inputRule: function (root) {
  22286. utils.each(root.getNodesByTagName('iframe'), function (node) {
  22287. if (node.getAttr('class') == 'edui-faked-webapp') {
  22288. var img = UE.uNode.createElement(createInsertStr({
  22289. title: node.getAttr('title'),
  22290. 'width': node.getAttr('width'),
  22291. 'height': node.getAttr('height'),
  22292. 'align': node.getAttr('align'),
  22293. 'cssfloat': node.getStyle('float'),
  22294. 'url': node.getAttr("src"),
  22295. 'logo': node.getAttr('logo_url')
  22296. }));
  22297. node.parentNode.replaceChild(img, node);
  22298. }
  22299. })
  22300. },
  22301. commands: {
  22302. /**
  22303. * 插入百度应用
  22304. * @command webapp
  22305. * @method execCommand
  22306. * @remind 需要百度APPKey
  22307. * @remind 百度应用主页: <a href="http://app.baidu.com/" target="_blank">http://app.baidu.com/</a>
  22308. * @param { Object } appOptions 应用所需的参数项, 支持的key有: title=>应用标题, width=>应用容器宽度,
  22309. * height=>应用容器高度,logo=>应用logo,url=>应用地址
  22310. * @example
  22311. * ```javascript
  22312. * //editor是编辑器实例
  22313. * //在编辑器里插入一个“植物大战僵尸”的APP
  22314. * editor.execCommand( 'webapp' , {
  22315. * title: '植物大战僵尸',
  22316. * width: 560,
  22317. * height: 465,
  22318. * logo: '应用展示的图片',
  22319. * url: '百度应用的地址'
  22320. * } );
  22321. * ```
  22322. */
  22323. 'webapp': {
  22324. execCommand: function (cmd, obj) {
  22325. var me = this,
  22326. str = createInsertStr(utils.extend(obj, {
  22327. align: 'none'
  22328. }), false);
  22329. me.execCommand("inserthtml", str);
  22330. },
  22331. queryCommandState: function () {
  22332. var me = this,
  22333. img = me.selection.getRange().getClosedNode(),
  22334. flag = img && (img.className == "edui-faked-webapp");
  22335. return flag ? 1 : 0;
  22336. }
  22337. }
  22338. }
  22339. }
  22340. });
  22341. // plugins/template.js
  22342. ///import core
  22343. ///import plugins\inserthtml.js
  22344. ///import plugins\cleardoc.js
  22345. ///commands 模板
  22346. ///commandsName template
  22347. ///commandsTitle 模板
  22348. ///commandsDialog dialogs\template
  22349. UE.plugins['template'] = function () {
  22350. UE.commands['template'] = {
  22351. execCommand: function (cmd, obj) {
  22352. obj.html && this.execCommand("inserthtml", obj.html);
  22353. }
  22354. };
  22355. this.addListener("click", function (type, evt) {
  22356. var el = evt.target || evt.srcElement,
  22357. range = this.selection.getRange();
  22358. var tnode = domUtils.findParent(el, function (node) {
  22359. if (node.className && domUtils.hasClass(node, "ue_t")) {
  22360. return node;
  22361. }
  22362. }, true);
  22363. tnode && range.selectNode(tnode).shrinkBoundary().select();
  22364. });
  22365. this.addListener("keydown", function (type, evt) {
  22366. var range = this.selection.getRange();
  22367. if (!range.collapsed) {
  22368. if (!evt.ctrlKey && !evt.metaKey && !evt.shiftKey && !evt.altKey) {
  22369. var tnode = domUtils.findParent(range.startContainer, function (node) {
  22370. if (node.className && domUtils.hasClass(node, "ue_t")) {
  22371. return node;
  22372. }
  22373. }, true);
  22374. if (tnode) {
  22375. domUtils.removeClasses(tnode, ["ue_t"]);
  22376. }
  22377. }
  22378. }
  22379. });
  22380. };
  22381. // plugins/music.js
  22382. /**
  22383. * 插入音乐命令
  22384. * @file
  22385. */
  22386. UE.plugin.register('music', function () {
  22387. var me = this;
  22388. function creatInsertStr(url, width, height, align, cssfloat, toEmbed) {
  22389. return !toEmbed ?
  22390. '<img ' +
  22391. (align && !cssfloat ? 'align="' + align + '"' : '') +
  22392. (cssfloat ? 'style="float:' + cssfloat + '"' : '') +
  22393. ' width="' + width + '" height="' + height + '" _url="' + url + '" class="edui-faked-music"' +
  22394. ' src="' + me.options.langPath + me.options.lang + '/images/music.png" />' :
  22395. '<embed type="application/x-shockwave-flash" class="edui-faked-music" pluginspage="http://www.macromedia.com/go/getflashplayer"' +
  22396. ' src="' + url + '" width="' + width + '" height="' + height + '" ' + (align && !cssfloat ? 'align="' + align + '"' : '') +
  22397. (cssfloat ? 'style="float:' + cssfloat + '"' : '') +
  22398. ' wmode="transparent" play="true" loop="false" menu="false" allowscriptaccess="never" allowfullscreen="true" >';
  22399. }
  22400. return {
  22401. outputRule: function (root) {
  22402. utils.each(root.getNodesByTagName('img'), function (node) {
  22403. var html;
  22404. if (node.getAttr('class') == 'edui-faked-music') {
  22405. var cssfloat = node.getStyle('float');
  22406. var align = node.getAttr('align');
  22407. html = creatInsertStr(node.getAttr("_url"), node.getAttr('width'), node.getAttr('height'), align, cssfloat, true);
  22408. var embed = UE.uNode.createElement(html);
  22409. node.parentNode.replaceChild(embed, node);
  22410. }
  22411. })
  22412. },
  22413. inputRule: function (root) {
  22414. utils.each(root.getNodesByTagName('embed'), function (node) {
  22415. if (node.getAttr('class') == 'edui-faked-music') {
  22416. var cssfloat = node.getStyle('float');
  22417. var align = node.getAttr('align');
  22418. html = creatInsertStr(node.getAttr("src"), node.getAttr('width'), node.getAttr('height'), align, cssfloat, false);
  22419. var img = UE.uNode.createElement(html);
  22420. node.parentNode.replaceChild(img, node);
  22421. }
  22422. })
  22423. },
  22424. commands: {
  22425. /**
  22426. * 插入音乐
  22427. * @command music
  22428. * @method execCommand
  22429. * @param { Object } musicOptions 插入音乐的参数项, 支持的key有: url=>音乐地址;
  22430. * width=>音乐容器宽度;height=>音乐容器高度;align=>音乐文件的对齐方式, 可选值有: left, center, right, none
  22431. * @example
  22432. * ```javascript
  22433. * //editor是编辑器实例
  22434. * //在编辑器里插入一个“植物大战僵尸”的APP
  22435. * editor.execCommand( 'music' , {
  22436. * width: 400,
  22437. * height: 95,
  22438. * align: "center",
  22439. * url: "音乐地址"
  22440. * } );
  22441. * ```
  22442. */
  22443. 'music': {
  22444. execCommand: function (cmd, musicObj) {
  22445. var me = this,
  22446. str = creatInsertStr(musicObj.url, musicObj.width || 400, musicObj.height || 95, "none", false);
  22447. me.execCommand("inserthtml", str);
  22448. },
  22449. queryCommandState: function () {
  22450. var me = this,
  22451. img = me.selection.getRange().getClosedNode(),
  22452. flag = img && (img.className == "edui-faked-music");
  22453. return flag ? 1 : 0;
  22454. }
  22455. }
  22456. }
  22457. }
  22458. });
  22459. // plugins/autoupload.js
  22460. /**
  22461. * @description
  22462. * 1.拖放文件到编辑区域,自动上传并插入到选区
  22463. * 2.插入粘贴板的图片,自动上传并插入到选区
  22464. * @author Jinqn
  22465. * @date 2013-10-14
  22466. */
  22467. UE.plugin.register('autoupload', function () {
  22468. function sendAndInsertFile(file, editor) {
  22469. var me = editor;
  22470. //模拟数据
  22471. var fieldName, urlPrefix, maxSize, allowFiles, actionUrl,
  22472. loadingHtml, errorHandler, successHandler,
  22473. filetype = /image\/\w+/i.test(file.type) ? 'image' : 'file',
  22474. loadingId = 'loading_' + (+new Date()).toString(36);
  22475. fieldName = me.getOpt(filetype + 'FieldName');
  22476. urlPrefix = me.getOpt(filetype + 'UrlPrefix');
  22477. maxSize = me.getOpt(filetype + 'MaxSize');
  22478. allowFiles = me.getOpt(filetype + 'AllowFiles');
  22479. actionUrl = me.getActionUrl(me.getOpt(filetype + 'ActionName'));
  22480. errorHandler = function (title) {
  22481. var loader = me.document.getElementById(loadingId);
  22482. loader && domUtils.remove(loader);
  22483. me.fireEvent('showmessage', {
  22484. 'id': loadingId,
  22485. 'content': title,
  22486. 'type': 'error',
  22487. 'timeout': 4000
  22488. });
  22489. };
  22490. if (filetype == 'image') {
  22491. loadingHtml = '<img class="loadingclass" id="' + loadingId + '" src="' +
  22492. me.options.themePath + me.options.theme +
  22493. '/images/spacer.gif" title="' + (me.getLang('autoupload.loading') || '') + '" >';
  22494. successHandler = function (data) {
  22495. var link = urlPrefix + data.url,
  22496. loader = me.document.getElementById(loadingId);
  22497. if (loader) {
  22498. loader.setAttribute('src', link);
  22499. loader.setAttribute('_src', link);
  22500. loader.setAttribute('title', data.title || '');
  22501. loader.setAttribute('alt', data.original || '');
  22502. loader.removeAttribute('id');
  22503. domUtils.removeClasses(loader, 'loadingclass');
  22504. }
  22505. };
  22506. } else {
  22507. loadingHtml = '<p>' +
  22508. '<img class="loadingclass" id="' + loadingId + '" src="' +
  22509. me.options.themePath + me.options.theme +
  22510. '/images/spacer.gif" title="' + (me.getLang('autoupload.loading') || '') + '" >' +
  22511. '</p>';
  22512. successHandler = function (data) {
  22513. var link = urlPrefix + data.url,
  22514. loader = me.document.getElementById(loadingId);
  22515. var rng = me.selection.getRange(),
  22516. bk = rng.createBookmark();
  22517. rng.selectNode(loader).select();
  22518. me.execCommand('insertfile', {
  22519. 'url': link
  22520. });
  22521. rng.moveToBookmark(bk).select();
  22522. };
  22523. }
  22524. /* 插入loading的占位符 */
  22525. me.execCommand('inserthtml', loadingHtml);
  22526. /* 判断后端配置是否没有加载成功 */
  22527. if (!me.getOpt(filetype + 'ActionName')) {
  22528. errorHandler(me.getLang('autoupload.errorLoadConfig'));
  22529. return;
  22530. }
  22531. /* 判断文件大小是否超出限制 */
  22532. if (file.size > maxSize) {
  22533. errorHandler(me.getLang('autoupload.exceedSizeError'));
  22534. return;
  22535. }
  22536. /* 判断文件格式是否超出允许 */
  22537. var fileext = file.name ? file.name.substr(file.name.lastIndexOf('.')) : '';
  22538. if ((fileext && filetype != 'image') || (allowFiles && (allowFiles.join('') + '.').indexOf(fileext.toLowerCase() + '.') == -1)) {
  22539. errorHandler(me.getLang('autoupload.exceedTypeError'));
  22540. return;
  22541. }
  22542. /* 创建Ajax并提交 */
  22543. var xhr = new XMLHttpRequest(),
  22544. fd = new FormData(),
  22545. params = utils.serializeParam(me.queryCommandValue('serverparam')) || '',
  22546. url = utils.formatUrl(actionUrl + (actionUrl.indexOf('?') == -1 ? '?' : '&') + params);
  22547. fd.append(fieldName, file, file.name || ('blob.' + file.type.substr('image/'.length)));
  22548. fd.append('type', 'ajax');
  22549. xhr.open("post", url, true);
  22550. xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
  22551. xhr.addEventListener('load', function (e) {
  22552. try {
  22553. var json = (new Function("return " + utils.trim(e.target.response)))();
  22554. if (json.state == 'SUCCESS' && json.url) {
  22555. successHandler(json);
  22556. } else {
  22557. errorHandler(json.state);
  22558. }
  22559. } catch (er) {
  22560. errorHandler(me.getLang('autoupload.loadError'));
  22561. }
  22562. });
  22563. xhr.send(fd);
  22564. }
  22565. function getPasteImage(e) {
  22566. return e.clipboardData && e.clipboardData.items && e.clipboardData.items.length == 1 && /^image\//.test(e.clipboardData.items[0].type) ? e.clipboardData.items : null;
  22567. }
  22568. function getDropImage(e) {
  22569. return e.dataTransfer && e.dataTransfer.files ? e.dataTransfer.files : null;
  22570. }
  22571. return {
  22572. outputRule: function (root) {
  22573. utils.each(root.getNodesByTagName('img'), function (n) {
  22574. if (/\b(loaderrorclass)|(bloaderrorclass)\b/.test(n.getAttr('class'))) {
  22575. n.parentNode.removeChild(n);
  22576. }
  22577. });
  22578. utils.each(root.getNodesByTagName('p'), function (n) {
  22579. if (/\bloadpara\b/.test(n.getAttr('class'))) {
  22580. n.parentNode.removeChild(n);
  22581. }
  22582. });
  22583. },
  22584. bindEvents: {
  22585. //插入粘贴板的图片,拖放插入图片
  22586. 'ready': function (e) {
  22587. var me = this;
  22588. if (window.FormData && window.FileReader) {
  22589. domUtils.on(me.body, 'paste drop', function (e) {
  22590. var hasImg = false,
  22591. items;
  22592. //获取粘贴板文件列表或者拖放文件列表
  22593. items = e.type == 'paste' ? getPasteImage(e) : getDropImage(e);
  22594. if (items) {
  22595. var len = items.length,
  22596. file;
  22597. while (len--) {
  22598. file = items[len];
  22599. if (file.getAsFile) file = file.getAsFile();
  22600. if (file && file.size > 0) {
  22601. sendAndInsertFile(file, me);
  22602. hasImg = true;
  22603. }
  22604. }
  22605. hasImg && e.preventDefault();
  22606. }
  22607. });
  22608. //取消拖放图片时出现的文字光标位置提示
  22609. domUtils.on(me.body, 'dragover', function (e) {
  22610. if (e.dataTransfer.types[0] == 'Files') {
  22611. e.preventDefault();
  22612. }
  22613. });
  22614. //设置loading的样式
  22615. utils.cssRule('loading',
  22616. '.loadingclass{display:inline-block;cursor:default;background: url(\'' +
  22617. this.options.themePath +
  22618. this.options.theme + '/images/loading.gif\') no-repeat center center transparent;border:1px solid #cccccc;margin-left:1px;height: 22px;width: 22px;}\n' +
  22619. '.loaderrorclass{display:inline-block;cursor:default;background: url(\'' +
  22620. this.options.themePath +
  22621. this.options.theme + '/images/loaderror.png\') no-repeat center center transparent;border:1px solid #cccccc;margin-right:1px;height: 22px;width: 22px;' +
  22622. '}',
  22623. this.document);
  22624. }
  22625. }
  22626. }
  22627. }
  22628. });
  22629. // plugins/autosave.js
  22630. UE.plugin.register('autosave', function () {
  22631. var me = this,
  22632. //无限循环保护
  22633. lastSaveTime = new Date(),
  22634. //最小保存间隔时间
  22635. MIN_TIME = 20,
  22636. //auto save key
  22637. saveKey = null;
  22638. function save(editor) {
  22639. var saveData;
  22640. if (new Date() - lastSaveTime < MIN_TIME) {
  22641. return;
  22642. }
  22643. if (!editor.hasContents()) {
  22644. //这里不能调用命令来删除, 会造成事件死循环
  22645. saveKey && me.removePreferences(saveKey);
  22646. return;
  22647. }
  22648. lastSaveTime = new Date();
  22649. editor._saveFlag = null;
  22650. saveData = me.body.innerHTML;
  22651. if (editor.fireEvent("beforeautosave", {
  22652. content: saveData
  22653. }) === false) {
  22654. return;
  22655. }
  22656. me.setPreferences(saveKey, saveData);
  22657. editor.fireEvent("afterautosave", {
  22658. content: saveData
  22659. });
  22660. }
  22661. return {
  22662. defaultOptions: {
  22663. //默认间隔时间
  22664. saveInterval: 500,
  22665. enableAutoSave: true // HaoChuan9421
  22666. },
  22667. bindEvents: {
  22668. 'ready': function () {
  22669. var _suffix = "-drafts-data",
  22670. key = null;
  22671. if (me.key) {
  22672. key = me.key + _suffix;
  22673. } else {
  22674. key = (me.container.parentNode.id || 'ue-common') + _suffix;
  22675. }
  22676. //页面地址+编辑器ID 保持唯一
  22677. saveKey = (location.protocol + location.host + location.pathname).replace(/[.:\/]/g, '_') + key;
  22678. },
  22679. 'contentchange': function () {
  22680. // HaoChuan9421
  22681. if (!me.getOpt('enableAutoSave')) {
  22682. return;
  22683. }
  22684. if (!saveKey) {
  22685. return;
  22686. }
  22687. if (me._saveFlag) {
  22688. window.clearTimeout(me._saveFlag);
  22689. }
  22690. if (me.options.saveInterval > 0) {
  22691. me._saveFlag = window.setTimeout(function () {
  22692. save(me);
  22693. }, me.options.saveInterval);
  22694. } else {
  22695. save(me);
  22696. }
  22697. }
  22698. },
  22699. commands: {
  22700. 'clearlocaldata': {
  22701. execCommand: function (cmd, name) {
  22702. if (saveKey && me.getPreferences(saveKey)) {
  22703. me.removePreferences(saveKey)
  22704. }
  22705. },
  22706. notNeedUndo: true,
  22707. ignoreContentChange: true
  22708. },
  22709. 'getlocaldata': {
  22710. execCommand: function (cmd, name) {
  22711. return saveKey ? me.getPreferences(saveKey) || '' : '';
  22712. },
  22713. notNeedUndo: true,
  22714. ignoreContentChange: true
  22715. },
  22716. 'drafts': {
  22717. execCommand: function (cmd, name) {
  22718. if (saveKey) {
  22719. me.body.innerHTML = me.getPreferences(saveKey) || '<p>' + domUtils.fillHtml + '</p>';
  22720. me.focus(true);
  22721. }
  22722. },
  22723. queryCommandState: function () {
  22724. return saveKey ? (me.getPreferences(saveKey) === null ? -1 : 0) : -1;
  22725. },
  22726. notNeedUndo: true,
  22727. ignoreContentChange: true
  22728. }
  22729. }
  22730. }
  22731. });
  22732. // plugins/charts.js
  22733. UE.plugin.register('charts', function () {
  22734. var me = this;
  22735. return {
  22736. bindEvents: {
  22737. 'chartserror': function () {}
  22738. },
  22739. commands: {
  22740. 'charts': {
  22741. execCommand: function (cmd, data) {
  22742. var tableNode = domUtils.findParentByTagName(this.selection.getRange().startContainer, 'table', true),
  22743. flagText = [],
  22744. config = {};
  22745. if (!tableNode) {
  22746. return false;
  22747. }
  22748. if (!validData(tableNode)) {
  22749. me.fireEvent("chartserror");
  22750. return false;
  22751. }
  22752. config.title = data.title || '';
  22753. config.subTitle = data.subTitle || '';
  22754. config.xTitle = data.xTitle || '';
  22755. config.yTitle = data.yTitle || '';
  22756. config.suffix = data.suffix || '';
  22757. config.tip = data.tip || '';
  22758. //数据对齐方式
  22759. config.dataFormat = data.tableDataFormat || '';
  22760. //图表类型
  22761. config.chartType = data.chartType || 0;
  22762. for (var key in config) {
  22763. if (!config.hasOwnProperty(key)) {
  22764. continue;
  22765. }
  22766. flagText.push(key + ":" + config[key]);
  22767. }
  22768. tableNode.setAttribute("data-chart", flagText.join(";"));
  22769. domUtils.addClass(tableNode, "edui-charts-table");
  22770. },
  22771. queryCommandState: function (cmd, name) {
  22772. var tableNode = domUtils.findParentByTagName(this.selection.getRange().startContainer, 'table', true);
  22773. return tableNode && validData(tableNode) ? 0 : -1;
  22774. }
  22775. }
  22776. },
  22777. inputRule: function (root) {
  22778. utils.each(root.getNodesByTagName('table'), function (tableNode) {
  22779. if (tableNode.getAttr("data-chart") !== undefined) {
  22780. tableNode.setAttr("style");
  22781. }
  22782. })
  22783. },
  22784. outputRule: function (root) {
  22785. utils.each(root.getNodesByTagName('table'), function (tableNode) {
  22786. if (tableNode.getAttr("data-chart") !== undefined) {
  22787. tableNode.setAttr("style", "display: none;");
  22788. }
  22789. })
  22790. }
  22791. }
  22792. function validData(table) {
  22793. var firstRows = null,
  22794. cellCount = 0;
  22795. //行数不够
  22796. if (table.rows.length < 2) {
  22797. return false;
  22798. }
  22799. //列数不够
  22800. if (table.rows[0].cells.length < 2) {
  22801. return false;
  22802. }
  22803. //第一行所有cell必须是th
  22804. firstRows = table.rows[0].cells;
  22805. cellCount = firstRows.length;
  22806. for (var i = 0, cell; cell = firstRows[i]; i++) {
  22807. if (cell.tagName.toLowerCase() !== 'th') {
  22808. return false;
  22809. }
  22810. }
  22811. for (var i = 1, row; row = table.rows[i]; i++) {
  22812. //每行单元格数不匹配, 返回false
  22813. if (row.cells.length != cellCount) {
  22814. return false;
  22815. }
  22816. //第一列不是th也返回false
  22817. if (row.cells[0].tagName.toLowerCase() !== 'th') {
  22818. return false;
  22819. }
  22820. for (var j = 1, cell; cell = row.cells[j]; j++) {
  22821. var value = utils.trim((cell.innerText || cell.textContent || ''));
  22822. value = value.replace(new RegExp(UE.dom.domUtils.fillChar, 'g'), '').replace(/^\s+|\s+$/g, '');
  22823. //必须是数字
  22824. if (!/^\d*\.?\d+$/.test(value)) {
  22825. return false;
  22826. }
  22827. }
  22828. }
  22829. return true;
  22830. }
  22831. });
  22832. // plugins/section.js
  22833. /**
  22834. * 目录大纲支持插件
  22835. * @file
  22836. * @since 1.3.0
  22837. */
  22838. UE.plugin.register('section', function () {
  22839. /* 目录节点对象 */
  22840. function Section(option) {
  22841. this.tag = '';
  22842. this.level = -1,
  22843. this.dom = null;
  22844. this.nextSection = null;
  22845. this.previousSection = null;
  22846. this.parentSection = null;
  22847. this.startAddress = [];
  22848. this.endAddress = [];
  22849. this.children = [];
  22850. }
  22851. function getSection(option) {
  22852. var section = new Section();
  22853. return utils.extend(section, option);
  22854. }
  22855. function getNodeFromAddress(startAddress, root) {
  22856. var current = root;
  22857. for (var i = 0; i < startAddress.length; i++) {
  22858. if (!current.childNodes) return null;
  22859. current = current.childNodes[startAddress[i]];
  22860. }
  22861. return current;
  22862. }
  22863. var me = this;
  22864. return {
  22865. bindMultiEvents: {
  22866. type: 'aftersetcontent afterscencerestore',
  22867. handler: function () {
  22868. me.fireEvent('updateSections');
  22869. }
  22870. },
  22871. bindEvents: {
  22872. /* 初始化、拖拽、粘贴、执行setcontent之后 */
  22873. 'ready': function () {
  22874. me.fireEvent('updateSections');
  22875. domUtils.on(me.body, 'drop paste', function () {
  22876. me.fireEvent('updateSections');
  22877. });
  22878. },
  22879. /* 执行paragraph命令之后 */
  22880. 'afterexeccommand': function (type, cmd) {
  22881. if (cmd == 'paragraph') {
  22882. me.fireEvent('updateSections');
  22883. }
  22884. },
  22885. /* 部分键盘操作,触发updateSections事件 */
  22886. 'keyup': function (type, e) {
  22887. var me = this,
  22888. range = me.selection.getRange();
  22889. if (range.collapsed != true) {
  22890. me.fireEvent('updateSections');
  22891. } else {
  22892. var keyCode = e.keyCode || e.which;
  22893. if (keyCode == 13 || keyCode == 8 || keyCode == 46) {
  22894. me.fireEvent('updateSections');
  22895. }
  22896. }
  22897. }
  22898. },
  22899. commands: {
  22900. 'getsections': {
  22901. execCommand: function (cmd, levels) {
  22902. var levelFn = levels || ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
  22903. for (var i = 0; i < levelFn.length; i++) {
  22904. if (typeof levelFn[i] == 'string') {
  22905. levelFn[i] = function (fn) {
  22906. return function (node) {
  22907. return node.tagName == fn.toUpperCase()
  22908. };
  22909. }(levelFn[i]);
  22910. } else if (typeof levelFn[i] != 'function') {
  22911. levelFn[i] = function (node) {
  22912. return null;
  22913. }
  22914. }
  22915. }
  22916. function getSectionLevel(node) {
  22917. for (var i = 0; i < levelFn.length; i++) {
  22918. if (levelFn[i](node)) return i;
  22919. }
  22920. return -1;
  22921. }
  22922. var me = this,
  22923. Directory = getSection({
  22924. 'level': -1,
  22925. 'title': 'root'
  22926. }),
  22927. previous = Directory;
  22928. function traversal(node, Directory) {
  22929. var level,
  22930. tmpSection = null,
  22931. parent,
  22932. child,
  22933. children = node.childNodes;
  22934. for (var i = 0, len = children.length; i < len; i++) {
  22935. child = children[i];
  22936. level = getSectionLevel(child);
  22937. if (level >= 0) {
  22938. var address = me.selection.getRange().selectNode(child).createAddress(true).startAddress,
  22939. current = getSection({
  22940. 'tag': child.tagName,
  22941. 'title': child.innerText || child.textContent || '',
  22942. 'level': level,
  22943. 'dom': child,
  22944. 'startAddress': utils.clone(address, []),
  22945. 'endAddress': utils.clone(address, []),
  22946. 'children': []
  22947. });
  22948. previous.nextSection = current;
  22949. current.previousSection = previous;
  22950. parent = previous;
  22951. while (level <= parent.level) {
  22952. parent = parent.parentSection;
  22953. }
  22954. current.parentSection = parent;
  22955. parent.children.push(current);
  22956. tmpSection = previous = current;
  22957. } else {
  22958. child.nodeType === 1 && traversal(child, Directory);
  22959. tmpSection && tmpSection.endAddress[tmpSection.endAddress.length - 1]++;
  22960. }
  22961. }
  22962. }
  22963. traversal(me.body, Directory);
  22964. return Directory;
  22965. },
  22966. notNeedUndo: true
  22967. },
  22968. 'movesection': {
  22969. execCommand: function (cmd, sourceSection, targetSection, isAfter) {
  22970. var me = this,
  22971. targetAddress,
  22972. target;
  22973. if (!sourceSection || !targetSection || targetSection.level == -1) return;
  22974. targetAddress = isAfter ? targetSection.endAddress : targetSection.startAddress;
  22975. target = getNodeFromAddress(targetAddress, me.body);
  22976. /* 判断目标地址是否被源章节包含 */
  22977. if (!targetAddress || !target || isContainsAddress(sourceSection.startAddress, sourceSection.endAddress, targetAddress)) return;
  22978. var startNode = getNodeFromAddress(sourceSection.startAddress, me.body),
  22979. endNode = getNodeFromAddress(sourceSection.endAddress, me.body),
  22980. current,
  22981. nextNode;
  22982. if (isAfter) {
  22983. current = endNode;
  22984. while (current && !(domUtils.getPosition(startNode, current) & domUtils.POSITION_FOLLOWING)) {
  22985. nextNode = current.previousSibling;
  22986. domUtils.insertAfter(target, current);
  22987. if (current == startNode) break;
  22988. current = nextNode;
  22989. }
  22990. } else {
  22991. current = startNode;
  22992. while (current && !(domUtils.getPosition(current, endNode) & domUtils.POSITION_FOLLOWING)) {
  22993. nextNode = current.nextSibling;
  22994. target.parentNode.insertBefore(current, target);
  22995. if (current == endNode) break;
  22996. current = nextNode;
  22997. }
  22998. }
  22999. me.fireEvent('updateSections');
  23000. /* 获取地址的包含关系 */
  23001. function isContainsAddress(startAddress, endAddress, addressTarget) {
  23002. var isAfterStartAddress = false,
  23003. isBeforeEndAddress = false;
  23004. for (var i = 0; i < startAddress.length; i++) {
  23005. if (i >= addressTarget.length) break;
  23006. if (addressTarget[i] > startAddress[i]) {
  23007. isAfterStartAddress = true;
  23008. break;
  23009. } else if (addressTarget[i] < startAddress[i]) {
  23010. break;
  23011. }
  23012. }
  23013. for (var i = 0; i < endAddress.length; i++) {
  23014. if (i >= addressTarget.length) break;
  23015. if (addressTarget[i] < startAddress[i]) {
  23016. isBeforeEndAddress = true;
  23017. break;
  23018. } else if (addressTarget[i] > startAddress[i]) {
  23019. break;
  23020. }
  23021. }
  23022. return isAfterStartAddress && isBeforeEndAddress;
  23023. }
  23024. }
  23025. },
  23026. 'deletesection': {
  23027. execCommand: function (cmd, section, keepChildren) {
  23028. var me = this;
  23029. if (!section) return;
  23030. function getNodeFromAddress(startAddress) {
  23031. var current = me.body;
  23032. for (var i = 0; i < startAddress.length; i++) {
  23033. if (!current.childNodes) return null;
  23034. current = current.childNodes[startAddress[i]];
  23035. }
  23036. return current;
  23037. }
  23038. var startNode = getNodeFromAddress(section.startAddress),
  23039. endNode = getNodeFromAddress(section.endAddress),
  23040. current = startNode,
  23041. nextNode;
  23042. if (!keepChildren) {
  23043. while (current && domUtils.inDoc(endNode, me.document) && !(domUtils.getPosition(current, endNode) & domUtils.POSITION_FOLLOWING)) {
  23044. nextNode = current.nextSibling;
  23045. domUtils.remove(current);
  23046. current = nextNode;
  23047. }
  23048. } else {
  23049. domUtils.remove(current);
  23050. }
  23051. me.fireEvent('updateSections');
  23052. }
  23053. },
  23054. 'selectsection': {
  23055. execCommand: function (cmd, section) {
  23056. if (!section && !section.dom) return false;
  23057. var me = this,
  23058. range = me.selection.getRange(),
  23059. address = {
  23060. 'startAddress': utils.clone(section.startAddress, []),
  23061. 'endAddress': utils.clone(section.endAddress, [])
  23062. };
  23063. address.endAddress[address.endAddress.length - 1]++;
  23064. range.moveToAddress(address).select().scrollToView();
  23065. return true;
  23066. },
  23067. notNeedUndo: true
  23068. },
  23069. 'scrolltosection': {
  23070. execCommand: function (cmd, section) {
  23071. if (!section && !section.dom) return false;
  23072. var me = this,
  23073. range = me.selection.getRange(),
  23074. address = {
  23075. 'startAddress': section.startAddress,
  23076. 'endAddress': section.endAddress
  23077. };
  23078. address.endAddress[address.endAddress.length - 1]++;
  23079. range.moveToAddress(address).scrollToView();
  23080. return true;
  23081. },
  23082. notNeedUndo: true
  23083. }
  23084. }
  23085. }
  23086. });
  23087. // plugins/simpleupload.js
  23088. /**
  23089. * @description
  23090. * 简单上传:点击按钮,直接选择文件上传。
  23091. * 原 UEditor 作者使用了 form 表单 + iframe 的方式上传
  23092. * 但由于同源策略的限制,父页面无法访问跨域的 iframe 内容
  23093. * 导致无法获取接口返回的数据,使得单图上传无法在跨域的情况下使用
  23094. * 这里改为普通的XHR上传,兼容到IE10+
  23095. * @author HaoChuan9421 <hc199421@gmail.com>
  23096. * @date 2018-12-20
  23097. */
  23098. UE.plugin.register('simpleupload', function () {
  23099. var me = this,
  23100. containerBtn,
  23101. timestrap = (+new Date()).toString(36);
  23102. function initUploadBtn() {
  23103. var w = containerBtn.offsetWidth || 20,
  23104. h = containerBtn.offsetHeight || 20,
  23105. 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;';
  23106. var form = document.createElement('form');
  23107. var input = document.createElement('input');
  23108. form.id = 'edui_form_' + timestrap;
  23109. form.enctype = 'multipart/form-data';
  23110. form.style = btnStyle;
  23111. input.id = 'edui_input_' + timestrap;
  23112. input.type = 'file'
  23113. input.accept = 'image/*';
  23114. input.name = me.options.imageFieldName;
  23115. input.style = btnStyle;
  23116. form.appendChild(input);
  23117. containerBtn.appendChild(form);
  23118. input.addEventListener('change', function (event) {
  23119. if (!input.value) return;
  23120. var loadingId = 'loading_' + (+new Date()).toString(36);
  23121. var imageActionUrl = me.getActionUrl(me.getOpt('imageActionName'));
  23122. var params = utils.serializeParam(me.queryCommandValue('serverparam')) || '';
  23123. var action = utils.formatUrl(imageActionUrl + (imageActionUrl.indexOf('?') == -1 ? '?' : '&') + params);
  23124. var allowFiles = me.getOpt('imageAllowFiles');
  23125. me.focus();
  23126. me.execCommand('inserthtml', '<img class="loadingclass" id="' + loadingId + '" src="' + me.options.themePath + me.options.theme + '/images/spacer.gif" title="' + (me.getLang('simpleupload.loading') || '') + '" >');
  23127. function showErrorLoader(title) {
  23128. if (loadingId) {
  23129. var loader = me.document.getElementById(loadingId);
  23130. loader && domUtils.remove(loader);
  23131. me.fireEvent('showmessage', {
  23132. 'id': loadingId,
  23133. 'content': title,
  23134. 'type': 'error',
  23135. 'timeout': 4000
  23136. });
  23137. }
  23138. }
  23139. /* 判断后端配置是否没有加载成功 */
  23140. if (!me.getOpt('imageActionName')) {
  23141. showErrorLoader(me.getLang('autoupload.errorLoadConfig'));
  23142. return;
  23143. }
  23144. // 判断文件格式是否错误
  23145. var filename = input.value,
  23146. fileext = filename ? filename.substr(filename.lastIndexOf('.')) : '';
  23147. if (!fileext || (allowFiles && (allowFiles.join('') + '.').indexOf(fileext.toLowerCase() + '.') == -1)) {
  23148. showErrorLoader(me.getLang('simpleupload.exceedTypeError'));
  23149. return;
  23150. }
  23151. var xhr = new XMLHttpRequest()
  23152. xhr.open('post', action, true)
  23153. if (me.options.headers && Object.prototype.toString.apply(me.options.headers) === "[object Object]") {
  23154. for (var key in me.options.headers) {
  23155. xhr.setRequestHeader(key, me.options.headers[key])
  23156. }
  23157. }
  23158. xhr.onload = function () {
  23159. if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
  23160. var res = JSON.parse(xhr.responseText)
  23161. var link = me.options.imageUrlPrefix + res.url;
  23162. if (res.state == 'SUCCESS' && res.url) {
  23163. loader = me.document.getElementById(loadingId);
  23164. loader.setAttribute('src', link);
  23165. loader.setAttribute('_src', link);
  23166. loader.setAttribute('title', res.title || '');
  23167. loader.setAttribute('alt', res.original || '');
  23168. loader.removeAttribute('id');
  23169. domUtils.removeClasses(loader, 'loadingclass');
  23170. me.fireEvent("contentchange");
  23171. } else {
  23172. showErrorLoader(res.state);
  23173. }
  23174. } else {
  23175. showErrorLoader(me.getLang('simpleupload.loadError'));
  23176. }
  23177. };
  23178. xhr.onerror = function () {
  23179. showErrorLoader(me.getLang('simpleupload.loadError'));
  23180. };
  23181. xhr.send(new FormData(form));
  23182. form.reset();
  23183. })
  23184. }
  23185. return {
  23186. bindEvents: {
  23187. 'ready': function () {
  23188. //设置loading的样式
  23189. utils.cssRule('loading',
  23190. '.loadingclass{display:inline-block;cursor:default;background: url(\'' +
  23191. this.options.themePath +
  23192. this.options.theme + '/images/loading.gif\') no-repeat center center transparent;border:1px solid #cccccc;margin-right:1px;height: 22px;width: 22px;}\n' +
  23193. '.loaderrorclass{display:inline-block;cursor:default;background: url(\'' +
  23194. this.options.themePath +
  23195. this.options.theme + '/images/loaderror.png\') no-repeat center center transparent;border:1px solid #cccccc;margin-right:1px;height: 22px;width: 22px;' +
  23196. '}',
  23197. this.document);
  23198. },
  23199. /* 初始化简单上传按钮 */
  23200. 'simpleuploadbtnready': function (type, container) {
  23201. containerBtn = container;
  23202. me.afterConfigReady(initUploadBtn);
  23203. }
  23204. },
  23205. outputRule: function (root) {
  23206. utils.each(root.getNodesByTagName('img'), function (n) {
  23207. if (/\b(loaderrorclass)|(bloaderrorclass)\b/.test(n.getAttr('class'))) {
  23208. n.parentNode.removeChild(n);
  23209. }
  23210. });
  23211. }
  23212. }
  23213. });
  23214. // plugins/serverparam.js
  23215. /**
  23216. * 服务器提交的额外参数列表设置插件
  23217. * @file
  23218. * @since 1.2.6.1
  23219. */
  23220. UE.plugin.register('serverparam', function () {
  23221. var me = this,
  23222. serverParam = {};
  23223. return {
  23224. commands: {
  23225. /**
  23226. * 修改服务器提交的额外参数列表,清除所有项
  23227. * @command serverparam
  23228. * @method execCommand
  23229. * @param { String } cmd 命令字符串
  23230. * @example
  23231. * ```javascript
  23232. * editor.execCommand('serverparam');
  23233. * editor.queryCommandValue('serverparam'); //返回空
  23234. * ```
  23235. */
  23236. /**
  23237. * 修改服务器提交的额外参数列表,删除指定项
  23238. * @command serverparam
  23239. * @method execCommand
  23240. * @param { String } cmd 命令字符串
  23241. * @param { String } key 要清除的属性
  23242. * @example
  23243. * ```javascript
  23244. * editor.execCommand('serverparam', 'name'); //删除属性name
  23245. * ```
  23246. */
  23247. /**
  23248. * 修改服务器提交的额外参数列表,使用键值添加项
  23249. * @command serverparam
  23250. * @method execCommand
  23251. * @param { String } cmd 命令字符串
  23252. * @param { String } key 要添加的属性
  23253. * @param { String } value 要添加属性的值
  23254. * @example
  23255. * ```javascript
  23256. * editor.execCommand('serverparam', 'name', 'hello');
  23257. * editor.queryCommandValue('serverparam'); //返回对象 {'name': 'hello'}
  23258. * ```
  23259. */
  23260. /**
  23261. * 修改服务器提交的额外参数列表,传入键值对对象添加多项
  23262. * @command serverparam
  23263. * @method execCommand
  23264. * @param { String } cmd 命令字符串
  23265. * @param { Object } key 传入的键值对对象
  23266. * @example
  23267. * ```javascript
  23268. * editor.execCommand('serverparam', {'name': 'hello'});
  23269. * editor.queryCommandValue('serverparam'); //返回对象 {'name': 'hello'}
  23270. * ```
  23271. */
  23272. /**
  23273. * 修改服务器提交的额外参数列表,使用自定义函数添加多项
  23274. * @command serverparam
  23275. * @method execCommand
  23276. * @param { String } cmd 命令字符串
  23277. * @param { Function } key 自定义获取参数的函数
  23278. * @example
  23279. * ```javascript
  23280. * editor.execCommand('serverparam', function(editor){
  23281. * return {'key': 'value'};
  23282. * });
  23283. * editor.queryCommandValue('serverparam'); //返回对象 {'key': 'value'}
  23284. * ```
  23285. */
  23286. /**
  23287. * 获取服务器提交的额外参数列表
  23288. * @command serverparam
  23289. * @method queryCommandValue
  23290. * @param { String } cmd 命令字符串
  23291. * @example
  23292. * ```javascript
  23293. * editor.queryCommandValue( 'serverparam' ); //返回对象 {'key': 'value'}
  23294. * ```
  23295. */
  23296. 'serverparam': {
  23297. execCommand: function (cmd, key, value) {
  23298. if (key === undefined || key === null) { //不传参数,清空列表
  23299. serverParam = {};
  23300. } else if (utils.isString(key)) { //传入键值
  23301. if (value === undefined || value === null) {
  23302. delete serverParam[key];
  23303. } else {
  23304. serverParam[key] = value;
  23305. }
  23306. } else if (utils.isObject(key)) { //传入对象,覆盖列表项
  23307. utils.extend(serverParam, key, true);
  23308. } else if (utils.isFunction(key)) { //传入函数,添加列表项
  23309. utils.extend(serverParam, key(), true);
  23310. }
  23311. },
  23312. queryCommandValue: function () {
  23313. return serverParam || {};
  23314. }
  23315. }
  23316. }
  23317. }
  23318. });
  23319. // plugins/insertfile.js
  23320. /**
  23321. * 插入附件
  23322. */
  23323. UE.plugin.register('insertfile', function () {
  23324. var me = this;
  23325. function getFileIcon(url) {
  23326. var ext = url.substr(url.lastIndexOf('.') + 1).toLowerCase(),
  23327. maps = {
  23328. "rar": "icon_rar.gif",
  23329. "zip": "icon_rar.gif",
  23330. "tar": "icon_rar.gif",
  23331. "gz": "icon_rar.gif",
  23332. "bz2": "icon_rar.gif",
  23333. "doc": "icon_doc.gif",
  23334. "docx": "icon_doc.gif",
  23335. "pdf": "icon_pdf.gif",
  23336. "mp3": "icon_mp3.gif",
  23337. "xls": "icon_xls.gif",
  23338. "chm": "icon_chm.gif",
  23339. "ppt": "icon_ppt.gif",
  23340. "pptx": "icon_ppt.gif",
  23341. "avi": "icon_mv.gif",
  23342. "rmvb": "icon_mv.gif",
  23343. "wmv": "icon_mv.gif",
  23344. "flv": "icon_mv.gif",
  23345. "swf": "icon_mv.gif",
  23346. "rm": "icon_mv.gif",
  23347. "exe": "icon_exe.gif",
  23348. "psd": "icon_psd.gif",
  23349. "txt": "icon_txt.gif",
  23350. "jpg": "icon_jpg.gif",
  23351. "png": "icon_jpg.gif",
  23352. "jpeg": "icon_jpg.gif",
  23353. "gif": "icon_jpg.gif",
  23354. "ico": "icon_jpg.gif",
  23355. "bmp": "icon_jpg.gif"
  23356. };
  23357. return maps[ext] ? maps[ext] : maps['txt'];
  23358. }
  23359. return {
  23360. commands: {
  23361. 'insertfile': {
  23362. execCommand: function (command, filelist) {
  23363. filelist = utils.isArray(filelist) ? filelist : [filelist];
  23364. var i, item, icon, title,
  23365. html = '',
  23366. URL = me.getOpt('UEDITOR_HOME_URL'),
  23367. iconDir = URL + (URL.substr(URL.length - 1) == '/' ? '' : '/') + 'dialogs/attachment/fileTypeImages/';
  23368. for (i = 0; i < filelist.length; i++) {
  23369. item = filelist[i];
  23370. icon = iconDir + getFileIcon(item.url);
  23371. title = item.title || item.url.substr(item.url.lastIndexOf('/') + 1);
  23372. html += '<p style="line-height: 16px;">' +
  23373. '<img style="vertical-align: middle; margin-right: 2px;" src="' + icon + '" _src="' + icon + '" />' +
  23374. '<a style="font-size:12px; color:#0066cc;" href="' + item.url + '" title="' + title + '">' + title + '</a>' +
  23375. '</p>';
  23376. }
  23377. me.execCommand('insertHtml', html);
  23378. }
  23379. }
  23380. }
  23381. }
  23382. });
  23383. // plugins/xssFilter.js
  23384. /**
  23385. * @file xssFilter.js
  23386. * @desc xss过滤器
  23387. * @author robbenmu
  23388. */
  23389. UE.plugins.xssFilter = function () {
  23390. var config = UEDITOR_CONFIG;
  23391. var whitList = config.whitList;
  23392. function filter(node) {
  23393. var tagName = node.tagName;
  23394. var attrs = node.attrs;
  23395. if (!whitList.hasOwnProperty(tagName)) {
  23396. node.parentNode.removeChild(node);
  23397. return false;
  23398. }
  23399. UE.utils.each(attrs, function (val, key) {
  23400. if (whitList[tagName].indexOf(key) === -1) {
  23401. node.setAttr(key);
  23402. }
  23403. });
  23404. }
  23405. // 添加inserthtml\paste等操作用的过滤规则
  23406. if (whitList && config.xssFilterRules) {
  23407. this.options.filterRules = function () {
  23408. var result = {};
  23409. UE.utils.each(whitList, function (val, key) {
  23410. result[key] = function (node) {
  23411. return filter(node);
  23412. };
  23413. });
  23414. return result;
  23415. }();
  23416. }
  23417. var tagList = [];
  23418. UE.utils.each(whitList, function (val, key) {
  23419. tagList.push(key);
  23420. });
  23421. // 添加input过滤规则
  23422. //
  23423. if (whitList && config.inputXssFilter) {
  23424. this.addInputRule(function (root) {
  23425. root.traversal(function (node) {
  23426. if (node.type !== 'element') {
  23427. return false;
  23428. }
  23429. filter(node);
  23430. });
  23431. });
  23432. }
  23433. // 添加output过滤规则
  23434. //
  23435. if (whitList && config.outputXssFilter) {
  23436. this.addOutputRule(function (root) {
  23437. root.traversal(function (node) {
  23438. if (node.type !== 'element') {
  23439. return false;
  23440. }
  23441. filter(node);
  23442. });
  23443. });
  23444. }
  23445. };
  23446. // ui/ui.js
  23447. var baidu = baidu || {};
  23448. baidu.editor = baidu.editor || {};
  23449. UE.ui = baidu.editor.ui = {};
  23450. // ui/uiutils.js
  23451. (function () {
  23452. var browser = baidu.editor.browser,
  23453. domUtils = baidu.editor.dom.domUtils;
  23454. var magic = '$EDITORUI';
  23455. var root = window[magic] = {};
  23456. var uidMagic = 'ID' + magic;
  23457. var uidCount = 0;
  23458. var uiUtils = baidu.editor.ui.uiUtils = {
  23459. uid: function (obj) {
  23460. return (obj ? obj[uidMagic] || (obj[uidMagic] = ++uidCount) : ++uidCount);
  23461. },
  23462. hook: function (fn, callback) {
  23463. var dg;
  23464. if (fn && fn._callbacks) {
  23465. dg = fn;
  23466. } else {
  23467. dg = function () {
  23468. var q;
  23469. if (fn) {
  23470. q = fn.apply(this, arguments);
  23471. }
  23472. var callbacks = dg._callbacks;
  23473. var k = callbacks.length;
  23474. while (k--) {
  23475. var r = callbacks[k].apply(this, arguments);
  23476. if (q === undefined) {
  23477. q = r;
  23478. }
  23479. }
  23480. return q;
  23481. };
  23482. dg._callbacks = [];
  23483. }
  23484. dg._callbacks.push(callback);
  23485. return dg;
  23486. },
  23487. createElementByHtml: function (html) {
  23488. var el = document.createElement('div');
  23489. el.innerHTML = html;
  23490. el = el.firstChild;
  23491. el.parentNode.removeChild(el);
  23492. return el;
  23493. },
  23494. getViewportElement: function () {
  23495. return (browser.ie && browser.quirks) ?
  23496. document.body : document.documentElement;
  23497. },
  23498. getClientRect: function (element) {
  23499. var bcr;
  23500. //trace IE6下在控制编辑器显隐时可能会报错,catch一下
  23501. try {
  23502. bcr = element.getBoundingClientRect();
  23503. } catch (e) {
  23504. bcr = {
  23505. left: 0,
  23506. top: 0,
  23507. height: 0,
  23508. width: 0
  23509. }
  23510. }
  23511. var rect = {
  23512. left: Math.round(bcr.left),
  23513. top: Math.round(bcr.top),
  23514. height: Math.round(bcr.bottom - bcr.top),
  23515. width: Math.round(bcr.right - bcr.left)
  23516. };
  23517. var doc;
  23518. while ((doc = element.ownerDocument) !== document &&
  23519. (element = domUtils.getWindow(doc).frameElement)) {
  23520. bcr = element.getBoundingClientRect();
  23521. rect.left += bcr.left;
  23522. rect.top += bcr.top;
  23523. }
  23524. rect.bottom = rect.top + rect.height;
  23525. rect.right = rect.left + rect.width;
  23526. return rect;
  23527. },
  23528. getViewportRect: function () {
  23529. var viewportEl = uiUtils.getViewportElement();
  23530. var width = (window.innerWidth || viewportEl.clientWidth) | 0;
  23531. var height = (window.innerHeight || viewportEl.clientHeight) | 0;
  23532. return {
  23533. left: 0,
  23534. top: 0,
  23535. height: height,
  23536. width: width,
  23537. bottom: height,
  23538. right: width
  23539. };
  23540. },
  23541. setViewportOffset: function (element, offset) {
  23542. var rect;
  23543. var fixedLayer = uiUtils.getFixedLayer();
  23544. if (element.parentNode === fixedLayer) {
  23545. element.style.left = offset.left + 'px';
  23546. element.style.top = offset.top + 'px';
  23547. } else {
  23548. domUtils.setViewportOffset(element, offset);
  23549. }
  23550. },
  23551. getEventOffset: function (evt) {
  23552. var el = evt.target || evt.srcElement;
  23553. var rect = uiUtils.getClientRect(el);
  23554. var offset = uiUtils.getViewportOffsetByEvent(evt);
  23555. return {
  23556. left: offset.left - rect.left,
  23557. top: offset.top - rect.top
  23558. };
  23559. },
  23560. getViewportOffsetByEvent: function (evt) {
  23561. var el = evt.target || evt.srcElement;
  23562. var frameEl = domUtils.getWindow(el).frameElement;
  23563. var offset = {
  23564. left: evt.clientX,
  23565. top: evt.clientY
  23566. };
  23567. if (frameEl && el.ownerDocument !== document) {
  23568. var rect = uiUtils.getClientRect(frameEl);
  23569. offset.left += rect.left;
  23570. offset.top += rect.top;
  23571. }
  23572. return offset;
  23573. },
  23574. setGlobal: function (id, obj) {
  23575. root[id] = obj;
  23576. return magic + '["' + id + '"]';
  23577. },
  23578. unsetGlobal: function (id) {
  23579. delete root[id];
  23580. },
  23581. copyAttributes: function (tgt, src) {
  23582. var attributes = src.attributes;
  23583. var k = attributes.length;
  23584. while (k--) {
  23585. var attrNode = attributes[k];
  23586. if (attrNode.nodeName != 'style' && attrNode.nodeName != 'class' && (!browser.ie || attrNode.specified)) {
  23587. tgt.setAttribute(attrNode.nodeName, attrNode.nodeValue);
  23588. }
  23589. }
  23590. if (src.className) {
  23591. domUtils.addClass(tgt, src.className);
  23592. }
  23593. if (src.style.cssText) {
  23594. tgt.style.cssText += ';' + src.style.cssText;
  23595. }
  23596. },
  23597. removeStyle: function (el, styleName) {
  23598. if (el.style.removeProperty) {
  23599. el.style.removeProperty(styleName);
  23600. } else if (el.style.removeAttribute) {
  23601. el.style.removeAttribute(styleName);
  23602. } else throw '';
  23603. },
  23604. contains: function (elA, elB) {
  23605. return elA && elB && (elA === elB ? false : (
  23606. elA.contains ? elA.contains(elB) :
  23607. elA.compareDocumentPosition(elB) & 16
  23608. ));
  23609. },
  23610. startDrag: function (evt, callbacks, doc) {
  23611. var doc = doc || document;
  23612. var startX = evt.clientX;
  23613. var startY = evt.clientY;
  23614. function handleMouseMove(evt) {
  23615. var x = evt.clientX - startX;
  23616. var y = evt.clientY - startY;
  23617. callbacks.ondragmove(x, y, evt);
  23618. if (evt.stopPropagation) {
  23619. evt.stopPropagation();
  23620. } else {
  23621. evt.cancelBubble = true;
  23622. }
  23623. }
  23624. if (doc.addEventListener) {
  23625. function handleMouseUp(evt) {
  23626. doc.removeEventListener('mousemove', handleMouseMove, true);
  23627. doc.removeEventListener('mouseup', handleMouseUp, true);
  23628. window.removeEventListener('mouseup', handleMouseUp, true);
  23629. callbacks.ondragstop();
  23630. }
  23631. doc.addEventListener('mousemove', handleMouseMove, true);
  23632. doc.addEventListener('mouseup', handleMouseUp, true);
  23633. window.addEventListener('mouseup', handleMouseUp, true);
  23634. evt.preventDefault();
  23635. } else {
  23636. var elm = evt.srcElement;
  23637. elm.setCapture();
  23638. function releaseCaptrue() {
  23639. elm.releaseCapture();
  23640. elm.detachEvent('onmousemove', handleMouseMove);
  23641. elm.detachEvent('onmouseup', releaseCaptrue);
  23642. elm.detachEvent('onlosecaptrue', releaseCaptrue);
  23643. callbacks.ondragstop();
  23644. }
  23645. elm.attachEvent('onmousemove', handleMouseMove);
  23646. elm.attachEvent('onmouseup', releaseCaptrue);
  23647. elm.attachEvent('onlosecaptrue', releaseCaptrue);
  23648. evt.returnValue = false;
  23649. }
  23650. callbacks.ondragstart();
  23651. },
  23652. getFixedLayer: function () {
  23653. var layer = document.getElementById('edui_fixedlayer');
  23654. if (layer == null) {
  23655. layer = document.createElement('div');
  23656. layer.id = 'edui_fixedlayer';
  23657. document.body.appendChild(layer);
  23658. if (browser.ie && browser.version <= 8) {
  23659. layer.style.position = 'absolute';
  23660. bindFixedLayer();
  23661. setTimeout(updateFixedOffset);
  23662. } else {
  23663. layer.style.position = 'fixed';
  23664. }
  23665. layer.style.left = '0';
  23666. layer.style.top = '0';
  23667. layer.style.width = '0';
  23668. layer.style.height = '0';
  23669. }
  23670. return layer;
  23671. },
  23672. makeUnselectable: function (element) {
  23673. if (browser.opera || (browser.ie && browser.version < 9)) {
  23674. element.unselectable = 'on';
  23675. if (element.hasChildNodes()) {
  23676. for (var i = 0; i < element.childNodes.length; i++) {
  23677. if (element.childNodes[i].nodeType == 1) {
  23678. uiUtils.makeUnselectable(element.childNodes[i]);
  23679. }
  23680. }
  23681. }
  23682. } else {
  23683. if (element.style.MozUserSelect !== undefined) {
  23684. element.style.MozUserSelect = 'none';
  23685. } else if (element.style.WebkitUserSelect !== undefined) {
  23686. element.style.WebkitUserSelect = 'none';
  23687. } else if (element.style.KhtmlUserSelect !== undefined) {
  23688. element.style.KhtmlUserSelect = 'none';
  23689. }
  23690. }
  23691. }
  23692. };
  23693. function updateFixedOffset() {
  23694. var layer = document.getElementById('edui_fixedlayer');
  23695. uiUtils.setViewportOffset(layer, {
  23696. left: 0,
  23697. top: 0
  23698. });
  23699. // layer.style.display = 'none';
  23700. // layer.style.display = 'block';
  23701. //#trace: 1354
  23702. // setTimeout(updateFixedOffset);
  23703. }
  23704. function bindFixedLayer(adjOffset) {
  23705. domUtils.on(window, 'scroll', updateFixedOffset);
  23706. domUtils.on(window, 'resize', baidu.editor.utils.defer(updateFixedOffset, 0, true));
  23707. }
  23708. })();
  23709. // ui/uibase.js
  23710. (function () {
  23711. var utils = baidu.editor.utils,
  23712. uiUtils = baidu.editor.ui.uiUtils,
  23713. EventBase = baidu.editor.EventBase,
  23714. UIBase = baidu.editor.ui.UIBase = function () {};
  23715. UIBase.prototype = {
  23716. className: '',
  23717. uiName: '',
  23718. initOptions: function (options) {
  23719. var me = this;
  23720. for (var k in options) {
  23721. me[k] = options[k];
  23722. }
  23723. this.id = this.id || 'edui' + uiUtils.uid();
  23724. },
  23725. initUIBase: function () {
  23726. this._globalKey = utils.unhtml(uiUtils.setGlobal(this.id, this));
  23727. },
  23728. render: function (holder) {
  23729. var html = this.renderHtml();
  23730. var el = uiUtils.createElementByHtml(html);
  23731. //by xuheng 给每个node添加class
  23732. var list = domUtils.getElementsByTagName(el, "*");
  23733. var theme = "edui-" + (this.theme || this.editor.options.theme);
  23734. var layer = document.getElementById('edui_fixedlayer');
  23735. for (var i = 0, node; node = list[i++];) {
  23736. domUtils.addClass(node, theme);
  23737. }
  23738. domUtils.addClass(el, theme);
  23739. if (layer) {
  23740. layer.className = "";
  23741. domUtils.addClass(layer, theme);
  23742. }
  23743. var seatEl = this.getDom();
  23744. if (seatEl != null) {
  23745. seatEl.parentNode.replaceChild(el, seatEl);
  23746. uiUtils.copyAttributes(el, seatEl);
  23747. } else {
  23748. if (typeof holder == 'string') {
  23749. holder = document.getElementById(holder);
  23750. }
  23751. holder = holder || uiUtils.getFixedLayer();
  23752. domUtils.addClass(holder, theme);
  23753. holder.appendChild(el);
  23754. }
  23755. this.postRender();
  23756. },
  23757. getDom: function (name) {
  23758. if (!name) {
  23759. return document.getElementById(this.id);
  23760. } else {
  23761. return document.getElementById(this.id + '_' + name);
  23762. }
  23763. },
  23764. postRender: function () {
  23765. this.fireEvent('postrender');
  23766. },
  23767. getHtmlTpl: function () {
  23768. return '';
  23769. },
  23770. formatHtml: function (tpl) {
  23771. var prefix = 'edui-' + this.uiName;
  23772. return (tpl
  23773. .replace(/##/g, this.id)
  23774. .replace(/%%-/g, this.uiName ? prefix + '-' : '')
  23775. .replace(/%%/g, (this.uiName ? prefix : '') + ' ' + this.className)
  23776. .replace(/\$\$/g, this._globalKey));
  23777. },
  23778. renderHtml: function () {
  23779. return this.formatHtml(this.getHtmlTpl());
  23780. },
  23781. dispose: function () {
  23782. var box = this.getDom();
  23783. if (box) baidu.editor.dom.domUtils.remove(box);
  23784. uiUtils.unsetGlobal(this.id);
  23785. }
  23786. };
  23787. utils.inherits(UIBase, EventBase);
  23788. })();
  23789. // ui/separator.js
  23790. (function () {
  23791. var utils = baidu.editor.utils,
  23792. UIBase = baidu.editor.ui.UIBase,
  23793. Separator = baidu.editor.ui.Separator = function (options) {
  23794. this.initOptions(options);
  23795. this.initSeparator();
  23796. };
  23797. Separator.prototype = {
  23798. uiName: 'separator',
  23799. initSeparator: function () {
  23800. this.initUIBase();
  23801. },
  23802. getHtmlTpl: function () {
  23803. return '<div id="##" class="edui-box %%"></div>';
  23804. }
  23805. };
  23806. utils.inherits(Separator, UIBase);
  23807. })();
  23808. // ui/mask.js
  23809. ///import core
  23810. ///import uicore
  23811. (function () {
  23812. var utils = baidu.editor.utils,
  23813. domUtils = baidu.editor.dom.domUtils,
  23814. UIBase = baidu.editor.ui.UIBase,
  23815. uiUtils = baidu.editor.ui.uiUtils;
  23816. var Mask = baidu.editor.ui.Mask = function (options) {
  23817. this.initOptions(options);
  23818. this.initUIBase();
  23819. };
  23820. Mask.prototype = {
  23821. getHtmlTpl: function () {
  23822. return '<div id="##" class="edui-mask %%" onclick="return $$._onClick(event, this);" onmousedown="return $$._onMouseDown(event, this);"></div>';
  23823. },
  23824. postRender: function () {
  23825. var me = this;
  23826. domUtils.on(window, 'resize', function () {
  23827. setTimeout(function () {
  23828. if (!me.isHidden()) {
  23829. me._fill();
  23830. }
  23831. });
  23832. });
  23833. },
  23834. show: function (zIndex) {
  23835. this._fill();
  23836. this.getDom().style.display = '';
  23837. this.getDom().style.zIndex = zIndex;
  23838. },
  23839. hide: function () {
  23840. this.getDom().style.display = 'none';
  23841. this.getDom().style.zIndex = '';
  23842. },
  23843. isHidden: function () {
  23844. return this.getDom().style.display == 'none';
  23845. },
  23846. _onMouseDown: function () {
  23847. return false;
  23848. },
  23849. _onClick: function (e, target) {
  23850. this.fireEvent('click', e, target);
  23851. },
  23852. _fill: function () {
  23853. var el = this.getDom();
  23854. var vpRect = uiUtils.getViewportRect();
  23855. el.style.width = vpRect.width + 'px';
  23856. el.style.height = vpRect.height + 'px';
  23857. }
  23858. };
  23859. utils.inherits(Mask, UIBase);
  23860. })();
  23861. // ui/popup.js
  23862. ///import core
  23863. ///import uicore
  23864. (function () {
  23865. var utils = baidu.editor.utils,
  23866. uiUtils = baidu.editor.ui.uiUtils,
  23867. domUtils = baidu.editor.dom.domUtils,
  23868. UIBase = baidu.editor.ui.UIBase,
  23869. Popup = baidu.editor.ui.Popup = function (options) {
  23870. this.initOptions(options);
  23871. this.initPopup();
  23872. };
  23873. var allPopups = [];
  23874. function closeAllPopup(evt, el) {
  23875. for (var i = 0; i < allPopups.length; i++) {
  23876. var pop = allPopups[i];
  23877. if (!pop.isHidden()) {
  23878. if (pop.queryAutoHide(el) !== false) {
  23879. if (evt && /scroll/ig.test(evt.type) && pop.className == "edui-wordpastepop") return;
  23880. pop.hide();
  23881. }
  23882. }
  23883. }
  23884. if (allPopups.length)
  23885. pop.editor.fireEvent("afterhidepop");
  23886. }
  23887. Popup.postHide = closeAllPopup;
  23888. var ANCHOR_CLASSES = ['edui-anchor-topleft', 'edui-anchor-topright',
  23889. 'edui-anchor-bottomleft', 'edui-anchor-bottomright'
  23890. ];
  23891. Popup.prototype = {
  23892. SHADOW_RADIUS: 5,
  23893. content: null,
  23894. _hidden: false,
  23895. autoRender: true,
  23896. canSideLeft: true,
  23897. canSideUp: true,
  23898. initPopup: function () {
  23899. this.initUIBase();
  23900. allPopups.push(this);
  23901. },
  23902. getHtmlTpl: function () {
  23903. return '<div id="##" class="edui-popup %%" onmousedown="return false;">' +
  23904. ' <div id="##_body" class="edui-popup-body">' +
  23905. ' <iframe style="position:absolute;z-index:-1;left:0;top:0;background-color: transparent;" frameborder="0" width="100%" height="100%" src="about:blank"></iframe>' +
  23906. ' <div class="edui-shadow"></div>' +
  23907. ' <div id="##_content" class="edui-popup-content">' +
  23908. this.getContentHtmlTpl() +
  23909. ' </div>' +
  23910. ' </div>' +
  23911. '</div>';
  23912. },
  23913. getContentHtmlTpl: function () {
  23914. if (this.content) {
  23915. if (typeof this.content == 'string') {
  23916. return this.content;
  23917. }
  23918. return this.content.renderHtml();
  23919. } else {
  23920. return ''
  23921. }
  23922. },
  23923. _UIBase_postRender: UIBase.prototype.postRender,
  23924. postRender: function () {
  23925. if (this.content instanceof UIBase) {
  23926. this.content.postRender();
  23927. }
  23928. //捕获鼠标滚轮
  23929. if (this.captureWheel && !this.captured) {
  23930. this.captured = true;
  23931. var winHeight = (document.documentElement.clientHeight || document.body.clientHeight) - 80,
  23932. _height = this.getDom().offsetHeight,
  23933. _top = uiUtils.getClientRect(this.combox.getDom()).top,
  23934. content = this.getDom('content'),
  23935. ifr = this.getDom('body').getElementsByTagName('iframe'),
  23936. me = this;
  23937. ifr.length && (ifr = ifr[0]);
  23938. while (_top + _height > winHeight) {
  23939. _height -= 30;
  23940. }
  23941. content.style.height = _height + 'px';
  23942. //同步更改iframe高度
  23943. ifr && (ifr.style.height = _height + 'px');
  23944. //阻止在combox上的鼠标滚轮事件, 防止用户的正常操作被误解
  23945. if (window.XMLHttpRequest) {
  23946. domUtils.on(content, ('onmousewheel' in document.body) ? 'mousewheel' : 'DOMMouseScroll', function (e) {
  23947. if (e.preventDefault) {
  23948. e.preventDefault();
  23949. } else {
  23950. e.returnValue = false;
  23951. }
  23952. if (e.wheelDelta) {
  23953. content.scrollTop -= (e.wheelDelta / 120) * 60;
  23954. } else {
  23955. content.scrollTop -= (e.detail / -3) * 60;
  23956. }
  23957. });
  23958. } else {
  23959. //ie6
  23960. domUtils.on(this.getDom(), 'mousewheel', function (e) {
  23961. e.returnValue = false;
  23962. me.getDom('content').scrollTop -= (e.wheelDelta / 120) * 60;
  23963. });
  23964. }
  23965. }
  23966. this.fireEvent('postRenderAfter');
  23967. this.hide(true);
  23968. this._UIBase_postRender();
  23969. },
  23970. _doAutoRender: function () {
  23971. if (!this.getDom() && this.autoRender) {
  23972. this.render();
  23973. }
  23974. },
  23975. mesureSize: function () {
  23976. var box = this.getDom('content');
  23977. return uiUtils.getClientRect(box);
  23978. },
  23979. fitSize: function () {
  23980. if (this.captureWheel && this.sized) {
  23981. return this.__size;
  23982. }
  23983. this.sized = true;
  23984. var popBodyEl = this.getDom('body');
  23985. popBodyEl.style.width = '';
  23986. popBodyEl.style.height = '';
  23987. var size = this.mesureSize();
  23988. if (this.captureWheel) {
  23989. popBodyEl.style.width = -(-20 - size.width) + 'px';
  23990. var height = parseInt(this.getDom('content').style.height, 10);
  23991. !window.isNaN(height) && (size.height = height);
  23992. } else {
  23993. popBodyEl.style.width = size.width + 'px';
  23994. }
  23995. popBodyEl.style.height = size.height + 'px';
  23996. this.__size = size;
  23997. this.captureWheel && (this.getDom('content').style.overflow = 'auto');
  23998. return size;
  23999. },
  24000. showAnchor: function (element, hoz) {
  24001. this.showAnchorRect(uiUtils.getClientRect(element), hoz);
  24002. },
  24003. showAnchorRect: function (rect, hoz, adj) {
  24004. this._doAutoRender();
  24005. var vpRect = uiUtils.getViewportRect();
  24006. this.getDom().style.visibility = 'hidden';
  24007. this._show();
  24008. var popSize = this.fitSize();
  24009. var sideLeft, sideUp, left, top;
  24010. if (hoz) {
  24011. sideLeft = this.canSideLeft && (rect.right + popSize.width > vpRect.right && rect.left > popSize.width);
  24012. sideUp = this.canSideUp && (rect.top + popSize.height > vpRect.bottom && rect.bottom > popSize.height);
  24013. left = (sideLeft ? rect.left - popSize.width : rect.right);
  24014. top = (sideUp ? rect.bottom - popSize.height : rect.top);
  24015. } else {
  24016. sideLeft = this.canSideLeft && (rect.right + popSize.width > vpRect.right && rect.left > popSize.width);
  24017. sideUp = this.canSideUp && (rect.top + popSize.height > vpRect.bottom && rect.bottom > popSize.height);
  24018. left = (sideLeft ? rect.right - popSize.width : rect.left);
  24019. top = (sideUp ? rect.top - popSize.height : rect.bottom);
  24020. }
  24021. var popEl = this.getDom();
  24022. uiUtils.setViewportOffset(popEl, {
  24023. left: left,
  24024. top: top
  24025. });
  24026. domUtils.removeClasses(popEl, ANCHOR_CLASSES);
  24027. popEl.className += ' ' + ANCHOR_CLASSES[(sideUp ? 1 : 0) * 2 + (sideLeft ? 1 : 0)];
  24028. if (this.editor) {
  24029. popEl.style.zIndex = this.editor.container.style.zIndex * 1 + 10;
  24030. baidu.editor.ui.uiUtils.getFixedLayer().style.zIndex = popEl.style.zIndex - 1;
  24031. }
  24032. this.getDom().style.visibility = 'visible';
  24033. },
  24034. showAt: function (offset) {
  24035. var left = offset.left;
  24036. var top = offset.top;
  24037. var rect = {
  24038. left: left,
  24039. top: top,
  24040. right: left,
  24041. bottom: top,
  24042. height: 0,
  24043. width: 0
  24044. };
  24045. this.showAnchorRect(rect, false, true);
  24046. },
  24047. _show: function () {
  24048. if (this._hidden) {
  24049. var box = this.getDom();
  24050. box.style.display = '';
  24051. this._hidden = false;
  24052. // if (box.setActive) {
  24053. // box.setActive();
  24054. // }
  24055. this.fireEvent('show');
  24056. }
  24057. },
  24058. isHidden: function () {
  24059. return this._hidden;
  24060. },
  24061. show: function () {
  24062. this._doAutoRender();
  24063. this._show();
  24064. },
  24065. hide: function (notNofity) {
  24066. if (!this._hidden && this.getDom()) {
  24067. this.getDom().style.display = 'none';
  24068. this._hidden = true;
  24069. if (!notNofity) {
  24070. this.fireEvent('hide');
  24071. }
  24072. }
  24073. },
  24074. queryAutoHide: function (el) {
  24075. return !el || !uiUtils.contains(this.getDom(), el);
  24076. }
  24077. };
  24078. utils.inherits(Popup, UIBase);
  24079. domUtils.on(document, 'mousedown', function (evt) {
  24080. var el = evt.target || evt.srcElement;
  24081. closeAllPopup(evt, el);
  24082. });
  24083. domUtils.on(window, 'scroll', function (evt, el) {
  24084. closeAllPopup(evt, el);
  24085. });
  24086. })();
  24087. // ui/colorpicker.js
  24088. ///import core
  24089. ///import uicore
  24090. (function () {
  24091. var utils = baidu.editor.utils,
  24092. UIBase = baidu.editor.ui.UIBase,
  24093. ColorPicker = baidu.editor.ui.ColorPicker = function (options) {
  24094. this.initOptions(options);
  24095. this.noColorText = this.noColorText || this.editor.getLang("clearColor");
  24096. this.initUIBase();
  24097. };
  24098. ColorPicker.prototype = {
  24099. getHtmlTpl: function () {
  24100. return genColorPicker(this.noColorText, this.editor);
  24101. },
  24102. _onTableClick: function (evt) {
  24103. var tgt = evt.target || evt.srcElement;
  24104. var color = tgt.getAttribute('data-color');
  24105. if (color) {
  24106. this.fireEvent('pickcolor', color);
  24107. }
  24108. },
  24109. _onTableOver: function (evt) {
  24110. var tgt = evt.target || evt.srcElement;
  24111. var color = tgt.getAttribute('data-color');
  24112. if (color) {
  24113. this.getDom('preview').style.backgroundColor = color;
  24114. }
  24115. },
  24116. _onTableOut: function () {
  24117. this.getDom('preview').style.backgroundColor = '';
  24118. },
  24119. _onPickNoColor: function () {
  24120. this.fireEvent('picknocolor');
  24121. }
  24122. };
  24123. utils.inherits(ColorPicker, UIBase);
  24124. var COLORS = (
  24125. 'ffffff,000000,eeece1,1f497d,4f81bd,c0504d,9bbb59,8064a2,4bacc6,f79646,' +
  24126. 'f2f2f2,7f7f7f,ddd9c3,c6d9f0,dbe5f1,f2dcdb,ebf1dd,e5e0ec,dbeef3,fdeada,' +
  24127. 'd8d8d8,595959,c4bd97,8db3e2,b8cce4,e5b9b7,d7e3bc,ccc1d9,b7dde8,fbd5b5,' +
  24128. 'bfbfbf,3f3f3f,938953,548dd4,95b3d7,d99694,c3d69b,b2a2c7,92cddc,fac08f,' +
  24129. 'a5a5a5,262626,494429,17365d,366092,953734,76923c,5f497a,31859b,e36c09,' +
  24130. '7f7f7f,0c0c0c,1d1b10,0f243e,244061,632423,4f6128,3f3151,205867,974806,' +
  24131. 'c00000,ff0000,ffc000,ffff00,92d050,00b050,00b0f0,0070c0,002060,7030a0,').split(',');
  24132. function genColorPicker(noColorText, editor) {
  24133. var html = '<div id="##" class="edui-colorpicker %%">' +
  24134. '<div class="edui-colorpicker-topbar edui-clearfix">' +
  24135. '<div unselectable="on" id="##_preview" class="edui-colorpicker-preview"></div>' +
  24136. '<div unselectable="on" class="edui-colorpicker-nocolor" onclick="$$._onPickNoColor(event, this);">' + noColorText + '</div>' +
  24137. '</div>' +
  24138. '<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">' +
  24139. '<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>' +
  24140. '<tr class="edui-colorpicker-tablefirstrow" >';
  24141. for (var i = 0; i < COLORS.length; i++) {
  24142. if (i && i % 10 === 0) {
  24143. 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"' : '') + '>';
  24144. }
  24145. 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"' +
  24146. ' data-color="#' + COLORS[i] + '"' +
  24147. ' style="background-color:#' + COLORS[i] + ';border:solid #ccc;' +
  24148. (i < 10 || i >= 60 ? 'border-width:1px;' :
  24149. i >= 10 && i < 20 ? 'border-width:1px 1px 0 1px;' :
  24150. 'border-width:0 1px 0 1px;') +
  24151. '"' +
  24152. '></a></td>' : '';
  24153. }
  24154. html += '</tr></table></div>';
  24155. return html;
  24156. }
  24157. })();
  24158. // ui/tablepicker.js
  24159. ///import core
  24160. ///import uicore
  24161. (function () {
  24162. var utils = baidu.editor.utils,
  24163. uiUtils = baidu.editor.ui.uiUtils,
  24164. UIBase = baidu.editor.ui.UIBase;
  24165. var TablePicker = baidu.editor.ui.TablePicker = function (options) {
  24166. this.initOptions(options);
  24167. this.initTablePicker();
  24168. };
  24169. TablePicker.prototype = {
  24170. defaultNumRows: 10,
  24171. defaultNumCols: 10,
  24172. maxNumRows: 20,
  24173. maxNumCols: 20,
  24174. numRows: 10,
  24175. numCols: 10,
  24176. lengthOfCellSide: 22,
  24177. initTablePicker: function () {
  24178. this.initUIBase();
  24179. },
  24180. getHtmlTpl: function () {
  24181. var me = this;
  24182. return '<div id="##" class="edui-tablepicker %%">' +
  24183. '<div class="edui-tablepicker-body">' +
  24184. '<div class="edui-infoarea">' +
  24185. '<span id="##_label" class="edui-label"></span>' +
  24186. '</div>' +
  24187. '<div class="edui-pickarea"' +
  24188. ' onmousemove="$$._onMouseMove(event, this);"' +
  24189. ' onmouseover="$$._onMouseOver(event, this);"' +
  24190. ' onmouseout="$$._onMouseOut(event, this);"' +
  24191. ' onclick="$$._onClick(event, this);"' +
  24192. '>' +
  24193. '<div id="##_overlay" class="edui-overlay"></div>' +
  24194. '</div>' +
  24195. '</div>' +
  24196. '</div>';
  24197. },
  24198. _UIBase_render: UIBase.prototype.render,
  24199. render: function (holder) {
  24200. this._UIBase_render(holder);
  24201. this.getDom('label').innerHTML = '0' + this.editor.getLang("t_row") + ' x 0' + this.editor.getLang("t_col");
  24202. },
  24203. _track: function (numCols, numRows) {
  24204. var style = this.getDom('overlay').style;
  24205. var sideLen = this.lengthOfCellSide;
  24206. style.width = numCols * sideLen + 'px';
  24207. style.height = numRows * sideLen + 'px';
  24208. var label = this.getDom('label');
  24209. label.innerHTML = numCols + this.editor.getLang("t_col") + ' x ' + numRows + this.editor.getLang("t_row");
  24210. this.numCols = numCols;
  24211. this.numRows = numRows;
  24212. },
  24213. _onMouseOver: function (evt, el) {
  24214. var rel = evt.relatedTarget || evt.fromElement;
  24215. if (!uiUtils.contains(el, rel) && el !== rel) {
  24216. this.getDom('label').innerHTML = '0' + this.editor.getLang("t_col") + ' x 0' + this.editor.getLang("t_row");
  24217. this.getDom('overlay').style.visibility = '';
  24218. }
  24219. },
  24220. _onMouseOut: function (evt, el) {
  24221. var rel = evt.relatedTarget || evt.toElement;
  24222. if (!uiUtils.contains(el, rel) && el !== rel) {
  24223. this.getDom('label').innerHTML = '0' + this.editor.getLang("t_col") + ' x 0' + this.editor.getLang("t_row");
  24224. this.getDom('overlay').style.visibility = 'hidden';
  24225. }
  24226. },
  24227. _onMouseMove: function (evt, el) {
  24228. var style = this.getDom('overlay').style;
  24229. var offset = uiUtils.getEventOffset(evt);
  24230. var sideLen = this.lengthOfCellSide;
  24231. var numCols = Math.ceil(offset.left / sideLen);
  24232. var numRows = Math.ceil(offset.top / sideLen);
  24233. this._track(numCols, numRows);
  24234. },
  24235. _onClick: function () {
  24236. this.fireEvent('picktable', this.numCols, this.numRows);
  24237. }
  24238. };
  24239. utils.inherits(TablePicker, UIBase);
  24240. })();
  24241. // ui/stateful.js
  24242. (function () {
  24243. var browser = baidu.editor.browser,
  24244. domUtils = baidu.editor.dom.domUtils,
  24245. uiUtils = baidu.editor.ui.uiUtils;
  24246. var TPL_STATEFUL = 'onmousedown="$$.Stateful_onMouseDown(event, this);"' +
  24247. ' onmouseup="$$.Stateful_onMouseUp(event, this);"' +
  24248. (browser.ie ? (
  24249. ' onmouseenter="$$.Stateful_onMouseEnter(event, this);"' +
  24250. ' onmouseleave="$$.Stateful_onMouseLeave(event, this);"') :
  24251. (
  24252. ' onmouseover="$$.Stateful_onMouseOver(event, this);"' +
  24253. ' onmouseout="$$.Stateful_onMouseOut(event, this);"'));
  24254. baidu.editor.ui.Stateful = {
  24255. alwalysHoverable: false,
  24256. target: null, //目标元素和this指向dom不一样
  24257. Stateful_init: function () {
  24258. this._Stateful_dGetHtmlTpl = this.getHtmlTpl;
  24259. this.getHtmlTpl = this.Stateful_getHtmlTpl;
  24260. },
  24261. Stateful_getHtmlTpl: function () {
  24262. var tpl = this._Stateful_dGetHtmlTpl();
  24263. // 使用function避免$转义
  24264. return tpl.replace(/stateful/g, function () {
  24265. return TPL_STATEFUL;
  24266. });
  24267. },
  24268. Stateful_onMouseEnter: function (evt, el) {
  24269. this.target = el;
  24270. if (!this.isDisabled() || this.alwalysHoverable) {
  24271. this.addState('hover');
  24272. this.fireEvent('over');
  24273. }
  24274. },
  24275. Stateful_onMouseLeave: function (evt, el) {
  24276. if (!this.isDisabled() || this.alwalysHoverable) {
  24277. this.removeState('hover');
  24278. this.removeState('active');
  24279. this.fireEvent('out');
  24280. }
  24281. },
  24282. Stateful_onMouseOver: function (evt, el) {
  24283. var rel = evt.relatedTarget;
  24284. if (!uiUtils.contains(el, rel) && el !== rel) {
  24285. this.Stateful_onMouseEnter(evt, el);
  24286. }
  24287. },
  24288. Stateful_onMouseOut: function (evt, el) {
  24289. var rel = evt.relatedTarget;
  24290. if (!uiUtils.contains(el, rel) && el !== rel) {
  24291. this.Stateful_onMouseLeave(evt, el);
  24292. }
  24293. },
  24294. Stateful_onMouseDown: function (evt, el) {
  24295. if (!this.isDisabled()) {
  24296. this.addState('active');
  24297. }
  24298. },
  24299. Stateful_onMouseUp: function (evt, el) {
  24300. if (!this.isDisabled()) {
  24301. this.removeState('active');
  24302. }
  24303. },
  24304. Stateful_postRender: function () {
  24305. if (this.disabled && !this.hasState('disabled')) {
  24306. this.addState('disabled');
  24307. }
  24308. },
  24309. hasState: function (state) {
  24310. return domUtils.hasClass(this.getStateDom(), 'edui-state-' + state);
  24311. },
  24312. addState: function (state) {
  24313. if (!this.hasState(state)) {
  24314. this.getStateDom().className += ' edui-state-' + state;
  24315. }
  24316. },
  24317. removeState: function (state) {
  24318. if (this.hasState(state)) {
  24319. domUtils.removeClasses(this.getStateDom(), ['edui-state-' + state]);
  24320. }
  24321. },
  24322. getStateDom: function () {
  24323. return this.getDom('state');
  24324. },
  24325. isChecked: function () {
  24326. return this.hasState('checked');
  24327. },
  24328. setChecked: function (checked) {
  24329. if (!this.isDisabled() && checked) {
  24330. this.addState('checked');
  24331. } else {
  24332. this.removeState('checked');
  24333. }
  24334. },
  24335. isDisabled: function () {
  24336. return this.hasState('disabled');
  24337. },
  24338. setDisabled: function (disabled) {
  24339. if (disabled) {
  24340. this.removeState('hover');
  24341. this.removeState('checked');
  24342. this.removeState('active');
  24343. this.addState('disabled');
  24344. } else {
  24345. this.removeState('disabled');
  24346. }
  24347. }
  24348. };
  24349. })();
  24350. // ui/button.js
  24351. ///import core
  24352. ///import uicore
  24353. ///import ui/stateful.js
  24354. (function () {
  24355. var utils = baidu.editor.utils,
  24356. UIBase = baidu.editor.ui.UIBase,
  24357. Stateful = baidu.editor.ui.Stateful,
  24358. Button = baidu.editor.ui.Button = function (options) {
  24359. if (options.name) {
  24360. var btnName = options.name;
  24361. var cssRules = options.cssRules;
  24362. if (!options.className) {
  24363. options.className = 'edui-for-' + btnName;
  24364. }
  24365. options.cssRules = '.edui-default .edui-for-' + btnName + ' .edui-icon {' + cssRules + '}'
  24366. }
  24367. this.initOptions(options);
  24368. this.initButton();
  24369. };
  24370. Button.prototype = {
  24371. uiName: 'button',
  24372. label: '',
  24373. title: '',
  24374. showIcon: true,
  24375. showText: true,
  24376. cssRules: '',
  24377. initButton: function () {
  24378. this.initUIBase();
  24379. this.Stateful_init();
  24380. if (this.cssRules) {
  24381. utils.cssRule('edui-customize-' + this.name + '-style', this.cssRules);
  24382. }
  24383. },
  24384. getHtmlTpl: function () {
  24385. return '<div id="##" class="edui-box %%">' +
  24386. '<div id="##_state" stateful>' +
  24387. '<div class="%%-wrap"><div id="##_body" unselectable="on" ' + (this.title ? 'title="' + this.title + '"' : '') +
  24388. ' class="%%-body" onmousedown="return $$._onMouseDown(event, this);" onclick="return $$._onClick(event, this);">' +
  24389. (this.showIcon ? '<div class="edui-box edui-icon"></div>' : '') +
  24390. (this.showText ? '<div class="edui-box edui-label">' + this.label + '</div>' : '') +
  24391. '</div>' +
  24392. '</div>' +
  24393. '</div></div>';
  24394. },
  24395. postRender: function () {
  24396. this.Stateful_postRender();
  24397. this.setDisabled(this.disabled)
  24398. },
  24399. _onMouseDown: function (e) {
  24400. var target = e.target || e.srcElement,
  24401. tagName = target && target.tagName && target.tagName.toLowerCase();
  24402. if (tagName == 'input' || tagName == 'object' || tagName == 'object') {
  24403. return false;
  24404. }
  24405. },
  24406. _onClick: function () {
  24407. if (!this.isDisabled()) {
  24408. this.fireEvent('click');
  24409. }
  24410. },
  24411. setTitle: function (text) {
  24412. var label = this.getDom('label');
  24413. label.innerHTML = text;
  24414. }
  24415. };
  24416. utils.inherits(Button, UIBase);
  24417. utils.extend(Button.prototype, Stateful);
  24418. })();
  24419. // ui/splitbutton.js
  24420. ///import core
  24421. ///import uicore
  24422. ///import ui/stateful.js
  24423. (function () {
  24424. var utils = baidu.editor.utils,
  24425. uiUtils = baidu.editor.ui.uiUtils,
  24426. domUtils = baidu.editor.dom.domUtils,
  24427. UIBase = baidu.editor.ui.UIBase,
  24428. Stateful = baidu.editor.ui.Stateful,
  24429. SplitButton = baidu.editor.ui.SplitButton = function (options) {
  24430. this.initOptions(options);
  24431. this.initSplitButton();
  24432. };
  24433. SplitButton.prototype = {
  24434. popup: null,
  24435. uiName: 'splitbutton',
  24436. title: '',
  24437. initSplitButton: function () {
  24438. this.initUIBase();
  24439. this.Stateful_init();
  24440. var me = this;
  24441. if (this.popup != null) {
  24442. var popup = this.popup;
  24443. this.popup = null;
  24444. this.setPopup(popup);
  24445. }
  24446. },
  24447. _UIBase_postRender: UIBase.prototype.postRender,
  24448. postRender: function () {
  24449. this.Stateful_postRender();
  24450. this._UIBase_postRender();
  24451. },
  24452. setPopup: function (popup) {
  24453. if (this.popup === popup) return;
  24454. if (this.popup != null) {
  24455. this.popup.dispose();
  24456. }
  24457. popup.addListener('show', utils.bind(this._onPopupShow, this));
  24458. popup.addListener('hide', utils.bind(this._onPopupHide, this));
  24459. popup.addListener('postrender', utils.bind(function () {
  24460. popup.getDom('body').appendChild(
  24461. uiUtils.createElementByHtml('<div id="' +
  24462. this.popup.id + '_bordereraser" class="edui-bordereraser edui-background" style="width:' +
  24463. (uiUtils.getClientRect(this.getDom()).width + 20) + 'px"></div>')
  24464. );
  24465. popup.getDom().className += ' ' + this.className;
  24466. }, this));
  24467. this.popup = popup;
  24468. },
  24469. _onPopupShow: function () {
  24470. this.addState('opened');
  24471. },
  24472. _onPopupHide: function () {
  24473. this.removeState('opened');
  24474. },
  24475. getHtmlTpl: function () {
  24476. return '<div id="##" class="edui-box %%">' +
  24477. '<div ' + (this.title ? 'title="' + this.title + '"' : '') + ' id="##_state" stateful><div class="%%-body">' +
  24478. '<div id="##_button_body" class="edui-box edui-button-body" onclick="$$._onButtonClick(event, this);">' +
  24479. '<div class="edui-box edui-icon"></div>' +
  24480. '</div>' +
  24481. '<div class="edui-box edui-splitborder"></div>' +
  24482. '<div class="edui-box edui-arrow" onclick="$$._onArrowClick();"></div>' +
  24483. '</div></div></div>';
  24484. },
  24485. showPopup: function () {
  24486. // 当popup往上弹出的时候,做特殊处理
  24487. var rect = uiUtils.getClientRect(this.getDom());
  24488. rect.top -= this.popup.SHADOW_RADIUS;
  24489. rect.height += this.popup.SHADOW_RADIUS;
  24490. this.popup.showAnchorRect(rect);
  24491. },
  24492. _onArrowClick: function (event, el) {
  24493. if (!this.isDisabled()) {
  24494. this.showPopup();
  24495. }
  24496. },
  24497. _onButtonClick: function () {
  24498. if (!this.isDisabled()) {
  24499. this.fireEvent('buttonclick');
  24500. }
  24501. }
  24502. };
  24503. utils.inherits(SplitButton, UIBase);
  24504. utils.extend(SplitButton.prototype, Stateful, true);
  24505. })();
  24506. // ui/colorbutton.js
  24507. ///import core
  24508. ///import uicore
  24509. ///import ui/colorpicker.js
  24510. ///import ui/popup.js
  24511. ///import ui/splitbutton.js
  24512. (function () {
  24513. var utils = baidu.editor.utils,
  24514. uiUtils = baidu.editor.ui.uiUtils,
  24515. ColorPicker = baidu.editor.ui.ColorPicker,
  24516. Popup = baidu.editor.ui.Popup,
  24517. SplitButton = baidu.editor.ui.SplitButton,
  24518. ColorButton = baidu.editor.ui.ColorButton = function (options) {
  24519. this.initOptions(options);
  24520. this.initColorButton();
  24521. };
  24522. ColorButton.prototype = {
  24523. initColorButton: function () {
  24524. var me = this;
  24525. this.popup = new Popup({
  24526. content: new ColorPicker({
  24527. noColorText: me.editor.getLang("clearColor"),
  24528. editor: me.editor,
  24529. onpickcolor: function (t, color) {
  24530. me._onPickColor(color);
  24531. },
  24532. onpicknocolor: function (t, color) {
  24533. me._onPickNoColor(color);
  24534. }
  24535. }),
  24536. editor: me.editor
  24537. });
  24538. this.initSplitButton();
  24539. },
  24540. _SplitButton_postRender: SplitButton.prototype.postRender,
  24541. postRender: function () {
  24542. this._SplitButton_postRender();
  24543. this.getDom('button_body').appendChild(
  24544. uiUtils.createElementByHtml('<div id="' + this.id + '_colorlump" class="edui-colorlump"></div>')
  24545. );
  24546. this.getDom().className += ' edui-colorbutton';
  24547. },
  24548. setColor: function (color) {
  24549. this.getDom('colorlump').style.backgroundColor = color;
  24550. this.color = color;
  24551. },
  24552. _onPickColor: function (color) {
  24553. if (this.fireEvent('pickcolor', color) !== false) {
  24554. this.setColor(color);
  24555. this.popup.hide();
  24556. }
  24557. },
  24558. _onPickNoColor: function (color) {
  24559. if (this.fireEvent('picknocolor') !== false) {
  24560. this.popup.hide();
  24561. }
  24562. }
  24563. };
  24564. utils.inherits(ColorButton, SplitButton);
  24565. })();
  24566. // ui/tablebutton.js
  24567. ///import core
  24568. ///import uicore
  24569. ///import ui/popup.js
  24570. ///import ui/tablepicker.js
  24571. ///import ui/splitbutton.js
  24572. (function () {
  24573. var utils = baidu.editor.utils,
  24574. Popup = baidu.editor.ui.Popup,
  24575. TablePicker = baidu.editor.ui.TablePicker,
  24576. SplitButton = baidu.editor.ui.SplitButton,
  24577. TableButton = baidu.editor.ui.TableButton = function (options) {
  24578. this.initOptions(options);
  24579. this.initTableButton();
  24580. };
  24581. TableButton.prototype = {
  24582. initTableButton: function () {
  24583. var me = this;
  24584. this.popup = new Popup({
  24585. content: new TablePicker({
  24586. editor: me.editor,
  24587. onpicktable: function (t, numCols, numRows) {
  24588. me._onPickTable(numCols, numRows);
  24589. }
  24590. }),
  24591. 'editor': me.editor
  24592. });
  24593. this.initSplitButton();
  24594. },
  24595. _onPickTable: function (numCols, numRows) {
  24596. if (this.fireEvent('picktable', numCols, numRows) !== false) {
  24597. this.popup.hide();
  24598. }
  24599. }
  24600. };
  24601. utils.inherits(TableButton, SplitButton);
  24602. })();
  24603. // ui/autotypesetpicker.js
  24604. ///import core
  24605. ///import uicore
  24606. (function () {
  24607. var utils = baidu.editor.utils,
  24608. UIBase = baidu.editor.ui.UIBase;
  24609. var AutoTypeSetPicker = baidu.editor.ui.AutoTypeSetPicker = function (options) {
  24610. this.initOptions(options);
  24611. this.initAutoTypeSetPicker();
  24612. };
  24613. AutoTypeSetPicker.prototype = {
  24614. initAutoTypeSetPicker: function () {
  24615. this.initUIBase();
  24616. },
  24617. getHtmlTpl: function () {
  24618. var me = this.editor,
  24619. opt = me.options.autotypeset,
  24620. lang = me.getLang("autoTypeSet");
  24621. var textAlignInputName = 'textAlignValue' + me.uid,
  24622. imageBlockInputName = 'imageBlockLineValue' + me.uid,
  24623. symbolConverInputName = 'symbolConverValue' + me.uid;
  24624. return '<div id="##" class="edui-autotypesetpicker %%">' +
  24625. '<div class="edui-autotypesetpicker-body">' +
  24626. '<table >' +
  24627. '<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>' +
  24628. '<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>' +
  24629. '<tr>' +
  24630. '<td nowrap><input type="checkbox" name="textAlign" ' + (opt["textAlign"] ? "checked" : "") + '>' + lang.alignment + '</td>' +
  24631. '<td colspan="2" id="' + textAlignInputName + '">' +
  24632. '<input type="radio" name="' + textAlignInputName + '" value="left" ' + ((opt["textAlign"] && opt["textAlign"] == "left") ? "checked" : "") + '>' + me.getLang("justifyleft") +
  24633. '<input type="radio" name="' + textAlignInputName + '" value="center" ' + ((opt["textAlign"] && opt["textAlign"] == "center") ? "checked" : "") + '>' + me.getLang("justifycenter") +
  24634. '<input type="radio" name="' + textAlignInputName + '" value="right" ' + ((opt["textAlign"] && opt["textAlign"] == "right") ? "checked" : "") + '>' + me.getLang("justifyright") +
  24635. '</td>' +
  24636. '</tr>' +
  24637. '<tr>' +
  24638. '<td nowrap><input type="checkbox" name="imageBlockLine" ' + (opt["imageBlockLine"] ? "checked" : "") + '>' + lang.imageFloat + '</td>' +
  24639. '<td nowrap id="' + imageBlockInputName + '">' +
  24640. '<input type="radio" name="' + imageBlockInputName + '" value="none" ' + ((opt["imageBlockLine"] && opt["imageBlockLine"] == "none") ? "checked" : "") + '>' + me.getLang("default") +
  24641. '<input type="radio" name="' + imageBlockInputName + '" value="left" ' + ((opt["imageBlockLine"] && opt["imageBlockLine"] == "left") ? "checked" : "") + '>' + me.getLang("justifyleft") +
  24642. '<input type="radio" name="' + imageBlockInputName + '" value="center" ' + ((opt["imageBlockLine"] && opt["imageBlockLine"] == "center") ? "checked" : "") + '>' + me.getLang("justifycenter") +
  24643. '<input type="radio" name="' + imageBlockInputName + '" value="right" ' + ((opt["imageBlockLine"] && opt["imageBlockLine"] == "right") ? "checked" : "") + '>' + me.getLang("justifyright") +
  24644. '</td>' +
  24645. '</tr>' +
  24646. '<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>' +
  24647. '<tr><td nowrap colspan="3"><input type="checkbox" name="removeEmptyNode" ' + (opt["removeEmptyNode"] ? "checked" : "") + '>' + lang.removeHtml + '</td></tr>' +
  24648. '<tr><td nowrap colspan="3"><input type="checkbox" name="pasteFilter" ' + (opt["pasteFilter"] ? "checked" : "") + '>' + lang.pasteFilter + '</td></tr>' +
  24649. '<tr>' +
  24650. '<td nowrap><input type="checkbox" name="symbolConver" ' + (opt["bdc2sb"] || opt["tobdc"] ? "checked" : "") + '>' + lang.symbol + '</td>' +
  24651. '<td id="' + symbolConverInputName + '">' +
  24652. '<input type="radio" name="bdc" value="bdc2sb" ' + (opt["bdc2sb"] ? "checked" : "") + '>' + lang.bdc2sb +
  24653. '<input type="radio" name="bdc" value="tobdc" ' + (opt["tobdc"] ? "checked" : "") + '>' + lang.tobdc + '' +
  24654. '</td>' +
  24655. '<td nowrap align="right"><button >' + lang.run + '</button></td>' +
  24656. '</tr>' +
  24657. '</table>' +
  24658. '</div>' +
  24659. '</div>';
  24660. },
  24661. _UIBase_render: UIBase.prototype.render
  24662. };
  24663. utils.inherits(AutoTypeSetPicker, UIBase);
  24664. })();
  24665. // ui/autotypesetbutton.js
  24666. ///import core
  24667. ///import uicore
  24668. ///import ui/popup.js
  24669. ///import ui/autotypesetpicker.js
  24670. ///import ui/splitbutton.js
  24671. (function () {
  24672. var utils = baidu.editor.utils,
  24673. Popup = baidu.editor.ui.Popup,
  24674. AutoTypeSetPicker = baidu.editor.ui.AutoTypeSetPicker,
  24675. SplitButton = baidu.editor.ui.SplitButton,
  24676. AutoTypeSetButton = baidu.editor.ui.AutoTypeSetButton = function (options) {
  24677. this.initOptions(options);
  24678. this.initAutoTypeSetButton();
  24679. };
  24680. function getPara(me) {
  24681. var opt = {},
  24682. cont = me.getDom(),
  24683. editorId = me.editor.uid,
  24684. inputType = null,
  24685. attrName = null,
  24686. ipts = domUtils.getElementsByTagName(cont, "input");
  24687. for (var i = ipts.length - 1, ipt; ipt = ipts[i--];) {
  24688. inputType = ipt.getAttribute("type");
  24689. if (inputType == "checkbox") {
  24690. attrName = ipt.getAttribute("name");
  24691. opt[attrName] && delete opt[attrName];
  24692. if (ipt.checked) {
  24693. var attrValue = document.getElementById(attrName + "Value" + editorId);
  24694. if (attrValue) {
  24695. if (/input/ig.test(attrValue.tagName)) {
  24696. opt[attrName] = attrValue.value;
  24697. } else {
  24698. var iptChilds = attrValue.getElementsByTagName("input");
  24699. for (var j = iptChilds.length - 1, iptchild; iptchild = iptChilds[j--];) {
  24700. if (iptchild.checked) {
  24701. opt[attrName] = iptchild.value;
  24702. break;
  24703. }
  24704. }
  24705. }
  24706. } else {
  24707. opt[attrName] = true;
  24708. }
  24709. } else {
  24710. opt[attrName] = false;
  24711. }
  24712. } else {
  24713. opt[ipt.getAttribute("value")] = ipt.checked;
  24714. }
  24715. }
  24716. var selects = domUtils.getElementsByTagName(cont, "select");
  24717. for (var i = 0, si; si = selects[i++];) {
  24718. var attr = si.getAttribute('name');
  24719. opt[attr] = opt[attr] ? si.value : '';
  24720. }
  24721. utils.extend(me.editor.options.autotypeset, opt);
  24722. me.editor.setPreferences('autotypeset', opt);
  24723. }
  24724. AutoTypeSetButton.prototype = {
  24725. initAutoTypeSetButton: function () {
  24726. var me = this;
  24727. this.popup = new Popup({
  24728. //传入配置参数
  24729. content: new AutoTypeSetPicker({
  24730. editor: me.editor
  24731. }),
  24732. 'editor': me.editor,
  24733. hide: function () {
  24734. if (!this._hidden && this.getDom()) {
  24735. getPara(this);
  24736. this.getDom().style.display = 'none';
  24737. this._hidden = true;
  24738. this.fireEvent('hide');
  24739. }
  24740. }
  24741. });
  24742. var flag = 0;
  24743. this.popup.addListener('postRenderAfter', function () {
  24744. var popupUI = this;
  24745. if (flag) return;
  24746. var cont = this.getDom(),
  24747. btn = cont.getElementsByTagName('button')[0];
  24748. btn.onclick = function () {
  24749. getPara(popupUI);
  24750. me.editor.execCommand('autotypeset');
  24751. popupUI.hide()
  24752. };
  24753. domUtils.on(cont, 'click', function (e) {
  24754. var target = e.target || e.srcElement,
  24755. editorId = me.editor.uid;
  24756. if (target && target.tagName == 'INPUT') {
  24757. // 点击图片浮动的checkbox,去除对应的radio
  24758. if (target.name == 'imageBlockLine' || target.name == 'textAlign' || target.name == 'symbolConver') {
  24759. var checked = target.checked,
  24760. radioTd = document.getElementById(target.name + 'Value' + editorId),
  24761. radios = radioTd.getElementsByTagName('input'),
  24762. defalutSelect = {
  24763. 'imageBlockLine': 'none',
  24764. 'textAlign': 'left',
  24765. 'symbolConver': 'tobdc'
  24766. };
  24767. for (var i = 0; i < radios.length; i++) {
  24768. if (checked) {
  24769. if (radios[i].value == defalutSelect[target.name]) {
  24770. radios[i].checked = 'checked';
  24771. }
  24772. } else {
  24773. radios[i].checked = false;
  24774. }
  24775. }
  24776. }
  24777. // 点击radio,选中对应的checkbox
  24778. if (target.name == ('imageBlockLineValue' + editorId) || target.name == ('textAlignValue' + editorId) || target.name == 'bdc') {
  24779. var checkboxs = target.parentNode.previousSibling.getElementsByTagName('input');
  24780. checkboxs && (checkboxs[0].checked = true);
  24781. }
  24782. getPara(popupUI);
  24783. }
  24784. });
  24785. flag = 1;
  24786. });
  24787. this.initSplitButton();
  24788. }
  24789. };
  24790. utils.inherits(AutoTypeSetButton, SplitButton);
  24791. })();
  24792. // ui/cellalignpicker.js
  24793. ///import core
  24794. ///import uicore
  24795. (function () {
  24796. var utils = baidu.editor.utils,
  24797. Popup = baidu.editor.ui.Popup,
  24798. Stateful = baidu.editor.ui.Stateful,
  24799. UIBase = baidu.editor.ui.UIBase;
  24800. /**
  24801. * 该参数将新增一个参数: selected, 参数类型为一个Object, 形如{ 'align': 'center', 'valign': 'top' }, 表示单元格的初始
  24802. * 对齐状态为: 竖直居上,水平居中; 其中 align的取值为:'center', 'left', 'right'; valign的取值为: 'top', 'middle', 'bottom'
  24803. * @update 2013/4/2 hancong03@baidu.com
  24804. */
  24805. var CellAlignPicker = baidu.editor.ui.CellAlignPicker = function (options) {
  24806. this.initOptions(options);
  24807. this.initSelected();
  24808. this.initCellAlignPicker();
  24809. };
  24810. CellAlignPicker.prototype = {
  24811. //初始化选中状态, 该方法将根据传递进来的参数获取到应该选中的对齐方式图标的索引
  24812. initSelected: function () {
  24813. var status = {
  24814. valign: {
  24815. top: 0,
  24816. middle: 1,
  24817. bottom: 2
  24818. },
  24819. align: {
  24820. left: 0,
  24821. center: 1,
  24822. right: 2
  24823. },
  24824. count: 3
  24825. },
  24826. result = -1;
  24827. if (this.selected) {
  24828. this.selectedIndex = status.valign[this.selected.valign] * status.count + status.align[this.selected.align];
  24829. }
  24830. },
  24831. initCellAlignPicker: function () {
  24832. this.initUIBase();
  24833. this.Stateful_init();
  24834. },
  24835. getHtmlTpl: function () {
  24836. var alignType = ['left', 'center', 'right'],
  24837. COUNT = 9,
  24838. tempClassName = null,
  24839. tempIndex = -1,
  24840. tmpl = [];
  24841. for (var i = 0; i < COUNT; i++) {
  24842. tempClassName = this.selectedIndex === i ? ' class="edui-cellalign-selected" ' : '';
  24843. tempIndex = i % 3;
  24844. tempIndex === 0 && tmpl.push('<tr>');
  24845. tmpl.push('<td index="' + i + '" ' + tempClassName + ' stateful><div class="edui-icon edui-' + alignType[tempIndex] + '"></div></td>');
  24846. tempIndex === 2 && tmpl.push('</tr>');
  24847. }
  24848. return '<div id="##" class="edui-cellalignpicker %%">' +
  24849. '<div class="edui-cellalignpicker-body">' +
  24850. '<table onclick="$$._onClick(event);">' +
  24851. tmpl.join('') +
  24852. '</table>' +
  24853. '</div>' +
  24854. '</div>';
  24855. },
  24856. getStateDom: function () {
  24857. return this.target;
  24858. },
  24859. _onClick: function (evt) {
  24860. var target = evt.target || evt.srcElement;
  24861. if (/icon/.test(target.className)) {
  24862. this.items[target.parentNode.getAttribute("index")].onclick();
  24863. Popup.postHide(evt);
  24864. }
  24865. },
  24866. _UIBase_render: UIBase.prototype.render
  24867. };
  24868. utils.inherits(CellAlignPicker, UIBase);
  24869. utils.extend(CellAlignPicker.prototype, Stateful, true);
  24870. })();
  24871. // ui/pastepicker.js
  24872. ///import core
  24873. ///import uicore
  24874. (function () {
  24875. var utils = baidu.editor.utils,
  24876. Stateful = baidu.editor.ui.Stateful,
  24877. uiUtils = baidu.editor.ui.uiUtils,
  24878. UIBase = baidu.editor.ui.UIBase;
  24879. var PastePicker = baidu.editor.ui.PastePicker = function (options) {
  24880. this.initOptions(options);
  24881. this.initPastePicker();
  24882. };
  24883. PastePicker.prototype = {
  24884. initPastePicker: function () {
  24885. this.initUIBase();
  24886. this.Stateful_init();
  24887. },
  24888. getHtmlTpl: function () {
  24889. return '<div class="edui-pasteicon" onclick="$$._onClick(this)"></div>' +
  24890. '<div class="edui-pastecontainer">' +
  24891. '<div class="edui-title">' + this.editor.getLang("pasteOpt") + '</div>' +
  24892. '<div class="edui-button">' +
  24893. '<div title="' + this.editor.getLang("pasteSourceFormat") + '" onclick="$$.format(false)" stateful>' +
  24894. '<div class="edui-richtxticon"></div></div>' +
  24895. '<div title="' + this.editor.getLang("tagFormat") + '" onclick="$$.format(2)" stateful>' +
  24896. '<div class="edui-tagicon"></div></div>' +
  24897. '<div title="' + this.editor.getLang("pasteTextFormat") + '" onclick="$$.format(true)" stateful>' +
  24898. '<div class="edui-plaintxticon"></div></div>' +
  24899. '</div>' +
  24900. '</div>' +
  24901. '</div>'
  24902. },
  24903. getStateDom: function () {
  24904. return this.target;
  24905. },
  24906. format: function (param) {
  24907. this.editor.ui._isTransfer = true;
  24908. this.editor.fireEvent('pasteTransfer', param);
  24909. },
  24910. _onClick: function (cur) {
  24911. var node = domUtils.getNextDomNode(cur),
  24912. screenHt = uiUtils.getViewportRect().height,
  24913. subPop = uiUtils.getClientRect(node);
  24914. if ((subPop.top + subPop.height) > screenHt)
  24915. node.style.top = (-subPop.height - cur.offsetHeight) + "px";
  24916. else
  24917. node.style.top = "";
  24918. if (/hidden/ig.test(domUtils.getComputedStyle(node, "visibility"))) {
  24919. node.style.visibility = "visible";
  24920. domUtils.addClass(cur, "edui-state-opened");
  24921. } else {
  24922. node.style.visibility = "hidden";
  24923. domUtils.removeClasses(cur, "edui-state-opened")
  24924. }
  24925. },
  24926. _UIBase_render: UIBase.prototype.render
  24927. };
  24928. utils.inherits(PastePicker, UIBase);
  24929. utils.extend(PastePicker.prototype, Stateful, true);
  24930. })();
  24931. // ui/toolbar.js
  24932. (function () {
  24933. var utils = baidu.editor.utils,
  24934. uiUtils = baidu.editor.ui.uiUtils,
  24935. UIBase = baidu.editor.ui.UIBase,
  24936. Toolbar = baidu.editor.ui.Toolbar = function (options) {
  24937. this.initOptions(options);
  24938. this.initToolbar();
  24939. };
  24940. Toolbar.prototype = {
  24941. items: null,
  24942. initToolbar: function () {
  24943. this.items = this.items || [];
  24944. this.initUIBase();
  24945. },
  24946. add: function (item, index) {
  24947. if (index === undefined) {
  24948. this.items.push(item);
  24949. } else {
  24950. this.items.splice(index, 0, item)
  24951. }
  24952. },
  24953. getHtmlTpl: function () {
  24954. var buff = [];
  24955. for (var i = 0; i < this.items.length; i++) {
  24956. buff[i] = this.items[i].renderHtml();
  24957. }
  24958. return '<div id="##" class="edui-toolbar %%" onselectstart="return false;" onmousedown="return $$._onMouseDown(event, this);">' +
  24959. buff.join('') +
  24960. '</div>'
  24961. },
  24962. postRender: function () {
  24963. var box = this.getDom();
  24964. for (var i = 0; i < this.items.length; i++) {
  24965. this.items[i].postRender();
  24966. }
  24967. uiUtils.makeUnselectable(box);
  24968. },
  24969. _onMouseDown: function (e) {
  24970. var target = e.target || e.srcElement,
  24971. tagName = target && target.tagName && target.tagName.toLowerCase();
  24972. if (tagName == 'input' || tagName == 'object' || tagName == 'object') {
  24973. return false;
  24974. }
  24975. }
  24976. };
  24977. utils.inherits(Toolbar, UIBase);
  24978. })();
  24979. // ui/menu.js
  24980. ///import core
  24981. ///import uicore
  24982. ///import ui\popup.js
  24983. ///import ui\stateful.js
  24984. (function () {
  24985. var utils = baidu.editor.utils,
  24986. domUtils = baidu.editor.dom.domUtils,
  24987. uiUtils = baidu.editor.ui.uiUtils,
  24988. UIBase = baidu.editor.ui.UIBase,
  24989. Popup = baidu.editor.ui.Popup,
  24990. Stateful = baidu.editor.ui.Stateful,
  24991. CellAlignPicker = baidu.editor.ui.CellAlignPicker,
  24992. Menu = baidu.editor.ui.Menu = function (options) {
  24993. this.initOptions(options);
  24994. this.initMenu();
  24995. };
  24996. var menuSeparator = {
  24997. renderHtml: function () {
  24998. return '<div class="edui-menuitem edui-menuseparator"><div class="edui-menuseparator-inner"></div></div>';
  24999. },
  25000. postRender: function () {},
  25001. queryAutoHide: function () {
  25002. return true;
  25003. }
  25004. };
  25005. Menu.prototype = {
  25006. items: null,
  25007. uiName: 'menu',
  25008. initMenu: function () {
  25009. this.items = this.items || [];
  25010. this.initPopup();
  25011. this.initItems();
  25012. },
  25013. initItems: function () {
  25014. for (var i = 0; i < this.items.length; i++) {
  25015. var item = this.items[i];
  25016. if (item == '-') {
  25017. this.items[i] = this.getSeparator();
  25018. } else if (!(item instanceof MenuItem)) {
  25019. item.editor = this.editor;
  25020. item.theme = this.editor.options.theme;
  25021. this.items[i] = this.createItem(item);
  25022. }
  25023. }
  25024. },
  25025. getSeparator: function () {
  25026. return menuSeparator;
  25027. },
  25028. createItem: function (item) {
  25029. //新增一个参数menu, 该参数存储了menuItem所对应的menu引用
  25030. item.menu = this;
  25031. return new MenuItem(item);
  25032. },
  25033. _Popup_getContentHtmlTpl: Popup.prototype.getContentHtmlTpl,
  25034. getContentHtmlTpl: function () {
  25035. if (this.items.length == 0) {
  25036. return this._Popup_getContentHtmlTpl();
  25037. }
  25038. var buff = [];
  25039. for (var i = 0; i < this.items.length; i++) {
  25040. var item = this.items[i];
  25041. buff[i] = item.renderHtml();
  25042. }
  25043. return ('<div class="%%-body">' + buff.join('') + '</div>');
  25044. },
  25045. _Popup_postRender: Popup.prototype.postRender,
  25046. postRender: function () {
  25047. var me = this;
  25048. for (var i = 0; i < this.items.length; i++) {
  25049. var item = this.items[i];
  25050. item.ownerMenu = this;
  25051. item.postRender();
  25052. }
  25053. domUtils.on(this.getDom(), 'mouseover', function (evt) {
  25054. evt = evt || event;
  25055. var rel = evt.relatedTarget || evt.fromElement;
  25056. var el = me.getDom();
  25057. if (!uiUtils.contains(el, rel) && el !== rel) {
  25058. me.fireEvent('over');
  25059. }
  25060. });
  25061. this._Popup_postRender();
  25062. },
  25063. queryAutoHide: function (el) {
  25064. if (el) {
  25065. if (uiUtils.contains(this.getDom(), el)) {
  25066. return false;
  25067. }
  25068. for (var i = 0; i < this.items.length; i++) {
  25069. var item = this.items[i];
  25070. if (item.queryAutoHide(el) === false) {
  25071. return false;
  25072. }
  25073. }
  25074. }
  25075. },
  25076. clearItems: function () {
  25077. for (var i = 0; i < this.items.length; i++) {
  25078. var item = this.items[i];
  25079. clearTimeout(item._showingTimer);
  25080. clearTimeout(item._closingTimer);
  25081. if (item.subMenu) {
  25082. item.subMenu.destroy();
  25083. }
  25084. }
  25085. this.items = [];
  25086. },
  25087. destroy: function () {
  25088. if (this.getDom()) {
  25089. domUtils.remove(this.getDom());
  25090. }
  25091. this.clearItems();
  25092. },
  25093. dispose: function () {
  25094. this.destroy();
  25095. }
  25096. };
  25097. utils.inherits(Menu, Popup);
  25098. /**
  25099. * @update 2013/04/03 hancong03 新增一个参数menu, 该参数存储了menuItem所对应的menu引用
  25100. * @type {Function}
  25101. */
  25102. var MenuItem = baidu.editor.ui.MenuItem = function (options) {
  25103. this.initOptions(options);
  25104. this.initUIBase();
  25105. this.Stateful_init();
  25106. if (this.subMenu && !(this.subMenu instanceof Menu)) {
  25107. if (options.className && options.className.indexOf("aligntd") != -1) {
  25108. var me = this;
  25109. //获取单元格对齐初始状态
  25110. this.subMenu.selected = this.editor.queryCommandValue('cellalignment');
  25111. this.subMenu = new Popup({
  25112. content: new CellAlignPicker(this.subMenu),
  25113. parentMenu: me,
  25114. editor: me.editor,
  25115. destroy: function () {
  25116. if (this.getDom()) {
  25117. domUtils.remove(this.getDom());
  25118. }
  25119. }
  25120. });
  25121. this.subMenu.addListener("postRenderAfter", function () {
  25122. domUtils.on(this.getDom(), "mouseover", function () {
  25123. me.addState('opened');
  25124. });
  25125. });
  25126. } else {
  25127. this.subMenu = new Menu(this.subMenu);
  25128. }
  25129. }
  25130. };
  25131. MenuItem.prototype = {
  25132. label: '',
  25133. subMenu: null,
  25134. ownerMenu: null,
  25135. uiName: 'menuitem',
  25136. alwalysHoverable: true,
  25137. getHtmlTpl: function () {
  25138. return '<div id="##" class="%%" stateful onclick="$$._onClick(event, this);">' +
  25139. '<div class="%%-body">' +
  25140. this.renderLabelHtml() +
  25141. '</div>' +
  25142. '</div>';
  25143. },
  25144. postRender: function () {
  25145. var me = this;
  25146. this.addListener('over', function () {
  25147. me.ownerMenu.fireEvent('submenuover', me);
  25148. if (me.subMenu) {
  25149. me.delayShowSubMenu();
  25150. }
  25151. });
  25152. if (this.subMenu) {
  25153. this.getDom().className += ' edui-hassubmenu';
  25154. this.subMenu.render();
  25155. this.addListener('out', function () {
  25156. me.delayHideSubMenu();
  25157. });
  25158. this.subMenu.addListener('over', function () {
  25159. clearTimeout(me._closingTimer);
  25160. me._closingTimer = null;
  25161. me.addState('opened');
  25162. });
  25163. this.ownerMenu.addListener('hide', function () {
  25164. me.hideSubMenu();
  25165. });
  25166. this.ownerMenu.addListener('submenuover', function (t, subMenu) {
  25167. if (subMenu !== me) {
  25168. me.delayHideSubMenu();
  25169. }
  25170. });
  25171. this.subMenu._bakQueryAutoHide = this.subMenu.queryAutoHide;
  25172. this.subMenu.queryAutoHide = function (el) {
  25173. if (el && uiUtils.contains(me.getDom(), el)) {
  25174. return false;
  25175. }
  25176. return this._bakQueryAutoHide(el);
  25177. };
  25178. }
  25179. this.getDom().style.tabIndex = '-1';
  25180. uiUtils.makeUnselectable(this.getDom());
  25181. this.Stateful_postRender();
  25182. },
  25183. delayShowSubMenu: function () {
  25184. var me = this;
  25185. if (!me.isDisabled()) {
  25186. me.addState('opened');
  25187. clearTimeout(me._showingTimer);
  25188. clearTimeout(me._closingTimer);
  25189. me._closingTimer = null;
  25190. me._showingTimer = setTimeout(function () {
  25191. me.showSubMenu();
  25192. }, 250);
  25193. }
  25194. },
  25195. delayHideSubMenu: function () {
  25196. var me = this;
  25197. if (!me.isDisabled()) {
  25198. me.removeState('opened');
  25199. clearTimeout(me._showingTimer);
  25200. if (!me._closingTimer) {
  25201. me._closingTimer = setTimeout(function () {
  25202. if (!me.hasState('opened')) {
  25203. me.hideSubMenu();
  25204. }
  25205. me._closingTimer = null;
  25206. }, 400);
  25207. }
  25208. }
  25209. },
  25210. renderLabelHtml: function () {
  25211. return '<div class="edui-arrow"></div>' +
  25212. '<div class="edui-box edui-icon"></div>' +
  25213. '<div class="edui-box edui-label %%-label">' + (this.label || '') + '</div>';
  25214. },
  25215. getStateDom: function () {
  25216. return this.getDom();
  25217. },
  25218. queryAutoHide: function (el) {
  25219. if (this.subMenu && this.hasState('opened')) {
  25220. return this.subMenu.queryAutoHide(el);
  25221. }
  25222. },
  25223. _onClick: function (event, this_) {
  25224. if (this.hasState('disabled')) return;
  25225. if (this.fireEvent('click', event, this_) !== false) {
  25226. if (this.subMenu) {
  25227. this.showSubMenu();
  25228. } else {
  25229. Popup.postHide(event);
  25230. }
  25231. }
  25232. },
  25233. showSubMenu: function () {
  25234. var rect = uiUtils.getClientRect(this.getDom());
  25235. rect.right -= 5;
  25236. rect.left += 2;
  25237. rect.width -= 7;
  25238. rect.top -= 4;
  25239. rect.bottom += 4;
  25240. rect.height += 8;
  25241. this.subMenu.showAnchorRect(rect, true, true);
  25242. },
  25243. hideSubMenu: function () {
  25244. this.subMenu.hide();
  25245. }
  25246. };
  25247. utils.inherits(MenuItem, UIBase);
  25248. utils.extend(MenuItem.prototype, Stateful, true);
  25249. })();
  25250. // ui/combox.js
  25251. ///import core
  25252. ///import uicore
  25253. ///import ui/menu.js
  25254. ///import ui/splitbutton.js
  25255. (function () {
  25256. // todo: menu和item提成通用list
  25257. var utils = baidu.editor.utils,
  25258. uiUtils = baidu.editor.ui.uiUtils,
  25259. Menu = baidu.editor.ui.Menu,
  25260. SplitButton = baidu.editor.ui.SplitButton,
  25261. Combox = baidu.editor.ui.Combox = function (options) {
  25262. this.initOptions(options);
  25263. this.initCombox();
  25264. };
  25265. Combox.prototype = {
  25266. uiName: 'combox',
  25267. onbuttonclick: function () {
  25268. this.showPopup();
  25269. },
  25270. initCombox: function () {
  25271. var me = this;
  25272. this.items = this.items || [];
  25273. for (var i = 0; i < this.items.length; i++) {
  25274. var item = this.items[i];
  25275. item.uiName = 'listitem';
  25276. item.index = i;
  25277. item.onclick = function () {
  25278. me.selectByIndex(this.index);
  25279. };
  25280. }
  25281. this.popup = new Menu({
  25282. items: this.items,
  25283. uiName: 'list',
  25284. editor: this.editor,
  25285. captureWheel: true,
  25286. combox: this
  25287. });
  25288. this.initSplitButton();
  25289. },
  25290. _SplitButton_postRender: SplitButton.prototype.postRender,
  25291. postRender: function () {
  25292. this._SplitButton_postRender();
  25293. this.setLabel(this.label || '');
  25294. this.setValue(this.initValue || '');
  25295. },
  25296. showPopup: function () {
  25297. var rect = uiUtils.getClientRect(this.getDom());
  25298. rect.top += 1;
  25299. rect.bottom -= 1;
  25300. rect.height -= 2;
  25301. this.popup.showAnchorRect(rect);
  25302. },
  25303. getValue: function () {
  25304. return this.value;
  25305. },
  25306. setValue: function (value) {
  25307. var index = this.indexByValue(value);
  25308. if (index != -1) {
  25309. this.selectedIndex = index;
  25310. this.setLabel(this.items[index].label);
  25311. this.value = this.items[index].value;
  25312. } else {
  25313. this.selectedIndex = -1;
  25314. this.setLabel(this.getLabelForUnknowValue(value));
  25315. this.value = value;
  25316. }
  25317. },
  25318. setLabel: function (label) {
  25319. this.getDom('button_body').innerHTML = label;
  25320. this.label = label;
  25321. },
  25322. getLabelForUnknowValue: function (value) {
  25323. return value;
  25324. },
  25325. indexByValue: function (value) {
  25326. for (var i = 0; i < this.items.length; i++) {
  25327. if (value == this.items[i].value) {
  25328. return i;
  25329. }
  25330. }
  25331. return -1;
  25332. },
  25333. getItem: function (index) {
  25334. return this.items[index];
  25335. },
  25336. selectByIndex: function (index) {
  25337. if (index < this.items.length && this.fireEvent('select', index) !== false) {
  25338. this.selectedIndex = index;
  25339. this.value = this.items[index].value;
  25340. this.setLabel(this.items[index].label);
  25341. }
  25342. }
  25343. };
  25344. utils.inherits(Combox, SplitButton);
  25345. })();
  25346. // ui/dialog.js
  25347. ///import core
  25348. ///import uicore
  25349. ///import ui/mask.js
  25350. ///import ui/button.js
  25351. (function () {
  25352. var utils = baidu.editor.utils,
  25353. domUtils = baidu.editor.dom.domUtils,
  25354. uiUtils = baidu.editor.ui.uiUtils,
  25355. Mask = baidu.editor.ui.Mask,
  25356. UIBase = baidu.editor.ui.UIBase,
  25357. Button = baidu.editor.ui.Button,
  25358. Dialog = baidu.editor.ui.Dialog = function (options) {
  25359. if (options.name) {
  25360. var name = options.name;
  25361. var cssRules = options.cssRules;
  25362. if (!options.className) {
  25363. options.className = 'edui-for-' + name;
  25364. }
  25365. if (cssRules) {
  25366. options.cssRules = '.edui-default .edui-for-' + name + ' .edui-dialog-content {' + cssRules + '}'
  25367. }
  25368. }
  25369. this.initOptions(utils.extend({
  25370. autoReset: true,
  25371. draggable: true,
  25372. onok: function () {},
  25373. oncancel: function () {},
  25374. onclose: function (t, ok) {
  25375. return ok ? this.onok() : this.oncancel();
  25376. },
  25377. //是否控制dialog中的scroll事件, 默认为不阻止
  25378. holdScroll: false
  25379. }, options));
  25380. this.initDialog();
  25381. };
  25382. var modalMask;
  25383. var dragMask;
  25384. var activeDialog;
  25385. Dialog.prototype = {
  25386. draggable: false,
  25387. uiName: 'dialog',
  25388. initDialog: function () {
  25389. var me = this,
  25390. theme = this.editor.options.theme;
  25391. if (this.cssRules) {
  25392. utils.cssRule('edui-customize-' + this.name + '-style', this.cssRules);
  25393. }
  25394. this.initUIBase();
  25395. this.modalMask = (modalMask || (modalMask = new Mask({
  25396. className: 'edui-dialog-modalmask',
  25397. theme: theme,
  25398. onclick: function () {
  25399. activeDialog && activeDialog.close(false);
  25400. }
  25401. })));
  25402. this.dragMask = (dragMask || (dragMask = new Mask({
  25403. className: 'edui-dialog-dragmask',
  25404. theme: theme
  25405. })));
  25406. this.closeButton = new Button({
  25407. className: 'edui-dialog-closebutton',
  25408. title: me.closeDialog,
  25409. theme: theme,
  25410. onclick: function () {
  25411. me.close(false);
  25412. }
  25413. });
  25414. this.fullscreen && this.initResizeEvent();
  25415. if (this.buttons) {
  25416. for (var i = 0; i < this.buttons.length; i++) {
  25417. if (!(this.buttons[i] instanceof Button)) {
  25418. this.buttons[i] = new Button(utils.extend(this.buttons[i], {
  25419. editor: this.editor
  25420. }, true));
  25421. }
  25422. }
  25423. }
  25424. },
  25425. initResizeEvent: function () {
  25426. var me = this;
  25427. domUtils.on(window, "resize", function () {
  25428. if (me._hidden || me._hidden === undefined) {
  25429. return;
  25430. }
  25431. if (me.__resizeTimer) {
  25432. window.clearTimeout(me.__resizeTimer);
  25433. }
  25434. me.__resizeTimer = window.setTimeout(function () {
  25435. me.__resizeTimer = null;
  25436. var dialogWrapNode = me.getDom(),
  25437. contentNode = me.getDom('content'),
  25438. wrapRect = UE.ui.uiUtils.getClientRect(dialogWrapNode),
  25439. contentRect = UE.ui.uiUtils.getClientRect(contentNode),
  25440. vpRect = uiUtils.getViewportRect();
  25441. contentNode.style.width = (vpRect.width - wrapRect.width + contentRect.width) + "px";
  25442. contentNode.style.height = (vpRect.height - wrapRect.height + contentRect.height) + "px";
  25443. dialogWrapNode.style.width = vpRect.width + "px";
  25444. dialogWrapNode.style.height = vpRect.height + "px";
  25445. me.fireEvent("resize");
  25446. }, 100);
  25447. });
  25448. },
  25449. fitSize: function () {
  25450. var popBodyEl = this.getDom('body');
  25451. // if (!(baidu.editor.browser.ie && baidu.editor.browser.version == 7)) {
  25452. // uiUtils.removeStyle(popBodyEl, 'width');
  25453. // uiUtils.removeStyle(popBodyEl, 'height');
  25454. // }
  25455. var size = this.mesureSize();
  25456. popBodyEl.style.width = size.width + 'px';
  25457. popBodyEl.style.height = size.height + 'px';
  25458. return size;
  25459. },
  25460. safeSetOffset: function (offset) {
  25461. var me = this;
  25462. var el = me.getDom();
  25463. var vpRect = uiUtils.getViewportRect();
  25464. var rect = uiUtils.getClientRect(el);
  25465. var left = offset.left;
  25466. if (left + rect.width > vpRect.right) {
  25467. left = vpRect.right - rect.width;
  25468. }
  25469. var top = offset.top;
  25470. if (top + rect.height > vpRect.bottom) {
  25471. top = vpRect.bottom - rect.height;
  25472. }
  25473. el.style.left = Math.max(left, 0) + 'px';
  25474. el.style.top = Math.max(top, 0) + 'px';
  25475. },
  25476. showAtCenter: function () {
  25477. var vpRect = uiUtils.getViewportRect();
  25478. if (!this.fullscreen) {
  25479. this.getDom().style.display = '';
  25480. var popSize = this.fitSize();
  25481. var titleHeight = this.getDom('titlebar').offsetHeight | 0;
  25482. var left = vpRect.width / 2 - popSize.width / 2;
  25483. var top = vpRect.height / 2 - (popSize.height - titleHeight) / 2 - titleHeight;
  25484. var popEl = this.getDom();
  25485. this.safeSetOffset({
  25486. left: Math.max(left | 0, 0),
  25487. top: Math.max(top | 0, 0)
  25488. });
  25489. if (!domUtils.hasClass(popEl, 'edui-state-centered')) {
  25490. popEl.className += ' edui-state-centered';
  25491. }
  25492. } else {
  25493. var dialogWrapNode = this.getDom(),
  25494. contentNode = this.getDom('content');
  25495. dialogWrapNode.style.display = "block";
  25496. var wrapRect = UE.ui.uiUtils.getClientRect(dialogWrapNode),
  25497. contentRect = UE.ui.uiUtils.getClientRect(contentNode);
  25498. dialogWrapNode.style.left = "-100000px";
  25499. contentNode.style.width = (vpRect.width - wrapRect.width + contentRect.width) + "px";
  25500. contentNode.style.height = (vpRect.height - wrapRect.height + contentRect.height) + "px";
  25501. dialogWrapNode.style.width = vpRect.width + "px";
  25502. dialogWrapNode.style.height = vpRect.height + "px";
  25503. dialogWrapNode.style.left = 0;
  25504. //保存环境的overflow值
  25505. this._originalContext = {
  25506. html: {
  25507. overflowX: document.documentElement.style.overflowX,
  25508. overflowY: document.documentElement.style.overflowY
  25509. },
  25510. body: {
  25511. overflowX: document.body.style.overflowX,
  25512. overflowY: document.body.style.overflowY
  25513. }
  25514. };
  25515. document.documentElement.style.overflowX = 'hidden';
  25516. document.documentElement.style.overflowY = 'hidden';
  25517. document.body.style.overflowX = 'hidden';
  25518. document.body.style.overflowY = 'hidden';
  25519. }
  25520. this._show();
  25521. },
  25522. getContentHtml: function () {
  25523. var contentHtml = '';
  25524. if (typeof this.content == 'string') {
  25525. contentHtml = this.content;
  25526. } else if (this.iframeUrl) {
  25527. contentHtml = '<span id="' + this.id + '_contmask" class="dialogcontmask"></span><iframe id="' + this.id +
  25528. '_iframe" class="%%-iframe" height="100%" width="100%" frameborder="0" src="' + this.iframeUrl + '"></iframe>';
  25529. }
  25530. return contentHtml;
  25531. },
  25532. getHtmlTpl: function () {
  25533. var footHtml = '';
  25534. if (this.buttons) {
  25535. var buff = [];
  25536. for (var i = 0; i < this.buttons.length; i++) {
  25537. buff[i] = this.buttons[i].renderHtml();
  25538. }
  25539. footHtml = '<div class="%%-foot">' +
  25540. '<div id="##_buttons" class="%%-buttons">' + buff.join('') + '</div>' +
  25541. '</div>';
  25542. }
  25543. return '<div id="##" class="%%"><div ' + (!this.fullscreen ? 'class="%%"' : 'class="%%-wrap edui-dialog-fullscreen-flag"') + '><div id="##_body" class="%%-body">' +
  25544. '<div class="%%-shadow"></div>' +
  25545. '<div id="##_titlebar" class="%%-titlebar">' +
  25546. '<div class="%%-draghandle" onmousedown="$$._onTitlebarMouseDown(event, this);">' +
  25547. '<span class="%%-caption">' + (this.title || '') + '</span>' +
  25548. '</div>' +
  25549. this.closeButton.renderHtml() +
  25550. '</div>' +
  25551. '<div id="##_content" class="%%-content">' + (this.autoReset ? '' : this.getContentHtml()) + '</div>' +
  25552. footHtml +
  25553. '</div></div></div>';
  25554. },
  25555. postRender: function () {
  25556. // todo: 保持居中/记住上次关闭位置选项
  25557. if (!this.modalMask.getDom()) {
  25558. this.modalMask.render();
  25559. this.modalMask.hide();
  25560. }
  25561. if (!this.dragMask.getDom()) {
  25562. this.dragMask.render();
  25563. this.dragMask.hide();
  25564. }
  25565. var me = this;
  25566. this.addListener('show', function () {
  25567. me.modalMask.show(this.getDom().style.zIndex - 2);
  25568. });
  25569. this.addListener('hide', function () {
  25570. me.modalMask.hide();
  25571. });
  25572. if (this.buttons) {
  25573. for (var i = 0; i < this.buttons.length; i++) {
  25574. this.buttons[i].postRender();
  25575. }
  25576. }
  25577. domUtils.on(window, 'resize', function () {
  25578. setTimeout(function () {
  25579. if (!me.isHidden()) {
  25580. me.safeSetOffset(uiUtils.getClientRect(me.getDom()));
  25581. }
  25582. });
  25583. });
  25584. //hold住scroll事件,防止dialog的滚动影响页面
  25585. // if( this.holdScroll ) {
  25586. //
  25587. // if( !me.iframeUrl ) {
  25588. // domUtils.on( document.getElementById( me.id + "_iframe"), !browser.gecko ? "mousewheel" : "DOMMouseScroll", function(e){
  25589. // domUtils.preventDefault(e);
  25590. // } );
  25591. // } else {
  25592. // me.addListener('dialogafterreset', function(){
  25593. // window.setTimeout(function(){
  25594. // var iframeWindow = document.getElementById( me.id + "_iframe").contentWindow;
  25595. //
  25596. // if( browser.ie ) {
  25597. //
  25598. // var timer = window.setInterval(function(){
  25599. //
  25600. // if( iframeWindow.document && iframeWindow.document.body ) {
  25601. // window.clearInterval( timer );
  25602. // timer = null;
  25603. // domUtils.on( iframeWindow.document.body, !browser.gecko ? "mousewheel" : "DOMMouseScroll", function(e){
  25604. // domUtils.preventDefault(e);
  25605. // } );
  25606. // }
  25607. //
  25608. // }, 100);
  25609. //
  25610. // } else {
  25611. // domUtils.on( iframeWindow, !browser.gecko ? "mousewheel" : "DOMMouseScroll", function(e){
  25612. // domUtils.preventDefault(e);
  25613. // } );
  25614. // }
  25615. //
  25616. // }, 1);
  25617. // });
  25618. // }
  25619. //
  25620. // }
  25621. this._hide();
  25622. },
  25623. mesureSize: function () {
  25624. var body = this.getDom('body');
  25625. var width = uiUtils.getClientRect(this.getDom('content')).width;
  25626. var dialogBodyStyle = body.style;
  25627. dialogBodyStyle.width = width;
  25628. return uiUtils.getClientRect(body);
  25629. },
  25630. _onTitlebarMouseDown: function (evt, el) {
  25631. if (this.draggable) {
  25632. var rect;
  25633. var vpRect = uiUtils.getViewportRect();
  25634. var me = this;
  25635. uiUtils.startDrag(evt, {
  25636. ondragstart: function () {
  25637. rect = uiUtils.getClientRect(me.getDom());
  25638. me.getDom('contmask').style.visibility = 'visible';
  25639. me.dragMask.show(me.getDom().style.zIndex - 1);
  25640. },
  25641. ondragmove: function (x, y) {
  25642. var left = rect.left + x;
  25643. var top = rect.top + y;
  25644. me.safeSetOffset({
  25645. left: left,
  25646. top: top
  25647. });
  25648. },
  25649. ondragstop: function () {
  25650. me.getDom('contmask').style.visibility = 'hidden';
  25651. domUtils.removeClasses(me.getDom(), ['edui-state-centered']);
  25652. me.dragMask.hide();
  25653. }
  25654. });
  25655. }
  25656. },
  25657. reset: function () {
  25658. this.getDom('content').innerHTML = this.getContentHtml();
  25659. this.fireEvent('dialogafterreset');
  25660. },
  25661. _show: function () {
  25662. if (this._hidden) {
  25663. this.getDom().style.display = '';
  25664. //要高过编辑器的zindxe
  25665. this.editor.container.style.zIndex && (this.getDom().style.zIndex = this.editor.container.style.zIndex * 1 + 10);
  25666. this._hidden = false;
  25667. this.fireEvent('show');
  25668. baidu.editor.ui.uiUtils.getFixedLayer().style.zIndex = this.getDom().style.zIndex - 4;
  25669. }
  25670. },
  25671. isHidden: function () {
  25672. return this._hidden;
  25673. },
  25674. _hide: function () {
  25675. if (!this._hidden) {
  25676. var wrapNode = this.getDom();
  25677. wrapNode.style.display = 'none';
  25678. wrapNode.style.zIndex = '';
  25679. wrapNode.style.width = '';
  25680. wrapNode.style.height = '';
  25681. this._hidden = true;
  25682. this.fireEvent('hide');
  25683. }
  25684. },
  25685. open: function () {
  25686. if (this.autoReset) {
  25687. //有可能还没有渲染
  25688. try {
  25689. this.reset();
  25690. } catch (e) {
  25691. this.render();
  25692. this.open()
  25693. }
  25694. }
  25695. this.showAtCenter();
  25696. if (this.iframeUrl) {
  25697. try {
  25698. this.getDom('iframe').focus();
  25699. } catch (ex) {}
  25700. }
  25701. activeDialog = this;
  25702. },
  25703. _onCloseButtonClick: function (evt, el) {
  25704. this.close(false);
  25705. },
  25706. close: function (ok) {
  25707. if (this.fireEvent('close', ok) !== false) {
  25708. //还原环境
  25709. if (this.fullscreen) {
  25710. document.documentElement.style.overflowX = this._originalContext.html.overflowX;
  25711. document.documentElement.style.overflowY = this._originalContext.html.overflowY;
  25712. document.body.style.overflowX = this._originalContext.body.overflowX;
  25713. document.body.style.overflowY = this._originalContext.body.overflowY;
  25714. delete this._originalContext;
  25715. }
  25716. this._hide();
  25717. //销毁content
  25718. var content = this.getDom('content');
  25719. var iframe = this.getDom('iframe');
  25720. if (content && iframe) {
  25721. var doc = iframe.contentDocument || iframe.contentWindow.document;
  25722. doc && (doc.body.innerHTML = '');
  25723. domUtils.remove(content);
  25724. }
  25725. }
  25726. }
  25727. };
  25728. utils.inherits(Dialog, UIBase);
  25729. })();
  25730. // ui/menubutton.js
  25731. ///import core
  25732. ///import uicore
  25733. ///import ui/menu.js
  25734. ///import ui/splitbutton.js
  25735. (function () {
  25736. var utils = baidu.editor.utils,
  25737. Menu = baidu.editor.ui.Menu,
  25738. SplitButton = baidu.editor.ui.SplitButton,
  25739. MenuButton = baidu.editor.ui.MenuButton = function (options) {
  25740. this.initOptions(options);
  25741. this.initMenuButton();
  25742. };
  25743. MenuButton.prototype = {
  25744. initMenuButton: function () {
  25745. var me = this;
  25746. this.uiName = "menubutton";
  25747. this.popup = new Menu({
  25748. items: me.items,
  25749. className: me.className,
  25750. editor: me.editor
  25751. });
  25752. this.popup.addListener('show', function () {
  25753. var list = this;
  25754. for (var i = 0; i < list.items.length; i++) {
  25755. list.items[i].removeState('checked');
  25756. if (list.items[i].value == me._value) {
  25757. list.items[i].addState('checked');
  25758. this.value = me._value;
  25759. }
  25760. }
  25761. });
  25762. this.initSplitButton();
  25763. },
  25764. setValue: function (value) {
  25765. this._value = value;
  25766. }
  25767. };
  25768. utils.inherits(MenuButton, SplitButton);
  25769. })();
  25770. // ui/multiMenu.js
  25771. ///import core
  25772. ///import uicore
  25773. ///commands 表情
  25774. (function () {
  25775. var utils = baidu.editor.utils,
  25776. Popup = baidu.editor.ui.Popup,
  25777. SplitButton = baidu.editor.ui.SplitButton,
  25778. MultiMenuPop = baidu.editor.ui.MultiMenuPop = function (options) {
  25779. this.initOptions(options);
  25780. this.initMultiMenu();
  25781. };
  25782. MultiMenuPop.prototype = {
  25783. initMultiMenu: function () {
  25784. var me = this;
  25785. this.popup = new Popup({
  25786. content: '',
  25787. editor: me.editor,
  25788. iframe_rendered: false,
  25789. onshow: function () {
  25790. if (!this.iframe_rendered) {
  25791. this.iframe_rendered = true;
  25792. this.getDom('content').innerHTML = '<iframe id="' + me.id + '_iframe" src="' + me.iframeUrl + '" frameborder="0"></iframe>';
  25793. me.editor.container.style.zIndex && (this.getDom().style.zIndex = me.editor.container.style.zIndex * 1 + 1);
  25794. }
  25795. }
  25796. // canSideUp:false,
  25797. // canSideLeft:false
  25798. });
  25799. this.onbuttonclick = function () {
  25800. this.showPopup();
  25801. };
  25802. this.initSplitButton();
  25803. }
  25804. };
  25805. utils.inherits(MultiMenuPop, SplitButton);
  25806. })();
  25807. // ui/shortcutmenu.js
  25808. (function () {
  25809. var UI = baidu.editor.ui,
  25810. UIBase = UI.UIBase,
  25811. uiUtils = UI.uiUtils,
  25812. utils = baidu.editor.utils,
  25813. domUtils = baidu.editor.dom.domUtils;
  25814. var allMenus = [], //存储所有快捷菜单
  25815. timeID,
  25816. isSubMenuShow = false; //是否有子pop显示
  25817. var ShortCutMenu = UI.ShortCutMenu = function (options) {
  25818. this.initOptions(options);
  25819. this.initShortCutMenu();
  25820. };
  25821. ShortCutMenu.postHide = hideAllMenu;
  25822. ShortCutMenu.prototype = {
  25823. isHidden: true,
  25824. SPACE: 5,
  25825. initShortCutMenu: function () {
  25826. this.items = this.items || [];
  25827. this.initUIBase();
  25828. this.initItems();
  25829. this.initEvent();
  25830. allMenus.push(this);
  25831. },
  25832. initEvent: function () {
  25833. var me = this,
  25834. doc = me.editor.document;
  25835. domUtils.on(doc, "mousemove", function (e) {
  25836. if (me.isHidden === false) {
  25837. //有pop显示就不隐藏快捷菜单
  25838. if (me.getSubMenuMark() || me.eventType == "contextmenu") return;
  25839. var flag = true,
  25840. el = me.getDom(),
  25841. wt = el.offsetWidth,
  25842. ht = el.offsetHeight,
  25843. distanceX = wt / 2 + me.SPACE, //距离中心X标准
  25844. distanceY = ht / 2, //距离中心Y标准
  25845. x = Math.abs(e.screenX - me.left), //离中心距离横坐标
  25846. y = Math.abs(e.screenY - me.top); //离中心距离纵坐标
  25847. clearTimeout(timeID);
  25848. timeID = setTimeout(function () {
  25849. if (y > 0 && y < distanceY) {
  25850. me.setOpacity(el, "1");
  25851. } else if (y > distanceY && y < distanceY + 70) {
  25852. me.setOpacity(el, "0.5");
  25853. flag = false;
  25854. } else if (y > distanceY + 70 && y < distanceY + 140) {
  25855. me.hide();
  25856. }
  25857. if (flag && x > 0 && x < distanceX) {
  25858. me.setOpacity(el, "1")
  25859. } else if (x > distanceX && x < distanceX + 70) {
  25860. me.setOpacity(el, "0.5")
  25861. } else if (x > distanceX + 70 && x < distanceX + 140) {
  25862. me.hide();
  25863. }
  25864. });
  25865. }
  25866. });
  25867. //ie\ff下 mouseout不准
  25868. if (browser.chrome) {
  25869. domUtils.on(doc, "mouseout", function (e) {
  25870. var relatedTgt = e.relatedTarget || e.toElement;
  25871. if (relatedTgt == null || relatedTgt.tagName == "HTML") {
  25872. me.hide();
  25873. }
  25874. });
  25875. }
  25876. me.editor.addListener("afterhidepop", function () {
  25877. if (!me.isHidden) {
  25878. isSubMenuShow = true;
  25879. }
  25880. });
  25881. },
  25882. initItems: function () {
  25883. if (utils.isArray(this.items)) {
  25884. for (var i = 0, len = this.items.length; i < len; i++) {
  25885. var item = this.items[i].toLowerCase();
  25886. if (UI[item]) {
  25887. this.items[i] = new UI[item](this.editor);
  25888. this.items[i].className += " edui-shortcutsubmenu ";
  25889. }
  25890. }
  25891. }
  25892. },
  25893. setOpacity: function (el, value) {
  25894. if (browser.ie && browser.version < 9) {
  25895. el.style.filter = "alpha(opacity = " + parseFloat(value) * 100 + ");"
  25896. } else {
  25897. el.style.opacity = value;
  25898. }
  25899. },
  25900. getSubMenuMark: function () {
  25901. isSubMenuShow = false;
  25902. var layerEle = uiUtils.getFixedLayer();
  25903. var list = domUtils.getElementsByTagName(layerEle, "div", function (node) {
  25904. return domUtils.hasClass(node, "edui-shortcutsubmenu edui-popup")
  25905. });
  25906. for (var i = 0, node; node = list[i++];) {
  25907. if (node.style.display != "none") {
  25908. isSubMenuShow = true;
  25909. }
  25910. }
  25911. return isSubMenuShow;
  25912. },
  25913. show: function (e, hasContextmenu) {
  25914. var me = this,
  25915. offset = {},
  25916. el = this.getDom(),
  25917. fixedlayer = uiUtils.getFixedLayer();
  25918. function setPos(offset) {
  25919. if (offset.left < 0) {
  25920. offset.left = 0;
  25921. }
  25922. if (offset.top < 0) {
  25923. offset.top = 0;
  25924. }
  25925. el.style.cssText = "position:absolute;left:" + offset.left + "px;top:" + offset.top + "px;";
  25926. }
  25927. function setPosByCxtMenu(menu) {
  25928. if (!menu.tagName) {
  25929. menu = menu.getDom();
  25930. }
  25931. offset.left = parseInt(menu.style.left);
  25932. offset.top = parseInt(menu.style.top);
  25933. offset.top -= el.offsetHeight + 15;
  25934. setPos(offset);
  25935. }
  25936. me.eventType = e.type;
  25937. el.style.cssText = "display:block;left:-9999px";
  25938. if (e.type == "contextmenu" && hasContextmenu) {
  25939. var menu = domUtils.getElementsByTagName(fixedlayer, "div", "edui-contextmenu")[0];
  25940. if (menu) {
  25941. setPosByCxtMenu(menu)
  25942. } else {
  25943. me.editor.addListener("aftershowcontextmenu", function (type, menu) {
  25944. setPosByCxtMenu(menu);
  25945. });
  25946. }
  25947. } else {
  25948. offset = uiUtils.getViewportOffsetByEvent(e);
  25949. offset.top -= el.offsetHeight + me.SPACE;
  25950. offset.left += me.SPACE + 20;
  25951. setPos(offset);
  25952. me.setOpacity(el, 0.2);
  25953. }
  25954. me.isHidden = false;
  25955. me.left = e.screenX + el.offsetWidth / 2 - me.SPACE;
  25956. me.top = e.screenY - (el.offsetHeight / 2) - me.SPACE;
  25957. if (me.editor) {
  25958. el.style.zIndex = me.editor.container.style.zIndex * 1 + 10;
  25959. fixedlayer.style.zIndex = el.style.zIndex - 1;
  25960. }
  25961. },
  25962. hide: function () {
  25963. if (this.getDom()) {
  25964. this.getDom().style.display = "none";
  25965. }
  25966. this.isHidden = true;
  25967. },
  25968. postRender: function () {
  25969. if (utils.isArray(this.items)) {
  25970. for (var i = 0, item; item = this.items[i++];) {
  25971. item.postRender();
  25972. }
  25973. }
  25974. },
  25975. getHtmlTpl: function () {
  25976. var buff;
  25977. if (utils.isArray(this.items)) {
  25978. buff = [];
  25979. for (var i = 0; i < this.items.length; i++) {
  25980. buff[i] = this.items[i].renderHtml();
  25981. }
  25982. buff = buff.join("");
  25983. } else {
  25984. buff = this.items;
  25985. }
  25986. return '<div id="##" class="%% edui-toolbar" data-src="shortcutmenu" onmousedown="return false;" onselectstart="return false;" >' +
  25987. buff +
  25988. '</div>';
  25989. }
  25990. };
  25991. utils.inherits(ShortCutMenu, UIBase);
  25992. function hideAllMenu(e) {
  25993. var tgt = e.target || e.srcElement,
  25994. cur = domUtils.findParent(tgt, function (node) {
  25995. return domUtils.hasClass(node, "edui-shortcutmenu") || domUtils.hasClass(node, "edui-popup");
  25996. }, true);
  25997. if (!cur) {
  25998. for (var i = 0, menu; menu = allMenus[i++];) {
  25999. menu.hide()
  26000. }
  26001. }
  26002. }
  26003. domUtils.on(document, 'mousedown', function (e) {
  26004. hideAllMenu(e);
  26005. });
  26006. domUtils.on(window, 'scroll', function (e) {
  26007. hideAllMenu(e);
  26008. });
  26009. })();
  26010. // ui/breakline.js
  26011. (function () {
  26012. var utils = baidu.editor.utils,
  26013. UIBase = baidu.editor.ui.UIBase,
  26014. Breakline = baidu.editor.ui.Breakline = function (options) {
  26015. this.initOptions(options);
  26016. this.initSeparator();
  26017. };
  26018. Breakline.prototype = {
  26019. uiName: 'Breakline',
  26020. initSeparator: function () {
  26021. this.initUIBase();
  26022. },
  26023. getHtmlTpl: function () {
  26024. return '<br/>';
  26025. }
  26026. };
  26027. utils.inherits(Breakline, UIBase);
  26028. })();
  26029. // ui/message.js
  26030. ///import core
  26031. ///import uicore
  26032. (function () {
  26033. var utils = baidu.editor.utils,
  26034. domUtils = baidu.editor.dom.domUtils,
  26035. UIBase = baidu.editor.ui.UIBase,
  26036. Message = baidu.editor.ui.Message = function (options) {
  26037. this.initOptions(options);
  26038. this.initMessage();
  26039. };
  26040. Message.prototype = {
  26041. initMessage: function () {
  26042. this.initUIBase();
  26043. },
  26044. getHtmlTpl: function () {
  26045. return '<div id="##" class="edui-message %%">' +
  26046. ' <div id="##_closer" class="edui-message-closer">×</div>' +
  26047. ' <div id="##_body" class="edui-message-body edui-message-type-info">' +
  26048. ' <iframe style="position:absolute;z-index:-1;left:0;top:0;background-color: transparent;" frameborder="0" width="100%" height="100%" src="about:blank"></iframe>' +
  26049. ' <div class="edui-shadow"></div>' +
  26050. ' <div id="##_content" class="edui-message-content">' +
  26051. ' </div>' +
  26052. ' </div>' +
  26053. '</div>';
  26054. },
  26055. reset: function (opt) {
  26056. var me = this;
  26057. if (!opt.keepshow) {
  26058. clearTimeout(this.timer);
  26059. me.timer = setTimeout(function () {
  26060. me.hide();
  26061. }, opt.timeout || 4000);
  26062. }
  26063. opt.content !== undefined && me.setContent(opt.content);
  26064. opt.type !== undefined && me.setType(opt.type);
  26065. me.show();
  26066. },
  26067. postRender: function () {
  26068. var me = this,
  26069. closer = this.getDom('closer');
  26070. closer && domUtils.on(closer, 'click', function () {
  26071. me.hide();
  26072. });
  26073. },
  26074. setContent: function (content) {
  26075. this.getDom('content').innerHTML = content;
  26076. },
  26077. setType: function (type) {
  26078. type = type || 'info';
  26079. var body = this.getDom('body');
  26080. body.className = body.className.replace(/edui-message-type-[\w-]+/, 'edui-message-type-' + type);
  26081. },
  26082. getContent: function () {
  26083. return this.getDom('content').innerHTML;
  26084. },
  26085. getType: function () {
  26086. var arr = this.getDom('body').match(/edui-message-type-([\w-]+)/);
  26087. return arr ? arr[1] : '';
  26088. },
  26089. show: function () {
  26090. this.getDom().style.display = 'block';
  26091. },
  26092. hide: function () {
  26093. var dom = this.getDom();
  26094. if (dom) {
  26095. dom.style.display = 'none';
  26096. dom.parentNode && dom.parentNode.removeChild(dom);
  26097. }
  26098. }
  26099. };
  26100. utils.inherits(Message, UIBase);
  26101. })();
  26102. // adapter/editorui.js
  26103. //ui跟编辑器的适配層
  26104. //那个按钮弹出是dialog,是下拉筐等都是在这个js中配置
  26105. //自己写的ui也要在这里配置,放到baidu.editor.ui下边,当编辑器实例化的时候会根据ueditor.config中的toolbars找到相应的进行实例化
  26106. (function () {
  26107. var utils = baidu.editor.utils;
  26108. var editorui = baidu.editor.ui;
  26109. var _Dialog = editorui.Dialog;
  26110. editorui.buttons = {};
  26111. editorui.Dialog = function (options) {
  26112. var dialog = new _Dialog(options);
  26113. dialog.addListener('hide', function () {
  26114. if (dialog.editor) {
  26115. var editor = dialog.editor;
  26116. try {
  26117. if (browser.gecko) {
  26118. var y = editor.window.scrollY,
  26119. x = editor.window.scrollX;
  26120. editor.body.focus();
  26121. editor.window.scrollTo(x, y);
  26122. } else {
  26123. editor.focus();
  26124. }
  26125. } catch (ex) {}
  26126. }
  26127. });
  26128. return dialog;
  26129. };
  26130. var iframeUrlMap = {
  26131. 'anchor': '~/dialogs/anchor/anchor.html',
  26132. 'insertimage': '~/dialogs/image/image.html',
  26133. 'link': '~/dialogs/link/link.html',
  26134. 'spechars': '~/dialogs/spechars/spechars.html',
  26135. 'searchreplace': '~/dialogs/searchreplace/searchreplace.html',
  26136. 'map': '~/dialogs/map/map.html',
  26137. 'gmap': '~/dialogs/gmap/gmap.html',
  26138. 'insertvideo': '~/dialogs/video/video.html',
  26139. 'help': '~/dialogs/help/help.html',
  26140. 'preview': '~/dialogs/preview/preview.html',
  26141. 'emotion': '~/dialogs/emotion/emotion.html',
  26142. 'wordimage': '~/dialogs/wordimage/wordimage.html',
  26143. 'attachment': '~/dialogs/attachment/attachment.html',
  26144. 'insertframe': '~/dialogs/insertframe/insertframe.html',
  26145. 'edittip': '~/dialogs/table/edittip.html',
  26146. 'edittable': '~/dialogs/table/edittable.html',
  26147. 'edittd': '~/dialogs/table/edittd.html',
  26148. 'webapp': '~/dialogs/webapp/webapp.html',
  26149. 'snapscreen': '~/dialogs/snapscreen/snapscreen.html',
  26150. 'scrawl': '~/dialogs/scrawl/scrawl.html',
  26151. 'music': '~/dialogs/music/music.html',
  26152. 'template': '~/dialogs/template/template.html',
  26153. 'background': '~/dialogs/background/background.html',
  26154. 'charts': '~/dialogs/charts/charts.html'
  26155. };
  26156. //为工具栏添加按钮,以下都是统一的按钮触发命令,所以写在一起
  26157. var btnCmds = ['undo', 'redo', 'formatmatch',
  26158. 'bold', 'italic', 'underline', 'fontborder', 'touppercase', 'tolowercase',
  26159. 'strikethrough', 'subscript', 'superscript', 'source', 'indent', 'outdent',
  26160. 'blockquote', 'pasteplain', 'pagebreak',
  26161. 'selectall', 'print', 'horizontal', 'removeformat', 'time', 'date', 'unlink',
  26162. 'insertparagraphbeforetable', 'insertrow', 'insertcol', 'mergeright', 'mergedown', 'deleterow',
  26163. 'deletecol', 'splittorows', 'splittocols', 'splittocells', 'mergecells', 'deletetable', 'drafts'
  26164. ];
  26165. for (var i = 0, ci; ci = btnCmds[i++];) {
  26166. ci = ci.toLowerCase();
  26167. editorui[ci] = function (cmd) {
  26168. return function (editor) {
  26169. var ui = new editorui.Button({
  26170. className: 'edui-for-' + cmd,
  26171. title: editor.options.labelMap[cmd] || editor.getLang("labelMap." + cmd) || '',
  26172. onclick: function () {
  26173. editor.execCommand(cmd);
  26174. },
  26175. theme: editor.options.theme,
  26176. showText: false
  26177. });
  26178. editorui.buttons[cmd] = ui;
  26179. editor.addListener('selectionchange', function (type, causeByUi, uiReady) {
  26180. var state = editor.queryCommandState(cmd);
  26181. if (state == -1) {
  26182. ui.setDisabled(true);
  26183. ui.setChecked(false);
  26184. } else {
  26185. if (!uiReady) {
  26186. ui.setDisabled(false);
  26187. ui.setChecked(state);
  26188. }
  26189. }
  26190. });
  26191. return ui;
  26192. };
  26193. }(ci);
  26194. }
  26195. //清除文档
  26196. editorui.cleardoc = function (editor) {
  26197. var ui = new editorui.Button({
  26198. className: 'edui-for-cleardoc',
  26199. title: editor.options.labelMap.cleardoc || editor.getLang("labelMap.cleardoc") || '',
  26200. theme: editor.options.theme,
  26201. onclick: function () {
  26202. if (confirm(editor.getLang("confirmClear"))) {
  26203. editor.execCommand('cleardoc');
  26204. }
  26205. }
  26206. });
  26207. editorui.buttons["cleardoc"] = ui;
  26208. editor.addListener('selectionchange', function () {
  26209. ui.setDisabled(editor.queryCommandState('cleardoc') == -1);
  26210. });
  26211. return ui;
  26212. };
  26213. //排版,图片排版,文字方向
  26214. var typeset = {
  26215. 'justify': ['left', 'right', 'center', 'justify'],
  26216. 'imagefloat': ['none', 'left', 'center', 'right'],
  26217. 'directionality': ['ltr', 'rtl']
  26218. };
  26219. for (var p in typeset) {
  26220. (function (cmd, val) {
  26221. for (var i = 0, ci; ci = val[i++];) {
  26222. (function (cmd2) {
  26223. editorui[cmd.replace('float', '') + cmd2] = function (editor) {
  26224. var ui = new editorui.Button({
  26225. className: 'edui-for-' + cmd.replace('float', '') + cmd2,
  26226. title: editor.options.labelMap[cmd.replace('float', '') + cmd2] || editor.getLang("labelMap." + cmd.replace('float', '') + cmd2) || '',
  26227. theme: editor.options.theme,
  26228. onclick: function () {
  26229. editor.execCommand(cmd, cmd2);
  26230. }
  26231. });
  26232. editorui.buttons[cmd] = ui;
  26233. editor.addListener('selectionchange', function (type, causeByUi, uiReady) {
  26234. ui.setDisabled(editor.queryCommandState(cmd) == -1);
  26235. ui.setChecked(editor.queryCommandValue(cmd) == cmd2 && !uiReady);
  26236. });
  26237. return ui;
  26238. };
  26239. })(ci)
  26240. }
  26241. })(p, typeset[p])
  26242. }
  26243. //字体颜色和背景颜色
  26244. for (var i = 0, ci; ci = ['backcolor', 'forecolor'][i++];) {
  26245. editorui[ci] = function (cmd) {
  26246. return function (editor) {
  26247. var ui = new editorui.ColorButton({
  26248. className: 'edui-for-' + cmd,
  26249. color: 'default',
  26250. title: editor.options.labelMap[cmd] || editor.getLang("labelMap." + cmd) || '',
  26251. editor: editor,
  26252. onpickcolor: function (t, color) {
  26253. editor.execCommand(cmd, color);
  26254. },
  26255. onpicknocolor: function () {
  26256. editor.execCommand(cmd, 'default');
  26257. this.setColor('transparent');
  26258. this.color = 'default';
  26259. },
  26260. onbuttonclick: function () {
  26261. editor.execCommand(cmd, this.color);
  26262. }
  26263. });
  26264. editorui.buttons[cmd] = ui;
  26265. editor.addListener('selectionchange', function () {
  26266. ui.setDisabled(editor.queryCommandState(cmd) == -1);
  26267. });
  26268. return ui;
  26269. };
  26270. }(ci);
  26271. }
  26272. var dialogBtns = {
  26273. noOk: ['searchreplace', 'help', 'spechars', 'webapp', 'preview'],
  26274. ok: ['attachment', 'anchor', 'link', 'insertimage', 'map', 'gmap', 'insertframe', 'wordimage',
  26275. 'insertvideo', 'insertframe', 'edittip', 'edittable', 'edittd', 'scrawl', 'template', 'music', 'background', 'charts'
  26276. ]
  26277. };
  26278. for (var p in dialogBtns) {
  26279. (function (type, vals) {
  26280. for (var i = 0, ci; ci = vals[i++];) {
  26281. //todo opera下存在问题
  26282. if (browser.opera && ci === "searchreplace") {
  26283. continue;
  26284. }
  26285. (function (cmd) {
  26286. editorui[cmd] = function (editor, iframeUrl, title) {
  26287. iframeUrl = iframeUrl || (editor.options.iframeUrlMap || {})[cmd] || iframeUrlMap[cmd];
  26288. title = editor.options.labelMap[cmd] || editor.getLang("labelMap." + cmd) || '';
  26289. var dialog;
  26290. //没有iframeUrl不创建dialog
  26291. if (iframeUrl) {
  26292. dialog = new editorui.Dialog(utils.extend({
  26293. iframeUrl: editor.ui.mapUrl(iframeUrl),
  26294. editor: editor,
  26295. className: 'edui-for-' + cmd,
  26296. title: title,
  26297. holdScroll: cmd === 'insertimage',
  26298. fullscreen: /charts|preview/.test(cmd),
  26299. closeDialog: editor.getLang("closeDialog")
  26300. }, type == 'ok' ? {
  26301. buttons: [{
  26302. className: 'edui-okbutton',
  26303. label: editor.getLang("ok"),
  26304. editor: editor,
  26305. onclick: function () {
  26306. dialog.close(true);
  26307. }
  26308. },
  26309. {
  26310. className: 'edui-cancelbutton',
  26311. label: editor.getLang("cancel"),
  26312. editor: editor,
  26313. onclick: function () {
  26314. dialog.close(false);
  26315. }
  26316. }
  26317. ]
  26318. } : {}));
  26319. editor.ui._dialogs[cmd + "Dialog"] = dialog;
  26320. }
  26321. var ui = new editorui.Button({
  26322. className: 'edui-for-' + cmd,
  26323. title: title,
  26324. onclick: function () {
  26325. if (dialog) {
  26326. switch (cmd) {
  26327. case "wordimage":
  26328. var images = editor.execCommand("wordimage");
  26329. if (images && images.length) {
  26330. dialog.render();
  26331. dialog.open();
  26332. }
  26333. break;
  26334. case "scrawl":
  26335. if (editor.queryCommandState("scrawl") != -1) {
  26336. dialog.render();
  26337. dialog.open();
  26338. }
  26339. break;
  26340. default:
  26341. dialog.render();
  26342. dialog.open();
  26343. }
  26344. }
  26345. },
  26346. theme: editor.options.theme,
  26347. disabled: (cmd == 'scrawl' && editor.queryCommandState("scrawl") == -1) || (cmd == 'charts')
  26348. });
  26349. editorui.buttons[cmd] = ui;
  26350. editor.addListener('selectionchange', function () {
  26351. //只存在于右键菜单而无工具栏按钮的ui不需要检测状态
  26352. var unNeedCheckState = {
  26353. 'edittable': 1
  26354. };
  26355. if (cmd in unNeedCheckState) return;
  26356. var state = editor.queryCommandState(cmd);
  26357. if (ui.getDom()) {
  26358. ui.setDisabled(state == -1);
  26359. ui.setChecked(state);
  26360. }
  26361. });
  26362. return ui;
  26363. };
  26364. })(ci.toLowerCase())
  26365. }
  26366. })(p, dialogBtns[p]);
  26367. }
  26368. editorui.snapscreen = function (editor, iframeUrl, title) {
  26369. title = editor.options.labelMap['snapscreen'] || editor.getLang("labelMap.snapscreen") || '';
  26370. var ui = new editorui.Button({
  26371. className: 'edui-for-snapscreen',
  26372. title: title,
  26373. onclick: function () {
  26374. editor.execCommand("snapscreen");
  26375. },
  26376. theme: editor.options.theme
  26377. });
  26378. editorui.buttons['snapscreen'] = ui;
  26379. iframeUrl = iframeUrl || (editor.options.iframeUrlMap || {})["snapscreen"] || iframeUrlMap["snapscreen"];
  26380. if (iframeUrl) {
  26381. var dialog = new editorui.Dialog({
  26382. iframeUrl: editor.ui.mapUrl(iframeUrl),
  26383. editor: editor,
  26384. className: 'edui-for-snapscreen',
  26385. title: title,
  26386. buttons: [{
  26387. className: 'edui-okbutton',
  26388. label: editor.getLang("ok"),
  26389. editor: editor,
  26390. onclick: function () {
  26391. dialog.close(true);
  26392. }
  26393. },
  26394. {
  26395. className: 'edui-cancelbutton',
  26396. label: editor.getLang("cancel"),
  26397. editor: editor,
  26398. onclick: function () {
  26399. dialog.close(false);
  26400. }
  26401. }
  26402. ]
  26403. });
  26404. dialog.render();
  26405. editor.ui._dialogs["snapscreenDialog"] = dialog;
  26406. }
  26407. editor.addListener('selectionchange', function () {
  26408. ui.setDisabled(editor.queryCommandState('snapscreen') == -1);
  26409. });
  26410. return ui;
  26411. };
  26412. editorui.insertcode = function (editor, list, title) {
  26413. list = editor.options['insertcode'] || [];
  26414. title = editor.options.labelMap['insertcode'] || editor.getLang("labelMap.insertcode") || '';
  26415. // if (!list.length) return;
  26416. var items = [];
  26417. utils.each(list, function (key, val) {
  26418. items.push({
  26419. label: key,
  26420. value: val,
  26421. theme: editor.options.theme,
  26422. renderLabelHtml: function () {
  26423. return '<div class="edui-label %%-label" >' + (this.label || '') + '</div>';
  26424. }
  26425. });
  26426. });
  26427. var ui = new editorui.Combox({
  26428. editor: editor,
  26429. items: items,
  26430. onselect: function (t, index) {
  26431. editor.execCommand('insertcode', this.items[index].value);
  26432. },
  26433. onbuttonclick: function () {
  26434. this.showPopup();
  26435. },
  26436. title: title,
  26437. initValue: title,
  26438. className: 'edui-for-insertcode',
  26439. indexByValue: function (value) {
  26440. if (value) {
  26441. for (var i = 0, ci; ci = this.items[i]; i++) {
  26442. if (ci.value.indexOf(value) != -1)
  26443. return i;
  26444. }
  26445. }
  26446. return -1;
  26447. }
  26448. });
  26449. editorui.buttons['insertcode'] = ui;
  26450. editor.addListener('selectionchange', function (type, causeByUi, uiReady) {
  26451. if (!uiReady) {
  26452. var state = editor.queryCommandState('insertcode');
  26453. if (state == -1) {
  26454. ui.setDisabled(true);
  26455. } else {
  26456. ui.setDisabled(false);
  26457. var value = editor.queryCommandValue('insertcode');
  26458. if (!value) {
  26459. ui.setValue(title);
  26460. return;
  26461. }
  26462. //trace:1871 ie下从源码模式切换回来时,字体会带单引号,而且会有逗号
  26463. value && (value = value.replace(/['"]/g, '').split(',')[0]);
  26464. ui.setValue(value);
  26465. }
  26466. }
  26467. });
  26468. return ui;
  26469. };
  26470. editorui.fontfamily = function (editor, list, title) {
  26471. list = editor.options['fontfamily'] || [];
  26472. title = editor.options.labelMap['fontfamily'] || editor.getLang("labelMap.fontfamily") || '';
  26473. if (!list.length) return;
  26474. for (var i = 0, ci, items = []; ci = list[i]; i++) {
  26475. var langLabel = editor.getLang('fontfamily')[ci.name] || "";
  26476. (function (key, val) {
  26477. items.push({
  26478. label: key,
  26479. value: val,
  26480. theme: editor.options.theme,
  26481. renderLabelHtml: function () {
  26482. return '<div class="edui-label %%-label" style="font-family:' +
  26483. utils.unhtml(this.value) + '">' + (this.label || '') + '</div>';
  26484. }
  26485. });
  26486. })(ci.label || langLabel, ci.val)
  26487. }
  26488. var ui = new editorui.Combox({
  26489. editor: editor,
  26490. items: items,
  26491. onselect: function (t, index) {
  26492. editor.execCommand('FontFamily', this.items[index].value);
  26493. },
  26494. onbuttonclick: function () {
  26495. this.showPopup();
  26496. },
  26497. title: title,
  26498. initValue: title,
  26499. className: 'edui-for-fontfamily',
  26500. indexByValue: function (value) {
  26501. if (value) {
  26502. for (var i = 0, ci; ci = this.items[i]; i++) {
  26503. if (ci.value.indexOf(value) != -1)
  26504. return i;
  26505. }
  26506. }
  26507. return -1;
  26508. }
  26509. });
  26510. editorui.buttons['fontfamily'] = ui;
  26511. editor.addListener('selectionchange', function (type, causeByUi, uiReady) {
  26512. if (!uiReady) {
  26513. var state = editor.queryCommandState('FontFamily');
  26514. if (state == -1) {
  26515. ui.setDisabled(true);
  26516. } else {
  26517. ui.setDisabled(false);
  26518. var value = editor.queryCommandValue('FontFamily');
  26519. //trace:1871 ie下从源码模式切换回来时,字体会带单引号,而且会有逗号
  26520. value && (value = value.replace(/['"]/g, '').split(',')[0]);
  26521. ui.setValue(value);
  26522. }
  26523. }
  26524. });
  26525. return ui;
  26526. };
  26527. editorui.fontsize = function (editor, list, title) {
  26528. title = editor.options.labelMap['fontsize'] || editor.getLang("labelMap.fontsize") || '';
  26529. list = list || editor.options['fontsize'] || [];
  26530. if (!list.length) return;
  26531. var items = [];
  26532. for (var i = 0; i < list.length; i++) {
  26533. var size = list[i] + 'px';
  26534. items.push({
  26535. label: size,
  26536. value: size,
  26537. theme: editor.options.theme,
  26538. renderLabelHtml: function () {
  26539. return '<div class="edui-label %%-label" style="line-height:1;font-size:' +
  26540. this.value + '">' + (this.label || '') + '</div>';
  26541. }
  26542. });
  26543. }
  26544. var ui = new editorui.Combox({
  26545. editor: editor,
  26546. items: items,
  26547. title: title,
  26548. initValue: title,
  26549. onselect: function (t, index) {
  26550. editor.execCommand('FontSize', this.items[index].value);
  26551. },
  26552. onbuttonclick: function () {
  26553. this.showPopup();
  26554. },
  26555. className: 'edui-for-fontsize'
  26556. });
  26557. editorui.buttons['fontsize'] = ui;
  26558. editor.addListener('selectionchange', function (type, causeByUi, uiReady) {
  26559. if (!uiReady) {
  26560. var state = editor.queryCommandState('FontSize');
  26561. if (state == -1) {
  26562. ui.setDisabled(true);
  26563. } else {
  26564. ui.setDisabled(false);
  26565. ui.setValue(editor.queryCommandValue('FontSize'));
  26566. }
  26567. }
  26568. });
  26569. return ui;
  26570. };
  26571. editorui.paragraph = function (editor, list, title) {
  26572. title = editor.options.labelMap['paragraph'] || editor.getLang("labelMap.paragraph") || '';
  26573. list = editor.options['paragraph'] || [];
  26574. if (utils.isEmptyObject(list)) return;
  26575. var items = [];
  26576. for (var i in list) {
  26577. items.push({
  26578. value: i,
  26579. label: list[i] || editor.getLang("paragraph")[i],
  26580. theme: editor.options.theme,
  26581. renderLabelHtml: function () {
  26582. return '<div class="edui-label %%-label"><span class="edui-for-' + this.value + '">' + (this.label || '') + '</span></div>';
  26583. }
  26584. })
  26585. }
  26586. var ui = new editorui.Combox({
  26587. editor: editor,
  26588. items: items,
  26589. title: title,
  26590. initValue: title,
  26591. className: 'edui-for-paragraph',
  26592. onselect: function (t, index) {
  26593. editor.execCommand('Paragraph', this.items[index].value);
  26594. },
  26595. onbuttonclick: function () {
  26596. this.showPopup();
  26597. }
  26598. });
  26599. editorui.buttons['paragraph'] = ui;
  26600. editor.addListener('selectionchange', function (type, causeByUi, uiReady) {
  26601. if (!uiReady) {
  26602. var state = editor.queryCommandState('Paragraph');
  26603. if (state == -1) {
  26604. ui.setDisabled(true);
  26605. } else {
  26606. ui.setDisabled(false);
  26607. var value = editor.queryCommandValue('Paragraph');
  26608. var index = ui.indexByValue(value);
  26609. if (index != -1) {
  26610. ui.setValue(value);
  26611. } else {
  26612. ui.setValue(ui.initValue);
  26613. }
  26614. }
  26615. }
  26616. });
  26617. return ui;
  26618. };
  26619. //自定义标题
  26620. editorui.customstyle = function (editor) {
  26621. var list = editor.options['customstyle'] || [],
  26622. title = editor.options.labelMap['customstyle'] || editor.getLang("labelMap.customstyle") || '';
  26623. if (!list.length) return;
  26624. var langCs = editor.getLang('customstyle');
  26625. for (var i = 0, items = [], t; t = list[i++];) {
  26626. (function (t) {
  26627. var ck = {};
  26628. ck.label = t.label ? t.label : langCs[t.name];
  26629. ck.style = t.style;
  26630. ck.className = t.className;
  26631. ck.tag = t.tag;
  26632. items.push({
  26633. label: ck.label,
  26634. value: ck,
  26635. theme: editor.options.theme,
  26636. renderLabelHtml: function () {
  26637. return '<div class="edui-label %%-label">' + '<' + ck.tag + ' ' + (ck.className ? ' class="' + ck.className + '"' : "") +
  26638. (ck.style ? ' style="' + ck.style + '"' : "") + '>' + ck.label + "<\/" + ck.tag + ">" +
  26639. '</div>';
  26640. }
  26641. });
  26642. })(t);
  26643. }
  26644. var ui = new editorui.Combox({
  26645. editor: editor,
  26646. items: items,
  26647. title: title,
  26648. initValue: title,
  26649. className: 'edui-for-customstyle',
  26650. onselect: function (t, index) {
  26651. editor.execCommand('customstyle', this.items[index].value);
  26652. },
  26653. onbuttonclick: function () {
  26654. this.showPopup();
  26655. },
  26656. indexByValue: function (value) {
  26657. for (var i = 0, ti; ti = this.items[i++];) {
  26658. if (ti.label == value) {
  26659. return i - 1
  26660. }
  26661. }
  26662. return -1;
  26663. }
  26664. });
  26665. editorui.buttons['customstyle'] = ui;
  26666. editor.addListener('selectionchange', function (type, causeByUi, uiReady) {
  26667. if (!uiReady) {
  26668. var state = editor.queryCommandState('customstyle');
  26669. if (state == -1) {
  26670. ui.setDisabled(true);
  26671. } else {
  26672. ui.setDisabled(false);
  26673. var value = editor.queryCommandValue('customstyle');
  26674. var index = ui.indexByValue(value);
  26675. if (index != -1) {
  26676. ui.setValue(value);
  26677. } else {
  26678. ui.setValue(ui.initValue);
  26679. }
  26680. }
  26681. }
  26682. });
  26683. return ui;
  26684. };
  26685. editorui.inserttable = function (editor, iframeUrl, title) {
  26686. title = editor.options.labelMap['inserttable'] || editor.getLang("labelMap.inserttable") || '';
  26687. var ui = new editorui.TableButton({
  26688. editor: editor,
  26689. title: title,
  26690. className: 'edui-for-inserttable',
  26691. onpicktable: function (t, numCols, numRows) {
  26692. editor.execCommand('InsertTable', {
  26693. numRows: numRows,
  26694. numCols: numCols,
  26695. border: 1
  26696. });
  26697. },
  26698. onbuttonclick: function () {
  26699. this.showPopup();
  26700. }
  26701. });
  26702. editorui.buttons['inserttable'] = ui;
  26703. editor.addListener('selectionchange', function () {
  26704. ui.setDisabled(editor.queryCommandState('inserttable') == -1);
  26705. });
  26706. return ui;
  26707. };
  26708. editorui.lineheight = function (editor) {
  26709. var val = editor.options.lineheight || [];
  26710. if (!val.length) return;
  26711. for (var i = 0, ci, items = []; ci = val[i++];) {
  26712. items.push({
  26713. //todo:写死了
  26714. label: ci,
  26715. value: ci,
  26716. theme: editor.options.theme,
  26717. onclick: function () {
  26718. editor.execCommand("lineheight", this.value);
  26719. }
  26720. })
  26721. }
  26722. var ui = new editorui.MenuButton({
  26723. editor: editor,
  26724. className: 'edui-for-lineheight',
  26725. title: editor.options.labelMap['lineheight'] || editor.getLang("labelMap.lineheight") || '',
  26726. items: items,
  26727. onbuttonclick: function () {
  26728. var value = editor.queryCommandValue('LineHeight') || this.value;
  26729. editor.execCommand("LineHeight", value);
  26730. }
  26731. });
  26732. editorui.buttons['lineheight'] = ui;
  26733. editor.addListener('selectionchange', function () {
  26734. var state = editor.queryCommandState('LineHeight');
  26735. if (state == -1) {
  26736. ui.setDisabled(true);
  26737. } else {
  26738. ui.setDisabled(false);
  26739. var value = editor.queryCommandValue('LineHeight');
  26740. value && ui.setValue((value + '').replace(/cm/, ''));
  26741. ui.setChecked(state)
  26742. }
  26743. });
  26744. return ui;
  26745. };
  26746. var rowspacings = ['top', 'bottom'];
  26747. for (var r = 0, ri; ri = rowspacings[r++];) {
  26748. (function (cmd) {
  26749. editorui['rowspacing' + cmd] = function (editor) {
  26750. var val = editor.options['rowspacing' + cmd] || [];
  26751. if (!val.length) return null;
  26752. for (var i = 0, ci, items = []; ci = val[i++];) {
  26753. items.push({
  26754. label: ci,
  26755. value: ci,
  26756. theme: editor.options.theme,
  26757. onclick: function () {
  26758. editor.execCommand("rowspacing", this.value, cmd);
  26759. }
  26760. })
  26761. }
  26762. var ui = new editorui.MenuButton({
  26763. editor: editor,
  26764. className: 'edui-for-rowspacing' + cmd,
  26765. title: editor.options.labelMap['rowspacing' + cmd] || editor.getLang("labelMap.rowspacing" + cmd) || '',
  26766. items: items,
  26767. onbuttonclick: function () {
  26768. var value = editor.queryCommandValue('rowspacing', cmd) || this.value;
  26769. editor.execCommand("rowspacing", value, cmd);
  26770. }
  26771. });
  26772. editorui.buttons[cmd] = ui;
  26773. editor.addListener('selectionchange', function () {
  26774. var state = editor.queryCommandState('rowspacing', cmd);
  26775. if (state == -1) {
  26776. ui.setDisabled(true);
  26777. } else {
  26778. ui.setDisabled(false);
  26779. var value = editor.queryCommandValue('rowspacing', cmd);
  26780. value && ui.setValue((value + '').replace(/%/, ''));
  26781. ui.setChecked(state)
  26782. }
  26783. });
  26784. return ui;
  26785. }
  26786. })(ri)
  26787. }
  26788. //有序,无序列表
  26789. var lists = ['insertorderedlist', 'insertunorderedlist'];
  26790. for (var l = 0, cl; cl = lists[l++];) {
  26791. (function (cmd) {
  26792. editorui[cmd] = function (editor) {
  26793. var vals = editor.options[cmd],
  26794. _onMenuClick = function () {
  26795. editor.execCommand(cmd, this.value);
  26796. },
  26797. items = [];
  26798. for (var i in vals) {
  26799. items.push({
  26800. label: vals[i] || editor.getLang()[cmd][i] || "",
  26801. value: i,
  26802. theme: editor.options.theme,
  26803. onclick: _onMenuClick
  26804. })
  26805. }
  26806. var ui = new editorui.MenuButton({
  26807. editor: editor,
  26808. className: 'edui-for-' + cmd,
  26809. title: editor.getLang("labelMap." + cmd) || '',
  26810. 'items': items,
  26811. onbuttonclick: function () {
  26812. var value = editor.queryCommandValue(cmd) || this.value;
  26813. editor.execCommand(cmd, value);
  26814. }
  26815. });
  26816. editorui.buttons[cmd] = ui;
  26817. editor.addListener('selectionchange', function () {
  26818. var state = editor.queryCommandState(cmd);
  26819. if (state == -1) {
  26820. ui.setDisabled(true);
  26821. } else {
  26822. ui.setDisabled(false);
  26823. var value = editor.queryCommandValue(cmd);
  26824. ui.setValue(value);
  26825. ui.setChecked(state)
  26826. }
  26827. });
  26828. return ui;
  26829. };
  26830. })(cl)
  26831. }
  26832. editorui.fullscreen = function (editor, title) {
  26833. title = editor.options.labelMap['fullscreen'] || editor.getLang("labelMap.fullscreen") || '';
  26834. var ui = new editorui.Button({
  26835. className: 'edui-for-fullscreen',
  26836. title: title,
  26837. theme: editor.options.theme,
  26838. onclick: function () {
  26839. if (editor.ui) {
  26840. editor.ui.setFullScreen(!editor.ui.isFullScreen());
  26841. }
  26842. this.setChecked(editor.ui.isFullScreen());
  26843. }
  26844. });
  26845. editorui.buttons['fullscreen'] = ui;
  26846. editor.addListener('selectionchange', function () {
  26847. var state = editor.queryCommandState('fullscreen');
  26848. ui.setDisabled(state == -1);
  26849. ui.setChecked(editor.ui.isFullScreen());
  26850. });
  26851. return ui;
  26852. };
  26853. // 表情
  26854. editorui["emotion"] = function (editor, iframeUrl) {
  26855. var cmd = "emotion";
  26856. var ui = new editorui.MultiMenuPop({
  26857. title: editor.options.labelMap[cmd] || editor.getLang("labelMap." + cmd + "") || '',
  26858. editor: editor,
  26859. className: 'edui-for-' + cmd,
  26860. iframeUrl: editor.ui.mapUrl(iframeUrl || (editor.options.iframeUrlMap || {})[cmd] || iframeUrlMap[cmd])
  26861. });
  26862. editorui.buttons[cmd] = ui;
  26863. editor.addListener('selectionchange', function () {
  26864. ui.setDisabled(editor.queryCommandState(cmd) == -1)
  26865. });
  26866. return ui;
  26867. };
  26868. editorui.autotypeset = function (editor) {
  26869. var ui = new editorui.AutoTypeSetButton({
  26870. editor: editor,
  26871. title: editor.options.labelMap['autotypeset'] || editor.getLang("labelMap.autotypeset") || '',
  26872. className: 'edui-for-autotypeset',
  26873. onbuttonclick: function () {
  26874. editor.execCommand('autotypeset')
  26875. }
  26876. });
  26877. editorui.buttons['autotypeset'] = ui;
  26878. editor.addListener('selectionchange', function () {
  26879. ui.setDisabled(editor.queryCommandState('autotypeset') == -1);
  26880. });
  26881. return ui;
  26882. };
  26883. /* 简单上传插件 */
  26884. editorui["simpleupload"] = function (editor) {
  26885. var name = 'simpleupload',
  26886. ui = new editorui.Button({
  26887. className: 'edui-for-' + name,
  26888. title: editor.options.labelMap[name] || editor.getLang("labelMap." + name) || '',
  26889. onclick: function () {},
  26890. theme: editor.options.theme,
  26891. showText: false
  26892. });
  26893. editorui.buttons[name] = ui;
  26894. editor.addListener('ready', function () {
  26895. var b = ui.getDom('body'),
  26896. iconSpan = b.children[0];
  26897. editor.fireEvent('simpleuploadbtnready', iconSpan);
  26898. });
  26899. editor.addListener('selectionchange', function (type, causeByUi, uiReady) {
  26900. var state = editor.queryCommandState(name);
  26901. if (state == -1) {
  26902. ui.setDisabled(true);
  26903. ui.setChecked(false);
  26904. } else {
  26905. if (!uiReady) {
  26906. ui.setDisabled(false);
  26907. ui.setChecked(state);
  26908. }
  26909. }
  26910. });
  26911. return ui;
  26912. };
  26913. })();
  26914. // adapter/editor.js
  26915. ///import core
  26916. ///commands 全屏
  26917. ///commandsName FullScreen
  26918. ///commandsTitle 全屏
  26919. (function () {
  26920. var utils = baidu.editor.utils,
  26921. uiUtils = baidu.editor.ui.uiUtils,
  26922. UIBase = baidu.editor.ui.UIBase,
  26923. domUtils = baidu.editor.dom.domUtils;
  26924. var nodeStack = [];
  26925. function EditorUI(options) {
  26926. this.initOptions(options);
  26927. this.initEditorUI();
  26928. }
  26929. EditorUI.prototype = {
  26930. uiName: 'editor',
  26931. initEditorUI: function () {
  26932. this.editor.ui = this;
  26933. this._dialogs = {};
  26934. this.initUIBase();
  26935. this._initToolbars();
  26936. var editor = this.editor,
  26937. me = this;
  26938. editor.addListener('ready', function () {
  26939. //提供getDialog方法
  26940. editor.getDialog = function (name) {
  26941. return editor.ui._dialogs[name + "Dialog"];
  26942. };
  26943. domUtils.on(editor.window, 'scroll', function (evt) {
  26944. baidu.editor.ui.Popup.postHide(evt);
  26945. });
  26946. //提供编辑器实时宽高(全屏时宽高不变化)
  26947. editor.ui._actualFrameWidth = editor.options.initialFrameWidth;
  26948. UE.browser.ie && UE.browser.version === 6 && editor.container.ownerDocument.execCommand("BackgroundImageCache", false, true);
  26949. //display bottom-bar label based on config
  26950. if (editor.options.elementPathEnabled) {
  26951. editor.ui.getDom('elementpath').innerHTML = '<div class="edui-editor-breadcrumb">' + editor.getLang("elementPathTip") + ':</div>';
  26952. }
  26953. if (editor.options.wordCount) {
  26954. function countFn() {
  26955. setCount(editor, me);
  26956. domUtils.un(editor.document, "click", arguments.callee);
  26957. }
  26958. domUtils.on(editor.document, "click", countFn);
  26959. editor.ui.getDom('wordcount').innerHTML = editor.getLang("wordCountTip");
  26960. }
  26961. editor.ui._scale();
  26962. if (editor.options.scaleEnabled) {
  26963. if (editor.autoHeightEnabled) {
  26964. editor.disableAutoHeight();
  26965. }
  26966. me.enableScale();
  26967. } else {
  26968. me.disableScale();
  26969. }
  26970. if (!editor.options.elementPathEnabled && !editor.options.wordCount && !editor.options.scaleEnabled) {
  26971. editor.ui.getDom('elementpath').style.display = "none";
  26972. editor.ui.getDom('wordcount').style.display = "none";
  26973. editor.ui.getDom('scale').style.display = "none";
  26974. }
  26975. if (!editor.selection.isFocus()) return;
  26976. editor.fireEvent('selectionchange', false, true);
  26977. });
  26978. editor.addListener('mousedown', function (t, evt) {
  26979. var el = evt.target || evt.srcElement;
  26980. baidu.editor.ui.Popup.postHide(evt, el);
  26981. baidu.editor.ui.ShortCutMenu.postHide(evt);
  26982. });
  26983. editor.addListener("delcells", function () {
  26984. if (UE.ui['edittip']) {
  26985. new UE.ui['edittip'](editor);
  26986. }
  26987. editor.getDialog('edittip').open();
  26988. });
  26989. var pastePop, isPaste = false,
  26990. timer;
  26991. editor.addListener("afterpaste", function () {
  26992. if (editor.queryCommandState('pasteplain'))
  26993. return;
  26994. if (baidu.editor.ui.PastePicker) {
  26995. pastePop = new baidu.editor.ui.Popup({
  26996. content: new baidu.editor.ui.PastePicker({
  26997. editor: editor
  26998. }),
  26999. editor: editor,
  27000. className: 'edui-wordpastepop'
  27001. });
  27002. pastePop.render();
  27003. }
  27004. isPaste = true;
  27005. });
  27006. editor.addListener("afterinserthtml", function () {
  27007. clearTimeout(timer);
  27008. timer = setTimeout(function () {
  27009. if (pastePop && (isPaste || editor.ui._isTransfer)) {
  27010. if (pastePop.isHidden()) {
  27011. var span = domUtils.createElement(editor.document, 'span', {
  27012. 'style': "line-height:0px;",
  27013. 'innerHTML': '\ufeff'
  27014. }),
  27015. range = editor.selection.getRange();
  27016. range.insertNode(span);
  27017. var tmp = getDomNode(span, 'firstChild', 'previousSibling');
  27018. tmp && pastePop.showAnchor(tmp.nodeType == 3 ? tmp.parentNode : tmp);
  27019. domUtils.remove(span);
  27020. } else {
  27021. pastePop.show();
  27022. }
  27023. delete editor.ui._isTransfer;
  27024. isPaste = false;
  27025. }
  27026. }, 200)
  27027. });
  27028. editor.addListener('contextmenu', function (t, evt) {
  27029. baidu.editor.ui.Popup.postHide(evt);
  27030. });
  27031. editor.addListener('keydown', function (t, evt) {
  27032. if (pastePop) pastePop.dispose(evt);
  27033. var keyCode = evt.keyCode || evt.which;
  27034. if (evt.altKey && keyCode == 90) {
  27035. UE.ui.buttons['fullscreen'].onclick();
  27036. }
  27037. });
  27038. editor.addListener('wordcount', function (type) {
  27039. setCount(this, me);
  27040. });
  27041. function setCount(editor, ui) {
  27042. editor.setOpt({
  27043. wordCount: true,
  27044. maximumWords: 10000,
  27045. wordCountMsg: editor.options.wordCountMsg || editor.getLang("wordCountMsg"),
  27046. wordOverFlowMsg: editor.options.wordOverFlowMsg || editor.getLang("wordOverFlowMsg")
  27047. });
  27048. var opt = editor.options,
  27049. max = opt.maximumWords,
  27050. msg = opt.wordCountMsg,
  27051. errMsg = opt.wordOverFlowMsg,
  27052. countDom = ui.getDom('wordcount');
  27053. if (!opt.wordCount) {
  27054. return;
  27055. }
  27056. var count = editor.getContentLength(true);
  27057. if (count > max) {
  27058. countDom.innerHTML = errMsg;
  27059. editor.fireEvent("wordcountoverflow");
  27060. } else {
  27061. countDom.innerHTML = msg.replace("{#leave}", max - count).replace("{#count}", count);
  27062. }
  27063. }
  27064. editor.addListener('selectionchange', function () {
  27065. if (editor.options.elementPathEnabled) {
  27066. me[(editor.queryCommandState('elementpath') == -1 ? 'dis' : 'en') + 'ableElementPath']()
  27067. }
  27068. if (editor.options.scaleEnabled) {
  27069. me[(editor.queryCommandState('scale') == -1 ? 'dis' : 'en') + 'ableScale']();
  27070. }
  27071. });
  27072. var popup = new baidu.editor.ui.Popup({
  27073. editor: editor,
  27074. content: '',
  27075. className: 'edui-bubble',
  27076. _onEditButtonClick: function () {
  27077. this.hide();
  27078. editor.ui._dialogs.linkDialog.open();
  27079. },
  27080. _onImgEditButtonClick: function (name) {
  27081. this.hide();
  27082. editor.ui._dialogs[name] && editor.ui._dialogs[name].open();
  27083. },
  27084. _onImgSetFloat: function (value) {
  27085. this.hide();
  27086. editor.execCommand("imagefloat", value);
  27087. },
  27088. _setIframeAlign: function (value) {
  27089. var frame = popup.anchorEl;
  27090. var newFrame = frame.cloneNode(true);
  27091. switch (value) {
  27092. case -2:
  27093. newFrame.setAttribute("align", "");
  27094. break;
  27095. case -1:
  27096. newFrame.setAttribute("align", "left");
  27097. break;
  27098. case 1:
  27099. newFrame.setAttribute("align", "right");
  27100. break;
  27101. }
  27102. frame.parentNode.insertBefore(newFrame, frame);
  27103. domUtils.remove(frame);
  27104. popup.anchorEl = newFrame;
  27105. popup.showAnchor(popup.anchorEl);
  27106. },
  27107. _updateIframe: function () {
  27108. var frame = editor._iframe = popup.anchorEl;
  27109. if (domUtils.hasClass(frame, 'ueditor_baidumap')) {
  27110. editor.selection.getRange().selectNode(frame).select();
  27111. editor.ui._dialogs.mapDialog.open();
  27112. popup.hide();
  27113. } else {
  27114. editor.ui._dialogs.insertframeDialog.open();
  27115. popup.hide();
  27116. }
  27117. },
  27118. _onRemoveButtonClick: function (cmdName) {
  27119. editor.execCommand(cmdName);
  27120. this.hide();
  27121. },
  27122. queryAutoHide: function (el) {
  27123. if (el && el.ownerDocument == editor.document) {
  27124. if (el.tagName.toLowerCase() == 'img' || domUtils.findParentByTagName(el, 'a', true)) {
  27125. return el !== popup.anchorEl;
  27126. }
  27127. }
  27128. return baidu.editor.ui.Popup.prototype.queryAutoHide.call(this, el);
  27129. }
  27130. });
  27131. popup.render();
  27132. if (editor.options.imagePopup) {
  27133. editor.addListener('mouseover', function (t, evt) {
  27134. evt = evt || window.event;
  27135. var el = evt.target || evt.srcElement;
  27136. if (editor.ui._dialogs.insertframeDialog && /iframe/ig.test(el.tagName)) {
  27137. var html = popup.formatHtml(
  27138. '<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;' +
  27139. ' <span onclick="$$._updateIframe( this);" class="edui-clickable">' + editor.getLang("modify") + '</span></nobr>');
  27140. if (html) {
  27141. popup.getDom('content').innerHTML = html;
  27142. popup.anchorEl = el;
  27143. popup.showAnchor(popup.anchorEl);
  27144. } else {
  27145. popup.hide();
  27146. }
  27147. }
  27148. });
  27149. editor.addListener('selectionchange', function (t, causeByUi) {
  27150. if (!causeByUi) return;
  27151. var html = '',
  27152. str = "",
  27153. img = editor.selection.getRange().getClosedNode(),
  27154. dialogs = editor.ui._dialogs;
  27155. if (img && img.tagName == 'IMG') {
  27156. var dialogName = 'insertimageDialog';
  27157. if (img.className.indexOf("edui-faked-video") != -1 || img.className.indexOf("edui-upload-video") != -1) {
  27158. dialogName = "insertvideoDialog"
  27159. }
  27160. if (img.className.indexOf("edui-faked-webapp") != -1) {
  27161. dialogName = "webappDialog"
  27162. }
  27163. if (img.src.indexOf("http://api.map.baidu.com") != -1) {
  27164. dialogName = "mapDialog"
  27165. }
  27166. if (img.className.indexOf("edui-faked-music") != -1) {
  27167. dialogName = "musicDialog"
  27168. }
  27169. if (img.src.indexOf("http://maps.google.com/maps/api/staticmap") != -1) {
  27170. dialogName = "gmapDialog"
  27171. }
  27172. if (img.getAttribute("anchorname")) {
  27173. dialogName = "anchorDialog";
  27174. html = popup.formatHtml(
  27175. '<nobr>' + editor.getLang("property") + ': <span onclick=$$._onImgEditButtonClick("anchorDialog") class="edui-clickable">' + editor.getLang("modify") + '</span>&nbsp;&nbsp;' +
  27176. '<span onclick=$$._onRemoveButtonClick(\'anchor\') class="edui-clickable">' + editor.getLang("delete") + '</span></nobr>');
  27177. }
  27178. if (img.getAttribute("word_img")) {
  27179. //todo 放到dialog去做查询
  27180. editor.word_img = [img.getAttribute("word_img")];
  27181. dialogName = "wordimageDialog"
  27182. }
  27183. if (domUtils.hasClass(img, 'loadingclass') || domUtils.hasClass(img, 'loaderrorclass')) {
  27184. dialogName = "";
  27185. }
  27186. if (!dialogs[dialogName]) {
  27187. return;
  27188. }
  27189. str = '<nobr>' + editor.getLang("property") + ': ' +
  27190. '<span onclick=$$._onImgSetFloat("none") class="edui-clickable">' + editor.getLang("default") + '</span>&nbsp;&nbsp;' +
  27191. '<span onclick=$$._onImgSetFloat("left") class="edui-clickable">' + editor.getLang("justifyleft") + '</span>&nbsp;&nbsp;' +
  27192. '<span onclick=$$._onImgSetFloat("right") class="edui-clickable">' + editor.getLang("justifyright") + '</span>&nbsp;&nbsp;' +
  27193. '<span onclick=$$._onImgSetFloat("center") class="edui-clickable">' + editor.getLang("justifycenter") + '</span>&nbsp;&nbsp;' +
  27194. '<span onclick="$$._onImgEditButtonClick(\'' + dialogName + '\');" class="edui-clickable">' + editor.getLang("modify") + '</span></nobr>';
  27195. !html && (html = popup.formatHtml(str))
  27196. }
  27197. if (editor.ui._dialogs.linkDialog) {
  27198. var link = editor.queryCommandValue('link');
  27199. var url;
  27200. if (link && (url = (link.getAttribute('_href') || link.getAttribute('href', 2)))) {
  27201. var txt = url;
  27202. if (url.length > 30) {
  27203. txt = url.substring(0, 20) + "...";
  27204. }
  27205. if (html) {
  27206. html += '<div style="height:5px;"></div>'
  27207. }
  27208. html += popup.formatHtml(
  27209. '<nobr>' + editor.getLang("anthorMsg") + ': <a target="_blank" href="' + url + '" title="' + url + '" >' + txt + '</a>' +
  27210. ' <span class="edui-clickable" onclick="$$._onEditButtonClick();">' + editor.getLang("modify") + '</span>' +
  27211. ' <span class="edui-clickable" onclick="$$._onRemoveButtonClick(\'unlink\');"> ' + editor.getLang("clear") + '</span></nobr>');
  27212. popup.showAnchor(link);
  27213. }
  27214. }
  27215. if (html) {
  27216. popup.getDom('content').innerHTML = html;
  27217. popup.anchorEl = img || link;
  27218. popup.showAnchor(popup.anchorEl);
  27219. } else {
  27220. popup.hide();
  27221. }
  27222. });
  27223. }
  27224. },
  27225. _initToolbars: function () {
  27226. var editor = this.editor;
  27227. var toolbars = this.toolbars || [];
  27228. var toolbarUis = [];
  27229. for (var i = 0; i < toolbars.length; i++) {
  27230. var toolbar = toolbars[i];
  27231. var toolbarUi = new baidu.editor.ui.Toolbar({
  27232. theme: editor.options.theme
  27233. });
  27234. for (var j = 0; j < toolbar.length; j++) {
  27235. var toolbarItem = toolbar[j];
  27236. var toolbarItemUi = null;
  27237. if (typeof toolbarItem == 'string') {
  27238. toolbarItem = toolbarItem.toLowerCase();
  27239. if (toolbarItem == '|') {
  27240. toolbarItem = 'Separator';
  27241. }
  27242. if (toolbarItem == '||') {
  27243. toolbarItem = 'Breakline';
  27244. }
  27245. if (baidu.editor.ui[toolbarItem]) {
  27246. toolbarItemUi = new baidu.editor.ui[toolbarItem](editor);
  27247. }
  27248. //fullscreen这里单独处理一下,放到首行去
  27249. if (toolbarItem == 'fullscreen') {
  27250. if (toolbarUis && toolbarUis[0]) {
  27251. toolbarUis[0].items.splice(0, 0, toolbarItemUi);
  27252. } else {
  27253. toolbarItemUi && toolbarUi.items.splice(0, 0, toolbarItemUi);
  27254. }
  27255. continue;
  27256. }
  27257. } else {
  27258. toolbarItemUi = toolbarItem;
  27259. }
  27260. if (toolbarItemUi && toolbarItemUi.id) {
  27261. toolbarUi.add(toolbarItemUi);
  27262. }
  27263. }
  27264. toolbarUis[i] = toolbarUi;
  27265. }
  27266. //接受外部定制的UI(修复因 utils.each 无法准确的循环出对象的全部元素而导致的自定义 UI 不符合预期的 BUG by HaoChuan9421)
  27267. // utils.each(UE._customizeUI,function(obj,key){
  27268. // var itemUI,index;
  27269. // if(obj.id && obj.id != editor.key){
  27270. // return false;
  27271. // }
  27272. // itemUI = obj.execFn.call(editor,editor,key);
  27273. // if(itemUI){
  27274. // index = obj.index;
  27275. // if(index === undefined){
  27276. // index = toolbarUi.items.length;
  27277. // }
  27278. // toolbarUi.add(itemUI,index)
  27279. // }
  27280. // });
  27281. for (var key in UE._customizeUI) {
  27282. var obj = UE._customizeUI[key]
  27283. var itemUI, index;
  27284. if (!obj.id || obj.id == editor.key) {
  27285. itemUI = obj.execFn.call(editor, editor, key);
  27286. if (itemUI) {
  27287. index = obj.index;
  27288. if (index === undefined) {
  27289. index = toolbarUi.items.length;
  27290. }
  27291. toolbarUi.add(itemUI, index)
  27292. }
  27293. }
  27294. }
  27295. this.toolbars = toolbarUis;
  27296. },
  27297. getHtmlTpl: function () {
  27298. return '<div id="##" class="%%">' +
  27299. '<div id="##_toolbarbox" class="%%-toolbarbox">' +
  27300. (this.toolbars.length ?
  27301. '<div id="##_toolbarboxouter" class="%%-toolbarboxouter"><div class="%%-toolbarboxinner">' +
  27302. this.renderToolbarBoxHtml() +
  27303. '</div></div>' : '') +
  27304. '<div id="##_toolbarmsg" class="%%-toolbarmsg" style="display:none;">' +
  27305. '<div id = "##_upload_dialog" class="%%-toolbarmsg-upload" onclick="$$.showWordImageDialog();">' + this.editor.getLang("clickToUpload") + '</div>' +
  27306. '<div class="%%-toolbarmsg-close" onclick="$$.hideToolbarMsg();">x</div>' +
  27307. '<div id="##_toolbarmsg_label" class="%%-toolbarmsg-label"></div>' +
  27308. '<div style="height:0;overflow:hidden;clear:both;"></div>' +
  27309. '</div>' +
  27310. '<div id="##_message_holder" class="%%-messageholder"></div>' +
  27311. '</div>' +
  27312. '<div id="##_iframeholder" class="%%-iframeholder">' +
  27313. '</div>' +
  27314. //modify wdcount by matao
  27315. '<div id="##_bottombar" class="%%-bottomContainer"><table><tr>' +
  27316. '<td id="##_elementpath" class="%%-bottombar"></td>' +
  27317. '<td id="##_wordcount" class="%%-wordcount"></td>' +
  27318. '<td id="##_scale" class="%%-scale"><div class="%%-icon"></div></td>' +
  27319. '</tr></table></div>' +
  27320. '<div id="##_scalelayer"></div>' +
  27321. '</div>';
  27322. },
  27323. showWordImageDialog: function () {
  27324. this._dialogs['wordimageDialog'].open();
  27325. },
  27326. renderToolbarBoxHtml: function () {
  27327. var buff = [];
  27328. for (var i = 0; i < this.toolbars.length; i++) {
  27329. buff.push(this.toolbars[i].renderHtml());
  27330. }
  27331. return buff.join('');
  27332. },
  27333. setFullScreen: function (fullscreen) {
  27334. var editor = this.editor,
  27335. container = editor.container.parentNode.parentNode;
  27336. if (this._fullscreen != fullscreen) {
  27337. this._fullscreen = fullscreen;
  27338. this.editor.fireEvent('beforefullscreenchange', fullscreen);
  27339. if (baidu.editor.browser.gecko) {
  27340. var bk = editor.selection.getRange().createBookmark();
  27341. }
  27342. if (fullscreen) {
  27343. while (container.tagName != "BODY") {
  27344. var position = baidu.editor.dom.domUtils.getComputedStyle(container, "position");
  27345. nodeStack.push(position);
  27346. container.style.position = "static";
  27347. container = container.parentNode;
  27348. }
  27349. this._bakHtmlOverflow = document.documentElement.style.overflow;
  27350. this._bakBodyOverflow = document.body.style.overflow;
  27351. this._bakAutoHeight = this.editor.autoHeightEnabled;
  27352. this._bakScrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
  27353. this._bakEditorContaninerWidth = editor.iframe.parentNode.offsetWidth;
  27354. if (this._bakAutoHeight) {
  27355. //当全屏时不能执行自动长高
  27356. editor.autoHeightEnabled = false;
  27357. this.editor.disableAutoHeight();
  27358. }
  27359. document.documentElement.style.overflow = 'hidden';
  27360. //修复,滚动条不收起的问题
  27361. window.scrollTo(0, window.scrollY);
  27362. this._bakCssText = this.getDom().style.cssText;
  27363. this._bakCssText1 = this.getDom('iframeholder').style.cssText;
  27364. editor.iframe.parentNode.style.width = '';
  27365. this._updateFullScreen();
  27366. } else {
  27367. while (container.tagName != "BODY") {
  27368. container.style.position = nodeStack.shift();
  27369. container = container.parentNode;
  27370. }
  27371. this.getDom().style.cssText = this._bakCssText;
  27372. this.getDom('iframeholder').style.cssText = this._bakCssText1;
  27373. if (this._bakAutoHeight) {
  27374. editor.autoHeightEnabled = true;
  27375. this.editor.enableAutoHeight();
  27376. }
  27377. document.documentElement.style.overflow = this._bakHtmlOverflow;
  27378. document.body.style.overflow = this._bakBodyOverflow;
  27379. editor.iframe.parentNode.style.width = this._bakEditorContaninerWidth + 'px';
  27380. window.scrollTo(0, this._bakScrollTop);
  27381. }
  27382. if (browser.gecko && editor.body.contentEditable === 'true') {
  27383. var input = document.createElement('input');
  27384. document.body.appendChild(input);
  27385. editor.body.contentEditable = false;
  27386. setTimeout(function () {
  27387. input.focus();
  27388. setTimeout(function () {
  27389. editor.body.contentEditable = true;
  27390. editor.fireEvent('fullscreenchanged', fullscreen);
  27391. editor.selection.getRange().moveToBookmark(bk).select(true);
  27392. baidu.editor.dom.domUtils.remove(input);
  27393. fullscreen && window.scroll(0, 0);
  27394. }, 0)
  27395. }, 0)
  27396. }
  27397. if (editor.body.contentEditable === 'true') {
  27398. this.editor.fireEvent('fullscreenchanged', fullscreen);
  27399. this.triggerLayout();
  27400. }
  27401. }
  27402. },
  27403. _updateFullScreen: function () {
  27404. if (this._fullscreen) {
  27405. var vpRect = uiUtils.getViewportRect();
  27406. 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);
  27407. uiUtils.setViewportOffset(this.getDom(), {
  27408. left: 0,
  27409. top: this.editor.options.topOffset || 0
  27410. });
  27411. this.editor.setHeight(vpRect.height - this.getDom('toolbarbox').offsetHeight - this.getDom('bottombar').offsetHeight - (this.editor.options.topOffset || 0), true);
  27412. //不手动调一下,会导致全屏失效
  27413. if (browser.gecko) {
  27414. try {
  27415. window.onresize();
  27416. } catch (e) {
  27417. }
  27418. }
  27419. }
  27420. },
  27421. _updateElementPath: function () {
  27422. var bottom = this.getDom('elementpath'),
  27423. list;
  27424. if (this.elementPathEnabled && (list = this.editor.queryCommandValue('elementpath'))) {
  27425. var buff = [];
  27426. for (var i = 0, ci; ci = list[i]; i++) {
  27427. buff[i] = this.formatHtml('<span unselectable="on" onclick="$$.editor.execCommand(&quot;elementpath&quot;, &quot;' + i + '&quot;);">' + ci + '</span>');
  27428. }
  27429. bottom.innerHTML = '<div class="edui-editor-breadcrumb" onmousedown="return false;">' + this.editor.getLang("elementPathTip") + ': ' + buff.join(' &gt; ') + '</div>';
  27430. } else {
  27431. bottom.style.display = 'none'
  27432. }
  27433. },
  27434. disableElementPath: function () {
  27435. var bottom = this.getDom('elementpath');
  27436. bottom.innerHTML = '';
  27437. bottom.style.display = 'none';
  27438. this.elementPathEnabled = false;
  27439. },
  27440. enableElementPath: function () {
  27441. var bottom = this.getDom('elementpath');
  27442. bottom.style.display = '';
  27443. this.elementPathEnabled = true;
  27444. this._updateElementPath();
  27445. },
  27446. _scale: function () {
  27447. var doc = document,
  27448. editor = this.editor,
  27449. editorHolder = editor.container,
  27450. editorDocument = editor.document,
  27451. toolbarBox = this.getDom("toolbarbox"),
  27452. bottombar = this.getDom("bottombar"),
  27453. scale = this.getDom("scale"),
  27454. scalelayer = this.getDom("scalelayer");
  27455. var isMouseMove = false,
  27456. position = null,
  27457. minEditorHeight = 0,
  27458. minEditorWidth = editor.options.minFrameWidth,
  27459. pageX = 0,
  27460. pageY = 0,
  27461. scaleWidth = 0,
  27462. scaleHeight = 0;
  27463. function down() {
  27464. position = domUtils.getXY(editorHolder);
  27465. if (!minEditorHeight) {
  27466. minEditorHeight = editor.options.minFrameHeight + toolbarBox.offsetHeight + bottombar.offsetHeight;
  27467. }
  27468. 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:" +
  27469. editorHolder.offsetHeight + "px;z-index:" + (editor.options.zIndex + 1);
  27470. domUtils.on(doc, "mousemove", move);
  27471. domUtils.on(editorDocument, "mouseup", up);
  27472. domUtils.on(doc, "mouseup", up);
  27473. }
  27474. var me = this;
  27475. //by xuheng 全屏时关掉缩放
  27476. this.editor.addListener('fullscreenchanged', function (e, fullScreen) {
  27477. if (fullScreen) {
  27478. me.disableScale();
  27479. } else {
  27480. if (me.editor.options.scaleEnabled) {
  27481. me.enableScale();
  27482. var tmpNode = me.editor.document.createElement('span');
  27483. me.editor.body.appendChild(tmpNode);
  27484. me.editor.body.style.height = Math.max(domUtils.getXY(tmpNode).y, me.editor.iframe.offsetHeight - 20) + 'px';
  27485. domUtils.remove(tmpNode)
  27486. }
  27487. }
  27488. });
  27489. function move(event) {
  27490. clearSelection();
  27491. var e = event || window.event;
  27492. pageX = e.pageX || (doc.documentElement.scrollLeft + e.clientX);
  27493. pageY = e.pageY || (doc.documentElement.scrollTop + e.clientY);
  27494. scaleWidth = pageX - position.x;
  27495. scaleHeight = pageY - position.y;
  27496. if (scaleWidth >= minEditorWidth) {
  27497. isMouseMove = true;
  27498. scalelayer.style.width = scaleWidth + 'px';
  27499. }
  27500. if (scaleHeight >= minEditorHeight) {
  27501. isMouseMove = true;
  27502. scalelayer.style.height = scaleHeight + "px";
  27503. }
  27504. }
  27505. function up() {
  27506. if (isMouseMove) {
  27507. isMouseMove = false;
  27508. editor.ui._actualFrameWidth = scalelayer.offsetWidth - 2;
  27509. editorHolder.style.width = editor.ui._actualFrameWidth + 'px';
  27510. editor.setHeight(scalelayer.offsetHeight - bottombar.offsetHeight - toolbarBox.offsetHeight - 2, true);
  27511. }
  27512. if (scalelayer) {
  27513. scalelayer.style.display = "none";
  27514. }
  27515. clearSelection();
  27516. domUtils.un(doc, "mousemove", move);
  27517. domUtils.un(editorDocument, "mouseup", up);
  27518. domUtils.un(doc, "mouseup", up);
  27519. }
  27520. function clearSelection() {
  27521. if (browser.ie)
  27522. doc.selection.clear();
  27523. else
  27524. window.getSelection().removeAllRanges();
  27525. }
  27526. this.enableScale = function () {
  27527. //trace:2868
  27528. if (editor.queryCommandState("source") == 1) return;
  27529. scale.style.display = "";
  27530. this.scaleEnabled = true;
  27531. domUtils.on(scale, "mousedown", down);
  27532. };
  27533. this.disableScale = function () {
  27534. scale.style.display = "none";
  27535. this.scaleEnabled = false;
  27536. domUtils.un(scale, "mousedown", down);
  27537. };
  27538. },
  27539. isFullScreen: function () {
  27540. return this._fullscreen;
  27541. },
  27542. postRender: function () {
  27543. UIBase.prototype.postRender.call(this);
  27544. for (var i = 0; i < this.toolbars.length; i++) {
  27545. this.toolbars[i].postRender();
  27546. }
  27547. var me = this;
  27548. var timerId,
  27549. domUtils = baidu.editor.dom.domUtils,
  27550. updateFullScreenTime = function () {
  27551. clearTimeout(timerId);
  27552. timerId = setTimeout(function () {
  27553. me._updateFullScreen();
  27554. });
  27555. };
  27556. domUtils.on(window, 'resize', updateFullScreenTime);
  27557. me.addListener('destroy', function () {
  27558. domUtils.un(window, 'resize', updateFullScreenTime);
  27559. clearTimeout(timerId);
  27560. })
  27561. },
  27562. showToolbarMsg: function (msg, flag) {
  27563. this.getDom('toolbarmsg_label').innerHTML = msg;
  27564. this.getDom('toolbarmsg').style.display = '';
  27565. //
  27566. if (!flag) {
  27567. var w = this.getDom('upload_dialog');
  27568. w.style.display = 'none';
  27569. }
  27570. },
  27571. hideToolbarMsg: function () {
  27572. this.getDom('toolbarmsg').style.display = 'none';
  27573. },
  27574. mapUrl: function (url) {
  27575. return url ? url.replace('~/', this.editor.options.UEDITOR_HOME_URL || '') : ''
  27576. },
  27577. triggerLayout: function () {
  27578. var dom = this.getDom();
  27579. if (dom.style.zoom == '1') {
  27580. dom.style.zoom = '100%';
  27581. } else {
  27582. dom.style.zoom = '1';
  27583. }
  27584. }
  27585. };
  27586. utils.inherits(EditorUI, baidu.editor.ui.UIBase);
  27587. var instances = {};
  27588. UE.ui.Editor = function (options) {
  27589. var editor = new UE.Editor(options);
  27590. editor.options.editor = editor;
  27591. utils.loadFile(document, {
  27592. href: editor.options.themePath + editor.options.theme + "/css/ueditor.css",
  27593. tag: "link",
  27594. type: "text/css",
  27595. rel: "stylesheet"
  27596. });
  27597. var oldRender = editor.render;
  27598. editor.render = function (holder) {
  27599. if (holder.constructor === String) {
  27600. editor.key = holder;
  27601. instances[holder] = editor;
  27602. }
  27603. utils.domReady(function () {
  27604. editor.langIsReady ? renderUI() : editor.addListener("langReady", renderUI);
  27605. function renderUI() {
  27606. editor.setOpt({
  27607. labelMap: editor.options.labelMap || editor.getLang('labelMap')
  27608. });
  27609. new EditorUI(editor.options);
  27610. if (holder) {
  27611. if (holder.constructor === String) {
  27612. holder = document.getElementById(holder);
  27613. }
  27614. holder && holder.getAttribute('name') && (editor.options.textarea = holder.getAttribute('name'));
  27615. if (holder && /script|textarea/ig.test(holder.tagName)) {
  27616. var newDiv = document.createElement('div');
  27617. holder.parentNode.insertBefore(newDiv, holder);
  27618. var cont = holder.value || holder.innerHTML;
  27619. editor.options.initialContent = /^[\t\r\n ]*$/.test(cont) ? editor.options.initialContent :
  27620. cont.replace(/>[\n\r\t]+([ ]{4})+/g, '>')
  27621. .replace(/[\n\r\t]+([ ]{4})+</g, '<')
  27622. .replace(/>[\n\r\t]+</g, '><');
  27623. holder.className && (newDiv.className = holder.className);
  27624. holder.style.cssText && (newDiv.style.cssText = holder.style.cssText);
  27625. if (/textarea/i.test(holder.tagName)) {
  27626. editor.textarea = holder;
  27627. editor.textarea.style.display = 'none';
  27628. } else {
  27629. holder.parentNode.removeChild(holder);
  27630. }
  27631. if (holder.id) {
  27632. newDiv.id = holder.id;
  27633. domUtils.removeAttributes(holder, 'id');
  27634. }
  27635. holder = newDiv;
  27636. holder.innerHTML = '';
  27637. }
  27638. }
  27639. domUtils.addClass(holder, "edui-" + editor.options.theme);
  27640. editor.ui.render(holder);
  27641. var opt = editor.options;
  27642. //给实例添加一个编辑器的容器引用
  27643. editor.container = editor.ui.getDom();
  27644. var parents = domUtils.findParents(holder, true);
  27645. var displays = [];
  27646. for (var i = 0, ci; ci = parents[i]; i++) {
  27647. displays[i] = ci.style.display;
  27648. ci.style.display = 'block'
  27649. }
  27650. if (opt.initialFrameWidth) {
  27651. opt.minFrameWidth = opt.initialFrameWidth;
  27652. } else {
  27653. opt.minFrameWidth = opt.initialFrameWidth = holder.offsetWidth;
  27654. var styleWidth = holder.style.width;
  27655. if (/%$/.test(styleWidth)) {
  27656. opt.initialFrameWidth = styleWidth;
  27657. }
  27658. }
  27659. if (opt.initialFrameHeight) {
  27660. opt.minFrameHeight = opt.initialFrameHeight;
  27661. } else {
  27662. opt.initialFrameHeight = opt.minFrameHeight = holder.offsetHeight;
  27663. }
  27664. for (var i = 0, ci; ci = parents[i]; i++) {
  27665. ci.style.display = displays[i]
  27666. }
  27667. //编辑器最外容器设置了高度,会导致,编辑器不占位
  27668. //todo 先去掉,没有找到原因
  27669. if (holder.style.height) {
  27670. holder.style.height = ''
  27671. }
  27672. editor.container.style.width = opt.initialFrameWidth + (/%$/.test(opt.initialFrameWidth) ? '' : 'px');
  27673. editor.container.style.zIndex = opt.zIndex;
  27674. oldRender.call(editor, editor.ui.getDom('iframeholder'));
  27675. editor.fireEvent("afteruiready");
  27676. }
  27677. })
  27678. };
  27679. return editor;
  27680. };
  27681. /**
  27682. * @file
  27683. * @name UE
  27684. * @short UE
  27685. * @desc UEditor的顶部命名空间
  27686. */
  27687. /**
  27688. * @name getEditor
  27689. * @since 1.2.4+
  27690. * @grammar UE.getEditor(id,[opt]) => Editor实例
  27691. * @desc 提供一个全局的方法得到编辑器实例
  27692. *
  27693. * * ''id'' 放置编辑器的容器id, 如果容器下的编辑器已经存在,就直接返回
  27694. * * ''opt'' 编辑器的可选参数
  27695. * @example
  27696. * UE.getEditor('containerId',{onready:function(){//创建一个编辑器实例
  27697. * this.setContent('hello')
  27698. * }});
  27699. * UE.getEditor('containerId'); //返回刚创建的实例
  27700. *
  27701. */
  27702. UE.getEditor = function (id, opt) {
  27703. var editor = instances[id];
  27704. if (!editor) {
  27705. editor = instances[id] = new UE.ui.Editor(opt);
  27706. editor.render(id);
  27707. }
  27708. return editor;
  27709. };
  27710. UE.delEditor = function (id) {
  27711. var editor;
  27712. if (editor = instances[id]) {
  27713. editor.key && editor.destroy();
  27714. delete instances[id]
  27715. }
  27716. };
  27717. UE.registerUI = function (uiName, fn, index, editorId) {
  27718. utils.each(uiName.split(/\s+/), function (name) {
  27719. UE._customizeUI[name] = {
  27720. id: editorId,
  27721. execFn: fn,
  27722. index: index
  27723. };
  27724. })
  27725. }
  27726. })();
  27727. // adapter/message.js
  27728. UE.registerUI('message', function (editor) {
  27729. var editorui = baidu.editor.ui;
  27730. var Message = editorui.Message;
  27731. var holder;
  27732. var _messageItems = [];
  27733. var me = editor;
  27734. me.addListener('ready', function () {
  27735. holder = document.getElementById(me.ui.id + '_message_holder');
  27736. updateHolderPos();
  27737. // HaoChuan9421
  27738. // setTimeout(function(){
  27739. // updateHolderPos();
  27740. // }, 500);
  27741. });
  27742. me.addListener('showmessage', function (type, opt) {
  27743. opt = utils.isString(opt) ? {
  27744. 'content': opt
  27745. } : opt;
  27746. var message = new Message({
  27747. 'timeout': opt.timeout,
  27748. 'type': opt.type,
  27749. 'content': opt.content,
  27750. 'keepshow': opt.keepshow,
  27751. 'editor': me
  27752. }),
  27753. mid = opt.id || ('msg_' + (+new Date()).toString(36));
  27754. message.render(holder);
  27755. _messageItems[mid] = message;
  27756. message.reset(opt);
  27757. updateHolderPos();
  27758. return mid;
  27759. });
  27760. me.addListener('updatemessage', function (type, id, opt) {
  27761. opt = utils.isString(opt) ? {
  27762. 'content': opt
  27763. } : opt;
  27764. var message = _messageItems[id];
  27765. message.render(holder);
  27766. message && message.reset(opt);
  27767. });
  27768. me.addListener('hidemessage', function (type, id) {
  27769. var message = _messageItems[id];
  27770. message && message.hide();
  27771. });
  27772. function updateHolderPos() {
  27773. var toolbarbox = me.ui.getDom('toolbarbox');
  27774. if (toolbarbox) {
  27775. holder.style.top = toolbarbox.offsetHeight + 3 + 'px';
  27776. }
  27777. holder.style.zIndex = Math.max(me.options.zIndex, me.iframe.style.zIndex) + 1;
  27778. }
  27779. });
  27780. // adapter/autosave.js
  27781. UE.registerUI('autosave', function (editor) {
  27782. var timer = null,
  27783. uid = null;
  27784. editor.on('afterautosave', function () {
  27785. clearTimeout(timer);
  27786. timer = setTimeout(function () {
  27787. if (uid) {
  27788. editor.trigger('hidemessage', uid);
  27789. }
  27790. uid = editor.trigger('showmessage', {
  27791. content: editor.getLang('autosave.success'),
  27792. timeout: 2000
  27793. });
  27794. }, 2000)
  27795. })
  27796. });
  27797. })();