ueditor.all.js 998 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931
  1. /*!
  2. * ueditor
  3. * version: 2.0.0
  4. * build: Tue Sep 07 2021 18:13:40 GMT+0800 (China Standard Time)
  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 = {
  12. plugins: {},
  13. commands: {},
  14. instants: {},
  15. I18N: {},
  16. _customizeUI: {},
  17. version: "1.5.0"
  18. };
  19. var dom = (UE.dom = {});
  20. // core/browser.js
  21. /**
  22. * 浏览器判断模块
  23. * @file
  24. * @module UE.browser
  25. * @since 1.2.6.1
  26. */
  27. /**
  28. * 提供浏览器检测的模块
  29. * @unfile
  30. * @module UE.browser
  31. */
  32. var browser = (UE.browser = (function() {
  33. var agent = navigator.userAgent.toLowerCase(),
  34. opera = window.opera,
  35. browser = {
  36. /**
  37. * @property {boolean} ie 检测当前浏览器是否为IE
  38. * @example
  39. * ```javascript
  40. * if ( UE.browser.ie ) {
  41. * console.log( '当前浏览器是IE' );
  42. * }
  43. * ```
  44. */
  45. ie: /(msie\s|trident.*rv:)([\w.]+)/i.test(agent),
  46. /**
  47. * @property {boolean} opera 检测当前浏览器是否为Opera
  48. * @example
  49. * ```javascript
  50. * if ( UE.browser.opera ) {
  51. * console.log( '当前浏览器是Opera' );
  52. * }
  53. * ```
  54. */
  55. opera: !!opera && opera.version,
  56. /**
  57. * @property {boolean} webkit 检测当前浏览器是否是webkit内核的浏览器
  58. * @example
  59. * ```javascript
  60. * if ( UE.browser.webkit ) {
  61. * console.log( '当前浏览器是webkit内核浏览器' );
  62. * }
  63. * ```
  64. */
  65. webkit: agent.indexOf(" applewebkit/") > -1,
  66. /**
  67. * @property {boolean} mac 检测当前浏览器是否是运行在mac平台下
  68. * @example
  69. * ```javascript
  70. * if ( UE.browser.mac ) {
  71. * console.log( '当前浏览器运行在mac平台下' );
  72. * }
  73. * ```
  74. */
  75. mac: agent.indexOf("macintosh") > -1,
  76. /**
  77. * @property {boolean} quirks 检测当前浏览器是否处于“怪异模式”下
  78. * @example
  79. * ```javascript
  80. * if ( UE.browser.quirks ) {
  81. * console.log( '当前浏览器运行处于“怪异模式”' );
  82. * }
  83. * ```
  84. */
  85. quirks: document.compatMode == "BackCompat"
  86. };
  87. /**
  88. * @property {boolean} gecko 检测当前浏览器内核是否是gecko内核
  89. * @example
  90. * ```javascript
  91. * if ( UE.browser.gecko ) {
  92. * console.log( '当前浏览器内核是gecko内核' );
  93. * }
  94. * ```
  95. */
  96. browser.gecko =
  97. navigator.product == "Gecko" &&
  98. !browser.webkit &&
  99. !browser.opera &&
  100. !browser.ie;
  101. var version = 0;
  102. // Internet Explorer 6.0+
  103. if (browser.ie) {
  104. var v1 = agent.match(/(?:msie\s([\w.]+))/);
  105. var v2 = agent.match(/(?:trident.*rv:([\w.]+))/);
  106. if (v1 && v2 && v1[1] && v2[1]) {
  107. version = Math.max(v1[1] * 1, v2[1] * 1);
  108. } else if (v1 && v1[1]) {
  109. version = v1[1] * 1;
  110. } else if (v2 && v2[1]) {
  111. version = v2[1] * 1;
  112. } else {
  113. version = 0;
  114. }
  115. browser.ie11Compat = document.documentMode == 11;
  116. /**
  117. * @property { boolean } ie9Compat 检测浏览器模式是否为 IE9 兼容模式
  118. * @warning 如果浏览器不是IE, 则该值为undefined
  119. * @example
  120. * ```javascript
  121. * if ( UE.browser.ie9Compat ) {
  122. * console.log( '当前浏览器运行在IE9兼容模式下' );
  123. * }
  124. * ```
  125. */
  126. browser.ie9Compat = document.documentMode == 9;
  127. /**
  128. * @property { boolean } ie8 检测浏览器是否是IE8浏览器
  129. * @warning 如果浏览器不是IE, 则该值为undefined
  130. * @example
  131. * ```javascript
  132. * if ( UE.browser.ie8 ) {
  133. * console.log( '当前浏览器是IE8浏览器' );
  134. * }
  135. * ```
  136. */
  137. browser.ie8 = !!document.documentMode;
  138. /**
  139. * @property { boolean } ie8Compat 检测浏览器模式是否为 IE8 兼容模式
  140. * @warning 如果浏览器不是IE, 则该值为undefined
  141. * @example
  142. * ```javascript
  143. * if ( UE.browser.ie8Compat ) {
  144. * console.log( '当前浏览器运行在IE8兼容模式下' );
  145. * }
  146. * ```
  147. */
  148. browser.ie8Compat = document.documentMode == 8;
  149. /**
  150. * @property { boolean } ie7Compat 检测浏览器模式是否为 IE7 兼容模式
  151. * @warning 如果浏览器不是IE, 则该值为undefined
  152. * @example
  153. * ```javascript
  154. * if ( UE.browser.ie7Compat ) {
  155. * console.log( '当前浏览器运行在IE7兼容模式下' );
  156. * }
  157. * ```
  158. */
  159. browser.ie7Compat =
  160. (version == 7 && !document.documentMode) || document.documentMode == 7;
  161. /**
  162. * @property { boolean } ie6Compat 检测浏览器模式是否为 IE6 模式 或者怪异模式
  163. * @warning 如果浏览器不是IE, 则该值为undefined
  164. * @example
  165. * ```javascript
  166. * if ( UE.browser.ie6Compat ) {
  167. * console.log( '当前浏览器运行在IE6模式或者怪异模式下' );
  168. * }
  169. * ```
  170. */
  171. browser.ie6Compat = version < 7 || browser.quirks;
  172. browser.ie9above = version > 8;
  173. browser.ie9below = version < 9;
  174. browser.ie11above = version > 10;
  175. browser.ie11below = version < 11;
  176. }
  177. // Gecko.
  178. if (browser.gecko) {
  179. var geckoRelease = agent.match(/rv:([\d\.]+)/);
  180. if (geckoRelease) {
  181. geckoRelease = geckoRelease[1].split(".");
  182. version =
  183. geckoRelease[0] * 10000 +
  184. (geckoRelease[1] || 0) * 100 +
  185. (geckoRelease[2] || 0) * 1;
  186. }
  187. }
  188. /**
  189. * @property { Number } chrome 检测当前浏览器是否为Chrome, 如果是,则返回Chrome的大版本号
  190. * @warning 如果浏览器不是chrome, 则该值为undefined
  191. * @example
  192. * ```javascript
  193. * if ( UE.browser.chrome ) {
  194. * console.log( '当前浏览器是Chrome' );
  195. * }
  196. * ```
  197. */
  198. if (/chrome\/(\d+\.\d)/i.test(agent)) {
  199. browser.chrome = +RegExp["\x241"];
  200. }
  201. /**
  202. * @property { Number } safari 检测当前浏览器是否为Safari, 如果是,则返回Safari的大版本号
  203. * @warning 如果浏览器不是safari, 则该值为undefined
  204. * @example
  205. * ```javascript
  206. * if ( UE.browser.safari ) {
  207. * console.log( '当前浏览器是Safari' );
  208. * }
  209. * ```
  210. */
  211. if (
  212. /(\d+\.\d)?(?:\.\d)?\s+safari\/?(\d+\.\d+)?/i.test(agent) &&
  213. !/chrome/i.test(agent)
  214. ) {
  215. browser.safari = +(RegExp["\x241"] || RegExp["\x242"]);
  216. }
  217. // Opera 9.50+
  218. if (browser.opera) version = parseFloat(opera.version());
  219. // WebKit 522+ (Safari 3+)
  220. if (browser.webkit)
  221. version = parseFloat(agent.match(/ applewebkit\/(\d+)/)[1]);
  222. /**
  223. * @property { Number } version 检测当前浏览器版本号
  224. * @remind
  225. * <ul>
  226. * <li>IE系列返回值为5,6,7,8,9,10等</li>
  227. * <li>gecko系列会返回10900,158900等</li>
  228. * <li>webkit系列会返回其build号 (如 522等)</li>
  229. * </ul>
  230. * @example
  231. * ```javascript
  232. * console.log( '当前浏览器版本号是: ' + UE.browser.version );
  233. * ```
  234. */
  235. browser.version = version;
  236. /**
  237. * @property { boolean } isCompatible 检测当前浏览器是否能够与UEditor良好兼容
  238. * @example
  239. * ```javascript
  240. * if ( UE.browser.isCompatible ) {
  241. * console.log( '浏览器与UEditor能够良好兼容' );
  242. * }
  243. * ```
  244. */
  245. browser.isCompatible =
  246. !browser.mobile &&
  247. ((browser.ie && version >= 6) ||
  248. (browser.gecko && version >= 10801) ||
  249. (browser.opera && version >= 9.5) ||
  250. (browser.air && version >= 1) ||
  251. (browser.webkit && version >= 522) ||
  252. false);
  253. return browser;
  254. })());
  255. //快捷方式
  256. var ie = browser.ie,
  257. webkit = browser.webkit,
  258. gecko = browser.gecko,
  259. opera = browser.opera;
  260. // core/utils.js
  261. /**
  262. * 工具函数包
  263. * @file
  264. * @module UE.utils
  265. * @since 1.2.6.1
  266. */
  267. /**
  268. * UEditor封装使用的静态工具函数
  269. * @module UE.utils
  270. * @unfile
  271. */
  272. var utils = (UE.utils = {
  273. /**
  274. * 用给定的迭代器遍历对象
  275. * @method each
  276. * @param { Object } obj 需要遍历的对象
  277. * @param { Function } iterator 迭代器, 该方法接受两个参数, 第一个参数是当前所处理的value, 第二个参数是当前遍历对象的key
  278. * @example
  279. * ```javascript
  280. * var demoObj = {
  281. * key1: 1,
  282. * key2: 2
  283. * };
  284. *
  285. * //output: key1: 1, key2: 2
  286. * UE.utils.each( demoObj, funciton ( value, key ) {
  287. *
  288. * console.log( key + ":" + value );
  289. *
  290. * } );
  291. * ```
  292. */
  293. /**
  294. * 用给定的迭代器遍历数组或类数组对象
  295. * @method each
  296. * @param { Array } array 需要遍历的数组或者类数组
  297. * @param { Function } iterator 迭代器, 该方法接受两个参数, 第一个参数是当前所处理的value, 第二个参数是当前遍历对象的key
  298. * @example
  299. * ```javascript
  300. * var divs = document.getElmentByTagNames( "div" );
  301. *
  302. * //output: 0: DIV, 1: DIV ...
  303. * UE.utils.each( divs, funciton ( value, key ) {
  304. *
  305. * console.log( key + ":" + value.tagName );
  306. *
  307. * } );
  308. * ```
  309. */
  310. each: function(obj, iterator, context) {
  311. if (obj == null) return;
  312. if (obj.length === +obj.length) {
  313. for (var i = 0, l = obj.length; i < l; i++) {
  314. if (iterator.call(context, obj[i], i, obj) === false) return false;
  315. }
  316. } else {
  317. for (var key in obj) {
  318. if (obj.hasOwnProperty(key)) {
  319. if (iterator.call(context, obj[key], key, obj) === false)
  320. return false;
  321. }
  322. }
  323. }
  324. },
  325. /**
  326. * 以给定对象作为原型创建一个新对象
  327. * @method makeInstance
  328. * @param { Object } protoObject 该对象将作为新创建对象的原型
  329. * @return { Object } 新的对象, 该对象的原型是给定的protoObject对象
  330. * @example
  331. * ```javascript
  332. *
  333. * var protoObject = { sayHello: function () { console.log('Hello UEditor!'); } };
  334. *
  335. * var newObject = UE.utils.makeInstance( protoObject );
  336. * //output: Hello UEditor!
  337. * newObject.sayHello();
  338. * ```
  339. */
  340. makeInstance: function(obj) {
  341. var noop = new Function();
  342. noop.prototype = obj;
  343. obj = new noop();
  344. noop.prototype = null;
  345. return obj;
  346. },
  347. /**
  348. * 将source对象中的属性扩展到target对象上
  349. * @method extend
  350. * @remind 该方法将强制把source对象上的属性复制到target对象上
  351. * @see UE.utils.extend(Object,Object,Boolean)
  352. * @param { Object } target 目标对象, 新的属性将附加到该对象上
  353. * @param { Object } source 源对象, 该对象的属性会被附加到target对象上
  354. * @return { Object } 返回target对象
  355. * @example
  356. * ```javascript
  357. *
  358. * var target = { name: 'target', sex: 1 },
  359. * source = { name: 'source', age: 17 };
  360. *
  361. * UE.utils.extend( target, source );
  362. *
  363. * //output: { name: 'source', sex: 1, age: 17 }
  364. * console.log( target );
  365. *
  366. * ```
  367. */
  368. /**
  369. * 将source对象中的属性扩展到target对象上, 根据指定的isKeepTarget值决定是否保留目标对象中与
  370. * 源对象属性名相同的属性值。
  371. * @method extend
  372. * @param { Object } target 目标对象, 新的属性将附加到该对象上
  373. * @param { Object } source 源对象, 该对象的属性会被附加到target对象上
  374. * @param { Boolean } isKeepTarget 是否保留目标对象中与源对象中属性名相同的属性
  375. * @return { Object } 返回target对象
  376. * @example
  377. * ```javascript
  378. *
  379. * var target = { name: 'target', sex: 1 },
  380. * source = { name: 'source', age: 17 };
  381. *
  382. * UE.utils.extend( target, source, true );
  383. *
  384. * //output: { name: 'target', sex: 1, age: 17 }
  385. * console.log( target );
  386. *
  387. * ```
  388. */
  389. extend: function(t, s, b) {
  390. if (s) {
  391. for (var k in s) {
  392. if (!b || !t.hasOwnProperty(k)) {
  393. t[k] = s[k];
  394. }
  395. }
  396. }
  397. return t;
  398. },
  399. /**
  400. * 将给定的多个对象的属性复制到目标对象target上
  401. * @method extend2
  402. * @remind 该方法将强制把源对象上的属性复制到target对象上
  403. * @remind 该方法支持两个及以上的参数, 从第二个参数开始, 其属性都会被复制到第一个参数上。 如果遇到同名的属性,
  404. * 将会覆盖掉之前的值。
  405. * @param { Object } target 目标对象, 新的属性将附加到该对象上
  406. * @param { Object... } source 源对象, 支持多个对象, 该对象的属性会被附加到target对象上
  407. * @return { Object } 返回target对象
  408. * @example
  409. * ```javascript
  410. *
  411. * var target = {},
  412. * source1 = { name: 'source', age: 17 },
  413. * source2 = { title: 'dev' };
  414. *
  415. * UE.utils.extend2( target, source1, source2 );
  416. *
  417. * //output: { name: 'source', age: 17, title: 'dev' }
  418. * console.log( target );
  419. *
  420. * ```
  421. */
  422. extend2: function(t) {
  423. var a = arguments;
  424. for (var i = 1; i < a.length; i++) {
  425. var x = a[i];
  426. for (var k in x) {
  427. if (!t.hasOwnProperty(k)) {
  428. t[k] = x[k];
  429. }
  430. }
  431. }
  432. return t;
  433. },
  434. /**
  435. * 模拟继承机制, 使得subClass继承自superClass
  436. * @method inherits
  437. * @param { Object } subClass 子类对象
  438. * @param { Object } superClass 超类对象
  439. * @warning 该方法只能让subClass继承超类的原型, subClass对象自身的属性和方法不会被继承
  440. * @return { Object } 继承superClass后的子类对象
  441. * @example
  442. * ```javascript
  443. * function SuperClass(){
  444. * this.name = "小李";
  445. * }
  446. *
  447. * SuperClass.prototype = {
  448. * hello:function(str){
  449. * console.log(this.name + str);
  450. * }
  451. * }
  452. *
  453. * function SubClass(){
  454. * this.name = "小张";
  455. * }
  456. *
  457. * UE.utils.inherits(SubClass,SuperClass);
  458. *
  459. * var sub = new SubClass();
  460. * //output: '小张早上好!
  461. * sub.hello("早上好!");
  462. * ```
  463. */
  464. inherits: function(subClass, superClass) {
  465. var oldP = subClass.prototype,
  466. newP = utils.makeInstance(superClass.prototype);
  467. utils.extend(newP, oldP, true);
  468. subClass.prototype = newP;
  469. return (newP.constructor = subClass);
  470. },
  471. /**
  472. * 用指定的context对象作为函数fn的上下文
  473. * @method bind
  474. * @param { Function } fn 需要绑定上下文的函数对象
  475. * @param { Object } content 函数fn新的上下文对象
  476. * @return { Function } 一个新的函数, 该函数作为原始函数fn的代理, 将完成fn的上下文调换工作。
  477. * @example
  478. * ```javascript
  479. *
  480. * var name = 'window',
  481. * newTest = null;
  482. *
  483. * function test () {
  484. * console.log( this.name );
  485. * }
  486. *
  487. * newTest = UE.utils.bind( test, { name: 'object' } );
  488. *
  489. * //output: object
  490. * newTest();
  491. *
  492. * //output: window
  493. * test();
  494. *
  495. * ```
  496. */
  497. bind: function(fn, context) {
  498. return function() {
  499. return fn.apply(context, arguments);
  500. };
  501. },
  502. /**
  503. * 创建延迟指定时间后执行的函数fn
  504. * @method defer
  505. * @param { Function } fn 需要延迟执行的函数对象
  506. * @param { int } delay 延迟的时间, 单位是毫秒
  507. * @warning 该方法的时间控制是不精确的,仅仅只能保证函数的执行是在给定的时间之后,
  508. * 而不能保证刚好到达延迟时间时执行。
  509. * @return { Function } 目标函数fn的代理函数, 只有执行该函数才能起到延时效果
  510. * @example
  511. * ```javascript
  512. * var start = 0;
  513. *
  514. * function test(){
  515. * console.log( new Date() - start );
  516. * }
  517. *
  518. * var testDefer = UE.utils.defer( test, 1000 );
  519. * //
  520. * start = new Date();
  521. * //output: (大约在1000毫秒之后输出) 1000
  522. * testDefer();
  523. * ```
  524. */
  525. /**
  526. * 创建延迟指定时间后执行的函数fn, 如果在延迟时间内再次执行该方法, 将会根据指定的exclusion的值,
  527. * 决定是否取消前一次函数的执行, 如果exclusion的值为true, 则取消执行,反之,将继续执行前一个方法。
  528. * @method defer
  529. * @param { Function } fn 需要延迟执行的函数对象
  530. * @param { int } delay 延迟的时间, 单位是毫秒
  531. * @param { Boolean } exclusion 如果在延迟时间内再次执行该函数,该值将决定是否取消执行前一次函数的执行,
  532. * 值为true表示取消执行, 反之则将在执行前一次函数之后才执行本次函数调用。
  533. * @warning 该方法的时间控制是不精确的,仅仅只能保证函数的执行是在给定的时间之后,
  534. * 而不能保证刚好到达延迟时间时执行。
  535. * @return { Function } 目标函数fn的代理函数, 只有执行该函数才能起到延时效果
  536. * @example
  537. * ```javascript
  538. *
  539. * function test(){
  540. * console.log(1);
  541. * }
  542. *
  543. * var testDefer = UE.utils.defer( test, 1000, true );
  544. *
  545. * //output: (两次调用仅有一次输出) 1
  546. * testDefer();
  547. * testDefer();
  548. * ```
  549. */
  550. defer: function(fn, delay, exclusion) {
  551. var timerID;
  552. return function() {
  553. if (exclusion) {
  554. clearTimeout(timerID);
  555. }
  556. timerID = setTimeout(fn, delay);
  557. };
  558. },
  559. /**
  560. * 获取元素item在数组array中首次出现的位置, 如果未找到item, 则返回-1
  561. * @method indexOf
  562. * @remind 该方法的匹配过程使用的是恒等“===”
  563. * @param { Array } array 需要查找的数组对象
  564. * @param { * } item 需要在目标数组中查找的值
  565. * @return { int } 返回item在目标数组array中首次出现的位置, 如果在数组中未找到item, 则返回-1
  566. * @example
  567. * ```javascript
  568. * var item = 1,
  569. * arr = [ 3, 4, 6, 8, 1, 1, 2 ];
  570. *
  571. * //output: 4
  572. * console.log( UE.utils.indexOf( arr, item ) );
  573. * ```
  574. */
  575. /**
  576. * 获取元素item数组array中首次出现的位置, 如果未找到item, 则返回-1。通过start的值可以指定搜索的起始位置。
  577. * @method indexOf
  578. * @remind 该方法的匹配过程使用的是恒等“===”
  579. * @param { Array } array 需要查找的数组对象
  580. * @param { * } item 需要在目标数组中查找的值
  581. * @param { int } start 搜索的起始位置
  582. * @return { int } 返回item在目标数组array中的start位置之后首次出现的位置, 如果在数组中未找到item, 则返回-1
  583. * @example
  584. * ```javascript
  585. * var item = 1,
  586. * arr = [ 3, 4, 6, 8, 1, 2, 8, 3, 2, 1, 1, 4 ];
  587. *
  588. * //output: 9
  589. * console.log( UE.utils.indexOf( arr, item, 5 ) );
  590. * ```
  591. */
  592. indexOf: function(array, item, start) {
  593. var index = -1;
  594. start = this.isNumber(start) ? start : 0;
  595. this.each(array, function(v, i) {
  596. if (i >= start && v === item) {
  597. index = i;
  598. return false;
  599. }
  600. });
  601. return index;
  602. },
  603. /**
  604. * 移除数组array中所有的元素item
  605. * @method removeItem
  606. * @param { Array } array 要移除元素的目标数组
  607. * @param { * } item 将要被移除的元素
  608. * @remind 该方法的匹配过程使用的是恒等“===”
  609. * @example
  610. * ```javascript
  611. * var arr = [ 4, 5, 7, 1, 3, 4, 6 ];
  612. *
  613. * UE.utils.removeItem( arr, 4 );
  614. * //output: [ 5, 7, 1, 3, 6 ]
  615. * console.log( arr );
  616. *
  617. * ```
  618. */
  619. removeItem: function(array, item) {
  620. for (var i = 0, l = array.length; i < l; i++) {
  621. if (array[i] === item) {
  622. array.splice(i, 1);
  623. i--;
  624. }
  625. }
  626. },
  627. /**
  628. * 删除字符串str的首尾空格
  629. * @method trim
  630. * @param { String } str 需要删除首尾空格的字符串
  631. * @return { String } 删除了首尾的空格后的字符串
  632. * @example
  633. * ```javascript
  634. *
  635. * var str = " UEdtior ";
  636. *
  637. * //output: 9
  638. * console.log( str.length );
  639. *
  640. * //output: 7
  641. * console.log( UE.utils.trim( " UEdtior " ).length );
  642. *
  643. * //output: 9
  644. * console.log( str.length );
  645. *
  646. * ```
  647. */
  648. trim: function(str) {
  649. return str.replace(/(^[ \t\n\r]+)|([ \t\n\r]+$)/g, "");
  650. },
  651. /**
  652. * 将字符串str以','分隔成数组后,将该数组转换成哈希对象, 其生成的hash对象的key为数组中的元素, value为1
  653. * @method listToMap
  654. * @warning 该方法在生成的hash对象中,会为每一个key同时生成一个另一个全大写的key。
  655. * @param { String } str 该字符串将被以','分割为数组, 然后进行转化
  656. * @return { Object } 转化之后的hash对象
  657. * @example
  658. * ```javascript
  659. *
  660. * //output: Object {UEdtior: 1, UEDTIOR: 1, Hello: 1, HELLO: 1}
  661. * console.log( UE.utils.listToMap( 'UEdtior,Hello' ) );
  662. *
  663. * ```
  664. */
  665. /**
  666. * 将字符串数组转换成哈希对象, 其生成的hash对象的key为数组中的元素, value为1
  667. * @method listToMap
  668. * @warning 该方法在生成的hash对象中,会为每一个key同时生成一个另一个全大写的key。
  669. * @param { Array } arr 字符串数组
  670. * @return { Object } 转化之后的hash对象
  671. * @example
  672. * ```javascript
  673. *
  674. * //output: Object {UEdtior: 1, UEDTIOR: 1, Hello: 1, HELLO: 1}
  675. * console.log( UE.utils.listToMap( [ 'UEdtior', 'Hello' ] ) );
  676. *
  677. * ```
  678. */
  679. listToMap: function(list) {
  680. if (!list) return {};
  681. list = utils.isArray(list) ? list : list.split(",");
  682. for (var i = 0, ci, obj = {}; (ci = list[i++]); ) {
  683. obj[ci.toUpperCase()] = obj[ci] = 1;
  684. }
  685. return obj;
  686. },
  687. /**
  688. * 将str中的html符号转义,将转义“',&,<,",>,”,“”七个字符
  689. * @method unhtml
  690. * @param { String } str 需要转义的字符串
  691. * @return { String } 转义后的字符串
  692. * @example
  693. * ```javascript
  694. * var html = '<body>&</body>';
  695. *
  696. * //output: &lt;body&gt;&amp;&lt;/body&gt;
  697. * console.log( UE.utils.unhtml( html ) );
  698. *
  699. * ```
  700. */
  701. unhtml: function(str, reg) {
  702. return str
  703. ? str.replace(
  704. reg || /[&<">'](?:(amp|lt|ldquo|rdquo|quot|gt|#39|nbsp|#\d+);)?/g,
  705. function(a, b) {
  706. if (b) {
  707. return a;
  708. } else {
  709. return {
  710. "<": "&lt;",
  711. "&": "&amp;",
  712. '"': "&quot;",
  713. "“": "&ldquo;",
  714. "”": "&rdquo;",
  715. ">": "&gt;",
  716. "'": "&#39;"
  717. }[a];
  718. }
  719. }
  720. )
  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
  741. ? str.replace(/&((g|l|quo|ldquo|rdquo)t|amp|#39|nbsp);/g, function(m) {
  742. return {
  743. "&lt;": "<",
  744. "&amp;": "&",
  745. "&quot;": '"',
  746. "&ldquo;": "“",
  747. "&rdquo;": "”",
  748. "&gt;": ">",
  749. "&#39;": "'",
  750. "&nbsp;": " "
  751. }[m];
  752. })
  753. : "";
  754. },
  755. /**
  756. * 将css样式转换为驼峰的形式
  757. * @method cssStyleToDomStyle
  758. * @param { String } cssName 需要转换的css样式名
  759. * @return { String } 转换成驼峰形式后的css样式名
  760. * @example
  761. * ```javascript
  762. *
  763. * var str = 'border-top';
  764. *
  765. * //output: borderTop
  766. * console.log( UE.utils.cssStyleToDomStyle( str ) );
  767. *
  768. * ```
  769. */
  770. cssStyleToDomStyle: (function() {
  771. var test = document.createElement("div").style,
  772. cache = {
  773. float: test.cssFloat != undefined
  774. ? "cssFloat"
  775. : test.styleFloat != undefined ? "styleFloat" : "float"
  776. };
  777. return function(cssName) {
  778. return (
  779. cache[cssName] ||
  780. (cache[cssName] = cssName.toLowerCase().replace(/-./g, function(match) {
  781. return match.charAt(1).toUpperCase();
  782. }))
  783. );
  784. };
  785. })(),
  786. /**
  787. * 动态加载文件到doc中
  788. * @method loadFile
  789. * @param { DomDocument } document 需要加载资源文件的文档对象
  790. * @param { Object } options 加载资源文件的属性集合, 取值请参考代码示例
  791. * @example
  792. * ```javascript
  793. *
  794. * UE.utils.loadFile( document, {
  795. * src:"test.js",
  796. * tag:"script",
  797. * type:"text/javascript",
  798. * defer:"defer"
  799. * } );
  800. *
  801. * ```
  802. */
  803. /**
  804. * 动态加载文件到doc中,加载成功后执行的回调函数fn
  805. * @method loadFile
  806. * @param { DomDocument } document 需要加载资源文件的文档对象
  807. * @param { Object } options 加载资源文件的属性集合, 该集合支持的值是script标签和style标签支持的所有属性。
  808. * @param { Function } fn 资源文件加载成功之后执行的回调
  809. * @warning 对于在同一个文档中多次加载同一URL的文件, 该方法会在第一次加载之后缓存该请求,
  810. * 在此之后的所有同一URL的请求, 将会直接触发回调。
  811. * @example
  812. * ```javascript
  813. *
  814. * UE.utils.loadFile( document, {
  815. * src:"test.js",
  816. * tag:"script",
  817. * type:"text/javascript",
  818. * defer:"defer"
  819. * }, function () {
  820. * console.log('加载成功');
  821. * } );
  822. *
  823. * ```
  824. */
  825. loadFile: (function() {
  826. var tmpList = [];
  827. function getItem(doc, obj) {
  828. try {
  829. for (var i = 0, ci; (ci = tmpList[i++]); ) {
  830. if (ci.doc === doc && ci.url == (obj.src || obj.href)) {
  831. return ci;
  832. }
  833. }
  834. } catch (e) {
  835. return null;
  836. }
  837. }
  838. return function(doc, obj, fn) {
  839. var item = getItem(doc, obj);
  840. if (item) {
  841. if (item.ready) {
  842. fn && fn();
  843. } else {
  844. item.funs.push(fn);
  845. }
  846. return;
  847. }
  848. tmpList.push({
  849. doc: doc,
  850. url: obj.src || obj.href,
  851. funs: [fn]
  852. });
  853. if (!doc.body) {
  854. var html = [];
  855. for (var p in obj) {
  856. if (p == "tag") continue;
  857. html.push(p + '="' + obj[p] + '"');
  858. }
  859. doc.write(
  860. "<" + obj.tag + " " + html.join(" ") + " ></" + obj.tag + ">"
  861. );
  862. return;
  863. }
  864. if (obj.id && doc.getElementById(obj.id)) {
  865. return;
  866. }
  867. var element = doc.createElement(obj.tag);
  868. delete obj.tag;
  869. for (var p in obj) {
  870. element.setAttribute(p, obj[p]);
  871. }
  872. element.onload = element.onreadystatechange = function() {
  873. if (!this.readyState || /loaded|complete/.test(this.readyState)) {
  874. item = getItem(doc, obj);
  875. if (item.funs.length > 0) {
  876. item.ready = 1;
  877. for (var fi; (fi = item.funs.pop()); ) {
  878. fi();
  879. }
  880. }
  881. element.onload = element.onreadystatechange = null;
  882. }
  883. };
  884. element.onerror = function() {
  885. throw Error(
  886. "The load " +
  887. (obj.href || obj.src) +
  888. " fails,check the url settings of file ueditor.config.js "
  889. );
  890. };
  891. doc.getElementsByTagName("head")[0].appendChild(element);
  892. };
  893. })(),
  894. /**
  895. * 判断obj对象是否为空
  896. * @method isEmptyObject
  897. * @param { * } obj 需要判断的对象
  898. * @remind 如果判断的对象是NULL, 将直接返回true, 如果是数组且为空, 返回true, 如果是字符串, 且字符串为空,
  899. * 返回true, 如果是普通对象, 且该对象没有任何实例属性, 返回true
  900. * @return { Boolean } 对象是否为空
  901. * @example
  902. * ```javascript
  903. *
  904. * //output: true
  905. * console.log( UE.utils.isEmptyObject( {} ) );
  906. *
  907. * //output: true
  908. * console.log( UE.utils.isEmptyObject( [] ) );
  909. *
  910. * //output: true
  911. * console.log( UE.utils.isEmptyObject( "" ) );
  912. *
  913. * //output: false
  914. * console.log( UE.utils.isEmptyObject( { key: 1 } ) );
  915. *
  916. * //output: false
  917. * console.log( UE.utils.isEmptyObject( [1] ) );
  918. *
  919. * //output: false
  920. * console.log( UE.utils.isEmptyObject( "1" ) );
  921. *
  922. * ```
  923. */
  924. isEmptyObject: function(obj) {
  925. if (obj == null) return true;
  926. if (this.isArray(obj) || this.isString(obj)) return obj.length === 0;
  927. for (var key in obj) if (obj.hasOwnProperty(key)) return false;
  928. return true;
  929. },
  930. /**
  931. * 把rgb格式的颜色值转换成16进制格式
  932. * @method fixColor
  933. * @param { String } rgb格式的颜色值
  934. * @param { String }
  935. * @example
  936. * rgb(255,255,255) => "#ffffff"
  937. */
  938. fixColor: function(name, value) {
  939. if (/color/i.test(name) && /rgba?/.test(value)) {
  940. var array = value.split(",");
  941. if (array.length > 3) return "";
  942. value = "#";
  943. for (var i = 0, color; (color = array[i++]); ) {
  944. color = parseInt(color.replace(/[^\d]/gi, ""), 10).toString(16);
  945. value += color.length == 1 ? "0" + color : color;
  946. }
  947. value = value.toUpperCase();
  948. }
  949. return value;
  950. },
  951. /**
  952. * 只针对border,padding,margin做了处理,因为性能问题
  953. * @public
  954. * @function
  955. * @param {String} val style字符串
  956. */
  957. optCss: function(val) {
  958. var padding, margin, border;
  959. val = val.replace(/(padding|margin|border)\-([^:]+):([^;]+);?/gi, function(
  960. str,
  961. key,
  962. name,
  963. val
  964. ) {
  965. if (val.split(" ").length == 1) {
  966. switch (key) {
  967. case "padding":
  968. !padding && (padding = {});
  969. padding[name] = val;
  970. return "";
  971. case "margin":
  972. !margin && (margin = {});
  973. margin[name] = val;
  974. return "";
  975. case "border":
  976. return val == "initial" ? "" : str;
  977. }
  978. }
  979. return str;
  980. });
  981. function opt(obj, name) {
  982. if (!obj) {
  983. return "";
  984. }
  985. var t = obj.top,
  986. b = obj.bottom,
  987. l = obj.left,
  988. r = obj.right,
  989. val = "";
  990. if (!t || !l || !b || !r) {
  991. for (var p in obj) {
  992. val += ";" + name + "-" + p + ":" + obj[p] + ";";
  993. }
  994. } else {
  995. val +=
  996. ";" +
  997. name +
  998. ":" +
  999. (t == b && b == l && l == r
  1000. ? t
  1001. : t == b && l == r
  1002. ? t + " " + l
  1003. : l == r
  1004. ? t + " " + l + " " + b
  1005. : t + " " + r + " " + b + " " + l) +
  1006. ";";
  1007. }
  1008. return val;
  1009. }
  1010. val += opt(padding, "padding") + opt(margin, "margin");
  1011. return val
  1012. .replace(/^[ \n\r\t;]*|[ \n\r\t]*$/, "")
  1013. .replace(/;([ \n\r\t]+)|\1;/g, ";")
  1014. .replace(/(&((l|g)t|quot|#39))?;{2,}/g, function(a, b) {
  1015. return b ? b + ";;" : ";";
  1016. });
  1017. },
  1018. /**
  1019. * 克隆对象
  1020. * @method clone
  1021. * @param { Object } source 源对象
  1022. * @return { Object } source的一个副本
  1023. */
  1024. /**
  1025. * 深度克隆对象,将source的属性克隆到target对象, 会覆盖target重名的属性。
  1026. * @method clone
  1027. * @param { Object } source 源对象
  1028. * @param { Object } target 目标对象
  1029. * @return { Object } 附加了source对象所有属性的target对象
  1030. */
  1031. clone: function(source, target) {
  1032. var tmp;
  1033. target = target || {};
  1034. for (var i in source) {
  1035. if (source.hasOwnProperty(i)) {
  1036. tmp = source[i];
  1037. if (typeof tmp == "object") {
  1038. target[i] = utils.isArray(tmp) ? [] : {};
  1039. utils.clone(source[i], target[i]);
  1040. } else {
  1041. target[i] = tmp;
  1042. }
  1043. }
  1044. }
  1045. return target;
  1046. },
  1047. /**
  1048. * 把cm/pt为单位的值转换为px为单位的值
  1049. * @method transUnitToPx
  1050. * @param { String } 待转换的带单位的字符串
  1051. * @return { String } 转换为px为计量单位的值的字符串
  1052. * @example
  1053. * ```javascript
  1054. *
  1055. * //output: 500px
  1056. * console.log( UE.utils.transUnitToPx( '20cm' ) );
  1057. *
  1058. * //output: 27px
  1059. * console.log( UE.utils.transUnitToPx( '20pt' ) );
  1060. *
  1061. * ```
  1062. */
  1063. transUnitToPx: function(val) {
  1064. if (!/(pt|cm)/.test(val)) {
  1065. return val;
  1066. }
  1067. var unit;
  1068. val.replace(/([\d.]+)(\w+)/, function(str, v, u) {
  1069. val = v;
  1070. unit = u;
  1071. });
  1072. switch (unit) {
  1073. case "cm":
  1074. val = parseFloat(val) * 25;
  1075. break;
  1076. case "pt":
  1077. val = Math.round(parseFloat(val) * 96 / 72);
  1078. }
  1079. return val + (val ? "px" : "");
  1080. },
  1081. /**
  1082. * 在dom树ready之后执行给定的回调函数
  1083. * @method domReady
  1084. * @remind 如果在执行该方法的时候, dom树已经ready, 那么回调函数将立刻执行
  1085. * @param { Function } fn dom树ready之后的回调函数
  1086. * @example
  1087. * ```javascript
  1088. *
  1089. * UE.utils.domReady( function () {
  1090. *
  1091. * console.log('123');
  1092. *
  1093. * } );
  1094. *
  1095. * ```
  1096. */
  1097. domReady: (function() {
  1098. var fnArr = [];
  1099. function doReady(doc) {
  1100. //确保onready只执行一次
  1101. doc.isReady = true;
  1102. for (var ci; (ci = fnArr.pop()); ci()) {}
  1103. }
  1104. return function(onready, win) {
  1105. win = win || window;
  1106. var doc = win.document;
  1107. onready && fnArr.push(onready);
  1108. if (doc.readyState === "complete") {
  1109. doReady(doc);
  1110. } else {
  1111. doc.isReady && doReady(doc);
  1112. if (browser.ie && browser.version != 11) {
  1113. (function() {
  1114. if (doc.isReady) return;
  1115. try {
  1116. doc.documentElement.doScroll("left");
  1117. } catch (error) {
  1118. setTimeout(arguments.callee, 0);
  1119. return;
  1120. }
  1121. doReady(doc);
  1122. })();
  1123. win.attachEvent("onload", function() {
  1124. doReady(doc);
  1125. });
  1126. } else {
  1127. doc.addEventListener(
  1128. "DOMContentLoaded",
  1129. function() {
  1130. doc.removeEventListener(
  1131. "DOMContentLoaded",
  1132. arguments.callee,
  1133. false
  1134. );
  1135. doReady(doc);
  1136. },
  1137. false
  1138. );
  1139. win.addEventListener(
  1140. "load",
  1141. function() {
  1142. doReady(doc);
  1143. },
  1144. false
  1145. );
  1146. }
  1147. }
  1148. };
  1149. })(),
  1150. /**
  1151. * 动态添加css样式
  1152. * @method cssRule
  1153. * @param { String } 节点名称
  1154. * @grammar UE.utils.cssRule('添加的样式的节点名称',['样式','放到哪个document上'])
  1155. * @grammar UE.utils.cssRule('body','body{background:#ccc}') => null //给body添加背景颜色
  1156. * @grammar UE.utils.cssRule('body') =>样式的字符串 //取得key值为body的样式的内容,如果没有找到key值先关的样式将返回空,例如刚才那个背景颜色,将返回 body{background:#ccc}
  1157. * @grammar UE.utils.cssRule('body',document) => 返回指定key的样式,并且指定是哪个document
  1158. * @grammar UE.utils.cssRule('body','') =>null //清空给定的key值的背景颜色
  1159. */
  1160. cssRule: browser.ie && browser.version != 11
  1161. ? function(key, style, doc) {
  1162. var indexList, index;
  1163. if (
  1164. style === undefined ||
  1165. (style && style.nodeType && style.nodeType == 9)
  1166. ) {
  1167. //获取样式
  1168. doc = style && style.nodeType && style.nodeType == 9
  1169. ? style
  1170. : doc || document;
  1171. indexList = doc.indexList || (doc.indexList = {});
  1172. index = indexList[key];
  1173. if (index !== undefined) {
  1174. return doc.styleSheets[index].cssText;
  1175. }
  1176. return undefined;
  1177. }
  1178. doc = doc || document;
  1179. indexList = doc.indexList || (doc.indexList = {});
  1180. index = indexList[key];
  1181. //清除样式
  1182. if (style === "") {
  1183. if (index !== undefined) {
  1184. doc.styleSheets[index].cssText = "";
  1185. delete indexList[key];
  1186. return true;
  1187. }
  1188. return false;
  1189. }
  1190. //添加样式
  1191. if (index !== undefined) {
  1192. sheetStyle = doc.styleSheets[index];
  1193. } else {
  1194. sheetStyle = doc.createStyleSheet(
  1195. "",
  1196. (index = doc.styleSheets.length)
  1197. );
  1198. indexList[key] = index;
  1199. }
  1200. sheetStyle.cssText = style;
  1201. }
  1202. : function(key, style, doc) {
  1203. var head, node;
  1204. if (
  1205. style === undefined ||
  1206. (style && style.nodeType && style.nodeType == 9)
  1207. ) {
  1208. //获取样式
  1209. doc = style && style.nodeType && style.nodeType == 9
  1210. ? style
  1211. : doc || document;
  1212. node = doc.getElementById(key);
  1213. return node ? node.innerHTML : undefined;
  1214. }
  1215. doc = doc || document;
  1216. node = doc.getElementById(key);
  1217. //清除样式
  1218. if (style === "") {
  1219. if (node) {
  1220. node.parentNode.removeChild(node);
  1221. return true;
  1222. }
  1223. return false;
  1224. }
  1225. //添加样式
  1226. if (node) {
  1227. node.innerHTML = style;
  1228. } else {
  1229. node = doc.createElement("style");
  1230. node.id = key;
  1231. node.innerHTML = style;
  1232. doc.getElementsByTagName("head")[0].appendChild(node);
  1233. }
  1234. },
  1235. sort: function(array, compareFn) {
  1236. compareFn =
  1237. compareFn ||
  1238. function(item1, item2) {
  1239. return item1.localeCompare(item2);
  1240. };
  1241. for (var i = 0, len = array.length; i < len; i++) {
  1242. for (var j = i, length = array.length; j < length; j++) {
  1243. if (compareFn(array[i], array[j]) > 0) {
  1244. var t = array[i];
  1245. array[i] = array[j];
  1246. array[j] = t;
  1247. }
  1248. }
  1249. }
  1250. return array;
  1251. },
  1252. serializeParam: function(json) {
  1253. var strArr = [];
  1254. for (var i in json) {
  1255. //忽略默认的几个参数
  1256. if (i == "method" || i == "timeout" || i == "async") continue;
  1257. //传递过来的对象和函数不在提交之列
  1258. if (
  1259. !(
  1260. (typeof json[i]).toLowerCase() == "function" ||
  1261. (typeof json[i]).toLowerCase() == "object"
  1262. )
  1263. ) {
  1264. strArr.push(encodeURIComponent(i) + "=" + encodeURIComponent(json[i]));
  1265. } else if (utils.isArray(json[i])) {
  1266. //支持传数组内容
  1267. for (var j = 0; j < json[i].length; j++) {
  1268. strArr.push(
  1269. encodeURIComponent(i) + "[]=" + encodeURIComponent(json[i][j])
  1270. );
  1271. }
  1272. }
  1273. }
  1274. return strArr.join("&");
  1275. },
  1276. formatUrl: function(url) {
  1277. var u = url.replace(/&&/g, "&");
  1278. u = u.replace(/\?&/g, "?");
  1279. u = u.replace(/&$/g, "");
  1280. u = u.replace(/&#/g, "#");
  1281. u = u.replace(/&+/g, "&");
  1282. return u;
  1283. },
  1284. isCrossDomainUrl: function(url) {
  1285. var a = document.createElement("a");
  1286. a.href = url;
  1287. if (browser.ie) {
  1288. a.href = a.href;
  1289. }
  1290. return !(
  1291. a.protocol == location.protocol &&
  1292. a.hostname == location.hostname &&
  1293. (a.port == location.port ||
  1294. (a.port == "80" && location.port == "") ||
  1295. (a.port == "" && location.port == "80"))
  1296. );
  1297. },
  1298. clearEmptyAttrs: function(obj) {
  1299. for (var p in obj) {
  1300. if (obj[p] === "") {
  1301. delete obj[p];
  1302. }
  1303. }
  1304. return obj;
  1305. },
  1306. str2json: function(s) {
  1307. if (!utils.isString(s)) return null;
  1308. if (window.JSON) {
  1309. return JSON.parse(s);
  1310. } else {
  1311. return new Function("return " + utils.trim(s || ""))();
  1312. }
  1313. },
  1314. json2str: (function() {
  1315. if (window.JSON) {
  1316. return JSON.stringify;
  1317. } else {
  1318. var escapeMap = {
  1319. "\b": "\\b",
  1320. "\t": "\\t",
  1321. "\n": "\\n",
  1322. "\f": "\\f",
  1323. "\r": "\\r",
  1324. '"': '\\"',
  1325. "\\": "\\\\"
  1326. };
  1327. function encodeString(source) {
  1328. if (/["\\\x00-\x1f]/.test(source)) {
  1329. source = source.replace(/["\\\x00-\x1f]/g, function(match) {
  1330. var c = escapeMap[match];
  1331. if (c) {
  1332. return c;
  1333. }
  1334. c = match.charCodeAt();
  1335. return (
  1336. "\\u00" + Math.floor(c / 16).toString(16) + (c % 16).toString(16)
  1337. );
  1338. });
  1339. }
  1340. return '"' + source + '"';
  1341. }
  1342. function encodeArray(source) {
  1343. var result = ["["],
  1344. l = source.length,
  1345. preComma,
  1346. i,
  1347. item;
  1348. for (i = 0; i < l; i++) {
  1349. item = source[i];
  1350. switch (typeof item) {
  1351. case "undefined":
  1352. case "function":
  1353. case "unknown":
  1354. break;
  1355. default:
  1356. if (preComma) {
  1357. result.push(",");
  1358. }
  1359. result.push(utils.json2str(item));
  1360. preComma = 1;
  1361. }
  1362. }
  1363. result.push("]");
  1364. return result.join("");
  1365. }
  1366. function pad(source) {
  1367. return source < 10 ? "0" + source : source;
  1368. }
  1369. function encodeDate(source) {
  1370. return (
  1371. '"' +
  1372. source.getFullYear() +
  1373. "-" +
  1374. pad(source.getMonth() + 1) +
  1375. "-" +
  1376. pad(source.getDate()) +
  1377. "T" +
  1378. pad(source.getHours()) +
  1379. ":" +
  1380. pad(source.getMinutes()) +
  1381. ":" +
  1382. pad(source.getSeconds()) +
  1383. '"'
  1384. );
  1385. }
  1386. return function(value) {
  1387. switch (typeof value) {
  1388. case "undefined":
  1389. return "undefined";
  1390. case "number":
  1391. return isFinite(value) ? String(value) : "null";
  1392. case "string":
  1393. return encodeString(value);
  1394. case "boolean":
  1395. return String(value);
  1396. default:
  1397. if (value === null) {
  1398. return "null";
  1399. } else if (utils.isArray(value)) {
  1400. return encodeArray(value);
  1401. } else if (utils.isDate(value)) {
  1402. return encodeDate(value);
  1403. } else {
  1404. var result = ["{"],
  1405. encode = utils.json2str,
  1406. preComma,
  1407. item;
  1408. for (var key in value) {
  1409. if (Object.prototype.hasOwnProperty.call(value, key)) {
  1410. item = value[key];
  1411. switch (typeof item) {
  1412. case "undefined":
  1413. case "unknown":
  1414. case "function":
  1415. break;
  1416. default:
  1417. if (preComma) {
  1418. result.push(",");
  1419. }
  1420. preComma = 1;
  1421. result.push(encode(key) + ":" + encode(item));
  1422. }
  1423. }
  1424. }
  1425. result.push("}");
  1426. return result.join("");
  1427. }
  1428. }
  1429. };
  1430. }
  1431. })()
  1432. });
  1433. /**
  1434. * 判断给定的对象是否是字符串
  1435. * @method isString
  1436. * @param { * } object 需要判断的对象
  1437. * @return { Boolean } 给定的对象是否是字符串
  1438. */
  1439. /**
  1440. * 判断给定的对象是否是数组
  1441. * @method isArray
  1442. * @param { * } object 需要判断的对象
  1443. * @return { Boolean } 给定的对象是否是数组
  1444. */
  1445. /**
  1446. * 判断给定的对象是否是一个Function
  1447. * @method isFunction
  1448. * @param { * } object 需要判断的对象
  1449. * @return { Boolean } 给定的对象是否是Function
  1450. */
  1451. /**
  1452. * 判断给定的对象是否是Number
  1453. * @method isNumber
  1454. * @param { * } object 需要判断的对象
  1455. * @return { Boolean } 给定的对象是否是Number
  1456. */
  1457. /**
  1458. * 判断给定的对象是否是一个正则表达式
  1459. * @method isRegExp
  1460. * @param { * } object 需要判断的对象
  1461. * @return { Boolean } 给定的对象是否是正则表达式
  1462. */
  1463. /**
  1464. * 判断给定的对象是否是一个普通对象
  1465. * @method isObject
  1466. * @param { * } object 需要判断的对象
  1467. * @return { Boolean } 给定的对象是否是普通对象
  1468. */
  1469. utils.each(
  1470. ["String", "Function", "Array", "Number", "RegExp", "Object", "Date"],
  1471. function(v) {
  1472. UE.utils["is" + v] = function(obj) {
  1473. return Object.prototype.toString.apply(obj) == "[object " + v + "]";
  1474. };
  1475. }
  1476. );
  1477. // core/EventBase.js
  1478. /**
  1479. * UE采用的事件基类
  1480. * @file
  1481. * @module UE
  1482. * @class EventBase
  1483. * @since 1.2.6.1
  1484. */
  1485. /**
  1486. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  1487. * @unfile
  1488. * @module UE
  1489. */
  1490. /**
  1491. * UE采用的事件基类,继承此类的对应类将获取addListener,removeListener,fireEvent方法。
  1492. * 在UE中,Editor以及所有ui实例都继承了该类,故可以在对应的ui对象以及editor对象上使用上述方法。
  1493. * @unfile
  1494. * @module UE
  1495. * @class EventBase
  1496. */
  1497. /**
  1498. * 通过此构造器,子类可以继承EventBase获取事件监听的方法
  1499. * @constructor
  1500. * @example
  1501. * ```javascript
  1502. * UE.EventBase.call(editor);
  1503. * ```
  1504. */
  1505. var EventBase = (UE.EventBase = function() {});
  1506. EventBase.prototype = {
  1507. /**
  1508. * 注册事件监听器
  1509. * @method addListener
  1510. * @param { String } types 监听的事件名称,同时监听多个事件使用空格分隔
  1511. * @param { Function } fn 监听的事件被触发时,会执行该回调函数
  1512. * @waining 事件被触发时,监听的函数假如返回的值恒等于true,回调函数的队列中后面的函数将不执行
  1513. * @example
  1514. * ```javascript
  1515. * editor.addListener('selectionchange',function(){
  1516. * console.log("选区已经变化!");
  1517. * })
  1518. * editor.addListener('beforegetcontent aftergetcontent',function(type){
  1519. * if(type == 'beforegetcontent'){
  1520. * //do something
  1521. * }else{
  1522. * //do something
  1523. * }
  1524. * console.log(this.getContent) // this是注册的事件的编辑器实例
  1525. * })
  1526. * ```
  1527. * @see UE.EventBase:fireEvent(String)
  1528. */
  1529. addListener: function(types, listener) {
  1530. types = utils.trim(types).split(/\s+/);
  1531. for (var i = 0, ti; (ti = types[i++]); ) {
  1532. getListener(this, ti, true).push(listener);
  1533. }
  1534. },
  1535. on: function(types, listener) {
  1536. return this.addListener(types, listener);
  1537. },
  1538. off: function(types, listener) {
  1539. return this.removeListener(types, listener);
  1540. },
  1541. trigger: function() {
  1542. return this.fireEvent.apply(this, arguments);
  1543. },
  1544. /**
  1545. * 移除事件监听器
  1546. * @method removeListener
  1547. * @param { String } types 移除的事件名称,同时移除多个事件使用空格分隔
  1548. * @param { Function } fn 移除监听事件的函数引用
  1549. * @example
  1550. * ```javascript
  1551. * //changeCallback为方法体
  1552. * editor.removeListener("selectionchange",changeCallback);
  1553. * ```
  1554. */
  1555. removeListener: function(types, listener) {
  1556. types = utils.trim(types).split(/\s+/);
  1557. for (var i = 0, ti; (ti = types[i++]); ) {
  1558. utils.removeItem(getListener(this, ti) || [], listener);
  1559. }
  1560. },
  1561. /**
  1562. * 触发事件
  1563. * @method fireEvent
  1564. * @param { String } types 触发的事件名称,同时触发多个事件使用空格分隔
  1565. * @remind 该方法会触发addListener
  1566. * @return { * } 返回触发事件的队列中,最后执行的回调函数的返回值
  1567. * @example
  1568. * ```javascript
  1569. * editor.fireEvent("selectionchange");
  1570. * ```
  1571. */
  1572. /**
  1573. * 触发事件
  1574. * @method fireEvent
  1575. * @param { String } types 触发的事件名称,同时触发多个事件使用空格分隔
  1576. * @param { *... } options 可选参数,可以传入一个或多个参数,会传给事件触发的回调函数
  1577. * @return { * } 返回触发事件的队列中,最后执行的回调函数的返回值
  1578. * @example
  1579. * ```javascript
  1580. *
  1581. * editor.addListener( "selectionchange", function ( type, arg1, arg2 ) {
  1582. *
  1583. * console.log( arg1 + " " + arg2 );
  1584. *
  1585. * } );
  1586. *
  1587. * //触发selectionchange事件, 会执行上面的事件监听器
  1588. * //output: Hello World
  1589. * editor.fireEvent("selectionchange", "Hello", "World");
  1590. * ```
  1591. */
  1592. fireEvent: function() {
  1593. var types = arguments[0];
  1594. types = utils.trim(types).split(" ");
  1595. for (var i = 0, ti; (ti = types[i++]); ) {
  1596. var listeners = getListener(this, ti),
  1597. r,
  1598. t,
  1599. k;
  1600. if (listeners) {
  1601. k = listeners.length;
  1602. while (k--) {
  1603. if (!listeners[k]) continue;
  1604. t = listeners[k].apply(this, arguments);
  1605. if (t === true) {
  1606. return t;
  1607. }
  1608. if (t !== undefined) {
  1609. r = t;
  1610. }
  1611. }
  1612. }
  1613. if ((t = this["on" + ti.toLowerCase()])) {
  1614. r = t.apply(this, arguments);
  1615. }
  1616. }
  1617. return r;
  1618. }
  1619. };
  1620. /**
  1621. * 获得对象所拥有监听类型的所有监听器
  1622. * @unfile
  1623. * @module UE
  1624. * @since 1.2.6.1
  1625. * @method getListener
  1626. * @public
  1627. * @param { Object } obj 查询监听器的对象
  1628. * @param { String } type 事件类型
  1629. * @param { Boolean } force 为true且当前所有type类型的侦听器不存在时,创建一个空监听器数组
  1630. * @return { Array } 监听器数组
  1631. */
  1632. function getListener(obj, type, force) {
  1633. var allListeners;
  1634. type = type.toLowerCase();
  1635. return (
  1636. (allListeners =
  1637. obj.__allListeners || (force && (obj.__allListeners = {}))) &&
  1638. (allListeners[type] || (force && (allListeners[type] = [])))
  1639. );
  1640. }
  1641. // core/dtd.js
  1642. ///import editor.js
  1643. ///import core/dom/dom.js
  1644. ///import core/utils.js
  1645. /**
  1646. * dtd html语义化的体现类
  1647. * @constructor
  1648. * @namespace dtd
  1649. */
  1650. var dtd = (dom.dtd = (function() {
  1651. function _(s) {
  1652. for (var k in s) {
  1653. s[k.toUpperCase()] = s[k];
  1654. }
  1655. return s;
  1656. }
  1657. var X = utils.extend2;
  1658. var A = _({ isindex: 1, fieldset: 1 }),
  1659. B = _({ input: 1, button: 1, select: 1, textarea: 1, label: 1 }),
  1660. C = X(_({ a: 1 }), B),
  1661. D = X({ iframe: 1 }, C),
  1662. E = _({
  1663. hr: 1,
  1664. ul: 1,
  1665. menu: 1,
  1666. div: 1,
  1667. blockquote: 1,
  1668. noscript: 1,
  1669. table: 1,
  1670. center: 1,
  1671. address: 1,
  1672. dir: 1,
  1673. pre: 1,
  1674. h5: 1,
  1675. dl: 1,
  1676. h4: 1,
  1677. noframes: 1,
  1678. h6: 1,
  1679. ol: 1,
  1680. h1: 1,
  1681. h3: 1,
  1682. h2: 1
  1683. }),
  1684. F = _({ ins: 1, del: 1, script: 1, style: 1 }),
  1685. G = X(
  1686. _({
  1687. mark: 1,
  1688. b: 1,
  1689. acronym: 1,
  1690. bdo: 1,
  1691. var: 1,
  1692. "#": 1,
  1693. abbr: 1,
  1694. code: 1,
  1695. br: 1,
  1696. i: 1,
  1697. cite: 1,
  1698. kbd: 1,
  1699. u: 1,
  1700. strike: 1,
  1701. s: 1,
  1702. tt: 1,
  1703. strong: 1,
  1704. q: 1,
  1705. samp: 1,
  1706. em: 1,
  1707. dfn: 1,
  1708. span: 1
  1709. }),
  1710. F
  1711. ),
  1712. H = X(
  1713. _({
  1714. sub: 1,
  1715. img: 1,
  1716. embed: 1,
  1717. object: 1,
  1718. sup: 1,
  1719. basefont: 1,
  1720. map: 1,
  1721. applet: 1,
  1722. font: 1,
  1723. big: 1,
  1724. small: 1
  1725. }),
  1726. G
  1727. ),
  1728. I = X(_({ p: 1 }), H),
  1729. J = X(_({ iframe: 1 }), H, B),
  1730. K = _({
  1731. img: 1,
  1732. embed: 1,
  1733. noscript: 1,
  1734. br: 1,
  1735. kbd: 1,
  1736. center: 1,
  1737. button: 1,
  1738. basefont: 1,
  1739. h5: 1,
  1740. h4: 1,
  1741. samp: 1,
  1742. h6: 1,
  1743. ol: 1,
  1744. h1: 1,
  1745. h3: 1,
  1746. h2: 1,
  1747. form: 1,
  1748. font: 1,
  1749. "#": 1,
  1750. select: 1,
  1751. menu: 1,
  1752. ins: 1,
  1753. abbr: 1,
  1754. label: 1,
  1755. code: 1,
  1756. table: 1,
  1757. script: 1,
  1758. cite: 1,
  1759. input: 1,
  1760. iframe: 1,
  1761. strong: 1,
  1762. textarea: 1,
  1763. noframes: 1,
  1764. big: 1,
  1765. small: 1,
  1766. span: 1,
  1767. hr: 1,
  1768. sub: 1,
  1769. bdo: 1,
  1770. var: 1,
  1771. div: 1,
  1772. object: 1,
  1773. sup: 1,
  1774. strike: 1,
  1775. dir: 1,
  1776. map: 1,
  1777. dl: 1,
  1778. applet: 1,
  1779. del: 1,
  1780. isindex: 1,
  1781. fieldset: 1,
  1782. ul: 1,
  1783. b: 1,
  1784. acronym: 1,
  1785. a: 1,
  1786. blockquote: 1,
  1787. i: 1,
  1788. u: 1,
  1789. s: 1,
  1790. tt: 1,
  1791. address: 1,
  1792. q: 1,
  1793. pre: 1,
  1794. p: 1,
  1795. em: 1,
  1796. dfn: 1
  1797. }),
  1798. L = X(_({ a: 0 }), J), //a不能被切开,所以把他
  1799. M = _({ tr: 1 }),
  1800. N = _({ "#": 1 }),
  1801. O = X(_({ param: 1 }), K),
  1802. P = X(_({ form: 1 }), A, D, E, I),
  1803. Q = _({ li: 1, ol: 1, ul: 1 }),
  1804. R = _({ style: 1, script: 1 }),
  1805. S = _({ base: 1, link: 1, meta: 1, title: 1 }),
  1806. T = X(S, R),
  1807. U = _({ head: 1, body: 1 }),
  1808. V = _({ html: 1 });
  1809. var block = _({
  1810. address: 1,
  1811. blockquote: 1,
  1812. center: 1,
  1813. dir: 1,
  1814. div: 1,
  1815. dl: 1,
  1816. fieldset: 1,
  1817. form: 1,
  1818. h1: 1,
  1819. h2: 1,
  1820. h3: 1,
  1821. h4: 1,
  1822. h5: 1,
  1823. h6: 1,
  1824. hr: 1,
  1825. isindex: 1,
  1826. menu: 1,
  1827. noframes: 1,
  1828. ol: 1,
  1829. p: 1,
  1830. pre: 1,
  1831. table: 1,
  1832. ul: 1
  1833. }),
  1834. empty = _({
  1835. area: 1,
  1836. base: 1,
  1837. basefont: 1,
  1838. br: 1,
  1839. col: 1,
  1840. command: 1,
  1841. dialog: 1,
  1842. embed: 1,
  1843. hr: 1,
  1844. img: 1,
  1845. input: 1,
  1846. isindex: 1,
  1847. keygen: 1,
  1848. link: 1,
  1849. meta: 1,
  1850. param: 1,
  1851. source: 1,
  1852. track: 1,
  1853. wbr: 1
  1854. });
  1855. return _({
  1856. // $ 表示自定的属性
  1857. // body外的元素列表.
  1858. $nonBodyContent: X(V, U, S),
  1859. //块结构元素列表
  1860. $block: block,
  1861. //内联元素列表
  1862. $inline: L,
  1863. $inlineWithA: X(_({ a: 1 }), L),
  1864. $body: X(_({ script: 1, style: 1 }), block),
  1865. $cdata: _({ script: 1, style: 1 }),
  1866. //自闭和元素
  1867. $empty: empty,
  1868. //不是自闭合,但不能让range选中里边
  1869. $nonChild: _({ iframe: 1, textarea: 1 }),
  1870. //列表元素列表
  1871. $listItem: _({ dd: 1, dt: 1, li: 1 }),
  1872. //列表根元素列表
  1873. $list: _({ ul: 1, ol: 1, dl: 1 }),
  1874. //不能认为是空的元素
  1875. $isNotEmpty: _({
  1876. table: 1,
  1877. ul: 1,
  1878. ol: 1,
  1879. dl: 1,
  1880. iframe: 1,
  1881. area: 1,
  1882. base: 1,
  1883. col: 1,
  1884. hr: 1,
  1885. img: 1,
  1886. embed: 1,
  1887. input: 1,
  1888. textarea: 1,
  1889. link: 1,
  1890. meta: 1,
  1891. param: 1,
  1892. h1: 1,
  1893. h2: 1,
  1894. h3: 1,
  1895. h4: 1,
  1896. h5: 1,
  1897. h6: 1
  1898. }),
  1899. //如果没有子节点就可以删除的元素列表,像span,a
  1900. $removeEmpty: _({
  1901. a: 1,
  1902. abbr: 1,
  1903. acronym: 1,
  1904. address: 1,
  1905. b: 1,
  1906. bdo: 1,
  1907. big: 1,
  1908. cite: 1,
  1909. code: 1,
  1910. del: 1,
  1911. dfn: 1,
  1912. em: 1,
  1913. font: 1,
  1914. i: 1,
  1915. ins: 1,
  1916. label: 1,
  1917. kbd: 1,
  1918. q: 1,
  1919. s: 1,
  1920. samp: 1,
  1921. small: 1,
  1922. span: 1,
  1923. strike: 1,
  1924. strong: 1,
  1925. sub: 1,
  1926. sup: 1,
  1927. tt: 1,
  1928. u: 1,
  1929. var: 1
  1930. }),
  1931. $removeEmptyBlock: _({ p: 1, div: 1 }),
  1932. //在table元素里的元素列表
  1933. $tableContent: _({
  1934. caption: 1,
  1935. col: 1,
  1936. colgroup: 1,
  1937. tbody: 1,
  1938. td: 1,
  1939. tfoot: 1,
  1940. th: 1,
  1941. thead: 1,
  1942. tr: 1,
  1943. table: 1
  1944. }),
  1945. //不转换的标签
  1946. $notTransContent: _({ pre: 1, script: 1, style: 1, textarea: 1 }),
  1947. html: U,
  1948. head: T,
  1949. style: N,
  1950. script: N,
  1951. body: P,
  1952. base: {},
  1953. link: {},
  1954. meta: {},
  1955. title: N,
  1956. col: {},
  1957. tr: _({ td: 1, th: 1 }),
  1958. img: {},
  1959. embed: {},
  1960. colgroup: _({ thead: 1, col: 1, tbody: 1, tr: 1, tfoot: 1 }),
  1961. noscript: P,
  1962. td: P,
  1963. br: {},
  1964. th: P,
  1965. center: P,
  1966. kbd: L,
  1967. button: X(I, E),
  1968. basefont: {},
  1969. h5: L,
  1970. h4: L,
  1971. samp: L,
  1972. h6: L,
  1973. ol: Q,
  1974. h1: L,
  1975. h3: L,
  1976. option: N,
  1977. h2: L,
  1978. form: X(A, D, E, I),
  1979. select: _({ optgroup: 1, option: 1 }),
  1980. font: L,
  1981. ins: L,
  1982. menu: Q,
  1983. abbr: L,
  1984. label: L,
  1985. table: _({
  1986. thead: 1,
  1987. col: 1,
  1988. tbody: 1,
  1989. tr: 1,
  1990. colgroup: 1,
  1991. caption: 1,
  1992. tfoot: 1
  1993. }),
  1994. code: L,
  1995. tfoot: M,
  1996. cite: L,
  1997. li: P,
  1998. input: {},
  1999. iframe: P,
  2000. strong: L,
  2001. textarea: N,
  2002. noframes: P,
  2003. big: L,
  2004. small: L,
  2005. //trace:
  2006. span: _({
  2007. "#": 1,
  2008. br: 1,
  2009. b: 1,
  2010. strong: 1,
  2011. u: 1,
  2012. i: 1,
  2013. em: 1,
  2014. sub: 1,
  2015. sup: 1,
  2016. strike: 1,
  2017. span: 1
  2018. }),
  2019. hr: L,
  2020. dt: L,
  2021. sub: L,
  2022. optgroup: _({ option: 1 }),
  2023. param: {},
  2024. bdo: L,
  2025. var: L,
  2026. div: P,
  2027. object: O,
  2028. sup: L,
  2029. dd: P,
  2030. strike: L,
  2031. area: {},
  2032. dir: Q,
  2033. map: X(_({ area: 1, form: 1, p: 1 }), A, F, E),
  2034. applet: O,
  2035. dl: _({ dt: 1, dd: 1 }),
  2036. del: L,
  2037. isindex: {},
  2038. fieldset: X(_({ legend: 1 }), K),
  2039. thead: M,
  2040. ul: Q,
  2041. acronym: L,
  2042. b: L,
  2043. a: X(_({ a: 1 }), J),
  2044. blockquote: X(_({ td: 1, tr: 1, tbody: 1, li: 1 }), P),
  2045. caption: L,
  2046. i: L,
  2047. u: L,
  2048. tbody: M,
  2049. s: L,
  2050. address: X(D, I),
  2051. tt: L,
  2052. legend: L,
  2053. q: L,
  2054. pre: X(G, C),
  2055. p: X(_({ a: 1 }), L),
  2056. em: L,
  2057. dfn: L,
  2058. mark: L
  2059. });
  2060. })());
  2061. // core/domUtils.js
  2062. /**
  2063. * Dom操作工具包
  2064. * @file
  2065. * @module UE.dom.domUtils
  2066. * @since 1.2.6.1
  2067. */
  2068. /**
  2069. * Dom操作工具包
  2070. * @unfile
  2071. * @module UE.dom.domUtils
  2072. */
  2073. function getDomNode(node, start, ltr, startFromChild, fn, guard) {
  2074. var tmpNode = startFromChild && node[start],
  2075. parent;
  2076. !tmpNode && (tmpNode = node[ltr]);
  2077. while (!tmpNode && (parent = (parent || node).parentNode)) {
  2078. if (parent.tagName == "BODY" || (guard && !guard(parent))) {
  2079. return null;
  2080. }
  2081. tmpNode = parent[ltr];
  2082. }
  2083. if (tmpNode && fn && !fn(tmpNode)) {
  2084. return getDomNode(tmpNode, start, ltr, false, fn);
  2085. }
  2086. return tmpNode;
  2087. }
  2088. var attrFix = ie && browser.version < 9
  2089. ? {
  2090. tabindex: "tabIndex",
  2091. readonly: "readOnly",
  2092. for: "htmlFor",
  2093. class: "className",
  2094. maxlength: "maxLength",
  2095. cellspacing: "cellSpacing",
  2096. cellpadding: "cellPadding",
  2097. rowspan: "rowSpan",
  2098. colspan: "colSpan",
  2099. usemap: "useMap",
  2100. frameborder: "frameBorder"
  2101. }
  2102. : {
  2103. tabindex: "tabIndex",
  2104. readonly: "readOnly"
  2105. },
  2106. styleBlock = utils.listToMap([
  2107. "-webkit-box",
  2108. "-moz-box",
  2109. "block",
  2110. "list-item",
  2111. "table",
  2112. "table-row-group",
  2113. "table-header-group",
  2114. "table-footer-group",
  2115. "table-row",
  2116. "table-column-group",
  2117. "table-column",
  2118. "table-cell",
  2119. "table-caption"
  2120. ]);
  2121. var domUtils = (dom.domUtils = {
  2122. //节点常量
  2123. NODE_ELEMENT: 1,
  2124. NODE_DOCUMENT: 9,
  2125. NODE_TEXT: 3,
  2126. NODE_COMMENT: 8,
  2127. NODE_DOCUMENT_FRAGMENT: 11,
  2128. //位置关系
  2129. POSITION_IDENTICAL: 0,
  2130. POSITION_DISCONNECTED: 1,
  2131. POSITION_FOLLOWING: 2,
  2132. POSITION_PRECEDING: 4,
  2133. POSITION_IS_CONTAINED: 8,
  2134. POSITION_CONTAINS: 16,
  2135. //ie6使用其他的会有一段空白出现
  2136. fillChar: ie && browser.version == "6" ? "\ufeff" : "\u200B",
  2137. //-------------------------Node部分--------------------------------
  2138. keys: {
  2139. /*Backspace*/ 8: 1,
  2140. /*Delete*/ 46: 1,
  2141. /*Shift*/ 16: 1,
  2142. /*Ctrl*/ 17: 1,
  2143. /*Alt*/ 18: 1,
  2144. 37: 1,
  2145. 38: 1,
  2146. 39: 1,
  2147. 40: 1,
  2148. 13: 1 /*enter*/
  2149. },
  2150. /**
  2151. * 获取节点A相对于节点B的位置关系
  2152. * @method getPosition
  2153. * @param { Node } nodeA 需要查询位置关系的节点A
  2154. * @param { Node } nodeB 需要查询位置关系的节点B
  2155. * @return { Number } 节点A与节点B的关系
  2156. * @example
  2157. * ```javascript
  2158. * //output: 20
  2159. * var position = UE.dom.domUtils.getPosition( document.documentElement, document.body );
  2160. *
  2161. * switch ( position ) {
  2162. *
  2163. * //0
  2164. * case UE.dom.domUtils.POSITION_IDENTICAL:
  2165. * console.log('元素相同');
  2166. * break;
  2167. * //1
  2168. * case UE.dom.domUtils.POSITION_DISCONNECTED:
  2169. * console.log('两个节点在不同的文档中');
  2170. * break;
  2171. * //2
  2172. * case UE.dom.domUtils.POSITION_FOLLOWING:
  2173. * console.log('节点A在节点B之后');
  2174. * break;
  2175. * //4
  2176. * case UE.dom.domUtils.POSITION_PRECEDING;
  2177. * console.log('节点A在节点B之前');
  2178. * break;
  2179. * //8
  2180. * case UE.dom.domUtils.POSITION_IS_CONTAINED:
  2181. * console.log('节点A被节点B包含');
  2182. * break;
  2183. * case 10:
  2184. * console.log('节点A被节点B包含且节点A在节点B之后');
  2185. * break;
  2186. * //16
  2187. * case UE.dom.domUtils.POSITION_CONTAINS:
  2188. * console.log('节点A包含节点B');
  2189. * break;
  2190. * case 20:
  2191. * console.log('节点A包含节点B且节点A在节点B之前');
  2192. * break;
  2193. *
  2194. * }
  2195. * ```
  2196. */
  2197. getPosition: function(nodeA, nodeB) {
  2198. // 如果两个节点是同一个节点
  2199. if (nodeA === nodeB) {
  2200. // domUtils.POSITION_IDENTICAL
  2201. return 0;
  2202. }
  2203. var node,
  2204. parentsA = [nodeA],
  2205. parentsB = [nodeB];
  2206. node = nodeA;
  2207. while ((node = node.parentNode)) {
  2208. // 如果nodeB是nodeA的祖先节点
  2209. if (node === nodeB) {
  2210. // domUtils.POSITION_IS_CONTAINED + domUtils.POSITION_FOLLOWING
  2211. return 10;
  2212. }
  2213. parentsA.push(node);
  2214. }
  2215. node = nodeB;
  2216. while ((node = node.parentNode)) {
  2217. // 如果nodeA是nodeB的祖先节点
  2218. if (node === nodeA) {
  2219. // domUtils.POSITION_CONTAINS + domUtils.POSITION_PRECEDING
  2220. return 20;
  2221. }
  2222. parentsB.push(node);
  2223. }
  2224. parentsA.reverse();
  2225. parentsB.reverse();
  2226. if (parentsA[0] !== parentsB[0]) {
  2227. // domUtils.POSITION_DISCONNECTED
  2228. return 1;
  2229. }
  2230. var i = -1;
  2231. while ((i++, parentsA[i] === parentsB[i])) {}
  2232. nodeA = parentsA[i];
  2233. nodeB = parentsB[i];
  2234. while ((nodeA = nodeA.nextSibling)) {
  2235. if (nodeA === nodeB) {
  2236. // domUtils.POSITION_PRECEDING
  2237. return 4;
  2238. }
  2239. }
  2240. // domUtils.POSITION_FOLLOWING
  2241. return 2;
  2242. },
  2243. /**
  2244. * 检测节点node在父节点中的索引位置
  2245. * @method getNodeIndex
  2246. * @param { Node } node 需要检测的节点对象
  2247. * @return { Number } 该节点在父节点中的位置
  2248. * @see UE.dom.domUtils.getNodeIndex(Node,Boolean)
  2249. */
  2250. /**
  2251. * 检测节点node在父节点中的索引位置, 根据给定的mergeTextNode参数决定是否要合并多个连续的文本节点为一个节点
  2252. * @method getNodeIndex
  2253. * @param { Node } node 需要检测的节点对象
  2254. * @param { Boolean } mergeTextNode 是否合并多个连续的文本节点为一个节点
  2255. * @return { Number } 该节点在父节点中的位置
  2256. * @example
  2257. * ```javascript
  2258. *
  2259. * var node = document.createElement("div");
  2260. *
  2261. * node.appendChild( document.createTextNode( "hello" ) );
  2262. * node.appendChild( document.createTextNode( "world" ) );
  2263. * node.appendChild( node = document.createElement( "div" ) );
  2264. *
  2265. * //output: 2
  2266. * console.log( UE.dom.domUtils.getNodeIndex( node ) );
  2267. *
  2268. * //output: 1
  2269. * console.log( UE.dom.domUtils.getNodeIndex( node, true ) );
  2270. *
  2271. * ```
  2272. */
  2273. getNodeIndex: function(node, ignoreTextNode) {
  2274. var preNode = node,
  2275. i = 0;
  2276. while ((preNode = preNode.previousSibling)) {
  2277. if (ignoreTextNode && preNode.nodeType == 3) {
  2278. if (preNode.nodeType != preNode.nextSibling.nodeType) {
  2279. i++;
  2280. }
  2281. continue;
  2282. }
  2283. i++;
  2284. }
  2285. return i;
  2286. },
  2287. /**
  2288. * 检测节点node是否在给定的document对象上
  2289. * @method inDoc
  2290. * @param { Node } node 需要检测的节点对象
  2291. * @param { DomDocument } doc 需要检测的document对象
  2292. * @return { Boolean } 该节点node是否在给定的document的dom树上
  2293. * @example
  2294. * ```javascript
  2295. *
  2296. * var node = document.createElement("div");
  2297. *
  2298. * //output: false
  2299. * console.log( UE.do.domUtils.inDoc( node, document ) );
  2300. *
  2301. * document.body.appendChild( node );
  2302. *
  2303. * //output: true
  2304. * console.log( UE.do.domUtils.inDoc( node, document ) );
  2305. *
  2306. * ```
  2307. */
  2308. inDoc: function(node, doc) {
  2309. return domUtils.getPosition(node, doc) == 10;
  2310. },
  2311. /**
  2312. * 根据给定的过滤规则filterFn, 查找符合该过滤规则的node节点的第一个祖先节点,
  2313. * 查找的起点是给定node节点的父节点。
  2314. * @method findParent
  2315. * @param { Node } node 需要查找的节点
  2316. * @param { Function } filterFn 自定义的过滤方法。
  2317. * @warning 查找的终点是到body节点为止
  2318. * @remind 自定义的过滤方法filterFn接受一个Node对象作为参数, 该对象代表当前执行检测的祖先节点。 如果该
  2319. * 节点满足过滤条件, 则要求返回true, 这时将直接返回该节点作为findParent()的结果, 否则, 请返回false。
  2320. * @return { Node | Null } 如果找到符合过滤条件的节点, 就返回该节点, 否则返回NULL
  2321. * @example
  2322. * ```javascript
  2323. * var filterNode = UE.dom.domUtils.findParent( document.body.firstChild, function ( node ) {
  2324. *
  2325. * //由于查找的终点是body节点, 所以永远也不会匹配当前过滤器的条件, 即这里永远会返回false
  2326. * return node.tagName === "HTML";
  2327. *
  2328. * } );
  2329. *
  2330. * //output: true
  2331. * console.log( filterNode === null );
  2332. * ```
  2333. */
  2334. /**
  2335. * 根据给定的过滤规则filterFn, 查找符合该过滤规则的node节点的第一个祖先节点,
  2336. * 如果includeSelf的值为true,则查找的起点是给定的节点node, 否则, 起点是node的父节点
  2337. * @method findParent
  2338. * @param { Node } node 需要查找的节点
  2339. * @param { Function } filterFn 自定义的过滤方法。
  2340. * @param { Boolean } includeSelf 查找过程是否包含自身
  2341. * @warning 查找的终点是到body节点为止
  2342. * @remind 自定义的过滤方法filterFn接受一个Node对象作为参数, 该对象代表当前执行检测的祖先节点。 如果该
  2343. * 节点满足过滤条件, 则要求返回true, 这时将直接返回该节点作为findParent()的结果, 否则, 请返回false。
  2344. * @remind 如果includeSelf为true, 则过滤器第一次执行时的参数会是节点本身。
  2345. * 反之, 过滤器第一次执行时的参数将是该节点的父节点。
  2346. * @return { Node | Null } 如果找到符合过滤条件的节点, 就返回该节点, 否则返回NULL
  2347. * @example
  2348. * ```html
  2349. * <body>
  2350. *
  2351. * <div id="test">
  2352. * </div>
  2353. *
  2354. * <script type="text/javascript">
  2355. *
  2356. * //output: DIV, BODY
  2357. * var filterNode = UE.dom.domUtils.findParent( document.getElementById( "test" ), function ( node ) {
  2358. *
  2359. * console.log( node.tagName );
  2360. * return false;
  2361. *
  2362. * }, true );
  2363. *
  2364. * </script>
  2365. * </body>
  2366. * ```
  2367. */
  2368. findParent: function(node, filterFn, includeSelf) {
  2369. if (node && !domUtils.isBody(node)) {
  2370. node = includeSelf ? node : node.parentNode;
  2371. while (node) {
  2372. if (!filterFn || filterFn(node) || domUtils.isBody(node)) {
  2373. return filterFn && !filterFn(node) && domUtils.isBody(node)
  2374. ? null
  2375. : node;
  2376. }
  2377. node = node.parentNode;
  2378. }
  2379. }
  2380. return null;
  2381. },
  2382. /**
  2383. * 查找node的节点名为tagName的第一个祖先节点, 查找的起点是node节点的父节点。
  2384. * @method findParentByTagName
  2385. * @param { Node } node 需要查找的节点对象
  2386. * @param { Array } tagNames 需要查找的父节点的名称数组
  2387. * @warning 查找的终点是到body节点为止
  2388. * @return { Node | NULL } 如果找到符合条件的节点, 则返回该节点, 否则返回NULL
  2389. * @example
  2390. * ```javascript
  2391. * var node = UE.dom.domUtils.findParentByTagName( document.getElementsByTagName("div")[0], [ "BODY" ] );
  2392. * //output: BODY
  2393. * console.log( node.tagName );
  2394. * ```
  2395. */
  2396. /**
  2397. * 查找node的节点名为tagName的祖先节点, 如果includeSelf的值为true,则查找的起点是给定的节点node,
  2398. * 否则, 起点是node的父节点。
  2399. * @method findParentByTagName
  2400. * @param { Node } node 需要查找的节点对象
  2401. * @param { Array } tagNames 需要查找的父节点的名称数组
  2402. * @param { Boolean } includeSelf 查找过程是否包含node节点自身
  2403. * @warning 查找的终点是到body节点为止
  2404. * @return { Node | NULL } 如果找到符合条件的节点, 则返回该节点, 否则返回NULL
  2405. * @example
  2406. * ```javascript
  2407. * var queryTarget = document.getElementsByTagName("div")[0];
  2408. * var node = UE.dom.domUtils.findParentByTagName( queryTarget, [ "DIV" ], true );
  2409. * //output: true
  2410. * console.log( queryTarget === node );
  2411. * ```
  2412. */
  2413. findParentByTagName: function(node, tagNames, includeSelf, excludeFn) {
  2414. tagNames = utils.listToMap(utils.isArray(tagNames) ? tagNames : [tagNames]);
  2415. return domUtils.findParent(
  2416. node,
  2417. function(node) {
  2418. return tagNames[node.tagName] && !(excludeFn && excludeFn(node));
  2419. },
  2420. includeSelf
  2421. );
  2422. },
  2423. /**
  2424. * 查找节点node的祖先节点集合, 查找的起点是给定节点的父节点,结果集中不包含给定的节点。
  2425. * @method findParents
  2426. * @param { Node } node 需要查找的节点对象
  2427. * @return { Array } 给定节点的祖先节点数组
  2428. * @grammar UE.dom.domUtils.findParents(node) => Array //返回一个祖先节点数组集合,不包含自身
  2429. * @grammar UE.dom.domUtils.findParents(node,includeSelf) => Array //返回一个祖先节点数组集合,includeSelf指定是否包含自身
  2430. * @grammar UE.dom.domUtils.findParents(node,includeSelf,filterFn) => Array //返回一个祖先节点数组集合,filterFn指定过滤条件,返回true的node将被选取
  2431. * @grammar UE.dom.domUtils.findParents(node,includeSelf,filterFn,closerFirst) => Array //返回一个祖先节点数组集合,closerFirst为true的话,node的直接父亲节点是数组的第0个
  2432. */
  2433. /**
  2434. * 查找节点node的祖先节点集合, 如果includeSelf的值为true,
  2435. * 则返回的结果集中允许出现当前给定的节点, 否则, 该节点不会出现在其结果集中。
  2436. * @method findParents
  2437. * @param { Node } node 需要查找的节点对象
  2438. * @param { Boolean } includeSelf 查找的结果中是否允许包含当前查找的节点对象
  2439. * @return { Array } 给定节点的祖先节点数组
  2440. */
  2441. findParents: function(node, includeSelf, filterFn, closerFirst) {
  2442. var parents = includeSelf && ((filterFn && filterFn(node)) || !filterFn)
  2443. ? [node]
  2444. : [];
  2445. while ((node = domUtils.findParent(node, filterFn))) {
  2446. parents.push(node);
  2447. }
  2448. return closerFirst ? parents : parents.reverse();
  2449. },
  2450. /**
  2451. * 在节点node后面插入新节点newNode
  2452. * @method insertAfter
  2453. * @param { Node } node 目标节点
  2454. * @param { Node } newNode 新插入的节点, 该节点将置于目标节点之后
  2455. * @return { Node } 新插入的节点
  2456. */
  2457. insertAfter: function(node, newNode) {
  2458. return node.nextSibling
  2459. ? node.parentNode.insertBefore(newNode, node.nextSibling)
  2460. : node.parentNode.appendChild(newNode);
  2461. },
  2462. /**
  2463. * 删除节点node及其下属的所有节点
  2464. * @method remove
  2465. * @param { Node } node 需要删除的节点对象
  2466. * @return { Node } 返回刚删除的节点对象
  2467. * @example
  2468. * ```html
  2469. * <div id="test">
  2470. * <div id="child">你好</div>
  2471. * </div>
  2472. * <script>
  2473. * UE.dom.domUtils.remove( document.body, false );
  2474. * //output: false
  2475. * console.log( document.getElementById( "child" ) !== null );
  2476. * </script>
  2477. * ```
  2478. */
  2479. /**
  2480. * 删除节点node,并根据keepChildren的值决定是否保留子节点
  2481. * @method remove
  2482. * @param { Node } node 需要删除的节点对象
  2483. * @param { Boolean } keepChildren 是否需要保留子节点
  2484. * @return { Node } 返回刚删除的节点对象
  2485. * @example
  2486. * ```html
  2487. * <div id="test">
  2488. * <div id="child">你好</div>
  2489. * </div>
  2490. * <script>
  2491. * UE.dom.domUtils.remove( document.body, true );
  2492. * //output: true
  2493. * console.log( document.getElementById( "child" ) !== null );
  2494. * </script>
  2495. * ```
  2496. */
  2497. remove: function(node, keepChildren) {
  2498. var parent = node.parentNode,
  2499. child;
  2500. if (parent) {
  2501. if (keepChildren && node.hasChildNodes()) {
  2502. while ((child = node.firstChild)) {
  2503. parent.insertBefore(child, node);
  2504. }
  2505. }
  2506. parent.removeChild(node);
  2507. }
  2508. return node;
  2509. },
  2510. /**
  2511. * 取得node节点的下一个兄弟节点, 如果该节点其后没有兄弟节点, 则递归查找其父节点之后的第一个兄弟节点,
  2512. * 直到找到满足条件的节点或者递归到BODY节点之后才会结束。
  2513. * @method getNextDomNode
  2514. * @param { Node } node 需要获取其后的兄弟节点的节点对象
  2515. * @return { Node | NULL } 如果找满足条件的节点, 则返回该节点, 否则返回NULL
  2516. * @example
  2517. * ```html
  2518. * <body>
  2519. * <div id="test">
  2520. * <span></span>
  2521. * </div>
  2522. * <i>xxx</i>
  2523. * </body>
  2524. * <script>
  2525. *
  2526. * //output: i节点
  2527. * console.log( UE.dom.domUtils.getNextDomNode( document.getElementById( "test" ) ) );
  2528. *
  2529. * </script>
  2530. * ```
  2531. * @example
  2532. * ```html
  2533. * <body>
  2534. * <div>
  2535. * <span></span>
  2536. * <i id="test">xxx</i>
  2537. * </div>
  2538. * <b>xxx</b>
  2539. * </body>
  2540. * <script>
  2541. *
  2542. * //由于id为test的i节点之后没有兄弟节点, 则查找其父节点(div)后面的兄弟节点
  2543. * //output: b节点
  2544. * console.log( UE.dom.domUtils.getNextDomNode( document.getElementById( "test" ) ) );
  2545. *
  2546. * </script>
  2547. * ```
  2548. */
  2549. /**
  2550. * 取得node节点的下一个兄弟节点, 如果startFromChild的值为ture,则先获取其子节点,
  2551. * 如果有子节点则直接返回第一个子节点;如果没有子节点或者startFromChild的值为false,
  2552. * 则执行<a href="#UE.dom.domUtils.getNextDomNode(Node)">getNextDomNode(Node node)</a>的查找过程。
  2553. * @method getNextDomNode
  2554. * @param { Node } node 需要获取其后的兄弟节点的节点对象
  2555. * @param { Boolean } startFromChild 查找过程是否从其子节点开始
  2556. * @return { Node | NULL } 如果找满足条件的节点, 则返回该节点, 否则返回NULL
  2557. * @see UE.dom.domUtils.getNextDomNode(Node)
  2558. */
  2559. getNextDomNode: function(node, startFromChild, filterFn, guard) {
  2560. return getDomNode(
  2561. node,
  2562. "firstChild",
  2563. "nextSibling",
  2564. startFromChild,
  2565. filterFn,
  2566. guard
  2567. );
  2568. },
  2569. getPreDomNode: function(node, startFromChild, filterFn, guard) {
  2570. return getDomNode(
  2571. node,
  2572. "lastChild",
  2573. "previousSibling",
  2574. startFromChild,
  2575. filterFn,
  2576. guard
  2577. );
  2578. },
  2579. /**
  2580. * 检测节点node是否属是UEditor定义的bookmark节点
  2581. * @method isBookmarkNode
  2582. * @private
  2583. * @param { Node } node 需要检测的节点对象
  2584. * @return { Boolean } 是否是bookmark节点
  2585. * @example
  2586. * ```html
  2587. * <span id="_baidu_bookmark_1"></span>
  2588. * <script>
  2589. * var bookmarkNode = document.getElementById("_baidu_bookmark_1");
  2590. * //output: true
  2591. * console.log( UE.dom.domUtils.isBookmarkNode( bookmarkNode ) );
  2592. * </script>
  2593. * ```
  2594. */
  2595. isBookmarkNode: function(node) {
  2596. return node.nodeType == 1 && node.id && /^_baidu_bookmark_/i.test(node.id);
  2597. },
  2598. /**
  2599. * 获取节点node所属的window对象
  2600. * @method getWindow
  2601. * @param { Node } node 节点对象
  2602. * @return { Window } 当前节点所属的window对象
  2603. * @example
  2604. * ```javascript
  2605. * //output: true
  2606. * console.log( UE.dom.domUtils.getWindow( document.body ) === window );
  2607. * ```
  2608. */
  2609. getWindow: function(node) {
  2610. var doc = node.ownerDocument || node;
  2611. return doc.defaultView || doc.parentWindow;
  2612. },
  2613. /**
  2614. * 获取离nodeA与nodeB最近的公共的祖先节点
  2615. * @method getCommonAncestor
  2616. * @param { Node } nodeA 第一个节点
  2617. * @param { Node } nodeB 第二个节点
  2618. * @remind 如果给定的两个节点是同一个节点, 将直接返回该节点。
  2619. * @return { Node | NULL } 如果未找到公共节点, 返回NULL, 否则返回最近的公共祖先节点。
  2620. * @example
  2621. * ```javascript
  2622. * var commonAncestor = UE.dom.domUtils.getCommonAncestor( document.body, document.body.firstChild );
  2623. * //output: true
  2624. * console.log( commonAncestor.tagName.toLowerCase() === 'body' );
  2625. * ```
  2626. */
  2627. getCommonAncestor: function(nodeA, nodeB) {
  2628. if (nodeA === nodeB) return nodeA;
  2629. var parentsA = [nodeA],
  2630. parentsB = [nodeB],
  2631. parent = nodeA,
  2632. i = -1;
  2633. while ((parent = parent.parentNode)) {
  2634. if (parent === nodeB) {
  2635. return parent;
  2636. }
  2637. parentsA.push(parent);
  2638. }
  2639. parent = nodeB;
  2640. while ((parent = parent.parentNode)) {
  2641. if (parent === nodeA) return parent;
  2642. parentsB.push(parent);
  2643. }
  2644. parentsA.reverse();
  2645. parentsB.reverse();
  2646. while ((i++, parentsA[i] === parentsB[i])) {}
  2647. return i == 0 ? null : parentsA[i - 1];
  2648. },
  2649. /**
  2650. * 清除node节点左右连续为空的兄弟inline节点
  2651. * @method clearEmptySibling
  2652. * @param { Node } node 执行的节点对象, 如果该节点的左右连续的兄弟节点是空的inline节点,
  2653. * 则这些兄弟节点将被删除
  2654. * @grammar UE.dom.domUtils.clearEmptySibling(node,ignoreNext) //ignoreNext指定是否忽略右边空节点
  2655. * @grammar UE.dom.domUtils.clearEmptySibling(node,ignoreNext,ignorePre) //ignorePre指定是否忽略左边空节点
  2656. * @example
  2657. * ```html
  2658. * <body>
  2659. * <div></div>
  2660. * <span id="test"></span>
  2661. * <i></i>
  2662. * <b></b>
  2663. * <em>xxx</em>
  2664. * <span></span>
  2665. * </body>
  2666. * <script>
  2667. *
  2668. * UE.dom.domUtils.clearEmptySibling( document.getElementById( "test" ) );
  2669. *
  2670. * //output: <div></div><span id="test"></span><em>xxx</em><span></span>
  2671. * console.log( document.body.innerHTML );
  2672. *
  2673. * </script>
  2674. * ```
  2675. */
  2676. /**
  2677. * 清除node节点左右连续为空的兄弟inline节点, 如果ignoreNext的值为true,
  2678. * 则忽略对右边兄弟节点的操作。
  2679. * @method clearEmptySibling
  2680. * @param { Node } node 执行的节点对象, 如果该节点的左右连续的兄弟节点是空的inline节点,
  2681. * @param { Boolean } ignoreNext 是否忽略忽略对右边的兄弟节点的操作
  2682. * 则这些兄弟节点将被删除
  2683. * @see UE.dom.domUtils.clearEmptySibling(Node)
  2684. */
  2685. /**
  2686. * 清除node节点左右连续为空的兄弟inline节点, 如果ignoreNext的值为true,
  2687. * 则忽略对右边兄弟节点的操作, 如果ignorePre的值为true,则忽略对左边兄弟节点的操作。
  2688. * @method clearEmptySibling
  2689. * @param { Node } node 执行的节点对象, 如果该节点的左右连续的兄弟节点是空的inline节点,
  2690. * @param { Boolean } ignoreNext 是否忽略忽略对右边的兄弟节点的操作
  2691. * @param { Boolean } ignorePre 是否忽略忽略对左边的兄弟节点的操作
  2692. * 则这些兄弟节点将被删除
  2693. * @see UE.dom.domUtils.clearEmptySibling(Node)
  2694. */
  2695. clearEmptySibling: function(node, ignoreNext, ignorePre) {
  2696. function clear(next, dir) {
  2697. var tmpNode;
  2698. while (
  2699. next &&
  2700. !domUtils.isBookmarkNode(next) &&
  2701. (domUtils.isEmptyInlineElement(next) ||
  2702. //这里不能把空格算进来会吧空格干掉,出现文字间的空格丢掉了
  2703. !new RegExp("[^\t\n\r" + domUtils.fillChar + "]").test(
  2704. next.nodeValue
  2705. ))
  2706. ) {
  2707. tmpNode = next[dir];
  2708. domUtils.remove(next);
  2709. next = tmpNode;
  2710. }
  2711. }
  2712. !ignoreNext && clear(node.nextSibling, "nextSibling");
  2713. !ignorePre && clear(node.previousSibling, "previousSibling");
  2714. },
  2715. /**
  2716. * 将一个文本节点textNode拆分成两个文本节点,offset指定拆分位置
  2717. * @method split
  2718. * @param { Node } textNode 需要拆分的文本节点对象
  2719. * @param { int } offset 需要拆分的位置, 位置计算从0开始
  2720. * @return { Node } 拆分后形成的新节点
  2721. * @example
  2722. * ```html
  2723. * <div id="test">abcdef</div>
  2724. * <script>
  2725. * var newNode = UE.dom.domUtils.split( document.getElementById( "test" ).firstChild, 3 );
  2726. * //output: def
  2727. * console.log( newNode.nodeValue );
  2728. * </script>
  2729. * ```
  2730. */
  2731. split: function(node, offset) {
  2732. var doc = node.ownerDocument;
  2733. if (browser.ie && offset == node.nodeValue.length) {
  2734. var next = doc.createTextNode("");
  2735. return domUtils.insertAfter(node, next);
  2736. }
  2737. var retval = node.splitText(offset);
  2738. //ie8下splitText不会跟新childNodes,我们手动触发他的更新
  2739. if (browser.ie8) {
  2740. var tmpNode = doc.createTextNode("");
  2741. domUtils.insertAfter(retval, tmpNode);
  2742. domUtils.remove(tmpNode);
  2743. }
  2744. return retval;
  2745. },
  2746. /**
  2747. * 检测文本节点textNode是否为空节点(包括空格、换行、占位符等字符)
  2748. * @method isWhitespace
  2749. * @param { Node } node 需要检测的节点对象
  2750. * @return { Boolean } 检测的节点是否为空
  2751. * @example
  2752. * ```html
  2753. * <div id="test">
  2754. *
  2755. * </div>
  2756. * <script>
  2757. * //output: true
  2758. * console.log( UE.dom.domUtils.isWhitespace( document.getElementById("test").firstChild ) );
  2759. * </script>
  2760. * ```
  2761. */
  2762. isWhitespace: function(node) {
  2763. return !new RegExp("[^ \t\n\r" + domUtils.fillChar + "]").test(
  2764. node.nodeValue
  2765. );
  2766. },
  2767. /**
  2768. * 获取元素element相对于viewport的位置坐标
  2769. * @method getXY
  2770. * @param { Node } element 需要计算位置的节点对象
  2771. * @return { Object } 返回形如{x:left,y:top}的一个key-value映射对象, 其中键x代表水平偏移距离,
  2772. * y代表垂直偏移距离。
  2773. *
  2774. * @example
  2775. * ```javascript
  2776. * var location = UE.dom.domUtils.getXY( document.getElementById("test") );
  2777. * //output: test的坐标为: 12, 24
  2778. * console.log( 'test的坐标为: ', location.x, ',', location.y );
  2779. * ```
  2780. */
  2781. getXY: function(element) {
  2782. var x = 0,
  2783. y = 0;
  2784. while (element.offsetParent) {
  2785. y += element.offsetTop;
  2786. x += element.offsetLeft;
  2787. element = element.offsetParent;
  2788. }
  2789. return { x: x, y: y };
  2790. },
  2791. /**
  2792. * 为元素element绑定原生DOM事件,type为事件类型,handler为处理函数
  2793. * @method on
  2794. * @param { Node } element 需要绑定事件的节点对象
  2795. * @param { String } type 绑定的事件类型
  2796. * @param { Function } handler 事件处理器
  2797. * @example
  2798. * ```javascript
  2799. * UE.dom.domUtils.on(document.body,"click",function(e){
  2800. * //e为事件对象,this为被点击元素对戏那个
  2801. * });
  2802. * ```
  2803. */
  2804. /**
  2805. * 为元素element绑定原生DOM事件,type为事件类型,handler为处理函数
  2806. * @method on
  2807. * @param { Node } element 需要绑定事件的节点对象
  2808. * @param { Array } type 绑定的事件类型数组
  2809. * @param { Function } handler 事件处理器
  2810. * @example
  2811. * ```javascript
  2812. * UE.dom.domUtils.on(document.body,["click","mousedown"],function(evt){
  2813. * //evt为事件对象,this为被点击元素对象
  2814. * });
  2815. * ```
  2816. */
  2817. on: function(element, type, handler) {
  2818. var types = utils.isArray(type) ? type : utils.trim(type).split(/\s+/),
  2819. k = types.length;
  2820. if (k)
  2821. while (k--) {
  2822. type = types[k];
  2823. if (element.addEventListener) {
  2824. element.addEventListener(type, handler, false);
  2825. } else {
  2826. if (!handler._d) {
  2827. handler._d = {
  2828. els: []
  2829. };
  2830. }
  2831. var key = type + handler.toString(),
  2832. index = utils.indexOf(handler._d.els, element);
  2833. if (!handler._d[key] || index == -1) {
  2834. if (index == -1) {
  2835. handler._d.els.push(element);
  2836. }
  2837. if (!handler._d[key]) {
  2838. handler._d[key] = function(evt) {
  2839. return handler.call(evt.srcElement, evt || window.event);
  2840. };
  2841. }
  2842. element.attachEvent("on" + type, handler._d[key]);
  2843. }
  2844. }
  2845. }
  2846. element = null;
  2847. },
  2848. /**
  2849. * 解除DOM事件绑定
  2850. * @method un
  2851. * @param { Node } element 需要解除事件绑定的节点对象
  2852. * @param { String } type 需要接触绑定的事件类型
  2853. * @param { Function } handler 对应的事件处理器
  2854. * @example
  2855. * ```javascript
  2856. * UE.dom.domUtils.un(document.body,"click",function(evt){
  2857. * //evt为事件对象,this为被点击元素对象
  2858. * });
  2859. * ```
  2860. */
  2861. /**
  2862. * 解除DOM事件绑定
  2863. * @method un
  2864. * @param { Node } element 需要解除事件绑定的节点对象
  2865. * @param { Array } type 需要接触绑定的事件类型数组
  2866. * @param { Function } handler 对应的事件处理器
  2867. * @example
  2868. * ```javascript
  2869. * UE.dom.domUtils.un(document.body, ["click","mousedown"],function(evt){
  2870. * //evt为事件对象,this为被点击元素对象
  2871. * });
  2872. * ```
  2873. */
  2874. un: function(element, type, handler) {
  2875. var types = utils.isArray(type) ? type : utils.trim(type).split(/\s+/),
  2876. k = types.length;
  2877. if (k)
  2878. while (k--) {
  2879. type = types[k];
  2880. if (element.removeEventListener) {
  2881. element.removeEventListener(type, handler, false);
  2882. } else {
  2883. var key = type + handler.toString();
  2884. try {
  2885. element.detachEvent(
  2886. "on" + type,
  2887. handler._d ? handler._d[key] : handler
  2888. );
  2889. } catch (e) {}
  2890. if (handler._d && handler._d[key]) {
  2891. var index = utils.indexOf(handler._d.els, element);
  2892. if (index != -1) {
  2893. handler._d.els.splice(index, 1);
  2894. }
  2895. handler._d.els.length == 0 && delete handler._d[key];
  2896. }
  2897. }
  2898. }
  2899. },
  2900. /**
  2901. * 比较节点nodeA与节点nodeB是否具有相同的标签名、属性名以及属性值
  2902. * @method isSameElement
  2903. * @param { Node } nodeA 需要比较的节点
  2904. * @param { Node } nodeB 需要比较的节点
  2905. * @return { Boolean } 两个节点是否具有相同的标签名、属性名以及属性值
  2906. * @example
  2907. * ```html
  2908. * <span style="font-size:12px">ssss</span>
  2909. * <span style="font-size:12px">bbbbb</span>
  2910. * <span style="font-size:13px">ssss</span>
  2911. * <span style="font-size:14px">bbbbb</span>
  2912. *
  2913. * <script>
  2914. *
  2915. * var nodes = document.getElementsByTagName( "span" );
  2916. *
  2917. * //output: true
  2918. * console.log( UE.dom.domUtils.isSameElement( nodes[0], nodes[1] ) );
  2919. *
  2920. * //output: false
  2921. * console.log( UE.dom.domUtils.isSameElement( nodes[2], nodes[3] ) );
  2922. *
  2923. * </script>
  2924. * ```
  2925. */
  2926. isSameElement: function(nodeA, nodeB) {
  2927. if (nodeA.tagName != nodeB.tagName) {
  2928. return false;
  2929. }
  2930. var thisAttrs = nodeA.attributes,
  2931. otherAttrs = nodeB.attributes;
  2932. if (!ie && thisAttrs.length != otherAttrs.length) {
  2933. return false;
  2934. }
  2935. var attrA,
  2936. attrB,
  2937. al = 0,
  2938. bl = 0;
  2939. for (var i = 0; (attrA = thisAttrs[i++]); ) {
  2940. if (attrA.nodeName == "style") {
  2941. if (attrA.specified) {
  2942. al++;
  2943. }
  2944. if (domUtils.isSameStyle(nodeA, nodeB)) {
  2945. continue;
  2946. } else {
  2947. return false;
  2948. }
  2949. }
  2950. if (ie) {
  2951. if (attrA.specified) {
  2952. al++;
  2953. attrB = otherAttrs.getNamedItem(attrA.nodeName);
  2954. } else {
  2955. continue;
  2956. }
  2957. } else {
  2958. attrB = nodeB.attributes[attrA.nodeName];
  2959. }
  2960. if (!attrB.specified || attrA.nodeValue != attrB.nodeValue) {
  2961. return false;
  2962. }
  2963. }
  2964. // 有可能attrB的属性包含了attrA的属性之外还有自己的属性
  2965. if (ie) {
  2966. for (i = 0; (attrB = otherAttrs[i++]); ) {
  2967. if (attrB.specified) {
  2968. bl++;
  2969. }
  2970. }
  2971. if (al != bl) {
  2972. return false;
  2973. }
  2974. }
  2975. return true;
  2976. },
  2977. /**
  2978. * 判断节点nodeA与节点nodeB的元素的style属性是否一致
  2979. * @method isSameStyle
  2980. * @param { Node } nodeA 需要比较的节点
  2981. * @param { Node } nodeB 需要比较的节点
  2982. * @return { Boolean } 两个节点是否具有相同的style属性值
  2983. * @example
  2984. * ```html
  2985. * <span style="font-size:12px">ssss</span>
  2986. * <span style="font-size:12px">bbbbb</span>
  2987. * <span style="font-size:13px">ssss</span>
  2988. * <span style="font-size:14px">bbbbb</span>
  2989. *
  2990. * <script>
  2991. *
  2992. * var nodes = document.getElementsByTagName( "span" );
  2993. *
  2994. * //output: true
  2995. * console.log( UE.dom.domUtils.isSameStyle( nodes[0], nodes[1] ) );
  2996. *
  2997. * //output: false
  2998. * console.log( UE.dom.domUtils.isSameStyle( nodes[2], nodes[3] ) );
  2999. *
  3000. * </script>
  3001. * ```
  3002. */
  3003. isSameStyle: function(nodeA, nodeB) {
  3004. var styleA = nodeA.style.cssText
  3005. .replace(/( ?; ?)/g, ";")
  3006. .replace(/( ?: ?)/g, ":"),
  3007. styleB = nodeB.style.cssText
  3008. .replace(/( ?; ?)/g, ";")
  3009. .replace(/( ?: ?)/g, ":");
  3010. if (browser.opera) {
  3011. styleA = nodeA.style;
  3012. styleB = nodeB.style;
  3013. if (styleA.length != styleB.length) return false;
  3014. for (var p in styleA) {
  3015. if (/^(\d+|csstext)$/i.test(p)) {
  3016. continue;
  3017. }
  3018. if (styleA[p] != styleB[p]) {
  3019. return false;
  3020. }
  3021. }
  3022. return true;
  3023. }
  3024. if (!styleA || !styleB) {
  3025. return styleA == styleB;
  3026. }
  3027. styleA = styleA.split(";");
  3028. styleB = styleB.split(";");
  3029. if (styleA.length != styleB.length) {
  3030. return false;
  3031. }
  3032. for (var i = 0, ci; (ci = styleA[i++]); ) {
  3033. if (utils.indexOf(styleB, ci) == -1) {
  3034. return false;
  3035. }
  3036. }
  3037. return true;
  3038. },
  3039. /**
  3040. * 检查节点node是否为block元素
  3041. * @method isBlockElm
  3042. * @param { Node } node 需要检测的节点对象
  3043. * @return { Boolean } 是否是block元素节点
  3044. * @warning 该方法的判断规则如下: 如果该元素原本是block元素, 则不论该元素当前的css样式是什么都会返回true;
  3045. * 否则,检测该元素的css样式, 如果该元素当前是block元素, 则返回true。 其余情况下都返回false。
  3046. * @example
  3047. * ```html
  3048. * <span id="test1" style="display: block"></span>
  3049. * <span id="test2"></span>
  3050. * <div id="test3" style="display: inline"></div>
  3051. *
  3052. * <script>
  3053. *
  3054. * //output: true
  3055. * console.log( UE.dom.domUtils.isBlockElm( document.getElementById("test1") ) );
  3056. *
  3057. * //output: false
  3058. * console.log( UE.dom.domUtils.isBlockElm( document.getElementById("test2") ) );
  3059. *
  3060. * //output: true
  3061. * console.log( UE.dom.domUtils.isBlockElm( document.getElementById("test3") ) );
  3062. *
  3063. * </script>
  3064. * ```
  3065. */
  3066. isBlockElm: function(node) {
  3067. return (
  3068. node.nodeType == 1 &&
  3069. (dtd.$block[node.tagName] ||
  3070. styleBlock[domUtils.getComputedStyle(node, "display")]) &&
  3071. !dtd.$nonChild[node.tagName]
  3072. );
  3073. },
  3074. /**
  3075. * 检测node节点是否为body节点
  3076. * @method isBody
  3077. * @param { Element } node 需要检测的dom元素
  3078. * @return { Boolean } 给定的元素是否是body元素
  3079. * @example
  3080. * ```javascript
  3081. * //output: true
  3082. * console.log( UE.dom.domUtils.isBody( document.body ) );
  3083. * ```
  3084. */
  3085. isBody: function(node) {
  3086. return node && node.nodeType == 1 && node.tagName.toLowerCase() == "body";
  3087. },
  3088. /**
  3089. * 以node节点为分界,将该节点的指定祖先节点parent拆分成两个独立的节点,
  3090. * 拆分形成的两个节点之间是node节点
  3091. * @method breakParent
  3092. * @param { Node } node 作为分界的节点对象
  3093. * @param { Node } parent 该节点必须是node节点的祖先节点, 且是block节点。
  3094. * @return { Node } 给定的node分界节点
  3095. * @example
  3096. * ```javascript
  3097. *
  3098. * var node = document.createElement("span"),
  3099. * wrapNode = document.createElement( "div" ),
  3100. * parent = document.createElement("p");
  3101. *
  3102. * parent.appendChild( node );
  3103. * wrapNode.appendChild( parent );
  3104. *
  3105. * //拆分前
  3106. * //output: <p><span></span></p>
  3107. * console.log( wrapNode.innerHTML );
  3108. *
  3109. *
  3110. * UE.dom.domUtils.breakParent( node, parent );
  3111. * //拆分后
  3112. * //output: <p></p><span></span><p></p>
  3113. * console.log( wrapNode.innerHTML );
  3114. *
  3115. * ```
  3116. */
  3117. breakParent: function(node, parent) {
  3118. var tmpNode,
  3119. parentClone = node,
  3120. clone = node,
  3121. leftNodes,
  3122. rightNodes;
  3123. do {
  3124. parentClone = parentClone.parentNode;
  3125. if (leftNodes) {
  3126. tmpNode = parentClone.cloneNode(false);
  3127. tmpNode.appendChild(leftNodes);
  3128. leftNodes = tmpNode;
  3129. tmpNode = parentClone.cloneNode(false);
  3130. tmpNode.appendChild(rightNodes);
  3131. rightNodes = tmpNode;
  3132. } else {
  3133. leftNodes = parentClone.cloneNode(false);
  3134. rightNodes = leftNodes.cloneNode(false);
  3135. }
  3136. while ((tmpNode = clone.previousSibling)) {
  3137. leftNodes.insertBefore(tmpNode, leftNodes.firstChild);
  3138. }
  3139. while ((tmpNode = clone.nextSibling)) {
  3140. rightNodes.appendChild(tmpNode);
  3141. }
  3142. clone = parentClone;
  3143. } while (parent !== parentClone);
  3144. tmpNode = parent.parentNode;
  3145. tmpNode.insertBefore(leftNodes, parent);
  3146. tmpNode.insertBefore(rightNodes, parent);
  3147. tmpNode.insertBefore(node, rightNodes);
  3148. domUtils.remove(parent);
  3149. return node;
  3150. },
  3151. /**
  3152. * 检查节点node是否是空inline节点
  3153. * @method isEmptyInlineElement
  3154. * @param { Node } node 需要检测的节点对象
  3155. * @return { Number } 如果给定的节点是空的inline节点, 则返回1, 否则返回0。
  3156. * @example
  3157. * ```html
  3158. * <b><i></i></b> => 1
  3159. * <b><i></i><u></u></b> => 1
  3160. * <b></b> => 1
  3161. * <b>xx<i></i></b> => 0
  3162. * ```
  3163. */
  3164. isEmptyInlineElement: function(node) {
  3165. if (node.nodeType != 1 || !dtd.$removeEmpty[node.tagName]) {
  3166. return 0;
  3167. }
  3168. node = node.firstChild;
  3169. while (node) {
  3170. //如果是创建的bookmark就跳过
  3171. if (domUtils.isBookmarkNode(node)) {
  3172. return 0;
  3173. }
  3174. if (
  3175. (node.nodeType == 1 && !domUtils.isEmptyInlineElement(node)) ||
  3176. (node.nodeType == 3 && !domUtils.isWhitespace(node))
  3177. ) {
  3178. return 0;
  3179. }
  3180. node = node.nextSibling;
  3181. }
  3182. return 1;
  3183. },
  3184. /**
  3185. * 删除node节点下首尾两端的空白文本子节点
  3186. * @method trimWhiteTextNode
  3187. * @param { Element } node 需要执行删除操作的元素对象
  3188. * @example
  3189. * ```javascript
  3190. * var node = document.createElement("div");
  3191. *
  3192. * node.appendChild( document.createTextNode( "" ) );
  3193. *
  3194. * node.appendChild( document.createElement("div") );
  3195. *
  3196. * node.appendChild( document.createTextNode( "" ) );
  3197. *
  3198. * //3
  3199. * console.log( node.childNodes.length );
  3200. *
  3201. * UE.dom.domUtils.trimWhiteTextNode( node );
  3202. *
  3203. * //1
  3204. * console.log( node.childNodes.length );
  3205. * ```
  3206. */
  3207. trimWhiteTextNode: function(node) {
  3208. function remove(dir) {
  3209. var child;
  3210. while (
  3211. (child = node[dir]) &&
  3212. child.nodeType == 3 &&
  3213. domUtils.isWhitespace(child)
  3214. ) {
  3215. node.removeChild(child);
  3216. }
  3217. }
  3218. remove("firstChild");
  3219. remove("lastChild");
  3220. },
  3221. /**
  3222. * 合并node节点下相同的子节点
  3223. * @name mergeChild
  3224. * @desc
  3225. * UE.dom.domUtils.mergeChild(node,tagName) //tagName要合并的子节点的标签
  3226. * @example
  3227. * <p><span style="font-size:12px;">xx<span style="font-size:12px;">aa</span>xx</span></p>
  3228. * ==> UE.dom.domUtils.mergeChild(node,'span')
  3229. * <p><span style="font-size:12px;">xxaaxx</span></p>
  3230. */
  3231. mergeChild: function(node, tagName, attrs) {
  3232. var list = domUtils.getElementsByTagName(node, node.tagName.toLowerCase());
  3233. for (var i = 0, ci; (ci = list[i++]); ) {
  3234. if (!ci.parentNode || domUtils.isBookmarkNode(ci)) {
  3235. continue;
  3236. }
  3237. //span单独处理
  3238. if (ci.tagName.toLowerCase() == "span") {
  3239. if (node === ci.parentNode) {
  3240. domUtils.trimWhiteTextNode(node);
  3241. if (node.childNodes.length == 1) {
  3242. node.style.cssText = ci.style.cssText + ";" + node.style.cssText;
  3243. domUtils.remove(ci, true);
  3244. continue;
  3245. }
  3246. }
  3247. ci.style.cssText = node.style.cssText + ";" + ci.style.cssText;
  3248. if (attrs) {
  3249. var style = attrs.style;
  3250. if (style) {
  3251. style = style.split(";");
  3252. for (var j = 0, s; (s = style[j++]); ) {
  3253. ci.style[utils.cssStyleToDomStyle(s.split(":")[0])] = s.split(
  3254. ":"
  3255. )[1];
  3256. }
  3257. }
  3258. }
  3259. if (domUtils.isSameStyle(ci, node)) {
  3260. domUtils.remove(ci, true);
  3261. }
  3262. continue;
  3263. }
  3264. if (domUtils.isSameElement(node, ci)) {
  3265. domUtils.remove(ci, true);
  3266. }
  3267. }
  3268. },
  3269. /**
  3270. * 原生方法getElementsByTagName的封装
  3271. * @method getElementsByTagName
  3272. * @param { Node } node 目标节点对象
  3273. * @param { String } tagName 需要查找的节点的tagName, 多个tagName以空格分割
  3274. * @return { Array } 符合条件的节点集合
  3275. */
  3276. getElementsByTagName: function(node, name, filter) {
  3277. if (filter && utils.isString(filter)) {
  3278. var className = filter;
  3279. filter = function(node) {
  3280. return domUtils.hasClass(node, className);
  3281. };
  3282. }
  3283. name = utils.trim(name).replace(/[ ]{2,}/g, " ").split(" ");
  3284. var arr = [];
  3285. for (var n = 0, ni; (ni = name[n++]); ) {
  3286. var list = node.getElementsByTagName(ni);
  3287. for (var i = 0, ci; (ci = list[i++]); ) {
  3288. if (!filter || filter(ci)) arr.push(ci);
  3289. }
  3290. }
  3291. return arr;
  3292. },
  3293. /**
  3294. * 将节点node提取到父节点上
  3295. * @method mergeToParent
  3296. * @param { Element } node 需要提取的元素对象
  3297. * @example
  3298. * ```html
  3299. * <div id="parent">
  3300. * <div id="sub">
  3301. * <span id="child"></span>
  3302. * </div>
  3303. * </div>
  3304. *
  3305. * <script>
  3306. *
  3307. * var child = document.getElementById( "child" );
  3308. *
  3309. * //output: sub
  3310. * console.log( child.parentNode.id );
  3311. *
  3312. * UE.dom.domUtils.mergeToParent( child );
  3313. *
  3314. * //output: parent
  3315. * console.log( child.parentNode.id );
  3316. *
  3317. * </script>
  3318. * ```
  3319. */
  3320. mergeToParent: function(node) {
  3321. var parent = node.parentNode;
  3322. while (parent && dtd.$removeEmpty[parent.tagName]) {
  3323. if (parent.tagName == node.tagName || parent.tagName == "A") {
  3324. //针对a标签单独处理
  3325. domUtils.trimWhiteTextNode(parent);
  3326. //span需要特殊处理 不处理这样的情况 <span stlye="color:#fff">xxx<span style="color:#ccc">xxx</span>xxx</span>
  3327. if (
  3328. (parent.tagName == "SPAN" && !domUtils.isSameStyle(parent, node)) ||
  3329. (parent.tagName == "A" && node.tagName == "SPAN")
  3330. ) {
  3331. if (parent.childNodes.length > 1 || parent !== node.parentNode) {
  3332. node.style.cssText =
  3333. parent.style.cssText + ";" + node.style.cssText;
  3334. parent = parent.parentNode;
  3335. continue;
  3336. } else {
  3337. parent.style.cssText += ";" + node.style.cssText;
  3338. //trace:952 a标签要保持下划线
  3339. if (parent.tagName == "A") {
  3340. parent.style.textDecoration = "underline";
  3341. }
  3342. }
  3343. }
  3344. if (parent.tagName != "A") {
  3345. parent === node.parentNode && domUtils.remove(node, true);
  3346. break;
  3347. }
  3348. }
  3349. parent = parent.parentNode;
  3350. }
  3351. },
  3352. /**
  3353. * 合并节点node的左右兄弟节点
  3354. * @method mergeSibling
  3355. * @param { Element } node 需要合并的目标节点
  3356. * @example
  3357. * ```html
  3358. * <b>xxxx</b><b id="test">ooo</b><b>xxxx</b>
  3359. *
  3360. * <script>
  3361. * var demoNode = document.getElementById("test");
  3362. * UE.dom.domUtils.mergeSibling( demoNode );
  3363. * //output: xxxxoooxxxx
  3364. * console.log( demoNode.innerHTML );
  3365. * </script>
  3366. * ```
  3367. */
  3368. /**
  3369. * 合并节点node的左右兄弟节点, 可以根据给定的条件选择是否忽略合并左节点。
  3370. * @method mergeSibling
  3371. * @param { Element } node 需要合并的目标节点
  3372. * @param { Boolean } ignorePre 是否忽略合并左节点
  3373. * @example
  3374. * ```html
  3375. * <b>xxxx</b><b id="test">ooo</b><b>xxxx</b>
  3376. *
  3377. * <script>
  3378. * var demoNode = document.getElementById("test");
  3379. * UE.dom.domUtils.mergeSibling( demoNode, true );
  3380. * //output: oooxxxx
  3381. * console.log( demoNode.innerHTML );
  3382. * </script>
  3383. * ```
  3384. */
  3385. /**
  3386. * 合并节点node的左右兄弟节点,可以根据给定的条件选择是否忽略合并左右节点。
  3387. * @method mergeSibling
  3388. * @param { Element } node 需要合并的目标节点
  3389. * @param { Boolean } ignorePre 是否忽略合并左节点
  3390. * @param { Boolean } ignoreNext 是否忽略合并右节点
  3391. * @remind 如果同时忽略左右节点, 则该操作什么也不会做
  3392. * @example
  3393. * ```html
  3394. * <b>xxxx</b><b id="test">ooo</b><b>xxxx</b>
  3395. *
  3396. * <script>
  3397. * var demoNode = document.getElementById("test");
  3398. * UE.dom.domUtils.mergeSibling( demoNode, false, true );
  3399. * //output: xxxxooo
  3400. * console.log( demoNode.innerHTML );
  3401. * </script>
  3402. * ```
  3403. */
  3404. mergeSibling: function(node, ignorePre, ignoreNext) {
  3405. function merge(rtl, start, node) {
  3406. var next;
  3407. if (
  3408. (next = node[rtl]) &&
  3409. !domUtils.isBookmarkNode(next) &&
  3410. next.nodeType == 1 &&
  3411. domUtils.isSameElement(node, next)
  3412. ) {
  3413. while (next.firstChild) {
  3414. if (start == "firstChild") {
  3415. node.insertBefore(next.lastChild, node.firstChild);
  3416. } else {
  3417. node.appendChild(next.firstChild);
  3418. }
  3419. }
  3420. domUtils.remove(next);
  3421. }
  3422. }
  3423. !ignorePre && merge("previousSibling", "firstChild", node);
  3424. !ignoreNext && merge("nextSibling", "lastChild", node);
  3425. },
  3426. /**
  3427. * 设置节点node及其子节点不会被选中
  3428. * @method unSelectable
  3429. * @param { Element } node 需要执行操作的dom元素
  3430. * @remind 执行该操作后的节点, 将不能被鼠标选中
  3431. * @example
  3432. * ```javascript
  3433. * UE.dom.domUtils.unSelectable( document.body );
  3434. * ```
  3435. */
  3436. unSelectable: (ie && browser.ie9below) || browser.opera
  3437. ? function(node) {
  3438. //for ie9
  3439. node.onselectstart = function() {
  3440. return false;
  3441. };
  3442. node.onclick = node.onkeyup = node.onkeydown = function() {
  3443. return false;
  3444. };
  3445. node.unselectable = "on";
  3446. node.setAttribute("unselectable", "on");
  3447. for (var i = 0, ci; (ci = node.all[i++]); ) {
  3448. switch (ci.tagName.toLowerCase()) {
  3449. case "iframe":
  3450. case "textarea":
  3451. case "input":
  3452. case "select":
  3453. break;
  3454. default:
  3455. ci.unselectable = "on";
  3456. node.setAttribute("unselectable", "on");
  3457. }
  3458. }
  3459. }
  3460. : function(node) {
  3461. node.style.MozUserSelect = node.style.webkitUserSelect = node.style.msUserSelect = node.style.KhtmlUserSelect =
  3462. "none";
  3463. },
  3464. /**
  3465. * 删除节点node上的指定属性名称的属性
  3466. * @method removeAttributes
  3467. * @param { Node } node 需要删除属性的节点对象
  3468. * @param { String } attrNames 可以是空格隔开的多个属性名称,该操作将会依次删除相应的属性
  3469. * @example
  3470. * ```html
  3471. * <div id="wrap">
  3472. * <span style="font-size:14px;" id="test" name="followMe">xxxxx</span>
  3473. * </div>
  3474. *
  3475. * <script>
  3476. *
  3477. * UE.dom.domUtils.removeAttributes( document.getElementById( "test" ), "id name" );
  3478. *
  3479. * //output: <span style="font-size:14px;">xxxxx</span>
  3480. * console.log( document.getElementById("wrap").innerHTML );
  3481. *
  3482. * </script>
  3483. * ```
  3484. */
  3485. /**
  3486. * 删除节点node上的指定属性名称的属性
  3487. * @method removeAttributes
  3488. * @param { Node } node 需要删除属性的节点对象
  3489. * @param { Array } attrNames 需要删除的属性名数组
  3490. * @example
  3491. * ```html
  3492. * <div id="wrap">
  3493. * <span style="font-size:14px;" id="test" name="followMe">xxxxx</span>
  3494. * </div>
  3495. *
  3496. * <script>
  3497. *
  3498. * UE.dom.domUtils.removeAttributes( document.getElementById( "test" ), ["id", "name"] );
  3499. *
  3500. * //output: <span style="font-size:14px;">xxxxx</span>
  3501. * console.log( document.getElementById("wrap").innerHTML );
  3502. *
  3503. * </script>
  3504. * ```
  3505. */
  3506. removeAttributes: function(node, attrNames) {
  3507. attrNames = utils.isArray(attrNames)
  3508. ? attrNames
  3509. : utils.trim(attrNames).replace(/[ ]{2,}/g, " ").split(" ");
  3510. for (var i = 0, ci; (ci = attrNames[i++]); ) {
  3511. ci = attrFix[ci] || ci;
  3512. switch (ci) {
  3513. case "className":
  3514. node[ci] = "";
  3515. break;
  3516. case "style":
  3517. node.style.cssText = "";
  3518. var val = node.getAttributeNode("style");
  3519. !browser.ie && val && node.removeAttributeNode(val);
  3520. }
  3521. node.removeAttribute(ci);
  3522. }
  3523. },
  3524. /**
  3525. * 在doc下创建一个标签名为tag,属性为attrs的元素
  3526. * @method createElement
  3527. * @param { DomDocument } doc 新创建的元素属于该document节点创建
  3528. * @param { String } tagName 需要创建的元素的标签名
  3529. * @param { Object } attrs 新创建的元素的属性key-value集合
  3530. * @return { Element } 新创建的元素对象
  3531. * @example
  3532. * ```javascript
  3533. * var ele = UE.dom.domUtils.createElement( document, 'div', {
  3534. * id: 'test'
  3535. * } );
  3536. *
  3537. * //output: DIV
  3538. * console.log( ele.tagName );
  3539. *
  3540. * //output: test
  3541. * console.log( ele.id );
  3542. *
  3543. * ```
  3544. */
  3545. createElement: function(doc, tag, attrs) {
  3546. return domUtils.setAttributes(doc.createElement(tag), attrs);
  3547. },
  3548. /**
  3549. * 为节点node添加属性attrs,attrs为属性键值对
  3550. * @method setAttributes
  3551. * @param { Element } node 需要设置属性的元素对象
  3552. * @param { Object } attrs 需要设置的属性名-值对
  3553. * @return { Element } 设置属性的元素对象
  3554. * @example
  3555. * ```html
  3556. * <span id="test"></span>
  3557. *
  3558. * <script>
  3559. *
  3560. * var testNode = UE.dom.domUtils.setAttributes( document.getElementById( "test" ), {
  3561. * id: 'demo'
  3562. * } );
  3563. *
  3564. * //output: demo
  3565. * console.log( testNode.id );
  3566. *
  3567. * </script>
  3568. *
  3569. */
  3570. setAttributes: function(node, attrs) {
  3571. for (var attr in attrs) {
  3572. if (attrs.hasOwnProperty(attr)) {
  3573. var value = attrs[attr];
  3574. switch (attr) {
  3575. case "class":
  3576. //ie下要这样赋值,setAttribute不起作用
  3577. node.className = value;
  3578. break;
  3579. case "style":
  3580. node.style.cssText = node.style.cssText + ";" + value;
  3581. break;
  3582. case "innerHTML":
  3583. node[attr] = value;
  3584. break;
  3585. case "value":
  3586. node.value = value;
  3587. break;
  3588. default:
  3589. node.setAttribute(attrFix[attr] || attr, value);
  3590. }
  3591. }
  3592. }
  3593. return node;
  3594. },
  3595. /**
  3596. * 获取元素element经过计算后的样式值
  3597. * @method getComputedStyle
  3598. * @param { Element } element 需要获取样式的元素对象
  3599. * @param { String } styleName 需要获取的样式名
  3600. * @return { String } 获取到的样式值
  3601. * @example
  3602. * ```html
  3603. * <style type="text/css">
  3604. * #test {
  3605. * font-size: 15px;
  3606. * }
  3607. * </style>
  3608. *
  3609. * <span id="test"></span>
  3610. *
  3611. * <script>
  3612. * //output: 15px
  3613. * console.log( UE.dom.domUtils.getComputedStyle( document.getElementById( "test" ), 'font-size' ) );
  3614. * </script>
  3615. * ```
  3616. */
  3617. getComputedStyle: function(element, styleName) {
  3618. //一下的属性单独处理
  3619. var pros = "width height top left";
  3620. if (pros.indexOf(styleName) > -1) {
  3621. return (
  3622. element[
  3623. "offset" +
  3624. styleName.replace(/^\w/, function(s) {
  3625. return s.toUpperCase();
  3626. })
  3627. ] + "px"
  3628. );
  3629. }
  3630. //忽略文本节点
  3631. if (element.nodeType == 3) {
  3632. element = element.parentNode;
  3633. }
  3634. //ie下font-size若body下定义了font-size,则从currentStyle里会取到这个font-size. 取不到实际值,故此修改.
  3635. if (
  3636. browser.ie &&
  3637. browser.version < 9 &&
  3638. styleName == "font-size" &&
  3639. !element.style.fontSize &&
  3640. !dtd.$empty[element.tagName] &&
  3641. !dtd.$nonChild[element.tagName]
  3642. ) {
  3643. var span = element.ownerDocument.createElement("span");
  3644. span.style.cssText = "padding:0;border:0;font-family:simsun;";
  3645. span.innerHTML = ".";
  3646. element.appendChild(span);
  3647. var result = span.offsetHeight;
  3648. element.removeChild(span);
  3649. span = null;
  3650. return result + "px";
  3651. }
  3652. try {
  3653. var value =
  3654. domUtils.getStyle(element, styleName) ||
  3655. (window.getComputedStyle
  3656. ? domUtils
  3657. .getWindow(element)
  3658. .getComputedStyle(element, "")
  3659. .getPropertyValue(styleName)
  3660. : (element.currentStyle || element.style)[
  3661. utils.cssStyleToDomStyle(styleName)
  3662. ]);
  3663. } catch (e) {
  3664. return "";
  3665. }
  3666. return utils.transUnitToPx(utils.fixColor(styleName, value));
  3667. },
  3668. /**
  3669. * 删除元素element指定的className
  3670. * @method removeClasses
  3671. * @param { Element } ele 需要删除class的元素节点
  3672. * @param { String } classNames 需要删除的className, 多个className之间以空格分开
  3673. * @example
  3674. * ```html
  3675. * <span id="test" class="test1 test2 test3">xxx</span>
  3676. *
  3677. * <script>
  3678. *
  3679. * var testNode = document.getElementById( "test" );
  3680. * UE.dom.domUtils.removeClasses( testNode, "test1 test2" );
  3681. *
  3682. * //output: test3
  3683. * console.log( testNode.className );
  3684. *
  3685. * </script>
  3686. * ```
  3687. */
  3688. /**
  3689. * 删除元素element指定的className
  3690. * @method removeClasses
  3691. * @param { Element } ele 需要删除class的元素节点
  3692. * @param { Array } classNames 需要删除的className数组
  3693. * @example
  3694. * ```html
  3695. * <span id="test" class="test1 test2 test3">xxx</span>
  3696. *
  3697. * <script>
  3698. *
  3699. * var testNode = document.getElementById( "test" );
  3700. * UE.dom.domUtils.removeClasses( testNode, ["test1", "test2"] );
  3701. *
  3702. * //output: test3
  3703. * console.log( testNode.className );
  3704. *
  3705. * </script>
  3706. * ```
  3707. */
  3708. removeClasses: function(elm, classNames) {
  3709. classNames = utils.isArray(classNames)
  3710. ? classNames
  3711. : utils.trim(classNames).replace(/[ ]{2,}/g, " ").split(" ");
  3712. for (var i = 0, ci, cls = elm.className; (ci = classNames[i++]); ) {
  3713. cls = cls.replace(new RegExp("\\b" + ci + "\\b"), "");
  3714. }
  3715. cls = utils.trim(cls).replace(/[ ]{2,}/g, " ");
  3716. if (cls) {
  3717. elm.className = cls;
  3718. } else {
  3719. domUtils.removeAttributes(elm, ["class"]);
  3720. }
  3721. },
  3722. /**
  3723. * 给元素element添加className
  3724. * @method addClass
  3725. * @param { Node } ele 需要增加className的元素
  3726. * @param { String } classNames 需要添加的className, 多个className之间以空格分割
  3727. * @remind 相同的类名不会被重复添加
  3728. * @example
  3729. * ```html
  3730. * <span id="test" class="cls1 cls2"></span>
  3731. *
  3732. * <script>
  3733. * var testNode = document.getElementById("test");
  3734. *
  3735. * UE.dom.domUtils.addClass( testNode, "cls2 cls3 cls4" );
  3736. *
  3737. * //output: cl1 cls2 cls3 cls4
  3738. * console.log( testNode.className );
  3739. *
  3740. * <script>
  3741. * ```
  3742. */
  3743. /**
  3744. * 给元素element添加className
  3745. * @method addClass
  3746. * @param { Node } ele 需要增加className的元素
  3747. * @param { Array } classNames 需要添加的className的数组
  3748. * @remind 相同的类名不会被重复添加
  3749. * @example
  3750. * ```html
  3751. * <span id="test" class="cls1 cls2"></span>
  3752. *
  3753. * <script>
  3754. * var testNode = document.getElementById("test");
  3755. *
  3756. * UE.dom.domUtils.addClass( testNode, ["cls2", "cls3", "cls4"] );
  3757. *
  3758. * //output: cl1 cls2 cls3 cls4
  3759. * console.log( testNode.className );
  3760. *
  3761. * <script>
  3762. * ```
  3763. */
  3764. addClass: function(elm, classNames) {
  3765. if (!elm) return;
  3766. classNames = utils.trim(classNames).replace(/[ ]{2,}/g, " ").split(" ");
  3767. for (var i = 0, ci, cls = elm.className; (ci = classNames[i++]); ) {
  3768. if (!new RegExp("\\b" + ci + "\\b").test(cls)) {
  3769. cls += " " + ci;
  3770. }
  3771. }
  3772. elm.className = utils.trim(cls);
  3773. },
  3774. /**
  3775. * 判断元素element是否包含给定的样式类名className
  3776. * @method hasClass
  3777. * @param { Node } ele 需要检测的元素
  3778. * @param { String } classNames 需要检测的className, 多个className之间用空格分割
  3779. * @return { Boolean } 元素是否包含所有给定的className
  3780. * @example
  3781. * ```html
  3782. * <span id="test1" class="cls1 cls2"></span>
  3783. *
  3784. * <script>
  3785. * var test1 = document.getElementById("test1");
  3786. *
  3787. * //output: false
  3788. * console.log( UE.dom.domUtils.hasClass( test1, "cls2 cls1 cls3" ) );
  3789. *
  3790. * //output: true
  3791. * console.log( UE.dom.domUtils.hasClass( test1, "cls2 cls1" ) );
  3792. * </script>
  3793. * ```
  3794. */
  3795. /**
  3796. * 判断元素element是否包含给定的样式类名className
  3797. * @method hasClass
  3798. * @param { Node } ele 需要检测的元素
  3799. * @param { Array } classNames 需要检测的className数组
  3800. * @return { Boolean } 元素是否包含所有给定的className
  3801. * @example
  3802. * ```html
  3803. * <span id="test1" class="cls1 cls2"></span>
  3804. *
  3805. * <script>
  3806. * var test1 = document.getElementById("test1");
  3807. *
  3808. * //output: false
  3809. * console.log( UE.dom.domUtils.hasClass( test1, [ "cls2", "cls1", "cls3" ] ) );
  3810. *
  3811. * //output: true
  3812. * console.log( UE.dom.domUtils.hasClass( test1, [ "cls2", "cls1" ]) );
  3813. * </script>
  3814. * ```
  3815. */
  3816. hasClass: function(element, className) {
  3817. if (utils.isRegExp(className)) {
  3818. return className.test(element.className);
  3819. }
  3820. className = utils.trim(className).replace(/[ ]{2,}/g, " ").split(" ");
  3821. for (var i = 0, ci, cls = element.className; (ci = className[i++]); ) {
  3822. if (!new RegExp("\\b" + ci + "\\b", "i").test(cls)) {
  3823. return false;
  3824. }
  3825. }
  3826. return i - 1 == className.length;
  3827. },
  3828. /**
  3829. * 阻止事件默认行为
  3830. * @method preventDefault
  3831. * @param { Event } evt 需要阻止默认行为的事件对象
  3832. * @example
  3833. * ```javascript
  3834. * UE.dom.domUtils.preventDefault( evt );
  3835. * ```
  3836. */
  3837. preventDefault: function(evt) {
  3838. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
  3839. },
  3840. /**
  3841. * 删除元素element指定的样式
  3842. * @method removeStyle
  3843. * @param { Element } element 需要删除样式的元素
  3844. * @param { String } styleName 需要删除的样式名
  3845. * @example
  3846. * ```html
  3847. * <span id="test" style="color: red; background: blue;"></span>
  3848. *
  3849. * <script>
  3850. *
  3851. * var testNode = document.getElementById("test");
  3852. *
  3853. * UE.dom.domUtils.removeStyle( testNode, 'color' );
  3854. *
  3855. * //output: background: blue;
  3856. * console.log( testNode.style.cssText );
  3857. *
  3858. * </script>
  3859. * ```
  3860. */
  3861. removeStyle: function(element, name) {
  3862. if (browser.ie) {
  3863. //针对color先单独处理一下
  3864. if (name == "color") {
  3865. name = "(^|;)" + name;
  3866. }
  3867. element.style.cssText = element.style.cssText.replace(
  3868. new RegExp(name + "[^:]*:[^;]+;?", "ig"),
  3869. ""
  3870. );
  3871. } else {
  3872. if (element.style.removeProperty) {
  3873. element.style.removeProperty(name);
  3874. } else {
  3875. element.style.removeAttribute(utils.cssStyleToDomStyle(name));
  3876. }
  3877. }
  3878. if (!element.style.cssText) {
  3879. domUtils.removeAttributes(element, ["style"]);
  3880. }
  3881. },
  3882. /**
  3883. * 获取元素element的style属性的指定值
  3884. * @method getStyle
  3885. * @param { Element } element 需要获取属性值的元素
  3886. * @param { String } styleName 需要获取的style的名称
  3887. * @warning 该方法仅获取元素style属性中所标明的值
  3888. * @return { String } 该元素包含指定的style属性值
  3889. * @example
  3890. * ```html
  3891. * <div id="test" style="color: red;"></div>
  3892. *
  3893. * <script>
  3894. *
  3895. * var testNode = document.getElementById( "test" );
  3896. *
  3897. * //output: red
  3898. * console.log( UE.dom.domUtils.getStyle( testNode, "color" ) );
  3899. *
  3900. * //output: ""
  3901. * console.log( UE.dom.domUtils.getStyle( testNode, "background" ) );
  3902. *
  3903. * </script>
  3904. * ```
  3905. */
  3906. getStyle: function(element, name) {
  3907. var value = element.style[utils.cssStyleToDomStyle(name)];
  3908. return utils.fixColor(name, value);
  3909. },
  3910. /**
  3911. * 为元素element设置样式属性值
  3912. * @method setStyle
  3913. * @param { Element } element 需要设置样式的元素
  3914. * @param { String } styleName 样式名
  3915. * @param { String } styleValue 样式值
  3916. * @example
  3917. * ```html
  3918. * <div id="test"></div>
  3919. *
  3920. * <script>
  3921. *
  3922. * var testNode = document.getElementById( "test" );
  3923. *
  3924. * //output: ""
  3925. * console.log( testNode.style.color );
  3926. *
  3927. * UE.dom.domUtils.setStyle( testNode, 'color', 'red' );
  3928. * //output: "red"
  3929. * console.log( testNode.style.color );
  3930. *
  3931. * </script>
  3932. * ```
  3933. */
  3934. setStyle: function(element, name, value) {
  3935. element.style[utils.cssStyleToDomStyle(name)] = value;
  3936. if (!utils.trim(element.style.cssText)) {
  3937. this.removeAttributes(element, "style");
  3938. }
  3939. },
  3940. /**
  3941. * 为元素element设置多个样式属性值
  3942. * @method setStyles
  3943. * @param { Element } element 需要设置样式的元素
  3944. * @param { Object } styles 样式名值对
  3945. * @example
  3946. * ```html
  3947. * <div id="test"></div>
  3948. *
  3949. * <script>
  3950. *
  3951. * var testNode = document.getElementById( "test" );
  3952. *
  3953. * //output: ""
  3954. * console.log( testNode.style.color );
  3955. *
  3956. * UE.dom.domUtils.setStyles( testNode, {
  3957. * 'color': 'red'
  3958. * } );
  3959. * //output: "red"
  3960. * console.log( testNode.style.color );
  3961. *
  3962. * </script>
  3963. * ```
  3964. */
  3965. setStyles: function(element, styles) {
  3966. for (var name in styles) {
  3967. if (styles.hasOwnProperty(name)) {
  3968. domUtils.setStyle(element, name, styles[name]);
  3969. }
  3970. }
  3971. },
  3972. /**
  3973. * 删除_moz_dirty属性
  3974. * @private
  3975. * @method removeDirtyAttr
  3976. */
  3977. removeDirtyAttr: function(node) {
  3978. for (
  3979. var i = 0, ci, nodes = node.getElementsByTagName("*");
  3980. (ci = nodes[i++]);
  3981. ) {
  3982. ci.removeAttribute("_moz_dirty");
  3983. }
  3984. node.removeAttribute("_moz_dirty");
  3985. },
  3986. /**
  3987. * 获取子节点的数量
  3988. * @method getChildCount
  3989. * @param { Element } node 需要检测的元素
  3990. * @return { Number } 给定的node元素的子节点数量
  3991. * @example
  3992. * ```html
  3993. * <div id="test">
  3994. * <span></span>
  3995. * </div>
  3996. *
  3997. * <script>
  3998. *
  3999. * //output: 3
  4000. * console.log( UE.dom.domUtils.getChildCount( document.getElementById("test") ) );
  4001. *
  4002. * </script>
  4003. * ```
  4004. */
  4005. /**
  4006. * 根据给定的过滤规则, 获取符合条件的子节点的数量
  4007. * @method getChildCount
  4008. * @param { Element } node 需要检测的元素
  4009. * @param { Function } fn 过滤器, 要求对符合条件的子节点返回true, 反之则要求返回false
  4010. * @return { Number } 符合过滤条件的node元素的子节点数量
  4011. * @example
  4012. * ```html
  4013. * <div id="test">
  4014. * <span></span>
  4015. * </div>
  4016. *
  4017. * <script>
  4018. *
  4019. * //output: 1
  4020. * console.log( UE.dom.domUtils.getChildCount( document.getElementById("test"), function ( node ) {
  4021. *
  4022. * return node.nodeType === 1;
  4023. *
  4024. * } ) );
  4025. *
  4026. * </script>
  4027. * ```
  4028. */
  4029. getChildCount: function(node, fn) {
  4030. var count = 0,
  4031. first = node.firstChild;
  4032. fn =
  4033. fn ||
  4034. function() {
  4035. return 1;
  4036. };
  4037. while (first) {
  4038. if (fn(first)) {
  4039. count++;
  4040. }
  4041. first = first.nextSibling;
  4042. }
  4043. return count;
  4044. },
  4045. /**
  4046. * 判断给定节点是否为空节点
  4047. * @method isEmptyNode
  4048. * @param { Node } node 需要检测的节点对象
  4049. * @return { Boolean } 节点是否为空
  4050. * @example
  4051. * ```javascript
  4052. * UE.dom.domUtils.isEmptyNode( document.body );
  4053. * ```
  4054. */
  4055. isEmptyNode: function(node) {
  4056. return (
  4057. !node.firstChild ||
  4058. domUtils.getChildCount(node, function(node) {
  4059. return (
  4060. !domUtils.isBr(node) &&
  4061. !domUtils.isBookmarkNode(node) &&
  4062. !domUtils.isWhitespace(node)
  4063. );
  4064. }) == 0
  4065. );
  4066. },
  4067. clearSelectedArr: function(nodes) {
  4068. var node;
  4069. while ((node = nodes.pop())) {
  4070. domUtils.removeAttributes(node, ["class"]);
  4071. }
  4072. },
  4073. /**
  4074. * 将显示区域滚动到指定节点的位置
  4075. * @method scrollToView
  4076. * @param {Node} node 节点
  4077. * @param {window} win window对象
  4078. * @param {Number} offsetTop 距离上方的偏移量
  4079. */
  4080. scrollToView: function(node, win, offsetTop) {
  4081. var getViewPaneSize = function() {
  4082. var doc = win.document,
  4083. mode = doc.compatMode == "CSS1Compat";
  4084. return {
  4085. width:
  4086. (mode ? doc.documentElement.clientWidth : doc.body.clientWidth) || 0,
  4087. height:
  4088. (mode ? doc.documentElement.clientHeight : doc.body.clientHeight) || 0
  4089. };
  4090. },
  4091. getScrollPosition = function(win) {
  4092. if ("pageXOffset" in win) {
  4093. return {
  4094. x: win.pageXOffset || 0,
  4095. y: win.pageYOffset || 0
  4096. };
  4097. } else {
  4098. var doc = win.document;
  4099. return {
  4100. x: doc.documentElement.scrollLeft || doc.body.scrollLeft || 0,
  4101. y: doc.documentElement.scrollTop || doc.body.scrollTop || 0
  4102. };
  4103. }
  4104. };
  4105. var winHeight = getViewPaneSize().height,
  4106. offset = winHeight * -1 + offsetTop;
  4107. offset += node.offsetHeight || 0;
  4108. var elementPosition = domUtils.getXY(node);
  4109. offset += elementPosition.y;
  4110. var currentScroll = getScrollPosition(win).y;
  4111. // offset += 50;
  4112. if (offset > currentScroll || offset < currentScroll - winHeight) {
  4113. win.scrollTo(0, offset + (offset < 0 ? -20 : 20));
  4114. }
  4115. },
  4116. /**
  4117. * 判断给定节点是否为br
  4118. * @method isBr
  4119. * @param { Node } node 需要判断的节点对象
  4120. * @return { Boolean } 给定的节点是否是br节点
  4121. */
  4122. isBr: function(node) {
  4123. return node.nodeType == 1 && node.tagName == "BR";
  4124. },
  4125. /**
  4126. * 判断给定的节点是否是一个“填充”节点
  4127. * @private
  4128. * @method isFillChar
  4129. * @param { Node } node 需要判断的节点
  4130. * @param { Boolean } isInStart 是否从节点内容的开始位置匹配
  4131. * @returns { Boolean } 节点是否是填充节点
  4132. */
  4133. isFillChar: function(node, isInStart) {
  4134. if (node.nodeType != 3) return false;
  4135. var text = node.nodeValue;
  4136. if (isInStart) {
  4137. return new RegExp("^" + domUtils.fillChar).test(text);
  4138. }
  4139. return !text.replace(new RegExp(domUtils.fillChar, "g"), "").length;
  4140. },
  4141. isStartInblock: function(range) {
  4142. var tmpRange = range.cloneRange(),
  4143. flag = 0,
  4144. start = tmpRange.startContainer,
  4145. tmp;
  4146. if (start.nodeType == 1 && start.childNodes[tmpRange.startOffset]) {
  4147. start = start.childNodes[tmpRange.startOffset];
  4148. var pre = start.previousSibling;
  4149. while (pre && domUtils.isFillChar(pre)) {
  4150. start = pre;
  4151. pre = pre.previousSibling;
  4152. }
  4153. }
  4154. if (this.isFillChar(start, true) && tmpRange.startOffset == 1) {
  4155. tmpRange.setStartBefore(start);
  4156. start = tmpRange.startContainer;
  4157. }
  4158. while (start && domUtils.isFillChar(start)) {
  4159. tmp = start;
  4160. start = start.previousSibling;
  4161. }
  4162. if (tmp) {
  4163. tmpRange.setStartBefore(tmp);
  4164. start = tmpRange.startContainer;
  4165. }
  4166. if (
  4167. start.nodeType == 1 &&
  4168. domUtils.isEmptyNode(start) &&
  4169. tmpRange.startOffset == 1
  4170. ) {
  4171. tmpRange.setStart(start, 0).collapse(true);
  4172. }
  4173. while (!tmpRange.startOffset) {
  4174. start = tmpRange.startContainer;
  4175. if (domUtils.isBlockElm(start) || domUtils.isBody(start)) {
  4176. flag = 1;
  4177. break;
  4178. }
  4179. var pre = tmpRange.startContainer.previousSibling,
  4180. tmpNode;
  4181. if (!pre) {
  4182. tmpRange.setStartBefore(tmpRange.startContainer);
  4183. } else {
  4184. while (pre && domUtils.isFillChar(pre)) {
  4185. tmpNode = pre;
  4186. pre = pre.previousSibling;
  4187. }
  4188. if (tmpNode) {
  4189. tmpRange.setStartBefore(tmpNode);
  4190. } else {
  4191. tmpRange.setStartBefore(tmpRange.startContainer);
  4192. }
  4193. }
  4194. }
  4195. return flag && !domUtils.isBody(tmpRange.startContainer) ? 1 : 0;
  4196. },
  4197. /**
  4198. * 判断给定的元素是否是一个空元素
  4199. * @method isEmptyBlock
  4200. * @param { Element } node 需要判断的元素
  4201. * @return { Boolean } 是否是空元素
  4202. * @example
  4203. * ```html
  4204. * <div id="test"></div>
  4205. *
  4206. * <script>
  4207. * //output: true
  4208. * console.log( UE.dom.domUtils.isEmptyBlock( document.getElementById("test") ) );
  4209. * </script>
  4210. * ```
  4211. */
  4212. /**
  4213. * 根据指定的判断规则判断给定的元素是否是一个空元素
  4214. * @method isEmptyBlock
  4215. * @param { Element } node 需要判断的元素
  4216. * @param { RegExp } reg 对内容执行判断的正则表达式对象
  4217. * @return { Boolean } 是否是空元素
  4218. */
  4219. isEmptyBlock: function(node, reg) {
  4220. if (node.nodeType != 1) return 0;
  4221. reg = reg || new RegExp("[ \xa0\t\r\n" + domUtils.fillChar + "]", "g");
  4222. if (
  4223. node[browser.ie ? "innerText" : "textContent"].replace(reg, "").length > 0
  4224. ) {
  4225. return 0;
  4226. }
  4227. for (var n in dtd.$isNotEmpty) {
  4228. if (node.getElementsByTagName(n).length) {
  4229. return 0;
  4230. }
  4231. }
  4232. return 1;
  4233. },
  4234. /**
  4235. * 移动元素使得该元素的位置移动指定的偏移量的距离
  4236. * @method setViewportOffset
  4237. * @param { Element } element 需要设置偏移量的元素
  4238. * @param { Object } offset 偏移量, 形如{ left: 100, top: 50 }的一个键值对, 表示该元素将在
  4239. * 现有的位置上向水平方向偏移offset.left的距离, 在竖直方向上偏移
  4240. * offset.top的距离
  4241. * @example
  4242. * ```html
  4243. * <div id="test" style="top: 100px; left: 50px; position: absolute;"></div>
  4244. *
  4245. * <script>
  4246. *
  4247. * var testNode = document.getElementById("test");
  4248. *
  4249. * UE.dom.domUtils.setViewportOffset( testNode, {
  4250. * left: 200,
  4251. * top: 50
  4252. * } );
  4253. *
  4254. * //output: top: 300px; left: 100px; position: absolute;
  4255. * console.log( testNode.style.cssText );
  4256. *
  4257. * </script>
  4258. * ```
  4259. */
  4260. setViewportOffset: function(element, offset) {
  4261. var left = parseInt(element.style.left) | 0;
  4262. var top = parseInt(element.style.top) | 0;
  4263. var rect = element.getBoundingClientRect();
  4264. var offsetLeft = offset.left - rect.left;
  4265. var offsetTop = offset.top - rect.top;
  4266. if (offsetLeft) {
  4267. element.style.left = left + offsetLeft + "px";
  4268. }
  4269. if (offsetTop) {
  4270. element.style.top = top + offsetTop + "px";
  4271. }
  4272. },
  4273. /**
  4274. * 用“填充字符”填充节点
  4275. * @method fillNode
  4276. * @private
  4277. * @param { DomDocument } doc 填充的节点所在的docment对象
  4278. * @param { Node } node 需要填充的节点对象
  4279. * @example
  4280. * ```html
  4281. * <div id="test"></div>
  4282. *
  4283. * <script>
  4284. * var testNode = document.getElementById("test");
  4285. *
  4286. * //output: 0
  4287. * console.log( testNode.childNodes.length );
  4288. *
  4289. * UE.dom.domUtils.fillNode( document, testNode );
  4290. *
  4291. * //output: 1
  4292. * console.log( testNode.childNodes.length );
  4293. *
  4294. * </script>
  4295. * ```
  4296. */
  4297. fillNode: function(doc, node) {
  4298. var tmpNode = browser.ie
  4299. ? doc.createTextNode(domUtils.fillChar)
  4300. : doc.createElement("br");
  4301. node.innerHTML = "";
  4302. node.appendChild(tmpNode);
  4303. },
  4304. /**
  4305. * 把节点src的所有子节点追加到另一个节点tag上去
  4306. * @method moveChild
  4307. * @param { Node } src 源节点, 该节点下的所有子节点将被移除
  4308. * @param { Node } tag 目标节点, 从源节点移除的子节点将被追加到该节点下
  4309. * @example
  4310. * ```html
  4311. * <div id="test1">
  4312. * <span></span>
  4313. * </div>
  4314. * <div id="test2">
  4315. * <div></div>
  4316. * </div>
  4317. *
  4318. * <script>
  4319. *
  4320. * var test1 = document.getElementById("test1"),
  4321. * test2 = document.getElementById("test2");
  4322. *
  4323. * UE.dom.domUtils.moveChild( test1, test2 );
  4324. *
  4325. * //output: ""(空字符串)
  4326. * console.log( test1.innerHTML );
  4327. *
  4328. * //output: "<div></div><span></span>"
  4329. * console.log( test2.innerHTML );
  4330. *
  4331. * </script>
  4332. * ```
  4333. */
  4334. /**
  4335. * 把节点src的所有子节点移动到另一个节点tag上去, 可以通过dir参数控制附加的行为是“追加”还是“插入顶部”
  4336. * @method moveChild
  4337. * @param { Node } src 源节点, 该节点下的所有子节点将被移除
  4338. * @param { Node } tag 目标节点, 从源节点移除的子节点将被附加到该节点下
  4339. * @param { Boolean } dir 附加方式, 如果为true, 则附加进去的节点将被放到目标节点的顶部, 反之,则放到末尾
  4340. * @example
  4341. * ```html
  4342. * <div id="test1">
  4343. * <span></span>
  4344. * </div>
  4345. * <div id="test2">
  4346. * <div></div>
  4347. * </div>
  4348. *
  4349. * <script>
  4350. *
  4351. * var test1 = document.getElementById("test1"),
  4352. * test2 = document.getElementById("test2");
  4353. *
  4354. * UE.dom.domUtils.moveChild( test1, test2, true );
  4355. *
  4356. * //output: ""(空字符串)
  4357. * console.log( test1.innerHTML );
  4358. *
  4359. * //output: "<span></span><div></div>"
  4360. * console.log( test2.innerHTML );
  4361. *
  4362. * </script>
  4363. * ```
  4364. */
  4365. moveChild: function(src, tag, dir) {
  4366. while (src.firstChild) {
  4367. if (dir && tag.firstChild) {
  4368. tag.insertBefore(src.lastChild, tag.firstChild);
  4369. } else {
  4370. tag.appendChild(src.firstChild);
  4371. }
  4372. }
  4373. },
  4374. /**
  4375. * 判断节点的标签上是否不存在任何属性
  4376. * @method hasNoAttributes
  4377. * @private
  4378. * @param { Node } node 需要检测的节点对象
  4379. * @return { Boolean } 节点是否不包含任何属性
  4380. * @example
  4381. * ```html
  4382. * <div id="test"><span>xxxx</span></div>
  4383. *
  4384. * <script>
  4385. *
  4386. * //output: false
  4387. * console.log( UE.dom.domUtils.hasNoAttributes( document.getElementById("test") ) );
  4388. *
  4389. * //output: true
  4390. * console.log( UE.dom.domUtils.hasNoAttributes( document.getElementById("test").firstChild ) );
  4391. *
  4392. * </script>
  4393. * ```
  4394. */
  4395. hasNoAttributes: function(node) {
  4396. return browser.ie
  4397. ? /^<\w+\s*?>/.test(node.outerHTML)
  4398. : node.attributes.length == 0;
  4399. },
  4400. /**
  4401. * 检测节点是否是UEditor所使用的辅助节点
  4402. * @method isCustomeNode
  4403. * @private
  4404. * @param { Node } node 需要检测的节点
  4405. * @remind 辅助节点是指编辑器要完成工作临时添加的节点, 在输出的时候将会从编辑器内移除, 不会影响最终的结果。
  4406. * @return { Boolean } 给定的节点是否是一个辅助节点
  4407. */
  4408. isCustomeNode: function(node) {
  4409. return node.nodeType == 1 && node.getAttribute("_ue_custom_node_");
  4410. },
  4411. /**
  4412. * 检测节点的标签是否是给定的标签
  4413. * @method isTagNode
  4414. * @param { Node } node 需要检测的节点对象
  4415. * @param { String } tagName 标签
  4416. * @return { Boolean } 节点的标签是否是给定的标签
  4417. * @example
  4418. * ```html
  4419. * <div id="test"></div>
  4420. *
  4421. * <script>
  4422. *
  4423. * //output: true
  4424. * console.log( UE.dom.domUtils.isTagNode( document.getElementById("test"), "div" ) );
  4425. *
  4426. * </script>
  4427. * ```
  4428. */
  4429. isTagNode: function(node, tagNames) {
  4430. return (
  4431. node.nodeType == 1 &&
  4432. new RegExp("\\b" + node.tagName + "\\b", "i").test(tagNames)
  4433. );
  4434. },
  4435. /**
  4436. * 给定一个节点数组,在通过指定的过滤器过滤后, 获取其中满足过滤条件的第一个节点
  4437. * @method filterNodeList
  4438. * @param { Array } nodeList 需要过滤的节点数组
  4439. * @param { Function } fn 过滤器, 对符合条件的节点, 执行结果返回true, 反之则返回false
  4440. * @return { Node | NULL } 如果找到符合过滤条件的节点, 则返回该节点, 否则返回NULL
  4441. * @example
  4442. * ```javascript
  4443. * var divNodes = document.getElementsByTagName("div");
  4444. * divNodes = [].slice.call( divNodes, 0 );
  4445. *
  4446. * //output: null
  4447. * console.log( UE.dom.domUtils.filterNodeList( divNodes, function ( node ) {
  4448. * return node.tagName.toLowerCase() !== 'div';
  4449. * } ) );
  4450. * ```
  4451. */
  4452. /**
  4453. * 给定一个节点数组nodeList和一组标签名tagNames, 获取其中能够匹配标签名的节点集合中的第一个节点
  4454. * @method filterNodeList
  4455. * @param { Array } nodeList 需要过滤的节点数组
  4456. * @param { String } tagNames 需要匹配的标签名, 多个标签名之间用空格分割
  4457. * @return { Node | NULL } 如果找到标签名匹配的节点, 则返回该节点, 否则返回NULL
  4458. * @example
  4459. * ```javascript
  4460. * var divNodes = document.getElementsByTagName("div");
  4461. * divNodes = [].slice.call( divNodes, 0 );
  4462. *
  4463. * //output: null
  4464. * console.log( UE.dom.domUtils.filterNodeList( divNodes, 'a span' ) );
  4465. * ```
  4466. */
  4467. /**
  4468. * 给定一个节点数组,在通过指定的过滤器过滤后, 如果参数forAll为true, 则会返回所有满足过滤
  4469. * 条件的节点集合, 否则, 返回满足条件的节点集合中的第一个节点
  4470. * @method filterNodeList
  4471. * @param { Array } nodeList 需要过滤的节点数组
  4472. * @param { Function } fn 过滤器, 对符合条件的节点, 执行结果返回true, 反之则返回false
  4473. * @param { Boolean } forAll 是否返回整个节点数组, 如果该参数为false, 则返回节点集合中的第一个节点
  4474. * @return { Array | Node | NULL } 如果找到符合过滤条件的节点, 则根据参数forAll的值决定返回满足
  4475. * 过滤条件的节点数组或第一个节点, 否则返回NULL
  4476. * @example
  4477. * ```javascript
  4478. * var divNodes = document.getElementsByTagName("div");
  4479. * divNodes = [].slice.call( divNodes, 0 );
  4480. *
  4481. * //output: 3(假定有3个div)
  4482. * console.log( divNodes.length );
  4483. *
  4484. * var nodes = UE.dom.domUtils.filterNodeList( divNodes, function ( node ) {
  4485. * return node.tagName.toLowerCase() === 'div';
  4486. * }, true );
  4487. *
  4488. * //output: 3
  4489. * console.log( nodes.length );
  4490. *
  4491. * var node = UE.dom.domUtils.filterNodeList( divNodes, function ( node ) {
  4492. * return node.tagName.toLowerCase() === 'div';
  4493. * }, false );
  4494. *
  4495. * //output: div
  4496. * console.log( node.nodeName );
  4497. * ```
  4498. */
  4499. filterNodeList: function(nodelist, filter, forAll) {
  4500. var results = [];
  4501. if (!utils.isFunction(filter)) {
  4502. var str = filter;
  4503. filter = function(n) {
  4504. return (
  4505. utils.indexOf(
  4506. utils.isArray(str) ? str : str.split(" "),
  4507. n.tagName.toLowerCase()
  4508. ) != -1
  4509. );
  4510. };
  4511. }
  4512. utils.each(nodelist, function(n) {
  4513. filter(n) && results.push(n);
  4514. });
  4515. return results.length == 0
  4516. ? null
  4517. : results.length == 1 || !forAll ? results[0] : results;
  4518. },
  4519. /**
  4520. * 查询给定的range选区是否在给定的node节点内,且在该节点的最末尾
  4521. * @method isInNodeEndBoundary
  4522. * @param { UE.dom.Range } rng 需要判断的range对象, 该对象的startContainer不能为NULL
  4523. * @param node 需要检测的节点对象
  4524. * @return { Number } 如果给定的选取range对象是在node内部的最末端, 则返回1, 否则返回0
  4525. */
  4526. isInNodeEndBoundary: function(rng, node) {
  4527. var start = rng.startContainer;
  4528. if (start.nodeType == 3 && rng.startOffset != start.nodeValue.length) {
  4529. return 0;
  4530. }
  4531. if (start.nodeType == 1 && rng.startOffset != start.childNodes.length) {
  4532. return 0;
  4533. }
  4534. while (start !== node) {
  4535. if (start.nextSibling) {
  4536. return 0;
  4537. }
  4538. start = start.parentNode;
  4539. }
  4540. return 1;
  4541. },
  4542. isBoundaryNode: function(node, dir) {
  4543. var tmp;
  4544. while (!domUtils.isBody(node)) {
  4545. tmp = node;
  4546. node = node.parentNode;
  4547. if (tmp !== node[dir]) {
  4548. return false;
  4549. }
  4550. }
  4551. return true;
  4552. },
  4553. fillHtml: browser.ie11below ? "&nbsp;" : "<br/>"
  4554. });
  4555. var fillCharReg = new RegExp(domUtils.fillChar, "g");
  4556. // core/Range.js
  4557. /**
  4558. * Range封装
  4559. * @file
  4560. * @module UE.dom
  4561. * @class Range
  4562. * @since 1.2.6.1
  4563. */
  4564. /**
  4565. * dom操作封装
  4566. * @unfile
  4567. * @module UE.dom
  4568. */
  4569. /**
  4570. * Range实现类,本类是UEditor底层核心类,封装不同浏览器之间的Range操作。
  4571. * @unfile
  4572. * @module UE.dom
  4573. * @class Range
  4574. */
  4575. (function() {
  4576. var guid = 0,
  4577. fillChar = domUtils.fillChar,
  4578. fillData;
  4579. /**
  4580. * 更新range的collapse状态
  4581. * @param {Range} range range对象
  4582. */
  4583. function updateCollapse(range) {
  4584. range.collapsed =
  4585. range.startContainer &&
  4586. range.endContainer &&
  4587. range.startContainer === range.endContainer &&
  4588. range.startOffset == range.endOffset;
  4589. }
  4590. function selectOneNode(rng) {
  4591. return (
  4592. !rng.collapsed &&
  4593. rng.startContainer.nodeType == 1 &&
  4594. rng.startContainer === rng.endContainer &&
  4595. rng.endOffset - rng.startOffset == 1
  4596. );
  4597. }
  4598. function setEndPoint(toStart, node, offset, range) {
  4599. //如果node是自闭合标签要处理
  4600. if (
  4601. node.nodeType == 1 &&
  4602. (dtd.$empty[node.tagName] || dtd.$nonChild[node.tagName])
  4603. ) {
  4604. offset = domUtils.getNodeIndex(node) + (toStart ? 0 : 1);
  4605. node = node.parentNode;
  4606. }
  4607. if (toStart) {
  4608. range.startContainer = node;
  4609. range.startOffset = offset;
  4610. if (!range.endContainer) {
  4611. range.collapse(true);
  4612. }
  4613. } else {
  4614. range.endContainer = node;
  4615. range.endOffset = offset;
  4616. if (!range.startContainer) {
  4617. range.collapse(false);
  4618. }
  4619. }
  4620. updateCollapse(range);
  4621. return range;
  4622. }
  4623. function execContentsAction(range, action) {
  4624. //调整边界
  4625. //range.includeBookmark();
  4626. var start = range.startContainer,
  4627. end = range.endContainer,
  4628. startOffset = range.startOffset,
  4629. endOffset = range.endOffset,
  4630. doc = range.document,
  4631. frag = doc.createDocumentFragment(),
  4632. tmpStart,
  4633. tmpEnd;
  4634. if (start.nodeType == 1) {
  4635. start =
  4636. start.childNodes[startOffset] ||
  4637. (tmpStart = start.appendChild(doc.createTextNode("")));
  4638. }
  4639. if (end.nodeType == 1) {
  4640. end =
  4641. end.childNodes[endOffset] ||
  4642. (tmpEnd = end.appendChild(doc.createTextNode("")));
  4643. }
  4644. if (start === end && start.nodeType == 3) {
  4645. frag.appendChild(
  4646. doc.createTextNode(
  4647. start.substringData(startOffset, endOffset - startOffset)
  4648. )
  4649. );
  4650. //is not clone
  4651. if (action) {
  4652. start.deleteData(startOffset, endOffset - startOffset);
  4653. range.collapse(true);
  4654. }
  4655. return frag;
  4656. }
  4657. var current,
  4658. currentLevel,
  4659. clone = frag,
  4660. startParents = domUtils.findParents(start, true),
  4661. endParents = domUtils.findParents(end, true);
  4662. for (var i = 0; startParents[i] == endParents[i]; ) {
  4663. i++;
  4664. }
  4665. for (var j = i, si; (si = startParents[j]); j++) {
  4666. current = si.nextSibling;
  4667. if (si == start) {
  4668. if (!tmpStart) {
  4669. if (range.startContainer.nodeType == 3) {
  4670. clone.appendChild(
  4671. doc.createTextNode(start.nodeValue.slice(startOffset))
  4672. );
  4673. //is not clone
  4674. if (action) {
  4675. start.deleteData(
  4676. startOffset,
  4677. start.nodeValue.length - startOffset
  4678. );
  4679. }
  4680. } else {
  4681. clone.appendChild(!action ? start.cloneNode(true) : start);
  4682. }
  4683. }
  4684. } else {
  4685. currentLevel = si.cloneNode(false);
  4686. clone.appendChild(currentLevel);
  4687. }
  4688. while (current) {
  4689. if (current === end || current === endParents[j]) {
  4690. break;
  4691. }
  4692. si = current.nextSibling;
  4693. clone.appendChild(!action ? current.cloneNode(true) : current);
  4694. current = si;
  4695. }
  4696. clone = currentLevel;
  4697. }
  4698. clone = frag;
  4699. if (!startParents[i]) {
  4700. clone.appendChild(startParents[i - 1].cloneNode(false));
  4701. clone = clone.firstChild;
  4702. }
  4703. for (var j = i, ei; (ei = endParents[j]); j++) {
  4704. current = ei.previousSibling;
  4705. if (ei == end) {
  4706. if (!tmpEnd && range.endContainer.nodeType == 3) {
  4707. clone.appendChild(
  4708. doc.createTextNode(end.substringData(0, endOffset))
  4709. );
  4710. //is not clone
  4711. if (action) {
  4712. end.deleteData(0, endOffset);
  4713. }
  4714. }
  4715. } else {
  4716. currentLevel = ei.cloneNode(false);
  4717. clone.appendChild(currentLevel);
  4718. }
  4719. //如果两端同级,右边第一次已经被开始做了
  4720. if (j != i || !startParents[i]) {
  4721. while (current) {
  4722. if (current === start) {
  4723. break;
  4724. }
  4725. ei = current.previousSibling;
  4726. clone.insertBefore(
  4727. !action ? current.cloneNode(true) : current,
  4728. clone.firstChild
  4729. );
  4730. current = ei;
  4731. }
  4732. }
  4733. clone = currentLevel;
  4734. }
  4735. if (action) {
  4736. range
  4737. .setStartBefore(
  4738. !endParents[i]
  4739. ? endParents[i - 1]
  4740. : !startParents[i] ? startParents[i - 1] : endParents[i]
  4741. )
  4742. .collapse(true);
  4743. }
  4744. tmpStart && domUtils.remove(tmpStart);
  4745. tmpEnd && domUtils.remove(tmpEnd);
  4746. return frag;
  4747. }
  4748. /**
  4749. * 创建一个跟document绑定的空的Range实例
  4750. * @constructor
  4751. * @param { Document } document 新建的选区所属的文档对象
  4752. */
  4753. /**
  4754. * @property { Node } startContainer 当前Range的开始边界的容器节点, 可以是一个元素节点或者是文本节点
  4755. */
  4756. /**
  4757. * @property { Node } startOffset 当前Range的开始边界容器节点的偏移量, 如果是元素节点,
  4758. * 该值就是childNodes中的第几个节点, 如果是文本节点就是文本内容的第几个字符
  4759. */
  4760. /**
  4761. * @property { Node } endContainer 当前Range的结束边界的容器节点, 可以是一个元素节点或者是文本节点
  4762. */
  4763. /**
  4764. * @property { Node } endOffset 当前Range的结束边界容器节点的偏移量, 如果是元素节点,
  4765. * 该值就是childNodes中的第几个节点, 如果是文本节点就是文本内容的第几个字符
  4766. */
  4767. /**
  4768. * @property { Boolean } collapsed 当前Range是否闭合
  4769. * @default true
  4770. * @remind Range是闭合的时候, startContainer === endContainer && startOffset === endOffset
  4771. */
  4772. /**
  4773. * @property { Document } document 当前Range所属的Document对象
  4774. * @remind 不同range的的document属性可以是不同的
  4775. */
  4776. var Range = (dom.Range = function(document) {
  4777. var me = this;
  4778. me.startContainer = me.startOffset = me.endContainer = me.endOffset = null;
  4779. me.document = document;
  4780. me.collapsed = true;
  4781. });
  4782. /**
  4783. * 删除fillData
  4784. * @param doc
  4785. * @param excludeNode
  4786. */
  4787. function removeFillData(doc, excludeNode) {
  4788. try {
  4789. if (fillData && domUtils.inDoc(fillData, doc)) {
  4790. if (!fillData.nodeValue.replace(fillCharReg, "").length) {
  4791. var tmpNode = fillData.parentNode;
  4792. domUtils.remove(fillData);
  4793. while (
  4794. tmpNode &&
  4795. domUtils.isEmptyInlineElement(tmpNode) &&
  4796. //safari的contains有bug
  4797. (browser.safari
  4798. ? !(
  4799. domUtils.getPosition(tmpNode, excludeNode) &
  4800. domUtils.POSITION_CONTAINS
  4801. )
  4802. : !tmpNode.contains(excludeNode))
  4803. ) {
  4804. fillData = tmpNode.parentNode;
  4805. domUtils.remove(tmpNode);
  4806. tmpNode = fillData;
  4807. }
  4808. } else {
  4809. fillData.nodeValue = fillData.nodeValue.replace(fillCharReg, "");
  4810. }
  4811. }
  4812. } catch (e) {}
  4813. }
  4814. /**
  4815. * @param node
  4816. * @param dir
  4817. */
  4818. function mergeSibling(node, dir) {
  4819. var tmpNode;
  4820. node = node[dir];
  4821. while (node && domUtils.isFillChar(node)) {
  4822. tmpNode = node[dir];
  4823. domUtils.remove(node);
  4824. node = tmpNode;
  4825. }
  4826. }
  4827. Range.prototype = {
  4828. /**
  4829. * 克隆选区的内容到一个DocumentFragment里
  4830. * @method cloneContents
  4831. * @return { DocumentFragment | NULL } 如果选区是闭合的将返回null, 否则, 返回包含所clone内容的DocumentFragment元素
  4832. * @example
  4833. * ```html
  4834. * <body>
  4835. * <!-- 中括号表示选区 -->
  4836. * <b>x<i>x[x</i>xx]x</b>
  4837. *
  4838. * <script>
  4839. * //range是已选中的选区
  4840. * var fragment = range.cloneContents(),
  4841. * node = document.createElement("div");
  4842. *
  4843. * node.appendChild( fragment );
  4844. *
  4845. * //output: <i>x</i>xx
  4846. * console.log( node.innerHTML );
  4847. *
  4848. * </script>
  4849. * </body>
  4850. * ```
  4851. */
  4852. cloneContents: function() {
  4853. return this.collapsed ? null : execContentsAction(this, 0);
  4854. },
  4855. /**
  4856. * 删除当前选区范围中的所有内容
  4857. * @method deleteContents
  4858. * @remind 执行完该操作后, 当前Range对象变成了闭合状态
  4859. * @return { UE.dom.Range } 当前操作的Range对象
  4860. * @example
  4861. * ```html
  4862. * <body>
  4863. * <!-- 中括号表示选区 -->
  4864. * <b>x<i>x[x</i>xx]x</b>
  4865. *
  4866. * <script>
  4867. * //range是已选中的选区
  4868. * range.deleteContents();
  4869. *
  4870. * //竖线表示闭合后的选区位置
  4871. * //output: <b>x<i>x</i>|x</b>
  4872. * console.log( document.body.innerHTML );
  4873. *
  4874. * //此时, range的各项属性为
  4875. * //output: B
  4876. * console.log( range.startContainer.tagName );
  4877. * //output: 2
  4878. * console.log( range.startOffset );
  4879. * //output: B
  4880. * console.log( range.endContainer.tagName );
  4881. * //output: 2
  4882. * console.log( range.endOffset );
  4883. * //output: true
  4884. * console.log( range.collapsed );
  4885. *
  4886. * </script>
  4887. * </body>
  4888. * ```
  4889. */
  4890. deleteContents: function() {
  4891. var txt;
  4892. if (!this.collapsed) {
  4893. execContentsAction(this, 1);
  4894. }
  4895. if (browser.webkit) {
  4896. txt = this.startContainer;
  4897. if (txt.nodeType == 3 && !txt.nodeValue.length) {
  4898. this.setStartBefore(txt).collapse(true);
  4899. domUtils.remove(txt);
  4900. }
  4901. }
  4902. return this;
  4903. },
  4904. /**
  4905. * 将当前选区的内容提取到一个DocumentFragment里
  4906. * @method extractContents
  4907. * @remind 执行该操作后, 选区将变成闭合状态
  4908. * @warning 执行该操作后, 原来选区所选中的内容将从dom树上剥离出来
  4909. * @return { DocumentFragment } 返回包含所提取内容的DocumentFragment对象
  4910. * @example
  4911. * ```html
  4912. * <body>
  4913. * <!-- 中括号表示选区 -->
  4914. * <b>x<i>x[x</i>xx]x</b>
  4915. *
  4916. * <script>
  4917. * //range是已选中的选区
  4918. * var fragment = range.extractContents(),
  4919. * node = document.createElement( "div" );
  4920. *
  4921. * node.appendChild( fragment );
  4922. *
  4923. * //竖线表示闭合后的选区位置
  4924. *
  4925. * //output: <b>x<i>x</i>|x</b>
  4926. * console.log( document.body.innerHTML );
  4927. * //output: <i>x</i>xx
  4928. * console.log( node.innerHTML );
  4929. *
  4930. * //此时, range的各项属性为
  4931. * //output: B
  4932. * console.log( range.startContainer.tagName );
  4933. * //output: 2
  4934. * console.log( range.startOffset );
  4935. * //output: B
  4936. * console.log( range.endContainer.tagName );
  4937. * //output: 2
  4938. * console.log( range.endOffset );
  4939. * //output: true
  4940. * console.log( range.collapsed );
  4941. *
  4942. * </script>
  4943. * </body>
  4944. */
  4945. extractContents: function() {
  4946. return this.collapsed ? null : execContentsAction(this, 2);
  4947. },
  4948. /**
  4949. * 设置Range的开始容器节点和偏移量
  4950. * @method setStart
  4951. * @remind 如果给定的节点是元素节点,那么offset指的是其子元素中索引为offset的元素,
  4952. * 如果是文本节点,那么offset指的是其文本内容的第offset个字符
  4953. * @remind 如果提供的容器节点是一个不能包含子元素的节点, 则该选区的开始容器将被设置
  4954. * 为该节点的父节点, 此时, 其距离开始容器的偏移量也变成了该节点在其父节点
  4955. * 中的索引
  4956. * @param { Node } node 将被设为当前选区开始边界容器的节点对象
  4957. * @param { int } offset 选区的开始位置偏移量
  4958. * @return { UE.dom.Range } 当前range对象
  4959. * @example
  4960. * ```html
  4961. * <!-- 选区 -->
  4962. * <b>xxx<i>x<span>xx</span>xx<em>xx</em>xxx</i>[xxx]</b>
  4963. *
  4964. * <script>
  4965. *
  4966. * //执行操作
  4967. * range.setStart( document.getElementsByTagName("i")[0], 1 );
  4968. *
  4969. * //此时, 选区变成了
  4970. * //<b>xxx<i>x[<span>xx</span>xx<em>xx</em>xxx</i>xxx]</b>
  4971. *
  4972. * </script>
  4973. * ```
  4974. * @example
  4975. * ```html
  4976. * <!-- 选区 -->
  4977. * <b>xxx<img>[xx]x</b>
  4978. *
  4979. * <script>
  4980. *
  4981. * //执行操作
  4982. * range.setStart( document.getElementsByTagName("img")[0], 3 );
  4983. *
  4984. * //此时, 选区变成了
  4985. * //<b>xxx[<img>xx]x</b>
  4986. *
  4987. * </script>
  4988. * ```
  4989. */
  4990. setStart: function(node, offset) {
  4991. return setEndPoint(true, node, offset, this);
  4992. },
  4993. /**
  4994. * 设置Range的结束容器和偏移量
  4995. * @method setEnd
  4996. * @param { Node } node 作为当前选区结束边界容器的节点对象
  4997. * @param { int } offset 结束边界的偏移量
  4998. * @see UE.dom.Range:setStart(Node,int)
  4999. * @return { UE.dom.Range } 当前range对象
  5000. */
  5001. setEnd: function(node, offset) {
  5002. return setEndPoint(false, node, offset, this);
  5003. },
  5004. /**
  5005. * 将Range开始位置设置到node节点之后
  5006. * @method setStartAfter
  5007. * @remind 该操作将会把给定节点的父节点作为range的开始容器, 且偏移量是该节点在其父节点中的位置索引+1
  5008. * @param { Node } node 选区的开始边界将紧接着该节点之后
  5009. * @return { UE.dom.Range } 当前range对象
  5010. * @example
  5011. * ```html
  5012. * <!-- 选区示例 -->
  5013. * <b>xx<i>xxx</i><span>xx[x</span>xxx]</b>
  5014. *
  5015. * <script>
  5016. *
  5017. * //执行操作
  5018. * range.setStartAfter( document.getElementsByTagName("i")[0] );
  5019. *
  5020. * //结果选区
  5021. * //<b>xx<i>xxx</i>[<span>xxx</span>xxx]</b>
  5022. *
  5023. * </script>
  5024. * ```
  5025. */
  5026. setStartAfter: function(node) {
  5027. return this.setStart(node.parentNode, domUtils.getNodeIndex(node) + 1);
  5028. },
  5029. /**
  5030. * 将Range开始位置设置到node节点之前
  5031. * @method setStartBefore
  5032. * @remind 该操作将会把给定节点的父节点作为range的开始容器, 且偏移量是该节点在其父节点中的位置索引
  5033. * @param { Node } node 新的选区开始位置在该节点之前
  5034. * @see UE.dom.Range:setStartAfter(Node)
  5035. * @return { UE.dom.Range } 当前range对象
  5036. */
  5037. setStartBefore: function(node) {
  5038. return this.setStart(node.parentNode, domUtils.getNodeIndex(node));
  5039. },
  5040. /**
  5041. * 将Range结束位置设置到node节点之后
  5042. * @method setEndAfter
  5043. * @remind 该操作将会把给定节点的父节点作为range的结束容器, 且偏移量是该节点在其父节点中的位置索引+1
  5044. * @param { Node } node 目标节点
  5045. * @see UE.dom.Range:setStartAfter(Node)
  5046. * @return { UE.dom.Range } 当前range对象
  5047. * @example
  5048. * ```html
  5049. * <!-- 选区示例 -->
  5050. * <b>[xx<i>xxx</i><span>xx]x</span>xxx</b>
  5051. *
  5052. * <script>
  5053. *
  5054. * //执行操作
  5055. * range.setStartAfter( document.getElementsByTagName("span")[0] );
  5056. *
  5057. * //结果选区
  5058. * //<b>[xx<i>xxx</i><span>xxx</span>]xxx</b>
  5059. *
  5060. * </script>
  5061. * ```
  5062. */
  5063. setEndAfter: function(node) {
  5064. return this.setEnd(node.parentNode, domUtils.getNodeIndex(node) + 1);
  5065. },
  5066. /**
  5067. * 将Range结束位置设置到node节点之前
  5068. * @method setEndBefore
  5069. * @remind 该操作将会把给定节点的父节点作为range的结束容器, 且偏移量是该节点在其父节点中的位置索引
  5070. * @param { Node } node 目标节点
  5071. * @see UE.dom.Range:setEndAfter(Node)
  5072. * @return { UE.dom.Range } 当前range对象
  5073. */
  5074. setEndBefore: function(node) {
  5075. return this.setEnd(node.parentNode, domUtils.getNodeIndex(node));
  5076. },
  5077. /**
  5078. * 设置Range的开始位置到node节点内的第一个子节点之前
  5079. * @method setStartAtFirst
  5080. * @remind 选区的开始容器将变成给定的节点, 且偏移量为0
  5081. * @remind 如果给定的节点是元素节点, 则该节点必须是允许包含子节点的元素。
  5082. * @param { Node } node 目标节点
  5083. * @see UE.dom.Range:setStartBefore(Node)
  5084. * @return { UE.dom.Range } 当前range对象
  5085. * @example
  5086. * ```html
  5087. * <!-- 选区示例 -->
  5088. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5089. *
  5090. * <script>
  5091. *
  5092. * //执行操作
  5093. * range.setStartAtFirst( document.getElementsByTagName("i")[0] );
  5094. *
  5095. * //结果选区
  5096. * //<b>xx<i>[xxx</i><span>xx]x</span>xxx</b>
  5097. *
  5098. * </script>
  5099. * ```
  5100. */
  5101. setStartAtFirst: function(node) {
  5102. return this.setStart(node, 0);
  5103. },
  5104. /**
  5105. * 设置Range的开始位置到node节点内的最后一个节点之后
  5106. * @method setStartAtLast
  5107. * @remind 选区的开始容器将变成给定的节点, 且偏移量为该节点的子节点数
  5108. * @remind 如果给定的节点是元素节点, 则该节点必须是允许包含子节点的元素。
  5109. * @param { Node } node 目标节点
  5110. * @see UE.dom.Range:setStartAtFirst(Node)
  5111. * @return { UE.dom.Range } 当前range对象
  5112. */
  5113. setStartAtLast: function(node) {
  5114. return this.setStart(
  5115. node,
  5116. node.nodeType == 3 ? node.nodeValue.length : node.childNodes.length
  5117. );
  5118. },
  5119. /**
  5120. * 设置Range的结束位置到node节点内的第一个节点之前
  5121. * @method setEndAtFirst
  5122. * @param { Node } node 目标节点
  5123. * @remind 选区的结束容器将变成给定的节点, 且偏移量为0
  5124. * @remind node必须是一个元素节点, 且必须是允许包含子节点的元素。
  5125. * @see UE.dom.Range:setStartAtFirst(Node)
  5126. * @return { UE.dom.Range } 当前range对象
  5127. */
  5128. setEndAtFirst: function(node) {
  5129. return this.setEnd(node, 0);
  5130. },
  5131. /**
  5132. * 设置Range的结束位置到node节点内的最后一个节点之后
  5133. * @method setEndAtLast
  5134. * @param { Node } node 目标节点
  5135. * @remind 选区的结束容器将变成给定的节点, 且偏移量为该节点的子节点数量
  5136. * @remind node必须是一个元素节点, 且必须是允许包含子节点的元素。
  5137. * @see UE.dom.Range:setStartAtFirst(Node)
  5138. * @return { UE.dom.Range } 当前range对象
  5139. */
  5140. setEndAtLast: function(node) {
  5141. return this.setEnd(
  5142. node,
  5143. node.nodeType == 3 ? node.nodeValue.length : node.childNodes.length
  5144. );
  5145. },
  5146. /**
  5147. * 选中给定节点
  5148. * @method selectNode
  5149. * @remind 此时, 选区的开始容器和结束容器都是该节点的父节点, 其startOffset是该节点在父节点中的位置索引,
  5150. * 而endOffset为startOffset+1
  5151. * @param { Node } node 需要选中的节点
  5152. * @return { UE.dom.Range } 当前range对象,此时的range仅包含当前给定的节点对象
  5153. * @example
  5154. * ```html
  5155. * <!-- 选区示例 -->
  5156. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5157. *
  5158. * <script>
  5159. *
  5160. * //执行操作
  5161. * range.selectNode( document.getElementsByTagName("i")[0] );
  5162. *
  5163. * //结果选区
  5164. * //<b>xx[<i>xxx</i>]<span>xxx</span>xxx</b>
  5165. *
  5166. * </script>
  5167. * ```
  5168. */
  5169. selectNode: function(node) {
  5170. return this.setStartBefore(node).setEndAfter(node);
  5171. },
  5172. /**
  5173. * 选中给定节点内部的所有节点
  5174. * @method selectNodeContents
  5175. * @remind 此时, 选区的开始容器和结束容器都是该节点, 其startOffset为0,
  5176. * 而endOffset是该节点的子节点数。
  5177. * @param { Node } node 目标节点, 当前range将包含该节点内的所有节点
  5178. * @return { UE.dom.Range } 当前range对象, 此时range仅包含给定节点的所有子节点
  5179. * @example
  5180. * ```html
  5181. * <!-- 选区示例 -->
  5182. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5183. *
  5184. * <script>
  5185. *
  5186. * //执行操作
  5187. * range.selectNode( document.getElementsByTagName("b")[0] );
  5188. *
  5189. * //结果选区
  5190. * //<b>[xx<i>xxx</i><span>xxx</span>xxx]</b>
  5191. *
  5192. * </script>
  5193. * ```
  5194. */
  5195. selectNodeContents: function(node) {
  5196. return this.setStart(node, 0).setEndAtLast(node);
  5197. },
  5198. /**
  5199. * clone当前Range对象
  5200. * @method cloneRange
  5201. * @remind 返回的range是一个全新的range对象, 其内部所有属性与当前被clone的range相同。
  5202. * @return { UE.dom.Range } 当前range对象的一个副本
  5203. */
  5204. cloneRange: function() {
  5205. var me = this;
  5206. return new Range(me.document)
  5207. .setStart(me.startContainer, me.startOffset)
  5208. .setEnd(me.endContainer, me.endOffset);
  5209. },
  5210. /**
  5211. * 向当前选区的结束处闭合选区
  5212. * @method collapse
  5213. * @return { UE.dom.Range } 当前range对象
  5214. * @example
  5215. * ```html
  5216. * <!-- 选区示例 -->
  5217. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5218. *
  5219. * <script>
  5220. *
  5221. * //执行操作
  5222. * range.collapse();
  5223. *
  5224. * //结果选区
  5225. * //“|”表示选区已闭合
  5226. * //<b>xx<i>xxx</i><span>xx|x</span>xxx</b>
  5227. *
  5228. * </script>
  5229. * ```
  5230. */
  5231. /**
  5232. * 闭合当前选区,根据给定的toStart参数项决定是向当前选区开始处闭合还是向结束处闭合,
  5233. * 如果toStart的值为true,则向开始位置闭合, 反之,向结束位置闭合。
  5234. * @method collapse
  5235. * @param { Boolean } toStart 是否向选区开始处闭合
  5236. * @return { UE.dom.Range } 当前range对象,此时range对象处于闭合状态
  5237. * @see UE.dom.Range:collapse()
  5238. * @example
  5239. * ```html
  5240. * <!-- 选区示例 -->
  5241. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5242. *
  5243. * <script>
  5244. *
  5245. * //执行操作
  5246. * range.collapse( true );
  5247. *
  5248. * //结果选区
  5249. * //“|”表示选区已闭合
  5250. * //<b>xx<i>xxx</i><span>|xxx</span>xxx</b>
  5251. *
  5252. * </script>
  5253. * ```
  5254. */
  5255. collapse: function(toStart) {
  5256. var me = this;
  5257. if (toStart) {
  5258. me.endContainer = me.startContainer;
  5259. me.endOffset = me.startOffset;
  5260. } else {
  5261. me.startContainer = me.endContainer;
  5262. me.startOffset = me.endOffset;
  5263. }
  5264. me.collapsed = true;
  5265. return me;
  5266. },
  5267. /**
  5268. * 调整range的开始位置和结束位置,使其"收缩"到最小的位置
  5269. * @method shrinkBoundary
  5270. * @return { UE.dom.Range } 当前range对象
  5271. * @example
  5272. * ```html
  5273. * <span>xx<b>xx[</b>xxxxx]</span> => <span>xx<b>xx</b>[xxxxx]</span>
  5274. * ```
  5275. *
  5276. * @example
  5277. * ```html
  5278. * <!-- 选区示例 -->
  5279. * <b>x[xx</b><i>]xxx</i>
  5280. *
  5281. * <script>
  5282. *
  5283. * //执行收缩
  5284. * range.shrinkBoundary();
  5285. *
  5286. * //结果选区
  5287. * //<b>x[xx]</b><i>xxx</i>
  5288. * </script>
  5289. * ```
  5290. *
  5291. * @example
  5292. * ```html
  5293. * [<b><i>xxxx</i>xxxxxxx</b>] => <b><i>[xxxx</i>xxxxxxx]</b>
  5294. * ```
  5295. */
  5296. /**
  5297. * 调整range的开始位置和结束位置,使其"收缩"到最小的位置,
  5298. * 如果ignoreEnd的值为true,则忽略对结束位置的调整
  5299. * @method shrinkBoundary
  5300. * @param { Boolean } ignoreEnd 是否忽略对结束位置的调整
  5301. * @return { UE.dom.Range } 当前range对象
  5302. * @see UE.dom.domUtils.Range:shrinkBoundary()
  5303. */
  5304. shrinkBoundary: function(ignoreEnd) {
  5305. var me = this,
  5306. child,
  5307. collapsed = me.collapsed;
  5308. function check(node) {
  5309. return (
  5310. node.nodeType == 1 &&
  5311. !domUtils.isBookmarkNode(node) &&
  5312. !dtd.$empty[node.tagName] &&
  5313. !dtd.$nonChild[node.tagName]
  5314. );
  5315. }
  5316. while (
  5317. me.startContainer.nodeType == 1 && //是element
  5318. (child = me.startContainer.childNodes[me.startOffset]) && //子节点也是element
  5319. check(child)
  5320. ) {
  5321. me.setStart(child, 0);
  5322. }
  5323. if (collapsed) {
  5324. return me.collapse(true);
  5325. }
  5326. if (!ignoreEnd) {
  5327. while (
  5328. me.endContainer.nodeType == 1 && //是element
  5329. me.endOffset > 0 && //如果是空元素就退出 endOffset=0那么endOffst-1为负值,childNodes[endOffset]报错
  5330. (child = me.endContainer.childNodes[me.endOffset - 1]) && //子节点也是element
  5331. check(child)
  5332. ) {
  5333. me.setEnd(child, child.childNodes.length);
  5334. }
  5335. }
  5336. return me;
  5337. },
  5338. /**
  5339. * 获取离当前选区内包含的所有节点最近的公共祖先节点,
  5340. * @method getCommonAncestor
  5341. * @remind 返回的公共祖先节点一定不是range自身的容器节点, 但有可能是一个文本节点
  5342. * @return { Node } 当前range对象内所有节点的公共祖先节点
  5343. * @example
  5344. * ```html
  5345. * //选区示例
  5346. * <span>xxx<b>x[x<em>xx]x</em>xxx</b>xx</span>
  5347. * <script>
  5348. *
  5349. * var node = range.getCommonAncestor();
  5350. *
  5351. * //公共祖先节点是: b节点
  5352. * //输出: B
  5353. * console.log(node.tagName);
  5354. *
  5355. * </script>
  5356. * ```
  5357. */
  5358. /**
  5359. * 获取当前选区所包含的所有节点的公共祖先节点, 可以根据给定的参数 includeSelf 决定获取到
  5360. * 的公共祖先节点是否可以是当前选区的startContainer或endContainer节点, 如果 includeSelf
  5361. * 的取值为true, 则返回的节点可以是自身的容器节点, 否则, 则不能是容器节点
  5362. * @method getCommonAncestor
  5363. * @param { Boolean } includeSelf 是否允许获取到的公共祖先节点是当前range对象的容器节点
  5364. * @return { Node } 当前range对象内所有节点的公共祖先节点
  5365. * @see UE.dom.Range:getCommonAncestor()
  5366. * @example
  5367. * ```html
  5368. * <body>
  5369. *
  5370. * <!-- 选区示例 -->
  5371. * <b>xxx<i>xxxx<span>xx[x</span>xx]x</i>xxxxxxx</b>
  5372. *
  5373. * <script>
  5374. *
  5375. * var node = range.getCommonAncestor( false );
  5376. *
  5377. * //这里的公共祖先节点是B而不是I, 是因为参数限制了获取到的节点不能是容器节点
  5378. * //output: B
  5379. * console.log( node.tagName );
  5380. *
  5381. * </script>
  5382. *
  5383. * </body>
  5384. * ```
  5385. */
  5386. /**
  5387. * 获取当前选区所包含的所有节点的公共祖先节点, 可以根据给定的参数 includeSelf 决定获取到
  5388. * 的公共祖先节点是否可以是当前选区的startContainer或endContainer节点, 如果 includeSelf
  5389. * 的取值为true, 则返回的节点可以是自身的容器节点, 否则, 则不能是容器节点; 同时可以根据
  5390. * ignoreTextNode 参数的取值决定是否忽略类型为文本节点的祖先节点。
  5391. * @method getCommonAncestor
  5392. * @param { Boolean } includeSelf 是否允许获取到的公共祖先节点是当前range对象的容器节点
  5393. * @param { Boolean } ignoreTextNode 获取祖先节点的过程中是否忽略类型为文本节点的祖先节点
  5394. * @return { Node } 当前range对象内所有节点的公共祖先节点
  5395. * @see UE.dom.Range:getCommonAncestor()
  5396. * @see UE.dom.Range:getCommonAncestor(Boolean)
  5397. * @example
  5398. * ```html
  5399. * <body>
  5400. *
  5401. * <!-- 选区示例 -->
  5402. * <b>xxx<i>xxxx<span>x[x]x</span>xxx</i>xxxxxxx</b>
  5403. *
  5404. * <script>
  5405. *
  5406. * var node = range.getCommonAncestor( true, false );
  5407. *
  5408. * //output: SPAN
  5409. * console.log( node.tagName );
  5410. *
  5411. * </script>
  5412. *
  5413. * </body>
  5414. * ```
  5415. */
  5416. getCommonAncestor: function(includeSelf, ignoreTextNode) {
  5417. var me = this,
  5418. start = me.startContainer,
  5419. end = me.endContainer;
  5420. if (start === end) {
  5421. if (includeSelf && selectOneNode(this)) {
  5422. start = start.childNodes[me.startOffset];
  5423. if (start.nodeType == 1) return start;
  5424. }
  5425. //只有在上来就相等的情况下才会出现是文本的情况
  5426. return ignoreTextNode && start.nodeType == 3 ? start.parentNode : start;
  5427. }
  5428. return domUtils.getCommonAncestor(start, end);
  5429. },
  5430. /**
  5431. * 调整当前Range的开始和结束边界容器,如果是容器节点是文本节点,就调整到包含该文本节点的父节点上
  5432. * @method trimBoundary
  5433. * @remind 该操作有可能会引起文本节点被切开
  5434. * @return { UE.dom.Range } 当前range对象
  5435. * @example
  5436. * ```html
  5437. *
  5438. * //选区示例
  5439. * <b>xxx<i>[xxxxx]</i>xxx</b>
  5440. *
  5441. * <script>
  5442. * //未调整前, 选区的开始容器和结束都是文本节点
  5443. * //执行调整
  5444. * range.trimBoundary();
  5445. *
  5446. * //调整之后, 容器节点变成了i节点
  5447. * //<b>xxx[<i>xxxxx</i>]xxx</b>
  5448. * </script>
  5449. * ```
  5450. */
  5451. /**
  5452. * 调整当前Range的开始和结束边界容器,如果是容器节点是文本节点,就调整到包含该文本节点的父节点上,
  5453. * 可以根据 ignoreEnd 参数的值决定是否调整对结束边界的调整
  5454. * @method trimBoundary
  5455. * @param { Boolean } ignoreEnd 是否忽略对结束边界的调整
  5456. * @return { UE.dom.Range } 当前range对象
  5457. * @example
  5458. * ```html
  5459. *
  5460. * //选区示例
  5461. * <b>xxx<i>[xxxxx]</i>xxx</b>
  5462. *
  5463. * <script>
  5464. * //未调整前, 选区的开始容器和结束都是文本节点
  5465. * //执行调整
  5466. * range.trimBoundary( true );
  5467. *
  5468. * //调整之后, 开始容器节点变成了i节点
  5469. * //但是, 结束容器没有发生变化
  5470. * //<b>xxx[<i>xxxxx]</i>xxx</b>
  5471. * </script>
  5472. * ```
  5473. */
  5474. trimBoundary: function(ignoreEnd) {
  5475. this.txtToElmBoundary();
  5476. var start = this.startContainer,
  5477. offset = this.startOffset,
  5478. collapsed = this.collapsed,
  5479. end = this.endContainer;
  5480. if (start.nodeType == 3) {
  5481. if (offset == 0) {
  5482. this.setStartBefore(start);
  5483. } else {
  5484. if (offset >= start.nodeValue.length) {
  5485. this.setStartAfter(start);
  5486. } else {
  5487. var textNode = domUtils.split(start, offset);
  5488. //跟新结束边界
  5489. if (start === end) {
  5490. this.setEnd(textNode, this.endOffset - offset);
  5491. } else if (start.parentNode === end) {
  5492. this.endOffset += 1;
  5493. }
  5494. this.setStartBefore(textNode);
  5495. }
  5496. }
  5497. if (collapsed) {
  5498. return this.collapse(true);
  5499. }
  5500. }
  5501. if (!ignoreEnd) {
  5502. offset = this.endOffset;
  5503. end = this.endContainer;
  5504. if (end.nodeType == 3) {
  5505. if (offset == 0) {
  5506. this.setEndBefore(end);
  5507. } else {
  5508. offset < end.nodeValue.length && domUtils.split(end, offset);
  5509. this.setEndAfter(end);
  5510. }
  5511. }
  5512. }
  5513. return this;
  5514. },
  5515. /**
  5516. * 如果选区在文本的边界上,就扩展选区到文本的父节点上, 如果当前选区是闭合的, 则什么也不做
  5517. * @method txtToElmBoundary
  5518. * @remind 该操作不会修改dom节点
  5519. * @return { UE.dom.Range } 当前range对象
  5520. */
  5521. /**
  5522. * 如果选区在文本的边界上,就扩展选区到文本的父节点上, 如果当前选区是闭合的, 则根据参数项
  5523. * ignoreCollapsed 的值决定是否执行该调整
  5524. * @method txtToElmBoundary
  5525. * @param { Boolean } ignoreCollapsed 是否忽略选区的闭合状态, 如果该参数取值为true, 则
  5526. * 不论选区是否闭合, 都会执行该操作, 反之, 则不会对闭合的选区执行该操作
  5527. * @return { UE.dom.Range } 当前range对象
  5528. */
  5529. txtToElmBoundary: function(ignoreCollapsed) {
  5530. function adjust(r, c) {
  5531. var container = r[c + "Container"],
  5532. offset = r[c + "Offset"];
  5533. if (container.nodeType == 3) {
  5534. if (!offset) {
  5535. r[
  5536. "set" +
  5537. c.replace(/(\w)/, function(a) {
  5538. return a.toUpperCase();
  5539. }) +
  5540. "Before"
  5541. ](container);
  5542. } else if (offset >= container.nodeValue.length) {
  5543. r[
  5544. "set" +
  5545. c.replace(/(\w)/, function(a) {
  5546. return a.toUpperCase();
  5547. }) +
  5548. "After"
  5549. ](container);
  5550. }
  5551. }
  5552. }
  5553. if (ignoreCollapsed || !this.collapsed) {
  5554. adjust(this, "start");
  5555. adjust(this, "end");
  5556. }
  5557. return this;
  5558. },
  5559. /**
  5560. * 在当前选区的开始位置前插入节点,新插入的节点会被该range包含
  5561. * @method insertNode
  5562. * @param { Node } node 需要插入的节点
  5563. * @remind 插入的节点可以是一个DocumentFragment依次插入多个节点
  5564. * @return { UE.dom.Range } 当前range对象
  5565. */
  5566. insertNode: function(node) {
  5567. var first = node,
  5568. length = 1;
  5569. if (node.nodeType == 11) {
  5570. first = node.firstChild;
  5571. length = node.childNodes.length;
  5572. }
  5573. this.trimBoundary(true);
  5574. var start = this.startContainer,
  5575. offset = this.startOffset;
  5576. var nextNode = start.childNodes[offset];
  5577. if (nextNode) {
  5578. start.insertBefore(node, nextNode);
  5579. } else {
  5580. start.appendChild(node);
  5581. }
  5582. if (first.parentNode === this.endContainer) {
  5583. this.endOffset = this.endOffset + length;
  5584. }
  5585. return this.setStartBefore(first);
  5586. },
  5587. /**
  5588. * 闭合选区到当前选区的开始位置, 并且定位光标到闭合后的位置
  5589. * @method setCursor
  5590. * @return { UE.dom.Range } 当前range对象
  5591. * @see UE.dom.Range:collapse()
  5592. */
  5593. /**
  5594. * 闭合选区,可以根据参数toEnd的值控制选区是向前闭合还是向后闭合, 并且定位光标到闭合后的位置。
  5595. * @method setCursor
  5596. * @param { Boolean } toEnd 是否向后闭合, 如果为true, 则闭合选区时, 将向结束容器方向闭合,
  5597. * 反之,则向开始容器方向闭合
  5598. * @return { UE.dom.Range } 当前range对象
  5599. * @see UE.dom.Range:collapse(Boolean)
  5600. */
  5601. setCursor: function(toEnd, noFillData) {
  5602. return this.collapse(!toEnd).select(noFillData);
  5603. },
  5604. /**
  5605. * 创建当前range的一个书签,记录下当前range的位置,方便当dom树改变时,还能找回原来的选区位置
  5606. * @method createBookmark
  5607. * @param { Boolean } serialize 控制返回的标记位置是对当前位置的引用还是ID,如果该值为true,则
  5608. * 返回标记位置的ID, 反之则返回标记位置节点的引用
  5609. * @return { Object } 返回一个书签记录键值对, 其包含的key有: start => 开始标记的ID或者引用,
  5610. * end => 结束标记的ID或引用, id => 当前标记的类型, 如果为true,则表示
  5611. * 返回的记录的类型为ID, 反之则为引用
  5612. */
  5613. createBookmark: function(serialize, same) {
  5614. var endNode,
  5615. startNode = this.document.createElement("span");
  5616. startNode.style.cssText = "display:none;line-height:0px;";
  5617. startNode.appendChild(this.document.createTextNode("\u200D"));
  5618. startNode.id = "_baidu_bookmark_start_" + (same ? "" : guid++);
  5619. if (!this.collapsed) {
  5620. endNode = startNode.cloneNode(true);
  5621. endNode.id = "_baidu_bookmark_end_" + (same ? "" : guid++);
  5622. }
  5623. this.insertNode(startNode);
  5624. if (endNode) {
  5625. this.collapse().insertNode(endNode).setEndBefore(endNode);
  5626. }
  5627. this.setStartAfter(startNode);
  5628. return {
  5629. start: serialize ? startNode.id : startNode,
  5630. end: endNode ? (serialize ? endNode.id : endNode) : null,
  5631. id: serialize
  5632. };
  5633. },
  5634. /**
  5635. * 调整当前range的边界到书签位置,并删除该书签对象所标记的位置内的节点
  5636. * @method moveToBookmark
  5637. * @param { BookMark } bookmark createBookmark所创建的标签对象
  5638. * @return { UE.dom.Range } 当前range对象
  5639. * @see UE.dom.Range:createBookmark(Boolean)
  5640. */
  5641. moveToBookmark: function(bookmark) {
  5642. var start = bookmark.id
  5643. ? this.document.getElementById(bookmark.start)
  5644. : bookmark.start,
  5645. end = bookmark.end && bookmark.id
  5646. ? this.document.getElementById(bookmark.end)
  5647. : bookmark.end;
  5648. this.setStartBefore(start);
  5649. domUtils.remove(start);
  5650. if (end) {
  5651. this.setEndBefore(end);
  5652. domUtils.remove(end);
  5653. } else {
  5654. this.collapse(true);
  5655. }
  5656. return this;
  5657. },
  5658. /**
  5659. * 调整range的边界,使其"放大"到最近的父节点
  5660. * @method enlarge
  5661. * @remind 会引起选区的变化
  5662. * @return { UE.dom.Range } 当前range对象
  5663. */
  5664. /**
  5665. * 调整range的边界,使其"放大"到最近的父节点,根据参数 toBlock 的取值, 可以
  5666. * 要求扩大之后的父节点是block节点
  5667. * @method enlarge
  5668. * @param { Boolean } toBlock 是否要求扩大之后的父节点必须是block节点
  5669. * @return { UE.dom.Range } 当前range对象
  5670. */
  5671. enlarge: function(toBlock, stopFn) {
  5672. var isBody = domUtils.isBody,
  5673. pre,
  5674. node,
  5675. tmp = this.document.createTextNode("");
  5676. if (toBlock) {
  5677. node = this.startContainer;
  5678. if (node.nodeType == 1) {
  5679. if (node.childNodes[this.startOffset]) {
  5680. pre = node = node.childNodes[this.startOffset];
  5681. } else {
  5682. node.appendChild(tmp);
  5683. pre = node = tmp;
  5684. }
  5685. } else {
  5686. pre = node;
  5687. }
  5688. while (1) {
  5689. if (domUtils.isBlockElm(node)) {
  5690. node = pre;
  5691. while ((pre = node.previousSibling) && !domUtils.isBlockElm(pre)) {
  5692. node = pre;
  5693. }
  5694. this.setStartBefore(node);
  5695. break;
  5696. }
  5697. pre = node;
  5698. node = node.parentNode;
  5699. }
  5700. node = this.endContainer;
  5701. if (node.nodeType == 1) {
  5702. if ((pre = node.childNodes[this.endOffset])) {
  5703. node.insertBefore(tmp, pre);
  5704. } else {
  5705. node.appendChild(tmp);
  5706. }
  5707. pre = node = tmp;
  5708. } else {
  5709. pre = node;
  5710. }
  5711. while (1) {
  5712. if (domUtils.isBlockElm(node)) {
  5713. node = pre;
  5714. while ((pre = node.nextSibling) && !domUtils.isBlockElm(pre)) {
  5715. node = pre;
  5716. }
  5717. this.setEndAfter(node);
  5718. break;
  5719. }
  5720. pre = node;
  5721. node = node.parentNode;
  5722. }
  5723. if (tmp.parentNode === this.endContainer) {
  5724. this.endOffset--;
  5725. }
  5726. domUtils.remove(tmp);
  5727. }
  5728. // 扩展边界到最大
  5729. if (!this.collapsed) {
  5730. while (this.startOffset == 0) {
  5731. if (stopFn && stopFn(this.startContainer)) {
  5732. break;
  5733. }
  5734. if (isBody(this.startContainer)) {
  5735. break;
  5736. }
  5737. this.setStartBefore(this.startContainer);
  5738. }
  5739. while (
  5740. this.endOffset ==
  5741. (this.endContainer.nodeType == 1
  5742. ? this.endContainer.childNodes.length
  5743. : this.endContainer.nodeValue.length)
  5744. ) {
  5745. if (stopFn && stopFn(this.endContainer)) {
  5746. break;
  5747. }
  5748. if (isBody(this.endContainer)) {
  5749. break;
  5750. }
  5751. this.setEndAfter(this.endContainer);
  5752. }
  5753. }
  5754. return this;
  5755. },
  5756. enlargeToBlockElm: function(ignoreEnd) {
  5757. while (!domUtils.isBlockElm(this.startContainer)) {
  5758. this.setStartBefore(this.startContainer);
  5759. }
  5760. if (!ignoreEnd) {
  5761. while (!domUtils.isBlockElm(this.endContainer)) {
  5762. this.setEndAfter(this.endContainer);
  5763. }
  5764. }
  5765. return this;
  5766. },
  5767. /**
  5768. * 调整Range的边界,使其"缩小"到最合适的位置
  5769. * @method adjustmentBoundary
  5770. * @return { UE.dom.Range } 当前range对象
  5771. * @see UE.dom.Range:shrinkBoundary()
  5772. */
  5773. adjustmentBoundary: function() {
  5774. if (!this.collapsed) {
  5775. while (
  5776. !domUtils.isBody(this.startContainer) &&
  5777. this.startOffset ==
  5778. this.startContainer[
  5779. this.startContainer.nodeType == 3 ? "nodeValue" : "childNodes"
  5780. ].length &&
  5781. this.startContainer[
  5782. this.startContainer.nodeType == 3 ? "nodeValue" : "childNodes"
  5783. ].length
  5784. ) {
  5785. this.setStartAfter(this.startContainer);
  5786. }
  5787. while (
  5788. !domUtils.isBody(this.endContainer) &&
  5789. !this.endOffset &&
  5790. this.endContainer[
  5791. this.endContainer.nodeType == 3 ? "nodeValue" : "childNodes"
  5792. ].length
  5793. ) {
  5794. this.setEndBefore(this.endContainer);
  5795. }
  5796. }
  5797. return this;
  5798. },
  5799. /**
  5800. * 给range选区中的内容添加给定的inline标签
  5801. * @method applyInlineStyle
  5802. * @param { String } tagName 需要添加的标签名
  5803. * @example
  5804. * ```html
  5805. * <p>xxxx[xxxx]x</p> ==> range.applyInlineStyle("strong") ==> <p>xxxx[<strong>xxxx</strong>]x</p>
  5806. * ```
  5807. */
  5808. /**
  5809. * 给range选区中的内容添加给定的inline标签, 并且为标签附加上一些初始化属性。
  5810. * @method applyInlineStyle
  5811. * @param { String } tagName 需要添加的标签名
  5812. * @param { Object } attrs 跟随新添加的标签的属性
  5813. * @return { UE.dom.Range } 当前选区
  5814. * @example
  5815. * ```html
  5816. * <p>xxxx[xxxx]x</p>
  5817. *
  5818. * ==>
  5819. *
  5820. * <!-- 执行操作 -->
  5821. * range.applyInlineStyle("strong",{"style":"font-size:12px"})
  5822. *
  5823. * ==>
  5824. *
  5825. * <p>xxxx[<strong style="font-size:12px">xxxx</strong>]x</p>
  5826. * ```
  5827. */
  5828. applyInlineStyle: function(tagName, attrs, list) {
  5829. if (this.collapsed) return this;
  5830. this.trimBoundary()
  5831. .enlarge(false, function(node) {
  5832. return node.nodeType == 1 && domUtils.isBlockElm(node);
  5833. })
  5834. .adjustmentBoundary();
  5835. var bookmark = this.createBookmark(),
  5836. end = bookmark.end,
  5837. filterFn = function(node) {
  5838. return node.nodeType == 1
  5839. ? node.tagName.toLowerCase() != "br"
  5840. : !domUtils.isWhitespace(node);
  5841. },
  5842. current = domUtils.getNextDomNode(bookmark.start, false, filterFn),
  5843. node,
  5844. pre,
  5845. range = this.cloneRange();
  5846. while (
  5847. current &&
  5848. domUtils.getPosition(current, end) & domUtils.POSITION_PRECEDING
  5849. ) {
  5850. if (current.nodeType == 3 || dtd[tagName][current.tagName]) {
  5851. range.setStartBefore(current);
  5852. node = current;
  5853. while (
  5854. node &&
  5855. (node.nodeType == 3 || dtd[tagName][node.tagName]) &&
  5856. node !== end
  5857. ) {
  5858. pre = node;
  5859. node = domUtils.getNextDomNode(
  5860. node,
  5861. node.nodeType == 1,
  5862. null,
  5863. function(parent) {
  5864. return dtd[tagName][parent.tagName];
  5865. }
  5866. );
  5867. }
  5868. var frag = range.setEndAfter(pre).extractContents(),
  5869. elm;
  5870. if (list && list.length > 0) {
  5871. var level, top;
  5872. top = level = list[0].cloneNode(false);
  5873. for (var i = 1, ci; (ci = list[i++]); ) {
  5874. level.appendChild(ci.cloneNode(false));
  5875. level = level.firstChild;
  5876. }
  5877. elm = level;
  5878. } else {
  5879. elm = range.document.createElement(tagName);
  5880. }
  5881. if (attrs) {
  5882. domUtils.setAttributes(elm, attrs);
  5883. }
  5884. elm.appendChild(frag);
  5885. //针对嵌套span的全局样式指定,做容错处理
  5886. if (elm.tagName == "SPAN" && attrs && attrs.style) {
  5887. utils.each(elm.getElementsByTagName("span"), function(s) {
  5888. s.style.cssText = s.style.cssText + ";" + attrs.style;
  5889. });
  5890. }
  5891. range.insertNode(list ? top : elm);
  5892. //处理下滑线在a上的情况
  5893. var aNode;
  5894. if (
  5895. tagName == "span" &&
  5896. attrs.style &&
  5897. /text\-decoration/.test(attrs.style) &&
  5898. (aNode = domUtils.findParentByTagName(elm, "a", true))
  5899. ) {
  5900. domUtils.setAttributes(aNode, attrs);
  5901. domUtils.remove(elm, true);
  5902. elm = aNode;
  5903. } else {
  5904. domUtils.mergeSibling(elm);
  5905. domUtils.clearEmptySibling(elm);
  5906. }
  5907. //去除子节点相同的
  5908. domUtils.mergeChild(elm, attrs);
  5909. current = domUtils.getNextDomNode(elm, false, filterFn);
  5910. domUtils.mergeToParent(elm);
  5911. if (node === end) {
  5912. break;
  5913. }
  5914. } else {
  5915. current = domUtils.getNextDomNode(current, true, filterFn);
  5916. }
  5917. }
  5918. return this.moveToBookmark(bookmark);
  5919. },
  5920. /**
  5921. * 移除当前选区内指定的inline标签,但保留其中的内容
  5922. * @method removeInlineStyle
  5923. * @param { String } tagName 需要移除的标签名
  5924. * @return { UE.dom.Range } 当前的range对象
  5925. * @example
  5926. * ```html
  5927. * xx[x<span>xxx<em>yyy</em>zz]z</span> => range.removeInlineStyle(["em"]) => xx[x<span>xxxyyyzz]z</span>
  5928. * ```
  5929. */
  5930. /**
  5931. * 移除当前选区内指定的一组inline标签,但保留其中的内容
  5932. * @method removeInlineStyle
  5933. * @param { Array } tagNameArr 需要移除的标签名的数组
  5934. * @return { UE.dom.Range } 当前的range对象
  5935. * @see UE.dom.Range:removeInlineStyle(String)
  5936. */
  5937. removeInlineStyle: function(tagNames) {
  5938. if (this.collapsed) return this;
  5939. tagNames = utils.isArray(tagNames) ? tagNames : [tagNames];
  5940. this.shrinkBoundary().adjustmentBoundary();
  5941. var start = this.startContainer,
  5942. end = this.endContainer;
  5943. while (1) {
  5944. if (start.nodeType == 1) {
  5945. if (utils.indexOf(tagNames, start.tagName.toLowerCase()) > -1) {
  5946. break;
  5947. }
  5948. if (start.tagName.toLowerCase() == "body") {
  5949. start = null;
  5950. break;
  5951. }
  5952. }
  5953. start = start.parentNode;
  5954. }
  5955. while (1) {
  5956. if (end.nodeType == 1) {
  5957. if (utils.indexOf(tagNames, end.tagName.toLowerCase()) > -1) {
  5958. break;
  5959. }
  5960. if (end.tagName.toLowerCase() == "body") {
  5961. end = null;
  5962. break;
  5963. }
  5964. }
  5965. end = end.parentNode;
  5966. }
  5967. var bookmark = this.createBookmark(),
  5968. frag,
  5969. tmpRange;
  5970. if (start) {
  5971. tmpRange = this.cloneRange()
  5972. .setEndBefore(bookmark.start)
  5973. .setStartBefore(start);
  5974. frag = tmpRange.extractContents();
  5975. tmpRange.insertNode(frag);
  5976. domUtils.clearEmptySibling(start, true);
  5977. start.parentNode.insertBefore(bookmark.start, start);
  5978. }
  5979. if (end) {
  5980. tmpRange = this.cloneRange()
  5981. .setStartAfter(bookmark.end)
  5982. .setEndAfter(end);
  5983. frag = tmpRange.extractContents();
  5984. tmpRange.insertNode(frag);
  5985. domUtils.clearEmptySibling(end, false, true);
  5986. end.parentNode.insertBefore(bookmark.end, end.nextSibling);
  5987. }
  5988. var current = domUtils.getNextDomNode(bookmark.start, false, function(
  5989. node
  5990. ) {
  5991. return node.nodeType == 1;
  5992. }),
  5993. next;
  5994. while (current && current !== bookmark.end) {
  5995. next = domUtils.getNextDomNode(current, true, function(node) {
  5996. return node.nodeType == 1;
  5997. });
  5998. if (utils.indexOf(tagNames, current.tagName.toLowerCase()) > -1) {
  5999. domUtils.remove(current, true);
  6000. }
  6001. current = next;
  6002. }
  6003. return this.moveToBookmark(bookmark);
  6004. },
  6005. /**
  6006. * 获取当前选中的自闭合的节点
  6007. * @method getClosedNode
  6008. * @return { Node | NULL } 如果当前选中的是自闭合节点, 则返回该节点, 否则返回NULL
  6009. */
  6010. getClosedNode: function() {
  6011. var node;
  6012. if (!this.collapsed) {
  6013. var range = this.cloneRange().adjustmentBoundary().shrinkBoundary();
  6014. if (selectOneNode(range)) {
  6015. var child = range.startContainer.childNodes[range.startOffset];
  6016. if (
  6017. child &&
  6018. child.nodeType == 1 &&
  6019. (dtd.$empty[child.tagName] || dtd.$nonChild[child.tagName])
  6020. ) {
  6021. node = child;
  6022. }
  6023. }
  6024. }
  6025. return node;
  6026. },
  6027. /**
  6028. * 在页面上高亮range所表示的选区
  6029. * @method select
  6030. * @return { UE.dom.Range } 返回当前Range对象
  6031. */
  6032. //这里不区分ie9以上,trace:3824
  6033. select: browser.ie
  6034. ? function(noFillData, textRange) {
  6035. var nativeRange;
  6036. if (!this.collapsed) this.shrinkBoundary();
  6037. var node = this.getClosedNode();
  6038. if (node && !textRange) {
  6039. try {
  6040. nativeRange = this.document.body.createControlRange();
  6041. nativeRange.addElement(node);
  6042. nativeRange.select();
  6043. } catch (e) {}
  6044. return this;
  6045. }
  6046. var bookmark = this.createBookmark(),
  6047. start = bookmark.start,
  6048. end;
  6049. nativeRange = this.document.body.createTextRange();
  6050. nativeRange.moveToElementText(start);
  6051. nativeRange.moveStart("character", 1);
  6052. if (!this.collapsed) {
  6053. var nativeRangeEnd = this.document.body.createTextRange();
  6054. end = bookmark.end;
  6055. nativeRangeEnd.moveToElementText(end);
  6056. nativeRange.setEndPoint("EndToEnd", nativeRangeEnd);
  6057. } else {
  6058. if (!noFillData && this.startContainer.nodeType != 3) {
  6059. //使用<span>|x<span>固定住光标
  6060. var tmpText = this.document.createTextNode(fillChar),
  6061. tmp = this.document.createElement("span");
  6062. tmp.appendChild(this.document.createTextNode(fillChar));
  6063. start.parentNode.insertBefore(tmp, start);
  6064. start.parentNode.insertBefore(tmpText, start);
  6065. //当点b,i,u时,不能清除i上边的b
  6066. removeFillData(this.document, tmpText);
  6067. fillData = tmpText;
  6068. mergeSibling(tmp, "previousSibling");
  6069. mergeSibling(start, "nextSibling");
  6070. nativeRange.moveStart("character", -1);
  6071. nativeRange.collapse(true);
  6072. }
  6073. }
  6074. this.moveToBookmark(bookmark);
  6075. tmp && domUtils.remove(tmp);
  6076. //IE在隐藏状态下不支持range操作,catch一下
  6077. try {
  6078. nativeRange.select();
  6079. } catch (e) {}
  6080. return this;
  6081. }
  6082. : function(notInsertFillData) {
  6083. function checkOffset(rng) {
  6084. function check(node, offset, dir) {
  6085. if (node.nodeType == 3 && node.nodeValue.length < offset) {
  6086. rng[dir + "Offset"] = node.nodeValue.length;
  6087. }
  6088. }
  6089. check(rng.startContainer, rng.startOffset, "start");
  6090. check(rng.endContainer, rng.endOffset, "end");
  6091. }
  6092. var win = domUtils.getWindow(this.document),
  6093. sel = win.getSelection(),
  6094. txtNode;
  6095. //FF下关闭自动长高时滚动条在关闭dialog时会跳
  6096. //ff下如果不body.focus将不能定位闭合光标到编辑器内
  6097. browser.gecko ? this.document.body.focus() : win.focus();
  6098. if (sel) {
  6099. sel.removeAllRanges();
  6100. // trace:870 chrome/safari后边是br对于闭合得range不能定位 所以去掉了判断
  6101. // this.startContainer.nodeType != 3 &&! ((child = this.startContainer.childNodes[this.startOffset]) && child.nodeType == 1 && child.tagName == 'BR'
  6102. if (this.collapsed && !notInsertFillData) {
  6103. // //opear如果没有节点接着,原生的不能够定位,不能在body的第一级插入空白节点
  6104. // if (notInsertFillData && browser.opera && !domUtils.isBody(this.startContainer) && this.startContainer.nodeType == 1) {
  6105. // var tmp = this.document.createTextNode('');
  6106. // this.insertNode(tmp).setStart(tmp, 0).collapse(true);
  6107. // }
  6108. //
  6109. //处理光标落在文本节点的情况
  6110. //处理以下的情况
  6111. //<b>|xxxx</b>
  6112. //<b>xxxx</b>|xxxx
  6113. //xxxx<b>|</b>
  6114. var start = this.startContainer,
  6115. child = start;
  6116. if (start.nodeType == 1) {
  6117. child = start.childNodes[this.startOffset];
  6118. }
  6119. if (
  6120. !(start.nodeType == 3 && this.startOffset) &&
  6121. (child
  6122. ? !child.previousSibling ||
  6123. child.previousSibling.nodeType != 3
  6124. : !start.lastChild || start.lastChild.nodeType != 3)
  6125. ) {
  6126. txtNode = this.document.createTextNode(fillChar);
  6127. //跟着前边走
  6128. this.insertNode(txtNode);
  6129. removeFillData(this.document, txtNode);
  6130. mergeSibling(txtNode, "previousSibling");
  6131. mergeSibling(txtNode, "nextSibling");
  6132. fillData = txtNode;
  6133. this.setStart(txtNode, browser.webkit ? 1 : 0).collapse(true);
  6134. }
  6135. }
  6136. var nativeRange = this.document.createRange();
  6137. if (
  6138. this.collapsed &&
  6139. browser.opera &&
  6140. this.startContainer.nodeType == 1
  6141. ) {
  6142. var child = this.startContainer.childNodes[this.startOffset];
  6143. if (!child) {
  6144. //往前靠拢
  6145. child = this.startContainer.lastChild;
  6146. if (child && domUtils.isBr(child)) {
  6147. this.setStartBefore(child).collapse(true);
  6148. }
  6149. } else {
  6150. //向后靠拢
  6151. while (child && domUtils.isBlockElm(child)) {
  6152. if (child.nodeType == 1 && child.childNodes[0]) {
  6153. child = child.childNodes[0];
  6154. } else {
  6155. break;
  6156. }
  6157. }
  6158. child && this.setStartBefore(child).collapse(true);
  6159. }
  6160. }
  6161. //是createAddress最后一位算的不准,现在这里进行微调
  6162. checkOffset(this);
  6163. nativeRange.setStart(this.startContainer, this.startOffset);
  6164. nativeRange.setEnd(this.endContainer, this.endOffset);
  6165. sel.addRange(nativeRange);
  6166. }
  6167. return this;
  6168. },
  6169. /**
  6170. * 滚动到当前range开始的位置
  6171. * @method scrollToView
  6172. * @param { Window } win 当前range对象所属的window对象
  6173. * @return { UE.dom.Range } 当前Range对象
  6174. */
  6175. /**
  6176. * 滚动到距离当前range开始位置 offset 的位置处
  6177. * @method scrollToView
  6178. * @param { Window } win 当前range对象所属的window对象
  6179. * @param { Number } offset 距离range开始位置处的偏移量, 如果为正数, 则向下偏移, 反之, 则向上偏移
  6180. * @return { UE.dom.Range } 当前Range对象
  6181. */
  6182. scrollToView: function(win, offset) {
  6183. win = win ? window : domUtils.getWindow(this.document);
  6184. var me = this,
  6185. span = me.document.createElement("span");
  6186. //trace:717
  6187. span.innerHTML = "&nbsp;";
  6188. me.cloneRange().insertNode(span);
  6189. domUtils.scrollToView(span, win, offset);
  6190. domUtils.remove(span);
  6191. return me;
  6192. },
  6193. /**
  6194. * 判断当前选区内容是否占位符
  6195. * @private
  6196. * @method inFillChar
  6197. * @return { Boolean } 如果是占位符返回true,否则返回false
  6198. */
  6199. inFillChar: function() {
  6200. var start = this.startContainer;
  6201. if (
  6202. this.collapsed &&
  6203. start.nodeType == 3 &&
  6204. start.nodeValue.replace(new RegExp("^" + domUtils.fillChar), "")
  6205. .length +
  6206. 1 ==
  6207. start.nodeValue.length
  6208. ) {
  6209. return true;
  6210. }
  6211. return false;
  6212. },
  6213. /**
  6214. * 保存
  6215. * @method createAddress
  6216. * @private
  6217. * @return { Boolean } 返回开始和结束的位置
  6218. * @example
  6219. * ```html
  6220. * <body>
  6221. * <p>
  6222. * aaaa
  6223. * <em>
  6224. * <!-- 选区开始 -->
  6225. * bbbb
  6226. * <!-- 选区结束 -->
  6227. * </em>
  6228. * </p>
  6229. *
  6230. * <script>
  6231. * //output: {startAddress:[0,1,0,0],endAddress:[0,1,0,4]}
  6232. * console.log( range.createAddress() );
  6233. * </script>
  6234. * </body>
  6235. * ```
  6236. */
  6237. createAddress: function(ignoreEnd, ignoreTxt) {
  6238. var addr = {},
  6239. me = this;
  6240. function getAddress(isStart) {
  6241. var node = isStart ? me.startContainer : me.endContainer;
  6242. var parents = domUtils.findParents(node, true, function(node) {
  6243. return !domUtils.isBody(node);
  6244. }),
  6245. addrs = [];
  6246. for (var i = 0, ci; (ci = parents[i++]); ) {
  6247. addrs.push(domUtils.getNodeIndex(ci, ignoreTxt));
  6248. }
  6249. var firstIndex = 0;
  6250. if (ignoreTxt) {
  6251. if (node.nodeType == 3) {
  6252. var tmpNode = node.previousSibling;
  6253. while (tmpNode && tmpNode.nodeType == 3) {
  6254. firstIndex += tmpNode.nodeValue.replace(fillCharReg, "").length;
  6255. tmpNode = tmpNode.previousSibling;
  6256. }
  6257. firstIndex += isStart ? me.startOffset : me.endOffset; // - (fillCharReg.test(node.nodeValue) ? 1 : 0 )
  6258. } else {
  6259. node = node.childNodes[isStart ? me.startOffset : me.endOffset];
  6260. if (node) {
  6261. firstIndex = domUtils.getNodeIndex(node, ignoreTxt);
  6262. } else {
  6263. node = isStart ? me.startContainer : me.endContainer;
  6264. var first = node.firstChild;
  6265. while (first) {
  6266. if (domUtils.isFillChar(first)) {
  6267. first = first.nextSibling;
  6268. continue;
  6269. }
  6270. firstIndex++;
  6271. if (first.nodeType == 3) {
  6272. while (first && first.nodeType == 3) {
  6273. first = first.nextSibling;
  6274. }
  6275. } else {
  6276. first = first.nextSibling;
  6277. }
  6278. }
  6279. }
  6280. }
  6281. } else {
  6282. firstIndex = isStart
  6283. ? domUtils.isFillChar(node) ? 0 : me.startOffset
  6284. : me.endOffset;
  6285. }
  6286. if (firstIndex < 0) {
  6287. firstIndex = 0;
  6288. }
  6289. addrs.push(firstIndex);
  6290. return addrs;
  6291. }
  6292. addr.startAddress = getAddress(true);
  6293. if (!ignoreEnd) {
  6294. addr.endAddress = me.collapsed
  6295. ? [].concat(addr.startAddress)
  6296. : getAddress();
  6297. }
  6298. return addr;
  6299. },
  6300. /**
  6301. * 保存
  6302. * @method createAddress
  6303. * @private
  6304. * @return { Boolean } 返回开始和结束的位置
  6305. * @example
  6306. * ```html
  6307. * <body>
  6308. * <p>
  6309. * aaaa
  6310. * <em>
  6311. * <!-- 选区开始 -->
  6312. * bbbb
  6313. * <!-- 选区结束 -->
  6314. * </em>
  6315. * </p>
  6316. *
  6317. * <script>
  6318. * var range = editor.selection.getRange();
  6319. * range.moveToAddress({startAddress:[0,1,0,0],endAddress:[0,1,0,4]});
  6320. * range.select();
  6321. * //output: 'bbbb'
  6322. * console.log(editor.selection.getText());
  6323. * </script>
  6324. * </body>
  6325. * ```
  6326. */
  6327. moveToAddress: function(addr, ignoreEnd) {
  6328. var me = this;
  6329. function getNode(address, isStart) {
  6330. var tmpNode = me.document.body,
  6331. parentNode,
  6332. offset;
  6333. for (var i = 0, ci, l = address.length; i < l; i++) {
  6334. ci = address[i];
  6335. parentNode = tmpNode;
  6336. tmpNode = tmpNode.childNodes[ci];
  6337. if (!tmpNode) {
  6338. offset = ci;
  6339. break;
  6340. }
  6341. }
  6342. if (isStart) {
  6343. if (tmpNode) {
  6344. me.setStartBefore(tmpNode);
  6345. } else {
  6346. me.setStart(parentNode, offset);
  6347. }
  6348. } else {
  6349. if (tmpNode) {
  6350. me.setEndBefore(tmpNode);
  6351. } else {
  6352. me.setEnd(parentNode, offset);
  6353. }
  6354. }
  6355. }
  6356. getNode(addr.startAddress, true);
  6357. !ignoreEnd && addr.endAddress && getNode(addr.endAddress);
  6358. return me;
  6359. },
  6360. /**
  6361. * 判断给定的Range对象是否和当前Range对象表示的是同一个选区
  6362. * @method equals
  6363. * @param { UE.dom.Range } 需要判断的Range对象
  6364. * @return { Boolean } 如果给定的Range对象与当前Range对象表示的是同一个选区, 则返回true, 否则返回false
  6365. */
  6366. equals: function(rng) {
  6367. for (var p in this) {
  6368. if (this.hasOwnProperty(p)) {
  6369. if (this[p] !== rng[p]) return false;
  6370. }
  6371. }
  6372. return true;
  6373. },
  6374. /**
  6375. * 遍历range内的节点。每当遍历一个节点时, 都会执行参数项 doFn 指定的函数, 该函数的接受当前遍历的节点
  6376. * 作为其参数。
  6377. * @method traversal
  6378. * @param { Function } doFn 对每个遍历的节点要执行的方法, 该方法接受当前遍历的节点作为其参数
  6379. * @return { UE.dom.Range } 当前range对象
  6380. * @example
  6381. * ```html
  6382. *
  6383. * <body>
  6384. *
  6385. * <!-- 选区开始 -->
  6386. * <span></span>
  6387. * <a></a>
  6388. * <!-- 选区结束 -->
  6389. * </body>
  6390. *
  6391. * <script>
  6392. *
  6393. * //output: <span></span><a></a>
  6394. * console.log( range.cloneContents() );
  6395. *
  6396. * range.traversal( function ( node ) {
  6397. *
  6398. * if ( node.nodeType === 1 ) {
  6399. * node.className = "test";
  6400. * }
  6401. *
  6402. * } );
  6403. *
  6404. * //output: <span class="test"></span><a class="test"></a>
  6405. * console.log( range.cloneContents() );
  6406. *
  6407. * </script>
  6408. * ```
  6409. */
  6410. /**
  6411. * 遍历range内的节点。
  6412. * 每当遍历一个节点时, 都会执行参数项 doFn 指定的函数, 该函数的接受当前遍历的节点
  6413. * 作为其参数。
  6414. * 可以通过参数项 filterFn 来指定一个过滤器, 只有符合该过滤器过滤规则的节点才会触
  6415. * 发doFn函数的执行
  6416. * @method traversal
  6417. * @param { Function } doFn 对每个遍历的节点要执行的方法, 该方法接受当前遍历的节点作为其参数
  6418. * @param { Function } filterFn 过滤器, 该函数接受当前遍历的节点作为参数, 如果该节点满足过滤
  6419. * 规则, 请返回true, 该节点会触发doFn, 否则, 请返回false, 则该节点不
  6420. * 会触发doFn。
  6421. * @return { UE.dom.Range } 当前range对象
  6422. * @see UE.dom.Range:traversal(Function)
  6423. * @example
  6424. * ```html
  6425. *
  6426. * <body>
  6427. *
  6428. * <!-- 选区开始 -->
  6429. * <span></span>
  6430. * <a></a>
  6431. * <!-- 选区结束 -->
  6432. * </body>
  6433. *
  6434. * <script>
  6435. *
  6436. * //output: <span></span><a></a>
  6437. * console.log( range.cloneContents() );
  6438. *
  6439. * range.traversal( function ( node ) {
  6440. *
  6441. * node.className = "test";
  6442. *
  6443. * }, function ( node ) {
  6444. * return node.nodeType === 1;
  6445. * } );
  6446. *
  6447. * //output: <span class="test"></span><a class="test"></a>
  6448. * console.log( range.cloneContents() );
  6449. *
  6450. * </script>
  6451. * ```
  6452. */
  6453. traversal: function(doFn, filterFn) {
  6454. if (this.collapsed) return this;
  6455. var bookmark = this.createBookmark(),
  6456. end = bookmark.end,
  6457. current = domUtils.getNextDomNode(bookmark.start, false, filterFn);
  6458. while (
  6459. current &&
  6460. current !== end &&
  6461. domUtils.getPosition(current, end) & domUtils.POSITION_PRECEDING
  6462. ) {
  6463. var tmpNode = domUtils.getNextDomNode(current, false, filterFn);
  6464. doFn(current);
  6465. current = tmpNode;
  6466. }
  6467. return this.moveToBookmark(bookmark);
  6468. }
  6469. };
  6470. })();
  6471. // core/Selection.js
  6472. /**
  6473. * 选集
  6474. * @file
  6475. * @module UE.dom
  6476. * @class Selection
  6477. * @since 1.2.6.1
  6478. */
  6479. /**
  6480. * 选区集合
  6481. * @unfile
  6482. * @module UE.dom
  6483. * @class Selection
  6484. */
  6485. (function() {
  6486. function getBoundaryInformation(range, start) {
  6487. var getIndex = domUtils.getNodeIndex;
  6488. range = range.duplicate();
  6489. range.collapse(start);
  6490. var parent = range.parentElement();
  6491. //如果节点里没有子节点,直接退出
  6492. if (!parent.hasChildNodes()) {
  6493. return { container: parent, offset: 0 };
  6494. }
  6495. var siblings = parent.children,
  6496. child,
  6497. testRange = range.duplicate(),
  6498. startIndex = 0,
  6499. endIndex = siblings.length - 1,
  6500. index = -1,
  6501. distance;
  6502. while (startIndex <= endIndex) {
  6503. index = Math.floor((startIndex + endIndex) / 2);
  6504. child = siblings[index];
  6505. testRange.moveToElementText(child);
  6506. var position = testRange.compareEndPoints("StartToStart", range);
  6507. if (position > 0) {
  6508. endIndex = index - 1;
  6509. } else if (position < 0) {
  6510. startIndex = index + 1;
  6511. } else {
  6512. //trace:1043
  6513. return { container: parent, offset: getIndex(child) };
  6514. }
  6515. }
  6516. if (index == -1) {
  6517. testRange.moveToElementText(parent);
  6518. testRange.setEndPoint("StartToStart", range);
  6519. distance = testRange.text.replace(/(\r\n|\r)/g, "\n").length;
  6520. siblings = parent.childNodes;
  6521. if (!distance) {
  6522. child = siblings[siblings.length - 1];
  6523. return { container: child, offset: child.nodeValue.length };
  6524. }
  6525. var i = siblings.length;
  6526. while (distance > 0) {
  6527. distance -= siblings[--i].nodeValue.length;
  6528. }
  6529. return { container: siblings[i], offset: -distance };
  6530. }
  6531. testRange.collapse(position > 0);
  6532. testRange.setEndPoint(position > 0 ? "StartToStart" : "EndToStart", range);
  6533. distance = testRange.text.replace(/(\r\n|\r)/g, "\n").length;
  6534. if (!distance) {
  6535. return dtd.$empty[child.tagName] || dtd.$nonChild[child.tagName]
  6536. ? {
  6537. container: parent,
  6538. offset: getIndex(child) + (position > 0 ? 0 : 1)
  6539. }
  6540. : {
  6541. container: child,
  6542. offset: position > 0 ? 0 : child.childNodes.length
  6543. };
  6544. }
  6545. while (distance > 0) {
  6546. try {
  6547. var pre = child;
  6548. child = child[position > 0 ? "previousSibling" : "nextSibling"];
  6549. distance -= child.nodeValue.length;
  6550. } catch (e) {
  6551. return { container: parent, offset: getIndex(pre) };
  6552. }
  6553. }
  6554. return {
  6555. container: child,
  6556. offset: position > 0 ? -distance : child.nodeValue.length + distance
  6557. };
  6558. }
  6559. /**
  6560. * 将ieRange转换为Range对象
  6561. * @param {Range} ieRange ieRange对象
  6562. * @param {Range} range Range对象
  6563. * @return {Range} range 返回转换后的Range对象
  6564. */
  6565. function transformIERangeToRange(ieRange, range) {
  6566. if (ieRange.item) {
  6567. range.selectNode(ieRange.item(0));
  6568. } else {
  6569. var bi = getBoundaryInformation(ieRange, true);
  6570. range.setStart(bi.container, bi.offset);
  6571. if (ieRange.compareEndPoints("StartToEnd", ieRange) != 0) {
  6572. bi = getBoundaryInformation(ieRange, false);
  6573. range.setEnd(bi.container, bi.offset);
  6574. }
  6575. }
  6576. return range;
  6577. }
  6578. /**
  6579. * 获得ieRange
  6580. * @param {Selection} sel Selection对象
  6581. * @return {ieRange} 得到ieRange
  6582. */
  6583. function _getIERange(sel) {
  6584. var ieRange;
  6585. //ie下有可能报错
  6586. try {
  6587. ieRange = sel.getNative().createRange();
  6588. } catch (e) {
  6589. return null;
  6590. }
  6591. var el = ieRange.item ? ieRange.item(0) : ieRange.parentElement();
  6592. if ((el.ownerDocument || el) === sel.document) {
  6593. return ieRange;
  6594. }
  6595. return null;
  6596. }
  6597. var Selection = (dom.Selection = function(doc) {
  6598. var me = this,
  6599. iframe;
  6600. me.document = doc;
  6601. if (browser.ie9below) {
  6602. iframe = domUtils.getWindow(doc).frameElement;
  6603. domUtils.on(iframe, "beforedeactivate", function() {
  6604. me._bakIERange = me.getIERange();
  6605. });
  6606. domUtils.on(iframe, "activate", function() {
  6607. try {
  6608. if (!_getIERange(me) && me._bakIERange) {
  6609. me._bakIERange.select();
  6610. }
  6611. } catch (ex) {}
  6612. me._bakIERange = null;
  6613. });
  6614. }
  6615. iframe = doc = null;
  6616. });
  6617. Selection.prototype = {
  6618. rangeInBody: function(rng, txtRange) {
  6619. var node = browser.ie9below || txtRange
  6620. ? rng.item ? rng.item() : rng.parentElement()
  6621. : rng.startContainer;
  6622. return node === this.document.body || domUtils.inDoc(node, this.document);
  6623. },
  6624. /**
  6625. * 获取原生seleciton对象
  6626. * @method getNative
  6627. * @return { Object } 获得selection对象
  6628. * @example
  6629. * ```javascript
  6630. * editor.selection.getNative();
  6631. * ```
  6632. */
  6633. getNative: function() {
  6634. var doc = this.document;
  6635. try {
  6636. return !doc
  6637. ? null
  6638. : browser.ie9below
  6639. ? doc.selection
  6640. : domUtils.getWindow(doc).getSelection();
  6641. } catch (e) {
  6642. return null;
  6643. }
  6644. },
  6645. /**
  6646. * 获得ieRange
  6647. * @method getIERange
  6648. * @return { Object } 返回ie原生的Range
  6649. * @example
  6650. * ```javascript
  6651. * editor.selection.getIERange();
  6652. * ```
  6653. */
  6654. getIERange: function() {
  6655. var ieRange = _getIERange(this);
  6656. if (!ieRange) {
  6657. if (this._bakIERange) {
  6658. return this._bakIERange;
  6659. }
  6660. }
  6661. return ieRange;
  6662. },
  6663. /**
  6664. * 缓存当前选区的range和选区的开始节点
  6665. * @method cache
  6666. */
  6667. cache: function() {
  6668. this.clear();
  6669. this._cachedRange = this.getRange();
  6670. this._cachedStartElement = this.getStart();
  6671. this._cachedStartElementPath = this.getStartElementPath();
  6672. },
  6673. /**
  6674. * 获取选区开始位置的父节点到body
  6675. * @method getStartElementPath
  6676. * @return { Array } 返回父节点集合
  6677. * @example
  6678. * ```javascript
  6679. * editor.selection.getStartElementPath();
  6680. * ```
  6681. */
  6682. getStartElementPath: function() {
  6683. if (this._cachedStartElementPath) {
  6684. return this._cachedStartElementPath;
  6685. }
  6686. var start = this.getStart();
  6687. if (start) {
  6688. return domUtils.findParents(start, true, null, true);
  6689. }
  6690. return [];
  6691. },
  6692. /**
  6693. * 清空缓存
  6694. * @method clear
  6695. */
  6696. clear: function() {
  6697. this._cachedStartElementPath = this._cachedRange = this._cachedStartElement = null;
  6698. },
  6699. /**
  6700. * 编辑器是否得到了选区
  6701. * @method isFocus
  6702. */
  6703. isFocus: function() {
  6704. try {
  6705. if (browser.ie9below) {
  6706. var nativeRange = _getIERange(this);
  6707. return !!(nativeRange && this.rangeInBody(nativeRange));
  6708. } else {
  6709. return !!this.getNative().rangeCount;
  6710. }
  6711. } catch (e) {
  6712. return false;
  6713. }
  6714. },
  6715. /**
  6716. * 获取选区对应的Range
  6717. * @method getRange
  6718. * @return { Object } 得到Range对象
  6719. * @example
  6720. * ```javascript
  6721. * editor.selection.getRange();
  6722. * ```
  6723. */
  6724. getRange: function() {
  6725. var me = this;
  6726. function optimze(range) {
  6727. var child = me.document.body.firstChild,
  6728. collapsed = range.collapsed;
  6729. while (child && child.firstChild) {
  6730. range.setStart(child, 0);
  6731. child = child.firstChild;
  6732. }
  6733. if (!range.startContainer) {
  6734. range.setStart(me.document.body, 0);
  6735. }
  6736. if (collapsed) {
  6737. range.collapse(true);
  6738. }
  6739. }
  6740. if (me._cachedRange != null) {
  6741. return this._cachedRange;
  6742. }
  6743. var range = new baidu.editor.dom.Range(me.document);
  6744. if (browser.ie9below) {
  6745. var nativeRange = me.getIERange();
  6746. if (nativeRange) {
  6747. //备份的_bakIERange可能已经实效了,dom树发生了变化比如从源码模式切回来,所以try一下,实效就放到body开始位置
  6748. try {
  6749. transformIERangeToRange(nativeRange, range);
  6750. } catch (e) {
  6751. optimze(range);
  6752. }
  6753. } else {
  6754. optimze(range);
  6755. }
  6756. } else {
  6757. var sel = me.getNative();
  6758. if (sel && sel.rangeCount) {
  6759. var firstRange = sel.getRangeAt(0);
  6760. var lastRange = sel.getRangeAt(sel.rangeCount - 1);
  6761. range
  6762. .setStart(firstRange.startContainer, firstRange.startOffset)
  6763. .setEnd(lastRange.endContainer, lastRange.endOffset);
  6764. if (
  6765. range.collapsed &&
  6766. domUtils.isBody(range.startContainer) &&
  6767. !range.startOffset
  6768. ) {
  6769. optimze(range);
  6770. }
  6771. } else {
  6772. //trace:1734 有可能已经不在dom树上了,标识的节点
  6773. if (
  6774. this._bakRange &&
  6775. domUtils.inDoc(this._bakRange.startContainer, this.document)
  6776. ) {
  6777. return this._bakRange;
  6778. }
  6779. optimze(range);
  6780. }
  6781. }
  6782. return (this._bakRange = range);
  6783. },
  6784. /**
  6785. * 获取开始元素,用于状态反射
  6786. * @method getStart
  6787. * @return { Element } 获得开始元素
  6788. * @example
  6789. * ```javascript
  6790. * editor.selection.getStart();
  6791. * ```
  6792. */
  6793. getStart: function() {
  6794. if (this._cachedStartElement) {
  6795. return this._cachedStartElement;
  6796. }
  6797. var range = browser.ie9below ? this.getIERange() : this.getRange(),
  6798. tmpRange,
  6799. start,
  6800. tmp,
  6801. parent;
  6802. if (browser.ie9below) {
  6803. if (!range) {
  6804. //todo 给第一个值可能会有问题
  6805. return this.document.body.firstChild;
  6806. }
  6807. //control元素
  6808. if (range.item) {
  6809. return range.item(0);
  6810. }
  6811. tmpRange = range.duplicate();
  6812. //修正ie下<b>x</b>[xx] 闭合后 <b>x|</b>xx
  6813. tmpRange.text.length > 0 && tmpRange.moveStart("character", 1);
  6814. tmpRange.collapse(1);
  6815. start = tmpRange.parentElement();
  6816. parent = tmp = range.parentElement();
  6817. while ((tmp = tmp.parentNode)) {
  6818. if (tmp == start) {
  6819. start = parent;
  6820. break;
  6821. }
  6822. }
  6823. } else {
  6824. range.shrinkBoundary();
  6825. start = range.startContainer;
  6826. if (start.nodeType == 1 && start.hasChildNodes()) {
  6827. start =
  6828. start.childNodes[
  6829. Math.min(start.childNodes.length - 1, range.startOffset)
  6830. ];
  6831. }
  6832. if (start.nodeType == 3) {
  6833. return start.parentNode;
  6834. }
  6835. }
  6836. return start;
  6837. },
  6838. /**
  6839. * 得到选区中的文本
  6840. * @method getText
  6841. * @return { String } 选区中包含的文本
  6842. * @example
  6843. * ```javascript
  6844. * editor.selection.getText();
  6845. * ```
  6846. */
  6847. getText: function() {
  6848. var nativeSel, nativeRange;
  6849. if (this.isFocus() && (nativeSel = this.getNative())) {
  6850. nativeRange = browser.ie9below
  6851. ? nativeSel.createRange()
  6852. : nativeSel.getRangeAt(0);
  6853. return browser.ie9below ? nativeRange.text : nativeRange.toString();
  6854. }
  6855. return "";
  6856. },
  6857. /**
  6858. * 清除选区
  6859. * @method clearRange
  6860. * @example
  6861. * ```javascript
  6862. * editor.selection.clearRange();
  6863. * ```
  6864. */
  6865. clearRange: function() {
  6866. this.getNative()[browser.ie9below ? "empty" : "removeAllRanges"]();
  6867. }
  6868. };
  6869. })();
  6870. // core/Editor.js
  6871. /**
  6872. * 编辑器主类,包含编辑器提供的大部分公用接口
  6873. * @file
  6874. * @module UE
  6875. * @class Editor
  6876. * @since 1.2.6.1
  6877. */
  6878. /**
  6879. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  6880. * @unfile
  6881. * @module UE
  6882. */
  6883. /**
  6884. * UEditor的核心类,为用户提供与编辑器交互的接口。
  6885. * @unfile
  6886. * @module UE
  6887. * @class Editor
  6888. */
  6889. (function() {
  6890. var uid = 0,
  6891. _selectionChangeTimer;
  6892. /**
  6893. * 获取编辑器的html内容,赋值到编辑器所在表单的textarea文本域里面
  6894. * @private
  6895. * @method setValue
  6896. * @param { UE.Editor } editor 编辑器事例
  6897. */
  6898. function setValue(form, editor) {
  6899. var textarea;
  6900. if (editor.options.textarea) {
  6901. if (utils.isString(editor.options.textarea)) {
  6902. for (
  6903. var i = 0, ti, tis = domUtils.getElementsByTagName(form, "textarea");
  6904. (ti = tis[i++]);
  6905. ) {
  6906. if (ti.id == "ueditor_textarea_" + editor.options.textarea) {
  6907. textarea = ti;
  6908. break;
  6909. }
  6910. }
  6911. } else {
  6912. textarea = editor.textarea;
  6913. }
  6914. }
  6915. if (!textarea) {
  6916. form.appendChild(
  6917. (textarea = domUtils.createElement(document, "textarea", {
  6918. name: editor.options.textarea,
  6919. id: "ueditor_textarea_" + editor.options.textarea,
  6920. style: "display:none"
  6921. }))
  6922. );
  6923. //不要产生多个textarea
  6924. editor.textarea = textarea;
  6925. }
  6926. !textarea.getAttribute("name") &&
  6927. textarea.setAttribute("name", editor.options.textarea);
  6928. textarea.value = editor.hasContents()
  6929. ? editor.options.allHtmlEnabled
  6930. ? editor.getAllHtml()
  6931. : editor.getContent(null, null, true)
  6932. : "";
  6933. }
  6934. function loadPlugins(me) {
  6935. //初始化插件
  6936. for (var pi in UE.plugins) {
  6937. UE.plugins[pi].call(me);
  6938. }
  6939. }
  6940. function checkCurLang(I18N) {
  6941. for (var lang in I18N) {
  6942. return lang;
  6943. }
  6944. }
  6945. function langReadied(me) {
  6946. me.langIsReady = true;
  6947. me.fireEvent("langReady");
  6948. }
  6949. /**
  6950. * 编辑器准备就绪后会触发该事件
  6951. * @module UE
  6952. * @class Editor
  6953. * @event ready
  6954. * @remind render方法执行完成之后,会触发该事件
  6955. * @remind
  6956. * @example
  6957. * ```javascript
  6958. * editor.addListener( 'ready', function( editor ) {
  6959. * editor.execCommand( 'focus' ); //编辑器家在完成后,让编辑器拿到焦点
  6960. * } );
  6961. * ```
  6962. */
  6963. /**
  6964. * 执行destroy方法,会触发该事件
  6965. * @module UE
  6966. * @class Editor
  6967. * @event destroy
  6968. * @see UE.Editor:destroy()
  6969. */
  6970. /**
  6971. * 执行reset方法,会触发该事件
  6972. * @module UE
  6973. * @class Editor
  6974. * @event reset
  6975. * @see UE.Editor:reset()
  6976. */
  6977. /**
  6978. * 执行focus方法,会触发该事件
  6979. * @module UE
  6980. * @class Editor
  6981. * @event focus
  6982. * @see UE.Editor:focus(Boolean)
  6983. */
  6984. /**
  6985. * 语言加载完成会触发该事件
  6986. * @module UE
  6987. * @class Editor
  6988. * @event langReady
  6989. */
  6990. /**
  6991. * 运行命令之后会触发该命令
  6992. * @module UE
  6993. * @class Editor
  6994. * @event beforeExecCommand
  6995. */
  6996. /**
  6997. * 运行命令之后会触发该命令
  6998. * @module UE
  6999. * @class Editor
  7000. * @event afterExecCommand
  7001. */
  7002. /**
  7003. * 运行命令之前会触发该命令
  7004. * @module UE
  7005. * @class Editor
  7006. * @event firstBeforeExecCommand
  7007. */
  7008. /**
  7009. * 在getContent方法执行之前会触发该事件
  7010. * @module UE
  7011. * @class Editor
  7012. * @event beforeGetContent
  7013. * @see UE.Editor:getContent()
  7014. */
  7015. /**
  7016. * 在getContent方法执行之后会触发该事件
  7017. * @module UE
  7018. * @class Editor
  7019. * @event afterGetContent
  7020. * @see UE.Editor:getContent()
  7021. */
  7022. /**
  7023. * 在getAllHtml方法执行时会触发该事件
  7024. * @module UE
  7025. * @class Editor
  7026. * @event getAllHtml
  7027. * @see UE.Editor:getAllHtml()
  7028. */
  7029. /**
  7030. * 在setContent方法执行之前会触发该事件
  7031. * @module UE
  7032. * @class Editor
  7033. * @event beforeSetContent
  7034. * @see UE.Editor:setContent(String)
  7035. */
  7036. /**
  7037. * 在setContent方法执行之后会触发该事件
  7038. * @module UE
  7039. * @class Editor
  7040. * @event afterSetContent
  7041. * @see UE.Editor:setContent(String)
  7042. */
  7043. /**
  7044. * 每当编辑器内部选区发生改变时,将触发该事件
  7045. * @event selectionchange
  7046. * @warning 该事件的触发非常频繁,不建议在该事件的处理过程中做重量级的处理
  7047. * @example
  7048. * ```javascript
  7049. * editor.addListener( 'selectionchange', function( editor ) {
  7050. * console.log('选区发生改变');
  7051. * }
  7052. */
  7053. /**
  7054. * 在所有selectionchange的监听函数执行之前,会触发该事件
  7055. * @module UE
  7056. * @class Editor
  7057. * @event beforeSelectionChange
  7058. * @see UE.Editor:selectionchange
  7059. */
  7060. /**
  7061. * 在所有selectionchange的监听函数执行完之后,会触发该事件
  7062. * @module UE
  7063. * @class Editor
  7064. * @event afterSelectionChange
  7065. * @see UE.Editor:selectionchange
  7066. */
  7067. /**
  7068. * 编辑器内容发生改变时会触发该事件
  7069. * @module UE
  7070. * @class Editor
  7071. * @event contentChange
  7072. */
  7073. /**
  7074. * 以默认参数构建一个编辑器实例
  7075. * @constructor
  7076. * @remind 通过 改构造方法实例化的编辑器,不带ui层.需要render到一个容器,编辑器实例才能正常渲染到页面
  7077. * @example
  7078. * ```javascript
  7079. * var editor = new UE.Editor();
  7080. * editor.execCommand('blod');
  7081. * ```
  7082. * @see UE.Config
  7083. */
  7084. /**
  7085. * 以给定的参数集合创建一个编辑器实例,对于未指定的参数,将应用默认参数。
  7086. * @constructor
  7087. * @remind 通过 改构造方法实例化的编辑器,不带ui层.需要render到一个容器,编辑器实例才能正常渲染到页面
  7088. * @param { Object } setting 创建编辑器的参数
  7089. * @example
  7090. * ```javascript
  7091. * var editor = new UE.Editor();
  7092. * editor.execCommand('blod');
  7093. * ```
  7094. * @see UE.Config
  7095. */
  7096. var Editor = (UE.Editor = function(options) {
  7097. var me = this;
  7098. me.uid = uid++;
  7099. EventBase.call(me);
  7100. me.commands = {};
  7101. me.options = utils.extend(utils.clone(options || {}), UEDITOR_CONFIG, true);
  7102. me.shortcutkeys = {};
  7103. me.inputRules = [];
  7104. me.outputRules = [];
  7105. //设置默认的常用属性
  7106. me.setOpt(Editor.defaultOptions(me));
  7107. /* 尝试异步加载后台配置 */
  7108. me.loadServerConfig();
  7109. if (!utils.isEmptyObject(UE.I18N)) {
  7110. //修改默认的语言类型
  7111. me.options.lang = checkCurLang(UE.I18N);
  7112. UE.plugin.load(me);
  7113. langReadied(me);
  7114. } else {
  7115. utils.loadFile(
  7116. document,
  7117. {
  7118. src:
  7119. me.options.langPath +
  7120. me.options.lang +
  7121. "/" +
  7122. me.options.lang +
  7123. ".js",
  7124. tag: "script",
  7125. type: "text/javascript",
  7126. defer: "defer"
  7127. },
  7128. function() {
  7129. UE.plugin.load(me);
  7130. langReadied(me);
  7131. }
  7132. );
  7133. }
  7134. UE.instants["ueditorInstant" + me.uid] = me;
  7135. });
  7136. Editor.prototype = {
  7137. registerCommand: function(name, obj) {
  7138. this.commands[name] = obj;
  7139. },
  7140. /**
  7141. * 编辑器对外提供的监听ready事件的接口, 通过调用该方法,达到的效果与监听ready事件是一致的
  7142. * @method ready
  7143. * @param { Function } fn 编辑器ready之后所执行的回调, 如果在注册事件之前编辑器已经ready,将会
  7144. * 立即触发该回调。
  7145. * @remind 需要等待编辑器加载完成后才能执行的代码,可以使用该方法传入
  7146. * @example
  7147. * ```javascript
  7148. * editor.ready( function( editor ) {
  7149. * editor.setContent('初始化完毕');
  7150. * } );
  7151. * ```
  7152. * @see UE.Editor.event:ready
  7153. */
  7154. ready: function(fn) {
  7155. var me = this;
  7156. if (fn) {
  7157. me.isReady ? fn.apply(me) : me.addListener("ready", fn);
  7158. }
  7159. },
  7160. /**
  7161. * 该方法是提供给插件里面使用,设置配置项默认值
  7162. * @method setOpt
  7163. * @warning 三处设置配置项的优先级: 实例化时传入参数 > setOpt()设置 > config文件里设置
  7164. * @warning 该方法仅供编辑器插件内部和编辑器初始化时调用,其他地方不能调用。
  7165. * @param { String } key 编辑器的可接受的选项名称
  7166. * @param { * } val 该选项可接受的值
  7167. * @example
  7168. * ```javascript
  7169. * editor.setOpt( 'initContent', '欢迎使用编辑器' );
  7170. * ```
  7171. */
  7172. /**
  7173. * 该方法是提供给插件里面使用,以{key:value}集合的方式设置插件内用到的配置项默认值
  7174. * @method setOpt
  7175. * @warning 三处设置配置项的优先级: 实例化时传入参数 > setOpt()设置 > config文件里设置
  7176. * @warning 该方法仅供编辑器插件内部和编辑器初始化时调用,其他地方不能调用。
  7177. * @param { Object } options 将要设置的选项的键值对对象
  7178. * @example
  7179. * ```javascript
  7180. * editor.setOpt( {
  7181. * 'initContent': '欢迎使用编辑器'
  7182. * } );
  7183. * ```
  7184. */
  7185. setOpt: function(key, val) {
  7186. var obj = {};
  7187. if (utils.isString(key)) {
  7188. obj[key] = val;
  7189. } else {
  7190. obj = key;
  7191. }
  7192. utils.extend(this.options, obj, true);
  7193. },
  7194. getOpt: function(key) {
  7195. return this.options[key];
  7196. },
  7197. /**
  7198. * 销毁编辑器实例,使用textarea代替
  7199. * @method destroy
  7200. * @example
  7201. * ```javascript
  7202. * editor.destroy();
  7203. * ```
  7204. */
  7205. destroy: function() {
  7206. var me = this;
  7207. me.fireEvent("destroy");
  7208. var container = me.container.parentNode;
  7209. var textarea = me.textarea;
  7210. if (!textarea) {
  7211. textarea = document.createElement("textarea");
  7212. container.parentNode.insertBefore(textarea, container);
  7213. } else {
  7214. textarea.style.display = "";
  7215. }
  7216. textarea.style.width = me.iframe.offsetWidth + "px";
  7217. textarea.style.height = me.iframe.offsetHeight + "px";
  7218. textarea.value = me.getContent();
  7219. textarea.id = me.key;
  7220. container.innerHTML = "";
  7221. domUtils.remove(container);
  7222. var key = me.key;
  7223. //trace:2004
  7224. for (var p in me) {
  7225. if (me.hasOwnProperty(p)) {
  7226. delete this[p];
  7227. }
  7228. }
  7229. UE.delEditor(key);
  7230. },
  7231. /**
  7232. * 渲染编辑器的DOM到指定容器
  7233. * @method render
  7234. * @param { String } containerId 指定一个容器ID
  7235. * @remind 执行该方法,会触发ready事件
  7236. * @warning 必须且只能调用一次
  7237. */
  7238. /**
  7239. * 渲染编辑器的DOM到指定容器
  7240. * @method render
  7241. * @param { Element } containerDom 直接指定容器对象
  7242. * @remind 执行该方法,会触发ready事件
  7243. * @warning 必须且只能调用一次
  7244. */
  7245. render: function(container) {
  7246. var me = this,
  7247. options = me.options,
  7248. getStyleValue = function(attr) {
  7249. return parseInt(domUtils.getComputedStyle(container, attr));
  7250. };
  7251. if (utils.isString(container)) {
  7252. container = document.getElementById(container);
  7253. }
  7254. if (container) {
  7255. if (options.initialFrameWidth) {
  7256. options.minFrameWidth = options.initialFrameWidth;
  7257. } else {
  7258. options.minFrameWidth = options.initialFrameWidth =
  7259. container.offsetWidth;
  7260. }
  7261. if (options.initialFrameHeight) {
  7262. options.minFrameHeight = options.initialFrameHeight;
  7263. } else {
  7264. options.initialFrameHeight = options.minFrameHeight =
  7265. container.offsetHeight;
  7266. }
  7267. container.style.width = /%$/.test(options.initialFrameWidth)
  7268. ? "100%"
  7269. : options.initialFrameWidth -
  7270. getStyleValue("padding-left") -
  7271. getStyleValue("padding-right") +
  7272. "px";
  7273. container.style.height = /%$/.test(options.initialFrameHeight)
  7274. ? "100%"
  7275. : options.initialFrameHeight -
  7276. getStyleValue("padding-top") -
  7277. getStyleValue("padding-bottom") +
  7278. "px";
  7279. container.style.zIndex = options.zIndex;
  7280. var html =
  7281. (ie && browser.version < 9 ? "" : "<!DOCTYPE html>") +
  7282. "<html xmlns='http://www.w3.org/1999/xhtml' class='view' >" +
  7283. "<head>" +
  7284. "<style type='text/css'>" +
  7285. //设置四周的留边
  7286. ".view{padding:0;word-wrap:break-word;cursor:text;height:90%;}\n" +
  7287. //设置默认字体和字号
  7288. //font-family不能呢随便改,在safari下fillchar会有解析问题
  7289. "body{margin:8px;font-family:sans-serif;font-size:16px;}" +
  7290. //设置段落间距
  7291. "p{margin:5px 0;}</style>" +
  7292. (options.iframeCssUrl
  7293. ? "<link rel='stylesheet' type='text/css' href='" +
  7294. utils.unhtml(options.iframeCssUrl) +
  7295. "'/>"
  7296. : "") +
  7297. (options.initialStyle
  7298. ? "<style>" + options.initialStyle + "</style>"
  7299. : "") +
  7300. "</head>" +
  7301. "<body class='view' ></body>" +
  7302. "<script type='text/javascript' " +
  7303. (ie ? "defer='defer'" : "") +
  7304. " id='_initialScript'>" +
  7305. "setTimeout(function(){editor = window.parent.UE.instants['ueditorInstant" +
  7306. me.uid +
  7307. "'];editor._setup(document);},0);" +
  7308. "var _tmpScript = document.getElementById('_initialScript');_tmpScript.parentNode.removeChild(_tmpScript);" +
  7309. "</script>" +
  7310. (options.iframeJsUrl
  7311. ? "<script type='text/javascript' src='" +
  7312. utils.unhtml(options.iframeJsUrl) +
  7313. "'></script>"
  7314. : "") +
  7315. "</html>";
  7316. container.appendChild(
  7317. domUtils.createElement(document, "iframe", {
  7318. id: "ueditor_" + me.uid,
  7319. width: "100%",
  7320. height: "100%",
  7321. frameborder: "0",
  7322. //先注释掉了,加的原因忘记了,但开启会直接导致全屏模式下内容多时不会出现滚动条
  7323. // scrolling :'no',
  7324. src:
  7325. "javascript:void(function(){document.open();" +
  7326. (options.customDomain && document.domain != location.hostname
  7327. ? 'document.domain="' + document.domain + '";'
  7328. : "") +
  7329. 'document.write("' +
  7330. html +
  7331. '");document.close();}())'
  7332. })
  7333. );
  7334. container.style.overflow = "hidden";
  7335. //解决如果是给定的百分比,会导致高度算不对的问题
  7336. setTimeout(function() {
  7337. if (/%$/.test(options.initialFrameWidth)) {
  7338. options.minFrameWidth = options.initialFrameWidth =
  7339. container.offsetWidth;
  7340. //如果这里给定宽度,会导致ie在拖动窗口大小时,编辑区域不随着变化
  7341. // container.style.width = options.initialFrameWidth + 'px';
  7342. }
  7343. if (/%$/.test(options.initialFrameHeight)) {
  7344. options.minFrameHeight = options.initialFrameHeight =
  7345. container.offsetHeight;
  7346. container.style.height = options.initialFrameHeight + "px";
  7347. }
  7348. });
  7349. }
  7350. },
  7351. /**
  7352. * 编辑器初始化
  7353. * @method _setup
  7354. * @private
  7355. * @param { Element } doc 编辑器Iframe中的文档对象
  7356. */
  7357. _setup: function(doc) {
  7358. var me = this,
  7359. options = me.options;
  7360. if (ie) {
  7361. doc.body.disabled = true;
  7362. doc.body.contentEditable = true;
  7363. doc.body.disabled = false;
  7364. } else {
  7365. doc.body.contentEditable = true;
  7366. }
  7367. doc.body.spellcheck = false;
  7368. me.document = doc;
  7369. me.window = doc.defaultView || doc.parentWindow;
  7370. me.iframe = me.window.frameElement;
  7371. me.body = doc.body;
  7372. me.selection = new dom.Selection(doc);
  7373. //gecko初始化就能得到range,无法判断isFocus了
  7374. var geckoSel;
  7375. if (browser.gecko && (geckoSel = this.selection.getNative())) {
  7376. geckoSel.removeAllRanges();
  7377. }
  7378. this._initEvents();
  7379. //为form提交提供一个隐藏的textarea
  7380. for (
  7381. var form = this.iframe.parentNode;
  7382. !domUtils.isBody(form);
  7383. form = form.parentNode
  7384. ) {
  7385. if (form.tagName == "FORM") {
  7386. me.form = form;
  7387. if (me.options.autoSyncData) {
  7388. domUtils.on(me.window, "blur", function() {
  7389. setValue(form, me);
  7390. });
  7391. } else {
  7392. domUtils.on(form, "submit", function() {
  7393. setValue(this, me);
  7394. });
  7395. }
  7396. break;
  7397. }
  7398. }
  7399. if (options.initialContent) {
  7400. if (options.autoClearinitialContent) {
  7401. var oldExecCommand = me.execCommand;
  7402. me.execCommand = function() {
  7403. me.fireEvent("firstBeforeExecCommand");
  7404. return oldExecCommand.apply(me, arguments);
  7405. };
  7406. this._setDefaultContent(options.initialContent);
  7407. } else this.setContent(options.initialContent, false, true);
  7408. }
  7409. //编辑器不能为空内容
  7410. if (domUtils.isEmptyNode(me.body)) {
  7411. me.body.innerHTML = "<p>" + (browser.ie ? "" : "<br/>") + "</p>";
  7412. }
  7413. //如果要求focus, 就把光标定位到内容开始
  7414. if (options.focus) {
  7415. setTimeout(function() {
  7416. me.focus(me.options.focusInEnd);
  7417. //如果自动清除开着,就不需要做selectionchange;
  7418. !me.options.autoClearinitialContent && me._selectionChange();
  7419. }, 0);
  7420. }
  7421. if (!me.container) {
  7422. me.container = this.iframe.parentNode;
  7423. }
  7424. if (options.fullscreen && me.ui) {
  7425. me.ui.setFullScreen(true);
  7426. }
  7427. try {
  7428. me.document.execCommand("2D-position", false, false);
  7429. } catch (e) {}
  7430. try {
  7431. me.document.execCommand("enableInlineTableEditing", false, false);
  7432. } catch (e) {}
  7433. try {
  7434. me.document.execCommand("enableObjectResizing", false, false);
  7435. } catch (e) {}
  7436. //挂接快捷键
  7437. me._bindshortcutKeys();
  7438. me.isReady = 1;
  7439. me.fireEvent("ready");
  7440. options.onready && options.onready.call(me);
  7441. if (!browser.ie9below) {
  7442. domUtils.on(me.window, ["blur", "focus"], function(e) {
  7443. //chrome下会出现alt+tab切换时,导致选区位置不对
  7444. if (e.type == "blur") {
  7445. me._bakRange = me.selection.getRange();
  7446. try {
  7447. me._bakNativeRange = me.selection.getNative().getRangeAt(0);
  7448. me.selection.getNative().removeAllRanges();
  7449. } catch (e) {
  7450. me._bakNativeRange = null;
  7451. }
  7452. } else {
  7453. try {
  7454. me._bakRange && me._bakRange.select();
  7455. } catch (e) {}
  7456. }
  7457. });
  7458. }
  7459. //trace:1518 ff3.6body不够寛,会导致点击空白处无法获得焦点
  7460. if (browser.gecko && browser.version <= 10902) {
  7461. //修复ff3.6初始化进来,不能点击获得焦点
  7462. me.body.contentEditable = false;
  7463. setTimeout(function() {
  7464. me.body.contentEditable = true;
  7465. }, 100);
  7466. setInterval(function() {
  7467. me.body.style.height = me.iframe.offsetHeight - 20 + "px";
  7468. }, 100);
  7469. }
  7470. !options.isShow && me.setHide();
  7471. options.readonly && me.setDisabled();
  7472. },
  7473. /**
  7474. * 同步数据到编辑器所在的form
  7475. * 从编辑器的容器节点向上查找form元素,若找到,就同步编辑内容到找到的form里,为提交数据做准备,主要用于是手动提交的情况
  7476. * 后台取得数据的键值,使用你容器上的name属性,如果没有就使用参数里的textarea项
  7477. * @method sync
  7478. * @example
  7479. * ```javascript
  7480. * editor.sync();
  7481. * form.sumbit(); //form变量已经指向了form元素
  7482. * ```
  7483. */
  7484. /**
  7485. * 根据传入的formId,在页面上查找要同步数据的表单,若找到,就同步编辑内容到找到的form里,为提交数据做准备
  7486. * 后台取得数据的键值,该键值默认使用给定的编辑器容器的name属性,如果没有name属性则使用参数项里给定的“textarea”项
  7487. * @method sync
  7488. * @param { String } formID 指定一个要同步数据的form的id,编辑器的数据会同步到你指定form下
  7489. */
  7490. sync: function(formId) {
  7491. var me = this,
  7492. form = formId
  7493. ? document.getElementById(formId)
  7494. : domUtils.findParent(
  7495. me.iframe.parentNode,
  7496. function(node) {
  7497. return node.tagName == "FORM";
  7498. },
  7499. true
  7500. );
  7501. form && setValue(form, me);
  7502. },
  7503. /**
  7504. * 设置编辑器高度
  7505. * @method setHeight
  7506. * @remind 当配置项autoHeightEnabled为真时,该方法无效
  7507. * @param { Number } number 设置的高度值,纯数值,不带单位
  7508. * @example
  7509. * ```javascript
  7510. * editor.setHeight(number);
  7511. * ```
  7512. */
  7513. setHeight: function(height, notSetHeight) {
  7514. if (height !== parseInt(this.iframe.parentNode.style.height)) {
  7515. this.iframe.parentNode.style.height = height + "px";
  7516. }
  7517. !notSetHeight &&
  7518. (this.options.minFrameHeight = this.options.initialFrameHeight = height);
  7519. this.body.style.height = height + "px";
  7520. !notSetHeight && this.trigger("setHeight");
  7521. },
  7522. /**
  7523. * 为编辑器的编辑命令提供快捷键
  7524. * 这个接口是为插件扩展提供的接口,主要是为新添加的插件,如果需要添加快捷键,所提供的接口
  7525. * @method addshortcutkey
  7526. * @param { Object } keyset 命令名和快捷键键值对对象,多个按钮的快捷键用“+”分隔
  7527. * @example
  7528. * ```javascript
  7529. * editor.addshortcutkey({
  7530. * "Bold" : "ctrl+66",//^B
  7531. * "Italic" : "ctrl+73", //^I
  7532. * });
  7533. * ```
  7534. */
  7535. /**
  7536. * 这个接口是为插件扩展提供的接口,主要是为新添加的插件,如果需要添加快捷键,所提供的接口
  7537. * @method addshortcutkey
  7538. * @param { String } cmd 触发快捷键时,响应的命令
  7539. * @param { String } keys 快捷键的字符串,多个按钮用“+”分隔
  7540. * @example
  7541. * ```javascript
  7542. * editor.addshortcutkey("Underline", "ctrl+85"); //^U
  7543. * ```
  7544. */
  7545. addshortcutkey: function(cmd, keys) {
  7546. var obj = {};
  7547. if (keys) {
  7548. obj[cmd] = keys;
  7549. } else {
  7550. obj = cmd;
  7551. }
  7552. utils.extend(this.shortcutkeys, obj);
  7553. },
  7554. /**
  7555. * 对编辑器设置keydown事件监听,绑定快捷键和命令,当快捷键组合触发成功,会响应对应的命令
  7556. * @method _bindshortcutKeys
  7557. * @private
  7558. */
  7559. _bindshortcutKeys: function() {
  7560. var me = this,
  7561. shortcutkeys = this.shortcutkeys;
  7562. me.addListener("keydown", function(type, e) {
  7563. var keyCode = e.keyCode || e.which;
  7564. for (var i in shortcutkeys) {
  7565. var tmp = shortcutkeys[i].split(",");
  7566. for (var t = 0, ti; (ti = tmp[t++]); ) {
  7567. ti = ti.split(":");
  7568. var key = ti[0],
  7569. param = ti[1];
  7570. if (
  7571. /^(ctrl)(\+shift)?\+(\d+)$/.test(key.toLowerCase()) ||
  7572. /^(\d+)$/.test(key)
  7573. ) {
  7574. if (
  7575. ((RegExp.$1 == "ctrl" ? e.ctrlKey || e.metaKey : 0) &&
  7576. (RegExp.$2 != "" ? e[RegExp.$2.slice(1) + "Key"] : 1) &&
  7577. keyCode == RegExp.$3) ||
  7578. keyCode == RegExp.$1
  7579. ) {
  7580. if (me.queryCommandState(i, param) != -1)
  7581. me.execCommand(i, param);
  7582. domUtils.preventDefault(e);
  7583. }
  7584. }
  7585. }
  7586. }
  7587. });
  7588. },
  7589. /**
  7590. * 获取编辑器的内容
  7591. * @method getContent
  7592. * @warning 该方法获取到的是经过编辑器内置的过滤规则进行过滤后得到的内容
  7593. * @return { String } 编辑器的内容字符串, 如果编辑器的内容为空,或者是空的标签内容(如:”&lt;p&gt;&lt;br/&gt;&lt;/p&gt;“), 则返回空字符串
  7594. * @example
  7595. * ```javascript
  7596. * //编辑器html内容:<p>1<strong>2<em>34</em>5</strong>6</p>
  7597. * var content = editor.getContent(); //返回值:<p>1<strong>2<em>34</em>5</strong>6</p>
  7598. * ```
  7599. */
  7600. /**
  7601. * 获取编辑器的内容。 可以通过参数定义编辑器内置的判空规则
  7602. * @method getContent
  7603. * @param { Function } fn 自定的判空规则, 要求该方法返回一个boolean类型的值,
  7604. * 代表当前编辑器的内容是否空,
  7605. * 如果返回true, 则该方法将直接返回空字符串;如果返回false,则编辑器将返回
  7606. * 经过内置过滤规则处理后的内容。
  7607. * @remind 该方法在处理包含有初始化内容的时候能起到很好的作用。
  7608. * @warning 该方法获取到的是经过编辑器内置的过滤规则进行过滤后得到的内容
  7609. * @return { String } 编辑器的内容字符串
  7610. * @example
  7611. * ```javascript
  7612. * // editor 是一个编辑器的实例
  7613. * var content = editor.getContent( function ( editor ) {
  7614. * return editor.body.innerHTML === '欢迎使用UEditor'; //返回空字符串
  7615. * } );
  7616. * ```
  7617. */
  7618. getContent: function(cmd, fn, notSetCursor, ignoreBlank, formatter) {
  7619. var me = this;
  7620. if (cmd && utils.isFunction(cmd)) {
  7621. fn = cmd;
  7622. cmd = "";
  7623. }
  7624. if (fn ? !fn() : !this.hasContents()) {
  7625. return "";
  7626. }
  7627. me.fireEvent("beforegetcontent");
  7628. var root = UE.htmlparser(me.body.innerHTML, ignoreBlank);
  7629. me.filterOutputRule(root);
  7630. me.fireEvent("aftergetcontent", cmd, root);
  7631. return root.toHtml(formatter);
  7632. },
  7633. /**
  7634. * 取得完整的html代码,可以直接显示成完整的html文档
  7635. * @method getAllHtml
  7636. * @return { String } 编辑器的内容html文档字符串
  7637. * @eaxmple
  7638. * ```javascript
  7639. * editor.getAllHtml(); //返回格式大致是: <html><head>...</head><body>...</body></html>
  7640. * ```
  7641. */
  7642. getAllHtml: function() {
  7643. var me = this,
  7644. headHtml = [],
  7645. html = "";
  7646. me.fireEvent("getAllHtml", headHtml);
  7647. if (browser.ie && browser.version > 8) {
  7648. var headHtmlForIE9 = "";
  7649. utils.each(me.document.styleSheets, function(si) {
  7650. headHtmlForIE9 += si.href
  7651. ? '<link rel="stylesheet" type="text/css" href="' + si.href + '" />'
  7652. : "<style>" + si.cssText + "</style>";
  7653. });
  7654. utils.each(me.document.getElementsByTagName("script"), function(si) {
  7655. headHtmlForIE9 += si.outerHTML;
  7656. });
  7657. }
  7658. return (
  7659. "<html><head>" +
  7660. (me.options.charset
  7661. ? '<meta http-equiv="Content-Type" content="text/html; charset=' +
  7662. me.options.charset +
  7663. '"/>'
  7664. : "") +
  7665. (headHtmlForIE9 ||
  7666. me.document.getElementsByTagName("head")[0].innerHTML) +
  7667. headHtml.join("\n") +
  7668. "</head>" +
  7669. "<body " +
  7670. (ie && browser.version < 9 ? 'class="view"' : "") +
  7671. ">" +
  7672. me.getContent(null, null, true) +
  7673. "</body></html>"
  7674. );
  7675. },
  7676. /**
  7677. * 得到编辑器的纯文本内容,但会保留段落格式
  7678. * @method getPlainTxt
  7679. * @return { String } 编辑器带段落格式的纯文本内容字符串
  7680. * @example
  7681. * ```javascript
  7682. * //编辑器html内容:<p><strong>1</strong></p><p><strong>2</strong></p>
  7683. * console.log(editor.getPlainTxt()); //输出:"1\n2\n
  7684. * ```
  7685. */
  7686. getPlainTxt: function() {
  7687. var reg = new RegExp(domUtils.fillChar, "g"),
  7688. html = this.body.innerHTML.replace(/[\n\r]/g, ""); //ie要先去了\n在处理
  7689. html = html
  7690. .replace(/<(p|div)[^>]*>(<br\/?>|&nbsp;)<\/\1>/gi, "\n")
  7691. .replace(/<br\/?>/gi, "\n")
  7692. .replace(/<[^>/]+>/g, "")
  7693. .replace(/(\n)?<\/([^>]+)>/g, function(a, b, c) {
  7694. return dtd.$block[c] ? "\n" : b ? b : "";
  7695. });
  7696. //取出来的空格会有c2a0会变成乱码,处理这种情况\u00a0
  7697. return html
  7698. .replace(reg, "")
  7699. .replace(/\u00a0/g, " ")
  7700. .replace(/&nbsp;/g, " ");
  7701. },
  7702. /**
  7703. * 获取编辑器中的纯文本内容,没有段落格式
  7704. * @method getContentTxt
  7705. * @return { String } 编辑器不带段落格式的纯文本内容字符串
  7706. * @example
  7707. * ```javascript
  7708. * //编辑器html内容:<p><strong>1</strong></p><p><strong>2</strong></p>
  7709. * console.log(editor.getPlainTxt()); //输出:"12
  7710. * ```
  7711. */
  7712. getContentTxt: function() {
  7713. var reg = new RegExp(domUtils.fillChar, "g");
  7714. //取出来的空格会有c2a0会变成乱码,处理这种情况\u00a0
  7715. return this.body[browser.ie ? "innerText" : "textContent"]
  7716. .replace(reg, "")
  7717. .replace(/\u00a0/g, " ");
  7718. },
  7719. /**
  7720. * 设置编辑器的内容,可修改编辑器当前的html内容
  7721. * @method setContent
  7722. * @warning 通过该方法插入的内容,是经过编辑器内置的过滤规则进行过滤后得到的内容
  7723. * @warning 该方法会触发selectionchange事件
  7724. * @param { String } html 要插入的html内容
  7725. * @example
  7726. * ```javascript
  7727. * editor.getContent('<p>test</p>');
  7728. * ```
  7729. */
  7730. /**
  7731. * 设置编辑器的内容,可修改编辑器当前的html内容
  7732. * @method setContent
  7733. * @warning 通过该方法插入的内容,是经过编辑器内置的过滤规则进行过滤后得到的内容
  7734. * @warning 该方法会触发selectionchange事件
  7735. * @param { String } html 要插入的html内容
  7736. * @param { Boolean } isAppendTo 若传入true,不清空原来的内容,在最后插入内容,否则,清空内容再插入
  7737. * @example
  7738. * ```javascript
  7739. * //假设设置前的编辑器内容是 <p>old text</p>
  7740. * editor.setContent('<p>new text</p>', true); //插入的结果是<p>old text</p><p>new text</p>
  7741. * ```
  7742. */
  7743. setContent: function(html, isAppendTo, notFireSelectionchange) {
  7744. var me = this;
  7745. me.fireEvent("beforesetcontent", html);
  7746. var root = UE.htmlparser(html);
  7747. me.filterInputRule(root);
  7748. html = root.toHtml();
  7749. me.body.innerHTML = (isAppendTo ? me.body.innerHTML : "") + html;
  7750. function isCdataDiv(node) {
  7751. return node.tagName == "DIV" && node.getAttribute("cdata_tag");
  7752. }
  7753. //给文本或者inline节点套p标签
  7754. if (me.options.enterTag == "p") {
  7755. var child = this.body.firstChild,
  7756. tmpNode;
  7757. if (
  7758. !child ||
  7759. (child.nodeType == 1 &&
  7760. (dtd.$cdata[child.tagName] ||
  7761. isCdataDiv(child) ||
  7762. domUtils.isCustomeNode(child)) &&
  7763. child === this.body.lastChild)
  7764. ) {
  7765. this.body.innerHTML =
  7766. "<p>" +
  7767. (browser.ie ? "&nbsp;" : "<br/>") +
  7768. "</p>" +
  7769. this.body.innerHTML;
  7770. } else {
  7771. var p = me.document.createElement("p");
  7772. while (child) {
  7773. while (
  7774. child &&
  7775. (child.nodeType == 3 ||
  7776. (child.nodeType == 1 &&
  7777. dtd.p[child.tagName] &&
  7778. !dtd.$cdata[child.tagName]))
  7779. ) {
  7780. tmpNode = child.nextSibling;
  7781. p.appendChild(child);
  7782. child = tmpNode;
  7783. }
  7784. if (p.firstChild) {
  7785. if (!child) {
  7786. me.body.appendChild(p);
  7787. break;
  7788. } else {
  7789. child.parentNode.insertBefore(p, child);
  7790. p = me.document.createElement("p");
  7791. }
  7792. }
  7793. child = child.nextSibling;
  7794. }
  7795. }
  7796. }
  7797. me.fireEvent("aftersetcontent");
  7798. me.fireEvent("contentchange");
  7799. !notFireSelectionchange && me._selectionChange();
  7800. //清除保存的选区
  7801. me._bakRange = me._bakIERange = me._bakNativeRange = null;
  7802. //trace:1742 setContent后gecko能得到焦点问题
  7803. var geckoSel;
  7804. if (browser.gecko && (geckoSel = this.selection.getNative())) {
  7805. geckoSel.removeAllRanges();
  7806. }
  7807. if (me.options.autoSyncData) {
  7808. me.form && setValue(me.form, me);
  7809. }
  7810. },
  7811. /**
  7812. * 让编辑器获得焦点,默认focus到编辑器头部
  7813. * @method focus
  7814. * @example
  7815. * ```javascript
  7816. * editor.focus()
  7817. * ```
  7818. */
  7819. /**
  7820. * 让编辑器获得焦点,toEnd确定focus位置
  7821. * @method focus
  7822. * @param { Boolean } toEnd 默认focus到编辑器头部,toEnd为true时focus到内容尾部
  7823. * @example
  7824. * ```javascript
  7825. * editor.focus(true)
  7826. * ```
  7827. */
  7828. focus: function(toEnd) {
  7829. try {
  7830. var me = this,
  7831. rng = me.selection.getRange();
  7832. if (toEnd) {
  7833. var node = me.body.lastChild;
  7834. if (node && node.nodeType == 1 && !dtd.$empty[node.tagName]) {
  7835. if (domUtils.isEmptyBlock(node)) {
  7836. rng.setStartAtFirst(node);
  7837. } else {
  7838. rng.setStartAtLast(node);
  7839. }
  7840. rng.collapse(true);
  7841. }
  7842. rng.setCursor(true);
  7843. } else {
  7844. if (
  7845. !rng.collapsed &&
  7846. domUtils.isBody(rng.startContainer) &&
  7847. rng.startOffset == 0
  7848. ) {
  7849. var node = me.body.firstChild;
  7850. if (node && node.nodeType == 1 && !dtd.$empty[node.tagName]) {
  7851. rng.setStartAtFirst(node).collapse(true);
  7852. }
  7853. }
  7854. rng.select(true);
  7855. }
  7856. this.fireEvent("focus selectionchange");
  7857. } catch (e) {}
  7858. },
  7859. isFocus: function() {
  7860. return this.selection.isFocus();
  7861. },
  7862. blur: function() {
  7863. var sel = this.selection.getNative();
  7864. if (sel.empty && browser.ie) {
  7865. var nativeRng = document.body.createTextRange();
  7866. nativeRng.moveToElementText(document.body);
  7867. nativeRng.collapse(true);
  7868. nativeRng.select();
  7869. sel.empty();
  7870. } else {
  7871. sel.removeAllRanges();
  7872. }
  7873. //this.fireEvent('blur selectionchange');
  7874. },
  7875. /**
  7876. * 初始化UE事件及部分事件代理
  7877. * @method _initEvents
  7878. * @private
  7879. */
  7880. _initEvents: function() {
  7881. var me = this,
  7882. doc = me.document,
  7883. win = me.window;
  7884. me._proxyDomEvent = utils.bind(me._proxyDomEvent, me);
  7885. domUtils.on(
  7886. doc,
  7887. [
  7888. "click",
  7889. "contextmenu",
  7890. "mousedown",
  7891. "keydown",
  7892. "keyup",
  7893. "keypress",
  7894. "mouseup",
  7895. "mouseover",
  7896. "mouseout",
  7897. "selectstart"
  7898. ],
  7899. me._proxyDomEvent
  7900. );
  7901. domUtils.on(win, ["focus", "blur"], me._proxyDomEvent);
  7902. domUtils.on(me.body, "drop", function(e) {
  7903. //阻止ff下默认的弹出新页面打开图片
  7904. if (browser.gecko && e.stopPropagation) {
  7905. e.stopPropagation();
  7906. }
  7907. me.fireEvent("contentchange");
  7908. });
  7909. domUtils.on(doc, ["mouseup", "keydown"], function(evt) {
  7910. //特殊键不触发selectionchange
  7911. if (
  7912. evt.type == "keydown" &&
  7913. (evt.ctrlKey || evt.metaKey || evt.shiftKey || evt.altKey)
  7914. ) {
  7915. return;
  7916. }
  7917. if (evt.button == 2) return;
  7918. me._selectionChange(250, evt);
  7919. });
  7920. },
  7921. /**
  7922. * 触发事件代理
  7923. * @method _proxyDomEvent
  7924. * @private
  7925. * @return { * } fireEvent的返回值
  7926. * @see UE.EventBase:fireEvent(String)
  7927. */
  7928. _proxyDomEvent: function(evt) {
  7929. if (
  7930. this.fireEvent("before" + evt.type.replace(/^on/, "").toLowerCase()) ===
  7931. false
  7932. ) {
  7933. return false;
  7934. }
  7935. if (this.fireEvent(evt.type.replace(/^on/, ""), evt) === false) {
  7936. return false;
  7937. }
  7938. return this.fireEvent(
  7939. "after" + evt.type.replace(/^on/, "").toLowerCase()
  7940. );
  7941. },
  7942. /**
  7943. * 变化选区
  7944. * @method _selectionChange
  7945. * @private
  7946. */
  7947. _selectionChange: function(delay, evt) {
  7948. var me = this;
  7949. //有光标才做selectionchange 为了解决未focus时点击source不能触发更改工具栏状态的问题(source命令notNeedUndo=1)
  7950. // if ( !me.selection.isFocus() ){
  7951. // return;
  7952. // }
  7953. var hackForMouseUp = false;
  7954. var mouseX, mouseY;
  7955. if (browser.ie && browser.version < 9 && evt && evt.type == "mouseup") {
  7956. var range = this.selection.getRange();
  7957. if (!range.collapsed) {
  7958. hackForMouseUp = true;
  7959. mouseX = evt.clientX;
  7960. mouseY = evt.clientY;
  7961. }
  7962. }
  7963. clearTimeout(_selectionChangeTimer);
  7964. _selectionChangeTimer = setTimeout(function() {
  7965. if (!me.selection || !me.selection.getNative()) {
  7966. return;
  7967. }
  7968. //修复一个IE下的bug: 鼠标点击一段已选择的文本中间时,可能在mouseup后的一段时间内取到的range是在selection的type为None下的错误值.
  7969. //IE下如果用户是拖拽一段已选择文本,则不会触发mouseup事件,所以这里的特殊处理不会对其有影响
  7970. var ieRange;
  7971. if (hackForMouseUp && me.selection.getNative().type == "None") {
  7972. ieRange = me.document.body.createTextRange();
  7973. try {
  7974. ieRange.moveToPoint(mouseX, mouseY);
  7975. } catch (ex) {
  7976. ieRange = null;
  7977. }
  7978. }
  7979. var bakGetIERange;
  7980. if (ieRange) {
  7981. bakGetIERange = me.selection.getIERange;
  7982. me.selection.getIERange = function() {
  7983. return ieRange;
  7984. };
  7985. }
  7986. me.selection.cache();
  7987. if (bakGetIERange) {
  7988. me.selection.getIERange = bakGetIERange;
  7989. }
  7990. if (me.selection._cachedRange && me.selection._cachedStartElement) {
  7991. me.fireEvent("beforeselectionchange");
  7992. // 第二个参数causeByUi为true代表由用户交互造成的selectionchange.
  7993. me.fireEvent("selectionchange", !!evt);
  7994. me.fireEvent("afterselectionchange");
  7995. me.selection.clear();
  7996. }
  7997. }, delay || 50);
  7998. },
  7999. /**
  8000. * 执行编辑命令
  8001. * @method _callCmdFn
  8002. * @private
  8003. * @param { String } fnName 函数名称
  8004. * @param { * } args 传给命令函数的参数
  8005. * @return { * } 返回命令函数运行的返回值
  8006. */
  8007. _callCmdFn: function(fnName, args) {
  8008. var cmdName = args[0].toLowerCase(),
  8009. cmd,
  8010. cmdFn;
  8011. cmd = this.commands[cmdName] || UE.commands[cmdName];
  8012. cmdFn = cmd && cmd[fnName];
  8013. //没有querycommandstate或者没有command的都默认返回0
  8014. if ((!cmd || !cmdFn) && fnName == "queryCommandState") {
  8015. return 0;
  8016. } else if (cmdFn) {
  8017. return cmdFn.apply(this, args);
  8018. }
  8019. },
  8020. /**
  8021. * 执行编辑命令cmdName,完成富文本编辑效果
  8022. * @method execCommand
  8023. * @param { String } cmdName 需要执行的命令
  8024. * @remind 具体命令的使用请参考<a href="#COMMAND.LIST">命令列表</a>
  8025. * @return { * } 返回命令函数运行的返回值
  8026. * @example
  8027. * ```javascript
  8028. * editor.execCommand(cmdName);
  8029. * ```
  8030. */
  8031. execCommand: function(cmdName) {
  8032. cmdName = cmdName.toLowerCase();
  8033. var me = this,
  8034. result,
  8035. cmd = me.commands[cmdName] || UE.commands[cmdName];
  8036. if (!cmd || !cmd.execCommand) {
  8037. return null;
  8038. }
  8039. if (!cmd.notNeedUndo && !me.__hasEnterExecCommand) {
  8040. me.__hasEnterExecCommand = true;
  8041. if (me.queryCommandState.apply(me, arguments) != -1) {
  8042. me.fireEvent("saveScene");
  8043. me.fireEvent.apply(
  8044. me,
  8045. ["beforeexeccommand", cmdName].concat(arguments)
  8046. );
  8047. result = this._callCmdFn("execCommand", arguments);
  8048. //保存场景时,做了内容对比,再看是否进行contentchange触发,这里多触发了一次,去掉
  8049. // (!cmd.ignoreContentChange && !me._ignoreContentChange) && me.fireEvent('contentchange');
  8050. me.fireEvent.apply(
  8051. me,
  8052. ["afterexeccommand", cmdName].concat(arguments)
  8053. );
  8054. me.fireEvent("saveScene");
  8055. }
  8056. me.__hasEnterExecCommand = false;
  8057. } else {
  8058. result = this._callCmdFn("execCommand", arguments);
  8059. !me.__hasEnterExecCommand &&
  8060. !cmd.ignoreContentChange &&
  8061. !me._ignoreContentChange &&
  8062. me.fireEvent("contentchange");
  8063. }
  8064. !me.__hasEnterExecCommand &&
  8065. !cmd.ignoreContentChange &&
  8066. !me._ignoreContentChange &&
  8067. me._selectionChange();
  8068. return result;
  8069. },
  8070. /**
  8071. * 根据传入的command命令,查选编辑器当前的选区,返回命令的状态
  8072. * @method queryCommandState
  8073. * @param { String } cmdName 需要查询的命令名称
  8074. * @remind 具体命令的使用请参考<a href="#COMMAND.LIST">命令列表</a>
  8075. * @return { Number } number 返回放前命令的状态,返回值三种情况:(-1|0|1)
  8076. * @example
  8077. * ```javascript
  8078. * editor.queryCommandState(cmdName) => (-1|0|1)
  8079. * ```
  8080. * @see COMMAND.LIST
  8081. */
  8082. queryCommandState: function(cmdName) {
  8083. return this._callCmdFn("queryCommandState", arguments);
  8084. },
  8085. /**
  8086. * 根据传入的command命令,查选编辑器当前的选区,根据命令返回相关的值
  8087. * @method queryCommandValue
  8088. * @param { String } cmdName 需要查询的命令名称
  8089. * @remind 具体命令的使用请参考<a href="#COMMAND.LIST">命令列表</a>
  8090. * @remind 只有部分插件有此方法
  8091. * @return { * } 返回每个命令特定的当前状态值
  8092. * @grammar editor.queryCommandValue(cmdName) => {*}
  8093. * @see COMMAND.LIST
  8094. */
  8095. queryCommandValue: function(cmdName) {
  8096. return this._callCmdFn("queryCommandValue", arguments);
  8097. },
  8098. /**
  8099. * 检查编辑区域中是否有内容
  8100. * @method hasContents
  8101. * @remind 默认有文本内容,或者有以下节点都不认为是空
  8102. * table,ul,ol,dl,iframe,area,base,col,hr,img,embed,input,link,meta,param
  8103. * @return { Boolean } 检查有内容返回true,否则返回false
  8104. * @example
  8105. * ```javascript
  8106. * editor.hasContents()
  8107. * ```
  8108. */
  8109. /**
  8110. * 检查编辑区域中是否有内容,若包含参数tags中的节点类型,直接返回true
  8111. * @method hasContents
  8112. * @param { Array } tags 传入数组判断时用到的节点类型
  8113. * @return { Boolean } 若文档中包含tags数组里对应的tag,返回true,否则返回false
  8114. * @example
  8115. * ```javascript
  8116. * editor.hasContents(['span']);
  8117. * ```
  8118. */
  8119. hasContents: function(tags) {
  8120. if (tags) {
  8121. for (var i = 0, ci; (ci = tags[i++]); ) {
  8122. if (this.document.getElementsByTagName(ci).length > 0) {
  8123. return true;
  8124. }
  8125. }
  8126. }
  8127. if (!domUtils.isEmptyBlock(this.body)) {
  8128. return true;
  8129. }
  8130. //随时添加,定义的特殊标签如果存在,不能认为是空
  8131. tags = ["div"];
  8132. for (i = 0; (ci = tags[i++]); ) {
  8133. var nodes = domUtils.getElementsByTagName(this.document, ci);
  8134. for (var n = 0, cn; (cn = nodes[n++]); ) {
  8135. if (domUtils.isCustomeNode(cn)) {
  8136. return true;
  8137. }
  8138. }
  8139. }
  8140. return false;
  8141. },
  8142. /**
  8143. * 重置编辑器,可用来做多个tab使用同一个编辑器实例
  8144. * @method reset
  8145. * @remind 此方法会清空编辑器内容,清空回退列表,会触发reset事件
  8146. * @example
  8147. * ```javascript
  8148. * editor.reset()
  8149. * ```
  8150. */
  8151. reset: function() {
  8152. this.fireEvent("reset");
  8153. },
  8154. /**
  8155. * 设置当前编辑区域可以编辑
  8156. * @method setEnabled
  8157. * @example
  8158. * ```javascript
  8159. * editor.setEnabled()
  8160. * ```
  8161. */
  8162. setEnabled: function() {
  8163. var me = this,
  8164. range;
  8165. if (me.body.contentEditable == "false") {
  8166. me.body.contentEditable = true;
  8167. range = me.selection.getRange();
  8168. //有可能内容丢失了
  8169. try {
  8170. range.moveToBookmark(me.lastBk);
  8171. delete me.lastBk;
  8172. } catch (e) {
  8173. range.setStartAtFirst(me.body).collapse(true);
  8174. }
  8175. range.select(true);
  8176. if (me.bkqueryCommandState) {
  8177. me.queryCommandState = me.bkqueryCommandState;
  8178. delete me.bkqueryCommandState;
  8179. }
  8180. if (me.bkqueryCommandValue) {
  8181. me.queryCommandValue = me.bkqueryCommandValue;
  8182. delete me.bkqueryCommandValue;
  8183. }
  8184. me.fireEvent("selectionchange");
  8185. }
  8186. },
  8187. enable: function() {
  8188. return this.setEnabled();
  8189. },
  8190. /** 设置当前编辑区域不可编辑
  8191. * @method setDisabled
  8192. */
  8193. /** 设置当前编辑区域不可编辑,except中的命令除外
  8194. * @method setDisabled
  8195. * @param { String } except 例外命令的字符串
  8196. * @remind 即使设置了disable,此处配置的例外命令仍然可以执行
  8197. * @example
  8198. * ```javascript
  8199. * editor.setDisabled('bold'); //禁用工具栏中除加粗之外的所有功能
  8200. * ```
  8201. */
  8202. /** 设置当前编辑区域不可编辑,except中的命令除外
  8203. * @method setDisabled
  8204. * @param { Array } except 例外命令的字符串数组,数组中的命令仍然可以执行
  8205. * @remind 即使设置了disable,此处配置的例外命令仍然可以执行
  8206. * @example
  8207. * ```javascript
  8208. * editor.setDisabled(['bold','insertimage']); //禁用工具栏中除加粗和插入图片之外的所有功能
  8209. * ```
  8210. */
  8211. setDisabled: function(except) {
  8212. var me = this;
  8213. except = except ? (utils.isArray(except) ? except : [except]) : [];
  8214. if (me.body.contentEditable == "true") {
  8215. if (!me.lastBk) {
  8216. me.lastBk = me.selection.getRange().createBookmark(true);
  8217. }
  8218. me.body.contentEditable = false;
  8219. me.bkqueryCommandState = me.queryCommandState;
  8220. me.bkqueryCommandValue = me.queryCommandValue;
  8221. me.queryCommandState = function(type) {
  8222. if (utils.indexOf(except, type) != -1) {
  8223. return me.bkqueryCommandState.apply(me, arguments);
  8224. }
  8225. return -1;
  8226. };
  8227. me.queryCommandValue = function(type) {
  8228. if (utils.indexOf(except, type) != -1) {
  8229. return me.bkqueryCommandValue.apply(me, arguments);
  8230. }
  8231. return null;
  8232. };
  8233. me.fireEvent("selectionchange");
  8234. }
  8235. },
  8236. disable: function(except) {
  8237. return this.setDisabled(except);
  8238. },
  8239. /**
  8240. * 设置默认内容
  8241. * @method _setDefaultContent
  8242. * @private
  8243. * @param { String } cont 要存入的内容
  8244. */
  8245. _setDefaultContent: (function() {
  8246. function clear() {
  8247. var me = this;
  8248. if (me.document.getElementById("initContent")) {
  8249. me.body.innerHTML = "<p>" + (ie ? "" : "<br/>") + "</p>";
  8250. me.removeListener("firstBeforeExecCommand focus", clear);
  8251. setTimeout(function() {
  8252. me.focus();
  8253. me._selectionChange();
  8254. }, 0);
  8255. }
  8256. }
  8257. return function(cont) {
  8258. var me = this;
  8259. me.body.innerHTML = '<p id="initContent">' + cont + "</p>";
  8260. me.addListener("firstBeforeExecCommand focus", clear);
  8261. };
  8262. })(),
  8263. /**
  8264. * 显示编辑器
  8265. * @method setShow
  8266. * @example
  8267. * ```javascript
  8268. * editor.setShow()
  8269. * ```
  8270. */
  8271. setShow: function() {
  8272. var me = this,
  8273. range = me.selection.getRange();
  8274. if (me.container.style.display == "none") {
  8275. //有可能内容丢失了
  8276. try {
  8277. range.moveToBookmark(me.lastBk);
  8278. delete me.lastBk;
  8279. } catch (e) {
  8280. range.setStartAtFirst(me.body).collapse(true);
  8281. }
  8282. //ie下focus实效,所以做了个延迟
  8283. setTimeout(function() {
  8284. range.select(true);
  8285. }, 100);
  8286. me.container.style.display = "";
  8287. }
  8288. },
  8289. show: function() {
  8290. return this.setShow();
  8291. },
  8292. /**
  8293. * 隐藏编辑器
  8294. * @method setHide
  8295. * @example
  8296. * ```javascript
  8297. * editor.setHide()
  8298. * ```
  8299. */
  8300. setHide: function() {
  8301. var me = this;
  8302. if (!me.lastBk) {
  8303. me.lastBk = me.selection.getRange().createBookmark(true);
  8304. }
  8305. me.container.style.display = "none";
  8306. },
  8307. hide: function() {
  8308. return this.setHide();
  8309. },
  8310. /**
  8311. * 根据指定的路径,获取对应的语言资源
  8312. * @method getLang
  8313. * @param { String } path 路径根据的是lang目录下的语言文件的路径结构
  8314. * @return { Object | String } 根据路径返回语言资源的Json格式对象或者语言字符串
  8315. * @example
  8316. * ```javascript
  8317. * editor.getLang('contextMenu.delete'); //如果当前是中文,那返回是的是'删除'
  8318. * ```
  8319. */
  8320. getLang: function(path) {
  8321. var lang = UE.I18N[this.options.lang];
  8322. if (!lang) {
  8323. throw Error("not import language file");
  8324. }
  8325. path = (path || "").split(".");
  8326. for (var i = 0, ci; (ci = path[i++]); ) {
  8327. lang = lang[ci];
  8328. if (!lang) break;
  8329. }
  8330. return lang;
  8331. },
  8332. /**
  8333. * 计算编辑器html内容字符串的长度
  8334. * @method getContentLength
  8335. * @return { Number } 返回计算的长度
  8336. * @example
  8337. * ```javascript
  8338. * //编辑器html内容<p><strong>132</strong></p>
  8339. * editor.getContentLength() //返回27
  8340. * ```
  8341. */
  8342. /**
  8343. * 计算编辑器当前纯文本内容的长度
  8344. * @method getContentLength
  8345. * @param { Boolean } ingoneHtml 传入true时,只按照纯文本来计算
  8346. * @return { Number } 返回计算的长度,内容中有hr/img/iframe标签,长度加1
  8347. * @example
  8348. * ```javascript
  8349. * //编辑器html内容<p><strong>132</strong></p>
  8350. * editor.getContentLength() //返回3
  8351. * ```
  8352. */
  8353. getContentLength: function(ingoneHtml, tagNames) {
  8354. var count = this.getContent(false, false, true).length;
  8355. if (ingoneHtml) {
  8356. tagNames = (tagNames || []).concat(["hr", "img", "iframe"]);
  8357. count = this.getContentTxt().replace(/[\t\r\n]+/g, "").length;
  8358. for (var i = 0, ci; (ci = tagNames[i++]); ) {
  8359. count += this.document.getElementsByTagName(ci).length;
  8360. }
  8361. }
  8362. return count;
  8363. },
  8364. /**
  8365. * 注册输入过滤规则
  8366. * @method addInputRule
  8367. * @param { Function } rule 要添加的过滤规则
  8368. * @example
  8369. * ```javascript
  8370. * editor.addInputRule(function(root){
  8371. * $.each(root.getNodesByTagName('div'),function(i,node){
  8372. * node.tagName="p";
  8373. * });
  8374. * });
  8375. * ```
  8376. */
  8377. addInputRule: function(rule) {
  8378. this.inputRules.push(rule);
  8379. },
  8380. /**
  8381. * 执行注册的过滤规则
  8382. * @method filterInputRule
  8383. * @param { UE.uNode } root 要过滤的uNode节点
  8384. * @remind 执行editor.setContent方法和执行'inserthtml'命令后,会运行该过滤函数
  8385. * @example
  8386. * ```javascript
  8387. * editor.filterInputRule(editor.body);
  8388. * ```
  8389. * @see UE.Editor:addInputRule
  8390. */
  8391. filterInputRule: function(root) {
  8392. for (var i = 0, ci; (ci = this.inputRules[i++]); ) {
  8393. ci.call(this, root);
  8394. }
  8395. },
  8396. /**
  8397. * 注册输出过滤规则
  8398. * @method addOutputRule
  8399. * @param { Function } rule 要添加的过滤规则
  8400. * @example
  8401. * ```javascript
  8402. * editor.addOutputRule(function(root){
  8403. * $.each(root.getNodesByTagName('p'),function(i,node){
  8404. * node.tagName="div";
  8405. * });
  8406. * });
  8407. * ```
  8408. */
  8409. addOutputRule: function(rule) {
  8410. this.outputRules.push(rule);
  8411. },
  8412. /**
  8413. * 根据输出过滤规则,过滤编辑器内容
  8414. * @method filterOutputRule
  8415. * @remind 执行editor.getContent方法的时候,会先运行该过滤函数
  8416. * @param { UE.uNode } root 要过滤的uNode节点
  8417. * @example
  8418. * ```javascript
  8419. * editor.filterOutputRule(editor.body);
  8420. * ```
  8421. * @see UE.Editor:addOutputRule
  8422. */
  8423. filterOutputRule: function(root) {
  8424. for (var i = 0, ci; (ci = this.outputRules[i++]); ) {
  8425. ci.call(this, root);
  8426. }
  8427. },
  8428. /**
  8429. * 根据action名称获取请求的路径
  8430. * @method getActionUrl
  8431. * @remind 假如没有设置serverUrl,会根据imageUrl设置默认的controller路径
  8432. * @param { String } action action名称
  8433. * @example
  8434. * ```javascript
  8435. * editor.getActionUrl('config'); //返回 "/ueditor/php/controller.php?action=config"
  8436. * editor.getActionUrl('image'); //返回 "/ueditor/php/controller.php?action=uplaodimage"
  8437. * editor.getActionUrl('scrawl'); //返回 "/ueditor/php/controller.php?action=uplaodscrawl"
  8438. * editor.getActionUrl('imageManager'); //返回 "/ueditor/php/controller.php?action=listimage"
  8439. * ```
  8440. */
  8441. getActionUrl: function(action) {
  8442. var actionName = this.getOpt(action) || action,
  8443. imageUrl = this.getOpt("imageUrl"),
  8444. serverUrl = this.getOpt("serverUrl");
  8445. if (!serverUrl && imageUrl) {
  8446. serverUrl = imageUrl.replace(/^(.*[\/]).+([\.].+)$/, "$1controller$2");
  8447. }
  8448. if (serverUrl) {
  8449. serverUrl =
  8450. serverUrl +
  8451. (serverUrl.indexOf("?") == -1 ? "?" : "&") +
  8452. "action=" +
  8453. (actionName || "");
  8454. return utils.formatUrl(serverUrl);
  8455. } else {
  8456. return "";
  8457. }
  8458. }
  8459. };
  8460. utils.inherits(Editor, EventBase);
  8461. })();
  8462. // core/Editor.defaultoptions.js
  8463. //维护编辑器一下默认的不在插件中的配置项
  8464. UE.Editor.defaultOptions = function(editor) {
  8465. var _url = editor.options.UEDITOR_HOME_URL;
  8466. return {
  8467. isShow: true,
  8468. initialContent: "",
  8469. initialStyle: "",
  8470. autoClearinitialContent: false,
  8471. iframeCssUrl: _url + "themes/iframe.css",
  8472. textarea: "editorValue",
  8473. focus: false,
  8474. focusInEnd: true,
  8475. autoClearEmptyNode: true,
  8476. fullscreen: false,
  8477. readonly: false,
  8478. zIndex: 999,
  8479. imagePopup: true,
  8480. enterTag: "p",
  8481. customDomain: false,
  8482. lang: "zh-cn",
  8483. langPath: _url + "lang/",
  8484. theme: "default",
  8485. themePath: _url + "themes/",
  8486. allHtmlEnabled: false,
  8487. scaleEnabled: false,
  8488. tableNativeEditInFF: false,
  8489. autoSyncData: true,
  8490. fileNameFormat: "{time}{rand:6}"
  8491. };
  8492. };
  8493. // core/loadconfig.js
  8494. (function() {
  8495. UE.Editor.prototype.loadServerConfig = function() {
  8496. var me = this;
  8497. setTimeout(function() {
  8498. try {
  8499. me.options.imageUrl &&
  8500. me.setOpt(
  8501. "serverUrl",
  8502. me.options.imageUrl.replace(
  8503. /^(.*[\/]).+([\.].+)$/,
  8504. "$1controller$2"
  8505. )
  8506. );
  8507. var configUrl = me.getActionUrl("config"),
  8508. isJsonp = utils.isCrossDomainUrl(configUrl);
  8509. /* 发出ajax请求 */
  8510. me._serverConfigLoaded = false;
  8511. configUrl &&
  8512. UE.ajax.request(configUrl, {
  8513. method: "GET",
  8514. dataType: isJsonp ? "jsonp" : "",
  8515. onsuccess: function(r) {
  8516. try {
  8517. var config = isJsonp ? r : eval("(" + r.responseText + ")");
  8518. utils.extend(me.options, config);
  8519. me.fireEvent("serverConfigLoaded");
  8520. me._serverConfigLoaded = true;
  8521. } catch (e) {
  8522. showErrorMsg(me.getLang("loadconfigFormatError"));
  8523. }
  8524. },
  8525. onerror: function() {
  8526. showErrorMsg(me.getLang("loadconfigHttpError"));
  8527. }
  8528. });
  8529. } catch (e) {
  8530. showErrorMsg(me.getLang("loadconfigError"));
  8531. }
  8532. });
  8533. function showErrorMsg(msg) {
  8534. console && console.error(msg);
  8535. //me.fireEvent('showMessage', {
  8536. // 'title': msg,
  8537. // 'type': 'error'
  8538. //});
  8539. }
  8540. };
  8541. UE.Editor.prototype.isServerConfigLoaded = function() {
  8542. var me = this;
  8543. return me._serverConfigLoaded || false;
  8544. };
  8545. UE.Editor.prototype.afterConfigReady = function(handler) {
  8546. if (!handler || !utils.isFunction(handler)) return;
  8547. var me = this;
  8548. var readyHandler = function() {
  8549. handler.apply(me, arguments);
  8550. me.removeListener("serverConfigLoaded", readyHandler);
  8551. };
  8552. if (me.isServerConfigLoaded()) {
  8553. handler.call(me, "serverConfigLoaded");
  8554. } else {
  8555. me.addListener("serverConfigLoaded", readyHandler);
  8556. }
  8557. };
  8558. })();
  8559. // core/ajax.js
  8560. /**
  8561. * @file
  8562. * @module UE.ajax
  8563. * @since 1.2.6.1
  8564. */
  8565. /**
  8566. * 提供对ajax请求的支持
  8567. * @module UE.ajax
  8568. */
  8569. UE.ajax = (function() {
  8570. //创建一个ajaxRequest对象
  8571. var fnStr = "XMLHttpRequest()";
  8572. try {
  8573. new ActiveXObject("Msxml2.XMLHTTP");
  8574. fnStr = "ActiveXObject('Msxml2.XMLHTTP')";
  8575. } catch (e) {
  8576. try {
  8577. new ActiveXObject("Microsoft.XMLHTTP");
  8578. fnStr = "ActiveXObject('Microsoft.XMLHTTP')";
  8579. } catch (e) {}
  8580. }
  8581. var creatAjaxRequest = new Function("return new " + fnStr);
  8582. /**
  8583. * 将json参数转化成适合ajax提交的参数列表
  8584. * @param json
  8585. */
  8586. function json2str(json) {
  8587. var strArr = [];
  8588. for (var i in json) {
  8589. //忽略默认的几个参数
  8590. if (
  8591. i == "method" ||
  8592. i == "timeout" ||
  8593. i == "async" ||
  8594. i == "dataType" ||
  8595. i == "callback"
  8596. )
  8597. continue;
  8598. //忽略控制
  8599. if (json[i] == undefined || json[i] == null) continue;
  8600. //传递过来的对象和函数不在提交之列
  8601. if (
  8602. !(
  8603. (typeof json[i]).toLowerCase() == "function" ||
  8604. (typeof json[i]).toLowerCase() == "object"
  8605. )
  8606. ) {
  8607. strArr.push(encodeURIComponent(i) + "=" + encodeURIComponent(json[i]));
  8608. } else if (utils.isArray(json[i])) {
  8609. //支持传数组内容
  8610. for (var j = 0; j < json[i].length; j++) {
  8611. strArr.push(
  8612. encodeURIComponent(i) + "[]=" + encodeURIComponent(json[i][j])
  8613. );
  8614. }
  8615. }
  8616. }
  8617. return strArr.join("&");
  8618. }
  8619. function doAjax(url, ajaxOptions) {
  8620. var xhr = creatAjaxRequest(),
  8621. //是否超时
  8622. timeIsOut = false,
  8623. //默认参数
  8624. defaultAjaxOptions = {
  8625. method: "POST",
  8626. timeout: 5000,
  8627. async: true,
  8628. data: {}, //需要传递对象的话只能覆盖
  8629. onsuccess: function() {},
  8630. onerror: function() {}
  8631. };
  8632. if (typeof url === "object") {
  8633. ajaxOptions = url;
  8634. url = ajaxOptions.url;
  8635. }
  8636. if (!xhr || !url) return;
  8637. var ajaxOpts = ajaxOptions
  8638. ? utils.extend(defaultAjaxOptions, ajaxOptions)
  8639. : defaultAjaxOptions;
  8640. var submitStr = json2str(ajaxOpts); // { name:"Jim",city:"Beijing" } --> "name=Jim&city=Beijing"
  8641. //如果用户直接通过data参数传递json对象过来,则也要将此json对象转化为字符串
  8642. if (!utils.isEmptyObject(ajaxOpts.data)) {
  8643. submitStr += (submitStr ? "&" : "") + json2str(ajaxOpts.data);
  8644. }
  8645. //超时检测
  8646. var timerID = setTimeout(function() {
  8647. if (xhr.readyState != 4) {
  8648. timeIsOut = true;
  8649. xhr.abort();
  8650. clearTimeout(timerID);
  8651. }
  8652. }, ajaxOpts.timeout);
  8653. var method = ajaxOpts.method.toUpperCase();
  8654. var str =
  8655. url +
  8656. (url.indexOf("?") == -1 ? "?" : "&") +
  8657. (method == "POST" ? "" : submitStr + "&noCache=" + +new Date());
  8658. xhr.open(method, str, ajaxOpts.async);
  8659. xhr.onreadystatechange = function() {
  8660. if (xhr.readyState == 4) {
  8661. if (!timeIsOut && xhr.status == 200) {
  8662. ajaxOpts.onsuccess(xhr);
  8663. } else {
  8664. ajaxOpts.onerror(xhr);
  8665. }
  8666. }
  8667. };
  8668. if (method == "POST") {
  8669. xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  8670. xhr.send(submitStr);
  8671. } else {
  8672. xhr.send(null);
  8673. }
  8674. }
  8675. function doJsonp(url, opts) {
  8676. var successhandler = opts.onsuccess || function() {},
  8677. scr = document.createElement("SCRIPT"),
  8678. options = opts || {},
  8679. charset = options["charset"],
  8680. callbackField = options["jsonp"] || "callback",
  8681. callbackFnName,
  8682. timeOut = options["timeOut"] || 0,
  8683. timer,
  8684. reg = new RegExp("(\\?|&)" + callbackField + "=([^&]*)"),
  8685. matches;
  8686. if (utils.isFunction(successhandler)) {
  8687. callbackFnName =
  8688. "bd__editor__" + Math.floor(Math.random() * 2147483648).toString(36);
  8689. window[callbackFnName] = getCallBack(0);
  8690. } else if (utils.isString(successhandler)) {
  8691. callbackFnName = successhandler;
  8692. } else {
  8693. if ((matches = reg.exec(url))) {
  8694. callbackFnName = matches[2];
  8695. }
  8696. }
  8697. url = url.replace(reg, "\x241" + callbackField + "=" + callbackFnName);
  8698. if (url.search(reg) < 0) {
  8699. url +=
  8700. (url.indexOf("?") < 0 ? "?" : "&") +
  8701. callbackField +
  8702. "=" +
  8703. callbackFnName;
  8704. }
  8705. var queryStr = json2str(opts); // { name:"Jim",city:"Beijing" } --> "name=Jim&city=Beijing"
  8706. //如果用户直接通过data参数传递json对象过来,则也要将此json对象转化为字符串
  8707. if (!utils.isEmptyObject(opts.data)) {
  8708. queryStr += (queryStr ? "&" : "") + json2str(opts.data);
  8709. }
  8710. if (queryStr) {
  8711. url = url.replace(/\?/, "?" + queryStr + "&");
  8712. }
  8713. scr.onerror = getCallBack(1);
  8714. if (timeOut) {
  8715. timer = setTimeout(getCallBack(1), timeOut);
  8716. }
  8717. createScriptTag(scr, url, charset);
  8718. function createScriptTag(scr, url, charset) {
  8719. scr.setAttribute("type", "text/javascript");
  8720. scr.setAttribute("defer", "defer");
  8721. charset && scr.setAttribute("charset", charset);
  8722. scr.setAttribute("src", url);
  8723. document.getElementsByTagName("head")[0].appendChild(scr);
  8724. }
  8725. function getCallBack(onTimeOut) {
  8726. return function() {
  8727. try {
  8728. if (onTimeOut) {
  8729. options.onerror && options.onerror();
  8730. } else {
  8731. try {
  8732. clearTimeout(timer);
  8733. successhandler.apply(window, arguments);
  8734. } catch (e) {}
  8735. }
  8736. } catch (exception) {
  8737. options.onerror && options.onerror.call(window, exception);
  8738. } finally {
  8739. options.oncomplete && options.oncomplete.apply(window, arguments);
  8740. scr.parentNode && scr.parentNode.removeChild(scr);
  8741. window[callbackFnName] = null;
  8742. try {
  8743. delete window[callbackFnName];
  8744. } catch (e) {}
  8745. }
  8746. };
  8747. }
  8748. }
  8749. return {
  8750. /**
  8751. * 根据给定的参数项,向指定的url发起一个ajax请求。 ajax请求完成后,会根据请求结果调用相应回调: 如果请求
  8752. * 成功, 则调用onsuccess回调, 失败则调用 onerror 回调
  8753. * @method request
  8754. * @param { URLString } url ajax请求的url地址
  8755. * @param { Object } ajaxOptions ajax请求选项的键值对,支持的选项如下:
  8756. * @example
  8757. * ```javascript
  8758. * //向sayhello.php发起一个异步的Ajax GET请求, 请求超时时间为10s, 请求完成后执行相应的回调。
  8759. * UE.ajax.requeset( 'sayhello.php', {
  8760. *
  8761. * //请求方法。可选值: 'GET', 'POST',默认值是'POST'
  8762. * method: 'GET',
  8763. *
  8764. * //超时时间。 默认为5000, 单位是ms
  8765. * timeout: 10000,
  8766. *
  8767. * //是否是异步请求。 true为异步请求, false为同步请求
  8768. * async: true,
  8769. *
  8770. * //请求携带的数据。如果请求为GET请求, data会经过stringify后附加到请求url之后。
  8771. * data: {
  8772. * name: 'ueditor'
  8773. * },
  8774. *
  8775. * //请求成功后的回调, 该回调接受当前的XMLHttpRequest对象作为参数。
  8776. * onsuccess: function ( xhr ) {
  8777. * console.log( xhr.responseText );
  8778. * },
  8779. *
  8780. * //请求失败或者超时后的回调。
  8781. * onerror: function ( xhr ) {
  8782. * alert( 'Ajax请求失败' );
  8783. * }
  8784. *
  8785. * } );
  8786. * ```
  8787. */
  8788. /**
  8789. * 根据给定的参数项发起一个ajax请求, 参数项里必须包含一个url地址。 ajax请求完成后,会根据请求结果调用相应回调: 如果请求
  8790. * 成功, 则调用onsuccess回调, 失败则调用 onerror 回调。
  8791. * @method request
  8792. * @warning 如果在参数项里未提供一个key为“url”的地址值,则该请求将直接退出。
  8793. * @param { Object } ajaxOptions ajax请求选项的键值对,支持的选项如下:
  8794. * @example
  8795. * ```javascript
  8796. *
  8797. * //向sayhello.php发起一个异步的Ajax POST请求, 请求超时时间为5s, 请求完成后不执行任何回调。
  8798. * UE.ajax.requeset( 'sayhello.php', {
  8799. *
  8800. * //请求的地址, 该项是必须的。
  8801. * url: 'sayhello.php'
  8802. *
  8803. * } );
  8804. * ```
  8805. */
  8806. request: function(url, opts) {
  8807. if (opts && opts.dataType == "jsonp") {
  8808. doJsonp(url, opts);
  8809. } else {
  8810. doAjax(url, opts);
  8811. }
  8812. },
  8813. getJSONP: function(url, data, fn) {
  8814. var opts = {
  8815. data: data,
  8816. oncomplete: fn
  8817. };
  8818. doJsonp(url, opts);
  8819. }
  8820. };
  8821. })();
  8822. // core/filterword.js
  8823. /**
  8824. * UE过滤word的静态方法
  8825. * @file
  8826. */
  8827. /**
  8828. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  8829. * @module UE
  8830. */
  8831. /**
  8832. * 根据传入html字符串过滤word
  8833. * @module UE
  8834. * @since 1.2.6.1
  8835. * @method filterWord
  8836. * @param { String } html html字符串
  8837. * @return { String } 已过滤后的结果字符串
  8838. * @example
  8839. * ```javascript
  8840. * UE.filterWord(html);
  8841. * ```
  8842. */
  8843. var filterWord = (UE.filterWord = (function() {
  8844. //是否是word过来的内容
  8845. function isWordDocument(str) {
  8846. return /(class="?Mso|style="[^"]*\bmso\-|w:WordDocument|<(v|o):|lang=)/gi.test(
  8847. str
  8848. );
  8849. }
  8850. //去掉小数
  8851. function transUnit(v) {
  8852. v = v.replace(/[\d.]+\w+/g, function(m) {
  8853. return utils.transUnitToPx(m);
  8854. });
  8855. return v;
  8856. }
  8857. function filterPasteWord(str) {
  8858. return (
  8859. str
  8860. .replace(/[\t\r\n]+/g, " ")
  8861. .replace(/<!--[\s\S]*?-->/gi, "")
  8862. //转换图片
  8863. .replace(/<v:shape [^>]*>[\s\S]*?.<\/v:shape>/gi, function(str) {
  8864. //opera能自己解析出image所这里直接返回空
  8865. if (browser.opera) {
  8866. return "";
  8867. }
  8868. try {
  8869. //有可能是bitmap占为图,无用,直接过滤掉,主要体现在粘贴excel表格中
  8870. if (/Bitmap/i.test(str)) {
  8871. return "";
  8872. }
  8873. var width = str.match(/width:([ \d.]*p[tx])/i)[1],
  8874. height = str.match(/height:([ \d.]*p[tx])/i)[1],
  8875. src = str.match(/src=\s*"([^"]*)"/i)[1];
  8876. return (
  8877. '<img width="' +
  8878. transUnit(width) +
  8879. '" height="' +
  8880. transUnit(height) +
  8881. '" src="' +
  8882. src +
  8883. '" />'
  8884. );
  8885. } catch (e) {
  8886. return "";
  8887. }
  8888. })
  8889. //针对wps添加的多余标签处理
  8890. .replace(/<\/?div[^>]*>/g, "")
  8891. //去掉多余的属性
  8892. .replace(/v:\w+=(["']?)[^'"]+\1/g, "")
  8893. .replace(
  8894. /<(!|script[^>]*>.*?<\/script(?=[>\s])|\/?(\?xml(:\w+)?|xml|meta|link|style|\w+:\w+)(?=[\s\/>]))[^>]*>/gi,
  8895. ""
  8896. )
  8897. .replace(
  8898. /<p [^>]*class="?MsoHeading"?[^>]*>(.*?)<\/p>/gi,
  8899. "<p><strong>$1</strong></p>"
  8900. )
  8901. //去掉多余的属性
  8902. .replace(/\s+(class|lang|align)\s*=\s*(['"]?)([\w-]+)\2/gi, function(
  8903. str,
  8904. name,
  8905. marks,
  8906. val
  8907. ) {
  8908. //保留list的标示
  8909. return name == "class" && val == "MsoListParagraph" ? str : "";
  8910. })
  8911. //清除多余的font/span不能匹配&nbsp;有可能是空格
  8912. .replace(/<(font|span)[^>]*>(\s*)<\/\1>/gi, function(a, b, c) {
  8913. return c.replace(/[\t\r\n ]+/g, " ");
  8914. })
  8915. //处理style的问题
  8916. .replace(/(<[a-z][^>]*)\sstyle=(["'])([^\2]*?)\2/gi, function(
  8917. str,
  8918. tag,
  8919. tmp,
  8920. style
  8921. ) {
  8922. var n = [],
  8923. s = style
  8924. .replace(/^\s+|\s+$/, "")
  8925. .replace(/&#39;/g, "'")
  8926. .replace(/&quot;/gi, "'")
  8927. .replace(/[\d.]+(cm|pt)/g, function(str) {
  8928. return utils.transUnitToPx(str);
  8929. })
  8930. .split(/;\s*/g);
  8931. for (var i = 0, v; (v = s[i]); i++) {
  8932. var name,
  8933. value,
  8934. parts = v.split(":");
  8935. if (parts.length == 2) {
  8936. name = parts[0].toLowerCase();
  8937. value = parts[1].toLowerCase();
  8938. if (
  8939. (/^(background)\w*/.test(name) &&
  8940. value.replace(/(initial|\s)/g, "").length == 0) ||
  8941. (/^(margin)\w*/.test(name) && /^0\w+$/.test(value))
  8942. ) {
  8943. continue;
  8944. }
  8945. switch (name) {
  8946. case "mso-padding-alt":
  8947. case "mso-padding-top-alt":
  8948. case "mso-padding-right-alt":
  8949. case "mso-padding-bottom-alt":
  8950. case "mso-padding-left-alt":
  8951. case "mso-margin-alt":
  8952. case "mso-margin-top-alt":
  8953. case "mso-margin-right-alt":
  8954. case "mso-margin-bottom-alt":
  8955. case "mso-margin-left-alt":
  8956. //ie下会出现挤到一起的情况
  8957. //case "mso-table-layout-alt":
  8958. case "mso-height":
  8959. case "mso-width":
  8960. case "mso-vertical-align-alt":
  8961. //trace:1819 ff下会解析出padding在table上
  8962. if (!/<table/.test(tag))
  8963. n[i] =
  8964. name.replace(/^mso-|-alt$/g, "") + ":" + transUnit(value);
  8965. continue;
  8966. case "horiz-align":
  8967. n[i] = "text-align:" + value;
  8968. continue;
  8969. case "vert-align":
  8970. n[i] = "vertical-align:" + value;
  8971. continue;
  8972. case "font-color":
  8973. case "mso-foreground":
  8974. n[i] = "color:" + value;
  8975. continue;
  8976. case "mso-background":
  8977. case "mso-highlight":
  8978. n[i] = "background:" + value;
  8979. continue;
  8980. case "mso-default-height":
  8981. n[i] = "min-height:" + transUnit(value);
  8982. continue;
  8983. case "mso-default-width":
  8984. n[i] = "min-width:" + transUnit(value);
  8985. continue;
  8986. case "mso-padding-between-alt":
  8987. n[i] =
  8988. "border-collapse:separate;border-spacing:" +
  8989. transUnit(value);
  8990. continue;
  8991. case "text-line-through":
  8992. if (value == "single" || value == "double") {
  8993. n[i] = "text-decoration:line-through";
  8994. }
  8995. continue;
  8996. case "mso-zero-height":
  8997. if (value == "yes") {
  8998. n[i] = "display:none";
  8999. }
  9000. continue;
  9001. // case 'background':
  9002. // break;
  9003. case "margin":
  9004. if (!/[1-9]/.test(value)) {
  9005. continue;
  9006. }
  9007. }
  9008. if (
  9009. /^(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(
  9010. name
  9011. ) ||
  9012. (/text\-indent|padding|margin/.test(name) &&
  9013. /\-[\d.]+/.test(value))
  9014. ) {
  9015. continue;
  9016. }
  9017. n[i] = name + ":" + parts[1];
  9018. }
  9019. }
  9020. return (
  9021. tag +
  9022. (n.length
  9023. ? ' style="' + n.join(";").replace(/;{2,}/g, ";") + '"'
  9024. : "")
  9025. );
  9026. })
  9027. );
  9028. }
  9029. return function(html) {
  9030. return isWordDocument(html) ? filterPasteWord(html) : html;
  9031. };
  9032. })());
  9033. // core/node.js
  9034. /**
  9035. * 编辑器模拟的节点类
  9036. * @file
  9037. * @module UE
  9038. * @class uNode
  9039. * @since 1.2.6.1
  9040. */
  9041. /**
  9042. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  9043. * @unfile
  9044. * @module UE
  9045. */
  9046. (function() {
  9047. /**
  9048. * 编辑器模拟的节点类
  9049. * @unfile
  9050. * @module UE
  9051. * @class uNode
  9052. */
  9053. /**
  9054. * 通过一个键值对,创建一个uNode对象
  9055. * @constructor
  9056. * @param { Object } attr 传入要创建的uNode的初始属性
  9057. * @example
  9058. * ```javascript
  9059. * var node = new uNode({
  9060. * type:'element',
  9061. * tagName:'span',
  9062. * attrs:{style:'font-size:14px;'}
  9063. * })
  9064. * ```
  9065. */
  9066. var uNode = (UE.uNode = function(obj) {
  9067. this.type = obj.type;
  9068. this.data = obj.data;
  9069. this.tagName = obj.tagName;
  9070. this.parentNode = obj.parentNode;
  9071. this.attrs = obj.attrs || {};
  9072. this.children = obj.children;
  9073. });
  9074. var notTransAttrs = {
  9075. href: 1,
  9076. src: 1,
  9077. _src: 1,
  9078. _href: 1,
  9079. cdata_data: 1
  9080. };
  9081. var notTransTagName = {
  9082. style: 1,
  9083. script: 1
  9084. };
  9085. var indentChar = " ",
  9086. breakChar = "\n";
  9087. function insertLine(arr, current, begin) {
  9088. arr.push(breakChar);
  9089. return current + (begin ? 1 : -1);
  9090. }
  9091. function insertIndent(arr, current) {
  9092. //插入缩进
  9093. for (var i = 0; i < current; i++) {
  9094. arr.push(indentChar);
  9095. }
  9096. }
  9097. //创建uNode的静态方法
  9098. //支持标签和html
  9099. uNode.createElement = function(html) {
  9100. if (/[<>]/.test(html)) {
  9101. return UE.htmlparser(html).children[0];
  9102. } else {
  9103. return new uNode({
  9104. type: "element",
  9105. children: [],
  9106. tagName: html
  9107. });
  9108. }
  9109. };
  9110. uNode.createText = function(data, noTrans) {
  9111. return new UE.uNode({
  9112. type: "text",
  9113. data: noTrans ? data : utils.unhtml(data || "")
  9114. });
  9115. };
  9116. function nodeToHtml(node, arr, formatter, current) {
  9117. switch (node.type) {
  9118. case "root":
  9119. for (var i = 0, ci; (ci = node.children[i++]); ) {
  9120. //插入新行
  9121. if (
  9122. formatter &&
  9123. ci.type == "element" &&
  9124. !dtd.$inlineWithA[ci.tagName] &&
  9125. i > 1
  9126. ) {
  9127. insertLine(arr, current, true);
  9128. insertIndent(arr, current);
  9129. }
  9130. nodeToHtml(ci, arr, formatter, current);
  9131. }
  9132. break;
  9133. case "text":
  9134. isText(node, arr);
  9135. break;
  9136. case "element":
  9137. isElement(node, arr, formatter, current);
  9138. break;
  9139. case "comment":
  9140. isComment(node, arr, formatter);
  9141. }
  9142. return arr;
  9143. }
  9144. function isText(node, arr) {
  9145. if (node.parentNode.tagName == "pre") {
  9146. //源码模式下输入html标签,不能做转换处理,直接输出
  9147. arr.push(node.data);
  9148. } else {
  9149. arr.push(
  9150. notTransTagName[node.parentNode.tagName]
  9151. ? utils.html(node.data)
  9152. : node.data.replace(/[ ]{2}/g, " &nbsp;")
  9153. );
  9154. }
  9155. }
  9156. function isElement(node, arr, formatter, current) {
  9157. var attrhtml = "";
  9158. if (node.attrs) {
  9159. attrhtml = [];
  9160. var attrs = node.attrs;
  9161. for (var a in attrs) {
  9162. //这里就针对
  9163. //<p>'<img src='http://nsclick.baidu.com/u.gif?&asdf=\"sdf&asdfasdfs;asdf'></p>
  9164. //这里边的\"做转换,要不用innerHTML直接被截断了,属性src
  9165. //有可能做的不够
  9166. attrhtml.push(
  9167. a +
  9168. (attrs[a] !== undefined
  9169. ? '="' +
  9170. (notTransAttrs[a]
  9171. ? utils.html(attrs[a]).replace(/["]/g, function(a) {
  9172. return "&quot;";
  9173. })
  9174. : utils.unhtml(attrs[a])) +
  9175. '"'
  9176. : "")
  9177. );
  9178. }
  9179. attrhtml = attrhtml.join(" ");
  9180. }
  9181. arr.push(
  9182. "<" +
  9183. node.tagName +
  9184. (attrhtml ? " " + attrhtml : "") +
  9185. (dtd.$empty[node.tagName] ? "/" : "") +
  9186. ">"
  9187. );
  9188. //插入新行
  9189. if (formatter && !dtd.$inlineWithA[node.tagName] && node.tagName != "pre") {
  9190. if (node.children && node.children.length) {
  9191. current = insertLine(arr, current, true);
  9192. insertIndent(arr, current);
  9193. }
  9194. }
  9195. if (node.children && node.children.length) {
  9196. for (var i = 0, ci; (ci = node.children[i++]); ) {
  9197. if (
  9198. formatter &&
  9199. ci.type == "element" &&
  9200. !dtd.$inlineWithA[ci.tagName] &&
  9201. i > 1
  9202. ) {
  9203. insertLine(arr, current);
  9204. insertIndent(arr, current);
  9205. }
  9206. nodeToHtml(ci, arr, formatter, current);
  9207. }
  9208. }
  9209. if (!dtd.$empty[node.tagName]) {
  9210. if (
  9211. formatter &&
  9212. !dtd.$inlineWithA[node.tagName] &&
  9213. node.tagName != "pre"
  9214. ) {
  9215. if (node.children && node.children.length) {
  9216. current = insertLine(arr, current);
  9217. insertIndent(arr, current);
  9218. }
  9219. }
  9220. arr.push("</" + node.tagName + ">");
  9221. }
  9222. }
  9223. function isComment(node, arr) {
  9224. arr.push("<!--" + node.data + "-->");
  9225. }
  9226. function getNodeById(root, id) {
  9227. var node;
  9228. if (root.type == "element" && root.getAttr("id") == id) {
  9229. return root;
  9230. }
  9231. if (root.children && root.children.length) {
  9232. for (var i = 0, ci; (ci = root.children[i++]); ) {
  9233. if ((node = getNodeById(ci, id))) {
  9234. return node;
  9235. }
  9236. }
  9237. }
  9238. }
  9239. function getNodesByTagName(node, tagName, arr) {
  9240. if (node.type == "element" && node.tagName == tagName) {
  9241. arr.push(node);
  9242. }
  9243. if (node.children && node.children.length) {
  9244. for (var i = 0, ci; (ci = node.children[i++]); ) {
  9245. getNodesByTagName(ci, tagName, arr);
  9246. }
  9247. }
  9248. }
  9249. function nodeTraversal(root, fn) {
  9250. if (root.children && root.children.length) {
  9251. for (var i = 0, ci; (ci = root.children[i]); ) {
  9252. nodeTraversal(ci, fn);
  9253. //ci被替换的情况,这里就不再走 fn了
  9254. if (ci.parentNode) {
  9255. if (ci.children && ci.children.length) {
  9256. fn(ci);
  9257. }
  9258. if (ci.parentNode) i++;
  9259. }
  9260. }
  9261. } else {
  9262. fn(root);
  9263. }
  9264. }
  9265. uNode.prototype = {
  9266. /**
  9267. * 当前节点对象,转换成html文本
  9268. * @method toHtml
  9269. * @return { String } 返回转换后的html字符串
  9270. * @example
  9271. * ```javascript
  9272. * node.toHtml();
  9273. * ```
  9274. */
  9275. /**
  9276. * 当前节点对象,转换成html文本
  9277. * @method toHtml
  9278. * @param { Boolean } formatter 是否格式化返回值
  9279. * @return { String } 返回转换后的html字符串
  9280. * @example
  9281. * ```javascript
  9282. * node.toHtml( true );
  9283. * ```
  9284. */
  9285. toHtml: function(formatter) {
  9286. var arr = [];
  9287. nodeToHtml(this, arr, formatter, 0);
  9288. return arr.join("");
  9289. },
  9290. /**
  9291. * 获取节点的html内容
  9292. * @method innerHTML
  9293. * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
  9294. * @return { String } 返回节点的html内容
  9295. * @example
  9296. * ```javascript
  9297. * var htmlstr = node.innerHTML();
  9298. * ```
  9299. */
  9300. /**
  9301. * 设置节点的html内容
  9302. * @method innerHTML
  9303. * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
  9304. * @param { String } htmlstr 传入要设置的html内容
  9305. * @return { UE.uNode } 返回节点本身
  9306. * @example
  9307. * ```javascript
  9308. * node.innerHTML('<span>text</span>');
  9309. * ```
  9310. */
  9311. innerHTML: function(htmlstr) {
  9312. if (this.type != "element" || dtd.$empty[this.tagName]) {
  9313. return this;
  9314. }
  9315. if (utils.isString(htmlstr)) {
  9316. if (this.children) {
  9317. for (var i = 0, ci; (ci = this.children[i++]); ) {
  9318. ci.parentNode = null;
  9319. }
  9320. }
  9321. this.children = [];
  9322. var tmpRoot = UE.htmlparser(htmlstr);
  9323. for (var i = 0, ci; (ci = tmpRoot.children[i++]); ) {
  9324. this.children.push(ci);
  9325. ci.parentNode = this;
  9326. }
  9327. return this;
  9328. } else {
  9329. var tmpRoot = new UE.uNode({
  9330. type: "root",
  9331. children: this.children
  9332. });
  9333. return tmpRoot.toHtml();
  9334. }
  9335. },
  9336. /**
  9337. * 获取节点的纯文本内容
  9338. * @method innerText
  9339. * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
  9340. * @return { String } 返回节点的存文本内容
  9341. * @example
  9342. * ```javascript
  9343. * var textStr = node.innerText();
  9344. * ```
  9345. */
  9346. /**
  9347. * 设置节点的纯文本内容
  9348. * @method innerText
  9349. * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
  9350. * @param { String } textStr 传入要设置的文本内容
  9351. * @return { UE.uNode } 返回节点本身
  9352. * @example
  9353. * ```javascript
  9354. * node.innerText('<span>text</span>');
  9355. * ```
  9356. */
  9357. innerText: function(textStr, noTrans) {
  9358. if (this.type != "element" || dtd.$empty[this.tagName]) {
  9359. return this;
  9360. }
  9361. if (textStr) {
  9362. if (this.children) {
  9363. for (var i = 0, ci; (ci = this.children[i++]); ) {
  9364. ci.parentNode = null;
  9365. }
  9366. }
  9367. this.children = [];
  9368. this.appendChild(uNode.createText(textStr, noTrans));
  9369. return this;
  9370. } else {
  9371. return this.toHtml().replace(/<[^>]+>/g, "");
  9372. }
  9373. },
  9374. /**
  9375. * 获取当前对象的data属性
  9376. * @method getData
  9377. * @return { Object } 若节点的type值是elemenet,返回空字符串,否则返回节点的data属性
  9378. * @example
  9379. * ```javascript
  9380. * node.getData();
  9381. * ```
  9382. */
  9383. getData: function() {
  9384. if (this.type == "element") return "";
  9385. return this.data;
  9386. },
  9387. /**
  9388. * 获取当前节点下的第一个子节点
  9389. * @method firstChild
  9390. * @return { UE.uNode } 返回第一个子节点
  9391. * @example
  9392. * ```javascript
  9393. * node.firstChild(); //返回第一个子节点
  9394. * ```
  9395. */
  9396. firstChild: function() {
  9397. // if (this.type != 'element' || dtd.$empty[this.tagName]) {
  9398. // return this;
  9399. // }
  9400. return this.children ? this.children[0] : null;
  9401. },
  9402. /**
  9403. * 获取当前节点下的最后一个子节点
  9404. * @method lastChild
  9405. * @return { UE.uNode } 返回最后一个子节点
  9406. * @example
  9407. * ```javascript
  9408. * node.lastChild(); //返回最后一个子节点
  9409. * ```
  9410. */
  9411. lastChild: function() {
  9412. // if (this.type != 'element' || dtd.$empty[this.tagName] ) {
  9413. // return this;
  9414. // }
  9415. return this.children ? this.children[this.children.length - 1] : null;
  9416. },
  9417. /**
  9418. * 获取和当前节点有相同父亲节点的前一个节点
  9419. * @method previousSibling
  9420. * @return { UE.uNode } 返回前一个节点
  9421. * @example
  9422. * ```javascript
  9423. * node.children[2].previousSibling(); //返回子节点node.children[1]
  9424. * ```
  9425. */
  9426. previousSibling: function() {
  9427. var parent = this.parentNode;
  9428. for (var i = 0, ci; (ci = parent.children[i]); i++) {
  9429. if (ci === this) {
  9430. return i == 0 ? null : parent.children[i - 1];
  9431. }
  9432. }
  9433. },
  9434. /**
  9435. * 获取和当前节点有相同父亲节点的后一个节点
  9436. * @method nextSibling
  9437. * @return { UE.uNode } 返回后一个节点,找不到返回null
  9438. * @example
  9439. * ```javascript
  9440. * node.children[2].nextSibling(); //如果有,返回子节点node.children[3]
  9441. * ```
  9442. */
  9443. nextSibling: function() {
  9444. var parent = this.parentNode;
  9445. for (var i = 0, ci; (ci = parent.children[i++]); ) {
  9446. if (ci === this) {
  9447. return parent.children[i];
  9448. }
  9449. }
  9450. },
  9451. /**
  9452. * 用新的节点替换当前节点
  9453. * @method replaceChild
  9454. * @param { UE.uNode } target 要替换成该节点参数
  9455. * @param { UE.uNode } source 要被替换掉的节点
  9456. * @return { UE.uNode } 返回替换之后的节点对象
  9457. * @example
  9458. * ```javascript
  9459. * node.replaceChild(newNode, childNode); //用newNode替换childNode,childNode是node的子节点
  9460. * ```
  9461. */
  9462. replaceChild: function(target, source) {
  9463. if (this.children) {
  9464. if (target.parentNode) {
  9465. target.parentNode.removeChild(target);
  9466. }
  9467. for (var i = 0, ci; (ci = this.children[i]); i++) {
  9468. if (ci === source) {
  9469. this.children.splice(i, 1, target);
  9470. source.parentNode = null;
  9471. target.parentNode = this;
  9472. return target;
  9473. }
  9474. }
  9475. }
  9476. },
  9477. /**
  9478. * 在节点的子节点列表最后位置插入一个节点
  9479. * @method appendChild
  9480. * @param { UE.uNode } node 要插入的节点
  9481. * @return { UE.uNode } 返回刚插入的子节点
  9482. * @example
  9483. * ```javascript
  9484. * node.appendChild( newNode ); //在node内插入子节点newNode
  9485. * ```
  9486. */
  9487. appendChild: function(node) {
  9488. if (
  9489. this.type == "root" ||
  9490. (this.type == "element" && !dtd.$empty[this.tagName])
  9491. ) {
  9492. if (!this.children) {
  9493. this.children = [];
  9494. }
  9495. if (node.parentNode) {
  9496. node.parentNode.removeChild(node);
  9497. }
  9498. for (var i = 0, ci; (ci = this.children[i]); i++) {
  9499. if (ci === node) {
  9500. this.children.splice(i, 1);
  9501. break;
  9502. }
  9503. }
  9504. this.children.push(node);
  9505. node.parentNode = this;
  9506. return node;
  9507. }
  9508. },
  9509. /**
  9510. * 在传入节点的前面插入一个节点
  9511. * @method insertBefore
  9512. * @param { UE.uNode } target 要插入的节点
  9513. * @param { UE.uNode } source 在该参数节点前面插入
  9514. * @return { UE.uNode } 返回刚插入的子节点
  9515. * @example
  9516. * ```javascript
  9517. * node.parentNode.insertBefore(newNode, node); //在node节点后面插入newNode
  9518. * ```
  9519. */
  9520. insertBefore: function(target, source) {
  9521. if (this.children) {
  9522. if (target.parentNode) {
  9523. target.parentNode.removeChild(target);
  9524. }
  9525. for (var i = 0, ci; (ci = this.children[i]); i++) {
  9526. if (ci === source) {
  9527. this.children.splice(i, 0, target);
  9528. target.parentNode = this;
  9529. return target;
  9530. }
  9531. }
  9532. }
  9533. },
  9534. /**
  9535. * 在传入节点的后面插入一个节点
  9536. * @method insertAfter
  9537. * @param { UE.uNode } target 要插入的节点
  9538. * @param { UE.uNode } source 在该参数节点后面插入
  9539. * @return { UE.uNode } 返回刚插入的子节点
  9540. * @example
  9541. * ```javascript
  9542. * node.parentNode.insertAfter(newNode, node); //在node节点后面插入newNode
  9543. * ```
  9544. */
  9545. insertAfter: function(target, source) {
  9546. if (this.children) {
  9547. if (target.parentNode) {
  9548. target.parentNode.removeChild(target);
  9549. }
  9550. for (var i = 0, ci; (ci = this.children[i]); i++) {
  9551. if (ci === source) {
  9552. this.children.splice(i + 1, 0, target);
  9553. target.parentNode = this;
  9554. return target;
  9555. }
  9556. }
  9557. }
  9558. },
  9559. /**
  9560. * 从当前节点的子节点列表中,移除节点
  9561. * @method removeChild
  9562. * @param { UE.uNode } node 要移除的节点引用
  9563. * @param { Boolean } keepChildren 是否保留移除节点的子节点,若传入true,自动把移除节点的子节点插入到移除的位置
  9564. * @return { * } 返回刚移除的子节点
  9565. * @example
  9566. * ```javascript
  9567. * node.removeChild(childNode,true); //在node的子节点列表中移除child节点,并且吧child的子节点插入到移除的位置
  9568. * ```
  9569. */
  9570. removeChild: function(node, keepChildren) {
  9571. if (this.children) {
  9572. for (var i = 0, ci; (ci = this.children[i]); i++) {
  9573. if (ci === node) {
  9574. this.children.splice(i, 1);
  9575. ci.parentNode = null;
  9576. if (keepChildren && ci.children && ci.children.length) {
  9577. for (var j = 0, cj; (cj = ci.children[j]); j++) {
  9578. this.children.splice(i + j, 0, cj);
  9579. cj.parentNode = this;
  9580. }
  9581. }
  9582. return ci;
  9583. }
  9584. }
  9585. }
  9586. },
  9587. /**
  9588. * 获取当前节点所代表的元素属性,即获取attrs对象下的属性值
  9589. * @method getAttr
  9590. * @param { String } attrName 要获取的属性名称
  9591. * @return { * } 返回attrs对象下的属性值
  9592. * @example
  9593. * ```javascript
  9594. * node.getAttr('title');
  9595. * ```
  9596. */
  9597. getAttr: function(attrName) {
  9598. return this.attrs && this.attrs[attrName.toLowerCase()];
  9599. },
  9600. /**
  9601. * 设置当前节点所代表的元素属性,即设置attrs对象下的属性值
  9602. * @method setAttr
  9603. * @param { String } attrName 要设置的属性名称
  9604. * @param { * } attrVal 要设置的属性值,类型视设置的属性而定
  9605. * @return { * } 返回attrs对象下的属性值
  9606. * @example
  9607. * ```javascript
  9608. * node.setAttr('title','标题');
  9609. * ```
  9610. */
  9611. setAttr: function(attrName, attrVal) {
  9612. if (!attrName) {
  9613. delete this.attrs;
  9614. return;
  9615. }
  9616. if (!this.attrs) {
  9617. this.attrs = {};
  9618. }
  9619. if (utils.isObject(attrName)) {
  9620. for (var a in attrName) {
  9621. if (!attrName[a]) {
  9622. delete this.attrs[a];
  9623. } else {
  9624. this.attrs[a.toLowerCase()] = attrName[a];
  9625. }
  9626. }
  9627. } else {
  9628. if (!attrVal) {
  9629. delete this.attrs[attrName];
  9630. } else {
  9631. this.attrs[attrName.toLowerCase()] = attrVal;
  9632. }
  9633. }
  9634. },
  9635. /**
  9636. * 获取当前节点在父节点下的位置索引
  9637. * @method getIndex
  9638. * @return { Number } 返回索引数值,如果没有父节点,返回-1
  9639. * @example
  9640. * ```javascript
  9641. * node.getIndex();
  9642. * ```
  9643. */
  9644. getIndex: function() {
  9645. var parent = this.parentNode;
  9646. for (var i = 0, ci; (ci = parent.children[i]); i++) {
  9647. if (ci === this) {
  9648. return i;
  9649. }
  9650. }
  9651. return -1;
  9652. },
  9653. /**
  9654. * 在当前节点下,根据id查找节点
  9655. * @method getNodeById
  9656. * @param { String } id 要查找的id
  9657. * @return { UE.uNode } 返回找到的节点
  9658. * @example
  9659. * ```javascript
  9660. * node.getNodeById('textId');
  9661. * ```
  9662. */
  9663. getNodeById: function(id) {
  9664. var node;
  9665. if (this.children && this.children.length) {
  9666. for (var i = 0, ci; (ci = this.children[i++]); ) {
  9667. if ((node = getNodeById(ci, id))) {
  9668. return node;
  9669. }
  9670. }
  9671. }
  9672. },
  9673. /**
  9674. * 在当前节点下,根据元素名称查找节点列表
  9675. * @method getNodesByTagName
  9676. * @param { String } tagNames 要查找的元素名称
  9677. * @return { Array } 返回找到的节点列表
  9678. * @example
  9679. * ```javascript
  9680. * node.getNodesByTagName('span');
  9681. * ```
  9682. */
  9683. getNodesByTagName: function(tagNames) {
  9684. tagNames = utils.trim(tagNames).replace(/[ ]{2,}/g, " ").split(" ");
  9685. var arr = [],
  9686. me = this;
  9687. utils.each(tagNames, function(tagName) {
  9688. if (me.children && me.children.length) {
  9689. for (var i = 0, ci; (ci = me.children[i++]); ) {
  9690. getNodesByTagName(ci, tagName, arr);
  9691. }
  9692. }
  9693. });
  9694. return arr;
  9695. },
  9696. /**
  9697. * 根据样式名称,获取节点的样式值
  9698. * @method getStyle
  9699. * @param { String } name 要获取的样式名称
  9700. * @return { String } 返回样式值
  9701. * @example
  9702. * ```javascript
  9703. * node.getStyle('font-size');
  9704. * ```
  9705. */
  9706. getStyle: function(name) {
  9707. var cssStyle = this.getAttr("style");
  9708. if (!cssStyle) {
  9709. return "";
  9710. }
  9711. var reg = new RegExp("(^|;)\\s*" + name + ":([^;]+)", "i");
  9712. var match = cssStyle.match(reg);
  9713. if (match && match[0]) {
  9714. return match[2];
  9715. }
  9716. return "";
  9717. },
  9718. /**
  9719. * 给节点设置样式
  9720. * @method setStyle
  9721. * @param { String } name 要设置的的样式名称
  9722. * @param { String } val 要设置的的样值
  9723. * @example
  9724. * ```javascript
  9725. * node.setStyle('font-size', '12px');
  9726. * ```
  9727. */
  9728. setStyle: function(name, val) {
  9729. function exec(name, val) {
  9730. var reg = new RegExp("(^|;)\\s*" + name + ":([^;]+;?)", "gi");
  9731. cssStyle = cssStyle.replace(reg, "$1");
  9732. if (val) {
  9733. cssStyle = name + ":" + utils.unhtml(val) + ";" + cssStyle;
  9734. }
  9735. }
  9736. var cssStyle = this.getAttr("style");
  9737. if (!cssStyle) {
  9738. cssStyle = "";
  9739. }
  9740. if (utils.isObject(name)) {
  9741. for (var a in name) {
  9742. exec(a, name[a]);
  9743. }
  9744. } else {
  9745. exec(name, val);
  9746. }
  9747. this.setAttr("style", utils.trim(cssStyle));
  9748. },
  9749. /**
  9750. * 传入一个函数,递归遍历当前节点下的所有节点
  9751. * @method traversal
  9752. * @param { Function } fn 遍历到节点的时,传入节点作为参数,运行此函数
  9753. * @example
  9754. * ```javascript
  9755. * traversal(node, function(){
  9756. * console.log(node.type);
  9757. * });
  9758. * ```
  9759. */
  9760. traversal: function(fn) {
  9761. if (this.children && this.children.length) {
  9762. nodeTraversal(this, fn);
  9763. }
  9764. return this;
  9765. }
  9766. };
  9767. })();
  9768. // core/htmlparser.js
  9769. /**
  9770. * html字符串转换成uNode节点
  9771. * @file
  9772. * @module UE
  9773. * @since 1.2.6.1
  9774. */
  9775. /**
  9776. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  9777. * @unfile
  9778. * @module UE
  9779. */
  9780. /**
  9781. * html字符串转换成uNode节点的静态方法
  9782. * @method htmlparser
  9783. * @param { String } htmlstr 要转换的html代码
  9784. * @param { Boolean } ignoreBlank 若设置为true,转换的时候忽略\n\r\t等空白字符
  9785. * @return { uNode } 给定的html片段转换形成的uNode对象
  9786. * @example
  9787. * ```javascript
  9788. * var root = UE.htmlparser('<p><b>htmlparser</b></p>', true);
  9789. * ```
  9790. */
  9791. var htmlparser = (UE.htmlparser = function(htmlstr, ignoreBlank) {
  9792. //todo 原来的方式 [^"'<>\/] 有\/就不能配对上 <TD vAlign=top background=../AAA.JPG> 这样的标签了
  9793. //先去掉了,加上的原因忘了,这里先记录
  9794. //var re_tag = /<(?:(?:\/([^>]+)>)|(?:!--([\S|\s]*?)-->)|(?:([^\s\/<>]+)\s*((?:(?:"[^"]*")|(?:'[^']*')|[^"'<>])*)\/?>))/g,
  9795. //以上的正则表达式无法匹配:<div style="text-align:center;font-family:" font-size:14px;"=""><img src="http://hs-album.oss.aliyuncs.com/static/27/78/35/image/20161206/20161206174331_41105.gif" alt="" /><br /></div>
  9796. //修改为如下正则表达式:
  9797. var re_tag = /<(?:(?:\/([^>]+)>)|(?:!--([\S|\s]*?)-->)|(?:([^\/\s>]+)((?:\s+[\w\-:.]+(?:\s*=\s*?(?:(?:"[^"]*")|(?:'[^']*')|[^\s"'\/>]+))?)*)[\S\s]*?(\/?)>))/g,
  9798. re_attr = /([\w\-:.]+)(?:(?:\s*=\s*(?:(?:"([^"]*)")|(?:'([^']*)')|([^\s>]+)))|(?=\s|$))/g;
  9799. //ie下取得的html可能会有\n存在,要去掉,在处理replace(/[\t\r\n]*/g,'');代码高量的\n不能去除
  9800. var allowEmptyTags = {
  9801. b: 1,
  9802. code: 1,
  9803. i: 1,
  9804. u: 1,
  9805. strike: 1,
  9806. s: 1,
  9807. tt: 1,
  9808. strong: 1,
  9809. q: 1,
  9810. samp: 1,
  9811. em: 1,
  9812. span: 1,
  9813. sub: 1,
  9814. img: 1,
  9815. sup: 1,
  9816. font: 1,
  9817. big: 1,
  9818. small: 1,
  9819. iframe: 1,
  9820. a: 1,
  9821. br: 1,
  9822. pre: 1
  9823. };
  9824. htmlstr = htmlstr.replace(new RegExp(domUtils.fillChar, "g"), "");
  9825. if (!ignoreBlank) {
  9826. htmlstr = htmlstr.replace(
  9827. new RegExp(
  9828. "[\\r\\t\\n" +
  9829. (ignoreBlank ? "" : " ") +
  9830. "]*</?(\\w+)\\s*(?:[^>]*)>[\\r\\t\\n" +
  9831. (ignoreBlank ? "" : " ") +
  9832. "]*",
  9833. "g"
  9834. ),
  9835. function(a, b) {
  9836. //br暂时单独处理
  9837. if (b && allowEmptyTags[b.toLowerCase()]) {
  9838. return a.replace(/(^[\n\r]+)|([\n\r]+$)/g, "");
  9839. }
  9840. return a
  9841. .replace(new RegExp("^[\\r\\n" + (ignoreBlank ? "" : " ") + "]+"), "")
  9842. .replace(
  9843. new RegExp("[\\r\\n" + (ignoreBlank ? "" : " ") + "]+$"),
  9844. ""
  9845. );
  9846. }
  9847. );
  9848. }
  9849. var notTransAttrs = {
  9850. href: 1,
  9851. src: 1
  9852. };
  9853. var uNode = UE.uNode,
  9854. needParentNode = {
  9855. td: "tr",
  9856. tr: ["tbody", "thead", "tfoot"],
  9857. tbody: "table",
  9858. th: "tr",
  9859. thead: "table",
  9860. tfoot: "table",
  9861. caption: "table",
  9862. li: ["ul", "ol"],
  9863. dt: "dl",
  9864. dd: "dl",
  9865. option: "select"
  9866. },
  9867. needChild = {
  9868. ol: "li",
  9869. ul: "li"
  9870. };
  9871. function text(parent, data) {
  9872. if (needChild[parent.tagName]) {
  9873. var tmpNode = uNode.createElement(needChild[parent.tagName]);
  9874. parent.appendChild(tmpNode);
  9875. tmpNode.appendChild(uNode.createText(data));
  9876. parent = tmpNode;
  9877. } else {
  9878. parent.appendChild(uNode.createText(data));
  9879. }
  9880. }
  9881. function element(parent, tagName, htmlattr) {
  9882. var needParentTag;
  9883. if ((needParentTag = needParentNode[tagName])) {
  9884. var tmpParent = parent,
  9885. hasParent;
  9886. while (tmpParent.type != "root") {
  9887. if (
  9888. utils.isArray(needParentTag)
  9889. ? utils.indexOf(needParentTag, tmpParent.tagName) != -1
  9890. : needParentTag == tmpParent.tagName
  9891. ) {
  9892. parent = tmpParent;
  9893. hasParent = true;
  9894. break;
  9895. }
  9896. tmpParent = tmpParent.parentNode;
  9897. }
  9898. if (!hasParent) {
  9899. parent = element(
  9900. parent,
  9901. utils.isArray(needParentTag) ? needParentTag[0] : needParentTag
  9902. );
  9903. }
  9904. }
  9905. //按dtd处理嵌套
  9906. // if(parent.type != 'root' && !dtd[parent.tagName][tagName])
  9907. // parent = parent.parentNode;
  9908. var elm = new uNode({
  9909. parentNode: parent,
  9910. type: "element",
  9911. tagName: tagName.toLowerCase(),
  9912. //是自闭合的处理一下
  9913. children: dtd.$empty[tagName] ? null : []
  9914. });
  9915. //如果属性存在,处理属性
  9916. if (htmlattr) {
  9917. var attrs = {},
  9918. match;
  9919. while ((match = re_attr.exec(htmlattr))) {
  9920. attrs[match[1].toLowerCase()] = notTransAttrs[match[1].toLowerCase()]
  9921. ? match[2] || match[3] || match[4]
  9922. : utils.unhtml(match[2] || match[3] || match[4]);
  9923. }
  9924. elm.attrs = attrs;
  9925. }
  9926. //trace:3970
  9927. // //如果parent下不能放elm
  9928. // if(dtd.$inline[parent.tagName] && dtd.$block[elm.tagName] && !dtd[parent.tagName][elm.tagName]){
  9929. // parent = parent.parentNode;
  9930. // elm.parentNode = parent;
  9931. // }
  9932. parent.children.push(elm);
  9933. //如果是自闭合节点返回父亲节点
  9934. return dtd.$empty[tagName] ? parent : elm;
  9935. }
  9936. function comment(parent, data) {
  9937. parent.children.push(
  9938. new uNode({
  9939. type: "comment",
  9940. data: data,
  9941. parentNode: parent
  9942. })
  9943. );
  9944. }
  9945. var match,
  9946. currentIndex = 0,
  9947. nextIndex = 0;
  9948. //设置根节点
  9949. var root = new uNode({
  9950. type: "root",
  9951. children: []
  9952. });
  9953. var currentParent = root;
  9954. while ((match = re_tag.exec(htmlstr))) {
  9955. currentIndex = match.index;
  9956. try {
  9957. if (currentIndex > nextIndex) {
  9958. //text node
  9959. text(currentParent, htmlstr.slice(nextIndex, currentIndex));
  9960. }
  9961. if (match[3]) {
  9962. if (dtd.$cdata[currentParent.tagName]) {
  9963. text(currentParent, match[0]);
  9964. } else {
  9965. //start tag
  9966. currentParent = element(
  9967. currentParent,
  9968. match[3].toLowerCase(),
  9969. match[4]
  9970. );
  9971. }
  9972. } else if (match[1]) {
  9973. if (currentParent.type != "root") {
  9974. if (dtd.$cdata[currentParent.tagName] && !dtd.$cdata[match[1]]) {
  9975. text(currentParent, match[0]);
  9976. } else {
  9977. var tmpParent = currentParent;
  9978. while (
  9979. currentParent.type == "element" &&
  9980. currentParent.tagName != match[1].toLowerCase()
  9981. ) {
  9982. currentParent = currentParent.parentNode;
  9983. if (currentParent.type == "root") {
  9984. currentParent = tmpParent;
  9985. throw "break";
  9986. }
  9987. }
  9988. //end tag
  9989. currentParent = currentParent.parentNode;
  9990. }
  9991. }
  9992. } else if (match[2]) {
  9993. //comment
  9994. comment(currentParent, match[2]);
  9995. }
  9996. } catch (e) {}
  9997. nextIndex = re_tag.lastIndex;
  9998. }
  9999. //如果结束是文本,就有可能丢掉,所以这里手动判断一下
  10000. //例如 <li>sdfsdfsdf<li>sdfsdfsdfsdf
  10001. if (nextIndex < htmlstr.length) {
  10002. text(currentParent, htmlstr.slice(nextIndex));
  10003. }
  10004. return root;
  10005. });
  10006. // core/filternode.js
  10007. /**
  10008. * UE过滤节点的静态方法
  10009. * @file
  10010. */
  10011. /**
  10012. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  10013. * @module UE
  10014. */
  10015. /**
  10016. * 根据传入节点和过滤规则过滤相应节点
  10017. * @module UE
  10018. * @since 1.2.6.1
  10019. * @method filterNode
  10020. * @param { Object } root 指定root节点
  10021. * @param { Object } rules 过滤规则json对象
  10022. * @example
  10023. * ```javascript
  10024. * UE.filterNode(root,editor.options.filterRules);
  10025. * ```
  10026. */
  10027. var filterNode = (UE.filterNode = (function() {
  10028. function filterNode(node, rules) {
  10029. switch (node.type) {
  10030. case "text":
  10031. break;
  10032. case "element":
  10033. var val;
  10034. if ((val = rules[node.tagName])) {
  10035. if (val === "-") {
  10036. node.parentNode.removeChild(node);
  10037. } else if (utils.isFunction(val)) {
  10038. var parentNode = node.parentNode,
  10039. index = node.getIndex();
  10040. val(node);
  10041. if (node.parentNode) {
  10042. if (node.children) {
  10043. for (var i = 0, ci; (ci = node.children[i]); ) {
  10044. filterNode(ci, rules);
  10045. if (ci.parentNode) {
  10046. i++;
  10047. }
  10048. }
  10049. }
  10050. } else {
  10051. for (var i = index, ci; (ci = parentNode.children[i]); ) {
  10052. filterNode(ci, rules);
  10053. if (ci.parentNode) {
  10054. i++;
  10055. }
  10056. }
  10057. }
  10058. } else {
  10059. var attrs = val["$"];
  10060. if (attrs && node.attrs) {
  10061. var tmpAttrs = {},
  10062. tmpVal;
  10063. for (var a in attrs) {
  10064. tmpVal = node.getAttr(a);
  10065. //todo 只先对style单独处理
  10066. if (a == "style" && utils.isArray(attrs[a])) {
  10067. var tmpCssStyle = [];
  10068. utils.each(attrs[a], function(v) {
  10069. var tmp;
  10070. if ((tmp = node.getStyle(v))) {
  10071. tmpCssStyle.push(v + ":" + tmp);
  10072. }
  10073. });
  10074. tmpVal = tmpCssStyle.join(";");
  10075. }
  10076. if (tmpVal) {
  10077. tmpAttrs[a] = tmpVal;
  10078. }
  10079. }
  10080. node.attrs = tmpAttrs;
  10081. }
  10082. if (node.children) {
  10083. for (var i = 0, ci; (ci = node.children[i]); ) {
  10084. filterNode(ci, rules);
  10085. if (ci.parentNode) {
  10086. i++;
  10087. }
  10088. }
  10089. }
  10090. }
  10091. } else {
  10092. //如果不在名单里扣出子节点并删除该节点,cdata除外
  10093. if (dtd.$cdata[node.tagName]) {
  10094. node.parentNode.removeChild(node);
  10095. } else {
  10096. var parentNode = node.parentNode,
  10097. index = node.getIndex();
  10098. node.parentNode.removeChild(node, true);
  10099. for (var i = index, ci; (ci = parentNode.children[i]); ) {
  10100. filterNode(ci, rules);
  10101. if (ci.parentNode) {
  10102. i++;
  10103. }
  10104. }
  10105. }
  10106. }
  10107. break;
  10108. case "comment":
  10109. node.parentNode.removeChild(node);
  10110. }
  10111. }
  10112. return function(root, rules) {
  10113. if (utils.isEmptyObject(rules)) {
  10114. return root;
  10115. }
  10116. var val;
  10117. if ((val = rules["-"])) {
  10118. utils.each(val.split(" "), function(k) {
  10119. rules[k] = "-";
  10120. });
  10121. }
  10122. for (var i = 0, ci; (ci = root.children[i]); ) {
  10123. filterNode(ci, rules);
  10124. if (ci.parentNode) {
  10125. i++;
  10126. }
  10127. }
  10128. return root;
  10129. };
  10130. })());
  10131. // core/plugin.js
  10132. /**
  10133. * Created with JetBrains PhpStorm.
  10134. * User: campaign
  10135. * Date: 10/8/13
  10136. * Time: 6:15 PM
  10137. * To change this template use File | Settings | File Templates.
  10138. */
  10139. UE.plugin = (function() {
  10140. var _plugins = {};
  10141. return {
  10142. register: function(pluginName, fn, oldOptionName, afterDisabled) {
  10143. if (oldOptionName && utils.isFunction(oldOptionName)) {
  10144. afterDisabled = oldOptionName;
  10145. oldOptionName = null;
  10146. }
  10147. _plugins[pluginName] = {
  10148. optionName: oldOptionName || pluginName,
  10149. execFn: fn,
  10150. //当插件被禁用时执行
  10151. afterDisabled: afterDisabled
  10152. };
  10153. },
  10154. load: function(editor) {
  10155. utils.each(_plugins, function(plugin) {
  10156. var _export = plugin.execFn.call(editor);
  10157. if (editor.options[plugin.optionName] !== false) {
  10158. if (_export) {
  10159. //后边需要再做扩展
  10160. utils.each(_export, function(v, k) {
  10161. switch (k.toLowerCase()) {
  10162. case "shortcutkey":
  10163. editor.addshortcutkey(v);
  10164. break;
  10165. case "bindevents":
  10166. utils.each(v, function(fn, eventName) {
  10167. editor.addListener(eventName, fn);
  10168. });
  10169. break;
  10170. case "bindmultievents":
  10171. utils.each(utils.isArray(v) ? v : [v], function(event) {
  10172. var types = utils.trim(event.type).split(/\s+/);
  10173. utils.each(types, function(eventName) {
  10174. editor.addListener(eventName, event.handler);
  10175. });
  10176. });
  10177. break;
  10178. case "commands":
  10179. utils.each(v, function(execFn, execName) {
  10180. editor.commands[execName] = execFn;
  10181. });
  10182. break;
  10183. case "outputrule":
  10184. editor.addOutputRule(v);
  10185. break;
  10186. case "inputrule":
  10187. editor.addInputRule(v);
  10188. break;
  10189. case "defaultoptions":
  10190. editor.setOpt(v);
  10191. }
  10192. });
  10193. }
  10194. } else if (plugin.afterDisabled) {
  10195. plugin.afterDisabled.call(editor);
  10196. }
  10197. });
  10198. //向下兼容
  10199. utils.each(UE.plugins, function(plugin) {
  10200. plugin.call(editor);
  10201. });
  10202. },
  10203. run: function(pluginName, editor) {
  10204. var plugin = _plugins[pluginName];
  10205. if (plugin) {
  10206. plugin.exeFn.call(editor);
  10207. }
  10208. }
  10209. };
  10210. })();
  10211. // core/keymap.js
  10212. var keymap = (UE.keymap = {
  10213. Backspace: 8,
  10214. Tab: 9,
  10215. Enter: 13,
  10216. Shift: 16,
  10217. Control: 17,
  10218. Alt: 18,
  10219. CapsLock: 20,
  10220. Esc: 27,
  10221. Spacebar: 32,
  10222. PageUp: 33,
  10223. PageDown: 34,
  10224. End: 35,
  10225. Home: 36,
  10226. Left: 37,
  10227. Up: 38,
  10228. Right: 39,
  10229. Down: 40,
  10230. Insert: 45,
  10231. Del: 46,
  10232. NumLock: 144,
  10233. Cmd: 91,
  10234. "=": 187,
  10235. "-": 189,
  10236. b: 66,
  10237. i: 73,
  10238. //回退
  10239. z: 90,
  10240. y: 89,
  10241. //粘贴
  10242. v: 86,
  10243. x: 88,
  10244. s: 83,
  10245. n: 78
  10246. });
  10247. // core/localstorage.js
  10248. //存储媒介封装
  10249. var LocalStorage = (UE.LocalStorage = (function() {
  10250. var storage = window.localStorage || getUserData() || null,
  10251. LOCAL_FILE = "localStorage";
  10252. return {
  10253. saveLocalData: function(key, data) {
  10254. if (storage && data) {
  10255. storage.setItem(key, data);
  10256. return true;
  10257. }
  10258. return false;
  10259. },
  10260. getLocalData: function(key) {
  10261. if (storage) {
  10262. return storage.getItem(key);
  10263. }
  10264. return null;
  10265. },
  10266. removeItem: function(key) {
  10267. storage && storage.removeItem(key);
  10268. }
  10269. };
  10270. function getUserData() {
  10271. var container = document.createElement("div");
  10272. container.style.display = "none";
  10273. if (!container.addBehavior) {
  10274. return null;
  10275. }
  10276. container.addBehavior("#default#userdata");
  10277. return {
  10278. getItem: function(key) {
  10279. var result = null;
  10280. try {
  10281. document.body.appendChild(container);
  10282. container.load(LOCAL_FILE);
  10283. result = container.getAttribute(key);
  10284. document.body.removeChild(container);
  10285. } catch (e) {}
  10286. return result;
  10287. },
  10288. setItem: function(key, value) {
  10289. document.body.appendChild(container);
  10290. container.setAttribute(key, value);
  10291. container.save(LOCAL_FILE);
  10292. document.body.removeChild(container);
  10293. },
  10294. //// 暂时没有用到
  10295. //clear: function () {
  10296. //
  10297. // var expiresTime = new Date();
  10298. // expiresTime.setFullYear(expiresTime.getFullYear() - 1);
  10299. // document.body.appendChild(container);
  10300. // container.expires = expiresTime.toUTCString();
  10301. // container.save(LOCAL_FILE);
  10302. // document.body.removeChild(container);
  10303. //
  10304. //},
  10305. removeItem: function(key) {
  10306. document.body.appendChild(container);
  10307. container.removeAttribute(key);
  10308. container.save(LOCAL_FILE);
  10309. document.body.removeChild(container);
  10310. }
  10311. };
  10312. }
  10313. })());
  10314. (function() {
  10315. var ROOTKEY = "ueditor_preference";
  10316. UE.Editor.prototype.setPreferences = function(key, value) {
  10317. var obj = {};
  10318. if (utils.isString(key)) {
  10319. obj[key] = value;
  10320. } else {
  10321. obj = key;
  10322. }
  10323. var data = LocalStorage.getLocalData(ROOTKEY);
  10324. if (data && (data = utils.str2json(data))) {
  10325. utils.extend(data, obj);
  10326. } else {
  10327. data = obj;
  10328. }
  10329. data && LocalStorage.saveLocalData(ROOTKEY, utils.json2str(data));
  10330. };
  10331. UE.Editor.prototype.getPreferences = function(key) {
  10332. var data = LocalStorage.getLocalData(ROOTKEY);
  10333. if (data && (data = utils.str2json(data))) {
  10334. return key ? data[key] : data;
  10335. }
  10336. return null;
  10337. };
  10338. UE.Editor.prototype.removePreferences = function(key) {
  10339. var data = LocalStorage.getLocalData(ROOTKEY);
  10340. if (data && (data = utils.str2json(data))) {
  10341. data[key] = undefined;
  10342. delete data[key];
  10343. }
  10344. data && LocalStorage.saveLocalData(ROOTKEY, utils.json2str(data));
  10345. };
  10346. })();
  10347. // plugins/defaultfilter.js
  10348. ///import core
  10349. ///plugin 编辑器默认的过滤转换机制
  10350. UE.plugins["defaultfilter"] = function() {
  10351. var me = this;
  10352. me.setOpt({
  10353. allowDivTransToP: true,
  10354. disabledTableInTable: true,
  10355. rgb2Hex: true
  10356. });
  10357. //默认的过滤处理
  10358. //进入编辑器的内容处理
  10359. me.addInputRule(function(root) {
  10360. var allowDivTransToP = this.options.allowDivTransToP;
  10361. var val;
  10362. function tdParent(node) {
  10363. while (node && node.type == "element") {
  10364. if (node.tagName == "td") {
  10365. return true;
  10366. }
  10367. node = node.parentNode;
  10368. }
  10369. return false;
  10370. }
  10371. //进行默认的处理
  10372. root.traversal(function(node) {
  10373. if (node.type == "element") {
  10374. if (
  10375. !dtd.$cdata[node.tagName] &&
  10376. me.options.autoClearEmptyNode &&
  10377. dtd.$inline[node.tagName] &&
  10378. !dtd.$empty[node.tagName] &&
  10379. (!node.attrs || utils.isEmptyObject(node.attrs))
  10380. ) {
  10381. if (!node.firstChild()) node.parentNode.removeChild(node);
  10382. else if (
  10383. node.tagName == "span" &&
  10384. (!node.attrs || utils.isEmptyObject(node.attrs))
  10385. ) {
  10386. node.parentNode.removeChild(node, true);
  10387. }
  10388. return;
  10389. }
  10390. switch (node.tagName) {
  10391. case "style":
  10392. case "script":
  10393. node.setAttr({
  10394. cdata_tag: node.tagName,
  10395. cdata_data: node.innerHTML() || "",
  10396. _ue_custom_node_: "true"
  10397. });
  10398. node.tagName = "div";
  10399. node.innerHTML("");
  10400. break;
  10401. case "a":
  10402. if ((val = node.getAttr("href"))) {
  10403. node.setAttr("_href", val);
  10404. }
  10405. break;
  10406. case "img":
  10407. //todo base64暂时去掉,后边做远程图片上传后,干掉这个
  10408. if ((val = node.getAttr("src"))) {
  10409. if (/^data:/.test(val)) {
  10410. node.parentNode.removeChild(node);
  10411. break;
  10412. }
  10413. }
  10414. node.setAttr("_src", node.getAttr("src"));
  10415. break;
  10416. case "span":
  10417. if (browser.webkit && (val = node.getStyle("white-space"))) {
  10418. if (/nowrap|normal/.test(val)) {
  10419. node.setStyle("white-space", "");
  10420. if (
  10421. me.options.autoClearEmptyNode &&
  10422. utils.isEmptyObject(node.attrs)
  10423. ) {
  10424. node.parentNode.removeChild(node, true);
  10425. }
  10426. }
  10427. }
  10428. val = node.getAttr("id");
  10429. if (val && /^_baidu_bookmark_/i.test(val)) {
  10430. node.parentNode.removeChild(node);
  10431. }
  10432. break;
  10433. case "p":
  10434. if ((val = node.getAttr("align"))) {
  10435. node.setAttr("align");
  10436. node.setStyle("text-align", val);
  10437. }
  10438. //trace:3431
  10439. // var cssStyle = node.getAttr('style');
  10440. // if (cssStyle) {
  10441. // cssStyle = cssStyle.replace(/(margin|padding)[^;]+/g, '');
  10442. // node.setAttr('style', cssStyle)
  10443. //
  10444. // }
  10445. //p标签不允许嵌套
  10446. utils.each(node.children, function(n) {
  10447. if (n.type == "element" && n.tagName == "p") {
  10448. var next = n.nextSibling();
  10449. node.parentNode.insertAfter(n, node);
  10450. var last = n;
  10451. while (next) {
  10452. var tmp = next.nextSibling();
  10453. node.parentNode.insertAfter(next, last);
  10454. last = next;
  10455. next = tmp;
  10456. }
  10457. return false;
  10458. }
  10459. });
  10460. if (!node.firstChild()) {
  10461. node.innerHTML(browser.ie ? "&nbsp;" : "<br/>");
  10462. }
  10463. break;
  10464. case "div":
  10465. if (node.getAttr("cdata_tag")) {
  10466. break;
  10467. }
  10468. //针对代码这里不处理插入代码的div
  10469. val = node.getAttr("class");
  10470. if (val && /^line number\d+/.test(val)) {
  10471. break;
  10472. }
  10473. if (!allowDivTransToP) {
  10474. break;
  10475. }
  10476. var tmpNode,
  10477. p = UE.uNode.createElement("p");
  10478. while ((tmpNode = node.firstChild())) {
  10479. if (
  10480. tmpNode.type == "text" ||
  10481. !UE.dom.dtd.$block[tmpNode.tagName]
  10482. ) {
  10483. p.appendChild(tmpNode);
  10484. } else {
  10485. if (p.firstChild()) {
  10486. node.parentNode.insertBefore(p, node);
  10487. p = UE.uNode.createElement("p");
  10488. } else {
  10489. node.parentNode.insertBefore(tmpNode, node);
  10490. }
  10491. }
  10492. }
  10493. if (p.firstChild()) {
  10494. node.parentNode.insertBefore(p, node);
  10495. }
  10496. node.parentNode.removeChild(node);
  10497. break;
  10498. case "dl":
  10499. node.tagName = "ul";
  10500. break;
  10501. case "dt":
  10502. case "dd":
  10503. node.tagName = "li";
  10504. break;
  10505. case "li":
  10506. var className = node.getAttr("class");
  10507. if (!className || !/list\-/.test(className)) {
  10508. node.setAttr();
  10509. }
  10510. var tmpNodes = node.getNodesByTagName("ol ul");
  10511. UE.utils.each(tmpNodes, function(n) {
  10512. node.parentNode.insertAfter(n, node);
  10513. });
  10514. break;
  10515. case "td":
  10516. case "th":
  10517. case "caption":
  10518. if (!node.children || !node.children.length) {
  10519. node.appendChild(
  10520. browser.ie11below
  10521. ? UE.uNode.createText(" ")
  10522. : UE.uNode.createElement("br")
  10523. );
  10524. }
  10525. break;
  10526. case "table":
  10527. if (me.options.disabledTableInTable && tdParent(node)) {
  10528. node.parentNode.insertBefore(
  10529. UE.uNode.createText(node.innerText()),
  10530. node
  10531. );
  10532. node.parentNode.removeChild(node);
  10533. }
  10534. }
  10535. }
  10536. // if(node.type == 'comment'){
  10537. // node.parentNode.removeChild(node);
  10538. // }
  10539. });
  10540. });
  10541. //从编辑器出去的内容处理
  10542. me.addOutputRule(function(root) {
  10543. var val;
  10544. root.traversal(function(node) {
  10545. if (node.type == "element") {
  10546. if (
  10547. me.options.autoClearEmptyNode &&
  10548. dtd.$inline[node.tagName] &&
  10549. !dtd.$empty[node.tagName] &&
  10550. (!node.attrs || utils.isEmptyObject(node.attrs))
  10551. ) {
  10552. if (!node.firstChild()) node.parentNode.removeChild(node);
  10553. else if (
  10554. node.tagName == "span" &&
  10555. (!node.attrs || utils.isEmptyObject(node.attrs))
  10556. ) {
  10557. node.parentNode.removeChild(node, true);
  10558. }
  10559. return;
  10560. }
  10561. switch (node.tagName) {
  10562. case "div":
  10563. if ((val = node.getAttr("cdata_tag"))) {
  10564. node.tagName = val;
  10565. node.appendChild(UE.uNode.createText(node.getAttr("cdata_data")));
  10566. node.setAttr({
  10567. cdata_tag: "",
  10568. cdata_data: "",
  10569. _ue_custom_node_: ""
  10570. });
  10571. }
  10572. break;
  10573. case "a":
  10574. if ((val = node.getAttr("_href"))) {
  10575. node.setAttr({
  10576. href: utils.html(val),
  10577. _href: ""
  10578. });
  10579. }
  10580. break;
  10581. break;
  10582. case "span":
  10583. val = node.getAttr("id");
  10584. if (val && /^_baidu_bookmark_/i.test(val)) {
  10585. node.parentNode.removeChild(node);
  10586. }
  10587. //将color的rgb格式转换为#16进制格式
  10588. if (me.getOpt("rgb2Hex")) {
  10589. var cssStyle = node.getAttr("style");
  10590. if (cssStyle) {
  10591. node.setAttr(
  10592. "style",
  10593. cssStyle.replace(/rgba?\(([\d,\s]+)\)/g, function(a, value) {
  10594. var array = value.split(",");
  10595. if (array.length > 3) return "";
  10596. value = "#";
  10597. for (var i = 0, color; (color = array[i++]); ) {
  10598. color = parseInt(
  10599. color.replace(/[^\d]/gi, ""),
  10600. 10
  10601. ).toString(16);
  10602. value += color.length == 1 ? "0" + color : color;
  10603. }
  10604. return value.toUpperCase();
  10605. })
  10606. );
  10607. }
  10608. }
  10609. break;
  10610. case "img":
  10611. if ((val = node.getAttr("_src"))) {
  10612. node.setAttr({
  10613. src: node.getAttr("_src"),
  10614. _src: ""
  10615. });
  10616. }
  10617. }
  10618. }
  10619. });
  10620. });
  10621. };
  10622. // plugins/inserthtml.js
  10623. /**
  10624. * 插入html字符串插件
  10625. * @file
  10626. * @since 1.2.6.1
  10627. */
  10628. /**
  10629. * 插入html代码
  10630. * @command inserthtml
  10631. * @method execCommand
  10632. * @param { String } cmd 命令字符串
  10633. * @param { String } html 插入的html字符串
  10634. * @remaind 插入的标签内容是在当前的选区位置上插入,如果当前是闭合状态,那直接插入内容, 如果当前是选中状态,将先清除当前选中内容后,再做插入
  10635. * @warning 注意:该命令会对当前选区的位置,对插入的内容进行过滤转换处理。 过滤的规则遵循html语意化的原则。
  10636. * @example
  10637. * ```javascript
  10638. * //xxx[BB]xxx 当前选区为非闭合选区,选中BB这两个文本
  10639. * //执行命令,插入<b>CC</b>
  10640. * //插入后的效果 xxx<b>CC</b>xxx
  10641. * //<p>xx|xxx</p> 当前选区为闭合状态
  10642. * //插入<p>CC</p>
  10643. * //结果 <p>xx</p><p>CC</p><p>xxx</p>
  10644. * //<p>xxxx</p>|</p>xxx</p> 当前选区在两个p标签之间
  10645. * //插入 xxxx
  10646. * //结果 <p>xxxx</p><p>xxxx</p></p>xxx</p>
  10647. * ```
  10648. */
  10649. UE.commands["inserthtml"] = {
  10650. execCommand: function(command, html, notNeedFilter) {
  10651. var me = this,
  10652. range,
  10653. div;
  10654. if (!html) {
  10655. return;
  10656. }
  10657. if (me.fireEvent("beforeinserthtml", html) === true) {
  10658. return;
  10659. }
  10660. range = me.selection.getRange();
  10661. div = range.document.createElement("div");
  10662. div.style.display = "inline";
  10663. if (!notNeedFilter) {
  10664. var root = UE.htmlparser(html);
  10665. //如果给了过滤规则就先进行过滤
  10666. if (me.options.filterRules) {
  10667. UE.filterNode(root, me.options.filterRules);
  10668. }
  10669. //执行默认的处理
  10670. me.filterInputRule(root);
  10671. html = root.toHtml();
  10672. }
  10673. div.innerHTML = utils.trim(html);
  10674. if (!range.collapsed) {
  10675. var tmpNode = range.startContainer;
  10676. if (domUtils.isFillChar(tmpNode)) {
  10677. range.setStartBefore(tmpNode);
  10678. }
  10679. tmpNode = range.endContainer;
  10680. if (domUtils.isFillChar(tmpNode)) {
  10681. range.setEndAfter(tmpNode);
  10682. }
  10683. range.txtToElmBoundary();
  10684. //结束边界可能放到了br的前边,要把br包含进来
  10685. // x[xxx]<br/>
  10686. if (range.endContainer && range.endContainer.nodeType == 1) {
  10687. tmpNode = range.endContainer.childNodes[range.endOffset];
  10688. if (tmpNode && domUtils.isBr(tmpNode)) {
  10689. range.setEndAfter(tmpNode);
  10690. }
  10691. }
  10692. if (range.startOffset == 0) {
  10693. tmpNode = range.startContainer;
  10694. if (domUtils.isBoundaryNode(tmpNode, "firstChild")) {
  10695. tmpNode = range.endContainer;
  10696. if (
  10697. range.endOffset ==
  10698. (tmpNode.nodeType == 3
  10699. ? tmpNode.nodeValue.length
  10700. : tmpNode.childNodes.length) &&
  10701. domUtils.isBoundaryNode(tmpNode, "lastChild")
  10702. ) {
  10703. me.body.innerHTML = "<p>" + (browser.ie ? "" : "<br/>") + "</p>";
  10704. range.setStart(me.body.firstChild, 0).collapse(true);
  10705. }
  10706. }
  10707. }
  10708. !range.collapsed && range.deleteContents();
  10709. if (range.startContainer.nodeType == 1) {
  10710. var child = range.startContainer.childNodes[range.startOffset],
  10711. pre;
  10712. if (
  10713. child &&
  10714. domUtils.isBlockElm(child) &&
  10715. (pre = child.previousSibling) &&
  10716. domUtils.isBlockElm(pre)
  10717. ) {
  10718. range.setEnd(pre, pre.childNodes.length).collapse();
  10719. while (child.firstChild) {
  10720. pre.appendChild(child.firstChild);
  10721. }
  10722. domUtils.remove(child);
  10723. }
  10724. }
  10725. }
  10726. var child,
  10727. parent,
  10728. pre,
  10729. tmp,
  10730. hadBreak = 0,
  10731. nextNode;
  10732. //如果当前位置选中了fillchar要干掉,要不会产生空行
  10733. if (range.inFillChar()) {
  10734. child = range.startContainer;
  10735. if (domUtils.isFillChar(child)) {
  10736. range.setStartBefore(child).collapse(true);
  10737. domUtils.remove(child);
  10738. } else if (domUtils.isFillChar(child, true)) {
  10739. child.nodeValue = child.nodeValue.replace(fillCharReg, "");
  10740. range.startOffset--;
  10741. range.collapsed && range.collapse(true);
  10742. }
  10743. }
  10744. //列表单独处理
  10745. var li = domUtils.findParentByTagName(range.startContainer, "li", true);
  10746. if (li) {
  10747. var next, last;
  10748. while ((child = div.firstChild)) {
  10749. //针对hr单独处理一下先
  10750. while (
  10751. child &&
  10752. (child.nodeType == 3 ||
  10753. !domUtils.isBlockElm(child) ||
  10754. child.tagName == "HR")
  10755. ) {
  10756. next = child.nextSibling;
  10757. range.insertNode(child).collapse();
  10758. last = child;
  10759. child = next;
  10760. }
  10761. if (child) {
  10762. if (/^(ol|ul)$/i.test(child.tagName)) {
  10763. while (child.firstChild) {
  10764. last = child.firstChild;
  10765. domUtils.insertAfter(li, child.firstChild);
  10766. li = li.nextSibling;
  10767. }
  10768. domUtils.remove(child);
  10769. } else {
  10770. var tmpLi;
  10771. next = child.nextSibling;
  10772. tmpLi = me.document.createElement("li");
  10773. domUtils.insertAfter(li, tmpLi);
  10774. tmpLi.appendChild(child);
  10775. last = child;
  10776. child = next;
  10777. li = tmpLi;
  10778. }
  10779. }
  10780. }
  10781. li = domUtils.findParentByTagName(range.startContainer, "li", true);
  10782. if (domUtils.isEmptyBlock(li)) {
  10783. domUtils.remove(li);
  10784. }
  10785. if (last) {
  10786. range.setStartAfter(last).collapse(true).select(true);
  10787. }
  10788. } else {
  10789. while ((child = div.firstChild)) {
  10790. if (hadBreak) {
  10791. var p = me.document.createElement("p");
  10792. while (child && (child.nodeType == 3 || !dtd.$block[child.tagName])) {
  10793. nextNode = child.nextSibling;
  10794. p.appendChild(child);
  10795. child = nextNode;
  10796. }
  10797. if (p.firstChild) {
  10798. child = p;
  10799. }
  10800. }
  10801. range.insertNode(child);
  10802. nextNode = child.nextSibling;
  10803. if (
  10804. !hadBreak &&
  10805. child.nodeType == domUtils.NODE_ELEMENT &&
  10806. domUtils.isBlockElm(child)
  10807. ) {
  10808. parent = domUtils.findParent(child, function(node) {
  10809. return domUtils.isBlockElm(node);
  10810. });
  10811. if (
  10812. parent &&
  10813. parent.tagName.toLowerCase() != "body" &&
  10814. !(
  10815. dtd[parent.tagName][child.nodeName] && child.parentNode === parent
  10816. )
  10817. ) {
  10818. if (!dtd[parent.tagName][child.nodeName]) {
  10819. pre = parent;
  10820. } else {
  10821. tmp = child.parentNode;
  10822. while (tmp !== parent) {
  10823. pre = tmp;
  10824. tmp = tmp.parentNode;
  10825. }
  10826. }
  10827. domUtils.breakParent(child, pre || tmp);
  10828. //去掉break后前一个多余的节点 <p>|<[p> ==> <p></p><div></div><p>|</p>
  10829. var pre = child.previousSibling;
  10830. domUtils.trimWhiteTextNode(pre);
  10831. if (!pre.childNodes.length) {
  10832. domUtils.remove(pre);
  10833. }
  10834. //trace:2012,在非ie的情况,切开后剩下的节点有可能不能点入光标添加br占位
  10835. if (
  10836. !browser.ie &&
  10837. (next = child.nextSibling) &&
  10838. domUtils.isBlockElm(next) &&
  10839. next.lastChild &&
  10840. !domUtils.isBr(next.lastChild)
  10841. ) {
  10842. next.appendChild(me.document.createElement("br"));
  10843. }
  10844. hadBreak = 1;
  10845. }
  10846. }
  10847. var next = child.nextSibling;
  10848. if (!div.firstChild && next && domUtils.isBlockElm(next)) {
  10849. range.setStart(next, 0).collapse(true);
  10850. break;
  10851. }
  10852. range.setEndAfter(child).collapse();
  10853. }
  10854. child = range.startContainer;
  10855. if (nextNode && domUtils.isBr(nextNode)) {
  10856. domUtils.remove(nextNode);
  10857. }
  10858. //用chrome可能有空白展位符
  10859. if (domUtils.isBlockElm(child) && domUtils.isEmptyNode(child)) {
  10860. if ((nextNode = child.nextSibling)) {
  10861. domUtils.remove(child);
  10862. if (nextNode.nodeType == 1 && dtd.$block[nextNode.tagName]) {
  10863. range.setStart(nextNode, 0).collapse(true).shrinkBoundary();
  10864. }
  10865. } else {
  10866. try {
  10867. child.innerHTML = browser.ie ? domUtils.fillChar : "<br/>";
  10868. } catch (e) {
  10869. range.setStartBefore(child);
  10870. domUtils.remove(child);
  10871. }
  10872. }
  10873. }
  10874. //加上true因为在删除表情等时会删两次,第一次是删的fillData
  10875. try {
  10876. range.select(true);
  10877. } catch (e) {}
  10878. }
  10879. setTimeout(function() {
  10880. range = me.selection.getRange();
  10881. range.scrollToView(
  10882. me.autoHeightEnabled,
  10883. me.autoHeightEnabled ? domUtils.getXY(me.iframe).y : 0
  10884. );
  10885. me.fireEvent("afterinserthtml", html);
  10886. }, 200);
  10887. }
  10888. };
  10889. // plugins/autotypeset.js
  10890. /**
  10891. * 自动排版
  10892. * @file
  10893. * @since 1.2.6.1
  10894. */
  10895. /**
  10896. * 对当前编辑器的内容执行自动排版, 排版的行为根据config配置文件里的“autotypeset”选项进行控制。
  10897. * @command autotypeset
  10898. * @method execCommand
  10899. * @param { String } cmd 命令字符串
  10900. * @example
  10901. * ```javascript
  10902. * editor.execCommand( 'autotypeset' );
  10903. * ```
  10904. */
  10905. UE.plugins["autotypeset"] = function() {
  10906. this.setOpt({
  10907. autotypeset: {
  10908. mergeEmptyline: true, //合并空行
  10909. removeClass: true, //去掉冗余的class
  10910. removeEmptyline: false, //去掉空行
  10911. textAlign: "left", //段落的排版方式,可以是 left,right,center,justify 去掉这个属性表示不执行排版
  10912. imageBlockLine: "center", //图片的浮动方式,独占一行剧中,左右浮动,默认: center,left,right,none 去掉这个属性表示不执行排版
  10913. pasteFilter: false, //根据规则过滤没事粘贴进来的内容
  10914. clearFontSize: false, //去掉所有的内嵌字号,使用编辑器默认的字号
  10915. clearFontFamily: false, //去掉所有的内嵌字体,使用编辑器默认的字体
  10916. removeEmptyNode: false, // 去掉空节点
  10917. //可以去掉的标签
  10918. removeTagNames: utils.extend({ div: 1 }, dtd.$removeEmpty),
  10919. indent: false, // 行首缩进
  10920. indentValue: "2em", //行首缩进的大小
  10921. bdc2sb: false,
  10922. tobdc: false
  10923. }
  10924. });
  10925. var me = this,
  10926. opt = me.options.autotypeset,
  10927. remainClass = {
  10928. selectTdClass: 1,
  10929. pagebreak: 1,
  10930. anchorclass: 1
  10931. },
  10932. remainTag = {
  10933. li: 1
  10934. },
  10935. tags = {
  10936. div: 1,
  10937. p: 1,
  10938. //trace:2183 这些也认为是行
  10939. blockquote: 1,
  10940. center: 1,
  10941. h1: 1,
  10942. h2: 1,
  10943. h3: 1,
  10944. h4: 1,
  10945. h5: 1,
  10946. h6: 1,
  10947. span: 1
  10948. },
  10949. highlightCont;
  10950. //升级了版本,但配置项目里没有autotypeset
  10951. if (!opt) {
  10952. return;
  10953. }
  10954. readLocalOpts();
  10955. function isLine(node, notEmpty) {
  10956. if (!node || node.nodeType == 3) return 0;
  10957. if (domUtils.isBr(node)) return 1;
  10958. if (node && node.parentNode && tags[node.tagName.toLowerCase()]) {
  10959. if (
  10960. (highlightCont && highlightCont.contains(node)) ||
  10961. node.getAttribute("pagebreak")
  10962. ) {
  10963. return 0;
  10964. }
  10965. return notEmpty
  10966. ? !domUtils.isEmptyBlock(node)
  10967. : domUtils.isEmptyBlock(
  10968. node,
  10969. new RegExp("[\\s" + domUtils.fillChar + "]", "g")
  10970. );
  10971. }
  10972. }
  10973. function removeNotAttributeSpan(node) {
  10974. if (!node.style.cssText) {
  10975. domUtils.removeAttributes(node, ["style"]);
  10976. if (
  10977. node.tagName.toLowerCase() == "span" &&
  10978. domUtils.hasNoAttributes(node)
  10979. ) {
  10980. domUtils.remove(node, true);
  10981. }
  10982. }
  10983. }
  10984. function autotype(type, html) {
  10985. var me = this,
  10986. cont;
  10987. if (html) {
  10988. if (!opt.pasteFilter) {
  10989. return;
  10990. }
  10991. cont = me.document.createElement("div");
  10992. cont.innerHTML = html.html;
  10993. } else {
  10994. cont = me.document.body;
  10995. }
  10996. var nodes = domUtils.getElementsByTagName(cont, "*");
  10997. // 行首缩进,段落方向,段间距,段内间距
  10998. for (var i = 0, ci; (ci = nodes[i++]); ) {
  10999. if (me.fireEvent("excludeNodeinautotype", ci) === true) {
  11000. continue;
  11001. }
  11002. //font-size
  11003. if (opt.clearFontSize && ci.style.fontSize) {
  11004. domUtils.removeStyle(ci, "font-size");
  11005. removeNotAttributeSpan(ci);
  11006. }
  11007. //font-family
  11008. if (opt.clearFontFamily && ci.style.fontFamily) {
  11009. domUtils.removeStyle(ci, "font-family");
  11010. removeNotAttributeSpan(ci);
  11011. }
  11012. if (isLine(ci)) {
  11013. //合并空行
  11014. if (opt.mergeEmptyline) {
  11015. var next = ci.nextSibling,
  11016. tmpNode,
  11017. isBr = domUtils.isBr(ci);
  11018. while (isLine(next)) {
  11019. tmpNode = next;
  11020. next = tmpNode.nextSibling;
  11021. if (isBr && (!next || (next && !domUtils.isBr(next)))) {
  11022. break;
  11023. }
  11024. domUtils.remove(tmpNode);
  11025. }
  11026. }
  11027. //去掉空行,保留占位的空行
  11028. if (
  11029. opt.removeEmptyline &&
  11030. domUtils.inDoc(ci, cont) &&
  11031. !remainTag[ci.parentNode.tagName.toLowerCase()]
  11032. ) {
  11033. if (domUtils.isBr(ci)) {
  11034. next = ci.nextSibling;
  11035. if (next && !domUtils.isBr(next)) {
  11036. continue;
  11037. }
  11038. }
  11039. domUtils.remove(ci);
  11040. continue;
  11041. }
  11042. }
  11043. if (isLine(ci, true) && ci.tagName != "SPAN") {
  11044. if (opt.indent) {
  11045. ci.style.textIndent = opt.indentValue;
  11046. }
  11047. if (opt.textAlign) {
  11048. ci.style.textAlign = opt.textAlign;
  11049. }
  11050. // if(opt.lineHeight)
  11051. // ci.style.lineHeight = opt.lineHeight + 'cm';
  11052. }
  11053. //去掉class,保留的class不去掉
  11054. if (
  11055. opt.removeClass &&
  11056. ci.className &&
  11057. !remainClass[ci.className.toLowerCase()]
  11058. ) {
  11059. if (highlightCont && highlightCont.contains(ci)) {
  11060. continue;
  11061. }
  11062. domUtils.removeAttributes(ci, ["class"]);
  11063. }
  11064. //表情不处理
  11065. if (
  11066. opt.imageBlockLine &&
  11067. ci.tagName.toLowerCase() == "img" &&
  11068. !ci.getAttribute("emotion")
  11069. ) {
  11070. if (html) {
  11071. var img = ci;
  11072. switch (opt.imageBlockLine) {
  11073. case "left":
  11074. case "right":
  11075. case "none":
  11076. var pN = img.parentNode,
  11077. tmpNode,
  11078. pre,
  11079. next;
  11080. while (dtd.$inline[pN.tagName] || pN.tagName == "A") {
  11081. pN = pN.parentNode;
  11082. }
  11083. tmpNode = pN;
  11084. if (
  11085. tmpNode.tagName == "P" &&
  11086. domUtils.getStyle(tmpNode, "text-align") == "center"
  11087. ) {
  11088. if (
  11089. !domUtils.isBody(tmpNode) &&
  11090. domUtils.getChildCount(tmpNode, function(node) {
  11091. return !domUtils.isBr(node) && !domUtils.isWhitespace(node);
  11092. }) == 1
  11093. ) {
  11094. pre = tmpNode.previousSibling;
  11095. next = tmpNode.nextSibling;
  11096. if (
  11097. pre &&
  11098. next &&
  11099. pre.nodeType == 1 &&
  11100. next.nodeType == 1 &&
  11101. pre.tagName == next.tagName &&
  11102. domUtils.isBlockElm(pre)
  11103. ) {
  11104. pre.appendChild(tmpNode.firstChild);
  11105. while (next.firstChild) {
  11106. pre.appendChild(next.firstChild);
  11107. }
  11108. domUtils.remove(tmpNode);
  11109. domUtils.remove(next);
  11110. } else {
  11111. domUtils.setStyle(tmpNode, "text-align", "");
  11112. }
  11113. }
  11114. }
  11115. domUtils.setStyle(img, "float", opt.imageBlockLine);
  11116. break;
  11117. case "center":
  11118. if (me.queryCommandValue("imagefloat") != "center") {
  11119. pN = img.parentNode;
  11120. domUtils.setStyle(img, "float", "none");
  11121. tmpNode = img;
  11122. while (
  11123. pN &&
  11124. domUtils.getChildCount(pN, function(node) {
  11125. return !domUtils.isBr(node) && !domUtils.isWhitespace(node);
  11126. }) == 1 &&
  11127. (dtd.$inline[pN.tagName] || pN.tagName == "A")
  11128. ) {
  11129. tmpNode = pN;
  11130. pN = pN.parentNode;
  11131. }
  11132. var pNode = me.document.createElement("p");
  11133. domUtils.setAttributes(pNode, {
  11134. style: "text-align:center"
  11135. });
  11136. tmpNode.parentNode.insertBefore(pNode, tmpNode);
  11137. pNode.appendChild(tmpNode);
  11138. domUtils.setStyle(tmpNode, "float", "");
  11139. }
  11140. }
  11141. } else {
  11142. var range = me.selection.getRange();
  11143. range.selectNode(ci).select();
  11144. me.execCommand("imagefloat", opt.imageBlockLine);
  11145. }
  11146. }
  11147. //去掉冗余的标签
  11148. if (opt.removeEmptyNode) {
  11149. if (
  11150. opt.removeTagNames[ci.tagName.toLowerCase()] &&
  11151. domUtils.hasNoAttributes(ci) &&
  11152. domUtils.isEmptyBlock(ci)
  11153. ) {
  11154. domUtils.remove(ci);
  11155. }
  11156. }
  11157. }
  11158. if (opt.tobdc) {
  11159. var root = UE.htmlparser(cont.innerHTML);
  11160. root.traversal(function(node) {
  11161. if (node.type == "text") {
  11162. node.data = ToDBC(node.data);
  11163. }
  11164. });
  11165. cont.innerHTML = root.toHtml();
  11166. }
  11167. if (opt.bdc2sb) {
  11168. var root = UE.htmlparser(cont.innerHTML);
  11169. root.traversal(function(node) {
  11170. if (node.type == "text") {
  11171. node.data = DBC2SB(node.data);
  11172. }
  11173. });
  11174. cont.innerHTML = root.toHtml();
  11175. }
  11176. if (html) {
  11177. html.html = cont.innerHTML;
  11178. }
  11179. }
  11180. if (opt.pasteFilter) {
  11181. me.addListener("beforepaste", autotype);
  11182. }
  11183. function DBC2SB(str) {
  11184. var result = "";
  11185. for (var i = 0; i < str.length; i++) {
  11186. var code = str.charCodeAt(i); //获取当前字符的unicode编码
  11187. if (code >= 65281 && code <= 65373) {
  11188. //在这个unicode编码范围中的是所有的英文字母已经各种字符
  11189. result += String.fromCharCode(str.charCodeAt(i) - 65248); //把全角字符的unicode编码转换为对应半角字符的unicode码
  11190. } else if (code == 12288) {
  11191. //空格
  11192. result += String.fromCharCode(str.charCodeAt(i) - 12288 + 32);
  11193. } else {
  11194. result += str.charAt(i);
  11195. }
  11196. }
  11197. return result;
  11198. }
  11199. function ToDBC(txtstring) {
  11200. txtstring = utils.html(txtstring);
  11201. var tmp = "";
  11202. var mark = ""; /*用于判断,如果是html尖括里的标记,则不进行全角的转换*/
  11203. for (var i = 0; i < txtstring.length; i++) {
  11204. if (txtstring.charCodeAt(i) == 32) {
  11205. tmp = tmp + String.fromCharCode(12288);
  11206. } else if (txtstring.charCodeAt(i) < 127) {
  11207. tmp = tmp + String.fromCharCode(txtstring.charCodeAt(i) + 65248);
  11208. } else {
  11209. tmp += txtstring.charAt(i);
  11210. }
  11211. }
  11212. return tmp;
  11213. }
  11214. function readLocalOpts() {
  11215. var cookieOpt = me.getPreferences("autotypeset");
  11216. utils.extend(me.options.autotypeset, cookieOpt);
  11217. }
  11218. me.commands["autotypeset"] = {
  11219. execCommand: function() {
  11220. me.removeListener("beforepaste", autotype);
  11221. if (opt.pasteFilter) {
  11222. me.addListener("beforepaste", autotype);
  11223. }
  11224. autotype.call(me);
  11225. }
  11226. };
  11227. };
  11228. // plugins/autosubmit.js
  11229. /**
  11230. * 快捷键提交
  11231. * @file
  11232. * @since 1.2.6.1
  11233. */
  11234. /**
  11235. * 提交表单
  11236. * @command autosubmit
  11237. * @method execCommand
  11238. * @param { String } cmd 命令字符串
  11239. * @example
  11240. * ```javascript
  11241. * editor.execCommand( 'autosubmit' );
  11242. * ```
  11243. */
  11244. UE.plugin.register("autosubmit", function() {
  11245. return {
  11246. shortcutkey: {
  11247. autosubmit: "ctrl+13" //手动提交
  11248. },
  11249. commands: {
  11250. autosubmit: {
  11251. execCommand: function() {
  11252. var me = this,
  11253. form = domUtils.findParentByTagName(me.iframe, "form", false);
  11254. if (form) {
  11255. if (me.fireEvent("beforesubmit") === false) {
  11256. return;
  11257. }
  11258. me.sync();
  11259. form.submit();
  11260. }
  11261. }
  11262. }
  11263. }
  11264. };
  11265. });
  11266. // plugins/background.js
  11267. /**
  11268. * 背景插件,为UEditor提供设置背景功能
  11269. * @file
  11270. * @since 1.2.6.1
  11271. */
  11272. UE.plugin.register("background", function() {
  11273. var me = this,
  11274. cssRuleId = "editor_background",
  11275. isSetColored,
  11276. reg = new RegExp("body[\\s]*\\{(.+)\\}", "i");
  11277. function stringToObj(str) {
  11278. var obj = {},
  11279. styles = str.split(";");
  11280. utils.each(styles, function(v) {
  11281. var index = v.indexOf(":"),
  11282. key = utils.trim(v.substr(0, index)).toLowerCase();
  11283. key && (obj[key] = utils.trim(v.substr(index + 1) || ""));
  11284. });
  11285. return obj;
  11286. }
  11287. function setBackground(obj) {
  11288. if (obj) {
  11289. var styles = [];
  11290. for (var name in obj) {
  11291. if (obj.hasOwnProperty(name)) {
  11292. styles.push(name + ":" + obj[name] + "; ");
  11293. }
  11294. }
  11295. utils.cssRule(
  11296. cssRuleId,
  11297. styles.length ? "body{" + styles.join("") + "}" : "",
  11298. me.document
  11299. );
  11300. } else {
  11301. utils.cssRule(cssRuleId, "", me.document);
  11302. }
  11303. }
  11304. //重写editor.hasContent方法
  11305. var orgFn = me.hasContents;
  11306. me.hasContents = function() {
  11307. if (me.queryCommandValue("background")) {
  11308. return true;
  11309. }
  11310. return orgFn.apply(me, arguments);
  11311. };
  11312. return {
  11313. bindEvents: {
  11314. getAllHtml: function(type, headHtml) {
  11315. var body = this.body,
  11316. su = domUtils.getComputedStyle(body, "background-image"),
  11317. url = "";
  11318. if (su.indexOf(me.options.imagePath) > 0) {
  11319. url = su
  11320. .substring(su.indexOf(me.options.imagePath), su.length - 1)
  11321. .replace(/"|\(|\)/gi, "");
  11322. } else {
  11323. url = su != "none" ? su.replace(/url\("?|"?\)/gi, "") : "";
  11324. }
  11325. var html = '<style type="text/css">body{';
  11326. var bgObj = {
  11327. "background-color":
  11328. domUtils.getComputedStyle(body, "background-color") || "#ffffff",
  11329. "background-image": url ? "url(" + url + ")" : "",
  11330. "background-repeat":
  11331. domUtils.getComputedStyle(body, "background-repeat") || "",
  11332. "background-position": browser.ie
  11333. ? domUtils.getComputedStyle(body, "background-position-x") +
  11334. " " +
  11335. domUtils.getComputedStyle(body, "background-position-y")
  11336. : domUtils.getComputedStyle(body, "background-position"),
  11337. height: domUtils.getComputedStyle(body, "height")
  11338. };
  11339. for (var name in bgObj) {
  11340. if (bgObj.hasOwnProperty(name)) {
  11341. html += name + ":" + bgObj[name] + "; ";
  11342. }
  11343. }
  11344. html += "}</style> ";
  11345. headHtml.push(html);
  11346. },
  11347. aftersetcontent: function() {
  11348. if (isSetColored == false) setBackground();
  11349. }
  11350. },
  11351. inputRule: function(root) {
  11352. isSetColored = false;
  11353. utils.each(root.getNodesByTagName("p"), function(p) {
  11354. var styles = p.getAttr("data-background");
  11355. if (styles) {
  11356. isSetColored = true;
  11357. setBackground(stringToObj(styles));
  11358. p.parentNode.removeChild(p);
  11359. }
  11360. });
  11361. },
  11362. outputRule: function(root) {
  11363. var me = this,
  11364. styles = (utils.cssRule(cssRuleId, me.document) || "")
  11365. .replace(/[\n\r]+/g, "")
  11366. .match(reg);
  11367. if (styles) {
  11368. root.appendChild(
  11369. UE.uNode.createElement(
  11370. '<p style="display:none;" data-background="' +
  11371. utils.trim(styles[1].replace(/"/g, "").replace(/[\s]+/g, " ")) +
  11372. '"><br/></p>'
  11373. )
  11374. );
  11375. }
  11376. },
  11377. commands: {
  11378. background: {
  11379. execCommand: function(cmd, obj) {
  11380. setBackground(obj);
  11381. },
  11382. queryCommandValue: function() {
  11383. var me = this,
  11384. styles = (utils.cssRule(cssRuleId, me.document) || "")
  11385. .replace(/[\n\r]+/g, "")
  11386. .match(reg);
  11387. return styles ? stringToObj(styles[1]) : null;
  11388. },
  11389. notNeedUndo: true
  11390. }
  11391. }
  11392. };
  11393. });
  11394. // plugins/image.js
  11395. /**
  11396. * 图片插入、排版插件
  11397. * @file
  11398. * @since 1.2.6.1
  11399. */
  11400. /**
  11401. * 图片对齐方式
  11402. * @command imagefloat
  11403. * @method execCommand
  11404. * @remind 值center为独占一行居中
  11405. * @param { String } cmd 命令字符串
  11406. * @param { String } align 对齐方式,可传left、right、none、center
  11407. * @remaind center表示图片独占一行
  11408. * @example
  11409. * ```javascript
  11410. * editor.execCommand( 'imagefloat', 'center' );
  11411. * ```
  11412. */
  11413. /**
  11414. * 如果选区所在位置是图片区域
  11415. * @command imagefloat
  11416. * @method queryCommandValue
  11417. * @param { String } cmd 命令字符串
  11418. * @return { String } 返回图片对齐方式
  11419. * @example
  11420. * ```javascript
  11421. * editor.queryCommandValue( 'imagefloat' );
  11422. * ```
  11423. */
  11424. UE.commands["imagefloat"] = {
  11425. execCommand: function(cmd, align) {
  11426. var me = this,
  11427. range = me.selection.getRange();
  11428. if (!range.collapsed) {
  11429. var img = range.getClosedNode();
  11430. if (img && img.tagName == "IMG") {
  11431. switch (align) {
  11432. case "left":
  11433. case "right":
  11434. case "none":
  11435. var pN = img.parentNode,
  11436. tmpNode,
  11437. pre,
  11438. next;
  11439. while (dtd.$inline[pN.tagName] || pN.tagName == "A") {
  11440. pN = pN.parentNode;
  11441. }
  11442. tmpNode = pN;
  11443. if (
  11444. tmpNode.tagName == "P" &&
  11445. domUtils.getStyle(tmpNode, "text-align") == "center"
  11446. ) {
  11447. if (
  11448. !domUtils.isBody(tmpNode) &&
  11449. domUtils.getChildCount(tmpNode, function(node) {
  11450. return !domUtils.isBr(node) && !domUtils.isWhitespace(node);
  11451. }) == 1
  11452. ) {
  11453. pre = tmpNode.previousSibling;
  11454. next = tmpNode.nextSibling;
  11455. if (
  11456. pre &&
  11457. next &&
  11458. pre.nodeType == 1 &&
  11459. next.nodeType == 1 &&
  11460. pre.tagName == next.tagName &&
  11461. domUtils.isBlockElm(pre)
  11462. ) {
  11463. pre.appendChild(tmpNode.firstChild);
  11464. while (next.firstChild) {
  11465. pre.appendChild(next.firstChild);
  11466. }
  11467. domUtils.remove(tmpNode);
  11468. domUtils.remove(next);
  11469. } else {
  11470. domUtils.setStyle(tmpNode, "text-align", "");
  11471. }
  11472. }
  11473. range.selectNode(img).select();
  11474. }
  11475. domUtils.setStyle(img, "float", align == "none" ? "" : align);
  11476. if (align == "none") {
  11477. domUtils.removeAttributes(img, "align");
  11478. }
  11479. break;
  11480. case "center":
  11481. if (me.queryCommandValue("imagefloat") != "center") {
  11482. pN = img.parentNode;
  11483. domUtils.setStyle(img, "float", "");
  11484. domUtils.removeAttributes(img, "align");
  11485. tmpNode = img;
  11486. while (
  11487. pN &&
  11488. domUtils.getChildCount(pN, function(node) {
  11489. return !domUtils.isBr(node) && !domUtils.isWhitespace(node);
  11490. }) == 1 &&
  11491. (dtd.$inline[pN.tagName] || pN.tagName == "A")
  11492. ) {
  11493. tmpNode = pN;
  11494. pN = pN.parentNode;
  11495. }
  11496. range.setStartBefore(tmpNode).setCursor(false);
  11497. pN = me.document.createElement("div");
  11498. pN.appendChild(tmpNode);
  11499. domUtils.setStyle(tmpNode, "float", "");
  11500. me.execCommand(
  11501. "insertHtml",
  11502. '<p id="_img_parent_tmp" style="text-align:center">' +
  11503. pN.innerHTML +
  11504. "</p>"
  11505. );
  11506. tmpNode = me.document.getElementById("_img_parent_tmp");
  11507. tmpNode.removeAttribute("id");
  11508. tmpNode = tmpNode.firstChild;
  11509. range.selectNode(tmpNode).select();
  11510. //去掉后边多余的元素
  11511. next = tmpNode.parentNode.nextSibling;
  11512. if (next && domUtils.isEmptyNode(next)) {
  11513. domUtils.remove(next);
  11514. }
  11515. }
  11516. break;
  11517. }
  11518. }
  11519. }
  11520. },
  11521. queryCommandValue: function() {
  11522. var range = this.selection.getRange(),
  11523. startNode,
  11524. floatStyle;
  11525. if (range.collapsed) {
  11526. return "none";
  11527. }
  11528. startNode = range.getClosedNode();
  11529. if (startNode && startNode.nodeType == 1 && startNode.tagName == "IMG") {
  11530. floatStyle =
  11531. domUtils.getComputedStyle(startNode, "float") ||
  11532. startNode.getAttribute("align");
  11533. if (floatStyle == "none") {
  11534. floatStyle = domUtils.getComputedStyle(
  11535. startNode.parentNode,
  11536. "text-align"
  11537. ) == "center"
  11538. ? "center"
  11539. : floatStyle;
  11540. }
  11541. return {
  11542. left: 1,
  11543. right: 1,
  11544. center: 1
  11545. }[floatStyle]
  11546. ? floatStyle
  11547. : "none";
  11548. }
  11549. return "none";
  11550. },
  11551. queryCommandState: function() {
  11552. var range = this.selection.getRange(),
  11553. startNode;
  11554. if (range.collapsed) return -1;
  11555. startNode = range.getClosedNode();
  11556. if (startNode && startNode.nodeType == 1 && startNode.tagName == "IMG") {
  11557. return 0;
  11558. }
  11559. return -1;
  11560. }
  11561. };
  11562. /**
  11563. * 插入图片
  11564. * @command insertimage
  11565. * @method execCommand
  11566. * @param { String } cmd 命令字符串
  11567. * @param { Object } opt 属性键值对,这些属性都将被复制到当前插入图片
  11568. * @remind 该命令第二个参数可接受一个图片配置项对象的数组,可以插入多张图片,
  11569. * 此时数组的每一个元素都是一个Object类型的图片属性集合。
  11570. * @example
  11571. * ```javascript
  11572. * editor.execCommand( 'insertimage', {
  11573. * src:'a/b/c.jpg',
  11574. * width:'100',
  11575. * height:'100'
  11576. * } );
  11577. * ```
  11578. * @example
  11579. * ```javascript
  11580. * editor.execCommand( 'insertimage', [{
  11581. * src:'a/b/c.jpg',
  11582. * width:'100',
  11583. * height:'100'
  11584. * },{
  11585. * src:'a/b/d.jpg',
  11586. * width:'100',
  11587. * height:'100'
  11588. * }] );
  11589. * ```
  11590. */
  11591. UE.commands["insertimage"] = {
  11592. execCommand: function(cmd, opt) {
  11593. opt = utils.isArray(opt) ? opt : [opt];
  11594. if (!opt.length) {
  11595. return;
  11596. }
  11597. var me = this,
  11598. range = me.selection.getRange(),
  11599. img = range.getClosedNode();
  11600. if (me.fireEvent("beforeinsertimage", opt) === true) {
  11601. return;
  11602. }
  11603. if (
  11604. img &&
  11605. /img/i.test(img.tagName) &&
  11606. (img.className != "edui-faked-video" ||
  11607. img.className.indexOf("edui-upload-video") != -1) &&
  11608. !img.getAttribute("word_img")
  11609. ) {
  11610. var first = opt.shift();
  11611. var floatStyle = first["floatStyle"];
  11612. delete first["floatStyle"];
  11613. //// img.style.border = (first.border||0) +"px solid #000";
  11614. //// img.style.margin = (first.margin||0) +"px";
  11615. // img.style.cssText += ';margin:' + (first.margin||0) +"px;" + 'border:' + (first.border||0) +"px solid #000";
  11616. domUtils.setAttributes(img, first);
  11617. me.execCommand("imagefloat", floatStyle);
  11618. if (opt.length > 0) {
  11619. range.setStartAfter(img).setCursor(false, true);
  11620. me.execCommand("insertimage", opt);
  11621. }
  11622. } else {
  11623. var html = [],
  11624. str = "",
  11625. ci;
  11626. ci = opt[0];
  11627. if (opt.length == 1) {
  11628. str =
  11629. '<img src="' +
  11630. ci.src +
  11631. '" ' +
  11632. (ci._src ? ' _src="' + ci._src + '" ' : "") +
  11633. (ci.width ? 'width="' + ci.width + '" ' : "") +
  11634. (ci.height ? ' height="' + ci.height + '" ' : "") +
  11635. (ci["floatStyle"] == "left" || ci["floatStyle"] == "right"
  11636. ? ' style="float:' + ci["floatStyle"] + ';"'
  11637. : "") +
  11638. (ci.title && ci.title != "" ? ' title="' + ci.title + '"' : "") +
  11639. (ci.border && ci.border != "0" ? ' border="' + ci.border + '"' : "") +
  11640. (ci.alt && ci.alt != "" ? ' alt="' + ci.alt + '"' : "") +
  11641. (ci.hspace && ci.hspace != "0"
  11642. ? ' hspace = "' + ci.hspace + '"'
  11643. : "") +
  11644. (ci.vspace && ci.vspace != "0"
  11645. ? ' vspace = "' + ci.vspace + '"'
  11646. : "") +
  11647. "/>";
  11648. if (ci["floatStyle"] == "center") {
  11649. str = '<p style="text-align: center">' + str + "</p>";
  11650. }
  11651. html.push(str);
  11652. } else {
  11653. for (var i = 0; (ci = opt[i++]); ) {
  11654. str =
  11655. "<p " +
  11656. (ci["floatStyle"] == "center"
  11657. ? 'style="text-align: center" '
  11658. : "") +
  11659. '><img src="' +
  11660. ci.src +
  11661. '" ' +
  11662. (ci.width ? 'width="' + ci.width + '" ' : "") +
  11663. (ci._src ? ' _src="' + ci._src + '" ' : "") +
  11664. (ci.height ? ' height="' + ci.height + '" ' : "") +
  11665. ' style="' +
  11666. (ci["floatStyle"] && ci["floatStyle"] != "center"
  11667. ? "float:" + ci["floatStyle"] + ";"
  11668. : "") +
  11669. (ci.border || "") +
  11670. '" ' +
  11671. (ci.title ? ' title="' + ci.title + '"' : "") +
  11672. " /></p>";
  11673. html.push(str);
  11674. }
  11675. }
  11676. me.execCommand("insertHtml", html.join(""));
  11677. }
  11678. me.fireEvent("afterinsertimage", opt);
  11679. }
  11680. };
  11681. // plugins/justify.js
  11682. /**
  11683. * 段落格式
  11684. * @file
  11685. * @since 1.2.6.1
  11686. */
  11687. /**
  11688. * 段落对齐方式
  11689. * @command justify
  11690. * @method execCommand
  11691. * @param { String } cmd 命令字符串
  11692. * @param { String } align 对齐方式:left => 居左,right => 居右,center => 居中,justify => 两端对齐
  11693. * @example
  11694. * ```javascript
  11695. * editor.execCommand( 'justify', 'center' );
  11696. * ```
  11697. */
  11698. /**
  11699. * 如果选区所在位置是段落区域,返回当前段落对齐方式
  11700. * @command justify
  11701. * @method queryCommandValue
  11702. * @param { String } cmd 命令字符串
  11703. * @return { String } 返回段落对齐方式
  11704. * @example
  11705. * ```javascript
  11706. * editor.queryCommandValue( 'justify' );
  11707. * ```
  11708. */
  11709. UE.plugins["justify"] = function() {
  11710. var me = this,
  11711. block = domUtils.isBlockElm,
  11712. defaultValue = {
  11713. left: 1,
  11714. right: 1,
  11715. center: 1,
  11716. justify: 1
  11717. },
  11718. doJustify = function(range, style) {
  11719. var bookmark = range.createBookmark(),
  11720. filterFn = function(node) {
  11721. return node.nodeType == 1
  11722. ? node.tagName.toLowerCase() != "br" &&
  11723. !domUtils.isBookmarkNode(node)
  11724. : !domUtils.isWhitespace(node);
  11725. };
  11726. range.enlarge(true);
  11727. var bookmark2 = range.createBookmark(),
  11728. current = domUtils.getNextDomNode(bookmark2.start, false, filterFn),
  11729. tmpRange = range.cloneRange(),
  11730. tmpNode;
  11731. while (
  11732. current &&
  11733. !(
  11734. domUtils.getPosition(current, bookmark2.end) &
  11735. domUtils.POSITION_FOLLOWING
  11736. )
  11737. ) {
  11738. if (current.nodeType == 3 || !block(current)) {
  11739. tmpRange.setStartBefore(current);
  11740. while (current && current !== bookmark2.end && !block(current)) {
  11741. tmpNode = current;
  11742. current = domUtils.getNextDomNode(current, false, null, function(
  11743. node
  11744. ) {
  11745. return !block(node);
  11746. });
  11747. }
  11748. tmpRange.setEndAfter(tmpNode);
  11749. var common = tmpRange.getCommonAncestor();
  11750. if (!domUtils.isBody(common) && block(common)) {
  11751. domUtils.setStyles(
  11752. common,
  11753. utils.isString(style) ? { "text-align": style } : style
  11754. );
  11755. current = common;
  11756. } else {
  11757. var p = range.document.createElement("p");
  11758. domUtils.setStyles(
  11759. p,
  11760. utils.isString(style) ? { "text-align": style } : style
  11761. );
  11762. var frag = tmpRange.extractContents();
  11763. p.appendChild(frag);
  11764. tmpRange.insertNode(p);
  11765. current = p;
  11766. }
  11767. current = domUtils.getNextDomNode(current, false, filterFn);
  11768. } else {
  11769. current = domUtils.getNextDomNode(current, true, filterFn);
  11770. }
  11771. }
  11772. return range.moveToBookmark(bookmark2).moveToBookmark(bookmark);
  11773. };
  11774. UE.commands["justify"] = {
  11775. execCommand: function(cmdName, align) {
  11776. var range = this.selection.getRange(),
  11777. txt;
  11778. //闭合时单独处理
  11779. if (range.collapsed) {
  11780. txt = this.document.createTextNode("p");
  11781. range.insertNode(txt);
  11782. }
  11783. doJustify(range, align);
  11784. if (txt) {
  11785. range.setStartBefore(txt).collapse(true);
  11786. domUtils.remove(txt);
  11787. }
  11788. range.select();
  11789. return true;
  11790. },
  11791. queryCommandValue: function() {
  11792. var startNode = this.selection.getStart(),
  11793. value = domUtils.getComputedStyle(startNode, "text-align");
  11794. return defaultValue[value] ? value : "left";
  11795. },
  11796. queryCommandState: function() {
  11797. var start = this.selection.getStart(),
  11798. cell =
  11799. start &&
  11800. domUtils.findParentByTagName(start, ["td", "th", "caption"], true);
  11801. return cell ? -1 : 0;
  11802. }
  11803. };
  11804. };
  11805. // plugins/font.js
  11806. /**
  11807. * 字体颜色,背景色,字号,字体,下划线,删除线
  11808. * @file
  11809. * @since 1.2.6.1
  11810. */
  11811. /**
  11812. * 字体颜色
  11813. * @command forecolor
  11814. * @method execCommand
  11815. * @param { String } cmd 命令字符串
  11816. * @param { String } value 色值(必须十六进制)
  11817. * @example
  11818. * ```javascript
  11819. * editor.execCommand( 'forecolor', '#000' );
  11820. * ```
  11821. */
  11822. /**
  11823. * 返回选区字体颜色
  11824. * @command forecolor
  11825. * @method queryCommandValue
  11826. * @param { String } cmd 命令字符串
  11827. * @return { String } 返回字体颜色
  11828. * @example
  11829. * ```javascript
  11830. * editor.queryCommandValue( 'forecolor' );
  11831. * ```
  11832. */
  11833. /**
  11834. * 字体背景颜色
  11835. * @command backcolor
  11836. * @method execCommand
  11837. * @param { String } cmd 命令字符串
  11838. * @param { String } value 色值(必须十六进制)
  11839. * @example
  11840. * ```javascript
  11841. * editor.execCommand( 'backcolor', '#000' );
  11842. * ```
  11843. */
  11844. /**
  11845. * 返回选区字体颜色
  11846. * @command backcolor
  11847. * @method queryCommandValue
  11848. * @param { String } cmd 命令字符串
  11849. * @return { String } 返回字体背景颜色
  11850. * @example
  11851. * ```javascript
  11852. * editor.queryCommandValue( 'backcolor' );
  11853. * ```
  11854. */
  11855. /**
  11856. * 字体大小
  11857. * @command fontsize
  11858. * @method execCommand
  11859. * @param { String } cmd 命令字符串
  11860. * @param { String } value 字体大小
  11861. * @example
  11862. * ```javascript
  11863. * editor.execCommand( 'fontsize', '14px' );
  11864. * ```
  11865. */
  11866. /**
  11867. * 返回选区字体大小
  11868. * @command fontsize
  11869. * @method queryCommandValue
  11870. * @param { String } cmd 命令字符串
  11871. * @return { String } 返回字体大小
  11872. * @example
  11873. * ```javascript
  11874. * editor.queryCommandValue( 'fontsize' );
  11875. * ```
  11876. */
  11877. /**
  11878. * 字体样式
  11879. * @command fontfamily
  11880. * @method execCommand
  11881. * @param { String } cmd 命令字符串
  11882. * @param { String } value 字体样式
  11883. * @example
  11884. * ```javascript
  11885. * editor.execCommand( 'fontfamily', '微软雅黑' );
  11886. * ```
  11887. */
  11888. /**
  11889. * 返回选区字体样式
  11890. * @command fontfamily
  11891. * @method queryCommandValue
  11892. * @param { String } cmd 命令字符串
  11893. * @return { String } 返回字体样式
  11894. * @example
  11895. * ```javascript
  11896. * editor.queryCommandValue( 'fontfamily' );
  11897. * ```
  11898. */
  11899. /**
  11900. * 字体下划线,与删除线互斥
  11901. * @command underline
  11902. * @method execCommand
  11903. * @param { String } cmd 命令字符串
  11904. * @example
  11905. * ```javascript
  11906. * editor.execCommand( 'underline' );
  11907. * ```
  11908. */
  11909. /**
  11910. * 字体删除线,与下划线互斥
  11911. * @command strikethrough
  11912. * @method execCommand
  11913. * @param { String } cmd 命令字符串
  11914. * @example
  11915. * ```javascript
  11916. * editor.execCommand( 'strikethrough' );
  11917. * ```
  11918. */
  11919. /**
  11920. * 字体边框
  11921. * @command fontborder
  11922. * @method execCommand
  11923. * @param { String } cmd 命令字符串
  11924. * @example
  11925. * ```javascript
  11926. * editor.execCommand( 'fontborder' );
  11927. * ```
  11928. */
  11929. UE.plugins["font"] = function() {
  11930. var me = this,
  11931. fonts = {
  11932. forecolor: "color",
  11933. backcolor: "background-color",
  11934. fontsize: "font-size",
  11935. fontfamily: "font-family",
  11936. underline: "text-decoration",
  11937. strikethrough: "text-decoration",
  11938. fontborder: "border"
  11939. },
  11940. needCmd = { underline: 1, strikethrough: 1, fontborder: 1 },
  11941. needSetChild = {
  11942. forecolor: "color",
  11943. backcolor: "background-color",
  11944. fontsize: "font-size",
  11945. fontfamily: "font-family"
  11946. };
  11947. me.setOpt({
  11948. fontfamily: [
  11949. { name: "songti", val: "宋体,SimSun" },
  11950. { name: "yahei", val: "微软雅黑,Microsoft YaHei" },
  11951. { name: "kaiti", val: "楷体,楷体_GB2312, SimKai" },
  11952. { name: "heiti", val: "黑体, SimHei" },
  11953. { name: "lishu", val: "隶书, SimLi" },
  11954. { name: "andaleMono", val: "andale mono" },
  11955. { name: "arial", val: "arial, helvetica,sans-serif" },
  11956. { name: "arialBlack", val: "arial black,avant garde" },
  11957. { name: "comicSansMs", val: "comic sans ms" },
  11958. { name: "impact", val: "impact,chicago" },
  11959. { name: "timesNewRoman", val: "times new roman" }
  11960. ],
  11961. fontsize: [10, 11, 12, 14, 16, 18, 20, 24, 36]
  11962. });
  11963. function mergeWithParent(node) {
  11964. var parent;
  11965. while ((parent = node.parentNode)) {
  11966. if (
  11967. parent.tagName == "SPAN" &&
  11968. domUtils.getChildCount(parent, function(child) {
  11969. return !domUtils.isBookmarkNode(child) && !domUtils.isBr(child);
  11970. }) == 1
  11971. ) {
  11972. parent.style.cssText += node.style.cssText;
  11973. domUtils.remove(node, true);
  11974. node = parent;
  11975. } else {
  11976. break;
  11977. }
  11978. }
  11979. }
  11980. function mergeChild(rng, cmdName, value) {
  11981. if (needSetChild[cmdName]) {
  11982. rng.adjustmentBoundary();
  11983. if (!rng.collapsed && rng.startContainer.nodeType == 1) {
  11984. var start = rng.startContainer.childNodes[rng.startOffset];
  11985. if (start && domUtils.isTagNode(start, "span")) {
  11986. var bk = rng.createBookmark();
  11987. utils.each(domUtils.getElementsByTagName(start, "span"), function(
  11988. span
  11989. ) {
  11990. if (!span.parentNode || domUtils.isBookmarkNode(span)) return;
  11991. if (
  11992. cmdName == "backcolor" &&
  11993. domUtils
  11994. .getComputedStyle(span, "background-color")
  11995. .toLowerCase() === value
  11996. ) {
  11997. return;
  11998. }
  11999. domUtils.removeStyle(span, needSetChild[cmdName]);
  12000. if (span.style.cssText.replace(/^\s+$/, "").length == 0) {
  12001. domUtils.remove(span, true);
  12002. }
  12003. });
  12004. rng.moveToBookmark(bk);
  12005. }
  12006. }
  12007. }
  12008. }
  12009. function mergesibling(rng, cmdName, value) {
  12010. var collapsed = rng.collapsed,
  12011. bk = rng.createBookmark(),
  12012. common;
  12013. if (collapsed) {
  12014. common = bk.start.parentNode;
  12015. while (dtd.$inline[common.tagName]) {
  12016. common = common.parentNode;
  12017. }
  12018. } else {
  12019. common = domUtils.getCommonAncestor(bk.start, bk.end);
  12020. }
  12021. utils.each(domUtils.getElementsByTagName(common, "span"), function(span) {
  12022. if (!span.parentNode || domUtils.isBookmarkNode(span)) return;
  12023. if (/\s*border\s*:\s*none;?\s*/i.test(span.style.cssText)) {
  12024. if (/^\s*border\s*:\s*none;?\s*$/.test(span.style.cssText)) {
  12025. domUtils.remove(span, true);
  12026. } else {
  12027. domUtils.removeStyle(span, "border");
  12028. }
  12029. return;
  12030. }
  12031. if (
  12032. /border/i.test(span.style.cssText) &&
  12033. span.parentNode.tagName == "SPAN" &&
  12034. /border/i.test(span.parentNode.style.cssText)
  12035. ) {
  12036. span.style.cssText = span.style.cssText.replace(
  12037. /border[^:]*:[^;]+;?/gi,
  12038. ""
  12039. );
  12040. }
  12041. if (!(cmdName == "fontborder" && value == "none")) {
  12042. var next = span.nextSibling;
  12043. while (next && next.nodeType == 1 && next.tagName == "SPAN") {
  12044. if (domUtils.isBookmarkNode(next) && cmdName == "fontborder") {
  12045. span.appendChild(next);
  12046. next = span.nextSibling;
  12047. continue;
  12048. }
  12049. if (next.style.cssText == span.style.cssText) {
  12050. domUtils.moveChild(next, span);
  12051. domUtils.remove(next);
  12052. }
  12053. if (span.nextSibling === next) break;
  12054. next = span.nextSibling;
  12055. }
  12056. }
  12057. mergeWithParent(span);
  12058. if (browser.ie && browser.version > 8) {
  12059. //拷贝父亲们的特别的属性,这里只做背景颜色的处理
  12060. var parent = domUtils.findParent(span, function(n) {
  12061. return (
  12062. n.tagName == "SPAN" && /background-color/.test(n.style.cssText)
  12063. );
  12064. });
  12065. if (parent && !/background-color/.test(span.style.cssText)) {
  12066. span.style.backgroundColor = parent.style.backgroundColor;
  12067. }
  12068. }
  12069. });
  12070. rng.moveToBookmark(bk);
  12071. mergeChild(rng, cmdName, value);
  12072. }
  12073. me.addInputRule(function(root) {
  12074. utils.each(root.getNodesByTagName("u s del font strike"), function(node) {
  12075. if (node.tagName == "font") {
  12076. var cssStyle = [];
  12077. for (var p in node.attrs) {
  12078. switch (p) {
  12079. case "size":
  12080. cssStyle.push(
  12081. "font-size:" +
  12082. ({
  12083. "1": "10",
  12084. "2": "12",
  12085. "3": "16",
  12086. "4": "18",
  12087. "5": "24",
  12088. "6": "32",
  12089. "7": "48"
  12090. }[node.attrs[p]] || node.attrs[p]) +
  12091. "px"
  12092. );
  12093. break;
  12094. case "color":
  12095. cssStyle.push("color:" + node.attrs[p]);
  12096. break;
  12097. case "face":
  12098. cssStyle.push("font-family:" + node.attrs[p]);
  12099. break;
  12100. case "style":
  12101. cssStyle.push(node.attrs[p]);
  12102. }
  12103. }
  12104. node.attrs = {
  12105. style: cssStyle.join(";")
  12106. };
  12107. } else {
  12108. var val = node.tagName == "u" ? "underline" : "line-through";
  12109. node.attrs = {
  12110. style: (node.getAttr("style") || "") + "text-decoration:" + val + ";"
  12111. };
  12112. }
  12113. node.tagName = "span";
  12114. });
  12115. // utils.each(root.getNodesByTagName('span'), function (node) {
  12116. // var val;
  12117. // if(val = node.getAttr('class')){
  12118. // if(/fontstrikethrough/.test(val)){
  12119. // node.setStyle('text-decoration','line-through');
  12120. // if(node.attrs['class']){
  12121. // node.attrs['class'] = node.attrs['class'].replace(/fontstrikethrough/,'');
  12122. // }else{
  12123. // node.setAttr('class')
  12124. // }
  12125. // }
  12126. // if(/fontborder/.test(val)){
  12127. // node.setStyle('border','1px solid #000');
  12128. // if(node.attrs['class']){
  12129. // node.attrs['class'] = node.attrs['class'].replace(/fontborder/,'');
  12130. // }else{
  12131. // node.setAttr('class')
  12132. // }
  12133. // }
  12134. // }
  12135. // });
  12136. });
  12137. // me.addOutputRule(function(root){
  12138. // utils.each(root.getNodesByTagName('span'), function (node) {
  12139. // var val;
  12140. // if(val = node.getStyle('text-decoration')){
  12141. // if(/line-through/.test(val)){
  12142. // if(node.attrs['class']){
  12143. // node.attrs['class'] += ' fontstrikethrough';
  12144. // }else{
  12145. // node.setAttr('class','fontstrikethrough')
  12146. // }
  12147. // }
  12148. //
  12149. // node.setStyle('text-decoration')
  12150. // }
  12151. // if(val = node.getStyle('border')){
  12152. // if(/1px/.test(val) && /solid/.test(val)){
  12153. // if(node.attrs['class']){
  12154. // node.attrs['class'] += ' fontborder';
  12155. //
  12156. // }else{
  12157. // node.setAttr('class','fontborder')
  12158. // }
  12159. // }
  12160. // node.setStyle('border')
  12161. //
  12162. // }
  12163. // });
  12164. // });
  12165. for (var p in fonts) {
  12166. (function(cmd, style) {
  12167. UE.commands[cmd] = {
  12168. execCommand: function(cmdName, value) {
  12169. value =
  12170. value ||
  12171. (this.queryCommandState(cmdName)
  12172. ? "none"
  12173. : cmdName == "underline"
  12174. ? "underline"
  12175. : cmdName == "fontborder" ? "1px solid #000" : "line-through");
  12176. var me = this,
  12177. range = this.selection.getRange(),
  12178. text;
  12179. if (value == "default") {
  12180. if (range.collapsed) {
  12181. text = me.document.createTextNode("font");
  12182. range.insertNode(text).select();
  12183. }
  12184. me.execCommand("removeFormat", "span,a", style);
  12185. if (text) {
  12186. range.setStartBefore(text).collapse(true);
  12187. domUtils.remove(text);
  12188. }
  12189. mergesibling(range, cmdName, value);
  12190. range.select();
  12191. } else {
  12192. if (!range.collapsed) {
  12193. if (needCmd[cmd] && me.queryCommandValue(cmd)) {
  12194. me.execCommand("removeFormat", "span,a", style);
  12195. }
  12196. range = me.selection.getRange();
  12197. range.applyInlineStyle("span", { style: style + ":" + value });
  12198. mergesibling(range, cmdName, value);
  12199. range.select();
  12200. } else {
  12201. var span = domUtils.findParentByTagName(
  12202. range.startContainer,
  12203. "span",
  12204. true
  12205. );
  12206. text = me.document.createTextNode("font");
  12207. if (
  12208. span &&
  12209. !span.children.length &&
  12210. !span[browser.ie ? "innerText" : "textContent"].replace(
  12211. fillCharReg,
  12212. ""
  12213. ).length
  12214. ) {
  12215. //for ie hack when enter
  12216. range.insertNode(text);
  12217. if (needCmd[cmd]) {
  12218. range.selectNode(text).select();
  12219. me.execCommand("removeFormat", "span,a", style, null);
  12220. span = domUtils.findParentByTagName(text, "span", true);
  12221. range.setStartBefore(text);
  12222. }
  12223. span && (span.style.cssText += ";" + style + ":" + value);
  12224. range.collapse(true).select();
  12225. } else {
  12226. range.insertNode(text);
  12227. range.selectNode(text).select();
  12228. span = range.document.createElement("span");
  12229. if (needCmd[cmd]) {
  12230. //a标签内的不处理跳过
  12231. if (domUtils.findParentByTagName(text, "a", true)) {
  12232. range.setStartBefore(text).setCursor();
  12233. domUtils.remove(text);
  12234. return;
  12235. }
  12236. me.execCommand("removeFormat", "span,a", style);
  12237. }
  12238. span.style.cssText = style + ":" + value;
  12239. text.parentNode.insertBefore(span, text);
  12240. //修复,span套span 但样式不继承的问题
  12241. if (!browser.ie || (browser.ie && browser.version == 9)) {
  12242. var spanParent = span.parentNode;
  12243. while (!domUtils.isBlockElm(spanParent)) {
  12244. if (spanParent.tagName == "SPAN") {
  12245. //opera合并style不会加入";"
  12246. span.style.cssText =
  12247. spanParent.style.cssText + ";" + span.style.cssText;
  12248. }
  12249. spanParent = spanParent.parentNode;
  12250. }
  12251. }
  12252. if (opera) {
  12253. setTimeout(function() {
  12254. range.setStart(span, 0).collapse(true);
  12255. mergesibling(range, cmdName, value);
  12256. range.select();
  12257. });
  12258. } else {
  12259. range.setStart(span, 0).collapse(true);
  12260. mergesibling(range, cmdName, value);
  12261. range.select();
  12262. }
  12263. //trace:981
  12264. //domUtils.mergeToParent(span)
  12265. }
  12266. domUtils.remove(text);
  12267. }
  12268. }
  12269. return true;
  12270. },
  12271. queryCommandValue: function(cmdName) {
  12272. var startNode = this.selection.getStart();
  12273. //trace:946
  12274. if (cmdName == "underline" || cmdName == "strikethrough") {
  12275. var tmpNode = startNode,
  12276. value;
  12277. while (
  12278. tmpNode &&
  12279. !domUtils.isBlockElm(tmpNode) &&
  12280. !domUtils.isBody(tmpNode)
  12281. ) {
  12282. if (tmpNode.nodeType == 1) {
  12283. value = domUtils.getComputedStyle(tmpNode, style);
  12284. if (value != "none") {
  12285. return value;
  12286. }
  12287. }
  12288. tmpNode = tmpNode.parentNode;
  12289. }
  12290. return "none";
  12291. }
  12292. if (cmdName == "fontborder") {
  12293. var tmp = startNode,
  12294. val;
  12295. while (tmp && dtd.$inline[tmp.tagName]) {
  12296. if ((val = domUtils.getComputedStyle(tmp, "border"))) {
  12297. if (/1px/.test(val) && /solid/.test(val)) {
  12298. return val;
  12299. }
  12300. }
  12301. tmp = tmp.parentNode;
  12302. }
  12303. return "";
  12304. }
  12305. if (cmdName == "FontSize") {
  12306. var styleVal = domUtils.getComputedStyle(startNode, style),
  12307. tmp = /^([\d\.]+)(\w+)$/.exec(styleVal);
  12308. if (tmp) {
  12309. return Math.floor(tmp[1]) + tmp[2];
  12310. }
  12311. return styleVal;
  12312. }
  12313. return domUtils.getComputedStyle(startNode, style);
  12314. },
  12315. queryCommandState: function(cmdName) {
  12316. if (!needCmd[cmdName]) return 0;
  12317. var val = this.queryCommandValue(cmdName);
  12318. if (cmdName == "fontborder") {
  12319. return /1px/.test(val) && /solid/.test(val);
  12320. } else {
  12321. return cmdName == "underline"
  12322. ? /underline/.test(val)
  12323. : /line\-through/.test(val);
  12324. }
  12325. }
  12326. };
  12327. })(p, fonts[p]);
  12328. }
  12329. };
  12330. // plugins/link.js
  12331. /**
  12332. * 超链接
  12333. * @file
  12334. * @since 1.2.6.1
  12335. */
  12336. /**
  12337. * 插入超链接
  12338. * @command link
  12339. * @method execCommand
  12340. * @param { String } cmd 命令字符串
  12341. * @param { Object } options 设置自定义属性,例如:url、title、target
  12342. * @example
  12343. * ```javascript
  12344. * editor.execCommand( 'link', '{
  12345. * url:'ueditor.baidu.com',
  12346. * title:'ueditor',
  12347. * target:'_blank'
  12348. * }' );
  12349. * ```
  12350. */
  12351. /**
  12352. * 返回当前选中的第一个超链接节点
  12353. * @command link
  12354. * @method queryCommandValue
  12355. * @param { String } cmd 命令字符串
  12356. * @return { Element } 超链接节点
  12357. * @example
  12358. * ```javascript
  12359. * editor.queryCommandValue( 'link' );
  12360. * ```
  12361. */
  12362. /**
  12363. * 取消超链接
  12364. * @command unlink
  12365. * @method execCommand
  12366. * @param { String } cmd 命令字符串
  12367. * @example
  12368. * ```javascript
  12369. * editor.execCommand( 'unlink');
  12370. * ```
  12371. */
  12372. UE.plugins["link"] = function() {
  12373. function optimize(range) {
  12374. var start = range.startContainer,
  12375. end = range.endContainer;
  12376. if ((start = domUtils.findParentByTagName(start, "a", true))) {
  12377. range.setStartBefore(start);
  12378. }
  12379. if ((end = domUtils.findParentByTagName(end, "a", true))) {
  12380. range.setEndAfter(end);
  12381. }
  12382. }
  12383. UE.commands["unlink"] = {
  12384. execCommand: function() {
  12385. var range = this.selection.getRange(),
  12386. bookmark;
  12387. if (
  12388. range.collapsed &&
  12389. !domUtils.findParentByTagName(range.startContainer, "a", true)
  12390. ) {
  12391. return;
  12392. }
  12393. bookmark = range.createBookmark();
  12394. optimize(range);
  12395. range.removeInlineStyle("a").moveToBookmark(bookmark).select();
  12396. },
  12397. queryCommandState: function() {
  12398. return !this.highlight && this.queryCommandValue("link") ? 0 : -1;
  12399. }
  12400. };
  12401. function doLink(range, opt, me) {
  12402. var rngClone = range.cloneRange(),
  12403. link = me.queryCommandValue("link");
  12404. optimize((range = range.adjustmentBoundary()));
  12405. var start = range.startContainer;
  12406. if (start.nodeType == 1 && link) {
  12407. start = start.childNodes[range.startOffset];
  12408. if (
  12409. start &&
  12410. start.nodeType == 1 &&
  12411. start.tagName == "A" &&
  12412. /^(?:https?|ftp|file)\s*:\s*\/\//.test(
  12413. start[browser.ie ? "innerText" : "textContent"]
  12414. )
  12415. ) {
  12416. start[browser.ie ? "innerText" : "textContent"] = utils.html(
  12417. opt.textValue || opt.href
  12418. );
  12419. }
  12420. }
  12421. if (!rngClone.collapsed || link) {
  12422. range.removeInlineStyle("a");
  12423. rngClone = range.cloneRange();
  12424. }
  12425. if (rngClone.collapsed) {
  12426. var a = range.document.createElement("a"),
  12427. text = "";
  12428. if (opt.textValue) {
  12429. text = utils.html(opt.textValue);
  12430. delete opt.textValue;
  12431. } else {
  12432. text = utils.html(opt.href);
  12433. }
  12434. domUtils.setAttributes(a, opt);
  12435. start = domUtils.findParentByTagName(rngClone.startContainer, "a", true);
  12436. if (start && domUtils.isInNodeEndBoundary(rngClone, start)) {
  12437. range.setStartAfter(start).collapse(true);
  12438. }
  12439. a[browser.ie ? "innerText" : "textContent"] = text;
  12440. range.insertNode(a).selectNode(a);
  12441. } else {
  12442. range.applyInlineStyle("a", opt);
  12443. }
  12444. }
  12445. UE.commands["link"] = {
  12446. execCommand: function(cmdName, opt) {
  12447. var range;
  12448. opt._href && (opt._href = utils.unhtml(opt._href, /[<">]/g));
  12449. opt.href && (opt.href = utils.unhtml(opt.href, /[<">]/g));
  12450. opt.textValue && (opt.textValue = utils.unhtml(opt.textValue, /[<">]/g));
  12451. doLink((range = this.selection.getRange()), opt, this);
  12452. //闭合都不加占位符,如果加了会在a后边多个占位符节点,导致a是图片背景组成的列表,出现空白问题
  12453. range.collapse().select(true);
  12454. },
  12455. queryCommandValue: function() {
  12456. var range = this.selection.getRange(),
  12457. node;
  12458. if (range.collapsed) {
  12459. // node = this.selection.getStart();
  12460. //在ie下getstart()取值偏上了
  12461. node = range.startContainer;
  12462. node = node.nodeType == 1 ? node : node.parentNode;
  12463. if (
  12464. node &&
  12465. (node = domUtils.findParentByTagName(node, "a", true)) &&
  12466. !domUtils.isInNodeEndBoundary(range, node)
  12467. ) {
  12468. return node;
  12469. }
  12470. } else {
  12471. //trace:1111 如果是<p><a>xx</a></p> startContainer是p就会找不到a
  12472. range.shrinkBoundary();
  12473. var start = range.startContainer.nodeType == 3 ||
  12474. !range.startContainer.childNodes[range.startOffset]
  12475. ? range.startContainer
  12476. : range.startContainer.childNodes[range.startOffset],
  12477. end = range.endContainer.nodeType == 3 || range.endOffset == 0
  12478. ? range.endContainer
  12479. : range.endContainer.childNodes[range.endOffset - 1],
  12480. common = range.getCommonAncestor();
  12481. node = domUtils.findParentByTagName(common, "a", true);
  12482. if (!node && common.nodeType == 1) {
  12483. var as = common.getElementsByTagName("a"),
  12484. ps,
  12485. pe;
  12486. for (var i = 0, ci; (ci = as[i++]); ) {
  12487. (ps = domUtils.getPosition(ci, start)), (pe = domUtils.getPosition(
  12488. ci,
  12489. end
  12490. ));
  12491. if (
  12492. (ps & domUtils.POSITION_FOLLOWING ||
  12493. ps & domUtils.POSITION_CONTAINS) &&
  12494. (pe & domUtils.POSITION_PRECEDING ||
  12495. pe & domUtils.POSITION_CONTAINS)
  12496. ) {
  12497. node = ci;
  12498. break;
  12499. }
  12500. }
  12501. }
  12502. return node;
  12503. }
  12504. },
  12505. queryCommandState: function() {
  12506. //判断如果是视频的话连接不可用
  12507. //fix 853
  12508. var img = this.selection.getRange().getClosedNode(),
  12509. flag =
  12510. img &&
  12511. (img.className == "edui-faked-video" ||
  12512. img.className.indexOf("edui-upload-video") != -1);
  12513. return flag ? -1 : 0;
  12514. }
  12515. };
  12516. };
  12517. // plugins/iframe.js
  12518. ///import core
  12519. ///import plugins\inserthtml.js
  12520. ///commands 插入框架
  12521. ///commandsName InsertFrame
  12522. ///commandsTitle 插入Iframe
  12523. ///commandsDialog dialogs\insertframe
  12524. UE.plugins["insertframe"] = function() {
  12525. var me = this;
  12526. function deleteIframe() {
  12527. me._iframe && delete me._iframe;
  12528. }
  12529. me.addListener("selectionchange", function() {
  12530. deleteIframe();
  12531. });
  12532. };
  12533. // plugins/scrawl.js
  12534. ///import core
  12535. ///commands 涂鸦
  12536. ///commandsName Scrawl
  12537. ///commandsTitle 涂鸦
  12538. ///commandsDialog dialogs\scrawl
  12539. UE.commands["scrawl"] = {
  12540. queryCommandState: function() {
  12541. return browser.ie && browser.version <= 8 ? -1 : 0;
  12542. }
  12543. };
  12544. // plugins/removeformat.js
  12545. /**
  12546. * 清除格式
  12547. * @file
  12548. * @since 1.2.6.1
  12549. */
  12550. /**
  12551. * 清除文字样式
  12552. * @command removeformat
  12553. * @method execCommand
  12554. * @param { String } cmd 命令字符串
  12555. * @param {String} tags 以逗号隔开的标签。如:strong
  12556. * @param {String} style 样式如:color
  12557. * @param {String} attrs 属性如:width
  12558. * @example
  12559. * ```javascript
  12560. * editor.execCommand( 'removeformat', 'strong','color','width' );
  12561. * ```
  12562. */
  12563. UE.plugins["removeformat"] = function() {
  12564. var me = this;
  12565. me.setOpt({
  12566. removeFormatTags:
  12567. "b,big,code,del,dfn,em,font,i,ins,kbd,q,samp,small,span,strike,strong,sub,sup,tt,u,var",
  12568. removeFormatAttributes: "class,style,lang,width,height,align,hspace,valign"
  12569. });
  12570. me.commands["removeformat"] = {
  12571. execCommand: function(cmdName, tags, style, attrs, notIncludeA) {
  12572. var tagReg = new RegExp(
  12573. "^(?:" +
  12574. (tags || this.options.removeFormatTags).replace(/,/g, "|") +
  12575. ")$",
  12576. "i"
  12577. ),
  12578. removeFormatAttributes = style
  12579. ? []
  12580. : (attrs || this.options.removeFormatAttributes).split(","),
  12581. range = new dom.Range(this.document),
  12582. bookmark,
  12583. node,
  12584. parent,
  12585. filter = function(node) {
  12586. return node.nodeType == 1;
  12587. };
  12588. function isRedundantSpan(node) {
  12589. if (node.nodeType == 3 || node.tagName.toLowerCase() != "span") {
  12590. return 0;
  12591. }
  12592. if (browser.ie) {
  12593. //ie 下判断实效,所以只能简单用style来判断
  12594. //return node.style.cssText == '' ? 1 : 0;
  12595. var attrs = node.attributes;
  12596. if (attrs.length) {
  12597. for (var i = 0, l = attrs.length; i < l; i++) {
  12598. if (attrs[i].specified) {
  12599. return 0;
  12600. }
  12601. }
  12602. return 1;
  12603. }
  12604. }
  12605. return !node.attributes.length;
  12606. }
  12607. function doRemove(range) {
  12608. var bookmark1 = range.createBookmark();
  12609. if (range.collapsed) {
  12610. range.enlarge(true);
  12611. }
  12612. //不能把a标签切了
  12613. if (!notIncludeA) {
  12614. var aNode = domUtils.findParentByTagName(
  12615. range.startContainer,
  12616. "a",
  12617. true
  12618. );
  12619. if (aNode) {
  12620. range.setStartBefore(aNode);
  12621. }
  12622. aNode = domUtils.findParentByTagName(range.endContainer, "a", true);
  12623. if (aNode) {
  12624. range.setEndAfter(aNode);
  12625. }
  12626. }
  12627. bookmark = range.createBookmark();
  12628. node = bookmark.start;
  12629. //切开始
  12630. while ((parent = node.parentNode) && !domUtils.isBlockElm(parent)) {
  12631. domUtils.breakParent(node, parent);
  12632. domUtils.clearEmptySibling(node);
  12633. }
  12634. if (bookmark.end) {
  12635. //切结束
  12636. node = bookmark.end;
  12637. while ((parent = node.parentNode) && !domUtils.isBlockElm(parent)) {
  12638. domUtils.breakParent(node, parent);
  12639. domUtils.clearEmptySibling(node);
  12640. }
  12641. //开始去除样式
  12642. var current = domUtils.getNextDomNode(bookmark.start, false, filter),
  12643. next;
  12644. while (current) {
  12645. if (current == bookmark.end) {
  12646. break;
  12647. }
  12648. next = domUtils.getNextDomNode(current, true, filter);
  12649. if (
  12650. !dtd.$empty[current.tagName.toLowerCase()] &&
  12651. !domUtils.isBookmarkNode(current)
  12652. ) {
  12653. if (tagReg.test(current.tagName)) {
  12654. if (style) {
  12655. domUtils.removeStyle(current, style);
  12656. if (isRedundantSpan(current) && style != "text-decoration") {
  12657. domUtils.remove(current, true);
  12658. }
  12659. } else {
  12660. domUtils.remove(current, true);
  12661. }
  12662. } else {
  12663. //trace:939 不能把list上的样式去掉
  12664. if (
  12665. !dtd.$tableContent[current.tagName] &&
  12666. !dtd.$list[current.tagName]
  12667. ) {
  12668. domUtils.removeAttributes(current, removeFormatAttributes);
  12669. if (isRedundantSpan(current)) {
  12670. domUtils.remove(current, true);
  12671. }
  12672. }
  12673. }
  12674. }
  12675. current = next;
  12676. }
  12677. }
  12678. //trace:1035
  12679. //trace:1096 不能把td上的样式去掉,比如边框
  12680. var pN = bookmark.start.parentNode;
  12681. if (
  12682. domUtils.isBlockElm(pN) &&
  12683. !dtd.$tableContent[pN.tagName] &&
  12684. !dtd.$list[pN.tagName]
  12685. ) {
  12686. domUtils.removeAttributes(pN, removeFormatAttributes);
  12687. }
  12688. pN = bookmark.end.parentNode;
  12689. if (
  12690. bookmark.end &&
  12691. domUtils.isBlockElm(pN) &&
  12692. !dtd.$tableContent[pN.tagName] &&
  12693. !dtd.$list[pN.tagName]
  12694. ) {
  12695. domUtils.removeAttributes(pN, removeFormatAttributes);
  12696. }
  12697. range.moveToBookmark(bookmark).moveToBookmark(bookmark1);
  12698. //清除冗余的代码 <b><bookmark></b>
  12699. var node = range.startContainer,
  12700. tmp,
  12701. collapsed = range.collapsed;
  12702. while (
  12703. node.nodeType == 1 &&
  12704. domUtils.isEmptyNode(node) &&
  12705. dtd.$removeEmpty[node.tagName]
  12706. ) {
  12707. tmp = node.parentNode;
  12708. range.setStartBefore(node);
  12709. //trace:937
  12710. //更新结束边界
  12711. if (range.startContainer === range.endContainer) {
  12712. range.endOffset--;
  12713. }
  12714. domUtils.remove(node);
  12715. node = tmp;
  12716. }
  12717. if (!collapsed) {
  12718. node = range.endContainer;
  12719. while (
  12720. node.nodeType == 1 &&
  12721. domUtils.isEmptyNode(node) &&
  12722. dtd.$removeEmpty[node.tagName]
  12723. ) {
  12724. tmp = node.parentNode;
  12725. range.setEndBefore(node);
  12726. domUtils.remove(node);
  12727. node = tmp;
  12728. }
  12729. }
  12730. }
  12731. range = this.selection.getRange();
  12732. doRemove(range);
  12733. range.select();
  12734. }
  12735. };
  12736. };
  12737. // plugins/blockquote.js
  12738. /**
  12739. * 添加引用
  12740. * @file
  12741. * @since 1.2.6.1
  12742. */
  12743. /**
  12744. * 添加引用
  12745. * @command blockquote
  12746. * @method execCommand
  12747. * @param { String } cmd 命令字符串
  12748. * @example
  12749. * ```javascript
  12750. * editor.execCommand( 'blockquote' );
  12751. * ```
  12752. */
  12753. /**
  12754. * 添加引用
  12755. * @command blockquote
  12756. * @method execCommand
  12757. * @param { String } cmd 命令字符串
  12758. * @param { Object } attrs 节点属性
  12759. * @example
  12760. * ```javascript
  12761. * editor.execCommand( 'blockquote',{
  12762. * style: "color: red;"
  12763. * } );
  12764. * ```
  12765. */
  12766. UE.plugins["blockquote"] = function() {
  12767. var me = this;
  12768. function getObj(editor) {
  12769. return domUtils.filterNodeList(
  12770. editor.selection.getStartElementPath(),
  12771. "blockquote"
  12772. );
  12773. }
  12774. me.commands["blockquote"] = {
  12775. execCommand: function(cmdName, attrs) {
  12776. var range = this.selection.getRange(),
  12777. obj = getObj(this),
  12778. blockquote = dtd.blockquote,
  12779. bookmark = range.createBookmark();
  12780. if (obj) {
  12781. var start = range.startContainer,
  12782. startBlock = domUtils.isBlockElm(start)
  12783. ? start
  12784. : domUtils.findParent(start, function(node) {
  12785. return domUtils.isBlockElm(node);
  12786. }),
  12787. end = range.endContainer,
  12788. endBlock = domUtils.isBlockElm(end)
  12789. ? end
  12790. : domUtils.findParent(end, function(node) {
  12791. return domUtils.isBlockElm(node);
  12792. });
  12793. //处理一下li
  12794. startBlock =
  12795. domUtils.findParentByTagName(startBlock, "li", true) || startBlock;
  12796. endBlock =
  12797. domUtils.findParentByTagName(endBlock, "li", true) || endBlock;
  12798. if (
  12799. startBlock.tagName == "LI" ||
  12800. startBlock.tagName == "TD" ||
  12801. startBlock === obj ||
  12802. domUtils.isBody(startBlock)
  12803. ) {
  12804. domUtils.remove(obj, true);
  12805. } else {
  12806. domUtils.breakParent(startBlock, obj);
  12807. }
  12808. if (startBlock !== endBlock) {
  12809. obj = domUtils.findParentByTagName(endBlock, "blockquote");
  12810. if (obj) {
  12811. if (
  12812. endBlock.tagName == "LI" ||
  12813. endBlock.tagName == "TD" ||
  12814. domUtils.isBody(endBlock)
  12815. ) {
  12816. obj.parentNode && domUtils.remove(obj, true);
  12817. } else {
  12818. domUtils.breakParent(endBlock, obj);
  12819. }
  12820. }
  12821. }
  12822. var blockquotes = domUtils.getElementsByTagName(
  12823. this.document,
  12824. "blockquote"
  12825. );
  12826. for (var i = 0, bi; (bi = blockquotes[i++]); ) {
  12827. if (!bi.childNodes.length) {
  12828. domUtils.remove(bi);
  12829. } else if (
  12830. domUtils.getPosition(bi, startBlock) &
  12831. domUtils.POSITION_FOLLOWING &&
  12832. domUtils.getPosition(bi, endBlock) & domUtils.POSITION_PRECEDING
  12833. ) {
  12834. domUtils.remove(bi, true);
  12835. }
  12836. }
  12837. } else {
  12838. var tmpRange = range.cloneRange(),
  12839. node = tmpRange.startContainer.nodeType == 1
  12840. ? tmpRange.startContainer
  12841. : tmpRange.startContainer.parentNode,
  12842. preNode = node,
  12843. doEnd = 1;
  12844. //调整开始
  12845. while (1) {
  12846. if (domUtils.isBody(node)) {
  12847. if (preNode !== node) {
  12848. if (range.collapsed) {
  12849. tmpRange.selectNode(preNode);
  12850. doEnd = 0;
  12851. } else {
  12852. tmpRange.setStartBefore(preNode);
  12853. }
  12854. } else {
  12855. tmpRange.setStart(node, 0);
  12856. }
  12857. break;
  12858. }
  12859. if (!blockquote[node.tagName]) {
  12860. if (range.collapsed) {
  12861. tmpRange.selectNode(preNode);
  12862. } else {
  12863. tmpRange.setStartBefore(preNode);
  12864. }
  12865. break;
  12866. }
  12867. preNode = node;
  12868. node = node.parentNode;
  12869. }
  12870. //调整结束
  12871. if (doEnd) {
  12872. preNode = node = node = tmpRange.endContainer.nodeType == 1
  12873. ? tmpRange.endContainer
  12874. : tmpRange.endContainer.parentNode;
  12875. while (1) {
  12876. if (domUtils.isBody(node)) {
  12877. if (preNode !== node) {
  12878. tmpRange.setEndAfter(preNode);
  12879. } else {
  12880. tmpRange.setEnd(node, node.childNodes.length);
  12881. }
  12882. break;
  12883. }
  12884. if (!blockquote[node.tagName]) {
  12885. tmpRange.setEndAfter(preNode);
  12886. break;
  12887. }
  12888. preNode = node;
  12889. node = node.parentNode;
  12890. }
  12891. }
  12892. node = range.document.createElement("blockquote");
  12893. domUtils.setAttributes(node, attrs);
  12894. node.appendChild(tmpRange.extractContents());
  12895. tmpRange.insertNode(node);
  12896. //去除重复的
  12897. var childs = domUtils.getElementsByTagName(node, "blockquote");
  12898. for (var i = 0, ci; (ci = childs[i++]); ) {
  12899. if (ci.parentNode) {
  12900. domUtils.remove(ci, true);
  12901. }
  12902. }
  12903. }
  12904. range.moveToBookmark(bookmark).select();
  12905. },
  12906. queryCommandState: function() {
  12907. return getObj(this) ? 1 : 0;
  12908. }
  12909. };
  12910. };
  12911. // plugins/convertcase.js
  12912. /**
  12913. * 大小写转换
  12914. * @file
  12915. * @since 1.2.6.1
  12916. */
  12917. /**
  12918. * 把选区内文本变大写,与“tolowercase”命令互斥
  12919. * @command touppercase
  12920. * @method execCommand
  12921. * @param { String } cmd 命令字符串
  12922. * @example
  12923. * ```javascript
  12924. * editor.execCommand( 'touppercase' );
  12925. * ```
  12926. */
  12927. /**
  12928. * 把选区内文本变小写,与“touppercase”命令互斥
  12929. * @command tolowercase
  12930. * @method execCommand
  12931. * @param { String } cmd 命令字符串
  12932. * @example
  12933. * ```javascript
  12934. * editor.execCommand( 'tolowercase' );
  12935. * ```
  12936. */
  12937. UE.commands["touppercase"] = UE.commands["tolowercase"] = {
  12938. execCommand: function(cmd) {
  12939. var me = this;
  12940. var rng = me.selection.getRange();
  12941. if (rng.collapsed) {
  12942. return rng;
  12943. }
  12944. var bk = rng.createBookmark(),
  12945. bkEnd = bk.end,
  12946. filterFn = function(node) {
  12947. return !domUtils.isBr(node) && !domUtils.isWhitespace(node);
  12948. },
  12949. curNode = domUtils.getNextDomNode(bk.start, false, filterFn);
  12950. while (
  12951. curNode &&
  12952. domUtils.getPosition(curNode, bkEnd) & domUtils.POSITION_PRECEDING
  12953. ) {
  12954. if (curNode.nodeType == 3) {
  12955. curNode.nodeValue = curNode.nodeValue[
  12956. cmd == "touppercase" ? "toUpperCase" : "toLowerCase"
  12957. ]();
  12958. }
  12959. curNode = domUtils.getNextDomNode(curNode, true, filterFn);
  12960. if (curNode === bkEnd) {
  12961. break;
  12962. }
  12963. }
  12964. rng.moveToBookmark(bk).select();
  12965. }
  12966. };
  12967. // plugins/indent.js
  12968. /**
  12969. * 首行缩进
  12970. * @file
  12971. * @since 1.2.6.1
  12972. */
  12973. /**
  12974. * 缩进
  12975. * @command indent
  12976. * @method execCommand
  12977. * @param { String } cmd 命令字符串
  12978. * @example
  12979. * ```javascript
  12980. * editor.execCommand( 'indent' );
  12981. * ```
  12982. */
  12983. UE.commands["indent"] = {
  12984. execCommand: function() {
  12985. var me = this,
  12986. value = me.queryCommandState("indent")
  12987. ? "0em"
  12988. : me.options.indentValue || "2em";
  12989. me.execCommand("Paragraph", "p", { style: "text-indent:" + value });
  12990. },
  12991. queryCommandState: function() {
  12992. var pN = domUtils.filterNodeList(
  12993. this.selection.getStartElementPath(),
  12994. "p h1 h2 h3 h4 h5 h6"
  12995. );
  12996. return pN && pN.style.textIndent && parseInt(pN.style.textIndent) ? 1 : 0;
  12997. }
  12998. };
  12999. // plugins/print.js
  13000. /**
  13001. * 打印
  13002. * @file
  13003. * @since 1.2.6.1
  13004. */
  13005. /**
  13006. * 打印
  13007. * @command print
  13008. * @method execCommand
  13009. * @param { String } cmd 命令字符串
  13010. * @example
  13011. * ```javascript
  13012. * editor.execCommand( 'print' );
  13013. * ```
  13014. */
  13015. UE.commands["print"] = {
  13016. execCommand: function() {
  13017. this.window.print();
  13018. },
  13019. notNeedUndo: 1
  13020. };
  13021. // plugins/preview.js
  13022. /**
  13023. * 预览
  13024. * @file
  13025. * @since 1.2.6.1
  13026. */
  13027. /**
  13028. * 预览
  13029. * @command preview
  13030. * @method execCommand
  13031. * @param { String } cmd 命令字符串
  13032. * @example
  13033. * ```javascript
  13034. * editor.execCommand( 'preview' );
  13035. * ```
  13036. */
  13037. UE.commands["preview"] = {
  13038. execCommand: function() {
  13039. var w = window.open("", "_blank", ""),
  13040. d = w.document;
  13041. d.open();
  13042. d.write(
  13043. '<!DOCTYPE html><html><head><meta charset="utf-8"/><script src="' +
  13044. this.options.UEDITOR_HOME_URL +
  13045. 'ueditor.parse.js"></script><script>' +
  13046. "setTimeout(function(){uParse('div',{rootPath: '" +
  13047. this.options.UEDITOR_HOME_URL +
  13048. "'})},300)" +
  13049. "</script></head><body><div>" +
  13050. this.getContent(null, null, true) +
  13051. "</div></body></html>"
  13052. );
  13053. d.close();
  13054. },
  13055. notNeedUndo: 1
  13056. };
  13057. // plugins/selectall.js
  13058. /**
  13059. * 全选
  13060. * @file
  13061. * @since 1.2.6.1
  13062. */
  13063. /**
  13064. * 选中所有内容
  13065. * @command selectall
  13066. * @method execCommand
  13067. * @param { String } cmd 命令字符串
  13068. * @example
  13069. * ```javascript
  13070. * editor.execCommand( 'selectall' );
  13071. * ```
  13072. */
  13073. UE.plugins["selectall"] = function() {
  13074. var me = this;
  13075. me.commands["selectall"] = {
  13076. execCommand: function() {
  13077. //去掉了原生的selectAll,因为会出现报错和当内容为空时,不能出现闭合状态的光标
  13078. var me = this,
  13079. body = me.body,
  13080. range = me.selection.getRange();
  13081. range.selectNodeContents(body);
  13082. if (domUtils.isEmptyBlock(body)) {
  13083. //opera不能自动合并到元素的里边,要手动处理一下
  13084. if (browser.opera && body.firstChild && body.firstChild.nodeType == 1) {
  13085. range.setStartAtFirst(body.firstChild);
  13086. }
  13087. range.collapse(true);
  13088. }
  13089. range.select(true);
  13090. },
  13091. notNeedUndo: 1
  13092. };
  13093. //快捷键
  13094. me.addshortcutkey({
  13095. selectAll: "ctrl+65"
  13096. });
  13097. };
  13098. // plugins/paragraph.js
  13099. /**
  13100. * 段落样式
  13101. * @file
  13102. * @since 1.2.6.1
  13103. */
  13104. /**
  13105. * 段落格式
  13106. * @command paragraph
  13107. * @method execCommand
  13108. * @param { String } cmd 命令字符串
  13109. * @param {String} style 标签值为:'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'
  13110. * @param {Object} attrs 标签的属性
  13111. * @example
  13112. * ```javascript
  13113. * editor.execCommand( 'Paragraph','h1','{
  13114. * class:'test'
  13115. * }' );
  13116. * ```
  13117. */
  13118. /**
  13119. * 返回选区内节点标签名
  13120. * @command paragraph
  13121. * @method queryCommandValue
  13122. * @param { String } cmd 命令字符串
  13123. * @return { String } 节点标签名
  13124. * @example
  13125. * ```javascript
  13126. * editor.queryCommandValue( 'Paragraph' );
  13127. * ```
  13128. */
  13129. UE.plugins["paragraph"] = function() {
  13130. var me = this,
  13131. block = domUtils.isBlockElm,
  13132. notExchange = ["TD", "LI", "PRE"],
  13133. doParagraph = function(range, style, attrs, sourceCmdName) {
  13134. var bookmark = range.createBookmark(),
  13135. filterFn = function(node) {
  13136. return node.nodeType == 1
  13137. ? node.tagName.toLowerCase() != "br" &&
  13138. !domUtils.isBookmarkNode(node)
  13139. : !domUtils.isWhitespace(node);
  13140. },
  13141. para;
  13142. range.enlarge(true);
  13143. var bookmark2 = range.createBookmark(),
  13144. current = domUtils.getNextDomNode(bookmark2.start, false, filterFn),
  13145. tmpRange = range.cloneRange(),
  13146. tmpNode;
  13147. while (
  13148. current &&
  13149. !(
  13150. domUtils.getPosition(current, bookmark2.end) &
  13151. domUtils.POSITION_FOLLOWING
  13152. )
  13153. ) {
  13154. if (current.nodeType == 3 || !block(current)) {
  13155. tmpRange.setStartBefore(current);
  13156. while (current && current !== bookmark2.end && !block(current)) {
  13157. tmpNode = current;
  13158. current = domUtils.getNextDomNode(current, false, null, function(
  13159. node
  13160. ) {
  13161. return !block(node);
  13162. });
  13163. }
  13164. tmpRange.setEndAfter(tmpNode);
  13165. para = range.document.createElement(style);
  13166. if (attrs) {
  13167. domUtils.setAttributes(para, attrs);
  13168. if (
  13169. sourceCmdName &&
  13170. sourceCmdName == "customstyle" &&
  13171. attrs.style
  13172. ) {
  13173. para.style.cssText = attrs.style;
  13174. }
  13175. }
  13176. para.appendChild(tmpRange.extractContents());
  13177. //需要内容占位
  13178. if (domUtils.isEmptyNode(para)) {
  13179. domUtils.fillChar(range.document, para);
  13180. }
  13181. tmpRange.insertNode(para);
  13182. var parent = para.parentNode;
  13183. //如果para上一级是一个block元素且不是body,td就删除它
  13184. if (
  13185. block(parent) &&
  13186. !domUtils.isBody(para.parentNode) &&
  13187. utils.indexOf(notExchange, parent.tagName) == -1
  13188. ) {
  13189. //存储dir,style
  13190. if (!(sourceCmdName && sourceCmdName == "customstyle")) {
  13191. parent.getAttribute("dir") &&
  13192. para.setAttribute("dir", parent.getAttribute("dir"));
  13193. //trace:1070
  13194. parent.style.cssText &&
  13195. (para.style.cssText =
  13196. parent.style.cssText + ";" + para.style.cssText);
  13197. //trace:1030
  13198. parent.style.textAlign &&
  13199. !para.style.textAlign &&
  13200. (para.style.textAlign = parent.style.textAlign);
  13201. parent.style.textIndent &&
  13202. !para.style.textIndent &&
  13203. (para.style.textIndent = parent.style.textIndent);
  13204. parent.style.padding &&
  13205. !para.style.padding &&
  13206. (para.style.padding = parent.style.padding);
  13207. }
  13208. //trace:1706 选择的就是h1-6要删除
  13209. if (
  13210. attrs &&
  13211. /h\d/i.test(parent.tagName) &&
  13212. !/h\d/i.test(para.tagName)
  13213. ) {
  13214. domUtils.setAttributes(parent, attrs);
  13215. if (
  13216. sourceCmdName &&
  13217. sourceCmdName == "customstyle" &&
  13218. attrs.style
  13219. ) {
  13220. parent.style.cssText = attrs.style;
  13221. }
  13222. domUtils.remove(para.parentNode, true);
  13223. para = parent;
  13224. } else {
  13225. domUtils.remove(para.parentNode, true);
  13226. }
  13227. }
  13228. if (utils.indexOf(notExchange, parent.tagName) != -1) {
  13229. current = parent;
  13230. } else {
  13231. current = para;
  13232. }
  13233. current = domUtils.getNextDomNode(current, false, filterFn);
  13234. } else {
  13235. current = domUtils.getNextDomNode(current, true, filterFn);
  13236. }
  13237. }
  13238. return range.moveToBookmark(bookmark2).moveToBookmark(bookmark);
  13239. };
  13240. me.setOpt("paragraph", {
  13241. p: "",
  13242. h1: "",
  13243. h2: "",
  13244. h3: "",
  13245. h4: "",
  13246. h5: "",
  13247. h6: ""
  13248. });
  13249. me.commands["paragraph"] = {
  13250. execCommand: function(cmdName, style, attrs, sourceCmdName) {
  13251. var range = this.selection.getRange();
  13252. //闭合时单独处理
  13253. if (range.collapsed) {
  13254. var txt = this.document.createTextNode("p");
  13255. range.insertNode(txt);
  13256. //去掉冗余的fillchar
  13257. if (browser.ie) {
  13258. var node = txt.previousSibling;
  13259. if (node && domUtils.isWhitespace(node)) {
  13260. domUtils.remove(node);
  13261. }
  13262. node = txt.nextSibling;
  13263. if (node && domUtils.isWhitespace(node)) {
  13264. domUtils.remove(node);
  13265. }
  13266. }
  13267. }
  13268. range = doParagraph(range, style, attrs, sourceCmdName);
  13269. if (txt) {
  13270. range.setStartBefore(txt).collapse(true);
  13271. pN = txt.parentNode;
  13272. domUtils.remove(txt);
  13273. if (domUtils.isBlockElm(pN) && domUtils.isEmptyNode(pN)) {
  13274. domUtils.fillNode(this.document, pN);
  13275. }
  13276. }
  13277. if (
  13278. browser.gecko &&
  13279. range.collapsed &&
  13280. range.startContainer.nodeType == 1
  13281. ) {
  13282. var child = range.startContainer.childNodes[range.startOffset];
  13283. if (
  13284. child &&
  13285. child.nodeType == 1 &&
  13286. child.tagName.toLowerCase() == style
  13287. ) {
  13288. range.setStart(child, 0).collapse(true);
  13289. }
  13290. }
  13291. //trace:1097 原来有true,原因忘了,但去了就不能清除多余的占位符了
  13292. range.select();
  13293. return true;
  13294. },
  13295. queryCommandValue: function() {
  13296. var node = domUtils.filterNodeList(
  13297. this.selection.getStartElementPath(),
  13298. "p h1 h2 h3 h4 h5 h6"
  13299. );
  13300. return node ? node.tagName.toLowerCase() : "";
  13301. }
  13302. };
  13303. };
  13304. // plugins/directionality.js
  13305. /**
  13306. * 设置文字输入的方向的插件
  13307. * @file
  13308. * @since 1.2.6.1
  13309. */
  13310. (function() {
  13311. var block = domUtils.isBlockElm,
  13312. getObj = function(editor) {
  13313. // var startNode = editor.selection.getStart(),
  13314. // parents;
  13315. // if ( startNode ) {
  13316. // //查找所有的是block的父亲节点
  13317. // parents = domUtils.findParents( startNode, true, block, true );
  13318. // for ( var i = 0,ci; ci = parents[i++]; ) {
  13319. // if ( ci.getAttribute( 'dir' ) ) {
  13320. // return ci;
  13321. // }
  13322. // }
  13323. // }
  13324. return domUtils.filterNodeList(
  13325. editor.selection.getStartElementPath(),
  13326. function(n) {
  13327. return n && n.nodeType == 1 && n.getAttribute("dir");
  13328. }
  13329. );
  13330. },
  13331. doDirectionality = function(range, editor, forward) {
  13332. var bookmark,
  13333. filterFn = function(node) {
  13334. return node.nodeType == 1
  13335. ? !domUtils.isBookmarkNode(node)
  13336. : !domUtils.isWhitespace(node);
  13337. },
  13338. obj = getObj(editor);
  13339. if (obj && range.collapsed) {
  13340. obj.setAttribute("dir", forward);
  13341. return range;
  13342. }
  13343. bookmark = range.createBookmark();
  13344. range.enlarge(true);
  13345. var bookmark2 = range.createBookmark(),
  13346. current = domUtils.getNextDomNode(bookmark2.start, false, filterFn),
  13347. tmpRange = range.cloneRange(),
  13348. tmpNode;
  13349. while (
  13350. current &&
  13351. !(
  13352. domUtils.getPosition(current, bookmark2.end) &
  13353. domUtils.POSITION_FOLLOWING
  13354. )
  13355. ) {
  13356. if (current.nodeType == 3 || !block(current)) {
  13357. tmpRange.setStartBefore(current);
  13358. while (current && current !== bookmark2.end && !block(current)) {
  13359. tmpNode = current;
  13360. current = domUtils.getNextDomNode(current, false, null, function(
  13361. node
  13362. ) {
  13363. return !block(node);
  13364. });
  13365. }
  13366. tmpRange.setEndAfter(tmpNode);
  13367. var common = tmpRange.getCommonAncestor();
  13368. if (!domUtils.isBody(common) && block(common)) {
  13369. //遍历到了block节点
  13370. common.setAttribute("dir", forward);
  13371. current = common;
  13372. } else {
  13373. //没有遍历到,添加一个block节点
  13374. var p = range.document.createElement("p");
  13375. p.setAttribute("dir", forward);
  13376. var frag = tmpRange.extractContents();
  13377. p.appendChild(frag);
  13378. tmpRange.insertNode(p);
  13379. current = p;
  13380. }
  13381. current = domUtils.getNextDomNode(current, false, filterFn);
  13382. } else {
  13383. current = domUtils.getNextDomNode(current, true, filterFn);
  13384. }
  13385. }
  13386. return range.moveToBookmark(bookmark2).moveToBookmark(bookmark);
  13387. };
  13388. /**
  13389. * 文字输入方向
  13390. * @command directionality
  13391. * @method execCommand
  13392. * @param { String } cmdName 命令字符串
  13393. * @param { String } forward 传入'ltr'表示从左向右输入,传入'rtl'表示从右向左输入
  13394. * @example
  13395. * ```javascript
  13396. * editor.execCommand( 'directionality', 'ltr');
  13397. * ```
  13398. */
  13399. /**
  13400. * 查询当前选区的文字输入方向
  13401. * @command directionality
  13402. * @method queryCommandValue
  13403. * @param { String } cmdName 命令字符串
  13404. * @return { String } 返回'ltr'表示从左向右输入,返回'rtl'表示从右向左输入
  13405. * @example
  13406. * ```javascript
  13407. * editor.queryCommandValue( 'directionality');
  13408. * ```
  13409. */
  13410. UE.commands["directionality"] = {
  13411. execCommand: function(cmdName, forward) {
  13412. var range = this.selection.getRange();
  13413. //闭合时单独处理
  13414. if (range.collapsed) {
  13415. var txt = this.document.createTextNode("d");
  13416. range.insertNode(txt);
  13417. }
  13418. doDirectionality(range, this, forward);
  13419. if (txt) {
  13420. range.setStartBefore(txt).collapse(true);
  13421. domUtils.remove(txt);
  13422. }
  13423. range.select();
  13424. return true;
  13425. },
  13426. queryCommandValue: function() {
  13427. var node = getObj(this);
  13428. return node ? node.getAttribute("dir") : "ltr";
  13429. }
  13430. };
  13431. })();
  13432. // plugins/horizontal.js
  13433. /**
  13434. * 插入分割线插件
  13435. * @file
  13436. * @since 1.2.6.1
  13437. */
  13438. /**
  13439. * 插入分割线
  13440. * @command horizontal
  13441. * @method execCommand
  13442. * @param { String } cmdName 命令字符串
  13443. * @example
  13444. * ```javascript
  13445. * editor.execCommand( 'horizontal' );
  13446. * ```
  13447. */
  13448. UE.plugins["horizontal"] = function() {
  13449. var me = this;
  13450. me.commands["horizontal"] = {
  13451. execCommand: function(cmdName) {
  13452. var me = this;
  13453. if (me.queryCommandState(cmdName) !== -1) {
  13454. me.execCommand("insertHtml", "<hr>");
  13455. var range = me.selection.getRange(),
  13456. start = range.startContainer;
  13457. if (start.nodeType == 1 && !start.childNodes[range.startOffset]) {
  13458. var tmp;
  13459. if ((tmp = start.childNodes[range.startOffset - 1])) {
  13460. if (tmp.nodeType == 1 && tmp.tagName == "HR") {
  13461. if (me.options.enterTag == "p") {
  13462. tmp = me.document.createElement("p");
  13463. range.insertNode(tmp);
  13464. range.setStart(tmp, 0).setCursor();
  13465. } else {
  13466. tmp = me.document.createElement("br");
  13467. range.insertNode(tmp);
  13468. range.setStartBefore(tmp).setCursor();
  13469. }
  13470. }
  13471. }
  13472. }
  13473. return true;
  13474. }
  13475. },
  13476. //边界在table里不能加分隔线
  13477. queryCommandState: function() {
  13478. return domUtils.filterNodeList(
  13479. this.selection.getStartElementPath(),
  13480. "table"
  13481. )
  13482. ? -1
  13483. : 0;
  13484. }
  13485. };
  13486. // me.addListener('delkeyup',function(){
  13487. // var rng = this.selection.getRange();
  13488. // if(browser.ie && browser.version > 8){
  13489. // rng.txtToElmBoundary(true);
  13490. // if(domUtils.isStartInblock(rng)){
  13491. // var tmpNode = rng.startContainer;
  13492. // var pre = tmpNode.previousSibling;
  13493. // if(pre && domUtils.isTagNode(pre,'hr')){
  13494. // domUtils.remove(pre);
  13495. // rng.select();
  13496. // return;
  13497. // }
  13498. // }
  13499. // }
  13500. // if(domUtils.isBody(rng.startContainer)){
  13501. // var hr = rng.startContainer.childNodes[rng.startOffset -1];
  13502. // if(hr && hr.nodeName == 'HR'){
  13503. // var next = hr.nextSibling;
  13504. // if(next){
  13505. // rng.setStart(next,0)
  13506. // }else if(hr.previousSibling){
  13507. // rng.setStartAtLast(hr.previousSibling)
  13508. // }else{
  13509. // var p = this.document.createElement('p');
  13510. // hr.parentNode.insertBefore(p,hr);
  13511. // domUtils.fillNode(this.document,p);
  13512. // rng.setStart(p,0);
  13513. // }
  13514. // domUtils.remove(hr);
  13515. // rng.setCursor(false,true);
  13516. // }
  13517. // }
  13518. // })
  13519. me.addListener("delkeydown", function(name, evt) {
  13520. var rng = this.selection.getRange();
  13521. rng.txtToElmBoundary(true);
  13522. if (domUtils.isStartInblock(rng)) {
  13523. var tmpNode = rng.startContainer;
  13524. var pre = tmpNode.previousSibling;
  13525. if (pre && domUtils.isTagNode(pre, "hr")) {
  13526. domUtils.remove(pre);
  13527. rng.select();
  13528. domUtils.preventDefault(evt);
  13529. return true;
  13530. }
  13531. }
  13532. });
  13533. };
  13534. // plugins/time.js
  13535. /**
  13536. * 插入时间和日期
  13537. * @file
  13538. * @since 1.2.6.1
  13539. */
  13540. /**
  13541. * 插入时间,默认格式:12:59:59
  13542. * @command time
  13543. * @method execCommand
  13544. * @param { String } cmd 命令字符串
  13545. * @example
  13546. * ```javascript
  13547. * editor.execCommand( 'time');
  13548. * ```
  13549. */
  13550. /**
  13551. * 插入日期,默认格式:2013-08-30
  13552. * @command date
  13553. * @method execCommand
  13554. * @param { String } cmd 命令字符串
  13555. * @example
  13556. * ```javascript
  13557. * editor.execCommand( 'date');
  13558. * ```
  13559. */
  13560. UE.commands["time"] = UE.commands["date"] = {
  13561. execCommand: function(cmd, format) {
  13562. var date = new Date();
  13563. function formatTime(date, format) {
  13564. var hh = ("0" + date.getHours()).slice(-2),
  13565. ii = ("0" + date.getMinutes()).slice(-2),
  13566. ss = ("0" + date.getSeconds()).slice(-2);
  13567. format = format || "hh:ii:ss";
  13568. return format.replace(/hh/gi, hh).replace(/ii/gi, ii).replace(/ss/gi, ss);
  13569. }
  13570. function formatDate(date, format) {
  13571. var yyyy = ("000" + date.getFullYear()).slice(-4),
  13572. yy = yyyy.slice(-2),
  13573. mm = ("0" + (date.getMonth() + 1)).slice(-2),
  13574. dd = ("0" + date.getDate()).slice(-2);
  13575. format = format || "yyyy-mm-dd";
  13576. return format
  13577. .replace(/yyyy/gi, yyyy)
  13578. .replace(/yy/gi, yy)
  13579. .replace(/mm/gi, mm)
  13580. .replace(/dd/gi, dd);
  13581. }
  13582. this.execCommand(
  13583. "insertHtml",
  13584. cmd == "time" ? formatTime(date, format) : formatDate(date, format)
  13585. );
  13586. }
  13587. };
  13588. // plugins/rowspacing.js
  13589. /**
  13590. * 段前段后间距插件
  13591. * @file
  13592. * @since 1.2.6.1
  13593. */
  13594. /**
  13595. * 设置段间距
  13596. * @command rowspacing
  13597. * @method execCommand
  13598. * @param { String } cmd 命令字符串
  13599. * @param { String } value 段间距的值,以px为单位
  13600. * @param { String } dir 间距位置,top或bottom,分别表示段前和段后
  13601. * @example
  13602. * ```javascript
  13603. * editor.execCommand( 'rowspacing', '10', 'top' );
  13604. * ```
  13605. */
  13606. UE.plugins["rowspacing"] = function() {
  13607. var me = this;
  13608. me.setOpt({
  13609. rowspacingtop: ["5", "10", "15", "20", "25"],
  13610. rowspacingbottom: ["5", "10", "15", "20", "25"]
  13611. });
  13612. me.commands["rowspacing"] = {
  13613. execCommand: function(cmdName, value, dir) {
  13614. this.execCommand("paragraph", "p", {
  13615. style: "margin-" + dir + ":" + value + "px"
  13616. });
  13617. return true;
  13618. },
  13619. queryCommandValue: function(cmdName, dir) {
  13620. var pN = domUtils.filterNodeList(
  13621. this.selection.getStartElementPath(),
  13622. function(node) {
  13623. return domUtils.isBlockElm(node);
  13624. }
  13625. ),
  13626. value;
  13627. //trace:1026
  13628. if (pN) {
  13629. value = domUtils
  13630. .getComputedStyle(pN, "margin-" + dir)
  13631. .replace(/[^\d]/g, "");
  13632. return !value ? 0 : value;
  13633. }
  13634. return 0;
  13635. }
  13636. };
  13637. };
  13638. // plugins/lineheight.js
  13639. /**
  13640. * 设置行内间距
  13641. * @file
  13642. * @since 1.2.6.1
  13643. */
  13644. UE.plugins["lineheight"] = function() {
  13645. var me = this;
  13646. me.setOpt({ lineheight: ["1", "1.5", "1.75", "2", "3", "4", "5"] });
  13647. /**
  13648. * 行距
  13649. * @command lineheight
  13650. * @method execCommand
  13651. * @param { String } cmdName 命令字符串
  13652. * @param { String } value 传入的行高值, 该值是当前字体的倍数, 例如: 1.5, 1.75
  13653. * @example
  13654. * ```javascript
  13655. * editor.execCommand( 'lineheight', 1.5);
  13656. * ```
  13657. */
  13658. /**
  13659. * 查询当前选区内容的行高大小
  13660. * @command lineheight
  13661. * @method queryCommandValue
  13662. * @param { String } cmd 命令字符串
  13663. * @return { String } 返回当前行高大小
  13664. * @example
  13665. * ```javascript
  13666. * editor.queryCommandValue( 'lineheight' );
  13667. * ```
  13668. */
  13669. me.commands["lineheight"] = {
  13670. execCommand: function(cmdName, value) {
  13671. this.execCommand("paragraph", "p", {
  13672. style: "line-height:" + (value == "1" ? "normal" : value + "em")
  13673. });
  13674. return true;
  13675. },
  13676. queryCommandValue: function() {
  13677. var pN = domUtils.filterNodeList(
  13678. this.selection.getStartElementPath(),
  13679. function(node) {
  13680. return domUtils.isBlockElm(node);
  13681. }
  13682. );
  13683. if (pN) {
  13684. var value = domUtils.getComputedStyle(pN, "line-height");
  13685. return value == "normal" ? 1 : value.replace(/[^\d.]*/gi, "");
  13686. }
  13687. }
  13688. };
  13689. };
  13690. // plugins/insertcode.js
  13691. /**
  13692. * 插入代码插件
  13693. * @file
  13694. * @since 1.2.6.1
  13695. */
  13696. UE.plugins["insertcode"] = function() {
  13697. var me = this;
  13698. me.ready(function() {
  13699. utils.cssRule(
  13700. "pre",
  13701. "pre{margin:.5em 0;padding:.4em .6em;border-radius:8px;background:#f8f8f8;}",
  13702. me.document
  13703. );
  13704. });
  13705. me.setOpt("insertcode", {
  13706. as3: "ActionScript3",
  13707. bash: "Bash/Shell",
  13708. cpp: "C/C++",
  13709. css: "Css",
  13710. cf: "CodeFunction",
  13711. "c#": "C#",
  13712. delphi: "Delphi",
  13713. diff: "Diff",
  13714. erlang: "Erlang",
  13715. groovy: "Groovy",
  13716. html: "Html",
  13717. java: "Java",
  13718. jfx: "JavaFx",
  13719. js: "Javascript",
  13720. pl: "Perl",
  13721. php: "Php",
  13722. plain: "Plain Text",
  13723. ps: "PowerShell",
  13724. python: "Python",
  13725. ruby: "Ruby",
  13726. scala: "Scala",
  13727. sql: "Sql",
  13728. vb: "Vb",
  13729. xml: "Xml"
  13730. });
  13731. /**
  13732. * 插入代码
  13733. * @command insertcode
  13734. * @method execCommand
  13735. * @param { String } cmd 命令字符串
  13736. * @param { String } lang 插入代码的语言
  13737. * @example
  13738. * ```javascript
  13739. * editor.execCommand( 'insertcode', 'javascript' );
  13740. * ```
  13741. */
  13742. /**
  13743. * 如果选区所在位置是插入插入代码区域,返回代码的语言
  13744. * @command insertcode
  13745. * @method queryCommandValue
  13746. * @param { String } cmd 命令字符串
  13747. * @return { String } 返回代码的语言
  13748. * @example
  13749. * ```javascript
  13750. * editor.queryCommandValue( 'insertcode' );
  13751. * ```
  13752. */
  13753. me.commands["insertcode"] = {
  13754. execCommand: function(cmd, lang) {
  13755. var me = this,
  13756. rng = me.selection.getRange(),
  13757. pre = domUtils.findParentByTagName(rng.startContainer, "pre", true);
  13758. if (pre) {
  13759. pre.className = "brush:" + lang + ";toolbar:false;";
  13760. } else {
  13761. var code = "";
  13762. if (rng.collapsed) {
  13763. code = browser.ie && browser.ie11below
  13764. ? browser.version <= 8 ? "&nbsp;" : ""
  13765. : "<br/>";
  13766. } else {
  13767. var frag = rng.extractContents();
  13768. var div = me.document.createElement("div");
  13769. div.appendChild(frag);
  13770. utils.each(
  13771. UE.filterNode(
  13772. UE.htmlparser(div.innerHTML.replace(/[\r\t]/g, "")),
  13773. me.options.filterTxtRules
  13774. ).children,
  13775. function(node) {
  13776. if (browser.ie && browser.ie11below && browser.version > 8) {
  13777. if (node.type == "element") {
  13778. if (node.tagName == "br") {
  13779. code += "\n";
  13780. } else if (!dtd.$empty[node.tagName]) {
  13781. utils.each(node.children, function(cn) {
  13782. if (cn.type == "element") {
  13783. if (cn.tagName == "br") {
  13784. code += "\n";
  13785. } else if (!dtd.$empty[node.tagName]) {
  13786. code += cn.innerText();
  13787. }
  13788. } else {
  13789. code += cn.data;
  13790. }
  13791. });
  13792. if (!/\n$/.test(code)) {
  13793. code += "\n";
  13794. }
  13795. }
  13796. } else {
  13797. code += node.data + "\n";
  13798. }
  13799. if (!node.nextSibling() && /\n$/.test(code)) {
  13800. code = code.replace(/\n$/, "");
  13801. }
  13802. } else {
  13803. if (browser.ie && browser.ie11below) {
  13804. if (node.type == "element") {
  13805. if (node.tagName == "br") {
  13806. code += "<br>";
  13807. } else if (!dtd.$empty[node.tagName]) {
  13808. utils.each(node.children, function(cn) {
  13809. if (cn.type == "element") {
  13810. if (cn.tagName == "br") {
  13811. code += "<br>";
  13812. } else if (!dtd.$empty[node.tagName]) {
  13813. code += cn.innerText();
  13814. }
  13815. } else {
  13816. code += cn.data;
  13817. }
  13818. });
  13819. if (!/br>$/.test(code)) {
  13820. code += "<br>";
  13821. }
  13822. }
  13823. } else {
  13824. code += node.data + "<br>";
  13825. }
  13826. if (!node.nextSibling() && /<br>$/.test(code)) {
  13827. code = code.replace(/<br>$/, "");
  13828. }
  13829. } else {
  13830. code += node.type == "element"
  13831. ? dtd.$empty[node.tagName] ? "" : node.innerText()
  13832. : node.data;
  13833. if (!/br\/?\s*>$/.test(code)) {
  13834. if (!node.nextSibling()) return;
  13835. code += "<br>";
  13836. }
  13837. }
  13838. }
  13839. }
  13840. );
  13841. }
  13842. me.execCommand(
  13843. "inserthtml",
  13844. '<pre id="coder"class="brush:' +
  13845. lang +
  13846. ';toolbar:false">' +
  13847. code +
  13848. "</pre>",
  13849. true
  13850. );
  13851. pre = me.document.getElementById("coder");
  13852. domUtils.removeAttributes(pre, "id");
  13853. var tmpNode = pre.previousSibling;
  13854. if (
  13855. tmpNode &&
  13856. ((tmpNode.nodeType == 3 &&
  13857. tmpNode.nodeValue.length == 1 &&
  13858. browser.ie &&
  13859. browser.version == 6) ||
  13860. domUtils.isEmptyBlock(tmpNode))
  13861. ) {
  13862. domUtils.remove(tmpNode);
  13863. }
  13864. var rng = me.selection.getRange();
  13865. if (domUtils.isEmptyBlock(pre)) {
  13866. rng.setStart(pre, 0).setCursor(false, true);
  13867. } else {
  13868. rng.selectNodeContents(pre).select();
  13869. }
  13870. }
  13871. },
  13872. queryCommandValue: function() {
  13873. var path = this.selection.getStartElementPath();
  13874. var lang = "";
  13875. utils.each(path, function(node) {
  13876. if (node.nodeName == "PRE") {
  13877. var match = node.className.match(/brush:([^;]+)/);
  13878. lang = match && match[1] ? match[1] : "";
  13879. return false;
  13880. }
  13881. });
  13882. return lang;
  13883. }
  13884. };
  13885. me.addInputRule(function(root) {
  13886. utils.each(root.getNodesByTagName("pre"), function(pre) {
  13887. var brs = pre.getNodesByTagName("br");
  13888. if (brs.length) {
  13889. browser.ie &&
  13890. browser.ie11below &&
  13891. browser.version > 8 &&
  13892. utils.each(brs, function(br) {
  13893. var txt = UE.uNode.createText("\n");
  13894. br.parentNode.insertBefore(txt, br);
  13895. br.parentNode.removeChild(br);
  13896. });
  13897. return;
  13898. }
  13899. if (browser.ie && browser.ie11below && browser.version > 8) return;
  13900. var code = pre.innerText().split(/\n/);
  13901. pre.innerHTML("");
  13902. utils.each(code, function(c) {
  13903. if (c.length) {
  13904. pre.appendChild(UE.uNode.createText(c));
  13905. }
  13906. pre.appendChild(UE.uNode.createElement("br"));
  13907. });
  13908. });
  13909. });
  13910. me.addOutputRule(function(root) {
  13911. utils.each(root.getNodesByTagName("pre"), function(pre) {
  13912. var code = "";
  13913. utils.each(pre.children, function(n) {
  13914. if (n.type == "text") {
  13915. //在ie下文本内容有可能末尾带有\n要去掉
  13916. //trace:3396
  13917. code += n.data.replace(/[ ]/g, "&nbsp;").replace(/\n$/, "");
  13918. } else {
  13919. if (n.tagName == "br") {
  13920. code += "\n";
  13921. } else {
  13922. code += !dtd.$empty[n.tagName] ? "" : n.innerText();
  13923. }
  13924. }
  13925. });
  13926. pre.innerText(code.replace(/(&nbsp;|\n)+$/, ""));
  13927. });
  13928. });
  13929. //不需要判断highlight的command列表
  13930. me.notNeedCodeQuery = {
  13931. help: 1,
  13932. undo: 1,
  13933. redo: 1,
  13934. source: 1,
  13935. print: 1,
  13936. searchreplace: 1,
  13937. fullscreen: 1,
  13938. preview: 1,
  13939. insertparagraph: 1,
  13940. elementpath: 1,
  13941. insertcode: 1,
  13942. inserthtml: 1,
  13943. selectall: 1
  13944. };
  13945. //将queyCommamndState重置
  13946. var orgQuery = me.queryCommandState;
  13947. me.queryCommandState = function(cmd) {
  13948. var me = this;
  13949. if (
  13950. !me.notNeedCodeQuery[cmd.toLowerCase()] &&
  13951. me.selection &&
  13952. me.queryCommandValue("insertcode")
  13953. ) {
  13954. return -1;
  13955. }
  13956. return UE.Editor.prototype.queryCommandState.apply(this, arguments);
  13957. };
  13958. me.addListener("beforeenterkeydown", function() {
  13959. var rng = me.selection.getRange();
  13960. var pre = domUtils.findParentByTagName(rng.startContainer, "pre", true);
  13961. if (pre) {
  13962. me.fireEvent("saveScene");
  13963. if (!rng.collapsed) {
  13964. rng.deleteContents();
  13965. }
  13966. if (!browser.ie || browser.ie9above) {
  13967. var tmpNode = me.document.createElement("br"),
  13968. pre;
  13969. rng.insertNode(tmpNode).setStartAfter(tmpNode).collapse(true);
  13970. var next = tmpNode.nextSibling;
  13971. if (!next && (!browser.ie || browser.version > 10)) {
  13972. rng.insertNode(tmpNode.cloneNode(false));
  13973. } else {
  13974. rng.setStartAfter(tmpNode);
  13975. }
  13976. pre = tmpNode.previousSibling;
  13977. var tmp;
  13978. while (pre) {
  13979. tmp = pre;
  13980. pre = pre.previousSibling;
  13981. if (!pre || pre.nodeName == "BR") {
  13982. pre = tmp;
  13983. break;
  13984. }
  13985. }
  13986. if (pre) {
  13987. var str = "";
  13988. while (
  13989. pre &&
  13990. pre.nodeName != "BR" &&
  13991. new RegExp("^[\\s" + domUtils.fillChar + "]*$").test(pre.nodeValue)
  13992. ) {
  13993. str += pre.nodeValue;
  13994. pre = pre.nextSibling;
  13995. }
  13996. if (pre.nodeName != "BR") {
  13997. var match = pre.nodeValue.match(
  13998. new RegExp("^([\\s" + domUtils.fillChar + "]+)")
  13999. );
  14000. if (match && match[1]) {
  14001. str += match[1];
  14002. }
  14003. }
  14004. if (str) {
  14005. str = me.document.createTextNode(str);
  14006. rng.insertNode(str).setStartAfter(str);
  14007. }
  14008. }
  14009. rng.collapse(true).select(true);
  14010. } else {
  14011. if (browser.version > 8) {
  14012. var txt = me.document.createTextNode("\n");
  14013. var start = rng.startContainer;
  14014. if (rng.startOffset == 0) {
  14015. var preNode = start.previousSibling;
  14016. if (preNode) {
  14017. rng.insertNode(txt);
  14018. var fillchar = me.document.createTextNode(" ");
  14019. rng
  14020. .setStartAfter(txt)
  14021. .insertNode(fillchar)
  14022. .setStart(fillchar, 0)
  14023. .collapse(true)
  14024. .select(true);
  14025. }
  14026. } else {
  14027. rng.insertNode(txt).setStartAfter(txt);
  14028. var fillchar = me.document.createTextNode(" ");
  14029. start = rng.startContainer.childNodes[rng.startOffset];
  14030. if (start && !/^\n/.test(start.nodeValue)) {
  14031. rng.setStartBefore(txt);
  14032. }
  14033. rng
  14034. .insertNode(fillchar)
  14035. .setStart(fillchar, 0)
  14036. .collapse(true)
  14037. .select(true);
  14038. }
  14039. } else {
  14040. var tmpNode = me.document.createElement("br");
  14041. rng.insertNode(tmpNode);
  14042. rng.insertNode(me.document.createTextNode(domUtils.fillChar));
  14043. rng.setStartAfter(tmpNode);
  14044. pre = tmpNode.previousSibling;
  14045. var tmp;
  14046. while (pre) {
  14047. tmp = pre;
  14048. pre = pre.previousSibling;
  14049. if (!pre || pre.nodeName == "BR") {
  14050. pre = tmp;
  14051. break;
  14052. }
  14053. }
  14054. if (pre) {
  14055. var str = "";
  14056. while (
  14057. pre &&
  14058. pre.nodeName != "BR" &&
  14059. new RegExp("^[ " + domUtils.fillChar + "]*$").test(pre.nodeValue)
  14060. ) {
  14061. str += pre.nodeValue;
  14062. pre = pre.nextSibling;
  14063. }
  14064. if (pre.nodeName != "BR") {
  14065. var match = pre.nodeValue.match(
  14066. new RegExp("^([ " + domUtils.fillChar + "]+)")
  14067. );
  14068. if (match && match[1]) {
  14069. str += match[1];
  14070. }
  14071. }
  14072. str = me.document.createTextNode(str);
  14073. rng.insertNode(str).setStartAfter(str);
  14074. }
  14075. rng.collapse(true).select();
  14076. }
  14077. }
  14078. me.fireEvent("saveScene");
  14079. return true;
  14080. }
  14081. });
  14082. me.addListener("tabkeydown", function(cmd, evt) {
  14083. var rng = me.selection.getRange();
  14084. var pre = domUtils.findParentByTagName(rng.startContainer, "pre", true);
  14085. if (pre) {
  14086. me.fireEvent("saveScene");
  14087. if (evt.shiftKey) {
  14088. } else {
  14089. if (!rng.collapsed) {
  14090. var bk = rng.createBookmark();
  14091. var start = bk.start.previousSibling;
  14092. while (start) {
  14093. if (pre.firstChild === start && !domUtils.isBr(start)) {
  14094. pre.insertBefore(me.document.createTextNode(" "), start);
  14095. break;
  14096. }
  14097. if (domUtils.isBr(start)) {
  14098. pre.insertBefore(
  14099. me.document.createTextNode(" "),
  14100. start.nextSibling
  14101. );
  14102. break;
  14103. }
  14104. start = start.previousSibling;
  14105. }
  14106. var end = bk.end;
  14107. start = bk.start.nextSibling;
  14108. if (pre.firstChild === bk.start) {
  14109. pre.insertBefore(
  14110. me.document.createTextNode(" "),
  14111. start.nextSibling
  14112. );
  14113. }
  14114. while (start && start !== end) {
  14115. if (domUtils.isBr(start) && start.nextSibling) {
  14116. if (start.nextSibling === end) {
  14117. break;
  14118. }
  14119. pre.insertBefore(
  14120. me.document.createTextNode(" "),
  14121. start.nextSibling
  14122. );
  14123. }
  14124. start = start.nextSibling;
  14125. }
  14126. rng.moveToBookmark(bk).select();
  14127. } else {
  14128. var tmpNode = me.document.createTextNode(" ");
  14129. rng
  14130. .insertNode(tmpNode)
  14131. .setStartAfter(tmpNode)
  14132. .collapse(true)
  14133. .select(true);
  14134. }
  14135. }
  14136. me.fireEvent("saveScene");
  14137. return true;
  14138. }
  14139. });
  14140. me.addListener("beforeinserthtml", function(evtName, html) {
  14141. var me = this,
  14142. rng = me.selection.getRange(),
  14143. pre = domUtils.findParentByTagName(rng.startContainer, "pre", true);
  14144. if (pre) {
  14145. if (!rng.collapsed) {
  14146. rng.deleteContents();
  14147. }
  14148. var htmlstr = "";
  14149. if (browser.ie && browser.version > 8) {
  14150. utils.each(
  14151. UE.filterNode(UE.htmlparser(html), me.options.filterTxtRules)
  14152. .children,
  14153. function(node) {
  14154. if (node.type == "element") {
  14155. if (node.tagName == "br") {
  14156. htmlstr += "\n";
  14157. } else if (!dtd.$empty[node.tagName]) {
  14158. utils.each(node.children, function(cn) {
  14159. if (cn.type == "element") {
  14160. if (cn.tagName == "br") {
  14161. htmlstr += "\n";
  14162. } else if (!dtd.$empty[node.tagName]) {
  14163. htmlstr += cn.innerText();
  14164. }
  14165. } else {
  14166. htmlstr += cn.data;
  14167. }
  14168. });
  14169. if (!/\n$/.test(htmlstr)) {
  14170. htmlstr += "\n";
  14171. }
  14172. }
  14173. } else {
  14174. htmlstr += node.data + "\n";
  14175. }
  14176. if (!node.nextSibling() && /\n$/.test(htmlstr)) {
  14177. htmlstr = htmlstr.replace(/\n$/, "");
  14178. }
  14179. }
  14180. );
  14181. var tmpNode = me.document.createTextNode(
  14182. utils.html(htmlstr.replace(/&nbsp;/g, " "))
  14183. );
  14184. rng.insertNode(tmpNode).selectNode(tmpNode).select();
  14185. } else {
  14186. var frag = me.document.createDocumentFragment();
  14187. utils.each(
  14188. UE.filterNode(UE.htmlparser(html), me.options.filterTxtRules)
  14189. .children,
  14190. function(node) {
  14191. if (node.type == "element") {
  14192. if (node.tagName == "br") {
  14193. frag.appendChild(me.document.createElement("br"));
  14194. } else if (!dtd.$empty[node.tagName]) {
  14195. utils.each(node.children, function(cn) {
  14196. if (cn.type == "element") {
  14197. if (cn.tagName == "br") {
  14198. frag.appendChild(me.document.createElement("br"));
  14199. } else if (!dtd.$empty[node.tagName]) {
  14200. frag.appendChild(
  14201. me.document.createTextNode(
  14202. utils.html(cn.innerText().replace(/&nbsp;/g, " "))
  14203. )
  14204. );
  14205. }
  14206. } else {
  14207. frag.appendChild(
  14208. me.document.createTextNode(
  14209. utils.html(cn.data.replace(/&nbsp;/g, " "))
  14210. )
  14211. );
  14212. }
  14213. });
  14214. if (frag.lastChild.nodeName != "BR") {
  14215. frag.appendChild(me.document.createElement("br"));
  14216. }
  14217. }
  14218. } else {
  14219. frag.appendChild(
  14220. me.document.createTextNode(
  14221. utils.html(node.data.replace(/&nbsp;/g, " "))
  14222. )
  14223. );
  14224. }
  14225. if (!node.nextSibling() && frag.lastChild.nodeName == "BR") {
  14226. frag.removeChild(frag.lastChild);
  14227. }
  14228. }
  14229. );
  14230. rng.insertNode(frag).select();
  14231. }
  14232. return true;
  14233. }
  14234. });
  14235. //方向键的处理
  14236. me.addListener("keydown", function(cmd, evt) {
  14237. var me = this,
  14238. keyCode = evt.keyCode || evt.which;
  14239. if (keyCode == 40) {
  14240. var rng = me.selection.getRange(),
  14241. pre,
  14242. start = rng.startContainer;
  14243. if (
  14244. rng.collapsed &&
  14245. (pre = domUtils.findParentByTagName(rng.startContainer, "pre", true)) &&
  14246. !pre.nextSibling
  14247. ) {
  14248. var last = pre.lastChild;
  14249. while (last && last.nodeName == "BR") {
  14250. last = last.previousSibling;
  14251. }
  14252. if (
  14253. last === start ||
  14254. (rng.startContainer === pre &&
  14255. rng.startOffset == pre.childNodes.length)
  14256. ) {
  14257. me.execCommand("insertparagraph");
  14258. domUtils.preventDefault(evt);
  14259. }
  14260. }
  14261. }
  14262. });
  14263. //trace:3395
  14264. me.addListener("delkeydown", function(type, evt) {
  14265. var rng = this.selection.getRange();
  14266. rng.txtToElmBoundary(true);
  14267. var start = rng.startContainer;
  14268. if (
  14269. domUtils.isTagNode(start, "pre") &&
  14270. rng.collapsed &&
  14271. domUtils.isStartInblock(rng)
  14272. ) {
  14273. var p = me.document.createElement("p");
  14274. domUtils.fillNode(me.document, p);
  14275. start.parentNode.insertBefore(p, start);
  14276. domUtils.remove(start);
  14277. rng.setStart(p, 0).setCursor(false, true);
  14278. domUtils.preventDefault(evt);
  14279. return true;
  14280. }
  14281. });
  14282. };
  14283. // plugins/cleardoc.js
  14284. /**
  14285. * 清空文档插件
  14286. * @file
  14287. * @since 1.2.6.1
  14288. */
  14289. /**
  14290. * 清空文档
  14291. * @command cleardoc
  14292. * @method execCommand
  14293. * @param { String } cmd 命令字符串
  14294. * @example
  14295. * ```javascript
  14296. * //editor 是编辑器实例
  14297. * editor.execCommand('cleardoc');
  14298. * ```
  14299. */
  14300. UE.commands["cleardoc"] = {
  14301. execCommand: function(cmdName) {
  14302. var me = this,
  14303. enterTag = me.options.enterTag,
  14304. range = me.selection.getRange();
  14305. if (enterTag == "br") {
  14306. me.body.innerHTML = "<br/>";
  14307. range.setStart(me.body, 0).setCursor();
  14308. } else {
  14309. me.body.innerHTML = "<p>" + (ie ? "" : "<br/>") + "</p>";
  14310. range.setStart(me.body.firstChild, 0).setCursor(false, true);
  14311. }
  14312. setTimeout(function() {
  14313. me.fireEvent("clearDoc");
  14314. }, 0);
  14315. }
  14316. };
  14317. // plugins/anchor.js
  14318. /**
  14319. * 锚点插件,为UEditor提供插入锚点支持
  14320. * @file
  14321. * @since 1.2.6.1
  14322. */
  14323. UE.plugin.register("anchor", function() {
  14324. return {
  14325. bindEvents: {
  14326. ready: function() {
  14327. utils.cssRule(
  14328. "anchor",
  14329. ".anchorclass{background: url('" +
  14330. this.options.themePath +
  14331. this.options.theme +
  14332. "/images/anchor.gif') no-repeat scroll left center transparent;cursor: auto;display: inline-block;height: 16px;width: 15px;}",
  14333. this.document
  14334. );
  14335. }
  14336. },
  14337. outputRule: function(root) {
  14338. utils.each(root.getNodesByTagName("img"), function(a) {
  14339. var val;
  14340. if ((val = a.getAttr("anchorname"))) {
  14341. a.tagName = "a";
  14342. a.setAttr({
  14343. anchorname: "",
  14344. name: val,
  14345. class: ""
  14346. });
  14347. }
  14348. });
  14349. },
  14350. inputRule: function(root) {
  14351. utils.each(root.getNodesByTagName("a"), function(a) {
  14352. var val;
  14353. if ((val = a.getAttr("name")) && !a.getAttr("href")) {
  14354. //过滤掉word冗余标签
  14355. //_Toc\d+有可能勿命中
  14356. if (/^\_Toc\d+$/.test(val)) {
  14357. a.parentNode.removeChild(a);
  14358. return;
  14359. }
  14360. a.tagName = "img";
  14361. a.setAttr({
  14362. anchorname: a.getAttr("name"),
  14363. class: "anchorclass"
  14364. });
  14365. a.setAttr("name");
  14366. }
  14367. });
  14368. },
  14369. commands: {
  14370. /**
  14371. * 插入锚点
  14372. * @command anchor
  14373. * @method execCommand
  14374. * @param { String } cmd 命令字符串
  14375. * @param { String } name 锚点名称字符串
  14376. * @example
  14377. * ```javascript
  14378. * //editor 是编辑器实例
  14379. * editor.execCommand('anchor', 'anchor1');
  14380. * ```
  14381. */
  14382. anchor: {
  14383. execCommand: function(cmd, name) {
  14384. var range = this.selection.getRange(),
  14385. img = range.getClosedNode();
  14386. if (img && img.getAttribute("anchorname")) {
  14387. if (name) {
  14388. img.setAttribute("anchorname", name);
  14389. } else {
  14390. range.setStartBefore(img).setCursor();
  14391. domUtils.remove(img);
  14392. }
  14393. } else {
  14394. if (name) {
  14395. //只在选区的开始插入
  14396. var anchor = this.document.createElement("img");
  14397. range.collapse(true);
  14398. domUtils.setAttributes(anchor, {
  14399. anchorname: name,
  14400. class: "anchorclass"
  14401. });
  14402. range
  14403. .insertNode(anchor)
  14404. .setStartAfter(anchor)
  14405. .setCursor(false, true);
  14406. }
  14407. }
  14408. }
  14409. }
  14410. }
  14411. };
  14412. });
  14413. // plugins/wordcount.js
  14414. ///import core
  14415. ///commands 字数统计
  14416. ///commandsName WordCount,wordCount
  14417. ///commandsTitle 字数统计
  14418. /*
  14419. * Created by JetBrains WebStorm.
  14420. * User: taoqili
  14421. * Date: 11-9-7
  14422. * Time: 下午8:18
  14423. * To change this template use File | Settings | File Templates.
  14424. */
  14425. UE.plugins["wordcount"] = function() {
  14426. var me = this;
  14427. me.setOpt("wordCount", true);
  14428. me.addListener("contentchange", function() {
  14429. me.fireEvent("wordcount");
  14430. });
  14431. var timer;
  14432. me.addListener("ready", function() {
  14433. var me = this;
  14434. domUtils.on(me.body, "keyup", function(evt) {
  14435. var code = evt.keyCode || evt.which,
  14436. //忽略的按键,ctr,alt,shift,方向键
  14437. ignores = {
  14438. "16": 1,
  14439. "18": 1,
  14440. "20": 1,
  14441. "37": 1,
  14442. "38": 1,
  14443. "39": 1,
  14444. "40": 1
  14445. };
  14446. if (code in ignores) return;
  14447. clearTimeout(timer);
  14448. timer = setTimeout(function() {
  14449. me.fireEvent("wordcount");
  14450. }, 200);
  14451. });
  14452. });
  14453. };
  14454. // plugins/pagebreak.js
  14455. /**
  14456. * 分页功能插件
  14457. * @file
  14458. * @since 1.2.6.1
  14459. */
  14460. UE.plugins["pagebreak"] = function() {
  14461. var me = this,
  14462. notBreakTags = ["td"];
  14463. me.setOpt("pageBreakTag", "_ueditor_page_break_tag_");
  14464. function fillNode(node) {
  14465. if (domUtils.isEmptyBlock(node)) {
  14466. var firstChild = node.firstChild,
  14467. tmpNode;
  14468. while (
  14469. firstChild &&
  14470. firstChild.nodeType == 1 &&
  14471. domUtils.isEmptyBlock(firstChild)
  14472. ) {
  14473. tmpNode = firstChild;
  14474. firstChild = firstChild.firstChild;
  14475. }
  14476. !tmpNode && (tmpNode = node);
  14477. domUtils.fillNode(me.document, tmpNode);
  14478. }
  14479. }
  14480. //分页符样式添加
  14481. me.ready(function() {
  14482. utils.cssRule(
  14483. "pagebreak",
  14484. ".pagebreak{display:block;clear:both !important;cursor:default !important;width: 100% !important;margin:0;}",
  14485. me.document
  14486. );
  14487. });
  14488. function isHr(node) {
  14489. return (
  14490. node &&
  14491. node.nodeType == 1 &&
  14492. node.tagName == "HR" &&
  14493. node.className == "pagebreak"
  14494. );
  14495. }
  14496. me.addInputRule(function(root) {
  14497. root.traversal(function(node) {
  14498. if (node.type == "text" && node.data == me.options.pageBreakTag) {
  14499. var hr = UE.uNode.createElement(
  14500. '<hr class="pagebreak" noshade="noshade" size="5" style="-webkit-user-select: none;">'
  14501. );
  14502. node.parentNode.insertBefore(hr, node);
  14503. node.parentNode.removeChild(node);
  14504. }
  14505. });
  14506. });
  14507. me.addOutputRule(function(node) {
  14508. utils.each(node.getNodesByTagName("hr"), function(n) {
  14509. if (n.getAttr("class") == "pagebreak") {
  14510. var txt = UE.uNode.createText(me.options.pageBreakTag);
  14511. n.parentNode.insertBefore(txt, n);
  14512. n.parentNode.removeChild(n);
  14513. }
  14514. });
  14515. });
  14516. /**
  14517. * 插入分页符
  14518. * @command pagebreak
  14519. * @method execCommand
  14520. * @param { String } cmd 命令字符串
  14521. * @remind 在表格中插入分页符会把表格切分成两部分
  14522. * @remind 获取编辑器内的数据时, 编辑器会把分页符转换成“_ueditor_page_break_tag_”字符串,
  14523. * 以便于提交数据到服务器端后处理分页。
  14524. * @example
  14525. * ```javascript
  14526. * editor.execCommand( 'pagebreak'); //插入一个hr标签,带有样式类名pagebreak
  14527. * ```
  14528. */
  14529. me.commands["pagebreak"] = {
  14530. execCommand: function() {
  14531. var range = me.selection.getRange(),
  14532. hr = me.document.createElement("hr");
  14533. domUtils.setAttributes(hr, {
  14534. class: "pagebreak",
  14535. noshade: "noshade",
  14536. size: "5"
  14537. });
  14538. domUtils.unSelectable(hr);
  14539. //table单独处理
  14540. var node = domUtils.findParentByTagName(
  14541. range.startContainer,
  14542. notBreakTags,
  14543. true
  14544. ),
  14545. parents = [],
  14546. pN;
  14547. if (node) {
  14548. switch (node.tagName) {
  14549. case "TD":
  14550. pN = node.parentNode;
  14551. if (!pN.previousSibling) {
  14552. var table = domUtils.findParentByTagName(pN, "table");
  14553. // var tableWrapDiv = table.parentNode;
  14554. // if(tableWrapDiv && tableWrapDiv.nodeType == 1
  14555. // && tableWrapDiv.tagName == 'DIV'
  14556. // && tableWrapDiv.getAttribute('dropdrag')
  14557. // ){
  14558. // domUtils.remove(tableWrapDiv,true);
  14559. // }
  14560. table.parentNode.insertBefore(hr, table);
  14561. parents = domUtils.findParents(hr, true);
  14562. } else {
  14563. pN.parentNode.insertBefore(hr, pN);
  14564. parents = domUtils.findParents(hr);
  14565. }
  14566. pN = parents[1];
  14567. if (hr !== pN) {
  14568. domUtils.breakParent(hr, pN);
  14569. }
  14570. //table要重写绑定一下拖拽
  14571. me.fireEvent("afteradjusttable", me.document);
  14572. }
  14573. } else {
  14574. if (!range.collapsed) {
  14575. range.deleteContents();
  14576. var start = range.startContainer;
  14577. while (
  14578. !domUtils.isBody(start) &&
  14579. domUtils.isBlockElm(start) &&
  14580. domUtils.isEmptyNode(start)
  14581. ) {
  14582. range.setStartBefore(start).collapse(true);
  14583. domUtils.remove(start);
  14584. start = range.startContainer;
  14585. }
  14586. }
  14587. range.insertNode(hr);
  14588. var pN = hr.parentNode,
  14589. nextNode;
  14590. while (!domUtils.isBody(pN)) {
  14591. domUtils.breakParent(hr, pN);
  14592. nextNode = hr.nextSibling;
  14593. if (nextNode && domUtils.isEmptyBlock(nextNode)) {
  14594. domUtils.remove(nextNode);
  14595. }
  14596. pN = hr.parentNode;
  14597. }
  14598. nextNode = hr.nextSibling;
  14599. var pre = hr.previousSibling;
  14600. if (isHr(pre)) {
  14601. domUtils.remove(pre);
  14602. } else {
  14603. pre && fillNode(pre);
  14604. }
  14605. if (!nextNode) {
  14606. var p = me.document.createElement("p");
  14607. hr.parentNode.appendChild(p);
  14608. domUtils.fillNode(me.document, p);
  14609. range.setStart(p, 0).collapse(true);
  14610. } else {
  14611. if (isHr(nextNode)) {
  14612. domUtils.remove(nextNode);
  14613. } else {
  14614. fillNode(nextNode);
  14615. }
  14616. range.setEndAfter(hr).collapse(false);
  14617. }
  14618. range.select(true);
  14619. }
  14620. }
  14621. };
  14622. };
  14623. // plugins/wordimage.js
  14624. ///import core
  14625. ///commands 本地图片引导上传
  14626. ///commandsName WordImage
  14627. ///commandsTitle 本地图片引导上传
  14628. ///commandsDialog dialogs\wordimage
  14629. UE.plugin.register("wordimage", function() {
  14630. var me = this,
  14631. images = [];
  14632. return {
  14633. commands: {
  14634. wordimage: {
  14635. execCommand: function() {
  14636. var images = domUtils.getElementsByTagName(me.body, "img");
  14637. var urlList = [];
  14638. for (var i = 0, ci; (ci = images[i++]); ) {
  14639. var url = ci.getAttribute("word_img");
  14640. url && urlList.push(url);
  14641. }
  14642. return urlList;
  14643. },
  14644. queryCommandState: function() {
  14645. images = domUtils.getElementsByTagName(me.body, "img");
  14646. for (var i = 0, ci; (ci = images[i++]); ) {
  14647. if (ci.getAttribute("word_img")) {
  14648. return 1;
  14649. }
  14650. }
  14651. return -1;
  14652. },
  14653. notNeedUndo: true
  14654. }
  14655. },
  14656. inputRule: function(root) {
  14657. utils.each(root.getNodesByTagName("img"), function(img) {
  14658. var attrs = img.attrs,
  14659. flag = parseInt(attrs.width) < 128 || parseInt(attrs.height) < 43,
  14660. opt = me.options,
  14661. src = opt.UEDITOR_HOME_URL + "themes/default/images/spacer.gif";
  14662. if (attrs["src"] && /^(?:(file:\/+))/.test(attrs["src"])) {
  14663. img.setAttr({
  14664. width: attrs.width,
  14665. height: attrs.height,
  14666. alt: attrs.alt,
  14667. word_img: attrs.src,
  14668. src: src,
  14669. style:
  14670. "background:url(" +
  14671. (flag
  14672. ? opt.themePath + opt.theme + "/images/word.gif"
  14673. : opt.langPath + opt.lang + "/images/localimage.png") +
  14674. ") no-repeat center center;border:1px solid #ddd"
  14675. });
  14676. }
  14677. });
  14678. }
  14679. };
  14680. });
  14681. // plugins/dragdrop.js
  14682. UE.plugins["dragdrop"] = function() {
  14683. var me = this;
  14684. me.ready(function() {
  14685. domUtils.on(this.body, "dragend", function() {
  14686. var rng = me.selection.getRange();
  14687. var node = rng.getClosedNode() || me.selection.getStart();
  14688. if (node && node.tagName == "IMG") {
  14689. var pre = node.previousSibling,
  14690. next;
  14691. while ((next = node.nextSibling)) {
  14692. if (
  14693. next.nodeType == 1 &&
  14694. next.tagName == "SPAN" &&
  14695. !next.firstChild
  14696. ) {
  14697. domUtils.remove(next);
  14698. } else {
  14699. break;
  14700. }
  14701. }
  14702. if (
  14703. ((pre && pre.nodeType == 1 && !domUtils.isEmptyBlock(pre)) || !pre) &&
  14704. (!next || (next && !domUtils.isEmptyBlock(next)))
  14705. ) {
  14706. if (pre && pre.tagName == "P" && !domUtils.isEmptyBlock(pre)) {
  14707. pre.appendChild(node);
  14708. domUtils.moveChild(next, pre);
  14709. domUtils.remove(next);
  14710. } else if (
  14711. next &&
  14712. next.tagName == "P" &&
  14713. !domUtils.isEmptyBlock(next)
  14714. ) {
  14715. next.insertBefore(node, next.firstChild);
  14716. }
  14717. if (pre && pre.tagName == "P" && domUtils.isEmptyBlock(pre)) {
  14718. domUtils.remove(pre);
  14719. }
  14720. if (next && next.tagName == "P" && domUtils.isEmptyBlock(next)) {
  14721. domUtils.remove(next);
  14722. }
  14723. rng.selectNode(node).select();
  14724. me.fireEvent("saveScene");
  14725. }
  14726. }
  14727. });
  14728. });
  14729. me.addListener("keyup", function(type, evt) {
  14730. var keyCode = evt.keyCode || evt.which;
  14731. if (keyCode == 13) {
  14732. var rng = me.selection.getRange(),
  14733. node;
  14734. if (
  14735. (node = domUtils.findParentByTagName(rng.startContainer, "p", true))
  14736. ) {
  14737. if (domUtils.getComputedStyle(node, "text-align") == "center") {
  14738. domUtils.removeStyle(node, "text-align");
  14739. }
  14740. }
  14741. }
  14742. });
  14743. };
  14744. // plugins/undo.js
  14745. /**
  14746. * undo redo
  14747. * @file
  14748. * @since 1.2.6.1
  14749. */
  14750. /**
  14751. * 撤销上一次执行的命令
  14752. * @command undo
  14753. * @method execCommand
  14754. * @param { String } cmd 命令字符串
  14755. * @example
  14756. * ```javascript
  14757. * editor.execCommand( 'undo' );
  14758. * ```
  14759. */
  14760. /**
  14761. * 重做上一次执行的命令
  14762. * @command redo
  14763. * @method execCommand
  14764. * @param { String } cmd 命令字符串
  14765. * @example
  14766. * ```javascript
  14767. * editor.execCommand( 'redo' );
  14768. * ```
  14769. */
  14770. UE.plugins["undo"] = function() {
  14771. var saveSceneTimer;
  14772. var me = this,
  14773. maxUndoCount = me.options.maxUndoCount || 20,
  14774. maxInputCount = me.options.maxInputCount || 20,
  14775. fillchar = new RegExp(domUtils.fillChar + "|</hr>", "gi"); // ie会产生多余的</hr>
  14776. var noNeedFillCharTags = {
  14777. ol: 1,
  14778. ul: 1,
  14779. table: 1,
  14780. tbody: 1,
  14781. tr: 1,
  14782. body: 1
  14783. };
  14784. var orgState = me.options.autoClearEmptyNode;
  14785. function compareAddr(indexA, indexB) {
  14786. if (indexA.length != indexB.length) return 0;
  14787. for (var i = 0, l = indexA.length; i < l; i++) {
  14788. if (indexA[i] != indexB[i]) return 0;
  14789. }
  14790. return 1;
  14791. }
  14792. function compareRangeAddress(rngAddrA, rngAddrB) {
  14793. if (rngAddrA.collapsed != rngAddrB.collapsed) {
  14794. return 0;
  14795. }
  14796. if (
  14797. !compareAddr(rngAddrA.startAddress, rngAddrB.startAddress) ||
  14798. !compareAddr(rngAddrA.endAddress, rngAddrB.endAddress)
  14799. ) {
  14800. return 0;
  14801. }
  14802. return 1;
  14803. }
  14804. function UndoManager() {
  14805. this.list = [];
  14806. this.index = 0;
  14807. this.hasUndo = false;
  14808. this.hasRedo = false;
  14809. this.undo = function() {
  14810. if (this.hasUndo) {
  14811. if (!this.list[this.index - 1] && this.list.length == 1) {
  14812. this.reset();
  14813. return;
  14814. }
  14815. while (
  14816. this.list[this.index].content == this.list[this.index - 1].content
  14817. ) {
  14818. this.index--;
  14819. if (this.index == 0) {
  14820. return this.restore(0);
  14821. }
  14822. }
  14823. this.restore(--this.index);
  14824. }
  14825. };
  14826. this.redo = function() {
  14827. if (this.hasRedo) {
  14828. while (
  14829. this.list[this.index].content == this.list[this.index + 1].content
  14830. ) {
  14831. this.index++;
  14832. if (this.index == this.list.length - 1) {
  14833. return this.restore(this.index);
  14834. }
  14835. }
  14836. this.restore(++this.index);
  14837. }
  14838. };
  14839. this.restore = function() {
  14840. var me = this.editor;
  14841. var scene = this.list[this.index];
  14842. var root = UE.htmlparser(scene.content.replace(fillchar, ""));
  14843. me.options.autoClearEmptyNode = false;
  14844. me.filterInputRule(root);
  14845. me.options.autoClearEmptyNode = orgState;
  14846. //trace:873
  14847. //去掉展位符
  14848. me.document.body.innerHTML = root.toHtml();
  14849. me.fireEvent("afterscencerestore");
  14850. //处理undo后空格不展位的问题
  14851. if (browser.ie) {
  14852. utils.each(
  14853. domUtils.getElementsByTagName(me.document, "td th caption p"),
  14854. function(node) {
  14855. if (domUtils.isEmptyNode(node)) {
  14856. domUtils.fillNode(me.document, node);
  14857. }
  14858. }
  14859. );
  14860. }
  14861. try {
  14862. var rng = new dom.Range(me.document).moveToAddress(scene.address);
  14863. rng.select(
  14864. noNeedFillCharTags[rng.startContainer.nodeName.toLowerCase()]
  14865. );
  14866. } catch (e) {}
  14867. this.update();
  14868. this.clearKey();
  14869. //不能把自己reset了
  14870. me.fireEvent("reset", true);
  14871. };
  14872. this.getScene = function() {
  14873. var me = this.editor;
  14874. var rng = me.selection.getRange(),
  14875. rngAddress = rng.createAddress(false, true);
  14876. me.fireEvent("beforegetscene");
  14877. var root = UE.htmlparser(me.body.innerHTML);
  14878. me.options.autoClearEmptyNode = false;
  14879. me.filterOutputRule(root);
  14880. me.options.autoClearEmptyNode = orgState;
  14881. var cont = root.toHtml();
  14882. //trace:3461
  14883. //这个会引起回退时导致空格丢失的情况
  14884. // browser.ie && (cont = cont.replace(/>&nbsp;</g, '><').replace(/\s*</g, '<').replace(/>\s*/g, '>'));
  14885. me.fireEvent("aftergetscene");
  14886. return {
  14887. address: rngAddress,
  14888. content: cont
  14889. };
  14890. };
  14891. this.save = function(notCompareRange, notSetCursor) {
  14892. clearTimeout(saveSceneTimer);
  14893. var currentScene = this.getScene(notSetCursor),
  14894. lastScene = this.list[this.index];
  14895. if (lastScene && lastScene.content != currentScene.content) {
  14896. me.trigger("contentchange");
  14897. }
  14898. //内容相同位置相同不存
  14899. if (
  14900. lastScene &&
  14901. lastScene.content == currentScene.content &&
  14902. (notCompareRange
  14903. ? 1
  14904. : compareRangeAddress(lastScene.address, currentScene.address))
  14905. ) {
  14906. return;
  14907. }
  14908. this.list = this.list.slice(0, this.index + 1);
  14909. this.list.push(currentScene);
  14910. //如果大于最大数量了,就把最前的剔除
  14911. if (this.list.length > maxUndoCount) {
  14912. this.list.shift();
  14913. }
  14914. this.index = this.list.length - 1;
  14915. this.clearKey();
  14916. //跟新undo/redo状态
  14917. this.update();
  14918. };
  14919. this.update = function() {
  14920. this.hasRedo = !!this.list[this.index + 1];
  14921. this.hasUndo = !!this.list[this.index - 1];
  14922. };
  14923. this.reset = function() {
  14924. this.list = [];
  14925. this.index = 0;
  14926. this.hasUndo = false;
  14927. this.hasRedo = false;
  14928. this.clearKey();
  14929. };
  14930. this.clearKey = function() {
  14931. keycont = 0;
  14932. lastKeyCode = null;
  14933. };
  14934. }
  14935. me.undoManger = new UndoManager();
  14936. me.undoManger.editor = me;
  14937. function saveScene() {
  14938. this.undoManger.save();
  14939. }
  14940. me.addListener("saveScene", function() {
  14941. var args = Array.prototype.splice.call(arguments, 1);
  14942. this.undoManger.save.apply(this.undoManger, args);
  14943. });
  14944. // me.addListener('beforeexeccommand', saveScene);
  14945. // me.addListener('afterexeccommand', saveScene);
  14946. me.addListener("reset", function(type, exclude) {
  14947. if (!exclude) {
  14948. this.undoManger.reset();
  14949. }
  14950. });
  14951. me.commands["redo"] = me.commands["undo"] = {
  14952. execCommand: function(cmdName) {
  14953. this.undoManger[cmdName]();
  14954. },
  14955. queryCommandState: function(cmdName) {
  14956. return this.undoManger[
  14957. "has" + (cmdName.toLowerCase() == "undo" ? "Undo" : "Redo")
  14958. ]
  14959. ? 0
  14960. : -1;
  14961. },
  14962. notNeedUndo: 1
  14963. };
  14964. var keys = {
  14965. // /*Backspace*/ 8:1, /*Delete*/ 46:1,
  14966. /*Shift*/ 16: 1,
  14967. /*Ctrl*/ 17: 1,
  14968. /*Alt*/ 18: 1,
  14969. 37: 1,
  14970. 38: 1,
  14971. 39: 1,
  14972. 40: 1
  14973. },
  14974. keycont = 0,
  14975. lastKeyCode;
  14976. //输入法状态下不计算字符数
  14977. var inputType = false;
  14978. me.addListener("ready", function() {
  14979. domUtils.on(this.body, "compositionstart", function() {
  14980. inputType = true;
  14981. });
  14982. domUtils.on(this.body, "compositionend", function() {
  14983. inputType = false;
  14984. });
  14985. });
  14986. //快捷键
  14987. me.addshortcutkey({
  14988. Undo: "ctrl+90", //undo
  14989. Redo: "ctrl+89" //redo
  14990. });
  14991. var isCollapsed = true;
  14992. me.addListener("keydown", function(type, evt) {
  14993. var me = this;
  14994. var keyCode = evt.keyCode || evt.which;
  14995. if (
  14996. !keys[keyCode] &&
  14997. !evt.ctrlKey &&
  14998. !evt.metaKey &&
  14999. !evt.shiftKey &&
  15000. !evt.altKey
  15001. ) {
  15002. if (inputType) return;
  15003. if (!me.selection.getRange().collapsed) {
  15004. me.undoManger.save(false, true);
  15005. isCollapsed = false;
  15006. return;
  15007. }
  15008. if (me.undoManger.list.length == 0) {
  15009. me.undoManger.save(true);
  15010. }
  15011. clearTimeout(saveSceneTimer);
  15012. function save(cont) {
  15013. cont.undoManger.save(false, true);
  15014. cont.fireEvent("selectionchange");
  15015. }
  15016. saveSceneTimer = setTimeout(function() {
  15017. if (inputType) {
  15018. var interalTimer = setInterval(function() {
  15019. if (!inputType) {
  15020. save(me);
  15021. clearInterval(interalTimer);
  15022. }
  15023. }, 300);
  15024. return;
  15025. }
  15026. save(me);
  15027. }, 200);
  15028. lastKeyCode = keyCode;
  15029. keycont++;
  15030. if (keycont >= maxInputCount) {
  15031. save(me);
  15032. }
  15033. }
  15034. });
  15035. me.addListener("keyup", function(type, evt) {
  15036. var keyCode = evt.keyCode || evt.which;
  15037. if (
  15038. !keys[keyCode] &&
  15039. !evt.ctrlKey &&
  15040. !evt.metaKey &&
  15041. !evt.shiftKey &&
  15042. !evt.altKey
  15043. ) {
  15044. if (inputType) return;
  15045. if (!isCollapsed) {
  15046. this.undoManger.save(false, true);
  15047. isCollapsed = true;
  15048. }
  15049. }
  15050. });
  15051. //扩展实例,添加关闭和开启命令undo
  15052. me.stopCmdUndo = function() {
  15053. me.__hasEnterExecCommand = true;
  15054. };
  15055. me.startCmdUndo = function() {
  15056. me.__hasEnterExecCommand = false;
  15057. };
  15058. };
  15059. // plugins/copy.js
  15060. UE.plugin.register("copy", function() {
  15061. var me = this;
  15062. function initZeroClipboard() {
  15063. ZeroClipboard.config({
  15064. debug: false,
  15065. swfPath:
  15066. me.options.UEDITOR_HOME_URL +
  15067. "third-party/zeroclipboard/ZeroClipboard.swf"
  15068. });
  15069. var client = (me.zeroclipboard = new ZeroClipboard());
  15070. // 复制内容
  15071. client.on("copy", function(e) {
  15072. var client = e.client,
  15073. rng = me.selection.getRange(),
  15074. div = document.createElement("div");
  15075. div.appendChild(rng.cloneContents());
  15076. client.setText(div.innerText || div.textContent);
  15077. client.setHtml(div.innerHTML);
  15078. rng.select();
  15079. });
  15080. // hover事件传递到target
  15081. client.on("mouseover mouseout", function(e) {
  15082. var target = e.target;
  15083. if (target) {
  15084. if (e.type == "mouseover") {
  15085. domUtils.addClass(target, "edui-state-hover");
  15086. } else if (e.type == "mouseout") {
  15087. domUtils.removeClasses(target, "edui-state-hover");
  15088. }
  15089. }
  15090. });
  15091. // flash加载不成功
  15092. client.on("wrongflash noflash", function() {
  15093. ZeroClipboard.destroy();
  15094. });
  15095. // 触发事件
  15096. me.fireEvent("zeroclipboardready", client);
  15097. }
  15098. return {
  15099. bindEvents: {
  15100. ready: function() {
  15101. if (!browser.ie) {
  15102. if (window.ZeroClipboard) {
  15103. initZeroClipboard();
  15104. } else {
  15105. utils.loadFile(
  15106. document,
  15107. {
  15108. src:
  15109. me.options.UEDITOR_HOME_URL +
  15110. "third-party/zeroclipboard/ZeroClipboard.js",
  15111. tag: "script",
  15112. type: "text/javascript",
  15113. defer: "defer"
  15114. },
  15115. function() {
  15116. initZeroClipboard();
  15117. }
  15118. );
  15119. }
  15120. }
  15121. }
  15122. },
  15123. commands: {
  15124. copy: {
  15125. execCommand: function(cmd) {
  15126. if (!me.document.execCommand("copy")) {
  15127. alert(me.getLang("copymsg"));
  15128. }
  15129. }
  15130. }
  15131. }
  15132. };
  15133. });
  15134. // plugins/paste.js
  15135. ///import core
  15136. ///import plugins/inserthtml.js
  15137. ///import plugins/undo.js
  15138. ///import plugins/serialize.js
  15139. ///commands 粘贴
  15140. ///commandsName PastePlain
  15141. ///commandsTitle 纯文本粘贴模式
  15142. /**
  15143. * @description 粘贴
  15144. * @author zhanyi
  15145. */
  15146. UE.plugins["paste"] = function() {
  15147. function getClipboardData(callback) {
  15148. var doc = this.document;
  15149. if (doc.getElementById("baidu_pastebin")) {
  15150. return;
  15151. }
  15152. var range = this.selection.getRange(),
  15153. bk = range.createBookmark(),
  15154. //创建剪贴的容器div
  15155. pastebin = doc.createElement("div");
  15156. pastebin.id = "baidu_pastebin";
  15157. // Safari 要求div必须有内容,才能粘贴内容进来
  15158. browser.webkit &&
  15159. pastebin.appendChild(
  15160. doc.createTextNode(domUtils.fillChar + domUtils.fillChar)
  15161. );
  15162. doc.body.appendChild(pastebin);
  15163. //trace:717 隐藏的span不能得到top
  15164. //bk.start.innerHTML = '&nbsp;';
  15165. bk.start.style.display = "";
  15166. pastebin.style.cssText =
  15167. "position:absolute;width:1px;height:1px;overflow:hidden;left:-1000px;white-space:nowrap;top:" +
  15168. //要在现在光标平行的位置加入,否则会出现跳动的问题
  15169. domUtils.getXY(bk.start).y +
  15170. "px";
  15171. range.selectNodeContents(pastebin).select(true);
  15172. setTimeout(function() {
  15173. if (browser.webkit) {
  15174. for (
  15175. var i = 0, pastebins = doc.querySelectorAll("#baidu_pastebin"), pi;
  15176. (pi = pastebins[i++]);
  15177. ) {
  15178. if (domUtils.isEmptyNode(pi)) {
  15179. domUtils.remove(pi);
  15180. } else {
  15181. pastebin = pi;
  15182. break;
  15183. }
  15184. }
  15185. }
  15186. try {
  15187. pastebin.parentNode.removeChild(pastebin);
  15188. } catch (e) {}
  15189. range.moveToBookmark(bk).select(true);
  15190. callback(pastebin);
  15191. }, 0);
  15192. }
  15193. var me = this;
  15194. me.setOpt({
  15195. retainOnlyLabelPasted: false
  15196. });
  15197. var txtContent, htmlContent, address;
  15198. function getPureHtml(html) {
  15199. return html.replace(/<(\/?)([\w\-]+)([^>]*)>/gi, function(
  15200. a,
  15201. b,
  15202. tagName,
  15203. attrs
  15204. ) {
  15205. tagName = tagName.toLowerCase();
  15206. if ({ img: 1 }[tagName]) {
  15207. return a;
  15208. }
  15209. attrs = attrs.replace(
  15210. /([\w\-]*?)\s*=\s*(("([^"]*)")|('([^']*)')|([^\s>]+))/gi,
  15211. function(str, atr, val) {
  15212. if (
  15213. {
  15214. src: 1,
  15215. href: 1,
  15216. name: 1
  15217. }[atr.toLowerCase()]
  15218. ) {
  15219. return atr + "=" + val + " ";
  15220. }
  15221. return "";
  15222. }
  15223. );
  15224. if (
  15225. {
  15226. span: 1,
  15227. div: 1
  15228. }[tagName]
  15229. ) {
  15230. return "";
  15231. } else {
  15232. return "<" + b + tagName + " " + utils.trim(attrs) + ">";
  15233. }
  15234. });
  15235. }
  15236. function filter(div) {
  15237. var html;
  15238. if (div.firstChild) {
  15239. //去掉cut中添加的边界值
  15240. var nodes = domUtils.getElementsByTagName(div, "span");
  15241. for (var i = 0, ni; (ni = nodes[i++]); ) {
  15242. if (ni.id == "_baidu_cut_start" || ni.id == "_baidu_cut_end") {
  15243. domUtils.remove(ni);
  15244. }
  15245. }
  15246. if (browser.webkit) {
  15247. var brs = div.querySelectorAll("div br");
  15248. for (var i = 0, bi; (bi = brs[i++]); ) {
  15249. var pN = bi.parentNode;
  15250. if (pN.tagName == "DIV" && pN.childNodes.length == 1) {
  15251. pN.innerHTML = "<p><br/></p>";
  15252. domUtils.remove(pN);
  15253. }
  15254. }
  15255. var divs = div.querySelectorAll("#baidu_pastebin");
  15256. for (var i = 0, di; (di = divs[i++]); ) {
  15257. var tmpP = me.document.createElement("p");
  15258. di.parentNode.insertBefore(tmpP, di);
  15259. while (di.firstChild) {
  15260. tmpP.appendChild(di.firstChild);
  15261. }
  15262. domUtils.remove(di);
  15263. }
  15264. var metas = div.querySelectorAll("meta");
  15265. for (var i = 0, ci; (ci = metas[i++]); ) {
  15266. domUtils.remove(ci);
  15267. }
  15268. var brs = div.querySelectorAll("br");
  15269. for (i = 0; (ci = brs[i++]); ) {
  15270. if (/^apple-/i.test(ci.className)) {
  15271. domUtils.remove(ci);
  15272. }
  15273. }
  15274. }
  15275. if (browser.gecko) {
  15276. var dirtyNodes = div.querySelectorAll("[_moz_dirty]");
  15277. for (i = 0; (ci = dirtyNodes[i++]); ) {
  15278. ci.removeAttribute("_moz_dirty");
  15279. }
  15280. }
  15281. if (!browser.ie) {
  15282. var spans = div.querySelectorAll("span.Apple-style-span");
  15283. for (var i = 0, ci; (ci = spans[i++]); ) {
  15284. domUtils.remove(ci, true);
  15285. }
  15286. }
  15287. //ie下使用innerHTML会产生多余的\r\n字符,也会产生&nbsp;这里过滤掉
  15288. html = div.innerHTML; //.replace(/>(?:(\s|&nbsp;)*?)</g,'><');
  15289. //过滤word粘贴过来的冗余属性
  15290. html = UE.filterWord(html);
  15291. //取消了忽略空白的第二个参数,粘贴过来的有些是有空白的,会被套上相关的标签
  15292. var root = UE.htmlparser(html);
  15293. //如果给了过滤规则就先进行过滤
  15294. if (me.options.filterRules) {
  15295. UE.filterNode(root, me.options.filterRules);
  15296. }
  15297. //执行默认的处理
  15298. me.filterInputRule(root);
  15299. //针对chrome的处理
  15300. if (browser.webkit) {
  15301. var br = root.lastChild();
  15302. if (br && br.type == "element" && br.tagName == "br") {
  15303. root.removeChild(br);
  15304. }
  15305. utils.each(me.body.querySelectorAll("div"), function(node) {
  15306. if (domUtils.isEmptyBlock(node)) {
  15307. domUtils.remove(node, true);
  15308. }
  15309. });
  15310. }
  15311. html = { html: root.toHtml() };
  15312. me.fireEvent("beforepaste", html, root);
  15313. //抢了默认的粘贴,那后边的内容就不执行了,比如表格粘贴
  15314. if (!html.html) {
  15315. return;
  15316. }
  15317. root = UE.htmlparser(html.html, true);
  15318. //如果开启了纯文本模式
  15319. if (me.queryCommandState("pasteplain") === 1) {
  15320. me.execCommand(
  15321. "insertHtml",
  15322. UE.filterNode(root, me.options.filterTxtRules).toHtml(),
  15323. true
  15324. );
  15325. } else {
  15326. //文本模式
  15327. UE.filterNode(root, me.options.filterTxtRules);
  15328. txtContent = root.toHtml();
  15329. //完全模式
  15330. htmlContent = html.html;
  15331. address = me.selection.getRange().createAddress(true);
  15332. me.execCommand(
  15333. "insertHtml",
  15334. me.getOpt("retainOnlyLabelPasted") === true
  15335. ? getPureHtml(htmlContent)
  15336. : htmlContent,
  15337. true
  15338. );
  15339. }
  15340. me.fireEvent("afterpaste", html);
  15341. }
  15342. }
  15343. me.addListener("pasteTransfer", function(cmd, plainType) {
  15344. if (address && txtContent && htmlContent && txtContent != htmlContent) {
  15345. var range = me.selection.getRange();
  15346. range.moveToAddress(address, true);
  15347. if (!range.collapsed) {
  15348. while (!domUtils.isBody(range.startContainer)) {
  15349. var start = range.startContainer;
  15350. if (start.nodeType == 1) {
  15351. start = start.childNodes[range.startOffset];
  15352. if (!start) {
  15353. range.setStartBefore(range.startContainer);
  15354. continue;
  15355. }
  15356. var pre = start.previousSibling;
  15357. if (
  15358. pre &&
  15359. pre.nodeType == 3 &&
  15360. new RegExp("^[\n\r\t " + domUtils.fillChar + "]*$").test(
  15361. pre.nodeValue
  15362. )
  15363. ) {
  15364. range.setStartBefore(pre);
  15365. }
  15366. }
  15367. if (range.startOffset == 0) {
  15368. range.setStartBefore(range.startContainer);
  15369. } else {
  15370. break;
  15371. }
  15372. }
  15373. while (!domUtils.isBody(range.endContainer)) {
  15374. var end = range.endContainer;
  15375. if (end.nodeType == 1) {
  15376. end = end.childNodes[range.endOffset];
  15377. if (!end) {
  15378. range.setEndAfter(range.endContainer);
  15379. continue;
  15380. }
  15381. var next = end.nextSibling;
  15382. if (
  15383. next &&
  15384. next.nodeType == 3 &&
  15385. new RegExp("^[\n\r\t" + domUtils.fillChar + "]*$").test(
  15386. next.nodeValue
  15387. )
  15388. ) {
  15389. range.setEndAfter(next);
  15390. }
  15391. }
  15392. if (
  15393. range.endOffset ==
  15394. range.endContainer[
  15395. range.endContainer.nodeType == 3 ? "nodeValue" : "childNodes"
  15396. ].length
  15397. ) {
  15398. range.setEndAfter(range.endContainer);
  15399. } else {
  15400. break;
  15401. }
  15402. }
  15403. }
  15404. range.deleteContents();
  15405. range.select(true);
  15406. me.__hasEnterExecCommand = true;
  15407. var html = htmlContent;
  15408. if (plainType === 2) {
  15409. html = getPureHtml(html);
  15410. } else if (plainType) {
  15411. html = txtContent;
  15412. }
  15413. me.execCommand("inserthtml", html, true);
  15414. me.__hasEnterExecCommand = false;
  15415. var rng = me.selection.getRange();
  15416. while (
  15417. !domUtils.isBody(rng.startContainer) &&
  15418. !rng.startOffset &&
  15419. rng.startContainer[
  15420. rng.startContainer.nodeType == 3 ? "nodeValue" : "childNodes"
  15421. ].length
  15422. ) {
  15423. rng.setStartBefore(rng.startContainer);
  15424. }
  15425. var tmpAddress = rng.createAddress(true);
  15426. address.endAddress = tmpAddress.startAddress;
  15427. }
  15428. });
  15429. me.addListener("ready", function() {
  15430. domUtils.on(me.body, "cut", function() {
  15431. var range = me.selection.getRange();
  15432. if (!range.collapsed && me.undoManger) {
  15433. me.undoManger.save();
  15434. }
  15435. });
  15436. //ie下beforepaste在点击右键时也会触发,所以用监控键盘才处理
  15437. domUtils.on(
  15438. me.body,
  15439. browser.ie || browser.opera ? "keydown" : "paste",
  15440. function(e) {
  15441. if (
  15442. (browser.ie || browser.opera) &&
  15443. ((!e.ctrlKey && !e.metaKey) || e.keyCode != "86")
  15444. ) {
  15445. return;
  15446. }
  15447. getClipboardData.call(me, function(div) {
  15448. filter(div);
  15449. });
  15450. }
  15451. );
  15452. });
  15453. me.commands["paste"] = {
  15454. execCommand: function(cmd) {
  15455. if (browser.ie) {
  15456. getClipboardData.call(me, function(div) {
  15457. filter(div);
  15458. });
  15459. me.document.execCommand("paste");
  15460. } else {
  15461. alert(me.getLang("pastemsg"));
  15462. }
  15463. }
  15464. };
  15465. };
  15466. // plugins/puretxtpaste.js
  15467. /**
  15468. * 纯文本粘贴插件
  15469. * @file
  15470. * @since 1.2.6.1
  15471. */
  15472. UE.plugins["pasteplain"] = function() {
  15473. var me = this;
  15474. me.setOpt({
  15475. pasteplain: false,
  15476. filterTxtRules: (function() {
  15477. function transP(node) {
  15478. node.tagName = "p";
  15479. node.setStyle();
  15480. }
  15481. function removeNode(node) {
  15482. node.parentNode.removeChild(node, true);
  15483. }
  15484. return {
  15485. //直接删除及其字节点内容
  15486. "-": "script style object iframe embed input select",
  15487. p: { $: {} },
  15488. br: { $: {} },
  15489. div: function(node) {
  15490. var tmpNode,
  15491. p = UE.uNode.createElement("p");
  15492. while ((tmpNode = node.firstChild())) {
  15493. if (tmpNode.type == "text" || !UE.dom.dtd.$block[tmpNode.tagName]) {
  15494. p.appendChild(tmpNode);
  15495. } else {
  15496. if (p.firstChild()) {
  15497. node.parentNode.insertBefore(p, node);
  15498. p = UE.uNode.createElement("p");
  15499. } else {
  15500. node.parentNode.insertBefore(tmpNode, node);
  15501. }
  15502. }
  15503. }
  15504. if (p.firstChild()) {
  15505. node.parentNode.insertBefore(p, node);
  15506. }
  15507. node.parentNode.removeChild(node);
  15508. },
  15509. ol: removeNode,
  15510. ul: removeNode,
  15511. dl: removeNode,
  15512. dt: removeNode,
  15513. dd: removeNode,
  15514. li: removeNode,
  15515. caption: transP,
  15516. th: transP,
  15517. tr: transP,
  15518. h1: transP,
  15519. h2: transP,
  15520. h3: transP,
  15521. h4: transP,
  15522. h5: transP,
  15523. h6: transP,
  15524. td: function(node) {
  15525. //没有内容的td直接删掉
  15526. var txt = !!node.innerText();
  15527. if (txt) {
  15528. node.parentNode.insertAfter(
  15529. UE.uNode.createText(" &nbsp; &nbsp;"),
  15530. node
  15531. );
  15532. }
  15533. node.parentNode.removeChild(node, node.innerText());
  15534. }
  15535. };
  15536. })()
  15537. });
  15538. //暂时这里支持一下老版本的属性
  15539. var pasteplain = me.options.pasteplain;
  15540. /**
  15541. * 启用或取消纯文本粘贴模式
  15542. * @command pasteplain
  15543. * @method execCommand
  15544. * @param { String } cmd 命令字符串
  15545. * @example
  15546. * ```javascript
  15547. * editor.queryCommandState( 'pasteplain' );
  15548. * ```
  15549. */
  15550. /**
  15551. * 查询当前是否处于纯文本粘贴模式
  15552. * @command pasteplain
  15553. * @method queryCommandState
  15554. * @param { String } cmd 命令字符串
  15555. * @return { int } 如果处于纯文本模式,返回1,否则,返回0
  15556. * @example
  15557. * ```javascript
  15558. * editor.queryCommandState( 'pasteplain' );
  15559. * ```
  15560. */
  15561. me.commands["pasteplain"] = {
  15562. queryCommandState: function() {
  15563. return pasteplain ? 1 : 0;
  15564. },
  15565. execCommand: function() {
  15566. pasteplain = !pasteplain | 0;
  15567. },
  15568. notNeedUndo: 1
  15569. };
  15570. };
  15571. // plugins/list.js
  15572. /**
  15573. * 有序列表,无序列表插件
  15574. * @file
  15575. * @since 1.2.6.1
  15576. */
  15577. UE.plugins["list"] = function() {
  15578. var me = this,
  15579. notExchange = {
  15580. TD: 1,
  15581. PRE: 1,
  15582. BLOCKQUOTE: 1
  15583. };
  15584. var customStyle = {
  15585. cn: "cn-1-",
  15586. cn1: "cn-2-",
  15587. cn2: "cn-3-",
  15588. num: "num-1-",
  15589. num1: "num-2-",
  15590. num2: "num-3-",
  15591. dash: "dash",
  15592. dot: "dot"
  15593. };
  15594. me.setOpt({
  15595. autoTransWordToList: false,
  15596. insertorderedlist: {
  15597. num: "",
  15598. num1: "",
  15599. num2: "",
  15600. cn: "",
  15601. cn1: "",
  15602. cn2: "",
  15603. decimal: "",
  15604. "lower-alpha": "",
  15605. "lower-roman": "",
  15606. "upper-alpha": "",
  15607. "upper-roman": ""
  15608. },
  15609. insertunorderedlist: {
  15610. circle: "",
  15611. disc: "",
  15612. square: "",
  15613. dash: "",
  15614. dot: ""
  15615. },
  15616. listDefaultPaddingLeft: "30",
  15617. listiconpath: "http://bs.baidu.com/listicon/",
  15618. maxListLevel: -1, //-1不限制
  15619. disablePInList: false
  15620. });
  15621. function listToArray(list) {
  15622. var arr = [];
  15623. for (var p in list) {
  15624. arr.push(p);
  15625. }
  15626. return arr;
  15627. }
  15628. var listStyle = {
  15629. OL: listToArray(me.options.insertorderedlist),
  15630. UL: listToArray(me.options.insertunorderedlist)
  15631. };
  15632. var liiconpath = me.options.listiconpath;
  15633. //根据用户配置,调整customStyle
  15634. for (var s in customStyle) {
  15635. if (
  15636. !me.options.insertorderedlist.hasOwnProperty(s) &&
  15637. !me.options.insertunorderedlist.hasOwnProperty(s)
  15638. ) {
  15639. delete customStyle[s];
  15640. }
  15641. }
  15642. me.ready(function() {
  15643. var customCss = [];
  15644. for (var p in customStyle) {
  15645. if (p == "dash" || p == "dot") {
  15646. customCss.push(
  15647. "li.list-" +
  15648. customStyle[p] +
  15649. "{background-image:url(" +
  15650. liiconpath +
  15651. customStyle[p] +
  15652. ".gif)}"
  15653. );
  15654. customCss.push(
  15655. "ul.custom_" +
  15656. p +
  15657. "{list-style:none;}ul.custom_" +
  15658. p +
  15659. " li{background-position:0 3px;background-repeat:no-repeat}"
  15660. );
  15661. } else {
  15662. for (var i = 0; i < 99; i++) {
  15663. customCss.push(
  15664. "li.list-" +
  15665. customStyle[p] +
  15666. i +
  15667. "{background-image:url(" +
  15668. liiconpath +
  15669. "list-" +
  15670. customStyle[p] +
  15671. i +
  15672. ".gif)}"
  15673. );
  15674. }
  15675. customCss.push(
  15676. "ol.custom_" +
  15677. p +
  15678. "{list-style:none;}ol.custom_" +
  15679. p +
  15680. " li{background-position:0 3px;background-repeat:no-repeat}"
  15681. );
  15682. }
  15683. switch (p) {
  15684. case "cn":
  15685. customCss.push("li.list-" + p + "-paddingleft-1{padding-left:25px}");
  15686. customCss.push("li.list-" + p + "-paddingleft-2{padding-left:40px}");
  15687. customCss.push("li.list-" + p + "-paddingleft-3{padding-left:55px}");
  15688. break;
  15689. case "cn1":
  15690. customCss.push("li.list-" + p + "-paddingleft-1{padding-left:30px}");
  15691. customCss.push("li.list-" + p + "-paddingleft-2{padding-left:40px}");
  15692. customCss.push("li.list-" + p + "-paddingleft-3{padding-left:55px}");
  15693. break;
  15694. case "cn2":
  15695. customCss.push("li.list-" + p + "-paddingleft-1{padding-left:40px}");
  15696. customCss.push("li.list-" + p + "-paddingleft-2{padding-left:55px}");
  15697. customCss.push("li.list-" + p + "-paddingleft-3{padding-left:68px}");
  15698. break;
  15699. case "num":
  15700. case "num1":
  15701. customCss.push("li.list-" + p + "-paddingleft-1{padding-left:25px}");
  15702. break;
  15703. case "num2":
  15704. customCss.push("li.list-" + p + "-paddingleft-1{padding-left:35px}");
  15705. customCss.push("li.list-" + p + "-paddingleft-2{padding-left:40px}");
  15706. break;
  15707. case "dash":
  15708. customCss.push("li.list-" + p + "-paddingleft{padding-left:35px}");
  15709. break;
  15710. case "dot":
  15711. customCss.push("li.list-" + p + "-paddingleft{padding-left:20px}");
  15712. }
  15713. }
  15714. customCss.push(".list-paddingleft-1{padding-left:0}");
  15715. customCss.push(
  15716. ".list-paddingleft-2{padding-left:" +
  15717. me.options.listDefaultPaddingLeft +
  15718. "px}"
  15719. );
  15720. customCss.push(
  15721. ".list-paddingleft-3{padding-left:" +
  15722. me.options.listDefaultPaddingLeft * 2 +
  15723. "px}"
  15724. );
  15725. //如果不给宽度会在自定应样式里出现滚动条
  15726. utils.cssRule(
  15727. "list",
  15728. "ol,ul{margin:0;pading:0;" +
  15729. (browser.ie ? "" : "width:95%") +
  15730. "}li{clear:both;}" +
  15731. customCss.join("\n"),
  15732. me.document
  15733. );
  15734. });
  15735. //单独处理剪切的问题
  15736. me.ready(function() {
  15737. domUtils.on(me.body, "cut", function() {
  15738. setTimeout(function() {
  15739. var rng = me.selection.getRange(),
  15740. li;
  15741. //trace:3416
  15742. if (!rng.collapsed) {
  15743. if (
  15744. (li = domUtils.findParentByTagName(rng.startContainer, "li", true))
  15745. ) {
  15746. if (!li.nextSibling && domUtils.isEmptyBlock(li)) {
  15747. var pn = li.parentNode,
  15748. node;
  15749. if ((node = pn.previousSibling)) {
  15750. domUtils.remove(pn);
  15751. rng.setStartAtLast(node).collapse(true);
  15752. rng.select(true);
  15753. } else if ((node = pn.nextSibling)) {
  15754. domUtils.remove(pn);
  15755. rng.setStartAtFirst(node).collapse(true);
  15756. rng.select(true);
  15757. } else {
  15758. var tmpNode = me.document.createElement("p");
  15759. domUtils.fillNode(me.document, tmpNode);
  15760. pn.parentNode.insertBefore(tmpNode, pn);
  15761. domUtils.remove(pn);
  15762. rng.setStart(tmpNode, 0).collapse(true);
  15763. rng.select(true);
  15764. }
  15765. }
  15766. }
  15767. }
  15768. });
  15769. });
  15770. });
  15771. function getStyle(node) {
  15772. var cls = node.className;
  15773. if (domUtils.hasClass(node, /custom_/)) {
  15774. return cls.match(/custom_(\w+)/)[1];
  15775. }
  15776. return domUtils.getStyle(node, "list-style-type");
  15777. }
  15778. me.addListener("beforepaste", function(type, html) {
  15779. var me = this,
  15780. rng = me.selection.getRange(),
  15781. li;
  15782. var root = UE.htmlparser(html.html, true);
  15783. if ((li = domUtils.findParentByTagName(rng.startContainer, "li", true))) {
  15784. var list = li.parentNode,
  15785. tagName = list.tagName == "OL" ? "ul" : "ol";
  15786. utils.each(root.getNodesByTagName(tagName), function(n) {
  15787. n.tagName = list.tagName;
  15788. n.setAttr();
  15789. if (n.parentNode === root) {
  15790. type = getStyle(list) || (list.tagName == "OL" ? "decimal" : "disc");
  15791. } else {
  15792. var className = n.parentNode.getAttr("class");
  15793. if (className && /custom_/.test(className)) {
  15794. type = className.match(/custom_(\w+)/)[1];
  15795. } else {
  15796. type = n.parentNode.getStyle("list-style-type");
  15797. }
  15798. if (!type) {
  15799. type = list.tagName == "OL" ? "decimal" : "disc";
  15800. }
  15801. }
  15802. var index = utils.indexOf(listStyle[list.tagName], type);
  15803. if (n.parentNode !== root)
  15804. index = index + 1 == listStyle[list.tagName].length ? 0 : index + 1;
  15805. var currentStyle = listStyle[list.tagName][index];
  15806. if (customStyle[currentStyle]) {
  15807. n.setAttr("class", "custom_" + currentStyle);
  15808. } else {
  15809. n.setStyle("list-style-type", currentStyle);
  15810. }
  15811. });
  15812. }
  15813. html.html = root.toHtml();
  15814. });
  15815. //导出时,去掉p标签
  15816. me.getOpt("disablePInList") === true &&
  15817. me.addOutputRule(function(root) {
  15818. utils.each(root.getNodesByTagName("li"), function(li) {
  15819. var newChildrens = [],
  15820. index = 0;
  15821. utils.each(li.children, function(n) {
  15822. if (n.tagName == "p") {
  15823. var tmpNode;
  15824. while ((tmpNode = n.children.pop())) {
  15825. newChildrens.splice(index, 0, tmpNode);
  15826. tmpNode.parentNode = li;
  15827. lastNode = tmpNode;
  15828. }
  15829. tmpNode = newChildrens[newChildrens.length - 1];
  15830. if (
  15831. !tmpNode ||
  15832. tmpNode.type != "element" ||
  15833. tmpNode.tagName != "br"
  15834. ) {
  15835. var br = UE.uNode.createElement("br");
  15836. br.parentNode = li;
  15837. newChildrens.push(br);
  15838. }
  15839. index = newChildrens.length;
  15840. }
  15841. });
  15842. if (newChildrens.length) {
  15843. li.children = newChildrens;
  15844. }
  15845. });
  15846. });
  15847. //进入编辑器的li要套p标签
  15848. me.addInputRule(function(root) {
  15849. utils.each(root.getNodesByTagName("li"), function(li) {
  15850. var tmpP = UE.uNode.createElement("p");
  15851. for (var i = 0, ci; (ci = li.children[i]); ) {
  15852. if (ci.type == "text" || dtd.p[ci.tagName]) {
  15853. tmpP.appendChild(ci);
  15854. } else {
  15855. if (tmpP.firstChild()) {
  15856. li.insertBefore(tmpP, ci);
  15857. tmpP = UE.uNode.createElement("p");
  15858. i = i + 2;
  15859. } else {
  15860. i++;
  15861. }
  15862. }
  15863. }
  15864. if ((tmpP.firstChild() && !tmpP.parentNode) || !li.firstChild()) {
  15865. li.appendChild(tmpP);
  15866. }
  15867. //trace:3357
  15868. //p不能为空
  15869. if (!tmpP.firstChild()) {
  15870. tmpP.innerHTML(browser.ie ? "&nbsp;" : "<br/>");
  15871. }
  15872. //去掉末尾的空白
  15873. var p = li.firstChild();
  15874. var lastChild = p.lastChild();
  15875. if (
  15876. lastChild &&
  15877. lastChild.type == "text" &&
  15878. /^\s*$/.test(lastChild.data)
  15879. ) {
  15880. p.removeChild(lastChild);
  15881. }
  15882. });
  15883. if (me.options.autoTransWordToList) {
  15884. var orderlisttype = {
  15885. num1: /^\d+\)/,
  15886. decimal: /^\d+\./,
  15887. "lower-alpha": /^[a-z]+\)/,
  15888. "upper-alpha": /^[A-Z]+\./,
  15889. cn: /^[\u4E00\u4E8C\u4E09\u56DB\u516d\u4e94\u4e03\u516b\u4e5d]+[\u3001]/,
  15890. cn2: /^\([\u4E00\u4E8C\u4E09\u56DB\u516d\u4e94\u4e03\u516b\u4e5d]+\)/
  15891. },
  15892. unorderlisttype = {
  15893. square: "n"
  15894. };
  15895. function checkListType(content, container) {
  15896. var span = container.firstChild();
  15897. if (
  15898. span &&
  15899. span.type == "element" &&
  15900. span.tagName == "span" &&
  15901. /Wingdings|Symbol/.test(span.getStyle("font-family"))
  15902. ) {
  15903. for (var p in unorderlisttype) {
  15904. if (unorderlisttype[p] == span.data) {
  15905. return p;
  15906. }
  15907. }
  15908. return "disc";
  15909. }
  15910. for (var p in orderlisttype) {
  15911. if (orderlisttype[p].test(content)) {
  15912. return p;
  15913. }
  15914. }
  15915. }
  15916. utils.each(root.getNodesByTagName("p"), function(node) {
  15917. if (node.getAttr("class") != "MsoListParagraph") {
  15918. return;
  15919. }
  15920. //word粘贴过来的会带有margin要去掉,但这样也可能会误命中一些央视
  15921. node.setStyle("margin", "");
  15922. node.setStyle("margin-left", "");
  15923. node.setAttr("class", "");
  15924. function appendLi(list, p, type) {
  15925. if (list.tagName == "ol") {
  15926. if (browser.ie) {
  15927. var first = p.firstChild();
  15928. if (
  15929. first.type == "element" &&
  15930. first.tagName == "span" &&
  15931. orderlisttype[type].test(first.innerText())
  15932. ) {
  15933. p.removeChild(first);
  15934. }
  15935. } else {
  15936. p.innerHTML(p.innerHTML().replace(orderlisttype[type], ""));
  15937. }
  15938. } else {
  15939. p.removeChild(p.firstChild());
  15940. }
  15941. var li = UE.uNode.createElement("li");
  15942. li.appendChild(p);
  15943. list.appendChild(li);
  15944. }
  15945. var tmp = node,
  15946. type,
  15947. cacheNode = node;
  15948. if (
  15949. node.parentNode.tagName != "li" &&
  15950. (type = checkListType(node.innerText(), node))
  15951. ) {
  15952. var list = UE.uNode.createElement(
  15953. me.options.insertorderedlist.hasOwnProperty(type) ? "ol" : "ul"
  15954. );
  15955. if (customStyle[type]) {
  15956. list.setAttr("class", "custom_" + type);
  15957. } else {
  15958. list.setStyle("list-style-type", type);
  15959. }
  15960. while (
  15961. node &&
  15962. node.parentNode.tagName != "li" &&
  15963. checkListType(node.innerText(), node)
  15964. ) {
  15965. tmp = node.nextSibling();
  15966. if (!tmp) {
  15967. node.parentNode.insertBefore(list, node);
  15968. }
  15969. appendLi(list, node, type);
  15970. node = tmp;
  15971. }
  15972. if (!list.parentNode && node && node.parentNode) {
  15973. node.parentNode.insertBefore(list, node);
  15974. }
  15975. }
  15976. var span = cacheNode.firstChild();
  15977. if (
  15978. span &&
  15979. span.type == "element" &&
  15980. span.tagName == "span" &&
  15981. /^\s*(&nbsp;)+\s*$/.test(span.innerText())
  15982. ) {
  15983. span.parentNode.removeChild(span);
  15984. }
  15985. });
  15986. }
  15987. });
  15988. //调整索引标签
  15989. me.addListener("contentchange", function() {
  15990. adjustListStyle(me.document);
  15991. });
  15992. function adjustListStyle(doc, ignore) {
  15993. utils.each(domUtils.getElementsByTagName(doc, "ol ul"), function(node) {
  15994. if (!domUtils.inDoc(node, doc)) return;
  15995. var parent = node.parentNode;
  15996. if (parent.tagName == node.tagName) {
  15997. var nodeStyleType =
  15998. getStyle(node) || (node.tagName == "OL" ? "decimal" : "disc"),
  15999. parentStyleType =
  16000. getStyle(parent) || (parent.tagName == "OL" ? "decimal" : "disc");
  16001. if (nodeStyleType == parentStyleType) {
  16002. var styleIndex = utils.indexOf(
  16003. listStyle[node.tagName],
  16004. nodeStyleType
  16005. );
  16006. styleIndex = styleIndex + 1 == listStyle[node.tagName].length
  16007. ? 0
  16008. : styleIndex + 1;
  16009. setListStyle(node, listStyle[node.tagName][styleIndex]);
  16010. }
  16011. }
  16012. var index = 0,
  16013. type = 2;
  16014. if (domUtils.hasClass(node, /custom_/)) {
  16015. if (
  16016. !(
  16017. /[ou]l/i.test(parent.tagName) &&
  16018. domUtils.hasClass(parent, /custom_/)
  16019. )
  16020. ) {
  16021. type = 1;
  16022. }
  16023. } else {
  16024. if (
  16025. /[ou]l/i.test(parent.tagName) &&
  16026. domUtils.hasClass(parent, /custom_/)
  16027. ) {
  16028. type = 3;
  16029. }
  16030. }
  16031. var style = domUtils.getStyle(node, "list-style-type");
  16032. style && (node.style.cssText = "list-style-type:" + style);
  16033. node.className =
  16034. utils.trim(node.className.replace(/list-paddingleft-\w+/, "")) +
  16035. " list-paddingleft-" +
  16036. type;
  16037. utils.each(domUtils.getElementsByTagName(node, "li"), function(li) {
  16038. li.style.cssText && (li.style.cssText = "");
  16039. if (!li.firstChild) {
  16040. domUtils.remove(li);
  16041. return;
  16042. }
  16043. if (li.parentNode !== node) {
  16044. return;
  16045. }
  16046. index++;
  16047. if (domUtils.hasClass(node, /custom_/)) {
  16048. var paddingLeft = 1,
  16049. currentStyle = getStyle(node);
  16050. if (node.tagName == "OL") {
  16051. if (currentStyle) {
  16052. switch (currentStyle) {
  16053. case "cn":
  16054. case "cn1":
  16055. case "cn2":
  16056. if (
  16057. index > 10 &&
  16058. (index % 10 == 0 || (index > 10 && index < 20))
  16059. ) {
  16060. paddingLeft = 2;
  16061. } else if (index > 20) {
  16062. paddingLeft = 3;
  16063. }
  16064. break;
  16065. case "num2":
  16066. if (index > 9) {
  16067. paddingLeft = 2;
  16068. }
  16069. }
  16070. }
  16071. li.className =
  16072. "list-" +
  16073. customStyle[currentStyle] +
  16074. index +
  16075. " " +
  16076. "list-" +
  16077. currentStyle +
  16078. "-paddingleft-" +
  16079. paddingLeft;
  16080. } else {
  16081. li.className =
  16082. "list-" +
  16083. customStyle[currentStyle] +
  16084. " " +
  16085. "list-" +
  16086. currentStyle +
  16087. "-paddingleft";
  16088. }
  16089. } else {
  16090. li.className = li.className.replace(/list-[\w\-]+/gi, "");
  16091. }
  16092. var className = li.getAttribute("class");
  16093. if (className !== null && !className.replace(/\s/g, "")) {
  16094. domUtils.removeAttributes(li, "class");
  16095. }
  16096. });
  16097. !ignore &&
  16098. adjustList(
  16099. node,
  16100. node.tagName.toLowerCase(),
  16101. getStyle(node) || domUtils.getStyle(node, "list-style-type"),
  16102. true
  16103. );
  16104. });
  16105. }
  16106. function adjustList(list, tag, style, ignoreEmpty) {
  16107. var nextList = list.nextSibling;
  16108. if (
  16109. nextList &&
  16110. nextList.nodeType == 1 &&
  16111. nextList.tagName.toLowerCase() == tag &&
  16112. (getStyle(nextList) ||
  16113. domUtils.getStyle(nextList, "list-style-type") ||
  16114. (tag == "ol" ? "decimal" : "disc")) == style
  16115. ) {
  16116. domUtils.moveChild(nextList, list);
  16117. if (nextList.childNodes.length == 0) {
  16118. domUtils.remove(nextList);
  16119. }
  16120. }
  16121. if (nextList && domUtils.isFillChar(nextList)) {
  16122. domUtils.remove(nextList);
  16123. }
  16124. var preList = list.previousSibling;
  16125. if (
  16126. preList &&
  16127. preList.nodeType == 1 &&
  16128. preList.tagName.toLowerCase() == tag &&
  16129. (getStyle(preList) ||
  16130. domUtils.getStyle(preList, "list-style-type") ||
  16131. (tag == "ol" ? "decimal" : "disc")) == style
  16132. ) {
  16133. domUtils.moveChild(list, preList);
  16134. }
  16135. if (preList && domUtils.isFillChar(preList)) {
  16136. domUtils.remove(preList);
  16137. }
  16138. !ignoreEmpty && domUtils.isEmptyBlock(list) && domUtils.remove(list);
  16139. if (getStyle(list)) {
  16140. adjustListStyle(list.ownerDocument, true);
  16141. }
  16142. }
  16143. function setListStyle(list, style) {
  16144. if (customStyle[style]) {
  16145. list.className = "custom_" + style;
  16146. }
  16147. try {
  16148. domUtils.setStyle(list, "list-style-type", style);
  16149. } catch (e) {}
  16150. }
  16151. function clearEmptySibling(node) {
  16152. var tmpNode = node.previousSibling;
  16153. if (tmpNode && domUtils.isEmptyBlock(tmpNode)) {
  16154. domUtils.remove(tmpNode);
  16155. }
  16156. tmpNode = node.nextSibling;
  16157. if (tmpNode && domUtils.isEmptyBlock(tmpNode)) {
  16158. domUtils.remove(tmpNode);
  16159. }
  16160. }
  16161. me.addListener("keydown", function(type, evt) {
  16162. function preventAndSave() {
  16163. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
  16164. me.fireEvent("contentchange");
  16165. me.undoManger && me.undoManger.save();
  16166. }
  16167. function findList(node, filterFn) {
  16168. while (node && !domUtils.isBody(node)) {
  16169. if (filterFn(node)) {
  16170. return null;
  16171. }
  16172. if (node.nodeType == 1 && /[ou]l/i.test(node.tagName)) {
  16173. return node;
  16174. }
  16175. node = node.parentNode;
  16176. }
  16177. return null;
  16178. }
  16179. var keyCode = evt.keyCode || evt.which;
  16180. if (keyCode == 13 && !evt.shiftKey) {
  16181. //回车
  16182. var rng = me.selection.getRange(),
  16183. parent = domUtils.findParent(
  16184. rng.startContainer,
  16185. function(node) {
  16186. return domUtils.isBlockElm(node);
  16187. },
  16188. true
  16189. ),
  16190. li = domUtils.findParentByTagName(rng.startContainer, "li", true);
  16191. if (parent && parent.tagName != "PRE" && !li) {
  16192. var html = parent.innerHTML.replace(
  16193. new RegExp(domUtils.fillChar, "g"),
  16194. ""
  16195. );
  16196. if (/^\s*1\s*\.[^\d]/.test(html)) {
  16197. parent.innerHTML = html.replace(/^\s*1\s*\./, "");
  16198. rng.setStartAtLast(parent).collapse(true).select();
  16199. me.__hasEnterExecCommand = true;
  16200. me.execCommand("insertorderedlist");
  16201. me.__hasEnterExecCommand = false;
  16202. }
  16203. }
  16204. var range = me.selection.getRange(),
  16205. start = findList(range.startContainer, function(node) {
  16206. return node.tagName == "TABLE";
  16207. }),
  16208. end = range.collapsed
  16209. ? start
  16210. : findList(range.endContainer, function(node) {
  16211. return node.tagName == "TABLE";
  16212. });
  16213. if (start && end && start === end) {
  16214. if (!range.collapsed) {
  16215. start = domUtils.findParentByTagName(
  16216. range.startContainer,
  16217. "li",
  16218. true
  16219. );
  16220. end = domUtils.findParentByTagName(range.endContainer, "li", true);
  16221. if (start && end && start === end) {
  16222. range.deleteContents();
  16223. li = domUtils.findParentByTagName(range.startContainer, "li", true);
  16224. if (li && domUtils.isEmptyBlock(li)) {
  16225. pre = li.previousSibling;
  16226. next = li.nextSibling;
  16227. p = me.document.createElement("p");
  16228. domUtils.fillNode(me.document, p);
  16229. parentList = li.parentNode;
  16230. if (pre && next) {
  16231. range.setStart(next, 0).collapse(true).select(true);
  16232. domUtils.remove(li);
  16233. } else {
  16234. if ((!pre && !next) || !pre) {
  16235. parentList.parentNode.insertBefore(p, parentList);
  16236. } else {
  16237. li.parentNode.parentNode.insertBefore(
  16238. p,
  16239. parentList.nextSibling
  16240. );
  16241. }
  16242. domUtils.remove(li);
  16243. if (!parentList.firstChild) {
  16244. domUtils.remove(parentList);
  16245. }
  16246. range.setStart(p, 0).setCursor();
  16247. }
  16248. preventAndSave();
  16249. return;
  16250. }
  16251. } else {
  16252. var tmpRange = range.cloneRange(),
  16253. bk = tmpRange.collapse(false).createBookmark();
  16254. range.deleteContents();
  16255. tmpRange.moveToBookmark(bk);
  16256. var li = domUtils.findParentByTagName(
  16257. tmpRange.startContainer,
  16258. "li",
  16259. true
  16260. );
  16261. clearEmptySibling(li);
  16262. tmpRange.select();
  16263. preventAndSave();
  16264. return;
  16265. }
  16266. }
  16267. li = domUtils.findParentByTagName(range.startContainer, "li", true);
  16268. if (li) {
  16269. if (domUtils.isEmptyBlock(li)) {
  16270. bk = range.createBookmark();
  16271. var parentList = li.parentNode;
  16272. if (li !== parentList.lastChild) {
  16273. domUtils.breakParent(li, parentList);
  16274. clearEmptySibling(li);
  16275. } else {
  16276. parentList.parentNode.insertBefore(li, parentList.nextSibling);
  16277. if (domUtils.isEmptyNode(parentList)) {
  16278. domUtils.remove(parentList);
  16279. }
  16280. }
  16281. //嵌套不处理
  16282. if (!dtd.$list[li.parentNode.tagName]) {
  16283. if (!domUtils.isBlockElm(li.firstChild)) {
  16284. p = me.document.createElement("p");
  16285. li.parentNode.insertBefore(p, li);
  16286. while (li.firstChild) {
  16287. p.appendChild(li.firstChild);
  16288. }
  16289. domUtils.remove(li);
  16290. } else {
  16291. domUtils.remove(li, true);
  16292. }
  16293. }
  16294. range.moveToBookmark(bk).select();
  16295. } else {
  16296. var first = li.firstChild;
  16297. if (!first || !domUtils.isBlockElm(first)) {
  16298. var p = me.document.createElement("p");
  16299. !li.firstChild && domUtils.fillNode(me.document, p);
  16300. while (li.firstChild) {
  16301. p.appendChild(li.firstChild);
  16302. }
  16303. li.appendChild(p);
  16304. first = p;
  16305. }
  16306. var span = me.document.createElement("span");
  16307. range.insertNode(span);
  16308. domUtils.breakParent(span, li);
  16309. var nextLi = span.nextSibling;
  16310. first = nextLi.firstChild;
  16311. if (!first) {
  16312. p = me.document.createElement("p");
  16313. domUtils.fillNode(me.document, p);
  16314. nextLi.appendChild(p);
  16315. first = p;
  16316. }
  16317. if (domUtils.isEmptyNode(first)) {
  16318. first.innerHTML = "";
  16319. domUtils.fillNode(me.document, first);
  16320. }
  16321. range.setStart(first, 0).collapse(true).shrinkBoundary().select();
  16322. domUtils.remove(span);
  16323. var pre = nextLi.previousSibling;
  16324. if (pre && domUtils.isEmptyBlock(pre)) {
  16325. pre.innerHTML = "<p></p>";
  16326. domUtils.fillNode(me.document, pre.firstChild);
  16327. }
  16328. }
  16329. // }
  16330. preventAndSave();
  16331. }
  16332. }
  16333. }
  16334. if (keyCode == 8) {
  16335. //修中ie中li下的问题
  16336. range = me.selection.getRange();
  16337. if (range.collapsed && domUtils.isStartInblock(range)) {
  16338. tmpRange = range.cloneRange().trimBoundary();
  16339. li = domUtils.findParentByTagName(range.startContainer, "li", true);
  16340. //要在li的最左边,才能处理
  16341. if (li && domUtils.isStartInblock(tmpRange)) {
  16342. start = domUtils.findParentByTagName(range.startContainer, "p", true);
  16343. if (start && start !== li.firstChild) {
  16344. var parentList = domUtils.findParentByTagName(start, ["ol", "ul"]);
  16345. domUtils.breakParent(start, parentList);
  16346. clearEmptySibling(start);
  16347. me.fireEvent("contentchange");
  16348. range.setStart(start, 0).setCursor(false, true);
  16349. me.fireEvent("saveScene");
  16350. domUtils.preventDefault(evt);
  16351. return;
  16352. }
  16353. if (li && (pre = li.previousSibling)) {
  16354. if (keyCode == 46 && li.childNodes.length) {
  16355. return;
  16356. }
  16357. //有可能上边的兄弟节点是个2级菜单,要追加到2级菜单的最后的li
  16358. if (dtd.$list[pre.tagName]) {
  16359. pre = pre.lastChild;
  16360. }
  16361. me.undoManger && me.undoManger.save();
  16362. first = li.firstChild;
  16363. if (domUtils.isBlockElm(first)) {
  16364. if (domUtils.isEmptyNode(first)) {
  16365. // range.setEnd(pre, pre.childNodes.length).shrinkBoundary().collapse().select(true);
  16366. pre.appendChild(first);
  16367. range.setStart(first, 0).setCursor(false, true);
  16368. //first不是唯一的节点
  16369. while (li.firstChild) {
  16370. pre.appendChild(li.firstChild);
  16371. }
  16372. } else {
  16373. span = me.document.createElement("span");
  16374. range.insertNode(span);
  16375. //判断pre是否是空的节点,如果是<p><br/></p>类型的空节点,干掉p标签防止它占位
  16376. if (domUtils.isEmptyBlock(pre)) {
  16377. pre.innerHTML = "";
  16378. }
  16379. domUtils.moveChild(li, pre);
  16380. range.setStartBefore(span).collapse(true).select(true);
  16381. domUtils.remove(span);
  16382. }
  16383. } else {
  16384. if (domUtils.isEmptyNode(li)) {
  16385. var p = me.document.createElement("p");
  16386. pre.appendChild(p);
  16387. range.setStart(p, 0).setCursor();
  16388. // range.setEnd(pre, pre.childNodes.length).shrinkBoundary().collapse().select(true);
  16389. } else {
  16390. range
  16391. .setEnd(pre, pre.childNodes.length)
  16392. .collapse()
  16393. .select(true);
  16394. while (li.firstChild) {
  16395. pre.appendChild(li.firstChild);
  16396. }
  16397. }
  16398. }
  16399. domUtils.remove(li);
  16400. me.fireEvent("contentchange");
  16401. me.fireEvent("saveScene");
  16402. domUtils.preventDefault(evt);
  16403. return;
  16404. }
  16405. //trace:980
  16406. if (li && !li.previousSibling) {
  16407. var parentList = li.parentNode;
  16408. var bk = range.createBookmark();
  16409. if (domUtils.isTagNode(parentList.parentNode, "ol ul")) {
  16410. parentList.parentNode.insertBefore(li, parentList);
  16411. if (domUtils.isEmptyNode(parentList)) {
  16412. domUtils.remove(parentList);
  16413. }
  16414. } else {
  16415. while (li.firstChild) {
  16416. parentList.parentNode.insertBefore(li.firstChild, parentList);
  16417. }
  16418. domUtils.remove(li);
  16419. if (domUtils.isEmptyNode(parentList)) {
  16420. domUtils.remove(parentList);
  16421. }
  16422. }
  16423. range.moveToBookmark(bk).setCursor(false, true);
  16424. me.fireEvent("contentchange");
  16425. me.fireEvent("saveScene");
  16426. domUtils.preventDefault(evt);
  16427. return;
  16428. }
  16429. }
  16430. }
  16431. }
  16432. });
  16433. me.addListener("keyup", function(type, evt) {
  16434. var keyCode = evt.keyCode || evt.which;
  16435. if (keyCode == 8) {
  16436. var rng = me.selection.getRange(),
  16437. list;
  16438. if (
  16439. (list = domUtils.findParentByTagName(
  16440. rng.startContainer,
  16441. ["ol", "ul"],
  16442. true
  16443. ))
  16444. ) {
  16445. adjustList(
  16446. list,
  16447. list.tagName.toLowerCase(),
  16448. getStyle(list) || domUtils.getComputedStyle(list, "list-style-type"),
  16449. true
  16450. );
  16451. }
  16452. }
  16453. });
  16454. //处理tab键
  16455. me.addListener("tabkeydown", function() {
  16456. var range = me.selection.getRange();
  16457. //控制级数
  16458. function checkLevel(li) {
  16459. if (me.options.maxListLevel != -1) {
  16460. var level = li.parentNode,
  16461. levelNum = 0;
  16462. while (/[ou]l/i.test(level.tagName)) {
  16463. levelNum++;
  16464. level = level.parentNode;
  16465. }
  16466. if (levelNum >= me.options.maxListLevel) {
  16467. return true;
  16468. }
  16469. }
  16470. }
  16471. //只以开始为准
  16472. //todo 后续改进
  16473. var li = domUtils.findParentByTagName(range.startContainer, "li", true);
  16474. if (li) {
  16475. var bk;
  16476. if (range.collapsed) {
  16477. if (checkLevel(li)) return true;
  16478. var parentLi = li.parentNode,
  16479. list = me.document.createElement(parentLi.tagName),
  16480. index = utils.indexOf(
  16481. listStyle[list.tagName],
  16482. getStyle(parentLi) ||
  16483. domUtils.getComputedStyle(parentLi, "list-style-type")
  16484. );
  16485. index = index + 1 == listStyle[list.tagName].length ? 0 : index + 1;
  16486. var currentStyle = listStyle[list.tagName][index];
  16487. setListStyle(list, currentStyle);
  16488. if (domUtils.isStartInblock(range)) {
  16489. me.fireEvent("saveScene");
  16490. bk = range.createBookmark();
  16491. parentLi.insertBefore(list, li);
  16492. list.appendChild(li);
  16493. adjustList(list, list.tagName.toLowerCase(), currentStyle);
  16494. me.fireEvent("contentchange");
  16495. range.moveToBookmark(bk).select(true);
  16496. return true;
  16497. }
  16498. } else {
  16499. me.fireEvent("saveScene");
  16500. bk = range.createBookmark();
  16501. for (
  16502. var i = 0, closeList, parents = domUtils.findParents(li), ci;
  16503. (ci = parents[i++]);
  16504. ) {
  16505. if (domUtils.isTagNode(ci, "ol ul")) {
  16506. closeList = ci;
  16507. break;
  16508. }
  16509. }
  16510. var current = li;
  16511. if (bk.end) {
  16512. while (
  16513. current &&
  16514. !(
  16515. domUtils.getPosition(current, bk.end) &
  16516. domUtils.POSITION_FOLLOWING
  16517. )
  16518. ) {
  16519. if (checkLevel(current)) {
  16520. current = domUtils.getNextDomNode(current, false, null, function(
  16521. node
  16522. ) {
  16523. return node !== closeList;
  16524. });
  16525. continue;
  16526. }
  16527. var parentLi = current.parentNode,
  16528. list = me.document.createElement(parentLi.tagName),
  16529. index = utils.indexOf(
  16530. listStyle[list.tagName],
  16531. getStyle(parentLi) ||
  16532. domUtils.getComputedStyle(parentLi, "list-style-type")
  16533. );
  16534. var currentIndex = index + 1 == listStyle[list.tagName].length
  16535. ? 0
  16536. : index + 1;
  16537. var currentStyle = listStyle[list.tagName][currentIndex];
  16538. setListStyle(list, currentStyle);
  16539. parentLi.insertBefore(list, current);
  16540. while (
  16541. current &&
  16542. !(
  16543. domUtils.getPosition(current, bk.end) &
  16544. domUtils.POSITION_FOLLOWING
  16545. )
  16546. ) {
  16547. li = current.nextSibling;
  16548. list.appendChild(current);
  16549. if (!li || domUtils.isTagNode(li, "ol ul")) {
  16550. if (li) {
  16551. while ((li = li.firstChild)) {
  16552. if (li.tagName == "LI") {
  16553. break;
  16554. }
  16555. }
  16556. } else {
  16557. li = domUtils.getNextDomNode(current, false, null, function(
  16558. node
  16559. ) {
  16560. return node !== closeList;
  16561. });
  16562. }
  16563. break;
  16564. }
  16565. current = li;
  16566. }
  16567. adjustList(list, list.tagName.toLowerCase(), currentStyle);
  16568. current = li;
  16569. }
  16570. }
  16571. me.fireEvent("contentchange");
  16572. range.moveToBookmark(bk).select();
  16573. return true;
  16574. }
  16575. }
  16576. });
  16577. function getLi(start) {
  16578. while (start && !domUtils.isBody(start)) {
  16579. if (start.nodeName == "TABLE") {
  16580. return null;
  16581. }
  16582. if (start.nodeName == "LI") {
  16583. return start;
  16584. }
  16585. start = start.parentNode;
  16586. }
  16587. }
  16588. /**
  16589. * 有序列表,与“insertunorderedlist”命令互斥
  16590. * @command insertorderedlist
  16591. * @method execCommand
  16592. * @param { String } command 命令字符串
  16593. * @param { String } style 插入的有序列表类型,值为:decimal,lower-alpha,lower-roman,upper-alpha,upper-roman,cn,cn1,cn2,num,num1,num2
  16594. * @example
  16595. * ```javascript
  16596. * editor.execCommand( 'insertorderedlist','decimal');
  16597. * ```
  16598. */
  16599. /**
  16600. * 查询当前选区内容是否有序列表
  16601. * @command insertorderedlist
  16602. * @method queryCommandState
  16603. * @param { String } cmd 命令字符串
  16604. * @return { int } 如果当前选区是有序列表返回1,否则返回0
  16605. * @example
  16606. * ```javascript
  16607. * editor.queryCommandState( 'insertorderedlist' );
  16608. * ```
  16609. */
  16610. /**
  16611. * 查询当前选区内容是否有序列表
  16612. * @command insertorderedlist
  16613. * @method queryCommandValue
  16614. * @param { String } cmd 命令字符串
  16615. * @return { String } 返回当前有序列表的类型,值为null或decimal,lower-alpha,lower-roman,upper-alpha,upper-roman,cn,cn1,cn2,num,num1,num2
  16616. * @example
  16617. * ```javascript
  16618. * editor.queryCommandValue( 'insertorderedlist' );
  16619. * ```
  16620. */
  16621. /**
  16622. * 无序列表,与“insertorderedlist”命令互斥
  16623. * @command insertunorderedlist
  16624. * @method execCommand
  16625. * @param { String } command 命令字符串
  16626. * @param { String } style 插入的无序列表类型,值为:circle,disc,square,dash,dot
  16627. * @example
  16628. * ```javascript
  16629. * editor.execCommand( 'insertunorderedlist','circle');
  16630. * ```
  16631. */
  16632. /**
  16633. * 查询当前是否有word文档粘贴进来的图片
  16634. * @command insertunorderedlist
  16635. * @method insertunorderedlist
  16636. * @param { String } command 命令字符串
  16637. * @return { int } 如果当前选区是无序列表返回1,否则返回0
  16638. * @example
  16639. * ```javascript
  16640. * editor.queryCommandState( 'insertunorderedlist' );
  16641. * ```
  16642. */
  16643. /**
  16644. * 查询当前选区内容是否有序列表
  16645. * @command insertunorderedlist
  16646. * @method queryCommandValue
  16647. * @param { String } command 命令字符串
  16648. * @return { String } 返回当前无序列表的类型,值为null或circle,disc,square,dash,dot
  16649. * @example
  16650. * ```javascript
  16651. * editor.queryCommandValue( 'insertunorderedlist' );
  16652. * ```
  16653. */
  16654. me.commands["insertorderedlist"] = me.commands["insertunorderedlist"] = {
  16655. execCommand: function(command, style) {
  16656. if (!style) {
  16657. style = command.toLowerCase() == "insertorderedlist"
  16658. ? "decimal"
  16659. : "disc";
  16660. }
  16661. var me = this,
  16662. range = this.selection.getRange(),
  16663. filterFn = function(node) {
  16664. return node.nodeType == 1
  16665. ? node.tagName.toLowerCase() != "br"
  16666. : !domUtils.isWhitespace(node);
  16667. },
  16668. tag = command.toLowerCase() == "insertorderedlist" ? "ol" : "ul",
  16669. frag = me.document.createDocumentFragment();
  16670. //去掉是因为会出现选到末尾,导致adjustmentBoundary缩到ol/ul的位置
  16671. //range.shrinkBoundary();//.adjustmentBoundary();
  16672. range.adjustmentBoundary().shrinkBoundary();
  16673. var bko = range.createBookmark(true),
  16674. start = getLi(me.document.getElementById(bko.start)),
  16675. modifyStart = 0,
  16676. end = getLi(me.document.getElementById(bko.end)),
  16677. modifyEnd = 0,
  16678. startParent,
  16679. endParent,
  16680. list,
  16681. tmp;
  16682. if (start || end) {
  16683. start && (startParent = start.parentNode);
  16684. if (!bko.end) {
  16685. end = start;
  16686. }
  16687. end && (endParent = end.parentNode);
  16688. if (startParent === endParent) {
  16689. while (start !== end) {
  16690. tmp = start;
  16691. start = start.nextSibling;
  16692. if (!domUtils.isBlockElm(tmp.firstChild)) {
  16693. var p = me.document.createElement("p");
  16694. while (tmp.firstChild) {
  16695. p.appendChild(tmp.firstChild);
  16696. }
  16697. tmp.appendChild(p);
  16698. }
  16699. frag.appendChild(tmp);
  16700. }
  16701. tmp = me.document.createElement("span");
  16702. startParent.insertBefore(tmp, end);
  16703. if (!domUtils.isBlockElm(end.firstChild)) {
  16704. p = me.document.createElement("p");
  16705. while (end.firstChild) {
  16706. p.appendChild(end.firstChild);
  16707. }
  16708. end.appendChild(p);
  16709. }
  16710. frag.appendChild(end);
  16711. domUtils.breakParent(tmp, startParent);
  16712. if (domUtils.isEmptyNode(tmp.previousSibling)) {
  16713. domUtils.remove(tmp.previousSibling);
  16714. }
  16715. if (domUtils.isEmptyNode(tmp.nextSibling)) {
  16716. domUtils.remove(tmp.nextSibling);
  16717. }
  16718. var nodeStyle =
  16719. getStyle(startParent) ||
  16720. domUtils.getComputedStyle(startParent, "list-style-type") ||
  16721. (command.toLowerCase() == "insertorderedlist" ? "decimal" : "disc");
  16722. if (startParent.tagName.toLowerCase() == tag && nodeStyle == style) {
  16723. for (
  16724. var i = 0, ci, tmpFrag = me.document.createDocumentFragment();
  16725. (ci = frag.firstChild);
  16726. ) {
  16727. if (domUtils.isTagNode(ci, "ol ul")) {
  16728. // 删除时,子列表不处理
  16729. // utils.each(domUtils.getElementsByTagName(ci,'li'),function(li){
  16730. // while(li.firstChild){
  16731. // tmpFrag.appendChild(li.firstChild);
  16732. // }
  16733. //
  16734. // });
  16735. tmpFrag.appendChild(ci);
  16736. } else {
  16737. while (ci.firstChild) {
  16738. tmpFrag.appendChild(ci.firstChild);
  16739. domUtils.remove(ci);
  16740. }
  16741. }
  16742. }
  16743. tmp.parentNode.insertBefore(tmpFrag, tmp);
  16744. } else {
  16745. list = me.document.createElement(tag);
  16746. setListStyle(list, style);
  16747. list.appendChild(frag);
  16748. tmp.parentNode.insertBefore(list, tmp);
  16749. }
  16750. domUtils.remove(tmp);
  16751. list && adjustList(list, tag, style);
  16752. range.moveToBookmark(bko).select();
  16753. return;
  16754. }
  16755. //开始
  16756. if (start) {
  16757. while (start) {
  16758. tmp = start.nextSibling;
  16759. if (domUtils.isTagNode(start, "ol ul")) {
  16760. frag.appendChild(start);
  16761. } else {
  16762. var tmpfrag = me.document.createDocumentFragment(),
  16763. hasBlock = 0;
  16764. while (start.firstChild) {
  16765. if (domUtils.isBlockElm(start.firstChild)) {
  16766. hasBlock = 1;
  16767. }
  16768. tmpfrag.appendChild(start.firstChild);
  16769. }
  16770. if (!hasBlock) {
  16771. var tmpP = me.document.createElement("p");
  16772. tmpP.appendChild(tmpfrag);
  16773. frag.appendChild(tmpP);
  16774. } else {
  16775. frag.appendChild(tmpfrag);
  16776. }
  16777. domUtils.remove(start);
  16778. }
  16779. start = tmp;
  16780. }
  16781. startParent.parentNode.insertBefore(frag, startParent.nextSibling);
  16782. if (domUtils.isEmptyNode(startParent)) {
  16783. range.setStartBefore(startParent);
  16784. domUtils.remove(startParent);
  16785. } else {
  16786. range.setStartAfter(startParent);
  16787. }
  16788. modifyStart = 1;
  16789. }
  16790. if (end && domUtils.inDoc(endParent, me.document)) {
  16791. //结束
  16792. start = endParent.firstChild;
  16793. while (start && start !== end) {
  16794. tmp = start.nextSibling;
  16795. if (domUtils.isTagNode(start, "ol ul")) {
  16796. frag.appendChild(start);
  16797. } else {
  16798. tmpfrag = me.document.createDocumentFragment();
  16799. hasBlock = 0;
  16800. while (start.firstChild) {
  16801. if (domUtils.isBlockElm(start.firstChild)) {
  16802. hasBlock = 1;
  16803. }
  16804. tmpfrag.appendChild(start.firstChild);
  16805. }
  16806. if (!hasBlock) {
  16807. tmpP = me.document.createElement("p");
  16808. tmpP.appendChild(tmpfrag);
  16809. frag.appendChild(tmpP);
  16810. } else {
  16811. frag.appendChild(tmpfrag);
  16812. }
  16813. domUtils.remove(start);
  16814. }
  16815. start = tmp;
  16816. }
  16817. var tmpDiv = domUtils.createElement(me.document, "div", {
  16818. tmpDiv: 1
  16819. });
  16820. domUtils.moveChild(end, tmpDiv);
  16821. frag.appendChild(tmpDiv);
  16822. domUtils.remove(end);
  16823. endParent.parentNode.insertBefore(frag, endParent);
  16824. range.setEndBefore(endParent);
  16825. if (domUtils.isEmptyNode(endParent)) {
  16826. domUtils.remove(endParent);
  16827. }
  16828. modifyEnd = 1;
  16829. }
  16830. }
  16831. if (!modifyStart) {
  16832. range.setStartBefore(me.document.getElementById(bko.start));
  16833. }
  16834. if (bko.end && !modifyEnd) {
  16835. range.setEndAfter(me.document.getElementById(bko.end));
  16836. }
  16837. range.enlarge(true, function(node) {
  16838. return notExchange[node.tagName];
  16839. });
  16840. frag = me.document.createDocumentFragment();
  16841. var bk = range.createBookmark(),
  16842. current = domUtils.getNextDomNode(bk.start, false, filterFn),
  16843. tmpRange = range.cloneRange(),
  16844. tmpNode,
  16845. block = domUtils.isBlockElm;
  16846. while (
  16847. current &&
  16848. current !== bk.end &&
  16849. domUtils.getPosition(current, bk.end) & domUtils.POSITION_PRECEDING
  16850. ) {
  16851. if (current.nodeType == 3 || dtd.li[current.tagName]) {
  16852. if (current.nodeType == 1 && dtd.$list[current.tagName]) {
  16853. while (current.firstChild) {
  16854. frag.appendChild(current.firstChild);
  16855. }
  16856. tmpNode = domUtils.getNextDomNode(current, false, filterFn);
  16857. domUtils.remove(current);
  16858. current = tmpNode;
  16859. continue;
  16860. }
  16861. tmpNode = current;
  16862. tmpRange.setStartBefore(current);
  16863. while (
  16864. current &&
  16865. current !== bk.end &&
  16866. (!block(current) || domUtils.isBookmarkNode(current))
  16867. ) {
  16868. tmpNode = current;
  16869. current = domUtils.getNextDomNode(current, false, null, function(
  16870. node
  16871. ) {
  16872. return !notExchange[node.tagName];
  16873. });
  16874. }
  16875. if (current && block(current)) {
  16876. tmp = domUtils.getNextDomNode(tmpNode, false, filterFn);
  16877. if (tmp && domUtils.isBookmarkNode(tmp)) {
  16878. current = domUtils.getNextDomNode(tmp, false, filterFn);
  16879. tmpNode = tmp;
  16880. }
  16881. }
  16882. tmpRange.setEndAfter(tmpNode);
  16883. current = domUtils.getNextDomNode(tmpNode, false, filterFn);
  16884. var li = range.document.createElement("li");
  16885. li.appendChild(tmpRange.extractContents());
  16886. if (domUtils.isEmptyNode(li)) {
  16887. var tmpNode = range.document.createElement("p");
  16888. while (li.firstChild) {
  16889. tmpNode.appendChild(li.firstChild);
  16890. }
  16891. li.appendChild(tmpNode);
  16892. }
  16893. frag.appendChild(li);
  16894. } else {
  16895. current = domUtils.getNextDomNode(current, true, filterFn);
  16896. }
  16897. }
  16898. range.moveToBookmark(bk).collapse(true);
  16899. list = me.document.createElement(tag);
  16900. setListStyle(list, style);
  16901. list.appendChild(frag);
  16902. range.insertNode(list);
  16903. //当前list上下看能否合并
  16904. adjustList(list, tag, style);
  16905. //去掉冗余的tmpDiv
  16906. for (
  16907. var i = 0, ci, tmpDivs = domUtils.getElementsByTagName(list, "div");
  16908. (ci = tmpDivs[i++]);
  16909. ) {
  16910. if (ci.getAttribute("tmpDiv")) {
  16911. domUtils.remove(ci, true);
  16912. }
  16913. }
  16914. range.moveToBookmark(bko).select();
  16915. },
  16916. queryCommandState: function(command) {
  16917. var tag = command.toLowerCase() == "insertorderedlist" ? "ol" : "ul";
  16918. var path = this.selection.getStartElementPath();
  16919. for (var i = 0, ci; (ci = path[i++]); ) {
  16920. if (ci.nodeName == "TABLE") {
  16921. return 0;
  16922. }
  16923. if (tag == ci.nodeName.toLowerCase()) {
  16924. return 1;
  16925. }
  16926. }
  16927. return 0;
  16928. },
  16929. queryCommandValue: function(command) {
  16930. var tag = command.toLowerCase() == "insertorderedlist" ? "ol" : "ul";
  16931. var path = this.selection.getStartElementPath(),
  16932. node;
  16933. for (var i = 0, ci; (ci = path[i++]); ) {
  16934. if (ci.nodeName == "TABLE") {
  16935. node = null;
  16936. break;
  16937. }
  16938. if (tag == ci.nodeName.toLowerCase()) {
  16939. node = ci;
  16940. break;
  16941. }
  16942. }
  16943. return node
  16944. ? getStyle(node) || domUtils.getComputedStyle(node, "list-style-type")
  16945. : null;
  16946. }
  16947. };
  16948. };
  16949. // plugins/source.js
  16950. /**
  16951. * 源码编辑插件
  16952. * @file
  16953. * @since 1.2.6.1
  16954. */
  16955. (function() {
  16956. var sourceEditors = {
  16957. textarea: function(editor, holder) {
  16958. var textarea = holder.ownerDocument.createElement("textarea");
  16959. textarea.style.cssText =
  16960. "position:absolute;resize:none;width:100%;height:100%;border:0;padding:0;margin:0;overflow-y:auto;";
  16961. // todo: IE下只有onresize属性可用... 很纠结
  16962. if (browser.ie && browser.version < 8) {
  16963. textarea.style.width = holder.offsetWidth + "px";
  16964. textarea.style.height = holder.offsetHeight + "px";
  16965. holder.onresize = function() {
  16966. textarea.style.width = holder.offsetWidth + "px";
  16967. textarea.style.height = holder.offsetHeight + "px";
  16968. };
  16969. }
  16970. holder.appendChild(textarea);
  16971. return {
  16972. setContent: function(content) {
  16973. textarea.value = content;
  16974. },
  16975. getContent: function() {
  16976. return textarea.value;
  16977. },
  16978. select: function() {
  16979. var range;
  16980. if (browser.ie) {
  16981. range = textarea.createTextRange();
  16982. range.collapse(true);
  16983. range.select();
  16984. } else {
  16985. //todo: chrome下无法设置焦点
  16986. textarea.setSelectionRange(0, 0);
  16987. textarea.focus();
  16988. }
  16989. },
  16990. dispose: function() {
  16991. holder.removeChild(textarea);
  16992. // todo
  16993. holder.onresize = null;
  16994. textarea = null;
  16995. holder = null;
  16996. },
  16997. focus: function (){
  16998. textarea.focus();
  16999. },
  17000. blur: function (){
  17001. textarea.blur();
  17002. }
  17003. };
  17004. },
  17005. codemirror: function(editor, holder) {
  17006. var codeEditor = window.CodeMirror(holder, {
  17007. mode: "text/html",
  17008. tabMode: "indent",
  17009. lineNumbers: true,
  17010. lineWrapping: true
  17011. });
  17012. var dom = codeEditor.getWrapperElement();
  17013. dom.style.cssText =
  17014. 'position:absolute;left:0;top:0;width:100%;height:100%;font-family:consolas,"Courier new",monospace;font-size:13px;';
  17015. codeEditor.getScrollerElement().style.cssText =
  17016. "position:absolute;left:0;top:0;width:100%;height:100%;";
  17017. codeEditor.refresh();
  17018. return {
  17019. getCodeMirror: function() {
  17020. return codeEditor;
  17021. },
  17022. setContent: function(content) {
  17023. codeEditor.setValue(content);
  17024. },
  17025. getContent: function() {
  17026. return codeEditor.getValue();
  17027. },
  17028. select: function() {
  17029. codeEditor.focus();
  17030. },
  17031. dispose: function() {
  17032. holder.removeChild(dom);
  17033. dom = null;
  17034. codeEditor = null;
  17035. },
  17036. focus: function (){
  17037. codeEditor.focus();
  17038. },
  17039. blur: function (){
  17040. // codeEditor.blur();
  17041. // since codemirror not support blur()
  17042. codeEditor.setOption('readOnly', true);
  17043. codeEditor.setOption('readOnly', false);
  17044. }
  17045. };
  17046. }
  17047. };
  17048. UE.plugins["source"] = function() {
  17049. var me = this;
  17050. var opt = this.options;
  17051. var sourceMode = false;
  17052. var sourceEditor;
  17053. var orgSetContent;
  17054. var orgFocus;
  17055. var orgBlur;
  17056. opt.sourceEditor = browser.ie
  17057. ? "textarea"
  17058. : opt.sourceEditor || "codemirror";
  17059. me.setOpt({
  17060. sourceEditorFirst: false
  17061. });
  17062. function createSourceEditor(holder) {
  17063. return sourceEditors[
  17064. opt.sourceEditor == "codemirror" && window.CodeMirror
  17065. ? "codemirror"
  17066. : "textarea"
  17067. ](me, holder);
  17068. }
  17069. var bakCssText;
  17070. //解决在源码模式下getContent不能得到最新的内容问题
  17071. var oldGetContent, bakAddress;
  17072. /**
  17073. * 切换源码模式和编辑模式
  17074. * @command source
  17075. * @method execCommand
  17076. * @param { String } cmd 命令字符串
  17077. * @example
  17078. * ```javascript
  17079. * editor.execCommand( 'source');
  17080. * ```
  17081. */
  17082. /**
  17083. * 查询当前编辑区域的状态是源码模式还是可视化模式
  17084. * @command source
  17085. * @method queryCommandState
  17086. * @param { String } cmd 命令字符串
  17087. * @return { int } 如果当前是源码编辑模式,返回1,否则返回0
  17088. * @example
  17089. * ```javascript
  17090. * editor.queryCommandState( 'source' );
  17091. * ```
  17092. */
  17093. me.commands["source"] = {
  17094. execCommand: function() {
  17095. sourceMode = !sourceMode;
  17096. if (sourceMode) {
  17097. bakAddress = me.selection.getRange().createAddress(false, true);
  17098. me.undoManger && me.undoManger.save(true);
  17099. if (browser.gecko) {
  17100. me.body.contentEditable = false;
  17101. }
  17102. bakCssText = me.iframe.style.cssText;
  17103. me.iframe.style.cssText +=
  17104. "position:absolute;left:-32768px;top:-32768px;";
  17105. me.fireEvent("beforegetcontent");
  17106. var root = UE.htmlparser(me.body.innerHTML);
  17107. me.filterOutputRule(root);
  17108. root.traversal(function(node) {
  17109. if (node.type == "element") {
  17110. switch (node.tagName) {
  17111. case "td":
  17112. case "th":
  17113. case "caption":
  17114. if (node.children && node.children.length == 1) {
  17115. if (node.firstChild().tagName == "br") {
  17116. node.removeChild(node.firstChild());
  17117. }
  17118. }
  17119. break;
  17120. case "pre":
  17121. node.innerText(node.innerText().replace(/&nbsp;/g, " "));
  17122. }
  17123. }
  17124. });
  17125. me.fireEvent("aftergetcontent");
  17126. var content = root.toHtml(true);
  17127. sourceEditor = createSourceEditor(me.iframe.parentNode);
  17128. sourceEditor.setContent(content);
  17129. orgSetContent = me.setContent;
  17130. me.setContent = function(html) {
  17131. //这里暂时不触发事件,防止报错
  17132. var root = UE.htmlparser(html);
  17133. me.filterInputRule(root);
  17134. html = root.toHtml();
  17135. sourceEditor.setContent(html);
  17136. };
  17137. setTimeout(function() {
  17138. sourceEditor.select();
  17139. me.addListener("fullscreenchanged", function() {
  17140. try {
  17141. sourceEditor.getCodeMirror().refresh();
  17142. } catch (e) {}
  17143. });
  17144. });
  17145. //重置getContent,源码模式下取值也能是最新的数据
  17146. oldGetContent = me.getContent;
  17147. me.getContent = function() {
  17148. return (
  17149. sourceEditor.getContent() ||
  17150. "<p>" + (browser.ie ? "" : "<br/>") + "</p>"
  17151. );
  17152. };
  17153. orgFocus = me.focus;
  17154. orgBlur = me.blur;
  17155. me.focus = function(){
  17156. sourceEditor.focus();
  17157. };
  17158. me.blur = function(){
  17159. orgBlur.call(me);
  17160. sourceEditor.blur();
  17161. };
  17162. } else {
  17163. me.iframe.style.cssText = bakCssText;
  17164. var cont =
  17165. sourceEditor.getContent() ||
  17166. "<p>" + (browser.ie ? "" : "<br/>") + "</p>";
  17167. //处理掉block节点前后的空格,有可能会误命中,暂时不考虑
  17168. cont = cont.replace(
  17169. new RegExp("[\\r\\t\\n ]*</?(\\w+)\\s*(?:[^>]*)>", "g"),
  17170. function(a, b) {
  17171. if (b && !dtd.$inlineWithA[b.toLowerCase()]) {
  17172. return a.replace(/(^[\n\r\t ]*)|([\n\r\t ]*$)/g, "");
  17173. }
  17174. return a.replace(/(^[\n\r\t]*)|([\n\r\t]*$)/g, "");
  17175. }
  17176. );
  17177. me.setContent = orgSetContent;
  17178. me.setContent(cont);
  17179. sourceEditor.dispose();
  17180. sourceEditor = null;
  17181. //还原getContent方法
  17182. me.getContent = oldGetContent;
  17183. me.focus = orgFocus;
  17184. me.blur = orgBlur;
  17185. var first = me.body.firstChild;
  17186. //trace:1106 都删除空了,下边会报错,所以补充一个p占位
  17187. if (!first) {
  17188. me.body.innerHTML = "<p>" + (browser.ie ? "" : "<br/>") + "</p>";
  17189. first = me.body.firstChild;
  17190. }
  17191. //要在ifm为显示时ff才能取到selection,否则报错
  17192. //这里不能比较位置了
  17193. me.undoManger && me.undoManger.save(true);
  17194. if (browser.gecko) {
  17195. var input = document.createElement("input");
  17196. input.style.cssText = "position:absolute;left:0;top:-32768px";
  17197. document.body.appendChild(input);
  17198. me.body.contentEditable = false;
  17199. setTimeout(function() {
  17200. domUtils.setViewportOffset(input, { left: -32768, top: 0 });
  17201. input.focus();
  17202. setTimeout(function() {
  17203. me.body.contentEditable = true;
  17204. me.selection.getRange().moveToAddress(bakAddress).select(true);
  17205. domUtils.remove(input);
  17206. });
  17207. });
  17208. } else {
  17209. //ie下有可能报错,比如在代码顶头的情况
  17210. try {
  17211. me.selection.getRange().moveToAddress(bakAddress).select(true);
  17212. } catch (e) {}
  17213. }
  17214. }
  17215. this.fireEvent("sourcemodechanged", sourceMode);
  17216. },
  17217. queryCommandState: function() {
  17218. return sourceMode | 0;
  17219. },
  17220. notNeedUndo: 1
  17221. };
  17222. var oldQueryCommandState = me.queryCommandState;
  17223. me.queryCommandState = function(cmdName) {
  17224. cmdName = cmdName.toLowerCase();
  17225. if (sourceMode) {
  17226. //源码模式下可以开启的命令
  17227. return cmdName in
  17228. {
  17229. source: 1,
  17230. fullscreen: 1
  17231. }
  17232. ? 1
  17233. : -1;
  17234. }
  17235. return oldQueryCommandState.apply(this, arguments);
  17236. };
  17237. if (opt.sourceEditor == "codemirror") {
  17238. me.addListener("ready", function() {
  17239. utils.loadFile(
  17240. document,
  17241. {
  17242. src:
  17243. opt.codeMirrorJsUrl ||
  17244. opt.UEDITOR_HOME_URL + "third-party/codemirror/codemirror.js",
  17245. tag: "script",
  17246. type: "text/javascript",
  17247. defer: "defer"
  17248. },
  17249. function() {
  17250. if (opt.sourceEditorFirst) {
  17251. setTimeout(function() {
  17252. me.execCommand("source");
  17253. }, 0);
  17254. }
  17255. }
  17256. );
  17257. utils.loadFile(document, {
  17258. tag: "link",
  17259. rel: "stylesheet",
  17260. type: "text/css",
  17261. href:
  17262. opt.codeMirrorCssUrl ||
  17263. opt.UEDITOR_HOME_URL + "third-party/codemirror/codemirror.css"
  17264. });
  17265. });
  17266. }
  17267. };
  17268. })();
  17269. // plugins/enterkey.js
  17270. ///import core
  17271. ///import plugins/undo.js
  17272. ///commands 设置回车标签p或br
  17273. ///commandsName EnterKey
  17274. ///commandsTitle 设置回车标签p或br
  17275. /**
  17276. * @description 处理回车
  17277. * @author zhanyi
  17278. */
  17279. UE.plugins["enterkey"] = function() {
  17280. var hTag,
  17281. me = this,
  17282. tag = me.options.enterTag;
  17283. me.addListener("keyup", function(type, evt) {
  17284. var keyCode = evt.keyCode || evt.which;
  17285. if (keyCode == 13) {
  17286. var range = me.selection.getRange(),
  17287. start = range.startContainer,
  17288. doSave;
  17289. //修正在h1-h6里边回车后不能嵌套p的问题
  17290. if (!browser.ie) {
  17291. if (/h\d/i.test(hTag)) {
  17292. if (browser.gecko) {
  17293. var h = domUtils.findParentByTagName(
  17294. start,
  17295. [
  17296. "h1",
  17297. "h2",
  17298. "h3",
  17299. "h4",
  17300. "h5",
  17301. "h6",
  17302. "blockquote",
  17303. "caption",
  17304. "table"
  17305. ],
  17306. true
  17307. );
  17308. if (!h) {
  17309. me.document.execCommand("formatBlock", false, "<p>");
  17310. doSave = 1;
  17311. }
  17312. } else {
  17313. //chrome remove div
  17314. if (start.nodeType == 1) {
  17315. var tmp = me.document.createTextNode(""),
  17316. div;
  17317. range.insertNode(tmp);
  17318. div = domUtils.findParentByTagName(tmp, "div", true);
  17319. if (div) {
  17320. var p = me.document.createElement("p");
  17321. while (div.firstChild) {
  17322. p.appendChild(div.firstChild);
  17323. }
  17324. div.parentNode.insertBefore(p, div);
  17325. domUtils.remove(div);
  17326. range.setStartBefore(tmp).setCursor();
  17327. doSave = 1;
  17328. }
  17329. domUtils.remove(tmp);
  17330. }
  17331. }
  17332. if (me.undoManger && doSave) {
  17333. me.undoManger.save();
  17334. }
  17335. }
  17336. //没有站位符,会出现多行的问题
  17337. browser.opera && range.select();
  17338. } else {
  17339. me.fireEvent("saveScene", true, true);
  17340. }
  17341. }
  17342. });
  17343. me.addListener("keydown", function(type, evt) {
  17344. var keyCode = evt.keyCode || evt.which;
  17345. if (keyCode == 13) {
  17346. //回车
  17347. if (me.fireEvent("beforeenterkeydown")) {
  17348. domUtils.preventDefault(evt);
  17349. return;
  17350. }
  17351. me.fireEvent("saveScene", true, true);
  17352. hTag = "";
  17353. var range = me.selection.getRange();
  17354. if (!range.collapsed) {
  17355. //跨td不能删
  17356. var start = range.startContainer,
  17357. end = range.endContainer,
  17358. startTd = domUtils.findParentByTagName(start, "td", true),
  17359. endTd = domUtils.findParentByTagName(end, "td", true);
  17360. if (
  17361. (startTd && endTd && startTd !== endTd) ||
  17362. (!startTd && endTd) ||
  17363. (startTd && !endTd)
  17364. ) {
  17365. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
  17366. return;
  17367. }
  17368. }
  17369. if (tag == "p") {
  17370. if (!browser.ie) {
  17371. start = domUtils.findParentByTagName(
  17372. range.startContainer,
  17373. [
  17374. "ol",
  17375. "ul",
  17376. "p",
  17377. "h1",
  17378. "h2",
  17379. "h3",
  17380. "h4",
  17381. "h5",
  17382. "h6",
  17383. "blockquote",
  17384. "caption"
  17385. ],
  17386. true
  17387. );
  17388. //opera下执行formatblock会在table的场景下有问题,回车在opera原生支持很好,所以暂时在opera去掉调用这个原生的command
  17389. //trace:2431
  17390. if (!start && !browser.opera) {
  17391. me.document.execCommand("formatBlock", false, "<p>");
  17392. if (browser.gecko) {
  17393. range = me.selection.getRange();
  17394. start = domUtils.findParentByTagName(
  17395. range.startContainer,
  17396. "p",
  17397. true
  17398. );
  17399. start && domUtils.removeDirtyAttr(start);
  17400. }
  17401. } else {
  17402. hTag = start.tagName;
  17403. start.tagName.toLowerCase() == "p" &&
  17404. browser.gecko &&
  17405. domUtils.removeDirtyAttr(start);
  17406. }
  17407. }
  17408. } else {
  17409. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
  17410. if (!range.collapsed) {
  17411. range.deleteContents();
  17412. start = range.startContainer;
  17413. if (
  17414. start.nodeType == 1 &&
  17415. (start = start.childNodes[range.startOffset])
  17416. ) {
  17417. while (start.nodeType == 1) {
  17418. if (dtd.$empty[start.tagName]) {
  17419. range.setStartBefore(start).setCursor();
  17420. if (me.undoManger) {
  17421. me.undoManger.save();
  17422. }
  17423. return false;
  17424. }
  17425. if (!start.firstChild) {
  17426. var br = range.document.createElement("br");
  17427. start.appendChild(br);
  17428. range.setStart(start, 0).setCursor();
  17429. if (me.undoManger) {
  17430. me.undoManger.save();
  17431. }
  17432. return false;
  17433. }
  17434. start = start.firstChild;
  17435. }
  17436. if (start === range.startContainer.childNodes[range.startOffset]) {
  17437. br = range.document.createElement("br");
  17438. range.insertNode(br).setCursor();
  17439. } else {
  17440. range.setStart(start, 0).setCursor();
  17441. }
  17442. } else {
  17443. br = range.document.createElement("br");
  17444. range.insertNode(br).setStartAfter(br).setCursor();
  17445. }
  17446. } else {
  17447. br = range.document.createElement("br");
  17448. range.insertNode(br);
  17449. var parent = br.parentNode;
  17450. if (parent.lastChild === br) {
  17451. br.parentNode.insertBefore(br.cloneNode(true), br);
  17452. range.setStartBefore(br);
  17453. } else {
  17454. range.setStartAfter(br);
  17455. }
  17456. range.setCursor();
  17457. }
  17458. }
  17459. }
  17460. });
  17461. };
  17462. // plugins/keystrokes.js
  17463. /* 处理特殊键的兼容性问题 */
  17464. UE.plugins["keystrokes"] = function() {
  17465. var me = this;
  17466. var collapsed = true;
  17467. me.addListener("keydown", function(type, evt) {
  17468. var keyCode = evt.keyCode || evt.which,
  17469. rng = me.selection.getRange();
  17470. //处理全选的情况
  17471. if (
  17472. !rng.collapsed &&
  17473. !(evt.ctrlKey || evt.shiftKey || evt.altKey || evt.metaKey) &&
  17474. ((keyCode >= 65 && keyCode <= 90) ||
  17475. (keyCode >= 48 && keyCode <= 57) ||
  17476. (keyCode >= 96 && keyCode <= 111) ||
  17477. {
  17478. 13: 1,
  17479. 8: 1,
  17480. 46: 1
  17481. }[keyCode])
  17482. ) {
  17483. var tmpNode = rng.startContainer;
  17484. if (domUtils.isFillChar(tmpNode)) {
  17485. rng.setStartBefore(tmpNode);
  17486. }
  17487. tmpNode = rng.endContainer;
  17488. if (domUtils.isFillChar(tmpNode)) {
  17489. rng.setEndAfter(tmpNode);
  17490. }
  17491. rng.txtToElmBoundary();
  17492. //结束边界可能放到了br的前边,要把br包含进来
  17493. // x[xxx]<br/>
  17494. if (rng.endContainer && rng.endContainer.nodeType == 1) {
  17495. tmpNode = rng.endContainer.childNodes[rng.endOffset];
  17496. if (tmpNode && domUtils.isBr(tmpNode)) {
  17497. rng.setEndAfter(tmpNode);
  17498. }
  17499. }
  17500. if (rng.startOffset == 0) {
  17501. tmpNode = rng.startContainer;
  17502. if (domUtils.isBoundaryNode(tmpNode, "firstChild")) {
  17503. tmpNode = rng.endContainer;
  17504. if (
  17505. rng.endOffset ==
  17506. (tmpNode.nodeType == 3
  17507. ? tmpNode.nodeValue.length
  17508. : tmpNode.childNodes.length) &&
  17509. domUtils.isBoundaryNode(tmpNode, "lastChild")
  17510. ) {
  17511. me.fireEvent("saveScene");
  17512. me.body.innerHTML = "<p>" + (browser.ie ? "" : "<br/>") + "</p>";
  17513. rng.setStart(me.body.firstChild, 0).setCursor(false, true);
  17514. me._selectionChange();
  17515. return;
  17516. }
  17517. }
  17518. }
  17519. }
  17520. //处理backspace
  17521. if (keyCode == keymap.Backspace) {
  17522. rng = me.selection.getRange();
  17523. collapsed = rng.collapsed;
  17524. if (me.fireEvent("delkeydown", evt)) {
  17525. return;
  17526. }
  17527. var start, end;
  17528. //避免按两次删除才能生效的问题
  17529. if (rng.collapsed && rng.inFillChar()) {
  17530. start = rng.startContainer;
  17531. if (domUtils.isFillChar(start)) {
  17532. rng.setStartBefore(start).shrinkBoundary(true).collapse(true);
  17533. domUtils.remove(start);
  17534. } else {
  17535. start.nodeValue = start.nodeValue.replace(
  17536. new RegExp("^" + domUtils.fillChar),
  17537. ""
  17538. );
  17539. rng.startOffset--;
  17540. rng.collapse(true).select(true);
  17541. }
  17542. }
  17543. //解决选中control元素不能删除的问题
  17544. if ((start = rng.getClosedNode())) {
  17545. me.fireEvent("saveScene");
  17546. rng.setStartBefore(start);
  17547. domUtils.remove(start);
  17548. rng.setCursor();
  17549. me.fireEvent("saveScene");
  17550. domUtils.preventDefault(evt);
  17551. return;
  17552. }
  17553. //阻止在table上的删除
  17554. if (!browser.ie) {
  17555. start = domUtils.findParentByTagName(rng.startContainer, "table", true);
  17556. end = domUtils.findParentByTagName(rng.endContainer, "table", true);
  17557. if ((start && !end) || (!start && end) || start !== end) {
  17558. evt.preventDefault();
  17559. return;
  17560. }
  17561. }
  17562. }
  17563. //处理tab键的逻辑
  17564. if (keyCode == keymap.Tab) {
  17565. //不处理以下标签
  17566. var excludeTagNameForTabKey = {
  17567. ol: 1,
  17568. ul: 1,
  17569. table: 1
  17570. };
  17571. //处理组件里的tab按下事件
  17572. if (me.fireEvent("tabkeydown", evt)) {
  17573. domUtils.preventDefault(evt);
  17574. return;
  17575. }
  17576. var range = me.selection.getRange();
  17577. me.fireEvent("saveScene");
  17578. for (
  17579. var i = 0,
  17580. txt = "",
  17581. tabSize = me.options.tabSize || 4,
  17582. tabNode = me.options.tabNode || "&nbsp;";
  17583. i < tabSize;
  17584. i++
  17585. ) {
  17586. txt += tabNode;
  17587. }
  17588. var span = me.document.createElement("span");
  17589. span.innerHTML = txt + domUtils.fillChar;
  17590. if (range.collapsed) {
  17591. range.insertNode(span.cloneNode(true).firstChild).setCursor(true);
  17592. } else {
  17593. var filterFn = function(node) {
  17594. return (
  17595. domUtils.isBlockElm(node) &&
  17596. !excludeTagNameForTabKey[node.tagName.toLowerCase()]
  17597. );
  17598. };
  17599. //普通的情况
  17600. start = domUtils.findParent(range.startContainer, filterFn, true);
  17601. end = domUtils.findParent(range.endContainer, filterFn, true);
  17602. if (start && end && start === end) {
  17603. range.deleteContents();
  17604. range.insertNode(span.cloneNode(true).firstChild).setCursor(true);
  17605. } else {
  17606. var bookmark = range.createBookmark();
  17607. range.enlarge(true);
  17608. var bookmark2 = range.createBookmark(),
  17609. current = domUtils.getNextDomNode(bookmark2.start, false, filterFn);
  17610. while (
  17611. current &&
  17612. !(
  17613. domUtils.getPosition(current, bookmark2.end) &
  17614. domUtils.POSITION_FOLLOWING
  17615. )
  17616. ) {
  17617. current.insertBefore(
  17618. span.cloneNode(true).firstChild,
  17619. current.firstChild
  17620. );
  17621. current = domUtils.getNextDomNode(current, false, filterFn);
  17622. }
  17623. range.moveToBookmark(bookmark2).moveToBookmark(bookmark).select();
  17624. }
  17625. }
  17626. domUtils.preventDefault(evt);
  17627. }
  17628. //trace:1634
  17629. //ff的del键在容器空的时候,也会删除
  17630. if (browser.gecko && keyCode == 46) {
  17631. range = me.selection.getRange();
  17632. if (range.collapsed) {
  17633. start = range.startContainer;
  17634. if (domUtils.isEmptyBlock(start)) {
  17635. var parent = start.parentNode;
  17636. while (
  17637. domUtils.getChildCount(parent) == 1 &&
  17638. !domUtils.isBody(parent)
  17639. ) {
  17640. start = parent;
  17641. parent = parent.parentNode;
  17642. }
  17643. if (start === parent.lastChild) evt.preventDefault();
  17644. return;
  17645. }
  17646. }
  17647. }
  17648. /* 修复在编辑区域快捷键 (Mac:meta+alt+I; Win:ctrl+shift+I) 打不开 chrome 控制台的问题 */
  17649. browser.chrome &&
  17650. me.on("keydown", function(type, e) {
  17651. var keyCode = e.keyCode || e.which;
  17652. if (
  17653. ((e.metaKey && e.altKey) || (e.ctrlKey && e.shiftKey)) &&
  17654. keyCode == 73
  17655. ) {
  17656. return true;
  17657. }
  17658. });
  17659. });
  17660. me.addListener("keyup", function(type, evt) {
  17661. var keyCode = evt.keyCode || evt.which,
  17662. rng,
  17663. me = this;
  17664. if (keyCode == keymap.Backspace) {
  17665. if (me.fireEvent("delkeyup")) {
  17666. return;
  17667. }
  17668. rng = me.selection.getRange();
  17669. if (rng.collapsed) {
  17670. var tmpNode,
  17671. autoClearTagName = ["h1", "h2", "h3", "h4", "h5", "h6"];
  17672. if (
  17673. (tmpNode = domUtils.findParentByTagName(
  17674. rng.startContainer,
  17675. autoClearTagName,
  17676. true
  17677. ))
  17678. ) {
  17679. if (domUtils.isEmptyBlock(tmpNode)) {
  17680. var pre = tmpNode.previousSibling;
  17681. if (pre && pre.nodeName != "TABLE") {
  17682. domUtils.remove(tmpNode);
  17683. rng.setStartAtLast(pre).setCursor(false, true);
  17684. return;
  17685. } else {
  17686. var next = tmpNode.nextSibling;
  17687. if (next && next.nodeName != "TABLE") {
  17688. domUtils.remove(tmpNode);
  17689. rng.setStartAtFirst(next).setCursor(false, true);
  17690. return;
  17691. }
  17692. }
  17693. }
  17694. }
  17695. //处理当删除到body时,要重新给p标签展位
  17696. if (domUtils.isBody(rng.startContainer)) {
  17697. var tmpNode = domUtils.createElement(me.document, "p", {
  17698. innerHTML: browser.ie ? domUtils.fillChar : "<br/>"
  17699. });
  17700. rng.insertNode(tmpNode).setStart(tmpNode, 0).setCursor(false, true);
  17701. }
  17702. }
  17703. //chrome下如果删除了inline标签,浏览器会有记忆,在输入文字还是会套上刚才删除的标签,所以这里再选一次就不会了
  17704. if (
  17705. !collapsed &&
  17706. (rng.startContainer.nodeType == 3 ||
  17707. (rng.startContainer.nodeType == 1 &&
  17708. domUtils.isEmptyBlock(rng.startContainer)))
  17709. ) {
  17710. if (browser.ie) {
  17711. var span = rng.document.createElement("span");
  17712. rng.insertNode(span).setStartBefore(span).collapse(true);
  17713. rng.select();
  17714. domUtils.remove(span);
  17715. } else {
  17716. rng.select();
  17717. }
  17718. }
  17719. }
  17720. });
  17721. };
  17722. // plugins/fiximgclick.js
  17723. ///import core
  17724. ///commands 修复chrome下图片不能点击的问题,出现八个角可改变大小
  17725. ///commandsName FixImgClick
  17726. ///commandsTitle 修复chrome下图片不能点击的问题,出现八个角可改变大小
  17727. //修复chrome下图片不能点击的问题,出现八个角可改变大小
  17728. UE.plugins["fiximgclick"] = (function() {
  17729. var elementUpdated = false;
  17730. function Scale() {
  17731. this.editor = null;
  17732. this.resizer = null;
  17733. this.cover = null;
  17734. this.doc = document;
  17735. this.prePos = { x: 0, y: 0 };
  17736. this.startPos = { x: 0, y: 0 };
  17737. }
  17738. (function() {
  17739. var rect = [
  17740. //[left, top, width, height]
  17741. [0, 0, -1, -1],
  17742. [0, 0, 0, -1],
  17743. [0, 0, 1, -1],
  17744. [0, 0, -1, 0],
  17745. [0, 0, 1, 0],
  17746. [0, 0, -1, 1],
  17747. [0, 0, 0, 1],
  17748. [0, 0, 1, 1]
  17749. ];
  17750. Scale.prototype = {
  17751. init: function(editor) {
  17752. var me = this;
  17753. me.editor = editor;
  17754. me.startPos = this.prePos = { x: 0, y: 0 };
  17755. me.dragId = -1;
  17756. var hands = [],
  17757. cover = (me.cover = document.createElement("div")),
  17758. resizer = (me.resizer = document.createElement("div"));
  17759. cover.id = me.editor.ui.id + "_imagescale_cover";
  17760. cover.style.cssText =
  17761. "position:absolute;display:none;z-index:" +
  17762. me.editor.options.zIndex +
  17763. ";filter:alpha(opacity=0); opacity:0;background:#CCC;";
  17764. domUtils.on(cover, "mousedown click", function() {
  17765. me.hide();
  17766. });
  17767. for (i = 0; i < 8; i++) {
  17768. hands.push(
  17769. '<span class="edui-editor-imagescale-hand' + i + '"></span>'
  17770. );
  17771. }
  17772. resizer.id = me.editor.ui.id + "_imagescale";
  17773. resizer.className = "edui-editor-imagescale";
  17774. resizer.innerHTML = hands.join("");
  17775. resizer.style.cssText +=
  17776. ";display:none;border:1px solid #3b77ff;z-index:" +
  17777. me.editor.options.zIndex +
  17778. ";";
  17779. me.editor.ui.getDom().appendChild(cover);
  17780. me.editor.ui.getDom().appendChild(resizer);
  17781. me.initStyle();
  17782. me.initEvents();
  17783. },
  17784. initStyle: function() {
  17785. utils.cssRule(
  17786. "imagescale",
  17787. ".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;}" +
  17788. ".edui-editor-imagescale span{position:absolute;width:6px;height:6px;overflow:hidden;font-size:0px;display:block;background-color:#3C9DD0;}" +
  17789. ".edui-editor-imagescale .edui-editor-imagescale-hand0{cursor:nw-resize;top:0;margin-top:-4px;left:0;margin-left:-4px;}" +
  17790. ".edui-editor-imagescale .edui-editor-imagescale-hand1{cursor:n-resize;top:0;margin-top:-4px;left:50%;margin-left:-4px;}" +
  17791. ".edui-editor-imagescale .edui-editor-imagescale-hand2{cursor:ne-resize;top:0;margin-top:-4px;left:100%;margin-left:-3px;}" +
  17792. ".edui-editor-imagescale .edui-editor-imagescale-hand3{cursor:w-resize;top:50%;margin-top:-4px;left:0;margin-left:-4px;}" +
  17793. ".edui-editor-imagescale .edui-editor-imagescale-hand4{cursor:e-resize;top:50%;margin-top:-4px;left:100%;margin-left:-3px;}" +
  17794. ".edui-editor-imagescale .edui-editor-imagescale-hand5{cursor:sw-resize;top:100%;margin-top:-3px;left:0;margin-left:-4px;}" +
  17795. ".edui-editor-imagescale .edui-editor-imagescale-hand6{cursor:s-resize;top:100%;margin-top:-3px;left:50%;margin-left:-4px;}" +
  17796. ".edui-editor-imagescale .edui-editor-imagescale-hand7{cursor:se-resize;top:100%;margin-top:-3px;left:100%;margin-left:-3px;}"
  17797. );
  17798. },
  17799. initEvents: function() {
  17800. var me = this;
  17801. me.startPos.x = me.startPos.y = 0;
  17802. me.isDraging = false;
  17803. },
  17804. _eventHandler: function(e) {
  17805. var me = this;
  17806. switch (e.type) {
  17807. case "mousedown":
  17808. var hand = e.target || e.srcElement,
  17809. hand;
  17810. if (
  17811. hand.className.indexOf("edui-editor-imagescale-hand") != -1 &&
  17812. me.dragId == -1
  17813. ) {
  17814. me.dragId = hand.className.slice(-1);
  17815. me.startPos.x = me.prePos.x = e.clientX;
  17816. me.startPos.y = me.prePos.y = e.clientY;
  17817. domUtils.on(me.doc, "mousemove", me.proxy(me._eventHandler, me));
  17818. }
  17819. break;
  17820. case "mousemove":
  17821. if (me.dragId != -1) {
  17822. me.updateContainerStyle(me.dragId, {
  17823. x: e.clientX - me.prePos.x,
  17824. y: e.clientY - me.prePos.y
  17825. });
  17826. me.prePos.x = e.clientX;
  17827. me.prePos.y = e.clientY;
  17828. elementUpdated = true;
  17829. me.updateTargetElement();
  17830. }
  17831. break;
  17832. case "mouseup":
  17833. if (me.dragId != -1) {
  17834. me.updateContainerStyle(me.dragId, {
  17835. x: e.clientX - me.prePos.x,
  17836. y: e.clientY - me.prePos.y
  17837. });
  17838. me.updateTargetElement();
  17839. if (me.target.parentNode) me.attachTo(me.target);
  17840. me.dragId = -1;
  17841. }
  17842. domUtils.un(me.doc, "mousemove", me.proxy(me._eventHandler, me));
  17843. //修复只是点击挪动点,但没有改变大小,不应该触发contentchange
  17844. if (elementUpdated) {
  17845. elementUpdated = false;
  17846. me.editor.fireEvent("contentchange");
  17847. }
  17848. break;
  17849. default:
  17850. break;
  17851. }
  17852. },
  17853. updateTargetElement: function() {
  17854. var me = this;
  17855. domUtils.setStyles(me.target, {
  17856. width: me.resizer.style.width,
  17857. height: me.resizer.style.height
  17858. });
  17859. me.target.width = parseInt(me.resizer.style.width);
  17860. me.target.height = parseInt(me.resizer.style.height);
  17861. me.attachTo(me.target);
  17862. },
  17863. updateContainerStyle: function(dir, offset) {
  17864. var me = this,
  17865. dom = me.resizer,
  17866. tmp;
  17867. if (rect[dir][0] != 0) {
  17868. tmp = parseInt(dom.style.left) + offset.x;
  17869. dom.style.left = me._validScaledProp("left", tmp) + "px";
  17870. }
  17871. if (rect[dir][1] != 0) {
  17872. tmp = parseInt(dom.style.top) + offset.y;
  17873. dom.style.top = me._validScaledProp("top", tmp) + "px";
  17874. }
  17875. if (rect[dir][2] != 0) {
  17876. tmp = dom.clientWidth + rect[dir][2] * offset.x;
  17877. dom.style.width = me._validScaledProp("width", tmp) + "px";
  17878. }
  17879. if (rect[dir][3] != 0) {
  17880. tmp = dom.clientHeight + rect[dir][3] * offset.y;
  17881. dom.style.height = me._validScaledProp("height", tmp) + "px";
  17882. }
  17883. },
  17884. _validScaledProp: function(prop, value) {
  17885. var ele = this.resizer,
  17886. wrap = document;
  17887. value = isNaN(value) ? 0 : value;
  17888. switch (prop) {
  17889. case "left":
  17890. return value < 0
  17891. ? 0
  17892. : value + ele.clientWidth > wrap.clientWidth
  17893. ? wrap.clientWidth - ele.clientWidth
  17894. : value;
  17895. case "top":
  17896. return value < 0
  17897. ? 0
  17898. : value + ele.clientHeight > wrap.clientHeight
  17899. ? wrap.clientHeight - ele.clientHeight
  17900. : value;
  17901. case "width":
  17902. return value <= 0
  17903. ? 1
  17904. : value + ele.offsetLeft > wrap.clientWidth
  17905. ? wrap.clientWidth - ele.offsetLeft
  17906. : value;
  17907. case "height":
  17908. return value <= 0
  17909. ? 1
  17910. : value + ele.offsetTop > wrap.clientHeight
  17911. ? wrap.clientHeight - ele.offsetTop
  17912. : value;
  17913. }
  17914. },
  17915. hideCover: function() {
  17916. this.cover.style.display = "none";
  17917. },
  17918. showCover: function() {
  17919. var me = this,
  17920. editorPos = domUtils.getXY(me.editor.ui.getDom()),
  17921. iframePos = domUtils.getXY(me.editor.iframe);
  17922. domUtils.setStyles(me.cover, {
  17923. width: me.editor.iframe.offsetWidth + "px",
  17924. height: me.editor.iframe.offsetHeight + "px",
  17925. top: iframePos.y - editorPos.y + "px",
  17926. left: iframePos.x - editorPos.x + "px",
  17927. position: "absolute",
  17928. display: ""
  17929. });
  17930. },
  17931. show: function(targetObj) {
  17932. var me = this;
  17933. me.resizer.style.display = "block";
  17934. if (targetObj) me.attachTo(targetObj);
  17935. domUtils.on(this.resizer, "mousedown", me.proxy(me._eventHandler, me));
  17936. domUtils.on(me.doc, "mouseup", me.proxy(me._eventHandler, me));
  17937. me.showCover();
  17938. me.editor.fireEvent("afterscaleshow", me);
  17939. me.editor.fireEvent("saveScene");
  17940. },
  17941. hide: function() {
  17942. var me = this;
  17943. me.hideCover();
  17944. me.resizer.style.display = "none";
  17945. domUtils.un(me.resizer, "mousedown", me.proxy(me._eventHandler, me));
  17946. domUtils.un(me.doc, "mouseup", me.proxy(me._eventHandler, me));
  17947. me.editor.fireEvent("afterscalehide", me);
  17948. },
  17949. proxy: function(fn, context) {
  17950. return function(e) {
  17951. return fn.apply(context || this, arguments);
  17952. };
  17953. },
  17954. attachTo: function(targetObj) {
  17955. var me = this,
  17956. target = (me.target = targetObj),
  17957. resizer = this.resizer,
  17958. imgPos = domUtils.getXY(target),
  17959. iframePos = domUtils.getXY(me.editor.iframe),
  17960. editorPos = domUtils.getXY(resizer.parentNode);
  17961. domUtils.setStyles(resizer, {
  17962. width: target.width + "px",
  17963. height: target.height + "px",
  17964. left:
  17965. iframePos.x +
  17966. imgPos.x -
  17967. me.editor.document.body.scrollLeft -
  17968. editorPos.x -
  17969. parseInt(resizer.style.borderLeftWidth) +
  17970. "px",
  17971. top:
  17972. iframePos.y +
  17973. imgPos.y -
  17974. me.editor.document.body.scrollTop -
  17975. editorPos.y -
  17976. parseInt(resizer.style.borderTopWidth) +
  17977. "px"
  17978. });
  17979. }
  17980. };
  17981. })();
  17982. return function() {
  17983. var me = this,
  17984. imageScale;
  17985. me.setOpt("imageScaleEnabled", true);
  17986. if (!browser.ie && me.options.imageScaleEnabled) {
  17987. me.addListener("click", function(type, e) {
  17988. var range = me.selection.getRange(),
  17989. img = range.getClosedNode();
  17990. if (img && img.tagName == "IMG" && me.body.contentEditable != "false") {
  17991. if (
  17992. img.className.indexOf("edui-faked-music") != -1 ||
  17993. img.getAttribute("anchorname") ||
  17994. domUtils.hasClass(img, "loadingclass") ||
  17995. domUtils.hasClass(img, "loaderrorclass")
  17996. ) {
  17997. return;
  17998. }
  17999. if (!imageScale) {
  18000. imageScale = new Scale();
  18001. imageScale.init(me);
  18002. me.ui.getDom().appendChild(imageScale.resizer);
  18003. var _keyDownHandler = function(e) {
  18004. imageScale.hide();
  18005. if (imageScale.target)
  18006. me.selection.getRange().selectNode(imageScale.target).select();
  18007. },
  18008. _mouseDownHandler = function(e) {
  18009. var ele = e.target || e.srcElement;
  18010. if (
  18011. ele &&
  18012. (ele.className === undefined ||
  18013. ele.className.indexOf("edui-editor-imagescale") == -1)
  18014. ) {
  18015. _keyDownHandler(e);
  18016. }
  18017. },
  18018. timer;
  18019. me.addListener("afterscaleshow", function(e) {
  18020. me.addListener("beforekeydown", _keyDownHandler);
  18021. me.addListener("beforemousedown", _mouseDownHandler);
  18022. domUtils.on(document, "keydown", _keyDownHandler);
  18023. domUtils.on(document, "mousedown", _mouseDownHandler);
  18024. me.selection.getNative().removeAllRanges();
  18025. });
  18026. me.addListener("afterscalehide", function(e) {
  18027. me.removeListener("beforekeydown", _keyDownHandler);
  18028. me.removeListener("beforemousedown", _mouseDownHandler);
  18029. domUtils.un(document, "keydown", _keyDownHandler);
  18030. domUtils.un(document, "mousedown", _mouseDownHandler);
  18031. var target = imageScale.target;
  18032. if (target.parentNode) {
  18033. me.selection.getRange().selectNode(target).select();
  18034. }
  18035. });
  18036. //TODO 有iframe的情况,mousedown不能往下传。。
  18037. domUtils.on(imageScale.resizer, "mousedown", function(e) {
  18038. me.selection.getNative().removeAllRanges();
  18039. var ele = e.target || e.srcElement;
  18040. if (
  18041. ele &&
  18042. ele.className.indexOf("edui-editor-imagescale-hand") == -1
  18043. ) {
  18044. timer = setTimeout(function() {
  18045. imageScale.hide();
  18046. if (imageScale.target)
  18047. me.selection.getRange().selectNode(ele).select();
  18048. }, 200);
  18049. }
  18050. });
  18051. domUtils.on(imageScale.resizer, "mouseup", function(e) {
  18052. var ele = e.target || e.srcElement;
  18053. if (
  18054. ele &&
  18055. ele.className.indexOf("edui-editor-imagescale-hand") == -1
  18056. ) {
  18057. clearTimeout(timer);
  18058. }
  18059. });
  18060. }
  18061. imageScale.show(img);
  18062. } else {
  18063. if (imageScale && imageScale.resizer.style.display != "none")
  18064. imageScale.hide();
  18065. }
  18066. });
  18067. }
  18068. if (browser.webkit) {
  18069. me.addListener("click", function(type, e) {
  18070. if (e.target.tagName == "IMG" && me.body.contentEditable != "false") {
  18071. var range = new dom.Range(me.document);
  18072. range.selectNode(e.target).select();
  18073. }
  18074. });
  18075. }
  18076. };
  18077. })();
  18078. // plugins/autolink.js
  18079. ///import core
  18080. ///commands 为非ie浏览器自动添加a标签
  18081. ///commandsName AutoLink
  18082. ///commandsTitle 自动增加链接
  18083. /**
  18084. * @description 为非ie浏览器自动添加a标签
  18085. * @author zhanyi
  18086. */
  18087. UE.plugin.register(
  18088. "autolink",
  18089. function() {
  18090. var cont = 0;
  18091. return !browser.ie
  18092. ? {
  18093. bindEvents: {
  18094. reset: function() {
  18095. cont = 0;
  18096. },
  18097. keydown: function(type, evt) {
  18098. var me = this;
  18099. var keyCode = evt.keyCode || evt.which;
  18100. if (keyCode == 32 || keyCode == 13) {
  18101. var sel = me.selection.getNative(),
  18102. range = sel.getRangeAt(0).cloneRange(),
  18103. offset,
  18104. charCode;
  18105. var start = range.startContainer;
  18106. while (start.nodeType == 1 && range.startOffset > 0) {
  18107. start =
  18108. range.startContainer.childNodes[range.startOffset - 1];
  18109. if (!start) {
  18110. break;
  18111. }
  18112. range.setStart(
  18113. start,
  18114. start.nodeType == 1
  18115. ? start.childNodes.length
  18116. : start.nodeValue.length
  18117. );
  18118. range.collapse(true);
  18119. start = range.startContainer;
  18120. }
  18121. do {
  18122. if (range.startOffset == 0) {
  18123. start = range.startContainer.previousSibling;
  18124. while (start && start.nodeType == 1) {
  18125. start = start.lastChild;
  18126. }
  18127. if (!start || domUtils.isFillChar(start)) {
  18128. break;
  18129. }
  18130. offset = start.nodeValue.length;
  18131. } else {
  18132. start = range.startContainer;
  18133. offset = range.startOffset;
  18134. }
  18135. range.setStart(start, offset - 1);
  18136. charCode = range.toString().charCodeAt(0);
  18137. } while (charCode != 160 && charCode != 32);
  18138. if (
  18139. range
  18140. .toString()
  18141. .replace(new RegExp(domUtils.fillChar, "g"), "")
  18142. .match(/(?:https?:\/\/|ssh:\/\/|ftp:\/\/|file:\/|www\.)/i)
  18143. ) {
  18144. while (range.toString().length) {
  18145. if (
  18146. /^(?:https?:\/\/|ssh:\/\/|ftp:\/\/|file:\/|www\.)/i.test(
  18147. range.toString()
  18148. )
  18149. ) {
  18150. break;
  18151. }
  18152. try {
  18153. range.setStart(
  18154. range.startContainer,
  18155. range.startOffset + 1
  18156. );
  18157. } catch (e) {
  18158. //trace:2121
  18159. var start = range.startContainer;
  18160. while (!(next = start.nextSibling)) {
  18161. if (domUtils.isBody(start)) {
  18162. return;
  18163. }
  18164. start = start.parentNode;
  18165. }
  18166. range.setStart(next, 0);
  18167. }
  18168. }
  18169. //range的开始边界已经在a标签里的不再处理
  18170. if (
  18171. domUtils.findParentByTagName(
  18172. range.startContainer,
  18173. "a",
  18174. true
  18175. )
  18176. ) {
  18177. return;
  18178. }
  18179. var a = me.document.createElement("a"),
  18180. text = me.document.createTextNode(" "),
  18181. href;
  18182. me.undoManger && me.undoManger.save();
  18183. a.appendChild(range.extractContents());
  18184. a.href = a.innerHTML = a.innerHTML.replace(/<[^>]+>/g, "");
  18185. href = a
  18186. .getAttribute("href")
  18187. .replace(new RegExp(domUtils.fillChar, "g"), "");
  18188. href = /^(?:https?:\/\/)/gi.test(href)
  18189. ? href
  18190. : "http://" + href;
  18191. a.setAttribute("_src", utils.html(href));
  18192. a.href = utils.html(href);
  18193. range.insertNode(a);
  18194. a.parentNode.insertBefore(text, a.nextSibling);
  18195. range.setStart(text, 0);
  18196. range.collapse(true);
  18197. sel.removeAllRanges();
  18198. sel.addRange(range);
  18199. me.undoManger && me.undoManger.save();
  18200. }
  18201. }
  18202. }
  18203. }
  18204. }
  18205. : {};
  18206. },
  18207. function() {
  18208. var keyCodes = {
  18209. 37: 1,
  18210. 38: 1,
  18211. 39: 1,
  18212. 40: 1,
  18213. 13: 1,
  18214. 32: 1
  18215. };
  18216. function checkIsCludeLink(node) {
  18217. if (node.nodeType == 3) {
  18218. return null;
  18219. }
  18220. if (node.nodeName == "A") {
  18221. return node;
  18222. }
  18223. var lastChild = node.lastChild;
  18224. while (lastChild) {
  18225. if (lastChild.nodeName == "A") {
  18226. return lastChild;
  18227. }
  18228. if (lastChild.nodeType == 3) {
  18229. if (domUtils.isWhitespace(lastChild)) {
  18230. lastChild = lastChild.previousSibling;
  18231. continue;
  18232. }
  18233. return null;
  18234. }
  18235. lastChild = lastChild.lastChild;
  18236. }
  18237. }
  18238. browser.ie &&
  18239. this.addListener("keyup", function(cmd, evt) {
  18240. var me = this,
  18241. keyCode = evt.keyCode;
  18242. if (keyCodes[keyCode]) {
  18243. var rng = me.selection.getRange();
  18244. var start = rng.startContainer;
  18245. if (keyCode == 13) {
  18246. while (
  18247. start &&
  18248. !domUtils.isBody(start) &&
  18249. !domUtils.isBlockElm(start)
  18250. ) {
  18251. start = start.parentNode;
  18252. }
  18253. if (start && !domUtils.isBody(start) && start.nodeName == "P") {
  18254. var pre = start.previousSibling;
  18255. if (pre && pre.nodeType == 1) {
  18256. var pre = checkIsCludeLink(pre);
  18257. if (pre && !pre.getAttribute("_href")) {
  18258. domUtils.remove(pre, true);
  18259. }
  18260. }
  18261. }
  18262. } else if (keyCode == 32) {
  18263. if (start.nodeType == 3 && /^\s$/.test(start.nodeValue)) {
  18264. start = start.previousSibling;
  18265. if (
  18266. start &&
  18267. start.nodeName == "A" &&
  18268. !start.getAttribute("_href")
  18269. ) {
  18270. domUtils.remove(start, true);
  18271. }
  18272. }
  18273. } else {
  18274. start = domUtils.findParentByTagName(start, "a", true);
  18275. if (start && !start.getAttribute("_href")) {
  18276. var bk = rng.createBookmark();
  18277. domUtils.remove(start, true);
  18278. rng.moveToBookmark(bk).select(true);
  18279. }
  18280. }
  18281. }
  18282. });
  18283. }
  18284. );
  18285. // plugins/autoheight.js
  18286. ///import core
  18287. ///commands 当输入内容超过编辑器高度时,编辑器自动增高
  18288. ///commandsName AutoHeight,autoHeightEnabled
  18289. ///commandsTitle 自动增高
  18290. /**
  18291. * @description 自动伸展
  18292. * @author zhanyi
  18293. */
  18294. UE.plugins["autoheight"] = function() {
  18295. var me = this;
  18296. //提供开关,就算加载也可以关闭
  18297. me.autoHeightEnabled = me.options.autoHeightEnabled !== false;
  18298. if (!me.autoHeightEnabled) {
  18299. return;
  18300. }
  18301. var bakOverflow,
  18302. lastHeight = 0,
  18303. options = me.options,
  18304. currentHeight,
  18305. timer;
  18306. function adjustHeight() {
  18307. var me = this;
  18308. clearTimeout(timer);
  18309. if (isFullscreen) return;
  18310. if (
  18311. !me.queryCommandState ||
  18312. (me.queryCommandState && me.queryCommandState("source") != 1)
  18313. ) {
  18314. timer = setTimeout(function() {
  18315. var node = me.body.lastChild;
  18316. while (node && node.nodeType != 1) {
  18317. node = node.previousSibling;
  18318. }
  18319. if (node && node.nodeType == 1) {
  18320. node.style.clear = "both";
  18321. currentHeight = Math.max(
  18322. domUtils.getXY(node).y + node.offsetHeight + 25,
  18323. Math.max(options.minFrameHeight, options.initialFrameHeight)
  18324. );
  18325. if (currentHeight != lastHeight) {
  18326. if (currentHeight !== parseInt(me.iframe.parentNode.style.height)) {
  18327. me.iframe.parentNode.style.height = currentHeight + "px";
  18328. }
  18329. me.body.style.height = currentHeight + "px";
  18330. lastHeight = currentHeight;
  18331. }
  18332. domUtils.removeStyle(node, "clear");
  18333. }
  18334. }, 50);
  18335. }
  18336. }
  18337. var isFullscreen;
  18338. me.addListener("fullscreenchanged", function(cmd, f) {
  18339. isFullscreen = f;
  18340. });
  18341. me.addListener("destroy", function() {
  18342. domUtils.un(me.window, "scroll", fixedScrollTop);
  18343. me.removeListener(
  18344. "contentchange afterinserthtml keyup mouseup",
  18345. adjustHeight
  18346. );
  18347. });
  18348. me.enableAutoHeight = function() {
  18349. var me = this;
  18350. if (!me.autoHeightEnabled) {
  18351. return;
  18352. }
  18353. var doc = me.document;
  18354. me.autoHeightEnabled = true;
  18355. bakOverflow = doc.body.style.overflowY;
  18356. doc.body.style.overflowY = "hidden";
  18357. me.addListener("contentchange afterinserthtml keyup mouseup", adjustHeight);
  18358. //ff不给事件算得不对
  18359. setTimeout(function() {
  18360. adjustHeight.call(me);
  18361. }, browser.gecko ? 100 : 0);
  18362. me.fireEvent("autoheightchanged", me.autoHeightEnabled);
  18363. };
  18364. me.disableAutoHeight = function() {
  18365. me.body.style.overflowY = bakOverflow || "";
  18366. me.removeListener("contentchange", adjustHeight);
  18367. me.removeListener("keyup", adjustHeight);
  18368. me.removeListener("mouseup", adjustHeight);
  18369. me.autoHeightEnabled = false;
  18370. me.fireEvent("autoheightchanged", me.autoHeightEnabled);
  18371. };
  18372. me.on("setHeight", function() {
  18373. me.disableAutoHeight();
  18374. });
  18375. me.addListener("ready", function() {
  18376. me.enableAutoHeight();
  18377. //trace:1764
  18378. var timer;
  18379. domUtils.on(
  18380. browser.ie ? me.body : me.document,
  18381. browser.webkit ? "dragover" : "drop",
  18382. function() {
  18383. clearTimeout(timer);
  18384. timer = setTimeout(function() {
  18385. //trace:3681
  18386. adjustHeight.call(me);
  18387. }, 100);
  18388. }
  18389. );
  18390. //修复内容过多时,回到顶部,顶部内容被工具栏遮挡问题
  18391. domUtils.on(me.window, "scroll", fixedScrollTop);
  18392. });
  18393. var lastScrollY;
  18394. function fixedScrollTop() {
  18395. if (!me.window) return;
  18396. if (lastScrollY === null) {
  18397. lastScrollY = me.window.scrollY;
  18398. } else if (me.window.scrollY == 0 && lastScrollY != 0) {
  18399. me.window.scrollTo(0, 0);
  18400. lastScrollY = null;
  18401. }
  18402. }
  18403. };
  18404. // plugins/autofloat.js
  18405. ///import core
  18406. ///commands 悬浮工具栏
  18407. ///commandsName AutoFloat,autoFloatEnabled
  18408. ///commandsTitle 悬浮工具栏
  18409. /**
  18410. * modified by chengchao01
  18411. * 注意: 引入此功能后,在IE6下会将body的背景图片覆盖掉!
  18412. */
  18413. UE.plugins["autofloat"] = function() {
  18414. var me = this,
  18415. lang = me.getLang();
  18416. me.setOpt({
  18417. topOffset: 0
  18418. });
  18419. var optsAutoFloatEnabled = me.options.autoFloatEnabled !== false,
  18420. topOffset = me.options.topOffset;
  18421. //如果不固定toolbar的位置,则直接退出
  18422. if (!optsAutoFloatEnabled) {
  18423. return;
  18424. }
  18425. var uiUtils = UE.ui.uiUtils,
  18426. LteIE6 = browser.ie && browser.version <= 6,
  18427. quirks = browser.quirks;
  18428. function checkHasUI() {
  18429. if (!UE.ui) {
  18430. alert(lang.autofloatMsg);
  18431. return 0;
  18432. }
  18433. return 1;
  18434. }
  18435. function fixIE6FixedPos() {
  18436. var docStyle = document.body.style;
  18437. docStyle.backgroundImage = 'url("about:blank")';
  18438. docStyle.backgroundAttachment = "fixed";
  18439. }
  18440. var bakCssText,
  18441. placeHolder = document.createElement("div"),
  18442. toolbarBox,
  18443. orgTop,
  18444. getPosition,
  18445. flag = true; //ie7模式下需要偏移
  18446. function setFloating() {
  18447. var toobarBoxPos = domUtils.getXY(toolbarBox),
  18448. origalFloat = domUtils.getComputedStyle(toolbarBox, "position"),
  18449. origalLeft = domUtils.getComputedStyle(toolbarBox, "left");
  18450. toolbarBox.style.width = toolbarBox.offsetWidth + "px";
  18451. toolbarBox.style.zIndex = me.options.zIndex * 1 + 1;
  18452. toolbarBox.parentNode.insertBefore(placeHolder, toolbarBox);
  18453. if (LteIE6 || (quirks && browser.ie)) {
  18454. if (toolbarBox.style.position != "absolute") {
  18455. toolbarBox.style.position = "absolute";
  18456. }
  18457. toolbarBox.style.top =
  18458. (document.body.scrollTop || document.documentElement.scrollTop) -
  18459. orgTop +
  18460. topOffset +
  18461. "px";
  18462. } else {
  18463. if (browser.ie7Compat && flag) {
  18464. flag = false;
  18465. toolbarBox.style.left =
  18466. domUtils.getXY(toolbarBox).x -
  18467. document.documentElement.getBoundingClientRect().left +
  18468. 2 +
  18469. "px";
  18470. }
  18471. if (toolbarBox.style.position != "fixed") {
  18472. toolbarBox.style.position = "fixed";
  18473. toolbarBox.style.top = topOffset + "px";
  18474. (origalFloat == "absolute" || origalFloat == "relative") &&
  18475. parseFloat(origalLeft) &&
  18476. (toolbarBox.style.left = toobarBoxPos.x + "px");
  18477. }
  18478. }
  18479. }
  18480. function unsetFloating() {
  18481. flag = true;
  18482. if (placeHolder.parentNode) {
  18483. placeHolder.parentNode.removeChild(placeHolder);
  18484. }
  18485. toolbarBox.style.cssText = bakCssText;
  18486. }
  18487. function updateFloating() {
  18488. var rect3 = getPosition(me.container);
  18489. var offset = me.options.toolbarTopOffset || 0;
  18490. if (rect3.top < 0 && rect3.bottom - toolbarBox.offsetHeight > offset) {
  18491. setFloating();
  18492. } else {
  18493. unsetFloating();
  18494. }
  18495. }
  18496. var defer_updateFloating = utils.defer(
  18497. function() {
  18498. updateFloating();
  18499. },
  18500. browser.ie ? 200 : 100,
  18501. true
  18502. );
  18503. me.addListener("destroy", function() {
  18504. domUtils.un(window, ["scroll", "resize"], updateFloating);
  18505. me.removeListener("keydown", defer_updateFloating);
  18506. });
  18507. me.addListener("ready", function() {
  18508. if (checkHasUI(me)) {
  18509. //加载了ui组件,但在new时,没有加载ui,导致编辑器实例上没有ui类,所以这里做判断
  18510. if (!me.ui) {
  18511. return;
  18512. }
  18513. getPosition = uiUtils.getClientRect;
  18514. toolbarBox = me.ui.getDom("toolbarbox");
  18515. orgTop = getPosition(toolbarBox).top;
  18516. bakCssText = toolbarBox.style.cssText;
  18517. placeHolder.style.height = toolbarBox.offsetHeight + "px";
  18518. if (LteIE6) {
  18519. fixIE6FixedPos();
  18520. }
  18521. domUtils.on(window, ["scroll", "resize"], updateFloating);
  18522. me.addListener("keydown", defer_updateFloating);
  18523. me.addListener("beforefullscreenchange", function(t, enabled) {
  18524. if (enabled) {
  18525. unsetFloating();
  18526. }
  18527. });
  18528. me.addListener("fullscreenchanged", function(t, enabled) {
  18529. if (!enabled) {
  18530. updateFloating();
  18531. }
  18532. });
  18533. me.addListener("sourcemodechanged", function(t, enabled) {
  18534. setTimeout(function() {
  18535. updateFloating();
  18536. }, 0);
  18537. });
  18538. me.addListener("clearDoc", function() {
  18539. setTimeout(function() {
  18540. updateFloating();
  18541. }, 0);
  18542. });
  18543. }
  18544. });
  18545. };
  18546. // plugins/video.js
  18547. /**
  18548. * video插件, 为UEditor提供视频插入支持
  18549. * @file
  18550. * @since 1.2.6.1
  18551. */
  18552. UE.plugins["video"] = function() {
  18553. var me = this;
  18554. /**
  18555. * 创建插入视频字符窜
  18556. * @param url 视频地址
  18557. * @param width 视频宽度
  18558. * @param height 视频高度
  18559. * @param align 视频对齐
  18560. * @param toEmbed 是否以flash代替显示
  18561. * @param addParagraph 是否需要添加P 标签
  18562. */
  18563. function creatInsertStr(url, width, height, id, align, classname, type) {
  18564. var str;
  18565. switch (type) {
  18566. case "image":
  18567. str =
  18568. "<img " +
  18569. (id ? 'id="' + id + '"' : "") +
  18570. ' width="' +
  18571. width +
  18572. '" height="' +
  18573. height +
  18574. '" _url="' +
  18575. url +
  18576. '" class="' +
  18577. classname.replace(/\bvideo-js\b/, "") +
  18578. '"' +
  18579. ' src="' +
  18580. me.options.UEDITOR_HOME_URL +
  18581. 'themes/default/images/spacer.gif" style="background:url(' +
  18582. me.options.UEDITOR_HOME_URL +
  18583. "themes/default/images/videologo.gif) no-repeat center center; border:1px solid gray;" +
  18584. (align ? "float:" + align + ";" : "") +
  18585. '" />';
  18586. break;
  18587. case "embed":
  18588. str =
  18589. '<embed type="application/x-shockwave-flash" class="' +
  18590. classname +
  18591. '" pluginspage="http://www.macromedia.com/go/getflashplayer"' +
  18592. ' src="' +
  18593. utils.html(url) +
  18594. '" width="' +
  18595. width +
  18596. '" height="' +
  18597. height +
  18598. '"' +
  18599. (align ? ' style="float:' + align + '"' : "") +
  18600. ' wmode="transparent" play="true" loop="false" menu="false" allowscriptaccess="never" allowfullscreen="true" >';
  18601. break;
  18602. case "video":
  18603. var ext = url.substr(url.lastIndexOf(".") + 1);
  18604. if (ext == "ogv") ext = "ogg";
  18605. str =
  18606. "<video" +
  18607. (id ? ' id="' + id + '"' : "") +
  18608. ' class="' +
  18609. classname +
  18610. ' video-js" ' +
  18611. (align ? ' style="float:' + align + '"' : "") +
  18612. ' controls preload="none" width="' +
  18613. width +
  18614. '" height="' +
  18615. height +
  18616. '" src="' +
  18617. url +
  18618. '" data-setup="{}">' +
  18619. '<source src="' +
  18620. url +
  18621. '" type="video/' +
  18622. ext +
  18623. '" /></video>';
  18624. break;
  18625. }
  18626. return str;
  18627. }
  18628. function switchImgAndVideo(root, img2video) {
  18629. utils.each(
  18630. root.getNodesByTagName(img2video ? "img" : "embed video"),
  18631. function(node) {
  18632. var className = node.getAttr("class");
  18633. if (className && className.indexOf("edui-faked-video") != -1) {
  18634. var html = creatInsertStr(
  18635. img2video ? node.getAttr("_url") : node.getAttr("src"),
  18636. node.getAttr("width"),
  18637. node.getAttr("height"),
  18638. null,
  18639. node.getStyle("float") || "",
  18640. className,
  18641. img2video ? "embed" : "image"
  18642. );
  18643. node.parentNode.replaceChild(UE.uNode.createElement(html), node);
  18644. }
  18645. if (className && className.indexOf("edui-upload-video") != -1) {
  18646. var html = creatInsertStr(
  18647. img2video ? node.getAttr("_url") : node.getAttr("src"),
  18648. node.getAttr("width"),
  18649. node.getAttr("height"),
  18650. null,
  18651. node.getStyle("float") || "",
  18652. className,
  18653. img2video ? "video" : "image"
  18654. );
  18655. node.parentNode.replaceChild(UE.uNode.createElement(html), node);
  18656. }
  18657. }
  18658. );
  18659. }
  18660. me.addOutputRule(function(root) {
  18661. switchImgAndVideo(root, true);
  18662. });
  18663. me.addInputRule(function(root) {
  18664. switchImgAndVideo(root);
  18665. });
  18666. /**
  18667. * 插入视频
  18668. * @command insertvideo
  18669. * @method execCommand
  18670. * @param { String } cmd 命令字符串
  18671. * @param { Object } videoAttr 键值对对象, 描述一个视频的所有属性
  18672. * @example
  18673. * ```javascript
  18674. *
  18675. * var videoAttr = {
  18676. * //视频地址
  18677. * url: 'http://www.youku.com/xxx',
  18678. * //视频宽高值, 单位px
  18679. * width: 200,
  18680. * height: 100
  18681. * };
  18682. *
  18683. * //editor 是编辑器实例
  18684. * //向编辑器插入单个视频
  18685. * editor.execCommand( 'insertvideo', videoAttr );
  18686. * ```
  18687. */
  18688. /**
  18689. * 插入视频
  18690. * @command insertvideo
  18691. * @method execCommand
  18692. * @param { String } cmd 命令字符串
  18693. * @param { Array } videoArr 需要插入的视频的数组, 其中的每一个元素都是一个键值对对象, 描述了一个视频的所有属性
  18694. * @example
  18695. * ```javascript
  18696. *
  18697. * var videoAttr1 = {
  18698. * //视频地址
  18699. * url: 'http://www.youku.com/xxx',
  18700. * //视频宽高值, 单位px
  18701. * width: 200,
  18702. * height: 100
  18703. * },
  18704. * videoAttr2 = {
  18705. * //视频地址
  18706. * url: 'http://www.youku.com/xxx',
  18707. * //视频宽高值, 单位px
  18708. * width: 200,
  18709. * height: 100
  18710. * }
  18711. *
  18712. * //editor 是编辑器实例
  18713. * //该方法将会向编辑器内插入两个视频
  18714. * editor.execCommand( 'insertvideo', [ videoAttr1, videoAttr2 ] );
  18715. * ```
  18716. */
  18717. /**
  18718. * 查询当前光标所在处是否是一个视频
  18719. * @command insertvideo
  18720. * @method queryCommandState
  18721. * @param { String } cmd 需要查询的命令字符串
  18722. * @return { int } 如果当前光标所在处的元素是一个视频对象, 则返回1,否则返回0
  18723. * @example
  18724. * ```javascript
  18725. *
  18726. * //editor 是编辑器实例
  18727. * editor.queryCommandState( 'insertvideo' );
  18728. * ```
  18729. */
  18730. me.commands["insertvideo"] = {
  18731. execCommand: function(cmd, videoObjs, type) {
  18732. videoObjs = utils.isArray(videoObjs) ? videoObjs : [videoObjs];
  18733. if (me.fireEvent("beforeinsertvideo", videoObjs) === true) {
  18734. return;
  18735. }
  18736. var html = [],
  18737. id = "tmpVedio",
  18738. cl;
  18739. for (var i = 0, vi, len = videoObjs.length; i < len; i++) {
  18740. vi = videoObjs[i];
  18741. cl = type == "upload"
  18742. ? "edui-upload-video video-js vjs-default-skin"
  18743. : "edui-faked-video";
  18744. html.push(
  18745. creatInsertStr(
  18746. vi.url,
  18747. vi.width || 420,
  18748. vi.height || 280,
  18749. id + i,
  18750. null,
  18751. cl,
  18752. "image"
  18753. )
  18754. );
  18755. }
  18756. me.execCommand("inserthtml", html.join(""), true);
  18757. var rng = this.selection.getRange();
  18758. for (var i = 0, len = videoObjs.length; i < len; i++) {
  18759. var img = this.document.getElementById("tmpVedio" + i);
  18760. domUtils.removeAttributes(img, "id");
  18761. rng.selectNode(img).select();
  18762. me.execCommand("imagefloat", videoObjs[i].align);
  18763. }
  18764. me.fireEvent("afterinsertvideo", videoObjs);
  18765. },
  18766. queryCommandState: function() {
  18767. var img = me.selection.getRange().getClosedNode(),
  18768. flag =
  18769. img &&
  18770. (img.className == "edui-faked-video" ||
  18771. img.className.indexOf("edui-upload-video") != -1);
  18772. return flag ? 1 : 0;
  18773. }
  18774. };
  18775. };
  18776. // plugins/table.core.js
  18777. /**
  18778. * Created with JetBrains WebStorm.
  18779. * User: taoqili
  18780. * Date: 13-1-18
  18781. * Time: 上午11:09
  18782. * To change this template use File | Settings | File Templates.
  18783. */
  18784. /**
  18785. * UE表格操作类
  18786. * @param table
  18787. * @constructor
  18788. */
  18789. (function() {
  18790. var UETable = (UE.UETable = function(table) {
  18791. this.table = table;
  18792. this.indexTable = [];
  18793. this.selectedTds = [];
  18794. this.cellsRange = {};
  18795. this.update(table);
  18796. });
  18797. //===以下为静态工具方法===
  18798. UETable.removeSelectedClass = function(cells) {
  18799. utils.each(cells, function(cell) {
  18800. domUtils.removeClasses(cell, "selectTdClass");
  18801. });
  18802. };
  18803. UETable.addSelectedClass = function(cells) {
  18804. utils.each(cells, function(cell) {
  18805. domUtils.addClass(cell, "selectTdClass");
  18806. });
  18807. };
  18808. UETable.isEmptyBlock = function(node) {
  18809. var reg = new RegExp(domUtils.fillChar, "g");
  18810. if (
  18811. node[browser.ie ? "innerText" : "textContent"]
  18812. .replace(/^\s*$/, "")
  18813. .replace(reg, "").length > 0
  18814. ) {
  18815. return 0;
  18816. }
  18817. for (var i in dtd.$isNotEmpty)
  18818. if (dtd.$isNotEmpty.hasOwnProperty(i)) {
  18819. if (node.getElementsByTagName(i).length) {
  18820. return 0;
  18821. }
  18822. }
  18823. return 1;
  18824. };
  18825. UETable.getWidth = function(cell) {
  18826. if (!cell) return 0;
  18827. return parseInt(domUtils.getComputedStyle(cell, "width"), 10);
  18828. };
  18829. /**
  18830. * 获取单元格或者单元格组的“对齐”状态。 如果当前的检测对象是一个单元格组, 只有在满足所有单元格的 水平和竖直 对齐属性都相同的
  18831. * 条件时才会返回其状态值,否则将返回null; 如果当前只检测了一个单元格, 则直接返回当前单元格的对齐状态;
  18832. * @param table cell or table cells , 支持单个单元格dom对象 或者 单元格dom对象数组
  18833. * @return { align: 'left' || 'right' || 'center', valign: 'top' || 'middle' || 'bottom' } 或者 null
  18834. */
  18835. UETable.getTableCellAlignState = function(cells) {
  18836. !utils.isArray(cells) && (cells = [cells]);
  18837. var result = {},
  18838. status = ["align", "valign"],
  18839. tempStatus = null,
  18840. isSame = true; //状态是否相同
  18841. utils.each(cells, function(cellNode) {
  18842. utils.each(status, function(currentState) {
  18843. tempStatus = cellNode.getAttribute(currentState);
  18844. if (!result[currentState] && tempStatus) {
  18845. result[currentState] = tempStatus;
  18846. } else if (
  18847. !result[currentState] ||
  18848. tempStatus !== result[currentState]
  18849. ) {
  18850. isSame = false;
  18851. return false;
  18852. }
  18853. });
  18854. return isSame;
  18855. });
  18856. return isSame ? result : null;
  18857. };
  18858. /**
  18859. * 根据当前选区获取相关的table信息
  18860. * @return {Object}
  18861. */
  18862. UETable.getTableItemsByRange = function(editor) {
  18863. var start = editor.selection.getStart();
  18864. //ff下会选中bookmark
  18865. if (
  18866. start &&
  18867. start.id &&
  18868. start.id.indexOf("_baidu_bookmark_start_") === 0 &&
  18869. start.nextSibling
  18870. ) {
  18871. start = start.nextSibling;
  18872. }
  18873. //在table或者td边缘有可能存在选中tr的情况
  18874. var cell = start && domUtils.findParentByTagName(start, ["td", "th"], true),
  18875. tr = cell && cell.parentNode,
  18876. table = tr && domUtils.findParentByTagName(tr, ["table"]),
  18877. caption = table && table.getElementsByTagName("caption")[0];
  18878. return {
  18879. cell: cell,
  18880. tr: tr,
  18881. table: table,
  18882. caption: caption
  18883. };
  18884. };
  18885. UETable.getUETableBySelected = function(editor) {
  18886. var table = UETable.getTableItemsByRange(editor).table;
  18887. if (table && table.ueTable && table.ueTable.selectedTds.length) {
  18888. return table.ueTable;
  18889. }
  18890. return null;
  18891. };
  18892. UETable.getDefaultValue = function(editor, table) {
  18893. var borderMap = {
  18894. thin: "0px",
  18895. medium: "1px",
  18896. thick: "2px"
  18897. },
  18898. tableBorder,
  18899. tdPadding,
  18900. tdBorder,
  18901. tmpValue;
  18902. if (!table) {
  18903. table = editor.document.createElement("table");
  18904. table.insertRow(0).insertCell(0).innerHTML = "xxx";
  18905. editor.body.appendChild(table);
  18906. var td = table.getElementsByTagName("td")[0];
  18907. tmpValue = domUtils.getComputedStyle(table, "border-left-width");
  18908. tableBorder = parseInt(borderMap[tmpValue] || tmpValue, 10);
  18909. tmpValue = domUtils.getComputedStyle(td, "padding-left");
  18910. tdPadding = parseInt(borderMap[tmpValue] || tmpValue, 10);
  18911. tmpValue = domUtils.getComputedStyle(td, "border-left-width");
  18912. tdBorder = parseInt(borderMap[tmpValue] || tmpValue, 10);
  18913. domUtils.remove(table);
  18914. return {
  18915. tableBorder: tableBorder,
  18916. tdPadding: tdPadding,
  18917. tdBorder: tdBorder
  18918. };
  18919. } else {
  18920. td = table.getElementsByTagName("td")[0];
  18921. tmpValue = domUtils.getComputedStyle(table, "border-left-width");
  18922. tableBorder = parseInt(borderMap[tmpValue] || tmpValue, 10);
  18923. tmpValue = domUtils.getComputedStyle(td, "padding-left");
  18924. tdPadding = parseInt(borderMap[tmpValue] || tmpValue, 10);
  18925. tmpValue = domUtils.getComputedStyle(td, "border-left-width");
  18926. tdBorder = parseInt(borderMap[tmpValue] || tmpValue, 10);
  18927. return {
  18928. tableBorder: tableBorder,
  18929. tdPadding: tdPadding,
  18930. tdBorder: tdBorder
  18931. };
  18932. }
  18933. };
  18934. /**
  18935. * 根据当前点击的td或者table获取索引对象
  18936. * @param tdOrTable
  18937. */
  18938. UETable.getUETable = function(tdOrTable) {
  18939. var tag = tdOrTable.tagName.toLowerCase();
  18940. tdOrTable = tag == "td" || tag == "th" || tag == "caption"
  18941. ? domUtils.findParentByTagName(tdOrTable, "table", true)
  18942. : tdOrTable;
  18943. if (!tdOrTable.ueTable) {
  18944. tdOrTable.ueTable = new UETable(tdOrTable);
  18945. }
  18946. return tdOrTable.ueTable;
  18947. };
  18948. UETable.cloneCell = function(cell, ignoreMerge, keepPro) {
  18949. if (!cell || utils.isString(cell)) {
  18950. return this.table.ownerDocument.createElement(cell || "td");
  18951. }
  18952. var flag = domUtils.hasClass(cell, "selectTdClass");
  18953. flag && domUtils.removeClasses(cell, "selectTdClass");
  18954. var tmpCell = cell.cloneNode(true);
  18955. if (ignoreMerge) {
  18956. tmpCell.rowSpan = tmpCell.colSpan = 1;
  18957. }
  18958. //去掉宽高
  18959. !keepPro && domUtils.removeAttributes(tmpCell, "width height");
  18960. !keepPro && domUtils.removeAttributes(tmpCell, "style");
  18961. tmpCell.style.borderLeftStyle = "";
  18962. tmpCell.style.borderTopStyle = "";
  18963. tmpCell.style.borderLeftColor = cell.style.borderRightColor;
  18964. tmpCell.style.borderLeftWidth = cell.style.borderRightWidth;
  18965. tmpCell.style.borderTopColor = cell.style.borderBottomColor;
  18966. tmpCell.style.borderTopWidth = cell.style.borderBottomWidth;
  18967. flag && domUtils.addClass(cell, "selectTdClass");
  18968. return tmpCell;
  18969. };
  18970. UETable.prototype = {
  18971. getMaxRows: function() {
  18972. var rows = this.table.rows,
  18973. maxLen = 1;
  18974. for (var i = 0, row; (row = rows[i]); i++) {
  18975. var currentMax = 1;
  18976. for (var j = 0, cj; (cj = row.cells[j++]); ) {
  18977. currentMax = Math.max(cj.rowSpan || 1, currentMax);
  18978. }
  18979. maxLen = Math.max(currentMax + i, maxLen);
  18980. }
  18981. return maxLen;
  18982. },
  18983. /**
  18984. * 获取当前表格的最大列数
  18985. */
  18986. getMaxCols: function() {
  18987. var rows = this.table.rows,
  18988. maxLen = 0,
  18989. cellRows = {};
  18990. for (var i = 0, row; (row = rows[i]); i++) {
  18991. var cellsNum = 0;
  18992. for (var j = 0, cj; (cj = row.cells[j++]); ) {
  18993. cellsNum += cj.colSpan || 1;
  18994. if (cj.rowSpan && cj.rowSpan > 1) {
  18995. for (var k = 1; k < cj.rowSpan; k++) {
  18996. if (!cellRows["row_" + (i + k)]) {
  18997. cellRows["row_" + (i + k)] = cj.colSpan || 1;
  18998. } else {
  18999. cellRows["row_" + (i + k)]++;
  19000. }
  19001. }
  19002. }
  19003. }
  19004. cellsNum += cellRows["row_" + i] || 0;
  19005. maxLen = Math.max(cellsNum, maxLen);
  19006. }
  19007. return maxLen;
  19008. },
  19009. getCellColIndex: function(cell) {},
  19010. /**
  19011. * 获取当前cell旁边的单元格,
  19012. * @param cell
  19013. * @param right
  19014. */
  19015. getHSideCell: function(cell, right) {
  19016. try {
  19017. var cellInfo = this.getCellInfo(cell),
  19018. previewRowIndex,
  19019. previewColIndex;
  19020. var len = this.selectedTds.length,
  19021. range = this.cellsRange;
  19022. //首行或者首列没有前置单元格
  19023. if (
  19024. (!right && (!len ? !cellInfo.colIndex : !range.beginColIndex)) ||
  19025. (right &&
  19026. (!len
  19027. ? cellInfo.colIndex == this.colsNum - 1
  19028. : range.endColIndex == this.colsNum - 1))
  19029. )
  19030. return null;
  19031. previewRowIndex = !len ? cellInfo.rowIndex : range.beginRowIndex;
  19032. previewColIndex = !right
  19033. ? !len
  19034. ? cellInfo.colIndex < 1 ? 0 : cellInfo.colIndex - 1
  19035. : range.beginColIndex - 1
  19036. : !len ? cellInfo.colIndex + 1 : range.endColIndex + 1;
  19037. return this.getCell(
  19038. this.indexTable[previewRowIndex][previewColIndex].rowIndex,
  19039. this.indexTable[previewRowIndex][previewColIndex].cellIndex
  19040. );
  19041. } catch (e) {
  19042. showError(e);
  19043. }
  19044. },
  19045. getTabNextCell: function(cell, preRowIndex) {
  19046. var cellInfo = this.getCellInfo(cell),
  19047. rowIndex = preRowIndex || cellInfo.rowIndex,
  19048. colIndex = cellInfo.colIndex + 1 + (cellInfo.colSpan - 1),
  19049. nextCell;
  19050. try {
  19051. nextCell = this.getCell(
  19052. this.indexTable[rowIndex][colIndex].rowIndex,
  19053. this.indexTable[rowIndex][colIndex].cellIndex
  19054. );
  19055. } catch (e) {
  19056. try {
  19057. rowIndex = rowIndex * 1 + 1;
  19058. colIndex = 0;
  19059. nextCell = this.getCell(
  19060. this.indexTable[rowIndex][colIndex].rowIndex,
  19061. this.indexTable[rowIndex][colIndex].cellIndex
  19062. );
  19063. } catch (e) {}
  19064. }
  19065. return nextCell;
  19066. },
  19067. /**
  19068. * 获取视觉上的后置单元格
  19069. * @param cell
  19070. * @param bottom
  19071. */
  19072. getVSideCell: function(cell, bottom, ignoreRange) {
  19073. try {
  19074. var cellInfo = this.getCellInfo(cell),
  19075. nextRowIndex,
  19076. nextColIndex;
  19077. var len = this.selectedTds.length && !ignoreRange,
  19078. range = this.cellsRange;
  19079. //末行或者末列没有后置单元格
  19080. if (
  19081. (!bottom && cellInfo.rowIndex == 0) ||
  19082. (bottom &&
  19083. (!len
  19084. ? cellInfo.rowIndex + cellInfo.rowSpan > this.rowsNum - 1
  19085. : range.endRowIndex == this.rowsNum - 1))
  19086. )
  19087. return null;
  19088. nextRowIndex = !bottom
  19089. ? !len ? cellInfo.rowIndex - 1 : range.beginRowIndex - 1
  19090. : !len ? cellInfo.rowIndex + cellInfo.rowSpan : range.endRowIndex + 1;
  19091. nextColIndex = !len ? cellInfo.colIndex : range.beginColIndex;
  19092. return this.getCell(
  19093. this.indexTable[nextRowIndex][nextColIndex].rowIndex,
  19094. this.indexTable[nextRowIndex][nextColIndex].cellIndex
  19095. );
  19096. } catch (e) {
  19097. showError(e);
  19098. }
  19099. },
  19100. /**
  19101. * 获取相同结束位置的单元格,xOrY指代了是获取x轴相同还是y轴相同
  19102. */
  19103. getSameEndPosCells: function(cell, xOrY) {
  19104. try {
  19105. var flag = xOrY.toLowerCase() === "x",
  19106. end =
  19107. domUtils.getXY(cell)[flag ? "x" : "y"] +
  19108. cell["offset" + (flag ? "Width" : "Height")],
  19109. rows = this.table.rows,
  19110. cells = null,
  19111. returns = [];
  19112. for (var i = 0; i < this.rowsNum; i++) {
  19113. cells = rows[i].cells;
  19114. for (var j = 0, tmpCell; (tmpCell = cells[j++]); ) {
  19115. var tmpEnd =
  19116. domUtils.getXY(tmpCell)[flag ? "x" : "y"] +
  19117. tmpCell["offset" + (flag ? "Width" : "Height")];
  19118. //对应行的td已经被上面行rowSpan了
  19119. if (tmpEnd > end && flag) break;
  19120. if (cell == tmpCell || end == tmpEnd) {
  19121. //只获取单一的单元格
  19122. //todo 仅获取单一单元格在特定情况下会造成returns为空,从而影响后续的拖拽实现,修正这个。需考虑性能
  19123. if (tmpCell[flag ? "colSpan" : "rowSpan"] == 1) {
  19124. returns.push(tmpCell);
  19125. }
  19126. if (flag) break;
  19127. }
  19128. }
  19129. }
  19130. return returns;
  19131. } catch (e) {
  19132. showError(e);
  19133. }
  19134. },
  19135. setCellContent: function(cell, content) {
  19136. cell.innerHTML = content || (browser.ie ? domUtils.fillChar : "<br />");
  19137. },
  19138. cloneCell: UETable.cloneCell,
  19139. /**
  19140. * 获取跟当前单元格的右边竖线为左边的所有未合并单元格
  19141. */
  19142. getSameStartPosXCells: function(cell) {
  19143. try {
  19144. var start = domUtils.getXY(cell).x + cell.offsetWidth,
  19145. rows = this.table.rows,
  19146. cells,
  19147. returns = [];
  19148. for (var i = 0; i < this.rowsNum; i++) {
  19149. cells = rows[i].cells;
  19150. for (var j = 0, tmpCell; (tmpCell = cells[j++]); ) {
  19151. var tmpStart = domUtils.getXY(tmpCell).x;
  19152. if (tmpStart > start) break;
  19153. if (tmpStart == start && tmpCell.colSpan == 1) {
  19154. returns.push(tmpCell);
  19155. break;
  19156. }
  19157. }
  19158. }
  19159. return returns;
  19160. } catch (e) {
  19161. showError(e);
  19162. }
  19163. },
  19164. /**
  19165. * 更新table对应的索引表
  19166. */
  19167. update: function(table) {
  19168. this.table = table || this.table;
  19169. this.selectedTds = [];
  19170. this.cellsRange = {};
  19171. this.indexTable = [];
  19172. var rows = this.table.rows,
  19173. rowsNum = this.getMaxRows(),
  19174. dNum = rowsNum - rows.length,
  19175. colsNum = this.getMaxCols();
  19176. while (dNum--) {
  19177. this.table.insertRow(rows.length);
  19178. }
  19179. this.rowsNum = rowsNum;
  19180. this.colsNum = colsNum;
  19181. for (var i = 0, len = rows.length; i < len; i++) {
  19182. this.indexTable[i] = new Array(colsNum);
  19183. }
  19184. //填充索引表
  19185. for (var rowIndex = 0, row; (row = rows[rowIndex]); rowIndex++) {
  19186. for (
  19187. var cellIndex = 0, cell, cells = row.cells;
  19188. (cell = cells[cellIndex]);
  19189. cellIndex++
  19190. ) {
  19191. //修正整行被rowSpan时导致的行数计算错误
  19192. if (cell.rowSpan > rowsNum) {
  19193. cell.rowSpan = rowsNum;
  19194. }
  19195. var colIndex = cellIndex,
  19196. rowSpan = cell.rowSpan || 1,
  19197. colSpan = cell.colSpan || 1;
  19198. //当已经被上一行rowSpan或者被前一列colSpan了,则跳到下一个单元格进行
  19199. while (this.indexTable[rowIndex][colIndex]) colIndex++;
  19200. for (var j = 0; j < rowSpan; j++) {
  19201. for (var k = 0; k < colSpan; k++) {
  19202. this.indexTable[rowIndex + j][colIndex + k] = {
  19203. rowIndex: rowIndex,
  19204. cellIndex: cellIndex,
  19205. colIndex: colIndex,
  19206. rowSpan: rowSpan,
  19207. colSpan: colSpan
  19208. };
  19209. }
  19210. }
  19211. }
  19212. }
  19213. //修复残缺td
  19214. for (j = 0; j < rowsNum; j++) {
  19215. for (k = 0; k < colsNum; k++) {
  19216. if (this.indexTable[j][k] === undefined) {
  19217. row = rows[j];
  19218. cell = row.cells[row.cells.length - 1];
  19219. cell = cell
  19220. ? cell.cloneNode(true)
  19221. : this.table.ownerDocument.createElement("td");
  19222. this.setCellContent(cell);
  19223. if (cell.colSpan !== 1) cell.colSpan = 1;
  19224. if (cell.rowSpan !== 1) cell.rowSpan = 1;
  19225. row.appendChild(cell);
  19226. this.indexTable[j][k] = {
  19227. rowIndex: j,
  19228. cellIndex: cell.cellIndex,
  19229. colIndex: k,
  19230. rowSpan: 1,
  19231. colSpan: 1
  19232. };
  19233. }
  19234. }
  19235. }
  19236. //当框选后删除行或者列后撤销,需要重建选区。
  19237. var tds = domUtils.getElementsByTagName(this.table, "td"),
  19238. selectTds = [];
  19239. utils.each(tds, function(td) {
  19240. if (domUtils.hasClass(td, "selectTdClass")) {
  19241. selectTds.push(td);
  19242. }
  19243. });
  19244. if (selectTds.length) {
  19245. var start = selectTds[0],
  19246. end = selectTds[selectTds.length - 1],
  19247. startInfo = this.getCellInfo(start),
  19248. endInfo = this.getCellInfo(end);
  19249. this.selectedTds = selectTds;
  19250. this.cellsRange = {
  19251. beginRowIndex: startInfo.rowIndex,
  19252. beginColIndex: startInfo.colIndex,
  19253. endRowIndex: endInfo.rowIndex + endInfo.rowSpan - 1,
  19254. endColIndex: endInfo.colIndex + endInfo.colSpan - 1
  19255. };
  19256. }
  19257. //给第一行设置firstRow的样式名称,在排序图标的样式上使用到
  19258. if (!domUtils.hasClass(this.table.rows[0], "firstRow")) {
  19259. domUtils.addClass(this.table.rows[0], "firstRow");
  19260. for (var i = 1; i < this.table.rows.length; i++) {
  19261. domUtils.removeClasses(this.table.rows[i], "firstRow");
  19262. }
  19263. }
  19264. },
  19265. /**
  19266. * 获取单元格的索引信息
  19267. */
  19268. getCellInfo: function(cell) {
  19269. if (!cell) return;
  19270. var cellIndex = cell.cellIndex,
  19271. rowIndex = cell.parentNode.rowIndex,
  19272. rowInfo = this.indexTable[rowIndex],
  19273. numCols = this.colsNum;
  19274. for (var colIndex = cellIndex; colIndex < numCols; colIndex++) {
  19275. var cellInfo = rowInfo[colIndex];
  19276. if (
  19277. cellInfo.rowIndex === rowIndex &&
  19278. cellInfo.cellIndex === cellIndex
  19279. ) {
  19280. return cellInfo;
  19281. }
  19282. }
  19283. },
  19284. /**
  19285. * 根据行列号获取单元格
  19286. */
  19287. getCell: function(rowIndex, cellIndex) {
  19288. return (
  19289. (rowIndex < this.rowsNum &&
  19290. this.table.rows[rowIndex].cells[cellIndex]) ||
  19291. null
  19292. );
  19293. },
  19294. /**
  19295. * 删除单元格
  19296. */
  19297. deleteCell: function(cell, rowIndex) {
  19298. rowIndex = typeof rowIndex == "number"
  19299. ? rowIndex
  19300. : cell.parentNode.rowIndex;
  19301. var row = this.table.rows[rowIndex];
  19302. row.deleteCell(cell.cellIndex);
  19303. },
  19304. /**
  19305. * 根据始末两个单元格获取被框选的所有单元格范围
  19306. */
  19307. getCellsRange: function(cellA, cellB) {
  19308. function checkRange(
  19309. beginRowIndex,
  19310. beginColIndex,
  19311. endRowIndex,
  19312. endColIndex
  19313. ) {
  19314. var tmpBeginRowIndex = beginRowIndex,
  19315. tmpBeginColIndex = beginColIndex,
  19316. tmpEndRowIndex = endRowIndex,
  19317. tmpEndColIndex = endColIndex,
  19318. cellInfo,
  19319. colIndex,
  19320. rowIndex;
  19321. // 通过indexTable检查是否存在超出TableRange上边界的情况
  19322. if (beginRowIndex > 0) {
  19323. for (colIndex = beginColIndex; colIndex < endColIndex; colIndex++) {
  19324. cellInfo = me.indexTable[beginRowIndex][colIndex];
  19325. rowIndex = cellInfo.rowIndex;
  19326. if (rowIndex < beginRowIndex) {
  19327. tmpBeginRowIndex = Math.min(rowIndex, tmpBeginRowIndex);
  19328. }
  19329. }
  19330. }
  19331. // 通过indexTable检查是否存在超出TableRange右边界的情况
  19332. if (endColIndex < me.colsNum) {
  19333. for (rowIndex = beginRowIndex; rowIndex < endRowIndex; rowIndex++) {
  19334. cellInfo = me.indexTable[rowIndex][endColIndex];
  19335. colIndex = cellInfo.colIndex + cellInfo.colSpan - 1;
  19336. if (colIndex > endColIndex) {
  19337. tmpEndColIndex = Math.max(colIndex, tmpEndColIndex);
  19338. }
  19339. }
  19340. }
  19341. // 检查是否有超出TableRange下边界的情况
  19342. if (endRowIndex < me.rowsNum) {
  19343. for (colIndex = beginColIndex; colIndex < endColIndex; colIndex++) {
  19344. cellInfo = me.indexTable[endRowIndex][colIndex];
  19345. rowIndex = cellInfo.rowIndex + cellInfo.rowSpan - 1;
  19346. if (rowIndex > endRowIndex) {
  19347. tmpEndRowIndex = Math.max(rowIndex, tmpEndRowIndex);
  19348. }
  19349. }
  19350. }
  19351. // 检查是否有超出TableRange左边界的情况
  19352. if (beginColIndex > 0) {
  19353. for (rowIndex = beginRowIndex; rowIndex < endRowIndex; rowIndex++) {
  19354. cellInfo = me.indexTable[rowIndex][beginColIndex];
  19355. colIndex = cellInfo.colIndex;
  19356. if (colIndex < beginColIndex) {
  19357. tmpBeginColIndex = Math.min(cellInfo.colIndex, tmpBeginColIndex);
  19358. }
  19359. }
  19360. }
  19361. //递归调用直至所有完成所有框选单元格的扩展
  19362. if (
  19363. tmpBeginRowIndex != beginRowIndex ||
  19364. tmpBeginColIndex != beginColIndex ||
  19365. tmpEndRowIndex != endRowIndex ||
  19366. tmpEndColIndex != endColIndex
  19367. ) {
  19368. return checkRange(
  19369. tmpBeginRowIndex,
  19370. tmpBeginColIndex,
  19371. tmpEndRowIndex,
  19372. tmpEndColIndex
  19373. );
  19374. } else {
  19375. // 不需要扩展TableRange的情况
  19376. return {
  19377. beginRowIndex: beginRowIndex,
  19378. beginColIndex: beginColIndex,
  19379. endRowIndex: endRowIndex,
  19380. endColIndex: endColIndex
  19381. };
  19382. }
  19383. }
  19384. try {
  19385. var me = this,
  19386. cellAInfo = me.getCellInfo(cellA);
  19387. if (cellA === cellB) {
  19388. return {
  19389. beginRowIndex: cellAInfo.rowIndex,
  19390. beginColIndex: cellAInfo.colIndex,
  19391. endRowIndex: cellAInfo.rowIndex + cellAInfo.rowSpan - 1,
  19392. endColIndex: cellAInfo.colIndex + cellAInfo.colSpan - 1
  19393. };
  19394. }
  19395. var cellBInfo = me.getCellInfo(cellB);
  19396. // 计算TableRange的四个边
  19397. var beginRowIndex = Math.min(cellAInfo.rowIndex, cellBInfo.rowIndex),
  19398. beginColIndex = Math.min(cellAInfo.colIndex, cellBInfo.colIndex),
  19399. endRowIndex = Math.max(
  19400. cellAInfo.rowIndex + cellAInfo.rowSpan - 1,
  19401. cellBInfo.rowIndex + cellBInfo.rowSpan - 1
  19402. ),
  19403. endColIndex = Math.max(
  19404. cellAInfo.colIndex + cellAInfo.colSpan - 1,
  19405. cellBInfo.colIndex + cellBInfo.colSpan - 1
  19406. );
  19407. return checkRange(
  19408. beginRowIndex,
  19409. beginColIndex,
  19410. endRowIndex,
  19411. endColIndex
  19412. );
  19413. } catch (e) {
  19414. //throw e;
  19415. }
  19416. },
  19417. /**
  19418. * 依据cellsRange获取对应的单元格集合
  19419. */
  19420. getCells: function(range) {
  19421. //每次获取cells之前必须先清除上次的选择,否则会对后续获取操作造成影响
  19422. this.clearSelected();
  19423. var beginRowIndex = range.beginRowIndex,
  19424. beginColIndex = range.beginColIndex,
  19425. endRowIndex = range.endRowIndex,
  19426. endColIndex = range.endColIndex,
  19427. cellInfo,
  19428. rowIndex,
  19429. colIndex,
  19430. tdHash = {},
  19431. returnTds = [];
  19432. for (var i = beginRowIndex; i <= endRowIndex; i++) {
  19433. for (var j = beginColIndex; j <= endColIndex; j++) {
  19434. cellInfo = this.indexTable[i][j];
  19435. rowIndex = cellInfo.rowIndex;
  19436. colIndex = cellInfo.colIndex;
  19437. // 如果Cells里已经包含了此Cell则跳过
  19438. var key = rowIndex + "|" + colIndex;
  19439. if (tdHash[key]) continue;
  19440. tdHash[key] = 1;
  19441. if (
  19442. rowIndex < i ||
  19443. colIndex < j ||
  19444. rowIndex + cellInfo.rowSpan - 1 > endRowIndex ||
  19445. colIndex + cellInfo.colSpan - 1 > endColIndex
  19446. ) {
  19447. return null;
  19448. }
  19449. returnTds.push(this.getCell(rowIndex, cellInfo.cellIndex));
  19450. }
  19451. }
  19452. return returnTds;
  19453. },
  19454. /**
  19455. * 清理已经选中的单元格
  19456. */
  19457. clearSelected: function() {
  19458. UETable.removeSelectedClass(this.selectedTds);
  19459. this.selectedTds = [];
  19460. this.cellsRange = {};
  19461. },
  19462. /**
  19463. * 根据range设置已经选中的单元格
  19464. */
  19465. setSelected: function(range) {
  19466. var cells = this.getCells(range);
  19467. UETable.addSelectedClass(cells);
  19468. this.selectedTds = cells;
  19469. this.cellsRange = range;
  19470. },
  19471. isFullRow: function() {
  19472. var range = this.cellsRange;
  19473. return range.endColIndex - range.beginColIndex + 1 == this.colsNum;
  19474. },
  19475. isFullCol: function() {
  19476. var range = this.cellsRange,
  19477. table = this.table,
  19478. ths = table.getElementsByTagName("th"),
  19479. rows = range.endRowIndex - range.beginRowIndex + 1;
  19480. return !ths.length
  19481. ? rows == this.rowsNum
  19482. : rows == this.rowsNum || rows == this.rowsNum - 1;
  19483. },
  19484. /**
  19485. * 获取视觉上的前置单元格,默认是左边,top传入时
  19486. * @param cell
  19487. * @param top
  19488. */
  19489. getNextCell: function(cell, bottom, ignoreRange) {
  19490. try {
  19491. var cellInfo = this.getCellInfo(cell),
  19492. nextRowIndex,
  19493. nextColIndex;
  19494. var len = this.selectedTds.length && !ignoreRange,
  19495. range = this.cellsRange;
  19496. //末行或者末列没有后置单元格
  19497. if (
  19498. (!bottom && cellInfo.rowIndex == 0) ||
  19499. (bottom &&
  19500. (!len
  19501. ? cellInfo.rowIndex + cellInfo.rowSpan > this.rowsNum - 1
  19502. : range.endRowIndex == this.rowsNum - 1))
  19503. )
  19504. return null;
  19505. nextRowIndex = !bottom
  19506. ? !len ? cellInfo.rowIndex - 1 : range.beginRowIndex - 1
  19507. : !len ? cellInfo.rowIndex + cellInfo.rowSpan : range.endRowIndex + 1;
  19508. nextColIndex = !len ? cellInfo.colIndex : range.beginColIndex;
  19509. return this.getCell(
  19510. this.indexTable[nextRowIndex][nextColIndex].rowIndex,
  19511. this.indexTable[nextRowIndex][nextColIndex].cellIndex
  19512. );
  19513. } catch (e) {
  19514. showError(e);
  19515. }
  19516. },
  19517. getPreviewCell: function(cell, top) {
  19518. try {
  19519. var cellInfo = this.getCellInfo(cell),
  19520. previewRowIndex,
  19521. previewColIndex;
  19522. var len = this.selectedTds.length,
  19523. range = this.cellsRange;
  19524. //首行或者首列没有前置单元格
  19525. if (
  19526. (!top && (!len ? !cellInfo.colIndex : !range.beginColIndex)) ||
  19527. (top &&
  19528. (!len
  19529. ? cellInfo.rowIndex > this.colsNum - 1
  19530. : range.endColIndex == this.colsNum - 1))
  19531. )
  19532. return null;
  19533. previewRowIndex = !top
  19534. ? !len ? cellInfo.rowIndex : range.beginRowIndex
  19535. : !len
  19536. ? cellInfo.rowIndex < 1 ? 0 : cellInfo.rowIndex - 1
  19537. : range.beginRowIndex;
  19538. previewColIndex = !top
  19539. ? !len
  19540. ? cellInfo.colIndex < 1 ? 0 : cellInfo.colIndex - 1
  19541. : range.beginColIndex - 1
  19542. : !len ? cellInfo.colIndex : range.endColIndex + 1;
  19543. return this.getCell(
  19544. this.indexTable[previewRowIndex][previewColIndex].rowIndex,
  19545. this.indexTable[previewRowIndex][previewColIndex].cellIndex
  19546. );
  19547. } catch (e) {
  19548. showError(e);
  19549. }
  19550. },
  19551. /**
  19552. * 移动单元格中的内容
  19553. */
  19554. moveContent: function(cellTo, cellFrom) {
  19555. if (UETable.isEmptyBlock(cellFrom)) return;
  19556. if (UETable.isEmptyBlock(cellTo)) {
  19557. cellTo.innerHTML = cellFrom.innerHTML;
  19558. return;
  19559. }
  19560. var child = cellTo.lastChild;
  19561. if (child.nodeType == 3 || !dtd.$block[child.tagName]) {
  19562. cellTo.appendChild(cellTo.ownerDocument.createElement("br"));
  19563. }
  19564. while ((child = cellFrom.firstChild)) {
  19565. cellTo.appendChild(child);
  19566. }
  19567. },
  19568. /**
  19569. * 向右合并单元格
  19570. */
  19571. mergeRight: function(cell) {
  19572. var cellInfo = this.getCellInfo(cell),
  19573. rightColIndex = cellInfo.colIndex + cellInfo.colSpan,
  19574. rightCellInfo = this.indexTable[cellInfo.rowIndex][rightColIndex],
  19575. rightCell = this.getCell(
  19576. rightCellInfo.rowIndex,
  19577. rightCellInfo.cellIndex
  19578. );
  19579. //合并
  19580. cell.colSpan = cellInfo.colSpan + rightCellInfo.colSpan;
  19581. //被合并的单元格不应存在宽度属性
  19582. cell.removeAttribute("width");
  19583. //移动内容
  19584. this.moveContent(cell, rightCell);
  19585. //删掉被合并的Cell
  19586. this.deleteCell(rightCell, rightCellInfo.rowIndex);
  19587. this.update();
  19588. },
  19589. /**
  19590. * 向下合并单元格
  19591. */
  19592. mergeDown: function(cell) {
  19593. var cellInfo = this.getCellInfo(cell),
  19594. downRowIndex = cellInfo.rowIndex + cellInfo.rowSpan,
  19595. downCellInfo = this.indexTable[downRowIndex][cellInfo.colIndex],
  19596. downCell = this.getCell(downCellInfo.rowIndex, downCellInfo.cellIndex);
  19597. cell.rowSpan = cellInfo.rowSpan + downCellInfo.rowSpan;
  19598. cell.removeAttribute("height");
  19599. this.moveContent(cell, downCell);
  19600. this.deleteCell(downCell, downCellInfo.rowIndex);
  19601. this.update();
  19602. },
  19603. /**
  19604. * 合并整个range中的内容
  19605. */
  19606. mergeRange: function() {
  19607. //由于合并操作可以在任意时刻进行,所以无法通过鼠标位置等信息实时生成range,只能通过缓存实例中的cellsRange对象来访问
  19608. var range = this.cellsRange,
  19609. leftTopCell = this.getCell(
  19610. range.beginRowIndex,
  19611. this.indexTable[range.beginRowIndex][range.beginColIndex].cellIndex
  19612. );
  19613. // 这段关于行表头或者列表头的特殊处理会导致表头合并范围错误
  19614. // 为什么有这段代码的原因未明,暂且注释掉,希望原作者看到后出面说明下
  19615. // if (
  19616. // leftTopCell.tagName == "TH" &&
  19617. // range.endRowIndex !== range.beginRowIndex
  19618. // ) {
  19619. // var index = this.indexTable,
  19620. // info = this.getCellInfo(leftTopCell);
  19621. // leftTopCell = this.getCell(1, index[1][info.colIndex].cellIndex);
  19622. // range = this.getCellsRange(
  19623. // leftTopCell,
  19624. // this.getCell(
  19625. // index[this.rowsNum - 1][info.colIndex].rowIndex,
  19626. // index[this.rowsNum - 1][info.colIndex].cellIndex
  19627. // )
  19628. // );
  19629. // }
  19630. // 删除剩余的Cells
  19631. var cells = this.getCells(range);
  19632. for (var i = 0, ci; (ci = cells[i++]); ) {
  19633. if (ci !== leftTopCell) {
  19634. this.moveContent(leftTopCell, ci);
  19635. this.deleteCell(ci);
  19636. }
  19637. }
  19638. // 修改左上角Cell的rowSpan和colSpan,并调整宽度属性设置
  19639. leftTopCell.rowSpan = range.endRowIndex - range.beginRowIndex + 1;
  19640. leftTopCell.rowSpan > 1 && leftTopCell.removeAttribute("height");
  19641. leftTopCell.colSpan = range.endColIndex - range.beginColIndex + 1;
  19642. leftTopCell.colSpan > 1 && leftTopCell.removeAttribute("width");
  19643. if (leftTopCell.rowSpan == this.rowsNum && leftTopCell.colSpan != 1) {
  19644. leftTopCell.colSpan = 1;
  19645. }
  19646. if (leftTopCell.colSpan == this.colsNum && leftTopCell.rowSpan != 1) {
  19647. var rowIndex = leftTopCell.parentNode.rowIndex;
  19648. //解决IE下的表格操作问题
  19649. if (this.table.deleteRow) {
  19650. for (
  19651. var i = rowIndex + 1,
  19652. curIndex = rowIndex + 1,
  19653. len = leftTopCell.rowSpan;
  19654. i < len;
  19655. i++
  19656. ) {
  19657. this.table.deleteRow(curIndex);
  19658. }
  19659. } else {
  19660. for (var i = 0, len = leftTopCell.rowSpan - 1; i < len; i++) {
  19661. var row = this.table.rows[rowIndex + 1];
  19662. row.parentNode.removeChild(row);
  19663. }
  19664. }
  19665. leftTopCell.rowSpan = 1;
  19666. }
  19667. this.update();
  19668. },
  19669. /**
  19670. * 插入一行单元格
  19671. */
  19672. insertRow: function(rowIndex, sourceCell) {
  19673. var numCols = this.colsNum,
  19674. table = this.table,
  19675. row = table.insertRow(rowIndex),
  19676. cell,
  19677. thead = null,
  19678. isInsertTitle =
  19679. typeof sourceCell == "string" && sourceCell.toUpperCase() == "TH";
  19680. function replaceTdToTh(colIndex, cell, tableRow) {
  19681. if (colIndex == 0) {
  19682. var tr = tableRow.nextSibling || tableRow.previousSibling,
  19683. th = tr.cells[colIndex];
  19684. if (th.tagName == "TH") {
  19685. th = cell.ownerDocument.createElement("th");
  19686. th.appendChild(cell.firstChild);
  19687. tableRow.insertBefore(th, cell);
  19688. domUtils.remove(cell);
  19689. }
  19690. } else {
  19691. if (cell.tagName == "TH") {
  19692. var td = cell.ownerDocument.createElement("td");
  19693. td.appendChild(cell.firstChild);
  19694. tableRow.insertBefore(td, cell);
  19695. domUtils.remove(cell);
  19696. }
  19697. }
  19698. }
  19699. //首行直接插入,无需考虑部分单元格被rowspan的情况
  19700. if (rowIndex == 0 || rowIndex == this.rowsNum) {
  19701. for (var colIndex = 0; colIndex < numCols; colIndex++) {
  19702. cell = this.cloneCell(sourceCell, true);
  19703. this.setCellContent(cell);
  19704. cell.getAttribute("vAlign") &&
  19705. cell.setAttribute("vAlign", cell.getAttribute("vAlign"));
  19706. row.appendChild(cell);
  19707. if (!isInsertTitle) replaceTdToTh(colIndex, cell, row);
  19708. }
  19709. if (isInsertTitle) {
  19710. thead = table.createTHead();
  19711. thead.insertBefore(row, thead.firstChild);
  19712. }
  19713. } else {
  19714. var infoRow = this.indexTable[rowIndex],
  19715. cellIndex = 0;
  19716. for (colIndex = 0; colIndex < numCols; colIndex++) {
  19717. var cellInfo = infoRow[colIndex];
  19718. //如果存在某个单元格的rowspan穿过待插入行的位置,则修改该单元格的rowspan即可,无需插入单元格
  19719. if (cellInfo.rowIndex < rowIndex) {
  19720. cell = this.getCell(cellInfo.rowIndex, cellInfo.cellIndex);
  19721. cell.rowSpan = cellInfo.rowSpan + 1;
  19722. } else {
  19723. cell = this.cloneCell(sourceCell, true);
  19724. this.setCellContent(cell);
  19725. row.appendChild(cell);
  19726. }
  19727. if (!isInsertTitle) replaceTdToTh(colIndex, cell, row);
  19728. }
  19729. }
  19730. //框选时插入不触发contentchange,需要手动更新索引。
  19731. this.update();
  19732. return row;
  19733. },
  19734. /**
  19735. * 删除一行单元格
  19736. * @param rowIndex
  19737. */
  19738. deleteRow: function(rowIndex) {
  19739. var row = this.table.rows[rowIndex],
  19740. infoRow = this.indexTable[rowIndex],
  19741. colsNum = this.colsNum,
  19742. count = 0; //处理计数
  19743. for (var colIndex = 0; colIndex < colsNum; ) {
  19744. var cellInfo = infoRow[colIndex],
  19745. cell = this.getCell(cellInfo.rowIndex, cellInfo.cellIndex);
  19746. if (cell.rowSpan > 1) {
  19747. if (cellInfo.rowIndex == rowIndex) {
  19748. var clone = cell.cloneNode(true);
  19749. clone.rowSpan = cell.rowSpan - 1;
  19750. clone.innerHTML = "";
  19751. cell.rowSpan = 1;
  19752. var nextRowIndex = rowIndex + 1,
  19753. nextRow = this.table.rows[nextRowIndex],
  19754. insertCellIndex,
  19755. preMerged =
  19756. this.getPreviewMergedCellsNum(nextRowIndex, colIndex) - count;
  19757. if (preMerged < colIndex) {
  19758. insertCellIndex = colIndex - preMerged - 1;
  19759. //nextRow.insertCell(insertCellIndex);
  19760. domUtils.insertAfter(nextRow.cells[insertCellIndex], clone);
  19761. } else {
  19762. if (nextRow.cells.length)
  19763. nextRow.insertBefore(clone, nextRow.cells[0]);
  19764. }
  19765. count += 1;
  19766. //cell.parentNode.removeChild(cell);
  19767. }
  19768. }
  19769. colIndex += cell.colSpan || 1;
  19770. }
  19771. var deleteTds = [],
  19772. cacheMap = {};
  19773. for (colIndex = 0; colIndex < colsNum; colIndex++) {
  19774. var tmpRowIndex = infoRow[colIndex].rowIndex,
  19775. tmpCellIndex = infoRow[colIndex].cellIndex,
  19776. key = tmpRowIndex + "_" + tmpCellIndex;
  19777. if (cacheMap[key]) continue;
  19778. cacheMap[key] = 1;
  19779. cell = this.getCell(tmpRowIndex, tmpCellIndex);
  19780. deleteTds.push(cell);
  19781. }
  19782. var mergeTds = [];
  19783. utils.each(deleteTds, function(td) {
  19784. if (td.rowSpan == 1) {
  19785. td.parentNode.removeChild(td);
  19786. } else {
  19787. mergeTds.push(td);
  19788. }
  19789. });
  19790. utils.each(mergeTds, function(td) {
  19791. td.rowSpan--;
  19792. });
  19793. row.parentNode.removeChild(row);
  19794. //浏览器方法本身存在bug,采用自定义方法删除
  19795. //this.table.deleteRow(rowIndex);
  19796. this.update();
  19797. },
  19798. insertCol: function(colIndex, sourceCell, defaultValue) {
  19799. var rowsNum = this.rowsNum,
  19800. rowIndex = 0,
  19801. tableRow,
  19802. cell,
  19803. backWidth = parseInt(
  19804. (this.table.offsetWidth -
  19805. (this.colsNum + 1) * 20 -
  19806. (this.colsNum + 1)) /
  19807. (this.colsNum + 1),
  19808. 10
  19809. ),
  19810. isInsertTitleCol =
  19811. typeof sourceCell == "string" && sourceCell.toUpperCase() == "TH";
  19812. function replaceTdToTh(rowIndex, cell, tableRow) {
  19813. if (rowIndex == 0) {
  19814. var th = cell.nextSibling || cell.previousSibling;
  19815. if (th.tagName == "TH") {
  19816. th = cell.ownerDocument.createElement("th");
  19817. th.appendChild(cell.firstChild);
  19818. tableRow.insertBefore(th, cell);
  19819. domUtils.remove(cell);
  19820. }
  19821. } else {
  19822. if (cell.tagName == "TH") {
  19823. var td = cell.ownerDocument.createElement("td");
  19824. td.appendChild(cell.firstChild);
  19825. tableRow.insertBefore(td, cell);
  19826. domUtils.remove(cell);
  19827. }
  19828. }
  19829. }
  19830. var preCell;
  19831. if (colIndex == 0 || colIndex == this.colsNum) {
  19832. for (; rowIndex < rowsNum; rowIndex++) {
  19833. tableRow = this.table.rows[rowIndex];
  19834. preCell =
  19835. tableRow.cells[colIndex == 0 ? colIndex : tableRow.cells.length];
  19836. cell = this.cloneCell(sourceCell, true); //tableRow.insertCell(colIndex == 0 ? colIndex : tableRow.cells.length);
  19837. this.setCellContent(cell);
  19838. cell.setAttribute("vAlign", cell.getAttribute("vAlign"));
  19839. preCell && cell.setAttribute("width", preCell.getAttribute("width"));
  19840. if (!colIndex) {
  19841. tableRow.insertBefore(cell, tableRow.cells[0]);
  19842. } else {
  19843. domUtils.insertAfter(
  19844. tableRow.cells[tableRow.cells.length - 1],
  19845. cell
  19846. );
  19847. }
  19848. if (!isInsertTitleCol) replaceTdToTh(rowIndex, cell, tableRow);
  19849. }
  19850. } else {
  19851. for (; rowIndex < rowsNum; rowIndex++) {
  19852. var cellInfo = this.indexTable[rowIndex][colIndex];
  19853. if (cellInfo.colIndex < colIndex) {
  19854. cell = this.getCell(cellInfo.rowIndex, cellInfo.cellIndex);
  19855. cell.colSpan = cellInfo.colSpan + 1;
  19856. } else {
  19857. tableRow = this.table.rows[rowIndex];
  19858. preCell = tableRow.cells[cellInfo.cellIndex];
  19859. cell = this.cloneCell(sourceCell, true); //tableRow.insertCell(cellInfo.cellIndex);
  19860. this.setCellContent(cell);
  19861. cell.setAttribute("vAlign", cell.getAttribute("vAlign"));
  19862. preCell &&
  19863. cell.setAttribute("width", preCell.getAttribute("width"));
  19864. //防止IE下报错
  19865. preCell
  19866. ? tableRow.insertBefore(cell, preCell)
  19867. : tableRow.appendChild(cell);
  19868. }
  19869. if (!isInsertTitleCol) replaceTdToTh(rowIndex, cell, tableRow);
  19870. }
  19871. }
  19872. //框选时插入不触发contentchange,需要手动更新索引
  19873. this.update();
  19874. this.updateWidth(
  19875. backWidth,
  19876. defaultValue || { tdPadding: 10, tdBorder: 1 }
  19877. );
  19878. },
  19879. updateWidth: function(width, defaultValue) {
  19880. var table = this.table,
  19881. tmpWidth =
  19882. UETable.getWidth(table) -
  19883. defaultValue.tdPadding * 2 -
  19884. defaultValue.tdBorder +
  19885. width;
  19886. if (tmpWidth < table.ownerDocument.body.offsetWidth) {
  19887. table.setAttribute("width", tmpWidth);
  19888. return;
  19889. }
  19890. var tds = domUtils.getElementsByTagName(this.table, "td th");
  19891. utils.each(tds, function(td) {
  19892. td.setAttribute("width", width);
  19893. });
  19894. },
  19895. deleteCol: function(colIndex) {
  19896. var indexTable = this.indexTable,
  19897. tableRows = this.table.rows,
  19898. backTableWidth = this.table.getAttribute("width"),
  19899. backTdWidth = 0,
  19900. rowsNum = this.rowsNum,
  19901. cacheMap = {};
  19902. for (var rowIndex = 0; rowIndex < rowsNum; ) {
  19903. var infoRow = indexTable[rowIndex],
  19904. cellInfo = infoRow[colIndex],
  19905. key = cellInfo.rowIndex + "_" + cellInfo.colIndex;
  19906. // 跳过已经处理过的Cell
  19907. if (cacheMap[key]) continue;
  19908. cacheMap[key] = 1;
  19909. var cell = this.getCell(cellInfo.rowIndex, cellInfo.cellIndex);
  19910. if (!backTdWidth)
  19911. backTdWidth =
  19912. cell && parseInt(cell.offsetWidth / cell.colSpan, 10).toFixed(0);
  19913. // 如果Cell的colSpan大于1, 就修改colSpan, 否则就删掉这个Cell
  19914. if (cell.colSpan > 1) {
  19915. cell.colSpan--;
  19916. } else {
  19917. tableRows[rowIndex].deleteCell(cellInfo.cellIndex);
  19918. }
  19919. rowIndex += cellInfo.rowSpan || 1;
  19920. }
  19921. this.table.setAttribute("width", backTableWidth - backTdWidth);
  19922. this.update();
  19923. },
  19924. splitToCells: function(cell) {
  19925. var me = this,
  19926. cells = this.splitToRows(cell);
  19927. utils.each(cells, function(cell) {
  19928. me.splitToCols(cell);
  19929. });
  19930. },
  19931. splitToRows: function(cell) {
  19932. var cellInfo = this.getCellInfo(cell),
  19933. rowIndex = cellInfo.rowIndex,
  19934. colIndex = cellInfo.colIndex,
  19935. results = [];
  19936. // 修改Cell的rowSpan
  19937. cell.rowSpan = 1;
  19938. results.push(cell);
  19939. // 补齐单元格
  19940. for (
  19941. var i = rowIndex, endRow = rowIndex + cellInfo.rowSpan;
  19942. i < endRow;
  19943. i++
  19944. ) {
  19945. if (i == rowIndex) continue;
  19946. var tableRow = this.table.rows[i],
  19947. tmpCell = tableRow.insertCell(
  19948. colIndex - this.getPreviewMergedCellsNum(i, colIndex)
  19949. );
  19950. tmpCell.colSpan = cellInfo.colSpan;
  19951. this.setCellContent(tmpCell);
  19952. tmpCell.setAttribute("vAlign", cell.getAttribute("vAlign"));
  19953. tmpCell.setAttribute("align", cell.getAttribute("align"));
  19954. if (cell.style.cssText) {
  19955. tmpCell.style.cssText = cell.style.cssText;
  19956. }
  19957. results.push(tmpCell);
  19958. }
  19959. this.update();
  19960. return results;
  19961. },
  19962. getPreviewMergedCellsNum: function(rowIndex, colIndex) {
  19963. var indexRow = this.indexTable[rowIndex],
  19964. num = 0;
  19965. for (var i = 0; i < colIndex; ) {
  19966. var colSpan = indexRow[i].colSpan,
  19967. tmpRowIndex = indexRow[i].rowIndex;
  19968. num += colSpan - (tmpRowIndex == rowIndex ? 1 : 0);
  19969. i += colSpan;
  19970. }
  19971. return num;
  19972. },
  19973. splitToCols: function(cell) {
  19974. var backWidth = (cell.offsetWidth / cell.colSpan - 22).toFixed(0),
  19975. cellInfo = this.getCellInfo(cell),
  19976. rowIndex = cellInfo.rowIndex,
  19977. colIndex = cellInfo.colIndex,
  19978. results = [];
  19979. // 修改Cell的rowSpan
  19980. cell.colSpan = 1;
  19981. cell.setAttribute("width", backWidth);
  19982. results.push(cell);
  19983. // 补齐单元格
  19984. for (
  19985. var j = colIndex, endCol = colIndex + cellInfo.colSpan;
  19986. j < endCol;
  19987. j++
  19988. ) {
  19989. if (j == colIndex) continue;
  19990. var tableRow = this.table.rows[rowIndex],
  19991. tmpCell = tableRow.insertCell(
  19992. this.indexTable[rowIndex][j].cellIndex + 1
  19993. );
  19994. tmpCell.rowSpan = cellInfo.rowSpan;
  19995. this.setCellContent(tmpCell);
  19996. tmpCell.setAttribute("vAlign", cell.getAttribute("vAlign"));
  19997. tmpCell.setAttribute("align", cell.getAttribute("align"));
  19998. tmpCell.setAttribute("width", backWidth);
  19999. if (cell.style.cssText) {
  20000. tmpCell.style.cssText = cell.style.cssText;
  20001. }
  20002. //处理th的情况
  20003. if (cell.tagName == "TH") {
  20004. var th = cell.ownerDocument.createElement("th");
  20005. th.appendChild(tmpCell.firstChild);
  20006. th.setAttribute("vAlign", cell.getAttribute("vAlign"));
  20007. th.rowSpan = tmpCell.rowSpan;
  20008. tableRow.insertBefore(th, tmpCell);
  20009. domUtils.remove(tmpCell);
  20010. }
  20011. results.push(tmpCell);
  20012. }
  20013. this.update();
  20014. return results;
  20015. },
  20016. isLastCell: function(cell, rowsNum, colsNum) {
  20017. rowsNum = rowsNum || this.rowsNum;
  20018. colsNum = colsNum || this.colsNum;
  20019. var cellInfo = this.getCellInfo(cell);
  20020. return (
  20021. cellInfo.rowIndex + cellInfo.rowSpan == rowsNum &&
  20022. cellInfo.colIndex + cellInfo.colSpan == colsNum
  20023. );
  20024. },
  20025. getLastCell: function(cells) {
  20026. cells = cells || this.table.getElementsByTagName("td");
  20027. var firstInfo = this.getCellInfo(cells[0]);
  20028. var me = this,
  20029. last = cells[0],
  20030. tr = last.parentNode,
  20031. cellsNum = 0,
  20032. cols = 0,
  20033. rows;
  20034. utils.each(cells, function(cell) {
  20035. if (cell.parentNode == tr) cols += cell.colSpan || 1;
  20036. cellsNum += cell.rowSpan * cell.colSpan || 1;
  20037. });
  20038. rows = cellsNum / cols;
  20039. utils.each(cells, function(cell) {
  20040. if (me.isLastCell(cell, rows, cols)) {
  20041. last = cell;
  20042. return false;
  20043. }
  20044. });
  20045. return last;
  20046. },
  20047. selectRow: function(rowIndex) {
  20048. var indexRow = this.indexTable[rowIndex],
  20049. start = this.getCell(indexRow[0].rowIndex, indexRow[0].cellIndex),
  20050. end = this.getCell(
  20051. indexRow[this.colsNum - 1].rowIndex,
  20052. indexRow[this.colsNum - 1].cellIndex
  20053. ),
  20054. range = this.getCellsRange(start, end);
  20055. this.setSelected(range);
  20056. },
  20057. selectTable: function() {
  20058. var tds = this.table.getElementsByTagName("td"),
  20059. range = this.getCellsRange(tds[0], tds[tds.length - 1]);
  20060. this.setSelected(range);
  20061. },
  20062. setBackground: function(cells, value) {
  20063. if (typeof value === "string") {
  20064. utils.each(cells, function(cell) {
  20065. cell.style.backgroundColor = value;
  20066. });
  20067. } else if (typeof value === "object") {
  20068. value = utils.extend(
  20069. {
  20070. repeat: true,
  20071. colorList: ["#ddd", "#fff"]
  20072. },
  20073. value
  20074. );
  20075. var rowIndex = this.getCellInfo(cells[0]).rowIndex,
  20076. count = 0,
  20077. colors = value.colorList,
  20078. getColor = function(list, index, repeat) {
  20079. return list[index]
  20080. ? list[index]
  20081. : repeat ? list[index % list.length] : "";
  20082. };
  20083. for (var i = 0, cell; (cell = cells[i++]); ) {
  20084. var cellInfo = this.getCellInfo(cell);
  20085. cell.style.backgroundColor = getColor(
  20086. colors,
  20087. rowIndex + count == cellInfo.rowIndex ? count : ++count,
  20088. value.repeat
  20089. );
  20090. }
  20091. }
  20092. },
  20093. removeBackground: function(cells) {
  20094. utils.each(cells, function(cell) {
  20095. cell.style.backgroundColor = "";
  20096. });
  20097. }
  20098. };
  20099. function showError(e) {}
  20100. })();
  20101. // plugins/table.cmds.js
  20102. /**
  20103. * Created with JetBrains PhpStorm.
  20104. * User: taoqili
  20105. * Date: 13-2-20
  20106. * Time: 下午6:25
  20107. * To change this template use File | Settings | File Templates.
  20108. */
  20109. (function() {
  20110. var UT = UE.UETable,
  20111. getTableItemsByRange = function(editor) {
  20112. return UT.getTableItemsByRange(editor);
  20113. },
  20114. getUETableBySelected = function(editor) {
  20115. return UT.getUETableBySelected(editor);
  20116. },
  20117. getDefaultValue = function(editor, table) {
  20118. return UT.getDefaultValue(editor, table);
  20119. },
  20120. getUETable = function(tdOrTable) {
  20121. return UT.getUETable(tdOrTable);
  20122. };
  20123. UE.commands["inserttable"] = {
  20124. queryCommandState: function() {
  20125. return getTableItemsByRange(this).table ? -1 : 0;
  20126. },
  20127. execCommand: function(cmd, opt) {
  20128. function createTable(opt, tdWidth) {
  20129. var html = [],
  20130. rowsNum = opt.numRows,
  20131. colsNum = opt.numCols;
  20132. for (var r = 0; r < rowsNum; r++) {
  20133. html.push("<tr" + (r == 0 ? ' class="firstRow"' : "") + ">");
  20134. for (var c = 0; c < colsNum; c++) {
  20135. html.push(
  20136. '<td width="' +
  20137. tdWidth +
  20138. '" vAlign="' +
  20139. opt.tdvalign +
  20140. '" >' +
  20141. (browser.ie && browser.version < 11
  20142. ? domUtils.fillChar
  20143. : "<br/>") +
  20144. "</td>"
  20145. );
  20146. }
  20147. html.push("</tr>");
  20148. }
  20149. //禁止指定table-width
  20150. return "<table><tbody>" + html.join("") + "</tbody></table>";
  20151. }
  20152. if (!opt) {
  20153. opt = utils.extend(
  20154. {},
  20155. {
  20156. numCols: this.options.defaultCols,
  20157. numRows: this.options.defaultRows,
  20158. tdvalign: this.options.tdvalign
  20159. }
  20160. );
  20161. }
  20162. var me = this;
  20163. var range = this.selection.getRange(),
  20164. start = range.startContainer,
  20165. firstParentBlock =
  20166. domUtils.findParent(
  20167. start,
  20168. function(node) {
  20169. return domUtils.isBlockElm(node);
  20170. },
  20171. true
  20172. ) || me.body;
  20173. var defaultValue = getDefaultValue(me),
  20174. tableWidth = firstParentBlock.offsetWidth,
  20175. tdWidth = Math.floor(
  20176. tableWidth / opt.numCols -
  20177. defaultValue.tdPadding * 2 -
  20178. defaultValue.tdBorder
  20179. );
  20180. //todo其他属性
  20181. !opt.tdvalign && (opt.tdvalign = me.options.tdvalign);
  20182. me.execCommand("inserthtml", createTable(opt, tdWidth));
  20183. }
  20184. };
  20185. UE.commands["insertparagraphbeforetable"] = {
  20186. queryCommandState: function() {
  20187. return getTableItemsByRange(this).cell ? 0 : -1;
  20188. },
  20189. execCommand: function() {
  20190. var table = getTableItemsByRange(this).table;
  20191. if (table) {
  20192. var p = this.document.createElement("p");
  20193. p.innerHTML = browser.ie ? "&nbsp;" : "<br />";
  20194. table.parentNode.insertBefore(p, table);
  20195. this.selection.getRange().setStart(p, 0).setCursor();
  20196. }
  20197. }
  20198. };
  20199. UE.commands["deletetable"] = {
  20200. queryCommandState: function() {
  20201. var rng = this.selection.getRange();
  20202. return domUtils.findParentByTagName(rng.startContainer, "table", true)
  20203. ? 0
  20204. : -1;
  20205. },
  20206. execCommand: function(cmd, table) {
  20207. var rng = this.selection.getRange();
  20208. table =
  20209. table ||
  20210. domUtils.findParentByTagName(rng.startContainer, "table", true);
  20211. if (table) {
  20212. var next = table.nextSibling;
  20213. if (!next) {
  20214. next = domUtils.createElement(this.document, "p", {
  20215. innerHTML: browser.ie ? domUtils.fillChar : "<br/>"
  20216. });
  20217. table.parentNode.insertBefore(next, table);
  20218. }
  20219. domUtils.remove(table);
  20220. rng = this.selection.getRange();
  20221. if (next.nodeType == 3) {
  20222. rng.setStartBefore(next);
  20223. } else {
  20224. rng.setStart(next, 0);
  20225. }
  20226. rng.setCursor(false, true);
  20227. this.fireEvent("tablehasdeleted");
  20228. }
  20229. }
  20230. };
  20231. UE.commands["cellalign"] = {
  20232. queryCommandState: function() {
  20233. return getSelectedArr(this).length ? 0 : -1;
  20234. },
  20235. execCommand: function(cmd, align) {
  20236. var selectedTds = getSelectedArr(this);
  20237. if (selectedTds.length) {
  20238. for (var i = 0, ci; (ci = selectedTds[i++]); ) {
  20239. ci.setAttribute("align", align);
  20240. }
  20241. }
  20242. }
  20243. };
  20244. UE.commands["cellvalign"] = {
  20245. queryCommandState: function() {
  20246. return getSelectedArr(this).length ? 0 : -1;
  20247. },
  20248. execCommand: function(cmd, valign) {
  20249. var selectedTds = getSelectedArr(this);
  20250. if (selectedTds.length) {
  20251. for (var i = 0, ci; (ci = selectedTds[i++]); ) {
  20252. ci.setAttribute("vAlign", valign);
  20253. }
  20254. }
  20255. }
  20256. };
  20257. UE.commands["insertcaption"] = {
  20258. queryCommandState: function() {
  20259. var table = getTableItemsByRange(this).table;
  20260. if (table) {
  20261. return table.getElementsByTagName("caption").length == 0 ? 1 : -1;
  20262. }
  20263. return -1;
  20264. },
  20265. execCommand: function() {
  20266. var table = getTableItemsByRange(this).table;
  20267. if (table) {
  20268. var caption = this.document.createElement("caption");
  20269. caption.innerHTML = browser.ie ? domUtils.fillChar : "<br/>";
  20270. table.insertBefore(caption, table.firstChild);
  20271. var range = this.selection.getRange();
  20272. range.setStart(caption, 0).setCursor();
  20273. }
  20274. }
  20275. };
  20276. UE.commands["deletecaption"] = {
  20277. queryCommandState: function() {
  20278. var rng = this.selection.getRange(),
  20279. table = domUtils.findParentByTagName(rng.startContainer, "table");
  20280. if (table) {
  20281. return table.getElementsByTagName("caption").length == 0 ? -1 : 1;
  20282. }
  20283. return -1;
  20284. },
  20285. execCommand: function() {
  20286. var rng = this.selection.getRange(),
  20287. table = domUtils.findParentByTagName(rng.startContainer, "table");
  20288. if (table) {
  20289. domUtils.remove(table.getElementsByTagName("caption")[0]);
  20290. var range = this.selection.getRange();
  20291. range.setStart(table.rows[0].cells[0], 0).setCursor();
  20292. }
  20293. }
  20294. };
  20295. UE.commands["inserttitle"] = {
  20296. queryCommandState: function() {
  20297. var table = getTableItemsByRange(this).table;
  20298. if (table) {
  20299. var firstRow = table.rows[0];
  20300. return firstRow.cells[
  20301. firstRow.cells.length - 1
  20302. ].tagName.toLowerCase() != "th"
  20303. ? 0
  20304. : -1;
  20305. }
  20306. return -1;
  20307. },
  20308. execCommand: function() {
  20309. var table = getTableItemsByRange(this).table;
  20310. if (table) {
  20311. getUETable(table).insertRow(0, "th");
  20312. }
  20313. var th = table.getElementsByTagName("th")[0];
  20314. this.selection.getRange().setStart(th, 0).setCursor(false, true);
  20315. }
  20316. };
  20317. UE.commands["deletetitle"] = {
  20318. queryCommandState: function() {
  20319. var table = getTableItemsByRange(this).table;
  20320. if (table) {
  20321. var firstRow = table.rows[0];
  20322. return firstRow.cells[
  20323. firstRow.cells.length - 1
  20324. ].tagName.toLowerCase() == "th"
  20325. ? 0
  20326. : -1;
  20327. }
  20328. return -1;
  20329. },
  20330. execCommand: function() {
  20331. var table = getTableItemsByRange(this).table;
  20332. if (table) {
  20333. domUtils.remove(table.rows[0]);
  20334. }
  20335. var td = table.getElementsByTagName("td")[0];
  20336. this.selection.getRange().setStart(td, 0).setCursor(false, true);
  20337. }
  20338. };
  20339. UE.commands["inserttitlecol"] = {
  20340. queryCommandState: function() {
  20341. var table = getTableItemsByRange(this).table;
  20342. if (table) {
  20343. var lastRow = table.rows[table.rows.length - 1];
  20344. return lastRow.getElementsByTagName("th").length ? -1 : 0;
  20345. }
  20346. return -1;
  20347. },
  20348. execCommand: function(cmd) {
  20349. var table = getTableItemsByRange(this).table;
  20350. if (table) {
  20351. getUETable(table).insertCol(0, "th");
  20352. }
  20353. resetTdWidth(table, this);
  20354. var th = table.getElementsByTagName("th")[0];
  20355. this.selection.getRange().setStart(th, 0).setCursor(false, true);
  20356. }
  20357. };
  20358. UE.commands["deletetitlecol"] = {
  20359. queryCommandState: function() {
  20360. var table = getTableItemsByRange(this).table;
  20361. if (table) {
  20362. var lastRow = table.rows[table.rows.length - 1];
  20363. return lastRow.getElementsByTagName("th").length ? 0 : -1;
  20364. }
  20365. return -1;
  20366. },
  20367. execCommand: function() {
  20368. var table = getTableItemsByRange(this).table;
  20369. if (table) {
  20370. for (var i = 0; i < table.rows.length; i++) {
  20371. domUtils.remove(table.rows[i].children[0]);
  20372. }
  20373. }
  20374. resetTdWidth(table, this);
  20375. var td = table.getElementsByTagName("td")[0];
  20376. this.selection.getRange().setStart(td, 0).setCursor(false, true);
  20377. }
  20378. };
  20379. UE.commands["mergeright"] = {
  20380. queryCommandState: function(cmd) {
  20381. var tableItems = getTableItemsByRange(this),
  20382. table = tableItems.table,
  20383. cell = tableItems.cell;
  20384. if (!table || !cell) return -1;
  20385. var ut = getUETable(table);
  20386. if (ut.selectedTds.length) return -1;
  20387. var cellInfo = ut.getCellInfo(cell),
  20388. rightColIndex = cellInfo.colIndex + cellInfo.colSpan;
  20389. if (rightColIndex >= ut.colsNum) return -1; // 如果处于最右边则不能向右合并
  20390. var rightCellInfo = ut.indexTable[cellInfo.rowIndex][rightColIndex],
  20391. rightCell =
  20392. table.rows[rightCellInfo.rowIndex].cells[rightCellInfo.cellIndex];
  20393. if (!rightCell || cell.tagName != rightCell.tagName) return -1; // TH和TD不能相互合并
  20394. // 当且仅当两个Cell的开始列号和结束列号一致时能进行合并
  20395. return rightCellInfo.rowIndex == cellInfo.rowIndex &&
  20396. rightCellInfo.rowSpan == cellInfo.rowSpan
  20397. ? 0
  20398. : -1;
  20399. },
  20400. execCommand: function(cmd) {
  20401. var rng = this.selection.getRange(),
  20402. bk = rng.createBookmark(true);
  20403. var cell = getTableItemsByRange(this).cell,
  20404. ut = getUETable(cell);
  20405. ut.mergeRight(cell);
  20406. rng.moveToBookmark(bk).select();
  20407. }
  20408. };
  20409. UE.commands["mergedown"] = {
  20410. queryCommandState: function(cmd) {
  20411. var tableItems = getTableItemsByRange(this),
  20412. table = tableItems.table,
  20413. cell = tableItems.cell;
  20414. if (!table || !cell) return -1;
  20415. var ut = getUETable(table);
  20416. if (ut.selectedTds.length) return -1;
  20417. var cellInfo = ut.getCellInfo(cell),
  20418. downRowIndex = cellInfo.rowIndex + cellInfo.rowSpan;
  20419. if (downRowIndex >= ut.rowsNum) return -1; // 如果处于最下边则不能向下合并
  20420. var downCellInfo = ut.indexTable[downRowIndex][cellInfo.colIndex],
  20421. downCell =
  20422. table.rows[downCellInfo.rowIndex].cells[downCellInfo.cellIndex];
  20423. if (!downCell || cell.tagName != downCell.tagName) return -1; // TH和TD不能相互合并
  20424. // 当且仅当两个Cell的开始列号和结束列号一致时能进行合并
  20425. return downCellInfo.colIndex == cellInfo.colIndex &&
  20426. downCellInfo.colSpan == cellInfo.colSpan
  20427. ? 0
  20428. : -1;
  20429. },
  20430. execCommand: function() {
  20431. var rng = this.selection.getRange(),
  20432. bk = rng.createBookmark(true);
  20433. var cell = getTableItemsByRange(this).cell,
  20434. ut = getUETable(cell);
  20435. ut.mergeDown(cell);
  20436. rng.moveToBookmark(bk).select();
  20437. }
  20438. };
  20439. UE.commands["mergecells"] = {
  20440. queryCommandState: function() {
  20441. return getUETableBySelected(this) ? 0 : -1;
  20442. },
  20443. execCommand: function() {
  20444. var ut = getUETableBySelected(this);
  20445. if (ut && ut.selectedTds.length) {
  20446. var cell = ut.selectedTds[0];
  20447. ut.mergeRange();
  20448. var rng = this.selection.getRange();
  20449. if (domUtils.isEmptyBlock(cell)) {
  20450. rng.setStart(cell, 0).collapse(true);
  20451. } else {
  20452. rng.selectNodeContents(cell);
  20453. }
  20454. rng.select();
  20455. }
  20456. }
  20457. };
  20458. UE.commands["insertrow"] = {
  20459. queryCommandState: function() {
  20460. var tableItems = getTableItemsByRange(this),
  20461. cell = tableItems.cell;
  20462. return cell &&
  20463. (cell.tagName == "TD" ||
  20464. (cell.tagName == "TH" &&
  20465. tableItems.tr !== tableItems.table.rows[0])) &&
  20466. getUETable(tableItems.table).rowsNum < this.options.maxRowNum
  20467. ? 0
  20468. : -1;
  20469. },
  20470. execCommand: function() {
  20471. var rng = this.selection.getRange(),
  20472. bk = rng.createBookmark(true);
  20473. var tableItems = getTableItemsByRange(this),
  20474. cell = tableItems.cell,
  20475. table = tableItems.table,
  20476. ut = getUETable(table),
  20477. cellInfo = ut.getCellInfo(cell);
  20478. //ut.insertRow(!ut.selectedTds.length ? cellInfo.rowIndex:ut.cellsRange.beginRowIndex,'');
  20479. if (!ut.selectedTds.length) {
  20480. ut.insertRow(cellInfo.rowIndex, cell);
  20481. } else {
  20482. var range = ut.cellsRange;
  20483. for (
  20484. var i = 0, len = range.endRowIndex - range.beginRowIndex + 1;
  20485. i < len;
  20486. i++
  20487. ) {
  20488. ut.insertRow(range.beginRowIndex, cell);
  20489. }
  20490. }
  20491. rng.moveToBookmark(bk).select();
  20492. if (table.getAttribute("interlaced") === "enabled")
  20493. this.fireEvent("interlacetable", table);
  20494. }
  20495. };
  20496. //后插入行
  20497. UE.commands["insertrownext"] = {
  20498. queryCommandState: function() {
  20499. var tableItems = getTableItemsByRange(this),
  20500. cell = tableItems.cell;
  20501. return cell &&
  20502. cell.tagName == "TD" &&
  20503. getUETable(tableItems.table).rowsNum < this.options.maxRowNum
  20504. ? 0
  20505. : -1;
  20506. },
  20507. execCommand: function() {
  20508. var rng = this.selection.getRange(),
  20509. bk = rng.createBookmark(true);
  20510. var tableItems = getTableItemsByRange(this),
  20511. cell = tableItems.cell,
  20512. table = tableItems.table,
  20513. ut = getUETable(table),
  20514. cellInfo = ut.getCellInfo(cell);
  20515. //ut.insertRow(!ut.selectedTds.length? cellInfo.rowIndex + cellInfo.rowSpan : ut.cellsRange.endRowIndex + 1,'');
  20516. if (!ut.selectedTds.length) {
  20517. ut.insertRow(cellInfo.rowIndex + cellInfo.rowSpan, cell);
  20518. } else {
  20519. var range = ut.cellsRange;
  20520. for (
  20521. var i = 0, len = range.endRowIndex - range.beginRowIndex + 1;
  20522. i < len;
  20523. i++
  20524. ) {
  20525. ut.insertRow(range.endRowIndex + 1, cell);
  20526. }
  20527. }
  20528. rng.moveToBookmark(bk).select();
  20529. if (table.getAttribute("interlaced") === "enabled")
  20530. this.fireEvent("interlacetable", table);
  20531. }
  20532. };
  20533. UE.commands["deleterow"] = {
  20534. queryCommandState: function() {
  20535. var tableItems = getTableItemsByRange(this);
  20536. return tableItems.cell ? 0 : -1;
  20537. },
  20538. execCommand: function() {
  20539. var cell = getTableItemsByRange(this).cell,
  20540. ut = getUETable(cell),
  20541. cellsRange = ut.cellsRange,
  20542. cellInfo = ut.getCellInfo(cell),
  20543. preCell = ut.getVSideCell(cell),
  20544. nextCell = ut.getVSideCell(cell, true),
  20545. rng = this.selection.getRange();
  20546. if (utils.isEmptyObject(cellsRange)) {
  20547. ut.deleteRow(cellInfo.rowIndex);
  20548. } else {
  20549. for (
  20550. var i = cellsRange.beginRowIndex;
  20551. i < cellsRange.endRowIndex + 1;
  20552. i++
  20553. ) {
  20554. ut.deleteRow(cellsRange.beginRowIndex);
  20555. }
  20556. }
  20557. var table = ut.table;
  20558. if (!table.getElementsByTagName("td").length) {
  20559. var nextSibling = table.nextSibling;
  20560. domUtils.remove(table);
  20561. if (nextSibling) {
  20562. rng.setStart(nextSibling, 0).setCursor(false, true);
  20563. }
  20564. } else {
  20565. if (
  20566. cellInfo.rowSpan == 1 ||
  20567. cellInfo.rowSpan ==
  20568. cellsRange.endRowIndex - cellsRange.beginRowIndex + 1
  20569. ) {
  20570. if (nextCell || preCell)
  20571. rng.selectNodeContents(nextCell || preCell).setCursor(false, true);
  20572. } else {
  20573. var newCell = ut.getCell(
  20574. cellInfo.rowIndex,
  20575. ut.indexTable[cellInfo.rowIndex][cellInfo.colIndex].cellIndex
  20576. );
  20577. if (newCell) rng.selectNodeContents(newCell).setCursor(false, true);
  20578. }
  20579. }
  20580. if (table.getAttribute("interlaced") === "enabled")
  20581. this.fireEvent("interlacetable", table);
  20582. }
  20583. };
  20584. UE.commands["insertcol"] = {
  20585. queryCommandState: function(cmd) {
  20586. var tableItems = getTableItemsByRange(this),
  20587. cell = tableItems.cell;
  20588. return cell &&
  20589. (cell.tagName == "TD" ||
  20590. (cell.tagName == "TH" && cell !== tableItems.tr.cells[0])) &&
  20591. getUETable(tableItems.table).colsNum < this.options.maxColNum
  20592. ? 0
  20593. : -1;
  20594. },
  20595. execCommand: function(cmd) {
  20596. var rng = this.selection.getRange(),
  20597. bk = rng.createBookmark(true);
  20598. if (this.queryCommandState(cmd) == -1) return;
  20599. var cell = getTableItemsByRange(this).cell,
  20600. ut = getUETable(cell),
  20601. cellInfo = ut.getCellInfo(cell);
  20602. //ut.insertCol(!ut.selectedTds.length ? cellInfo.colIndex:ut.cellsRange.beginColIndex);
  20603. if (!ut.selectedTds.length) {
  20604. ut.insertCol(cellInfo.colIndex, cell);
  20605. } else {
  20606. var range = ut.cellsRange;
  20607. for (
  20608. var i = 0, len = range.endColIndex - range.beginColIndex + 1;
  20609. i < len;
  20610. i++
  20611. ) {
  20612. ut.insertCol(range.beginColIndex, cell);
  20613. }
  20614. }
  20615. rng.moveToBookmark(bk).select(true);
  20616. }
  20617. };
  20618. UE.commands["insertcolnext"] = {
  20619. queryCommandState: function() {
  20620. var tableItems = getTableItemsByRange(this),
  20621. cell = tableItems.cell;
  20622. return cell &&
  20623. getUETable(tableItems.table).colsNum < this.options.maxColNum
  20624. ? 0
  20625. : -1;
  20626. },
  20627. execCommand: function() {
  20628. var rng = this.selection.getRange(),
  20629. bk = rng.createBookmark(true);
  20630. var cell = getTableItemsByRange(this).cell,
  20631. ut = getUETable(cell),
  20632. cellInfo = ut.getCellInfo(cell);
  20633. //ut.insertCol(!ut.selectedTds.length ? cellInfo.colIndex + cellInfo.colSpan:ut.cellsRange.endColIndex +1);
  20634. if (!ut.selectedTds.length) {
  20635. ut.insertCol(cellInfo.colIndex + cellInfo.colSpan, cell);
  20636. } else {
  20637. var range = ut.cellsRange;
  20638. for (
  20639. var i = 0, len = range.endColIndex - range.beginColIndex + 1;
  20640. i < len;
  20641. i++
  20642. ) {
  20643. ut.insertCol(range.endColIndex + 1, cell);
  20644. }
  20645. }
  20646. rng.moveToBookmark(bk).select();
  20647. }
  20648. };
  20649. UE.commands["deletecol"] = {
  20650. queryCommandState: function() {
  20651. var tableItems = getTableItemsByRange(this);
  20652. return tableItems.cell ? 0 : -1;
  20653. },
  20654. execCommand: function() {
  20655. var cell = getTableItemsByRange(this).cell,
  20656. ut = getUETable(cell),
  20657. range = ut.cellsRange,
  20658. cellInfo = ut.getCellInfo(cell),
  20659. preCell = ut.getHSideCell(cell),
  20660. nextCell = ut.getHSideCell(cell, true);
  20661. if (utils.isEmptyObject(range)) {
  20662. ut.deleteCol(cellInfo.colIndex);
  20663. } else {
  20664. for (var i = range.beginColIndex; i < range.endColIndex + 1; i++) {
  20665. ut.deleteCol(range.beginColIndex);
  20666. }
  20667. }
  20668. var table = ut.table,
  20669. rng = this.selection.getRange();
  20670. if (!table.getElementsByTagName("td").length) {
  20671. var nextSibling = table.nextSibling;
  20672. domUtils.remove(table);
  20673. if (nextSibling) {
  20674. rng.setStart(nextSibling, 0).setCursor(false, true);
  20675. }
  20676. } else {
  20677. if (domUtils.inDoc(cell, this.document)) {
  20678. rng.setStart(cell, 0).setCursor(false, true);
  20679. } else {
  20680. if (nextCell && domUtils.inDoc(nextCell, this.document)) {
  20681. rng.selectNodeContents(nextCell).setCursor(false, true);
  20682. } else {
  20683. if (preCell && domUtils.inDoc(preCell, this.document)) {
  20684. rng.selectNodeContents(preCell).setCursor(true, true);
  20685. }
  20686. }
  20687. }
  20688. }
  20689. }
  20690. };
  20691. UE.commands["splittocells"] = {
  20692. queryCommandState: function() {
  20693. var tableItems = getTableItemsByRange(this),
  20694. cell = tableItems.cell;
  20695. if (!cell) return -1;
  20696. var ut = getUETable(tableItems.table);
  20697. if (ut.selectedTds.length > 0) return -1;
  20698. return cell && (cell.colSpan > 1 || cell.rowSpan > 1) ? 0 : -1;
  20699. },
  20700. execCommand: function() {
  20701. var rng = this.selection.getRange(),
  20702. bk = rng.createBookmark(true);
  20703. var cell = getTableItemsByRange(this).cell,
  20704. ut = getUETable(cell);
  20705. ut.splitToCells(cell);
  20706. rng.moveToBookmark(bk).select();
  20707. }
  20708. };
  20709. UE.commands["splittorows"] = {
  20710. queryCommandState: function() {
  20711. var tableItems = getTableItemsByRange(this),
  20712. cell = tableItems.cell;
  20713. if (!cell) return -1;
  20714. var ut = getUETable(tableItems.table);
  20715. if (ut.selectedTds.length > 0) return -1;
  20716. return cell && cell.rowSpan > 1 ? 0 : -1;
  20717. },
  20718. execCommand: function() {
  20719. var rng = this.selection.getRange(),
  20720. bk = rng.createBookmark(true);
  20721. var cell = getTableItemsByRange(this).cell,
  20722. ut = getUETable(cell);
  20723. ut.splitToRows(cell);
  20724. rng.moveToBookmark(bk).select();
  20725. }
  20726. };
  20727. UE.commands["splittocols"] = {
  20728. queryCommandState: function() {
  20729. var tableItems = getTableItemsByRange(this),
  20730. cell = tableItems.cell;
  20731. if (!cell) return -1;
  20732. var ut = getUETable(tableItems.table);
  20733. if (ut.selectedTds.length > 0) return -1;
  20734. return cell && cell.colSpan > 1 ? 0 : -1;
  20735. },
  20736. execCommand: function() {
  20737. var rng = this.selection.getRange(),
  20738. bk = rng.createBookmark(true);
  20739. var cell = getTableItemsByRange(this).cell,
  20740. ut = getUETable(cell);
  20741. ut.splitToCols(cell);
  20742. rng.moveToBookmark(bk).select();
  20743. }
  20744. };
  20745. UE.commands["adaptbytext"] = UE.commands["adaptbywindow"] = {
  20746. queryCommandState: function() {
  20747. return getTableItemsByRange(this).table ? 0 : -1;
  20748. },
  20749. execCommand: function(cmd) {
  20750. var tableItems = getTableItemsByRange(this),
  20751. table = tableItems.table;
  20752. if (table) {
  20753. if (cmd == "adaptbywindow") {
  20754. resetTdWidth(table, this);
  20755. } else {
  20756. var cells = domUtils.getElementsByTagName(table, "td th");
  20757. utils.each(cells, function(cell) {
  20758. cell.removeAttribute("width");
  20759. });
  20760. table.removeAttribute("width");
  20761. }
  20762. }
  20763. }
  20764. };
  20765. //平均分配各列
  20766. UE.commands["averagedistributecol"] = {
  20767. queryCommandState: function() {
  20768. var ut = getUETableBySelected(this);
  20769. if (!ut) return -1;
  20770. return ut.isFullRow() || ut.isFullCol() ? 0 : -1;
  20771. },
  20772. execCommand: function(cmd) {
  20773. var me = this,
  20774. ut = getUETableBySelected(me);
  20775. function getAverageWidth() {
  20776. var tb = ut.table,
  20777. averageWidth,
  20778. sumWidth = 0,
  20779. colsNum = 0,
  20780. tbAttr = getDefaultValue(me, tb);
  20781. if (ut.isFullRow()) {
  20782. sumWidth = tb.offsetWidth;
  20783. colsNum = ut.colsNum;
  20784. } else {
  20785. var begin = ut.cellsRange.beginColIndex,
  20786. end = ut.cellsRange.endColIndex,
  20787. node;
  20788. for (var i = begin; i <= end; ) {
  20789. node = ut.selectedTds[i];
  20790. sumWidth += node.offsetWidth;
  20791. i += node.colSpan;
  20792. colsNum += 1;
  20793. }
  20794. }
  20795. averageWidth =
  20796. Math.ceil(sumWidth / colsNum) -
  20797. tbAttr.tdBorder * 2 -
  20798. tbAttr.tdPadding * 2;
  20799. return averageWidth;
  20800. }
  20801. function setAverageWidth(averageWidth) {
  20802. utils.each(domUtils.getElementsByTagName(ut.table, "th"), function(
  20803. node
  20804. ) {
  20805. node.setAttribute("width", "");
  20806. });
  20807. var cells = ut.isFullRow()
  20808. ? domUtils.getElementsByTagName(ut.table, "td")
  20809. : ut.selectedTds;
  20810. utils.each(cells, function(node) {
  20811. if (node.colSpan == 1) {
  20812. node.setAttribute("width", averageWidth);
  20813. }
  20814. });
  20815. }
  20816. if (ut && ut.selectedTds.length) {
  20817. setAverageWidth(getAverageWidth());
  20818. }
  20819. }
  20820. };
  20821. //平均分配各行
  20822. UE.commands["averagedistributerow"] = {
  20823. queryCommandState: function() {
  20824. var ut = getUETableBySelected(this);
  20825. if (!ut) return -1;
  20826. if (ut.selectedTds && /th/gi.test(ut.selectedTds[0].tagName)) return -1;
  20827. return ut.isFullRow() || ut.isFullCol() ? 0 : -1;
  20828. },
  20829. execCommand: function(cmd) {
  20830. var me = this,
  20831. ut = getUETableBySelected(me);
  20832. function getAverageHeight() {
  20833. var averageHeight,
  20834. rowNum,
  20835. sumHeight = 0,
  20836. tb = ut.table,
  20837. tbAttr = getDefaultValue(me, tb),
  20838. tdpadding = parseInt(
  20839. domUtils.getComputedStyle(
  20840. tb.getElementsByTagName("td")[0],
  20841. "padding-top"
  20842. )
  20843. );
  20844. if (ut.isFullCol()) {
  20845. var captionArr = domUtils.getElementsByTagName(tb, "caption"),
  20846. thArr = domUtils.getElementsByTagName(tb, "th"),
  20847. captionHeight,
  20848. thHeight;
  20849. if (captionArr.length > 0) {
  20850. captionHeight = captionArr[0].offsetHeight;
  20851. }
  20852. if (thArr.length > 0) {
  20853. thHeight = thArr[0].offsetHeight;
  20854. }
  20855. sumHeight = tb.offsetHeight - (captionHeight || 0) - (thHeight || 0);
  20856. rowNum = thArr.length == 0 ? ut.rowsNum : ut.rowsNum - 1;
  20857. } else {
  20858. var begin = ut.cellsRange.beginRowIndex,
  20859. end = ut.cellsRange.endRowIndex,
  20860. count = 0,
  20861. trs = domUtils.getElementsByTagName(tb, "tr");
  20862. for (var i = begin; i <= end; i++) {
  20863. sumHeight += trs[i].offsetHeight;
  20864. count += 1;
  20865. }
  20866. rowNum = count;
  20867. }
  20868. //ie8下是混杂模式
  20869. if (browser.ie && browser.version < 9) {
  20870. averageHeight = Math.ceil(sumHeight / rowNum);
  20871. } else {
  20872. averageHeight =
  20873. Math.ceil(sumHeight / rowNum) - tbAttr.tdBorder * 2 - tdpadding * 2;
  20874. }
  20875. return averageHeight;
  20876. }
  20877. function setAverageHeight(averageHeight) {
  20878. var cells = ut.isFullCol()
  20879. ? domUtils.getElementsByTagName(ut.table, "td")
  20880. : ut.selectedTds;
  20881. utils.each(cells, function(node) {
  20882. if (node.rowSpan == 1) {
  20883. node.setAttribute("height", averageHeight);
  20884. }
  20885. });
  20886. }
  20887. if (ut && ut.selectedTds.length) {
  20888. setAverageHeight(getAverageHeight());
  20889. }
  20890. }
  20891. };
  20892. //单元格对齐方式
  20893. UE.commands["cellalignment"] = {
  20894. queryCommandState: function() {
  20895. return getTableItemsByRange(this).table ? 0 : -1;
  20896. },
  20897. execCommand: function(cmd, data) {
  20898. var me = this,
  20899. ut = getUETableBySelected(me);
  20900. if (!ut) {
  20901. var start = me.selection.getStart(),
  20902. cell =
  20903. start &&
  20904. domUtils.findParentByTagName(start, ["td", "th", "caption"], true);
  20905. if (!/caption/gi.test(cell.tagName)) {
  20906. domUtils.setAttributes(cell, data);
  20907. } else {
  20908. cell.style.textAlign = data.align;
  20909. cell.style.verticalAlign = data.vAlign;
  20910. }
  20911. me.selection.getRange().setCursor(true);
  20912. } else {
  20913. utils.each(ut.selectedTds, function(cell) {
  20914. domUtils.setAttributes(cell, data);
  20915. });
  20916. }
  20917. },
  20918. /**
  20919. * 查询当前点击的单元格的对齐状态, 如果当前已经选择了多个单元格, 则会返回所有单元格经过统一协调过后的状态
  20920. * @see UE.UETable.getTableCellAlignState
  20921. */
  20922. queryCommandValue: function(cmd) {
  20923. var activeMenuCell = getTableItemsByRange(this).cell;
  20924. if (!activeMenuCell) {
  20925. activeMenuCell = getSelectedArr(this)[0];
  20926. }
  20927. if (!activeMenuCell) {
  20928. return null;
  20929. } else {
  20930. //获取同时选中的其他单元格
  20931. var cells = UE.UETable.getUETable(activeMenuCell).selectedTds;
  20932. !cells.length && (cells = activeMenuCell);
  20933. return UE.UETable.getTableCellAlignState(cells);
  20934. }
  20935. }
  20936. };
  20937. //表格对齐方式
  20938. UE.commands["tablealignment"] = {
  20939. queryCommandState: function() {
  20940. if (browser.ie && browser.version < 8) {
  20941. return -1;
  20942. }
  20943. return getTableItemsByRange(this).table ? 0 : -1;
  20944. },
  20945. execCommand: function(cmd, value) {
  20946. var me = this,
  20947. start = me.selection.getStart(),
  20948. table = start && domUtils.findParentByTagName(start, ["table"], true);
  20949. if (table) {
  20950. table.setAttribute("align", value);
  20951. }
  20952. }
  20953. };
  20954. //表格属性
  20955. UE.commands["edittable"] = {
  20956. queryCommandState: function() {
  20957. return getTableItemsByRange(this).table ? 0 : -1;
  20958. },
  20959. execCommand: function(cmd, color) {
  20960. var rng = this.selection.getRange(),
  20961. table = domUtils.findParentByTagName(rng.startContainer, "table");
  20962. if (table) {
  20963. var arr = domUtils
  20964. .getElementsByTagName(table, "td")
  20965. .concat(
  20966. domUtils.getElementsByTagName(table, "th"),
  20967. domUtils.getElementsByTagName(table, "caption")
  20968. );
  20969. utils.each(arr, function(node) {
  20970. node.style.borderColor = color;
  20971. });
  20972. }
  20973. }
  20974. };
  20975. //单元格属性
  20976. UE.commands["edittd"] = {
  20977. queryCommandState: function() {
  20978. return getTableItemsByRange(this).table ? 0 : -1;
  20979. },
  20980. execCommand: function(cmd, bkColor) {
  20981. var me = this,
  20982. ut = getUETableBySelected(me);
  20983. if (!ut) {
  20984. var start = me.selection.getStart(),
  20985. cell =
  20986. start &&
  20987. domUtils.findParentByTagName(start, ["td", "th", "caption"], true);
  20988. if (cell) {
  20989. cell.style.backgroundColor = bkColor;
  20990. }
  20991. } else {
  20992. utils.each(ut.selectedTds, function(cell) {
  20993. cell.style.backgroundColor = bkColor;
  20994. });
  20995. }
  20996. }
  20997. };
  20998. UE.commands["settablebackground"] = {
  20999. queryCommandState: function() {
  21000. return getSelectedArr(this).length > 1 ? 0 : -1;
  21001. },
  21002. execCommand: function(cmd, value) {
  21003. var cells, ut;
  21004. cells = getSelectedArr(this);
  21005. ut = getUETable(cells[0]);
  21006. ut.setBackground(cells, value);
  21007. }
  21008. };
  21009. UE.commands["cleartablebackground"] = {
  21010. queryCommandState: function() {
  21011. var cells = getSelectedArr(this);
  21012. if (!cells.length) return -1;
  21013. for (var i = 0, cell; (cell = cells[i++]); ) {
  21014. if (cell.style.backgroundColor !== "") return 0;
  21015. }
  21016. return -1;
  21017. },
  21018. execCommand: function() {
  21019. var cells = getSelectedArr(this),
  21020. ut = getUETable(cells[0]);
  21021. ut.removeBackground(cells);
  21022. }
  21023. };
  21024. UE.commands["interlacetable"] = UE.commands["uninterlacetable"] = {
  21025. queryCommandState: function(cmd) {
  21026. var table = getTableItemsByRange(this).table;
  21027. if (!table) return -1;
  21028. var interlaced = table.getAttribute("interlaced");
  21029. if (cmd == "interlacetable") {
  21030. //TODO 待定
  21031. //是否需要待定,如果设置,则命令只能单次执行成功,但反射具备toggle效果;否则可以覆盖前次命令,但反射将不存在toggle效果
  21032. return interlaced === "enabled" ? -1 : 0;
  21033. } else {
  21034. return !interlaced || interlaced === "disabled" ? -1 : 0;
  21035. }
  21036. },
  21037. execCommand: function(cmd, classList) {
  21038. var table = getTableItemsByRange(this).table;
  21039. if (cmd == "interlacetable") {
  21040. table.setAttribute("interlaced", "enabled");
  21041. this.fireEvent("interlacetable", table, classList);
  21042. } else {
  21043. table.setAttribute("interlaced", "disabled");
  21044. this.fireEvent("uninterlacetable", table);
  21045. }
  21046. }
  21047. };
  21048. UE.commands["setbordervisible"] = {
  21049. queryCommandState: function(cmd) {
  21050. var table = getTableItemsByRange(this).table;
  21051. if (!table) return -1;
  21052. return 0;
  21053. },
  21054. execCommand: function() {
  21055. var table = getTableItemsByRange(this).table;
  21056. utils.each(domUtils.getElementsByTagName(table, "td"), function(td) {
  21057. td.style.borderWidth = "1px";
  21058. td.style.borderStyle = "solid";
  21059. });
  21060. }
  21061. };
  21062. function resetTdWidth(table, editor) {
  21063. var tds = domUtils.getElementsByTagName(table, "td th");
  21064. utils.each(tds, function(td) {
  21065. td.removeAttribute("width");
  21066. });
  21067. table.setAttribute(
  21068. "width",
  21069. getTableWidth(editor, true, getDefaultValue(editor, table))
  21070. );
  21071. var tdsWidths = [];
  21072. setTimeout(function() {
  21073. utils.each(tds, function(td) {
  21074. td.colSpan == 1 && tdsWidths.push(td.offsetWidth);
  21075. });
  21076. utils.each(tds, function(td, i) {
  21077. td.colSpan == 1 && td.setAttribute("width", tdsWidths[i] + "");
  21078. });
  21079. }, 0);
  21080. }
  21081. function getTableWidth(editor, needIEHack, defaultValue) {
  21082. var body = editor.body;
  21083. return (
  21084. body.offsetWidth -
  21085. (needIEHack
  21086. ? parseInt(domUtils.getComputedStyle(body, "margin-left"), 10) * 2
  21087. : 0) -
  21088. defaultValue.tableBorder * 2 -
  21089. (editor.options.offsetWidth || 0)
  21090. );
  21091. }
  21092. function getSelectedArr(editor) {
  21093. var cell = getTableItemsByRange(editor).cell;
  21094. if (cell) {
  21095. var ut = getUETable(cell);
  21096. return ut.selectedTds.length ? ut.selectedTds : [cell];
  21097. } else {
  21098. return [];
  21099. }
  21100. }
  21101. })();
  21102. // plugins/table.action.js
  21103. /**
  21104. * Created with JetBrains PhpStorm.
  21105. * User: taoqili
  21106. * Date: 12-10-12
  21107. * Time: 上午10:05
  21108. * To change this template use File | Settings | File Templates.
  21109. */
  21110. UE.plugins["table"] = function() {
  21111. var me = this,
  21112. tabTimer = null,
  21113. //拖动计时器
  21114. tableDragTimer = null,
  21115. //双击计时器
  21116. tableResizeTimer = null,
  21117. //单元格最小宽度
  21118. cellMinWidth = 5,
  21119. isInResizeBuffer = false,
  21120. //单元格边框大小
  21121. cellBorderWidth = 5,
  21122. //鼠标偏移距离
  21123. offsetOfTableCell = 10,
  21124. //记录在有限时间内的点击状态, 共有3个取值, 0, 1, 2。 0代表未初始化, 1代表单击了1次,2代表2次
  21125. singleClickState = 0,
  21126. userActionStatus = null,
  21127. //双击允许的时间范围
  21128. dblclickTime = 360,
  21129. UT = UE.UETable,
  21130. getUETable = function(tdOrTable) {
  21131. return UT.getUETable(tdOrTable);
  21132. },
  21133. getUETableBySelected = function(editor) {
  21134. return UT.getUETableBySelected(editor);
  21135. },
  21136. getDefaultValue = function(editor, table) {
  21137. return UT.getDefaultValue(editor, table);
  21138. },
  21139. removeSelectedClass = function(cells) {
  21140. return UT.removeSelectedClass(cells);
  21141. };
  21142. function showError(e) {
  21143. // throw e;
  21144. }
  21145. me.ready(function() {
  21146. var me = this;
  21147. var orgGetText = me.selection.getText;
  21148. me.selection.getText = function() {
  21149. var table = getUETableBySelected(me);
  21150. if (table) {
  21151. var str = "";
  21152. utils.each(table.selectedTds, function(td) {
  21153. str += td[browser.ie ? "innerText" : "textContent"];
  21154. });
  21155. return str;
  21156. } else {
  21157. return orgGetText.call(me.selection);
  21158. }
  21159. };
  21160. });
  21161. //处理拖动及框选相关方法
  21162. var startTd = null, //鼠标按下时的锚点td
  21163. currentTd = null, //当前鼠标经过时的td
  21164. onDrag = "", //指示当前拖动状态,其值可为"","h","v" ,分别表示未拖动状态,横向拖动状态,纵向拖动状态,用于鼠标移动过程中的判断
  21165. onBorder = false, //检测鼠标按下时是否处在单元格边缘位置
  21166. dragButton = null,
  21167. dragOver = false,
  21168. dragLine = null, //模拟的拖动线
  21169. dragTd = null; //发生拖动的目标td
  21170. var mousedown = false,
  21171. //todo 判断混乱模式
  21172. needIEHack = true;
  21173. me.setOpt({
  21174. maxColNum: 20,
  21175. maxRowNum: 100,
  21176. defaultCols: 5,
  21177. defaultRows: 5,
  21178. tdvalign: "top",
  21179. cursorpath: me.options.UEDITOR_HOME_URL + "themes/default/images/cursor_",
  21180. tableDragable: false,
  21181. classList: [
  21182. "ue-table-interlace-color-single",
  21183. "ue-table-interlace-color-double"
  21184. ]
  21185. });
  21186. me.getUETable = getUETable;
  21187. var commands = {
  21188. deletetable: 1,
  21189. inserttable: 1,
  21190. cellvalign: 1,
  21191. insertcaption: 1,
  21192. deletecaption: 1,
  21193. inserttitle: 1,
  21194. deletetitle: 1,
  21195. mergeright: 1,
  21196. mergedown: 1,
  21197. mergecells: 1,
  21198. insertrow: 1,
  21199. insertrownext: 1,
  21200. deleterow: 1,
  21201. insertcol: 1,
  21202. insertcolnext: 1,
  21203. deletecol: 1,
  21204. splittocells: 1,
  21205. splittorows: 1,
  21206. splittocols: 1,
  21207. adaptbytext: 1,
  21208. adaptbywindow: 1,
  21209. adaptbycustomer: 1,
  21210. insertparagraph: 1,
  21211. insertparagraphbeforetable: 1,
  21212. averagedistributecol: 1,
  21213. averagedistributerow: 1
  21214. };
  21215. me.ready(function() {
  21216. utils.cssRule(
  21217. "table",
  21218. //选中的td上的样式
  21219. ".selectTdClass{background-color:#edf5fa !important}" +
  21220. "table.noBorderTable td,table.noBorderTable th,table.noBorderTable caption{border:1px dashed #ddd !important}" +
  21221. //插入的表格的默认样式
  21222. "table{margin-bottom:10px;border-collapse:collapse;display:table;}" +
  21223. "td,th{padding: 5px 10px;border: 1px solid #DDD;}" +
  21224. "caption{border:1px dashed #DDD;border-bottom:0;padding:3px;text-align:center;}" +
  21225. "th{border-top:1px solid #BBB;background-color:#F7F7F7;}" +
  21226. "table tr.firstRow th{border-top-width:2px;}" +
  21227. ".ue-table-interlace-color-single{ background-color: #fcfcfc; } .ue-table-interlace-color-double{ background-color: #f7faff; }" +
  21228. "td p{margin:0;padding:0;}",
  21229. me.document
  21230. );
  21231. var tableCopyList, isFullCol, isFullRow;
  21232. //注册del/backspace事件
  21233. me.addListener("keydown", function(cmd, evt) {
  21234. var me = this;
  21235. var keyCode = evt.keyCode || evt.which;
  21236. if (keyCode == 8) {
  21237. var ut = getUETableBySelected(me);
  21238. if (ut && ut.selectedTds.length) {
  21239. if (ut.isFullCol()) {
  21240. me.execCommand("deletecol");
  21241. } else if (ut.isFullRow()) {
  21242. me.execCommand("deleterow");
  21243. } else {
  21244. me.fireEvent("delcells");
  21245. }
  21246. domUtils.preventDefault(evt);
  21247. }
  21248. var caption = domUtils.findParentByTagName(
  21249. me.selection.getStart(),
  21250. "caption",
  21251. true
  21252. ),
  21253. range = me.selection.getRange();
  21254. if (range.collapsed && caption && isEmptyBlock(caption)) {
  21255. me.fireEvent("saveScene");
  21256. var table = caption.parentNode;
  21257. domUtils.remove(caption);
  21258. if (table) {
  21259. range.setStart(table.rows[0].cells[0], 0).setCursor(false, true);
  21260. }
  21261. me.fireEvent("saveScene");
  21262. }
  21263. }
  21264. if (keyCode == 46) {
  21265. ut = getUETableBySelected(me);
  21266. if (ut) {
  21267. me.fireEvent("saveScene");
  21268. for (var i = 0, ci; (ci = ut.selectedTds[i++]); ) {
  21269. domUtils.fillNode(me.document, ci);
  21270. }
  21271. me.fireEvent("saveScene");
  21272. domUtils.preventDefault(evt);
  21273. }
  21274. }
  21275. if (keyCode == 13) {
  21276. var rng = me.selection.getRange(),
  21277. caption = domUtils.findParentByTagName(
  21278. rng.startContainer,
  21279. "caption",
  21280. true
  21281. );
  21282. if (caption) {
  21283. var table = domUtils.findParentByTagName(caption, "table");
  21284. if (!rng.collapsed) {
  21285. rng.deleteContents();
  21286. me.fireEvent("saveScene");
  21287. } else {
  21288. if (caption) {
  21289. rng.setStart(table.rows[0].cells[0], 0).setCursor(false, true);
  21290. }
  21291. }
  21292. domUtils.preventDefault(evt);
  21293. return;
  21294. }
  21295. if (rng.collapsed) {
  21296. var table = domUtils.findParentByTagName(rng.startContainer, "table");
  21297. if (table) {
  21298. var cell = table.rows[0].cells[0],
  21299. start = domUtils.findParentByTagName(
  21300. me.selection.getStart(),
  21301. ["td", "th"],
  21302. true
  21303. ),
  21304. preNode = table.previousSibling;
  21305. if (
  21306. cell === start &&
  21307. (!preNode ||
  21308. (preNode.nodeType == 1 && preNode.tagName == "TABLE")) &&
  21309. domUtils.isStartInblock(rng)
  21310. ) {
  21311. var first = domUtils.findParent(
  21312. me.selection.getStart(),
  21313. function(n) {
  21314. return domUtils.isBlockElm(n);
  21315. },
  21316. true
  21317. );
  21318. if (
  21319. first &&
  21320. (/t(h|d)/i.test(first.tagName) || first === start.firstChild)
  21321. ) {
  21322. me.execCommand("insertparagraphbeforetable");
  21323. domUtils.preventDefault(evt);
  21324. }
  21325. }
  21326. }
  21327. }
  21328. }
  21329. if ((evt.ctrlKey || evt.metaKey) && evt.keyCode == "67") {
  21330. tableCopyList = null;
  21331. var ut = getUETableBySelected(me);
  21332. if (ut) {
  21333. var tds = ut.selectedTds;
  21334. isFullCol = ut.isFullCol();
  21335. isFullRow = ut.isFullRow();
  21336. tableCopyList = [[ut.cloneCell(tds[0], null, true)]];
  21337. for (var i = 1, ci; (ci = tds[i]); i++) {
  21338. if (ci.parentNode !== tds[i - 1].parentNode) {
  21339. tableCopyList.push([ut.cloneCell(ci, null, true)]);
  21340. } else {
  21341. tableCopyList[tableCopyList.length - 1].push(
  21342. ut.cloneCell(ci, null, true)
  21343. );
  21344. }
  21345. }
  21346. }
  21347. }
  21348. });
  21349. me.addListener("tablehasdeleted", function() {
  21350. toggleDraggableState(this, false, "", null);
  21351. if (dragButton) domUtils.remove(dragButton);
  21352. });
  21353. me.addListener("beforepaste", function(cmd, html) {
  21354. var me = this;
  21355. var rng = me.selection.getRange();
  21356. if (domUtils.findParentByTagName(rng.startContainer, "caption", true)) {
  21357. var div = me.document.createElement("div");
  21358. div.innerHTML = html.html;
  21359. //trace:3729
  21360. html.html = div[browser.ie9below ? "innerText" : "textContent"];
  21361. return;
  21362. }
  21363. var table = getUETableBySelected(me);
  21364. if (tableCopyList) {
  21365. me.fireEvent("saveScene");
  21366. var rng = me.selection.getRange();
  21367. var td = domUtils.findParentByTagName(
  21368. rng.startContainer,
  21369. ["td", "th"],
  21370. true
  21371. ),
  21372. tmpNode,
  21373. preNode;
  21374. if (td) {
  21375. var ut = getUETable(td);
  21376. if (isFullRow) {
  21377. var rowIndex = ut.getCellInfo(td).rowIndex;
  21378. if (td.tagName == "TH") {
  21379. rowIndex++;
  21380. }
  21381. for (var i = 0, ci; (ci = tableCopyList[i++]); ) {
  21382. var tr = ut.insertRow(rowIndex++, "td");
  21383. for (var j = 0, cj; (cj = ci[j]); j++) {
  21384. var cell = tr.cells[j];
  21385. if (!cell) {
  21386. cell = tr.insertCell(j);
  21387. }
  21388. cell.innerHTML = cj.innerHTML;
  21389. cj.getAttribute("width") &&
  21390. cell.setAttribute("width", cj.getAttribute("width"));
  21391. cj.getAttribute("vAlign") &&
  21392. cell.setAttribute("vAlign", cj.getAttribute("vAlign"));
  21393. cj.getAttribute("align") &&
  21394. cell.setAttribute("align", cj.getAttribute("align"));
  21395. cj.style.cssText && (cell.style.cssText = cj.style.cssText);
  21396. }
  21397. for (var j = 0, cj; (cj = tr.cells[j]); j++) {
  21398. if (!ci[j]) break;
  21399. cj.innerHTML = ci[j].innerHTML;
  21400. ci[j].getAttribute("width") &&
  21401. cj.setAttribute("width", ci[j].getAttribute("width"));
  21402. ci[j].getAttribute("vAlign") &&
  21403. cj.setAttribute("vAlign", ci[j].getAttribute("vAlign"));
  21404. ci[j].getAttribute("align") &&
  21405. cj.setAttribute("align", ci[j].getAttribute("align"));
  21406. ci[j].style.cssText && (cj.style.cssText = ci[j].style.cssText);
  21407. }
  21408. }
  21409. } else {
  21410. if (isFullCol) {
  21411. cellInfo = ut.getCellInfo(td);
  21412. var maxColNum = 0;
  21413. for (var j = 0, ci = tableCopyList[0], cj; (cj = ci[j++]); ) {
  21414. maxColNum += cj.colSpan || 1;
  21415. }
  21416. me.__hasEnterExecCommand = true;
  21417. for (i = 0; i < maxColNum; i++) {
  21418. me.execCommand("insertcol");
  21419. }
  21420. me.__hasEnterExecCommand = false;
  21421. td = ut.table.rows[0].cells[cellInfo.cellIndex];
  21422. if (td.tagName == "TH") {
  21423. td = ut.table.rows[1].cells[cellInfo.cellIndex];
  21424. }
  21425. }
  21426. for (var i = 0, ci; (ci = tableCopyList[i++]); ) {
  21427. tmpNode = td;
  21428. for (var j = 0, cj; (cj = ci[j++]); ) {
  21429. if (td) {
  21430. td.innerHTML = cj.innerHTML;
  21431. //todo 定制处理
  21432. cj.getAttribute("width") &&
  21433. td.setAttribute("width", cj.getAttribute("width"));
  21434. cj.getAttribute("vAlign") &&
  21435. td.setAttribute("vAlign", cj.getAttribute("vAlign"));
  21436. cj.getAttribute("align") &&
  21437. td.setAttribute("align", cj.getAttribute("align"));
  21438. cj.style.cssText && (td.style.cssText = cj.style.cssText);
  21439. preNode = td;
  21440. td = td.nextSibling;
  21441. } else {
  21442. var cloneTd = cj.cloneNode(true);
  21443. domUtils.removeAttributes(cloneTd, [
  21444. "class",
  21445. "rowSpan",
  21446. "colSpan"
  21447. ]);
  21448. preNode.parentNode.appendChild(cloneTd);
  21449. }
  21450. }
  21451. td = ut.getNextCell(tmpNode, true, true);
  21452. if (!tableCopyList[i]) break;
  21453. if (!td) {
  21454. var cellInfo = ut.getCellInfo(tmpNode);
  21455. ut.table.insertRow(ut.table.rows.length);
  21456. ut.update();
  21457. td = ut.getVSideCell(tmpNode, true);
  21458. }
  21459. }
  21460. }
  21461. ut.update();
  21462. } else {
  21463. table = me.document.createElement("table");
  21464. for (var i = 0, ci; (ci = tableCopyList[i++]); ) {
  21465. var tr = table.insertRow(table.rows.length);
  21466. for (var j = 0, cj; (cj = ci[j++]); ) {
  21467. cloneTd = UT.cloneCell(cj, null, true);
  21468. domUtils.removeAttributes(cloneTd, ["class"]);
  21469. tr.appendChild(cloneTd);
  21470. }
  21471. if (j == 2 && cloneTd.rowSpan > 1) {
  21472. cloneTd.rowSpan = 1;
  21473. }
  21474. }
  21475. var defaultValue = getDefaultValue(me),
  21476. width =
  21477. me.body.offsetWidth -
  21478. (needIEHack
  21479. ? parseInt(
  21480. domUtils.getComputedStyle(me.body, "margin-left"),
  21481. 10
  21482. ) * 2
  21483. : 0) -
  21484. defaultValue.tableBorder * 2 -
  21485. (me.options.offsetWidth || 0);
  21486. me.execCommand(
  21487. "insertHTML",
  21488. "<table " +
  21489. (isFullCol && isFullRow ? 'width="' + width + '"' : "") +
  21490. ">" +
  21491. table.innerHTML
  21492. .replace(/>\s*</g, "><")
  21493. .replace(/\bth\b/gi, "td") +
  21494. "</table>"
  21495. );
  21496. }
  21497. me.fireEvent("contentchange");
  21498. me.fireEvent("saveScene");
  21499. html.html = "";
  21500. return true;
  21501. } else {
  21502. var div = me.document.createElement("div"),
  21503. tables;
  21504. div.innerHTML = html.html;
  21505. tables = div.getElementsByTagName("table");
  21506. if (domUtils.findParentByTagName(me.selection.getStart(), "table")) {
  21507. utils.each(tables, function(t) {
  21508. domUtils.remove(t);
  21509. });
  21510. if (
  21511. domUtils.findParentByTagName(
  21512. me.selection.getStart(),
  21513. "caption",
  21514. true
  21515. )
  21516. ) {
  21517. div.innerHTML = div[browser.ie ? "innerText" : "textContent"];
  21518. }
  21519. } else {
  21520. utils.each(tables, function(table) {
  21521. removeStyleSize(table, true);
  21522. domUtils.removeAttributes(table, ["style", "border"]);
  21523. utils.each(domUtils.getElementsByTagName(table, "td"), function(
  21524. td
  21525. ) {
  21526. if (isEmptyBlock(td)) {
  21527. domUtils.fillNode(me.document, td);
  21528. }
  21529. removeStyleSize(td, true);
  21530. // domUtils.removeAttributes(td, ['style'])
  21531. });
  21532. });
  21533. }
  21534. html.html = div.innerHTML;
  21535. }
  21536. });
  21537. me.addListener("afterpaste", function() {
  21538. utils.each(domUtils.getElementsByTagName(me.body, "table"), function(
  21539. table
  21540. ) {
  21541. if (table.offsetWidth > me.body.offsetWidth) {
  21542. var defaultValue = getDefaultValue(me, table);
  21543. table.style.width =
  21544. me.body.offsetWidth -
  21545. (needIEHack
  21546. ? parseInt(
  21547. domUtils.getComputedStyle(me.body, "margin-left"),
  21548. 10
  21549. ) * 2
  21550. : 0) -
  21551. defaultValue.tableBorder * 2 -
  21552. (me.options.offsetWidth || 0) +
  21553. "px";
  21554. }
  21555. });
  21556. });
  21557. me.addListener("blur", function() {
  21558. tableCopyList = null;
  21559. });
  21560. var timer;
  21561. me.addListener("keydown", function() {
  21562. clearTimeout(timer);
  21563. timer = setTimeout(function() {
  21564. var rng = me.selection.getRange(),
  21565. cell = domUtils.findParentByTagName(
  21566. rng.startContainer,
  21567. ["th", "td"],
  21568. true
  21569. );
  21570. if (cell) {
  21571. var table = cell.parentNode.parentNode.parentNode;
  21572. if (table.offsetWidth > table.getAttribute("width")) {
  21573. cell.style.wordBreak = "break-all";
  21574. }
  21575. }
  21576. }, 100);
  21577. });
  21578. me.addListener("selectionchange", function() {
  21579. toggleDraggableState(me, false, "", null);
  21580. });
  21581. //内容变化时触发索引更新
  21582. //todo 可否考虑标记检测,如果不涉及表格的变化就不进行索引重建和更新
  21583. me.addListener("contentchange", function() {
  21584. var me = this;
  21585. //尽可能排除一些不需要更新的状况
  21586. hideDragLine(me);
  21587. if (getUETableBySelected(me)) return;
  21588. var rng = me.selection.getRange();
  21589. var start = rng.startContainer;
  21590. start = domUtils.findParentByTagName(start, ["td", "th"], true);
  21591. utils.each(domUtils.getElementsByTagName(me.document, "table"), function(
  21592. table
  21593. ) {
  21594. if (me.fireEvent("excludetable", table) === true) return;
  21595. table.ueTable = new UT(table);
  21596. //trace:3742
  21597. // utils.each(domUtils.getElementsByTagName(me.document, 'td'), function (td) {
  21598. //
  21599. // if (domUtils.isEmptyBlock(td) && td !== start) {
  21600. // domUtils.fillNode(me.document, td);
  21601. // if (browser.ie && browser.version == 6) {
  21602. // td.innerHTML = '&nbsp;'
  21603. // }
  21604. // }
  21605. // });
  21606. // utils.each(domUtils.getElementsByTagName(me.document, 'th'), function (th) {
  21607. // if (domUtils.isEmptyBlock(th) && th !== start) {
  21608. // domUtils.fillNode(me.document, th);
  21609. // if (browser.ie && browser.version == 6) {
  21610. // th.innerHTML = '&nbsp;'
  21611. // }
  21612. // }
  21613. // });
  21614. table.onmouseover = function() {
  21615. me.fireEvent("tablemouseover", table);
  21616. };
  21617. table.onmousemove = function() {
  21618. me.fireEvent("tablemousemove", table);
  21619. me.options.tableDragable && toggleDragButton(true, this, me);
  21620. utils.defer(function() {
  21621. me.fireEvent("contentchange", 50);
  21622. }, true);
  21623. };
  21624. table.onmouseout = function() {
  21625. me.fireEvent("tablemouseout", table);
  21626. toggleDraggableState(me, false, "", null);
  21627. hideDragLine(me);
  21628. };
  21629. table.onclick = function(evt) {
  21630. evt = me.window.event || evt;
  21631. var target = getParentTdOrTh(evt.target || evt.srcElement);
  21632. if (!target) return;
  21633. var ut = getUETable(target),
  21634. table = ut.table,
  21635. cellInfo = ut.getCellInfo(target),
  21636. cellsRange,
  21637. rng = me.selection.getRange();
  21638. // if ("topLeft" == inPosition(table, mouseCoords(evt))) {
  21639. // cellsRange = ut.getCellsRange(ut.table.rows[0].cells[0], ut.getLastCell());
  21640. // ut.setSelected(cellsRange);
  21641. // return;
  21642. // }
  21643. // if ("bottomRight" == inPosition(table, mouseCoords(evt))) {
  21644. //
  21645. // return;
  21646. // }
  21647. if (inTableSide(table, target, evt, true)) {
  21648. var endTdCol = ut.getCell(
  21649. ut.indexTable[ut.rowsNum - 1][cellInfo.colIndex].rowIndex,
  21650. ut.indexTable[ut.rowsNum - 1][cellInfo.colIndex].cellIndex
  21651. );
  21652. if (evt.shiftKey && ut.selectedTds.length) {
  21653. if (ut.selectedTds[0] !== endTdCol) {
  21654. cellsRange = ut.getCellsRange(ut.selectedTds[0], endTdCol);
  21655. ut.setSelected(cellsRange);
  21656. } else {
  21657. rng && rng.selectNodeContents(endTdCol).select();
  21658. }
  21659. } else {
  21660. if (target !== endTdCol) {
  21661. cellsRange = ut.getCellsRange(target, endTdCol);
  21662. ut.setSelected(cellsRange);
  21663. } else {
  21664. rng && rng.selectNodeContents(endTdCol).select();
  21665. }
  21666. }
  21667. return;
  21668. }
  21669. if (inTableSide(table, target, evt)) {
  21670. var endTdRow = ut.getCell(
  21671. ut.indexTable[cellInfo.rowIndex][ut.colsNum - 1].rowIndex,
  21672. ut.indexTable[cellInfo.rowIndex][ut.colsNum - 1].cellIndex
  21673. );
  21674. if (evt.shiftKey && ut.selectedTds.length) {
  21675. if (ut.selectedTds[0] !== endTdRow) {
  21676. cellsRange = ut.getCellsRange(ut.selectedTds[0], endTdRow);
  21677. ut.setSelected(cellsRange);
  21678. } else {
  21679. rng && rng.selectNodeContents(endTdRow).select();
  21680. }
  21681. } else {
  21682. if (target !== endTdRow) {
  21683. cellsRange = ut.getCellsRange(target, endTdRow);
  21684. ut.setSelected(cellsRange);
  21685. } else {
  21686. rng && rng.selectNodeContents(endTdRow).select();
  21687. }
  21688. }
  21689. }
  21690. };
  21691. });
  21692. switchBorderColor(me, true);
  21693. });
  21694. domUtils.on(me.document, "mousemove", mouseMoveEvent);
  21695. domUtils.on(me.document, "mouseout", function(evt) {
  21696. var target = evt.target || evt.srcElement;
  21697. if (target.tagName == "TABLE") {
  21698. toggleDraggableState(me, false, "", null);
  21699. }
  21700. });
  21701. /**
  21702. * 表格隔行变色
  21703. */
  21704. me.addListener("interlacetable", function(type, table, classList) {
  21705. if (!table) return;
  21706. var me = this,
  21707. rows = table.rows,
  21708. len = rows.length,
  21709. getClass = function(list, index, repeat) {
  21710. return list[index]
  21711. ? list[index]
  21712. : repeat ? list[index % list.length] : "";
  21713. };
  21714. for (var i = 0; i < len; i++) {
  21715. rows[i].className = getClass(
  21716. classList || me.options.classList,
  21717. i,
  21718. true
  21719. );
  21720. }
  21721. });
  21722. me.addListener("uninterlacetable", function(type, table) {
  21723. if (!table) return;
  21724. var me = this,
  21725. rows = table.rows,
  21726. classList = me.options.classList,
  21727. len = rows.length;
  21728. for (var i = 0; i < len; i++) {
  21729. domUtils.removeClasses(rows[i], classList);
  21730. }
  21731. });
  21732. me.addListener("mousedown", mouseDownEvent);
  21733. me.addListener("mouseup", mouseUpEvent);
  21734. //拖动的时候触发mouseup
  21735. domUtils.on(me.body, "dragstart", function(evt) {
  21736. mouseUpEvent.call(me, "dragstart", evt);
  21737. });
  21738. me.addOutputRule(function(root) {
  21739. utils.each(root.getNodesByTagName("div"), function(n) {
  21740. if (n.getAttr("id") == "ue_tableDragLine") {
  21741. n.parentNode.removeChild(n);
  21742. }
  21743. });
  21744. });
  21745. var currentRowIndex = 0;
  21746. me.addListener("mousedown", function() {
  21747. currentRowIndex = 0;
  21748. });
  21749. me.addListener("tabkeydown", function() {
  21750. var range = this.selection.getRange(),
  21751. common = range.getCommonAncestor(true, true),
  21752. table = domUtils.findParentByTagName(common, "table");
  21753. if (table) {
  21754. if (domUtils.findParentByTagName(common, "caption", true)) {
  21755. var cell = domUtils.getElementsByTagName(table, "th td");
  21756. if (cell && cell.length) {
  21757. range.setStart(cell[0], 0).setCursor(false, true);
  21758. }
  21759. } else {
  21760. var cell = domUtils.findParentByTagName(common, ["td", "th"], true),
  21761. ua = getUETable(cell);
  21762. currentRowIndex = cell.rowSpan > 1
  21763. ? currentRowIndex
  21764. : ua.getCellInfo(cell).rowIndex;
  21765. var nextCell = ua.getTabNextCell(cell, currentRowIndex);
  21766. if (nextCell) {
  21767. if (isEmptyBlock(nextCell)) {
  21768. range.setStart(nextCell, 0).setCursor(false, true);
  21769. } else {
  21770. range.selectNodeContents(nextCell).select();
  21771. }
  21772. } else {
  21773. me.fireEvent("saveScene");
  21774. me.__hasEnterExecCommand = true;
  21775. this.execCommand("insertrownext");
  21776. me.__hasEnterExecCommand = false;
  21777. range = this.selection.getRange();
  21778. range
  21779. .setStart(table.rows[table.rows.length - 1].cells[0], 0)
  21780. .setCursor();
  21781. me.fireEvent("saveScene");
  21782. }
  21783. }
  21784. return true;
  21785. }
  21786. });
  21787. browser.ie &&
  21788. me.addListener("selectionchange", function() {
  21789. toggleDraggableState(this, false, "", null);
  21790. });
  21791. me.addListener("keydown", function(type, evt) {
  21792. var me = this;
  21793. //处理在表格的最后一个输入tab产生新的表格
  21794. var keyCode = evt.keyCode || evt.which;
  21795. if (keyCode == 8 || keyCode == 46) {
  21796. return;
  21797. }
  21798. var notCtrlKey =
  21799. !evt.ctrlKey && !evt.metaKey && !evt.shiftKey && !evt.altKey;
  21800. notCtrlKey &&
  21801. removeSelectedClass(domUtils.getElementsByTagName(me.body, "td"));
  21802. var ut = getUETableBySelected(me);
  21803. if (!ut) return;
  21804. notCtrlKey && ut.clearSelected();
  21805. });
  21806. me.addListener("beforegetcontent", function() {
  21807. switchBorderColor(this, false);
  21808. browser.ie &&
  21809. utils.each(this.document.getElementsByTagName("caption"), function(ci) {
  21810. if (domUtils.isEmptyNode(ci)) {
  21811. ci.innerHTML = "&nbsp;";
  21812. }
  21813. });
  21814. });
  21815. me.addListener("aftergetcontent", function() {
  21816. switchBorderColor(this, true);
  21817. });
  21818. me.addListener("getAllHtml", function() {
  21819. removeSelectedClass(me.document.getElementsByTagName("td"));
  21820. });
  21821. //修正全屏状态下插入的表格宽度在非全屏状态下撑开编辑器的情况
  21822. me.addListener("fullscreenchanged", function(type, fullscreen) {
  21823. if (!fullscreen) {
  21824. var ratio = this.body.offsetWidth / document.body.offsetWidth,
  21825. tables = domUtils.getElementsByTagName(this.body, "table");
  21826. utils.each(tables, function(table) {
  21827. if (table.offsetWidth < me.body.offsetWidth) return false;
  21828. var tds = domUtils.getElementsByTagName(table, "td"),
  21829. backWidths = [];
  21830. utils.each(tds, function(td) {
  21831. backWidths.push(td.offsetWidth);
  21832. });
  21833. for (var i = 0, td; (td = tds[i]); i++) {
  21834. td.setAttribute("width", Math.floor(backWidths[i] * ratio));
  21835. }
  21836. table.setAttribute(
  21837. "width",
  21838. Math.floor(getTableWidth(me, needIEHack, getDefaultValue(me)))
  21839. );
  21840. });
  21841. }
  21842. });
  21843. //重写execCommand命令,用于处理框选时的处理
  21844. var oldExecCommand = me.execCommand;
  21845. me.execCommand = function(cmd, datatat) {
  21846. var me = this,
  21847. args = arguments;
  21848. cmd = cmd.toLowerCase();
  21849. var ut = getUETableBySelected(me),
  21850. tds,
  21851. range = new dom.Range(me.document),
  21852. cmdFun = me.commands[cmd] || UE.commands[cmd],
  21853. result;
  21854. if (!cmdFun) return;
  21855. if (
  21856. ut &&
  21857. !commands[cmd] &&
  21858. !cmdFun.notNeedUndo &&
  21859. !me.__hasEnterExecCommand
  21860. ) {
  21861. me.__hasEnterExecCommand = true;
  21862. me.fireEvent("beforeexeccommand", cmd);
  21863. tds = ut.selectedTds;
  21864. var lastState = -2,
  21865. lastValue = -2,
  21866. value,
  21867. state;
  21868. for (var i = 0, td; (td = tds[i]); i++) {
  21869. if (isEmptyBlock(td)) {
  21870. range.setStart(td, 0).setCursor(false, true);
  21871. } else {
  21872. range.selectNode(td).select(true);
  21873. }
  21874. state = me.queryCommandState(cmd);
  21875. value = me.queryCommandValue(cmd);
  21876. if (state != -1) {
  21877. if (lastState !== state || lastValue !== value) {
  21878. me._ignoreContentChange = true;
  21879. result = oldExecCommand.apply(me, arguments);
  21880. me._ignoreContentChange = false;
  21881. }
  21882. lastState = me.queryCommandState(cmd);
  21883. lastValue = me.queryCommandValue(cmd);
  21884. if (domUtils.isEmptyBlock(td)) {
  21885. domUtils.fillNode(me.document, td);
  21886. }
  21887. }
  21888. }
  21889. range.setStart(tds[0], 0).shrinkBoundary(true).setCursor(false, true);
  21890. me.fireEvent("contentchange");
  21891. me.fireEvent("afterexeccommand", cmd);
  21892. me.__hasEnterExecCommand = false;
  21893. me._selectionChange();
  21894. } else {
  21895. result = oldExecCommand.apply(me, arguments);
  21896. }
  21897. return result;
  21898. };
  21899. });
  21900. /**
  21901. * 删除obj的宽高style,改成属性宽高
  21902. * @param obj
  21903. * @param replaceToProperty
  21904. */
  21905. function removeStyleSize(obj, replaceToProperty) {
  21906. removeStyle(obj, "width", true);
  21907. removeStyle(obj, "height", true);
  21908. }
  21909. function removeStyle(obj, styleName, replaceToProperty) {
  21910. if (obj.style[styleName]) {
  21911. replaceToProperty &&
  21912. obj.setAttribute(styleName, parseInt(obj.style[styleName], 10));
  21913. obj.style[styleName] = "";
  21914. }
  21915. }
  21916. function getParentTdOrTh(ele) {
  21917. if (ele.tagName == "TD" || ele.tagName == "TH") return ele;
  21918. var td;
  21919. if (
  21920. (td =
  21921. domUtils.findParentByTagName(ele, "td", true) ||
  21922. domUtils.findParentByTagName(ele, "th", true))
  21923. )
  21924. return td;
  21925. return null;
  21926. }
  21927. function isEmptyBlock(node) {
  21928. var reg = new RegExp(domUtils.fillChar, "g");
  21929. if (
  21930. node[browser.ie ? "innerText" : "textContent"]
  21931. .replace(/^\s*$/, "")
  21932. .replace(reg, "").length > 0
  21933. ) {
  21934. return 0;
  21935. }
  21936. for (var n in dtd.$isNotEmpty) {
  21937. if (node.getElementsByTagName(n).length) {
  21938. return 0;
  21939. }
  21940. }
  21941. return 1;
  21942. }
  21943. function mouseCoords(evt) {
  21944. if (evt.pageX || evt.pageY) {
  21945. return { x: evt.pageX, y: evt.pageY };
  21946. }
  21947. return {
  21948. x:
  21949. evt.clientX + me.document.body.scrollLeft - me.document.body.clientLeft,
  21950. y: evt.clientY + me.document.body.scrollTop - me.document.body.clientTop
  21951. };
  21952. }
  21953. function mouseMoveEvent(evt) {
  21954. if (isEditorDisabled()) {
  21955. return;
  21956. }
  21957. try {
  21958. //普通状态下鼠标移动
  21959. var target = getParentTdOrTh(evt.target || evt.srcElement),
  21960. pos;
  21961. //区分用户的行为是拖动还是双击
  21962. if (isInResizeBuffer) {
  21963. me.body.style.webkitUserSelect = "none";
  21964. if (
  21965. Math.abs(userActionStatus.x - evt.clientX) > offsetOfTableCell ||
  21966. Math.abs(userActionStatus.y - evt.clientY) > offsetOfTableCell
  21967. ) {
  21968. clearTableDragTimer();
  21969. isInResizeBuffer = false;
  21970. singleClickState = 0;
  21971. //drag action
  21972. tableBorderDrag(evt);
  21973. }
  21974. }
  21975. //修改单元格大小时的鼠标移动
  21976. if (onDrag && dragTd) {
  21977. singleClickState = 0;
  21978. me.body.style.webkitUserSelect = "none";
  21979. me.selection.getNative()[
  21980. browser.ie9below ? "empty" : "removeAllRanges"
  21981. ]();
  21982. pos = mouseCoords(evt);
  21983. toggleDraggableState(me, true, onDrag, pos, target);
  21984. if (onDrag == "h") {
  21985. dragLine.style.left = getPermissionX(dragTd, evt) + "px";
  21986. } else if (onDrag == "v") {
  21987. dragLine.style.top = getPermissionY(dragTd, evt) + "px";
  21988. }
  21989. return;
  21990. }
  21991. //当鼠标处于table上时,修改移动过程中的光标状态
  21992. if (target) {
  21993. //针对使用table作为容器的组件不触发拖拽效果
  21994. if (me.fireEvent("excludetable", target) === true) return;
  21995. pos = mouseCoords(evt);
  21996. var state = getRelation(target, pos),
  21997. table = domUtils.findParentByTagName(target, "table", true);
  21998. if (inTableSide(table, target, evt, true)) {
  21999. if (me.fireEvent("excludetable", table) === true) return;
  22000. me.body.style.cursor =
  22001. "url(" + me.options.cursorpath + "h.png),pointer";
  22002. } else if (inTableSide(table, target, evt)) {
  22003. if (me.fireEvent("excludetable", table) === true) return;
  22004. me.body.style.cursor =
  22005. "url(" + me.options.cursorpath + "v.png),pointer";
  22006. } else {
  22007. me.body.style.cursor = "text";
  22008. var curCell = target;
  22009. if (/\d/.test(state)) {
  22010. state = state.replace(/\d/, "");
  22011. target = getUETable(target).getPreviewCell(target, state == "v");
  22012. }
  22013. //位于第一行的顶部或者第一列的左边时不可拖动
  22014. toggleDraggableState(
  22015. me,
  22016. target ? !!state : false,
  22017. target ? state : "",
  22018. pos,
  22019. target
  22020. );
  22021. }
  22022. } else {
  22023. toggleDragButton(false, table, me);
  22024. }
  22025. } catch (e) {
  22026. showError(e);
  22027. }
  22028. }
  22029. var dragButtonTimer;
  22030. function toggleDragButton(show, table, editor) {
  22031. if (!show) {
  22032. if (dragOver) return;
  22033. dragButtonTimer = setTimeout(function() {
  22034. !dragOver &&
  22035. dragButton &&
  22036. dragButton.parentNode &&
  22037. dragButton.parentNode.removeChild(dragButton);
  22038. }, 2000);
  22039. } else {
  22040. createDragButton(table, editor);
  22041. }
  22042. }
  22043. function createDragButton(table, editor) {
  22044. var pos = domUtils.getXY(table),
  22045. doc = table.ownerDocument;
  22046. if (dragButton && dragButton.parentNode) return dragButton;
  22047. dragButton = doc.createElement("div");
  22048. dragButton.contentEditable = false;
  22049. dragButton.innerHTML = "";
  22050. dragButton.style.cssText =
  22051. "width:15px;height:15px;background-image:url(" +
  22052. editor.options.UEDITOR_HOME_URL +
  22053. "dialogs/table/dragicon.png);position: absolute;cursor:move;top:" +
  22054. (pos.y - 15) +
  22055. "px;left:" +
  22056. pos.x +
  22057. "px;";
  22058. domUtils.unSelectable(dragButton);
  22059. dragButton.onmouseover = function(evt) {
  22060. dragOver = true;
  22061. };
  22062. dragButton.onmouseout = function(evt) {
  22063. dragOver = false;
  22064. };
  22065. domUtils.on(dragButton, "click", function(type, evt) {
  22066. doClick(evt, this);
  22067. });
  22068. domUtils.on(dragButton, "dblclick", function(type, evt) {
  22069. doDblClick(evt);
  22070. });
  22071. domUtils.on(dragButton, "dragstart", function(type, evt) {
  22072. domUtils.preventDefault(evt);
  22073. });
  22074. var timer;
  22075. function doClick(evt, button) {
  22076. // 部分浏览器下需要清理
  22077. clearTimeout(timer);
  22078. timer = setTimeout(function() {
  22079. editor.fireEvent("tableClicked", table, button);
  22080. }, 300);
  22081. }
  22082. function doDblClick(evt) {
  22083. clearTimeout(timer);
  22084. var ut = getUETable(table),
  22085. start = table.rows[0].cells[0],
  22086. end = ut.getLastCell(),
  22087. range = ut.getCellsRange(start, end);
  22088. editor.selection.getRange().setStart(start, 0).setCursor(false, true);
  22089. ut.setSelected(range);
  22090. }
  22091. doc.body.appendChild(dragButton);
  22092. }
  22093. // function inPosition(table, pos) {
  22094. // var tablePos = domUtils.getXY(table),
  22095. // width = table.offsetWidth,
  22096. // height = table.offsetHeight;
  22097. // if (pos.x - tablePos.x < 5 && pos.y - tablePos.y < 5) {
  22098. // return "topLeft";
  22099. // } else if (tablePos.x + width - pos.x < 5 && tablePos.y + height - pos.y < 5) {
  22100. // return "bottomRight";
  22101. // }
  22102. // }
  22103. function inTableSide(table, cell, evt, top) {
  22104. var pos = mouseCoords(evt),
  22105. state = getRelation(cell, pos);
  22106. if (top) {
  22107. var caption = table.getElementsByTagName("caption")[0],
  22108. capHeight = caption ? caption.offsetHeight : 0;
  22109. return state == "v1" && pos.y - domUtils.getXY(table).y - capHeight < 8;
  22110. } else {
  22111. return state == "h1" && pos.x - domUtils.getXY(table).x < 8;
  22112. }
  22113. }
  22114. /**
  22115. * 获取拖动时允许的X轴坐标
  22116. * @param dragTd
  22117. * @param evt
  22118. */
  22119. function getPermissionX(dragTd, evt) {
  22120. var ut = getUETable(dragTd);
  22121. if (ut) {
  22122. var preTd = ut.getSameEndPosCells(dragTd, "x")[0],
  22123. nextTd = ut.getSameStartPosXCells(dragTd)[0],
  22124. mouseX = mouseCoords(evt).x,
  22125. left =
  22126. (preTd ? domUtils.getXY(preTd).x : domUtils.getXY(ut.table).x) + 20,
  22127. right = nextTd
  22128. ? domUtils.getXY(nextTd).x + nextTd.offsetWidth - 20
  22129. : me.body.offsetWidth + 5 ||
  22130. parseInt(domUtils.getComputedStyle(me.body, "width"), 10);
  22131. left += cellMinWidth;
  22132. right -= cellMinWidth;
  22133. return mouseX < left ? left : mouseX > right ? right : mouseX;
  22134. }
  22135. }
  22136. /**
  22137. * 获取拖动时允许的Y轴坐标
  22138. */
  22139. function getPermissionY(dragTd, evt) {
  22140. try {
  22141. var top = domUtils.getXY(dragTd).y,
  22142. mousePosY = mouseCoords(evt).y;
  22143. return mousePosY < top ? top : mousePosY;
  22144. } catch (e) {
  22145. showError(e);
  22146. }
  22147. }
  22148. /**
  22149. * 移动状态切换
  22150. */
  22151. function toggleDraggableState(editor, draggable, dir, mousePos, cell) {
  22152. try {
  22153. editor.body.style.cursor = dir == "h"
  22154. ? "col-resize"
  22155. : dir == "v" ? "row-resize" : "text";
  22156. if (browser.ie) {
  22157. if (dir && !mousedown && !getUETableBySelected(editor)) {
  22158. getDragLine(editor, editor.document);
  22159. showDragLineAt(dir, cell);
  22160. } else {
  22161. hideDragLine(editor);
  22162. }
  22163. }
  22164. onBorder = draggable;
  22165. } catch (e) {
  22166. showError(e);
  22167. }
  22168. }
  22169. /**
  22170. * 获取与UETable相关的resize line
  22171. * @param uetable UETable对象
  22172. */
  22173. function getResizeLineByUETable() {
  22174. var lineId = "_UETableResizeLine",
  22175. line = this.document.getElementById(lineId);
  22176. if (!line) {
  22177. line = this.document.createElement("div");
  22178. line.id = lineId;
  22179. line.contnetEditable = false;
  22180. line.setAttribute("unselectable", "on");
  22181. var styles = {
  22182. width: 2 * cellBorderWidth + 1 + "px",
  22183. position: "absolute",
  22184. "z-index": 100000,
  22185. cursor: "col-resize",
  22186. background: "red",
  22187. display: "none"
  22188. };
  22189. //切换状态
  22190. line.onmouseout = function() {
  22191. this.style.display = "none";
  22192. };
  22193. utils.extend(line.style, styles);
  22194. this.document.body.appendChild(line);
  22195. }
  22196. return line;
  22197. }
  22198. /**
  22199. * 更新resize-line
  22200. */
  22201. function updateResizeLine(cell, uetable) {
  22202. var line = getResizeLineByUETable.call(this),
  22203. table = uetable.table,
  22204. styles = {
  22205. top: domUtils.getXY(table).y + "px",
  22206. left:
  22207. domUtils.getXY(cell).x + cell.offsetWidth - cellBorderWidth + "px",
  22208. display: "block",
  22209. height: table.offsetHeight + "px"
  22210. };
  22211. utils.extend(line.style, styles);
  22212. }
  22213. /**
  22214. * 显示resize-line
  22215. */
  22216. function showResizeLine(cell) {
  22217. var uetable = getUETable(cell);
  22218. updateResizeLine.call(this, cell, uetable);
  22219. }
  22220. /**
  22221. * 获取鼠标与当前单元格的相对位置
  22222. * @param ele
  22223. * @param mousePos
  22224. */
  22225. function getRelation(ele, mousePos) {
  22226. var elePos = domUtils.getXY(ele);
  22227. if (!elePos) {
  22228. return "";
  22229. }
  22230. if (elePos.x + ele.offsetWidth - mousePos.x < cellBorderWidth) {
  22231. return "h";
  22232. }
  22233. if (mousePos.x - elePos.x < cellBorderWidth) {
  22234. return "h1";
  22235. }
  22236. if (elePos.y + ele.offsetHeight - mousePos.y < cellBorderWidth) {
  22237. return "v";
  22238. }
  22239. if (mousePos.y - elePos.y < cellBorderWidth) {
  22240. return "v1";
  22241. }
  22242. return "";
  22243. }
  22244. function mouseDownEvent(type, evt) {
  22245. if (isEditorDisabled()) {
  22246. return;
  22247. }
  22248. userActionStatus = {
  22249. x: evt.clientX,
  22250. y: evt.clientY
  22251. };
  22252. //右键菜单单独处理
  22253. if (evt.button == 2) {
  22254. var ut = getUETableBySelected(me),
  22255. flag = false;
  22256. if (ut) {
  22257. var td = getTargetTd(me, evt);
  22258. utils.each(ut.selectedTds, function(ti) {
  22259. if (ti === td) {
  22260. flag = true;
  22261. }
  22262. });
  22263. if (!flag) {
  22264. removeSelectedClass(domUtils.getElementsByTagName(me.body, "th td"));
  22265. ut.clearSelected();
  22266. } else {
  22267. td = ut.selectedTds[0];
  22268. setTimeout(function() {
  22269. me.selection.getRange().setStart(td, 0).setCursor(false, true);
  22270. }, 0);
  22271. }
  22272. }
  22273. } else {
  22274. tableClickHander(evt);
  22275. }
  22276. }
  22277. //清除表格的计时器
  22278. function clearTableTimer() {
  22279. tabTimer && clearTimeout(tabTimer);
  22280. tabTimer = null;
  22281. }
  22282. //双击收缩
  22283. function tableDbclickHandler(evt) {
  22284. singleClickState = 0;
  22285. evt = evt || me.window.event;
  22286. var target = getParentTdOrTh(evt.target || evt.srcElement);
  22287. if (target) {
  22288. var h;
  22289. if ((h = getRelation(target, mouseCoords(evt)))) {
  22290. hideDragLine(me);
  22291. if (h == "h1") {
  22292. h = "h";
  22293. if (
  22294. inTableSide(
  22295. domUtils.findParentByTagName(target, "table"),
  22296. target,
  22297. evt
  22298. )
  22299. ) {
  22300. me.execCommand("adaptbywindow");
  22301. } else {
  22302. target = getUETable(target).getPreviewCell(target);
  22303. if (target) {
  22304. var rng = me.selection.getRange();
  22305. rng.selectNodeContents(target).setCursor(true, true);
  22306. }
  22307. }
  22308. }
  22309. if (h == "h") {
  22310. var ut = getUETable(target),
  22311. table = ut.table,
  22312. cells = getCellsByMoveBorder(target, table, true);
  22313. cells = extractArray(cells, "left");
  22314. ut.width = ut.offsetWidth;
  22315. var oldWidth = [],
  22316. newWidth = [];
  22317. utils.each(cells, function(cell) {
  22318. oldWidth.push(cell.offsetWidth);
  22319. });
  22320. utils.each(cells, function(cell) {
  22321. cell.removeAttribute("width");
  22322. });
  22323. window.setTimeout(function() {
  22324. //是否允许改变
  22325. var changeable = true;
  22326. utils.each(cells, function(cell, index) {
  22327. var width = cell.offsetWidth;
  22328. if (width > oldWidth[index]) {
  22329. changeable = false;
  22330. return false;
  22331. }
  22332. newWidth.push(width);
  22333. });
  22334. var change = changeable ? newWidth : oldWidth;
  22335. utils.each(cells, function(cell, index) {
  22336. cell.width = change[index] - getTabcellSpace();
  22337. });
  22338. }, 0);
  22339. // minWidth -= cellMinWidth;
  22340. //
  22341. // table.removeAttribute("width");
  22342. // utils.each(cells, function (cell) {
  22343. // cell.style.width = "";
  22344. // cell.width -= minWidth;
  22345. // });
  22346. }
  22347. }
  22348. }
  22349. }
  22350. function tableClickHander(evt) {
  22351. removeSelectedClass(domUtils.getElementsByTagName(me.body, "td th"));
  22352. //trace:3113
  22353. //选中单元格,点击table外部,不会清掉table上挂的ueTable,会引起getUETableBySelected方法返回值
  22354. utils.each(me.document.getElementsByTagName("table"), function(t) {
  22355. t.ueTable = null;
  22356. });
  22357. startTd = getTargetTd(me, evt);
  22358. if (!startTd) return;
  22359. var table = domUtils.findParentByTagName(startTd, "table", true);
  22360. ut = getUETable(table);
  22361. ut && ut.clearSelected();
  22362. //判断当前鼠标状态
  22363. if (!onBorder) {
  22364. me.document.body.style.webkitUserSelect = "";
  22365. mousedown = true;
  22366. me.addListener("mouseover", mouseOverEvent);
  22367. } else {
  22368. //边框上的动作处理
  22369. borderActionHandler(evt);
  22370. }
  22371. }
  22372. //处理表格边框上的动作, 这里做延时处理,避免两种动作互相影响
  22373. function borderActionHandler(evt) {
  22374. if (browser.ie) {
  22375. evt = reconstruct(evt);
  22376. }
  22377. clearTableDragTimer();
  22378. //是否正在等待resize的缓冲中
  22379. isInResizeBuffer = true;
  22380. tableDragTimer = setTimeout(function() {
  22381. tableBorderDrag(evt);
  22382. }, dblclickTime);
  22383. }
  22384. function extractArray(originArr, key) {
  22385. var result = [],
  22386. tmp = null;
  22387. for (var i = 0, len = originArr.length; i < len; i++) {
  22388. tmp = originArr[i][key];
  22389. if (tmp) {
  22390. result.push(tmp);
  22391. }
  22392. }
  22393. return result;
  22394. }
  22395. function clearTableDragTimer() {
  22396. tableDragTimer && clearTimeout(tableDragTimer);
  22397. tableDragTimer = null;
  22398. }
  22399. function reconstruct(obj) {
  22400. var attrs = [
  22401. "pageX",
  22402. "pageY",
  22403. "clientX",
  22404. "clientY",
  22405. "srcElement",
  22406. "target"
  22407. ],
  22408. newObj = {};
  22409. if (obj) {
  22410. for (var i = 0, key, val; (key = attrs[i]); i++) {
  22411. val = obj[key];
  22412. val && (newObj[key] = val);
  22413. }
  22414. }
  22415. return newObj;
  22416. }
  22417. //边框拖动
  22418. function tableBorderDrag(evt) {
  22419. isInResizeBuffer = false;
  22420. startTd = evt.target || evt.srcElement;
  22421. if (!startTd) return;
  22422. var state = getRelation(startTd, mouseCoords(evt));
  22423. if (/\d/.test(state)) {
  22424. state = state.replace(/\d/, "");
  22425. startTd = getUETable(startTd).getPreviewCell(startTd, state == "v");
  22426. }
  22427. hideDragLine(me);
  22428. getDragLine(me, me.document);
  22429. me.fireEvent("saveScene");
  22430. showDragLineAt(state, startTd);
  22431. mousedown = true;
  22432. //拖动开始
  22433. onDrag = state;
  22434. dragTd = startTd;
  22435. }
  22436. function mouseUpEvent(type, evt) {
  22437. if (isEditorDisabled()) {
  22438. return;
  22439. }
  22440. clearTableDragTimer();
  22441. isInResizeBuffer = false;
  22442. if (onBorder) {
  22443. singleClickState = ++singleClickState % 3;
  22444. userActionStatus = {
  22445. x: evt.clientX,
  22446. y: evt.clientY
  22447. };
  22448. tableResizeTimer = setTimeout(function() {
  22449. singleClickState > 0 && singleClickState--;
  22450. }, dblclickTime);
  22451. if (singleClickState === 2) {
  22452. singleClickState = 0;
  22453. tableDbclickHandler(evt);
  22454. return;
  22455. }
  22456. }
  22457. if (evt.button == 2) return;
  22458. var me = this;
  22459. //清除表格上原生跨选问题
  22460. var range = me.selection.getRange(),
  22461. start = domUtils.findParentByTagName(range.startContainer, "table", true),
  22462. end = domUtils.findParentByTagName(range.endContainer, "table", true);
  22463. if (start || end) {
  22464. if (start === end) {
  22465. start = domUtils.findParentByTagName(
  22466. range.startContainer,
  22467. ["td", "th", "caption"],
  22468. true
  22469. );
  22470. end = domUtils.findParentByTagName(
  22471. range.endContainer,
  22472. ["td", "th", "caption"],
  22473. true
  22474. );
  22475. if (start !== end) {
  22476. me.selection.clearRange();
  22477. }
  22478. } else {
  22479. me.selection.clearRange();
  22480. }
  22481. }
  22482. mousedown = false;
  22483. me.document.body.style.webkitUserSelect = "";
  22484. //拖拽状态下的mouseUP
  22485. if (onDrag && dragTd) {
  22486. me.selection.getNative()[
  22487. browser.ie9below ? "empty" : "removeAllRanges"
  22488. ]();
  22489. singleClickState = 0;
  22490. dragLine = me.document.getElementById("ue_tableDragLine");
  22491. // trace 3973
  22492. if (dragLine) {
  22493. var dragTdPos = domUtils.getXY(dragTd),
  22494. dragLinePos = domUtils.getXY(dragLine);
  22495. switch (onDrag) {
  22496. case "h":
  22497. changeColWidth(dragTd, dragLinePos.x - dragTdPos.x);
  22498. break;
  22499. case "v":
  22500. changeRowHeight(
  22501. dragTd,
  22502. dragLinePos.y - dragTdPos.y - dragTd.offsetHeight
  22503. );
  22504. break;
  22505. default:
  22506. }
  22507. onDrag = "";
  22508. dragTd = null;
  22509. hideDragLine(me);
  22510. me.fireEvent("saveScene");
  22511. return;
  22512. }
  22513. }
  22514. //正常状态下的mouseup
  22515. if (!startTd) {
  22516. var target = domUtils.findParentByTagName(
  22517. evt.target || evt.srcElement,
  22518. "td",
  22519. true
  22520. );
  22521. if (!target)
  22522. target = domUtils.findParentByTagName(
  22523. evt.target || evt.srcElement,
  22524. "th",
  22525. true
  22526. );
  22527. if (target && (target.tagName == "TD" || target.tagName == "TH")) {
  22528. if (me.fireEvent("excludetable", target) === true) return;
  22529. range = new dom.Range(me.document);
  22530. range.setStart(target, 0).setCursor(false, true);
  22531. }
  22532. } else {
  22533. var ut = getUETable(startTd),
  22534. cell = ut ? ut.selectedTds[0] : null;
  22535. if (cell) {
  22536. range = new dom.Range(me.document);
  22537. if (domUtils.isEmptyBlock(cell)) {
  22538. range.setStart(cell, 0).setCursor(false, true);
  22539. } else {
  22540. range
  22541. .selectNodeContents(cell)
  22542. .shrinkBoundary()
  22543. .setCursor(false, true);
  22544. }
  22545. } else {
  22546. range = me.selection.getRange().shrinkBoundary();
  22547. if (!range.collapsed) {
  22548. var start = domUtils.findParentByTagName(
  22549. range.startContainer,
  22550. ["td", "th"],
  22551. true
  22552. ),
  22553. end = domUtils.findParentByTagName(
  22554. range.endContainer,
  22555. ["td", "th"],
  22556. true
  22557. );
  22558. //在table里边的不能清除
  22559. if (
  22560. (start && !end) ||
  22561. (!start && end) ||
  22562. (start && end && start !== end)
  22563. ) {
  22564. range.setCursor(false, true);
  22565. }
  22566. }
  22567. }
  22568. startTd = null;
  22569. me.removeListener("mouseover", mouseOverEvent);
  22570. }
  22571. me._selectionChange(250, evt);
  22572. }
  22573. function mouseOverEvent(type, evt) {
  22574. if (isEditorDisabled()) {
  22575. return;
  22576. }
  22577. var me = this,
  22578. tar = evt.target || evt.srcElement;
  22579. currentTd =
  22580. domUtils.findParentByTagName(tar, "td", true) ||
  22581. domUtils.findParentByTagName(tar, "th", true);
  22582. //需要判断两个TD是否位于同一个表格内
  22583. if (
  22584. startTd &&
  22585. currentTd &&
  22586. ((startTd.tagName == "TD" && currentTd.tagName == "TD") ||
  22587. (startTd.tagName == "TH" && currentTd.tagName == "TH")) &&
  22588. domUtils.findParentByTagName(startTd, "table") ==
  22589. domUtils.findParentByTagName(currentTd, "table")
  22590. ) {
  22591. var ut = getUETable(currentTd);
  22592. if (startTd != currentTd) {
  22593. me.document.body.style.webkitUserSelect = "none";
  22594. me.selection.getNative()[
  22595. browser.ie9below ? "empty" : "removeAllRanges"
  22596. ]();
  22597. var range = ut.getCellsRange(startTd, currentTd);
  22598. ut.setSelected(range);
  22599. } else {
  22600. me.document.body.style.webkitUserSelect = "";
  22601. ut.clearSelected();
  22602. }
  22603. }
  22604. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
  22605. }
  22606. function setCellHeight(cell, height, backHeight) {
  22607. var lineHight = parseInt(
  22608. domUtils.getComputedStyle(cell, "line-height"),
  22609. 10
  22610. ),
  22611. tmpHeight = backHeight + height;
  22612. height = tmpHeight < lineHight ? lineHight : tmpHeight;
  22613. if (cell.style.height) cell.style.height = "";
  22614. cell.rowSpan == 1
  22615. ? cell.setAttribute("height", height)
  22616. : cell.removeAttribute && cell.removeAttribute("height");
  22617. }
  22618. function getWidth(cell) {
  22619. if (!cell) return 0;
  22620. return parseInt(domUtils.getComputedStyle(cell, "width"), 10);
  22621. }
  22622. function changeColWidth(cell, changeValue) {
  22623. var ut = getUETable(cell);
  22624. if (ut) {
  22625. //根据当前移动的边框获取相关的单元格
  22626. var table = ut.table,
  22627. cells = getCellsByMoveBorder(cell, table);
  22628. table.style.width = "";
  22629. table.removeAttribute("width");
  22630. //修正改变量
  22631. changeValue = correctChangeValue(changeValue, cell, cells);
  22632. if (cell.nextSibling) {
  22633. var i = 0;
  22634. utils.each(cells, function(cellGroup) {
  22635. cellGroup.left.width = +cellGroup.left.width + changeValue;
  22636. cellGroup.right &&
  22637. (cellGroup.right.width = +cellGroup.right.width - changeValue);
  22638. });
  22639. } else {
  22640. utils.each(cells, function(cellGroup) {
  22641. cellGroup.left.width -= -changeValue;
  22642. });
  22643. }
  22644. }
  22645. }
  22646. function isEditorDisabled() {
  22647. return me.body.contentEditable === "false";
  22648. }
  22649. function changeRowHeight(td, changeValue) {
  22650. if (Math.abs(changeValue) < 10) return;
  22651. var ut = getUETable(td);
  22652. if (ut) {
  22653. var cells = ut.getSameEndPosCells(td, "y"),
  22654. //备份需要连带变化的td的原始高度,否则后期无法获取正确的值
  22655. backHeight = cells[0] ? cells[0].offsetHeight : 0;
  22656. for (var i = 0, cell; (cell = cells[i++]); ) {
  22657. setCellHeight(cell, changeValue, backHeight);
  22658. }
  22659. }
  22660. }
  22661. /**
  22662. * 获取调整单元格大小的相关单元格
  22663. * @isContainMergeCell 返回的结果中是否包含发生合并后的单元格
  22664. */
  22665. function getCellsByMoveBorder(cell, table, isContainMergeCell) {
  22666. if (!table) {
  22667. table = domUtils.findParentByTagName(cell, "table");
  22668. }
  22669. if (!table) {
  22670. return null;
  22671. }
  22672. //获取到该单元格所在行的序列号
  22673. var index = domUtils.getNodeIndex(cell),
  22674. temp = cell,
  22675. rows = table.rows,
  22676. colIndex = 0;
  22677. while (temp) {
  22678. //获取到当前单元格在未发生单元格合并时的序列
  22679. if (temp.nodeType === 1) {
  22680. colIndex += temp.colSpan || 1;
  22681. }
  22682. temp = temp.previousSibling;
  22683. }
  22684. temp = null;
  22685. //记录想关的单元格
  22686. var borderCells = [];
  22687. utils.each(rows, function(tabRow) {
  22688. var cells = tabRow.cells,
  22689. currIndex = 0;
  22690. utils.each(cells, function(tabCell) {
  22691. currIndex += tabCell.colSpan || 1;
  22692. if (currIndex === colIndex) {
  22693. borderCells.push({
  22694. left: tabCell,
  22695. right: tabCell.nextSibling || null
  22696. });
  22697. return false;
  22698. } else if (currIndex > colIndex) {
  22699. if (isContainMergeCell) {
  22700. borderCells.push({
  22701. left: tabCell
  22702. });
  22703. }
  22704. return false;
  22705. }
  22706. });
  22707. });
  22708. return borderCells;
  22709. }
  22710. /**
  22711. * 通过给定的单元格集合获取最小的单元格width
  22712. */
  22713. function getMinWidthByTableCells(cells) {
  22714. var minWidth = Number.MAX_VALUE;
  22715. for (var i = 0, curCell; (curCell = cells[i]); i++) {
  22716. minWidth = Math.min(
  22717. minWidth,
  22718. curCell.width || getTableCellWidth(curCell)
  22719. );
  22720. }
  22721. return minWidth;
  22722. }
  22723. function correctChangeValue(changeValue, relatedCell, cells) {
  22724. //为单元格的paading预留空间
  22725. changeValue -= getTabcellSpace();
  22726. if (changeValue < 0) {
  22727. return 0;
  22728. }
  22729. changeValue -= getTableCellWidth(relatedCell);
  22730. //确定方向
  22731. var direction = changeValue < 0 ? "left" : "right";
  22732. changeValue = Math.abs(changeValue);
  22733. //只关心非最后一个单元格就可以
  22734. utils.each(cells, function(cellGroup) {
  22735. var curCell = cellGroup[direction];
  22736. //为单元格保留最小空间
  22737. if (curCell) {
  22738. changeValue = Math.min(
  22739. changeValue,
  22740. getTableCellWidth(curCell) - cellMinWidth
  22741. );
  22742. }
  22743. });
  22744. //修正越界
  22745. changeValue = changeValue < 0 ? 0 : changeValue;
  22746. return direction === "left" ? -changeValue : changeValue;
  22747. }
  22748. function getTableCellWidth(cell) {
  22749. var width = 0,
  22750. //偏移纠正量
  22751. offset = 0,
  22752. width = cell.offsetWidth - getTabcellSpace();
  22753. //最后一个节点纠正一下
  22754. if (!cell.nextSibling) {
  22755. width -= getTableCellOffset(cell);
  22756. }
  22757. width = width < 0 ? 0 : width;
  22758. try {
  22759. cell.width = width;
  22760. } catch (e) {}
  22761. return width;
  22762. }
  22763. /**
  22764. * 获取单元格所在表格的最末单元格的偏移量
  22765. */
  22766. function getTableCellOffset(cell) {
  22767. tab = domUtils.findParentByTagName(cell, "table", false);
  22768. if (tab.offsetVal === undefined) {
  22769. var prev = cell.previousSibling;
  22770. if (prev) {
  22771. //最后一个单元格和前一个单元格的width diff结果 如果恰好为一个border width, 则条件成立
  22772. tab.offsetVal = cell.offsetWidth - prev.offsetWidth === UT.borderWidth
  22773. ? UT.borderWidth
  22774. : 0;
  22775. } else {
  22776. tab.offsetVal = 0;
  22777. }
  22778. }
  22779. return tab.offsetVal;
  22780. }
  22781. function getTabcellSpace() {
  22782. if (UT.tabcellSpace === undefined) {
  22783. var cell = null,
  22784. tab = me.document.createElement("table"),
  22785. tbody = me.document.createElement("tbody"),
  22786. trow = me.document.createElement("tr"),
  22787. tabcell = me.document.createElement("td"),
  22788. mirror = null;
  22789. tabcell.style.cssText = "border: 0;";
  22790. tabcell.width = 1;
  22791. trow.appendChild(tabcell);
  22792. trow.appendChild((mirror = tabcell.cloneNode(false)));
  22793. tbody.appendChild(trow);
  22794. tab.appendChild(tbody);
  22795. tab.style.cssText = "visibility: hidden;";
  22796. me.body.appendChild(tab);
  22797. UT.paddingSpace = tabcell.offsetWidth - 1;
  22798. var tmpTabWidth = tab.offsetWidth;
  22799. tabcell.style.cssText = "";
  22800. mirror.style.cssText = "";
  22801. UT.borderWidth = (tab.offsetWidth - tmpTabWidth) / 3;
  22802. UT.tabcellSpace = UT.paddingSpace + UT.borderWidth;
  22803. me.body.removeChild(tab);
  22804. }
  22805. getTabcellSpace = function() {
  22806. return UT.tabcellSpace;
  22807. };
  22808. return UT.tabcellSpace;
  22809. }
  22810. function getDragLine(editor, doc) {
  22811. if (mousedown) return;
  22812. dragLine = editor.document.createElement("div");
  22813. domUtils.setAttributes(dragLine, {
  22814. id: "ue_tableDragLine",
  22815. unselectable: "on",
  22816. contenteditable: false,
  22817. onresizestart: "return false",
  22818. ondragstart: "return false",
  22819. onselectstart: "return false",
  22820. style:
  22821. "background-color:blue;position:absolute;padding:0;margin:0;background-image:none;border:0px none;opacity:0;filter:alpha(opacity=0)"
  22822. });
  22823. editor.body.appendChild(dragLine);
  22824. }
  22825. function hideDragLine(editor) {
  22826. if (mousedown) return;
  22827. var line;
  22828. while ((line = editor.document.getElementById("ue_tableDragLine"))) {
  22829. domUtils.remove(line);
  22830. }
  22831. }
  22832. /**
  22833. * 依据state(v|h)在cell位置显示横线
  22834. * @param state
  22835. * @param cell
  22836. */
  22837. function showDragLineAt(state, cell) {
  22838. if (!cell) return;
  22839. var table = domUtils.findParentByTagName(cell, "table"),
  22840. caption = table.getElementsByTagName("caption"),
  22841. width = table.offsetWidth,
  22842. height =
  22843. table.offsetHeight - (caption.length > 0 ? caption[0].offsetHeight : 0),
  22844. tablePos = domUtils.getXY(table),
  22845. cellPos = domUtils.getXY(cell),
  22846. css;
  22847. switch (state) {
  22848. case "h":
  22849. css =
  22850. "height:" +
  22851. height +
  22852. "px;top:" +
  22853. (tablePos.y + (caption.length > 0 ? caption[0].offsetHeight : 0)) +
  22854. "px;left:" +
  22855. (cellPos.x + cell.offsetWidth);
  22856. dragLine.style.cssText =
  22857. css +
  22858. "px;position: absolute;display:block;background-color:blue;width:1px;border:0; color:blue;opacity:.3;filter:alpha(opacity=30)";
  22859. break;
  22860. case "v":
  22861. css =
  22862. "width:" +
  22863. width +
  22864. "px;left:" +
  22865. tablePos.x +
  22866. "px;top:" +
  22867. (cellPos.y + cell.offsetHeight);
  22868. //必须加上border:0和color:blue,否则低版ie不支持背景色显示
  22869. dragLine.style.cssText =
  22870. css +
  22871. "px;overflow:hidden;position: absolute;display:block;background-color:blue;height:1px;border:0;color:blue;opacity:.2;filter:alpha(opacity=20)";
  22872. break;
  22873. default:
  22874. }
  22875. }
  22876. /**
  22877. * 当表格边框颜色为白色时设置为虚线,true为添加虚线
  22878. * @param editor
  22879. * @param flag
  22880. */
  22881. function switchBorderColor(editor, flag) {
  22882. var tableArr = domUtils.getElementsByTagName(editor.body, "table"),
  22883. color;
  22884. for (var i = 0, node; (node = tableArr[i++]); ) {
  22885. var td = domUtils.getElementsByTagName(node, "td");
  22886. if (td[0]) {
  22887. if (flag) {
  22888. color = td[0].style.borderColor.replace(/\s/g, "");
  22889. if (/(#ffffff)|(rgb\(255,255,255\))/gi.test(color))
  22890. domUtils.addClass(node, "noBorderTable");
  22891. } else {
  22892. domUtils.removeClasses(node, "noBorderTable");
  22893. }
  22894. }
  22895. }
  22896. }
  22897. function getTableWidth(editor, needIEHack, defaultValue) {
  22898. var body = editor.body;
  22899. return (
  22900. body.offsetWidth -
  22901. (needIEHack
  22902. ? parseInt(domUtils.getComputedStyle(body, "margin-left"), 10) * 2
  22903. : 0) -
  22904. defaultValue.tableBorder * 2 -
  22905. (editor.options.offsetWidth || 0)
  22906. );
  22907. }
  22908. /**
  22909. * 获取当前拖动的单元格
  22910. */
  22911. function getTargetTd(editor, evt) {
  22912. var target = domUtils.findParentByTagName(
  22913. evt.target || evt.srcElement,
  22914. ["td", "th"],
  22915. true
  22916. ),
  22917. dir = null;
  22918. if (!target) {
  22919. return null;
  22920. }
  22921. dir = getRelation(target, mouseCoords(evt));
  22922. //如果有前一个节点, 需要做一个修正, 否则可能会得到一个错误的td
  22923. if (!target) {
  22924. return null;
  22925. }
  22926. if (dir === "h1" && target.previousSibling) {
  22927. var position = domUtils.getXY(target),
  22928. cellWidth = target.offsetWidth;
  22929. if (Math.abs(position.x + cellWidth - evt.clientX) > cellWidth / 3) {
  22930. target = target.previousSibling;
  22931. }
  22932. } else if (dir === "v1" && target.parentNode.previousSibling) {
  22933. var position = domUtils.getXY(target),
  22934. cellHeight = target.offsetHeight;
  22935. if (Math.abs(position.y + cellHeight - evt.clientY) > cellHeight / 3) {
  22936. target = target.parentNode.previousSibling.firstChild;
  22937. }
  22938. }
  22939. //排除了非td内部以及用于代码高亮部分的td
  22940. return target && !(editor.fireEvent("excludetable", target) === true)
  22941. ? target
  22942. : null;
  22943. }
  22944. };
  22945. // plugins/table.sort.js
  22946. /**
  22947. * Created with JetBrains PhpStorm.
  22948. * User: Jinqn
  22949. * Date: 13-10-12
  22950. * Time: 上午10:20
  22951. * To change this template use File | Settings | File Templates.
  22952. */
  22953. UE.UETable.prototype.sortTable = function(sortByCellIndex, compareFn) {
  22954. var table = this.table,
  22955. rows = table.rows,
  22956. trArray = [],
  22957. flag = rows[0].cells[0].tagName === "TH",
  22958. lastRowIndex = 0;
  22959. if (this.selectedTds.length) {
  22960. var range = this.cellsRange,
  22961. len = range.endRowIndex + 1;
  22962. for (var i = range.beginRowIndex; i < len; i++) {
  22963. trArray[i] = rows[i];
  22964. }
  22965. trArray.splice(0, range.beginRowIndex);
  22966. lastRowIndex = range.endRowIndex + 1 === this.rowsNum
  22967. ? 0
  22968. : range.endRowIndex + 1;
  22969. } else {
  22970. for (var i = 0, len = rows.length; i < len; i++) {
  22971. trArray[i] = rows[i];
  22972. }
  22973. }
  22974. var Fn = {
  22975. reversecurrent: function(td1, td2) {
  22976. return 1;
  22977. },
  22978. orderbyasc: function(td1, td2) {
  22979. var value1 = td1.innerText || td1.textContent,
  22980. value2 = td2.innerText || td2.textContent;
  22981. return value1.localeCompare(value2);
  22982. },
  22983. reversebyasc: function(td1, td2) {
  22984. var value1 = td1.innerHTML,
  22985. value2 = td2.innerHTML;
  22986. return value2.localeCompare(value1);
  22987. },
  22988. orderbynum: function(td1, td2) {
  22989. var value1 = td1[browser.ie ? "innerText" : "textContent"].match(/\d+/),
  22990. value2 = td2[browser.ie ? "innerText" : "textContent"].match(/\d+/);
  22991. if (value1) value1 = +value1[0];
  22992. if (value2) value2 = +value2[0];
  22993. return (value1 || 0) - (value2 || 0);
  22994. },
  22995. reversebynum: function(td1, td2) {
  22996. var value1 = td1[browser.ie ? "innerText" : "textContent"].match(/\d+/),
  22997. value2 = td2[browser.ie ? "innerText" : "textContent"].match(/\d+/);
  22998. if (value1) value1 = +value1[0];
  22999. if (value2) value2 = +value2[0];
  23000. return (value2 || 0) - (value1 || 0);
  23001. }
  23002. };
  23003. //对表格设置排序的标记data-sort-type
  23004. table.setAttribute(
  23005. "data-sort-type",
  23006. compareFn && typeof compareFn === "string" && Fn[compareFn] ? compareFn : ""
  23007. );
  23008. //th不参与排序
  23009. flag && trArray.splice(0, 1);
  23010. trArray = utils.sort(trArray, function(tr1, tr2) {
  23011. var result;
  23012. if (compareFn && typeof compareFn === "function") {
  23013. result = compareFn.call(
  23014. this,
  23015. tr1.cells[sortByCellIndex],
  23016. tr2.cells[sortByCellIndex]
  23017. );
  23018. } else if (compareFn && typeof compareFn === "number") {
  23019. result = 1;
  23020. } else if (compareFn && typeof compareFn === "string" && Fn[compareFn]) {
  23021. result = Fn[compareFn].call(
  23022. this,
  23023. tr1.cells[sortByCellIndex],
  23024. tr2.cells[sortByCellIndex]
  23025. );
  23026. } else {
  23027. result = Fn["orderbyasc"].call(
  23028. this,
  23029. tr1.cells[sortByCellIndex],
  23030. tr2.cells[sortByCellIndex]
  23031. );
  23032. }
  23033. return result;
  23034. });
  23035. var fragment = table.ownerDocument.createDocumentFragment();
  23036. for (var j = 0, len = trArray.length; j < len; j++) {
  23037. fragment.appendChild(trArray[j]);
  23038. }
  23039. var tbody = table.getElementsByTagName("tbody")[0];
  23040. if (!lastRowIndex) {
  23041. tbody.appendChild(fragment);
  23042. } else {
  23043. tbody.insertBefore(
  23044. fragment,
  23045. rows[lastRowIndex - range.endRowIndex + range.beginRowIndex - 1]
  23046. );
  23047. }
  23048. };
  23049. UE.plugins["tablesort"] = function() {
  23050. var me = this,
  23051. UT = UE.UETable,
  23052. getUETable = function(tdOrTable) {
  23053. return UT.getUETable(tdOrTable);
  23054. },
  23055. getTableItemsByRange = function(editor) {
  23056. return UT.getTableItemsByRange(editor);
  23057. };
  23058. me.ready(function() {
  23059. //添加表格可排序的样式
  23060. utils.cssRule(
  23061. "tablesort",
  23062. "table.sortEnabled tr.firstRow th,table.sortEnabled tr.firstRow td{padding-right:20px;background-repeat: no-repeat;background-position: center right;" +
  23063. " background-image:url(" +
  23064. me.options.themePath +
  23065. me.options.theme +
  23066. "/images/sortable.png);}",
  23067. me.document
  23068. );
  23069. //做单元格合并操作时,清除可排序标识
  23070. me.addListener("afterexeccommand", function(type, cmd) {
  23071. if (cmd == "mergeright" || cmd == "mergedown" || cmd == "mergecells") {
  23072. this.execCommand("disablesort");
  23073. }
  23074. });
  23075. });
  23076. //表格排序
  23077. UE.commands["sorttable"] = {
  23078. queryCommandState: function() {
  23079. var me = this,
  23080. tableItems = getTableItemsByRange(me);
  23081. if (!tableItems.cell) return -1;
  23082. var table = tableItems.table,
  23083. cells = table.getElementsByTagName("td");
  23084. for (var i = 0, cell; (cell = cells[i++]); ) {
  23085. if (cell.rowSpan != 1 || cell.colSpan != 1) return -1;
  23086. }
  23087. return 0;
  23088. },
  23089. execCommand: function(cmd, fn) {
  23090. var me = this,
  23091. range = me.selection.getRange(),
  23092. bk = range.createBookmark(true),
  23093. tableItems = getTableItemsByRange(me),
  23094. cell = tableItems.cell,
  23095. ut = getUETable(tableItems.table),
  23096. cellInfo = ut.getCellInfo(cell);
  23097. ut.sortTable(cellInfo.cellIndex, fn);
  23098. range.moveToBookmark(bk);
  23099. try {
  23100. range.select();
  23101. } catch (e) {}
  23102. }
  23103. };
  23104. //设置表格可排序,清除表格可排序
  23105. UE.commands["enablesort"] = UE.commands["disablesort"] = {
  23106. queryCommandState: function(cmd) {
  23107. var table = getTableItemsByRange(this).table;
  23108. if (table && cmd == "enablesort") {
  23109. var cells = domUtils.getElementsByTagName(table, "th td");
  23110. for (var i = 0; i < cells.length; i++) {
  23111. if (
  23112. cells[i].getAttribute("colspan") > 1 ||
  23113. cells[i].getAttribute("rowspan") > 1
  23114. )
  23115. return -1;
  23116. }
  23117. }
  23118. return !table
  23119. ? -1
  23120. : (cmd == "enablesort") ^
  23121. (table.getAttribute("data-sort") != "sortEnabled")
  23122. ? -1
  23123. : 0;
  23124. },
  23125. execCommand: function(cmd) {
  23126. var table = getTableItemsByRange(this).table;
  23127. table.setAttribute(
  23128. "data-sort",
  23129. cmd == "enablesort" ? "sortEnabled" : "sortDisabled"
  23130. );
  23131. cmd == "enablesort"
  23132. ? domUtils.addClass(table, "sortEnabled")
  23133. : domUtils.removeClasses(table, "sortEnabled");
  23134. }
  23135. };
  23136. };
  23137. // plugins/contextmenu.js
  23138. ///import core
  23139. ///commands 右键菜单
  23140. ///commandsName ContextMenu
  23141. ///commandsTitle 右键菜单
  23142. /**
  23143. * 右键菜单
  23144. * @function
  23145. * @name baidu.editor.plugins.contextmenu
  23146. * @author zhanyi
  23147. */
  23148. UE.plugins["contextmenu"] = function() {
  23149. var me = this;
  23150. me.setOpt("enableContextMenu", me.getOpt("enableContextMenu") || true);
  23151. if (me.getOpt("enableContextMenu") === false) {
  23152. return;
  23153. }
  23154. var lang = me.getLang("contextMenu"),
  23155. menu,
  23156. items = me.options.contextMenu || [
  23157. { label: lang["selectall"], cmdName: "selectall" },
  23158. {
  23159. label: lang.cleardoc,
  23160. cmdName: "cleardoc",
  23161. exec: function() {
  23162. if (confirm(lang.confirmclear)) {
  23163. this.execCommand("cleardoc");
  23164. }
  23165. }
  23166. },
  23167. "-",
  23168. {
  23169. label: lang.unlink,
  23170. cmdName: "unlink"
  23171. },
  23172. "-",
  23173. {
  23174. group: lang.paragraph,
  23175. icon: "justifyjustify",
  23176. subMenu: [
  23177. {
  23178. label: lang.justifyleft,
  23179. cmdName: "justify",
  23180. value: "left"
  23181. },
  23182. {
  23183. label: lang.justifyright,
  23184. cmdName: "justify",
  23185. value: "right"
  23186. },
  23187. {
  23188. label: lang.justifycenter,
  23189. cmdName: "justify",
  23190. value: "center"
  23191. },
  23192. {
  23193. label: lang.justifyjustify,
  23194. cmdName: "justify",
  23195. value: "justify"
  23196. }
  23197. ]
  23198. },
  23199. "-",
  23200. {
  23201. group: lang.table,
  23202. icon: "table",
  23203. subMenu: [
  23204. {
  23205. label: lang.inserttable,
  23206. cmdName: "inserttable"
  23207. },
  23208. {
  23209. label: lang.deletetable,
  23210. cmdName: "deletetable"
  23211. },
  23212. "-",
  23213. {
  23214. label: lang.deleterow,
  23215. cmdName: "deleterow"
  23216. },
  23217. {
  23218. label: lang.deletecol,
  23219. cmdName: "deletecol"
  23220. },
  23221. {
  23222. label: lang.insertcol,
  23223. cmdName: "insertcol"
  23224. },
  23225. {
  23226. label: lang.insertcolnext,
  23227. cmdName: "insertcolnext"
  23228. },
  23229. {
  23230. label: lang.insertrow,
  23231. cmdName: "insertrow"
  23232. },
  23233. {
  23234. label: lang.insertrownext,
  23235. cmdName: "insertrownext"
  23236. },
  23237. "-",
  23238. {
  23239. label: lang.insertcaption,
  23240. cmdName: "insertcaption"
  23241. },
  23242. {
  23243. label: lang.deletecaption,
  23244. cmdName: "deletecaption"
  23245. },
  23246. {
  23247. label: lang.inserttitle,
  23248. cmdName: "inserttitle"
  23249. },
  23250. {
  23251. label: lang.deletetitle,
  23252. cmdName: "deletetitle"
  23253. },
  23254. {
  23255. label: lang.inserttitlecol,
  23256. cmdName: "inserttitlecol"
  23257. },
  23258. {
  23259. label: lang.deletetitlecol,
  23260. cmdName: "deletetitlecol"
  23261. },
  23262. "-",
  23263. {
  23264. label: lang.mergecells,
  23265. cmdName: "mergecells"
  23266. },
  23267. {
  23268. label: lang.mergeright,
  23269. cmdName: "mergeright"
  23270. },
  23271. {
  23272. label: lang.mergedown,
  23273. cmdName: "mergedown"
  23274. },
  23275. "-",
  23276. {
  23277. label: lang.splittorows,
  23278. cmdName: "splittorows"
  23279. },
  23280. {
  23281. label: lang.splittocols,
  23282. cmdName: "splittocols"
  23283. },
  23284. {
  23285. label: lang.splittocells,
  23286. cmdName: "splittocells"
  23287. },
  23288. "-",
  23289. {
  23290. label: lang.averageDiseRow,
  23291. cmdName: "averagedistributerow"
  23292. },
  23293. {
  23294. label: lang.averageDisCol,
  23295. cmdName: "averagedistributecol"
  23296. },
  23297. "-",
  23298. {
  23299. label: lang.edittd,
  23300. cmdName: "edittd",
  23301. exec: function() {
  23302. if (UE.ui["edittd"]) {
  23303. new UE.ui["edittd"](this);
  23304. }
  23305. this.getDialog("edittd").open();
  23306. }
  23307. },
  23308. {
  23309. label: lang.edittable,
  23310. cmdName: "edittable",
  23311. exec: function() {
  23312. if (UE.ui["edittable"]) {
  23313. new UE.ui["edittable"](this);
  23314. }
  23315. this.getDialog("edittable").open();
  23316. }
  23317. },
  23318. {
  23319. label: lang.setbordervisible,
  23320. cmdName: "setbordervisible"
  23321. }
  23322. ]
  23323. },
  23324. {
  23325. group: lang.tablesort,
  23326. icon: "tablesort",
  23327. subMenu: [
  23328. {
  23329. label: lang.enablesort,
  23330. cmdName: "enablesort"
  23331. },
  23332. {
  23333. label: lang.disablesort,
  23334. cmdName: "disablesort"
  23335. },
  23336. "-",
  23337. {
  23338. label: lang.reversecurrent,
  23339. cmdName: "sorttable",
  23340. value: "reversecurrent"
  23341. },
  23342. {
  23343. label: lang.orderbyasc,
  23344. cmdName: "sorttable",
  23345. value: "orderbyasc"
  23346. },
  23347. {
  23348. label: lang.reversebyasc,
  23349. cmdName: "sorttable",
  23350. value: "reversebyasc"
  23351. },
  23352. {
  23353. label: lang.orderbynum,
  23354. cmdName: "sorttable",
  23355. value: "orderbynum"
  23356. },
  23357. {
  23358. label: lang.reversebynum,
  23359. cmdName: "sorttable",
  23360. value: "reversebynum"
  23361. }
  23362. ]
  23363. },
  23364. {
  23365. group: lang.borderbk,
  23366. icon: "borderBack",
  23367. subMenu: [
  23368. {
  23369. label: lang.setcolor,
  23370. cmdName: "interlacetable",
  23371. exec: function() {
  23372. this.execCommand("interlacetable");
  23373. }
  23374. },
  23375. {
  23376. label: lang.unsetcolor,
  23377. cmdName: "uninterlacetable",
  23378. exec: function() {
  23379. this.execCommand("uninterlacetable");
  23380. }
  23381. },
  23382. {
  23383. label: lang.setbackground,
  23384. cmdName: "settablebackground",
  23385. exec: function() {
  23386. this.execCommand("settablebackground", {
  23387. repeat: true,
  23388. colorList: ["#bbb", "#ccc"]
  23389. });
  23390. }
  23391. },
  23392. {
  23393. label: lang.unsetbackground,
  23394. cmdName: "cleartablebackground",
  23395. exec: function() {
  23396. this.execCommand("cleartablebackground");
  23397. }
  23398. },
  23399. {
  23400. label: lang.redandblue,
  23401. cmdName: "settablebackground",
  23402. exec: function() {
  23403. this.execCommand("settablebackground", {
  23404. repeat: true,
  23405. colorList: ["red", "blue"]
  23406. });
  23407. }
  23408. },
  23409. {
  23410. label: lang.threecolorgradient,
  23411. cmdName: "settablebackground",
  23412. exec: function() {
  23413. this.execCommand("settablebackground", {
  23414. repeat: true,
  23415. colorList: ["#aaa", "#bbb", "#ccc"]
  23416. });
  23417. }
  23418. }
  23419. ]
  23420. },
  23421. {
  23422. group: lang.aligntd,
  23423. icon: "aligntd",
  23424. subMenu: [
  23425. {
  23426. cmdName: "cellalignment",
  23427. value: { align: "left", vAlign: "top" }
  23428. },
  23429. {
  23430. cmdName: "cellalignment",
  23431. value: { align: "center", vAlign: "top" }
  23432. },
  23433. {
  23434. cmdName: "cellalignment",
  23435. value: { align: "right", vAlign: "top" }
  23436. },
  23437. {
  23438. cmdName: "cellalignment",
  23439. value: { align: "left", vAlign: "middle" }
  23440. },
  23441. {
  23442. cmdName: "cellalignment",
  23443. value: { align: "center", vAlign: "middle" }
  23444. },
  23445. {
  23446. cmdName: "cellalignment",
  23447. value: { align: "right", vAlign: "middle" }
  23448. },
  23449. {
  23450. cmdName: "cellalignment",
  23451. value: { align: "left", vAlign: "bottom" }
  23452. },
  23453. {
  23454. cmdName: "cellalignment",
  23455. value: { align: "center", vAlign: "bottom" }
  23456. },
  23457. {
  23458. cmdName: "cellalignment",
  23459. value: { align: "right", vAlign: "bottom" }
  23460. }
  23461. ]
  23462. },
  23463. {
  23464. group: lang.aligntable,
  23465. icon: "aligntable",
  23466. subMenu: [
  23467. {
  23468. cmdName: "tablealignment",
  23469. className: "left",
  23470. label: lang.tableleft,
  23471. value: "left"
  23472. },
  23473. {
  23474. cmdName: "tablealignment",
  23475. className: "center",
  23476. label: lang.tablecenter,
  23477. value: "center"
  23478. },
  23479. {
  23480. cmdName: "tablealignment",
  23481. className: "right",
  23482. label: lang.tableright,
  23483. value: "right"
  23484. }
  23485. ]
  23486. },
  23487. "-",
  23488. {
  23489. label: lang.insertparagraphbefore,
  23490. cmdName: "insertparagraph",
  23491. value: true
  23492. },
  23493. {
  23494. label: lang.insertparagraphafter,
  23495. cmdName: "insertparagraph"
  23496. },
  23497. {
  23498. label: lang["copy"],
  23499. cmdName: "copy"
  23500. },
  23501. {
  23502. label: lang["paste"],
  23503. cmdName: "paste"
  23504. }
  23505. ];
  23506. if (!items.length) {
  23507. return;
  23508. }
  23509. var uiUtils = UE.ui.uiUtils;
  23510. me.addListener("contextmenu", function(type, evt) {
  23511. var offset = uiUtils.getViewportOffsetByEvent(evt);
  23512. me.fireEvent("beforeselectionchange");
  23513. if (menu) {
  23514. menu.destroy();
  23515. }
  23516. for (var i = 0, ti, contextItems = []; (ti = items[i]); i++) {
  23517. var last;
  23518. (function(item) {
  23519. if (item == "-") {
  23520. if ((last = contextItems[contextItems.length - 1]) && last !== "-") {
  23521. contextItems.push("-");
  23522. }
  23523. } else if (item.hasOwnProperty("group")) {
  23524. for (var j = 0, cj, subMenu = []; (cj = item.subMenu[j]); j++) {
  23525. (function(subItem) {
  23526. if (subItem == "-") {
  23527. if ((last = subMenu[subMenu.length - 1]) && last !== "-") {
  23528. subMenu.push("-");
  23529. } else {
  23530. subMenu.splice(subMenu.length - 1);
  23531. }
  23532. } else {
  23533. if (
  23534. (me.commands[subItem.cmdName] ||
  23535. UE.commands[subItem.cmdName] ||
  23536. subItem.query) &&
  23537. (subItem.query
  23538. ? subItem.query()
  23539. : me.queryCommandState(subItem.cmdName)) > -1
  23540. ) {
  23541. subMenu.push({
  23542. label:
  23543. subItem.label ||
  23544. me.getLang(
  23545. "contextMenu." +
  23546. subItem.cmdName +
  23547. (subItem.value || "")
  23548. ) ||
  23549. "",
  23550. className:
  23551. "edui-for-" +
  23552. subItem.cmdName +
  23553. (subItem.className
  23554. ? " edui-for-" +
  23555. subItem.cmdName +
  23556. "-" +
  23557. subItem.className
  23558. : ""),
  23559. onclick: subItem.exec
  23560. ? function() {
  23561. subItem.exec.call(me);
  23562. }
  23563. : function() {
  23564. me.execCommand(subItem.cmdName, subItem.value);
  23565. }
  23566. });
  23567. }
  23568. }
  23569. })(cj);
  23570. }
  23571. if (subMenu.length) {
  23572. function getLabel() {
  23573. switch (item.icon) {
  23574. case "table":
  23575. return me.getLang("contextMenu.table");
  23576. case "justifyjustify":
  23577. return me.getLang("contextMenu.paragraph");
  23578. case "aligntd":
  23579. return me.getLang("contextMenu.aligntd");
  23580. case "aligntable":
  23581. return me.getLang("contextMenu.aligntable");
  23582. case "tablesort":
  23583. return lang.tablesort;
  23584. case "borderBack":
  23585. return lang.borderbk;
  23586. default:
  23587. return "";
  23588. }
  23589. }
  23590. contextItems.push({
  23591. //todo 修正成自动获取方式
  23592. label: getLabel(),
  23593. className: "edui-for-" + item.icon,
  23594. subMenu: {
  23595. items: subMenu,
  23596. editor: me
  23597. }
  23598. });
  23599. }
  23600. } else {
  23601. //有可能commmand没有加载右键不能出来,或者没有command也想能展示出来添加query方法
  23602. if (
  23603. (me.commands[item.cmdName] ||
  23604. UE.commands[item.cmdName] ||
  23605. item.query) &&
  23606. (item.query
  23607. ? item.query.call(me)
  23608. : me.queryCommandState(item.cmdName)) > -1
  23609. ) {
  23610. contextItems.push({
  23611. label: item.label || me.getLang("contextMenu." + item.cmdName),
  23612. className:
  23613. "edui-for-" +
  23614. (item.icon ? item.icon : item.cmdName + (item.value || "")),
  23615. onclick: item.exec
  23616. ? function() {
  23617. item.exec.call(me);
  23618. }
  23619. : function() {
  23620. me.execCommand(item.cmdName, item.value);
  23621. }
  23622. });
  23623. }
  23624. }
  23625. })(ti);
  23626. }
  23627. if (contextItems[contextItems.length - 1] == "-") {
  23628. contextItems.pop();
  23629. }
  23630. menu = new UE.ui.Menu({
  23631. items: contextItems,
  23632. className: "edui-contextmenu",
  23633. editor: me
  23634. });
  23635. menu.render();
  23636. menu.showAt(offset);
  23637. me.fireEvent("aftershowcontextmenu", menu);
  23638. domUtils.preventDefault(evt);
  23639. if (browser.ie) {
  23640. var ieRange;
  23641. try {
  23642. ieRange = me.selection.getNative().createRange();
  23643. } catch (e) {
  23644. return;
  23645. }
  23646. if (ieRange.item) {
  23647. var range = new dom.Range(me.document);
  23648. range.selectNode(ieRange.item(0)).select(true, true);
  23649. }
  23650. }
  23651. });
  23652. // 添加复制的flash按钮
  23653. me.addListener("aftershowcontextmenu", function(type, menu) {
  23654. if (me.zeroclipboard) {
  23655. var items = menu.items;
  23656. for (var key in items) {
  23657. if (items[key].className == "edui-for-copy") {
  23658. me.zeroclipboard.clip(items[key].getDom());
  23659. }
  23660. }
  23661. }
  23662. });
  23663. };
  23664. // plugins/shortcutmenu.js
  23665. ///import core
  23666. ///commands 弹出菜单
  23667. // commandsName popupmenu
  23668. ///commandsTitle 弹出菜单
  23669. /**
  23670. * 弹出菜单
  23671. * @function
  23672. * @name baidu.editor.plugins.popupmenu
  23673. * @author xuheng
  23674. */
  23675. UE.plugins["shortcutmenu"] = function() {
  23676. var me = this,
  23677. menu,
  23678. items = me.options.shortcutMenu || [];
  23679. if (!items.length) {
  23680. return;
  23681. }
  23682. me.addListener("contextmenu mouseup", function(type, e) {
  23683. var me = this,
  23684. customEvt = {
  23685. type: type,
  23686. target: e.target || e.srcElement,
  23687. screenX: e.screenX,
  23688. screenY: e.screenY,
  23689. clientX: e.clientX,
  23690. clientY: e.clientY
  23691. };
  23692. setTimeout(function() {
  23693. var rng = me.selection.getRange();
  23694. if (rng.collapsed === false || type == "contextmenu") {
  23695. if (!menu) {
  23696. menu = new baidu.editor.ui.ShortCutMenu({
  23697. editor: me,
  23698. items: items,
  23699. theme: me.options.theme,
  23700. className: "edui-shortcutmenu"
  23701. });
  23702. menu.render();
  23703. me.fireEvent("afterrendershortcutmenu", menu);
  23704. }
  23705. menu.show(customEvt, !!UE.plugins["contextmenu"]);
  23706. }
  23707. });
  23708. if (type == "contextmenu") {
  23709. domUtils.preventDefault(e);
  23710. if (browser.ie9below) {
  23711. var ieRange;
  23712. try {
  23713. ieRange = me.selection.getNative().createRange();
  23714. } catch (e) {
  23715. return;
  23716. }
  23717. if (ieRange.item) {
  23718. var range = new dom.Range(me.document);
  23719. range.selectNode(ieRange.item(0)).select(true, true);
  23720. }
  23721. }
  23722. }
  23723. });
  23724. me.addListener("keydown", function(type) {
  23725. if (type == "keydown") {
  23726. menu && !menu.isHidden && menu.hide();
  23727. }
  23728. });
  23729. };
  23730. // plugins/basestyle.js
  23731. /**
  23732. * B、I、sub、super命令支持
  23733. * @file
  23734. * @since 1.2.6.1
  23735. */
  23736. UE.plugins["basestyle"] = function() {
  23737. /**
  23738. * 字体加粗
  23739. * @command bold
  23740. * @param { String } cmd 命令字符串
  23741. * @remind 对已加粗的文本内容执行该命令, 将取消加粗
  23742. * @method execCommand
  23743. * @example
  23744. * ```javascript
  23745. * //editor是编辑器实例
  23746. * //对当前选中的文本内容执行加粗操作
  23747. * //第一次执行, 文本内容加粗
  23748. * editor.execCommand( 'bold' );
  23749. *
  23750. * //第二次执行, 文本内容取消加粗
  23751. * editor.execCommand( 'bold' );
  23752. * ```
  23753. */
  23754. /**
  23755. * 字体倾斜
  23756. * @command italic
  23757. * @method execCommand
  23758. * @param { String } cmd 命令字符串
  23759. * @remind 对已倾斜的文本内容执行该命令, 将取消倾斜
  23760. * @example
  23761. * ```javascript
  23762. * //editor是编辑器实例
  23763. * //对当前选中的文本内容执行斜体操作
  23764. * //第一次操作, 文本内容将变成斜体
  23765. * editor.execCommand( 'italic' );
  23766. *
  23767. * //再次对同一文本内容执行, 则文本内容将恢复正常
  23768. * editor.execCommand( 'italic' );
  23769. * ```
  23770. */
  23771. /**
  23772. * 下标文本,与“superscript”命令互斥
  23773. * @command subscript
  23774. * @method execCommand
  23775. * @remind 把选中的文本内容切换成下标文本, 如果当前选中的文本已经是下标, 则该操作会把文本内容还原成正常文本
  23776. * @param { String } cmd 命令字符串
  23777. * @example
  23778. * ```javascript
  23779. * //editor是编辑器实例
  23780. * //对当前选中的文本内容执行下标操作
  23781. * //第一次操作, 文本内容将变成下标文本
  23782. * editor.execCommand( 'subscript' );
  23783. *
  23784. * //再次对同一文本内容执行, 则文本内容将恢复正常
  23785. * editor.execCommand( 'subscript' );
  23786. * ```
  23787. */
  23788. /**
  23789. * 上标文本,与“subscript”命令互斥
  23790. * @command superscript
  23791. * @method execCommand
  23792. * @remind 把选中的文本内容切换成上标文本, 如果当前选中的文本已经是上标, 则该操作会把文本内容还原成正常文本
  23793. * @param { String } cmd 命令字符串
  23794. * @example
  23795. * ```javascript
  23796. * //editor是编辑器实例
  23797. * //对当前选中的文本内容执行上标操作
  23798. * //第一次操作, 文本内容将变成上标文本
  23799. * editor.execCommand( 'superscript' );
  23800. *
  23801. * //再次对同一文本内容执行, 则文本内容将恢复正常
  23802. * editor.execCommand( 'superscript' );
  23803. * ```
  23804. */
  23805. var basestyles = {
  23806. bold: ["strong", "b"],
  23807. italic: ["em", "i"],
  23808. subscript: ["sub"],
  23809. superscript: ["sup"]
  23810. },
  23811. getObj = function(editor, tagNames) {
  23812. return domUtils.filterNodeList(
  23813. editor.selection.getStartElementPath(),
  23814. tagNames
  23815. );
  23816. },
  23817. me = this;
  23818. //添加快捷键
  23819. me.addshortcutkey({
  23820. Bold: "ctrl+66", //^B
  23821. Italic: "ctrl+73", //^I
  23822. Underline: "ctrl+85" //^U
  23823. });
  23824. me.addInputRule(function(root) {
  23825. utils.each(root.getNodesByTagName("b i"), function(node) {
  23826. switch (node.tagName) {
  23827. case "b":
  23828. node.tagName = "strong";
  23829. break;
  23830. case "i":
  23831. node.tagName = "em";
  23832. }
  23833. });
  23834. });
  23835. for (var style in basestyles) {
  23836. (function(cmd, tagNames) {
  23837. me.commands[cmd] = {
  23838. execCommand: function(cmdName) {
  23839. var range = me.selection.getRange(),
  23840. obj = getObj(this, tagNames);
  23841. if (range.collapsed) {
  23842. if (obj) {
  23843. var tmpText = me.document.createTextNode("");
  23844. range.insertNode(tmpText).removeInlineStyle(tagNames);
  23845. range.setStartBefore(tmpText);
  23846. domUtils.remove(tmpText);
  23847. } else {
  23848. var tmpNode = range.document.createElement(tagNames[0]);
  23849. if (cmdName == "superscript" || cmdName == "subscript") {
  23850. tmpText = me.document.createTextNode("");
  23851. range
  23852. .insertNode(tmpText)
  23853. .removeInlineStyle(["sub", "sup"])
  23854. .setStartBefore(tmpText)
  23855. .collapse(true);
  23856. }
  23857. range.insertNode(tmpNode).setStart(tmpNode, 0);
  23858. }
  23859. range.collapse(true);
  23860. } else {
  23861. if (cmdName == "superscript" || cmdName == "subscript") {
  23862. if (!obj || obj.tagName.toLowerCase() != cmdName) {
  23863. range.removeInlineStyle(["sub", "sup"]);
  23864. }
  23865. }
  23866. obj
  23867. ? range.removeInlineStyle(tagNames)
  23868. : range.applyInlineStyle(tagNames[0]);
  23869. }
  23870. range.select();
  23871. },
  23872. queryCommandState: function() {
  23873. return getObj(this, tagNames) ? 1 : 0;
  23874. }
  23875. };
  23876. })(style, basestyles[style]);
  23877. }
  23878. };
  23879. // plugins/elementpath.js
  23880. /**
  23881. * 选取路径命令
  23882. * @file
  23883. */
  23884. UE.plugins["elementpath"] = function() {
  23885. var currentLevel,
  23886. tagNames,
  23887. me = this;
  23888. me.setOpt("elementPathEnabled", true);
  23889. if (!me.options.elementPathEnabled) {
  23890. return;
  23891. }
  23892. me.commands["elementpath"] = {
  23893. execCommand: function(cmdName, level) {
  23894. var start = tagNames[level],
  23895. range = me.selection.getRange();
  23896. currentLevel = level * 1;
  23897. range.selectNode(start).select();
  23898. },
  23899. queryCommandValue: function() {
  23900. //产生一个副本,不能修改原来的startElementPath;
  23901. var parents = [].concat(this.selection.getStartElementPath()).reverse(),
  23902. names = [];
  23903. tagNames = parents;
  23904. for (var i = 0, ci; (ci = parents[i]); i++) {
  23905. if (ci.nodeType == 3) {
  23906. continue;
  23907. }
  23908. var name = ci.tagName.toLowerCase();
  23909. if (name == "img" && ci.getAttribute("anchorname")) {
  23910. name = "anchor";
  23911. }
  23912. names[i] = name;
  23913. if (currentLevel == i) {
  23914. currentLevel = -1;
  23915. break;
  23916. }
  23917. }
  23918. return names;
  23919. }
  23920. };
  23921. };
  23922. // plugins/formatmatch.js
  23923. /**
  23924. * 格式刷,只格式inline的
  23925. * @file
  23926. * @since 1.2.6.1
  23927. */
  23928. /**
  23929. * 格式刷
  23930. * @command formatmatch
  23931. * @method execCommand
  23932. * @remind 该操作不能复制段落格式
  23933. * @param { String } cmd 命令字符串
  23934. * @example
  23935. * ```javascript
  23936. * //editor是编辑器实例
  23937. * //获取格式刷
  23938. * editor.execCommand( 'formatmatch' );
  23939. * ```
  23940. */
  23941. UE.plugins["formatmatch"] = function() {
  23942. var me = this,
  23943. list = [],
  23944. img,
  23945. flag = 0;
  23946. me.addListener("reset", function() {
  23947. list = [];
  23948. flag = 0;
  23949. });
  23950. function addList(type, evt) {
  23951. if (browser.webkit) {
  23952. var target = evt.target.tagName == "IMG" ? evt.target : null;
  23953. }
  23954. function addFormat(range) {
  23955. if (text) {
  23956. range.selectNode(text);
  23957. }
  23958. return range.applyInlineStyle(list[list.length - 1].tagName, null, list);
  23959. }
  23960. me.undoManger && me.undoManger.save();
  23961. var range = me.selection.getRange(),
  23962. imgT = target || range.getClosedNode();
  23963. if (img && imgT && imgT.tagName == "IMG") {
  23964. //trace:964
  23965. imgT.style.cssText +=
  23966. ";float:" +
  23967. (img.style.cssFloat || img.style.styleFloat || "none") +
  23968. ";display:" +
  23969. (img.style.display || "inline");
  23970. img = null;
  23971. } else {
  23972. if (!img) {
  23973. var collapsed = range.collapsed;
  23974. if (collapsed) {
  23975. var text = me.document.createTextNode("match");
  23976. range.insertNode(text).select();
  23977. }
  23978. me.__hasEnterExecCommand = true;
  23979. //不能把block上的属性干掉
  23980. //trace:1553
  23981. var removeFormatAttributes = me.options.removeFormatAttributes;
  23982. me.options.removeFormatAttributes = "";
  23983. me.execCommand("removeformat");
  23984. me.options.removeFormatAttributes = removeFormatAttributes;
  23985. me.__hasEnterExecCommand = false;
  23986. //trace:969
  23987. range = me.selection.getRange();
  23988. if (list.length) {
  23989. addFormat(range);
  23990. }
  23991. if (text) {
  23992. range.setStartBefore(text).collapse(true);
  23993. }
  23994. range.select();
  23995. text && domUtils.remove(text);
  23996. }
  23997. }
  23998. me.undoManger && me.undoManger.save();
  23999. me.removeListener("mouseup", addList);
  24000. flag = 0;
  24001. }
  24002. me.commands["formatmatch"] = {
  24003. execCommand: function(cmdName) {
  24004. if (flag) {
  24005. flag = 0;
  24006. list = [];
  24007. me.removeListener("mouseup", addList);
  24008. return;
  24009. }
  24010. var range = me.selection.getRange();
  24011. img = range.getClosedNode();
  24012. if (!img || img.tagName != "IMG") {
  24013. range.collapse(true).shrinkBoundary();
  24014. var start = range.startContainer;
  24015. list = domUtils.findParents(start, true, function(node) {
  24016. return !domUtils.isBlockElm(node) && node.nodeType == 1;
  24017. });
  24018. //a不能加入格式刷, 并且克隆节点
  24019. for (var i = 0, ci; (ci = list[i]); i++) {
  24020. if (ci.tagName == "A") {
  24021. list.splice(i, 1);
  24022. break;
  24023. }
  24024. }
  24025. }
  24026. me.addListener("mouseup", addList);
  24027. flag = 1;
  24028. },
  24029. queryCommandState: function() {
  24030. return flag;
  24031. },
  24032. notNeedUndo: 1
  24033. };
  24034. };
  24035. // plugins/searchreplace.js
  24036. ///import core
  24037. ///commands 查找替换
  24038. ///commandsName SearchReplace
  24039. ///commandsTitle 查询替换
  24040. ///commandsDialog dialogs\searchreplace
  24041. /**
  24042. * @description 查找替换
  24043. * @author zhanyi
  24044. */
  24045. UE.plugin.register("searchreplace", function() {
  24046. var me = this;
  24047. var _blockElm = { table: 1, tbody: 1, tr: 1, ol: 1, ul: 1 };
  24048. var lastRng = null;
  24049. function getText(node) {
  24050. var text = node.nodeType == 3
  24051. ? node.nodeValue
  24052. : node[browser.ie ? "innerText" : "textContent"];
  24053. return text.replace(domUtils.fillChar, "");
  24054. }
  24055. function findTextInString(textContent, opt, currentIndex) {
  24056. var str = opt.searchStr;
  24057. var reg = new RegExp(str, "g" + (opt.casesensitive ? "" : "i")),
  24058. match;
  24059. if (opt.dir == -1) {
  24060. textContent = textContent.substr(0, currentIndex);
  24061. textContent = textContent.split("").reverse().join("");
  24062. str = str.split("").reverse().join("");
  24063. match = reg.exec(textContent);
  24064. if (match) {
  24065. return currentIndex - match.index - str.length;
  24066. }
  24067. } else {
  24068. textContent = textContent.substr(currentIndex);
  24069. match = reg.exec(textContent);
  24070. if (match) {
  24071. return match.index + currentIndex;
  24072. }
  24073. }
  24074. return -1;
  24075. }
  24076. function findTextBlockElm(node, currentIndex, opt) {
  24077. var textContent,
  24078. index,
  24079. methodName = opt.all || opt.dir == 1 ? "getNextDomNode" : "getPreDomNode";
  24080. if (domUtils.isBody(node)) {
  24081. node = node.firstChild;
  24082. }
  24083. var first = 1;
  24084. while (node) {
  24085. textContent = getText(node);
  24086. index = findTextInString(textContent, opt, currentIndex);
  24087. first = 0;
  24088. if (index != -1) {
  24089. return {
  24090. node: node,
  24091. index: index
  24092. };
  24093. }
  24094. node = domUtils[methodName](node);
  24095. while (node && _blockElm[node.nodeName.toLowerCase()]) {
  24096. node = domUtils[methodName](node, true);
  24097. }
  24098. if (node) {
  24099. currentIndex = opt.dir == -1 ? getText(node).length : 0;
  24100. }
  24101. }
  24102. }
  24103. function findNTextInBlockElm(node, index, str) {
  24104. var currentIndex = 0,
  24105. currentNode = node.firstChild,
  24106. currentNodeLength = 0,
  24107. result;
  24108. while (currentNode) {
  24109. if (currentNode.nodeType == 3) {
  24110. currentNodeLength = getText(currentNode).replace(
  24111. /(^[\t\r\n]+)|([\t\r\n]+$)/,
  24112. ""
  24113. ).length;
  24114. currentIndex += currentNodeLength;
  24115. if (currentIndex >= index) {
  24116. return {
  24117. node: currentNode,
  24118. index: currentNodeLength - (currentIndex - index)
  24119. };
  24120. }
  24121. } else if (!dtd.$empty[currentNode.tagName]) {
  24122. currentNodeLength = getText(currentNode).replace(
  24123. /(^[\t\r\n]+)|([\t\r\n]+$)/,
  24124. ""
  24125. ).length;
  24126. currentIndex += currentNodeLength;
  24127. if (currentIndex >= index) {
  24128. result = findNTextInBlockElm(
  24129. currentNode,
  24130. currentNodeLength - (currentIndex - index),
  24131. str
  24132. );
  24133. if (result) {
  24134. return result;
  24135. }
  24136. }
  24137. }
  24138. currentNode = domUtils.getNextDomNode(currentNode);
  24139. }
  24140. }
  24141. function searchReplace(me, opt) {
  24142. var rng = lastRng || me.selection.getRange(),
  24143. startBlockNode,
  24144. searchStr = opt.searchStr,
  24145. span = me.document.createElement("span");
  24146. span.innerHTML = "$$ueditor_searchreplace_key$$";
  24147. rng.shrinkBoundary(true);
  24148. //判断是不是第一次选中
  24149. if (!rng.collapsed) {
  24150. rng.select();
  24151. var rngText = me.selection.getText();
  24152. if (
  24153. new RegExp(
  24154. "^" + opt.searchStr + "$",
  24155. opt.casesensitive ? "" : "i"
  24156. ).test(rngText)
  24157. ) {
  24158. if (opt.replaceStr != undefined) {
  24159. replaceText(rng, opt.replaceStr);
  24160. rng.select();
  24161. return true;
  24162. } else {
  24163. rng.collapse(opt.dir == -1);
  24164. }
  24165. }
  24166. }
  24167. rng.insertNode(span);
  24168. rng.enlargeToBlockElm(true);
  24169. startBlockNode = rng.startContainer;
  24170. var currentIndex = getText(startBlockNode).indexOf(
  24171. "$$ueditor_searchreplace_key$$"
  24172. );
  24173. rng.setStartBefore(span);
  24174. domUtils.remove(span);
  24175. var result = findTextBlockElm(startBlockNode, currentIndex, opt);
  24176. if (result) {
  24177. var rngStart = findNTextInBlockElm(result.node, result.index, searchStr);
  24178. var rngEnd = findNTextInBlockElm(
  24179. result.node,
  24180. result.index + searchStr.length,
  24181. searchStr
  24182. );
  24183. rng
  24184. .setStart(rngStart.node, rngStart.index)
  24185. .setEnd(rngEnd.node, rngEnd.index);
  24186. if (opt.replaceStr !== undefined) {
  24187. replaceText(rng, opt.replaceStr);
  24188. }
  24189. rng.select();
  24190. return true;
  24191. } else {
  24192. rng.setCursor();
  24193. }
  24194. }
  24195. function replaceText(rng, str) {
  24196. str = me.document.createTextNode(str);
  24197. rng.deleteContents().insertNode(str);
  24198. }
  24199. return {
  24200. commands: {
  24201. searchreplace: {
  24202. execCommand: function(cmdName, opt) {
  24203. utils.extend(
  24204. opt,
  24205. {
  24206. all: false,
  24207. casesensitive: false,
  24208. dir: 1
  24209. },
  24210. true
  24211. );
  24212. var num = 0;
  24213. if (opt.all) {
  24214. lastRng = null;
  24215. var rng = me.selection.getRange(),
  24216. first = me.body.firstChild;
  24217. if (first && first.nodeType == 1) {
  24218. rng.setStart(first, 0);
  24219. rng.shrinkBoundary(true);
  24220. } else if (first.nodeType == 3) {
  24221. rng.setStartBefore(first);
  24222. }
  24223. rng.collapse(true).select(true);
  24224. if (opt.replaceStr !== undefined) {
  24225. me.fireEvent("saveScene");
  24226. }
  24227. while (searchReplace(this, opt)) {
  24228. num++;
  24229. lastRng = me.selection.getRange();
  24230. lastRng.collapse(opt.dir == -1);
  24231. }
  24232. if (num) {
  24233. me.fireEvent("saveScene");
  24234. }
  24235. } else {
  24236. if (opt.replaceStr !== undefined) {
  24237. me.fireEvent("saveScene");
  24238. }
  24239. if (searchReplace(this, opt)) {
  24240. num++;
  24241. lastRng = me.selection.getRange();
  24242. lastRng.collapse(opt.dir == -1);
  24243. }
  24244. if (num) {
  24245. me.fireEvent("saveScene");
  24246. }
  24247. }
  24248. return num;
  24249. },
  24250. notNeedUndo: 1
  24251. }
  24252. },
  24253. bindEvents: {
  24254. clearlastSearchResult: function() {
  24255. lastRng = null;
  24256. }
  24257. }
  24258. };
  24259. });
  24260. // plugins/customstyle.js
  24261. /**
  24262. * 自定义样式
  24263. * @file
  24264. * @since 1.2.6.1
  24265. */
  24266. /**
  24267. * 根据config配置文件里“customstyle”选项的值对匹配的标签执行样式替换。
  24268. * @command customstyle
  24269. * @method execCommand
  24270. * @param { String } cmd 命令字符串
  24271. * @example
  24272. * ```javascript
  24273. * editor.execCommand( 'customstyle' );
  24274. * ```
  24275. */
  24276. UE.plugins["customstyle"] = function() {
  24277. var me = this;
  24278. me.setOpt({
  24279. customstyle: [
  24280. {
  24281. tag: "h1",
  24282. name: "tc",
  24283. style:
  24284. "font-size:32px;font-weight:bold;border-bottom:#ccc 2px solid;padding:0 4px 0 0;text-align:center;margin:0 0 20px 0;"
  24285. },
  24286. {
  24287. tag: "h1",
  24288. name: "tl",
  24289. style:
  24290. "font-size:32px;font-weight:bold;border-bottom:#ccc 2px solid;padding:0 4px 0 0;text-align:left;margin:0 0 10px 0;"
  24291. },
  24292. {
  24293. tag: "span",
  24294. name: "im",
  24295. style:
  24296. "font-size:16px;font-style:italic;font-weight:bold;line-height:18px;"
  24297. },
  24298. {
  24299. tag: "span",
  24300. name: "hi",
  24301. style:
  24302. "font-size:16px;font-style:italic;font-weight:bold;color:rgb(51, 153, 204);line-height:18px;"
  24303. }
  24304. ]
  24305. });
  24306. me.commands["customstyle"] = {
  24307. execCommand: function(cmdName, obj) {
  24308. var me = this,
  24309. tagName = obj.tag,
  24310. node = domUtils.findParent(
  24311. me.selection.getStart(),
  24312. function(node) {
  24313. return node.getAttribute("label");
  24314. },
  24315. true
  24316. ),
  24317. range,
  24318. bk,
  24319. tmpObj = {};
  24320. for (var p in obj) {
  24321. if (obj[p] !== undefined) tmpObj[p] = obj[p];
  24322. }
  24323. delete tmpObj.tag;
  24324. if (node && node.getAttribute("label") == obj.label) {
  24325. range = this.selection.getRange();
  24326. bk = range.createBookmark();
  24327. if (range.collapsed) {
  24328. //trace:1732 删掉自定义标签,要有p来回填站位
  24329. if (dtd.$block[node.tagName]) {
  24330. var fillNode = me.document.createElement("p");
  24331. domUtils.moveChild(node, fillNode);
  24332. node.parentNode.insertBefore(fillNode, node);
  24333. domUtils.remove(node);
  24334. } else {
  24335. domUtils.remove(node, true);
  24336. }
  24337. } else {
  24338. var common = domUtils.getCommonAncestor(bk.start, bk.end),
  24339. nodes = domUtils.getElementsByTagName(common, tagName);
  24340. if (new RegExp(tagName, "i").test(common.tagName)) {
  24341. nodes.push(common);
  24342. }
  24343. for (var i = 0, ni; (ni = nodes[i++]); ) {
  24344. if (ni.getAttribute("label") == obj.label) {
  24345. var ps = domUtils.getPosition(ni, bk.start),
  24346. pe = domUtils.getPosition(ni, bk.end);
  24347. if (
  24348. (ps & domUtils.POSITION_FOLLOWING ||
  24349. ps & domUtils.POSITION_CONTAINS) &&
  24350. (pe & domUtils.POSITION_PRECEDING ||
  24351. pe & domUtils.POSITION_CONTAINS)
  24352. )
  24353. if (dtd.$block[tagName]) {
  24354. var fillNode = me.document.createElement("p");
  24355. domUtils.moveChild(ni, fillNode);
  24356. ni.parentNode.insertBefore(fillNode, ni);
  24357. }
  24358. domUtils.remove(ni, true);
  24359. }
  24360. }
  24361. node = domUtils.findParent(
  24362. common,
  24363. function(node) {
  24364. return node.getAttribute("label") == obj.label;
  24365. },
  24366. true
  24367. );
  24368. if (node) {
  24369. domUtils.remove(node, true);
  24370. }
  24371. }
  24372. range.moveToBookmark(bk).select();
  24373. } else {
  24374. if (dtd.$block[tagName]) {
  24375. this.execCommand("paragraph", tagName, tmpObj, "customstyle");
  24376. range = me.selection.getRange();
  24377. if (!range.collapsed) {
  24378. range.collapse();
  24379. node = domUtils.findParent(
  24380. me.selection.getStart(),
  24381. function(node) {
  24382. return node.getAttribute("label") == obj.label;
  24383. },
  24384. true
  24385. );
  24386. var pNode = me.document.createElement("p");
  24387. domUtils.insertAfter(node, pNode);
  24388. domUtils.fillNode(me.document, pNode);
  24389. range.setStart(pNode, 0).setCursor();
  24390. }
  24391. } else {
  24392. range = me.selection.getRange();
  24393. if (range.collapsed) {
  24394. node = me.document.createElement(tagName);
  24395. domUtils.setAttributes(node, tmpObj);
  24396. range.insertNode(node).setStart(node, 0).setCursor();
  24397. return;
  24398. }
  24399. bk = range.createBookmark();
  24400. range.applyInlineStyle(tagName, tmpObj).moveToBookmark(bk).select();
  24401. }
  24402. }
  24403. },
  24404. queryCommandValue: function() {
  24405. var parent = domUtils.filterNodeList(
  24406. this.selection.getStartElementPath(),
  24407. function(node) {
  24408. return node.getAttribute("label");
  24409. }
  24410. );
  24411. return parent ? parent.getAttribute("label") : "";
  24412. }
  24413. };
  24414. //当去掉customstyle是,如果是块元素,用p代替
  24415. me.addListener("keyup", function(type, evt) {
  24416. var keyCode = evt.keyCode || evt.which;
  24417. if (keyCode == 32 || keyCode == 13) {
  24418. var range = me.selection.getRange();
  24419. if (range.collapsed) {
  24420. var node = domUtils.findParent(
  24421. me.selection.getStart(),
  24422. function(node) {
  24423. return node.getAttribute("label");
  24424. },
  24425. true
  24426. );
  24427. if (node && dtd.$block[node.tagName] && domUtils.isEmptyNode(node)) {
  24428. var p = me.document.createElement("p");
  24429. domUtils.insertAfter(node, p);
  24430. domUtils.fillNode(me.document, p);
  24431. domUtils.remove(node);
  24432. range.setStart(p, 0).setCursor();
  24433. }
  24434. }
  24435. }
  24436. });
  24437. };
  24438. // plugins/catchremoteimage.js
  24439. ///import core
  24440. ///commands 远程图片抓取
  24441. ///commandsName catchRemoteImage,catchremoteimageenable
  24442. ///commandsTitle 远程图片抓取
  24443. /**
  24444. * 远程图片抓取,当开启本插件时所有不符合本地域名的图片都将被抓取成为本地服务器上的图片
  24445. */
  24446. UE.plugins["catchremoteimage"] = function() {
  24447. var me = this,
  24448. ajax = UE.ajax;
  24449. /* 设置默认值 */
  24450. if (me.options.catchRemoteImageEnable === false) return;
  24451. me.setOpt({
  24452. catchRemoteImageEnable: false
  24453. });
  24454. me.addListener("afterpaste", function() {
  24455. me.fireEvent("catchRemoteImage");
  24456. });
  24457. me.addListener("catchRemoteImage", function() {
  24458. var catcherLocalDomain = me.getOpt("catcherLocalDomain"),
  24459. catcherActionUrl = me.getActionUrl(me.getOpt("catcherActionName")),
  24460. catcherUrlPrefix = me.getOpt("catcherUrlPrefix"),
  24461. catcherFieldName = me.getOpt("catcherFieldName");
  24462. var remoteImages = [],
  24463. loadingIMG = me.options.themePath + me.options.theme + '/images/spacer.gif',
  24464. imgs = me.document.querySelectorAll('[style*="url"],img'),
  24465. test = function(src, urls) {
  24466. if (src.indexOf(location.host) != -1 || /(^\.)|(^\/)/.test(src)) {
  24467. return true;
  24468. }
  24469. if (urls) {
  24470. for (var j = 0, url; (url = urls[j++]); ) {
  24471. if (src.indexOf(url) !== -1) {
  24472. return true;
  24473. }
  24474. }
  24475. }
  24476. return false;
  24477. };
  24478. for (var i = 0, ci; (ci = imgs[i++]); ) {
  24479. if (ci.getAttribute("word_img")) {
  24480. continue;
  24481. }
  24482. if(ci.nodeName == "IMG"){
  24483. var src = ci.getAttribute("_src") || ci.src || "";
  24484. if (/^(https?|ftp):/i.test(src) && !test(src, catcherLocalDomain)) {
  24485. remoteImages.push(src);
  24486. // 添加上传时的uploading动画
  24487. domUtils.setAttributes(ci, {
  24488. class: "loadingclass",
  24489. _src: src,
  24490. src: loadingIMG
  24491. })
  24492. }
  24493. } else {
  24494. // 获取背景图片url
  24495. var backgroundImageurl = ci.style.cssText.replace(/.*\s?url\([\'\"]?/, '').replace(/[\'\"]?\).*/, '');
  24496. if (/^(https?|ftp):/i.test(backgroundImageurl) && !test(backgroundImageurl, catcherLocalDomain)) {
  24497. remoteImages.push(backgroundImageurl);
  24498. ci.style.cssText = ci.style.cssText.replace(backgroundImageurl, loadingIMG);
  24499. domUtils.setAttributes(ci, {
  24500. "data-background": backgroundImageurl
  24501. })
  24502. }
  24503. }
  24504. }
  24505. if (remoteImages.length) {
  24506. catchremoteimage(remoteImages, {
  24507. //成功抓取
  24508. success: function(r) {
  24509. try {
  24510. var info = r.state !== undefined
  24511. ? r
  24512. : eval("(" + r.responseText + ")");
  24513. } catch (e) {
  24514. return;
  24515. }
  24516. /* 获取源路径和新路径 */
  24517. var i,
  24518. j,
  24519. ci,
  24520. cj,
  24521. oldSrc,
  24522. newSrc,
  24523. list = info.list;
  24524. /* 抓取失败统计 */
  24525. var catchFailList = [];
  24526. /* 抓取成功统计 */
  24527. var catchSuccessList = [];
  24528. /* 抓取失败时显示的图片 */
  24529. var failIMG = me.options.themePath + me.options.theme + '/images/img-cracked.png';
  24530. for (i = 0; ci = imgs[i++];) {
  24531. oldSrc = ci.getAttribute("_src") || ci.src || "";
  24532. oldBgIMG = ci.getAttribute("data-background") || "";
  24533. for (j = 0; cj = list[j++];) {
  24534. if (oldSrc == cj.source && cj.state == "SUCCESS") {
  24535. newSrc = catcherUrlPrefix + cj.url;
  24536. // 上传成功是删除uploading动画
  24537. domUtils.removeClasses( ci, "loadingclass" );
  24538. domUtils.setAttributes(ci, {
  24539. "src": newSrc,
  24540. "_src": newSrc,
  24541. "data-catchResult":"img_catchSuccess" // 添加catch成功标记
  24542. });
  24543. catchSuccessList.push(ci);
  24544. break;
  24545. } else if (oldSrc == cj.source && cj.state == "FAIL") {
  24546. // 替换成统一的失败图片
  24547. domUtils.removeClasses( ci, "loadingclass" );
  24548. domUtils.setAttributes(ci, {
  24549. "src": failIMG,
  24550. "_src": failIMG,
  24551. "data-catchResult":"img_catchFail" // 添加catch失败标记
  24552. });
  24553. catchFailList.push(ci);
  24554. break;
  24555. } else if (oldBgIMG == cj.source && cj.state == "SUCCESS") {
  24556. newBgIMG = catcherUrlPrefix + cj.url;
  24557. ci.style.cssText = ci.style.cssText.replace(loadingIMG, newBgIMG);
  24558. domUtils.removeAttributes(ci,"data-background");
  24559. domUtils.setAttributes(ci, {
  24560. "data-catchResult":"img_catchSuccess" // 添加catch成功标记
  24561. });
  24562. catchSuccessList.push(ci);
  24563. break;
  24564. } else if (oldBgIMG == cj.source && cj.state == "FAIL"){
  24565. ci.style.cssText = ci.style.cssText.replace(loadingIMG, failIMG);
  24566. domUtils.removeAttributes(ci,"data-background");
  24567. domUtils.setAttributes(ci, {
  24568. "data-catchResult":"img_catchFail" // 添加catch失败标记
  24569. });
  24570. catchFailList.push(ci);
  24571. break;
  24572. }
  24573. }
  24574. }
  24575. // 监听事件添加成功抓取和抓取失败的dom列表参数
  24576. me.fireEvent('catchremotesuccess',catchSuccessList,catchFailList);
  24577. },
  24578. //回调失败,本次请求超时
  24579. error: function() {
  24580. me.fireEvent("catchremoteerror");
  24581. }
  24582. });
  24583. }
  24584. function catchremoteimage(imgs, callbacks) {
  24585. var params =
  24586. utils.serializeParam(me.queryCommandValue("serverparam")) || "",
  24587. url = utils.formatUrl(
  24588. catcherActionUrl +
  24589. (catcherActionUrl.indexOf("?") == -1 ? "?" : "&") +
  24590. params
  24591. ),
  24592. isJsonp = utils.isCrossDomainUrl(url),
  24593. opt = {
  24594. method: "POST",
  24595. dataType: isJsonp ? "jsonp" : "",
  24596. timeout: 60000, //单位:毫秒,回调请求超时设置。目标用户如果网速不是很快的话此处建议设置一个较大的数值
  24597. onsuccess: callbacks["success"],
  24598. onerror: callbacks["error"]
  24599. };
  24600. opt[catcherFieldName] = imgs;
  24601. ajax.request(url, opt);
  24602. }
  24603. });
  24604. };
  24605. // plugins/snapscreen.js
  24606. /**
  24607. * 截屏插件,为UEditor提供插入支持
  24608. * @file
  24609. * @since 1.4.2
  24610. */
  24611. UE.plugin.register("snapscreen", function() {
  24612. var me = this;
  24613. var snapplugin;
  24614. function getLocation(url) {
  24615. var search,
  24616. a = document.createElement("a"),
  24617. params = utils.serializeParam(me.queryCommandValue("serverparam")) || "";
  24618. a.href = url;
  24619. if (browser.ie) {
  24620. a.href = a.href;
  24621. }
  24622. search = a.search;
  24623. if (params) {
  24624. search = search + (search.indexOf("?") == -1 ? "?" : "&") + params;
  24625. search = search.replace(/[&]+/gi, "&");
  24626. }
  24627. return {
  24628. port: a.port,
  24629. hostname: a.hostname,
  24630. path: a.pathname + search || +a.hash
  24631. };
  24632. }
  24633. return {
  24634. commands: {
  24635. /**
  24636. * 字体背景颜色
  24637. * @command snapscreen
  24638. * @method execCommand
  24639. * @param { String } cmd 命令字符串
  24640. * @example
  24641. * ```javascript
  24642. * editor.execCommand('snapscreen');
  24643. * ```
  24644. */
  24645. snapscreen: {
  24646. execCommand: function(cmd) {
  24647. var url, local, res;
  24648. var lang = me.getLang("snapScreen_plugin");
  24649. if (!snapplugin) {
  24650. var container = me.container;
  24651. var doc = me.container.ownerDocument || me.container.document;
  24652. snapplugin = doc.createElement("object");
  24653. try {
  24654. snapplugin.type = "application/x-pluginbaidusnap";
  24655. } catch (e) {
  24656. return;
  24657. }
  24658. snapplugin.style.cssText =
  24659. "position:absolute;left:-9999px;width:0;height:0;";
  24660. snapplugin.setAttribute("width", "0");
  24661. snapplugin.setAttribute("height", "0");
  24662. container.appendChild(snapplugin);
  24663. }
  24664. function onSuccess(rs) {
  24665. try {
  24666. rs = eval("(" + rs + ")");
  24667. if (rs.state == "SUCCESS") {
  24668. var opt = me.options;
  24669. me.execCommand("insertimage", {
  24670. src: opt.snapscreenUrlPrefix + rs.url,
  24671. _src: opt.snapscreenUrlPrefix + rs.url,
  24672. alt: rs.title || "",
  24673. floatStyle: opt.snapscreenImgAlign
  24674. });
  24675. } else {
  24676. alert(rs.state);
  24677. }
  24678. } catch (e) {
  24679. alert(lang.callBackErrorMsg);
  24680. }
  24681. }
  24682. url = me.getActionUrl(me.getOpt("snapscreenActionName"));
  24683. local = getLocation(url);
  24684. setTimeout(function() {
  24685. try {
  24686. res = snapplugin.saveSnapshot(
  24687. local.hostname,
  24688. local.path,
  24689. local.port
  24690. );
  24691. } catch (e) {
  24692. me.ui._dialogs["snapscreenDialog"].open();
  24693. return;
  24694. }
  24695. onSuccess(res);
  24696. }, 50);
  24697. },
  24698. queryCommandState: function() {
  24699. return navigator.userAgent.indexOf("Windows", 0) != -1 ? 0 : -1;
  24700. }
  24701. }
  24702. }
  24703. };
  24704. });
  24705. // plugins/insertparagraph.js
  24706. /**
  24707. * 插入段落
  24708. * @file
  24709. * @since 1.2.6.1
  24710. */
  24711. /**
  24712. * 插入段落
  24713. * @command insertparagraph
  24714. * @method execCommand
  24715. * @param { String } cmd 命令字符串
  24716. * @example
  24717. * ```javascript
  24718. * //editor是编辑器实例
  24719. * editor.execCommand( 'insertparagraph' );
  24720. * ```
  24721. */
  24722. UE.commands["insertparagraph"] = {
  24723. execCommand: function(cmdName, front) {
  24724. var me = this,
  24725. range = me.selection.getRange(),
  24726. start = range.startContainer,
  24727. tmpNode;
  24728. while (start) {
  24729. if (domUtils.isBody(start)) {
  24730. break;
  24731. }
  24732. tmpNode = start;
  24733. start = start.parentNode;
  24734. }
  24735. if (tmpNode) {
  24736. var p = me.document.createElement("p");
  24737. if (front) {
  24738. tmpNode.parentNode.insertBefore(p, tmpNode);
  24739. } else {
  24740. tmpNode.parentNode.insertBefore(p, tmpNode.nextSibling);
  24741. }
  24742. domUtils.fillNode(me.document, p);
  24743. range.setStart(p, 0).setCursor(false, true);
  24744. }
  24745. }
  24746. };
  24747. // plugins/webapp.js
  24748. /**
  24749. * 百度应用
  24750. * @file
  24751. * @since 1.2.6.1
  24752. */
  24753. /**
  24754. * 插入百度应用
  24755. * @command webapp
  24756. * @method execCommand
  24757. * @remind 需要百度APPKey
  24758. * @remind 百度应用主页: <a href="http://app.baidu.com/" target="_blank">http://app.baidu.com/</a>
  24759. * @param { Object } appOptions 应用所需的参数项, 支持的key有: title=>应用标题, width=>应用容器宽度,
  24760. * height=>应用容器高度,logo=>应用logo,url=>应用地址
  24761. * @example
  24762. * ```javascript
  24763. * //editor是编辑器实例
  24764. * //在编辑器里插入一个“植物大战僵尸”的APP
  24765. * editor.execCommand( 'webapp' , {
  24766. * title: '植物大战僵尸',
  24767. * width: 560,
  24768. * height: 465,
  24769. * logo: '应用展示的图片',
  24770. * url: '百度应用的地址'
  24771. * } );
  24772. * ```
  24773. */
  24774. //UE.plugins['webapp'] = function () {
  24775. // var me = this;
  24776. // function createInsertStr( obj, toIframe, addParagraph ) {
  24777. // return !toIframe ?
  24778. // (addParagraph ? '<p>' : '') + '<img title="'+obj.title+'" width="' + obj.width + '" height="' + obj.height + '"' +
  24779. // ' 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 + '" />' +
  24780. // (addParagraph ? '</p>' : '')
  24781. // :
  24782. // '<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>';
  24783. // }
  24784. //
  24785. // function switchImgAndIframe( img2frame ) {
  24786. // var tmpdiv,
  24787. // nodes = domUtils.getElementsByTagName( me.document, !img2frame ? "iframe" : "img" );
  24788. // for ( var i = 0, node; node = nodes[i++]; ) {
  24789. // if ( node.className != "edui-faked-webapp" ){
  24790. // continue;
  24791. // }
  24792. // tmpdiv = me.document.createElement( "div" );
  24793. // 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 );
  24794. // node.parentNode.replaceChild( tmpdiv.firstChild, node );
  24795. // }
  24796. // }
  24797. //
  24798. // me.addListener( "beforegetcontent", function () {
  24799. // switchImgAndIframe( true );
  24800. // } );
  24801. // me.addListener( 'aftersetcontent', function () {
  24802. // switchImgAndIframe( false );
  24803. // } );
  24804. // me.addListener( 'aftergetcontent', function ( cmdName ) {
  24805. // if ( cmdName == 'aftergetcontent' && me.queryCommandState( 'source' ) ){
  24806. // return;
  24807. // }
  24808. // switchImgAndIframe( false );
  24809. // } );
  24810. //
  24811. // me.commands['webapp'] = {
  24812. // execCommand:function ( cmd, obj ) {
  24813. // me.execCommand( "inserthtml", createInsertStr( obj, false,true ) );
  24814. // }
  24815. // };
  24816. //};
  24817. UE.plugin.register("webapp", function() {
  24818. var me = this;
  24819. function createInsertStr(obj, toEmbed) {
  24820. return !toEmbed
  24821. ? '<img title="' +
  24822. obj.title +
  24823. '" width="' +
  24824. obj.width +
  24825. '" height="' +
  24826. obj.height +
  24827. '"' +
  24828. ' src="' +
  24829. me.options.UEDITOR_HOME_URL +
  24830. 'themes/default/images/spacer.gif" _logo_url="' +
  24831. obj.logo +
  24832. '" style="background:url(' +
  24833. obj.logo +
  24834. ') no-repeat center center; border:1px solid gray;" class="edui-faked-webapp" _url="' +
  24835. obj.url +
  24836. '" ' +
  24837. (obj.align && !obj.cssfloat ? 'align="' + obj.align + '"' : "") +
  24838. (obj.cssfloat ? 'style="float:' + obj.cssfloat + '"' : "") +
  24839. "/>"
  24840. : '<iframe class="edui-faked-webapp" title="' +
  24841. obj.title +
  24842. '" ' +
  24843. (obj.align && !obj.cssfloat ? 'align="' + obj.align + '"' : "") +
  24844. (obj.cssfloat ? 'style="float:' + obj.cssfloat + '"' : "") +
  24845. 'width="' +
  24846. obj.width +
  24847. '" height="' +
  24848. obj.height +
  24849. '" scrolling="no" frameborder="0" src="' +
  24850. obj.url +
  24851. '" logo_url = "' +
  24852. obj.logo +
  24853. '"></iframe>';
  24854. }
  24855. return {
  24856. outputRule: function(root) {
  24857. utils.each(root.getNodesByTagName("img"), function(node) {
  24858. var html;
  24859. if (node.getAttr("class") == "edui-faked-webapp") {
  24860. html = createInsertStr(
  24861. {
  24862. title: node.getAttr("title"),
  24863. width: node.getAttr("width"),
  24864. height: node.getAttr("height"),
  24865. align: node.getAttr("align"),
  24866. cssfloat: node.getStyle("float"),
  24867. url: node.getAttr("_url"),
  24868. logo: node.getAttr("_logo_url")
  24869. },
  24870. true
  24871. );
  24872. var embed = UE.uNode.createElement(html);
  24873. node.parentNode.replaceChild(embed, node);
  24874. }
  24875. });
  24876. },
  24877. inputRule: function(root) {
  24878. utils.each(root.getNodesByTagName("iframe"), function(node) {
  24879. if (node.getAttr("class") == "edui-faked-webapp") {
  24880. var img = UE.uNode.createElement(
  24881. createInsertStr({
  24882. title: node.getAttr("title"),
  24883. width: node.getAttr("width"),
  24884. height: node.getAttr("height"),
  24885. align: node.getAttr("align"),
  24886. cssfloat: node.getStyle("float"),
  24887. url: node.getAttr("src"),
  24888. logo: node.getAttr("logo_url")
  24889. })
  24890. );
  24891. node.parentNode.replaceChild(img, node);
  24892. }
  24893. });
  24894. },
  24895. commands: {
  24896. /**
  24897. * 插入百度应用
  24898. * @command webapp
  24899. * @method execCommand
  24900. * @remind 需要百度APPKey
  24901. * @remind 百度应用主页: <a href="http://app.baidu.com/" target="_blank">http://app.baidu.com/</a>
  24902. * @param { Object } appOptions 应用所需的参数项, 支持的key有: title=>应用标题, width=>应用容器宽度,
  24903. * height=>应用容器高度,logo=>应用logo,url=>应用地址
  24904. * @example
  24905. * ```javascript
  24906. * //editor是编辑器实例
  24907. * //在编辑器里插入一个“植物大战僵尸”的APP
  24908. * editor.execCommand( 'webapp' , {
  24909. * title: '植物大战僵尸',
  24910. * width: 560,
  24911. * height: 465,
  24912. * logo: '应用展示的图片',
  24913. * url: '百度应用的地址'
  24914. * } );
  24915. * ```
  24916. */
  24917. webapp: {
  24918. execCommand: function(cmd, obj) {
  24919. var me = this,
  24920. str = createInsertStr(
  24921. utils.extend(obj, {
  24922. align: "none"
  24923. }),
  24924. false
  24925. );
  24926. me.execCommand("inserthtml", str);
  24927. },
  24928. queryCommandState: function() {
  24929. var me = this,
  24930. img = me.selection.getRange().getClosedNode(),
  24931. flag = img && img.className == "edui-faked-webapp";
  24932. return flag ? 1 : 0;
  24933. }
  24934. }
  24935. }
  24936. };
  24937. });
  24938. // plugins/template.js
  24939. ///import core
  24940. ///import plugins\inserthtml.js
  24941. ///import plugins\cleardoc.js
  24942. ///commands 模板
  24943. ///commandsName template
  24944. ///commandsTitle 模板
  24945. ///commandsDialog dialogs\template
  24946. UE.plugins["template"] = function() {
  24947. UE.commands["template"] = {
  24948. execCommand: function(cmd, obj) {
  24949. obj.html && this.execCommand("inserthtml", obj.html);
  24950. }
  24951. };
  24952. this.addListener("click", function(type, evt) {
  24953. var el = evt.target || evt.srcElement,
  24954. range = this.selection.getRange();
  24955. var tnode = domUtils.findParent(
  24956. el,
  24957. function(node) {
  24958. if (node.className && domUtils.hasClass(node, "ue_t")) {
  24959. return node;
  24960. }
  24961. },
  24962. true
  24963. );
  24964. tnode && range.selectNode(tnode).shrinkBoundary().select();
  24965. });
  24966. this.addListener("keydown", function(type, evt) {
  24967. var range = this.selection.getRange();
  24968. if (!range.collapsed) {
  24969. if (!evt.ctrlKey && !evt.metaKey && !evt.shiftKey && !evt.altKey) {
  24970. var tnode = domUtils.findParent(
  24971. range.startContainer,
  24972. function(node) {
  24973. if (node.className && domUtils.hasClass(node, "ue_t")) {
  24974. return node;
  24975. }
  24976. },
  24977. true
  24978. );
  24979. if (tnode) {
  24980. domUtils.removeClasses(tnode, ["ue_t"]);
  24981. }
  24982. }
  24983. }
  24984. });
  24985. };
  24986. // plugins/music.js
  24987. /**
  24988. * 插入音乐命令
  24989. * @file
  24990. */
  24991. UE.plugin.register("music", function() {
  24992. var me = this;
  24993. function creatInsertStr(url, width, height, align, cssfloat, toEmbed) {
  24994. return !toEmbed
  24995. ? "<img " +
  24996. (align && !cssfloat ? 'align="' + align + '"' : "") +
  24997. (cssfloat ? 'style="float:' + cssfloat + '"' : "") +
  24998. ' width="' +
  24999. width +
  25000. '" height="' +
  25001. height +
  25002. '" _url="' +
  25003. url +
  25004. '" class="edui-faked-music"' +
  25005. ' src="' +
  25006. me.options.langPath +
  25007. me.options.lang +
  25008. '/images/music.png" />'
  25009. : '<embed type="application/x-shockwave-flash" class="edui-faked-music" pluginspage="http://www.macromedia.com/go/getflashplayer"' +
  25010. ' src="' +
  25011. url +
  25012. '" width="' +
  25013. width +
  25014. '" height="' +
  25015. height +
  25016. '" ' +
  25017. (align && !cssfloat ? 'align="' + align + '"' : "") +
  25018. (cssfloat ? 'style="float:' + cssfloat + '"' : "") +
  25019. ' wmode="transparent" play="true" loop="false" menu="false" allowscriptaccess="never" allowfullscreen="true" >';
  25020. }
  25021. return {
  25022. outputRule: function(root) {
  25023. utils.each(root.getNodesByTagName("img"), function(node) {
  25024. var html;
  25025. if (node.getAttr("class") == "edui-faked-music") {
  25026. var cssfloat = node.getStyle("float");
  25027. var align = node.getAttr("align");
  25028. html = creatInsertStr(
  25029. node.getAttr("_url"),
  25030. node.getAttr("width"),
  25031. node.getAttr("height"),
  25032. align,
  25033. cssfloat,
  25034. true
  25035. );
  25036. var embed = UE.uNode.createElement(html);
  25037. node.parentNode.replaceChild(embed, node);
  25038. }
  25039. });
  25040. },
  25041. inputRule: function(root) {
  25042. utils.each(root.getNodesByTagName("embed"), function(node) {
  25043. if (node.getAttr("class") == "edui-faked-music") {
  25044. var cssfloat = node.getStyle("float");
  25045. var align = node.getAttr("align");
  25046. html = creatInsertStr(
  25047. node.getAttr("src"),
  25048. node.getAttr("width"),
  25049. node.getAttr("height"),
  25050. align,
  25051. cssfloat,
  25052. false
  25053. );
  25054. var img = UE.uNode.createElement(html);
  25055. node.parentNode.replaceChild(img, node);
  25056. }
  25057. });
  25058. },
  25059. commands: {
  25060. /**
  25061. * 插入音乐
  25062. * @command music
  25063. * @method execCommand
  25064. * @param { Object } musicOptions 插入音乐的参数项, 支持的key有: url=>音乐地址;
  25065. * width=>音乐容器宽度;height=>音乐容器高度;align=>音乐文件的对齐方式, 可选值有: left, center, right, none
  25066. * @example
  25067. * ```javascript
  25068. * //editor是编辑器实例
  25069. * //在编辑器里插入一个“植物大战僵尸”的APP
  25070. * editor.execCommand( 'music' , {
  25071. * width: 400,
  25072. * height: 95,
  25073. * align: "center",
  25074. * url: "音乐地址"
  25075. * } );
  25076. * ```
  25077. */
  25078. music: {
  25079. execCommand: function(cmd, musicObj) {
  25080. var me = this,
  25081. str = creatInsertStr(
  25082. musicObj.url,
  25083. musicObj.width || 400,
  25084. musicObj.height || 95,
  25085. "none",
  25086. false
  25087. );
  25088. me.execCommand("inserthtml", str);
  25089. },
  25090. queryCommandState: function() {
  25091. var me = this,
  25092. img = me.selection.getRange().getClosedNode(),
  25093. flag = img && img.className == "edui-faked-music";
  25094. return flag ? 1 : 0;
  25095. }
  25096. }
  25097. }
  25098. };
  25099. });
  25100. // plugins/autoupload.js
  25101. /**
  25102. * @description
  25103. * 1.拖放文件到编辑区域,自动上传并插入到选区
  25104. * 2.插入粘贴板的图片,自动上传并插入到选区
  25105. * @author Jinqn
  25106. * @date 2013-10-14
  25107. */
  25108. UE.plugin.register("autoupload", function() {
  25109. function sendAndInsertFile(file, editor) {
  25110. var me = editor;
  25111. //模拟数据
  25112. var fieldName,
  25113. urlPrefix,
  25114. maxSize,
  25115. allowFiles,
  25116. actionUrl,
  25117. loadingHtml,
  25118. errorHandler,
  25119. successHandler,
  25120. filetype = /image\/\w+/i.test(file.type) ? "image" : "file",
  25121. loadingId = "loading_" + (+new Date()).toString(36);
  25122. fieldName = me.getOpt(filetype + "FieldName");
  25123. urlPrefix = me.getOpt(filetype + "UrlPrefix");
  25124. maxSize = me.getOpt(filetype + "MaxSize");
  25125. allowFiles = me.getOpt(filetype + "AllowFiles");
  25126. actionUrl = me.getActionUrl(me.getOpt(filetype + "ActionName"));
  25127. errorHandler = function(title) {
  25128. var loader = me.document.getElementById(loadingId);
  25129. loader && domUtils.remove(loader);
  25130. me.fireEvent("showmessage", {
  25131. id: loadingId,
  25132. content: title,
  25133. type: "error",
  25134. timeout: 4000
  25135. });
  25136. };
  25137. if (filetype == "image") {
  25138. loadingHtml =
  25139. '<img class="loadingclass" id="' +
  25140. loadingId +
  25141. '" src="' +
  25142. me.options.themePath +
  25143. me.options.theme +
  25144. '/images/spacer.gif">';
  25145. successHandler = function(data) {
  25146. var link = urlPrefix + data.url,
  25147. loader = me.document.getElementById(loadingId);
  25148. if (loader) {
  25149. domUtils.removeClasses(loader, "loadingclass");
  25150. loader.setAttribute("src", link);
  25151. loader.setAttribute("_src", link);
  25152. loader.setAttribute("alt", data.original || "");
  25153. loader.removeAttribute("id");
  25154. me.trigger("contentchange", loader);
  25155. }
  25156. };
  25157. } else {
  25158. loadingHtml =
  25159. "<p>" +
  25160. '<img class="loadingclass" id="' +
  25161. loadingId +
  25162. '" src="' +
  25163. me.options.themePath +
  25164. me.options.theme +
  25165. '/images/spacer.gif">' +
  25166. "</p>";
  25167. successHandler = function(data) {
  25168. var link = urlPrefix + data.url,
  25169. loader = me.document.getElementById(loadingId);
  25170. var rng = me.selection.getRange(),
  25171. bk = rng.createBookmark();
  25172. rng.selectNode(loader).select();
  25173. me.execCommand("insertfile", { url: link });
  25174. rng.moveToBookmark(bk).select();
  25175. };
  25176. }
  25177. /* 插入loading的占位符 */
  25178. me.execCommand("inserthtml", loadingHtml);
  25179. /* 判断后端配置是否没有加载成功 */
  25180. if (!me.getOpt(filetype + "ActionName")) {
  25181. errorHandler(me.getLang("autoupload.errorLoadConfig"));
  25182. return;
  25183. }
  25184. /* 判断文件大小是否超出限制 */
  25185. if (file.size > maxSize) {
  25186. errorHandler(me.getLang("autoupload.exceedSizeError"));
  25187. return;
  25188. }
  25189. /* 判断文件格式是否超出允许 */
  25190. var fileext = file.name ? file.name.substr(file.name.lastIndexOf(".")) : "";
  25191. if (
  25192. (fileext && filetype != "image") ||
  25193. (allowFiles &&
  25194. (allowFiles.join("") + ".").indexOf(fileext.toLowerCase() + ".") == -1)
  25195. ) {
  25196. errorHandler(me.getLang("autoupload.exceedTypeError"));
  25197. return;
  25198. }
  25199. /* 创建Ajax并提交 */
  25200. var xhr = new XMLHttpRequest(),
  25201. fd = new FormData(),
  25202. params = utils.serializeParam(me.queryCommandValue("serverparam")) || "",
  25203. url = utils.formatUrl(
  25204. actionUrl + (actionUrl.indexOf("?") == -1 ? "?" : "&") + params
  25205. );
  25206. fd.append(
  25207. fieldName,
  25208. file,
  25209. file.name || "blob." + file.type.substr("image/".length)
  25210. );
  25211. fd.append("type", "ajax");
  25212. xhr.open("post", url, true);
  25213. xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
  25214. xhr.addEventListener("load", function(e) {
  25215. try {
  25216. var json = new Function("return " + utils.trim(e.target.response))();
  25217. if (json.state == "SUCCESS" && json.url) {
  25218. successHandler(json);
  25219. } else {
  25220. errorHandler(json.state);
  25221. }
  25222. } catch (er) {
  25223. errorHandler(me.getLang("autoupload.loadError"));
  25224. }
  25225. });
  25226. xhr.send(fd);
  25227. }
  25228. function getPasteImage(e) {
  25229. return e.clipboardData &&
  25230. e.clipboardData.items &&
  25231. e.clipboardData.items.length == 1 &&
  25232. /^image\//.test(e.clipboardData.items[0].type)
  25233. ? e.clipboardData.items
  25234. : null;
  25235. }
  25236. function getDropImage(e) {
  25237. return e.dataTransfer && e.dataTransfer.files ? e.dataTransfer.files : null;
  25238. }
  25239. return {
  25240. outputRule: function(root) {
  25241. utils.each(root.getNodesByTagName("img"), function(n) {
  25242. if (/\b(loaderrorclass)|(bloaderrorclass)\b/.test(n.getAttr("class"))) {
  25243. n.parentNode.removeChild(n);
  25244. }
  25245. });
  25246. utils.each(root.getNodesByTagName("p"), function(n) {
  25247. if (/\bloadpara\b/.test(n.getAttr("class"))) {
  25248. n.parentNode.removeChild(n);
  25249. }
  25250. });
  25251. },
  25252. bindEvents: {
  25253. defaultOptions: {
  25254. //默认间隔时间
  25255. enableDragUpload: true,
  25256. enablePasteUpload: true
  25257. },
  25258. //插入粘贴板的图片,拖放插入图片
  25259. ready: function(e) {
  25260. var me = this;
  25261. if (window.FormData && window.FileReader) {
  25262. var handler = function(e) {
  25263. var hasImg = false,
  25264. items;
  25265. //获取粘贴板文件列表或者拖放文件列表
  25266. items = e.type == "paste" ? getPasteImage(e) : getDropImage(e);
  25267. if (items) {
  25268. var len = items.length,
  25269. file;
  25270. while (len--) {
  25271. file = items[len];
  25272. if (file.getAsFile) file = file.getAsFile();
  25273. if (file && file.size > 0) {
  25274. sendAndInsertFile(file, me);
  25275. hasImg = true;
  25276. }
  25277. }
  25278. hasImg && e.preventDefault();
  25279. }
  25280. };
  25281. if (me.getOpt("enablePasteUpload") !== false) {
  25282. domUtils.on(me.body, "paste ", handler);
  25283. }
  25284. if (me.getOpt("enableDragUpload") !== false) {
  25285. domUtils.on(me.body, "drop", handler);
  25286. //取消拖放图片时出现的文字光标位置提示
  25287. domUtils.on(me.body, "dragover", function(e) {
  25288. if (e.dataTransfer.types[0] == "Files") {
  25289. e.preventDefault();
  25290. }
  25291. });
  25292. } else {
  25293. if (browser.gecko) {
  25294. domUtils.on(me.body, "drop", function(e) {
  25295. if (getDropImage(e)) {
  25296. e.preventDefault();
  25297. }
  25298. });
  25299. }
  25300. }
  25301. //设置loading的样式
  25302. utils.cssRule(
  25303. "loading",
  25304. ".loadingclass{display:inline-block;cursor:default;background: url('" +
  25305. this.options.themePath +
  25306. this.options.theme +
  25307. "/images/loading.gif') no-repeat center center transparent;border:1px solid #cccccc;margin-left:1px;height: 22px;width: 22px;}\n" +
  25308. ".loaderrorclass{display:inline-block;cursor:default;background: url('" +
  25309. this.options.themePath +
  25310. this.options.theme +
  25311. "/images/loaderror.png') no-repeat center center transparent;border:1px solid #cccccc;margin-right:1px;height: 22px;width: 22px;" +
  25312. "}",
  25313. this.document
  25314. );
  25315. }
  25316. }
  25317. }
  25318. };
  25319. });
  25320. // plugins/autosave.js
  25321. UE.plugin.register("autosave", function() {
  25322. var me = this,
  25323. //无限循环保护
  25324. lastSaveTime = new Date(),
  25325. //最小保存间隔时间
  25326. MIN_TIME = 20,
  25327. //auto save key
  25328. saveKey = null;
  25329. function save(editor) {
  25330. var saveData;
  25331. if (new Date() - lastSaveTime < MIN_TIME) {
  25332. return;
  25333. }
  25334. if (!editor.hasContents()) {
  25335. //这里不能调用命令来删除, 会造成事件死循环
  25336. saveKey && me.removePreferences(saveKey);
  25337. return;
  25338. }
  25339. lastSaveTime = new Date();
  25340. editor._saveFlag = null;
  25341. saveData = me.body.innerHTML;
  25342. if (
  25343. editor.fireEvent("beforeautosave", {
  25344. content: saveData
  25345. }) === false
  25346. ) {
  25347. return;
  25348. }
  25349. me.setPreferences(saveKey, saveData);
  25350. editor.fireEvent("afterautosave", {
  25351. content: saveData
  25352. });
  25353. }
  25354. return {
  25355. defaultOptions: {
  25356. //默认间隔时间
  25357. saveInterval: 500,
  25358. enableAutoSave: true
  25359. },
  25360. bindEvents: {
  25361. ready: function() {
  25362. var _suffix = "-drafts-data",
  25363. key = null;
  25364. if (me.key) {
  25365. key = me.key + _suffix;
  25366. } else {
  25367. key = (me.container.parentNode.id || "ue-common") + _suffix;
  25368. }
  25369. //页面地址+编辑器ID 保持唯一
  25370. saveKey =
  25371. (location.protocol + location.host + location.pathname).replace(
  25372. /[.:\/]/g,
  25373. "_"
  25374. ) + key;
  25375. },
  25376. contentchange: function() {
  25377. if (!me.getOpt("enableAutoSave")) {
  25378. return;
  25379. }
  25380. if (!saveKey) {
  25381. return;
  25382. }
  25383. if (me._saveFlag) {
  25384. window.clearTimeout(me._saveFlag);
  25385. }
  25386. if (me.options.saveInterval > 0) {
  25387. me._saveFlag = window.setTimeout(function() {
  25388. save(me);
  25389. }, me.options.saveInterval);
  25390. } else {
  25391. save(me);
  25392. }
  25393. }
  25394. },
  25395. commands: {
  25396. clearlocaldata: {
  25397. execCommand: function(cmd, name) {
  25398. if (saveKey && me.getPreferences(saveKey)) {
  25399. me.removePreferences(saveKey);
  25400. }
  25401. },
  25402. notNeedUndo: true,
  25403. ignoreContentChange: true
  25404. },
  25405. getlocaldata: {
  25406. execCommand: function(cmd, name) {
  25407. return saveKey ? me.getPreferences(saveKey) || "" : "";
  25408. },
  25409. notNeedUndo: true,
  25410. ignoreContentChange: true
  25411. },
  25412. drafts: {
  25413. execCommand: function(cmd, name) {
  25414. if (saveKey) {
  25415. me.body.innerHTML =
  25416. me.getPreferences(saveKey) || "<p>" + domUtils.fillHtml + "</p>";
  25417. me.focus(true);
  25418. }
  25419. },
  25420. queryCommandState: function() {
  25421. return saveKey ? (me.getPreferences(saveKey) === null ? -1 : 0) : -1;
  25422. },
  25423. notNeedUndo: true,
  25424. ignoreContentChange: true
  25425. }
  25426. }
  25427. };
  25428. });
  25429. // plugins/charts.js
  25430. UE.plugin.register("charts", function() {
  25431. var me = this;
  25432. return {
  25433. bindEvents: {
  25434. chartserror: function() {}
  25435. },
  25436. commands: {
  25437. charts: {
  25438. execCommand: function(cmd, data) {
  25439. var tableNode = domUtils.findParentByTagName(
  25440. this.selection.getRange().startContainer,
  25441. "table",
  25442. true
  25443. ),
  25444. flagText = [],
  25445. config = {};
  25446. if (!tableNode) {
  25447. return false;
  25448. }
  25449. if (!validData(tableNode)) {
  25450. me.fireEvent("chartserror");
  25451. return false;
  25452. }
  25453. config.title = data.title || "";
  25454. config.subTitle = data.subTitle || "";
  25455. config.xTitle = data.xTitle || "";
  25456. config.yTitle = data.yTitle || "";
  25457. config.suffix = data.suffix || "";
  25458. config.tip = data.tip || "";
  25459. //数据对齐方式
  25460. config.dataFormat = data.tableDataFormat || "";
  25461. //图表类型
  25462. config.chartType = data.chartType || 0;
  25463. for (var key in config) {
  25464. if (!config.hasOwnProperty(key)) {
  25465. continue;
  25466. }
  25467. flagText.push(key + ":" + config[key]);
  25468. }
  25469. tableNode.setAttribute("data-chart", flagText.join(";"));
  25470. domUtils.addClass(tableNode, "edui-charts-table");
  25471. },
  25472. queryCommandState: function(cmd, name) {
  25473. var tableNode = domUtils.findParentByTagName(
  25474. this.selection.getRange().startContainer,
  25475. "table",
  25476. true
  25477. );
  25478. return tableNode && validData(tableNode) ? 0 : -1;
  25479. }
  25480. }
  25481. },
  25482. inputRule: function(root) {
  25483. utils.each(root.getNodesByTagName("table"), function(tableNode) {
  25484. if (tableNode.getAttr("data-chart") !== undefined) {
  25485. tableNode.setAttr("style");
  25486. }
  25487. });
  25488. },
  25489. outputRule: function(root) {
  25490. utils.each(root.getNodesByTagName("table"), function(tableNode) {
  25491. if (tableNode.getAttr("data-chart") !== undefined) {
  25492. tableNode.setAttr("style", "display: none;");
  25493. }
  25494. });
  25495. }
  25496. };
  25497. function validData(table) {
  25498. var firstRows = null,
  25499. cellCount = 0;
  25500. //行数不够
  25501. if (table.rows.length < 2) {
  25502. return false;
  25503. }
  25504. //列数不够
  25505. if (table.rows[0].cells.length < 2) {
  25506. return false;
  25507. }
  25508. //第一行所有cell必须是th
  25509. firstRows = table.rows[0].cells;
  25510. cellCount = firstRows.length;
  25511. for (var i = 0, cell; (cell = firstRows[i]); i++) {
  25512. if (cell.tagName.toLowerCase() !== "th") {
  25513. return false;
  25514. }
  25515. }
  25516. for (var i = 1, row; (row = table.rows[i]); i++) {
  25517. //每行单元格数不匹配, 返回false
  25518. if (row.cells.length != cellCount) {
  25519. return false;
  25520. }
  25521. //第一列不是th也返回false
  25522. if (row.cells[0].tagName.toLowerCase() !== "th") {
  25523. return false;
  25524. }
  25525. for (var j = 1, cell; (cell = row.cells[j]); j++) {
  25526. var value = utils.trim(cell.innerText || cell.textContent || "");
  25527. value = value
  25528. .replace(new RegExp(UE.dom.domUtils.fillChar, "g"), "")
  25529. .replace(/^\s+|\s+$/g, "");
  25530. //必须是数字
  25531. if (!/^\d*\.?\d+$/.test(value)) {
  25532. return false;
  25533. }
  25534. }
  25535. }
  25536. return true;
  25537. }
  25538. });
  25539. // plugins/section.js
  25540. /**
  25541. * 目录大纲支持插件
  25542. * @file
  25543. * @since 1.3.0
  25544. */
  25545. UE.plugin.register("section", function() {
  25546. /* 目录节点对象 */
  25547. function Section(option) {
  25548. this.tag = "";
  25549. (this.level = -1), (this.dom = null);
  25550. this.nextSection = null;
  25551. this.previousSection = null;
  25552. this.parentSection = null;
  25553. this.startAddress = [];
  25554. this.endAddress = [];
  25555. this.children = [];
  25556. }
  25557. function getSection(option) {
  25558. var section = new Section();
  25559. return utils.extend(section, option);
  25560. }
  25561. function getNodeFromAddress(startAddress, root) {
  25562. var current = root;
  25563. for (var i = 0; i < startAddress.length; i++) {
  25564. if (!current.childNodes) return null;
  25565. current = current.childNodes[startAddress[i]];
  25566. }
  25567. return current;
  25568. }
  25569. var me = this;
  25570. return {
  25571. bindMultiEvents: {
  25572. type: "aftersetcontent afterscencerestore",
  25573. handler: function() {
  25574. me.fireEvent("updateSections");
  25575. }
  25576. },
  25577. bindEvents: {
  25578. /* 初始化、拖拽、粘贴、执行setcontent之后 */
  25579. ready: function() {
  25580. me.fireEvent("updateSections");
  25581. domUtils.on(me.body, "drop paste", function() {
  25582. me.fireEvent("updateSections");
  25583. });
  25584. },
  25585. /* 执行paragraph命令之后 */
  25586. afterexeccommand: function(type, cmd) {
  25587. if (cmd == "paragraph") {
  25588. me.fireEvent("updateSections");
  25589. }
  25590. },
  25591. /* 部分键盘操作,触发updateSections事件 */
  25592. keyup: function(type, e) {
  25593. var me = this,
  25594. range = me.selection.getRange();
  25595. if (range.collapsed != true) {
  25596. me.fireEvent("updateSections");
  25597. } else {
  25598. var keyCode = e.keyCode || e.which;
  25599. if (keyCode == 13 || keyCode == 8 || keyCode == 46) {
  25600. me.fireEvent("updateSections");
  25601. }
  25602. }
  25603. }
  25604. },
  25605. commands: {
  25606. getsections: {
  25607. execCommand: function(cmd, levels) {
  25608. var levelFn = levels || ["h1", "h2", "h3", "h4", "h5", "h6"];
  25609. for (var i = 0; i < levelFn.length; i++) {
  25610. if (typeof levelFn[i] == "string") {
  25611. levelFn[i] = (function(fn) {
  25612. return function(node) {
  25613. return node.tagName == fn.toUpperCase();
  25614. };
  25615. })(levelFn[i]);
  25616. } else if (typeof levelFn[i] != "function") {
  25617. levelFn[i] = function(node) {
  25618. return null;
  25619. };
  25620. }
  25621. }
  25622. function getSectionLevel(node) {
  25623. for (var i = 0; i < levelFn.length; i++) {
  25624. if (levelFn[i](node)) return i;
  25625. }
  25626. return -1;
  25627. }
  25628. var me = this,
  25629. Directory = getSection({ level: -1, title: "root" }),
  25630. previous = Directory;
  25631. function traversal(node, Directory) {
  25632. var level,
  25633. tmpSection = null,
  25634. parent,
  25635. child,
  25636. children = node.childNodes;
  25637. for (var i = 0, len = children.length; i < len; i++) {
  25638. child = children[i];
  25639. level = getSectionLevel(child);
  25640. if (level >= 0) {
  25641. var address = me.selection
  25642. .getRange()
  25643. .selectNode(child)
  25644. .createAddress(true).startAddress,
  25645. current = getSection({
  25646. tag: child.tagName,
  25647. title: child.innerText || child.textContent || "",
  25648. level: level,
  25649. dom: child,
  25650. startAddress: utils.clone(address, []),
  25651. endAddress: utils.clone(address, []),
  25652. children: []
  25653. });
  25654. previous.nextSection = current;
  25655. current.previousSection = previous;
  25656. parent = previous;
  25657. while (level <= parent.level) {
  25658. parent = parent.parentSection;
  25659. }
  25660. current.parentSection = parent;
  25661. parent.children.push(current);
  25662. tmpSection = previous = current;
  25663. } else {
  25664. child.nodeType === 1 && traversal(child, Directory);
  25665. tmpSection &&
  25666. tmpSection.endAddress[tmpSection.endAddress.length - 1]++;
  25667. }
  25668. }
  25669. }
  25670. traversal(me.body, Directory);
  25671. return Directory;
  25672. },
  25673. notNeedUndo: true
  25674. },
  25675. movesection: {
  25676. execCommand: function(cmd, sourceSection, targetSection, isAfter) {
  25677. var me = this,
  25678. targetAddress,
  25679. target;
  25680. if (!sourceSection || !targetSection || targetSection.level == -1)
  25681. return;
  25682. targetAddress = isAfter
  25683. ? targetSection.endAddress
  25684. : targetSection.startAddress;
  25685. target = getNodeFromAddress(targetAddress, me.body);
  25686. /* 判断目标地址是否被源章节包含 */
  25687. if (
  25688. !targetAddress ||
  25689. !target ||
  25690. isContainsAddress(
  25691. sourceSection.startAddress,
  25692. sourceSection.endAddress,
  25693. targetAddress
  25694. )
  25695. )
  25696. return;
  25697. var startNode = getNodeFromAddress(
  25698. sourceSection.startAddress,
  25699. me.body
  25700. ),
  25701. endNode = getNodeFromAddress(sourceSection.endAddress, me.body),
  25702. current,
  25703. nextNode;
  25704. if (isAfter) {
  25705. current = endNode;
  25706. while (
  25707. current &&
  25708. !(
  25709. domUtils.getPosition(startNode, current) &
  25710. domUtils.POSITION_FOLLOWING
  25711. )
  25712. ) {
  25713. nextNode = current.previousSibling;
  25714. domUtils.insertAfter(target, current);
  25715. if (current == startNode) break;
  25716. current = nextNode;
  25717. }
  25718. } else {
  25719. current = startNode;
  25720. while (
  25721. current &&
  25722. !(
  25723. domUtils.getPosition(current, endNode) &
  25724. domUtils.POSITION_FOLLOWING
  25725. )
  25726. ) {
  25727. nextNode = current.nextSibling;
  25728. target.parentNode.insertBefore(current, target);
  25729. if (current == endNode) break;
  25730. current = nextNode;
  25731. }
  25732. }
  25733. me.fireEvent("updateSections");
  25734. /* 获取地址的包含关系 */
  25735. function isContainsAddress(startAddress, endAddress, addressTarget) {
  25736. var isAfterStartAddress = false,
  25737. isBeforeEndAddress = false;
  25738. for (var i = 0; i < startAddress.length; i++) {
  25739. if (i >= addressTarget.length) break;
  25740. if (addressTarget[i] > startAddress[i]) {
  25741. isAfterStartAddress = true;
  25742. break;
  25743. } else if (addressTarget[i] < startAddress[i]) {
  25744. break;
  25745. }
  25746. }
  25747. for (var i = 0; i < endAddress.length; i++) {
  25748. if (i >= addressTarget.length) break;
  25749. if (addressTarget[i] < startAddress[i]) {
  25750. isBeforeEndAddress = true;
  25751. break;
  25752. } else if (addressTarget[i] > startAddress[i]) {
  25753. break;
  25754. }
  25755. }
  25756. return isAfterStartAddress && isBeforeEndAddress;
  25757. }
  25758. }
  25759. },
  25760. deletesection: {
  25761. execCommand: function(cmd, section, keepChildren) {
  25762. var me = this;
  25763. if (!section) return;
  25764. function getNodeFromAddress(startAddress) {
  25765. var current = me.body;
  25766. for (var i = 0; i < startAddress.length; i++) {
  25767. if (!current.childNodes) return null;
  25768. current = current.childNodes[startAddress[i]];
  25769. }
  25770. return current;
  25771. }
  25772. var startNode = getNodeFromAddress(section.startAddress),
  25773. endNode = getNodeFromAddress(section.endAddress),
  25774. current = startNode,
  25775. nextNode;
  25776. if (!keepChildren) {
  25777. while (
  25778. current &&
  25779. domUtils.inDoc(endNode, me.document) &&
  25780. !(
  25781. domUtils.getPosition(current, endNode) &
  25782. domUtils.POSITION_FOLLOWING
  25783. )
  25784. ) {
  25785. nextNode = current.nextSibling;
  25786. domUtils.remove(current);
  25787. current = nextNode;
  25788. }
  25789. } else {
  25790. domUtils.remove(current);
  25791. }
  25792. me.fireEvent("updateSections");
  25793. }
  25794. },
  25795. selectsection: {
  25796. execCommand: function(cmd, section) {
  25797. if (!section && !section.dom) return false;
  25798. var me = this,
  25799. range = me.selection.getRange(),
  25800. address = {
  25801. startAddress: utils.clone(section.startAddress, []),
  25802. endAddress: utils.clone(section.endAddress, [])
  25803. };
  25804. address.endAddress[address.endAddress.length - 1]++;
  25805. range.moveToAddress(address).select().scrollToView();
  25806. return true;
  25807. },
  25808. notNeedUndo: true
  25809. },
  25810. scrolltosection: {
  25811. execCommand: function(cmd, section) {
  25812. if (!section && !section.dom) return false;
  25813. var me = this,
  25814. range = me.selection.getRange(),
  25815. address = {
  25816. startAddress: section.startAddress,
  25817. endAddress: section.endAddress
  25818. };
  25819. address.endAddress[address.endAddress.length - 1]++;
  25820. range.moveToAddress(address).scrollToView();
  25821. return true;
  25822. },
  25823. notNeedUndo: true
  25824. }
  25825. }
  25826. };
  25827. });
  25828. // plugins/simpleupload.js
  25829. /**
  25830. * @description
  25831. * 简单上传:点击按钮,直接选择文件上传
  25832. * @author Jinqn
  25833. * @date 2014-03-31
  25834. */
  25835. UE.plugin.register("simpleupload", function() {
  25836. var me = this,
  25837. isLoaded = false,
  25838. containerBtn;
  25839. function initUploadBtn() {
  25840. var w = containerBtn.offsetWidth || 20,
  25841. h = containerBtn.offsetHeight || 20,
  25842. btnIframe = document.createElement("iframe"),
  25843. btnStyle =
  25844. "display:block;width:" +
  25845. w +
  25846. "px;height:" +
  25847. h +
  25848. "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;";
  25849. domUtils.on(btnIframe, "load", function() {
  25850. var timestrap = (+new Date()).toString(36),
  25851. wrapper,
  25852. btnIframeDoc,
  25853. btnIframeBody;
  25854. btnIframeDoc =
  25855. btnIframe.contentDocument || btnIframe.contentWindow.document;
  25856. btnIframeBody = btnIframeDoc.body;
  25857. wrapper = btnIframeDoc.createElement("div");
  25858. wrapper.innerHTML =
  25859. '<form id="edui_form_' +
  25860. timestrap +
  25861. '" target="edui_iframe_' +
  25862. timestrap +
  25863. '" method="POST" enctype="multipart/form-data" action="' +
  25864. me.getOpt("serverUrl") +
  25865. '" ' +
  25866. 'style="' +
  25867. btnStyle +
  25868. '">' +
  25869. '<input id="edui_input_' +
  25870. timestrap +
  25871. '" type="file" accept="image/*" name="' +
  25872. me.options.imageFieldName +
  25873. '" ' +
  25874. 'style="' +
  25875. btnStyle +
  25876. '">' +
  25877. "</form>" +
  25878. '<iframe id="edui_iframe_' +
  25879. timestrap +
  25880. '" name="edui_iframe_' +
  25881. timestrap +
  25882. '" style="display:none;width:0;height:0;border:0;margin:0;padding:0;position:absolute;"></iframe>';
  25883. wrapper.className = "edui-" + me.options.theme;
  25884. wrapper.id = me.ui.id + "_iframeupload";
  25885. btnIframeBody.style.cssText = btnStyle;
  25886. btnIframeBody.style.width = w + "px";
  25887. btnIframeBody.style.height = h + "px";
  25888. btnIframeBody.appendChild(wrapper);
  25889. if (btnIframeBody.parentNode) {
  25890. btnIframeBody.parentNode.style.width = w + "px";
  25891. btnIframeBody.parentNode.style.height = w + "px";
  25892. }
  25893. var form = btnIframeDoc.getElementById("edui_form_" + timestrap);
  25894. var input = btnIframeDoc.getElementById("edui_input_" + timestrap);
  25895. var iframe = btnIframeDoc.getElementById("edui_iframe_" + timestrap);
  25896. domUtils.on(input, "change", function() {
  25897. if (!input.value) return;
  25898. var loadingId = "loading_" + (+new Date()).toString(36);
  25899. var params =
  25900. utils.serializeParam(me.queryCommandValue("serverparam")) || "";
  25901. var imageActionUrl = me.getActionUrl(me.getOpt("imageActionName"));
  25902. var allowFiles = me.getOpt("imageAllowFiles");
  25903. me.focus();
  25904. me.execCommand(
  25905. "inserthtml",
  25906. '<img class="loadingclass" id="' +
  25907. loadingId +
  25908. '" src="' +
  25909. me.options.themePath +
  25910. me.options.theme +
  25911. '/images/spacer.gif">'
  25912. );
  25913. function callback() {
  25914. try {
  25915. var link,
  25916. json,
  25917. loader,
  25918. body = (iframe.contentDocument || iframe.contentWindow.document)
  25919. .body,
  25920. result = body.innerText || body.textContent || "";
  25921. json = new Function("return " + result)();
  25922. link = me.options.imageUrlPrefix + json.url;
  25923. if (json.state == "SUCCESS" && json.url) {
  25924. loader = me.document.getElementById(loadingId);
  25925. domUtils.removeClasses(loader, "loadingclass");
  25926. loader.setAttribute("src", link);
  25927. loader.setAttribute("_src", link);
  25928. loader.setAttribute("alt", json.original || "");
  25929. loader.removeAttribute("id");
  25930. me.fireEvent("contentchange");
  25931. } else {
  25932. showErrorLoader && showErrorLoader(json.state);
  25933. }
  25934. } catch (er) {
  25935. showErrorLoader &&
  25936. showErrorLoader(me.getLang("simpleupload.loadError"));
  25937. }
  25938. form.reset();
  25939. domUtils.un(iframe, "load", callback);
  25940. }
  25941. function showErrorLoader(title) {
  25942. if (loadingId) {
  25943. var loader = me.document.getElementById(loadingId);
  25944. loader && domUtils.remove(loader);
  25945. me.fireEvent("showmessage", {
  25946. id: loadingId,
  25947. content: title,
  25948. type: "error",
  25949. timeout: 4000
  25950. });
  25951. }
  25952. }
  25953. /* 判断后端配置是否没有加载成功 */
  25954. if (!me.getOpt("imageActionName")) {
  25955. errorHandler(me.getLang("autoupload.errorLoadConfig"));
  25956. return;
  25957. }
  25958. // 判断文件格式是否错误
  25959. var filename = input.value,
  25960. fileext = filename ? filename.substr(filename.lastIndexOf(".")) : "";
  25961. if (
  25962. !fileext ||
  25963. (allowFiles &&
  25964. (allowFiles.join("") + ".").indexOf(fileext.toLowerCase() + ".") ==
  25965. -1)
  25966. ) {
  25967. showErrorLoader(me.getLang("simpleupload.exceedTypeError"));
  25968. return;
  25969. }
  25970. domUtils.on(iframe, "load", callback);
  25971. form.action = utils.formatUrl(
  25972. imageActionUrl +
  25973. (imageActionUrl.indexOf("?") == -1 ? "?" : "&") +
  25974. params
  25975. );
  25976. form.submit();
  25977. });
  25978. var stateTimer;
  25979. me.addListener("selectionchange", function() {
  25980. clearTimeout(stateTimer);
  25981. stateTimer = setTimeout(function() {
  25982. var state = me.queryCommandState("simpleupload");
  25983. if (state == -1) {
  25984. input.disabled = "disabled";
  25985. } else {
  25986. input.disabled = false;
  25987. }
  25988. }, 400);
  25989. });
  25990. isLoaded = true;
  25991. });
  25992. btnIframe.style.cssText = btnStyle;
  25993. containerBtn.appendChild(btnIframe);
  25994. }
  25995. return {
  25996. bindEvents: {
  25997. ready: function() {
  25998. //设置loading的样式
  25999. utils.cssRule(
  26000. "loading",
  26001. ".loadingclass{display:inline-block;cursor:default;background: url('" +
  26002. this.options.themePath +
  26003. this.options.theme +
  26004. "/images/loading.gif') no-repeat center center transparent;border:1px solid #cccccc;margin-right:1px;height: 22px;width: 22px;}\n" +
  26005. ".loaderrorclass{display:inline-block;cursor:default;background: url('" +
  26006. this.options.themePath +
  26007. this.options.theme +
  26008. "/images/loaderror.png') no-repeat center center transparent;border:1px solid #cccccc;margin-right:1px;height: 22px;width: 22px;" +
  26009. "}",
  26010. this.document
  26011. );
  26012. },
  26013. /* 初始化简单上传按钮 */
  26014. simpleuploadbtnready: function(type, container) {
  26015. containerBtn = container;
  26016. me.afterConfigReady(initUploadBtn);
  26017. }
  26018. },
  26019. outputRule: function(root) {
  26020. utils.each(root.getNodesByTagName("img"), function(n) {
  26021. if (/\b(loaderrorclass)|(bloaderrorclass)\b/.test(n.getAttr("class"))) {
  26022. n.parentNode.removeChild(n);
  26023. }
  26024. });
  26025. },
  26026. commands: {
  26027. simpleupload: {
  26028. queryCommandState: function() {
  26029. return isLoaded ? 0 : -1;
  26030. }
  26031. }
  26032. }
  26033. };
  26034. });
  26035. // plugins/serverparam.js
  26036. /**
  26037. * 服务器提交的额外参数列表设置插件
  26038. * @file
  26039. * @since 1.2.6.1
  26040. */
  26041. UE.plugin.register("serverparam", function() {
  26042. var me = this,
  26043. serverParam = {};
  26044. return {
  26045. commands: {
  26046. /**
  26047. * 修改服务器提交的额外参数列表,清除所有项
  26048. * @command serverparam
  26049. * @method execCommand
  26050. * @param { String } cmd 命令字符串
  26051. * @example
  26052. * ```javascript
  26053. * editor.execCommand('serverparam');
  26054. * editor.queryCommandValue('serverparam'); //返回空
  26055. * ```
  26056. */
  26057. /**
  26058. * 修改服务器提交的额外参数列表,删除指定项
  26059. * @command serverparam
  26060. * @method execCommand
  26061. * @param { String } cmd 命令字符串
  26062. * @param { String } key 要清除的属性
  26063. * @example
  26064. * ```javascript
  26065. * editor.execCommand('serverparam', 'name'); //删除属性name
  26066. * ```
  26067. */
  26068. /**
  26069. * 修改服务器提交的额外参数列表,使用键值添加项
  26070. * @command serverparam
  26071. * @method execCommand
  26072. * @param { String } cmd 命令字符串
  26073. * @param { String } key 要添加的属性
  26074. * @param { String } value 要添加属性的值
  26075. * @example
  26076. * ```javascript
  26077. * editor.execCommand('serverparam', 'name', 'hello');
  26078. * editor.queryCommandValue('serverparam'); //返回对象 {'name': 'hello'}
  26079. * ```
  26080. */
  26081. /**
  26082. * 修改服务器提交的额外参数列表,传入键值对对象添加多项
  26083. * @command serverparam
  26084. * @method execCommand
  26085. * @param { String } cmd 命令字符串
  26086. * @param { Object } key 传入的键值对对象
  26087. * @example
  26088. * ```javascript
  26089. * editor.execCommand('serverparam', {'name': 'hello'});
  26090. * editor.queryCommandValue('serverparam'); //返回对象 {'name': 'hello'}
  26091. * ```
  26092. */
  26093. /**
  26094. * 修改服务器提交的额外参数列表,使用自定义函数添加多项
  26095. * @command serverparam
  26096. * @method execCommand
  26097. * @param { String } cmd 命令字符串
  26098. * @param { Function } key 自定义获取参数的函数
  26099. * @example
  26100. * ```javascript
  26101. * editor.execCommand('serverparam', function(editor){
  26102. * return {'key': 'value'};
  26103. * });
  26104. * editor.queryCommandValue('serverparam'); //返回对象 {'key': 'value'}
  26105. * ```
  26106. */
  26107. /**
  26108. * 获取服务器提交的额外参数列表
  26109. * @command serverparam
  26110. * @method queryCommandValue
  26111. * @param { String } cmd 命令字符串
  26112. * @example
  26113. * ```javascript
  26114. * editor.queryCommandValue( 'serverparam' ); //返回对象 {'key': 'value'}
  26115. * ```
  26116. */
  26117. serverparam: {
  26118. execCommand: function(cmd, key, value) {
  26119. if (key === undefined || key === null) {
  26120. //不传参数,清空列表
  26121. serverParam = {};
  26122. } else if (utils.isString(key)) {
  26123. //传入键值
  26124. if (value === undefined || value === null) {
  26125. delete serverParam[key];
  26126. } else {
  26127. serverParam[key] = value;
  26128. }
  26129. } else if (utils.isObject(key)) {
  26130. //传入对象,覆盖列表项
  26131. utils.extend(serverParam, key, false);
  26132. } else if (utils.isFunction(key)) {
  26133. //传入函数,添加列表项
  26134. utils.extend(serverParam, key(), false);
  26135. }
  26136. },
  26137. queryCommandValue: function() {
  26138. return serverParam || {};
  26139. }
  26140. }
  26141. }
  26142. };
  26143. });
  26144. // plugins/insertfile.js
  26145. /**
  26146. * 插入附件
  26147. */
  26148. UE.plugin.register("insertfile", function() {
  26149. var me = this;
  26150. function getFileIcon(url) {
  26151. var ext = url.substr(url.lastIndexOf(".") + 1).toLowerCase(),
  26152. maps = {
  26153. rar: "icon_rar.gif",
  26154. zip: "icon_rar.gif",
  26155. tar: "icon_rar.gif",
  26156. gz: "icon_rar.gif",
  26157. bz2: "icon_rar.gif",
  26158. doc: "icon_doc.gif",
  26159. docx: "icon_doc.gif",
  26160. pdf: "icon_pdf.gif",
  26161. mp3: "icon_mp3.gif",
  26162. xls: "icon_xls.gif",
  26163. chm: "icon_chm.gif",
  26164. ppt: "icon_ppt.gif",
  26165. pptx: "icon_ppt.gif",
  26166. avi: "icon_mv.gif",
  26167. rmvb: "icon_mv.gif",
  26168. wmv: "icon_mv.gif",
  26169. flv: "icon_mv.gif",
  26170. swf: "icon_mv.gif",
  26171. rm: "icon_mv.gif",
  26172. exe: "icon_exe.gif",
  26173. psd: "icon_psd.gif",
  26174. txt: "icon_txt.gif",
  26175. jpg: "icon_jpg.gif",
  26176. png: "icon_jpg.gif",
  26177. jpeg: "icon_jpg.gif",
  26178. gif: "icon_jpg.gif",
  26179. ico: "icon_jpg.gif",
  26180. bmp: "icon_jpg.gif"
  26181. };
  26182. return maps[ext] ? maps[ext] : maps["txt"];
  26183. }
  26184. return {
  26185. commands: {
  26186. insertfile: {
  26187. execCommand: function(command, filelist) {
  26188. filelist = utils.isArray(filelist) ? filelist : [filelist];
  26189. if (me.fireEvent("beforeinsertfile", filelist) === true) {
  26190. return;
  26191. }
  26192. var i,
  26193. item,
  26194. icon,
  26195. title,
  26196. html = "",
  26197. URL = me.getOpt("UEDITOR_HOME_URL"),
  26198. iconDir =
  26199. URL +
  26200. (URL.substr(URL.length - 1) == "/" ? "" : "/") +
  26201. "dialogs/attachment/fileTypeImages/";
  26202. for (i = 0; i < filelist.length; i++) {
  26203. item = filelist[i];
  26204. icon = iconDir + getFileIcon(item.url);
  26205. title =
  26206. item.title || item.url.substr(item.url.lastIndexOf("/") + 1);
  26207. html +=
  26208. '<p style="line-height: 16px;">' +
  26209. '<img style="vertical-align: middle; margin-right: 2px;" src="' +
  26210. icon +
  26211. '" _src="' +
  26212. icon +
  26213. '" />' +
  26214. '<a style="font-size:12px; color:#0066cc;" href="' +
  26215. item.url +
  26216. '" title="' +
  26217. title +
  26218. '">' +
  26219. title +
  26220. "</a>" +
  26221. "</p>";
  26222. }
  26223. me.execCommand("insertHtml", html);
  26224. me.fireEvent("afterinsertfile", filelist);
  26225. }
  26226. }
  26227. }
  26228. };
  26229. });
  26230. // ui/ui.js
  26231. var baidu = baidu || {};
  26232. baidu.editor = baidu.editor || {};
  26233. UE.ui = baidu.editor.ui = {};
  26234. // ui/uiutils.js
  26235. (function() {
  26236. var browser = baidu.editor.browser,
  26237. domUtils = baidu.editor.dom.domUtils;
  26238. var magic = "$EDITORUI";
  26239. var root = (window[magic] = {});
  26240. var uidMagic = "ID" + magic;
  26241. var uidCount = 0;
  26242. var uiUtils = (baidu.editor.ui.uiUtils = {
  26243. uid: function(obj) {
  26244. return obj ? obj[uidMagic] || (obj[uidMagic] = ++uidCount) : ++uidCount;
  26245. },
  26246. hook: function(fn, callback) {
  26247. var dg;
  26248. if (fn && fn._callbacks) {
  26249. dg = fn;
  26250. } else {
  26251. dg = function() {
  26252. var q;
  26253. if (fn) {
  26254. q = fn.apply(this, arguments);
  26255. }
  26256. var callbacks = dg._callbacks;
  26257. var k = callbacks.length;
  26258. while (k--) {
  26259. var r = callbacks[k].apply(this, arguments);
  26260. if (q === undefined) {
  26261. q = r;
  26262. }
  26263. }
  26264. return q;
  26265. };
  26266. dg._callbacks = [];
  26267. }
  26268. dg._callbacks.push(callback);
  26269. return dg;
  26270. },
  26271. createElementByHtml: function(html) {
  26272. var el = document.createElement("div");
  26273. el.innerHTML = html;
  26274. el = el.firstChild;
  26275. el.parentNode.removeChild(el);
  26276. return el;
  26277. },
  26278. getViewportElement: function() {
  26279. return browser.ie && browser.quirks
  26280. ? document.body
  26281. : document.documentElement;
  26282. },
  26283. getClientRect: function(element) {
  26284. var bcr;
  26285. //trace IE6下在控制编辑器显隐时可能会报错,catch一下
  26286. try {
  26287. bcr = element.getBoundingClientRect();
  26288. } catch (e) {
  26289. bcr = { left: 0, top: 0, height: 0, width: 0 };
  26290. }
  26291. var rect = {
  26292. left: Math.round(bcr.left),
  26293. top: Math.round(bcr.top),
  26294. height: Math.round(bcr.bottom - bcr.top),
  26295. width: Math.round(bcr.right - bcr.left)
  26296. };
  26297. var doc;
  26298. while (
  26299. (doc = element.ownerDocument) !== document &&
  26300. (element = domUtils.getWindow(doc).frameElement)
  26301. ) {
  26302. bcr = element.getBoundingClientRect();
  26303. rect.left += bcr.left;
  26304. rect.top += bcr.top;
  26305. }
  26306. rect.bottom = rect.top + rect.height;
  26307. rect.right = rect.left + rect.width;
  26308. return rect;
  26309. },
  26310. getViewportRect: function() {
  26311. var viewportEl = uiUtils.getViewportElement();
  26312. var width = (window.innerWidth || viewportEl.clientWidth) | 0;
  26313. var height = (window.innerHeight || viewportEl.clientHeight) | 0;
  26314. return {
  26315. left: 0,
  26316. top: 0,
  26317. height: height,
  26318. width: width,
  26319. bottom: height,
  26320. right: width
  26321. };
  26322. },
  26323. setViewportOffset: function(element, offset) {
  26324. var rect;
  26325. var fixedLayer = uiUtils.getFixedLayer();
  26326. if (element.parentNode === fixedLayer) {
  26327. element.style.left = offset.left + "px";
  26328. element.style.top = offset.top + "px";
  26329. } else {
  26330. domUtils.setViewportOffset(element, offset);
  26331. }
  26332. },
  26333. getEventOffset: function(evt) {
  26334. var el = evt.target || evt.srcElement;
  26335. var rect = uiUtils.getClientRect(el);
  26336. var offset = uiUtils.getViewportOffsetByEvent(evt);
  26337. return {
  26338. left: offset.left - rect.left,
  26339. top: offset.top - rect.top
  26340. };
  26341. },
  26342. getViewportOffsetByEvent: function(evt) {
  26343. var el = evt.target || evt.srcElement;
  26344. var frameEl = domUtils.getWindow(el).frameElement;
  26345. var offset = {
  26346. left: evt.clientX,
  26347. top: evt.clientY
  26348. };
  26349. if (frameEl && el.ownerDocument !== document) {
  26350. var rect = uiUtils.getClientRect(frameEl);
  26351. offset.left += rect.left;
  26352. offset.top += rect.top;
  26353. }
  26354. return offset;
  26355. },
  26356. setGlobal: function(id, obj) {
  26357. root[id] = obj;
  26358. return magic + '["' + id + '"]';
  26359. },
  26360. unsetGlobal: function(id) {
  26361. delete root[id];
  26362. },
  26363. copyAttributes: function(tgt, src) {
  26364. var attributes = src.attributes;
  26365. var k = attributes.length;
  26366. while (k--) {
  26367. var attrNode = attributes[k];
  26368. if (
  26369. attrNode.nodeName != "style" &&
  26370. attrNode.nodeName != "class" &&
  26371. (!browser.ie || attrNode.specified)
  26372. ) {
  26373. tgt.setAttribute(attrNode.nodeName, attrNode.nodeValue);
  26374. }
  26375. }
  26376. if (src.className) {
  26377. domUtils.addClass(tgt, src.className);
  26378. }
  26379. if (src.style.cssText) {
  26380. tgt.style.cssText += ";" + src.style.cssText;
  26381. }
  26382. },
  26383. removeStyle: function(el, styleName) {
  26384. if (el.style.removeProperty) {
  26385. el.style.removeProperty(styleName);
  26386. } else if (el.style.removeAttribute) {
  26387. el.style.removeAttribute(styleName);
  26388. } else throw "";
  26389. },
  26390. contains: function(elA, elB) {
  26391. return (
  26392. elA &&
  26393. elB &&
  26394. (elA === elB
  26395. ? false
  26396. : elA.contains
  26397. ? elA.contains(elB)
  26398. : elA.compareDocumentPosition(elB) & 16)
  26399. );
  26400. },
  26401. startDrag: function(evt, callbacks, doc) {
  26402. var doc = doc || document;
  26403. var startX = evt.clientX;
  26404. var startY = evt.clientY;
  26405. function handleMouseMove(evt) {
  26406. var x = evt.clientX - startX;
  26407. var y = evt.clientY - startY;
  26408. callbacks.ondragmove(x, y, evt);
  26409. if (evt.stopPropagation) {
  26410. evt.stopPropagation();
  26411. } else {
  26412. evt.cancelBubble = true;
  26413. }
  26414. }
  26415. if (doc.addEventListener) {
  26416. function handleMouseUp(evt) {
  26417. doc.removeEventListener("mousemove", handleMouseMove, true);
  26418. doc.removeEventListener("mouseup", handleMouseUp, true);
  26419. window.removeEventListener("mouseup", handleMouseUp, true);
  26420. callbacks.ondragstop();
  26421. }
  26422. doc.addEventListener("mousemove", handleMouseMove, true);
  26423. doc.addEventListener("mouseup", handleMouseUp, true);
  26424. window.addEventListener("mouseup", handleMouseUp, true);
  26425. evt.preventDefault();
  26426. } else {
  26427. var elm = evt.srcElement;
  26428. elm.setCapture();
  26429. function releaseCaptrue() {
  26430. elm.releaseCapture();
  26431. elm.detachEvent("onmousemove", handleMouseMove);
  26432. elm.detachEvent("onmouseup", releaseCaptrue);
  26433. elm.detachEvent("onlosecaptrue", releaseCaptrue);
  26434. callbacks.ondragstop();
  26435. }
  26436. elm.attachEvent("onmousemove", handleMouseMove);
  26437. elm.attachEvent("onmouseup", releaseCaptrue);
  26438. elm.attachEvent("onlosecaptrue", releaseCaptrue);
  26439. evt.returnValue = false;
  26440. }
  26441. callbacks.ondragstart();
  26442. },
  26443. getFixedLayer: function() {
  26444. var layer = document.getElementById("edui_fixedlayer");
  26445. if (layer == null) {
  26446. layer = document.createElement("div");
  26447. layer.id = "edui_fixedlayer";
  26448. document.body.appendChild(layer);
  26449. if (browser.ie && browser.version <= 8) {
  26450. layer.style.position = "absolute";
  26451. bindFixedLayer();
  26452. setTimeout(updateFixedOffset);
  26453. } else {
  26454. layer.style.position = "fixed";
  26455. }
  26456. layer.style.left = "0";
  26457. layer.style.top = "0";
  26458. layer.style.width = "0";
  26459. layer.style.height = "0";
  26460. }
  26461. return layer;
  26462. },
  26463. makeUnselectable: function(element) {
  26464. if (browser.opera || (browser.ie && browser.version < 9)) {
  26465. element.unselectable = "on";
  26466. if (element.hasChildNodes()) {
  26467. for (var i = 0; i < element.childNodes.length; i++) {
  26468. if (element.childNodes[i].nodeType == 1) {
  26469. uiUtils.makeUnselectable(element.childNodes[i]);
  26470. }
  26471. }
  26472. }
  26473. } else {
  26474. if (element.style.MozUserSelect !== undefined) {
  26475. element.style.MozUserSelect = "none";
  26476. } else if (element.style.WebkitUserSelect !== undefined) {
  26477. element.style.WebkitUserSelect = "none";
  26478. } else if (element.style.KhtmlUserSelect !== undefined) {
  26479. element.style.KhtmlUserSelect = "none";
  26480. }
  26481. }
  26482. }
  26483. });
  26484. function updateFixedOffset() {
  26485. var layer = document.getElementById("edui_fixedlayer");
  26486. uiUtils.setViewportOffset(layer, {
  26487. left: 0,
  26488. top: 0
  26489. });
  26490. // layer.style.display = 'none';
  26491. // layer.style.display = 'block';
  26492. //#trace: 1354
  26493. // setTimeout(updateFixedOffset);
  26494. }
  26495. function bindFixedLayer(adjOffset) {
  26496. domUtils.on(window, "scroll", updateFixedOffset);
  26497. domUtils.on(
  26498. window,
  26499. "resize",
  26500. baidu.editor.utils.defer(updateFixedOffset, 0, true)
  26501. );
  26502. }
  26503. })();
  26504. // ui/uibase.js
  26505. (function() {
  26506. var utils = baidu.editor.utils,
  26507. uiUtils = baidu.editor.ui.uiUtils,
  26508. EventBase = baidu.editor.EventBase,
  26509. UIBase = (baidu.editor.ui.UIBase = function() {});
  26510. UIBase.prototype = {
  26511. className: "",
  26512. uiName: "",
  26513. initOptions: function(options) {
  26514. var me = this;
  26515. for (var k in options) {
  26516. me[k] = options[k];
  26517. }
  26518. this.id = this.id || "edui" + uiUtils.uid();
  26519. },
  26520. initUIBase: function() {
  26521. this._globalKey = utils.unhtml(uiUtils.setGlobal(this.id, this));
  26522. },
  26523. render: function(holder) {
  26524. var html = this.renderHtml();
  26525. var el = uiUtils.createElementByHtml(html);
  26526. //by xuheng 给每个node添加class
  26527. var list = domUtils.getElementsByTagName(el, "*");
  26528. var theme = "edui-" + (this.theme || this.editor.options.theme);
  26529. var layer = document.getElementById("edui_fixedlayer");
  26530. for (var i = 0, node; (node = list[i++]); ) {
  26531. domUtils.addClass(node, theme);
  26532. }
  26533. domUtils.addClass(el, theme);
  26534. if (layer) {
  26535. layer.className = "";
  26536. domUtils.addClass(layer, theme);
  26537. }
  26538. var seatEl = this.getDom();
  26539. if (seatEl != null) {
  26540. seatEl.parentNode.replaceChild(el, seatEl);
  26541. uiUtils.copyAttributes(el, seatEl);
  26542. } else {
  26543. if (typeof holder == "string") {
  26544. holder = document.getElementById(holder);
  26545. }
  26546. holder = holder || uiUtils.getFixedLayer();
  26547. domUtils.addClass(holder, theme);
  26548. holder.appendChild(el);
  26549. }
  26550. this.postRender();
  26551. },
  26552. getDom: function(name) {
  26553. if (!name) {
  26554. return document.getElementById(this.id);
  26555. } else {
  26556. return document.getElementById(this.id + "_" + name);
  26557. }
  26558. },
  26559. postRender: function() {
  26560. this.fireEvent("postrender");
  26561. },
  26562. getHtmlTpl: function() {
  26563. return "";
  26564. },
  26565. formatHtml: function(tpl) {
  26566. var prefix = "edui-" + this.uiName;
  26567. return tpl
  26568. .replace(/##/g, this.id)
  26569. .replace(/%%-/g, this.uiName ? prefix + "-" : "")
  26570. .replace(/%%/g, (this.uiName ? prefix : "") + " " + this.className)
  26571. .replace(/\$\$/g, this._globalKey);
  26572. },
  26573. renderHtml: function() {
  26574. return this.formatHtml(this.getHtmlTpl());
  26575. },
  26576. dispose: function() {
  26577. var box = this.getDom();
  26578. if (box) baidu.editor.dom.domUtils.remove(box);
  26579. uiUtils.unsetGlobal(this.id);
  26580. }
  26581. };
  26582. utils.inherits(UIBase, EventBase);
  26583. })();
  26584. // ui/separator.js
  26585. (function() {
  26586. var utils = baidu.editor.utils,
  26587. UIBase = baidu.editor.ui.UIBase,
  26588. Separator = (baidu.editor.ui.Separator = function(options) {
  26589. this.initOptions(options);
  26590. this.initSeparator();
  26591. });
  26592. Separator.prototype = {
  26593. uiName: "separator",
  26594. initSeparator: function() {
  26595. this.initUIBase();
  26596. },
  26597. getHtmlTpl: function() {
  26598. return '<div id="##" class="edui-box %%"></div>';
  26599. }
  26600. };
  26601. utils.inherits(Separator, UIBase);
  26602. })();
  26603. // ui/mask.js
  26604. ///import core
  26605. ///import uicore
  26606. (function() {
  26607. var utils = baidu.editor.utils,
  26608. domUtils = baidu.editor.dom.domUtils,
  26609. UIBase = baidu.editor.ui.UIBase,
  26610. uiUtils = baidu.editor.ui.uiUtils;
  26611. var Mask = (baidu.editor.ui.Mask = function(options) {
  26612. this.initOptions(options);
  26613. this.initUIBase();
  26614. });
  26615. Mask.prototype = {
  26616. getHtmlTpl: function() {
  26617. return '<div id="##" class="edui-mask %%" onclick="return $$._onClick(event, this);" onmousedown="return $$._onMouseDown(event, this);"></div>';
  26618. },
  26619. postRender: function() {
  26620. var me = this;
  26621. domUtils.on(window, "resize", function() {
  26622. setTimeout(function() {
  26623. if (!me.isHidden()) {
  26624. me._fill();
  26625. }
  26626. });
  26627. });
  26628. },
  26629. show: function(zIndex) {
  26630. this._fill();
  26631. this.getDom().style.display = "";
  26632. this.getDom().style.zIndex = zIndex;
  26633. },
  26634. hide: function() {
  26635. this.getDom().style.display = "none";
  26636. this.getDom().style.zIndex = "";
  26637. },
  26638. isHidden: function() {
  26639. return this.getDom().style.display == "none";
  26640. },
  26641. _onMouseDown: function() {
  26642. return false;
  26643. },
  26644. _onClick: function(e, target) {
  26645. this.fireEvent("click", e, target);
  26646. },
  26647. _fill: function() {
  26648. var el = this.getDom();
  26649. var vpRect = uiUtils.getViewportRect();
  26650. el.style.width = vpRect.width + "px";
  26651. el.style.height = vpRect.height + "px";
  26652. }
  26653. };
  26654. utils.inherits(Mask, UIBase);
  26655. })();
  26656. // ui/popup.js
  26657. ///import core
  26658. ///import uicore
  26659. (function() {
  26660. var utils = baidu.editor.utils,
  26661. uiUtils = baidu.editor.ui.uiUtils,
  26662. domUtils = baidu.editor.dom.domUtils,
  26663. UIBase = baidu.editor.ui.UIBase,
  26664. Popup = (baidu.editor.ui.Popup = function(options) {
  26665. this.initOptions(options);
  26666. this.initPopup();
  26667. });
  26668. var allPopups = [];
  26669. function closeAllPopup(evt, el) {
  26670. for (var i = 0; i < allPopups.length; i++) {
  26671. var pop = allPopups[i];
  26672. if (!pop.isHidden()) {
  26673. if (pop.queryAutoHide(el) !== false) {
  26674. if (
  26675. evt &&
  26676. /scroll/gi.test(evt.type) &&
  26677. pop.className == "edui-wordpastepop"
  26678. )
  26679. return;
  26680. pop.hide();
  26681. }
  26682. }
  26683. }
  26684. if (allPopups.length) pop.editor.fireEvent("afterhidepop");
  26685. }
  26686. Popup.postHide = closeAllPopup;
  26687. var ANCHOR_CLASSES = [
  26688. "edui-anchor-topleft",
  26689. "edui-anchor-topright",
  26690. "edui-anchor-bottomleft",
  26691. "edui-anchor-bottomright"
  26692. ];
  26693. Popup.prototype = {
  26694. SHADOW_RADIUS: 5,
  26695. content: null,
  26696. _hidden: false,
  26697. autoRender: true,
  26698. canSideLeft: true,
  26699. canSideUp: true,
  26700. initPopup: function() {
  26701. this.initUIBase();
  26702. allPopups.push(this);
  26703. },
  26704. getHtmlTpl: function() {
  26705. return (
  26706. '<div id="##" class="edui-popup %%" onmousedown="return false;">' +
  26707. ' <div id="##_body" class="edui-popup-body">' +
  26708. ' <iframe style="position:absolute;z-index:-1;left:0;top:0;background-color: transparent;" frameborder="0" width="100%" height="100%" src="about:blank"></iframe>' +
  26709. ' <div class="edui-shadow"></div>' +
  26710. ' <div id="##_content" class="edui-popup-content">' +
  26711. this.getContentHtmlTpl() +
  26712. " </div>" +
  26713. " </div>" +
  26714. "</div>"
  26715. );
  26716. },
  26717. getContentHtmlTpl: function() {
  26718. if (this.content) {
  26719. if (typeof this.content == "string") {
  26720. return this.content;
  26721. }
  26722. return this.content.renderHtml();
  26723. } else {
  26724. return "";
  26725. }
  26726. },
  26727. _UIBase_postRender: UIBase.prototype.postRender,
  26728. postRender: function() {
  26729. if (this.content instanceof UIBase) {
  26730. this.content.postRender();
  26731. }
  26732. //捕获鼠标滚轮
  26733. if (this.captureWheel && !this.captured) {
  26734. this.captured = true;
  26735. var winHeight =
  26736. (document.documentElement.clientHeight ||
  26737. document.body.clientHeight) - 80,
  26738. _height = this.getDom().offsetHeight,
  26739. _top = uiUtils.getClientRect(this.combox.getDom()).top,
  26740. content = this.getDom("content"),
  26741. ifr = this.getDom("body").getElementsByTagName("iframe"),
  26742. me = this;
  26743. ifr.length && (ifr = ifr[0]);
  26744. while (_top + _height > winHeight) {
  26745. _height -= 30;
  26746. }
  26747. content.style.height = _height + "px";
  26748. //同步更改iframe高度
  26749. ifr && (ifr.style.height = _height + "px");
  26750. //阻止在combox上的鼠标滚轮事件, 防止用户的正常操作被误解
  26751. if (window.XMLHttpRequest) {
  26752. domUtils.on(
  26753. content,
  26754. "onmousewheel" in document.body ? "mousewheel" : "DOMMouseScroll",
  26755. function(e) {
  26756. if (e.preventDefault) {
  26757. e.preventDefault();
  26758. } else {
  26759. e.returnValue = false;
  26760. }
  26761. if (e.wheelDelta) {
  26762. content.scrollTop -= e.wheelDelta / 120 * 60;
  26763. } else {
  26764. content.scrollTop -= e.detail / -3 * 60;
  26765. }
  26766. }
  26767. );
  26768. } else {
  26769. //ie6
  26770. domUtils.on(this.getDom(), "mousewheel", function(e) {
  26771. e.returnValue = false;
  26772. me.getDom("content").scrollTop -= e.wheelDelta / 120 * 60;
  26773. });
  26774. }
  26775. }
  26776. this.fireEvent("postRenderAfter");
  26777. this.hide(true);
  26778. this._UIBase_postRender();
  26779. },
  26780. _doAutoRender: function() {
  26781. if (!this.getDom() && this.autoRender) {
  26782. this.render();
  26783. }
  26784. },
  26785. mesureSize: function() {
  26786. var box = this.getDom("content");
  26787. return uiUtils.getClientRect(box);
  26788. },
  26789. fitSize: function() {
  26790. if (this.captureWheel && this.sized) {
  26791. return this.__size;
  26792. }
  26793. this.sized = true;
  26794. var popBodyEl = this.getDom("body");
  26795. popBodyEl.style.width = "";
  26796. popBodyEl.style.height = "";
  26797. var size = this.mesureSize();
  26798. if (this.captureWheel) {
  26799. popBodyEl.style.width = -(-20 - size.width) + "px";
  26800. var height = parseInt(this.getDom("content").style.height, 10);
  26801. !window.isNaN(height) && (size.height = height);
  26802. } else {
  26803. popBodyEl.style.width = size.width + "px";
  26804. }
  26805. popBodyEl.style.height = size.height + "px";
  26806. this.__size = size;
  26807. this.captureWheel && (this.getDom("content").style.overflow = "auto");
  26808. return size;
  26809. },
  26810. showAnchor: function(element, hoz) {
  26811. this.showAnchorRect(uiUtils.getClientRect(element), hoz);
  26812. },
  26813. showAnchorRect: function(rect, hoz, adj) {
  26814. this._doAutoRender();
  26815. var vpRect = uiUtils.getViewportRect();
  26816. this.getDom().style.visibility = "hidden";
  26817. this._show();
  26818. var popSize = this.fitSize();
  26819. var sideLeft, sideUp, left, top;
  26820. if (hoz) {
  26821. sideLeft =
  26822. this.canSideLeft &&
  26823. (rect.right + popSize.width > vpRect.right &&
  26824. rect.left > popSize.width);
  26825. sideUp =
  26826. this.canSideUp &&
  26827. (rect.top + popSize.height > vpRect.bottom &&
  26828. rect.bottom > popSize.height);
  26829. left = sideLeft ? rect.left - popSize.width : rect.right;
  26830. top = sideUp ? rect.bottom - popSize.height : rect.top;
  26831. } else {
  26832. sideLeft =
  26833. this.canSideLeft &&
  26834. (rect.right + popSize.width > vpRect.right &&
  26835. rect.left > popSize.width);
  26836. sideUp =
  26837. this.canSideUp &&
  26838. (rect.top + popSize.height > vpRect.bottom &&
  26839. rect.bottom > popSize.height);
  26840. left = sideLeft ? rect.right - popSize.width : rect.left;
  26841. top = sideUp ? rect.top - popSize.height : rect.bottom;
  26842. }
  26843. var popEl = this.getDom();
  26844. uiUtils.setViewportOffset(popEl, {
  26845. left: left,
  26846. top: top
  26847. });
  26848. domUtils.removeClasses(popEl, ANCHOR_CLASSES);
  26849. popEl.className +=
  26850. " " + ANCHOR_CLASSES[(sideUp ? 1 : 0) * 2 + (sideLeft ? 1 : 0)];
  26851. if (this.editor) {
  26852. popEl.style.zIndex = this.editor.container.style.zIndex * 1 + 10;
  26853. baidu.editor.ui.uiUtils.getFixedLayer().style.zIndex =
  26854. popEl.style.zIndex - 1;
  26855. }
  26856. this.getDom().style.visibility = "visible";
  26857. },
  26858. showAt: function(offset) {
  26859. var left = offset.left;
  26860. var top = offset.top;
  26861. var rect = {
  26862. left: left,
  26863. top: top,
  26864. right: left,
  26865. bottom: top,
  26866. height: 0,
  26867. width: 0
  26868. };
  26869. this.showAnchorRect(rect, false, true);
  26870. },
  26871. _show: function() {
  26872. if (this._hidden) {
  26873. var box = this.getDom();
  26874. box.style.display = "";
  26875. this._hidden = false;
  26876. // if (box.setActive) {
  26877. // box.setActive();
  26878. // }
  26879. this.fireEvent("show");
  26880. }
  26881. },
  26882. isHidden: function() {
  26883. return this._hidden;
  26884. },
  26885. show: function() {
  26886. this._doAutoRender();
  26887. this._show();
  26888. },
  26889. hide: function(notNofity) {
  26890. if (!this._hidden && this.getDom()) {
  26891. this.getDom().style.display = "none";
  26892. this._hidden = true;
  26893. if (!notNofity) {
  26894. this.fireEvent("hide");
  26895. }
  26896. }
  26897. },
  26898. queryAutoHide: function(el) {
  26899. return !el || !uiUtils.contains(this.getDom(), el);
  26900. }
  26901. };
  26902. utils.inherits(Popup, UIBase);
  26903. domUtils.on(document, "mousedown", function(evt) {
  26904. var el = evt.target || evt.srcElement;
  26905. closeAllPopup(evt, el);
  26906. });
  26907. domUtils.on(window, "scroll", function(evt, el) {
  26908. closeAllPopup(evt, el);
  26909. });
  26910. })();
  26911. // ui/colorpicker.js
  26912. ///import core
  26913. ///import uicore
  26914. (function() {
  26915. var utils = baidu.editor.utils,
  26916. UIBase = baidu.editor.ui.UIBase,
  26917. ColorPicker = (baidu.editor.ui.ColorPicker = function(options) {
  26918. this.initOptions(options);
  26919. this.noColorText = this.noColorText || this.editor.getLang("clearColor");
  26920. this.initUIBase();
  26921. });
  26922. ColorPicker.prototype = {
  26923. getHtmlTpl: function() {
  26924. return genColorPicker(this.noColorText, this.editor);
  26925. },
  26926. _onTableClick: function(evt) {
  26927. var tgt = evt.target || evt.srcElement;
  26928. var color = tgt.getAttribute("data-color");
  26929. if (color) {
  26930. this.fireEvent("pickcolor", color);
  26931. }
  26932. },
  26933. _onTableOver: function(evt) {
  26934. var tgt = evt.target || evt.srcElement;
  26935. var color = tgt.getAttribute("data-color");
  26936. if (color) {
  26937. this.getDom("preview").style.backgroundColor = color;
  26938. }
  26939. },
  26940. _onTableOut: function() {
  26941. this.getDom("preview").style.backgroundColor = "";
  26942. },
  26943. _onPickNoColor: function() {
  26944. this.fireEvent("picknocolor");
  26945. }
  26946. };
  26947. utils.inherits(ColorPicker, UIBase);
  26948. var COLORS = ("ffffff,000000,eeece1,1f497d,4f81bd,c0504d,9bbb59,8064a2,4bacc6,f79646," +
  26949. "f2f2f2,7f7f7f,ddd9c3,c6d9f0,dbe5f1,f2dcdb,ebf1dd,e5e0ec,dbeef3,fdeada," +
  26950. "d8d8d8,595959,c4bd97,8db3e2,b8cce4,e5b9b7,d7e3bc,ccc1d9,b7dde8,fbd5b5," +
  26951. "bfbfbf,3f3f3f,938953,548dd4,95b3d7,d99694,c3d69b,b2a2c7,92cddc,fac08f," +
  26952. "a5a5a5,262626,494429,17365d,366092,953734,76923c,5f497a,31859b,e36c09," +
  26953. "7f7f7f,0c0c0c,1d1b10,0f243e,244061,632423,4f6128,3f3151,205867,974806," +
  26954. "c00000,ff0000,ffc000,ffff00,92d050,00b050,00b0f0,0070c0,002060,7030a0,").split(
  26955. ","
  26956. );
  26957. function genColorPicker(noColorText, editor) {
  26958. var html =
  26959. '<div id="##" class="edui-colorpicker %%">' +
  26960. '<div class="edui-colorpicker-topbar edui-clearfix">' +
  26961. '<div unselectable="on" id="##_preview" class="edui-colorpicker-preview"></div>' +
  26962. '<div unselectable="on" class="edui-colorpicker-nocolor" onclick="$$._onPickNoColor(event, this);">' +
  26963. noColorText +
  26964. "</div>" +
  26965. "</div>" +
  26966. '<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">' +
  26967. '<tr style="border-bottom: 1px solid #ddd;font-size: 13px;line-height: 25px;color:#39C;padding-top: 2px"><td colspan="10">' +
  26968. editor.getLang("themeColor") +
  26969. "</td> </tr>" +
  26970. '<tr class="edui-colorpicker-tablefirstrow" >';
  26971. for (var i = 0; i < COLORS.length; i++) {
  26972. if (i && i % 10 === 0) {
  26973. html +=
  26974. "</tr>" +
  26975. (i == 60
  26976. ? '<tr style="border-bottom: 1px solid #ddd;font-size: 13px;line-height: 25px;color:#39C;"><td colspan="10">' +
  26977. editor.getLang("standardColor") +
  26978. "</td></tr>"
  26979. : "") +
  26980. "<tr" +
  26981. (i == 60 ? ' class="edui-colorpicker-tablefirstrow"' : "") +
  26982. ">";
  26983. }
  26984. html += i < 70
  26985. ? '<td style="padding: 0 2px;"><a hidefocus title="' +
  26986. COLORS[i] +
  26987. '" onclick="return false;" href="javascript:" unselectable="on" class="edui-box edui-colorpicker-colorcell"' +
  26988. ' data-color="#' +
  26989. COLORS[i] +
  26990. '"' +
  26991. ' style="background-color:#' +
  26992. COLORS[i] +
  26993. ";border:solid #ccc;" +
  26994. (i < 10 || i >= 60
  26995. ? "border-width:1px;"
  26996. : i >= 10 && i < 20
  26997. ? "border-width:1px 1px 0 1px;"
  26998. : "border-width:0 1px 0 1px;") +
  26999. '"' +
  27000. "></a></td>"
  27001. : "";
  27002. }
  27003. html += "</tr></table></div>";
  27004. return html;
  27005. }
  27006. })();
  27007. // ui/tablepicker.js
  27008. ///import core
  27009. ///import uicore
  27010. (function() {
  27011. var utils = baidu.editor.utils,
  27012. uiUtils = baidu.editor.ui.uiUtils,
  27013. UIBase = baidu.editor.ui.UIBase;
  27014. var TablePicker = (baidu.editor.ui.TablePicker = function(options) {
  27015. this.initOptions(options);
  27016. this.initTablePicker();
  27017. });
  27018. TablePicker.prototype = {
  27019. defaultNumRows: 10,
  27020. defaultNumCols: 10,
  27021. maxNumRows: 20,
  27022. maxNumCols: 20,
  27023. numRows: 10,
  27024. numCols: 10,
  27025. lengthOfCellSide: 22,
  27026. initTablePicker: function() {
  27027. this.initUIBase();
  27028. },
  27029. getHtmlTpl: function() {
  27030. var me = this;
  27031. return (
  27032. '<div id="##" class="edui-tablepicker %%">' +
  27033. '<div class="edui-tablepicker-body">' +
  27034. '<div class="edui-infoarea">' +
  27035. '<span id="##_label" class="edui-label"></span>' +
  27036. "</div>" +
  27037. '<div class="edui-pickarea"' +
  27038. ' onmousemove="$$._onMouseMove(event, this);"' +
  27039. ' onmouseover="$$._onMouseOver(event, this);"' +
  27040. ' onmouseout="$$._onMouseOut(event, this);"' +
  27041. ' onclick="$$._onClick(event, this);"' +
  27042. ">" +
  27043. '<div id="##_overlay" class="edui-overlay"></div>' +
  27044. "</div>" +
  27045. "</div>" +
  27046. "</div>"
  27047. );
  27048. },
  27049. _UIBase_render: UIBase.prototype.render,
  27050. render: function(holder) {
  27051. this._UIBase_render(holder);
  27052. this.getDom("label").innerHTML =
  27053. "0" +
  27054. this.editor.getLang("t_row") +
  27055. " x 0" +
  27056. this.editor.getLang("t_col");
  27057. },
  27058. _track: function(numCols, numRows) {
  27059. var style = this.getDom("overlay").style;
  27060. var sideLen = this.lengthOfCellSide;
  27061. style.width = numCols * sideLen + "px";
  27062. style.height = numRows * sideLen + "px";
  27063. var label = this.getDom("label");
  27064. label.innerHTML =
  27065. numCols +
  27066. this.editor.getLang("t_col") +
  27067. " x " +
  27068. numRows +
  27069. this.editor.getLang("t_row");
  27070. this.numCols = numCols;
  27071. this.numRows = numRows;
  27072. },
  27073. _onMouseOver: function(evt, el) {
  27074. var rel = evt.relatedTarget || evt.fromElement;
  27075. if (!uiUtils.contains(el, rel) && el !== rel) {
  27076. this.getDom("label").innerHTML =
  27077. "0" +
  27078. this.editor.getLang("t_col") +
  27079. " x 0" +
  27080. this.editor.getLang("t_row");
  27081. this.getDom("overlay").style.visibility = "";
  27082. }
  27083. },
  27084. _onMouseOut: function(evt, el) {
  27085. var rel = evt.relatedTarget || evt.toElement;
  27086. if (!uiUtils.contains(el, rel) && el !== rel) {
  27087. this.getDom("label").innerHTML =
  27088. "0" +
  27089. this.editor.getLang("t_col") +
  27090. " x 0" +
  27091. this.editor.getLang("t_row");
  27092. this.getDom("overlay").style.visibility = "hidden";
  27093. }
  27094. },
  27095. _onMouseMove: function(evt, el) {
  27096. var style = this.getDom("overlay").style;
  27097. var offset = uiUtils.getEventOffset(evt);
  27098. var sideLen = this.lengthOfCellSide;
  27099. var numCols = Math.ceil(offset.left / sideLen);
  27100. var numRows = Math.ceil(offset.top / sideLen);
  27101. this._track(numCols, numRows);
  27102. },
  27103. _onClick: function() {
  27104. this.fireEvent("picktable", this.numCols, this.numRows);
  27105. }
  27106. };
  27107. utils.inherits(TablePicker, UIBase);
  27108. })();
  27109. // ui/stateful.js
  27110. (function() {
  27111. var browser = baidu.editor.browser,
  27112. domUtils = baidu.editor.dom.domUtils,
  27113. uiUtils = baidu.editor.ui.uiUtils;
  27114. var TPL_STATEFUL =
  27115. 'onmousedown="$$.Stateful_onMouseDown(event, this);"' +
  27116. ' onmouseup="$$.Stateful_onMouseUp(event, this);"' +
  27117. (browser.ie
  27118. ? ' onmouseenter="$$.Stateful_onMouseEnter(event, this);"' +
  27119. ' onmouseleave="$$.Stateful_onMouseLeave(event, this);"'
  27120. : ' onmouseover="$$.Stateful_onMouseOver(event, this);"' +
  27121. ' onmouseout="$$.Stateful_onMouseOut(event, this);"');
  27122. baidu.editor.ui.Stateful = {
  27123. alwalysHoverable: false,
  27124. target: null, //目标元素和this指向dom不一样
  27125. Stateful_init: function() {
  27126. this._Stateful_dGetHtmlTpl = this.getHtmlTpl;
  27127. this.getHtmlTpl = this.Stateful_getHtmlTpl;
  27128. },
  27129. Stateful_getHtmlTpl: function() {
  27130. var tpl = this._Stateful_dGetHtmlTpl();
  27131. // 使用function避免$转义
  27132. return tpl.replace(/stateful/g, function() {
  27133. return TPL_STATEFUL;
  27134. });
  27135. },
  27136. Stateful_onMouseEnter: function(evt, el) {
  27137. this.target = el;
  27138. if (!this.isDisabled() || this.alwalysHoverable) {
  27139. this.addState("hover");
  27140. this.fireEvent("over");
  27141. }
  27142. },
  27143. Stateful_onMouseLeave: function(evt, el) {
  27144. if (!this.isDisabled() || this.alwalysHoverable) {
  27145. this.removeState("hover");
  27146. this.removeState("active");
  27147. this.fireEvent("out");
  27148. }
  27149. },
  27150. Stateful_onMouseOver: function(evt, el) {
  27151. var rel = evt.relatedTarget;
  27152. if (!uiUtils.contains(el, rel) && el !== rel) {
  27153. this.Stateful_onMouseEnter(evt, el);
  27154. }
  27155. },
  27156. Stateful_onMouseOut: function(evt, el) {
  27157. var rel = evt.relatedTarget;
  27158. if (!uiUtils.contains(el, rel) && el !== rel) {
  27159. this.Stateful_onMouseLeave(evt, el);
  27160. }
  27161. },
  27162. Stateful_onMouseDown: function(evt, el) {
  27163. if (!this.isDisabled()) {
  27164. this.addState("active");
  27165. }
  27166. },
  27167. Stateful_onMouseUp: function(evt, el) {
  27168. if (!this.isDisabled()) {
  27169. this.removeState("active");
  27170. }
  27171. },
  27172. Stateful_postRender: function() {
  27173. if (this.disabled && !this.hasState("disabled")) {
  27174. this.addState("disabled");
  27175. }
  27176. },
  27177. hasState: function(state) {
  27178. return domUtils.hasClass(this.getStateDom(), "edui-state-" + state);
  27179. },
  27180. addState: function(state) {
  27181. if (!this.hasState(state)) {
  27182. this.getStateDom().className += " edui-state-" + state;
  27183. }
  27184. },
  27185. removeState: function(state) {
  27186. if (this.hasState(state)) {
  27187. domUtils.removeClasses(this.getStateDom(), ["edui-state-" + state]);
  27188. }
  27189. },
  27190. getStateDom: function() {
  27191. return this.getDom("state");
  27192. },
  27193. isChecked: function() {
  27194. return this.hasState("checked");
  27195. },
  27196. setChecked: function(checked) {
  27197. if (!this.isDisabled() && checked) {
  27198. this.addState("checked");
  27199. } else {
  27200. this.removeState("checked");
  27201. }
  27202. },
  27203. isDisabled: function() {
  27204. return this.hasState("disabled");
  27205. },
  27206. setDisabled: function(disabled) {
  27207. if (disabled) {
  27208. this.removeState("hover");
  27209. this.removeState("checked");
  27210. this.removeState("active");
  27211. this.addState("disabled");
  27212. } else {
  27213. this.removeState("disabled");
  27214. }
  27215. }
  27216. };
  27217. })();
  27218. // ui/button.js
  27219. ///import core
  27220. ///import uicore
  27221. ///import ui/stateful.js
  27222. (function() {
  27223. var utils = baidu.editor.utils,
  27224. UIBase = baidu.editor.ui.UIBase,
  27225. Stateful = baidu.editor.ui.Stateful,
  27226. Button = (baidu.editor.ui.Button = function(options) {
  27227. if (options.name) {
  27228. var btnName = options.name;
  27229. var cssRules = options.cssRules;
  27230. if (!options.className) {
  27231. options.className = "edui-for-" + btnName;
  27232. }
  27233. options.cssRules =
  27234. ".edui-" +
  27235. (options.theme || "default") +
  27236. " .edui-toolbar .edui-button.edui-for-" +
  27237. btnName +
  27238. " .edui-icon {" +
  27239. cssRules +
  27240. "}";
  27241. }
  27242. this.initOptions(options);
  27243. this.initButton();
  27244. });
  27245. Button.prototype = {
  27246. uiName: "button",
  27247. label: "",
  27248. title: "",
  27249. showIcon: true,
  27250. showText: true,
  27251. cssRules: "",
  27252. initButton: function() {
  27253. this.initUIBase();
  27254. this.Stateful_init();
  27255. if (this.cssRules) {
  27256. utils.cssRule("edui-customize-" + this.name + "-style", this.cssRules);
  27257. }
  27258. },
  27259. getHtmlTpl: function() {
  27260. return (
  27261. '<div id="##" class="edui-box %%">' +
  27262. '<div id="##_state" stateful>' +
  27263. '<div class="%%-wrap"><div id="##_body" unselectable="on" ' +
  27264. (this.title ? 'title="' + this.title + '"' : "") +
  27265. ' class="%%-body" onmousedown="return $$._onMouseDown(event, this);" onclick="return $$._onClick(event, this);">' +
  27266. (this.showIcon ? '<div class="edui-box edui-icon"></div>' : "") +
  27267. (this.showText
  27268. ? '<div class="edui-box edui-label">' + this.label + "</div>"
  27269. : "") +
  27270. "</div>" +
  27271. "</div>" +
  27272. "</div></div>"
  27273. );
  27274. },
  27275. postRender: function() {
  27276. this.Stateful_postRender();
  27277. this.setDisabled(this.disabled);
  27278. },
  27279. _onMouseDown: function(e) {
  27280. var target = e.target || e.srcElement,
  27281. tagName = target && target.tagName && target.tagName.toLowerCase();
  27282. if (tagName == "input" || tagName == "object" || tagName == "object") {
  27283. return false;
  27284. }
  27285. },
  27286. _onClick: function() {
  27287. if (!this.isDisabled()) {
  27288. this.fireEvent("click");
  27289. }
  27290. },
  27291. setTitle: function(text) {
  27292. var label = this.getDom("label");
  27293. label.innerHTML = text;
  27294. }
  27295. };
  27296. utils.inherits(Button, UIBase);
  27297. utils.extend(Button.prototype, Stateful);
  27298. })();
  27299. // ui/splitbutton.js
  27300. ///import core
  27301. ///import uicore
  27302. ///import ui/stateful.js
  27303. (function() {
  27304. var utils = baidu.editor.utils,
  27305. uiUtils = baidu.editor.ui.uiUtils,
  27306. domUtils = baidu.editor.dom.domUtils,
  27307. UIBase = baidu.editor.ui.UIBase,
  27308. Stateful = baidu.editor.ui.Stateful,
  27309. SplitButton = (baidu.editor.ui.SplitButton = function(options) {
  27310. this.initOptions(options);
  27311. this.initSplitButton();
  27312. });
  27313. SplitButton.prototype = {
  27314. popup: null,
  27315. uiName: "splitbutton",
  27316. title: "",
  27317. initSplitButton: function() {
  27318. this.initUIBase();
  27319. this.Stateful_init();
  27320. var me = this;
  27321. if (this.popup != null) {
  27322. var popup = this.popup;
  27323. this.popup = null;
  27324. this.setPopup(popup);
  27325. }
  27326. },
  27327. _UIBase_postRender: UIBase.prototype.postRender,
  27328. postRender: function() {
  27329. this.Stateful_postRender();
  27330. this._UIBase_postRender();
  27331. },
  27332. setPopup: function(popup) {
  27333. if (this.popup === popup) return;
  27334. if (this.popup != null) {
  27335. this.popup.dispose();
  27336. }
  27337. popup.addListener("show", utils.bind(this._onPopupShow, this));
  27338. popup.addListener("hide", utils.bind(this._onPopupHide, this));
  27339. popup.addListener(
  27340. "postrender",
  27341. utils.bind(function() {
  27342. popup
  27343. .getDom("body")
  27344. .appendChild(
  27345. uiUtils.createElementByHtml(
  27346. '<div id="' +
  27347. this.popup.id +
  27348. '_bordereraser" class="edui-bordereraser edui-background" style="width:' +
  27349. (uiUtils.getClientRect(this.getDom()).width + 20) +
  27350. 'px"></div>'
  27351. )
  27352. );
  27353. popup.getDom().className += " " + this.className;
  27354. }, this)
  27355. );
  27356. this.popup = popup;
  27357. },
  27358. _onPopupShow: function() {
  27359. this.addState("opened");
  27360. },
  27361. _onPopupHide: function() {
  27362. this.removeState("opened");
  27363. },
  27364. getHtmlTpl: function() {
  27365. return (
  27366. '<div id="##" class="edui-box %%">' +
  27367. "<div " +
  27368. (this.title ? 'title="' + this.title + '"' : "") +
  27369. ' id="##_state" stateful><div class="%%-body">' +
  27370. '<div id="##_button_body" class="edui-box edui-button-body" onclick="$$._onButtonClick(event, this);">' +
  27371. '<div class="edui-box edui-icon"></div>' +
  27372. "</div>" +
  27373. '<div class="edui-box edui-splitborder"></div>' +
  27374. '<div class="edui-box edui-arrow" onclick="$$._onArrowClick();"></div>' +
  27375. "</div></div></div>"
  27376. );
  27377. },
  27378. showPopup: function() {
  27379. // 当popup往上弹出的时候,做特殊处理
  27380. var rect = uiUtils.getClientRect(this.getDom());
  27381. rect.top -= this.popup.SHADOW_RADIUS;
  27382. rect.height += this.popup.SHADOW_RADIUS;
  27383. this.popup.showAnchorRect(rect);
  27384. },
  27385. _onArrowClick: function(event, el) {
  27386. if (!this.isDisabled()) {
  27387. this.showPopup();
  27388. }
  27389. },
  27390. _onButtonClick: function() {
  27391. if (!this.isDisabled()) {
  27392. this.fireEvent("buttonclick");
  27393. }
  27394. }
  27395. };
  27396. utils.inherits(SplitButton, UIBase);
  27397. utils.extend(SplitButton.prototype, Stateful, true);
  27398. })();
  27399. // ui/colorbutton.js
  27400. ///import core
  27401. ///import uicore
  27402. ///import ui/colorpicker.js
  27403. ///import ui/popup.js
  27404. ///import ui/splitbutton.js
  27405. (function() {
  27406. var utils = baidu.editor.utils,
  27407. uiUtils = baidu.editor.ui.uiUtils,
  27408. ColorPicker = baidu.editor.ui.ColorPicker,
  27409. Popup = baidu.editor.ui.Popup,
  27410. SplitButton = baidu.editor.ui.SplitButton,
  27411. ColorButton = (baidu.editor.ui.ColorButton = function(options) {
  27412. this.initOptions(options);
  27413. this.initColorButton();
  27414. });
  27415. ColorButton.prototype = {
  27416. initColorButton: function() {
  27417. var me = this;
  27418. this.popup = new Popup({
  27419. content: new ColorPicker({
  27420. noColorText: me.editor.getLang("clearColor"),
  27421. editor: me.editor,
  27422. onpickcolor: function(t, color) {
  27423. me._onPickColor(color);
  27424. },
  27425. onpicknocolor: function(t, color) {
  27426. me._onPickNoColor(color);
  27427. }
  27428. }),
  27429. editor: me.editor
  27430. });
  27431. this.initSplitButton();
  27432. },
  27433. _SplitButton_postRender: SplitButton.prototype.postRender,
  27434. postRender: function() {
  27435. this._SplitButton_postRender();
  27436. this.getDom("button_body").appendChild(
  27437. uiUtils.createElementByHtml(
  27438. '<div id="' + this.id + '_colorlump" class="edui-colorlump"></div>'
  27439. )
  27440. );
  27441. this.getDom().className += " edui-colorbutton";
  27442. },
  27443. setColor: function(color) {
  27444. this.getDom("colorlump").style.backgroundColor = color;
  27445. this.color = color;
  27446. },
  27447. _onPickColor: function(color) {
  27448. if (this.fireEvent("pickcolor", color) !== false) {
  27449. this.setColor(color);
  27450. this.popup.hide();
  27451. }
  27452. },
  27453. _onPickNoColor: function(color) {
  27454. if (this.fireEvent("picknocolor") !== false) {
  27455. this.popup.hide();
  27456. }
  27457. }
  27458. };
  27459. utils.inherits(ColorButton, SplitButton);
  27460. })();
  27461. // ui/tablebutton.js
  27462. ///import core
  27463. ///import uicore
  27464. ///import ui/popup.js
  27465. ///import ui/tablepicker.js
  27466. ///import ui/splitbutton.js
  27467. (function() {
  27468. var utils = baidu.editor.utils,
  27469. Popup = baidu.editor.ui.Popup,
  27470. TablePicker = baidu.editor.ui.TablePicker,
  27471. SplitButton = baidu.editor.ui.SplitButton,
  27472. TableButton = (baidu.editor.ui.TableButton = function(options) {
  27473. this.initOptions(options);
  27474. this.initTableButton();
  27475. });
  27476. TableButton.prototype = {
  27477. initTableButton: function() {
  27478. var me = this;
  27479. this.popup = new Popup({
  27480. content: new TablePicker({
  27481. editor: me.editor,
  27482. onpicktable: function(t, numCols, numRows) {
  27483. me._onPickTable(numCols, numRows);
  27484. }
  27485. }),
  27486. editor: me.editor
  27487. });
  27488. this.initSplitButton();
  27489. },
  27490. _onPickTable: function(numCols, numRows) {
  27491. if (this.fireEvent("picktable", numCols, numRows) !== false) {
  27492. this.popup.hide();
  27493. }
  27494. }
  27495. };
  27496. utils.inherits(TableButton, SplitButton);
  27497. })();
  27498. // ui/autotypesetpicker.js
  27499. ///import core
  27500. ///import uicore
  27501. (function() {
  27502. var utils = baidu.editor.utils,
  27503. UIBase = baidu.editor.ui.UIBase;
  27504. var AutoTypeSetPicker = (baidu.editor.ui.AutoTypeSetPicker = function(
  27505. options
  27506. ) {
  27507. this.initOptions(options);
  27508. this.initAutoTypeSetPicker();
  27509. });
  27510. AutoTypeSetPicker.prototype = {
  27511. initAutoTypeSetPicker: function() {
  27512. this.initUIBase();
  27513. },
  27514. getHtmlTpl: function() {
  27515. var me = this.editor,
  27516. opt = me.options.autotypeset,
  27517. lang = me.getLang("autoTypeSet");
  27518. var textAlignInputName = "textAlignValue" + me.uid,
  27519. imageBlockInputName = "imageBlockLineValue" + me.uid,
  27520. symbolConverInputName = "symbolConverValue" + me.uid;
  27521. return (
  27522. '<div id="##" class="edui-autotypesetpicker %%">' +
  27523. '<div class="edui-autotypesetpicker-body">' +
  27524. "<table >" +
  27525. '<tr><td nowrap><input type="checkbox" name="mergeEmptyline" ' +
  27526. (opt["mergeEmptyline"] ? "checked" : "") +
  27527. ">" +
  27528. lang.mergeLine +
  27529. '</td><td colspan="2"><input type="checkbox" name="removeEmptyline" ' +
  27530. (opt["removeEmptyline"] ? "checked" : "") +
  27531. ">" +
  27532. lang.delLine +
  27533. "</td></tr>" +
  27534. '<tr><td nowrap><input type="checkbox" name="removeClass" ' +
  27535. (opt["removeClass"] ? "checked" : "") +
  27536. ">" +
  27537. lang.removeFormat +
  27538. '</td><td colspan="2"><input type="checkbox" name="indent" ' +
  27539. (opt["indent"] ? "checked" : "") +
  27540. ">" +
  27541. lang.indent +
  27542. "</td></tr>" +
  27543. "<tr>" +
  27544. '<td nowrap><input type="checkbox" name="textAlign" ' +
  27545. (opt["textAlign"] ? "checked" : "") +
  27546. ">" +
  27547. lang.alignment +
  27548. "</td>" +
  27549. '<td colspan="2" id="' +
  27550. textAlignInputName +
  27551. '">' +
  27552. '<input type="radio" name="' +
  27553. textAlignInputName +
  27554. '" value="left" ' +
  27555. (opt["textAlign"] && opt["textAlign"] == "left" ? "checked" : "") +
  27556. ">" +
  27557. me.getLang("justifyleft") +
  27558. '<input type="radio" name="' +
  27559. textAlignInputName +
  27560. '" value="center" ' +
  27561. (opt["textAlign"] && opt["textAlign"] == "center" ? "checked" : "") +
  27562. ">" +
  27563. me.getLang("justifycenter") +
  27564. '<input type="radio" name="' +
  27565. textAlignInputName +
  27566. '" value="right" ' +
  27567. (opt["textAlign"] && opt["textAlign"] == "right" ? "checked" : "") +
  27568. ">" +
  27569. me.getLang("justifyright") +
  27570. "</td>" +
  27571. "</tr>" +
  27572. "<tr>" +
  27573. '<td nowrap><input type="checkbox" name="imageBlockLine" ' +
  27574. (opt["imageBlockLine"] ? "checked" : "") +
  27575. ">" +
  27576. lang.imageFloat +
  27577. "</td>" +
  27578. '<td nowrap id="' +
  27579. imageBlockInputName +
  27580. '">' +
  27581. '<input type="radio" name="' +
  27582. imageBlockInputName +
  27583. '" value="none" ' +
  27584. (opt["imageBlockLine"] && opt["imageBlockLine"] == "none"
  27585. ? "checked"
  27586. : "") +
  27587. ">" +
  27588. me.getLang("default") +
  27589. '<input type="radio" name="' +
  27590. imageBlockInputName +
  27591. '" value="left" ' +
  27592. (opt["imageBlockLine"] && opt["imageBlockLine"] == "left"
  27593. ? "checked"
  27594. : "") +
  27595. ">" +
  27596. me.getLang("justifyleft") +
  27597. '<input type="radio" name="' +
  27598. imageBlockInputName +
  27599. '" value="center" ' +
  27600. (opt["imageBlockLine"] && opt["imageBlockLine"] == "center"
  27601. ? "checked"
  27602. : "") +
  27603. ">" +
  27604. me.getLang("justifycenter") +
  27605. '<input type="radio" name="' +
  27606. imageBlockInputName +
  27607. '" value="right" ' +
  27608. (opt["imageBlockLine"] && opt["imageBlockLine"] == "right"
  27609. ? "checked"
  27610. : "") +
  27611. ">" +
  27612. me.getLang("justifyright") +
  27613. "</td>" +
  27614. "</tr>" +
  27615. '<tr><td nowrap><input type="checkbox" name="clearFontSize" ' +
  27616. (opt["clearFontSize"] ? "checked" : "") +
  27617. ">" +
  27618. lang.removeFontsize +
  27619. '</td><td colspan="2"><input type="checkbox" name="clearFontFamily" ' +
  27620. (opt["clearFontFamily"] ? "checked" : "") +
  27621. ">" +
  27622. lang.removeFontFamily +
  27623. "</td></tr>" +
  27624. '<tr><td nowrap colspan="3"><input type="checkbox" name="removeEmptyNode" ' +
  27625. (opt["removeEmptyNode"] ? "checked" : "") +
  27626. ">" +
  27627. lang.removeHtml +
  27628. "</td></tr>" +
  27629. '<tr><td nowrap colspan="3"><input type="checkbox" name="pasteFilter" ' +
  27630. (opt["pasteFilter"] ? "checked" : "") +
  27631. ">" +
  27632. lang.pasteFilter +
  27633. "</td></tr>" +
  27634. "<tr>" +
  27635. '<td nowrap><input type="checkbox" name="symbolConver" ' +
  27636. (opt["bdc2sb"] || opt["tobdc"] ? "checked" : "") +
  27637. ">" +
  27638. lang.symbol +
  27639. "</td>" +
  27640. '<td id="' +
  27641. symbolConverInputName +
  27642. '">' +
  27643. '<input type="radio" name="bdc" value="bdc2sb" ' +
  27644. (opt["bdc2sb"] ? "checked" : "") +
  27645. ">" +
  27646. lang.bdc2sb +
  27647. '<input type="radio" name="bdc" value="tobdc" ' +
  27648. (opt["tobdc"] ? "checked" : "") +
  27649. ">" +
  27650. lang.tobdc +
  27651. "" +
  27652. "</td>" +
  27653. '<td nowrap align="right"><button >' +
  27654. lang.run +
  27655. "</button></td>" +
  27656. "</tr>" +
  27657. "</table>" +
  27658. "</div>" +
  27659. "</div>"
  27660. );
  27661. },
  27662. _UIBase_render: UIBase.prototype.render
  27663. };
  27664. utils.inherits(AutoTypeSetPicker, UIBase);
  27665. })();
  27666. // ui/autotypesetbutton.js
  27667. ///import core
  27668. ///import uicore
  27669. ///import ui/popup.js
  27670. ///import ui/autotypesetpicker.js
  27671. ///import ui/splitbutton.js
  27672. (function() {
  27673. var utils = baidu.editor.utils,
  27674. Popup = baidu.editor.ui.Popup,
  27675. AutoTypeSetPicker = baidu.editor.ui.AutoTypeSetPicker,
  27676. SplitButton = baidu.editor.ui.SplitButton,
  27677. AutoTypeSetButton = (baidu.editor.ui.AutoTypeSetButton = function(options) {
  27678. this.initOptions(options);
  27679. this.initAutoTypeSetButton();
  27680. });
  27681. function getPara(me) {
  27682. var opt = {},
  27683. cont = me.getDom(),
  27684. editorId = me.editor.uid,
  27685. inputType = null,
  27686. attrName = null,
  27687. ipts = domUtils.getElementsByTagName(cont, "input");
  27688. for (var i = ipts.length - 1, ipt; (ipt = ipts[i--]); ) {
  27689. inputType = ipt.getAttribute("type");
  27690. if (inputType == "checkbox") {
  27691. attrName = ipt.getAttribute("name");
  27692. opt[attrName] && delete opt[attrName];
  27693. if (ipt.checked) {
  27694. var attrValue = document.getElementById(
  27695. attrName + "Value" + editorId
  27696. );
  27697. if (attrValue) {
  27698. if (/input/gi.test(attrValue.tagName)) {
  27699. opt[attrName] = attrValue.value;
  27700. } else {
  27701. var iptChilds = attrValue.getElementsByTagName("input");
  27702. for (
  27703. var j = iptChilds.length - 1, iptchild;
  27704. (iptchild = iptChilds[j--]);
  27705. ) {
  27706. if (iptchild.checked) {
  27707. opt[attrName] = iptchild.value;
  27708. break;
  27709. }
  27710. }
  27711. }
  27712. } else {
  27713. opt[attrName] = true;
  27714. }
  27715. } else {
  27716. opt[attrName] = false;
  27717. }
  27718. } else {
  27719. opt[ipt.getAttribute("value")] = ipt.checked;
  27720. }
  27721. }
  27722. var selects = domUtils.getElementsByTagName(cont, "select");
  27723. for (var i = 0, si; (si = selects[i++]); ) {
  27724. var attr = si.getAttribute("name");
  27725. opt[attr] = opt[attr] ? si.value : "";
  27726. }
  27727. utils.extend(me.editor.options.autotypeset, opt);
  27728. me.editor.setPreferences("autotypeset", opt);
  27729. }
  27730. AutoTypeSetButton.prototype = {
  27731. initAutoTypeSetButton: function() {
  27732. var me = this;
  27733. this.popup = new Popup({
  27734. //传入配置参数
  27735. content: new AutoTypeSetPicker({ editor: me.editor }),
  27736. editor: me.editor,
  27737. hide: function() {
  27738. if (!this._hidden && this.getDom()) {
  27739. getPara(this);
  27740. this.getDom().style.display = "none";
  27741. this._hidden = true;
  27742. this.fireEvent("hide");
  27743. }
  27744. }
  27745. });
  27746. var flag = 0;
  27747. this.popup.addListener("postRenderAfter", function() {
  27748. var popupUI = this;
  27749. if (flag) return;
  27750. var cont = this.getDom(),
  27751. btn = cont.getElementsByTagName("button")[0];
  27752. btn.onclick = function() {
  27753. getPara(popupUI);
  27754. me.editor.execCommand("autotypeset");
  27755. popupUI.hide();
  27756. };
  27757. domUtils.on(cont, "click", function(e) {
  27758. var target = e.target || e.srcElement,
  27759. editorId = me.editor.uid;
  27760. if (target && target.tagName == "INPUT") {
  27761. // 点击图片浮动的checkbox,去除对应的radio
  27762. if (
  27763. target.name == "imageBlockLine" ||
  27764. target.name == "textAlign" ||
  27765. target.name == "symbolConver"
  27766. ) {
  27767. var checked = target.checked,
  27768. radioTd = document.getElementById(
  27769. target.name + "Value" + editorId
  27770. ),
  27771. radios = radioTd.getElementsByTagName("input"),
  27772. defalutSelect = {
  27773. imageBlockLine: "none",
  27774. textAlign: "left",
  27775. symbolConver: "tobdc"
  27776. };
  27777. for (var i = 0; i < radios.length; i++) {
  27778. if (checked) {
  27779. if (radios[i].value == defalutSelect[target.name]) {
  27780. radios[i].checked = "checked";
  27781. }
  27782. } else {
  27783. radios[i].checked = false;
  27784. }
  27785. }
  27786. }
  27787. // 点击radio,选中对应的checkbox
  27788. if (
  27789. target.name == "imageBlockLineValue" + editorId ||
  27790. target.name == "textAlignValue" + editorId ||
  27791. target.name == "bdc"
  27792. ) {
  27793. var checkboxs = target.parentNode.previousSibling.getElementsByTagName(
  27794. "input"
  27795. );
  27796. checkboxs && (checkboxs[0].checked = true);
  27797. }
  27798. getPara(popupUI);
  27799. }
  27800. });
  27801. flag = 1;
  27802. });
  27803. this.initSplitButton();
  27804. }
  27805. };
  27806. utils.inherits(AutoTypeSetButton, SplitButton);
  27807. })();
  27808. // ui/cellalignpicker.js
  27809. ///import core
  27810. ///import uicore
  27811. (function() {
  27812. var utils = baidu.editor.utils,
  27813. Popup = baidu.editor.ui.Popup,
  27814. Stateful = baidu.editor.ui.Stateful,
  27815. UIBase = baidu.editor.ui.UIBase;
  27816. /**
  27817. * 该参数将新增一个参数: selected, 参数类型为一个Object, 形如{ 'align': 'center', 'valign': 'top' }, 表示单元格的初始
  27818. * 对齐状态为: 竖直居上,水平居中; 其中 align的取值为:'center', 'left', 'right'; valign的取值为: 'top', 'middle', 'bottom'
  27819. * @update 2013/4/2 hancong03@baidu.com
  27820. */
  27821. var CellAlignPicker = (baidu.editor.ui.CellAlignPicker = function(options) {
  27822. this.initOptions(options);
  27823. this.initSelected();
  27824. this.initCellAlignPicker();
  27825. });
  27826. CellAlignPicker.prototype = {
  27827. //初始化选中状态, 该方法将根据传递进来的参数获取到应该选中的对齐方式图标的索引
  27828. initSelected: function() {
  27829. var status = {
  27830. valign: {
  27831. top: 0,
  27832. middle: 1,
  27833. bottom: 2
  27834. },
  27835. align: {
  27836. left: 0,
  27837. center: 1,
  27838. right: 2
  27839. },
  27840. count: 3
  27841. },
  27842. result = -1;
  27843. if (this.selected) {
  27844. this.selectedIndex =
  27845. status.valign[this.selected.valign] * status.count +
  27846. status.align[this.selected.align];
  27847. }
  27848. },
  27849. initCellAlignPicker: function() {
  27850. this.initUIBase();
  27851. this.Stateful_init();
  27852. },
  27853. getHtmlTpl: function() {
  27854. var alignType = ["left", "center", "right"],
  27855. COUNT = 9,
  27856. tempClassName = null,
  27857. tempIndex = -1,
  27858. tmpl = [];
  27859. for (var i = 0; i < COUNT; i++) {
  27860. tempClassName = this.selectedIndex === i
  27861. ? ' class="edui-cellalign-selected" '
  27862. : "";
  27863. tempIndex = i % 3;
  27864. tempIndex === 0 && tmpl.push("<tr>");
  27865. tmpl.push(
  27866. '<td index="' +
  27867. i +
  27868. '" ' +
  27869. tempClassName +
  27870. ' stateful><div class="edui-icon edui-' +
  27871. alignType[tempIndex] +
  27872. '"></div></td>'
  27873. );
  27874. tempIndex === 2 && tmpl.push("</tr>");
  27875. }
  27876. return (
  27877. '<div id="##" class="edui-cellalignpicker %%">' +
  27878. '<div class="edui-cellalignpicker-body">' +
  27879. '<table onclick="$$._onClick(event);">' +
  27880. tmpl.join("") +
  27881. "</table>" +
  27882. "</div>" +
  27883. "</div>"
  27884. );
  27885. },
  27886. getStateDom: function() {
  27887. return this.target;
  27888. },
  27889. _onClick: function(evt) {
  27890. var target = evt.target || evt.srcElement;
  27891. if (/icon/.test(target.className)) {
  27892. this.items[target.parentNode.getAttribute("index")].onclick();
  27893. Popup.postHide(evt);
  27894. }
  27895. },
  27896. _UIBase_render: UIBase.prototype.render
  27897. };
  27898. utils.inherits(CellAlignPicker, UIBase);
  27899. utils.extend(CellAlignPicker.prototype, Stateful, true);
  27900. })();
  27901. // ui/pastepicker.js
  27902. ///import core
  27903. ///import uicore
  27904. (function() {
  27905. var utils = baidu.editor.utils,
  27906. Stateful = baidu.editor.ui.Stateful,
  27907. uiUtils = baidu.editor.ui.uiUtils,
  27908. UIBase = baidu.editor.ui.UIBase;
  27909. var PastePicker = (baidu.editor.ui.PastePicker = function(options) {
  27910. this.initOptions(options);
  27911. this.initPastePicker();
  27912. });
  27913. PastePicker.prototype = {
  27914. initPastePicker: function() {
  27915. this.initUIBase();
  27916. this.Stateful_init();
  27917. },
  27918. getHtmlTpl: function() {
  27919. return (
  27920. '<div class="edui-pasteicon" onclick="$$._onClick(this)"></div>' +
  27921. '<div class="edui-pastecontainer">' +
  27922. '<div class="edui-title">' +
  27923. this.editor.getLang("pasteOpt") +
  27924. "</div>" +
  27925. '<div class="edui-button">' +
  27926. '<div title="' +
  27927. this.editor.getLang("pasteSourceFormat") +
  27928. '" onclick="$$.format(false)" stateful>' +
  27929. '<div class="edui-richtxticon"></div></div>' +
  27930. '<div title="' +
  27931. this.editor.getLang("tagFormat") +
  27932. '" onclick="$$.format(2)" stateful>' +
  27933. '<div class="edui-tagicon"></div></div>' +
  27934. '<div title="' +
  27935. this.editor.getLang("pasteTextFormat") +
  27936. '" onclick="$$.format(true)" stateful>' +
  27937. '<div class="edui-plaintxticon"></div></div>' +
  27938. "</div>" +
  27939. "</div>" +
  27940. "</div>"
  27941. );
  27942. },
  27943. getStateDom: function() {
  27944. return this.target;
  27945. },
  27946. format: function(param) {
  27947. this.editor.ui._isTransfer = true;
  27948. this.editor.fireEvent("pasteTransfer", param);
  27949. },
  27950. _onClick: function(cur) {
  27951. var node = domUtils.getNextDomNode(cur),
  27952. screenHt = uiUtils.getViewportRect().height,
  27953. subPop = uiUtils.getClientRect(node);
  27954. if (subPop.top + subPop.height > screenHt)
  27955. node.style.top = -subPop.height - cur.offsetHeight + "px";
  27956. else node.style.top = "";
  27957. if (/hidden/gi.test(domUtils.getComputedStyle(node, "visibility"))) {
  27958. node.style.visibility = "visible";
  27959. domUtils.addClass(cur, "edui-state-opened");
  27960. } else {
  27961. node.style.visibility = "hidden";
  27962. domUtils.removeClasses(cur, "edui-state-opened");
  27963. }
  27964. },
  27965. _UIBase_render: UIBase.prototype.render
  27966. };
  27967. utils.inherits(PastePicker, UIBase);
  27968. utils.extend(PastePicker.prototype, Stateful, true);
  27969. })();
  27970. // ui/toolbar.js
  27971. (function() {
  27972. var utils = baidu.editor.utils,
  27973. uiUtils = baidu.editor.ui.uiUtils,
  27974. UIBase = baidu.editor.ui.UIBase,
  27975. Toolbar = (baidu.editor.ui.Toolbar = function(options) {
  27976. this.initOptions(options);
  27977. this.initToolbar();
  27978. });
  27979. Toolbar.prototype = {
  27980. items: null,
  27981. initToolbar: function() {
  27982. this.items = this.items || [];
  27983. this.initUIBase();
  27984. },
  27985. add: function(item, index) {
  27986. if (index === undefined) {
  27987. this.items.push(item);
  27988. } else {
  27989. this.items.splice(index, 0, item);
  27990. }
  27991. },
  27992. getHtmlTpl: function() {
  27993. var buff = [];
  27994. for (var i = 0; i < this.items.length; i++) {
  27995. buff[i] = this.items[i].renderHtml();
  27996. }
  27997. return (
  27998. '<div id="##" class="edui-toolbar %%" onselectstart="return false;" onmousedown="return $$._onMouseDown(event, this);">' +
  27999. buff.join("") +
  28000. "</div>"
  28001. );
  28002. },
  28003. postRender: function() {
  28004. var box = this.getDom();
  28005. for (var i = 0; i < this.items.length; i++) {
  28006. this.items[i].postRender();
  28007. }
  28008. uiUtils.makeUnselectable(box);
  28009. },
  28010. _onMouseDown: function(e) {
  28011. var target = e.target || e.srcElement,
  28012. tagName = target && target.tagName && target.tagName.toLowerCase();
  28013. if (tagName == "input" || tagName == "object" || tagName == "object") {
  28014. return false;
  28015. }
  28016. }
  28017. };
  28018. utils.inherits(Toolbar, UIBase);
  28019. })();
  28020. // ui/menu.js
  28021. ///import core
  28022. ///import uicore
  28023. ///import ui\popup.js
  28024. ///import ui\stateful.js
  28025. (function() {
  28026. var utils = baidu.editor.utils,
  28027. domUtils = baidu.editor.dom.domUtils,
  28028. uiUtils = baidu.editor.ui.uiUtils,
  28029. UIBase = baidu.editor.ui.UIBase,
  28030. Popup = baidu.editor.ui.Popup,
  28031. Stateful = baidu.editor.ui.Stateful,
  28032. CellAlignPicker = baidu.editor.ui.CellAlignPicker,
  28033. Menu = (baidu.editor.ui.Menu = function(options) {
  28034. this.initOptions(options);
  28035. this.initMenu();
  28036. });
  28037. var menuSeparator = {
  28038. renderHtml: function() {
  28039. return '<div class="edui-menuitem edui-menuseparator"><div class="edui-menuseparator-inner"></div></div>';
  28040. },
  28041. postRender: function() {},
  28042. queryAutoHide: function() {
  28043. return true;
  28044. }
  28045. };
  28046. Menu.prototype = {
  28047. items: null,
  28048. uiName: "menu",
  28049. initMenu: function() {
  28050. this.items = this.items || [];
  28051. this.initPopup();
  28052. this.initItems();
  28053. },
  28054. initItems: function() {
  28055. for (var i = 0; i < this.items.length; i++) {
  28056. var item = this.items[i];
  28057. if (item == "-") {
  28058. this.items[i] = this.getSeparator();
  28059. } else if (!(item instanceof MenuItem)) {
  28060. item.editor = this.editor;
  28061. item.theme = this.editor.options.theme;
  28062. this.items[i] = this.createItem(item);
  28063. }
  28064. }
  28065. },
  28066. getSeparator: function() {
  28067. return menuSeparator;
  28068. },
  28069. createItem: function(item) {
  28070. //新增一个参数menu, 该参数存储了menuItem所对应的menu引用
  28071. item.menu = this;
  28072. return new MenuItem(item);
  28073. },
  28074. _Popup_getContentHtmlTpl: Popup.prototype.getContentHtmlTpl,
  28075. getContentHtmlTpl: function() {
  28076. if (this.items.length == 0) {
  28077. return this._Popup_getContentHtmlTpl();
  28078. }
  28079. var buff = [];
  28080. for (var i = 0; i < this.items.length; i++) {
  28081. var item = this.items[i];
  28082. buff[i] = item.renderHtml();
  28083. }
  28084. return '<div class="%%-body">' + buff.join("") + "</div>";
  28085. },
  28086. _Popup_postRender: Popup.prototype.postRender,
  28087. postRender: function() {
  28088. var me = this;
  28089. for (var i = 0; i < this.items.length; i++) {
  28090. var item = this.items[i];
  28091. item.ownerMenu = this;
  28092. item.postRender();
  28093. }
  28094. domUtils.on(this.getDom(), "mouseover", function(evt) {
  28095. evt = evt || event;
  28096. var rel = evt.relatedTarget || evt.fromElement;
  28097. var el = me.getDom();
  28098. if (!uiUtils.contains(el, rel) && el !== rel) {
  28099. me.fireEvent("over");
  28100. }
  28101. });
  28102. this._Popup_postRender();
  28103. },
  28104. queryAutoHide: function(el) {
  28105. if (el) {
  28106. if (uiUtils.contains(this.getDom(), el)) {
  28107. return false;
  28108. }
  28109. for (var i = 0; i < this.items.length; i++) {
  28110. var item = this.items[i];
  28111. if (item.queryAutoHide(el) === false) {
  28112. return false;
  28113. }
  28114. }
  28115. }
  28116. },
  28117. clearItems: function() {
  28118. for (var i = 0; i < this.items.length; i++) {
  28119. var item = this.items[i];
  28120. clearTimeout(item._showingTimer);
  28121. clearTimeout(item._closingTimer);
  28122. if (item.subMenu) {
  28123. item.subMenu.destroy();
  28124. }
  28125. }
  28126. this.items = [];
  28127. },
  28128. destroy: function() {
  28129. if (this.getDom()) {
  28130. domUtils.remove(this.getDom());
  28131. }
  28132. this.clearItems();
  28133. },
  28134. dispose: function() {
  28135. this.destroy();
  28136. }
  28137. };
  28138. utils.inherits(Menu, Popup);
  28139. /**
  28140. * @update 2013/04/03 hancong03 新增一个参数menu, 该参数存储了menuItem所对应的menu引用
  28141. * @type {Function}
  28142. */
  28143. var MenuItem = (baidu.editor.ui.MenuItem = function(options) {
  28144. this.initOptions(options);
  28145. this.initUIBase();
  28146. this.Stateful_init();
  28147. if (this.subMenu && !(this.subMenu instanceof Menu)) {
  28148. if (options.className && options.className.indexOf("aligntd") != -1) {
  28149. var me = this;
  28150. //获取单元格对齐初始状态
  28151. this.subMenu.selected = this.editor.queryCommandValue("cellalignment");
  28152. this.subMenu = new Popup({
  28153. content: new CellAlignPicker(this.subMenu),
  28154. parentMenu: me,
  28155. editor: me.editor,
  28156. destroy: function() {
  28157. if (this.getDom()) {
  28158. domUtils.remove(this.getDom());
  28159. }
  28160. }
  28161. });
  28162. this.subMenu.addListener("postRenderAfter", function() {
  28163. domUtils.on(this.getDom(), "mouseover", function() {
  28164. me.addState("opened");
  28165. });
  28166. });
  28167. } else {
  28168. this.subMenu = new Menu(this.subMenu);
  28169. }
  28170. }
  28171. });
  28172. MenuItem.prototype = {
  28173. label: "",
  28174. subMenu: null,
  28175. ownerMenu: null,
  28176. uiName: "menuitem",
  28177. alwalysHoverable: true,
  28178. getHtmlTpl: function() {
  28179. return (
  28180. '<div id="##" class="%%" stateful onclick="$$._onClick(event, this);">' +
  28181. '<div class="%%-body">' +
  28182. this.renderLabelHtml() +
  28183. "</div>" +
  28184. "</div>"
  28185. );
  28186. },
  28187. postRender: function() {
  28188. var me = this;
  28189. this.addListener("over", function() {
  28190. me.ownerMenu.fireEvent("submenuover", me);
  28191. if (me.subMenu) {
  28192. me.delayShowSubMenu();
  28193. }
  28194. });
  28195. if (this.subMenu) {
  28196. this.getDom().className += " edui-hassubmenu";
  28197. this.subMenu.render();
  28198. this.addListener("out", function() {
  28199. me.delayHideSubMenu();
  28200. });
  28201. this.subMenu.addListener("over", function() {
  28202. clearTimeout(me._closingTimer);
  28203. me._closingTimer = null;
  28204. me.addState("opened");
  28205. });
  28206. this.ownerMenu.addListener("hide", function() {
  28207. me.hideSubMenu();
  28208. });
  28209. this.ownerMenu.addListener("submenuover", function(t, subMenu) {
  28210. if (subMenu !== me) {
  28211. me.delayHideSubMenu();
  28212. }
  28213. });
  28214. this.subMenu._bakQueryAutoHide = this.subMenu.queryAutoHide;
  28215. this.subMenu.queryAutoHide = function(el) {
  28216. if (el && uiUtils.contains(me.getDom(), el)) {
  28217. return false;
  28218. }
  28219. return this._bakQueryAutoHide(el);
  28220. };
  28221. }
  28222. this.getDom().style.tabIndex = "-1";
  28223. uiUtils.makeUnselectable(this.getDom());
  28224. this.Stateful_postRender();
  28225. },
  28226. delayShowSubMenu: function() {
  28227. var me = this;
  28228. if (!me.isDisabled()) {
  28229. me.addState("opened");
  28230. clearTimeout(me._showingTimer);
  28231. clearTimeout(me._closingTimer);
  28232. me._closingTimer = null;
  28233. me._showingTimer = setTimeout(function() {
  28234. me.showSubMenu();
  28235. }, 250);
  28236. }
  28237. },
  28238. delayHideSubMenu: function() {
  28239. var me = this;
  28240. if (!me.isDisabled()) {
  28241. me.removeState("opened");
  28242. clearTimeout(me._showingTimer);
  28243. if (!me._closingTimer) {
  28244. me._closingTimer = setTimeout(function() {
  28245. if (!me.hasState("opened")) {
  28246. me.hideSubMenu();
  28247. }
  28248. me._closingTimer = null;
  28249. }, 400);
  28250. }
  28251. }
  28252. },
  28253. renderLabelHtml: function() {
  28254. return (
  28255. '<div class="edui-arrow"></div>' +
  28256. '<div class="edui-box edui-icon"></div>' +
  28257. '<div class="edui-box edui-label %%-label">' +
  28258. (this.label || "") +
  28259. "</div>"
  28260. );
  28261. },
  28262. getStateDom: function() {
  28263. return this.getDom();
  28264. },
  28265. queryAutoHide: function(el) {
  28266. if (this.subMenu && this.hasState("opened")) {
  28267. return this.subMenu.queryAutoHide(el);
  28268. }
  28269. },
  28270. _onClick: function(event, this_) {
  28271. if (this.hasState("disabled")) return;
  28272. if (this.fireEvent("click", event, this_) !== false) {
  28273. if (this.subMenu) {
  28274. this.showSubMenu();
  28275. } else {
  28276. Popup.postHide(event);
  28277. }
  28278. }
  28279. },
  28280. showSubMenu: function() {
  28281. var rect = uiUtils.getClientRect(this.getDom());
  28282. rect.right -= 5;
  28283. rect.left += 2;
  28284. rect.width -= 7;
  28285. rect.top -= 4;
  28286. rect.bottom += 4;
  28287. rect.height += 8;
  28288. this.subMenu.showAnchorRect(rect, true, true);
  28289. },
  28290. hideSubMenu: function() {
  28291. this.subMenu.hide();
  28292. }
  28293. };
  28294. utils.inherits(MenuItem, UIBase);
  28295. utils.extend(MenuItem.prototype, Stateful, true);
  28296. })();
  28297. // ui/combox.js
  28298. ///import core
  28299. ///import uicore
  28300. ///import ui/menu.js
  28301. ///import ui/splitbutton.js
  28302. (function() {
  28303. // todo: menu和item提成通用list
  28304. var utils = baidu.editor.utils,
  28305. uiUtils = baidu.editor.ui.uiUtils,
  28306. Menu = baidu.editor.ui.Menu,
  28307. SplitButton = baidu.editor.ui.SplitButton,
  28308. Combox = (baidu.editor.ui.Combox = function(options) {
  28309. this.initOptions(options);
  28310. this.initCombox();
  28311. });
  28312. Combox.prototype = {
  28313. uiName: "combox",
  28314. onbuttonclick: function() {
  28315. this.showPopup();
  28316. },
  28317. initCombox: function() {
  28318. var me = this;
  28319. this.items = this.items || [];
  28320. for (var i = 0; i < this.items.length; i++) {
  28321. var item = this.items[i];
  28322. item.uiName = "listitem";
  28323. item.index = i;
  28324. item.onclick = function() {
  28325. me.selectByIndex(this.index);
  28326. };
  28327. }
  28328. this.popup = new Menu({
  28329. items: this.items,
  28330. uiName: "list",
  28331. editor: this.editor,
  28332. captureWheel: true,
  28333. combox: this
  28334. });
  28335. this.initSplitButton();
  28336. },
  28337. _SplitButton_postRender: SplitButton.prototype.postRender,
  28338. postRender: function() {
  28339. this._SplitButton_postRender();
  28340. this.setLabel(this.label || "");
  28341. this.setValue(this.initValue || "");
  28342. },
  28343. showPopup: function() {
  28344. var rect = uiUtils.getClientRect(this.getDom());
  28345. rect.top += 1;
  28346. rect.bottom -= 1;
  28347. rect.height -= 2;
  28348. this.popup.showAnchorRect(rect);
  28349. },
  28350. getValue: function() {
  28351. return this.value;
  28352. },
  28353. setValue: function(value) {
  28354. var index = this.indexByValue(value);
  28355. if (index != -1) {
  28356. this.selectedIndex = index;
  28357. this.setLabel(this.items[index].label);
  28358. this.value = this.items[index].value;
  28359. } else {
  28360. this.selectedIndex = -1;
  28361. this.setLabel(this.getLabelForUnknowValue(value));
  28362. this.value = value;
  28363. }
  28364. },
  28365. setLabel: function(label) {
  28366. this.getDom("button_body").innerHTML = label;
  28367. this.label = label;
  28368. },
  28369. getLabelForUnknowValue: function(value) {
  28370. return value;
  28371. },
  28372. indexByValue: function(value) {
  28373. for (var i = 0; i < this.items.length; i++) {
  28374. if (value == this.items[i].value) {
  28375. return i;
  28376. }
  28377. }
  28378. return -1;
  28379. },
  28380. getItem: function(index) {
  28381. return this.items[index];
  28382. },
  28383. selectByIndex: function(index) {
  28384. if (
  28385. index < this.items.length &&
  28386. this.fireEvent("select", index) !== false
  28387. ) {
  28388. this.selectedIndex = index;
  28389. this.value = this.items[index].value;
  28390. this.setLabel(this.items[index].label);
  28391. }
  28392. }
  28393. };
  28394. utils.inherits(Combox, SplitButton);
  28395. })();
  28396. // ui/dialog.js
  28397. ///import core
  28398. ///import uicore
  28399. ///import ui/mask.js
  28400. ///import ui/button.js
  28401. (function() {
  28402. var utils = baidu.editor.utils,
  28403. domUtils = baidu.editor.dom.domUtils,
  28404. uiUtils = baidu.editor.ui.uiUtils,
  28405. Mask = baidu.editor.ui.Mask,
  28406. UIBase = baidu.editor.ui.UIBase,
  28407. Button = baidu.editor.ui.Button,
  28408. Dialog = (baidu.editor.ui.Dialog = function(options) {
  28409. if (options.name) {
  28410. var name = options.name;
  28411. var cssRules = options.cssRules;
  28412. if (!options.className) {
  28413. options.className = "edui-for-" + name;
  28414. }
  28415. if (cssRules) {
  28416. options.cssRules =
  28417. ".edui-for-" + name + " .edui-dialog-content {" + cssRules + "}";
  28418. }
  28419. }
  28420. this.initOptions(
  28421. utils.extend(
  28422. {
  28423. autoReset: true,
  28424. draggable: true,
  28425. onok: function() {},
  28426. oncancel: function() {},
  28427. onclose: function(t, ok) {
  28428. return ok ? this.onok() : this.oncancel();
  28429. },
  28430. //是否控制dialog中的scroll事件, 默认为不阻止
  28431. holdScroll: false
  28432. },
  28433. options
  28434. )
  28435. );
  28436. this.initDialog();
  28437. });
  28438. var modalMask;
  28439. var dragMask;
  28440. var activeDialog;
  28441. Dialog.prototype = {
  28442. draggable: false,
  28443. uiName: "dialog",
  28444. initDialog: function() {
  28445. var me = this,
  28446. theme = this.editor.options.theme;
  28447. if (this.cssRules) {
  28448. this.cssRules = ".edui-" + theme + " " + this.cssRules;
  28449. utils.cssRule("edui-customize-" + this.name + "-style", this.cssRules);
  28450. }
  28451. this.initUIBase();
  28452. this.modalMask =
  28453. modalMask ||
  28454. (modalMask = new Mask({
  28455. className: "edui-dialog-modalmask",
  28456. theme: theme,
  28457. onclick: function() {
  28458. activeDialog && activeDialog.close(false);
  28459. }
  28460. }));
  28461. this.dragMask =
  28462. dragMask ||
  28463. (dragMask = new Mask({
  28464. className: "edui-dialog-dragmask",
  28465. theme: theme
  28466. }));
  28467. this.closeButton = new Button({
  28468. className: "edui-dialog-closebutton",
  28469. title: me.closeDialog,
  28470. theme: theme,
  28471. onclick: function() {
  28472. me.close(false);
  28473. }
  28474. });
  28475. this.fullscreen && this.initResizeEvent();
  28476. if (this.buttons) {
  28477. for (var i = 0; i < this.buttons.length; i++) {
  28478. if (!(this.buttons[i] instanceof Button)) {
  28479. this.buttons[i] = new Button(
  28480. utils.extend(
  28481. this.buttons[i],
  28482. {
  28483. editor: this.editor
  28484. },
  28485. true
  28486. )
  28487. );
  28488. }
  28489. }
  28490. }
  28491. },
  28492. initResizeEvent: function() {
  28493. var me = this;
  28494. domUtils.on(window, "resize", function() {
  28495. if (me._hidden || me._hidden === undefined) {
  28496. return;
  28497. }
  28498. if (me.__resizeTimer) {
  28499. window.clearTimeout(me.__resizeTimer);
  28500. }
  28501. me.__resizeTimer = window.setTimeout(function() {
  28502. me.__resizeTimer = null;
  28503. var dialogWrapNode = me.getDom(),
  28504. contentNode = me.getDom("content"),
  28505. wrapRect = UE.ui.uiUtils.getClientRect(dialogWrapNode),
  28506. contentRect = UE.ui.uiUtils.getClientRect(contentNode),
  28507. vpRect = uiUtils.getViewportRect();
  28508. contentNode.style.width =
  28509. vpRect.width - wrapRect.width + contentRect.width + "px";
  28510. contentNode.style.height =
  28511. vpRect.height - wrapRect.height + contentRect.height + "px";
  28512. dialogWrapNode.style.width = vpRect.width + "px";
  28513. dialogWrapNode.style.height = vpRect.height + "px";
  28514. me.fireEvent("resize");
  28515. }, 100);
  28516. });
  28517. },
  28518. fitSize: function() {
  28519. var popBodyEl = this.getDom("body");
  28520. // if (!(baidu.editor.browser.ie && baidu.editor.browser.version == 7)) {
  28521. // uiUtils.removeStyle(popBodyEl, 'width');
  28522. // uiUtils.removeStyle(popBodyEl, 'height');
  28523. // }
  28524. var size = this.mesureSize();
  28525. popBodyEl.style.width = size.width + "px";
  28526. popBodyEl.style.height = size.height + "px";
  28527. return size;
  28528. },
  28529. safeSetOffset: function(offset) {
  28530. var me = this;
  28531. var el = me.getDom();
  28532. var vpRect = uiUtils.getViewportRect();
  28533. var rect = uiUtils.getClientRect(el);
  28534. var left = offset.left;
  28535. if (left + rect.width > vpRect.right) {
  28536. left = vpRect.right - rect.width;
  28537. }
  28538. var top = offset.top;
  28539. if (top + rect.height > vpRect.bottom) {
  28540. top = vpRect.bottom - rect.height;
  28541. }
  28542. el.style.left = Math.max(left, 0) + "px";
  28543. el.style.top = Math.max(top, 0) + "px";
  28544. },
  28545. showAtCenter: function() {
  28546. var vpRect = uiUtils.getViewportRect();
  28547. if (!this.fullscreen) {
  28548. this.getDom().style.display = "";
  28549. var popSize = this.fitSize();
  28550. var titleHeight = this.getDom("titlebar").offsetHeight | 0;
  28551. var left = vpRect.width / 2 - popSize.width / 2;
  28552. var top =
  28553. vpRect.height / 2 - (popSize.height - titleHeight) / 2 - titleHeight;
  28554. var popEl = this.getDom();
  28555. this.safeSetOffset({
  28556. left: Math.max(left | 0, 0),
  28557. top: Math.max(top | 0, 0)
  28558. });
  28559. if (!domUtils.hasClass(popEl, "edui-state-centered")) {
  28560. popEl.className += " edui-state-centered";
  28561. }
  28562. } else {
  28563. var dialogWrapNode = this.getDom(),
  28564. contentNode = this.getDom("content");
  28565. dialogWrapNode.style.display = "block";
  28566. var wrapRect = UE.ui.uiUtils.getClientRect(dialogWrapNode),
  28567. contentRect = UE.ui.uiUtils.getClientRect(contentNode);
  28568. dialogWrapNode.style.left = "-100000px";
  28569. contentNode.style.width =
  28570. vpRect.width - wrapRect.width + contentRect.width + "px";
  28571. contentNode.style.height =
  28572. vpRect.height - wrapRect.height + contentRect.height + "px";
  28573. dialogWrapNode.style.width = vpRect.width + "px";
  28574. dialogWrapNode.style.height = vpRect.height + "px";
  28575. dialogWrapNode.style.left = 0;
  28576. //保存环境的overflow值
  28577. this._originalContext = {
  28578. html: {
  28579. overflowX: document.documentElement.style.overflowX,
  28580. overflowY: document.documentElement.style.overflowY
  28581. },
  28582. body: {
  28583. overflowX: document.body.style.overflowX,
  28584. overflowY: document.body.style.overflowY
  28585. }
  28586. };
  28587. document.documentElement.style.overflowX = "hidden";
  28588. document.documentElement.style.overflowY = "hidden";
  28589. document.body.style.overflowX = "hidden";
  28590. document.body.style.overflowY = "hidden";
  28591. }
  28592. this._show();
  28593. },
  28594. getContentHtml: function() {
  28595. var contentHtml = "";
  28596. if (typeof this.content == "string") {
  28597. contentHtml = this.content;
  28598. } else if (this.iframeUrl) {
  28599. contentHtml =
  28600. '<span id="' +
  28601. this.id +
  28602. '_contmask" class="dialogcontmask"></span><iframe id="' +
  28603. this.id +
  28604. '_iframe" class="%%-iframe" height="100%" width="100%" frameborder="0" src="' +
  28605. this.iframeUrl +
  28606. '"></iframe>';
  28607. }
  28608. return contentHtml;
  28609. },
  28610. getHtmlTpl: function() {
  28611. var footHtml = "";
  28612. if (this.buttons) {
  28613. var buff = [];
  28614. for (var i = 0; i < this.buttons.length; i++) {
  28615. buff[i] = this.buttons[i].renderHtml();
  28616. }
  28617. footHtml =
  28618. '<div class="%%-foot">' +
  28619. '<div id="##_buttons" class="%%-buttons">' +
  28620. buff.join("") +
  28621. "</div>" +
  28622. "</div>";
  28623. }
  28624. return (
  28625. '<div id="##" class="%%"><div ' +
  28626. (!this.fullscreen
  28627. ? 'class="%%"'
  28628. : 'class="%%-wrap edui-dialog-fullscreen-flag"') +
  28629. '><div id="##_body" class="%%-body">' +
  28630. '<div class="%%-shadow"></div>' +
  28631. '<div id="##_titlebar" class="%%-titlebar">' +
  28632. '<div class="%%-draghandle" onmousedown="$$._onTitlebarMouseDown(event, this);">' +
  28633. '<span class="%%-caption">' +
  28634. (this.title || "") +
  28635. "</span>" +
  28636. "</div>" +
  28637. this.closeButton.renderHtml() +
  28638. "</div>" +
  28639. '<div id="##_content" class="%%-content">' +
  28640. (this.autoReset ? "" : this.getContentHtml()) +
  28641. "</div>" +
  28642. footHtml +
  28643. "</div></div></div>"
  28644. );
  28645. },
  28646. postRender: function() {
  28647. // todo: 保持居中/记住上次关闭位置选项
  28648. if (!this.modalMask.getDom()) {
  28649. this.modalMask.render();
  28650. this.modalMask.hide();
  28651. }
  28652. if (!this.dragMask.getDom()) {
  28653. this.dragMask.render();
  28654. this.dragMask.hide();
  28655. }
  28656. var me = this;
  28657. this.addListener("show", function() {
  28658. me.modalMask.show(this.getDom().style.zIndex - 2);
  28659. });
  28660. this.addListener("hide", function() {
  28661. me.modalMask.hide();
  28662. });
  28663. if (this.buttons) {
  28664. for (var i = 0; i < this.buttons.length; i++) {
  28665. this.buttons[i].postRender();
  28666. }
  28667. }
  28668. domUtils.on(window, "resize", function() {
  28669. setTimeout(function() {
  28670. if (!me.isHidden()) {
  28671. me.safeSetOffset(uiUtils.getClientRect(me.getDom()));
  28672. }
  28673. });
  28674. });
  28675. //hold住scroll事件,防止dialog的滚动影响页面
  28676. // if( this.holdScroll ) {
  28677. //
  28678. // if( !me.iframeUrl ) {
  28679. // domUtils.on( document.getElementById( me.id + "_iframe"), !browser.gecko ? "mousewheel" : "DOMMouseScroll", function(e){
  28680. // domUtils.preventDefault(e);
  28681. // } );
  28682. // } else {
  28683. // me.addListener('dialogafterreset', function(){
  28684. // window.setTimeout(function(){
  28685. // var iframeWindow = document.getElementById( me.id + "_iframe").contentWindow;
  28686. //
  28687. // if( browser.ie ) {
  28688. //
  28689. // var timer = window.setInterval(function(){
  28690. //
  28691. // if( iframeWindow.document && iframeWindow.document.body ) {
  28692. // window.clearInterval( timer );
  28693. // timer = null;
  28694. // domUtils.on( iframeWindow.document.body, !browser.gecko ? "mousewheel" : "DOMMouseScroll", function(e){
  28695. // domUtils.preventDefault(e);
  28696. // } );
  28697. // }
  28698. //
  28699. // }, 100);
  28700. //
  28701. // } else {
  28702. // domUtils.on( iframeWindow, !browser.gecko ? "mousewheel" : "DOMMouseScroll", function(e){
  28703. // domUtils.preventDefault(e);
  28704. // } );
  28705. // }
  28706. //
  28707. // }, 1);
  28708. // });
  28709. // }
  28710. //
  28711. // }
  28712. this._hide();
  28713. },
  28714. mesureSize: function() {
  28715. var body = this.getDom("body");
  28716. var width = uiUtils.getClientRect(this.getDom("content")).width;
  28717. var dialogBodyStyle = body.style;
  28718. dialogBodyStyle.width = width;
  28719. return uiUtils.getClientRect(body);
  28720. },
  28721. _onTitlebarMouseDown: function(evt, el) {
  28722. if (this.draggable) {
  28723. var rect;
  28724. var vpRect = uiUtils.getViewportRect();
  28725. var me = this;
  28726. uiUtils.startDrag(evt, {
  28727. ondragstart: function() {
  28728. rect = uiUtils.getClientRect(me.getDom());
  28729. me.getDom("contmask").style.visibility = "visible";
  28730. me.dragMask.show(me.getDom().style.zIndex - 1);
  28731. },
  28732. ondragmove: function(x, y) {
  28733. var left = rect.left + x;
  28734. var top = rect.top + y;
  28735. me.safeSetOffset({
  28736. left: left,
  28737. top: top
  28738. });
  28739. },
  28740. ondragstop: function() {
  28741. me.getDom("contmask").style.visibility = "hidden";
  28742. domUtils.removeClasses(me.getDom(), ["edui-state-centered"]);
  28743. me.dragMask.hide();
  28744. }
  28745. });
  28746. }
  28747. },
  28748. reset: function() {
  28749. this.getDom("content").innerHTML = this.getContentHtml();
  28750. this.fireEvent("dialogafterreset");
  28751. },
  28752. _show: function() {
  28753. if (this._hidden) {
  28754. this.getDom().style.display = "";
  28755. //要高过编辑器的zindxe
  28756. this.editor.container.style.zIndex &&
  28757. (this.getDom().style.zIndex =
  28758. this.editor.container.style.zIndex * 1 + 10);
  28759. this._hidden = false;
  28760. this.fireEvent("show");
  28761. baidu.editor.ui.uiUtils.getFixedLayer().style.zIndex =
  28762. this.getDom().style.zIndex - 4;
  28763. }
  28764. },
  28765. isHidden: function() {
  28766. return this._hidden;
  28767. },
  28768. _hide: function() {
  28769. if (!this._hidden) {
  28770. var wrapNode = this.getDom();
  28771. wrapNode.style.display = "none";
  28772. wrapNode.style.zIndex = "";
  28773. wrapNode.style.width = "";
  28774. wrapNode.style.height = "";
  28775. this._hidden = true;
  28776. this.fireEvent("hide");
  28777. }
  28778. },
  28779. open: function() {
  28780. if (this.autoReset) {
  28781. //有可能还没有渲染
  28782. try {
  28783. this.reset();
  28784. } catch (e) {
  28785. this.render();
  28786. this.open();
  28787. }
  28788. }
  28789. this.showAtCenter();
  28790. if (this.iframeUrl) {
  28791. try {
  28792. this.getDom("iframe").focus();
  28793. } catch (ex) {}
  28794. }
  28795. activeDialog = this;
  28796. },
  28797. _onCloseButtonClick: function(evt, el) {
  28798. this.close(false);
  28799. },
  28800. close: function(ok) {
  28801. if (this.fireEvent("close", ok) !== false) {
  28802. //还原环境
  28803. if (this.fullscreen) {
  28804. document.documentElement.style.overflowX = this._originalContext.html.overflowX;
  28805. document.documentElement.style.overflowY = this._originalContext.html.overflowY;
  28806. document.body.style.overflowX = this._originalContext.body.overflowX;
  28807. document.body.style.overflowY = this._originalContext.body.overflowY;
  28808. delete this._originalContext;
  28809. }
  28810. this._hide();
  28811. //销毁content
  28812. var content = this.getDom("content");
  28813. var iframe = this.getDom("iframe");
  28814. if (content && iframe) {
  28815. var doc = iframe.contentDocument || iframe.contentWindow.document;
  28816. doc && (doc.body.innerHTML = "");
  28817. domUtils.remove(content);
  28818. }
  28819. }
  28820. }
  28821. };
  28822. utils.inherits(Dialog, UIBase);
  28823. })();
  28824. // ui/menubutton.js
  28825. ///import core
  28826. ///import uicore
  28827. ///import ui/menu.js
  28828. ///import ui/splitbutton.js
  28829. (function() {
  28830. var utils = baidu.editor.utils,
  28831. Menu = baidu.editor.ui.Menu,
  28832. SplitButton = baidu.editor.ui.SplitButton,
  28833. MenuButton = (baidu.editor.ui.MenuButton = function(options) {
  28834. this.initOptions(options);
  28835. this.initMenuButton();
  28836. });
  28837. MenuButton.prototype = {
  28838. initMenuButton: function() {
  28839. var me = this;
  28840. this.uiName = "menubutton";
  28841. this.popup = new Menu({
  28842. items: me.items,
  28843. className: me.className,
  28844. editor: me.editor
  28845. });
  28846. this.popup.addListener("show", function() {
  28847. var list = this;
  28848. for (var i = 0; i < list.items.length; i++) {
  28849. list.items[i].removeState("checked");
  28850. if (list.items[i].value == me._value) {
  28851. list.items[i].addState("checked");
  28852. this.value = me._value;
  28853. }
  28854. }
  28855. });
  28856. this.initSplitButton();
  28857. },
  28858. setValue: function(value) {
  28859. this._value = value;
  28860. }
  28861. };
  28862. utils.inherits(MenuButton, SplitButton);
  28863. })();
  28864. // ui/multiMenu.js
  28865. ///import core
  28866. ///import uicore
  28867. ///commands 表情
  28868. (function() {
  28869. var utils = baidu.editor.utils,
  28870. Popup = baidu.editor.ui.Popup,
  28871. SplitButton = baidu.editor.ui.SplitButton,
  28872. MultiMenuPop = (baidu.editor.ui.MultiMenuPop = function(options) {
  28873. this.initOptions(options);
  28874. this.initMultiMenu();
  28875. });
  28876. MultiMenuPop.prototype = {
  28877. initMultiMenu: function() {
  28878. var me = this;
  28879. this.popup = new Popup({
  28880. content: "",
  28881. editor: me.editor,
  28882. iframe_rendered: false,
  28883. onshow: function() {
  28884. if (!this.iframe_rendered) {
  28885. this.iframe_rendered = true;
  28886. this.getDom("content").innerHTML =
  28887. '<iframe id="' +
  28888. me.id +
  28889. '_iframe" src="' +
  28890. me.iframeUrl +
  28891. '" frameborder="0"></iframe>';
  28892. me.editor.container.style.zIndex &&
  28893. (this.getDom().style.zIndex =
  28894. me.editor.container.style.zIndex * 1 + 1);
  28895. }
  28896. }
  28897. // canSideUp:false,
  28898. // canSideLeft:false
  28899. });
  28900. this.onbuttonclick = function() {
  28901. this.showPopup();
  28902. };
  28903. this.initSplitButton();
  28904. }
  28905. };
  28906. utils.inherits(MultiMenuPop, SplitButton);
  28907. })();
  28908. // ui/shortcutmenu.js
  28909. (function() {
  28910. var UI = baidu.editor.ui,
  28911. UIBase = UI.UIBase,
  28912. uiUtils = UI.uiUtils,
  28913. utils = baidu.editor.utils,
  28914. domUtils = baidu.editor.dom.domUtils;
  28915. var allMenus = [], //存储所有快捷菜单
  28916. timeID,
  28917. isSubMenuShow = false; //是否有子pop显示
  28918. var ShortCutMenu = (UI.ShortCutMenu = function(options) {
  28919. this.initOptions(options);
  28920. this.initShortCutMenu();
  28921. });
  28922. ShortCutMenu.postHide = hideAllMenu;
  28923. ShortCutMenu.prototype = {
  28924. isHidden: true,
  28925. SPACE: 5,
  28926. initShortCutMenu: function() {
  28927. this.items = this.items || [];
  28928. this.initUIBase();
  28929. this.initItems();
  28930. this.initEvent();
  28931. allMenus.push(this);
  28932. },
  28933. initEvent: function() {
  28934. var me = this,
  28935. doc = me.editor.document;
  28936. domUtils.on(doc, "mousemove", function(e) {
  28937. if (me.isHidden === false) {
  28938. //有pop显示就不隐藏快捷菜单
  28939. if (me.getSubMenuMark() || me.eventType == "contextmenu") return;
  28940. var flag = true,
  28941. el = me.getDom(),
  28942. wt = el.offsetWidth,
  28943. ht = el.offsetHeight,
  28944. distanceX = wt / 2 + me.SPACE, //距离中心X标准
  28945. distanceY = ht / 2, //距离中心Y标准
  28946. x = Math.abs(e.screenX - me.left), //离中心距离横坐标
  28947. y = Math.abs(e.screenY - me.top); //离中心距离纵坐标
  28948. clearTimeout(timeID);
  28949. timeID = setTimeout(function() {
  28950. if (y > 0 && y < distanceY) {
  28951. me.setOpacity(el, "1");
  28952. } else if (y > distanceY && y < distanceY + 70) {
  28953. me.setOpacity(el, "0.5");
  28954. flag = false;
  28955. } else if (y > distanceY + 70 && y < distanceY + 140) {
  28956. me.hide();
  28957. }
  28958. if (flag && x > 0 && x < distanceX) {
  28959. me.setOpacity(el, "1");
  28960. } else if (x > distanceX && x < distanceX + 70) {
  28961. me.setOpacity(el, "0.5");
  28962. } else if (x > distanceX + 70 && x < distanceX + 140) {
  28963. me.hide();
  28964. }
  28965. });
  28966. }
  28967. });
  28968. //ie\ff下 mouseout不准
  28969. if (browser.chrome) {
  28970. domUtils.on(doc, "mouseout", function(e) {
  28971. var relatedTgt = e.relatedTarget || e.toElement;
  28972. if (relatedTgt == null || relatedTgt.tagName == "HTML") {
  28973. me.hide();
  28974. }
  28975. });
  28976. }
  28977. me.editor.addListener("afterhidepop", function() {
  28978. if (!me.isHidden) {
  28979. isSubMenuShow = true;
  28980. }
  28981. });
  28982. },
  28983. initItems: function() {
  28984. if (utils.isArray(this.items)) {
  28985. for (var i = 0, len = this.items.length; i < len; i++) {
  28986. var item = this.items[i].toLowerCase();
  28987. if (UI[item]) {
  28988. this.items[i] = new UI[item](this.editor);
  28989. this.items[i].className += " edui-shortcutsubmenu ";
  28990. }
  28991. }
  28992. }
  28993. },
  28994. setOpacity: function(el, value) {
  28995. if (browser.ie && browser.version < 9) {
  28996. el.style.filter = "alpha(opacity = " + parseFloat(value) * 100 + ");";
  28997. } else {
  28998. el.style.opacity = value;
  28999. }
  29000. },
  29001. getSubMenuMark: function() {
  29002. isSubMenuShow = false;
  29003. var layerEle = uiUtils.getFixedLayer();
  29004. var list = domUtils.getElementsByTagName(layerEle, "div", function(node) {
  29005. return domUtils.hasClass(node, "edui-shortcutsubmenu edui-popup");
  29006. });
  29007. for (var i = 0, node; (node = list[i++]); ) {
  29008. if (node.style.display != "none") {
  29009. isSubMenuShow = true;
  29010. }
  29011. }
  29012. return isSubMenuShow;
  29013. },
  29014. show: function(e, hasContextmenu) {
  29015. var me = this,
  29016. offset = {},
  29017. el = this.getDom(),
  29018. fixedlayer = uiUtils.getFixedLayer();
  29019. function setPos(offset) {
  29020. if (offset.left < 0) {
  29021. offset.left = 0;
  29022. }
  29023. if (offset.top < 0) {
  29024. offset.top = 0;
  29025. }
  29026. el.style.cssText =
  29027. "position:absolute;left:" +
  29028. offset.left +
  29029. "px;top:" +
  29030. offset.top +
  29031. "px;";
  29032. }
  29033. function setPosByCxtMenu(menu) {
  29034. if (!menu.tagName) {
  29035. menu = menu.getDom();
  29036. }
  29037. offset.left = parseInt(menu.style.left);
  29038. offset.top = parseInt(menu.style.top);
  29039. offset.top -= el.offsetHeight + 15;
  29040. setPos(offset);
  29041. }
  29042. me.eventType = e.type;
  29043. el.style.cssText = "display:block;left:-9999px";
  29044. if (e.type == "contextmenu" && hasContextmenu) {
  29045. var menu = domUtils.getElementsByTagName(
  29046. fixedlayer,
  29047. "div",
  29048. "edui-contextmenu"
  29049. )[0];
  29050. if (menu) {
  29051. setPosByCxtMenu(menu);
  29052. } else {
  29053. me.editor.addListener("aftershowcontextmenu", function(type, menu) {
  29054. setPosByCxtMenu(menu);
  29055. });
  29056. }
  29057. } else {
  29058. offset = uiUtils.getViewportOffsetByEvent(e);
  29059. offset.top -= el.offsetHeight + me.SPACE;
  29060. offset.left += me.SPACE + 20;
  29061. setPos(offset);
  29062. me.setOpacity(el, 0.2);
  29063. }
  29064. me.isHidden = false;
  29065. me.left = e.screenX + el.offsetWidth / 2 - me.SPACE;
  29066. me.top = e.screenY - el.offsetHeight / 2 - me.SPACE;
  29067. if (me.editor) {
  29068. el.style.zIndex = me.editor.container.style.zIndex * 1 + 10;
  29069. fixedlayer.style.zIndex = el.style.zIndex - 1;
  29070. }
  29071. },
  29072. hide: function() {
  29073. if (this.getDom()) {
  29074. this.getDom().style.display = "none";
  29075. }
  29076. this.isHidden = true;
  29077. },
  29078. postRender: function() {
  29079. if (utils.isArray(this.items)) {
  29080. for (var i = 0, item; (item = this.items[i++]); ) {
  29081. item.postRender();
  29082. }
  29083. }
  29084. },
  29085. getHtmlTpl: function() {
  29086. var buff;
  29087. if (utils.isArray(this.items)) {
  29088. buff = [];
  29089. for (var i = 0; i < this.items.length; i++) {
  29090. buff[i] = this.items[i].renderHtml();
  29091. }
  29092. buff = buff.join("");
  29093. } else {
  29094. buff = this.items;
  29095. }
  29096. return (
  29097. '<div id="##" class="%% edui-toolbar" data-src="shortcutmenu" onmousedown="return false;" onselectstart="return false;" >' +
  29098. buff +
  29099. "</div>"
  29100. );
  29101. }
  29102. };
  29103. utils.inherits(ShortCutMenu, UIBase);
  29104. function hideAllMenu(e) {
  29105. var tgt = e.target || e.srcElement,
  29106. cur = domUtils.findParent(
  29107. tgt,
  29108. function(node) {
  29109. return (
  29110. domUtils.hasClass(node, "edui-shortcutmenu") ||
  29111. domUtils.hasClass(node, "edui-popup")
  29112. );
  29113. },
  29114. true
  29115. );
  29116. if (!cur) {
  29117. for (var i = 0, menu; (menu = allMenus[i++]); ) {
  29118. menu.hide();
  29119. }
  29120. }
  29121. }
  29122. domUtils.on(document, "mousedown", function(e) {
  29123. hideAllMenu(e);
  29124. });
  29125. domUtils.on(window, "scroll", function(e) {
  29126. hideAllMenu(e);
  29127. });
  29128. })();
  29129. // ui/breakline.js
  29130. (function() {
  29131. var utils = baidu.editor.utils,
  29132. UIBase = baidu.editor.ui.UIBase,
  29133. Breakline = (baidu.editor.ui.Breakline = function(options) {
  29134. this.initOptions(options);
  29135. this.initSeparator();
  29136. });
  29137. Breakline.prototype = {
  29138. uiName: "Breakline",
  29139. initSeparator: function() {
  29140. this.initUIBase();
  29141. },
  29142. getHtmlTpl: function() {
  29143. return "<br/>";
  29144. }
  29145. };
  29146. utils.inherits(Breakline, UIBase);
  29147. })();
  29148. // ui/message.js
  29149. ///import core
  29150. ///import uicore
  29151. (function() {
  29152. var utils = baidu.editor.utils,
  29153. domUtils = baidu.editor.dom.domUtils,
  29154. UIBase = baidu.editor.ui.UIBase,
  29155. Message = (baidu.editor.ui.Message = function(options) {
  29156. this.initOptions(options);
  29157. this.initMessage();
  29158. });
  29159. Message.prototype = {
  29160. initMessage: function() {
  29161. this.initUIBase();
  29162. },
  29163. getHtmlTpl: function() {
  29164. return (
  29165. '<div id="##" class="edui-message %%">' +
  29166. ' <div id="##_closer" class="edui-message-closer">×</div>' +
  29167. ' <div id="##_body" class="edui-message-body edui-message-type-info">' +
  29168. ' <iframe style="position:absolute;z-index:-1;left:0;top:0;background-color: transparent;" frameborder="0" width="100%" height="100%" src="about:blank"></iframe>' +
  29169. ' <div class="edui-shadow"></div>' +
  29170. ' <div id="##_content" class="edui-message-content">' +
  29171. " </div>" +
  29172. " </div>" +
  29173. "</div>"
  29174. );
  29175. },
  29176. reset: function(opt) {
  29177. var me = this;
  29178. if (!opt.keepshow) {
  29179. clearTimeout(this.timer);
  29180. me.timer = setTimeout(function() {
  29181. me.hide();
  29182. }, opt.timeout || 4000);
  29183. }
  29184. opt.content !== undefined && me.setContent(opt.content);
  29185. opt.type !== undefined && me.setType(opt.type);
  29186. me.show();
  29187. },
  29188. postRender: function() {
  29189. var me = this,
  29190. closer = this.getDom("closer");
  29191. closer &&
  29192. domUtils.on(closer, "click", function() {
  29193. me.hide();
  29194. });
  29195. },
  29196. setContent: function(content) {
  29197. this.getDom("content").innerHTML = content;
  29198. },
  29199. setType: function(type) {
  29200. type = type || "info";
  29201. var body = this.getDom("body");
  29202. body.className = body.className.replace(
  29203. /edui-message-type-[\w-]+/,
  29204. "edui-message-type-" + type
  29205. );
  29206. },
  29207. getContent: function() {
  29208. return this.getDom("content").innerHTML;
  29209. },
  29210. getType: function() {
  29211. var arr = this.getDom("body").match(/edui-message-type-([\w-]+)/);
  29212. return arr ? arr[1] : "";
  29213. },
  29214. show: function() {
  29215. this.getDom().style.display = "block";
  29216. },
  29217. hide: function() {
  29218. var dom = this.getDom();
  29219. if (dom) {
  29220. dom.style.display = "none";
  29221. dom.parentNode && dom.parentNode.removeChild(dom);
  29222. }
  29223. }
  29224. };
  29225. utils.inherits(Message, UIBase);
  29226. })();
  29227. // adapter/editorui.js
  29228. //ui跟编辑器的适配層
  29229. //那个按钮弹出是dialog,是下拉筐等都是在这个js中配置
  29230. //自己写的ui也要在这里配置,放到baidu.editor.ui下边,当编辑器实例化的时候会根据ueditor.config中的toolbars找到相应的进行实例化
  29231. (function() {
  29232. var utils = baidu.editor.utils;
  29233. var editorui = baidu.editor.ui;
  29234. var _Dialog = editorui.Dialog;
  29235. editorui.buttons = {};
  29236. editorui.Dialog = function(options) {
  29237. var dialog = new _Dialog(options);
  29238. dialog.addListener("hide", function() {
  29239. if (dialog.editor) {
  29240. var editor = dialog.editor;
  29241. try {
  29242. if (browser.gecko) {
  29243. var y = editor.window.scrollY,
  29244. x = editor.window.scrollX;
  29245. editor.body.focus();
  29246. editor.window.scrollTo(x, y);
  29247. } else {
  29248. editor.focus();
  29249. }
  29250. } catch (ex) {}
  29251. }
  29252. });
  29253. return dialog;
  29254. };
  29255. var iframeUrlMap = {
  29256. anchor: "~/dialogs/anchor/anchor.html",
  29257. insertimage: "~/dialogs/image/image.html",
  29258. link: "~/dialogs/link/link.html",
  29259. spechars: "~/dialogs/spechars/spechars.html",
  29260. searchreplace: "~/dialogs/searchreplace/searchreplace.html",
  29261. map: "~/dialogs/map/map.html",
  29262. gmap: "~/dialogs/gmap/gmap.html",
  29263. insertvideo: "~/dialogs/video/video.html",
  29264. help: "~/dialogs/help/help.html",
  29265. preview: "~/dialogs/preview/preview.html",
  29266. emotion: "~/dialogs/emotion/emotion.html",
  29267. wordimage: "~/dialogs/wordimage/wordimage.html",
  29268. attachment: "~/dialogs/attachment/attachment.html",
  29269. insertframe: "~/dialogs/insertframe/insertframe.html",
  29270. edittip: "~/dialogs/table/edittip.html",
  29271. edittable: "~/dialogs/table/edittable.html",
  29272. edittd: "~/dialogs/table/edittd.html",
  29273. webapp: "~/dialogs/webapp/webapp.html",
  29274. snapscreen: "~/dialogs/snapscreen/snapscreen.html",
  29275. scrawl: "~/dialogs/scrawl/scrawl.html",
  29276. music: "~/dialogs/music/music.html",
  29277. template: "~/dialogs/template/template.html",
  29278. background: "~/dialogs/background/background.html",
  29279. charts: "~/dialogs/charts/charts.html"
  29280. };
  29281. //为工具栏添加按钮,以下都是统一的按钮触发命令,所以写在一起
  29282. var btnCmds = [
  29283. "undo",
  29284. "redo",
  29285. "formatmatch",
  29286. "bold",
  29287. "italic",
  29288. "underline",
  29289. "fontborder",
  29290. "touppercase",
  29291. "tolowercase",
  29292. "strikethrough",
  29293. "subscript",
  29294. "superscript",
  29295. "source",
  29296. "indent",
  29297. "outdent",
  29298. "blockquote",
  29299. "pasteplain",
  29300. "pagebreak",
  29301. "selectall",
  29302. "print",
  29303. "horizontal",
  29304. "removeformat",
  29305. "time",
  29306. "date",
  29307. "unlink",
  29308. "insertparagraphbeforetable",
  29309. "insertrow",
  29310. "insertcol",
  29311. "mergeright",
  29312. "mergedown",
  29313. "deleterow",
  29314. "deletecol",
  29315. "splittorows",
  29316. "splittocols",
  29317. "splittocells",
  29318. "mergecells",
  29319. "deletetable",
  29320. "drafts"
  29321. ];
  29322. for (var i = 0, ci; (ci = btnCmds[i++]); ) {
  29323. ci = ci.toLowerCase();
  29324. editorui[ci] = (function(cmd) {
  29325. return function(editor) {
  29326. var ui = new editorui.Button({
  29327. className: "edui-for-" + cmd,
  29328. title:
  29329. editor.options.labelMap[cmd] ||
  29330. editor.getLang("labelMap." + cmd) ||
  29331. "",
  29332. onclick: function() {
  29333. editor.execCommand(cmd);
  29334. },
  29335. theme: editor.options.theme,
  29336. showText: false
  29337. });
  29338. editorui.buttons[cmd] = ui;
  29339. editor.addListener("selectionchange", function(
  29340. type,
  29341. causeByUi,
  29342. uiReady
  29343. ) {
  29344. var state = editor.queryCommandState(cmd);
  29345. if (state == -1) {
  29346. ui.setDisabled(true);
  29347. ui.setChecked(false);
  29348. } else {
  29349. if (!uiReady) {
  29350. ui.setDisabled(false);
  29351. ui.setChecked(state);
  29352. }
  29353. }
  29354. });
  29355. return ui;
  29356. };
  29357. })(ci);
  29358. }
  29359. //清除文档
  29360. editorui.cleardoc = function(editor) {
  29361. var ui = new editorui.Button({
  29362. className: "edui-for-cleardoc",
  29363. title:
  29364. editor.options.labelMap.cleardoc ||
  29365. editor.getLang("labelMap.cleardoc") ||
  29366. "",
  29367. theme: editor.options.theme,
  29368. onclick: function() {
  29369. if (confirm(editor.getLang("confirmClear"))) {
  29370. editor.execCommand("cleardoc");
  29371. }
  29372. }
  29373. });
  29374. editorui.buttons["cleardoc"] = ui;
  29375. editor.addListener("selectionchange", function() {
  29376. ui.setDisabled(editor.queryCommandState("cleardoc") == -1);
  29377. });
  29378. return ui;
  29379. };
  29380. //排版,图片排版,文字方向
  29381. var typeset = {
  29382. justify: ["left", "right", "center", "justify"],
  29383. imagefloat: ["none", "left", "center", "right"],
  29384. directionality: ["ltr", "rtl"]
  29385. };
  29386. for (var p in typeset) {
  29387. (function(cmd, val) {
  29388. for (var i = 0, ci; (ci = val[i++]); ) {
  29389. (function(cmd2) {
  29390. editorui[cmd.replace("float", "") + cmd2] = function(editor) {
  29391. var ui = new editorui.Button({
  29392. className: "edui-for-" + cmd.replace("float", "") + cmd2,
  29393. title:
  29394. editor.options.labelMap[cmd.replace("float", "") + cmd2] ||
  29395. editor.getLang(
  29396. "labelMap." + cmd.replace("float", "") + cmd2
  29397. ) ||
  29398. "",
  29399. theme: editor.options.theme,
  29400. onclick: function() {
  29401. editor.execCommand(cmd, cmd2);
  29402. }
  29403. });
  29404. editorui.buttons[cmd] = ui;
  29405. editor.addListener("selectionchange", function(
  29406. type,
  29407. causeByUi,
  29408. uiReady
  29409. ) {
  29410. ui.setDisabled(editor.queryCommandState(cmd) == -1);
  29411. ui.setChecked(editor.queryCommandValue(cmd) == cmd2 && !uiReady);
  29412. });
  29413. return ui;
  29414. };
  29415. })(ci);
  29416. }
  29417. })(p, typeset[p]);
  29418. }
  29419. //字体颜色和背景颜色
  29420. for (var i = 0, ci; (ci = ["backcolor", "forecolor"][i++]); ) {
  29421. editorui[ci] = (function(cmd) {
  29422. return function(editor) {
  29423. var ui = new editorui.ColorButton({
  29424. className: "edui-for-" + cmd,
  29425. color: "default",
  29426. title:
  29427. editor.options.labelMap[cmd] ||
  29428. editor.getLang("labelMap." + cmd) ||
  29429. "",
  29430. editor: editor,
  29431. onpickcolor: function(t, color) {
  29432. editor.execCommand(cmd, color);
  29433. },
  29434. onpicknocolor: function() {
  29435. editor.execCommand(cmd, "default");
  29436. this.setColor("transparent");
  29437. this.color = "default";
  29438. },
  29439. onbuttonclick: function() {
  29440. editor.execCommand(cmd, this.color);
  29441. }
  29442. });
  29443. editorui.buttons[cmd] = ui;
  29444. editor.addListener("selectionchange", function() {
  29445. ui.setDisabled(editor.queryCommandState(cmd) == -1);
  29446. });
  29447. return ui;
  29448. };
  29449. })(ci);
  29450. }
  29451. var dialogBtns = {
  29452. noOk: ["searchreplace", "help", "spechars", "webapp", "preview"],
  29453. ok: [
  29454. "attachment",
  29455. "anchor",
  29456. "link",
  29457. "insertimage",
  29458. "map",
  29459. "gmap",
  29460. "insertframe",
  29461. "wordimage",
  29462. "insertvideo",
  29463. "insertframe",
  29464. "edittip",
  29465. "edittable",
  29466. "edittd",
  29467. "scrawl",
  29468. "template",
  29469. "music",
  29470. "background",
  29471. "charts"
  29472. ]
  29473. };
  29474. for (var p in dialogBtns) {
  29475. (function(type, vals) {
  29476. for (var i = 0, ci; (ci = vals[i++]); ) {
  29477. //todo opera下存在问题
  29478. if (browser.opera && ci === "searchreplace") {
  29479. continue;
  29480. }
  29481. (function(cmd) {
  29482. editorui[cmd] = function(editor, iframeUrl, title) {
  29483. iframeUrl =
  29484. iframeUrl ||
  29485. (editor.options.iframeUrlMap || {})[cmd] ||
  29486. iframeUrlMap[cmd];
  29487. title =
  29488. editor.options.labelMap[cmd] ||
  29489. editor.getLang("labelMap." + cmd) ||
  29490. "";
  29491. var dialog;
  29492. //没有iframeUrl不创建dialog
  29493. if (iframeUrl) {
  29494. dialog = new editorui.Dialog(
  29495. utils.extend(
  29496. {
  29497. iframeUrl: editor.ui.mapUrl(iframeUrl),
  29498. editor: editor,
  29499. className: "edui-for-" + cmd,
  29500. title: title,
  29501. holdScroll: cmd === "insertimage",
  29502. fullscreen: /charts|preview/.test(cmd),
  29503. closeDialog: editor.getLang("closeDialog")
  29504. },
  29505. type == "ok"
  29506. ? {
  29507. buttons: [
  29508. {
  29509. className: "edui-okbutton",
  29510. label: editor.getLang("ok"),
  29511. editor: editor,
  29512. onclick: function() {
  29513. dialog.close(true);
  29514. }
  29515. },
  29516. {
  29517. className: "edui-cancelbutton",
  29518. label: editor.getLang("cancel"),
  29519. editor: editor,
  29520. onclick: function() {
  29521. dialog.close(false);
  29522. }
  29523. }
  29524. ]
  29525. }
  29526. : {}
  29527. )
  29528. );
  29529. editor.ui._dialogs[cmd + "Dialog"] = dialog;
  29530. }
  29531. var ui = new editorui.Button({
  29532. className: "edui-for-" + cmd,
  29533. title: title,
  29534. onclick: function() {
  29535. if (dialog) {
  29536. switch (cmd) {
  29537. case "wordimage":
  29538. var images = editor.execCommand("wordimage");
  29539. if (images && images.length) {
  29540. dialog.render();
  29541. dialog.open();
  29542. }
  29543. break;
  29544. case "scrawl":
  29545. if (editor.queryCommandState("scrawl") != -1) {
  29546. dialog.render();
  29547. dialog.open();
  29548. }
  29549. break;
  29550. default:
  29551. dialog.render();
  29552. dialog.open();
  29553. }
  29554. }
  29555. },
  29556. theme: editor.options.theme,
  29557. disabled:
  29558. (cmd == "scrawl" && editor.queryCommandState("scrawl") == -1) ||
  29559. cmd == "charts"
  29560. });
  29561. editorui.buttons[cmd] = ui;
  29562. editor.addListener("selectionchange", function() {
  29563. //只存在于右键菜单而无工具栏按钮的ui不需要检测状态
  29564. var unNeedCheckState = { edittable: 1 };
  29565. if (cmd in unNeedCheckState) return;
  29566. var state = editor.queryCommandState(cmd);
  29567. if (ui.getDom()) {
  29568. ui.setDisabled(state == -1);
  29569. ui.setChecked(state);
  29570. }
  29571. });
  29572. return ui;
  29573. };
  29574. })(ci.toLowerCase());
  29575. }
  29576. })(p, dialogBtns[p]);
  29577. }
  29578. editorui.snapscreen = function(editor, iframeUrl, title) {
  29579. title =
  29580. editor.options.labelMap["snapscreen"] ||
  29581. editor.getLang("labelMap.snapscreen") ||
  29582. "";
  29583. var ui = new editorui.Button({
  29584. className: "edui-for-snapscreen",
  29585. title: title,
  29586. onclick: function() {
  29587. editor.execCommand("snapscreen");
  29588. },
  29589. theme: editor.options.theme
  29590. });
  29591. editorui.buttons["snapscreen"] = ui;
  29592. iframeUrl =
  29593. iframeUrl ||
  29594. (editor.options.iframeUrlMap || {})["snapscreen"] ||
  29595. iframeUrlMap["snapscreen"];
  29596. if (iframeUrl) {
  29597. var dialog = new editorui.Dialog({
  29598. iframeUrl: editor.ui.mapUrl(iframeUrl),
  29599. editor: editor,
  29600. className: "edui-for-snapscreen",
  29601. title: title,
  29602. buttons: [
  29603. {
  29604. className: "edui-okbutton",
  29605. label: editor.getLang("ok"),
  29606. editor: editor,
  29607. onclick: function() {
  29608. dialog.close(true);
  29609. }
  29610. },
  29611. {
  29612. className: "edui-cancelbutton",
  29613. label: editor.getLang("cancel"),
  29614. editor: editor,
  29615. onclick: function() {
  29616. dialog.close(false);
  29617. }
  29618. }
  29619. ]
  29620. });
  29621. dialog.render();
  29622. editor.ui._dialogs["snapscreenDialog"] = dialog;
  29623. }
  29624. editor.addListener("selectionchange", function() {
  29625. ui.setDisabled(editor.queryCommandState("snapscreen") == -1);
  29626. });
  29627. return ui;
  29628. };
  29629. editorui.insertcode = function(editor, list, title) {
  29630. list = editor.options["insertcode"] || [];
  29631. title =
  29632. editor.options.labelMap["insertcode"] ||
  29633. editor.getLang("labelMap.insertcode") ||
  29634. "";
  29635. // if (!list.length) return;
  29636. var items = [];
  29637. utils.each(list, function(key, val) {
  29638. items.push({
  29639. label: key,
  29640. value: val,
  29641. theme: editor.options.theme,
  29642. renderLabelHtml: function() {
  29643. return (
  29644. '<div class="edui-label %%-label" >' + (this.label || "") + "</div>"
  29645. );
  29646. }
  29647. });
  29648. });
  29649. var ui = new editorui.Combox({
  29650. editor: editor,
  29651. items: items,
  29652. onselect: function(t, index) {
  29653. editor.execCommand("insertcode", this.items[index].value);
  29654. },
  29655. onbuttonclick: function() {
  29656. this.showPopup();
  29657. },
  29658. title: title,
  29659. initValue: title,
  29660. className: "edui-for-insertcode",
  29661. indexByValue: function(value) {
  29662. if (value) {
  29663. for (var i = 0, ci; (ci = this.items[i]); i++) {
  29664. if (ci.value.indexOf(value) != -1) return i;
  29665. }
  29666. }
  29667. return -1;
  29668. }
  29669. });
  29670. editorui.buttons["insertcode"] = ui;
  29671. editor.addListener("selectionchange", function(type, causeByUi, uiReady) {
  29672. if (!uiReady) {
  29673. var state = editor.queryCommandState("insertcode");
  29674. if (state == -1) {
  29675. ui.setDisabled(true);
  29676. } else {
  29677. ui.setDisabled(false);
  29678. var value = editor.queryCommandValue("insertcode");
  29679. if (!value) {
  29680. ui.setValue(title);
  29681. return;
  29682. }
  29683. //trace:1871 ie下从源码模式切换回来时,字体会带单引号,而且会有逗号
  29684. value && (value = value.replace(/['"]/g, "").split(",")[0]);
  29685. ui.setValue(value);
  29686. }
  29687. }
  29688. });
  29689. return ui;
  29690. };
  29691. editorui.fontfamily = function(editor, list, title) {
  29692. list = editor.options["fontfamily"] || [];
  29693. title =
  29694. editor.options.labelMap["fontfamily"] ||
  29695. editor.getLang("labelMap.fontfamily") ||
  29696. "";
  29697. if (!list.length) return;
  29698. for (var i = 0, ci, items = []; (ci = list[i]); i++) {
  29699. var langLabel = editor.getLang("fontfamily")[ci.name] || "";
  29700. (function(key, val) {
  29701. items.push({
  29702. label: key,
  29703. value: val,
  29704. theme: editor.options.theme,
  29705. renderLabelHtml: function() {
  29706. return (
  29707. '<div class="edui-label %%-label" style="font-family:' +
  29708. utils.unhtml(this.value) +
  29709. '">' +
  29710. (this.label || "") +
  29711. "</div>"
  29712. );
  29713. }
  29714. });
  29715. })(ci.label || langLabel, ci.val);
  29716. }
  29717. var ui = new editorui.Combox({
  29718. editor: editor,
  29719. items: items,
  29720. onselect: function(t, index) {
  29721. editor.execCommand("FontFamily", this.items[index].value);
  29722. },
  29723. onbuttonclick: function() {
  29724. this.showPopup();
  29725. },
  29726. title: title,
  29727. initValue: title,
  29728. className: "edui-for-fontfamily",
  29729. indexByValue: function(value) {
  29730. if (value) {
  29731. for (var i = 0, ci; (ci = this.items[i]); i++) {
  29732. if (ci.value.indexOf(value) != -1) return i;
  29733. }
  29734. }
  29735. return -1;
  29736. }
  29737. });
  29738. editorui.buttons["fontfamily"] = ui;
  29739. editor.addListener("selectionchange", function(type, causeByUi, uiReady) {
  29740. if (!uiReady) {
  29741. var state = editor.queryCommandState("FontFamily");
  29742. if (state == -1) {
  29743. ui.setDisabled(true);
  29744. } else {
  29745. ui.setDisabled(false);
  29746. var value = editor.queryCommandValue("FontFamily");
  29747. //trace:1871 ie下从源码模式切换回来时,字体会带单引号,而且会有逗号
  29748. value && (value = value.replace(/['"]/g, "").split(",")[0]);
  29749. ui.setValue(value);
  29750. }
  29751. }
  29752. });
  29753. return ui;
  29754. };
  29755. editorui.fontsize = function(editor, list, title) {
  29756. title =
  29757. editor.options.labelMap["fontsize"] ||
  29758. editor.getLang("labelMap.fontsize") ||
  29759. "";
  29760. list = list || editor.options["fontsize"] || [];
  29761. if (!list.length) return;
  29762. var items = [];
  29763. for (var i = 0; i < list.length; i++) {
  29764. var size = list[i] + "px";
  29765. items.push({
  29766. label: size,
  29767. value: size,
  29768. theme: editor.options.theme,
  29769. renderLabelHtml: function() {
  29770. return (
  29771. '<div class="edui-label %%-label" style="line-height:1;font-size:' +
  29772. this.value +
  29773. '">' +
  29774. (this.label || "") +
  29775. "</div>"
  29776. );
  29777. }
  29778. });
  29779. }
  29780. var ui = new editorui.Combox({
  29781. editor: editor,
  29782. items: items,
  29783. title: title,
  29784. initValue: title,
  29785. onselect: function(t, index) {
  29786. editor.execCommand("FontSize", this.items[index].value);
  29787. },
  29788. onbuttonclick: function() {
  29789. this.showPopup();
  29790. },
  29791. className: "edui-for-fontsize"
  29792. });
  29793. editorui.buttons["fontsize"] = ui;
  29794. editor.addListener("selectionchange", function(type, causeByUi, uiReady) {
  29795. if (!uiReady) {
  29796. var state = editor.queryCommandState("FontSize");
  29797. if (state == -1) {
  29798. ui.setDisabled(true);
  29799. } else {
  29800. ui.setDisabled(false);
  29801. ui.setValue(editor.queryCommandValue("FontSize"));
  29802. }
  29803. }
  29804. });
  29805. return ui;
  29806. };
  29807. editorui.paragraph = function(editor, list, title) {
  29808. title =
  29809. editor.options.labelMap["paragraph"] ||
  29810. editor.getLang("labelMap.paragraph") ||
  29811. "";
  29812. list = editor.options["paragraph"] || [];
  29813. if (utils.isEmptyObject(list)) return;
  29814. var items = [];
  29815. for (var i in list) {
  29816. items.push({
  29817. value: i,
  29818. label: list[i] || editor.getLang("paragraph")[i],
  29819. theme: editor.options.theme,
  29820. renderLabelHtml: function() {
  29821. return (
  29822. '<div class="edui-label %%-label"><span class="edui-for-' +
  29823. this.value +
  29824. '">' +
  29825. (this.label || "") +
  29826. "</span></div>"
  29827. );
  29828. }
  29829. });
  29830. }
  29831. var ui = new editorui.Combox({
  29832. editor: editor,
  29833. items: items,
  29834. title: title,
  29835. initValue: title,
  29836. className: "edui-for-paragraph",
  29837. onselect: function(t, index) {
  29838. editor.execCommand("Paragraph", this.items[index].value);
  29839. },
  29840. onbuttonclick: function() {
  29841. this.showPopup();
  29842. }
  29843. });
  29844. editorui.buttons["paragraph"] = ui;
  29845. editor.addListener("selectionchange", function(type, causeByUi, uiReady) {
  29846. if (!uiReady) {
  29847. var state = editor.queryCommandState("Paragraph");
  29848. if (state == -1) {
  29849. ui.setDisabled(true);
  29850. } else {
  29851. ui.setDisabled(false);
  29852. var value = editor.queryCommandValue("Paragraph");
  29853. var index = ui.indexByValue(value);
  29854. if (index != -1) {
  29855. ui.setValue(value);
  29856. } else {
  29857. ui.setValue(ui.initValue);
  29858. }
  29859. }
  29860. }
  29861. });
  29862. return ui;
  29863. };
  29864. //自定义标题
  29865. editorui.customstyle = function(editor) {
  29866. var list = editor.options["customstyle"] || [],
  29867. title =
  29868. editor.options.labelMap["customstyle"] ||
  29869. editor.getLang("labelMap.customstyle") ||
  29870. "";
  29871. if (!list.length) return;
  29872. var langCs = editor.getLang("customstyle");
  29873. for (var i = 0, items = [], t; (t = list[i++]); ) {
  29874. (function(t) {
  29875. var ck = {};
  29876. ck.label = t.label ? t.label : langCs[t.name];
  29877. ck.style = t.style;
  29878. ck.className = t.className;
  29879. ck.tag = t.tag;
  29880. items.push({
  29881. label: ck.label,
  29882. value: ck,
  29883. theme: editor.options.theme,
  29884. renderLabelHtml: function() {
  29885. return (
  29886. '<div class="edui-label %%-label">' +
  29887. "<" +
  29888. ck.tag +
  29889. " " +
  29890. (ck.className ? ' class="' + ck.className + '"' : "") +
  29891. (ck.style ? ' style="' + ck.style + '"' : "") +
  29892. ">" +
  29893. ck.label +
  29894. "</" +
  29895. ck.tag +
  29896. ">" +
  29897. "</div>"
  29898. );
  29899. }
  29900. });
  29901. })(t);
  29902. }
  29903. var ui = new editorui.Combox({
  29904. editor: editor,
  29905. items: items,
  29906. title: title,
  29907. initValue: title,
  29908. className: "edui-for-customstyle",
  29909. onselect: function(t, index) {
  29910. editor.execCommand("customstyle", this.items[index].value);
  29911. },
  29912. onbuttonclick: function() {
  29913. this.showPopup();
  29914. },
  29915. indexByValue: function(value) {
  29916. for (var i = 0, ti; (ti = this.items[i++]); ) {
  29917. if (ti.label == value) {
  29918. return i - 1;
  29919. }
  29920. }
  29921. return -1;
  29922. }
  29923. });
  29924. editorui.buttons["customstyle"] = ui;
  29925. editor.addListener("selectionchange", function(type, causeByUi, uiReady) {
  29926. if (!uiReady) {
  29927. var state = editor.queryCommandState("customstyle");
  29928. if (state == -1) {
  29929. ui.setDisabled(true);
  29930. } else {
  29931. ui.setDisabled(false);
  29932. var value = editor.queryCommandValue("customstyle");
  29933. var index = ui.indexByValue(value);
  29934. if (index != -1) {
  29935. ui.setValue(value);
  29936. } else {
  29937. ui.setValue(ui.initValue);
  29938. }
  29939. }
  29940. }
  29941. });
  29942. return ui;
  29943. };
  29944. editorui.inserttable = function(editor, iframeUrl, title) {
  29945. title =
  29946. editor.options.labelMap["inserttable"] ||
  29947. editor.getLang("labelMap.inserttable") ||
  29948. "";
  29949. var ui = new editorui.TableButton({
  29950. editor: editor,
  29951. title: title,
  29952. className: "edui-for-inserttable",
  29953. onpicktable: function(t, numCols, numRows) {
  29954. editor.execCommand("InsertTable", {
  29955. numRows: numRows,
  29956. numCols: numCols,
  29957. border: 1
  29958. });
  29959. },
  29960. onbuttonclick: function() {
  29961. this.showPopup();
  29962. }
  29963. });
  29964. editorui.buttons["inserttable"] = ui;
  29965. editor.addListener("selectionchange", function() {
  29966. ui.setDisabled(editor.queryCommandState("inserttable") == -1);
  29967. });
  29968. return ui;
  29969. };
  29970. editorui.lineheight = function(editor) {
  29971. var val = editor.options.lineheight || [];
  29972. if (!val.length) return;
  29973. for (var i = 0, ci, items = []; (ci = val[i++]); ) {
  29974. items.push({
  29975. //todo:写死了
  29976. label: ci,
  29977. value: ci,
  29978. theme: editor.options.theme,
  29979. onclick: function() {
  29980. editor.execCommand("lineheight", this.value);
  29981. }
  29982. });
  29983. }
  29984. var ui = new editorui.MenuButton({
  29985. editor: editor,
  29986. className: "edui-for-lineheight",
  29987. title:
  29988. editor.options.labelMap["lineheight"] ||
  29989. editor.getLang("labelMap.lineheight") ||
  29990. "",
  29991. items: items,
  29992. onbuttonclick: function() {
  29993. var value = editor.queryCommandValue("LineHeight") || this.value;
  29994. editor.execCommand("LineHeight", value);
  29995. }
  29996. });
  29997. editorui.buttons["lineheight"] = ui;
  29998. editor.addListener("selectionchange", function() {
  29999. var state = editor.queryCommandState("LineHeight");
  30000. if (state == -1) {
  30001. ui.setDisabled(true);
  30002. } else {
  30003. ui.setDisabled(false);
  30004. var value = editor.queryCommandValue("LineHeight");
  30005. value && ui.setValue((value + "").replace(/cm/, ""));
  30006. ui.setChecked(state);
  30007. }
  30008. });
  30009. return ui;
  30010. };
  30011. var rowspacings = ["top", "bottom"];
  30012. for (var r = 0, ri; (ri = rowspacings[r++]); ) {
  30013. (function(cmd) {
  30014. editorui["rowspacing" + cmd] = function(editor) {
  30015. var val = editor.options["rowspacing" + cmd] || [];
  30016. if (!val.length) return null;
  30017. for (var i = 0, ci, items = []; (ci = val[i++]); ) {
  30018. items.push({
  30019. label: ci,
  30020. value: ci,
  30021. theme: editor.options.theme,
  30022. onclick: function() {
  30023. editor.execCommand("rowspacing", this.value, cmd);
  30024. }
  30025. });
  30026. }
  30027. var ui = new editorui.MenuButton({
  30028. editor: editor,
  30029. className: "edui-for-rowspacing" + cmd,
  30030. title:
  30031. editor.options.labelMap["rowspacing" + cmd] ||
  30032. editor.getLang("labelMap.rowspacing" + cmd) ||
  30033. "",
  30034. items: items,
  30035. onbuttonclick: function() {
  30036. var value =
  30037. editor.queryCommandValue("rowspacing", cmd) || this.value;
  30038. editor.execCommand("rowspacing", value, cmd);
  30039. }
  30040. });
  30041. editorui.buttons[cmd] = ui;
  30042. editor.addListener("selectionchange", function() {
  30043. var state = editor.queryCommandState("rowspacing", cmd);
  30044. if (state == -1) {
  30045. ui.setDisabled(true);
  30046. } else {
  30047. ui.setDisabled(false);
  30048. var value = editor.queryCommandValue("rowspacing", cmd);
  30049. value && ui.setValue((value + "").replace(/%/, ""));
  30050. ui.setChecked(state);
  30051. }
  30052. });
  30053. return ui;
  30054. };
  30055. })(ri);
  30056. }
  30057. //有序,无序列表
  30058. var lists = ["insertorderedlist", "insertunorderedlist"];
  30059. for (var l = 0, cl; (cl = lists[l++]); ) {
  30060. (function(cmd) {
  30061. editorui[cmd] = function(editor) {
  30062. var vals = editor.options[cmd],
  30063. _onMenuClick = function() {
  30064. editor.execCommand(cmd, this.value);
  30065. },
  30066. items = [];
  30067. for (var i in vals) {
  30068. items.push({
  30069. label: vals[i] || editor.getLang()[cmd][i] || "",
  30070. value: i,
  30071. theme: editor.options.theme,
  30072. onclick: _onMenuClick
  30073. });
  30074. }
  30075. var ui = new editorui.MenuButton({
  30076. editor: editor,
  30077. className: "edui-for-" + cmd,
  30078. title: editor.getLang("labelMap." + cmd) || "",
  30079. items: items,
  30080. onbuttonclick: function() {
  30081. var value = editor.queryCommandValue(cmd) || this.value;
  30082. editor.execCommand(cmd, value);
  30083. }
  30084. });
  30085. editorui.buttons[cmd] = ui;
  30086. editor.addListener("selectionchange", function() {
  30087. var state = editor.queryCommandState(cmd);
  30088. if (state == -1) {
  30089. ui.setDisabled(true);
  30090. } else {
  30091. ui.setDisabled(false);
  30092. var value = editor.queryCommandValue(cmd);
  30093. ui.setValue(value);
  30094. ui.setChecked(state);
  30095. }
  30096. });
  30097. return ui;
  30098. };
  30099. })(cl);
  30100. }
  30101. editorui.fullscreen = function(editor, title) {
  30102. title =
  30103. editor.options.labelMap["fullscreen"] ||
  30104. editor.getLang("labelMap.fullscreen") ||
  30105. "";
  30106. var ui = new editorui.Button({
  30107. className: "edui-for-fullscreen",
  30108. title: title,
  30109. theme: editor.options.theme,
  30110. onclick: function() {
  30111. if (editor.ui) {
  30112. editor.ui.setFullScreen(!editor.ui.isFullScreen());
  30113. }
  30114. this.setChecked(editor.ui.isFullScreen());
  30115. }
  30116. });
  30117. editorui.buttons["fullscreen"] = ui;
  30118. editor.addListener("selectionchange", function() {
  30119. var state = editor.queryCommandState("fullscreen");
  30120. ui.setDisabled(state == -1);
  30121. ui.setChecked(editor.ui.isFullScreen());
  30122. });
  30123. return ui;
  30124. };
  30125. // 表情
  30126. editorui["emotion"] = function(editor, iframeUrl) {
  30127. var cmd = "emotion";
  30128. var ui = new editorui.MultiMenuPop({
  30129. title:
  30130. editor.options.labelMap[cmd] ||
  30131. editor.getLang("labelMap." + cmd + "") ||
  30132. "",
  30133. editor: editor,
  30134. className: "edui-for-" + cmd,
  30135. iframeUrl: editor.ui.mapUrl(
  30136. iframeUrl ||
  30137. (editor.options.iframeUrlMap || {})[cmd] ||
  30138. iframeUrlMap[cmd]
  30139. )
  30140. });
  30141. editorui.buttons[cmd] = ui;
  30142. editor.addListener("selectionchange", function() {
  30143. ui.setDisabled(editor.queryCommandState(cmd) == -1);
  30144. });
  30145. return ui;
  30146. };
  30147. editorui.autotypeset = function(editor) {
  30148. var ui = new editorui.AutoTypeSetButton({
  30149. editor: editor,
  30150. title:
  30151. editor.options.labelMap["autotypeset"] ||
  30152. editor.getLang("labelMap.autotypeset") ||
  30153. "",
  30154. className: "edui-for-autotypeset",
  30155. onbuttonclick: function() {
  30156. editor.execCommand("autotypeset");
  30157. }
  30158. });
  30159. editorui.buttons["autotypeset"] = ui;
  30160. editor.addListener("selectionchange", function() {
  30161. ui.setDisabled(editor.queryCommandState("autotypeset") == -1);
  30162. });
  30163. return ui;
  30164. };
  30165. /* 简单上传插件 */
  30166. editorui["simpleupload"] = function(editor) {
  30167. var name = "simpleupload",
  30168. ui = new editorui.Button({
  30169. className: "edui-for-" + name,
  30170. title:
  30171. editor.options.labelMap[name] ||
  30172. editor.getLang("labelMap." + name) ||
  30173. "",
  30174. onclick: function() {},
  30175. theme: editor.options.theme,
  30176. showText: false
  30177. });
  30178. editorui.buttons[name] = ui;
  30179. editor.addListener("ready", function() {
  30180. var b = ui.getDom("body"),
  30181. iconSpan = b.children[0];
  30182. editor.fireEvent("simpleuploadbtnready", iconSpan);
  30183. });
  30184. editor.addListener("selectionchange", function(type, causeByUi, uiReady) {
  30185. var state = editor.queryCommandState(name);
  30186. if (state == -1) {
  30187. ui.setDisabled(true);
  30188. ui.setChecked(false);
  30189. } else {
  30190. if (!uiReady) {
  30191. ui.setDisabled(false);
  30192. ui.setChecked(state);
  30193. }
  30194. }
  30195. });
  30196. return ui;
  30197. };
  30198. })();
  30199. // adapter/editor.js
  30200. ///import core
  30201. ///commands 全屏
  30202. ///commandsName FullScreen
  30203. ///commandsTitle 全屏
  30204. (function() {
  30205. var utils = baidu.editor.utils,
  30206. uiUtils = baidu.editor.ui.uiUtils,
  30207. UIBase = baidu.editor.ui.UIBase,
  30208. domUtils = baidu.editor.dom.domUtils;
  30209. var nodeStack = [];
  30210. function EditorUI(options) {
  30211. this.initOptions(options);
  30212. this.initEditorUI();
  30213. }
  30214. EditorUI.prototype = {
  30215. uiName: "editor",
  30216. initEditorUI: function() {
  30217. this.editor.ui = this;
  30218. this._dialogs = {};
  30219. this.initUIBase();
  30220. this._initToolbars();
  30221. var editor = this.editor,
  30222. me = this;
  30223. editor.addListener("ready", function() {
  30224. //提供getDialog方法
  30225. editor.getDialog = function(name) {
  30226. return editor.ui._dialogs[name + "Dialog"];
  30227. };
  30228. domUtils.on(editor.window, "scroll", function(evt) {
  30229. baidu.editor.ui.Popup.postHide(evt);
  30230. });
  30231. //提供编辑器实时宽高(全屏时宽高不变化)
  30232. editor.ui._actualFrameWidth = editor.options.initialFrameWidth;
  30233. UE.browser.ie &&
  30234. UE.browser.version === 6 &&
  30235. editor.container.ownerDocument.execCommand(
  30236. "BackgroundImageCache",
  30237. false,
  30238. true
  30239. );
  30240. //display bottom-bar label based on config
  30241. if (editor.options.elementPathEnabled) {
  30242. editor.ui.getDom("elementpath").innerHTML =
  30243. '<div class="edui-editor-breadcrumb">' +
  30244. editor.getLang("elementPathTip") +
  30245. ":</div>";
  30246. }
  30247. if (editor.options.wordCount) {
  30248. function countFn() {
  30249. setCount(editor, me);
  30250. domUtils.un(editor.document, "click", arguments.callee);
  30251. }
  30252. domUtils.on(editor.document, "click", countFn);
  30253. editor.ui.getDom("wordcount").innerHTML = editor.getLang(
  30254. "wordCountTip"
  30255. );
  30256. }
  30257. editor.ui._scale();
  30258. if (editor.options.scaleEnabled) {
  30259. if (editor.autoHeightEnabled) {
  30260. editor.disableAutoHeight();
  30261. }
  30262. me.enableScale();
  30263. } else {
  30264. me.disableScale();
  30265. }
  30266. if (
  30267. !editor.options.elementPathEnabled &&
  30268. !editor.options.wordCount &&
  30269. !editor.options.scaleEnabled
  30270. ) {
  30271. editor.ui.getDom("elementpath").style.display = "none";
  30272. editor.ui.getDom("wordcount").style.display = "none";
  30273. editor.ui.getDom("scale").style.display = "none";
  30274. }
  30275. if (!editor.selection.isFocus()) return;
  30276. editor.fireEvent("selectionchange", false, true);
  30277. });
  30278. editor.addListener("mousedown", function(t, evt) {
  30279. var el = evt.target || evt.srcElement;
  30280. baidu.editor.ui.Popup.postHide(evt, el);
  30281. baidu.editor.ui.ShortCutMenu.postHide(evt);
  30282. });
  30283. editor.addListener("delcells", function() {
  30284. if (UE.ui["edittip"]) {
  30285. new UE.ui["edittip"](editor);
  30286. }
  30287. editor.getDialog("edittip").open();
  30288. });
  30289. var pastePop,
  30290. isPaste = false,
  30291. timer;
  30292. editor.addListener("afterpaste", function() {
  30293. if (editor.queryCommandState("pasteplain")) return;
  30294. if (baidu.editor.ui.PastePicker) {
  30295. pastePop = new baidu.editor.ui.Popup({
  30296. content: new baidu.editor.ui.PastePicker({ editor: editor }),
  30297. editor: editor,
  30298. className: "edui-wordpastepop"
  30299. });
  30300. pastePop.render();
  30301. }
  30302. isPaste = true;
  30303. });
  30304. editor.addListener("afterinserthtml", function() {
  30305. clearTimeout(timer);
  30306. timer = setTimeout(function() {
  30307. if (pastePop && (isPaste || editor.ui._isTransfer)) {
  30308. if (pastePop.isHidden()) {
  30309. var span = domUtils.createElement(editor.document, "span", {
  30310. style: "line-height:0px;",
  30311. innerHTML: "\ufeff"
  30312. }),
  30313. range = editor.selection.getRange();
  30314. range.insertNode(span);
  30315. var tmp = getDomNode(span, "firstChild", "previousSibling");
  30316. tmp &&
  30317. pastePop.showAnchor(tmp.nodeType == 3 ? tmp.parentNode : tmp);
  30318. domUtils.remove(span);
  30319. } else {
  30320. pastePop.show();
  30321. }
  30322. delete editor.ui._isTransfer;
  30323. isPaste = false;
  30324. }
  30325. }, 200);
  30326. });
  30327. editor.addListener("contextmenu", function(t, evt) {
  30328. baidu.editor.ui.Popup.postHide(evt);
  30329. });
  30330. editor.addListener("keydown", function(t, evt) {
  30331. if (pastePop) pastePop.dispose(evt);
  30332. var keyCode = evt.keyCode || evt.which;
  30333. if (evt.altKey && keyCode == 90) {
  30334. UE.ui.buttons["fullscreen"].onclick();
  30335. }
  30336. });
  30337. editor.addListener("wordcount", function(type) {
  30338. setCount(this, me);
  30339. });
  30340. function setCount(editor, ui) {
  30341. editor.setOpt({
  30342. wordCount: true,
  30343. maximumWords: 10000,
  30344. wordCountMsg:
  30345. editor.options.wordCountMsg || editor.getLang("wordCountMsg"),
  30346. wordOverFlowMsg:
  30347. editor.options.wordOverFlowMsg || editor.getLang("wordOverFlowMsg")
  30348. });
  30349. var opt = editor.options,
  30350. max = opt.maximumWords,
  30351. msg = opt.wordCountMsg,
  30352. errMsg = opt.wordOverFlowMsg,
  30353. countDom = ui.getDom("wordcount");
  30354. if (!opt.wordCount) {
  30355. return;
  30356. }
  30357. var count = editor.getContentLength(true);
  30358. if (count > max) {
  30359. countDom.innerHTML = errMsg;
  30360. editor.fireEvent("wordcountoverflow");
  30361. } else {
  30362. countDom.innerHTML = msg
  30363. .replace("{#leave}", max - count)
  30364. .replace("{#count}", count);
  30365. }
  30366. }
  30367. editor.addListener("selectionchange", function() {
  30368. if (editor.options.elementPathEnabled) {
  30369. me[
  30370. (editor.queryCommandState("elementpath") == -1 ? "dis" : "en") +
  30371. "ableElementPath"
  30372. ]();
  30373. }
  30374. if (editor.options.scaleEnabled) {
  30375. me[
  30376. (editor.queryCommandState("scale") == -1 ? "dis" : "en") +
  30377. "ableScale"
  30378. ]();
  30379. }
  30380. });
  30381. var popup = new baidu.editor.ui.Popup({
  30382. editor: editor,
  30383. content: "",
  30384. className: "edui-bubble",
  30385. _onEditButtonClick: function() {
  30386. this.hide();
  30387. editor.ui._dialogs.linkDialog.open();
  30388. },
  30389. _onImgEditButtonClick: function(name) {
  30390. this.hide();
  30391. editor.ui._dialogs[name] && editor.ui._dialogs[name].open();
  30392. },
  30393. _onImgSetFloat: function(value) {
  30394. this.hide();
  30395. editor.execCommand("imagefloat", value);
  30396. },
  30397. _setIframeAlign: function(value) {
  30398. var frame = popup.anchorEl;
  30399. var newFrame = frame.cloneNode(true);
  30400. switch (value) {
  30401. case -2:
  30402. newFrame.setAttribute("align", "");
  30403. break;
  30404. case -1:
  30405. newFrame.setAttribute("align", "left");
  30406. break;
  30407. case 1:
  30408. newFrame.setAttribute("align", "right");
  30409. break;
  30410. }
  30411. frame.parentNode.insertBefore(newFrame, frame);
  30412. domUtils.remove(frame);
  30413. popup.anchorEl = newFrame;
  30414. popup.showAnchor(popup.anchorEl);
  30415. },
  30416. _updateIframe: function() {
  30417. var frame = (editor._iframe = popup.anchorEl);
  30418. if (domUtils.hasClass(frame, "ueditor_baidumap")) {
  30419. editor.selection.getRange().selectNode(frame).select();
  30420. editor.ui._dialogs.mapDialog.open();
  30421. popup.hide();
  30422. } else {
  30423. editor.ui._dialogs.insertframeDialog.open();
  30424. popup.hide();
  30425. }
  30426. },
  30427. _onRemoveButtonClick: function(cmdName) {
  30428. editor.execCommand(cmdName);
  30429. this.hide();
  30430. },
  30431. queryAutoHide: function(el) {
  30432. if (el && el.ownerDocument == editor.document) {
  30433. if (
  30434. el.tagName.toLowerCase() == "img" ||
  30435. domUtils.findParentByTagName(el, "a", true)
  30436. ) {
  30437. return el !== popup.anchorEl;
  30438. }
  30439. }
  30440. return baidu.editor.ui.Popup.prototype.queryAutoHide.call(this, el);
  30441. }
  30442. });
  30443. popup.render();
  30444. if (editor.options.imagePopup) {
  30445. editor.addListener("mouseover", function(t, evt) {
  30446. evt = evt || window.event;
  30447. var el = evt.target || evt.srcElement;
  30448. if (
  30449. editor.ui._dialogs.insertframeDialog &&
  30450. /iframe/gi.test(el.tagName)
  30451. ) {
  30452. var html = popup.formatHtml(
  30453. "<nobr>" +
  30454. editor.getLang("property") +
  30455. ': <span onclick=$$._setIframeAlign(-2) class="edui-clickable">' +
  30456. editor.getLang("default") +
  30457. '</span>&nbsp;&nbsp;<span onclick=$$._setIframeAlign(-1) class="edui-clickable">' +
  30458. editor.getLang("justifyleft") +
  30459. '</span>&nbsp;&nbsp;<span onclick=$$._setIframeAlign(1) class="edui-clickable">' +
  30460. editor.getLang("justifyright") +
  30461. "</span>&nbsp;&nbsp;" +
  30462. ' <span onclick="$$._updateIframe( this);" class="edui-clickable">' +
  30463. editor.getLang("modify") +
  30464. "</span></nobr>"
  30465. );
  30466. if (html) {
  30467. popup.getDom("content").innerHTML = html;
  30468. popup.anchorEl = el;
  30469. popup.showAnchor(popup.anchorEl);
  30470. } else {
  30471. popup.hide();
  30472. }
  30473. }
  30474. });
  30475. editor.addListener("selectionchange", function(t, causeByUi) {
  30476. if (!causeByUi) return;
  30477. var html = "",
  30478. str = "",
  30479. img = editor.selection.getRange().getClosedNode(),
  30480. dialogs = editor.ui._dialogs;
  30481. if (img && img.tagName == "IMG") {
  30482. var dialogName = "insertimageDialog";
  30483. if (
  30484. img.className.indexOf("edui-faked-video") != -1 ||
  30485. img.className.indexOf("edui-upload-video") != -1
  30486. ) {
  30487. dialogName = "insertvideoDialog";
  30488. }
  30489. if (img.className.indexOf("edui-faked-webapp") != -1) {
  30490. dialogName = "webappDialog";
  30491. }
  30492. if (img.src.indexOf("http://api.map.baidu.com") != -1) {
  30493. dialogName = "mapDialog";
  30494. }
  30495. if (img.className.indexOf("edui-faked-music") != -1) {
  30496. dialogName = "musicDialog";
  30497. }
  30498. if (
  30499. img.src.indexOf("http://maps.google.com/maps/api/staticmap") != -1
  30500. ) {
  30501. dialogName = "gmapDialog";
  30502. }
  30503. if (img.getAttribute("anchorname")) {
  30504. dialogName = "anchorDialog";
  30505. html = popup.formatHtml(
  30506. "<nobr>" +
  30507. editor.getLang("property") +
  30508. ': <span onclick=$$._onImgEditButtonClick("anchorDialog") class="edui-clickable">' +
  30509. editor.getLang("modify") +
  30510. "</span>&nbsp;&nbsp;" +
  30511. "<span onclick=$$._onRemoveButtonClick('anchor') class=\"edui-clickable\">" +
  30512. editor.getLang("delete") +
  30513. "</span></nobr>"
  30514. );
  30515. }
  30516. if (img.getAttribute("word_img")) {
  30517. //todo 放到dialog去做查询
  30518. editor.word_img = [img.getAttribute("word_img")];
  30519. dialogName = "wordimageDialog";
  30520. }
  30521. if (
  30522. domUtils.hasClass(img, "loadingclass") ||
  30523. domUtils.hasClass(img, "loaderrorclass")
  30524. ) {
  30525. dialogName = "";
  30526. }
  30527. if (!dialogs[dialogName]) {
  30528. return;
  30529. }
  30530. str =
  30531. "<nobr>" +
  30532. editor.getLang("property") +
  30533. ": " +
  30534. '<span onclick=$$._onImgSetFloat("none") class="edui-clickable">' +
  30535. editor.getLang("default") +
  30536. "</span>&nbsp;&nbsp;" +
  30537. '<span onclick=$$._onImgSetFloat("left") class="edui-clickable">' +
  30538. editor.getLang("justifyleft") +
  30539. "</span>&nbsp;&nbsp;" +
  30540. '<span onclick=$$._onImgSetFloat("right") class="edui-clickable">' +
  30541. editor.getLang("justifyright") +
  30542. "</span>&nbsp;&nbsp;" +
  30543. '<span onclick=$$._onImgSetFloat("center") class="edui-clickable">' +
  30544. editor.getLang("justifycenter") +
  30545. "</span>&nbsp;&nbsp;" +
  30546. "<span onclick=\"$$._onImgEditButtonClick('" +
  30547. dialogName +
  30548. '\');" class="edui-clickable">' +
  30549. editor.getLang("modify") +
  30550. "</span></nobr>";
  30551. !html && (html = popup.formatHtml(str));
  30552. }
  30553. if (editor.ui._dialogs.linkDialog) {
  30554. var link = editor.queryCommandValue("link");
  30555. var url;
  30556. if (
  30557. link &&
  30558. (url = link.getAttribute("_href") || link.getAttribute("href", 2))
  30559. ) {
  30560. var txt = url;
  30561. if (url.length > 30) {
  30562. txt = url.substring(0, 20) + "...";
  30563. }
  30564. if (html) {
  30565. html += '<div style="height:5px;"></div>';
  30566. }
  30567. html += popup.formatHtml(
  30568. "<nobr>" +
  30569. editor.getLang("anthorMsg") +
  30570. ': <a target="_blank" href="' +
  30571. url +
  30572. '" title="' +
  30573. url +
  30574. '" >' +
  30575. txt +
  30576. "</a>" +
  30577. ' <span class="edui-clickable" onclick="$$._onEditButtonClick();">' +
  30578. editor.getLang("modify") +
  30579. "</span>" +
  30580. ' <span class="edui-clickable" onclick="$$._onRemoveButtonClick(\'unlink\');"> ' +
  30581. editor.getLang("clear") +
  30582. "</span></nobr>"
  30583. );
  30584. popup.showAnchor(link);
  30585. }
  30586. }
  30587. if (html) {
  30588. popup.getDom("content").innerHTML = html;
  30589. popup.anchorEl = img || link;
  30590. popup.showAnchor(popup.anchorEl);
  30591. } else {
  30592. popup.hide();
  30593. }
  30594. });
  30595. }
  30596. },
  30597. _initToolbars: function() {
  30598. var editor = this.editor;
  30599. var toolbars = this.toolbars || [];
  30600. var toolbarUis = [];
  30601. var extraUIs = [];
  30602. for (var i = 0; i < toolbars.length; i++) {
  30603. var toolbar = toolbars[i];
  30604. var toolbarUi = new baidu.editor.ui.Toolbar({
  30605. theme: editor.options.theme
  30606. });
  30607. for (var j = 0; j < toolbar.length; j++) {
  30608. var toolbarItem = toolbar[j];
  30609. var toolbarItemUi = null;
  30610. if (typeof toolbarItem == "string") {
  30611. toolbarItem = toolbarItem.toLowerCase();
  30612. if (toolbarItem == "|") {
  30613. toolbarItem = "Separator";
  30614. }
  30615. if (toolbarItem == "||") {
  30616. toolbarItem = "Breakline";
  30617. }
  30618. var ui = baidu.editor.ui[toolbarItem];
  30619. if (ui) {
  30620. if (utils.isFunction(ui)) {
  30621. toolbarItemUi = new baidu.editor.ui[toolbarItem](editor);
  30622. } else {
  30623. if (ui.id && ui.id != editor.key) {
  30624. continue;
  30625. }
  30626. var itemUI = ui.execFn.call(editor, editor, toolbarItem);
  30627. if (itemUI) {
  30628. if (ui.index === undefined) {
  30629. toolbarUi.add(itemUI);
  30630. continue;
  30631. } else {
  30632. extraUIs.push({
  30633. index: ui.index,
  30634. itemUI: itemUI
  30635. });
  30636. }
  30637. }
  30638. }
  30639. }
  30640. //fullscreen这里单独处理一下,放到首行去
  30641. if (toolbarItem == "fullscreen") {
  30642. if (toolbarUis && toolbarUis[0]) {
  30643. toolbarUis[0].items.splice(0, 0, toolbarItemUi);
  30644. } else {
  30645. toolbarItemUi && toolbarUi.items.splice(0, 0, toolbarItemUi);
  30646. }
  30647. continue;
  30648. }
  30649. } else {
  30650. toolbarItemUi = toolbarItem;
  30651. }
  30652. if (toolbarItemUi && toolbarItemUi.id) {
  30653. toolbarUi.add(toolbarItemUi);
  30654. }
  30655. }
  30656. toolbarUis[i] = toolbarUi;
  30657. }
  30658. //接受外部定制的UI
  30659. utils.each(extraUIs, function(obj) {
  30660. toolbarUi.add(obj.itemUI, obj.index);
  30661. });
  30662. this.toolbars = toolbarUis;
  30663. },
  30664. getHtmlTpl: function() {
  30665. return (
  30666. '<div id="##" class="%%">' +
  30667. '<div id="##_toolbarbox" class="%%-toolbarbox">' +
  30668. (this.toolbars.length
  30669. ? '<div id="##_toolbarboxouter" class="%%-toolbarboxouter"><div class="%%-toolbarboxinner">' +
  30670. this.renderToolbarBoxHtml() +
  30671. "</div></div>"
  30672. : "") +
  30673. '<div id="##_toolbarmsg" class="%%-toolbarmsg" style="display:none;">' +
  30674. '<div id = "##_upload_dialog" class="%%-toolbarmsg-upload" onclick="$$.showWordImageDialog();">' +
  30675. this.editor.getLang("clickToUpload") +
  30676. "</div>" +
  30677. '<div class="%%-toolbarmsg-close" onclick="$$.hideToolbarMsg();">x</div>' +
  30678. '<div id="##_toolbarmsg_label" class="%%-toolbarmsg-label"></div>' +
  30679. '<div style="height:0;overflow:hidden;clear:both;"></div>' +
  30680. "</div>" +
  30681. '<div id="##_message_holder" class="%%-messageholder"></div>' +
  30682. "</div>" +
  30683. '<div id="##_iframeholder" class="%%-iframeholder">' +
  30684. "</div>" +
  30685. //modify wdcount by matao
  30686. '<div id="##_bottombar" class="%%-bottomContainer"><table><tr>' +
  30687. '<td id="##_elementpath" class="%%-bottombar"></td>' +
  30688. '<td id="##_wordcount" class="%%-wordcount"></td>' +
  30689. '<td id="##_scale" class="%%-scale"><div class="%%-icon"></div></td>' +
  30690. "</tr></table></div>" +
  30691. '<div id="##_scalelayer"></div>' +
  30692. "</div>"
  30693. );
  30694. },
  30695. showWordImageDialog: function() {
  30696. this._dialogs["wordimageDialog"].open();
  30697. },
  30698. renderToolbarBoxHtml: function() {
  30699. var buff = [];
  30700. for (var i = 0; i < this.toolbars.length; i++) {
  30701. buff.push(this.toolbars[i].renderHtml());
  30702. }
  30703. return buff.join("");
  30704. },
  30705. setFullScreen: function(fullscreen) {
  30706. var editor = this.editor,
  30707. container = editor.container.parentNode.parentNode;
  30708. if (this._fullscreen != fullscreen) {
  30709. this._fullscreen = fullscreen;
  30710. this.editor.fireEvent("beforefullscreenchange", fullscreen);
  30711. if (baidu.editor.browser.gecko) {
  30712. var bk = editor.selection.getRange().createBookmark();
  30713. }
  30714. if (fullscreen) {
  30715. while (container.tagName != "BODY") {
  30716. var position = baidu.editor.dom.domUtils.getComputedStyle(
  30717. container,
  30718. "position"
  30719. );
  30720. nodeStack.push(position);
  30721. container.style.position = "static";
  30722. container = container.parentNode;
  30723. }
  30724. this._bakHtmlOverflow = document.documentElement.style.overflow;
  30725. this._bakBodyOverflow = document.body.style.overflow;
  30726. this._bakAutoHeight = this.editor.autoHeightEnabled;
  30727. this._bakScrollTop = Math.max(
  30728. document.documentElement.scrollTop,
  30729. document.body.scrollTop
  30730. );
  30731. this._bakEditorContaninerWidth = editor.iframe.parentNode.offsetWidth;
  30732. if (this._bakAutoHeight) {
  30733. //当全屏时不能执行自动长高
  30734. editor.autoHeightEnabled = false;
  30735. this.editor.disableAutoHeight();
  30736. }
  30737. document.documentElement.style.overflow = "hidden";
  30738. //修复,滚动条不收起的问题
  30739. window.scrollTo(0, window.scrollY);
  30740. this._bakCssText = this.getDom().style.cssText;
  30741. this._bakCssText1 = this.getDom("iframeholder").style.cssText;
  30742. editor.iframe.parentNode.style.width = "";
  30743. this._updateFullScreen();
  30744. } else {
  30745. while (container.tagName != "BODY") {
  30746. container.style.position = nodeStack.shift();
  30747. container = container.parentNode;
  30748. }
  30749. this.getDom().style.cssText = this._bakCssText;
  30750. this.getDom("iframeholder").style.cssText = this._bakCssText1;
  30751. if (this._bakAutoHeight) {
  30752. editor.autoHeightEnabled = true;
  30753. this.editor.enableAutoHeight();
  30754. }
  30755. document.documentElement.style.overflow = this._bakHtmlOverflow;
  30756. document.body.style.overflow = this._bakBodyOverflow;
  30757. editor.iframe.parentNode.style.width =
  30758. this._bakEditorContaninerWidth + "px";
  30759. window.scrollTo(0, this._bakScrollTop);
  30760. }
  30761. if (browser.gecko && editor.body.contentEditable === "true") {
  30762. var input = document.createElement("input");
  30763. document.body.appendChild(input);
  30764. editor.body.contentEditable = false;
  30765. setTimeout(function() {
  30766. input.focus();
  30767. setTimeout(function() {
  30768. editor.body.contentEditable = true;
  30769. editor.fireEvent("fullscreenchanged", fullscreen);
  30770. editor.selection.getRange().moveToBookmark(bk).select(true);
  30771. baidu.editor.dom.domUtils.remove(input);
  30772. fullscreen && window.scroll(0, 0);
  30773. }, 0);
  30774. }, 0);
  30775. }
  30776. if (editor.body.contentEditable === "true") {
  30777. this.editor.fireEvent("fullscreenchanged", fullscreen);
  30778. this.triggerLayout();
  30779. }
  30780. }
  30781. },
  30782. _updateFullScreen: function() {
  30783. if (this._fullscreen) {
  30784. var vpRect = uiUtils.getViewportRect();
  30785. this.getDom().style.cssText =
  30786. "border:0;position:absolute;left:0;top:" +
  30787. (this.editor.options.topOffset || 0) +
  30788. "px;width:" +
  30789. vpRect.width +
  30790. "px;height:" +
  30791. vpRect.height +
  30792. "px;z-index:" +
  30793. (this.getDom().style.zIndex * 1 + 100);
  30794. uiUtils.setViewportOffset(this.getDom(), {
  30795. left: 0,
  30796. top: this.editor.options.topOffset || 0
  30797. });
  30798. this.editor.setHeight(
  30799. vpRect.height -
  30800. this.getDom("toolbarbox").offsetHeight -
  30801. this.getDom("bottombar").offsetHeight -
  30802. (this.editor.options.topOffset || 0),
  30803. true
  30804. );
  30805. //不手动调一下,会导致全屏失效
  30806. if (browser.gecko) {
  30807. try {
  30808. window.onresize();
  30809. } catch (e) {}
  30810. }
  30811. }
  30812. },
  30813. _updateElementPath: function() {
  30814. var bottom = this.getDom("elementpath"),
  30815. list;
  30816. if (
  30817. this.elementPathEnabled &&
  30818. (list = this.editor.queryCommandValue("elementpath"))
  30819. ) {
  30820. var buff = [];
  30821. for (var i = 0, ci; (ci = list[i]); i++) {
  30822. buff[i] = this.formatHtml(
  30823. '<span unselectable="on" onclick="$$.editor.execCommand(&quot;elementpath&quot;, &quot;' +
  30824. i +
  30825. '&quot;);">' +
  30826. ci +
  30827. "</span>"
  30828. );
  30829. }
  30830. bottom.innerHTML =
  30831. '<div class="edui-editor-breadcrumb" onmousedown="return false;">' +
  30832. this.editor.getLang("elementPathTip") +
  30833. ": " +
  30834. buff.join(" &gt; ") +
  30835. "</div>";
  30836. } else {
  30837. bottom.style.display = "none";
  30838. }
  30839. },
  30840. disableElementPath: function() {
  30841. var bottom = this.getDom("elementpath");
  30842. bottom.innerHTML = "";
  30843. bottom.style.display = "none";
  30844. this.elementPathEnabled = false;
  30845. },
  30846. enableElementPath: function() {
  30847. var bottom = this.getDom("elementpath");
  30848. bottom.style.display = "";
  30849. this.elementPathEnabled = true;
  30850. this._updateElementPath();
  30851. },
  30852. _scale: function() {
  30853. var doc = document,
  30854. editor = this.editor,
  30855. editorHolder = editor.container,
  30856. editorDocument = editor.document,
  30857. toolbarBox = this.getDom("toolbarbox"),
  30858. bottombar = this.getDom("bottombar"),
  30859. scale = this.getDom("scale"),
  30860. scalelayer = this.getDom("scalelayer");
  30861. var isMouseMove = false,
  30862. position = null,
  30863. minEditorHeight = 0,
  30864. minEditorWidth = editor.options.minFrameWidth,
  30865. pageX = 0,
  30866. pageY = 0,
  30867. scaleWidth = 0,
  30868. scaleHeight = 0;
  30869. function down() {
  30870. position = domUtils.getXY(editorHolder);
  30871. if (!minEditorHeight) {
  30872. minEditorHeight =
  30873. editor.options.minFrameHeight +
  30874. toolbarBox.offsetHeight +
  30875. bottombar.offsetHeight;
  30876. }
  30877. scalelayer.style.cssText =
  30878. "position:absolute;left:0;display:;top:0;background-color:#41ABFF;opacity:0.4;filter: Alpha(opacity=40);width:" +
  30879. editorHolder.offsetWidth +
  30880. "px;height:" +
  30881. editorHolder.offsetHeight +
  30882. "px;z-index:" +
  30883. (editor.options.zIndex + 1);
  30884. domUtils.on(doc, "mousemove", move);
  30885. domUtils.on(editorDocument, "mouseup", up);
  30886. domUtils.on(doc, "mouseup", up);
  30887. }
  30888. var me = this;
  30889. //by xuheng 全屏时关掉缩放
  30890. this.editor.addListener("fullscreenchanged", function(e, fullScreen) {
  30891. if (fullScreen) {
  30892. me.disableScale();
  30893. } else {
  30894. if (me.editor.options.scaleEnabled) {
  30895. me.enableScale();
  30896. var tmpNode = me.editor.document.createElement("span");
  30897. me.editor.body.appendChild(tmpNode);
  30898. me.editor.body.style.height =
  30899. Math.max(
  30900. domUtils.getXY(tmpNode).y,
  30901. me.editor.iframe.offsetHeight - 20
  30902. ) + "px";
  30903. domUtils.remove(tmpNode);
  30904. }
  30905. }
  30906. });
  30907. function move(event) {
  30908. clearSelection();
  30909. var e = event || window.event;
  30910. pageX = e.pageX || doc.documentElement.scrollLeft + e.clientX;
  30911. pageY = e.pageY || doc.documentElement.scrollTop + e.clientY;
  30912. scaleWidth = pageX - position.x;
  30913. scaleHeight = pageY - position.y;
  30914. if (scaleWidth >= minEditorWidth) {
  30915. isMouseMove = true;
  30916. scalelayer.style.width = scaleWidth + "px";
  30917. }
  30918. if (scaleHeight >= minEditorHeight) {
  30919. isMouseMove = true;
  30920. scalelayer.style.height = scaleHeight + "px";
  30921. }
  30922. }
  30923. function up() {
  30924. if (isMouseMove) {
  30925. isMouseMove = false;
  30926. editor.ui._actualFrameWidth = scalelayer.offsetWidth - 2;
  30927. editorHolder.style.width = editor.ui._actualFrameWidth + "px";
  30928. editor.setHeight(
  30929. scalelayer.offsetHeight -
  30930. bottombar.offsetHeight -
  30931. toolbarBox.offsetHeight -
  30932. 2,
  30933. true
  30934. );
  30935. }
  30936. if (scalelayer) {
  30937. scalelayer.style.display = "none";
  30938. }
  30939. clearSelection();
  30940. domUtils.un(doc, "mousemove", move);
  30941. domUtils.un(editorDocument, "mouseup", up);
  30942. domUtils.un(doc, "mouseup", up);
  30943. }
  30944. function clearSelection() {
  30945. if (browser.ie) doc.selection.clear();
  30946. else window.getSelection().removeAllRanges();
  30947. }
  30948. this.enableScale = function() {
  30949. //trace:2868
  30950. if (editor.queryCommandState("source") == 1) return;
  30951. scale.style.display = "";
  30952. this.scaleEnabled = true;
  30953. domUtils.on(scale, "mousedown", down);
  30954. };
  30955. this.disableScale = function() {
  30956. scale.style.display = "none";
  30957. this.scaleEnabled = false;
  30958. domUtils.un(scale, "mousedown", down);
  30959. };
  30960. },
  30961. isFullScreen: function() {
  30962. return this._fullscreen;
  30963. },
  30964. postRender: function() {
  30965. UIBase.prototype.postRender.call(this);
  30966. for (var i = 0; i < this.toolbars.length; i++) {
  30967. this.toolbars[i].postRender();
  30968. }
  30969. var me = this;
  30970. var timerId,
  30971. domUtils = baidu.editor.dom.domUtils,
  30972. updateFullScreenTime = function() {
  30973. clearTimeout(timerId);
  30974. timerId = setTimeout(function() {
  30975. me._updateFullScreen();
  30976. });
  30977. };
  30978. domUtils.on(window, "resize", updateFullScreenTime);
  30979. me.addListener("destroy", function() {
  30980. domUtils.un(window, "resize", updateFullScreenTime);
  30981. clearTimeout(timerId);
  30982. });
  30983. },
  30984. showToolbarMsg: function(msg, flag) {
  30985. this.getDom("toolbarmsg_label").innerHTML = msg;
  30986. this.getDom("toolbarmsg").style.display = "";
  30987. //
  30988. if (!flag) {
  30989. var w = this.getDom("upload_dialog");
  30990. w.style.display = "none";
  30991. }
  30992. },
  30993. hideToolbarMsg: function() {
  30994. this.getDom("toolbarmsg").style.display = "none";
  30995. },
  30996. mapUrl: function(url) {
  30997. return url
  30998. ? url.replace("~/", this.editor.options.UEDITOR_HOME_URL || "")
  30999. : "";
  31000. },
  31001. triggerLayout: function() {
  31002. var dom = this.getDom();
  31003. if (dom.style.zoom == "1") {
  31004. dom.style.zoom = "100%";
  31005. } else {
  31006. dom.style.zoom = "1";
  31007. }
  31008. }
  31009. };
  31010. utils.inherits(EditorUI, baidu.editor.ui.UIBase);
  31011. var instances = {};
  31012. UE.ui.Editor = function(options) {
  31013. var editor = new UE.Editor(options);
  31014. editor.options.editor = editor;
  31015. utils.loadFile(document, {
  31016. href:
  31017. editor.options.themePath + editor.options.theme + "/css/ueditor.css",
  31018. tag: "link",
  31019. type: "text/css",
  31020. rel: "stylesheet"
  31021. });
  31022. var oldRender = editor.render;
  31023. editor.render = function(holder) {
  31024. if (holder.constructor === String) {
  31025. editor.key = holder;
  31026. instances[holder] = editor;
  31027. }
  31028. utils.domReady(function() {
  31029. editor.langIsReady
  31030. ? renderUI()
  31031. : editor.addListener("langReady", renderUI);
  31032. function renderUI() {
  31033. editor.setOpt({
  31034. labelMap: editor.options.labelMap || editor.getLang("labelMap")
  31035. });
  31036. new EditorUI(editor.options);
  31037. if (holder) {
  31038. if (holder.constructor === String) {
  31039. holder = document.getElementById(holder);
  31040. }
  31041. holder &&
  31042. holder.getAttribute("name") &&
  31043. (editor.options.textarea = holder.getAttribute("name"));
  31044. if (holder && /script|textarea/gi.test(holder.tagName)) {
  31045. var newDiv = document.createElement("div");
  31046. holder.parentNode.insertBefore(newDiv, holder);
  31047. var cont = holder.value || holder.innerHTML;
  31048. editor.options.initialContent = /^[\t\r\n ]*$/.test(cont)
  31049. ? editor.options.initialContent
  31050. : cont
  31051. .replace(/>[\n\r\t]+([ ]{4})+/g, ">")
  31052. .replace(/[\n\r\t]+([ ]{4})+</g, "<")
  31053. .replace(/>[\n\r\t]+</g, "><");
  31054. holder.className && (newDiv.className = holder.className);
  31055. holder.style.cssText &&
  31056. (newDiv.style.cssText = holder.style.cssText);
  31057. if (/textarea/i.test(holder.tagName)) {
  31058. editor.textarea = holder;
  31059. editor.textarea.style.display = "none";
  31060. } else {
  31061. holder.parentNode.removeChild(holder);
  31062. }
  31063. if (holder.id) {
  31064. newDiv.id = holder.id;
  31065. domUtils.removeAttributes(holder, "id");
  31066. }
  31067. holder = newDiv;
  31068. holder.innerHTML = "";
  31069. }
  31070. }
  31071. domUtils.addClass(holder, "edui-" + editor.options.theme);
  31072. editor.ui.render(holder);
  31073. var opt = editor.options;
  31074. //给实例添加一个编辑器的容器引用
  31075. editor.container = editor.ui.getDom();
  31076. var parents = domUtils.findParents(holder, true);
  31077. var displays = [];
  31078. for (var i = 0, ci; (ci = parents[i]); i++) {
  31079. displays[i] = ci.style.display;
  31080. ci.style.display = "block";
  31081. }
  31082. if (opt.initialFrameWidth) {
  31083. opt.minFrameWidth = opt.initialFrameWidth;
  31084. } else {
  31085. opt.minFrameWidth = opt.initialFrameWidth = holder.offsetWidth;
  31086. var styleWidth = holder.style.width;
  31087. if (/%$/.test(styleWidth)) {
  31088. opt.initialFrameWidth = styleWidth;
  31089. }
  31090. }
  31091. if (opt.initialFrameHeight) {
  31092. opt.minFrameHeight = opt.initialFrameHeight;
  31093. } else {
  31094. opt.initialFrameHeight = opt.minFrameHeight = holder.offsetHeight;
  31095. }
  31096. for (var i = 0, ci; (ci = parents[i]); i++) {
  31097. ci.style.display = displays[i];
  31098. }
  31099. //编辑器最外容器设置了高度,会导致,编辑器不占位
  31100. //todo 先去掉,没有找到原因
  31101. if (holder.style.height) {
  31102. holder.style.height = "";
  31103. }
  31104. editor.container.style.width =
  31105. opt.initialFrameWidth +
  31106. (/%$/.test(opt.initialFrameWidth) ? "" : "px");
  31107. editor.container.style.zIndex = opt.zIndex;
  31108. oldRender.call(editor, editor.ui.getDom("iframeholder"));
  31109. editor.fireEvent("afteruiready");
  31110. }
  31111. });
  31112. };
  31113. return editor;
  31114. };
  31115. /**
  31116. * @file
  31117. * @name UE
  31118. * @short UE
  31119. * @desc UEditor的顶部命名空间
  31120. */
  31121. /**
  31122. * @name getEditor
  31123. * @since 1.2.4+
  31124. * @grammar UE.getEditor(id,[opt]) => Editor实例
  31125. * @desc 提供一个全局的方法得到编辑器实例
  31126. *
  31127. * * ''id'' 放置编辑器的容器id, 如果容器下的编辑器已经存在,就直接返回
  31128. * * ''opt'' 编辑器的可选参数
  31129. * @example
  31130. * UE.getEditor('containerId',{onready:function(){//创建一个编辑器实例
  31131. * this.setContent('hello')
  31132. * }});
  31133. * UE.getEditor('containerId'); //返回刚创建的实例
  31134. *
  31135. */
  31136. UE.getEditor = function(id, opt) {
  31137. var editor = instances[id];
  31138. if (!editor) {
  31139. editor = instances[id] = new UE.ui.Editor(opt);
  31140. editor.render(id);
  31141. }
  31142. return editor;
  31143. };
  31144. UE.delEditor = function(id) {
  31145. var editor;
  31146. if ((editor = instances[id])) {
  31147. editor.key && editor.destroy();
  31148. delete instances[id];
  31149. }
  31150. };
  31151. UE.registerUI = function(uiName, fn, index, editorId) {
  31152. utils.each(uiName.split(/\s+/), function(name) {
  31153. baidu.editor.ui[name] = {
  31154. id: editorId,
  31155. execFn: fn,
  31156. index: index
  31157. };
  31158. });
  31159. };
  31160. })();
  31161. // adapter/message.js
  31162. UE.registerUI("message", function(editor) {
  31163. var editorui = baidu.editor.ui;
  31164. var Message = editorui.Message;
  31165. var holder;
  31166. var _messageItems = [];
  31167. var me = editor;
  31168. me.setOpt("enableMessageShow", true);
  31169. if (me.getOpt("enableMessageShow") === false) {
  31170. return;
  31171. }
  31172. me.addListener("ready", function() {
  31173. holder = document.getElementById(me.ui.id + "_message_holder");
  31174. updateHolderPos();
  31175. setTimeout(function() {
  31176. updateHolderPos();
  31177. }, 500);
  31178. });
  31179. me.addListener("showmessage", function(type, opt) {
  31180. opt = utils.isString(opt)
  31181. ? {
  31182. content: opt
  31183. }
  31184. : opt;
  31185. var message = new Message({
  31186. timeout: opt.timeout,
  31187. type: opt.type,
  31188. content: opt.content,
  31189. keepshow: opt.keepshow,
  31190. editor: me
  31191. }),
  31192. mid = opt.id || "msg_" + (+new Date()).toString(36);
  31193. message.render(holder);
  31194. _messageItems[mid] = message;
  31195. message.reset(opt);
  31196. updateHolderPos();
  31197. return mid;
  31198. });
  31199. me.addListener("updatemessage", function(type, id, opt) {
  31200. opt = utils.isString(opt)
  31201. ? {
  31202. content: opt
  31203. }
  31204. : opt;
  31205. var message = _messageItems[id];
  31206. message.render(holder);
  31207. message && message.reset(opt);
  31208. });
  31209. me.addListener("hidemessage", function(type, id) {
  31210. var message = _messageItems[id];
  31211. message && message.hide();
  31212. });
  31213. function updateHolderPos() {
  31214. if (!holder || !me.ui) return;
  31215. var toolbarbox = me.ui.getDom("toolbarbox");
  31216. if (toolbarbox) {
  31217. holder.style.top = toolbarbox.offsetHeight + 3 + "px";
  31218. }
  31219. holder.style.zIndex =
  31220. Math.max(me.options.zIndex, me.iframe.style.zIndex) + 1;
  31221. }
  31222. });
  31223. // adapter/autosave.js
  31224. UE.registerUI("autosave", function(editor) {
  31225. var timer = null,
  31226. uid = null;
  31227. editor.on("afterautosave", function() {
  31228. clearTimeout(timer);
  31229. timer = setTimeout(function() {
  31230. if (uid) {
  31231. editor.trigger("hidemessage", uid);
  31232. }
  31233. uid = editor.trigger("showmessage", {
  31234. content: editor.getLang("autosave.success"),
  31235. timeout: 2000
  31236. });
  31237. }, 2000);
  31238. });
  31239. });
  31240. })();